diff --git a/community/README.md b/community/README.md deleted file mode 100644 index f8395ce4582..00000000000 --- a/community/README.md +++ /dev/null @@ -1,60 +0,0 @@ -![Logo](https://storage.googleapis.com/tf_model_garden/tf_model_garden_logo.png) - -# TensorFlow Community Models - -This repository provides a curated list of the GitHub repositories with machine learning models and implementations powered by TensorFlow 2. - -**Note**: Contributing companies or individuals are responsible for maintaining their repositories. - -## Computer Vision - -### Image Recognition - -| Model | Paper | Features | Maintainer | -|-------|-------|----------|------------| -| [DenseNet 169](https://github.com/IntelAI/models/tree/master/benchmarks/image_recognition/tensorflow/densenet169) | [Densely Connected Convolutional Networks](https://arxiv.org/pdf/1608.06993) | • FP32 Inference | [Intel](https://github.com/IntelAI) | -| [Inception V3](https://github.com/IntelAI/models/tree/master/benchmarks/image_recognition/tensorflow/inceptionv3) | [Rethinking the Inception Architecture
for Computer Vision](https://arxiv.org/pdf/1512.00567.pdf) | • Int8 Inference
• FP32 Inference | [Intel](https://github.com/IntelAI) | -| [Inception V4](https://github.com/IntelAI/models/tree/master/benchmarks/image_recognition/tensorflow/inceptionv4) | [Inception-v4, Inception-ResNet and the Impact
of Residual Connections on Learning](https://arxiv.org/pdf/1602.07261) | • Int8 Inference
• FP32 Inference | [Intel](https://github.com/IntelAI) | -| [MobileNet V1](https://github.com/IntelAI/models/tree/master/benchmarks/image_recognition/tensorflow/mobilenet_v1) | [MobileNets: Efficient Convolutional Neural Networks
for Mobile Vision Applications](https://arxiv.org/pdf/1704.04861) | • Int8 Inference
• FP32 Inference | [Intel](https://github.com/IntelAI) | -| [ResNet 101](https://github.com/IntelAI/models/tree/master/benchmarks/image_recognition/tensorflow/resnet101) | [Deep Residual Learning for Image Recognition](https://arxiv.org/pdf/1512.03385) | • Int8 Inference
• FP32 Inference | [Intel](https://github.com/IntelAI) | -| [ResNet 50](https://github.com/IntelAI/models/tree/master/benchmarks/image_recognition/tensorflow/resnet50) | [Deep Residual Learning for Image Recognition](https://arxiv.org/pdf/1512.03385) | • Int8 Inference
• FP32 Inference | [Intel](https://github.com/IntelAI) | -| [ResNet 50v1.5](https://github.com/IntelAI/models/tree/master/benchmarks/image_recognition/tensorflow/resnet50v1_5) | [Deep Residual Learning for Image Recognition](https://arxiv.org/pdf/1512.03385) | • Int8 Inference
• FP32 Inference
• FP32 Training | [Intel](https://github.com/IntelAI) | -| EfficientNet [v1](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/Classification/ConvNets/efficientnet_v1) [v2](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/Classification/ConvNets/efficientnet_v2) | [EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks](https://arxiv.org/pdf/1905.11946.pdf) | • Automatic mixed precision
• Horovod Multi-GPU training (NCCL)
• Multi-node training on a Pyxis/Enroot Slurm cluster
• XLA | [NVIDIA](https://github.com/NVIDIA) | - -### Object Detection - -| Model | Paper | Features | Maintainer | -|-------|-------|----------|------------| -| [R-FCN](https://github.com/IntelAI/models/tree/master/benchmarks/object_detection/tensorflow/rfcn) | [R-FCN: Object Detection
via Region-based Fully Convolutional Networks](https://arxiv.org/pdf/1605.06409) | • Int8 Inference
• FP32 Inference | [Intel](https://github.com/IntelAI) | -| [SSD-MobileNet](https://github.com/IntelAI/models/tree/master/benchmarks/object_detection/tensorflow/ssd-mobilenet) | [MobileNets: Efficient Convolutional Neural Networks
for Mobile Vision Applications](https://arxiv.org/pdf/1704.04861) | • Int8 Inference
• FP32 Inference | [Intel](https://github.com/IntelAI) | -| [SSD-ResNet34](https://github.com/IntelAI/models/tree/master/benchmarks/object_detection/tensorflow/ssd-resnet34) | [SSD: Single Shot MultiBox Detector](https://arxiv.org/pdf/1512.02325) | • Int8 Inference
• FP32 Inference
• FP32 Training | [Intel](https://github.com/IntelAI) | - -### Segmentation - -| Model | Paper | Features | Maintainer | -|-------|-------|----------|------------| -| [Mask R-CNN](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/Segmentation/MaskRCNN) | [Mask R-CNN](https://arxiv.org/abs/1703.06870) | • Automatic Mixed Precision
• Multi-GPU training support with Horovod
• TensorRT | [NVIDIA](https://github.com/NVIDIA) | -| [U-Net Medical Image Segmentation](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/Segmentation/UNet_Medical) | [U-Net: Convolutional Networks for Biomedical Image Segmentation](https://arxiv.org/abs/1505.04597) | • Automatic Mixed Precision
• Multi-GPU training support with Horovod
• TensorRT | [NVIDIA](https://github.com/NVIDIA) | - -## Natural Language Processing - -| Model | Paper | Features | Maintainer | -|-------|-------|----------|------------| -| [BERT](https://github.com/IntelAI/models/tree/master/benchmarks/language_modeling/tensorflow/bert_large) | [BERT: Pre-training of Deep Bidirectional Transformers
for Language Understanding](https://arxiv.org/pdf/1810.04805) | • FP32 Inference
• FP32 Training | [Intel](https://github.com/IntelAI) | -| [BERT](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/LanguageModeling/BERT) | [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/pdf/1810.04805) | • Horovod Multi-GPU
• Multi-node with Horovod and Pyxis/Enroot Slurm cluster
• XLA
• Automatic mixed precision
• LAMB | [NVIDIA](https://github.com/NVIDIA) | -| [ELECTRA](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/LanguageModeling/ELECTRA) | [ELECTRA: Pre-training Text Encoders as Discriminators Rather Than Generators](https://openreview.net/forum?id=r1xMH1BtvB) | • Automatic Mixed Precision
• Multi-GPU training support with Horovod
• Multi-node training on a Pyxis/Enroot Slurm cluster | [NVIDIA](https://github.com/NVIDIA) | -| [GNMT](https://github.com/IntelAI/models/tree/master/benchmarks/language_translation/tensorflow/mlperf_gnmt) | [Google’s Neural Machine Translation System:
Bridging the Gap between Human and Machine Translation](https://arxiv.org/pdf/1609.08144) | • FP32 Inference | [Intel](https://github.com/IntelAI) | -| [Transformer-LT (Official)](https://github.com/IntelAI/models/tree/master/benchmarks/language_translation/tensorflow/transformer_lt_official) | [Attention Is All You Need](https://arxiv.org/pdf/1706.03762) | • FP32 Inference | [Intel](https://github.com/IntelAI) | -| [Transformer-LT (MLPerf)](https://github.com/IntelAI/models/tree/master/benchmarks/language_translation/tensorflow/transformer_mlperf) | [Attention Is All You Need](https://arxiv.org/pdf/1706.03762) | • FP32 Training | [Intel](https://github.com/IntelAI) | - -## Recommendation Systems - -| Model | Paper | Features | Maintainer | -|-------|-------|----------|------------| -| [Wide & Deep](https://github.com/IntelAI/models/tree/master/benchmarks/recommendation/tensorflow/wide_deep_large_ds) | [Wide & Deep Learning for Recommender Systems](https://arxiv.org/pdf/1606.07792) | • FP32 Inference
• FP32 Training | [Intel](https://github.com/IntelAI) | -| [Wide & Deep](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/Recommendation/WideAndDeep) | [Wide & Deep Learning for Recommender Systems](https://arxiv.org/pdf/1606.07792) | • Automatic mixed precision
• Multi-GPU training support with Horovod
• XLA | [NVIDIA](https://github.com/NVIDIA) | -| [DLRM](https://github.com/NVIDIA/DeepLearningExamples/tree/master/TensorFlow2/Recommendation/DLRM) | [Deep Learning Recommendation Model for Personalization and Recommendation Systems](https://arxiv.org/pdf/1906.00091.pdf) | • Automatic Mixed Precision
• Hybrid-parallel multiGPU training using Horovod all2all
• Multinode training for Pyxis/Enroot Slurm clusters
• XLA
• Criteo dataset preprocessing with Spark on GPU | [NVIDIA](https://github.com/NVIDIA) | - -## Contributions - -If you want to contribute, please review the [contribution guidelines](https://github.com/tensorflow/models/wiki/How-to-contribute). diff --git a/official/pip_package/setup.py b/official/pip_package/setup.py index d085dbf0235..7209b42ad21 100644 --- a/official/pip_package/setup.py +++ b/official/pip_package/setup.py @@ -20,8 +20,8 @@ from setuptools import find_packages from setuptools import setup -version = '2.11.0' -tf_version = '2.11.0' # Major version. +version = '2.12.0' +tf_version = '2.12.0' # Major version. project_name = 'tf-models-official' diff --git a/research/README.md b/research/README.md deleted file mode 100644 index 808ad28384d..00000000000 --- a/research/README.md +++ /dev/null @@ -1,79 +0,0 @@ -![Logo](https://storage.googleapis.com/tf_model_garden/tf_model_garden_logo.png) - -# TensorFlow Research Models - -This directory contains code implementations and pre-trained models of published research papers. - -The research models are maintained by their respective authors. - -## Table of Contents -- [TensorFlow Research Models](#tensorflow-research-models) - - [Table of Contents](#table-of-contents) - - [Modeling Libraries and Models](#modeling-libraries-and-models) - - [Models and Implementations](#models-and-implementations) - - [Computer Vision](#computer-vision) - - [Natural Language Processing](#natural-language-processing) - - [Audio and Speech](#audio-and-speech) - - [Reinforcement Learning](#reinforcement-learning) - - [Others](#others) - - [Old Models and Implementations in TensorFlow 1](#old-models-and-implementations-in-tensorflow-1) - - [Contributions](#contributions) - -## Modeling Libraries and Models - -| Directory | Name | Description | Maintainer(s) | -|-----------|------|-------------|---------------| -| [object_detection](object_detection) | TensorFlow Object Detection API | A framework that makes it easy to construct, train and deploy object detection models

A collection of object detection models pre-trained on the COCO dataset, the Kitti dataset, the Open Images dataset, the AVA v2.1 dataset, and the iNaturalist Species Detection Dataset| jch1, tombstone, pkulzc | -| [slim](slim) | TensorFlow-Slim Image Classification Model Library | A lightweight high-level API of TensorFlow for defining, training and evaluating image classification models
• Inception V1/V2/V3/V4
• Inception-ResNet-v2
• ResNet V1/V2
• VGG 16/19
• MobileNet V1/V2/V3
• NASNet-A_Mobile/Large
• PNASNet-5_Large/Mobile | sguada, marksandler2 | - -## Models and Implementations - -### Computer Vision - -| Directory | Paper(s) | Conference | Maintainer(s) | -|-----------|----------|------------|---------------| -| [attention_ocr](attention_ocr) | [Attention-based Extraction of Structured Information from Street View Imagery](https://arxiv.org/abs/1704.03549) | ICDAR 2017 | xavigibert | -| [autoaugment](autoaugment) | [1] [AutoAugment](https://arxiv.org/abs/1805.09501)
[2] [Wide Residual Networks](https://arxiv.org/abs/1605.07146)
[3] [Shake-Shake regularization](https://arxiv.org/abs/1705.07485)
[4] [ShakeDrop Regularization for Deep Residual Learning](https://arxiv.org/abs/1802.02375) | [1] CVPR 2019
[2] BMVC 2016
[3] ICLR 2017
[4] ICLR 2018 | barretzoph | -| [deeplab](deeplab) | [1] [DeepLabv1: Semantic Image Segmentation with Deep Convolutional Nets and Fully Connected CRFs](https://arxiv.org/abs/1412.7062)
[2] [DeepLabv2: Semantic Image Segmentation with Deep Convolutional Nets, Atrous Convolution, and Fully Connected CRFs](https://arxiv.org/abs/1606.00915)
[3] [DeepLabv3: Rethinking Atrous Convolution for Semantic Image Segmentation](https://arxiv.org/abs/1706.05587)
[4] [DeepLabv3+: Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation](https://arxiv.org/abs/1802.02611)
| [1] ICLR 2015
[2] TPAMI 2017
[4] ECCV 2018 | aquariusjay, yknzhu | -| [delf](delf) | [1] DELF (DEep Local Features): [Large-Scale Image Retrieval with Attentive Deep Local Features](https://arxiv.org/abs/1612.06321)
[2] [Detect-to-Retrieve: Efficient Regional Aggregation for Image Search](https://arxiv.org/abs/1812.01584)
[3] DELG (DEep Local and Global features): [Unifying Deep Local and Global Features for Image Search](https://arxiv.org/abs/2001.05027)
[4] GLDv2: [Google Landmarks Dataset v2 -- A Large-Scale Benchmark for Instance-Level Recognition and Retrieval](https://arxiv.org/abs/2004.01804) | [1] ICCV 2017
[2] CVPR 2019
[4] CVPR 2020 | andrefaraujo | -| [lstm_object_detection](lstm_object_detection) | [Mobile Video Object Detection with Temporally-Aware Feature Maps](https://arxiv.org/abs/1711.06368) | CVPR 2018 | yinxiaoli, yongzhe2160, lzyuan | -| [marco](marco) | MARCO: [Classification of crystallization outcomes using deep convolutional neural networks](https://arxiv.org/abs/1803.10342) | | vincentvanhoucke | -| [vid2depth](vid2depth) | [Unsupervised Learning of Depth and Ego-Motion from Monocular Video Using 3D Geometric Constraints](https://arxiv.org/abs/1802.05522) | CVPR 2018 | rezama | - -### Natural Language Processing - -| Directory | Paper(s) | Conference | Maintainer(s) | -|-----------|----------|------------|---------------| -| [adversarial_text](adversarial_text) | [1] [Adversarial Training Methods for Semi-Supervised Text](https://arxiv.org/abs/1605.07725) Classification
[2] [Semi-supervised Sequence Learning](https://arxiv.org/abs/1511.01432) | [1] ICLR 2017
[2] NIPS 2015 | rsepassi, a-dai | -| [cvt_text](cvt_text) | [Semi-Supervised Sequence Modeling with Cross-View Training](https://arxiv.org/abs/1809.08370) | EMNLP 2018 | clarkkev, lmthang | - -### Audio and Speech - -| Directory | Paper(s) | Conference | Maintainer(s) | -|-----------|----------|------------|---------------| -| [audioset](audioset) | [1] [Audio Set: An ontology and human-labeled dataset for audio events](https://research.google/pubs/pub45857/)
[2] [CNN Architectures for Large-Scale Audio Classification](https://research.google/pubs/pub45611/) | ICASSP 2017 | plakal, dpwe | -| [deep_speech](deep_speech) | [Deep Speech 2](https://arxiv.org/abs/1512.02595) | ICLR 2016 | yhliang2018 | - -### Reinforcement Learning - -| Directory | Paper(s) | Conference | Maintainer(s) | -|-----------|----------|------------|---------------| -| [efficient-hrl](efficient-hrl) | [1] [Data-Efficient Hierarchical Reinforcement Learning](https://arxiv.org/abs/1805.08296)
[2] [Near-Optimal Representation Learning for Hierarchical Reinforcement Learning](https://arxiv.org/abs/1810.01257) | [1] NIPS 2018
[2] ICLR 2019 | ofirnachum | -| [pcl_rl](pcl_rl) | [1] [Improving Policy Gradient by Exploring Under-appreciated Rewards](https://arxiv.org/abs/1611.09321)
[2] [Bridging the Gap Between Value and Policy Based Reinforcement Learning](https://arxiv.org/abs/1702.08892)
[3] [Trust-PCL: An Off-Policy Trust Region Method for Continuous Control](https://arxiv.org/abs/1707.01891) | [1] ICLR 2017
[2] NIPS 2017
[3] ICLR 2018 | ofirnachum | - -### Others - -| Directory | Paper(s) | Conference | Maintainer(s) | -|-----------|----------|------------|---------------| -| [lfads](lfads) | [LFADS - Latent Factor Analysis via Dynamical Systems](https://arxiv.org/abs/1608.06315) | | jazcollins, sussillo | -| [rebar](rebar) | [REBAR: Low-variance, unbiased gradient estimates for discrete latent variable models](https://arxiv.org/abs/1703.07370) | NIPS 2017 | gjtucker | - -### Old Models and Implementations in TensorFlow 1 - -:warning: If you are looking for old models, please visit the [Archive branch](https://github.com/tensorflow/models/tree/archive/research). - ---- - -## Contributions - -If you want to contribute, please review the [contribution guidelines](https://github.com/tensorflow/models/wiki/How-to-contribute). diff --git a/research/adversarial_text/README.md b/research/adversarial_text/README.md deleted file mode 100644 index 643ed8b556f..00000000000 --- a/research/adversarial_text/README.md +++ /dev/null @@ -1,160 +0,0 @@ -![TensorFlow Requirement: 1.x](https://img.shields.io/badge/TensorFlow%20Requirement-1.x-brightgreen) -![TensorFlow 2 Not Supported](https://img.shields.io/badge/TensorFlow%202%20Not%20Supported-%E2%9C%95-red.svg) - -# Adversarial Text Classification - -Code for [*Adversarial Training Methods for Semi-Supervised Text Classification*](https://arxiv.org/abs/1605.07725) and [*Semi-Supervised Sequence Learning*](https://arxiv.org/abs/1511.01432). - -## Requirements - -* TensorFlow >= v1.3 - -## End-to-end IMDB Sentiment Classification - -### Fetch data - -```bash -$ wget http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz \ - -O /tmp/imdb.tar.gz -$ tar -xf /tmp/imdb.tar.gz -C /tmp -``` - -The directory `/tmp/aclImdb` contains the raw IMDB data. - -### Generate vocabulary - -```bash -$ IMDB_DATA_DIR=/tmp/imdb -$ python gen_vocab.py \ - --output_dir=$IMDB_DATA_DIR \ - --dataset=imdb \ - --imdb_input_dir=/tmp/aclImdb \ - --lowercase=False -``` - -Vocabulary and frequency files will be generated in `$IMDB_DATA_DIR`. - -###  Generate training, validation, and test data - -```bash -$ python gen_data.py \ - --output_dir=$IMDB_DATA_DIR \ - --dataset=imdb \ - --imdb_input_dir=/tmp/aclImdb \ - --lowercase=False \ - --label_gain=False -``` - -`$IMDB_DATA_DIR` contains TFRecords files. - -### Pretrain IMDB Language Model - -```bash -$ PRETRAIN_DIR=/tmp/models/imdb_pretrain -$ python pretrain.py \ - --train_dir=$PRETRAIN_DIR \ - --data_dir=$IMDB_DATA_DIR \ - --vocab_size=87007 \ - --embedding_dims=256 \ - --rnn_cell_size=1024 \ - --num_candidate_samples=1024 \ - --batch_size=256 \ - --learning_rate=0.001 \ - --learning_rate_decay_factor=0.9999 \ - --max_steps=100000 \ - --max_grad_norm=1.0 \ - --num_timesteps=400 \ - --keep_prob_emb=0.5 \ - --normalize_embeddings -``` - -`$PRETRAIN_DIR` contains checkpoints of the pretrained language model. - -### Train classifier - -Most flags stay the same, save for the removal of candidate sampling and the -addition of `pretrained_model_dir`, from which the classifier will load the -pretrained embedding and LSTM variables, and flags related to adversarial -training and classification. - -```bash -$ TRAIN_DIR=/tmp/models/imdb_classify -$ python train_classifier.py \ - --train_dir=$TRAIN_DIR \ - --pretrained_model_dir=$PRETRAIN_DIR \ - --data_dir=$IMDB_DATA_DIR \ - --vocab_size=87007 \ - --embedding_dims=256 \ - --rnn_cell_size=1024 \ - --cl_num_layers=1 \ - --cl_hidden_size=30 \ - --batch_size=64 \ - --learning_rate=0.0005 \ - --learning_rate_decay_factor=0.9998 \ - --max_steps=15000 \ - --max_grad_norm=1.0 \ - --num_timesteps=400 \ - --keep_prob_emb=0.5 \ - --normalize_embeddings \ - --adv_training_method=vat \ - --perturb_norm_length=5.0 -``` - -### Evaluate on test data - -```bash -$ EVAL_DIR=/tmp/models/imdb_eval -$ python evaluate.py \ - --eval_dir=$EVAL_DIR \ - --checkpoint_dir=$TRAIN_DIR \ - --eval_data=test \ - --run_once \ - --num_examples=25000 \ - --data_dir=$IMDB_DATA_DIR \ - --vocab_size=87007 \ - --embedding_dims=256 \ - --rnn_cell_size=1024 \ - --batch_size=256 \ - --num_timesteps=400 \ - --normalize_embeddings -``` - -## Code Overview - -The main entry points are the binaries listed below. Each training binary builds -a `VatxtModel`, defined in `graphs.py`, which in turn uses graph building blocks -defined in `inputs.py` (defines input data reading and parsing), `layers.py` -(defines core model components), and `adversarial_losses.py` (defines -adversarial training losses). The training loop itself is defined in -`train_utils.py`. - -### Binaries - -* Pretraining: `pretrain.py` -* Classifier Training: `train_classifier.py` -* Evaluation: `evaluate.py` - -### Command-Line Flags - -Flags related to distributed training and the training loop itself are defined -in [`train_utils.py`](https://github.com/tensorflow/models/tree/master/research/adversarial_text/train_utils.py). - -Flags related to model hyperparameters are defined in [`graphs.py`](https://github.com/tensorflow/models/tree/master/research/adversarial_text/graphs.py). - -Flags related to adversarial training are defined in [`adversarial_losses.py`](https://github.com/tensorflow/models/tree/master/research/adversarial_text/adversarial_losses.py). - -Flags particular to each job are defined in the main binary files. - -### Data Generation - -* Vocabulary generation: [`gen_vocab.py`](https://github.com/tensorflow/models/tree/master/research/adversarial_text/gen_vocab.py) -* Data generation: [`gen_data.py`](https://github.com/tensorflow/models/tree/master/research/adversarial_text/gen_data.py) - -Command-line flags defined in [`document_generators.py`](https://github.com/tensorflow/models/tree/master/research/adversarial_text/data/document_generators.py) -control which dataset is processed and how. - -## Contact for Issues - -* Ryan Sepassi, @rsepassi -* Andrew M. Dai, @a-dai -* Takeru Miyato, @takerum (Original implementation) diff --git a/research/adversarial_text/__init__.py b/research/adversarial_text/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/adversarial_text/adversarial_losses.py b/research/adversarial_text/adversarial_losses.py deleted file mode 100644 index 671315e8a99..00000000000 --- a/research/adversarial_text/adversarial_losses.py +++ /dev/null @@ -1,236 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Adversarial losses for text models.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# Dependency imports - -from six.moves import xrange -import tensorflow as tf - -flags = tf.app.flags -FLAGS = flags.FLAGS - -# Adversarial and virtual adversarial training parameters. -flags.DEFINE_float('perturb_norm_length', 5.0, - 'Norm length of adversarial perturbation to be ' - 'optimized with validation. ' - '5.0 is optimal on IMDB with virtual adversarial training. ') - -# Virtual adversarial training parameters -flags.DEFINE_integer('num_power_iteration', 1, 'The number of power iteration') -flags.DEFINE_float('small_constant_for_finite_diff', 1e-1, - 'Small constant for finite difference method') - -# Parameters for building the graph -flags.DEFINE_string('adv_training_method', None, - 'The flag which specifies training method. ' - '"" : non-adversarial training (e.g. for running the ' - ' semi-supervised sequence learning model) ' - '"rp" : random perturbation training ' - '"at" : adversarial training ' - '"vat" : virtual adversarial training ' - '"atvat" : at + vat ') -flags.DEFINE_float('adv_reg_coeff', 1.0, - 'Regularization coefficient of adversarial loss.') - - -def random_perturbation_loss(embedded, length, loss_fn): - """Adds noise to embeddings and recomputes classification loss.""" - noise = tf.random_normal(shape=tf.shape(embedded)) - perturb = _scale_l2(_mask_by_length(noise, length), FLAGS.perturb_norm_length) - return loss_fn(embedded + perturb) - - -def adversarial_loss(embedded, loss, loss_fn): - """Adds gradient to embedding and recomputes classification loss.""" - grad, = tf.gradients( - loss, - embedded, - aggregation_method=tf.AggregationMethod.EXPERIMENTAL_ACCUMULATE_N) - grad = tf.stop_gradient(grad) - perturb = _scale_l2(grad, FLAGS.perturb_norm_length) - return loss_fn(embedded + perturb) - - -def virtual_adversarial_loss(logits, embedded, inputs, - logits_from_embedding_fn): - """Virtual adversarial loss. - - Computes virtual adversarial perturbation by finite difference method and - power iteration, adds it to the embedding, and computes the KL divergence - between the new logits and the original logits. - - Args: - logits: 3-D float Tensor, [batch_size, num_timesteps, m], where m=1 if - num_classes=2, otherwise m=num_classes. - embedded: 3-D float Tensor, [batch_size, num_timesteps, embedding_dim]. - inputs: VatxtInput. - logits_from_embedding_fn: callable that takes embeddings and returns - classifier logits. - - Returns: - kl: float scalar. - """ - # Stop gradient of logits. See https://arxiv.org/abs/1507.00677 for details. - logits = tf.stop_gradient(logits) - - # Only care about the KL divergence on the final timestep. - weights = inputs.eos_weights - assert weights is not None - if FLAGS.single_label: - indices = tf.stack([tf.range(FLAGS.batch_size), inputs.length - 1], 1) - weights = tf.expand_dims(tf.gather_nd(inputs.eos_weights, indices), 1) - - # Initialize perturbation with random noise. - # shape(embedded) = (batch_size, num_timesteps, embedding_dim) - d = tf.random_normal(shape=tf.shape(embedded)) - - # Perform finite difference method and power iteration. - # See Eq.(8) in the paper http://arxiv.org/pdf/1507.00677.pdf, - # Adding small noise to input and taking gradient with respect to the noise - # corresponds to 1 power iteration. - for _ in xrange(FLAGS.num_power_iteration): - d = _scale_l2( - _mask_by_length(d, inputs.length), FLAGS.small_constant_for_finite_diff) - - d_logits = logits_from_embedding_fn(embedded + d) - kl = _kl_divergence_with_logits(logits, d_logits, weights) - d, = tf.gradients( - kl, - d, - aggregation_method=tf.AggregationMethod.EXPERIMENTAL_ACCUMULATE_N) - d = tf.stop_gradient(d) - - perturb = _scale_l2(d, FLAGS.perturb_norm_length) - vadv_logits = logits_from_embedding_fn(embedded + perturb) - return _kl_divergence_with_logits(logits, vadv_logits, weights) - - -def random_perturbation_loss_bidir(embedded, length, loss_fn): - """Adds noise to embeddings and recomputes classification loss.""" - noise = [tf.random_normal(shape=tf.shape(emb)) for emb in embedded] - masked = [_mask_by_length(n, length) for n in noise] - scaled = [_scale_l2(m, FLAGS.perturb_norm_length) for m in masked] - return loss_fn([e + s for (e, s) in zip(embedded, scaled)]) - - -def adversarial_loss_bidir(embedded, loss, loss_fn): - """Adds gradient to embeddings and recomputes classification loss.""" - grads = tf.gradients( - loss, - embedded, - aggregation_method=tf.AggregationMethod.EXPERIMENTAL_ACCUMULATE_N) - adv_exs = [ - emb + _scale_l2(tf.stop_gradient(g), FLAGS.perturb_norm_length) - for emb, g in zip(embedded, grads) - ] - return loss_fn(adv_exs) - - -def virtual_adversarial_loss_bidir(logits, embedded, inputs, - logits_from_embedding_fn): - """Virtual adversarial loss for bidirectional models.""" - logits = tf.stop_gradient(logits) - f_inputs, _ = inputs - weights = f_inputs.eos_weights - if FLAGS.single_label: - indices = tf.stack([tf.range(FLAGS.batch_size), f_inputs.length - 1], 1) - weights = tf.expand_dims(tf.gather_nd(f_inputs.eos_weights, indices), 1) - assert weights is not None - - perturbs = [ - _mask_by_length(tf.random_normal(shape=tf.shape(emb)), f_inputs.length) - for emb in embedded - ] - for _ in xrange(FLAGS.num_power_iteration): - perturbs = [ - _scale_l2(d, FLAGS.small_constant_for_finite_diff) for d in perturbs - ] - d_logits = logits_from_embedding_fn( - [emb + d for (emb, d) in zip(embedded, perturbs)]) - kl = _kl_divergence_with_logits(logits, d_logits, weights) - perturbs = tf.gradients( - kl, - perturbs, - aggregation_method=tf.AggregationMethod.EXPERIMENTAL_ACCUMULATE_N) - perturbs = [tf.stop_gradient(d) for d in perturbs] - - perturbs = [_scale_l2(d, FLAGS.perturb_norm_length) for d in perturbs] - vadv_logits = logits_from_embedding_fn( - [emb + d for (emb, d) in zip(embedded, perturbs)]) - return _kl_divergence_with_logits(logits, vadv_logits, weights) - - -def _mask_by_length(t, length): - """Mask t, 3-D [batch, time, dim], by length, 1-D [batch,].""" - maxlen = t.get_shape().as_list()[1] - - # Subtract 1 from length to prevent the perturbation from going on 'eos' - mask = tf.sequence_mask(length - 1, maxlen=maxlen) - mask = tf.expand_dims(tf.cast(mask, tf.float32), -1) - # shape(mask) = (batch, num_timesteps, 1) - return t * mask - - -def _scale_l2(x, norm_length): - # shape(x) = (batch, num_timesteps, d) - # Divide x by max(abs(x)) for a numerically stable L2 norm. - # 2norm(x) = a * 2norm(x/a) - # Scale over the full sequence, dims (1, 2) - alpha = tf.reduce_max(tf.abs(x), (1, 2), keep_dims=True) + 1e-12 - l2_norm = alpha * tf.sqrt( - tf.reduce_sum(tf.pow(x / alpha, 2), (1, 2), keep_dims=True) + 1e-6) - x_unit = x / l2_norm - return norm_length * x_unit - - -def _kl_divergence_with_logits(q_logits, p_logits, weights): - """Returns weighted KL divergence between distributions q and p. - - Args: - q_logits: logits for 1st argument of KL divergence shape - [batch_size, num_timesteps, num_classes] if num_classes > 2, and - [batch_size, num_timesteps] if num_classes == 2. - p_logits: logits for 2nd argument of KL divergence with same shape q_logits. - weights: 1-D float tensor with shape [batch_size, num_timesteps]. - Elements should be 1.0 only on end of sequences - - Returns: - KL: float scalar. - """ - # For logistic regression - if FLAGS.num_classes == 2: - q = tf.nn.sigmoid(q_logits) - kl = (-tf.nn.sigmoid_cross_entropy_with_logits(logits=q_logits, labels=q) + - tf.nn.sigmoid_cross_entropy_with_logits(logits=p_logits, labels=q)) - kl = tf.squeeze(kl, 2) - - # For softmax regression - else: - q = tf.nn.softmax(q_logits) - kl = tf.reduce_sum( - q * (tf.nn.log_softmax(q_logits) - tf.nn.log_softmax(p_logits)), -1) - - num_labels = tf.reduce_sum(weights) - num_labels = tf.where(tf.equal(num_labels, 0.), 1., num_labels) - - kl.get_shape().assert_has_rank(2) - weights.get_shape().assert_has_rank(2) - - loss = tf.identity(tf.reduce_sum(weights * kl) / num_labels, name='kl') - return loss diff --git a/research/adversarial_text/data/__init__.py b/research/adversarial_text/data/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/adversarial_text/data/data_utils.py b/research/adversarial_text/data/data_utils.py deleted file mode 100644 index 55d9e3a0922..00000000000 --- a/research/adversarial_text/data/data_utils.py +++ /dev/null @@ -1,332 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utilities for generating/preprocessing data for adversarial text models.""" - -import operator -import os -import random -import re - -# Dependency imports - -import tensorflow as tf - -EOS_TOKEN = '' - -# Data filenames -# Sequence Autoencoder -ALL_SA = 'all_sa.tfrecords' -TRAIN_SA = 'train_sa.tfrecords' -TEST_SA = 'test_sa.tfrecords' -# Language Model -ALL_LM = 'all_lm.tfrecords' -TRAIN_LM = 'train_lm.tfrecords' -TEST_LM = 'test_lm.tfrecords' -# Classification -TRAIN_CLASS = 'train_classification.tfrecords' -TEST_CLASS = 'test_classification.tfrecords' -VALID_CLASS = 'validate_classification.tfrecords' -# LM with bidirectional LSTM -TRAIN_REV_LM = 'train_reverse_lm.tfrecords' -TEST_REV_LM = 'test_reverse_lm.tfrecords' -# Classification with bidirectional LSTM -TRAIN_BD_CLASS = 'train_bidir_classification.tfrecords' -TEST_BD_CLASS = 'test_bidir_classification.tfrecords' -VALID_BD_CLASS = 'validate_bidir_classification.tfrecords' - - -class ShufflingTFRecordWriter(object): - """Thin wrapper around TFRecordWriter that shuffles records.""" - - def __init__(self, path): - self._path = path - self._records = [] - self._closed = False - - def write(self, record): - assert not self._closed - self._records.append(record) - - def close(self): - assert not self._closed - random.shuffle(self._records) - with tf.python_io.TFRecordWriter(self._path) as f: - for record in self._records: - f.write(record) - self._closed = True - - def __enter__(self): - return self - - def __exit__(self, unused_type, unused_value, unused_traceback): - self.close() - - -class Timestep(object): - """Represents a single timestep in a SequenceWrapper.""" - - def __init__(self, token, label, weight, multivalent_tokens=False): - """Constructs Timestep from empty Features.""" - self._token = token - self._label = label - self._weight = weight - self._multivalent_tokens = multivalent_tokens - self._fill_with_defaults() - - @property - def token(self): - if self._multivalent_tokens: - raise TypeError('Timestep may contain multiple values; use `tokens`') - return self._token.int64_list.value[0] - - @property - def tokens(self): - return self._token.int64_list.value - - @property - def label(self): - return self._label.int64_list.value[0] - - @property - def weight(self): - return self._weight.float_list.value[0] - - def set_token(self, token): - if self._multivalent_tokens: - raise TypeError('Timestep may contain multiple values; use `add_token`') - self._token.int64_list.value[0] = token - return self - - def add_token(self, token): - self._token.int64_list.value.append(token) - return self - - def set_label(self, label): - self._label.int64_list.value[0] = label - return self - - def set_weight(self, weight): - self._weight.float_list.value[0] = weight - return self - - def copy_from(self, timestep): - self.set_token(timestep.token).set_label(timestep.label).set_weight( - timestep.weight) - return self - - def _fill_with_defaults(self): - if not self._multivalent_tokens: - self._token.int64_list.value.append(0) - self._label.int64_list.value.append(0) - self._weight.float_list.value.append(0.0) - - -class SequenceWrapper(object): - """Wrapper around tf.SequenceExample.""" - - F_TOKEN_ID = 'token_id' - F_LABEL = 'label' - F_WEIGHT = 'weight' - - def __init__(self, multivalent_tokens=False): - self._seq = tf.train.SequenceExample() - self._flist = self._seq.feature_lists.feature_list - self._timesteps = [] - self._multivalent_tokens = multivalent_tokens - - @property - def seq(self): - return self._seq - - @property - def multivalent_tokens(self): - return self._multivalent_tokens - - @property - def _tokens(self): - return self._flist[SequenceWrapper.F_TOKEN_ID].feature - - @property - def _labels(self): - return self._flist[SequenceWrapper.F_LABEL].feature - - @property - def _weights(self): - return self._flist[SequenceWrapper.F_WEIGHT].feature - - def add_timestep(self): - timestep = Timestep( - self._tokens.add(), - self._labels.add(), - self._weights.add(), - multivalent_tokens=self._multivalent_tokens) - self._timesteps.append(timestep) - return timestep - - def __iter__(self): - for timestep in self._timesteps: - yield timestep - - def __len__(self): - return len(self._timesteps) - - def __getitem__(self, idx): - return self._timesteps[idx] - - -def build_reverse_sequence(seq): - """Builds a sequence that is the reverse of the input sequence.""" - reverse_seq = SequenceWrapper() - - # Copy all but last timestep - for timestep in reversed(seq[:-1]): - reverse_seq.add_timestep().copy_from(timestep) - - # Copy final timestep - reverse_seq.add_timestep().copy_from(seq[-1]) - - return reverse_seq - - -def build_bidirectional_seq(seq, rev_seq): - bidir_seq = SequenceWrapper(multivalent_tokens=True) - for forward_ts, reverse_ts in zip(seq, rev_seq): - bidir_seq.add_timestep().add_token(forward_ts.token).add_token( - reverse_ts.token) - - return bidir_seq - - -def build_lm_sequence(seq): - """Builds language model sequence from input sequence. - - Args: - seq: SequenceWrapper. - - Returns: - SequenceWrapper with `seq` tokens copied over to output sequence tokens and - labels (offset by 1, i.e. predict next token) with weights set to 1.0, - except for token. - """ - lm_seq = SequenceWrapper() - for i, timestep in enumerate(seq): - if i == len(seq) - 1: - lm_seq.add_timestep().set_token(timestep.token).set_label( - seq[i].token).set_weight(0.0) - else: - lm_seq.add_timestep().set_token(timestep.token).set_label( - seq[i + 1].token).set_weight(1.0) - return lm_seq - - -def build_seq_ae_sequence(seq): - """Builds seq_ae sequence from input sequence. - - Args: - seq: SequenceWrapper. - - Returns: - SequenceWrapper with `seq` inputs copied and concatenated, and with labels - copied in on the right-hand (i.e. decoder) side with weights set to 1.0. - The new sequence will have length `len(seq) * 2 - 1`, as the last timestep - of the encoder section and the first step of the decoder section will - overlap. - """ - seq_ae_seq = SequenceWrapper() - - for i in range(len(seq) * 2 - 1): - ts = seq_ae_seq.add_timestep() - - if i < len(seq) - 1: - # Encoder - ts.set_token(seq[i].token) - elif i == len(seq) - 1: - # Transition step - ts.set_token(seq[i].token) - ts.set_label(seq[0].token) - ts.set_weight(1.0) - else: - # Decoder - ts.set_token(seq[i % len(seq)].token) - ts.set_label(seq[(i + 1) % len(seq)].token) - ts.set_weight(1.0) - - return seq_ae_seq - - -def build_labeled_sequence(seq, class_label, label_gain=False): - """Builds labeled sequence from input sequence. - - Args: - seq: SequenceWrapper. - class_label: integer, starting from 0. - label_gain: bool. If True, class_label will be put on every timestep and - weight will increase linearly from 0 to 1. - - Returns: - SequenceWrapper with `seq` copied in and `class_label` added as label to - final timestep. - """ - label_seq = SequenceWrapper(multivalent_tokens=seq.multivalent_tokens) - - # Copy sequence without labels - seq_len = len(seq) - final_timestep = None - for i, timestep in enumerate(seq): - label_timestep = label_seq.add_timestep() - if seq.multivalent_tokens: - for token in timestep.tokens: - label_timestep.add_token(token) - else: - label_timestep.set_token(timestep.token) - if label_gain: - label_timestep.set_label(int(class_label)) - weight = 1.0 if seq_len < 2 else float(i) / (seq_len - 1) - label_timestep.set_weight(weight) - if i == (seq_len - 1): - final_timestep = label_timestep - - # Edit final timestep to have class label and weight = 1. - final_timestep.set_label(int(class_label)).set_weight(1.0) - - return label_seq - - -def split_by_punct(segment): - """Splits str segment by punctuation, filters our empties and spaces.""" - return [s for s in re.split(r'\W+', segment) if s and not s.isspace()] - - -def sort_vocab_by_frequency(vocab_freq_map): - """Sorts vocab_freq_map by count. - - Args: - vocab_freq_map: dict, vocabulary terms with counts. - - Returns: - list> sorted by count, descending. - """ - return sorted( - vocab_freq_map.items(), key=operator.itemgetter(1), reverse=True) - - -def write_vocab_and_frequency(ordered_vocab_freqs, output_dir): - """Writes ordered_vocab_freqs into vocab.txt and vocab_freq.txt.""" - tf.gfile.MakeDirs(output_dir) - with open(os.path.join(output_dir, 'vocab.txt'), 'w', encoding='utf-8') as vocab_f: - with open(os.path.join(output_dir, 'vocab_freq.txt'), 'w', encoding='utf-8') as freq_f: - for word, freq in ordered_vocab_freqs: - vocab_f.write('{}\n'.format(word)) - freq_f.write('{}\n'.format(freq)) diff --git a/research/adversarial_text/data/data_utils_test.py b/research/adversarial_text/data/data_utils_test.py deleted file mode 100644 index 7d225ef08c0..00000000000 --- a/research/adversarial_text/data/data_utils_test.py +++ /dev/null @@ -1,200 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for data_utils.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# Dependency imports - -import tensorflow as tf - -from data import data_utils - -data = data_utils - - -class SequenceWrapperTest(tf.test.TestCase): - - def testDefaultTimesteps(self): - seq = data.SequenceWrapper() - t1 = seq.add_timestep() - _ = seq.add_timestep() - self.assertEqual(len(seq), 2) - - self.assertEqual(t1.weight, 0.0) - self.assertEqual(t1.label, 0) - self.assertEqual(t1.token, 0) - - def testSettersAndGetters(self): - ts = data.SequenceWrapper().add_timestep() - ts.set_token(3) - ts.set_label(4) - ts.set_weight(2.0) - self.assertEqual(ts.token, 3) - self.assertEqual(ts.label, 4) - self.assertEqual(ts.weight, 2.0) - - def testTimestepIteration(self): - seq = data.SequenceWrapper() - seq.add_timestep().set_token(0) - seq.add_timestep().set_token(1) - seq.add_timestep().set_token(2) - for i, ts in enumerate(seq): - self.assertEqual(ts.token, i) - - def testFillsSequenceExampleCorrectly(self): - seq = data.SequenceWrapper() - seq.add_timestep().set_token(1).set_label(2).set_weight(3.0) - seq.add_timestep().set_token(10).set_label(20).set_weight(30.0) - - seq_ex = seq.seq - fl = seq_ex.feature_lists.feature_list - fl_token = fl[data.SequenceWrapper.F_TOKEN_ID].feature - fl_label = fl[data.SequenceWrapper.F_LABEL].feature - fl_weight = fl[data.SequenceWrapper.F_WEIGHT].feature - _ = [self.assertEqual(len(f), 2) for f in [fl_token, fl_label, fl_weight]] - self.assertAllEqual([f.int64_list.value[0] for f in fl_token], [1, 10]) - self.assertAllEqual([f.int64_list.value[0] for f in fl_label], [2, 20]) - self.assertAllEqual([f.float_list.value[0] for f in fl_weight], [3.0, 30.0]) - - -class DataUtilsTest(tf.test.TestCase): - - def testSplitByPunct(self): - output = data.split_by_punct( - 'hello! world, i\'ve been\nwaiting\tfor\ryou for.a long time') - expected = [ - 'hello', 'world', 'i', 've', 'been', 'waiting', 'for', 'you', 'for', - 'a', 'long', 'time' - ] - self.assertListEqual(output, expected) - - def _buildDummySequence(self): - seq = data.SequenceWrapper() - for i in range(10): - seq.add_timestep().set_token(i) - return seq - - def testBuildLMSeq(self): - seq = self._buildDummySequence() - lm_seq = data.build_lm_sequence(seq) - for i, ts in enumerate(lm_seq): - # For end of sequence, the token and label should be same, and weight - # should be 0.0. - if i == len(lm_seq) - 1: - self.assertEqual(ts.token, i) - self.assertEqual(ts.label, i) - self.assertEqual(ts.weight, 0.0) - else: - self.assertEqual(ts.token, i) - self.assertEqual(ts.label, i + 1) - self.assertEqual(ts.weight, 1.0) - - def testBuildSAESeq(self): - seq = self._buildDummySequence() - sa_seq = data.build_seq_ae_sequence(seq) - - self.assertEqual(len(sa_seq), len(seq) * 2 - 1) - - # Tokens should be sequence twice, minus the EOS token at the end - for i, ts in enumerate(sa_seq): - self.assertEqual(ts.token, seq[i % 10].token) - - # Weights should be len-1 0.0's and len 1.0's. - for i in range(len(seq) - 1): - self.assertEqual(sa_seq[i].weight, 0.0) - for i in range(len(seq) - 1, len(sa_seq)): - self.assertEqual(sa_seq[i].weight, 1.0) - - # Labels should be len-1 0's, and then the sequence - for i in range(len(seq) - 1): - self.assertEqual(sa_seq[i].label, 0) - for i in range(len(seq) - 1, len(sa_seq)): - self.assertEqual(sa_seq[i].label, seq[i - (len(seq) - 1)].token) - - def testBuildLabelSeq(self): - seq = self._buildDummySequence() - eos_id = len(seq) - 1 - label_seq = data.build_labeled_sequence(seq, True) - for i, ts in enumerate(label_seq[:-1]): - self.assertEqual(ts.token, i) - self.assertEqual(ts.label, 0) - self.assertEqual(ts.weight, 0.0) - - final_timestep = label_seq[-1] - self.assertEqual(final_timestep.token, eos_id) - self.assertEqual(final_timestep.label, 1) - self.assertEqual(final_timestep.weight, 1.0) - - def testBuildBidirLabelSeq(self): - seq = self._buildDummySequence() - reverse_seq = data.build_reverse_sequence(seq) - bidir_seq = data.build_bidirectional_seq(seq, reverse_seq) - label_seq = data.build_labeled_sequence(bidir_seq, True) - - for (i, ts), j in zip( - enumerate(label_seq[:-1]), reversed(range(len(seq) - 1))): - self.assertAllEqual(ts.tokens, [i, j]) - self.assertEqual(ts.label, 0) - self.assertEqual(ts.weight, 0.0) - - final_timestep = label_seq[-1] - eos_id = len(seq) - 1 - self.assertAllEqual(final_timestep.tokens, [eos_id, eos_id]) - self.assertEqual(final_timestep.label, 1) - self.assertEqual(final_timestep.weight, 1.0) - - def testReverseSeq(self): - seq = self._buildDummySequence() - reverse_seq = data.build_reverse_sequence(seq) - for i, ts in enumerate(reversed(reverse_seq[:-1])): - self.assertEqual(ts.token, i) - self.assertEqual(ts.label, 0) - self.assertEqual(ts.weight, 0.0) - - final_timestep = reverse_seq[-1] - eos_id = len(seq) - 1 - self.assertEqual(final_timestep.token, eos_id) - self.assertEqual(final_timestep.label, 0) - self.assertEqual(final_timestep.weight, 0.0) - - def testBidirSeq(self): - seq = self._buildDummySequence() - reverse_seq = data.build_reverse_sequence(seq) - bidir_seq = data.build_bidirectional_seq(seq, reverse_seq) - for (i, ts), j in zip( - enumerate(bidir_seq[:-1]), reversed(range(len(seq) - 1))): - self.assertAllEqual(ts.tokens, [i, j]) - self.assertEqual(ts.label, 0) - self.assertEqual(ts.weight, 0.0) - - final_timestep = bidir_seq[-1] - eos_id = len(seq) - 1 - self.assertAllEqual(final_timestep.tokens, [eos_id, eos_id]) - self.assertEqual(final_timestep.label, 0) - self.assertEqual(final_timestep.weight, 0.0) - - def testLabelGain(self): - seq = self._buildDummySequence() - label_seq = data.build_labeled_sequence(seq, True, label_gain=True) - for i, ts in enumerate(label_seq): - self.assertEqual(ts.token, i) - self.assertEqual(ts.label, 1) - self.assertNear(ts.weight, float(i) / (len(seq) - 1), 1e-3) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/adversarial_text/data/document_generators.py b/research/adversarial_text/data/document_generators.py deleted file mode 100644 index 00d515bff7b..00000000000 --- a/research/adversarial_text/data/document_generators.py +++ /dev/null @@ -1,383 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Input readers and document/token generators for datasets.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from collections import namedtuple -import csv -import os -import random - -# Dependency imports - -import tensorflow as tf - -from data import data_utils - -flags = tf.app.flags -FLAGS = flags.FLAGS - -flags.DEFINE_string('dataset', '', 'Which dataset to generate data for') - -# Preprocessing config -flags.DEFINE_boolean('output_unigrams', True, 'Whether to output unigrams.') -flags.DEFINE_boolean('output_bigrams', False, 'Whether to output bigrams.') -flags.DEFINE_boolean('output_char', False, 'Whether to output characters.') -flags.DEFINE_boolean('lowercase', True, 'Whether to lowercase document terms.') - -# IMDB -flags.DEFINE_string('imdb_input_dir', '', 'The input directory containing the ' - 'IMDB sentiment dataset.') -flags.DEFINE_integer('imdb_validation_pos_start_id', 10621, 'File id of the ' - 'first file in the pos sentiment validation set.') -flags.DEFINE_integer('imdb_validation_neg_start_id', 10625, 'File id of the ' - 'first file in the neg sentiment validation set.') - -# DBpedia -flags.DEFINE_string('dbpedia_input_dir', '', - 'Path to DBpedia directory containing train.csv and ' - 'test.csv.') - -# Reuters Corpus (rcv1) -flags.DEFINE_string('rcv1_input_dir', '', - 'Path to rcv1 directory containing train.csv, unlab.csv, ' - 'and test.csv.') - -# Rotten Tomatoes -flags.DEFINE_string('rt_input_dir', '', - 'The Rotten Tomatoes dataset input directory.') - -# The amazon reviews input file to use in either the RT or IMDB datasets. -flags.DEFINE_string('amazon_unlabeled_input_file', '', - 'The unlabeled Amazon Reviews dataset input file. If set, ' - 'the input file is used to augment RT and IMDB vocab.') - -Document = namedtuple('Document', - 'content is_validation is_test label add_tokens') - - -def documents(dataset='train', - include_unlabeled=False, - include_validation=False): - """Generates Documents based on FLAGS.dataset. - - Args: - dataset: str, identifies folder within IMDB data directory, test or train. - include_unlabeled: bool, whether to include the unsup directory. Only valid - when dataset=train. - include_validation: bool, whether to include validation data. - - Yields: - Document - - Raises: - ValueError: if include_unlabeled is true but dataset is not 'train' - """ - - if include_unlabeled and dataset != 'train': - raise ValueError('If include_unlabeled=True, must use train dataset') - - # Set the random seed so that we have the same validation set when running - # gen_data and gen_vocab. - random.seed(302) - - ds = FLAGS.dataset - if ds == 'imdb': - docs_gen = imdb_documents - elif ds == 'dbpedia': - docs_gen = dbpedia_documents - elif ds == 'rcv1': - docs_gen = rcv1_documents - elif ds == 'rt': - docs_gen = rt_documents - else: - raise ValueError('Unrecognized dataset %s' % FLAGS.dataset) - - for doc in docs_gen(dataset, include_unlabeled, include_validation): - yield doc - - -def tokens(doc): - """Given a Document, produces character or word tokens. - - Tokens can be either characters, or word-level tokens (unigrams and/or - bigrams). - - Args: - doc: Document to produce tokens from. - - Yields: - token - - Raises: - ValueError: if all FLAGS.{output_unigrams, output_bigrams, output_char} - are False. - """ - if not (FLAGS.output_unigrams or FLAGS.output_bigrams or FLAGS.output_char): - raise ValueError( - 'At least one of {FLAGS.output_unigrams, FLAGS.output_bigrams, ' - 'FLAGS.output_char} must be true') - - content = doc.content.strip() - if FLAGS.lowercase: - content = content.lower() - - if FLAGS.output_char: - for char in content: - yield char - - else: - tokens_ = data_utils.split_by_punct(content) - for i, token in enumerate(tokens_): - if FLAGS.output_unigrams: - yield token - - if FLAGS.output_bigrams: - previous_token = (tokens_[i - 1] if i > 0 else data_utils.EOS_TOKEN) - bigram = '_'.join([previous_token, token]) - yield bigram - if (i + 1) == len(tokens_): - bigram = '_'.join([token, data_utils.EOS_TOKEN]) - yield bigram - - -def imdb_documents(dataset='train', - include_unlabeled=False, - include_validation=False): - """Generates Documents for IMDB dataset. - - Data from http://ai.stanford.edu/~amaas/data/sentiment/ - - Args: - dataset: str, identifies folder within IMDB data directory, test or train. - include_unlabeled: bool, whether to include the unsup directory. Only valid - when dataset=train. - include_validation: bool, whether to include validation data. - - Yields: - Document - - Raises: - ValueError: if FLAGS.imdb_input_dir is empty. - """ - if not FLAGS.imdb_input_dir: - raise ValueError('Must provide FLAGS.imdb_input_dir') - - tf.logging.info('Generating IMDB documents...') - - def check_is_validation(filename, class_label): - if class_label is None: - return False - file_idx = int(filename.split('_')[0]) - is_pos_valid = (class_label and - file_idx >= FLAGS.imdb_validation_pos_start_id) - is_neg_valid = (not class_label and - file_idx >= FLAGS.imdb_validation_neg_start_id) - return is_pos_valid or is_neg_valid - - dirs = [(dataset + '/pos', True), (dataset + '/neg', False)] - if include_unlabeled: - dirs.append(('train/unsup', None)) - - for d, class_label in dirs: - for filename in os.listdir(os.path.join(FLAGS.imdb_input_dir, d)): - is_validation = check_is_validation(filename, class_label) - if is_validation and not include_validation: - continue - - with open(os.path.join(FLAGS.imdb_input_dir, d, filename), encoding='utf-8') as imdb_f: - content = imdb_f.read() - yield Document( - content=content, - is_validation=is_validation, - is_test=False, - label=class_label, - add_tokens=True) - - if FLAGS.amazon_unlabeled_input_file and include_unlabeled: - with open(FLAGS.amazon_unlabeled_input_file, encoding='utf-8') as rt_f: - for content in rt_f: - yield Document( - content=content, - is_validation=False, - is_test=False, - label=None, - add_tokens=False) - - -def dbpedia_documents(dataset='train', - include_unlabeled=False, - include_validation=False): - """Generates Documents for DBpedia dataset. - - Dataset linked to at https://github.com/zhangxiangxiao/Crepe. - - Args: - dataset: str, identifies the csv file within the DBpedia data directory, - test or train. - include_unlabeled: bool, unused. - include_validation: bool, whether to include validation data, which is a - randomly selected 10% of the data. - - Yields: - Document - - Raises: - ValueError: if FLAGS.dbpedia_input_dir is empty. - """ - del include_unlabeled - - if not FLAGS.dbpedia_input_dir: - raise ValueError('Must provide FLAGS.dbpedia_input_dir') - - tf.logging.info('Generating DBpedia documents...') - - with open(os.path.join(FLAGS.dbpedia_input_dir, dataset + '.csv')) as db_f: - reader = csv.reader(db_f) - for row in reader: - # 10% of the data is randomly held out - is_validation = random.randint(1, 10) == 1 - if is_validation and not include_validation: - continue - - content = row[1] + ' ' + row[2] - yield Document( - content=content, - is_validation=is_validation, - is_test=False, - label=int(row[0]) - 1, # Labels should start from 0 - add_tokens=True) - - -def rcv1_documents(dataset='train', - include_unlabeled=True, - include_validation=False): - # pylint:disable=line-too-long - """Generates Documents for Reuters Corpus (rcv1) dataset. - - Dataset described at - http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/lyrl2004_rcv1v2_README.htm - - Args: - dataset: str, identifies the csv file within the rcv1 data directory. - include_unlabeled: bool, whether to include the unlab file. Only valid - when dataset=train. - include_validation: bool, whether to include validation data, which is a - randomly selected 10% of the data. - - Yields: - Document - - Raises: - ValueError: if FLAGS.rcv1_input_dir is empty. - """ - # pylint:enable=line-too-long - - if not FLAGS.rcv1_input_dir: - raise ValueError('Must provide FLAGS.rcv1_input_dir') - - tf.logging.info('Generating rcv1 documents...') - - datasets = [dataset] - if include_unlabeled: - if dataset == 'train': - datasets.append('unlab') - for dset in datasets: - with open(os.path.join(FLAGS.rcv1_input_dir, dset + '.csv')) as db_f: - reader = csv.reader(db_f) - for row in reader: - # 10% of the data is randomly held out - is_validation = random.randint(1, 10) == 1 - if is_validation and not include_validation: - continue - - content = row[1] - yield Document( - content=content, - is_validation=is_validation, - is_test=False, - label=int(row[0]), - add_tokens=True) - - -def rt_documents(dataset='train', - include_unlabeled=True, - include_validation=False): - # pylint:disable=line-too-long - """Generates Documents for the Rotten Tomatoes dataset. - - Dataset available at http://www.cs.cornell.edu/people/pabo/movie-review-data/ - In this dataset, amazon reviews are used for the unlabeled data. - - Args: - dataset: str, identifies the data subdirectory. - include_unlabeled: bool, whether to include the unlabeled data. Only valid - when dataset=train. - include_validation: bool, whether to include validation data, which is a - randomly selected 10% of the data. - - Yields: - Document - - Raises: - ValueError: if FLAGS.rt_input_dir is empty. - """ - # pylint:enable=line-too-long - - if not FLAGS.rt_input_dir: - raise ValueError('Must provide FLAGS.rt_input_dir') - - tf.logging.info('Generating rt documents...') - - data_files = [] - input_filenames = os.listdir(FLAGS.rt_input_dir) - for inp_fname in input_filenames: - if inp_fname.endswith('.pos'): - data_files.append((os.path.join(FLAGS.rt_input_dir, inp_fname), True)) - elif inp_fname.endswith('.neg'): - data_files.append((os.path.join(FLAGS.rt_input_dir, inp_fname), False)) - if include_unlabeled and FLAGS.amazon_unlabeled_input_file: - data_files.append((FLAGS.amazon_unlabeled_input_file, None)) - - for filename, class_label in data_files: - with open(filename) as rt_f: - for content in rt_f: - if class_label is None: - # Process Amazon Review data for unlabeled dataset - if content.startswith('review/text'): - yield Document( - content=content, - is_validation=False, - is_test=False, - label=None, - add_tokens=False) - else: - # 10% of the data is randomly held out for the validation set and - # another 10% of it is randomly held out for the test set - random_int = random.randint(1, 10) - is_validation = random_int == 1 - is_test = random_int == 2 - if (is_test and dataset != 'test') or (is_validation and - not include_validation): - continue - - yield Document( - content=content, - is_validation=is_validation, - is_test=is_test, - label=class_label, - add_tokens=True) diff --git a/research/adversarial_text/evaluate.py b/research/adversarial_text/evaluate.py deleted file mode 100644 index d7ea8c0188f..00000000000 --- a/research/adversarial_text/evaluate.py +++ /dev/null @@ -1,140 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Evaluates text classification model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math -import time - -# Dependency imports - -import tensorflow as tf - -import graphs - -flags = tf.app.flags -FLAGS = flags.FLAGS - -flags.DEFINE_string('master', '', - 'BNS name prefix of the Tensorflow eval master, ' - 'or "local".') -flags.DEFINE_string('eval_dir', '/tmp/text_eval', - 'Directory where to write event logs.') -flags.DEFINE_string('eval_data', 'test', 'Specify which dataset is used. ' - '("train", "valid", "test") ') - -flags.DEFINE_string('checkpoint_dir', '/tmp/text_train', - 'Directory where to read model checkpoints.') -flags.DEFINE_integer('eval_interval_secs', 60, 'How often to run the eval.') -flags.DEFINE_integer('num_examples', 32, 'Number of examples to run.') -flags.DEFINE_bool('run_once', False, 'Whether to run eval only once.') - - -def restore_from_checkpoint(sess, saver): - """Restore model from checkpoint. - - Args: - sess: Session. - saver: Saver for restoring the checkpoint. - - Returns: - bool: Whether the checkpoint was found and restored - """ - ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir) - if not ckpt or not ckpt.model_checkpoint_path: - tf.logging.info('No checkpoint found at %s', FLAGS.checkpoint_dir) - return False - - saver.restore(sess, ckpt.model_checkpoint_path) - return True - - -def run_eval(eval_ops, summary_writer, saver): - """Runs evaluation over FLAGS.num_examples examples. - - Args: - eval_ops: dict - summary_writer: Summary writer. - saver: Saver. - - Returns: - dict, with value being the average over all examples. - """ - sv = tf.train.Supervisor( - logdir=FLAGS.eval_dir, saver=None, summary_op=None, summary_writer=None) - with sv.managed_session( - master=FLAGS.master, start_standard_services=False) as sess: - if not restore_from_checkpoint(sess, saver): - return - sv.start_queue_runners(sess) - - metric_names, ops = zip(*eval_ops.items()) - value_ops, update_ops = zip(*ops) - - value_ops_dict = dict(zip(metric_names, value_ops)) - - # Run update ops - num_batches = int(math.ceil(FLAGS.num_examples / FLAGS.batch_size)) - tf.logging.info('Running %d batches for evaluation.', num_batches) - for i in range(num_batches): - if (i + 1) % 10 == 0: - tf.logging.info('Running batch %d/%d...', i + 1, num_batches) - if (i + 1) % 50 == 0: - _log_values(sess, value_ops_dict) - sess.run(update_ops) - - _log_values(sess, value_ops_dict, summary_writer=summary_writer) - - -def _log_values(sess, value_ops, summary_writer=None): - """Evaluate, log, and write summaries of the eval metrics in value_ops.""" - metric_names, value_ops = zip(*value_ops.items()) - values = sess.run(value_ops) - - tf.logging.info('Eval metric values:') - summary = tf.summary.Summary() - for name, val in zip(metric_names, values): - summary.value.add(tag=name, simple_value=val) - tf.logging.info('%s = %.3f', name, val) - - if summary_writer is not None: - global_step_val = sess.run(tf.train.get_global_step()) - tf.logging.info('Finished eval for step ' + str(global_step_val)) - summary_writer.add_summary(summary, global_step_val) - - -def main(_): - tf.logging.set_verbosity(tf.logging.INFO) - tf.gfile.MakeDirs(FLAGS.eval_dir) - tf.logging.info('Building eval graph...') - output = graphs.get_model().eval_graph(FLAGS.eval_data) - eval_ops, moving_averaged_variables = output - - saver = tf.train.Saver(moving_averaged_variables) - summary_writer = tf.summary.FileWriter( - FLAGS.eval_dir, graph=tf.get_default_graph()) - - while True: - run_eval(eval_ops, summary_writer, saver) - if FLAGS.run_once: - break - time.sleep(FLAGS.eval_interval_secs) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/adversarial_text/gen_data.py b/research/adversarial_text/gen_data.py deleted file mode 100644 index 2c3de65b799..00000000000 --- a/research/adversarial_text/gen_data.py +++ /dev/null @@ -1,217 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Create TFRecord files of SequenceExample protos from dataset. - -Constructs 3 datasets: - 1. Labeled data for the LSTM classification model, optionally with label gain. - "*_classification.tfrecords" (for both unidirectional and bidirectional - models). - 2. Data for the unsupervised LM-LSTM model that predicts the next token. - "*_lm.tfrecords" (generates forward and reverse data). - 3. Data for the unsupervised SA-LSTM model that uses Seq2Seq. - "*_sa.tfrecords". -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import string - -# Dependency imports - -import tensorflow as tf - -from data import data_utils -from data import document_generators - -data = data_utils -flags = tf.app.flags -FLAGS = flags.FLAGS - -# Flags for input data are in document_generators.py -flags.DEFINE_string('vocab_file', '', 'Path to the vocabulary file. Defaults ' - 'to FLAGS.output_dir/vocab.txt.') -flags.DEFINE_string('output_dir', '', 'Path to save tfrecords.') - -# Config -flags.DEFINE_boolean('label_gain', False, - 'Enable linear label gain. If True, sentiment label will ' - 'be included at each timestep with linear weight ' - 'increase.') - - -def build_shuffling_tf_record_writer(fname): - return data.ShufflingTFRecordWriter(os.path.join(FLAGS.output_dir, fname)) - - -def build_tf_record_writer(fname): - return tf.python_io.TFRecordWriter(os.path.join(FLAGS.output_dir, fname)) - - -def build_input_sequence(doc, vocab_ids): - """Builds input sequence from file. - - Splits lines on whitespace. Treats punctuation as whitespace. For word-level - sequences, only keeps terms that are in the vocab. - - Terms are added as token in the SequenceExample. The EOS_TOKEN is also - appended. Label and weight features are set to 0. - - Args: - doc: Document (defined in `document_generators`) from which to build the - sequence. - vocab_ids: dict. - - Returns: - SequenceExampleWrapper. - """ - seq = data.SequenceWrapper() - for token in document_generators.tokens(doc): - if token in vocab_ids: - seq.add_timestep().set_token(vocab_ids[token]) - - # Add EOS token to end - seq.add_timestep().set_token(vocab_ids[data.EOS_TOKEN]) - - return seq - - -def make_vocab_ids(vocab_filename): - if FLAGS.output_char: - ret = dict([(char, i) for i, char in enumerate(string.printable)]) - ret[data.EOS_TOKEN] = len(string.printable) - return ret - else: - with open(vocab_filename, encoding='utf-8') as vocab_f: - return dict([(line.strip(), i) for i, line in enumerate(vocab_f)]) - - -def generate_training_data(vocab_ids, writer_lm_all, writer_seq_ae_all): - """Generates training data.""" - - # Construct training data writers - writer_lm = build_shuffling_tf_record_writer(data.TRAIN_LM) - writer_seq_ae = build_shuffling_tf_record_writer(data.TRAIN_SA) - writer_class = build_shuffling_tf_record_writer(data.TRAIN_CLASS) - writer_valid_class = build_tf_record_writer(data.VALID_CLASS) - writer_rev_lm = build_shuffling_tf_record_writer(data.TRAIN_REV_LM) - writer_bd_class = build_shuffling_tf_record_writer(data.TRAIN_BD_CLASS) - writer_bd_valid_class = build_shuffling_tf_record_writer(data.VALID_BD_CLASS) - - for doc in document_generators.documents( - dataset='train', include_unlabeled=True, include_validation=True): - input_seq = build_input_sequence(doc, vocab_ids) - if len(input_seq) < 2: - continue - rev_seq = data.build_reverse_sequence(input_seq) - lm_seq = data.build_lm_sequence(input_seq) - rev_lm_seq = data.build_lm_sequence(rev_seq) - seq_ae_seq = data.build_seq_ae_sequence(input_seq) - if doc.label is not None: - # Used for sentiment classification. - label_seq = data.build_labeled_sequence( - input_seq, - doc.label, - label_gain=(FLAGS.label_gain and not doc.is_validation)) - bd_label_seq = data.build_labeled_sequence( - data.build_bidirectional_seq(input_seq, rev_seq), - doc.label, - label_gain=(FLAGS.label_gain and not doc.is_validation)) - class_writer = writer_valid_class if doc.is_validation else writer_class - bd_class_writer = (writer_bd_valid_class - if doc.is_validation else writer_bd_class) - class_writer.write(label_seq.seq.SerializeToString()) - bd_class_writer.write(bd_label_seq.seq.SerializeToString()) - - # Write - lm_seq_ser = lm_seq.seq.SerializeToString() - seq_ae_seq_ser = seq_ae_seq.seq.SerializeToString() - writer_lm_all.write(lm_seq_ser) - writer_seq_ae_all.write(seq_ae_seq_ser) - if not doc.is_validation: - writer_lm.write(lm_seq_ser) - writer_rev_lm.write(rev_lm_seq.seq.SerializeToString()) - writer_seq_ae.write(seq_ae_seq_ser) - - # Close writers - writer_lm.close() - writer_seq_ae.close() - writer_class.close() - writer_valid_class.close() - writer_rev_lm.close() - writer_bd_class.close() - writer_bd_valid_class.close() - - -def generate_test_data(vocab_ids, writer_lm_all, writer_seq_ae_all): - """Generates test data.""" - # Construct test data writers - writer_lm = build_shuffling_tf_record_writer(data.TEST_LM) - writer_rev_lm = build_shuffling_tf_record_writer(data.TEST_REV_LM) - writer_seq_ae = build_shuffling_tf_record_writer(data.TEST_SA) - writer_class = build_tf_record_writer(data.TEST_CLASS) - writer_bd_class = build_shuffling_tf_record_writer(data.TEST_BD_CLASS) - - for doc in document_generators.documents( - dataset='test', include_unlabeled=False, include_validation=True): - input_seq = build_input_sequence(doc, vocab_ids) - if len(input_seq) < 2: - continue - rev_seq = data.build_reverse_sequence(input_seq) - lm_seq = data.build_lm_sequence(input_seq) - rev_lm_seq = data.build_lm_sequence(rev_seq) - seq_ae_seq = data.build_seq_ae_sequence(input_seq) - label_seq = data.build_labeled_sequence(input_seq, doc.label) - bd_label_seq = data.build_labeled_sequence( - data.build_bidirectional_seq(input_seq, rev_seq), doc.label) - - # Write - writer_class.write(label_seq.seq.SerializeToString()) - writer_bd_class.write(bd_label_seq.seq.SerializeToString()) - lm_seq_ser = lm_seq.seq.SerializeToString() - seq_ae_seq_ser = seq_ae_seq.seq.SerializeToString() - writer_lm.write(lm_seq_ser) - writer_rev_lm.write(rev_lm_seq.seq.SerializeToString()) - writer_seq_ae.write(seq_ae_seq_ser) - writer_lm_all.write(lm_seq_ser) - writer_seq_ae_all.write(seq_ae_seq_ser) - - # Close test writers - writer_lm.close() - writer_rev_lm.close() - writer_seq_ae.close() - writer_class.close() - writer_bd_class.close() - - -def main(_): - tf.logging.set_verbosity(tf.logging.INFO) - tf.logging.info('Assigning vocabulary ids...') - vocab_ids = make_vocab_ids( - FLAGS.vocab_file or os.path.join(FLAGS.output_dir, 'vocab.txt')) - - with build_shuffling_tf_record_writer(data.ALL_LM) as writer_lm_all: - with build_shuffling_tf_record_writer(data.ALL_SA) as writer_seq_ae_all: - - tf.logging.info('Generating training data...') - generate_training_data(vocab_ids, writer_lm_all, writer_seq_ae_all) - - tf.logging.info('Generating test data...') - generate_test_data(vocab_ids, writer_lm_all, writer_seq_ae_all) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/adversarial_text/gen_vocab.py b/research/adversarial_text/gen_vocab.py deleted file mode 100644 index 17b91864ce7..00000000000 --- a/research/adversarial_text/gen_vocab.py +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Generates vocabulary and term frequency files for datasets.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from six import iteritems - -from collections import defaultdict - -# Dependency imports - -import tensorflow as tf - -from data import data_utils -from data import document_generators - -flags = tf.app.flags -FLAGS = flags.FLAGS - -# Flags controlling input are in document_generators.py - -flags.DEFINE_string('output_dir', '', - 'Path to save vocab.txt and vocab_freq.txt.') - -flags.DEFINE_boolean('use_unlabeled', True, 'Whether to use the ' - 'unlabeled sentiment dataset in the vocabulary.') -flags.DEFINE_boolean('include_validation', False, 'Whether to include the ' - 'validation set in the vocabulary.') -flags.DEFINE_integer('doc_count_threshold', 1, 'The minimum number of ' - 'documents a word or bigram should occur in to keep ' - 'it in the vocabulary.') - -MAX_VOCAB_SIZE = 100 * 1000 - - -def fill_vocab_from_doc(doc, vocab_freqs, doc_counts): - """Fills vocabulary and doc counts with tokens from doc. - - Args: - doc: Document to read tokens from. - vocab_freqs: dict - doc_counts: dict - - Returns: - None - """ - doc_seen = set() - - for token in document_generators.tokens(doc): - if doc.add_tokens or token in vocab_freqs: - vocab_freqs[token] += 1 - if token not in doc_seen: - doc_counts[token] += 1 - doc_seen.add(token) - - -def main(_): - tf.logging.set_verbosity(tf.logging.INFO) - vocab_freqs = defaultdict(int) - doc_counts = defaultdict(int) - - # Fill vocabulary frequencies map and document counts map - for doc in document_generators.documents( - dataset='train', - include_unlabeled=FLAGS.use_unlabeled, - include_validation=FLAGS.include_validation): - fill_vocab_from_doc(doc, vocab_freqs, doc_counts) - - # Filter out low-occurring terms - vocab_freqs = dict((term, freq) for term, freq in iteritems(vocab_freqs) - if doc_counts[term] > FLAGS.doc_count_threshold) - - # Sort by frequency - ordered_vocab_freqs = data_utils.sort_vocab_by_frequency(vocab_freqs) - - # Limit vocab size - ordered_vocab_freqs = ordered_vocab_freqs[:MAX_VOCAB_SIZE] - - # Add EOS token - ordered_vocab_freqs.append((data_utils.EOS_TOKEN, 1)) - - # Write - tf.gfile.MakeDirs(FLAGS.output_dir) - data_utils.write_vocab_and_frequency(ordered_vocab_freqs, FLAGS.output_dir) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/adversarial_text/graphs.py b/research/adversarial_text/graphs.py deleted file mode 100644 index 9610a698dd0..00000000000 --- a/research/adversarial_text/graphs.py +++ /dev/null @@ -1,687 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Virtual adversarial text models.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import csv -import os - -# Dependency imports - -import tensorflow as tf - -import adversarial_losses as adv_lib -import inputs as inputs_lib -import layers as layers_lib - -flags = tf.app.flags -FLAGS = flags.FLAGS - -# Flags governing adversarial training are defined in adversarial_losses.py. - -# Classifier -flags.DEFINE_integer('num_classes', 2, 'Number of classes for classification') - -# Data path -flags.DEFINE_string('data_dir', '/tmp/IMDB', - 'Directory path to preprocessed text dataset.') -flags.DEFINE_string('vocab_freq_path', None, - 'Path to pre-calculated vocab frequency data. If ' - 'None, use FLAGS.data_dir/vocab_freq.txt.') -flags.DEFINE_integer('batch_size', 64, 'Size of the batch.') -flags.DEFINE_integer('num_timesteps', 100, 'Number of timesteps for BPTT') - -# Model architechture -flags.DEFINE_bool('bidir_lstm', False, 'Whether to build a bidirectional LSTM.') -flags.DEFINE_bool('single_label', True, 'Whether the sequence has a single ' - 'label, for optimization.') -flags.DEFINE_integer('rnn_num_layers', 1, 'Number of LSTM layers.') -flags.DEFINE_integer('rnn_cell_size', 512, - 'Number of hidden units in the LSTM.') -flags.DEFINE_integer('cl_num_layers', 1, - 'Number of hidden layers of classification model.') -flags.DEFINE_integer('cl_hidden_size', 30, - 'Number of hidden units in classification layer.') -flags.DEFINE_integer('num_candidate_samples', -1, - 'Num samples used in the sampled output layer.') -flags.DEFINE_bool('use_seq2seq_autoencoder', False, - 'If True, seq2seq auto-encoder is used to pretrain. ' - 'If False, standard language model is used.') - -# Vocabulary and embeddings -flags.DEFINE_integer('embedding_dims', 256, 'Dimensions of embedded vector.') -flags.DEFINE_integer('vocab_size', 86934, - 'The size of the vocaburary. This value ' - 'should be exactly same as the number of the ' - 'vocabulary used in dataset. Because the last ' - 'indexed vocabulary of the dataset preprocessed by ' - 'my preprocessed code, is always and here we ' - 'specify the with the the index.') -flags.DEFINE_bool('normalize_embeddings', True, - 'Normalize word embeddings by vocab frequency') - -# Optimization -flags.DEFINE_float('learning_rate', 0.001, 'Learning rate while fine-tuning.') -flags.DEFINE_float('learning_rate_decay_factor', 1.0, - 'Learning rate decay factor') -flags.DEFINE_boolean('sync_replicas', False, 'sync_replica or not') -flags.DEFINE_integer('replicas_to_aggregate', 1, - 'The number of replicas to aggregate') - -# Regularization -flags.DEFINE_float('max_grad_norm', 1.0, - 'Clip the global gradient norm to this value.') -flags.DEFINE_float('keep_prob_emb', 1.0, 'keep probability on embedding layer. ' - '0.5 is optimal on IMDB with virtual adversarial training.') -flags.DEFINE_float('keep_prob_lstm_out', 1.0, - 'keep probability on lstm output.') -flags.DEFINE_float('keep_prob_cl_hidden', 1.0, - 'keep probability on classification hidden layer') - - -def get_model(): - if FLAGS.bidir_lstm: - return VatxtBidirModel() - else: - return VatxtModel() - - -class VatxtModel(object): - """Constructs training and evaluation graphs. - - Main methods: `classifier_training()`, `language_model_training()`, - and `eval_graph()`. - - Variable reuse is a critical part of the model, both for sharing variables - between the language model and the classifier, and for reusing variables for - the adversarial loss calculation. To ensure correct variable reuse, all - variables are created in Keras-style layers, wherein stateful layers (i.e. - layers with variables) are represented as callable instances of the Layer - class. Each time the Layer instance is called, it is using the same variables. - - All Layers are constructed in the __init__ method and reused in the various - graph-building functions. - """ - - def __init__(self, cl_logits_input_dim=None): - self.global_step = tf.train.get_or_create_global_step() - self.vocab_freqs = _get_vocab_freqs() - - # Cache VatxtInput objects - self.cl_inputs = None - self.lm_inputs = None - - # Cache intermediate Tensors that are reused - self.tensors = {} - - # Construct layers which are reused in constructing the LM and - # Classification graphs. Instantiating them all once here ensures that - # variable reuse works correctly. - self.layers = {} - self.layers['embedding'] = layers_lib.Embedding( - FLAGS.vocab_size, FLAGS.embedding_dims, FLAGS.normalize_embeddings, - self.vocab_freqs, FLAGS.keep_prob_emb) - self.layers['lstm'] = layers_lib.LSTM( - FLAGS.rnn_cell_size, FLAGS.rnn_num_layers, FLAGS.keep_prob_lstm_out) - self.layers['lm_loss'] = layers_lib.SoftmaxLoss( - FLAGS.vocab_size, - FLAGS.num_candidate_samples, - self.vocab_freqs, - name='LM_loss') - - cl_logits_input_dim = cl_logits_input_dim or FLAGS.rnn_cell_size - self.layers['cl_logits'] = layers_lib.cl_logits_subgraph( - [FLAGS.cl_hidden_size] * FLAGS.cl_num_layers, cl_logits_input_dim, - FLAGS.num_classes, FLAGS.keep_prob_cl_hidden) - - @property - def pretrained_variables(self): - return (self.layers['embedding'].trainable_weights + - self.layers['lstm'].trainable_weights) - - def classifier_training(self): - loss = self.classifier_graph() - train_op = optimize(loss, self.global_step) - return train_op, loss, self.global_step - - def language_model_training(self): - loss = self.language_model_graph() - train_op = optimize(loss, self.global_step) - return train_op, loss, self.global_step - - def classifier_graph(self): - """Constructs classifier graph from inputs to classifier loss. - - * Caches the VatxtInput object in `self.cl_inputs` - * Caches tensors: `cl_embedded`, `cl_logits`, `cl_loss` - - Returns: - loss: scalar float. - """ - inputs = _inputs('train', pretrain=False) - self.cl_inputs = inputs - embedded = self.layers['embedding'](inputs.tokens) - self.tensors['cl_embedded'] = embedded - - _, next_state, logits, loss = self.cl_loss_from_embedding( - embedded, return_intermediates=True) - tf.summary.scalar('classification_loss', loss) - self.tensors['cl_logits'] = logits - self.tensors['cl_loss'] = loss - - if FLAGS.single_label: - indices = tf.stack([tf.range(FLAGS.batch_size), inputs.length - 1], 1) - labels = tf.expand_dims(tf.gather_nd(inputs.labels, indices), 1) - weights = tf.expand_dims(tf.gather_nd(inputs.weights, indices), 1) - else: - labels = inputs.labels - weights = inputs.weights - acc = layers_lib.accuracy(logits, labels, weights) - tf.summary.scalar('accuracy', acc) - - adv_loss = (self.adversarial_loss() * tf.constant( - FLAGS.adv_reg_coeff, name='adv_reg_coeff')) - tf.summary.scalar('adversarial_loss', adv_loss) - - total_loss = loss + adv_loss - - with tf.control_dependencies([inputs.save_state(next_state)]): - total_loss = tf.identity(total_loss) - tf.summary.scalar('total_classification_loss', total_loss) - return total_loss - - def language_model_graph(self, compute_loss=True): - """Constructs LM graph from inputs to LM loss. - - * Caches the VatxtInput object in `self.lm_inputs` - * Caches tensors: `lm_embedded` - - Args: - compute_loss: bool, whether to compute and return the loss or stop after - the LSTM computation. - - Returns: - loss: scalar float. - """ - inputs = _inputs('train', pretrain=True) - self.lm_inputs = inputs - return self._lm_loss(inputs, compute_loss=compute_loss) - - def _lm_loss(self, - inputs, - emb_key='lm_embedded', - lstm_layer='lstm', - lm_loss_layer='lm_loss', - loss_name='lm_loss', - compute_loss=True): - embedded = self.layers['embedding'](inputs.tokens) - self.tensors[emb_key] = embedded - lstm_out, next_state = self.layers[lstm_layer](embedded, inputs.state, - inputs.length) - if compute_loss: - loss = self.layers[lm_loss_layer]( - [lstm_out, inputs.labels, inputs.weights]) - with tf.control_dependencies([inputs.save_state(next_state)]): - loss = tf.identity(loss) - tf.summary.scalar(loss_name, loss) - - return loss - - def eval_graph(self, dataset='test'): - """Constructs classifier evaluation graph. - - Args: - dataset: the labeled dataset to evaluate, {'train', 'test', 'valid'}. - - Returns: - eval_ops: dict - var_restore_dict: dict mapping variable restoration names to variables. - Trainable variables will be mapped to their moving average names. - """ - inputs = _inputs(dataset, pretrain=False) - embedded = self.layers['embedding'](inputs.tokens) - _, next_state, logits, _ = self.cl_loss_from_embedding( - embedded, inputs=inputs, return_intermediates=True) - - if FLAGS.single_label: - indices = tf.stack([tf.range(FLAGS.batch_size), inputs.length - 1], 1) - labels = tf.expand_dims(tf.gather_nd(inputs.labels, indices), 1) - weights = tf.expand_dims(tf.gather_nd(inputs.weights, indices), 1) - else: - labels = inputs.labels - weights = inputs.weights - eval_ops = { - 'accuracy': - tf.contrib.metrics.streaming_accuracy( - layers_lib.predictions(logits), labels, weights) - } - - with tf.control_dependencies([inputs.save_state(next_state)]): - acc, acc_update = eval_ops['accuracy'] - acc_update = tf.identity(acc_update) - eval_ops['accuracy'] = (acc, acc_update) - - var_restore_dict = make_restore_average_vars_dict() - return eval_ops, var_restore_dict - - def cl_loss_from_embedding(self, - embedded, - inputs=None, - return_intermediates=False): - """Compute classification loss from embedding. - - Args: - embedded: 3-D float Tensor [batch_size, num_timesteps, embedding_dim] - inputs: VatxtInput, defaults to self.cl_inputs. - return_intermediates: bool, whether to return intermediate tensors or only - the final loss. - - Returns: - If return_intermediates is True: - lstm_out, next_state, logits, loss - Else: - loss - """ - if inputs is None: - inputs = self.cl_inputs - - lstm_out, next_state = self.layers['lstm'](embedded, inputs.state, - inputs.length) - if FLAGS.single_label: - indices = tf.stack([tf.range(FLAGS.batch_size), inputs.length - 1], 1) - lstm_out = tf.expand_dims(tf.gather_nd(lstm_out, indices), 1) - labels = tf.expand_dims(tf.gather_nd(inputs.labels, indices), 1) - weights = tf.expand_dims(tf.gather_nd(inputs.weights, indices), 1) - else: - labels = inputs.labels - weights = inputs.weights - logits = self.layers['cl_logits'](lstm_out) - loss = layers_lib.classification_loss(logits, labels, weights) - - if return_intermediates: - return lstm_out, next_state, logits, loss - else: - return loss - - def adversarial_loss(self): - """Compute adversarial loss based on FLAGS.adv_training_method.""" - - def random_perturbation_loss(): - return adv_lib.random_perturbation_loss(self.tensors['cl_embedded'], - self.cl_inputs.length, - self.cl_loss_from_embedding) - - def adversarial_loss(): - return adv_lib.adversarial_loss(self.tensors['cl_embedded'], - self.tensors['cl_loss'], - self.cl_loss_from_embedding) - - def virtual_adversarial_loss(): - """Computes virtual adversarial loss. - - Uses lm_inputs and constructs the language model graph if it hasn't yet - been constructed. - - Also ensures that the LM input states are saved for LSTM state-saving - BPTT. - - Returns: - loss: float scalar. - """ - if self.lm_inputs is None: - self.language_model_graph(compute_loss=False) - - def logits_from_embedding(embedded, return_next_state=False): - _, next_state, logits, _ = self.cl_loss_from_embedding( - embedded, inputs=self.lm_inputs, return_intermediates=True) - if return_next_state: - return next_state, logits - else: - return logits - - next_state, lm_cl_logits = logits_from_embedding( - self.tensors['lm_embedded'], return_next_state=True) - - va_loss = adv_lib.virtual_adversarial_loss( - lm_cl_logits, self.tensors['lm_embedded'], self.lm_inputs, - logits_from_embedding) - - with tf.control_dependencies([self.lm_inputs.save_state(next_state)]): - va_loss = tf.identity(va_loss) - - return va_loss - - def combo_loss(): - return adversarial_loss() + virtual_adversarial_loss() - - adv_training_methods = { - # Random perturbation - 'rp': random_perturbation_loss, - # Adversarial training - 'at': adversarial_loss, - # Virtual adversarial training - 'vat': virtual_adversarial_loss, - # Both at and vat - 'atvat': combo_loss, - '': lambda: tf.constant(0.), - None: lambda: tf.constant(0.), - } - - with tf.name_scope('adversarial_loss'): - return adv_training_methods[FLAGS.adv_training_method]() - - -class VatxtBidirModel(VatxtModel): - """Extension of VatxtModel that supports bidirectional input.""" - - def __init__(self): - super(VatxtBidirModel, - self).__init__(cl_logits_input_dim=FLAGS.rnn_cell_size * 2) - - # Reverse LSTM and LM loss for bidirectional models - self.layers['lstm_reverse'] = layers_lib.LSTM( - FLAGS.rnn_cell_size, - FLAGS.rnn_num_layers, - FLAGS.keep_prob_lstm_out, - name='LSTM_Reverse') - self.layers['lm_loss_reverse'] = layers_lib.SoftmaxLoss( - FLAGS.vocab_size, - FLAGS.num_candidate_samples, - self.vocab_freqs, - name='LM_loss_reverse') - - @property - def pretrained_variables(self): - variables = super(VatxtBidirModel, self).pretrained_variables - variables.extend(self.layers['lstm_reverse'].trainable_weights) - return variables - - def classifier_graph(self): - """Constructs classifier graph from inputs to classifier loss. - - * Caches the VatxtInput objects in `self.cl_inputs` - * Caches tensors: `cl_embedded` (tuple of forward and reverse), `cl_logits`, - `cl_loss` - - Returns: - loss: scalar float. - """ - inputs = _inputs('train', pretrain=False, bidir=True) - self.cl_inputs = inputs - f_inputs, _ = inputs - - # Embed both forward and reverse with a shared embedding - embedded = [self.layers['embedding'](inp.tokens) for inp in inputs] - self.tensors['cl_embedded'] = embedded - - _, next_states, logits, loss = self.cl_loss_from_embedding( - embedded, return_intermediates=True) - tf.summary.scalar('classification_loss', loss) - self.tensors['cl_logits'] = logits - self.tensors['cl_loss'] = loss - - acc = layers_lib.accuracy(logits, f_inputs.labels, f_inputs.weights) - tf.summary.scalar('accuracy', acc) - - adv_loss = (self.adversarial_loss() * tf.constant( - FLAGS.adv_reg_coeff, name='adv_reg_coeff')) - tf.summary.scalar('adversarial_loss', adv_loss) - - total_loss = loss + adv_loss - - - saves = [inp.save_state(state) for (inp, state) in zip(inputs, next_states)] - with tf.control_dependencies(saves): - total_loss = tf.identity(total_loss) - tf.summary.scalar('total_classification_loss', total_loss) - return total_loss - - def language_model_graph(self, compute_loss=True): - """Constructs forward and reverse LM graphs from inputs to LM losses. - - * Caches the VatxtInput objects in `self.lm_inputs` - * Caches tensors: `lm_embedded`, `lm_embedded_reverse` - - Args: - compute_loss: bool, whether to compute and return the loss or stop after - the LSTM computation. - - Returns: - loss: scalar float, sum of forward and reverse losses. - """ - inputs = _inputs('train', pretrain=True, bidir=True) - self.lm_inputs = inputs - f_inputs, r_inputs = inputs - f_loss = self._lm_loss(f_inputs, compute_loss=compute_loss) - r_loss = self._lm_loss( - r_inputs, - emb_key='lm_embedded_reverse', - lstm_layer='lstm_reverse', - lm_loss_layer='lm_loss_reverse', - loss_name='lm_loss_reverse', - compute_loss=compute_loss) - if compute_loss: - return f_loss + r_loss - - def eval_graph(self, dataset='test'): - """Constructs classifier evaluation graph. - - Args: - dataset: the labeled dataset to evaluate, {'train', 'test', 'valid'}. - - Returns: - eval_ops: dict - var_restore_dict: dict mapping variable restoration names to variables. - Trainable variables will be mapped to their moving average names. - """ - inputs = _inputs(dataset, pretrain=False, bidir=True) - embedded = [self.layers['embedding'](inp.tokens) for inp in inputs] - _, next_states, logits, _ = self.cl_loss_from_embedding( - embedded, inputs=inputs, return_intermediates=True) - f_inputs, _ = inputs - - eval_ops = { - 'accuracy': - tf.contrib.metrics.streaming_accuracy( - layers_lib.predictions(logits), f_inputs.labels, - f_inputs.weights) - } - - # Save states on accuracy update - saves = [inp.save_state(state) for (inp, state) in zip(inputs, next_states)] - with tf.control_dependencies(saves): - acc, acc_update = eval_ops['accuracy'] - acc_update = tf.identity(acc_update) - eval_ops['accuracy'] = (acc, acc_update) - - var_restore_dict = make_restore_average_vars_dict() - return eval_ops, var_restore_dict - - def cl_loss_from_embedding(self, - embedded, - inputs=None, - return_intermediates=False): - """Compute classification loss from embedding. - - Args: - embedded: Length 2 tuple of 3-D float Tensor - [batch_size, num_timesteps, embedding_dim]. - inputs: Length 2 tuple of VatxtInput, defaults to self.cl_inputs. - return_intermediates: bool, whether to return intermediate tensors or only - the final loss. - - Returns: - If return_intermediates is True: - lstm_out, next_states, logits, loss - Else: - loss - """ - if inputs is None: - inputs = self.cl_inputs - - out = [] - for (layer_name, emb, inp) in zip(['lstm', 'lstm_reverse'], embedded, - inputs): - out.append(self.layers[layer_name](emb, inp.state, inp.length)) - lstm_outs, next_states = zip(*out) - - # Concatenate output of forward and reverse LSTMs - lstm_out = tf.concat(lstm_outs, 1) - - logits = self.layers['cl_logits'](lstm_out) - f_inputs, _ = inputs # pylint: disable=unpacking-non-sequence - loss = layers_lib.classification_loss(logits, f_inputs.labels, - f_inputs.weights) - - if return_intermediates: - return lstm_out, next_states, logits, loss - else: - return loss - - def adversarial_loss(self): - """Compute adversarial loss based on FLAGS.adv_training_method.""" - - def random_perturbation_loss(): - return adv_lib.random_perturbation_loss_bidir(self.tensors['cl_embedded'], - self.cl_inputs[0].length, - self.cl_loss_from_embedding) - - def adversarial_loss(): - return adv_lib.adversarial_loss_bidir(self.tensors['cl_embedded'], - self.tensors['cl_loss'], - self.cl_loss_from_embedding) - - def virtual_adversarial_loss(): - """Computes virtual adversarial loss. - - Uses lm_inputs and constructs the language model graph if it hasn't yet - been constructed. - - Also ensures that the LM input states are saved for LSTM state-saving - BPTT. - - Returns: - loss: float scalar. - """ - if self.lm_inputs is None: - self.language_model_graph(compute_loss=False) - - def logits_from_embedding(embedded, return_next_state=False): - _, next_states, logits, _ = self.cl_loss_from_embedding( - embedded, inputs=self.lm_inputs, return_intermediates=True) - if return_next_state: - return next_states, logits - else: - return logits - - lm_embedded = (self.tensors['lm_embedded'], - self.tensors['lm_embedded_reverse']) - next_states, lm_cl_logits = logits_from_embedding( - lm_embedded, return_next_state=True) - - va_loss = adv_lib.virtual_adversarial_loss_bidir( - lm_cl_logits, lm_embedded, self.lm_inputs, logits_from_embedding) - - saves = [ - inp.save_state(state) - for (inp, state) in zip(self.lm_inputs, next_states) - ] - with tf.control_dependencies(saves): - va_loss = tf.identity(va_loss) - - return va_loss - - def combo_loss(): - return adversarial_loss() + virtual_adversarial_loss() - - adv_training_methods = { - # Random perturbation - 'rp': random_perturbation_loss, - # Adversarial training - 'at': adversarial_loss, - # Virtual adversarial training - 'vat': virtual_adversarial_loss, - # Both at and vat - 'atvat': combo_loss, - '': lambda: tf.constant(0.), - None: lambda: tf.constant(0.), - } - - with tf.name_scope('adversarial_loss'): - return adv_training_methods[FLAGS.adv_training_method]() - - -def _inputs(dataset='train', pretrain=False, bidir=False): - return inputs_lib.inputs( - data_dir=FLAGS.data_dir, - phase=dataset, - bidir=bidir, - pretrain=pretrain, - use_seq2seq=pretrain and FLAGS.use_seq2seq_autoencoder, - state_size=FLAGS.rnn_cell_size, - num_layers=FLAGS.rnn_num_layers, - batch_size=FLAGS.batch_size, - unroll_steps=FLAGS.num_timesteps, - eos_id=FLAGS.vocab_size - 1) - - -def _get_vocab_freqs(): - """Returns vocab frequencies. - - Returns: - List of integers, length=FLAGS.vocab_size. - - Raises: - ValueError: if the length of the frequency file is not equal to the vocab - size, or if the file is not found. - """ - path = FLAGS.vocab_freq_path or os.path.join(FLAGS.data_dir, 'vocab_freq.txt') - - if tf.gfile.Exists(path): - with tf.gfile.Open(path) as f: - # Get pre-calculated frequencies of words. - reader = csv.reader(f, quoting=csv.QUOTE_NONE) - freqs = [int(row[-1]) for row in reader] - if len(freqs) != FLAGS.vocab_size: - raise ValueError('Frequency file length %d != vocab size %d' % - (len(freqs), FLAGS.vocab_size)) - else: - if FLAGS.vocab_freq_path: - raise ValueError('vocab_freq_path not found') - freqs = [1] * FLAGS.vocab_size - - return freqs - - -def make_restore_average_vars_dict(): - """Returns dict mapping moving average names to variables.""" - var_restore_dict = {} - variable_averages = tf.train.ExponentialMovingAverage(0.999) - for v in tf.global_variables(): - if v in tf.trainable_variables(): - name = variable_averages.average_name(v) - else: - name = v.op.name - var_restore_dict[name] = v - return var_restore_dict - - -def optimize(loss, global_step): - return layers_lib.optimize( - loss, global_step, FLAGS.max_grad_norm, FLAGS.learning_rate, - FLAGS.learning_rate_decay_factor, FLAGS.sync_replicas, - FLAGS.replicas_to_aggregate, FLAGS.task) diff --git a/research/adversarial_text/graphs_test.py b/research/adversarial_text/graphs_test.py deleted file mode 100644 index b04765a316f..00000000000 --- a/research/adversarial_text/graphs_test.py +++ /dev/null @@ -1,225 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for graphs.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from collections import defaultdict -import operator -import os -import random -import shutil -import string -import tempfile - -# Dependency imports - -import tensorflow as tf - -import graphs -from data import data_utils - -flags = tf.app.flags -FLAGS = flags.FLAGS -data = data_utils - -flags.DEFINE_integer('task', 0, 'Task id; needed for SyncReplicas test') - - -def _build_random_vocabulary(vocab_size=100): - """Builds and returns a dict.""" - vocab = set() - while len(vocab) < (vocab_size - 1): - rand_word = ''.join( - random.choice(string.ascii_lowercase) - for _ in range(random.randint(1, 10))) - vocab.add(rand_word) - - vocab_ids = dict([(word, i) for i, word in enumerate(vocab)]) - vocab_ids[data.EOS_TOKEN] = vocab_size - 1 - return vocab_ids - - -def _build_random_sequence(vocab_ids): - seq_len = random.randint(10, 200) - ids = vocab_ids.values() - seq = data.SequenceWrapper() - for token_id in [random.choice(ids) for _ in range(seq_len)]: - seq.add_timestep().set_token(token_id) - return seq - - -def _build_vocab_frequencies(seqs, vocab_ids): - vocab_freqs = defaultdict(int) - ids_to_words = dict([(i, word) for word, i in vocab_ids.iteritems()]) - for seq in seqs: - for timestep in seq: - vocab_freqs[ids_to_words[timestep.token]] += 1 - - vocab_freqs[data.EOS_TOKEN] = 0 - return vocab_freqs - - -class GraphsTest(tf.test.TestCase): - """Test graph construction methods.""" - - @classmethod - def setUpClass(cls): - # Make model small - FLAGS.batch_size = 2 - FLAGS.num_timesteps = 3 - FLAGS.embedding_dims = 4 - FLAGS.rnn_num_layers = 2 - FLAGS.rnn_cell_size = 4 - FLAGS.cl_num_layers = 2 - FLAGS.cl_hidden_size = 4 - FLAGS.vocab_size = 10 - - # Set input/output flags - FLAGS.data_dir = tempfile.mkdtemp() - - # Build and write sequence files. - vocab_ids = _build_random_vocabulary(FLAGS.vocab_size) - seqs = [_build_random_sequence(vocab_ids) for _ in range(5)] - seqs_label = [ - data.build_labeled_sequence(seq, random.choice([True, False])) - for seq in seqs - ] - seqs_lm = [data.build_lm_sequence(seq) for seq in seqs] - seqs_ae = [data.build_seq_ae_sequence(seq) for seq in seqs] - seqs_rev = [data.build_reverse_sequence(seq) for seq in seqs] - seqs_bidir = [ - data.build_bidirectional_seq(seq, rev) - for seq, rev in zip(seqs, seqs_rev) - ] - seqs_bidir_label = [ - data.build_labeled_sequence(bd_seq, random.choice([True, False])) - for bd_seq in seqs_bidir - ] - - filenames = [ - data.TRAIN_CLASS, data.TRAIN_LM, data.TRAIN_SA, data.TEST_CLASS, - data.TRAIN_REV_LM, data.TRAIN_BD_CLASS, data.TEST_BD_CLASS - ] - seq_lists = [ - seqs_label, seqs_lm, seqs_ae, seqs_label, seqs_rev, seqs_bidir, - seqs_bidir_label - ] - for fname, seq_list in zip(filenames, seq_lists): - with tf.python_io.TFRecordWriter( - os.path.join(FLAGS.data_dir, fname)) as writer: - for seq in seq_list: - writer.write(seq.seq.SerializeToString()) - - # Write vocab.txt and vocab_freq.txt - vocab_freqs = _build_vocab_frequencies(seqs, vocab_ids) - ordered_vocab_freqs = sorted( - vocab_freqs.items(), key=operator.itemgetter(1), reverse=True) - with open(os.path.join(FLAGS.data_dir, 'vocab.txt'), 'w') as vocab_f: - with open(os.path.join(FLAGS.data_dir, 'vocab_freq.txt'), 'w') as freq_f: - for word, freq in ordered_vocab_freqs: - vocab_f.write('{}\n'.format(word)) - freq_f.write('{}\n'.format(freq)) - - @classmethod - def tearDownClass(cls): - shutil.rmtree(FLAGS.data_dir) - - def setUp(self): - # Reset FLAGS - FLAGS.rnn_num_layers = 1 - FLAGS.sync_replicas = False - FLAGS.adv_training_method = None - FLAGS.num_candidate_samples = -1 - FLAGS.num_classes = 2 - FLAGS.use_seq2seq_autoencoder = False - - # Reset Graph - tf.reset_default_graph() - - def testClassifierGraph(self): - FLAGS.rnn_num_layers = 2 - model = graphs.VatxtModel() - train_op, _, _ = model.classifier_training() - # Pretrained vars: embedding + LSTM layers - self.assertEqual( - len(model.pretrained_variables), 1 + 2 * FLAGS.rnn_num_layers) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - tf.train.start_queue_runners(sess) - sess.run(train_op) - - def testLanguageModelGraph(self): - train_op, _, _ = graphs.VatxtModel().language_model_training() - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - tf.train.start_queue_runners(sess) - sess.run(train_op) - - def testMulticlass(self): - FLAGS.num_classes = 10 - graphs.VatxtModel().classifier_graph() - - def testATMethods(self): - at_methods = [None, 'rp', 'at', 'vat', 'atvat'] - for method in at_methods: - FLAGS.adv_training_method = method - with tf.Graph().as_default(): - graphs.VatxtModel().classifier_graph() - - # Ensure variables have been reused - # Embedding + LSTM layers + hidden layers + logits layer - expected_num_vars = 1 + 2 * FLAGS.rnn_num_layers + 2 * ( - FLAGS.cl_num_layers) + 2 - self.assertEqual(len(tf.trainable_variables()), expected_num_vars) - - def testSyncReplicas(self): - FLAGS.sync_replicas = True - graphs.VatxtModel().language_model_training() - - def testCandidateSampling(self): - FLAGS.num_candidate_samples = 10 - graphs.VatxtModel().language_model_training() - - def testSeqAE(self): - FLAGS.use_seq2seq_autoencoder = True - graphs.VatxtModel().language_model_training() - - def testBidirLM(self): - graphs.VatxtBidirModel().language_model_graph() - - def testBidirClassifier(self): - at_methods = [None, 'rp', 'at', 'vat', 'atvat'] - for method in at_methods: - FLAGS.adv_training_method = method - with tf.Graph().as_default(): - graphs.VatxtBidirModel().classifier_graph() - - # Ensure variables have been reused - # Embedding + 2 LSTM layers + hidden layers + logits layer - expected_num_vars = 1 + 2 * 2 * FLAGS.rnn_num_layers + 2 * ( - FLAGS.cl_num_layers) + 2 - self.assertEqual(len(tf.trainable_variables()), expected_num_vars) - - def testEvalGraph(self): - _, _ = graphs.VatxtModel().eval_graph() - - def testBidirEvalGraph(self): - _, _ = graphs.VatxtBidirModel().eval_graph() - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/adversarial_text/inputs.py b/research/adversarial_text/inputs.py deleted file mode 100644 index 48a523d8d48..00000000000 --- a/research/adversarial_text/inputs.py +++ /dev/null @@ -1,342 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Input utils for virtual adversarial text classification.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -# Dependency imports - -import tensorflow as tf - -from data import data_utils - - -class VatxtInput(object): - """Wrapper around NextQueuedSequenceBatch.""" - - def __init__(self, - batch, - state_name=None, - tokens=None, - num_states=0, - eos_id=None): - """Construct VatxtInput. - - Args: - batch: NextQueuedSequenceBatch. - state_name: str, name of state to fetch and save. - tokens: int Tensor, tokens. Defaults to batch's F_TOKEN_ID sequence. - num_states: int The number of states to store. - eos_id: int Id of end of Sequence. - """ - self._batch = batch - self._state_name = state_name - self._tokens = (tokens if tokens is not None else - batch.sequences[data_utils.SequenceWrapper.F_TOKEN_ID]) - self._num_states = num_states - - w = batch.sequences[data_utils.SequenceWrapper.F_WEIGHT] - self._weights = w - - l = batch.sequences[data_utils.SequenceWrapper.F_LABEL] - self._labels = l - - # eos weights - self._eos_weights = None - if eos_id: - ew = tf.cast(tf.equal(self._tokens, eos_id), tf.float32) - self._eos_weights = ew - - @property - def tokens(self): - return self._tokens - - @property - def weights(self): - return self._weights - - @property - def eos_weights(self): - return self._eos_weights - - @property - def labels(self): - return self._labels - - @property - def length(self): - return self._batch.length - - @property - def state_name(self): - return self._state_name - - @property - def state(self): - # LSTM tuple states - state_names = _get_tuple_state_names(self._num_states, self._state_name) - return tuple([ - tf.contrib.rnn.LSTMStateTuple( - self._batch.state(c_name), self._batch.state(h_name)) - for c_name, h_name in state_names - ]) - - def save_state(self, value): - # LSTM tuple states - state_names = _get_tuple_state_names(self._num_states, self._state_name) - save_ops = [] - for (c_state, h_state), (c_name, h_name) in zip(value, state_names): - save_ops.append(self._batch.save_state(c_name, c_state)) - save_ops.append(self._batch.save_state(h_name, h_state)) - return tf.group(*save_ops) - - -def _get_tuple_state_names(num_states, base_name): - """Returns state names for use with LSTM tuple state.""" - state_names = [('{}_{}_c'.format(i, base_name), '{}_{}_h'.format( - i, base_name)) for i in range(num_states)] - return state_names - - -def _split_bidir_tokens(batch): - tokens = batch.sequences[data_utils.SequenceWrapper.F_TOKEN_ID] - # Tokens have shape [batch, time, 2] - # forward and reverse have shape [batch, time]. - forward, reverse = [ - tf.squeeze(t, axis=[2]) for t in tf.split(tokens, 2, axis=2) - ] - return forward, reverse - - -def _filenames_for_data_spec(phase, bidir, pretrain, use_seq2seq): - """Returns input filenames for configuration. - - Args: - phase: str, 'train', 'test', or 'valid'. - bidir: bool, bidirectional model. - pretrain: bool, pretraining or classification. - use_seq2seq: bool, seq2seq data, only valid if pretrain=True. - - Returns: - Tuple of filenames. - - Raises: - ValueError: if an invalid combination of arguments is provided that does not - map to any data files (e.g. pretrain=False, use_seq2seq=True). - """ - data_spec = (phase, bidir, pretrain, use_seq2seq) - data_specs = { - ('train', True, True, False): (data_utils.TRAIN_LM, - data_utils.TRAIN_REV_LM), - ('train', True, False, False): (data_utils.TRAIN_BD_CLASS,), - ('train', False, True, False): (data_utils.TRAIN_LM,), - ('train', False, True, True): (data_utils.TRAIN_SA,), - ('train', False, False, False): (data_utils.TRAIN_CLASS,), - ('test', True, True, False): (data_utils.TEST_LM, - data_utils.TRAIN_REV_LM), - ('test', True, False, False): (data_utils.TEST_BD_CLASS,), - ('test', False, True, False): (data_utils.TEST_LM,), - ('test', False, True, True): (data_utils.TEST_SA,), - ('test', False, False, False): (data_utils.TEST_CLASS,), - ('valid', True, False, False): (data_utils.VALID_BD_CLASS,), - ('valid', False, False, False): (data_utils.VALID_CLASS,), - } - if data_spec not in data_specs: - raise ValueError( - 'Data specification (phase, bidir, pretrain, use_seq2seq) %s not ' - 'supported' % str(data_spec)) - - return data_specs[data_spec] - - -def _read_single_sequence_example(file_list, tokens_shape=None): - """Reads and parses SequenceExamples from TFRecord-encoded file_list.""" - tf.logging.info('Constructing TFRecordReader from files: %s', file_list) - file_queue = tf.train.string_input_producer(file_list) - reader = tf.TFRecordReader() - seq_key, serialized_record = reader.read(file_queue) - ctx, sequence = tf.parse_single_sequence_example( - serialized_record, - sequence_features={ - data_utils.SequenceWrapper.F_TOKEN_ID: - tf.FixedLenSequenceFeature(tokens_shape or [], dtype=tf.int64), - data_utils.SequenceWrapper.F_LABEL: - tf.FixedLenSequenceFeature([], dtype=tf.int64), - data_utils.SequenceWrapper.F_WEIGHT: - tf.FixedLenSequenceFeature([], dtype=tf.float32), - }) - return seq_key, ctx, sequence - - -def _read_and_batch(data_dir, - fname, - state_name, - state_size, - num_layers, - unroll_steps, - batch_size, - bidir_input=False): - """Inputs for text model. - - Args: - data_dir: str, directory containing TFRecord files of SequenceExample. - fname: str, input file name. - state_name: string, key for saved state of LSTM. - state_size: int, size of LSTM state. - num_layers: int, the number of layers in the LSTM. - unroll_steps: int, number of timesteps to unroll for TBTT. - batch_size: int, batch size. - bidir_input: bool, whether the input is bidirectional. If True, creates 2 - states, state_name and state_name + '_reverse'. - - Returns: - Instance of NextQueuedSequenceBatch - - Raises: - ValueError: if file for input specification is not found. - """ - data_path = os.path.join(data_dir, fname) - if not tf.gfile.Exists(data_path): - raise ValueError('Failed to find file: %s' % data_path) - - tokens_shape = [2] if bidir_input else [] - seq_key, ctx, sequence = _read_single_sequence_example( - [data_path], tokens_shape=tokens_shape) - # Set up stateful queue reader. - state_names = _get_tuple_state_names(num_layers, state_name) - initial_states = {} - for c_state, h_state in state_names: - initial_states[c_state] = tf.zeros(state_size) - initial_states[h_state] = tf.zeros(state_size) - if bidir_input: - rev_state_names = _get_tuple_state_names(num_layers, - '{}_reverse'.format(state_name)) - for rev_c_state, rev_h_state in rev_state_names: - initial_states[rev_c_state] = tf.zeros(state_size) - initial_states[rev_h_state] = tf.zeros(state_size) - batch = tf.contrib.training.batch_sequences_with_states( - input_key=seq_key, - input_sequences=sequence, - input_context=ctx, - input_length=tf.shape(sequence['token_id'])[0], - initial_states=initial_states, - num_unroll=unroll_steps, - batch_size=batch_size, - allow_small_batch=False, - num_threads=4, - capacity=batch_size * 10, - make_keys_unique=True, - make_keys_unique_seed=29392) - return batch - - -def inputs(data_dir=None, - phase='train', - bidir=False, - pretrain=False, - use_seq2seq=False, - state_name='lstm', - state_size=None, - num_layers=0, - batch_size=32, - unroll_steps=100, - eos_id=None): - """Inputs for text model. - - Args: - data_dir: str, directory containing TFRecord files of SequenceExample. - phase: str, dataset for evaluation {'train', 'valid', 'test'}. - bidir: bool, bidirectional LSTM. - pretrain: bool, whether to read pretraining data or classification data. - use_seq2seq: bool, whether to read seq2seq data or the language model data. - state_name: string, key for saved state of LSTM. - state_size: int, size of LSTM state. - num_layers: int, the number of LSTM layers. - batch_size: int, batch size. - unroll_steps: int, number of timesteps to unroll for TBTT. - eos_id: int, id of end of sequence. used for the kl weights on vat - Returns: - Instance of VatxtInput (x2 if bidir=True and pretrain=True, i.e. forward and - reverse). - """ - with tf.name_scope('inputs'): - filenames = _filenames_for_data_spec(phase, bidir, pretrain, use_seq2seq) - - if bidir and pretrain: - # Bidirectional pretraining - # Requires separate forward and reverse language model data. - forward_fname, reverse_fname = filenames - forward_batch = _read_and_batch(data_dir, forward_fname, state_name, - state_size, num_layers, unroll_steps, - batch_size) - state_name_rev = state_name + '_reverse' - reverse_batch = _read_and_batch(data_dir, reverse_fname, state_name_rev, - state_size, num_layers, unroll_steps, - batch_size) - forward_input = VatxtInput( - forward_batch, - state_name=state_name, - num_states=num_layers, - eos_id=eos_id) - reverse_input = VatxtInput( - reverse_batch, - state_name=state_name_rev, - num_states=num_layers, - eos_id=eos_id) - return forward_input, reverse_input - - elif bidir: - # Classifier bidirectional LSTM - # Shared data source, but separate token/state streams - fname, = filenames - batch = _read_and_batch( - data_dir, - fname, - state_name, - state_size, - num_layers, - unroll_steps, - batch_size, - bidir_input=True) - forward_tokens, reverse_tokens = _split_bidir_tokens(batch) - forward_input = VatxtInput( - batch, - state_name=state_name, - tokens=forward_tokens, - num_states=num_layers) - reverse_input = VatxtInput( - batch, - state_name=state_name + '_reverse', - tokens=reverse_tokens, - num_states=num_layers) - return forward_input, reverse_input - else: - # Unidirectional LM or classifier - fname, = filenames - batch = _read_and_batch( - data_dir, - fname, - state_name, - state_size, - num_layers, - unroll_steps, - batch_size, - bidir_input=False) - return VatxtInput( - batch, state_name=state_name, num_states=num_layers, eos_id=eos_id) diff --git a/research/adversarial_text/layers.py b/research/adversarial_text/layers.py deleted file mode 100644 index be4c7a47e08..00000000000 --- a/research/adversarial_text/layers.py +++ /dev/null @@ -1,397 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Layers for VatxtModel.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# Dependency imports - -from six.moves import xrange -import tensorflow as tf -K = tf.keras - - -def cl_logits_subgraph(layer_sizes, input_size, num_classes, keep_prob=1.): - """Construct multiple ReLU layers with dropout and a linear layer.""" - subgraph = K.models.Sequential(name='cl_logits') - for i, layer_size in enumerate(layer_sizes): - if i == 0: - subgraph.add( - K.layers.Dense(layer_size, activation='relu', input_dim=input_size)) - else: - subgraph.add(K.layers.Dense(layer_size, activation='relu')) - - if keep_prob < 1.: - subgraph.add(K.layers.Dropout(1. - keep_prob)) - subgraph.add(K.layers.Dense(1 if num_classes == 2 else num_classes)) - return subgraph - - -class Embedding(K.layers.Layer): - """Embedding layer with frequency-based normalization and dropout.""" - - def __init__(self, - vocab_size, - embedding_dim, - normalize=False, - vocab_freqs=None, - keep_prob=1., - **kwargs): - self.vocab_size = vocab_size - self.embedding_dim = embedding_dim - self.normalized = normalize - self.keep_prob = keep_prob - - if normalize: - assert vocab_freqs is not None - self.vocab_freqs = tf.constant( - vocab_freqs, dtype=tf.float32, shape=(vocab_size, 1)) - - super(Embedding, self).__init__(**kwargs) - - def build(self, input_shape): - with tf.device('/cpu:0'): - self.var = self.add_weight( - shape=(self.vocab_size, self.embedding_dim), - initializer=tf.random_uniform_initializer(-1., 1.), - name='embedding', - dtype=tf.float32) - - if self.normalized: - self.var = self._normalize(self.var) - - super(Embedding, self).build(input_shape) - - def call(self, x): - embedded = tf.nn.embedding_lookup(self.var, x) - if self.keep_prob < 1.: - shape = embedded.get_shape().as_list() - - # Use same dropout masks at each timestep with specifying noise_shape. - # This slightly improves performance. - # Please see https://arxiv.org/abs/1512.05287 for the theoretical - # explanation. - embedded = tf.nn.dropout( - embedded, self.keep_prob, noise_shape=(shape[0], 1, shape[2])) - return embedded - - def _normalize(self, emb): - weights = self.vocab_freqs / tf.reduce_sum(self.vocab_freqs) - mean = tf.reduce_sum(weights * emb, 0, keep_dims=True) - var = tf.reduce_sum(weights * tf.pow(emb - mean, 2.), 0, keep_dims=True) - stddev = tf.sqrt(1e-6 + var) - return (emb - mean) / stddev - - -class LSTM(object): - """LSTM layer using dynamic_rnn. - - Exposes variables in `trainable_weights` property. - """ - - def __init__(self, cell_size, num_layers=1, keep_prob=1., name='LSTM'): - self.cell_size = cell_size - self.num_layers = num_layers - self.keep_prob = keep_prob - self.reuse = None - self.trainable_weights = None - self.name = name - - def __call__(self, x, initial_state, seq_length): - with tf.variable_scope(self.name, reuse=self.reuse) as vs: - cell = tf.contrib.rnn.MultiRNNCell([ - tf.contrib.rnn.BasicLSTMCell( - self.cell_size, - forget_bias=0.0, - reuse=tf.get_variable_scope().reuse) - for _ in xrange(self.num_layers) - ]) - - # shape(x) = (batch_size, num_timesteps, embedding_dim) - - lstm_out, next_state = tf.nn.dynamic_rnn( - cell, x, initial_state=initial_state, sequence_length=seq_length) - - # shape(lstm_out) = (batch_size, timesteps, cell_size) - - if self.keep_prob < 1.: - lstm_out = tf.nn.dropout(lstm_out, self.keep_prob) - - if self.reuse is None: - self.trainable_weights = vs.global_variables() - - self.reuse = True - - return lstm_out, next_state - - -class SoftmaxLoss(K.layers.Layer): - """Softmax xentropy loss with candidate sampling.""" - - def __init__(self, - vocab_size, - num_candidate_samples=-1, - vocab_freqs=None, - **kwargs): - self.vocab_size = vocab_size - self.num_candidate_samples = num_candidate_samples - self.vocab_freqs = vocab_freqs - super(SoftmaxLoss, self).__init__(**kwargs) - self.multiclass_dense_layer = K.layers.Dense(self.vocab_size) - - def build(self, input_shape): - input_shape = input_shape[0].as_list() - with tf.device('/cpu:0'): - self.lin_w = self.add_weight( - shape=(input_shape[-1], self.vocab_size), - name='lm_lin_w', - initializer=K.initializers.glorot_uniform()) - self.lin_b = self.add_weight( - shape=(self.vocab_size,), - name='lm_lin_b', - initializer=K.initializers.glorot_uniform()) - self.multiclass_dense_layer.build(input_shape) - - super(SoftmaxLoss, self).build(input_shape) - - def call(self, inputs): - x, labels, weights = inputs - if self.num_candidate_samples > -1: - assert self.vocab_freqs is not None - labels_reshaped = tf.reshape(labels, [-1]) - labels_reshaped = tf.expand_dims(labels_reshaped, -1) - sampled = tf.nn.fixed_unigram_candidate_sampler( - true_classes=labels_reshaped, - num_true=1, - num_sampled=self.num_candidate_samples, - unique=True, - range_max=self.vocab_size, - unigrams=self.vocab_freqs) - inputs_reshaped = tf.reshape(x, [-1, int(x.get_shape()[2])]) - - lm_loss = tf.nn.sampled_softmax_loss( - weights=tf.transpose(self.lin_w), - biases=self.lin_b, - labels=labels_reshaped, - inputs=inputs_reshaped, - num_sampled=self.num_candidate_samples, - num_classes=self.vocab_size, - sampled_values=sampled) - lm_loss = tf.reshape( - lm_loss, - [int(x.get_shape()[0]), int(x.get_shape()[1])]) - else: - logits = self.multiclass_dense_layer(x) - lm_loss = tf.nn.sparse_softmax_cross_entropy_with_logits( - logits=logits, labels=labels) - - lm_loss = tf.identity( - tf.reduce_sum(lm_loss * weights) / _num_labels(weights), - name='lm_xentropy_loss') - return lm_loss - - -def classification_loss(logits, labels, weights): - """Computes cross entropy loss between logits and labels. - - Args: - logits: 2-D [timesteps*batch_size, m] float tensor, where m=1 if - num_classes=2, otherwise m=num_classes. - labels: 1-D [timesteps*batch_size] integer tensor. - weights: 1-D [timesteps*batch_size] float tensor. - - Returns: - Loss scalar of type float. - """ - inner_dim = logits.get_shape().as_list()[-1] - with tf.name_scope('classifier_loss'): - # Logistic loss - if inner_dim == 1: - loss = tf.nn.sigmoid_cross_entropy_with_logits( - logits=tf.squeeze(logits, -1), labels=tf.cast(labels, tf.float32)) - # Softmax loss - else: - loss = tf.nn.sparse_softmax_cross_entropy_with_logits( - logits=logits, labels=labels) - - num_lab = _num_labels(weights) - tf.summary.scalar('num_labels', num_lab) - return tf.identity( - tf.reduce_sum(weights * loss) / num_lab, name='classification_xentropy') - - -def accuracy(logits, targets, weights): - """Computes prediction accuracy. - - Args: - logits: 2-D classifier logits [timesteps*batch_size, num_classes] - targets: 1-D [timesteps*batch_size] integer tensor. - weights: 1-D [timesteps*batch_size] float tensor. - - Returns: - Accuracy: float scalar. - """ - with tf.name_scope('accuracy'): - eq = tf.cast(tf.equal(predictions(logits), targets), tf.float32) - return tf.identity( - tf.reduce_sum(weights * eq) / _num_labels(weights), name='accuracy') - - -def predictions(logits): - """Class prediction from logits.""" - inner_dim = logits.get_shape().as_list()[-1] - with tf.name_scope('predictions'): - # For binary classification - if inner_dim == 1: - pred = tf.cast(tf.greater(tf.squeeze(logits, -1), 0.), tf.int64) - # For multi-class classification - else: - pred = tf.argmax(logits, 2) - return pred - - -def _num_labels(weights): - """Number of 1's in weights. Returns 1. if 0.""" - num_labels = tf.reduce_sum(weights) - num_labels = tf.where(tf.equal(num_labels, 0.), 1., num_labels) - return num_labels - - -def optimize(loss, - global_step, - max_grad_norm, - lr, - lr_decay, - sync_replicas=False, - replicas_to_aggregate=1, - task_id=0): - """Builds optimization graph. - - * Creates an optimizer, and optionally wraps with SyncReplicasOptimizer - * Computes, clips, and applies gradients - * Maintains moving averages for all trainable variables - * Summarizes variables and gradients - - Args: - loss: scalar loss to minimize. - global_step: integer scalar Variable. - max_grad_norm: float scalar. Grads will be clipped to this value. - lr: float scalar, learning rate. - lr_decay: float scalar, learning rate decay rate. - sync_replicas: bool, whether to use SyncReplicasOptimizer. - replicas_to_aggregate: int, number of replicas to aggregate when using - SyncReplicasOptimizer. - task_id: int, id of the current task; used to ensure proper initialization - of SyncReplicasOptimizer. - - Returns: - train_op - """ - with tf.name_scope('optimization'): - # Compute gradients. - tvars = tf.trainable_variables() - grads = tf.gradients( - loss, - tvars, - aggregation_method=tf.AggregationMethod.EXPERIMENTAL_ACCUMULATE_N) - - # Clip non-embedding grads - non_embedding_grads_and_vars = [(g, v) for (g, v) in zip(grads, tvars) - if 'embedding' not in v.op.name] - embedding_grads_and_vars = [(g, v) for (g, v) in zip(grads, tvars) - if 'embedding' in v.op.name] - - ne_grads, ne_vars = zip(*non_embedding_grads_and_vars) - ne_grads, _ = tf.clip_by_global_norm(ne_grads, max_grad_norm) - non_embedding_grads_and_vars = zip(ne_grads, ne_vars) - - grads_and_vars = embedding_grads_and_vars + list(non_embedding_grads_and_vars) - - # Summarize - _summarize_vars_and_grads(grads_and_vars) - - # Decaying learning rate - lr = tf.train.exponential_decay( - lr, global_step, 1, lr_decay, staircase=True) - tf.summary.scalar('learning_rate', lr) - opt = tf.train.AdamOptimizer(lr) - - # Track the moving averages of all trainable variables. - variable_averages = tf.train.ExponentialMovingAverage(0.999, global_step) - - # Apply gradients - if sync_replicas: - opt = tf.train.SyncReplicasOptimizer( - opt, - replicas_to_aggregate, - variable_averages=variable_averages, - variables_to_average=tvars, - total_num_replicas=replicas_to_aggregate) - apply_gradient_op = opt.apply_gradients( - grads_and_vars, global_step=global_step) - with tf.control_dependencies([apply_gradient_op]): - train_op = tf.no_op(name='train_op') - - # Initialization ops - tf.add_to_collection(tf.GraphKeys.QUEUE_RUNNERS, - opt.get_chief_queue_runner()) - if task_id == 0: # Chief task - local_init_op = opt.chief_init_op - tf.add_to_collection('chief_init_op', opt.get_init_tokens_op()) - else: - local_init_op = opt.local_step_init_op - tf.add_to_collection('local_init_op', local_init_op) - tf.add_to_collection('ready_for_local_init_op', - opt.ready_for_local_init_op) - else: - # Non-sync optimizer - apply_gradient_op = opt.apply_gradients(grads_and_vars, global_step) - with tf.control_dependencies([apply_gradient_op]): - train_op = variable_averages.apply(tvars) - - return train_op - - -def _summarize_vars_and_grads(grads_and_vars): - tf.logging.info('Trainable variables:') - tf.logging.info('-' * 60) - for grad, var in grads_and_vars: - tf.logging.info(var) - - def tag(name, v=var): - return v.op.name + '_' + name - - # Variable summary - mean = tf.reduce_mean(var) - tf.summary.scalar(tag('mean'), mean) - with tf.name_scope(tag('stddev')): - stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean))) - tf.summary.scalar(tag('stddev'), stddev) - tf.summary.scalar(tag('max'), tf.reduce_max(var)) - tf.summary.scalar(tag('min'), tf.reduce_min(var)) - tf.summary.histogram(tag('histogram'), var) - - # Gradient summary - if grad is not None: - if isinstance(grad, tf.IndexedSlices): - grad_values = grad.values - else: - grad_values = grad - - tf.summary.histogram(tag('gradient'), grad_values) - tf.summary.scalar(tag('gradient_norm'), tf.global_norm([grad_values])) - else: - tf.logging.info('Var %s has no gradient', var.op.name) diff --git a/research/adversarial_text/pretrain.py b/research/adversarial_text/pretrain.py deleted file mode 100644 index 4e1fa6a4cbb..00000000000 --- a/research/adversarial_text/pretrain.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Pretrains a recurrent language model. - -Computational time: - 2 days to train 100000 steps on 1 layer 1024 hidden units LSTM, - 256 embeddings, 400 truncated BP, 256 minibatch and on single GPU (Pascal - Titan X, cuDNNv5). -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# Dependency imports - -import tensorflow as tf - -import graphs -import train_utils - -FLAGS = tf.app.flags.FLAGS - - -def main(_): - """Trains Language Model.""" - tf.logging.set_verbosity(tf.logging.INFO) - with tf.device(tf.train.replica_device_setter(FLAGS.ps_tasks)): - model = graphs.get_model() - train_op, loss, global_step = model.language_model_training() - train_utils.run_training(train_op, loss, global_step) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/adversarial_text/train_classifier.py b/research/adversarial_text/train_classifier.py deleted file mode 100644 index f498d2c2fb9..00000000000 --- a/research/adversarial_text/train_classifier.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Trains LSTM text classification model. - -Model trains with adversarial or virtual adversarial training. - -Computational time: - 1.8 hours to train 10000 steps without adversarial or virtual adversarial - training, on 1 layer 1024 hidden units LSTM, 256 embeddings, 400 truncated - BP, 64 minibatch and on single GPU (Pascal Titan X, cuDNNv5). - - 4 hours to train 10000 steps with adversarial or virtual adversarial - training, with above condition. - -To initialize embedding and LSTM cell weights from a pretrained model, set -FLAGS.pretrained_model_dir to the pretrained model's checkpoint directory. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# Dependency imports - -import tensorflow as tf - -import graphs -import train_utils - -flags = tf.app.flags -FLAGS = flags.FLAGS - -flags.DEFINE_string('pretrained_model_dir', None, - 'Directory path to pretrained model to restore from') - - -def main(_): - """Trains LSTM classification model.""" - tf.logging.set_verbosity(tf.logging.INFO) - with tf.device(tf.train.replica_device_setter(FLAGS.ps_tasks)): - model = graphs.get_model() - train_op, loss, global_step = model.classifier_training() - train_utils.run_training( - train_op, - loss, - global_step, - variables_to_restore=model.pretrained_variables, - pretrained_model_dir=FLAGS.pretrained_model_dir) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/adversarial_text/train_utils.py b/research/adversarial_text/train_utils.py deleted file mode 100644 index 577237967d0..00000000000 --- a/research/adversarial_text/train_utils.py +++ /dev/null @@ -1,133 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utilities for training adversarial text models.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import time - -# Dependency imports - -import numpy as np -import tensorflow as tf - -flags = tf.app.flags -FLAGS = flags.FLAGS - -flags.DEFINE_string('master', '', 'Master address.') -flags.DEFINE_integer('task', 0, 'Task id of the replica running the training.') -flags.DEFINE_integer('ps_tasks', 0, 'Number of parameter servers.') -flags.DEFINE_string('train_dir', '/tmp/text_train', - 'Directory for logs and checkpoints.') -flags.DEFINE_integer('max_steps', 1000000, 'Number of batches to run.') -flags.DEFINE_boolean('log_device_placement', False, - 'Whether to log device placement.') - - -def run_training(train_op, - loss, - global_step, - variables_to_restore=None, - pretrained_model_dir=None): - """Sets up and runs training loop.""" - tf.gfile.MakeDirs(FLAGS.train_dir) - - # Create pretrain Saver - if pretrained_model_dir: - assert variables_to_restore - tf.logging.info('Will attempt restore from %s: %s', pretrained_model_dir, - variables_to_restore) - saver_for_restore = tf.train.Saver(variables_to_restore) - - # Init ops - if FLAGS.sync_replicas: - local_init_op = tf.get_collection('local_init_op')[0] - ready_for_local_init_op = tf.get_collection('ready_for_local_init_op')[0] - else: - local_init_op = tf.train.Supervisor.USE_DEFAULT - ready_for_local_init_op = tf.train.Supervisor.USE_DEFAULT - - is_chief = FLAGS.task == 0 - sv = tf.train.Supervisor( - logdir=FLAGS.train_dir, - is_chief=is_chief, - save_summaries_secs=30, - save_model_secs=30, - local_init_op=local_init_op, - ready_for_local_init_op=ready_for_local_init_op, - global_step=global_step) - - # Delay starting standard services to allow possible pretrained model restore. - with sv.managed_session( - master=FLAGS.master, - config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement), - start_standard_services=False) as sess: - # Initialization - if is_chief: - if pretrained_model_dir: - maybe_restore_pretrained_model(sess, saver_for_restore, - pretrained_model_dir) - if FLAGS.sync_replicas: - sess.run(tf.get_collection('chief_init_op')[0]) - sv.start_standard_services(sess) - - sv.start_queue_runners(sess) - - # Training loop - global_step_val = 0 - while not sv.should_stop() and global_step_val < FLAGS.max_steps: - global_step_val = train_step(sess, train_op, loss, global_step) - - # Final checkpoint - if is_chief and global_step_val >= FLAGS.max_steps: - sv.saver.save(sess, sv.save_path, global_step=global_step) - - -def maybe_restore_pretrained_model(sess, saver_for_restore, model_dir): - """Restores pretrained model if there is no ckpt model.""" - ckpt = tf.train.get_checkpoint_state(FLAGS.train_dir) - checkpoint_exists = ckpt and ckpt.model_checkpoint_path - if checkpoint_exists: - tf.logging.info('Checkpoint exists in FLAGS.train_dir; skipping ' - 'pretraining restore') - return - - pretrain_ckpt = tf.train.get_checkpoint_state(model_dir) - if not (pretrain_ckpt and pretrain_ckpt.model_checkpoint_path): - raise ValueError( - 'Asked to restore model from %s but no checkpoint found.' % model_dir) - saver_for_restore.restore(sess, pretrain_ckpt.model_checkpoint_path) - - -def train_step(sess, train_op, loss, global_step): - """Runs a single training step.""" - start_time = time.time() - _, loss_val, global_step_val = sess.run([train_op, loss, global_step]) - duration = time.time() - start_time - - # Logging - if global_step_val % 10 == 0: - examples_per_sec = FLAGS.batch_size / duration - sec_per_batch = float(duration) - - format_str = ('step %d, loss = %.2f (%.1f examples/sec; %.3f ' 'sec/batch)') - tf.logging.info(format_str % (global_step_val, loss_val, examples_per_sec, - sec_per_batch)) - - if np.isnan(loss_val): - raise OverflowError('Loss is nan') - - return global_step_val diff --git a/research/attention_ocr/README.md b/research/attention_ocr/README.md deleted file mode 100644 index f2042e573fa..00000000000 --- a/research/attention_ocr/README.md +++ /dev/null @@ -1,262 +0,0 @@ -# Attention-based Extraction of Structured Information from Street View Imagery - -[![PWC](https://img.shields.io/endpoint.svg?url=https://paperswithcode.com/badge/attention-based-extraction-of-structured/optical-character-recognition-on-fsns-test)](https://paperswithcode.com/sota/optical-character-recognition-on-fsns-test?p=attention-based-extraction-of-structured) -[![Paper](http://img.shields.io/badge/paper-arXiv.1704.03549-B3181B.svg)](https://arxiv.org/abs/1704.03549) -[![TensorFlow 1.15](https://img.shields.io/badge/tensorflow-1.15-brightgreen)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -*A TensorFlow model for real-world image text extraction problems.* - -This folder contains the code needed to train a new Attention OCR model on the -[FSNS dataset][FSNS] to transcribe street names in France. You can also train the code on your own data. - -More details can be found in our paper: - -["Attention-based Extraction of Structured Information from Street View -Imagery"](https://arxiv.org/abs/1704.03549) - -## Description - -* Paper presents a model based on ConvNets, RNN's and a novel attention mechanism. -Achieves **84.2%** on FSNS beating the previous benchmark (**72.46%**). Also studies -the speed/accuracy tradeoff that results from using CNN feature extractors of -different depths. - -## Contacts - -Authors - -* Zbigniew Wojna (zbigniewwojna@gmail.com) -* Alexander Gorban (gorban@google.com) - -Maintainer - -* Xavier Gibert ([@xavigibert](https://github.com/xavigibert)) - -## Table of Contents - -* [Requirements](https://github.com/tensorflow/models/blob/master/research/attention_ocr/README.md#requirements) -* [Dataset](https://github.com/tensorflow/models/blob/master/research/attention_ocr/README.md#dataset) -* [How to use this code](https://github.com/tensorflow/models/blob/master/research/attention_ocr/README.md#how-to-use-this-code) -* [Using your own image data](https://github.com/tensorflow/models/blob/master/research/attention_ocr/README.md#using-your-own-image-data) -* [How to use a pre-trained model](https://github.com/tensorflow/models/blob/master/research/attention_ocr/README.md#how-to-use-a-pre-trained-model) -* [Disclaimer](https://github.com/tensorflow/models/blob/master/research/attention_ocr/README.md#disclaimer) - -## Requirements - -1. Install the TensorFlow library ([instructions][TF]). For example: - -``` -python3 -m venv ~/.tensorflow -source ~/.tensorflow/bin/activate -pip install --upgrade pip -pip install --upgrade tensorflow-gpu=1.15 -``` - -2. At least 158GB of free disk space to download the FSNS dataset: - -``` -cd research/attention_ocr/python/datasets -aria2c -c -j 20 -i ../../../street/python/fsns_urls.txt -cd .. -``` - -3. 16GB of RAM or more; 32GB is recommended. -4. `train.py` works with both CPU and GPU, though using GPU is preferable. It has been tested with a Titan X and with a GTX980. - -[TF]: https://www.tensorflow.org/install/ -[FSNS]: https://github.com/tensorflow/models/tree/master/research/street - -## Dataset - -The French Street Name Signs (FSNS) dataset is split into subsets, -each of which is composed of multiple files. Note that these datasets -are very large. The approximate sizes are: - -* Train: 512 files of 300MB each. -* Validation: 64 files of 40MB each. -* Test: 64 files of 50MB each. -* The datasets download includes a directory `testdata` that contains -some small datasets that are big enough to test that models can -actually learn something. -* Total: around 158GB - -The download paths are in the following list: - -``` -https://download.tensorflow.org/data/fsns-20160927/charset_size=134.txt -https://download.tensorflow.org/data/fsns-20160927/test/test-00000-of-00064 -... -https://download.tensorflow.org/data/fsns-20160927/test/test-00063-of-00064 -https://download.tensorflow.org/data/fsns-20160927/testdata/arial-32-00000-of-00001 -https://download.tensorflow.org/data/fsns-20160927/testdata/fsns-00000-of-00001 -https://download.tensorflow.org/data/fsns-20160927/testdata/mnist-sample-00000-of-00001 -https://download.tensorflow.org/data/fsns-20160927/testdata/numbers-16-00000-of-00001 -https://download.tensorflow.org/data/fsns-20160927/train/train-00000-of-00512 -... -https://download.tensorflow.org/data/fsns-20160927/train/train-00511-of-00512 -https://download.tensorflow.org/data/fsns-20160927/validation/validation-00000-of-00064 -... -https://download.tensorflow.org/data/fsns-20160927/validation/validation-00063-of-00064 -``` - -All URLs are stored in the [research/street](https://github.com/tensorflow/models/tree/master/research/street) -repository in the text file `python/fsns_urls.txt`. - -## How to use this code - -To run all unit tests: - -``` -cd research/attention_ocr/python -find . -name "*_test.py" -printf '%P\n' | xargs python3 -m unittest -``` - -To train from scratch: - -``` -python train.py -``` - -To train a model using pre-trained Inception weights as initialization: - -``` -wget http://download.tensorflow.org/models/inception_v3_2016_08_28.tar.gz -tar xf inception_v3_2016_08_28.tar.gz -python train.py --checkpoint_inception=./inception_v3.ckpt -``` - -To fine tune the Attention OCR model using a checkpoint: - -``` -wget http://download.tensorflow.org/models/attention_ocr_2017_08_09.tar.gz -tar xf attention_ocr_2017_08_09.tar.gz -python train.py --checkpoint=model.ckpt-399731 -``` - -## Using your own image data - -You need to define a new dataset. There are two options: - -1. Store data in the same format as the FSNS dataset and just reuse the -[python/datasets/fsns.py](https://github.com/tensorflow/models/blob/master/research/attention_ocr/python/datasets/fsns.py) -module. E.g., create a file datasets/newtextdataset.py: -``` -import fsns - -DEFAULT_DATASET_DIR = 'path/to/the/dataset' - -DEFAULT_CONFIG = { - 'name': - 'MYDATASET', - 'splits': { - 'train': { - 'size': 123, - 'pattern': 'tfexample_train*' - }, - 'test': { - 'size': 123, - 'pattern': 'tfexample_test*' - } - }, - 'charset_filename': - 'charset_size.txt', - 'image_shape': (150, 600, 3), - 'num_of_views': - 4, - 'max_sequence_length': - 37, - 'null_code': - 42, - 'items_to_descriptions': { - 'image': - 'A [150 x 600 x 3] color image.', - 'label': - 'Characters codes.', - 'text': - 'A unicode string.', - 'length': - 'A length of the encoded text.', - 'num_of_views': - 'A number of different views stored within the image.' - } -} - - -def get_split(split_name, dataset_dir=None, config=None): - if not dataset_dir: - dataset_dir = DEFAULT_DATASET_DIR - if not config: - config = DEFAULT_CONFIG - - return fsns.get_split(split_name, dataset_dir, config) -``` -You will also need to include it into the `datasets/__init__.py` and specify the -dataset name in the command line. - -``` -python train.py --dataset_name=newtextdataset -``` - -Please note that eval.py will also require the same flag. - -To learn how to store a data in the FSNS - format please refer to the https://stackoverflow.com/a/44461910/743658. - -2. Define a new dataset format. The model needs the following data to train: - -- images: input images, shape [batch_size x H x W x 3]; -- labels: ground truth label ids, shape=[batch_size x seq_length]; -- labels_one_hot: labels in one-hot encoding, shape [batch_size x seq_length x num_char_classes]; - -Refer to [python/data_provider.py](https://github.com/tensorflow/models/blob/master/research/attention_ocr/python/data_provider.py#L33) -for more details. You can use [python/datasets/fsns.py](https://github.com/tensorflow/models/blob/master/research/attention_ocr/python/datasets/fsns.py) -as the example. - -## How to use a pre-trained model - -The inference part was not released yet, but it is pretty straightforward to -implement one in Python or C++. - -The recommended way is to use the [Serving infrastructure][serving]. - -To export to SavedModel format: - -``` -python model_export.py \ - --checkpoint=model.ckpt-399731 \ - --export_dir=/tmp/attention_ocr_export -``` - -Alternatively you can: -1. define a placeholder for images (or use directly an numpy array) -2. [create a graph ](https://github.com/tensorflow/models/blob/master/research/attention_ocr/python/eval.py#L60) -``` -endpoints = model.create_base(images_placeholder, labels_one_hot=None) -``` -3. [load a pretrained model](https://github.com/tensorflow/models/blob/master/research/attention_ocr/python/model.py#L494) -4. run computations through the graph: -``` -predictions = sess.run(endpoints.predicted_chars, - feed_dict={images_placeholder:images_actual_data}) -``` -5. Convert character IDs (predictions) to UTF8 using the provided charset file. - -Please note that tensor names may change overtime and old stored checkpoints can -become unloadable. In many cases such backward incompatible changes can be -fixed with a [string substitution][1] to update the checkpoint itself or using a -custom var_list with [assign_from_checkpoint_fn][2]. For anything -other than a one time experiment please use the [TensorFlow Serving][serving]. - -[1]: https://github.com/tensorflow/tensorflow/blob/aaf7adc/tensorflow/contrib/rnn/python/tools/checkpoint_convert.py -[2]: https://www.tensorflow.org/api_docs/python/tf/contrib/framework/assign_from_checkpoint_fn -[serving]: https://www.tensorflow.org/tfx/serving/serving_basic - -## Disclaimer - -This code is a modified version of the internal model we used for our paper. -Currently it reaches 83.79% full sequence accuracy after 400k steps of training. -The main difference between this version and the version used in the paper - for -the paper we used a distributed training with 50 GPU (K80) workers (asynchronous -updates), the provided checkpoint was created using this code after ~6 days of -training on a single GPU (Titan X) (it reached 81% after 24 hours of training), -the coordinate encoding is disabled by default. diff --git a/research/attention_ocr/python/all_jobs.screenrc b/research/attention_ocr/python/all_jobs.screenrc deleted file mode 100644 index ef7fdf23738..00000000000 --- a/research/attention_ocr/python/all_jobs.screenrc +++ /dev/null @@ -1,9 +0,0 @@ -# A GPU/screen config to run all jobs for training and evaluation in parallel. -# Execute: -# source /path/to/your/virtualenv/bin/activate -# screen -R TF -c all_jobs.screenrc - -screen -t train 0 python train.py --train_log_dir=workdir/train -screen -t eval_train 1 python eval.py --split_name=train --train_log_dir=workdir/train --eval_log_dir=workdir/eval_train -screen -t eval_test 2 python eval.py --split_name=test --train_log_dir=workdir/train --eval_log_dir=workdir/eval_test -screen -t tensorboard 3 tensorboard --logdir=workdir diff --git a/research/attention_ocr/python/common_flags.py b/research/attention_ocr/python/common_flags.py deleted file mode 100644 index 60aa49ffea8..00000000000 --- a/research/attention_ocr/python/common_flags.py +++ /dev/null @@ -1,157 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Define flags are common for both train.py and eval.py scripts.""" -import logging -import sys - -from tensorflow.compat.v1 import flags - -import datasets -import model - -FLAGS = flags.FLAGS - -logging.basicConfig( - level=logging.DEBUG, - stream=sys.stderr, - format='%(levelname)s ' - '%(asctime)s.%(msecs)06d: ' - '%(filename)s: ' - '%(lineno)d ' - '%(message)s', - datefmt='%Y-%m-%d %H:%M:%S') - - -_common_flags_defined = False - -def define(): - """Define common flags.""" - # yapf: disable - # common_flags.define() may be called multiple times in unit tests. - global _common_flags_defined - if _common_flags_defined: - return - _common_flags_defined = True - - flags.DEFINE_integer('batch_size', 32, - 'Batch size.') - - flags.DEFINE_integer('crop_width', None, - 'Width of the central crop for images.') - - flags.DEFINE_integer('crop_height', None, - 'Height of the central crop for images.') - - flags.DEFINE_string('train_log_dir', '/tmp/attention_ocr/train', - 'Directory where to write event logs.') - - flags.DEFINE_string('dataset_name', 'fsns', - 'Name of the dataset. Supported: fsns') - - flags.DEFINE_string('split_name', 'train', - 'Dataset split name to run evaluation for: test,train.') - - flags.DEFINE_string('dataset_dir', None, - 'Dataset root folder.') - - flags.DEFINE_string('checkpoint', '', - 'Path for checkpoint to restore weights from.') - - flags.DEFINE_string('master', - '', - 'BNS name of the TensorFlow master to use.') - - # Model hyper parameters - flags.DEFINE_float('learning_rate', 0.004, - 'learning rate') - - flags.DEFINE_string('optimizer', 'momentum', - 'the optimizer to use') - - flags.DEFINE_float('momentum', 0.9, - 'momentum value for the momentum optimizer if used') - - flags.DEFINE_bool('use_augment_input', True, - 'If True will use image augmentation') - - # Method hyper parameters - # conv_tower_fn - flags.DEFINE_string('final_endpoint', 'Mixed_5d', - 'Endpoint to cut inception tower') - - # sequence_logit_fn - flags.DEFINE_bool('use_attention', True, - 'If True will use the attention mechanism') - - flags.DEFINE_bool('use_autoregression', True, - 'If True will use autoregression (a feedback link)') - - flags.DEFINE_integer('num_lstm_units', 256, - 'number of LSTM units for sequence LSTM') - - flags.DEFINE_float('weight_decay', 0.00004, - 'weight decay for char prediction FC layers') - - flags.DEFINE_float('lstm_state_clip_value', 10.0, - 'cell state is clipped by this value prior to the cell' - ' output activation') - - # 'sequence_loss_fn' - flags.DEFINE_float('label_smoothing', 0.1, - 'weight for label smoothing') - - flags.DEFINE_bool('ignore_nulls', True, - 'ignore null characters for computing the loss') - - flags.DEFINE_bool('average_across_timesteps', False, - 'divide the returned cost by the total label weight') - # yapf: enable - - -def get_crop_size(): - if FLAGS.crop_width and FLAGS.crop_height: - return (FLAGS.crop_width, FLAGS.crop_height) - else: - return None - - -def create_dataset(split_name): - ds_module = getattr(datasets, FLAGS.dataset_name) - return ds_module.get_split(split_name, dataset_dir=FLAGS.dataset_dir) - - -def create_mparams(): - return { - 'conv_tower_fn': - model.ConvTowerParams(final_endpoint=FLAGS.final_endpoint), - 'sequence_logit_fn': - model.SequenceLogitsParams( - use_attention=FLAGS.use_attention, - use_autoregression=FLAGS.use_autoregression, - num_lstm_units=FLAGS.num_lstm_units, - weight_decay=FLAGS.weight_decay, - lstm_state_clip_value=FLAGS.lstm_state_clip_value), - 'sequence_loss_fn': - model.SequenceLossParams( - label_smoothing=FLAGS.label_smoothing, - ignore_nulls=FLAGS.ignore_nulls, - average_across_timesteps=FLAGS.average_across_timesteps) - } - - -def create_model(*args, **kwargs): - ocr_model = model.Model(mparams=create_mparams(), *args, **kwargs) - return ocr_model diff --git a/research/attention_ocr/python/data_provider.py b/research/attention_ocr/python/data_provider.py deleted file mode 100644 index 7a5a2d40cee..00000000000 --- a/research/attention_ocr/python/data_provider.py +++ /dev/null @@ -1,197 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Functions to read, decode and pre-process input data for the Model. -""" -import collections -import functools -import tensorflow as tf -from tensorflow.contrib import slim - -import inception_preprocessing - -# Tuple to store input data endpoints for the Model. -# It has following fields (tensors): -# images: input images, -# shape [batch_size x H x W x 3]; -# labels: ground truth label ids, -# shape=[batch_size x seq_length]; -# labels_one_hot: labels in one-hot encoding, -# shape [batch_size x seq_length x num_char_classes]; -InputEndpoints = collections.namedtuple( - 'InputEndpoints', ['images', 'images_orig', 'labels', 'labels_one_hot']) - -# A namedtuple to define a configuration for shuffled batch fetching. -# num_batching_threads: A number of parallel threads to fetch data. -# queue_capacity: a max number of elements in the batch shuffling queue. -# min_after_dequeue: a min number elements in the queue after a dequeue, used -# to ensure a level of mixing of elements. -ShuffleBatchConfig = collections.namedtuple('ShuffleBatchConfig', [ - 'num_batching_threads', 'queue_capacity', 'min_after_dequeue' -]) - -DEFAULT_SHUFFLE_CONFIG = ShuffleBatchConfig( - num_batching_threads=8, queue_capacity=3000, min_after_dequeue=1000) - - -def augment_image(image): - """Augmentation the image with a random modification. - - Args: - image: input Tensor image of rank 3, with the last dimension - of size 3. - - Returns: - Distorted Tensor image of the same shape. - """ - with tf.compat.v1.variable_scope('AugmentImage'): - height = image.get_shape().dims[0].value - width = image.get_shape().dims[1].value - - # Random crop cut from the street sign image, resized to the same size. - # Assures that the crop is covers at least 0.8 area of the input image. - bbox_begin, bbox_size, _ = tf.image.sample_distorted_bounding_box( - image_size=tf.shape(input=image), - bounding_boxes=tf.zeros([0, 0, 4]), - min_object_covered=0.8, - aspect_ratio_range=[0.8, 1.2], - area_range=[0.8, 1.0], - use_image_if_no_bounding_boxes=True) - distorted_image = tf.slice(image, bbox_begin, bbox_size) - - # Randomly chooses one of the 4 interpolation methods - distorted_image = inception_preprocessing.apply_with_random_selector( - distorted_image, - lambda x, method: tf.image.resize(x, [height, width], method), - num_cases=4) - distorted_image.set_shape([height, width, 3]) - - # Color distortion - distorted_image = inception_preprocessing.apply_with_random_selector( - distorted_image, - functools.partial( - inception_preprocessing.distort_color, fast_mode=False), - num_cases=4) - distorted_image = tf.clip_by_value(distorted_image, -1.5, 1.5) - - return distorted_image - - -def central_crop(image, crop_size): - """Returns a central crop for the specified size of an image. - - Args: - image: A tensor with shape [height, width, channels] - crop_size: A tuple (crop_width, crop_height) - - Returns: - A tensor of shape [crop_height, crop_width, channels]. - """ - with tf.compat.v1.variable_scope('CentralCrop'): - target_width, target_height = crop_size - image_height, image_width = tf.shape( - input=image)[0], tf.shape(input=image)[1] - assert_op1 = tf.Assert( - tf.greater_equal(image_height, target_height), - ['image_height < target_height', image_height, target_height]) - assert_op2 = tf.Assert( - tf.greater_equal(image_width, target_width), - ['image_width < target_width', image_width, target_width]) - with tf.control_dependencies([assert_op1, assert_op2]): - offset_width = tf.cast((image_width - target_width) / 2, tf.int32) - offset_height = tf.cast((image_height - target_height) / 2, tf.int32) - return tf.image.crop_to_bounding_box(image, offset_height, offset_width, - target_height, target_width) - - -def preprocess_image(image, augment=False, central_crop_size=None, - num_towers=4): - """Normalizes image to have values in a narrow range around zero. - - Args: - image: a [H x W x 3] uint8 tensor. - augment: optional, if True do random image distortion. - central_crop_size: A tuple (crop_width, crop_height). - num_towers: optional, number of shots of the same image in the input image. - - Returns: - A float32 tensor of shape [H x W x 3] with RGB values in the required - range. - """ - with tf.compat.v1.variable_scope('PreprocessImage'): - image = tf.image.convert_image_dtype(image, dtype=tf.float32) - if augment or central_crop_size: - if num_towers == 1: - images = [image] - else: - images = tf.split(value=image, num_or_size_splits=num_towers, axis=1) - if central_crop_size: - view_crop_size = (int(central_crop_size[0] / num_towers), - central_crop_size[1]) - images = [central_crop(img, view_crop_size) for img in images] - if augment: - images = [augment_image(img) for img in images] - image = tf.concat(images, 1) - - return image - - -def get_data(dataset, - batch_size, - augment=False, - central_crop_size=None, - shuffle_config=None, - shuffle=True): - """Wraps calls to DatasetDataProviders and shuffle_batch. - - For more details about supported Dataset objects refer to datasets/fsns.py. - - Args: - dataset: a slim.data.dataset.Dataset object. - batch_size: number of samples per batch. - augment: optional, if True does random image distortion. - central_crop_size: A CharLogittuple (crop_width, crop_height). - shuffle_config: A namedtuple ShuffleBatchConfig. - shuffle: if True use data shuffling. - - Returns: - - """ - if not shuffle_config: - shuffle_config = DEFAULT_SHUFFLE_CONFIG - - provider = slim.dataset_data_provider.DatasetDataProvider( - dataset, - shuffle=shuffle, - common_queue_capacity=2 * batch_size, - common_queue_min=batch_size) - image_orig, label = provider.get(['image', 'label']) - - image = preprocess_image( - image_orig, augment, central_crop_size, num_towers=dataset.num_of_views) - label_one_hot = slim.one_hot_encoding(label, dataset.num_char_classes) - - images, images_orig, labels, labels_one_hot = (tf.compat.v1.train.shuffle_batch( - [image, image_orig, label, label_one_hot], - batch_size=batch_size, - num_threads=shuffle_config.num_batching_threads, - capacity=shuffle_config.queue_capacity, - min_after_dequeue=shuffle_config.min_after_dequeue)) - - return InputEndpoints( - images=images, - images_orig=images_orig, - labels=labels, - labels_one_hot=labels_one_hot) diff --git a/research/attention_ocr/python/data_provider_test.py b/research/attention_ocr/python/data_provider_test.py deleted file mode 100644 index 551bc75e02c..00000000000 --- a/research/attention_ocr/python/data_provider_test.py +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for data_provider.""" - -import numpy as np -import tensorflow as tf -from tensorflow.contrib.slim import queues - -import datasets -import data_provider - - -class DataProviderTest(tf.test.TestCase): - def setUp(self): - tf.test.TestCase.setUp(self) - - def test_preprocessed_image_values_are_in_range(self): - image_shape = (5, 4, 3) - fake_image = np.random.randint(low=0, high=255, size=image_shape) - image_tf = data_provider.preprocess_image(fake_image) - - with self.test_session() as sess: - image_np = sess.run(image_tf) - - self.assertEqual(image_np.shape, image_shape) - min_value, max_value = np.min(image_np), np.max(image_np) - self.assertTrue((-1.28 < min_value) and (min_value < 1.27)) - self.assertTrue((-1.28 < max_value) and (max_value < 1.27)) - - def test_provided_data_has_correct_shape(self): - batch_size = 4 - data = data_provider.get_data( - dataset=datasets.fsns_test.get_test_split(), - batch_size=batch_size, - augment=True, - central_crop_size=None) - - with self.test_session() as sess, queues.QueueRunners(sess): - images_np, labels_np = sess.run([data.images, data.labels_one_hot]) - - self.assertEqual(images_np.shape, (batch_size, 150, 600, 3)) - self.assertEqual(labels_np.shape, (batch_size, 37, 134)) - - def test_optionally_applies_central_crop(self): - batch_size = 4 - data = data_provider.get_data( - dataset=datasets.fsns_test.get_test_split(), - batch_size=batch_size, - augment=True, - central_crop_size=(500, 100)) - - with self.test_session() as sess, queues.QueueRunners(sess): - images_np = sess.run(data.images) - - self.assertEqual(images_np.shape, (batch_size, 100, 500, 3)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/datasets/__init__.py b/research/attention_ocr/python/datasets/__init__.py deleted file mode 100644 index 5d9a20dc7b9..00000000000 --- a/research/attention_ocr/python/datasets/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -from datasets import fsns -from datasets import fsns_test - -__all__ = [fsns, fsns_test] diff --git a/research/attention_ocr/python/datasets/fsns.py b/research/attention_ocr/python/datasets/fsns.py deleted file mode 100644 index ab6d0f28b13..00000000000 --- a/research/attention_ocr/python/datasets/fsns.py +++ /dev/null @@ -1,185 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Configuration to read FSNS dataset https://goo.gl/3Ldm8v.""" - -import os -import re -import tensorflow as tf -from tensorflow.contrib import slim -import logging - -DEFAULT_DATASET_DIR = os.path.join(os.path.dirname(__file__), 'data', 'fsns') - -# The dataset configuration, should be used only as a default value. -DEFAULT_CONFIG = { - 'name': 'FSNS', - 'splits': { - 'train': { - 'size': 1044868, - 'pattern': 'train/train*' - }, - 'test': { - 'size': 20404, - 'pattern': 'test/test*' - }, - 'validation': { - 'size': 16150, - 'pattern': 'validation/validation*' - } - }, - 'charset_filename': 'charset_size=134.txt', - 'image_shape': (150, 600, 3), - 'num_of_views': 4, - 'max_sequence_length': 37, - 'null_code': 133, - 'items_to_descriptions': { - 'image': 'A [150 x 600 x 3] color image.', - 'label': 'Characters codes.', - 'text': 'A unicode string.', - 'length': 'A length of the encoded text.', - 'num_of_views': 'A number of different views stored within the image.' - } -} - - -def read_charset(filename, null_character=u'\u2591'): - """Reads a charset definition from a tab separated text file. - - charset file has to have format compatible with the FSNS dataset. - - Args: - filename: a path to the charset file. - null_character: a unicode character used to replace '' character. the - default value is a light shade block '░'. - - Returns: - a dictionary with keys equal to character codes and values - unicode - characters. - """ - pattern = re.compile(r'(\d+)\t(.+)') - charset = {} - with tf.io.gfile.GFile(filename) as f: - for i, line in enumerate(f): - m = pattern.match(line) - if m is None: - logging.warning('incorrect charset file. line #%d: %s', i, line) - continue - code = int(m.group(1)) - char = m.group(2) - if char == '': - char = null_character - charset[code] = char - return charset - - -class _NumOfViewsHandler(slim.tfexample_decoder.ItemHandler): - """Convenience handler to determine number of views stored in an image.""" - - def __init__(self, width_key, original_width_key, num_of_views): - super(_NumOfViewsHandler, self).__init__([width_key, original_width_key]) - self._width_key = width_key - self._original_width_key = original_width_key - self._num_of_views = num_of_views - - def tensors_to_item(self, keys_to_tensors): - return tf.cast( - self._num_of_views * keys_to_tensors[self._original_width_key] / - keys_to_tensors[self._width_key], dtype=tf.int64) - - -def get_split(split_name, dataset_dir=None, config=None): - """Returns a dataset tuple for FSNS dataset. - - Args: - split_name: A train/test split name. - dataset_dir: The base directory of the dataset sources, by default it uses - a predefined CNS path (see DEFAULT_DATASET_DIR). - config: A dictionary with dataset configuration. If None - will use the - DEFAULT_CONFIG. - - Returns: - A `Dataset` namedtuple. - - Raises: - ValueError: if `split_name` is not a valid train/test split. - """ - if not dataset_dir: - dataset_dir = DEFAULT_DATASET_DIR - - if not config: - config = DEFAULT_CONFIG - - if split_name not in config['splits']: - raise ValueError('split name %s was not recognized.' % split_name) - - logging.info('Using %s dataset split_name=%s dataset_dir=%s', config['name'], - split_name, dataset_dir) - - # Ignores the 'image/height' feature. - zero = tf.zeros([1], dtype=tf.int64) - keys_to_features = { - 'image/encoded': - tf.io.FixedLenFeature((), tf.string, default_value=''), - 'image/format': - tf.io.FixedLenFeature((), tf.string, default_value='png'), - 'image/width': - tf.io.FixedLenFeature([1], tf.int64, default_value=zero), - 'image/orig_width': - tf.io.FixedLenFeature([1], tf.int64, default_value=zero), - 'image/class': - tf.io.FixedLenFeature([config['max_sequence_length']], tf.int64), - 'image/unpadded_class': - tf.io.VarLenFeature(tf.int64), - 'image/text': - tf.io.FixedLenFeature([1], tf.string, default_value=''), - } - items_to_handlers = { - 'image': - slim.tfexample_decoder.Image( - shape=config['image_shape'], - image_key='image/encoded', - format_key='image/format'), - 'label': - slim.tfexample_decoder.Tensor(tensor_key='image/class'), - 'text': - slim.tfexample_decoder.Tensor(tensor_key='image/text'), - 'num_of_views': - _NumOfViewsHandler( - width_key='image/width', - original_width_key='image/orig_width', - num_of_views=config['num_of_views']) - } - decoder = slim.tfexample_decoder.TFExampleDecoder(keys_to_features, - items_to_handlers) - charset_file = os.path.join(dataset_dir, config['charset_filename']) - charset = read_charset(charset_file) - file_pattern = os.path.join(dataset_dir, - config['splits'][split_name]['pattern']) - return slim.dataset.Dataset( - data_sources=file_pattern, - reader=tf.compat.v1.TFRecordReader, - decoder=decoder, - num_samples=config['splits'][split_name]['size'], - items_to_descriptions=config['items_to_descriptions'], - # additional parameters for convenience. - charset=charset, - charset_file=charset_file, - image_shape=config['image_shape'], - num_char_classes=len(charset), - num_of_views=config['num_of_views'], - max_sequence_length=config['max_sequence_length'], - null_code=config['null_code']) diff --git a/research/attention_ocr/python/datasets/fsns_test.py b/research/attention_ocr/python/datasets/fsns_test.py deleted file mode 100644 index 2f5f3afc78e..00000000000 --- a/research/attention_ocr/python/datasets/fsns_test.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for FSNS datasets module.""" - -import collections -import os -import tensorflow as tf -from tensorflow.contrib import slim - -from datasets import fsns -from datasets import unittest_utils -from tensorflow.compat.v1 import flags - -FLAGS = flags.FLAGS - - -def get_test_split(): - config = fsns.DEFAULT_CONFIG.copy() - config['splits'] = {'test': {'size': 5, 'pattern': 'fsns-00000-of-00001'}} - return fsns.get_split('test', dataset_dir(), config) - - -def dataset_dir(): - return os.path.join(os.path.dirname(__file__), 'testdata/fsns') - - -class FsnsTest(tf.test.TestCase): - def test_decodes_example_proto(self): - expected_label = range(37) - expected_image, encoded = unittest_utils.create_random_image( - 'PNG', shape=(150, 600, 3)) - serialized = unittest_utils.create_serialized_example({ - 'image/encoded': [encoded], - 'image/format': [b'PNG'], - 'image/class': - expected_label, - 'image/unpadded_class': - range(10), - 'image/text': [b'Raw text'], - 'image/orig_width': [150], - 'image/width': [600] - }) - - decoder = fsns.get_split('train', dataset_dir()).decoder - with self.test_session() as sess: - data_tuple = collections.namedtuple('DecodedData', decoder.list_items()) - data = sess.run(data_tuple(*decoder.decode(serialized))) - - self.assertAllEqual(expected_image, data.image) - self.assertAllEqual(expected_label, data.label) - self.assertEqual([b'Raw text'], data.text) - self.assertEqual([1], data.num_of_views) - - def test_label_has_shape_defined(self): - serialized = 'fake' - decoder = fsns.get_split('train', dataset_dir()).decoder - - [label_tf] = decoder.decode(serialized, ['label']) - - self.assertEqual(label_tf.get_shape().dims[0], 37) - - def test_dataset_tuple_has_all_extra_attributes(self): - dataset = fsns.get_split('train', dataset_dir()) - - self.assertTrue(dataset.charset) - self.assertTrue(dataset.num_char_classes) - self.assertTrue(dataset.num_of_views) - self.assertTrue(dataset.max_sequence_length) - self.assertTrue(dataset.null_code) - - def test_can_use_the_test_data(self): - batch_size = 1 - dataset = get_test_split() - provider = slim.dataset_data_provider.DatasetDataProvider( - dataset, - shuffle=True, - common_queue_capacity=2 * batch_size, - common_queue_min=batch_size) - image_tf, label_tf = provider.get(['image', 'label']) - - with self.test_session() as sess: - sess.run(tf.compat.v1.global_variables_initializer()) - with slim.queues.QueueRunners(sess): - image_np, label_np = sess.run([image_tf, label_tf]) - - self.assertEqual((150, 600, 3), image_np.shape) - self.assertEqual((37, ), label_np.shape) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/datasets/testdata/fsns/charset_size=134.txt b/research/attention_ocr/python/datasets/testdata/fsns/charset_size=134.txt deleted file mode 100644 index 5c7fcde2ae0..00000000000 --- a/research/attention_ocr/python/datasets/testdata/fsns/charset_size=134.txt +++ /dev/null @@ -1,139 +0,0 @@ -0 -133 -1 l -2 ’ -3 é -4 t -5 e -6 i -7 n -8 s -9 x -10 g -11 u -12 o -13 1 -14 8 -15 7 -16 0 -17 - -18 . -19 p -20 a -21 r -22 è -23 d -24 c -25 V -26 v -27 b -28 m -29 ) -30 C -31 z -32 S -33 y -34 , -35 k -36 É -37 A -38 h -39 E -40 » -41 D -42 / -43 H -44 M -45 ( -46 G -47 P -48 ç -2 ' -49 R -50 f -51 " -52 2 -53 j -54 | -55 N -56 6 -57 ° -58 5 -59 T -60 O -61 U -62 3 -63 % -64 9 -65 q -66 Z -67 B -68 K -69 w -70 W -71 : -72 4 -73 L -74 F -75 ] -76 ï -2 ‘ -77 I -78 J -79 ä -80 î -81 ; -82 à -83 ê -84 X -85 ü -86 Y -87 ô -88 = -89 + -90 \ -91 { -92 } -93 _ -94 Q -95 œ -96 ñ -97 * -98 ! -99 Ü -51 “ -100 â -101 Ç -102 Œ -103 û -104 ? -105 $ -106 ë -107 « -108 € -109 & -110 < -51 ” -111 æ -112 # -113 ® -114  -115 È -116 > -117 [ -17 — -118 Æ -119 ù -120 Î -121 Ô -122 ÿ -123 À -124 Ê -125 @ -126 Ï -127 © -128 Ë -129 Ù -130 £ -131 Ÿ -132 Û diff --git a/research/attention_ocr/python/datasets/testdata/fsns/download_data.py b/research/attention_ocr/python/datasets/testdata/fsns/download_data.py deleted file mode 100644 index 126ef58060b..00000000000 --- a/research/attention_ocr/python/datasets/testdata/fsns/download_data.py +++ /dev/null @@ -1,17 +0,0 @@ -import urllib.request -import tensorflow as tf -import itertools - -URL = 'http://download.tensorflow.org/data/fsns-20160927/testdata/fsns-00000-of-00001' -DST_ORIG = 'fsns-00000-of-00001.orig' -DST = 'fsns-00000-of-00001' -KEEP_NUM_RECORDS = 5 - -print('Downloading %s ...' % URL) -urllib.request.urlretrieve(URL, DST_ORIG) - -print('Writing %d records from %s to %s ...' % - (KEEP_NUM_RECORDS, DST_ORIG, DST)) -with tf.io.TFRecordWriter(DST) as writer: - for raw_record in itertools.islice(tf.compat.v1.python_io.tf_record_iterator(DST_ORIG), KEEP_NUM_RECORDS): - writer.write(raw_record) diff --git a/research/attention_ocr/python/datasets/testdata/fsns/fsns-00000-of-00001 b/research/attention_ocr/python/datasets/testdata/fsns/fsns-00000-of-00001 deleted file mode 100644 index 4f2f1885297..00000000000 Binary files a/research/attention_ocr/python/datasets/testdata/fsns/fsns-00000-of-00001 and /dev/null differ diff --git a/research/attention_ocr/python/datasets/testdata/fsns/links.txt b/research/attention_ocr/python/datasets/testdata/fsns/links.txt deleted file mode 100644 index da98d305fa0..00000000000 --- a/research/attention_ocr/python/datasets/testdata/fsns/links.txt +++ /dev/null @@ -1 +0,0 @@ -http://download.tensorflow.org/data/fsns-20160927/testdata/fsns-00000-of-00001 diff --git a/research/attention_ocr/python/datasets/unittest_utils.py b/research/attention_ocr/python/datasets/unittest_utils.py deleted file mode 100644 index 7f483dbfaf6..00000000000 --- a/research/attention_ocr/python/datasets/unittest_utils.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Functions to make unit testing easier.""" - -import numpy as np -import io -from PIL import Image as PILImage -import tensorflow as tf - -def create_random_image(image_format, shape): - """Creates an image with random values. - - Args: - image_format: An image format (PNG or JPEG). - shape: A tuple with image shape (including channels). - - Returns: - A tuple (, ) - """ - image = np.random.randint(low=0, high=255, size=shape, dtype='uint8') - fd = io.BytesIO() - image_pil = PILImage.fromarray(image) - image_pil.save(fd, image_format, subsampling=0, quality=100) - return image, fd.getvalue() - - -def create_serialized_example(name_to_values): - """Creates a tf.Example proto using a dictionary. - - It automatically detects type of values and define a corresponding feature. - - Args: - name_to_values: A dictionary. - - Returns: - tf.Example proto. - """ - example = tf.train.Example() - for name, values in name_to_values.items(): - feature = example.features.feature[name] - if isinstance(values[0], str) or isinstance(values[0], bytes): - add = feature.bytes_list.value.extend - elif isinstance(values[0], float): - add = feature.float32_list.value.extend - elif isinstance(values[0], int): - add = feature.int64_list.value.extend - else: - raise AssertionError('Unsupported type: %s' % type(values[0])) - add(values) - return example.SerializeToString() diff --git a/research/attention_ocr/python/datasets/unittest_utils_test.py b/research/attention_ocr/python/datasets/unittest_utils_test.py deleted file mode 100644 index c2413874637..00000000000 --- a/research/attention_ocr/python/datasets/unittest_utils_test.py +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for unittest_utils.""" - -import numpy as np -import io -from PIL import Image as PILImage -import tensorflow as tf - -from datasets import unittest_utils - - -class UnittestUtilsTest(tf.test.TestCase): - def test_creates_an_image_of_specified_shape(self): - image, _ = unittest_utils.create_random_image('PNG', (10, 20, 3)) - self.assertEqual(image.shape, (10, 20, 3)) - - def test_encoded_image_corresponds_to_numpy_array(self): - image, encoded = unittest_utils.create_random_image('PNG', (20, 10, 3)) - pil_image = PILImage.open(io.BytesIO(encoded)) - self.assertAllEqual(image, np.array(pil_image)) - - def test_created_example_has_correct_values(self): - example_serialized = unittest_utils.create_serialized_example({ - 'labels': [1, 2, 3], - 'data': [b'FAKE'] - }) - example = tf.train.Example() - example.ParseFromString(example_serialized) - self.assertProtoEquals(""" - features { - feature { - key: "labels" - value { int64_list { - value: 1 - value: 2 - value: 3 - }} - } - feature { - key: "data" - value { bytes_list { - value: "FAKE" - }} - } - } - """, example) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/demo_inference.py b/research/attention_ocr/python/demo_inference.py deleted file mode 100644 index 8c6531d844f..00000000000 --- a/research/attention_ocr/python/demo_inference.py +++ /dev/null @@ -1,97 +0,0 @@ -"""A script to run inference on a set of image files. - -NOTE #1: The Attention OCR model was trained only using FSNS train dataset and -it will work only for images which look more or less similar to french street -names. In order to apply it to images from a different distribution you need -to retrain (or at least fine-tune) it using images from that distribution. - -NOTE #2: This script exists for demo purposes only. It is highly recommended -to use tools and mechanisms provided by the TensorFlow Serving system to run -inference on TensorFlow models in production: -https://www.tensorflow.org/serving/serving_basic - -Usage: -python demo_inference.py --batch_size=32 \ - --checkpoint=model.ckpt-399731\ - --image_path_pattern=./datasets/data/fsns/temp/fsns_train_%02d.png -""" -import numpy as np -import PIL.Image - -import tensorflow as tf -from tensorflow.compat.v1 import flags -from tensorflow.python.training import monitored_session - -import common_flags -import datasets -import data_provider - -FLAGS = flags.FLAGS -common_flags.define() - -# e.g. ./datasets/data/fsns/temp/fsns_train_%02d.png -flags.DEFINE_string('image_path_pattern', '', - 'A file pattern with a placeholder for the image index.') - - -def get_dataset_image_size(dataset_name): - # Ideally this info should be exposed through the dataset interface itself. - # But currently it is not available by other means. - ds_module = getattr(datasets, dataset_name) - height, width, _ = ds_module.DEFAULT_CONFIG['image_shape'] - return width, height - - -def load_images(file_pattern, batch_size, dataset_name): - width, height = get_dataset_image_size(dataset_name) - images_actual_data = np.ndarray(shape=(batch_size, height, width, 3), - dtype='uint8') - for i in range(batch_size): - path = file_pattern % i - print("Reading %s" % path) - pil_image = PIL.Image.open(tf.io.gfile.GFile(path, 'rb')) - images_actual_data[i, ...] = np.asarray(pil_image) - return images_actual_data - - -def create_model(batch_size, dataset_name): - width, height = get_dataset_image_size(dataset_name) - dataset = common_flags.create_dataset(split_name=FLAGS.split_name) - model = common_flags.create_model( - num_char_classes=dataset.num_char_classes, - seq_length=dataset.max_sequence_length, - num_views=dataset.num_of_views, - null_code=dataset.null_code, - charset=dataset.charset) - raw_images = tf.compat.v1.placeholder( - tf.uint8, shape=[batch_size, height, width, 3]) - images = tf.map_fn(data_provider.preprocess_image, raw_images, - dtype=tf.float32) - endpoints = model.create_base(images, labels_one_hot=None) - return raw_images, endpoints - - -def run(checkpoint, batch_size, dataset_name, image_path_pattern): - images_placeholder, endpoints = create_model(batch_size, - dataset_name) - images_data = load_images(image_path_pattern, batch_size, - dataset_name) - session_creator = monitored_session.ChiefSessionCreator( - checkpoint_filename_with_path=checkpoint) - with monitored_session.MonitoredSession( - session_creator=session_creator) as sess: - predictions = sess.run(endpoints.predicted_text, - feed_dict={images_placeholder: images_data}) - return [pr_bytes.decode('utf-8') for pr_bytes in predictions.tolist()] - - -def main(_): - print("Predicted strings:") - predictions = run(FLAGS.checkpoint, FLAGS.batch_size, FLAGS.dataset_name, - FLAGS.image_path_pattern) - for line in predictions: - print(line) - - -if __name__ == '__main__': - tf.compat.v1.app.run() diff --git a/research/attention_ocr/python/demo_inference_test.py b/research/attention_ocr/python/demo_inference_test.py deleted file mode 100644 index 91b86033832..00000000000 --- a/research/attention_ocr/python/demo_inference_test.py +++ /dev/null @@ -1,91 +0,0 @@ -#!/usr/bin/python -# -*- coding: UTF-8 -*- -import os -import demo_inference -import tensorflow as tf -from tensorflow.python.training import monitored_session -from tensorflow.compat.v1 import flags - -_CHECKPOINT = 'model.ckpt-399731' -_CHECKPOINT_URL = 'http://download.tensorflow.org/models/attention_ocr_2017_08_09.tar.gz' - - -class DemoInferenceTest(tf.test.TestCase): - def setUp(self): - super(DemoInferenceTest, self).setUp() - for suffix in ['.meta', '.index', '.data-00000-of-00001']: - filename = _CHECKPOINT + suffix - self.assertTrue(tf.io.gfile.exists(filename), - msg='Missing checkpoint file %s. ' - 'Please download and extract it from %s' % - (filename, _CHECKPOINT_URL)) - self._batch_size = 32 - flags.FLAGS.dataset_dir = os.path.join( - os.path.dirname(__file__), 'datasets/testdata/fsns') - - def test_moving_variables_properly_loaded_from_a_checkpoint(self): - batch_size = 32 - dataset_name = 'fsns' - images_placeholder, endpoints = demo_inference.create_model(batch_size, - dataset_name) - image_path_pattern = 'testdata/fsns_train_%02d.png' - images_data = demo_inference.load_images(image_path_pattern, batch_size, - dataset_name) - tensor_name = 'AttentionOcr_v1/conv_tower_fn/INCE/InceptionV3/Conv2d_2a_3x3/BatchNorm/moving_mean' - moving_mean_tf = tf.compat.v1.get_default_graph().get_tensor_by_name( - tensor_name + ':0') - reader = tf.compat.v1.train.NewCheckpointReader(_CHECKPOINT) - moving_mean_expected = reader.get_tensor(tensor_name) - - session_creator = monitored_session.ChiefSessionCreator( - checkpoint_filename_with_path=_CHECKPOINT) - with monitored_session.MonitoredSession( - session_creator=session_creator) as sess: - moving_mean_np = sess.run(moving_mean_tf, - feed_dict={images_placeholder: images_data}) - - self.assertAllEqual(moving_mean_expected, moving_mean_np) - - def test_correct_results_on_test_data(self): - image_path_pattern = 'testdata/fsns_train_%02d.png' - predictions = demo_inference.run(_CHECKPOINT, self._batch_size, - 'fsns', - image_path_pattern) - self.assertEqual([ - u'Boulevard de Lunel░░░░░░░░░░░░░░░░░░░', - 'Rue de Provence░░░░░░░░░░░░░░░░░░░░░░', - 'Rue de Port Maria░░░░░░░░░░░░░░░░░░░░', - 'Avenue Charles Gounod░░░░░░░░░░░░░░░░', - 'Rue de l‘Aurore░░░░░░░░░░░░░░░░░░░░░░', - 'Rue de Beuzeville░░░░░░░░░░░░░░░░░░░░', - 'Rue d‘Orbey░░░░░░░░░░░░░░░░░░░░░░░░░░', - 'Rue Victor Schoulcher░░░░░░░░░░░░░░░░', - 'Rue de la Gare░░░░░░░░░░░░░░░░░░░░░░░', - 'Rue des Tulipes░░░░░░░░░░░░░░░░░░░░░░', - 'Rue André Maginot░░░░░░░░░░░░░░░░░░░░', - 'Route de Pringy░░░░░░░░░░░░░░░░░░░░░░', - 'Rue des Landelles░░░░░░░░░░░░░░░░░░░░', - 'Rue des Ilettes░░░░░░░░░░░░░░░░░░░░░░', - 'Avenue de Maurin░░░░░░░░░░░░░░░░░░░░░', - 'Rue Théresa░░░░░░░░░░░░░░░░░░░░░░░░░░', # GT='Rue Thérésa' - 'Route de la Balme░░░░░░░░░░░░░░░░░░░░', - 'Rue Hélène Roederer░░░░░░░░░░░░░░░░░░', - 'Rue Emile Bernard░░░░░░░░░░░░░░░░░░░░', - 'Place de la Mairie░░░░░░░░░░░░░░░░░░░', - 'Rue des Perrots░░░░░░░░░░░░░░░░░░░░░░', - 'Rue de la Libération░░░░░░░░░░░░░░░░░', - 'Impasse du Capcir░░░░░░░░░░░░░░░░░░░░', - 'Avenue de la Grand Mare░░░░░░░░░░░░░░', - 'Rue Pierre Brossolette░░░░░░░░░░░░░░░', - 'Rue de Provence░░░░░░░░░░░░░░░░░░░░░░', - 'Rue du Docteur Mourre░░░░░░░░░░░░░░░░', - 'Rue d‘Ortheuil░░░░░░░░░░░░░░░░░░░░░░░', - 'Rue des Sarments░░░░░░░░░░░░░░░░░░░░░', - 'Rue du Centre░░░░░░░░░░░░░░░░░░░░░░░░', - 'Impasse Pierre Mourgues░░░░░░░░░░░░░░', - 'Rue Marcel Dassault░░░░░░░░░░░░░░░░░░' - ], predictions) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/eval.py b/research/attention_ocr/python/eval.py deleted file mode 100644 index 108227ba91c..00000000000 --- a/research/attention_ocr/python/eval.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Script to evaluate a trained Attention OCR model. - -A simple usage example: -python eval.py -""" -import tensorflow as tf -from tensorflow.contrib import slim -from tensorflow import app -from tensorflow.compat.v1 import flags - -import data_provider -import common_flags - -FLAGS = flags.FLAGS -common_flags.define() - -# yapf: disable -flags.DEFINE_integer('num_batches', 100, - 'Number of batches to run eval for.') - -flags.DEFINE_string('eval_log_dir', '/tmp/attention_ocr/eval', - 'Directory where the evaluation results are saved to.') - -flags.DEFINE_integer('eval_interval_secs', 60, - 'Frequency in seconds to run evaluations.') - -flags.DEFINE_integer('number_of_steps', None, - 'Number of times to run evaluation.') -# yapf: enable - - -def main(_): - if not tf.io.gfile.exists(FLAGS.eval_log_dir): - tf.io.gfile.makedirs(FLAGS.eval_log_dir) - - dataset = common_flags.create_dataset(split_name=FLAGS.split_name) - model = common_flags.create_model(dataset.num_char_classes, - dataset.max_sequence_length, - dataset.num_of_views, dataset.null_code) - data = data_provider.get_data( - dataset, - FLAGS.batch_size, - augment=False, - central_crop_size=common_flags.get_crop_size()) - endpoints = model.create_base(data.images, labels_one_hot=None) - model.create_loss(data, endpoints) - eval_ops = model.create_summaries( - data, endpoints, dataset.charset, is_training=False) - slim.get_or_create_global_step() - session_config = tf.compat.v1.ConfigProto(device_count={"GPU": 0}) - slim.evaluation.evaluation_loop( - master=FLAGS.master, - checkpoint_dir=FLAGS.train_log_dir, - logdir=FLAGS.eval_log_dir, - eval_op=eval_ops, - num_evals=FLAGS.num_batches, - eval_interval_secs=FLAGS.eval_interval_secs, - max_number_of_evaluations=FLAGS.number_of_steps, - session_config=session_config) - - -if __name__ == '__main__': - app.run() diff --git a/research/attention_ocr/python/inception_preprocessing.py b/research/attention_ocr/python/inception_preprocessing.py deleted file mode 100644 index b61b895021c..00000000000 --- a/research/attention_ocr/python/inception_preprocessing.py +++ /dev/null @@ -1,315 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Provides utilities to preprocess images for the Inception networks.""" - -# TODO(gorban): add as a dependency, when slim or tensorflow/models are pipfied -# Source: -# https://raw.githubusercontent.com/tensorflow/models/a9d0e6e8923a4/slim/preprocessing/inception_preprocessing.py -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from tensorflow.python.ops import control_flow_ops - - -def apply_with_random_selector(x, func, num_cases): - """Computes func(x, sel), with sel sampled from [0...num_cases-1]. - - Args: - x: input Tensor. - func: Python function to apply. - num_cases: Python int32, number of cases to sample sel from. - - Returns: - The result of func(x, sel), where func receives the value of the - selector as a python integer, but sel is sampled dynamically. - """ - sel = tf.random.uniform([], maxval=num_cases, dtype=tf.int32) - # Pass the real x only to one of the func calls. - return control_flow_ops.merge([ - func(control_flow_ops.switch(x, tf.equal(sel, case))[1], case) - for case in range(num_cases) - ])[0] - - -def distort_color(image, color_ordering=0, fast_mode=True, scope=None): - """Distort the color of a Tensor image. - - Each color distortion is non-commutative and thus ordering of the color ops - matters. Ideally we would randomly permute the ordering of the color ops. - Rather than adding that level of complication, we select a distinct ordering - of color ops for each preprocessing thread. - - Args: - image: 3-D Tensor containing single image in [0, 1]. - color_ordering: Python int, a type of distortion (valid values: 0-3). - fast_mode: Avoids slower ops (random_hue and random_contrast) - scope: Optional scope for name_scope. - Returns: - 3-D Tensor color-distorted image on range [0, 1] - Raises: - ValueError: if color_ordering not in [0, 3] - """ - with tf.compat.v1.name_scope(scope, 'distort_color', [image]): - if fast_mode: - if color_ordering == 0: - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - else: - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - else: - if color_ordering == 0: - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_hue(image, max_delta=0.2) - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - elif color_ordering == 1: - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - image = tf.image.random_hue(image, max_delta=0.2) - elif color_ordering == 2: - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - image = tf.image.random_hue(image, max_delta=0.2) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - elif color_ordering == 3: - image = tf.image.random_hue(image, max_delta=0.2) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - else: - raise ValueError('color_ordering must be in [0, 3]') - - # The random_* ops do not necessarily clamp. - return tf.clip_by_value(image, 0.0, 1.0) - - -def distorted_bounding_box_crop(image, - bbox, - min_object_covered=0.1, - aspect_ratio_range=(0.75, 1.33), - area_range=(0.05, 1.0), - max_attempts=100, - scope=None): - """Generates cropped_image using a one of the bboxes randomly distorted. - - See `tf.image.sample_distorted_bounding_box` for more documentation. - - Args: - image: 3-D Tensor of image (it will be converted to floats in [0, 1]). - bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] - where each coordinate is [0, 1) and the coordinates are arranged - as [ymin, xmin, ymax, xmax]. If num_boxes is 0 then it would use the - whole image. - min_object_covered: An optional `float`. Defaults to `0.1`. The cropped - area of the image must contain at least this fraction of any bounding box - supplied. - aspect_ratio_range: An optional list of `floats`. The cropped area of the - image must have an aspect ratio = width / height within this range. - area_range: An optional list of `floats`. The cropped area of the image - must contain a fraction of the supplied image within in this range. - max_attempts: An optional `int`. Number of attempts at generating a cropped - region of the image of the specified constraints. After `max_attempts` - failures, return the entire image. - scope: Optional scope for name_scope. - Returns: - A tuple, a 3-D Tensor cropped_image and the distorted bbox - """ - with tf.compat.v1.name_scope(scope, 'distorted_bounding_box_crop', [image, bbox]): - # Each bounding box has shape [1, num_boxes, box coords] and - # the coordinates are ordered [ymin, xmin, ymax, xmax]. - - # A large fraction of image datasets contain a human-annotated bounding - # box delineating the region of the image containing the object of interest. - # We choose to create a new bounding box for the object which is a randomly - # distorted version of the human-annotated bounding box that obeys an - # allowed range of aspect ratios, sizes and overlap with the human-annotated - # bounding box. If no box is supplied, then we assume the bounding box is - # the entire image. - sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box( - image_size=tf.shape(input=image), - bounding_boxes=bbox, - min_object_covered=min_object_covered, - aspect_ratio_range=aspect_ratio_range, - area_range=area_range, - max_attempts=max_attempts, - use_image_if_no_bounding_boxes=True) - bbox_begin, bbox_size, distort_bbox = sample_distorted_bounding_box - - # Crop the image to the specified bounding box. - cropped_image = tf.slice(image, bbox_begin, bbox_size) - return cropped_image, distort_bbox - - -def preprocess_for_train(image, - height, - width, - bbox, - fast_mode=True, - scope=None): - """Distort one image for training a network. - - Distorting images provides a useful technique for augmenting the data - set during training in order to make the network invariant to aspects - of the image that do not effect the label. - - Additionally it would create image_summaries to display the different - transformations applied to the image. - - Args: - image: 3-D Tensor of image. If dtype is tf.float32 then the range should be - [0, 1], otherwise it would converted to tf.float32 assuming that the range - is [0, MAX], where MAX is largest positive representable number for - int(8/16/32) data type (see `tf.image.convert_image_dtype` for details). - height: integer - width: integer - bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] - where each coordinate is [0, 1) and the coordinates are arranged - as [ymin, xmin, ymax, xmax]. - fast_mode: Optional boolean, if True avoids slower transformations (i.e. - bi-cubic resizing, random_hue or random_contrast). - scope: Optional scope for name_scope. - Returns: - 3-D float Tensor of distorted image used for training with range [-1, 1]. - """ - with tf.compat.v1.name_scope(scope, 'distort_image', [image, height, width, bbox]): - if bbox is None: - bbox = tf.constant( - [0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]) - if image.dtype != tf.float32: - image = tf.image.convert_image_dtype(image, dtype=tf.float32) - # Each bounding box has shape [1, num_boxes, box coords] and - # the coordinates are ordered [ymin, xmin, ymax, xmax]. - image_with_box = tf.image.draw_bounding_boxes( - tf.expand_dims(image, 0), bbox) - tf.compat.v1.summary.image('image_with_bounding_boxes', image_with_box) - - distorted_image, distorted_bbox = distorted_bounding_box_crop(image, bbox) - # Restore the shape since the dynamic slice based upon the bbox_size loses - # the third dimension. - distorted_image.set_shape([None, None, 3]) - image_with_distorted_box = tf.image.draw_bounding_boxes( - tf.expand_dims(image, 0), distorted_bbox) - tf.compat.v1.summary.image('images_with_distorted_bounding_box', - image_with_distorted_box) - - # This resizing operation may distort the images because the aspect - # ratio is not respected. We select a resize method in a round robin - # fashion based on the thread number. - # Note that ResizeMethod contains 4 enumerated resizing methods. - - # We select only 1 case for fast_mode bilinear. - num_resize_cases = 1 if fast_mode else 4 - distorted_image = apply_with_random_selector( - distorted_image, - lambda x, method: tf.image.resize(x, [height, width], method=method), - num_cases=num_resize_cases) - - tf.compat.v1.summary.image('cropped_resized_image', - tf.expand_dims(distorted_image, 0)) - - # Randomly flip the image horizontally. - distorted_image = tf.image.random_flip_left_right(distorted_image) - - # Randomly distort the colors. There are 4 ways to do it. - distorted_image = apply_with_random_selector( - distorted_image, - lambda x, ordering: distort_color(x, ordering, fast_mode), - num_cases=4) - - tf.compat.v1.summary.image('final_distorted_image', - tf.expand_dims(distorted_image, 0)) - distorted_image = tf.subtract(distorted_image, 0.5) - distorted_image = tf.multiply(distorted_image, 2.0) - return distorted_image - - -def preprocess_for_eval(image, - height, - width, - central_fraction=0.875, - scope=None): - """Prepare one image for evaluation. - - If height and width are specified it would output an image with that size by - applying resize_bilinear. - - If central_fraction is specified it would cropt the central fraction of the - input image. - - Args: - image: 3-D Tensor of image. If dtype is tf.float32 then the range should be - [0, 1], otherwise it would converted to tf.float32 assuming that the range - is [0, MAX], where MAX is largest positive representable number for - int(8/16/32) data type (see `tf.image.convert_image_dtype` for details) - height: integer - width: integer - central_fraction: Optional Float, fraction of the image to crop. - scope: Optional scope for name_scope. - Returns: - 3-D float Tensor of prepared image. - """ - with tf.compat.v1.name_scope(scope, 'eval_image', [image, height, width]): - if image.dtype != tf.float32: - image = tf.image.convert_image_dtype(image, dtype=tf.float32) - # Crop the central region of the image with an area containing 87.5% of - # the original image. - if central_fraction: - image = tf.image.central_crop(image, central_fraction=central_fraction) - - if height and width: - # Resize the image to the specified height and width. - image = tf.expand_dims(image, 0) - image = tf.image.resize( - image, [height, width], method=tf.image.ResizeMethod.BILINEAR) - image = tf.squeeze(image, [0]) - image = tf.subtract(image, 0.5) - image = tf.multiply(image, 2.0) - return image - - -def preprocess_image(image, - height, - width, - is_training=False, - bbox=None, - fast_mode=True): - """Pre-process one image for training or evaluation. - - Args: - image: 3-D Tensor [height, width, channels] with the image. - height: integer, image expected height. - width: integer, image expected width. - is_training: Boolean. If true it would transform an image for train, - otherwise it would transform it for evaluation. - bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] - where each coordinate is [0, 1) and the coordinates are arranged as - [ymin, xmin, ymax, xmax]. - fast_mode: Optional boolean, if True avoids slower transformations. - - Returns: - 3-D float Tensor containing an appropriately scaled image - - Raises: - ValueError: if user does not provide bounding box - """ - if is_training: - return preprocess_for_train(image, height, width, bbox, fast_mode) - else: - return preprocess_for_eval(image, height, width) diff --git a/research/attention_ocr/python/metrics.py b/research/attention_ocr/python/metrics.py deleted file mode 100644 index fbd50b0f647..00000000000 --- a/research/attention_ocr/python/metrics.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Quality metrics for the model.""" - -import tensorflow as tf - - -def char_accuracy(predictions, targets, rej_char, streaming=False): - """Computes character level accuracy. - - Both predictions and targets should have the same shape - [batch_size x seq_length]. - - Args: - predictions: predicted characters ids. - targets: ground truth character ids. - rej_char: the character id used to mark an empty element (end of sequence). - streaming: if True, uses the streaming mean from the slim.metric module. - - Returns: - a update_ops for execution and value tensor whose value on evaluation - returns the total character accuracy. - """ - with tf.compat.v1.variable_scope('CharAccuracy'): - predictions.get_shape().assert_is_compatible_with(targets.get_shape()) - - targets = tf.cast(targets, dtype=tf.int32) - const_rej_char = tf.constant(rej_char, shape=targets.get_shape()) - weights = tf.cast(tf.not_equal(targets, const_rej_char), dtype=tf.float32) - correct_chars = tf.cast(tf.equal(predictions, targets), dtype=tf.float32) - accuracy_per_example = tf.compat.v1.div( - tf.reduce_sum(input_tensor=tf.multiply( - correct_chars, weights), axis=1), - tf.reduce_sum(input_tensor=weights, axis=1)) - if streaming: - return tf.metrics.mean(accuracy_per_example) - else: - return tf.reduce_mean(input_tensor=accuracy_per_example) - - -def sequence_accuracy(predictions, targets, rej_char, streaming=False): - """Computes sequence level accuracy. - - Both input tensors should have the same shape: [batch_size x seq_length]. - - Args: - predictions: predicted character classes. - targets: ground truth character classes. - rej_char: the character id used to mark empty element (end of sequence). - streaming: if True, uses the streaming mean from the slim.metric module. - - Returns: - a update_ops for execution and value tensor whose value on evaluation - returns the total sequence accuracy. - """ - - with tf.compat.v1.variable_scope('SequenceAccuracy'): - predictions.get_shape().assert_is_compatible_with(targets.get_shape()) - - targets = tf.cast(targets, dtype=tf.int32) - const_rej_char = tf.constant( - rej_char, shape=targets.get_shape(), dtype=tf.int32) - include_mask = tf.not_equal(targets, const_rej_char) - include_predictions = tf.cast( - tf.compat.v1.where(include_mask, predictions, - tf.zeros_like(predictions) + rej_char), dtype=tf.int32) - correct_chars = tf.cast( - tf.equal(include_predictions, targets), dtype=tf.float32) - correct_chars_counts = tf.cast( - tf.reduce_sum(input_tensor=correct_chars, axis=[1]), dtype=tf.int32) - target_length = targets.get_shape().dims[1].value - target_chars_counts = tf.constant( - target_length, shape=correct_chars_counts.get_shape()) - accuracy_per_example = tf.cast( - tf.equal(correct_chars_counts, target_chars_counts), dtype=tf.float32) - if streaming: - return tf.metrics.mean(accuracy_per_example) - else: - return tf.reduce_mean(input_tensor=accuracy_per_example) diff --git a/research/attention_ocr/python/metrics_test.py b/research/attention_ocr/python/metrics_test.py deleted file mode 100644 index 3e83194523e..00000000000 --- a/research/attention_ocr/python/metrics_test.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for the metrics module.""" -import contextlib -import numpy as np -import tensorflow as tf - -import metrics - - -class AccuracyTest(tf.test.TestCase): - def setUp(self): - tf.test.TestCase.setUp(self) - self.rng = np.random.RandomState([11, 23, 50]) - self.num_char_classes = 3 - self.batch_size = 4 - self.seq_length = 5 - self.rej_char = 42 - - @contextlib.contextmanager - def initialized_session(self): - """Wrapper for test session context manager with required initialization. - - Yields: - A session object that should be used as a context manager. - """ - with self.cached_session() as sess: - sess.run(tf.compat.v1.global_variables_initializer()) - sess.run(tf.compat.v1.local_variables_initializer()) - yield sess - - def _fake_labels(self): - return self.rng.randint( - low=0, - high=self.num_char_classes, - size=(self.batch_size, self.seq_length), - dtype='int32') - - def _incorrect_copy(self, values, bad_indexes): - incorrect = np.copy(values) - incorrect[bad_indexes] = values[bad_indexes] + 1 - return incorrect - - def test_sequence_accuracy_identical_samples(self): - labels_tf = tf.convert_to_tensor(value=self._fake_labels()) - - accuracy_tf = metrics.sequence_accuracy(labels_tf, labels_tf, - self.rej_char) - with self.initialized_session() as sess: - accuracy_np = sess.run(accuracy_tf) - - self.assertAlmostEqual(accuracy_np, 1.0) - - def test_sequence_accuracy_one_char_difference(self): - ground_truth_np = self._fake_labels() - ground_truth_tf = tf.convert_to_tensor(value=ground_truth_np) - prediction_tf = tf.convert_to_tensor( - value=self._incorrect_copy(ground_truth_np, bad_indexes=((0, 0)))) - - accuracy_tf = metrics.sequence_accuracy(prediction_tf, ground_truth_tf, - self.rej_char) - with self.initialized_session() as sess: - accuracy_np = sess.run(accuracy_tf) - - # 1 of 4 sequences is incorrect. - self.assertAlmostEqual(accuracy_np, 1.0 - 1.0 / self.batch_size) - - def test_char_accuracy_one_char_difference_with_padding(self): - ground_truth_np = self._fake_labels() - ground_truth_tf = tf.convert_to_tensor(value=ground_truth_np) - prediction_tf = tf.convert_to_tensor( - value=self._incorrect_copy(ground_truth_np, bad_indexes=((0, 0)))) - - accuracy_tf = metrics.char_accuracy(prediction_tf, ground_truth_tf, - self.rej_char) - with self.initialized_session() as sess: - accuracy_np = sess.run(accuracy_tf) - - chars_count = self.seq_length * self.batch_size - self.assertAlmostEqual(accuracy_np, 1.0 - 1.0 / chars_count) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/model.py b/research/attention_ocr/python/model.py deleted file mode 100644 index b489f964e9d..00000000000 --- a/research/attention_ocr/python/model.py +++ /dev/null @@ -1,757 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Functions to build the Attention OCR model. - -Usage example: - ocr_model = model.Model(num_char_classes, seq_length, num_of_views) - - data = ... # create namedtuple InputEndpoints - endpoints = model.create_base(data.images, data.labels_one_hot) - # endpoints.predicted_chars is a tensor with predicted character codes. - total_loss = model.create_loss(data, endpoints) -""" -import sys -import collections -import logging -import numpy as np -import tensorflow as tf -from tensorflow.contrib import slim -from tensorflow.contrib.slim.nets import inception - -import metrics -import sequence_layers -import utils - -OutputEndpoints = collections.namedtuple('OutputEndpoints', [ - 'chars_logit', 'chars_log_prob', 'predicted_chars', 'predicted_scores', - 'predicted_text', 'predicted_length', 'predicted_conf', - 'normalized_seq_conf' -]) - -# TODO(gorban): replace with tf.HParams when it is released. -ModelParams = collections.namedtuple( - 'ModelParams', ['num_char_classes', 'seq_length', 'num_views', 'null_code']) - -ConvTowerParams = collections.namedtuple('ConvTowerParams', ['final_endpoint']) - -SequenceLogitsParams = collections.namedtuple('SequenceLogitsParams', [ - 'use_attention', 'use_autoregression', 'num_lstm_units', 'weight_decay', - 'lstm_state_clip_value' -]) - -SequenceLossParams = collections.namedtuple( - 'SequenceLossParams', - ['label_smoothing', 'ignore_nulls', 'average_across_timesteps']) - -EncodeCoordinatesParams = collections.namedtuple('EncodeCoordinatesParams', - ['enabled']) - - -def _dict_to_array(id_to_char, default_character): - num_char_classes = max(id_to_char.keys()) + 1 - array = [default_character] * num_char_classes - for k, v in id_to_char.items(): - array[k] = v - return array - - -class CharsetMapper(object): - """A simple class to map tensor ids into strings. - - It works only when the character set is 1:1 mapping between individual - characters and individual ids. - - Make sure you call tf.tables_initializer().run() as part of the init op. - """ - - def __init__(self, charset, default_character='?'): - """Creates a lookup table. - - Args: - charset: a dictionary with id-to-character mapping. - """ - mapping_strings = tf.constant(_dict_to_array(charset, default_character)) - self.table = tf.contrib.lookup.index_to_string_table_from_tensor( - mapping=mapping_strings, default_value=default_character) - - def get_text(self, ids): - """Returns a string corresponding to a sequence of character ids. - - Args: - ids: a tensor with shape [batch_size, max_sequence_length] - """ - return tf.strings.reduce_join( - inputs=self.table.lookup(tf.cast(ids, dtype=tf.int64)), axis=1) - - -def get_softmax_loss_fn(label_smoothing): - """Returns sparse or dense loss function depending on the label_smoothing. - - Args: - label_smoothing: weight for label smoothing - - Returns: - a function which takes labels and predictions as arguments and returns - a softmax loss for the selected type of labels (sparse or dense). - """ - if label_smoothing > 0: - - def loss_fn(labels, logits): - return (tf.nn.softmax_cross_entropy_with_logits( - logits=logits, labels=tf.stop_gradient(labels))) - else: - - def loss_fn(labels, logits): - return tf.nn.sparse_softmax_cross_entropy_with_logits( - logits=logits, labels=labels) - - return loss_fn - - -def get_tensor_dimensions(tensor): - """Returns the shape components of a 4D tensor with variable batch size. - - Args: - tensor : A 4D tensor, whose last 3 dimensions are known at graph - construction time. - - Returns: - batch_size : The first dimension as a tensor object. - height : The second dimension as a scalar value. - width : The third dimension as a scalar value. - num_features : The forth dimension as a scalar value. - - Raises: - ValueError: if input tensor does not have 4 dimensions. - """ - if len(tensor.get_shape().dims) != 4: - raise ValueError( - 'Incompatible shape: len(tensor.get_shape().dims) != 4 (%d != 4)' % - len(tensor.get_shape().dims)) - batch_size = tf.shape(input=tensor)[0] - height = tensor.get_shape().dims[1].value - width = tensor.get_shape().dims[2].value - num_features = tensor.get_shape().dims[3].value - return batch_size, height, width, num_features - - -def lookup_indexed_value(indices, row_vecs): - """Lookup values in each row of 'row_vecs' indexed by 'indices'. - - For each sample in the batch, look up the element for the corresponding - index. - - Args: - indices : A tensor of shape (batch, ) - row_vecs : A tensor of shape [batch, depth] - - Returns: - A tensor of shape (batch, ) formed by row_vecs[i, indices[i]]. - """ - gather_indices = tf.stack((tf.range( - tf.shape(input=row_vecs)[0], dtype=tf.int32), tf.cast(indices, tf.int32)), - axis=1) - return tf.gather_nd(row_vecs, gather_indices) - - -@utils.ConvertAllInputsToTensors -def max_char_logprob_cumsum(char_log_prob): - """Computes the cumulative sum of character logprob for all sequence lengths. - - Args: - char_log_prob: A tensor of shape [batch x seq_length x num_char_classes] - with log probabilities of a character. - - Returns: - A tensor of shape [batch x (seq_length+1)] where each element x[_, j] is - the sum of the max char logprob for all positions upto j. - Note this duplicates the final column and produces (seq_length+1) columns - so the same function can be used regardless whether use_length_predictions - is true or false. - """ - max_char_log_prob = tf.reduce_max(input_tensor=char_log_prob, axis=2) - # For an input array [a, b, c]) tf.cumsum returns [a, a + b, a + b + c] if - # exclusive set to False (default). - return tf.cumsum(max_char_log_prob, axis=1, exclusive=False) - - -def find_length_by_null(predicted_chars, null_code): - """Determine sequence length by finding null_code among predicted char IDs. - - Given the char class ID for each position, compute the sequence length. - Note that this function computes this based on the number of null_code, - instead of the position of the first null_code. - - Args: - predicted_chars: A tensor of [batch x seq_length] where each element stores - the char class ID with max probability; - null_code: an int32, character id for the NULL. - - Returns: - A [batch, ] tensor which stores the sequence length for each sample. - """ - return tf.reduce_sum( - input_tensor=tf.cast(tf.not_equal(null_code, predicted_chars), tf.int32), axis=1) - - -def axis_pad(tensor, axis, before=0, after=0, constant_values=0.0): - """Pad a tensor with the specified values along a single axis. - - Args: - tensor: a Tensor; - axis: the dimension to add pad along to; - before: number of values to add before the contents of tensor in the - selected dimension; - after: number of values to add after the contents of tensor in the selected - dimension; - constant_values: the scalar pad value to use. Must be same type as tensor. - - Returns: - A Tensor. Has the same type as the input tensor, but with a changed shape - along the specified dimension. - """ - if before == 0 and after == 0: - return tensor - ndims = tensor.shape.ndims - padding_size = np.zeros((ndims, 2), dtype='int32') - padding_size[axis] = before, after - return tf.pad( - tensor=tensor, - paddings=tf.constant(padding_size), - constant_values=constant_values) - - -def null_based_length_prediction(chars_log_prob, null_code): - """Computes length and confidence of prediction based on positions of NULLs. - - Args: - chars_log_prob: A tensor of shape [batch x seq_length x num_char_classes] - with log probabilities of a character; - null_code: an int32, character id for the NULL. - - Returns: - A tuple (text_log_prob, predicted_length), where - text_log_prob - is a tensor of the same shape as length_log_prob. - Element #0 of the output corresponds to probability of the empty string, - element #seq_length - is the probability of length=seq_length. - predicted_length is a tensor with shape [batch]. - """ - predicted_chars = tf.cast( - tf.argmax(input=chars_log_prob, axis=2), dtype=tf.int32) - # We do right pad to support sequences with seq_length elements. - text_log_prob = max_char_logprob_cumsum( - axis_pad(chars_log_prob, axis=1, after=1)) - predicted_length = find_length_by_null(predicted_chars, null_code) - return text_log_prob, predicted_length - - -class Model(object): - """Class to create the Attention OCR Model.""" - - def __init__(self, - num_char_classes, - seq_length, - num_views, - null_code, - mparams=None, - charset=None): - """Initialized model parameters. - - Args: - num_char_classes: size of character set. - seq_length: number of characters in a sequence. - num_views: Number of views (conv towers) to use. - null_code: A character code corresponding to a character which indicates - end of a sequence. - mparams: a dictionary with hyper parameters for methods, keys - function - names, values - corresponding namedtuples. - charset: an optional dictionary with a mapping between character ids and - utf8 strings. If specified the OutputEndpoints.predicted_text will utf8 - encoded strings corresponding to the character ids returned by - OutputEndpoints.predicted_chars (by default the predicted_text contains - an empty vector). - NOTE: Make sure you call tf.tables_initializer().run() if the charset - specified. - """ - super(Model, self).__init__() - self._params = ModelParams( - num_char_classes=num_char_classes, - seq_length=seq_length, - num_views=num_views, - null_code=null_code) - self._mparams = self.default_mparams() - if mparams: - self._mparams.update(mparams) - self._charset = charset - - def default_mparams(self): - return { - 'conv_tower_fn': - ConvTowerParams(final_endpoint='Mixed_5d'), - 'sequence_logit_fn': - SequenceLogitsParams( - use_attention=True, - use_autoregression=True, - num_lstm_units=256, - weight_decay=0.00004, - lstm_state_clip_value=10.0), - 'sequence_loss_fn': - SequenceLossParams( - label_smoothing=0.1, - ignore_nulls=True, - average_across_timesteps=False), - 'encode_coordinates_fn': - EncodeCoordinatesParams(enabled=False) - } - - def set_mparam(self, function, **kwargs): - self._mparams[function] = self._mparams[function]._replace(**kwargs) - - def conv_tower_fn(self, images, is_training=True, reuse=None): - """Computes convolutional features using the InceptionV3 model. - - Args: - images: A tensor of shape [batch_size, height, width, channels]. - is_training: whether is training or not. - reuse: whether or not the network and its variables should be reused. To - be able to reuse 'scope' must be given. - - Returns: - A tensor of shape [batch_size, OH, OW, N], where OWxOH is resolution of - output feature map and N is number of output features (depends on the - network architecture). - """ - mparams = self._mparams['conv_tower_fn'] - logging.debug('Using final_endpoint=%s', mparams.final_endpoint) - with tf.compat.v1.variable_scope('conv_tower_fn/INCE'): - if reuse: - tf.compat.v1.get_variable_scope().reuse_variables() - with slim.arg_scope(inception.inception_v3_arg_scope()): - with slim.arg_scope([slim.batch_norm, slim.dropout], - is_training=is_training): - net, _ = inception.inception_v3_base( - images, final_endpoint=mparams.final_endpoint) - return net - - def _create_lstm_inputs(self, net): - """Splits an input tensor into a list of tensors (features). - - Args: - net: A feature map of shape [batch_size, num_features, feature_size]. - - Raises: - AssertionError: if num_features is less than seq_length. - - Returns: - A list with seq_length tensors of shape [batch_size, feature_size] - """ - num_features = net.get_shape().dims[1].value - if num_features < self._params.seq_length: - raise AssertionError( - 'Incorrect dimension #1 of input tensor' - ' %d should be bigger than %d (shape=%s)' % - (num_features, self._params.seq_length, net.get_shape())) - elif num_features > self._params.seq_length: - logging.warning('Ignoring some features: use %d of %d (shape=%s)', - self._params.seq_length, num_features, net.get_shape()) - net = tf.slice(net, [0, 0, 0], [-1, self._params.seq_length, -1]) - - return tf.unstack(net, axis=1) - - def sequence_logit_fn(self, net, labels_one_hot): - mparams = self._mparams['sequence_logit_fn'] - # TODO(gorban): remove /alias suffixes from the scopes. - with tf.compat.v1.variable_scope('sequence_logit_fn/SQLR'): - layer_class = sequence_layers.get_layer_class(mparams.use_attention, - mparams.use_autoregression) - layer = layer_class(net, labels_one_hot, self._params, mparams) - return layer.create_logits() - - def max_pool_views(self, nets_list): - """Max pool across all nets in spatial dimensions. - - Args: - nets_list: A list of 4D tensors with identical size. - - Returns: - A tensor with the same size as any input tensors. - """ - batch_size, height, width, num_features = [ - d.value for d in nets_list[0].get_shape().dims - ] - xy_flat_shape = (batch_size, 1, height * width, num_features) - nets_for_merge = [] - with tf.compat.v1.variable_scope('max_pool_views', values=nets_list): - for net in nets_list: - nets_for_merge.append(tf.reshape(net, xy_flat_shape)) - merged_net = tf.concat(nets_for_merge, 1) - net = slim.max_pool2d( - merged_net, kernel_size=[len(nets_list), 1], stride=1) - net = tf.reshape(net, (batch_size, height, width, num_features)) - return net - - def pool_views_fn(self, nets): - """Combines output of multiple convolutional towers into a single tensor. - - It stacks towers one on top another (in height dim) in a 4x1 grid. - The order is arbitrary design choice and shouldn't matter much. - - Args: - nets: list of tensors of shape=[batch_size, height, width, num_features]. - - Returns: - A tensor of shape [batch_size, seq_length, features_size]. - """ - with tf.compat.v1.variable_scope('pool_views_fn/STCK'): - net = tf.concat(nets, 1) - batch_size = tf.shape(input=net)[0] - image_size = net.get_shape().dims[1].value * \ - net.get_shape().dims[2].value - feature_size = net.get_shape().dims[3].value - return tf.reshape(net, tf.stack([batch_size, image_size, feature_size])) - - def char_predictions(self, chars_logit): - """Returns confidence scores (softmax values) for predicted characters. - - Args: - chars_logit: chars logits, a tensor with shape [batch_size x seq_length x - num_char_classes] - - Returns: - A tuple (ids, log_prob, scores), where: - ids - predicted characters, a int32 tensor with shape - [batch_size x seq_length]; - log_prob - a log probability of all characters, a float tensor with - shape [batch_size, seq_length, num_char_classes]; - scores - corresponding confidence scores for characters, a float - tensor - with shape [batch_size x seq_length]. - """ - log_prob = utils.logits_to_log_prob(chars_logit) - ids = tf.cast(tf.argmax(input=log_prob, axis=2), - name='predicted_chars', dtype=tf.int32) - mask = tf.cast( - slim.one_hot_encoding(ids, self._params.num_char_classes), tf.bool) - all_scores = tf.nn.softmax(chars_logit) - selected_scores = tf.boolean_mask( - tensor=all_scores, mask=mask, name='char_scores') - scores = tf.reshape( - selected_scores, - shape=(-1, self._params.seq_length), - name='predicted_scores') - return ids, log_prob, scores - - def encode_coordinates_fn(self, net): - """Adds one-hot encoding of coordinates to different views in the networks. - - For each "pixel" of a feature map it adds a onehot encoded x and y - coordinates. - - Args: - net: a tensor of shape=[batch_size, height, width, num_features] - - Returns: - a tensor with the same height and width, but altered feature_size. - """ - mparams = self._mparams['encode_coordinates_fn'] - if mparams.enabled: - batch_size, h, w, _ = get_tensor_dimensions(net) - x, y = tf.meshgrid(tf.range(w), tf.range(h)) - w_loc = slim.one_hot_encoding(x, num_classes=w) - h_loc = slim.one_hot_encoding(y, num_classes=h) - loc = tf.concat([h_loc, w_loc], 2) - loc = tf.tile(tf.expand_dims(loc, 0), tf.stack([batch_size, 1, 1, 1])) - return tf.concat([net, loc], 3) - else: - return net - - def create_base(self, - images, - labels_one_hot, - scope='AttentionOcr_v1', - reuse=None): - """Creates a base part of the Model (no gradients, losses or summaries). - - Args: - images: A tensor of shape [batch_size, height, width, channels] with pixel - values in the range [0.0, 1.0]. - labels_one_hot: Optional (can be None) one-hot encoding for ground truth - labels. If provided the function will create a model for training. - scope: Optional variable_scope. - reuse: whether or not the network and its variables should be reused. To - be able to reuse 'scope' must be given. - - Returns: - A named tuple OutputEndpoints. - """ - logging.debug('images: %s', images) - is_training = labels_one_hot is not None - - # Normalize image pixel values to have a symmetrical range around zero. - images = tf.subtract(images, 0.5) - images = tf.multiply(images, 2.5) - - with tf.compat.v1.variable_scope(scope, reuse=reuse): - views = tf.split( - value=images, num_or_size_splits=self._params.num_views, axis=2) - logging.debug('Views=%d single view: %s', len(views), views[0]) - - nets = [ - self.conv_tower_fn(v, is_training, reuse=(i != 0)) - for i, v in enumerate(views) - ] - logging.debug('Conv tower: %s', nets[0]) - - nets = [self.encode_coordinates_fn(net) for net in nets] - logging.debug('Conv tower w/ encoded coordinates: %s', nets[0]) - - net = self.pool_views_fn(nets) - logging.debug('Pooled views: %s', net) - - chars_logit = self.sequence_logit_fn(net, labels_one_hot) - logging.debug('chars_logit: %s', chars_logit) - - predicted_chars, chars_log_prob, predicted_scores = ( - self.char_predictions(chars_logit)) - if self._charset: - character_mapper = CharsetMapper(self._charset) - predicted_text = character_mapper.get_text(predicted_chars) - else: - predicted_text = tf.constant([]) - - text_log_prob, predicted_length = null_based_length_prediction( - chars_log_prob, self._params.null_code) - predicted_conf = lookup_indexed_value(predicted_length, text_log_prob) - # Convert predicted confidence from sum of logs to geometric mean - normalized_seq_conf = tf.exp( - tf.divide(predicted_conf, - tf.cast(predicted_length + 1, predicted_conf.dtype)), - name='normalized_seq_conf') - predicted_conf = tf.identity(predicted_conf, name='predicted_conf') - predicted_text = tf.identity(predicted_text, name='predicted_text') - predicted_length = tf.identity(predicted_length, name='predicted_length') - - return OutputEndpoints( - chars_logit=chars_logit, - chars_log_prob=chars_log_prob, - predicted_chars=predicted_chars, - predicted_scores=predicted_scores, - predicted_length=predicted_length, - predicted_text=predicted_text, - predicted_conf=predicted_conf, - normalized_seq_conf=normalized_seq_conf) - - def create_loss(self, data, endpoints): - """Creates all losses required to train the model. - - Args: - data: InputEndpoints namedtuple. - endpoints: Model namedtuple. - - Returns: - Total loss. - """ - # NOTE: the return value of ModelLoss is not used directly for the - # gradient computation because under the hood it calls slim.losses.AddLoss, - # which registers the loss in an internal collection and later returns it - # as part of GetTotalLoss. We need to use total loss because model may have - # multiple losses including regularization losses. - self.sequence_loss_fn(endpoints.chars_logit, data.labels) - total_loss = slim.losses.get_total_loss() - tf.compat.v1.summary.scalar('TotalLoss', total_loss) - return total_loss - - def label_smoothing_regularization(self, chars_labels, weight=0.1): - """Applies a label smoothing regularization. - - Uses the same method as in https://arxiv.org/abs/1512.00567. - - Args: - chars_labels: ground truth ids of charactes, shape=[batch_size, - seq_length]; - weight: label-smoothing regularization weight. - - Returns: - A sensor with the same shape as the input. - """ - one_hot_labels = tf.one_hot( - chars_labels, depth=self._params.num_char_classes, axis=-1) - pos_weight = 1.0 - weight - neg_weight = weight / self._params.num_char_classes - return one_hot_labels * pos_weight + neg_weight - - def sequence_loss_fn(self, chars_logits, chars_labels): - """Loss function for char sequence. - - Depending on values of hyper parameters it applies label smoothing and can - also ignore all null chars after the first one. - - Args: - chars_logits: logits for predicted characters, shape=[batch_size, - seq_length, num_char_classes]; - chars_labels: ground truth ids of characters, shape=[batch_size, - seq_length]; - mparams: method hyper parameters. - - Returns: - A Tensor with shape [batch_size] - the log-perplexity for each sequence. - """ - mparams = self._mparams['sequence_loss_fn'] - with tf.compat.v1.variable_scope('sequence_loss_fn/SLF'): - if mparams.label_smoothing > 0: - smoothed_one_hot_labels = self.label_smoothing_regularization( - chars_labels, mparams.label_smoothing) - labels_list = tf.unstack(smoothed_one_hot_labels, axis=1) - else: - # NOTE: in case of sparse softmax we are not using one-hot - # encoding. - labels_list = tf.unstack(chars_labels, axis=1) - - batch_size, seq_length, _ = chars_logits.shape.as_list() - if mparams.ignore_nulls: - weights = tf.ones((batch_size, seq_length), dtype=tf.float32) - else: - # Suppose that reject character is the last in the charset. - reject_char = tf.constant( - self._params.num_char_classes - 1, - shape=(batch_size, seq_length), - dtype=tf.int64) - known_char = tf.not_equal(chars_labels, reject_char) - weights = tf.cast(known_char, dtype=tf.float32) - - logits_list = tf.unstack(chars_logits, axis=1) - weights_list = tf.unstack(weights, axis=1) - loss = tf.contrib.legacy_seq2seq.sequence_loss( - logits_list, - labels_list, - weights_list, - softmax_loss_function=get_softmax_loss_fn(mparams.label_smoothing), - average_across_timesteps=mparams.average_across_timesteps) - tf.compat.v1.losses.add_loss(loss) - return loss - - def create_summaries(self, data, endpoints, charset, is_training): - """Creates all summaries for the model. - - Args: - data: InputEndpoints namedtuple. - endpoints: OutputEndpoints namedtuple. - charset: A dictionary with mapping between character codes and unicode - characters. Use the one provided by a dataset.charset. - is_training: If True will create summary prefixes for training job, - otherwise - for evaluation. - - Returns: - A list of evaluation ops - """ - - def sname(label): - prefix = 'train' if is_training else 'eval' - return '%s/%s' % (prefix, label) - - max_outputs = 4 - # TODO(gorban): uncomment, when tf.summary.text released. - # charset_mapper = CharsetMapper(charset) - # pr_text = charset_mapper.get_text( - # endpoints.predicted_chars[:max_outputs,:]) - # tf.summary.text(sname('text/pr'), pr_text) - # gt_text = charset_mapper.get_text(data.labels[:max_outputs,:]) - # tf.summary.text(sname('text/gt'), gt_text) - tf.compat.v1.summary.image( - sname('image'), data.images, max_outputs=max_outputs) - - if is_training: - tf.compat.v1.summary.image( - sname('image/orig'), data.images_orig, max_outputs=max_outputs) - for var in tf.compat.v1.trainable_variables(): - tf.compat.v1.summary.histogram(var.op.name, var) - return None - - else: - names_to_values = {} - names_to_updates = {} - - def use_metric(name, value_update_tuple): - names_to_values[name] = value_update_tuple[0] - names_to_updates[name] = value_update_tuple[1] - - use_metric( - 'CharacterAccuracy', - metrics.char_accuracy( - endpoints.predicted_chars, - data.labels, - streaming=True, - rej_char=self._params.null_code)) - # Sequence accuracy computed by cutting sequence at the first null char - use_metric( - 'SequenceAccuracy', - metrics.sequence_accuracy( - endpoints.predicted_chars, - data.labels, - streaming=True, - rej_char=self._params.null_code)) - - for name, value in names_to_values.items(): - summary_name = 'eval/' + name - tf.compat.v1.summary.scalar( - summary_name, tf.compat.v1.Print(value, [value], summary_name)) - return list(names_to_updates.values()) - - def create_init_fn_to_restore(self, - master_checkpoint, - inception_checkpoint=None): - """Creates an init operations to restore weights from various checkpoints. - - Args: - master_checkpoint: path to a checkpoint which contains all weights for the - whole model. - inception_checkpoint: path to a checkpoint which contains weights for the - inception part only. - - Returns: - a function to run initialization ops. - """ - all_assign_ops = [] - all_feed_dict = {} - - def assign_from_checkpoint(variables, checkpoint): - logging.info('Request to re-store %d weights from %s', len(variables), - checkpoint) - if not variables: - logging.error('Can\'t find any variables to restore.') - sys.exit(1) - assign_op, feed_dict = slim.assign_from_checkpoint(checkpoint, variables) - all_assign_ops.append(assign_op) - all_feed_dict.update(feed_dict) - - logging.info('variables_to_restore:\n%s', - utils.variables_to_restore().keys()) - logging.info('moving_average_variables:\n%s', - [v.op.name for v in tf.compat.v1.moving_average_variables()]) - logging.info('trainable_variables:\n%s', - [v.op.name for v in tf.compat.v1.trainable_variables()]) - if master_checkpoint: - assign_from_checkpoint(utils.variables_to_restore(), master_checkpoint) - - if inception_checkpoint: - variables = utils.variables_to_restore( - 'AttentionOcr_v1/conv_tower_fn/INCE', strip_scope=True) - assign_from_checkpoint(variables, inception_checkpoint) - - def init_assign_fn(sess): - logging.info('Restoring checkpoint(s)') - sess.run(all_assign_ops, all_feed_dict) - - return init_assign_fn diff --git a/research/attention_ocr/python/model_export.py b/research/attention_ocr/python/model_export.py deleted file mode 100644 index c4606003ae6..00000000000 --- a/research/attention_ocr/python/model_export.py +++ /dev/null @@ -1,198 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Converts existing checkpoint into a SavedModel. - -Usage example: -python model_export.py \ - --logtostderr --checkpoint=model.ckpt-399731 \ - --export_dir=/tmp/attention_ocr_export -""" -import os - -import tensorflow as tf -from tensorflow import app -from tensorflow.contrib import slim -from tensorflow.compat.v1 import flags - -import common_flags -import model_export_lib - -FLAGS = flags.FLAGS -common_flags.define() - -flags.DEFINE_string('export_dir', None, 'Directory to export model files to.') -flags.DEFINE_integer( - 'image_width', None, - 'Image width used during training (or crop width if used)' - ' If not set, the dataset default is used instead.') -flags.DEFINE_integer( - 'image_height', None, - 'Image height used during training(or crop height if used)' - ' If not set, the dataset default is used instead.') -flags.DEFINE_string('work_dir', '/tmp', - 'A directory to store temporary files.') -flags.DEFINE_integer('version_number', 1, 'Version number of the model') -flags.DEFINE_bool( - 'export_for_serving', True, - 'Whether the exported model accepts serialized tf.Example ' - 'protos as input') - - -def get_checkpoint_path(): - """Returns a path to a checkpoint based on specified commandline flags. - - In order to specify a full path to a checkpoint use --checkpoint flag. - Alternatively, if --train_log_dir was specified it will return a path to the - most recent checkpoint. - - Raises: - ValueError: in case it can't find a checkpoint. - - Returns: - A string. - """ - if FLAGS.checkpoint: - return FLAGS.checkpoint - else: - model_save_path = tf.train.latest_checkpoint(FLAGS.train_log_dir) - if not model_save_path: - raise ValueError('Can\'t find a checkpoint in: %s' % FLAGS.train_log_dir) - return model_save_path - - -def export_model(export_dir, - export_for_serving, - batch_size=None, - crop_image_width=None, - crop_image_height=None): - """Exports a model to the named directory. - - Note that --datatset_name and --checkpoint are required and parsed by the - underlying module common_flags. - - Args: - export_dir: The output dir where model is exported to. - export_for_serving: If True, expects a serialized image as input and attach - image normalization as part of exported graph. - batch_size: For non-serving export, the input batch_size needs to be - specified. - crop_image_width: Width of the input image. Uses the dataset default if - None. - crop_image_height: Height of the input image. Uses the dataset default if - None. - - Returns: - Returns the model signature_def. - """ - # Dataset object used only to get all parameters for the model. - dataset = common_flags.create_dataset(split_name='test') - model = common_flags.create_model( - dataset.num_char_classes, - dataset.max_sequence_length, - dataset.num_of_views, - dataset.null_code, - charset=dataset.charset) - dataset_image_height, dataset_image_width, image_depth = dataset.image_shape - - # Add check for charmap file - if not os.path.exists(dataset.charset_file): - raise ValueError('No charset defined at {}: export will fail'.format( - dataset.charset)) - - # Default to dataset dimensions, otherwise use provided dimensions. - image_width = crop_image_width or dataset_image_width - image_height = crop_image_height or dataset_image_height - - if export_for_serving: - images_orig = tf.compat.v1.placeholder( - tf.string, shape=[batch_size], name='tf_example') - images_orig_float = model_export_lib.generate_tfexample_image( - images_orig, - image_height, - image_width, - image_depth, - name='float_images') - else: - images_shape = (batch_size, image_height, image_width, image_depth) - images_orig = tf.compat.v1.placeholder( - tf.uint8, shape=images_shape, name='original_image') - images_orig_float = tf.image.convert_image_dtype( - images_orig, dtype=tf.float32, name='float_images') - - endpoints = model.create_base(images_orig_float, labels_one_hot=None) - - sess = tf.compat.v1.Session() - saver = tf.compat.v1.train.Saver( - slim.get_variables_to_restore(), sharded=True) - saver.restore(sess, get_checkpoint_path()) - tf.compat.v1.logging.info('Model restored successfully.') - - # Create model signature. - if export_for_serving: - input_tensors = { - tf.saved_model.CLASSIFY_INPUTS: images_orig - } - else: - input_tensors = {'images': images_orig} - signature_inputs = model_export_lib.build_tensor_info(input_tensors) - # NOTE: Tensors 'image_float' and 'chars_logit' are used by the inference - # or to compute saliency maps. - output_tensors = { - 'images_float': images_orig_float, - 'predictions': endpoints.predicted_chars, - 'scores': endpoints.predicted_scores, - 'chars_logit': endpoints.chars_logit, - 'predicted_length': endpoints.predicted_length, - 'predicted_text': endpoints.predicted_text, - 'predicted_conf': endpoints.predicted_conf, - 'normalized_seq_conf': endpoints.normalized_seq_conf - } - for i, t in enumerate( - model_export_lib.attention_ocr_attention_masks( - dataset.max_sequence_length)): - output_tensors['attention_mask_%d' % i] = t - signature_outputs = model_export_lib.build_tensor_info(output_tensors) - signature_def = tf.compat.v1.saved_model.signature_def_utils.build_signature_def( - signature_inputs, signature_outputs, - tf.saved_model.CLASSIFY_METHOD_NAME) - # Save model. - builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_dir) - builder.add_meta_graph_and_variables( - sess, [tf.saved_model.SERVING], - signature_def_map={ - tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY: - signature_def - }, - main_op=tf.compat.v1.tables_initializer(), - strip_default_attrs=True) - builder.save() - tf.compat.v1.logging.info('Model has been exported to %s' % export_dir) - - return signature_def - - -def main(unused_argv): - if os.path.exists(FLAGS.export_dir): - raise ValueError('export_dir already exists: exporting will fail') - - export_model(FLAGS.export_dir, FLAGS.export_for_serving, FLAGS.batch_size, - FLAGS.image_width, FLAGS.image_height) - - -if __name__ == '__main__': - flags.mark_flag_as_required('dataset_name') - flags.mark_flag_as_required('export_dir') - app.run(main) diff --git a/research/attention_ocr/python/model_export_lib.py b/research/attention_ocr/python/model_export_lib.py deleted file mode 100644 index d5d141be2a8..00000000000 --- a/research/attention_ocr/python/model_export_lib.py +++ /dev/null @@ -1,108 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utility functions for exporting Attention OCR model.""" - -import tensorflow as tf - - -# Function borrowed from research/object_detection/core/preprocessor.py -def normalize_image(image, original_minval, original_maxval, target_minval, - target_maxval): - """Normalizes pixel values in the image. - - Moves the pixel values from the current [original_minval, original_maxval] - range to a the [target_minval, target_maxval] range. - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, - channels]. - original_minval: current image minimum value. - original_maxval: current image maximum value. - target_minval: target image minimum value. - target_maxval: target image maximum value. - - Returns: - image: image which is the same shape as input image. - """ - with tf.compat.v1.name_scope('NormalizeImage', values=[image]): - original_minval = float(original_minval) - original_maxval = float(original_maxval) - target_minval = float(target_minval) - target_maxval = float(target_maxval) - image = tf.cast(image, dtype=tf.float32) - image = tf.subtract(image, original_minval) - image = tf.multiply(image, (target_maxval - target_minval) / - (original_maxval - original_minval)) - image = tf.add(image, target_minval) - return image - - -def generate_tfexample_image(input_example_strings, - image_height, - image_width, - image_channels, - name=None): - """Parses a 1D tensor of serialized tf.Example protos and returns image batch. - - Args: - input_example_strings: A 1-Dimensional tensor of size [batch_size] and type - tf.string containing a serialized Example proto per image. - image_height: First image dimension. - image_width: Second image dimension. - image_channels: Third image dimension. - name: optional tensor name. - - Returns: - A tensor with shape [batch_size, height, width, channels] of type float32 - with values in the range [0..1] - """ - batch_size = tf.shape(input=input_example_strings)[0] - images_shape = tf.stack( - [batch_size, image_height, image_width, image_channels]) - tf_example_image_key = 'image/encoded' - feature_configs = { - tf_example_image_key: - tf.io.FixedLenFeature( - image_height * image_width * image_channels, dtype=tf.float32) - } - feature_tensors = tf.io.parse_example( - serialized=input_example_strings, features=feature_configs) - float_images = tf.reshape( - normalize_image( - feature_tensors[tf_example_image_key], - original_minval=0.0, - original_maxval=255.0, - target_minval=0.0, - target_maxval=1.0), - images_shape, - name=name) - return float_images - - -def attention_ocr_attention_masks(num_characters): - # TODO(gorban): use tensors directly after replacing LSTM unroll methods. - prefix = ('AttentionOcr_v1/' - 'sequence_logit_fn/SQLR/LSTM/attention_decoder/Attention_0') - names = ['%s/Softmax:0' % (prefix)] - for i in range(1, num_characters): - names += ['%s_%d/Softmax:0' % (prefix, i)] - return [tf.compat.v1.get_default_graph().get_tensor_by_name(n) for n in names] - - -def build_tensor_info(tensor_dict): - return { - k: tf.compat.v1.saved_model.utils.build_tensor_info(t) - for k, t in tensor_dict.items() - } diff --git a/research/attention_ocr/python/model_export_test.py b/research/attention_ocr/python/model_export_test.py deleted file mode 100644 index 4dc6688ca46..00000000000 --- a/research/attention_ocr/python/model_export_test.py +++ /dev/null @@ -1,161 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for model_export.""" -import os - -import numpy as np -from absl.testing import flagsaver -import tensorflow as tf -from tensorflow.compat.v1 import flags - -import common_flags -import model_export - -_CHECKPOINT = 'model.ckpt-399731' -_CHECKPOINT_URL = ( - 'http://download.tensorflow.org/models/attention_ocr_2017_08_09.tar.gz') - - -def _clean_up(): - tf.io.gfile.rmtree(tf.compat.v1.test.get_temp_dir()) - - -def _create_tf_example_string(image): - """Create a serialized tf.Example proto for feeding the model.""" - example = tf.train.Example() - example.features.feature['image/encoded'].float_list.value.extend( - list(np.reshape(image, (-1)))) - return example.SerializeToString() - - -class AttentionOcrExportTest(tf.test.TestCase): - """Tests for model_export.export_model.""" - - def setUp(self): - for suffix in ['.meta', '.index', '.data-00000-of-00001']: - filename = _CHECKPOINT + suffix - self.assertTrue( - tf.io.gfile.exists(filename), - msg='Missing checkpoint file %s. ' - 'Please download and extract it from %s' % - (filename, _CHECKPOINT_URL)) - flags.FLAGS.dataset_name = 'fsns' - flags.FLAGS.checkpoint = _CHECKPOINT - flags.FLAGS.dataset_dir = os.path.join( - os.path.dirname(__file__), 'datasets/testdata/fsns') - tf.test.TestCase.setUp(self) - _clean_up() - self.export_dir = os.path.join( - tf.compat.v1.test.get_temp_dir(), 'exported_model') - self.minimal_output_signature = { - 'predictions': 'AttentionOcr_v1/predicted_chars:0', - 'scores': 'AttentionOcr_v1/predicted_scores:0', - 'predicted_length': 'AttentionOcr_v1/predicted_length:0', - 'predicted_text': 'AttentionOcr_v1/predicted_text:0', - 'predicted_conf': 'AttentionOcr_v1/predicted_conf:0', - 'normalized_seq_conf': 'AttentionOcr_v1/normalized_seq_conf:0' - } - - def create_input_feed(self, graph_def, serving): - """Returns the input feed for the model. - - Creates random images, according to the size specified by dataset_name, - format it in the correct way depending on whether the model was exported - for serving, and return the correctly keyed feed_dict for inference. - - Args: - graph_def: Graph definition of the loaded model. - serving: Whether the model was exported for Serving. - - Returns: - The feed_dict suitable for model inference. - """ - # Creates a dataset based on FLAGS.dataset_name. - self.dataset = common_flags.create_dataset('test') - # Create some random images to test inference for any dataset. - self.images = { - 'img1': - np.random.uniform(low=64, high=192, - size=self.dataset.image_shape).astype('uint8'), - 'img2': - np.random.uniform(low=32, high=224, - size=self.dataset.image_shape).astype('uint8'), - } - signature_def = graph_def.signature_def[ - tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY] - if serving: - input_name = signature_def.inputs[ - tf.saved_model.CLASSIFY_INPUTS].name - # Model for serving takes input: inputs['inputs'] = 'tf_example:0' - feed_dict = { - input_name: [ - _create_tf_example_string(self.images['img1']), - _create_tf_example_string(self.images['img2']) - ] - } - else: - input_name = signature_def.inputs['images'].name - # Model for direct use takes input: inputs['images'] = 'original_image:0' - feed_dict = { - input_name: np.stack([self.images['img1'], self.images['img2']]) - } - return feed_dict - - def verify_export_load_and_inference(self, export_for_serving=False): - """Verify exported model can be loaded and inference can run successfully. - - This function will load the exported model in self.export_dir, then create - some fake images according to the specification of FLAGS.dataset_name. - It then feeds the input through the model, and verify the minimal set of - output signatures are present. - Note: Model and dataset creation in the underlying library depends on the - following commandline flags: - FLAGS.dataset_name - Args: - export_for_serving: True if the model was exported for Serving. This - affects how input is fed into the model. - """ - tf.compat.v1.reset_default_graph() - sess = tf.compat.v1.Session() - graph_def = tf.compat.v1.saved_model.loader.load( - sess=sess, - tags=[tf.saved_model.SERVING], - export_dir=self.export_dir) - feed_dict = self.create_input_feed(graph_def, export_for_serving) - results = sess.run(self.minimal_output_signature, feed_dict=feed_dict) - - out_shape = (2,) - self.assertEqual(np.shape(results['predicted_conf']), out_shape) - self.assertEqual(np.shape(results['predicted_text']), out_shape) - self.assertEqual(np.shape(results['predicted_length']), out_shape) - self.assertEqual(np.shape(results['normalized_seq_conf']), out_shape) - out_shape = (2, self.dataset.max_sequence_length) - self.assertEqual(np.shape(results['scores']), out_shape) - self.assertEqual(np.shape(results['predictions']), out_shape) - - @flagsaver.flagsaver - def test_fsns_export_for_serving_and_load_inference(self): - model_export.export_model(self.export_dir, True) - self.verify_export_load_and_inference(True) - - @flagsaver.flagsaver - def test_fsns_export_and_load_inference(self): - model_export.export_model(self.export_dir, False, batch_size=2) - self.verify_export_load_and_inference(False) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/model_test.py b/research/attention_ocr/python/model_test.py deleted file mode 100644 index 6632a38358a..00000000000 --- a/research/attention_ocr/python/model_test.py +++ /dev/null @@ -1,300 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the model.""" -import string - -import numpy as np -import tensorflow as tf -from tensorflow.contrib import slim - -import model -import data_provider - - -def create_fake_charset(num_char_classes): - charset = {} - for i in range(num_char_classes): - charset[i] = string.printable[i % len(string.printable)] - return charset - - -class ModelTest(tf.test.TestCase): - - def setUp(self): - tf.test.TestCase.setUp(self) - - self.rng = np.random.RandomState([11, 23, 50]) - - self.batch_size = 4 - self.image_width = 600 - self.image_height = 30 - self.seq_length = 40 - self.num_char_classes = 72 - self.null_code = 62 - self.num_views = 4 - - feature_size = 288 - self.conv_tower_shape = (self.batch_size, 1, 72, feature_size) - self.features_shape = (self.batch_size, self.seq_length, feature_size) - self.chars_logit_shape = (self.batch_size, self.seq_length, - self.num_char_classes) - self.length_logit_shape = (self.batch_size, self.seq_length + 1) - # Placeholder knows image dimensions, but not batch size. - self.input_images = tf.compat.v1.placeholder( - tf.float32, - shape=(None, self.image_height, self.image_width, 3), - name='input_node') - - self.initialize_fakes() - - def initialize_fakes(self): - self.images_shape = (self.batch_size, self.image_height, self.image_width, - 3) - self.fake_images = self.rng.randint( - low=0, high=255, size=self.images_shape).astype('float32') - self.fake_conv_tower_np = self.rng.randn(*self.conv_tower_shape).astype( - 'float32') - self.fake_conv_tower = tf.constant(self.fake_conv_tower_np) - self.fake_logits = tf.constant( - self.rng.randn(*self.chars_logit_shape).astype('float32')) - self.fake_labels = tf.constant( - self.rng.randint( - low=0, - high=self.num_char_classes, - size=(self.batch_size, self.seq_length)).astype('int64')) - - def create_model(self, charset=None): - return model.Model( - self.num_char_classes, - self.seq_length, - num_views=4, - null_code=62, - charset=charset) - - def test_char_related_shapes(self): - charset = create_fake_charset(self.num_char_classes) - ocr_model = self.create_model(charset=charset) - with self.test_session() as sess: - endpoints_tf = ocr_model.create_base( - images=self.input_images, labels_one_hot=None) - sess.run(tf.compat.v1.global_variables_initializer()) - tf.compat.v1.tables_initializer().run() - endpoints = sess.run( - endpoints_tf, feed_dict={self.input_images: self.fake_images}) - - self.assertEqual( - (self.batch_size, self.seq_length, self.num_char_classes), - endpoints.chars_logit.shape) - self.assertEqual( - (self.batch_size, self.seq_length, self.num_char_classes), - endpoints.chars_log_prob.shape) - self.assertEqual((self.batch_size, self.seq_length), - endpoints.predicted_chars.shape) - self.assertEqual((self.batch_size, self.seq_length), - endpoints.predicted_scores.shape) - self.assertEqual((self.batch_size,), endpoints.predicted_text.shape) - self.assertEqual((self.batch_size,), endpoints.predicted_conf.shape) - self.assertEqual((self.batch_size,), endpoints.normalized_seq_conf.shape) - - def test_predicted_scores_are_within_range(self): - ocr_model = self.create_model() - - _, _, scores = ocr_model.char_predictions(self.fake_logits) - with self.test_session() as sess: - scores_np = sess.run( - scores, feed_dict={self.input_images: self.fake_images}) - - values_in_range = (scores_np >= 0.0) & (scores_np <= 1.0) - self.assertTrue( - np.all(values_in_range), - msg=('Scores contains out of the range values %s' % - scores_np[np.logical_not(values_in_range)])) - - def test_conv_tower_shape(self): - with self.test_session() as sess: - ocr_model = self.create_model() - conv_tower = ocr_model.conv_tower_fn(self.input_images) - - sess.run(tf.compat.v1.global_variables_initializer()) - conv_tower_np = sess.run( - conv_tower, feed_dict={self.input_images: self.fake_images}) - - self.assertEqual(self.conv_tower_shape, conv_tower_np.shape) - - def test_model_size_less_then1_gb(self): - # NOTE: Actual amount of memory occupied my TF during training will be at - # least 4X times bigger because of space need to store original weights, - # updates, gradients and variances. It also depends on the type of used - # optimizer. - ocr_model = self.create_model() - ocr_model.create_base(images=self.input_images, labels_one_hot=None) - with self.test_session() as sess: - tfprof_root = tf.compat.v1.profiler.profile( - sess.graph, - options=tf.compat.v1.profiler.ProfileOptionBuilder - .trainable_variables_parameter()) - - model_size_bytes = 4 * tfprof_root.total_parameters - self.assertLess(model_size_bytes, 1 * 2**30) - - def test_create_summaries_is_runnable(self): - ocr_model = self.create_model() - data = data_provider.InputEndpoints( - images=self.fake_images, - images_orig=self.fake_images, - labels=self.fake_labels, - labels_one_hot=slim.one_hot_encoding(self.fake_labels, - self.num_char_classes)) - endpoints = ocr_model.create_base( - images=self.fake_images, labels_one_hot=None) - charset = create_fake_charset(self.num_char_classes) - summaries = ocr_model.create_summaries( - data, endpoints, charset, is_training=False) - with self.test_session() as sess: - sess.run(tf.compat.v1.global_variables_initializer()) - sess.run(tf.compat.v1.local_variables_initializer()) - tf.compat.v1.tables_initializer().run() - sess.run(summaries) # just check it is runnable - - def test_sequence_loss_function_without_label_smoothing(self): - model = self.create_model() - model.set_mparam('sequence_loss_fn', label_smoothing=0) - - loss = model.sequence_loss_fn(self.fake_logits, self.fake_labels) - with self.test_session() as sess: - loss_np = sess.run(loss, feed_dict={self.input_images: self.fake_images}) - - # This test checks that the loss function is 'runnable'. - self.assertEqual(loss_np.shape, tuple()) - - def encode_coordinates_alt(self, net): - """An alternative implemenation for the encoding coordinates. - - Args: - net: a tensor of shape=[batch_size, height, width, num_features] - - Returns: - a list of tensors with encoded image coordinates in them. - """ - batch_size = tf.shape(input=net)[0] - _, h, w, _ = net.shape.as_list() - h_loc = [ - tf.tile( - tf.reshape( - tf.contrib.layers.one_hot_encoding( - tf.constant([i]), num_classes=h), [h, 1]), [1, w]) - for i in range(h) - ] - h_loc = tf.concat([tf.expand_dims(t, 2) for t in h_loc], 2) - w_loc = [ - tf.tile( - tf.contrib.layers.one_hot_encoding( - tf.constant([i]), num_classes=w), - [h, 1]) for i in range(w) - ] - w_loc = tf.concat([tf.expand_dims(t, 2) for t in w_loc], 2) - loc = tf.concat([h_loc, w_loc], 2) - loc = tf.tile(tf.expand_dims(loc, 0), [batch_size, 1, 1, 1]) - return tf.concat([net, loc], 3) - - def test_encoded_coordinates_have_correct_shape(self): - model = self.create_model() - model.set_mparam('encode_coordinates_fn', enabled=True) - conv_w_coords_tf = model.encode_coordinates_fn(self.fake_conv_tower) - - with self.test_session() as sess: - conv_w_coords = sess.run( - conv_w_coords_tf, feed_dict={self.input_images: self.fake_images}) - - batch_size, height, width, feature_size = self.conv_tower_shape - self.assertEqual(conv_w_coords.shape, - (batch_size, height, width, feature_size + height + width)) - - def test_disabled_coordinate_encoding_returns_features_unchanged(self): - model = self.create_model() - model.set_mparam('encode_coordinates_fn', enabled=False) - conv_w_coords_tf = model.encode_coordinates_fn(self.fake_conv_tower) - - with self.test_session() as sess: - conv_w_coords = sess.run( - conv_w_coords_tf, feed_dict={self.input_images: self.fake_images}) - - self.assertAllEqual(conv_w_coords, self.fake_conv_tower_np) - - def test_coordinate_encoding_is_correct_for_simple_example(self): - shape = (1, 2, 3, 4) # batch_size, height, width, feature_size - fake_conv_tower = tf.constant(2 * np.ones(shape), dtype=tf.float32) - model = self.create_model() - model.set_mparam('encode_coordinates_fn', enabled=True) - conv_w_coords_tf = model.encode_coordinates_fn(fake_conv_tower) - - with self.test_session() as sess: - conv_w_coords = sess.run( - conv_w_coords_tf, feed_dict={self.input_images: self.fake_images}) - - # Original features - self.assertAllEqual(conv_w_coords[0, :, :, :4], - [[[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]], - [[2, 2, 2, 2], [2, 2, 2, 2], [2, 2, 2, 2]]]) - # Encoded coordinates - self.assertAllEqual(conv_w_coords[0, :, :, 4:], - [[[1, 0, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 0, 1]], - [[0, 1, 1, 0, 0], [0, 1, 0, 1, 0], [0, 1, 0, 0, 1]]]) - - def test_alt_implementation_of_coordinate_encoding_returns_same_values(self): - model = self.create_model() - model.set_mparam('encode_coordinates_fn', enabled=True) - conv_w_coords_tf = model.encode_coordinates_fn(self.fake_conv_tower) - conv_w_coords_alt_tf = self.encode_coordinates_alt(self.fake_conv_tower) - - with self.test_session() as sess: - conv_w_coords_tf, conv_w_coords_alt_tf = sess.run( - [conv_w_coords_tf, conv_w_coords_alt_tf]) - - self.assertAllEqual(conv_w_coords_tf, conv_w_coords_alt_tf) - - def test_predicted_text_has_correct_shape_w_charset(self): - charset = create_fake_charset(self.num_char_classes) - ocr_model = self.create_model(charset=charset) - - with self.test_session() as sess: - endpoints_tf = ocr_model.create_base( - images=self.fake_images, labels_one_hot=None) - - sess.run(tf.compat.v1.global_variables_initializer()) - tf.compat.v1.tables_initializer().run() - endpoints = sess.run(endpoints_tf) - - self.assertEqual(endpoints.predicted_text.shape, (self.batch_size,)) - self.assertEqual(len(endpoints.predicted_text[0]), self.seq_length) - - -class CharsetMapperTest(tf.test.TestCase): - - def test_text_corresponds_to_ids(self): - charset = create_fake_charset(36) - ids = tf.constant([[17, 14, 21, 21, 24], [32, 24, 27, 21, 13]], - dtype=tf.int64) - charset_mapper = model.CharsetMapper(charset) - - with self.test_session() as sess: - tf.compat.v1.tables_initializer().run() - text = sess.run(charset_mapper.get_text(ids)) - - self.assertAllEqual(text, [b'hello', b'world']) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/sequence_layers.py b/research/attention_ocr/python/sequence_layers.py deleted file mode 100644 index 15c4b1c3f94..00000000000 --- a/research/attention_ocr/python/sequence_layers.py +++ /dev/null @@ -1,422 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Various implementations of sequence layers for character prediction. - -A 'sequence layer' is a part of a computation graph which is responsible of -producing a sequence of characters using extracted image features. There are -many reasonable ways to implement such layers. All of them are using RNNs. -This module provides implementations which uses 'attention' mechanism to -spatially 'pool' image features and also can use a previously predicted -character to predict the next (aka auto regression). - -Usage: - Select one of available classes, e.g. Attention or use a wrapper function to - pick one based on your requirements: - layer_class = sequence_layers.get_layer_class(use_attention=True, - use_autoregression=True) - layer = layer_class(net, labels_one_hot, model_params, method_params) - char_logits = layer.create_logits() -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import abc -import logging -import numpy as np - -import tensorflow as tf - -from tensorflow.contrib import slim - - -def orthogonal_initializer(shape, dtype=tf.float32, *args, **kwargs): - """Generates orthonormal matrices with random values. - - Orthonormal initialization is important for RNNs: - http://arxiv.org/abs/1312.6120 - http://smerity.com/articles/2016/orthogonal_init.html - - For non-square shapes the returned matrix will be semi-orthonormal: if the - number of columns exceeds the number of rows, then the rows are orthonormal - vectors; but if the number of rows exceeds the number of columns, then the - columns are orthonormal vectors. - - We use SVD decomposition to generate an orthonormal matrix with random - values. The same way as it is done in the Lasagne library for Theano. Note - that both u and v returned by the svd are orthogonal and random. We just need - to pick one with the right shape. - - Args: - shape: a shape of the tensor matrix to initialize. - dtype: a dtype of the initialized tensor. - *args: not used. - **kwargs: not used. - - Returns: - An initialized tensor. - """ - del args - del kwargs - flat_shape = (shape[0], np.prod(shape[1:])) - w = np.random.randn(*flat_shape) - u, _, v = np.linalg.svd(w, full_matrices=False) - w = u if u.shape == flat_shape else v - return tf.constant(w.reshape(shape), dtype=dtype) - - -SequenceLayerParams = collections.namedtuple('SequenceLogitsParams', [ - 'num_lstm_units', 'weight_decay', 'lstm_state_clip_value' -]) - - -class SequenceLayerBase(object): - """A base abstruct class for all sequence layers. - - A child class has to define following methods: - get_train_input - get_eval_input - unroll_cell - """ - __metaclass__ = abc.ABCMeta - - def __init__(self, net, labels_one_hot, model_params, method_params): - """Stores argument in member variable for further use. - - Args: - net: A tensor with shape [batch_size, num_features, feature_size] which - contains some extracted image features. - labels_one_hot: An optional (can be None) ground truth labels for the - input features. Is a tensor with shape - [batch_size, seq_length, num_char_classes] - model_params: A namedtuple with model parameters (model.ModelParams). - method_params: A SequenceLayerParams instance. - """ - self._params = model_params - self._mparams = method_params - self._net = net - self._labels_one_hot = labels_one_hot - self._batch_size = tf.shape(input=net)[0] - - # Initialize parameters for char logits which will be computed on the fly - # inside an LSTM decoder. - self._char_logits = {} - regularizer = tf.keras.regularizers.l2(0.5 * (self._mparams.weight_decay)) - self._softmax_w = slim.model_variable( - 'softmax_w', - [self._mparams.num_lstm_units, self._params.num_char_classes], - initializer=orthogonal_initializer, - regularizer=regularizer) - self._softmax_b = slim.model_variable( - 'softmax_b', [self._params.num_char_classes], - initializer=tf.compat.v1.zeros_initializer(), - regularizer=regularizer) - - @abc.abstractmethod - def get_train_input(self, prev, i): - """Returns a sample to be used to predict a character during training. - - This function is used as a loop_function for an RNN decoder. - - Args: - prev: output tensor from previous step of the RNN. A tensor with shape: - [batch_size, num_char_classes]. - i: index of a character in the output sequence. - - Returns: - A tensor with shape [batch_size, ?] - depth depends on implementation - details. - """ - pass - - @abc.abstractmethod - def get_eval_input(self, prev, i): - """Returns a sample to be used to predict a character during inference. - - This function is used as a loop_function for an RNN decoder. - - Args: - prev: output tensor from previous step of the RNN. A tensor with shape: - [batch_size, num_char_classes]. - i: index of a character in the output sequence. - - Returns: - A tensor with shape [batch_size, ?] - depth depends on implementation - details. - """ - raise AssertionError('Not implemented') - - @abc.abstractmethod - def unroll_cell(self, decoder_inputs, initial_state, loop_function, cell): - """Unrolls an RNN cell for all inputs. - - This is a placeholder to call some RNN decoder. It has a similar to - tf.seq2seq.rnn_decode interface. - - Args: - decoder_inputs: A list of 2D Tensors* [batch_size x input_size]. In fact, - most of existing decoders in presence of a loop_function use only the - first element to determine batch_size and length of the list to - determine number of steps. - initial_state: 2D Tensor with shape [batch_size x cell.state_size]. - loop_function: function will be applied to the i-th output in order to - generate the i+1-st input (see self.get_input). - cell: rnn_cell.RNNCell defining the cell function and size. - - Returns: - A tuple of the form (outputs, state), where: - outputs: A list of character logits of the same length as - decoder_inputs of 2D Tensors with shape [batch_size x num_characters]. - state: The state of each cell at the final time-step. - It is a 2D Tensor of shape [batch_size x cell.state_size]. - """ - pass - - def is_training(self): - """Returns True if the layer is created for training stage.""" - return self._labels_one_hot is not None - - def char_logit(self, inputs, char_index): - """Creates logits for a character if required. - - Args: - inputs: A tensor with shape [batch_size, ?] (depth is implementation - dependent). - char_index: A integer index of a character in the output sequence. - - Returns: - A tensor with shape [batch_size, num_char_classes] - """ - if char_index not in self._char_logits: - self._char_logits[char_index] = tf.compat.v1.nn.xw_plus_b(inputs, self._softmax_w, - self._softmax_b) - return self._char_logits[char_index] - - def char_one_hot(self, logit): - """Creates one hot encoding for a logit of a character. - - Args: - logit: A tensor with shape [batch_size, num_char_classes]. - - Returns: - A tensor with shape [batch_size, num_char_classes] - """ - prediction = tf.argmax(input=logit, axis=1) - return slim.one_hot_encoding(prediction, self._params.num_char_classes) - - def get_input(self, prev, i): - """A wrapper for get_train_input and get_eval_input. - - Args: - prev: output tensor from previous step of the RNN. A tensor with shape: - [batch_size, num_char_classes]. - i: index of a character in the output sequence. - - Returns: - A tensor with shape [batch_size, ?] - depth depends on implementation - details. - """ - if self.is_training(): - return self.get_train_input(prev, i) - else: - return self.get_eval_input(prev, i) - - def create_logits(self): - """Creates character sequence logits for a net specified in the constructor. - - A "main" method for the sequence layer which glues together all pieces. - - Returns: - A tensor with shape [batch_size, seq_length, num_char_classes]. - """ - with tf.compat.v1.variable_scope('LSTM'): - first_label = self.get_input(prev=None, i=0) - decoder_inputs = [first_label] + [None] * (self._params.seq_length - 1) - lstm_cell = tf.compat.v1.nn.rnn_cell.LSTMCell( - self._mparams.num_lstm_units, - use_peepholes=False, - cell_clip=self._mparams.lstm_state_clip_value, - state_is_tuple=True, - initializer=orthogonal_initializer) - lstm_outputs, _ = self.unroll_cell( - decoder_inputs=decoder_inputs, - initial_state=lstm_cell.zero_state(self._batch_size, tf.float32), - loop_function=self.get_input, - cell=lstm_cell) - - with tf.compat.v1.variable_scope('logits'): - logits_list = [ - tf.expand_dims(self.char_logit(logit, i), axis=1) - for i, logit in enumerate(lstm_outputs) - ] - - return tf.concat(logits_list, 1) - - -class NetSlice(SequenceLayerBase): - """A layer which uses a subset of image features to predict each character. - """ - - def __init__(self, *args, **kwargs): - super(NetSlice, self).__init__(*args, **kwargs) - self._zero_label = tf.zeros( - tf.stack([self._batch_size, self._params.num_char_classes])) - - def get_image_feature(self, char_index): - """Returns a subset of image features for a character. - - Args: - char_index: an index of a character. - - Returns: - A tensor with shape [batch_size, ?]. The output depth depends on the - depth of input net. - """ - batch_size, features_num, _ = [d.value for d in self._net.get_shape()] - slice_len = int(features_num / self._params.seq_length) - # In case when features_num != seq_length, we just pick a subset of image - # features, this choice is arbitrary and there is no intuitive geometrical - # interpretation. If features_num is not dividable by seq_length there will - # be unused image features. - net_slice = self._net[:, char_index:char_index + slice_len, :] - feature = tf.reshape(net_slice, [batch_size, -1]) - logging.debug('Image feature: %s', feature) - return feature - - def get_eval_input(self, prev, i): - """See SequenceLayerBase.get_eval_input for details.""" - del prev - return self.get_image_feature(i) - - def get_train_input(self, prev, i): - """See SequenceLayerBase.get_train_input for details.""" - return self.get_eval_input(prev, i) - - def unroll_cell(self, decoder_inputs, initial_state, loop_function, cell): - """See SequenceLayerBase.unroll_cell for details.""" - return tf.contrib.legacy_seq2seq.rnn_decoder( - decoder_inputs=decoder_inputs, - initial_state=initial_state, - cell=cell, - loop_function=self.get_input) - - -class NetSliceWithAutoregression(NetSlice): - """A layer similar to NetSlice, but it also uses auto regression. - - The "auto regression" means that we use network output for previous character - as a part of input for the current character. - """ - - def __init__(self, *args, **kwargs): - super(NetSliceWithAutoregression, self).__init__(*args, **kwargs) - - def get_eval_input(self, prev, i): - """See SequenceLayerBase.get_eval_input for details.""" - if i == 0: - prev = self._zero_label - else: - logit = self.char_logit(prev, char_index=i - 1) - prev = self.char_one_hot(logit) - image_feature = self.get_image_feature(char_index=i) - return tf.concat([image_feature, prev], 1) - - def get_train_input(self, prev, i): - """See SequenceLayerBase.get_train_input for details.""" - if i == 0: - prev = self._zero_label - else: - prev = self._labels_one_hot[:, i - 1, :] - image_feature = self.get_image_feature(i) - return tf.concat([image_feature, prev], 1) - - -class Attention(SequenceLayerBase): - """A layer which uses attention mechanism to select image features.""" - - def __init__(self, *args, **kwargs): - super(Attention, self).__init__(*args, **kwargs) - self._zero_label = tf.zeros( - tf.stack([self._batch_size, self._params.num_char_classes])) - - def get_eval_input(self, prev, i): - """See SequenceLayerBase.get_eval_input for details.""" - del prev, i - # The attention_decoder will fetch image features from the net, no need for - # extra inputs. - return self._zero_label - - def get_train_input(self, prev, i): - """See SequenceLayerBase.get_train_input for details.""" - return self.get_eval_input(prev, i) - - def unroll_cell(self, decoder_inputs, initial_state, loop_function, cell): - return tf.contrib.legacy_seq2seq.attention_decoder( - decoder_inputs=decoder_inputs, - initial_state=initial_state, - attention_states=self._net, - cell=cell, - loop_function=self.get_input) - - -class AttentionWithAutoregression(Attention): - """A layer which uses both attention and auto regression.""" - - def __init__(self, *args, **kwargs): - super(AttentionWithAutoregression, self).__init__(*args, **kwargs) - - def get_train_input(self, prev, i): - """See SequenceLayerBase.get_train_input for details.""" - if i == 0: - return self._zero_label - else: - # TODO(gorban): update to gradually introduce gt labels. - return self._labels_one_hot[:, i - 1, :] - - def get_eval_input(self, prev, i): - """See SequenceLayerBase.get_eval_input for details.""" - if i == 0: - return self._zero_label - else: - logit = self.char_logit(prev, char_index=i - 1) - return self.char_one_hot(logit) - - -def get_layer_class(use_attention, use_autoregression): - """A convenience function to get a layer class based on requirements. - - Args: - use_attention: if True a returned class will use attention. - use_autoregression: if True a returned class will use auto regression. - - Returns: - One of available sequence layers (child classes for SequenceLayerBase). - """ - if use_attention and use_autoregression: - layer_class = AttentionWithAutoregression - elif use_attention and not use_autoregression: - layer_class = Attention - elif not use_attention and not use_autoregression: - layer_class = NetSlice - elif not use_attention and use_autoregression: - layer_class = NetSliceWithAutoregression - else: - raise AssertionError('Unsupported sequence layer class') - - logging.debug('Use %s as a layer class', layer_class.__name__) - return layer_class diff --git a/research/attention_ocr/python/sequence_layers_test.py b/research/attention_ocr/python/sequence_layers_test.py deleted file mode 100644 index 29be1875b2a..00000000000 --- a/research/attention_ocr/python/sequence_layers_test.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for sequence_layers.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf -from tensorflow.contrib import slim - -import model -import sequence_layers - - -def fake_net(batch_size, num_features, feature_size): - return tf.convert_to_tensor( - value=np.random.uniform(size=(batch_size, num_features, feature_size)), - dtype=tf.float32) - - -def fake_labels(batch_size, seq_length, num_char_classes): - labels_np = tf.convert_to_tensor( - value=np.random.randint( - low=0, high=num_char_classes, size=(batch_size, seq_length))) - return slim.one_hot_encoding(labels_np, num_classes=num_char_classes) - - -def create_layer(layer_class, batch_size, seq_length, num_char_classes): - model_params = model.ModelParams( - num_char_classes=num_char_classes, - seq_length=seq_length, - num_views=1, - null_code=num_char_classes) - net = fake_net( - batch_size=batch_size, num_features=seq_length * 5, feature_size=6) - labels_one_hot = fake_labels(batch_size, seq_length, num_char_classes) - layer_params = sequence_layers.SequenceLayerParams( - num_lstm_units=10, weight_decay=0.00004, lstm_state_clip_value=10.0) - return layer_class(net, labels_one_hot, model_params, layer_params) - - -class SequenceLayersTest(tf.test.TestCase): - def test_net_slice_char_logits_with_correct_shape(self): - batch_size = 2 - seq_length = 4 - num_char_classes = 3 - - layer = create_layer(sequence_layers.NetSlice, batch_size, seq_length, - num_char_classes) - char_logits = layer.create_logits() - - self.assertEqual( - tf.TensorShape([batch_size, seq_length, num_char_classes]), - char_logits.get_shape()) - - def test_net_slice_with_autoregression_char_logits_with_correct_shape(self): - batch_size = 2 - seq_length = 4 - num_char_classes = 3 - - layer = create_layer(sequence_layers.NetSliceWithAutoregression, - batch_size, seq_length, num_char_classes) - char_logits = layer.create_logits() - - self.assertEqual( - tf.TensorShape([batch_size, seq_length, num_char_classes]), - char_logits.get_shape()) - - def test_attention_char_logits_with_correct_shape(self): - batch_size = 2 - seq_length = 4 - num_char_classes = 3 - - layer = create_layer(sequence_layers.Attention, batch_size, seq_length, - num_char_classes) - char_logits = layer.create_logits() - - self.assertEqual( - tf.TensorShape([batch_size, seq_length, num_char_classes]), - char_logits.get_shape()) - - def test_attention_with_autoregression_char_logits_with_correct_shape(self): - batch_size = 2 - seq_length = 4 - num_char_classes = 3 - - layer = create_layer(sequence_layers.AttentionWithAutoregression, - batch_size, seq_length, num_char_classes) - char_logits = layer.create_logits() - - self.assertEqual( - tf.TensorShape([batch_size, seq_length, num_char_classes]), - char_logits.get_shape()) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/attention_ocr/python/testdata/fsns_train_00.png b/research/attention_ocr/python/testdata/fsns_train_00.png deleted file mode 100644 index 17b59aeb8bd..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_00.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_01.png b/research/attention_ocr/python/testdata/fsns_train_01.png deleted file mode 100644 index 808ba29539f..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_01.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_02.png b/research/attention_ocr/python/testdata/fsns_train_02.png deleted file mode 100644 index d2afa327320..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_02.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_03.png b/research/attention_ocr/python/testdata/fsns_train_03.png deleted file mode 100644 index 2db55919f72..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_03.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_04.png b/research/attention_ocr/python/testdata/fsns_train_04.png deleted file mode 100644 index d18b4f2483f..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_04.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_05.png b/research/attention_ocr/python/testdata/fsns_train_05.png deleted file mode 100644 index f2fe0b62f4c..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_05.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_06.png b/research/attention_ocr/python/testdata/fsns_train_06.png deleted file mode 100644 index 94280ba572f..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_06.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_07.png b/research/attention_ocr/python/testdata/fsns_train_07.png deleted file mode 100644 index 8fc25f851ff..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_07.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_08.png b/research/attention_ocr/python/testdata/fsns_train_08.png deleted file mode 100644 index df94c054e8d..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_08.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_09.png b/research/attention_ocr/python/testdata/fsns_train_09.png deleted file mode 100644 index fa9a58d9b58..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_09.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_10.png b/research/attention_ocr/python/testdata/fsns_train_10.png deleted file mode 100644 index f32e32e3b42..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_10.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_11.png b/research/attention_ocr/python/testdata/fsns_train_11.png deleted file mode 100644 index d0e984cd178..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_11.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_12.png b/research/attention_ocr/python/testdata/fsns_train_12.png deleted file mode 100644 index 0be37f90335..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_12.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_13.png b/research/attention_ocr/python/testdata/fsns_train_13.png deleted file mode 100644 index bbc63da2d84..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_13.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_14.png b/research/attention_ocr/python/testdata/fsns_train_14.png deleted file mode 100644 index a4dabe1748f..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_14.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_15.png b/research/attention_ocr/python/testdata/fsns_train_15.png deleted file mode 100644 index 695553a68f8..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_15.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_16.png b/research/attention_ocr/python/testdata/fsns_train_16.png deleted file mode 100644 index 7aebe189ba8..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_16.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_17.png b/research/attention_ocr/python/testdata/fsns_train_17.png deleted file mode 100644 index 407ee6bd082..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_17.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_18.png b/research/attention_ocr/python/testdata/fsns_train_18.png deleted file mode 100644 index 14dfacdd339..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_18.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_19.png b/research/attention_ocr/python/testdata/fsns_train_19.png deleted file mode 100644 index d874db7d4a0..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_19.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_20.png b/research/attention_ocr/python/testdata/fsns_train_20.png deleted file mode 100644 index c0af3bcd0d1..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_20.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_21.png b/research/attention_ocr/python/testdata/fsns_train_21.png deleted file mode 100644 index f57e191e3e6..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_21.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_22.png b/research/attention_ocr/python/testdata/fsns_train_22.png deleted file mode 100644 index dd02ef39a62..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_22.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_23.png b/research/attention_ocr/python/testdata/fsns_train_23.png deleted file mode 100644 index b7669c4bd52..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_23.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_24.png b/research/attention_ocr/python/testdata/fsns_train_24.png deleted file mode 100644 index 95ec53f8133..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_24.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_25.png b/research/attention_ocr/python/testdata/fsns_train_25.png deleted file mode 100644 index 15b21a9a9e1..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_25.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_26.png b/research/attention_ocr/python/testdata/fsns_train_26.png deleted file mode 100644 index ca4eaea80bb..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_26.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_27.png b/research/attention_ocr/python/testdata/fsns_train_27.png deleted file mode 100644 index cf1949479f5..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_27.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_28.png b/research/attention_ocr/python/testdata/fsns_train_28.png deleted file mode 100644 index c95a96e446c..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_28.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_29.png b/research/attention_ocr/python/testdata/fsns_train_29.png deleted file mode 100644 index 301bb1300fd..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_29.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_30.png b/research/attention_ocr/python/testdata/fsns_train_30.png deleted file mode 100644 index d01325cf226..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_30.png and /dev/null differ diff --git a/research/attention_ocr/python/testdata/fsns_train_31.png b/research/attention_ocr/python/testdata/fsns_train_31.png deleted file mode 100644 index 30f8ff43ef6..00000000000 Binary files a/research/attention_ocr/python/testdata/fsns_train_31.png and /dev/null differ diff --git a/research/attention_ocr/python/train.py b/research/attention_ocr/python/train.py deleted file mode 100644 index 9b59fd38060..00000000000 --- a/research/attention_ocr/python/train.py +++ /dev/null @@ -1,209 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Script to train the Attention OCR model. - -A simple usage example: -python train.py -""" -import collections -import logging -import tensorflow as tf -from tensorflow.contrib import slim -from tensorflow import app -from tensorflow.compat.v1 import flags -from tensorflow.contrib.tfprof import model_analyzer - -import data_provider -import common_flags - -FLAGS = flags.FLAGS -common_flags.define() - -# yapf: disable -flags.DEFINE_integer('task', 0, - 'The Task ID. This value is used when training with ' - 'multiple workers to identify each worker.') - -flags.DEFINE_integer('ps_tasks', 0, - 'The number of parameter servers. If the value is 0, then' - ' the parameters are handled locally by the worker.') - -flags.DEFINE_integer('save_summaries_secs', 60, - 'The frequency with which summaries are saved, in ' - 'seconds.') - -flags.DEFINE_integer('save_interval_secs', 600, - 'Frequency in seconds of saving the model.') - -flags.DEFINE_integer('max_number_of_steps', int(1e10), - 'The maximum number of gradient steps.') - -flags.DEFINE_string('checkpoint_inception', '', - 'Checkpoint to recover inception weights from.') - -flags.DEFINE_float('clip_gradient_norm', 2.0, - 'If greater than 0 then the gradients would be clipped by ' - 'it.') - -flags.DEFINE_bool('sync_replicas', False, - 'If True will synchronize replicas during training.') - -flags.DEFINE_integer('replicas_to_aggregate', 1, - 'The number of gradients updates before updating params.') - -flags.DEFINE_integer('total_num_replicas', 1, - 'Total number of worker replicas.') - -flags.DEFINE_integer('startup_delay_steps', 15, - 'Number of training steps between replicas startup.') - -flags.DEFINE_boolean('reset_train_dir', False, - 'If true will delete all files in the train_log_dir') - -flags.DEFINE_boolean('show_graph_stats', False, - 'Output model size stats to stderr.') -# yapf: enable - -TrainingHParams = collections.namedtuple('TrainingHParams', [ - 'learning_rate', - 'optimizer', - 'momentum', - 'use_augment_input', -]) - - -def get_training_hparams(): - return TrainingHParams( - learning_rate=FLAGS.learning_rate, - optimizer=FLAGS.optimizer, - momentum=FLAGS.momentum, - use_augment_input=FLAGS.use_augment_input) - - -def create_optimizer(hparams): - """Creates optimized based on the specified flags.""" - if hparams.optimizer == 'momentum': - optimizer = tf.compat.v1.train.MomentumOptimizer( - hparams.learning_rate, momentum=hparams.momentum) - elif hparams.optimizer == 'adam': - optimizer = tf.compat.v1.train.AdamOptimizer(hparams.learning_rate) - elif hparams.optimizer == 'adadelta': - optimizer = tf.compat.v1.train.AdadeltaOptimizer(hparams.learning_rate) - elif hparams.optimizer == 'adagrad': - optimizer = tf.compat.v1.train.AdagradOptimizer(hparams.learning_rate) - elif hparams.optimizer == 'rmsprop': - optimizer = tf.compat.v1.train.RMSPropOptimizer( - hparams.learning_rate, momentum=hparams.momentum) - return optimizer - - -def train(loss, init_fn, hparams): - """Wraps slim.learning.train to run a training loop. - - Args: - loss: a loss tensor - init_fn: A callable to be executed after all other initialization is done. - hparams: a model hyper parameters - """ - optimizer = create_optimizer(hparams) - - if FLAGS.sync_replicas: - replica_id = tf.constant(FLAGS.task, tf.int32, shape=()) - optimizer = tf.LegacySyncReplicasOptimizer( - opt=optimizer, - replicas_to_aggregate=FLAGS.replicas_to_aggregate, - replica_id=replica_id, - total_num_replicas=FLAGS.total_num_replicas) - sync_optimizer = optimizer - startup_delay_steps = 0 - else: - startup_delay_steps = 0 - sync_optimizer = None - - train_op = slim.learning.create_train_op( - loss, - optimizer, - summarize_gradients=True, - clip_gradient_norm=FLAGS.clip_gradient_norm) - - slim.learning.train( - train_op=train_op, - logdir=FLAGS.train_log_dir, - graph=loss.graph, - master=FLAGS.master, - is_chief=(FLAGS.task == 0), - number_of_steps=FLAGS.max_number_of_steps, - save_summaries_secs=FLAGS.save_summaries_secs, - save_interval_secs=FLAGS.save_interval_secs, - startup_delay_steps=startup_delay_steps, - sync_optimizer=sync_optimizer, - init_fn=init_fn) - - -def prepare_training_dir(): - if not tf.io.gfile.exists(FLAGS.train_log_dir): - logging.info('Create a new training directory %s', FLAGS.train_log_dir) - tf.io.gfile.makedirs(FLAGS.train_log_dir) - else: - if FLAGS.reset_train_dir: - logging.info('Reset the training directory %s', FLAGS.train_log_dir) - tf.io.gfile.rmtree(FLAGS.train_log_dir) - tf.io.gfile.makedirs(FLAGS.train_log_dir) - else: - logging.info('Use already existing training directory %s', - FLAGS.train_log_dir) - - -def calculate_graph_metrics(): - param_stats = model_analyzer.print_model_analysis( - tf.compat.v1.get_default_graph(), - tfprof_options=model_analyzer.TRAINABLE_VARS_PARAMS_STAT_OPTIONS) - return param_stats.total_parameters - - -def main(_): - prepare_training_dir() - - dataset = common_flags.create_dataset(split_name=FLAGS.split_name) - model = common_flags.create_model(dataset.num_char_classes, - dataset.max_sequence_length, - dataset.num_of_views, dataset.null_code) - hparams = get_training_hparams() - - # If ps_tasks is zero, the local device is used. When using multiple - # (non-local) replicas, the ReplicaDeviceSetter distributes the variables - # across the different devices. - device_setter = tf.compat.v1.train.replica_device_setter( - FLAGS.ps_tasks, merge_devices=True) - with tf.device(device_setter): - data = data_provider.get_data( - dataset, - FLAGS.batch_size, - augment=hparams.use_augment_input, - central_crop_size=common_flags.get_crop_size()) - endpoints = model.create_base(data.images, data.labels_one_hot) - total_loss = model.create_loss(data, endpoints) - model.create_summaries(data, endpoints, dataset.charset, is_training=True) - init_fn = model.create_init_fn_to_restore(FLAGS.checkpoint, - FLAGS.checkpoint_inception) - if FLAGS.show_graph_stats: - logging.info('Total number of weights in the graph: %s', - calculate_graph_metrics()) - train(total_loss, init_fn, hparams) - - -if __name__ == '__main__': - app.run() diff --git a/research/attention_ocr/python/utils.py b/research/attention_ocr/python/utils.py deleted file mode 100644 index 5d282f72874..00000000000 --- a/research/attention_ocr/python/utils.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Functions to support building models for StreetView text transcription.""" - -import tensorflow as tf -from tensorflow.contrib import slim - - -def logits_to_log_prob(logits): - """Computes log probabilities using numerically stable trick. - - This uses two numerical stability tricks: - 1) softmax(x) = softmax(x - c) where c is a constant applied to all - arguments. If we set c = max(x) then the softmax is more numerically - stable. - 2) log softmax(x) is not numerically stable, but we can stabilize it - by using the identity log softmax(x) = x - log sum exp(x) - - Args: - logits: Tensor of arbitrary shape whose last dimension contains logits. - - Returns: - A tensor of the same shape as the input, but with corresponding log - probabilities. - """ - - with tf.compat.v1.variable_scope('log_probabilities'): - reduction_indices = len(logits.shape.as_list()) - 1 - max_logits = tf.reduce_max( - input_tensor=logits, axis=reduction_indices, keepdims=True) - safe_logits = tf.subtract(logits, max_logits) - sum_exp = tf.reduce_sum( - input_tensor=tf.exp(safe_logits), - axis=reduction_indices, - keepdims=True) - log_probs = tf.subtract(safe_logits, tf.math.log(sum_exp)) - return log_probs - - -def variables_to_restore(scope=None, strip_scope=False): - """Returns a list of variables to restore for the specified list of methods. - - It is supposed that variable name starts with the method's scope (a prefix - returned by _method_scope function). - - Args: - methods_names: a list of names of configurable methods. - strip_scope: if True will return variable names without method's scope. - If methods_names is None will return names unchanged. - model_scope: a scope for a whole model. - - Returns: - a dictionary mapping variable names to variables for restore. - """ - if scope: - variable_map = {} - method_variables = slim.get_variables_to_restore(include=[scope]) - for var in method_variables: - if strip_scope: - var_name = var.op.name[len(scope) + 1:] - else: - var_name = var.op.name - variable_map[var_name] = var - - return variable_map - else: - return {v.op.name: v for v in slim.get_variables_to_restore()} - - -def ConvertAllInputsToTensors(func): - """A decorator to convert all function's inputs into tensors. - - Args: - func: a function to decorate. - - Returns: - A decorated function. - """ - - def FuncWrapper(*args): - tensors = [tf.convert_to_tensor(value=a) for a in args] - return func(*tensors) - - return FuncWrapper diff --git a/research/audioset/README.md b/research/audioset/README.md deleted file mode 100644 index c5a39b28ec1..00000000000 --- a/research/audioset/README.md +++ /dev/null @@ -1,55 +0,0 @@ -![TensorFlow Requirement: 1.x](https://img.shields.io/badge/TensorFlow%20Requirement-1.x-brightgreen) -![TensorFlow 2 Not Supported](https://img.shields.io/badge/TensorFlow%202%20Not%20Supported-%E2%9C%95-red.svg) - -# Models for AudioSet: A Large Scale Dataset of Audio Events - -This repository provides models and supporting code associated with -[AudioSet](http://g.co/audioset), a dataset of over 2 million human-labeled -10-second YouTube video soundtracks, with labels taken from an ontology of -more than 600 audio event classes. - -AudioSet was -[released](https://research.googleblog.com/2017/03/announcing-audioset-dataset-for-audio.html) -in March 2017 by Google's Sound Understanding team to provide a common -large-scale evaluation task for audio event detection as well as a starting -point for a comprehensive vocabulary of sound events. - -For more details about AudioSet and the various models we have trained, please -visit the [AudioSet website](http://g.co/audioset) and read our papers: - -* Gemmeke, J. et. al., - [AudioSet: An ontology and human-labelled dataset for audio events](https://research.google.com/pubs/pub45857.html), - ICASSP 2017 - -* Hershey, S. et. al., - [CNN Architectures for Large-Scale Audio Classification](https://research.google.com/pubs/pub45611.html), - ICASSP 2017 - -If you use any of our pre-trained models in your published research, we ask that -you cite [CNN Architectures for Large-Scale Audio Classification](https://research.google.com/pubs/pub45611.html). -If you use the AudioSet dataset or the released embeddings of AudioSet segments, -please cite -[AudioSet: An ontology and human-labelled dataset for audio events](https://research.google.com/pubs/pub45857.html). - -## Contact - -For general questions about AudioSet and these models, please use the -[audioset-users@googlegroups.com](https://groups.google.com/forum/#!forum/audioset-users) -mailing list. - -For technical problems with the released model and code, please open an issue on -the [tensorflow/models issue tracker](https://github.com/tensorflow/models/issues) -and __*assign to @plakal and @dpwe*__. Please note that because the issue tracker -is shared across all models released by Google, we won't be notified about an -issue unless you explicitly @-mention us (@plakal and @dpwe) or assign the issue -to us. - -## Credits - -Original authors and reviewers of the code in this package include (in -alphabetical order): - -* DAn Ellis -* Shawn Hershey -* Aren Jansen -* Manoj Plakal diff --git a/research/audioset/vggish/README.md b/research/audioset/vggish/README.md deleted file mode 100644 index ec5bf4bd0c4..00000000000 --- a/research/audioset/vggish/README.md +++ /dev/null @@ -1,176 +0,0 @@ -# VGGish - -The initial AudioSet release included 128-dimensional embeddings of each -AudioSet segment produced from a VGG-like audio classification model that was -trained on a large YouTube dataset (a preliminary version of what later became -[YouTube-8M](https://research.google.com/youtube8m)). - -We provide a TensorFlow definition of this model, which we call __*VGGish*__, as -well as supporting code to extract input features for the model from audio -waveforms and to post-process the model embedding output into the same format as -the released embedding features. - -## Installation - -VGGish depends on the following Python packages: - -* [`numpy`](http://www.numpy.org/) -* [`resampy`](http://resampy.readthedocs.io/en/latest/) -* [`tensorflow`](http://www.tensorflow.org/) -* [`tf_slim`](https://github.com/google-research/tf-slim) -* [`six`](https://pythonhosted.org/six/) -* [`soundfile`](https://pysoundfile.readthedocs.io/) - -These are all easily installable via, e.g., `pip install numpy` (as in the -sample installation session below). Any reasonably recent version of these -packages shold work. - -VGGish also requires downloading two data files: - -* [VGGish model checkpoint](https://storage.googleapis.com/audioset/vggish_model.ckpt), - in TensorFlow checkpoint format. -* [Embedding PCA parameters](https://storage.googleapis.com/audioset/vggish_pca_params.npz), - in NumPy compressed archive format. - -After downloading these files into the same directory as this README, the -installation can be tested by running `python vggish_smoke_test.py` which -runs a known signal through the model and checks the output. - -Here's a sample installation and test session: - -```shell -# You can optionally install and test VGGish within a Python virtualenv, which -# is useful for isolating changes from the rest of your system. For example, you -# may have an existing version of some packages that you do not want to upgrade, -# or you want to try Python 3 instead of Python 2. If you decide to use a -# virtualenv, you can create one by running -# $ virtualenv vggish # For Python 2 -# or -# $ python3 -m venv vggish # For Python 3 -# and then enter the virtual environment by running -# $ source vggish/bin/activate # Assuming you use bash -# Leave the virtual environment at the end of the session by running -# $ deactivate -# Within the virtual environment, do not use 'sudo'. - -# Upgrade pip first. Also make sure wheel is installed. -$ sudo python -m pip install --upgrade pip wheel - -# Install all dependences. -$ sudo pip install numpy resampy tensorflow tf_slim six soundfile - -# Clone TensorFlow models repo into a 'models' directory. -$ git clone https://github.com/tensorflow/models.git -$ cd models/research/audioset/vggish -# Download data files into same directory as code. -$ curl -O https://storage.googleapis.com/audioset/vggish_model.ckpt -$ curl -O https://storage.googleapis.com/audioset/vggish_pca_params.npz - -# Installation ready, let's test it. -$ python vggish_smoke_test.py -# If we see "Looks Good To Me", then we're all set. -``` - -## Usage - -VGGish can be used in two ways: - -* *As a feature extractor*: VGGish converts audio input features into a - semantically meaningful, high-level 128-D embedding which can be fed as input - to a downstream classification model. The downstream model can be shallower - than usual because the VGGish embedding is more semantically compact than raw - audio features. - - So, for example, you could train a classifier for 10 of the AudioSet classes - by using the released embeddings as features. Then, you could use that - trained classifier with any arbitrary audio input by running the audio through - the audio feature extractor and VGGish model provided here, passing the - resulting embedding features as input to your trained model. - `vggish_inference_demo.py` shows how to produce VGGish embeddings from - arbitrary audio. - -* *As part of a larger model*: Here, we treat VGGish as a "warm start" for the - lower layers of a model that takes audio features as input and adds more - layers on top of the VGGish embedding. This can be used to fine-tune VGGish - (or parts thereof) if you have large datasets that might be very different - from the typical YouTube video clip. `vggish_train_demo.py` shows how to add - layers on top of VGGish and train the whole model. - -## About the Model - -The VGGish code layout is as follows: - -* `vggish_slim.py`: Model definition in TensorFlow Slim notation. -* `vggish_params.py`: Hyperparameters. -* `vggish_input.py`: Converter from audio waveform into input examples. -* `mel_features.py`: Audio feature extraction helpers. -* `vggish_postprocess.py`: Embedding postprocessing. -* `vggish_inference_demo.py`: Demo of VGGish in inference mode. -* `vggish_train_demo.py`: Demo of VGGish in training mode. -* `vggish_smoke_test.py`: Simple test of a VGGish installation - -### Architecture - -See `vggish_slim.py` and `vggish_params.py`. - -VGGish is a variant of the [VGG](https://arxiv.org/abs/1409.1556) model, in -particular Configuration A with 11 weight layers. Specifically, here are the -changes we made: - -* The input size was changed to 96x64 for log mel spectrogram audio inputs. - -* We drop the last group of convolutional and maxpool layers, so we now have - only four groups of convolution/maxpool layers instead of five. - -* Instead of a 1000-wide fully connected layer at the end, we use a 128-wide - fully connected layer. This acts as a compact embedding layer. - -The model definition provided here defines layers up to and including the -128-wide embedding layer. Note that the embedding layer does not include -a final non-linear activation, so the embedding value is pre-activation. -When training a model stacked on top of VGGish, you should send the -embedding through a non-linearity of your choice before adding more layers. - -### Input: Audio Features - -See `vggish_input.py` and `mel_features.py`. - -VGGish was trained with audio features computed as follows: - -* All audio is resampled to 16 kHz mono. -* A spectrogram is computed using magnitudes of the Short-Time Fourier Transform - with a window size of 25 ms, a window hop of 10 ms, and a periodic Hann - window. -* A mel spectrogram is computed by mapping the spectrogram to 64 mel bins - covering the range 125-7500 Hz. -* A stabilized log mel spectrogram is computed by applying - log(mel-spectrum + 0.01) where the offset is used to avoid taking a logarithm - of zero. -* These features are then framed into non-overlapping examples of 0.96 seconds, - where each example covers 64 mel bands and 96 frames of 10 ms each. - -We provide our own NumPy implementation that produces features that are very -similar to those produced by our internal production code. - -### Output: Embeddings - -See `vggish_postprocess.py`. - -The released AudioSet embeddings were postprocessed before release by applying a -PCA transformation (which performs both PCA and whitening) as well as -quantization to 8 bits per embedding element. This was done to be compatible -with the [YouTube-8M](https://research.google.com/youtube8m) project which has -released visual and audio embeddings for millions of YouTube videos in the same -PCA/whitened/quantized format. - -We provide a Python implementation of the postprocessing which can be applied to -batches of embeddings produced by VGGish. `vggish_inference_demo.py` shows how -the postprocessor can be run after inference. - -If you don't need to use the released embeddings or YouTube-8M, then you could -skip postprocessing and use raw embeddings. - -A Colab showing how to download the model and calculate the embeddings on your -own sound data is available here: -[VGGish Embedding Colab](https://colab.research.google.com/drive/1E3CaPAqCai9P9QhJ3WYPNCVmrJU4lAhF). - diff --git a/research/audioset/vggish/mel_features.py b/research/audioset/vggish/mel_features.py deleted file mode 100644 index ac58fb5427f..00000000000 --- a/research/audioset/vggish/mel_features.py +++ /dev/null @@ -1,223 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Defines routines to compute mel spectrogram features from audio waveform.""" - -import numpy as np - - -def frame(data, window_length, hop_length): - """Convert array into a sequence of successive possibly overlapping frames. - - An n-dimensional array of shape (num_samples, ...) is converted into an - (n+1)-D array of shape (num_frames, window_length, ...), where each frame - starts hop_length points after the preceding one. - - This is accomplished using stride_tricks, so the original data is not - copied. However, there is no zero-padding, so any incomplete frames at the - end are not included. - - Args: - data: np.array of dimension N >= 1. - window_length: Number of samples in each frame. - hop_length: Advance (in samples) between each window. - - Returns: - (N+1)-D np.array with as many rows as there are complete frames that can be - extracted. - """ - num_samples = data.shape[0] - num_frames = 1 + int(np.floor((num_samples - window_length) / hop_length)) - shape = (num_frames, window_length) + data.shape[1:] - strides = (data.strides[0] * hop_length,) + data.strides - return np.lib.stride_tricks.as_strided(data, shape=shape, strides=strides) - - -def periodic_hann(window_length): - """Calculate a "periodic" Hann window. - - The classic Hann window is defined as a raised cosine that starts and - ends on zero, and where every value appears twice, except the middle - point for an odd-length window. Matlab calls this a "symmetric" window - and np.hanning() returns it. However, for Fourier analysis, this - actually represents just over one cycle of a period N-1 cosine, and - thus is not compactly expressed on a length-N Fourier basis. Instead, - it's better to use a raised cosine that ends just before the final - zero value - i.e. a complete cycle of a period-N cosine. Matlab - calls this a "periodic" window. This routine calculates it. - - Args: - window_length: The number of points in the returned window. - - Returns: - A 1D np.array containing the periodic hann window. - """ - return 0.5 - (0.5 * np.cos(2 * np.pi / window_length * - np.arange(window_length))) - - -def stft_magnitude(signal, fft_length, - hop_length=None, - window_length=None): - """Calculate the short-time Fourier transform magnitude. - - Args: - signal: 1D np.array of the input time-domain signal. - fft_length: Size of the FFT to apply. - hop_length: Advance (in samples) between each frame passed to FFT. - window_length: Length of each block of samples to pass to FFT. - - Returns: - 2D np.array where each row contains the magnitudes of the fft_length/2+1 - unique values of the FFT for the corresponding frame of input samples. - """ - frames = frame(signal, window_length, hop_length) - # Apply frame window to each frame. We use a periodic Hann (cosine of period - # window_length) instead of the symmetric Hann of np.hanning (period - # window_length-1). - window = periodic_hann(window_length) - windowed_frames = frames * window - return np.abs(np.fft.rfft(windowed_frames, int(fft_length))) - - -# Mel spectrum constants and functions. -_MEL_BREAK_FREQUENCY_HERTZ = 700.0 -_MEL_HIGH_FREQUENCY_Q = 1127.0 - - -def hertz_to_mel(frequencies_hertz): - """Convert frequencies to mel scale using HTK formula. - - Args: - frequencies_hertz: Scalar or np.array of frequencies in hertz. - - Returns: - Object of same size as frequencies_hertz containing corresponding values - on the mel scale. - """ - return _MEL_HIGH_FREQUENCY_Q * np.log( - 1.0 + (frequencies_hertz / _MEL_BREAK_FREQUENCY_HERTZ)) - - -def spectrogram_to_mel_matrix(num_mel_bins=20, - num_spectrogram_bins=129, - audio_sample_rate=8000, - lower_edge_hertz=125.0, - upper_edge_hertz=3800.0): - """Return a matrix that can post-multiply spectrogram rows to make mel. - - Returns a np.array matrix A that can be used to post-multiply a matrix S of - spectrogram values (STFT magnitudes) arranged as frames x bins to generate a - "mel spectrogram" M of frames x num_mel_bins. M = S A. - - The classic HTK algorithm exploits the complementarity of adjacent mel bands - to multiply each FFT bin by only one mel weight, then add it, with positive - and negative signs, to the two adjacent mel bands to which that bin - contributes. Here, by expressing this operation as a matrix multiply, we go - from num_fft multiplies per frame (plus around 2*num_fft adds) to around - num_fft^2 multiplies and adds. However, because these are all presumably - accomplished in a single call to np.dot(), it's not clear which approach is - faster in Python. The matrix multiplication has the attraction of being more - general and flexible, and much easier to read. - - Args: - num_mel_bins: How many bands in the resulting mel spectrum. This is - the number of columns in the output matrix. - num_spectrogram_bins: How many bins there are in the source spectrogram - data, which is understood to be fft_size/2 + 1, i.e. the spectrogram - only contains the nonredundant FFT bins. - audio_sample_rate: Samples per second of the audio at the input to the - spectrogram. We need this to figure out the actual frequencies for - each spectrogram bin, which dictates how they are mapped into mel. - lower_edge_hertz: Lower bound on the frequencies to be included in the mel - spectrum. This corresponds to the lower edge of the lowest triangular - band. - upper_edge_hertz: The desired top edge of the highest frequency band. - - Returns: - An np.array with shape (num_spectrogram_bins, num_mel_bins). - - Raises: - ValueError: if frequency edges are incorrectly ordered or out of range. - """ - nyquist_hertz = audio_sample_rate / 2. - if lower_edge_hertz < 0.0: - raise ValueError("lower_edge_hertz %.1f must be >= 0" % lower_edge_hertz) - if lower_edge_hertz >= upper_edge_hertz: - raise ValueError("lower_edge_hertz %.1f >= upper_edge_hertz %.1f" % - (lower_edge_hertz, upper_edge_hertz)) - if upper_edge_hertz > nyquist_hertz: - raise ValueError("upper_edge_hertz %.1f is greater than Nyquist %.1f" % - (upper_edge_hertz, nyquist_hertz)) - spectrogram_bins_hertz = np.linspace(0.0, nyquist_hertz, num_spectrogram_bins) - spectrogram_bins_mel = hertz_to_mel(spectrogram_bins_hertz) - # The i'th mel band (starting from i=1) has center frequency - # band_edges_mel[i], lower edge band_edges_mel[i-1], and higher edge - # band_edges_mel[i+1]. Thus, we need num_mel_bins + 2 values in - # the band_edges_mel arrays. - band_edges_mel = np.linspace(hertz_to_mel(lower_edge_hertz), - hertz_to_mel(upper_edge_hertz), num_mel_bins + 2) - # Matrix to post-multiply feature arrays whose rows are num_spectrogram_bins - # of spectrogram values. - mel_weights_matrix = np.empty((num_spectrogram_bins, num_mel_bins)) - for i in range(num_mel_bins): - lower_edge_mel, center_mel, upper_edge_mel = band_edges_mel[i:i + 3] - # Calculate lower and upper slopes for every spectrogram bin. - # Line segments are linear in the *mel* domain, not hertz. - lower_slope = ((spectrogram_bins_mel - lower_edge_mel) / - (center_mel - lower_edge_mel)) - upper_slope = ((upper_edge_mel - spectrogram_bins_mel) / - (upper_edge_mel - center_mel)) - # .. then intersect them with each other and zero. - mel_weights_matrix[:, i] = np.maximum(0.0, np.minimum(lower_slope, - upper_slope)) - # HTK excludes the spectrogram DC bin; make sure it always gets a zero - # coefficient. - mel_weights_matrix[0, :] = 0.0 - return mel_weights_matrix - - -def log_mel_spectrogram(data, - audio_sample_rate=8000, - log_offset=0.0, - window_length_secs=0.025, - hop_length_secs=0.010, - **kwargs): - """Convert waveform to a log magnitude mel-frequency spectrogram. - - Args: - data: 1D np.array of waveform data. - audio_sample_rate: The sampling rate of data. - log_offset: Add this to values when taking log to avoid -Infs. - window_length_secs: Duration of each window to analyze. - hop_length_secs: Advance between successive analysis windows. - **kwargs: Additional arguments to pass to spectrogram_to_mel_matrix. - - Returns: - 2D np.array of (num_frames, num_mel_bins) consisting of log mel filterbank - magnitudes for successive frames. - """ - window_length_samples = int(round(audio_sample_rate * window_length_secs)) - hop_length_samples = int(round(audio_sample_rate * hop_length_secs)) - fft_length = 2 ** int(np.ceil(np.log(window_length_samples) / np.log(2.0))) - spectrogram = stft_magnitude( - data, - fft_length=fft_length, - hop_length=hop_length_samples, - window_length=window_length_samples) - mel_spectrogram = np.dot(spectrogram, spectrogram_to_mel_matrix( - num_spectrogram_bins=spectrogram.shape[1], - audio_sample_rate=audio_sample_rate, **kwargs)) - return np.log(mel_spectrogram + log_offset) diff --git a/research/audioset/vggish/vggish_export_tfhub.py b/research/audioset/vggish/vggish_export_tfhub.py deleted file mode 100644 index c3956f2365a..00000000000 --- a/research/audioset/vggish/vggish_export_tfhub.py +++ /dev/null @@ -1,126 +0,0 @@ -"""Exports VGGish as a SavedModel for publication to TF Hub. - -The exported SavedModel accepts a 1-d float32 Tensor of arbitrary shape -containing an audio waveform (assumed to be mono 16 kHz samples in the [-1, +1] -range) and returns a 2-d float32 batch of 128-d VGGish embeddings, one per -0.96s example generated from the waveform. - -Requires pip-installing tensorflow_hub. - -Usage: - vggish_export_tfhub.py -""" - -import sys -sys.path.append('..') # Lets us import yamnet modules from sibling directory. - -import numpy as np -import resampy -import tensorflow as tf -assert tf.version.VERSION >= '2.0.0', ( - 'Need at least TF 2.0, you have TF v{}'.format(tf.version.VERSION)) -import tensorflow_hub as tfhub - -import vggish_input -import vggish_params -import vggish_slim -from yamnet import features as yamnet_features -from yamnet import params as yamnet_params - - -def vggish_definer(variables, checkpoint_path): - """Defines VGGish with variables tracked and initialized from a checkpoint.""" - reader = tf.compat.v1.train.NewCheckpointReader(checkpoint_path) - - def var_tracker(next_creator, **kwargs): - """Variable creation hook that assigns initial values from a checkpoint.""" - var_name = kwargs['name'] - var_value = reader.get_tensor(var_name) - kwargs.update({'initial_value': var_value}) - var = next_creator(**kwargs) - variables.append(var) - return var - - def waveform_to_features(waveform): - """Creates VGGish features using the YAMNet feature extractor.""" - params = yamnet_params.Params( - sample_rate=vggish_params.SAMPLE_RATE, - stft_window_seconds=vggish_params.STFT_WINDOW_LENGTH_SECONDS, - stft_hop_seconds=vggish_params.STFT_HOP_LENGTH_SECONDS, - mel_bands=vggish_params.NUM_MEL_BINS, - mel_min_hz=vggish_params.MEL_MIN_HZ, - mel_max_hz=vggish_params.MEL_MAX_HZ, - log_offset=vggish_params.LOG_OFFSET, - patch_window_seconds=vggish_params.EXAMPLE_WINDOW_SECONDS, - patch_hop_seconds=vggish_params.EXAMPLE_HOP_SECONDS) - log_mel_spectrogram, features = yamnet_features.waveform_to_log_mel_spectrogram_patches( - waveform, params) - return features - - def define_vggish(waveform): - with tf.variable_creator_scope(var_tracker): - features = waveform_to_features(waveform) - return vggish_slim.define_vggish_slim(features, training=False) - - return define_vggish - - -class VGGish(tf.Module): - """A TF2 Module wrapper around VGGish.""" - def __init__(self, checkpoint_path): - super().__init__() - self._variables = [] - self._vggish_fn = tf.compat.v1.wrap_function( - vggish_definer(self._variables, checkpoint_path), - signature=(tf.TensorSpec(shape=[None], dtype=tf.float32),)) - - @tf.function(input_signature=(tf.TensorSpec(shape=[None], dtype=tf.float32),)) - def __call__(self, waveform): - return self._vggish_fn(waveform) - - -def check_model(model_fn): - """Applies vggish_smoke_test's sanity check to an instance of VGGish.""" - num_secs = 3 - freq = 1000 - sr = 44100 - t = np.arange(0, num_secs, 1 / sr) - waveform = np.sin(2 * np.pi * freq * t) - - waveform = resampy.resample(waveform, sr, vggish_params.SAMPLE_RATE) - embeddings = model_fn(waveform) - - expected_embedding_mean = -0.0333 - expected_embedding_std = 0.380 - rel_error = 0.1 - np.testing.assert_allclose( - [np.mean(embeddings), np.std(embeddings)], - [expected_embedding_mean, expected_embedding_std], - rtol=rel_error) - - -def main(args): - # Create a TF2 wrapper around VGGish. - vggish_checkpoint_path = args[0] - vggish = VGGish(vggish_checkpoint_path) - check_model(vggish) - - # Make TF-Hub export. - vggish_tfhub_export_path = args[1] - tf.saved_model.save(vggish, vggish_tfhub_export_path) - - # Check export in TF2. - model = tfhub.load(vggish_tfhub_export_path) - check_model(model) - - # Check export in TF1. - with tf.compat.v1.Graph().as_default(), tf.compat.v1.Session() as sess: - model = tfhub.load(vggish_tfhub_export_path) - sess.run(tf.compat.v1.global_variables_initializer()) - def run_model(waveform): - embeddings = model(waveform) - return sess.run(embeddings) - check_model(run_model) - -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/research/audioset/vggish/vggish_inference_demo.py b/research/audioset/vggish/vggish_inference_demo.py deleted file mode 100644 index 2294698f561..00000000000 --- a/research/audioset/vggish/vggish_inference_demo.py +++ /dev/null @@ -1,153 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""A simple demonstration of running VGGish in inference mode. - -This is intended as a toy example that demonstrates how the various building -blocks (feature extraction, model definition and loading, postprocessing) work -together in an inference context. - -A WAV file (assumed to contain signed 16-bit PCM samples) is read in, converted -into log mel spectrogram examples, fed into VGGish, the raw embedding output is -whitened and quantized, and the postprocessed embeddings are optionally written -in a SequenceExample to a TFRecord file (using the same format as the embedding -features released in AudioSet). - -Usage: - # Run a WAV file through the model and print the embeddings. The model - # checkpoint is loaded from vggish_model.ckpt and the PCA parameters are - # loaded from vggish_pca_params.npz in the current directory. - $ python vggish_inference_demo.py --wav_file /path/to/a/wav/file - - # Run a WAV file through the model and also write the embeddings to - # a TFRecord file. The model checkpoint and PCA parameters are explicitly - # passed in as well. - $ python vggish_inference_demo.py --wav_file /path/to/a/wav/file \ - --tfrecord_file /path/to/tfrecord/file \ - --checkpoint /path/to/model/checkpoint \ - --pca_params /path/to/pca/params - - # Run a built-in input (a sine wav) through the model and print the - # embeddings. Associated model files are read from the current directory. - $ python vggish_inference_demo.py -""" - -from __future__ import print_function - -import numpy as np -import six -import soundfile -import tensorflow.compat.v1 as tf - -import vggish_input -import vggish_params -import vggish_postprocess -import vggish_slim - -flags = tf.app.flags - -flags.DEFINE_string( - 'wav_file', None, - 'Path to a wav file. Should contain signed 16-bit PCM samples. ' - 'If none is provided, a synthetic sound is used.') - -flags.DEFINE_string( - 'checkpoint', 'vggish_model.ckpt', - 'Path to the VGGish checkpoint file.') - -flags.DEFINE_string( - 'pca_params', 'vggish_pca_params.npz', - 'Path to the VGGish PCA parameters file.') - -flags.DEFINE_string( - 'tfrecord_file', None, - 'Path to a TFRecord file where embeddings will be written.') - -FLAGS = flags.FLAGS - - -def main(_): - # In this simple example, we run the examples from a single audio file through - # the model. If none is provided, we generate a synthetic input. - if FLAGS.wav_file: - wav_file = FLAGS.wav_file - else: - # Write a WAV of a sine wav into an in-memory file object. - num_secs = 5 - freq = 1000 - sr = 44100 - t = np.arange(0, num_secs, 1 / sr) - x = np.sin(2 * np.pi * freq * t) - # Convert to signed 16-bit samples. - samples = np.clip(x * 32768, -32768, 32767).astype(np.int16) - wav_file = six.BytesIO() - soundfile.write(wav_file, samples, sr, format='WAV', subtype='PCM_16') - wav_file.seek(0) - examples_batch = vggish_input.wavfile_to_examples(wav_file) - print(examples_batch) - - # Prepare a postprocessor to munge the model embeddings. - pproc = vggish_postprocess.Postprocessor(FLAGS.pca_params) - - # If needed, prepare a record writer to store the postprocessed embeddings. - writer = tf.python_io.TFRecordWriter( - FLAGS.tfrecord_file) if FLAGS.tfrecord_file else None - - with tf.Graph().as_default(), tf.Session() as sess: - # Define the model in inference mode, load the checkpoint, and - # locate input and output tensors. - vggish_slim.define_vggish_slim(training=False) - vggish_slim.load_vggish_slim_checkpoint(sess, FLAGS.checkpoint) - features_tensor = sess.graph.get_tensor_by_name( - vggish_params.INPUT_TENSOR_NAME) - embedding_tensor = sess.graph.get_tensor_by_name( - vggish_params.OUTPUT_TENSOR_NAME) - - # Run inference and postprocessing. - [embedding_batch] = sess.run([embedding_tensor], - feed_dict={features_tensor: examples_batch}) - print(embedding_batch) - postprocessed_batch = pproc.postprocess(embedding_batch) - print(postprocessed_batch) - - # Write the postprocessed embeddings as a SequenceExample, in a similar - # format as the features released in AudioSet. Each row of the batch of - # embeddings corresponds to roughly a second of audio (96 10ms frames), and - # the rows are written as a sequence of bytes-valued features, where each - # feature value contains the 128 bytes of the whitened quantized embedding. - seq_example = tf.train.SequenceExample( - feature_lists=tf.train.FeatureLists( - feature_list={ - vggish_params.AUDIO_EMBEDDING_FEATURE_NAME: - tf.train.FeatureList( - feature=[ - tf.train.Feature( - bytes_list=tf.train.BytesList( - value=[embedding.tobytes()])) - for embedding in postprocessed_batch - ] - ) - } - ) - ) - print(seq_example) - if writer: - writer.write(seq_example.SerializeToString()) - - if writer: - writer.close() - -if __name__ == '__main__': - tf.app.run() diff --git a/research/audioset/vggish/vggish_input.py b/research/audioset/vggish/vggish_input.py deleted file mode 100644 index f283afbcffa..00000000000 --- a/research/audioset/vggish/vggish_input.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Compute input examples for VGGish from audio waveform.""" - -import numpy as np -import resampy - -import mel_features -import vggish_params - -try: - import soundfile as sf - - def wav_read(wav_file): - wav_data, sr = sf.read(wav_file, dtype='int16') - return wav_data, sr - -except ImportError: - - def wav_read(wav_file): - raise NotImplementedError('WAV file reading requires soundfile package.') - - -def waveform_to_examples(data, sample_rate): - """Converts audio waveform into an array of examples for VGGish. - - Args: - data: np.array of either one dimension (mono) or two dimensions - (multi-channel, with the outer dimension representing channels). - Each sample is generally expected to lie in the range [-1.0, +1.0], - although this is not required. - sample_rate: Sample rate of data. - - Returns: - 3-D np.array of shape [num_examples, num_frames, num_bands] which represents - a sequence of examples, each of which contains a patch of log mel - spectrogram, covering num_frames frames of audio and num_bands mel frequency - bands, where the frame length is vggish_params.STFT_HOP_LENGTH_SECONDS. - """ - # Convert to mono. - if len(data.shape) > 1: - data = np.mean(data, axis=1) - # Resample to the rate assumed by VGGish. - if sample_rate != vggish_params.SAMPLE_RATE: - data = resampy.resample(data, sample_rate, vggish_params.SAMPLE_RATE) - - # Compute log mel spectrogram features. - log_mel = mel_features.log_mel_spectrogram( - data, - audio_sample_rate=vggish_params.SAMPLE_RATE, - log_offset=vggish_params.LOG_OFFSET, - window_length_secs=vggish_params.STFT_WINDOW_LENGTH_SECONDS, - hop_length_secs=vggish_params.STFT_HOP_LENGTH_SECONDS, - num_mel_bins=vggish_params.NUM_MEL_BINS, - lower_edge_hertz=vggish_params.MEL_MIN_HZ, - upper_edge_hertz=vggish_params.MEL_MAX_HZ) - - # Frame features into examples. - features_sample_rate = 1.0 / vggish_params.STFT_HOP_LENGTH_SECONDS - example_window_length = int(round( - vggish_params.EXAMPLE_WINDOW_SECONDS * features_sample_rate)) - example_hop_length = int(round( - vggish_params.EXAMPLE_HOP_SECONDS * features_sample_rate)) - log_mel_examples = mel_features.frame( - log_mel, - window_length=example_window_length, - hop_length=example_hop_length) - return log_mel_examples - - -def wavfile_to_examples(wav_file): - """Convenience wrapper around waveform_to_examples() for a common WAV format. - - Args: - wav_file: String path to a file, or a file-like object. The file - is assumed to contain WAV audio data with signed 16-bit PCM samples. - - Returns: - See waveform_to_examples. - """ - wav_data, sr = wav_read(wav_file) - assert wav_data.dtype == np.int16, 'Bad sample type: %r' % wav_data.dtype - samples = wav_data / 32768.0 # Convert to [-1.0, +1.0] - return waveform_to_examples(samples, sr) diff --git a/research/audioset/vggish/vggish_params.py b/research/audioset/vggish/vggish_params.py deleted file mode 100644 index a38ce26c9d6..00000000000 --- a/research/audioset/vggish/vggish_params.py +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Global parameters for the VGGish model. - -See vggish_slim.py for more information. -""" - -# Architectural constants. -NUM_FRAMES = 96 # Frames in input mel-spectrogram patch. -NUM_BANDS = 64 # Frequency bands in input mel-spectrogram patch. -EMBEDDING_SIZE = 128 # Size of embedding layer. - -# Hyperparameters used in feature and example generation. -SAMPLE_RATE = 16000 -STFT_WINDOW_LENGTH_SECONDS = 0.025 -STFT_HOP_LENGTH_SECONDS = 0.010 -NUM_MEL_BINS = NUM_BANDS -MEL_MIN_HZ = 125 -MEL_MAX_HZ = 7500 -LOG_OFFSET = 0.01 # Offset used for stabilized log of input mel-spectrogram. -EXAMPLE_WINDOW_SECONDS = 0.96 # Each example contains 96 10ms frames -EXAMPLE_HOP_SECONDS = 0.96 # with zero overlap. - -# Parameters used for embedding postprocessing. -PCA_EIGEN_VECTORS_NAME = 'pca_eigen_vectors' -PCA_MEANS_NAME = 'pca_means' -QUANTIZE_MIN_VAL = -2.0 -QUANTIZE_MAX_VAL = +2.0 - -# Hyperparameters used in training. -INIT_STDDEV = 0.01 # Standard deviation used to initialize weights. -LEARNING_RATE = 1e-4 # Learning rate for the Adam optimizer. -ADAM_EPSILON = 1e-8 # Epsilon for the Adam optimizer. - -# Names of ops, tensors, and features. -INPUT_OP_NAME = 'vggish/input_features' -INPUT_TENSOR_NAME = INPUT_OP_NAME + ':0' -OUTPUT_OP_NAME = 'vggish/embedding' -OUTPUT_TENSOR_NAME = OUTPUT_OP_NAME + ':0' -AUDIO_EMBEDDING_FEATURE_NAME = 'audio_embedding' diff --git a/research/audioset/vggish/vggish_postprocess.py b/research/audioset/vggish/vggish_postprocess.py deleted file mode 100644 index aef23babef6..00000000000 --- a/research/audioset/vggish/vggish_postprocess.py +++ /dev/null @@ -1,91 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Post-process embeddings from VGGish.""" - -import numpy as np - -import vggish_params - - -class Postprocessor(object): - """Post-processes VGGish embeddings. - - The initial release of AudioSet included 128-D VGGish embeddings for each - segment of AudioSet. These released embeddings were produced by applying - a PCA transformation (technically, a whitening transform is included as well) - and 8-bit quantization to the raw embedding output from VGGish, in order to - stay compatible with the YouTube-8M project which provides visual embeddings - in the same format for a large set of YouTube videos. This class implements - the same PCA (with whitening) and quantization transformations. - """ - - def __init__(self, pca_params_npz_path): - """Constructs a postprocessor. - - Args: - pca_params_npz_path: Path to a NumPy-format .npz file that - contains the PCA parameters used in postprocessing. - """ - params = np.load(pca_params_npz_path) - self._pca_matrix = params[vggish_params.PCA_EIGEN_VECTORS_NAME] - # Load means into a column vector for easier broadcasting later. - self._pca_means = params[vggish_params.PCA_MEANS_NAME].reshape(-1, 1) - assert self._pca_matrix.shape == ( - vggish_params.EMBEDDING_SIZE, vggish_params.EMBEDDING_SIZE), ( - 'Bad PCA matrix shape: %r' % (self._pca_matrix.shape,)) - assert self._pca_means.shape == (vggish_params.EMBEDDING_SIZE, 1), ( - 'Bad PCA means shape: %r' % (self._pca_means.shape,)) - - def postprocess(self, embeddings_batch): - """Applies postprocessing to a batch of embeddings. - - Args: - embeddings_batch: An nparray of shape [batch_size, embedding_size] - containing output from the embedding layer of VGGish. - - Returns: - An nparray of the same shape as the input but of type uint8, - containing the PCA-transformed and quantized version of the input. - """ - assert len(embeddings_batch.shape) == 2, ( - 'Expected 2-d batch, got %r' % (embeddings_batch.shape,)) - assert embeddings_batch.shape[1] == vggish_params.EMBEDDING_SIZE, ( - 'Bad batch shape: %r' % (embeddings_batch.shape,)) - - # Apply PCA. - # - Embeddings come in as [batch_size, embedding_size]. - # - Transpose to [embedding_size, batch_size]. - # - Subtract pca_means column vector from each column. - # - Premultiply by PCA matrix of shape [output_dims, input_dims] - # where both are are equal to embedding_size in our case. - # - Transpose result back to [batch_size, embedding_size]. - pca_applied = np.dot(self._pca_matrix, - (embeddings_batch.T - self._pca_means)).T - - # Quantize by: - # - clipping to [min, max] range - clipped_embeddings = np.clip( - pca_applied, vggish_params.QUANTIZE_MIN_VAL, - vggish_params.QUANTIZE_MAX_VAL) - # - convert to 8-bit in range [0.0, 255.0] - quantized_embeddings = ( - (clipped_embeddings - vggish_params.QUANTIZE_MIN_VAL) * - (255.0 / - (vggish_params.QUANTIZE_MAX_VAL - vggish_params.QUANTIZE_MIN_VAL))) - # - cast 8-bit float to uint8 - quantized_embeddings = quantized_embeddings.astype(np.uint8) - - return quantized_embeddings diff --git a/research/audioset/vggish/vggish_slim.py b/research/audioset/vggish/vggish_slim.py deleted file mode 100644 index 84a8aac3986..00000000000 --- a/research/audioset/vggish/vggish_slim.py +++ /dev/null @@ -1,136 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Defines the 'VGGish' model used to generate AudioSet embedding features. - -The public AudioSet release (https://research.google.com/audioset/download.html) -includes 128-D features extracted from the embedding layer of a VGG-like model -that was trained on a large Google-internal YouTube dataset. Here we provide -a TF-Slim definition of the same model, without any dependences on libraries -internal to Google. We call it 'VGGish'. - -Note that we only define the model up to the embedding layer, which is the -penultimate layer before the final classifier layer. We also provide various -hyperparameter values (in vggish_params.py) that were used to train this model -internally. - -For comparison, here is TF-Slim's VGG definition: -https://github.com/tensorflow/models/blob/master/research/slim/nets/vgg.py -""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -import vggish_params as params - - -def define_vggish_slim(features_tensor=None, training=False): - """Defines the VGGish TensorFlow model. - - All ops are created in the current default graph, under the scope 'vggish/'. - - The input is either a tensor passed in via the optional 'features_tensor' - argument or a placeholder created below named 'vggish/input_features'. The - input is expected to have dtype float32 and shape [batch_size, num_frames, - num_bands] where batch_size is variable and num_frames and num_bands are - constants, and [num_frames, num_bands] represents a log-mel-scale spectrogram - patch covering num_bands frequency bands and num_frames time frames (where - each frame step is usually 10ms). This is produced by computing the stabilized - log(mel-spectrogram + params.LOG_OFFSET). The output is a tensor named - 'vggish/embedding' which produces the pre-activation values of a 128-D - embedding layer, which is usually the penultimate layer when used as part of a - full model with a final classifier layer. - - Args: - features_tensor: If not None, the tensor containing the input features. - If None, a placeholder input is created. - training: If true, all parameters are marked trainable. - - Returns: - The op 'vggish/embeddings'. - """ - # Defaults: - # - All weights are initialized to N(0, INIT_STDDEV). - # - All biases are initialized to 0. - # - All activations are ReLU. - # - All convolutions are 3x3 with stride 1 and SAME padding. - # - All max-pools are 2x2 with stride 2 and SAME padding. - with slim.arg_scope([slim.conv2d, slim.fully_connected], - weights_initializer=tf.truncated_normal_initializer( - stddev=params.INIT_STDDEV), - biases_initializer=tf.zeros_initializer(), - activation_fn=tf.nn.relu, - trainable=training), \ - slim.arg_scope([slim.conv2d], - kernel_size=[3, 3], stride=1, padding='SAME'), \ - slim.arg_scope([slim.max_pool2d], - kernel_size=[2, 2], stride=2, padding='SAME'), \ - tf.variable_scope('vggish'): - # Input: a batch of 2-D log-mel-spectrogram patches. - if features_tensor is None: - features_tensor = tf.placeholder( - tf.float32, shape=(None, params.NUM_FRAMES, params.NUM_BANDS), - name='input_features') - # Reshape to 4-D so that we can convolve a batch with conv2d(). - net = tf.reshape(features_tensor, - [-1, params.NUM_FRAMES, params.NUM_BANDS, 1]) - - # The VGG stack of alternating convolutions and max-pools. - net = slim.conv2d(net, 64, scope='conv1') - net = slim.max_pool2d(net, scope='pool1') - net = slim.conv2d(net, 128, scope='conv2') - net = slim.max_pool2d(net, scope='pool2') - net = slim.repeat(net, 2, slim.conv2d, 256, scope='conv3') - net = slim.max_pool2d(net, scope='pool3') - net = slim.repeat(net, 2, slim.conv2d, 512, scope='conv4') - net = slim.max_pool2d(net, scope='pool4') - - # Flatten before entering fully-connected layers - net = slim.flatten(net) - net = slim.repeat(net, 2, slim.fully_connected, 4096, scope='fc1') - # The embedding layer. - net = slim.fully_connected(net, params.EMBEDDING_SIZE, scope='fc2', - activation_fn=None) - return tf.identity(net, name='embedding') - - -def load_vggish_slim_checkpoint(session, checkpoint_path): - """Loads a pre-trained VGGish-compatible checkpoint. - - This function can be used as an initialization function (referred to as - init_fn in TensorFlow documentation) which is called in a Session after - initializating all variables. When used as an init_fn, this will load - a pre-trained checkpoint that is compatible with the VGGish model - definition. Only variables defined by VGGish will be loaded. - - Args: - session: an active TensorFlow session. - checkpoint_path: path to a file containing a checkpoint that is - compatible with the VGGish model definition. - """ - # Get the list of names of all VGGish variables that exist in - # the checkpoint (i.e., all inference-mode VGGish variables). - with tf.Graph().as_default(): - define_vggish_slim(training=False) - vggish_var_names = [v.name for v in tf.global_variables()] - - # Get the list of all currently existing variables that match - # the list of variable names we just computed. - vggish_vars = [v for v in tf.global_variables() if v.name in vggish_var_names] - - # Use a Saver to restore just the variables selected above. - saver = tf.train.Saver(vggish_vars, name='vggish_load_pretrained', - write_version=1) - saver.restore(session, checkpoint_path) diff --git a/research/audioset/vggish/vggish_smoke_test.py b/research/audioset/vggish/vggish_smoke_test.py deleted file mode 100644 index 82a644a91e3..00000000000 --- a/research/audioset/vggish/vggish_smoke_test.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A smoke test for VGGish. - -This is a simple smoke test of a local install of VGGish and its associated -downloaded files. We create a synthetic sound, extract log mel spectrogram -features, run them through VGGish, post-process the embedding ouputs, and -check some simple statistics of the results, allowing for variations that -might occur due to platform/version differences in the libraries we use. - -Usage: -- Download the VGGish checkpoint and PCA parameters into the same directory as - the VGGish source code. If you keep them elsewhere, update the checkpoint_path - and pca_params_path variables below. -- Run: - $ python vggish_smoke_test.py -""" - -from __future__ import print_function - -import numpy as np -import tensorflow.compat.v1 as tf - -import vggish_input -import vggish_params -import vggish_postprocess -import vggish_slim - -print('\nTesting your install of VGGish\n') - -# Paths to downloaded VGGish files. -checkpoint_path = 'vggish_model.ckpt' -pca_params_path = 'vggish_pca_params.npz' - -# Relative tolerance of errors in mean and standard deviation of embeddings. -rel_error = 0.1 # Up to 10% - -# Generate a 1 kHz sine wave at 44.1 kHz (we use a high sampling rate -# to test resampling to 16 kHz during feature extraction). -num_secs = 3 -freq = 1000 -sr = 44100 -t = np.arange(0, num_secs, 1 / sr) -x = np.sin(2 * np.pi * freq * t) - -# Produce a batch of log mel spectrogram examples. -input_batch = vggish_input.waveform_to_examples(x, sr) -print('Log Mel Spectrogram example: ', input_batch[0]) -np.testing.assert_equal( - input_batch.shape, - [num_secs, vggish_params.NUM_FRAMES, vggish_params.NUM_BANDS]) - -# Define VGGish, load the checkpoint, and run the batch through the model to -# produce embeddings. -with tf.Graph().as_default(), tf.Session() as sess: - vggish_slim.define_vggish_slim() - vggish_slim.load_vggish_slim_checkpoint(sess, checkpoint_path) - - features_tensor = sess.graph.get_tensor_by_name( - vggish_params.INPUT_TENSOR_NAME) - embedding_tensor = sess.graph.get_tensor_by_name( - vggish_params.OUTPUT_TENSOR_NAME) - [embedding_batch] = sess.run([embedding_tensor], - feed_dict={features_tensor: input_batch}) - print('VGGish embedding: ', embedding_batch[0]) - expected_embedding_mean = -0.0333 - expected_embedding_std = 0.380 - np.testing.assert_allclose( - [np.mean(embedding_batch), np.std(embedding_batch)], - [expected_embedding_mean, expected_embedding_std], - rtol=rel_error) - -# Postprocess the results to produce whitened quantized embeddings. -pproc = vggish_postprocess.Postprocessor(pca_params_path) -postprocessed_batch = pproc.postprocess(embedding_batch) -print('Postprocessed VGGish embedding: ', postprocessed_batch[0]) -expected_postprocessed_mean = 122.0 -expected_postprocessed_std = 93.5 -np.testing.assert_allclose( - [np.mean(postprocessed_batch), np.std(postprocessed_batch)], - [expected_postprocessed_mean, expected_postprocessed_std], - rtol=rel_error) - -print('\nLooks Good To Me!\n') diff --git a/research/audioset/vggish/vggish_train_demo.py b/research/audioset/vggish/vggish_train_demo.py deleted file mode 100644 index 7a968b17110..00000000000 --- a/research/audioset/vggish/vggish_train_demo.py +++ /dev/null @@ -1,184 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""A simple demonstration of running VGGish in training mode. - -This is intended as a toy example that demonstrates how to use the VGGish model -definition within a larger model that adds more layers on top, and then train -the larger model. If you let VGGish train as well, then this allows you to -fine-tune the VGGish model parameters for your application. If you don't let -VGGish train, then you use VGGish as a feature extractor for the layers above -it. - -For this toy task, we are training a classifier to distinguish between three -classes: sine waves, constant signals, and white noise. We generate synthetic -waveforms from each of these classes, convert into shuffled batches of log mel -spectrogram examples with associated labels, and feed the batches into a model -that includes VGGish at the bottom and a couple of additional layers on top. We -also plumb in labels that are associated with the examples, which feed a label -loss used for training. - -Usage: - # Run training for 100 steps using a model checkpoint in the default - # location (vggish_model.ckpt in the current directory). Allow VGGish - # to get fine-tuned. - $ python vggish_train_demo.py --num_batches 100 - - # Same as before but run for fewer steps and don't change VGGish parameters - # and use a checkpoint in a different location - $ python vggish_train_demo.py --num_batches 50 \ - --train_vggish=False \ - --checkpoint /path/to/model/checkpoint -""" - -from __future__ import print_function - -from random import shuffle - -import numpy as np -import tensorflow.compat.v1 as tf -import tf_slim as slim - -import vggish_input -import vggish_params -import vggish_slim - -flags = tf.app.flags - -flags.DEFINE_integer( - 'num_batches', 30, - 'Number of batches of examples to feed into the model. Each batch is of ' - 'variable size and contains shuffled examples of each class of audio.') - -flags.DEFINE_boolean( - 'train_vggish', True, - 'If True, allow VGGish parameters to change during training, thus ' - 'fine-tuning VGGish. If False, VGGish parameters are fixed, thus using ' - 'VGGish as a fixed feature extractor.') - -flags.DEFINE_string( - 'checkpoint', 'vggish_model.ckpt', - 'Path to the VGGish checkpoint file.') - -FLAGS = flags.FLAGS - -_NUM_CLASSES = 3 - - -def _get_examples_batch(): - """Returns a shuffled batch of examples of all audio classes. - - Note that this is just a toy function because this is a simple demo intended - to illustrate how the training code might work. - - Returns: - a tuple (features, labels) where features is a NumPy array of shape - [batch_size, num_frames, num_bands] where the batch_size is variable and - each row is a log mel spectrogram patch of shape [num_frames, num_bands] - suitable for feeding VGGish, while labels is a NumPy array of shape - [batch_size, num_classes] where each row is a multi-hot label vector that - provides the labels for corresponding rows in features. - """ - # Make a waveform for each class. - num_seconds = 5 - sr = 44100 # Sampling rate. - t = np.arange(0, num_seconds, 1 / sr) # Time axis - # Random sine wave. - freq = np.random.uniform(100, 1000) - sine = np.sin(2 * np.pi * freq * t) - # Random constant signal. - magnitude = np.random.uniform(-1, 1) - const = magnitude * t - # White noise. - noise = np.random.normal(-1, 1, size=t.shape) - - # Make examples of each signal and corresponding labels. - # Sine is class index 0, Const class index 1, Noise class index 2. - sine_examples = vggish_input.waveform_to_examples(sine, sr) - sine_labels = np.array([[1, 0, 0]] * sine_examples.shape[0]) - const_examples = vggish_input.waveform_to_examples(const, sr) - const_labels = np.array([[0, 1, 0]] * const_examples.shape[0]) - noise_examples = vggish_input.waveform_to_examples(noise, sr) - noise_labels = np.array([[0, 0, 1]] * noise_examples.shape[0]) - - # Shuffle (example, label) pairs across all classes. - all_examples = np.concatenate((sine_examples, const_examples, noise_examples)) - all_labels = np.concatenate((sine_labels, const_labels, noise_labels)) - labeled_examples = list(zip(all_examples, all_labels)) - shuffle(labeled_examples) - - # Separate and return the features and labels. - features = [example for (example, _) in labeled_examples] - labels = [label for (_, label) in labeled_examples] - return (features, labels) - - -def main(_): - with tf.Graph().as_default(), tf.Session() as sess: - # Define VGGish. - embeddings = vggish_slim.define_vggish_slim(training=FLAGS.train_vggish) - - # Define a shallow classification model and associated training ops on top - # of VGGish. - with tf.variable_scope('mymodel'): - # Add a fully connected layer with 100 units. Add an activation function - # to the embeddings since they are pre-activation. - num_units = 100 - fc = slim.fully_connected(tf.nn.relu(embeddings), num_units) - - # Add a classifier layer at the end, consisting of parallel logistic - # classifiers, one per class. This allows for multi-class tasks. - logits = slim.fully_connected( - fc, _NUM_CLASSES, activation_fn=None, scope='logits') - tf.sigmoid(logits, name='prediction') - - # Add training ops. - with tf.variable_scope('train'): - global_step = tf.train.create_global_step() - - # Labels are assumed to be fed as a batch multi-hot vectors, with - # a 1 in the position of each positive class label, and 0 elsewhere. - labels_input = tf.placeholder( - tf.float32, shape=(None, _NUM_CLASSES), name='labels') - - # Cross-entropy label loss. - xent = tf.nn.sigmoid_cross_entropy_with_logits( - logits=logits, labels=labels_input, name='xent') - loss = tf.reduce_mean(xent, name='loss_op') - tf.summary.scalar('loss', loss) - - # We use the same optimizer and hyperparameters as used to train VGGish. - optimizer = tf.train.AdamOptimizer( - learning_rate=vggish_params.LEARNING_RATE, - epsilon=vggish_params.ADAM_EPSILON) - train_op = optimizer.minimize(loss, global_step=global_step) - - # Initialize all variables in the model, and then load the pre-trained - # VGGish checkpoint. - sess.run(tf.global_variables_initializer()) - vggish_slim.load_vggish_slim_checkpoint(sess, FLAGS.checkpoint) - - # The training loop. - features_input = sess.graph.get_tensor_by_name( - vggish_params.INPUT_TENSOR_NAME) - for _ in range(FLAGS.num_batches): - (features, labels) = _get_examples_batch() - [num_steps, loss_value, _] = sess.run( - [global_step, loss, train_op], - feed_dict={features_input: features, labels_input: labels}) - print('Step %d: loss %g' % (num_steps, loss_value)) - -if __name__ == '__main__': - tf.app.run() diff --git a/research/audioset/yamnet/README.md b/research/audioset/yamnet/README.md deleted file mode 100644 index 4f3caddfd0f..00000000000 --- a/research/audioset/yamnet/README.md +++ /dev/null @@ -1,134 +0,0 @@ -# YAMNet - -YAMNet is a pretrained deep net that predicts 521 audio event classes based on -the [AudioSet-YouTube corpus](http://g.co/audioset), and employing the -[Mobilenet_v1](https://arxiv.org/pdf/1704.04861.pdf) depthwise-separable -convolution architecture. - -This directory contains the Keras code to construct the model, and example code -for applying the model to input sound files. - -## Installation - -YAMNet depends on the following Python packages: - -* [`numpy`](http://www.numpy.org/) -* [`resampy`](http://resampy.readthedocs.io/en/latest/) -* [`tensorflow`](http://www.tensorflow.org/) -* [`pysoundfile`](https://pysoundfile.readthedocs.io/) - -These are all easily installable via, e.g., `pip install numpy` (as in the -example command sequence below). Any reasonably recent version of these -packages should work. - -YAMNet also requires downloading the following data file: - -* [YAMNet model weights](https://storage.googleapis.com/audioset/yamnet.h5) - in Keras saved weights in HDF5 format. - -After downloading this file into the same directory as this README, the -installation can be tested by running `python yamnet_test.py` which -runs some synthetic signals through the model and checks the outputs. - -Here's a sample installation and test session: - -```shell -# Upgrade pip first. Also make sure wheel is installed. -python -m pip install --upgrade pip wheel - -# Install dependences. -pip install numpy resampy tensorflow soundfile - -# Clone TensorFlow models repo into a 'models' directory. -git clone https://github.com/tensorflow/models.git -cd models/research/audioset/yamnet -# Download data file into same directory as code. -curl -O https://storage.googleapis.com/audioset/yamnet.h5 - -# Installation ready, let's test it. -python yamnet_test.py -# If we see "Ran 4 tests ... OK ...", then we're all set. -``` - -## Usage - -You can run the model over existing soundfiles using inference.py: - -```shell -python inference.py input_sound.wav -``` -The code will report the top-5 highest-scoring classes averaged over all the -frames of the input. You can access greater detail by modifying the example -code in inference.py. - -See the jupyter notebook `yamnet_visualization.ipynb` for an example of -displaying the per-frame model output scores. - - -## About the Model - -The YAMNet code layout is as follows: - -* `yamnet.py`: Model definition in Keras. -* `params.py`: Hyperparameters. You can usefully modify PATCH_HOP_SECONDS. -* `features.py`: Audio feature extraction helpers. -* `inference.py`: Example code to classify input wav files. -* `yamnet_test.py`: Simple test of YAMNet installation - -### Input: Audio Features - -See `features.py`. - -As with our previous release -[VGGish](https://github.com/tensorflow/models/tree/master/research/audioset/vggish), -YAMNet was trained with audio features computed as follows: - -* All audio is resampled to 16 kHz mono. -* A spectrogram is computed using magnitudes of the Short-Time Fourier Transform - with a window size of 25 ms, a window hop of 10 ms, and a periodic Hann - window. -* A mel spectrogram is computed by mapping the spectrogram to 64 mel bins - covering the range 125-7500 Hz. -* A stabilized log mel spectrogram is computed by applying - log(mel-spectrum + 0.001) where the offset is used to avoid taking a logarithm - of zero. -* These features are then framed into 50%-overlapping examples of 0.96 seconds, - where each example covers 64 mel bands and 96 frames of 10 ms each. - -These 96x64 patches are then fed into the Mobilenet_v1 model to yield a 3x2 -array of activations for 1024 kernels at the top of the convolution. These are -averaged to give a 1024-dimension embedding, then put through a single logistic -layer to get the 521 per-class output scores corresponding to the 960 ms input -waveform segment. (Because of the window framing, you need at least 975 ms of -input waveform to get the first frame of output scores.) - -### Class vocabulary - -The file `yamnet_class_map.csv` describes the audio event classes associated -with each of the 521 outputs of the network. Its format is: - -```text -index,mid,display_name -``` - -where `index` is the model output index (0..520), `mid` is the machine -identifier for that class (e.g. `/m/09x0r`), and display_name is a -human-readable description of the class (e.g. `Speech`). - -The original Audioset data release had 527 classes. This model drops six of -them on the recommendation of our Fairness reviewers to avoid potentially -offensive mislabelings. We dropped the gendered versions (Male/Female) of -Speech and Singing. We also dropped Battle cry and Funny music. - -### Performance - -On the 20,366-segment AudioSet eval set, over the 521 included classes, the -balanced average d-prime is 2.318, balanced mAP is 0.306, and the balanced -average lwlrap is 0.393. - -According to our calculations, the classifier has 3.7M weights and performs -69.2M multiplies for each 960ms input frame. - -### Contact information - -This model repository is maintained by [Manoj Plakal](https://github.com/plakal) and [Dan Ellis](https://github.com/dpwe). diff --git a/research/audioset/yamnet/export.py b/research/audioset/yamnet/export.py deleted file mode 100644 index b04ac4bcf42..00000000000 --- a/research/audioset/yamnet/export.py +++ /dev/null @@ -1,214 +0,0 @@ -"""Exports YAMNet as: TF2 SavedModel, TF-Lite model, TF-JS model. - -The exported models all accept as input: -- 1-d float32 Tensor of arbitrary shape containing an audio waveform - (assumed to be mono 16 kHz samples in the [-1, +1] range) -and return as output: -- a 2-d float32 Tensor of shape [num_frames, num_classes] containing - predicted class scores for each frame of audio extracted from the input. -- a 2-d float32 Tensor of shape [num_frames, embedding_size] containing - embeddings of each frame of audio. -- a 2-d float32 Tensor of shape [num_spectrogram_frames, num_mel_bins] - containing the log mel spectrogram of the entire waveform. -The SavedModels will also contain (as an asset) a class map CSV file that maps -class indices to AudioSet class names and Freebase MIDs. The path to the class -map is available as the 'class_map_path()' method of the restored model. - -Requires pip-installing tensorflow_hub and tensorflowjs. - -Usage: - export.py -and the various exports will be created in subdirectories of the output directory. -Assumes that it will be run in the yamnet source directory from where it loads -the class map. Skips an export if the corresponding directory already exists. -""" - -import os -import sys -import tempfile -import time - -import numpy as np -import tensorflow as tf -assert tf.version.VERSION >= '2.0.0', ( - 'Need at least TF 2.0, you have TF v{}'.format(tf.version.VERSION)) -import tensorflow_hub as tfhub -from tensorflowjs.converters import tf_saved_model_conversion_v2 as tfjs_saved_model_converter - -import params as yamnet_params -import yamnet - - -def log(msg): - print('\n=====\n{} | {}\n=====\n'.format(time.asctime(), msg), flush=True) - - -class YAMNet(tf.Module): - """A TF2 Module wrapper around YAMNet.""" - def __init__(self, weights_path, params): - super().__init__() - self._yamnet = yamnet.yamnet_frames_model(params) - self._yamnet.load_weights(weights_path) - self._class_map_asset = tf.saved_model.Asset('yamnet_class_map.csv') - - @tf.function(input_signature=[]) - def class_map_path(self): - return self._class_map_asset.asset_path - - @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)]) - def __call__(self, waveform): - predictions, embeddings, log_mel_spectrogram = self._yamnet(waveform) - - return {'predictions': predictions, - 'embeddings': embeddings, - 'log_mel_spectrogram': log_mel_spectrogram} - - -def check_model(model_fn, class_map_path, params): - yamnet_classes = yamnet.class_names(class_map_path) - - """Applies yamnet_test's sanity checks to an instance of YAMNet.""" - def clip_test(waveform, expected_class_name, top_n=10): - results = model_fn(waveform=waveform) - predictions = results['predictions'] - embeddings = results['embeddings'] - log_mel_spectrogram = results['log_mel_spectrogram'] - clip_predictions = np.mean(predictions, axis=0) - top_n_indices = np.argsort(clip_predictions)[-top_n:] - top_n_scores = clip_predictions[top_n_indices] - top_n_class_names = yamnet_classes[top_n_indices] - top_n_predictions = list(zip(top_n_class_names, top_n_scores)) - assert expected_class_name in top_n_class_names, ( - 'Did not find expected class {} in top {} predictions: {}'.format( - expected_class_name, top_n, top_n_predictions)) - - clip_test( - waveform=np.zeros((int(3 * params.sample_rate),), dtype=np.float32), - expected_class_name='Silence') - - np.random.seed(51773) # Ensure repeatability. - clip_test( - waveform=np.random.uniform(-1.0, +1.0, - (int(3 * params.sample_rate),)).astype(np.float32), - expected_class_name='White noise') - - clip_test( - waveform=np.sin(2 * np.pi * 440 * - np.arange(0, 3, 1 / params.sample_rate), dtype=np.float32), - expected_class_name='Sine wave') - - -def make_tf2_export(weights_path, export_dir): - if os.path.exists(export_dir): - log('TF2 export already exists in {}, skipping TF2 export'.format( - export_dir)) - return - - # Create a TF2 Module wrapper around YAMNet. - log('Building and checking TF2 Module ...') - params = yamnet_params.Params() - yamnet = YAMNet(weights_path, params) - check_model(yamnet, yamnet.class_map_path(), params) - log('Done') - - # Make TF2 SavedModel export. - log('Making TF2 SavedModel export ...') - tf.saved_model.save( - yamnet, export_dir, - signatures={'serving_default': yamnet.__call__.get_concrete_function()}) - log('Done') - - # Check export with TF-Hub in TF2. - log('Checking TF2 SavedModel export in TF2 ...') - model = tfhub.load(export_dir) - check_model(model, model.class_map_path(), params) - log('Done') - - # Check export with TF-Hub in TF1. - log('Checking TF2 SavedModel export in TF1 ...') - with tf.compat.v1.Graph().as_default(), tf.compat.v1.Session() as sess: - model = tfhub.load(export_dir) - sess.run(tf.compat.v1.global_variables_initializer()) - def run_model(waveform): - return sess.run(model(waveform)) - check_model(run_model, model.class_map_path().eval(), params) - log('Done') - - -def make_tflite_export(weights_path, export_dir): - if os.path.exists(export_dir): - log('TF-Lite export already exists in {}, skipping TF-Lite export'.format( - export_dir)) - return - - # Create a TF-Lite compatible Module wrapper around YAMNet. - log('Building and checking TF-Lite Module ...') - params = yamnet_params.Params(tflite_compatible=True) - yamnet = YAMNet(weights_path, params) - check_model(yamnet, yamnet.class_map_path(), params) - log('Done') - - # Make TF-Lite SavedModel export. - log('Making TF-Lite SavedModel export ...') - saved_model_dir = os.path.join(export_dir, 'saved_model') - os.makedirs(saved_model_dir) - tf.saved_model.save( - yamnet, saved_model_dir, - signatures={'serving_default': yamnet.__call__.get_concrete_function()}) - log('Done') - - # Check that the export can be loaded and works. - log('Checking TF-Lite SavedModel export in TF2 ...') - model = tf.saved_model.load(saved_model_dir) - check_model(model, model.class_map_path(), params) - log('Done') - - # Make a TF-Lite model from the SavedModel. - log('Making TF-Lite model ...') - tflite_converter = tf.lite.TFLiteConverter.from_saved_model( - saved_model_dir, signature_keys=['serving_default']) - tflite_model = tflite_converter.convert() - tflite_model_path = os.path.join(export_dir, 'yamnet.tflite') - with open(tflite_model_path, 'wb') as f: - f.write(tflite_model) - log('Done') - - # Check the TF-Lite export. - log('Checking TF-Lite model ...') - interpreter = tf.lite.Interpreter(tflite_model_path) - runner = interpreter.get_signature_runner('serving_default') - check_model(runner, 'yamnet_class_map.csv', params) - log('Done') - - return saved_model_dir - - -def make_tfjs_export(tflite_saved_model_dir, export_dir): - if os.path.exists(export_dir): - log('TF-JS export already exists in {}, skipping TF-JS export'.format( - export_dir)) - return - - # Make a TF-JS model from the TF-Lite SavedModel export. - log('Making TF-JS model ...') - os.makedirs(export_dir) - tfjs_saved_model_converter.convert_tf_saved_model( - tflite_saved_model_dir, export_dir) - log('Done') - - -def main(args): - weights_path = args[0] - output_dir = args[1] - - tf2_export_dir = os.path.join(output_dir, 'tf2') - make_tf2_export(weights_path, tf2_export_dir) - - tflite_export_dir = os.path.join(output_dir, 'tflite') - tflite_saved_model_dir = make_tflite_export(weights_path, tflite_export_dir) - - tfjs_export_dir = os.path.join(output_dir, 'tfjs') - make_tfjs_export(tflite_saved_model_dir, tfjs_export_dir) - -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/research/audioset/yamnet/features.py b/research/audioset/yamnet/features.py deleted file mode 100644 index 9b1cf7775db..00000000000 --- a/research/audioset/yamnet/features.py +++ /dev/null @@ -1,165 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Feature computation for YAMNet.""" - -import numpy as np -import tensorflow as tf - - -def waveform_to_log_mel_spectrogram_patches(waveform, params): - """Compute log mel spectrogram patches of a 1-D waveform.""" - with tf.name_scope('log_mel_features'): - # waveform has shape [<# samples>] - - # Convert waveform into spectrogram using a Short-Time Fourier Transform. - # Note that tf.signal.stft() uses a periodic Hann window by default. - window_length_samples = int( - round(params.sample_rate * params.stft_window_seconds)) - hop_length_samples = int( - round(params.sample_rate * params.stft_hop_seconds)) - fft_length = 2 ** int(np.ceil(np.log(window_length_samples) / np.log(2.0))) - num_spectrogram_bins = fft_length // 2 + 1 - if params.tflite_compatible: - magnitude_spectrogram = _tflite_stft_magnitude( - signal=waveform, - frame_length=window_length_samples, - frame_step=hop_length_samples, - fft_length=fft_length) - else: - magnitude_spectrogram = tf.abs(tf.signal.stft( - signals=waveform, - frame_length=window_length_samples, - frame_step=hop_length_samples, - fft_length=fft_length)) - # magnitude_spectrogram has shape [<# STFT frames>, num_spectrogram_bins] - - # Convert spectrogram into log mel spectrogram. - linear_to_mel_weight_matrix = tf.signal.linear_to_mel_weight_matrix( - num_mel_bins=params.mel_bands, - num_spectrogram_bins=num_spectrogram_bins, - sample_rate=params.sample_rate, - lower_edge_hertz=params.mel_min_hz, - upper_edge_hertz=params.mel_max_hz) - mel_spectrogram = tf.matmul( - magnitude_spectrogram, linear_to_mel_weight_matrix) - log_mel_spectrogram = tf.math.log(mel_spectrogram + params.log_offset) - # log_mel_spectrogram has shape [<# STFT frames>, params.mel_bands] - - # Frame spectrogram (shape [<# STFT frames>, params.mel_bands]) into patches - # (the input examples). Only complete frames are emitted, so if there is - # less than params.patch_window_seconds of waveform then nothing is emitted - # (to avoid this, zero-pad before processing). - spectrogram_hop_length_samples = int( - round(params.sample_rate * params.stft_hop_seconds)) - spectrogram_sample_rate = params.sample_rate / spectrogram_hop_length_samples - patch_window_length_samples = int( - round(spectrogram_sample_rate * params.patch_window_seconds)) - patch_hop_length_samples = int( - round(spectrogram_sample_rate * params.patch_hop_seconds)) - features = tf.signal.frame( - signal=log_mel_spectrogram, - frame_length=patch_window_length_samples, - frame_step=patch_hop_length_samples, - axis=0) - # features has shape [<# patches>, <# STFT frames in an patch>, params.mel_bands] - - return log_mel_spectrogram, features - - -def pad_waveform(waveform, params): - """Pads waveform with silence if needed to get an integral number of patches.""" - # In order to produce one patch of log mel spectrogram input to YAMNet, we - # need at least one patch window length of waveform plus enough extra samples - # to complete the final STFT analysis window. - min_waveform_seconds = ( - params.patch_window_seconds + - params.stft_window_seconds - params.stft_hop_seconds) - min_num_samples = tf.cast(min_waveform_seconds * params.sample_rate, tf.int32) - num_samples = tf.shape(waveform)[0] - num_padding_samples = tf.maximum(0, min_num_samples - num_samples) - - # In addition, there might be enough waveform for one or more additional - # patches formed by hopping forward. If there are more samples than one patch, - # round up to an integral number of hops. - num_samples = tf.maximum(num_samples, min_num_samples) - num_samples_after_first_patch = num_samples - min_num_samples - hop_samples = tf.cast(params.patch_hop_seconds * params.sample_rate, tf.int32) - num_hops_after_first_patch = tf.cast(tf.math.ceil( - tf.cast(num_samples_after_first_patch, tf.float32) / - tf.cast(hop_samples, tf.float32)), tf.int32) - num_padding_samples += ( - hop_samples * num_hops_after_first_patch - num_samples_after_first_patch) - - padded_waveform = tf.pad(waveform, [[0, num_padding_samples]], - mode='CONSTANT', constant_values=0.0) - return padded_waveform - - -def _tflite_stft_magnitude(signal, frame_length, frame_step, fft_length): - """TF-Lite-compatible version of tf.abs(tf.signal.stft()).""" - def _hann_window(): - return tf.reshape( - tf.constant( - (0.5 - 0.5 * np.cos(2 * np.pi * np.arange(0, 1.0, 1.0 / frame_length)) - ).astype(np.float32), - name='hann_window'), [1, frame_length]) - - def _dft_matrix(dft_length): - """Calculate the full DFT matrix in NumPy.""" - # See https://en.wikipedia.org/wiki/DFT_matrix - omega = (0 + 1j) * 2.0 * np.pi / float(dft_length) - # Don't include 1/sqrt(N) scaling, tf.signal.rfft doesn't apply it. - return np.exp(omega * np.outer(np.arange(dft_length), np.arange(dft_length))) - - def _rdft(framed_signal, fft_length): - """Implement real-input Discrete Fourier Transform by matmul.""" - # We are right-multiplying by the DFT matrix, and we are keeping only the - # first half ("positive frequencies"). So discard the second half of rows, - # but transpose the array for right-multiplication. The DFT matrix is - # symmetric, so we could have done it more directly, but this reflects our - # intention better. - complex_dft_matrix_kept_values = _dft_matrix(fft_length)[:( - fft_length // 2 + 1), :].transpose() - real_dft_matrix = tf.constant( - np.real(complex_dft_matrix_kept_values).astype(np.float32), - name='real_dft_matrix') - imag_dft_matrix = tf.constant( - np.imag(complex_dft_matrix_kept_values).astype(np.float32), - name='imaginary_dft_matrix') - signal_frame_length = tf.shape(framed_signal)[-1] - half_pad = (fft_length - signal_frame_length) // 2 - padded_frames = tf.pad( - framed_signal, - [ - # Don't add any padding in the frame dimension. - [0, 0], - # Pad before and after the signal within each frame. - [half_pad, fft_length - signal_frame_length - half_pad] - ], - mode='CONSTANT', - constant_values=0.0) - real_stft = tf.matmul(padded_frames, real_dft_matrix) - imag_stft = tf.matmul(padded_frames, imag_dft_matrix) - return real_stft, imag_stft - - def _complex_abs(real, imag): - return tf.sqrt(tf.add(real * real, imag * imag)) - - framed_signal = tf.signal.frame(signal, frame_length, frame_step) - windowed_signal = framed_signal * _hann_window() - real_stft, imag_stft = _rdft(windowed_signal, fft_length) - stft_magnitude = _complex_abs(real_stft, imag_stft) - return stft_magnitude diff --git a/research/audioset/yamnet/inference.py b/research/audioset/yamnet/inference.py deleted file mode 100644 index 88509b0f0e2..00000000000 --- a/research/audioset/yamnet/inference.py +++ /dev/null @@ -1,64 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Inference demo for YAMNet.""" -from __future__ import division, print_function - -import sys - -import numpy as np -import resampy -import soundfile as sf -import tensorflow as tf - -import params as yamnet_params -import yamnet as yamnet_model - - -def main(argv): - assert argv, 'Usage: inference.py ...' - - params = yamnet_params.Params() - yamnet = yamnet_model.yamnet_frames_model(params) - yamnet.load_weights('yamnet.h5') - yamnet_classes = yamnet_model.class_names('yamnet_class_map.csv') - - for file_name in argv: - # Decode the WAV file. - wav_data, sr = sf.read(file_name, dtype=np.int16) - assert wav_data.dtype == np.int16, 'Bad sample type: %r' % wav_data.dtype - waveform = wav_data / 32768.0 # Convert to [-1.0, +1.0] - waveform = waveform.astype('float32') - - # Convert to mono and the sample rate expected by YAMNet. - if len(waveform.shape) > 1: - waveform = np.mean(waveform, axis=1) - if sr != params.sample_rate: - waveform = resampy.resample(waveform, sr, params.sample_rate) - - # Predict YAMNet classes. - scores, embeddings, spectrogram = yamnet(waveform) - # Scores is a matrix of (time_frames, num_classes) classifier scores. - # Average them along time to get an overall classifier output for the clip. - prediction = np.mean(scores, axis=0) - # Report the highest-scoring classes and their scores. - top5_i = np.argsort(prediction)[::-1][:5] - print(file_name, ':\n' + - '\n'.join(' {:12s}: {:.3f}'.format(yamnet_classes[i], prediction[i]) - for i in top5_i)) - - -if __name__ == '__main__': - main(sys.argv[1:]) diff --git a/research/audioset/yamnet/params.py b/research/audioset/yamnet/params.py deleted file mode 100644 index 306c94218d9..00000000000 --- a/research/audioset/yamnet/params.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Hyperparameters for YAMNet.""" - -from dataclasses import dataclass - -# The following hyperparameters (except patch_hop_seconds) were used to train YAMNet, -# so expect some variability in performance if you change these. The patch hop can -# be changed arbitrarily: a smaller hop should give you more patches from the same -# clip and possibly better performance at a larger computational cost. -@dataclass(frozen=True) # Instances of this class are immutable. -class Params: - sample_rate: float = 16000.0 - stft_window_seconds: float = 0.025 - stft_hop_seconds: float = 0.010 - mel_bands: int = 64 - mel_min_hz: float = 125.0 - mel_max_hz: float = 7500.0 - log_offset: float = 0.001 - patch_window_seconds: float = 0.96 - patch_hop_seconds: float = 0.48 - - @property - def patch_frames(self): - return int(round(self.patch_window_seconds / self.stft_hop_seconds)) - - @property - def patch_bands(self): - return self.mel_bands - - num_classes: int = 521 - conv_padding: str = 'same' - batchnorm_center: bool = True - batchnorm_scale: bool = False - batchnorm_epsilon: float = 1e-4 - classifier_activation: str = 'sigmoid' - - tflite_compatible: bool = False diff --git a/research/audioset/yamnet/yamnet.py b/research/audioset/yamnet/yamnet.py deleted file mode 100644 index cac7f87d99e..00000000000 --- a/research/audioset/yamnet/yamnet.py +++ /dev/null @@ -1,138 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Core model definition of YAMNet.""" - -import csv - -import numpy as np -import tensorflow as tf -from tensorflow.keras import Model, layers - -import features as features_lib - - -def _batch_norm(name, params): - def _bn_layer(layer_input): - return layers.BatchNormalization( - name=name, - center=params.batchnorm_center, - scale=params.batchnorm_scale, - epsilon=params.batchnorm_epsilon)(layer_input) - return _bn_layer - - -def _conv(name, kernel, stride, filters, params): - def _conv_layer(layer_input): - output = layers.Conv2D(name='{}/conv'.format(name), - filters=filters, - kernel_size=kernel, - strides=stride, - padding=params.conv_padding, - use_bias=False, - activation=None)(layer_input) - output = _batch_norm('{}/conv/bn'.format(name), params)(output) - output = layers.ReLU(name='{}/relu'.format(name))(output) - return output - return _conv_layer - - -def _separable_conv(name, kernel, stride, filters, params): - def _separable_conv_layer(layer_input): - output = layers.DepthwiseConv2D(name='{}/depthwise_conv'.format(name), - kernel_size=kernel, - strides=stride, - depth_multiplier=1, - padding=params.conv_padding, - use_bias=False, - activation=None)(layer_input) - output = _batch_norm('{}/depthwise_conv/bn'.format(name), params)(output) - output = layers.ReLU(name='{}/depthwise_conv/relu'.format(name))(output) - output = layers.Conv2D(name='{}/pointwise_conv'.format(name), - filters=filters, - kernel_size=(1, 1), - strides=1, - padding=params.conv_padding, - use_bias=False, - activation=None)(output) - output = _batch_norm('{}/pointwise_conv/bn'.format(name), params)(output) - output = layers.ReLU(name='{}/pointwise_conv/relu'.format(name))(output) - return output - return _separable_conv_layer - - -_YAMNET_LAYER_DEFS = [ - # (layer_function, kernel, stride, num_filters) - (_conv, [3, 3], 2, 32), - (_separable_conv, [3, 3], 1, 64), - (_separable_conv, [3, 3], 2, 128), - (_separable_conv, [3, 3], 1, 128), - (_separable_conv, [3, 3], 2, 256), - (_separable_conv, [3, 3], 1, 256), - (_separable_conv, [3, 3], 2, 512), - (_separable_conv, [3, 3], 1, 512), - (_separable_conv, [3, 3], 1, 512), - (_separable_conv, [3, 3], 1, 512), - (_separable_conv, [3, 3], 1, 512), - (_separable_conv, [3, 3], 1, 512), - (_separable_conv, [3, 3], 2, 1024), - (_separable_conv, [3, 3], 1, 1024) -] - - -def yamnet(features, params): - """Define the core YAMNet mode in Keras.""" - net = layers.Reshape( - (params.patch_frames, params.patch_bands, 1), - input_shape=(params.patch_frames, params.patch_bands))(features) - for (i, (layer_fun, kernel, stride, filters)) in enumerate(_YAMNET_LAYER_DEFS): - net = layer_fun('layer{}'.format(i + 1), kernel, stride, filters, params)(net) - embeddings = layers.GlobalAveragePooling2D()(net) - logits = layers.Dense(units=params.num_classes, use_bias=True)(embeddings) - predictions = layers.Activation(activation=params.classifier_activation)(logits) - return predictions, embeddings - - -def yamnet_frames_model(params): - """Defines the YAMNet waveform-to-class-scores model. - - Args: - params: An instance of Params containing hyperparameters. - - Returns: - A model accepting (num_samples,) waveform input and emitting: - - predictions: (num_patches, num_classes) matrix of class scores per time frame - - embeddings: (num_patches, embedding size) matrix of embeddings per time frame - - log_mel_spectrogram: (num_spectrogram_frames, num_mel_bins) spectrogram feature matrix - """ - waveform = layers.Input(batch_shape=(None,), dtype=tf.float32) - waveform_padded = features_lib.pad_waveform(waveform, params) - log_mel_spectrogram, features = features_lib.waveform_to_log_mel_spectrogram_patches( - waveform_padded, params) - predictions, embeddings = yamnet(features, params) - frames_model = Model( - name='yamnet_frames', inputs=waveform, - outputs=[predictions, embeddings, log_mel_spectrogram]) - return frames_model - - -def class_names(class_map_csv): - """Read the class name definition file and return a list of strings.""" - if tf.is_tensor(class_map_csv): - class_map_csv = class_map_csv.numpy() - with open(class_map_csv) as csv_file: - reader = csv.reader(csv_file) - next(reader) # Skip header - return np.array([display_name for (_, _, display_name) in reader]) diff --git a/research/audioset/yamnet/yamnet_class_map.csv b/research/audioset/yamnet/yamnet_class_map.csv deleted file mode 100644 index 9c07d818a6f..00000000000 --- a/research/audioset/yamnet/yamnet_class_map.csv +++ /dev/null @@ -1,522 +0,0 @@ -index,mid,display_name -0,/m/09x0r,Speech -1,/m/0ytgt,"Child speech, kid speaking" -2,/m/01h8n0,Conversation -3,/m/02qldy,"Narration, monologue" -4,/m/0261r1,Babbling -5,/m/0brhx,Speech synthesizer -6,/m/07p6fty,Shout -7,/m/07q4ntr,Bellow -8,/m/07rwj3x,Whoop -9,/m/07sr1lc,Yell -10,/t/dd00135,Children shouting -11,/m/03qc9zr,Screaming -12,/m/02rtxlg,Whispering -13,/m/01j3sz,Laughter -14,/t/dd00001,Baby laughter -15,/m/07r660_,Giggle -16,/m/07s04w4,Snicker -17,/m/07sq110,Belly laugh -18,/m/07rgt08,"Chuckle, chortle" -19,/m/0463cq4,"Crying, sobbing" -20,/t/dd00002,"Baby cry, infant cry" -21,/m/07qz6j3,Whimper -22,/m/07qw_06,"Wail, moan" -23,/m/07plz5l,Sigh -24,/m/015lz1,Singing -25,/m/0l14jd,Choir -26,/m/01swy6,Yodeling -27,/m/02bk07,Chant -28,/m/01c194,Mantra -29,/t/dd00005,Child singing -30,/t/dd00006,Synthetic singing -31,/m/06bxc,Rapping -32,/m/02fxyj,Humming -33,/m/07s2xch,Groan -34,/m/07r4k75,Grunt -35,/m/01w250,Whistling -36,/m/0lyf6,Breathing -37,/m/07mzm6,Wheeze -38,/m/01d3sd,Snoring -39,/m/07s0dtb,Gasp -40,/m/07pyy8b,Pant -41,/m/07q0yl5,Snort -42,/m/01b_21,Cough -43,/m/0dl9sf8,Throat clearing -44,/m/01hsr_,Sneeze -45,/m/07ppn3j,Sniff -46,/m/06h7j,Run -47,/m/07qv_x_,Shuffle -48,/m/07pbtc8,"Walk, footsteps" -49,/m/03cczk,"Chewing, mastication" -50,/m/07pdhp0,Biting -51,/m/0939n_,Gargling -52,/m/01g90h,Stomach rumble -53,/m/03q5_w,"Burping, eructation" -54,/m/02p3nc,Hiccup -55,/m/02_nn,Fart -56,/m/0k65p,Hands -57,/m/025_jnm,Finger snapping -58,/m/0l15bq,Clapping -59,/m/01jg02,"Heart sounds, heartbeat" -60,/m/01jg1z,Heart murmur -61,/m/053hz1,Cheering -62,/m/028ght,Applause -63,/m/07rkbfh,Chatter -64,/m/03qtwd,Crowd -65,/m/07qfr4h,"Hubbub, speech noise, speech babble" -66,/t/dd00013,Children playing -67,/m/0jbk,Animal -68,/m/068hy,"Domestic animals, pets" -69,/m/0bt9lr,Dog -70,/m/05tny_,Bark -71,/m/07r_k2n,Yip -72,/m/07qf0zm,Howl -73,/m/07rc7d9,Bow-wow -74,/m/0ghcn6,Growling -75,/t/dd00136,Whimper (dog) -76,/m/01yrx,Cat -77,/m/02yds9,Purr -78,/m/07qrkrw,Meow -79,/m/07rjwbb,Hiss -80,/m/07r81j2,Caterwaul -81,/m/0ch8v,"Livestock, farm animals, working animals" -82,/m/03k3r,Horse -83,/m/07rv9rh,Clip-clop -84,/m/07q5rw0,"Neigh, whinny" -85,/m/01xq0k1,"Cattle, bovinae" -86,/m/07rpkh9,Moo -87,/m/0239kh,Cowbell -88,/m/068zj,Pig -89,/t/dd00018,Oink -90,/m/03fwl,Goat -91,/m/07q0h5t,Bleat -92,/m/07bgp,Sheep -93,/m/025rv6n,Fowl -94,/m/09b5t,"Chicken, rooster" -95,/m/07st89h,Cluck -96,/m/07qn5dc,"Crowing, cock-a-doodle-doo" -97,/m/01rd7k,Turkey -98,/m/07svc2k,Gobble -99,/m/09ddx,Duck -100,/m/07qdb04,Quack -101,/m/0dbvp,Goose -102,/m/07qwf61,Honk -103,/m/01280g,Wild animals -104,/m/0cdnk,"Roaring cats (lions, tigers)" -105,/m/04cvmfc,Roar -106,/m/015p6,Bird -107,/m/020bb7,"Bird vocalization, bird call, bird song" -108,/m/07pggtn,"Chirp, tweet" -109,/m/07sx8x_,Squawk -110,/m/0h0rv,"Pigeon, dove" -111,/m/07r_25d,Coo -112,/m/04s8yn,Crow -113,/m/07r5c2p,Caw -114,/m/09d5_,Owl -115,/m/07r_80w,Hoot -116,/m/05_wcq,"Bird flight, flapping wings" -117,/m/01z5f,"Canidae, dogs, wolves" -118,/m/06hps,"Rodents, rats, mice" -119,/m/04rmv,Mouse -120,/m/07r4gkf,Patter -121,/m/03vt0,Insect -122,/m/09xqv,Cricket -123,/m/09f96,Mosquito -124,/m/0h2mp,"Fly, housefly" -125,/m/07pjwq1,Buzz -126,/m/01h3n,"Bee, wasp, etc." -127,/m/09ld4,Frog -128,/m/07st88b,Croak -129,/m/078jl,Snake -130,/m/07qn4z3,Rattle -131,/m/032n05,Whale vocalization -132,/m/04rlf,Music -133,/m/04szw,Musical instrument -134,/m/0fx80y,Plucked string instrument -135,/m/0342h,Guitar -136,/m/02sgy,Electric guitar -137,/m/018vs,Bass guitar -138,/m/042v_gx,Acoustic guitar -139,/m/06w87,"Steel guitar, slide guitar" -140,/m/01glhc,Tapping (guitar technique) -141,/m/07s0s5r,Strum -142,/m/018j2,Banjo -143,/m/0jtg0,Sitar -144,/m/04rzd,Mandolin -145,/m/01bns_,Zither -146,/m/07xzm,Ukulele -147,/m/05148p4,Keyboard (musical) -148,/m/05r5c,Piano -149,/m/01s0ps,Electric piano -150,/m/013y1f,Organ -151,/m/03xq_f,Electronic organ -152,/m/03gvt,Hammond organ -153,/m/0l14qv,Synthesizer -154,/m/01v1d8,Sampler -155,/m/03q5t,Harpsichord -156,/m/0l14md,Percussion -157,/m/02hnl,Drum kit -158,/m/0cfdd,Drum machine -159,/m/026t6,Drum -160,/m/06rvn,Snare drum -161,/m/03t3fj,Rimshot -162,/m/02k_mr,Drum roll -163,/m/0bm02,Bass drum -164,/m/011k_j,Timpani -165,/m/01p970,Tabla -166,/m/01qbl,Cymbal -167,/m/03qtq,Hi-hat -168,/m/01sm1g,Wood block -169,/m/07brj,Tambourine -170,/m/05r5wn,Rattle (instrument) -171,/m/0xzly,Maraca -172,/m/0mbct,Gong -173,/m/016622,Tubular bells -174,/m/0j45pbj,Mallet percussion -175,/m/0dwsp,"Marimba, xylophone" -176,/m/0dwtp,Glockenspiel -177,/m/0dwt5,Vibraphone -178,/m/0l156b,Steelpan -179,/m/05pd6,Orchestra -180,/m/01kcd,Brass instrument -181,/m/0319l,French horn -182,/m/07gql,Trumpet -183,/m/07c6l,Trombone -184,/m/0l14_3,Bowed string instrument -185,/m/02qmj0d,String section -186,/m/07y_7,"Violin, fiddle" -187,/m/0d8_n,Pizzicato -188,/m/01xqw,Cello -189,/m/02fsn,Double bass -190,/m/085jw,"Wind instrument, woodwind instrument" -191,/m/0l14j_,Flute -192,/m/06ncr,Saxophone -193,/m/01wy6,Clarinet -194,/m/03m5k,Harp -195,/m/0395lw,Bell -196,/m/03w41f,Church bell -197,/m/027m70_,Jingle bell -198,/m/0gy1t2s,Bicycle bell -199,/m/07n_g,Tuning fork -200,/m/0f8s22,Chime -201,/m/026fgl,Wind chime -202,/m/0150b9,Change ringing (campanology) -203,/m/03qjg,Harmonica -204,/m/0mkg,Accordion -205,/m/0192l,Bagpipes -206,/m/02bxd,Didgeridoo -207,/m/0l14l2,Shofar -208,/m/07kc_,Theremin -209,/m/0l14t7,Singing bowl -210,/m/01hgjl,Scratching (performance technique) -211,/m/064t9,Pop music -212,/m/0glt670,Hip hop music -213,/m/02cz_7,Beatboxing -214,/m/06by7,Rock music -215,/m/03lty,Heavy metal -216,/m/05r6t,Punk rock -217,/m/0dls3,Grunge -218,/m/0dl5d,Progressive rock -219,/m/07sbbz2,Rock and roll -220,/m/05w3f,Psychedelic rock -221,/m/06j6l,Rhythm and blues -222,/m/0gywn,Soul music -223,/m/06cqb,Reggae -224,/m/01lyv,Country -225,/m/015y_n,Swing music -226,/m/0gg8l,Bluegrass -227,/m/02x8m,Funk -228,/m/02w4v,Folk music -229,/m/06j64v,Middle Eastern music -230,/m/03_d0,Jazz -231,/m/026z9,Disco -232,/m/0ggq0m,Classical music -233,/m/05lls,Opera -234,/m/02lkt,Electronic music -235,/m/03mb9,House music -236,/m/07gxw,Techno -237,/m/07s72n,Dubstep -238,/m/0283d,Drum and bass -239,/m/0m0jc,Electronica -240,/m/08cyft,Electronic dance music -241,/m/0fd3y,Ambient music -242,/m/07lnk,Trance music -243,/m/0g293,Music of Latin America -244,/m/0ln16,Salsa music -245,/m/0326g,Flamenco -246,/m/0155w,Blues -247,/m/05fw6t,Music for children -248,/m/02v2lh,New-age music -249,/m/0y4f8,Vocal music -250,/m/0z9c,A capella -251,/m/0164x2,Music of Africa -252,/m/0145m,Afrobeat -253,/m/02mscn,Christian music -254,/m/016cjb,Gospel music -255,/m/028sqc,Music of Asia -256,/m/015vgc,Carnatic music -257,/m/0dq0md,Music of Bollywood -258,/m/06rqw,Ska -259,/m/02p0sh1,Traditional music -260,/m/05rwpb,Independent music -261,/m/074ft,Song -262,/m/025td0t,Background music -263,/m/02cjck,Theme music -264,/m/03r5q_,Jingle (music) -265,/m/0l14gg,Soundtrack music -266,/m/07pkxdp,Lullaby -267,/m/01z7dr,Video game music -268,/m/0140xf,Christmas music -269,/m/0ggx5q,Dance music -270,/m/04wptg,Wedding music -271,/t/dd00031,Happy music -272,/t/dd00033,Sad music -273,/t/dd00034,Tender music -274,/t/dd00035,Exciting music -275,/t/dd00036,Angry music -276,/t/dd00037,Scary music -277,/m/03m9d0z,Wind -278,/m/09t49,Rustling leaves -279,/t/dd00092,Wind noise (microphone) -280,/m/0jb2l,Thunderstorm -281,/m/0ngt1,Thunder -282,/m/0838f,Water -283,/m/06mb1,Rain -284,/m/07r10fb,Raindrop -285,/t/dd00038,Rain on surface -286,/m/0j6m2,Stream -287,/m/0j2kx,Waterfall -288,/m/05kq4,Ocean -289,/m/034srq,"Waves, surf" -290,/m/06wzb,Steam -291,/m/07swgks,Gurgling -292,/m/02_41,Fire -293,/m/07pzfmf,Crackle -294,/m/07yv9,Vehicle -295,/m/019jd,"Boat, Water vehicle" -296,/m/0hsrw,"Sailboat, sailing ship" -297,/m/056ks2,"Rowboat, canoe, kayak" -298,/m/02rlv9,"Motorboat, speedboat" -299,/m/06q74,Ship -300,/m/012f08,Motor vehicle (road) -301,/m/0k4j,Car -302,/m/0912c9,"Vehicle horn, car horn, honking" -303,/m/07qv_d5,Toot -304,/m/02mfyn,Car alarm -305,/m/04gxbd,"Power windows, electric windows" -306,/m/07rknqz,Skidding -307,/m/0h9mv,Tire squeal -308,/t/dd00134,Car passing by -309,/m/0ltv,"Race car, auto racing" -310,/m/07r04,Truck -311,/m/0gvgw0,Air brake -312,/m/05x_td,"Air horn, truck horn" -313,/m/02rhddq,Reversing beeps -314,/m/03cl9h,"Ice cream truck, ice cream van" -315,/m/01bjv,Bus -316,/m/03j1ly,Emergency vehicle -317,/m/04qvtq,Police car (siren) -318,/m/012n7d,Ambulance (siren) -319,/m/012ndj,"Fire engine, fire truck (siren)" -320,/m/04_sv,Motorcycle -321,/m/0btp2,"Traffic noise, roadway noise" -322,/m/06d_3,Rail transport -323,/m/07jdr,Train -324,/m/04zmvq,Train whistle -325,/m/0284vy3,Train horn -326,/m/01g50p,"Railroad car, train wagon" -327,/t/dd00048,Train wheels squealing -328,/m/0195fx,"Subway, metro, underground" -329,/m/0k5j,Aircraft -330,/m/014yck,Aircraft engine -331,/m/04229,Jet engine -332,/m/02l6bg,"Propeller, airscrew" -333,/m/09ct_,Helicopter -334,/m/0cmf2,"Fixed-wing aircraft, airplane" -335,/m/0199g,Bicycle -336,/m/06_fw,Skateboard -337,/m/02mk9,Engine -338,/t/dd00065,Light engine (high frequency) -339,/m/08j51y,"Dental drill, dentist's drill" -340,/m/01yg9g,Lawn mower -341,/m/01j4z9,Chainsaw -342,/t/dd00066,Medium engine (mid frequency) -343,/t/dd00067,Heavy engine (low frequency) -344,/m/01h82_,Engine knocking -345,/t/dd00130,Engine starting -346,/m/07pb8fc,Idling -347,/m/07q2z82,"Accelerating, revving, vroom" -348,/m/02dgv,Door -349,/m/03wwcy,Doorbell -350,/m/07r67yg,Ding-dong -351,/m/02y_763,Sliding door -352,/m/07rjzl8,Slam -353,/m/07r4wb8,Knock -354,/m/07qcpgn,Tap -355,/m/07q6cd_,Squeak -356,/m/0642b4,Cupboard open or close -357,/m/0fqfqc,Drawer open or close -358,/m/04brg2,"Dishes, pots, and pans" -359,/m/023pjk,"Cutlery, silverware" -360,/m/07pn_8q,Chopping (food) -361,/m/0dxrf,Frying (food) -362,/m/0fx9l,Microwave oven -363,/m/02pjr4,Blender -364,/m/02jz0l,"Water tap, faucet" -365,/m/0130jx,Sink (filling or washing) -366,/m/03dnzn,Bathtub (filling or washing) -367,/m/03wvsk,Hair dryer -368,/m/01jt3m,Toilet flush -369,/m/012xff,Toothbrush -370,/m/04fgwm,Electric toothbrush -371,/m/0d31p,Vacuum cleaner -372,/m/01s0vc,Zipper (clothing) -373,/m/03v3yw,Keys jangling -374,/m/0242l,Coin (dropping) -375,/m/01lsmm,Scissors -376,/m/02g901,"Electric shaver, electric razor" -377,/m/05rj2,Shuffling cards -378,/m/0316dw,Typing -379,/m/0c2wf,Typewriter -380,/m/01m2v,Computer keyboard -381,/m/081rb,Writing -382,/m/07pp_mv,Alarm -383,/m/07cx4,Telephone -384,/m/07pp8cl,Telephone bell ringing -385,/m/01hnzm,Ringtone -386,/m/02c8p,"Telephone dialing, DTMF" -387,/m/015jpf,Dial tone -388,/m/01z47d,Busy signal -389,/m/046dlr,Alarm clock -390,/m/03kmc9,Siren -391,/m/0dgbq,Civil defense siren -392,/m/030rvx,Buzzer -393,/m/01y3hg,"Smoke detector, smoke alarm" -394,/m/0c3f7m,Fire alarm -395,/m/04fq5q,Foghorn -396,/m/0l156k,Whistle -397,/m/06hck5,Steam whistle -398,/t/dd00077,Mechanisms -399,/m/02bm9n,"Ratchet, pawl" -400,/m/01x3z,Clock -401,/m/07qjznt,Tick -402,/m/07qjznl,Tick-tock -403,/m/0l7xg,Gears -404,/m/05zc1,Pulleys -405,/m/0llzx,Sewing machine -406,/m/02x984l,Mechanical fan -407,/m/025wky1,Air conditioning -408,/m/024dl,Cash register -409,/m/01m4t,Printer -410,/m/0dv5r,Camera -411,/m/07bjf,Single-lens reflex camera -412,/m/07k1x,Tools -413,/m/03l9g,Hammer -414,/m/03p19w,Jackhammer -415,/m/01b82r,Sawing -416,/m/02p01q,Filing (rasp) -417,/m/023vsd,Sanding -418,/m/0_ksk,Power tool -419,/m/01d380,Drill -420,/m/014zdl,Explosion -421,/m/032s66,"Gunshot, gunfire" -422,/m/04zjc,Machine gun -423,/m/02z32qm,Fusillade -424,/m/0_1c,Artillery fire -425,/m/073cg4,Cap gun -426,/m/0g6b5,Fireworks -427,/g/122z_qxw,Firecracker -428,/m/07qsvvw,"Burst, pop" -429,/m/07pxg6y,Eruption -430,/m/07qqyl4,Boom -431,/m/083vt,Wood -432,/m/07pczhz,Chop -433,/m/07pl1bw,Splinter -434,/m/07qs1cx,Crack -435,/m/039jq,Glass -436,/m/07q7njn,"Chink, clink" -437,/m/07rn7sz,Shatter -438,/m/04k94,Liquid -439,/m/07rrlb6,"Splash, splatter" -440,/m/07p6mqd,Slosh -441,/m/07qlwh6,Squish -442,/m/07r5v4s,Drip -443,/m/07prgkl,Pour -444,/m/07pqc89,"Trickle, dribble" -445,/t/dd00088,Gush -446,/m/07p7b8y,Fill (with liquid) -447,/m/07qlf79,Spray -448,/m/07ptzwd,Pump (liquid) -449,/m/07ptfmf,Stir -450,/m/0dv3j,Boiling -451,/m/0790c,Sonar -452,/m/0dl83,Arrow -453,/m/07rqsjt,"Whoosh, swoosh, swish" -454,/m/07qnq_y,"Thump, thud" -455,/m/07rrh0c,Thunk -456,/m/0b_fwt,Electronic tuner -457,/m/02rr_,Effects unit -458,/m/07m2kt,Chorus effect -459,/m/018w8,Basketball bounce -460,/m/07pws3f,Bang -461,/m/07ryjzk,"Slap, smack" -462,/m/07rdhzs,"Whack, thwack" -463,/m/07pjjrj,"Smash, crash" -464,/m/07pc8lb,Breaking -465,/m/07pqn27,Bouncing -466,/m/07rbp7_,Whip -467,/m/07pyf11,Flap -468,/m/07qb_dv,Scratch -469,/m/07qv4k0,Scrape -470,/m/07pdjhy,Rub -471,/m/07s8j8t,Roll -472,/m/07plct2,Crushing -473,/t/dd00112,"Crumpling, crinkling" -474,/m/07qcx4z,Tearing -475,/m/02fs_r,"Beep, bleep" -476,/m/07qwdck,Ping -477,/m/07phxs1,Ding -478,/m/07rv4dm,Clang -479,/m/07s02z0,Squeal -480,/m/07qh7jl,Creak -481,/m/07qwyj0,Rustle -482,/m/07s34ls,Whir -483,/m/07qmpdm,Clatter -484,/m/07p9k1k,Sizzle -485,/m/07qc9xj,Clicking -486,/m/07rwm0c,Clickety-clack -487,/m/07phhsh,Rumble -488,/m/07qyrcz,Plop -489,/m/07qfgpx,"Jingle, tinkle" -490,/m/07rcgpl,Hum -491,/m/07p78v5,Zing -492,/t/dd00121,Boing -493,/m/07s12q4,Crunch -494,/m/028v0c,Silence -495,/m/01v_m0,Sine wave -496,/m/0b9m1,Harmonic -497,/m/0hdsk,Chirp tone -498,/m/0c1dj,Sound effect -499,/m/07pt_g0,Pulse -500,/t/dd00125,"Inside, small room" -501,/t/dd00126,"Inside, large room or hall" -502,/t/dd00127,"Inside, public space" -503,/t/dd00128,"Outside, urban or manmade" -504,/t/dd00129,"Outside, rural or natural" -505,/m/01b9nn,Reverberation -506,/m/01jnbd,Echo -507,/m/096m7z,Noise -508,/m/06_y0by,Environmental noise -509,/m/07rgkc5,Static -510,/m/06xkwv,Mains hum -511,/m/0g12c5,Distortion -512,/m/08p9q4,Sidetone -513,/m/07szfh9,Cacophony -514,/m/0chx_,White noise -515,/m/0cj0r,Pink noise -516,/m/07p_0gm,Throbbing -517,/m/01jwx6,Vibration -518,/m/07c52,Television -519,/m/06bz3,Radio -520,/m/07hvw1,Field recording diff --git a/research/audioset/yamnet/yamnet_test.py b/research/audioset/yamnet/yamnet_test.py deleted file mode 100644 index d0d16da8082..00000000000 --- a/research/audioset/yamnet/yamnet_test.py +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Installation test for YAMNet.""" - -import numpy as np -import tensorflow as tf - -import params -import yamnet - -class YAMNetTest(tf.test.TestCase): - - _params = None - _yamnet = None - _yamnet_classes = None - - @classmethod - def setUpClass(cls): - super().setUpClass() - cls._params = params.Params() - cls._yamnet = yamnet.yamnet_frames_model(cls._params) - cls._yamnet.load_weights('yamnet.h5') - cls._yamnet_classes = yamnet.class_names('yamnet_class_map.csv') - - def clip_test(self, waveform, expected_class_name, top_n=10): - """Run the model on the waveform, check that expected class is in top-n.""" - predictions, embeddings, log_mel_spectrogram = YAMNetTest._yamnet(waveform) - clip_predictions = np.mean(predictions, axis=0) - top_n_indices = np.argsort(clip_predictions)[-top_n:] - top_n_scores = clip_predictions[top_n_indices] - top_n_class_names = YAMNetTest._yamnet_classes[top_n_indices] - top_n_predictions = list(zip(top_n_class_names, top_n_scores)) - self.assertIn(expected_class_name, top_n_class_names, - 'Did not find expected class {} in top {} predictions: {}'.format( - expected_class_name, top_n, top_n_predictions)) - - def testZeros(self): - self.clip_test( - waveform=np.zeros((int(3 * YAMNetTest._params.sample_rate),)), - expected_class_name='Silence') - - def testRandom(self): - np.random.seed(51773) # Ensure repeatability. - self.clip_test( - waveform=np.random.uniform(-1.0, +1.0, - (int(3 * YAMNetTest._params.sample_rate),)), - expected_class_name='White noise') - - def testSine(self): - self.clip_test( - waveform=np.sin(2 * np.pi * 440 * - np.arange(0, 3, 1 / YAMNetTest._params.sample_rate)), - expected_class_name='Sine wave') - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/audioset/yamnet/yamnet_visualization.ipynb b/research/audioset/yamnet/yamnet_visualization.ipynb deleted file mode 100644 index db08acfbc98..00000000000 --- a/research/audioset/yamnet/yamnet_visualization.ipynb +++ /dev/null @@ -1,274 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "kernelspec": { - "display_name": "Python 3", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.7.6" - }, - "colab": { - "name": "yamnet_visualization.ipynb", - "provenance": [] - } - }, - "cells": [ - { - "cell_type": "code", - "metadata": { - "id": "xcZmKVHusxQT" - }, - "source": [ - "# Copyright 2019 The TensorFlow Authors All Rights Reserved.\n", - "#\n", - "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# http://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", - "# See the License for the specific language governing permissions and\n", - "# limitations under the License.\n", - "# ==============================================================================" - ], - "execution_count": 1, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "0sDpjNbksxQa" - }, - "source": [ - "# Visualization of the YAMNet audio event classification model.\n", - "# See https://github.com/tensorflow/models/tree/master/research/audioset/yamnet/\n", - "#\n", - "# This notebook can be run in Google Colab at https://colab.research.google.com\n", - "# by either downloading this ipynb and uploading it, or by looking up the\n", - "# notebook directly on GitHub in Colab's \"Open notebook\" dialog." - ], - "execution_count": 2, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "hnI3jFRHs-N7", - "outputId": "24b5696f-e4cb-4d49-bddc-40ab3ef211b9", - "colab": { - "base_uri": "https://localhost:8080/" - } - }, - "source": [ - "# Install required packages.\n", - "!pip install soundfile\n", - "!git clone https://github.com/tensorflow/models.git\n", - "%cd models/research/audioset/yamnet\n", - "\n", - "# Download YAMNet data\n", - "!curl -O https://storage.googleapis.com/audioset/yamnet.h5\n", - "\n", - "# Download audio for testing\n", - "!curl -O https://storage.googleapis.com/audioset/speech_whistling2.wav\n", - "\n", - "!ls -l" - ], - "execution_count": 3, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Collecting soundfile\n", - " Downloading https://files.pythonhosted.org/packages/eb/f2/3cbbbf3b96fb9fa91582c438b574cff3f45b29c772f94c400e2c99ef5db9/SoundFile-0.10.3.post1-py2.py3-none-any.whl\n", - "Requirement already satisfied: cffi>=1.0 in /usr/local/lib/python3.6/dist-packages (from soundfile) (1.14.3)\n", - "Requirement already satisfied: pycparser in /usr/local/lib/python3.6/dist-packages (from cffi>=1.0->soundfile) (2.20)\n", - "Installing collected packages: soundfile\n", - "Successfully installed soundfile-0.10.3.post1\n", - "Cloning into 'models'...\n", - "remote: Enumerating objects: 67, done.\u001b[K\n", - "remote: Counting objects: 100% (67/67), done.\u001b[K\n", - "remote: Compressing objects: 100% (65/65), done.\u001b[K\n", - "remote: Total 46144 (delta 26), reused 43 (delta 2), pack-reused 46077\u001b[K\n", - "Receiving objects: 100% (46144/46144), 551.17 MiB | 32.01 MiB/s, done.\n", - "Resolving deltas: 100% (31621/31621), done.\n", - "/content/models/research/audioset/yamnet\n", - " % Total % Received % Xferd Average Speed Time Time Time Current\n", - " Dload Upload Total Spent Left Speed\n", - "100 14.5M 100 14.5M 0 0 17.0M 0 --:--:-- --:--:-- --:--:-- 17.0M\n", - " % Total % Received % Xferd Average Speed Time Time Time Current\n", - " Dload Upload Total Spent Left Speed\n", - "100 153k 100 153k 0 0 1314k 0 --:--:-- --:--:-- --:--:-- 1314k\n", - "total 15296\n", - "-rw-r--r-- 1 root root 7816 Oct 22 17:31 export.py\n", - "-rw-r--r-- 1 root root 7490 Oct 22 17:31 features.py\n", - "-rw-r--r-- 1 root root 2307 Oct 22 17:31 inference.py\n", - "-rw-r--r-- 1 root root 1847 Oct 22 17:31 params.py\n", - "-rw-r--r-- 1 root root 5012 Oct 22 17:31 README.md\n", - "-rw-r--r-- 1 root root 157484 Oct 22 17:31 speech_whistling2.wav\n", - "-rw-r--r-- 1 root root 14096 Oct 22 17:31 yamnet_class_map.csv\n", - "-rw-r--r-- 1 root root 15296092 Oct 22 17:31 yamnet.h5\n", - "-rw-r--r-- 1 root root 5549 Oct 22 17:31 yamnet.py\n", - "-rw-r--r-- 1 root root 2564 Oct 22 17:31 yamnet_test.py\n", - "-rw-r--r-- 1 root root 140923 Oct 22 17:31 yamnet_visualization.ipynb\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "M0woGtbhsxQg" - }, - "source": [ - "# Imports.\n", - "import numpy as np\n", - "import soundfile as sf\n", - "\n", - "import matplotlib.pyplot as plt\n", - "\n", - "import params as yamnet_params\n", - "import yamnet as yamnet_model\n", - "import tensorflow as tf" - ], - "execution_count": 4, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "jt2v3i94sxQl" - }, - "source": [ - "# Read in the audio.\n", - "wav_file_name = 'speech_whistling2.wav'\n", - "wav_data, sr = sf.read(wav_file_name, dtype=np.int16)\n", - "waveform = wav_data / 32768.0" - ], - "execution_count": 5, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "HiLKl_rVUqHJ", - "outputId": "1a7c0344-6dd0-4228-9e62-611ad8847f84", - "colab": { - "base_uri": "https://localhost:8080/" - } - }, - "source": [ - "# The graph is designed for a sampling rate of 16 kHz, but higher rates should work too.\n", - "# We also generate scores at a 10 Hz frame rate.\n", - "params = yamnet_params.Params(sample_rate=sr, patch_hop_seconds=0.1)\n", - "print(\"Sample rate =\", params.sample_rate)" - ], - "execution_count": 6, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Sample rate = 16000\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "bHNJU9JUsxQs" - }, - "source": [ - "# Set up the YAMNet model.\n", - "class_names = yamnet_model.class_names('yamnet_class_map.csv')\n", - "yamnet = yamnet_model.yamnet_frames_model(params)\n", - "yamnet.load_weights('yamnet.h5')" - ], - "execution_count": 7, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "XCrhG2WrsxQx" - }, - "source": [ - "# Run the model.\n", - "scores, embeddings, spectrogram = yamnet(waveform)\n", - "scores = scores.numpy()\n", - "spectrogram = spectrogram.numpy()" - ], - "execution_count": 8, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "XN67xLQasxQ2", - "outputId": "9b0744bd-bc2f-4996-c9d9-c7ad2e08725a", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 483 - } - }, - "source": [ - "# Visualize the results.\n", - "plt.figure(figsize=(10, 8))\n", - "\n", - "# Plot the waveform.\n", - "plt.subplot(3, 1, 1)\n", - "plt.plot(waveform)\n", - "plt.xlim([0, len(waveform)])\n", - "# Plot the log-mel spectrogram (returned by the model).\n", - "plt.subplot(3, 1, 2)\n", - "plt.imshow(spectrogram.T, aspect='auto', interpolation='nearest', origin='bottom')\n", - "\n", - "# Plot and label the model output scores for the top-scoring classes.\n", - "mean_scores = np.mean(scores, axis=0)\n", - "top_N = 10\n", - "top_class_indices = np.argsort(mean_scores)[::-1][:top_N]\n", - "plt.subplot(3, 1, 3)\n", - "plt.imshow(scores[:, top_class_indices].T, aspect='auto', interpolation='nearest', cmap='gray_r')\n", - "# Compensate for the patch_window_seconds (0.96s) context window to align with spectrogram.\n", - "patch_padding = (params.patch_window_seconds / 2) / params.patch_hop_seconds\n", - "plt.xlim([-patch_padding, scores.shape[0] + patch_padding])\n", - "# Label the top_N classes.\n", - "yticks = range(0, top_N, 1)\n", - "plt.yticks(yticks, [class_names[top_class_indices[x]] for x in yticks])\n", - "_ = plt.ylim(-0.5 + np.array([top_N, 0]))\n" - ], - "execution_count": 9, - "outputs": [ - { - "output_type": "display_data", - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAArgAAAHSCAYAAAAHR7iOAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nOzdd5wcdd3A8c93ZtvVXBrpIYGEEkggEHqVDlEQC4IFUJHHR1F5eBRDFZES7GJ5BEEUVATBAoYiobcAoYWEAOmk9+Tqtpnf88fM3e1dbu9297bd3ff9yr2yO/U3v92Z/c5vfkWMMSillFJKKdVfWKVOgFJKKaWUUvmkAa5SSimllOpXNMBVSimllFL9iga4SimllFKqX9EAVymllFJK9Ssa4CqllFJKqX4lUOoEqPIxbNgwM2HChFInQymllFKqR6+//voWY8zwruZpgKvaTJgwgfnz55c6GUoppZRSPRKRVenmaRUFpZRSSinVr2iAq5RSSiml+hUNcJVSSimlVL+iAa5SWWiKJZkwaw73v7a61ElRSimlVBoa4CqVhQ31UQB+++yyEqdEKaWUUulogKuUUkoppfoVDXDLnIicJiLvi8hSEZnVxfwLRWSziLzl/12UMu8CEVni/11Q3JQrpZRSSpWG9oNbxkTEBn4NnAysAV4TkYeMMe92WvQ+Y8wlndYdAnwPmAEY4HV/3e1FSLpSSimlVMloCW55OxRYaoxZboyJA38Fzspw3VOBJ4wx2/yg9gngtAKlc8BavL6eC+96lVjSKXVSlFJKKeXTALe8jQFSm+uv8ad19kkRWSAiD4jIuGzWFZGLRWS+iMzfvHlzvtLdbxnT8f2sBxfwzPubefPDHaVJkFJKKaV2oQFu3/cwMMEYMw2vlPaP2axsjLndGDPDGDNj+PAuh3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/74G3GH7szW70ncdTlKPpMnl9XbAq6nuob4OeEuplr7rMLY/bHtX21Op/k/7qe1ZpN8aSqKH6FrlU+4FwDjgYtufbHNI0YCkbwEzgJ2AB4GPA1cC3wV2B34FvM12/YNo0UaSjgDmA8t45n7Aj1Ddh5u+62CS9qN6GGkc1UTXd21/QtIeVA/k7gAsBk6wva59kcZQJM0AzrR9TPqtsQxwIyIiIqKr5BaFiIiIiOgqGeBGRERERFfJADciIiIiukoGuBERERHRVTLAjYiIiIiukgFuRERERHSVDHAjIiIioqtkgBsRERERXeX/AT5DbvTc/nXuAAAAAElFTkSuQmCC\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "tags": [], - "needs_background": "light" - } - } - ] - } - ] -} \ No newline at end of file diff --git a/research/autoaugment/README.md b/research/autoaugment/README.md deleted file mode 100644 index a26f4872cc3..00000000000 --- a/research/autoaugment/README.md +++ /dev/null @@ -1,70 +0,0 @@ -![TensorFlow Requirement: 1.x](https://img.shields.io/badge/TensorFlow%20Requirement-1.x-brightgreen) -![TensorFlow 2 Not Supported](https://img.shields.io/badge/TensorFlow%202%20Not%20Supported-%E2%9C%95-red.svg) - -Train Wide-ResNet, Shake-Shake and ShakeDrop models on CIFAR-10 -and CIFAR-100 dataset with AutoAugment. - -The CIFAR-10/CIFAR-100 data can be downloaded from: -https://www.cs.toronto.edu/~kriz/cifar.html. Use the Python version instead of the binary version. - -The code replicates the results from Tables 1 and 2 on CIFAR-10/100 with the -following models: Wide-ResNet-28-10, Shake-Shake (26 2x32d), Shake-Shake (26 -2x96d) and PyramidNet+ShakeDrop. - -Related papers: - -AutoAugment: Learning Augmentation Policies from Data - -https://arxiv.org/abs/1805.09501 - -Wide Residual Networks - -https://arxiv.org/abs/1605.07146 - -Shake-Shake regularization - -https://arxiv.org/abs/1705.07485 - -ShakeDrop regularization - -https://arxiv.org/abs/1802.02375 - -Settings: - -CIFAR-10 Model | Learning Rate | Weight Decay | Num. Epochs | Batch Size ----------------------- | ------------- | ------------ | ----------- | ---------- -Wide-ResNet-28-10 | 0.1 | 5e-4 | 200 | 128 -Shake-Shake (26 2x32d) | 0.01 | 1e-3 | 1800 | 128 -Shake-Shake (26 2x96d) | 0.01 | 1e-3 | 1800 | 128 -PyramidNet + ShakeDrop | 0.05 | 5e-5 | 1800 | 64 - -Prerequisite: - -1. Install TensorFlow. Be sure to run the code using python2 and not python3. - -2. Download CIFAR-10/CIFAR-100 dataset. - -```shell -curl -o cifar-10-binary.tar.gz https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz -curl -o cifar-100-binary.tar.gz https://www.cs.toronto.edu/~kriz/cifar-100-python.tar.gz -``` - -How to run: - -```shell -# cd to the your workspace. -# Specify the directory where dataset is located using the data_path flag. -# Note: User can split samples from training set into the eval set by changing train_size and validation_size. - -# For example, to train the Wide-ResNet-28-10 model on a GPU. -python train_cifar.py --model_name=wrn \ - --checkpoint_dir=/tmp/training \ - --data_path=/tmp/data \ - --dataset='cifar10' \ - --use_cpu=0 -``` - -## Contact for Issues - -* Barret Zoph, @barretzoph -* Ekin Dogus Cubuk, diff --git a/research/autoaugment/augmentation_transforms.py b/research/autoaugment/augmentation_transforms.py deleted file mode 100644 index 584cce45eb5..00000000000 --- a/research/autoaugment/augmentation_transforms.py +++ /dev/null @@ -1,451 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Transforms used in the Augmentation Policies.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import random -import numpy as np -# pylint:disable=g-multiple-import -from PIL import ImageOps, ImageEnhance, ImageFilter, Image -# pylint:enable=g-multiple-import - - -IMAGE_SIZE = 32 -# What is the dataset mean and std of the images on the training set -MEANS = [0.49139968, 0.48215841, 0.44653091] -STDS = [0.24703223, 0.24348513, 0.26158784] -PARAMETER_MAX = 10 # What is the max 'level' a transform could be predicted - - -def random_flip(x): - """Flip the input x horizontally with 50% probability.""" - if np.random.rand(1)[0] > 0.5: - return np.fliplr(x) - return x - - -def zero_pad_and_crop(img, amount=4): - """Zero pad by `amount` zero pixels on each side then take a random crop. - - Args: - img: numpy image that will be zero padded and cropped. - amount: amount of zeros to pad `img` with horizontally and verically. - - Returns: - The cropped zero padded img. The returned numpy array will be of the same - shape as `img`. - """ - padded_img = np.zeros((img.shape[0] + amount * 2, img.shape[1] + amount * 2, - img.shape[2])) - padded_img[amount:img.shape[0] + amount, amount: - img.shape[1] + amount, :] = img - top = np.random.randint(low=0, high=2 * amount) - left = np.random.randint(low=0, high=2 * amount) - new_img = padded_img[top:top + img.shape[0], left:left + img.shape[1], :] - return new_img - - -def create_cutout_mask(img_height, img_width, num_channels, size): - """Creates a zero mask used for cutout of shape `img_height` x `img_width`. - - Args: - img_height: Height of image cutout mask will be applied to. - img_width: Width of image cutout mask will be applied to. - num_channels: Number of channels in the image. - size: Size of the zeros mask. - - Returns: - A mask of shape `img_height` x `img_width` with all ones except for a - square of zeros of shape `size` x `size`. This mask is meant to be - elementwise multiplied with the original image. Additionally returns - the `upper_coord` and `lower_coord` which specify where the cutout mask - will be applied. - """ - assert img_height == img_width - - # Sample center where cutout mask will be applied - height_loc = np.random.randint(low=0, high=img_height) - width_loc = np.random.randint(low=0, high=img_width) - - # Determine upper right and lower left corners of patch - upper_coord = (max(0, height_loc - size // 2), max(0, width_loc - size // 2)) - lower_coord = (min(img_height, height_loc + size // 2), - min(img_width, width_loc + size // 2)) - mask_height = lower_coord[0] - upper_coord[0] - mask_width = lower_coord[1] - upper_coord[1] - assert mask_height > 0 - assert mask_width > 0 - - mask = np.ones((img_height, img_width, num_channels)) - zeros = np.zeros((mask_height, mask_width, num_channels)) - mask[upper_coord[0]:lower_coord[0], upper_coord[1]:lower_coord[1], :] = ( - zeros) - return mask, upper_coord, lower_coord - - -def cutout_numpy(img, size=16): - """Apply cutout with mask of shape `size` x `size` to `img`. - - The cutout operation is from the paper https://arxiv.org/abs/1708.04552. - This operation applies a `size`x`size` mask of zeros to a random location - within `img`. - - Args: - img: Numpy image that cutout will be applied to. - size: Height/width of the cutout mask that will be - - Returns: - A numpy tensor that is the result of applying the cutout mask to `img`. - """ - img_height, img_width, num_channels = (img.shape[0], img.shape[1], - img.shape[2]) - assert len(img.shape) == 3 - mask, _, _ = create_cutout_mask(img_height, img_width, num_channels, size) - return img * mask - - -def float_parameter(level, maxval): - """Helper function to scale `val` between 0 and maxval . - - Args: - level: Level of the operation that will be between [0, `PARAMETER_MAX`]. - maxval: Maximum value that the operation can have. This will be scaled - to level/PARAMETER_MAX. - - Returns: - A float that results from scaling `maxval` according to `level`. - """ - return float(level) * maxval / PARAMETER_MAX - - -def int_parameter(level, maxval): - """Helper function to scale `val` between 0 and maxval . - - Args: - level: Level of the operation that will be between [0, `PARAMETER_MAX`]. - maxval: Maximum value that the operation can have. This will be scaled - to level/PARAMETER_MAX. - - Returns: - An int that results from scaling `maxval` according to `level`. - """ - return int(level * maxval / PARAMETER_MAX) - - -def pil_wrap(img): - """Convert the `img` numpy tensor to a PIL Image.""" - return Image.fromarray( - np.uint8((img * STDS + MEANS) * 255.0)).convert('RGBA') - - -def pil_unwrap(pil_img): - """Converts the PIL img to a numpy array.""" - pic_array = (np.array(pil_img.getdata()).reshape((32, 32, 4)) / 255.0) - i1, i2 = np.where(pic_array[:, :, 3] == 0) - pic_array = (pic_array[:, :, :3] - MEANS) / STDS - pic_array[i1, i2] = [0, 0, 0] - return pic_array - - -def apply_policy(policy, img): - """Apply the `policy` to the numpy `img`. - - Args: - policy: A list of tuples with the form (name, probability, level) where - `name` is the name of the augmentation operation to apply, `probability` - is the probability of applying the operation and `level` is what strength - the operation to apply. - img: Numpy image that will have `policy` applied to it. - - Returns: - The result of applying `policy` to `img`. - """ - pil_img = pil_wrap(img) - for xform in policy: - assert len(xform) == 3 - name, probability, level = xform - xform_fn = NAME_TO_TRANSFORM[name].pil_transformer(probability, level) - pil_img = xform_fn(pil_img) - return pil_unwrap(pil_img) - - -class TransformFunction(object): - """Wraps the Transform function for pretty printing options.""" - - def __init__(self, func, name): - self.f = func - self.name = name - - def __repr__(self): - return '<' + self.name + '>' - - def __call__(self, pil_img): - return self.f(pil_img) - - -class TransformT(object): - """Each instance of this class represents a specific transform.""" - - def __init__(self, name, xform_fn): - self.name = name - self.xform = xform_fn - - def pil_transformer(self, probability, level): - - def return_function(im): - if random.random() < probability: - im = self.xform(im, level) - return im - - name = self.name + '({:.1f},{})'.format(probability, level) - return TransformFunction(return_function, name) - - def do_transform(self, image, level): - f = self.pil_transformer(PARAMETER_MAX, level) - return pil_unwrap(f(pil_wrap(image))) - - -################## Transform Functions ################## -identity = TransformT('identity', lambda pil_img, level: pil_img) -flip_lr = TransformT( - 'FlipLR', - lambda pil_img, level: pil_img.transpose(Image.FLIP_LEFT_RIGHT)) -flip_ud = TransformT( - 'FlipUD', - lambda pil_img, level: pil_img.transpose(Image.FLIP_TOP_BOTTOM)) -# pylint:disable=g-long-lambda -auto_contrast = TransformT( - 'AutoContrast', - lambda pil_img, level: ImageOps.autocontrast( - pil_img.convert('RGB')).convert('RGBA')) -equalize = TransformT( - 'Equalize', - lambda pil_img, level: ImageOps.equalize( - pil_img.convert('RGB')).convert('RGBA')) -invert = TransformT( - 'Invert', - lambda pil_img, level: ImageOps.invert( - pil_img.convert('RGB')).convert('RGBA')) -# pylint:enable=g-long-lambda -blur = TransformT( - 'Blur', lambda pil_img, level: pil_img.filter(ImageFilter.BLUR)) -smooth = TransformT( - 'Smooth', - lambda pil_img, level: pil_img.filter(ImageFilter.SMOOTH)) - - -def _rotate_impl(pil_img, level): - """Rotates `pil_img` from -30 to 30 degrees depending on `level`.""" - degrees = int_parameter(level, 30) - if random.random() > 0.5: - degrees = -degrees - return pil_img.rotate(degrees) - - -rotate = TransformT('Rotate', _rotate_impl) - - -def _posterize_impl(pil_img, level): - """Applies PIL Posterize to `pil_img`.""" - level = int_parameter(level, 4) - return ImageOps.posterize(pil_img.convert('RGB'), 4 - level).convert('RGBA') - - -posterize = TransformT('Posterize', _posterize_impl) - - -def _shear_x_impl(pil_img, level): - """Applies PIL ShearX to `pil_img`. - - The ShearX operation shears the image along the horizontal axis with `level` - magnitude. - - Args: - pil_img: Image in PIL object. - level: Strength of the operation specified as an Integer from - [0, `PARAMETER_MAX`]. - - Returns: - A PIL Image that has had ShearX applied to it. - """ - level = float_parameter(level, 0.3) - if random.random() > 0.5: - level = -level - return pil_img.transform((32, 32), Image.AFFINE, (1, level, 0, 0, 1, 0)) - - -shear_x = TransformT('ShearX', _shear_x_impl) - - -def _shear_y_impl(pil_img, level): - """Applies PIL ShearY to `pil_img`. - - The ShearY operation shears the image along the vertical axis with `level` - magnitude. - - Args: - pil_img: Image in PIL object. - level: Strength of the operation specified as an Integer from - [0, `PARAMETER_MAX`]. - - Returns: - A PIL Image that has had ShearX applied to it. - """ - level = float_parameter(level, 0.3) - if random.random() > 0.5: - level = -level - return pil_img.transform((32, 32), Image.AFFINE, (1, 0, 0, level, 1, 0)) - - -shear_y = TransformT('ShearY', _shear_y_impl) - - -def _translate_x_impl(pil_img, level): - """Applies PIL TranslateX to `pil_img`. - - Translate the image in the horizontal direction by `level` - number of pixels. - - Args: - pil_img: Image in PIL object. - level: Strength of the operation specified as an Integer from - [0, `PARAMETER_MAX`]. - - Returns: - A PIL Image that has had TranslateX applied to it. - """ - level = int_parameter(level, 10) - if random.random() > 0.5: - level = -level - return pil_img.transform((32, 32), Image.AFFINE, (1, 0, level, 0, 1, 0)) - - -translate_x = TransformT('TranslateX', _translate_x_impl) - - -def _translate_y_impl(pil_img, level): - """Applies PIL TranslateY to `pil_img`. - - Translate the image in the vertical direction by `level` - number of pixels. - - Args: - pil_img: Image in PIL object. - level: Strength of the operation specified as an Integer from - [0, `PARAMETER_MAX`]. - - Returns: - A PIL Image that has had TranslateY applied to it. - """ - level = int_parameter(level, 10) - if random.random() > 0.5: - level = -level - return pil_img.transform((32, 32), Image.AFFINE, (1, 0, 0, 0, 1, level)) - - -translate_y = TransformT('TranslateY', _translate_y_impl) - - -def _crop_impl(pil_img, level, interpolation=Image.BILINEAR): - """Applies a crop to `pil_img` with the size depending on the `level`.""" - cropped = pil_img.crop((level, level, IMAGE_SIZE - level, IMAGE_SIZE - level)) - resized = cropped.resize((IMAGE_SIZE, IMAGE_SIZE), interpolation) - return resized - - -crop_bilinear = TransformT('CropBilinear', _crop_impl) - - -def _solarize_impl(pil_img, level): - """Applies PIL Solarize to `pil_img`. - - Translate the image in the vertical direction by `level` - number of pixels. - - Args: - pil_img: Image in PIL object. - level: Strength of the operation specified as an Integer from - [0, `PARAMETER_MAX`]. - - Returns: - A PIL Image that has had Solarize applied to it. - """ - level = int_parameter(level, 256) - return ImageOps.solarize(pil_img.convert('RGB'), 256 - level).convert('RGBA') - - -solarize = TransformT('Solarize', _solarize_impl) - - -def _cutout_pil_impl(pil_img, level): - """Apply cutout to pil_img at the specified level.""" - size = int_parameter(level, 20) - if size <= 0: - return pil_img - img_height, img_width, num_channels = (32, 32, 3) - _, upper_coord, lower_coord = ( - create_cutout_mask(img_height, img_width, num_channels, size)) - pixels = pil_img.load() # create the pixel map - for i in range(upper_coord[0], lower_coord[0]): # for every col: - for j in range(upper_coord[1], lower_coord[1]): # For every row - pixels[i, j] = (125, 122, 113, 0) # set the colour accordingly - return pil_img - -cutout = TransformT('Cutout', _cutout_pil_impl) - - -def _enhancer_impl(enhancer): - """Sets level to be between 0.1 and 1.8 for ImageEnhance transforms of PIL.""" - def impl(pil_img, level): - v = float_parameter(level, 1.8) + .1 # going to 0 just destroys it - return enhancer(pil_img).enhance(v) - return impl - - -color = TransformT('Color', _enhancer_impl(ImageEnhance.Color)) -contrast = TransformT('Contrast', _enhancer_impl(ImageEnhance.Contrast)) -brightness = TransformT('Brightness', _enhancer_impl( - ImageEnhance.Brightness)) -sharpness = TransformT('Sharpness', _enhancer_impl(ImageEnhance.Sharpness)) - -ALL_TRANSFORMS = [ - flip_lr, - flip_ud, - auto_contrast, - equalize, - invert, - rotate, - posterize, - crop_bilinear, - solarize, - color, - contrast, - brightness, - sharpness, - shear_x, - shear_y, - translate_x, - translate_y, - cutout, - blur, - smooth -] - -NAME_TO_TRANSFORM = {t.name: t for t in ALL_TRANSFORMS} -TRANSFORM_NAMES = NAME_TO_TRANSFORM.keys() diff --git a/research/autoaugment/custom_ops.py b/research/autoaugment/custom_ops.py deleted file mode 100644 index d2a69073860..00000000000 --- a/research/autoaugment/custom_ops.py +++ /dev/null @@ -1,197 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Contains convenience wrappers for typical Neural Network TensorFlow layers. - - Ops that have different behavior during training or eval have an is_training - parameter. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - -import numpy as np -import tensorflow as tf - - -arg_scope = tf.contrib.framework.arg_scope - - -def variable(name, shape, dtype, initializer, trainable): - """Returns a TF variable with the passed in specifications.""" - var = tf.get_variable( - name, - shape=shape, - dtype=dtype, - initializer=initializer, - trainable=trainable) - return var - - -def global_avg_pool(x, scope=None): - """Average pools away spatial height and width dimension of 4D tensor.""" - assert x.get_shape().ndims == 4 - with tf.name_scope(scope, 'global_avg_pool', [x]): - kernel_size = (1, int(x.shape[1]), int(x.shape[2]), 1) - squeeze_dims = (1, 2) - result = tf.nn.avg_pool( - x, - ksize=kernel_size, - strides=(1, 1, 1, 1), - padding='VALID', - data_format='NHWC') - return tf.squeeze(result, squeeze_dims) - - -def zero_pad(inputs, in_filter, out_filter): - """Zero pads `input` tensor to have `out_filter` number of filters.""" - outputs = tf.pad(inputs, [[0, 0], [0, 0], [0, 0], - [(out_filter - in_filter) // 2, - (out_filter - in_filter) // 2]]) - return outputs - - -@tf.contrib.framework.add_arg_scope -def batch_norm(inputs, - decay=0.999, - center=True, - scale=False, - epsilon=0.001, - is_training=True, - reuse=None, - scope=None): - """Small wrapper around tf.contrib.layers.batch_norm.""" - return tf.contrib.layers.batch_norm( - inputs, - decay=decay, - center=center, - scale=scale, - epsilon=epsilon, - activation_fn=None, - param_initializers=None, - updates_collections=tf.GraphKeys.UPDATE_OPS, - is_training=is_training, - reuse=reuse, - trainable=True, - fused=True, - data_format='NHWC', - zero_debias_moving_mean=False, - scope=scope) - - -def stride_arr(stride_h, stride_w): - return [1, stride_h, stride_w, 1] - - -@tf.contrib.framework.add_arg_scope -def conv2d(inputs, - num_filters_out, - kernel_size, - stride=1, - scope=None, - reuse=None): - """Adds a 2D convolution. - - conv2d creates a variable called 'weights', representing the convolutional - kernel, that is convolved with the input. - - Args: - inputs: a 4D tensor in NHWC format. - num_filters_out: the number of output filters. - kernel_size: an int specifying the kernel height and width size. - stride: an int specifying the height and width stride. - scope: Optional scope for variable_scope. - reuse: whether or not the layer and its variables should be reused. - Returns: - a tensor that is the result of a convolution being applied to `inputs`. - """ - with tf.variable_scope(scope, 'Conv', [inputs], reuse=reuse): - num_filters_in = int(inputs.shape[3]) - weights_shape = [kernel_size, kernel_size, num_filters_in, num_filters_out] - - # Initialization - n = int(weights_shape[0] * weights_shape[1] * weights_shape[3]) - weights_initializer = tf.random_normal_initializer( - stddev=np.sqrt(2.0 / n)) - - weights = variable( - name='weights', - shape=weights_shape, - dtype=tf.float32, - initializer=weights_initializer, - trainable=True) - strides = stride_arr(stride, stride) - outputs = tf.nn.conv2d( - inputs, weights, strides, padding='SAME', data_format='NHWC') - return outputs - - -@tf.contrib.framework.add_arg_scope -def fc(inputs, - num_units_out, - scope=None, - reuse=None): - """Creates a fully connected layer applied to `inputs`. - - Args: - inputs: a tensor that the fully connected layer will be applied to. It - will be reshaped if it is not 2D. - num_units_out: the number of output units in the layer. - scope: Optional scope for variable_scope. - reuse: whether or not the layer and its variables should be reused. - - Returns: - a tensor that is the result of applying a linear matrix to `inputs`. - """ - if len(inputs.shape) > 2: - inputs = tf.reshape(inputs, [int(inputs.shape[0]), -1]) - - with tf.variable_scope(scope, 'FC', [inputs], reuse=reuse): - num_units_in = inputs.shape[1] - weights_shape = [num_units_in, num_units_out] - unif_init_range = 1.0 / (num_units_out)**(0.5) - weights_initializer = tf.random_uniform_initializer( - -unif_init_range, unif_init_range) - weights = variable( - name='weights', - shape=weights_shape, - dtype=tf.float32, - initializer=weights_initializer, - trainable=True) - bias_initializer = tf.constant_initializer(0.0) - biases = variable( - name='biases', - shape=[num_units_out,], - dtype=tf.float32, - initializer=bias_initializer, - trainable=True) - outputs = tf.nn.xw_plus_b(inputs, weights, biases) - return outputs - - -@tf.contrib.framework.add_arg_scope -def avg_pool(inputs, kernel_size, stride=2, padding='VALID', scope=None): - """Wrapper around tf.nn.avg_pool.""" - with tf.name_scope(scope, 'AvgPool', [inputs]): - kernel = stride_arr(kernel_size, kernel_size) - strides = stride_arr(stride, stride) - return tf.nn.avg_pool( - inputs, - ksize=kernel, - strides=strides, - padding=padding, - data_format='NHWC') - diff --git a/research/autoaugment/data_utils.py b/research/autoaugment/data_utils.py deleted file mode 100644 index 9bf911560d1..00000000000 --- a/research/autoaugment/data_utils.py +++ /dev/null @@ -1,184 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Data utils for CIFAR-10 and CIFAR-100.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import copy -import cPickle -import os -import augmentation_transforms -import numpy as np -import policies as found_policies -import tensorflow as tf - - -# pylint:disable=logging-format-interpolation - - -class DataSet(object): - """Dataset object that produces augmented training and eval data.""" - - def __init__(self, hparams): - self.hparams = hparams - self.epochs = 0 - self.curr_train_index = 0 - - all_labels = [] - - self.good_policies = found_policies.good_policies() - - # Determine how many databatched to load - num_data_batches_to_load = 5 - total_batches_to_load = num_data_batches_to_load - train_batches_to_load = total_batches_to_load - assert hparams.train_size + hparams.validation_size <= 50000 - if hparams.eval_test: - total_batches_to_load += 1 - # Determine how many images we have loaded - total_dataset_size = 10000 * num_data_batches_to_load - train_dataset_size = total_dataset_size - if hparams.eval_test: - total_dataset_size += 10000 - - if hparams.dataset == 'cifar10': - all_data = np.empty((total_batches_to_load, 10000, 3072), dtype=np.uint8) - elif hparams.dataset == 'cifar100': - assert num_data_batches_to_load == 5 - all_data = np.empty((1, 50000, 3072), dtype=np.uint8) - if hparams.eval_test: - test_data = np.empty((1, 10000, 3072), dtype=np.uint8) - if hparams.dataset == 'cifar10': - tf.logging.info('Cifar10') - datafiles = [ - 'data_batch_1', 'data_batch_2', 'data_batch_3', 'data_batch_4', - 'data_batch_5'] - - datafiles = datafiles[:train_batches_to_load] - if hparams.eval_test: - datafiles.append('test_batch') - num_classes = 10 - elif hparams.dataset == 'cifar100': - datafiles = ['train'] - if hparams.eval_test: - datafiles.append('test') - num_classes = 100 - else: - raise NotImplementedError('Unimplemented dataset: ', hparams.dataset) - if hparams.dataset != 'test': - for file_num, f in enumerate(datafiles): - d = unpickle(os.path.join(hparams.data_path, f)) - if f == 'test': - test_data[0] = copy.deepcopy(d['data']) - all_data = np.concatenate([all_data, test_data], axis=1) - else: - all_data[file_num] = copy.deepcopy(d['data']) - if hparams.dataset == 'cifar10': - labels = np.array(d['labels']) - else: - labels = np.array(d['fine_labels']) - nsamples = len(labels) - for idx in range(nsamples): - all_labels.append(labels[idx]) - - all_data = all_data.reshape(total_dataset_size, 3072) - all_data = all_data.reshape(-1, 3, 32, 32) - all_data = all_data.transpose(0, 2, 3, 1).copy() - all_data = all_data / 255.0 - mean = augmentation_transforms.MEANS - std = augmentation_transforms.STDS - tf.logging.info('mean:{} std: {}'.format(mean, std)) - - all_data = (all_data - mean) / std - all_labels = np.eye(num_classes)[np.array(all_labels, dtype=np.int32)] - assert len(all_data) == len(all_labels) - tf.logging.info( - 'In CIFAR10 loader, number of images: {}'.format(len(all_data))) - - # Break off test data - if hparams.eval_test: - self.test_images = all_data[train_dataset_size:] - self.test_labels = all_labels[train_dataset_size:] - - # Shuffle the rest of the data - all_data = all_data[:train_dataset_size] - all_labels = all_labels[:train_dataset_size] - np.random.seed(0) - perm = np.arange(len(all_data)) - np.random.shuffle(perm) - all_data = all_data[perm] - all_labels = all_labels[perm] - - # Break into train and val - train_size, val_size = hparams.train_size, hparams.validation_size - assert 50000 >= train_size + val_size - self.train_images = all_data[:train_size] - self.train_labels = all_labels[:train_size] - self.val_images = all_data[train_size:train_size + val_size] - self.val_labels = all_labels[train_size:train_size + val_size] - self.num_train = self.train_images.shape[0] - - def next_batch(self): - """Return the next minibatch of augmented data.""" - next_train_index = self.curr_train_index + self.hparams.batch_size - if next_train_index > self.num_train: - # Increase epoch number - epoch = self.epochs + 1 - self.reset() - self.epochs = epoch - batched_data = ( - self.train_images[self.curr_train_index: - self.curr_train_index + self.hparams.batch_size], - self.train_labels[self.curr_train_index: - self.curr_train_index + self.hparams.batch_size]) - final_imgs = [] - - images, labels = batched_data - for data in images: - epoch_policy = self.good_policies[np.random.choice( - len(self.good_policies))] - final_img = augmentation_transforms.apply_policy( - epoch_policy, data) - final_img = augmentation_transforms.random_flip( - augmentation_transforms.zero_pad_and_crop(final_img, 4)) - # Apply cutout - final_img = augmentation_transforms.cutout_numpy(final_img) - final_imgs.append(final_img) - batched_data = (np.array(final_imgs, np.float32), labels) - self.curr_train_index += self.hparams.batch_size - return batched_data - - def reset(self): - """Reset training data and index into the training data.""" - self.epochs = 0 - # Shuffle the training data - perm = np.arange(self.num_train) - np.random.shuffle(perm) - assert self.num_train == self.train_images.shape[ - 0], 'Error incorrect shuffling mask' - self.train_images = self.train_images[perm] - self.train_labels = self.train_labels[perm] - self.curr_train_index = 0 - - -def unpickle(f): - tf.logging.info('loading file: {}'.format(f)) - fo = tf.gfile.Open(f, 'r') - d = cPickle.load(fo) - fo.close() - return d diff --git a/research/autoaugment/helper_utils.py b/research/autoaugment/helper_utils.py deleted file mode 100644 index e896874383f..00000000000 --- a/research/autoaugment/helper_utils.py +++ /dev/null @@ -1,149 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Helper functions used for training AutoAugment models.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - - -def setup_loss(logits, labels): - """Returns the cross entropy for the given `logits` and `labels`.""" - predictions = tf.nn.softmax(logits) - cost = tf.losses.softmax_cross_entropy(onehot_labels=labels, - logits=logits) - return predictions, cost - - -def decay_weights(cost, weight_decay_rate): - """Calculates the loss for l2 weight decay and adds it to `cost`.""" - costs = [] - for var in tf.trainable_variables(): - costs.append(tf.nn.l2_loss(var)) - cost += tf.multiply(weight_decay_rate, tf.add_n(costs)) - return cost - - -def eval_child_model(session, model, data_loader, mode): - """Evaluates `model` on held out data depending on `mode`. - - Args: - session: TensorFlow session the model will be run with. - model: TensorFlow model that will be evaluated. - data_loader: DataSet object that contains data that `model` will - evaluate. - mode: Will `model` either evaluate validation or test data. - - Returns: - Accuracy of `model` when evaluated on the specified dataset. - - Raises: - ValueError: if invalid dataset `mode` is specified. - """ - if mode == 'val': - images = data_loader.val_images - labels = data_loader.val_labels - elif mode == 'test': - images = data_loader.test_images - labels = data_loader.test_labels - else: - raise ValueError('Not valid eval mode') - assert len(images) == len(labels) - tf.logging.info('model.batch_size is {}'.format(model.batch_size)) - assert len(images) % model.batch_size == 0 - eval_batches = int(len(images) / model.batch_size) - for i in range(eval_batches): - eval_images = images[i * model.batch_size:(i + 1) * model.batch_size] - eval_labels = labels[i * model.batch_size:(i + 1) * model.batch_size] - _ = session.run( - model.eval_op, - feed_dict={ - model.images: eval_images, - model.labels: eval_labels, - }) - return session.run(model.accuracy) - - -def cosine_lr(learning_rate, epoch, iteration, batches_per_epoch, total_epochs): - """Cosine Learning rate. - - Args: - learning_rate: Initial learning rate. - epoch: Current epoch we are one. This is one based. - iteration: Current batch in this epoch. - batches_per_epoch: Batches per epoch. - total_epochs: Total epochs you are training for. - - Returns: - The learning rate to be used for this current batch. - """ - t_total = total_epochs * batches_per_epoch - t_cur = float(epoch * batches_per_epoch + iteration) - return 0.5 * learning_rate * (1 + np.cos(np.pi * t_cur / t_total)) - - -def get_lr(curr_epoch, hparams, iteration=None): - """Returns the learning rate during training based on the current epoch.""" - assert iteration is not None - batches_per_epoch = int(hparams.train_size / hparams.batch_size) - lr = cosine_lr(hparams.lr, curr_epoch, iteration, batches_per_epoch, - hparams.num_epochs) - return lr - - -def run_epoch_training(session, model, data_loader, curr_epoch): - """Runs one epoch of training for the model passed in. - - Args: - session: TensorFlow session the model will be run with. - model: TensorFlow model that will be evaluated. - data_loader: DataSet object that contains data that `model` will - evaluate. - curr_epoch: How many of epochs of training have been done so far. - - Returns: - The accuracy of 'model' on the training set - """ - steps_per_epoch = int(model.hparams.train_size / model.hparams.batch_size) - tf.logging.info('steps per epoch: {}'.format(steps_per_epoch)) - curr_step = session.run(model.global_step) - assert curr_step % steps_per_epoch == 0 - - # Get the current learning rate for the model based on the current epoch - curr_lr = get_lr(curr_epoch, model.hparams, iteration=0) - tf.logging.info('lr of {} for epoch {}'.format(curr_lr, curr_epoch)) - - for step in xrange(steps_per_epoch): - curr_lr = get_lr(curr_epoch, model.hparams, iteration=(step + 1)) - # Update the lr rate variable to the current LR. - model.lr_rate_ph.load(curr_lr, session=session) - if step % 20 == 0: - tf.logging.info('Training {}/{}'.format(step, steps_per_epoch)) - - train_images, train_labels = data_loader.next_batch() - _, step, _ = session.run( - [model.train_op, model.global_step, model.eval_op], - feed_dict={ - model.images: train_images, - model.labels: train_labels, - }) - - train_accuracy = session.run(model.accuracy) - tf.logging.info('Train accuracy: {}'.format(train_accuracy)) - return train_accuracy diff --git a/research/autoaugment/policies.py b/research/autoaugment/policies.py deleted file mode 100644 index 36b10b0ee4c..00000000000 --- a/research/autoaugment/policies.py +++ /dev/null @@ -1,140 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - -def good_policies(): - """AutoAugment policies found on Cifar.""" - exp0_0 = [ - [('Invert', 0.1, 7), ('Contrast', 0.2, 6)], - [('Rotate', 0.7, 2), ('TranslateX', 0.3, 9)], - [('Sharpness', 0.8, 1), ('Sharpness', 0.9, 3)], - [('ShearY', 0.5, 8), ('TranslateY', 0.7, 9)], - [('AutoContrast', 0.5, 8), ('Equalize', 0.9, 2)]] - exp0_1 = [ - [('Solarize', 0.4, 5), ('AutoContrast', 0.9, 3)], - [('TranslateY', 0.9, 9), ('TranslateY', 0.7, 9)], - [('AutoContrast', 0.9, 2), ('Solarize', 0.8, 3)], - [('Equalize', 0.8, 8), ('Invert', 0.1, 3)], - [('TranslateY', 0.7, 9), ('AutoContrast', 0.9, 1)]] - exp0_2 = [ - [('Solarize', 0.4, 5), ('AutoContrast', 0.0, 2)], - [('TranslateY', 0.7, 9), ('TranslateY', 0.7, 9)], - [('AutoContrast', 0.9, 0), ('Solarize', 0.4, 3)], - [('Equalize', 0.7, 5), ('Invert', 0.1, 3)], - [('TranslateY', 0.7, 9), ('TranslateY', 0.7, 9)]] - exp0_3 = [ - [('Solarize', 0.4, 5), ('AutoContrast', 0.9, 1)], - [('TranslateY', 0.8, 9), ('TranslateY', 0.9, 9)], - [('AutoContrast', 0.8, 0), ('TranslateY', 0.7, 9)], - [('TranslateY', 0.2, 7), ('Color', 0.9, 6)], - [('Equalize', 0.7, 6), ('Color', 0.4, 9)]] - exp1_0 = [ - [('ShearY', 0.2, 7), ('Posterize', 0.3, 7)], - [('Color', 0.4, 3), ('Brightness', 0.6, 7)], - [('Sharpness', 0.3, 9), ('Brightness', 0.7, 9)], - [('Equalize', 0.6, 5), ('Equalize', 0.5, 1)], - [('Contrast', 0.6, 7), ('Sharpness', 0.6, 5)]] - exp1_1 = [ - [('Brightness', 0.3, 7), ('AutoContrast', 0.5, 8)], - [('AutoContrast', 0.9, 4), ('AutoContrast', 0.5, 6)], - [('Solarize', 0.3, 5), ('Equalize', 0.6, 5)], - [('TranslateY', 0.2, 4), ('Sharpness', 0.3, 3)], - [('Brightness', 0.0, 8), ('Color', 0.8, 8)]] - exp1_2 = [ - [('Solarize', 0.2, 6), ('Color', 0.8, 6)], - [('Solarize', 0.2, 6), ('AutoContrast', 0.8, 1)], - [('Solarize', 0.4, 1), ('Equalize', 0.6, 5)], - [('Brightness', 0.0, 0), ('Solarize', 0.5, 2)], - [('AutoContrast', 0.9, 5), ('Brightness', 0.5, 3)]] - exp1_3 = [ - [('Contrast', 0.7, 5), ('Brightness', 0.0, 2)], - [('Solarize', 0.2, 8), ('Solarize', 0.1, 5)], - [('Contrast', 0.5, 1), ('TranslateY', 0.2, 9)], - [('AutoContrast', 0.6, 5), ('TranslateY', 0.0, 9)], - [('AutoContrast', 0.9, 4), ('Equalize', 0.8, 4)]] - exp1_4 = [ - [('Brightness', 0.0, 7), ('Equalize', 0.4, 7)], - [('Solarize', 0.2, 5), ('Equalize', 0.7, 5)], - [('Equalize', 0.6, 8), ('Color', 0.6, 2)], - [('Color', 0.3, 7), ('Color', 0.2, 4)], - [('AutoContrast', 0.5, 2), ('Solarize', 0.7, 2)]] - exp1_5 = [ - [('AutoContrast', 0.2, 0), ('Equalize', 0.1, 0)], - [('ShearY', 0.6, 5), ('Equalize', 0.6, 5)], - [('Brightness', 0.9, 3), ('AutoContrast', 0.4, 1)], - [('Equalize', 0.8, 8), ('Equalize', 0.7, 7)], - [('Equalize', 0.7, 7), ('Solarize', 0.5, 0)]] - exp1_6 = [ - [('Equalize', 0.8, 4), ('TranslateY', 0.8, 9)], - [('TranslateY', 0.8, 9), ('TranslateY', 0.6, 9)], - [('TranslateY', 0.9, 0), ('TranslateY', 0.5, 9)], - [('AutoContrast', 0.5, 3), ('Solarize', 0.3, 4)], - [('Solarize', 0.5, 3), ('Equalize', 0.4, 4)]] - exp2_0 = [ - [('Color', 0.7, 7), ('TranslateX', 0.5, 8)], - [('Equalize', 0.3, 7), ('AutoContrast', 0.4, 8)], - [('TranslateY', 0.4, 3), ('Sharpness', 0.2, 6)], - [('Brightness', 0.9, 6), ('Color', 0.2, 8)], - [('Solarize', 0.5, 2), ('Invert', 0.0, 3)]] - exp2_1 = [ - [('AutoContrast', 0.1, 5), ('Brightness', 0.0, 0)], - [('Cutout', 0.2, 4), ('Equalize', 0.1, 1)], - [('Equalize', 0.7, 7), ('AutoContrast', 0.6, 4)], - [('Color', 0.1, 8), ('ShearY', 0.2, 3)], - [('ShearY', 0.4, 2), ('Rotate', 0.7, 0)]] - exp2_2 = [ - [('ShearY', 0.1, 3), ('AutoContrast', 0.9, 5)], - [('TranslateY', 0.3, 6), ('Cutout', 0.3, 3)], - [('Equalize', 0.5, 0), ('Solarize', 0.6, 6)], - [('AutoContrast', 0.3, 5), ('Rotate', 0.2, 7)], - [('Equalize', 0.8, 2), ('Invert', 0.4, 0)]] - exp2_3 = [ - [('Equalize', 0.9, 5), ('Color', 0.7, 0)], - [('Equalize', 0.1, 1), ('ShearY', 0.1, 3)], - [('AutoContrast', 0.7, 3), ('Equalize', 0.7, 0)], - [('Brightness', 0.5, 1), ('Contrast', 0.1, 7)], - [('Contrast', 0.1, 4), ('Solarize', 0.6, 5)]] - exp2_4 = [ - [('Solarize', 0.2, 3), ('ShearX', 0.0, 0)], - [('TranslateX', 0.3, 0), ('TranslateX', 0.6, 0)], - [('Equalize', 0.5, 9), ('TranslateY', 0.6, 7)], - [('ShearX', 0.1, 0), ('Sharpness', 0.5, 1)], - [('Equalize', 0.8, 6), ('Invert', 0.3, 6)]] - exp2_5 = [ - [('AutoContrast', 0.3, 9), ('Cutout', 0.5, 3)], - [('ShearX', 0.4, 4), ('AutoContrast', 0.9, 2)], - [('ShearX', 0.0, 3), ('Posterize', 0.0, 3)], - [('Solarize', 0.4, 3), ('Color', 0.2, 4)], - [('Equalize', 0.1, 4), ('Equalize', 0.7, 6)]] - exp2_6 = [ - [('Equalize', 0.3, 8), ('AutoContrast', 0.4, 3)], - [('Solarize', 0.6, 4), ('AutoContrast', 0.7, 6)], - [('AutoContrast', 0.2, 9), ('Brightness', 0.4, 8)], - [('Equalize', 0.1, 0), ('Equalize', 0.0, 6)], - [('Equalize', 0.8, 4), ('Equalize', 0.0, 4)]] - exp2_7 = [ - [('Equalize', 0.5, 5), ('AutoContrast', 0.1, 2)], - [('Solarize', 0.5, 5), ('AutoContrast', 0.9, 5)], - [('AutoContrast', 0.6, 1), ('AutoContrast', 0.7, 8)], - [('Equalize', 0.2, 0), ('AutoContrast', 0.1, 2)], - [('Equalize', 0.6, 9), ('Equalize', 0.4, 4)]] - exp0s = exp0_0 + exp0_1 + exp0_2 + exp0_3 - exp1s = exp1_0 + exp1_1 + exp1_2 + exp1_3 + exp1_4 + exp1_5 + exp1_6 - exp2s = exp2_0 + exp2_1 + exp2_2 + exp2_3 + exp2_4 + exp2_5 + exp2_6 + exp2_7 - return exp0s + exp1s + exp2s diff --git a/research/autoaugment/shake_drop.py b/research/autoaugment/shake_drop.py deleted file mode 100644 index b6d3bcdb6c7..00000000000 --- a/research/autoaugment/shake_drop.py +++ /dev/null @@ -1,178 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Builds the Shake-Shake Model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math -import custom_ops as ops -import tensorflow as tf - - -def round_int(x): - """Rounds `x` and then converts to an int.""" - return int(math.floor(x + 0.5)) - - -def shortcut(x, output_filters, stride): - """Applies strided avg pool or zero padding to make output_filters match x.""" - num_filters = int(x.shape[3]) - if stride == 2: - x = ops.avg_pool(x, 2, stride=stride, padding='SAME') - if num_filters != output_filters: - diff = output_filters - num_filters - assert diff > 0 - # Zero padd diff zeros - padding = [[0, 0], [0, 0], [0, 0], [0, diff]] - x = tf.pad(x, padding) - return x - - -def calc_prob(curr_layer, total_layers, p_l): - """Calculates drop prob depending on the current layer.""" - return 1 - (float(curr_layer) / total_layers) * p_l - - -def bottleneck_layer(x, n, stride, prob, is_training, alpha, beta): - """Bottleneck layer for shake drop model.""" - assert alpha[1] > alpha[0] - assert beta[1] > beta[0] - with tf.variable_scope('bottleneck_{}'.format(prob)): - input_layer = x - x = ops.batch_norm(x, scope='bn_1_pre') - x = ops.conv2d(x, n, 1, scope='1x1_conv_contract') - x = ops.batch_norm(x, scope='bn_1_post') - x = tf.nn.relu(x) - x = ops.conv2d(x, n, 3, stride=stride, scope='3x3') - x = ops.batch_norm(x, scope='bn_2') - x = tf.nn.relu(x) - x = ops.conv2d(x, n * 4, 1, scope='1x1_conv_expand') - x = ops.batch_norm(x, scope='bn_3') - - # Apply regularization here - # Sample bernoulli with prob - if is_training: - batch_size = tf.shape(x)[0] - bern_shape = [batch_size, 1, 1, 1] - random_tensor = prob - random_tensor += tf.random_uniform(bern_shape, dtype=tf.float32) - binary_tensor = tf.floor(random_tensor) - - alpha_values = tf.random_uniform( - [batch_size, 1, 1, 1], minval=alpha[0], maxval=alpha[1], - dtype=tf.float32) - beta_values = tf.random_uniform( - [batch_size, 1, 1, 1], minval=beta[0], maxval=beta[1], - dtype=tf.float32) - rand_forward = ( - binary_tensor + alpha_values - binary_tensor * alpha_values) - rand_backward = ( - binary_tensor + beta_values - binary_tensor * beta_values) - x = x * rand_backward + tf.stop_gradient(x * rand_forward - - x * rand_backward) - else: - expected_alpha = (alpha[1] + alpha[0])/2 - # prob is the expectation of the bernoulli variable - x = (prob + expected_alpha - prob * expected_alpha) * x - - res = shortcut(input_layer, n * 4, stride) - return x + res - - -def build_shake_drop_model(images, num_classes, is_training): - """Builds the PyramidNet Shake-Drop model. - - Build the PyramidNet Shake-Drop model from https://arxiv.org/abs/1802.02375. - - Args: - images: Tensor of images that will be fed into the Wide ResNet Model. - num_classes: Number of classed that the model needs to predict. - is_training: Is the model training or not. - - Returns: - The logits of the PyramidNet Shake-Drop model. - """ - # ShakeDrop Hparams - p_l = 0.5 - alpha_shake = [-1, 1] - beta_shake = [0, 1] - - # PyramidNet Hparams - alpha = 200 - depth = 272 - # This is for the bottleneck architecture specifically - n = int((depth - 2) / 9) - start_channel = 16 - add_channel = alpha / (3 * n) - - # Building the models - x = images - x = ops.conv2d(x, 16, 3, scope='init_conv') - x = ops.batch_norm(x, scope='init_bn') - - layer_num = 1 - total_layers = n * 3 - start_channel += add_channel - prob = calc_prob(layer_num, total_layers, p_l) - x = bottleneck_layer( - x, round_int(start_channel), 1, prob, is_training, alpha_shake, - beta_shake) - layer_num += 1 - for _ in range(1, n): - start_channel += add_channel - prob = calc_prob(layer_num, total_layers, p_l) - x = bottleneck_layer( - x, round_int(start_channel), 1, prob, is_training, alpha_shake, - beta_shake) - layer_num += 1 - - start_channel += add_channel - prob = calc_prob(layer_num, total_layers, p_l) - x = bottleneck_layer( - x, round_int(start_channel), 2, prob, is_training, alpha_shake, - beta_shake) - layer_num += 1 - for _ in range(1, n): - start_channel += add_channel - prob = calc_prob(layer_num, total_layers, p_l) - x = bottleneck_layer( - x, round_int(start_channel), 1, prob, is_training, alpha_shake, - beta_shake) - layer_num += 1 - - start_channel += add_channel - prob = calc_prob(layer_num, total_layers, p_l) - x = bottleneck_layer( - x, round_int(start_channel), 2, prob, is_training, alpha_shake, - beta_shake) - layer_num += 1 - for _ in range(1, n): - start_channel += add_channel - prob = calc_prob(layer_num, total_layers, p_l) - x = bottleneck_layer( - x, round_int(start_channel), 1, prob, is_training, alpha_shake, - beta_shake) - layer_num += 1 - - assert layer_num - 1 == total_layers - x = ops.batch_norm(x, scope='final_bn') - x = tf.nn.relu(x) - x = ops.global_avg_pool(x) - # Fully connected - logits = ops.fc(x, num_classes) - return logits diff --git a/research/autoaugment/shake_shake.py b/research/autoaugment/shake_shake.py deleted file mode 100644 index b937372c5e5..00000000000 --- a/research/autoaugment/shake_shake.py +++ /dev/null @@ -1,147 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Builds the Shake-Shake Model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import custom_ops as ops -import tensorflow as tf - - -def _shake_shake_skip_connection(x, output_filters, stride): - """Adds a residual connection to the filter x for the shake-shake model.""" - curr_filters = int(x.shape[3]) - if curr_filters == output_filters: - return x - stride_spec = ops.stride_arr(stride, stride) - # Skip path 1 - path1 = tf.nn.avg_pool( - x, [1, 1, 1, 1], stride_spec, 'VALID', data_format='NHWC') - path1 = ops.conv2d(path1, int(output_filters / 2), 1, scope='path1_conv') - - # Skip path 2 - # First pad with 0's then crop - pad_arr = [[0, 0], [0, 1], [0, 1], [0, 0]] - path2 = tf.pad(x, pad_arr)[:, 1:, 1:, :] - concat_axis = 3 - - path2 = tf.nn.avg_pool( - path2, [1, 1, 1, 1], stride_spec, 'VALID', data_format='NHWC') - path2 = ops.conv2d(path2, int(output_filters / 2), 1, scope='path2_conv') - - # Concat and apply BN - final_path = tf.concat(values=[path1, path2], axis=concat_axis) - final_path = ops.batch_norm(final_path, scope='final_path_bn') - return final_path - - -def _shake_shake_branch(x, output_filters, stride, rand_forward, rand_backward, - is_training): - """Building a 2 branching convnet.""" - x = tf.nn.relu(x) - x = ops.conv2d(x, output_filters, 3, stride=stride, scope='conv1') - x = ops.batch_norm(x, scope='bn1') - x = tf.nn.relu(x) - x = ops.conv2d(x, output_filters, 3, scope='conv2') - x = ops.batch_norm(x, scope='bn2') - if is_training: - x = x * rand_backward + tf.stop_gradient(x * rand_forward - - x * rand_backward) - else: - x *= 1.0 / 2 - return x - - -def _shake_shake_block(x, output_filters, stride, is_training): - """Builds a full shake-shake sub layer.""" - batch_size = tf.shape(x)[0] - - # Generate random numbers for scaling the branches - rand_forward = [ - tf.random_uniform( - [batch_size, 1, 1, 1], minval=0, maxval=1, dtype=tf.float32) - for _ in range(2) - ] - rand_backward = [ - tf.random_uniform( - [batch_size, 1, 1, 1], minval=0, maxval=1, dtype=tf.float32) - for _ in range(2) - ] - # Normalize so that all sum to 1 - total_forward = tf.add_n(rand_forward) - total_backward = tf.add_n(rand_backward) - rand_forward = [samp / total_forward for samp in rand_forward] - rand_backward = [samp / total_backward for samp in rand_backward] - zipped_rand = zip(rand_forward, rand_backward) - - branches = [] - for branch, (r_forward, r_backward) in enumerate(zipped_rand): - with tf.variable_scope('branch_{}'.format(branch)): - b = _shake_shake_branch(x, output_filters, stride, r_forward, r_backward, - is_training) - branches.append(b) - res = _shake_shake_skip_connection(x, output_filters, stride) - return res + tf.add_n(branches) - - -def _shake_shake_layer(x, output_filters, num_blocks, stride, - is_training): - """Builds many sub layers into one full layer.""" - for block_num in range(num_blocks): - curr_stride = stride if (block_num == 0) else 1 - with tf.variable_scope('layer_{}'.format(block_num)): - x = _shake_shake_block(x, output_filters, curr_stride, - is_training) - return x - - -def build_shake_shake_model(images, num_classes, hparams, is_training): - """Builds the Shake-Shake model. - - Build the Shake-Shake model from https://arxiv.org/abs/1705.07485. - - Args: - images: Tensor of images that will be fed into the Wide ResNet Model. - num_classes: Number of classed that the model needs to predict. - hparams: tf.HParams object that contains additional hparams needed to - construct the model. In this case it is the `shake_shake_widen_factor` - that is used to determine how many filters the model has. - is_training: Is the model training or not. - - Returns: - The logits of the Shake-Shake model. - """ - depth = 26 - k = hparams.shake_shake_widen_factor # The widen factor - n = int((depth - 2) / 6) - x = images - - x = ops.conv2d(x, 16, 3, scope='init_conv') - x = ops.batch_norm(x, scope='init_bn') - with tf.variable_scope('L1'): - x = _shake_shake_layer(x, 16 * k, n, 1, is_training) - with tf.variable_scope('L2'): - x = _shake_shake_layer(x, 32 * k, n, 2, is_training) - with tf.variable_scope('L3'): - x = _shake_shake_layer(x, 64 * k, n, 2, is_training) - x = tf.nn.relu(x) - x = ops.global_avg_pool(x) - - # Fully connected - logits = ops.fc(x, num_classes) - return logits diff --git a/research/autoaugment/train_cifar.py b/research/autoaugment/train_cifar.py deleted file mode 100644 index 9e3942ee26b..00000000000 --- a/research/autoaugment/train_cifar.py +++ /dev/null @@ -1,452 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""AutoAugment Train/Eval module. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import contextlib -import os -import time - -import custom_ops as ops -import data_utils -import helper_utils -import numpy as np -from shake_drop import build_shake_drop_model -from shake_shake import build_shake_shake_model -import tensorflow as tf -from wrn import build_wrn_model - -tf.flags.DEFINE_string('model_name', 'wrn', - 'wrn, shake_shake_32, shake_shake_96, shake_shake_112, ' - 'pyramid_net') -tf.flags.DEFINE_string('checkpoint_dir', '/tmp/training', 'Training Directory.') -tf.flags.DEFINE_string('data_path', '/tmp/data', - 'Directory where dataset is located.') -tf.flags.DEFINE_string('dataset', 'cifar10', - 'Dataset to train with. Either cifar10 or cifar100') -tf.flags.DEFINE_integer('use_cpu', 1, '1 if use CPU, else GPU.') - -FLAGS = tf.flags.FLAGS - -arg_scope = tf.contrib.framework.arg_scope - - -def setup_arg_scopes(is_training): - """Sets up the argscopes that will be used when building an image model. - - Args: - is_training: Is the model training or not. - - Returns: - Arg scopes to be put around the model being constructed. - """ - - batch_norm_decay = 0.9 - batch_norm_epsilon = 1e-5 - batch_norm_params = { - # Decay for the moving averages. - 'decay': batch_norm_decay, - # epsilon to prevent 0s in variance. - 'epsilon': batch_norm_epsilon, - 'scale': True, - # collection containing the moving mean and moving variance. - 'is_training': is_training, - } - - scopes = [] - - scopes.append(arg_scope([ops.batch_norm], **batch_norm_params)) - return scopes - - -def build_model(inputs, num_classes, is_training, hparams): - """Constructs the vision model being trained/evaled. - - Args: - inputs: input features/images being fed to the image model build built. - num_classes: number of output classes being predicted. - is_training: is the model training or not. - hparams: additional hyperparameters associated with the image model. - - Returns: - The logits of the image model. - """ - scopes = setup_arg_scopes(is_training) - with contextlib.nested(*scopes): - if hparams.model_name == 'pyramid_net': - logits = build_shake_drop_model( - inputs, num_classes, is_training) - elif hparams.model_name == 'wrn': - logits = build_wrn_model( - inputs, num_classes, hparams.wrn_size) - elif hparams.model_name == 'shake_shake': - logits = build_shake_shake_model( - inputs, num_classes, hparams, is_training) - return logits - - -class CifarModel(object): - """Builds an image model for Cifar10/Cifar100.""" - - def __init__(self, hparams): - self.hparams = hparams - - def build(self, mode): - """Construct the cifar model.""" - assert mode in ['train', 'eval'] - self.mode = mode - self._setup_misc(mode) - self._setup_images_and_labels() - self._build_graph(self.images, self.labels, mode) - - self.init = tf.group(tf.global_variables_initializer(), - tf.local_variables_initializer()) - - def _setup_misc(self, mode): - """Sets up miscellaneous in the cifar model constructor.""" - self.lr_rate_ph = tf.Variable(0.0, name='lrn_rate', trainable=False) - self.reuse = None if (mode == 'train') else True - self.batch_size = self.hparams.batch_size - if mode == 'eval': - self.batch_size = 25 - - def _setup_images_and_labels(self): - """Sets up image and label placeholders for the cifar model.""" - if FLAGS.dataset == 'cifar10': - self.num_classes = 10 - else: - self.num_classes = 100 - self.images = tf.placeholder(tf.float32, [self.batch_size, 32, 32, 3]) - self.labels = tf.placeholder(tf.float32, - [self.batch_size, self.num_classes]) - - def assign_epoch(self, session, epoch_value): - session.run(self._epoch_update, feed_dict={self._new_epoch: epoch_value}) - - def _build_graph(self, images, labels, mode): - """Constructs the TF graph for the cifar model. - - Args: - images: A 4-D image Tensor - labels: A 2-D labels Tensor. - mode: string indicating training mode ( e.g., 'train', 'valid', 'test'). - """ - is_training = 'train' in mode - if is_training: - self.global_step = tf.train.get_or_create_global_step() - - logits = build_model( - images, - self.num_classes, - is_training, - self.hparams) - self.predictions, self.cost = helper_utils.setup_loss( - logits, labels) - self.accuracy, self.eval_op = tf.metrics.accuracy( - tf.argmax(labels, 1), tf.argmax(self.predictions, 1)) - self._calc_num_trainable_params() - - # Adds L2 weight decay to the cost - self.cost = helper_utils.decay_weights(self.cost, - self.hparams.weight_decay_rate) - - if is_training: - self._build_train_op() - - # Setup checkpointing for this child model - # Keep 2 or more checkpoints around during training. - with tf.device('/cpu:0'): - self.saver = tf.train.Saver(max_to_keep=2) - - self.init = tf.group(tf.global_variables_initializer(), - tf.local_variables_initializer()) - - def _calc_num_trainable_params(self): - self.num_trainable_params = np.sum([ - np.prod(var.get_shape().as_list()) for var in tf.trainable_variables() - ]) - tf.logging.info('number of trainable params: {}'.format( - self.num_trainable_params)) - - def _build_train_op(self): - """Builds the train op for the cifar model.""" - hparams = self.hparams - tvars = tf.trainable_variables() - grads = tf.gradients(self.cost, tvars) - if hparams.gradient_clipping_by_global_norm > 0.0: - grads, norm = tf.clip_by_global_norm( - grads, hparams.gradient_clipping_by_global_norm) - tf.summary.scalar('grad_norm', norm) - - # Setup the initial learning rate - initial_lr = self.lr_rate_ph - optimizer = tf.train.MomentumOptimizer( - initial_lr, - 0.9, - use_nesterov=True) - - self.optimizer = optimizer - apply_op = optimizer.apply_gradients( - zip(grads, tvars), global_step=self.global_step, name='train_step') - train_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) - with tf.control_dependencies([apply_op]): - self.train_op = tf.group(*train_ops) - - -class CifarModelTrainer(object): - """Trains an instance of the CifarModel class.""" - - def __init__(self, hparams): - self._session = None - self.hparams = hparams - - self.model_dir = os.path.join(FLAGS.checkpoint_dir, 'model') - self.log_dir = os.path.join(FLAGS.checkpoint_dir, 'log') - # Set the random seed to be sure the same validation set - # is used for each model - np.random.seed(0) - self.data_loader = data_utils.DataSet(hparams) - np.random.seed() # Put the random seed back to random - self.data_loader.reset() - - def save_model(self, step=None): - """Dumps model into the backup_dir. - - Args: - step: If provided, creates a checkpoint with the given step - number, instead of overwriting the existing checkpoints. - """ - model_save_name = os.path.join(self.model_dir, 'model.ckpt') - if not tf.gfile.IsDirectory(self.model_dir): - tf.gfile.MakeDirs(self.model_dir) - self.saver.save(self.session, model_save_name, global_step=step) - tf.logging.info('Saved child model') - - def extract_model_spec(self): - """Loads a checkpoint with the architecture structure stored in the name.""" - checkpoint_path = tf.train.latest_checkpoint(self.model_dir) - if checkpoint_path is not None: - self.saver.restore(self.session, checkpoint_path) - tf.logging.info('Loaded child model checkpoint from %s', - checkpoint_path) - else: - self.save_model(step=0) - - def eval_child_model(self, model, data_loader, mode): - """Evaluate the child model. - - Args: - model: image model that will be evaluated. - data_loader: dataset object to extract eval data from. - mode: will the model be evalled on train, val or test. - - Returns: - Accuracy of the model on the specified dataset. - """ - tf.logging.info('Evaluating child model in mode %s', mode) - while True: - try: - with self._new_session(model): - accuracy = helper_utils.eval_child_model( - self.session, - model, - data_loader, - mode) - tf.logging.info('Eval child model accuracy: {}'.format(accuracy)) - # If epoch trained without raising the below errors, break - # from loop. - break - except (tf.errors.AbortedError, tf.errors.UnavailableError) as e: - tf.logging.info('Retryable error caught: %s. Retrying.', e) - - return accuracy - - @contextlib.contextmanager - def _new_session(self, m): - """Creates a new session for model m.""" - # Create a new session for this model, initialize - # variables, and save / restore from - # checkpoint. - self._session = tf.Session( - '', - config=tf.ConfigProto( - allow_soft_placement=True, log_device_placement=False)) - self.session.run(m.init) - - # Load in a previous checkpoint, or save this one - self.extract_model_spec() - try: - yield - finally: - tf.Session.reset('') - self._session = None - - def _build_models(self): - """Builds the image models for train and eval.""" - # Determine if we should build the train and eval model. When using - # distributed training we only want to build one or the other and not both. - with tf.variable_scope('model', use_resource=False): - m = CifarModel(self.hparams) - m.build('train') - self._num_trainable_params = m.num_trainable_params - self._saver = m.saver - with tf.variable_scope('model', reuse=True, use_resource=False): - meval = CifarModel(self.hparams) - meval.build('eval') - return m, meval - - def _calc_starting_epoch(self, m): - """Calculates the starting epoch for model m based on global step.""" - hparams = self.hparams - batch_size = hparams.batch_size - steps_per_epoch = int(hparams.train_size / batch_size) - with self._new_session(m): - curr_step = self.session.run(m.global_step) - total_steps = steps_per_epoch * hparams.num_epochs - epochs_left = (total_steps - curr_step) // steps_per_epoch - starting_epoch = hparams.num_epochs - epochs_left - return starting_epoch - - def _run_training_loop(self, m, curr_epoch): - """Trains the cifar model `m` for one epoch.""" - start_time = time.time() - while True: - try: - with self._new_session(m): - train_accuracy = helper_utils.run_epoch_training( - self.session, m, self.data_loader, curr_epoch) - tf.logging.info('Saving model after epoch') - self.save_model(step=curr_epoch) - break - except (tf.errors.AbortedError, tf.errors.UnavailableError) as e: - tf.logging.info('Retryable error caught: %s. Retrying.', e) - tf.logging.info('Finished epoch: {}'.format(curr_epoch)) - tf.logging.info('Epoch time(min): {}'.format( - (time.time() - start_time) / 60.0)) - return train_accuracy - - def _compute_final_accuracies(self, meval): - """Run once training is finished to compute final val/test accuracies.""" - valid_accuracy = self.eval_child_model(meval, self.data_loader, 'val') - if self.hparams.eval_test: - test_accuracy = self.eval_child_model(meval, self.data_loader, 'test') - else: - test_accuracy = 0 - tf.logging.info('Test Accuracy: {}'.format(test_accuracy)) - return valid_accuracy, test_accuracy - - def run_model(self): - """Trains and evalutes the image model.""" - hparams = self.hparams - - # Build the child graph - with tf.Graph().as_default(), tf.device( - '/cpu:0' if FLAGS.use_cpu else '/gpu:0'): - m, meval = self._build_models() - - # Figure out what epoch we are on - starting_epoch = self._calc_starting_epoch(m) - - # Run the validation error right at the beginning - valid_accuracy = self.eval_child_model( - meval, self.data_loader, 'val') - tf.logging.info('Before Training Epoch: {} Val Acc: {}'.format( - starting_epoch, valid_accuracy)) - training_accuracy = None - - for curr_epoch in xrange(starting_epoch, hparams.num_epochs): - - # Run one training epoch - training_accuracy = self._run_training_loop(m, curr_epoch) - - valid_accuracy = self.eval_child_model( - meval, self.data_loader, 'val') - tf.logging.info('Epoch: {} Valid Acc: {}'.format( - curr_epoch, valid_accuracy)) - - valid_accuracy, test_accuracy = self._compute_final_accuracies( - meval) - - tf.logging.info( - 'Train Acc: {} Valid Acc: {} Test Acc: {}'.format( - training_accuracy, valid_accuracy, test_accuracy)) - - @property - def saver(self): - return self._saver - - @property - def session(self): - return self._session - - @property - def num_trainable_params(self): - return self._num_trainable_params - - -def main(_): - if FLAGS.dataset not in ['cifar10', 'cifar100']: - raise ValueError('Invalid dataset: %s' % FLAGS.dataset) - hparams = tf.contrib.training.HParams( - train_size=50000, - validation_size=0, - eval_test=1, - dataset=FLAGS.dataset, - data_path=FLAGS.data_path, - batch_size=128, - gradient_clipping_by_global_norm=5.0) - if FLAGS.model_name == 'wrn': - hparams.add_hparam('model_name', 'wrn') - hparams.add_hparam('num_epochs', 200) - hparams.add_hparam('wrn_size', 160) - hparams.add_hparam('lr', 0.1) - hparams.add_hparam('weight_decay_rate', 5e-4) - elif FLAGS.model_name == 'shake_shake_32': - hparams.add_hparam('model_name', 'shake_shake') - hparams.add_hparam('num_epochs', 1800) - hparams.add_hparam('shake_shake_widen_factor', 2) - hparams.add_hparam('lr', 0.01) - hparams.add_hparam('weight_decay_rate', 0.001) - elif FLAGS.model_name == 'shake_shake_96': - hparams.add_hparam('model_name', 'shake_shake') - hparams.add_hparam('num_epochs', 1800) - hparams.add_hparam('shake_shake_widen_factor', 6) - hparams.add_hparam('lr', 0.01) - hparams.add_hparam('weight_decay_rate', 0.001) - elif FLAGS.model_name == 'shake_shake_112': - hparams.add_hparam('model_name', 'shake_shake') - hparams.add_hparam('num_epochs', 1800) - hparams.add_hparam('shake_shake_widen_factor', 7) - hparams.add_hparam('lr', 0.01) - hparams.add_hparam('weight_decay_rate', 0.001) - elif FLAGS.model_name == 'pyramid_net': - hparams.add_hparam('model_name', 'pyramid_net') - hparams.add_hparam('num_epochs', 1800) - hparams.add_hparam('lr', 0.05) - hparams.add_hparam('weight_decay_rate', 5e-5) - hparams.batch_size = 64 - else: - raise ValueError('Not Valid Model Name: %s' % FLAGS.model_name) - cifar_trainer = CifarModelTrainer(hparams) - cifar_trainer.run_model() - -if __name__ == '__main__': - tf.logging.set_verbosity(tf.logging.INFO) - tf.app.run() diff --git a/research/autoaugment/wrn.py b/research/autoaugment/wrn.py deleted file mode 100644 index ea04e19cfc3..00000000000 --- a/research/autoaugment/wrn.py +++ /dev/null @@ -1,158 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Builds the Wide-ResNet Model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import custom_ops as ops -import numpy as np -import tensorflow as tf - - - -def residual_block( - x, in_filter, out_filter, stride, activate_before_residual=False): - """Adds residual connection to `x` in addition to applying BN->ReLU->3x3 Conv. - - Args: - x: Tensor that is the output of the previous layer in the model. - in_filter: Number of filters `x` has. - out_filter: Number of filters that the output of this layer will have. - stride: Integer that specified what stride should be applied to `x`. - activate_before_residual: Boolean on whether a BN->ReLU should be applied - to x before the convolution is applied. - - Returns: - A Tensor that is the result of applying two sequences of BN->ReLU->3x3 Conv - and then adding that Tensor to `x`. - """ - - if activate_before_residual: # Pass up RELU and BN activation for resnet - with tf.variable_scope('shared_activation'): - x = ops.batch_norm(x, scope='init_bn') - x = tf.nn.relu(x) - orig_x = x - else: - orig_x = x - - block_x = x - if not activate_before_residual: - with tf.variable_scope('residual_only_activation'): - block_x = ops.batch_norm(block_x, scope='init_bn') - block_x = tf.nn.relu(block_x) - - with tf.variable_scope('sub1'): - block_x = ops.conv2d( - block_x, out_filter, 3, stride=stride, scope='conv1') - - with tf.variable_scope('sub2'): - block_x = ops.batch_norm(block_x, scope='bn2') - block_x = tf.nn.relu(block_x) - block_x = ops.conv2d( - block_x, out_filter, 3, stride=1, scope='conv2') - - with tf.variable_scope( - 'sub_add'): # If number of filters do not agree then zero pad them - if in_filter != out_filter: - orig_x = ops.avg_pool(orig_x, stride, stride) - orig_x = ops.zero_pad(orig_x, in_filter, out_filter) - x = orig_x + block_x - return x - - -def _res_add(in_filter, out_filter, stride, x, orig_x): - """Adds `x` with `orig_x`, both of which are layers in the model. - - Args: - in_filter: Number of filters in `orig_x`. - out_filter: Number of filters in `x`. - stride: Integer specifying the stide that should be applied `orig_x`. - x: Tensor that is the output of the previous layer. - orig_x: Tensor that is the output of an earlier layer in the network. - - Returns: - A Tensor that is the result of `x` and `orig_x` being added after - zero padding and striding are applied to `orig_x` to get the shapes - to match. - """ - if in_filter != out_filter: - orig_x = ops.avg_pool(orig_x, stride, stride) - orig_x = ops.zero_pad(orig_x, in_filter, out_filter) - x = x + orig_x - orig_x = x - return x, orig_x - - -def build_wrn_model(images, num_classes, wrn_size): - """Builds the WRN model. - - Build the Wide ResNet model from https://arxiv.org/abs/1605.07146. - - Args: - images: Tensor of images that will be fed into the Wide ResNet Model. - num_classes: Number of classed that the model needs to predict. - wrn_size: Parameter that scales the number of filters in the Wide ResNet - model. - - Returns: - The logits of the Wide ResNet model. - """ - kernel_size = wrn_size - filter_size = 3 - num_blocks_per_resnet = 4 - filters = [ - min(kernel_size, 16), kernel_size, kernel_size * 2, kernel_size * 4 - ] - strides = [1, 2, 2] # stride for each resblock - - # Run the first conv - with tf.variable_scope('init'): - x = images - output_filters = filters[0] - x = ops.conv2d(x, output_filters, filter_size, scope='init_conv') - - first_x = x # Res from the beginning - orig_x = x # Res from previous block - - for block_num in range(1, 4): - with tf.variable_scope('unit_{}_0'.format(block_num)): - activate_before_residual = True if block_num == 1 else False - x = residual_block( - x, - filters[block_num - 1], - filters[block_num], - strides[block_num - 1], - activate_before_residual=activate_before_residual) - for i in range(1, num_blocks_per_resnet): - with tf.variable_scope('unit_{}_{}'.format(block_num, i)): - x = residual_block( - x, - filters[block_num], - filters[block_num], - 1, - activate_before_residual=False) - x, orig_x = _res_add(filters[block_num - 1], filters[block_num], - strides[block_num - 1], x, orig_x) - final_stride_val = np.prod(strides) - x, _ = _res_add(filters[0], filters[3], final_stride_val, x, first_x) - with tf.variable_scope('unit_last'): - x = ops.batch_norm(x, scope='final_bn') - x = tf.nn.relu(x) - x = ops.global_avg_pool(x) - logits = ops.fc(x, num_classes) - return logits diff --git a/research/cognitive_planning/BUILD b/research/cognitive_planning/BUILD deleted file mode 100644 index 3561987df8a..00000000000 --- a/research/cognitive_planning/BUILD +++ /dev/null @@ -1,19 +0,0 @@ -package(default_visibility = [":internal"]) - -licenses(["notice"]) # Apache 2.0 - -exports_files(["LICENSE"]) - -package_group( - name = "internal", - packages = [ - "//cognitive_planning/...", - ], -) - -py_binary( - name = "train_supervised_active_vision", - srcs = [ - "train_supervised_active_vision.py", - ], -) diff --git a/research/cognitive_planning/README.md b/research/cognitive_planning/README.md deleted file mode 100644 index 1c63ddc3f90..00000000000 --- a/research/cognitive_planning/README.md +++ /dev/null @@ -1,157 +0,0 @@ -# cognitive_planning - -**Visual Representation for Semantic Target Driven Navigation** - -Arsalan Mousavian, Alexander Toshev, Marek Fiser, Jana Kosecka, James Davidson - -This is the implementation of semantic target driven navigation training and evaluation on -Active Vision dataset. - -ECCV Workshop on Visual Learning and Embodied Agents in Simulation Environments -2018. - -
- - - - - - - - - - - - - - - - - -
Target: FridgeTarget: Television
Target: MicrowaveTarget: Couch
-
- - - -Paper: [https://arxiv.org/abs/1805.06066](https://arxiv.org/abs/1805.06066) - - -## 1. Installation - -### Requirements - -#### Python Packages - -```shell -networkx -gin-config -``` - -### Download cognitive_planning - -```shell -git clone --depth 1 https://github.com/tensorflow/models.git -``` - -## 2. Datasets - -### Download ActiveVision Dataset -We used Active Vision Dataset (AVD) which can be downloaded from [here](http://cs.unc.edu/~ammirato/active_vision_dataset_website/). To make our code faster and reduce memory footprint, we created the AVD Minimal dataset. AVD Minimal consists of low resolution images from the original AVD dataset. In addition, we added annotations for target views, predicted object detections from pre-trained object detector on MS-COCO dataset, and predicted semantic segmentation from pre-trained model on NYU-v2 dataset. AVD minimal can be downloaded from [here](https://storage.googleapis.com/active-vision-dataset/AVD_Minimal.zip). Set `$AVD_DIR` as the path to the downloaded AVD Minimal. - -### TODO: SUNCG Dataset -Current version of the code does not support SUNCG dataset. It can be added by -implementing necessary functions of `envs/task_env.py` using the public -released code of SUNCG environment such as -[House3d](https://github.com/facebookresearch/House3D) and -[MINOS](https://github.com/minosworld/minos). - -### ActiveVisionDataset Demo - - -If you wish to navigate the environment, to see how the AVD looks like you can use the following command: -```shell -python viz_active_vision_dataset_main -- \ - --mode=human \ - --gin_config=envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root=$AVD_DIR' -``` - -## 3. Training -Right now, the released version only supports training and inference using the real data from Active Vision Dataset. - -When RGB image modality is used, the Resnet embeddings are initialized. To start the training download pre-trained Resnet50 check point in the working directory ./resnet_v2_50_checkpoint/resnet_v2_50.ckpt - -``` -wget http://download.tensorflow.org/models/resnet_v2_50_2017_04_14.tar.gz -``` -### Run training -Use the following command for training: -```shell -# Train -python train_supervised_active_vision.py \ - --mode='train' \ - --logdir=$CHECKPOINT_DIR \ - --modality_types='det' \ - --batch_size=8 \ - --train_iters=200000 \ - --lstm_cell_size=2048 \ - --policy_fc_size=2048 \ - --sequence_length=20 \ - --max_eval_episode_length=100 \ - --test_iters=194 \ - --gin_config=envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root=$AVD_DIR' \ - --logtostderr -``` - -The training can be run for different modalities and modality combinations, including semantic segmentation, object detectors, RGB images, depth images. Low resolution images and outputs of detectors pretrained on COCO dataset and semantic segmenation pre trained on NYU dataset are provided as a part of this distribution and can be found in Meta directory of AVD_Minimal. -Additional details are described in the comments of the code and in the paper. - -### Run Evaluation -Use the following command for unrolling the policy on the eval environments. The inference code periodically check the checkpoint folder for new checkpoints to use it for unrolling the policy on the eval environments. After each evaluation, it will create a folder in the $CHECKPOINT_DIR/evals/$ITER where $ITER is the iteration number at which the checkpoint is stored. -```shell -# Eval -python train_supervised_active_vision.py \ - --mode='eval' \ - --logdir=$CHECKPOINT_DIR \ - --modality_types='det' \ - --batch_size=8 \ - --train_iters=200000 \ - --lstm_cell_size=2048 \ - --policy_fc_size=2048 \ - --sequence_length=20 \ - --max_eval_episode_length=100 \ - --test_iters=194 \ - --gin_config=envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root=$AVD_DIR' \ - --logtostderr -``` -At any point, you can run the following command to compute statistics such as success rate over all the evaluations so far. It also generates gif images for unrolling of the best policy. -```shell -# Visualize and Compute Stats -python viz_active_vision_dataset_main.py \ - --mode=eval \ - --eval_folder=$CHECKPOINT_DIR/evals/ \ - --output_folder=$OUTPUT_GIFS_FOLDER \ - --gin_config=envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root=$AVD_DIR' -``` -## Contact - -To ask questions or report issues please open an issue on the tensorflow/models -[issues tracker](https://github.com/tensorflow/models/issues). -Please assign issues to @arsalan-mousavian. - -## Reference -The details of the training and experiments can be found in the following paper. If you find our work useful in your research please consider citing our paper: - -``` -@inproceedings{MousavianECCVW18, - author = {A. Mousavian and A. Toshev and M. Fiser and J. Kosecka and J. Davidson}, - title = {Visual Representations for Semantic Target Driven Navigation}, - booktitle = {ECCV Workshop on Visual Learning and Embodied Agents in Simulation Environments}, - year = {2018}, -} -``` - - diff --git a/research/cognitive_planning/__init__.py b/research/cognitive_planning/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cognitive_planning/command b/research/cognitive_planning/command deleted file mode 100644 index daf634b3c6e..00000000000 --- a/research/cognitive_planning/command +++ /dev/null @@ -1,14 +0,0 @@ -python train_supervised_active_vision \ - --mode='train' \ - --logdir=/usr/local/google/home/kosecka/checkin_log_det/ \ - --modality_types='det' \ - --batch_size=8 \ - --train_iters=200000 \ - --lstm_cell_size=2048 \ - --policy_fc_size=2048 \ - --sequence_length=20 \ - --max_eval_episode_length=100 \ - --test_iters=194 \ - --gin_config=robotics/cognitive_planning/envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root="/usr/local/google/home/kosecka/AVD_minimal/"' \ - --logtostderr diff --git a/research/cognitive_planning/embedders.py b/research/cognitive_planning/embedders.py deleted file mode 100644 index 91ed9f45e2f..00000000000 --- a/research/cognitive_planning/embedders.py +++ /dev/null @@ -1,547 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Interface for different embedders for modalities.""" - -import abc -import numpy as np -import tensorflow as tf -import preprocessing -from tensorflow.contrib.slim.nets import resnet_v2 - -slim = tf.contrib.slim - - -class Embedder(object): - """Represents the embedder for different modalities. - - Modalities can be semantic segmentation, depth channel, object detection and - so on, which require specific embedder for them. - """ - __metaclass__ = abc.ABCMeta - - @abc.abstractmethod - def build(self, observation): - """Builds the model to embed the observation modality. - - Args: - observation: tensor that contains the raw observation from modality. - Returns: - Embedding tensor for the given observation tensor. - """ - raise NotImplementedError( - 'Needs to be implemented as part of Embedder Interface') - - -class DetectionBoxEmbedder(Embedder): - """Represents the model that encodes the detection boxes from images.""" - - def __init__(self, rnn_state_size, scope=None): - self._rnn_state_size = rnn_state_size - self._scope = scope - - def build(self, observations): - """Builds the model to embed object detection observations. - - Args: - observations: a tuple of (dets, det_num). - dets is a tensor of BxTxLxE that has the detection boxes in all the - images of the batch. B is the batch size, T is the maximum length of - episode, L is the maximum number of detections per image in the batch - and E is the size of each detection embedding. - det_num is a tensor of BxT that contains the number of detected boxes - each image of each sequence in the batch. - Returns: - For each image in the batch, returns the accumulative embedding of all the - detection boxes in that image. - """ - with tf.variable_scope(self._scope, default_name=''): - shape = observations[0].shape - dets = tf.reshape(observations[0], [-1, shape[-2], shape[-1]]) - det_num = tf.reshape(observations[1], [-1]) - lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self._rnn_state_size) - batch_size = tf.shape(dets)[0] - lstm_outputs, _ = tf.nn.dynamic_rnn( - cell=lstm_cell, - inputs=dets, - sequence_length=det_num, - initial_state=lstm_cell.zero_state(batch_size, dtype=tf.float32), - dtype=tf.float32) - # Gathering the last state of each sequence in the batch. - batch_range = tf.range(batch_size) - indices = tf.stack([batch_range, det_num - 1], axis=1) - last_lstm_outputs = tf.gather_nd(lstm_outputs, indices) - last_lstm_outputs = tf.reshape(last_lstm_outputs, - [-1, shape[1], self._rnn_state_size]) - return last_lstm_outputs - - -class ResNet(Embedder): - """Residual net embedder for image data.""" - - def __init__(self, params, *args, **kwargs): - super(ResNet, self).__init__(*args, **kwargs) - self._params = params - self._extra_train_ops = [] - - def build(self, images): - shape = images.get_shape().as_list() - if len(shape) == 5: - images = tf.reshape(images, - [shape[0] * shape[1], shape[2], shape[3], shape[4]]) - embedding = self._build_model(images) - if len(shape) == 5: - embedding = tf.reshape(embedding, [shape[0], shape[1], -1]) - - return embedding - - @property - def extra_train_ops(self): - return self._extra_train_ops - - def _build_model(self, images): - """Builds the model.""" - - # Convert images to floats and normalize them. - images = tf.to_float(images) - bs = images.get_shape().as_list()[0] - images = [ - tf.image.per_image_standardization(tf.squeeze(i)) - for i in tf.split(images, bs) - ] - images = tf.concat([tf.expand_dims(i, axis=0) for i in images], axis=0) - - with tf.variable_scope('init'): - x = self._conv('init_conv', images, 3, 3, 16, self._stride_arr(1)) - - strides = [1, 2, 2] - activate_before_residual = [True, False, False] - if self._params.use_bottleneck: - res_func = self._bottleneck_residual - filters = [16, 64, 128, 256] - else: - res_func = self._residual - filters = [16, 16, 32, 128] - - with tf.variable_scope('unit_1_0'): - x = res_func(x, filters[0], filters[1], self._stride_arr(strides[0]), - activate_before_residual[0]) - for i in xrange(1, self._params.num_residual_units): - with tf.variable_scope('unit_1_%d' % i): - x = res_func(x, filters[1], filters[1], self._stride_arr(1), False) - - with tf.variable_scope('unit_2_0'): - x = res_func(x, filters[1], filters[2], self._stride_arr(strides[1]), - activate_before_residual[1]) - for i in xrange(1, self._params.num_residual_units): - with tf.variable_scope('unit_2_%d' % i): - x = res_func(x, filters[2], filters[2], self._stride_arr(1), False) - - with tf.variable_scope('unit_3_0'): - x = res_func(x, filters[2], filters[3], self._stride_arr(strides[2]), - activate_before_residual[2]) - for i in xrange(1, self._params.num_residual_units): - with tf.variable_scope('unit_3_%d' % i): - x = res_func(x, filters[3], filters[3], self._stride_arr(1), False) - - with tf.variable_scope('unit_last'): - x = self._batch_norm('final_bn', x) - x = self._relu(x, self._params.relu_leakiness) - - with tf.variable_scope('pool_logit'): - x = self._global_avg_pooling(x) - - return x - - def _stride_arr(self, stride): - return [1, stride, stride, 1] - - def _batch_norm(self, name, x): - """batch norm implementation.""" - with tf.variable_scope(name): - params_shape = [x.shape[-1]] - - beta = tf.get_variable( - 'beta', - params_shape, - tf.float32, - initializer=tf.constant_initializer(0.0, tf.float32)) - gamma = tf.get_variable( - 'gamma', - params_shape, - tf.float32, - initializer=tf.constant_initializer(1.0, tf.float32)) - - if self._params.is_train: - mean, variance = tf.nn.moments(x, [0, 1, 2], name='moments') - - moving_mean = tf.get_variable( - 'moving_mean', - params_shape, - tf.float32, - initializer=tf.constant_initializer(0.0, tf.float32), - trainable=False) - moving_variance = tf.get_variable( - 'moving_variance', - params_shape, - tf.float32, - initializer=tf.constant_initializer(1.0, tf.float32), - trainable=False) - - self._extra_train_ops.append( - tf.assign_moving_average(moving_mean, mean, 0.9)) - self._extra_train_ops.append( - tf.assign_moving_average(moving_variance, variance, 0.9)) - else: - mean = tf.get_variable( - 'moving_mean', - params_shape, - tf.float32, - initializer=tf.constant_initializer(0.0, tf.float32), - trainable=False) - variance = tf.get_variable( - 'moving_variance', - params_shape, - tf.float32, - initializer=tf.constant_initializer(1.0, tf.float32), - trainable=False) - tf.summary.histogram(mean.op.name, mean) - tf.summary.histogram(variance.op.name, variance) - # elipson used to be 1e-5. Maybe 0.001 solves NaN problem in deeper net. - y = tf.nn.batch_normalization(x, mean, variance, beta, gamma, 0.001) - y.set_shape(x.shape) - return y - - def _residual(self, - x, - in_filter, - out_filter, - stride, - activate_before_residual=False): - """Residual unit with 2 sub layers.""" - - if activate_before_residual: - with tf.variable_scope('shared_activation'): - x = self._batch_norm('init_bn', x) - x = self._relu(x, self._params.relu_leakiness) - orig_x = x - else: - with tf.variable_scope('residual_only_activation'): - orig_x = x - x = self._batch_norm('init_bn', x) - x = self._relu(x, self._params.relu_leakiness) - - with tf.variable_scope('sub1'): - x = self._conv('conv1', x, 3, in_filter, out_filter, stride) - - with tf.variable_scope('sub2'): - x = self._batch_norm('bn2', x) - x = self._relu(x, self._params.relu_leakiness) - x = self._conv('conv2', x, 3, out_filter, out_filter, [1, 1, 1, 1]) - - with tf.variable_scope('sub_add'): - if in_filter != out_filter: - orig_x = tf.nn.avg_pool(orig_x, stride, stride, 'VALID') - orig_x = tf.pad( - orig_x, [[0, 0], [0, 0], [0, 0], [(out_filter - in_filter) // 2, - (out_filter - in_filter) // 2]]) - x += orig_x - - return x - - def _bottleneck_residual(self, - x, - in_filter, - out_filter, - stride, - activate_before_residual=False): - """A residual convolutional layer with a bottleneck. - - The layer is a composite of three convolutional layers with a ReLU non- - linearity and batch normalization after each linear convolution. The depth - if the second and third layer is out_filter / 4 (hence it is a bottleneck). - - Args: - x: a float 4 rank Tensor representing the input to the layer. - in_filter: a python integer representing depth of the input. - out_filter: a python integer representing depth of the output. - stride: a python integer denoting the stride of the layer applied before - the first convolution. - activate_before_residual: a python boolean. If True, then a ReLU is - applied as a first operation on the input x before everything else. - Returns: - A 4 rank Tensor with batch_size = batch size of input, width and height = - width / stride and height / stride of the input and depth = out_filter. - """ - if activate_before_residual: - with tf.variable_scope('common_bn_relu'): - x = self._batch_norm('init_bn', x) - x = self._relu(x, self._params.relu_leakiness) - orig_x = x - else: - with tf.variable_scope('residual_bn_relu'): - orig_x = x - x = self._batch_norm('init_bn', x) - x = self._relu(x, self._params.relu_leakiness) - - with tf.variable_scope('sub1'): - x = self._conv('conv1', x, 1, in_filter, out_filter / 4, stride) - - with tf.variable_scope('sub2'): - x = self._batch_norm('bn2', x) - x = self._relu(x, self._params.relu_leakiness) - x = self._conv('conv2', x, 3, out_filter / 4, out_filter / 4, - [1, 1, 1, 1]) - - with tf.variable_scope('sub3'): - x = self._batch_norm('bn3', x) - x = self._relu(x, self._params.relu_leakiness) - x = self._conv('conv3', x, 1, out_filter / 4, out_filter, [1, 1, 1, 1]) - - with tf.variable_scope('sub_add'): - if in_filter != out_filter: - orig_x = self._conv('project', orig_x, 1, in_filter, out_filter, stride) - x += orig_x - - return x - - def _decay(self): - costs = [] - for var in tf.trainable_variables(): - if var.op.name.find(r'DW') > 0: - costs.append(tf.nn.l2_loss(var)) - - return tf.mul(self._params.weight_decay_rate, tf.add_n(costs)) - - def _conv(self, name, x, filter_size, in_filters, out_filters, strides): - """Convolution.""" - with tf.variable_scope(name): - n = filter_size * filter_size * out_filters - kernel = tf.get_variable( - 'DW', [filter_size, filter_size, in_filters, out_filters], - tf.float32, - initializer=tf.random_normal_initializer(stddev=np.sqrt(2.0 / n))) - return tf.nn.conv2d(x, kernel, strides, padding='SAME') - - def _relu(self, x, leakiness=0.0): - return tf.where(tf.less(x, 0.0), leakiness * x, x, name='leaky_relu') - - def _fully_connected(self, x, out_dim): - x = tf.reshape(x, [self._params.batch_size, -1]) - w = tf.get_variable( - 'DW', [x.get_shape()[1], out_dim], - initializer=tf.uniform_unit_scaling_initializer(factor=1.0)) - b = tf.get_variable( - 'biases', [out_dim], initializer=tf.constant_initializer()) - return tf.nn.xw_plus_b(x, w, b) - - def _global_avg_pooling(self, x): - assert x.get_shape().ndims == 4 - return tf.reduce_mean(x, [1, 2]) - - -class MLPEmbedder(Embedder): - """Embedder of vectorial data. - - The net is a multi-layer perceptron, with ReLU nonlinearities in all layers - except the last one. - """ - - def __init__(self, layers, *args, **kwargs): - """Constructs MLPEmbedder. - - Args: - layers: a list of python integers representing layer sizes. - *args: arguments for super constructor. - **kwargs: keyed arguments for super constructor. - """ - super(MLPEmbedder, self).__init__(*args, **kwargs) - self._layers = layers - - def build(self, features): - shape = features.get_shape().as_list() - if len(shape) == 3: - features = tf.reshape(features, [shape[0] * shape[1], shape[2]]) - x = features - for i, dim in enumerate(self._layers): - with tf.variable_scope('layer_%i' % i): - x = self._fully_connected(x, dim) - if i < len(self._layers) - 1: - x = self._relu(x) - - if len(shape) == 3: - x = tf.reshape(x, shape[:-1] + [self._layers[-1]]) - return x - - def _fully_connected(self, x, out_dim): - w = tf.get_variable( - 'DW', [x.get_shape()[1], out_dim], - initializer=tf.variance_scaling_initializer(distribution='uniform')) - b = tf.get_variable( - 'biases', [out_dim], initializer=tf.constant_initializer()) - return tf.nn.xw_plus_b(x, w, b) - - def _relu(self, x, leakiness=0.0): - return tf.where(tf.less(x, 0.0), leakiness * x, x, name='leaky_relu') - - -class SmallNetworkEmbedder(Embedder): - """Embedder for image like observations. - - The network is comprised of multiple conv layers and a fully connected layer - at the end. The number of conv layers and the parameters are configured from - params. - """ - - def __init__(self, params, *args, **kwargs): - """Constructs the small network. - - Args: - params: params should be tf.hparams type. params need to have a list of - conv_sizes, conv_strides, conv_channels. The length of these lists - should be equal to each other and to the number of conv layers in the - network. Plus, it also needs to have boolean variable named to_one_hot - which indicates whether the input should be converted to one hot or not. - The size of the fully connected layer is specified by - params.embedding_size. - - *args: The rest of the parameters. - **kwargs: the reset of the parameters. - - Raises: - ValueError: If the length of params.conv_strides, params.conv_sizes, and - params.conv_channels are not equal. - - """ - - super(SmallNetworkEmbedder, self).__init__(*args, **kwargs) - self._params = params - if len(self._params.conv_sizes) != len(self._params.conv_strides): - raise ValueError( - 'Conv sizes and strides should have the same length: {} != {}'.format( - len(self._params.conv_sizes), len(self._params.conv_strides))) - - if len(self._params.conv_sizes) != len(self._params.conv_channels): - raise ValueError( - 'Conv sizes and channels should have the same length: {} != {}'. - format(len(self._params.conv_sizes), len(self._params.conv_channels))) - - def build(self, images): - """Builds the embedder with the given speicifcation. - - Args: - images: a tensor that contains the input images which has the shape of - NxTxHxWxC where N is the batch size, T is the maximum length of the - sequence, H and W are the height and width of the images and C is the - number of channels. - - Returns: - A tensor that is the embedding of the images. - """ - - shape = images.get_shape().as_list() - images = tf.reshape(images, - [shape[0] * shape[1], shape[2], shape[3], shape[4]]) - - with slim.arg_scope( - [slim.conv2d, slim.fully_connected], - activation_fn=tf.nn.relu, - weights_regularizer=slim.l2_regularizer(self._params.weight_decay_rate), - biases_initializer=tf.zeros_initializer()): - with slim.arg_scope([slim.conv2d], padding='SAME'): - # convert the image to one hot if needed. - if self._params.to_one_hot: - net = tf.one_hot( - tf.squeeze(tf.to_int32(images), axis=[-1]), - self._params.one_hot_length) - else: - net = images - - p = self._params - # Adding conv layers with the specified configurations. - for conv_id, kernel_stride_channel in enumerate( - zip(p.conv_sizes, p.conv_strides, p.conv_channels)): - kernel_size, stride, channels = kernel_stride_channel - net = slim.conv2d( - net, - channels, [kernel_size, kernel_size], - stride, - scope='conv_{}'.format(conv_id + 1)) - - net = slim.flatten(net) - net = slim.fully_connected(net, self._params.embedding_size, scope='fc') - - output = tf.reshape(net, [shape[0], shape[1], -1]) - return output - - -class ResNet50Embedder(Embedder): - """Uses ResNet50 to embed input images.""" - - def build(self, images): - """Builds a ResNet50 embedder for the input images. - - It assumes that the range of the pixel values in the images tensor is - [0,255] and should be castable to tf.uint8. - - Args: - images: a tensor that contains the input images which has the shape of - NxTxHxWx3 where N is the batch size, T is the maximum length of the - sequence, H and W are the height and width of the images and C is the - number of channels. - Returns: - The embedding of the input image with the shape of NxTxL where L is the - embedding size of the output. - - Raises: - ValueError: if the shape of the input does not agree with the expected - shape explained in the Args section. - """ - shape = images.get_shape().as_list() - if len(shape) != 5: - raise ValueError( - 'The tensor shape should have 5 elements, {} is provided'.format( - len(shape))) - if shape[4] != 3: - raise ValueError('Three channels are expected for the input image') - - images = tf.cast(images, tf.uint8) - images = tf.reshape(images, - [shape[0] * shape[1], shape[2], shape[3], shape[4]]) - with slim.arg_scope(resnet_v2.resnet_arg_scope()): - - def preprocess_fn(x): - x = tf.expand_dims(x, 0) - x = tf.image.resize_bilinear(x, [299, 299], - align_corners=False) - return(tf.squeeze(x, [0])) - - images = tf.map_fn(preprocess_fn, images, dtype=tf.float32) - - net, _ = resnet_v2.resnet_v2_50( - images, is_training=False, global_pool=True) - output = tf.reshape(net, [shape[0], shape[1], -1]) - return output - - -class IdentityEmbedder(Embedder): - """This embedder just returns the input as the output. - - Used for modalitites that the embedding of the modality is the same as the - modality itself. For example, it can be used for one_hot goal. - """ - - def build(self, images): - return images diff --git a/research/cognitive_planning/envs/__init__.py b/research/cognitive_planning/envs/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cognitive_planning/envs/active_vision_dataset_env.py b/research/cognitive_planning/envs/active_vision_dataset_env.py deleted file mode 100644 index 507cde76890..00000000000 --- a/research/cognitive_planning/envs/active_vision_dataset_env.py +++ /dev/null @@ -1,1097 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Gym environment for the ActiveVision Dataset. - - The dataset is captured with a robot moving around and taking picture in - multiple directions. The actions are moving in four directions, and rotate - clockwise or counter clockwise. The observations are the output of vision - pipelines such as object detectors. The goal is to find objects of interest - in each environment. For more details, refer: - http://cs.unc.edu/~ammirato/active_vision_dataset_website/. -""" -import tensorflow as tf -import collections -import copy -import json -import os -from StringIO import StringIO -import time -import gym -from gym.envs.registration import register -import gym.spaces -import networkx as nx -import numpy as np -import scipy.io as sio -from absl import logging -import gin -import cv2 -import label_map_util -import visualization_utils as vis_util -from envs import task_env - - -register( - id='active-vision-env-v0', - entry_point= - 'cognitive_planning.envs.active_vision_dataset_env:ActiveVisionDatasetEnv', # pylint: disable=line-too-long -) - -_MAX_DEPTH_VALUE = 12102 - -SUPPORTED_ACTIONS = [ - 'right', 'rotate_cw', 'rotate_ccw', 'forward', 'left', 'backward', 'stop' -] -SUPPORTED_MODALITIES = [ - task_env.ModalityTypes.SEMANTIC_SEGMENTATION, - task_env.ModalityTypes.DEPTH, - task_env.ModalityTypes.OBJECT_DETECTION, - task_env.ModalityTypes.IMAGE, - task_env.ModalityTypes.GOAL, - task_env.ModalityTypes.PREV_ACTION, - task_env.ModalityTypes.DISTANCE, -] - -# Data structure for storing the information related to the graph of the world. -_Graph = collections.namedtuple('_Graph', [ - 'graph', 'id_to_index', 'index_to_id', 'target_indexes', 'distance_to_goal' -]) - - -def _init_category_index(label_map_path): - """Creates category index from class indexes to name of the classes. - - Args: - label_map_path: path to the mapping. - Returns: - A map for mapping int keys to string categories. - """ - - label_map = label_map_util.load_labelmap(label_map_path) - num_classes = np.max(x.id for x in label_map.item) - categories = label_map_util.convert_label_map_to_categories( - label_map, max_num_classes=num_classes, use_display_name=True) - category_index = label_map_util.create_category_index(categories) - return category_index - - -def _draw_detections(image_np, detections, category_index): - """Draws detections on to the image. - - Args: - image_np: Image in the form of uint8 numpy array. - detections: a dictionary that contains the detection outputs. - category_index: contains the mapping between indexes and the category names. - - Returns: - Does not return anything but draws the boxes on the - """ - vis_util.visualize_boxes_and_labels_on_image_array( - image_np, - detections['detection_boxes'], - detections['detection_classes'], - detections['detection_scores'], - category_index, - use_normalized_coordinates=True, - max_boxes_to_draw=1000, - min_score_thresh=.0, - agnostic_mode=False) - - -def generate_detection_image(detections, - image_size, - category_map, - num_classes, - is_binary=True): - """Generates one_hot vector of the image using the detection boxes. - - Args: - detections: 2D object detections from the image. It's a dictionary that - contains detection_boxes, detection_classes, and detection_scores with - dimensions of nx4, nx1, nx1 where n is the number of detections. - image_size: The resolution of the output image. - category_map: dictionary that maps label names to index. - num_classes: Number of classes. - is_binary: If true, it sets the corresponding channels to 0 and 1. - Otherwise, sets the score in the corresponding channel. - Returns: - Returns image_size x image_size x num_classes image for the detection boxes. - """ - res = np.zeros((image_size, image_size, num_classes), dtype=np.float32) - boxes = detections['detection_boxes'] - labels = detections['detection_classes'] - scores = detections['detection_scores'] - for box, label, score in zip(boxes, labels, scores): - transformed_boxes = [int(round(t)) for t in box * image_size] - y1, x1, y2, x2 = transformed_boxes - # Detector returns fixed number of detections. Boxes with area of zero - # are equivalent of boxes that don't correspond to any detection box. - # So, we need to skip the boxes with area 0. - if (y2 - y1) * (x2 - x1) == 0: - continue - assert category_map[label] < num_classes, 'label = {}'.format(label) - value = score - if is_binary: - value = 1 - res[y1:y2, x1:x2, category_map[label]] = value - return res - - -def _get_detection_path(root, detection_folder_name, world): - return os.path.join(root, 'Meta', detection_folder_name, world + '.npy') - - -def _get_image_folder(root, world): - return os.path.join(root, world, 'jpg_rgb') - - -def _get_json_path(root, world): - return os.path.join(root, world, 'annotations.json') - - -def _get_image_path(root, world, image_id): - return os.path.join(_get_image_folder(root, world), image_id + '.jpg') - - -def _get_image_list(path, worlds): - """Builds a dictionary for all the worlds. - - Args: - path: the path to the dataset on cns. - worlds: list of the worlds. - - Returns: - dictionary where the key is the world names and the values - are the image_ids of that world. - """ - world_id_dict = {} - for loc in worlds: - files = [t[:-4] for t in tf.gfile.ListDir(_get_image_folder(path, loc))] - world_id_dict[loc] = files - return world_id_dict - - -def read_all_poses(dataset_root, world): - """Reads all the poses for each world. - - Args: - dataset_root: the path to the root of the dataset. - world: string, name of the world. - - Returns: - Dictionary of poses for all the images in each world. The key is the image - id of each view and the values are tuple of (x, z, R, scale). Where x and z - are the first and third coordinate of translation. R is the 3x3 rotation - matrix and scale is a float scalar that indicates the scale that needs to - be multipled to x and z in order to get the real world coordinates. - - Raises: - ValueError: if the number of images do not match the number of poses read. - """ - path = os.path.join(dataset_root, world, 'image_structs.mat') - with tf.gfile.Open(path) as f: - data = sio.loadmat(f) - xyz = data['image_structs']['world_pos'] - image_names = data['image_structs']['image_name'][0] - rot = data['image_structs']['R'][0] - scale = data['scale'][0][0] - n = xyz.shape[1] - x = [xyz[0][i][0][0] for i in range(n)] - z = [xyz[0][i][2][0] for i in range(n)] - names = [name[0][:-4] for name in image_names] - if len(names) != len(x): - raise ValueError('number of image names are not equal to the number of ' - 'poses {} != {}'.format(len(names), len(x))) - output = {} - for i in range(n): - if rot[i].shape[0] != 0: - assert rot[i].shape[0] == 3 - assert rot[i].shape[1] == 3 - output[names[i]] = (x[i], z[i], rot[i], scale) - else: - output[names[i]] = (x[i], z[i], None, scale) - - return output - - -def read_cached_data(should_load_images, dataset_root, segmentation_file_name, - targets_file_name, output_size): - """Reads all the necessary cached data. - - Args: - should_load_images: whether to load the images or not. - dataset_root: path to the root of the dataset. - segmentation_file_name: The name of the file that contains semantic - segmentation annotations. - targets_file_name: The name of the file the contains targets annotated for - each world. - output_size: Size of the output images. This is used for pre-processing the - loaded images. - Returns: - Dictionary of all the cached data. - """ - - load_start = time.time() - result_data = {} - - annotated_target_path = os.path.join(dataset_root, 'Meta', - targets_file_name + '.npy') - - logging.info('loading targets: %s', annotated_target_path) - with tf.gfile.Open(annotated_target_path) as f: - result_data['targets'] = np.load(f).item() - - depth_image_path = os.path.join(dataset_root, 'Meta/depth_imgs.npy') - logging.info('loading depth: %s', depth_image_path) - with tf.gfile.Open(depth_image_path) as f: - depth_data = np.load(f).item() - - logging.info('processing depth') - for home_id in depth_data: - images = depth_data[home_id] - for image_id in images: - depth = images[image_id] - depth = cv2.resize( - depth / _MAX_DEPTH_VALUE, (output_size, output_size), - interpolation=cv2.INTER_NEAREST) - depth_mask = (depth > 0).astype(np.float32) - depth = np.dstack((depth, depth_mask)) - images[image_id] = depth - result_data[task_env.ModalityTypes.DEPTH] = depth_data - - sseg_path = os.path.join(dataset_root, 'Meta', - segmentation_file_name + '.npy') - logging.info('loading sseg: %s', sseg_path) - with tf.gfile.Open(sseg_path) as f: - sseg_data = np.load(f).item() - - logging.info('processing sseg') - for home_id in sseg_data: - images = sseg_data[home_id] - for image_id in images: - sseg = images[image_id] - sseg = cv2.resize( - sseg, (output_size, output_size), interpolation=cv2.INTER_NEAREST) - images[image_id] = np.expand_dims(sseg, axis=-1).astype(np.float32) - result_data[task_env.ModalityTypes.SEMANTIC_SEGMENTATION] = sseg_data - - if should_load_images: - image_path = os.path.join(dataset_root, 'Meta/imgs.npy') - logging.info('loading imgs: %s', image_path) - with tf.gfile.Open(image_path) as f: - image_data = np.load(f).item() - - result_data[task_env.ModalityTypes.IMAGE] = image_data - - with tf.gfile.Open(os.path.join(dataset_root, 'Meta/world_id_dict.npy')) as f: - result_data['world_id_dict'] = np.load(f).item() - - logging.info('logging done in %f seconds', time.time() - load_start) - return result_data - - -@gin.configurable -def get_spec_dtype_map(): - return {gym.spaces.Box: np.float32} - - -@gin.configurable -class ActiveVisionDatasetEnv(task_env.TaskEnv): - """Simulates the environment from ActiveVisionDataset.""" - cached_data = None - - def __init__( - self, - episode_length, - modality_types, - confidence_threshold, - output_size, - worlds, - targets, - compute_distance, - should_draw_detections, - dataset_root, - labelmap_path, - reward_collision, - reward_goal_range, - num_detection_classes, - segmentation_file_name, - detection_folder_name, - actions, - targets_file_name, - eval_init_points_file_name=None, - shaped_reward=False, - ): - """Instantiates the environment for ActiveVision Dataset. - - Args: - episode_length: the length of each episode. - modality_types: a list of the strings where each entry indicates the name - of the modalities to be loaded. Valid entries are "sseg", "det", - "depth", "image", "distance", and "prev_action". "distance" should be - used for computing metrics in tf agents. - confidence_threshold: Consider detections more than confidence_threshold - for potential targets. - output_size: Resolution of the output image. - worlds: List of the name of the worlds. - targets: List of the target names. Each entry is a string label of the - target category (e.g. 'fridge', 'microwave', so on). - compute_distance: If True, outputs the distance of the view to the goal. - should_draw_detections (bool): If True, the image returned for the - observation will contains the bounding boxes. - dataset_root: the path to the root folder of the dataset. - labelmap_path: path to the dictionary that converts label strings to - indexes. - reward_collision: the reward the agents get after hitting an obstacle. - It should be a non-positive number. - reward_goal_range: the number of steps from goal, such that the agent is - considered to have reached the goal. If the agent's distance is less - than the specified goal range, the episode is also finishes by setting - done = True. - num_detection_classes: number of classes that detector outputs. - segmentation_file_name: the name of the file that contains the semantic - information. The file should be in the dataset_root/Meta/ folder. - detection_folder_name: Name of the folder that contains the detections - for each world. The folder should be under dataset_root/Meta/ folder. - actions: The list of the action names. Valid entries are listed in - SUPPORTED_ACTIONS. - targets_file_name: the name of the file that contains the annotated - targets. The file should be in the dataset_root/Meta/Folder - eval_init_points_file_name: The name of the file that contains the initial - points for evaluating the performance of the agent. If set to None, - episodes start at random locations. Should be only set for evaluation. - shaped_reward: Whether to add delta goal distance to the reward each step. - - Raises: - ValueError: If one of the targets are not available in the annotated - targets or the modality names are not from the domain specified above. - ValueError: If one of the actions is not in SUPPORTED_ACTIONS. - ValueError: If the reward_collision is a positive number. - ValueError: If there is no action other than stop provided. - """ - if reward_collision > 0: - raise ValueError('"reward" for collision should be non positive') - - if reward_goal_range < 0: - logging.warning('environment does not terminate the episode if the agent ' - 'is too close to the environment') - - if not modality_types: - raise ValueError('modality names can not be empty') - - for name in modality_types: - if name not in SUPPORTED_MODALITIES: - raise ValueError('invalid modality type: {}'.format(name)) - - actions_other_than_stop_found = False - for a in actions: - if a != 'stop': - actions_other_than_stop_found = True - if a not in SUPPORTED_ACTIONS: - raise ValueError('invalid action %s', a) - - if not actions_other_than_stop_found: - raise ValueError('environment needs to have actions other than stop.') - - super(ActiveVisionDatasetEnv, self).__init__() - - self._episode_length = episode_length - self._modality_types = set(modality_types) - self._confidence_threshold = confidence_threshold - self._output_size = output_size - self._dataset_root = dataset_root - self._worlds = worlds - self._targets = targets - self._all_graph = {} - for world in self._worlds: - with tf.gfile.Open(_get_json_path(self._dataset_root, world), 'r') as f: - file_content = f.read() - file_content = file_content.replace('.jpg', '') - io = StringIO(file_content) - self._all_graph[world] = json.load(io) - - self._cur_world = '' - self._cur_image_id = '' - self._cur_graph = None # Loaded by _update_graph - self._steps_taken = 0 - self._last_action_success = True - self._category_index = _init_category_index(labelmap_path) - self._category_map = dict( - [(c, i) for i, c in enumerate(self._category_index)]) - self._detection_cache = {} - if not ActiveVisionDatasetEnv.cached_data: - ActiveVisionDatasetEnv.cached_data = read_cached_data( - True, self._dataset_root, segmentation_file_name, targets_file_name, - self._output_size) - cached_data = ActiveVisionDatasetEnv.cached_data - - self._world_id_dict = cached_data['world_id_dict'] - self._depth_images = cached_data[task_env.ModalityTypes.DEPTH] - self._semantic_segmentations = cached_data[ - task_env.ModalityTypes.SEMANTIC_SEGMENTATION] - self._annotated_targets = cached_data['targets'] - self._cached_imgs = cached_data[task_env.ModalityTypes.IMAGE] - self._graph_cache = {} - self._compute_distance = compute_distance - self._should_draw_detections = should_draw_detections - self._reward_collision = reward_collision - self._reward_goal_range = reward_goal_range - self._num_detection_classes = num_detection_classes - self._actions = actions - self._detection_folder_name = detection_folder_name - self._shaped_reward = shaped_reward - - self._eval_init_points = None - if eval_init_points_file_name is not None: - self._eval_init_index = 0 - init_points_path = os.path.join(self._dataset_root, 'Meta', - eval_init_points_file_name + '.npy') - with tf.gfile.Open(init_points_path) as points_file: - data = np.load(points_file).item() - self._eval_init_points = [] - for world in self._worlds: - for goal in self._targets: - if world in self._annotated_targets[goal]: - for image_id in data[world]: - self._eval_init_points.append((world, image_id[0], goal)) - logging.info('loaded %d eval init points', len(self._eval_init_points)) - - self.action_space = gym.spaces.Discrete(len(self._actions)) - - obs_shapes = {} - if task_env.ModalityTypes.SEMANTIC_SEGMENTATION in self._modality_types: - obs_shapes[task_env.ModalityTypes.SEMANTIC_SEGMENTATION] = gym.spaces.Box( - low=0, high=255, shape=(self._output_size, self._output_size, 1)) - if task_env.ModalityTypes.OBJECT_DETECTION in self._modality_types: - obs_shapes[task_env.ModalityTypes.OBJECT_DETECTION] = gym.spaces.Box( - low=0, - high=255, - shape=(self._output_size, self._output_size, - self._num_detection_classes)) - if task_env.ModalityTypes.DEPTH in self._modality_types: - obs_shapes[task_env.ModalityTypes.DEPTH] = gym.spaces.Box( - low=0, - high=_MAX_DEPTH_VALUE, - shape=(self._output_size, self._output_size, 2)) - if task_env.ModalityTypes.IMAGE in self._modality_types: - obs_shapes[task_env.ModalityTypes.IMAGE] = gym.spaces.Box( - low=0, high=255, shape=(self._output_size, self._output_size, 3)) - if task_env.ModalityTypes.GOAL in self._modality_types: - obs_shapes[task_env.ModalityTypes.GOAL] = gym.spaces.Box( - low=0, high=1., shape=(len(self._targets),)) - if task_env.ModalityTypes.PREV_ACTION in self._modality_types: - obs_shapes[task_env.ModalityTypes.PREV_ACTION] = gym.spaces.Box( - low=0, high=1., shape=(len(self._actions) + 1,)) - if task_env.ModalityTypes.DISTANCE in self._modality_types: - obs_shapes[task_env.ModalityTypes.DISTANCE] = gym.spaces.Box( - low=0, high=255, shape=(1,)) - self.observation_space = gym.spaces.Dict(obs_shapes) - - self._prev_action = np.zeros((len(self._actions) + 1), dtype=np.float32) - - # Loading all the poses. - all_poses = {} - for world in self._worlds: - all_poses[world] = read_all_poses(self._dataset_root, world) - self._cached_poses = all_poses - self._vertex_to_pose = {} - self._pose_to_vertex = {} - - @property - def actions(self): - """Returns list of actions for the env.""" - return self._actions - - def _next_image(self, image_id, action): - """Given the action, returns the name of the image that agent ends up in. - - Args: - image_id: The image id of the current view. - action: valid actions are ['right', 'rotate_cw', 'rotate_ccw', - 'forward', 'left']. Each rotation is 30 degrees. - - Returns: - The image name for the next location of the agent. If the action results - in collision or it is not possible for the agent to execute that action, - returns empty string. - """ - assert action in self._actions, 'invalid action : {}'.format(action) - assert self._cur_world in self._all_graph, 'invalid world {}'.format( - self._cur_world) - assert image_id in self._all_graph[ - self._cur_world], 'image_id {} is not in {}'.format( - image_id, self._cur_world) - return self._all_graph[self._cur_world][image_id][action] - - def _largest_detection_for_image(self, image_id, detections_dict): - """Assigns area of the largest box for the view with given image id. - - Args: - image_id: Image id of the view. - detections_dict: Detections for the view. - """ - for cls, box, score in zip(detections_dict['detection_classes'], - detections_dict['detection_boxes'], - detections_dict['detection_scores']): - if cls not in self._targets: - continue - if score < self._confidence_threshold: - continue - ymin, xmin, ymax, xmax = box - area = (ymax - ymin) * (xmax - xmin) - if abs(area) < 1e-5: - continue - if image_id not in self._detection_area: - self._detection_area[image_id] = area - else: - self._detection_area[image_id] = max(self._detection_area[image_id], - area) - - def _compute_goal_indexes(self): - """Computes the goal indexes for the environment. - - Returns: - The indexes of the goals that are closest to target categories. A vertex - is goal vertice if the desired objects are detected in the image and the - target categories are not seen by moving forward from that vertice. - """ - for image_id in self._world_id_dict[self._cur_world]: - detections_dict = self._detection_table[image_id] - self._largest_detection_for_image(image_id, detections_dict) - goal_indexes = [] - for image_id in self._world_id_dict[self._cur_world]: - if image_id not in self._detection_area: - continue - # Detection box is large enough. - if self._detection_area[image_id] < 0.01: - continue - ok = True - next_image_id = self._next_image(image_id, 'forward') - if next_image_id: - if next_image_id in self._detection_area: - ok = False - if ok: - goal_indexes.append(self._cur_graph.id_to_index[image_id]) - return goal_indexes - - def to_image_id(self, vid): - """Converts vertex id to the image id. - - Args: - vid: vertex id of the view. - Returns: - image id of the input vertex id. - """ - return self._cur_graph.index_to_id[vid] - - def to_vertex(self, image_id): - return self._cur_graph.id_to_index[image_id] - - def observation(self, view_pose): - """Returns the observation at the given the vertex. - - Args: - view_pose: pose of the view of interest. - - Returns: - Observation at the given view point. - - Raises: - ValueError: if the given view pose is not similar to any of the poses in - the current world. - """ - vertex = self.pose_to_vertex(view_pose) - if vertex is None: - raise ValueError('The given found is not close enough to any of the poses' - ' in the environment.') - image_id = self._cur_graph.index_to_id[vertex] - output = collections.OrderedDict() - - if task_env.ModalityTypes.SEMANTIC_SEGMENTATION in self._modality_types: - output[task_env.ModalityTypes. - SEMANTIC_SEGMENTATION] = self._semantic_segmentations[ - self._cur_world][image_id] - - detection = None - need_det = ( - task_env.ModalityTypes.OBJECT_DETECTION in self._modality_types or - (task_env.ModalityTypes.IMAGE in self._modality_types and - self._should_draw_detections)) - if need_det: - detection = self._detection_table[image_id] - detection_image = generate_detection_image( - detection, - self._output_size, - self._category_map, - num_classes=self._num_detection_classes) - - if task_env.ModalityTypes.OBJECT_DETECTION in self._modality_types: - output[task_env.ModalityTypes.OBJECT_DETECTION] = detection_image - - if task_env.ModalityTypes.DEPTH in self._modality_types: - output[task_env.ModalityTypes.DEPTH] = self._depth_images[ - self._cur_world][image_id] - - if task_env.ModalityTypes.IMAGE in self._modality_types: - output_img = self._cached_imgs[self._cur_world][image_id] - if self._should_draw_detections: - output_img = output_img.copy() - _draw_detections(output_img, detection, self._category_index) - output[task_env.ModalityTypes.IMAGE] = output_img - - if task_env.ModalityTypes.GOAL in self._modality_types: - goal = np.zeros((len(self._targets),), dtype=np.float32) - goal[self._targets.index(self._cur_goal)] = 1. - output[task_env.ModalityTypes.GOAL] = goal - - if task_env.ModalityTypes.PREV_ACTION in self._modality_types: - output[task_env.ModalityTypes.PREV_ACTION] = self._prev_action - - if task_env.ModalityTypes.DISTANCE in self._modality_types: - output[task_env.ModalityTypes.DISTANCE] = np.asarray( - [self.gt_value(self._cur_goal, vertex)], dtype=np.float32) - - return output - - def _step_no_reward(self, action): - """Performs a step in the environment with given action. - - Args: - action: Action that is used to step in the environment. Action can be - string or integer. If the type is integer then it uses the ith element - from self._actions list. Otherwise, uses the string value as the action. - - Returns: - observation, done, info - observation: dictonary that contains all the observations specified in - modality_types. - observation[task_env.ModalityTypes.OBJECT_DETECTION]: contains the - detection of the current view. - observation[task_env.ModalityTypes.IMAGE]: contains the - image of the current view. Note that if using the images for training, - should_load_images should be set to false. - observation[task_env.ModalityTypes.SEMANTIC_SEGMENTATION]: contains the - semantic segmentation of the current view. - observation[task_env.ModalityTypes.DEPTH]: If selected, returns the - depth map for the current view. - observation[task_env.ModalityTypes.PREV_ACTION]: If selected, returns - a numpy of (action_size + 1,). The first action_size elements indicate - the action and the last element indicates whether the previous action - was successful or not. - done: True after episode_length steps have been taken, False otherwise. - info: Empty dictionary. - - Raises: - ValueError: for invalid actions. - """ - # Primarily used for gym interface. - if not isinstance(action, str): - if not self.action_space.contains(action): - raise ValueError('Not a valid actions: %d', action) - - action = self._actions[action] - - if action not in self._actions: - raise ValueError('Not a valid action: %s', action) - - action_index = self._actions.index(action) - - if action == 'stop': - next_image_id = self._cur_image_id - done = True - success = True - else: - next_image_id = self._next_image(self._cur_image_id, action) - self._steps_taken += 1 - done = False - success = True - if not next_image_id: - success = False - else: - self._cur_image_id = next_image_id - - if self._steps_taken >= self._episode_length: - done = True - - cur_vertex = self._cur_graph.id_to_index[self._cur_image_id] - observation = self.observation(self.vertex_to_pose(cur_vertex)) - - # Concatenation of one-hot prev action + a binary number for success of - # previous actions. - self._prev_action = np.zeros((len(self._actions) + 1,), dtype=np.float32) - self._prev_action[action_index] = 1. - self._prev_action[-1] = float(success) - - distance_to_goal = self.gt_value(self._cur_goal, cur_vertex) - if success: - if distance_to_goal <= self._reward_goal_range: - done = True - - return observation, done, {'success': success} - - @property - def graph(self): - return self._cur_graph.graph - - def state(self): - return self.vertex_to_pose(self.to_vertex(self._cur_image_id)) - - def gt_value(self, goal, v): - """Computes the distance to the goal from vertex v. - - Args: - goal: name of the goal. - v: vertex id. - - Returns: - Minimmum number of steps to the given goal. - """ - assert goal in self._cur_graph.distance_to_goal, 'goal: {}'.format(goal) - assert v in self._cur_graph.distance_to_goal[goal] - res = self._cur_graph.distance_to_goal[goal][v] - return res - - def _update_graph(self): - """Creates the graph for each environment and updates the _cur_graph.""" - if self._cur_world not in self._graph_cache: - graph = nx.DiGraph() - id_to_index = {} - index_to_id = {} - image_list = self._world_id_dict[self._cur_world] - for i, image_id in enumerate(image_list): - id_to_index[image_id] = i - index_to_id[i] = image_id - graph.add_node(i) - - for image_id in image_list: - for action in self._actions: - if action == 'stop': - continue - next_image = self._all_graph[self._cur_world][image_id][action] - if next_image: - graph.add_edge( - id_to_index[image_id], id_to_index[next_image], action=action) - target_indexes = {} - number_of_nodes_without_targets = graph.number_of_nodes() - distance_to_goal = {} - for goal in self._targets: - if self._cur_world not in self._annotated_targets[goal]: - continue - goal_indexes = [ - id_to_index[i] - for i in self._annotated_targets[goal][self._cur_world] - if i - ] - super_source_index = graph.number_of_nodes() - target_indexes[goal] = super_source_index - graph.add_node(super_source_index) - index_to_id[super_source_index] = goal - id_to_index[goal] = super_source_index - for v in goal_indexes: - graph.add_edge(v, super_source_index, action='stop') - graph.add_edge(super_source_index, v, action='stop') - distance_to_goal[goal] = {} - for v in range(number_of_nodes_without_targets): - distance_to_goal[goal][v] = len( - nx.shortest_path(graph, v, super_source_index)) - 2 - - self._graph_cache[self._cur_world] = _Graph( - graph, id_to_index, index_to_id, target_indexes, distance_to_goal) - self._cur_graph = self._graph_cache[self._cur_world] - - def reset_for_eval(self, new_world, new_goal, new_image_id): - """Resets to the given goal and image_id.""" - return self._reset_env(new_world=new_world, new_goal=new_goal, new_image_id=new_image_id) - - def get_init_config(self, path): - """Exposes the initial state of the agent for the given path. - - Args: - path: sequences of the vertexes that the agent moves. - - Returns: - image_id of the first view, world, and the goal. - """ - return self._cur_graph.index_to_id[path[0]], self._cur_world, self._cur_goal - - def _reset_env( - self, - new_world=None, - new_goal=None, - new_image_id=None, - ): - """Resets the agent in a random world and random id. - - Args: - new_world: If not None, sets the new world to new_world. - new_goal: If not None, sets the new goal to new_goal. - new_image_id: If not None, sets the first image id to new_image_id. - - Returns: - observation: dictionary of the observations. Content of the observation - is similar to that of the step function. - Raises: - ValueError: if it can't find a world and annotated goal. - """ - self._steps_taken = 0 - # The first prev_action is special all zero vector + success=1. - self._prev_action = np.zeros((len(self._actions) + 1,), dtype=np.float32) - self._prev_action[len(self._actions)] = 1. - if self._eval_init_points is not None: - if self._eval_init_index >= len(self._eval_init_points): - self._eval_init_index = 0 - a = self._eval_init_points[self._eval_init_index] - self._cur_world, self._cur_image_id, self._cur_goal = a - self._eval_init_index += 1 - elif not new_world: - attempts = 100 - found = False - while attempts >= 0: - attempts -= 1 - self._cur_goal = np.random.choice(self._targets) - available_worlds = list( - set(self._annotated_targets[self._cur_goal].keys()).intersection( - set(self._worlds))) - if available_worlds: - found = True - break - if not found: - raise ValueError('could not find a world that has a target annotated') - self._cur_world = np.random.choice(available_worlds) - else: - self._cur_world = new_world - self._cur_goal = new_goal - if new_world not in self._annotated_targets[new_goal]: - return None - - self._cur_goal_index = self._targets.index(self._cur_goal) - if new_image_id: - self._cur_image_id = new_image_id - else: - self._cur_image_id = np.random.choice( - self._world_id_dict[self._cur_world]) - if self._cur_world not in self._detection_cache: - with tf.gfile.Open( - _get_detection_path(self._dataset_root, self._detection_folder_name, - self._cur_world)) as f: - # Each file contains a dictionary with image ids as keys and detection - # dicts as values. - self._detection_cache[self._cur_world] = np.load(f).item() - self._detection_table = self._detection_cache[self._cur_world] - self._detection_area = {} - self._update_graph() - if self._cur_world not in self._vertex_to_pose: - # adding fake pose for the super nodes of each target categories. - self._vertex_to_pose[self._cur_world] = { - index: (-index,) for index in self._cur_graph.target_indexes.values() - } - # Calling vetex_to_pose for each vertex results in filling out the - # dictionaries that contain pose related data. - for image_id in self._world_id_dict[self._cur_world]: - self.vertex_to_pose(self.to_vertex(image_id)) - - # Filling out pose_to_vertex from vertex_to_pose. - self._pose_to_vertex[self._cur_world] = { - tuple(v): k - for k, v in self._vertex_to_pose[self._cur_world].iteritems() - } - - cur_vertex = self._cur_graph.id_to_index[self._cur_image_id] - observation = self.observation(self.vertex_to_pose(cur_vertex)) - return observation - - def cur_vertex(self): - return self._cur_graph.id_to_index[self._cur_image_id] - - def cur_image_id(self): - return self._cur_image_id - - def path_to_goal(self, image_id=None): - """Returns the path from image_id to the self._cur_goal. - - Args: - image_id: If set to None, computes the path from the current view. - Otherwise, sets the current view to the given image_id. - Returns: - The path to the goal. - Raises: - Exception if there's no path from the view to the goal. - """ - if image_id is None: - image_id = self._cur_image_id - super_source = self._cur_graph.target_indexes[self._cur_goal] - try: - path = nx.shortest_path(self._cur_graph.graph, - self._cur_graph.id_to_index[image_id], - super_source) - except: - print 'path not found, image_id = ', self._cur_world, self._cur_image_id - raise - return path[:-1] - - def targets(self): - return [self.vertex_to_pose(self._cur_graph.target_indexes[self._cur_goal])] - - def vertex_to_pose(self, v): - """Returns pose of the view for a given vertex. - - Args: - v: integer, vertex index. - - Returns: - (x, z, dir_x, dir_z) where x and z are the tranlation and dir_x, dir_z are - a vector giving direction of the view. - """ - if v in self._vertex_to_pose[self._cur_world]: - return np.copy(self._vertex_to_pose[self._cur_world][v]) - - x, z, rot, scale = self._cached_poses[self._cur_world][self.to_image_id( - v)] - if rot is None: # if rotation is not provided for the given vertex. - self._vertex_to_pose[self._cur_world][v] = np.asarray( - [x * scale, z * scale, v]) - return np.copy(self._vertex_to_pose[self._cur_world][v]) - # Multiply rotation matrix by [0,0,1] to get a vector of length 1 in the - # direction of the ray. - direction = np.zeros((3, 1), dtype=np.float32) - direction[2][0] = 1 - direction = np.matmul(np.transpose(rot), direction) - direction = [direction[0][0], direction[2][0]] - self._vertex_to_pose[self._cur_world][v] = np.asarray( - [x * scale, z * scale, direction[0], direction[1]]) - return np.copy(self._vertex_to_pose[self._cur_world][v]) - - def pose_to_vertex(self, pose): - """Returns the vertex id for the given pose.""" - if tuple(pose) not in self._pose_to_vertex[self._cur_world]: - raise ValueError( - 'The given pose is not present in the dictionary: {}'.format( - tuple(pose))) - - return self._pose_to_vertex[self._cur_world][tuple(pose)] - - def check_scene_graph(self, world, goal): - """Checks the connectivity of the scene graph. - - Goes over all the views. computes the shortest path to the goal. If it - crashes it means that it's not connected. Otherwise, the env graph is fine. - - Args: - world: the string name of the world. - goal: the string label for the goal. - Returns: - Nothing. - """ - obs = self._reset_env(new_world=world, new_goal=goal) - if not obs: - print '{} is not availble in {}'.format(goal, world) - return True - for image_id in self._world_id_dict[self._cur_world]: - print 'check image_id = {}'.format(image_id) - self._cur_image_id = image_id - path = self.path_to_goal() - actions = [] - for i in range(len(path) - 2): - actions.append(self.action(path[i], path[i + 1])) - actions.append('stop') - - @property - def goal_one_hot(self): - res = np.zeros((len(self._targets),), dtype=np.float32) - res[self._cur_goal_index] = 1. - return res - - @property - def goal_index(self): - return self._cur_goal_index - - @property - def goal_string(self): - return self._cur_goal - - @property - def worlds(self): - return self._worlds - - @property - def possible_targets(self): - return self._targets - - def action(self, from_pose, to_pose): - """Returns the action that takes source vertex to destination vertex. - - Args: - from_pose: pose of the source. - to_pose: pose of the destination. - Returns: - Returns the index of the action. - Raises: - ValueError: If it is not possible to go from the first vertice to second - vertice with one action, it raises value error. - """ - from_index = self.pose_to_vertex(from_pose) - to_index = self.pose_to_vertex(to_pose) - if to_index not in self.graph[from_index]: - from_image_id = self.to_image_id(from_index) - to_image_id = self.to_image_id(to_index) - raise ValueError('{},{} is not connected to {},{}'.format( - from_index, from_image_id, to_index, to_image_id)) - return self._actions.index(self.graph[from_index][to_index]['action']) - - def random_step_sequence(self, min_len=None, max_len=None): - """Generates random step sequence that takes agent to the goal. - - Args: - min_len: integer, minimum length of a step sequence. Not yet implemented. - max_len: integer, should be set to an integer and it is the maximum number - of observations and path length to be max_len. - Returns: - Tuple of (path, actions, states, step_outputs). - path: a random path from a random starting point and random environment. - actions: actions of the returned path. - states: viewpoints of all the states in between. - step_outputs: list of step() return tuples. - Raises: - ValueError: if first_n is not greater than zero; if min_len is different - from None. - """ - if max_len is None: - raise ValueError('max_len can not be set as None') - if max_len < 1: - raise ValueError('first_n must be greater or equal to 1.') - if min_len is not None: - raise ValueError('min_len is not yet implemented.') - - path = [] - actions = [] - states = [] - step_outputs = [] - obs = self.reset() - last_obs_tuple = [obs, 0, False, {}] - for _ in xrange(max_len): - action = np.random.choice(self._actions) - # We don't want to sample stop action because stop does not add new - # information. - while action == 'stop': - action = np.random.choice(self._actions) - path.append(self.to_vertex(self._cur_image_id)) - onehot = np.zeros((len(self._actions),), dtype=np.float32) - onehot[self._actions.index(action)] = 1. - actions.append(onehot) - states.append(self.vertex_to_pose(path[-1])) - step_outputs.append(copy.deepcopy(last_obs_tuple)) - last_obs_tuple = self.step(action) - - return path, actions, states, step_outputs diff --git a/research/cognitive_planning/envs/configs/active_vision_config.gin b/research/cognitive_planning/envs/configs/active_vision_config.gin deleted file mode 100644 index edb10dc1f53..00000000000 --- a/research/cognitive_planning/envs/configs/active_vision_config.gin +++ /dev/null @@ -1,27 +0,0 @@ -#-*-Python-*- -ActiveVisionDatasetEnv.episode_length = 200 -ActiveVisionDatasetEnv.actions = [ - 'right', 'rotate_cw', 'rotate_ccw', 'forward', 'left', 'backward', 'stop' -] -ActiveVisionDatasetEnv.confidence_threshold = 0.5 -ActiveVisionDatasetEnv.output_size = 64 -ActiveVisionDatasetEnv.worlds = [ - 'Home_001_1', 'Home_001_2', 'Home_002_1', 'Home_003_1', 'Home_003_2', - 'Home_004_1', 'Home_004_2', 'Home_005_1', 'Home_005_2', 'Home_006_1', - 'Home_007_1', 'Home_010_1', 'Home_011_1', 'Home_013_1', 'Home_014_1', - 'Home_014_2', 'Home_015_1', 'Home_016_1' -] -ActiveVisionDatasetEnv.targets = [ - 'tv', 'dining_table', 'fridge', 'microwave', 'couch' -] -ActiveVisionDatasetEnv.compute_distance = False -ActiveVisionDatasetEnv.should_draw_detections = False -ActiveVisionDatasetEnv.dataset_root = '/usr/local/google/home/kosecka/AVD_Minimal/' -ActiveVisionDatasetEnv.labelmap_path = 'label_map.txt' -ActiveVisionDatasetEnv.reward_collision = 0 -ActiveVisionDatasetEnv.reward_goal_range = 2 -ActiveVisionDatasetEnv.num_detection_classes = 90 -ActiveVisionDatasetEnv.segmentation_file_name='sseg_crf' -ActiveVisionDatasetEnv.detection_folder_name='Detections' -ActiveVisionDatasetEnv.targets_file_name='annotated_targets' -ActiveVisionDatasetEnv.shaped_reward=False diff --git a/research/cognitive_planning/envs/task_env.py b/research/cognitive_planning/envs/task_env.py deleted file mode 100644 index 84d527cd2e4..00000000000 --- a/research/cognitive_planning/envs/task_env.py +++ /dev/null @@ -1,218 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""An interface representing the topology of an environment. - -Allows for high level planning and high level instruction generation for -navigation tasks. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc -import enum -import gym -import gin - - -@gin.config.constants_from_enum -class ModalityTypes(enum.Enum): - """Types of the modalities that can be used.""" - IMAGE = 0 - SEMANTIC_SEGMENTATION = 1 - OBJECT_DETECTION = 2 - DEPTH = 3 - GOAL = 4 - PREV_ACTION = 5 - PREV_SUCCESS = 6 - STATE = 7 - DISTANCE = 8 - CAN_STEP = 9 - - def __lt__(self, other): - if self.__class__ is other.__class__: - return self.value < other.value - return NotImplemented - - -class TaskEnvInterface(object): - """Interface for an environment topology. - - An environment can implement this interface if there is a topological graph - underlying this environment. All paths below are defined as paths in this - graph. Using path_to_actions function one can translate a topological path - to a geometric path in the environment. - """ - - __metaclass__ = abc.ABCMeta - - @abc.abstractmethod - def random_step_sequence(self, min_len=None, max_len=None): - """Generates a random sequence of actions and executes them. - - Args: - min_len: integer, minimum length of a step sequence. - max_len: integer, if it is set to non-None, the method returns only - the first n steps of a random sequence. If the environment is - computationally heavy this argument should be set to speed up the - training and avoid unnecessary computations by the environment. - - Returns: - A path, defined as a list of vertex indices, a list of actions, a list of - states, and a list of step() return tuples. - """ - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - @abc.abstractmethod - def targets(self): - """A list of targets in the environment. - - Returns: - A list of target locations. - """ - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - @abc.abstractproperty - def state(self): - """Returns the position for the current location of agent.""" - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - @abc.abstractproperty - def graph(self): - """Returns a graph representing the environment topology. - - Returns: - nx.Graph object. - """ - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - @abc.abstractmethod - def vertex_to_pose(self, vertex_index): - """Maps a vertex index to a pose in the environment. - - Pose of the camera can be represented by (x,y,theta) or (x,y,z,theta). - Args: - vertex_index: index of a vertex in the topology graph. - - Returns: - A np.array of floats of size 3 or 4 representing the pose of the vertex. - """ - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - @abc.abstractmethod - def pose_to_vertex(self, pose): - """Maps a coordinate in the maze to the closest vertex in topology graph. - - Args: - pose: np.array of floats containing a the pose of the view. - - Returns: - index of a vertex. - """ - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - @abc.abstractmethod - def observation(self, state): - """Returns observation at location xy and orientation theta. - - Args: - state: a np.array of floats containing coordinates of a location and - orientation. - - Returns: - Dictionary of observations in the case of multiple observations. - The keys are the modality names and the values are the np.array of float - of observations for corresponding modality. - """ - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - def action(self, init_state, final_state): - """Computes the transition action from state1 to state2. - - If the environment is discrete and the views are not adjacent in the - environment. i.e. it is not possible to move from the first view to the - second view with one action it should return None. In the continuous case, - it will be the continuous difference of first view and second view. - - Args: - init_state: numpy array, the initial view of the agent. - final_state: numpy array, the final view of the agent. - """ - raise NotImplementedError( - 'Needs implementation as part of EnvTopology interface.') - - -@gin.configurable -class TaskEnv(gym.Env, TaskEnvInterface): - """An environment which uses a Task to compute reward. - - The environment implements a a gym interface, as well as EnvTopology. The - former makes sure it can be used within an RL training, while the latter - makes sure it can be used by a Task. - - This environment requires _step_no_reward to be implemented, which steps - through it but does not return reward. Instead, the reward calculation is - delegated to the Task object, which in return can access needed properties - of the environment. These properties are exposed via the EnvTopology - interface. - """ - - def __init__(self, task=None): - self._task = task - - def set_task(self, task): - self._task = task - - @abc.abstractmethod - def _step_no_reward(self, action): - """Same as _step without returning reward. - - Args: - action: see _step. - - Returns: - state, done, info as defined in _step. - """ - raise NotImplementedError('Implement step.') - - @abc.abstractmethod - def _reset_env(self): - """Resets the environment. Returns initial observation.""" - raise NotImplementedError('Implement _reset. Must call super!') - - def step(self, action): - obs, done, info = self._step_no_reward(action) - - reward = 0.0 - if self._task is not None: - obs, reward, done, info = self._task.reward(obs, done, info) - - return obs, reward, done, info - - def reset(self): - """Resets the environment. Gym API.""" - obs = self._reset_env() - if self._task is not None: - self._task.reset(obs) - return obs diff --git a/research/cognitive_planning/envs/util.py b/research/cognitive_planning/envs/util.py deleted file mode 100644 index 32a384bc5fa..00000000000 --- a/research/cognitive_planning/envs/util.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A module with utility functions. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np - - -def trajectory_to_deltas(trajectory, state): - """Computes a sequence of deltas of a state to traverse a trajectory in 2D. - - The initial state of the agent contains its pose -- location in 2D and - orientation. When the computed deltas are incrementally added to it, it - traverses the specified trajectory while keeping its orientation parallel to - the trajectory. - - Args: - trajectory: a np.array of floats of shape n x 2. The n-th row contains the - n-th point. - state: a 3 element np.array of floats containing agent's location and - orientation in radians. - - Returns: - A np.array of floats of size n x 3. - """ - state = np.reshape(state, [-1]) - init_xy = state[0:2] - init_theta = state[2] - - delta_xy = trajectory - np.concatenate( - [np.reshape(init_xy, [1, 2]), trajectory[:-1, :]], axis=0) - - thetas = np.reshape(np.arctan2(delta_xy[:, 1], delta_xy[:, 0]), [-1, 1]) - thetas = np.concatenate([np.reshape(init_theta, [1, 1]), thetas], axis=0) - delta_thetas = thetas[1:] - thetas[:-1] - - deltas = np.concatenate([delta_xy, delta_thetas], axis=1) - return deltas diff --git a/research/cognitive_planning/label_map.txt b/research/cognitive_planning/label_map.txt deleted file mode 100644 index 1f4872bd0c7..00000000000 --- a/research/cognitive_planning/label_map.txt +++ /dev/null @@ -1,400 +0,0 @@ -item { - name: "/m/01g317" - id: 1 - display_name: "person" -} -item { - name: "/m/0199g" - id: 2 - display_name: "bicycle" -} -item { - name: "/m/0k4j" - id: 3 - display_name: "car" -} -item { - name: "/m/04_sv" - id: 4 - display_name: "motorcycle" -} -item { - name: "/m/05czz6l" - id: 5 - display_name: "airplane" -} -item { - name: "/m/01bjv" - id: 6 - display_name: "bus" -} -item { - name: "/m/07jdr" - id: 7 - display_name: "train" -} -item { - name: "/m/07r04" - id: 8 - display_name: "truck" -} -item { - name: "/m/019jd" - id: 9 - display_name: "boat" -} -item { - name: "/m/015qff" - id: 10 - display_name: "traffic light" -} -item { - name: "/m/01pns0" - id: 11 - display_name: "fire hydrant" -} -item { - name: "/m/02pv19" - id: 13 - display_name: "stop sign" -} -item { - name: "/m/015qbp" - id: 14 - display_name: "parking meter" -} -item { - name: "/m/0cvnqh" - id: 15 - display_name: "bench" -} -item { - name: "/m/015p6" - id: 16 - display_name: "bird" -} -item { - name: "/m/01yrx" - id: 17 - display_name: "cat" -} -item { - name: "/m/0bt9lr" - id: 18 - display_name: "dog" -} -item { - name: "/m/03k3r" - id: 19 - display_name: "horse" -} -item { - name: "/m/07bgp" - id: 20 - display_name: "sheep" -} -item { - name: "/m/01xq0k1" - id: 21 - display_name: "cow" -} -item { - name: "/m/0bwd_0j" - id: 22 - display_name: "elephant" -} -item { - name: "/m/01dws" - id: 23 - display_name: "bear" -} -item { - name: "/m/0898b" - id: 24 - display_name: "zebra" -} -item { - name: "/m/03bk1" - id: 25 - display_name: "giraffe" -} -item { - name: "/m/01940j" - id: 27 - display_name: "backpack" -} -item { - name: "/m/0hnnb" - id: 28 - display_name: "umbrella" -} -item { - name: "/m/080hkjn" - id: 31 - display_name: "handbag" -} -item { - name: "/m/01rkbr" - id: 32 - display_name: "tie" -} -item { - name: "/m/01s55n" - id: 33 - display_name: "suitcase" -} -item { - name: "/m/02wmf" - id: 34 - display_name: "frisbee" -} -item { - name: "/m/071p9" - id: 35 - display_name: "skis" -} -item { - name: "/m/06__v" - id: 36 - display_name: "snowboard" -} -item { - name: "/m/018xm" - id: 37 - display_name: "sports ball" -} -item { - name: "/m/02zt3" - id: 38 - display_name: "kite" -} -item { - name: "/m/03g8mr" - id: 39 - display_name: "baseball bat" -} -item { - name: "/m/03grzl" - id: 40 - display_name: "baseball glove" -} -item { - name: "/m/06_fw" - id: 41 - display_name: "skateboard" -} -item { - name: "/m/019w40" - id: 42 - display_name: "surfboard" -} -item { - name: "/m/0dv9c" - id: 43 - display_name: "tennis racket" -} -item { - name: "/m/04dr76w" - id: 44 - display_name: "bottle" -} -item { - name: "/m/09tvcd" - id: 46 - display_name: "wine glass" -} -item { - name: "/m/08gqpm" - id: 47 - display_name: "cup" -} -item { - name: "/m/0dt3t" - id: 48 - display_name: "fork" -} -item { - name: "/m/04ctx" - id: 49 - display_name: "knife" -} -item { - name: "/m/0cmx8" - id: 50 - display_name: "spoon" -} -item { - name: "/m/04kkgm" - id: 51 - display_name: "bowl" -} -item { - name: "/m/09qck" - id: 52 - display_name: "banana" -} -item { - name: "/m/014j1m" - id: 53 - display_name: "apple" -} -item { - name: "/m/0l515" - id: 54 - display_name: "sandwich" -} -item { - name: "/m/0cyhj_" - id: 55 - display_name: "orange" -} -item { - name: "/m/0hkxq" - id: 56 - display_name: "broccoli" -} -item { - name: "/m/0fj52s" - id: 57 - display_name: "carrot" -} -item { - name: "/m/01b9xk" - id: 58 - display_name: "hot dog" -} -item { - name: "/m/0663v" - id: 59 - display_name: "pizza" -} -item { - name: "/m/0jy4k" - id: 60 - display_name: "donut" -} -item { - name: "/m/0fszt" - id: 61 - display_name: "cake" -} -item { - name: "/m/01mzpv" - id: 62 - display_name: "chair" -} -item { - name: "/m/02crq1" - id: 63 - display_name: "couch" -} -item { - name: "/m/03fp41" - id: 64 - display_name: "potted plant" -} -item { - name: "/m/03ssj5" - id: 65 - display_name: "bed" -} -item { - name: "/m/04bcr3" - id: 67 - display_name: "dining table" -} -item { - name: "/m/09g1w" - id: 70 - display_name: "toilet" -} -item { - name: "/m/07c52" - id: 72 - display_name: "tv" -} -item { - name: "/m/01c648" - id: 73 - display_name: "laptop" -} -item { - name: "/m/020lf" - id: 74 - display_name: "mouse" -} -item { - name: "/m/0qjjc" - id: 75 - display_name: "remote" -} -item { - name: "/m/01m2v" - id: 76 - display_name: "keyboard" -} -item { - name: "/m/050k8" - id: 77 - display_name: "cell phone" -} -item { - name: "/m/0fx9l" - id: 78 - display_name: "microwave" -} -item { - name: "/m/029bxz" - id: 79 - display_name: "oven" -} -item { - name: "/m/01k6s3" - id: 80 - display_name: "toaster" -} -item { - name: "/m/0130jx" - id: 81 - display_name: "sink" -} -item { - name: "/m/040b_t" - id: 82 - display_name: "refrigerator" -} -item { - name: "/m/0bt_c3" - id: 84 - display_name: "book" -} -item { - name: "/m/01x3z" - id: 85 - display_name: "clock" -} -item { - name: "/m/02s195" - id: 86 - display_name: "vase" -} -item { - name: "/m/01lsmm" - id: 87 - display_name: "scissors" -} -item { - name: "/m/0kmg4" - id: 88 - display_name: "teddy bear" -} -item { - name: "/m/03wvsk" - id: 89 - display_name: "hair drier" -} -item { - name: "/m/012xff" - id: 90 - display_name: "toothbrush" -} diff --git a/research/cognitive_planning/label_map_util.py b/research/cognitive_planning/label_map_util.py deleted file mode 100644 index e258e3ab57f..00000000000 --- a/research/cognitive_planning/label_map_util.py +++ /dev/null @@ -1,181 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Label map utility functions.""" - -import logging - -import tensorflow as tf -from google.protobuf import text_format -import string_int_label_map_pb2 - - -def _validate_label_map(label_map): - """Checks if a label map is valid. - - Args: - label_map: StringIntLabelMap to validate. - - Raises: - ValueError: if label map is invalid. - """ - for item in label_map.item: - if item.id < 0: - raise ValueError('Label map ids should be >= 0.') - if (item.id == 0 and item.name != 'background' and - item.display_name != 'background'): - raise ValueError('Label map id 0 is reserved for the background label') - - -def create_category_index(categories): - """Creates dictionary of COCO compatible categories keyed by category id. - - Args: - categories: a list of dicts, each of which has the following keys: - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name - e.g., 'cat', 'dog', 'pizza'. - - Returns: - category_index: a dict containing the same entries as categories, but keyed - by the 'id' field of each category. - """ - category_index = {} - for cat in categories: - category_index[cat['id']] = cat - return category_index - - -def get_max_label_map_index(label_map): - """Get maximum index in label map. - - Args: - label_map: a StringIntLabelMapProto - - Returns: - an integer - """ - return max([item.id for item in label_map.item]) - - -def convert_label_map_to_categories(label_map, - max_num_classes, - use_display_name=True): - """Loads label map proto and returns categories list compatible with eval. - - This function loads a label map and returns a list of dicts, each of which - has the following keys: - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name - e.g., 'cat', 'dog', 'pizza'. - We only allow class into the list if its id-label_id_offset is - between 0 (inclusive) and max_num_classes (exclusive). - If there are several items mapping to the same id in the label map, - we will only keep the first one in the categories list. - - Args: - label_map: a StringIntLabelMapProto or None. If None, a default categories - list is created with max_num_classes categories. - max_num_classes: maximum number of (consecutive) label indices to include. - use_display_name: (boolean) choose whether to load 'display_name' field - as category name. If False or if the display_name field does not exist, - uses 'name' field as category names instead. - Returns: - categories: a list of dictionaries representing all possible categories. - """ - categories = [] - list_of_ids_already_added = [] - if not label_map: - label_id_offset = 1 - for class_id in range(max_num_classes): - categories.append({ - 'id': class_id + label_id_offset, - 'name': 'category_{}'.format(class_id + label_id_offset) - }) - return categories - for item in label_map.item: - if not 0 < item.id <= max_num_classes: - logging.info('Ignore item %d since it falls outside of requested ' - 'label range.', item.id) - continue - if use_display_name and item.HasField('display_name'): - name = item.display_name - else: - name = item.name - if item.id not in list_of_ids_already_added: - list_of_ids_already_added.append(item.id) - categories.append({'id': item.id, 'name': name}) - return categories - - -def load_labelmap(path): - """Loads label map proto. - - Args: - path: path to StringIntLabelMap proto text file. - Returns: - a StringIntLabelMapProto - """ - with tf.gfile.GFile(path, 'r') as fid: - label_map_string = fid.read() - label_map = string_int_label_map_pb2.StringIntLabelMap() - try: - text_format.Merge(label_map_string, label_map) - except text_format.ParseError: - label_map.ParseFromString(label_map_string) - _validate_label_map(label_map) - return label_map - - -def get_label_map_dict(label_map_path, use_display_name=False): - """Reads a label map and returns a dictionary of label names to id. - - Args: - label_map_path: path to label_map. - use_display_name: whether to use the label map items' display names as keys. - - Returns: - A dictionary mapping label names to id. - """ - label_map = load_labelmap(label_map_path) - label_map_dict = {} - for item in label_map.item: - if use_display_name: - label_map_dict[item.display_name] = item.id - else: - label_map_dict[item.name] = item.id - return label_map_dict - - -def create_category_index_from_labelmap(label_map_path): - """Reads a label map and returns a category index. - - Args: - label_map_path: Path to `StringIntLabelMap` proto text file. - - Returns: - A category index, which is a dictionary that maps integer ids to dicts - containing categories, e.g. - {1: {'id': 1, 'name': 'dog'}, 2: {'id': 2, 'name': 'cat'}, ...} - """ - label_map = load_labelmap(label_map_path) - max_num_classes = max(item.id for item in label_map.item) - categories = convert_label_map_to_categories(label_map, max_num_classes) - return create_category_index(categories) - - -def create_class_agnostic_category_index(): - """Creates a category index with a single `object` class.""" - return {1: {'id': 1, 'name': 'object'}} diff --git a/research/cognitive_planning/policies.py b/research/cognitive_planning/policies.py deleted file mode 100644 index 5c7e2207db1..00000000000 --- a/research/cognitive_planning/policies.py +++ /dev/null @@ -1,474 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Interface for the policy of the agents use for navigation.""" - -import abc -import tensorflow as tf -from absl import logging -import embedders -from envs import task_env - -slim = tf.contrib.slim - -def _print_debug_ios(history, goal, output): - """Prints sizes of history, goal and outputs.""" - if history is not None: - shape = history.get_shape().as_list() - # logging.info('history embedding shape ') - # logging.info(shape) - if len(shape) != 3: - raise ValueError('history Tensor must have rank=3') - if goal is not None: - logging.info('goal embedding shape ') - logging.info(goal.get_shape().as_list()) - if output is not None: - logging.info('targets shape ') - logging.info(output.get_shape().as_list()) - - -class Policy(object): - """Represents the policy of the agent for navigation tasks. - - Instantiates a policy that takes embedders for each modality and builds a - model to infer the actions. - """ - __metaclass__ = abc.ABCMeta - - def __init__(self, embedders_dict, action_size): - """Instantiates the policy. - - Args: - embedders_dict: Dictionary of embedders for different modalities. Keys - should be identical to keys of observation modality. - action_size: Number of possible actions. - """ - self._embedders = embedders_dict - self._action_size = action_size - - @abc.abstractmethod - def build(self, observations, prev_state): - """Builds the model that represents the policy of the agent. - - Args: - observations: Dictionary of observations from different modalities. Keys - are the name of the modalities. - prev_state: The tensor of the previous state of the model. Should be set - to None if the policy is stateless - Returns: - Tuple of (action, state) where action is the action logits and state is - the state of the model after taking new observation. - """ - raise NotImplementedError( - 'Needs implementation as part of Policy interface') - - -class LSTMPolicy(Policy): - """Represents the implementation of the LSTM based policy. - - The architecture of the model is as follows. It embeds all the observations - using the embedders, concatenates the embeddings of all the modalities. Feed - them through two fully connected layers. The lstm takes the features from - fully connected layer and the previous action and success of previous action - and feed them to LSTM. The value for each action is predicted afterwards. - Although the class name has the word LSTM in it, it also supports a mode that - builds the network without LSTM just for comparison purposes. - """ - - def __init__(self, - modality_names, - embedders_dict, - action_size, - params, - max_episode_length, - feedforward_mode=False): - """Instantiates the LSTM policy. - - Args: - modality_names: List of modality names. Makes sure the ordering in - concatenation remains the same as modality_names list. Each modality - needs to be in the embedders_dict. - embedders_dict: Dictionary of embedders for different modalities. Keys - should be identical to keys of observation modality. Values should be - instance of Embedder class. All the observations except PREV_ACTION - requires embedder. - action_size: Number of possible actions. - params: is instance of tf.hparams and contains the hyperparameters for the - policy network. - max_episode_length: integer, specifying the maximum length of each - episode. - feedforward_mode: If True, it does not add LSTM to the model. It should - only be set True for comparison between LSTM and feedforward models. - """ - super(LSTMPolicy, self).__init__(embedders_dict, action_size) - - self._modality_names = modality_names - - self._lstm_state_size = params.lstm_state_size - self._fc_channels = params.fc_channels - self._weight_decay = params.weight_decay - self._target_embedding_size = params.target_embedding_size - self._max_episode_length = max_episode_length - self._feedforward_mode = feedforward_mode - - def _build_lstm(self, encoded_inputs, prev_state, episode_length, - prev_action=None): - """Builds an LSTM on top of the encoded inputs. - - If prev_action is not None then it concatenates them to the input of LSTM. - - Args: - encoded_inputs: The embedding of the observations and goal. - prev_state: previous state of LSTM. - episode_length: The tensor that contains the length of the sequence for - each element of the batch. - prev_action: tensor to previous chosen action and additional bit for - indicating whether the previous action was successful or not. - - Returns: - a tuple of (lstm output, lstm state). - """ - - # Adding prev action and success in addition to the embeddings of the - # modalities. - if prev_action is not None: - encoded_inputs = tf.concat([encoded_inputs, prev_action], axis=-1) - - with tf.variable_scope('LSTM'): - lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(self._lstm_state_size) - if prev_state is None: - # If prev state is set to None, a state of all zeros will be - # passed as a previous value for the cell. Should be used for the - # first step of each episode. - tf_prev_state = lstm_cell.zero_state( - encoded_inputs.get_shape().as_list()[0], dtype=tf.float32) - else: - tf_prev_state = tf.nn.rnn_cell.LSTMStateTuple(prev_state[0], - prev_state[1]) - - lstm_outputs, lstm_state = tf.nn.dynamic_rnn( - cell=lstm_cell, - inputs=encoded_inputs, - sequence_length=episode_length, - initial_state=tf_prev_state, - dtype=tf.float32, - ) - lstm_outputs = tf.reshape(lstm_outputs, [-1, lstm_cell.output_size]) - return lstm_outputs, lstm_state - - def build( - self, - observations, - prev_state, - ): - """Builds the model that represents the policy of the agent. - - Args: - observations: Dictionary of observations from different modalities. Keys - are the name of the modalities. Observation should have the following - key-values. - observations['goal']: One-hot tensor that indicates the semantic - category of the goal. The shape should be - (batch_size x max_sequence_length x goals). - observations[task_env.ModalityTypes.PREV_ACTION]: has action_size + 1 - elements where the first action_size numbers are the one hot vector - of the previous action and the last element indicates whether the - previous action was successful or not. If - task_env.ModalityTypes.PREV_ACTION is not in the observation, it - will not be used in the policy. - prev_state: Previous state of the model. It should be a tuple of (c,h) - where c and h are the previous cell value and hidden state of the lstm. - Each element of tuple has shape of (batch_size x lstm_cell_size). - If it is set to None, then it initializes the state of the lstm with all - zeros. - - Returns: - Tuple of (action, state) where action is the action logits and state is - the state of the model after taking new observation. - Raises: - ValueError: If any of the modality names is not in observations or - embedders_dict. - ValueError: If 'goal' is not in the observations. - """ - - for modality_name in self._modality_names: - if modality_name not in observations: - raise ValueError('modality name does not exist in observations: {} not ' - 'in {}'.format(modality_name, observations.keys())) - if modality_name not in self._embedders: - if modality_name == task_env.ModalityTypes.PREV_ACTION: - continue - raise ValueError('modality name does not have corresponding embedder' - ' {} not in {}'.format(modality_name, - self._embedders.keys())) - - if task_env.ModalityTypes.GOAL not in observations: - raise ValueError('goal should be provided in the observations') - - goal = observations[task_env.ModalityTypes.GOAL] - prev_action = None - if task_env.ModalityTypes.PREV_ACTION in observations: - prev_action = observations[task_env.ModalityTypes.PREV_ACTION] - - with tf.variable_scope('policy'): - with slim.arg_scope( - [slim.fully_connected], - activation_fn=tf.nn.relu, - weights_initializer=tf.truncated_normal_initializer(stddev=0.01), - weights_regularizer=slim.l2_regularizer(self._weight_decay)): - all_inputs = [] - - # Concatenating the embedding of each modality by applying the embedders - # to corresponding observations. - def embed(name): - with tf.variable_scope('embed_{}'.format(name)): - # logging.info('Policy uses embedding %s', name) - return self._embedders[name].build(observations[name]) - - all_inputs = map(embed, [ - x for x in self._modality_names - if x != task_env.ModalityTypes.PREV_ACTION - ]) - - # Computing goal embedding. - shape = goal.get_shape().as_list() - with tf.variable_scope('embed_goal'): - encoded_goal = tf.reshape(goal, [shape[0] * shape[1], -1]) - encoded_goal = slim.fully_connected(encoded_goal, - self._target_embedding_size) - encoded_goal = tf.reshape(encoded_goal, [shape[0], shape[1], -1]) - all_inputs.append(encoded_goal) - - # Concatenating all the modalities and goal. - all_inputs = tf.concat(all_inputs, axis=-1, name='concat_embeddings') - - shape = all_inputs.get_shape().as_list() - all_inputs = tf.reshape(all_inputs, [shape[0] * shape[1], shape[2]]) - - # Applying fully connected layers. - encoded_inputs = slim.fully_connected(all_inputs, self._fc_channels) - encoded_inputs = slim.fully_connected(encoded_inputs, self._fc_channels) - - if not self._feedforward_mode: - encoded_inputs = tf.reshape(encoded_inputs, - [shape[0], shape[1], self._fc_channels]) - lstm_outputs, lstm_state = self._build_lstm( - encoded_inputs=encoded_inputs, - prev_state=prev_state, - episode_length=tf.ones((shape[0],), dtype=tf.float32) * - self._max_episode_length, - prev_action=prev_action, - ) - else: - # If feedforward_mode=True, directly compute bypass the whole LSTM - # computations. - lstm_outputs = encoded_inputs - - lstm_outputs = slim.fully_connected(lstm_outputs, self._fc_channels) - action_values = slim.fully_connected( - lstm_outputs, self._action_size, activation_fn=None) - action_values = tf.reshape(action_values, [shape[0], shape[1], -1]) - if not self._feedforward_mode: - return action_values, lstm_state - else: - return action_values, None - - -class TaskPolicy(Policy): - """A covenience abstract class providing functionality to deal with Tasks.""" - - def __init__(self, - task_config, - model_hparams=None, - embedder_hparams=None, - train_hparams=None): - """Constructs a policy which knows how to work with tasks (see tasks.py). - - It allows to read task history, goal and outputs in consistency with the - task config. - - Args: - task_config: an object of type tasks.TaskIOConfig (see tasks.py) - model_hparams: a tf.HParams object containing parameter pertaining to - model (these are implementation specific) - embedder_hparams: a tf.HParams object containing parameter pertaining to - history, goal embedders (these are implementation specific) - train_hparams: a tf.HParams object containing parameter pertaining to - trainin (these are implementation specific)` - """ - super(TaskPolicy, self).__init__(None, None) - self._model_hparams = model_hparams - self._embedder_hparams = embedder_hparams - self._train_hparams = train_hparams - self._task_config = task_config - self._extra_train_ops = [] - - @property - def extra_train_ops(self): - """Training ops in addition to the loss, e.g. batch norm updates. - - Returns: - A list of tf ops. - """ - return self._extra_train_ops - - def _embed_task_ios(self, streams): - """Embeds a list of heterogenous streams. - - These streams correspond to task history, goal and output. The number of - streams is equal to the total number of history, plus one for the goal if - present, plus one for the output. If the number of history is k, then the - first k streams are the history. - - The used embedders depend on the input (or goal) types. If an input is an - image, then a ResNet embedder is used, otherwise - MLPEmbedder (see embedders.py). - - Args: - streams: a list of Tensors. - Returns: - Three float Tensors history, goal, output. If there are no history, or no - goal, then the corresponding returned values are None. The shape of the - embedded history is batch_size x sequence_length x sum of all embedding - dimensions for all history. The shape of the goal is embedding dimension. - """ - # EMBED history. - index = 0 - inps = [] - scopes = [] - for c in self._task_config.inputs: - if c == task_env.ModalityTypes.IMAGE: - scope_name = 'image_embedder/image' - reuse = scope_name in scopes - scopes.append(scope_name) - with tf.variable_scope(scope_name, reuse=reuse): - resnet_embedder = embedders.ResNet(self._embedder_hparams.image) - image_embeddings = resnet_embedder.build(streams[index]) - # Uncover batch norm ops. - if self._embedder_hparams.image.is_train: - self._extra_train_ops += resnet_embedder.extra_train_ops - inps.append(image_embeddings) - index += 1 - else: - scope_name = 'input_embedder/vector' - reuse = scope_name in scopes - scopes.append(scope_name) - with tf.variable_scope(scope_name, reuse=reuse): - input_vector_embedder = embedders.MLPEmbedder( - layers=self._embedder_hparams.vector) - vector_embedder = input_vector_embedder.build(streams[index]) - inps.append(vector_embedder) - index += 1 - history = tf.concat(inps, axis=2) if inps else None - - # EMBED goal. - goal = None - if self._task_config.query is not None: - scope_name = 'image_embedder/query' - reuse = scope_name in scopes - scopes.append(scope_name) - with tf.variable_scope(scope_name, reuse=reuse): - resnet_goal_embedder = embedders.ResNet(self._embedder_hparams.goal) - goal = resnet_goal_embedder.build(streams[index]) - if self._embedder_hparams.goal.is_train: - self._extra_train_ops += resnet_goal_embedder.extra_train_ops - index += 1 - - # Embed true targets if needed (tbd). - true_target = streams[index] - - return history, goal, true_target - - @abc.abstractmethod - def build(self, feeds, prev_state): - pass - - -class ReactivePolicy(TaskPolicy): - """A policy which ignores history. - - It processes only the current observation (last element in history) and the - goal to output a prediction. - """ - - def __init__(self, *args, **kwargs): - super(ReactivePolicy, self).__init__(*args, **kwargs) - - # The current implementation ignores the prev_state as it is purely reactive. - # It returns None for the current state. - def build(self, feeds, prev_state): - history, goal, _ = self._embed_task_ios(feeds) - _print_debug_ios(history, goal, None) - - with tf.variable_scope('output_decoder'): - # Concatenate the embeddings of the current observation and the goal. - reactive_input = tf.concat([tf.squeeze(history[:, -1, :]), goal], axis=1) - oconfig = self._task_config.output.shape - assert len(oconfig) == 1 - decoder = embedders.MLPEmbedder( - layers=self._embedder_hparams.predictions.layer_sizes + oconfig) - predictions = decoder.build(reactive_input) - - return predictions, None - - -class RNNPolicy(TaskPolicy): - """A policy which takes into account the full history via RNN. - - The implementation might and will change. - The history, together with the goal, is processed using a stacked LSTM. The - output of the last LSTM step is used to produce a prediction. Currently, only - a single step output is supported. - """ - - def __init__(self, lstm_hparams, *args, **kwargs): - super(RNNPolicy, self).__init__(*args, **kwargs) - self._lstm_hparams = lstm_hparams - - # The prev_state is ignored as for now the full history is specified as first - # element of the feeds. It might turn out to be beneficial to keep the state - # as part of the policy object. - def build(self, feeds, state): - history, goal, _ = self._embed_task_ios(feeds) - _print_debug_ios(history, goal, None) - - params = self._lstm_hparams - cell = lambda: tf.contrib.rnn.BasicLSTMCell(params.cell_size) - stacked_lstm = tf.contrib.rnn.MultiRNNCell( - [cell() for _ in range(params.num_layers)]) - # history is of shape batch_size x seq_len x embedding_dimension - batch_size, seq_len, _ = tuple(history.get_shape().as_list()) - - if state is None: - state = stacked_lstm.zero_state(batch_size, tf.float32) - for t in range(seq_len): - if params.concat_goal_everywhere: - lstm_input = tf.concat([tf.squeeze(history[:, t, :]), goal], axis=1) - else: - lstm_input = tf.squeeze(history[:, t, :]) - output, state = stacked_lstm(lstm_input, state) - - with tf.variable_scope('output_decoder'): - oconfig = self._task_config.output.shape - assert len(oconfig) == 1 - features = tf.concat([output, goal], axis=1) - assert len(output.get_shape().as_list()) == 2 - assert len(goal.get_shape().as_list()) == 2 - decoder = embedders.MLPEmbedder( - layers=self._embedder_hparams.predictions.layer_sizes + oconfig) - # Prediction is done off the last step lstm output and the goal. - predictions = decoder.build(features) - - return predictions, state diff --git a/research/cognitive_planning/preprocessing/__init__.py b/research/cognitive_planning/preprocessing/__init__.py deleted file mode 100644 index 8b137891791..00000000000 --- a/research/cognitive_planning/preprocessing/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/research/cognitive_planning/preprocessing/cifarnet_preprocessing.py b/research/cognitive_planning/preprocessing/cifarnet_preprocessing.py deleted file mode 100644 index 0b5a88fa4c3..00000000000 --- a/research/cognitive_planning/preprocessing/cifarnet_preprocessing.py +++ /dev/null @@ -1,128 +0,0 @@ -# Copyright 2016 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Provides utilities to preprocess images in CIFAR-10. - -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -_PADDING = 4 - -slim = tf.contrib.slim - - -def preprocess_for_train(image, - output_height, - output_width, - padding=_PADDING, - add_image_summaries=True): - """Preprocesses the given image for training. - - Note that the actual resizing scale is sampled from - [`resize_size_min`, `resize_size_max`]. - - Args: - image: A `Tensor` representing an image of arbitrary size. - output_height: The height of the image after preprocessing. - output_width: The width of the image after preprocessing. - padding: The amound of padding before and after each dimension of the image. - add_image_summaries: Enable image summaries. - - Returns: - A preprocessed image. - """ - if add_image_summaries: - tf.summary.image('image', tf.expand_dims(image, 0)) - - # Transform the image to floats. - image = tf.to_float(image) - if padding > 0: - image = tf.pad(image, [[padding, padding], [padding, padding], [0, 0]]) - # Randomly crop a [height, width] section of the image. - distorted_image = tf.random_crop(image, - [output_height, output_width, 3]) - - # Randomly flip the image horizontally. - distorted_image = tf.image.random_flip_left_right(distorted_image) - - if add_image_summaries: - tf.summary.image('distorted_image', tf.expand_dims(distorted_image, 0)) - - # Because these operations are not commutative, consider randomizing - # the order their operation. - distorted_image = tf.image.random_brightness(distorted_image, - max_delta=63) - distorted_image = tf.image.random_contrast(distorted_image, - lower=0.2, upper=1.8) - # Subtract off the mean and divide by the variance of the pixels. - return tf.image.per_image_standardization(distorted_image) - - -def preprocess_for_eval(image, output_height, output_width, - add_image_summaries=True): - """Preprocesses the given image for evaluation. - - Args: - image: A `Tensor` representing an image of arbitrary size. - output_height: The height of the image after preprocessing. - output_width: The width of the image after preprocessing. - add_image_summaries: Enable image summaries. - - Returns: - A preprocessed image. - """ - if add_image_summaries: - tf.summary.image('image', tf.expand_dims(image, 0)) - # Transform the image to floats. - image = tf.to_float(image) - - # Resize and crop if needed. - resized_image = tf.image.resize_image_with_crop_or_pad(image, - output_width, - output_height) - if add_image_summaries: - tf.summary.image('resized_image', tf.expand_dims(resized_image, 0)) - - # Subtract off the mean and divide by the variance of the pixels. - return tf.image.per_image_standardization(resized_image) - - -def preprocess_image(image, output_height, output_width, is_training=False, - add_image_summaries=True): - """Preprocesses the given image. - - Args: - image: A `Tensor` representing an image of arbitrary size. - output_height: The height of the image after preprocessing. - output_width: The width of the image after preprocessing. - is_training: `True` if we're preprocessing the image for training and - `False` otherwise. - add_image_summaries: Enable image summaries. - - Returns: - A preprocessed image. - """ - if is_training: - return preprocess_for_train( - image, output_height, output_width, - add_image_summaries=add_image_summaries) - else: - return preprocess_for_eval( - image, output_height, output_width, - add_image_summaries=add_image_summaries) diff --git a/research/cognitive_planning/preprocessing/inception_preprocessing.py b/research/cognitive_planning/preprocessing/inception_preprocessing.py deleted file mode 100644 index 846b81b3cd3..00000000000 --- a/research/cognitive_planning/preprocessing/inception_preprocessing.py +++ /dev/null @@ -1,318 +0,0 @@ -# Copyright 2016 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Provides utilities to preprocess images for the Inception networks.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from tensorflow.python.ops import control_flow_ops - - -def apply_with_random_selector(x, func, num_cases): - """Computes func(x, sel), with sel sampled from [0...num_cases-1]. - - Args: - x: input Tensor. - func: Python function to apply. - num_cases: Python int32, number of cases to sample sel from. - - Returns: - The result of func(x, sel), where func receives the value of the - selector as a python integer, but sel is sampled dynamically. - """ - sel = tf.random_uniform([], maxval=num_cases, dtype=tf.int32) - # Pass the real x only to one of the func calls. - return control_flow_ops.merge([ - func(control_flow_ops.switch(x, tf.equal(sel, case))[1], case) - for case in range(num_cases)])[0] - - -def distort_color(image, color_ordering=0, fast_mode=True, scope=None): - """Distort the color of a Tensor image. - - Each color distortion is non-commutative and thus ordering of the color ops - matters. Ideally we would randomly permute the ordering of the color ops. - Rather then adding that level of complication, we select a distinct ordering - of color ops for each preprocessing thread. - - Args: - image: 3-D Tensor containing single image in [0, 1]. - color_ordering: Python int, a type of distortion (valid values: 0-3). - fast_mode: Avoids slower ops (random_hue and random_contrast) - scope: Optional scope for name_scope. - Returns: - 3-D Tensor color-distorted image on range [0, 1] - Raises: - ValueError: if color_ordering not in [0, 3] - """ - with tf.name_scope(scope, 'distort_color', [image]): - if fast_mode: - if color_ordering == 0: - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - else: - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - else: - if color_ordering == 0: - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_hue(image, max_delta=0.2) - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - elif color_ordering == 1: - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - image = tf.image.random_hue(image, max_delta=0.2) - elif color_ordering == 2: - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - image = tf.image.random_hue(image, max_delta=0.2) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - elif color_ordering == 3: - image = tf.image.random_hue(image, max_delta=0.2) - image = tf.image.random_saturation(image, lower=0.5, upper=1.5) - image = tf.image.random_contrast(image, lower=0.5, upper=1.5) - image = tf.image.random_brightness(image, max_delta=32. / 255.) - else: - raise ValueError('color_ordering must be in [0, 3]') - - # The random_* ops do not necessarily clamp. - return tf.clip_by_value(image, 0.0, 1.0) - - -def distorted_bounding_box_crop(image, - bbox, - min_object_covered=0.1, - aspect_ratio_range=(0.75, 1.33), - area_range=(0.05, 1.0), - max_attempts=100, - scope=None): - """Generates cropped_image using a one of the bboxes randomly distorted. - - See `tf.image.sample_distorted_bounding_box` for more documentation. - - Args: - image: 3-D Tensor of image (it will be converted to floats in [0, 1]). - bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] - where each coordinate is [0, 1) and the coordinates are arranged - as [ymin, xmin, ymax, xmax]. If num_boxes is 0 then it would use the whole - image. - min_object_covered: An optional `float`. Defaults to `0.1`. The cropped - area of the image must contain at least this fraction of any bounding box - supplied. - aspect_ratio_range: An optional list of `floats`. The cropped area of the - image must have an aspect ratio = width / height within this range. - area_range: An optional list of `floats`. The cropped area of the image - must contain a fraction of the supplied image within in this range. - max_attempts: An optional `int`. Number of attempts at generating a cropped - region of the image of the specified constraints. After `max_attempts` - failures, return the entire image. - scope: Optional scope for name_scope. - Returns: - A tuple, a 3-D Tensor cropped_image and the distorted bbox - """ - with tf.name_scope(scope, 'distorted_bounding_box_crop', [image, bbox]): - # Each bounding box has shape [1, num_boxes, box coords] and - # the coordinates are ordered [ymin, xmin, ymax, xmax]. - - # A large fraction of image datasets contain a human-annotated bounding - # box delineating the region of the image containing the object of interest. - # We choose to create a new bounding box for the object which is a randomly - # distorted version of the human-annotated bounding box that obeys an - # allowed range of aspect ratios, sizes and overlap with the human-annotated - # bounding box. If no box is supplied, then we assume the bounding box is - # the entire image. - sample_distorted_bounding_box = tf.image.sample_distorted_bounding_box( - tf.shape(image), - bounding_boxes=bbox, - min_object_covered=min_object_covered, - aspect_ratio_range=aspect_ratio_range, - area_range=area_range, - max_attempts=max_attempts, - use_image_if_no_bounding_boxes=True) - bbox_begin, bbox_size, distort_bbox = sample_distorted_bounding_box - - # Crop the image to the specified bounding box. - cropped_image = tf.slice(image, bbox_begin, bbox_size) - return cropped_image, distort_bbox - - -def preprocess_for_train(image, height, width, bbox, - fast_mode=True, - scope=None, - add_image_summaries=True): - """Distort one image for training a network. - - Distorting images provides a useful technique for augmenting the data - set during training in order to make the network invariant to aspects - of the image that do not effect the label. - - Additionally it would create image_summaries to display the different - transformations applied to the image. - - Args: - image: 3-D Tensor of image. If dtype is tf.float32 then the range should be - [0, 1], otherwise it would converted to tf.float32 assuming that the range - is [0, MAX], where MAX is largest positive representable number for - int(8/16/32) data type (see `tf.image.convert_image_dtype` for details). - height: integer - width: integer - bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] - where each coordinate is [0, 1) and the coordinates are arranged - as [ymin, xmin, ymax, xmax]. - fast_mode: Optional boolean, if True avoids slower transformations (i.e. - bi-cubic resizing, random_hue or random_contrast). - scope: Optional scope for name_scope. - add_image_summaries: Enable image summaries. - Returns: - 3-D float Tensor of distorted image used for training with range [-1, 1]. - """ - with tf.name_scope(scope, 'distort_image', [image, height, width, bbox]): - if bbox is None: - bbox = tf.constant([0.0, 0.0, 1.0, 1.0], - dtype=tf.float32, - shape=[1, 1, 4]) - if image.dtype != tf.float32: - image = tf.image.convert_image_dtype(image, dtype=tf.float32) - # Each bounding box has shape [1, num_boxes, box coords] and - # the coordinates are ordered [ymin, xmin, ymax, xmax]. - image_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(image, 0), - bbox) - if add_image_summaries: - tf.summary.image('image_with_bounding_boxes', image_with_box) - - distorted_image, distorted_bbox = distorted_bounding_box_crop(image, bbox) - # Restore the shape since the dynamic slice based upon the bbox_size loses - # the third dimension. - distorted_image.set_shape([None, None, 3]) - image_with_distorted_box = tf.image.draw_bounding_boxes( - tf.expand_dims(image, 0), distorted_bbox) - if add_image_summaries: - tf.summary.image('images_with_distorted_bounding_box', - image_with_distorted_box) - - # This resizing operation may distort the images because the aspect - # ratio is not respected. We select a resize method in a round robin - # fashion based on the thread number. - # Note that ResizeMethod contains 4 enumerated resizing methods. - - # We select only 1 case for fast_mode bilinear. - num_resize_cases = 1 if fast_mode else 4 - distorted_image = apply_with_random_selector( - distorted_image, - lambda x, method: tf.image.resize_images(x, [height, width], method), - num_cases=num_resize_cases) - - if add_image_summaries: - tf.summary.image('cropped_resized_image', - tf.expand_dims(distorted_image, 0)) - - # Randomly flip the image horizontally. - distorted_image = tf.image.random_flip_left_right(distorted_image) - - # Randomly distort the colors. There are 1 or 4 ways to do it. - num_distort_cases = 1 if fast_mode else 4 - distorted_image = apply_with_random_selector( - distorted_image, - lambda x, ordering: distort_color(x, ordering, fast_mode), - num_cases=num_distort_cases) - - if add_image_summaries: - tf.summary.image('final_distorted_image', - tf.expand_dims(distorted_image, 0)) - distorted_image = tf.subtract(distorted_image, 0.5) - distorted_image = tf.multiply(distorted_image, 2.0) - return distorted_image - - -def preprocess_for_eval(image, height, width, - central_fraction=0.875, scope=None): - """Prepare one image for evaluation. - - If height and width are specified it would output an image with that size by - applying resize_bilinear. - - If central_fraction is specified it would crop the central fraction of the - input image. - - Args: - image: 3-D Tensor of image. If dtype is tf.float32 then the range should be - [0, 1], otherwise it would converted to tf.float32 assuming that the range - is [0, MAX], where MAX is largest positive representable number for - int(8/16/32) data type (see `tf.image.convert_image_dtype` for details). - height: integer - width: integer - central_fraction: Optional Float, fraction of the image to crop. - scope: Optional scope for name_scope. - Returns: - 3-D float Tensor of prepared image. - """ - with tf.name_scope(scope, 'eval_image', [image, height, width]): - if image.dtype != tf.float32: - image = tf.image.convert_image_dtype(image, dtype=tf.float32) - # Crop the central region of the image with an area containing 87.5% of - # the original image. - if central_fraction: - image = tf.image.central_crop(image, central_fraction=central_fraction) - - if height and width: - # Resize the image to the specified height and width. - image = tf.expand_dims(image, 0) - image = tf.image.resize_bilinear(image, [height, width], - align_corners=False) - image = tf.squeeze(image, [0]) - image = tf.subtract(image, 0.5) - image = tf.multiply(image, 2.0) - return image - - -def preprocess_image(image, height, width, - is_training=False, - bbox=None, - fast_mode=True, - add_image_summaries=True): - """Pre-process one image for training or evaluation. - - Args: - image: 3-D Tensor [height, width, channels] with the image. If dtype is - tf.float32 then the range should be [0, 1], otherwise it would converted - to tf.float32 assuming that the range is [0, MAX], where MAX is largest - positive representable number for int(8/16/32) data type (see - `tf.image.convert_image_dtype` for details). - height: integer, image expected height. - width: integer, image expected width. - is_training: Boolean. If true it would transform an image for train, - otherwise it would transform it for evaluation. - bbox: 3-D float Tensor of bounding boxes arranged [1, num_boxes, coords] - where each coordinate is [0, 1) and the coordinates are arranged as - [ymin, xmin, ymax, xmax]. - fast_mode: Optional boolean, if True avoids slower transformations. - add_image_summaries: Enable image summaries. - - Returns: - 3-D float Tensor containing an appropriately scaled image - - Raises: - ValueError: if user does not provide bounding box - """ - if is_training: - return preprocess_for_train(image, height, width, bbox, fast_mode, - add_image_summaries=add_image_summaries) - else: - return preprocess_for_eval(image, height, width) diff --git a/research/cognitive_planning/preprocessing/lenet_preprocessing.py b/research/cognitive_planning/preprocessing/lenet_preprocessing.py deleted file mode 100644 index ac5e71af889..00000000000 --- a/research/cognitive_planning/preprocessing/lenet_preprocessing.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2016 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Provides utilities for preprocessing.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -slim = tf.contrib.slim - - -def preprocess_image(image, output_height, output_width, is_training): - """Preprocesses the given image. - - Args: - image: A `Tensor` representing an image of arbitrary size. - output_height: The height of the image after preprocessing. - output_width: The width of the image after preprocessing. - is_training: `True` if we're preprocessing the image for training and - `False` otherwise. - - Returns: - A preprocessed image. - """ - image = tf.to_float(image) - image = tf.image.resize_image_with_crop_or_pad( - image, output_width, output_height) - image = tf.subtract(image, 128.0) - image = tf.div(image, 128.0) - return image diff --git a/research/cognitive_planning/preprocessing/preprocessing_factory.py b/research/cognitive_planning/preprocessing/preprocessing_factory.py deleted file mode 100644 index 1bd04c252a6..00000000000 --- a/research/cognitive_planning/preprocessing/preprocessing_factory.py +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright 2016 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Contains a factory for building various models.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from preprocessing import cifarnet_preprocessing -from preprocessing import inception_preprocessing -from preprocessing import lenet_preprocessing -from preprocessing import vgg_preprocessing - -slim = tf.contrib.slim - - -def get_preprocessing(name, is_training=False): - """Returns preprocessing_fn(image, height, width, **kwargs). - - Args: - name: The name of the preprocessing function. - is_training: `True` if the model is being used for training and `False` - otherwise. - - Returns: - preprocessing_fn: A function that preprocessing a single image (pre-batch). - It has the following signature: - image = preprocessing_fn(image, output_height, output_width, ...). - - Raises: - ValueError: If Preprocessing `name` is not recognized. - """ - preprocessing_fn_map = { - 'cifarnet': cifarnet_preprocessing, - 'inception': inception_preprocessing, - 'inception_v1': inception_preprocessing, - 'inception_v2': inception_preprocessing, - 'inception_v3': inception_preprocessing, - 'inception_v4': inception_preprocessing, - 'inception_resnet_v2': inception_preprocessing, - 'lenet': lenet_preprocessing, - 'mobilenet_v1': inception_preprocessing, - 'nasnet_mobile': inception_preprocessing, - 'nasnet_large': inception_preprocessing, - 'pnasnet_large': inception_preprocessing, - 'resnet_v1_50': vgg_preprocessing, - 'resnet_v1_101': vgg_preprocessing, - 'resnet_v1_152': vgg_preprocessing, - 'resnet_v1_200': vgg_preprocessing, - 'resnet_v2_50': vgg_preprocessing, - 'resnet_v2_101': vgg_preprocessing, - 'resnet_v2_152': vgg_preprocessing, - 'resnet_v2_200': vgg_preprocessing, - 'vgg': vgg_preprocessing, - 'vgg_a': vgg_preprocessing, - 'vgg_16': vgg_preprocessing, - 'vgg_19': vgg_preprocessing, - } - - if name not in preprocessing_fn_map: - raise ValueError('Preprocessing name [%s] was not recognized' % name) - - def preprocessing_fn(image, output_height, output_width, **kwargs): - return preprocessing_fn_map[name].preprocess_image( - image, output_height, output_width, is_training=is_training, **kwargs) - - return preprocessing_fn diff --git a/research/cognitive_planning/preprocessing/vgg_preprocessing.py b/research/cognitive_planning/preprocessing/vgg_preprocessing.py deleted file mode 100644 index 3bd50598cde..00000000000 --- a/research/cognitive_planning/preprocessing/vgg_preprocessing.py +++ /dev/null @@ -1,365 +0,0 @@ -# Copyright 2016 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Provides utilities to preprocess images. - -The preprocessing steps for VGG were introduced in the following technical -report: - - Very Deep Convolutional Networks For Large-Scale Image Recognition - Karen Simonyan and Andrew Zisserman - arXiv technical report, 2015 - PDF: http://arxiv.org/pdf/1409.1556.pdf - ILSVRC 2014 Slides: http://www.robots.ox.ac.uk/~karen/pdf/ILSVRC_2014.pdf - CC-BY-4.0 - -More information can be obtained from the VGG website: -www.robots.ox.ac.uk/~vgg/research/very_deep/ -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -slim = tf.contrib.slim - -_R_MEAN = 123.68 -_G_MEAN = 116.78 -_B_MEAN = 103.94 - -_RESIZE_SIDE_MIN = 256 -_RESIZE_SIDE_MAX = 512 - - -def _crop(image, offset_height, offset_width, crop_height, crop_width): - """Crops the given image using the provided offsets and sizes. - - Note that the method doesn't assume we know the input image size but it does - assume we know the input image rank. - - Args: - image: an image of shape [height, width, channels]. - offset_height: a scalar tensor indicating the height offset. - offset_width: a scalar tensor indicating the width offset. - crop_height: the height of the cropped image. - crop_width: the width of the cropped image. - - Returns: - the cropped (and resized) image. - - Raises: - InvalidArgumentError: if the rank is not 3 or if the image dimensions are - less than the crop size. - """ - original_shape = tf.shape(image) - - rank_assertion = tf.Assert( - tf.equal(tf.rank(image), 3), - ['Rank of image must be equal to 3.']) - with tf.control_dependencies([rank_assertion]): - cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]]) - - size_assertion = tf.Assert( - tf.logical_and( - tf.greater_equal(original_shape[0], crop_height), - tf.greater_equal(original_shape[1], crop_width)), - ['Crop size greater than the image size.']) - - offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) - - # Use tf.slice instead of crop_to_bounding box as it accepts tensors to - # define the crop size. - with tf.control_dependencies([size_assertion]): - image = tf.slice(image, offsets, cropped_shape) - return tf.reshape(image, cropped_shape) - - -def _random_crop(image_list, crop_height, crop_width): - """Crops the given list of images. - - The function applies the same crop to each image in the list. This can be - effectively applied when there are multiple image inputs of the same - dimension such as: - - image, depths, normals = _random_crop([image, depths, normals], 120, 150) - - Args: - image_list: a list of image tensors of the same dimension but possibly - varying channel. - crop_height: the new height. - crop_width: the new width. - - Returns: - the image_list with cropped images. - - Raises: - ValueError: if there are multiple image inputs provided with different size - or the images are smaller than the crop dimensions. - """ - if not image_list: - raise ValueError('Empty image_list.') - - # Compute the rank assertions. - rank_assertions = [] - for i in range(len(image_list)): - image_rank = tf.rank(image_list[i]) - rank_assert = tf.Assert( - tf.equal(image_rank, 3), - ['Wrong rank for tensor %s [expected] [actual]', - image_list[i].name, 3, image_rank]) - rank_assertions.append(rank_assert) - - with tf.control_dependencies([rank_assertions[0]]): - image_shape = tf.shape(image_list[0]) - image_height = image_shape[0] - image_width = image_shape[1] - crop_size_assert = tf.Assert( - tf.logical_and( - tf.greater_equal(image_height, crop_height), - tf.greater_equal(image_width, crop_width)), - ['Crop size greater than the image size.']) - - asserts = [rank_assertions[0], crop_size_assert] - - for i in range(1, len(image_list)): - image = image_list[i] - asserts.append(rank_assertions[i]) - with tf.control_dependencies([rank_assertions[i]]): - shape = tf.shape(image) - height = shape[0] - width = shape[1] - - height_assert = tf.Assert( - tf.equal(height, image_height), - ['Wrong height for tensor %s [expected][actual]', - image.name, height, image_height]) - width_assert = tf.Assert( - tf.equal(width, image_width), - ['Wrong width for tensor %s [expected][actual]', - image.name, width, image_width]) - asserts.extend([height_assert, width_assert]) - - # Create a random bounding box. - # - # Use tf.random_uniform and not numpy.random.rand as doing the former would - # generate random numbers at graph eval time, unlike the latter which - # generates random numbers at graph definition time. - with tf.control_dependencies(asserts): - max_offset_height = tf.reshape(image_height - crop_height + 1, []) - with tf.control_dependencies(asserts): - max_offset_width = tf.reshape(image_width - crop_width + 1, []) - offset_height = tf.random_uniform( - [], maxval=max_offset_height, dtype=tf.int32) - offset_width = tf.random_uniform( - [], maxval=max_offset_width, dtype=tf.int32) - - return [_crop(image, offset_height, offset_width, - crop_height, crop_width) for image in image_list] - - -def _central_crop(image_list, crop_height, crop_width): - """Performs central crops of the given image list. - - Args: - image_list: a list of image tensors of the same dimension but possibly - varying channel. - crop_height: the height of the image following the crop. - crop_width: the width of the image following the crop. - - Returns: - the list of cropped images. - """ - outputs = [] - for image in image_list: - image_height = tf.shape(image)[0] - image_width = tf.shape(image)[1] - - offset_height = (image_height - crop_height) / 2 - offset_width = (image_width - crop_width) / 2 - - outputs.append(_crop(image, offset_height, offset_width, - crop_height, crop_width)) - return outputs - - -def _mean_image_subtraction(image, means): - """Subtracts the given means from each image channel. - - For example: - means = [123.68, 116.779, 103.939] - image = _mean_image_subtraction(image, means) - - Note that the rank of `image` must be known. - - Args: - image: a tensor of size [height, width, C]. - means: a C-vector of values to subtract from each channel. - - Returns: - the centered image. - - Raises: - ValueError: If the rank of `image` is unknown, if `image` has a rank other - than three or if the number of channels in `image` doesn't match the - number of values in `means`. - """ - if image.get_shape().ndims != 3: - raise ValueError('Input must be of size [height, width, C>0]') - num_channels = image.get_shape().as_list()[-1] - if len(means) != num_channels: - raise ValueError('len(means) must match the number of channels') - - channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image) - for i in range(num_channels): - channels[i] -= means[i] - return tf.concat(axis=2, values=channels) - - -def _smallest_size_at_least(height, width, smallest_side): - """Computes new shape with the smallest side equal to `smallest_side`. - - Computes new shape with the smallest side equal to `smallest_side` while - preserving the original aspect ratio. - - Args: - height: an int32 scalar tensor indicating the current height. - width: an int32 scalar tensor indicating the current width. - smallest_side: A python integer or scalar `Tensor` indicating the size of - the smallest side after resize. - - Returns: - new_height: an int32 scalar tensor indicating the new height. - new_width: and int32 scalar tensor indicating the new width. - """ - smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32) - - height = tf.to_float(height) - width = tf.to_float(width) - smallest_side = tf.to_float(smallest_side) - - scale = tf.cond(tf.greater(height, width), - lambda: smallest_side / width, - lambda: smallest_side / height) - new_height = tf.to_int32(tf.rint(height * scale)) - new_width = tf.to_int32(tf.rint(width * scale)) - return new_height, new_width - - -def _aspect_preserving_resize(image, smallest_side): - """Resize images preserving the original aspect ratio. - - Args: - image: A 3-D image `Tensor`. - smallest_side: A python integer or scalar `Tensor` indicating the size of - the smallest side after resize. - - Returns: - resized_image: A 3-D tensor containing the resized image. - """ - smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32) - - shape = tf.shape(image) - height = shape[0] - width = shape[1] - new_height, new_width = _smallest_size_at_least(height, width, smallest_side) - image = tf.expand_dims(image, 0) - resized_image = tf.image.resize_bilinear(image, [new_height, new_width], - align_corners=False) - resized_image = tf.squeeze(resized_image) - resized_image.set_shape([None, None, 3]) - return resized_image - - -def preprocess_for_train(image, - output_height, - output_width, - resize_side_min=_RESIZE_SIDE_MIN, - resize_side_max=_RESIZE_SIDE_MAX): - """Preprocesses the given image for training. - - Note that the actual resizing scale is sampled from - [`resize_size_min`, `resize_size_max`]. - - Args: - image: A `Tensor` representing an image of arbitrary size. - output_height: The height of the image after preprocessing. - output_width: The width of the image after preprocessing. - resize_side_min: The lower bound for the smallest side of the image for - aspect-preserving resizing. - resize_side_max: The upper bound for the smallest side of the image for - aspect-preserving resizing. - - Returns: - A preprocessed image. - """ - resize_side = tf.random_uniform( - [], minval=resize_side_min, maxval=resize_side_max+1, dtype=tf.int32) - - image = _aspect_preserving_resize(image, resize_side) - image = _random_crop([image], output_height, output_width)[0] - image.set_shape([output_height, output_width, 3]) - image = tf.to_float(image) - image = tf.image.random_flip_left_right(image) - return _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN]) - - -def preprocess_for_eval(image, output_height, output_width, resize_side): - """Preprocesses the given image for evaluation. - - Args: - image: A `Tensor` representing an image of arbitrary size. - output_height: The height of the image after preprocessing. - output_width: The width of the image after preprocessing. - resize_side: The smallest side of the image for aspect-preserving resizing. - - Returns: - A preprocessed image. - """ - image = _aspect_preserving_resize(image, resize_side) - image = _central_crop([image], output_height, output_width)[0] - image.set_shape([output_height, output_width, 3]) - image = tf.to_float(image) - return _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN]) - - -def preprocess_image(image, output_height, output_width, is_training=False, - resize_side_min=_RESIZE_SIDE_MIN, - resize_side_max=_RESIZE_SIDE_MAX): - """Preprocesses the given image. - - Args: - image: A `Tensor` representing an image of arbitrary size. - output_height: The height of the image after preprocessing. - output_width: The width of the image after preprocessing. - is_training: `True` if we're preprocessing the image for training and - `False` otherwise. - resize_side_min: The lower bound for the smallest side of the image for - aspect-preserving resizing. If `is_training` is `False`, then this value - is used for rescaling. - resize_side_max: The upper bound for the smallest side of the image for - aspect-preserving resizing. If `is_training` is `False`, this value is - ignored. Otherwise, the resize side is sampled from - [resize_size_min, resize_size_max]. - - Returns: - A preprocessed image. - """ - if is_training: - return preprocess_for_train(image, output_height, output_width, - resize_side_min, resize_side_max) - else: - return preprocess_for_eval(image, output_height, output_width, - resize_side_min) diff --git a/research/cognitive_planning/standard_fields.py b/research/cognitive_planning/standard_fields.py deleted file mode 100644 index 99e04e66c56..00000000000 --- a/research/cognitive_planning/standard_fields.py +++ /dev/null @@ -1,224 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Contains classes specifying naming conventions used for object detection. - - -Specifies: - InputDataFields: standard fields used by reader/preprocessor/batcher. - DetectionResultFields: standard fields returned by object detector. - BoxListFields: standard field used by BoxList - TfExampleFields: standard fields for tf-example data format (go/tf-example). -""" - - -class InputDataFields(object): - """Names for the input tensors. - - Holds the standard data field names to use for identifying input tensors. This - should be used by the decoder to identify keys for the returned tensor_dict - containing input tensors. And it should be used by the model to identify the - tensors it needs. - - Attributes: - image: image. - image_additional_channels: additional channels. - original_image: image in the original input size. - key: unique key corresponding to image. - source_id: source of the original image. - filename: original filename of the dataset (without common path). - groundtruth_image_classes: image-level class labels. - groundtruth_boxes: coordinates of the ground truth boxes in the image. - groundtruth_classes: box-level class labels. - groundtruth_label_types: box-level label types (e.g. explicit negative). - groundtruth_is_crowd: [DEPRECATED, use groundtruth_group_of instead] - is the groundtruth a single object or a crowd. - groundtruth_area: area of a groundtruth segment. - groundtruth_difficult: is a `difficult` object - groundtruth_group_of: is a `group_of` objects, e.g. multiple objects of the - same class, forming a connected group, where instances are heavily - occluding each other. - proposal_boxes: coordinates of object proposal boxes. - proposal_objectness: objectness score of each proposal. - groundtruth_instance_masks: ground truth instance masks. - groundtruth_instance_boundaries: ground truth instance boundaries. - groundtruth_instance_classes: instance mask-level class labels. - groundtruth_keypoints: ground truth keypoints. - groundtruth_keypoint_visibilities: ground truth keypoint visibilities. - groundtruth_label_scores: groundtruth label scores. - groundtruth_weights: groundtruth weight factor for bounding boxes. - num_groundtruth_boxes: number of groundtruth boxes. - true_image_shapes: true shapes of images in the resized images, as resized - images can be padded with zeros. - multiclass_scores: the label score per class for each box. - """ - image = 'image' - image_additional_channels = 'image_additional_channels' - original_image = 'original_image' - key = 'key' - source_id = 'source_id' - filename = 'filename' - groundtruth_image_classes = 'groundtruth_image_classes' - groundtruth_boxes = 'groundtruth_boxes' - groundtruth_classes = 'groundtruth_classes' - groundtruth_label_types = 'groundtruth_label_types' - groundtruth_is_crowd = 'groundtruth_is_crowd' - groundtruth_area = 'groundtruth_area' - groundtruth_difficult = 'groundtruth_difficult' - groundtruth_group_of = 'groundtruth_group_of' - proposal_boxes = 'proposal_boxes' - proposal_objectness = 'proposal_objectness' - groundtruth_instance_masks = 'groundtruth_instance_masks' - groundtruth_instance_boundaries = 'groundtruth_instance_boundaries' - groundtruth_instance_classes = 'groundtruth_instance_classes' - groundtruth_keypoints = 'groundtruth_keypoints' - groundtruth_keypoint_visibilities = 'groundtruth_keypoint_visibilities' - groundtruth_label_scores = 'groundtruth_label_scores' - groundtruth_weights = 'groundtruth_weights' - num_groundtruth_boxes = 'num_groundtruth_boxes' - true_image_shape = 'true_image_shape' - multiclass_scores = 'multiclass_scores' - - -class DetectionResultFields(object): - """Naming conventions for storing the output of the detector. - - Attributes: - source_id: source of the original image. - key: unique key corresponding to image. - detection_boxes: coordinates of the detection boxes in the image. - detection_scores: detection scores for the detection boxes in the image. - detection_classes: detection-level class labels. - detection_masks: contains a segmentation mask for each detection box. - detection_boundaries: contains an object boundary for each detection box. - detection_keypoints: contains detection keypoints for each detection box. - num_detections: number of detections in the batch. - """ - - source_id = 'source_id' - key = 'key' - detection_boxes = 'detection_boxes' - detection_scores = 'detection_scores' - detection_classes = 'detection_classes' - detection_masks = 'detection_masks' - detection_boundaries = 'detection_boundaries' - detection_keypoints = 'detection_keypoints' - num_detections = 'num_detections' - - -class BoxListFields(object): - """Naming conventions for BoxLists. - - Attributes: - boxes: bounding box coordinates. - classes: classes per bounding box. - scores: scores per bounding box. - weights: sample weights per bounding box. - objectness: objectness score per bounding box. - masks: masks per bounding box. - boundaries: boundaries per bounding box. - keypoints: keypoints per bounding box. - keypoint_heatmaps: keypoint heatmaps per bounding box. - is_crowd: is_crowd annotation per bounding box. - """ - boxes = 'boxes' - classes = 'classes' - scores = 'scores' - weights = 'weights' - objectness = 'objectness' - masks = 'masks' - boundaries = 'boundaries' - keypoints = 'keypoints' - keypoint_heatmaps = 'keypoint_heatmaps' - is_crowd = 'is_crowd' - - -class TfExampleFields(object): - """TF-example proto feature names for object detection. - - Holds the standard feature names to load from an Example proto for object - detection. - - Attributes: - image_encoded: JPEG encoded string - image_format: image format, e.g. "JPEG" - filename: filename - channels: number of channels of image - colorspace: colorspace, e.g. "RGB" - height: height of image in pixels, e.g. 462 - width: width of image in pixels, e.g. 581 - source_id: original source of the image - image_class_text: image-level label in text format - image_class_label: image-level label in numerical format - object_class_text: labels in text format, e.g. ["person", "cat"] - object_class_label: labels in numbers, e.g. [16, 8] - object_bbox_xmin: xmin coordinates of groundtruth box, e.g. 10, 30 - object_bbox_xmax: xmax coordinates of groundtruth box, e.g. 50, 40 - object_bbox_ymin: ymin coordinates of groundtruth box, e.g. 40, 50 - object_bbox_ymax: ymax coordinates of groundtruth box, e.g. 80, 70 - object_view: viewpoint of object, e.g. ["frontal", "left"] - object_truncated: is object truncated, e.g. [true, false] - object_occluded: is object occluded, e.g. [true, false] - object_difficult: is object difficult, e.g. [true, false] - object_group_of: is object a single object or a group of objects - object_depiction: is object a depiction - object_is_crowd: [DEPRECATED, use object_group_of instead] - is the object a single object or a crowd - object_segment_area: the area of the segment. - object_weight: a weight factor for the object's bounding box. - instance_masks: instance segmentation masks. - instance_boundaries: instance boundaries. - instance_classes: Classes for each instance segmentation mask. - detection_class_label: class label in numbers. - detection_bbox_ymin: ymin coordinates of a detection box. - detection_bbox_xmin: xmin coordinates of a detection box. - detection_bbox_ymax: ymax coordinates of a detection box. - detection_bbox_xmax: xmax coordinates of a detection box. - detection_score: detection score for the class label and box. - """ - image_encoded = 'image/encoded' - image_format = 'image/format' # format is reserved keyword - filename = 'image/filename' - channels = 'image/channels' - colorspace = 'image/colorspace' - height = 'image/height' - width = 'image/width' - source_id = 'image/source_id' - image_class_text = 'image/class/text' - image_class_label = 'image/class/label' - object_class_text = 'image/object/class/text' - object_class_label = 'image/object/class/label' - object_bbox_ymin = 'image/object/bbox/ymin' - object_bbox_xmin = 'image/object/bbox/xmin' - object_bbox_ymax = 'image/object/bbox/ymax' - object_bbox_xmax = 'image/object/bbox/xmax' - object_view = 'image/object/view' - object_truncated = 'image/object/truncated' - object_occluded = 'image/object/occluded' - object_difficult = 'image/object/difficult' - object_group_of = 'image/object/group_of' - object_depiction = 'image/object/depiction' - object_is_crowd = 'image/object/is_crowd' - object_segment_area = 'image/object/segment/area' - object_weight = 'image/object/weight' - instance_masks = 'image/segmentation/object' - instance_boundaries = 'image/boundaries/object' - instance_classes = 'image/segmentation/object/class' - detection_class_label = 'image/detection/label' - detection_bbox_ymin = 'image/detection/bbox/ymin' - detection_bbox_xmin = 'image/detection/bbox/xmin' - detection_bbox_ymax = 'image/detection/bbox/ymax' - detection_bbox_xmax = 'image/detection/bbox/xmax' - detection_score = 'image/detection/score' diff --git a/research/cognitive_planning/string_int_label_map_pb2.py b/research/cognitive_planning/string_int_label_map_pb2.py deleted file mode 100644 index 44a46d7abb4..00000000000 --- a/research/cognitive_planning/string_int_label_map_pb2.py +++ /dev/null @@ -1,138 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# Generated by the protocol buffer compiler. DO NOT EDIT! -# source: object_detection/protos/string_int_label_map.proto - -import sys -_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) -from google.protobuf import descriptor as _descriptor -from google.protobuf import message as _message -from google.protobuf import reflection as _reflection -from google.protobuf import symbol_database as _symbol_database -from google.protobuf import descriptor_pb2 -# @@protoc_insertion_point(imports) - -_sym_db = _symbol_database.Default() - - - - -DESCRIPTOR = _descriptor.FileDescriptor( - name='object_detection/protos/string_int_label_map.proto', - package='object_detection.protos', - syntax='proto2', - serialized_pb=_b('\n2object_detection/protos/string_int_label_map.proto\x12\x17object_detection.protos\"G\n\x15StringIntLabelMapItem\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\n\n\x02id\x18\x02 \x01(\x05\x12\x14\n\x0c\x64isplay_name\x18\x03 \x01(\t\"Q\n\x11StringIntLabelMap\x12<\n\x04item\x18\x01 \x03(\x0b\x32..object_detection.protos.StringIntLabelMapItem') -) - - - - -_STRINGINTLABELMAPITEM = _descriptor.Descriptor( - name='StringIntLabelMapItem', - full_name='object_detection.protos.StringIntLabelMapItem', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='name', full_name='object_detection.protos.StringIntLabelMapItem.name', index=0, - number=1, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='id', full_name='object_detection.protos.StringIntLabelMapItem.id', index=1, - number=2, type=5, cpp_type=1, label=1, - has_default_value=False, default_value=0, - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - _descriptor.FieldDescriptor( - name='display_name', full_name='object_detection.protos.StringIntLabelMapItem.display_name', index=2, - number=3, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=_b("").decode('utf-8'), - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - options=None, - is_extendable=False, - syntax='proto2', - extension_ranges=[], - oneofs=[ - ], - serialized_start=79, - serialized_end=150, -) - - -_STRINGINTLABELMAP = _descriptor.Descriptor( - name='StringIntLabelMap', - full_name='object_detection.protos.StringIntLabelMap', - filename=None, - file=DESCRIPTOR, - containing_type=None, - fields=[ - _descriptor.FieldDescriptor( - name='item', full_name='object_detection.protos.StringIntLabelMap.item', index=0, - number=1, type=11, cpp_type=10, label=3, - has_default_value=False, default_value=[], - message_type=None, enum_type=None, containing_type=None, - is_extension=False, extension_scope=None, - options=None), - ], - extensions=[ - ], - nested_types=[], - enum_types=[ - ], - options=None, - is_extendable=False, - syntax='proto2', - extension_ranges=[], - oneofs=[ - ], - serialized_start=152, - serialized_end=233, -) - -_STRINGINTLABELMAP.fields_by_name['item'].message_type = _STRINGINTLABELMAPITEM -DESCRIPTOR.message_types_by_name['StringIntLabelMapItem'] = _STRINGINTLABELMAPITEM -DESCRIPTOR.message_types_by_name['StringIntLabelMap'] = _STRINGINTLABELMAP -_sym_db.RegisterFileDescriptor(DESCRIPTOR) - -StringIntLabelMapItem = _reflection.GeneratedProtocolMessageType('StringIntLabelMapItem', (_message.Message,), dict( - DESCRIPTOR = _STRINGINTLABELMAPITEM, - __module__ = 'object_detection.protos.string_int_label_map_pb2' - # @@protoc_insertion_point(class_scope:object_detection.protos.StringIntLabelMapItem) - )) -_sym_db.RegisterMessage(StringIntLabelMapItem) - -StringIntLabelMap = _reflection.GeneratedProtocolMessageType('StringIntLabelMap', (_message.Message,), dict( - DESCRIPTOR = _STRINGINTLABELMAP, - __module__ = 'object_detection.protos.string_int_label_map_pb2' - # @@protoc_insertion_point(class_scope:object_detection.protos.StringIntLabelMap) - )) -_sym_db.RegisterMessage(StringIntLabelMap) - - -# @@protoc_insertion_point(module_scope) diff --git a/research/cognitive_planning/tasks.py b/research/cognitive_planning/tasks.py deleted file mode 100644 index c3ef6ca328f..00000000000 --- a/research/cognitive_planning/tasks.py +++ /dev/null @@ -1,1507 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A library of tasks. - -This interface is intended to implement a wide variety of navigation -tasks. See go/navigation_tasks for a list. -""" - -import abc -import collections -import math -import threading -import networkx as nx -import numpy as np -import tensorflow as tf -#from pyglib import logging -#import gin -from envs import task_env -from envs import util as envs_util - - -# Utility functions. -def _pad_or_clip_array(np_arr, arr_len, is_front_clip=True, output_mask=False): - """Make np_arr array to have length arr_len. - - If the array is shorter than arr_len, then it is padded from the front with - zeros. If it is longer, then it is clipped either from the back or from the - front. Only the first dimension is modified. - - Args: - np_arr: numpy array. - arr_len: integer scalar. - is_front_clip: a boolean. If true then clipping is done in the front, - otherwise in the back. - output_mask: If True, outputs a numpy array of rank 1 which represents - a mask of which values have been added (0 - added, 1 - actual output). - - Returns: - A numpy array and the size of padding (as a python int32). This size is - negative is the array is clipped. - """ - shape = list(np_arr.shape) - pad_size = arr_len - shape[0] - padded_or_clipped = None - if pad_size < 0: - if is_front_clip: - padded_or_clipped = np_arr[-pad_size:, :] - else: - padded_or_clipped = np_arr[:arr_len, :] - elif pad_size > 0: - padding = np.zeros([pad_size] + shape[1:], dtype=np_arr.dtype) - padded_or_clipped = np.concatenate([np_arr, padding], axis=0) - else: - padded_or_clipped = np_arr - - if output_mask: - mask = np.ones((arr_len,), dtype=np.int) - if pad_size > 0: - mask[-pad_size:] = 0 - return padded_or_clipped, pad_size, mask - else: - return padded_or_clipped, pad_size - - -def classification_loss(truth, predicted, weights=None, is_one_hot=True): - """A cross entropy loss. - - Computes the mean of cross entropy losses for all pairs of true labels and - predictions. It wraps around a tf implementation of the cross entropy loss - with additional reformating of the inputs. If the truth and predicted are - n-rank Tensors with n > 2, then these are reshaped to 2-rank Tensors. It - allows for truth to be specified as one hot vector or class indices. Finally, - a weight can be specified for each element in truth and predicted. - - Args: - truth: an n-rank or (n-1)-rank Tensor containing labels. If is_one_hot is - True, then n-rank Tensor is expected, otherwise (n-1) rank one. - predicted: an n-rank float Tensor containing prediction probabilities. - weights: an (n-1)-rank float Tensor of weights - is_one_hot: a boolean. - - Returns: - A TF float scalar. - """ - num_labels = predicted.get_shape().as_list()[-1] - if not is_one_hot: - truth = tf.reshape(truth, [-1]) - truth = tf.one_hot( - truth, depth=num_labels, on_value=1.0, off_value=0.0, axis=-1) - else: - truth = tf.reshape(truth, [-1, num_labels]) - predicted = tf.reshape(predicted, [-1, num_labels]) - losses = tf.nn.softmax_cross_entropy_with_logits( - labels=truth, logits=predicted) - if weights is not None: - losses = tf.boolean_mask(losses, - tf.cast(tf.reshape(weights, [-1]), dtype=tf.bool)) - return tf.reduce_mean(losses) - - -class UnrolledTaskIOConfig(object): - """Configuration of task inputs and outputs. - - A task can have multiple inputs, which define the context, and a task query - which defines what is to be executed in this context. The desired execution - is encoded in an output. The config defines the shapes of the inputs, the - query and the outputs. - """ - - def __init__(self, inputs, output, query=None): - """Constructs a Task input/output config. - - Args: - inputs: a list of tuples. Each tuple represents the configuration of an - input, with first element being the type (a string value) and the second - element the shape. - output: a tuple representing the configuration of the output. - query: a tuple representing the configuration of the query. If no query, - then None. - """ - # A configuration of a single input, output or query. Consists of the type, - # which can be one of the three specified above, and a shape. The shape must - # be consistent with the type, e.g. if type == 'image', then shape is a 3 - # valued list. - io_config = collections.namedtuple('IOConfig', ['type', 'shape']) - - def assert_config(config): - if not isinstance(config, tuple): - raise ValueError('config must be a tuple. Received {}'.format( - type(config))) - if len(config) != 2: - raise ValueError('config must have 2 elements, has %d' % len(config)) - if not isinstance(config[0], tf.DType): - raise ValueError('First element of config must be a tf.DType.') - if not isinstance(config[1], list): - raise ValueError('Second element of config must be a list.') - - assert isinstance(inputs, collections.OrderedDict) - for modality_type in inputs: - assert_config(inputs[modality_type]) - self._inputs = collections.OrderedDict( - [(k, io_config(*value)) for k, value in inputs.iteritems()]) - - if query is not None: - assert_config(query) - self._query = io_config(*query) - else: - self._query = None - - assert_config(output) - self._output = io_config(*output) - - @property - def inputs(self): - return self._inputs - - @property - def output(self): - return self._output - - @property - def query(self): - return self._query - - -class UnrolledTask(object): - """An interface for a Task which can be unrolled during training. - - Each example is called episode and consists of inputs and target output, where - the output can be considered as desired unrolled sequence of actions for the - inputs. For the specified tasks, these action sequences are to be - unambiguously definable. - """ - __metaclass__ = abc.ABCMeta - - def __init__(self, config): - assert isinstance(config, UnrolledTaskIOConfig) - self._config = config - # A dict of bookkeeping variables. - self.info = {} - # Tensorflow input is multithreaded and this lock is needed to prevent - # race condition in the environment. Without the lock, non-thread safe - # environments crash. - self._lock = threading.Lock() - - @property - def config(self): - return self._config - - @abc.abstractmethod - def episode(self): - """Returns data needed to train and test a single episode. - - Each episode consists of inputs, which define the context of the task, a - query which defines the task, and a target output, which defines a - sequence of actions to be executed for this query. This sequence should not - require feedback, i.e. can be predicted purely from input and query.] - - Returns: - inputs, query, output, where inputs is a list of numpy arrays and query - and output are numpy arrays. These arrays must be of shape and type as - specified in the task configuration. - """ - pass - - def reset(self, observation): - """Called after the environment is reset.""" - pass - - def episode_batch(self, batch_size): - """Returns a batch of episodes. - - Args: - batch_size: size of batch. - - Returns: - (inputs, query, output, masks) where inputs is list of numpy arrays and - query, output, and mask are numpy arrays. These arrays must be of shape - and type as specified in the task configuration with one additional - preceding dimension corresponding to the batch. - - Raises: - ValueError: if self.episode() returns illegal values. - """ - batched_inputs = collections.OrderedDict( - [[mtype, []] for mtype in self.config.inputs]) - batched_queries = [] - batched_outputs = [] - batched_masks = [] - for _ in range(int(batch_size)): - with self._lock: - # The episode function needs to be thread-safe. Since the current - # implementation for the envs are not thread safe we need to have lock - # the operations here. - inputs, query, outputs = self.episode() - if not isinstance(outputs, tuple): - raise ValueError('Outputs return value must be tuple.') - if len(outputs) != 2: - raise ValueError('Output tuple must be of size 2.') - if inputs is not None: - for modality_type in batched_inputs: - batched_inputs[modality_type].append( - np.expand_dims(inputs[modality_type], axis=0)) - - if query is not None: - batched_queries.append(np.expand_dims(query, axis=0)) - batched_outputs.append(np.expand_dims(outputs[0], axis=0)) - if outputs[1] is not None: - batched_masks.append(np.expand_dims(outputs[1], axis=0)) - - batched_inputs = { - k: np.concatenate(i, axis=0) for k, i in batched_inputs.iteritems() - } - if batched_queries: - batched_queries = np.concatenate(batched_queries, axis=0) - batched_outputs = np.concatenate(batched_outputs, axis=0) - if batched_masks: - batched_masks = np.concatenate(batched_masks, axis=0).astype(np.float32) - else: - # When the array is empty, the default np.dtype is float64 which causes - # py_func to crash in the tests. - batched_masks = np.array([], dtype=np.float32) - batched_inputs = [batched_inputs[k] for k in self._config.inputs] - return batched_inputs, batched_queries, batched_outputs, batched_masks - - def tf_episode_batch(self, batch_size): - """A batch of episodes as TF Tensors. - - Same as episode_batch with the difference that the return values are TF - Tensors. - - Args: - batch_size: a python float for the batch size. - - Returns: - inputs, query, output, mask where inputs is a dictionary of tf.Tensor - where the keys are the modality types specified in the config.inputs. - query, output, and mask are TF Tensors. These tensors must - be of shape and type as specified in the task configuration with one - additional preceding dimension corresponding to the batch. Both mask and - output have the same shape as output. - """ - - # Define TF outputs. - touts = [] - shapes = [] - for _, i in self._config.inputs.iteritems(): - touts.append(i.type) - shapes.append(i.shape) - if self._config.query is not None: - touts.append(self._config.query.type) - shapes.append(self._config.query.shape) - # Shapes and types for batched_outputs. - touts.append(self._config.output.type) - shapes.append(self._config.output.shape) - # Shapes and types for batched_masks. - touts.append(self._config.output.type) - shapes.append(self._config.output.shape[0:1]) - - def episode_batch_func(): - if self.config.query is None: - inp, _, output, masks = self.episode_batch(int(batch_size)) - return tuple(inp) + (output, masks) - else: - inp, query, output, masks = self.episode_batch(int(batch_size)) - return tuple(inp) + (query, output, masks) - - tf_episode_batch = tf.py_func(episode_batch_func, [], touts, - stateful=True, name='taskdata') - for episode, shape in zip(tf_episode_batch, shapes): - episode.set_shape([batch_size] + shape) - - tf_episode_batch_dict = collections.OrderedDict([ - (mtype, episode) - for mtype, episode in zip(self.config.inputs.keys(), tf_episode_batch) - ]) - cur_index = len(self.config.inputs.keys()) - tf_query = None - if self.config.query is not None: - tf_query = tf_episode_batch[cur_index] - cur_index += 1 - tf_outputs = tf_episode_batch[cur_index] - tf_masks = tf_episode_batch[cur_index + 1] - - return tf_episode_batch_dict, tf_query, tf_outputs, tf_masks - - @abc.abstractmethod - def target_loss(self, true_targets, targets, weights=None): - """A loss for training a task model. - - This loss measures the discrepancy between the task outputs, the true and - predicted ones. - - Args: - true_targets: tf.Tensor of shape and type as defined in the task config - containing the true outputs. - targets: tf.Tensor of shape and type as defined in the task config - containing the predicted outputs. - weights: a bool tf.Tensor of shape as targets. Only true values are - considered when formulating the loss. - """ - pass - - def reward(self, obs, done, info): - """Returns a reward. - - The tasks has to compute a reward based on the state of the environment. The - reward computation, though, is task specific. The task is to use the - environment interface, as defined in task_env.py, to compute the reward. If - this interface does not expose enough information, it is to be updated. - - Args: - obs: Observation from environment's step function. - done: Done flag from environment's step function. - info: Info dict from environment's step function. - - Returns: - obs: Observation. - reward: Floating point value. - done: Done flag. - info: Info dict. - """ - # Default implementation does not do anything. - return obs, 0.0, done, info - - -class RandomExplorationBasedTask(UnrolledTask): - """A Task which starts with a random exploration of the environment.""" - - def __init__(self, - env, - seed, - add_query_noise=False, - query_noise_var=0.0, - *args, - **kwargs): # pylint: disable=keyword-arg-before-vararg - """Initializes a Task using a random exploration runs. - - Args: - env: an instance of type TaskEnv and gym.Env. - seed: a random seed. - add_query_noise: boolean, if True then whatever queries are generated, - they are randomly perturbed. The semantics of the queries depends on the - concrete task implementation. - query_noise_var: float, the variance of Gaussian noise used for query - perturbation. Used iff add_query_noise==True. - *args: see super class. - **kwargs: see super class. - """ - super(RandomExplorationBasedTask, self).__init__(*args, **kwargs) - assert isinstance(env, task_env.TaskEnv) - self._env = env - self._env.set_task(self) - self._rng = np.random.RandomState(seed) - self._add_query_noise = add_query_noise - self._query_noise_var = query_noise_var - - # GoToStaticXTask can also take empty config but for the rest of the classes - # the number of modality types is 1. - if len(self.config.inputs.keys()) > 1: - raise NotImplementedError('current implementation supports input ' - 'with only one modality type or less.') - - def _exploration(self): - """Generates a random exploration run. - - The function uses the environment to generate a run. - - Returns: - A tuple of numpy arrays. The i-th array contains observation of type and - shape as specified in config.inputs[i]. - A list of states along the exploration path. - A list of vertex indices corresponding to the path of the exploration. - """ - in_seq_len = self._config.inputs.values()[0].shape[0] - path, _, states, step_outputs = self._env.random_step_sequence( - min_len=in_seq_len) - obs = {modality_type: [] for modality_type in self._config.inputs} - for o in step_outputs: - step_obs, _, done, _ = o - # It is expected that each value of step_obs is a dict of observations, - # whose dimensions are consistent with the config.inputs sizes. - for modality_type in self._config.inputs: - assert modality_type in step_obs, '{}'.format(type(step_obs)) - o = step_obs[modality_type] - i = self._config.inputs[modality_type] - assert len(o.shape) == len(i.shape) - 1 - for dim_o, dim_i in zip(o.shape, i.shape[1:]): - assert dim_o == dim_i, '{} != {}'.format(dim_o, dim_i) - obs[modality_type].append(o) - if done: - break - - if not obs: - return obs, states, path - - max_path_len = int( - round(in_seq_len * float(len(path)) / float(len(obs.values()[0])))) - path = path[-max_path_len:] - states = states[-in_seq_len:] - - # The above obs is a list of tuples of np,array. Re-format them as tuple of - # np.array, each array containing all observations from all steps. - def regroup(obs, i): - """Regroups observations. - - Args: - obs: a list of tuples of same size. The k-th tuple contains all the - observations from k-th step. Each observation is a numpy array. - i: the index of the observation in each tuple to be grouped. - - Returns: - A numpy array of shape config.inputs[i] which contains all i-th - observations from all steps. These are concatenated along the first - dimension. In addition, if the number of observations is different from - the one specified in config.inputs[i].shape[0], then the array is either - padded from front or clipped. - """ - grouped_obs = np.concatenate( - [np.expand_dims(o, axis=0) for o in obs[i]], axis=0) - in_seq_len = self._config.inputs[i].shape[0] - # pylint: disable=unbalanced-tuple-unpacking - grouped_obs, _ = _pad_or_clip_array( - grouped_obs, in_seq_len, is_front_clip=True) - return grouped_obs - - all_obs = {i: regroup(obs, i) for i in self._config.inputs} - - return all_obs, states, path - - def _obs_to_state(self, path, states): - """Computes mapping between path nodes and states.""" - # Generate a numpy array of locations corresponding to the path vertices. - path_coordinates = map(self._env.vertex_to_pose, path) - path_coordinates = np.concatenate( - [np.reshape(p, [1, 2]) for p in path_coordinates]) - - # The observations are taken along a smoothed trajectory following the path. - # We compute a mapping between the obeservations and the map vertices. - path_to_obs = collections.defaultdict(list) - obs_to_state = [] - for i, s in enumerate(states): - location = np.reshape(s[0:2], [1, 2]) - index = np.argmin( - np.reshape( - np.sum(np.power(path_coordinates - location, 2), axis=1), [-1])) - index = path[index] - path_to_obs[index].append(i) - obs_to_state.append(index) - return path_to_obs, obs_to_state - - def _perturb_state(self, state, noise_var): - """Perturbes the state. - - The location are purturbed using a Gaussian noise with variance - noise_var. The orientation is uniformly sampled. - - Args: - state: a numpy array containing an env state (x, y locations). - noise_var: float - Returns: - The perturbed state. - """ - - def normal(v, std): - if std > 0: - n = self._rng.normal(0.0, std) - n = min(n, 2.0 * std) - n = max(n, -2.0 * std) - return v + n - else: - return v - - state = state.copy() - state[0] = normal(state[0], noise_var) - state[1] = normal(state[1], noise_var) - if state.size > 2: - state[2] = self._rng.uniform(-math.pi, math.pi) - return state - - def _sample_obs(self, - indices, - observations, - observation_states, - path_to_obs, - max_obs_index=None, - use_exploration_obs=True): - """Samples one observation which corresponds to vertex_index in path. - - In addition, the sampled observation must have index in observations less - than max_obs_index. If these two conditions cannot be satisfied the - function returns None. - - Args: - indices: a list of integers. - observations: a list of numpy arrays containing all the observations. - observation_states: a list of numpy arrays, each array representing the - state of the observation. - path_to_obs: a dict of path indices to lists of observation indices. - max_obs_index: an integer. - use_exploration_obs: if True, then the observation is sampled among the - specified observations, otherwise it is obtained from the environment. - Returns: - A tuple of: - -- A numpy array of size width x height x 3 representing the sampled - observation. - -- The index of the sampld observation among the input observations. - -- The state at which the observation is captured. - Raises: - ValueError: if the observation and observation_states lists are of - different lengths. - """ - if len(observations) != len(observation_states): - raise ValueError('observation and observation_states lists must have ' - 'equal lengths') - if not indices: - return None, None, None - vertex_index = self._rng.choice(indices) - if use_exploration_obs: - obs_indices = path_to_obs[vertex_index] - - if max_obs_index is not None: - obs_indices = [i for i in obs_indices if i < max_obs_index] - - if obs_indices: - index = self._rng.choice(obs_indices) - if self._add_query_noise: - xytheta = self._perturb_state(observation_states[index], - self._query_noise_var) - return self._env.observation(xytheta), index, xytheta - else: - return observations[index], index, observation_states[index] - else: - return None, None, None - else: - xy = self._env.vertex_to_pose(vertex_index) - xytheta = np.array([xy[0], xy[1], 0.0]) - xytheta = self._perturb_state(xytheta, self._query_noise_var) - return self._env.observation(xytheta), None, xytheta - - -class AreNearbyTask(RandomExplorationBasedTask): - """A task of identifying whether a query is nearby current location or not. - - The query is guaranteed to be in proximity of an already visited location, - i.e. close to one of the observations. For each observation we have one - query, which is either close or not to this observation. - """ - - def __init__( - self, - max_distance=0, - *args, - **kwargs): # pylint: disable=keyword-arg-before-vararg - super(AreNearbyTask, self).__init__(*args, **kwargs) - self._max_distance = max_distance - - if len(self.config.inputs.keys()) != 1: - raise NotImplementedError('current implementation supports input ' - 'with only one modality type') - - def episode(self): - """Episode data. - - Returns: - observations: a tuple with one element. This element is a numpy array of - size in_seq_len x observation_size x observation_size x 3 containing - in_seq_len images. - query: a numpy array of size - in_seq_len x observation_size X observation_size x 3 containing a query - image. - A tuple of size two. First element is a in_seq_len x 2 numpy array of - either 1.0 or 0.0. The i-th element denotes whether the i-th query - image is neraby (value 1.0) or not (value 0.0) to the i-th observation. - The second element in the tuple is a mask, a numpy array of size - in_seq_len x 1 and values 1.0 or 0.0 denoting whether the query is - valid or not (it can happen that the query is not valid, e.g. there are - not enough observations to have a meaningful queries). - """ - observations, states, path = self._exploration() - assert len(observations.values()[0]) == len(states) - - # The observations are taken along a smoothed trajectory following the path. - # We compute a mapping between the obeservations and the map vertices. - path_to_obs, obs_to_path = self._obs_to_state(path, states) - - # Go over all observations, and sample a query. With probability 0.5 this - # query is a nearby observation (defined as belonging to the same vertex - # in path). - g = self._env.graph - queries = [] - labels = [] - validity_masks = [] - query_index_in_observations = [] - for i, curr_o in enumerate(observations.values()[0]): - p = obs_to_path[i] - low = max(0, i - self._max_distance) - - # A list of lists of vertex indices. Each list in this group corresponds - # to one possible label. - index_groups = [[], [], []] - # Nearby visited indices, label 1. - nearby_visited = [ - ii for ii in path[low:i + 1] + g[p].keys() if ii in obs_to_path[:i] - ] - nearby_visited = [ii for ii in index_groups[1] if ii in path_to_obs] - # NOT Nearby visited indices, label 0. - not_nearby_visited = [ii for ii in path[:low] if ii not in g[p].keys()] - not_nearby_visited = [ii for ii in index_groups[0] if ii in path_to_obs] - # NOT visited indices, label 2. - not_visited = [ - ii for ii in range(g.number_of_nodes()) if ii not in path[:i + 1] - ] - - index_groups = [not_nearby_visited, nearby_visited, not_visited] - - # Consider only labels for which there are indices. - allowed_labels = [ii for ii, group in enumerate(index_groups) if group] - label = self._rng.choice(allowed_labels) - - indices = list(set(index_groups[label])) - max_obs_index = None if label == 2 else i - use_exploration_obs = False if label == 2 else True - o, obs_index, _ = self._sample_obs( - indices=indices, - observations=observations.values()[0], - observation_states=states, - path_to_obs=path_to_obs, - max_obs_index=max_obs_index, - use_exploration_obs=use_exploration_obs) - query_index_in_observations.append(obs_index) - - # If we cannot sample a valid query, we mark it as not valid in mask. - if o is None: - label = 0.0 - o = curr_o - validity_masks.append(0) - else: - validity_masks.append(1) - - queries.append(o.values()[0]) - labels.append(label) - - query = np.concatenate([np.expand_dims(q, axis=0) for q in queries], axis=0) - - def one_hot(label, num_labels=3): - a = np.zeros((num_labels,), dtype=np.float) - a[int(label)] = 1.0 - return a - - outputs = np.stack([one_hot(l) for l in labels], axis=0) - validity_mask = np.reshape( - np.array(validity_masks, dtype=np.int32), [-1, 1]) - - self.info['query_index_in_observations'] = query_index_in_observations - self.info['observation_states'] = states - - return observations, query, (outputs, validity_mask) - - def target_loss(self, truth, predicted, weights=None): - pass - - -class NeighboringQueriesTask(RandomExplorationBasedTask): - """A task of identifying whether two queries are closeby or not. - - The proximity between queries is defined by the length of the shorest path - between them. - """ - - def __init__( - self, - max_distance=1, - *args, - **kwargs): # pylint: disable=keyword-arg-before-vararg - """Initializes a NeighboringQueriesTask. - - Args: - max_distance: integer, the maximum distance in terms of number of vertices - between the two queries, so that they are considered neighboring. - *args: for super class. - **kwargs: for super class. - """ - super(NeighboringQueriesTask, self).__init__(*args, **kwargs) - self._max_distance = max_distance - if len(self.config.inputs.keys()) != 1: - raise NotImplementedError('current implementation supports input ' - 'with only one modality type') - - def episode(self): - """Episode data. - - Returns: - observations: a tuple with one element. This element is a numpy array of - size in_seq_len x observation_size x observation_size x 3 containing - in_seq_len images. - query: a numpy array of size - 2 x observation_size X observation_size x 3 containing a pair of query - images. - A tuple of size two. First element is a numpy array of size 2 containing - a one hot vector of whether the two observations are neighobring. Second - element is a boolean numpy value denoting whether this is a valid - episode. - """ - observations, states, path = self._exploration() - assert len(observations.values()[0]) == len(states) - path_to_obs, _ = self._obs_to_state(path, states) - # Restrict path to ones for which observations have been generated. - path = [p for p in path if p in path_to_obs] - # Sample first query. - query1_index = self._rng.choice(path) - # Sample label. - label = self._rng.randint(2) - # Sample second query. - # If label == 1, then second query must be nearby, otherwise not. - closest_indices = nx.single_source_shortest_path( - self._env.graph, query1_index, self._max_distance).keys() - if label == 0: - # Closest indices on the path. - indices = [p for p in path if p not in closest_indices] - else: - # Indices which are not closest on the path. - indices = [p for p in closest_indices if p in path] - - query2_index = self._rng.choice(indices) - # Generate an observation. - query1, query1_index, _ = self._sample_obs( - [query1_index], - observations.values()[0], - states, - path_to_obs, - max_obs_index=None, - use_exploration_obs=True) - query2, query2_index, _ = self._sample_obs( - [query2_index], - observations.values()[0], - states, - path_to_obs, - max_obs_index=None, - use_exploration_obs=True) - - queries = np.concatenate( - [np.expand_dims(q, axis=0) for q in [query1, query2]]) - labels = np.array([0, 0]) - labels[label] = 1 - is_valid = np.array([1]) - - self.info['observation_states'] = states - self.info['query_indices_in_observations'] = [query1_index, query2_index] - - return observations, queries, (labels, is_valid) - - def target_loss(self, truth, predicted, weights=None): - pass - - -#@gin.configurable -class GotoStaticXTask(RandomExplorationBasedTask): - """Task go to a static X. - - If continuous reward is used only one goal is allowed so that the reward can - be computed as a delta-distance to that goal.. - """ - - def __init__(self, - step_reward=0.0, - goal_reward=1.0, - hit_wall_reward=-1.0, - done_at_target=False, - use_continuous_reward=False, - *args, - **kwargs): # pylint: disable=keyword-arg-before-vararg - super(GotoStaticXTask, self).__init__(*args, **kwargs) - if len(self.config.inputs.keys()) > 1: - raise NotImplementedError('current implementation supports input ' - 'with only one modality type or less.') - - self._step_reward = step_reward - self._goal_reward = goal_reward - self._hit_wall_reward = hit_wall_reward - self._done_at_target = done_at_target - self._use_continuous_reward = use_continuous_reward - - self._previous_path_length = None - - def episode(self): - observations, _, path = self._exploration() - if len(path) < 2: - raise ValueError('The exploration path has only one node.') - - g = self._env.graph - start = path[-1] - while True: - goal = self._rng.choice(path[:-1]) - if goal != start: - break - goal_path = nx.shortest_path(g, start, goal) - - init_orientation = self._rng.uniform(0, np.pi, (1,)) - trajectory = np.array( - [list(self._env.vertex_to_pose(p)) for p in goal_path]) - init_xy = np.reshape(trajectory[0, :], [-1]) - init_state = np.concatenate([init_xy, init_orientation], 0) - - trajectory = trajectory[1:, :] - deltas = envs_util.trajectory_to_deltas(trajectory, init_state) - output_seq_len = self._config.output.shape[0] - arr = _pad_or_clip_array(deltas, output_seq_len, output_mask=True) - # pylint: disable=unbalanced-tuple-unpacking - thetas, _, thetas_mask = arr - - query = self._env.observation(self._env.vertex_to_pose(goal)).values()[0] - - return observations, query, (thetas, thetas_mask) - - def reward(self, obs, done, info): - if 'wall_collision' in info and info['wall_collision']: - return obs, self._hit_wall_reward, done, info - - reward = 0.0 - current_vertex = self._env.pose_to_vertex(self._env.state) - - if current_vertex in self._env.targets(): - if self._done_at_target: - done = True - else: - obs = self._env.reset() - reward = self._goal_reward - else: - if self._use_continuous_reward: - if len(self._env.targets()) != 1: - raise ValueError( - 'FindX task with continuous reward is assuming only one target.') - goal_vertex = self._env.targets()[0] - path_length = self._compute_path_length(goal_vertex) - reward = self._previous_path_length - path_length - self._previous_path_length = path_length - else: - reward = self._step_reward - - return obs, reward, done, info - - def _compute_path_length(self, goal_vertex): - current_vertex = self._env.pose_to_vertex(self._env.state) - path = nx.shortest_path(self._env.graph, current_vertex, goal_vertex) - assert len(path) >= 2 - curr_xy = np.array(self._env.state[:2]) - next_xy = np.array(self._env.vertex_to_pose(path[1])) - last_step_distance = np.linalg.norm(next_xy - curr_xy) - return (len(path) - 2) * self._env.cell_size_px + last_step_distance - - def reset(self, observation): - if self._use_continuous_reward: - if len(self._env.targets()) != 1: - raise ValueError( - 'FindX task with continuous reward is assuming only one target.') - goal_vertex = self._env.targets()[0] - self._previous_path_length = self._compute_path_length(goal_vertex) - - def target_loss(self, truth, predicted, weights=None): - """Action classification loss. - - Args: - truth: a batch_size x sequence length x number of labels float - Tensor containing a one hot vector for each label in each batch and - time. - predicted: a batch_size x sequence length x number of labels float - Tensor containing a predicted distribution over all actions. - weights: a batch_size x sequence_length float Tensor of bool - denoting which actions are valid. - - Returns: - An average cross entropy over all batches and elements in sequence. - """ - return classification_loss( - truth=truth, predicted=predicted, weights=weights, is_one_hot=True) - - -class RelativeLocationTask(RandomExplorationBasedTask): - """A task of estimating the relative location of a query w.r.t current. - - It is to be used for debugging. It is designed such that the output is a - single value, out of a discrete set of values, so that it can be phrased as - a classification problem. - """ - - def __init__(self, num_labels, *args, **kwargs): - """Initializes a relative location task. - - Args: - num_labels: integer, number of orientations to bin the relative - orientation into. - *args: see super class. - **kwargs: see super class. - """ - super(RelativeLocationTask, self).__init__(*args, **kwargs) - self._num_labels = num_labels - if len(self.config.inputs.keys()) != 1: - raise NotImplementedError('current implementation supports input ' - 'with only one modality type') - - def episode(self): - observations, states, path = self._exploration() - - # Select a random element from history. - path_to_obs, _ = self._obs_to_state(path, states) - use_exploration_obs = not self._add_query_noise - query, _, query_state = self._sample_obs( - path[:-1], - observations.values()[0], - states, - path_to_obs, - max_obs_index=None, - use_exploration_obs=use_exploration_obs) - - x, y, theta = tuple(states[-1]) - q_x, q_y, _ = tuple(query_state) - t_x, t_y = q_x - x, q_y - y - (rt_x, rt_y) = (np.sin(theta) * t_x - np.cos(theta) * t_y, - np.cos(theta) * t_x + np.sin(theta) * t_y) - # Bins are [a(i), a(i+1)] for a(i) = -pi + 0.5 * bin_size + i * bin_size. - shift = np.pi * (1 - 1.0 / (2.0 * self._num_labels)) - orientation = np.arctan2(rt_y, rt_x) + shift - if orientation < 0: - orientation += 2 * np.pi - label = int(np.floor(self._num_labels * orientation / (2 * np.pi))) - - out_shape = self._config.output.shape - if len(out_shape) != 1: - raise ValueError('Output shape should be of rank 1.') - if out_shape[0] != self._num_labels: - raise ValueError('Output shape must be of size %d' % self._num_labels) - output = np.zeros(out_shape, dtype=np.float32) - output[label] = 1 - - return observations, query, (output, None) - - def target_loss(self, truth, predicted, weights=None): - return classification_loss( - truth=truth, predicted=predicted, weights=weights, is_one_hot=True) - - -class LocationClassificationTask(UnrolledTask): - """A task of classifying a location as one of several classes. - - The task does not have an input, but just a query and an output. The query - is an observation of the current location, e.g. an image taken from the - current state. The output is a label classifying this location in one of - predefined set of locations (or landmarks). - - The current implementation classifies locations as intersections based on the - number and directions of biforcations. It is expected that a location can have - at most 4 different directions, aligned with the axes. As each of these four - directions might be present or not, the number of possible intersections are - 2^4 = 16. - """ - - def __init__(self, env, seed, *args, **kwargs): - super(LocationClassificationTask, self).__init__(*args, **kwargs) - self._env = env - self._rng = np.random.RandomState(seed) - # A location property which can be set. If not set, a random one is - # generated. - self._location = None - if len(self.config.inputs.keys()) > 1: - raise NotImplementedError('current implementation supports input ' - 'with only one modality type or less.') - - @property - def location(self): - return self._location - - @location.setter - def location(self, location): - self._location = location - - def episode(self): - # Get a location. If not set, sample on at a vertex with a random - # orientation - location = self._location - if location is None: - num_nodes = self._env.graph.number_of_nodes() - vertex = int(math.floor(self._rng.uniform(0, num_nodes))) - xy = self._env.vertex_to_pose(vertex) - theta = self._rng.uniform(0, 2 * math.pi) - location = np.concatenate( - [np.reshape(xy, [-1]), np.array([theta])], axis=0) - else: - vertex = self._env.pose_to_vertex(location) - - theta = location[2] - neighbors = self._env.graph.neighbors(vertex) - xy_s = [self._env.vertex_to_pose(n) for n in neighbors] - - def rotate(xy, theta): - """Rotates a vector around the origin by angle theta. - - Args: - xy: a numpy darray of shape (2, ) of floats containing the x and y - coordinates of a vector. - theta: a python float containing the rotation angle in radians. - - Returns: - A numpy darray of floats of shape (2,) containing the x and y - coordinates rotated xy. - """ - rotated_x = np.cos(theta) * xy[0] - np.sin(theta) * xy[1] - rotated_y = np.sin(theta) * xy[0] + np.cos(theta) * xy[1] - return np.array([rotated_x, rotated_y]) - - # Rotate all intersection biforcation by the orientation of the agent as the - # intersection label is defined in an agent centered fashion. - xy_s = [ - rotate(xy - location[0:2], -location[2] - math.pi / 4) for xy in xy_s - ] - th_s = [np.arctan2(xy[1], xy[0]) for xy in xy_s] - - out_shape = self._config.output.shape - if len(out_shape) != 1: - raise ValueError('Output shape should be of rank 1.') - num_labels = out_shape[0] - if num_labels != 16: - raise ValueError('Currently only 16 labels are supported ' - '(there are 16 different 4 way intersection types).') - - th_s = set([int(math.floor(4 * (th / (2 * np.pi) + 0.5))) for th in th_s]) - one_hot_label = np.zeros((num_labels,), dtype=np.float32) - label = 0 - for th in th_s: - label += pow(2, th) - one_hot_label[int(label)] = 1.0 - - query = self._env.observation(location).values()[0] - return [], query, (one_hot_label, None) - - def reward(self, obs, done, info): - raise ValueError('Do not call.') - - def target_loss(self, truth, predicted, weights=None): - return classification_loss( - truth=truth, predicted=predicted, weights=weights, is_one_hot=True) - - -class GotoStaticXNoExplorationTask(UnrolledTask): - """An interface for findX tasks without exploration. - - The agent is initialized a random location in a random world and a random goal - and the objective is for the agent to move toward the goal. This class - generates episode for such task. Each generates a sequence of observations x - and target outputs y. x is the observations and is an OrderedDict with keys - provided from config.inputs.keys() and the shapes provided in the - config.inputs. The output is a numpy arrays with the shape specified in the - config.output. The shape of the array is (sequence_length x action_size) where - action is the number of actions that can be done in the environment. Note that - config.output.shape should be set according to the number of actions that can - be done in the env. - target outputs y are the groundtruth value of each action that is computed - from the environment graph. The target output for each action is proportional - to the progress that each action makes. Target value of 1 means that the - action takes the agent one step closer, -1 means the action takes the agent - one step farther. Value of -2 means that action should not take place at all. - This can be because the action leads to collision or it wants to terminate the - episode prematurely. - """ - - def __init__(self, env, *args, **kwargs): - super(GotoStaticXNoExplorationTask, self).__init__(*args, **kwargs) - - if self._config.query is not None: - raise ValueError('query should be None.') - if len(self._config.output.shape) != 2: - raise ValueError('output should only have two dimensions:' - '(sequence_length x number_of_actions)') - for input_config in self._config.inputs.values(): - if input_config.shape[0] != self._config.output.shape[0]: - raise ValueError('the first dimension of the input and output should' - 'be the same.') - if len(self._config.output.shape) != 2: - raise ValueError('output shape should be ' - '(sequence_length x number_of_actions)') - - self._env = env - - def _compute_shortest_path_length(self, vertex, target_vertices): - """Computes length of the shortest path from vertex to any target vertexes. - - Args: - vertex: integer, index of the vertex in the environment graph. - target_vertices: list of the target vertexes - - Returns: - integer, minimum distance from the vertex to any of the target_vertices. - - Raises: - ValueError: if there is no path between the vertex and at least one of - the target_vertices. - """ - try: - return np.min([ - len(nx.shortest_path(self._env.graph, vertex, t)) - for t in target_vertices - ]) - except: - #logging.error('there is no path between vertex %d and at least one of ' - # 'the targets %r', vertex, target_vertices) - raise - - def _compute_gt_value(self, vertex, target_vertices): - """Computes groundtruth value of all the actions at the vertex. - - The value of each action is the difference each action makes in the length - of the shortest path to the goal. If an action takes the agent one step - closer to the goal the value is 1. In case, it takes the agent one step away - from the goal it would be -1. If it leads to collision or if the agent uses - action stop before reaching to the goal it is -2. To avoid scale issues the - gt_values are multipled by 0.5. - - Args: - vertex: integer, the index of current vertex. - target_vertices: list of the integer indexes of the target views. - - Returns: - numpy array with shape (action_size,) and each element is the groundtruth - value of each action based on the progress each action makes. - """ - action_size = self._config.output.shape[1] - output_value = np.ones((action_size), dtype=np.float32) * -2 - my_distance = self._compute_shortest_path_length(vertex, target_vertices) - for adj in self._env.graph[vertex]: - adj_distance = self._compute_shortest_path_length(adj, target_vertices) - if adj_distance is None: - continue - action_index = self._env.action( - self._env.vertex_to_pose(vertex), self._env.vertex_to_pose(adj)) - assert action_index is not None, ('{} is not adjacent to {}. There might ' - 'be a problem in environment graph ' - 'connectivity because there is no ' - 'direct edge between the given ' - 'vertices').format( - self._env.vertex_to_pose(vertex), - self._env.vertex_to_pose(adj)) - output_value[action_index] = my_distance - adj_distance - - return output_value * 0.5 - - def episode(self): - """Returns data needed to train and test a single episode. - - Returns: - (inputs, None, output) where inputs is a dictionary of modality types to - numpy arrays. The second element is query but we assume that the goal - is also given as part of observation so it should be None for this task, - and the outputs is the tuple of ground truth action values with the - shape of (sequence_length x action_size) that is coming from - config.output.shape and a numpy array with the shape of - (sequence_length,) that is 1 if the corresponding element of the - input and output should be used in the training optimization. - - Raises: - ValueError: If the output values for env.random_step_sequence is not - valid. - ValueError: If the shape of observations coming from the env is not - consistent with the config. - ValueError: If there is a modality type specified in the config but the - environment does not return that. - """ - # Sequence length is the first dimension of any of the input tensors. - sequence_length = self._config.inputs.values()[0].shape[0] - modality_types = self._config.inputs.keys() - - path, _, _, step_outputs = self._env.random_step_sequence( - max_len=sequence_length) - target_vertices = [self._env.pose_to_vertex(x) for x in self._env.targets()] - - if len(path) != len(step_outputs): - raise ValueError('path, and step_outputs should have equal length' - ' {}!={}'.format(len(path), len(step_outputs))) - - # Building up observations. observations will be a OrderedDict of - # modality types. The values are numpy arrays that follow the given shape - # in the input config for each modality type. - observations = collections.OrderedDict([k, []] for k in modality_types) - for step_output in step_outputs: - obs_dict = step_output[0] - # Only going over the modality types that are specified in the input - # config. - for modality_type in modality_types: - if modality_type not in obs_dict: - raise ValueError('modality type is not returned from the environment.' - '{} not in {}'.format(modality_type, - obs_dict.keys())) - obs = obs_dict[modality_type] - if np.any( - obs.shape != tuple(self._config.inputs[modality_type].shape[1:])): - raise ValueError( - 'The observations should have the same size as speicifed in' - 'config for modality type {}. {} != {}'.format( - modality_type, obs.shape, - self._config.inputs[modality_type].shape[1:])) - observations[modality_type].append(obs) - - gt_value = [self._compute_gt_value(v, target_vertices) for v in path] - - # pylint: disable=unbalanced-tuple-unpacking - gt_value, _, value_mask = _pad_or_clip_array( - np.array(gt_value), - sequence_length, - is_front_clip=False, - output_mask=True, - ) - for modality_type, obs in observations.iteritems(): - observations[modality_type], _, mask = _pad_or_clip_array( - np.array(obs), sequence_length, is_front_clip=False, output_mask=True) - assert np.all(mask == value_mask) - - return observations, None, (gt_value, value_mask) - - def reset(self, observation): - """Called after the environment is reset.""" - pass - - def target_loss(self, true_targets, targets, weights=None): - """A loss for training a task model. - - This loss measures the discrepancy between the task outputs, the true and - predicted ones. - - Args: - true_targets: tf.Tensor of tf.float32 with the shape of - (batch_size x sequence_length x action_size). - targets: tf.Tensor of tf.float32 with the shape of - (batch_size x sequence_length x action_size). - weights: tf.Tensor of tf.bool with the shape of - (batch_size x sequence_length). - - Raises: - ValueError: if the shapes of the input tensors are not consistent. - - Returns: - L2 loss between the predicted action values and true action values. - """ - targets_shape = targets.get_shape().as_list() - true_targets_shape = true_targets.get_shape().as_list() - if len(targets_shape) != 3 or len(true_targets_shape) != 3: - raise ValueError('invalid shape for targets or true_targets_shape') - if np.any(targets_shape != true_targets_shape): - raise ValueError('the shape of targets and true_targets are not the same' - '{} != {}'.format(targets_shape, true_targets_shape)) - - if weights is not None: - # Filtering targets and true_targets using weights. - weights_shape = weights.get_shape().as_list() - if np.any(weights_shape != targets_shape[0:2]): - raise ValueError('The first two elements of weights shape should match' - 'target. {} != {}'.format(weights_shape, - targets_shape)) - true_targets = tf.boolean_mask(true_targets, weights) - targets = tf.boolean_mask(targets, weights) - - return tf.losses.mean_squared_error(tf.reshape(targets, [-1]), - tf.reshape(true_targets, [-1])) - - def reward(self, obs, done, info): - raise NotImplementedError('reward is not implemented for this task') - - -################################################################################ -class NewTask(UnrolledTask): - def __init__(self, env, *args, **kwargs): - super(NewTask, self).__init__(*args, **kwargs) - self._env = env - - def _compute_shortest_path_length(self, vertex, target_vertices): - """Computes length of the shortest path from vertex to any target vertexes. - - Args: - vertex: integer, index of the vertex in the environment graph. - target_vertices: list of the target vertexes - - Returns: - integer, minimum distance from the vertex to any of the target_vertices. - - Raises: - ValueError: if there is no path between the vertex and at least one of - the target_vertices. - """ - try: - return np.min([ - len(nx.shortest_path(self._env.graph, vertex, t)) - for t in target_vertices - ]) - except: - logging.error('there is no path between vertex %d and at least one of ' - 'the targets %r', vertex, target_vertices) - raise - - def _compute_gt_value(self, vertex, target_vertices): - """Computes groundtruth value of all the actions at the vertex. - - The value of each action is the difference each action makes in the length - of the shortest path to the goal. If an action takes the agent one step - closer to the goal the value is 1. In case, it takes the agent one step away - from the goal it would be -1. If it leads to collision or if the agent uses - action stop before reaching to the goal it is -2. To avoid scale issues the - gt_values are multipled by 0.5. - - Args: - vertex: integer, the index of current vertex. - target_vertices: list of the integer indexes of the target views. - - Returns: - numpy array with shape (action_size,) and each element is the groundtruth - value of each action based on the progress each action makes. - """ - action_size = self._config.output.shape[1] - output_value = np.ones((action_size), dtype=np.float32) * -2 - # own compute _compute_shortest_path_length - returnts float - my_distance = self._compute_shortest_path_length(vertex, target_vertices) - for adj in self._env.graph[vertex]: - adj_distance = self._compute_shortest_path_length(adj, target_vertices) - if adj_distance is None: - continue - action_index = self._env.action( - self._env.vertex_to_pose(vertex), self._env.vertex_to_pose(adj)) - assert action_index is not None, ('{} is not adjacent to {}. There might ' - 'be a problem in environment graph ' - 'connectivity because there is no ' - 'direct edge between the given ' - 'vertices').format( - self._env.vertex_to_pose(vertex), - self._env.vertex_to_pose(adj)) - output_value[action_index] = my_distance - adj_distance - - return output_value * 0.5 - - def episode(self): - """Returns data needed to train and test a single episode. - - Returns: - (inputs, None, output) where inputs is a dictionary of modality types to - numpy arrays. The second element is query but we assume that the goal - is also given as part of observation so it should be None for this task, - and the outputs is the tuple of ground truth action values with the - shape of (sequence_length x action_size) that is coming from - config.output.shape and a numpy array with the shape of - (sequence_length,) that is 1 if the corresponding element of the - input and output should be used in the training optimization. - - Raises: - ValueError: If the output values for env.random_step_sequence is not - valid. - ValueError: If the shape of observations coming from the env is not - consistent with the config. - ValueError: If there is a modality type specified in the config but the - environment does not return that. - """ - # Sequence length is the first dimension of any of the input tensors. - sequence_length = self._config.inputs.values()[0].shape[0] - modality_types = self._config.inputs.keys() - - path, _, _, step_outputs = self._env.random_step_sequence( - max_len=sequence_length) - target_vertices = [self._env.pose_to_vertex(x) for x in self._env.targets()] - - if len(path) != len(step_outputs): - raise ValueError('path, and step_outputs should have equal length' - ' {}!={}'.format(len(path), len(step_outputs))) - - # Building up observations. observations will be a OrderedDict of - # modality types. The values are numpy arrays that follow the given shape - # in the input config for each modality type. - observations = collections.OrderedDict([k, []] for k in modality_types) - for step_output in step_outputs: - obs_dict = step_output[0] - # Only going over the modality types that are specified in the input - # config. - for modality_type in modality_types: - if modality_type not in obs_dict: - raise ValueError('modality type is not returned from the environment.' - '{} not in {}'.format(modality_type, - obs_dict.keys())) - obs = obs_dict[modality_type] - if np.any( - obs.shape != tuple(self._config.inputs[modality_type].shape[1:])): - raise ValueError( - 'The observations should have the same size as speicifed in' - 'config for modality type {}. {} != {}'.format( - modality_type, obs.shape, - self._config.inputs[modality_type].shape[1:])) - observations[modality_type].append(obs) - - gt_value = [self._compute_gt_value(v, target_vertices) for v in path] - - # pylint: disable=unbalanced-tuple-unpacking - gt_value, _, value_mask = _pad_or_clip_array( - np.array(gt_value), - sequence_length, - is_front_clip=False, - output_mask=True, - ) - for modality_type, obs in observations.iteritems(): - observations[modality_type], _, mask = _pad_or_clip_array( - np.array(obs), sequence_length, is_front_clip=False, output_mask=True) - assert np.all(mask == value_mask) - - return observations, None, (gt_value, value_mask) - - def reset(self, observation): - """Called after the environment is reset.""" - pass - - def target_loss(self, true_targets, targets, weights=None): - """A loss for training a task model. - - This loss measures the discrepancy between the task outputs, the true and - predicted ones. - - Args: - true_targets: tf.Tensor of tf.float32 with the shape of - (batch_size x sequence_length x action_size). - targets: tf.Tensor of tf.float32 with the shape of - (batch_size x sequence_length x action_size). - weights: tf.Tensor of tf.bool with the shape of - (batch_size x sequence_length). - - Raises: - ValueError: if the shapes of the input tensors are not consistent. - - Returns: - L2 loss between the predicted action values and true action values. - """ - targets_shape = targets.get_shape().as_list() - true_targets_shape = true_targets.get_shape().as_list() - if len(targets_shape) != 3 or len(true_targets_shape) != 3: - raise ValueError('invalid shape for targets or true_targets_shape') - if np.any(targets_shape != true_targets_shape): - raise ValueError('the shape of targets and true_targets are not the same' - '{} != {}'.format(targets_shape, true_targets_shape)) - - if weights is not None: - # Filtering targets and true_targets using weights. - weights_shape = weights.get_shape().as_list() - if np.any(weights_shape != targets_shape[0:2]): - raise ValueError('The first two elements of weights shape should match' - 'target. {} != {}'.format(weights_shape, - targets_shape)) - true_targets = tf.boolean_mask(true_targets, weights) - targets = tf.boolean_mask(targets, weights) - - return tf.losses.mean_squared_error(tf.reshape(targets, [-1]), - tf.reshape(true_targets, [-1])) - - def reward(self, obs, done, info): - raise NotImplementedError('reward is not implemented for this task') diff --git a/research/cognitive_planning/train_supervised_active_vision.py b/research/cognitive_planning/train_supervised_active_vision.py deleted file mode 100644 index 5931a24e15b..00000000000 --- a/research/cognitive_planning/train_supervised_active_vision.py +++ /dev/null @@ -1,503 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# pylint: disable=line-too-long -# pyformat: disable -"""Train and eval for supervised navigation training. - -For training: -python train_supervised_active_vision.py \ - --mode='train' \ - --logdir=$logdir/checkin_log_det/ \ - --modality_types='det' \ - --batch_size=8 \ - --train_iters=200000 \ - --lstm_cell_size=2048 \ - --policy_fc_size=2048 \ - --sequence_length=20 \ - --max_eval_episode_length=100 \ - --test_iters=194 \ - --gin_config=envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root="$datadir"' \ - --logtostderr - -For testing: -python train_supervised_active_vision.py - --mode='eval' \ - --logdir=$logdir/checkin_log_det/ \ - --modality_types='det' \ - --batch_size=8 \ - --train_iters=200000 \ - --lstm_cell_size=2048 \ - --policy_fc_size=2048 \ - --sequence_length=20 \ - --max_eval_episode_length=100 \ - --test_iters=194 \ - --gin_config=envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root="$datadir"' \ - --logtostderr -""" - -import collections -import os -import time -from absl import app -from absl import flags -from absl import logging -import networkx as nx -import numpy as np -import tensorflow as tf -import gin -import embedders -import policies -import tasks -from envs import active_vision_dataset_env -from envs import task_env - -slim = tf.contrib.slim - -flags.DEFINE_string('logdir', '', - 'Path to a directory to write summaries and checkpoints') -# Parameters controlling the training setup. In general one would not need to -# modify them. -flags.DEFINE_string('master', 'local', - 'BNS name of the TensorFlow master, or local.') -flags.DEFINE_integer('task_id', 0, - 'Task id of the replica running the training.') -flags.DEFINE_integer('ps_tasks', 0, - 'Number of tasks in the ps job. If 0 no ps job is used.') - -flags.DEFINE_integer('decay_steps', 1000, - 'Number of steps for exponential decay.') -flags.DEFINE_float('learning_rate', 0.0001, 'Learning rate.') -flags.DEFINE_integer('batch_size', 8, 'Batch size.') -flags.DEFINE_integer('sequence_length', 20, 'sequence length') -flags.DEFINE_integer('train_iters', 200000, 'number of training iterations.') -flags.DEFINE_integer('save_summaries_secs', 300, - 'number of seconds between saving summaries') -flags.DEFINE_integer('save_interval_secs', 300, - 'numer of seconds between saving variables') -flags.DEFINE_integer('log_every_n_steps', 20, 'number of steps between logging') -flags.DEFINE_string('modality_types', '', - 'modality names in _ separated format') -flags.DEFINE_string('conv_window_sizes', '8_4_3', - 'conv window size in separated by _') -flags.DEFINE_string('conv_strides', '4_2_1', '') -flags.DEFINE_string('conv_channels', '8_16_16', '') -flags.DEFINE_integer('embedding_fc_size', 128, - 'size of embedding for each modality') -flags.DEFINE_integer('obs_resolution', 64, - 'resolution of the input observations') -flags.DEFINE_integer('lstm_cell_size', 2048, 'size of lstm cell size') -flags.DEFINE_integer('policy_fc_size', 2048, - 'size of fully connected layers for policy part') -flags.DEFINE_float('weight_decay', 0.0002, 'weight decay') -flags.DEFINE_integer('goal_category_count', 5, 'number of goal categories') -flags.DEFINE_integer('action_size', 7, 'number of possible actions') -flags.DEFINE_integer('max_eval_episode_length', 100, - 'maximum sequence length for evaluation.') -flags.DEFINE_enum('mode', 'train', ['train', 'eval'], - 'indicates whether it is in training or evaluation') -flags.DEFINE_integer('test_iters', 194, - 'number of iterations that the eval needs to be run') -flags.DEFINE_multi_string('gin_config', [], - 'List of paths to a gin config files for the env.') -flags.DEFINE_multi_string('gin_params', [], - 'Newline separated list of Gin parameter bindings.') -flags.DEFINE_string( - 'resnet50_path', './resnet_v2_50_checkpoint/resnet_v2_50.ckpt', 'path to resnet50' - 'checkpoint') -flags.DEFINE_bool('freeze_resnet_weights', True, '') -flags.DEFINE_string( - 'eval_init_points_file_name', '', - 'Name of the file that containts the initial locations and' - 'worlds for each evalution point') - -FLAGS = flags.FLAGS -TRAIN_WORLDS = [ - 'Home_001_1', 'Home_001_2', 'Home_002_1', 'Home_003_1', 'Home_003_2', - 'Home_004_1', 'Home_004_2', 'Home_005_1', 'Home_005_2', 'Home_006_1', - 'Home_010_1' -] - -TEST_WORLDS = ['Home_011_1', 'Home_013_1', 'Home_016_1'] - - -def create_modality_types(): - """Parses the modality_types and returns a list of task_env.ModalityType.""" - if not FLAGS.modality_types: - raise ValueError('there needs to be at least one modality type') - modality_types = FLAGS.modality_types.split('_') - for x in modality_types: - if x not in ['image', 'sseg', 'det', 'depth']: - raise ValueError('invalid modality type: {}'.format(x)) - - conversion_dict = { - 'image': task_env.ModalityTypes.IMAGE, - 'sseg': task_env.ModalityTypes.SEMANTIC_SEGMENTATION, - 'depth': task_env.ModalityTypes.DEPTH, - 'det': task_env.ModalityTypes.OBJECT_DETECTION, - } - return [conversion_dict[k] for k in modality_types] - - -def create_task_io_config( - modality_types, - goal_category_count, - action_size, - sequence_length, -): - """Generates task io config.""" - shape_prefix = [sequence_length, FLAGS.obs_resolution, FLAGS.obs_resolution] - shapes = { - task_env.ModalityTypes.IMAGE: [sequence_length, 224, 224, 3], - task_env.ModalityTypes.DEPTH: shape_prefix + [ - 2, - ], - task_env.ModalityTypes.SEMANTIC_SEGMENTATION: shape_prefix + [ - 1, - ], - task_env.ModalityTypes.OBJECT_DETECTION: shape_prefix + [ - 90, - ] - } - types = {k: tf.float32 for k in shapes} - types[task_env.ModalityTypes.IMAGE] = tf.uint8 - inputs = collections.OrderedDict( - [[mtype, (types[mtype], shapes[mtype])] for mtype in modality_types]) - inputs[task_env.ModalityTypes.GOAL] = (tf.float32, - [sequence_length, goal_category_count]) - inputs[task_env.ModalityTypes.PREV_ACTION] = (tf.float32, [ - sequence_length, action_size + 1 - ]) - print inputs - return tasks.UnrolledTaskIOConfig( - inputs=inputs, - output=(tf.float32, [sequence_length, action_size]), - query=None) - - -def map_to_embedder(modality_type): - """Maps modality_type to its corresponding embedder.""" - if modality_type == task_env.ModalityTypes.PREV_ACTION: - return None - if modality_type == task_env.ModalityTypes.GOAL: - return embedders.IdentityEmbedder() - if modality_type == task_env.ModalityTypes.IMAGE: - return embedders.ResNet50Embedder() - conv_window_sizes = [int(x) for x in FLAGS.conv_window_sizes.split('_')] - conv_channels = [int(x) for x in FLAGS.conv_channels.split('_')] - conv_strides = [int(x) for x in FLAGS.conv_strides.split('_')] - params = tf.contrib.training.HParams( - to_one_hot=modality_type == task_env.ModalityTypes.SEMANTIC_SEGMENTATION, - one_hot_length=10, - conv_sizes=conv_window_sizes, - conv_strides=conv_strides, - conv_channels=conv_channels, - embedding_size=FLAGS.embedding_fc_size, - weight_decay_rate=FLAGS.weight_decay, - ) - return embedders.SmallNetworkEmbedder(params) - - -def create_train_and_init_ops(policy, task): - """Creates training ops given the arguments. - - Args: - policy: the policy for the task. - task: the task instance. - - Returns: - train_op: the op that needs to be runned at each step. - summaries_op: the summary op that is executed. - init_fn: the op that initializes the variables if there is no previous - checkpoint. If Resnet50 is not used in the model it is None, otherwise - it reads the weights from FLAGS.resnet50_path and sets the init_fn - to the op that initializes the ResNet50 with the pre-trained weights. - """ - assert isinstance(task, tasks.GotoStaticXNoExplorationTask) - assert isinstance(policy, policies.Policy) - - inputs, _, gt_outputs, masks = task.tf_episode_batch(FLAGS.batch_size) - outputs, _ = policy.build(inputs, None) - loss = task.target_loss(gt_outputs, outputs, masks) - - init_fn = None - - # If resnet is added to the graph, init_fn should initialize resnet weights - # if there is no previous checkpoint. - variables_assign_dict = {} - vars_list = [] - for v in slim.get_model_variables(): - if v.name.find('resnet') >= 0: - if not FLAGS.freeze_resnet_weights: - vars_list.append(v) - variables_assign_dict[v.name[v.name.find('resnet'):-2]] = v - else: - vars_list.append(v) - - global_step = tf.train.get_or_create_global_step() - learning_rate = tf.train.exponential_decay( - FLAGS.learning_rate, - global_step, - decay_steps=FLAGS.decay_steps, - decay_rate=0.98, - staircase=True) - optimizer = tf.train.AdamOptimizer(learning_rate) - train_op = slim.learning.create_train_op( - loss, - optimizer, - global_step=global_step, - variables_to_train=vars_list, - ) - - if variables_assign_dict: - init_fn = slim.assign_from_checkpoint_fn( - FLAGS.resnet50_path, - variables_assign_dict, - ignore_missing_vars=False) - scalar_summaries = {} - scalar_summaries['LR'] = learning_rate - scalar_summaries['loss'] = loss - - for name, summary in scalar_summaries.iteritems(): - tf.summary.scalar(name, summary) - - return train_op, init_fn - - -def create_eval_ops(policy, config, possible_targets): - """Creates the necessary ops for evaluation.""" - inputs_feed = collections.OrderedDict([[ - mtype, - tf.placeholder(config.inputs[mtype].type, - [1] + config.inputs[mtype].shape) - ] for mtype in config.inputs]) - inputs_feed[task_env.ModalityTypes.PREV_ACTION] = tf.placeholder( - tf.float32, [1, 1] + [ - config.output.shape[-1] + 1, - ]) - prev_state_feed = [ - tf.placeholder( - tf.float32, [1, FLAGS.lstm_cell_size], name='prev_state_{}'.format(i)) - for i in range(2) - ] - policy_outputs = policy.build(inputs_feed, prev_state_feed) - summary_feed = {} - for c in possible_targets + ['mean']: - summary_feed[c] = tf.placeholder( - tf.float32, [], name='eval_in_range_{}_input'.format(c)) - tf.summary.scalar('eval_in_range_{}'.format(c), summary_feed[c]) - - return inputs_feed, prev_state_feed, policy_outputs, (tf.summary.merge_all(), - summary_feed) - - -def unroll_policy_for_eval( - sess, - env, - inputs_feed, - prev_state_feed, - policy_outputs, - number_of_steps, - output_folder, -): - """unrolls the policy for testing. - - Args: - sess: tf.Session - env: The environment. - inputs_feed: dictionary of placeholder for the input modalities. - prev_state_feed: placeholder for the input to the prev_state of the model. - policy_outputs: tensor that contains outputs of the policy. - number_of_steps: maximum number of unrolling steps. - output_folder: output_folder where the function writes a dictionary of - detailed information about the path. The dictionary keys are 'states' and - 'distance'. The value for 'states' is the list of states that the agent - goes along the path. The value for 'distance' contains the length of - shortest path to the goal at each step. - - Returns: - states: list of states along the path. - distance: list of distances along the path. - """ - prev_state = [ - np.zeros((1, FLAGS.lstm_cell_size), dtype=np.float32) for _ in range(2) - ] - prev_action = np.zeros((1, 1, FLAGS.action_size + 1), dtype=np.float32) - obs = env.reset() - distances_to_goal = [] - states = [] - unique_id = '{}_{}'.format(env.cur_image_id(), env.goal_string) - for _ in range(number_of_steps): - distances_to_goal.append( - np.min([ - len( - nx.shortest_path(env.graph, env.pose_to_vertex(env.state()), - env.pose_to_vertex(target_view))) - for target_view in env.targets() - ])) - states.append(env.state()) - feed_dict = {inputs_feed[mtype]: [[obs[mtype]]] for mtype in inputs_feed} - feed_dict[prev_state_feed[0]] = prev_state[0] - feed_dict[prev_state_feed[1]] = prev_state[1] - action_values, prev_state = sess.run(policy_outputs, feed_dict=feed_dict) - chosen_action = np.argmax(action_values[0]) - obs, _, done, info = env.step(np.int32(chosen_action)) - prev_action[0][0][chosen_action] = 1. - prev_action[0][0][-1] = float(info['success']) - # If the agent chooses action stop or the number of steps exceeeded - # env._episode_length. - if done: - break - - # logging.info('distance = %d, id = %s, #steps = %d', distances_to_goal[-1], - output_path = os.path.join(output_folder, unique_id + '.npy') - with tf.gfile.Open(output_path, 'w') as f: - print 'saving path information to {}'.format(output_path) - np.save(f, {'states': states, 'distance': distances_to_goal}) - return states, distances_to_goal - - -def init(sequence_length, eval_init_points_file_name, worlds): - """Initializes the common operations between train and test.""" - modality_types = create_modality_types() - logging.info('modality types: %r', modality_types) - # negative reward_goal_range prevents the env from terminating early when the - # agent is close to the goal. The policy should keep the agent until the end - # of the 100 steps either through chosing stop action or oscilating around - # the target. - - env = active_vision_dataset_env.ActiveVisionDatasetEnv( - modality_types=modality_types + - [task_env.ModalityTypes.GOAL, task_env.ModalityTypes.PREV_ACTION], - reward_goal_range=-1, - eval_init_points_file_name=eval_init_points_file_name, - worlds=worlds, - output_size=FLAGS.obs_resolution, - ) - - config = create_task_io_config( - modality_types=modality_types, - goal_category_count=FLAGS.goal_category_count, - action_size=FLAGS.action_size, - sequence_length=sequence_length, - ) - task = tasks.GotoStaticXNoExplorationTask(env=env, config=config) - embedders_dict = {mtype: map_to_embedder(mtype) for mtype in config.inputs} - policy_params = tf.contrib.training.HParams( - lstm_state_size=FLAGS.lstm_cell_size, - fc_channels=FLAGS.policy_fc_size, - weight_decay=FLAGS.weight_decay, - target_embedding_size=FLAGS.embedding_fc_size, - ) - policy = policies.LSTMPolicy( - modality_names=config.inputs.keys(), - embedders_dict=embedders_dict, - action_size=FLAGS.action_size, - params=policy_params, - max_episode_length=sequence_length) - return env, config, task, policy - - -def test(): - """Contains all the operations for testing policies.""" - env, config, _, policy = init(1, 'all_init_configs', TEST_WORLDS) - inputs_feed, prev_state_feed, policy_outputs, summary_op = create_eval_ops( - policy, config, env.possible_targets) - sv = tf.train.Supervisor(logdir=FLAGS.logdir) - prev_checkpoint = None - with sv.managed_session( - start_standard_services=False, - config=tf.ConfigProto(allow_soft_placement=True)) as sess: - while not sv.should_stop(): - while True: - new_checkpoint = tf.train.latest_checkpoint(FLAGS.logdir) - print 'new_checkpoint ', new_checkpoint - if not new_checkpoint: - time.sleep(1) - continue - if prev_checkpoint is None: - prev_checkpoint = new_checkpoint - break - if prev_checkpoint != new_checkpoint: - prev_checkpoint = new_checkpoint - break - else: # if prev_checkpoint == new_checkpoint, we have to wait more. - time.sleep(1) - - checkpoint_step = int(new_checkpoint[new_checkpoint.rfind('-') + 1:]) - sv.saver.restore(sess, new_checkpoint) - print '--------------------' - print 'evaluating checkpoint {}'.format(new_checkpoint) - folder_path = os.path.join(FLAGS.logdir, 'evals', str(checkpoint_step)) - if not tf.gfile.Exists(folder_path): - tf.gfile.MakeDirs(folder_path) - eval_stats = {c: [] for c in env.possible_targets} - for test_iter in range(FLAGS.test_iters): - print 'evaluating {} of {}'.format(test_iter, FLAGS.test_iters) - _, distance_to_goal = unroll_policy_for_eval( - sess, - env, - inputs_feed, - prev_state_feed, - policy_outputs, - FLAGS.max_eval_episode_length, - folder_path, - ) - print 'goal = {}'.format(env.goal_string) - eval_stats[env.goal_string].append(float(distance_to_goal[-1] <= 7)) - eval_stats = {k: np.mean(v) for k, v in eval_stats.iteritems()} - eval_stats['mean'] = np.mean(eval_stats.values()) - print eval_stats - feed_dict = {summary_op[1][c]: eval_stats[c] for c in eval_stats} - summary_str = sess.run(summary_op[0], feed_dict=feed_dict) - writer = sv.summary_writer - writer.add_summary(summary_str, checkpoint_step) - writer.flush() - - -def train(): - _, _, task, policy = init(FLAGS.sequence_length, None, TRAIN_WORLDS) - print(FLAGS.save_summaries_secs) - print(FLAGS.save_interval_secs) - print(FLAGS.logdir) - - with tf.device( - tf.train.replica_device_setter(ps_tasks=FLAGS.ps_tasks, merge_devices=True)): - train_op, init_fn = create_train_and_init_ops(policy=policy, task=task) - print(FLAGS.logdir) - slim.learning.train( - train_op=train_op, - init_fn=init_fn, - logdir=FLAGS.logdir, - is_chief=FLAGS.task_id == 0, - number_of_steps=FLAGS.train_iters, - save_summaries_secs=FLAGS.save_summaries_secs, - save_interval_secs=FLAGS.save_interval_secs, - session_config=tf.ConfigProto(allow_soft_placement=True), - ) - - -def main(_): - gin.parse_config_files_and_bindings(FLAGS.gin_config, FLAGS.gin_params) - if FLAGS.mode == 'train': - train() - else: - test() - - -if __name__ == '__main__': - app.run(main) diff --git a/research/cognitive_planning/train_supervised_active_vision.sh b/research/cognitive_planning/train_supervised_active_vision.sh deleted file mode 100755 index f2ea2275344..00000000000 --- a/research/cognitive_planning/train_supervised_active_vision.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# blaze build -c opt train_supervised_active_vision -# bazel build -c opt --config=cuda --copt=-mavx train_supervised_active_vision && \ -bazel-bin/research/cognitive_planning/train_supervised_active_vision \ - --mode='train' \ - --logdir=/usr/local/google/home/kosecka/local_avd_train/ \ - --modality_types='det' \ - --batch_size=8 \ - --train_iters=200000 \ - --lstm_cell_size=2048 \ - --policy_fc_size=2048 \ - --sequence_length=20 \ - --max_eval_episode_length=100 \ - --test_iters=194 \ - --gin_config=envs/configs/active_vision_config.gin \ - --gin_params='ActiveVisionDatasetEnv.dataset_root="/cns/jn-d/home/kosecka/AVD_Minimal/"' \ - --logtostderr diff --git a/research/cognitive_planning/visualization_utils.py b/research/cognitive_planning/visualization_utils.py deleted file mode 100644 index 7a7aeb50561..00000000000 --- a/research/cognitive_planning/visualization_utils.py +++ /dev/null @@ -1,733 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A set of functions that are used for visualization. - -These functions often receive an image, perform some visualization on the image. -The functions do not return a value, instead they modify the image itself. - -""" -import collections -import functools -# Set headless-friendly backend. -import matplotlib; matplotlib.use('Agg') # pylint: disable=multiple-statements -import matplotlib.pyplot as plt # pylint: disable=g-import-not-at-top -import numpy as np -import PIL.Image as Image -import PIL.ImageColor as ImageColor -import PIL.ImageDraw as ImageDraw -import PIL.ImageFont as ImageFont -import six -import tensorflow as tf - -import standard_fields as fields - - -_TITLE_LEFT_MARGIN = 10 -_TITLE_TOP_MARGIN = 10 -STANDARD_COLORS = [ - 'AliceBlue', 'Chartreuse', 'Aqua', 'Aquamarine', 'Azure', 'Beige', 'Bisque', - 'BlanchedAlmond', 'BlueViolet', 'BurlyWood', 'CadetBlue', 'AntiqueWhite', - 'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan', - 'DarkCyan', 'DarkGoldenRod', 'DarkGrey', 'DarkKhaki', 'DarkOrange', - 'DarkOrchid', 'DarkSalmon', 'DarkSeaGreen', 'DarkTurquoise', 'DarkViolet', - 'DeepPink', 'DeepSkyBlue', 'DodgerBlue', 'FireBrick', 'FloralWhite', - 'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod', - 'Salmon', 'Tan', 'HoneyDew', 'HotPink', 'IndianRed', 'Ivory', 'Khaki', - 'Lavender', 'LavenderBlush', 'LawnGreen', 'LemonChiffon', 'LightBlue', - 'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey', - 'LightGreen', 'LightPink', 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue', - 'LightSlateGray', 'LightSlateGrey', 'LightSteelBlue', 'LightYellow', 'Lime', - 'LimeGreen', 'Linen', 'Magenta', 'MediumAquaMarine', 'MediumOrchid', - 'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue', 'MediumSpringGreen', - 'MediumTurquoise', 'MediumVioletRed', 'MintCream', 'MistyRose', 'Moccasin', - 'NavajoWhite', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed', - 'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed', - 'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple', - 'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Green', 'SandyBrown', - 'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue', - 'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'GreenYellow', - 'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White', - 'WhiteSmoke', 'Yellow', 'YellowGreen' -] - - -def save_image_array_as_png(image, output_path): - """Saves an image (represented as a numpy array) to PNG. - - Args: - image: a numpy array with shape [height, width, 3]. - output_path: path to which image should be written. - """ - image_pil = Image.fromarray(np.uint8(image)).convert('RGB') - with tf.gfile.Open(output_path, 'w') as fid: - image_pil.save(fid, 'PNG') - - -def encode_image_array_as_png_str(image): - """Encodes a numpy array into a PNG string. - - Args: - image: a numpy array with shape [height, width, 3]. - - Returns: - PNG encoded image string. - """ - image_pil = Image.fromarray(np.uint8(image)) - output = six.BytesIO() - image_pil.save(output, format='PNG') - png_string = output.getvalue() - output.close() - return png_string - - -def draw_bounding_box_on_image_array(image, - ymin, - xmin, - ymax, - xmax, - color='red', - thickness=4, - display_str_list=(), - use_normalized_coordinates=True): - """Adds a bounding box to an image (numpy array). - - Bounding box coordinates can be specified in either absolute (pixel) or - normalized coordinates by setting the use_normalized_coordinates argument. - - Args: - image: a numpy array with shape [height, width, 3]. - ymin: ymin of bounding box. - xmin: xmin of bounding box. - ymax: ymax of bounding box. - xmax: xmax of bounding box. - color: color to draw bounding box. Default is red. - thickness: line thickness. Default value is 4. - display_str_list: list of strings to display in box - (each to be shown on its own line). - use_normalized_coordinates: If True (default), treat coordinates - ymin, xmin, ymax, xmax as relative to the image. Otherwise treat - coordinates as absolute. - """ - image_pil = Image.fromarray(np.uint8(image)).convert('RGB') - draw_bounding_box_on_image(image_pil, ymin, xmin, ymax, xmax, color, - thickness, display_str_list, - use_normalized_coordinates) - np.copyto(image, np.array(image_pil)) - - -def draw_bounding_box_on_image(image, - ymin, - xmin, - ymax, - xmax, - color='red', - thickness=4, - display_str_list=(), - use_normalized_coordinates=True): - """Adds a bounding box to an image. - - Bounding box coordinates can be specified in either absolute (pixel) or - normalized coordinates by setting the use_normalized_coordinates argument. - - Each string in display_str_list is displayed on a separate line above the - bounding box in black text on a rectangle filled with the input 'color'. - If the top of the bounding box extends to the edge of the image, the strings - are displayed below the bounding box. - - Args: - image: a PIL.Image object. - ymin: ymin of bounding box. - xmin: xmin of bounding box. - ymax: ymax of bounding box. - xmax: xmax of bounding box. - color: color to draw bounding box. Default is red. - thickness: line thickness. Default value is 4. - display_str_list: list of strings to display in box - (each to be shown on its own line). - use_normalized_coordinates: If True (default), treat coordinates - ymin, xmin, ymax, xmax as relative to the image. Otherwise treat - coordinates as absolute. - """ - draw = ImageDraw.Draw(image) - im_width, im_height = image.size - if use_normalized_coordinates: - (left, right, top, bottom) = (xmin * im_width, xmax * im_width, - ymin * im_height, ymax * im_height) - else: - (left, right, top, bottom) = (xmin, xmax, ymin, ymax) - draw.line([(left, top), (left, bottom), (right, bottom), - (right, top), (left, top)], width=thickness, fill=color) - try: - font = ImageFont.truetype('arial.ttf', 24) - except IOError: - font = ImageFont.load_default() - - # If the total height of the display strings added to the top of the bounding - # box exceeds the top of the image, stack the strings below the bounding box - # instead of above. - display_str_heights = [font.getsize(ds)[1] for ds in display_str_list] - # Each display_str has a top and bottom margin of 0.05x. - total_display_str_height = (1 + 2 * 0.05) * sum(display_str_heights) - - if top > total_display_str_height: - text_bottom = top - else: - text_bottom = bottom + total_display_str_height - # Reverse list and print from bottom to top. - for display_str in display_str_list[::-1]: - text_width, text_height = font.getsize(display_str) - margin = np.ceil(0.05 * text_height) - draw.rectangle( - [(left, text_bottom - text_height - 2 * margin), (left + text_width, - text_bottom)], - fill=color) - draw.text( - (left + margin, text_bottom - text_height - margin), - display_str, - fill='black', - font=font) - text_bottom -= text_height - 2 * margin - - -def draw_bounding_boxes_on_image_array(image, - boxes, - color='red', - thickness=4, - display_str_list_list=()): - """Draws bounding boxes on image (numpy array). - - Args: - image: a numpy array object. - boxes: a 2 dimensional numpy array of [N, 4]: (ymin, xmin, ymax, xmax). - The coordinates are in normalized format between [0, 1]. - color: color to draw bounding box. Default is red. - thickness: line thickness. Default value is 4. - display_str_list_list: list of list of strings. - a list of strings for each bounding box. - The reason to pass a list of strings for a - bounding box is that it might contain - multiple labels. - - Raises: - ValueError: if boxes is not a [N, 4] array - """ - image_pil = Image.fromarray(image) - draw_bounding_boxes_on_image(image_pil, boxes, color, thickness, - display_str_list_list) - np.copyto(image, np.array(image_pil)) - - -def draw_bounding_boxes_on_image(image, - boxes, - color='red', - thickness=4, - display_str_list_list=()): - """Draws bounding boxes on image. - - Args: - image: a PIL.Image object. - boxes: a 2 dimensional numpy array of [N, 4]: (ymin, xmin, ymax, xmax). - The coordinates are in normalized format between [0, 1]. - color: color to draw bounding box. Default is red. - thickness: line thickness. Default value is 4. - display_str_list_list: list of list of strings. - a list of strings for each bounding box. - The reason to pass a list of strings for a - bounding box is that it might contain - multiple labels. - - Raises: - ValueError: if boxes is not a [N, 4] array - """ - boxes_shape = boxes.shape - if not boxes_shape: - return - if len(boxes_shape) != 2 or boxes_shape[1] != 4: - raise ValueError('Input must be of size [N, 4]') - for i in range(boxes_shape[0]): - display_str_list = () - if display_str_list_list: - display_str_list = display_str_list_list[i] - draw_bounding_box_on_image(image, boxes[i, 0], boxes[i, 1], boxes[i, 2], - boxes[i, 3], color, thickness, display_str_list) - - -def _visualize_boxes(image, boxes, classes, scores, category_index, **kwargs): - return visualize_boxes_and_labels_on_image_array( - image, boxes, classes, scores, category_index=category_index, **kwargs) - - -def _visualize_boxes_and_masks(image, boxes, classes, scores, masks, - category_index, **kwargs): - return visualize_boxes_and_labels_on_image_array( - image, - boxes, - classes, - scores, - category_index=category_index, - instance_masks=masks, - **kwargs) - - -def _visualize_boxes_and_keypoints(image, boxes, classes, scores, keypoints, - category_index, **kwargs): - return visualize_boxes_and_labels_on_image_array( - image, - boxes, - classes, - scores, - category_index=category_index, - keypoints=keypoints, - **kwargs) - - -def _visualize_boxes_and_masks_and_keypoints( - image, boxes, classes, scores, masks, keypoints, category_index, **kwargs): - return visualize_boxes_and_labels_on_image_array( - image, - boxes, - classes, - scores, - category_index=category_index, - instance_masks=masks, - keypoints=keypoints, - **kwargs) - - -def draw_bounding_boxes_on_image_tensors(images, - boxes, - classes, - scores, - category_index, - instance_masks=None, - keypoints=None, - max_boxes_to_draw=20, - min_score_thresh=0.2, - use_normalized_coordinates=True): - """Draws bounding boxes, masks, and keypoints on batch of image tensors. - - Args: - images: A 4D uint8 image tensor of shape [N, H, W, C]. If C > 3, additional - channels will be ignored. - boxes: [N, max_detections, 4] float32 tensor of detection boxes. - classes: [N, max_detections] int tensor of detection classes. Note that - classes are 1-indexed. - scores: [N, max_detections] float32 tensor of detection scores. - category_index: a dict that maps integer ids to category dicts. e.g. - {1: {1: 'dog'}, 2: {2: 'cat'}, ...} - instance_masks: A 4D uint8 tensor of shape [N, max_detection, H, W] with - instance masks. - keypoints: A 4D float32 tensor of shape [N, max_detection, num_keypoints, 2] - with keypoints. - max_boxes_to_draw: Maximum number of boxes to draw on an image. Default 20. - min_score_thresh: Minimum score threshold for visualization. Default 0.2. - use_normalized_coordinates: Whether to assume boxes and kepoints are in - normalized coordinates (as opposed to absolute coordiantes). - Default is True. - - Returns: - 4D image tensor of type uint8, with boxes drawn on top. - """ - # Additional channels are being ignored. - images = images[:, :, :, 0:3] - visualization_keyword_args = { - 'use_normalized_coordinates': use_normalized_coordinates, - 'max_boxes_to_draw': max_boxes_to_draw, - 'min_score_thresh': min_score_thresh, - 'agnostic_mode': False, - 'line_thickness': 4 - } - - if instance_masks is not None and keypoints is None: - visualize_boxes_fn = functools.partial( - _visualize_boxes_and_masks, - category_index=category_index, - **visualization_keyword_args) - elems = [images, boxes, classes, scores, instance_masks] - elif instance_masks is None and keypoints is not None: - visualize_boxes_fn = functools.partial( - _visualize_boxes_and_keypoints, - category_index=category_index, - **visualization_keyword_args) - elems = [images, boxes, classes, scores, keypoints] - elif instance_masks is not None and keypoints is not None: - visualize_boxes_fn = functools.partial( - _visualize_boxes_and_masks_and_keypoints, - category_index=category_index, - **visualization_keyword_args) - elems = [images, boxes, classes, scores, instance_masks, keypoints] - else: - visualize_boxes_fn = functools.partial( - _visualize_boxes, - category_index=category_index, - **visualization_keyword_args) - elems = [images, boxes, classes, scores] - - def draw_boxes(image_and_detections): - """Draws boxes on image.""" - image_with_boxes = tf.py_func(visualize_boxes_fn, image_and_detections, - tf.uint8) - return image_with_boxes - - images = tf.map_fn(draw_boxes, elems, dtype=tf.uint8, back_prop=False) - return images - - -def draw_side_by_side_evaluation_image(eval_dict, - category_index, - max_boxes_to_draw=20, - min_score_thresh=0.2, - use_normalized_coordinates=True): - """Creates a side-by-side image with detections and groundtruth. - - Bounding boxes (and instance masks, if available) are visualized on both - subimages. - - Args: - eval_dict: The evaluation dictionary returned by - eval_util.result_dict_for_single_example(). - category_index: A category index (dictionary) produced from a labelmap. - max_boxes_to_draw: The maximum number of boxes to draw for detections. - min_score_thresh: The minimum score threshold for showing detections. - use_normalized_coordinates: Whether to assume boxes and kepoints are in - normalized coordinates (as opposed to absolute coordiantes). - Default is True. - - Returns: - A [1, H, 2 * W, C] uint8 tensor. The subimage on the left corresponds to - detections, while the subimage on the right corresponds to groundtruth. - """ - detection_fields = fields.DetectionResultFields() - input_data_fields = fields.InputDataFields() - instance_masks = None - if detection_fields.detection_masks in eval_dict: - instance_masks = tf.cast( - tf.expand_dims(eval_dict[detection_fields.detection_masks], axis=0), - tf.uint8) - keypoints = None - if detection_fields.detection_keypoints in eval_dict: - keypoints = tf.expand_dims( - eval_dict[detection_fields.detection_keypoints], axis=0) - groundtruth_instance_masks = None - if input_data_fields.groundtruth_instance_masks in eval_dict: - groundtruth_instance_masks = tf.cast( - tf.expand_dims( - eval_dict[input_data_fields.groundtruth_instance_masks], axis=0), - tf.uint8) - images_with_detections = draw_bounding_boxes_on_image_tensors( - eval_dict[input_data_fields.original_image], - tf.expand_dims(eval_dict[detection_fields.detection_boxes], axis=0), - tf.expand_dims(eval_dict[detection_fields.detection_classes], axis=0), - tf.expand_dims(eval_dict[detection_fields.detection_scores], axis=0), - category_index, - instance_masks=instance_masks, - keypoints=keypoints, - max_boxes_to_draw=max_boxes_to_draw, - min_score_thresh=min_score_thresh, - use_normalized_coordinates=use_normalized_coordinates) - images_with_groundtruth = draw_bounding_boxes_on_image_tensors( - eval_dict[input_data_fields.original_image], - tf.expand_dims(eval_dict[input_data_fields.groundtruth_boxes], axis=0), - tf.expand_dims(eval_dict[input_data_fields.groundtruth_classes], axis=0), - tf.expand_dims( - tf.ones_like( - eval_dict[input_data_fields.groundtruth_classes], - dtype=tf.float32), - axis=0), - category_index, - instance_masks=groundtruth_instance_masks, - keypoints=None, - max_boxes_to_draw=None, - min_score_thresh=0.0, - use_normalized_coordinates=use_normalized_coordinates) - return tf.concat([images_with_detections, images_with_groundtruth], axis=2) - - -def draw_keypoints_on_image_array(image, - keypoints, - color='red', - radius=2, - use_normalized_coordinates=True): - """Draws keypoints on an image (numpy array). - - Args: - image: a numpy array with shape [height, width, 3]. - keypoints: a numpy array with shape [num_keypoints, 2]. - color: color to draw the keypoints with. Default is red. - radius: keypoint radius. Default value is 2. - use_normalized_coordinates: if True (default), treat keypoint values as - relative to the image. Otherwise treat them as absolute. - """ - image_pil = Image.fromarray(np.uint8(image)).convert('RGB') - draw_keypoints_on_image(image_pil, keypoints, color, radius, - use_normalized_coordinates) - np.copyto(image, np.array(image_pil)) - - -def draw_keypoints_on_image(image, - keypoints, - color='red', - radius=2, - use_normalized_coordinates=True): - """Draws keypoints on an image. - - Args: - image: a PIL.Image object. - keypoints: a numpy array with shape [num_keypoints, 2]. - color: color to draw the keypoints with. Default is red. - radius: keypoint radius. Default value is 2. - use_normalized_coordinates: if True (default), treat keypoint values as - relative to the image. Otherwise treat them as absolute. - """ - draw = ImageDraw.Draw(image) - im_width, im_height = image.size - keypoints_x = [k[1] for k in keypoints] - keypoints_y = [k[0] for k in keypoints] - if use_normalized_coordinates: - keypoints_x = tuple([im_width * x for x in keypoints_x]) - keypoints_y = tuple([im_height * y for y in keypoints_y]) - for keypoint_x, keypoint_y in zip(keypoints_x, keypoints_y): - draw.ellipse([(keypoint_x - radius, keypoint_y - radius), - (keypoint_x + radius, keypoint_y + radius)], - outline=color, fill=color) - - -def draw_mask_on_image_array(image, mask, color='red', alpha=0.4): - """Draws mask on an image. - - Args: - image: uint8 numpy array with shape (img_height, img_height, 3) - mask: a uint8 numpy array of shape (img_height, img_height) with - values between either 0 or 1. - color: color to draw the keypoints with. Default is red. - alpha: transparency value between 0 and 1. (default: 0.4) - - Raises: - ValueError: On incorrect data type for image or masks. - """ - if image.dtype != np.uint8: - raise ValueError('`image` not of type np.uint8') - if mask.dtype != np.uint8: - raise ValueError('`mask` not of type np.uint8') - if np.any(np.logical_and(mask != 1, mask != 0)): - raise ValueError('`mask` elements should be in [0, 1]') - if image.shape[:2] != mask.shape: - raise ValueError('The image has spatial dimensions %s but the mask has ' - 'dimensions %s' % (image.shape[:2], mask.shape)) - rgb = ImageColor.getrgb(color) - pil_image = Image.fromarray(image) - - solid_color = np.expand_dims( - np.ones_like(mask), axis=2) * np.reshape(list(rgb), [1, 1, 3]) - pil_solid_color = Image.fromarray(np.uint8(solid_color)).convert('RGBA') - pil_mask = Image.fromarray(np.uint8(255.0*alpha*mask)).convert('L') - pil_image = Image.composite(pil_solid_color, pil_image, pil_mask) - np.copyto(image, np.array(pil_image.convert('RGB'))) - - -def visualize_boxes_and_labels_on_image_array( - image, - boxes, - classes, - scores, - category_index, - instance_masks=None, - instance_boundaries=None, - keypoints=None, - use_normalized_coordinates=False, - max_boxes_to_draw=20, - min_score_thresh=.5, - agnostic_mode=False, - line_thickness=4, - groundtruth_box_visualization_color='black', - skip_scores=False, - skip_labels=False): - """Overlay labeled boxes on an image with formatted scores and label names. - - This function groups boxes that correspond to the same location - and creates a display string for each detection and overlays these - on the image. Note that this function modifies the image in place, and returns - that same image. - - Args: - image: uint8 numpy array with shape (img_height, img_width, 3) - boxes: a numpy array of shape [N, 4] - classes: a numpy array of shape [N]. Note that class indices are 1-based, - and match the keys in the label map. - scores: a numpy array of shape [N] or None. If scores=None, then - this function assumes that the boxes to be plotted are groundtruth - boxes and plot all boxes as black with no classes or scores. - category_index: a dict containing category dictionaries (each holding - category index `id` and category name `name`) keyed by category indices. - instance_masks: a numpy array of shape [N, image_height, image_width] with - values ranging between 0 and 1, can be None. - instance_boundaries: a numpy array of shape [N, image_height, image_width] - with values ranging between 0 and 1, can be None. - keypoints: a numpy array of shape [N, num_keypoints, 2], can - be None - use_normalized_coordinates: whether boxes is to be interpreted as - normalized coordinates or not. - max_boxes_to_draw: maximum number of boxes to visualize. If None, draw - all boxes. - min_score_thresh: minimum score threshold for a box to be visualized - agnostic_mode: boolean (default: False) controlling whether to evaluate in - class-agnostic mode or not. This mode will display scores but ignore - classes. - line_thickness: integer (default: 4) controlling line width of the boxes. - groundtruth_box_visualization_color: box color for visualizing groundtruth - boxes - skip_scores: whether to skip score when drawing a single detection - skip_labels: whether to skip label when drawing a single detection - - Returns: - uint8 numpy array with shape (img_height, img_width, 3) with overlaid boxes. - """ - # Create a display string (and color) for every box location, group any boxes - # that correspond to the same location. - box_to_display_str_map = collections.defaultdict(list) - box_to_color_map = collections.defaultdict(str) - box_to_instance_masks_map = {} - box_to_instance_boundaries_map = {} - box_to_keypoints_map = collections.defaultdict(list) - if not max_boxes_to_draw: - max_boxes_to_draw = boxes.shape[0] - for i in range(min(max_boxes_to_draw, boxes.shape[0])): - if scores is None or scores[i] > min_score_thresh: - box = tuple(boxes[i].tolist()) - if instance_masks is not None: - box_to_instance_masks_map[box] = instance_masks[i] - if instance_boundaries is not None: - box_to_instance_boundaries_map[box] = instance_boundaries[i] - if keypoints is not None: - box_to_keypoints_map[box].extend(keypoints[i]) - if scores is None: - box_to_color_map[box] = groundtruth_box_visualization_color - else: - display_str = '' - if not skip_labels: - if not agnostic_mode: - if classes[i] in category_index.keys(): - class_name = category_index[classes[i]]['name'] - else: - class_name = 'N/A' - display_str = str(class_name) - if not skip_scores: - if not display_str: - display_str = '{}%'.format(int(100*scores[i])) - else: - display_str = '{}: {}%'.format(display_str, int(100*scores[i])) - box_to_display_str_map[box].append(display_str) - if agnostic_mode: - box_to_color_map[box] = 'DarkOrange' - else: - box_to_color_map[box] = STANDARD_COLORS[ - classes[i] % len(STANDARD_COLORS)] - - # Draw all boxes onto image. - for box, color in box_to_color_map.items(): - ymin, xmin, ymax, xmax = box - if instance_masks is not None: - draw_mask_on_image_array( - image, - box_to_instance_masks_map[box], - color=color - ) - if instance_boundaries is not None: - draw_mask_on_image_array( - image, - box_to_instance_boundaries_map[box], - color='red', - alpha=1.0 - ) - draw_bounding_box_on_image_array( - image, - ymin, - xmin, - ymax, - xmax, - color=color, - thickness=line_thickness, - display_str_list=box_to_display_str_map[box], - use_normalized_coordinates=use_normalized_coordinates) - if keypoints is not None: - draw_keypoints_on_image_array( - image, - box_to_keypoints_map[box], - color=color, - radius=line_thickness / 2, - use_normalized_coordinates=use_normalized_coordinates) - - return image - - -def add_cdf_image_summary(values, name): - """Adds a tf.summary.image for a CDF plot of the values. - - Normalizes `values` such that they sum to 1, plots the cumulative distribution - function and creates a tf image summary. - - Args: - values: a 1-D float32 tensor containing the values. - name: name for the image summary. - """ - def cdf_plot(values): - """Numpy function to plot CDF.""" - normalized_values = values / np.sum(values) - sorted_values = np.sort(normalized_values) - cumulative_values = np.cumsum(sorted_values) - fraction_of_examples = (np.arange(cumulative_values.size, dtype=np.float32) - / cumulative_values.size) - fig = plt.figure(frameon=False) - ax = fig.add_subplot('111') - ax.plot(fraction_of_examples, cumulative_values) - ax.set_ylabel('cumulative normalized values') - ax.set_xlabel('fraction of examples') - fig.canvas.draw() - width, height = fig.get_size_inches() * fig.get_dpi() - image = np.fromstring(fig.canvas.tostring_rgb(), dtype='uint8').reshape( - 1, int(height), int(width), 3) - return image - cdf_plot = tf.py_func(cdf_plot, [values], tf.uint8) - tf.summary.image(name, cdf_plot) - - -def add_hist_image_summary(values, bins, name): - """Adds a tf.summary.image for a histogram plot of the values. - - Plots the histogram of values and creates a tf image summary. - - Args: - values: a 1-D float32 tensor containing the values. - bins: bin edges which will be directly passed to np.histogram. - name: name for the image summary. - """ - - def hist_plot(values, bins): - """Numpy function to plot hist.""" - fig = plt.figure(frameon=False) - ax = fig.add_subplot('111') - y, x = np.histogram(values, bins=bins) - ax.plot(x[:-1], y) - ax.set_ylabel('count') - ax.set_xlabel('value') - fig.canvas.draw() - width, height = fig.get_size_inches() * fig.get_dpi() - image = np.fromstring( - fig.canvas.tostring_rgb(), dtype='uint8').reshape( - 1, int(height), int(width), 3) - return image - hist_plot = tf.py_func(hist_plot, [values, bins], tf.uint8) - tf.summary.image(name, hist_plot) diff --git a/research/cognitive_planning/viz_active_vision_dataset_main.py b/research/cognitive_planning/viz_active_vision_dataset_main.py deleted file mode 100644 index e6b7deef63e..00000000000 --- a/research/cognitive_planning/viz_active_vision_dataset_main.py +++ /dev/null @@ -1,379 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Initializes at random location and visualizes the optimal path. - -Different modes of execution: -1) benchmark: It generates benchmark_iter sample trajectory to random goals - and plots the histogram of path lengths. It can be also used to see how fast - it runs. -2) vis: It visualizes the generated paths by image, semantic segmentation, and - so on. -3) human: allows the user to navigate through environment from keyboard input. - -python viz_active_vision_dataset_main -- \ - --mode=benchmark --benchmark_iter=1000 --gin_config=envs/configs/active_vision_config.gin - -python viz_active_vision_dataset_main -- \ - --mode=vis \ - --gin_config=envs/configs/active_vision_config.gin - -python viz_active_vision_dataset_main -- \ - --mode=human \ - --gin_config=envs/configs/active_vision_config.gin - -python viz_active_vision_dataset_main.py --mode=eval --eval_folder=/usr/local/google/home/$USER/checkin_log_det/evals/ --output_folder=/usr/local/google/home/$USER/test_imgs/ --gin_config=envs/configs/active_vision_config.gin - -""" - -import matplotlib -# pylint: disable=g-import-not-at-top -# Need Tk for interactive plots. -matplotlib.use('TkAgg') -import tensorflow as tf -from matplotlib import pyplot as plt -import numpy as np -import os -from pyglib import app -from pyglib import flags -import gin -import cv2 -from envs import active_vision_dataset_env -from envs import task_env - - -VIS_MODE = 'vis' -HUMAN_MODE = 'human' -BENCHMARK_MODE = 'benchmark' -GRAPH_MODE = 'graph' -EVAL_MODE = 'eval' - -flags.DEFINE_enum('mode', VIS_MODE, - [VIS_MODE, HUMAN_MODE, BENCHMARK_MODE, GRAPH_MODE, EVAL_MODE], - 'mode of the execution') -flags.DEFINE_integer('benchmark_iter', 1000, - 'number of iterations for benchmarking') -flags.DEFINE_string('eval_folder', '', 'the path to the eval folder') -flags.DEFINE_string('output_folder', '', - 'the path to which the images and gifs are written') -flags.DEFINE_multi_string('gin_config', [], - 'List of paths to a gin config files for the env.') -flags.DEFINE_multi_string('gin_params', [], - 'Newline separated list of Gin parameter bindings.') - -mt = task_env.ModalityTypes -FLAGS = flags.FLAGS - -def benchmark(env, targets): - """Benchmarks the speed of sequence generation by env. - - Args: - env: environment. - targets: list of target classes. - """ - episode_lengths = {} - all_init_configs = {} - all_actions = dict([(a, 0.) for a in env.actions]) - for i in range(FLAGS.benchmark_iter): - path, actions, _, _ = env.random_step_sequence() - selected_actions = np.argmax(actions, axis=-1) - new_actions = dict([(a, 0.) for a in env.actions]) - for a in selected_actions: - new_actions[env.actions[a]] += 1. / selected_actions.shape[0] - for a in new_actions: - all_actions[a] += new_actions[a] / FLAGS.benchmark_iter - start_image_id, world, goal = env.get_init_config(path) - print world - if world not in all_init_configs: - all_init_configs[world] = set() - all_init_configs[world].add((start_image_id, goal, len(actions))) - if env.goal_index not in episode_lengths: - episode_lengths[env.goal_index] = [] - episode_lengths[env.goal_index].append(len(actions)) - for i, cls in enumerate(episode_lengths): - plt.subplot(231 + i) - plt.hist(episode_lengths[cls]) - plt.title(targets[cls]) - plt.show() - - -def human(env, targets): - """Lets user play around the env manually.""" - string_key_map = { - 'a': 'left', - 'd': 'right', - 'w': 'forward', - 's': 'backward', - 'j': 'rotate_ccw', - 'l': 'rotate_cw', - 'n': 'stop' - } - integer_key_map = { - 'a': env.actions.index('left'), - 'd': env.actions.index('right'), - 'w': env.actions.index('forward'), - 's': env.actions.index('backward'), - 'j': env.actions.index('rotate_ccw'), - 'l': env.actions.index('rotate_cw'), - 'n': env.actions.index('stop') - } - for k in integer_key_map: - integer_key_map[k] = np.int32(integer_key_map[k]) - plt.ion() - for _ in range(20): - obs = env.reset() - steps = -1 - action = None - while True: - print 'distance = ', obs[task_env.ModalityTypes.DISTANCE] - steps += 1 - depth_value = obs[task_env.ModalityTypes.DEPTH][:, :, 0] - depth_mask = obs[task_env.ModalityTypes.DEPTH][:, :, 1] - seg_mask = np.squeeze(obs[task_env.ModalityTypes.SEMANTIC_SEGMENTATION]) - det_mask = np.argmax( - obs[task_env.ModalityTypes.OBJECT_DETECTION], axis=-1) - img = obs[task_env.ModalityTypes.IMAGE] - plt.subplot(231) - plt.title('steps = {}'.format(steps)) - plt.imshow(img.astype(np.uint8)) - plt.subplot(232) - plt.imshow(depth_value) - plt.title('depth value') - plt.subplot(233) - plt.imshow(depth_mask) - plt.title('depth mask') - plt.subplot(234) - plt.imshow(seg_mask) - plt.title('seg') - plt.subplot(235) - plt.imshow(det_mask) - plt.title('det') - plt.subplot(236) - plt.title('goal={}'.format(targets[env.goal_index])) - plt.draw() - while True: - s = raw_input('key = ') - if np.random.rand() > 0.5: - key_map = string_key_map - else: - key_map = integer_key_map - if s in key_map: - action = key_map[s] - break - else: - print 'invalid action' - print 'action = {}'.format(action) - if action == 'stop': - print 'dist to goal: {}'.format(len(env.path_to_goal()) - 2) - break - obs, reward, done, info = env.step(action) - print 'reward = {}, done = {}, success = {}'.format( - reward, done, info['success']) - - -def visualize_random_step_sequence(env): - """Visualizes random sequence of steps.""" - plt.ion() - for _ in range(20): - path, actions, _, step_outputs = env.random_step_sequence(max_len=30) - print 'path = {}'.format(path) - for action, step_output in zip(actions, step_outputs): - obs, _, done, _ = step_output - depth_value = obs[task_env.ModalityTypes.DEPTH][:, :, 0] - depth_mask = obs[task_env.ModalityTypes.DEPTH][:, :, 1] - seg_mask = np.squeeze(obs[task_env.ModalityTypes.SEMANTIC_SEGMENTATION]) - det_mask = np.argmax( - obs[task_env.ModalityTypes.OBJECT_DETECTION], axis=-1) - img = obs[task_env.ModalityTypes.IMAGE] - plt.subplot(231) - plt.imshow(img.astype(np.uint8)) - plt.subplot(232) - plt.imshow(depth_value) - plt.title('depth value') - plt.subplot(233) - plt.imshow(depth_mask) - plt.title('depth mask') - plt.subplot(234) - plt.imshow(seg_mask) - plt.title('seg') - plt.subplot(235) - plt.imshow(det_mask) - plt.title('det') - plt.subplot(236) - print 'action = {}'.format(action) - print 'done = {}'.format(done) - plt.draw() - if raw_input('press \'n\' to go to the next random sequence. Otherwise, ' - 'press any key to continue...') == 'n': - break - - -def visualize(env, input_folder, output_root_folder): - """visualizes images for sequence of steps from the evals folder.""" - def which_env(file_name): - img_name = file_name.split('_')[0][2:5] - env_dict = {'161': 'Home_016_1', '131': 'Home_013_1', '111': 'Home_011_1'} - if img_name in env_dict: - return env_dict[img_name] - else: - raise ValueError('could not resolve env: {} {}'.format( - img_name, file_name)) - - def which_goal(file_name): - return file_name[file_name.find('_')+1:] - - output_images_folder = os.path.join(output_root_folder, 'images') - output_gifs_folder = os.path.join(output_root_folder, 'gifs') - if not tf.gfile.IsDirectory(output_images_folder): - tf.gfile.MakeDirs(output_images_folder) - if not tf.gfile.IsDirectory(output_gifs_folder): - tf.gfile.MakeDirs(output_gifs_folder) - npy_files = [ - os.path.join(input_folder, name) - for name in tf.gfile.ListDirectory(input_folder) - if name.find('npy') >= 0 - ] - for i, npy_file in enumerate(npy_files): - print 'saving images {}/{}'.format(i, len(npy_files)) - pure_name = npy_file[npy_file.rfind('/') + 1:-4] - output_folder = os.path.join(output_images_folder, pure_name) - if not tf.gfile.IsDirectory(output_folder): - tf.gfile.MakeDirs(output_folder) - print '*******' - print pure_name[0:pure_name.find('_')] - env.reset_for_eval(which_env(pure_name), - which_goal(pure_name), - pure_name[0:pure_name.find('_')], - ) - with tf.gfile.Open(npy_file) as h: - states = np.load(h).item()['states'] - images = [ - env.observation(state)[mt.IMAGE] for state in states - ] - for j, img in enumerate(images): - cv2.imwrite(os.path.join(output_folder, '{0:03d}'.format(j) + '.jpg'), - img[:, :, ::-1]) - print 'converting to gif' - os.system( - 'convert -set delay 20 -colors 256 -dispose 1 {}/*.jpg {}.gif'.format( - output_folder, - os.path.join(output_gifs_folder, pure_name + '.gif') - ) - ) - -def evaluate_folder(env, folder_path): - """Evaluates the performance from the evals folder.""" - targets = ['fridge', 'dining_table', 'microwave', 'tv', 'couch'] - - def compute_acc(npy_file): - with tf.gfile.Open(npy_file) as h: - data = np.load(h).item() - if npy_file.find('dining_table') >= 0: - category = 'dining_table' - else: - category = npy_file[npy_file.rfind('_') + 1:-4] - return category, data['distance'][-1] - 2 - - def evaluate_iteration(folder): - """Evaluates the data from the folder of certain eval iteration.""" - print folder - npy_files = [ - os.path.join(folder, name) - for name in tf.gfile.ListDirectory(folder) - if name.find('npy') >= 0 - ] - eval_stats = {c: [] for c in targets} - for npy_file in npy_files: - try: - category, dist = compute_acc(npy_file) - except: # pylint: disable=bare-except - continue - eval_stats[category].append(float(dist <= 5)) - for c in eval_stats: - if not eval_stats[c]: - print 'incomplete eval {}: empty class {}'.format(folder_path, c) - return None - eval_stats[c] = np.mean(eval_stats[c]) - - eval_stats['mean'] = np.mean(eval_stats.values()) - return eval_stats - - checkpoint_folders = [ - folder_path + x - for x in tf.gfile.ListDirectory(folder_path) - if tf.gfile.IsDirectory(folder_path + x) - ] - - print '{} folders found'.format(len(checkpoint_folders)) - print '------------------------' - all_iters = [] - all_accs = [] - for i, folder in enumerate(checkpoint_folders): - print 'processing {}/{}'.format(i, len(checkpoint_folders)) - eval_stats = evaluate_iteration(folder) - if eval_stats is None: - continue - else: - iter_no = int(folder[folder.rfind('/') + 1:]) - print 'result ', iter_no, eval_stats['mean'] - all_accs.append(eval_stats['mean']) - all_iters.append(iter_no) - - all_accs = np.asarray(all_accs) - all_iters = np.asarray(all_iters) - idx = np.argmax(all_accs) - print 'best result at iteration {} was {}'.format(all_iters[idx], - all_accs[idx]) - order = np.argsort(all_iters) - all_iters = all_iters[order] - all_accs = all_accs[order] - #plt.plot(all_iters, all_accs) - #plt.show() - #print 'done plotting' - - best_iteration_folder = os.path.join(folder_path, str(all_iters[idx])) - - print 'generating gifs and images for {}'.format(best_iteration_folder) - visualize(env, best_iteration_folder, FLAGS.output_folder) - - -def main(_): - gin.parse_config_files_and_bindings(FLAGS.gin_config, FLAGS.gin_params) - print('********') - print(FLAGS.mode) - print(FLAGS.gin_config) - print(FLAGS.gin_params) - - env = active_vision_dataset_env.ActiveVisionDatasetEnv(modality_types=[ - task_env.ModalityTypes.IMAGE, - task_env.ModalityTypes.SEMANTIC_SEGMENTATION, - task_env.ModalityTypes.OBJECT_DETECTION, task_env.ModalityTypes.DEPTH, - task_env.ModalityTypes.DISTANCE - ]) - - if FLAGS.mode == BENCHMARK_MODE: - benchmark(env, env.possible_targets) - elif FLAGS.mode == GRAPH_MODE: - for loc in env.worlds: - env.check_scene_graph(loc, 'fridge') - elif FLAGS.mode == HUMAN_MODE: - human(env, env.possible_targets) - elif FLAGS.mode == VIS_MODE: - visualize_random_step_sequence(env) - elif FLAGS.mode == EVAL_MODE: - evaluate_folder(env, FLAGS.eval_folder) - -if __name__ == '__main__': - app.run(main) diff --git a/research/cvt_text/README.md b/research/cvt_text/README.md deleted file mode 100644 index 1c0a415b164..00000000000 --- a/research/cvt_text/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# Cross-View Training - -This repository contains code for *Semi-Supervised Sequence Modeling with Cross-View Training*. Currently sequence tagging and dependency parsing tasks are supported. - -## Requirements -* [Tensorflow](https://www.tensorflow.org/) -* [Numpy](http://www.numpy.org/) - -This code has been run with TensorFlow 1.10.1 and Numpy 1.14.5; other versions may work, but have not been tested. - -## Fetching and Preprocessing Data -Run `fetch_data.sh` to download and extract pretrained [GloVe](https://nlp.stanford.edu/projects/glove/) vectors, the [1 Billion Word Language Model Benchmark](http://www.statmt.org/lm-benchmark/) corpus of unlabeled data, and the CoNLL-2000 [text chunking](https://www.clips.uantwerpen.be/conll2000/chunking/) dataset. Unfortunately the other datasets from our paper are not freely available and so can't be included in this repository. - -To apply CVT to other datasets, the data should be placed in `data/raw_data//(train|dev|test).txt`. For sequence tagging data, each line should contain a word followed by a space followed by that word's tag. Sentences should be separated by empty lines. For dependency parsing, each tag should be of the form ``-`` (e.g., `0-root`). - -After all of the data has been downloaded, run `preprocessing.py`. - -## Training a Model -Run `python cvt.py --mode=train --model_name=chunking_model`. By default this trains a model on the chunking data downloaded with `fetch_data.sh`. To change which task(s) are trained on or model hyperparameters, modify [base/configure.py](base/configure.py). Models are automatically checkpointed every 1000 steps; training will continue from the latest checkpoint if training is interrupted and restarted. Model checkpoints and other data such as dev set accuracy over time are stored in `data/models/`. - -## Evaluating a Model -Run `python cvt.py --mode=eval --model_name=chunking_model`. A CVT model trained on the chunking data for 200k steps should get at least 97.1 F1 on the dev set and 96.6 F1 on the test set. - -## Citation -If you use this code for your publication, please cite the original paper: -``` -@inproceedings{clark2018semi, - title = {Semi-Supervised Sequence Modeling with Cross-View Training}, - author = {Kevin Clark and Minh-Thang Luong and Christopher D. Manning and Quoc V. Le}, - booktitle = {EMNLP}, - year = {2018} -} -``` - -## Contact -* [Kevin Clark](https://cs.stanford.edu/~kevclark/) (@clarkkev). -* [Thang Luong](https://nlp.stanford.edu/~lmthang/) (@lmthang). - diff --git a/research/cvt_text/__init__.py b/research/cvt_text/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cvt_text/base/__init__.py b/research/cvt_text/base/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cvt_text/base/configure.py b/research/cvt_text/base/configure.py deleted file mode 100644 index 38a69859412..00000000000 --- a/research/cvt_text/base/configure.py +++ /dev/null @@ -1,139 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Classes for storing hyperparameters, data locations, etc.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import json -from os.path import join -import tensorflow as tf - - -class Config(object): - """Stores everything needed to train a model.""" - - def __init__(self, **kwargs): - # general - self.data_dir = './data' # top directory for data (corpora, models, etc.) - self.model_name = 'default_model' # name identifying the current model - - # mode - self.mode = 'train' # either "train" or "eval" - self.task_names = ['chunk'] # list of tasks this model will learn - # more than one trains a multi-task model - self.is_semisup = True # whether to use CVT or train purely supervised - self.for_preprocessing = False # is this for the preprocessing script - - # embeddings - self.pretrained_embeddings = 'glove.6B.300d.txt' # which pretrained - # embeddings to use - self.word_embedding_size = 300 # size of each word embedding - - # encoder - self.use_chars = True # whether to include a character-level cnn - self.char_embedding_size = 50 # size of character embeddings - self.char_cnn_filter_widths = [2, 3, 4] # filter widths for the char cnn - self.char_cnn_n_filters = 100 # number of filters for each filter width - self.unidirectional_sizes = [1024] # size of first Bi-LSTM - self.bidirectional_sizes = [512] # size of second Bi-LSTM - self.projection_size = 512 # projections size for LSTMs and hidden layers - - # dependency parsing - self.depparse_projection_size = 128 # size of the representations used in - # the bilinear classifier for parsing - - # tagging - self.label_encoding = 'BIOES' # label encoding scheme for entity-level - # tagging tasks - self.label_smoothing = 0.1 # label smoothing rate for tagging tasks - - # optimization - self.lr = 0.5 # base learning rate - self.momentum = 0.9 # momentum - self.grad_clip = 1.0 # maximum gradient norm during optimization - self.warm_up_steps = 5000.0 # linearly ramp up the lr for this many steps - self.lr_decay = 0.005 # factor for gradually decaying the lr - - # EMA - self.ema_decay = 0.998 # EMA coefficient for averaged model weights - self.ema_test = True # whether to use EMA weights at test time - self.ema_teacher = False # whether to use EMA weights for the teacher model - - # regularization - self.labeled_keep_prob = 0.5 # 1 - dropout on labeled examples - self.unlabeled_keep_prob = 0.8 # 1 - dropout on unlabeled examples - - # sizing - self.max_sentence_length = 100 # maximum length of unlabeled sentences - self.max_word_length = 20 # maximum length of words for char cnn - self.train_batch_size = 64 # train batch size - self.test_batch_size = 64 # test batch size - self.buckets = [(0, 15), (15, 40), (40, 1000)] # buckets for binning - # sentences by length - - # training - self.print_every = 25 # how often to print out training progress - self.eval_dev_every = 500 # how often to evaluate on the dev set - self.eval_train_every = 2000 # how often to evaluate on the train set - self.save_model_every = 1000 # how often to checkpoint the model - - # data set - self.train_set_percent = 100 # how much of the train set to use - - for k, v in kwargs.iteritems(): - if k not in self.__dict__: - raise ValueError("Unknown argument", k) - self.__dict__[k] = v - - self.dev_set = self.mode == "train" # whether to evaluate on the dev or - # test set - - # locations of various data files - self.raw_data_topdir = join(self.data_dir, 'raw_data') - self.unsupervised_data = join( - self.raw_data_topdir, - 'unlabeled_data', - '1-billion-word-language-modeling-benchmark-r13output', - 'training-monolingual.tokenized.shuffled') - self.pretrained_embeddings_file = join( - self.raw_data_topdir, 'pretrained_embeddings', - self.pretrained_embeddings) - - self.preprocessed_data_topdir = join(self.data_dir, 'preprocessed_data') - self.embeddings_dir = join(self.preprocessed_data_topdir, - self.pretrained_embeddings.rsplit('.', 1)[0]) - self.word_vocabulary = join(self.embeddings_dir, 'word_vocabulary.pkl') - self.word_embeddings = join(self.embeddings_dir, 'word_embeddings.pkl') - - self.model_dir = join(self.data_dir, "models", self.model_name) - self.checkpoints_dir = join(self.model_dir, 'checkpoints') - self.checkpoint = join(self.checkpoints_dir, 'checkpoint.ckpt') - self.best_model_checkpoints_dir = join( - self.model_dir, 'best_model_checkpoints') - self.best_model_checkpoint = join( - self.best_model_checkpoints_dir, 'checkpoint.ckpt') - self.progress = join(self.checkpoints_dir, 'progress.pkl') - self.summaries_dir = join(self.model_dir, 'summaries') - self.history_file = join(self.model_dir, 'history.pkl') - - def write(self): - tf.gfile.MakeDirs(self.model_dir) - with open(join(self.model_dir, 'config.json'), 'w') as f: - f.write(json.dumps(self.__dict__, sort_keys=True, indent=4, - separators=(',', ': '))) - diff --git a/research/cvt_text/base/embeddings.py b/research/cvt_text/base/embeddings.py deleted file mode 100644 index 8863f547efb..00000000000 --- a/research/cvt_text/base/embeddings.py +++ /dev/null @@ -1,167 +0,0 @@ -# coding=utf-8 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - - -"""Utilities for handling word embeddings.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import re -import numpy as np -import tensorflow as tf - -from base import utils - - -_CHARS = [ - # punctuation - '!', '\'', '#', '$', '%', '&', '"', '(', ')', '*', '+', ',', '-', '.', - '/', '\\', '_', '`', '{', '}', '[', ']', '<', '>', ':', ';', '?', '@', - # digits - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - # letters - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', - 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', - 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', - # special characters - '£', '€', '®', '™', '�', '½', '»', '•', '—', '“', '”', '°', '‘', '’' -] - -# words not in GloVe that still should have embeddings -_EXTRA_WORDS = [ - # common digit patterns - '0/0', '0/00', '00/00', '0/000', - '00/00/00', '0/00/00', '00/00/0000', '0/00/0000', - '00-00', '00-00-00', '0-00-00', '00-00-0000', '0-00-0000', '0000-00-00', - '00-0-00-0', '00000000', '0:00.000', '00:00.000', - '0%', '00%', '00.' '0000.', '0.0bn', '0.0m', '0-', '00-', - # ontonotes uses **f to represent formulas and -amp- instead of amperstands - '**f', '-amp-' -] -SPECIAL_TOKENS = ['', '', '', '', ''] -NUM_CHARS = len(_CHARS) + len(SPECIAL_TOKENS) -PAD, UNK, START, END, MISSING = 0, 1, 2, 3, 4 - - -class Vocabulary(collections.OrderedDict): - def __getitem__(self, w): - return self.get(w, UNK) - - -@utils.Memoize -def get_char_vocab(): - characters = _CHARS - for i, special in enumerate(SPECIAL_TOKENS): - characters.insert(i, special) - return Vocabulary({c: i for i, c in enumerate(characters)}) - - -@utils.Memoize -def get_inv_char_vocab(): - return {i: c for c, i in get_char_vocab().items()} - - -def get_word_vocab(config): - return Vocabulary(utils.load_cpickle(config.word_vocabulary)) - - -def get_word_embeddings(config): - return utils.load_cpickle(config.word_embeddings) - - -@utils.Memoize -def _punctuation_ids(vocab_path): - vocab = Vocabulary(utils.load_cpickle(vocab_path)) - return set(i for w, i in vocab.iteritems() if w in [ - '!', '...', '``', '{', '}', '(', ')', '[', ']', '--', '-', ',', '.', - "''", '`', ';', ':', '?']) - - -def get_punctuation_ids(config): - return _punctuation_ids(config.word_vocabulary) - - -class PretrainedEmbeddingLoader(object): - def __init__(self, config): - self.config = config - self.vocabulary = {} - self.vectors = [] - self.vector_size = config.word_embedding_size - - def _add_vector(self, w): - if w not in self.vocabulary: - self.vocabulary[w] = len(self.vectors) - self.vectors.append(np.zeros(self.vector_size, dtype='float32')) - - def build(self): - utils.log('loading pretrained embeddings from', - self.config.pretrained_embeddings_file) - for special in SPECIAL_TOKENS: - self._add_vector(special) - for extra in _EXTRA_WORDS: - self._add_vector(extra) - with tf.gfile.GFile( - self.config.pretrained_embeddings_file, 'r') as f: - for i, line in enumerate(f): - if i % 10000 == 0: - utils.log('on line', i) - - split = line.decode('utf8').split() - w = normalize_word(split[0]) - - try: - vec = np.array(map(float, split[1:]), dtype='float32') - if vec.size != self.vector_size: - utils.log('vector for line', i, 'has size', vec.size, 'so skipping') - utils.log(line[:100] + '...') - continue - except: - utils.log('can\'t parse line', i, 'so skipping') - utils.log(line[:100] + '...') - continue - if w not in self.vocabulary: - self.vocabulary[w] = len(self.vectors) - self.vectors.append(vec) - utils.log('writing vectors!') - self._write() - - def _write(self): - utils.write_cpickle(np.vstack(self.vectors), self.config.word_embeddings) - utils.write_cpickle(self.vocabulary, self.config.word_vocabulary) - - -def normalize_chars(w): - if w == '-LRB-': - return '(' - elif w == '-RRB-': - return ')' - elif w == '-LCB-': - return '{' - elif w == '-RCB-': - return '}' - elif w == '-LSB-': - return '[' - elif w == '-RSB-': - return ']' - return w.replace(r'\/', '/').replace(r'\*', '*') - - -def normalize_word(w): - return re.sub(r'\d', '0', normalize_chars(w).lower()) diff --git a/research/cvt_text/base/utils.py b/research/cvt_text/base/utils.py deleted file mode 100644 index 3a9ee40d5a5..00000000000 --- a/research/cvt_text/base/utils.py +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Various utilities.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import cPickle -import sys -import tensorflow as tf - - -class Memoize(object): - def __init__(self, f): - self.f = f - self.cache = {} - - def __call__(self, *args): - if args not in self.cache: - self.cache[args] = self.f(*args) - return self.cache[args] - - -def load_cpickle(path, memoized=True): - return _load_cpickle_memoize(path) if memoized else _load_cpickle(path) - - -def _load_cpickle(path): - with tf.gfile.GFile(path, 'r') as f: - return cPickle.load(f) - - -@Memoize -def _load_cpickle_memoize(path): - return _load_cpickle(path) - - -def write_cpickle(o, path): - tf.gfile.MakeDirs(path.rsplit('/', 1)[0]) - with tf.gfile.GFile(path, 'w') as f: - cPickle.dump(o, f, -1) - - -def log(*args): - msg = ' '.join(map(str, args)) - sys.stdout.write(msg + '\n') - sys.stdout.flush() - - -def heading(*args): - log() - log(80 * '=') - log(*args) - log(80 * '=') diff --git a/research/cvt_text/corpus_processing/__init__.py b/research/cvt_text/corpus_processing/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cvt_text/corpus_processing/example.py b/research/cvt_text/corpus_processing/example.py deleted file mode 100644 index 023d2fa07cf..00000000000 --- a/research/cvt_text/corpus_processing/example.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base class for training examples.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from base import embeddings - - -CONTRACTION_WORDS = set(w + 'n' for w in - ['do', 'does', 'did', 'is', 'are', 'was', 'were', 'has', - 'have', 'had', 'could', 'would', 'should', 'ca', 'wo', - 'ai', 'might']) - - -class Example(object): - def __init__(self, words, word_vocab, char_vocab): - words = words[:] - # Fix inconsistent tokenization between datasets - for i in range(len(words)): - if (words[i].lower() == '\'t' and i > 0 and - words[i - 1].lower() in CONTRACTION_WORDS): - words[i] = words[i - 1][-1] + words[i] - words[i - 1] = words[i - 1][:-1] - - self.words = ([embeddings.START] + - [word_vocab[embeddings.normalize_word(w)] for w in words] + - [embeddings.END]) - self.chars = ([[embeddings.MISSING]] + - [[char_vocab[c] for c in embeddings.normalize_chars(w)] - for w in words] + - [[embeddings.MISSING]]) - - def __repr__(self,): - inv_char_vocab = embeddings.get_inv_char_vocab() - return ' '.join([''.join([inv_char_vocab[c] for c in w]) - for w in self.chars]) diff --git a/research/cvt_text/corpus_processing/minibatching.py b/research/cvt_text/corpus_processing/minibatching.py deleted file mode 100644 index c0ebbf723db..00000000000 --- a/research/cvt_text/corpus_processing/minibatching.py +++ /dev/null @@ -1,143 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Utilities for constructing minibatches.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import random -import numpy as np - -from base import embeddings - - -def get_bucket(config, l): - for i, (s, e) in enumerate(config.buckets): - if s <= l < e: - return config.buckets[i] - - -def build_array(nested_lists, dtype='int32'): - depth_to_sizes = collections.defaultdict(set) - _get_sizes(nested_lists, depth_to_sizes) - shape = [max(depth_to_sizes[depth]) for depth in range(len(depth_to_sizes))] - - copy_depth = len(depth_to_sizes) - 1 - while copy_depth > 0 and len(depth_to_sizes[copy_depth]) == 1: - copy_depth -= 1 - - arr = np.zeros(shape, dtype=dtype) - _fill_array(nested_lists, arr, copy_depth) - - return arr - - -def _get_sizes(nested_lists, depth_to_sizes, depth=0): - depth_to_sizes[depth].add(len(nested_lists)) - first_elem = nested_lists[0] - if (isinstance(first_elem, collections.Sequence) or - isinstance(first_elem, np.ndarray)): - for sublist in nested_lists: - _get_sizes(sublist, depth_to_sizes, depth + 1) - - -def _fill_array(nested_lists, arr, copy_depth, depth=0): - if depth == copy_depth: - for i in range(len(nested_lists)): - if isinstance(nested_lists[i], np.ndarray): - arr[i] = nested_lists[i] - else: - arr[i] = np.array(nested_lists[i]) - else: - for i in range(len(nested_lists)): - _fill_array(nested_lists[i], arr[i], copy_depth, depth + 1) - - -class Dataset(object): - def __init__(self, config, examples, task_name='unlabeled', is_training=False): - self._config = config - self.examples = examples - self.size = len(examples) - self.task_name = task_name - self.is_training = is_training - - def get_minibatches(self, minibatch_size): - by_bucket = collections.defaultdict(list) - for i, e in enumerate(self.examples): - by_bucket[get_bucket(self._config, len(e.words))].append(i) - - # save memory by weighting examples so longer sentences have - # smaller minibatches. - weight = lambda ind: np.sqrt(len(self.examples[ind].words)) - total_weight = float(sum(weight(i) for i in range(len(self.examples)))) - weight_per_batch = minibatch_size * total_weight / len(self.examples) - cumulative_weight = 0.0 - id_batches = [] - for _, ids in by_bucket.iteritems(): - ids = np.array(ids) - np.random.shuffle(ids) - curr_batch, curr_weight = [], 0.0 - for i, curr_id in enumerate(ids): - curr_batch.append(curr_id) - curr_weight += weight(curr_id) - if (i == len(ids) - 1 or cumulative_weight + curr_weight >= - (len(id_batches) + 1) * weight_per_batch): - cumulative_weight += curr_weight - id_batches.append(np.array(curr_batch)) - curr_batch, curr_weight = [], 0.0 - random.shuffle(id_batches) - - for id_batch in id_batches: - yield self._make_minibatch(id_batch) - - def endless_minibatches(self, minibatch_size): - while True: - for mb in self.get_minibatches(minibatch_size): - yield mb - - def _make_minibatch(self, ids): - examples = [self.examples[i] for i in ids] - sentence_lengths = np.array([len(e.words) for e in examples]) - max_word_length = min(max(max(len(word) for word in e.chars) - for e in examples), - self._config.max_word_length) - characters = [[[embeddings.PAD] + [embeddings.START] + w[:max_word_length] + - [embeddings.END] + [embeddings.PAD] for w in e.chars] - for e in examples] - # the first and last words are masked because they are start/end tokens - mask = build_array([[0] + [1] * (length - 2) + [0] - for length in sentence_lengths]) - words = build_array([e.words for e in examples]) - chars = build_array(characters, dtype='int16') - return Minibatch( - task_name=self.task_name, - size=ids.size, - examples=examples, - ids=ids, - teacher_predictions={}, - words=words, - chars=chars, - lengths=sentence_lengths, - mask=mask, - ) - - -Minibatch = collections.namedtuple('Minibatch', [ - 'task_name', 'size', 'examples', 'ids', 'teacher_predictions', - 'words', 'chars', 'lengths', 'mask' -]) diff --git a/research/cvt_text/corpus_processing/scorer.py b/research/cvt_text/corpus_processing/scorer.py deleted file mode 100644 index 8173dae36d8..00000000000 --- a/research/cvt_text/corpus_processing/scorer.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Abstract base class for evaluation.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc - - -class Scorer(object): - __metaclass__ = abc.ABCMeta - - def __init__(self): - self._updated = False - self._cached_results = {} - - @abc.abstractmethod - def update(self, examples, predictions, loss): - self._updated = True - - @abc.abstractmethod - def get_loss(self): - pass - - @abc.abstractmethod - def _get_results(self): - return [] - - def get_results(self, prefix=""): - results = self._get_results() if self._updated else self._cached_results - self._cached_results = results - self._updated = False - return [(prefix + k, v) for k, v in results] - - def results_str(self): - return " - ".join(["{:}: {:.2f}".format(k, v) - for k, v in self.get_results()]) diff --git a/research/cvt_text/corpus_processing/unlabeled_data.py b/research/cvt_text/corpus_processing/unlabeled_data.py deleted file mode 100644 index 0021c50618a..00000000000 --- a/research/cvt_text/corpus_processing/unlabeled_data.py +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Reads data from a large unlabeled corpus.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import tensorflow as tf - -from base import embeddings -from corpus_processing import example -from corpus_processing import minibatching - - -class UnlabeledDataReader(object): - def __init__(self, config, starting_file=0, starting_line=0, one_pass=False): - self.config = config - self.current_file = starting_file - self.current_line = starting_line - self._one_pass = one_pass - - def endless_minibatches(self): - for examples in self.get_unlabeled_examples(): - d = minibatching.Dataset(self.config, examples, 'unlabeled') - for mb in d.get_minibatches(self.config.train_batch_size): - yield mb - - def _make_examples(self, sentences): - word_vocab = embeddings.get_word_vocab(self.config) - char_vocab = embeddings.get_char_vocab() - return [ - example.Example(sentence, word_vocab, char_vocab) - for sentence in sentences - ] - - def get_unlabeled_examples(self): - lines = [] - for words in self.get_unlabeled_sentences(): - lines.append(words) - if len(lines) >= 10000: - yield self._make_examples(lines) - lines = [] - - def get_unlabeled_sentences(self): - while True: - file_ids_and_names = sorted([ - (int(fname.split('-')[1].replace('.txt', '')), fname) for fname in - tf.gfile.ListDirectory(self.config.unsupervised_data)]) - for fid, fname in file_ids_and_names: - if fid < self.current_file: - continue - self.current_file = fid - self.current_line = 0 - with tf.gfile.FastGFile(os.path.join(self.config.unsupervised_data, - fname), 'r') as f: - for i, line in enumerate(f): - if i < self.current_line: - continue - self.current_line = i - words = line.strip().split() - if len(words) < self.config.max_sentence_length: - yield words - self.current_file = 0 - self.current_line = 0 - if self._one_pass: - break diff --git a/research/cvt_text/cvt.py b/research/cvt_text/cvt.py deleted file mode 100644 index 593ce5bb62e..00000000000 --- a/research/cvt_text/cvt.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Run training and evaluation for CVT text models.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from base import configure -from base import utils -from training import trainer -from training import training_progress - - -FLAGS = tf.app.flags.FLAGS -tf.app.flags.DEFINE_string('mode', 'train', '"train" or "eval') -tf.app.flags.DEFINE_string('model_name', 'default_model', - 'A name identifying the model being ' - 'trained/evaluated') - - -def main(): - utils.heading('SETUP') - config = configure.Config(mode=FLAGS.mode, model_name=FLAGS.model_name) - config.write() - with tf.Graph().as_default() as graph: - model_trainer = trainer.Trainer(config) - summary_writer = tf.summary.FileWriter(config.summaries_dir) - checkpoints_saver = tf.train.Saver(max_to_keep=1) - best_model_saver = tf.train.Saver(max_to_keep=1) - init_op = tf.global_variables_initializer() - graph.finalize() - with tf.Session() as sess: - sess.run(init_op) - progress = training_progress.TrainingProgress( - config, sess, checkpoints_saver, best_model_saver, - config.mode == 'train') - utils.log() - if config.mode == 'train': - utils.heading('START TRAINING ({:})'.format(config.model_name)) - model_trainer.train(sess, progress, summary_writer) - elif config.mode == 'eval': - utils.heading('RUN EVALUATION ({:})'.format(config.model_name)) - progress.best_model_saver.restore(sess, tf.train.latest_checkpoint( - config.checkpoints_dir)) - model_trainer.evaluate_all_tasks(sess, summary_writer, None) - else: - raise ValueError('Mode must be "train" or "eval"') - - -if __name__ == '__main__': - main() diff --git a/research/cvt_text/fetch_data.sh b/research/cvt_text/fetch_data.sh deleted file mode 100755 index dcdb54cf883..00000000000 --- a/research/cvt_text/fetch_data.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -TOPDIR='./data' -RUNDIR=${PWD} - -mkdir -p ${TOPDIR} -cd ${TOPDIR} -mkdir -p raw_data -mkdir -p raw_data/pretrained_embeddings -mkdir -p raw_data/unlabeled_data -mkdir -p raw_data/chunk -cd ${RUNDIR} - -echo "Preparing GloVe embeddings" -cd "${TOPDIR}/raw_data/pretrained_embeddings" -curl -OL http://nlp.stanford.edu/data/glove.6B.zip -unzip glove.6B.zip -cd ${RUNDIR} -echo - -echo "Preparing lm1b corpus" -cd "${TOPDIR}/raw_data/unlabeled_data" -curl -OL http://www.statmt.org/lm-benchmark/1-billion-word-language-modeling-benchmark-r13output.tar.gz -tar xzf 1-billion-word-language-modeling-benchmark-r13output.tar.gz -cd ${RUNDIR} -echo - -echo "Preparing chunking corpus" -cd "${TOPDIR}/raw_data/chunk" -curl -OL https://www.clips.uantwerpen.be/conll2000/chunking/train.txt.gz -curl -OL http://www.clips.uantwerpen.be/conll2000/chunking/test.txt.gz -gunzip * -cd ${RUNDIR} -echo - -echo "Done with data fetching!" - diff --git a/research/cvt_text/model/__init__.py b/research/cvt_text/model/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cvt_text/model/encoder.py b/research/cvt_text/model/encoder.py deleted file mode 100644 index 4b6da1255f5..00000000000 --- a/research/cvt_text/model/encoder.py +++ /dev/null @@ -1,110 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""CNN-BiLSTM sentence encoder.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf -from base import embeddings -from model import model_helpers - - -class Encoder(object): - def __init__(self, config, inputs, pretrained_embeddings): - self._config = config - self._inputs = inputs - - self.word_reprs = self._get_word_reprs(pretrained_embeddings) - self.uni_fw, self.uni_bw = self._get_unidirectional_reprs(self.word_reprs) - self.uni_reprs = tf.concat([self.uni_fw, self.uni_bw], axis=-1) - self.bi_fw, self.bi_bw, self.bi_reprs = self._get_bidirectional_reprs( - self.uni_reprs) - - def _get_word_reprs(self, pretrained_embeddings): - with tf.variable_scope('word_embeddings'): - word_embedding_matrix = tf.get_variable( - 'word_embedding_matrix', initializer=pretrained_embeddings) - word_embeddings = tf.nn.embedding_lookup( - word_embedding_matrix, self._inputs.words) - word_embeddings = tf.nn.dropout(word_embeddings, self._inputs.keep_prob) - word_embeddings *= tf.get_variable('emb_scale', initializer=1.0) - - if not self._config.use_chars: - return word_embeddings - - with tf.variable_scope('char_embeddings'): - char_embedding_matrix = tf.get_variable( - 'char_embeddings', - shape=[embeddings.NUM_CHARS, self._config.char_embedding_size]) - char_embeddings = tf.nn.embedding_lookup(char_embedding_matrix, - self._inputs.chars) - shape = tf.shape(char_embeddings) - char_embeddings = tf.reshape( - char_embeddings, - shape=[-1, shape[-2], self._config.char_embedding_size]) - char_reprs = [] - for filter_width in self._config.char_cnn_filter_widths: - conv = tf.layers.conv1d( - char_embeddings, self._config.char_cnn_n_filters, filter_width) - conv = tf.nn.relu(conv) - conv = tf.nn.dropout(tf.reduce_max(conv, axis=1), - self._inputs.keep_prob) - conv = tf.reshape(conv, shape=[-1, shape[1], - self._config.char_cnn_n_filters]) - char_reprs.append(conv) - return tf.concat([word_embeddings] + char_reprs, axis=-1) - - def _get_unidirectional_reprs(self, word_reprs): - with tf.variable_scope('unidirectional_reprs'): - word_lstm_input_size = ( - self._config.word_embedding_size if not self._config.use_chars else - (self._config.word_embedding_size + - len(self._config.char_cnn_filter_widths) - * self._config.char_cnn_n_filters)) - word_reprs.set_shape([None, None, word_lstm_input_size]) - (outputs_fw, outputs_bw), _ = tf.nn.bidirectional_dynamic_rnn( - model_helpers.multi_lstm_cell(self._config.unidirectional_sizes, - self._inputs.keep_prob, - self._config.projection_size), - model_helpers.multi_lstm_cell(self._config.unidirectional_sizes, - self._inputs.keep_prob, - self._config.projection_size), - word_reprs, - dtype=tf.float32, - sequence_length=self._inputs.lengths, - scope='unilstm' - ) - return outputs_fw, outputs_bw - - def _get_bidirectional_reprs(self, uni_reprs): - with tf.variable_scope('bidirectional_reprs'): - current_outputs = uni_reprs - outputs_fw, outputs_bw = None, None - for size in self._config.bidirectional_sizes: - (outputs_fw, outputs_bw), _ = tf.nn.bidirectional_dynamic_rnn( - model_helpers.lstm_cell(size, self._inputs.keep_prob, - self._config.projection_size), - model_helpers.lstm_cell(size, self._inputs.keep_prob, - self._config.projection_size), - current_outputs, - dtype=tf.float32, - sequence_length=self._inputs.lengths, - scope='bilstm' - ) - current_outputs = tf.concat([outputs_fw, outputs_bw], axis=-1) - return outputs_fw, outputs_bw, current_outputs diff --git a/research/cvt_text/model/model_helpers.py b/research/cvt_text/model/model_helpers.py deleted file mode 100644 index 3c0cb670c45..00000000000 --- a/research/cvt_text/model/model_helpers.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Utilities for building the model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - - -def project(input_layers, size, name='projection'): - return tf.add_n([tf.layers.dense(layer, size, name=name + '_' + str(i)) - for i, layer in enumerate(input_layers)]) - - -def lstm_cell(cell_size, keep_prob, num_proj): - return tf.contrib.rnn.DropoutWrapper( - tf.contrib.rnn.LSTMCell(cell_size, num_proj=min(cell_size, num_proj)), - output_keep_prob=keep_prob) - - -def multi_lstm_cell(cell_sizes, keep_prob, num_proj): - return tf.contrib.rnn.MultiRNNCell([lstm_cell(cell_size, keep_prob, num_proj) - for cell_size in cell_sizes]) - - -def masked_ce_loss(logits, labels, mask, sparse=False, roll_direction=0): - if roll_direction != 0: - labels = _roll(labels, roll_direction, sparse) - mask *= _roll(mask, roll_direction, True) - ce = ((tf.nn.sparse_softmax_cross_entropy_with_logits if sparse - else tf.nn.softmax_cross_entropy_with_logits_v2) - (logits=logits, labels=labels)) - return tf.reduce_sum(mask * ce) / tf.to_float(tf.reduce_sum(mask)) - - -def _roll(arr, direction, sparse=False): - if sparse: - return tf.concat([arr[:, direction:], arr[:, :direction]], axis=1) - return tf.concat([arr[:, direction:, :], arr[:, :direction, :]], axis=1) diff --git a/research/cvt_text/model/multitask_model.py b/research/cvt_text/model/multitask_model.py deleted file mode 100644 index 16dfdf7da6d..00000000000 --- a/research/cvt_text/model/multitask_model.py +++ /dev/null @@ -1,132 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A multi-task and semi-supervised NLP model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from model import encoder -from model import shared_inputs - - -class Inference(object): - def __init__(self, config, inputs, pretrained_embeddings, tasks): - with tf.variable_scope('encoder'): - self.encoder = encoder.Encoder(config, inputs, pretrained_embeddings) - self.modules = {} - for task in tasks: - with tf.variable_scope(task.name): - self.modules[task.name] = task.get_module(inputs, self.encoder) - - -class Model(object): - def __init__(self, config, pretrained_embeddings, tasks): - self._config = config - self._tasks = tasks - - self._global_step, self._optimizer = self._get_optimizer() - self._inputs = shared_inputs.Inputs(config) - with tf.variable_scope('model', reuse=tf.AUTO_REUSE) as scope: - inference = Inference(config, self._inputs, pretrained_embeddings, - tasks) - self._trainer = inference - self._tester = inference - self._teacher = inference - if config.ema_test or config.ema_teacher: - ema = tf.train.ExponentialMovingAverage(config.ema_decay) - model_vars = tf.get_collection("trainable_variables", "model") - ema_op = ema.apply(model_vars) - tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, ema_op) - - def ema_getter(getter, name, *args, **kwargs): - var = getter(name, *args, **kwargs) - return ema.average(var) - - scope.set_custom_getter(ema_getter) - inference_ema = Inference( - config, self._inputs, pretrained_embeddings, tasks) - if config.ema_teacher: - self._teacher = inference_ema - if config.ema_test: - self._tester = inference_ema - - self._unlabeled_loss = self._get_consistency_loss(tasks) - self._unlabeled_train_op = self._get_train_op(self._unlabeled_loss) - self._labeled_train_ops = {} - for task in self._tasks: - task_loss = self._trainer.modules[task.name].supervised_loss - self._labeled_train_ops[task.name] = self._get_train_op(task_loss) - - def _get_consistency_loss(self, tasks): - return sum([self._trainer.modules[task.name].unsupervised_loss - for task in tasks]) - - def _get_optimizer(self): - global_step = tf.get_variable('global_step', initializer=0, trainable=False) - warm_up_multiplier = (tf.minimum(tf.to_float(global_step), - self._config.warm_up_steps) - / self._config.warm_up_steps) - decay_multiplier = 1.0 / (1 + self._config.lr_decay * - tf.sqrt(tf.to_float(global_step))) - lr = self._config.lr * warm_up_multiplier * decay_multiplier - optimizer = tf.train.MomentumOptimizer(lr, self._config.momentum) - return global_step, optimizer - - def _get_train_op(self, loss): - grads, vs = zip(*self._optimizer.compute_gradients(loss)) - grads, _ = tf.clip_by_global_norm(grads, self._config.grad_clip) - update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) - with tf.control_dependencies(update_ops): - return self._optimizer.apply_gradients( - zip(grads, vs), global_step=self._global_step) - - def _create_feed_dict(self, mb, model, is_training=True): - feed = self._inputs.create_feed_dict(mb, is_training) - if mb.task_name in model.modules: - model.modules[mb.task_name].update_feed_dict(feed, mb) - else: - for module in model.modules.values(): - module.update_feed_dict(feed, mb) - return feed - - def train_unlabeled(self, sess, mb): - return sess.run([self._unlabeled_train_op, self._unlabeled_loss], - feed_dict=self._create_feed_dict(mb, self._trainer))[1] - - def train_labeled(self, sess, mb): - return sess.run([self._labeled_train_ops[mb.task_name], - self._trainer.modules[mb.task_name].supervised_loss,], - feed_dict=self._create_feed_dict(mb, self._trainer))[1] - - def run_teacher(self, sess, mb): - result = sess.run({task.name: self._teacher.modules[task.name].probs - for task in self._tasks}, - feed_dict=self._create_feed_dict(mb, self._teacher, - False)) - for task_name, probs in result.iteritems(): - mb.teacher_predictions[task_name] = probs.astype('float16') - - def test(self, sess, mb): - return sess.run( - [self._tester.modules[mb.task_name].supervised_loss, - self._tester.modules[mb.task_name].preds], - feed_dict=self._create_feed_dict(mb, self._tester, False)) - - def get_global_step(self, sess): - return sess.run(self._global_step) diff --git a/research/cvt_text/model/shared_inputs.py b/research/cvt_text/model/shared_inputs.py deleted file mode 100644 index 2a97004b327..00000000000 --- a/research/cvt_text/model/shared_inputs.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Placeholders for non-task-specific model inputs.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - - -class Inputs(object): - def __init__(self, config): - self._config = config - self.keep_prob = tf.placeholder(tf.float32, name='keep_prob') - self.label_smoothing = tf.placeholder(tf.float32, name='label_smoothing') - self.lengths = tf.placeholder(tf.int32, shape=[None], name='lengths') - self.mask = tf.placeholder(tf.float32, [None, None], name='mask') - self.words = tf.placeholder(tf.int32, shape=[None, None], name='words') - self.chars = tf.placeholder(tf.int32, shape=[None, None, None], - name='chars') - - def create_feed_dict(self, mb, is_training): - cvt = mb.task_name == 'unlabeled' - return { - self.keep_prob: 1.0 if not is_training else - (self._config.unlabeled_keep_prob if cvt else - self._config.labeled_keep_prob), - self.label_smoothing: self._config.label_smoothing - if (is_training and not cvt) else 0.0, - self.lengths: mb.lengths, - self.words: mb.words, - self.chars: mb.chars, - self.mask: mb.mask.astype('float32') - } diff --git a/research/cvt_text/model/task_module.py b/research/cvt_text/model/task_module.py deleted file mode 100644 index 92440b4d98a..00000000000 --- a/research/cvt_text/model/task_module.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base classes for task-specific modules.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc - - -class SupervisedModule(object): - __metaclass__ = abc.ABCMeta - - def __init__(self): - self.supervised_loss = NotImplemented - self.probs = NotImplemented - self.preds = NotImplemented - - @abc.abstractmethod - def update_feed_dict(self, feed, mb): - pass - - -class SemiSupervisedModule(SupervisedModule): - __metaclass__ = abc.ABCMeta - - def __init__(self): - super(SemiSupervisedModule, self).__init__() - self.unsupervised_loss = NotImplemented - diff --git a/research/cvt_text/preprocessing.py b/research/cvt_text/preprocessing.py deleted file mode 100644 index 3dbf106be57..00000000000 --- a/research/cvt_text/preprocessing.py +++ /dev/null @@ -1,87 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -""" -Preprocesses pretrained word embeddings, creates dev sets for tasks without a -provided one, and figures out the set of output classes for each task. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import random - -from base import configure -from base import embeddings -from base import utils -from task_specific.word_level import word_level_data - - -def main(data_dir='./data'): - random.seed(0) - - utils.log("BUILDING WORD VOCABULARY/EMBEDDINGS") - for pretrained in ['glove.6B.300d.txt']: - config = configure.Config(data_dir=data_dir, - for_preprocessing=True, - pretrained_embeddings=pretrained, - word_embedding_size=300) - embeddings.PretrainedEmbeddingLoader(config).build() - - utils.log("CONSTRUCTING DEV SETS") - for task_name in ["chunk"]: - # chunking does not come with a provided dev split, so create one by - # selecting a random subset of the data - config = configure.Config(data_dir=data_dir, - for_preprocessing=True) - task_data_dir = os.path.join(config.raw_data_topdir, task_name) + '/' - train_sentences = word_level_data.TaggedDataLoader( - config, task_name, False).get_labeled_sentences("train") - random.shuffle(train_sentences) - write_sentences(task_data_dir + 'train_subset.txt', train_sentences[1500:]) - write_sentences(task_data_dir + 'dev.txt', train_sentences[:1500]) - - utils.log("WRITING LABEL MAPPINGS") - for task_name in ["chunk"]: - for i, label_encoding in enumerate(["BIOES"]): - config = configure.Config(data_dir=data_dir, - for_preprocessing=True, - label_encoding=label_encoding) - token_level = task_name in ["ccg", "pos", "depparse"] - loader = word_level_data.TaggedDataLoader(config, task_name, token_level) - if token_level: - if i != 0: - continue - utils.log("WRITING LABEL MAPPING FOR", task_name.upper()) - else: - utils.log(" Writing label mapping for", task_name.upper(), - label_encoding) - utils.log(" ", len(loader.label_mapping), "classes") - utils.write_cpickle(loader.label_mapping, - loader.label_mapping_path) - - -def write_sentences(fname, sentences): - with open(fname, 'w') as f: - for words, tags in sentences: - for word, tag in zip(words, tags): - f.write(word + " " + tag + "\n") - f.write("\n") - - -if __name__ == '__main__': - main() diff --git a/research/cvt_text/task_specific/__init__.py b/research/cvt_text/task_specific/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cvt_text/task_specific/task_definitions.py b/research/cvt_text/task_specific/task_definitions.py deleted file mode 100644 index f13fb559285..00000000000 --- a/research/cvt_text/task_specific/task_definitions.py +++ /dev/null @@ -1,91 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Defines all the tasks the model can learn.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc - -from base import embeddings -from task_specific.word_level import depparse_module -from task_specific.word_level import depparse_scorer -from task_specific.word_level import tagging_module -from task_specific.word_level import tagging_scorers -from task_specific.word_level import word_level_data - - -class Task(object): - __metaclass__ = abc.ABCMeta - - def __init__(self, config, name, loader): - self.config = config - self.name = name - self.loader = loader - self.train_set = self.loader.get_dataset("train") - self.val_set = self.loader.get_dataset("dev" if config.dev_set else "test") - - @abc.abstractmethod - def get_module(self, inputs, encoder): - pass - - @abc.abstractmethod - def get_scorer(self): - pass - - -class Tagging(Task): - def __init__(self, config, name, is_token_level=True): - super(Tagging, self).__init__( - config, name, word_level_data.TaggedDataLoader( - config, name, is_token_level)) - self.n_classes = len(set(self.loader.label_mapping.values())) - self.is_token_level = is_token_level - - def get_module(self, inputs, encoder): - return tagging_module.TaggingModule( - self.config, self.name, self.n_classes, inputs, encoder) - - def get_scorer(self): - if self.is_token_level: - return tagging_scorers.AccuracyScorer() - else: - return tagging_scorers.EntityLevelF1Scorer(self.loader.label_mapping) - - -class DependencyParsing(Tagging): - def __init__(self, config, name): - super(DependencyParsing, self).__init__(config, name, True) - - def get_module(self, inputs, encoder): - return depparse_module.DepparseModule( - self.config, self.name, self.n_classes, inputs, encoder) - - def get_scorer(self): - return depparse_scorer.DepparseScorer( - self.n_classes, (embeddings.get_punctuation_ids(self.config))) - - -def get_task(config, name): - if name in ["ccg", "pos"]: - return Tagging(config, name, True) - elif name in ["chunk", "ner", "er"]: - return Tagging(config, name, False) - elif name == "depparse": - return DependencyParsing(config, name) - else: - raise ValueError("Unknown task", name) diff --git a/research/cvt_text/task_specific/word_level/__init__.py b/research/cvt_text/task_specific/word_level/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cvt_text/task_specific/word_level/depparse_module.py b/research/cvt_text/task_specific/word_level/depparse_module.py deleted file mode 100644 index b1207a98152..00000000000 --- a/research/cvt_text/task_specific/word_level/depparse_module.py +++ /dev/null @@ -1,126 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Dependency parsing module.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from corpus_processing import minibatching -from model import model_helpers -from model import task_module - - -class DepparseModule(task_module.SemiSupervisedModule): - def __init__(self, config, task_name, n_classes, inputs, encoder): - super(DepparseModule, self).__init__() - - self.task_name = task_name - self.n_classes = n_classes - self.labels = labels = tf.placeholder(tf.float32, [None, None, None], - name=task_name + '_labels') - - class PredictionModule(object): - def __init__(self, name, dep_reprs, head_reprs, roll_direction=0): - self.name = name - with tf.variable_scope(name + '/predictions'): - # apply hidden layers to the input representations - arc_dep_hidden = model_helpers.project( - dep_reprs, config.projection_size, 'arc_dep_hidden') - arc_head_hidden = model_helpers.project( - head_reprs, config.projection_size, 'arc_head_hidden') - arc_dep_hidden = tf.nn.relu(arc_dep_hidden) - arc_head_hidden = tf.nn.relu(arc_head_hidden) - arc_head_hidden = tf.nn.dropout(arc_head_hidden, inputs.keep_prob) - arc_dep_hidden = tf.nn.dropout(arc_dep_hidden, inputs.keep_prob) - - # bilinear classifier excluding the final dot product - arc_head = tf.layers.dense( - arc_head_hidden, config.depparse_projection_size, name='arc_head') - W = tf.get_variable('shared_W', - shape=[config.projection_size, n_classes, - config.depparse_projection_size]) - Wr = tf.get_variable('relation_specific_W', - shape=[config.projection_size, - config.depparse_projection_size]) - Wr_proj = tf.tile(tf.expand_dims(Wr, axis=-2), [1, n_classes, 1]) - W += Wr_proj - arc_dep = tf.tensordot(arc_dep_hidden, W, axes=[[-1], [0]]) - shape = tf.shape(arc_dep) - arc_dep = tf.reshape(arc_dep, - [shape[0], -1, config.depparse_projection_size]) - - # apply the transformer scaling trick to prevent dot products from - # getting too large (possibly not necessary) - scale = np.power( - config.depparse_projection_size, 0.25).astype('float32') - scale = tf.get_variable('scale', initializer=scale, dtype=tf.float32) - arc_dep /= scale - arc_head /= scale - - # compute the scores for each candidate arc - word_scores = tf.matmul(arc_head, arc_dep, transpose_b=True) - root_scores = tf.layers.dense(arc_head, n_classes, name='root_score') - arc_scores = tf.concat([root_scores, word_scores], axis=-1) - - # disallow the model from making impossible predictions - mask = inputs.mask - mask_shape = tf.shape(mask) - mask = tf.tile(tf.expand_dims(mask, -1), [1, 1, n_classes]) - mask = tf.reshape(mask, [-1, mask_shape[1] * n_classes]) - mask = tf.concat([tf.ones((mask_shape[0], 1)), - tf.zeros((mask_shape[0], n_classes - 1)), mask], - axis=1) - mask = tf.tile(tf.expand_dims(mask, 1), [1, mask_shape[1], 1]) - arc_scores += (mask - 1) * 100.0 - - self.logits = arc_scores - self.loss = model_helpers.masked_ce_loss( - self.logits, labels, inputs.mask, - roll_direction=roll_direction) - - primary = PredictionModule( - 'primary', - [encoder.uni_reprs, encoder.bi_reprs], - [encoder.uni_reprs, encoder.bi_reprs]) - ps = [ - PredictionModule( - 'full', - [encoder.uni_reprs, encoder.bi_reprs], - [encoder.uni_reprs, encoder.bi_reprs]), - PredictionModule('fw_fw', [encoder.uni_fw], [encoder.uni_fw]), - PredictionModule('fw_bw', [encoder.uni_fw], [encoder.uni_bw]), - PredictionModule('bw_fw', [encoder.uni_bw], [encoder.uni_fw]), - PredictionModule('bw_bw', [encoder.uni_bw], [encoder.uni_bw]), - ] - - self.unsupervised_loss = sum(p.loss for p in ps) - self.supervised_loss = primary.loss - self.probs = tf.nn.softmax(primary.logits) - self.preds = tf.argmax(primary.logits, axis=-1) - - def update_feed_dict(self, feed, mb): - if self.task_name in mb.teacher_predictions: - feed[self.labels] = mb.teacher_predictions[self.task_name] - elif mb.task_name != 'unlabeled': - labels = minibatching.build_array( - [[0] + e.labels + [0] for e in mb.examples]) - feed[self.labels] = np.eye( - (1 + mb.words.shape[1]) * self.n_classes)[labels] - diff --git a/research/cvt_text/task_specific/word_level/depparse_scorer.py b/research/cvt_text/task_specific/word_level/depparse_scorer.py deleted file mode 100644 index 142cf79f9b3..00000000000 --- a/research/cvt_text/task_specific/word_level/depparse_scorer.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Dependency parsing evaluation (computes UAS/LAS).""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from task_specific.word_level import word_level_scorer - - -class DepparseScorer(word_level_scorer.WordLevelScorer): - def __init__(self, n_relations, punctuation): - super(DepparseScorer, self).__init__() - self._n_relations = n_relations - self._punctuation = punctuation if punctuation else None - - def _get_results(self): - correct_unlabeled, correct_labeled, count = 0, 0, 0 - for example, preds in zip(self._examples, self._preds): - for w, y_true, y_pred in zip(example.words[1:-1], example.labels, preds): - if w in self._punctuation: - continue - count += 1 - correct_labeled += (1 if y_pred == y_true else 0) - correct_unlabeled += (1 if int(y_pred // self._n_relations) == - int(y_true // self._n_relations) else 0) - return [ - ("las", 100.0 * correct_labeled / count), - ("uas", 100.0 * correct_unlabeled / count), - ("loss", self.get_loss()), - ] diff --git a/research/cvt_text/task_specific/word_level/tagging_module.py b/research/cvt_text/task_specific/word_level/tagging_module.py deleted file mode 100644 index f1d85f333db..00000000000 --- a/research/cvt_text/task_specific/word_level/tagging_module.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Sequence tagging module.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from corpus_processing import minibatching -from model import model_helpers -from model import task_module - - -class TaggingModule(task_module.SemiSupervisedModule): - def __init__(self, config, task_name, n_classes, inputs, - encoder): - super(TaggingModule, self).__init__() - self.task_name = task_name - self.n_classes = n_classes - self.labels = labels = tf.placeholder(tf.float32, [None, None, None], - name=task_name + '_labels') - - class PredictionModule(object): - def __init__(self, name, input_reprs, roll_direction=0, activate=True): - self.name = name - with tf.variable_scope(name + '/predictions'): - projected = model_helpers.project(input_reprs, config.projection_size) - if activate: - projected = tf.nn.relu(projected) - self.logits = tf.layers.dense(projected, n_classes, name='predict') - - targets = labels - targets *= (1 - inputs.label_smoothing) - targets += inputs.label_smoothing / n_classes - self.loss = model_helpers.masked_ce_loss( - self.logits, targets, inputs.mask, roll_direction=roll_direction) - - primary = PredictionModule('primary', - ([encoder.uni_reprs, encoder.bi_reprs])) - ps = [ - PredictionModule('full', ([encoder.uni_reprs, encoder.bi_reprs]), - activate=False), - PredictionModule('forwards', [encoder.uni_fw]), - PredictionModule('backwards', [encoder.uni_bw]), - PredictionModule('future', [encoder.uni_fw], roll_direction=1), - PredictionModule('past', [encoder.uni_bw], roll_direction=-1), - ] - - self.unsupervised_loss = sum(p.loss for p in ps) - self.supervised_loss = primary.loss - self.probs = tf.nn.softmax(primary.logits) - self.preds = tf.argmax(primary.logits, axis=-1) - - def update_feed_dict(self, feed, mb): - if self.task_name in mb.teacher_predictions: - feed[self.labels] = mb.teacher_predictions[self.task_name] - elif mb.task_name != 'unlabeled': - labels = minibatching.build_array( - [[0] + e.labels + [0] for e in mb.examples]) - feed[self.labels] = np.eye(self.n_classes)[labels] diff --git a/research/cvt_text/task_specific/word_level/tagging_scorers.py b/research/cvt_text/task_specific/word_level/tagging_scorers.py deleted file mode 100644 index ee8a7c74f81..00000000000 --- a/research/cvt_text/task_specific/word_level/tagging_scorers.py +++ /dev/null @@ -1,83 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Sequence tagging evaluation.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc - -from task_specific.word_level import tagging_utils -from task_specific.word_level import word_level_scorer - - -class AccuracyScorer(word_level_scorer.WordLevelScorer): - def __init__(self, auto_fail_label=None): - super(AccuracyScorer, self).__init__() - self._auto_fail_label = auto_fail_label - - def _get_results(self): - correct, count = 0, 0 - for example, preds in zip(self._examples, self._preds): - for y_true, y_pred in zip(example.labels, preds): - count += 1 - correct += (1 if y_pred == y_true and y_true != self._auto_fail_label - else 0) - return [ - ("accuracy", 100.0 * correct / count), - ("loss", self.get_loss()) - ] - - -class F1Scorer(word_level_scorer.WordLevelScorer): - __metaclass__ = abc.ABCMeta - - def __init__(self): - super(F1Scorer, self).__init__() - self._n_correct, self._n_predicted, self._n_gold = 0, 0, 0 - - def _get_results(self): - if self._n_correct == 0: - p, r, f1 = 0, 0, 0 - else: - p = 100.0 * self._n_correct / self._n_predicted - r = 100.0 * self._n_correct / self._n_gold - f1 = 2 * p * r / (p + r) - return [ - ("precision", p), - ("recall", r), - ("f1", f1), - ("loss", self.get_loss()), - ] - - -class EntityLevelF1Scorer(F1Scorer): - def __init__(self, label_mapping): - super(EntityLevelF1Scorer, self).__init__() - self._inv_label_mapping = {v: k for k, v in label_mapping.iteritems()} - - def _get_results(self): - self._n_correct, self._n_predicted, self._n_gold = 0, 0, 0 - for example, preds in zip(self._examples, self._preds): - sent_spans = set(tagging_utils.get_span_labels( - example.labels, self._inv_label_mapping)) - span_preds = set(tagging_utils.get_span_labels( - preds, self._inv_label_mapping)) - self._n_correct += len(sent_spans & span_preds) - self._n_gold += len(sent_spans) - self._n_predicted += len(span_preds) - return super(EntityLevelF1Scorer, self)._get_results() diff --git a/research/cvt_text/task_specific/word_level/tagging_utils.py b/research/cvt_text/task_specific/word_level/tagging_utils.py deleted file mode 100644 index b300e492592..00000000000 --- a/research/cvt_text/task_specific/word_level/tagging_utils.py +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Utilities for sequence tagging tasks for entity-level tasks (e.g., NER).""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - -def get_span_labels(sentence_tags, inv_label_mapping=None): - """Go from token-level labels to list of entities (start, end, class).""" - - if inv_label_mapping: - sentence_tags = [inv_label_mapping[i] for i in sentence_tags] - span_labels = [] - last = 'O' - start = -1 - for i, tag in enumerate(sentence_tags): - pos, _ = (None, 'O') if tag == 'O' else tag.split('-') - if (pos == 'S' or pos == 'B' or tag == 'O') and last != 'O': - span_labels.append((start, i - 1, last.split('-')[-1])) - if pos == 'B' or pos == 'S' or last == 'O': - start = i - last = tag - if sentence_tags[-1] != 'O': - span_labels.append((start, len(sentence_tags) - 1, - sentence_tags[-1].split('-')[-1])) - return span_labels - - -def get_tags(span_labels, length, encoding): - """Converts a list of entities to token-label labels based on the provided - encoding (e.g., BIOES). - """ - - tags = ['O' for _ in range(length)] - for s, e, t in span_labels: - for i in range(s, e + 1): - tags[i] = 'I-' + t - if 'E' in encoding: - tags[e] = 'E-' + t - if 'B' in encoding: - tags[s] = 'B-' + t - if 'S' in encoding and s == e: - tags[s] = 'S-' + t - return tags diff --git a/research/cvt_text/task_specific/word_level/word_level_data.py b/research/cvt_text/task_specific/word_level/word_level_data.py deleted file mode 100644 index 40fa27b188c..00000000000 --- a/research/cvt_text/task_specific/word_level/word_level_data.py +++ /dev/null @@ -1,161 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Utilities for processing word-level datasets.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import os -import random -import tensorflow as tf - -from base import embeddings -from base import utils -from corpus_processing import example -from corpus_processing import minibatching -from task_specific.word_level import tagging_utils - - -class TaggedDataLoader(object): - def __init__(self, config, name, is_token_level): - self._config = config - self._task_name = name - self._raw_data_path = os.path.join(config.raw_data_topdir, name) - self._is_token_level = is_token_level - self.label_mapping_path = os.path.join( - config.preprocessed_data_topdir, - (name if is_token_level else - name + '_' + config.label_encoding) + '_label_mapping.pkl') - - if self.label_mapping: - self._n_classes = len(set(self.label_mapping.values())) - else: - self._n_classes = None - - def get_dataset(self, split): - if (split == 'train' and not self._config.for_preprocessing and - tf.gfile.Exists(os.path.join(self._raw_data_path, 'train_subset.txt'))): - split = 'train_subset' - return minibatching.Dataset( - self._config, self._get_examples(split), self._task_name) - - def get_labeled_sentences(self, split): - sentences = [] - path = os.path.join(self._raw_data_path, split + '.txt') - if not tf.gfile.Exists(path): - if self._config.for_preprocessing: - return [] - else: - raise ValueError('Unable to load data from', path) - - with tf.gfile.GFile(path, 'r') as f: - sentence = [] - for line in f: - line = line.strip().split() - if not line: - if sentence: - words, tags = zip(*sentence) - sentences.append((words, tags)) - sentence = [] - continue - if line[0] == '-DOCSTART-': - continue - word, tag = line[0], line[-1] - sentence.append((word, tag)) - return sentences - - @property - def label_mapping(self): - if not self._config.for_preprocessing: - return utils.load_cpickle(self.label_mapping_path) - - tag_counts = collections.Counter() - train_tags = set() - for split in ['train', 'dev', 'test']: - for words, tags in self.get_labeled_sentences(split): - if not self._is_token_level: - span_labels = tagging_utils.get_span_labels(tags) - tags = tagging_utils.get_tags( - span_labels, len(words), self._config.label_encoding) - for tag in tags: - if self._task_name == 'depparse': - tag = tag.split('-')[1] - tag_counts[tag] += 1 - if split == 'train': - train_tags.add(tag) - if self._task_name == 'ccg': - # for CCG, there are tags in the test sets that aren't in the train set - # all tags not in the train set get mapped to a special label - # the model will never predict this label because it never sees it in the - # training set - not_in_train_tags = [] - for tag, count in tag_counts.items(): - if tag not in train_tags: - not_in_train_tags.append(tag) - label_mapping = { - label: i for i, label in enumerate(sorted(filter( - lambda t: t not in not_in_train_tags, tag_counts.keys()))) - } - n = len(label_mapping) - for tag in not_in_train_tags: - label_mapping[tag] = n - else: - labels = sorted(tag_counts.keys()) - if self._task_name == 'depparse': - labels.remove('root') - labels.insert(0, 'root') - label_mapping = {label: i for i, label in enumerate(labels)} - return label_mapping - - def _get_examples(self, split): - word_vocab = embeddings.get_word_vocab(self._config) - char_vocab = embeddings.get_char_vocab() - examples = [ - TaggingExample( - self._config, self._is_token_level, words, tags, - word_vocab, char_vocab, self.label_mapping, self._task_name) - for words, tags in self.get_labeled_sentences(split)] - if self._config.train_set_percent < 100: - utils.log('using reduced train set ({:}%)'.format( - self._config.train_set_percent)) - random.shuffle(examples) - examples = examples[:int(len(examples) * - self._config.train_set_percent / 100.0)] - return examples - - -class TaggingExample(example.Example): - def __init__(self, config, is_token_level, words, original_tags, - word_vocab, char_vocab, label_mapping, task_name): - super(TaggingExample, self).__init__(words, word_vocab, char_vocab) - if is_token_level: - labels = original_tags - else: - span_labels = tagging_utils.get_span_labels(original_tags) - labels = tagging_utils.get_tags( - span_labels, len(words), config.label_encoding) - - if task_name == 'depparse': - self.labels = [] - for l in labels: - split = l.split('-') - self.labels.append( - len(label_mapping) * (0 if split[0] == '0' else 1 + int(split[0])) - + label_mapping[split[1]]) - else: - self.labels = [label_mapping[l] for l in labels] diff --git a/research/cvt_text/task_specific/word_level/word_level_scorer.py b/research/cvt_text/task_specific/word_level/word_level_scorer.py deleted file mode 100644 index e29848d9ca1..00000000000 --- a/research/cvt_text/task_specific/word_level/word_level_scorer.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base class for word-level scorers.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc - -from corpus_processing import scorer - - -class WordLevelScorer(scorer.Scorer): - __metaclass__ = abc.ABCMeta - - def __init__(self): - super(WordLevelScorer, self).__init__() - self._total_loss = 0 - self._total_words = 0 - self._examples = [] - self._preds = [] - - def update(self, examples, predictions, loss): - super(WordLevelScorer, self).update(examples, predictions, loss) - n_words = 0 - for example, preds in zip(examples, predictions): - self._examples.append(example) - self._preds.append(list(preds)[1:len(example.words) - 1]) - n_words += len(example.words) - 2 - self._total_loss += loss * n_words - self._total_words += n_words - - def get_loss(self): - return self._total_loss / max(1, self._total_words) diff --git a/research/cvt_text/training/__init__.py b/research/cvt_text/training/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/cvt_text/training/trainer.py b/research/cvt_text/training/trainer.py deleted file mode 100644 index 23dc4dad2c1..00000000000 --- a/research/cvt_text/training/trainer.py +++ /dev/null @@ -1,139 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Runs training for CVT text models.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import bisect -import time -import numpy as np -import tensorflow as tf - -from base import utils -from model import multitask_model -from task_specific import task_definitions - - -class Trainer(object): - def __init__(self, config): - self._config = config - self.tasks = [task_definitions.get_task(self._config, task_name) - for task_name in self._config.task_names] - - utils.log('Loading Pretrained Embeddings') - pretrained_embeddings = utils.load_cpickle(self._config.word_embeddings) - - utils.log('Building Model') - self._model = multitask_model.Model( - self._config, pretrained_embeddings, self.tasks) - utils.log() - - def train(self, sess, progress, summary_writer): - heading = lambda s: utils.heading(s, '(' + self._config.model_name + ')') - trained_on_sentences = 0 - start_time = time.time() - unsupervised_loss_total, unsupervised_loss_count = 0, 0 - supervised_loss_total, supervised_loss_count = 0, 0 - for mb in self._get_training_mbs(progress.unlabeled_data_reader): - if mb.task_name != 'unlabeled': - loss = self._model.train_labeled(sess, mb) - supervised_loss_total += loss - supervised_loss_count += 1 - - if mb.task_name == 'unlabeled': - self._model.run_teacher(sess, mb) - loss = self._model.train_unlabeled(sess, mb) - unsupervised_loss_total += loss - unsupervised_loss_count += 1 - mb.teacher_predictions.clear() - - trained_on_sentences += mb.size - global_step = self._model.get_global_step(sess) - - if global_step % self._config.print_every == 0: - utils.log('step {:} - ' - 'supervised loss: {:.2f} - ' - 'unsupervised loss: {:.2f} - ' - '{:.1f} sentences per second'.format( - global_step, - supervised_loss_total / max(1, supervised_loss_count), - unsupervised_loss_total / max(1, unsupervised_loss_count), - trained_on_sentences / (time.time() - start_time))) - unsupervised_loss_total, unsupervised_loss_count = 0, 0 - supervised_loss_total, supervised_loss_count = 0, 0 - - if global_step % self._config.eval_dev_every == 0: - heading('EVAL ON DEV') - self.evaluate_all_tasks(sess, summary_writer, progress.history) - progress.save_if_best_dev_model(sess, global_step) - utils.log() - - if global_step % self._config.eval_train_every == 0: - heading('EVAL ON TRAIN') - self.evaluate_all_tasks(sess, summary_writer, progress.history, True) - utils.log() - - if global_step % self._config.save_model_every == 0: - heading('CHECKPOINTING MODEL') - progress.write(sess, global_step) - utils.log() - - def evaluate_all_tasks(self, sess, summary_writer, history, train_set=False): - for task in self.tasks: - results = self._evaluate_task(sess, task, summary_writer, train_set) - if history is not None: - results.append(('step', self._model.get_global_step(sess))) - history.append(results) - if history is not None: - utils.write_cpickle(history, self._config.history_file) - - def _evaluate_task(self, sess, task, summary_writer, train_set): - scorer = task.get_scorer() - data = task.train_set if train_set else task.val_set - for i, mb in enumerate(data.get_minibatches(self._config.test_batch_size)): - loss, batch_preds = self._model.test(sess, mb) - scorer.update(mb.examples, batch_preds, loss) - - results = scorer.get_results(task.name + - ('_train_' if train_set else '_dev_')) - utils.log(task.name.upper() + ': ' + scorer.results_str()) - write_summary(summary_writer, results, - global_step=self._model.get_global_step(sess)) - return results - - def _get_training_mbs(self, unlabeled_data_reader): - datasets = [task.train_set for task in self.tasks] - weights = [np.sqrt(dataset.size) for dataset in datasets] - thresholds = np.cumsum([w / np.sum(weights) for w in weights]) - - labeled_mbs = [dataset.endless_minibatches(self._config.train_batch_size) - for dataset in datasets] - unlabeled_mbs = unlabeled_data_reader.endless_minibatches() - while True: - dataset_ind = bisect.bisect(thresholds, np.random.random()) - yield next(labeled_mbs[dataset_ind]) - if self._config.is_semisup: - yield next(unlabeled_mbs) - - -def write_summary(writer, results, global_step): - for k, v in results: - if 'f1' in k or 'acc' in k or 'loss' in k: - writer.add_summary(tf.Summary( - value=[tf.Summary.Value(tag=k, simple_value=v)]), global_step) - writer.flush() diff --git a/research/cvt_text/training/training_progress.py b/research/cvt_text/training/training_progress.py deleted file mode 100644 index a9ec96d5df8..00000000000 --- a/research/cvt_text/training/training_progress.py +++ /dev/null @@ -1,79 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -""" -Tracks and saves training progress (models and other data such as the current -location in the lm1b corpus) for later reloading. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from base import utils -from corpus_processing import unlabeled_data - - -class TrainingProgress(object): - def __init__(self, config, sess, checkpoint_saver, best_model_saver, - restore_if_possible=True): - self.config = config - self.checkpoint_saver = checkpoint_saver - self.best_model_saver = best_model_saver - - tf.gfile.MakeDirs(config.checkpoints_dir) - if restore_if_possible and tf.gfile.Exists(config.progress): - history, current_file, current_line = utils.load_cpickle( - config.progress, memoized=False) - self.history = history - self.unlabeled_data_reader = unlabeled_data.UnlabeledDataReader( - config, current_file, current_line) - utils.log("Continuing from global step", dict(self.history[-1])["step"], - "(lm1b file {:}, line {:})".format(current_file, current_line)) - self.checkpoint_saver.restore(sess, tf.train.latest_checkpoint( - self.config.checkpoints_dir)) - else: - utils.log("No previous checkpoint found - starting from scratch") - self.history = [] - self.unlabeled_data_reader = ( - unlabeled_data.UnlabeledDataReader(config)) - - def write(self, sess, global_step): - self.checkpoint_saver.save(sess, self.config.checkpoint, - global_step=global_step) - utils.write_cpickle( - (self.history, self.unlabeled_data_reader.current_file, - self.unlabeled_data_reader.current_line), - self.config.progress) - - def save_if_best_dev_model(self, sess, global_step): - best_avg_score = 0 - for i, results in enumerate(self.history): - if any("train" in metric for metric, value in results): - continue - total, count = 0, 0 - for metric, value in results: - if "f1" in metric or "las" in metric or "accuracy" in metric: - total += value - count += 1 - avg_score = total / count - if avg_score >= best_avg_score: - best_avg_score = avg_score - if i == len(self.history) - 1: - utils.log("New best model! Saving...") - self.best_model_saver.save(sess, self.config.best_model_checkpoint, - global_step=global_step) diff --git a/research/deep_speech/README.md b/research/deep_speech/README.md deleted file mode 100644 index 06ded0c9127..00000000000 --- a/research/deep_speech/README.md +++ /dev/null @@ -1,74 +0,0 @@ -![No Maintenance Intended](https://img.shields.io/badge/No%20Maintenance%20Intended-%E2%9C%95-red.svg) -[![TensorFlow 1.15.3](https://img.shields.io/badge/TensorFlow-1.15.3-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.3) -[![TensorFlow 2.3](https://img.shields.io/badge/TensorFlow-2.3-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.3.0) - -# DeepSpeech2 Model - -## Overview -This is an implementation of the [DeepSpeech2](https://arxiv.org/pdf/1512.02595.pdf) model. Current implementation is based on the code from the authors' [DeepSpeech code](https://github.com/PaddlePaddle/DeepSpeech) and the implementation in the [MLPerf Repo](https://github.com/mlperf/reference/tree/master/speech_recognition). - -DeepSpeech2 is an end-to-end deep neural network for automatic speech -recognition (ASR). It consists of 2 convolutional layers, 5 bidirectional RNN -layers and a fully connected layer. The feature in use is linear spectrogram -extracted from audio input. The network uses Connectionist Temporal Classification [CTC](https://www.cs.toronto.edu/~graves/icml_2006.pdf) as the loss function. - -## Dataset -The [OpenSLR LibriSpeech Corpus](http://www.openslr.org/12/) are used for model training and evaluation. - -The training data is a combination of train-clean-100 and train-clean-360 (~130k -examples in total). The validation set is dev-clean which has 2.7K lines. -The download script will preprocess the data into three columns: wav_filename, -wav_filesize, transcript. data/dataset.py will parse the csv file and build a -tf.data.Dataset object to feed data. Within each epoch (except for the -first if sortagrad is enabled), the training data will be shuffled batch-wise. - -## Running Code - -### Configure Python path -Add the top-level /models folder to the Python path with the command: -``` -export PYTHONPATH="$PYTHONPATH:/path/to/models" -``` - -### Install dependencies - -First install shared dependencies before running the code. Issue the following command: -``` -pip3 install -r requirements.txt -``` -or -``` -pip install -r requirements.txt -``` - -### Run each step individually - -#### Download and preprocess dataset -To download the dataset, issue the following command: -``` -python data/download.py -``` -Arguments: - * `--data_dir`: Directory where to download and save the preprocessed data. By default, it is `/tmp/librispeech_data`. - -Use the `--help` or `-h` flag to get a full list of possible arguments. - -#### Train and evaluate model -To train and evaluate the model, issue the following command: -``` -python deep_speech.py -``` -Arguments: - * `--model_dir`: Directory to save model training checkpoints. By default, it is `/tmp/deep_speech_model/`. - * `--train_data_dir`: Directory of the training dataset. - * `--eval_data_dir`: Directory of the evaluation dataset. - * `--num_gpus`: Number of GPUs to use (specify -1 if you want to use all available GPUs). - -There are other arguments about DeepSpeech2 model and training/evaluation process. Use the `--help` or `-h` flag to get a full list of possible arguments with detailed descriptions. - -### Run the benchmark -A shell script [run_deep_speech.sh](run_deep_speech.sh) is provided to run the whole pipeline with default parameters. Issue the following command to run the benchmark: -``` -sh run_deep_speech.sh -``` -Note by default, the training dataset in the benchmark include train-clean-100, train-clean-360 and train-other-500, and the evaluation dataset include dev-clean and dev-other. diff --git a/research/deep_speech/__init__.py b/research/deep_speech/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deep_speech/data/__init__.py b/research/deep_speech/data/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deep_speech/data/dataset.py b/research/deep_speech/data/dataset.py deleted file mode 100644 index 32391773dfe..00000000000 --- a/research/deep_speech/data/dataset.py +++ /dev/null @@ -1,275 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Generate tf.data.Dataset object for deep speech training/evaluation.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math -import random -# pylint: disable=g-bad-import-order -import numpy as np -from six.moves import xrange # pylint: disable=redefined-builtin -import soundfile -import tensorflow as tf -from absl import logging -# pylint: enable=g-bad-import-order - -import data.featurizer as featurizer # pylint: disable=g-bad-import-order - - -class AudioConfig(object): - """Configs for spectrogram extraction from audio.""" - - def __init__(self, - sample_rate, - window_ms, - stride_ms, - normalize=False): - """Initialize the AudioConfig class. - - Args: - sample_rate: an integer denoting the sample rate of the input waveform. - window_ms: an integer for the length of a spectrogram frame, in ms. - stride_ms: an integer for the frame stride, in ms. - normalize: a boolean for whether apply normalization on the audio feature. - """ - - self.sample_rate = sample_rate - self.window_ms = window_ms - self.stride_ms = stride_ms - self.normalize = normalize - - -class DatasetConfig(object): - """Config class for generating the DeepSpeechDataset.""" - - def __init__(self, audio_config, data_path, vocab_file_path, sortagrad): - """Initialize the configs for deep speech dataset. - - Args: - audio_config: AudioConfig object specifying the audio-related configs. - data_path: a string denoting the full path of a manifest file. - vocab_file_path: a string specifying the vocabulary file path. - sortagrad: a boolean, if set to true, audio sequences will be fed by - increasing length in the first training epoch, which will - expedite network convergence. - - Raises: - RuntimeError: file path not exist. - """ - - self.audio_config = audio_config - assert tf.io.gfile.exists(data_path) - assert tf.io.gfile.exists(vocab_file_path) - self.data_path = data_path - self.vocab_file_path = vocab_file_path - self.sortagrad = sortagrad - - -def _normalize_audio_feature(audio_feature): - """Perform mean and variance normalization on the spectrogram feature. - - Args: - audio_feature: a numpy array for the spectrogram feature. - - Returns: - a numpy array of the normalized spectrogram. - """ - mean = np.mean(audio_feature, axis=0) - var = np.var(audio_feature, axis=0) - normalized = (audio_feature - mean) / (np.sqrt(var) + 1e-6) - - return normalized - - -def _preprocess_audio(audio_file_path, audio_featurizer, normalize): - """Load the audio file and compute spectrogram feature.""" - data, _ = soundfile.read(audio_file_path) - feature = featurizer.compute_spectrogram_feature( - data, audio_featurizer.sample_rate, audio_featurizer.stride_ms, - audio_featurizer.window_ms) - # Feature normalization - if normalize: - feature = _normalize_audio_feature(feature) - - # Adding Channel dimension for conv2D input. - feature = np.expand_dims(feature, axis=2) - return feature - - -def _preprocess_data(file_path): - """Generate a list of tuples (wav_filename, wav_filesize, transcript). - - Each dataset file contains three columns: "wav_filename", "wav_filesize", - and "transcript". This function parses the csv file and stores each example - by the increasing order of audio length (indicated by wav_filesize). - AS the waveforms are ordered in increasing length, audio samples in a - mini-batch have similar length. - - Args: - file_path: a string specifying the csv file path for a dataset. - - Returns: - A list of tuples (wav_filename, wav_filesize, transcript) sorted by - file_size. - """ - logging.info("Loading data set {}".format(file_path)) - with tf.io.gfile.GFile(file_path, "r") as f: - lines = f.read().splitlines() - # Skip the csv header in lines[0]. - lines = lines[1:] - # The metadata file is tab separated. - lines = [line.split("\t", 2) for line in lines] - # Sort input data by the length of audio sequence. - lines.sort(key=lambda item: int(item[1])) - - return [tuple(line) for line in lines] - - -class DeepSpeechDataset(object): - """Dataset class for training/evaluation of DeepSpeech model.""" - - def __init__(self, dataset_config): - """Initialize the DeepSpeechDataset class. - - Args: - dataset_config: DatasetConfig object. - """ - self.config = dataset_config - # Instantiate audio feature extractor. - self.audio_featurizer = featurizer.AudioFeaturizer( - sample_rate=self.config.audio_config.sample_rate, - window_ms=self.config.audio_config.window_ms, - stride_ms=self.config.audio_config.stride_ms) - # Instantiate text feature extractor. - self.text_featurizer = featurizer.TextFeaturizer( - vocab_file=self.config.vocab_file_path) - - self.speech_labels = self.text_featurizer.speech_labels - self.entries = _preprocess_data(self.config.data_path) - # The generated spectrogram will have 161 feature bins. - self.num_feature_bins = 161 - - -def batch_wise_dataset_shuffle(entries, epoch_index, sortagrad, batch_size): - """Batch-wise shuffling of the data entries. - - Each data entry is in the format of (audio_file, file_size, transcript). - If epoch_index is 0 and sortagrad is true, we don't perform shuffling and - return entries in sorted file_size order. Otherwise, do batch_wise shuffling. - - Args: - entries: a list of data entries. - epoch_index: an integer of epoch index - sortagrad: a boolean to control whether sorting the audio in the first - training epoch. - batch_size: an integer for the batch size. - - Returns: - The shuffled data entries. - """ - shuffled_entries = [] - if epoch_index == 0 and sortagrad: - # No need to shuffle. - shuffled_entries = entries - else: - # Shuffle entries batch-wise. - max_buckets = int(math.floor(len(entries) / batch_size)) - total_buckets = [i for i in xrange(max_buckets)] - random.shuffle(total_buckets) - shuffled_entries = [] - for i in total_buckets: - shuffled_entries.extend(entries[i * batch_size : (i + 1) * batch_size]) - # If the last batch doesn't contain enough batch_size examples, - # just append it to the shuffled_entries. - shuffled_entries.extend(entries[max_buckets * batch_size:]) - - return shuffled_entries - - -def input_fn(batch_size, deep_speech_dataset, repeat=1): - """Input function for model training and evaluation. - - Args: - batch_size: an integer denoting the size of a batch. - deep_speech_dataset: DeepSpeechDataset object. - repeat: an integer for how many times to repeat the dataset. - - Returns: - a tf.data.Dataset object for model to consume. - """ - # Dataset properties - data_entries = deep_speech_dataset.entries - num_feature_bins = deep_speech_dataset.num_feature_bins - audio_featurizer = deep_speech_dataset.audio_featurizer - feature_normalize = deep_speech_dataset.config.audio_config.normalize - text_featurizer = deep_speech_dataset.text_featurizer - - def _gen_data(): - """Dataset generator function.""" - for audio_file, _, transcript in data_entries: - features = _preprocess_audio( - audio_file, audio_featurizer, feature_normalize) - labels = featurizer.compute_label_feature( - transcript, text_featurizer.token_to_index) - input_length = [features.shape[0]] - label_length = [len(labels)] - # Yield a tuple of (features, labels) where features is a dict containing - # all info about the actual data features. - yield ( - { - "features": features, - "input_length": input_length, - "label_length": label_length - }, - labels) - - dataset = tf.data.Dataset.from_generator( - _gen_data, - output_types=( - { - "features": tf.float32, - "input_length": tf.int32, - "label_length": tf.int32 - }, - tf.int32), - output_shapes=( - { - "features": tf.TensorShape([None, num_feature_bins, 1]), - "input_length": tf.TensorShape([1]), - "label_length": tf.TensorShape([1]) - }, - tf.TensorShape([None])) - ) - - # Repeat and batch the dataset - dataset = dataset.repeat(repeat) - - # Padding the features to its max length dimensions. - dataset = dataset.padded_batch( - batch_size=batch_size, - padded_shapes=( - { - "features": tf.TensorShape([None, num_feature_bins, 1]), - "input_length": tf.TensorShape([1]), - "label_length": tf.TensorShape([1]) - }, - tf.TensorShape([None])) - ) - - # Prefetch to improve speed of input pipeline. - dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) - return dataset diff --git a/research/deep_speech/data/download.py b/research/deep_speech/data/download.py deleted file mode 100644 index 3ea6e2f3e54..00000000000 --- a/research/deep_speech/data/download.py +++ /dev/null @@ -1,209 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Download and preprocess LibriSpeech dataset for DeepSpeech model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import codecs -import fnmatch -import os -import sys -import tarfile -import tempfile -import unicodedata - -from absl import app as absl_app -from absl import flags as absl_flags -import pandas -from six.moves import urllib -from sox import Transformer -import tensorflow as tf -from absl import logging - -LIBRI_SPEECH_URLS = { - "train-clean-100": - "http://www.openslr.org/resources/12/train-clean-100.tar.gz", - "train-clean-360": - "http://www.openslr.org/resources/12/train-clean-360.tar.gz", - "train-other-500": - "http://www.openslr.org/resources/12/train-other-500.tar.gz", - "dev-clean": - "http://www.openslr.org/resources/12/dev-clean.tar.gz", - "dev-other": - "http://www.openslr.org/resources/12/dev-other.tar.gz", - "test-clean": - "http://www.openslr.org/resources/12/test-clean.tar.gz", - "test-other": - "http://www.openslr.org/resources/12/test-other.tar.gz" -} - - -def download_and_extract(directory, url): - """Download and extract the given split of dataset. - - Args: - directory: the directory where to extract the tarball. - url: the url to download the data file. - """ - - if not tf.io.gfile.exists(directory): - tf.io.gfile.makedirs(directory) - - _, tar_filepath = tempfile.mkstemp(suffix=".tar.gz") - - try: - logging.info("Downloading %s to %s" % (url, tar_filepath)) - - def _progress(count, block_size, total_size): - sys.stdout.write("\r>> Downloading {} {:.1f}%".format( - tar_filepath, 100.0 * count * block_size / total_size)) - sys.stdout.flush() - - urllib.request.urlretrieve(url, tar_filepath, _progress) - print() - statinfo = os.stat(tar_filepath) - logging.info( - "Successfully downloaded %s, size(bytes): %d" % (url, statinfo.st_size)) - with tarfile.open(tar_filepath, "r") as tar: - tar.extractall(directory) - finally: - tf.io.gfile.remove(tar_filepath) - - -def convert_audio_and_split_transcript(input_dir, source_name, target_name, - output_dir, output_file): - """Convert FLAC to WAV and split the transcript. - - For audio file, convert the format from FLAC to WAV using the sox.Transformer - library. - For transcripts, each line contains the sequence id and the corresponding - transcript (separated by space): - Input data format: seq-id transcript_of_seq-id - For example: - 1-2-0 transcript_of_1-2-0.flac - 1-2-1 transcript_of_1-2-1.flac - ... - - Each sequence id has a corresponding .flac file. - Parse the transcript file and generate a new csv file which has three columns: - "wav_filename": the absolute path to a wav file. - "wav_filesize": the size of the corresponding wav file. - "transcript": the transcript for this audio segement. - - Args: - input_dir: the directory which holds the input dataset. - source_name: the name of the specified dataset. e.g. test-clean - target_name: the directory name for the newly generated audio files. - e.g. test-clean-wav - output_dir: the directory to place the newly generated csv files. - output_file: the name of the newly generated csv file. e.g. test-clean.csv - """ - - logging.info("Preprocessing audio and transcript for %s" % source_name) - source_dir = os.path.join(input_dir, source_name) - target_dir = os.path.join(input_dir, target_name) - - if not tf.io.gfile.exists(target_dir): - tf.io.gfile.makedirs(target_dir) - - files = [] - tfm = Transformer() - # Convert all FLAC file into WAV format. At the same time, generate the csv - # file. - for root, _, filenames in tf.io.gfile.walk(source_dir): - for filename in fnmatch.filter(filenames, "*.trans.txt"): - trans_file = os.path.join(root, filename) - with codecs.open(trans_file, "r", "utf-8") as fin: - for line in fin: - seqid, transcript = line.split(" ", 1) - # We do a encode-decode transformation here because the output type - # of encode is a bytes object, we need convert it to string. - transcript = unicodedata.normalize("NFKD", transcript).encode( - "ascii", "ignore").decode("ascii", "ignore").strip().lower() - - # Convert FLAC to WAV. - flac_file = os.path.join(root, seqid + ".flac") - wav_file = os.path.join(target_dir, seqid + ".wav") - if not tf.io.gfile.exists(wav_file): - tfm.build(flac_file, wav_file) - wav_filesize = os.path.getsize(wav_file) - - files.append((os.path.abspath(wav_file), wav_filesize, transcript)) - - # Write to CSV file which contains three columns: - # "wav_filename", "wav_filesize", "transcript". - csv_file_path = os.path.join(output_dir, output_file) - df = pandas.DataFrame( - data=files, columns=["wav_filename", "wav_filesize", "transcript"]) - df.to_csv(csv_file_path, index=False, sep="\t") - logging.info("Successfully generated csv file {}".format(csv_file_path)) - - -def download_and_process_datasets(directory, datasets): - """Download and pre-process the specified list of LibriSpeech dataset. - - Args: - directory: the directory to put all the downloaded and preprocessed data. - datasets: list of dataset names that will be downloaded and processed. - """ - - logging.info("Preparing LibriSpeech dataset: {}".format( - ",".join(datasets))) - for dataset in datasets: - logging.info("Preparing dataset %s", dataset) - dataset_dir = os.path.join(directory, dataset) - download_and_extract(dataset_dir, LIBRI_SPEECH_URLS[dataset]) - convert_audio_and_split_transcript( - dataset_dir + "/LibriSpeech", dataset, dataset + "-wav", - dataset_dir + "/LibriSpeech", dataset + ".csv") - - -def define_data_download_flags(): - """Define flags for data downloading.""" - absl_flags.DEFINE_string( - "data_dir", "/tmp/librispeech_data", - "Directory to download data and extract the tarball") - absl_flags.DEFINE_bool("train_only", False, - "If true, only download the training set") - absl_flags.DEFINE_bool("dev_only", False, - "If true, only download the dev set") - absl_flags.DEFINE_bool("test_only", False, - "If true, only download the test set") - - -def main(_): - if not tf.io.gfile.exists(FLAGS.data_dir): - tf.io.gfile.makedirs(FLAGS.data_dir) - - if FLAGS.train_only: - download_and_process_datasets( - FLAGS.data_dir, - ["train-clean-100", "train-clean-360", "train-other-500"]) - elif FLAGS.dev_only: - download_and_process_datasets(FLAGS.data_dir, ["dev-clean", "dev-other"]) - elif FLAGS.test_only: - download_and_process_datasets(FLAGS.data_dir, ["test-clean", "test-other"]) - else: - # By default we download the entire dataset. - download_and_process_datasets(FLAGS.data_dir, LIBRI_SPEECH_URLS.keys()) - - -if __name__ == "__main__": - logging.set_verbosity(logging.INFO) - define_data_download_flags() - FLAGS = absl_flags.FLAGS - absl_app.run(main) diff --git a/research/deep_speech/data/featurizer.py b/research/deep_speech/data/featurizer.py deleted file mode 100644 index 10b7069d313..00000000000 --- a/research/deep_speech/data/featurizer.py +++ /dev/null @@ -1,118 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utility class for extracting features from the text and audio input.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import codecs -import numpy as np - - -def compute_spectrogram_feature(samples, sample_rate, stride_ms=10.0, - window_ms=20.0, max_freq=None, eps=1e-14): - """Compute the spectrograms for the input samples(waveforms). - - More about spectrogram computation, please refer to: - https://en.wikipedia.org/wiki/Short-time_Fourier_transform. - """ - if max_freq is None: - max_freq = sample_rate / 2 - if max_freq > sample_rate / 2: - raise ValueError("max_freq must not be greater than half of sample rate.") - - if stride_ms > window_ms: - raise ValueError("Stride size must not be greater than window size.") - - stride_size = int(0.001 * sample_rate * stride_ms) - window_size = int(0.001 * sample_rate * window_ms) - - # Extract strided windows - truncate_size = (len(samples) - window_size) % stride_size - samples = samples[:len(samples) - truncate_size] - nshape = (window_size, (len(samples) - window_size) // stride_size + 1) - nstrides = (samples.strides[0], samples.strides[0] * stride_size) - windows = np.lib.stride_tricks.as_strided( - samples, shape=nshape, strides=nstrides) - assert np.all( - windows[:, 1] == samples[stride_size:(stride_size + window_size)]) - - # Window weighting, squared Fast Fourier Transform (fft), scaling - weighting = np.hanning(window_size)[:, None] - fft = np.fft.rfft(windows * weighting, axis=0) - fft = np.absolute(fft) - fft = fft**2 - scale = np.sum(weighting**2) * sample_rate - fft[1:-1, :] *= (2.0 / scale) - fft[(0, -1), :] /= scale - # Prepare fft frequency list - freqs = float(sample_rate) / window_size * np.arange(fft.shape[0]) - - # Compute spectrogram feature - ind = np.where(freqs <= max_freq)[0][-1] + 1 - specgram = np.log(fft[:ind, :] + eps) - return np.transpose(specgram, (1, 0)) - - -class AudioFeaturizer(object): - """Class to extract spectrogram features from the audio input.""" - - def __init__(self, - sample_rate=16000, - window_ms=20.0, - stride_ms=10.0): - """Initialize the audio featurizer class according to the configs. - - Args: - sample_rate: an integer specifying the sample rate of the input waveform. - window_ms: an integer for the length of a spectrogram frame, in ms. - stride_ms: an integer for the frame stride, in ms. - """ - self.sample_rate = sample_rate - self.window_ms = window_ms - self.stride_ms = stride_ms - - -def compute_label_feature(text, token_to_idx): - """Convert string to a list of integers.""" - tokens = list(text.strip().lower()) - feats = [token_to_idx[token] for token in tokens] - return feats - - -class TextFeaturizer(object): - """Extract text feature based on char-level granularity. - - By looking up the vocabulary table, each input string (one line of transcript) - will be converted to a sequence of integer indexes. - """ - - def __init__(self, vocab_file): - lines = [] - with codecs.open(vocab_file, "r", "utf-8") as fin: - lines.extend(fin.readlines()) - self.token_to_index = {} - self.index_to_token = {} - self.speech_labels = "" - index = 0 - for line in lines: - line = line[:-1] # Strip the '\n' char. - if line.startswith("#"): - # Skip from reading comment line. - continue - self.token_to_index[line] = index - self.index_to_token[index] = line - self.speech_labels += line - index += 1 diff --git a/research/deep_speech/data/vocabulary.txt b/research/deep_speech/data/vocabulary.txt deleted file mode 100644 index 51852b3a78b..00000000000 --- a/research/deep_speech/data/vocabulary.txt +++ /dev/null @@ -1,33 +0,0 @@ -# List of alphabets (utf-8 encoded). Note that '#' starts a comment line, which -# will be ignored by the parser. -# begin of vocabulary - -a -b -c -d -e -f -g -h -i -j -k -l -m -n -o -p -q -r -s -t -u -v -w -x -y -z -' -- -# end of vocabulary diff --git a/research/deep_speech/decoder.py b/research/deep_speech/decoder.py deleted file mode 100644 index bf618bcb63c..00000000000 --- a/research/deep_speech/decoder.py +++ /dev/null @@ -1,95 +0,0 @@ - -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Deep speech decoder.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import itertools - -from nltk.metrics import distance -import numpy as np - - -class DeepSpeechDecoder(object): - """Greedy decoder implementation for Deep Speech model.""" - - def __init__(self, labels, blank_index=28): - """Decoder initialization. - - Args: - labels: a string specifying the speech labels for the decoder to use. - blank_index: an integer specifying index for the blank character. - Defaults to 28. - """ - # e.g. labels = "[a-z]' _" - self.labels = labels - self.blank_index = blank_index - self.int_to_char = dict([(i, c) for (i, c) in enumerate(labels)]) - - def convert_to_string(self, sequence): - """Convert a sequence of indexes into corresponding string.""" - return ''.join([self.int_to_char[i] for i in sequence]) - - def wer(self, decode, target): - """Computes the Word Error Rate (WER). - - WER is defined as the edit distance between the two provided sentences after - tokenizing to words. - - Args: - decode: string of the decoded output. - target: a string for the ground truth label. - - Returns: - A float number for the WER of the current decode-target pair. - """ - # Map each word to a new char. - words = set(decode.split() + target.split()) - word2char = dict(zip(words, range(len(words)))) - - new_decode = [chr(word2char[w]) for w in decode.split()] - new_target = [chr(word2char[w]) for w in target.split()] - - return distance.edit_distance(''.join(new_decode), ''.join(new_target)) - - def cer(self, decode, target): - """Computes the Character Error Rate (CER). - - CER is defined as the edit distance between the two given strings. - - Args: - decode: a string of the decoded output. - target: a string for the ground truth label. - - Returns: - A float number denoting the CER for the current sentence pair. - """ - return distance.edit_distance(decode, target) - - def decode(self, logits): - """Decode the best guess from logits using greedy algorithm.""" - # Choose the class with maximimum probability. - best = list(np.argmax(logits, axis=1)) - # Merge repeated chars. - merge = [k for k, _ in itertools.groupby(best)] - # Remove the blank index in the decoded sequence. - merge_remove_blank = [] - for k in merge: - if k != self.blank_index: - merge_remove_blank.append(k) - - return self.convert_to_string(merge_remove_blank) diff --git a/research/deep_speech/deep_speech.py b/research/deep_speech/deep_speech.py deleted file mode 100644 index 468c7133115..00000000000 --- a/research/deep_speech/deep_speech.py +++ /dev/null @@ -1,417 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Main entry to train and evaluate DeepSpeech model.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -# pylint: disable=g-bad-import-order -from absl import app as absl_app -from absl import flags -from absl import logging -import tensorflow as tf -# pylint: enable=g-bad-import-order - -import data.dataset as dataset -import decoder -import deep_speech_model -from official.utils.flags import core as flags_core -from official.utils.misc import distribution_utils -from official.utils.misc import model_helpers - -# Default vocabulary file -_VOCABULARY_FILE = os.path.join( - os.path.dirname(__file__), "data/vocabulary.txt") -# Evaluation metrics -_WER_KEY = "WER" -_CER_KEY = "CER" - - -def compute_length_after_conv(max_time_steps, ctc_time_steps, input_length): - """Computes the time_steps/ctc_input_length after convolution. - - Suppose that the original feature contains two parts: - 1) Real spectrogram signals, spanning input_length steps. - 2) Padded part with all 0s. - The total length of those two parts is denoted as max_time_steps, which is - the padded length of the current batch. After convolution layers, the time - steps of a spectrogram feature will be decreased. As we know the percentage - of its original length within the entire length, we can compute the time steps - for the signal after conv as follows (using ctc_input_length to denote): - ctc_input_length = (input_length / max_time_steps) * output_length_of_conv. - This length is then fed into ctc loss function to compute loss. - - Args: - max_time_steps: max_time_steps for the batch, after padding. - ctc_time_steps: number of timesteps after convolution. - input_length: actual length of the original spectrogram, without padding. - - Returns: - the ctc_input_length after convolution layer. - """ - ctc_input_length = tf.cast(tf.multiply( - input_length, ctc_time_steps), dtype=tf.float32) - return tf.cast(tf.math.floordiv( - ctc_input_length, tf.cast(max_time_steps, dtype=tf.float32)), dtype=tf.int32) - - -def evaluate_model(estimator, speech_labels, entries, input_fn_eval): - """Evaluate the model performance using WER anc CER as metrics. - - WER: Word Error Rate - CER: Character Error Rate - - Args: - estimator: estimator to evaluate. - speech_labels: a string specifying all the character in the vocabulary. - entries: a list of data entries (audio_file, file_size, transcript) for the - given dataset. - input_fn_eval: data input function for evaluation. - - Returns: - Evaluation result containing 'wer' and 'cer' as two metrics. - """ - # Get predictions - predictions = estimator.predict(input_fn=input_fn_eval) - - # Get probabilities of each predicted class - probs = [pred["probabilities"] for pred in predictions] - - num_of_examples = len(probs) - targets = [entry[2] for entry in entries] # The ground truth transcript - - total_wer, total_cer = 0, 0 - greedy_decoder = decoder.DeepSpeechDecoder(speech_labels) - for i in range(num_of_examples): - # Decode string. - decoded_str = greedy_decoder.decode(probs[i]) - # Compute CER. - total_cer += greedy_decoder.cer(decoded_str, targets[i]) / float( - len(targets[i])) - # Compute WER. - total_wer += greedy_decoder.wer(decoded_str, targets[i]) / float( - len(targets[i].split())) - - # Get mean value - total_cer /= num_of_examples - total_wer /= num_of_examples - - global_step = estimator.get_variable_value(tf.compat.v1.GraphKeys.GLOBAL_STEP) - eval_results = { - _WER_KEY: total_wer, - _CER_KEY: total_cer, - tf.compat.v1.GraphKeys.GLOBAL_STEP: global_step, - } - - return eval_results - - -def model_fn(features, labels, mode, params): - """Define model function for deep speech model. - - Args: - features: a dictionary of input_data features. It includes the data - input_length, label_length and the spectrogram features. - labels: a list of labels for the input data. - mode: current estimator mode; should be one of - `tf.estimator.ModeKeys.TRAIN`, `EVALUATE`, `PREDICT`. - params: a dict of hyper parameters to be passed to model_fn. - - Returns: - EstimatorSpec parameterized according to the input params and the - current mode. - """ - num_classes = params["num_classes"] - input_length = features["input_length"] - label_length = features["label_length"] - features = features["features"] - - # Create DeepSpeech2 model. - model = deep_speech_model.DeepSpeech2( - flags_obj.rnn_hidden_layers, flags_obj.rnn_type, - flags_obj.is_bidirectional, flags_obj.rnn_hidden_size, - num_classes, flags_obj.use_bias) - - if mode == tf.estimator.ModeKeys.PREDICT: - logits = model(features, training=False) - predictions = { - "classes": tf.argmax(logits, axis=2), - "probabilities": logits, - "logits": logits - } - return tf.estimator.EstimatorSpec( - mode=mode, - predictions=predictions) - - # In training mode. - logits = model(features, training=True) - ctc_input_length = compute_length_after_conv( - tf.shape(features)[1], tf.shape(logits)[1], input_length) - # Compute CTC loss - loss = tf.reduce_mean(tf.keras.backend.ctc_batch_cost( - labels, logits, ctc_input_length, label_length)) - - optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=flags_obj.learning_rate) - global_step = tf.compat.v1.train.get_or_create_global_step() - minimize_op = optimizer.minimize(loss, global_step=global_step) - update_ops = tf.compat.v1.get_collection(tf.compat.v1.GraphKeys.UPDATE_OPS) - # Create the train_op that groups both minimize_ops and update_ops - train_op = tf.group(minimize_op, update_ops) - - return tf.estimator.EstimatorSpec( - mode=mode, - loss=loss, - train_op=train_op) - - -def generate_dataset(data_dir): - """Generate a speech dataset.""" - audio_conf = dataset.AudioConfig(sample_rate=flags_obj.sample_rate, - window_ms=flags_obj.window_ms, - stride_ms=flags_obj.stride_ms, - normalize=True) - train_data_conf = dataset.DatasetConfig( - audio_conf, - data_dir, - flags_obj.vocabulary_file, - flags_obj.sortagrad - ) - speech_dataset = dataset.DeepSpeechDataset(train_data_conf) - return speech_dataset - -def per_device_batch_size(batch_size, num_gpus): - """For multi-gpu, batch-size must be a multiple of the number of GPUs. - - - Note that distribution strategy handles this automatically when used with - Keras. For using with Estimator, we need to get per GPU batch. - - Args: - batch_size: Global batch size to be divided among devices. This should be - equal to num_gpus times the single-GPU batch_size for multi-gpu training. - num_gpus: How many GPUs are used with DistributionStrategies. - - Returns: - Batch size per device. - - Raises: - ValueError: if batch_size is not divisible by number of devices - """ - if num_gpus <= 1: - return batch_size - - remainder = batch_size % num_gpus - if remainder: - err = ('When running with multiple GPUs, batch size ' - 'must be a multiple of the number of available GPUs. Found {} ' - 'GPUs with a batch size of {}; try --batch_size={} instead.' - ).format(num_gpus, batch_size, batch_size - remainder) - raise ValueError(err) - return int(batch_size / num_gpus) - -def run_deep_speech(_): - """Run deep speech training and eval loop.""" - tf.compat.v1.set_random_seed(flags_obj.seed) - # Data preprocessing - logging.info("Data preprocessing...") - train_speech_dataset = generate_dataset(flags_obj.train_data_dir) - eval_speech_dataset = generate_dataset(flags_obj.eval_data_dir) - - # Number of label classes. Label string is "[a-z]' -" - num_classes = len(train_speech_dataset.speech_labels) - - # Use distribution strategy for multi-gpu training - num_gpus = flags_core.get_num_gpus(flags_obj) - distribution_strategy = distribution_utils.get_distribution_strategy(num_gpus=num_gpus) - run_config = tf.estimator.RunConfig( - train_distribute=distribution_strategy) - - estimator = tf.estimator.Estimator( - model_fn=model_fn, - model_dir=flags_obj.model_dir, - config=run_config, - params={ - "num_classes": num_classes, - } - ) - - # Benchmark logging - run_params = { - "batch_size": flags_obj.batch_size, - "train_epochs": flags_obj.train_epochs, - "rnn_hidden_size": flags_obj.rnn_hidden_size, - "rnn_hidden_layers": flags_obj.rnn_hidden_layers, - "rnn_type": flags_obj.rnn_type, - "is_bidirectional": flags_obj.is_bidirectional, - "use_bias": flags_obj.use_bias - } - - per_replica_batch_size = per_device_batch_size(flags_obj.batch_size, num_gpus) - - def input_fn_train(): - return dataset.input_fn( - per_replica_batch_size, train_speech_dataset) - - def input_fn_eval(): - return dataset.input_fn( - per_replica_batch_size, eval_speech_dataset) - - total_training_cycle = (flags_obj.train_epochs // - flags_obj.epochs_between_evals) - for cycle_index in range(total_training_cycle): - logging.info("Starting a training cycle: %d/%d", - cycle_index + 1, total_training_cycle) - - # Perform batch_wise dataset shuffling - train_speech_dataset.entries = dataset.batch_wise_dataset_shuffle( - train_speech_dataset.entries, cycle_index, flags_obj.sortagrad, - flags_obj.batch_size) - - estimator.train(input_fn=input_fn_train) - - # Evaluation - logging.info("Starting to evaluate...") - - eval_results = evaluate_model( - estimator, eval_speech_dataset.speech_labels, - eval_speech_dataset.entries, input_fn_eval) - - # Log the WER and CER results. - benchmark_logger.log_evaluation_result(eval_results) - logging.info( - "Iteration {}: WER = {:.2f}, CER = {:.2f}".format( - cycle_index + 1, eval_results[_WER_KEY], eval_results[_CER_KEY])) - - # If some evaluation threshold is met - if model_helpers.past_stop_threshold( - flags_obj.wer_threshold, eval_results[_WER_KEY]): - break - - -def define_deep_speech_flags(): - """Add flags for run_deep_speech.""" - # Add common flags - flags_core.define_base( - data_dir=False, # we use train_data_dir and eval_data_dir instead - export_dir=True, - train_epochs=True, - hooks=True, - num_gpu=True, - epochs_between_evals=True - ) - flags_core.define_performance( - num_parallel_calls=False, - inter_op=False, - intra_op=False, - synthetic_data=False, - max_train_steps=False, - dtype=False - ) - flags_core.define_benchmark() - flags.adopt_module_key_flags(flags_core) - - flags_core.set_defaults( - model_dir="/tmp/deep_speech_model/", - export_dir="/tmp/deep_speech_saved_model/", - train_epochs=10, - batch_size=128, - hooks="") - - # Deep speech flags - flags.DEFINE_integer( - name="seed", default=1, - help=flags_core.help_wrap("The random seed.")) - - flags.DEFINE_string( - name="train_data_dir", - default="/tmp/librispeech_data/test-clean/LibriSpeech/test-clean.csv", - help=flags_core.help_wrap("The csv file path of train dataset.")) - - flags.DEFINE_string( - name="eval_data_dir", - default="/tmp/librispeech_data/test-clean/LibriSpeech/test-clean.csv", - help=flags_core.help_wrap("The csv file path of evaluation dataset.")) - - flags.DEFINE_bool( - name="sortagrad", default=True, - help=flags_core.help_wrap( - "If true, sort examples by audio length and perform no " - "batch_wise shuffling for the first epoch.")) - - flags.DEFINE_integer( - name="sample_rate", default=16000, - help=flags_core.help_wrap("The sample rate for audio.")) - - flags.DEFINE_integer( - name="window_ms", default=20, - help=flags_core.help_wrap("The frame length for spectrogram.")) - - flags.DEFINE_integer( - name="stride_ms", default=10, - help=flags_core.help_wrap("The frame step.")) - - flags.DEFINE_string( - name="vocabulary_file", default=_VOCABULARY_FILE, - help=flags_core.help_wrap("The file path of vocabulary file.")) - - # RNN related flags - flags.DEFINE_integer( - name="rnn_hidden_size", default=800, - help=flags_core.help_wrap("The hidden size of RNNs.")) - - flags.DEFINE_integer( - name="rnn_hidden_layers", default=5, - help=flags_core.help_wrap("The number of RNN layers.")) - - flags.DEFINE_bool( - name="use_bias", default=True, - help=flags_core.help_wrap("Use bias in the last fully-connected layer")) - - flags.DEFINE_bool( - name="is_bidirectional", default=True, - help=flags_core.help_wrap("If rnn unit is bidirectional")) - - flags.DEFINE_enum( - name="rnn_type", default="gru", - enum_values=deep_speech_model.SUPPORTED_RNNS.keys(), - case_sensitive=False, - help=flags_core.help_wrap("Type of RNN cell.")) - - # Training related flags - flags.DEFINE_float( - name="learning_rate", default=5e-4, - help=flags_core.help_wrap("The initial learning rate.")) - - # Evaluation metrics threshold - flags.DEFINE_float( - name="wer_threshold", default=None, - help=flags_core.help_wrap( - "If passed, training will stop when the evaluation metric WER is " - "greater than or equal to wer_threshold. For libri speech dataset " - "the desired wer_threshold is 0.23 which is the result achieved by " - "MLPerf implementation.")) - - -def main(_): - run_deep_speech(flags_obj) - - -if __name__ == "__main__": - logging.set_verbosity(logging.INFO) - define_deep_speech_flags() - flags_obj = flags.FLAGS - absl_app.run(main) - diff --git a/research/deep_speech/deep_speech_model.py b/research/deep_speech/deep_speech_model.py deleted file mode 100644 index 7860f379f0d..00000000000 --- a/research/deep_speech/deep_speech_model.py +++ /dev/null @@ -1,177 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Network structure for DeepSpeech2 model.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import xrange # pylint: disable=redefined-builtin -import tensorflow as tf - -# Supported rnn cells. -SUPPORTED_RNNS = { - "lstm": tf.keras.layers.LSTMCell, - "rnn": tf.keras.layers.SimpleRNNCell, - "gru": tf.keras.layers.GRUCell, -} - -# Parameters for batch normalization. -_BATCH_NORM_EPSILON = 1e-5 -_BATCH_NORM_DECAY = 0.997 - -# Filters of convolution layer -_CONV_FILTERS = 32 - - -def batch_norm(inputs, training): - """Batch normalization layer. - - Note that the momentum to use will affect validation accuracy over time. - Batch norm has different behaviors during training/evaluation. With a large - momentum, the model takes longer to get a near-accurate estimation of the - moving mean/variance over the entire training dataset, which means we need - more iterations to see good evaluation results. If the training data is evenly - distributed over the feature space, we can also try setting a smaller momentum - (such as 0.1) to get good evaluation result sooner. - - Args: - inputs: input data for batch norm layer. - training: a boolean to indicate if it is in training stage. - - Returns: - tensor output from batch norm layer. - """ - return tf.keras.layers.BatchNormalization( - momentum=_BATCH_NORM_DECAY, epsilon=_BATCH_NORM_EPSILON)(inputs, training=training) - - -def _conv_bn_layer(inputs, padding, filters, kernel_size, strides, layer_id, - training): - """Defines 2D convolutional + batch normalization layer. - - Args: - inputs: input data for convolution layer. - padding: padding to be applied before convolution layer. - filters: an integer, number of output filters in the convolution. - kernel_size: a tuple specifying the height and width of the 2D convolution - window. - strides: a tuple specifying the stride length of the convolution. - layer_id: an integer specifying the layer index. - training: a boolean to indicate which stage we are in (training/eval). - - Returns: - tensor output from the current layer. - """ - # Perform symmetric padding on the feature dimension of time_step - # This step is required to avoid issues when RNN output sequence is shorter - # than the label length. - inputs = tf.pad( - inputs, - [[0, 0], [padding[0], padding[0]], [padding[1], padding[1]], [0, 0]]) - inputs = tf.keras.layers.Conv2D( - filters=filters, kernel_size=kernel_size, strides=strides, - padding="valid", use_bias=False, activation=tf.nn.relu6, - name="cnn_{}".format(layer_id))(inputs) - return batch_norm(inputs, training) - - -def _rnn_layer(inputs, rnn_cell, rnn_hidden_size, layer_id, is_batch_norm, - is_bidirectional, training): - """Defines a batch normalization + rnn layer. - - Args: - inputs: input tensors for the current layer. - rnn_cell: RNN cell instance to use. - rnn_hidden_size: an integer for the dimensionality of the rnn output space. - layer_id: an integer for the index of current layer. - is_batch_norm: a boolean specifying whether to perform batch normalization - on input states. - is_bidirectional: a boolean specifying whether the rnn layer is - bi-directional. - training: a boolean to indicate which stage we are in (training/eval). - - Returns: - tensor output for the current layer. - """ - if is_batch_norm: - inputs = batch_norm(inputs, training) - - if is_bidirectional: - rnn_outputs = tf.keras.layers.Bidirectional( - tf.keras.layers.RNN(rnn_cell(rnn_hidden_size), - return_sequences=True))(inputs) - else: - rnn_outputs = tf.keras.layers.RNN( - rnn_cell(rnn_hidden_size), return_sequences=True)(inputs) - - return rnn_outputs - -class DeepSpeech2(object): - """Define DeepSpeech2 model.""" - - def __init__(self, num_rnn_layers, rnn_type, is_bidirectional, - rnn_hidden_size, num_classes, use_bias): - """Initialize DeepSpeech2 model. - - Args: - num_rnn_layers: an integer, the number of rnn layers. By default, it's 5. - rnn_type: a string, one of the supported rnn cells: gru, rnn and lstm. - is_bidirectional: a boolean to indicate if the rnn layer is bidirectional. - rnn_hidden_size: an integer for the number of hidden states in each unit. - num_classes: an integer, the number of output classes/labels. - use_bias: a boolean specifying whether to use bias in the last fc layer. - """ - self.num_rnn_layers = num_rnn_layers - self.rnn_type = rnn_type - self.is_bidirectional = is_bidirectional - self.rnn_hidden_size = rnn_hidden_size - self.num_classes = num_classes - self.use_bias = use_bias - - def __call__(self, inputs, training): - # Two cnn layers. - inputs = _conv_bn_layer( - inputs, padding=(20, 5), filters=_CONV_FILTERS, kernel_size=(41, 11), - strides=(2, 2), layer_id=1, training=training) - - inputs = _conv_bn_layer( - inputs, padding=(10, 5), filters=_CONV_FILTERS, kernel_size=(21, 11), - strides=(2, 1), layer_id=2, training=training) - - # output of conv_layer2 with the shape of - # [batch_size (N), times (T), features (F), channels (C)]. - # Convert the conv output to rnn input. - batch_size = tf.shape(inputs)[0] - feat_size = inputs.get_shape().as_list()[2] - inputs = tf.reshape( - inputs, - [batch_size, -1, feat_size * _CONV_FILTERS]) - - # RNN layers. - rnn_cell = SUPPORTED_RNNS[self.rnn_type] - for layer_counter in xrange(self.num_rnn_layers): - # No batch normalization on the first layer. - is_batch_norm = (layer_counter != 0) - inputs = _rnn_layer( - inputs, rnn_cell, self.rnn_hidden_size, layer_counter + 1, - is_batch_norm, self.is_bidirectional, training) - - # FC layer with batch norm. - inputs = batch_norm(inputs, training) - logits = tf.keras.layers.Dense( - self.num_classes, use_bias=self.use_bias, activation="softmax")(inputs) - - return logits - diff --git a/research/deep_speech/requirements.txt b/research/deep_speech/requirements.txt deleted file mode 100644 index d951b6c4392..00000000000 --- a/research/deep_speech/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -nltk>=3.3 -pandas>=0.23.3 -soundfile>=0.10.2 -sox>=1.3.3 diff --git a/research/deep_speech/run_deep_speech.sh b/research/deep_speech/run_deep_speech.sh deleted file mode 100755 index f1559aa614e..00000000000 --- a/research/deep_speech/run_deep_speech.sh +++ /dev/null @@ -1,50 +0,0 @@ -#!/bin/bash -# Script to run deep speech model to achieve the MLPerf target (WER = 0.23) -# Step 1: download the LibriSpeech dataset. -echo "Data downloading..." -python data/download.py - -## After data downloading, the dataset directories are: -train_clean_100="/tmp/librispeech_data/train-clean-100/LibriSpeech/train-clean-100.csv" -train_clean_360="/tmp/librispeech_data/train-clean-360/LibriSpeech/train-clean-360.csv" -train_other_500="/tmp/librispeech_data/train-other-500/LibriSpeech/train-other-500.csv" -dev_clean="/tmp/librispeech_data/dev-clean/LibriSpeech/dev-clean.csv" -dev_other="/tmp/librispeech_data/dev-other/LibriSpeech/dev-other.csv" -test_clean="/tmp/librispeech_data/test-clean/LibriSpeech/test-clean.csv" -test_other="/tmp/librispeech_data/test-other/LibriSpeech/test-other.csv" - -# Step 2: generate train dataset and evaluation dataset -echo "Data preprocessing..." -train_file="/tmp/librispeech_data/train_dataset.csv" -eval_file="/tmp/librispeech_data/eval_dataset.csv" - -head -1 $train_clean_100 > $train_file -for filename in $train_clean_100 $train_clean_360 $train_other_500 -do - sed 1d $filename >> $train_file -done - -head -1 $dev_clean > $eval_file -for filename in $dev_clean $dev_other -do - sed 1d $filename >> $eval_file -done - -# Step 3: filter out the audio files that exceed max time duration. -final_train_file="/tmp/librispeech_data/final_train_dataset.csv" -final_eval_file="/tmp/librispeech_data/final_eval_dataset.csv" - -MAX_AUDIO_LEN=27.0 -awk -v maxlen="$MAX_AUDIO_LEN" 'BEGIN{FS="\t";} NR==1{print $0} NR>1{cmd="soxi -D "$1""; cmd|getline x; if(x<=maxlen) {print $0}; close(cmd);}' $train_file > $final_train_file -awk -v maxlen="$MAX_AUDIO_LEN" 'BEGIN{FS="\t";} NR==1{print $0} NR>1{cmd="soxi -D "$1""; cmd|getline x; if(x<=maxlen) {print $0}; close(cmd);}' $eval_file > $final_eval_file - -# Step 4: run the training and evaluation loop in background, and save the running info to a log file -echo "Model training and evaluation..." -start=`date +%s` - -log_file=log_`date +%Y-%m-%d` -nohup python deep_speech.py --train_data_dir=$final_train_file --eval_data_dir=$final_eval_file --num_gpus=-1 --wer_threshold=0.23 --seed=1 >$log_file 2>&1& - -end=`date +%s` -runtime=$((end-start)) -echo "Model training time is" $runtime "seconds." diff --git a/research/deeplab/README.md b/research/deeplab/README.md deleted file mode 100644 index c80d36c1c29..00000000000 --- a/research/deeplab/README.md +++ /dev/null @@ -1,325 +0,0 @@ -# DeepLab: Deep Labelling for Semantic Image Segmentation - -**To new and existing DeepLab users**: We have released a unified codebase for -dense pixel labeling tasks in TensorFlow2 at https://github.com/google-research/deeplab2. -Please consider switching to the newer codebase for better support. - -DeepLab is a state-of-art deep learning model for semantic image segmentation, -where the goal is to assign semantic labels (e.g., person, dog, cat and so on) -to every pixel in the input image. Current implementation includes the following -features: - -1. DeepLabv1 [1]: We use *atrous convolution* to explicitly control the - resolution at which feature responses are computed within Deep Convolutional - Neural Networks. - -2. DeepLabv2 [2]: We use *atrous spatial pyramid pooling* (ASPP) to robustly - segment objects at multiple scales with filters at multiple sampling rates - and effective fields-of-views. - -3. DeepLabv3 [3]: We augment the ASPP module with *image-level feature* [5, 6] - to capture longer range information. We also include *batch normalization* - [7] parameters to facilitate the training. In particular, we applying atrous - convolution to extract output features at different output strides during - training and evaluation, which efficiently enables training BN at output - stride = 16 and attains a high performance at output stride = 8 during - evaluation. - -4. DeepLabv3+ [4]: We extend DeepLabv3 to include a simple yet effective - decoder module to refine the segmentation results especially along object - boundaries. Furthermore, in this encoder-decoder structure one can - arbitrarily control the resolution of extracted encoder features by atrous - convolution to trade-off precision and runtime. - -If you find the code useful for your research, please consider citing our latest -works: - -* DeepLabv3+: - -``` -@inproceedings{deeplabv3plus2018, - title={Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation}, - author={Liang-Chieh Chen and Yukun Zhu and George Papandreou and Florian Schroff and Hartwig Adam}, - booktitle={ECCV}, - year={2018} -} -``` - -* MobileNetv2: - -``` -@inproceedings{mobilenetv22018, - title={MobileNetV2: Inverted Residuals and Linear Bottlenecks}, - author={Mark Sandler and Andrew Howard and Menglong Zhu and Andrey Zhmoginov and Liang-Chieh Chen}, - booktitle={CVPR}, - year={2018} -} -``` - -* MobileNetv3: - -``` -@inproceedings{mobilenetv32019, - title={Searching for MobileNetV3}, - author={Andrew Howard and Mark Sandler and Grace Chu and Liang-Chieh Chen and Bo Chen and Mingxing Tan and Weijun Wang and Yukun Zhu and Ruoming Pang and Vijay Vasudevan and Quoc V. Le and Hartwig Adam}, - booktitle={ICCV}, - year={2019} -} -``` - -* Architecture search for dense prediction cell: - -``` -@inproceedings{dpc2018, - title={Searching for Efficient Multi-Scale Architectures for Dense Image Prediction}, - author={Liang-Chieh Chen and Maxwell D. Collins and Yukun Zhu and George Papandreou and Barret Zoph and Florian Schroff and Hartwig Adam and Jonathon Shlens}, - booktitle={NIPS}, - year={2018} -} - -``` - -* Auto-DeepLab (also called hnasnet in core/nas_network.py): - -``` -@inproceedings{autodeeplab2019, - title={Auto-DeepLab: Hierarchical Neural Architecture Search for Semantic -Image Segmentation}, - author={Chenxi Liu and Liang-Chieh Chen and Florian Schroff and Hartwig Adam - and Wei Hua and Alan Yuille and Li Fei-Fei}, - booktitle={CVPR}, - year={2019} -} - -``` - - -In the current implementation, we support adopting the following network -backbones: - -1. MobileNetv2 [8] and MobileNetv3 [16]: A fast network structure designed - for mobile devices. - -2. Xception [9, 10]: A powerful network structure intended for server-side - deployment. - -3. ResNet-v1-{50,101} [14]: We provide both the original ResNet-v1 and its - 'beta' variant where the 'stem' is modified for semantic segmentation. - -4. PNASNet [15]: A Powerful network structure found by neural architecture - search. - -5. Auto-DeepLab (called HNASNet in the code): A segmentation-specific network - backbone found by neural architecture search. - -This directory contains our TensorFlow [11] implementation. We provide codes -allowing users to train the model, evaluate results in terms of mIOU (mean -intersection-over-union), and visualize segmentation results. We use PASCAL VOC -2012 [12] and Cityscapes [13] semantic segmentation benchmarks as an example in -the code. - -Some segmentation results on Flickr images: -

-
-
-
-

- -## Contacts (Maintainers) - -* Liang-Chieh Chen, github: [aquariusjay](https://github.com/aquariusjay) -* YuKun Zhu, github: [yknzhu](https://github.com/YknZhu) -* George Papandreou, github: [gpapan](https://github.com/gpapan) -* Hui Hui, github: [huihui-personal](https://github.com/huihui-personal) -* Maxwell D. Collins, github: [mcollinswisc](https://github.com/mcollinswisc) -* Ting Liu: github: [tingliu](https://github.com/tingliu) - -## Tables of Contents - -Demo: - -*
Colab notebook for off-the-shelf inference.
- -Running: - -* Installation.
-* Running DeepLab on PASCAL VOC 2012 semantic segmentation dataset.
-* Running DeepLab on Cityscapes semantic segmentation dataset.
-* Running DeepLab on ADE20K semantic segmentation dataset.
- -Models: - -* Checkpoints and frozen inference graphs.
- -Misc: - -* Please check FAQ if you have some questions before reporting the issues.
- -## Getting Help - -To get help with issues you may encounter while using the DeepLab Tensorflow -implementation, create a new question on -[StackOverflow](https://stackoverflow.com/) with the tag "tensorflow". - -Please report bugs (i.e., broken code, not usage questions) to the -tensorflow/models GitHub [issue -tracker](https://github.com/tensorflow/models/issues), prefixing the issue name -with "deeplab". - -## License - -All the codes in deeplab folder is covered by the [LICENSE](https://github.com/tensorflow/models/blob/master/LICENSE) -under tensorflow/models. Please refer to the LICENSE for details. - -## Change Logs - -### March 26, 2020 -* Supported EdgeTPU-DeepLab and EdgeTPU-DeepLab-slim on Cityscapes. -**Contributor**: Yun Long. - -### November 20, 2019 -* Supported MobileNetV3 large and small model variants on Cityscapes. -**Contributor**: Yukun Zhu. - - -### March 27, 2019 - -* Supported using different loss weights on different classes during training. -**Contributor**: Yuwei Yang. - - -### March 26, 2019 - -* Supported ResNet-v1-18. **Contributor**: Michalis Raptis. - - -### March 6, 2019 - -* Released the evaluation code (under the `evaluation` folder) for image -parsing, a.k.a. panoptic segmentation. In particular, the released code supports -evaluating the parsing results in terms of both the parsing covering and -panoptic quality metrics. **Contributors**: Maxwell Collins and Ting Liu. - - -### February 6, 2019 - -* Updated decoder module to exploit multiple low-level features with different -output_strides. - -### December 3, 2018 - -* Released the MobileNet-v2 checkpoint on ADE20K. - - -### November 19, 2018 - -* Supported NAS architecture for feature extraction. **Contributor**: Chenxi Liu. - -* Supported hard pixel mining during training. - - -### October 1, 2018 - -* Released MobileNet-v2 depth-multiplier = 0.5 COCO-pretrained checkpoints on -PASCAL VOC 2012, and Xception-65 COCO pretrained checkpoint (i.e., no PASCAL -pretrained). - - -### September 5, 2018 - -* Released Cityscapes pretrained checkpoints with found best dense prediction cell. - - -### May 26, 2018 - -* Updated ADE20K pretrained checkpoint. - - -### May 18, 2018 -* Added builders for ResNet-v1 and Xception model variants. -* Added ADE20K support, including colormap and pretrained Xception_65 checkpoint. -* Fixed a bug on using non-default depth_multiplier for MobileNet-v2. - - -### March 22, 2018 - -* Released checkpoints using MobileNet-V2 as network backbone and pretrained on -PASCAL VOC 2012 and Cityscapes. - - -### March 5, 2018 - -* First release of DeepLab in TensorFlow including deeper Xception network -backbone. Included checkpoints that have been pretrained on PASCAL VOC 2012 -and Cityscapes. - -## References - -1. **Semantic Image Segmentation with Deep Convolutional Nets and Fully Connected CRFs**
- Liang-Chieh Chen+, George Papandreou+, Iasonas Kokkinos, Kevin Murphy, Alan L. Yuille (+ equal - contribution).
- [[link]](https://arxiv.org/abs/1412.7062). In ICLR, 2015. - -2. **DeepLab: Semantic Image Segmentation with Deep Convolutional Nets,** - **Atrous Convolution, and Fully Connected CRFs**
- Liang-Chieh Chen+, George Papandreou+, Iasonas Kokkinos, Kevin Murphy, and Alan L Yuille (+ equal - contribution).
- [[link]](http://arxiv.org/abs/1606.00915). TPAMI 2017. - -3. **Rethinking Atrous Convolution for Semantic Image Segmentation**
- Liang-Chieh Chen, George Papandreou, Florian Schroff, Hartwig Adam.
- [[link]](http://arxiv.org/abs/1706.05587). arXiv: 1706.05587, 2017. - -4. **Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation**
- Liang-Chieh Chen, Yukun Zhu, George Papandreou, Florian Schroff, Hartwig Adam.
- [[link]](https://arxiv.org/abs/1802.02611). In ECCV, 2018. - -5. **ParseNet: Looking Wider to See Better**
- Wei Liu, Andrew Rabinovich, Alexander C Berg
- [[link]](https://arxiv.org/abs/1506.04579). arXiv:1506.04579, 2015. - -6. **Pyramid Scene Parsing Network**
- Hengshuang Zhao, Jianping Shi, Xiaojuan Qi, Xiaogang Wang, Jiaya Jia
- [[link]](https://arxiv.org/abs/1612.01105). In CVPR, 2017. - -7. **Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate shift**
- Sergey Ioffe, Christian Szegedy
- [[link]](https://arxiv.org/abs/1502.03167). In ICML, 2015. - -8. **MobileNetV2: Inverted Residuals and Linear Bottlenecks**
- Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen
- [[link]](https://arxiv.org/abs/1801.04381). In CVPR, 2018. - -9. **Xception: Deep Learning with Depthwise Separable Convolutions**
- François Chollet
- [[link]](https://arxiv.org/abs/1610.02357). In CVPR, 2017. - -10. **Deformable Convolutional Networks -- COCO Detection and Segmentation Challenge 2017 Entry**
- Haozhi Qi, Zheng Zhang, Bin Xiao, Han Hu, Bowen Cheng, Yichen Wei, Jifeng Dai
- [[link]](http://presentations.cocodataset.org/COCO17-Detect-MSRA.pdf). ICCV COCO Challenge - Workshop, 2017. - -11. **Tensorflow: Large-Scale Machine Learning on Heterogeneous Distributed Systems**
- M. Abadi, A. Agarwal, et al.
- [[link]](https://arxiv.org/abs/1603.04467). arXiv:1603.04467, 2016. - -12. **The Pascal Visual Object Classes Challenge – A Retrospective,**
- Mark Everingham, S. M. Ali Eslami, Luc Van Gool, Christopher K. I. Williams, John - Winn, and Andrew Zisserma.
- [[link]](http://host.robots.ox.ac.uk/pascal/VOC/voc2012/). IJCV, 2014. - -13. **The Cityscapes Dataset for Semantic Urban Scene Understanding**
- Cordts, Marius, Mohamed Omran, Sebastian Ramos, Timo Rehfeld, Markus Enzweiler, Rodrigo Benenson, Uwe Franke, Stefan Roth, Bernt Schiele.
- [[link]](https://www.cityscapes-dataset.com/). In CVPR, 2016. - -14. **Deep Residual Learning for Image Recognition**
- Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun.
- [[link]](https://arxiv.org/abs/1512.03385). In CVPR, 2016. - -15. **Progressive Neural Architecture Search**
- Chenxi Liu, Barret Zoph, Maxim Neumann, Jonathon Shlens, Wei Hua, Li-Jia Li, Li Fei-Fei, Alan Yuille, Jonathan Huang, Kevin Murphy.
- [[link]](https://arxiv.org/abs/1712.00559). In ECCV, 2018. - -16. **Searching for MobileNetV3**
- Andrew Howard, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Chen, Mingxing Tan, Weijun Wang, Yukun Zhu, Ruoming Pang, Vijay Vasudevan, Quoc V. Le, Hartwig Adam.
- [[link]](https://arxiv.org/abs/1905.02244). In ICCV, 2019. diff --git a/research/deeplab/__init__.py b/research/deeplab/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deeplab/common.py b/research/deeplab/common.py deleted file mode 100644 index 928f7176c37..00000000000 --- a/research/deeplab/common.py +++ /dev/null @@ -1,295 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Provides flags that are common to scripts. - -Common flags from train/eval/vis/export_model.py are collected in this script. -""" -import collections -import copy -import json -import tensorflow as tf - -flags = tf.app.flags - -# Flags for input preprocessing. - -flags.DEFINE_integer('min_resize_value', None, - 'Desired size of the smaller image side.') - -flags.DEFINE_integer('max_resize_value', None, - 'Maximum allowed size of the larger image side.') - -flags.DEFINE_integer('resize_factor', None, - 'Resized dimensions are multiple of factor plus one.') - -flags.DEFINE_boolean('keep_aspect_ratio', True, - 'Keep aspect ratio after resizing or not.') - -# Model dependent flags. - -flags.DEFINE_integer('logits_kernel_size', 1, - 'The kernel size for the convolutional kernel that ' - 'generates logits.') - -# When using 'mobilent_v2', we set atrous_rates = decoder_output_stride = None. -# When using 'xception_65' or 'resnet_v1' model variants, we set -# atrous_rates = [6, 12, 18] (output stride 16) and decoder_output_stride = 4. -# See core/feature_extractor.py for supported model variants. -flags.DEFINE_string('model_variant', 'mobilenet_v2', 'DeepLab model variant.') - -flags.DEFINE_multi_float('image_pyramid', None, - 'Input scales for multi-scale feature extraction.') - -flags.DEFINE_boolean('add_image_level_feature', True, - 'Add image level feature.') - -flags.DEFINE_list( - 'image_pooling_crop_size', None, - 'Image pooling crop size [height, width] used in the ASPP module. When ' - 'value is None, the model performs image pooling with "crop_size". This' - 'flag is useful when one likes to use different image pooling sizes.') - -flags.DEFINE_list( - 'image_pooling_stride', '1,1', - 'Image pooling stride [height, width] used in the ASPP image pooling. ') - -flags.DEFINE_boolean('aspp_with_batch_norm', True, - 'Use batch norm parameters for ASPP or not.') - -flags.DEFINE_boolean('aspp_with_separable_conv', True, - 'Use separable convolution for ASPP or not.') - -# Defaults to None. Set multi_grid = [1, 2, 4] when using provided -# 'resnet_v1_{50,101}_beta' checkpoints. -flags.DEFINE_multi_integer('multi_grid', None, - 'Employ a hierarchy of atrous rates for ResNet.') - -flags.DEFINE_float('depth_multiplier', 1.0, - 'Multiplier for the depth (number of channels) for all ' - 'convolution ops used in MobileNet.') - -flags.DEFINE_integer('divisible_by', None, - 'An integer that ensures the layer # channels are ' - 'divisible by this value. Used in MobileNet.') - -# For `xception_65`, use decoder_output_stride = 4. For `mobilenet_v2`, use -# decoder_output_stride = None. -flags.DEFINE_list('decoder_output_stride', None, - 'Comma-separated list of strings with the number specifying ' - 'output stride of low-level features at each network level.' - 'Current semantic segmentation implementation assumes at ' - 'most one output stride (i.e., either None or a list with ' - 'only one element.') - -flags.DEFINE_boolean('decoder_use_separable_conv', True, - 'Employ separable convolution for decoder or not.') - -flags.DEFINE_enum('merge_method', 'max', ['max', 'avg'], - 'Scheme to merge multi scale features.') - -flags.DEFINE_boolean( - 'prediction_with_upsampled_logits', True, - 'When performing prediction, there are two options: (1) bilinear ' - 'upsampling the logits followed by softmax, or (2) softmax followed by ' - 'bilinear upsampling.') - -flags.DEFINE_string( - 'dense_prediction_cell_json', - '', - 'A JSON file that specifies the dense prediction cell.') - -flags.DEFINE_integer( - 'nas_stem_output_num_conv_filters', 20, - 'Number of filters of the stem output tensor in NAS models.') - -flags.DEFINE_bool('nas_use_classification_head', False, - 'Use image classification head for NAS model variants.') - -flags.DEFINE_bool('nas_remove_os32_stride', False, - 'Remove the stride in the output stride 32 branch.') - -flags.DEFINE_bool('use_bounded_activation', False, - 'Whether or not to use bounded activations. Bounded ' - 'activations better lend themselves to quantized inference.') - -flags.DEFINE_boolean('aspp_with_concat_projection', True, - 'ASPP with concat projection.') - -flags.DEFINE_boolean('aspp_with_squeeze_and_excitation', False, - 'ASPP with squeeze and excitation.') - -flags.DEFINE_integer('aspp_convs_filters', 256, 'ASPP convolution filters.') - -flags.DEFINE_boolean('decoder_use_sum_merge', False, - 'Decoder uses simply sum merge.') - -flags.DEFINE_integer('decoder_filters', 256, 'Decoder filters.') - -flags.DEFINE_boolean('decoder_output_is_logits', False, - 'Use decoder output as logits or not.') - -flags.DEFINE_boolean('image_se_uses_qsigmoid', False, 'Use q-sigmoid.') - -flags.DEFINE_multi_float( - 'label_weights', None, - 'A list of label weights, each element represents the weight for the label ' - 'of its index, for example, label_weights = [0.1, 0.5] means the weight ' - 'for label 0 is 0.1 and the weight for label 1 is 0.5. If set as None, all ' - 'the labels have the same weight 1.0.') - -flags.DEFINE_float('batch_norm_decay', 0.9997, 'Batchnorm decay.') - -FLAGS = flags.FLAGS - -# Constants - -# Perform semantic segmentation predictions. -OUTPUT_TYPE = 'semantic' - -# Semantic segmentation item names. -LABELS_CLASS = 'labels_class' -IMAGE = 'image' -HEIGHT = 'height' -WIDTH = 'width' -IMAGE_NAME = 'image_name' -LABEL = 'label' -ORIGINAL_IMAGE = 'original_image' - -# Test set name. -TEST_SET = 'test' - - -class ModelOptions( - collections.namedtuple('ModelOptions', [ - 'outputs_to_num_classes', - 'crop_size', - 'atrous_rates', - 'output_stride', - 'preprocessed_images_dtype', - 'merge_method', - 'add_image_level_feature', - 'image_pooling_crop_size', - 'image_pooling_stride', - 'aspp_with_batch_norm', - 'aspp_with_separable_conv', - 'multi_grid', - 'decoder_output_stride', - 'decoder_use_separable_conv', - 'logits_kernel_size', - 'model_variant', - 'depth_multiplier', - 'divisible_by', - 'prediction_with_upsampled_logits', - 'dense_prediction_cell_config', - 'nas_architecture_options', - 'use_bounded_activation', - 'aspp_with_concat_projection', - 'aspp_with_squeeze_and_excitation', - 'aspp_convs_filters', - 'decoder_use_sum_merge', - 'decoder_filters', - 'decoder_output_is_logits', - 'image_se_uses_qsigmoid', - 'label_weights', - 'sync_batch_norm_method', - 'batch_norm_decay', - ])): - """Immutable class to hold model options.""" - - __slots__ = () - - def __new__(cls, - outputs_to_num_classes, - crop_size=None, - atrous_rates=None, - output_stride=8, - preprocessed_images_dtype=tf.float32): - """Constructor to set default values. - - Args: - outputs_to_num_classes: A dictionary from output type to the number of - classes. For example, for the task of semantic segmentation with 21 - semantic classes, we would have outputs_to_num_classes['semantic'] = 21. - crop_size: A tuple [crop_height, crop_width]. - atrous_rates: A list of atrous convolution rates for ASPP. - output_stride: The ratio of input to output spatial resolution. - preprocessed_images_dtype: The type after the preprocessing function. - - Returns: - A new ModelOptions instance. - """ - dense_prediction_cell_config = None - if FLAGS.dense_prediction_cell_json: - with tf.gfile.Open(FLAGS.dense_prediction_cell_json, 'r') as f: - dense_prediction_cell_config = json.load(f) - decoder_output_stride = None - if FLAGS.decoder_output_stride: - decoder_output_stride = [ - int(x) for x in FLAGS.decoder_output_stride] - if sorted(decoder_output_stride, reverse=True) != decoder_output_stride: - raise ValueError('Decoder output stride need to be sorted in the ' - 'descending order.') - image_pooling_crop_size = None - if FLAGS.image_pooling_crop_size: - image_pooling_crop_size = [int(x) for x in FLAGS.image_pooling_crop_size] - image_pooling_stride = [1, 1] - if FLAGS.image_pooling_stride: - image_pooling_stride = [int(x) for x in FLAGS.image_pooling_stride] - label_weights = FLAGS.label_weights - if label_weights is None: - label_weights = 1.0 - nas_architecture_options = { - 'nas_stem_output_num_conv_filters': ( - FLAGS.nas_stem_output_num_conv_filters), - 'nas_use_classification_head': FLAGS.nas_use_classification_head, - 'nas_remove_os32_stride': FLAGS.nas_remove_os32_stride, - } - return super(ModelOptions, cls).__new__( - cls, outputs_to_num_classes, crop_size, atrous_rates, output_stride, - preprocessed_images_dtype, - FLAGS.merge_method, - FLAGS.add_image_level_feature, - image_pooling_crop_size, - image_pooling_stride, - FLAGS.aspp_with_batch_norm, - FLAGS.aspp_with_separable_conv, - FLAGS.multi_grid, - decoder_output_stride, - FLAGS.decoder_use_separable_conv, - FLAGS.logits_kernel_size, - FLAGS.model_variant, - FLAGS.depth_multiplier, - FLAGS.divisible_by, - FLAGS.prediction_with_upsampled_logits, - dense_prediction_cell_config, - nas_architecture_options, - FLAGS.use_bounded_activation, - FLAGS.aspp_with_concat_projection, - FLAGS.aspp_with_squeeze_and_excitation, - FLAGS.aspp_convs_filters, - FLAGS.decoder_use_sum_merge, - FLAGS.decoder_filters, - FLAGS.decoder_output_is_logits, - FLAGS.image_se_uses_qsigmoid, - label_weights, - 'None', - FLAGS.batch_norm_decay) - - def __deepcopy__(self, memo): - return ModelOptions(copy.deepcopy(self.outputs_to_num_classes), - self.crop_size, - self.atrous_rates, - self.output_stride, - self.preprocessed_images_dtype) diff --git a/research/deeplab/common_test.py b/research/deeplab/common_test.py deleted file mode 100644 index 45b64e50e3b..00000000000 --- a/research/deeplab/common_test.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for common.py.""" -import copy - -import tensorflow as tf - -from deeplab import common - - -class CommonTest(tf.test.TestCase): - - def testOutputsToNumClasses(self): - num_classes = 21 - model_options = common.ModelOptions( - outputs_to_num_classes={common.OUTPUT_TYPE: num_classes}) - self.assertEqual(model_options.outputs_to_num_classes[common.OUTPUT_TYPE], - num_classes) - - def testDeepcopy(self): - num_classes = 21 - model_options = common.ModelOptions( - outputs_to_num_classes={common.OUTPUT_TYPE: num_classes}) - model_options_new = copy.deepcopy(model_options) - self.assertEqual((model_options_new. - outputs_to_num_classes[common.OUTPUT_TYPE]), - num_classes) - - num_classes_new = 22 - model_options_new.outputs_to_num_classes[common.OUTPUT_TYPE] = ( - num_classes_new) - self.assertEqual(model_options.outputs_to_num_classes[common.OUTPUT_TYPE], - num_classes) - self.assertEqual((model_options_new. - outputs_to_num_classes[common.OUTPUT_TYPE]), - num_classes_new) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/convert_to_tflite.py b/research/deeplab/convert_to_tflite.py deleted file mode 100644 index d23ce9e2337..00000000000 --- a/research/deeplab/convert_to_tflite.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tools to convert a quantized deeplab model to tflite.""" - -from absl import app -from absl import flags -import numpy as np -from PIL import Image -import tensorflow as tf - - -flags.DEFINE_string('quantized_graph_def_path', None, - 'Path to quantized graphdef.') -flags.DEFINE_string('output_tflite_path', None, 'Output TFlite model path.') -flags.DEFINE_string( - 'input_tensor_name', None, - 'Input tensor to TFlite model. This usually should be the input tensor to ' - 'model backbone.' -) -flags.DEFINE_string( - 'output_tensor_name', 'ArgMax:0', - 'Output tensor name of TFlite model. By default we output the raw semantic ' - 'label predictions.' -) -flags.DEFINE_string( - 'test_image_path', None, - 'Path to an image to test the consistency between input graphdef / ' - 'converted tflite model.' -) - -FLAGS = flags.FLAGS - - -def convert_to_tflite(quantized_graphdef, - backbone_input_tensor, - output_tensor): - """Helper method to convert quantized deeplab model to TFlite.""" - with tf.Graph().as_default() as graph: - tf.graph_util.import_graph_def(quantized_graphdef, name='') - sess = tf.compat.v1.Session() - - tflite_input = graph.get_tensor_by_name(backbone_input_tensor) - tflite_output = graph.get_tensor_by_name(output_tensor) - converter = tf.compat.v1.lite.TFLiteConverter.from_session( - sess, [tflite_input], [tflite_output]) - converter.inference_type = tf.compat.v1.lite.constants.QUANTIZED_UINT8 - input_arrays = converter.get_input_arrays() - converter.quantized_input_stats = {input_arrays[0]: (127.5, 127.5)} - return converter.convert() - - -def check_tflite_consistency(graph_def, tflite_model, image_path): - """Runs tflite and frozen graph on same input, check their outputs match.""" - # Load tflite model and check input size. - interpreter = tf.lite.Interpreter(model_content=tflite_model) - interpreter.allocate_tensors() - input_details = interpreter.get_input_details() - output_details = interpreter.get_output_details() - height, width = input_details[0]['shape'][1:3] - - # Prepare input image data. - with tf.io.gfile.GFile(image_path, 'rb') as f: - image = Image.open(f) - image = np.asarray(image.convert('RGB').resize((width, height))) - image = np.expand_dims(image, 0) - - # Output from tflite model. - interpreter.set_tensor(input_details[0]['index'], image) - interpreter.invoke() - output_tflite = interpreter.get_tensor(output_details[0]['index']) - - with tf.Graph().as_default(): - tf.graph_util.import_graph_def(graph_def, name='') - with tf.compat.v1.Session() as sess: - # Note here the graph will include preprocessing part of the graph - # (e.g. resize, pad, normalize). Given the input image size is at the - # crop size (backbone input size), resize / pad should be an identity op. - output_graph = sess.run( - FLAGS.output_tensor_name, feed_dict={'ImageTensor:0': image}) - - print('%.2f%% pixels have matched semantic labels.' % ( - 100 * np.mean(output_graph == output_tflite))) - - -def main(unused_argv): - with tf.io.gfile.GFile(FLAGS.quantized_graph_def_path, 'rb') as f: - graph_def = tf.compat.v1.GraphDef.FromString(f.read()) - tflite_model = convert_to_tflite( - graph_def, FLAGS.input_tensor_name, FLAGS.output_tensor_name) - - if FLAGS.output_tflite_path: - with tf.io.gfile.GFile(FLAGS.output_tflite_path, 'wb') as f: - f.write(tflite_model) - - if FLAGS.test_image_path: - check_tflite_consistency(graph_def, tflite_model, FLAGS.test_image_path) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/deeplab/core/__init__.py b/research/deeplab/core/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deeplab/core/conv2d_ws.py b/research/deeplab/core/conv2d_ws.py deleted file mode 100644 index 9aaaf33dd3c..00000000000 --- a/research/deeplab/core/conv2d_ws.py +++ /dev/null @@ -1,369 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Augment slim.conv2d with optional Weight Standardization (WS). - -WS is a normalization method to accelerate micro-batch training. When used with -Group Normalization and trained with 1 image/GPU, WS is able to match or -outperform the performances of BN trained with large batch sizes. -[1] Siyuan Qiao, Huiyu Wang, Chenxi Liu, Wei Shen, Alan Yuille - Weight Standardization. arXiv:1903.10520 -[2] Lei Huang, Xianglong Liu, Yang Liu, Bo Lang, Dacheng Tao - Centered Weight Normalization in Accelerating Training of Deep Neural - Networks. ICCV 2017 -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf -from tensorflow.contrib import framework as contrib_framework -from tensorflow.contrib import layers as contrib_layers - -from tensorflow.contrib.layers.python.layers import layers -from tensorflow.contrib.layers.python.layers import utils - - -class Conv2D(tf.keras.layers.Conv2D, tf.layers.Layer): - """2D convolution layer (e.g. spatial convolution over images). - - This layer creates a convolution kernel that is convolved - (actually cross-correlated) with the layer input to produce a tensor of - outputs. If `use_bias` is True (and a `bias_initializer` is provided), - a bias vector is created and added to the outputs. Finally, if - `activation` is not `None`, it is applied to the outputs as well. - """ - - def __init__(self, - filters, - kernel_size, - strides=(1, 1), - padding='valid', - data_format='channels_last', - dilation_rate=(1, 1), - activation=None, - use_bias=True, - kernel_initializer=None, - bias_initializer=tf.zeros_initializer(), - kernel_regularizer=None, - bias_regularizer=None, - use_weight_standardization=False, - activity_regularizer=None, - kernel_constraint=None, - bias_constraint=None, - trainable=True, - name=None, - **kwargs): - """Constructs the 2D convolution layer. - - Args: - filters: Integer, the dimensionality of the output space (i.e. the number - of filters in the convolution). - kernel_size: An integer or tuple/list of 2 integers, specifying the height - and width of the 2D convolution window. Can be a single integer to - specify the same value for all spatial dimensions. - strides: An integer or tuple/list of 2 integers, specifying the strides of - the convolution along the height and width. Can be a single integer to - specify the same value for all spatial dimensions. Specifying any stride - value != 1 is incompatible with specifying any `dilation_rate` value != - 1. - padding: One of `"valid"` or `"same"` (case-insensitive). - data_format: A string, one of `channels_last` (default) or - `channels_first`. The ordering of the dimensions in the inputs. - `channels_last` corresponds to inputs with shape `(batch, height, width, - channels)` while `channels_first` corresponds to inputs with shape - `(batch, channels, height, width)`. - dilation_rate: An integer or tuple/list of 2 integers, specifying the - dilation rate to use for dilated convolution. Can be a single integer to - specify the same value for all spatial dimensions. Currently, specifying - any `dilation_rate` value != 1 is incompatible with specifying any - stride value != 1. - activation: Activation function. Set it to None to maintain a linear - activation. - use_bias: Boolean, whether the layer uses a bias. - kernel_initializer: An initializer for the convolution kernel. - bias_initializer: An initializer for the bias vector. If None, the default - initializer will be used. - kernel_regularizer: Optional regularizer for the convolution kernel. - bias_regularizer: Optional regularizer for the bias vector. - use_weight_standardization: Boolean, whether the layer uses weight - standardization. - activity_regularizer: Optional regularizer function for the output. - kernel_constraint: Optional projection function to be applied to the - kernel after being updated by an `Optimizer` (e.g. used to implement - norm constraints or value constraints for layer weights). The function - must take as input the unprojected variable and must return the - projected variable (which must have the same shape). Constraints are not - safe to use when doing asynchronous distributed training. - bias_constraint: Optional projection function to be applied to the bias - after being updated by an `Optimizer`. - trainable: Boolean, if `True` also add variables to the graph collection - `GraphKeys.TRAINABLE_VARIABLES` (see `tf.Variable`). - name: A string, the name of the layer. - **kwargs: Arbitrary keyword arguments passed to tf.keras.layers.Conv2D - """ - - super(Conv2D, self).__init__( - filters=filters, - kernel_size=kernel_size, - strides=strides, - padding=padding, - data_format=data_format, - dilation_rate=dilation_rate, - activation=activation, - use_bias=use_bias, - kernel_initializer=kernel_initializer, - bias_initializer=bias_initializer, - kernel_regularizer=kernel_regularizer, - bias_regularizer=bias_regularizer, - activity_regularizer=activity_regularizer, - kernel_constraint=kernel_constraint, - bias_constraint=bias_constraint, - trainable=trainable, - name=name, - **kwargs) - self.use_weight_standardization = use_weight_standardization - - def call(self, inputs): - if self.use_weight_standardization: - mean, var = tf.nn.moments(self.kernel, [0, 1, 2], keep_dims=True) - kernel = (self.kernel - mean) / tf.sqrt(var + 1e-5) - outputs = self._convolution_op(inputs, kernel) - else: - outputs = self._convolution_op(inputs, self.kernel) - - if self.use_bias: - if self.data_format == 'channels_first': - if self.rank == 1: - # tf.nn.bias_add does not accept a 1D input tensor. - bias = tf.reshape(self.bias, (1, self.filters, 1)) - outputs += bias - else: - outputs = tf.nn.bias_add(outputs, self.bias, data_format='NCHW') - else: - outputs = tf.nn.bias_add(outputs, self.bias, data_format='NHWC') - - if self.activation is not None: - return self.activation(outputs) - return outputs - - -@contrib_framework.add_arg_scope -def conv2d(inputs, - num_outputs, - kernel_size, - stride=1, - padding='SAME', - data_format=None, - rate=1, - activation_fn=tf.nn.relu, - normalizer_fn=None, - normalizer_params=None, - weights_initializer=contrib_layers.xavier_initializer(), - weights_regularizer=None, - biases_initializer=tf.zeros_initializer(), - biases_regularizer=None, - use_weight_standardization=False, - reuse=None, - variables_collections=None, - outputs_collections=None, - trainable=True, - scope=None): - """Adds a 2D convolution followed by an optional batch_norm layer. - - `convolution` creates a variable called `weights`, representing the - convolutional kernel, that is convolved (actually cross-correlated) with the - `inputs` to produce a `Tensor` of activations. If a `normalizer_fn` is - provided (such as `batch_norm`), it is then applied. Otherwise, if - `normalizer_fn` is None and a `biases_initializer` is provided then a `biases` - variable would be created and added the activations. Finally, if - `activation_fn` is not `None`, it is applied to the activations as well. - - Performs atrous convolution with input stride/dilation rate equal to `rate` - if a value > 1 for any dimension of `rate` is specified. In this case - `stride` values != 1 are not supported. - - Args: - inputs: A Tensor of rank N+2 of shape `[batch_size] + input_spatial_shape + - [in_channels]` if data_format does not start with "NC" (default), or - `[batch_size, in_channels] + input_spatial_shape` if data_format starts - with "NC". - num_outputs: Integer, the number of output filters. - kernel_size: A sequence of N positive integers specifying the spatial - dimensions of the filters. Can be a single integer to specify the same - value for all spatial dimensions. - stride: A sequence of N positive integers specifying the stride at which to - compute output. Can be a single integer to specify the same value for all - spatial dimensions. Specifying any `stride` value != 1 is incompatible - with specifying any `rate` value != 1. - padding: One of `"VALID"` or `"SAME"`. - data_format: A string or None. Specifies whether the channel dimension of - the `input` and output is the last dimension (default, or if `data_format` - does not start with "NC"), or the second dimension (if `data_format` - starts with "NC"). For N=1, the valid values are "NWC" (default) and - "NCW". For N=2, the valid values are "NHWC" (default) and "NCHW". For - N=3, the valid values are "NDHWC" (default) and "NCDHW". - rate: A sequence of N positive integers specifying the dilation rate to use - for atrous convolution. Can be a single integer to specify the same value - for all spatial dimensions. Specifying any `rate` value != 1 is - incompatible with specifying any `stride` value != 1. - activation_fn: Activation function. The default value is a ReLU function. - Explicitly set it to None to skip it and maintain a linear activation. - normalizer_fn: Normalization function to use instead of `biases`. If - `normalizer_fn` is provided then `biases_initializer` and - `biases_regularizer` are ignored and `biases` are not created nor added. - default set to None for no normalizer function - normalizer_params: Normalization function parameters. - weights_initializer: An initializer for the weights. - weights_regularizer: Optional regularizer for the weights. - biases_initializer: An initializer for the biases. If None skip biases. - biases_regularizer: Optional regularizer for the biases. - use_weight_standardization: Boolean, whether the layer uses weight - standardization. - reuse: Whether or not the layer and its variables should be reused. To be - able to reuse the layer scope must be given. - variables_collections: Optional list of collections for all the variables or - a dictionary containing a different list of collection per variable. - outputs_collections: Collection to add the outputs. - trainable: If `True` also add variables to the graph collection - `GraphKeys.TRAINABLE_VARIABLES` (see tf.Variable). - scope: Optional scope for `variable_scope`. - - Returns: - A tensor representing the output of the operation. - - Raises: - ValueError: If `data_format` is invalid. - ValueError: Both 'rate' and `stride` are not uniformly 1. - """ - if data_format not in [None, 'NWC', 'NCW', 'NHWC', 'NCHW', 'NDHWC', 'NCDHW']: - raise ValueError('Invalid data_format: %r' % (data_format,)) - - # pylint: disable=protected-access - layer_variable_getter = layers._build_variable_getter({ - 'bias': 'biases', - 'kernel': 'weights' - }) - # pylint: enable=protected-access - with tf.variable_scope( - scope, 'Conv', [inputs], reuse=reuse, - custom_getter=layer_variable_getter) as sc: - inputs = tf.convert_to_tensor(inputs) - input_rank = inputs.get_shape().ndims - - if input_rank != 4: - raise ValueError('Convolution expects input with rank %d, got %d' % - (4, input_rank)) - - data_format = ('channels_first' if data_format and - data_format.startswith('NC') else 'channels_last') - layer = Conv2D( - filters=num_outputs, - kernel_size=kernel_size, - strides=stride, - padding=padding, - data_format=data_format, - dilation_rate=rate, - activation=None, - use_bias=not normalizer_fn and biases_initializer, - kernel_initializer=weights_initializer, - bias_initializer=biases_initializer, - kernel_regularizer=weights_regularizer, - bias_regularizer=biases_regularizer, - use_weight_standardization=use_weight_standardization, - activity_regularizer=None, - trainable=trainable, - name=sc.name, - dtype=inputs.dtype.base_dtype, - _scope=sc, - _reuse=reuse) - outputs = layer.apply(inputs) - - # Add variables to collections. - # pylint: disable=protected-access - layers._add_variable_to_collections(layer.kernel, variables_collections, - 'weights') - if layer.use_bias: - layers._add_variable_to_collections(layer.bias, variables_collections, - 'biases') - # pylint: enable=protected-access - if normalizer_fn is not None: - normalizer_params = normalizer_params or {} - outputs = normalizer_fn(outputs, **normalizer_params) - - if activation_fn is not None: - outputs = activation_fn(outputs) - return utils.collect_named_outputs(outputs_collections, sc.name, outputs) - - -def conv2d_same(inputs, num_outputs, kernel_size, stride, rate=1, scope=None): - """Strided 2-D convolution with 'SAME' padding. - - When stride > 1, then we do explicit zero-padding, followed by conv2d with - 'VALID' padding. - - Note that - - net = conv2d_same(inputs, num_outputs, 3, stride=stride) - - is equivalent to - - net = conv2d(inputs, num_outputs, 3, stride=1, padding='SAME') - net = subsample(net, factor=stride) - - whereas - - net = conv2d(inputs, num_outputs, 3, stride=stride, padding='SAME') - - is different when the input's height or width is even, which is why we add the - current function. For more details, see ResnetUtilsTest.testConv2DSameEven(). - - Args: - inputs: A 4-D tensor of size [batch, height_in, width_in, channels]. - num_outputs: An integer, the number of output filters. - kernel_size: An int with the kernel_size of the filters. - stride: An integer, the output stride. - rate: An integer, rate for atrous convolution. - scope: Scope. - - Returns: - output: A 4-D tensor of size [batch, height_out, width_out, channels] with - the convolution output. - """ - if stride == 1: - return conv2d( - inputs, - num_outputs, - kernel_size, - stride=1, - rate=rate, - padding='SAME', - scope=scope) - else: - kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) - pad_total = kernel_size_effective - 1 - pad_beg = pad_total // 2 - pad_end = pad_total - pad_beg - inputs = tf.pad(inputs, - [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]]) - return conv2d( - inputs, - num_outputs, - kernel_size, - stride=stride, - rate=rate, - padding='VALID', - scope=scope) diff --git a/research/deeplab/core/conv2d_ws_test.py b/research/deeplab/core/conv2d_ws_test.py deleted file mode 100644 index b6bea85ee03..00000000000 --- a/research/deeplab/core/conv2d_ws_test.py +++ /dev/null @@ -1,420 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for conv2d_ws.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf -from tensorflow.contrib import framework as contrib_framework -from tensorflow.contrib import layers as contrib_layers -from deeplab.core import conv2d_ws - - -class ConvolutionTest(tf.test.TestCase): - - def testInvalidShape(self): - with self.cached_session(): - images_3d = tf.random_uniform((5, 6, 7, 9, 3), seed=1) - with self.assertRaisesRegexp( - ValueError, 'Convolution expects input with rank 4, got 5'): - conv2d_ws.conv2d(images_3d, 32, 3) - - def testInvalidDataFormat(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - with self.assertRaisesRegexp(ValueError, 'data_format'): - conv2d_ws.conv2d(images, 32, 3, data_format='CHWN') - - def testCreateConv(self): - height, width = 7, 9 - with self.cached_session(): - images = np.random.uniform(size=(5, height, width, 4)).astype(np.float32) - output = conv2d_ws.conv2d(images, 32, [3, 3]) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32]) - weights = contrib_framework.get_variables_by_name('weights')[0] - self.assertListEqual(weights.get_shape().as_list(), [3, 3, 4, 32]) - biases = contrib_framework.get_variables_by_name('biases')[0] - self.assertListEqual(biases.get_shape().as_list(), [32]) - - def testCreateConvWithWS(self): - height, width = 7, 9 - with self.cached_session(): - images = np.random.uniform(size=(5, height, width, 4)).astype(np.float32) - output = conv2d_ws.conv2d( - images, 32, [3, 3], use_weight_standardization=True) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32]) - weights = contrib_framework.get_variables_by_name('weights')[0] - self.assertListEqual(weights.get_shape().as_list(), [3, 3, 4, 32]) - biases = contrib_framework.get_variables_by_name('biases')[0] - self.assertListEqual(biases.get_shape().as_list(), [32]) - - def testCreateConvNCHW(self): - height, width = 7, 9 - with self.cached_session(): - images = np.random.uniform(size=(5, 4, height, width)).astype(np.float32) - output = conv2d_ws.conv2d(images, 32, [3, 3], data_format='NCHW') - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, 32, height, width]) - weights = contrib_framework.get_variables_by_name('weights')[0] - self.assertListEqual(weights.get_shape().as_list(), [3, 3, 4, 32]) - biases = contrib_framework.get_variables_by_name('biases')[0] - self.assertListEqual(biases.get_shape().as_list(), [32]) - - def testCreateSquareConv(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - output = conv2d_ws.conv2d(images, 32, 3) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32]) - - def testCreateConvWithTensorShape(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - output = conv2d_ws.conv2d(images, 32, images.get_shape()[1:3]) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32]) - - def testCreateFullyConv(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 32), seed=1) - output = conv2d_ws.conv2d( - images, 64, images.get_shape()[1:3], padding='VALID') - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, 1, 1, 64]) - biases = contrib_framework.get_variables_by_name('biases')[0] - self.assertListEqual(biases.get_shape().as_list(), [64]) - - def testFullyConvWithCustomGetter(self): - height, width = 7, 9 - with self.cached_session(): - called = [0] - - def custom_getter(getter, *args, **kwargs): - called[0] += 1 - return getter(*args, **kwargs) - - with tf.variable_scope('test', custom_getter=custom_getter): - images = tf.random_uniform((5, height, width, 32), seed=1) - conv2d_ws.conv2d(images, 64, images.get_shape()[1:3]) - self.assertEqual(called[0], 2) # Custom getter called twice. - - def testCreateVerticalConv(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 4), seed=1) - output = conv2d_ws.conv2d(images, 32, [3, 1]) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32]) - weights = contrib_framework.get_variables_by_name('weights')[0] - self.assertListEqual(weights.get_shape().as_list(), [3, 1, 4, 32]) - biases = contrib_framework.get_variables_by_name('biases')[0] - self.assertListEqual(biases.get_shape().as_list(), [32]) - - def testCreateHorizontalConv(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 4), seed=1) - output = conv2d_ws.conv2d(images, 32, [1, 3]) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32]) - weights = contrib_framework.get_variables_by_name('weights')[0] - self.assertListEqual(weights.get_shape().as_list(), [1, 3, 4, 32]) - - def testCreateConvWithStride(self): - height, width = 6, 8 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - output = conv2d_ws.conv2d(images, 32, [3, 3], stride=2) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), - [5, height / 2, width / 2, 32]) - - def testCreateConvCreatesWeightsAndBiasesVars(self): - height, width = 7, 9 - images = tf.random_uniform((5, height, width, 3), seed=1) - with self.cached_session(): - self.assertFalse(contrib_framework.get_variables('conv1/weights')) - self.assertFalse(contrib_framework.get_variables('conv1/biases')) - conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1') - self.assertTrue(contrib_framework.get_variables('conv1/weights')) - self.assertTrue(contrib_framework.get_variables('conv1/biases')) - - def testCreateConvWithScope(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - output = conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1') - self.assertEqual(output.op.name, 'conv1/Relu') - - def testCreateConvWithCollection(self): - height, width = 7, 9 - images = tf.random_uniform((5, height, width, 3), seed=1) - with tf.name_scope('fe'): - conv = conv2d_ws.conv2d( - images, 32, [3, 3], outputs_collections='outputs', scope='Conv') - output_collected = tf.get_collection('outputs')[0] - self.assertEqual(output_collected.aliases, ['Conv']) - self.assertEqual(output_collected, conv) - - def testCreateConvWithoutActivation(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - output = conv2d_ws.conv2d(images, 32, [3, 3], activation_fn=None) - self.assertEqual(output.op.name, 'Conv/BiasAdd') - - def testCreateConvValid(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - output = conv2d_ws.conv2d(images, 32, [3, 3], padding='VALID') - self.assertListEqual(output.get_shape().as_list(), [5, 5, 7, 32]) - - def testCreateConvWithWD(self): - height, width = 7, 9 - weight_decay = 0.01 - with self.cached_session() as sess: - images = tf.random_uniform((5, height, width, 3), seed=1) - regularizer = contrib_layers.l2_regularizer(weight_decay) - conv2d_ws.conv2d(images, 32, [3, 3], weights_regularizer=regularizer) - l2_loss = tf.nn.l2_loss( - contrib_framework.get_variables_by_name('weights')[0]) - wd = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)[0] - self.assertEqual(wd.op.name, 'Conv/kernel/Regularizer/l2_regularizer') - sess.run(tf.global_variables_initializer()) - self.assertAlmostEqual(sess.run(wd), weight_decay * l2_loss.eval()) - - def testCreateConvNoRegularizers(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - conv2d_ws.conv2d(images, 32, [3, 3]) - self.assertEqual( - tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES), []) - - def testReuseVars(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1') - self.assertEqual(len(contrib_framework.get_variables()), 2) - conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1', reuse=True) - self.assertEqual(len(contrib_framework.get_variables()), 2) - - def testNonReuseVars(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - conv2d_ws.conv2d(images, 32, [3, 3]) - self.assertEqual(len(contrib_framework.get_variables()), 2) - conv2d_ws.conv2d(images, 32, [3, 3]) - self.assertEqual(len(contrib_framework.get_variables()), 4) - - def testReuseConvWithWD(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 3), seed=1) - weight_decay = contrib_layers.l2_regularizer(0.01) - with contrib_framework.arg_scope([conv2d_ws.conv2d], - weights_regularizer=weight_decay): - conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1') - self.assertEqual(len(contrib_framework.get_variables()), 2) - self.assertEqual( - len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1) - conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1', reuse=True) - self.assertEqual(len(contrib_framework.get_variables()), 2) - self.assertEqual( - len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1) - - def testConvWithBatchNorm(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 32), seed=1) - with contrib_framework.arg_scope([conv2d_ws.conv2d], - normalizer_fn=contrib_layers.batch_norm, - normalizer_params={'decay': 0.9}): - net = conv2d_ws.conv2d(images, 32, [3, 3]) - net = conv2d_ws.conv2d(net, 32, [3, 3]) - self.assertEqual(len(contrib_framework.get_variables()), 8) - self.assertEqual( - len(contrib_framework.get_variables('Conv/BatchNorm')), 3) - self.assertEqual( - len(contrib_framework.get_variables('Conv_1/BatchNorm')), 3) - - def testReuseConvWithBatchNorm(self): - height, width = 7, 9 - with self.cached_session(): - images = tf.random_uniform((5, height, width, 32), seed=1) - with contrib_framework.arg_scope([conv2d_ws.conv2d], - normalizer_fn=contrib_layers.batch_norm, - normalizer_params={'decay': 0.9}): - net = conv2d_ws.conv2d(images, 32, [3, 3], scope='Conv') - net = conv2d_ws.conv2d(net, 32, [3, 3], scope='Conv', reuse=True) - self.assertEqual(len(contrib_framework.get_variables()), 4) - self.assertEqual( - len(contrib_framework.get_variables('Conv/BatchNorm')), 3) - self.assertEqual( - len(contrib_framework.get_variables('Conv_1/BatchNorm')), 0) - - def testCreateConvCreatesWeightsAndBiasesVarsWithRateTwo(self): - height, width = 7, 9 - images = tf.random_uniform((5, height, width, 3), seed=1) - with self.cached_session(): - self.assertFalse(contrib_framework.get_variables('conv1/weights')) - self.assertFalse(contrib_framework.get_variables('conv1/biases')) - conv2d_ws.conv2d(images, 32, [3, 3], rate=2, scope='conv1') - self.assertTrue(contrib_framework.get_variables('conv1/weights')) - self.assertTrue(contrib_framework.get_variables('conv1/biases')) - - def testOutputSizeWithRateTwoSamePadding(self): - num_filters = 32 - input_size = [5, 10, 12, 3] - expected_size = [5, 10, 12, num_filters] - - images = tf.random_uniform(input_size, seed=1) - output = conv2d_ws.conv2d( - images, num_filters, [3, 3], rate=2, padding='SAME') - self.assertListEqual(list(output.get_shape().as_list()), expected_size) - with self.cached_session() as sess: - sess.run(tf.global_variables_initializer()) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(list(output.eval().shape), expected_size) - - def testOutputSizeWithRateTwoValidPadding(self): - num_filters = 32 - input_size = [5, 10, 12, 3] - expected_size = [5, 6, 8, num_filters] - - images = tf.random_uniform(input_size, seed=1) - output = conv2d_ws.conv2d( - images, num_filters, [3, 3], rate=2, padding='VALID') - self.assertListEqual(list(output.get_shape().as_list()), expected_size) - with self.cached_session() as sess: - sess.run(tf.global_variables_initializer()) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(list(output.eval().shape), expected_size) - - def testOutputSizeWithRateTwoThreeValidPadding(self): - num_filters = 32 - input_size = [5, 10, 12, 3] - expected_size = [5, 6, 6, num_filters] - - images = tf.random_uniform(input_size, seed=1) - output = conv2d_ws.conv2d( - images, num_filters, [3, 3], rate=[2, 3], padding='VALID') - self.assertListEqual(list(output.get_shape().as_list()), expected_size) - with self.cached_session() as sess: - sess.run(tf.global_variables_initializer()) - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(list(output.eval().shape), expected_size) - - def testDynamicOutputSizeWithRateOneValidPadding(self): - num_filters = 32 - input_size = [5, 9, 11, 3] - expected_size = [None, None, None, num_filters] - expected_size_dynamic = [5, 7, 9, num_filters] - - with self.cached_session(): - images = tf.placeholder(np.float32, [None, None, None, input_size[3]]) - output = conv2d_ws.conv2d( - images, num_filters, [3, 3], rate=1, padding='VALID') - tf.global_variables_initializer().run() - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), expected_size) - eval_output = output.eval({images: np.zeros(input_size, np.float32)}) - self.assertListEqual(list(eval_output.shape), expected_size_dynamic) - - def testDynamicOutputSizeWithRateOneValidPaddingNCHW(self): - if tf.test.is_gpu_available(cuda_only=True): - num_filters = 32 - input_size = [5, 3, 9, 11] - expected_size = [None, num_filters, None, None] - expected_size_dynamic = [5, num_filters, 7, 9] - - with self.session(use_gpu=True): - images = tf.placeholder(np.float32, [None, input_size[1], None, None]) - output = conv2d_ws.conv2d( - images, - num_filters, [3, 3], - rate=1, - padding='VALID', - data_format='NCHW') - tf.global_variables_initializer().run() - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), expected_size) - eval_output = output.eval({images: np.zeros(input_size, np.float32)}) - self.assertListEqual(list(eval_output.shape), expected_size_dynamic) - - def testDynamicOutputSizeWithRateTwoValidPadding(self): - num_filters = 32 - input_size = [5, 9, 11, 3] - expected_size = [None, None, None, num_filters] - expected_size_dynamic = [5, 5, 7, num_filters] - - with self.cached_session(): - images = tf.placeholder(np.float32, [None, None, None, input_size[3]]) - output = conv2d_ws.conv2d( - images, num_filters, [3, 3], rate=2, padding='VALID') - tf.global_variables_initializer().run() - self.assertEqual(output.op.name, 'Conv/Relu') - self.assertListEqual(output.get_shape().as_list(), expected_size) - eval_output = output.eval({images: np.zeros(input_size, np.float32)}) - self.assertListEqual(list(eval_output.shape), expected_size_dynamic) - - def testWithScope(self): - num_filters = 32 - input_size = [5, 9, 11, 3] - expected_size = [5, 5, 7, num_filters] - - images = tf.random_uniform(input_size, seed=1) - output = conv2d_ws.conv2d( - images, num_filters, [3, 3], rate=2, padding='VALID', scope='conv7') - with self.cached_session() as sess: - sess.run(tf.global_variables_initializer()) - self.assertEqual(output.op.name, 'conv7/Relu') - self.assertListEqual(list(output.eval().shape), expected_size) - - def testWithScopeWithoutActivation(self): - num_filters = 32 - input_size = [5, 9, 11, 3] - expected_size = [5, 5, 7, num_filters] - - images = tf.random_uniform(input_size, seed=1) - output = conv2d_ws.conv2d( - images, - num_filters, [3, 3], - rate=2, - padding='VALID', - activation_fn=None, - scope='conv7') - with self.cached_session() as sess: - sess.run(tf.global_variables_initializer()) - self.assertEqual(output.op.name, 'conv7/BiasAdd') - self.assertListEqual(list(output.eval().shape), expected_size) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/core/dense_prediction_cell.py b/research/deeplab/core/dense_prediction_cell.py deleted file mode 100644 index 8e32f8e227f..00000000000 --- a/research/deeplab/core/dense_prediction_cell.py +++ /dev/null @@ -1,290 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Dense Prediction Cell class that can be evolved in semantic segmentation. - -DensePredictionCell is used as a `layer` in semantic segmentation whose -architecture is determined by the `config`, a dictionary specifying -the architecture. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf -from tensorflow.contrib import slim as contrib_slim - -from deeplab.core import utils - -slim = contrib_slim - -# Local constants. -_META_ARCHITECTURE_SCOPE = 'meta_architecture' -_CONCAT_PROJECTION_SCOPE = 'concat_projection' -_OP = 'op' -_CONV = 'conv' -_PYRAMID_POOLING = 'pyramid_pooling' -_KERNEL = 'kernel' -_RATE = 'rate' -_GRID_SIZE = 'grid_size' -_TARGET_SIZE = 'target_size' -_INPUT = 'input' - - -def dense_prediction_cell_hparams(): - """DensePredictionCell HParams. - - Returns: - A dictionary of hyper-parameters used for dense prediction cell with keys: - - reduction_size: Integer, the number of output filters for each operation - inside the cell. - - dropout_on_concat_features: Boolean, apply dropout on the concatenated - features or not. - - dropout_on_projection_features: Boolean, apply dropout on the projection - features or not. - - dropout_keep_prob: Float, when `dropout_on_concat_features' or - `dropout_on_projection_features' is True, the `keep_prob` value used - in the dropout operation. - - concat_channels: Integer, the concatenated features will be - channel-reduced to `concat_channels` channels. - - conv_rate_multiplier: Integer, used to multiply the convolution rates. - This is useful in the case when the output_stride is changed from 16 - to 8, we need to double the convolution rates correspondingly. - """ - return { - 'reduction_size': 256, - 'dropout_on_concat_features': True, - 'dropout_on_projection_features': False, - 'dropout_keep_prob': 0.9, - 'concat_channels': 256, - 'conv_rate_multiplier': 1, - } - - -class DensePredictionCell(object): - """DensePredictionCell class used as a 'layer' in semantic segmentation.""" - - def __init__(self, config, hparams=None): - """Initializes the dense prediction cell. - - Args: - config: A dictionary storing the architecture of a dense prediction cell. - hparams: A dictionary of hyper-parameters, provided by users. This - dictionary will be used to update the default dictionary returned by - dense_prediction_cell_hparams(). - - Raises: - ValueError: If `conv_rate_multiplier` has value < 1. - """ - self.hparams = dense_prediction_cell_hparams() - if hparams is not None: - self.hparams.update(hparams) - self.config = config - - # Check values in hparams are valid or not. - if self.hparams['conv_rate_multiplier'] < 1: - raise ValueError('conv_rate_multiplier cannot have value < 1.') - - def _get_pyramid_pooling_arguments( - self, crop_size, output_stride, image_grid, image_pooling_crop_size=None): - """Gets arguments for pyramid pooling. - - Args: - crop_size: A list of two integers, [crop_height, crop_width] specifying - whole patch crop size. - output_stride: Integer, output stride value for extracted features. - image_grid: A list of two integers, [image_grid_height, image_grid_width], - specifying the grid size of how the pyramid pooling will be performed. - image_pooling_crop_size: A list of two integers, [crop_height, crop_width] - specifying the crop size for image pooling operations. Note that we - decouple whole patch crop_size and image_pooling_crop_size as one could - perform the image_pooling with different crop sizes. - - Returns: - A list of (resize_value, pooled_kernel) - """ - resize_height = utils.scale_dimension(crop_size[0], 1. / output_stride) - resize_width = utils.scale_dimension(crop_size[1], 1. / output_stride) - # If image_pooling_crop_size is not specified, use crop_size. - if image_pooling_crop_size is None: - image_pooling_crop_size = crop_size - pooled_height = utils.scale_dimension( - image_pooling_crop_size[0], 1. / (output_stride * image_grid[0])) - pooled_width = utils.scale_dimension( - image_pooling_crop_size[1], 1. / (output_stride * image_grid[1])) - return ([resize_height, resize_width], [pooled_height, pooled_width]) - - def _parse_operation(self, config, crop_size, output_stride, - image_pooling_crop_size=None): - """Parses one operation. - - When 'operation' is 'pyramid_pooling', we compute the required - hyper-parameters and save in config. - - Args: - config: A dictionary storing required hyper-parameters for one - operation. - crop_size: A list of two integers, [crop_height, crop_width] specifying - whole patch crop size. - output_stride: Integer, output stride value for extracted features. - image_pooling_crop_size: A list of two integers, [crop_height, crop_width] - specifying the crop size for image pooling operations. Note that we - decouple whole patch crop_size and image_pooling_crop_size as one could - perform the image_pooling with different crop sizes. - - Returns: - A dictionary stores the related information for the operation. - """ - if config[_OP] == _PYRAMID_POOLING: - (config[_TARGET_SIZE], - config[_KERNEL]) = self._get_pyramid_pooling_arguments( - crop_size=crop_size, - output_stride=output_stride, - image_grid=config[_GRID_SIZE], - image_pooling_crop_size=image_pooling_crop_size) - - return config - - def build_cell(self, - features, - output_stride=16, - crop_size=None, - image_pooling_crop_size=None, - weight_decay=0.00004, - reuse=None, - is_training=False, - fine_tune_batch_norm=False, - scope=None): - """Builds the dense prediction cell based on the config. - - Args: - features: Input feature map of size [batch, height, width, channels]. - output_stride: Int, output stride at which the features were extracted. - crop_size: A list [crop_height, crop_width], determining the input - features resolution. - image_pooling_crop_size: A list of two integers, [crop_height, crop_width] - specifying the crop size for image pooling operations. Note that we - decouple whole patch crop_size and image_pooling_crop_size as one could - perform the image_pooling with different crop sizes. - weight_decay: Float, the weight decay for model variables. - reuse: Reuse the model variables or not. - is_training: Boolean, is training or not. - fine_tune_batch_norm: Boolean, fine-tuning batch norm parameters or not. - scope: Optional string, specifying the variable scope. - - Returns: - Features after passing through the constructed dense prediction cell with - shape = [batch, height, width, channels] where channels are determined - by `reduction_size` returned by dense_prediction_cell_hparams(). - - Raises: - ValueError: Use Convolution with kernel size not equal to 1x1 or 3x3 or - the operation is not recognized. - """ - batch_norm_params = { - 'is_training': is_training and fine_tune_batch_norm, - 'decay': 0.9997, - 'epsilon': 1e-5, - 'scale': True, - } - hparams = self.hparams - with slim.arg_scope( - [slim.conv2d, slim.separable_conv2d], - weights_regularizer=slim.l2_regularizer(weight_decay), - activation_fn=tf.nn.relu, - normalizer_fn=slim.batch_norm, - padding='SAME', - stride=1, - reuse=reuse): - with slim.arg_scope([slim.batch_norm], **batch_norm_params): - with tf.variable_scope(scope, _META_ARCHITECTURE_SCOPE, [features]): - depth = hparams['reduction_size'] - branch_logits = [] - for i, current_config in enumerate(self.config): - scope = 'branch%d' % i - current_config = self._parse_operation( - config=current_config, - crop_size=crop_size, - output_stride=output_stride, - image_pooling_crop_size=image_pooling_crop_size) - tf.logging.info(current_config) - if current_config[_INPUT] < 0: - operation_input = features - else: - operation_input = branch_logits[current_config[_INPUT]] - if current_config[_OP] == _CONV: - if current_config[_KERNEL] == [1, 1] or current_config[ - _KERNEL] == 1: - branch_logits.append( - slim.conv2d(operation_input, depth, 1, scope=scope)) - else: - conv_rate = [r * hparams['conv_rate_multiplier'] - for r in current_config[_RATE]] - branch_logits.append( - utils.split_separable_conv2d( - operation_input, - filters=depth, - kernel_size=current_config[_KERNEL], - rate=conv_rate, - weight_decay=weight_decay, - scope=scope)) - elif current_config[_OP] == _PYRAMID_POOLING: - pooled_features = slim.avg_pool2d( - operation_input, - kernel_size=current_config[_KERNEL], - stride=[1, 1], - padding='VALID') - pooled_features = slim.conv2d( - pooled_features, - depth, - 1, - scope=scope) - pooled_features = tf.image.resize_bilinear( - pooled_features, - current_config[_TARGET_SIZE], - align_corners=True) - # Set shape for resize_height/resize_width if they are not Tensor. - resize_height = current_config[_TARGET_SIZE][0] - resize_width = current_config[_TARGET_SIZE][1] - if isinstance(resize_height, tf.Tensor): - resize_height = None - if isinstance(resize_width, tf.Tensor): - resize_width = None - pooled_features.set_shape( - [None, resize_height, resize_width, depth]) - branch_logits.append(pooled_features) - else: - raise ValueError('Unrecognized operation.') - # Merge branch logits. - concat_logits = tf.concat(branch_logits, 3) - if self.hparams['dropout_on_concat_features']: - concat_logits = slim.dropout( - concat_logits, - keep_prob=self.hparams['dropout_keep_prob'], - is_training=is_training, - scope=_CONCAT_PROJECTION_SCOPE + '_dropout') - concat_logits = slim.conv2d(concat_logits, - self.hparams['concat_channels'], - 1, - scope=_CONCAT_PROJECTION_SCOPE) - if self.hparams['dropout_on_projection_features']: - concat_logits = slim.dropout( - concat_logits, - keep_prob=self.hparams['dropout_keep_prob'], - is_training=is_training, - scope=_CONCAT_PROJECTION_SCOPE + '_dropout') - return concat_logits diff --git a/research/deeplab/core/dense_prediction_cell_branch5_top1_cityscapes.json b/research/deeplab/core/dense_prediction_cell_branch5_top1_cityscapes.json deleted file mode 100644 index 12b093d07d1..00000000000 --- a/research/deeplab/core/dense_prediction_cell_branch5_top1_cityscapes.json +++ /dev/null @@ -1 +0,0 @@ -[{"kernel": 3, "rate": [1, 6], "op": "conv", "input": -1}, {"kernel": 3, "rate": [18, 15], "op": "conv", "input": 0}, {"kernel": 3, "rate": [6, 3], "op": "conv", "input": 1}, {"kernel": 3, "rate": [1, 1], "op": "conv", "input": 0}, {"kernel": 3, "rate": [6, 21], "op": "conv", "input": 0}] \ No newline at end of file diff --git a/research/deeplab/core/dense_prediction_cell_test.py b/research/deeplab/core/dense_prediction_cell_test.py deleted file mode 100644 index 1396a73626d..00000000000 --- a/research/deeplab/core/dense_prediction_cell_test.py +++ /dev/null @@ -1,136 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for dense_prediction_cell.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from deeplab.core import dense_prediction_cell - - -class DensePredictionCellTest(tf.test.TestCase): - - def setUp(self): - self.segmentation_layer = dense_prediction_cell.DensePredictionCell( - config=[ - { - dense_prediction_cell._INPUT: -1, - dense_prediction_cell._OP: dense_prediction_cell._CONV, - dense_prediction_cell._KERNEL: 1, - }, - { - dense_prediction_cell._INPUT: 0, - dense_prediction_cell._OP: dense_prediction_cell._CONV, - dense_prediction_cell._KERNEL: 3, - dense_prediction_cell._RATE: [1, 3], - }, - { - dense_prediction_cell._INPUT: 1, - dense_prediction_cell._OP: ( - dense_prediction_cell._PYRAMID_POOLING), - dense_prediction_cell._GRID_SIZE: [1, 2], - }, - ], - hparams={'conv_rate_multiplier': 2}) - - def testPyramidPoolingArguments(self): - features_size, pooled_kernel = ( - self.segmentation_layer._get_pyramid_pooling_arguments( - crop_size=[513, 513], - output_stride=16, - image_grid=[4, 4])) - self.assertListEqual(features_size, [33, 33]) - self.assertListEqual(pooled_kernel, [9, 9]) - - def testPyramidPoolingArgumentsWithImageGrid1x1(self): - features_size, pooled_kernel = ( - self.segmentation_layer._get_pyramid_pooling_arguments( - crop_size=[257, 257], - output_stride=16, - image_grid=[1, 1])) - self.assertListEqual(features_size, [17, 17]) - self.assertListEqual(pooled_kernel, [17, 17]) - - def testParseOperationStringWithConv1x1(self): - operation = self.segmentation_layer._parse_operation( - config={ - dense_prediction_cell._OP: dense_prediction_cell._CONV, - dense_prediction_cell._KERNEL: [1, 1], - }, - crop_size=[513, 513], output_stride=16) - self.assertEqual(operation[dense_prediction_cell._OP], - dense_prediction_cell._CONV) - self.assertListEqual(operation[dense_prediction_cell._KERNEL], [1, 1]) - - def testParseOperationStringWithConv3x3(self): - operation = self.segmentation_layer._parse_operation( - config={ - dense_prediction_cell._OP: dense_prediction_cell._CONV, - dense_prediction_cell._KERNEL: [3, 3], - dense_prediction_cell._RATE: [9, 6], - }, - crop_size=[513, 513], output_stride=16) - self.assertEqual(operation[dense_prediction_cell._OP], - dense_prediction_cell._CONV) - self.assertListEqual(operation[dense_prediction_cell._KERNEL], [3, 3]) - self.assertEqual(operation[dense_prediction_cell._RATE], [9, 6]) - - def testParseOperationStringWithPyramidPooling2x2(self): - operation = self.segmentation_layer._parse_operation( - config={ - dense_prediction_cell._OP: dense_prediction_cell._PYRAMID_POOLING, - dense_prediction_cell._GRID_SIZE: [2, 2], - }, - crop_size=[513, 513], - output_stride=16) - self.assertEqual(operation[dense_prediction_cell._OP], - dense_prediction_cell._PYRAMID_POOLING) - # The feature maps of size [33, 33] should be covered by 2x2 kernels with - # size [17, 17]. - self.assertListEqual( - operation[dense_prediction_cell._TARGET_SIZE], [33, 33]) - self.assertListEqual(operation[dense_prediction_cell._KERNEL], [17, 17]) - - def testBuildCell(self): - with self.test_session(graph=tf.Graph()) as sess: - features = tf.random_normal([2, 33, 33, 5]) - concat_logits = self.segmentation_layer.build_cell( - features, - output_stride=8, - crop_size=[257, 257]) - sess.run(tf.global_variables_initializer()) - concat_logits = sess.run(concat_logits) - self.assertTrue(concat_logits.any()) - - def testBuildCellWithImagePoolingCropSize(self): - with self.test_session(graph=tf.Graph()) as sess: - features = tf.random_normal([2, 33, 33, 5]) - concat_logits = self.segmentation_layer.build_cell( - features, - output_stride=8, - crop_size=[257, 257], - image_pooling_crop_size=[129, 129]) - sess.run(tf.global_variables_initializer()) - concat_logits = sess.run(concat_logits) - self.assertTrue(concat_logits.any()) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/core/feature_extractor.py b/research/deeplab/core/feature_extractor.py deleted file mode 100644 index 553bd9b6a73..00000000000 --- a/research/deeplab/core/feature_extractor.py +++ /dev/null @@ -1,711 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Extracts features for different models.""" -import copy -import functools - -import tensorflow.compat.v1 as tf -from tensorflow.contrib import slim as contrib_slim - -from deeplab.core import nas_network -from deeplab.core import resnet_v1_beta -from deeplab.core import xception -from nets.mobilenet import conv_blocks -from nets.mobilenet import mobilenet -from nets.mobilenet import mobilenet_v2 -from nets.mobilenet import mobilenet_v3 - -slim = contrib_slim - -# Default end point for MobileNetv2 (one-based indexing). -_MOBILENET_V2_FINAL_ENDPOINT = 'layer_18' -# Default end point for MobileNetv3. -_MOBILENET_V3_LARGE_FINAL_ENDPOINT = 'layer_17' -_MOBILENET_V3_SMALL_FINAL_ENDPOINT = 'layer_13' -# Default end point for EdgeTPU Mobilenet. -_MOBILENET_EDGETPU = 'layer_24' - - -def _mobilenet_v2(net, - depth_multiplier, - output_stride, - conv_defs=None, - divisible_by=None, - reuse=None, - scope=None, - final_endpoint=None): - """Auxiliary function to add support for 'reuse' to mobilenet_v2. - - Args: - net: Input tensor of shape [batch_size, height, width, channels]. - depth_multiplier: Float multiplier for the depth (number of channels) - for all convolution ops. The value must be greater than zero. Typical - usage will be to set this value in (0, 1) to reduce the number of - parameters or computation cost of the model. - output_stride: An integer that specifies the requested ratio of input to - output spatial resolution. If not None, then we invoke atrous convolution - if necessary to prevent the network from reducing the spatial resolution - of the activation maps. Allowed values are 8 (accurate fully convolutional - mode), 16 (fast fully convolutional mode), 32 (classification mode). - conv_defs: MobileNet con def. - divisible_by: None (use default setting) or an integer that ensures all - layers # channels will be divisible by this number. Used in MobileNet. - reuse: Reuse model variables. - scope: Optional variable scope. - final_endpoint: The endpoint to construct the network up to. - - Returns: - Features extracted by MobileNetv2. - """ - if divisible_by is None: - divisible_by = 8 if depth_multiplier == 1.0 else 1 - if conv_defs is None: - conv_defs = mobilenet_v2.V2_DEF - with tf.variable_scope( - scope, 'MobilenetV2', [net], reuse=reuse) as scope: - return mobilenet_v2.mobilenet_base( - net, - conv_defs=conv_defs, - depth_multiplier=depth_multiplier, - min_depth=8 if depth_multiplier == 1.0 else 1, - divisible_by=divisible_by, - final_endpoint=final_endpoint or _MOBILENET_V2_FINAL_ENDPOINT, - output_stride=output_stride, - scope=scope) - - -def _mobilenet_v3(net, - depth_multiplier, - output_stride, - conv_defs=None, - divisible_by=None, - reuse=None, - scope=None, - final_endpoint=None): - """Auxiliary function to build mobilenet v3. - - Args: - net: Input tensor of shape [batch_size, height, width, channels]. - depth_multiplier: Float multiplier for the depth (number of channels) - for all convolution ops. The value must be greater than zero. Typical - usage will be to set this value in (0, 1) to reduce the number of - parameters or computation cost of the model. - output_stride: An integer that specifies the requested ratio of input to - output spatial resolution. If not None, then we invoke atrous convolution - if necessary to prevent the network from reducing the spatial resolution - of the activation maps. Allowed values are 8 (accurate fully convolutional - mode), 16 (fast fully convolutional mode), 32 (classification mode). - conv_defs: A list of ConvDef namedtuples specifying the net architecture. - divisible_by: None (use default setting) or an integer that ensures all - layers # channels will be divisible by this number. Used in MobileNet. - reuse: Reuse model variables. - scope: Optional variable scope. - final_endpoint: The endpoint to construct the network up to. - - Returns: - net: The output tensor. - end_points: A set of activations for external use. - - Raises: - ValueError: If conv_defs or final_endpoint is not specified. - """ - del divisible_by - with tf.variable_scope( - scope, 'MobilenetV3', [net], reuse=reuse) as scope: - if conv_defs is None: - raise ValueError('conv_defs must be specified for mobilenet v3.') - if final_endpoint is None: - raise ValueError('Final endpoint must be specified for mobilenet v3.') - net, end_points = mobilenet_v3.mobilenet_base( - net, - depth_multiplier=depth_multiplier, - conv_defs=conv_defs, - output_stride=output_stride, - final_endpoint=final_endpoint, - scope=scope) - - return net, end_points - - -def mobilenet_v3_large_seg(net, - depth_multiplier, - output_stride, - divisible_by=None, - reuse=None, - scope=None, - final_endpoint=None): - """Final mobilenet v3 large model for segmentation task.""" - del divisible_by - del final_endpoint - conv_defs = copy.deepcopy(mobilenet_v3.V3_LARGE) - - # Reduce the filters by a factor of 2 in the last block. - for layer, expansion in [(13, 336), (14, 480), (15, 480), (16, None)]: - conv_defs['spec'][layer].params['num_outputs'] /= 2 - # Update expansion size - if expansion is not None: - factor = expansion / conv_defs['spec'][layer - 1].params['num_outputs'] - conv_defs['spec'][layer].params[ - 'expansion_size'] = mobilenet_v3.expand_input(factor) - - return _mobilenet_v3( - net, - depth_multiplier=depth_multiplier, - output_stride=output_stride, - divisible_by=8, - conv_defs=conv_defs, - reuse=reuse, - scope=scope, - final_endpoint=_MOBILENET_V3_LARGE_FINAL_ENDPOINT) - - -def mobilenet_edgetpu(net, - depth_multiplier, - output_stride, - divisible_by=None, - reuse=None, - scope=None, - final_endpoint=None): - """EdgeTPU version of mobilenet model for segmentation task.""" - del divisible_by - del final_endpoint - conv_defs = copy.deepcopy(mobilenet_v3.V3_EDGETPU) - - return _mobilenet_v3( - net, - depth_multiplier=depth_multiplier, - output_stride=output_stride, - divisible_by=8, - conv_defs=conv_defs, - reuse=reuse, - scope=scope, # the scope is 'MobilenetEdgeTPU' - final_endpoint=_MOBILENET_EDGETPU) - - -def mobilenet_v3_small_seg(net, - depth_multiplier, - output_stride, - divisible_by=None, - reuse=None, - scope=None, - final_endpoint=None): - """Final mobilenet v3 small model for segmentation task.""" - del divisible_by - del final_endpoint - conv_defs = copy.deepcopy(mobilenet_v3.V3_SMALL) - - # Reduce the filters by a factor of 2 in the last block. - for layer, expansion in [(9, 144), (10, 288), (11, 288), (12, None)]: - conv_defs['spec'][layer].params['num_outputs'] /= 2 - # Update expansion size - if expansion is not None: - factor = expansion / conv_defs['spec'][layer - 1].params['num_outputs'] - conv_defs['spec'][layer].params[ - 'expansion_size'] = mobilenet_v3.expand_input(factor) - - return _mobilenet_v3( - net, - depth_multiplier=depth_multiplier, - output_stride=output_stride, - divisible_by=8, - conv_defs=conv_defs, - reuse=reuse, - scope=scope, - final_endpoint=_MOBILENET_V3_SMALL_FINAL_ENDPOINT) - - -# A map from network name to network function. -networks_map = { - 'mobilenet_v2': _mobilenet_v2, - 'mobilenet_edgetpu': mobilenet_edgetpu, - 'mobilenet_v3_large_seg': mobilenet_v3_large_seg, - 'mobilenet_v3_small_seg': mobilenet_v3_small_seg, - 'resnet_v1_18': resnet_v1_beta.resnet_v1_18, - 'resnet_v1_18_beta': resnet_v1_beta.resnet_v1_18_beta, - 'resnet_v1_50': resnet_v1_beta.resnet_v1_50, - 'resnet_v1_50_beta': resnet_v1_beta.resnet_v1_50_beta, - 'resnet_v1_101': resnet_v1_beta.resnet_v1_101, - 'resnet_v1_101_beta': resnet_v1_beta.resnet_v1_101_beta, - 'xception_41': xception.xception_41, - 'xception_65': xception.xception_65, - 'xception_71': xception.xception_71, - 'nas_pnasnet': nas_network.pnasnet, - 'nas_hnasnet': nas_network.hnasnet, -} - - -def mobilenet_v2_arg_scope(is_training=True, - weight_decay=0.00004, - stddev=0.09, - activation=tf.nn.relu6, - bn_decay=0.997, - bn_epsilon=None, - bn_renorm=None): - """Defines the default MobilenetV2 arg scope. - - Args: - is_training: Whether or not we're training the model. If this is set to None - is_training parameter in batch_norm is not set. Please note that this also - sets the is_training parameter in dropout to None. - weight_decay: The weight decay to use for regularizing the model. - stddev: Standard deviation for initialization, if negative uses xavier. - activation: If True, a modified activation is used (initialized ~ReLU6). - bn_decay: decay for the batch norm moving averages. - bn_epsilon: batch normalization epsilon. - bn_renorm: whether to use batchnorm renormalization - - Returns: - An `arg_scope` to use for the mobilenet v1 model. - """ - batch_norm_params = { - 'center': True, - 'scale': True, - 'decay': bn_decay, - } - if bn_epsilon is not None: - batch_norm_params['epsilon'] = bn_epsilon - if is_training is not None: - batch_norm_params['is_training'] = is_training - if bn_renorm is not None: - batch_norm_params['renorm'] = bn_renorm - dropout_params = {} - if is_training is not None: - dropout_params['is_training'] = is_training - - instance_norm_params = { - 'center': True, - 'scale': True, - 'epsilon': 0.001, - } - - if stddev < 0: - weight_intitializer = slim.initializers.xavier_initializer() - else: - weight_intitializer = tf.truncated_normal_initializer(stddev=stddev) - - # Set weight_decay for weights in Conv and FC layers. - with slim.arg_scope( - [slim.conv2d, slim.fully_connected, slim.separable_conv2d], - weights_initializer=weight_intitializer, - activation_fn=activation, - normalizer_fn=slim.batch_norm), \ - slim.arg_scope( - [conv_blocks.expanded_conv], normalizer_fn=slim.batch_norm), \ - slim.arg_scope([mobilenet.apply_activation], activation_fn=activation),\ - slim.arg_scope([slim.batch_norm], **batch_norm_params), \ - slim.arg_scope([mobilenet.mobilenet_base, mobilenet.mobilenet], - is_training=is_training),\ - slim.arg_scope([slim.dropout], **dropout_params), \ - slim.arg_scope([slim.instance_norm], **instance_norm_params), \ - slim.arg_scope([slim.conv2d], \ - weights_regularizer=slim.l2_regularizer(weight_decay)), \ - slim.arg_scope([slim.separable_conv2d], weights_regularizer=None), \ - slim.arg_scope([slim.conv2d, slim.separable_conv2d], padding='SAME') as s: - return s - - -# A map from network name to network arg scope. -arg_scopes_map = { - 'mobilenet_v2': mobilenet_v2.training_scope, - 'mobilenet_edgetpu': mobilenet_v2_arg_scope, - 'mobilenet_v3_large_seg': mobilenet_v2_arg_scope, - 'mobilenet_v3_small_seg': mobilenet_v2_arg_scope, - 'resnet_v1_18': resnet_v1_beta.resnet_arg_scope, - 'resnet_v1_18_beta': resnet_v1_beta.resnet_arg_scope, - 'resnet_v1_50': resnet_v1_beta.resnet_arg_scope, - 'resnet_v1_50_beta': resnet_v1_beta.resnet_arg_scope, - 'resnet_v1_101': resnet_v1_beta.resnet_arg_scope, - 'resnet_v1_101_beta': resnet_v1_beta.resnet_arg_scope, - 'xception_41': xception.xception_arg_scope, - 'xception_65': xception.xception_arg_scope, - 'xception_71': xception.xception_arg_scope, - 'nas_pnasnet': nas_network.nas_arg_scope, - 'nas_hnasnet': nas_network.nas_arg_scope, -} - -# Names for end point features. -DECODER_END_POINTS = 'decoder_end_points' - -# A dictionary from network name to a map of end point features. -networks_to_feature_maps = { - 'mobilenet_v2': { - DECODER_END_POINTS: { - 4: ['layer_4/depthwise_output'], - 8: ['layer_7/depthwise_output'], - 16: ['layer_14/depthwise_output'], - }, - }, - 'mobilenet_v3_large_seg': { - DECODER_END_POINTS: { - 4: ['layer_4/depthwise_output'], - 8: ['layer_7/depthwise_output'], - 16: ['layer_13/depthwise_output'], - }, - }, - 'mobilenet_v3_small_seg': { - DECODER_END_POINTS: { - 4: ['layer_2/depthwise_output'], - 8: ['layer_4/depthwise_output'], - 16: ['layer_9/depthwise_output'], - }, - }, - 'resnet_v1_18': { - DECODER_END_POINTS: { - 4: ['block1/unit_1/lite_bottleneck_v1/conv2'], - 8: ['block2/unit_1/lite_bottleneck_v1/conv2'], - 16: ['block3/unit_1/lite_bottleneck_v1/conv2'], - }, - }, - 'resnet_v1_18_beta': { - DECODER_END_POINTS: { - 4: ['block1/unit_1/lite_bottleneck_v1/conv2'], - 8: ['block2/unit_1/lite_bottleneck_v1/conv2'], - 16: ['block3/unit_1/lite_bottleneck_v1/conv2'], - }, - }, - 'resnet_v1_50': { - DECODER_END_POINTS: { - 4: ['block1/unit_2/bottleneck_v1/conv3'], - 8: ['block2/unit_3/bottleneck_v1/conv3'], - 16: ['block3/unit_5/bottleneck_v1/conv3'], - }, - }, - 'resnet_v1_50_beta': { - DECODER_END_POINTS: { - 4: ['block1/unit_2/bottleneck_v1/conv3'], - 8: ['block2/unit_3/bottleneck_v1/conv3'], - 16: ['block3/unit_5/bottleneck_v1/conv3'], - }, - }, - 'resnet_v1_101': { - DECODER_END_POINTS: { - 4: ['block1/unit_2/bottleneck_v1/conv3'], - 8: ['block2/unit_3/bottleneck_v1/conv3'], - 16: ['block3/unit_22/bottleneck_v1/conv3'], - }, - }, - 'resnet_v1_101_beta': { - DECODER_END_POINTS: { - 4: ['block1/unit_2/bottleneck_v1/conv3'], - 8: ['block2/unit_3/bottleneck_v1/conv3'], - 16: ['block3/unit_22/bottleneck_v1/conv3'], - }, - }, - 'xception_41': { - DECODER_END_POINTS: { - 4: ['entry_flow/block2/unit_1/xception_module/' - 'separable_conv2_pointwise'], - 8: ['entry_flow/block3/unit_1/xception_module/' - 'separable_conv2_pointwise'], - 16: ['exit_flow/block1/unit_1/xception_module/' - 'separable_conv2_pointwise'], - }, - }, - 'xception_65': { - DECODER_END_POINTS: { - 4: ['entry_flow/block2/unit_1/xception_module/' - 'separable_conv2_pointwise'], - 8: ['entry_flow/block3/unit_1/xception_module/' - 'separable_conv2_pointwise'], - 16: ['exit_flow/block1/unit_1/xception_module/' - 'separable_conv2_pointwise'], - }, - }, - 'xception_71': { - DECODER_END_POINTS: { - 4: ['entry_flow/block3/unit_1/xception_module/' - 'separable_conv2_pointwise'], - 8: ['entry_flow/block5/unit_1/xception_module/' - 'separable_conv2_pointwise'], - 16: ['exit_flow/block1/unit_1/xception_module/' - 'separable_conv2_pointwise'], - }, - }, - 'nas_pnasnet': { - DECODER_END_POINTS: { - 4: ['Stem'], - 8: ['Cell_3'], - 16: ['Cell_7'], - }, - }, - 'nas_hnasnet': { - DECODER_END_POINTS: { - 4: ['Cell_2'], - 8: ['Cell_5'], - 16: ['Cell_7'], - }, - }, -} - -# A map from feature extractor name to the network name scope used in the -# ImageNet pretrained versions of these models. -name_scope = { - 'mobilenet_v2': 'MobilenetV2', - 'mobilenet_edgetpu': 'MobilenetEdgeTPU', - 'mobilenet_v3_large_seg': 'MobilenetV3', - 'mobilenet_v3_small_seg': 'MobilenetV3', - 'resnet_v1_18': 'resnet_v1_18', - 'resnet_v1_18_beta': 'resnet_v1_18', - 'resnet_v1_50': 'resnet_v1_50', - 'resnet_v1_50_beta': 'resnet_v1_50', - 'resnet_v1_101': 'resnet_v1_101', - 'resnet_v1_101_beta': 'resnet_v1_101', - 'xception_41': 'xception_41', - 'xception_65': 'xception_65', - 'xception_71': 'xception_71', - 'nas_pnasnet': 'pnasnet', - 'nas_hnasnet': 'hnasnet', -} - -# Mean pixel value. -_MEAN_RGB = [123.15, 115.90, 103.06] - - -def _preprocess_subtract_imagenet_mean(inputs, dtype=tf.float32): - """Subtract Imagenet mean RGB value.""" - mean_rgb = tf.reshape(_MEAN_RGB, [1, 1, 1, 3]) - num_channels = tf.shape(inputs)[-1] - # We set mean pixel as 0 for the non-RGB channels. - mean_rgb_extended = tf.concat( - [mean_rgb, tf.zeros([1, 1, 1, num_channels - 3])], axis=3) - return tf.cast(inputs - mean_rgb_extended, dtype=dtype) - - -def _preprocess_zero_mean_unit_range(inputs, dtype=tf.float32): - """Map image values from [0, 255] to [-1, 1].""" - preprocessed_inputs = (2.0 / 255.0) * tf.to_float(inputs) - 1.0 - return tf.cast(preprocessed_inputs, dtype=dtype) - - -_PREPROCESS_FN = { - 'mobilenet_v2': _preprocess_zero_mean_unit_range, - 'mobilenet_edgetpu': _preprocess_zero_mean_unit_range, - 'mobilenet_v3_large_seg': _preprocess_zero_mean_unit_range, - 'mobilenet_v3_small_seg': _preprocess_zero_mean_unit_range, - 'resnet_v1_18': _preprocess_subtract_imagenet_mean, - 'resnet_v1_18_beta': _preprocess_zero_mean_unit_range, - 'resnet_v1_50': _preprocess_subtract_imagenet_mean, - 'resnet_v1_50_beta': _preprocess_zero_mean_unit_range, - 'resnet_v1_101': _preprocess_subtract_imagenet_mean, - 'resnet_v1_101_beta': _preprocess_zero_mean_unit_range, - 'xception_41': _preprocess_zero_mean_unit_range, - 'xception_65': _preprocess_zero_mean_unit_range, - 'xception_71': _preprocess_zero_mean_unit_range, - 'nas_pnasnet': _preprocess_zero_mean_unit_range, - 'nas_hnasnet': _preprocess_zero_mean_unit_range, -} - - -def mean_pixel(model_variant=None): - """Gets mean pixel value. - - This function returns different mean pixel value, depending on the input - model_variant which adopts different preprocessing functions. We currently - handle the following preprocessing functions: - (1) _preprocess_subtract_imagenet_mean. We simply return mean pixel value. - (2) _preprocess_zero_mean_unit_range. We return [127.5, 127.5, 127.5]. - The return values are used in a way that the padded regions after - pre-processing will contain value 0. - - Args: - model_variant: Model variant (string) for feature extraction. For - backwards compatibility, model_variant=None returns _MEAN_RGB. - - Returns: - Mean pixel value. - """ - if model_variant in ['resnet_v1_50', - 'resnet_v1_101'] or model_variant is None: - return _MEAN_RGB - else: - return [127.5, 127.5, 127.5] - - -def extract_features(images, - output_stride=8, - multi_grid=None, - depth_multiplier=1.0, - divisible_by=None, - final_endpoint=None, - model_variant=None, - weight_decay=0.0001, - reuse=None, - is_training=False, - fine_tune_batch_norm=False, - regularize_depthwise=False, - preprocess_images=True, - preprocessed_images_dtype=tf.float32, - num_classes=None, - global_pool=False, - nas_architecture_options=None, - nas_training_hyper_parameters=None, - use_bounded_activation=False): - """Extracts features by the particular model_variant. - - Args: - images: A tensor of size [batch, height, width, channels]. - output_stride: The ratio of input to output spatial resolution. - multi_grid: Employ a hierarchy of different atrous rates within network. - depth_multiplier: Float multiplier for the depth (number of channels) - for all convolution ops used in MobileNet. - divisible_by: None (use default setting) or an integer that ensures all - layers # channels will be divisible by this number. Used in MobileNet. - final_endpoint: The MobileNet endpoint to construct the network up to. - model_variant: Model variant for feature extraction. - weight_decay: The weight decay for model variables. - reuse: Reuse the model variables or not. - is_training: Is training or not. - fine_tune_batch_norm: Fine-tune the batch norm parameters or not. - regularize_depthwise: Whether or not apply L2-norm regularization on the - depthwise convolution weights. - preprocess_images: Performs preprocessing on images or not. Defaults to - True. Set to False if preprocessing will be done by other functions. We - supprot two types of preprocessing: (1) Mean pixel substraction and (2) - Pixel values normalization to be [-1, 1]. - preprocessed_images_dtype: The type after the preprocessing function. - num_classes: Number of classes for image classification task. Defaults - to None for dense prediction tasks. - global_pool: Global pooling for image classification task. Defaults to - False, since dense prediction tasks do not use this. - nas_architecture_options: A dictionary storing NAS architecture options. - It is either None or its kerys are: - - `nas_stem_output_num_conv_filters`: Number of filters of the NAS stem - output tensor. - - `nas_use_classification_head`: Boolean, use image classification head. - nas_training_hyper_parameters: A dictionary storing hyper-parameters for - training nas models. It is either None or its keys are: - - `drop_path_keep_prob`: Probability to keep each path in the cell when - training. - - `total_training_steps`: Total training steps to help drop path - probability calculation. - use_bounded_activation: Whether or not to use bounded activations. Bounded - activations better lend themselves to quantized inference. Currently, - bounded activation is only used in xception model. - - Returns: - features: A tensor of size [batch, feature_height, feature_width, - feature_channels], where feature_height/feature_width are determined - by the images height/width and output_stride. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: Unrecognized model variant. - """ - if 'resnet' in model_variant: - arg_scope = arg_scopes_map[model_variant]( - weight_decay=weight_decay, - batch_norm_decay=0.95, - batch_norm_epsilon=1e-5, - batch_norm_scale=True) - features, end_points = get_network( - model_variant, preprocess_images, preprocessed_images_dtype, arg_scope)( - inputs=images, - num_classes=num_classes, - is_training=(is_training and fine_tune_batch_norm), - global_pool=global_pool, - output_stride=output_stride, - multi_grid=multi_grid, - reuse=reuse, - scope=name_scope[model_variant]) - elif 'xception' in model_variant: - arg_scope = arg_scopes_map[model_variant]( - weight_decay=weight_decay, - batch_norm_decay=0.9997, - batch_norm_epsilon=1e-3, - batch_norm_scale=True, - regularize_depthwise=regularize_depthwise, - use_bounded_activation=use_bounded_activation) - features, end_points = get_network( - model_variant, preprocess_images, preprocessed_images_dtype, arg_scope)( - inputs=images, - num_classes=num_classes, - is_training=(is_training and fine_tune_batch_norm), - global_pool=global_pool, - output_stride=output_stride, - regularize_depthwise=regularize_depthwise, - multi_grid=multi_grid, - reuse=reuse, - scope=name_scope[model_variant]) - elif 'mobilenet' in model_variant or model_variant.startswith('mnas'): - arg_scope = arg_scopes_map[model_variant]( - is_training=(is_training and fine_tune_batch_norm), - weight_decay=weight_decay) - features, end_points = get_network( - model_variant, preprocess_images, preprocessed_images_dtype, arg_scope)( - inputs=images, - depth_multiplier=depth_multiplier, - divisible_by=divisible_by, - output_stride=output_stride, - reuse=reuse, - scope=name_scope[model_variant], - final_endpoint=final_endpoint) - elif model_variant.startswith('nas'): - arg_scope = arg_scopes_map[model_variant]( - weight_decay=weight_decay, - batch_norm_decay=0.9997, - batch_norm_epsilon=1e-3) - features, end_points = get_network( - model_variant, preprocess_images, preprocessed_images_dtype, arg_scope)( - inputs=images, - num_classes=num_classes, - is_training=(is_training and fine_tune_batch_norm), - global_pool=global_pool, - output_stride=output_stride, - nas_architecture_options=nas_architecture_options, - nas_training_hyper_parameters=nas_training_hyper_parameters, - reuse=reuse, - scope=name_scope[model_variant]) - else: - raise ValueError('Unknown model variant %s.' % model_variant) - - return features, end_points - - -def get_network(network_name, preprocess_images, - preprocessed_images_dtype=tf.float32, arg_scope=None): - """Gets the network. - - Args: - network_name: Network name. - preprocess_images: Preprocesses the images or not. - preprocessed_images_dtype: The type after the preprocessing function. - arg_scope: Optional, arg_scope to build the network. If not provided the - default arg_scope of the network would be used. - - Returns: - A network function that is used to extract features. - - Raises: - ValueError: network is not supported. - """ - if network_name not in networks_map: - raise ValueError('Unsupported network %s.' % network_name) - arg_scope = arg_scope or arg_scopes_map[network_name]() - def _identity_function(inputs, dtype=preprocessed_images_dtype): - return tf.cast(inputs, dtype=dtype) - if preprocess_images: - preprocess_function = _PREPROCESS_FN[network_name] - else: - preprocess_function = _identity_function - func = networks_map[network_name] - @functools.wraps(func) - def network_fn(inputs, *args, **kwargs): - with slim.arg_scope(arg_scope): - return func(preprocess_function(inputs, preprocessed_images_dtype), - *args, **kwargs) - return network_fn diff --git a/research/deeplab/core/nas_cell.py b/research/deeplab/core/nas_cell.py deleted file mode 100644 index d179082dc72..00000000000 --- a/research/deeplab/core/nas_cell.py +++ /dev/null @@ -1,221 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Cell structure used by NAS.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -from six.moves import range -from six.moves import zip -import tensorflow as tf -from tensorflow.contrib import framework as contrib_framework -from tensorflow.contrib import slim as contrib_slim -from deeplab.core import xception as xception_utils -from deeplab.core.utils import resize_bilinear -from deeplab.core.utils import scale_dimension -from tensorflow.contrib.slim.nets import resnet_utils - -arg_scope = contrib_framework.arg_scope -slim = contrib_slim - -separable_conv2d_same = functools.partial(xception_utils.separable_conv2d_same, - regularize_depthwise=True) - - -class NASBaseCell(object): - """NASNet Cell class that is used as a 'layer' in image architectures.""" - - def __init__(self, num_conv_filters, operations, used_hiddenstates, - hiddenstate_indices, drop_path_keep_prob, total_num_cells, - total_training_steps, batch_norm_fn=slim.batch_norm): - """Init function. - - For more details about NAS cell, see - https://arxiv.org/abs/1707.07012 and https://arxiv.org/abs/1712.00559. - - Args: - num_conv_filters: The number of filters for each convolution operation. - operations: List of operations that are performed in the NASNet Cell in - order. - used_hiddenstates: Binary array that signals if the hiddenstate was used - within the cell. This is used to determine what outputs of the cell - should be concatenated together. - hiddenstate_indices: Determines what hiddenstates should be combined - together with the specified operations to create the NASNet cell. - drop_path_keep_prob: Float, drop path keep probability. - total_num_cells: Integer, total number of cells. - total_training_steps: Integer, total training steps. - batch_norm_fn: Function, batch norm function. Defaults to - slim.batch_norm. - """ - if len(hiddenstate_indices) != len(operations): - raise ValueError( - 'Number of hiddenstate_indices and operations should be the same.') - if len(operations) % 2: - raise ValueError('Number of operations should be even.') - self._num_conv_filters = num_conv_filters - self._operations = operations - self._used_hiddenstates = used_hiddenstates - self._hiddenstate_indices = hiddenstate_indices - self._drop_path_keep_prob = drop_path_keep_prob - self._total_num_cells = total_num_cells - self._total_training_steps = total_training_steps - self._batch_norm_fn = batch_norm_fn - - def __call__(self, net, scope, filter_scaling, stride, prev_layer, cell_num): - """Runs the conv cell.""" - self._cell_num = cell_num - self._filter_scaling = filter_scaling - self._filter_size = int(self._num_conv_filters * filter_scaling) - - with tf.variable_scope(scope): - net = self._cell_base(net, prev_layer) - for i in range(len(self._operations) // 2): - with tf.variable_scope('comb_iter_{}'.format(i)): - h1 = net[self._hiddenstate_indices[i * 2]] - h2 = net[self._hiddenstate_indices[i * 2 + 1]] - with tf.variable_scope('left'): - h1 = self._apply_conv_operation( - h1, self._operations[i * 2], stride, - self._hiddenstate_indices[i * 2] < 2) - with tf.variable_scope('right'): - h2 = self._apply_conv_operation( - h2, self._operations[i * 2 + 1], stride, - self._hiddenstate_indices[i * 2 + 1] < 2) - with tf.variable_scope('combine'): - h = h1 + h2 - net.append(h) - - with tf.variable_scope('cell_output'): - net = self._combine_unused_states(net) - - return net - - def _cell_base(self, net, prev_layer): - """Runs the beginning of the conv cell before the chosen ops are run.""" - filter_size = self._filter_size - - if prev_layer is None: - prev_layer = net - else: - if net.shape[2] != prev_layer.shape[2]: - prev_layer = resize_bilinear( - prev_layer, tf.shape(net)[1:3], prev_layer.dtype) - if filter_size != prev_layer.shape[3]: - prev_layer = tf.nn.relu(prev_layer) - prev_layer = slim.conv2d(prev_layer, filter_size, 1, scope='prev_1x1') - prev_layer = self._batch_norm_fn(prev_layer, scope='prev_bn') - - net = tf.nn.relu(net) - net = slim.conv2d(net, filter_size, 1, scope='1x1') - net = self._batch_norm_fn(net, scope='beginning_bn') - net = tf.split(axis=3, num_or_size_splits=1, value=net) - net.append(prev_layer) - return net - - def _apply_conv_operation(self, net, operation, stride, - is_from_original_input): - """Applies the predicted conv operation to net.""" - if stride > 1 and not is_from_original_input: - stride = 1 - input_filters = net.shape[3] - filter_size = self._filter_size - if 'separable' in operation: - num_layers = int(operation.split('_')[-1]) - kernel_size = int(operation.split('x')[0][-1]) - for layer_num in range(num_layers): - net = tf.nn.relu(net) - net = separable_conv2d_same( - net, - filter_size, - kernel_size, - depth_multiplier=1, - scope='separable_{0}x{0}_{1}'.format(kernel_size, layer_num + 1), - stride=stride) - net = self._batch_norm_fn( - net, scope='bn_sep_{0}x{0}_{1}'.format(kernel_size, layer_num + 1)) - stride = 1 - elif 'atrous' in operation: - kernel_size = int(operation.split('x')[0][-1]) - net = tf.nn.relu(net) - if stride == 2: - scaled_height = scale_dimension(tf.shape(net)[1], 0.5) - scaled_width = scale_dimension(tf.shape(net)[2], 0.5) - net = resize_bilinear(net, [scaled_height, scaled_width], net.dtype) - net = resnet_utils.conv2d_same( - net, filter_size, kernel_size, rate=1, stride=1, - scope='atrous_{0}x{0}'.format(kernel_size)) - else: - net = resnet_utils.conv2d_same( - net, filter_size, kernel_size, rate=2, stride=1, - scope='atrous_{0}x{0}'.format(kernel_size)) - net = self._batch_norm_fn(net, scope='bn_atr_{0}x{0}'.format(kernel_size)) - elif operation in ['none']: - if stride > 1 or (input_filters != filter_size): - net = tf.nn.relu(net) - net = slim.conv2d(net, filter_size, 1, stride=stride, scope='1x1') - net = self._batch_norm_fn(net, scope='bn_1') - elif 'pool' in operation: - pooling_type = operation.split('_')[0] - pooling_shape = int(operation.split('_')[-1].split('x')[0]) - if pooling_type == 'avg': - net = slim.avg_pool2d(net, pooling_shape, stride=stride, padding='SAME') - elif pooling_type == 'max': - net = slim.max_pool2d(net, pooling_shape, stride=stride, padding='SAME') - else: - raise ValueError('Unimplemented pooling type: ', pooling_type) - if input_filters != filter_size: - net = slim.conv2d(net, filter_size, 1, stride=1, scope='1x1') - net = self._batch_norm_fn(net, scope='bn_1') - else: - raise ValueError('Unimplemented operation', operation) - - if operation != 'none': - net = self._apply_drop_path(net) - return net - - def _combine_unused_states(self, net): - """Concatenates the unused hidden states of the cell.""" - used_hiddenstates = self._used_hiddenstates - states_to_combine = ([ - h for h, is_used in zip(net, used_hiddenstates) if not is_used]) - net = tf.concat(values=states_to_combine, axis=3) - return net - - @contrib_framework.add_arg_scope - def _apply_drop_path(self, net): - """Apply drop_path regularization.""" - drop_path_keep_prob = self._drop_path_keep_prob - if drop_path_keep_prob < 1.0: - # Scale keep prob by layer number. - assert self._cell_num != -1 - layer_ratio = (self._cell_num + 1) / float(self._total_num_cells) - drop_path_keep_prob = 1 - layer_ratio * (1 - drop_path_keep_prob) - # Decrease keep prob over time. - current_step = tf.cast(tf.train.get_or_create_global_step(), tf.float32) - current_ratio = tf.minimum(1.0, current_step / self._total_training_steps) - drop_path_keep_prob = (1 - current_ratio * (1 - drop_path_keep_prob)) - # Drop path. - noise_shape = [tf.shape(net)[0], 1, 1, 1] - random_tensor = drop_path_keep_prob - random_tensor += tf.random_uniform(noise_shape, dtype=tf.float32) - binary_tensor = tf.cast(tf.floor(random_tensor), net.dtype) - keep_prob_inv = tf.cast(1.0 / drop_path_keep_prob, net.dtype) - net = net * keep_prob_inv * binary_tensor - return net diff --git a/research/deeplab/core/nas_genotypes.py b/research/deeplab/core/nas_genotypes.py deleted file mode 100644 index a2e6dd55b45..00000000000 --- a/research/deeplab/core/nas_genotypes.py +++ /dev/null @@ -1,45 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Genotypes used by NAS.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from tensorflow.contrib import slim as contrib_slim -from deeplab.core import nas_cell - -slim = contrib_slim - - -class PNASCell(nas_cell.NASBaseCell): - """Configuration and construction of the PNASNet-5 Cell.""" - - def __init__(self, num_conv_filters, drop_path_keep_prob, total_num_cells, - total_training_steps, batch_norm_fn=slim.batch_norm): - # Name of operations: op_kernel-size_num-layers. - operations = [ - 'separable_5x5_2', 'max_pool_3x3', 'separable_7x7_2', 'max_pool_3x3', - 'separable_5x5_2', 'separable_3x3_2', 'separable_3x3_2', 'max_pool_3x3', - 'separable_3x3_2', 'none' - ] - used_hiddenstates = [1, 1, 0, 0, 0, 0, 0] - hiddenstate_indices = [1, 1, 0, 0, 0, 0, 4, 0, 1, 0] - - super(PNASCell, self).__init__( - num_conv_filters, operations, used_hiddenstates, hiddenstate_indices, - drop_path_keep_prob, total_num_cells, total_training_steps, - batch_norm_fn) diff --git a/research/deeplab/core/nas_network.py b/research/deeplab/core/nas_network.py deleted file mode 100644 index 1da2e04dbaa..00000000000 --- a/research/deeplab/core/nas_network.py +++ /dev/null @@ -1,368 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Network structure used by NAS. - -Here we provide a few NAS backbones for semantic segmentation. -Currently, we have - -1. pnasnet -"Progressive Neural Architecture Search", Chenxi Liu, Barret Zoph, -Maxim Neumann, Jonathon Shlens, Wei Hua, Li-Jia Li, Li Fei-Fei, -Alan Yuille, Jonathan Huang, Kevin Murphy. In ECCV, 2018. - -2. hnasnet (also called Auto-DeepLab) -"Auto-DeepLab: Hierarchical Neural Architecture Search for Semantic -Image Segmentation", Chenxi Liu, Liang-Chieh Chen, Florian Schroff, -Hartwig Adam, Wei Hua, Alan Yuille, Li Fei-Fei. In CVPR, 2019. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow as tf -from tensorflow.contrib import framework as contrib_framework -from tensorflow.contrib import layers as contrib_layers -from tensorflow.contrib import slim as contrib_slim -from tensorflow.contrib import training as contrib_training - -from deeplab.core import nas_genotypes -from deeplab.core import utils -from deeplab.core.nas_cell import NASBaseCell -from tensorflow.contrib.slim.nets import resnet_utils - -arg_scope = contrib_framework.arg_scope -slim = contrib_slim -resize_bilinear = utils.resize_bilinear -scale_dimension = utils.scale_dimension - - -def config(num_conv_filters=20, - total_training_steps=500000, - drop_path_keep_prob=1.0): - return contrib_training.HParams( - # Multiplier when spatial size is reduced by 2. - filter_scaling_rate=2.0, - # Number of filters of the stem output tensor. - num_conv_filters=num_conv_filters, - # Probability to keep each path in the cell when training. - drop_path_keep_prob=drop_path_keep_prob, - # Total training steps to help drop path probability calculation. - total_training_steps=total_training_steps, - ) - - -def nas_arg_scope(weight_decay=4e-5, - batch_norm_decay=0.9997, - batch_norm_epsilon=0.001, - sync_batch_norm_method='None'): - """Default arg scope for the NAS models.""" - batch_norm_params = { - # Decay for the moving averages. - 'decay': batch_norm_decay, - # epsilon to prevent 0s in variance. - 'epsilon': batch_norm_epsilon, - 'scale': True, - } - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - weights_regularizer = contrib_layers.l2_regularizer(weight_decay) - weights_initializer = contrib_layers.variance_scaling_initializer( - factor=1 / 3.0, mode='FAN_IN', uniform=True) - with arg_scope([slim.fully_connected, slim.conv2d, slim.separable_conv2d], - weights_regularizer=weights_regularizer, - weights_initializer=weights_initializer): - with arg_scope([slim.fully_connected], - activation_fn=None, scope='FC'): - with arg_scope([slim.conv2d, slim.separable_conv2d], - activation_fn=None, biases_initializer=None): - with arg_scope([batch_norm], **batch_norm_params) as sc: - return sc - - -def _nas_stem(inputs, - batch_norm_fn=slim.batch_norm): - """Stem used for NAS models.""" - net = resnet_utils.conv2d_same(inputs, 64, 3, stride=2, scope='conv0') - net = batch_norm_fn(net, scope='conv0_bn') - net = tf.nn.relu(net) - net = resnet_utils.conv2d_same(net, 64, 3, stride=1, scope='conv1') - net = batch_norm_fn(net, scope='conv1_bn') - cell_outputs = [net] - net = tf.nn.relu(net) - net = resnet_utils.conv2d_same(net, 128, 3, stride=2, scope='conv2') - net = batch_norm_fn(net, scope='conv2_bn') - cell_outputs.append(net) - return net, cell_outputs - - -def _build_nas_base(images, - cell, - backbone, - num_classes, - hparams, - global_pool=False, - output_stride=16, - nas_use_classification_head=False, - reuse=None, - scope=None, - final_endpoint=None, - batch_norm_fn=slim.batch_norm, - nas_remove_os32_stride=False): - """Constructs a NAS model. - - Args: - images: A tensor of size [batch, height, width, channels]. - cell: Cell structure used in the network. - backbone: Backbone structure used in the network. A list of integers in - which value 0 means "output_stride=4", value 1 means "output_stride=8", - value 2 means "output_stride=16", and value 3 means "output_stride=32". - num_classes: Number of classes to predict. - hparams: Hyperparameters needed to construct the network. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: Interger, the stride of output feature maps. - nas_use_classification_head: Boolean, use image classification head. - reuse: Whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - final_endpoint: The endpoint to construct the network up to. - batch_norm_fn: Batch norm function. - nas_remove_os32_stride: Boolean, remove stride in output_stride 32 branch. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: If output_stride is not a multiple of backbone output stride. - """ - with tf.variable_scope(scope, 'nas', [images], reuse=reuse): - end_points = {} - def add_and_check_endpoint(endpoint_name, net): - end_points[endpoint_name] = net - return final_endpoint and (endpoint_name == final_endpoint) - - net, cell_outputs = _nas_stem(images, - batch_norm_fn=batch_norm_fn) - if add_and_check_endpoint('Stem', net): - return net, end_points - - # Run the cells - filter_scaling = 1.0 - for cell_num in range(len(backbone)): - stride = 1 - if cell_num == 0: - if backbone[0] == 1: - stride = 2 - filter_scaling *= hparams.filter_scaling_rate - else: - if backbone[cell_num] == backbone[cell_num - 1] + 1: - stride = 2 - if backbone[cell_num] == 3 and nas_remove_os32_stride: - stride = 1 - filter_scaling *= hparams.filter_scaling_rate - elif backbone[cell_num] == backbone[cell_num - 1] - 1: - if backbone[cell_num - 1] == 3 and nas_remove_os32_stride: - # No need to rescale features. - pass - else: - # Scale features by a factor of 2. - scaled_height = scale_dimension(net.shape[1].value, 2) - scaled_width = scale_dimension(net.shape[2].value, 2) - net = resize_bilinear(net, [scaled_height, scaled_width], net.dtype) - filter_scaling /= hparams.filter_scaling_rate - net = cell( - net, - scope='cell_{}'.format(cell_num), - filter_scaling=filter_scaling, - stride=stride, - prev_layer=cell_outputs[-2], - cell_num=cell_num) - if add_and_check_endpoint('Cell_{}'.format(cell_num), net): - return net, end_points - cell_outputs.append(net) - net = tf.nn.relu(net) - - if nas_use_classification_head: - # Add image classification head. - # We will expand the filters for different output_strides. - output_stride_to_expanded_filters = {8: 256, 16: 512, 32: 1024} - current_output_scale = 2 + backbone[-1] - current_output_stride = 2 ** current_output_scale - if output_stride % current_output_stride != 0: - raise ValueError( - 'output_stride must be a multiple of backbone output stride.') - output_stride //= current_output_stride - rate = 1 - if current_output_stride != 32: - num_downsampling = 5 - current_output_scale - for i in range(num_downsampling): - # Gradually donwsample feature maps to output stride = 32. - target_output_stride = 2 ** (current_output_scale + 1 + i) - target_filters = output_stride_to_expanded_filters[ - target_output_stride] - scope = 'downsample_os{}'.format(target_output_stride) - if output_stride != 1: - stride = 2 - output_stride //= 2 - else: - stride = 1 - rate *= 2 - net = resnet_utils.conv2d_same( - net, target_filters, 3, stride=stride, rate=rate, - scope=scope + '_conv') - net = batch_norm_fn(net, scope=scope + '_bn') - add_and_check_endpoint(scope, net) - net = tf.nn.relu(net) - # Apply 1x1 convolution to expand dimension to 2048. - scope = 'classification_head' - net = slim.conv2d(net, 2048, 1, scope=scope + '_conv') - net = batch_norm_fn(net, scope=scope + '_bn') - add_and_check_endpoint(scope, net) - net = tf.nn.relu(net) - if global_pool: - # Global average pooling. - net = tf.reduce_mean(net, [1, 2], name='global_pool', keepdims=True) - if num_classes is not None: - net = slim.conv2d(net, num_classes, 1, activation_fn=None, - normalizer_fn=None, scope='logits') - end_points['predictions'] = slim.softmax(net, scope='predictions') - return net, end_points - - -def pnasnet(images, - num_classes, - is_training=True, - global_pool=False, - output_stride=16, - nas_architecture_options=None, - nas_training_hyper_parameters=None, - reuse=None, - scope='pnasnet', - final_endpoint=None, - sync_batch_norm_method='None'): - """Builds PNASNet model.""" - if nas_architecture_options is None: - raise ValueError( - 'Using NAS model variants. nas_architecture_options cannot be None.') - hparams = config(num_conv_filters=nas_architecture_options[ - 'nas_stem_output_num_conv_filters']) - if nas_training_hyper_parameters: - hparams.set_hparam('drop_path_keep_prob', - nas_training_hyper_parameters['drop_path_keep_prob']) - hparams.set_hparam('total_training_steps', - nas_training_hyper_parameters['total_training_steps']) - if not is_training: - tf.logging.info('During inference, setting drop_path_keep_prob = 1.0.') - hparams.set_hparam('drop_path_keep_prob', 1.0) - tf.logging.info(hparams) - if output_stride == 8: - backbone = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] - elif output_stride == 16: - backbone = [1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2] - elif output_stride == 32: - backbone = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3] - else: - raise ValueError('Unsupported output_stride ', output_stride) - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - cell = nas_genotypes.PNASCell(hparams.num_conv_filters, - hparams.drop_path_keep_prob, - len(backbone), - hparams.total_training_steps, - batch_norm_fn=batch_norm) - with arg_scope([slim.dropout, batch_norm], is_training=is_training): - return _build_nas_base( - images, - cell=cell, - backbone=backbone, - num_classes=num_classes, - hparams=hparams, - global_pool=global_pool, - output_stride=output_stride, - nas_use_classification_head=nas_architecture_options[ - 'nas_use_classification_head'], - reuse=reuse, - scope=scope, - final_endpoint=final_endpoint, - batch_norm_fn=batch_norm, - nas_remove_os32_stride=nas_architecture_options[ - 'nas_remove_os32_stride']) - - -# pylint: disable=unused-argument -def hnasnet(images, - num_classes, - is_training=True, - global_pool=False, - output_stride=8, - nas_architecture_options=None, - nas_training_hyper_parameters=None, - reuse=None, - scope='hnasnet', - final_endpoint=None, - sync_batch_norm_method='None'): - """Builds hierarchical model.""" - if nas_architecture_options is None: - raise ValueError( - 'Using NAS model variants. nas_architecture_options cannot be None.') - hparams = config(num_conv_filters=nas_architecture_options[ - 'nas_stem_output_num_conv_filters']) - if nas_training_hyper_parameters: - hparams.set_hparam('drop_path_keep_prob', - nas_training_hyper_parameters['drop_path_keep_prob']) - hparams.set_hparam('total_training_steps', - nas_training_hyper_parameters['total_training_steps']) - if not is_training: - tf.logging.info('During inference, setting drop_path_keep_prob = 1.0.') - hparams.set_hparam('drop_path_keep_prob', 1.0) - tf.logging.info(hparams) - operations = [ - 'atrous_5x5', 'separable_3x3_2', 'separable_3x3_2', 'atrous_3x3', - 'separable_3x3_2', 'separable_3x3_2', 'separable_5x5_2', - 'separable_5x5_2', 'separable_5x5_2', 'atrous_5x5' - ] - used_hiddenstates = [1, 1, 0, 0, 0, 0, 0] - hiddenstate_indices = [1, 0, 1, 0, 3, 1, 4, 2, 3, 5] - backbone = [0, 0, 0, 1, 2, 1, 2, 2, 3, 3, 2, 1] - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - cell = NASBaseCell(hparams.num_conv_filters, - operations, - used_hiddenstates, - hiddenstate_indices, - hparams.drop_path_keep_prob, - len(backbone), - hparams.total_training_steps, - batch_norm_fn=batch_norm) - with arg_scope([slim.dropout, batch_norm], is_training=is_training): - return _build_nas_base( - images, - cell=cell, - backbone=backbone, - num_classes=num_classes, - hparams=hparams, - global_pool=global_pool, - output_stride=output_stride, - nas_use_classification_head=nas_architecture_options[ - 'nas_use_classification_head'], - reuse=reuse, - scope=scope, - final_endpoint=final_endpoint, - batch_norm_fn=batch_norm, - nas_remove_os32_stride=nas_architecture_options[ - 'nas_remove_os32_stride']) diff --git a/research/deeplab/core/nas_network_test.py b/research/deeplab/core/nas_network_test.py deleted file mode 100644 index 18621b250ad..00000000000 --- a/research/deeplab/core/nas_network_test.py +++ /dev/null @@ -1,111 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for resnet_v1_beta module.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf -from tensorflow.contrib import framework as contrib_framework -from tensorflow.contrib import slim as contrib_slim -from tensorflow.contrib import training as contrib_training - -from deeplab.core import nas_genotypes -from deeplab.core import nas_network - -arg_scope = contrib_framework.arg_scope -slim = contrib_slim - - -def create_test_input(batch, height, width, channels): - """Creates test input tensor.""" - if None in [batch, height, width, channels]: - return tf.placeholder(tf.float32, (batch, height, width, channels)) - else: - return tf.to_float( - np.tile( - np.reshape( - np.reshape(np.arange(height), [height, 1]) + - np.reshape(np.arange(width), [1, width]), - [1, height, width, 1]), - [batch, 1, 1, channels])) - - -class NASNetworkTest(tf.test.TestCase): - """Tests with complete small NAS networks.""" - - def _pnasnet(self, - images, - backbone, - num_classes, - is_training=True, - output_stride=16, - final_endpoint=None): - """Build PNASNet model backbone.""" - hparams = contrib_training.HParams( - filter_scaling_rate=2.0, - num_conv_filters=10, - drop_path_keep_prob=1.0, - total_training_steps=200000, - ) - if not is_training: - hparams.set_hparam('drop_path_keep_prob', 1.0) - - cell = nas_genotypes.PNASCell(hparams.num_conv_filters, - hparams.drop_path_keep_prob, - len(backbone), - hparams.total_training_steps) - with arg_scope([slim.dropout, slim.batch_norm], is_training=is_training): - return nas_network._build_nas_base( - images, - cell=cell, - backbone=backbone, - num_classes=num_classes, - hparams=hparams, - reuse=tf.AUTO_REUSE, - scope='pnasnet_small', - final_endpoint=final_endpoint) - - def testFullyConvolutionalEndpointShapes(self): - num_classes = 10 - backbone = [0, 0, 0, 1, 2, 1, 2, 2, 3, 3, 2, 1] - inputs = create_test_input(None, 321, 321, 3) - with slim.arg_scope(nas_network.nas_arg_scope()): - _, end_points = self._pnasnet(inputs, backbone, num_classes) - endpoint_to_shape = { - 'Stem': [None, 81, 81, 128], - 'Cell_0': [None, 81, 81, 50], - 'Cell_1': [None, 81, 81, 50], - 'Cell_2': [None, 81, 81, 50], - 'Cell_3': [None, 41, 41, 100], - 'Cell_4': [None, 21, 21, 200], - 'Cell_5': [None, 41, 41, 100], - 'Cell_6': [None, 21, 21, 200], - 'Cell_7': [None, 21, 21, 200], - 'Cell_8': [None, 11, 11, 400], - 'Cell_9': [None, 11, 11, 400], - 'Cell_10': [None, 21, 21, 200], - 'Cell_11': [None, 41, 41, 100] - } - for endpoint, shape in endpoint_to_shape.items(): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/core/preprocess_utils.py b/research/deeplab/core/preprocess_utils.py deleted file mode 100644 index 440717e414d..00000000000 --- a/research/deeplab/core/preprocess_utils.py +++ /dev/null @@ -1,533 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Utility functions related to preprocessing inputs.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from six.moves import range -from six.moves import zip -import tensorflow as tf - - -def flip_dim(tensor_list, prob=0.5, dim=1): - """Randomly flips a dimension of the given tensor. - - The decision to randomly flip the `Tensors` is made together. In other words, - all or none of the images pass in are flipped. - - Note that tf.random_flip_left_right and tf.random_flip_up_down isn't used so - that we can control for the probability as well as ensure the same decision - is applied across the images. - - Args: - tensor_list: A list of `Tensors` with the same number of dimensions. - prob: The probability of a left-right flip. - dim: The dimension to flip, 0, 1, .. - - Returns: - outputs: A list of the possibly flipped `Tensors` as well as an indicator - `Tensor` at the end whose value is `True` if the inputs were flipped and - `False` otherwise. - - Raises: - ValueError: If dim is negative or greater than the dimension of a `Tensor`. - """ - random_value = tf.random_uniform([]) - - def flip(): - flipped = [] - for tensor in tensor_list: - if dim < 0 or dim >= len(tensor.get_shape().as_list()): - raise ValueError('dim must represent a valid dimension.') - flipped.append(tf.reverse_v2(tensor, [dim])) - return flipped - - is_flipped = tf.less_equal(random_value, prob) - outputs = tf.cond(is_flipped, flip, lambda: tensor_list) - if not isinstance(outputs, (list, tuple)): - outputs = [outputs] - outputs.append(is_flipped) - - return outputs - - -def _image_dimensions(image, rank): - """Returns the dimensions of an image tensor. - - Args: - image: A rank-D Tensor. For 3-D of shape: `[height, width, channels]`. - rank: The expected rank of the image - - Returns: - A list of corresponding to the dimensions of the input image. Dimensions - that are statically known are python integers, otherwise they are integer - scalar tensors. - """ - if image.get_shape().is_fully_defined(): - return image.get_shape().as_list() - else: - static_shape = image.get_shape().with_rank(rank).as_list() - dynamic_shape = tf.unstack(tf.shape(image), rank) - return [ - s if s is not None else d for s, d in zip(static_shape, dynamic_shape) - ] - - -def get_label_resize_method(label): - """Returns the resize method of labels depending on label dtype. - - Args: - label: Groundtruth label tensor. - - Returns: - tf.image.ResizeMethod.BILINEAR, if label dtype is floating. - tf.image.ResizeMethod.NEAREST_NEIGHBOR, if label dtype is integer. - - Raises: - ValueError: If label is neither floating nor integer. - """ - if label.dtype.is_floating: - return tf.image.ResizeMethod.BILINEAR - elif label.dtype.is_integer: - return tf.image.ResizeMethod.NEAREST_NEIGHBOR - else: - raise ValueError('Label type must be either floating or integer.') - - -def pad_to_bounding_box(image, offset_height, offset_width, target_height, - target_width, pad_value): - """Pads the given image with the given pad_value. - - Works like tf.image.pad_to_bounding_box, except it can pad the image - with any given arbitrary pad value and also handle images whose sizes are not - known during graph construction. - - Args: - image: 3-D tensor with shape [height, width, channels] - offset_height: Number of rows of zeros to add on top. - offset_width: Number of columns of zeros to add on the left. - target_height: Height of output image. - target_width: Width of output image. - pad_value: Value to pad the image tensor with. - - Returns: - 3-D tensor of shape [target_height, target_width, channels]. - - Raises: - ValueError: If the shape of image is incompatible with the offset_* or - target_* arguments. - """ - with tf.name_scope(None, 'pad_to_bounding_box', [image]): - image = tf.convert_to_tensor(image, name='image') - original_dtype = image.dtype - if original_dtype != tf.float32 and original_dtype != tf.float64: - # If image dtype is not float, we convert it to int32 to avoid overflow. - image = tf.cast(image, tf.int32) - image_rank_assert = tf.Assert( - tf.logical_or( - tf.equal(tf.rank(image), 3), - tf.equal(tf.rank(image), 4)), - ['Wrong image tensor rank.']) - with tf.control_dependencies([image_rank_assert]): - image -= pad_value - image_shape = image.get_shape() - is_batch = True - if image_shape.ndims == 3: - is_batch = False - image = tf.expand_dims(image, 0) - elif image_shape.ndims is None: - is_batch = False - image = tf.expand_dims(image, 0) - image.set_shape([None] * 4) - elif image.get_shape().ndims != 4: - raise ValueError('Input image must have either 3 or 4 dimensions.') - _, height, width, _ = _image_dimensions(image, rank=4) - target_width_assert = tf.Assert( - tf.greater_equal( - target_width, width), - ['target_width must be >= width']) - target_height_assert = tf.Assert( - tf.greater_equal(target_height, height), - ['target_height must be >= height']) - with tf.control_dependencies([target_width_assert]): - after_padding_width = target_width - offset_width - width - with tf.control_dependencies([target_height_assert]): - after_padding_height = target_height - offset_height - height - offset_assert = tf.Assert( - tf.logical_and( - tf.greater_equal(after_padding_width, 0), - tf.greater_equal(after_padding_height, 0)), - ['target size not possible with the given target offsets']) - batch_params = tf.stack([0, 0]) - height_params = tf.stack([offset_height, after_padding_height]) - width_params = tf.stack([offset_width, after_padding_width]) - channel_params = tf.stack([0, 0]) - with tf.control_dependencies([offset_assert]): - paddings = tf.stack([batch_params, height_params, width_params, - channel_params]) - padded = tf.pad(image, paddings) - if not is_batch: - padded = tf.squeeze(padded, axis=[0]) - outputs = padded + pad_value - if outputs.dtype != original_dtype: - outputs = tf.cast(outputs, original_dtype) - return outputs - - -def _crop(image, offset_height, offset_width, crop_height, crop_width): - """Crops the given image using the provided offsets and sizes. - - Note that the method doesn't assume we know the input image size but it does - assume we know the input image rank. - - Args: - image: an image of shape [height, width, channels]. - offset_height: a scalar tensor indicating the height offset. - offset_width: a scalar tensor indicating the width offset. - crop_height: the height of the cropped image. - crop_width: the width of the cropped image. - - Returns: - The cropped (and resized) image. - - Raises: - ValueError: if `image` doesn't have rank of 3. - InvalidArgumentError: if the rank is not 3 or if the image dimensions are - less than the crop size. - """ - original_shape = tf.shape(image) - - if len(image.get_shape().as_list()) != 3: - raise ValueError('input must have rank of 3') - original_channels = image.get_shape().as_list()[2] - - rank_assertion = tf.Assert( - tf.equal(tf.rank(image), 3), - ['Rank of image must be equal to 3.']) - with tf.control_dependencies([rank_assertion]): - cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]]) - - size_assertion = tf.Assert( - tf.logical_and( - tf.greater_equal(original_shape[0], crop_height), - tf.greater_equal(original_shape[1], crop_width)), - ['Crop size greater than the image size.']) - - offsets = tf.cast(tf.stack([offset_height, offset_width, 0]), tf.int32) - - # Use tf.slice instead of crop_to_bounding box as it accepts tensors to - # define the crop size. - with tf.control_dependencies([size_assertion]): - image = tf.slice(image, offsets, cropped_shape) - image = tf.reshape(image, cropped_shape) - image.set_shape([crop_height, crop_width, original_channels]) - return image - - -def random_crop(image_list, crop_height, crop_width): - """Crops the given list of images. - - The function applies the same crop to each image in the list. This can be - effectively applied when there are multiple image inputs of the same - dimension such as: - - image, depths, normals = random_crop([image, depths, normals], 120, 150) - - Args: - image_list: a list of image tensors of the same dimension but possibly - varying channel. - crop_height: the new height. - crop_width: the new width. - - Returns: - the image_list with cropped images. - - Raises: - ValueError: if there are multiple image inputs provided with different size - or the images are smaller than the crop dimensions. - """ - if not image_list: - raise ValueError('Empty image_list.') - - # Compute the rank assertions. - rank_assertions = [] - for i in range(len(image_list)): - image_rank = tf.rank(image_list[i]) - rank_assert = tf.Assert( - tf.equal(image_rank, 3), - ['Wrong rank for tensor %s [expected] [actual]', - image_list[i].name, 3, image_rank]) - rank_assertions.append(rank_assert) - - with tf.control_dependencies([rank_assertions[0]]): - image_shape = tf.shape(image_list[0]) - image_height = image_shape[0] - image_width = image_shape[1] - crop_size_assert = tf.Assert( - tf.logical_and( - tf.greater_equal(image_height, crop_height), - tf.greater_equal(image_width, crop_width)), - ['Crop size greater than the image size.']) - - asserts = [rank_assertions[0], crop_size_assert] - - for i in range(1, len(image_list)): - image = image_list[i] - asserts.append(rank_assertions[i]) - with tf.control_dependencies([rank_assertions[i]]): - shape = tf.shape(image) - height = shape[0] - width = shape[1] - - height_assert = tf.Assert( - tf.equal(height, image_height), - ['Wrong height for tensor %s [expected][actual]', - image.name, height, image_height]) - width_assert = tf.Assert( - tf.equal(width, image_width), - ['Wrong width for tensor %s [expected][actual]', - image.name, width, image_width]) - asserts.extend([height_assert, width_assert]) - - # Create a random bounding box. - # - # Use tf.random_uniform and not numpy.random.rand as doing the former would - # generate random numbers at graph eval time, unlike the latter which - # generates random numbers at graph definition time. - with tf.control_dependencies(asserts): - max_offset_height = tf.reshape(image_height - crop_height + 1, []) - max_offset_width = tf.reshape(image_width - crop_width + 1, []) - offset_height = tf.random_uniform( - [], maxval=max_offset_height, dtype=tf.int32) - offset_width = tf.random_uniform( - [], maxval=max_offset_width, dtype=tf.int32) - - return [_crop(image, offset_height, offset_width, - crop_height, crop_width) for image in image_list] - - -def get_random_scale(min_scale_factor, max_scale_factor, step_size): - """Gets a random scale value. - - Args: - min_scale_factor: Minimum scale value. - max_scale_factor: Maximum scale value. - step_size: The step size from minimum to maximum value. - - Returns: - A random scale value selected between minimum and maximum value. - - Raises: - ValueError: min_scale_factor has unexpected value. - """ - if min_scale_factor < 0 or min_scale_factor > max_scale_factor: - raise ValueError('Unexpected value of min_scale_factor.') - - if min_scale_factor == max_scale_factor: - return tf.cast(min_scale_factor, tf.float32) - - # When step_size = 0, we sample the value uniformly from [min, max). - if step_size == 0: - return tf.random_uniform([1], - minval=min_scale_factor, - maxval=max_scale_factor) - - # When step_size != 0, we randomly select one discrete value from [min, max]. - num_steps = int((max_scale_factor - min_scale_factor) / step_size + 1) - scale_factors = tf.lin_space(min_scale_factor, max_scale_factor, num_steps) - shuffled_scale_factors = tf.random_shuffle(scale_factors) - return shuffled_scale_factors[0] - - -def randomly_scale_image_and_label(image, label=None, scale=1.0): - """Randomly scales image and label. - - Args: - image: Image with shape [height, width, 3]. - label: Label with shape [height, width, 1]. - scale: The value to scale image and label. - - Returns: - Scaled image and label. - """ - # No random scaling if scale == 1. - if scale == 1.0: - return image, label - image_shape = tf.shape(image) - new_dim = tf.cast( - tf.cast([image_shape[0], image_shape[1]], tf.float32) * scale, - tf.int32) - - # Need squeeze and expand_dims because image interpolation takes - # 4D tensors as input. - image = tf.squeeze(tf.image.resize_bilinear( - tf.expand_dims(image, 0), - new_dim, - align_corners=True), [0]) - if label is not None: - label = tf.image.resize( - label, - new_dim, - method=get_label_resize_method(label), - align_corners=True) - - return image, label - - -def resolve_shape(tensor, rank=None, scope=None): - """Fully resolves the shape of a Tensor. - - Use as much as possible the shape components already known during graph - creation and resolve the remaining ones during runtime. - - Args: - tensor: Input tensor whose shape we query. - rank: The rank of the tensor, provided that we know it. - scope: Optional name scope. - - Returns: - shape: The full shape of the tensor. - """ - with tf.name_scope(scope, 'resolve_shape', [tensor]): - if rank is not None: - shape = tensor.get_shape().with_rank(rank).as_list() - else: - shape = tensor.get_shape().as_list() - - if None in shape: - shape_dynamic = tf.shape(tensor) - for i in range(len(shape)): - if shape[i] is None: - shape[i] = shape_dynamic[i] - - return shape - - -def resize_to_range(image, - label=None, - min_size=None, - max_size=None, - factor=None, - keep_aspect_ratio=True, - align_corners=True, - label_layout_is_chw=False, - scope=None, - method=tf.image.ResizeMethod.BILINEAR): - """Resizes image or label so their sides are within the provided range. - - The output size can be described by two cases: - 1. If the image can be rescaled so its minimum size is equal to min_size - without the other side exceeding max_size, then do so. - 2. Otherwise, resize so the largest side is equal to max_size. - - An integer in `range(factor)` is added to the computed sides so that the - final dimensions are multiples of `factor` plus one. - - Args: - image: A 3D tensor of shape [height, width, channels]. - label: (optional) A 3D tensor of shape [height, width, channels] (default) - or [channels, height, width] when label_layout_is_chw = True. - min_size: (scalar) desired size of the smaller image side. - max_size: (scalar) maximum allowed size of the larger image side. Note - that the output dimension is no larger than max_size and may be slightly - smaller than max_size when factor is not None. - factor: Make output size multiple of factor plus one. - keep_aspect_ratio: Boolean, keep aspect ratio or not. If True, the input - will be resized while keeping the original aspect ratio. If False, the - input will be resized to [max_resize_value, max_resize_value] without - keeping the original aspect ratio. - align_corners: If True, exactly align all 4 corners of input and output. - label_layout_is_chw: If true, the label has shape [channel, height, width]. - We support this case because for some instance segmentation dataset, the - instance segmentation is saved as [num_instances, height, width]. - scope: Optional name scope. - method: Image resize method. Defaults to tf.image.ResizeMethod.BILINEAR. - - Returns: - A 3-D tensor of shape [new_height, new_width, channels], where the image - has been resized (with the specified method) so that - min(new_height, new_width) == ceil(min_size) or - max(new_height, new_width) == ceil(max_size). - - Raises: - ValueError: If the image is not a 3D tensor. - """ - with tf.name_scope(scope, 'resize_to_range', [image]): - new_tensor_list = [] - min_size = tf.cast(min_size, tf.float32) - if max_size is not None: - max_size = tf.cast(max_size, tf.float32) - # Modify the max_size to be a multiple of factor plus 1 and make sure the - # max dimension after resizing is no larger than max_size. - if factor is not None: - max_size = (max_size - (max_size - 1) % factor) - - [orig_height, orig_width, _] = resolve_shape(image, rank=3) - orig_height = tf.cast(orig_height, tf.float32) - orig_width = tf.cast(orig_width, tf.float32) - orig_min_size = tf.minimum(orig_height, orig_width) - - # Calculate the larger of the possible sizes - large_scale_factor = min_size / orig_min_size - large_height = tf.cast(tf.floor(orig_height * large_scale_factor), tf.int32) - large_width = tf.cast(tf.floor(orig_width * large_scale_factor), tf.int32) - large_size = tf.stack([large_height, large_width]) - - new_size = large_size - if max_size is not None: - # Calculate the smaller of the possible sizes, use that if the larger - # is too big. - orig_max_size = tf.maximum(orig_height, orig_width) - small_scale_factor = max_size / orig_max_size - small_height = tf.cast( - tf.floor(orig_height * small_scale_factor), tf.int32) - small_width = tf.cast(tf.floor(orig_width * small_scale_factor), tf.int32) - small_size = tf.stack([small_height, small_width]) - new_size = tf.cond( - tf.cast(tf.reduce_max(large_size), tf.float32) > max_size, - lambda: small_size, - lambda: large_size) - # Ensure that both output sides are multiples of factor plus one. - if factor is not None: - new_size += (factor - (new_size - 1) % factor) % factor - if not keep_aspect_ratio: - # If not keep the aspect ratio, we resize everything to max_size, allowing - # us to do pre-processing without extra padding. - new_size = [tf.reduce_max(new_size), tf.reduce_max(new_size)] - new_tensor_list.append(tf.image.resize( - image, new_size, method=method, align_corners=align_corners)) - if label is not None: - if label_layout_is_chw: - # Input label has shape [channel, height, width]. - resized_label = tf.expand_dims(label, 3) - resized_label = tf.image.resize( - resized_label, - new_size, - method=get_label_resize_method(label), - align_corners=align_corners) - resized_label = tf.squeeze(resized_label, 3) - else: - # Input label has shape [height, width, channel]. - resized_label = tf.image.resize( - label, - new_size, - method=get_label_resize_method(label), - align_corners=align_corners) - new_tensor_list.append(resized_label) - else: - new_tensor_list.append(None) - return new_tensor_list diff --git a/research/deeplab/core/preprocess_utils_test.py b/research/deeplab/core/preprocess_utils_test.py deleted file mode 100644 index 606fe46dd62..00000000000 --- a/research/deeplab/core/preprocess_utils_test.py +++ /dev/null @@ -1,515 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for preprocess_utils.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -from six.moves import range -import tensorflow as tf - -from deeplab.core import preprocess_utils - - -class PreprocessUtilsTest(tf.test.TestCase): - - def testNoFlipWhenProbIsZero(self): - numpy_image = np.dstack([[[5., 6.], - [9., 0.]], - [[4., 3.], - [3., 5.]]]) - image = tf.convert_to_tensor(numpy_image) - - with self.test_session(): - actual, is_flipped = preprocess_utils.flip_dim([image], prob=0, dim=0) - self.assertAllEqual(numpy_image, actual.eval()) - self.assertAllEqual(False, is_flipped.eval()) - actual, is_flipped = preprocess_utils.flip_dim([image], prob=0, dim=1) - self.assertAllEqual(numpy_image, actual.eval()) - self.assertAllEqual(False, is_flipped.eval()) - actual, is_flipped = preprocess_utils.flip_dim([image], prob=0, dim=2) - self.assertAllEqual(numpy_image, actual.eval()) - self.assertAllEqual(False, is_flipped.eval()) - - def testFlipWhenProbIsOne(self): - numpy_image = np.dstack([[[5., 6.], - [9., 0.]], - [[4., 3.], - [3., 5.]]]) - dim0_flipped = np.dstack([[[9., 0.], - [5., 6.]], - [[3., 5.], - [4., 3.]]]) - dim1_flipped = np.dstack([[[6., 5.], - [0., 9.]], - [[3., 4.], - [5., 3.]]]) - dim2_flipped = np.dstack([[[4., 3.], - [3., 5.]], - [[5., 6.], - [9., 0.]]]) - image = tf.convert_to_tensor(numpy_image) - - with self.test_session(): - actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=0) - self.assertAllEqual(dim0_flipped, actual.eval()) - self.assertAllEqual(True, is_flipped.eval()) - actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=1) - self.assertAllEqual(dim1_flipped, actual.eval()) - self.assertAllEqual(True, is_flipped.eval()) - actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=2) - self.assertAllEqual(dim2_flipped, actual.eval()) - self.assertAllEqual(True, is_flipped.eval()) - - def testFlipMultipleImagesConsistentlyWhenProbIsOne(self): - numpy_image = np.dstack([[[5., 6.], - [9., 0.]], - [[4., 3.], - [3., 5.]]]) - numpy_label = np.dstack([[[0., 1.], - [2., 3.]]]) - image_dim1_flipped = np.dstack([[[6., 5.], - [0., 9.]], - [[3., 4.], - [5., 3.]]]) - label_dim1_flipped = np.dstack([[[1., 0.], - [3., 2.]]]) - image = tf.convert_to_tensor(numpy_image) - label = tf.convert_to_tensor(numpy_label) - - with self.test_session() as sess: - image, label, is_flipped = preprocess_utils.flip_dim( - [image, label], prob=1, dim=1) - actual_image, actual_label = sess.run([image, label]) - self.assertAllEqual(image_dim1_flipped, actual_image) - self.assertAllEqual(label_dim1_flipped, actual_label) - self.assertEqual(True, is_flipped.eval()) - - def testReturnRandomFlipsOnMultipleEvals(self): - numpy_image = np.dstack([[[5., 6.], - [9., 0.]], - [[4., 3.], - [3., 5.]]]) - dim1_flipped = np.dstack([[[6., 5.], - [0., 9.]], - [[3., 4.], - [5., 3.]]]) - image = tf.convert_to_tensor(numpy_image) - tf.compat.v1.set_random_seed(53) - - with self.test_session() as sess: - actual, is_flipped = preprocess_utils.flip_dim( - [image], prob=0.5, dim=1) - actual_image, actual_is_flipped = sess.run([actual, is_flipped]) - self.assertAllEqual(numpy_image, actual_image) - self.assertEqual(False, actual_is_flipped) - actual_image, actual_is_flipped = sess.run([actual, is_flipped]) - self.assertAllEqual(dim1_flipped, actual_image) - self.assertEqual(True, actual_is_flipped) - - def testReturnCorrectCropOfSingleImage(self): - np.random.seed(0) - - height, width = 10, 20 - image = np.random.randint(0, 256, size=(height, width, 3)) - - crop_height, crop_width = 2, 4 - - image_placeholder = tf.placeholder(tf.int32, shape=(None, None, 3)) - [cropped] = preprocess_utils.random_crop([image_placeholder], - crop_height, - crop_width) - - with self.test_session(): - cropped_image = cropped.eval(feed_dict={image_placeholder: image}) - - # Ensure we can find the cropped image in the original: - is_found = False - for x in range(0, width - crop_width + 1): - for y in range(0, height - crop_height + 1): - if np.isclose(image[y:y+crop_height, x:x+crop_width, :], - cropped_image).all(): - is_found = True - break - - self.assertTrue(is_found) - - def testRandomCropMaintainsNumberOfChannels(self): - np.random.seed(0) - - crop_height, crop_width = 10, 20 - image = np.random.randint(0, 256, size=(100, 200, 3)) - - tf.compat.v1.set_random_seed(37) - image_placeholder = tf.placeholder(tf.int32, shape=(None, None, 3)) - [cropped] = preprocess_utils.random_crop( - [image_placeholder], crop_height, crop_width) - - with self.test_session(): - cropped_image = cropped.eval(feed_dict={image_placeholder: image}) - self.assertTupleEqual(cropped_image.shape, (crop_height, crop_width, 3)) - - def testReturnDifferentCropAreasOnTwoEvals(self): - tf.compat.v1.set_random_seed(0) - - crop_height, crop_width = 2, 3 - image = np.random.randint(0, 256, size=(100, 200, 3)) - image_placeholder = tf.placeholder(tf.int32, shape=(None, None, 3)) - [cropped] = preprocess_utils.random_crop( - [image_placeholder], crop_height, crop_width) - - with self.test_session(): - crop0 = cropped.eval(feed_dict={image_placeholder: image}) - crop1 = cropped.eval(feed_dict={image_placeholder: image}) - self.assertFalse(np.isclose(crop0, crop1).all()) - - def testReturnConsistenCropsOfImagesInTheList(self): - tf.compat.v1.set_random_seed(0) - - height, width = 10, 20 - crop_height, crop_width = 2, 3 - labels = np.linspace(0, height * width-1, height * width) - labels = labels.reshape((height, width, 1)) - image = np.tile(labels, (1, 1, 3)) - - image_placeholder = tf.placeholder(tf.int32, shape=(None, None, 3)) - label_placeholder = tf.placeholder(tf.int32, shape=(None, None, 1)) - [cropped_image, cropped_label] = preprocess_utils.random_crop( - [image_placeholder, label_placeholder], crop_height, crop_width) - - with self.test_session() as sess: - cropped_image, cropped_labels = sess.run([cropped_image, cropped_label], - feed_dict={ - image_placeholder: image, - label_placeholder: labels}) - for i in range(3): - self.assertAllEqual(cropped_image[:, :, i], cropped_labels.squeeze()) - - def testDieOnRandomCropWhenImagesWithDifferentWidth(self): - crop_height, crop_width = 2, 3 - image1 = tf.placeholder(tf.float32, name='image1', shape=(None, None, 3)) - image2 = tf.placeholder(tf.float32, name='image2', shape=(None, None, 1)) - cropped = preprocess_utils.random_crop( - [image1, image2], crop_height, crop_width) - - with self.test_session() as sess: - with self.assertRaises(tf.errors.InvalidArgumentError): - sess.run(cropped, feed_dict={image1: np.random.rand(4, 5, 3), - image2: np.random.rand(4, 6, 1)}) - - def testDieOnRandomCropWhenImagesWithDifferentHeight(self): - crop_height, crop_width = 2, 3 - image1 = tf.placeholder(tf.float32, name='image1', shape=(None, None, 3)) - image2 = tf.placeholder(tf.float32, name='image2', shape=(None, None, 1)) - cropped = preprocess_utils.random_crop( - [image1, image2], crop_height, crop_width) - - with self.test_session() as sess: - with self.assertRaisesWithPredicateMatch( - tf.errors.InvalidArgumentError, - 'Wrong height for tensor'): - sess.run(cropped, feed_dict={image1: np.random.rand(4, 5, 3), - image2: np.random.rand(3, 5, 1)}) - - def testDieOnRandomCropWhenCropSizeIsGreaterThanImage(self): - crop_height, crop_width = 5, 9 - image1 = tf.placeholder(tf.float32, name='image1', shape=(None, None, 3)) - image2 = tf.placeholder(tf.float32, name='image2', shape=(None, None, 1)) - cropped = preprocess_utils.random_crop( - [image1, image2], crop_height, crop_width) - - with self.test_session() as sess: - with self.assertRaisesWithPredicateMatch( - tf.errors.InvalidArgumentError, - 'Crop size greater than the image size.'): - sess.run(cropped, feed_dict={image1: np.random.rand(4, 5, 3), - image2: np.random.rand(4, 5, 1)}) - - def testReturnPaddedImageWithNonZeroPadValue(self): - for dtype in [np.int32, np.int64, np.float32, np.float64]: - image = np.dstack([[[5, 6], - [9, 0]], - [[4, 3], - [3, 5]]]).astype(dtype) - expected_image = np.dstack([[[255, 255, 255, 255, 255], - [255, 255, 255, 255, 255], - [255, 5, 6, 255, 255], - [255, 9, 0, 255, 255], - [255, 255, 255, 255, 255]], - [[255, 255, 255, 255, 255], - [255, 255, 255, 255, 255], - [255, 4, 3, 255, 255], - [255, 3, 5, 255, 255], - [255, 255, 255, 255, 255]]]).astype(dtype) - - with self.session() as sess: - padded_image = preprocess_utils.pad_to_bounding_box( - image, 2, 1, 5, 5, 255) - padded_image = sess.run(padded_image) - self.assertAllClose(padded_image, expected_image) - # Add batch size = 1 to image. - padded_image = preprocess_utils.pad_to_bounding_box( - np.expand_dims(image, 0), 2, 1, 5, 5, 255) - padded_image = sess.run(padded_image) - self.assertAllClose(padded_image, np.expand_dims(expected_image, 0)) - - def testReturnOriginalImageWhenTargetSizeIsEqualToImageSize(self): - image = np.dstack([[[5, 6], - [9, 0]], - [[4, 3], - [3, 5]]]) - with self.session() as sess: - padded_image = preprocess_utils.pad_to_bounding_box( - image, 0, 0, 2, 2, 255) - padded_image = sess.run(padded_image) - self.assertAllClose(padded_image, image) - - def testDieOnTargetSizeGreaterThanImageSize(self): - image = np.dstack([[[5, 6], - [9, 0]], - [[4, 3], - [3, 5]]]) - with self.test_session(): - image_placeholder = tf.placeholder(tf.float32) - padded_image = preprocess_utils.pad_to_bounding_box( - image_placeholder, 0, 0, 2, 1, 255) - with self.assertRaisesWithPredicateMatch( - tf.errors.InvalidArgumentError, - 'target_width must be >= width'): - padded_image.eval(feed_dict={image_placeholder: image}) - padded_image = preprocess_utils.pad_to_bounding_box( - image_placeholder, 0, 0, 1, 2, 255) - with self.assertRaisesWithPredicateMatch( - tf.errors.InvalidArgumentError, - 'target_height must be >= height'): - padded_image.eval(feed_dict={image_placeholder: image}) - - def testDieIfTargetSizeNotPossibleWithGivenOffset(self): - image = np.dstack([[[5, 6], - [9, 0]], - [[4, 3], - [3, 5]]]) - with self.test_session(): - image_placeholder = tf.placeholder(tf.float32) - padded_image = preprocess_utils.pad_to_bounding_box( - image_placeholder, 3, 0, 4, 4, 255) - with self.assertRaisesWithPredicateMatch( - tf.errors.InvalidArgumentError, - 'target size not possible with the given target offsets'): - padded_image.eval(feed_dict={image_placeholder: image}) - - def testDieIfImageTensorRankIsTwo(self): - image = np.vstack([[5, 6], - [9, 0]]) - with self.test_session(): - image_placeholder = tf.placeholder(tf.float32) - padded_image = preprocess_utils.pad_to_bounding_box( - image_placeholder, 0, 0, 2, 2, 255) - with self.assertRaisesWithPredicateMatch( - tf.errors.InvalidArgumentError, - 'Wrong image tensor rank'): - padded_image.eval(feed_dict={image_placeholder: image}) - - def testResizeTensorsToRange(self): - test_shapes = [[60, 40], - [15, 30], - [15, 50]] - min_size = 50 - max_size = 100 - factor = None - expected_shape_list = [(75, 50, 3), - (50, 100, 3), - (30, 100, 3)] - for i, test_shape in enumerate(test_shapes): - image = tf.random.normal([test_shape[0], test_shape[1], 3]) - new_tensor_list = preprocess_utils.resize_to_range( - image=image, - label=None, - min_size=min_size, - max_size=max_size, - factor=factor, - align_corners=True) - with self.test_session() as session: - resized_image = session.run(new_tensor_list[0]) - self.assertEqual(resized_image.shape, expected_shape_list[i]) - - def testResizeTensorsToRangeWithFactor(self): - test_shapes = [[60, 40], - [15, 30], - [15, 50]] - min_size = 50 - max_size = 98 - factor = 8 - expected_image_shape_list = [(81, 57, 3), - (49, 97, 3), - (33, 97, 3)] - expected_label_shape_list = [(81, 57, 1), - (49, 97, 1), - (33, 97, 1)] - for i, test_shape in enumerate(test_shapes): - image = tf.random.normal([test_shape[0], test_shape[1], 3]) - label = tf.random.normal([test_shape[0], test_shape[1], 1]) - new_tensor_list = preprocess_utils.resize_to_range( - image=image, - label=label, - min_size=min_size, - max_size=max_size, - factor=factor, - align_corners=True) - with self.test_session() as session: - new_tensor_list = session.run(new_tensor_list) - self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i]) - self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i]) - - def testResizeTensorsToRangeWithFactorAndLabelShapeCHW(self): - test_shapes = [[60, 40], - [15, 30], - [15, 50]] - min_size = 50 - max_size = 98 - factor = 8 - expected_image_shape_list = [(81, 57, 3), - (49, 97, 3), - (33, 97, 3)] - expected_label_shape_list = [(5, 81, 57), - (5, 49, 97), - (5, 33, 97)] - for i, test_shape in enumerate(test_shapes): - image = tf.random.normal([test_shape[0], test_shape[1], 3]) - label = tf.random.normal([5, test_shape[0], test_shape[1]]) - new_tensor_list = preprocess_utils.resize_to_range( - image=image, - label=label, - min_size=min_size, - max_size=max_size, - factor=factor, - align_corners=True, - label_layout_is_chw=True) - with self.test_session() as session: - new_tensor_list = session.run(new_tensor_list) - self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i]) - self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i]) - - def testResizeTensorsToRangeWithSimilarMinMaxSizes(self): - test_shapes = [[60, 40], - [15, 30], - [15, 50]] - # Values set so that one of the side = 97. - min_size = 96 - max_size = 98 - factor = 8 - expected_image_shape_list = [(97, 65, 3), - (49, 97, 3), - (33, 97, 3)] - expected_label_shape_list = [(97, 65, 1), - (49, 97, 1), - (33, 97, 1)] - for i, test_shape in enumerate(test_shapes): - image = tf.random.normal([test_shape[0], test_shape[1], 3]) - label = tf.random.normal([test_shape[0], test_shape[1], 1]) - new_tensor_list = preprocess_utils.resize_to_range( - image=image, - label=label, - min_size=min_size, - max_size=max_size, - factor=factor, - align_corners=True) - with self.test_session() as session: - new_tensor_list = session.run(new_tensor_list) - self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i]) - self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i]) - - def testResizeTensorsToRangeWithEqualMaxSize(self): - test_shapes = [[97, 38], - [96, 97]] - # Make max_size equal to the larger value of test_shapes. - min_size = 97 - max_size = 97 - factor = 8 - expected_image_shape_list = [(97, 41, 3), - (97, 97, 3)] - expected_label_shape_list = [(97, 41, 1), - (97, 97, 1)] - for i, test_shape in enumerate(test_shapes): - image = tf.random.normal([test_shape[0], test_shape[1], 3]) - label = tf.random.normal([test_shape[0], test_shape[1], 1]) - new_tensor_list = preprocess_utils.resize_to_range( - image=image, - label=label, - min_size=min_size, - max_size=max_size, - factor=factor, - align_corners=True) - with self.test_session() as session: - new_tensor_list = session.run(new_tensor_list) - self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i]) - self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i]) - - def testResizeTensorsToRangeWithPotentialErrorInTFCeil(self): - test_shape = [3936, 5248] - # Make max_size equal to the larger value of test_shapes. - min_size = 1441 - max_size = 1441 - factor = 16 - expected_image_shape = (1089, 1441, 3) - expected_label_shape = (1089, 1441, 1) - image = tf.random.normal([test_shape[0], test_shape[1], 3]) - label = tf.random.normal([test_shape[0], test_shape[1], 1]) - new_tensor_list = preprocess_utils.resize_to_range( - image=image, - label=label, - min_size=min_size, - max_size=max_size, - factor=factor, - align_corners=True) - with self.test_session() as session: - new_tensor_list = session.run(new_tensor_list) - self.assertEqual(new_tensor_list[0].shape, expected_image_shape) - self.assertEqual(new_tensor_list[1].shape, expected_label_shape) - - def testResizeTensorsToRangeWithEqualMaxSizeWithoutAspectRatio(self): - test_shapes = [[97, 38], - [96, 97]] - # Make max_size equal to the larger value of test_shapes. - min_size = 97 - max_size = 97 - factor = 8 - keep_aspect_ratio = False - expected_image_shape_list = [(97, 97, 3), - (97, 97, 3)] - expected_label_shape_list = [(97, 97, 1), - (97, 97, 1)] - for i, test_shape in enumerate(test_shapes): - image = tf.random.normal([test_shape[0], test_shape[1], 3]) - label = tf.random.normal([test_shape[0], test_shape[1], 1]) - new_tensor_list = preprocess_utils.resize_to_range( - image=image, - label=label, - min_size=min_size, - max_size=max_size, - factor=factor, - keep_aspect_ratio=keep_aspect_ratio, - align_corners=True) - with self.test_session() as session: - new_tensor_list = session.run(new_tensor_list) - self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i]) - self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/core/resnet_v1_beta.py b/research/deeplab/core/resnet_v1_beta.py deleted file mode 100644 index 0d5f1f19a23..00000000000 --- a/research/deeplab/core/resnet_v1_beta.py +++ /dev/null @@ -1,827 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Resnet v1 model variants. - -Code branched out from slim/nets/resnet_v1.py, and please refer to it for -more details. - -The original version ResNets-v1 were proposed by: -[1] Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun - Deep Residual Learning for Image Recognition. arXiv:1512.03385 -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -from six.moves import range -import tensorflow as tf -from tensorflow.contrib import slim as contrib_slim -from deeplab.core import conv2d_ws -from deeplab.core import utils -from tensorflow.contrib.slim.nets import resnet_utils - -slim = contrib_slim - -_DEFAULT_MULTI_GRID = [1, 1, 1] -_DEFAULT_MULTI_GRID_RESNET_18 = [1, 1] - - -@slim.add_arg_scope -def bottleneck(inputs, - depth, - depth_bottleneck, - stride, - unit_rate=1, - rate=1, - outputs_collections=None, - scope=None): - """Bottleneck residual unit variant with BN after convolutions. - - This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for - its definition. Note that we use here the bottleneck variant which has an - extra bottleneck layer. - - When putting together two consecutive ResNet blocks that use this unit, one - should use stride = 2 in the last unit of the first block. - - Args: - inputs: A tensor of size [batch, height, width, channels]. - depth: The depth of the ResNet unit output. - depth_bottleneck: The depth of the bottleneck layers. - stride: The ResNet unit's stride. Determines the amount of downsampling of - the units output compared to its input. - unit_rate: An integer, unit rate for atrous convolution. - rate: An integer, rate for atrous convolution. - outputs_collections: Collection to add the ResNet unit output. - scope: Optional variable_scope. - - Returns: - The ResNet unit's output. - """ - with tf.variable_scope(scope, 'bottleneck_v1', [inputs]) as sc: - depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4) - if depth == depth_in: - shortcut = resnet_utils.subsample(inputs, stride, 'shortcut') - else: - shortcut = conv2d_ws.conv2d( - inputs, - depth, - [1, 1], - stride=stride, - activation_fn=None, - scope='shortcut') - - residual = conv2d_ws.conv2d(inputs, depth_bottleneck, [1, 1], stride=1, - scope='conv1') - residual = conv2d_ws.conv2d_same(residual, depth_bottleneck, 3, stride, - rate=rate*unit_rate, scope='conv2') - residual = conv2d_ws.conv2d(residual, depth, [1, 1], stride=1, - activation_fn=None, scope='conv3') - output = tf.nn.relu(shortcut + residual) - - return slim.utils.collect_named_outputs(outputs_collections, sc.name, - output) - - -@slim.add_arg_scope -def lite_bottleneck(inputs, - depth, - stride, - unit_rate=1, - rate=1, - outputs_collections=None, - scope=None): - """Bottleneck residual unit variant with BN after convolutions. - - This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for - its definition. Note that we use here the bottleneck variant which has an - extra bottleneck layer. - - When putting together two consecutive ResNet blocks that use this unit, one - should use stride = 2 in the last unit of the first block. - - Args: - inputs: A tensor of size [batch, height, width, channels]. - depth: The depth of the ResNet unit output. - stride: The ResNet unit's stride. Determines the amount of downsampling of - the units output compared to its input. - unit_rate: An integer, unit rate for atrous convolution. - rate: An integer, rate for atrous convolution. - outputs_collections: Collection to add the ResNet unit output. - scope: Optional variable_scope. - - Returns: - The ResNet unit's output. - """ - with tf.variable_scope(scope, 'lite_bottleneck_v1', [inputs]) as sc: - depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4) - if depth == depth_in: - shortcut = resnet_utils.subsample(inputs, stride, 'shortcut') - else: - shortcut = conv2d_ws.conv2d( - inputs, - depth, [1, 1], - stride=stride, - activation_fn=None, - scope='shortcut') - - residual = conv2d_ws.conv2d_same( - inputs, depth, 3, 1, rate=rate * unit_rate, scope='conv1') - with slim.arg_scope([conv2d_ws.conv2d], activation_fn=None): - residual = conv2d_ws.conv2d_same( - residual, depth, 3, stride, rate=rate * unit_rate, scope='conv2') - output = tf.nn.relu(shortcut + residual) - - return slim.utils.collect_named_outputs(outputs_collections, sc.name, - output) - - -def root_block_fn_for_beta_variant(net, depth_multiplier=1.0): - """Gets root_block_fn for beta variant. - - ResNet-v1 beta variant modifies the first original 7x7 convolution to three - 3x3 convolutions. - - Args: - net: A tensor of size [batch, height, width, channels], input to the model. - depth_multiplier: Controls the number of convolution output channels for - each input channel. The total number of depthwise convolution output - channels will be equal to `num_filters_out * depth_multiplier`. - - Returns: - A tensor after three 3x3 convolutions. - """ - net = conv2d_ws.conv2d_same( - net, int(64 * depth_multiplier), 3, stride=2, scope='conv1_1') - net = conv2d_ws.conv2d_same( - net, int(64 * depth_multiplier), 3, stride=1, scope='conv1_2') - net = conv2d_ws.conv2d_same( - net, int(128 * depth_multiplier), 3, stride=1, scope='conv1_3') - - return net - - -def resnet_v1_beta(inputs, - blocks, - num_classes=None, - is_training=None, - global_pool=True, - output_stride=None, - root_block_fn=None, - reuse=None, - scope=None, - sync_batch_norm_method='None'): - """Generator for v1 ResNet models (beta variant). - - This function generates a family of modified ResNet v1 models. In particular, - the first original 7x7 convolution is replaced with three 3x3 convolutions. - See the resnet_v1_*() methods for specific model instantiations, obtained by - selecting different block instantiations that produce ResNets of various - depths. - - The code is modified from slim/nets/resnet_v1.py, and please refer to it for - more details. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - blocks: A list of length equal to the number of ResNet blocks. Each element - is a resnet_utils.Block object describing the units in the block. - num_classes: Number of predicted classes for classification tasks. If None - we return the features before the logit layer. - is_training: Enable/disable is_training for batch normalization. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - root_block_fn: The function consisting of convolution operations applied to - the root input. If root_block_fn is None, use the original setting of - RseNet-v1, which is simply one convolution with 7x7 kernel and stride=2. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is None, then - net is the output of the last ResNet block, potentially after global - average pooling. If num_classes is not None, net contains the pre-softmax - activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: If the target output_stride is not valid. - """ - if root_block_fn is None: - root_block_fn = functools.partial(conv2d_ws.conv2d_same, - num_outputs=64, - kernel_size=7, - stride=2, - scope='conv1') - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - with tf.variable_scope(scope, 'resnet_v1', [inputs], reuse=reuse) as sc: - end_points_collection = sc.original_name_scope + '_end_points' - with slim.arg_scope([ - conv2d_ws.conv2d, bottleneck, lite_bottleneck, - resnet_utils.stack_blocks_dense - ], - outputs_collections=end_points_collection): - if is_training is not None: - arg_scope = slim.arg_scope([batch_norm], is_training=is_training) - else: - arg_scope = slim.arg_scope([]) - with arg_scope: - net = inputs - if output_stride is not None: - if output_stride % 4 != 0: - raise ValueError('The output_stride needs to be a multiple of 4.') - output_stride //= 4 - net = root_block_fn(net) - net = slim.max_pool2d(net, 3, stride=2, padding='SAME', scope='pool1') - net = resnet_utils.stack_blocks_dense(net, blocks, output_stride) - - if global_pool: - # Global average pooling. - net = tf.reduce_mean(net, [1, 2], name='pool5', keepdims=True) - if num_classes is not None: - net = conv2d_ws.conv2d(net, num_classes, [1, 1], activation_fn=None, - normalizer_fn=None, scope='logits', - use_weight_standardization=False) - # Convert end_points_collection into a dictionary of end_points. - end_points = slim.utils.convert_collection_to_dict( - end_points_collection) - if num_classes is not None: - end_points['predictions'] = slim.softmax(net, scope='predictions') - return net, end_points - - -def resnet_v1_beta_block(scope, base_depth, num_units, stride): - """Helper function for creating a resnet_v1 beta variant bottleneck block. - - Args: - scope: The scope of the block. - base_depth: The depth of the bottleneck layer for each unit. - num_units: The number of units in the block. - stride: The stride of the block, implemented as a stride in the last unit. - All other units have stride=1. - - Returns: - A resnet_v1 bottleneck block. - """ - return resnet_utils.Block(scope, bottleneck, [{ - 'depth': base_depth * 4, - 'depth_bottleneck': base_depth, - 'stride': 1, - 'unit_rate': 1 - }] * (num_units - 1) + [{ - 'depth': base_depth * 4, - 'depth_bottleneck': base_depth, - 'stride': stride, - 'unit_rate': 1 - }]) - - -def resnet_v1_small_beta_block(scope, base_depth, num_units, stride): - """Helper function for creating a resnet_18 beta variant bottleneck block. - - Args: - scope: The scope of the block. - base_depth: The depth of the bottleneck layer for each unit. - num_units: The number of units in the block. - stride: The stride of the block, implemented as a stride in the last unit. - All other units have stride=1. - - Returns: - A resnet_18 bottleneck block. - """ - block_args = [] - for _ in range(num_units - 1): - block_args.append({'depth': base_depth, 'stride': 1, 'unit_rate': 1}) - block_args.append({'depth': base_depth, 'stride': stride, 'unit_rate': 1}) - return resnet_utils.Block(scope, lite_bottleneck, block_args) - - -def resnet_v1_18(inputs, - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - multi_grid=None, - reuse=None, - scope='resnet_v1_18', - sync_batch_norm_method='None'): - """Resnet v1 18. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - num_classes: Number of predicted classes for classification tasks. If None - we return the features before the logit layer. - is_training: Enable/disable is_training for batch normalization. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - multi_grid: Employ a hierarchy of different atrous rates within network. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is None, then - net is the output of the last ResNet block, potentially after global - average pooling. If num_classes is not None, net contains the pre-softmax - activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: if multi_grid is not None and does not have length = 3. - """ - if multi_grid is None: - multi_grid = _DEFAULT_MULTI_GRID_RESNET_18 - else: - if len(multi_grid) != 2: - raise ValueError('Expect multi_grid to have length 2.') - - block4_args = [] - for rate in multi_grid: - block4_args.append({'depth': 512, 'stride': 1, 'unit_rate': rate}) - - blocks = [ - resnet_v1_small_beta_block( - 'block1', base_depth=64, num_units=2, stride=2), - resnet_v1_small_beta_block( - 'block2', base_depth=128, num_units=2, stride=2), - resnet_v1_small_beta_block( - 'block3', base_depth=256, num_units=2, stride=2), - resnet_utils.Block('block4', lite_bottleneck, block4_args), - ] - return resnet_v1_beta( - inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def resnet_v1_18_beta(inputs, - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - multi_grid=None, - root_depth_multiplier=0.25, - reuse=None, - scope='resnet_v1_18', - sync_batch_norm_method='None'): - """Resnet v1 18 beta variant. - - This variant modifies the first convolution layer of ResNet-v1-18. In - particular, it changes the original one 7x7 convolution to three 3x3 - convolutions. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - num_classes: Number of predicted classes for classification tasks. If None - we return the features before the logit layer. - is_training: Enable/disable is_training for batch normalization. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - multi_grid: Employ a hierarchy of different atrous rates within network. - root_depth_multiplier: Float, depth multiplier used for the first three - convolution layers that replace the 7x7 convolution. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is None, then - net is the output of the last ResNet block, potentially after global - average pooling. If num_classes is not None, net contains the pre-softmax - activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: if multi_grid is not None and does not have length = 3. - """ - if multi_grid is None: - multi_grid = _DEFAULT_MULTI_GRID_RESNET_18 - else: - if len(multi_grid) != 2: - raise ValueError('Expect multi_grid to have length 2.') - - block4_args = [] - for rate in multi_grid: - block4_args.append({'depth': 512, 'stride': 1, 'unit_rate': rate}) - - blocks = [ - resnet_v1_small_beta_block( - 'block1', base_depth=64, num_units=2, stride=2), - resnet_v1_small_beta_block( - 'block2', base_depth=128, num_units=2, stride=2), - resnet_v1_small_beta_block( - 'block3', base_depth=256, num_units=2, stride=2), - resnet_utils.Block('block4', lite_bottleneck, block4_args), - ] - return resnet_v1_beta( - inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - root_block_fn=functools.partial(root_block_fn_for_beta_variant, - depth_multiplier=root_depth_multiplier), - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def resnet_v1_50(inputs, - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - multi_grid=None, - reuse=None, - scope='resnet_v1_50', - sync_batch_norm_method='None'): - """Resnet v1 50. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - num_classes: Number of predicted classes for classification tasks. If None - we return the features before the logit layer. - is_training: Enable/disable is_training for batch normalization. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - multi_grid: Employ a hierarchy of different atrous rates within network. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is None, then - net is the output of the last ResNet block, potentially after global - average pooling. If num_classes is not None, net contains the pre-softmax - activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: if multi_grid is not None and does not have length = 3. - """ - if multi_grid is None: - multi_grid = _DEFAULT_MULTI_GRID - else: - if len(multi_grid) != 3: - raise ValueError('Expect multi_grid to have length 3.') - - blocks = [ - resnet_v1_beta_block( - 'block1', base_depth=64, num_units=3, stride=2), - resnet_v1_beta_block( - 'block2', base_depth=128, num_units=4, stride=2), - resnet_v1_beta_block( - 'block3', base_depth=256, num_units=6, stride=2), - resnet_utils.Block('block4', bottleneck, [ - {'depth': 2048, 'depth_bottleneck': 512, 'stride': 1, - 'unit_rate': rate} for rate in multi_grid]), - ] - return resnet_v1_beta( - inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def resnet_v1_50_beta(inputs, - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - multi_grid=None, - reuse=None, - scope='resnet_v1_50', - sync_batch_norm_method='None'): - """Resnet v1 50 beta variant. - - This variant modifies the first convolution layer of ResNet-v1-50. In - particular, it changes the original one 7x7 convolution to three 3x3 - convolutions. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - num_classes: Number of predicted classes for classification tasks. If None - we return the features before the logit layer. - is_training: Enable/disable is_training for batch normalization. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - multi_grid: Employ a hierarchy of different atrous rates within network. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is None, then - net is the output of the last ResNet block, potentially after global - average pooling. If num_classes is not None, net contains the pre-softmax - activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: if multi_grid is not None and does not have length = 3. - """ - if multi_grid is None: - multi_grid = _DEFAULT_MULTI_GRID - else: - if len(multi_grid) != 3: - raise ValueError('Expect multi_grid to have length 3.') - - blocks = [ - resnet_v1_beta_block( - 'block1', base_depth=64, num_units=3, stride=2), - resnet_v1_beta_block( - 'block2', base_depth=128, num_units=4, stride=2), - resnet_v1_beta_block( - 'block3', base_depth=256, num_units=6, stride=2), - resnet_utils.Block('block4', bottleneck, [ - {'depth': 2048, 'depth_bottleneck': 512, 'stride': 1, - 'unit_rate': rate} for rate in multi_grid]), - ] - return resnet_v1_beta( - inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - root_block_fn=functools.partial(root_block_fn_for_beta_variant), - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def resnet_v1_101(inputs, - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - multi_grid=None, - reuse=None, - scope='resnet_v1_101', - sync_batch_norm_method='None'): - """Resnet v1 101. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - num_classes: Number of predicted classes for classification tasks. If None - we return the features before the logit layer. - is_training: Enable/disable is_training for batch normalization. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - multi_grid: Employ a hierarchy of different atrous rates within network. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is None, then - net is the output of the last ResNet block, potentially after global - average pooling. If num_classes is not None, net contains the pre-softmax - activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: if multi_grid is not None and does not have length = 3. - """ - if multi_grid is None: - multi_grid = _DEFAULT_MULTI_GRID - else: - if len(multi_grid) != 3: - raise ValueError('Expect multi_grid to have length 3.') - - blocks = [ - resnet_v1_beta_block( - 'block1', base_depth=64, num_units=3, stride=2), - resnet_v1_beta_block( - 'block2', base_depth=128, num_units=4, stride=2), - resnet_v1_beta_block( - 'block3', base_depth=256, num_units=23, stride=2), - resnet_utils.Block('block4', bottleneck, [ - {'depth': 2048, 'depth_bottleneck': 512, 'stride': 1, - 'unit_rate': rate} for rate in multi_grid]), - ] - return resnet_v1_beta( - inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def resnet_v1_101_beta(inputs, - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - multi_grid=None, - reuse=None, - scope='resnet_v1_101', - sync_batch_norm_method='None'): - """Resnet v1 101 beta variant. - - This variant modifies the first convolution layer of ResNet-v1-101. In - particular, it changes the original one 7x7 convolution to three 3x3 - convolutions. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - num_classes: Number of predicted classes for classification tasks. If None - we return the features before the logit layer. - is_training: Enable/disable is_training for batch normalization. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - multi_grid: Employ a hierarchy of different atrous rates within network. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is None, then - net is the output of the last ResNet block, potentially after global - average pooling. If num_classes is not None, net contains the pre-softmax - activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: if multi_grid is not None and does not have length = 3. - """ - if multi_grid is None: - multi_grid = _DEFAULT_MULTI_GRID - else: - if len(multi_grid) != 3: - raise ValueError('Expect multi_grid to have length 3.') - - blocks = [ - resnet_v1_beta_block( - 'block1', base_depth=64, num_units=3, stride=2), - resnet_v1_beta_block( - 'block2', base_depth=128, num_units=4, stride=2), - resnet_v1_beta_block( - 'block3', base_depth=256, num_units=23, stride=2), - resnet_utils.Block('block4', bottleneck, [ - {'depth': 2048, 'depth_bottleneck': 512, 'stride': 1, - 'unit_rate': rate} for rate in multi_grid]), - ] - return resnet_v1_beta( - inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - root_block_fn=functools.partial(root_block_fn_for_beta_variant), - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def resnet_arg_scope(weight_decay=0.0001, - batch_norm_decay=0.997, - batch_norm_epsilon=1e-5, - batch_norm_scale=True, - activation_fn=tf.nn.relu, - use_batch_norm=True, - sync_batch_norm_method='None', - normalization_method='unspecified', - use_weight_standardization=False): - """Defines the default ResNet arg scope. - - Args: - weight_decay: The weight decay to use for regularizing the model. - batch_norm_decay: The moving average decay when estimating layer activation - statistics in batch normalization. - batch_norm_epsilon: Small constant to prevent division by zero when - normalizing activations by their variance in batch normalization. - batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the - activations in the batch normalization layer. - activation_fn: The activation function which is used in ResNet. - use_batch_norm: Deprecated in favor of normalization_method. - sync_batch_norm_method: String, sync batchnorm method. - normalization_method: String, one of `batch`, `none`, or `group`, to use - batch normalization, no normalization, or group normalization. - use_weight_standardization: Boolean, whether to use weight standardization. - - Returns: - An `arg_scope` to use for the resnet models. - """ - batch_norm_params = { - 'decay': batch_norm_decay, - 'epsilon': batch_norm_epsilon, - 'scale': batch_norm_scale, - } - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - if normalization_method == 'batch': - normalizer_fn = batch_norm - elif normalization_method == 'none': - normalizer_fn = None - elif normalization_method == 'group': - normalizer_fn = slim.group_norm - elif normalization_method == 'unspecified': - normalizer_fn = batch_norm if use_batch_norm else None - else: - raise ValueError('Unrecognized normalization_method %s' % - normalization_method) - - with slim.arg_scope([conv2d_ws.conv2d], - weights_regularizer=slim.l2_regularizer(weight_decay), - weights_initializer=slim.variance_scaling_initializer(), - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - use_weight_standardization=use_weight_standardization): - with slim.arg_scope([batch_norm], **batch_norm_params): - # The following implies padding='SAME' for pool1, which makes feature - # alignment easier for dense prediction tasks. This is also used in - # https://github.com/facebook/fb.resnet.torch. However the accompanying - # code of 'Deep Residual Learning for Image Recognition' uses - # padding='VALID' for pool1. You can switch to that choice by setting - # slim.arg_scope([slim.max_pool2d], padding='VALID'). - with slim.arg_scope([slim.max_pool2d], padding='SAME') as arg_sc: - return arg_sc diff --git a/research/deeplab/core/resnet_v1_beta_test.py b/research/deeplab/core/resnet_v1_beta_test.py deleted file mode 100644 index 8b61edcce21..00000000000 --- a/research/deeplab/core/resnet_v1_beta_test.py +++ /dev/null @@ -1,564 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for resnet_v1_beta module.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools - -import numpy as np -import six -import tensorflow as tf -from tensorflow.contrib import slim as contrib_slim - -from deeplab.core import resnet_v1_beta -from tensorflow.contrib.slim.nets import resnet_utils - -slim = contrib_slim - - -def create_test_input(batch, height, width, channels): - """Create test input tensor.""" - if None in [batch, height, width, channels]: - return tf.placeholder(tf.float32, (batch, height, width, channels)) - else: - return tf.to_float( - np.tile( - np.reshape( - np.reshape(np.arange(height), [height, 1]) + - np.reshape(np.arange(width), [1, width]), - [1, height, width, 1]), - [batch, 1, 1, channels])) - - -class ResnetCompleteNetworkTest(tf.test.TestCase): - """Tests with complete small ResNet v1 networks.""" - - def _resnet_small_lite_bottleneck(self, - inputs, - num_classes=None, - is_training=True, - global_pool=True, - output_stride=None, - multi_grid=None, - reuse=None, - scope='resnet_v1_small'): - """A shallow and thin ResNet v1 with lite_bottleneck.""" - if multi_grid is None: - multi_grid = [1, 1] - else: - if len(multi_grid) != 2: - raise ValueError('Expect multi_grid to have length 2.') - block = resnet_v1_beta.resnet_v1_small_beta_block - blocks = [ - block('block1', base_depth=1, num_units=1, stride=2), - block('block2', base_depth=2, num_units=1, stride=2), - block('block3', base_depth=4, num_units=1, stride=2), - resnet_utils.Block('block4', resnet_v1_beta.lite_bottleneck, [ - {'depth': 8, - 'stride': 1, - 'unit_rate': rate} for rate in multi_grid])] - return resnet_v1_beta.resnet_v1_beta( - inputs, - blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - root_block_fn=functools.partial( - resnet_v1_beta.root_block_fn_for_beta_variant, - depth_multiplier=0.25), - reuse=reuse, - scope=scope) - - def _resnet_small(self, - inputs, - num_classes=None, - is_training=True, - global_pool=True, - output_stride=None, - multi_grid=None, - reuse=None, - scope='resnet_v1_small'): - """A shallow and thin ResNet v1 for faster tests.""" - if multi_grid is None: - multi_grid = [1, 1, 1] - else: - if len(multi_grid) != 3: - raise ValueError('Expect multi_grid to have length 3.') - - block = resnet_v1_beta.resnet_v1_beta_block - blocks = [ - block('block1', base_depth=1, num_units=1, stride=2), - block('block2', base_depth=2, num_units=1, stride=2), - block('block3', base_depth=4, num_units=1, stride=2), - resnet_utils.Block('block4', resnet_v1_beta.bottleneck, [ - {'depth': 32, 'depth_bottleneck': 8, 'stride': 1, - 'unit_rate': rate} for rate in multi_grid])] - - return resnet_v1_beta.resnet_v1_beta( - inputs, - blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - root_block_fn=functools.partial( - resnet_v1_beta.root_block_fn_for_beta_variant), - reuse=reuse, - scope=scope) - - def testClassificationEndPointsWithLiteBottleneck(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - logits, end_points = self._resnet_small_lite_bottleneck( - inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertIn('predictions', end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - - def testClassificationEndPointsWithMultigridAndLiteBottleneck(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - multi_grid = [1, 2] - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - logits, end_points = self._resnet_small_lite_bottleneck( - inputs, - num_classes, - global_pool=global_pool, - multi_grid=multi_grid, - scope='resnet') - - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertIn('predictions', end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - - def testClassificationShapesWithLiteBottleneck(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - _, end_points = self._resnet_small_lite_bottleneck( - inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - endpoint_to_shape = { - 'resnet/conv1_1': [2, 112, 112, 16], - 'resnet/conv1_2': [2, 112, 112, 16], - 'resnet/conv1_3': [2, 112, 112, 32], - 'resnet/block1': [2, 28, 28, 1], - 'resnet/block2': [2, 14, 14, 2], - 'resnet/block3': [2, 7, 7, 4], - 'resnet/block4': [2, 7, 7, 8]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testFullyConvolutionalEndpointShapesWithLiteBottleneck(self): - global_pool = False - num_classes = 10 - inputs = create_test_input(2, 321, 321, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - _, end_points = self._resnet_small_lite_bottleneck( - inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - endpoint_to_shape = { - 'resnet/conv1_1': [2, 161, 161, 16], - 'resnet/conv1_2': [2, 161, 161, 16], - 'resnet/conv1_3': [2, 161, 161, 32], - 'resnet/block1': [2, 41, 41, 1], - 'resnet/block2': [2, 21, 21, 2], - 'resnet/block3': [2, 11, 11, 4], - 'resnet/block4': [2, 11, 11, 8]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testAtrousFullyConvolutionalEndpointShapesWithLiteBottleneck(self): - global_pool = False - num_classes = 10 - output_stride = 8 - inputs = create_test_input(2, 321, 321, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - _, end_points = self._resnet_small_lite_bottleneck( - inputs, - num_classes, - global_pool=global_pool, - output_stride=output_stride, - scope='resnet') - endpoint_to_shape = { - 'resnet/conv1_1': [2, 161, 161, 16], - 'resnet/conv1_2': [2, 161, 161, 16], - 'resnet/conv1_3': [2, 161, 161, 32], - 'resnet/block1': [2, 41, 41, 1], - 'resnet/block2': [2, 41, 41, 2], - 'resnet/block3': [2, 41, 41, 4], - 'resnet/block4': [2, 41, 41, 8]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testAtrousFullyConvolutionalValuesWithLiteBottleneck(self): - """Verify dense feature extraction with atrous convolution.""" - nominal_stride = 32 - for output_stride in [4, 8, 16, 32, None]: - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - with tf.Graph().as_default(): - with self.test_session() as sess: - tf.set_random_seed(0) - inputs = create_test_input(2, 81, 81, 3) - # Dense feature extraction followed by subsampling. - output, _ = self._resnet_small_lite_bottleneck( - inputs, - None, - is_training=False, - global_pool=False, - output_stride=output_stride) - if output_stride is None: - factor = 1 - else: - factor = nominal_stride // output_stride - output = resnet_utils.subsample(output, factor) - # Make the two networks use the same weights. - tf.get_variable_scope().reuse_variables() - # Feature extraction at the nominal network rate. - expected, _ = self._resnet_small_lite_bottleneck( - inputs, - None, - is_training=False, - global_pool=False) - sess.run(tf.global_variables_initializer()) - self.assertAllClose(output.eval(), expected.eval(), - atol=1e-4, rtol=1e-4) - - def testUnknownBatchSizeWithLiteBottleneck(self): - batch = 2 - height, width = 65, 65 - global_pool = True - num_classes = 10 - inputs = create_test_input(None, height, width, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - logits, _ = self._resnet_small_lite_bottleneck( - inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), - [None, 1, 1, num_classes]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(logits, {inputs: images.eval()}) - self.assertEqual(output.shape, (batch, 1, 1, num_classes)) - - def testFullyConvolutionalUnknownHeightWidthWithLiteBottleneck(self): - batch = 2 - height, width = 65, 65 - global_pool = False - inputs = create_test_input(batch, None, None, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - output, _ = self._resnet_small_lite_bottleneck( - inputs, - None, - global_pool=global_pool) - self.assertListEqual(output.get_shape().as_list(), - [batch, None, None, 8]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(output, {inputs: images.eval()}) - self.assertEqual(output.shape, (batch, 3, 3, 8)) - - def testAtrousFullyConvolutionalUnknownHeightWidthWithLiteBottleneck(self): - batch = 2 - height, width = 65, 65 - global_pool = False - output_stride = 8 - inputs = create_test_input(batch, None, None, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - output, _ = self._resnet_small_lite_bottleneck( - inputs, - None, - global_pool=global_pool, - output_stride=output_stride) - self.assertListEqual(output.get_shape().as_list(), - [batch, None, None, 8]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(output, {inputs: images.eval()}) - self.assertEqual(output.shape, (batch, 9, 9, 8)) - - def testClassificationEndPoints(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - logits, end_points = self._resnet_small(inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertIn('predictions', end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - - def testClassificationEndPointsWithWS(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with slim.arg_scope( - resnet_v1_beta.resnet_arg_scope(use_weight_standardization=True)): - logits, end_points = self._resnet_small( - inputs, num_classes, global_pool=global_pool, scope='resnet') - - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertIn('predictions', end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - - def testClassificationEndPointsWithGN(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with slim.arg_scope( - resnet_v1_beta.resnet_arg_scope(normalization_method='group')): - with slim.arg_scope([slim.group_norm], groups=1): - logits, end_points = self._resnet_small( - inputs, num_classes, global_pool=global_pool, scope='resnet') - - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertIn('predictions', end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - - def testInvalidGroupsWithGN(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with self.assertRaisesRegexp(ValueError, 'Invalid groups'): - with slim.arg_scope( - resnet_v1_beta.resnet_arg_scope(normalization_method='group')): - with slim.arg_scope([slim.group_norm], groups=32): - _, _ = self._resnet_small( - inputs, num_classes, global_pool=global_pool, scope='resnet') - - def testClassificationEndPointsWithGNWS(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with slim.arg_scope( - resnet_v1_beta.resnet_arg_scope( - normalization_method='group', use_weight_standardization=True)): - with slim.arg_scope([slim.group_norm], groups=1): - logits, end_points = self._resnet_small( - inputs, num_classes, global_pool=global_pool, scope='resnet') - - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertIn('predictions', end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - - def testClassificationEndPointsWithMultigrid(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - multi_grid = [1, 2, 4] - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - logits, end_points = self._resnet_small(inputs, - num_classes, - global_pool=global_pool, - multi_grid=multi_grid, - scope='resnet') - - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertIn('predictions', end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - - def testClassificationShapes(self): - global_pool = True - num_classes = 10 - inputs = create_test_input(2, 224, 224, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - _, end_points = self._resnet_small(inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - endpoint_to_shape = { - 'resnet/conv1_1': [2, 112, 112, 64], - 'resnet/conv1_2': [2, 112, 112, 64], - 'resnet/conv1_3': [2, 112, 112, 128], - 'resnet/block1': [2, 28, 28, 4], - 'resnet/block2': [2, 14, 14, 8], - 'resnet/block3': [2, 7, 7, 16], - 'resnet/block4': [2, 7, 7, 32]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testFullyConvolutionalEndpointShapes(self): - global_pool = False - num_classes = 10 - inputs = create_test_input(2, 321, 321, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - _, end_points = self._resnet_small(inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - endpoint_to_shape = { - 'resnet/conv1_1': [2, 161, 161, 64], - 'resnet/conv1_2': [2, 161, 161, 64], - 'resnet/conv1_3': [2, 161, 161, 128], - 'resnet/block1': [2, 41, 41, 4], - 'resnet/block2': [2, 21, 21, 8], - 'resnet/block3': [2, 11, 11, 16], - 'resnet/block4': [2, 11, 11, 32]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testAtrousFullyConvolutionalEndpointShapes(self): - global_pool = False - num_classes = 10 - output_stride = 8 - inputs = create_test_input(2, 321, 321, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - _, end_points = self._resnet_small(inputs, - num_classes, - global_pool=global_pool, - output_stride=output_stride, - scope='resnet') - endpoint_to_shape = { - 'resnet/conv1_1': [2, 161, 161, 64], - 'resnet/conv1_2': [2, 161, 161, 64], - 'resnet/conv1_3': [2, 161, 161, 128], - 'resnet/block1': [2, 41, 41, 4], - 'resnet/block2': [2, 41, 41, 8], - 'resnet/block3': [2, 41, 41, 16], - 'resnet/block4': [2, 41, 41, 32]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testAtrousFullyConvolutionalValues(self): - """Verify dense feature extraction with atrous convolution.""" - nominal_stride = 32 - for output_stride in [4, 8, 16, 32, None]: - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - with tf.Graph().as_default(): - with self.test_session() as sess: - tf.set_random_seed(0) - inputs = create_test_input(2, 81, 81, 3) - # Dense feature extraction followed by subsampling. - output, _ = self._resnet_small(inputs, - None, - is_training=False, - global_pool=False, - output_stride=output_stride) - if output_stride is None: - factor = 1 - else: - factor = nominal_stride // output_stride - output = resnet_utils.subsample(output, factor) - # Make the two networks use the same weights. - tf.get_variable_scope().reuse_variables() - # Feature extraction at the nominal network rate. - expected, _ = self._resnet_small(inputs, - None, - is_training=False, - global_pool=False) - sess.run(tf.global_variables_initializer()) - self.assertAllClose(output.eval(), expected.eval(), - atol=1e-4, rtol=1e-4) - - def testUnknownBatchSize(self): - batch = 2 - height, width = 65, 65 - global_pool = True - num_classes = 10 - inputs = create_test_input(None, height, width, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - logits, _ = self._resnet_small(inputs, - num_classes, - global_pool=global_pool, - scope='resnet') - self.assertTrue(logits.op.name.startswith('resnet/logits')) - self.assertListEqual(logits.get_shape().as_list(), - [None, 1, 1, num_classes]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(logits, {inputs: images.eval()}) - self.assertEqual(output.shape, (batch, 1, 1, num_classes)) - - def testFullyConvolutionalUnknownHeightWidth(self): - batch = 2 - height, width = 65, 65 - global_pool = False - inputs = create_test_input(batch, None, None, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - output, _ = self._resnet_small(inputs, - None, - global_pool=global_pool) - self.assertListEqual(output.get_shape().as_list(), - [batch, None, None, 32]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(output, {inputs: images.eval()}) - self.assertEqual(output.shape, (batch, 3, 3, 32)) - - def testAtrousFullyConvolutionalUnknownHeightWidth(self): - batch = 2 - height, width = 65, 65 - global_pool = False - output_stride = 8 - inputs = create_test_input(batch, None, None, 3) - with slim.arg_scope(resnet_utils.resnet_arg_scope()): - output, _ = self._resnet_small(inputs, - None, - global_pool=global_pool, - output_stride=output_stride) - self.assertListEqual(output.get_shape().as_list(), - [batch, None, None, 32]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(output, {inputs: images.eval()}) - self.assertEqual(output.shape, (batch, 9, 9, 32)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/core/utils.py b/research/deeplab/core/utils.py deleted file mode 100644 index 4bf3d09ad46..00000000000 --- a/research/deeplab/core/utils.py +++ /dev/null @@ -1,214 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""This script contains utility functions.""" -import tensorflow as tf -from tensorflow.contrib import framework as contrib_framework -from tensorflow.contrib import slim as contrib_slim - -slim = contrib_slim - - -# Quantized version of sigmoid function. -q_sigmoid = lambda x: tf.nn.relu6(x + 3) * 0.16667 - - -def resize_bilinear(images, size, output_dtype=tf.float32): - """Returns resized images as output_type. - - Args: - images: A tensor of size [batch, height_in, width_in, channels]. - size: A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size - for the images. - output_dtype: The destination type. - Returns: - A tensor of size [batch, height_out, width_out, channels] as a dtype of - output_dtype. - """ - images = tf.image.resize_bilinear(images, size, align_corners=True) - return tf.cast(images, dtype=output_dtype) - - -def scale_dimension(dim, scale): - """Scales the input dimension. - - Args: - dim: Input dimension (a scalar or a scalar Tensor). - scale: The amount of scaling applied to the input. - - Returns: - Scaled dimension. - """ - if isinstance(dim, tf.Tensor): - return tf.cast((tf.to_float(dim) - 1.0) * scale + 1.0, dtype=tf.int32) - else: - return int((float(dim) - 1.0) * scale + 1.0) - - -def split_separable_conv2d(inputs, - filters, - kernel_size=3, - rate=1, - weight_decay=0.00004, - depthwise_weights_initializer_stddev=0.33, - pointwise_weights_initializer_stddev=0.06, - scope=None): - """Splits a separable conv2d into depthwise and pointwise conv2d. - - This operation differs from `tf.layers.separable_conv2d` as this operation - applies activation function between depthwise and pointwise conv2d. - - Args: - inputs: Input tensor with shape [batch, height, width, channels]. - filters: Number of filters in the 1x1 pointwise convolution. - kernel_size: A list of length 2: [kernel_height, kernel_width] of - of the filters. Can be an int if both values are the same. - rate: Atrous convolution rate for the depthwise convolution. - weight_decay: The weight decay to use for regularizing the model. - depthwise_weights_initializer_stddev: The standard deviation of the - truncated normal weight initializer for depthwise convolution. - pointwise_weights_initializer_stddev: The standard deviation of the - truncated normal weight initializer for pointwise convolution. - scope: Optional scope for the operation. - - Returns: - Computed features after split separable conv2d. - """ - outputs = slim.separable_conv2d( - inputs, - None, - kernel_size=kernel_size, - depth_multiplier=1, - rate=rate, - weights_initializer=tf.truncated_normal_initializer( - stddev=depthwise_weights_initializer_stddev), - weights_regularizer=None, - scope=scope + '_depthwise') - return slim.conv2d( - outputs, - filters, - 1, - weights_initializer=tf.truncated_normal_initializer( - stddev=pointwise_weights_initializer_stddev), - weights_regularizer=slim.l2_regularizer(weight_decay), - scope=scope + '_pointwise') - - -def get_label_weight_mask(labels, ignore_label, num_classes, label_weights=1.0): - """Gets the label weight mask. - - Args: - labels: A Tensor of labels with the shape of [-1]. - ignore_label: Integer, label to ignore. - num_classes: Integer, the number of semantic classes. - label_weights: A float or a list of weights. If it is a float, it means all - the labels have the same weight. If it is a list of weights, then each - element in the list represents the weight for the label of its index, for - example, label_weights = [0.1, 0.5] means the weight for label 0 is 0.1 - and the weight for label 1 is 0.5. - - Returns: - A Tensor of label weights with the same shape of labels, each element is the - weight for the label with the same index in labels and the element is 0.0 - if the label is to ignore. - - Raises: - ValueError: If label_weights is neither a float nor a list, or if - label_weights is a list and its length is not equal to num_classes. - """ - if not isinstance(label_weights, (float, list)): - raise ValueError( - 'The type of label_weights is invalid, it must be a float or a list.') - - if isinstance(label_weights, list) and len(label_weights) != num_classes: - raise ValueError( - 'Length of label_weights must be equal to num_classes if it is a list, ' - 'label_weights: %s, num_classes: %d.' % (label_weights, num_classes)) - - not_ignore_mask = tf.not_equal(labels, ignore_label) - not_ignore_mask = tf.cast(not_ignore_mask, tf.float32) - if isinstance(label_weights, float): - return not_ignore_mask * label_weights - - label_weights = tf.constant(label_weights, tf.float32) - weight_mask = tf.einsum('...y,y->...', - tf.one_hot(labels, num_classes, dtype=tf.float32), - label_weights) - return tf.multiply(not_ignore_mask, weight_mask) - - -def get_batch_norm_fn(sync_batch_norm_method): - """Gets batch norm function. - - Currently we only support the following methods: - - `None` (no sync batch norm). We use slim.batch_norm in this case. - - Args: - sync_batch_norm_method: String, method used to sync batch norm. - - Returns: - Batchnorm function. - - Raises: - ValueError: If sync_batch_norm_method is not supported. - """ - if sync_batch_norm_method == 'None': - return slim.batch_norm - else: - raise ValueError('Unsupported sync_batch_norm_method.') - - -def get_batch_norm_params(decay=0.9997, - epsilon=1e-5, - center=True, - scale=True, - is_training=True, - sync_batch_norm_method='None', - initialize_gamma_as_zeros=False): - """Gets batch norm parameters. - - Args: - decay: Float, decay for the moving average. - epsilon: Float, value added to variance to avoid dividing by zero. - center: Boolean. If True, add offset of `beta` to normalized tensor. If - False,`beta` is ignored. - scale: Boolean. If True, multiply by `gamma`. If False, `gamma` is not used. - is_training: Boolean, whether or not the layer is in training mode. - sync_batch_norm_method: String, method used to sync batch norm. - initialize_gamma_as_zeros: Boolean, initializing `gamma` as zeros or not. - - Returns: - A dictionary for batchnorm parameters. - - Raises: - ValueError: If sync_batch_norm_method is not supported. - """ - batch_norm_params = { - 'is_training': is_training, - 'decay': decay, - 'epsilon': epsilon, - 'scale': scale, - 'center': center, - } - if initialize_gamma_as_zeros: - if sync_batch_norm_method == 'None': - # Slim-type gamma_initialier. - batch_norm_params['param_initializers'] = { - 'gamma': tf.zeros_initializer(), - } - else: - raise ValueError('Unsupported sync_batch_norm_method.') - return batch_norm_params diff --git a/research/deeplab/core/utils_test.py b/research/deeplab/core/utils_test.py deleted file mode 100644 index cfdb63ef2d3..00000000000 --- a/research/deeplab/core/utils_test.py +++ /dev/null @@ -1,90 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for utils.py.""" - -import numpy as np -import tensorflow as tf - -from deeplab.core import utils - - -class UtilsTest(tf.test.TestCase): - - def testScaleDimensionOutput(self): - self.assertEqual(161, utils.scale_dimension(321, 0.5)) - self.assertEqual(193, utils.scale_dimension(321, 0.6)) - self.assertEqual(241, utils.scale_dimension(321, 0.75)) - - def testGetLabelWeightMask_withFloatLabelWeights(self): - labels = tf.constant([0, 4, 1, 3, 2]) - ignore_label = 4 - num_classes = 5 - label_weights = 0.5 - expected_label_weight_mask = np.array([0.5, 0.0, 0.5, 0.5, 0.5], - dtype=np.float32) - - with self.test_session() as sess: - label_weight_mask = utils.get_label_weight_mask( - labels, ignore_label, num_classes, label_weights=label_weights) - label_weight_mask = sess.run(label_weight_mask) - self.assertAllEqual(label_weight_mask, expected_label_weight_mask) - - def testGetLabelWeightMask_withListLabelWeights(self): - labels = tf.constant([0, 4, 1, 3, 2]) - ignore_label = 4 - num_classes = 5 - label_weights = [0.0, 0.1, 0.2, 0.3, 0.4] - expected_label_weight_mask = np.array([0.0, 0.0, 0.1, 0.3, 0.2], - dtype=np.float32) - - with self.test_session() as sess: - label_weight_mask = utils.get_label_weight_mask( - labels, ignore_label, num_classes, label_weights=label_weights) - label_weight_mask = sess.run(label_weight_mask) - self.assertAllEqual(label_weight_mask, expected_label_weight_mask) - - def testGetLabelWeightMask_withInvalidLabelWeightsType(self): - labels = tf.constant([0, 4, 1, 3, 2]) - ignore_label = 4 - num_classes = 5 - - self.assertRaisesWithRegexpMatch( - ValueError, - '^The type of label_weights is invalid, it must be a float or a list', - utils.get_label_weight_mask, - labels=labels, - ignore_label=ignore_label, - num_classes=num_classes, - label_weights=None) - - def testGetLabelWeightMask_withInvalidLabelWeightsLength(self): - labels = tf.constant([0, 4, 1, 3, 2]) - ignore_label = 4 - num_classes = 5 - label_weights = [0.0, 0.1, 0.2] - - self.assertRaisesWithRegexpMatch( - ValueError, - '^Length of label_weights must be equal to num_classes if it is a list', - utils.get_label_weight_mask, - labels=labels, - ignore_label=ignore_label, - num_classes=num_classes, - label_weights=label_weights) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/core/xception.py b/research/deeplab/core/xception.py deleted file mode 100644 index f9925714716..00000000000 --- a/research/deeplab/core/xception.py +++ /dev/null @@ -1,945 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Xception model. - -"Xception: Deep Learning with Depthwise Separable Convolutions" -Fran{\c{c}}ois Chollet -https://arxiv.org/abs/1610.02357 - -We implement the modified version by Jifeng Dai et al. for their COCO 2017 -detection challenge submission, where the model is made deeper and has aligned -features for dense prediction tasks. See their slides for details: - -"Deformable Convolutional Networks -- COCO Detection and Segmentation Challenge -2017 Entry" -Haozhi Qi, Zheng Zhang, Bin Xiao, Han Hu, Bowen Cheng, Yichen Wei and Jifeng Dai -ICCV 2017 COCO Challenge workshop -http://presentations.cocodataset.org/COCO17-Detect-MSRA.pdf - -We made a few more changes on top of MSRA's modifications: -1. Fully convolutional: All the max-pooling layers are replaced with separable - conv2d with stride = 2. This allows us to use atrous convolution to extract - feature maps at any resolution. - -2. We support adding ReLU and BatchNorm after depthwise convolution, motivated - by the design of MobileNetv1. - -"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision -Applications" -Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, -Tobias Weyand, Marco Andreetto, Hartwig Adam -https://arxiv.org/abs/1704.04861 -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -from six.moves import range -import tensorflow as tf -from tensorflow.contrib import slim as contrib_slim - -from deeplab.core import utils -from tensorflow.contrib.slim.nets import resnet_utils -from nets.mobilenet import conv_blocks as mobilenet_v3_ops - -slim = contrib_slim - - -_DEFAULT_MULTI_GRID = [1, 1, 1] -# The cap for tf.clip_by_value. -_CLIP_CAP = 6 - - -class Block(collections.namedtuple('Block', ['scope', 'unit_fn', 'args'])): - """A named tuple describing an Xception block. - - Its parts are: - scope: The scope of the block. - unit_fn: The Xception unit function which takes as input a tensor and - returns another tensor with the output of the Xception unit. - args: A list of length equal to the number of units in the block. The list - contains one dictionary for each unit in the block to serve as argument to - unit_fn. - """ - - -def fixed_padding(inputs, kernel_size, rate=1): - """Pads the input along the spatial dimensions independently of input size. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - kernel_size: The kernel to be used in the conv2d or max_pool2d operation. - Should be a positive integer. - rate: An integer, rate for atrous convolution. - - Returns: - output: A tensor of size [batch, height_out, width_out, channels] with the - input, either intact (if kernel_size == 1) or padded (if kernel_size > 1). - """ - kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) - pad_total = kernel_size_effective - 1 - pad_beg = pad_total // 2 - pad_end = pad_total - pad_beg - padded_inputs = tf.pad(inputs, [[0, 0], [pad_beg, pad_end], - [pad_beg, pad_end], [0, 0]]) - return padded_inputs - - -@slim.add_arg_scope -def separable_conv2d_same(inputs, - num_outputs, - kernel_size, - depth_multiplier, - stride, - rate=1, - use_explicit_padding=True, - regularize_depthwise=False, - scope=None, - **kwargs): - """Strided 2-D separable convolution with 'SAME' padding. - - If stride > 1 and use_explicit_padding is True, then we do explicit zero- - padding, followed by conv2d with 'VALID' padding. - - Note that - - net = separable_conv2d_same(inputs, num_outputs, 3, - depth_multiplier=1, stride=stride) - - is equivalent to - - net = slim.separable_conv2d(inputs, num_outputs, 3, - depth_multiplier=1, stride=1, padding='SAME') - net = resnet_utils.subsample(net, factor=stride) - - whereas - - net = slim.separable_conv2d(inputs, num_outputs, 3, stride=stride, - depth_multiplier=1, padding='SAME') - - is different when the input's height or width is even, which is why we add the - current function. - - Consequently, if the input feature map has even height or width, setting - `use_explicit_padding=False` will result in feature misalignment by one pixel - along the corresponding dimension. - - Args: - inputs: A 4-D tensor of size [batch, height_in, width_in, channels]. - num_outputs: An integer, the number of output filters. - kernel_size: An int with the kernel_size of the filters. - depth_multiplier: The number of depthwise convolution output channels for - each input channel. The total number of depthwise convolution output - channels will be equal to `num_filters_in * depth_multiplier`. - stride: An integer, the output stride. - rate: An integer, rate for atrous convolution. - use_explicit_padding: If True, use explicit padding to make the model fully - compatible with the open source version, otherwise use the native - Tensorflow 'SAME' padding. - regularize_depthwise: Whether or not apply L2-norm regularization on the - depthwise convolution weights. - scope: Scope. - **kwargs: additional keyword arguments to pass to slim.conv2d - - Returns: - output: A 4-D tensor of size [batch, height_out, width_out, channels] with - the convolution output. - """ - def _separable_conv2d(padding): - """Wrapper for separable conv2d.""" - return slim.separable_conv2d(inputs, - num_outputs, - kernel_size, - depth_multiplier=depth_multiplier, - stride=stride, - rate=rate, - padding=padding, - scope=scope, - **kwargs) - def _split_separable_conv2d(padding): - """Splits separable conv2d into depthwise and pointwise conv2d.""" - outputs = slim.separable_conv2d(inputs, - None, - kernel_size, - depth_multiplier=depth_multiplier, - stride=stride, - rate=rate, - padding=padding, - scope=scope + '_depthwise', - **kwargs) - return slim.conv2d(outputs, - num_outputs, - 1, - scope=scope + '_pointwise', - **kwargs) - if stride == 1 or not use_explicit_padding: - if regularize_depthwise: - outputs = _separable_conv2d(padding='SAME') - else: - outputs = _split_separable_conv2d(padding='SAME') - else: - inputs = fixed_padding(inputs, kernel_size, rate) - if regularize_depthwise: - outputs = _separable_conv2d(padding='VALID') - else: - outputs = _split_separable_conv2d(padding='VALID') - return outputs - - -@slim.add_arg_scope -def xception_module(inputs, - depth_list, - skip_connection_type, - stride, - kernel_size=3, - unit_rate_list=None, - rate=1, - activation_fn_in_separable_conv=False, - regularize_depthwise=False, - outputs_collections=None, - scope=None, - use_bounded_activation=False, - use_explicit_padding=True, - use_squeeze_excite=False, - se_pool_size=None): - """An Xception module. - - The output of one Xception module is equal to the sum of `residual` and - `shortcut`, where `residual` is the feature computed by three separable - convolution. The `shortcut` is the feature computed by 1x1 convolution with - or without striding. In some cases, the `shortcut` path could be a simple - identity function or none (i.e, no shortcut). - - Note that we replace the max pooling operations in the Xception module with - another separable convolution with striding, since atrous rate is not properly - supported in current TensorFlow max pooling implementation. - - Args: - inputs: A tensor of size [batch, height, width, channels]. - depth_list: A list of three integers specifying the depth values of one - Xception module. - skip_connection_type: Skip connection type for the residual path. Only - supports 'conv', 'sum', or 'none'. - stride: The block unit's stride. Determines the amount of downsampling of - the units output compared to its input. - kernel_size: Integer, convolution kernel size. - unit_rate_list: A list of three integers, determining the unit rate for - each separable convolution in the xception module. - rate: An integer, rate for atrous convolution. - activation_fn_in_separable_conv: Includes activation function in the - separable convolution or not. - regularize_depthwise: Whether or not apply L2-norm regularization on the - depthwise convolution weights. - outputs_collections: Collection to add the Xception unit output. - scope: Optional variable_scope. - use_bounded_activation: Whether or not to use bounded activations. Bounded - activations better lend themselves to quantized inference. - use_explicit_padding: If True, use explicit padding to make the model fully - compatible with the open source version, otherwise use the native - Tensorflow 'SAME' padding. - use_squeeze_excite: Boolean, use squeeze-and-excitation or not. - se_pool_size: None or integer specifying the pooling size used in SE module. - - Returns: - The Xception module's output. - - Raises: - ValueError: If depth_list and unit_rate_list do not contain three elements, - or if stride != 1 for the third separable convolution operation in the - residual path, or unsupported skip connection type. - """ - if len(depth_list) != 3: - raise ValueError('Expect three elements in depth_list.') - if unit_rate_list: - if len(unit_rate_list) != 3: - raise ValueError('Expect three elements in unit_rate_list.') - - with tf.variable_scope(scope, 'xception_module', [inputs]) as sc: - residual = inputs - - def _separable_conv(features, depth, kernel_size, depth_multiplier, - regularize_depthwise, rate, stride, scope): - """Separable conv block.""" - if activation_fn_in_separable_conv: - activation_fn = tf.nn.relu6 if use_bounded_activation else tf.nn.relu - else: - if use_bounded_activation: - # When use_bounded_activation is True, we clip the feature values and - # apply relu6 for activation. - activation_fn = lambda x: tf.clip_by_value(x, -_CLIP_CAP, _CLIP_CAP) - features = tf.nn.relu6(features) - else: - # Original network design. - activation_fn = None - features = tf.nn.relu(features) - return separable_conv2d_same(features, - depth, - kernel_size, - depth_multiplier=depth_multiplier, - stride=stride, - rate=rate, - activation_fn=activation_fn, - use_explicit_padding=use_explicit_padding, - regularize_depthwise=regularize_depthwise, - scope=scope) - for i in range(3): - residual = _separable_conv(residual, - depth_list[i], - kernel_size=kernel_size, - depth_multiplier=1, - regularize_depthwise=regularize_depthwise, - rate=rate*unit_rate_list[i], - stride=stride if i == 2 else 1, - scope='separable_conv' + str(i+1)) - if use_squeeze_excite: - residual = mobilenet_v3_ops.squeeze_excite( - input_tensor=residual, - squeeze_factor=16, - inner_activation_fn=tf.nn.relu, - gating_fn=lambda x: tf.nn.relu6(x+3)*0.16667, - pool=se_pool_size) - - if skip_connection_type == 'conv': - shortcut = slim.conv2d(inputs, - depth_list[-1], - [1, 1], - stride=stride, - activation_fn=None, - scope='shortcut') - if use_bounded_activation: - residual = tf.clip_by_value(residual, -_CLIP_CAP, _CLIP_CAP) - shortcut = tf.clip_by_value(shortcut, -_CLIP_CAP, _CLIP_CAP) - outputs = residual + shortcut - if use_bounded_activation: - outputs = tf.nn.relu6(outputs) - elif skip_connection_type == 'sum': - if use_bounded_activation: - residual = tf.clip_by_value(residual, -_CLIP_CAP, _CLIP_CAP) - inputs = tf.clip_by_value(inputs, -_CLIP_CAP, _CLIP_CAP) - outputs = residual + inputs - if use_bounded_activation: - outputs = tf.nn.relu6(outputs) - elif skip_connection_type == 'none': - outputs = residual - else: - raise ValueError('Unsupported skip connection type.') - - return slim.utils.collect_named_outputs(outputs_collections, - sc.name, - outputs) - - -@slim.add_arg_scope -def stack_blocks_dense(net, - blocks, - output_stride=None, - outputs_collections=None): - """Stacks Xception blocks and controls output feature density. - - First, this function creates scopes for the Xception in the form of - 'block_name/unit_1', 'block_name/unit_2', etc. - - Second, this function allows the user to explicitly control the output - stride, which is the ratio of the input to output spatial resolution. This - is useful for dense prediction tasks such as semantic segmentation or - object detection. - - Control of the output feature density is implemented by atrous convolution. - - Args: - net: A tensor of size [batch, height, width, channels]. - blocks: A list of length equal to the number of Xception blocks. Each - element is an Xception Block object describing the units in the block. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution, which needs to be equal to - the product of unit strides from the start up to some level of Xception. - For example, if the Xception employs units with strides 1, 2, 1, 3, 4, 1, - then valid values for the output_stride are 1, 2, 6, 24 or None (which - is equivalent to output_stride=24). - outputs_collections: Collection to add the Xception block outputs. - - Returns: - net: Output tensor with stride equal to the specified output_stride. - - Raises: - ValueError: If the target output_stride is not valid. - """ - # The current_stride variable keeps track of the effective stride of the - # activations. This allows us to invoke atrous convolution whenever applying - # the next residual unit would result in the activations having stride larger - # than the target output_stride. - current_stride = 1 - - # The atrous convolution rate parameter. - rate = 1 - - for block in blocks: - with tf.variable_scope(block.scope, 'block', [net]) as sc: - for i, unit in enumerate(block.args): - if output_stride is not None and current_stride > output_stride: - raise ValueError('The target output_stride cannot be reached.') - with tf.variable_scope('unit_%d' % (i + 1), values=[net]): - # If we have reached the target output_stride, then we need to employ - # atrous convolution with stride=1 and multiply the atrous rate by the - # current unit's stride for use in subsequent layers. - if output_stride is not None and current_stride == output_stride: - net = block.unit_fn(net, rate=rate, **dict(unit, stride=1)) - rate *= unit.get('stride', 1) - else: - net = block.unit_fn(net, rate=1, **unit) - current_stride *= unit.get('stride', 1) - - # Collect activations at the block's end before performing subsampling. - net = slim.utils.collect_named_outputs(outputs_collections, sc.name, net) - - if output_stride is not None and current_stride != output_stride: - raise ValueError('The target output_stride cannot be reached.') - - return net - - -def xception(inputs, - blocks, - num_classes=None, - is_training=True, - global_pool=True, - keep_prob=0.5, - output_stride=None, - reuse=None, - scope=None, - sync_batch_norm_method='None'): - """Generator for Xception models. - - This function generates a family of Xception models. See the xception_*() - methods for specific model instantiations, obtained by selecting different - block instantiations that produce Xception of various depths. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. Must be - floating point. If a pretrained checkpoint is used, pixel values should be - the same as during training (see go/slim-classification-models for - specifics). - blocks: A list of length equal to the number of Xception blocks. Each - element is an Xception Block object describing the units in the block. - num_classes: Number of predicted classes for classification tasks. - If 0 or None, we return the features before the logit layer. - is_training: whether batch_norm layers are in training mode. - global_pool: If True, we perform global average pooling before computing the - logits. Set to True for image classification, False for dense prediction. - keep_prob: Keep probability used in the pre-logits dropout layer. - output_stride: If None, then the output will be computed at the nominal - network stride. If output_stride is not None, it specifies the requested - ratio of input to output spatial resolution. - reuse: whether or not the network and its variables should be reused. To be - able to reuse 'scope' must be given. - scope: Optional variable_scope. - sync_batch_norm_method: String, sync batchnorm method. Currently only - support `None`. - - Returns: - net: A rank-4 tensor of size [batch, height_out, width_out, channels_out]. - If global_pool is False, then height_out and width_out are reduced by a - factor of output_stride compared to the respective height_in and width_in, - else both height_out and width_out equal one. If num_classes is 0 or None, - then net is the output of the last Xception block, potentially after - global average pooling. If num_classes is a non-zero integer, net contains - the pre-softmax activations. - end_points: A dictionary from components of the network to the corresponding - activation. - - Raises: - ValueError: If the target output_stride is not valid. - """ - with tf.variable_scope( - scope, 'xception', [inputs], reuse=reuse) as sc: - end_points_collection = sc.original_name_scope + 'end_points' - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - with slim.arg_scope([slim.conv2d, - slim.separable_conv2d, - xception_module, - stack_blocks_dense], - outputs_collections=end_points_collection): - with slim.arg_scope([batch_norm], is_training=is_training): - net = inputs - if output_stride is not None: - if output_stride % 2 != 0: - raise ValueError('The output_stride needs to be a multiple of 2.') - output_stride //= 2 - # Root block function operated on inputs. - net = resnet_utils.conv2d_same(net, 32, 3, stride=2, - scope='entry_flow/conv1_1') - net = resnet_utils.conv2d_same(net, 64, 3, stride=1, - scope='entry_flow/conv1_2') - - # Extract features for entry_flow, middle_flow, and exit_flow. - net = stack_blocks_dense(net, blocks, output_stride) - - # Convert end_points_collection into a dictionary of end_points. - end_points = slim.utils.convert_collection_to_dict( - end_points_collection, clear_collection=True) - - if global_pool: - # Global average pooling. - net = tf.reduce_mean(net, [1, 2], name='global_pool', keepdims=True) - end_points['global_pool'] = net - if num_classes: - net = slim.dropout(net, keep_prob=keep_prob, is_training=is_training, - scope='prelogits_dropout') - net = slim.conv2d(net, num_classes, [1, 1], activation_fn=None, - normalizer_fn=None, scope='logits') - end_points[sc.name + '/logits'] = net - end_points['predictions'] = slim.softmax(net, scope='predictions') - return net, end_points - - -def xception_block(scope, - depth_list, - skip_connection_type, - activation_fn_in_separable_conv, - regularize_depthwise, - num_units, - stride, - kernel_size=3, - unit_rate_list=None, - use_squeeze_excite=False, - se_pool_size=None): - """Helper function for creating a Xception block. - - Args: - scope: The scope of the block. - depth_list: The depth of the bottleneck layer for each unit. - skip_connection_type: Skip connection type for the residual path. Only - supports 'conv', 'sum', or 'none'. - activation_fn_in_separable_conv: Includes activation function in the - separable convolution or not. - regularize_depthwise: Whether or not apply L2-norm regularization on the - depthwise convolution weights. - num_units: The number of units in the block. - stride: The stride of the block, implemented as a stride in the last unit. - All other units have stride=1. - kernel_size: Integer, convolution kernel size. - unit_rate_list: A list of three integers, determining the unit rate in the - corresponding xception block. - use_squeeze_excite: Boolean, use squeeze-and-excitation or not. - se_pool_size: None or integer specifying the pooling size used in SE module. - - Returns: - An Xception block. - """ - if unit_rate_list is None: - unit_rate_list = _DEFAULT_MULTI_GRID - return Block(scope, xception_module, [{ - 'depth_list': depth_list, - 'skip_connection_type': skip_connection_type, - 'activation_fn_in_separable_conv': activation_fn_in_separable_conv, - 'regularize_depthwise': regularize_depthwise, - 'stride': stride, - 'kernel_size': kernel_size, - 'unit_rate_list': unit_rate_list, - 'use_squeeze_excite': use_squeeze_excite, - 'se_pool_size': se_pool_size, - }] * num_units) - - -def xception_41(inputs, - num_classes=None, - is_training=True, - global_pool=True, - keep_prob=0.5, - output_stride=None, - regularize_depthwise=False, - multi_grid=None, - reuse=None, - scope='xception_41', - sync_batch_norm_method='None'): - """Xception-41 model.""" - blocks = [ - xception_block('entry_flow/block1', - depth_list=[128, 128, 128], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - xception_block('entry_flow/block2', - depth_list=[256, 256, 256], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - xception_block('entry_flow/block3', - depth_list=[728, 728, 728], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - xception_block('middle_flow/block1', - depth_list=[728, 728, 728], - skip_connection_type='sum', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=8, - stride=1), - xception_block('exit_flow/block1', - depth_list=[728, 1024, 1024], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - xception_block('exit_flow/block2', - depth_list=[1536, 1536, 2048], - skip_connection_type='none', - activation_fn_in_separable_conv=True, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=1, - unit_rate_list=multi_grid), - ] - return xception(inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - keep_prob=keep_prob, - output_stride=output_stride, - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def xception_65_factory(inputs, - num_classes=None, - is_training=True, - global_pool=True, - keep_prob=0.5, - output_stride=None, - regularize_depthwise=False, - kernel_size=3, - multi_grid=None, - reuse=None, - use_squeeze_excite=False, - se_pool_size=None, - scope='xception_65', - sync_batch_norm_method='None'): - """Xception-65 model factory.""" - blocks = [ - xception_block('entry_flow/block1', - depth_list=[128, 128, 128], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=False, - se_pool_size=se_pool_size), - xception_block('entry_flow/block2', - depth_list=[256, 256, 256], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=False, - se_pool_size=se_pool_size), - xception_block('entry_flow/block3', - depth_list=[728, 728, 728], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=use_squeeze_excite, - se_pool_size=se_pool_size), - xception_block('middle_flow/block1', - depth_list=[728, 728, 728], - skip_connection_type='sum', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=16, - stride=1, - kernel_size=kernel_size, - use_squeeze_excite=use_squeeze_excite, - se_pool_size=se_pool_size), - xception_block('exit_flow/block1', - depth_list=[728, 1024, 1024], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=use_squeeze_excite, - se_pool_size=se_pool_size), - xception_block('exit_flow/block2', - depth_list=[1536, 1536, 2048], - skip_connection_type='none', - activation_fn_in_separable_conv=True, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=1, - kernel_size=kernel_size, - unit_rate_list=multi_grid, - use_squeeze_excite=False, - se_pool_size=se_pool_size), - ] - return xception(inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - keep_prob=keep_prob, - output_stride=output_stride, - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def xception_65(inputs, - num_classes=None, - is_training=True, - global_pool=True, - keep_prob=0.5, - output_stride=None, - regularize_depthwise=False, - multi_grid=None, - reuse=None, - scope='xception_65', - sync_batch_norm_method='None'): - """Xception-65 model.""" - return xception_65_factory( - inputs=inputs, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - keep_prob=keep_prob, - output_stride=output_stride, - regularize_depthwise=regularize_depthwise, - multi_grid=multi_grid, - reuse=reuse, - scope=scope, - use_squeeze_excite=False, - se_pool_size=None, - sync_batch_norm_method=sync_batch_norm_method) - - -def xception_71_factory(inputs, - num_classes=None, - is_training=True, - global_pool=True, - keep_prob=0.5, - output_stride=None, - regularize_depthwise=False, - kernel_size=3, - multi_grid=None, - reuse=None, - scope='xception_71', - use_squeeze_excite=False, - se_pool_size=None, - sync_batch_norm_method='None'): - """Xception-71 model factory.""" - blocks = [ - xception_block('entry_flow/block1', - depth_list=[128, 128, 128], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=False, - se_pool_size=se_pool_size), - xception_block('entry_flow/block2', - depth_list=[256, 256, 256], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=1, - kernel_size=kernel_size, - use_squeeze_excite=False, - se_pool_size=se_pool_size), - xception_block('entry_flow/block3', - depth_list=[256, 256, 256], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=False, - se_pool_size=se_pool_size), - xception_block('entry_flow/block4', - depth_list=[728, 728, 728], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=1, - kernel_size=kernel_size, - use_squeeze_excite=use_squeeze_excite, - se_pool_size=se_pool_size), - xception_block('entry_flow/block5', - depth_list=[728, 728, 728], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=use_squeeze_excite, - se_pool_size=se_pool_size), - xception_block('middle_flow/block1', - depth_list=[728, 728, 728], - skip_connection_type='sum', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=16, - stride=1, - kernel_size=kernel_size, - use_squeeze_excite=use_squeeze_excite, - se_pool_size=se_pool_size), - xception_block('exit_flow/block1', - depth_list=[728, 1024, 1024], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2, - kernel_size=kernel_size, - use_squeeze_excite=use_squeeze_excite, - se_pool_size=se_pool_size), - xception_block('exit_flow/block2', - depth_list=[1536, 1536, 2048], - skip_connection_type='none', - activation_fn_in_separable_conv=True, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=1, - kernel_size=kernel_size, - unit_rate_list=multi_grid, - use_squeeze_excite=False, - se_pool_size=se_pool_size), - ] - return xception(inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - keep_prob=keep_prob, - output_stride=output_stride, - reuse=reuse, - scope=scope, - sync_batch_norm_method=sync_batch_norm_method) - - -def xception_71(inputs, - num_classes=None, - is_training=True, - global_pool=True, - keep_prob=0.5, - output_stride=None, - regularize_depthwise=False, - multi_grid=None, - reuse=None, - scope='xception_71', - sync_batch_norm_method='None'): - """Xception-71 model.""" - return xception_71_factory( - inputs=inputs, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - keep_prob=keep_prob, - output_stride=output_stride, - regularize_depthwise=regularize_depthwise, - multi_grid=multi_grid, - reuse=reuse, - scope=scope, - use_squeeze_excite=False, - se_pool_size=None, - sync_batch_norm_method=sync_batch_norm_method) - - -def xception_arg_scope(weight_decay=0.00004, - batch_norm_decay=0.9997, - batch_norm_epsilon=0.001, - batch_norm_scale=True, - weights_initializer_stddev=0.09, - regularize_depthwise=False, - use_batch_norm=True, - use_bounded_activation=False, - sync_batch_norm_method='None'): - """Defines the default Xception arg scope. - - Args: - weight_decay: The weight decay to use for regularizing the model. - batch_norm_decay: The moving average decay when estimating layer activation - statistics in batch normalization. - batch_norm_epsilon: Small constant to prevent division by zero when - normalizing activations by their variance in batch normalization. - batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the - activations in the batch normalization layer. - weights_initializer_stddev: The standard deviation of the trunctated normal - weight initializer. - regularize_depthwise: Whether or not apply L2-norm regularization on the - depthwise convolution weights. - use_batch_norm: Whether or not to use batch normalization. - use_bounded_activation: Whether or not to use bounded activations. Bounded - activations better lend themselves to quantized inference. - sync_batch_norm_method: String, sync batchnorm method. Currently only - support `None`. Also, it is only effective for Xception. - - Returns: - An `arg_scope` to use for the Xception models. - """ - batch_norm_params = { - 'decay': batch_norm_decay, - 'epsilon': batch_norm_epsilon, - 'scale': batch_norm_scale, - } - if regularize_depthwise: - depthwise_regularizer = slim.l2_regularizer(weight_decay) - else: - depthwise_regularizer = None - activation_fn = tf.nn.relu6 if use_bounded_activation else tf.nn.relu - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - with slim.arg_scope( - [slim.conv2d, slim.separable_conv2d], - weights_initializer=tf.truncated_normal_initializer( - stddev=weights_initializer_stddev), - activation_fn=activation_fn, - normalizer_fn=batch_norm if use_batch_norm else None): - with slim.arg_scope([batch_norm], **batch_norm_params): - with slim.arg_scope( - [slim.conv2d], - weights_regularizer=slim.l2_regularizer(weight_decay)): - with slim.arg_scope( - [slim.separable_conv2d], - weights_regularizer=depthwise_regularizer): - with slim.arg_scope( - [xception_module], - use_bounded_activation=use_bounded_activation, - use_explicit_padding=not use_bounded_activation) as arg_sc: - return arg_sc diff --git a/research/deeplab/core/xception_test.py b/research/deeplab/core/xception_test.py deleted file mode 100644 index fc338daa6e5..00000000000 --- a/research/deeplab/core/xception_test.py +++ /dev/null @@ -1,488 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for xception.py.""" -import numpy as np -import six -import tensorflow as tf -from tensorflow.contrib import slim as contrib_slim - -from deeplab.core import xception -from tensorflow.contrib.slim.nets import resnet_utils - -slim = contrib_slim - - -def create_test_input(batch, height, width, channels): - """Create test input tensor.""" - if None in [batch, height, width, channels]: - return tf.placeholder(tf.float32, (batch, height, width, channels)) - else: - return tf.cast( - np.tile( - np.reshape( - np.reshape(np.arange(height), [height, 1]) + - np.reshape(np.arange(width), [1, width]), - [1, height, width, 1]), - [batch, 1, 1, channels]), - tf.float32) - - -class UtilityFunctionTest(tf.test.TestCase): - - def testSeparableConv2DSameWithInputEvenSize(self): - n, n2 = 4, 2 - - # Input image. - x = create_test_input(1, n, n, 1) - - # Convolution kernel. - dw = create_test_input(1, 3, 3, 1) - dw = tf.reshape(dw, [3, 3, 1, 1]) - - tf.get_variable('Conv/depthwise_weights', initializer=dw) - tf.get_variable('Conv/pointwise_weights', - initializer=tf.ones([1, 1, 1, 1])) - tf.get_variable('Conv/biases', initializer=tf.zeros([1])) - tf.get_variable_scope().reuse_variables() - - y1 = slim.separable_conv2d(x, 1, [3, 3], depth_multiplier=1, - stride=1, scope='Conv') - y1_expected = tf.cast([[14, 28, 43, 26], - [28, 48, 66, 37], - [43, 66, 84, 46], - [26, 37, 46, 22]], tf.float32) - y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) - - y2 = resnet_utils.subsample(y1, 2) - y2_expected = tf.cast([[14, 43], - [43, 84]], tf.float32) - y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1]) - - y3 = xception.separable_conv2d_same(x, 1, 3, depth_multiplier=1, - regularize_depthwise=True, - stride=2, scope='Conv') - y3_expected = y2_expected - - y4 = slim.separable_conv2d(x, 1, [3, 3], depth_multiplier=1, - stride=2, scope='Conv') - y4_expected = tf.cast([[48, 37], - [37, 22]], tf.float32) - y4_expected = tf.reshape(y4_expected, [1, n2, n2, 1]) - - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - self.assertAllClose(y1.eval(), y1_expected.eval()) - self.assertAllClose(y2.eval(), y2_expected.eval()) - self.assertAllClose(y3.eval(), y3_expected.eval()) - self.assertAllClose(y4.eval(), y4_expected.eval()) - - def testSeparableConv2DSameWithInputOddSize(self): - n, n2 = 5, 3 - - # Input image. - x = create_test_input(1, n, n, 1) - - # Convolution kernel. - dw = create_test_input(1, 3, 3, 1) - dw = tf.reshape(dw, [3, 3, 1, 1]) - - tf.get_variable('Conv/depthwise_weights', initializer=dw) - tf.get_variable('Conv/pointwise_weights', - initializer=tf.ones([1, 1, 1, 1])) - tf.get_variable('Conv/biases', initializer=tf.zeros([1])) - tf.get_variable_scope().reuse_variables() - - y1 = slim.separable_conv2d(x, 1, [3, 3], depth_multiplier=1, - stride=1, scope='Conv') - y1_expected = tf.cast([[14, 28, 43, 58, 34], - [28, 48, 66, 84, 46], - [43, 66, 84, 102, 55], - [58, 84, 102, 120, 64], - [34, 46, 55, 64, 30]], tf.float32) - y1_expected = tf.reshape(y1_expected, [1, n, n, 1]) - - y2 = resnet_utils.subsample(y1, 2) - y2_expected = tf.cast([[14, 43, 34], - [43, 84, 55], - [34, 55, 30]], tf.float32) - y2_expected = tf.reshape(y2_expected, [1, n2, n2, 1]) - - y3 = xception.separable_conv2d_same(x, 1, 3, depth_multiplier=1, - regularize_depthwise=True, - stride=2, scope='Conv') - y3_expected = y2_expected - - y4 = slim.separable_conv2d(x, 1, [3, 3], depth_multiplier=1, - stride=2, scope='Conv') - y4_expected = y2_expected - - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - self.assertAllClose(y1.eval(), y1_expected.eval()) - self.assertAllClose(y2.eval(), y2_expected.eval()) - self.assertAllClose(y3.eval(), y3_expected.eval()) - self.assertAllClose(y4.eval(), y4_expected.eval()) - - -class XceptionNetworkTest(tf.test.TestCase): - """Tests with small Xception network.""" - - def _xception_small(self, - inputs, - num_classes=None, - is_training=True, - global_pool=True, - output_stride=None, - regularize_depthwise=True, - reuse=None, - scope='xception_small'): - """A shallow and thin Xception for faster tests.""" - block = xception.xception_block - blocks = [ - block('entry_flow/block1', - depth_list=[1, 1, 1], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - block('entry_flow/block2', - depth_list=[2, 2, 2], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - block('entry_flow/block3', - depth_list=[4, 4, 4], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=1), - block('entry_flow/block4', - depth_list=[4, 4, 4], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - block('middle_flow/block1', - depth_list=[4, 4, 4], - skip_connection_type='sum', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=2, - stride=1), - block('exit_flow/block1', - depth_list=[8, 8, 8], - skip_connection_type='conv', - activation_fn_in_separable_conv=False, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=2), - block('exit_flow/block2', - depth_list=[16, 16, 16], - skip_connection_type='none', - activation_fn_in_separable_conv=True, - regularize_depthwise=regularize_depthwise, - num_units=1, - stride=1), - ] - return xception.xception(inputs, - blocks=blocks, - num_classes=num_classes, - is_training=is_training, - global_pool=global_pool, - output_stride=output_stride, - reuse=reuse, - scope=scope) - - def testClassificationEndPoints(self): - global_pool = True - num_classes = 3 - inputs = create_test_input(2, 32, 32, 3) - with slim.arg_scope(xception.xception_arg_scope()): - logits, end_points = self._xception_small( - inputs, - num_classes=num_classes, - global_pool=global_pool, - scope='xception') - self.assertTrue( - logits.op.name.startswith('xception/logits')) - self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes]) - self.assertTrue('predictions' in end_points) - self.assertListEqual(end_points['predictions'].get_shape().as_list(), - [2, 1, 1, num_classes]) - self.assertTrue('global_pool' in end_points) - self.assertListEqual(end_points['global_pool'].get_shape().as_list(), - [2, 1, 1, 16]) - - def testEndpointNames(self): - global_pool = True - num_classes = 3 - inputs = create_test_input(2, 32, 32, 3) - with slim.arg_scope(xception.xception_arg_scope()): - _, end_points = self._xception_small( - inputs, - num_classes=num_classes, - global_pool=global_pool, - scope='xception') - expected = [ - 'xception/entry_flow/conv1_1', - 'xception/entry_flow/conv1_2', - 'xception/entry_flow/block1/unit_1/xception_module/separable_conv1', - 'xception/entry_flow/block1/unit_1/xception_module/separable_conv2', - 'xception/entry_flow/block1/unit_1/xception_module/separable_conv3', - 'xception/entry_flow/block1/unit_1/xception_module/shortcut', - 'xception/entry_flow/block1/unit_1/xception_module', - 'xception/entry_flow/block1', - 'xception/entry_flow/block2/unit_1/xception_module/separable_conv1', - 'xception/entry_flow/block2/unit_1/xception_module/separable_conv2', - 'xception/entry_flow/block2/unit_1/xception_module/separable_conv3', - 'xception/entry_flow/block2/unit_1/xception_module/shortcut', - 'xception/entry_flow/block2/unit_1/xception_module', - 'xception/entry_flow/block2', - 'xception/entry_flow/block3/unit_1/xception_module/separable_conv1', - 'xception/entry_flow/block3/unit_1/xception_module/separable_conv2', - 'xception/entry_flow/block3/unit_1/xception_module/separable_conv3', - 'xception/entry_flow/block3/unit_1/xception_module/shortcut', - 'xception/entry_flow/block3/unit_1/xception_module', - 'xception/entry_flow/block3', - 'xception/entry_flow/block4/unit_1/xception_module/separable_conv1', - 'xception/entry_flow/block4/unit_1/xception_module/separable_conv2', - 'xception/entry_flow/block4/unit_1/xception_module/separable_conv3', - 'xception/entry_flow/block4/unit_1/xception_module/shortcut', - 'xception/entry_flow/block4/unit_1/xception_module', - 'xception/entry_flow/block4', - 'xception/middle_flow/block1/unit_1/xception_module/separable_conv1', - 'xception/middle_flow/block1/unit_1/xception_module/separable_conv2', - 'xception/middle_flow/block1/unit_1/xception_module/separable_conv3', - 'xception/middle_flow/block1/unit_1/xception_module', - 'xception/middle_flow/block1/unit_2/xception_module/separable_conv1', - 'xception/middle_flow/block1/unit_2/xception_module/separable_conv2', - 'xception/middle_flow/block1/unit_2/xception_module/separable_conv3', - 'xception/middle_flow/block1/unit_2/xception_module', - 'xception/middle_flow/block1', - 'xception/exit_flow/block1/unit_1/xception_module/separable_conv1', - 'xception/exit_flow/block1/unit_1/xception_module/separable_conv2', - 'xception/exit_flow/block1/unit_1/xception_module/separable_conv3', - 'xception/exit_flow/block1/unit_1/xception_module/shortcut', - 'xception/exit_flow/block1/unit_1/xception_module', - 'xception/exit_flow/block1', - 'xception/exit_flow/block2/unit_1/xception_module/separable_conv1', - 'xception/exit_flow/block2/unit_1/xception_module/separable_conv2', - 'xception/exit_flow/block2/unit_1/xception_module/separable_conv3', - 'xception/exit_flow/block2/unit_1/xception_module', - 'xception/exit_flow/block2', - 'global_pool', - 'xception/logits', - 'predictions', - ] - self.assertItemsEqual(list(end_points.keys()), expected) - - def testClassificationShapes(self): - global_pool = True - num_classes = 3 - inputs = create_test_input(2, 64, 64, 3) - with slim.arg_scope(xception.xception_arg_scope()): - _, end_points = self._xception_small( - inputs, - num_classes, - global_pool=global_pool, - scope='xception') - endpoint_to_shape = { - 'xception/entry_flow/conv1_1': [2, 32, 32, 32], - 'xception/entry_flow/block1': [2, 16, 16, 1], - 'xception/entry_flow/block2': [2, 8, 8, 2], - 'xception/entry_flow/block4': [2, 4, 4, 4], - 'xception/middle_flow/block1': [2, 4, 4, 4], - 'xception/exit_flow/block1': [2, 2, 2, 8], - 'xception/exit_flow/block2': [2, 2, 2, 16]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testFullyConvolutionalEndpointShapes(self): - global_pool = False - num_classes = 3 - inputs = create_test_input(2, 65, 65, 3) - with slim.arg_scope(xception.xception_arg_scope()): - _, end_points = self._xception_small( - inputs, - num_classes, - global_pool=global_pool, - scope='xception') - endpoint_to_shape = { - 'xception/entry_flow/conv1_1': [2, 33, 33, 32], - 'xception/entry_flow/block1': [2, 17, 17, 1], - 'xception/entry_flow/block2': [2, 9, 9, 2], - 'xception/entry_flow/block4': [2, 5, 5, 4], - 'xception/middle_flow/block1': [2, 5, 5, 4], - 'xception/exit_flow/block1': [2, 3, 3, 8], - 'xception/exit_flow/block2': [2, 3, 3, 16]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testAtrousFullyConvolutionalEndpointShapes(self): - global_pool = False - num_classes = 3 - output_stride = 8 - inputs = create_test_input(2, 65, 65, 3) - with slim.arg_scope(xception.xception_arg_scope()): - _, end_points = self._xception_small( - inputs, - num_classes, - global_pool=global_pool, - output_stride=output_stride, - scope='xception') - endpoint_to_shape = { - 'xception/entry_flow/block1': [2, 17, 17, 1], - 'xception/entry_flow/block2': [2, 9, 9, 2], - 'xception/entry_flow/block4': [2, 9, 9, 4], - 'xception/middle_flow/block1': [2, 9, 9, 4], - 'xception/exit_flow/block1': [2, 9, 9, 8], - 'xception/exit_flow/block2': [2, 9, 9, 16]} - for endpoint, shape in six.iteritems(endpoint_to_shape): - self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape) - - def testAtrousFullyConvolutionalValues(self): - """Verify dense feature extraction with atrous convolution.""" - nominal_stride = 32 - for output_stride in [4, 8, 16, 32, None]: - with slim.arg_scope(xception.xception_arg_scope()): - with tf.Graph().as_default(): - with self.test_session() as sess: - tf.set_random_seed(0) - inputs = create_test_input(2, 96, 97, 3) - # Dense feature extraction followed by subsampling. - output, _ = self._xception_small( - inputs, - None, - is_training=False, - global_pool=False, - output_stride=output_stride) - if output_stride is None: - factor = 1 - else: - factor = nominal_stride // output_stride - output = resnet_utils.subsample(output, factor) - # Make the two networks use the same weights. - tf.get_variable_scope().reuse_variables() - # Feature extraction at the nominal network rate. - expected, _ = self._xception_small( - inputs, - None, - is_training=False, - global_pool=False) - sess.run(tf.global_variables_initializer()) - self.assertAllClose(output.eval(), expected.eval(), - atol=1e-5, rtol=1e-5) - - def testUnknownBatchSize(self): - batch = 2 - height, width = 65, 65 - global_pool = True - num_classes = 10 - inputs = create_test_input(None, height, width, 3) - with slim.arg_scope(xception.xception_arg_scope()): - logits, _ = self._xception_small( - inputs, - num_classes, - global_pool=global_pool, - scope='xception') - self.assertTrue(logits.op.name.startswith('xception/logits')) - self.assertListEqual(logits.get_shape().as_list(), - [None, 1, 1, num_classes]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(logits, {inputs: images.eval()}) - self.assertEquals(output.shape, (batch, 1, 1, num_classes)) - - def testFullyConvolutionalUnknownHeightWidth(self): - batch = 2 - height, width = 65, 65 - global_pool = False - inputs = create_test_input(batch, None, None, 3) - with slim.arg_scope(xception.xception_arg_scope()): - output, _ = self._xception_small( - inputs, - None, - global_pool=global_pool) - self.assertListEqual(output.get_shape().as_list(), - [batch, None, None, 16]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(output, {inputs: images.eval()}) - self.assertEquals(output.shape, (batch, 3, 3, 16)) - - def testAtrousFullyConvolutionalUnknownHeightWidth(self): - batch = 2 - height, width = 65, 65 - global_pool = False - output_stride = 8 - inputs = create_test_input(batch, None, None, 3) - with slim.arg_scope(xception.xception_arg_scope()): - output, _ = self._xception_small( - inputs, - None, - global_pool=global_pool, - output_stride=output_stride) - self.assertListEqual(output.get_shape().as_list(), - [batch, None, None, 16]) - images = create_test_input(batch, height, width, 3) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output = sess.run(output, {inputs: images.eval()}) - self.assertEquals(output.shape, (batch, 9, 9, 16)) - - def testEndpointsReuse(self): - inputs = create_test_input(2, 32, 32, 3) - with slim.arg_scope(xception.xception_arg_scope()): - _, end_points0 = xception.xception_65( - inputs, - num_classes=10, - reuse=False) - with slim.arg_scope(xception.xception_arg_scope()): - _, end_points1 = xception.xception_65( - inputs, - num_classes=10, - reuse=True) - self.assertItemsEqual(list(end_points0.keys()), list(end_points1.keys())) - - def testUseBoundedAcitvation(self): - global_pool = False - num_classes = 3 - output_stride = 16 - for use_bounded_activation in (True, False): - tf.reset_default_graph() - inputs = create_test_input(2, 65, 65, 3) - with slim.arg_scope(xception.xception_arg_scope( - use_bounded_activation=use_bounded_activation)): - _, _ = self._xception_small( - inputs, - num_classes, - global_pool=global_pool, - output_stride=output_stride, - scope='xception') - for node in tf.get_default_graph().as_graph_def().node: - if node.op.startswith('Relu'): - self.assertEqual(node.op == 'Relu6', use_bounded_activation) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/datasets/__init__.py b/research/deeplab/datasets/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deeplab/datasets/build_ade20k_data.py b/research/deeplab/datasets/build_ade20k_data.py deleted file mode 100644 index fc04ed0db04..00000000000 --- a/research/deeplab/datasets/build_ade20k_data.py +++ /dev/null @@ -1,123 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Converts ADE20K data to TFRecord file format with Example protos.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import math -import os -import random -import sys -import build_data -from six.moves import range -import tensorflow as tf - -FLAGS = tf.app.flags.FLAGS - -tf.app.flags.DEFINE_string( - 'train_image_folder', - './ADE20K/ADEChallengeData2016/images/training', - 'Folder containing trainng images') -tf.app.flags.DEFINE_string( - 'train_image_label_folder', - './ADE20K/ADEChallengeData2016/annotations/training', - 'Folder containing annotations for trainng images') - -tf.app.flags.DEFINE_string( - 'val_image_folder', - './ADE20K/ADEChallengeData2016/images/validation', - 'Folder containing validation images') - -tf.app.flags.DEFINE_string( - 'val_image_label_folder', - './ADE20K/ADEChallengeData2016/annotations/validation', - 'Folder containing annotations for validation') - -tf.app.flags.DEFINE_string( - 'output_dir', './ADE20K/tfrecord', - 'Path to save converted tfrecord of Tensorflow example') - -_NUM_SHARDS = 4 - - -def _convert_dataset(dataset_split, dataset_dir, dataset_label_dir): - """Converts the ADE20k dataset into into tfrecord format. - - Args: - dataset_split: Dataset split (e.g., train, val). - dataset_dir: Dir in which the dataset locates. - dataset_label_dir: Dir in which the annotations locates. - - Raises: - RuntimeError: If loaded image and label have different shape. - """ - - img_names = tf.gfile.Glob(os.path.join(dataset_dir, '*.jpg')) - random.shuffle(img_names) - seg_names = [] - for f in img_names: - # get the filename without the extension - basename = os.path.basename(f).split('.')[0] - # cover its corresponding *_seg.png - seg = os.path.join(dataset_label_dir, basename+'.png') - seg_names.append(seg) - - num_images = len(img_names) - num_per_shard = int(math.ceil(num_images / _NUM_SHARDS)) - - image_reader = build_data.ImageReader('jpeg', channels=3) - label_reader = build_data.ImageReader('png', channels=1) - - for shard_id in range(_NUM_SHARDS): - output_filename = os.path.join( - FLAGS.output_dir, - '%s-%05d-of-%05d.tfrecord' % (dataset_split, shard_id, _NUM_SHARDS)) - with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer: - start_idx = shard_id * num_per_shard - end_idx = min((shard_id + 1) * num_per_shard, num_images) - for i in range(start_idx, end_idx): - sys.stdout.write('\r>> Converting image %d/%d shard %d' % ( - i + 1, num_images, shard_id)) - sys.stdout.flush() - # Read the image. - image_filename = img_names[i] - image_data = tf.gfile.FastGFile(image_filename, 'rb').read() - height, width = image_reader.read_image_dims(image_data) - # Read the semantic segmentation annotation. - seg_filename = seg_names[i] - seg_data = tf.gfile.FastGFile(seg_filename, 'rb').read() - seg_height, seg_width = label_reader.read_image_dims(seg_data) - if height != seg_height or width != seg_width: - raise RuntimeError('Shape mismatched between image and label.') - # Convert to tf example. - example = build_data.image_seg_to_tfexample( - image_data, img_names[i], height, width, seg_data) - tfrecord_writer.write(example.SerializeToString()) - sys.stdout.write('\n') - sys.stdout.flush() - - -def main(unused_argv): - tf.gfile.MakeDirs(FLAGS.output_dir) - _convert_dataset( - 'train', FLAGS.train_image_folder, FLAGS.train_image_label_folder) - _convert_dataset('val', FLAGS.val_image_folder, FLAGS.val_image_label_folder) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/deeplab/datasets/build_cityscapes_data.py b/research/deeplab/datasets/build_cityscapes_data.py deleted file mode 100644 index 53c11e30310..00000000000 --- a/research/deeplab/datasets/build_cityscapes_data.py +++ /dev/null @@ -1,198 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Converts Cityscapes data to TFRecord file format with Example protos. - -The Cityscapes dataset is expected to have the following directory structure: - - + cityscapes - - build_cityscapes_data.py (current working directiory). - - build_data.py - + cityscapesscripts - + annotation - + evaluation - + helpers - + preparation - + viewer - + gtFine - + train - + val - + test - + leftImg8bit - + train - + val - + test - + tfrecord - -This script converts data into sharded data files and save at tfrecord folder. - -Note that before running this script, the users should (1) register the -Cityscapes dataset website at https://www.cityscapes-dataset.com to -download the dataset, and (2) run the script provided by Cityscapes -`preparation/createTrainIdLabelImgs.py` to generate the training groundtruth. - -Also note that the tensorflow model will be trained with `TrainId' instead -of `EvalId' used on the evaluation server. Thus, the users need to convert -the predicted labels to `EvalId` for evaluation on the server. See the -vis.py for more details. - -The Example proto contains the following fields: - - image/encoded: encoded image content. - image/filename: image filename. - image/format: image file format. - image/height: image height. - image/width: image width. - image/channels: image channels. - image/segmentation/class/encoded: encoded semantic segmentation content. - image/segmentation/class/format: semantic segmentation file format. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import glob -import math -import os.path -import re -import sys -import build_data -from six.moves import range -import tensorflow as tf - -FLAGS = tf.app.flags.FLAGS - -tf.app.flags.DEFINE_string('cityscapes_root', - './cityscapes', - 'Cityscapes dataset root folder.') - -tf.app.flags.DEFINE_string( - 'output_dir', - './tfrecord', - 'Path to save converted SSTable of TensorFlow examples.') - - -_NUM_SHARDS = 10 - -# A map from data type to folder name that saves the data. -_FOLDERS_MAP = { - 'image': 'leftImg8bit', - 'label': 'gtFine', -} - -# A map from data type to filename postfix. -_POSTFIX_MAP = { - 'image': '_leftImg8bit', - 'label': '_gtFine_labelTrainIds', -} - -# A map from data type to data format. -_DATA_FORMAT_MAP = { - 'image': 'png', - 'label': 'png', -} - -# Image file pattern. -_IMAGE_FILENAME_RE = re.compile('(.+)' + _POSTFIX_MAP['image']) - - -def _get_files(data, dataset_split): - """Gets files for the specified data type and dataset split. - - Args: - data: String, desired data ('image' or 'label'). - dataset_split: String, dataset split ('train_fine', 'val_fine', 'test_fine') - - Returns: - A list of sorted file names or None when getting label for - test set. - """ - if dataset_split == 'train_fine': - split_dir = 'train' - elif dataset_split == 'val_fine': - split_dir = 'val' - elif dataset_split == 'test_fine': - split_dir = 'test' - else: - raise RuntimeError("Split {} is not supported".format(dataset_split)) - pattern = '*%s.%s' % (_POSTFIX_MAP[data], _DATA_FORMAT_MAP[data]) - search_files = os.path.join( - FLAGS.cityscapes_root, _FOLDERS_MAP[data], split_dir, '*', pattern) - filenames = glob.glob(search_files) - return sorted(filenames) - - -def _convert_dataset(dataset_split): - """Converts the specified dataset split to TFRecord format. - - Args: - dataset_split: The dataset split (e.g., train_fine, val_fine). - - Raises: - RuntimeError: If loaded image and label have different shape, or if the - image file with specified postfix could not be found. - """ - image_files = _get_files('image', dataset_split) - label_files = _get_files('label', dataset_split) - - num_images = len(image_files) - num_labels = len(label_files) - num_per_shard = int(math.ceil(num_images / _NUM_SHARDS)) - - if num_images != num_labels: - raise RuntimeError("The number of images and labels doesn't match: {} {}".format(num_images, num_labels)) - - image_reader = build_data.ImageReader('png', channels=3) - label_reader = build_data.ImageReader('png', channels=1) - - for shard_id in range(_NUM_SHARDS): - shard_filename = '%s-%05d-of-%05d.tfrecord' % ( - dataset_split, shard_id, _NUM_SHARDS) - output_filename = os.path.join(FLAGS.output_dir, shard_filename) - with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer: - start_idx = shard_id * num_per_shard - end_idx = min((shard_id + 1) * num_per_shard, num_images) - for i in range(start_idx, end_idx): - sys.stdout.write('\r>> Converting image %d/%d shard %d' % ( - i + 1, num_images, shard_id)) - sys.stdout.flush() - # Read the image. - image_data = tf.gfile.FastGFile(image_files[i], 'rb').read() - height, width = image_reader.read_image_dims(image_data) - # Read the semantic segmentation annotation. - seg_data = tf.gfile.FastGFile(label_files[i], 'rb').read() - seg_height, seg_width = label_reader.read_image_dims(seg_data) - if height != seg_height or width != seg_width: - raise RuntimeError('Shape mismatched between image and label.') - # Convert to tf example. - re_match = _IMAGE_FILENAME_RE.search(image_files[i]) - if re_match is None: - raise RuntimeError('Invalid image filename: ' + image_files[i]) - filename = os.path.basename(re_match.group(1)) - example = build_data.image_seg_to_tfexample( - image_data, filename, height, width, seg_data) - tfrecord_writer.write(example.SerializeToString()) - sys.stdout.write('\n') - sys.stdout.flush() - - -def main(unused_argv): - # Only support converting 'train_fine', 'val_fine' and 'test_fine' sets for now. - for dataset_split in ['train_fine', 'val_fine', 'test_fine']: - _convert_dataset(dataset_split) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/deeplab/datasets/build_data.py b/research/deeplab/datasets/build_data.py deleted file mode 100644 index 45628674dbf..00000000000 --- a/research/deeplab/datasets/build_data.py +++ /dev/null @@ -1,161 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Contains common utility functions and classes for building dataset. - -This script contains utility functions and classes to converts dataset to -TFRecord file format with Example protos. - -The Example proto contains the following fields: - - image/encoded: encoded image content. - image/filename: image filename. - image/format: image file format. - image/height: image height. - image/width: image width. - image/channels: image channels. - image/segmentation/class/encoded: encoded semantic segmentation content. - image/segmentation/class/format: semantic segmentation file format. -""" -import collections -import six -import tensorflow as tf - -FLAGS = tf.app.flags.FLAGS - -tf.app.flags.DEFINE_enum('image_format', 'png', ['jpg', 'jpeg', 'png'], - 'Image format.') - -tf.app.flags.DEFINE_enum('label_format', 'png', ['png'], - 'Segmentation label format.') - -# A map from image format to expected data format. -_IMAGE_FORMAT_MAP = { - 'jpg': 'jpeg', - 'jpeg': 'jpeg', - 'png': 'png', -} - - -class ImageReader(object): - """Helper class that provides TensorFlow image coding utilities.""" - - def __init__(self, image_format='jpeg', channels=3): - """Class constructor. - - Args: - image_format: Image format. Only 'jpeg', 'jpg', or 'png' are supported. - channels: Image channels. - """ - with tf.Graph().as_default(): - self._decode_data = tf.placeholder(dtype=tf.string) - self._image_format = image_format - self._session = tf.Session() - if self._image_format in ('jpeg', 'jpg'): - self._decode = tf.image.decode_jpeg(self._decode_data, - channels=channels) - elif self._image_format == 'png': - self._decode = tf.image.decode_png(self._decode_data, - channels=channels) - - def read_image_dims(self, image_data): - """Reads the image dimensions. - - Args: - image_data: string of image data. - - Returns: - image_height and image_width. - """ - image = self.decode_image(image_data) - return image.shape[:2] - - def decode_image(self, image_data): - """Decodes the image data string. - - Args: - image_data: string of image data. - - Returns: - Decoded image data. - - Raises: - ValueError: Value of image channels not supported. - """ - image = self._session.run(self._decode, - feed_dict={self._decode_data: image_data}) - if len(image.shape) != 3 or image.shape[2] not in (1, 3): - raise ValueError('The image channels not supported.') - - return image - - -def _int64_list_feature(values): - """Returns a TF-Feature of int64_list. - - Args: - values: A scalar or list of values. - - Returns: - A TF-Feature. - """ - if not isinstance(values, collections.Iterable): - values = [values] - - return tf.train.Feature(int64_list=tf.train.Int64List(value=values)) - - -def _bytes_list_feature(values): - """Returns a TF-Feature of bytes. - - Args: - values: A string. - - Returns: - A TF-Feature. - """ - def norm2bytes(value): - return value.encode() if isinstance(value, str) and six.PY3 else value - - return tf.train.Feature( - bytes_list=tf.train.BytesList(value=[norm2bytes(values)])) - - -def image_seg_to_tfexample(image_data, filename, height, width, seg_data): - """Converts one image/segmentation pair to tf example. - - Args: - image_data: string of image data. - filename: image filename. - height: image height. - width: image width. - seg_data: string of semantic segmentation data. - - Returns: - tf example of one image/segmentation pair. - """ - return tf.train.Example(features=tf.train.Features(feature={ - 'image/encoded': _bytes_list_feature(image_data), - 'image/filename': _bytes_list_feature(filename), - 'image/format': _bytes_list_feature( - _IMAGE_FORMAT_MAP[FLAGS.image_format]), - 'image/height': _int64_list_feature(height), - 'image/width': _int64_list_feature(width), - 'image/channels': _int64_list_feature(3), - 'image/segmentation/class/encoded': ( - _bytes_list_feature(seg_data)), - 'image/segmentation/class/format': _bytes_list_feature( - FLAGS.label_format), - })) diff --git a/research/deeplab/datasets/build_voc2012_data.py b/research/deeplab/datasets/build_voc2012_data.py deleted file mode 100644 index f0bdecb6a0f..00000000000 --- a/research/deeplab/datasets/build_voc2012_data.py +++ /dev/null @@ -1,146 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Converts PASCAL VOC 2012 data to TFRecord file format with Example protos. - -PASCAL VOC 2012 dataset is expected to have the following directory structure: - - + pascal_voc_seg - - build_data.py - - build_voc2012_data.py (current working directory). - + VOCdevkit - + VOC2012 - + JPEGImages - + SegmentationClass - + ImageSets - + Segmentation - + tfrecord - -Image folder: - ./VOCdevkit/VOC2012/JPEGImages - -Semantic segmentation annotations: - ./VOCdevkit/VOC2012/SegmentationClass - -list folder: - ./VOCdevkit/VOC2012/ImageSets/Segmentation - -This script converts data into sharded data files and save at tfrecord folder. - -The Example proto contains the following fields: - - image/encoded: encoded image content. - image/filename: image filename. - image/format: image file format. - image/height: image height. - image/width: image width. - image/channels: image channels. - image/segmentation/class/encoded: encoded semantic segmentation content. - image/segmentation/class/format: semantic segmentation file format. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import math -import os.path -import sys -import build_data -from six.moves import range -import tensorflow as tf - -FLAGS = tf.app.flags.FLAGS - -tf.app.flags.DEFINE_string('image_folder', - './VOCdevkit/VOC2012/JPEGImages', - 'Folder containing images.') - -tf.app.flags.DEFINE_string( - 'semantic_segmentation_folder', - './VOCdevkit/VOC2012/SegmentationClassRaw', - 'Folder containing semantic segmentation annotations.') - -tf.app.flags.DEFINE_string( - 'list_folder', - './VOCdevkit/VOC2012/ImageSets/Segmentation', - 'Folder containing lists for training and validation') - -tf.app.flags.DEFINE_string( - 'output_dir', - './tfrecord', - 'Path to save converted SSTable of TensorFlow examples.') - - -_NUM_SHARDS = 4 - - -def _convert_dataset(dataset_split): - """Converts the specified dataset split to TFRecord format. - - Args: - dataset_split: The dataset split (e.g., train, test). - - Raises: - RuntimeError: If loaded image and label have different shape. - """ - dataset = os.path.basename(dataset_split)[:-4] - sys.stdout.write('Processing ' + dataset) - filenames = [x.strip('\n') for x in open(dataset_split, 'r')] - num_images = len(filenames) - num_per_shard = int(math.ceil(num_images / _NUM_SHARDS)) - - image_reader = build_data.ImageReader('jpeg', channels=3) - label_reader = build_data.ImageReader('png', channels=1) - - for shard_id in range(_NUM_SHARDS): - output_filename = os.path.join( - FLAGS.output_dir, - '%s-%05d-of-%05d.tfrecord' % (dataset, shard_id, _NUM_SHARDS)) - with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer: - start_idx = shard_id * num_per_shard - end_idx = min((shard_id + 1) * num_per_shard, num_images) - for i in range(start_idx, end_idx): - sys.stdout.write('\r>> Converting image %d/%d shard %d' % ( - i + 1, len(filenames), shard_id)) - sys.stdout.flush() - # Read the image. - image_filename = os.path.join( - FLAGS.image_folder, filenames[i] + '.' + FLAGS.image_format) - image_data = tf.gfile.GFile(image_filename, 'rb').read() - height, width = image_reader.read_image_dims(image_data) - # Read the semantic segmentation annotation. - seg_filename = os.path.join( - FLAGS.semantic_segmentation_folder, - filenames[i] + '.' + FLAGS.label_format) - seg_data = tf.gfile.GFile(seg_filename, 'rb').read() - seg_height, seg_width = label_reader.read_image_dims(seg_data) - if height != seg_height or width != seg_width: - raise RuntimeError('Shape mismatched between image and label.') - # Convert to tf example. - example = build_data.image_seg_to_tfexample( - image_data, filenames[i], height, width, seg_data) - tfrecord_writer.write(example.SerializeToString()) - sys.stdout.write('\n') - sys.stdout.flush() - - -def main(unused_argv): - dataset_splits = tf.gfile.Glob(os.path.join(FLAGS.list_folder, '*.txt')) - for dataset_split in dataset_splits: - _convert_dataset(dataset_split) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/deeplab/datasets/convert_cityscapes.sh b/research/deeplab/datasets/convert_cityscapes.sh deleted file mode 100644 index ddc39fb11dd..00000000000 --- a/research/deeplab/datasets/convert_cityscapes.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -# -# Script to preprocess the Cityscapes dataset. Note (1) the users should -# register the Cityscapes dataset website at -# https://www.cityscapes-dataset.com/downloads/ to download the dataset, -# and (2) the users should download the utility scripts provided by -# Cityscapes at https://github.com/mcordts/cityscapesScripts. -# -# Usage: -# bash ./convert_cityscapes.sh -# -# The folder structure is assumed to be: -# + datasets -# - build_cityscapes_data.py -# - convert_cityscapes.sh -# + cityscapes -# + cityscapesscripts (downloaded scripts) -# + gtFine -# + leftImg8bit -# - -# Exit immediately if a command exits with a non-zero status. -set -e - -CURRENT_DIR=$(pwd) -WORK_DIR="." - -# Root path for Cityscapes dataset. -CITYSCAPES_ROOT="${WORK_DIR}/cityscapes" - -export PYTHONPATH="${CITYSCAPES_ROOT}:${PYTHONPATH}" - -# Create training labels. -python "${CITYSCAPES_ROOT}/cityscapesscripts/preparation/createTrainIdLabelImgs.py" - -# Build TFRecords of the dataset. -# First, create output directory for storing TFRecords. -OUTPUT_DIR="${CITYSCAPES_ROOT}/tfrecord" -mkdir -p "${OUTPUT_DIR}" - -BUILD_SCRIPT="${CURRENT_DIR}/build_cityscapes_data.py" - -echo "Converting Cityscapes dataset..." -python "${BUILD_SCRIPT}" \ - --cityscapes_root="${CITYSCAPES_ROOT}" \ - --output_dir="${OUTPUT_DIR}" \ diff --git a/research/deeplab/datasets/data_generator.py b/research/deeplab/datasets/data_generator.py deleted file mode 100644 index d84e66f9c48..00000000000 --- a/research/deeplab/datasets/data_generator.py +++ /dev/null @@ -1,350 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Wrapper for providing semantic segmentaion data. - -The SegmentationDataset class provides both images and annotations (semantic -segmentation and/or instance segmentation) for TensorFlow. Currently, we -support the following datasets: - -1. PASCAL VOC 2012 (http://host.robots.ox.ac.uk/pascal/VOC/voc2012/). - -PASCAL VOC 2012 semantic segmentation dataset annotates 20 foreground objects -(e.g., bike, person, and so on) and leaves all the other semantic classes as -one background class. The dataset contains 1464, 1449, and 1456 annotated -images for the training, validation and test respectively. - -2. Cityscapes dataset (https://www.cityscapes-dataset.com) - -The Cityscapes dataset contains 19 semantic labels (such as road, person, car, -and so on) for urban street scenes. - -3. ADE20K dataset (http://groups.csail.mit.edu/vision/datasets/ADE20K) - -The ADE20K dataset contains 150 semantic labels both urban street scenes and -indoor scenes. - -References: - M. Everingham, S. M. A. Eslami, L. V. Gool, C. K. I. Williams, J. Winn, - and A. Zisserman, The pascal visual object classes challenge a retrospective. - IJCV, 2014. - - M. Cordts, M. Omran, S. Ramos, T. Rehfeld, M. Enzweiler, R. Benenson, - U. Franke, S. Roth, and B. Schiele, "The cityscapes dataset for semantic urban - scene understanding," In Proc. of CVPR, 2016. - - B. Zhou, H. Zhao, X. Puig, S. Fidler, A. Barriuso, A. Torralba, "Scene Parsing - through ADE20K dataset", In Proc. of CVPR, 2017. -""" - -import collections -import os -import tensorflow as tf -from deeplab import common -from deeplab import input_preprocess - -# Named tuple to describe the dataset properties. -DatasetDescriptor = collections.namedtuple( - 'DatasetDescriptor', - [ - 'splits_to_sizes', # Splits of the dataset into training, val and test. - 'num_classes', # Number of semantic classes, including the - # background class (if exists). For example, there - # are 20 foreground classes + 1 background class in - # the PASCAL VOC 2012 dataset. Thus, we set - # num_classes=21. - 'ignore_label', # Ignore label value. - ]) - -_CITYSCAPES_INFORMATION = DatasetDescriptor( - splits_to_sizes={'train_fine': 2975, - 'train_coarse': 22973, - 'trainval_fine': 3475, - 'trainval_coarse': 23473, - 'val_fine': 500, - 'test_fine': 1525}, - num_classes=19, - ignore_label=255, -) - -_PASCAL_VOC_SEG_INFORMATION = DatasetDescriptor( - splits_to_sizes={ - 'train': 1464, - 'train_aug': 10582, - 'trainval': 2913, - 'val': 1449, - }, - num_classes=21, - ignore_label=255, -) - -_ADE20K_INFORMATION = DatasetDescriptor( - splits_to_sizes={ - 'train': 20210, # num of samples in images/training - 'val': 2000, # num of samples in images/validation - }, - num_classes=151, - ignore_label=0, -) - -_DATASETS_INFORMATION = { - 'cityscapes': _CITYSCAPES_INFORMATION, - 'pascal_voc_seg': _PASCAL_VOC_SEG_INFORMATION, - 'ade20k': _ADE20K_INFORMATION, -} - -# Default file pattern of TFRecord of TensorFlow Example. -_FILE_PATTERN = '%s-*' - - -def get_cityscapes_dataset_name(): - return 'cityscapes' - - -class Dataset(object): - """Represents input dataset for deeplab model.""" - - def __init__(self, - dataset_name, - split_name, - dataset_dir, - batch_size, - crop_size, - min_resize_value=None, - max_resize_value=None, - resize_factor=None, - min_scale_factor=1., - max_scale_factor=1., - scale_factor_step_size=0, - model_variant=None, - num_readers=1, - is_training=False, - should_shuffle=False, - should_repeat=False): - """Initializes the dataset. - - Args: - dataset_name: Dataset name. - split_name: A train/val Split name. - dataset_dir: The directory of the dataset sources. - batch_size: Batch size. - crop_size: The size used to crop the image and label. - min_resize_value: Desired size of the smaller image side. - max_resize_value: Maximum allowed size of the larger image side. - resize_factor: Resized dimensions are multiple of factor plus one. - min_scale_factor: Minimum scale factor value. - max_scale_factor: Maximum scale factor value. - scale_factor_step_size: The step size from min scale factor to max scale - factor. The input is randomly scaled based on the value of - (min_scale_factor, max_scale_factor, scale_factor_step_size). - model_variant: Model variant (string) for choosing how to mean-subtract - the images. See feature_extractor.network_map for supported model - variants. - num_readers: Number of readers for data provider. - is_training: Boolean, if dataset is for training or not. - should_shuffle: Boolean, if should shuffle the input data. - should_repeat: Boolean, if should repeat the input data. - - Raises: - ValueError: Dataset name and split name are not supported. - """ - if dataset_name not in _DATASETS_INFORMATION: - raise ValueError('The specified dataset is not supported yet.') - self.dataset_name = dataset_name - - splits_to_sizes = _DATASETS_INFORMATION[dataset_name].splits_to_sizes - - if split_name not in splits_to_sizes: - raise ValueError('data split name %s not recognized' % split_name) - - if model_variant is None: - tf.logging.warning('Please specify a model_variant. See ' - 'feature_extractor.network_map for supported model ' - 'variants.') - - self.split_name = split_name - self.dataset_dir = dataset_dir - self.batch_size = batch_size - self.crop_size = crop_size - self.min_resize_value = min_resize_value - self.max_resize_value = max_resize_value - self.resize_factor = resize_factor - self.min_scale_factor = min_scale_factor - self.max_scale_factor = max_scale_factor - self.scale_factor_step_size = scale_factor_step_size - self.model_variant = model_variant - self.num_readers = num_readers - self.is_training = is_training - self.should_shuffle = should_shuffle - self.should_repeat = should_repeat - - self.num_of_classes = _DATASETS_INFORMATION[self.dataset_name].num_classes - self.ignore_label = _DATASETS_INFORMATION[self.dataset_name].ignore_label - - def _parse_function(self, example_proto): - """Function to parse the example proto. - - Args: - example_proto: Proto in the format of tf.Example. - - Returns: - A dictionary with parsed image, label, height, width and image name. - - Raises: - ValueError: Label is of wrong shape. - """ - - # Currently only supports jpeg and png. - # Need to use this logic because the shape is not known for - # tf.image.decode_image and we rely on this info to - # extend label if necessary. - def _decode_image(content, channels): - return tf.cond( - tf.image.is_jpeg(content), - lambda: tf.image.decode_jpeg(content, channels), - lambda: tf.image.decode_png(content, channels)) - - features = { - 'image/encoded': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/filename': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/format': - tf.FixedLenFeature((), tf.string, default_value='jpeg'), - 'image/height': - tf.FixedLenFeature((), tf.int64, default_value=0), - 'image/width': - tf.FixedLenFeature((), tf.int64, default_value=0), - 'image/segmentation/class/encoded': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/segmentation/class/format': - tf.FixedLenFeature((), tf.string, default_value='png'), - } - - parsed_features = tf.parse_single_example(example_proto, features) - - image = _decode_image(parsed_features['image/encoded'], channels=3) - - label = None - if self.split_name != common.TEST_SET: - label = _decode_image( - parsed_features['image/segmentation/class/encoded'], channels=1) - - image_name = parsed_features['image/filename'] - if image_name is None: - image_name = tf.constant('') - - sample = { - common.IMAGE: image, - common.IMAGE_NAME: image_name, - common.HEIGHT: parsed_features['image/height'], - common.WIDTH: parsed_features['image/width'], - } - - if label is not None: - if label.get_shape().ndims == 2: - label = tf.expand_dims(label, 2) - elif label.get_shape().ndims == 3 and label.shape.dims[2] == 1: - pass - else: - raise ValueError('Input label shape must be [height, width], or ' - '[height, width, 1].') - - label.set_shape([None, None, 1]) - - sample[common.LABELS_CLASS] = label - - return sample - - def _preprocess_image(self, sample): - """Preprocesses the image and label. - - Args: - sample: A sample containing image and label. - - Returns: - sample: Sample with preprocessed image and label. - - Raises: - ValueError: Ground truth label not provided during training. - """ - image = sample[common.IMAGE] - label = sample[common.LABELS_CLASS] - - original_image, image, label = input_preprocess.preprocess_image_and_label( - image=image, - label=label, - crop_height=self.crop_size[0], - crop_width=self.crop_size[1], - min_resize_value=self.min_resize_value, - max_resize_value=self.max_resize_value, - resize_factor=self.resize_factor, - min_scale_factor=self.min_scale_factor, - max_scale_factor=self.max_scale_factor, - scale_factor_step_size=self.scale_factor_step_size, - ignore_label=self.ignore_label, - is_training=self.is_training, - model_variant=self.model_variant) - - sample[common.IMAGE] = image - - if not self.is_training: - # Original image is only used during visualization. - sample[common.ORIGINAL_IMAGE] = original_image - - if label is not None: - sample[common.LABEL] = label - - # Remove common.LABEL_CLASS key in the sample since it is only used to - # derive label and not used in training and evaluation. - sample.pop(common.LABELS_CLASS, None) - - return sample - - def get_one_shot_iterator(self): - """Gets an iterator that iterates across the dataset once. - - Returns: - An iterator of type tf.data.Iterator. - """ - - files = self._get_all_files() - - dataset = ( - tf.data.TFRecordDataset(files, num_parallel_reads=self.num_readers) - .map(self._parse_function, num_parallel_calls=self.num_readers) - .map(self._preprocess_image, num_parallel_calls=self.num_readers)) - - if self.should_shuffle: - dataset = dataset.shuffle(buffer_size=100) - - if self.should_repeat: - dataset = dataset.repeat() # Repeat forever for training. - else: - dataset = dataset.repeat(1) - - dataset = dataset.batch(self.batch_size).prefetch(self.batch_size) - return dataset.make_one_shot_iterator() - - def _get_all_files(self): - """Gets all the files to read data from. - - Returns: - A list of input files. - """ - file_pattern = _FILE_PATTERN - file_pattern = os.path.join(self.dataset_dir, - file_pattern % self.split_name) - return tf.gfile.Glob(file_pattern) diff --git a/research/deeplab/datasets/data_generator_test.py b/research/deeplab/datasets/data_generator_test.py deleted file mode 100644 index f4425d01da0..00000000000 --- a/research/deeplab/datasets/data_generator_test.py +++ /dev/null @@ -1,115 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for deeplab.datasets.data_generator.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections - -from six.moves import range -import tensorflow as tf - -from deeplab import common -from deeplab.datasets import data_generator - -ImageAttributes = collections.namedtuple( - 'ImageAttributes', ['image', 'label', 'height', 'width', 'image_name']) - - -class DatasetTest(tf.test.TestCase): - - # Note: training dataset cannot be tested since there is shuffle operation. - # When disabling the shuffle, training dataset is operated same as validation - # dataset. Therefore it is not tested again. - def testPascalVocSegTestData(self): - dataset = data_generator.Dataset( - dataset_name='pascal_voc_seg', - split_name='val', - dataset_dir= - 'deeplab/testing/pascal_voc_seg', - batch_size=1, - crop_size=[3, 3], # Use small size for testing. - min_resize_value=3, - max_resize_value=3, - resize_factor=None, - min_scale_factor=0.01, - max_scale_factor=2.0, - scale_factor_step_size=0.25, - is_training=False, - model_variant='mobilenet_v2') - - self.assertAllEqual(dataset.num_of_classes, 21) - self.assertAllEqual(dataset.ignore_label, 255) - - num_of_images = 3 - with self.test_session() as sess: - iterator = dataset.get_one_shot_iterator() - - for i in range(num_of_images): - batch = iterator.get_next() - batch, = sess.run([batch]) - image_attributes = _get_attributes_of_image(i) - self.assertEqual(batch[common.HEIGHT][0], image_attributes.height) - self.assertEqual(batch[common.WIDTH][0], image_attributes.width) - self.assertEqual(batch[common.IMAGE_NAME][0], - image_attributes.image_name.encode()) - - # All data have been read. - with self.assertRaisesRegexp(tf.errors.OutOfRangeError, ''): - sess.run([iterator.get_next()]) - - -def _get_attributes_of_image(index): - """Gets the attributes of the image. - - Args: - index: Index of image in all images. - - Returns: - Attributes of the image in the format of ImageAttributes. - - Raises: - ValueError: If index is of wrong value. - """ - if index == 0: - return ImageAttributes( - image=None, - label=None, - height=366, - width=500, - image_name='2007_000033') - elif index == 1: - return ImageAttributes( - image=None, - label=None, - height=335, - width=500, - image_name='2007_000042') - elif index == 2: - return ImageAttributes( - image=None, - label=None, - height=333, - width=500, - image_name='2007_000061') - else: - raise ValueError('Index can only be 0, 1 or 2.') - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/datasets/download_and_convert_ade20k.sh b/research/deeplab/datasets/download_and_convert_ade20k.sh deleted file mode 100644 index 3614ae42c16..00000000000 --- a/research/deeplab/datasets/download_and_convert_ade20k.sh +++ /dev/null @@ -1,80 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -# -# Script to download and preprocess the ADE20K dataset. -# -# Usage: -# bash ./download_and_convert_ade20k.sh -# -# The folder structure is assumed to be: -# + datasets -# - build_data.py -# - build_ade20k_data.py -# - download_and_convert_ade20k.sh -# + ADE20K -# + tfrecord -# + ADEChallengeData2016 -# + annotations -# + training -# + validation -# + images -# + training -# + validation - -# Exit immediately if a command exits with a non-zero status. -set -e - -CURRENT_DIR=$(pwd) -WORK_DIR="./ADE20K" -mkdir -p "${WORK_DIR}" -cd "${WORK_DIR}" - -# Helper function to download and unpack ADE20K dataset. -download_and_uncompress() { - local BASE_URL=${1} - local FILENAME=${2} - - if [ ! -f "${FILENAME}" ]; then - echo "Downloading ${FILENAME} to ${WORK_DIR}" - wget -nd -c "${BASE_URL}/${FILENAME}" - fi - echo "Uncompressing ${FILENAME}" - unzip "${FILENAME}" -} - -# Download the images. -BASE_URL="http://data.csail.mit.edu/places/ADEchallenge" -FILENAME="ADEChallengeData2016.zip" - -download_and_uncompress "${BASE_URL}" "${FILENAME}" - -cd "${CURRENT_DIR}" - -# Root path for ADE20K dataset. -ADE20K_ROOT="${WORK_DIR}/ADEChallengeData2016" - -# Build TFRecords of the dataset. -# First, create output directory for storing TFRecords. -OUTPUT_DIR="${WORK_DIR}/tfrecord" -mkdir -p "${OUTPUT_DIR}" - -echo "Converting ADE20K dataset..." -python ./build_ade20k_data.py \ - --train_image_folder="${ADE20K_ROOT}/images/training/" \ - --train_image_label_folder="${ADE20K_ROOT}/annotations/training/" \ - --val_image_folder="${ADE20K_ROOT}/images/validation/" \ - --val_image_label_folder="${ADE20K_ROOT}/annotations/validation/" \ - --output_dir="${OUTPUT_DIR}" diff --git a/research/deeplab/datasets/download_and_convert_voc2012.sh b/research/deeplab/datasets/download_and_convert_voc2012.sh deleted file mode 100644 index 3126f729dec..00000000000 --- a/research/deeplab/datasets/download_and_convert_voc2012.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -# -# Script to download and preprocess the PASCAL VOC 2012 dataset. -# -# Usage: -# bash ./download_and_convert_voc2012.sh -# -# The folder structure is assumed to be: -# + datasets -# - build_data.py -# - build_voc2012_data.py -# - download_and_convert_voc2012.sh -# - remove_gt_colormap.py -# + pascal_voc_seg -# + VOCdevkit -# + VOC2012 -# + JPEGImages -# + SegmentationClass -# - -# Exit immediately if a command exits with a non-zero status. -set -e - -CURRENT_DIR=$(pwd) -WORK_DIR="./pascal_voc_seg" -SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -mkdir -p "${WORK_DIR}" -cd "${WORK_DIR}" - -# Helper function to download and unpack VOC 2012 dataset. -download_and_uncompress() { - local BASE_URL=${1} - local FILENAME=${2} - - if [ ! -f "${FILENAME}" ]; then - echo "Downloading ${FILENAME} to ${WORK_DIR}" - wget -nd -c "${BASE_URL}/${FILENAME}" - fi - echo "Uncompressing ${FILENAME}" - sudo apt install unzip - unzip "${FILENAME}" -} - -# Download the images. -BASE_URL="https://data.deepai.org/" -FILENAME="PascalVOC2012.zip" - -download_and_uncompress "${BASE_URL}" "${FILENAME}" - -cd "${CURRENT_DIR}" - -# Root path for PASCAL VOC 2012 dataset. -PASCAL_ROOT="${WORK_DIR}/VOC2012" - -# Remove the colormap in the ground truth annotations. -SEG_FOLDER="${PASCAL_ROOT}/SegmentationClass" -SEMANTIC_SEG_FOLDER="${PASCAL_ROOT}/SegmentationClassRaw" - -echo "Removing the color map in ground truth annotations..." -python3 "${SCRIPT_DIR}/remove_gt_colormap.py" \ - --original_gt_folder="${SEG_FOLDER}" \ - --output_dir="${SEMANTIC_SEG_FOLDER}" - -# Build TFRecords of the dataset. -# First, create output directory for storing TFRecords. -OUTPUT_DIR="${WORK_DIR}/tfrecord" -mkdir -p "${OUTPUT_DIR}" - -IMAGE_FOLDER="${PASCAL_ROOT}/JPEGImages" -LIST_FOLDER="${PASCAL_ROOT}/ImageSets/Segmentation" - -echo "Converting PASCAL VOC 2012 dataset..." -python3 "${SCRIPT_DIR}/build_voc2012_data.py" \ - --image_folder="${IMAGE_FOLDER}" \ - --semantic_segmentation_folder="${SEMANTIC_SEG_FOLDER}" \ - --list_folder="${LIST_FOLDER}" \ - --image_format="jpg" \ - --output_dir="${OUTPUT_DIR}" diff --git a/research/deeplab/datasets/remove_gt_colormap.py b/research/deeplab/datasets/remove_gt_colormap.py deleted file mode 100644 index 900570038ed..00000000000 --- a/research/deeplab/datasets/remove_gt_colormap.py +++ /dev/null @@ -1,83 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Removes the color map from segmentation annotations. - -Removes the color map from the ground truth segmentation annotations and save -the results to output_dir. -""" -import glob -import os.path -import numpy as np - -from PIL import Image - -import tensorflow as tf - -FLAGS = tf.compat.v1.flags.FLAGS - -tf.compat.v1.flags.DEFINE_string('original_gt_folder', - './VOCdevkit/VOC2012/SegmentationClass', - 'Original ground truth annotations.') - -tf.compat.v1.flags.DEFINE_string('segmentation_format', 'png', 'Segmentation format.') - -tf.compat.v1.flags.DEFINE_string('output_dir', - './VOCdevkit/VOC2012/SegmentationClassRaw', - 'folder to save modified ground truth annotations.') - - -def _remove_colormap(filename): - """Removes the color map from the annotation. - - Args: - filename: Ground truth annotation filename. - - Returns: - Annotation without color map. - """ - return np.array(Image.open(filename)) - - -def _save_annotation(annotation, filename): - """Saves the annotation as png file. - - Args: - annotation: Segmentation annotation. - filename: Output filename. - """ - pil_image = Image.fromarray(annotation.astype(dtype=np.uint8)) - with tf.io.gfile.GFile(filename, mode='w') as f: - pil_image.save(f, 'PNG') - - -def main(unused_argv): - # Create the output directory if not exists. - if not tf.io.gfile.isdir(FLAGS.output_dir): - tf.io.gfile.makedirs(FLAGS.output_dir) - - annotations = glob.glob(os.path.join(FLAGS.original_gt_folder, - '*.' + FLAGS.segmentation_format)) - for annotation in annotations: - raw_annotation = _remove_colormap(annotation) - filename = os.path.basename(annotation)[:-4] - _save_annotation(raw_annotation, - os.path.join( - FLAGS.output_dir, - filename + '.' + FLAGS.segmentation_format)) - - -if __name__ == '__main__': - tf.compat.v1.app.run() diff --git a/research/deeplab/deeplab_demo.ipynb b/research/deeplab/deeplab_demo.ipynb deleted file mode 100644 index 81ccfde1b64..00000000000 --- a/research/deeplab/deeplab_demo.ipynb +++ /dev/null @@ -1,369 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "KFPcBuVFw61h" - }, - "source": [ - "# Overview\n", - "\n", - "This colab demonstrates the steps to use the DeepLab model to perform semantic segmentation on a sample input image. Expected outputs are semantic labels overlayed on the sample image.\n", - "\n", - "### About DeepLab\n", - "The models used in this colab perform semantic segmentation. Semantic segmentation models focus on assigning semantic labels, such as sky, person, or car, to multiple objects and stuff in a single image." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "t3ozFsEEP-u_" - }, - "source": [ - "# Instructions\n", - "\u003ch3\u003e\u003ca href=\"https://cloud.google.com/tpu/\"\u003e\u003cimg valign=\"middle\" src=\"https://raw.githubusercontent.com/GoogleCloudPlatform/tensorflow-without-a-phd/master/tensorflow-rl-pong/images/tpu-hexagon.png\" width=\"50\"\u003e\u003c/a\u003e \u0026nbsp;\u0026nbsp;Use a free TPU device\u003c/h3\u003e\n", - "\n", - " 1. On the main menu, click Runtime and select **Change runtime type**. Set \"TPU\" as the hardware accelerator.\n", - " 1. Click Runtime again and select **Runtime \u003e Run All**. You can also run the cells manually with Shift-ENTER." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "7cRiapZ1P3wy" - }, - "source": [ - "## Import Libraries" - ] - }, - { - "cell_type": "code", - "execution_count": 0, - "metadata": { - "cellView": "code", - "colab": {}, - "colab_type": "code", - "id": "kAbdmRmvq0Je" - }, - "outputs": [], - "source": [ - "import os\n", - "from io import BytesIO\n", - "import tarfile\n", - "import tempfile\n", - "from six.moves import urllib\n", - "\n", - "from matplotlib import gridspec\n", - "from matplotlib import pyplot as plt\n", - "import numpy as np\n", - "from PIL import Image\n", - "\n", - "%tensorflow_version 1.x\n", - "import tensorflow as tf" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "p47cYGGOQE1W" - }, - "source": [ - "## Import helper methods\n", - "These methods help us perform the following tasks:\n", - "* Load the latest version of the pretrained DeepLab model\n", - "* Load the colormap from the PASCAL VOC dataset\n", - "* Adds colors to various labels, such as \"pink\" for people, \"green\" for bicycle and more\n", - "* Visualize an image, and add an overlay of colors on various regions" - ] - }, - { - "cell_type": "code", - "execution_count": 0, - "metadata": { - "cellView": "code", - "colab": {}, - "colab_type": "code", - "id": "vN0kU6NJ1Ye5" - }, - "outputs": [], - "source": [ - "class DeepLabModel(object):\n", - " \"\"\"Class to load deeplab model and run inference.\"\"\"\n", - "\n", - " INPUT_TENSOR_NAME = 'ImageTensor:0'\n", - " OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'\n", - " INPUT_SIZE = 513\n", - " FROZEN_GRAPH_NAME = 'frozen_inference_graph'\n", - "\n", - " def __init__(self, tarball_path):\n", - " \"\"\"Creates and loads pretrained deeplab model.\"\"\"\n", - " self.graph = tf.Graph()\n", - "\n", - " graph_def = None\n", - " # Extract frozen graph from tar archive.\n", - " tar_file = tarfile.open(tarball_path)\n", - " for tar_info in tar_file.getmembers():\n", - " if self.FROZEN_GRAPH_NAME in os.path.basename(tar_info.name):\n", - " file_handle = tar_file.extractfile(tar_info)\n", - " graph_def = tf.GraphDef.FromString(file_handle.read())\n", - " break\n", - "\n", - " tar_file.close()\n", - "\n", - " if graph_def is None:\n", - " raise RuntimeError('Cannot find inference graph in tar archive.')\n", - "\n", - " with self.graph.as_default():\n", - " tf.import_graph_def(graph_def, name='')\n", - "\n", - " self.sess = tf.Session(graph=self.graph)\n", - "\n", - " def run(self, image):\n", - " \"\"\"Runs inference on a single image.\n", - "\n", - " Args:\n", - " image: A PIL.Image object, raw input image.\n", - "\n", - " Returns:\n", - " resized_image: RGB image resized from original input image.\n", - " seg_map: Segmentation map of `resized_image`.\n", - " \"\"\"\n", - " width, height = image.size\n", - " resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)\n", - " target_size = (int(resize_ratio * width), int(resize_ratio * height))\n", - " resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)\n", - " batch_seg_map = self.sess.run(\n", - " self.OUTPUT_TENSOR_NAME,\n", - " feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})\n", - " seg_map = batch_seg_map[0]\n", - " return resized_image, seg_map\n", - "\n", - "\n", - "def create_pascal_label_colormap():\n", - " \"\"\"Creates a label colormap used in PASCAL VOC segmentation benchmark.\n", - "\n", - " Returns:\n", - " A Colormap for visualizing segmentation results.\n", - " \"\"\"\n", - " colormap = np.zeros((256, 3), dtype=int)\n", - " ind = np.arange(256, dtype=int)\n", - "\n", - " for shift in reversed(range(8)):\n", - " for channel in range(3):\n", - " colormap[:, channel] |= ((ind \u003e\u003e channel) \u0026 1) \u003c\u003c shift\n", - " ind \u003e\u003e= 3\n", - "\n", - " return colormap\n", - "\n", - "\n", - "def label_to_color_image(label):\n", - " \"\"\"Adds color defined by the dataset colormap to the label.\n", - "\n", - " Args:\n", - " label: A 2D array with integer type, storing the segmentation label.\n", - "\n", - " Returns:\n", - " result: A 2D array with floating type. The element of the array\n", - " is the color indexed by the corresponding element in the input label\n", - " to the PASCAL color map.\n", - "\n", - " Raises:\n", - " ValueError: If label is not of rank 2 or its value is larger than color\n", - " map maximum entry.\n", - " \"\"\"\n", - " if label.ndim != 2:\n", - " raise ValueError('Expect 2-D input label')\n", - "\n", - " colormap = create_pascal_label_colormap()\n", - "\n", - " if np.max(label) \u003e= len(colormap):\n", - " raise ValueError('label value too large.')\n", - "\n", - " return colormap[label]\n", - "\n", - "\n", - "def vis_segmentation(image, seg_map):\n", - " \"\"\"Visualizes input image, segmentation map and overlay view.\"\"\"\n", - " plt.figure(figsize=(15, 5))\n", - " grid_spec = gridspec.GridSpec(1, 4, width_ratios=[6, 6, 6, 1])\n", - "\n", - " plt.subplot(grid_spec[0])\n", - " plt.imshow(image)\n", - " plt.axis('off')\n", - " plt.title('input image')\n", - "\n", - " plt.subplot(grid_spec[1])\n", - " seg_image = label_to_color_image(seg_map).astype(np.uint8)\n", - " plt.imshow(seg_image)\n", - " plt.axis('off')\n", - " plt.title('segmentation map')\n", - "\n", - " plt.subplot(grid_spec[2])\n", - " plt.imshow(image)\n", - " plt.imshow(seg_image, alpha=0.7)\n", - " plt.axis('off')\n", - " plt.title('segmentation overlay')\n", - "\n", - " unique_labels = np.unique(seg_map)\n", - " ax = plt.subplot(grid_spec[3])\n", - " plt.imshow(\n", - " FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation='nearest')\n", - " ax.yaxis.tick_right()\n", - " plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])\n", - " plt.xticks([], [])\n", - " ax.tick_params(width=0.0)\n", - " plt.grid('off')\n", - " plt.show()\n", - "\n", - "\n", - "LABEL_NAMES = np.asarray([\n", - " 'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',\n", - " 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',\n", - " 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv'\n", - "])\n", - "\n", - "FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)\n", - "FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "nGcZzNkASG9A" - }, - "source": [ - "## Select a pretrained model\n", - "We have trained the DeepLab model using various backbone networks. Select one from the MODEL_NAME list." - ] - }, - { - "cell_type": "code", - "execution_count": 0, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "c4oXKmnjw6i_" - }, - "outputs": [], - "source": [ - "MODEL_NAME = 'mobilenetv2_coco_voctrainaug' # @param ['mobilenetv2_coco_voctrainaug', 'mobilenetv2_coco_voctrainval', 'xception_coco_voctrainaug', 'xception_coco_voctrainval']\n", - "\n", - "_DOWNLOAD_URL_PREFIX = 'http://download.tensorflow.org/models/'\n", - "_MODEL_URLS = {\n", - " 'mobilenetv2_coco_voctrainaug':\n", - " 'deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz',\n", - " 'mobilenetv2_coco_voctrainval':\n", - " 'deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz',\n", - " 'xception_coco_voctrainaug':\n", - " 'deeplabv3_pascal_train_aug_2018_01_04.tar.gz',\n", - " 'xception_coco_voctrainval':\n", - " 'deeplabv3_pascal_trainval_2018_01_04.tar.gz',\n", - "}\n", - "_TARBALL_NAME = 'deeplab_model.tar.gz'\n", - "\n", - "model_dir = tempfile.mkdtemp()\n", - "tf.gfile.MakeDirs(model_dir)\n", - "\n", - "download_path = os.path.join(model_dir, _TARBALL_NAME)\n", - "print('downloading model, this might take a while...')\n", - "urllib.request.urlretrieve(_DOWNLOAD_URL_PREFIX + _MODEL_URLS[MODEL_NAME],\n", - " download_path)\n", - "print('download completed! loading DeepLab model...')\n", - "\n", - "MODEL = DeepLabModel(download_path)\n", - "print('model loaded successfully!')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "SZst78N-4OKO" - }, - "source": [ - "## Run on sample images\n", - "\n", - "Select one of sample images (leave `IMAGE_URL` empty) or feed any internet image\n", - "url for inference.\n", - "\n", - "Note that this colab uses single scale inference for fast computation,\n", - "so the results may slightly differ from the visualizations in the\n", - "[README](https://github.com/tensorflow/models/blob/master/research/deeplab/README.md) file,\n", - "which uses multi-scale and left-right flipped inputs." - ] - }, - { - "cell_type": "code", - "execution_count": 0, - "metadata": { - "cellView": "form", - "colab": {}, - "colab_type": "code", - "id": "edGukUHXyymr" - }, - "outputs": [], - "source": [ - "\n", - "SAMPLE_IMAGE = 'image1' # @param ['image1', 'image2', 'image3']\n", - "IMAGE_URL = '' #@param {type:\"string\"}\n", - "\n", - "_SAMPLE_URL = ('https://github.com/tensorflow/models/blob/master/research/'\n", - " 'deeplab/g3doc/img/%s.jpg?raw=true')\n", - "\n", - "\n", - "def run_visualization(url):\n", - " \"\"\"Inferences DeepLab model and visualizes result.\"\"\"\n", - " try:\n", - " f = urllib.request.urlopen(url)\n", - " jpeg_str = f.read()\n", - " original_im = Image.open(BytesIO(jpeg_str))\n", - " except IOError:\n", - " print('Cannot retrieve image. Please check url: ' + url)\n", - " return\n", - "\n", - " print('running deeplab on image %s...' % url)\n", - " resized_im, seg_map = MODEL.run(original_im)\n", - "\n", - " vis_segmentation(resized_im, seg_map)\n", - "\n", - "\n", - "image_url = IMAGE_URL or _SAMPLE_URL % SAMPLE_IMAGE\n", - "run_visualization(image_url)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "aUbVoHScTJYe" - }, - "source": [ - "## What's next\n", - "\n", - "* Learn about [Cloud TPUs](https://cloud.google.com/tpu/docs) that Google designed and optimized specifically to speed up and scale up ML workloads for training and inference and to enable ML engineers and researchers to iterate more quickly.\n", - "* Explore the range of [Cloud TPU tutorials and Colabs](https://cloud.google.com/tpu/docs/tutorials) to find other examples that can be used when implementing your ML project.\n", - "* For more information on running the DeepLab model on Cloud TPUs, see the [DeepLab tutorial](https://cloud.google.com/tpu/docs/tutorials/deeplab).\n" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "DeepLab Demo.ipynb", - "provenance": [], - "toc_visible": true, - "version": "0.3.2" - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/research/deeplab/deprecated/__init__.py b/research/deeplab/deprecated/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deeplab/deprecated/segmentation_dataset.py b/research/deeplab/deprecated/segmentation_dataset.py deleted file mode 100644 index 8a6a8c766e4..00000000000 --- a/research/deeplab/deprecated/segmentation_dataset.py +++ /dev/null @@ -1,200 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Provides data from semantic segmentation datasets. - -The SegmentationDataset class provides both images and annotations (semantic -segmentation and/or instance segmentation) for TensorFlow. Currently, we -support the following datasets: - -1. PASCAL VOC 2012 (http://host.robots.ox.ac.uk/pascal/VOC/voc2012/). - -PASCAL VOC 2012 semantic segmentation dataset annotates 20 foreground objects -(e.g., bike, person, and so on) and leaves all the other semantic classes as -one background class. The dataset contains 1464, 1449, and 1456 annotated -images for the training, validation and test respectively. - -2. Cityscapes dataset (https://www.cityscapes-dataset.com) - -The Cityscapes dataset contains 19 semantic labels (such as road, person, car, -and so on) for urban street scenes. - -3. ADE20K dataset (http://groups.csail.mit.edu/vision/datasets/ADE20K) - -The ADE20K dataset contains 150 semantic labels both urban street scenes and -indoor scenes. - -References: - M. Everingham, S. M. A. Eslami, L. V. Gool, C. K. I. Williams, J. Winn, - and A. Zisserman, The pascal visual object classes challenge a retrospective. - IJCV, 2014. - - M. Cordts, M. Omran, S. Ramos, T. Rehfeld, M. Enzweiler, R. Benenson, - U. Franke, S. Roth, and B. Schiele, "The cityscapes dataset for semantic urban - scene understanding," In Proc. of CVPR, 2016. - - B. Zhou, H. Zhao, X. Puig, S. Fidler, A. Barriuso, A. Torralba, "Scene Parsing - through ADE20K dataset", In Proc. of CVPR, 2017. -""" -import collections -import os.path -import tensorflow as tf -from tensorflow.contrib import slim as contrib_slim - -slim = contrib_slim - -dataset = slim.dataset - -tfexample_decoder = slim.tfexample_decoder - - -_ITEMS_TO_DESCRIPTIONS = { - 'image': 'A color image of varying height and width.', - 'labels_class': ('A semantic segmentation label whose size matches image.' - 'Its values range from 0 (background) to num_classes.'), -} - -# Named tuple to describe the dataset properties. -DatasetDescriptor = collections.namedtuple( - 'DatasetDescriptor', - ['splits_to_sizes', # Splits of the dataset into training, val, and test. - 'num_classes', # Number of semantic classes, including the background - # class (if exists). For example, there are 20 - # foreground classes + 1 background class in the PASCAL - # VOC 2012 dataset. Thus, we set num_classes=21. - 'ignore_label', # Ignore label value. - ] -) - -_CITYSCAPES_INFORMATION = DatasetDescriptor( - splits_to_sizes={ - 'train_fine': 2975, - 'val_fine': 500, - }, - num_classes=19, - ignore_label=255, -) - -_PASCAL_VOC_SEG_INFORMATION = DatasetDescriptor( - splits_to_sizes={ - 'train': 1464, - 'train_aug': 10582, - 'trainval': 2913, - 'val': 1449, - }, - num_classes=21, - ignore_label=255, -) - -# These number (i.e., 'train'/'test') seems to have to be hard coded -# You are required to figure it out for your training/testing example. -_ADE20K_INFORMATION = DatasetDescriptor( - splits_to_sizes={ - 'train': 20210, # num of samples in images/training - 'val': 2000, # num of samples in images/validation - }, - num_classes=151, - ignore_label=0, -) - - -_DATASETS_INFORMATION = { - 'cityscapes': _CITYSCAPES_INFORMATION, - 'pascal_voc_seg': _PASCAL_VOC_SEG_INFORMATION, - 'ade20k': _ADE20K_INFORMATION, -} - -# Default file pattern of TFRecord of TensorFlow Example. -_FILE_PATTERN = '%s-*' - - -def get_cityscapes_dataset_name(): - return 'cityscapes' - - -def get_dataset(dataset_name, split_name, dataset_dir): - """Gets an instance of slim Dataset. - - Args: - dataset_name: Dataset name. - split_name: A train/val Split name. - dataset_dir: The directory of the dataset sources. - - Returns: - An instance of slim Dataset. - - Raises: - ValueError: if the dataset_name or split_name is not recognized. - """ - if dataset_name not in _DATASETS_INFORMATION: - raise ValueError('The specified dataset is not supported yet.') - - splits_to_sizes = _DATASETS_INFORMATION[dataset_name].splits_to_sizes - - if split_name not in splits_to_sizes: - raise ValueError('data split name %s not recognized' % split_name) - - # Prepare the variables for different datasets. - num_classes = _DATASETS_INFORMATION[dataset_name].num_classes - ignore_label = _DATASETS_INFORMATION[dataset_name].ignore_label - - file_pattern = _FILE_PATTERN - file_pattern = os.path.join(dataset_dir, file_pattern % split_name) - - # Specify how the TF-Examples are decoded. - keys_to_features = { - 'image/encoded': tf.FixedLenFeature( - (), tf.string, default_value=''), - 'image/filename': tf.FixedLenFeature( - (), tf.string, default_value=''), - 'image/format': tf.FixedLenFeature( - (), tf.string, default_value='jpeg'), - 'image/height': tf.FixedLenFeature( - (), tf.int64, default_value=0), - 'image/width': tf.FixedLenFeature( - (), tf.int64, default_value=0), - 'image/segmentation/class/encoded': tf.FixedLenFeature( - (), tf.string, default_value=''), - 'image/segmentation/class/format': tf.FixedLenFeature( - (), tf.string, default_value='png'), - } - items_to_handlers = { - 'image': tfexample_decoder.Image( - image_key='image/encoded', - format_key='image/format', - channels=3), - 'image_name': tfexample_decoder.Tensor('image/filename'), - 'height': tfexample_decoder.Tensor('image/height'), - 'width': tfexample_decoder.Tensor('image/width'), - 'labels_class': tfexample_decoder.Image( - image_key='image/segmentation/class/encoded', - format_key='image/segmentation/class/format', - channels=1), - } - - decoder = tfexample_decoder.TFExampleDecoder( - keys_to_features, items_to_handlers) - - return dataset.Dataset( - data_sources=file_pattern, - reader=tf.TFRecordReader, - decoder=decoder, - num_samples=splits_to_sizes[split_name], - items_to_descriptions=_ITEMS_TO_DESCRIPTIONS, - ignore_label=ignore_label, - num_classes=num_classes, - name=dataset_name, - multi_label=True) diff --git a/research/deeplab/eval.py b/research/deeplab/eval.py deleted file mode 100644 index 4f5fb8ba9c7..00000000000 --- a/research/deeplab/eval.py +++ /dev/null @@ -1,227 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Evaluation script for the DeepLab model. - -See model.py for more details and usage. -""" - -import numpy as np -import six -import tensorflow as tf -from tensorflow.contrib import metrics as contrib_metrics -from tensorflow.contrib import quantize as contrib_quantize -from tensorflow.contrib import tfprof as contrib_tfprof -from tensorflow.contrib import training as contrib_training -from deeplab import common -from deeplab import model -from deeplab.datasets import data_generator - -flags = tf.app.flags -FLAGS = flags.FLAGS - -flags.DEFINE_string('master', '', 'BNS name of the tensorflow server') - -# Settings for log directories. - -flags.DEFINE_string('eval_logdir', None, 'Where to write the event logs.') - -flags.DEFINE_string('checkpoint_dir', None, 'Directory of model checkpoints.') - -# Settings for evaluating the model. - -flags.DEFINE_integer('eval_batch_size', 1, - 'The number of images in each batch during evaluation.') - -flags.DEFINE_list('eval_crop_size', '513,513', - 'Image crop size [height, width] for evaluation.') - -flags.DEFINE_integer('eval_interval_secs', 60 * 5, - 'How often (in seconds) to run evaluation.') - -# For `xception_65`, use atrous_rates = [12, 24, 36] if output_stride = 8, or -# rates = [6, 12, 18] if output_stride = 16. For `mobilenet_v2`, use None. Note -# one could use different atrous_rates/output_stride during training/evaluation. -flags.DEFINE_multi_integer('atrous_rates', None, - 'Atrous rates for atrous spatial pyramid pooling.') - -flags.DEFINE_integer('output_stride', 16, - 'The ratio of input to output spatial resolution.') - -# Change to [0.5, 0.75, 1.0, 1.25, 1.5, 1.75] for multi-scale test. -flags.DEFINE_multi_float('eval_scales', [1.0], - 'The scales to resize images for evaluation.') - -# Change to True for adding flipped images during test. -flags.DEFINE_bool('add_flipped_images', False, - 'Add flipped images for evaluation or not.') - -flags.DEFINE_integer( - 'quantize_delay_step', -1, - 'Steps to start quantized training. If < 0, will not quantize model.') - -# Dataset settings. - -flags.DEFINE_string('dataset', 'pascal_voc_seg', - 'Name of the segmentation dataset.') - -flags.DEFINE_string('eval_split', 'val', - 'Which split of the dataset used for evaluation') - -flags.DEFINE_string('dataset_dir', None, 'Where the dataset reside.') - -flags.DEFINE_integer('max_number_of_evaluations', 0, - 'Maximum number of eval iterations. Will loop ' - 'indefinitely upon nonpositive values.') - - -def main(unused_argv): - tf.logging.set_verbosity(tf.logging.INFO) - - dataset = data_generator.Dataset( - dataset_name=FLAGS.dataset, - split_name=FLAGS.eval_split, - dataset_dir=FLAGS.dataset_dir, - batch_size=FLAGS.eval_batch_size, - crop_size=[int(sz) for sz in FLAGS.eval_crop_size], - min_resize_value=FLAGS.min_resize_value, - max_resize_value=FLAGS.max_resize_value, - resize_factor=FLAGS.resize_factor, - model_variant=FLAGS.model_variant, - num_readers=2, - is_training=False, - should_shuffle=False, - should_repeat=False) - - tf.gfile.MakeDirs(FLAGS.eval_logdir) - tf.logging.info('Evaluating on %s set', FLAGS.eval_split) - - with tf.Graph().as_default(): - samples = dataset.get_one_shot_iterator().get_next() - - model_options = common.ModelOptions( - outputs_to_num_classes={common.OUTPUT_TYPE: dataset.num_of_classes}, - crop_size=[int(sz) for sz in FLAGS.eval_crop_size], - atrous_rates=FLAGS.atrous_rates, - output_stride=FLAGS.output_stride) - - # Set shape in order for tf.contrib.tfprof.model_analyzer to work properly. - samples[common.IMAGE].set_shape( - [FLAGS.eval_batch_size, - int(FLAGS.eval_crop_size[0]), - int(FLAGS.eval_crop_size[1]), - 3]) - if tuple(FLAGS.eval_scales) == (1.0,): - tf.logging.info('Performing single-scale test.') - predictions = model.predict_labels(samples[common.IMAGE], model_options, - image_pyramid=FLAGS.image_pyramid) - else: - tf.logging.info('Performing multi-scale test.') - if FLAGS.quantize_delay_step >= 0: - raise ValueError( - 'Quantize mode is not supported with multi-scale test.') - - predictions = model.predict_labels_multi_scale( - samples[common.IMAGE], - model_options=model_options, - eval_scales=FLAGS.eval_scales, - add_flipped_images=FLAGS.add_flipped_images) - predictions = predictions[common.OUTPUT_TYPE] - predictions = tf.reshape(predictions, shape=[-1]) - labels = tf.reshape(samples[common.LABEL], shape=[-1]) - weights = tf.to_float(tf.not_equal(labels, dataset.ignore_label)) - - # Set ignore_label regions to label 0, because metrics.mean_iou requires - # range of labels = [0, dataset.num_classes). Note the ignore_label regions - # are not evaluated since the corresponding regions contain weights = 0. - labels = tf.where( - tf.equal(labels, dataset.ignore_label), tf.zeros_like(labels), labels) - - predictions_tag = 'miou' - for eval_scale in FLAGS.eval_scales: - predictions_tag += '_' + str(eval_scale) - if FLAGS.add_flipped_images: - predictions_tag += '_flipped' - - # Define the evaluation metric. - metric_map = {} - num_classes = dataset.num_of_classes - metric_map['eval/%s_overall' % predictions_tag] = tf.metrics.mean_iou( - labels=labels, predictions=predictions, num_classes=num_classes, - weights=weights) - # IoU for each class. - one_hot_predictions = tf.one_hot(predictions, num_classes) - one_hot_predictions = tf.reshape(one_hot_predictions, [-1, num_classes]) - one_hot_labels = tf.one_hot(labels, num_classes) - one_hot_labels = tf.reshape(one_hot_labels, [-1, num_classes]) - for c in range(num_classes): - predictions_tag_c = '%s_class_%d' % (predictions_tag, c) - tp, tp_op = tf.metrics.true_positives( - labels=one_hot_labels[:, c], predictions=one_hot_predictions[:, c], - weights=weights) - fp, fp_op = tf.metrics.false_positives( - labels=one_hot_labels[:, c], predictions=one_hot_predictions[:, c], - weights=weights) - fn, fn_op = tf.metrics.false_negatives( - labels=one_hot_labels[:, c], predictions=one_hot_predictions[:, c], - weights=weights) - tp_fp_fn_op = tf.group(tp_op, fp_op, fn_op) - iou = tf.where(tf.greater(tp + fn, 0.0), - tp / (tp + fn + fp), - tf.constant(np.NaN)) - metric_map['eval/%s' % predictions_tag_c] = (iou, tp_fp_fn_op) - - (metrics_to_values, - metrics_to_updates) = contrib_metrics.aggregate_metric_map(metric_map) - - summary_ops = [] - for metric_name, metric_value in six.iteritems(metrics_to_values): - op = tf.summary.scalar(metric_name, metric_value) - op = tf.Print(op, [metric_value], metric_name) - summary_ops.append(op) - - summary_op = tf.summary.merge(summary_ops) - summary_hook = contrib_training.SummaryAtEndHook( - log_dir=FLAGS.eval_logdir, summary_op=summary_op) - hooks = [summary_hook] - - num_eval_iters = None - if FLAGS.max_number_of_evaluations > 0: - num_eval_iters = FLAGS.max_number_of_evaluations - - if FLAGS.quantize_delay_step >= 0: - contrib_quantize.create_eval_graph() - - contrib_tfprof.model_analyzer.print_model_analysis( - tf.get_default_graph(), - tfprof_options=contrib_tfprof.model_analyzer - .TRAINABLE_VARS_PARAMS_STAT_OPTIONS) - contrib_tfprof.model_analyzer.print_model_analysis( - tf.get_default_graph(), - tfprof_options=contrib_tfprof.model_analyzer.FLOAT_OPS_OPTIONS) - contrib_training.evaluate_repeatedly( - checkpoint_dir=FLAGS.checkpoint_dir, - master=FLAGS.master, - eval_ops=list(metrics_to_updates.values()), - max_number_of_evaluations=num_eval_iters, - hooks=hooks, - eval_interval_secs=FLAGS.eval_interval_secs) - - -if __name__ == '__main__': - flags.mark_flag_as_required('checkpoint_dir') - flags.mark_flag_as_required('eval_logdir') - flags.mark_flag_as_required('dataset_dir') - tf.app.run() diff --git a/research/deeplab/evaluation/README.md b/research/deeplab/evaluation/README.md deleted file mode 100644 index 69255384e9a..00000000000 --- a/research/deeplab/evaluation/README.md +++ /dev/null @@ -1,311 +0,0 @@ -# Evaluation Metrics for Whole Image Parsing - -Whole Image Parsing [1], also known as Panoptic Segmentation [2], generalizes -the tasks of semantic segmentation for "stuff" classes and instance -segmentation for "thing" classes, assigning both semantic and instance labels -to every pixel in an image. - -Previous works evaluate the parsing result with separate metrics (e.g., one for -semantic segmentation result and one for object detection result). Recently, -Kirillov et al. propose the unified instance-based Panoptic Quality (PQ) metric -[2] into several benchmarks [3, 4]. - -However, we notice that the instance-based PQ metric often places -disproportionate emphasis on small instance parsing, as well as on "thing" over -"stuff" classes. To remedy these effects, we propose an alternative -region-based Parsing Covering (PC) metric [5], which adapts the Covering -metric [6], previously used for class-agnostics segmentation quality -evaluation, to the task of image parsing. - -Here, we provide implementation of both PQ and PC for evaluating the parsing -results. We briefly explain both metrics below for reference. - -## Panoptic Quality (PQ) - -Given a groundtruth segmentation S and a predicted segmentation S', PQ is -defined as follows: - -

- -

- -where R and R' are groundtruth regions and predicted regions respectively, -and |TP|, |FP|, and |FN| are the number of true positives, false postives, -and false negatives. The matching is determined by a threshold of 0.5 -Intersection-Over-Union (IOU). - -PQ treats all regions of the same ‘stuff‘ class as one instance, and the -size of instances is not considered. For example, instances with 10 × 10 -pixels contribute equally to the metric as instances with 1000 × 1000 pixels. -Therefore, PQ is sensitive to false positives with small regions and some -heuristics could improve the performance, such as removing those small -regions (as also pointed out in the open-sourced evaluation code from [2]). -Thus, we argue that PQ is suitable in applications where one cares equally for -the parsing quality of instances irrespective of their sizes. - -## Parsing Covering (PC) - -We notice that there are applications where one pays more attention to large -objects, e.g., autonomous driving (where nearby objects are more important -than far away ones). Motivated by this, we propose to also evaluate the -quality of image parsing results by extending the existing Covering metric [5], -which accounts for instance sizes. Specifically, our proposed metric, Parsing -Covering (PC), is defined as follows: - -

- -

- - -where Si and Si' are the groundtruth segmentation and -predicted segmentation for the i-th semantic class respectively, and -Ni is the total number of pixels of groundtruth regions from -Si . The Covering for class i, Covi , is computed in -the same way as the original Covering metric except that only groundtruth -regions from Si and predicted regions from Si' are -considered. PC is then obtained by computing the average of Covi -over C semantic classes. - -A notable difference between PQ and the proposed PC is that there is no -matching involved in PC and hence no matching threshold. As an attempt to -treat equally "thing" and "stuff", the segmentation of "stuff" classes still -receives partial PC score if the segmentation is only partially correct. For -example, if one out of three equally-sized trees is perfectly segmented, the -model will get the same partial score by using PC regardless of considering -"tree" as "stuff" or "thing". - -## Tutorial - -To evaluate the parsing results with PQ and PC, we provide two options: - -1. Python off-line evaluation with results saved in the [COCO format](http://cocodataset.org/#format-results). -2. TensorFlow on-line evaluation. - -Below, we explain each option in detail. - -#### 1. Python off-line evaluation with results saved in COCO format - -[COCO result format](http://cocodataset.org/#format-results) has been -adopted by several benchmarks [3, 4]. Therefore, we provide a convenient -function, `eval_coco_format`, to evaluate the results saved in COCO format -in terms of PC and re-implemented PQ. - -Before using the provided function, the users need to download the official COCO -panotpic segmentation task API. Please see [installation](../g3doc/installation.md#add-libraries-to-pythonpath) -for reference. - -Once the official COCO panoptic segmentation task API is downloaded, the -users should be able to run the `eval_coco_format.py` to evaluate the parsing -results in terms of both PC and reimplemented PQ. - -To be concrete, let's take a look at the function, `eval_coco_format` in -`eval_coco_format.py`: - -```python -eval_coco_format(gt_json_file, - pred_json_file, - gt_folder=None, - pred_folder=None, - metric='pq', - num_categories=201, - ignored_label=0, - max_instances_per_category=256, - intersection_offset=None, - normalize_by_image_size=True, - num_workers=0, - print_digits=3): - -``` -where - -1. `gt_json_file`: Path to a JSON file giving ground-truth annotations in COCO -format. -2. `pred_json_file`: Path to a JSON file for the predictions to evaluate. -3. `gt_folder`: Folder containing panoptic-format ID images to match -ground-truth annotations to image regions. -4. `pred_folder`: Path to a folder containing ID images for predictions. -5. `metric`: Name of a metric to compute. Set to `pc`, `pq` for evaluation in PC -or PQ, respectively. -6. `num_categories`: The number of segmentation categories (or "classes") in the -dataset. -7. `ignored_label`: A category id that is ignored in evaluation, e.g. the "void" -label in COCO panoptic segmentation dataset. -8. `max_instances_per_category`: The maximum number of instances for each -category to ensure unique instance labels. -9. `intersection_offset`: The maximum number of unique labels. -10. `normalize_by_image_size`: Whether to normalize groundtruth instance region -areas by image size when using PC. -11. `num_workers`: If set to a positive number, will spawn child processes to -compute parts of the metric in parallel by splitting the images between the -workers. If set to -1, will use the value of multiprocessing.cpu_count(). -12. `print_digits`: Number of significant digits to print in summary of computed -metrics. - -The input arguments have default values set for the COCO panoptic segmentation -dataset. Thus, users only need to provide the `gt_json_file` and the -`pred_json_file` (following the COCO format) to run the evaluation on COCO with -PQ. If users want to evaluate the results on other datasets, they may need -to change the default values. - -As an example, the interested users could take a look at the provided unit -test, `test_compare_pq_with_reference_eval`, in `eval_coco_format_test.py`. - -#### 2. TensorFlow on-line evaluation - -Users may also want to run the TensorFlow on-line evaluation, similar to the -[tf.contrib.metrics.streaming_mean_iou](https://www.tensorflow.org/api_docs/python/tf/contrib/metrics/streaming_mean_iou). - -Below, we provide a code snippet that shows how to use the provided -`streaming_panoptic_quality` and `streaming_parsing_covering`. - -```python -metric_map = {} -metric_map['panoptic_quality'] = streaming_metrics.streaming_panoptic_quality( - category_label, - instance_label, - category_prediction, - instance_prediction, - num_classes=201, - max_instances_per_category=256, - ignored_label=0, - offset=256*256) -metric_map['parsing_covering'] = streaming_metrics.streaming_parsing_covering( - category_label, - instance_label, - category_prediction, - instance_prediction, - num_classes=201, - max_instances_per_category=256, - ignored_label=0, - offset=256*256, - normalize_by_image_size=True) -metrics_to_values, metrics_to_updates = slim.metrics.aggregate_metric_map( - metric_map) -``` -where `metric_map` is a dictionary storing the streamed results of PQ and PC. - -The `category_label` and the `instance_label` are the semantic segmentation and -instance segmentation groundtruth, respectively. That is, in the panoptic -segmentation format: -panoptic_label = category_label * max_instances_per_category + instance_label. -Similarly, the `category_prediction` and the `instance_prediction` are the -predicted semantic segmentation and instance segmentation, respectively. - -Below, we provide a code snippet about how to summarize the results in the -context of tf.summary. - -```python -summary_ops = [] -for metric_name, metric_value in metrics_to_values.iteritems(): - if metric_name == 'panoptic_quality': - [pq, sq, rq, total_tp, total_fn, total_fp] = tf.unstack( - metric_value, 6, axis=0) - panoptic_metrics = { - # Panoptic quality. - 'pq': pq, - # Segmentation quality. - 'sq': sq, - # Recognition quality. - 'rq': rq, - # Total true positives. - 'total_tp': total_tp, - # Total false negatives. - 'total_fn': total_fn, - # Total false positives. - 'total_fp': total_fp, - } - # Find the valid classes that will be used for evaluation. We will - # ignore the `ignore_label` class and other classes which have (tp + fn - # + fp) equal to 0. - valid_classes = tf.logical_and( - tf.not_equal(tf.range(0, num_classes), void_label), - tf.not_equal(total_tp + total_fn + total_fp, 0)) - for target_metric, target_value in panoptic_metrics.iteritems(): - output_metric_name = '{}_{}'.format(metric_name, target_metric) - op = tf.summary.scalar( - output_metric_name, - tf.reduce_mean(tf.boolean_mask(target_value, valid_classes))) - op = tf.Print(op, [target_value], output_metric_name + '_classwise: ', - summarize=num_classes) - op = tf.Print( - op, - [tf.reduce_mean(tf.boolean_mask(target_value, valid_classes))], - output_metric_name + '_mean: ', - summarize=1) - summary_ops.append(op) - elif metric_name == 'parsing_covering': - [per_class_covering, - total_per_class_weighted_ious, - total_per_class_gt_areas] = tf.unstack(metric_value, 3, axis=0) - # Find the valid classes that will be used for evaluation. We will - # ignore the `void_label` class and other classes which have - # total_per_class_weighted_ious + total_per_class_gt_areas equal to 0. - valid_classes = tf.logical_and( - tf.not_equal(tf.range(0, num_classes), void_label), - tf.not_equal( - total_per_class_weighted_ious + total_per_class_gt_areas, 0)) - op = tf.summary.scalar( - metric_name, - tf.reduce_mean(tf.boolean_mask(per_class_covering, valid_classes))) - op = tf.Print(op, [per_class_covering], metric_name + '_classwise: ', - summarize=num_classes) - op = tf.Print( - op, - [tf.reduce_mean( - tf.boolean_mask(per_class_covering, valid_classes))], - metric_name + '_mean: ', - summarize=1) - summary_ops.append(op) - else: - raise ValueError('The metric_name "%s" is not supported.' % metric_name) -``` - -Afterwards, the users could use the following code to run the evaluation in -TensorFlow. - -Users can take a look at eval.py for reference which provides a simple -example to run the streaming evaluation of mIOU for semantic segmentation. - -```python -metric_values = slim.evaluation.evaluation_loop( - master=FLAGS.master, - checkpoint_dir=FLAGS.checkpoint_dir, - logdir=FLAGS.eval_logdir, - num_evals=num_batches, - eval_op=metrics_to_updates.values(), - final_op=metrics_to_values.values(), - summary_op=tf.summary.merge(summary_ops), - max_number_of_evaluations=FLAGS.max_number_of_evaluations, - eval_interval_secs=FLAGS.eval_interval_secs) -``` - - -### References - -1. **Image Parsing: Unifying Segmentation, Detection, and Recognition**
- Zhuowen Tu, Xiangrong Chen, Alan L. Yuille, and Song-Chun Zhu
- IJCV, 2005. - -2. **Panoptic Segmentation**
- Alexander Kirillov, Kaiming He, Ross Girshick, Carsten Rother and Piotr - Dollár
- arXiv:1801.00868, 2018. - -3. **Microsoft COCO: Common Objects in Context**
- Tsung-Yi Lin, Michael Maire, Serge Belongie, Lubomir Bourdev, Ross - Girshick, James Hays, Pietro Perona, Deva Ramanan, C. Lawrence Zitnick, - Piotr Dollar
- In the Proc. of ECCV, 2014. - -4. **The Mapillary Vistas Dataset for Semantic Understanding of Street Scenes**
- Gerhard Neuhold, Tobias Ollmann, Samuel Rota Bulò, and Peter Kontschieder
- In the Proc. of ICCV, 2017. - -5. **DeeperLab: Single-Shot Image Parser**
- Tien-Ju Yang, Maxwell D. Collins, Yukun Zhu, Jyh-Jing Hwang, Ting Liu, - Xiao Zhang, Vivienne Sze, George Papandreou, Liang-Chieh Chen
- arXiv: 1902.05093, 2019. - -6. **Contour Detection and Hierarchical Image Segmentation**
- Pablo Arbelaez, Michael Maire, Charless Fowlkes, and Jitendra Malik
- PAMI, 2011 diff --git a/research/deeplab/evaluation/__init__.py b/research/deeplab/evaluation/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deeplab/evaluation/base_metric.py b/research/deeplab/evaluation/base_metric.py deleted file mode 100644 index ee7606ef44c..00000000000 --- a/research/deeplab/evaluation/base_metric.py +++ /dev/null @@ -1,191 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Defines the top-level interface for evaluating segmentations.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc -import numpy as np -import six - - -_EPSILON = 1e-10 - - -def realdiv_maybe_zero(x, y): - """Element-wise x / y where y may contain zeros, for those returns 0 too.""" - return np.where( - np.less(np.abs(y), _EPSILON), np.zeros_like(x), np.divide(x, y)) - - -@six.add_metaclass(abc.ABCMeta) -class SegmentationMetric(object): - """Abstract base class for computers of segmentation metrics. - - Subclasses will implement both: - 1. Comparing the predicted segmentation for an image with the groundtruth. - 2. Computing the final metric over a set of images. - These are often done as separate steps, due to the need to accumulate - intermediate values other than the metric itself across images, computing the - actual metric value only on these accumulations after all the images have been - compared. - - A simple usage would be: - - metric = MetricImplementation(...) - for , in evaluation_set: - = run_segmentation() - metric.compare_and_accumulate(, ) - print(metric.result()) - - """ - - def __init__(self, num_categories, ignored_label, max_instances_per_category, - offset): - """Base initialization for SegmentationMetric. - - Args: - num_categories: The number of segmentation categories (or "classes" in the - dataset. - ignored_label: A category id that is ignored in evaluation, e.g. the void - label as defined in COCO panoptic segmentation dataset. - max_instances_per_category: The maximum number of instances for each - category. Used in ensuring unique instance labels. - offset: The maximum number of unique labels. This is used, by multiplying - the ground-truth labels, to generate unique ids for individual regions - of overlap between groundtruth and predicted segments. - """ - self.num_categories = num_categories - self.ignored_label = ignored_label - self.max_instances_per_category = max_instances_per_category - self.offset = offset - self.reset() - - def _naively_combine_labels(self, category_array, instance_array): - """Naively creates a combined label array from categories and instances.""" - return (category_array.astype(np.uint32) * self.max_instances_per_category + - instance_array.astype(np.uint32)) - - @abc.abstractmethod - def compare_and_accumulate( - self, groundtruth_category_array, groundtruth_instance_array, - predicted_category_array, predicted_instance_array): - """Compares predicted segmentation with groundtruth, accumulates its metric. - - It is not assumed that instance ids are unique across different categories. - See for example combine_semantic_and_instance_predictions.py in official - PanopticAPI evaluation code for issues to consider when fusing category - and instance labels. - - Instances ids of the ignored category have the meaning that id 0 is "void" - and remaining ones are crowd instances. - - Args: - groundtruth_category_array: A 2D numpy uint16 array of groundtruth - per-pixel category labels. - groundtruth_instance_array: A 2D numpy uint16 array of groundtruth - instance labels. - predicted_category_array: A 2D numpy uint16 array of predicted per-pixel - category labels. - predicted_instance_array: A 2D numpy uint16 array of predicted instance - labels. - - Returns: - The value of the metric over all comparisons done so far, including this - one, as a float scalar. - """ - raise NotImplementedError('Must be implemented in subclasses.') - - @abc.abstractmethod - def result(self): - """Computes the metric over all comparisons done so far.""" - raise NotImplementedError('Must be implemented in subclasses.') - - @abc.abstractmethod - def detailed_results(self, is_thing=None): - """Computes and returns the detailed final metric results. - - Args: - is_thing: A boolean array of length `num_categories`. The entry - `is_thing[category_id]` is True iff that category is a "thing" category - instead of "stuff." - - Returns: - A dictionary with a breakdown of metrics and/or metric factors by things, - stuff, and all categories. - """ - raise NotImplementedError('Not implemented in subclasses.') - - @abc.abstractmethod - def result_per_category(self): - """For supported metrics, return individual per-category metric values. - - Returns: - A numpy array of shape `[self.num_categories]`, where index `i` is the - metrics value over only that category. - """ - raise NotImplementedError('Not implemented in subclass.') - - def print_detailed_results(self, is_thing=None, print_digits=3): - """Prints out a detailed breakdown of metric results. - - Args: - is_thing: A boolean array of length num_categories. - `is_thing[category_id]` will say whether that category is a "thing" - rather than "stuff." - print_digits: Number of significant digits to print in computed metrics. - """ - raise NotImplementedError('Not implemented in subclass.') - - @abc.abstractmethod - def merge(self, other_instance): - """Combines the accumulated results of another instance into self. - - The following two cases should put `metric_a` into an equivalent state. - - Case 1 (with merge): - - metric_a = MetricsSubclass(...) - metric_a.compare_and_accumulate() - metric_a.compare_and_accumulate() - - metric_b = MetricsSubclass(...) - metric_b.compare_and_accumulate() - metric_b.compare_and_accumulate() - - metric_a.merge(metric_b) - - Case 2 (without merge): - - metric_a = MetricsSubclass(...) - metric_a.compare_and_accumulate() - metric_a.compare_and_accumulate() - metric_a.compare_and_accumulate() - metric_a.compare_and_accumulate() - - Args: - other_instance: Another compatible instance of the same metric subclass. - """ - raise NotImplementedError('Not implemented in subclass.') - - @abc.abstractmethod - def reset(self): - """Resets the accumulation to the metric class's state at initialization. - - Note that this function will be called in SegmentationMetric.__init__. - """ - raise NotImplementedError('Must be implemented in subclasses.') diff --git a/research/deeplab/evaluation/eval_coco_format.py b/research/deeplab/evaluation/eval_coco_format.py deleted file mode 100644 index 1a26446f16b..00000000000 --- a/research/deeplab/evaluation/eval_coco_format.py +++ /dev/null @@ -1,338 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Computes evaluation metrics on groundtruth and predictions in COCO format. - -The Common Objects in Context (COCO) dataset defines a format for specifying -combined semantic and instance segmentations as "panoptic" segmentations. This -is done with the combination of JSON and image files as specified at: -http://cocodataset.org/#format-results -where the JSON file specifies the overall structure of the result, -including the categories for each annotation, and the images specify the image -region for each annotation in that image by its ID. - -This script computes additional metrics such as Parsing Covering on datasets and -predictions in this format. An implementation of Panoptic Quality is also -provided for convenience. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import json -import multiprocessing -import os - -from absl import app -from absl import flags -from absl import logging -import numpy as np -from PIL import Image -import utils as panopticapi_utils -import six - -from deeplab.evaluation import panoptic_quality -from deeplab.evaluation import parsing_covering - -FLAGS = flags.FLAGS - -flags.DEFINE_string( - 'gt_json_file', None, - ' Path to a JSON file giving ground-truth annotations in COCO format.') -flags.DEFINE_string('pred_json_file', None, - 'Path to a JSON file for the predictions to evaluate.') -flags.DEFINE_string( - 'gt_folder', None, - 'Folder containing panoptic-format ID images to match ground-truth ' - 'annotations to image regions.') -flags.DEFINE_string('pred_folder', None, - 'Folder containing ID images for predictions.') -flags.DEFINE_enum( - 'metric', 'pq', ['pq', 'pc'], 'Shorthand name of a metric to compute. ' - 'Supported values are:\n' - 'Panoptic Quality (pq)\n' - 'Parsing Covering (pc)') -flags.DEFINE_integer( - 'num_categories', 201, - 'The number of segmentation categories (or "classes") in the dataset.') -flags.DEFINE_integer( - 'ignored_label', 0, - 'A category id that is ignored in evaluation, e.g. the void label as ' - 'defined in COCO panoptic segmentation dataset.') -flags.DEFINE_integer( - 'max_instances_per_category', 256, - 'The maximum number of instances for each category. Used in ensuring ' - 'unique instance labels.') -flags.DEFINE_integer('intersection_offset', None, - 'The maximum number of unique labels.') -flags.DEFINE_bool( - 'normalize_by_image_size', True, - 'Whether to normalize groundtruth instance region areas by image size. If ' - 'True, groundtruth instance areas and weighted IoUs will be divided by the ' - 'size of the corresponding image before accumulated across the dataset. ' - 'Only used for Parsing Covering (pc) evaluation.') -flags.DEFINE_integer( - 'num_workers', 0, 'If set to a positive number, will spawn child processes ' - 'to compute parts of the metric in parallel by splitting ' - 'the images between the workers. If set to -1, will use ' - 'the value of multiprocessing.cpu_count().') -flags.DEFINE_integer('print_digits', 3, - 'Number of significant digits to print in metrics.') - - -def _build_metric(metric, - num_categories, - ignored_label, - max_instances_per_category, - intersection_offset=None, - normalize_by_image_size=True): - """Creates a metric aggregator objet of the given name.""" - if metric == 'pq': - logging.warning('One should check Panoptic Quality results against the ' - 'official COCO API code. Small numerical differences ' - '(< 0.1%) can be magnified by rounding.') - return panoptic_quality.PanopticQuality(num_categories, ignored_label, - max_instances_per_category, - intersection_offset) - elif metric == 'pc': - return parsing_covering.ParsingCovering( - num_categories, ignored_label, max_instances_per_category, - intersection_offset, normalize_by_image_size) - else: - raise ValueError('No implementation for metric "%s"' % metric) - - -def _matched_annotations(gt_json, pred_json): - """Yields a set of (groundtruth, prediction) image annotation pairs..""" - image_id_to_pred_ann = { - annotation['image_id']: annotation - for annotation in pred_json['annotations'] - } - for gt_ann in gt_json['annotations']: - image_id = gt_ann['image_id'] - pred_ann = image_id_to_pred_ann[image_id] - yield gt_ann, pred_ann - - -def _open_panoptic_id_image(image_path): - """Loads a COCO-format panoptic ID image from file.""" - return panopticapi_utils.rgb2id( - np.array(Image.open(image_path), dtype=np.uint32)) - - -def _split_panoptic(ann_json, id_array, ignored_label, allow_crowds): - """Given the COCO JSON and ID map, splits into categories and instances.""" - category = np.zeros(id_array.shape, np.uint16) - instance = np.zeros(id_array.shape, np.uint16) - next_instance_id = collections.defaultdict(int) - # Skip instance label 0 for ignored label. That is reserved for void. - next_instance_id[ignored_label] = 1 - for segment_info in ann_json['segments_info']: - if allow_crowds and segment_info['iscrowd']: - category_id = ignored_label - else: - category_id = segment_info['category_id'] - mask = np.equal(id_array, segment_info['id']) - category[mask] = category_id - instance[mask] = next_instance_id[category_id] - next_instance_id[category_id] += 1 - return category, instance - - -def _category_and_instance_from_annotation(ann_json, folder, ignored_label, - allow_crowds): - """Given the COCO JSON annotations, finds maps of categories and instances.""" - panoptic_id_image = _open_panoptic_id_image( - os.path.join(folder, ann_json['file_name'])) - return _split_panoptic(ann_json, panoptic_id_image, ignored_label, - allow_crowds) - - -def _compute_metric(metric_aggregator, gt_folder, pred_folder, - annotation_pairs): - """Iterates over matched annotation pairs and computes a metric over them.""" - for gt_ann, pred_ann in annotation_pairs: - # We only expect "iscrowd" to appear in the ground-truth, and not in model - # output. In predicted JSON it is simply ignored, as done in official code. - gt_category, gt_instance = _category_and_instance_from_annotation( - gt_ann, gt_folder, metric_aggregator.ignored_label, True) - pred_category, pred_instance = _category_and_instance_from_annotation( - pred_ann, pred_folder, metric_aggregator.ignored_label, False) - - metric_aggregator.compare_and_accumulate(gt_category, gt_instance, - pred_category, pred_instance) - return metric_aggregator - - -def _iterate_work_queue(work_queue): - """Creates an iterable that retrieves items from a queue until one is None.""" - task = work_queue.get(block=True) - while task is not None: - yield task - task = work_queue.get(block=True) - - -def _run_metrics_worker(metric_aggregator, gt_folder, pred_folder, work_queue, - result_queue): - result = _compute_metric(metric_aggregator, gt_folder, pred_folder, - _iterate_work_queue(work_queue)) - result_queue.put(result, block=True) - - -def _is_thing_array(categories_json, ignored_label): - """is_thing[category_id] is a bool on if category is "thing" or "stuff".""" - is_thing_dict = {} - for category_json in categories_json: - is_thing_dict[category_json['id']] = bool(category_json['isthing']) - - # Check our assumption that the category ids are consecutive. - # Usually metrics should be able to handle this case, but adding a warning - # here. - max_category_id = max(six.iterkeys(is_thing_dict)) - if len(is_thing_dict) != max_category_id + 1: - seen_ids = six.viewkeys(is_thing_dict) - all_ids = set(six.moves.range(max_category_id + 1)) - unseen_ids = all_ids.difference(seen_ids) - if unseen_ids != {ignored_label}: - logging.warning( - 'Nonconsecutive category ids or no category JSON specified for ids: ' - '%s', unseen_ids) - - is_thing_array = np.zeros(max_category_id + 1) - for category_id, is_thing in six.iteritems(is_thing_dict): - is_thing_array[category_id] = is_thing - - return is_thing_array - - -def eval_coco_format(gt_json_file, - pred_json_file, - gt_folder=None, - pred_folder=None, - metric='pq', - num_categories=201, - ignored_label=0, - max_instances_per_category=256, - intersection_offset=None, - normalize_by_image_size=True, - num_workers=0, - print_digits=3): - """Top-level code to compute metrics on a COCO-format result. - - Note that the default values are set for COCO panoptic segmentation dataset, - and thus the users may want to change it for their own dataset evaluation. - - Args: - gt_json_file: Path to a JSON file giving ground-truth annotations in COCO - format. - pred_json_file: Path to a JSON file for the predictions to evaluate. - gt_folder: Folder containing panoptic-format ID images to match ground-truth - annotations to image regions. - pred_folder: Folder containing ID images for predictions. - metric: Name of a metric to compute. - num_categories: The number of segmentation categories (or "classes") in the - dataset. - ignored_label: A category id that is ignored in evaluation, e.g. the "void" - label as defined in the COCO panoptic segmentation dataset. - max_instances_per_category: The maximum number of instances for each - category. Used in ensuring unique instance labels. - intersection_offset: The maximum number of unique labels. - normalize_by_image_size: Whether to normalize groundtruth instance region - areas by image size. If True, groundtruth instance areas and weighted IoUs - will be divided by the size of the corresponding image before accumulated - across the dataset. Only used for Parsing Covering (pc) evaluation. - num_workers: If set to a positive number, will spawn child processes to - compute parts of the metric in parallel by splitting the images between - the workers. If set to -1, will use the value of - multiprocessing.cpu_count(). - print_digits: Number of significant digits to print in summary of computed - metrics. - - Returns: - The computed result of the metric as a float scalar. - """ - with open(gt_json_file, 'r') as gt_json_fo: - gt_json = json.load(gt_json_fo) - with open(pred_json_file, 'r') as pred_json_fo: - pred_json = json.load(pred_json_fo) - if gt_folder is None: - gt_folder = gt_json_file.replace('.json', '') - if pred_folder is None: - pred_folder = pred_json_file.replace('.json', '') - if intersection_offset is None: - intersection_offset = (num_categories + 1) * max_instances_per_category - - metric_aggregator = _build_metric( - metric, num_categories, ignored_label, max_instances_per_category, - intersection_offset, normalize_by_image_size) - - if num_workers == -1: - logging.info('Attempting to get the CPU count to set # workers.') - num_workers = multiprocessing.cpu_count() - - if num_workers > 0: - logging.info('Computing metric in parallel with %d workers.', num_workers) - work_queue = multiprocessing.Queue() - result_queue = multiprocessing.Queue() - workers = [] - worker_args = (metric_aggregator, gt_folder, pred_folder, work_queue, - result_queue) - for _ in six.moves.range(num_workers): - workers.append( - multiprocessing.Process(target=_run_metrics_worker, args=worker_args)) - for worker in workers: - worker.start() - for ann_pair in _matched_annotations(gt_json, pred_json): - work_queue.put(ann_pair, block=True) - - # Will cause each worker to return a result and terminate upon recieving a - # None task. - for _ in six.moves.range(num_workers): - work_queue.put(None, block=True) - - # Retrieve results. - for _ in six.moves.range(num_workers): - metric_aggregator.merge(result_queue.get(block=True)) - - for worker in workers: - worker.join() - else: - logging.info('Computing metric in a single process.') - annotation_pairs = _matched_annotations(gt_json, pred_json) - _compute_metric(metric_aggregator, gt_folder, pred_folder, annotation_pairs) - - is_thing = _is_thing_array(gt_json['categories'], ignored_label) - metric_aggregator.print_detailed_results( - is_thing=is_thing, print_digits=print_digits) - return metric_aggregator.detailed_results(is_thing=is_thing) - - -def main(argv): - if len(argv) > 1: - raise app.UsageError('Too many command-line arguments.') - - eval_coco_format(FLAGS.gt_json_file, FLAGS.pred_json_file, FLAGS.gt_folder, - FLAGS.pred_folder, FLAGS.metric, FLAGS.num_categories, - FLAGS.ignored_label, FLAGS.max_instances_per_category, - FLAGS.intersection_offset, FLAGS.normalize_by_image_size, - FLAGS.num_workers, FLAGS.print_digits) - - -if __name__ == '__main__': - flags.mark_flags_as_required( - ['gt_json_file', 'gt_folder', 'pred_json_file', 'pred_folder']) - app.run(main) diff --git a/research/deeplab/evaluation/eval_coco_format_test.py b/research/deeplab/evaluation/eval_coco_format_test.py deleted file mode 100644 index d9093ff127e..00000000000 --- a/research/deeplab/evaluation/eval_coco_format_test.py +++ /dev/null @@ -1,140 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for eval_coco_format script.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -from absl.testing import absltest -import evaluation as panopticapi_eval - -from deeplab.evaluation import eval_coco_format - -_TEST_DIR = 'deeplab/evaluation/testdata' - -FLAGS = flags.FLAGS - - -class EvalCocoFormatTest(absltest.TestCase): - - def test_compare_pq_with_reference_eval(self): - sample_data_dir = os.path.join(_TEST_DIR) - gt_json_file = os.path.join(sample_data_dir, 'coco_gt.json') - gt_folder = os.path.join(sample_data_dir, 'coco_gt') - pred_json_file = os.path.join(sample_data_dir, 'coco_pred.json') - pred_folder = os.path.join(sample_data_dir, 'coco_pred') - - panopticapi_results = panopticapi_eval.pq_compute( - gt_json_file, pred_json_file, gt_folder, pred_folder) - deeplab_results = eval_coco_format.eval_coco_format( - gt_json_file, - pred_json_file, - gt_folder, - pred_folder, - metric='pq', - num_categories=7, - ignored_label=0, - max_instances_per_category=256, - intersection_offset=(256 * 256)) - self.assertCountEqual( - list(deeplab_results.keys()), ['All', 'Things', 'Stuff']) - for cat_group in ['All', 'Things', 'Stuff']: - self.assertCountEqual(deeplab_results[cat_group], ['pq', 'sq', 'rq', 'n']) - for metric in ['pq', 'sq', 'rq', 'n']: - self.assertAlmostEqual(deeplab_results[cat_group][metric], - panopticapi_results[cat_group][metric]) - - def test_compare_pc_with_golden_value(self): - sample_data_dir = os.path.join(_TEST_DIR) - gt_json_file = os.path.join(sample_data_dir, 'coco_gt.json') - gt_folder = os.path.join(sample_data_dir, 'coco_gt') - pred_json_file = os.path.join(sample_data_dir, 'coco_pred.json') - pred_folder = os.path.join(sample_data_dir, 'coco_pred') - - deeplab_results = eval_coco_format.eval_coco_format( - gt_json_file, - pred_json_file, - gt_folder, - pred_folder, - metric='pc', - num_categories=7, - ignored_label=0, - max_instances_per_category=256, - intersection_offset=(256 * 256), - normalize_by_image_size=False) - self.assertCountEqual( - list(deeplab_results.keys()), ['All', 'Things', 'Stuff']) - for cat_group in ['All', 'Things', 'Stuff']: - self.assertCountEqual(deeplab_results[cat_group], ['pc', 'n']) - self.assertAlmostEqual(deeplab_results['All']['pc'], 0.68210561) - self.assertEqual(deeplab_results['All']['n'], 6) - self.assertAlmostEqual(deeplab_results['Things']['pc'], 0.5890529) - self.assertEqual(deeplab_results['Things']['n'], 4) - self.assertAlmostEqual(deeplab_results['Stuff']['pc'], 0.86821097) - self.assertEqual(deeplab_results['Stuff']['n'], 2) - - def test_compare_pc_with_golden_value_normalize_by_size(self): - sample_data_dir = os.path.join(_TEST_DIR) - gt_json_file = os.path.join(sample_data_dir, 'coco_gt.json') - gt_folder = os.path.join(sample_data_dir, 'coco_gt') - pred_json_file = os.path.join(sample_data_dir, 'coco_pred.json') - pred_folder = os.path.join(sample_data_dir, 'coco_pred') - - deeplab_results = eval_coco_format.eval_coco_format( - gt_json_file, - pred_json_file, - gt_folder, - pred_folder, - metric='pc', - num_categories=7, - ignored_label=0, - max_instances_per_category=256, - intersection_offset=(256 * 256), - normalize_by_image_size=True) - self.assertCountEqual( - list(deeplab_results.keys()), ['All', 'Things', 'Stuff']) - self.assertAlmostEqual(deeplab_results['All']['pc'], 0.68214908840) - - def test_pc_with_multiple_workers(self): - sample_data_dir = os.path.join(_TEST_DIR) - gt_json_file = os.path.join(sample_data_dir, 'coco_gt.json') - gt_folder = os.path.join(sample_data_dir, 'coco_gt') - pred_json_file = os.path.join(sample_data_dir, 'coco_pred.json') - pred_folder = os.path.join(sample_data_dir, 'coco_pred') - - deeplab_results = eval_coco_format.eval_coco_format( - gt_json_file, - pred_json_file, - gt_folder, - pred_folder, - metric='pc', - num_categories=7, - ignored_label=0, - max_instances_per_category=256, - intersection_offset=(256 * 256), - num_workers=3, - normalize_by_image_size=False) - self.assertCountEqual( - list(deeplab_results.keys()), ['All', 'Things', 'Stuff']) - self.assertAlmostEqual(deeplab_results['All']['pc'], 0.68210561668) - - -if __name__ == '__main__': - absltest.main() diff --git a/research/deeplab/evaluation/g3doc/img/equation_pc.png b/research/deeplab/evaluation/g3doc/img/equation_pc.png deleted file mode 100644 index 90f15e7a461..00000000000 Binary files a/research/deeplab/evaluation/g3doc/img/equation_pc.png and /dev/null differ diff --git a/research/deeplab/evaluation/g3doc/img/equation_pq.png b/research/deeplab/evaluation/g3doc/img/equation_pq.png deleted file mode 100644 index 13a4393c181..00000000000 Binary files a/research/deeplab/evaluation/g3doc/img/equation_pq.png and /dev/null differ diff --git a/research/deeplab/evaluation/panoptic_quality.py b/research/deeplab/evaluation/panoptic_quality.py deleted file mode 100644 index f7d0f3f98f0..00000000000 --- a/research/deeplab/evaluation/panoptic_quality.py +++ /dev/null @@ -1,259 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Implementation of the Panoptic Quality metric. - -Panoptic Quality is an instance-based metric for evaluating the task of -image parsing, aka panoptic segmentation. - -Please see the paper for details: -"Panoptic Segmentation", Alexander Kirillov, Kaiming He, Ross Girshick, -Carsten Rother and Piotr Dollar. arXiv:1801.00868, 2018. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import numpy as np -import prettytable -import six - -from deeplab.evaluation import base_metric - - -def _ids_to_counts(id_array): - """Given a numpy array, a mapping from each unique entry to its count.""" - ids, counts = np.unique(id_array, return_counts=True) - return dict(six.moves.zip(ids, counts)) - - -class PanopticQuality(base_metric.SegmentationMetric): - """Metric class for Panoptic Quality. - - "Panoptic Segmentation" by Alexander Kirillov, Kaiming He, Ross Girshick, - Carsten Rother, Piotr Dollar. - https://arxiv.org/abs/1801.00868 - """ - - def compare_and_accumulate( - self, groundtruth_category_array, groundtruth_instance_array, - predicted_category_array, predicted_instance_array): - """See base class.""" - # First, combine the category and instance labels so that every unique - # value for (category, instance) is assigned a unique integer label. - pred_segment_id = self._naively_combine_labels(predicted_category_array, - predicted_instance_array) - gt_segment_id = self._naively_combine_labels(groundtruth_category_array, - groundtruth_instance_array) - - # Pre-calculate areas for all groundtruth and predicted segments. - gt_segment_areas = _ids_to_counts(gt_segment_id) - pred_segment_areas = _ids_to_counts(pred_segment_id) - - # We assume there is only one void segment and it has instance id = 0. - void_segment_id = self.ignored_label * self.max_instances_per_category - - # There may be other ignored groundtruth segments with instance id > 0, find - # those ids using the unique segment ids extracted with the area computation - # above. - ignored_segment_ids = { - gt_segment_id for gt_segment_id in six.iterkeys(gt_segment_areas) - if (gt_segment_id // - self.max_instances_per_category) == self.ignored_label - } - - # Next, combine the groundtruth and predicted labels. Dividing up the pixels - # based on which groundtruth segment and which predicted segment they belong - # to, this will assign a different 32-bit integer label to each choice - # of (groundtruth segment, predicted segment), encoded as - # gt_segment_id * offset + pred_segment_id. - intersection_id_array = ( - gt_segment_id.astype(np.uint32) * self.offset + - pred_segment_id.astype(np.uint32)) - - # For every combination of (groundtruth segment, predicted segment) with a - # non-empty intersection, this counts the number of pixels in that - # intersection. - intersection_areas = _ids_to_counts(intersection_id_array) - - # Helper function that computes the area of the overlap between a predicted - # segment and the ground-truth void/ignored segment. - def prediction_void_overlap(pred_segment_id): - void_intersection_id = void_segment_id * self.offset + pred_segment_id - return intersection_areas.get(void_intersection_id, 0) - - # Compute overall ignored overlap. - def prediction_ignored_overlap(pred_segment_id): - total_ignored_overlap = 0 - for ignored_segment_id in ignored_segment_ids: - intersection_id = ignored_segment_id * self.offset + pred_segment_id - total_ignored_overlap += intersection_areas.get(intersection_id, 0) - return total_ignored_overlap - - # Sets that are populated with which segments groundtruth/predicted segments - # have been matched with overlapping predicted/groundtruth segments - # respectively. - gt_matched = set() - pred_matched = set() - - # Calculate IoU per pair of intersecting segments of the same category. - for intersection_id, intersection_area in six.iteritems(intersection_areas): - gt_segment_id = intersection_id // self.offset - pred_segment_id = intersection_id % self.offset - - gt_category = gt_segment_id // self.max_instances_per_category - pred_category = pred_segment_id // self.max_instances_per_category - if gt_category != pred_category: - continue - - # Union between the groundtruth and predicted segments being compared does - # not include the portion of the predicted segment that consists of - # groundtruth "void" pixels. - union = ( - gt_segment_areas[gt_segment_id] + - pred_segment_areas[pred_segment_id] - intersection_area - - prediction_void_overlap(pred_segment_id)) - iou = intersection_area / union - if iou > 0.5: - self.tp_per_class[gt_category] += 1 - self.iou_per_class[gt_category] += iou - gt_matched.add(gt_segment_id) - pred_matched.add(pred_segment_id) - - # Count false negatives for each category. - for gt_segment_id in six.iterkeys(gt_segment_areas): - if gt_segment_id in gt_matched: - continue - category = gt_segment_id // self.max_instances_per_category - # Failing to detect a void segment is not a false negative. - if category == self.ignored_label: - continue - self.fn_per_class[category] += 1 - - # Count false positives for each category. - for pred_segment_id in six.iterkeys(pred_segment_areas): - if pred_segment_id in pred_matched: - continue - # A false positive is not penalized if is mostly ignored in the - # groundtruth. - if (prediction_ignored_overlap(pred_segment_id) / - pred_segment_areas[pred_segment_id]) > 0.5: - continue - category = pred_segment_id // self.max_instances_per_category - self.fp_per_class[category] += 1 - - return self.result() - - def _valid_categories(self): - """Categories with a "valid" value for the metric, have > 0 instances. - - We will ignore the `ignore_label` class and other classes which have - `tp + fn + fp = 0`. - - Returns: - Boolean array of shape `[num_categories]`. - """ - valid_categories = np.not_equal( - self.tp_per_class + self.fn_per_class + self.fp_per_class, 0) - if self.ignored_label >= 0 and self.ignored_label < self.num_categories: - valid_categories[self.ignored_label] = False - return valid_categories - - def detailed_results(self, is_thing=None): - """See base class.""" - valid_categories = self._valid_categories() - - # If known, break down which categories are valid _and_ things/stuff. - category_sets = collections.OrderedDict() - category_sets['All'] = valid_categories - if is_thing is not None: - category_sets['Things'] = np.logical_and(valid_categories, is_thing) - category_sets['Stuff'] = np.logical_and(valid_categories, - np.logical_not(is_thing)) - - # Compute individual per-class metrics that constitute factors of PQ. - sq = base_metric.realdiv_maybe_zero(self.iou_per_class, self.tp_per_class) - rq = base_metric.realdiv_maybe_zero( - self.tp_per_class, - self.tp_per_class + 0.5 * self.fn_per_class + 0.5 * self.fp_per_class) - pq = np.multiply(sq, rq) - - # Assemble detailed results dictionary. - results = {} - for category_set_name, in_category_set in six.iteritems(category_sets): - if np.any(in_category_set): - results[category_set_name] = { - 'pq': np.mean(pq[in_category_set]), - 'sq': np.mean(sq[in_category_set]), - 'rq': np.mean(rq[in_category_set]), - # The number of categories in this subset. - 'n': np.sum(in_category_set.astype(np.int32)), - } - else: - results[category_set_name] = {'pq': 0, 'sq': 0, 'rq': 0, 'n': 0} - - return results - - def result_per_category(self): - """See base class.""" - sq = base_metric.realdiv_maybe_zero(self.iou_per_class, self.tp_per_class) - rq = base_metric.realdiv_maybe_zero( - self.tp_per_class, - self.tp_per_class + 0.5 * self.fn_per_class + 0.5 * self.fp_per_class) - return np.multiply(sq, rq) - - def print_detailed_results(self, is_thing=None, print_digits=3): - """See base class.""" - results = self.detailed_results(is_thing=is_thing) - - tab = prettytable.PrettyTable() - - tab.add_column('', [], align='l') - for fieldname in ['PQ', 'SQ', 'RQ', 'N']: - tab.add_column(fieldname, [], align='r') - - for category_set, subset_results in six.iteritems(results): - data_cols = [ - round(subset_results[col_key], print_digits) * 100 - for col_key in ['pq', 'sq', 'rq'] - ] - data_cols += [subset_results['n']] - tab.add_row([category_set] + data_cols) - - print(tab) - - def result(self): - """See base class.""" - pq_per_class = self.result_per_category() - valid_categories = self._valid_categories() - if not np.any(valid_categories): - return 0. - return np.mean(pq_per_class[valid_categories]) - - def merge(self, other_instance): - """See base class.""" - self.iou_per_class += other_instance.iou_per_class - self.tp_per_class += other_instance.tp_per_class - self.fn_per_class += other_instance.fn_per_class - self.fp_per_class += other_instance.fp_per_class - - def reset(self): - """See base class.""" - self.iou_per_class = np.zeros(self.num_categories, dtype=np.float64) - self.tp_per_class = np.zeros(self.num_categories, dtype=np.float64) - self.fn_per_class = np.zeros(self.num_categories, dtype=np.float64) - self.fp_per_class = np.zeros(self.num_categories, dtype=np.float64) diff --git a/research/deeplab/evaluation/panoptic_quality_test.py b/research/deeplab/evaluation/panoptic_quality_test.py deleted file mode 100644 index 00c88c293b8..00000000000 --- a/research/deeplab/evaluation/panoptic_quality_test.py +++ /dev/null @@ -1,336 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for Panoptic Quality metric.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - -from absl.testing import absltest -import numpy as np -import six - -from deeplab.evaluation import panoptic_quality -from deeplab.evaluation import test_utils - -# See the definition of the color names at: -# https://en.wikipedia.org/wiki/Web_colors. -_CLASS_COLOR_MAP = { - (0, 0, 0): 0, - (0, 0, 255): 1, # Person (blue). - (255, 0, 0): 2, # Bear (red). - (0, 255, 0): 3, # Tree (lime). - (255, 0, 255): 4, # Bird (fuchsia). - (0, 255, 255): 5, # Sky (aqua). - (255, 255, 0): 6, # Cat (yellow). -} - - -class PanopticQualityTest(absltest.TestCase): - - def test_perfect_match(self): - categories = np.zeros([6, 6], np.uint16) - instances = np.array([ - [1, 1, 1, 1, 1, 1], - [1, 2, 2, 2, 2, 1], - [1, 2, 2, 2, 2, 1], - [1, 2, 2, 2, 2, 1], - [1, 2, 2, 1, 1, 1], - [1, 2, 1, 1, 1, 1], - ], - dtype=np.uint16) - - pq = panoptic_quality.PanopticQuality( - num_categories=1, - ignored_label=2, - max_instances_per_category=16, - offset=16) - pq.compare_and_accumulate(categories, instances, categories, instances) - np.testing.assert_array_equal(pq.iou_per_class, [2.0]) - np.testing.assert_array_equal(pq.tp_per_class, [2]) - np.testing.assert_array_equal(pq.fn_per_class, [0]) - np.testing.assert_array_equal(pq.fp_per_class, [0]) - np.testing.assert_array_equal(pq.result_per_category(), [1.0]) - self.assertEqual(pq.result(), 1.0) - - def test_totally_wrong(self): - det_categories = np.array([ - [0, 0, 0, 0, 0, 0], - [0, 1, 0, 0, 1, 0], - [0, 1, 1, 1, 1, 0], - [0, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - ], - dtype=np.uint16) - gt_categories = 1 - det_categories - instances = np.zeros([6, 6], np.uint16) - - pq = panoptic_quality.PanopticQuality( - num_categories=2, - ignored_label=2, - max_instances_per_category=1, - offset=16) - pq.compare_and_accumulate(gt_categories, instances, det_categories, - instances) - np.testing.assert_array_equal(pq.iou_per_class, [0.0, 0.0]) - np.testing.assert_array_equal(pq.tp_per_class, [0, 0]) - np.testing.assert_array_equal(pq.fn_per_class, [1, 1]) - np.testing.assert_array_equal(pq.fp_per_class, [1, 1]) - np.testing.assert_array_equal(pq.result_per_category(), [0.0, 0.0]) - self.assertEqual(pq.result(), 0.0) - - def test_matches_by_iou(self): - good_det_labels = np.array( - [ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 2, 2, 2, 2, 1], - [1, 2, 2, 2, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - gt_labels = np.array( - [ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 2, 2, 2, 1], - [1, 2, 2, 2, 2, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - - pq = panoptic_quality.PanopticQuality( - num_categories=1, - ignored_label=2, - max_instances_per_category=16, - offset=16) - pq.compare_and_accumulate( - np.zeros_like(gt_labels), gt_labels, np.zeros_like(good_det_labels), - good_det_labels) - - # iou(1, 1) = 28/30 - # iou(2, 2) = 6/8 - np.testing.assert_array_almost_equal(pq.iou_per_class, [28 / 30 + 6 / 8]) - np.testing.assert_array_equal(pq.tp_per_class, [2]) - np.testing.assert_array_equal(pq.fn_per_class, [0]) - np.testing.assert_array_equal(pq.fp_per_class, [0]) - self.assertAlmostEqual(pq.result(), (28 / 30 + 6 / 8) / 2) - - bad_det_labels = np.array( - [ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 2, 2, 1], - [1, 1, 1, 2, 2, 1], - [1, 1, 1, 2, 2, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - - pq.reset() - pq.compare_and_accumulate( - np.zeros_like(gt_labels), gt_labels, np.zeros_like(bad_det_labels), - bad_det_labels) - - # iou(1, 1) = 27/32 - np.testing.assert_array_almost_equal(pq.iou_per_class, [27 / 32]) - np.testing.assert_array_equal(pq.tp_per_class, [1]) - np.testing.assert_array_equal(pq.fn_per_class, [1]) - np.testing.assert_array_equal(pq.fp_per_class, [1]) - self.assertAlmostEqual(pq.result(), (27 / 32) * (1 / 2)) - - def test_wrong_instances(self): - categories = np.array([ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 2, 2, 1, 2, 2], - [1, 2, 2, 1, 2, 2], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - predicted_instances = np.array([ - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 1], - [0, 0, 0, 0, 1, 1], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - ], - dtype=np.uint16) - groundtruth_instances = np.zeros([6, 6], dtype=np.uint16) - - pq = panoptic_quality.PanopticQuality( - num_categories=3, - ignored_label=0, - max_instances_per_category=10, - offset=100) - pq.compare_and_accumulate(categories, groundtruth_instances, categories, - predicted_instances) - - np.testing.assert_array_equal(pq.iou_per_class, [0.0, 1.0, 0.0]) - np.testing.assert_array_equal(pq.tp_per_class, [0, 1, 0]) - np.testing.assert_array_equal(pq.fn_per_class, [0, 0, 1]) - np.testing.assert_array_equal(pq.fp_per_class, [0, 0, 2]) - np.testing.assert_array_equal(pq.result_per_category(), [0, 1, 0]) - self.assertAlmostEqual(pq.result(), 0.5) - - def test_instance_order_is_arbitrary(self): - categories = np.array([ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 2, 2, 1, 2, 2], - [1, 2, 2, 1, 2, 2], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - predicted_instances = np.array([ - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 1, 1], - [0, 0, 0, 0, 1, 1], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - ], - dtype=np.uint16) - groundtruth_instances = np.array([ - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - [0, 1, 1, 0, 0, 0], - [0, 1, 1, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - ], - dtype=np.uint16) - - pq = panoptic_quality.PanopticQuality( - num_categories=3, - ignored_label=0, - max_instances_per_category=10, - offset=100) - pq.compare_and_accumulate(categories, groundtruth_instances, categories, - predicted_instances) - - np.testing.assert_array_equal(pq.iou_per_class, [0.0, 1.0, 2.0]) - np.testing.assert_array_equal(pq.tp_per_class, [0, 1, 2]) - np.testing.assert_array_equal(pq.fn_per_class, [0, 0, 0]) - np.testing.assert_array_equal(pq.fp_per_class, [0, 0, 0]) - np.testing.assert_array_equal(pq.result_per_category(), [0, 1, 1]) - self.assertAlmostEqual(pq.result(), 1.0) - - def test_matches_expected(self): - pred_classes = test_utils.read_segmentation_with_rgb_color_map( - 'team_pred_class.png', _CLASS_COLOR_MAP) - pred_instances = test_utils.read_test_image( - 'team_pred_instance.png', mode='L') - - instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - gt_instances, gt_classes = test_utils.panoptic_segmentation_with_class_map( - 'team_gt_instance.png', instance_class_map) - - pq = panoptic_quality.PanopticQuality( - num_categories=3, - ignored_label=0, - max_instances_per_category=256, - offset=256 * 256) - pq.compare_and_accumulate(gt_classes, gt_instances, pred_classes, - pred_instances) - np.testing.assert_array_almost_equal( - pq.iou_per_class, [2.06104, 5.26827, 0.54069], decimal=4) - np.testing.assert_array_equal(pq.tp_per_class, [1, 7, 1]) - np.testing.assert_array_equal(pq.fn_per_class, [0, 1, 0]) - np.testing.assert_array_equal(pq.fp_per_class, [0, 0, 0]) - np.testing.assert_array_almost_equal(pq.result_per_category(), - [2.061038, 0.702436, 0.54069]) - self.assertAlmostEqual(pq.result(), 0.62156287) - - def test_merge_accumulates_all_across_instances(self): - categories = np.zeros([6, 6], np.uint16) - good_det_labels = np.array([ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 2, 2, 2, 2, 1], - [1, 2, 2, 2, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - gt_labels = np.array([ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 2, 2, 2, 1], - [1, 2, 2, 2, 2, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - - good_pq = panoptic_quality.PanopticQuality( - num_categories=1, - ignored_label=2, - max_instances_per_category=16, - offset=16) - for _ in six.moves.range(2): - good_pq.compare_and_accumulate(categories, gt_labels, categories, - good_det_labels) - - bad_det_labels = np.array([ - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1], - [1, 1, 1, 2, 2, 1], - [1, 1, 1, 2, 2, 1], - [1, 1, 1, 2, 2, 1], - [1, 1, 1, 1, 1, 1], - ], - dtype=np.uint16) - - bad_pq = panoptic_quality.PanopticQuality( - num_categories=1, - ignored_label=2, - max_instances_per_category=16, - offset=16) - for _ in six.moves.range(2): - bad_pq.compare_and_accumulate(categories, gt_labels, categories, - bad_det_labels) - - good_pq.merge(bad_pq) - - np.testing.assert_array_almost_equal( - good_pq.iou_per_class, [2 * (28 / 30 + 6 / 8) + 2 * (27 / 32)]) - np.testing.assert_array_equal(good_pq.tp_per_class, [2 * 2 + 2]) - np.testing.assert_array_equal(good_pq.fn_per_class, [2]) - np.testing.assert_array_equal(good_pq.fp_per_class, [2]) - self.assertAlmostEqual(good_pq.result(), 0.63177083) - - -if __name__ == '__main__': - absltest.main() diff --git a/research/deeplab/evaluation/parsing_covering.py b/research/deeplab/evaluation/parsing_covering.py deleted file mode 100644 index a40e55fc6be..00000000000 --- a/research/deeplab/evaluation/parsing_covering.py +++ /dev/null @@ -1,246 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Implementation of the Parsing Covering metric. - -Parsing Covering is a region-based metric for evaluating the task of -image parsing, aka panoptic segmentation. - -Please see the paper for details: -"DeeperLab: Single-Shot Image Parser", Tien-Ju Yang, Maxwell D. Collins, -Yukun Zhu, Jyh-Jing Hwang, Ting Liu, Xiao Zhang, Vivienne Sze, -George Papandreou, Liang-Chieh Chen. arXiv: 1902.05093, 2019. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections - -import numpy as np -import prettytable -import six - -from deeplab.evaluation import base_metric - - -class ParsingCovering(base_metric.SegmentationMetric): - r"""Metric class for Parsing Covering. - - Computes segmentation covering metric introduced in (Arbelaez, et al., 2010) - with extension to handle multi-class semantic labels (a.k.a. parsing - covering). Specifically, segmentation covering (SC) is defined in Eq. (8) in - (Arbelaez et al., 2010) as: - - SC(c) = \sum_{R\in S}(|R| * \max_{R'\in S'}O(R,R')) / \sum_{R\in S}|R|, - - where S are the groundtruth instance regions and S' are the predicted - instance regions. The parsing covering is simply: - - PC = \sum_{c=1}^{C}SC(c) / C, - - where C is the number of classes. - """ - - def __init__(self, - num_categories, - ignored_label, - max_instances_per_category, - offset, - normalize_by_image_size=True): - """Initialization for ParsingCovering. - - Args: - num_categories: The number of segmentation categories (or "classes" in the - dataset. - ignored_label: A category id that is ignored in evaluation, e.g. the void - label as defined in COCO panoptic segmentation dataset. - max_instances_per_category: The maximum number of instances for each - category. Used in ensuring unique instance labels. - offset: The maximum number of unique labels. This is used, by multiplying - the ground-truth labels, to generate unique ids for individual regions - of overlap between groundtruth and predicted segments. - normalize_by_image_size: Whether to normalize groundtruth instance region - areas by image size. If True, groundtruth instance areas and weighted - IoUs will be divided by the size of the corresponding image before - accumulated across the dataset. - """ - super(ParsingCovering, self).__init__(num_categories, ignored_label, - max_instances_per_category, offset) - self.normalize_by_image_size = normalize_by_image_size - - def compare_and_accumulate( - self, groundtruth_category_array, groundtruth_instance_array, - predicted_category_array, predicted_instance_array): - """See base class.""" - # Allocate intermediate data structures. - max_ious = np.zeros([self.num_categories, self.max_instances_per_category], - dtype=np.float64) - gt_areas = np.zeros([self.num_categories, self.max_instances_per_category], - dtype=np.float64) - pred_areas = np.zeros( - [self.num_categories, self.max_instances_per_category], - dtype=np.float64) - # This is a dictionary in the format: - # {(category, gt_instance): [(pred_instance, intersection_area)]}. - intersections = collections.defaultdict(list) - - # First, combine the category and instance labels so that every unique - # value for (category, instance) is assigned a unique integer label. - pred_segment_id = self._naively_combine_labels(predicted_category_array, - predicted_instance_array) - gt_segment_id = self._naively_combine_labels(groundtruth_category_array, - groundtruth_instance_array) - - # Next, combine the groundtruth and predicted labels. Dividing up the pixels - # based on which groundtruth segment and which predicted segment they belong - # to, this will assign a different 32-bit integer label to each choice - # of (groundtruth segment, predicted segment), encoded as - # gt_segment_id * offset + pred_segment_id. - intersection_id_array = ( - gt_segment_id.astype(np.uint32) * self.offset + - pred_segment_id.astype(np.uint32)) - - # For every combination of (groundtruth segment, predicted segment) with a - # non-empty intersection, this counts the number of pixels in that - # intersection. - intersection_ids, intersection_areas = np.unique( - intersection_id_array, return_counts=True) - - # Find areas of all groundtruth and predicted instances, as well as of their - # intersections. - for intersection_id, intersection_area in six.moves.zip( - intersection_ids, intersection_areas): - gt_segment_id = intersection_id // self.offset - gt_category = gt_segment_id // self.max_instances_per_category - if gt_category == self.ignored_label: - continue - gt_instance = gt_segment_id % self.max_instances_per_category - gt_areas[gt_category, gt_instance] += intersection_area - - pred_segment_id = intersection_id % self.offset - pred_category = pred_segment_id // self.max_instances_per_category - pred_instance = pred_segment_id % self.max_instances_per_category - pred_areas[pred_category, pred_instance] += intersection_area - if pred_category != gt_category: - continue - - intersections[gt_category, gt_instance].append((pred_instance, - intersection_area)) - - # Find maximum IoU for every groundtruth instance. - for gt_label, instance_intersections in six.iteritems(intersections): - category, gt_instance = gt_label - gt_area = gt_areas[category, gt_instance] - ious = [] - for pred_instance, intersection_area in instance_intersections: - pred_area = pred_areas[category, pred_instance] - union = gt_area + pred_area - intersection_area - ious.append(intersection_area / union) - max_ious[category, gt_instance] = max(ious) - - # Normalize groundtruth instance areas by image size if necessary. - if self.normalize_by_image_size: - gt_areas /= groundtruth_category_array.size - - # Compute per-class weighted IoUs and areas summed over all groundtruth - # instances. - self.weighted_iou_per_class += np.sum(max_ious * gt_areas, axis=-1) - self.gt_area_per_class += np.sum(gt_areas, axis=-1) - - return self.result() - - def result_per_category(self): - """See base class.""" - return base_metric.realdiv_maybe_zero(self.weighted_iou_per_class, - self.gt_area_per_class) - - def _valid_categories(self): - """Categories with a "valid" value for the metric, have > 0 instances. - - We will ignore the `ignore_label` class and other classes which have - groundtruth area of 0. - - Returns: - Boolean array of shape `[num_categories]`. - """ - valid_categories = np.not_equal(self.gt_area_per_class, 0) - if self.ignored_label >= 0 and self.ignored_label < self.num_categories: - valid_categories[self.ignored_label] = False - return valid_categories - - def detailed_results(self, is_thing=None): - """See base class.""" - valid_categories = self._valid_categories() - - # If known, break down which categories are valid _and_ things/stuff. - category_sets = collections.OrderedDict() - category_sets['All'] = valid_categories - if is_thing is not None: - category_sets['Things'] = np.logical_and(valid_categories, is_thing) - category_sets['Stuff'] = np.logical_and(valid_categories, - np.logical_not(is_thing)) - - covering_per_class = self.result_per_category() - results = {} - for category_set_name, in_category_set in six.iteritems(category_sets): - if np.any(in_category_set): - results[category_set_name] = { - 'pc': np.mean(covering_per_class[in_category_set]), - # The number of valid categories in this subset. - 'n': np.sum(in_category_set.astype(np.int32)), - } - else: - results[category_set_name] = {'pc': 0, 'n': 0} - - return results - - def print_detailed_results(self, is_thing=None, print_digits=3): - """See base class.""" - results = self.detailed_results(is_thing=is_thing) - - tab = prettytable.PrettyTable() - - tab.add_column('', [], align='l') - for fieldname in ['PC', 'N']: - tab.add_column(fieldname, [], align='r') - - for category_set, subset_results in six.iteritems(results): - data_cols = [ - round(subset_results['pc'], print_digits) * 100, subset_results['n'] - ] - tab.add_row([category_set] + data_cols) - - print(tab) - - def result(self): - """See base class.""" - covering_per_class = self.result_per_category() - valid_categories = self._valid_categories() - if not np.any(valid_categories): - return 0. - return np.mean(covering_per_class[valid_categories]) - - def merge(self, other_instance): - """See base class.""" - self.weighted_iou_per_class += other_instance.weighted_iou_per_class - self.gt_area_per_class += other_instance.gt_area_per_class - - def reset(self): - """See base class.""" - self.weighted_iou_per_class = np.zeros( - self.num_categories, dtype=np.float64) - self.gt_area_per_class = np.zeros(self.num_categories, dtype=np.float64) diff --git a/research/deeplab/evaluation/parsing_covering_test.py b/research/deeplab/evaluation/parsing_covering_test.py deleted file mode 100644 index 124d1b37255..00000000000 --- a/research/deeplab/evaluation/parsing_covering_test.py +++ /dev/null @@ -1,173 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for Parsing Covering metric.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - - -from absl.testing import absltest -import numpy as np - -from deeplab.evaluation import parsing_covering -from deeplab.evaluation import test_utils - -# See the definition of the color names at: -# https://en.wikipedia.org/wiki/Web_colors. -_CLASS_COLOR_MAP = { - (0, 0, 0): 0, - (0, 0, 255): 1, # Person (blue). - (255, 0, 0): 2, # Bear (red). - (0, 255, 0): 3, # Tree (lime). - (255, 0, 255): 4, # Bird (fuchsia). - (0, 255, 255): 5, # Sky (aqua). - (255, 255, 0): 6, # Cat (yellow). -} - - -class CoveringConveringTest(absltest.TestCase): - - def test_perfect_match(self): - categories = np.zeros([6, 6], np.uint16) - instances = np.array([ - [2, 2, 2, 2, 2, 2], - [2, 4, 4, 4, 4, 2], - [2, 4, 4, 4, 4, 2], - [2, 4, 4, 4, 4, 2], - [2, 4, 4, 2, 2, 2], - [2, 4, 2, 2, 2, 2], - ], - dtype=np.uint16) - - pc = parsing_covering.ParsingCovering( - num_categories=3, - ignored_label=2, - max_instances_per_category=2, - offset=16, - normalize_by_image_size=False) - pc.compare_and_accumulate(categories, instances, categories, instances) - np.testing.assert_array_equal(pc.weighted_iou_per_class, [0.0, 21.0, 0.0]) - np.testing.assert_array_equal(pc.gt_area_per_class, [0.0, 21.0, 0.0]) - np.testing.assert_array_equal(pc.result_per_category(), [0.0, 1.0, 0.0]) - self.assertEqual(pc.result(), 1.0) - - def test_totally_wrong(self): - categories = np.zeros([6, 6], np.uint16) - gt_instances = np.array([ - [0, 0, 0, 0, 0, 0], - [0, 1, 0, 0, 1, 0], - [0, 1, 1, 1, 1, 0], - [0, 1, 1, 1, 1, 0], - [0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0], - ], - dtype=np.uint16) - pred_instances = 1 - gt_instances - - pc = parsing_covering.ParsingCovering( - num_categories=2, - ignored_label=0, - max_instances_per_category=1, - offset=16, - normalize_by_image_size=False) - pc.compare_and_accumulate(categories, gt_instances, categories, - pred_instances) - np.testing.assert_array_equal(pc.weighted_iou_per_class, [0.0, 0.0]) - np.testing.assert_array_equal(pc.gt_area_per_class, [0.0, 10.0]) - np.testing.assert_array_equal(pc.result_per_category(), [0.0, 0.0]) - self.assertEqual(pc.result(), 0.0) - - def test_matches_expected(self): - pred_classes = test_utils.read_segmentation_with_rgb_color_map( - 'team_pred_class.png', _CLASS_COLOR_MAP) - pred_instances = test_utils.read_test_image( - 'team_pred_instance.png', mode='L') - - instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - gt_instances, gt_classes = test_utils.panoptic_segmentation_with_class_map( - 'team_gt_instance.png', instance_class_map) - - pc = parsing_covering.ParsingCovering( - num_categories=3, - ignored_label=0, - max_instances_per_category=256, - offset=256 * 256, - normalize_by_image_size=False) - pc.compare_and_accumulate(gt_classes, gt_instances, pred_classes, - pred_instances) - np.testing.assert_array_almost_equal( - pc.weighted_iou_per_class, [0.0, 39864.14634, 3136], decimal=4) - np.testing.assert_array_equal(pc.gt_area_per_class, [0.0, 56870, 5800]) - np.testing.assert_array_almost_equal( - pc.result_per_category(), [0.0, 0.70097, 0.54069], decimal=4) - self.assertAlmostEqual(pc.result(), 0.6208296732) - - def test_matches_expected_normalize_by_size(self): - pred_classes = test_utils.read_segmentation_with_rgb_color_map( - 'team_pred_class.png', _CLASS_COLOR_MAP) - pred_instances = test_utils.read_test_image( - 'team_pred_instance.png', mode='L') - - instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - gt_instances, gt_classes = test_utils.panoptic_segmentation_with_class_map( - 'team_gt_instance.png', instance_class_map) - - pc = parsing_covering.ParsingCovering( - num_categories=3, - ignored_label=0, - max_instances_per_category=256, - offset=256 * 256, - normalize_by_image_size=True) - pc.compare_and_accumulate(gt_classes, gt_instances, pred_classes, - pred_instances) - np.testing.assert_array_almost_equal( - pc.weighted_iou_per_class, [0.0, 0.5002088756, 0.03935002196], - decimal=4) - np.testing.assert_array_almost_equal( - pc.gt_area_per_class, [0.0, 0.7135955832, 0.07277746408], decimal=4) - # Note that the per-category and overall PCs are identical to those without - # normalization in the previous test, because we only have a single image. - np.testing.assert_array_almost_equal( - pc.result_per_category(), [0.0, 0.70097, 0.54069], decimal=4) - self.assertAlmostEqual(pc.result(), 0.6208296732) - - -if __name__ == '__main__': - absltest.main() diff --git a/research/deeplab/evaluation/streaming_metrics.py b/research/deeplab/evaluation/streaming_metrics.py deleted file mode 100644 index 8313792676a..00000000000 --- a/research/deeplab/evaluation/streaming_metrics.py +++ /dev/null @@ -1,240 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Code to compute segmentation in a "streaming" pattern in Tensorflow. - -These aggregate the metric over examples of the evaluation set. Each example is -assumed to be fed in in a stream, and the metric implementation accumulates -across them. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from deeplab.evaluation import panoptic_quality -from deeplab.evaluation import parsing_covering - -_EPSILON = 1e-10 - - -def _realdiv_maybe_zero(x, y): - """Support tf.realdiv(x, y) where y may contain zeros.""" - return tf.where(tf.less(y, _EPSILON), tf.zeros_like(x), tf.realdiv(x, y)) - - -def _running_total(value, shape, name=None): - """Maintains a running total of tensor `value` between calls.""" - with tf.variable_scope(name, 'running_total', [value]): - total_var = tf.get_variable( - 'total', - shape, - value.dtype, - initializer=tf.zeros_initializer(), - trainable=False, - collections=[ - tf.GraphKeys.LOCAL_VARIABLES, tf.GraphKeys.METRIC_VARIABLES - ]) - updated_total = tf.assign_add(total_var, value, use_locking=True) - - return total_var, updated_total - - -def _panoptic_quality_helper( - groundtruth_category_array, groundtruth_instance_array, - predicted_category_array, predicted_instance_array, num_classes, - max_instances_per_category, ignored_label, offset): - """Helper function to compute panoptic quality.""" - pq = panoptic_quality.PanopticQuality(num_classes, ignored_label, - max_instances_per_category, offset) - pq.compare_and_accumulate(groundtruth_category_array, - groundtruth_instance_array, - predicted_category_array, predicted_instance_array) - return pq.iou_per_class, pq.tp_per_class, pq.fn_per_class, pq.fp_per_class - - -def streaming_panoptic_quality(groundtruth_categories, - groundtruth_instances, - predicted_categories, - predicted_instances, - num_classes, - max_instances_per_category, - ignored_label, - offset, - name=None): - """Aggregates the panoptic metric across calls with different input tensors. - - See tf.metrics.* functions for comparable functionality and usage. - - Args: - groundtruth_categories: A 2D uint16 tensor of groundtruth category labels. - groundtruth_instances: A 2D uint16 tensor of groundtruth instance labels. - predicted_categories: A 2D uint16 tensor of predicted category labels. - predicted_instances: A 2D uint16 tensor of predicted instance labels. - num_classes: Number of classes in the dataset as an integer. - max_instances_per_category: The maximum number of instances for each class - as an integer or integer tensor. - ignored_label: The class id to be ignored in evaluation as an integer or - integer tensor. - offset: The maximum number of unique labels as an integer or integer tensor. - name: An optional variable_scope name. - - Returns: - qualities: A tensor of shape `[6, num_classes]`, where (1) panoptic quality, - (2) segmentation quality, (3) recognition quality, (4) total_tp, - (5) total_fn and (6) total_fp are saved in the respective rows. - update_ops: List of operations that update the running overall panoptic - quality. - - Raises: - RuntimeError: If eager execution is enabled. - """ - if tf.executing_eagerly(): - raise RuntimeError('Cannot aggregate when eager execution is enabled.') - - input_args = [ - tf.convert_to_tensor(groundtruth_categories, tf.uint16), - tf.convert_to_tensor(groundtruth_instances, tf.uint16), - tf.convert_to_tensor(predicted_categories, tf.uint16), - tf.convert_to_tensor(predicted_instances, tf.uint16), - tf.convert_to_tensor(num_classes, tf.int32), - tf.convert_to_tensor(max_instances_per_category, tf.int32), - tf.convert_to_tensor(ignored_label, tf.int32), - tf.convert_to_tensor(offset, tf.int32), - ] - return_types = [ - tf.float64, - tf.float64, - tf.float64, - tf.float64, - ] - with tf.variable_scope(name, 'streaming_panoptic_quality', input_args): - panoptic_results = tf.py_func( - _panoptic_quality_helper, input_args, return_types, stateful=False) - iou, tp, fn, fp = tuple(panoptic_results) - - total_iou, updated_iou = _running_total( - iou, [num_classes], name='iou_total') - total_tp, updated_tp = _running_total(tp, [num_classes], name='tp_total') - total_fn, updated_fn = _running_total(fn, [num_classes], name='fn_total') - total_fp, updated_fp = _running_total(fp, [num_classes], name='fp_total') - update_ops = [updated_iou, updated_tp, updated_fn, updated_fp] - - sq = _realdiv_maybe_zero(total_iou, total_tp) - rq = _realdiv_maybe_zero(total_tp, - total_tp + 0.5 * total_fn + 0.5 * total_fp) - pq = tf.multiply(sq, rq) - qualities = tf.stack([pq, sq, rq, total_tp, total_fn, total_fp], axis=0) - return qualities, update_ops - - -def _parsing_covering_helper( - groundtruth_category_array, groundtruth_instance_array, - predicted_category_array, predicted_instance_array, num_classes, - max_instances_per_category, ignored_label, offset, normalize_by_image_size): - """Helper function to compute parsing covering.""" - pc = parsing_covering.ParsingCovering(num_classes, ignored_label, - max_instances_per_category, offset, - normalize_by_image_size) - pc.compare_and_accumulate(groundtruth_category_array, - groundtruth_instance_array, - predicted_category_array, predicted_instance_array) - return pc.weighted_iou_per_class, pc.gt_area_per_class - - -def streaming_parsing_covering(groundtruth_categories, - groundtruth_instances, - predicted_categories, - predicted_instances, - num_classes, - max_instances_per_category, - ignored_label, - offset, - normalize_by_image_size=True, - name=None): - """Aggregates the covering across calls with different input tensors. - - See tf.metrics.* functions for comparable functionality and usage. - - Args: - groundtruth_categories: A 2D uint16 tensor of groundtruth category labels. - groundtruth_instances: A 2D uint16 tensor of groundtruth instance labels. - predicted_categories: A 2D uint16 tensor of predicted category labels. - predicted_instances: A 2D uint16 tensor of predicted instance labels. - num_classes: Number of classes in the dataset as an integer. - max_instances_per_category: The maximum number of instances for each class - as an integer or integer tensor. - ignored_label: The class id to be ignored in evaluation as an integer or - integer tensor. - offset: The maximum number of unique labels as an integer or integer tensor. - normalize_by_image_size: Whether to normalize groundtruth region areas by - image size. If True, groundtruth instance areas and weighted IoUs will be - divided by the size of the corresponding image before accumulated across - the dataset. - name: An optional variable_scope name. - - Returns: - coverings: A tensor of shape `[3, num_classes]`, where (1) per class - coverings, (2) per class sum of weighted IoUs, and (3) per class sum of - groundtruth region areas are saved in the perspective rows. - update_ops: List of operations that update the running overall parsing - covering. - - Raises: - RuntimeError: If eager execution is enabled. - """ - if tf.executing_eagerly(): - raise RuntimeError('Cannot aggregate when eager execution is enabled.') - - input_args = [ - tf.convert_to_tensor(groundtruth_categories, tf.uint16), - tf.convert_to_tensor(groundtruth_instances, tf.uint16), - tf.convert_to_tensor(predicted_categories, tf.uint16), - tf.convert_to_tensor(predicted_instances, tf.uint16), - tf.convert_to_tensor(num_classes, tf.int32), - tf.convert_to_tensor(max_instances_per_category, tf.int32), - tf.convert_to_tensor(ignored_label, tf.int32), - tf.convert_to_tensor(offset, tf.int32), - tf.convert_to_tensor(normalize_by_image_size, tf.bool), - ] - return_types = [ - tf.float64, - tf.float64, - ] - with tf.variable_scope(name, 'streaming_parsing_covering', input_args): - covering_results = tf.py_func( - _parsing_covering_helper, input_args, return_types, stateful=False) - weighted_iou_per_class, gt_area_per_class = tuple(covering_results) - - total_weighted_iou_per_class, updated_weighted_iou_per_class = ( - _running_total( - weighted_iou_per_class, [num_classes], - name='weighted_iou_per_class_total')) - total_gt_area_per_class, updated_gt_area_per_class = _running_total( - gt_area_per_class, [num_classes], name='gt_area_per_class_total') - - covering_per_class = _realdiv_maybe_zero(total_weighted_iou_per_class, - total_gt_area_per_class) - coverings = tf.stack([ - covering_per_class, - total_weighted_iou_per_class, - total_gt_area_per_class, - ], - axis=0) - update_ops = [updated_weighted_iou_per_class, updated_gt_area_per_class] - - return coverings, update_ops diff --git a/research/deeplab/evaluation/streaming_metrics_test.py b/research/deeplab/evaluation/streaming_metrics_test.py deleted file mode 100644 index 656007e6238..00000000000 --- a/research/deeplab/evaluation/streaming_metrics_test.py +++ /dev/null @@ -1,549 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for segmentation "streaming" metrics.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections - - - -import numpy as np -import six -import tensorflow as tf - -from deeplab.evaluation import streaming_metrics -from deeplab.evaluation import test_utils - -# See the definition of the color names at: -# https://en.wikipedia.org/wiki/Web_colors. -_CLASS_COLOR_MAP = { - (0, 0, 0): 0, - (0, 0, 255): 1, # Person (blue). - (255, 0, 0): 2, # Bear (red). - (0, 255, 0): 3, # Tree (lime). - (255, 0, 255): 4, # Bird (fuchsia). - (0, 255, 255): 5, # Sky (aqua). - (255, 255, 0): 6, # Cat (yellow). -} - - -class StreamingPanopticQualityTest(tf.test.TestCase): - - def test_streaming_metric_on_single_image(self): - offset = 256 * 256 - - instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - gt_instances, gt_classes = test_utils.panoptic_segmentation_with_class_map( - 'team_gt_instance.png', instance_class_map) - - pred_classes = test_utils.read_segmentation_with_rgb_color_map( - 'team_pred_class.png', _CLASS_COLOR_MAP) - pred_instances = test_utils.read_test_image( - 'team_pred_instance.png', mode='L') - - gt_class_tensor = tf.placeholder(tf.uint16) - gt_instance_tensor = tf.placeholder(tf.uint16) - pred_class_tensor = tf.placeholder(tf.uint16) - pred_instance_tensor = tf.placeholder(tf.uint16) - qualities, update_pq = streaming_metrics.streaming_panoptic_quality( - gt_class_tensor, - gt_instance_tensor, - pred_class_tensor, - pred_instance_tensor, - num_classes=3, - max_instances_per_category=256, - ignored_label=0, - offset=offset) - pq, sq, rq, total_tp, total_fn, total_fp = tf.unstack(qualities, 6, axis=0) - feed_dict = { - gt_class_tensor: gt_classes, - gt_instance_tensor: gt_instances, - pred_class_tensor: pred_classes, - pred_instance_tensor: pred_instances - } - - with self.session() as sess: - sess.run(tf.local_variables_initializer()) - sess.run(update_pq, feed_dict=feed_dict) - (result_pq, result_sq, result_rq, result_total_tp, result_total_fn, - result_total_fp) = sess.run([pq, sq, rq, total_tp, total_fn, total_fp], - feed_dict=feed_dict) - np.testing.assert_array_almost_equal( - result_pq, [2.06104, 0.7024, 0.54069], decimal=4) - np.testing.assert_array_almost_equal( - result_sq, [2.06104, 0.7526, 0.54069], decimal=4) - np.testing.assert_array_almost_equal(result_rq, [1., 0.9333, 1.], decimal=4) - np.testing.assert_array_almost_equal( - result_total_tp, [1., 7., 1.], decimal=4) - np.testing.assert_array_almost_equal( - result_total_fn, [0., 1., 0.], decimal=4) - np.testing.assert_array_almost_equal( - result_total_fp, [0., 0., 0.], decimal=4) - - def test_streaming_metric_on_multiple_images(self): - num_classes = 7 - offset = 256 * 256 - - bird_gt_instance_class_map = { - 92: 5, - 176: 3, - 255: 4, - } - cat_gt_instance_class_map = { - 0: 0, - 255: 6, - } - team_gt_instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - test_image = collections.namedtuple( - 'TestImage', - ['gt_class_map', 'gt_path', 'pred_inst_path', 'pred_class_path']) - test_images = [ - test_image(bird_gt_instance_class_map, 'bird_gt.png', - 'bird_pred_instance.png', 'bird_pred_class.png'), - test_image(cat_gt_instance_class_map, 'cat_gt.png', - 'cat_pred_instance.png', 'cat_pred_class.png'), - test_image(team_gt_instance_class_map, 'team_gt_instance.png', - 'team_pred_instance.png', 'team_pred_class.png'), - ] - - gt_classes = [] - gt_instances = [] - pred_classes = [] - pred_instances = [] - for test_image in test_images: - (image_gt_instances, - image_gt_classes) = test_utils.panoptic_segmentation_with_class_map( - test_image.gt_path, test_image.gt_class_map) - gt_classes.append(image_gt_classes) - gt_instances.append(image_gt_instances) - - pred_classes.append( - test_utils.read_segmentation_with_rgb_color_map( - test_image.pred_class_path, _CLASS_COLOR_MAP)) - pred_instances.append( - test_utils.read_test_image(test_image.pred_inst_path, mode='L')) - - gt_class_tensor = tf.placeholder(tf.uint16) - gt_instance_tensor = tf.placeholder(tf.uint16) - pred_class_tensor = tf.placeholder(tf.uint16) - pred_instance_tensor = tf.placeholder(tf.uint16) - qualities, update_pq = streaming_metrics.streaming_panoptic_quality( - gt_class_tensor, - gt_instance_tensor, - pred_class_tensor, - pred_instance_tensor, - num_classes=num_classes, - max_instances_per_category=256, - ignored_label=0, - offset=offset) - pq, sq, rq, total_tp, total_fn, total_fp = tf.unstack(qualities, 6, axis=0) - with self.session() as sess: - sess.run(tf.local_variables_initializer()) - for pred_class, pred_instance, gt_class, gt_instance in six.moves.zip( - pred_classes, pred_instances, gt_classes, gt_instances): - sess.run( - update_pq, - feed_dict={ - gt_class_tensor: gt_class, - gt_instance_tensor: gt_instance, - pred_class_tensor: pred_class, - pred_instance_tensor: pred_instance - }) - (result_pq, result_sq, result_rq, result_total_tp, result_total_fn, - result_total_fp) = sess.run( - [pq, sq, rq, total_tp, total_fn, total_fp], - feed_dict={ - gt_class_tensor: 0, - gt_instance_tensor: 0, - pred_class_tensor: 0, - pred_instance_tensor: 0 - }) - np.testing.assert_array_almost_equal( - result_pq, - [4.3107, 0.7024, 0.54069, 0.745353, 0.85768, 0.99107, 0.77410], - decimal=4) - np.testing.assert_array_almost_equal( - result_sq, [5.3883, 0.7526, 0.5407, 0.7454, 0.8577, 0.9911, 0.7741], - decimal=4) - np.testing.assert_array_almost_equal( - result_rq, [0.8, 0.9333, 1., 1., 1., 1., 1.], decimal=4) - np.testing.assert_array_almost_equal( - result_total_tp, [2., 7., 1., 1., 1., 1., 1.], decimal=4) - np.testing.assert_array_almost_equal( - result_total_fn, [0., 1., 0., 0., 0., 0., 0.], decimal=4) - np.testing.assert_array_almost_equal( - result_total_fp, [1., 0., 0., 0., 0., 0., 0.], decimal=4) - - -class StreamingParsingCoveringTest(tf.test.TestCase): - - def test_streaming_metric_on_single_image(self): - offset = 256 * 256 - - instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - gt_instances, gt_classes = test_utils.panoptic_segmentation_with_class_map( - 'team_gt_instance.png', instance_class_map) - - pred_classes = test_utils.read_segmentation_with_rgb_color_map( - 'team_pred_class.png', _CLASS_COLOR_MAP) - pred_instances = test_utils.read_test_image( - 'team_pred_instance.png', mode='L') - - gt_class_tensor = tf.placeholder(tf.uint16) - gt_instance_tensor = tf.placeholder(tf.uint16) - pred_class_tensor = tf.placeholder(tf.uint16) - pred_instance_tensor = tf.placeholder(tf.uint16) - coverings, update_ops = streaming_metrics.streaming_parsing_covering( - gt_class_tensor, - gt_instance_tensor, - pred_class_tensor, - pred_instance_tensor, - num_classes=3, - max_instances_per_category=256, - ignored_label=0, - offset=offset, - normalize_by_image_size=False) - (per_class_coverings, per_class_weighted_ious, per_class_gt_areas) = ( - tf.unstack(coverings, num=3, axis=0)) - feed_dict = { - gt_class_tensor: gt_classes, - gt_instance_tensor: gt_instances, - pred_class_tensor: pred_classes, - pred_instance_tensor: pred_instances - } - - with self.session() as sess: - sess.run(tf.local_variables_initializer()) - sess.run(update_ops, feed_dict=feed_dict) - (result_per_class_coverings, result_per_class_weighted_ious, - result_per_class_gt_areas) = ( - sess.run([ - per_class_coverings, - per_class_weighted_ious, - per_class_gt_areas, - ], - feed_dict=feed_dict)) - - np.testing.assert_array_almost_equal( - result_per_class_coverings, [0.0, 0.7009696912, 0.5406896552], - decimal=4) - np.testing.assert_array_almost_equal( - result_per_class_weighted_ious, [0.0, 39864.14634, 3136], decimal=4) - np.testing.assert_array_equal(result_per_class_gt_areas, [0, 56870, 5800]) - - def test_streaming_metric_on_multiple_images(self): - """Tests streaming parsing covering metric.""" - num_classes = 7 - offset = 256 * 256 - - bird_gt_instance_class_map = { - 92: 5, - 176: 3, - 255: 4, - } - cat_gt_instance_class_map = { - 0: 0, - 255: 6, - } - team_gt_instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - test_image = collections.namedtuple( - 'TestImage', - ['gt_class_map', 'gt_path', 'pred_inst_path', 'pred_class_path']) - test_images = [ - test_image(bird_gt_instance_class_map, 'bird_gt.png', - 'bird_pred_instance.png', 'bird_pred_class.png'), - test_image(cat_gt_instance_class_map, 'cat_gt.png', - 'cat_pred_instance.png', 'cat_pred_class.png'), - test_image(team_gt_instance_class_map, 'team_gt_instance.png', - 'team_pred_instance.png', 'team_pred_class.png'), - ] - - gt_classes = [] - gt_instances = [] - pred_classes = [] - pred_instances = [] - for test_image in test_images: - (image_gt_instances, - image_gt_classes) = test_utils.panoptic_segmentation_with_class_map( - test_image.gt_path, test_image.gt_class_map) - gt_classes.append(image_gt_classes) - gt_instances.append(image_gt_instances) - - pred_instances.append( - test_utils.read_test_image(test_image.pred_inst_path, mode='L')) - pred_classes.append( - test_utils.read_segmentation_with_rgb_color_map( - test_image.pred_class_path, _CLASS_COLOR_MAP)) - - gt_class_tensor = tf.placeholder(tf.uint16) - gt_instance_tensor = tf.placeholder(tf.uint16) - pred_class_tensor = tf.placeholder(tf.uint16) - pred_instance_tensor = tf.placeholder(tf.uint16) - coverings, update_ops = streaming_metrics.streaming_parsing_covering( - gt_class_tensor, - gt_instance_tensor, - pred_class_tensor, - pred_instance_tensor, - num_classes=num_classes, - max_instances_per_category=256, - ignored_label=0, - offset=offset, - normalize_by_image_size=False) - (per_class_coverings, per_class_weighted_ious, per_class_gt_areas) = ( - tf.unstack(coverings, num=3, axis=0)) - - with self.session() as sess: - sess.run(tf.local_variables_initializer()) - for pred_class, pred_instance, gt_class, gt_instance in six.moves.zip( - pred_classes, pred_instances, gt_classes, gt_instances): - sess.run( - update_ops, - feed_dict={ - gt_class_tensor: gt_class, - gt_instance_tensor: gt_instance, - pred_class_tensor: pred_class, - pred_instance_tensor: pred_instance - }) - (result_per_class_coverings, result_per_class_weighted_ious, - result_per_class_gt_areas) = ( - sess.run( - [ - per_class_coverings, - per_class_weighted_ious, - per_class_gt_areas, - ], - feed_dict={ - gt_class_tensor: 0, - gt_instance_tensor: 0, - pred_class_tensor: 0, - pred_instance_tensor: 0 - })) - - np.testing.assert_array_almost_equal( - result_per_class_coverings, [ - 0.0, - 0.7009696912, - 0.5406896552, - 0.7453531599, - 0.8576779026, - 0.9910687881, - 0.7741046032, - ], - decimal=4) - np.testing.assert_array_almost_equal( - result_per_class_weighted_ious, [ - 0.0, - 39864.14634, - 3136, - 1177.657993, - 2498.41573, - 33366.31289, - 26671, - ], - decimal=4) - np.testing.assert_array_equal(result_per_class_gt_areas, [ - 0.0, - 56870, - 5800, - 1580, - 2913, - 33667, - 34454, - ]) - - def test_streaming_metric_on_multiple_images_normalize_by_size(self): - """Tests streaming parsing covering metric with image size normalization.""" - num_classes = 7 - offset = 256 * 256 - - bird_gt_instance_class_map = { - 92: 5, - 176: 3, - 255: 4, - } - cat_gt_instance_class_map = { - 0: 0, - 255: 6, - } - team_gt_instance_class_map = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 2, - 215: 1, - 244: 1, - 255: 1, - } - test_image = collections.namedtuple( - 'TestImage', - ['gt_class_map', 'gt_path', 'pred_inst_path', 'pred_class_path']) - test_images = [ - test_image(bird_gt_instance_class_map, 'bird_gt.png', - 'bird_pred_instance.png', 'bird_pred_class.png'), - test_image(cat_gt_instance_class_map, 'cat_gt.png', - 'cat_pred_instance.png', 'cat_pred_class.png'), - test_image(team_gt_instance_class_map, 'team_gt_instance.png', - 'team_pred_instance.png', 'team_pred_class.png'), - ] - - gt_classes = [] - gt_instances = [] - pred_classes = [] - pred_instances = [] - for test_image in test_images: - (image_gt_instances, - image_gt_classes) = test_utils.panoptic_segmentation_with_class_map( - test_image.gt_path, test_image.gt_class_map) - gt_classes.append(image_gt_classes) - gt_instances.append(image_gt_instances) - - pred_instances.append( - test_utils.read_test_image(test_image.pred_inst_path, mode='L')) - pred_classes.append( - test_utils.read_segmentation_with_rgb_color_map( - test_image.pred_class_path, _CLASS_COLOR_MAP)) - - gt_class_tensor = tf.placeholder(tf.uint16) - gt_instance_tensor = tf.placeholder(tf.uint16) - pred_class_tensor = tf.placeholder(tf.uint16) - pred_instance_tensor = tf.placeholder(tf.uint16) - coverings, update_ops = streaming_metrics.streaming_parsing_covering( - gt_class_tensor, - gt_instance_tensor, - pred_class_tensor, - pred_instance_tensor, - num_classes=num_classes, - max_instances_per_category=256, - ignored_label=0, - offset=offset, - normalize_by_image_size=True) - (per_class_coverings, per_class_weighted_ious, per_class_gt_areas) = ( - tf.unstack(coverings, num=3, axis=0)) - - with self.session() as sess: - sess.run(tf.local_variables_initializer()) - for pred_class, pred_instance, gt_class, gt_instance in six.moves.zip( - pred_classes, pred_instances, gt_classes, gt_instances): - sess.run( - update_ops, - feed_dict={ - gt_class_tensor: gt_class, - gt_instance_tensor: gt_instance, - pred_class_tensor: pred_class, - pred_instance_tensor: pred_instance - }) - (result_per_class_coverings, result_per_class_weighted_ious, - result_per_class_gt_areas) = ( - sess.run( - [ - per_class_coverings, - per_class_weighted_ious, - per_class_gt_areas, - ], - feed_dict={ - gt_class_tensor: 0, - gt_instance_tensor: 0, - pred_class_tensor: 0, - pred_instance_tensor: 0 - })) - - np.testing.assert_array_almost_equal( - result_per_class_coverings, [ - 0.0, - 0.7009696912, - 0.5406896552, - 0.7453531599, - 0.8576779026, - 0.9910687881, - 0.7741046032, - ], - decimal=4) - np.testing.assert_array_almost_equal( - result_per_class_weighted_ious, [ - 0.0, - 0.5002088756, - 0.03935002196, - 0.03086105851, - 0.06547211033, - 0.8743792686, - 0.2549565051, - ], - decimal=4) - np.testing.assert_array_almost_equal( - result_per_class_gt_areas, [ - 0.0, - 0.7135955832, - 0.07277746408, - 0.04140461216, - 0.07633647799, - 0.8822589099, - 0.3293566581, - ], - decimal=4) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/evaluation/test_utils.py b/research/deeplab/evaluation/test_utils.py deleted file mode 100644 index 9ad4f551271..00000000000 --- a/research/deeplab/evaluation/test_utils.py +++ /dev/null @@ -1,119 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utility functions to set up unit tests on Panoptic Segmentation code.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - - - -from absl import flags -import numpy as np -import scipy.misc -import six -from six.moves import map - -FLAGS = flags.FLAGS - -_TEST_DIR = 'deeplab/evaluation/testdata' - - -def read_test_image(testdata_path, *args, **kwargs): - """Loads a test image. - - Args: - testdata_path: Image path relative to panoptic_segmentation/testdata as a - string. - *args: Additional positional arguments passed to `imread`. - **kwargs: Additional keyword arguments passed to `imread`. - - Returns: - The image, as a numpy array. - """ - image_path = os.path.join(_TEST_DIR, testdata_path) - return scipy.misc.imread(image_path, *args, **kwargs) - - -def read_segmentation_with_rgb_color_map(image_testdata_path, - rgb_to_semantic_label, - output_dtype=None): - """Reads a test segmentation as an image and a map from colors to labels. - - Args: - image_testdata_path: Image path relative to panoptic_segmentation/testdata - as a string. - rgb_to_semantic_label: Mapping from RGB colors to integer labels as a - dictionary. - output_dtype: Type of the output labels. If None, defaults to the type of - the provided color map. - - Returns: - A 2D numpy array of labels. - - Raises: - ValueError: On an incomplete `rgb_to_semantic_label`. - """ - rgb_image = read_test_image(image_testdata_path, mode='RGB') - if len(rgb_image.shape) != 3 or rgb_image.shape[2] != 3: - raise AssertionError( - 'Expected RGB image, actual shape is %s' % rgb_image.sape) - - num_pixels = rgb_image.shape[0] * rgb_image.shape[1] - unique_colors = np.unique(np.reshape(rgb_image, [num_pixels, 3]), axis=0) - if not set(map(tuple, unique_colors)).issubset( - six.viewkeys(rgb_to_semantic_label)): - raise ValueError('RGB image has colors not in color map.') - - output_dtype = output_dtype or type( - next(six.itervalues(rgb_to_semantic_label))) - output_labels = np.empty(rgb_image.shape[:2], dtype=output_dtype) - for rgb_color, int_label in six.iteritems(rgb_to_semantic_label): - color_array = np.array(rgb_color, ndmin=3) - output_labels[np.all(rgb_image == color_array, axis=2)] = int_label - return output_labels - - -def panoptic_segmentation_with_class_map(instance_testdata_path, - instance_label_to_semantic_label): - """Reads in a panoptic segmentation with an instance map and a map to classes. - - Args: - instance_testdata_path: Path to a grayscale instance map, given as a string - and relative to panoptic_segmentation/testdata. - instance_label_to_semantic_label: A map from instance labels to class - labels. - - Returns: - A tuple `(instance_labels, class_labels)` of numpy arrays. - - Raises: - ValueError: On a mismatched set of instances in - the - `instance_label_to_semantic_label`. - """ - instance_labels = read_test_image(instance_testdata_path, mode='L') - if set(np.unique(instance_labels)) != set( - six.iterkeys(instance_label_to_semantic_label)): - raise ValueError('Provided class map does not match present instance ids.') - - class_labels = np.empty_like(instance_labels) - for instance_id, class_id in six.iteritems(instance_label_to_semantic_label): - class_labels[instance_labels == instance_id] = class_id - - return instance_labels, class_labels diff --git a/research/deeplab/evaluation/test_utils_test.py b/research/deeplab/evaluation/test_utils_test.py deleted file mode 100644 index 9e9bed37e4b..00000000000 --- a/research/deeplab/evaluation/test_utils_test.py +++ /dev/null @@ -1,74 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for test_utils.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - - -from absl.testing import absltest -import numpy as np - -from deeplab.evaluation import test_utils - - -class TestUtilsTest(absltest.TestCase): - - def test_read_test_image(self): - image_array = test_utils.read_test_image('team_pred_class.png') - self.assertSequenceEqual(image_array.shape, (231, 345, 4)) - - def test_reads_segmentation_with_color_map(self): - rgb_to_semantic_label = {(0, 0, 0): 0, (0, 0, 255): 1, (255, 0, 0): 23} - labels = test_utils.read_segmentation_with_rgb_color_map( - 'team_pred_class.png', rgb_to_semantic_label) - - input_image = test_utils.read_test_image('team_pred_class.png') - np.testing.assert_array_equal( - labels == 0, - np.logical_and(input_image[:, :, 0] == 0, input_image[:, :, 2] == 0)) - np.testing.assert_array_equal(labels == 1, input_image[:, :, 2] == 255) - np.testing.assert_array_equal(labels == 23, input_image[:, :, 0] == 255) - - def test_reads_gt_segmentation(self): - instance_label_to_semantic_label = { - 0: 0, - 47: 1, - 97: 1, - 133: 1, - 150: 1, - 174: 1, - 198: 23, - 215: 1, - 244: 1, - 255: 1, - } - instances, classes = test_utils.panoptic_segmentation_with_class_map( - 'team_gt_instance.png', instance_label_to_semantic_label) - - expected_label_shape = (231, 345) - self.assertSequenceEqual(instances.shape, expected_label_shape) - self.assertSequenceEqual(classes.shape, expected_label_shape) - np.testing.assert_array_equal(instances == 0, classes == 0) - np.testing.assert_array_equal(instances == 198, classes == 23) - np.testing.assert_array_equal( - np.logical_and(instances != 0, instances != 198), classes == 1) - - -if __name__ == '__main__': - absltest.main() diff --git a/research/deeplab/evaluation/testdata/README.md b/research/deeplab/evaluation/testdata/README.md deleted file mode 100644 index 711b4767de8..00000000000 --- a/research/deeplab/evaluation/testdata/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# Segmentation Evalaution Test Data - -## Source Images - -* [team_input.png](team_input.png) \ - Source: - https://ai.googleblog.com/2018/03/semantic-image-segmentation-with.html -* [cat_input.jpg](cat_input.jpg) \ - Source: https://www.flickr.com/photos/magdalena_b/4995858743 -* [bird_input.jpg](bird_input.jpg) \ - Source: https://www.flickr.com/photos/chivinskia/40619099560 -* [congress_input.jpg](congress_input.jpg) \ - Source: - https://cao.house.gov/sites/cao.house.gov/files/documents/SAR-Jan-Jun-2016.pdf diff --git a/research/deeplab/evaluation/testdata/bird_gt.png b/research/deeplab/evaluation/testdata/bird_gt.png deleted file mode 100644 index 05d854915d1..00000000000 Binary files a/research/deeplab/evaluation/testdata/bird_gt.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/bird_pred_class.png b/research/deeplab/evaluation/testdata/bird_pred_class.png deleted file mode 100644 index 07351bf0611..00000000000 Binary files a/research/deeplab/evaluation/testdata/bird_pred_class.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/bird_pred_instance.png b/research/deeplab/evaluation/testdata/bird_pred_instance.png deleted file mode 100644 index faa1371f525..00000000000 Binary files a/research/deeplab/evaluation/testdata/bird_pred_instance.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/cat_gt.png b/research/deeplab/evaluation/testdata/cat_gt.png deleted file mode 100644 index 41f60111f3d..00000000000 Binary files a/research/deeplab/evaluation/testdata/cat_gt.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/cat_pred_class.png b/research/deeplab/evaluation/testdata/cat_pred_class.png deleted file mode 100644 index 3728c68ced2..00000000000 Binary files a/research/deeplab/evaluation/testdata/cat_pred_class.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/cat_pred_instance.png b/research/deeplab/evaluation/testdata/cat_pred_instance.png deleted file mode 100644 index ebd9ba4855f..00000000000 Binary files a/research/deeplab/evaluation/testdata/cat_pred_instance.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_gt.json b/research/deeplab/evaluation/testdata/coco_gt.json deleted file mode 100644 index 5f79bf18433..00000000000 --- a/research/deeplab/evaluation/testdata/coco_gt.json +++ /dev/null @@ -1,214 +0,0 @@ -{ - "info": { - "description": "Test COCO-format dataset", - "url": "https://github.com/tensorflow/models/tree/master/research/deeplab", - "version": "1.0", - "year": 2019 - }, - "images": [ - { - "id": 1, - "file_name": "bird.jpg", - "height": 159, - "width": 240, - "flickr_url": "https://www.flickr.com/photos/chivinskia/40619099560" - }, - { - "id": 2, - "file_name": "cat.jpg", - "height": 330, - "width": 317, - "flickr_url": "https://www.flickr.com/photos/magdalena_b/4995858743" - }, - { - "id": 3, - "file_name": "team.jpg", - "height": 231, - "width": 345 - }, - { - "id": 4, - "file_name": "congress.jpg", - "height": 267, - "width": 525 - } - ], - "annotations": [ - { - "image_id": 1, - "file_name": "bird.png", - "segments_info": [ - { - "id": 255, - "area": 2913, - "category_id": 4, - "iscrowd": 0 - }, - { - "id": 2586368, - "area": 1580, - "category_id": 3, - "iscrowd": 0 - }, - { - "id": 16770360, - "area": 33667, - "category_id": 5, - "iscrowd": 0 - } - ] - }, - { - "image_id": 2, - "file_name": "cat.png", - "segments_info": [ - { - "id": 16711691, - "area": 34454, - "category_id": 6, - "iscrowd": 0 - } - ] - }, - { - "image_id": 3, - "file_name": "team.png", - "segments_info": [ - { - "id": 129, - "area": 5443, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 255, - "area": 3574, - "category_id": 2, - "iscrowd": 0 - }, - { - "id": 47615, - "area": 11483, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 65532, - "area": 7080, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 8585107, - "area": 11363, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 9011200, - "area": 7158, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 12858027, - "area": 6419, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16053492, - "area": 4350, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16711680, - "area": 5800, - "category_id": 1, - "iscrowd": 0 - } - ] - }, - { - "image_id": 4, - "file_name": "congress.png", - "segments_info": [ - { - "id": 255, - "area": 243, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 65315, - "area": 553, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 65516, - "area": 652, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 9895680, - "area": 82774, - "category_id": 1, - "iscrowd": 1 - }, - { - "id": 16711739, - "area": 137, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16711868, - "area": 179, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16762624, - "area": 2742, - "category_id": 1, - "iscrowd": 0 - } - ] - } - ], - "categories": [ - { - "id": 1, - "name": "person", - "isthing": 1 - }, - { - "id": 2, - "name": "umbrella", - "isthing": 1 - }, - { - "id": 3, - "name": "tree-merged", - "isthing": 0 - }, - { - "id": 4, - "name": "bird", - "isthing": 1 - }, - { - "id": 5, - "name": "sky", - "isthing": 0 - }, - { - "id": 6, - "name": "cat", - "isthing": 1 - } - ] -} diff --git a/research/deeplab/evaluation/testdata/coco_gt/bird.png b/research/deeplab/evaluation/testdata/coco_gt/bird.png deleted file mode 100644 index 9ef4ad95041..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_gt/bird.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_gt/cat.png b/research/deeplab/evaluation/testdata/coco_gt/cat.png deleted file mode 100644 index cb02530f2f9..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_gt/cat.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_gt/congress.png b/research/deeplab/evaluation/testdata/coco_gt/congress.png deleted file mode 100644 index a56b98d3361..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_gt/congress.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_gt/team.png b/research/deeplab/evaluation/testdata/coco_gt/team.png deleted file mode 100644 index bde358d151a..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_gt/team.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_pred.json b/research/deeplab/evaluation/testdata/coco_pred.json deleted file mode 100644 index 4aead17a65d..00000000000 --- a/research/deeplab/evaluation/testdata/coco_pred.json +++ /dev/null @@ -1,208 +0,0 @@ -{ - "info": { - "description": "Test COCO-format dataset", - "url": "https://github.com/tensorflow/models/tree/master/research/deeplab", - "version": "1.0", - "year": 2019 - }, - "images": [ - { - "id": 1, - "file_name": "bird.jpg", - "height": 159, - "width": 240, - "flickr_url": "https://www.flickr.com/photos/chivinskia/40619099560" - }, - { - "id": 2, - "file_name": "cat.jpg", - "height": 330, - "width": 317, - "flickr_url": "https://www.flickr.com/photos/magdalena_b/4995858743" - }, - { - "id": 3, - "file_name": "team.jpg", - "height": 231, - "width": 345 - }, - { - "id": 4, - "file_name": "congress.jpg", - "height": 267, - "width": 525 - } - ], - "annotations": [ - { - "image_id": 1, - "file_name": "bird.png", - "segments_info": [ - { - "id": 55551, - "area": 3039, - "category_id": 4, - "iscrowd": 0 - }, - { - "id": 16216831, - "area": 33659, - "category_id": 5, - "iscrowd": 0 - }, - { - "id": 16760832, - "area": 1237, - "category_id": 3, - "iscrowd": 0 - } - ] - }, - { - "image_id": 2, - "file_name": "cat.png", - "segments_info": [ - { - "id": 36493, - "area": 26910, - "category_id": 6, - "iscrowd": 0 - } - ] - }, - { - "image_id": 3, - "file_name": "team.png", - "segments_info": [ - { - "id": 0, - "area": 22164, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 129, - "area": 3418, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 255, - "area": 12827, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 740608, - "area": 8606, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 2555695, - "area": 7636, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 2883541, - "area": 6844, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 14408667, - "area": 4766, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16711820, - "area": 4767, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16768768, - "area": 8667, - "category_id": 1, - "iscrowd": 0 - } - ] - }, - { - "image_id": 4, - "file_name": "congress.png", - "segments_info": [ - { - "id": 255, - "area": 2599, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 37375, - "area": 386, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 62207, - "area": 384, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 5177088, - "area": 260, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16711691, - "area": 1011, - "category_id": 1, - "iscrowd": 0 - }, - { - "id": 16774912, - "area": 803, - "category_id": 1, - "iscrowd": 0 - } - ] - } - ], - "categories": [ - { - "id": 1, - "name": "person", - "isthing": 1 - }, - { - "id": 2, - "name": "umbrella", - "isthing": 1 - }, - { - "id": 3, - "name": "tree-merged", - "isthing": 0 - }, - { - "id": 4, - "name": "bird", - "isthing": 1 - }, - { - "id": 5, - "name": "sky", - "isthing": 0 - }, - { - "id": 6, - "name": "cat", - "isthing": 1 - } - ] -} diff --git a/research/deeplab/evaluation/testdata/coco_pred/bird.png b/research/deeplab/evaluation/testdata/coco_pred/bird.png deleted file mode 100644 index c9b4cbcbf44..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_pred/bird.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_pred/cat.png b/research/deeplab/evaluation/testdata/coco_pred/cat.png deleted file mode 100644 index 324583271c4..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_pred/cat.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_pred/congress.png b/research/deeplab/evaluation/testdata/coco_pred/congress.png deleted file mode 100644 index fc7bb06050e..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_pred/congress.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/coco_pred/team.png b/research/deeplab/evaluation/testdata/coco_pred/team.png deleted file mode 100644 index 7300bf41f03..00000000000 Binary files a/research/deeplab/evaluation/testdata/coco_pred/team.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/team_gt_instance.png b/research/deeplab/evaluation/testdata/team_gt_instance.png deleted file mode 100644 index 97abb55273c..00000000000 Binary files a/research/deeplab/evaluation/testdata/team_gt_instance.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/team_pred_class.png b/research/deeplab/evaluation/testdata/team_pred_class.png deleted file mode 100644 index 2ed78de2cbd..00000000000 Binary files a/research/deeplab/evaluation/testdata/team_pred_class.png and /dev/null differ diff --git a/research/deeplab/evaluation/testdata/team_pred_instance.png b/research/deeplab/evaluation/testdata/team_pred_instance.png deleted file mode 100644 index 264606a4d88..00000000000 Binary files a/research/deeplab/evaluation/testdata/team_pred_instance.png and /dev/null differ diff --git a/research/deeplab/export_model.py b/research/deeplab/export_model.py deleted file mode 100644 index b7307b5a212..00000000000 --- a/research/deeplab/export_model.py +++ /dev/null @@ -1,201 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Exports trained model to TensorFlow frozen graph.""" - -import os -import tensorflow as tf - -from tensorflow.contrib import quantize as contrib_quantize -from tensorflow.python.tools import freeze_graph -from deeplab import common -from deeplab import input_preprocess -from deeplab import model - -slim = tf.contrib.slim -flags = tf.app.flags - -FLAGS = flags.FLAGS - -flags.DEFINE_string('checkpoint_path', None, 'Checkpoint path') - -flags.DEFINE_string('export_path', None, - 'Path to output Tensorflow frozen graph.') - -flags.DEFINE_integer('num_classes', 21, 'Number of classes.') - -flags.DEFINE_multi_integer('crop_size', [513, 513], - 'Crop size [height, width].') - -# For `xception_65`, use atrous_rates = [12, 24, 36] if output_stride = 8, or -# rates = [6, 12, 18] if output_stride = 16. For `mobilenet_v2`, use None. Note -# one could use different atrous_rates/output_stride during training/evaluation. -flags.DEFINE_multi_integer('atrous_rates', None, - 'Atrous rates for atrous spatial pyramid pooling.') - -flags.DEFINE_integer('output_stride', 8, - 'The ratio of input to output spatial resolution.') - -# Change to [0.5, 0.75, 1.0, 1.25, 1.5, 1.75] for multi-scale inference. -flags.DEFINE_multi_float('inference_scales', [1.0], - 'The scales to resize images for inference.') - -flags.DEFINE_bool('add_flipped_images', False, - 'Add flipped images during inference or not.') - -flags.DEFINE_integer( - 'quantize_delay_step', -1, - 'Steps to start quantized training. If < 0, will not quantize model.') - -flags.DEFINE_bool('save_inference_graph', False, - 'Save inference graph in text proto.') - -# Input name of the exported model. -_INPUT_NAME = 'ImageTensor' - -# Output name of the exported predictions. -_OUTPUT_NAME = 'SemanticPredictions' -_RAW_OUTPUT_NAME = 'RawSemanticPredictions' - -# Output name of the exported probabilities. -_OUTPUT_PROB_NAME = 'SemanticProbabilities' -_RAW_OUTPUT_PROB_NAME = 'RawSemanticProbabilities' - - -def _create_input_tensors(): - """Creates and prepares input tensors for DeepLab model. - - This method creates a 4-D uint8 image tensor 'ImageTensor' with shape - [1, None, None, 3]. The actual input tensor name to use during inference is - 'ImageTensor:0'. - - Returns: - image: Preprocessed 4-D float32 tensor with shape [1, crop_height, - crop_width, 3]. - original_image_size: Original image shape tensor [height, width]. - resized_image_size: Resized image shape tensor [height, width]. - """ - # input_preprocess takes 4-D image tensor as input. - input_image = tf.placeholder(tf.uint8, [1, None, None, 3], name=_INPUT_NAME) - original_image_size = tf.shape(input_image)[1:3] - - # Squeeze the dimension in axis=0 since `preprocess_image_and_label` assumes - # image to be 3-D. - image = tf.squeeze(input_image, axis=0) - resized_image, image, _ = input_preprocess.preprocess_image_and_label( - image, - label=None, - crop_height=FLAGS.crop_size[0], - crop_width=FLAGS.crop_size[1], - min_resize_value=FLAGS.min_resize_value, - max_resize_value=FLAGS.max_resize_value, - resize_factor=FLAGS.resize_factor, - is_training=False, - model_variant=FLAGS.model_variant) - resized_image_size = tf.shape(resized_image)[:2] - - # Expand the dimension in axis=0, since the following operations assume the - # image to be 4-D. - image = tf.expand_dims(image, 0) - - return image, original_image_size, resized_image_size - - -def main(unused_argv): - tf.logging.set_verbosity(tf.logging.INFO) - tf.logging.info('Prepare to export model to: %s', FLAGS.export_path) - - with tf.Graph().as_default(): - image, image_size, resized_image_size = _create_input_tensors() - - model_options = common.ModelOptions( - outputs_to_num_classes={common.OUTPUT_TYPE: FLAGS.num_classes}, - crop_size=FLAGS.crop_size, - atrous_rates=FLAGS.atrous_rates, - output_stride=FLAGS.output_stride) - - if tuple(FLAGS.inference_scales) == (1.0,): - tf.logging.info('Exported model performs single-scale inference.') - predictions = model.predict_labels( - image, - model_options=model_options, - image_pyramid=FLAGS.image_pyramid) - else: - tf.logging.info('Exported model performs multi-scale inference.') - if FLAGS.quantize_delay_step >= 0: - raise ValueError( - 'Quantize mode is not supported with multi-scale test.') - predictions = model.predict_labels_multi_scale( - image, - model_options=model_options, - eval_scales=FLAGS.inference_scales, - add_flipped_images=FLAGS.add_flipped_images) - raw_predictions = tf.identity( - tf.cast(predictions[common.OUTPUT_TYPE], tf.float32), - _RAW_OUTPUT_NAME) - raw_probabilities = tf.identity( - predictions[common.OUTPUT_TYPE + model.PROB_SUFFIX], - _RAW_OUTPUT_PROB_NAME) - - # Crop the valid regions from the predictions. - semantic_predictions = raw_predictions[ - :, :resized_image_size[0], :resized_image_size[1]] - semantic_probabilities = raw_probabilities[ - :, :resized_image_size[0], :resized_image_size[1]] - - # Resize back the prediction to the original image size. - def _resize_label(label, label_size): - # Expand dimension of label to [1, height, width, 1] for resize operation. - label = tf.expand_dims(label, 3) - resized_label = tf.image.resize_images( - label, - label_size, - method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, - align_corners=True) - return tf.cast(tf.squeeze(resized_label, 3), tf.int32) - semantic_predictions = _resize_label(semantic_predictions, image_size) - semantic_predictions = tf.identity(semantic_predictions, name=_OUTPUT_NAME) - - semantic_probabilities = tf.image.resize_bilinear( - semantic_probabilities, image_size, align_corners=True, - name=_OUTPUT_PROB_NAME) - - if FLAGS.quantize_delay_step >= 0: - contrib_quantize.create_eval_graph() - - saver = tf.train.Saver(tf.all_variables()) - - dirname = os.path.dirname(FLAGS.export_path) - tf.gfile.MakeDirs(dirname) - graph_def = tf.get_default_graph().as_graph_def(add_shapes=True) - freeze_graph.freeze_graph_with_def_protos( - graph_def, - saver.as_saver_def(), - FLAGS.checkpoint_path, - _OUTPUT_NAME + ',' + _OUTPUT_PROB_NAME, - restore_op_name=None, - filename_tensor_name=None, - output_graph=FLAGS.export_path, - clear_devices=True, - initializer_nodes=None) - - if FLAGS.save_inference_graph: - tf.train.write_graph(graph_def, dirname, 'inference_graph.pbtxt') - - -if __name__ == '__main__': - flags.mark_flag_as_required('checkpoint_path') - flags.mark_flag_as_required('export_path') - tf.app.run() diff --git a/research/deeplab/g3doc/ade20k.md b/research/deeplab/g3doc/ade20k.md deleted file mode 100644 index 9505ab2cd99..00000000000 --- a/research/deeplab/g3doc/ade20k.md +++ /dev/null @@ -1,107 +0,0 @@ -# Running DeepLab on ADE20K Semantic Segmentation Dataset - -This page walks through the steps required to run DeepLab on ADE20K dataset on a -local machine. - -## Download dataset and convert to TFRecord - -We have prepared the script (under the folder `datasets`) to download and -convert ADE20K semantic segmentation dataset to TFRecord. - -```bash -# From the tensorflow/models/research/deeplab/datasets directory. -bash download_and_convert_ade20k.sh -``` - -The converted dataset will be saved at ./deeplab/datasets/ADE20K/tfrecord - -## Recommended Directory Structure for Training and Evaluation - -``` -+ datasets - - build_data.py - - build_ade20k_data.py - - download_and_convert_ade20k.sh - + ADE20K - + tfrecord - + exp - + train_on_train_set - + train - + eval - + vis - + ADEChallengeData2016 - + annotations - + training - + validation - + images - + training - + validation -``` - -where the folder `train_on_train_set` stores the train/eval/vis events and -results (when training DeepLab on the ADE20K train set). - -## Running the train/eval/vis jobs - -A local training job using `xception_65` can be run with the following command: - -```bash -# From tensorflow/models/research/ -python deeplab/train.py \ - --logtostderr \ - --training_number_of_steps=150000 \ - --train_split="train" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --train_crop_size="513,513" \ - --train_batch_size=4 \ - --min_resize_value=513 \ - --max_resize_value=513 \ - --resize_factor=16 \ - --dataset="ade20k" \ - --tf_initial_checkpoint=${PATH_TO_INITIAL_CHECKPOINT} \ - --train_logdir=${PATH_TO_TRAIN_DIR}\ - --dataset_dir=${PATH_TO_DATASET} -``` - -where ${PATH\_TO\_INITIAL\_CHECKPOINT} is the path to the initial checkpoint. -${PATH\_TO\_TRAIN\_DIR} is the directory in which training checkpoints and -events will be written to (it is recommended to set it to the -`train_on_train_set/train` above), and ${PATH\_TO\_DATASET} is the directory in -which the ADE20K dataset resides (the `tfrecord` above) - -**Note that for train.py:** - -1. In order to fine tune the BN layers, one needs to use large batch size (> - 12), and set fine_tune_batch_norm = True. Here, we simply use small batch - size during training for the purpose of demonstration. If the users have - limited GPU memory at hand, please fine-tune from our provided checkpoints - whose batch norm parameters have been trained, and use smaller learning rate - with fine_tune_batch_norm = False. - -2. User should fine tune the `min_resize_value` and `max_resize_value` to get - better result. Note that `resize_factor` has to be equal to `output_stride`. - -3. The users should change atrous_rates from [6, 12, 18] to [12, 24, 36] if - setting output_stride=8. - -4. The users could skip the flag, `decoder_output_stride`, if you do not want - to use the decoder structure. - -## Running Tensorboard - -Progress for training and evaluation jobs can be inspected using Tensorboard. If -using the recommended directory structure, Tensorboard can be run using the -following command: - -```bash -tensorboard --logdir=${PATH_TO_LOG_DIRECTORY} -``` - -where `${PATH_TO_LOG_DIRECTORY}` points to the directory that contains the train -directorie (e.g., the folder `train_on_train_set` in the above example). Please -note it may take Tensorboard a couple minutes to populate with data. diff --git a/research/deeplab/g3doc/cityscapes.md b/research/deeplab/g3doc/cityscapes.md deleted file mode 100644 index 5a660aaca34..00000000000 --- a/research/deeplab/g3doc/cityscapes.md +++ /dev/null @@ -1,159 +0,0 @@ -# Running DeepLab on Cityscapes Semantic Segmentation Dataset - -This page walks through the steps required to run DeepLab on Cityscapes on a -local machine. - -## Download dataset and convert to TFRecord - -We have prepared the script (under the folder `datasets`) to convert Cityscapes -dataset to TFRecord. The users are required to download the dataset beforehand -by registering the [website](https://www.cityscapes-dataset.com/). - -```bash -# From the tensorflow/models/research/deeplab/datasets directory. -sh convert_cityscapes.sh -``` - -The converted dataset will be saved at ./deeplab/datasets/cityscapes/tfrecord. - -## Recommended Directory Structure for Training and Evaluation - -``` -+ datasets - + cityscapes - + leftImg8bit - + gtFine - + tfrecord - + exp - + train_on_train_set - + train - + eval - + vis -``` - -where the folder `train_on_train_set` stores the train/eval/vis events and -results (when training DeepLab on the Cityscapes train set). - -## Running the train/eval/vis jobs - -A local training job using `xception_65` can be run with the following command: - -```bash -# From tensorflow/models/research/ -python deeplab/train.py \ - --logtostderr \ - --training_number_of_steps=90000 \ - --train_split="train_fine" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --train_crop_size="769,769" \ - --train_batch_size=1 \ - --dataset="cityscapes" \ - --tf_initial_checkpoint=${PATH_TO_INITIAL_CHECKPOINT} \ - --train_logdir=${PATH_TO_TRAIN_DIR} \ - --dataset_dir=${PATH_TO_DATASET} -``` - -where ${PATH_TO_INITIAL_CHECKPOINT} is the path to the initial checkpoint -(usually an ImageNet pretrained checkpoint), ${PATH_TO_TRAIN_DIR} is the -directory in which training checkpoints and events will be written to, and -${PATH_TO_DATASET} is the directory in which the Cityscapes dataset resides. - -**Note that for {train,eval,vis}.py**: - -1. In order to reproduce our results, one needs to use large batch size (> 8), - and set fine_tune_batch_norm = True. Here, we simply use small batch size - during training for the purpose of demonstration. If the users have limited - GPU memory at hand, please fine-tune from our provided checkpoints whose - batch norm parameters have been trained, and use smaller learning rate with - fine_tune_batch_norm = False. - -2. The users should change atrous_rates from [6, 12, 18] to [12, 24, 36] if - setting output_stride=8. - -3. The users could skip the flag, `decoder_output_stride`, if you do not want - to use the decoder structure. - -4. Change and add the following flags in order to use the provided dense - prediction cell. Note we need to set decoder_output_stride if you want to - use the provided checkpoints which include the decoder module. - -```bash ---model_variant="xception_71" ---dense_prediction_cell_json="deeplab/core/dense_prediction_cell_branch5_top1_cityscapes.json" ---decoder_output_stride=4 -``` - -A local evaluation job using `xception_65` can be run with the following -command: - -```bash -# From tensorflow/models/research/ -python deeplab/eval.py \ - --logtostderr \ - --eval_split="val_fine" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --eval_crop_size="1025,2049" \ - --dataset="cityscapes" \ - --checkpoint_dir=${PATH_TO_CHECKPOINT} \ - --eval_logdir=${PATH_TO_EVAL_DIR} \ - --dataset_dir=${PATH_TO_DATASET} -``` - -where ${PATH_TO_CHECKPOINT} is the path to the trained checkpoint (i.e., the -path to train_logdir), ${PATH_TO_EVAL_DIR} is the directory in which evaluation -events will be written to, and ${PATH_TO_DATASET} is the directory in which the -Cityscapes dataset resides. - -A local visualization job using `xception_65` can be run with the following -command: - -```bash -# From tensorflow/models/research/ -python deeplab/vis.py \ - --logtostderr \ - --vis_split="val_fine" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --vis_crop_size="1025,2049" \ - --dataset="cityscapes" \ - --colormap_type="cityscapes" \ - --checkpoint_dir=${PATH_TO_CHECKPOINT} \ - --vis_logdir=${PATH_TO_VIS_DIR} \ - --dataset_dir=${PATH_TO_DATASET} -``` - -where ${PATH_TO_CHECKPOINT} is the path to the trained checkpoint (i.e., the -path to train_logdir), ${PATH_TO_VIS_DIR} is the directory in which evaluation -events will be written to, and ${PATH_TO_DATASET} is the directory in which the -Cityscapes dataset resides. Note that if the users would like to save the -segmentation results for evaluation server, set also_save_raw_predictions = -True. - -## Running Tensorboard - -Progress for training and evaluation jobs can be inspected using Tensorboard. If -using the recommended directory structure, Tensorboard can be run using the -following command: - -```bash -tensorboard --logdir=${PATH_TO_LOG_DIRECTORY} -``` - -where `${PATH_TO_LOG_DIRECTORY}` points to the directory that contains the -train, eval, and vis directories (e.g., the folder `train_on_train_set` in the -above example). Please note it may take Tensorboard a couple minutes to populate -with data. diff --git a/research/deeplab/g3doc/export_model.md b/research/deeplab/g3doc/export_model.md deleted file mode 100644 index c41649e609a..00000000000 --- a/research/deeplab/g3doc/export_model.md +++ /dev/null @@ -1,23 +0,0 @@ -# Export trained deeplab model to frozen inference graph - -After model training finishes, you could export it to a frozen TensorFlow -inference graph proto. Your trained model checkpoint usually includes the -following files: - -* model.ckpt-${CHECKPOINT_NUMBER}.data-00000-of-00001, -* model.ckpt-${CHECKPOINT_NUMBER}.index -* model.ckpt-${CHECKPOINT_NUMBER}.meta - -After you have identified a candidate checkpoint to export, you can run the -following commandline to export to a frozen graph: - -```bash -# From tensorflow/models/research/ -# Assume all checkpoint files share the same path prefix `${CHECKPOINT_PATH}`. -python deeplab/export_model.py \ - --checkpoint_path=${CHECKPOINT_PATH} \ - --export_path=${OUTPUT_DIR}/frozen_inference_graph.pb -``` - -Please also add other model specific flags as you use for training, such as -`model_variant`, `add_image_level_feature`, etc. diff --git a/research/deeplab/g3doc/faq.md b/research/deeplab/g3doc/faq.md deleted file mode 100644 index 26ff4b3281c..00000000000 --- a/research/deeplab/g3doc/faq.md +++ /dev/null @@ -1,87 +0,0 @@ -# FAQ -___ -Q1: What if I want to use other network backbones, such as ResNet [1], instead of only those provided ones (e.g., Xception)? - -A: The users could modify the provided core/feature_extractor.py to support more network backbones. -___ -Q2: What if I want to train the model on other datasets? - -A: The users could modify the provided dataset/build_{cityscapes,voc2012}_data.py and dataset/segmentation_dataset.py to build their own dataset. -___ -Q3: Where can I download the PASCAL VOC augmented training set? - -A: The PASCAL VOC augmented training set is provided by Bharath Hariharan et al. [2] Please refer to their [website](http://home.bharathh.info/pubs/codes/SBD/download.html) for details and consider citing their paper if using the dataset. -___ -Q4: Why the implementation does not include DenseCRF [3]? - -A: We have not tried this. The interested users could take a look at Philipp Krähenbühl's [website](http://graphics.stanford.edu/projects/densecrf/) and [paper](https://arxiv.org/abs/1210.5644) for details. -___ -Q5: What if I want to train the model and fine-tune the batch normalization parameters? - -A: If given the limited resource at hand, we would suggest you simply fine-tune -from our provided checkpoint whose batch-norm parameters have been trained (i.e., -train with a smaller learning rate, set `fine_tune_batch_norm = false`, and -employ longer training iterations since the learning rate is small). If -you really would like to train by yourself, we would suggest - -1. Set `output_stride = 16` or maybe even `32` (remember to change the flag -`atrous_rates` accordingly, e.g., `atrous_rates = [3, 6, 9]` for -`output_stride = 32`). - -2. Use as many GPUs as possible (change the flag `num_clones` in train.py) and -set `train_batch_size` as large as possible. - -3. Adjust the `train_crop_size` in train.py. Maybe set it to be smaller, e.g., -513x513 (or even 321x321), so that you could use a larger batch size. - -4. Use a smaller network backbone, such as MobileNet-v2. - -___ -Q6: How can I train the model asynchronously? - -A: In the train.py, the users could set `num_replicas` (number of machines for training) and `num_ps_tasks` (we usually set `num_ps_tasks` = `num_replicas` / 2). See slim.deployment.model_deploy for more details. -___ -Q7: I could not reproduce the performance even with the provided checkpoints. - -A: Please try running - -```bash -# Run the simple test with Xception_65 as network backbone. -sh local_test.sh -``` - -or - -```bash -# Run the simple test with MobileNet-v2 as network backbone. -sh local_test_mobilenetv2.sh -``` - -First, make sure you could reproduce the results with our provided setting. -After that, you could start to make a new change one at a time to help debug. -___ -Q8: What value of `eval_crop_size` should I use? - -A: Our model uses whole-image inference, meaning that we need to set `eval_crop_size` equal to `output_stride` * k + 1, where k is an integer and set k so that the resulting `eval_crop_size` is slightly larger the largest -image dimension in the dataset. For example, we have `eval_crop_size` = 513x513 for PASCAL dataset whose largest image dimension is 512. Similarly, we set `eval_crop_size` = 1025x2049 for Cityscapes images whose -image dimension is all equal to 1024x2048. -___ -Q9: Why multi-gpu training is slow? - -A: Please try to use more threads to pre-process the inputs. For, example change [num_readers = 4](https://github.com/tensorflow/models/blob/master/research/deeplab/train.py#L457). -___ - - -## References - -1. **Deep Residual Learning for Image Recognition**
- Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
- [[link]](https://arxiv.org/abs/1512.03385), In CVPR, 2016. - -2. **Semantic Contours from Inverse Detectors**
- Bharath Hariharan, Pablo Arbelaez, Lubomir Bourdev, Subhransu Maji, Jitendra Malik
- [[link]](http://home.bharathh.info/pubs/codes/SBD/download.html), In ICCV, 2011. - -3. **Efficient Inference in Fully Connected CRFs with Gaussian Edge Potentials**
- Philipp Krähenbühl, Vladlen Koltun
- [[link]](http://graphics.stanford.edu/projects/densecrf/), In NIPS, 2011. diff --git a/research/deeplab/g3doc/img/image1.jpg b/research/deeplab/g3doc/img/image1.jpg deleted file mode 100644 index 939b6f9cef3..00000000000 Binary files a/research/deeplab/g3doc/img/image1.jpg and /dev/null differ diff --git a/research/deeplab/g3doc/img/image2.jpg b/research/deeplab/g3doc/img/image2.jpg deleted file mode 100644 index 5ec1b8ac278..00000000000 Binary files a/research/deeplab/g3doc/img/image2.jpg and /dev/null differ diff --git a/research/deeplab/g3doc/img/image3.jpg b/research/deeplab/g3doc/img/image3.jpg deleted file mode 100644 index d788e3dc68d..00000000000 Binary files a/research/deeplab/g3doc/img/image3.jpg and /dev/null differ diff --git a/research/deeplab/g3doc/img/image_info.txt b/research/deeplab/g3doc/img/image_info.txt deleted file mode 100644 index 583d113e7eb..00000000000 --- a/research/deeplab/g3doc/img/image_info.txt +++ /dev/null @@ -1,13 +0,0 @@ -Image provenance: - -image1.jpg: Philippe Put, - https://www.flickr.com/photos/34547181@N00/14499172124 - -image2.jpg: Peretz Partensky - https://www.flickr.com/photos/ifl/3926001309 - -image3.jpg: Peter Harrison - https://www.flickr.com/photos/devcentre/392585679 - - -vis[1-3].png: Showing original image together with DeepLab segmentation map. diff --git a/research/deeplab/g3doc/img/vis1.png b/research/deeplab/g3doc/img/vis1.png deleted file mode 100644 index 41b8ecd8959..00000000000 Binary files a/research/deeplab/g3doc/img/vis1.png and /dev/null differ diff --git a/research/deeplab/g3doc/img/vis2.png b/research/deeplab/g3doc/img/vis2.png deleted file mode 100644 index 7fa7a4cacc4..00000000000 Binary files a/research/deeplab/g3doc/img/vis2.png and /dev/null differ diff --git a/research/deeplab/g3doc/img/vis3.png b/research/deeplab/g3doc/img/vis3.png deleted file mode 100644 index 813b6340a61..00000000000 Binary files a/research/deeplab/g3doc/img/vis3.png and /dev/null differ diff --git a/research/deeplab/g3doc/installation.md b/research/deeplab/g3doc/installation.md deleted file mode 100644 index 591a1f8da50..00000000000 --- a/research/deeplab/g3doc/installation.md +++ /dev/null @@ -1,73 +0,0 @@ -# Installation - -## Dependencies - -DeepLab depends on the following libraries: - -* Numpy -* Pillow 1.0 -* tf Slim (which is included in the "tensorflow/models/research/" checkout) -* Jupyter notebook -* Matplotlib -* Tensorflow - -For detailed steps to install Tensorflow, follow the [Tensorflow installation -instructions](https://www.tensorflow.org/install/). A typical user can install -Tensorflow using one of the following commands: - -```bash -# For CPU -pip install tensorflow -# For GPU -pip install tensorflow-gpu -``` - -The remaining libraries can be installed on Ubuntu 14.04 using via apt-get: - -```bash -sudo apt-get install python-pil python-numpy -pip install --user jupyter -pip install --user matplotlib -pip install --user PrettyTable -``` - -## Add Libraries to PYTHONPATH - -When running locally, the tensorflow/models/research/ directory should be -appended to PYTHONPATH. This can be done by running the following from -tensorflow/models/research/: - -```bash -# From tensorflow/models/research/ -export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim - -# [Optional] for panoptic evaluation, you might need panopticapi: -# https://github.com/cocodataset/panopticapi -# Please clone it to a local directory ${PANOPTICAPI_DIR} -touch ${PANOPTICAPI_DIR}/panopticapi/__init__.py -export PYTHONPATH=$PYTHONPATH:${PANOPTICAPI_DIR}/panopticapi -``` - -Note: This command needs to run from every new terminal you start. If you wish -to avoid running this manually, you can add it as a new line to the end of your -~/.bashrc file. - -# Testing the Installation - -You can test if you have successfully installed the Tensorflow DeepLab by -running the following commands: - -Quick test by running model_test.py: - -```bash -# From tensorflow/models/research/ -python deeplab/model_test.py -``` - -Quick running the whole code on the PASCAL VOC 2012 dataset: - -```bash -# From tensorflow/models/research/deeplab -bash local_test.sh -``` - diff --git a/research/deeplab/g3doc/model_zoo.md b/research/deeplab/g3doc/model_zoo.md deleted file mode 100644 index 76972dc796e..00000000000 --- a/research/deeplab/g3doc/model_zoo.md +++ /dev/null @@ -1,254 +0,0 @@ -# TensorFlow DeepLab Model Zoo - -We provide deeplab models pretrained several datasets, including (1) PASCAL VOC -2012, (2) Cityscapes, and (3) ADE20K for reproducing our results, as well as -some checkpoints that are only pretrained on ImageNet for training your own -models. - -## DeepLab models trained on PASCAL VOC 2012 - -Un-tar'ed directory includes: - -* a frozen inference graph (`frozen_inference_graph.pb`). All frozen inference - graphs by default use output stride of 8, a single eval scale of 1.0 and - no left-right flips, unless otherwise specified. MobileNet-v2 based models - do not include the decoder module. - -* a checkpoint (`model.ckpt.data-00000-of-00001`, `model.ckpt.index`) - -### Model details - -We provide several checkpoints that have been pretrained on VOC 2012 train_aug -set or train_aug + trainval set. In the former case, one could train their model -with smaller batch size and freeze batch normalization when limited GPU memory -is available, since we have already fine-tuned the batch normalization for you. -In the latter case, one could directly evaluate the checkpoints on VOC 2012 test -set or use this checkpoint for demo. Note *MobileNet-v2* based models do not -employ ASPP and decoder modules for fast computation. - -Checkpoint name | Network backbone | Pretrained dataset | ASPP | Decoder ---------------------------- | :--------------: | :-----------------: | :---: | :-----: -mobilenetv2_dm05_coco_voc_trainaug | MobileNet-v2
Depth-Multiplier = 0.5 | ImageNet
MS-COCO
VOC 2012 train_aug set| N/A | N/A -mobilenetv2_dm05_coco_voc_trainval | MobileNet-v2
Depth-Multiplier = 0.5 | ImageNet
MS-COCO
VOC 2012 train_aug + trainval sets | N/A | N/A -mobilenetv2_coco_voc_trainaug | MobileNet-v2 | ImageNet
MS-COCO
VOC 2012 train_aug set| N/A | N/A -mobilenetv2_coco_voc_trainval | MobileNet-v2 | ImageNet
MS-COCO
VOC 2012 train_aug + trainval sets | N/A | N/A -xception65_coco_voc_trainaug | Xception_65 | ImageNet
MS-COCO
VOC 2012 train_aug set| [6,12,18] for OS=16
[12,24,36] for OS=8 | OS = 4 -xception65_coco_voc_trainval | Xception_65 | ImageNet
MS-COCO
VOC 2012 train_aug + trainval sets | [6,12,18] for OS=16
[12,24,36] for OS=8 | OS = 4 - -In the table, **OS** denotes output stride. - -Checkpoint name | Eval OS | Eval scales | Left-right Flip | Multiply-Adds | Runtime (sec) | PASCAL mIOU | File Size ------------------------------------------------------------------------------------------------------------------------- | :-------: | :------------------------: | :-------------: | :------------------: | :------------: | :----------------------------: | :-------: -[mobilenetv2_dm05_coco_voc_trainaug](http://download.tensorflow.org/models/deeplabv3_mnv2_dm05_pascal_trainaug_2018_10_01.tar.gz) | 16 | [1.0] | No | 0.88B | - | 70.19% (val) | 7.6MB -[mobilenetv2_dm05_coco_voc_trainval](http://download.tensorflow.org/models/deeplabv3_mnv2_dm05_pascal_trainval_2018_10_01.tar.gz) | 8 | [1.0] | No | 2.84B | - | 71.83% (test) | 7.6MB -[mobilenetv2_coco_voc_trainaug](http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz) | 16
8 | [1.0]
[0.5:0.25:1.75] | No
Yes | 2.75B
152.59B | 0.1
26.9 | 75.32% (val)
77.33 (val) | 23MB -[mobilenetv2_coco_voc_trainval](http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz) | 8 | [0.5:0.25:1.75] | Yes | 152.59B | 26.9 | 80.25% (**test**) | 23MB -[xception65_coco_voc_trainaug](http://download.tensorflow.org/models/deeplabv3_pascal_train_aug_2018_01_04.tar.gz) | 16
8 | [1.0]
[0.5:0.25:1.75] | No
Yes | 54.17B
3055.35B | 0.7
223.2 | 82.20% (val)
83.58% (val) | 439MB -[xception65_coco_voc_trainval](http://download.tensorflow.org/models/deeplabv3_pascal_trainval_2018_01_04.tar.gz) | 8 | [0.5:0.25:1.75] | Yes | 3055.35B | 223.2 | 87.80% (**test**) | 439MB - -In the table, we report both computation complexity (in terms of Multiply-Adds -and CPU Runtime) and segmentation performance (in terms of mIOU) on the PASCAL -VOC val or test set. The reported runtime is calculated by tfprof on a -workstation with CPU E5-1650 v3 @ 3.50GHz and 32GB memory. Note that applying -multi-scale inputs and left-right flips increases the segmentation performance -but also significantly increases the computation and thus may not be suitable -for real-time applications. - -## DeepLab models trained on Cityscapes - -### Model details - -We provide several checkpoints that have been pretrained on Cityscapes -train_fine set. Note *MobileNet-v2* based model has been pretrained on MS-COCO -dataset and does not employ ASPP and decoder modules for fast computation. - -Checkpoint name | Network backbone | Pretrained dataset | ASPP | Decoder -------------------------------------- | :--------------: | :-------------------------------------: | :----------------------------------------------: | :-----: -mobilenetv2_coco_cityscapes_trainfine | MobileNet-v2 | ImageNet
MS-COCO
Cityscapes train_fine set | N/A | N/A -mobilenetv3_large_cityscapes_trainfine | MobileNet-v3 Large | Cityscapes train_fine set
(No ImageNet) | N/A | OS = 8 -mobilenetv3_small_cityscapes_trainfine | MobileNet-v3 Small | Cityscapes train_fine set
(No ImageNet) | N/A | OS = 8 -xception65_cityscapes_trainfine | Xception_65 | ImageNet
Cityscapes train_fine set | [6, 12, 18] for OS=16
[12, 24, 36] for OS=8 | OS = 4 -xception71_dpc_cityscapes_trainfine | Xception_71 | ImageNet
MS-COCO
Cityscapes train_fine set | Dense Prediction Cell | OS = 4 -xception71_dpc_cityscapes_trainval | Xception_71 | ImageNet
MS-COCO
Cityscapes trainval_fine and coarse set | Dense Prediction Cell | OS = 4 - -In the table, **OS** denotes output stride. - -Note for mobilenet v3 models, we use additional commandline flags as follows: - -``` ---model_variant={ mobilenet_v3_large_seg | mobilenet_v3_small_seg } ---image_pooling_crop_size=769,769 ---image_pooling_stride=4,5 ---add_image_level_feature=1 ---aspp_convs_filters=128 ---aspp_with_concat_projection=0 ---aspp_with_squeeze_and_excitation=1 ---decoder_use_sum_merge=1 ---decoder_filters=19 ---decoder_output_is_logits=1 ---image_se_uses_qsigmoid=1 ---decoder_output_stride=8 ---output_stride=32 -``` - -Checkpoint name | Eval OS | Eval scales | Left-right Flip | Multiply-Adds | Runtime (sec) | Cityscapes mIOU | File Size --------------------------------------------------------------------------------------------------------------------------------- | :-------: | :-------------------------: | :-------------: | :-------------------: | :------------: | :----------------------------: | :-------: -[mobilenetv2_coco_cityscapes_trainfine](http://download.tensorflow.org/models/deeplabv3_mnv2_cityscapes_train_2018_02_05.tar.gz) | 16
8 | [1.0]
[0.75:0.25:1.25] | No
Yes | 21.27B
433.24B | 0.8
51.12 | 70.71% (val)
73.57% (val) | 23MB -[mobilenetv3_large_cityscapes_trainfine](http://download.tensorflow.org/models/deeplab_mnv3_large_cityscapes_trainfine_2019_11_15.tar.gz) | 32 | [1.0] | No | 15.95B | 0.6 | 72.41% (val) | 17MB -[mobilenetv3_small_cityscapes_trainfine](http://download.tensorflow.org/models/deeplab_mnv3_small_cityscapes_trainfine_2019_11_15.tar.gz) | 32 | [1.0] | No | 4.63B | 0.4 | 68.99% (val) | 5MB -[xception65_cityscapes_trainfine](http://download.tensorflow.org/models/deeplabv3_cityscapes_train_2018_02_06.tar.gz) | 16
8 | [1.0]
[0.75:0.25:1.25] | No
Yes | 418.64B
8677.92B | 5.0
422.8 | 78.79% (val)
80.42% (val) | 439MB -[xception71_dpc_cityscapes_trainfine](http://download.tensorflow.org/models/deeplab_cityscapes_xception71_trainfine_2018_09_08.tar.gz) | 16 | [1.0] | No | 502.07B | - | 80.31% (val) | 445MB -[xception71_dpc_cityscapes_trainval](http://download.tensorflow.org/models/deeplab_cityscapes_xception71_trainvalfine_2018_09_08.tar.gz) | 8 | [0.75:0.25:2] | Yes | - | - | 82.66% (**test**) | 446MB - -### EdgeTPU-DeepLab models on Cityscapes - -EdgeTPU is Google's machine learning accelerator architecture for edge devices -(exists in Coral devices and Pixel4's Neural Core). Leveraging nerual -architecture search (NAS, also named as Auto-ML) algorithms, -[EdgeTPU-Mobilenet](https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet) -has been released which yields higher hardware utilization, lower latency, as -well as better accuracy over Mobilenet-v2/v3. We use EdgeTPU-Mobilenet as the -backbone and provide checkpoints that have been pretrained on Cityscapes -train_fine set. We named them as EdgeTPU-DeepLab models. - -Checkpoint name | Network backbone | Pretrained dataset | ASPP | Decoder --------------------- | :----------------: | :----------------: | :--: | :-----: -EdgeTPU-DeepLab | EdgeMobilenet-1.0 | ImageNet | N/A | N/A -EdgeTPU-DeepLab-slim | EdgeMobilenet-0.75 | ImageNet | N/A | N/A - -For EdgeTPU-DeepLab-slim, the backbone feature extractor has depth multiplier = -0.75 and aspp_convs_filters = 128. We do not employ ASPP nor decoder modules to -further reduce the latency. We employ the same train/eval flags used for -MobileNet-v2 DeepLab model. Flags changed for EdgeTPU-DeepLab model are listed -here. - -``` ---decoder_output_stride='' ---aspp_convs_filters=256 ---model_variant=mobilenet_edgetpu -``` - -For EdgeTPU-DeepLab-slim, also include the following flags. - -``` ---depth_multiplier=0.75 ---aspp_convs_filters=128 -``` - -Checkpoint name | Eval OS | Eval scales | Cityscapes mIOU | Multiply-Adds | Simulator latency on Pixel 4 EdgeTPU ----------------------------------------------------------------------------------------------------- | :--------: | :---------: | :--------------------------: | :------------: | :----------------------------------: -[EdgeTPU-DeepLab](http://download.tensorflow.org/models/edgetpu-deeplab_2020_03_09.tar.gz) | 32
16 | [1.0] | 70.6% (val)
74.1% (val) | 5.6B
7.1B | 13.8 ms
17.5 ms -[EdgeTPU-DeepLab-slim](http://download.tensorflow.org/models/edgetpu-deeplab-slim_2020_03_09.tar.gz) | 32
16 | [1.0] | 70.0% (val)
73.2% (val) | 3.5B
4.3B | 9.9 ms
13.2 ms - -## DeepLab models trained on ADE20K - -### Model details - -We provide some checkpoints that have been pretrained on ADE20K training set. -Note that the model has only been pretrained on ImageNet, following the -dataset rule. - -Checkpoint name | Network backbone | Pretrained dataset | ASPP | Decoder | Input size -------------------------------------- | :--------------: | :-------------------------------------: | :----------------------------------------------: | :-----: | :-----: -mobilenetv2_ade20k_train | MobileNet-v2 | ImageNet
ADE20K training set | N/A | OS = 4 | 257x257 -xception65_ade20k_train | Xception_65 | ImageNet
ADE20K training set | [6, 12, 18] for OS=16
[12, 24, 36] for OS=8 | OS = 4 | 513x513 - -The input dimensions of ADE20K have a huge amount of variation. We resize inputs so that the longest size is 257 for MobileNet-v2 (faster inference) and 513 for Xception_65 (better performation). Note that we also include the decoder module in the MobileNet-v2 checkpoint. - -Checkpoint name | Eval OS | Eval scales | Left-right Flip | mIOU | Pixel-wise Accuracy | File Size -------------------------------------- | :-------: | :-------------------------: | :-------------: | :-------------------: | :-------------------: | :-------: -[mobilenetv2_ade20k_train](http://download.tensorflow.org/models/deeplabv3_mnv2_ade20k_train_2018_12_03.tar.gz) | 16 | [1.0] | No | 32.04% (val) | 75.41% (val) | 24.8MB -[xception65_ade20k_train](http://download.tensorflow.org/models/deeplabv3_xception_ade20k_train_2018_05_29.tar.gz) | 8 | [0.5:0.25:1.75] | Yes | 45.65% (val) | 82.52% (val) | 439MB - - -## Checkpoints pretrained on ImageNet - -Un-tar'ed directory includes: - -* model checkpoint (`model.ckpt.data-00000-of-00001`, `model.ckpt.index`). - -### Model details - -We also provide some checkpoints that are pretrained on ImageNet and/or COCO (as -post-fixed in the model name) so that one could use this for training your own -models. - -* mobilenet_v2: We refer the interested users to the TensorFlow open source - [MobileNet-V2](https://github.com/tensorflow/models/tree/master/research/slim/nets/mobilenet) - for details. - -* xception_{41,65,71}: We adapt the original Xception model to the task of - semantic segmentation with the following changes: (1) more layers, (2) all - max pooling operations are replaced by strided (atrous) separable - convolutions, and (3) extra batch-norm and ReLU after each 3x3 depthwise - convolution are added. We provide three Xception model variants with - different network depths. - -* resnet_v1_{50,101}_beta: We modify the original ResNet-101 [10], similar to - PSPNet [11] by replacing the first 7x7 convolution with three 3x3 - convolutions. See resnet_v1_beta.py for more details. - -Model name | File Size --------------------------------------------------------------------------------------- | :-------: -[xception_41_imagenet](http://download.tensorflow.org/models/xception_41_2018_05_09.tar.gz ) | 288MB -[xception_65_imagenet](http://download.tensorflow.org/models/deeplabv3_xception_2018_01_04.tar.gz) | 447MB -[xception_65_imagenet_coco](http://download.tensorflow.org/models/xception_65_coco_pretrained_2018_10_02.tar.gz) | 292MB -[xception_71_imagenet](http://download.tensorflow.org/models/xception_71_2018_05_09.tar.gz ) | 474MB -[resnet_v1_50_beta_imagenet](http://download.tensorflow.org/models/resnet_v1_50_2018_05_04.tar.gz) | 274MB -[resnet_v1_101_beta_imagenet](http://download.tensorflow.org/models/resnet_v1_101_2018_05_04.tar.gz) | 477MB - -## References - -1. **Mobilenets: Efficient convolutional neural networks for mobile vision applications**
- Andrew G. Howard, Menglong Zhu, Bo Chen, Dmitry Kalenichenko, Weijun Wang, Tobias Weyand, Marco Andreetto, Hartwig Adam
- [[link]](https://arxiv.org/abs/1704.04861). arXiv:1704.04861, 2017. - -2. **Inverted Residuals and Linear Bottlenecks: Mobile Networks for Classification, Detection and Segmentation**
- Mark Sandler, Andrew Howard, Menglong Zhu, Andrey Zhmoginov, Liang-Chieh Chen
- [[link]](https://arxiv.org/abs/1801.04381). arXiv:1801.04381, 2018. - -3. **Xception: Deep Learning with Depthwise Separable Convolutions**
- François Chollet
- [[link]](https://arxiv.org/abs/1610.02357). In the Proc. of CVPR, 2017. - -4. **Deformable Convolutional Networks -- COCO Detection and Segmentation Challenge 2017 Entry**
- Haozhi Qi, Zheng Zhang, Bin Xiao, Han Hu, Bowen Cheng, Yichen Wei, Jifeng Dai
- [[link]](http://presentations.cocodataset.org/COCO17-Detect-MSRA.pdf). ICCV COCO Challenge - Workshop, 2017. - -5. **The Pascal Visual Object Classes Challenge: A Retrospective**
- Mark Everingham, S. M. Ali Eslami, Luc Van Gool, Christopher K. I. Williams, John M. Winn, Andrew Zisserman
- [[link]](http://host.robots.ox.ac.uk/pascal/VOC/voc2012/). IJCV, 2014. - -6. **Semantic Contours from Inverse Detectors**
- Bharath Hariharan, Pablo Arbelaez, Lubomir Bourdev, Subhransu Maji, Jitendra Malik
- [[link]](http://home.bharathh.info/pubs/codes/SBD/download.html). In the Proc. of ICCV, 2011. - -7. **The Cityscapes Dataset for Semantic Urban Scene Understanding**
- Cordts, Marius, Mohamed Omran, Sebastian Ramos, Timo Rehfeld, Markus Enzweiler, Rodrigo Benenson, Uwe Franke, Stefan Roth, Bernt Schiele.
- [[link]](https://www.cityscapes-dataset.com/). In the Proc. of CVPR, 2016. - -8. **Microsoft COCO: Common Objects in Context**
- Tsung-Yi Lin, Michael Maire, Serge Belongie, Lubomir Bourdev, Ross Girshick, James Hays, Pietro Perona, Deva Ramanan, C. Lawrence Zitnick, Piotr Dollar
- [[link]](http://cocodataset.org/). In the Proc. of ECCV, 2014. - -9. **ImageNet Large Scale Visual Recognition Challenge**
- Olga Russakovsky, Jia Deng, Hao Su, Jonathan Krause, Sanjeev Satheesh, Sean Ma, Zhiheng Huang, Andrej Karpathy, Aditya Khosla, Michael Bernstein, Alexander C. Berg, Li Fei-Fei
- [[link]](http://www.image-net.org/). IJCV, 2015. - -10. **Deep Residual Learning for Image Recognition**
- Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun
- [[link]](https://arxiv.org/abs/1512.03385). CVPR, 2016. - -11. **Pyramid Scene Parsing Network**
- Hengshuang Zhao, Jianping Shi, Xiaojuan Qi, Xiaogang Wang, Jiaya Jia
- [[link]](https://arxiv.org/abs/1612.01105). In CVPR, 2017. - -12. **Scene Parsing through ADE20K Dataset**
- Bolei Zhou, Hang Zhao, Xavier Puig, Sanja Fidler, Adela Barriuso, Antonio Torralba
- [[link]](http://groups.csail.mit.edu/vision/datasets/ADE20K/). In CVPR, - 2017. - -13. **Searching for MobileNetV3**
- Andrew Howard, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Chen, Mingxing Tan, Weijun Wang, Yukun Zhu, Ruoming Pang, Vijay Vasudevan, Quoc V. Le, Hartwig Adam
- [[link]](https://arxiv.org/abs/1905.02244). In ICCV, 2019. diff --git a/research/deeplab/g3doc/pascal.md b/research/deeplab/g3doc/pascal.md deleted file mode 100644 index f4bc84eabb8..00000000000 --- a/research/deeplab/g3doc/pascal.md +++ /dev/null @@ -1,161 +0,0 @@ -# Running DeepLab on PASCAL VOC 2012 Semantic Segmentation Dataset - -This page walks through the steps required to run DeepLab on PASCAL VOC 2012 on -a local machine. - -## Download dataset and convert to TFRecord - -We have prepared the script (under the folder `datasets`) to download and -convert PASCAL VOC 2012 semantic segmentation dataset to TFRecord. - -```bash -# From the tensorflow/models/research/deeplab/datasets directory. -sh download_and_convert_voc2012.sh -``` - -The converted dataset will be saved at -./deeplab/datasets/pascal_voc_seg/tfrecord - -## Recommended Directory Structure for Training and Evaluation - -``` -+ datasets - + pascal_voc_seg - + VOCdevkit - + VOC2012 - + JPEGImages - + SegmentationClass - + tfrecord - + exp - + train_on_train_set - + train - + eval - + vis -``` - -where the folder `train_on_train_set` stores the train/eval/vis events and -results (when training DeepLab on the PASCAL VOC 2012 train set). - -## Running the train/eval/vis jobs - -A local training job using `xception_65` can be run with the following command: - -```bash -# From tensorflow/models/research/ -python deeplab/train.py \ - --logtostderr \ - --training_number_of_steps=30000 \ - --train_split="train" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --train_crop_size="513,513" \ - --train_batch_size=1 \ - --dataset="pascal_voc_seg" \ - --tf_initial_checkpoint=${PATH_TO_INITIAL_CHECKPOINT} \ - --train_logdir=${PATH_TO_TRAIN_DIR} \ - --dataset_dir=${PATH_TO_DATASET} -``` - -where ${PATH_TO_INITIAL_CHECKPOINT} is the path to the initial checkpoint -(usually an ImageNet pretrained checkpoint), ${PATH_TO_TRAIN_DIR} is the -directory in which training checkpoints and events will be written to, and -${PATH_TO_DATASET} is the directory in which the PASCAL VOC 2012 dataset -resides. - -**Note that for {train,eval,vis}.py:** - -1. In order to reproduce our results, one needs to use large batch size (> 12), - and set fine_tune_batch_norm = True. Here, we simply use small batch size - during training for the purpose of demonstration. If the users have limited - GPU memory at hand, please fine-tune from our provided checkpoints whose - batch norm parameters have been trained, and use smaller learning rate with - fine_tune_batch_norm = False. - -2. The users should change atrous_rates from [6, 12, 18] to [12, 24, 36] if - setting output_stride=8. - -3. The users could skip the flag, `decoder_output_stride`, if you do not want - to use the decoder structure. - -A local evaluation job using `xception_65` can be run with the following -command: - -```bash -# From tensorflow/models/research/ -python deeplab/eval.py \ - --logtostderr \ - --eval_split="val" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --eval_crop_size="513,513" \ - --dataset="pascal_voc_seg" \ - --checkpoint_dir=${PATH_TO_CHECKPOINT} \ - --eval_logdir=${PATH_TO_EVAL_DIR} \ - --dataset_dir=${PATH_TO_DATASET} -``` - -where ${PATH_TO_CHECKPOINT} is the path to the trained checkpoint (i.e., the -path to train_logdir), ${PATH_TO_EVAL_DIR} is the directory in which evaluation -events will be written to, and ${PATH_TO_DATASET} is the directory in which the -PASCAL VOC 2012 dataset resides. - -A local visualization job using `xception_65` can be run with the following -command: - -```bash -# From tensorflow/models/research/ -python deeplab/vis.py \ - --logtostderr \ - --vis_split="val" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --vis_crop_size="513,513" \ - --dataset="pascal_voc_seg" \ - --checkpoint_dir=${PATH_TO_CHECKPOINT} \ - --vis_logdir=${PATH_TO_VIS_DIR} \ - --dataset_dir=${PATH_TO_DATASET} -``` - -where ${PATH_TO_CHECKPOINT} is the path to the trained checkpoint (i.e., the -path to train_logdir), ${PATH_TO_VIS_DIR} is the directory in which evaluation -events will be written to, and ${PATH_TO_DATASET} is the directory in which the -PASCAL VOC 2012 dataset resides. Note that if the users would like to save the -segmentation results for evaluation server, set also_save_raw_predictions = -True. - -## Running Tensorboard - -Progress for training and evaluation jobs can be inspected using Tensorboard. If -using the recommended directory structure, Tensorboard can be run using the -following command: - -```bash -tensorboard --logdir=${PATH_TO_LOG_DIRECTORY} -``` - -where `${PATH_TO_LOG_DIRECTORY}` points to the directory that contains the -train, eval, and vis directories (e.g., the folder `train_on_train_set` in the -above example). Please note it may take Tensorboard a couple minutes to populate -with data. - -## Example - -We provide a script to run the {train,eval,vis,export_model}.py on the PASCAL VOC -2012 dataset as an example. See the code in local_test.sh for details. - -```bash -# From tensorflow/models/research/deeplab -sh local_test.sh -``` diff --git a/research/deeplab/g3doc/quantize.md b/research/deeplab/g3doc/quantize.md deleted file mode 100644 index 65dbdd70b4d..00000000000 --- a/research/deeplab/g3doc/quantize.md +++ /dev/null @@ -1,103 +0,0 @@ -# Quantize DeepLab model for faster on-device inference - -This page describes the steps required to quantize DeepLab model and convert it -to TFLite for on-device inference. The main steps include: - -1. Quantization-aware training -1. Exporting model -1. Converting to TFLite FlatBuffer - -We provide details for each step below. - -## Quantization-aware training - -DeepLab supports two approaches to quantize your model. - -1. **[Recommended]** Training a non-quantized model until convergence. Then - fine-tune the trained float model with quantization using a small learning - rate (on PASCAL we use the value of 3e-5) . This fine-tuning step usually - takes 2k to 5k steps to converge. - -1. Training a deeplab float model with delayed quantization. Usually we delay - quantization until the last a few thousand steps in training. - -In the current implementation, quantization is only supported with 1) -`num_clones=1` for training and 2) single scale inference for evaluation, -visualization and model export. To get the best performance for the quantized -model, we strongly recommend to train the float model with larger `num_clones` -and then fine-tune the model with a single clone. - -Here shows the commandline to quantize deeplab model trained on PASCAL VOC -dataset using fine-tuning: - -``` -# From tensorflow/models/research/ -python deeplab/train.py \ - --logtostderr \ - --training_number_of_steps=3000 \ - --train_split="train" \ - --model_variant="mobilenet_v2" \ - --output_stride=16 \ - --train_crop_size="513,513" \ - --train_batch_size=8 \ - --base_learning_rate=3e-5 \ - --dataset="pascal_voc_seg" \ - --quantize_delay_step=0 \ - --tf_initial_checkpoint=${PATH_TO_TRAINED_FLOAT_MODEL} \ - --train_logdir=${PATH_TO_TRAIN_DIR} \ - --dataset_dir=${PATH_TO_DATASET} -``` - -## Converting to TFLite FlatBuffer - -First use the following commandline to export your trained model. - -``` -# From tensorflow/models/research/ -python deeplab/export_model.py \ - --checkpoint_path=${CHECKPOINT_PATH} \ - --quantize_delay_step=0 \ - --export_path=${OUTPUT_DIR}/frozen_inference_graph.pb - -``` - -Commandline below shows how to convert exported graphdef to TFlite model. - -``` -# From tensorflow/models/research/ -python deeplab/convert_to_tflite.py \ - --quantized_graph_def_path=${OUTPUT_DIR}/frozen_inference_graph.pb \ - --input_tensor_name=MobilenetV2/MobilenetV2/input:0 \ - --output_tflite_path=${OUTPUT_DIR}/frozen_inference_graph.tflite \ - --test_image_path=${PATH_TO_TEST_IMAGE} -``` - -**[Important]** Note that converted model expects 513x513 RGB input and doesn't -include preprocessing (resize and pad input image) and post processing (crop -padded region and resize to original input size). These steps can be implemented -outside of TFlite model. - -## Quantized model on PASCAL VOC - -We provide float and quantized checkpoints that have been pretrained on VOC 2012 -train_aug set, using MobileNet-v2 backbone with different depth multipliers. -Quantized model usually have 1% decay in mIoU. - -For quantized (8bit) model, un-tar'ed directory includes: - -* a frozen inference graph (frozen_inference_graph.pb) - -* a checkpoint (model.ckpt.data*, model.ckpt.index) - -* a converted TFlite FlatBuffer file (frozen_inference_graph.tflite) - -Checkpoint name | Eval OS | Eval scales | Left-right Flip | Multiply-Adds | Quantize | PASCAL mIOU | Folder Size | TFLite File Size --------------------------------------------------------------------------------------------------------------------------------------------- | :-----: | :---------: | :-------------: | :-----------: | :------: | :----------: | :-------: | :-------: -[mobilenetv2_dm05_coco_voc_trainaug](http://download.tensorflow.org/models/deeplabv3_mnv2_dm05_pascal_trainaug_2018_10_01.tar.gz) | 16 | [1.0] | No | 0.88B | No | 70.19% (val) | 7.6MB | N/A -[mobilenetv2_dm05_coco_voc_trainaug_8bit](http://download.tensorflow.org/models/deeplabv3_mnv2_dm05_pascal_train_aug_8bit_2019_04_26.tar.gz) | 16 | [1.0] | No | 0.88B | Yes | 69.65% (val) | 8.2MB | 751.1KB -[mobilenetv2_coco_voc_trainaug](http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz) | 16 | [1.0] | No | 2.75B | No | 75.32% (val) | 23MB | N/A -[mobilenetv2_coco_voc_trainaug_8bit](http://download.tensorflow.org/models/deeplabv3_mnv2_pascal_train_aug_8bit_2019_04_26.tar.gz) | 16 | [1.0] | No | 2.75B | Yes | 74.26% (val) | 24MB | 2.2MB - -Note that you might need the nightly build of TensorFlow (see -[here](https://www.tensorflow.org/install) for install instructions) to convert -above quantized model to TFLite. diff --git a/research/deeplab/input_preprocess.py b/research/deeplab/input_preprocess.py deleted file mode 100644 index 9ca8bce4eb9..00000000000 --- a/research/deeplab/input_preprocess.py +++ /dev/null @@ -1,139 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Prepares the data used for DeepLab training/evaluation.""" -import tensorflow as tf -from deeplab.core import feature_extractor -from deeplab.core import preprocess_utils - - -# The probability of flipping the images and labels -# left-right during training -_PROB_OF_FLIP = 0.5 - - -def preprocess_image_and_label(image, - label, - crop_height, - crop_width, - min_resize_value=None, - max_resize_value=None, - resize_factor=None, - min_scale_factor=1., - max_scale_factor=1., - scale_factor_step_size=0, - ignore_label=255, - is_training=True, - model_variant=None): - """Preprocesses the image and label. - - Args: - image: Input image. - label: Ground truth annotation label. - crop_height: The height value used to crop the image and label. - crop_width: The width value used to crop the image and label. - min_resize_value: Desired size of the smaller image side. - max_resize_value: Maximum allowed size of the larger image side. - resize_factor: Resized dimensions are multiple of factor plus one. - min_scale_factor: Minimum scale factor value. - max_scale_factor: Maximum scale factor value. - scale_factor_step_size: The step size from min scale factor to max scale - factor. The input is randomly scaled based on the value of - (min_scale_factor, max_scale_factor, scale_factor_step_size). - ignore_label: The label value which will be ignored for training and - evaluation. - is_training: If the preprocessing is used for training or not. - model_variant: Model variant (string) for choosing how to mean-subtract the - images. See feature_extractor.network_map for supported model variants. - - Returns: - original_image: Original image (could be resized). - processed_image: Preprocessed image. - label: Preprocessed ground truth segmentation label. - - Raises: - ValueError: Ground truth label not provided during training. - """ - if is_training and label is None: - raise ValueError('During training, label must be provided.') - if model_variant is None: - tf.logging.warning('Default mean-subtraction is performed. Please specify ' - 'a model_variant. See feature_extractor.network_map for ' - 'supported model variants.') - - # Keep reference to original image. - original_image = image - - processed_image = tf.cast(image, tf.float32) - - if label is not None: - label = tf.cast(label, tf.int32) - - # Resize image and label to the desired range. - if min_resize_value or max_resize_value: - [processed_image, label] = ( - preprocess_utils.resize_to_range( - image=processed_image, - label=label, - min_size=min_resize_value, - max_size=max_resize_value, - factor=resize_factor, - align_corners=True)) - # The `original_image` becomes the resized image. - original_image = tf.identity(processed_image) - - # Data augmentation by randomly scaling the inputs. - if is_training: - scale = preprocess_utils.get_random_scale( - min_scale_factor, max_scale_factor, scale_factor_step_size) - processed_image, label = preprocess_utils.randomly_scale_image_and_label( - processed_image, label, scale) - processed_image.set_shape([None, None, 3]) - - # Pad image and label to have dimensions >= [crop_height, crop_width] - image_shape = tf.shape(processed_image) - image_height = image_shape[0] - image_width = image_shape[1] - - target_height = image_height + tf.maximum(crop_height - image_height, 0) - target_width = image_width + tf.maximum(crop_width - image_width, 0) - - # Pad image with mean pixel value. - mean_pixel = tf.reshape( - feature_extractor.mean_pixel(model_variant), [1, 1, 3]) - processed_image = preprocess_utils.pad_to_bounding_box( - processed_image, 0, 0, target_height, target_width, mean_pixel) - - if label is not None: - label = preprocess_utils.pad_to_bounding_box( - label, 0, 0, target_height, target_width, ignore_label) - - # Randomly crop the image and label. - if is_training and label is not None: - processed_image, label = preprocess_utils.random_crop( - [processed_image, label], crop_height, crop_width) - - processed_image.set_shape([crop_height, crop_width, 3]) - - if label is not None: - label.set_shape([crop_height, crop_width, 1]) - - if is_training: - # Randomly left-right flip the image and label. - processed_image, label, _ = preprocess_utils.flip_dim( - [processed_image, label], _PROB_OF_FLIP, dim=1) - - return original_image, processed_image, label diff --git a/research/deeplab/local_test.sh b/research/deeplab/local_test.sh deleted file mode 100644 index c9ad75f6928..00000000000 --- a/research/deeplab/local_test.sh +++ /dev/null @@ -1,147 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -# -# This script is used to run local test on PASCAL VOC 2012. Users could also -# modify from this script for their use case. -# -# Usage: -# # From the tensorflow/models/research/deeplab directory. -# bash ./local_test.sh -# -# - -# Exit immediately if a command exits with a non-zero status. -set -e - -# Move one-level up to tensorflow/models/research directory. -cd .. - -# Update PYTHONPATH. -export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim - -# Set up the working environment. -CURRENT_DIR=$(pwd) -WORK_DIR="${CURRENT_DIR}/deeplab" - -# Run model_test first to make sure the PYTHONPATH is correctly set. -python "${WORK_DIR}"/model_test.py - -# Go to datasets folder and download PASCAL VOC 2012 segmentation dataset. -DATASET_DIR="datasets" -cd "${WORK_DIR}/${DATASET_DIR}" -bash download_and_convert_voc2012.sh - -# Go back to original directory. -cd "${CURRENT_DIR}" - -# Set up the working directories. -PASCAL_FOLDER="pascal_voc_seg" -EXP_FOLDER="exp/train_on_trainval_set" -INIT_FOLDER="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/init_models" -TRAIN_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train" -EVAL_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/eval" -VIS_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/vis" -EXPORT_DIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/export" -mkdir -p "${INIT_FOLDER}" -mkdir -p "${TRAIN_LOGDIR}" -mkdir -p "${EVAL_LOGDIR}" -mkdir -p "${VIS_LOGDIR}" -mkdir -p "${EXPORT_DIR}" - -# Copy locally the trained checkpoint as the initial checkpoint. -TF_INIT_ROOT="http://download.tensorflow.org/models" -TF_INIT_CKPT="deeplabv3_pascal_train_aug_2018_01_04.tar.gz" -cd "${INIT_FOLDER}" -wget -nd -c "${TF_INIT_ROOT}/${TF_INIT_CKPT}" -tar -xf "${TF_INIT_CKPT}" -cd "${CURRENT_DIR}" - -PASCAL_DATASET="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/tfrecord" - -# Train 10 iterations. -NUM_ITERATIONS=10 -python "${WORK_DIR}"/train.py \ - --logtostderr \ - --train_split="trainval" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --train_crop_size="513,513" \ - --train_batch_size=4 \ - --training_number_of_steps="${NUM_ITERATIONS}" \ - --fine_tune_batch_norm=true \ - --tf_initial_checkpoint="${INIT_FOLDER}/deeplabv3_pascal_train_aug/model.ckpt" \ - --train_logdir="${TRAIN_LOGDIR}" \ - --dataset_dir="${PASCAL_DATASET}" - -# Run evaluation. This performs eval over the full val split (1449 images) and -# will take a while. -# Using the provided checkpoint, one should expect mIOU=82.20%. -python "${WORK_DIR}"/eval.py \ - --logtostderr \ - --eval_split="val" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --eval_crop_size="513,513" \ - --checkpoint_dir="${TRAIN_LOGDIR}" \ - --eval_logdir="${EVAL_LOGDIR}" \ - --dataset_dir="${PASCAL_DATASET}" \ - --max_number_of_evaluations=1 - -# Visualize the results. -python "${WORK_DIR}"/vis.py \ - --logtostderr \ - --vis_split="val" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --vis_crop_size="513,513" \ - --checkpoint_dir="${TRAIN_LOGDIR}" \ - --vis_logdir="${VIS_LOGDIR}" \ - --dataset_dir="${PASCAL_DATASET}" \ - --max_number_of_iterations=1 - -# Export the trained checkpoint. -CKPT_PATH="${TRAIN_LOGDIR}/model.ckpt-${NUM_ITERATIONS}" -EXPORT_PATH="${EXPORT_DIR}/frozen_inference_graph.pb" - -python "${WORK_DIR}"/export_model.py \ - --logtostderr \ - --checkpoint_path="${CKPT_PATH}" \ - --export_path="${EXPORT_PATH}" \ - --model_variant="xception_65" \ - --atrous_rates=6 \ - --atrous_rates=12 \ - --atrous_rates=18 \ - --output_stride=16 \ - --decoder_output_stride=4 \ - --num_classes=21 \ - --crop_size=513 \ - --crop_size=513 \ - --inference_scales=1.0 - -# Run inference with the exported checkpoint. -# Please refer to the provided deeplab_demo.ipynb for an example. diff --git a/research/deeplab/local_test_mobilenetv2.sh b/research/deeplab/local_test_mobilenetv2.sh deleted file mode 100644 index c38646fdf6c..00000000000 --- a/research/deeplab/local_test_mobilenetv2.sh +++ /dev/null @@ -1,129 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -# -# This script is used to run local test on PASCAL VOC 2012 using MobileNet-v2. -# Users could also modify from this script for their use case. -# -# Usage: -# # From the tensorflow/models/research/deeplab directory. -# sh ./local_test_mobilenetv2.sh -# -# - -# Exit immediately if a command exits with a non-zero status. -set -e - -# Move one-level up to tensorflow/models/research directory. -cd .. - -# Update PYTHONPATH. -export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim - -# Set up the working environment. -CURRENT_DIR=$(pwd) -WORK_DIR="${CURRENT_DIR}/deeplab" - -# Run model_test first to make sure the PYTHONPATH is correctly set. -python "${WORK_DIR}"/model_test.py -v - -# Go to datasets folder and download PASCAL VOC 2012 segmentation dataset. -DATASET_DIR="datasets" -cd "${WORK_DIR}/${DATASET_DIR}" -sh download_and_convert_voc2012.sh - -# Go back to original directory. -cd "${CURRENT_DIR}" - -# Set up the working directories. -PASCAL_FOLDER="pascal_voc_seg" -EXP_FOLDER="exp/train_on_trainval_set_mobilenetv2" -INIT_FOLDER="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/init_models" -TRAIN_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/train" -EVAL_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/eval" -VIS_LOGDIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/vis" -EXPORT_DIR="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/${EXP_FOLDER}/export" -mkdir -p "${INIT_FOLDER}" -mkdir -p "${TRAIN_LOGDIR}" -mkdir -p "${EVAL_LOGDIR}" -mkdir -p "${VIS_LOGDIR}" -mkdir -p "${EXPORT_DIR}" - -# Copy locally the trained checkpoint as the initial checkpoint. -TF_INIT_ROOT="http://download.tensorflow.org/models" -CKPT_NAME="deeplabv3_mnv2_pascal_train_aug" -TF_INIT_CKPT="${CKPT_NAME}_2018_01_29.tar.gz" -cd "${INIT_FOLDER}" -wget -nd -c "${TF_INIT_ROOT}/${TF_INIT_CKPT}" -tar -xf "${TF_INIT_CKPT}" -cd "${CURRENT_DIR}" - -PASCAL_DATASET="${WORK_DIR}/${DATASET_DIR}/${PASCAL_FOLDER}/tfrecord" - -# Train 10 iterations. -NUM_ITERATIONS=10 -python "${WORK_DIR}"/train.py \ - --logtostderr \ - --train_split="trainval" \ - --model_variant="mobilenet_v2" \ - --output_stride=16 \ - --train_crop_size="513,513" \ - --train_batch_size=4 \ - --training_number_of_steps="${NUM_ITERATIONS}" \ - --fine_tune_batch_norm=true \ - --tf_initial_checkpoint="${INIT_FOLDER}/${CKPT_NAME}/model.ckpt-30000" \ - --train_logdir="${TRAIN_LOGDIR}" \ - --dataset_dir="${PASCAL_DATASET}" - -# Run evaluation. This performs eval over the full val split (1449 images) and -# will take a while. -# Using the provided checkpoint, one should expect mIOU=75.34%. -python "${WORK_DIR}"/eval.py \ - --logtostderr \ - --eval_split="val" \ - --model_variant="mobilenet_v2" \ - --eval_crop_size="513,513" \ - --checkpoint_dir="${TRAIN_LOGDIR}" \ - --eval_logdir="${EVAL_LOGDIR}" \ - --dataset_dir="${PASCAL_DATASET}" \ - --max_number_of_evaluations=1 - -# Visualize the results. -python "${WORK_DIR}"/vis.py \ - --logtostderr \ - --vis_split="val" \ - --model_variant="mobilenet_v2" \ - --vis_crop_size="513,513" \ - --checkpoint_dir="${TRAIN_LOGDIR}" \ - --vis_logdir="${VIS_LOGDIR}" \ - --dataset_dir="${PASCAL_DATASET}" \ - --max_number_of_iterations=1 - -# Export the trained checkpoint. -CKPT_PATH="${TRAIN_LOGDIR}/model.ckpt-${NUM_ITERATIONS}" -EXPORT_PATH="${EXPORT_DIR}/frozen_inference_graph.pb" - -python "${WORK_DIR}"/export_model.py \ - --logtostderr \ - --checkpoint_path="${CKPT_PATH}" \ - --export_path="${EXPORT_PATH}" \ - --model_variant="mobilenet_v2" \ - --num_classes=21 \ - --crop_size=513 \ - --crop_size=513 \ - --inference_scales=1.0 - -# Run inference with the exported checkpoint. -# Please refer to the provided deeplab_demo.ipynb for an example. diff --git a/research/deeplab/model.py b/research/deeplab/model.py deleted file mode 100644 index 311aaa1acb1..00000000000 --- a/research/deeplab/model.py +++ /dev/null @@ -1,911 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Provides DeepLab model definition and helper functions. - -DeepLab is a deep learning system for semantic image segmentation with -the following features: - -(1) Atrous convolution to explicitly control the resolution at which -feature responses are computed within Deep Convolutional Neural Networks. - -(2) Atrous spatial pyramid pooling (ASPP) to robustly segment objects at -multiple scales with filters at multiple sampling rates and effective -fields-of-views. - -(3) ASPP module augmented with image-level feature and batch normalization. - -(4) A simple yet effective decoder module to recover the object boundaries. - -See the following papers for more details: - -"Encoder-Decoder with Atrous Separable Convolution for Semantic Image -Segmentation" -Liang-Chieh Chen, Yukun Zhu, George Papandreou, Florian Schroff, Hartwig Adam. -(https://arxiv.org/abs/1802.02611) - -"Rethinking Atrous Convolution for Semantic Image Segmentation," -Liang-Chieh Chen, George Papandreou, Florian Schroff, Hartwig Adam -(https://arxiv.org/abs/1706.05587) - -"DeepLab: Semantic Image Segmentation with Deep Convolutional Nets, -Atrous Convolution, and Fully Connected CRFs", -Liang-Chieh Chen*, George Papandreou*, Iasonas Kokkinos, Kevin Murphy, -Alan L Yuille (* equal contribution) -(https://arxiv.org/abs/1606.00915) - -"Semantic Image Segmentation with Deep Convolutional Nets and Fully Connected -CRFs" -Liang-Chieh Chen*, George Papandreou*, Iasonas Kokkinos, Kevin Murphy, -Alan L. Yuille (* equal contribution) -(https://arxiv.org/abs/1412.7062) -""" -import tensorflow as tf -from tensorflow.contrib import slim as contrib_slim -from deeplab.core import dense_prediction_cell -from deeplab.core import feature_extractor -from deeplab.core import utils - -slim = contrib_slim - -LOGITS_SCOPE_NAME = 'logits' -MERGED_LOGITS_SCOPE = 'merged_logits' -IMAGE_POOLING_SCOPE = 'image_pooling' -ASPP_SCOPE = 'aspp' -CONCAT_PROJECTION_SCOPE = 'concat_projection' -DECODER_SCOPE = 'decoder' -META_ARCHITECTURE_SCOPE = 'meta_architecture' - -PROB_SUFFIX = '_prob' - -_resize_bilinear = utils.resize_bilinear -scale_dimension = utils.scale_dimension -split_separable_conv2d = utils.split_separable_conv2d - - -def get_extra_layer_scopes(last_layers_contain_logits_only=False): - """Gets the scopes for extra layers. - - Args: - last_layers_contain_logits_only: Boolean, True if only consider logits as - the last layer (i.e., exclude ASPP module, decoder module and so on) - - Returns: - A list of scopes for extra layers. - """ - if last_layers_contain_logits_only: - return [LOGITS_SCOPE_NAME] - else: - return [ - LOGITS_SCOPE_NAME, - IMAGE_POOLING_SCOPE, - ASPP_SCOPE, - CONCAT_PROJECTION_SCOPE, - DECODER_SCOPE, - META_ARCHITECTURE_SCOPE, - ] - - -def predict_labels_multi_scale(images, - model_options, - eval_scales=(1.0,), - add_flipped_images=False): - """Predicts segmentation labels. - - Args: - images: A tensor of size [batch, height, width, channels]. - model_options: A ModelOptions instance to configure models. - eval_scales: The scales to resize images for evaluation. - add_flipped_images: Add flipped images for evaluation or not. - - Returns: - A dictionary with keys specifying the output_type (e.g., semantic - prediction) and values storing Tensors representing predictions (argmax - over channels). Each prediction has size [batch, height, width]. - """ - outputs_to_predictions = { - output: [] - for output in model_options.outputs_to_num_classes - } - - for i, image_scale in enumerate(eval_scales): - with tf.variable_scope(tf.get_variable_scope(), reuse=True if i else None): - outputs_to_scales_to_logits = multi_scale_logits( - images, - model_options=model_options, - image_pyramid=[image_scale], - is_training=False, - fine_tune_batch_norm=False) - - if add_flipped_images: - with tf.variable_scope(tf.get_variable_scope(), reuse=True): - outputs_to_scales_to_logits_reversed = multi_scale_logits( - tf.reverse_v2(images, [2]), - model_options=model_options, - image_pyramid=[image_scale], - is_training=False, - fine_tune_batch_norm=False) - - for output in sorted(outputs_to_scales_to_logits): - scales_to_logits = outputs_to_scales_to_logits[output] - logits = _resize_bilinear( - scales_to_logits[MERGED_LOGITS_SCOPE], - tf.shape(images)[1:3], - scales_to_logits[MERGED_LOGITS_SCOPE].dtype) - outputs_to_predictions[output].append( - tf.expand_dims(tf.nn.softmax(logits), 4)) - - if add_flipped_images: - scales_to_logits_reversed = ( - outputs_to_scales_to_logits_reversed[output]) - logits_reversed = _resize_bilinear( - tf.reverse_v2(scales_to_logits_reversed[MERGED_LOGITS_SCOPE], [2]), - tf.shape(images)[1:3], - scales_to_logits_reversed[MERGED_LOGITS_SCOPE].dtype) - outputs_to_predictions[output].append( - tf.expand_dims(tf.nn.softmax(logits_reversed), 4)) - - for output in sorted(outputs_to_predictions): - predictions = outputs_to_predictions[output] - # Compute average prediction across different scales and flipped images. - predictions = tf.reduce_mean(tf.concat(predictions, 4), axis=4) - outputs_to_predictions[output] = tf.argmax(predictions, 3) - outputs_to_predictions[output + PROB_SUFFIX] = tf.nn.softmax(predictions) - - return outputs_to_predictions - - -def predict_labels(images, model_options, image_pyramid=None): - """Predicts segmentation labels. - - Args: - images: A tensor of size [batch, height, width, channels]. - model_options: A ModelOptions instance to configure models. - image_pyramid: Input image scales for multi-scale feature extraction. - - Returns: - A dictionary with keys specifying the output_type (e.g., semantic - prediction) and values storing Tensors representing predictions (argmax - over channels). Each prediction has size [batch, height, width]. - """ - outputs_to_scales_to_logits = multi_scale_logits( - images, - model_options=model_options, - image_pyramid=image_pyramid, - is_training=False, - fine_tune_batch_norm=False) - - predictions = {} - for output in sorted(outputs_to_scales_to_logits): - scales_to_logits = outputs_to_scales_to_logits[output] - logits = scales_to_logits[MERGED_LOGITS_SCOPE] - # There are two ways to obtain the final prediction results: (1) bilinear - # upsampling the logits followed by argmax, or (2) argmax followed by - # nearest neighbor upsampling. The second option may introduce the "blocking - # effect" but is computationally efficient. - if model_options.prediction_with_upsampled_logits: - logits = _resize_bilinear(logits, - tf.shape(images)[1:3], - scales_to_logits[MERGED_LOGITS_SCOPE].dtype) - predictions[output] = tf.argmax(logits, 3) - predictions[output + PROB_SUFFIX] = tf.nn.softmax(logits) - else: - argmax_results = tf.argmax(logits, 3) - argmax_results = tf.image.resize_nearest_neighbor( - tf.expand_dims(argmax_results, 3), - tf.shape(images)[1:3], - align_corners=True, - name='resize_prediction') - predictions[output] = tf.squeeze(argmax_results, 3) - predictions[output + PROB_SUFFIX] = tf.image.resize_bilinear( - tf.nn.softmax(logits), - tf.shape(images)[1:3], - align_corners=True, - name='resize_prob') - return predictions - - -def multi_scale_logits(images, - model_options, - image_pyramid, - weight_decay=0.0001, - is_training=False, - fine_tune_batch_norm=False, - nas_training_hyper_parameters=None): - """Gets the logits for multi-scale inputs. - - The returned logits are all downsampled (due to max-pooling layers) - for both training and evaluation. - - Args: - images: A tensor of size [batch, height, width, channels]. - model_options: A ModelOptions instance to configure models. - image_pyramid: Input image scales for multi-scale feature extraction. - weight_decay: The weight decay for model variables. - is_training: Is training or not. - fine_tune_batch_norm: Fine-tune the batch norm parameters or not. - nas_training_hyper_parameters: A dictionary storing hyper-parameters for - training nas models. Its keys are: - - `drop_path_keep_prob`: Probability to keep each path in the cell when - training. - - `total_training_steps`: Total training steps to help drop path - probability calculation. - - Returns: - outputs_to_scales_to_logits: A map of maps from output_type (e.g., - semantic prediction) to a dictionary of multi-scale logits names to - logits. For each output_type, the dictionary has keys which - correspond to the scales and values which correspond to the logits. - For example, if `scales` equals [1.0, 1.5], then the keys would - include 'merged_logits', 'logits_1.00' and 'logits_1.50'. - - Raises: - ValueError: If model_options doesn't specify crop_size and its - add_image_level_feature = True, since add_image_level_feature requires - crop_size information. - """ - # Setup default values. - if not image_pyramid: - image_pyramid = [1.0] - crop_height = ( - model_options.crop_size[0] - if model_options.crop_size else tf.shape(images)[1]) - crop_width = ( - model_options.crop_size[1] - if model_options.crop_size else tf.shape(images)[2]) - if model_options.image_pooling_crop_size: - image_pooling_crop_height = model_options.image_pooling_crop_size[0] - image_pooling_crop_width = model_options.image_pooling_crop_size[1] - - # Compute the height, width for the output logits. - if model_options.decoder_output_stride: - logits_output_stride = min(model_options.decoder_output_stride) - else: - logits_output_stride = model_options.output_stride - - logits_height = scale_dimension( - crop_height, - max(1.0, max(image_pyramid)) / logits_output_stride) - logits_width = scale_dimension( - crop_width, - max(1.0, max(image_pyramid)) / logits_output_stride) - - # Compute the logits for each scale in the image pyramid. - outputs_to_scales_to_logits = { - k: {} - for k in model_options.outputs_to_num_classes - } - - num_channels = images.get_shape().as_list()[-1] - - for image_scale in image_pyramid: - if image_scale != 1.0: - scaled_height = scale_dimension(crop_height, image_scale) - scaled_width = scale_dimension(crop_width, image_scale) - scaled_crop_size = [scaled_height, scaled_width] - scaled_images = _resize_bilinear(images, scaled_crop_size, images.dtype) - if model_options.crop_size: - scaled_images.set_shape( - [None, scaled_height, scaled_width, num_channels]) - # Adjust image_pooling_crop_size accordingly. - scaled_image_pooling_crop_size = None - if model_options.image_pooling_crop_size: - scaled_image_pooling_crop_size = [ - scale_dimension(image_pooling_crop_height, image_scale), - scale_dimension(image_pooling_crop_width, image_scale)] - else: - scaled_crop_size = model_options.crop_size - scaled_images = images - scaled_image_pooling_crop_size = model_options.image_pooling_crop_size - - updated_options = model_options._replace( - crop_size=scaled_crop_size, - image_pooling_crop_size=scaled_image_pooling_crop_size) - outputs_to_logits = _get_logits( - scaled_images, - updated_options, - weight_decay=weight_decay, - reuse=tf.AUTO_REUSE, - is_training=is_training, - fine_tune_batch_norm=fine_tune_batch_norm, - nas_training_hyper_parameters=nas_training_hyper_parameters) - - # Resize the logits to have the same dimension before merging. - for output in sorted(outputs_to_logits): - outputs_to_logits[output] = _resize_bilinear( - outputs_to_logits[output], [logits_height, logits_width], - outputs_to_logits[output].dtype) - - # Return when only one input scale. - if len(image_pyramid) == 1: - for output in sorted(model_options.outputs_to_num_classes): - outputs_to_scales_to_logits[output][ - MERGED_LOGITS_SCOPE] = outputs_to_logits[output] - return outputs_to_scales_to_logits - - # Save logits to the output map. - for output in sorted(model_options.outputs_to_num_classes): - outputs_to_scales_to_logits[output][ - 'logits_%.2f' % image_scale] = outputs_to_logits[output] - - # Merge the logits from all the multi-scale inputs. - for output in sorted(model_options.outputs_to_num_classes): - # Concatenate the multi-scale logits for each output type. - all_logits = [ - tf.expand_dims(logits, axis=4) - for logits in outputs_to_scales_to_logits[output].values() - ] - all_logits = tf.concat(all_logits, 4) - merge_fn = ( - tf.reduce_max - if model_options.merge_method == 'max' else tf.reduce_mean) - outputs_to_scales_to_logits[output][MERGED_LOGITS_SCOPE] = merge_fn( - all_logits, axis=4) - - return outputs_to_scales_to_logits - - -def extract_features(images, - model_options, - weight_decay=0.0001, - reuse=None, - is_training=False, - fine_tune_batch_norm=False, - nas_training_hyper_parameters=None): - """Extracts features by the particular model_variant. - - Args: - images: A tensor of size [batch, height, width, channels]. - model_options: A ModelOptions instance to configure models. - weight_decay: The weight decay for model variables. - reuse: Reuse the model variables or not. - is_training: Is training or not. - fine_tune_batch_norm: Fine-tune the batch norm parameters or not. - nas_training_hyper_parameters: A dictionary storing hyper-parameters for - training nas models. Its keys are: - - `drop_path_keep_prob`: Probability to keep each path in the cell when - training. - - `total_training_steps`: Total training steps to help drop path - probability calculation. - - Returns: - concat_logits: A tensor of size [batch, feature_height, feature_width, - feature_channels], where feature_height/feature_width are determined by - the images height/width and output_stride. - end_points: A dictionary from components of the network to the corresponding - activation. - """ - features, end_points = feature_extractor.extract_features( - images, - output_stride=model_options.output_stride, - multi_grid=model_options.multi_grid, - model_variant=model_options.model_variant, - depth_multiplier=model_options.depth_multiplier, - divisible_by=model_options.divisible_by, - weight_decay=weight_decay, - reuse=reuse, - is_training=is_training, - preprocessed_images_dtype=model_options.preprocessed_images_dtype, - fine_tune_batch_norm=fine_tune_batch_norm, - nas_architecture_options=model_options.nas_architecture_options, - nas_training_hyper_parameters=nas_training_hyper_parameters, - use_bounded_activation=model_options.use_bounded_activation) - - if not model_options.aspp_with_batch_norm: - return features, end_points - else: - if model_options.dense_prediction_cell_config is not None: - tf.logging.info('Using dense prediction cell config.') - dense_prediction_layer = dense_prediction_cell.DensePredictionCell( - config=model_options.dense_prediction_cell_config, - hparams={ - 'conv_rate_multiplier': 16 // model_options.output_stride, - }) - concat_logits = dense_prediction_layer.build_cell( - features, - output_stride=model_options.output_stride, - crop_size=model_options.crop_size, - image_pooling_crop_size=model_options.image_pooling_crop_size, - weight_decay=weight_decay, - reuse=reuse, - is_training=is_training, - fine_tune_batch_norm=fine_tune_batch_norm) - return concat_logits, end_points - else: - # The following codes employ the DeepLabv3 ASPP module. Note that we - # could express the ASPP module as one particular dense prediction - # cell architecture. We do not do so but leave the following codes - # for backward compatibility. - batch_norm_params = utils.get_batch_norm_params( - decay=0.9997, - epsilon=1e-5, - scale=True, - is_training=(is_training and fine_tune_batch_norm), - sync_batch_norm_method=model_options.sync_batch_norm_method) - batch_norm = utils.get_batch_norm_fn( - model_options.sync_batch_norm_method) - activation_fn = ( - tf.nn.relu6 if model_options.use_bounded_activation else tf.nn.relu) - with slim.arg_scope( - [slim.conv2d, slim.separable_conv2d], - weights_regularizer=slim.l2_regularizer(weight_decay), - activation_fn=activation_fn, - normalizer_fn=batch_norm, - padding='SAME', - stride=1, - reuse=reuse): - with slim.arg_scope([batch_norm], **batch_norm_params): - depth = model_options.aspp_convs_filters - branch_logits = [] - - if model_options.add_image_level_feature: - if model_options.crop_size is not None: - image_pooling_crop_size = model_options.image_pooling_crop_size - # If image_pooling_crop_size is not specified, use crop_size. - if image_pooling_crop_size is None: - image_pooling_crop_size = model_options.crop_size - pool_height = scale_dimension( - image_pooling_crop_size[0], - 1. / model_options.output_stride) - pool_width = scale_dimension( - image_pooling_crop_size[1], - 1. / model_options.output_stride) - image_feature = slim.avg_pool2d( - features, [pool_height, pool_width], - model_options.image_pooling_stride, padding='VALID') - resize_height = scale_dimension( - model_options.crop_size[0], - 1. / model_options.output_stride) - resize_width = scale_dimension( - model_options.crop_size[1], - 1. / model_options.output_stride) - else: - # If crop_size is None, we simply do global pooling. - pool_height = tf.shape(features)[1] - pool_width = tf.shape(features)[2] - image_feature = tf.reduce_mean( - features, axis=[1, 2], keepdims=True) - resize_height = pool_height - resize_width = pool_width - image_feature_activation_fn = tf.nn.relu - image_feature_normalizer_fn = batch_norm - if model_options.aspp_with_squeeze_and_excitation: - image_feature_activation_fn = tf.nn.sigmoid - if model_options.image_se_uses_qsigmoid: - image_feature_activation_fn = utils.q_sigmoid - image_feature_normalizer_fn = None - image_feature = slim.conv2d( - image_feature, depth, 1, - activation_fn=image_feature_activation_fn, - normalizer_fn=image_feature_normalizer_fn, - scope=IMAGE_POOLING_SCOPE) - image_feature = _resize_bilinear( - image_feature, - [resize_height, resize_width], - image_feature.dtype) - # Set shape for resize_height/resize_width if they are not Tensor. - if isinstance(resize_height, tf.Tensor): - resize_height = None - if isinstance(resize_width, tf.Tensor): - resize_width = None - image_feature.set_shape([None, resize_height, resize_width, depth]) - if not model_options.aspp_with_squeeze_and_excitation: - branch_logits.append(image_feature) - - # Employ a 1x1 convolution. - branch_logits.append(slim.conv2d(features, depth, 1, - scope=ASPP_SCOPE + str(0))) - - if model_options.atrous_rates: - # Employ 3x3 convolutions with different atrous rates. - for i, rate in enumerate(model_options.atrous_rates, 1): - scope = ASPP_SCOPE + str(i) - if model_options.aspp_with_separable_conv: - aspp_features = split_separable_conv2d( - features, - filters=depth, - rate=rate, - weight_decay=weight_decay, - scope=scope) - else: - aspp_features = slim.conv2d( - features, depth, 3, rate=rate, scope=scope) - branch_logits.append(aspp_features) - - # Merge branch logits. - concat_logits = tf.concat(branch_logits, 3) - if model_options.aspp_with_concat_projection: - concat_logits = slim.conv2d( - concat_logits, depth, 1, scope=CONCAT_PROJECTION_SCOPE) - concat_logits = slim.dropout( - concat_logits, - keep_prob=0.9, - is_training=is_training, - scope=CONCAT_PROJECTION_SCOPE + '_dropout') - if (model_options.add_image_level_feature and - model_options.aspp_with_squeeze_and_excitation): - concat_logits *= image_feature - - return concat_logits, end_points - - -def _get_logits(images, - model_options, - weight_decay=0.0001, - reuse=None, - is_training=False, - fine_tune_batch_norm=False, - nas_training_hyper_parameters=None): - """Gets the logits by atrous/image spatial pyramid pooling. - - Args: - images: A tensor of size [batch, height, width, channels]. - model_options: A ModelOptions instance to configure models. - weight_decay: The weight decay for model variables. - reuse: Reuse the model variables or not. - is_training: Is training or not. - fine_tune_batch_norm: Fine-tune the batch norm parameters or not. - nas_training_hyper_parameters: A dictionary storing hyper-parameters for - training nas models. Its keys are: - - `drop_path_keep_prob`: Probability to keep each path in the cell when - training. - - `total_training_steps`: Total training steps to help drop path - probability calculation. - - Returns: - outputs_to_logits: A map from output_type to logits. - """ - features, end_points = extract_features( - images, - model_options, - weight_decay=weight_decay, - reuse=reuse, - is_training=is_training, - fine_tune_batch_norm=fine_tune_batch_norm, - nas_training_hyper_parameters=nas_training_hyper_parameters) - - if model_options.decoder_output_stride: - crop_size = model_options.crop_size - if crop_size is None: - crop_size = [tf.shape(images)[1], tf.shape(images)[2]] - features = refine_by_decoder( - features, - end_points, - crop_size=crop_size, - decoder_output_stride=model_options.decoder_output_stride, - decoder_use_separable_conv=model_options.decoder_use_separable_conv, - decoder_use_sum_merge=model_options.decoder_use_sum_merge, - decoder_filters=model_options.decoder_filters, - decoder_output_is_logits=model_options.decoder_output_is_logits, - model_variant=model_options.model_variant, - weight_decay=weight_decay, - reuse=reuse, - is_training=is_training, - fine_tune_batch_norm=fine_tune_batch_norm, - use_bounded_activation=model_options.use_bounded_activation) - - outputs_to_logits = {} - for output in sorted(model_options.outputs_to_num_classes): - if model_options.decoder_output_is_logits: - outputs_to_logits[output] = tf.identity(features, - name=output) - else: - outputs_to_logits[output] = get_branch_logits( - features, - model_options.outputs_to_num_classes[output], - model_options.atrous_rates, - aspp_with_batch_norm=model_options.aspp_with_batch_norm, - kernel_size=model_options.logits_kernel_size, - weight_decay=weight_decay, - reuse=reuse, - scope_suffix=output) - - return outputs_to_logits - - -def refine_by_decoder(features, - end_points, - crop_size=None, - decoder_output_stride=None, - decoder_use_separable_conv=False, - decoder_use_sum_merge=False, - decoder_filters=256, - decoder_output_is_logits=False, - model_variant=None, - weight_decay=0.0001, - reuse=None, - is_training=False, - fine_tune_batch_norm=False, - use_bounded_activation=False, - sync_batch_norm_method='None'): - """Adds the decoder to obtain sharper segmentation results. - - Args: - features: A tensor of size [batch, features_height, features_width, - features_channels]. - end_points: A dictionary from components of the network to the corresponding - activation. - crop_size: A tuple [crop_height, crop_width] specifying whole patch crop - size. - decoder_output_stride: A list of integers specifying the output stride of - low-level features used in the decoder module. - decoder_use_separable_conv: Employ separable convolution for decoder or not. - decoder_use_sum_merge: Boolean, decoder uses simple sum merge or not. - decoder_filters: Integer, decoder filter size. - decoder_output_is_logits: Boolean, using decoder output as logits or not. - model_variant: Model variant for feature extraction. - weight_decay: The weight decay for model variables. - reuse: Reuse the model variables or not. - is_training: Is training or not. - fine_tune_batch_norm: Fine-tune the batch norm parameters or not. - use_bounded_activation: Whether or not to use bounded activations. Bounded - activations better lend themselves to quantized inference. - sync_batch_norm_method: String, method used to sync batch norm. Currently - only support `None` (no sync batch norm) and `tpu` (use tpu code to - sync batch norm). - - Returns: - Decoder output with size [batch, decoder_height, decoder_width, - decoder_channels]. - - Raises: - ValueError: If crop_size is None. - """ - if crop_size is None: - raise ValueError('crop_size must be provided when using decoder.') - batch_norm_params = utils.get_batch_norm_params( - decay=0.9997, - epsilon=1e-5, - scale=True, - is_training=(is_training and fine_tune_batch_norm), - sync_batch_norm_method=sync_batch_norm_method) - batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method) - decoder_depth = decoder_filters - projected_filters = 48 - if decoder_use_sum_merge: - # When using sum merge, the projected filters must be equal to decoder - # filters. - projected_filters = decoder_filters - if decoder_output_is_logits: - # Overwrite the setting when decoder output is logits. - activation_fn = None - normalizer_fn = None - conv2d_kernel = 1 - # Use original conv instead of separable conv. - decoder_use_separable_conv = False - else: - # Default setting when decoder output is not logits. - activation_fn = tf.nn.relu6 if use_bounded_activation else tf.nn.relu - normalizer_fn = batch_norm - conv2d_kernel = 3 - with slim.arg_scope( - [slim.conv2d, slim.separable_conv2d], - weights_regularizer=slim.l2_regularizer(weight_decay), - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - padding='SAME', - stride=1, - reuse=reuse): - with slim.arg_scope([batch_norm], **batch_norm_params): - with tf.variable_scope(DECODER_SCOPE, DECODER_SCOPE, [features]): - decoder_features = features - decoder_stage = 0 - scope_suffix = '' - for output_stride in decoder_output_stride: - feature_list = feature_extractor.networks_to_feature_maps[ - model_variant][ - feature_extractor.DECODER_END_POINTS][output_stride] - # If only one decoder stage, we do not change the scope name in - # order for backward compactibility. - if decoder_stage: - scope_suffix = '_{}'.format(decoder_stage) - for i, name in enumerate(feature_list): - decoder_features_list = [decoder_features] - # MobileNet and NAS variants use different naming convention. - if ('mobilenet' in model_variant or - model_variant.startswith('mnas') or - model_variant.startswith('nas')): - feature_name = name - else: - feature_name = '{}/{}'.format( - feature_extractor.name_scope[model_variant], name) - decoder_features_list.append( - slim.conv2d( - end_points[feature_name], - projected_filters, - 1, - scope='feature_projection' + str(i) + scope_suffix)) - # Determine the output size. - decoder_height = scale_dimension(crop_size[0], 1.0 / output_stride) - decoder_width = scale_dimension(crop_size[1], 1.0 / output_stride) - # Resize to decoder_height/decoder_width. - for j, feature in enumerate(decoder_features_list): - decoder_features_list[j] = _resize_bilinear( - feature, [decoder_height, decoder_width], feature.dtype) - h = (None if isinstance(decoder_height, tf.Tensor) - else decoder_height) - w = (None if isinstance(decoder_width, tf.Tensor) - else decoder_width) - decoder_features_list[j].set_shape([None, h, w, None]) - if decoder_use_sum_merge: - decoder_features = _decoder_with_sum_merge( - decoder_features_list, - decoder_depth, - conv2d_kernel=conv2d_kernel, - decoder_use_separable_conv=decoder_use_separable_conv, - weight_decay=weight_decay, - scope_suffix=scope_suffix) - else: - if not decoder_use_separable_conv: - scope_suffix = str(i) + scope_suffix - decoder_features = _decoder_with_concat_merge( - decoder_features_list, - decoder_depth, - decoder_use_separable_conv=decoder_use_separable_conv, - weight_decay=weight_decay, - scope_suffix=scope_suffix) - decoder_stage += 1 - return decoder_features - - -def _decoder_with_sum_merge(decoder_features_list, - decoder_depth, - conv2d_kernel=3, - decoder_use_separable_conv=True, - weight_decay=0.0001, - scope_suffix=''): - """Decoder with sum to merge features. - - Args: - decoder_features_list: A list of decoder features. - decoder_depth: Integer, the filters used in the convolution. - conv2d_kernel: Integer, the convolution kernel size. - decoder_use_separable_conv: Boolean, use separable conv or not. - weight_decay: Weight decay for the model variables. - scope_suffix: String, used in the scope suffix. - - Returns: - decoder features merged with sum. - - Raises: - RuntimeError: If decoder_features_list have length not equal to 2. - """ - if len(decoder_features_list) != 2: - raise RuntimeError('Expect decoder_features has length 2.') - # Only apply one convolution when decoder use sum merge. - if decoder_use_separable_conv: - decoder_features = split_separable_conv2d( - decoder_features_list[0], - filters=decoder_depth, - rate=1, - weight_decay=weight_decay, - scope='decoder_split_sep_conv0'+scope_suffix) + decoder_features_list[1] - else: - decoder_features = slim.conv2d( - decoder_features_list[0], - decoder_depth, - conv2d_kernel, - scope='decoder_conv0'+scope_suffix) + decoder_features_list[1] - return decoder_features - - -def _decoder_with_concat_merge(decoder_features_list, - decoder_depth, - decoder_use_separable_conv=True, - weight_decay=0.0001, - scope_suffix=''): - """Decoder with concatenation to merge features. - - This decoder method applies two convolutions to smooth the features obtained - by concatenating the input decoder_features_list. - - This decoder module is proposed in the DeepLabv3+ paper. - - Args: - decoder_features_list: A list of decoder features. - decoder_depth: Integer, the filters used in the convolution. - decoder_use_separable_conv: Boolean, use separable conv or not. - weight_decay: Weight decay for the model variables. - scope_suffix: String, used in the scope suffix. - - Returns: - decoder features merged with concatenation. - """ - if decoder_use_separable_conv: - decoder_features = split_separable_conv2d( - tf.concat(decoder_features_list, 3), - filters=decoder_depth, - rate=1, - weight_decay=weight_decay, - scope='decoder_conv0'+scope_suffix) - decoder_features = split_separable_conv2d( - decoder_features, - filters=decoder_depth, - rate=1, - weight_decay=weight_decay, - scope='decoder_conv1'+scope_suffix) - else: - num_convs = 2 - decoder_features = slim.repeat( - tf.concat(decoder_features_list, 3), - num_convs, - slim.conv2d, - decoder_depth, - 3, - scope='decoder_conv'+scope_suffix) - return decoder_features - - -def get_branch_logits(features, - num_classes, - atrous_rates=None, - aspp_with_batch_norm=False, - kernel_size=1, - weight_decay=0.0001, - reuse=None, - scope_suffix=''): - """Gets the logits from each model's branch. - - The underlying model is branched out in the last layer when atrous - spatial pyramid pooling is employed, and all branches are sum-merged - to form the final logits. - - Args: - features: A float tensor of shape [batch, height, width, channels]. - num_classes: Number of classes to predict. - atrous_rates: A list of atrous convolution rates for last layer. - aspp_with_batch_norm: Use batch normalization layers for ASPP. - kernel_size: Kernel size for convolution. - weight_decay: Weight decay for the model variables. - reuse: Reuse model variables or not. - scope_suffix: Scope suffix for the model variables. - - Returns: - Merged logits with shape [batch, height, width, num_classes]. - - Raises: - ValueError: Upon invalid input kernel_size value. - """ - # When using batch normalization with ASPP, ASPP has been applied before - # in extract_features, and thus we simply apply 1x1 convolution here. - if aspp_with_batch_norm or atrous_rates is None: - if kernel_size != 1: - raise ValueError('Kernel size must be 1 when atrous_rates is None or ' - 'using aspp_with_batch_norm. Gets %d.' % kernel_size) - atrous_rates = [1] - - with slim.arg_scope( - [slim.conv2d], - weights_regularizer=slim.l2_regularizer(weight_decay), - weights_initializer=tf.truncated_normal_initializer(stddev=0.01), - reuse=reuse): - with tf.variable_scope(LOGITS_SCOPE_NAME, LOGITS_SCOPE_NAME, [features]): - branch_logits = [] - for i, rate in enumerate(atrous_rates): - scope = scope_suffix - if i: - scope += '_%d' % i - - branch_logits.append( - slim.conv2d( - features, - num_classes, - kernel_size=kernel_size, - rate=rate, - activation_fn=None, - normalizer_fn=None, - scope=scope)) - - return tf.add_n(branch_logits) diff --git a/research/deeplab/model_test.py b/research/deeplab/model_test.py deleted file mode 100644 index d8413d7395d..00000000000 --- a/research/deeplab/model_test.py +++ /dev/null @@ -1,148 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for DeepLab model and some helper functions.""" - -import tensorflow as tf - -from deeplab import common -from deeplab import model - - -class DeeplabModelTest(tf.test.TestCase): - - def testWrongDeepLabVariant(self): - model_options = common.ModelOptions([])._replace( - model_variant='no_such_variant') - with self.assertRaises(ValueError): - model._get_logits(images=[], model_options=model_options) - - def testBuildDeepLabv2(self): - batch_size = 2 - crop_size = [41, 41] - - # Test with two image_pyramids. - image_pyramids = [[1], [0.5, 1]] - - # Test two model variants. - model_variants = ['xception_65', 'mobilenet_v2'] - - # Test with two output_types. - outputs_to_num_classes = {'semantic': 3, - 'direction': 2} - - expected_endpoints = [['merged_logits'], - ['merged_logits', - 'logits_0.50', - 'logits_1.00']] - expected_num_logits = [1, 3] - - for model_variant in model_variants: - model_options = common.ModelOptions(outputs_to_num_classes)._replace( - add_image_level_feature=False, - aspp_with_batch_norm=False, - aspp_with_separable_conv=False, - model_variant=model_variant) - - for i, image_pyramid in enumerate(image_pyramids): - g = tf.Graph() - with g.as_default(): - with self.test_session(graph=g): - inputs = tf.random_uniform( - (batch_size, crop_size[0], crop_size[1], 3)) - outputs_to_scales_to_logits = model.multi_scale_logits( - inputs, model_options, image_pyramid=image_pyramid) - - # Check computed results for each output type. - for output in outputs_to_num_classes: - scales_to_logits = outputs_to_scales_to_logits[output] - self.assertListEqual(sorted(scales_to_logits.keys()), - sorted(expected_endpoints[i])) - - # Expected number of logits = len(image_pyramid) + 1, since the - # last logits is merged from all the scales. - self.assertEqual(len(scales_to_logits), expected_num_logits[i]) - - def testForwardpassDeepLabv3plus(self): - crop_size = [33, 33] - outputs_to_num_classes = {'semantic': 3} - - model_options = common.ModelOptions( - outputs_to_num_classes, - crop_size, - output_stride=16 - )._replace( - add_image_level_feature=True, - aspp_with_batch_norm=True, - logits_kernel_size=1, - decoder_output_stride=[4], - model_variant='mobilenet_v2') # Employ MobileNetv2 for fast test. - - g = tf.Graph() - with g.as_default(): - with self.test_session(graph=g) as sess: - inputs = tf.random_uniform( - (1, crop_size[0], crop_size[1], 3)) - outputs_to_scales_to_logits = model.multi_scale_logits( - inputs, - model_options, - image_pyramid=[1.0]) - - sess.run(tf.global_variables_initializer()) - outputs_to_scales_to_logits = sess.run(outputs_to_scales_to_logits) - - # Check computed results for each output type. - for output in outputs_to_num_classes: - scales_to_logits = outputs_to_scales_to_logits[output] - # Expect only one output. - self.assertEqual(len(scales_to_logits), 1) - for logits in scales_to_logits.values(): - self.assertTrue(logits.any()) - - def testBuildDeepLabWithDensePredictionCell(self): - batch_size = 1 - crop_size = [33, 33] - outputs_to_num_classes = {'semantic': 2} - expected_endpoints = ['merged_logits'] - dense_prediction_cell_config = [ - {'kernel': 3, 'rate': [1, 6], 'op': 'conv', 'input': -1}, - {'kernel': 3, 'rate': [18, 15], 'op': 'conv', 'input': 0}, - ] - model_options = common.ModelOptions( - outputs_to_num_classes, - crop_size, - output_stride=16)._replace( - aspp_with_batch_norm=True, - model_variant='mobilenet_v2', - dense_prediction_cell_config=dense_prediction_cell_config) - g = tf.Graph() - with g.as_default(): - with self.test_session(graph=g): - inputs = tf.random_uniform( - (batch_size, crop_size[0], crop_size[1], 3)) - outputs_to_scales_to_model_results = model.multi_scale_logits( - inputs, - model_options, - image_pyramid=[1.0]) - for output in outputs_to_num_classes: - scales_to_model_results = outputs_to_scales_to_model_results[output] - self.assertListEqual( - list(scales_to_model_results), expected_endpoints) - self.assertEqual(len(scales_to_model_results), 1) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/testing/info.md b/research/deeplab/testing/info.md deleted file mode 100644 index b84d2adb1c5..00000000000 --- a/research/deeplab/testing/info.md +++ /dev/null @@ -1,6 +0,0 @@ -This directory contains testing data. - -# pascal_voc_seg -This folder contains data specific to pascal_voc_seg dataset. val-00000-of-00001.tfrecord contains -three randomly generated images with format defined in -tensorflow/models/research/deeplab/datasets/build_voc2012_data.py. diff --git a/research/deeplab/testing/pascal_voc_seg/val-00000-of-00001.tfrecord b/research/deeplab/testing/pascal_voc_seg/val-00000-of-00001.tfrecord deleted file mode 100644 index e81455b2e1a..00000000000 Binary files a/research/deeplab/testing/pascal_voc_seg/val-00000-of-00001.tfrecord and /dev/null differ diff --git a/research/deeplab/train.py b/research/deeplab/train.py deleted file mode 100644 index fbe060dccd4..00000000000 --- a/research/deeplab/train.py +++ /dev/null @@ -1,464 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Training script for the DeepLab model. - -See model.py for more details and usage. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import six -import tensorflow as tf -from tensorflow.contrib import quantize as contrib_quantize -from tensorflow.contrib import tfprof as contrib_tfprof -from deeplab import common -from deeplab import model -from deeplab.datasets import data_generator -from deeplab.utils import train_utils -from deployment import model_deploy - -slim = tf.contrib.slim -flags = tf.app.flags -FLAGS = flags.FLAGS - -# Settings for multi-GPUs/multi-replicas training. - -flags.DEFINE_integer('num_clones', 1, 'Number of clones to deploy.') - -flags.DEFINE_boolean('clone_on_cpu', False, 'Use CPUs to deploy clones.') - -flags.DEFINE_integer('num_replicas', 1, 'Number of worker replicas.') - -flags.DEFINE_integer('startup_delay_steps', 15, - 'Number of training steps between replicas startup.') - -flags.DEFINE_integer( - 'num_ps_tasks', 0, - 'The number of parameter servers. If the value is 0, then ' - 'the parameters are handled locally by the worker.') - -flags.DEFINE_string('master', '', 'BNS name of the tensorflow server') - -flags.DEFINE_integer('task', 0, 'The task ID.') - -# Settings for logging. - -flags.DEFINE_string('train_logdir', None, - 'Where the checkpoint and logs are stored.') - -flags.DEFINE_integer('log_steps', 10, - 'Display logging information at every log_steps.') - -flags.DEFINE_integer('save_interval_secs', 1200, - 'How often, in seconds, we save the model to disk.') - -flags.DEFINE_integer('save_summaries_secs', 600, - 'How often, in seconds, we compute the summaries.') - -flags.DEFINE_boolean( - 'save_summaries_images', False, - 'Save sample inputs, labels, and semantic predictions as ' - 'images to summary.') - -# Settings for profiling. - -flags.DEFINE_string('profile_logdir', None, - 'Where the profile files are stored.') - -# Settings for training strategy. - -flags.DEFINE_enum('optimizer', 'momentum', ['momentum', 'adam'], - 'Which optimizer to use.') - - -# Momentum optimizer flags - -flags.DEFINE_enum('learning_policy', 'poly', ['poly', 'step'], - 'Learning rate policy for training.') - -# Use 0.007 when training on PASCAL augmented training set, train_aug. When -# fine-tuning on PASCAL trainval set, use learning rate=0.0001. -flags.DEFINE_float('base_learning_rate', .0001, - 'The base learning rate for model training.') - -flags.DEFINE_float('decay_steps', 0.0, - 'Decay steps for polynomial learning rate schedule.') - -flags.DEFINE_float('end_learning_rate', 0.0, - 'End learning rate for polynomial learning rate schedule.') - -flags.DEFINE_float('learning_rate_decay_factor', 0.1, - 'The rate to decay the base learning rate.') - -flags.DEFINE_integer('learning_rate_decay_step', 2000, - 'Decay the base learning rate at a fixed step.') - -flags.DEFINE_float('learning_power', 0.9, - 'The power value used in the poly learning policy.') - -flags.DEFINE_integer('training_number_of_steps', 30000, - 'The number of steps used for training') - -flags.DEFINE_float('momentum', 0.9, 'The momentum value to use') - -# Adam optimizer flags -flags.DEFINE_float('adam_learning_rate', 0.001, - 'Learning rate for the adam optimizer.') -flags.DEFINE_float('adam_epsilon', 1e-08, 'Adam optimizer epsilon.') - -# When fine_tune_batch_norm=True, use at least batch size larger than 12 -# (batch size more than 16 is better). Otherwise, one could use smaller batch -# size and set fine_tune_batch_norm=False. -flags.DEFINE_integer('train_batch_size', 8, - 'The number of images in each batch during training.') - -# For weight_decay, use 0.00004 for MobileNet-V2 or Xcpetion model variants. -# Use 0.0001 for ResNet model variants. -flags.DEFINE_float('weight_decay', 0.00004, - 'The value of the weight decay for training.') - -flags.DEFINE_list('train_crop_size', '513,513', - 'Image crop size [height, width] during training.') - -flags.DEFINE_float( - 'last_layer_gradient_multiplier', 1.0, - 'The gradient multiplier for last layers, which is used to ' - 'boost the gradient of last layers if the value > 1.') - -flags.DEFINE_boolean('upsample_logits', True, - 'Upsample logits during training.') - -# Hyper-parameters for NAS training strategy. - -flags.DEFINE_float( - 'drop_path_keep_prob', 1.0, - 'Probability to keep each path in the NAS cell when training.') - -# Settings for fine-tuning the network. - -flags.DEFINE_string('tf_initial_checkpoint', None, - 'The initial checkpoint in tensorflow format.') - -# Set to False if one does not want to re-use the trained classifier weights. -flags.DEFINE_boolean('initialize_last_layer', True, - 'Initialize the last layer.') - -flags.DEFINE_boolean('last_layers_contain_logits_only', False, - 'Only consider logits as last layers or not.') - -flags.DEFINE_integer('slow_start_step', 0, - 'Training model with small learning rate for few steps.') - -flags.DEFINE_float('slow_start_learning_rate', 1e-4, - 'Learning rate employed during slow start.') - -# Set to True if one wants to fine-tune the batch norm parameters in DeepLabv3. -# Set to False and use small batch size to save GPU memory. -flags.DEFINE_boolean('fine_tune_batch_norm', True, - 'Fine tune the batch norm parameters or not.') - -flags.DEFINE_float('min_scale_factor', 0.5, - 'Mininum scale factor for data augmentation.') - -flags.DEFINE_float('max_scale_factor', 2., - 'Maximum scale factor for data augmentation.') - -flags.DEFINE_float('scale_factor_step_size', 0.25, - 'Scale factor step size for data augmentation.') - -# For `xception_65`, use atrous_rates = [12, 24, 36] if output_stride = 8, or -# rates = [6, 12, 18] if output_stride = 16. For `mobilenet_v2`, use None. Note -# one could use different atrous_rates/output_stride during training/evaluation. -flags.DEFINE_multi_integer('atrous_rates', None, - 'Atrous rates for atrous spatial pyramid pooling.') - -flags.DEFINE_integer('output_stride', 16, - 'The ratio of input to output spatial resolution.') - -# Hard example mining related flags. -flags.DEFINE_integer( - 'hard_example_mining_step', 0, - 'The training step in which exact hard example mining kicks off. Note we ' - 'gradually reduce the mining percent to the specified ' - 'top_k_percent_pixels. For example, if hard_example_mining_step=100K and ' - 'top_k_percent_pixels=0.25, then mining percent will gradually reduce from ' - '100% to 25% until 100K steps after which we only mine top 25% pixels.') - -flags.DEFINE_float( - 'top_k_percent_pixels', 1.0, - 'The top k percent pixels (in terms of the loss values) used to compute ' - 'loss during training. This is useful for hard pixel mining.') - -# Quantization setting. -flags.DEFINE_integer( - 'quantize_delay_step', -1, - 'Steps to start quantized training. If < 0, will not quantize model.') - -# Dataset settings. -flags.DEFINE_string('dataset', 'pascal_voc_seg', - 'Name of the segmentation dataset.') - -flags.DEFINE_string('train_split', 'train', - 'Which split of the dataset to be used for training') - -flags.DEFINE_string('dataset_dir', None, 'Where the dataset reside.') - - -def _build_deeplab(iterator, outputs_to_num_classes, ignore_label): - """Builds a clone of DeepLab. - - Args: - iterator: An iterator of type tf.data.Iterator for images and labels. - outputs_to_num_classes: A map from output type to the number of classes. For - example, for the task of semantic segmentation with 21 semantic classes, - we would have outputs_to_num_classes['semantic'] = 21. - ignore_label: Ignore label. - """ - samples = iterator.get_next() - - # Add name to input and label nodes so we can add to summary. - samples[common.IMAGE] = tf.identity(samples[common.IMAGE], name=common.IMAGE) - samples[common.LABEL] = tf.identity(samples[common.LABEL], name=common.LABEL) - - model_options = common.ModelOptions( - outputs_to_num_classes=outputs_to_num_classes, - crop_size=[int(sz) for sz in FLAGS.train_crop_size], - atrous_rates=FLAGS.atrous_rates, - output_stride=FLAGS.output_stride) - - outputs_to_scales_to_logits = model.multi_scale_logits( - samples[common.IMAGE], - model_options=model_options, - image_pyramid=FLAGS.image_pyramid, - weight_decay=FLAGS.weight_decay, - is_training=True, - fine_tune_batch_norm=FLAGS.fine_tune_batch_norm, - nas_training_hyper_parameters={ - 'drop_path_keep_prob': FLAGS.drop_path_keep_prob, - 'total_training_steps': FLAGS.training_number_of_steps, - }) - - # Add name to graph node so we can add to summary. - output_type_dict = outputs_to_scales_to_logits[common.OUTPUT_TYPE] - output_type_dict[model.MERGED_LOGITS_SCOPE] = tf.identity( - output_type_dict[model.MERGED_LOGITS_SCOPE], name=common.OUTPUT_TYPE) - - for output, num_classes in six.iteritems(outputs_to_num_classes): - train_utils.add_softmax_cross_entropy_loss_for_each_scale( - outputs_to_scales_to_logits[output], - samples[common.LABEL], - num_classes, - ignore_label, - loss_weight=model_options.label_weights, - upsample_logits=FLAGS.upsample_logits, - hard_example_mining_step=FLAGS.hard_example_mining_step, - top_k_percent_pixels=FLAGS.top_k_percent_pixels, - scope=output) - - -def main(unused_argv): - tf.logging.set_verbosity(tf.logging.INFO) - # Set up deployment (i.e., multi-GPUs and/or multi-replicas). - config = model_deploy.DeploymentConfig( - num_clones=FLAGS.num_clones, - clone_on_cpu=FLAGS.clone_on_cpu, - replica_id=FLAGS.task, - num_replicas=FLAGS.num_replicas, - num_ps_tasks=FLAGS.num_ps_tasks) - - # Split the batch across GPUs. - assert FLAGS.train_batch_size % config.num_clones == 0, ( - 'Training batch size not divisble by number of clones (GPUs).') - - clone_batch_size = FLAGS.train_batch_size // config.num_clones - - tf.gfile.MakeDirs(FLAGS.train_logdir) - tf.logging.info('Training on %s set', FLAGS.train_split) - - with tf.Graph().as_default() as graph: - with tf.device(config.inputs_device()): - dataset = data_generator.Dataset( - dataset_name=FLAGS.dataset, - split_name=FLAGS.train_split, - dataset_dir=FLAGS.dataset_dir, - batch_size=clone_batch_size, - crop_size=[int(sz) for sz in FLAGS.train_crop_size], - min_resize_value=FLAGS.min_resize_value, - max_resize_value=FLAGS.max_resize_value, - resize_factor=FLAGS.resize_factor, - min_scale_factor=FLAGS.min_scale_factor, - max_scale_factor=FLAGS.max_scale_factor, - scale_factor_step_size=FLAGS.scale_factor_step_size, - model_variant=FLAGS.model_variant, - num_readers=4, - is_training=True, - should_shuffle=True, - should_repeat=True) - - # Create the global step on the device storing the variables. - with tf.device(config.variables_device()): - global_step = tf.train.get_or_create_global_step() - - # Define the model and create clones. - model_fn = _build_deeplab - model_args = (dataset.get_one_shot_iterator(), { - common.OUTPUT_TYPE: dataset.num_of_classes - }, dataset.ignore_label) - clones = model_deploy.create_clones(config, model_fn, args=model_args) - - # Gather update_ops from the first clone. These contain, for example, - # the updates for the batch_norm variables created by model_fn. - first_clone_scope = config.clone_scope(0) - update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) - - # Gather initial summaries. - summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) - - # Add summaries for model variables. - for model_var in tf.model_variables(): - summaries.add(tf.summary.histogram(model_var.op.name, model_var)) - - # Add summaries for images, labels, semantic predictions - if FLAGS.save_summaries_images: - summary_image = graph.get_tensor_by_name( - ('%s/%s:0' % (first_clone_scope, common.IMAGE)).strip('/')) - summaries.add( - tf.summary.image('samples/%s' % common.IMAGE, summary_image)) - - first_clone_label = graph.get_tensor_by_name( - ('%s/%s:0' % (first_clone_scope, common.LABEL)).strip('/')) - # Scale up summary image pixel values for better visualization. - pixel_scaling = max(1, 255 // dataset.num_of_classes) - summary_label = tf.cast(first_clone_label * pixel_scaling, tf.uint8) - summaries.add( - tf.summary.image('samples/%s' % common.LABEL, summary_label)) - - first_clone_output = graph.get_tensor_by_name( - ('%s/%s:0' % (first_clone_scope, common.OUTPUT_TYPE)).strip('/')) - predictions = tf.expand_dims(tf.argmax(first_clone_output, 3), -1) - - summary_predictions = tf.cast(predictions * pixel_scaling, tf.uint8) - summaries.add( - tf.summary.image( - 'samples/%s' % common.OUTPUT_TYPE, summary_predictions)) - - # Add summaries for losses. - for loss in tf.get_collection(tf.GraphKeys.LOSSES, first_clone_scope): - summaries.add(tf.summary.scalar('losses/%s' % loss.op.name, loss)) - - # Build the optimizer based on the device specification. - with tf.device(config.optimizer_device()): - learning_rate = train_utils.get_model_learning_rate( - FLAGS.learning_policy, - FLAGS.base_learning_rate, - FLAGS.learning_rate_decay_step, - FLAGS.learning_rate_decay_factor, - FLAGS.training_number_of_steps, - FLAGS.learning_power, - FLAGS.slow_start_step, - FLAGS.slow_start_learning_rate, - decay_steps=FLAGS.decay_steps, - end_learning_rate=FLAGS.end_learning_rate) - - summaries.add(tf.summary.scalar('learning_rate', learning_rate)) - - if FLAGS.optimizer == 'momentum': - optimizer = tf.train.MomentumOptimizer(learning_rate, FLAGS.momentum) - elif FLAGS.optimizer == 'adam': - optimizer = tf.train.AdamOptimizer( - learning_rate=FLAGS.adam_learning_rate, epsilon=FLAGS.adam_epsilon) - else: - raise ValueError('Unknown optimizer') - - if FLAGS.quantize_delay_step >= 0: - if FLAGS.num_clones > 1: - raise ValueError('Quantization doesn\'t support multi-clone yet.') - contrib_quantize.create_training_graph( - quant_delay=FLAGS.quantize_delay_step) - - startup_delay_steps = FLAGS.task * FLAGS.startup_delay_steps - - with tf.device(config.variables_device()): - total_loss, grads_and_vars = model_deploy.optimize_clones( - clones, optimizer) - total_loss = tf.check_numerics(total_loss, 'Loss is inf or nan.') - summaries.add(tf.summary.scalar('total_loss', total_loss)) - - # Modify the gradients for biases and last layer variables. - last_layers = model.get_extra_layer_scopes( - FLAGS.last_layers_contain_logits_only) - grad_mult = train_utils.get_model_gradient_multipliers( - last_layers, FLAGS.last_layer_gradient_multiplier) - if grad_mult: - grads_and_vars = slim.learning.multiply_gradients( - grads_and_vars, grad_mult) - - # Create gradient update op. - grad_updates = optimizer.apply_gradients( - grads_and_vars, global_step=global_step) - update_ops.append(grad_updates) - update_op = tf.group(*update_ops) - with tf.control_dependencies([update_op]): - train_tensor = tf.identity(total_loss, name='train_op') - - # Add the summaries from the first clone. These contain the summaries - # created by model_fn and either optimize_clones() or _gather_clone_loss(). - summaries |= set( - tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) - - # Merge all summaries together. - summary_op = tf.summary.merge(list(summaries)) - - # Soft placement allows placing on CPU ops without GPU implementation. - session_config = tf.ConfigProto( - allow_soft_placement=True, log_device_placement=False) - - # Start the training. - profile_dir = FLAGS.profile_logdir - if profile_dir is not None: - tf.gfile.MakeDirs(profile_dir) - - with contrib_tfprof.ProfileContext( - enabled=profile_dir is not None, profile_dir=profile_dir): - init_fn = None - if FLAGS.tf_initial_checkpoint: - init_fn = train_utils.get_model_init_fn( - FLAGS.train_logdir, - FLAGS.tf_initial_checkpoint, - FLAGS.initialize_last_layer, - last_layers, - ignore_missing_vars=True) - - slim.learning.train( - train_tensor, - logdir=FLAGS.train_logdir, - log_every_n_steps=FLAGS.log_steps, - master=FLAGS.master, - number_of_steps=FLAGS.training_number_of_steps, - is_chief=(FLAGS.task == 0), - session_config=session_config, - startup_delay_steps=startup_delay_steps, - init_fn=init_fn, - summary_op=summary_op, - save_summaries_secs=FLAGS.save_summaries_secs, - save_interval_secs=FLAGS.save_interval_secs) - - -if __name__ == '__main__': - flags.mark_flag_as_required('train_logdir') - flags.mark_flag_as_required('dataset_dir') - tf.app.run() diff --git a/research/deeplab/utils/__init__.py b/research/deeplab/utils/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/deeplab/utils/get_dataset_colormap.py b/research/deeplab/utils/get_dataset_colormap.py deleted file mode 100644 index c0502e3b3cd..00000000000 --- a/research/deeplab/utils/get_dataset_colormap.py +++ /dev/null @@ -1,416 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Visualizes the segmentation results via specified color map. - -Visualizes the semantic segmentation results by the color map -defined by the different datasets. Supported colormaps are: - -* ADE20K (http://groups.csail.mit.edu/vision/datasets/ADE20K/). - -* Cityscapes dataset (https://www.cityscapes-dataset.com). - -* Mapillary Vistas (https://research.mapillary.com). - -* PASCAL VOC 2012 (http://host.robots.ox.ac.uk/pascal/VOC/). -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import numpy as np -from six.moves import range - -# Dataset names. -_ADE20K = 'ade20k' -_CITYSCAPES = 'cityscapes' -_MAPILLARY_VISTAS = 'mapillary_vistas' -_PASCAL = 'pascal' - -# Max number of entries in the colormap for each dataset. -_DATASET_MAX_ENTRIES = { - _ADE20K: 151, - _CITYSCAPES: 256, - _MAPILLARY_VISTAS: 66, - _PASCAL: 512, -} - - -def create_ade20k_label_colormap(): - """Creates a label colormap used in ADE20K segmentation benchmark. - - Returns: - A colormap for visualizing segmentation results. - """ - return np.asarray([ - [0, 0, 0], - [120, 120, 120], - [180, 120, 120], - [6, 230, 230], - [80, 50, 50], - [4, 200, 3], - [120, 120, 80], - [140, 140, 140], - [204, 5, 255], - [230, 230, 230], - [4, 250, 7], - [224, 5, 255], - [235, 255, 7], - [150, 5, 61], - [120, 120, 70], - [8, 255, 51], - [255, 6, 82], - [143, 255, 140], - [204, 255, 4], - [255, 51, 7], - [204, 70, 3], - [0, 102, 200], - [61, 230, 250], - [255, 6, 51], - [11, 102, 255], - [255, 7, 71], - [255, 9, 224], - [9, 7, 230], - [220, 220, 220], - [255, 9, 92], - [112, 9, 255], - [8, 255, 214], - [7, 255, 224], - [255, 184, 6], - [10, 255, 71], - [255, 41, 10], - [7, 255, 255], - [224, 255, 8], - [102, 8, 255], - [255, 61, 6], - [255, 194, 7], - [255, 122, 8], - [0, 255, 20], - [255, 8, 41], - [255, 5, 153], - [6, 51, 255], - [235, 12, 255], - [160, 150, 20], - [0, 163, 255], - [140, 140, 140], - [250, 10, 15], - [20, 255, 0], - [31, 255, 0], - [255, 31, 0], - [255, 224, 0], - [153, 255, 0], - [0, 0, 255], - [255, 71, 0], - [0, 235, 255], - [0, 173, 255], - [31, 0, 255], - [11, 200, 200], - [255, 82, 0], - [0, 255, 245], - [0, 61, 255], - [0, 255, 112], - [0, 255, 133], - [255, 0, 0], - [255, 163, 0], - [255, 102, 0], - [194, 255, 0], - [0, 143, 255], - [51, 255, 0], - [0, 82, 255], - [0, 255, 41], - [0, 255, 173], - [10, 0, 255], - [173, 255, 0], - [0, 255, 153], - [255, 92, 0], - [255, 0, 255], - [255, 0, 245], - [255, 0, 102], - [255, 173, 0], - [255, 0, 20], - [255, 184, 184], - [0, 31, 255], - [0, 255, 61], - [0, 71, 255], - [255, 0, 204], - [0, 255, 194], - [0, 255, 82], - [0, 10, 255], - [0, 112, 255], - [51, 0, 255], - [0, 194, 255], - [0, 122, 255], - [0, 255, 163], - [255, 153, 0], - [0, 255, 10], - [255, 112, 0], - [143, 255, 0], - [82, 0, 255], - [163, 255, 0], - [255, 235, 0], - [8, 184, 170], - [133, 0, 255], - [0, 255, 92], - [184, 0, 255], - [255, 0, 31], - [0, 184, 255], - [0, 214, 255], - [255, 0, 112], - [92, 255, 0], - [0, 224, 255], - [112, 224, 255], - [70, 184, 160], - [163, 0, 255], - [153, 0, 255], - [71, 255, 0], - [255, 0, 163], - [255, 204, 0], - [255, 0, 143], - [0, 255, 235], - [133, 255, 0], - [255, 0, 235], - [245, 0, 255], - [255, 0, 122], - [255, 245, 0], - [10, 190, 212], - [214, 255, 0], - [0, 204, 255], - [20, 0, 255], - [255, 255, 0], - [0, 153, 255], - [0, 41, 255], - [0, 255, 204], - [41, 0, 255], - [41, 255, 0], - [173, 0, 255], - [0, 245, 255], - [71, 0, 255], - [122, 0, 255], - [0, 255, 184], - [0, 92, 255], - [184, 255, 0], - [0, 133, 255], - [255, 214, 0], - [25, 194, 194], - [102, 255, 0], - [92, 0, 255], - ]) - - -def create_cityscapes_label_colormap(): - """Creates a label colormap used in CITYSCAPES segmentation benchmark. - - Returns: - A colormap for visualizing segmentation results. - """ - colormap = np.zeros((256, 3), dtype=np.uint8) - colormap[0] = [128, 64, 128] - colormap[1] = [244, 35, 232] - colormap[2] = [70, 70, 70] - colormap[3] = [102, 102, 156] - colormap[4] = [190, 153, 153] - colormap[5] = [153, 153, 153] - colormap[6] = [250, 170, 30] - colormap[7] = [220, 220, 0] - colormap[8] = [107, 142, 35] - colormap[9] = [152, 251, 152] - colormap[10] = [70, 130, 180] - colormap[11] = [220, 20, 60] - colormap[12] = [255, 0, 0] - colormap[13] = [0, 0, 142] - colormap[14] = [0, 0, 70] - colormap[15] = [0, 60, 100] - colormap[16] = [0, 80, 100] - colormap[17] = [0, 0, 230] - colormap[18] = [119, 11, 32] - return colormap - - -def create_mapillary_vistas_label_colormap(): - """Creates a label colormap used in Mapillary Vistas segmentation benchmark. - - Returns: - A colormap for visualizing segmentation results. - """ - return np.asarray([ - [165, 42, 42], - [0, 192, 0], - [196, 196, 196], - [190, 153, 153], - [180, 165, 180], - [102, 102, 156], - [102, 102, 156], - [128, 64, 255], - [140, 140, 200], - [170, 170, 170], - [250, 170, 160], - [96, 96, 96], - [230, 150, 140], - [128, 64, 128], - [110, 110, 110], - [244, 35, 232], - [150, 100, 100], - [70, 70, 70], - [150, 120, 90], - [220, 20, 60], - [255, 0, 0], - [255, 0, 0], - [255, 0, 0], - [200, 128, 128], - [255, 255, 255], - [64, 170, 64], - [128, 64, 64], - [70, 130, 180], - [255, 255, 255], - [152, 251, 152], - [107, 142, 35], - [0, 170, 30], - [255, 255, 128], - [250, 0, 30], - [0, 0, 0], - [220, 220, 220], - [170, 170, 170], - [222, 40, 40], - [100, 170, 30], - [40, 40, 40], - [33, 33, 33], - [170, 170, 170], - [0, 0, 142], - [170, 170, 170], - [210, 170, 100], - [153, 153, 153], - [128, 128, 128], - [0, 0, 142], - [250, 170, 30], - [192, 192, 192], - [220, 220, 0], - [180, 165, 180], - [119, 11, 32], - [0, 0, 142], - [0, 60, 100], - [0, 0, 142], - [0, 0, 90], - [0, 0, 230], - [0, 80, 100], - [128, 64, 64], - [0, 0, 110], - [0, 0, 70], - [0, 0, 192], - [32, 32, 32], - [0, 0, 0], - [0, 0, 0], - ]) - - -def create_pascal_label_colormap(): - """Creates a label colormap used in PASCAL VOC segmentation benchmark. - - Returns: - A colormap for visualizing segmentation results. - """ - colormap = np.zeros((_DATASET_MAX_ENTRIES[_PASCAL], 3), dtype=int) - ind = np.arange(_DATASET_MAX_ENTRIES[_PASCAL], dtype=int) - - for shift in reversed(list(range(8))): - for channel in range(3): - colormap[:, channel] |= bit_get(ind, channel) << shift - ind >>= 3 - - return colormap - - -def get_ade20k_name(): - return _ADE20K - - -def get_cityscapes_name(): - return _CITYSCAPES - - -def get_mapillary_vistas_name(): - return _MAPILLARY_VISTAS - - -def get_pascal_name(): - return _PASCAL - - -def bit_get(val, idx): - """Gets the bit value. - - Args: - val: Input value, int or numpy int array. - idx: Which bit of the input val. - - Returns: - The "idx"-th bit of input val. - """ - return (val >> idx) & 1 - - -def create_label_colormap(dataset=_PASCAL): - """Creates a label colormap for the specified dataset. - - Args: - dataset: The colormap used in the dataset. - - Returns: - A numpy array of the dataset colormap. - - Raises: - ValueError: If the dataset is not supported. - """ - if dataset == _ADE20K: - return create_ade20k_label_colormap() - elif dataset == _CITYSCAPES: - return create_cityscapes_label_colormap() - elif dataset == _MAPILLARY_VISTAS: - return create_mapillary_vistas_label_colormap() - elif dataset == _PASCAL: - return create_pascal_label_colormap() - else: - raise ValueError('Unsupported dataset.') - - -def label_to_color_image(label, dataset=_PASCAL): - """Adds color defined by the dataset colormap to the label. - - Args: - label: A 2D array with integer type, storing the segmentation label. - dataset: The colormap used in the dataset. - - Returns: - result: A 2D array with floating type. The element of the array - is the color indexed by the corresponding element in the input label - to the dataset color map. - - Raises: - ValueError: If label is not of rank 2 or its value is larger than color - map maximum entry. - """ - if label.ndim != 2: - raise ValueError('Expect 2-D input label. Got {}'.format(label.shape)) - - if np.max(label) >= _DATASET_MAX_ENTRIES[dataset]: - raise ValueError( - 'label value too large: {} >= {}.'.format( - np.max(label), _DATASET_MAX_ENTRIES[dataset])) - - colormap = create_label_colormap(dataset) - return colormap[label] - - -def get_dataset_colormap_max_entries(dataset): - return _DATASET_MAX_ENTRIES[dataset] diff --git a/research/deeplab/utils/get_dataset_colormap_test.py b/research/deeplab/utils/get_dataset_colormap_test.py deleted file mode 100644 index 89adb2c7391..00000000000 --- a/research/deeplab/utils/get_dataset_colormap_test.py +++ /dev/null @@ -1,97 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for get_dataset_colormap.py.""" - -import numpy as np -import tensorflow as tf - -from deeplab.utils import get_dataset_colormap - - -class VisualizationUtilTest(tf.test.TestCase): - - def testBitGet(self): - """Test that if the returned bit value is correct.""" - self.assertEqual(1, get_dataset_colormap.bit_get(9, 0)) - self.assertEqual(0, get_dataset_colormap.bit_get(9, 1)) - self.assertEqual(0, get_dataset_colormap.bit_get(9, 2)) - self.assertEqual(1, get_dataset_colormap.bit_get(9, 3)) - - def testPASCALLabelColorMapValue(self): - """Test the getd color map value.""" - colormap = get_dataset_colormap.create_pascal_label_colormap() - - # Only test a few sampled entries in the color map. - self.assertTrue(np.array_equal([128., 0., 128.], colormap[5, :])) - self.assertTrue(np.array_equal([128., 192., 128.], colormap[23, :])) - self.assertTrue(np.array_equal([128., 0., 192.], colormap[37, :])) - self.assertTrue(np.array_equal([224., 192., 192.], colormap[127, :])) - self.assertTrue(np.array_equal([192., 160., 192.], colormap[175, :])) - - def testLabelToPASCALColorImage(self): - """Test the value of the converted label value.""" - label = np.array([[0, 16, 16], [52, 7, 52]]) - expected_result = np.array([ - [[0, 0, 0], [0, 64, 0], [0, 64, 0]], - [[0, 64, 192], [128, 128, 128], [0, 64, 192]] - ]) - colored_label = get_dataset_colormap.label_to_color_image( - label, get_dataset_colormap.get_pascal_name()) - self.assertTrue(np.array_equal(expected_result, colored_label)) - - def testUnExpectedLabelValueForLabelToPASCALColorImage(self): - """Raise ValueError when input value exceeds range.""" - label = np.array([[120], [600]]) - with self.assertRaises(ValueError): - get_dataset_colormap.label_to_color_image( - label, get_dataset_colormap.get_pascal_name()) - - def testUnExpectedLabelDimensionForLabelToPASCALColorImage(self): - """Raise ValueError if input dimension is not correct.""" - label = np.array([120]) - with self.assertRaises(ValueError): - get_dataset_colormap.label_to_color_image( - label, get_dataset_colormap.get_pascal_name()) - - def testGetColormapForUnsupportedDataset(self): - with self.assertRaises(ValueError): - get_dataset_colormap.create_label_colormap('unsupported_dataset') - - def testUnExpectedLabelDimensionForLabelToADE20KColorImage(self): - label = np.array([250]) - with self.assertRaises(ValueError): - get_dataset_colormap.label_to_color_image( - label, get_dataset_colormap.get_ade20k_name()) - - def testFirstColorInADE20KColorMap(self): - label = np.array([[1, 3], [10, 20]]) - expected_result = np.array([ - [[120, 120, 120], [6, 230, 230]], - [[4, 250, 7], [204, 70, 3]] - ]) - colored_label = get_dataset_colormap.label_to_color_image( - label, get_dataset_colormap.get_ade20k_name()) - self.assertTrue(np.array_equal(colored_label, expected_result)) - - def testMapillaryVistasColorMapValue(self): - colormap = get_dataset_colormap.create_mapillary_vistas_label_colormap() - self.assertTrue(np.array_equal([190, 153, 153], colormap[3, :])) - self.assertTrue(np.array_equal([102, 102, 156], colormap[6, :])) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/deeplab/utils/save_annotation.py b/research/deeplab/utils/save_annotation.py deleted file mode 100644 index 2444df79532..00000000000 --- a/research/deeplab/utils/save_annotation.py +++ /dev/null @@ -1,66 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Saves an annotation as one png image. - -This script saves an annotation as one png image, and has the option to add -colormap to the png image for better visualization. -""" - -import numpy as np -import PIL.Image as img -import tensorflow as tf - -from deeplab.utils import get_dataset_colormap - - -def save_annotation(label, - save_dir, - filename, - add_colormap=True, - normalize_to_unit_values=False, - scale_values=False, - colormap_type=get_dataset_colormap.get_pascal_name()): - """Saves the given label to image on disk. - - Args: - label: The numpy array to be saved. The data will be converted - to uint8 and saved as png image. - save_dir: String, the directory to which the results will be saved. - filename: String, the image filename. - add_colormap: Boolean, add color map to the label or not. - normalize_to_unit_values: Boolean, normalize the input values to [0, 1]. - scale_values: Boolean, scale the input values to [0, 255] for visualization. - colormap_type: String, colormap type for visualization. - """ - # Add colormap for visualizing the prediction. - if add_colormap: - colored_label = get_dataset_colormap.label_to_color_image( - label, colormap_type) - else: - colored_label = label - if normalize_to_unit_values: - min_value = np.amin(colored_label) - max_value = np.amax(colored_label) - range_value = max_value - min_value - if range_value != 0: - colored_label = (colored_label - min_value) / range_value - - if scale_values: - colored_label = 255. * colored_label - - pil_image = img.fromarray(colored_label.astype(dtype=np.uint8)) - with tf.gfile.Open('%s/%s.png' % (save_dir, filename), mode='w') as f: - pil_image.save(f, 'PNG') diff --git a/research/deeplab/utils/train_utils.py b/research/deeplab/utils/train_utils.py deleted file mode 100644 index 14bbd6ee7e5..00000000000 --- a/research/deeplab/utils/train_utils.py +++ /dev/null @@ -1,372 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utility functions for training.""" - -import six -import tensorflow as tf -from tensorflow.contrib import framework as contrib_framework - -from deeplab.core import preprocess_utils -from deeplab.core import utils - - -def _div_maybe_zero(total_loss, num_present): - """Normalizes the total loss with the number of present pixels.""" - return tf.to_float(num_present > 0) * tf.math.divide( - total_loss, - tf.maximum(1e-5, num_present)) - - -def add_softmax_cross_entropy_loss_for_each_scale(scales_to_logits, - labels, - num_classes, - ignore_label, - loss_weight=1.0, - upsample_logits=True, - hard_example_mining_step=0, - top_k_percent_pixels=1.0, - gt_is_matting_map=False, - scope=None): - """Adds softmax cross entropy loss for logits of each scale. - - Args: - scales_to_logits: A map from logits names for different scales to logits. - The logits have shape [batch, logits_height, logits_width, num_classes]. - labels: Groundtruth labels with shape [batch, image_height, image_width, 1]. - num_classes: Integer, number of target classes. - ignore_label: Integer, label to ignore. - loss_weight: A float or a list of loss weights. If it is a float, it means - all the labels have the same weight. If it is a list of weights, then each - element in the list represents the weight for the label of its index, for - example, loss_weight = [0.1, 0.5] means the weight for label 0 is 0.1 and - the weight for label 1 is 0.5. - upsample_logits: Boolean, upsample logits or not. - hard_example_mining_step: An integer, the training step in which the hard - exampling mining kicks off. Note that we gradually reduce the mining - percent to the top_k_percent_pixels. For example, if - hard_example_mining_step = 100K and top_k_percent_pixels = 0.25, then - mining percent will gradually reduce from 100% to 25% until 100K steps - after which we only mine top 25% pixels. - top_k_percent_pixels: A float, the value lies in [0.0, 1.0]. When its value - < 1.0, only compute the loss for the top k percent pixels (e.g., the top - 20% pixels). This is useful for hard pixel mining. - gt_is_matting_map: If true, the groundtruth is a matting map of confidence - score. If false, the groundtruth is an integer valued class mask. - scope: String, the scope for the loss. - - Raises: - ValueError: Label or logits is None, or groundtruth is matting map while - label is not floating value. - """ - if labels is None: - raise ValueError('No label for softmax cross entropy loss.') - - # If input groundtruth is a matting map of confidence, check if the input - # labels are floating point values. - if gt_is_matting_map and not labels.dtype.is_floating: - raise ValueError('Labels must be floats if groundtruth is a matting map.') - - for scale, logits in six.iteritems(scales_to_logits): - loss_scope = None - if scope: - loss_scope = '%s_%s' % (scope, scale) - - if upsample_logits: - # Label is not downsampled, and instead we upsample logits. - logits = tf.image.resize_bilinear( - logits, - preprocess_utils.resolve_shape(labels, 4)[1:3], - align_corners=True) - scaled_labels = labels - else: - # Label is downsampled to the same size as logits. - # When gt_is_matting_map = true, label downsampling with nearest neighbor - # method may introduce artifacts. However, to avoid ignore_label from - # being interpolated with other labels, we still perform nearest neighbor - # interpolation. - # TODO(huizhongc): Change to bilinear interpolation by processing padded - # and non-padded label separately. - if gt_is_matting_map: - tf.logging.warning( - 'Label downsampling with nearest neighbor may introduce artifacts.') - - scaled_labels = tf.image.resize_nearest_neighbor( - labels, - preprocess_utils.resolve_shape(logits, 4)[1:3], - align_corners=True) - - scaled_labels = tf.reshape(scaled_labels, shape=[-1]) - weights = utils.get_label_weight_mask( - scaled_labels, ignore_label, num_classes, label_weights=loss_weight) - # Dimension of keep_mask is equal to the total number of pixels. - keep_mask = tf.cast( - tf.not_equal(scaled_labels, ignore_label), dtype=tf.float32) - - train_labels = None - logits = tf.reshape(logits, shape=[-1, num_classes]) - - if gt_is_matting_map: - # When the groundtruth is integer label mask, we can assign class - # dependent label weights to the loss. When the groundtruth is image - # matting confidence, we do not apply class-dependent label weight (i.e., - # label_weight = 1.0). - if loss_weight != 1.0: - raise ValueError( - 'loss_weight must equal to 1 if groundtruth is matting map.') - - # Assign label value 0 to ignore pixels. The exact label value of ignore - # pixel does not matter, because those ignore_value pixel losses will be - # multiplied to 0 weight. - train_labels = scaled_labels * keep_mask - - train_labels = tf.expand_dims(train_labels, 1) - train_labels = tf.concat([1 - train_labels, train_labels], axis=1) - else: - train_labels = tf.one_hot( - scaled_labels, num_classes, on_value=1.0, off_value=0.0) - - default_loss_scope = ('softmax_all_pixel_loss' - if top_k_percent_pixels == 1.0 else - 'softmax_hard_example_mining') - with tf.name_scope(loss_scope, default_loss_scope, - [logits, train_labels, weights]): - # Compute the loss for all pixels. - pixel_losses = tf.nn.softmax_cross_entropy_with_logits_v2( - labels=tf.stop_gradient( - train_labels, name='train_labels_stop_gradient'), - logits=logits, - name='pixel_losses') - weighted_pixel_losses = tf.multiply(pixel_losses, weights) - - if top_k_percent_pixels == 1.0: - total_loss = tf.reduce_sum(weighted_pixel_losses) - num_present = tf.reduce_sum(keep_mask) - loss = _div_maybe_zero(total_loss, num_present) - tf.losses.add_loss(loss) - else: - num_pixels = tf.to_float(tf.shape(logits)[0]) - # Compute the top_k_percent pixels based on current training step. - if hard_example_mining_step == 0: - # Directly focus on the top_k pixels. - top_k_pixels = tf.to_int32(top_k_percent_pixels * num_pixels) - else: - # Gradually reduce the mining percent to top_k_percent_pixels. - global_step = tf.to_float(tf.train.get_or_create_global_step()) - ratio = tf.minimum(1.0, global_step / hard_example_mining_step) - top_k_pixels = tf.to_int32( - (ratio * top_k_percent_pixels + (1.0 - ratio)) * num_pixels) - top_k_losses, _ = tf.nn.top_k(weighted_pixel_losses, - k=top_k_pixels, - sorted=True, - name='top_k_percent_pixels') - total_loss = tf.reduce_sum(top_k_losses) - num_present = tf.reduce_sum( - tf.to_float(tf.not_equal(top_k_losses, 0.0))) - loss = _div_maybe_zero(total_loss, num_present) - tf.losses.add_loss(loss) - - -def get_model_init_fn(train_logdir, - tf_initial_checkpoint, - initialize_last_layer, - last_layers, - ignore_missing_vars=False): - """Gets the function initializing model variables from a checkpoint. - - Args: - train_logdir: Log directory for training. - tf_initial_checkpoint: TensorFlow checkpoint for initialization. - initialize_last_layer: Initialize last layer or not. - last_layers: Last layers of the model. - ignore_missing_vars: Ignore missing variables in the checkpoint. - - Returns: - Initialization function. - """ - if tf_initial_checkpoint is None: - tf.logging.info('Not initializing the model from a checkpoint.') - return None - - if tf.train.latest_checkpoint(train_logdir): - tf.logging.info('Ignoring initialization; other checkpoint exists') - return None - - tf.logging.info('Initializing model from path: %s', tf_initial_checkpoint) - - # Variables that will not be restored. - exclude_list = ['global_step'] - if not initialize_last_layer: - exclude_list.extend(last_layers) - - variables_to_restore = contrib_framework.get_variables_to_restore( - exclude=exclude_list) - - if variables_to_restore: - init_op, init_feed_dict = contrib_framework.assign_from_checkpoint( - tf_initial_checkpoint, - variables_to_restore, - ignore_missing_vars=ignore_missing_vars) - global_step = tf.train.get_or_create_global_step() - - def restore_fn(sess): - sess.run(init_op, init_feed_dict) - sess.run([global_step]) - - return restore_fn - - return None - - -def get_model_gradient_multipliers(last_layers, last_layer_gradient_multiplier): - """Gets the gradient multipliers. - - The gradient multipliers will adjust the learning rates for model - variables. For the task of semantic segmentation, the models are - usually fine-tuned from the models trained on the task of image - classification. To fine-tune the models, we usually set larger (e.g., - 10 times larger) learning rate for the parameters of last layer. - - Args: - last_layers: Scopes of last layers. - last_layer_gradient_multiplier: The gradient multiplier for last layers. - - Returns: - The gradient multiplier map with variables as key, and multipliers as value. - """ - gradient_multipliers = {} - - for var in tf.model_variables(): - # Double the learning rate for biases. - if 'biases' in var.op.name: - gradient_multipliers[var.op.name] = 2. - - # Use larger learning rate for last layer variables. - for layer in last_layers: - if layer in var.op.name and 'biases' in var.op.name: - gradient_multipliers[var.op.name] = 2 * last_layer_gradient_multiplier - break - elif layer in var.op.name: - gradient_multipliers[var.op.name] = last_layer_gradient_multiplier - break - - return gradient_multipliers - - -def get_model_learning_rate(learning_policy, - base_learning_rate, - learning_rate_decay_step, - learning_rate_decay_factor, - training_number_of_steps, - learning_power, - slow_start_step, - slow_start_learning_rate, - slow_start_burnin_type='none', - decay_steps=0.0, - end_learning_rate=0.0, - boundaries=None, - boundary_learning_rates=None): - """Gets model's learning rate. - - Computes the model's learning rate for different learning policy. - Right now, only "step" and "poly" are supported. - (1) The learning policy for "step" is computed as follows: - current_learning_rate = base_learning_rate * - learning_rate_decay_factor ^ (global_step / learning_rate_decay_step) - See tf.train.exponential_decay for details. - (2) The learning policy for "poly" is computed as follows: - current_learning_rate = base_learning_rate * - (1 - global_step / training_number_of_steps) ^ learning_power - - Args: - learning_policy: Learning rate policy for training. - base_learning_rate: The base learning rate for model training. - learning_rate_decay_step: Decay the base learning rate at a fixed step. - learning_rate_decay_factor: The rate to decay the base learning rate. - training_number_of_steps: Number of steps for training. - learning_power: Power used for 'poly' learning policy. - slow_start_step: Training model with small learning rate for the first - few steps. - slow_start_learning_rate: The learning rate employed during slow start. - slow_start_burnin_type: The burnin type for the slow start stage. Can be - `none` which means no burnin or `linear` which means the learning rate - increases linearly from slow_start_learning_rate and reaches - base_learning_rate after slow_start_steps. - decay_steps: Float, `decay_steps` for polynomial learning rate. - end_learning_rate: Float, `end_learning_rate` for polynomial learning rate. - boundaries: A list of `Tensor`s or `int`s or `float`s with strictly - increasing entries. - boundary_learning_rates: A list of `Tensor`s or `float`s or `int`s that - specifies the values for the intervals defined by `boundaries`. It should - have one more element than `boundaries`, and all elements should have the - same type. - - Returns: - Learning rate for the specified learning policy. - - Raises: - ValueError: If learning policy or slow start burnin type is not recognized. - ValueError: If `boundaries` and `boundary_learning_rates` are not set for - multi_steps learning rate decay. - """ - global_step = tf.train.get_or_create_global_step() - adjusted_global_step = tf.maximum(global_step - slow_start_step, 0) - if decay_steps == 0.0: - tf.logging.info('Setting decay_steps to total training steps.') - decay_steps = training_number_of_steps - slow_start_step - if learning_policy == 'step': - learning_rate = tf.train.exponential_decay( - base_learning_rate, - adjusted_global_step, - learning_rate_decay_step, - learning_rate_decay_factor, - staircase=True) - elif learning_policy == 'poly': - learning_rate = tf.train.polynomial_decay( - base_learning_rate, - adjusted_global_step, - decay_steps=decay_steps, - end_learning_rate=end_learning_rate, - power=learning_power) - elif learning_policy == 'cosine': - learning_rate = tf.train.cosine_decay( - base_learning_rate, - adjusted_global_step, - training_number_of_steps - slow_start_step) - elif learning_policy == 'multi_steps': - if boundaries is None or boundary_learning_rates is None: - raise ValueError('Must set `boundaries` and `boundary_learning_rates` ' - 'for multi_steps learning rate decay.') - learning_rate = tf.train.piecewise_constant_decay( - adjusted_global_step, - boundaries, - boundary_learning_rates) - else: - raise ValueError('Unknown learning policy.') - - adjusted_slow_start_learning_rate = slow_start_learning_rate - if slow_start_burnin_type == 'linear': - # Do linear burnin. Increase linearly from slow_start_learning_rate and - # reach base_learning_rate after (global_step >= slow_start_steps). - adjusted_slow_start_learning_rate = ( - slow_start_learning_rate + - (base_learning_rate - slow_start_learning_rate) * - tf.to_float(global_step) / slow_start_step) - elif slow_start_burnin_type != 'none': - raise ValueError('Unknown burnin type.') - - # Employ small learning rate at the first few steps for warm start. - return tf.where(global_step < slow_start_step, - adjusted_slow_start_learning_rate, learning_rate) diff --git a/research/deeplab/vis.py b/research/deeplab/vis.py deleted file mode 100644 index 20808d37bf2..00000000000 --- a/research/deeplab/vis.py +++ /dev/null @@ -1,327 +0,0 @@ -# Lint as: python2, python3 -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Segmentation results visualization on a given set of images. - -See model.py for more details and usage. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import os.path -import time -import numpy as np -from six.moves import range -import tensorflow as tf -from tensorflow.contrib import quantize as contrib_quantize -from tensorflow.contrib import training as contrib_training -from deeplab import common -from deeplab import model -from deeplab.datasets import data_generator -from deeplab.utils import save_annotation - -flags = tf.app.flags - -FLAGS = flags.FLAGS - -flags.DEFINE_string('master', '', 'BNS name of the tensorflow server') - -# Settings for log directories. - -flags.DEFINE_string('vis_logdir', None, 'Where to write the event logs.') - -flags.DEFINE_string('checkpoint_dir', None, 'Directory of model checkpoints.') - -# Settings for visualizing the model. - -flags.DEFINE_integer('vis_batch_size', 1, - 'The number of images in each batch during evaluation.') - -flags.DEFINE_list('vis_crop_size', '513,513', - 'Crop size [height, width] for visualization.') - -flags.DEFINE_integer('eval_interval_secs', 60 * 5, - 'How often (in seconds) to run evaluation.') - -# For `xception_65`, use atrous_rates = [12, 24, 36] if output_stride = 8, or -# rates = [6, 12, 18] if output_stride = 16. For `mobilenet_v2`, use None. Note -# one could use different atrous_rates/output_stride during training/evaluation. -flags.DEFINE_multi_integer('atrous_rates', None, - 'Atrous rates for atrous spatial pyramid pooling.') - -flags.DEFINE_integer('output_stride', 16, - 'The ratio of input to output spatial resolution.') - -# Change to [0.5, 0.75, 1.0, 1.25, 1.5, 1.75] for multi-scale test. -flags.DEFINE_multi_float('eval_scales', [1.0], - 'The scales to resize images for evaluation.') - -# Change to True for adding flipped images during test. -flags.DEFINE_bool('add_flipped_images', False, - 'Add flipped images for evaluation or not.') - -flags.DEFINE_integer( - 'quantize_delay_step', -1, - 'Steps to start quantized training. If < 0, will not quantize model.') - -# Dataset settings. - -flags.DEFINE_string('dataset', 'pascal_voc_seg', - 'Name of the segmentation dataset.') - -flags.DEFINE_string('vis_split', 'val', - 'Which split of the dataset used for visualizing results') - -flags.DEFINE_string('dataset_dir', None, 'Where the dataset reside.') - -flags.DEFINE_enum('colormap_type', 'pascal', ['pascal', 'cityscapes', 'ade20k'], - 'Visualization colormap type.') - -flags.DEFINE_boolean('also_save_raw_predictions', False, - 'Also save raw predictions.') - -flags.DEFINE_integer('max_number_of_iterations', 0, - 'Maximum number of visualization iterations. Will loop ' - 'indefinitely upon nonpositive values.') - -# The folder where semantic segmentation predictions are saved. -_SEMANTIC_PREDICTION_SAVE_FOLDER = 'segmentation_results' - -# The folder where raw semantic segmentation predictions are saved. -_RAW_SEMANTIC_PREDICTION_SAVE_FOLDER = 'raw_segmentation_results' - -# The format to save image. -_IMAGE_FORMAT = '%06d_image' - -# The format to save prediction -_PREDICTION_FORMAT = '%06d_prediction' - -# To evaluate Cityscapes results on the evaluation server, the labels used -# during training should be mapped to the labels for evaluation. -_CITYSCAPES_TRAIN_ID_TO_EVAL_ID = [7, 8, 11, 12, 13, 17, 19, 20, 21, 22, - 23, 24, 25, 26, 27, 28, 31, 32, 33] - - -def _convert_train_id_to_eval_id(prediction, train_id_to_eval_id): - """Converts the predicted label for evaluation. - - There are cases where the training labels are not equal to the evaluation - labels. This function is used to perform the conversion so that we could - evaluate the results on the evaluation server. - - Args: - prediction: Semantic segmentation prediction. - train_id_to_eval_id: A list mapping from train id to evaluation id. - - Returns: - Semantic segmentation prediction whose labels have been changed. - """ - converted_prediction = prediction.copy() - for train_id, eval_id in enumerate(train_id_to_eval_id): - converted_prediction[prediction == train_id] = eval_id - - return converted_prediction - - -def _process_batch(sess, original_images, semantic_predictions, image_names, - image_heights, image_widths, image_id_offset, save_dir, - raw_save_dir, train_id_to_eval_id=None): - """Evaluates one single batch qualitatively. - - Args: - sess: TensorFlow session. - original_images: One batch of original images. - semantic_predictions: One batch of semantic segmentation predictions. - image_names: Image names. - image_heights: Image heights. - image_widths: Image widths. - image_id_offset: Image id offset for indexing images. - save_dir: The directory where the predictions will be saved. - raw_save_dir: The directory where the raw predictions will be saved. - train_id_to_eval_id: A list mapping from train id to eval id. - """ - (original_images, - semantic_predictions, - image_names, - image_heights, - image_widths) = sess.run([original_images, semantic_predictions, - image_names, image_heights, image_widths]) - - num_image = semantic_predictions.shape[0] - for i in range(num_image): - image_height = np.squeeze(image_heights[i]) - image_width = np.squeeze(image_widths[i]) - original_image = np.squeeze(original_images[i]) - semantic_prediction = np.squeeze(semantic_predictions[i]) - crop_semantic_prediction = semantic_prediction[:image_height, :image_width] - - # Save image. - save_annotation.save_annotation( - original_image, save_dir, _IMAGE_FORMAT % (image_id_offset + i), - add_colormap=False) - - # Save prediction. - save_annotation.save_annotation( - crop_semantic_prediction, save_dir, - _PREDICTION_FORMAT % (image_id_offset + i), add_colormap=True, - colormap_type=FLAGS.colormap_type) - - if FLAGS.also_save_raw_predictions: - image_filename = os.path.basename(image_names[i]) - - if train_id_to_eval_id is not None: - crop_semantic_prediction = _convert_train_id_to_eval_id( - crop_semantic_prediction, - train_id_to_eval_id) - save_annotation.save_annotation( - crop_semantic_prediction, raw_save_dir, image_filename, - add_colormap=False) - - -def main(unused_argv): - tf.logging.set_verbosity(tf.logging.INFO) - - # Get dataset-dependent information. - dataset = data_generator.Dataset( - dataset_name=FLAGS.dataset, - split_name=FLAGS.vis_split, - dataset_dir=FLAGS.dataset_dir, - batch_size=FLAGS.vis_batch_size, - crop_size=[int(sz) for sz in FLAGS.vis_crop_size], - min_resize_value=FLAGS.min_resize_value, - max_resize_value=FLAGS.max_resize_value, - resize_factor=FLAGS.resize_factor, - model_variant=FLAGS.model_variant, - is_training=False, - should_shuffle=False, - should_repeat=False) - - train_id_to_eval_id = None - if dataset.dataset_name == data_generator.get_cityscapes_dataset_name(): - tf.logging.info('Cityscapes requires converting train_id to eval_id.') - train_id_to_eval_id = _CITYSCAPES_TRAIN_ID_TO_EVAL_ID - - # Prepare for visualization. - tf.gfile.MakeDirs(FLAGS.vis_logdir) - save_dir = os.path.join(FLAGS.vis_logdir, _SEMANTIC_PREDICTION_SAVE_FOLDER) - tf.gfile.MakeDirs(save_dir) - raw_save_dir = os.path.join( - FLAGS.vis_logdir, _RAW_SEMANTIC_PREDICTION_SAVE_FOLDER) - tf.gfile.MakeDirs(raw_save_dir) - - tf.logging.info('Visualizing on %s set', FLAGS.vis_split) - - with tf.Graph().as_default(): - samples = dataset.get_one_shot_iterator().get_next() - - model_options = common.ModelOptions( - outputs_to_num_classes={common.OUTPUT_TYPE: dataset.num_of_classes}, - crop_size=[int(sz) for sz in FLAGS.vis_crop_size], - atrous_rates=FLAGS.atrous_rates, - output_stride=FLAGS.output_stride) - - if tuple(FLAGS.eval_scales) == (1.0,): - tf.logging.info('Performing single-scale test.') - predictions = model.predict_labels( - samples[common.IMAGE], - model_options=model_options, - image_pyramid=FLAGS.image_pyramid) - else: - tf.logging.info('Performing multi-scale test.') - if FLAGS.quantize_delay_step >= 0: - raise ValueError( - 'Quantize mode is not supported with multi-scale test.') - predictions = model.predict_labels_multi_scale( - samples[common.IMAGE], - model_options=model_options, - eval_scales=FLAGS.eval_scales, - add_flipped_images=FLAGS.add_flipped_images) - predictions = predictions[common.OUTPUT_TYPE] - - if FLAGS.min_resize_value and FLAGS.max_resize_value: - # Only support batch_size = 1, since we assume the dimensions of original - # image after tf.squeeze is [height, width, 3]. - assert FLAGS.vis_batch_size == 1 - - # Reverse the resizing and padding operations performed in preprocessing. - # First, we slice the valid regions (i.e., remove padded region) and then - # we resize the predictions back. - original_image = tf.squeeze(samples[common.ORIGINAL_IMAGE]) - original_image_shape = tf.shape(original_image) - predictions = tf.slice( - predictions, - [0, 0, 0], - [1, original_image_shape[0], original_image_shape[1]]) - resized_shape = tf.to_int32([tf.squeeze(samples[common.HEIGHT]), - tf.squeeze(samples[common.WIDTH])]) - predictions = tf.squeeze( - tf.image.resize_images(tf.expand_dims(predictions, 3), - resized_shape, - method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, - align_corners=True), 3) - - tf.train.get_or_create_global_step() - if FLAGS.quantize_delay_step >= 0: - contrib_quantize.create_eval_graph() - - num_iteration = 0 - max_num_iteration = FLAGS.max_number_of_iterations - - checkpoints_iterator = contrib_training.checkpoints_iterator( - FLAGS.checkpoint_dir, min_interval_secs=FLAGS.eval_interval_secs) - for checkpoint_path in checkpoints_iterator: - num_iteration += 1 - tf.logging.info( - 'Starting visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S', - time.gmtime())) - tf.logging.info('Visualizing with model %s', checkpoint_path) - - scaffold = tf.train.Scaffold(init_op=tf.global_variables_initializer()) - session_creator = tf.train.ChiefSessionCreator( - scaffold=scaffold, - master=FLAGS.master, - checkpoint_filename_with_path=checkpoint_path) - with tf.train.MonitoredSession( - session_creator=session_creator, hooks=None) as sess: - batch = 0 - image_id_offset = 0 - - while not sess.should_stop(): - tf.logging.info('Visualizing batch %d', batch + 1) - _process_batch(sess=sess, - original_images=samples[common.ORIGINAL_IMAGE], - semantic_predictions=predictions, - image_names=samples[common.IMAGE_NAME], - image_heights=samples[common.HEIGHT], - image_widths=samples[common.WIDTH], - image_id_offset=image_id_offset, - save_dir=save_dir, - raw_save_dir=raw_save_dir, - train_id_to_eval_id=train_id_to_eval_id) - image_id_offset += FLAGS.vis_batch_size - batch += 1 - - tf.logging.info( - 'Finished visualization at ' + time.strftime('%Y-%m-%d-%H:%M:%S', - time.gmtime())) - if max_num_iteration > 0 and num_iteration >= max_num_iteration: - break - -if __name__ == '__main__': - flags.mark_flag_as_required('checkpoint_dir') - flags.mark_flag_as_required('vis_logdir') - flags.mark_flag_as_required('dataset_dir') - tf.app.run() diff --git a/research/delf/.gitignore b/research/delf/.gitignore deleted file mode 100644 index b61ddd10001..00000000000 --- a/research/delf/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -*pyc -*~ -*pb2.py -*pb2.pyc diff --git a/research/delf/DETECTION.md b/research/delf/DETECTION.md deleted file mode 100644 index 7fa7570f74d..00000000000 --- a/research/delf/DETECTION.md +++ /dev/null @@ -1,69 +0,0 @@ -## Quick start: landmark detection - -[![Paper](http://img.shields.io/badge/paper-arXiv.1812.01584-B3181B.svg)](https://arxiv.org/abs/1812.01584) - -### Install DELF library - -To be able to use this code, please follow -[these instructions](INSTALL_INSTRUCTIONS.md) to properly install the DELF -library. - -### Download Oxford buildings dataset - -To illustrate detector usage, please download the Oxford buildings dataset, by -following the instructions -[here](EXTRACTION_MATCHING.md#download-oxford-buildings-dataset). Then, create -the file `list_images_detector.txt` as follows: - -```bash -# From tensorflow/models/research/delf/delf/python/examples/ -echo data/oxford5k_images/all_souls_000002.jpg >> list_images_detector.txt -echo data/oxford5k_images/all_souls_000035.jpg >> list_images_detector.txt -``` - -### Download detector model - -Also, you will need to download the pre-trained detector model: - -```bash -# From tensorflow/models/research/delf/delf/python/examples/ -mkdir parameters && cd parameters -wget http://storage.googleapis.com/delf/d2r_frcnn_20190411.tar.gz -tar -xvzf d2r_frcnn_20190411.tar.gz -``` - -**Note**: this is the Faster-RCNN based model. We also release a MobileNet-SSD -model, see the [README](README.md#pre-trained-models) for download link. The -instructions should work seamlessly for both models. - -### Detecting landmarks - -Now that you have everything in place, running this command should detect boxes -for the images `all_souls_000002.jpg` and `all_souls_000035.jpg`, with a -threshold of 0.8, and produce visualizations. - -```bash -# From tensorflow/models/research/delf/delf/python/examples/ -python3 extract_boxes.py \ - --detector_path parameters/d2r_frcnn_20190411 \ - --detector_thresh 0.8 \ - --list_images_path list_images_detector.txt \ - --output_dir data/oxford5k_boxes \ - --output_viz_dir data/oxford5k_boxes_viz -``` - -Two images are generated in the `data/oxford5k_boxes_viz` directory, they should -look similar to these ones: - -![DetectionExample1](delf/python/examples/detection_example_1.jpg) -![DetectionExample2](delf/python/examples/detection_example_2.jpg) - -### Troubleshooting - -#### `matplotlib` - -`matplotlib` may complain with a message such as `no display name and no -$DISPLAY environment variable`. To fix this, one option is add the line -`backend : Agg` to the file `.config/matplotlib/matplotlibrc`. On this problem, -see the discussion -[here](https://stackoverflow.com/questions/37604289/tkinter-tclerror-no-display-name-and-no-display-environment-variable). diff --git a/research/delf/EXTRACTION_MATCHING.md b/research/delf/EXTRACTION_MATCHING.md deleted file mode 100644 index 53159638587..00000000000 --- a/research/delf/EXTRACTION_MATCHING.md +++ /dev/null @@ -1,87 +0,0 @@ -## Quick start: DELF extraction and matching - -[![Paper](http://img.shields.io/badge/paper-arXiv.1612.06321-B3181B.svg)](https://arxiv.org/abs/1612.06321) - -### Install DELF library - -To be able to use this code, please follow -[these instructions](INSTALL_INSTRUCTIONS.md) to properly install the DELF -library. - -### Download Oxford buildings dataset - -To illustrate DELF usage, please download the Oxford buildings dataset. To -follow these instructions closely, please download the dataset to the -`tensorflow/models/research/delf/delf/python/examples` directory, as in the -following commands: - -```bash -# From tensorflow/models/research/delf/delf/python/examples/ -mkdir data && cd data -wget http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/oxbuild_images.tgz -mkdir oxford5k_images oxford5k_features -tar -xvzf oxbuild_images.tgz -C oxford5k_images/ -cd ../ -echo data/oxford5k_images/hertford_000056.jpg >> list_images.txt -echo data/oxford5k_images/oxford_000317.jpg >> list_images.txt -``` - -### Download pre-trained DELF model - -Also, you will need to download the trained DELF model: - -```bash -# From tensorflow/models/research/delf/delf/python/examples/ -mkdir parameters && cd parameters -wget http://storage.googleapis.com/delf/delf_gld_20190411.tar.gz -tar -xvzf delf_gld_20190411.tar.gz -``` - -### DELF feature extraction - -Now that you have everything in place, running this command should extract DELF -features for the images `hertford_000056.jpg` and `oxford_000317.jpg`: - -```bash -# From tensorflow/models/research/delf/delf/python/examples/ -python3 extract_features.py \ - --config_path delf_config_example.pbtxt \ - --list_images_path list_images.txt \ - --output_dir data/oxford5k_features -``` - -### Image matching using DELF features - -After feature extraction, run this command to perform feature matching between -the images `hertford_000056.jpg` and `oxford_000317.jpg`: - -```bash -python3 match_images.py \ - --image_1_path data/oxford5k_images/hertford_000056.jpg \ - --image_2_path data/oxford5k_images/oxford_000317.jpg \ - --features_1_path data/oxford5k_features/hertford_000056.delf \ - --features_2_path data/oxford5k_features/oxford_000317.delf \ - --output_image matched_images.png -``` - -The image `matched_images.png` is generated and should look similar to this one: - -![MatchedImagesExample](delf/python/examples/matched_images_example.jpg) - -### Troubleshooting - -#### `matplotlib` - -`matplotlib` may complain with a message such as `no display name and no -$DISPLAY environment variable`. To fix this, one option is add the line -`backend : Agg` to the file `.config/matplotlib/matplotlibrc`. On this problem, -see the discussion -[here](https://stackoverflow.com/questions/37604289/tkinter-tclerror-no-display-name-and-no-display-environment-variable). - -#### 'skimage' - -By default, skimage 0.13.XX or 0.14.1 is installed if you followed the -instructions. According to -[https://github.com/scikit-image/scikit-image/issues/3649#issuecomment-455273659] -If you have scikit-image related issues, upgrading to a version above 0.14.1 -with `pip3 install -U scikit-image` should fix the issue diff --git a/research/delf/INSTALL_INSTRUCTIONS.md b/research/delf/INSTALL_INSTRUCTIONS.md deleted file mode 100644 index f5616f47ff0..00000000000 --- a/research/delf/INSTALL_INSTRUCTIONS.md +++ /dev/null @@ -1,157 +0,0 @@ -## DELF installation - -### Installation script - -We now have a script to do the entire installation in one shot. Navigate to the -directory `models/research/delf/delf/python/training`, then run: - -```bash -# From models/research/delf/delf/python/training -bash install_delf.sh -``` - -If this works, you are done! If not, see below for detailed instructions for -installing this codebase and its dependencies. - -*Please note that this installation script only works on 64 bits Linux -architectures due to the `protoc` binary that is automatically downloaded. If -you wish to install the DELF library on other architectures please update the -[`install_delf.sh`](delf/python/training/install_delf.sh) script by referencing -the desired `protoc` -[binary release](https://github.com/protocolbuffers/protobuf/releases).* - -In more detail: the `install_delf.sh` script installs both the DELF library and -its dependencies in the following sequence: - -* Install TensorFlow 2.2 and TensorFlow 2.2 for GPU. -* Install the [TF-Slim](https://github.com/google-research/tf-slim) library - from source. -* Download [protoc](https://github.com/protocolbuffers/protobuf) and compile - the DELF Protocol Buffers. -* Install the matplotlib, numpy, scikit-image, scipy and python3-tk Python - libraries. -* Install the - [TensorFlow Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection) - from the cloned TensorFlow Model Garden repository. -* Install the DELF package. - -### Tensorflow - -[![TensorFlow 2.2](https://img.shields.io/badge/tensorflow-2.2-brightgreen)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/) - -For detailed steps to install Tensorflow, follow the -[Tensorflow installation instructions](https://www.tensorflow.org/install/). A -typical user can install Tensorflow using one of the following commands: - -```bash -# For CPU: -pip3 install 'tensorflow>=2.2.0' -# For GPU: -pip3 install 'tensorflow-gpu>=2.2.0' -``` - -### TF-Slim - -Note: currently, we need to install the latest version from source, to avoid -using previous versions which relied on tf.contrib (which is now deprecated). - -```bash -git clone git@github.com:google-research/tf-slim.git -cd tf-slim -pip3 install . -``` - -Note that these commands assume you are cloning using SSH. If you are using -HTTPS instead, use `git clone https://github.com/google-research/tf-slim.git` -instead. See -[this link](https://help.github.com/en/github/using-git/which-remote-url-should-i-use) -for more information. - -### Protobuf - -The DELF library uses [protobuf](https://github.com/google/protobuf) (the python -version) to configure feature extraction and its format. You will need the -`protoc` compiler, version >= 3.3. The easiest way to get it is to download -directly. For Linux, this can be done as (see -[here](https://github.com/google/protobuf/releases) for other platforms): - -```bash -wget https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-linux-x86_64.zip -unzip protoc-3.3.0-linux-x86_64.zip -PATH_TO_PROTOC=`pwd` -``` - -### Python dependencies - -Install python library dependencies: - -```bash -pip3 install matplotlib numpy scikit-image scipy -sudo apt-get install python3-tk -``` - -### `tensorflow/models` - -Now, clone `tensorflow/models`, and install required libraries: (note that the -`object_detection` library requires you to add `tensorflow/models/research/` to -your `PYTHONPATH`, as instructed -[here](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md)) - -```bash -git clone git@github.com:tensorflow/models.git - -# Setup the object_detection module by editing PYTHONPATH. -cd .. -# From tensorflow/models/research/ -export PYTHONPATH=$PYTHONPATH:`pwd` -``` - -Note that these commands assume you are cloning using SSH. If you are using -HTTPS instead, use `git clone https://github.com/tensorflow/models.git` instead. -See -[this link](https://help.github.com/en/github/using-git/which-remote-url-should-i-use) -for more information. - -Then, compile DELF's protobufs. Use `PATH_TO_PROTOC` as the directory where you -downloaded the `protoc` compiler. - -```bash -# From tensorflow/models/research/delf/ -${PATH_TO_PROTOC?}/bin/protoc delf/protos/*.proto --python_out=. -``` - -Finally, install the DELF package. This may also install some other dependencies -under the hood. - -```bash -# From tensorflow/models/research/delf/ -pip3 install -e . # Install "delf" package. -``` - -At this point, running - -```bash -python3 -c 'import delf' -``` - -should just return without complaints. This indicates that the DELF package is -loaded successfully. - -### Troubleshooting - -#### `pip3 install` - -Issues might be observed if using `pip3 install` with `-e` option (editable -mode). You may try out to simply remove the `-e` from the commands above. Also, -depending on your machine setup, you might need to run the `sudo pip3 install` -command, that is with a `sudo` at the beginning. - -#### Cloning github repositories - -The default commands above assume you are cloning using SSH. If you are using -HTTPS instead, use for example `git clone -https://github.com/tensorflow/models.git` instead of `git clone -git@github.com:tensorflow/models.git`. See -[this link](https://help.github.com/en/github/using-git/which-remote-url-should-i-use) -for more information. diff --git a/research/delf/README.md b/research/delf/README.md deleted file mode 100644 index 274723db5be..00000000000 --- a/research/delf/README.md +++ /dev/null @@ -1,291 +0,0 @@ -# Deep Local and Global Image Features - -[![TensorFlow 2.2](https://img.shields.io/badge/tensorflow-2.2-brightgreen)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![Python 3.6](https://img.shields.io/badge/python-3.6-blue.svg)](https://www.python.org/downloads/release/python-360/) - -This project presents code for deep local and global image feature methods, -which are particularly useful for the computer vision tasks of instance-level -recognition and retrieval. These were introduced in the -[DELF](https://arxiv.org/abs/1612.06321), -[Detect-to-Retrieve](https://arxiv.org/abs/1812.01584), -[DELG](https://arxiv.org/abs/2001.05027) and -[Google Landmarks Dataset v2](https://arxiv.org/abs/2004.01804) papers. - -We provide Tensorflow code for building and training models, and python code for -image retrieval and local feature matching. Pre-trained models for the landmark -recognition domain are also provided. - -If you make use of this codebase, please consider citing the following papers: - -DELF: -[![Paper](http://img.shields.io/badge/paper-arXiv.1612.06321-B3181B.svg)](https://arxiv.org/abs/1612.06321) - -``` -"Large-Scale Image Retrieval with Attentive Deep Local Features", -H. Noh, A. Araujo, J. Sim, T. Weyand and B. Han, -Proc. ICCV'17 -``` - -Detect-to-Retrieve: -[![Paper](http://img.shields.io/badge/paper-arXiv.1812.01584-B3181B.svg)](https://arxiv.org/abs/1812.01584) - -``` -"Detect-to-Retrieve: Efficient Regional Aggregation for Image Search", -M. Teichmann*, A. Araujo*, M. Zhu and J. Sim, -Proc. CVPR'19 -``` - -DELG: -[![Paper](http://img.shields.io/badge/paper-arXiv.2001.05027-B3181B.svg)](https://arxiv.org/abs/2001.05027) - -``` -"Unifying Deep Local and Global Features for Image Search", -B. Cao*, A. Araujo* and J. Sim, -Proc. ECCV'20 -``` - -GLDv2: -[![Paper](http://img.shields.io/badge/paper-arXiv.2004.01804-B3181B.svg)](https://arxiv.org/abs/2004.01804) - -``` -"Google Landmarks Dataset v2 - A Large-Scale Benchmark for Instance-Level Recognition and Retrieval", -T. Weyand*, A. Araujo*, B. Cao and J. Sim, -Proc. CVPR'20 -``` - -## News - -- [Jul'20] Check out our ECCV'20 paper: - ["Unifying Deep Local and Global Features for Image Search"](https://arxiv.org/abs/2001.05027) -- [Apr'20] Check out our CVPR'20 paper: ["Google Landmarks Dataset v2 - A - Large-Scale Benchmark for Instance-Level Recognition and - Retrieval"](https://arxiv.org/abs/2004.01804) -- [Jun'19] DELF achieved 2nd place in - [CVPR Visual Localization challenge (Local Features track)](https://sites.google.com/corp/view/ltvl2019). - See our slides - [here](https://docs.google.com/presentation/d/e/2PACX-1vTswzoXelqFqI_pCEIVl2uazeyGr7aKNklWHQCX-CbQ7MB17gaycqIaDTguuUCRm6_lXHwCdrkP7n1x/pub?start=false&loop=false&delayms=3000). -- [Apr'19] Check out our CVPR'19 paper: - ["Detect-to-Retrieve: Efficient Regional Aggregation for Image Search"](https://arxiv.org/abs/1812.01584) -- [Jun'18] DELF achieved state-of-the-art results in a CVPR'18 image retrieval - paper: [Radenovic et al., "Revisiting Oxford and Paris: Large-Scale Image - Retrieval Benchmarking"](https://arxiv.org/abs/1803.11285). -- [Apr'18] DELF was featured in - [ModelDepot](https://modeldepot.io/mikeshi/delf/overview) -- [Mar'18] DELF is now available in - [TF-Hub](https://www.tensorflow.org/hub/modules/google/delf/1) - -## Datasets - -We have two Google-Landmarks dataset versions: - -- Initial version (v1) can be found - [here](https://www.kaggle.com/google/google-landmarks-dataset). In includes - the Google Landmark Boxes which were described in the Detect-to-Retrieve - paper. -- Second version (v2) has been released as part of two Kaggle challenges: - [Landmark Recognition](https://www.kaggle.com/c/landmark-recognition-2019) - and [Landmark Retrieval](https://www.kaggle.com/c/landmark-retrieval-2019). - It can be downloaded from CVDF - [here](https://github.com/cvdfoundation/google-landmark). See also - [the CVPR'20 paper](https://arxiv.org/abs/2004.01804) on this new dataset - version. - -If you make use of these datasets in your research, please consider citing the -papers mentioned above. - -## Installation - -To be able to use this code, please follow -[these instructions](INSTALL_INSTRUCTIONS.md) to properly install the DELF -library. - -## Quick start - -### Pre-trained models - -We release several pre-trained models. See instructions in the following -sections for examples on how to use the models. - -**DELF pre-trained on the Google-Landmarks dataset v1** -([link](http://storage.googleapis.com/delf/delf_gld_20190411.tar.gz)). Presented -in the [Detect-to-Retrieve paper](https://arxiv.org/abs/1812.01584). Boosts -performance by ~4% mAP compared to ICCV'17 DELF model. - -**DELG pre-trained on the Google-Landmarks dataset v1** -([R101-DELG](http://storage.googleapis.com/delf/r101delg_gld_20200814.tar.gz), -[R50-DELG](http://storage.googleapis.com/delf/r50delg_gld_20200814.tar.gz)). -Presented in the [DELG paper](https://arxiv.org/abs/2001.05027). - -**DELG pre-trained on the Google-Landmarks dataset v2 (clean)** -([R101-DELG](https://storage.googleapis.com/delf/r101delg_gldv2clean_20200914.tar.gz), -[R50-DELG](https://storage.googleapis.com/delf/r50delg_gldv2clean_20200914.tar.gz)). -Presented in the [DELG paper](https://arxiv.org/abs/2001.05027). - -**RN101-ArcFace pre-trained on the Google-Landmarks dataset v2 (train-clean)** -([link](https://storage.googleapis.com/delf/rn101_af_gldv2clean_20200814.tar.gz)). -Presented in the [GLDv2 paper](https://arxiv.org/abs/2004.01804). - -**DELF pre-trained on Landmarks-Clean/Landmarks-Full dataset** -([link](http://storage.googleapis.com/delf/delf_v1_20171026.tar.gz)). Presented -in the [DELF paper](https://arxiv.org/abs/1612.06321), model was trained on the -dataset released by the [DIR paper](https://arxiv.org/abs/1604.01325). - -**Faster-RCNN detector pre-trained on Google Landmark Boxes** -([link](http://storage.googleapis.com/delf/d2r_frcnn_20190411.tar.gz)). -Presented in the [Detect-to-Retrieve paper](https://arxiv.org/abs/1812.01584). - -**MobileNet-SSD detector pre-trained on Google Landmark Boxes** -([link](http://storage.googleapis.com/delf/d2r_mnetssd_20190411.tar.gz)). -Presented in the [Detect-to-Retrieve paper](https://arxiv.org/abs/1812.01584). - -Besides these, we also release pre-trained codebooks for local feature -aggregation. See the -[Detect-to-Retrieve instructions](delf/python/detect_to_retrieve/DETECT_TO_RETRIEVE_INSTRUCTIONS.md) -for details. - -### DELF extraction and matching - -Please follow [these instructions](EXTRACTION_MATCHING.md). At the end, you -should obtain a nice figure showing local feature matches, as: - -![MatchedImagesExample](delf/python/examples/matched_images_example.jpg) - -### DELF training - -Please follow [these instructions](delf/python/training/README.md). - -### DELG - -Please follow [these instructions](delf/python/delg/DELG_INSTRUCTIONS.md). At -the end, you should obtain image retrieval results on the Revisited Oxford/Paris -datasets. - -### GLDv2 baseline - -Please follow -[these instructions](delf/python/datasets/google_landmarks_dataset/README.md). At the -end, you should obtain image retrieval results on the Revisited Oxford/Paris -datasets. - -### Landmark detection - -Please follow [these instructions](DETECTION.md). At the end, you should obtain -a nice figure showing a detection, as: - -![DetectionExample1](delf/python/examples/detection_example_1.jpg) - -### Detect-to-Retrieve - -Please follow -[these instructions](delf/python/detect_to_retrieve/DETECT_TO_RETRIEVE_INSTRUCTIONS.md). -At the end, you should obtain image retrieval results on the Revisited -Oxford/Paris datasets. - -## Code overview - -DELF/D2R/DELG/GLD code is located under the `delf` directory. There are two -directories therein, `protos` and `python`. - -### `delf/protos` - -This directory contains protobufs for local feature aggregation -(`aggregation_config.proto`), serializing detected boxes (`box.proto`), -serializing float tensors (`datum.proto`), configuring DELF/DELG extraction -(`delf_config.proto`), serializing local features (`feature.proto`). - -### `delf/python` - -This directory contains files for several different purposes, such as: -reading/writing tensors/features (`box_io.py`, `datum_io.py`, `feature_io.py`), -local feature aggregation extraction and similarity computation -(`feature_aggregation_extractor.py`, `feature_aggregation_similarity.py`) and -helper functions for image/feature loading/processing (`utils.py`, -`feature_extractor.py`). - -The subdirectory `delf/python/examples` contains sample scripts to run DELF/DELG -feature extraction/matching (`extractor.py`, `extract_features.py`, -`match_images.py`) and object detection (`detector.py`, `extract_boxes.py`). -`delf_config_example.pbtxt` shows an example instantiation of the DelfConfig -proto, used for DELF feature extraction. - -The subdirectory `delf/python/delg` contains sample scripts/configs related to -the DELG paper: `extract_features.py` for local+global feature extraction (with -and example `delg_gld_config.pbtxt`) and `perform_retrieval.py` for performing -retrieval/scoring. - -The subdirectory `delf/python/detect_to_retrieve` contains sample -scripts/configs related to the Detect-to-Retrieve paper, for feature/box -extraction/aggregation/clustering (`aggregation_extraction.py`, -`boxes_and_features_extraction.py`, `cluster_delf_features.py`, -`extract_aggregation.py`, `extract_index_boxes_and_features.py`, -`extract_query_features.py`), image retrieval/reranking (`perform_retrieval.py`, -`image_reranking.py`), along with configs used for feature -extraction/aggregation (`delf_gld_config.pbtxt`, -`index_aggregation_config.pbtxt`, `query_aggregation_config.pbtxt`) and -Revisited Oxford/Paris dataset parsing/evaluation (`dataset.py`). - -The subdirectory `delf/python/google_landmarks_dataset` contains sample -scripts/modules for computing GLD metrics (`metrics.py`, -`compute_recognition_metrics.py`, `compute_retrieval_metrics.py`), GLD file IO -(`dataset_file_io.py`) / reproducing results from the GLDv2 paper -(`rn101_af_gldv2clean_config.pbtxt` and the instructions therein). - -The subdirectory `delf/python/training` contains sample scripts/modules for -performing model training (`train.py`) based on a ResNet50 DELF model -(`model/resnet50.py`, `model/delf_model.py`), also presenting relevant model -exporting scripts and associated utils (`model/export_model.py`, -`model/export_global_model.py`, `model/export_model_utils.py`) and dataset -downloading/preprocessing (`download_dataset.sh`, `build_image_dataset.py`, -`datasets/googlelandmarks.py`). - -Besides these, other files in the different subdirectories contain tests for the -various modules. - -## Maintainers - -André Araujo (@andrefaraujo) - -## Release history - -### Jul, 2020 - -- Full TF2 support. Only one minor `compat.v1` usage left. Updated - instructions to require TF2.2 -- Refactored / much improved training code, with very detailed, step-by-step - instructions - -**Thanks to contributors**: Dan Anghel, Barbara Fusinska and André -Araujo. - -### May, 2020 - -- Codebase is now Python3-first -- DELG model/code released -- GLDv2 baseline model released - -**Thanks to contributors**: Barbara Fusinska and André Araujo. - -### April, 2020 (version 2.0) - -- Initial DELF training code released. -- Codebase is now fully compatible with TF 2.1. - -**Thanks to contributors**: Arun Mukundan, Yuewei Na and André Araujo. - -### April, 2019 - -Detect-to-Retrieve code released. - -Includes pre-trained models to detect landmark boxes, and DELF model pre-trained -on Google Landmarks v1 dataset. - -**Thanks to contributors**: André Araujo, Marvin Teichmann, Menglong Zhu, -Jack Sim. - -### October, 2017 - -Initial release containing DELF-v1 code, including feature extraction and -matching examples. Pre-trained DELF model from ICCV'17 paper is released. - -**Thanks to contributors**: André Araujo, Hyeonwoo Noh, Youlong Cheng, -Jack Sim. diff --git a/research/delf/delf/__init__.py b/research/delf/delf/__init__.py deleted file mode 100644 index a3c5d37bc44..00000000000 --- a/research/delf/delf/__init__.py +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module to extract deep local features.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=unused-import -from delf.protos import aggregation_config_pb2 -from delf.protos import box_pb2 -from delf.protos import datum_pb2 -from delf.protos import delf_config_pb2 -from delf.protos import feature_pb2 -from delf.python import box_io -from delf.python import datum_io -from delf.python import feature_aggregation_extractor -from delf.python import feature_aggregation_similarity -from delf.python import feature_extractor -from delf.python import feature_io -from delf.python import utils -from delf.python import whiten -from delf.python.examples import detector -from delf.python.examples import extractor -from delf.python import detect_to_retrieve -from delf.python import training -from delf.python.training import model -from delf.python import datasets -from delf.python.datasets import google_landmarks_dataset -from delf.python.datasets import revisited_op -# pylint: enable=unused-import diff --git a/research/delf/delf/protos/__init__.py b/research/delf/delf/protos/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/delf/delf/protos/aggregation_config.proto b/research/delf/delf/protos/aggregation_config.proto deleted file mode 100644 index b1d5953d43f..00000000000 --- a/research/delf/delf/protos/aggregation_config.proto +++ /dev/null @@ -1,63 +0,0 @@ -// Protocol buffer for feature aggregation configuration. -// -// Used for both extraction and comparison of aggregated representations. Note -// that some options are only relevant for the former or the latter. -// -// For more details, please refer to the paper: -// "Detect-to-Retrieve: Efficient Regional Aggregation for Image Search", -// Proc. CVPR'19 (https://arxiv.org/abs/1812.01584). - -syntax = "proto2"; - -package delf.protos; - -message AggregationConfig { - // Number of codewords (ie, visual words) in the codebook. - optional int32 codebook_size = 1 [default = 65536]; - - // Dimensionality of local features (eg, 128 for DELF used in - // Detect-to-Retrieve paper). - optional int32 feature_dimensionality = 2 [default = 128]; - - // Type of aggregation to use. - // For example, to use R-ASMK*, `aggregation_type` should be set to ASMK_STAR - // and `use_regional_aggregation` should be set to true. - enum AggregationType { - INVALID = 0; - VLAD = 1; - ASMK = 2; - ASMK_STAR = 3; - } - optional AggregationType aggregation_type = 3 [default = ASMK_STAR]; - - // L2 normalization option. - // - For vanilla aggregated kernels (eg, VLAD/ASMK/ASMK*), this should be - // set to true. - // - For regional aggregated kernels (ie, if `use_regional_aggregation` is - // true, leading to R-VLAD/R-ASMK/R-ASMK*), this should be set to false. - // Note that it is used differently depending on the `aggregation_type`: - // - For VLAD, this option is only used for extraction. - // - For ASMK/ASMK*, this option is only used for comparisons. - optional bool use_l2_normalization = 4 [default = true]; - - // Additional options used only for extraction. - // - Path to codebook checkpoint for aggregation. - optional string codebook_path = 5; - // - Number of visual words to assign each feature. - optional int32 num_assignments = 6 [default = 1]; - // - Whether to use regional aggregation. - optional bool use_regional_aggregation = 7 [default = false]; - // - Batch size to use for local features when computing aggregated - // representations. Particularly useful if `codebook_size` and - // `feature_dimensionality` are large, to avoid OOM. A value of zero or - // lower indicates that no batching is used. - optional int32 feature_batch_size = 10 [default = 100]; - - // Additional options used only for comparison. - // Only relevant if `aggregation_type` is ASMK or ASMK_STAR. - // - Power-law exponent for similarity of visual word descriptors. - optional float alpha = 8 [default = 3.0]; - // - Threshold above which similarity of visual word descriptors are - // considered; below this, similarity is set to zero. - optional float tau = 9 [default = 0.0]; -} diff --git a/research/delf/delf/protos/box.proto b/research/delf/delf/protos/box.proto deleted file mode 100644 index 28da7fb7141..00000000000 --- a/research/delf/delf/protos/box.proto +++ /dev/null @@ -1,24 +0,0 @@ -// Protocol buffer for serializing detected bounding boxes. - -syntax = "proto2"; - -package delf.protos; - -message Box { - // Coordinates: [ymin, xmin, ymax, xmax] corresponds to - // [top, left, bottom, right]. - optional float ymin = 1; - optional float xmin = 2; - optional float ymax = 3; - optional float xmax = 4; - - // Detection score. Usually, the higher the more confident. - optional float score = 5; - - // Indicates which class the box corresponds to. - optional int32 class_index = 6; -} - -message Boxes { - repeated Box box = 1; -} diff --git a/research/delf/delf/protos/datum.proto b/research/delf/delf/protos/datum.proto deleted file mode 100644 index 6806e56b25e..00000000000 --- a/research/delf/delf/protos/datum.proto +++ /dev/null @@ -1,66 +0,0 @@ -// Protocol buffer for serializing arbitrary float tensors. -// Note: Currently only floating point feature is supported. - -syntax = "proto2"; - -package delf.protos; - -// A DatumProto is a data structure used to serialize tensor with arbitrary -// shape. DatumProto contains an array of floating point values and its shape -// is represented as a sequence of integer values. Values are contained in -// row major order. -// -// Example: -// 3 x 2 array -// -// [1.1, 2.2] -// [3.3, 4.4] -// [5.5, 6.6] -// -// can be represented with the following DatumProto: -// -// DatumProto { -// shape { -// dim: 3 -// dim: 2 -// } -// float_list { -// value: 1.1 -// value: 2.2 -// value: 3.3 -// value: 4.4 -// value: 5.5 -// value: 6.6 -// } -// } - -// DatumShape is array of dimension of the tensor. -message DatumShape { - repeated int64 dim = 1 [packed = true]; -} - -// FloatList is a container of tensor values, which are saved as a list of -// floating point values. -message FloatList { - repeated float value = 1 [packed = true]; -} - -// Uint32List is a container of tensor values, which are saved as a list of -// uint32 values. -message Uint32List { - repeated uint32 value = 1 [packed = true]; -} - -message DatumProto { - optional DatumShape shape = 1; - oneof kind_oneof { - FloatList float_list = 2; - Uint32List uint32_list = 3; - } -} - -// Groups two DatumProto's. -message DatumPairProto { - optional DatumProto first = 1; - optional DatumProto second = 2; -} diff --git a/research/delf/delf/protos/delf_config.proto b/research/delf/delf/protos/delf_config.proto deleted file mode 100644 index c7cd5b1ce27..00000000000 --- a/research/delf/delf/protos/delf_config.proto +++ /dev/null @@ -1,130 +0,0 @@ -// Protocol buffer for configuring DELF feature extraction. - -syntax = "proto2"; - -package delf.protos; - -message DelfPcaParameters { - // Path to PCA mean file. - optional string mean_path = 1; // Required. - - // Path to PCA matrix file. - optional string projection_matrix_path = 2; // Required. - - // Dimensionality of feature after PCA. - optional int32 pca_dim = 3; // Required. - - // If whitening is to be used, this must be set to true. - optional bool use_whitening = 4 [default = false]; - - // Path to PCA variances file, used for whitening. This is used only if - // use_whitening is set to true. - optional string pca_variances_path = 5; -} - -message DelfLocalFeatureConfig { - // If PCA is to be used, this must be set to true. - optional bool use_pca = 1 [default = true]; - - // Target layer name for DELF model. This is used to obtain receptive field - // parameters used for localizing features with respect to the input image. - optional string layer_name = 2 [default = ""]; - - // Intersection over union threshold for the non-max suppression (NMS) - // operation. If two features overlap by at most this amount, both are kept. - // Otherwise, the one with largest attention score is kept. This should be a - // number between 0.0 (no region is selected) and 1.0 (all regions are - // selected and NMS is not performed). - optional float iou_threshold = 3 [default = 1.0]; - - // Maximum number of features that will be selected. The features with largest - // scores (eg, largest attention score if score_type is "Att") are the - // selected ones. - optional int32 max_feature_num = 4 [default = 1000]; - - // Threshold to be used for feature selection: no feature with score lower - // than this number will be selected). - optional float score_threshold = 5 [default = 100.0]; - - // PCA parameters for DELF local feature. This is used only if use_pca is - // true. - optional DelfPcaParameters pca_parameters = 6; - - // If true, the returned keypoint locations are grounded to coordinates of the - // resized image used for extraction. If false (default), the returned - // keypoint locations are grounded to coordinates of the original image that - // is fed into feature extraction. - optional bool use_resized_coordinates = 7 [default = false]; -} - -message DelfGlobalFeatureConfig { - // If PCA is to be used, this must be set to true. - optional bool use_pca = 1 [default = true]; - - // PCA parameters for DELF global feature. This is used only if use_pca is - // true. - optional DelfPcaParameters pca_parameters = 2; - - // Denotes indices of DelfConfig's scales that will be used for global - // descriptor extraction. For example, if DelfConfig's image_scales are - // [0.25, 0.5, 1.0] and image_scales_ind is [0, 2], global descriptor - // extraction will use solely scales [0.25, 1.0]. Note that local feature - // extraction will still use [0.25, 0.5, 1.0] in this case. If empty (default) - // , all scales are used. - repeated int32 image_scales_ind = 3; -} - -message DelfConfig { - // Whether to extract local features when using the model. - // At least one of {use_local_features, use_global_features} must be true. - optional bool use_local_features = 7 [default = true]; - // Configuration used for local features. Note: this is used only if - // use_local_features is true. - optional DelfLocalFeatureConfig delf_local_config = 3; - - // Whether to extract global features when using the model. - // At least one of {use_local_features, use_global_features} must be true. - optional bool use_global_features = 8 [default = false]; - // Configuration used for global features. Note: this is used only if - // use_global_features is true. - optional DelfGlobalFeatureConfig delf_global_config = 9; - - // Path to DELF model. - optional string model_path = 1; // Required. - - // Whether model has been exported using TF version 2+. - optional bool is_tf2_exported = 10 [default = false]; - - // Image scales to be used. - repeated float image_scales = 2; - - // Image resizing options. - // - The maximum/minimum image size (in terms of height or width) to be used - // when extracting DELF features. If set to -1 (default), no upper/lower - // bound for image size. If use_square_images option is false (default): - // * If the height *OR* width is larger than max_image_size, it will be - // resized to max_image_size, and the other dimension will be resized by - // preserving the aspect ratio. - // * If both height *AND* width are smaller than min_image_size, the larger - // side is set to min_image_size. - // - If use_square_images option is true, it needs to be resized to square - // resolution. To be more specific: - // * If the height *OR* width is larger than max_image_size, it is resized - // to square resolution of max_image_size. - // * If both height *AND* width are smaller than min_image_size, it is - // resized to square resolution of min_image_size. - // * Else, if the input image's resolution is not square, it is resized to - // square resolution of the larger side. - // Image resizing is useful when we want to ensure that the input to the image - // pyramid has a reasonable number of pixels, which could have large impact in - // terms of image matching performance. - // When using local features, note that the feature locations and scales will - // be consistent with the original image input size. - // Note that when both max_image_size and min_image_size are specified - // (which is a valid and legit use case), as long as max_image_size >= - // min_image_size, there's no conflicting scenario (i.e. never triggers both - // enlarging / shrinking). Bilinear interpolation is used. - optional int32 max_image_size = 4 [default = -1]; - optional int32 min_image_size = 5 [default = -1]; - optional bool use_square_images = 6 [default = false]; -} diff --git a/research/delf/delf/protos/feature.proto b/research/delf/delf/protos/feature.proto deleted file mode 100644 index 64c342fe2c3..00000000000 --- a/research/delf/delf/protos/feature.proto +++ /dev/null @@ -1,22 +0,0 @@ -// Protocol buffer for serializing the DELF feature information. - -syntax = "proto2"; - -package delf.protos; - -import "delf/protos/datum.proto"; - -// FloatList is the container of tensor values. The tensor values are saved as -// a list of floating point values. -message DelfFeature { - optional DatumProto descriptor = 1; - optional float x = 2; - optional float y = 3; - optional float scale = 4; - optional float orientation = 5; - optional float strength = 6; -} - -message DelfFeatures { - repeated DelfFeature feature = 1; -} diff --git a/research/delf/delf/python/__init__.py b/research/delf/delf/python/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/delf/delf/python/box_io.py b/research/delf/delf/python/box_io.py deleted file mode 100644 index 8b0f0d2c973..00000000000 --- a/research/delf/delf/python/box_io.py +++ /dev/null @@ -1,151 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Python interface for Boxes proto. - -Support read and write of Boxes from/to numpy arrays and file. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from delf import box_pb2 - - -def ArraysToBoxes(boxes, scores, class_indices): - """Converts `boxes` to Boxes proto. - - Args: - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - - Returns: - boxes_proto: Boxes object. - """ - num_boxes = len(scores) - assert num_boxes == boxes.shape[0] - assert num_boxes == len(class_indices) - - boxes_proto = box_pb2.Boxes() - for i in range(num_boxes): - boxes_proto.box.add( - ymin=boxes[i, 0], - xmin=boxes[i, 1], - ymax=boxes[i, 2], - xmax=boxes[i, 3], - score=scores[i], - class_index=class_indices[i]) - - return boxes_proto - - -def BoxesToArrays(boxes_proto): - """Converts data saved in Boxes proto to numpy arrays. - - If there are no boxes, the function returns three empty arrays. - - Args: - boxes_proto: Boxes proto object. - - Returns: - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - """ - num_boxes = len(boxes_proto.box) - if num_boxes == 0: - return np.array([]), np.array([]), np.array([]) - - boxes = np.zeros([num_boxes, 4]) - scores = np.zeros([num_boxes]) - class_indices = np.zeros([num_boxes]) - - for i in range(num_boxes): - box_proto = boxes_proto.box[i] - boxes[i] = [box_proto.ymin, box_proto.xmin, box_proto.ymax, box_proto.xmax] - scores[i] = box_proto.score - class_indices[i] = box_proto.class_index - - return boxes, scores, class_indices - - -def SerializeToString(boxes, scores, class_indices): - """Converts numpy arrays to serialized Boxes. - - Args: - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - - Returns: - Serialized Boxes string. - """ - boxes_proto = ArraysToBoxes(boxes, scores, class_indices) - return boxes_proto.SerializeToString() - - -def ParseFromString(string): - """Converts serialized Boxes proto string to numpy arrays. - - Args: - string: Serialized Boxes string. - - Returns: - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - """ - boxes_proto = box_pb2.Boxes() - boxes_proto.ParseFromString(string) - return BoxesToArrays(boxes_proto) - - -def ReadFromFile(file_path): - """Helper function to load data from a Boxes proto format in a file. - - Args: - file_path: Path to file containing data. - - Returns: - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - """ - with tf.io.gfile.GFile(file_path, 'rb') as f: - return ParseFromString(f.read()) - - -def WriteToFile(file_path, boxes, scores, class_indices): - """Helper function to write data to a file in Boxes proto format. - - Args: - file_path: Path to file that will be written. - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - """ - serialized_data = SerializeToString(boxes, scores, class_indices) - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write(serialized_data) diff --git a/research/delf/delf/python/box_io_test.py b/research/delf/delf/python/box_io_test.py deleted file mode 100644 index c659185daee..00000000000 --- a/research/delf/delf/python/box_io_test.py +++ /dev/null @@ -1,82 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for box_io, the python interface of Boxes proto.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import numpy as np -import tensorflow as tf - -from delf import box_io - -FLAGS = flags.FLAGS - - -class BoxesIoTest(tf.test.TestCase): - - def _create_data(self): - """Creates data to be used in tests. - - Returns: - boxes: [N, 4] float array denoting bounding box coordinates, in format - [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - """ - boxes = np.arange(24, dtype=np.float32).reshape(6, 4) - scores = np.arange(6, dtype=np.float32) - class_indices = np.arange(6, dtype=np.int32) - - return boxes, scores, class_indices - - def testConversionAndBack(self): - boxes, scores, class_indices = self._create_data() - - serialized = box_io.SerializeToString(boxes, scores, class_indices) - parsed_data = box_io.ParseFromString(serialized) - - self.assertAllEqual(boxes, parsed_data[0]) - self.assertAllEqual(scores, parsed_data[1]) - self.assertAllEqual(class_indices, parsed_data[2]) - - def testWriteAndReadToFile(self): - boxes, scores, class_indices = self._create_data() - - filename = os.path.join(FLAGS.test_tmpdir, 'test.boxes') - box_io.WriteToFile(filename, boxes, scores, class_indices) - data_read = box_io.ReadFromFile(filename) - - self.assertAllEqual(boxes, data_read[0]) - self.assertAllEqual(scores, data_read[1]) - self.assertAllEqual(class_indices, data_read[2]) - - def testWriteAndReadToFileEmptyFile(self): - filename = os.path.join(FLAGS.test_tmpdir, 'test.box') - box_io.WriteToFile(filename, np.array([]), np.array([]), np.array([])) - data_read = box_io.ReadFromFile(filename) - - self.assertAllEqual(np.array([]), data_read[0]) - self.assertAllEqual(np.array([]), data_read[1]) - self.assertAllEqual(np.array([]), data_read[2]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datasets/__init__.py b/research/delf/delf/python/datasets/__init__.py deleted file mode 100644 index 8b137891791..00000000000 --- a/research/delf/delf/python/datasets/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/research/delf/delf/python/datasets/generic_dataset.py b/research/delf/delf/python/datasets/generic_dataset.py deleted file mode 100644 index a2e6d8f1e3c..00000000000 --- a/research/delf/delf/python/datasets/generic_dataset.py +++ /dev/null @@ -1,81 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Functions for generic image dataset creation.""" - -import os - -from delf.python.datasets import utils - - -class ImagesFromList(): - """A generic data loader that loads images from a list. - - Supports images of different sizes. - """ - - def __init__(self, root, image_paths, imsize=None, bounding_boxes=None, - loader=utils.default_loader): - """ImagesFromList object initialization. - - Args: - root: String, root directory path. - image_paths: List, relative image paths as strings. - imsize: Integer, defines the maximum size of longer image side. - bounding_boxes: List of (x1,y1,x2,y2) tuples to crop the query images. - loader: Callable, a function to load an image given its path. - - Raises: - ValueError: Raised if `image_paths` list is empty. - """ - # List of the full image filenames. - images_filenames = [os.path.join(root, image_path) for image_path in - image_paths] - - if not images_filenames: - raise ValueError("Dataset contains 0 images.") - - self.root = root - self.images = image_paths - self.imsize = imsize - self.images_filenames = images_filenames - self.bounding_boxes = bounding_boxes - self.loader = loader - - def __getitem__(self, index): - """Called to load an image at the given `index`. - - Args: - index: Integer, image index. - - Returns: - image: Tensor, loaded image. - """ - path = self.images_filenames[index] - - if self.bounding_boxes is not None: - img = self.loader(path, self.imsize, self.bounding_boxes[index]) - else: - img = self.loader(path, self.imsize) - - return img - - def __len__(self): - """Implements the built-in function len(). - - Returns: - len: Number of images in the dataset. - """ - return len(self.images_filenames) diff --git a/research/delf/delf/python/datasets/generic_dataset_test.py b/research/delf/delf/python/datasets/generic_dataset_test.py deleted file mode 100644 index 93c5de9598f..00000000000 --- a/research/delf/delf/python/datasets/generic_dataset_test.py +++ /dev/null @@ -1,60 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for generic dataset.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags - -import numpy as np -from PIL import Image -import tensorflow as tf - -from delf.python.datasets import generic_dataset - -FLAGS = flags.FLAGS - - -class GenericDatasetTest(tf.test.TestCase): - """Test functions for generic dataset.""" - - def testGenericDataset(self): - """Tests loading dummy images from list.""" - # Number of images to be created. - n = 2 - image_names = [] - - # Create and save `n` dummy images. - for i in range(n): - dummy_image = np.random.rand(1024, 750, 3) * 255 - img_out = Image.fromarray(dummy_image.astype('uint8')).convert('RGB') - filename = os.path.join(FLAGS.test_tmpdir, - 'test_image_{}.jpg'.format(i)) - img_out.save(filename) - image_names.append('test_image_{}.jpg'.format(i)) - - data = generic_dataset.ImagesFromList(root=FLAGS.test_tmpdir, - image_paths=image_names, - imsize=1024) - self.assertLen(data, n) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/README.md b/research/delf/delf/python/datasets/google_landmarks_dataset/README.md deleted file mode 100644 index 4f34b59aaa4..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/README.md +++ /dev/null @@ -1,123 +0,0 @@ -## GLDv2 code/models - -[![Paper](http://img.shields.io/badge/paper-arXiv.2004.01804-B3181B.svg)](https://arxiv.org/abs/2004.01804) - -These instructions can be used to reproduce results from the -[GLDv2 paper](https://arxiv.org/abs/2004.01804). We present here results on the -Revisited Oxford/Paris datasets since they are smaller and quicker to -reproduce -- but note that a very similar procedure can be used to obtain -results on the GLDv2 retrieval or recognition datasets. - -Note that this directory also contains code to compute GLDv2 metrics: see -`compute_retrieval_metrics.py`, `compute_recognition_metrics.py` and associated -file reading / metric computation modules. - -For more details on the dataset, please refer to its -[website](https://github.com/cvdfoundation/google-landmark). - -### Install DELF library - -To be able to use this code, please follow -[these instructions](../../../../INSTALL_INSTRUCTIONS.md) to properly install the -DELF library. - -### Download Revisited Oxford/Paris datasets - -```bash -mkdir -p ~/revisitop/data && cd ~/revisitop/data - -# Oxford dataset. -wget http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/oxbuild_images.tgz -mkdir oxford5k_images -tar -xvzf oxbuild_images.tgz -C oxford5k_images/ - -# Paris dataset. Download and move all images to same directory. -wget http://www.robots.ox.ac.uk/~vgg/data/parisbuildings/paris_1.tgz -wget http://www.robots.ox.ac.uk/~vgg/data/parisbuildings/paris_2.tgz -mkdir paris6k_images_tmp -tar -xvzf paris_1.tgz -C paris6k_images_tmp/ -tar -xvzf paris_2.tgz -C paris6k_images_tmp/ -mkdir paris6k_images -mv paris6k_images_tmp/paris/*/*.jpg paris6k_images/ - -# Revisited annotations. -wget http://cmp.felk.cvut.cz/revisitop/data/datasets/roxford5k/gnd_roxford5k.mat -wget http://cmp.felk.cvut.cz/revisitop/data/datasets/rparis6k/gnd_rparis6k.mat -``` - -### Download model - -```bash -# From models/research/delf/delf/python/datasets/google_landmarks_dataset -mkdir parameters && cd parameters - -# RN101-ArcFace model trained on GLDv2-clean. -wget https://storage.googleapis.com/delf/rn101_af_gldv2clean_20200814.tar.gz -tar -xvzf rn101_af_gldv2clean_20200814.tar.gz -``` - -### Feature extraction - -We present here commands for extraction on `roxford5k`. To extract on `rparis6k` -instead, please edit the arguments accordingly (especially the -`dataset_file_path` argument). - -#### Query feature extraction - -In the Revisited Oxford/Paris experimental protocol, query images must be the -cropped before feature extraction (this is done in the `extract_features` -script, when setting `image_set=query`). Note that this is specific to these -datasets, and not required for the GLDv2 retrieval/recognition datasets. - -Run query feature extraction as follows: - -```bash -# From models/research/delf/delf/python/datasets/google_landmarks_dataset -python3 ../../delg/extract_features.py \ - --delf_config_path rn101_af_gldv2clean_config.pbtxt \ - --dataset_file_path ~/revisitop/data/gnd_roxford5k.mat \ - --images_dir ~/revisitop/data/oxford5k_images \ - --image_set query \ - --output_features_dir ~/revisitop/data/oxford5k_features/query -``` - -#### Index feature extraction - -Run index feature extraction as follows: - -```bash -# From models/research/delf/delf/python/datasets/google_landmarks_dataset -python3 ../../delg/extract_features.py \ - --delf_config_path rn101_af_gldv2clean_config.pbtxt \ - --dataset_file_path ~/revisitop/data/gnd_roxford5k.mat \ - --images_dir ~/revisitop/data/oxford5k_images \ - --image_set index \ - --output_features_dir ~/revisitop/data/oxford5k_features/index -``` - -### Perform retrieval - -To run retrieval on `roxford5k`, the following command can be used: - -```bash -# From models/research/delf/delf/python/datasets/google_landmarks_dataset -python3 ../../delg/perform_retrieval.py \ - --dataset_file_path ~/revisitop/data/gnd_roxford5k.mat \ - --query_features_dir ~/revisitop/data/oxford5k_features/query \ - --index_features_dir ~/revisitop/data/oxford5k_features/index \ - --output_dir ~/revisitop/results/oxford5k -``` - -A file with named `metrics.txt` will be written to the path given in -`output_dir`. The contents should look approximately like: - -``` -hard - mAP=55.54 - mP@k[ 1 5 10] [88.57 80.86 70.14] - mR@k[ 1 5 10] [19.46 33.65 42.44] -medium - mAP=76.23 - mP@k[ 1 5 10] [95.71 92.86 90.43] - mR@k[ 1 5 10] [10.17 25.96 35.29] -``` diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/__init__.py b/research/delf/delf/python/datasets/google_landmarks_dataset/__init__.py deleted file mode 100644 index 4e24e0fb7c5..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module exposing Google Landmarks dataset for training.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=unused-import -from delf.python.datasets.google_landmarks_dataset import googlelandmarks -# pylint: enable=unused-import diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/compute_recognition_metrics.py b/research/delf/delf/python/datasets/google_landmarks_dataset/compute_recognition_metrics.py deleted file mode 100644 index 4c241ed5380..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/compute_recognition_metrics.py +++ /dev/null @@ -1,99 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Computes metrics for Google Landmarks Recognition dataset predictions. - -Metrics are written to stdout. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import sys - -from absl import app -from delf.python.datasets.google_landmarks_dataset import dataset_file_io -from delf.python.datasets.google_landmarks_dataset import metrics - -cmd_args = None - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read solution. - print('Reading solution...') - public_solution, private_solution, ignored_ids = dataset_file_io.ReadSolution( - cmd_args.solution_path, dataset_file_io.RECOGNITION_TASK_ID) - print('done!') - - # Read predictions. - print('Reading predictions...') - public_predictions, private_predictions = dataset_file_io.ReadPredictions( - cmd_args.predictions_path, set(public_solution.keys()), - set(private_solution.keys()), set(ignored_ids), - dataset_file_io.RECOGNITION_TASK_ID) - print('done!') - - # Global Average Precision. - print('**********************************************') - print('(Public) Global Average Precision: %f' % - metrics.GlobalAveragePrecision(public_predictions, public_solution)) - print('(Private) Global Average Precision: %f' % - metrics.GlobalAveragePrecision(private_predictions, private_solution)) - - # Global Average Precision ignoring non-landmark queries. - print('**********************************************') - print( - '(Public) Global Average Precision ignoring non-landmark queries: %f' % - metrics.GlobalAveragePrecision( - public_predictions, public_solution, ignore_non_gt_test_images=True)) - print( - '(Private) Global Average Precision ignoring non-landmark queries: %f' % - metrics.GlobalAveragePrecision( - private_predictions, private_solution, - ignore_non_gt_test_images=True)) - - # Top-1 accuracy. - print('**********************************************') - print('(Public) Top-1 accuracy: %.2f' % - (100.0 * metrics.Top1Accuracy(public_predictions, public_solution))) - print('(Private) Top-1 accuracy: %.2f' % - (100.0 * metrics.Top1Accuracy(private_predictions, private_solution))) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--predictions_path', - type=str, - default='/tmp/predictions.csv', - help=""" - Path to CSV predictions file, formatted with columns 'id,landmarks' (the - file should include a header). - """) - parser.add_argument( - '--solution_path', - type=str, - default='/tmp/solution.csv', - help=""" - Path to CSV solution file, formatted with columns 'id,landmarks,Usage' - (the file should include a header). - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/compute_retrieval_metrics.py b/research/delf/delf/python/datasets/google_landmarks_dataset/compute_retrieval_metrics.py deleted file mode 100644 index 231c320168c..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/compute_retrieval_metrics.py +++ /dev/null @@ -1,106 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Computes metrics for Google Landmarks Retrieval dataset predictions. - -Metrics are written to stdout. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import sys - -from absl import app -from delf.python.datasets.google_landmarks_dataset import dataset_file_io -from delf.python.datasets.google_landmarks_dataset import metrics - -cmd_args = None - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read solution. - print('Reading solution...') - public_solution, private_solution, ignored_ids = dataset_file_io.ReadSolution( - cmd_args.solution_path, dataset_file_io.RETRIEVAL_TASK_ID) - print('done!') - - # Read predictions. - print('Reading predictions...') - public_predictions, private_predictions = dataset_file_io.ReadPredictions( - cmd_args.predictions_path, set(public_solution.keys()), - set(private_solution.keys()), set(ignored_ids), - dataset_file_io.RETRIEVAL_TASK_ID) - print('done!') - - # Mean average precision. - print('**********************************************') - print('(Public) Mean Average Precision: %f' % - metrics.MeanAveragePrecision(public_predictions, public_solution)) - print('(Private) Mean Average Precision: %f' % - metrics.MeanAveragePrecision(private_predictions, private_solution)) - - # Mean precision@k. - print('**********************************************') - public_precisions = 100.0 * metrics.MeanPrecisions(public_predictions, - public_solution) - private_precisions = 100.0 * metrics.MeanPrecisions(private_predictions, - private_solution) - print('(Public) Mean precisions: P@1: %.2f, P@5: %.2f, P@10: %.2f, ' - 'P@50: %.2f, P@100: %.2f' % - (public_precisions[0], public_precisions[4], public_precisions[9], - public_precisions[49], public_precisions[99])) - print('(Private) Mean precisions: P@1: %.2f, P@5: %.2f, P@10: %.2f, ' - 'P@50: %.2f, P@100: %.2f' % - (private_precisions[0], private_precisions[4], private_precisions[9], - private_precisions[49], private_precisions[99])) - - # Mean/median position of first correct. - print('**********************************************') - public_mean_position, public_median_position = metrics.MeanMedianPosition( - public_predictions, public_solution) - private_mean_position, private_median_position = metrics.MeanMedianPosition( - private_predictions, private_solution) - print('(Public) Mean position: %.2f, median position: %.2f' % - (public_mean_position, public_median_position)) - print('(Private) Mean position: %.2f, median position: %.2f' % - (private_mean_position, private_median_position)) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--predictions_path', - type=str, - default='/tmp/predictions.csv', - help=""" - Path to CSV predictions file, formatted with columns 'id,images' (the - file should include a header). - """) - parser.add_argument( - '--solution_path', - type=str, - default='/tmp/solution.csv', - help=""" - Path to CSV solution file, formatted with columns 'id,images,Usage' - (the file should include a header). - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/dataset_file_io.py b/research/delf/delf/python/datasets/google_landmarks_dataset/dataset_file_io.py deleted file mode 100644 index 93f2785d78f..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/dataset_file_io.py +++ /dev/null @@ -1,159 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""IO module for files from Landmark recognition/retrieval challenges.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import csv - -import tensorflow as tf - -RECOGNITION_TASK_ID = 'recognition' -RETRIEVAL_TASK_ID = 'retrieval' - - -def ReadSolution(file_path, task): - """Reads solution from file, for a given task. - - Args: - file_path: Path to CSV file with solution. File contains a header. - task: Type of challenge task. Supported values: 'recognition', 'retrieval'. - - Returns: - public_solution: Dict mapping test image ID to list of ground-truth IDs, for - the Public subset of test images. If `task` == 'recognition', the IDs are - integers corresponding to landmark IDs. If `task` == 'retrieval', the IDs - are strings corresponding to index image IDs. - private_solution: Same as `public_solution`, but for the private subset of - test images. - ignored_ids: List of test images that are ignored in scoring. - - Raises: - ValueError: If Usage field is not Public, Private or Ignored; or if `task` - is not supported. - """ - public_solution = {} - private_solution = {} - ignored_ids = [] - with tf.io.gfile.GFile(file_path, 'r') as csv_file: - reader = csv.reader(csv_file) - next(reader, None) # Skip header. - for row in reader: - test_id = row[0] - if row[2] == 'Ignored': - ignored_ids.append(test_id) - else: - ground_truth_ids = [] - if task == RECOGNITION_TASK_ID: - if row[1]: - for landmark_id in row[1].split(' '): - ground_truth_ids.append(int(landmark_id)) - elif task == RETRIEVAL_TASK_ID: - for image_id in row[1].split(' '): - ground_truth_ids.append(image_id) - else: - raise ValueError('Unrecognized task: %s' % task) - - if row[2] == 'Public': - public_solution[test_id] = ground_truth_ids - elif row[2] == 'Private': - private_solution[test_id] = ground_truth_ids - else: - raise ValueError('Test image %s has unrecognized Usage tag %s' % - (row[0], row[2])) - - return public_solution, private_solution, ignored_ids - - -def ReadPredictions(file_path, public_ids, private_ids, ignored_ids, task): - """Reads predictions from file, for a given task. - - Args: - file_path: Path to CSV file with predictions. File contains a header. - public_ids: Set (or list) of test image IDs in Public subset of test images. - private_ids: Same as `public_ids`, but for the private subset of test - images. - ignored_ids: Set (or list) of test image IDs that are ignored in scoring and - are associated to no ground-truth. - task: Type of challenge task. Supported values: 'recognition', 'retrieval'. - - Returns: - public_predictions: Dict mapping test image ID to prediction, for the Public - subset of test images. If `task` == 'recognition', the prediction is a - dict with keys 'class' (integer) and 'score' (float). If `task` == - 'retrieval', the prediction is a list of strings corresponding to index - image IDs. - private_predictions: Same as `public_predictions`, but for the private - subset of test images. - - Raises: - ValueError: - - If test image ID is unrecognized/repeated; - - If `task` is not supported; - - If prediction is malformed. - """ - public_predictions = {} - private_predictions = {} - with tf.io.gfile.GFile(file_path, 'r') as csv_file: - reader = csv.reader(csv_file) - next(reader, None) # Skip header. - for row in reader: - # Skip row if empty. - if not row: - continue - - test_id = row[0] - - # Makes sure this query has not yet been seen. - if test_id in public_predictions: - raise ValueError('Test image %s is repeated.' % test_id) - if test_id in private_predictions: - raise ValueError('Test image %s is repeated' % test_id) - - # If ignored, skip it. - if test_id in ignored_ids: - continue - - # Only parse result if there is a prediction. - if row[1]: - prediction_split = row[1].split(' ') - # Remove empty spaces at end (if any). - if not prediction_split[-1]: - prediction_split = prediction_split[:-1] - - if task == RECOGNITION_TASK_ID: - if len(prediction_split) != 2: - raise ValueError('Prediction is malformed: there should only be 2 ' - 'elements in second column, but found %d for test ' - 'image %s' % (len(prediction_split), test_id)) - - landmark_id = int(prediction_split[0]) - score = float(prediction_split[1]) - prediction_entry = {'class': landmark_id, 'score': score} - elif task == RETRIEVAL_TASK_ID: - prediction_entry = prediction_split - else: - raise ValueError('Unrecognized task: %s' % task) - - if test_id in public_ids: - public_predictions[test_id] = prediction_entry - elif test_id in private_ids: - private_predictions[test_id] = prediction_entry - else: - raise ValueError('test_id %s is unrecognized' % test_id) - - return public_predictions, private_predictions diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/dataset_file_io_test.py b/research/delf/delf/python/datasets/google_landmarks_dataset/dataset_file_io_test.py deleted file mode 100644 index 8bd2ac5e0f3..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/dataset_file_io_test.py +++ /dev/null @@ -1,166 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for dataset file IO module.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import tensorflow as tf - -from delf.python.datasets.google_landmarks_dataset import dataset_file_io - -FLAGS = flags.FLAGS - - -class DatasetFileIoTest(tf.test.TestCase): - - def testReadRecognitionSolutionWorks(self): - # Define inputs. - file_path = os.path.join(FLAGS.test_tmpdir, 'recognition_solution.csv') - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write('id,landmarks,Usage\n') - f.write('0123456789abcdef,0 12,Public\n') - f.write('0223456789abcdef,,Public\n') - f.write('0323456789abcdef,100,Ignored\n') - f.write('0423456789abcdef,1,Private\n') - f.write('0523456789abcdef,,Ignored\n') - - # Run tested function. - (public_solution, private_solution, - ignored_ids) = dataset_file_io.ReadSolution( - file_path, dataset_file_io.RECOGNITION_TASK_ID) - - # Define expected results. - expected_public_solution = { - '0123456789abcdef': [0, 12], - '0223456789abcdef': [] - } - expected_private_solution = { - '0423456789abcdef': [1], - } - expected_ignored_ids = ['0323456789abcdef', '0523456789abcdef'] - - # Compare actual and expected results. - self.assertEqual(public_solution, expected_public_solution) - self.assertEqual(private_solution, expected_private_solution) - self.assertEqual(ignored_ids, expected_ignored_ids) - - def testReadRetrievalSolutionWorks(self): - # Define inputs. - file_path = os.path.join(FLAGS.test_tmpdir, 'retrieval_solution.csv') - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write('id,images,Usage\n') - f.write('0123456789abcdef,None,Ignored\n') - f.write('0223456789abcdef,fedcba9876543210 fedcba9876543200,Public\n') - f.write('0323456789abcdef,fedcba9876543200,Private\n') - f.write('0423456789abcdef,fedcba9876543220,Private\n') - f.write('0523456789abcdef,None,Ignored\n') - - # Run tested function. - (public_solution, private_solution, - ignored_ids) = dataset_file_io.ReadSolution( - file_path, dataset_file_io.RETRIEVAL_TASK_ID) - - # Define expected results. - expected_public_solution = { - '0223456789abcdef': ['fedcba9876543210', 'fedcba9876543200'], - } - expected_private_solution = { - '0323456789abcdef': ['fedcba9876543200'], - '0423456789abcdef': ['fedcba9876543220'], - } - expected_ignored_ids = ['0123456789abcdef', '0523456789abcdef'] - - # Compare actual and expected results. - self.assertEqual(public_solution, expected_public_solution) - self.assertEqual(private_solution, expected_private_solution) - self.assertEqual(ignored_ids, expected_ignored_ids) - - def testReadRecognitionPredictionsWorks(self): - # Define inputs. - file_path = os.path.join(FLAGS.test_tmpdir, 'recognition_predictions.csv') - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write('id,landmarks\n') - f.write('0123456789abcdef,12 0.1 \n') - f.write('0423456789abcdef,0 19.0\n') - f.write('0223456789abcdef,\n') - f.write('\n') - f.write('0523456789abcdef,14 0.01\n') - public_ids = ['0123456789abcdef', '0223456789abcdef'] - private_ids = ['0423456789abcdef'] - ignored_ids = ['0323456789abcdef', '0523456789abcdef'] - - # Run tested function. - public_predictions, private_predictions = dataset_file_io.ReadPredictions( - file_path, public_ids, private_ids, ignored_ids, - dataset_file_io.RECOGNITION_TASK_ID) - - # Define expected results. - expected_public_predictions = { - '0123456789abcdef': { - 'class': 12, - 'score': 0.1 - } - } - expected_private_predictions = { - '0423456789abcdef': { - 'class': 0, - 'score': 19.0 - } - } - - # Compare actual and expected results. - self.assertEqual(public_predictions, expected_public_predictions) - self.assertEqual(private_predictions, expected_private_predictions) - - def testReadRetrievalPredictionsWorks(self): - # Define inputs. - file_path = os.path.join(FLAGS.test_tmpdir, 'retrieval_predictions.csv') - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write('id,images\n') - f.write('0123456789abcdef,fedcba9876543250 \n') - f.write('0423456789abcdef,fedcba9876543260\n') - f.write('0223456789abcdef,fedcba9876543210 fedcba9876543200 ' - 'fedcba9876543220\n') - f.write('\n') - f.write('0523456789abcdef,\n') - public_ids = ['0223456789abcdef'] - private_ids = ['0323456789abcdef', '0423456789abcdef'] - ignored_ids = ['0123456789abcdef', '0523456789abcdef'] - - # Run tested function. - public_predictions, private_predictions = dataset_file_io.ReadPredictions( - file_path, public_ids, private_ids, ignored_ids, - dataset_file_io.RETRIEVAL_TASK_ID) - - # Define expected results. - expected_public_predictions = { - '0223456789abcdef': [ - 'fedcba9876543210', 'fedcba9876543200', 'fedcba9876543220' - ] - } - expected_private_predictions = {'0423456789abcdef': ['fedcba9876543260']} - - # Compare actual and expected results. - self.assertEqual(public_predictions, expected_public_predictions) - self.assertEqual(private_predictions, expected_private_predictions) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/googlelandmarks.py b/research/delf/delf/python/datasets/google_landmarks_dataset/googlelandmarks.py deleted file mode 100644 index b6122f5c79c..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/googlelandmarks.py +++ /dev/null @@ -1,186 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Google Landmarks Dataset(GLD). - -Placeholder for Google Landmarks dataset. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools - -import tensorflow as tf - - -class _GoogleLandmarksInfo(object): - """Metadata about the Google Landmarks dataset.""" - num_classes = {'gld_v1': 14951, 'gld_v2': 203094, 'gld_v2_clean': 81313} - - -class _DataAugmentationParams(object): - """Default parameters for augmentation.""" - # The following are used for training. - min_object_covered = 0.1 - aspect_ratio_range_min = 3. / 4 - aspect_ratio_range_max = 4. / 3 - area_range_min = 0.08 - area_range_max = 1.0 - max_attempts = 100 - update_labels = False - # 'central_fraction' is used for central crop in inference. - central_fraction = 0.875 - - random_reflection = False - - -def NormalizeImages(images, pixel_value_scale=0.5, pixel_value_offset=0.5): - """Normalize pixel values in image. - - Output is computed as - normalized_images = (images - pixel_value_offset) / pixel_value_scale. - - Args: - images: `Tensor`, images to normalize. - pixel_value_scale: float, scale. - pixel_value_offset: float, offset. - - Returns: - normalized_images: `Tensor`, normalized images. - """ - images = tf.cast(images, tf.float32) - normalized_images = tf.math.divide( - tf.subtract(images, pixel_value_offset), pixel_value_scale) - return normalized_images - - -def _ImageNetCrop(image, image_size): - """Imagenet-style crop with random bbox and aspect ratio. - - Args: - image: a `Tensor`, image to crop. - image_size: an `int`. The image size for the decoded image, on each side. - - Returns: - cropped_image: `Tensor`, cropped image. - """ - - params = _DataAugmentationParams() - bbox = tf.constant([0.0, 0.0, 1.0, 1.0], dtype=tf.float32, shape=[1, 1, 4]) - (bbox_begin, bbox_size, _) = tf.image.sample_distorted_bounding_box( - tf.shape(image), - bounding_boxes=bbox, - min_object_covered=params.min_object_covered, - aspect_ratio_range=(params.aspect_ratio_range_min, - params.aspect_ratio_range_max), - area_range=(params.area_range_min, params.area_range_max), - max_attempts=params.max_attempts, - use_image_if_no_bounding_boxes=True) - cropped_image = tf.slice(image, bbox_begin, bbox_size) - cropped_image.set_shape([None, None, 3]) - - cropped_image = tf.image.resize( - cropped_image, [image_size, image_size], method='area') - if params.random_reflection: - cropped_image = tf.image.random_flip_left_right(cropped_image) - - return cropped_image - - -def _ParseFunction(example, name_to_features, image_size, augmentation): - """Parse a single TFExample to get the image and label and process the image. - - Args: - example: a `TFExample`. - name_to_features: a `dict`. The mapping from feature names to its type. - image_size: an `int`. The image size for the decoded image, on each side. - augmentation: a `boolean`. True if the image will be augmented. - - Returns: - image: a `Tensor`. The processed image. - label: a `Tensor`. The ground-truth label. - """ - parsed_example = tf.io.parse_single_example(example, name_to_features) - # Parse to get image. - image = parsed_example['image/encoded'] - image = tf.io.decode_jpeg(image) - image = NormalizeImages( - image, pixel_value_scale=128.0, pixel_value_offset=128.0) - if augmentation: - image = _ImageNetCrop(image, image_size) - else: - image = tf.image.resize(image, [image_size, image_size]) - image.set_shape([image_size, image_size, 3]) - # Parse to get label. - label = parsed_example['image/class/label'] - - return image, label - - -def CreateDataset(file_pattern, - image_size=321, - batch_size=32, - augmentation=False, - seed=0): - """Creates a dataset. - - Args: - file_pattern: str, file pattern of the dataset files. - image_size: int, image size. - batch_size: int, batch size. - augmentation: bool, whether to apply augmentation. - seed: int, seed for shuffling the dataset. - - Returns: - tf.data.TFRecordDataset. - """ - - filenames = tf.io.gfile.glob(file_pattern) - - dataset = tf.data.TFRecordDataset(filenames) - dataset = dataset.repeat().shuffle(buffer_size=100, seed=seed) - - # Create a description of the features. - feature_description = { - 'image/height': tf.io.FixedLenFeature([], tf.int64, default_value=0), - 'image/width': tf.io.FixedLenFeature([], tf.int64, default_value=0), - 'image/channels': tf.io.FixedLenFeature([], tf.int64, default_value=0), - 'image/format': tf.io.FixedLenFeature([], tf.string, default_value=''), - 'image/id': tf.io.FixedLenFeature([], tf.string, default_value=''), - 'image/filename': tf.io.FixedLenFeature([], tf.string, default_value=''), - 'image/encoded': tf.io.FixedLenFeature([], tf.string, default_value=''), - 'image/class/label': tf.io.FixedLenFeature([], tf.int64, default_value=0), - } - - customized_parse_func = functools.partial( - _ParseFunction, - name_to_features=feature_description, - image_size=image_size, - augmentation=augmentation) - dataset = dataset.map(customized_parse_func) - dataset = dataset.batch(batch_size) - - return dataset - - -def GoogleLandmarksInfo(): - """Returns metadata information on the Google Landmarks dataset. - - Returns: - object _GoogleLandmarksInfo containing metadata about the GLD dataset. - """ - return _GoogleLandmarksInfo() diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/metrics.py b/research/delf/delf/python/datasets/google_landmarks_dataset/metrics.py deleted file mode 100644 index 1516be9d856..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/metrics.py +++ /dev/null @@ -1,254 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Python module to compute metrics for Google Landmarks dataset.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np - - -def _CountPositives(solution): - """Counts number of test images with non-empty ground-truth in `solution`. - - Args: - solution: Dict mapping test image ID to list of ground-truth IDs. - - Returns: - count: Number of test images with non-empty ground-truth. - """ - count = 0 - for v in solution.values(): - if v: - count += 1 - - return count - - -def GlobalAveragePrecision(predictions, - recognition_solution, - ignore_non_gt_test_images=False): - """Computes global average precision for recognition prediction. - - Args: - predictions: Dict mapping test image ID to a dict with keys 'class' - (integer) and 'score' (float). - recognition_solution: Dict mapping test image ID to list of ground-truth - landmark IDs. - ignore_non_gt_test_images: If True, ignore test images which do not have - associated ground-truth landmark IDs. For the Google Landmark Recognition - challenge, this should be set to False. - - Returns: - gap: Global average precision score (float). - """ - # Compute number of expected results. - num_positives = _CountPositives(recognition_solution) - - gap = 0.0 - total_predictions = 0 - correct_predictions = 0 - - # Sort predictions according to Kaggle's convention: - # - first by score (descending); - # - then by key (ascending); - # - then by class (ascending). - sorted_predictions_by_key_class = sorted( - predictions.items(), key=lambda item: (item[0], item[1]['class'])) - sorted_predictions = sorted( - sorted_predictions_by_key_class, - key=lambda item: item[1]['score'], - reverse=True) - - # Loop over sorted predictions (descending order) and compute GAPs. - for key, prediction in sorted_predictions: - if ignore_non_gt_test_images and not recognition_solution[key]: - continue - - total_predictions += 1 - if prediction['class'] in recognition_solution[key]: - correct_predictions += 1 - gap += correct_predictions / total_predictions - - gap /= num_positives - - return gap - - -def Top1Accuracy(predictions, recognition_solution): - """Computes top-1 accuracy for recognition prediction. - - Note that test images without ground-truth are ignored. - - Args: - predictions: Dict mapping test image ID to a dict with keys 'class' - (integer) and 'score' (float). - recognition_solution: Dict mapping test image ID to list of ground-truth - landmark IDs. - - Returns: - accuracy: Top-1 accuracy (float). - """ - # Loop over test images in solution. If it has at least one class label, we - # check if the predicion is correct. - num_correct_predictions = 0 - num_test_images_with_ground_truth = 0 - for key, ground_truth in recognition_solution.items(): - if ground_truth: - num_test_images_with_ground_truth += 1 - if key in predictions: - if predictions[key]['class'] in ground_truth: - num_correct_predictions += 1 - - return num_correct_predictions / num_test_images_with_ground_truth - - -def MeanAveragePrecision(predictions, retrieval_solution, max_predictions=100): - """Computes mean average precision for retrieval prediction. - - Args: - predictions: Dict mapping test image ID to a list of strings corresponding - to index image IDs. - retrieval_solution: Dict mapping test image ID to list of ground-truth image - IDs. - max_predictions: Maximum number of predictions per query to take into - account. For the Google Landmark Retrieval challenge, this should be set - to 100. - - Returns: - mean_ap: Mean average precision score (float). - - Raises: - ValueError: If a test image in `predictions` is not included in - `retrieval_solutions`. - """ - # Compute number of test images. - num_test_images = len(retrieval_solution.keys()) - - # Loop over predictions for each query and compute mAP. - mean_ap = 0.0 - for key, prediction in predictions.items(): - if key not in retrieval_solution: - raise ValueError('Test image %s is not part of retrieval_solution' % key) - - # Loop over predicted images, keeping track of those which were already - # used (duplicates are skipped). - ap = 0.0 - already_predicted = set() - num_expected_retrieved = min(len(retrieval_solution[key]), max_predictions) - num_correct = 0 - for i in range(min(len(prediction), max_predictions)): - if prediction[i] not in already_predicted: - if prediction[i] in retrieval_solution[key]: - num_correct += 1 - ap += num_correct / (i + 1) - already_predicted.add(prediction[i]) - - ap /= num_expected_retrieved - mean_ap += ap - - mean_ap /= num_test_images - - return mean_ap - - -def MeanPrecisions(predictions, retrieval_solution, max_predictions=100): - """Computes mean precisions for retrieval prediction. - - Args: - predictions: Dict mapping test image ID to a list of strings corresponding - to index image IDs. - retrieval_solution: Dict mapping test image ID to list of ground-truth image - IDs. - max_predictions: Maximum number of predictions per query to take into - account. - - Returns: - mean_precisions: NumPy array with mean precisions at ranks 1 through - `max_predictions`. - - Raises: - ValueError: If a test image in `predictions` is not included in - `retrieval_solutions`. - """ - # Compute number of test images. - num_test_images = len(retrieval_solution.keys()) - - # Loop over predictions for each query and compute precisions@k. - precisions = np.zeros((num_test_images, max_predictions)) - count_test_images = 0 - for key, prediction in predictions.items(): - if key not in retrieval_solution: - raise ValueError('Test image %s is not part of retrieval_solution' % key) - - # Loop over predicted images, keeping track of those which were already - # used (duplicates are skipped). - already_predicted = set() - num_correct = 0 - for i in range(max_predictions): - if i < len(prediction): - if prediction[i] not in already_predicted: - if prediction[i] in retrieval_solution[key]: - num_correct += 1 - already_predicted.add(prediction[i]) - precisions[count_test_images, i] = num_correct / (i + 1) - count_test_images += 1 - - mean_precisions = np.mean(precisions, axis=0) - - return mean_precisions - - -def MeanMedianPosition(predictions, retrieval_solution, max_predictions=100): - """Computes mean and median positions of first correct image. - - Args: - predictions: Dict mapping test image ID to a list of strings corresponding - to index image IDs. - retrieval_solution: Dict mapping test image ID to list of ground-truth image - IDs. - max_predictions: Maximum number of predictions per query to take into - account. - - Returns: - mean_position: Float. - median_position: Float. - - Raises: - ValueError: If a test image in `predictions` is not included in - `retrieval_solutions`. - """ - # Compute number of test images. - num_test_images = len(retrieval_solution.keys()) - - # Loop over predictions for each query to find first correct ranked image. - positions = (max_predictions + 1) * np.ones((num_test_images)) - count_test_images = 0 - for key, prediction in predictions.items(): - if key not in retrieval_solution: - raise ValueError('Test image %s is not part of retrieval_solution' % key) - - for i in range(min(len(prediction), max_predictions)): - if prediction[i] in retrieval_solution[key]: - positions[count_test_images] = i + 1 - break - - count_test_images += 1 - - mean_position = np.mean(positions) - median_position = np.median(positions) - - return mean_position, median_position diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/metrics_test.py b/research/delf/delf/python/datasets/google_landmarks_dataset/metrics_test.py deleted file mode 100644 index ee8a443de16..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/metrics_test.py +++ /dev/null @@ -1,219 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for Google Landmarks dataset metric computation.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from delf.python.datasets.google_landmarks_dataset import metrics - - -def _CreateRecognitionSolution(): - """Creates recognition solution to be used in tests. - - Returns: - solution: Dict mapping test image ID to list of ground-truth landmark IDs. - """ - return { - '0123456789abcdef': [0, 12], - '0223456789abcdef': [100, 200, 300], - '0323456789abcdef': [1], - '0423456789abcdef': [], - '0523456789abcdef': [], - } - - -def _CreateRecognitionPredictions(): - """Creates recognition predictions to be used in tests. - - Returns: - predictions: Dict mapping test image ID to a dict with keys 'class' - (integer) and 'score' (float). - """ - return { - '0223456789abcdef': { - 'class': 0, - 'score': 0.01 - }, - '0323456789abcdef': { - 'class': 1, - 'score': 10.0 - }, - '0423456789abcdef': { - 'class': 150, - 'score': 15.0 - }, - } - - -def _CreateRetrievalSolution(): - """Creates retrieval solution to be used in tests. - - Returns: - solution: Dict mapping test image ID to list of ground-truth image IDs. - """ - return { - '0123456789abcdef': ['fedcba9876543210', 'fedcba9876543220'], - '0223456789abcdef': ['fedcba9876543210'], - '0323456789abcdef': [ - 'fedcba9876543230', 'fedcba9876543240', 'fedcba9876543250' - ], - '0423456789abcdef': ['fedcba9876543230'], - } - - -def _CreateRetrievalPredictions(): - """Creates retrieval predictions to be used in tests. - - Returns: - predictions: Dict mapping test image ID to a list with predicted index image - ids. - """ - return { - '0223456789abcdef': ['fedcba9876543200', 'fedcba9876543210'], - '0323456789abcdef': ['fedcba9876543240'], - '0423456789abcdef': ['fedcba9876543230', 'fedcba9876543240'], - } - - -class MetricsTest(tf.test.TestCase): - - def testGlobalAveragePrecisionWorks(self): - # Define input. - predictions = _CreateRecognitionPredictions() - solution = _CreateRecognitionSolution() - - # Run tested function. - gap = metrics.GlobalAveragePrecision(predictions, solution) - - # Define expected results. - expected_gap = 0.166667 - - # Compare actual and expected results. - self.assertAllClose(gap, expected_gap) - - def testGlobalAveragePrecisionIgnoreNonGroundTruthWorks(self): - # Define input. - predictions = _CreateRecognitionPredictions() - solution = _CreateRecognitionSolution() - - # Run tested function. - gap = metrics.GlobalAveragePrecision( - predictions, solution, ignore_non_gt_test_images=True) - - # Define expected results. - expected_gap = 0.333333 - - # Compare actual and expected results. - self.assertAllClose(gap, expected_gap) - - def testTop1AccuracyWorks(self): - # Define input. - predictions = _CreateRecognitionPredictions() - solution = _CreateRecognitionSolution() - - # Run tested function. - accuracy = metrics.Top1Accuracy(predictions, solution) - - # Define expected results. - expected_accuracy = 0.333333 - - # Compare actual and expected results. - self.assertAllClose(accuracy, expected_accuracy) - - def testMeanAveragePrecisionWorks(self): - # Define input. - predictions = _CreateRetrievalPredictions() - solution = _CreateRetrievalSolution() - - # Run tested function. - mean_ap = metrics.MeanAveragePrecision(predictions, solution) - - # Define expected results. - expected_mean_ap = 0.458333 - - # Compare actual and expected results. - self.assertAllClose(mean_ap, expected_mean_ap) - - def testMeanAveragePrecisionMaxPredictionsWorks(self): - # Define input. - predictions = _CreateRetrievalPredictions() - solution = _CreateRetrievalSolution() - - # Run tested function. - mean_ap = metrics.MeanAveragePrecision( - predictions, solution, max_predictions=1) - - # Define expected results. - expected_mean_ap = 0.5 - - # Compare actual and expected results. - self.assertAllClose(mean_ap, expected_mean_ap) - - def testMeanPrecisionsWorks(self): - # Define input. - predictions = _CreateRetrievalPredictions() - solution = _CreateRetrievalSolution() - - # Run tested function. - mean_precisions = metrics.MeanPrecisions( - predictions, solution, max_predictions=2) - - # Define expected results. - expected_mean_precisions = [0.5, 0.375] - - # Compare actual and expected results. - self.assertAllClose(mean_precisions, expected_mean_precisions) - - def testMeanMedianPositionWorks(self): - # Define input. - predictions = _CreateRetrievalPredictions() - solution = _CreateRetrievalSolution() - - # Run tested function. - mean_position, median_position = metrics.MeanMedianPosition( - predictions, solution) - - # Define expected results. - expected_mean_position = 26.25 - expected_median_position = 1.5 - - # Compare actual and expected results. - self.assertAllClose(mean_position, expected_mean_position) - self.assertAllClose(median_position, expected_median_position) - - def testMeanMedianPositionMaxPredictionsWorks(self): - # Define input. - predictions = _CreateRetrievalPredictions() - solution = _CreateRetrievalSolution() - - # Run tested function. - mean_position, median_position = metrics.MeanMedianPosition( - predictions, solution, max_predictions=1) - - # Define expected results. - expected_mean_position = 1.5 - expected_median_position = 1.5 - - # Compare actual and expected results. - self.assertAllClose(mean_position, expected_mean_position) - self.assertAllClose(median_position, expected_median_position) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datasets/google_landmarks_dataset/rn101_af_gldv2clean_config.pbtxt b/research/delf/delf/python/datasets/google_landmarks_dataset/rn101_af_gldv2clean_config.pbtxt deleted file mode 100644 index 6a065d51280..00000000000 --- a/research/delf/delf/python/datasets/google_landmarks_dataset/rn101_af_gldv2clean_config.pbtxt +++ /dev/null @@ -1,10 +0,0 @@ -use_local_features: false -use_global_features: true -model_path: "parameters/rn101_af_gldv2clean_20200814" -image_scales: 0.70710677 -image_scales: 1.0 -image_scales: 1.4142135 -delf_global_config { - use_pca: false -} -max_image_size: 1024 diff --git a/research/delf/delf/python/datasets/revisited_op/__init__.py b/research/delf/delf/python/datasets/revisited_op/__init__.py deleted file mode 100644 index 1a8b35fb1b4..00000000000 --- a/research/delf/delf/python/datasets/revisited_op/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module for revisited Oxford and Paris datasets.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=unused-import -from delf.python.datasets.revisited_op import dataset -# pylint: enable=unused-import diff --git a/research/delf/delf/python/datasets/revisited_op/dataset.py b/research/delf/delf/python/datasets/revisited_op/dataset.py deleted file mode 100644 index ae3020cd345..00000000000 --- a/research/delf/delf/python/datasets/revisited_op/dataset.py +++ /dev/null @@ -1,535 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Python library to parse ground-truth/evaluate on Revisited datasets.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import pickle - -import numpy as np -from scipy.io import matlab -import tensorflow as tf - -_GROUND_TRUTH_KEYS = ['easy', 'hard', 'junk'] - -DATASET_NAMES = ['roxford5k', 'rparis6k'] - - -def ReadDatasetFile(dataset_file_path): - """Reads dataset file in Revisited Oxford/Paris ".mat" format. - - Args: - dataset_file_path: Path to dataset file, in .mat format. - - Returns: - query_list: List of query image names. - index_list: List of index image names. - ground_truth: List containing ground-truth information for dataset. Each - entry is a dict corresponding to the ground-truth information for a query. - The dict may have keys 'easy', 'hard', or 'junk', mapping to a NumPy - array of integers; additionally, it has a key 'bbx' mapping to a NumPy - array of floats with bounding box coordinates. - """ - with tf.io.gfile.GFile(dataset_file_path, 'rb') as f: - cfg = matlab.loadmat(f) - - # Parse outputs according to the specificities of the dataset file. - query_list = [str(im_array[0]) for im_array in np.squeeze(cfg['qimlist'])] - index_list = [str(im_array[0]) for im_array in np.squeeze(cfg['imlist'])] - ground_truth_raw = np.squeeze(cfg['gnd']) - ground_truth = [] - for query_ground_truth_raw in ground_truth_raw: - query_ground_truth = {} - for ground_truth_key in _GROUND_TRUTH_KEYS: - if ground_truth_key in query_ground_truth_raw.dtype.names: - adjusted_labels = query_ground_truth_raw[ground_truth_key] - 1 - query_ground_truth[ground_truth_key] = adjusted_labels.flatten() - - query_ground_truth['bbx'] = np.squeeze(query_ground_truth_raw['bbx']) - ground_truth.append(query_ground_truth) - - return query_list, index_list, ground_truth - - -def _ParseGroundTruth(ok_list, junk_list): - """Constructs dictionary of ok/junk indices for a data subset and query. - - Args: - ok_list: List of NumPy arrays containing true positive indices for query. - junk_list: List of NumPy arrays containing ignored indices for query. - - Returns: - ok_junk_dict: Dict mapping 'ok' and 'junk' strings to NumPy array of - indices. - """ - ok_junk_dict = {} - ok_junk_dict['ok'] = np.concatenate(ok_list) - ok_junk_dict['junk'] = np.concatenate(junk_list) - return ok_junk_dict - - -def ParseEasyMediumHardGroundTruth(ground_truth): - """Parses easy/medium/hard ground-truth from Revisited datasets. - - Args: - ground_truth: Usually the output from ReadDatasetFile(). List containing - ground-truth information for dataset. Each entry is a dict corresponding - to the ground-truth information for a query. The dict must have keys - 'easy', 'hard', and 'junk', mapping to a NumPy array of integers. - - Returns: - easy_ground_truth: List containing ground-truth information for easy subset - of dataset. Each entry is a dict corresponding to the ground-truth - information for a query. The dict has keys 'ok' and 'junk', mapping to a - NumPy array of integers. - medium_ground_truth: Same as `easy_ground_truth`, but for the medium subset. - hard_ground_truth: Same as `easy_ground_truth`, but for the hard subset. - """ - num_queries = len(ground_truth) - - easy_ground_truth = [] - medium_ground_truth = [] - hard_ground_truth = [] - for i in range(num_queries): - easy_ground_truth.append( - _ParseGroundTruth([ground_truth[i]['easy']], - [ground_truth[i]['junk'], ground_truth[i]['hard']])) - medium_ground_truth.append( - _ParseGroundTruth([ground_truth[i]['easy'], ground_truth[i]['hard']], - [ground_truth[i]['junk']])) - hard_ground_truth.append( - _ParseGroundTruth([ground_truth[i]['hard']], - [ground_truth[i]['junk'], ground_truth[i]['easy']])) - - return easy_ground_truth, medium_ground_truth, hard_ground_truth - - -def AdjustPositiveRanks(positive_ranks, junk_ranks): - """Adjusts positive ranks based on junk ranks. - - Args: - positive_ranks: Sorted 1D NumPy integer array. - junk_ranks: Sorted 1D NumPy integer array. - - Returns: - adjusted_positive_ranks: Sorted 1D NumPy array. - """ - if not junk_ranks.size: - return positive_ranks - - adjusted_positive_ranks = positive_ranks - j = 0 - for i, positive_index in enumerate(positive_ranks): - while (j < len(junk_ranks) and positive_index > junk_ranks[j]): - j += 1 - - adjusted_positive_ranks[i] -= j - - return adjusted_positive_ranks - - -def ComputeAveragePrecision(positive_ranks): - """Computes average precision according to dataset convention. - - It assumes that `positive_ranks` contains the ranks for all expected positive - index images to be retrieved. If `positive_ranks` is empty, returns - `average_precision` = 0. - - Note that average precision computation here does NOT use the finite sum - method (see - https://en.wikipedia.org/wiki/Evaluation_measures_(information_retrieval)#Average_precision) - which is common in information retrieval literature. Instead, the method - implemented here integrates over the precision-recall curve by averaging two - adjacent precision points, then multiplying by the recall step. This is the - convention for the Revisited Oxford/Paris datasets. - - Args: - positive_ranks: Sorted 1D NumPy integer array, zero-indexed. - - Returns: - average_precision: Float. - """ - average_precision = 0.0 - - num_expected_positives = len(positive_ranks) - if not num_expected_positives: - return average_precision - - recall_step = 1.0 / num_expected_positives - for i, rank in enumerate(positive_ranks): - if not rank: - left_precision = 1.0 - else: - left_precision = i / rank - - right_precision = (i + 1) / (rank + 1) - average_precision += (left_precision + right_precision) * recall_step / 2 - - return average_precision - - -def ComputePRAtRanks(positive_ranks, desired_pr_ranks): - """Computes precision/recall at desired ranks. - - It assumes that `positive_ranks` contains the ranks for all expected positive - index images to be retrieved. If `positive_ranks` is empty, return all-zeros - `precisions`/`recalls`. - - If a desired rank is larger than the last positive rank, its precision is - computed based on the last positive rank. For example, if `desired_pr_ranks` - is [10] and `positive_ranks` = [0, 7] --> `precisions` = [0.25], `recalls` = - [1.0]. - - Args: - positive_ranks: 1D NumPy integer array, zero-indexed. - desired_pr_ranks: List of integers containing the desired precision/recall - ranks to be reported. Eg, if precision@1/recall@1 and - precision@10/recall@10 are desired, this should be set to [1, 10]. - - Returns: - precisions: Precision @ `desired_pr_ranks` (NumPy array of - floats, with shape [len(desired_pr_ranks)]). - recalls: Recall @ `desired_pr_ranks` (NumPy array of floats, with - shape [len(desired_pr_ranks)]). - """ - num_desired_pr_ranks = len(desired_pr_ranks) - precisions = np.zeros([num_desired_pr_ranks]) - recalls = np.zeros([num_desired_pr_ranks]) - - num_expected_positives = len(positive_ranks) - if not num_expected_positives: - return precisions, recalls - - positive_ranks_one_indexed = positive_ranks + 1 - for i, desired_pr_rank in enumerate(desired_pr_ranks): - recalls[i] = np.sum( - positive_ranks_one_indexed <= desired_pr_rank) / num_expected_positives - - # If `desired_pr_rank` is larger than last positive's rank, only compute - # precision with respect to last positive's position. - precision_rank = min(max(positive_ranks_one_indexed), desired_pr_rank) - precisions[i] = np.sum( - positive_ranks_one_indexed <= precision_rank) / precision_rank - - return precisions, recalls - - -def ComputeMetrics(sorted_index_ids, ground_truth, desired_pr_ranks): - """Computes metrics for retrieval results on the Revisited datasets. - - If there are no valid ground-truth index images for a given query, the metric - results for the given query (`average_precisions`, `precisions` and `recalls`) - are set to NaN, and they are not taken into account when computing the - aggregated metrics (`mean_average_precision`, `mean_precisions` and - `mean_recalls`) over all queries. - - Args: - sorted_index_ids: Integer NumPy array of shape [#queries, #index_images]. - For each query, contains an array denoting the most relevant index images, - sorted from most to least relevant. - ground_truth: List containing ground-truth information for dataset. Each - entry is a dict corresponding to the ground-truth information for a query. - The dict has keys 'ok' and 'junk', mapping to a NumPy array of integers. - desired_pr_ranks: List of integers containing the desired precision/recall - ranks to be reported. Eg, if precision@1/recall@1 and - precision@10/recall@10 are desired, this should be set to [1, 10]. The - largest item should be <= #index_images. - - Returns: - mean_average_precision: Mean average precision (float). - mean_precisions: Mean precision @ `desired_pr_ranks` (NumPy array of - floats, with shape [len(desired_pr_ranks)]). - mean_recalls: Mean recall @ `desired_pr_ranks` (NumPy array of floats, with - shape [len(desired_pr_ranks)]). - average_precisions: Average precision for each query (NumPy array of floats, - with shape [#queries]). - precisions: Precision @ `desired_pr_ranks`, for each query (NumPy array of - floats, with shape [#queries, len(desired_pr_ranks)]). - recalls: Recall @ `desired_pr_ranks`, for each query (NumPy array of - floats, with shape [#queries, len(desired_pr_ranks)]). - - Raises: - ValueError: If largest desired PR rank in `desired_pr_ranks` > - #index_images. - """ - num_queries, num_index_images = sorted_index_ids.shape - num_desired_pr_ranks = len(desired_pr_ranks) - - sorted_desired_pr_ranks = sorted(desired_pr_ranks) - - if sorted_desired_pr_ranks[-1] > num_index_images: - raise ValueError( - 'Requested PR ranks up to %d, however there are only %d images' % - (sorted_desired_pr_ranks[-1], num_index_images)) - - # Instantiate all outputs, then loop over each query and gather metrics. - mean_average_precision = 0.0 - mean_precisions = np.zeros([num_desired_pr_ranks]) - mean_recalls = np.zeros([num_desired_pr_ranks]) - average_precisions = np.zeros([num_queries]) - precisions = np.zeros([num_queries, num_desired_pr_ranks]) - recalls = np.zeros([num_queries, num_desired_pr_ranks]) - num_empty_gt_queries = 0 - for i in range(num_queries): - ok_index_images = ground_truth[i]['ok'] - junk_index_images = ground_truth[i]['junk'] - - if not ok_index_images.size: - average_precisions[i] = float('nan') - precisions[i, :] = float('nan') - recalls[i, :] = float('nan') - num_empty_gt_queries += 1 - continue - - positive_ranks = np.arange(num_index_images)[np.in1d( - sorted_index_ids[i], ok_index_images)] - junk_ranks = np.arange(num_index_images)[np.in1d(sorted_index_ids[i], - junk_index_images)] - - adjusted_positive_ranks = AdjustPositiveRanks(positive_ranks, junk_ranks) - - average_precisions[i] = ComputeAveragePrecision(adjusted_positive_ranks) - precisions[i, :], recalls[i, :] = ComputePRAtRanks(adjusted_positive_ranks, - desired_pr_ranks) - - mean_average_precision += average_precisions[i] - mean_precisions += precisions[i, :] - mean_recalls += recalls[i, :] - - # Normalize aggregated metrics by number of queries. - num_valid_queries = num_queries - num_empty_gt_queries - mean_average_precision /= num_valid_queries - mean_precisions /= num_valid_queries - mean_recalls /= num_valid_queries - - return (mean_average_precision, mean_precisions, mean_recalls, - average_precisions, precisions, recalls) - - -def SaveMetricsFile(mean_average_precision, mean_precisions, mean_recalls, - pr_ranks, output_path): - """Saves aggregated retrieval metrics to text file. - - Args: - mean_average_precision: Dict mapping each dataset protocol to a float. - mean_precisions: Dict mapping each dataset protocol to a NumPy array of - floats with shape [len(pr_ranks)]. - mean_recalls: Dict mapping each dataset protocol to a NumPy array of floats - with shape [len(pr_ranks)]. - pr_ranks: List of integers. - output_path: Full file path. - """ - with tf.io.gfile.GFile(output_path, 'w') as f: - for k in sorted(mean_average_precision.keys()): - f.write('{}\n mAP={}\n mP@k{} {}\n mR@k{} {}\n'.format( - k, np.around(mean_average_precision[k] * 100, decimals=2), - np.array(pr_ranks), np.around(mean_precisions[k] * 100, decimals=2), - np.array(pr_ranks), np.around(mean_recalls[k] * 100, decimals=2))) - - -def _ParseSpaceSeparatedStringsInBrackets(line, prefixes, ind): - """Parses line containing space-separated strings in brackets. - - Args: - line: String, containing line in metrics file with mP@k or mR@k figures. - prefixes: Tuple/list of strings, containing valid prefixes. - ind: Integer indicating which field within brackets is parsed. - - Yields: - entry: String format entry. - - Raises: - ValueError: If input line does not contain a valid prefix. - """ - for prefix in prefixes: - if line.startswith(prefix): - line = line[len(prefix):] - break - else: - raise ValueError('Line %s is malformed, cannot find valid prefixes' % line) - - for entry in line.split('[')[ind].split(']')[0].split(): - yield entry - - -def _ParsePrRanks(line): - """Parses PR ranks from mP@k line in metrics file. - - Args: - line: String, containing line in metrics file with mP@k figures. - - Returns: - pr_ranks: List of integers, containing used ranks. - - Raises: - ValueError: If input line is malformed. - """ - return [ - int(pr_rank) for pr_rank in _ParseSpaceSeparatedStringsInBrackets( - line, [' mP@k['], 0) if pr_rank - ] - - -def _ParsePrScores(line, num_pr_ranks): - """Parses PR scores from line in metrics file. - - Args: - line: String, containing line in metrics file with mP@k or mR@k figures. - num_pr_ranks: Integer, number of scores that should be in output list. - - Returns: - pr_scores: List of floats, containing scores. - - Raises: - ValueError: If input line is malformed. - """ - pr_scores = [ - float(pr_score) for pr_score in _ParseSpaceSeparatedStringsInBrackets( - line, (' mP@k[', ' mR@k['), 1) if pr_score - ] - - if len(pr_scores) != num_pr_ranks: - raise ValueError('Line %s is malformed, expected %d scores but found %d' % - (line, num_pr_ranks, len(pr_scores))) - - return pr_scores - - -def ReadMetricsFile(metrics_path): - """Reads aggregated retrieval metrics from text file. - - Args: - metrics_path: Full file path, containing aggregated retrieval metrics. - - Returns: - mean_average_precision: Dict mapping each dataset protocol to a float. - pr_ranks: List of integer ranks used in aggregated recall/precision metrics. - mean_precisions: Dict mapping each dataset protocol to a NumPy array of - floats with shape [len(`pr_ranks`)]. - mean_recalls: Dict mapping each dataset protocol to a NumPy array of floats - with shape [len(`pr_ranks`)]. - - Raises: - ValueError: If input file is malformed. - """ - with tf.io.gfile.GFile(metrics_path, 'r') as f: - file_contents_stripped = [l.rstrip() for l in f] - - if len(file_contents_stripped) % 4: - raise ValueError( - 'Malformed input %s: number of lines must be a multiple of 4, ' - 'but it is %d' % (metrics_path, len(file_contents_stripped))) - - mean_average_precision = {} - pr_ranks = [] - mean_precisions = {} - mean_recalls = {} - protocols = set() - for i in range(0, len(file_contents_stripped), 4): - protocol = file_contents_stripped[i] - if protocol in protocols: - raise ValueError( - 'Malformed input %s: protocol %s is found a second time' % - (metrics_path, protocol)) - protocols.add(protocol) - - # Parse mAP. - mean_average_precision[protocol] = float( - file_contents_stripped[i + 1].split('=')[1]) / 100.0 - - # Parse (or check consistency of) pr_ranks. - parsed_pr_ranks = _ParsePrRanks(file_contents_stripped[i + 2]) - if not pr_ranks: - pr_ranks = parsed_pr_ranks - else: - if parsed_pr_ranks != pr_ranks: - raise ValueError('Malformed input %s: inconsistent PR ranks' % - metrics_path) - - # Parse mean precisions. - mean_precisions[protocol] = np.array( - _ParsePrScores(file_contents_stripped[i + 2], len(pr_ranks)), - dtype=float) / 100.0 - - # Parse mean recalls. - mean_recalls[protocol] = np.array( - _ParsePrScores(file_contents_stripped[i + 3], len(pr_ranks)), - dtype=float) / 100.0 - - return mean_average_precision, pr_ranks, mean_precisions, mean_recalls - - -def CreateConfigForTestDataset(dataset, dir_main): - """Creates the configuration dictionary for the test dataset. - - Args: - dataset: String, dataset name: either 'roxford5k' or 'rparis6k'. - dir_main: String, path to the folder containing ground truth files. - - Returns: - cfg: Dataset configuration in a form of dictionary. The configuration - includes: - `gnd_fname` - path to the ground truth file for the dataset, - `ext` and `qext` - image extensions for the images in the test dataset - and the query images, - `dir_data` - path to the folder containing ground truth files, - `dir_images` - path to the folder containing images, - `n` and `nq` - number of images and query images in the dataset - respectively, - `im_fname` and `qim_fname` - functions providing paths for the dataset - and query images respectively, - `dataset` - test dataset name. - - Raises: - ValueError: If an unknown dataset name is provided as an argument. - """ - dataset = dataset.lower() - - def _ConfigImname(cfg, i): - return os.path.join(cfg['dir_images'], cfg['imlist'][i] + cfg['ext']) - - def _ConfigQimname(cfg, i): - return os.path.join(cfg['dir_images'], cfg['qimlist'][i] + cfg['qext']) - - if dataset not in DATASET_NAMES: - raise ValueError('Unknown dataset: {}!'.format(dataset)) - - # Loading imlist, qimlist, and gnd in configuration as a dictionary. - gnd_fname = os.path.join(dir_main, 'gnd_{}.pkl'.format(dataset)) - with tf.io.gfile.GFile(gnd_fname, 'rb') as f: - cfg = pickle.load(f) - cfg['gnd_fname'] = gnd_fname - if dataset == 'rparis6k': - dir_images = 'paris6k_images' - elif dataset == 'roxford5k': - dir_images = 'oxford5k_images' - - cfg['ext'] = '.jpg' - cfg['qext'] = '.jpg' - cfg['dir_data'] = os.path.join(dir_main) - cfg['dir_images'] = os.path.join(cfg['dir_data'], dir_images) - - cfg['n'] = len(cfg['imlist']) - cfg['nq'] = len(cfg['qimlist']) - - cfg['im_fname'] = _ConfigImname - cfg['qim_fname'] = _ConfigQimname - - cfg['dataset'] = dataset - - return cfg diff --git a/research/delf/delf/python/datasets/revisited_op/dataset_test.py b/research/delf/delf/python/datasets/revisited_op/dataset_test.py deleted file mode 100644 index 04caa64f098..00000000000 --- a/research/delf/delf/python/datasets/revisited_op/dataset_test.py +++ /dev/null @@ -1,288 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the python library parsing Revisited Oxford/Paris datasets.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import numpy as np -import tensorflow as tf - -from delf.python.datasets.revisited_op import dataset - -FLAGS = flags.FLAGS - - -class DatasetTest(tf.test.TestCase): - - def testParseEasyMediumHardGroundTruth(self): - # Define input. - ground_truth = [{ - 'easy': np.array([10, 56, 100]), - 'hard': np.array([0]), - 'junk': np.array([6, 90]) - }, { - 'easy': np.array([], dtype='int64'), - 'hard': [5], - 'junk': [99, 100] - }, { - 'easy': [33], - 'hard': [66, 99], - 'junk': np.array([], dtype='int64') - }] - - # Run tested function. - (easy_ground_truth, medium_ground_truth, - hard_ground_truth) = dataset.ParseEasyMediumHardGroundTruth(ground_truth) - - # Define expected outputs. - expected_easy_ground_truth = [{ - 'ok': np.array([10, 56, 100]), - 'junk': np.array([6, 90, 0]) - }, { - 'ok': np.array([], dtype='int64'), - 'junk': np.array([99, 100, 5]) - }, { - 'ok': np.array([33]), - 'junk': np.array([66, 99]) - }] - expected_medium_ground_truth = [{ - 'ok': np.array([10, 56, 100, 0]), - 'junk': np.array([6, 90]) - }, { - 'ok': np.array([5]), - 'junk': np.array([99, 100]) - }, { - 'ok': np.array([33, 66, 99]), - 'junk': np.array([], dtype='int64') - }] - expected_hard_ground_truth = [{ - 'ok': np.array([0]), - 'junk': np.array([6, 90, 10, 56, 100]) - }, { - 'ok': np.array([5]), - 'junk': np.array([99, 100]) - }, { - 'ok': np.array([66, 99]), - 'junk': np.array([33]) - }] - - # Compare actual versus expected. - def _AssertListOfDictsOfArraysAreEqual(ground_truth, expected_ground_truth): - """Helper function to compare ground-truth data. - - Args: - ground_truth: List of dicts of arrays. - expected_ground_truth: List of dicts of arrays. - """ - self.assertEqual(len(ground_truth), len(expected_ground_truth)) - - for i, ground_truth_entry in enumerate(ground_truth): - self.assertEqual(sorted(ground_truth_entry.keys()), ['junk', 'ok']) - self.assertAllEqual(ground_truth_entry['junk'], - expected_ground_truth[i]['junk']) - self.assertAllEqual(ground_truth_entry['ok'], - expected_ground_truth[i]['ok']) - - _AssertListOfDictsOfArraysAreEqual(easy_ground_truth, - expected_easy_ground_truth) - _AssertListOfDictsOfArraysAreEqual(medium_ground_truth, - expected_medium_ground_truth) - _AssertListOfDictsOfArraysAreEqual(hard_ground_truth, - expected_hard_ground_truth) - - def testAdjustPositiveRanksWorks(self): - # Define inputs. - positive_ranks = np.array([0, 2, 6, 10, 20]) - junk_ranks = np.array([1, 8, 9, 30]) - - # Run tested function. - adjusted_positive_ranks = dataset.AdjustPositiveRanks( - positive_ranks, junk_ranks) - - # Define expected output. - expected_adjusted_positive_ranks = [0, 1, 5, 7, 17] - - # Compare actual versus expected. - self.assertAllEqual(adjusted_positive_ranks, - expected_adjusted_positive_ranks) - - def testComputeAveragePrecisionWorks(self): - # Define input. - positive_ranks = [0, 2, 5] - - # Run tested function. - average_precision = dataset.ComputeAveragePrecision(positive_ranks) - - # Define expected output. - expected_average_precision = 0.677778 - - # Compare actual versus expected. - self.assertAllClose(average_precision, expected_average_precision) - - def testComputePRAtRanksWorks(self): - # Define inputs. - positive_ranks = np.array([0, 2, 5]) - desired_pr_ranks = np.array([1, 5, 10]) - - # Run tested function. - precisions, recalls = dataset.ComputePRAtRanks(positive_ranks, - desired_pr_ranks) - - # Define expected outputs. - expected_precisions = [1.0, 0.4, 0.5] - expected_recalls = [0.333333, 0.666667, 1.0] - - # Compare actual versus expected. - self.assertAllClose(precisions, expected_precisions) - self.assertAllClose(recalls, expected_recalls) - - def testComputeMetricsWorks(self): - # Define inputs: 3 queries. For the last one, there are no expected images - # to be retrieved - sorted_index_ids = np.array([[4, 2, 0, 1, 3], [0, 2, 4, 1, 3], - [0, 1, 2, 3, 4]]) - ground_truth = [{ - 'ok': np.array([0, 1]), - 'junk': np.array([2]) - }, { - 'ok': np.array([0, 4]), - 'junk': np.array([], dtype='int64') - }, { - 'ok': np.array([], dtype='int64'), - 'junk': np.array([], dtype='int64') - }] - desired_pr_ranks = [1, 2, 5] - - # Run tested function. - (mean_average_precision, mean_precisions, mean_recalls, average_precisions, - precisions, recalls) = dataset.ComputeMetrics(sorted_index_ids, - ground_truth, - desired_pr_ranks) - - # Define expected outputs. - expected_mean_average_precision = 0.604167 - expected_mean_precisions = [0.5, 0.5, 0.666667] - expected_mean_recalls = [0.25, 0.5, 1.0] - expected_average_precisions = [0.416667, 0.791667, float('nan')] - expected_precisions = [[0.0, 0.5, 0.666667], [1.0, 0.5, 0.666667], - [float('nan'), - float('nan'), - float('nan')]] - expected_recalls = [[0.0, 0.5, 1.0], [0.5, 0.5, 1.0], - [float('nan'), float('nan'), - float('nan')]] - - # Compare actual versus expected. - self.assertAllClose(mean_average_precision, expected_mean_average_precision) - self.assertAllClose(mean_precisions, expected_mean_precisions) - self.assertAllClose(mean_recalls, expected_mean_recalls) - self.assertAllClose(average_precisions, expected_average_precisions) - self.assertAllClose(precisions, expected_precisions) - self.assertAllClose(recalls, expected_recalls) - - def testSaveMetricsFileWorks(self): - # Define inputs. - mean_average_precision = {'hard': 0.7, 'medium': 0.9} - mean_precisions = { - 'hard': np.array([1.0, 0.8]), - 'medium': np.array([1.0, 1.0]) - } - mean_recalls = { - 'hard': np.array([0.5, 0.8]), - 'medium': np.array([0.5, 1.0]) - } - pr_ranks = [1, 5] - output_path = os.path.join(FLAGS.test_tmpdir, 'metrics.txt') - - # Run tested function. - dataset.SaveMetricsFile(mean_average_precision, mean_precisions, - mean_recalls, pr_ranks, output_path) - - # Define expected results. - expected_metrics = ('hard\n' - ' mAP=70.0\n' - ' mP@k[1 5] [100. 80.]\n' - ' mR@k[1 5] [50. 80.]\n' - 'medium\n' - ' mAP=90.0\n' - ' mP@k[1 5] [100. 100.]\n' - ' mR@k[1 5] [ 50. 100.]\n') - - # Parse actual results, and compare to expected. - with tf.io.gfile.GFile(output_path) as f: - metrics = f.read() - - self.assertEqual(metrics, expected_metrics) - - def testSaveAndReadMetricsWorks(self): - # Define inputs. - mean_average_precision = {'hard': 0.7, 'medium': 0.9} - mean_precisions = { - 'hard': np.array([1.0, 0.8]), - 'medium': np.array([1.0, 1.0]) - } - mean_recalls = { - 'hard': np.array([0.5, 0.8]), - 'medium': np.array([0.5, 1.0]) - } - pr_ranks = [1, 5] - output_path = os.path.join(FLAGS.test_tmpdir, 'metrics.txt') - - # Run tested functions. - dataset.SaveMetricsFile(mean_average_precision, mean_precisions, - mean_recalls, pr_ranks, output_path) - (read_mean_average_precision, read_pr_ranks, read_mean_precisions, - read_mean_recalls) = dataset.ReadMetricsFile(output_path) - - # Compares actual and expected metrics. - self.assertEqual(read_mean_average_precision, mean_average_precision) - self.assertEqual(read_pr_ranks, pr_ranks) - self.assertEqual(read_mean_precisions.keys(), mean_precisions.keys()) - self.assertAllEqual(read_mean_precisions['hard'], mean_precisions['hard']) - self.assertAllEqual(read_mean_precisions['medium'], - mean_precisions['medium']) - self.assertEqual(read_mean_recalls.keys(), mean_recalls.keys()) - self.assertAllEqual(read_mean_recalls['hard'], mean_recalls['hard']) - self.assertAllEqual(read_mean_recalls['medium'], mean_recalls['medium']) - - def testReadMetricsWithRepeatedProtocolFails(self): - # Define inputs. - input_path = os.path.join(FLAGS.test_tmpdir, 'metrics.txt') - with tf.io.gfile.GFile(input_path, 'w') as f: - f.write('hard\n' - ' mAP=70.0\n' - ' mP@k[1 5] [ 100. 80.]\n' - ' mR@k[1 5] [ 50. 80.]\n' - 'medium\n' - ' mAP=90.0\n' - ' mP@k[1 5] [ 100. 100.]\n' - ' mR@k[1 5] [ 50. 100.]\n' - 'medium\n' - ' mAP=90.0\n' - ' mP@k[1 5] [ 100. 100.]\n' - ' mR@k[1 5] [ 50. 100.]\n') - - # Run tested functions. - with self.assertRaisesRegex(ValueError, 'Malformed input'): - dataset.ReadMetricsFile(input_path) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datasets/sfm120k/__init__.py b/research/delf/delf/python/datasets/sfm120k/__init__.py deleted file mode 100644 index 8f8fc48f4a6..00000000000 --- a/research/delf/delf/python/datasets/sfm120k/__init__.py +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module exposing Sfm120k dataset for training.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=unused-import -from delf.python.datasets.sfm120k import sfm120k -# pylint: enable=unused-import diff --git a/research/delf/delf/python/datasets/sfm120k/dataset_download.py b/research/delf/delf/python/datasets/sfm120k/dataset_download.py deleted file mode 100644 index ba6b17feaf2..00000000000 --- a/research/delf/delf/python/datasets/sfm120k/dataset_download.py +++ /dev/null @@ -1,103 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Structure-from-Motion dataset (Sfm120k) download function.""" - -import os - -import tensorflow as tf - - -def download_train(data_dir): - """Checks, and, if required, downloads the necessary files for the training. - - Checks if the data necessary for running the example training script exist. - If not, it downloads it in the following folder structure: - DATA_ROOT/train/retrieval-SfM-120k/ : folder with rsfm120k images and db - files. - DATA_ROOT/train/retrieval-SfM-30k/ : folder with rsfm30k images and db - files. - """ - - # Create data folder if does not exist. - if not tf.io.gfile.exists(data_dir): - tf.io.gfile.mkdir(data_dir) - - # Create datasets folder if does not exist. - datasets_dir = os.path.join(data_dir, 'train') - if not tf.io.gfile.exists(datasets_dir): - tf.io.gfile.mkdir(datasets_dir) - - # Download folder train/retrieval-SfM-120k/. - src_dir = 'http://cmp.felk.cvut.cz/cnnimageretrieval/data/train/ims' - dst_dir = os.path.join(datasets_dir, 'retrieval-SfM-120k', 'ims') - download_file = 'ims.tar.gz' - if not tf.io.gfile.exists(dst_dir): - src_file = os.path.join(src_dir, download_file) - dst_file = os.path.join(dst_dir, download_file) - print('>> Image directory does not exist. Creating: {}'.format(dst_dir)) - tf.io.gfile.makedirs(dst_dir) - print('>> Downloading ims.tar.gz...') - os.system('wget {} -O {}'.format(src_file, dst_file)) - print('>> Extracting {}...'.format(dst_file)) - os.system('tar -zxf {} -C {}'.format(dst_file, dst_dir)) - print('>> Extracted, deleting {}...'.format(dst_file)) - os.system('rm {}'.format(dst_file)) - - # Create symlink for train/retrieval-SfM-30k/. - dst_dir_old = os.path.join(datasets_dir, 'retrieval-SfM-120k', 'ims') - dst_dir = os.path.join(datasets_dir, 'retrieval-SfM-30k', 'ims') - if not (tf.io.gfile.exists(dst_dir) or os.path.islink(dst_dir)): - tf.io.gfile.makedirs(os.path.join(datasets_dir, 'retrieval-SfM-30k')) - os.system('ln -s {} {}'.format(dst_dir_old, dst_dir)) - print( - '>> Created symbolic link from retrieval-SfM-120k/ims to ' - 'retrieval-SfM-30k/ims') - - # Download db files. - src_dir = 'http://cmp.felk.cvut.cz/cnnimageretrieval/data/train/dbs' - datasets = ['retrieval-SfM-120k', 'retrieval-SfM-30k'] - for dataset in datasets: - dst_dir = os.path.join(datasets_dir, dataset) - if dataset == 'retrieval-SfM-120k': - download_files = ['{}.pkl'.format(dataset), - '{}-whiten.pkl'.format(dataset)] - download_eccv2020 = '{}-val-eccv2020.pkl'.format(dataset) - elif dataset == 'retrieval-SfM-30k': - download_files = ['{}-whiten.pkl'.format(dataset)] - download_eccv2020 = None - - if not tf.io.gfile.exists(dst_dir): - print('>> Dataset directory does not exist. Creating: {}'.format( - dst_dir)) - tf.io.gfile.mkdir(dst_dir) - - for i in range(len(download_files)): - src_file = os.path.join(src_dir, download_files[i]) - dst_file = os.path.join(dst_dir, download_files[i]) - if not os.path.isfile(dst_file): - print('>> DB file {} does not exist. Downloading...'.format( - download_files[i])) - os.system('wget {} -O {}'.format(src_file, dst_file)) - - if download_eccv2020: - eccv2020_dst_file = os.path.join(dst_dir, download_eccv2020) - if not os.path.isfile(eccv2020_dst_file): - eccv2020_src_dir = \ - "http://ptak.felk.cvut.cz/personal/toliageo/share/how/dataset/" - eccv2020_dst_file = os.path.join(dst_dir, download_eccv2020) - eccv2020_src_file = os.path.join(eccv2020_src_dir, - download_eccv2020) - os.system('wget {} -O {}'.format(eccv2020_src_file, - eccv2020_dst_file)) diff --git a/research/delf/delf/python/datasets/sfm120k/sfm120k.py b/research/delf/delf/python/datasets/sfm120k/sfm120k.py deleted file mode 100644 index 3be14b10e1e..00000000000 --- a/research/delf/delf/python/datasets/sfm120k/sfm120k.py +++ /dev/null @@ -1,143 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Structure-from-Motion dataset (Sfm120k) module. - -[1] From Single Image Query to Detailed 3D Reconstruction. -Johannes L. Schonberger, Filip Radenovic, Ondrej Chum, Jan-Michael Frahm. -The related paper can be found at: https://ieeexplore.ieee.org/document/7299148. -""" - -import os -import pickle -import tensorflow as tf - -from delf.python.datasets import tuples_dataset -from delf.python.datasets import utils - - -def id2filename(image_id, prefix): - """Creates a training image path out of its id name. - - Used for the image mapping in the Sfm120k datset. - - Args: - image_id: String, image id. - prefix: String, root directory where images are saved. - - Returns: - filename: String, full image filename. - """ - if prefix: - return os.path.join(prefix, image_id[-2:], image_id[-4:-2], image_id[-6:-4], - image_id) - else: - return os.path.join(image_id[-2:], image_id[-4:-2], image_id[-6:-4], - image_id) - - -class _Sfm120k(tuples_dataset.TuplesDataset): - """Structure-from-Motion (Sfm120k) dataset instance. - - The dataset contains the image names lists for training and validation, - the cluster ID (3D model ID) for each image and indices forming - query-positive pairs of images. The images are loaded per epoch and resized - on the fly to the desired dimensionality. - """ - - def __init__(self, mode, data_root, imsize=None, num_negatives=5, - num_queries=2000, pool_size=20000, loader=utils.default_loader, - eccv2020=False): - """Structure-from-Motion (Sfm120k) dataset initialization. - - Args: - mode: Either 'train' or 'val'. - data_root: Path to the root directory of the dataset. - imsize: Integer, defines the maximum size of longer image side. - num_negatives: Integer, number of negative images per one query. - num_queries: Integer, number of query images. - pool_size: Integer, size of the negative image pool, from where the - hard-negative images are chosen. - loader: Callable, a function to load an image given its path. - eccv2020: Bool, whether to use a new validation dataset used with ECCV - 2020 paper (https://arxiv.org/abs/2007.13172). - - Raises: - ValueError: Raised if `mode` is not one of 'train' or 'val'. - """ - if mode not in ['train', 'val']: - raise ValueError( - "`mode` argument should be either 'train' or 'val', passed as a " - "String.") - - # Setting up the paths for the dataset. - if eccv2020: - name = "retrieval-SfM-120k-val-eccv2020" - else: - name = "retrieval-SfM-120k" - db_root = os.path.join(data_root, 'train/retrieval-SfM-120k') - ims_root = os.path.join(db_root, 'ims/') - - # Loading the dataset db file. - db_filename = os.path.join(db_root, '{}.pkl'.format(name)) - - with tf.io.gfile.GFile(db_filename, 'rb') as f: - db = pickle.load(f)[mode] - - # Setting full paths for the dataset images. - self.images = [id2filename(img_name, None) for - img_name in db['cids']] - - # Initializing tuples dataset. - super().__init__(name, mode, db_root, imsize, num_negatives, num_queries, - pool_size, loader, ims_root) - - def Sfm120kInfo(self): - """Metadata for the Sfm120k dataset. - - The dataset contains the image names lists for training and - validation, the cluster ID (3D model ID) for each image and indices - forming query-positive pairs of images. The images are loaded per epoch - and resized on the fly to the desired dimensionality. - - Returns: - info: dictionary with the dataset parameters. - """ - info = {'train': {'clusters': 91642, 'pidxs': 181697, 'qidxs': 181697}, - 'val': {'clusters': 6403, 'pidxs': 1691, 'qidxs': 1691}} - return info - - -def CreateDataset(mode, data_root, imsize=None, num_negatives=5, - num_queries=2000, pool_size=20000, - loader=utils.default_loader, eccv2020=False): - '''Creates Structure-from-Motion (Sfm120k) dataset. - - Args: - mode: String, either 'train' or 'val'. - data_root: Path to the root directory of the dataset. - imsize: Integer, defines the maximum size of longer image side. - num_negatives: Integer, number of negative images per one query. - num_queries: Integer, number of query images. - pool_size: Integer, size of the negative image pool, from where the - hard-negative images are chosen. - loader: Callable, a function to load an image given its path. - eccv2020: Bool, whether to use a new validation dataset used with ECCV - 2020 paper (https://arxiv.org/abs/2007.13172). - - Returns: - sfm120k: Sfm120k dataset instance. - ''' - return _Sfm120k(mode, data_root, imsize, num_negatives, num_queries, - pool_size, loader, eccv2020) diff --git a/research/delf/delf/python/datasets/sfm120k/sfm120k_test.py b/research/delf/delf/python/datasets/sfm120k/sfm120k_test.py deleted file mode 100644 index 5b253200eb2..00000000000 --- a/research/delf/delf/python/datasets/sfm120k/sfm120k_test.py +++ /dev/null @@ -1,37 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for Sfm120k dataset module.""" - -import tensorflow as tf - -from delf.python.datasets.sfm120k import sfm120k - - -class Sfm120kTest(tf.test.TestCase): - """Tests for Sfm120k dataset module.""" - - def testId2Filename(self): - """Tests conversion of image id to full path mapping.""" - image_id = "29fdc243aeb939388cfdf2d081dc080e" - prefix = "train/retrieval-SfM-120k/ims/" - path = sfm120k.id2filename(image_id, prefix) - expected_path = "train/retrieval-SfM-120k/ims/0e/08/dc" \ - "/29fdc243aeb939388cfdf2d081dc080e" - self.assertEqual(path, expected_path) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datasets/tuples_dataset.py b/research/delf/delf/python/datasets/tuples_dataset.py deleted file mode 100644 index 8449c060fb1..00000000000 --- a/research/delf/delf/python/datasets/tuples_dataset.py +++ /dev/null @@ -1,328 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tuple dataset module. - -Based on the Radenovic et al. ECCV16: CNN image retrieval learns from BoW. -For more information refer to https://arxiv.org/abs/1604.02426. -""" - -import os -import pickle - -import numpy as np -import tensorflow as tf - -from delf.python.datasets import utils as image_loading_utils -from delf.python.training import global_features_utils -from delf.python.training.model import global_model - - -class TuplesDataset(): - """Data loader that loads training and validation tuples. - - After initialization, the function create_epoch_tuples() should be called to - create the dataset tuples. After that, the dataset can be iterated through - using next() function. - Tuples are based on Radenovic et al. ECCV16 work: CNN image retrieval - learns from BoW. For more information refer to - https://arxiv.org/abs/1604.02426. - """ - - def __init__(self, name, mode, data_root, imsize=None, num_negatives=5, - num_queries=2000, pool_size=20000, - loader=image_loading_utils.default_loader, ims_root=None): - """TuplesDataset object initialization. - - Args: - name: String, dataset name. I.e. 'retrieval-sfm-120k'. - mode: 'train' or 'val' for training and validation parts of dataset. - data_root: Path to the root directory of the dataset. - imsize: Integer, defines the maximum size of longer image side transform. - num_negatives: Integer, number of negative images for a query image in a - training tuple. - num_queries: Integer, number of query images to be processed in one epoch. - pool_size: Integer, size of the negative image pool, from where the - hard-negative images are re-mined. - loader: Callable, a function to load an image given its path. - ims_root: String, image root directory. - - Raises: - ValueError: If mode is not either 'train' or 'val'. - """ - - if mode not in ['train', 'val']: - raise ValueError( - "`mode` argument should be either 'train' or 'val', passed as a " - "String.") - - # Loading db. - db_filename = os.path.join(data_root, '{}.pkl'.format(name)) - with tf.io.gfile.GFile(db_filename, 'rb') as f: - db = pickle.load(f)[mode] - - # Initializing tuples dataset. - self._ims_root = data_root if ims_root is None else ims_root - self._name = name - self._mode = mode - self._imsize = imsize - self._clusters = db['cluster'] - self._query_pool = db['qidxs'] - self._positive_pool = db['pidxs'] - - if not hasattr(self, 'images'): - self.images = db['ids'] - - # Size of training subset for an epoch. - self._num_negatives = num_negatives - self._num_queries = min(num_queries, len(self._query_pool)) - self._pool_size = min(pool_size, len(self.images)) - self._qidxs = None - self._pidxs = None - self._nidxs = None - - self._loader = loader - self._print_freq = 10 - # Indexer for the iterator. - self._n = 0 - - def __iter__(self): - """Function for making TupleDataset an iterator. - - Returns: - iter: The iterator object itself (TupleDataset). - """ - return self - - def __next__(self): - """Function for making TupleDataset an iterator. - - Returns: - next: The next item in the sequence (next dataset image tuple). - """ - if self._n < len(self._qidxs): - result = self.__getitem__(self._n) - self._n += 1 - return result - else: - raise StopIteration - - def _img_names_to_full_path(self, image_list): - """Converts list of image names to the list of full paths to the images. - - Args: - image_list: Image names, either a list or a single image path. - - Returns: - image_full_paths: List of full paths to the images. - """ - if not isinstance(image_list, list): - return os.path.join(self._ims_root, image_list) - return [os.path.join(self._ims_root, img_name) for img_name in image_list] - - def __getitem__(self, index): - """Called to load an image tuple at the given `index`. - - Args: - index: Integer, index. - - Returns: - output: Tuple [q,p,n1,...,nN, target], loaded 'train'/'val' tuple at - index of qidxs. `q` is the query image tensor, `p` is the - corresponding positive image tensor, `n1`,...,`nN` are the negatives - associated with the query. `target` is a tensor (with the shape [2+N]) - of integer labels corresponding to the tuple list: query (-1), - positive (1), negative (0). - - Raises: - ValueError: Raised if the query indexes list `qidxs` is empty. - """ - if self.__len__() == 0: - raise ValueError( - "List `qidxs` is empty. Run `dataset.create_epoch_tuples(net)` " - "method to create subset for `train`/`val`.") - - output = [] - # Query image. - output.append(self._loader( - self._img_names_to_full_path(self.images[self._qidxs[index]]), - self._imsize)) - # Positive image. - output.append(self._loader( - self._img_names_to_full_path(self.images[self._pidxs[index]]), - self._imsize)) - # Negative images. - for nidx in self._nidxs[index]: - output.append(self._loader( - self._img_names_to_full_path(self.images[nidx]), - self._imsize)) - # Labels for the query (-1), positive (1), negative (0) images in the tuple. - target = tf.convert_to_tensor([-1, 1] + [0] * self._num_negatives) - output.append(target) - - return tuple(output) - - def __len__(self): - """Called to implement the built-in function len(). - - Returns: - len: Integer, number of query images. - """ - if self._qidxs is None: - return 0 - return len(self._qidxs) - - def __repr__(self): - """Metadata for the TupleDataset. - - Returns: - meta: String, containing TupleDataset meta. - """ - fmt_str = self.__class__.__name__ + '\n' - fmt_str += '\tName and mode: {} {}\n'.format(self._name, self._mode) - fmt_str += '\tNumber of images: {}\n'.format(len(self.images)) - fmt_str += '\tNumber of training tuples: {}\n'.format(len(self._query_pool)) - fmt_str += '\tNumber of negatives per tuple: {}\n'.format( - self._num_negatives) - fmt_str += '\tNumber of tuples processed in an epoch: {}\n'.format( - self._num_queries) - fmt_str += '\tPool size for negative remining: {}\n'.format(self._pool_size) - return fmt_str - - def create_epoch_tuples(self, net): - """Creates epoch tuples with the hard-negative re-mining. - - Negative examples are selected from clusters different than the cluster - of the query image, as the clusters are ideally non-overlaping. For - every query image we choose hard-negatives, that is, non-matching images - with the most similar descriptor. Hard-negatives depend on the current - CNN parameters. K-nearest neighbors from all non-matching images are - selected. Query images are selected randomly. Positives examples are - fixed for the related query image during the whole training process. - - Args: - net: Model, network to be used for negative re-mining. - - Raises: - ValueError: If the pool_size is smaller than the number of negative - images per tuple. - - Returns: - avg_l2: Float, average negative L2-distance. - """ - self._n = 0 - - if self._num_negatives < self._pool_size: - raise ValueError("Unable to create epoch tuples. Negative pool_size " - "should be larger than the number of negative images " - "per tuple.") - - global_features_utils.debug_and_log( - '>> Creating tuples for an epoch of {}-{}...'.format(self._name, - self._mode), - True) - global_features_utils.debug_and_log(">> Used network: ", True) - global_features_utils.debug_and_log(net.meta_repr(), True) - - ## Selecting queries. - # Draw `num_queries` random queries for the tuples. - idx_list = np.arange(len(self._query_pool)) - np.random.shuffle(idx_list) - idxs2query_pool = idx_list[:self._num_queries] - self._qidxs = [self._query_pool[i] for i in idxs2query_pool] - - ## Selecting positive pairs. - # Positives examples are fixed for each query during the whole training - # process. - self._pidxs = [self._positive_pool[i] for i in idxs2query_pool] - - ## Selecting negative pairs. - # If `num_negatives` = 0 create dummy nidxs. - # Useful when only positives used for training. - if self._num_negatives == 0: - self._nidxs = [[] for _ in range(len(self._qidxs))] - return 0 - - # Draw pool_size random images for pool of negatives images. - neg_idx_list = np.arange(len(self.images)) - np.random.shuffle(neg_idx_list) - neg_images_idxs = neg_idx_list[:self._pool_size] - - global_features_utils.debug_and_log( - '>> Extracting descriptors for query images...', debug=True) - - img_list = self._img_names_to_full_path([self.images[i] for i in - self._qidxs]) - qvecs = global_model.extract_global_descriptors_from_list( - net, - images=img_list, - image_size=self._imsize, - print_freq=self._print_freq) - - global_features_utils.debug_and_log( - '>> Extracting descriptors for negative pool...', debug=True) - - poolvecs = global_model.extract_global_descriptors_from_list( - net, - images=self._img_names_to_full_path([self.images[i] for i in - neg_images_idxs]), - image_size=self._imsize, - print_freq=self._print_freq) - - global_features_utils.debug_and_log('>> Searching for hard negatives...', - debug=True) - - # Compute dot product scores and ranks. - scores = tf.linalg.matmul(poolvecs, qvecs, transpose_a=True) - ranks = tf.argsort(scores, axis=0, direction='DESCENDING') - - sum_ndist = 0. - n_ndist = 0. - - # Selection of negative examples. - self._nidxs = [] - - for q, qidx in enumerate(self._qidxs): - # We are not using the query cluster, those images are potentially - # positive. - qcluster = self._clusters[qidx] - clusters = [qcluster] - nidxs = [] - rank = 0 - - while len(nidxs) < self._num_negatives: - if rank >= tf.shape(ranks)[0]: - raise ValueError("Unable to create epoch tuples. Number of required " - "negative images is larger than the number of " - "clusters in the dataset.") - potential = neg_images_idxs[ranks[rank, q]] - # Take at most one image from the same cluster. - if not self._clusters[potential] in clusters: - nidxs.append(potential) - clusters.append(self._clusters[potential]) - dist = tf.norm(qvecs[:, q] - poolvecs[:, ranks[rank, q]], - axis=0).numpy() - sum_ndist += dist - n_ndist += 1 - rank += 1 - - self._nidxs.append(nidxs) - - global_features_utils.debug_and_log( - '>> Average negative l2-distance: {:.2f}'.format( - sum_ndist / n_ndist)) - - # Return average negative L2-distance. - return sum_ndist / n_ndist diff --git a/research/delf/delf/python/datasets/tuples_dataset_test.py b/research/delf/delf/python/datasets/tuples_dataset_test.py deleted file mode 100644 index 4a34bb813b4..00000000000 --- a/research/delf/delf/python/datasets/tuples_dataset_test.py +++ /dev/null @@ -1,88 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"Tests for the tuples dataset module." - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import numpy as np -from PIL import Image -import tensorflow as tf -import pickle - -from delf.python.datasets import tuples_dataset -from delf.python.training.model import global_model - -FLAGS = flags.FLAGS - - -class TuplesDatasetTest(tf.test.TestCase): - """Tests for tuples dataset module.""" - - def testCreateEpochTuples(self): - """Tests epoch tuple creation.""" - # Create a tuples dataset instance. - name = 'test_dataset' - num_queries = 1 - pool_size = 5 - num_negatives = 2 - # Create a ground truth .pkl file. - gnd = { - 'train': {'ids': [str(i) + '.png' for i in range(2 * num_queries + pool_size)], - 'cluster': [0, 0, 1, 2, 3, 4, 5], - 'qidxs': [0], 'pidxs': [1]}} - gnd_name = name + '.pkl' - with tf.io.gfile.GFile(os.path.join(FLAGS.test_tmpdir, gnd_name), - 'wb') as gnd_file: - pickle.dump(gnd, gnd_file) - - # Create random images for the dataset. - for i in range(2 * num_queries + pool_size): - dummy_image = np.random.rand(1024, 750, 3) * 255 - img_out = Image.fromarray(dummy_image.astype('uint8')).convert('RGB') - filename = os.path.join(FLAGS.test_tmpdir, '{}.png'.format(i)) - img_out.save(filename) - - dataset = tuples_dataset.TuplesDataset( - name=name, - data_root=FLAGS.test_tmpdir, - mode='train', - imsize=1024, - num_negatives=num_negatives, - num_queries=num_queries, - pool_size=pool_size - ) - - # Assert that initially no negative images are set. - self.assertIsNone(dataset._nidxs) - - # Initialize a network for negative re-mining. - model_params = {'architecture': 'ResNet101', 'pooling': 'gem', - 'whitening': False, 'pretrained': True} - model = global_model.GlobalFeatureNet(**model_params) - - avg_neg_distance = dataset.create_epoch_tuples(model) - - # Check that an appropriate number of negative images has been chosen per - # query. - self.assertAllEqual(tf.shape(dataset._nidxs), [num_queries, num_negatives]) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datasets/utils.py b/research/delf/delf/python/datasets/utils.py deleted file mode 100644 index 596fca99a5d..00000000000 --- a/research/delf/delf/python/datasets/utils.py +++ /dev/null @@ -1,74 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Supporting functions for data loading.""" - -import numpy as np -from PIL import Image - -import tensorflow as tf -from delf import utils as image_loading_utils - - -def pil_imagenet_loader(path, imsize, bounding_box=None, preprocess=True): - """Pillow loader for the images. - - Args: - path: Path to image to be loaded. - imsize: Integer, defines the maximum size of longer image side. - bounding_box: (x1,y1,x2,y2) tuple to crop the query image. - preprocess: Bool, whether to preprocess the images in respect to the - ImageNet dataset. - - Returns: - image: `Tensor`, image in ImageNet suitable format. - """ - img = image_loading_utils.RgbLoader(path) - - if bounding_box is not None: - imfullsize = max(img.size) - img = img.crop(bounding_box) - imsize = imsize * max(img.size) / imfullsize - - # Unlike `resize`, `thumbnail` resizes to the largest size that preserves - # the aspect ratio, making sure that the output image does not exceed the - # original image size and the size specified in the arguments of thumbnail. - img.thumbnail((imsize, imsize), Image.ANTIALIAS) - img = np.array(img) - - if preprocess: - # Preprocessing for ImageNet data. Converts the images from RGB to BGR, - # then zero-centers each color channel with respect to the ImageNet - # dataset, without scaling. - tf.keras.applications.imagenet_utils.preprocess_input(img, mode='caffe') - - return img - - -def default_loader(path, imsize, bounding_box=None, preprocess=True): - """Default loader for the images is using Pillow. - - Args: - path: Path to image to be loaded. - imsize: Integer, defines the maximum size of longer image side. - bounding_box: (x1,y1,x2,y2) tuple to crop the query image. - preprocess: Bool, whether to preprocess the images in respect to the - ImageNet dataset. - - Returns: - image: `Tensor`, image in ImageNet suitable format. - """ - img = pil_imagenet_loader(path, imsize, bounding_box, preprocess) - return img diff --git a/research/delf/delf/python/datasets/utils_test.py b/research/delf/delf/python/datasets/utils_test.py deleted file mode 100644 index e38671bc1b7..00000000000 --- a/research/delf/delf/python/datasets/utils_test.py +++ /dev/null @@ -1,76 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for dataset utilities.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import numpy as np -from PIL import Image -import tensorflow as tf - -from delf.python.datasets import utils as image_loading_utils - -FLAGS = flags.FLAGS - - -class UtilsTest(tf.test.TestCase): - - def testDefaultLoader(self): - # Create a dummy image. - dummy_image = np.random.rand(1024, 750, 3) * 255 - img_out = Image.fromarray(dummy_image.astype('uint8')).convert('RGB') - filename = os.path.join(FLAGS.test_tmpdir, 'test_image.png') - # Save the dummy image. - img_out.save(filename) - - max_img_size = 1024 - # Load the saved dummy image. - img = image_loading_utils.default_loader( - filename, imsize=max_img_size, preprocess=False) - - # Make sure the values are the same before and after loading. - self.assertAllEqual(np.array(img_out), img) - - self.assertAllLessEqual(tf.shape(img), max_img_size) - - def testDefaultLoaderWithBoundingBox(self): - # Create a dummy image. - dummy_image = np.random.rand(1024, 750, 3) * 255 - img_out = Image.fromarray(dummy_image.astype('uint8')).convert('RGB') - filename = os.path.join(FLAGS.test_tmpdir, 'test_image.png') - # Save the dummy image. - img_out.save(filename) - - max_img_size = 1024 - # Load the saved dummy image. - expected_size = 400 - img = image_loading_utils.default_loader( - filename, - imsize=max_img_size, - bounding_box=[120, 120, 120 + expected_size, 120 + expected_size], - preprocess=False) - - # Check that the final shape is as expected. - self.assertAllEqual(tf.shape(img), [expected_size, expected_size, 3]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/datum_io.py b/research/delf/delf/python/datum_io.py deleted file mode 100644 index f0d4cbfd11a..00000000000 --- a/research/delf/delf/python/datum_io.py +++ /dev/null @@ -1,221 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Python interface for DatumProto. - -DatumProto is protocol buffer used to serialize tensor with arbitrary shape. -Please refer to datum.proto for details. - -Support read and write of DatumProto from/to NumPy array and file. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from delf import datum_pb2 - - -def ArrayToDatum(arr): - """Converts NumPy array to DatumProto. - - Supports arrays of types: - - float16 (it is converted into a float32 in DatumProto) - - float32 - - float64 (it is converted into a float32 in DatumProto) - - uint8 (it is converted into a uint32 in DatumProto) - - uint16 (it is converted into a uint32 in DatumProto) - - uint32 - - uint64 (it is converted into a uint32 in DatumProto) - - Args: - arr: NumPy array of arbitrary shape. - - Returns: - datum: DatumProto object. - - Raises: - ValueError: If array type is unsupported. - """ - datum = datum_pb2.DatumProto() - if arr.dtype in ('float16', 'float32', 'float64'): - datum.float_list.value.extend(arr.astype('float32').flat) - elif arr.dtype in ('uint8', 'uint16', 'uint32', 'uint64'): - datum.uint32_list.value.extend(arr.astype('uint32').flat) - else: - raise ValueError('Unsupported array type: %s' % arr.dtype) - - datum.shape.dim.extend(arr.shape) - return datum - - -def ArraysToDatumPair(arr_1, arr_2): - """Converts numpy arrays to DatumPairProto. - - Supports same formats as `ArrayToDatum`, see documentation therein. - - Args: - arr_1: NumPy array of arbitrary shape. - arr_2: NumPy array of arbitrary shape. - - Returns: - datum_pair: DatumPairProto object. - """ - datum_pair = datum_pb2.DatumPairProto() - datum_pair.first.CopyFrom(ArrayToDatum(arr_1)) - datum_pair.second.CopyFrom(ArrayToDatum(arr_2)) - - return datum_pair - - -def DatumToArray(datum): - """Converts data saved in DatumProto to NumPy array. - - Args: - datum: DatumProto object. - - Returns: - NumPy array of arbitrary shape. - """ - if datum.HasField('float_list'): - return np.array(datum.float_list.value).astype('float32').reshape( - datum.shape.dim) - elif datum.HasField('uint32_list'): - return np.array(datum.uint32_list.value).astype('uint32').reshape( - datum.shape.dim) - else: - raise ValueError('Input DatumProto does not have float_list or uint32_list') - - -def DatumPairToArrays(datum_pair): - """Converts data saved in DatumPairProto to NumPy arrays. - - Args: - datum_pair: DatumPairProto object. - - Returns: - Two NumPy arrays of arbitrary shape. - """ - first_datum = DatumToArray(datum_pair.first) - second_datum = DatumToArray(datum_pair.second) - return first_datum, second_datum - - -def SerializeToString(arr): - """Converts NumPy array to serialized DatumProto. - - Args: - arr: NumPy array of arbitrary shape. - - Returns: - Serialized DatumProto string. - """ - datum = ArrayToDatum(arr) - return datum.SerializeToString() - - -def SerializePairToString(arr_1, arr_2): - """Converts pair of NumPy arrays to serialized DatumPairProto. - - Args: - arr_1: NumPy array of arbitrary shape. - arr_2: NumPy array of arbitrary shape. - - Returns: - Serialized DatumPairProto string. - """ - datum_pair = ArraysToDatumPair(arr_1, arr_2) - return datum_pair.SerializeToString() - - -def ParseFromString(string): - """Converts serialized DatumProto string to NumPy array. - - Args: - string: Serialized DatumProto string. - - Returns: - NumPy array. - """ - datum = datum_pb2.DatumProto() - datum.ParseFromString(string) - return DatumToArray(datum) - - -def ParsePairFromString(string): - """Converts serialized DatumPairProto string to NumPy arrays. - - Args: - string: Serialized DatumProto string. - - Returns: - Two NumPy arrays. - """ - datum_pair = datum_pb2.DatumPairProto() - datum_pair.ParseFromString(string) - return DatumPairToArrays(datum_pair) - - -def ReadFromFile(file_path): - """Helper function to load data from a DatumProto format in a file. - - Args: - file_path: Path to file containing data. - - Returns: - data: NumPy array. - """ - with tf.io.gfile.GFile(file_path, 'rb') as f: - return ParseFromString(f.read()) - - -def ReadPairFromFile(file_path): - """Helper function to load data from a DatumPairProto format in a file. - - Args: - file_path: Path to file containing data. - - Returns: - Two NumPy arrays. - """ - with tf.io.gfile.GFile(file_path, 'rb') as f: - return ParsePairFromString(f.read()) - - -def WriteToFile(data, file_path): - """Helper function to write data to a file in DatumProto format. - - Args: - data: NumPy array. - file_path: Path to file that will be written. - """ - serialized_data = SerializeToString(data) - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write(serialized_data) - - -def WritePairToFile(arr_1, arr_2, file_path): - """Helper function to write pair of arrays to a file in DatumPairProto format. - - Args: - arr_1: NumPy array of arbitrary shape. - arr_2: NumPy array of arbitrary shape. - file_path: Path to file that will be written. - """ - serialized_data = SerializePairToString(arr_1, arr_2) - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write(serialized_data) diff --git a/research/delf/delf/python/datum_io_test.py b/research/delf/delf/python/datum_io_test.py deleted file mode 100644 index f3587a10017..00000000000 --- a/research/delf/delf/python/datum_io_test.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for datum_io, the python interface of DatumProto.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import numpy as np -import tensorflow as tf - -from delf import datum_io - -FLAGS = flags.FLAGS - - -class DatumIoTest(tf.test.TestCase): - - def Conversion2dTestWithType(self, dtype): - original_data = np.arange(9).reshape(3, 3).astype(dtype) - serialized = datum_io.SerializeToString(original_data) - retrieved_data = datum_io.ParseFromString(serialized) - self.assertTrue(np.array_equal(original_data, retrieved_data)) - - def Conversion3dTestWithType(self, dtype): - original_data = np.arange(24).reshape(2, 3, 4).astype(dtype) - serialized = datum_io.SerializeToString(original_data) - retrieved_data = datum_io.ParseFromString(serialized) - self.assertTrue(np.array_equal(original_data, retrieved_data)) - - # This test covers the following functions: ArrayToDatum, SerializeToString, - # ParseFromString, DatumToArray. - def testConversion2dWithType(self): - self.Conversion2dTestWithType(np.uint16) - self.Conversion2dTestWithType(np.uint32) - self.Conversion2dTestWithType(np.uint64) - self.Conversion2dTestWithType(np.float16) - self.Conversion2dTestWithType(np.float32) - self.Conversion2dTestWithType(np.float64) - - # This test covers the following functions: ArrayToDatum, SerializeToString, - # ParseFromString, DatumToArray. - def testConversion3dWithType(self): - self.Conversion3dTestWithType(np.uint16) - self.Conversion3dTestWithType(np.uint32) - self.Conversion3dTestWithType(np.uint64) - self.Conversion3dTestWithType(np.float16) - self.Conversion3dTestWithType(np.float32) - self.Conversion3dTestWithType(np.float64) - - def testConversionWithUnsupportedType(self): - with self.assertRaisesRegex(ValueError, 'Unsupported array type'): - self.Conversion3dTestWithType(int) - - # This test covers the following functions: ArrayToDatum, SerializeToString, - # WriteToFile, ReadFromFile, ParseFromString, DatumToArray. - def testWriteAndReadToFile(self): - data = np.array([[[-1.0, 125.0, -2.5], [14.5, 3.5, 0.0]], - [[20.0, 0.0, 30.0], [25.5, 36.0, 42.0]]]) - filename = os.path.join(FLAGS.test_tmpdir, 'test.datum') - datum_io.WriteToFile(data, filename) - data_read = datum_io.ReadFromFile(filename) - self.assertAllEqual(data_read, data) - - # This test covers the following functions: ArraysToDatumPair, - # SerializePairToString, WritePairToFile, ReadPairFromFile, - # ParsePairFromString, DatumPairToArrays. - def testWriteAndReadPairToFile(self): - data_1 = np.array([[[-1.0, 125.0, -2.5], [14.5, 3.5, 0.0]], - [[20.0, 0.0, 30.0], [25.5, 36.0, 42.0]]]) - data_2 = np.array( - [[[255, 0, 5], [10, 300, 0]], [[20, 1, 100], [255, 360, 420]]], - dtype='uint32') - filename = os.path.join(FLAGS.test_tmpdir, 'test.datum_pair') - datum_io.WritePairToFile(data_1, data_2, filename) - data_read_1, data_read_2 = datum_io.ReadPairFromFile(filename) - self.assertAllEqual(data_read_1, data_1) - self.assertAllEqual(data_read_2, data_2) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/delg/DELG_INSTRUCTIONS.md b/research/delf/delf/python/delg/DELG_INSTRUCTIONS.md deleted file mode 100644 index dc72422e87c..00000000000 --- a/research/delf/delf/python/delg/DELG_INSTRUCTIONS.md +++ /dev/null @@ -1,175 +0,0 @@ -## DELG instructions - -[![Paper](http://img.shields.io/badge/paper-arXiv.2001.05027-B3181B.svg)](https://arxiv.org/abs/2001.05027) - -These instructions can be used to reproduce the results from the -[DELG paper](https://arxiv.org/abs/2001.05027) for the Revisited Oxford/Paris -datasets. - -### Install DELF library - -To be able to use this code, please follow -[these instructions](../../../INSTALL_INSTRUCTIONS.md) to properly install the -DELF library. - -### Download datasets - -```bash -mkdir -p ~/delg/data && cd ~/delg/data - -# Oxford dataset. -wget http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/oxbuild_images.tgz -mkdir oxford5k_images -tar -xvzf oxbuild_images.tgz -C oxford5k_images/ - -# Paris dataset. Download and move all images to same directory. -wget http://www.robots.ox.ac.uk/~vgg/data/parisbuildings/paris_1.tgz -wget http://www.robots.ox.ac.uk/~vgg/data/parisbuildings/paris_2.tgz -mkdir paris6k_images_tmp -tar -xvzf paris_1.tgz -C paris6k_images_tmp/ -tar -xvzf paris_2.tgz -C paris6k_images_tmp/ -mkdir paris6k_images -mv paris6k_images_tmp/paris/*/*.jpg paris6k_images/ - -# Revisited annotations. -wget http://cmp.felk.cvut.cz/revisitop/data/datasets/roxford5k/gnd_roxford5k.mat -wget http://cmp.felk.cvut.cz/revisitop/data/datasets/rparis6k/gnd_rparis6k.mat -wget http://cmp.felk.cvut.cz/cnnimageretrieval/data/test/roxford5k/gnd_roxford5k.pkl -wget http://cmp.felk.cvut.cz/cnnimageretrieval/data/test/rparis6k/gnd_rparis6k.pkl -``` - -### Download model - -This is necessary to reproduce the main paper results. This example shows the -R50-DELG model, pretrained on GLD; see the available pre-trained models -[here](../../../README.md#pre-trained-models), for other variants (eg, R101, -trained on GLDv2-clean). - -```bash -# From models/research/delf/delf/python/delg -mkdir parameters && cd parameters - -# R50-DELG-GLD model. -wget http://storage.googleapis.com/delf/r50delg_gld_20200814.tar.gz -tar -xvzf r50delg_gld_20200814.tar.gz -``` - -### Feature extraction - -We present here commands for R50-DELG (pretrained on GLD) extraction on -`roxford5k`. - -- To use the R101-DELG model pretrained on GLD, first download it as mentioned - above; then, replace the below argument `delf_config_path` by - `r101delg_gld_config.pbtxt` -- To use the R50-DELG model pretrained on GLDv2-clean, first download it as - mentioned above; then, replace the below argument `delf_config_path` by - `r50delg_gldv2clean_config.pbtxt` -- To use the R101-DELG model pretrained on GLDv2-clean, first download it as - mentioned above; then, replace the below argument `delf_config_path` by - `r101delg_gldv2clean_config.pbtxt` -- To extract on `rparis6k` instead, please edit the arguments accordingly - (especially the `dataset_file_path` argument). - -#### Query feature extraction - -For query feature extraction, the cropped query image should be used to extract -features, according to the Revisited Oxford/Paris experimental protocol. Note -that this is done in the `extract_features` script, when setting -`image_set=query`. - -Query feature extraction can be run as follows: - -```bash -# From models/research/delf/delf/python/delg -python3 extract_features.py \ - --delf_config_path r50delg_gld_config.pbtxt \ - --dataset_file_path ~/delg/data/gnd_roxford5k.mat \ - --images_dir ~/delg/data/oxford5k_images \ - --image_set query \ - --output_features_dir ~/delg/data/oxford5k_features/query -``` - -#### Index feature extraction - -Run index feature extraction as follows: - -```bash -# From models/research/delf/delf/python/delg -python3 extract_features.py \ - --delf_config_path r50delg_gld_config.pbtxt \ - --dataset_file_path ~/delg/data/gnd_roxford5k.mat \ - --images_dir ~/delg/data/oxford5k_images \ - --image_set index \ - --output_features_dir ~/delg/data/oxford5k_features/index -``` - -### Perform retrieval - -To run retrieval on `roxford5k`, the following command can be used: - -```bash -# From models/research/delf/delf/python/delg -python3 perform_retrieval.py \ - --dataset_file_path ~/delg/data/gnd_roxford5k.mat \ - --query_features_dir ~/delg/data/oxford5k_features/query \ - --index_features_dir ~/delg/data/oxford5k_features/index \ - --output_dir ~/delg/results/oxford5k -``` - -A file with named `metrics.txt` will be written to the path given in -`output_dir`, with retrieval metrics for an experiment where geometric -verification is not used. The contents should look approximately like: - -``` -hard - mAP=45.11 - mP@k[ 1 5 10] [85.71 72.29 60.14] - mR@k[ 1 5 10] [19.15 29.72 36.32] -medium - mAP=69.71 - mP@k[ 1 5 10] [95.71 92. 86.86] - mR@k[ 1 5 10] [10.17 25.94 33.83] -``` - -which are the results presented in Table 3 of the paper. - -If you want to run retrieval with geometric verification, set -`use_geometric_verification` to `True`. It's much slower since (1) in this code -example the re-ranking is loading DELF local features from disk, and (2) -re-ranking needs to be performed separately for each dataset protocol, since the -junk images from each protocol should be removed when re-ranking. Here is an -example command: - -```bash -# From models/research/delf/delf/python/delg -python3 perform_retrieval.py \ - --dataset_file_path ~/delg/data/gnd_roxford5k.mat \ - --query_features_dir ~/delg/data/oxford5k_features/query \ - --index_features_dir ~/delg/data/oxford5k_features/index \ - --use_geometric_verification \ - --output_dir ~/delg/results/oxford5k_with_gv -``` - -The `metrics.txt` should now show: - -``` -hard - mAP=45.11 - mP@k[ 1 5 10] [85.71 72.29 60.14] - mR@k[ 1 5 10] [19.15 29.72 36.32] -hard_after_gv - mAP=53.72 - mP@k[ 1 5 10] [91.43 83.81 74.38] - mR@k[ 1 5 10] [19.45 34.45 44.64] -medium - mAP=69.71 - mP@k[ 1 5 10] [95.71 92. 86.86] - mR@k[ 1 5 10] [10.17 25.94 33.83] -medium_after_gv - mAP=75.42 - mP@k[ 1 5 10] [97.14 95.24 93.81] - mR@k[ 1 5 10] [10.21 27.21 37.72] -``` - -which, again, are the results presented in Table 3 of the paper. diff --git a/research/delf/delf/python/delg/extract_features.py b/research/delf/delf/python/delg/extract_features.py deleted file mode 100644 index 4ef10dc9415..00000000000 --- a/research/delf/delf/python/delg/extract_features.py +++ /dev/null @@ -1,163 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Extracts DELG features for images from Revisited Oxford/Paris datasets. - -Note that query images are cropped before feature extraction, as required by the -evaluation protocols of these datasets. - -The types of extracted features (local and/or global) depend on the input -DelfConfig. - -The program checks if features already exist, and skips computation for those. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import time - -from absl import app -from absl import flags -import numpy as np -import tensorflow as tf - -from google.protobuf import text_format -from delf import delf_config_pb2 -from delf import datum_io -from delf import feature_io -from delf import utils -from delf.python.datasets.revisited_op import dataset -from delf import extractor - -FLAGS = flags.FLAGS - -flags.DEFINE_string( - 'delf_config_path', '/tmp/delf_config_example.pbtxt', - 'Path to DelfConfig proto text file with configuration to be used for DELG ' - 'extraction. Local features are extracted if use_local_features is True; ' - 'global features are extracted if use_global_features is True.') -flags.DEFINE_string( - 'dataset_file_path', '/tmp/gnd_roxford5k.mat', - 'Dataset file for Revisited Oxford or Paris dataset, in .mat format.') -flags.DEFINE_string( - 'images_dir', '/tmp/images', - 'Directory where dataset images are located, all in .jpg format.') -flags.DEFINE_enum('image_set', 'query', ['query', 'index'], - 'Whether to extract features from query or index images.') -flags.DEFINE_string( - 'output_features_dir', '/tmp/features', - "Directory where DELG features will be written to. Each image's features " - 'will be written to files with same name but different extension: the ' - 'global feature is written to a file with extension .delg_global and the ' - 'local features are written to a file with extension .delg_local.') - -# Extensions. -_DELG_GLOBAL_EXTENSION = '.delg_global' -_DELG_LOCAL_EXTENSION = '.delg_local' -_IMAGE_EXTENSION = '.jpg' - -# Pace to report extraction log. -_STATUS_CHECK_ITERATIONS = 50 - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read list of images from dataset file. - print('Reading list of images from dataset file...') - query_list, index_list, ground_truth = dataset.ReadDatasetFile( - FLAGS.dataset_file_path) - if FLAGS.image_set == 'query': - image_list = query_list - else: - image_list = index_list - num_images = len(image_list) - print('done! Found %d images' % num_images) - - # Parse DelfConfig proto. - config = delf_config_pb2.DelfConfig() - with tf.io.gfile.GFile(FLAGS.delf_config_path, 'r') as f: - text_format.Parse(f.read(), config) - - # Create output directory if necessary. - if not tf.io.gfile.exists(FLAGS.output_features_dir): - tf.io.gfile.makedirs(FLAGS.output_features_dir) - - extractor_fn = extractor.MakeExtractor(config) - - start = time.time() - for i in range(num_images): - if i == 0: - print('Starting to extract features...') - elif i % _STATUS_CHECK_ITERATIONS == 0: - elapsed = (time.time() - start) - print('Processing image %d out of %d, last %d ' - 'images took %f seconds' % - (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed)) - start = time.time() - - image_name = image_list[i] - input_image_filename = os.path.join(FLAGS.images_dir, - image_name + _IMAGE_EXTENSION) - - # Compose output file name and decide if image should be skipped. - should_skip_global = True - should_skip_local = True - if config.use_global_features: - output_global_feature_filename = os.path.join( - FLAGS.output_features_dir, image_name + _DELG_GLOBAL_EXTENSION) - if not tf.io.gfile.exists(output_global_feature_filename): - should_skip_global = False - if config.use_local_features: - output_local_feature_filename = os.path.join( - FLAGS.output_features_dir, image_name + _DELG_LOCAL_EXTENSION) - if not tf.io.gfile.exists(output_local_feature_filename): - should_skip_local = False - if should_skip_global and should_skip_local: - print('Skipping %s' % image_name) - continue - - pil_im = utils.RgbLoader(input_image_filename) - resize_factor = 1.0 - if FLAGS.image_set == 'query': - # Crop query image according to bounding box. - original_image_size = max(pil_im.size) - bbox = [int(round(b)) for b in ground_truth[i]['bbx']] - pil_im = pil_im.crop(bbox) - cropped_image_size = max(pil_im.size) - resize_factor = cropped_image_size / original_image_size - - im = np.array(pil_im) - - # Extract and save features. - extracted_features = extractor_fn(im, resize_factor) - if config.use_global_features: - global_descriptor = extracted_features['global_descriptor'] - datum_io.WriteToFile(global_descriptor, output_global_feature_filename) - if config.use_local_features: - locations = extracted_features['local_features']['locations'] - descriptors = extracted_features['local_features']['descriptors'] - feature_scales = extracted_features['local_features']['scales'] - attention = extracted_features['local_features']['attention'] - feature_io.WriteToFile(output_local_feature_filename, locations, - feature_scales, descriptors, attention) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/delg/measure_latency.py b/research/delf/delf/python/delg/measure_latency.py deleted file mode 100644 index 966964d1072..00000000000 --- a/research/delf/delf/python/delg/measure_latency.py +++ /dev/null @@ -1,119 +0,0 @@ -# Copyright 2020 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Times DELF/G extraction.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import time - -from absl import app -from absl import flags -import numpy as np -from six.moves import range -import tensorflow as tf - -from google.protobuf import text_format -from delf import delf_config_pb2 -from delf import utils -from delf import extractor - -FLAGS = flags.FLAGS - -flags.DEFINE_string( - 'delf_config_path', '/tmp/delf_config_example.pbtxt', - 'Path to DelfConfig proto text file with configuration to be used for DELG ' - 'extraction. Local features are extracted if use_local_features is True; ' - 'global features are extracted if use_global_features is True.') -flags.DEFINE_string('list_images_path', '/tmp/list_images.txt', - 'Path to list of images whose features will be extracted.') -flags.DEFINE_integer('repeat_per_image', 10, - 'Number of times to repeat extraction per image.') -flags.DEFINE_boolean( - 'binary_local_features', False, - 'Whether to binarize local features after extraction, and take this extra ' - 'latency into account. This should only be used if use_local_features is ' - 'set in the input DelfConfig from `delf_config_path`.') - -# Pace to report extraction log. -_STATUS_CHECK_ITERATIONS = 100 - - -def _ReadImageList(list_path): - """Helper function to read image paths. - - Args: - list_path: Path to list of images, one image path per line. - - Returns: - image_paths: List of image paths. - """ - with tf.io.gfile.GFile(list_path, 'r') as f: - image_paths = f.readlines() - image_paths = [entry.rstrip() for entry in image_paths] - return image_paths - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read list of images. - print('Reading list of images...') - image_paths = _ReadImageList(FLAGS.list_images_path) - num_images = len(image_paths) - print(f'done! Found {num_images} images') - - # Load images in memory. - print('Loading images, %d times per image...' % FLAGS.repeat_per_image) - im_array = [] - for filename in image_paths: - im = np.array(utils.RgbLoader(filename)) - for _ in range(FLAGS.repeat_per_image): - im_array.append(im) - np.random.shuffle(im_array) - print('done!') - - # Parse DelfConfig proto. - config = delf_config_pb2.DelfConfig() - with tf.io.gfile.GFile(FLAGS.delf_config_path, 'r') as f: - text_format.Parse(f.read(), config) - - extractor_fn = extractor.MakeExtractor(config) - - start = time.time() - for i, im in enumerate(im_array): - if i == 0: - print('Starting to extract DELF features from images...') - elif i % _STATUS_CHECK_ITERATIONS == 0: - elapsed = (time.time() - start) - print(f'Processing image {i} out of {len(im_array)}, last ' - f'{_STATUS_CHECK_ITERATIONS} images took {elapsed} seconds,' - f'ie {elapsed/_STATUS_CHECK_ITERATIONS} secs/image.') - start = time.time() - - # Extract and save features. - extracted_features = extractor_fn(im) - - # Binarize local features, if desired (and if there are local features). - if (config.use_local_features and FLAGS.binary_local_features and - extracted_features['local_features']['attention'].size): - packed_descriptors = np.packbits( - extracted_features['local_features']['descriptors'] > 0, axis=1) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/delg/perform_retrieval.py b/research/delf/delf/python/delg/perform_retrieval.py deleted file mode 100644 index dc380077c56..00000000000 --- a/research/delf/delf/python/delg/perform_retrieval.py +++ /dev/null @@ -1,224 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Performs DELG-based image retrieval on Revisited Oxford/Paris datasets.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import time - -from absl import app -from absl import flags -import numpy as np -import tensorflow as tf - -from delf import datum_io -from delf.python.datasets.revisited_op import dataset -from delf.python.detect_to_retrieve import image_reranking - -FLAGS = flags.FLAGS - -flags.DEFINE_string( - 'dataset_file_path', '/tmp/gnd_roxford5k.mat', - 'Dataset file for Revisited Oxford or Paris dataset, in .mat format.') -flags.DEFINE_string('query_features_dir', '/tmp/features/query', - 'Directory where query DELG features are located.') -flags.DEFINE_string('index_features_dir', '/tmp/features/index', - 'Directory where index DELG features are located.') -flags.DEFINE_boolean( - 'use_geometric_verification', False, - 'If True, performs re-ranking using local feature-based geometric ' - 'verification.') -flags.DEFINE_float( - 'local_descriptor_matching_threshold', 1.0, - 'Optional, only used if `use_geometric_verification` is True. ' - 'Threshold below which a pair of local descriptors is considered ' - 'a potential match, and will be fed into RANSAC.') -flags.DEFINE_float( - 'ransac_residual_threshold', 20.0, - 'Optional, only used if `use_geometric_verification` is True. ' - 'Residual error threshold for considering matches as inliers, used in ' - 'RANSAC algorithm.') -flags.DEFINE_boolean( - 'use_ratio_test', False, - 'Optional, only used if `use_geometric_verification` is True. ' - 'Whether to use ratio test for local feature matching.') -flags.DEFINE_string( - 'output_dir', '/tmp/retrieval', - 'Directory where retrieval output will be written to. A file containing ' - "metrics for this run is saved therein, with file name 'metrics.txt'.") - -# Extensions. -_DELG_GLOBAL_EXTENSION = '.delg_global' -_DELG_LOCAL_EXTENSION = '.delg_local' - -# Precision-recall ranks to use in metric computation. -_PR_RANKS = (1, 5, 10) - -# Pace to log. -_STATUS_CHECK_LOAD_ITERATIONS = 50 - -# Output file names. -_METRICS_FILENAME = 'metrics.txt' - - -def _ReadDelgGlobalDescriptors(input_dir, image_list): - """Reads DELG global features. - - Args: - input_dir: Directory where features are located. - image_list: List of image names for which to load features. - - Returns: - global_descriptors: NumPy array of shape (len(image_list), D), where D - corresponds to the global descriptor dimensionality. - """ - num_images = len(image_list) - global_descriptors = [] - print('Starting to collect global descriptors for %d images...' % num_images) - start = time.time() - for i in range(num_images): - if i > 0 and i % _STATUS_CHECK_LOAD_ITERATIONS == 0: - elapsed = (time.time() - start) - print('Reading global descriptors for image %d out of %d, last %d ' - 'images took %f seconds' % - (i, num_images, _STATUS_CHECK_LOAD_ITERATIONS, elapsed)) - start = time.time() - - descriptor_filename = image_list[i] + _DELG_GLOBAL_EXTENSION - descriptor_fullpath = os.path.join(input_dir, descriptor_filename) - global_descriptors.append(datum_io.ReadFromFile(descriptor_fullpath)) - - return np.array(global_descriptors) - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Parse dataset to obtain query/index images, and ground-truth. - print('Parsing dataset...') - query_list, index_list, ground_truth = dataset.ReadDatasetFile( - FLAGS.dataset_file_path) - num_query_images = len(query_list) - num_index_images = len(index_list) - (_, medium_ground_truth, - hard_ground_truth) = dataset.ParseEasyMediumHardGroundTruth(ground_truth) - print('done! Found %d queries and %d index images' % - (num_query_images, num_index_images)) - - # Read global features. - query_global_features = _ReadDelgGlobalDescriptors(FLAGS.query_features_dir, - query_list) - index_global_features = _ReadDelgGlobalDescriptors(FLAGS.index_features_dir, - index_list) - - # Compute similarity between query and index images, potentially re-ranking - # with geometric verification. - ranks_before_gv = np.zeros([num_query_images, num_index_images], - dtype='int32') - if FLAGS.use_geometric_verification: - medium_ranks_after_gv = np.zeros([num_query_images, num_index_images], - dtype='int32') - hard_ranks_after_gv = np.zeros([num_query_images, num_index_images], - dtype='int32') - for i in range(num_query_images): - print('Performing retrieval with query %d (%s)...' % (i, query_list[i])) - start = time.time() - - # Compute similarity between global descriptors. - similarities = np.dot(index_global_features, query_global_features[i]) - ranks_before_gv[i] = np.argsort(-similarities) - - # Re-rank using geometric verification. - if FLAGS.use_geometric_verification: - medium_ranks_after_gv[i] = image_reranking.RerankByGeometricVerification( - input_ranks=ranks_before_gv[i], - initial_scores=similarities, - query_name=query_list[i], - index_names=index_list, - query_features_dir=FLAGS.query_features_dir, - index_features_dir=FLAGS.index_features_dir, - junk_ids=set(medium_ground_truth[i]['junk']), - local_feature_extension=_DELG_LOCAL_EXTENSION, - ransac_seed=0, - descriptor_matching_threshold=FLAGS - .local_descriptor_matching_threshold, - ransac_residual_threshold=FLAGS.ransac_residual_threshold, - use_ratio_test=FLAGS.use_ratio_test) - hard_ranks_after_gv[i] = image_reranking.RerankByGeometricVerification( - input_ranks=ranks_before_gv[i], - initial_scores=similarities, - query_name=query_list[i], - index_names=index_list, - query_features_dir=FLAGS.query_features_dir, - index_features_dir=FLAGS.index_features_dir, - junk_ids=set(hard_ground_truth[i]['junk']), - local_feature_extension=_DELG_LOCAL_EXTENSION, - ransac_seed=0, - descriptor_matching_threshold=FLAGS - .local_descriptor_matching_threshold, - ransac_residual_threshold=FLAGS.ransac_residual_threshold, - use_ratio_test=FLAGS.use_ratio_test) - - elapsed = (time.time() - start) - print('done! Retrieval for query %d took %f seconds' % (i, elapsed)) - - # Create output directory if necessary. - if not tf.io.gfile.exists(FLAGS.output_dir): - tf.io.gfile.makedirs(FLAGS.output_dir) - - # Compute metrics. - medium_metrics = dataset.ComputeMetrics(ranks_before_gv, medium_ground_truth, - _PR_RANKS) - hard_metrics = dataset.ComputeMetrics(ranks_before_gv, hard_ground_truth, - _PR_RANKS) - if FLAGS.use_geometric_verification: - medium_metrics_after_gv = dataset.ComputeMetrics(medium_ranks_after_gv, - medium_ground_truth, - _PR_RANKS) - hard_metrics_after_gv = dataset.ComputeMetrics(hard_ranks_after_gv, - hard_ground_truth, _PR_RANKS) - - # Write metrics to file. - mean_average_precision_dict = { - 'medium': medium_metrics[0], - 'hard': hard_metrics[0] - } - mean_precisions_dict = {'medium': medium_metrics[1], 'hard': hard_metrics[1]} - mean_recalls_dict = {'medium': medium_metrics[2], 'hard': hard_metrics[2]} - if FLAGS.use_geometric_verification: - mean_average_precision_dict.update({ - 'medium_after_gv': medium_metrics_after_gv[0], - 'hard_after_gv': hard_metrics_after_gv[0] - }) - mean_precisions_dict.update({ - 'medium_after_gv': medium_metrics_after_gv[1], - 'hard_after_gv': hard_metrics_after_gv[1] - }) - mean_recalls_dict.update({ - 'medium_after_gv': medium_metrics_after_gv[2], - 'hard_after_gv': hard_metrics_after_gv[2] - }) - dataset.SaveMetricsFile(mean_average_precision_dict, mean_precisions_dict, - mean_recalls_dict, _PR_RANKS, - os.path.join(FLAGS.output_dir, _METRICS_FILENAME)) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/delg/r101delg_gld_config.pbtxt b/research/delf/delf/python/delg/r101delg_gld_config.pbtxt deleted file mode 100644 index ea8a70b53df..00000000000 --- a/research/delf/delf/python/delg/r101delg_gld_config.pbtxt +++ /dev/null @@ -1,22 +0,0 @@ -use_local_features: true -use_global_features: true -model_path: "parameters/r101delg_gld_20200814" -image_scales: 0.25 -image_scales: 0.35355338 -image_scales: 0.5 -image_scales: 0.70710677 -image_scales: 1.0 -image_scales: 1.4142135 -image_scales: 2.0 -delf_local_config { - use_pca: false - max_feature_num: 1000 - score_threshold: 166.1 -} -delf_global_config { - use_pca: false - image_scales_ind: 3 - image_scales_ind: 4 - image_scales_ind: 5 -} -max_image_size: 1024 diff --git a/research/delf/delf/python/delg/r101delg_gldv2clean_config.pbtxt b/research/delf/delf/python/delg/r101delg_gldv2clean_config.pbtxt deleted file mode 100644 index d34a039a4ea..00000000000 --- a/research/delf/delf/python/delg/r101delg_gldv2clean_config.pbtxt +++ /dev/null @@ -1,22 +0,0 @@ -use_local_features: true -use_global_features: true -model_path: "parameters/r101delg_gldv2clean_20200914" -image_scales: 0.25 -image_scales: 0.35355338 -image_scales: 0.5 -image_scales: 0.70710677 -image_scales: 1.0 -image_scales: 1.4142135 -image_scales: 2.0 -delf_local_config { - use_pca: false - max_feature_num: 1000 - score_threshold: 357.48 -} -delf_global_config { - use_pca: false - image_scales_ind: 3 - image_scales_ind: 4 - image_scales_ind: 5 -} -max_image_size: 1024 diff --git a/research/delf/delf/python/delg/r50delg_gld_config.pbtxt b/research/delf/delf/python/delg/r50delg_gld_config.pbtxt deleted file mode 100644 index 4457810b575..00000000000 --- a/research/delf/delf/python/delg/r50delg_gld_config.pbtxt +++ /dev/null @@ -1,22 +0,0 @@ -use_local_features: true -use_global_features: true -model_path: "parameters/r50delg_gld_20200814" -image_scales: 0.25 -image_scales: 0.35355338 -image_scales: 0.5 -image_scales: 0.70710677 -image_scales: 1.0 -image_scales: 1.4142135 -image_scales: 2.0 -delf_local_config { - use_pca: false - max_feature_num: 1000 - score_threshold: 175.0 -} -delf_global_config { - use_pca: false - image_scales_ind: 3 - image_scales_ind: 4 - image_scales_ind: 5 -} -max_image_size: 1024 diff --git a/research/delf/delf/python/delg/r50delg_gldv2clean_config.pbtxt b/research/delf/delf/python/delg/r50delg_gldv2clean_config.pbtxt deleted file mode 100644 index 358d7cbe56c..00000000000 --- a/research/delf/delf/python/delg/r50delg_gldv2clean_config.pbtxt +++ /dev/null @@ -1,22 +0,0 @@ -use_local_features: true -use_global_features: true -model_path: "parameters/r50delg_gldv2clean_20200914" -image_scales: 0.25 -image_scales: 0.35355338 -image_scales: 0.5 -image_scales: 0.70710677 -image_scales: 1.0 -image_scales: 1.4142135 -image_scales: 2.0 -delf_local_config { - use_pca: false - max_feature_num: 1000 - score_threshold: 454.6 -} -delf_global_config { - use_pca: false - image_scales_ind: 3 - image_scales_ind: 4 - image_scales_ind: 5 -} -max_image_size: 1024 diff --git a/research/delf/delf/python/detect_to_retrieve/DETECT_TO_RETRIEVE_INSTRUCTIONS.md b/research/delf/delf/python/detect_to_retrieve/DETECT_TO_RETRIEVE_INSTRUCTIONS.md deleted file mode 100644 index 2d18a328997..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/DETECT_TO_RETRIEVE_INSTRUCTIONS.md +++ /dev/null @@ -1,231 +0,0 @@ -## Detect-to-Retrieve instructions - -[![Paper](http://img.shields.io/badge/paper-arXiv.1812.01584-B3181B.svg)](https://arxiv.org/abs/1812.01584) - -These instructions can be used to reproduce the results from the -[Detect-to-Retrieve paper](https://arxiv.org/abs/1812.01584) for the Revisited -Oxford/Paris datasets. - -### Install DELF library - -To be able to use this code, please follow -[these instructions](../../../INSTALL_INSTRUCTIONS.md) to properly install the -DELF library. - -### Download datasets - -```bash -mkdir -p ~/detect_to_retrieve/data && cd ~/detect_to_retrieve/data - -# Oxford dataset. -wget http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/oxbuild_images.tgz -mkdir oxford5k_images -tar -xvzf oxbuild_images.tgz -C oxford5k_images/ - -# Paris dataset. Download and move all images to same directory. -wget http://www.robots.ox.ac.uk/~vgg/data/parisbuildings/paris_1.tgz -wget http://www.robots.ox.ac.uk/~vgg/data/parisbuildings/paris_2.tgz -mkdir paris6k_images_tmp -tar -xvzf paris_1.tgz -C paris6k_images_tmp/ -tar -xvzf paris_2.tgz -C paris6k_images_tmp/ -mkdir paris6k_images -mv paris6k_images_tmp/paris/*/*.jpg paris6k_images/ - -# Revisited annotations. -wget http://cmp.felk.cvut.cz/revisitop/data/datasets/roxford5k/gnd_roxford5k.mat -wget http://cmp.felk.cvut.cz/revisitop/data/datasets/rparis6k/gnd_rparis6k.mat -``` - -### Download models - -These are necessary to reproduce the main paper results: - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -mkdir parameters && cd parameters - -# DELF-GLD model. -wget http://storage.googleapis.com/delf/delf_gld_20190411.tar.gz -tar -xvzf delf_gld_20190411.tar.gz - -# Faster-RCNN detector model. -wget http://storage.googleapis.com/delf/d2r_frcnn_20190411.tar.gz -tar -xvzf d2r_frcnn_20190411.tar.gz - -# Codebooks. -# Note: you should use codebook trained on rparis6k for roxford5k retrieval -# experiments, and vice-versa. -wget http://storage.googleapis.com/delf/rparis6k_codebook_65536.tar.gz -mkdir rparis6k_codebook_65536 -tar -xvzf rparis6k_codebook_65536.tar.gz -C rparis6k_codebook_65536/ -wget http://storage.googleapis.com/delf/roxford5k_codebook_65536.tar.gz -mkdir roxford5k_codebook_65536 -tar -xvzf roxford5k_codebook_65536.tar.gz -C roxford5k_codebook_65536/ -``` - -We also make available other models/parameters that can be used to reproduce -more results from the paper: - -- [MobileNet-SSD trained detector](http://storage.googleapis.com/delf/d2r_mnetssd_20190411.tar.gz). -- Codebooks with 1024 centroids: - [rparis6k](http://storage.googleapis.com/delf/rparis6k_codebook_1024.tar.gz), - [roxford5k](http://storage.googleapis.com/delf/roxford5k_codebook_1024.tar.gz). - -### Feature extraction - -We present here commands for extraction on `roxford5k`. To extract on `rparis6k` -instead, please edit the arguments accordingly (especially the -`dataset_file_path` argument). - -#### Query feature extraction - -For query feature extraction, the cropped query image should be used to extract -features, according to the Revisited Oxford/Paris experimental protocol. Note -that this is done in the `extract_query_features` script. - -Query feature extraction can be run as follows: - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -python3 extract_query_features.py \ - --delf_config_path delf_gld_config.pbtxt \ - --dataset_file_path ~/detect_to_retrieve/data/gnd_roxford5k.mat \ - --images_dir ~/detect_to_retrieve/data/oxford5k_images \ - --output_features_dir ~/detect_to_retrieve/data/oxford5k_features/query -``` - -#### Index feature extraction and box detection - -Index feature extraction / box detection can be run as follows: - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -python3 extract_index_boxes_and_features.py \ - --delf_config_path delf_gld_config.pbtxt \ - --detector_model_dir parameters/d2r_frcnn_20190411 \ - --detector_thresh 0.1 \ - --dataset_file_path ~/detect_to_retrieve/data/gnd_roxford5k.mat \ - --images_dir ~/detect_to_retrieve/data/oxford5k_images \ - --output_boxes_dir ~/detect_to_retrieve/data/oxford5k_boxes/index \ - --output_features_dir ~/detect_to_retrieve/data/oxford5k_features/index_0.1 \ - --output_index_mapping ~/detect_to_retrieve/data/oxford5k_features/index_mapping_0.1.csv -``` - -### R-ASMK* aggregation extraction - -We present here commands for aggregation extraction on `roxford5k`. To extract -on `rparis6k` instead, please edit the arguments accordingly. In particular, -note that feature aggregation on `roxford5k` should use a codebook trained on -`rparis6k`, and vice-versa (this can be edited in the -`query_aggregation_config.pbtxt` and `index_aggregation_config.pbtxt` files. - -#### Query - -Run query feature aggregation as follows: - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -python3 extract_aggregation.py \ - --use_query_images True \ - --aggregation_config_path query_aggregation_config.pbtxt \ - --dataset_file_path ~/detect_to_retrieve/data/gnd_roxford5k.mat \ - --features_dir ~/detect_to_retrieve/data/oxford5k_features/query \ - --output_aggregation_dir ~/detect_to_retrieve/data/oxford5k_aggregation/query -``` - -#### Index - -Run index feature aggregation as follows: - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -python3 extract_aggregation.py \ - --aggregation_config_path index_aggregation_config.pbtxt \ - --dataset_file_path ~/detect_to_retrieve/data/gnd_roxford5k.mat \ - --features_dir ~/detect_to_retrieve/data/oxford5k_features/index_0.1 \ - --index_mapping_path ~/detect_to_retrieve/data/oxford5k_features/index_mapping_0.1.csv \ - --output_aggregation_dir ~/detect_to_retrieve/data/oxford5k_aggregation/index_0.1 -``` - -### Perform retrieval - -Currently, we support retrieval via brute-force comparison of aggregated -features. - -To run retrieval on `roxford5k`, the following command can be used: - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -python3 perform_retrieval.py \ - --index_aggregation_config_path index_aggregation_config.pbtxt \ - --query_aggregation_config_path query_aggregation_config.pbtxt \ - --dataset_file_path ~/detect_to_retrieve/data/gnd_roxford5k.mat \ - --index_aggregation_dir ~/detect_to_retrieve/data/oxford5k_aggregation/index_0.1 \ - --query_aggregation_dir ~/detect_to_retrieve/data/oxford5k_aggregation/query \ - --output_dir ~/detect_to_retrieve/results/oxford5k -``` - -A file with named `metrics.txt` will be written to the path given in -`output_dir`, with retrieval metrics for an experiment where geometric -verification is not used. The contents should look approximately like: - -``` -hard -mAP=47.61 -mP@k[ 1 5 10] [84.29 73.71 64.43] -mR@k[ 1 5 10] [18.84 29.44 36.82] -medium -mAP=73.3 -mP@k[ 1 5 10] [97.14 94.57 90.14] -mR@k[ 1 5 10] [10.14 26.2 34.75] -``` - -which are the results presented in Table 2 of the paper (with small numerical -precision differences). - -If you want to run retrieval with geometric verification, set -`use_geometric_verification` to `True` and the arguments -`index_features_dir`/`query_features_dir`. It's much slower since (1) in this -code example the re-ranking is loading DELF local features from disk, and (2) -re-ranking needs to be performed separately for each dataset protocol, since the -junk images from each protocol should be removed when re-ranking. Here is an -example command: - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -python3 perform_retrieval.py \ - --index_aggregation_config_path index_aggregation_config.pbtxt \ - --query_aggregation_config_path query_aggregation_config.pbtxt \ - --dataset_file_path ~/detect_to_retrieve/data/gnd_roxford5k.mat \ - --index_aggregation_dir ~/detect_to_retrieve/data/oxford5k_aggregation/index_0.1 \ - --query_aggregation_dir ~/detect_to_retrieve/data/oxford5k_aggregation/query \ - --use_geometric_verification True \ - --index_features_dir ~/detect_to_retrieve/data/oxford5k_features/index_0.1 \ - --query_features_dir ~/detect_to_retrieve/data/oxford5k_features/query \ - --output_dir ~/detect_to_retrieve/results/oxford5k_with_gv -``` - -### Clustering - -In the code example above, we used a pre-trained DELF codebook. We also provide -code for re-training the codebook if desired. - -Note that for the time being this can only run on CPU, since the main ops in -K-means are not registered for GPU usage in Tensorflow. - -```bash -# From models/research/delf/delf/python/detect_to_retrieve -python3 cluster_delf_features.py \ - --dataset_file_path ~/detect_to_retrieve/data/gnd_rparis6k.mat \ - --features_dir ~/detect_to_retrieve/data/paris6k_features/index_0.1 \ - --num_clusters 1024 \ - --num_iterations 50 \ - --output_cluster_dir ~/detect_to_retrieve/data/paris6k_clusters_1024 -``` - -### Next steps - -To make retrieval more scalable and handle larger datasets more smoothly, we are -considering to provide code for inverted index building and retrieval. Please -reach out if you would like to help doing that -- feel free submit a pull -request. diff --git a/research/delf/delf/python/detect_to_retrieve/__init__.py b/research/delf/delf/python/detect_to_retrieve/__init__.py deleted file mode 100644 index 82a78321eb8..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/__init__.py +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module for Detect-to-Retrieve technique.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=unused-import -from delf.python.detect_to_retrieve import aggregation_extraction -from delf.python.detect_to_retrieve import boxes_and_features_extraction -# pylint: enable=unused-import diff --git a/research/delf/delf/python/detect_to_retrieve/aggregation_extraction.py b/research/delf/delf/python/detect_to_retrieve/aggregation_extraction.py deleted file mode 100644 index 4ddab944b8a..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/aggregation_extraction.py +++ /dev/null @@ -1,193 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Library to extract/save feature aggregation.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import csv -import os -import time - -import numpy as np -import tensorflow as tf - -from google.protobuf import text_format -from delf import aggregation_config_pb2 -from delf import datum_io -from delf import feature_aggregation_extractor -from delf import feature_io - -# Aliases for aggregation types. -_VLAD = aggregation_config_pb2.AggregationConfig.VLAD -_ASMK = aggregation_config_pb2.AggregationConfig.ASMK -_ASMK_STAR = aggregation_config_pb2.AggregationConfig.ASMK_STAR - -# Extensions. -_DELF_EXTENSION = '.delf' -_VLAD_EXTENSION_SUFFIX = 'vlad' -_ASMK_EXTENSION_SUFFIX = 'asmk' -_ASMK_STAR_EXTENSION_SUFFIX = 'asmk_star' - -# Pace to report extraction log. -_STATUS_CHECK_ITERATIONS = 50 - - -def _ReadMappingBasenameToBoxNames(input_path, index_image_names): - """Reads mapping from image name to DELF file names for each box. - - Args: - input_path: Path to CSV file containing mapping. - index_image_names: List containing index image names, in order, for the - dataset under consideration. - - Returns: - images_to_box_feature_files: Dict. key=string (image name); value=list of - strings (file names containing DELF features for boxes). - """ - images_to_box_feature_files = {} - with tf.io.gfile.GFile(input_path, 'r') as f: - reader = csv.DictReader(f) - for row in reader: - index_image_name = index_image_names[int(row['index_image_id'])] - if index_image_name not in images_to_box_feature_files: - images_to_box_feature_files[index_image_name] = [] - - images_to_box_feature_files[index_image_name].append(row['name']) - - return images_to_box_feature_files - - -def ExtractAggregatedRepresentationsToFiles(image_names, features_dir, - aggregation_config_path, - mapping_path, - output_aggregation_dir): - """Extracts aggregated feature representations, saving them to files. - - It checks if the aggregated representation for an image already exists, - and skips computation for those. - - Args: - image_names: List of image names. These are used to compose input file names - for the feature files, and the output file names for aggregated - representations. - features_dir: Directory where DELF features are located. - aggregation_config_path: Path to AggregationConfig proto text file with - configuration to be used for extraction. - mapping_path: Optional CSV file which maps each .delf file name to the index - image ID and detected box ID. If regional aggregation is performed, this - should be set. Otherwise, this is ignored. - output_aggregation_dir: Directory where aggregation output will be written - to. - - Raises: - ValueError: If AggregationConfig is malformed, or `mapping_path` is - missing. - """ - num_images = len(image_names) - - # Parse AggregationConfig proto, and select output extension. - config = aggregation_config_pb2.AggregationConfig() - with tf.io.gfile.GFile(aggregation_config_path, 'r') as f: - text_format.Merge(f.read(), config) - output_extension = '.' - if config.use_regional_aggregation: - output_extension += 'r' - if config.aggregation_type == _VLAD: - output_extension += _VLAD_EXTENSION_SUFFIX - elif config.aggregation_type == _ASMK: - output_extension += _ASMK_EXTENSION_SUFFIX - elif config.aggregation_type == _ASMK_STAR: - output_extension += _ASMK_STAR_EXTENSION_SUFFIX - else: - raise ValueError('Invalid aggregation type: %d' % config.aggregation_type) - - # Read index mapping path, if provided. - if mapping_path: - images_to_box_feature_files = _ReadMappingBasenameToBoxNames( - mapping_path, image_names) - - # Create output directory if necessary. - if not tf.io.gfile.exists(output_aggregation_dir): - tf.io.gfile.makedirs(output_aggregation_dir) - - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - - start = time.time() - for i in range(num_images): - if i == 0: - print('Starting to extract aggregation from images...') - elif i % _STATUS_CHECK_ITERATIONS == 0: - elapsed = (time.time() - start) - print('Processing image %d out of %d, last %d ' - 'images took %f seconds' % - (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed)) - start = time.time() - - image_name = image_names[i] - - # Compose output file name, skip extraction for this image if it already - # exists. - output_aggregation_filename = os.path.join(output_aggregation_dir, - image_name + output_extension) - if tf.io.gfile.exists(output_aggregation_filename): - print('Skipping %s' % image_name) - continue - - # Load DELF features. - if config.use_regional_aggregation: - if not mapping_path: - raise ValueError( - 'Requested regional aggregation, but mapping_path was not ' - 'provided') - descriptors_list = [] - num_features_per_box = [] - for box_feature_file in images_to_box_feature_files[image_name]: - delf_filename = os.path.join(features_dir, - box_feature_file + _DELF_EXTENSION) - _, _, box_descriptors, _, _ = feature_io.ReadFromFile(delf_filename) - # If `box_descriptors` is empty, reshape it such that it can be - # concatenated with other descriptors. - if not box_descriptors.shape[0]: - box_descriptors = np.reshape(box_descriptors, - [0, config.feature_dimensionality]) - descriptors_list.append(box_descriptors) - num_features_per_box.append(box_descriptors.shape[0]) - - descriptors = np.concatenate(descriptors_list) - else: - input_delf_filename = os.path.join(features_dir, - image_name + _DELF_EXTENSION) - _, _, descriptors, _, _ = feature_io.ReadFromFile(input_delf_filename) - # If `descriptors` is empty, reshape it to avoid extraction failure. - if not descriptors.shape[0]: - descriptors = np.reshape(descriptors, - [0, config.feature_dimensionality]) - num_features_per_box = None - - # Extract and save aggregation. If using VLAD, only - # `aggregated_descriptors` needs to be saved. - (aggregated_descriptors, - feature_visual_words) = extractor.Extract(descriptors, - num_features_per_box) - if config.aggregation_type == _VLAD: - datum_io.WriteToFile(aggregated_descriptors, - output_aggregation_filename) - else: - datum_io.WritePairToFile(aggregated_descriptors, - feature_visual_words.astype('uint32'), - output_aggregation_filename) diff --git a/research/delf/delf/python/detect_to_retrieve/boxes_and_features_extraction.py b/research/delf/delf/python/detect_to_retrieve/boxes_and_features_extraction.py deleted file mode 100644 index 1faef983b2e..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/boxes_and_features_extraction.py +++ /dev/null @@ -1,202 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Library to extract/save boxes and DELF features.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import csv -import math -import os -import time - -import numpy as np -import tensorflow as tf - -from google.protobuf import text_format -from delf import delf_config_pb2 -from delf import box_io -from delf import feature_io -from delf import utils -from delf import detector -from delf import extractor - -# Extension of feature files. -_BOX_EXTENSION = '.boxes' -_DELF_EXTENSION = '.delf' - -# Pace to report extraction log. -_STATUS_CHECK_ITERATIONS = 100 - - -def _WriteMappingBasenameToIds(index_names_ids_and_boxes, output_path): - """Helper function to write CSV mapping from DELF file name to IDs. - - Args: - index_names_ids_and_boxes: List containing 3-element lists with name, image - ID and box ID. - output_path: Output CSV path. - """ - with tf.io.gfile.GFile(output_path, 'w') as f: - csv_writer = csv.DictWriter( - f, fieldnames=['name', 'index_image_id', 'box_id']) - csv_writer.writeheader() - for name_imid_boxid in index_names_ids_and_boxes: - csv_writer.writerow({ - 'name': name_imid_boxid[0], - 'index_image_id': name_imid_boxid[1], - 'box_id': name_imid_boxid[2], - }) - - -def ExtractBoxesAndFeaturesToFiles(image_names, image_paths, delf_config_path, - detector_model_dir, detector_thresh, - output_features_dir, output_boxes_dir, - output_mapping): - """Extracts boxes and features, saving them to files. - - Boxes are saved to .boxes files. DELF features are extracted for - the entire image and saved into .delf files. In addition, DELF - features are extracted for each high-confidence bounding box in the image, and - saved into files named _0.delf, _1.delf, etc. - - It checks if descriptors/boxes already exist, and skips computation for those. - - Args: - image_names: List of image names. These are used to compose output file - names for boxes and features. - image_paths: List of image paths. image_paths[i] is the path for the image - named by image_names[i]. `image_names` and `image_paths` must have the - same number of elements. - delf_config_path: Path to DelfConfig proto text file. - detector_model_dir: Directory where detector SavedModel is located. - detector_thresh: Threshold used to decide if an image's detected box - undergoes feature extraction. - output_features_dir: Directory where DELF features will be written to. - output_boxes_dir: Directory where detected boxes will be written to. - output_mapping: CSV file which maps each .delf file name to the image ID and - detected box ID. - - Raises: - ValueError: If len(image_names) and len(image_paths) are different. - """ - num_images = len(image_names) - if len(image_paths) != num_images: - raise ValueError( - 'image_names and image_paths have different number of items') - - # Parse DelfConfig proto. - config = delf_config_pb2.DelfConfig() - with tf.io.gfile.GFile(delf_config_path, 'r') as f: - text_format.Merge(f.read(), config) - - # Create output directories if necessary. - if not tf.io.gfile.exists(output_features_dir): - tf.io.gfile.makedirs(output_features_dir) - if not tf.io.gfile.exists(output_boxes_dir): - tf.io.gfile.makedirs(output_boxes_dir) - if not tf.io.gfile.exists(os.path.dirname(output_mapping)): - tf.io.gfile.makedirs(os.path.dirname(output_mapping)) - - names_ids_and_boxes = [] - detector_fn = detector.MakeDetector(detector_model_dir) - delf_extractor_fn = extractor.MakeExtractor(config) - - start = time.time() - for i in range(num_images): - if i == 0: - print('Starting to extract features/boxes...') - elif i % _STATUS_CHECK_ITERATIONS == 0: - elapsed = (time.time() - start) - print('Processing image %d out of %d, last %d ' - 'images took %f seconds' % - (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed)) - start = time.time() - - image_name = image_names[i] - output_feature_filename_whole_image = os.path.join( - output_features_dir, image_name + _DELF_EXTENSION) - output_box_filename = os.path.join(output_boxes_dir, - image_name + _BOX_EXTENSION) - - pil_im = utils.RgbLoader(image_paths[i]) - width, height = pil_im.size - - # Extract and save boxes. - if tf.io.gfile.exists(output_box_filename): - print('Skipping box computation for %s' % image_name) - (boxes_out, scores_out, - class_indices_out) = box_io.ReadFromFile(output_box_filename) - else: - (boxes_out, scores_out, - class_indices_out) = detector_fn(np.expand_dims(pil_im, 0)) - # Using only one image per batch. - boxes_out = boxes_out[0] - scores_out = scores_out[0] - class_indices_out = class_indices_out[0] - box_io.WriteToFile(output_box_filename, boxes_out, scores_out, - class_indices_out) - - # Select boxes with scores greater than threshold. Those will be the - # ones with extracted DELF features (besides the whole image, whose DELF - # features are extracted in all cases). - num_delf_files = 1 - selected_boxes = [] - for box_ind, box in enumerate(boxes_out): - if scores_out[box_ind] >= detector_thresh: - selected_boxes.append(box) - num_delf_files += len(selected_boxes) - - # Extract and save DELF features. - for delf_file_ind in range(num_delf_files): - if delf_file_ind == 0: - box_name = image_name - output_feature_filename = output_feature_filename_whole_image - else: - box_name = image_name + '_' + str(delf_file_ind - 1) - output_feature_filename = os.path.join(output_features_dir, - box_name + _DELF_EXTENSION) - - names_ids_and_boxes.append([box_name, i, delf_file_ind - 1]) - - if tf.io.gfile.exists(output_feature_filename): - print('Skipping DELF computation for %s' % box_name) - continue - - if delf_file_ind >= 1: - bbox_for_cropping = selected_boxes[delf_file_ind - 1] - bbox_for_cropping_pil_convention = [ - int(math.floor(bbox_for_cropping[1] * width)), - int(math.floor(bbox_for_cropping[0] * height)), - int(math.ceil(bbox_for_cropping[3] * width)), - int(math.ceil(bbox_for_cropping[2] * height)) - ] - pil_cropped_im = pil_im.crop(bbox_for_cropping_pil_convention) - im = np.array(pil_cropped_im) - else: - im = np.array(pil_im) - - extracted_features = delf_extractor_fn(im) - locations_out = extracted_features['local_features']['locations'] - descriptors_out = extracted_features['local_features']['descriptors'] - feature_scales_out = extracted_features['local_features']['scales'] - attention_out = extracted_features['local_features']['attention'] - - feature_io.WriteToFile(output_feature_filename, locations_out, - feature_scales_out, descriptors_out, attention_out) - - # Save mapping from output DELF name to image id and box id. - _WriteMappingBasenameToIds(names_ids_and_boxes, output_mapping) diff --git a/research/delf/delf/python/detect_to_retrieve/cluster_delf_features.py b/research/delf/delf/python/detect_to_retrieve/cluster_delf_features.py deleted file mode 100644 index f77d47b1db9..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/cluster_delf_features.py +++ /dev/null @@ -1,214 +0,0 @@ -# Lint as: python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Clusters DELF features using the K-means algorithm. - -All DELF local feature descriptors for a given dataset's index images are loaded -as the input. - -Note that: -- we only use features extracted from whole images (no features from boxes are - used). -- the codebook should be trained on Paris images for Oxford retrieval - experiments, and vice-versa. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import os -import sys -import time - -from absl import app -import numpy as np -import tensorflow as tf - -from delf import feature_io -from delf.python.datasets.revisited_op import dataset - -cmd_args = None - -# Extensions. -_DELF_EXTENSION = '.delf' - -# Default DELF dimensionality. -_DELF_DIM = 128 - -# Pace to report log when collecting features. -_STATUS_CHECK_ITERATIONS = 100 - - -class _IteratorInitHook(tf.estimator.SessionRunHook): - """Hook to initialize data iterator after session is created.""" - - def __init__(self): - super(_IteratorInitHook, self).__init__() - self.iterator_initializer_fn = None - - def after_create_session(self, session, coord): - """Initialize the iterator after the session has been created.""" - del coord - self.iterator_initializer_fn(session) - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Process output directory. - if tf.io.gfile.exists(cmd_args.output_cluster_dir): - raise RuntimeError( - 'output_cluster_dir = %s already exists. This may indicate that a ' - 'previous run already wrote checkpoints in this directory, which would ' - 'lead to incorrect training. Please re-run this script by specifying an' - ' inexisting directory.' % cmd_args.output_cluster_dir) - else: - tf.io.gfile.makedirs(cmd_args.output_cluster_dir) - - # Read list of index images from dataset file. - print('Reading list of index images from dataset file...') - _, index_list, _ = dataset.ReadDatasetFile(cmd_args.dataset_file_path) - num_images = len(index_list) - print('done! Found %d images' % num_images) - - # Loop over list of index images and collect DELF features. - features_for_clustering = [] - start = time.clock() - print('Starting to collect features from index images...') - for i in range(num_images): - if i > 0 and i % _STATUS_CHECK_ITERATIONS == 0: - elapsed = (time.clock() - start) - print('Processing index image %d out of %d, last %d ' - 'images took %f seconds' % - (i, num_images, _STATUS_CHECK_ITERATIONS, elapsed)) - start = time.clock() - - features_filename = index_list[i] + _DELF_EXTENSION - features_fullpath = os.path.join(cmd_args.features_dir, features_filename) - _, _, features, _, _ = feature_io.ReadFromFile(features_fullpath) - if features.size != 0: - assert features.shape[1] == _DELF_DIM - for feature in features: - features_for_clustering.append(feature) - - features_for_clustering = np.array(features_for_clustering, dtype=np.float32) - print('All features were loaded! There are %d features, each with %d ' - 'dimensions' % - (features_for_clustering.shape[0], features_for_clustering.shape[1])) - - # Run K-means clustering. - def _get_input_fn(): - """Helper function to create input function and hook for training. - - Returns: - input_fn: Input function for k-means Estimator training. - init_hook: Hook used to load data during training. - """ - init_hook = _IteratorInitHook() - - def _input_fn(): - """Produces tf.data.Dataset object for k-means training. - - Returns: - Tensor with the data for training. - """ - features_placeholder = tf.compat.v1.placeholder( - tf.float32, features_for_clustering.shape) - delf_dataset = tf.data.Dataset.from_tensor_slices((features_placeholder)) - delf_dataset = delf_dataset.shuffle(1000).batch( - features_for_clustering.shape[0]) - iterator = tf.compat.v1.data.make_initializable_iterator(delf_dataset) - - def _initializer_fn(sess): - """Initialize dataset iterator, feed in the data.""" - sess.run( - iterator.initializer, - feed_dict={features_placeholder: features_for_clustering}) - - init_hook.iterator_initializer_fn = _initializer_fn - return iterator.get_next() - - return _input_fn, init_hook - - input_fn, init_hook = _get_input_fn() - - kmeans = tf.compat.v1.estimator.experimental.KMeans( - num_clusters=cmd_args.num_clusters, - model_dir=cmd_args.output_cluster_dir, - use_mini_batch=False, - ) - - print('Starting K-means clustering...') - start = time.clock() - for i in range(cmd_args.num_iterations): - kmeans.train(input_fn, hooks=[init_hook]) - average_sum_squared_error = kmeans.evaluate( - input_fn, hooks=[init_hook])['score'] / features_for_clustering.shape[0] - elapsed = (time.clock() - start) - print('K-means iteration %d (out of %d) took %f seconds, ' - 'average-sum-of-squares: %f' % - (i, cmd_args.num_iterations, elapsed, average_sum_squared_error)) - start = time.clock() - - print('K-means clustering finished!') - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--dataset_file_path', - type=str, - default='/tmp/gnd_roxford5k.mat', - help=""" - Dataset file for Revisited Oxford or Paris dataset, in .mat format. The - list of index images loaded from this file is used to collect local - features, which are assumed to be in .delf file format. - """) - parser.add_argument( - '--features_dir', - type=str, - default='/tmp/features', - help=""" - Directory where DELF feature files are to be found. - """) - parser.add_argument( - '--num_clusters', - type=int, - default=1024, - help=""" - Number of clusters to use. - """) - parser.add_argument( - '--num_iterations', - type=int, - default=50, - help=""" - Number of iterations to use. - """) - parser.add_argument( - '--output_cluster_dir', - type=str, - default='/tmp/cluster', - help=""" - Directory where clustering outputs are written to. This directory should - not exist before running this script; it will be created during - clustering. - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/detect_to_retrieve/delf_gld_config.pbtxt b/research/delf/delf/python/detect_to_retrieve/delf_gld_config.pbtxt deleted file mode 100644 index 046aed766ce..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/delf_gld_config.pbtxt +++ /dev/null @@ -1,25 +0,0 @@ -model_path: "parameters/delf_gld_20190411/model" -image_scales: .25 -image_scales: .3536 -image_scales: .5 -image_scales: .7071 -image_scales: 1.0 -image_scales: 1.4142 -image_scales: 2.0 -delf_local_config { - use_pca: true - # Note that for the exported model provided as an example, layer_name and - # iou_threshold are hard-coded in the checkpoint. So, the layer_name and - # iou_threshold variables here have no effect on the provided - # extract_features.py script. - layer_name: "resnet_v1_50/block3" - iou_threshold: 1.0 - max_feature_num: 1000 - score_threshold: 100.0 - pca_parameters { - mean_path: "parameters/delf_gld_20190411/pca/mean.datum" - projection_matrix_path: "parameters/delf_gld_20190411/pca/pca_proj_mat.datum" - pca_dim: 128 - use_whitening: false - } -} diff --git a/research/delf/delf/python/detect_to_retrieve/extract_aggregation.py b/research/delf/delf/python/detect_to_retrieve/extract_aggregation.py deleted file mode 100644 index 451c4137d93..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/extract_aggregation.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Extracts aggregation for images from Revisited Oxford/Paris datasets. - -The program checks if the aggregated representation for an image already exists, -and skips computation for those. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import sys - -from absl import app -from delf.python.datasets.revisited_op import dataset -from delf.python.detect_to_retrieve import aggregation_extraction - -cmd_args = None - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read list of images from dataset file. - print('Reading list of images from dataset file...') - query_list, index_list, _ = dataset.ReadDatasetFile( - cmd_args.dataset_file_path) - if cmd_args.use_query_images: - image_list = query_list - else: - image_list = index_list - num_images = len(image_list) - print('done! Found %d images' % num_images) - - aggregation_extraction.ExtractAggregatedRepresentationsToFiles( - image_names=image_list, - features_dir=cmd_args.features_dir, - aggregation_config_path=cmd_args.aggregation_config_path, - mapping_path=cmd_args.index_mapping_path, - output_aggregation_dir=cmd_args.output_aggregation_dir) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--aggregation_config_path', - type=str, - default='/tmp/aggregation_config.pbtxt', - help=""" - Path to AggregationConfig proto text file with configuration to be used - for extraction. - """) - parser.add_argument( - '--dataset_file_path', - type=str, - default='/tmp/gnd_roxford5k.mat', - help=""" - Dataset file for Revisited Oxford or Paris dataset, in .mat format. - """) - parser.add_argument( - '--use_query_images', - type=lambda x: (str(x).lower() == 'true'), - default=False, - help=""" - If True, processes the query images of the dataset. If False, processes - the database (ie, index) images. - """) - parser.add_argument( - '--features_dir', - type=str, - default='/tmp/features', - help=""" - Directory where image features are located, all in .delf format. - """) - parser.add_argument( - '--index_mapping_path', - type=str, - default='', - help=""" - Optional CSV file which maps each .delf file name to the index image ID - and detected box ID. If regional aggregation is performed, this should be - set. Otherwise, this is ignored. - Usually this file is obtained as an output from the - `extract_index_boxes_and_features.py` script. - """) - parser.add_argument( - '--output_aggregation_dir', - type=str, - default='/tmp/aggregation', - help=""" - Directory where aggregation output will be written to. Each image's - features will be written to a file with same name, and extension replaced - by one of - ['.vlad', '.asmk', '.asmk_star', '.rvlad', '.rasmk', '.rasmk_star']. - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/detect_to_retrieve/extract_index_boxes_and_features.py b/research/delf/delf/python/detect_to_retrieve/extract_index_boxes_and_features.py deleted file mode 100644 index 80bd721c874..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/extract_index_boxes_and_features.py +++ /dev/null @@ -1,151 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Extracts DELF and boxes from the Revisited Oxford/Paris index datasets. - -Boxes are saved to .boxes files. DELF features are extracted for the -entire image and saved into .delf files. In addition, DELF features -are extracted for each high-confidence bounding box in the image, and saved into -files named _0.delf, _1.delf, etc. - -The program checks if descriptors/boxes already exist, and skips computation for -those. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import os -import sys - -from absl import app -from delf.python.datasets.revisited_op import dataset -from delf.python.detect_to_retrieve import boxes_and_features_extraction - -cmd_args = None - -_IMAGE_EXTENSION = '.jpg' - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read list of index images from dataset file. - print('Reading list of index images from dataset file...') - _, index_list, _ = dataset.ReadDatasetFile(cmd_args.dataset_file_path) - num_images = len(index_list) - print('done! Found %d images' % num_images) - - # Compose list of image paths. - image_paths = [ - os.path.join(cmd_args.images_dir, index_image_name + _IMAGE_EXTENSION) - for index_image_name in index_list - ] - - # Extract boxes/features and save them to files. - boxes_and_features_extraction.ExtractBoxesAndFeaturesToFiles( - image_names=index_list, - image_paths=image_paths, - delf_config_path=cmd_args.delf_config_path, - detector_model_dir=cmd_args.detector_model_dir, - detector_thresh=cmd_args.detector_thresh, - output_features_dir=cmd_args.output_features_dir, - output_boxes_dir=cmd_args.output_boxes_dir, - output_mapping=cmd_args.output_index_mapping) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--delf_config_path', - type=str, - default='/tmp/delf_config_example.pbtxt', - help=""" - Path to DelfConfig proto text file with configuration to be used for DELF - extraction. - """) - parser.add_argument( - '--detector_model_dir', - type=str, - default='/tmp/detector_model', - help=""" - Directory where detector SavedModel is located. - """) - parser.add_argument( - '--detector_thresh', - type=float, - default=0.1, - help=""" - Threshold used to decide if an image's detected box undergoes feature - extraction. For all detected boxes with detection score larger than this, - a .delf file is saved containing the box features. Note that this - threshold is used only to select which boxes are used in feature - extraction; all detected boxes are actually saved in the .boxes file, even - those with score lower than detector_thresh. - """) - parser.add_argument( - '--dataset_file_path', - type=str, - default='/tmp/gnd_roxford5k.mat', - help=""" - Dataset file for Revisited Oxford or Paris dataset, in .mat format. - """) - parser.add_argument( - '--images_dir', - type=str, - default='/tmp/images', - help=""" - Directory where dataset images are located, all in .jpg format. - """) - parser.add_argument( - '--output_boxes_dir', - type=str, - default='/tmp/boxes', - help=""" - Directory where detected boxes will be written to. Each image's boxes - will be written to a file with same name, and extension replaced by - .boxes. - """) - parser.add_argument( - '--output_features_dir', - type=str, - default='/tmp/features', - help=""" - Directory where DELF features will be written to. Each image's features - will be written to a file with same name, and extension replaced by .delf, - eg: .delf. In addition, DELF features are extracted for each - high-confidence bounding box in the image, and saved into files named - _0.delf, _1.delf, etc. - """) - parser.add_argument( - '--output_index_mapping', - type=str, - default='/tmp/index_mapping.csv', - help=""" - CSV file which maps each .delf file name to the index image ID and - detected box ID. The format is 'name,index_image_id,box_id', including a - header. The 'name' refers to the .delf file name without extension. - - For example, a few lines may be like: - 'radcliffe_camera_000158,2,-1' - 'radcliffe_camera_000158_0,2,0' - 'radcliffe_camera_000158_1,2,1' - 'radcliffe_camera_000158_2,2,2' - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/detect_to_retrieve/extract_query_features.py b/research/delf/delf/python/detect_to_retrieve/extract_query_features.py deleted file mode 100644 index 2ff4a5a23f5..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/extract_query_features.py +++ /dev/null @@ -1,137 +0,0 @@ -# Lint as: python3 -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Extracts DELF features for query images from Revisited Oxford/Paris datasets. - -Note that query images are cropped before feature extraction, as required by the -evaluation protocols of these datasets. - -The program checks if descriptors already exist, and skips computation for -those. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import os -import sys -import time - -from absl import app -import numpy as np -import tensorflow as tf - -from google.protobuf import text_format -from delf import delf_config_pb2 -from delf import feature_io -from delf import utils -from delf.python.datasets.revisited_op import dataset -from delf import extractor - -cmd_args = None - -# Extensions. -_DELF_EXTENSION = '.delf' -_IMAGE_EXTENSION = '.jpg' - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read list of query images from dataset file. - print('Reading list of query images and boxes from dataset file...') - query_list, _, ground_truth = dataset.ReadDatasetFile( - cmd_args.dataset_file_path) - num_images = len(query_list) - print(f'done! Found {num_images} images') - - # Parse DelfConfig proto. - config = delf_config_pb2.DelfConfig() - with tf.io.gfile.GFile(cmd_args.delf_config_path, 'r') as f: - text_format.Merge(f.read(), config) - - # Create output directory if necessary. - if not tf.io.gfile.exists(cmd_args.output_features_dir): - tf.io.gfile.makedirs(cmd_args.output_features_dir) - - extractor_fn = extractor.MakeExtractor(config) - - start = time.time() - for i in range(num_images): - query_image_name = query_list[i] - input_image_filename = os.path.join(cmd_args.images_dir, - query_image_name + _IMAGE_EXTENSION) - output_feature_filename = os.path.join(cmd_args.output_features_dir, - query_image_name + _DELF_EXTENSION) - if tf.io.gfile.exists(output_feature_filename): - print(f'Skipping {query_image_name}') - continue - - # Crop query image according to bounding box. - bbox = [int(round(b)) for b in ground_truth[i]['bbx']] - im = np.array(utils.RgbLoader(input_image_filename).crop(bbox)) - - # Extract and save features. - extracted_features = extractor_fn(im) - locations_out = extracted_features['local_features']['locations'] - descriptors_out = extracted_features['local_features']['descriptors'] - feature_scales_out = extracted_features['local_features']['scales'] - attention_out = extracted_features['local_features']['attention'] - - feature_io.WriteToFile(output_feature_filename, locations_out, - feature_scales_out, descriptors_out, attention_out) - - elapsed = (time.time() - start) - print('Processed %d query images in %f seconds' % (num_images, elapsed)) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--delf_config_path', - type=str, - default='/tmp/delf_config_example.pbtxt', - help=""" - Path to DelfConfig proto text file with configuration to be used for DELF - extraction. - """) - parser.add_argument( - '--dataset_file_path', - type=str, - default='/tmp/gnd_roxford5k.mat', - help=""" - Dataset file for Revisited Oxford or Paris dataset, in .mat format. - """) - parser.add_argument( - '--images_dir', - type=str, - default='/tmp/images', - help=""" - Directory where dataset images are located, all in .jpg format. - """) - parser.add_argument( - '--output_features_dir', - type=str, - default='/tmp/features', - help=""" - Directory where DELF features will be written to. Each image's features - will be written to a file with same name, and extension replaced by .delf. - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/detect_to_retrieve/image_reranking.py b/research/delf/delf/python/detect_to_retrieve/image_reranking.py deleted file mode 100644 index 8c115835d63..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/image_reranking.py +++ /dev/null @@ -1,303 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Library to re-rank images based on geometric verification.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import io -import os - -import matplotlib.pyplot as plt -import numpy as np -from scipy import spatial -from skimage import feature -from skimage import measure -from skimage import transform - -from delf import feature_io - -# Extensions. -_DELF_EXTENSION = '.delf' - -# Pace to log. -_STATUS_CHECK_GV_ITERATIONS = 10 - -# Re-ranking / geometric verification parameters. -_NUM_TO_RERANK = 100 -_NUM_RANSAC_TRIALS = 1000 -_MIN_RANSAC_SAMPLES = 3 - - -def MatchFeatures(query_locations, - query_descriptors, - index_image_locations, - index_image_descriptors, - ransac_seed=None, - descriptor_matching_threshold=0.9, - ransac_residual_threshold=10.0, - query_im_array=None, - index_im_array=None, - query_im_scale_factors=None, - index_im_scale_factors=None, - use_ratio_test=False): - """Matches local features using geometric verification. - - First, finds putative local feature matches by matching `query_descriptors` - against a KD-tree from the `index_image_descriptors`. Then, attempts to fit an - affine transformation between the putative feature corresponces using their - locations. - - Args: - query_locations: Locations of local features for query image. NumPy array of - shape [#query_features, 2]. - query_descriptors: Descriptors of local features for query image. NumPy - array of shape [#query_features, depth]. - index_image_locations: Locations of local features for index image. NumPy - array of shape [#index_image_features, 2]. - index_image_descriptors: Descriptors of local features for index image. - NumPy array of shape [#index_image_features, depth]. - ransac_seed: Seed used by RANSAC. If None (default), no seed is provided. - descriptor_matching_threshold: Threshold below which a pair of local - descriptors is considered a potential match, and will be fed into RANSAC. - If use_ratio_test==False, this is a simple distance threshold. If - use_ratio_test==True, this is Lowe's ratio test threshold. - ransac_residual_threshold: Residual error threshold for considering matches - as inliers, used in RANSAC algorithm. - query_im_array: Optional. If not None, contains a NumPy array with the query - image, used to produce match visualization, if there is a match. - index_im_array: Optional. Same as `query_im_array`, but for index image. - query_im_scale_factors: Optional. If not None, contains a NumPy array with - the query image scales, used to produce match visualization, if there is a - match. If None and a visualization will be produced, [1.0, 1.0] is used - (ie, feature locations are not scaled). - index_im_scale_factors: Optional. Same as `query_im_scale_factors`, but for - index image. - use_ratio_test: If True, descriptor matching is performed via ratio test, - instead of distance-based threshold. - - Returns: - score: Number of inliers of match. If no match is found, returns 0. - match_viz_bytes: Encoded image bytes with visualization of the match, if - there is one, and if `query_im_array` and `index_im_array` are properly - set. Otherwise, it's an empty bytes string. - - Raises: - ValueError: If local descriptors from query and index images have different - dimensionalities. - """ - num_features_query = query_locations.shape[0] - num_features_index_image = index_image_locations.shape[0] - if not num_features_query or not num_features_index_image: - return 0, b'' - - local_feature_dim = query_descriptors.shape[1] - if index_image_descriptors.shape[1] != local_feature_dim: - raise ValueError( - 'Local feature dimensionality is not consistent for query and index ' - 'images.') - - # Construct KD-tree used to find nearest neighbors. - index_image_tree = spatial.cKDTree(index_image_descriptors) - if use_ratio_test: - distances, indices = index_image_tree.query( - query_descriptors, k=2, n_jobs=-1) - query_locations_to_use = np.array([ - query_locations[i,] - for i in range(num_features_query) - if distances[i][0] < descriptor_matching_threshold * distances[i][1] - ]) - index_image_locations_to_use = np.array([ - index_image_locations[indices[i][0],] - for i in range(num_features_query) - if distances[i][0] < descriptor_matching_threshold * distances[i][1] - ]) - else: - _, indices = index_image_tree.query( - query_descriptors, - distance_upper_bound=descriptor_matching_threshold, - n_jobs=-1) - - # Select feature locations for putative matches. - query_locations_to_use = np.array([ - query_locations[i,] - for i in range(num_features_query) - if indices[i] != num_features_index_image - ]) - index_image_locations_to_use = np.array([ - index_image_locations[indices[i],] - for i in range(num_features_query) - if indices[i] != num_features_index_image - ]) - - # If there are not enough putative matches, early return 0. - if query_locations_to_use.shape[0] <= _MIN_RANSAC_SAMPLES: - return 0, b'' - - # Perform geometric verification using RANSAC. - _, inliers = measure.ransac( - (index_image_locations_to_use, query_locations_to_use), - transform.AffineTransform, - min_samples=_MIN_RANSAC_SAMPLES, - residual_threshold=ransac_residual_threshold, - max_trials=_NUM_RANSAC_TRIALS, - random_state=ransac_seed) - match_viz_bytes = b'' - - if inliers is None: - inliers = [] - elif query_im_array is not None and index_im_array is not None: - if query_im_scale_factors is None: - query_im_scale_factors = [1.0, 1.0] - if index_im_scale_factors is None: - index_im_scale_factors = [1.0, 1.0] - inlier_idxs = np.nonzero(inliers)[0] - _, ax = plt.subplots() - ax.axis('off') - ax.xaxis.set_major_locator(plt.NullLocator()) - ax.yaxis.set_major_locator(plt.NullLocator()) - plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0) - plt.margins(0, 0) - feature.plot_matches( - ax, - query_im_array, - index_im_array, - query_locations_to_use * query_im_scale_factors, - index_image_locations_to_use * index_im_scale_factors, - np.column_stack((inlier_idxs, inlier_idxs)), - only_matches=True) - - match_viz_io = io.BytesIO() - plt.savefig(match_viz_io, format='jpeg', bbox_inches='tight', pad_inches=0) - match_viz_bytes = match_viz_io.getvalue() - - return sum(inliers), match_viz_bytes - - -def RerankByGeometricVerification(input_ranks, - initial_scores, - query_name, - index_names, - query_features_dir, - index_features_dir, - junk_ids, - local_feature_extension=_DELF_EXTENSION, - ransac_seed=None, - descriptor_matching_threshold=0.9, - ransac_residual_threshold=10.0, - use_ratio_test=False): - """Re-ranks retrieval results using geometric verification. - - Args: - input_ranks: 1D NumPy array with indices of top-ranked index images, sorted - from the most to the least similar. - initial_scores: 1D NumPy array with initial similarity scores between query - and index images. Entry i corresponds to score for image i. - query_name: Name for query image (string). - index_names: List of names for index images (strings). - query_features_dir: Directory where query local feature file is located - (string). - index_features_dir: Directory where index local feature files are located - (string). - junk_ids: Set with indices of junk images which should not be considered - during re-ranking. - local_feature_extension: String, extension to use for loading local feature - files. - ransac_seed: Seed used by RANSAC. If None (default), no seed is provided. - descriptor_matching_threshold: Threshold used for local descriptor matching. - ransac_residual_threshold: Residual error threshold for considering matches - as inliers, used in RANSAC algorithm. - use_ratio_test: If True, descriptor matching is performed via ratio test, - instead of distance-based threshold. - - Returns: - output_ranks: 1D NumPy array with index image indices, sorted from the most - to the least similar according to the geometric verification and initial - scores. - - Raises: - ValueError: If `input_ranks`, `initial_scores` and `index_names` do not have - the same number of entries. - """ - num_index_images = len(index_names) - if len(input_ranks) != num_index_images: - raise ValueError('input_ranks and index_names have different number of ' - 'elements: %d vs %d' % - (len(input_ranks), len(index_names))) - if len(initial_scores) != num_index_images: - raise ValueError('initial_scores and index_names have different number of ' - 'elements: %d vs %d' % - (len(initial_scores), len(index_names))) - - # Filter out junk images from list that will be re-ranked. - input_ranks_for_gv = [] - for ind in input_ranks: - if ind not in junk_ids: - input_ranks_for_gv.append(ind) - num_to_rerank = min(_NUM_TO_RERANK, len(input_ranks_for_gv)) - - # Load query image features. - query_features_path = os.path.join(query_features_dir, - query_name + local_feature_extension) - query_locations, _, query_descriptors, _, _ = feature_io.ReadFromFile( - query_features_path) - - # Initialize list containing number of inliers and initial similarity scores. - inliers_and_initial_scores = [] - for i in range(num_index_images): - inliers_and_initial_scores.append([0, initial_scores[i]]) - - # Loop over top-ranked images and get results. - print('Starting to re-rank') - for i in range(num_to_rerank): - if i > 0 and i % _STATUS_CHECK_GV_ITERATIONS == 0: - print('Re-ranking: i = %d out of %d' % (i, num_to_rerank)) - - index_image_id = input_ranks_for_gv[i] - - # Load index image features. - index_image_features_path = os.path.join( - index_features_dir, - index_names[index_image_id] + local_feature_extension) - (index_image_locations, _, index_image_descriptors, _, - _) = feature_io.ReadFromFile(index_image_features_path) - - inliers_and_initial_scores[index_image_id][0], _ = MatchFeatures( - query_locations, - query_descriptors, - index_image_locations, - index_image_descriptors, - ransac_seed=ransac_seed, - descriptor_matching_threshold=descriptor_matching_threshold, - ransac_residual_threshold=ransac_residual_threshold, - use_ratio_test=use_ratio_test) - - # Sort based on (inliers_score, initial_score). - def _InliersInitialScoresSorting(k): - """Helper function to sort list based on two entries. - - Args: - k: Index into `inliers_and_initial_scores`. - - Returns: - Tuple containing inlier score and initial score. - """ - return (inliers_and_initial_scores[k][0], inliers_and_initial_scores[k][1]) - - output_ranks = sorted( - range(num_index_images), key=_InliersInitialScoresSorting, reverse=True) - - return output_ranks diff --git a/research/delf/delf/python/detect_to_retrieve/index_aggregation_config.pbtxt b/research/delf/delf/python/detect_to_retrieve/index_aggregation_config.pbtxt deleted file mode 100644 index ba7ba4e4956..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/index_aggregation_config.pbtxt +++ /dev/null @@ -1,10 +0,0 @@ -codebook_size: 65536 -feature_dimensionality: 128 -aggregation_type: ASMK_STAR -use_l2_normalization: false -codebook_path: "parameters/rparis6k_codebook_65536/k65536_codebook_tfckpt/codebook" -num_assignments: 1 -use_regional_aggregation: true -feature_batch_size: 100 -alpha: 3.0 -tau: 0.0 diff --git a/research/delf/delf/python/detect_to_retrieve/perform_retrieval.py b/research/delf/delf/python/detect_to_retrieve/perform_retrieval.py deleted file mode 100644 index 2b7a2278925..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/perform_retrieval.py +++ /dev/null @@ -1,302 +0,0 @@ -# Lint as: python3 -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Performs image retrieval on Revisited Oxford/Paris datasets.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import os -import sys -import time - -from absl import app -import numpy as np -import tensorflow as tf - -from google.protobuf import text_format -from delf import aggregation_config_pb2 -from delf import datum_io -from delf import feature_aggregation_similarity -from delf.python.datasets.revisited_op import dataset -from delf.python.detect_to_retrieve import image_reranking - -cmd_args = None - -# Aliases for aggregation types. -_VLAD = aggregation_config_pb2.AggregationConfig.VLAD -_ASMK = aggregation_config_pb2.AggregationConfig.ASMK -_ASMK_STAR = aggregation_config_pb2.AggregationConfig.ASMK_STAR - -# Extensions. -_VLAD_EXTENSION_SUFFIX = 'vlad' -_ASMK_EXTENSION_SUFFIX = 'asmk' -_ASMK_STAR_EXTENSION_SUFFIX = 'asmk_star' - -# Precision-recall ranks to use in metric computation. -_PR_RANKS = (1, 5, 10) - -# Pace to log. -_STATUS_CHECK_LOAD_ITERATIONS = 50 - -# Output file names. -_METRICS_FILENAME = 'metrics.txt' - - -def _ReadAggregatedDescriptors(input_dir, image_list, config): - """Reads aggregated descriptors. - - Args: - input_dir: Directory where aggregated descriptors are located. - image_list: List of image names for which to load descriptors. - config: AggregationConfig used for images. - - Returns: - aggregated_descriptors: List containing #images items, each a 1D NumPy - array. - visual_words: If using VLAD aggregation, returns an empty list. Otherwise, - returns a list containing #images items, each a 1D NumPy array. - """ - # Compose extension of aggregated descriptors. - extension = '.' - if config.use_regional_aggregation: - extension += 'r' - if config.aggregation_type == _VLAD: - extension += _VLAD_EXTENSION_SUFFIX - elif config.aggregation_type == _ASMK: - extension += _ASMK_EXTENSION_SUFFIX - elif config.aggregation_type == _ASMK_STAR: - extension += _ASMK_STAR_EXTENSION_SUFFIX - else: - raise ValueError('Invalid aggregation type: %d' % config.aggregation_type) - - num_images = len(image_list) - aggregated_descriptors = [] - visual_words = [] - print('Starting to collect descriptors for %d images...' % num_images) - start = time.clock() - for i in range(num_images): - if i > 0 and i % _STATUS_CHECK_LOAD_ITERATIONS == 0: - elapsed = (time.clock() - start) - print('Reading descriptors for image %d out of %d, last %d ' - 'images took %f seconds' % - (i, num_images, _STATUS_CHECK_LOAD_ITERATIONS, elapsed)) - start = time.clock() - - descriptors_filename = image_list[i] + extension - descriptors_fullpath = os.path.join(input_dir, descriptors_filename) - if config.aggregation_type == _VLAD: - aggregated_descriptors.append(datum_io.ReadFromFile(descriptors_fullpath)) - else: - d, v = datum_io.ReadPairFromFile(descriptors_fullpath) - if config.aggregation_type == _ASMK_STAR: - d = d.astype('uint8') - - aggregated_descriptors.append(d) - visual_words.append(v) - - return aggregated_descriptors, visual_words - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Parse dataset to obtain query/index images, and ground-truth. - print('Parsing dataset...') - query_list, index_list, ground_truth = dataset.ReadDatasetFile( - cmd_args.dataset_file_path) - num_query_images = len(query_list) - num_index_images = len(index_list) - (_, medium_ground_truth, - hard_ground_truth) = dataset.ParseEasyMediumHardGroundTruth(ground_truth) - print('done! Found %d queries and %d index images' % - (num_query_images, num_index_images)) - - # Parse AggregationConfig protos. - query_config = aggregation_config_pb2.AggregationConfig() - with tf.io.gfile.GFile(cmd_args.query_aggregation_config_path, 'r') as f: - text_format.Merge(f.read(), query_config) - index_config = aggregation_config_pb2.AggregationConfig() - with tf.io.gfile.GFile(cmd_args.index_aggregation_config_path, 'r') as f: - text_format.Merge(f.read(), index_config) - - # Read aggregated descriptors. - query_aggregated_descriptors, query_visual_words = _ReadAggregatedDescriptors( - cmd_args.query_aggregation_dir, query_list, query_config) - index_aggregated_descriptors, index_visual_words = _ReadAggregatedDescriptors( - cmd_args.index_aggregation_dir, index_list, index_config) - - # Create similarity computer. - similarity_computer = ( - feature_aggregation_similarity.SimilarityAggregatedRepresentation( - index_config)) - - # Compute similarity between query and index images, potentially re-ranking - # with geometric verification. - ranks_before_gv = np.zeros([num_query_images, num_index_images], - dtype='int32') - if cmd_args.use_geometric_verification: - medium_ranks_after_gv = np.zeros([num_query_images, num_index_images], - dtype='int32') - hard_ranks_after_gv = np.zeros([num_query_images, num_index_images], - dtype='int32') - for i in range(num_query_images): - print('Performing retrieval with query %d (%s)...' % (i, query_list[i])) - start = time.clock() - - # Compute similarity between aggregated descriptors. - similarities = np.zeros([num_index_images]) - for j in range(num_index_images): - similarities[j] = similarity_computer.ComputeSimilarity( - query_aggregated_descriptors[i], index_aggregated_descriptors[j], - query_visual_words[i], index_visual_words[j]) - - ranks_before_gv[i] = np.argsort(-similarities) - - # Re-rank using geometric verification. - if cmd_args.use_geometric_verification: - medium_ranks_after_gv[i] = image_reranking.RerankByGeometricVerification( - ranks_before_gv[i], similarities, query_list[i], index_list, - cmd_args.query_features_dir, cmd_args.index_features_dir, - set(medium_ground_truth[i]['junk'])) - hard_ranks_after_gv[i] = image_reranking.RerankByGeometricVerification( - ranks_before_gv[i], similarities, query_list[i], index_list, - cmd_args.query_features_dir, cmd_args.index_features_dir, - set(hard_ground_truth[i]['junk'])) - - elapsed = (time.clock() - start) - print('done! Retrieval for query %d took %f seconds' % (i, elapsed)) - - # Create output directory if necessary. - if not tf.io.gfile.exists(cmd_args.output_dir): - tf.io.gfile.makedirs(cmd_args.output_dir) - - # Compute metrics. - medium_metrics = dataset.ComputeMetrics(ranks_before_gv, medium_ground_truth, - _PR_RANKS) - hard_metrics = dataset.ComputeMetrics(ranks_before_gv, hard_ground_truth, - _PR_RANKS) - if cmd_args.use_geometric_verification: - medium_metrics_after_gv = dataset.ComputeMetrics(medium_ranks_after_gv, - medium_ground_truth, - _PR_RANKS) - hard_metrics_after_gv = dataset.ComputeMetrics(hard_ranks_after_gv, - hard_ground_truth, _PR_RANKS) - - # Write metrics to file. - mean_average_precision_dict = { - 'medium': medium_metrics[0], - 'hard': hard_metrics[0] - } - mean_precisions_dict = {'medium': medium_metrics[1], 'hard': hard_metrics[1]} - mean_recalls_dict = {'medium': medium_metrics[2], 'hard': hard_metrics[2]} - if cmd_args.use_geometric_verification: - mean_average_precision_dict.update({ - 'medium_after_gv': medium_metrics_after_gv[0], - 'hard_after_gv': hard_metrics_after_gv[0] - }) - mean_precisions_dict.update({ - 'medium_after_gv': medium_metrics_after_gv[1], - 'hard_after_gv': hard_metrics_after_gv[1] - }) - mean_recalls_dict.update({ - 'medium_after_gv': medium_metrics_after_gv[2], - 'hard_after_gv': hard_metrics_after_gv[2] - }) - dataset.SaveMetricsFile(mean_average_precision_dict, mean_precisions_dict, - mean_recalls_dict, _PR_RANKS, - os.path.join(cmd_args.output_dir, _METRICS_FILENAME)) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--index_aggregation_config_path', - type=str, - default='/tmp/index_aggregation_config.pbtxt', - help=""" - Path to index AggregationConfig proto text file. This is used to load the - aggregated descriptors from the index, and to define the parameters used - in computing similarity for aggregated descriptors. - """) - parser.add_argument( - '--query_aggregation_config_path', - type=str, - default='/tmp/query_aggregation_config.pbtxt', - help=""" - Path to query AggregationConfig proto text file. This is only used to load - the aggregated descriptors for the queries. - """) - parser.add_argument( - '--dataset_file_path', - type=str, - default='/tmp/gnd_roxford5k.mat', - help=""" - Dataset file for Revisited Oxford or Paris dataset, in .mat format. - """) - parser.add_argument( - '--index_aggregation_dir', - type=str, - default='/tmp/index_aggregation', - help=""" - Directory where index aggregated descriptors are located. - """) - parser.add_argument( - '--query_aggregation_dir', - type=str, - default='/tmp/query_aggregation', - help=""" - Directory where query aggregated descriptors are located. - """) - parser.add_argument( - '--use_geometric_verification', - type=lambda x: (str(x).lower() == 'true'), - default=False, - help=""" - If True, performs re-ranking using local feature-based geometric - verification. - """) - parser.add_argument( - '--index_features_dir', - type=str, - default='/tmp/index_features', - help=""" - Only used if `use_geometric_verification` is True. - Directory where index local image features are located, all in .delf - format. - """) - parser.add_argument( - '--query_features_dir', - type=str, - default='/tmp/query_features', - help=""" - Only used if `use_geometric_verification` is True. - Directory where query local image features are located, all in .delf - format. - """) - parser.add_argument( - '--output_dir', - type=str, - default='/tmp/retrieval', - help=""" - Directory where retrieval output will be written to. A file containing - metrics for this run is saved therein, with file name "metrics.txt". - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/detect_to_retrieve/query_aggregation_config.pbtxt b/research/delf/delf/python/detect_to_retrieve/query_aggregation_config.pbtxt deleted file mode 100644 index 39a917eef43..00000000000 --- a/research/delf/delf/python/detect_to_retrieve/query_aggregation_config.pbtxt +++ /dev/null @@ -1,7 +0,0 @@ -codebook_size: 65536 -feature_dimensionality: 128 -aggregation_type: ASMK_STAR -codebook_path: "parameters/rparis6k_codebook_65536/k65536_codebook_tfckpt/codebook" -num_assignments: 1 -use_regional_aggregation: false -feature_batch_size: 100 diff --git a/research/delf/delf/python/examples/__init__.py b/research/delf/delf/python/examples/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/delf/delf/python/examples/delf_config_example.pbtxt b/research/delf/delf/python/examples/delf_config_example.pbtxt deleted file mode 100644 index ff2d9c0023c..00000000000 --- a/research/delf/delf/python/examples/delf_config_example.pbtxt +++ /dev/null @@ -1,25 +0,0 @@ -model_path: "parameters/delf_gld_20190411/model/" -image_scales: .25 -image_scales: .3536 -image_scales: .5 -image_scales: .7071 -image_scales: 1.0 -image_scales: 1.4142 -image_scales: 2.0 -delf_local_config { - use_pca: true - # Note that for the exported model provided as an example, layer_name and - # iou_threshold are hard-coded in the checkpoint. So, the layer_name and - # iou_threshold variables here have no effect on the provided - # extract_features.py script. - layer_name: "resnet_v1_50/block3" - iou_threshold: 1.0 - max_feature_num: 1000 - score_threshold: 100.0 - pca_parameters { - mean_path: "parameters/delf_gld_20190411/pca/mean.datum" - projection_matrix_path: "parameters/delf_gld_20190411/pca/pca_proj_mat.datum" - pca_dim: 40 - use_whitening: false - } -} diff --git a/research/delf/delf/python/examples/detection_example_1.jpg b/research/delf/delf/python/examples/detection_example_1.jpg deleted file mode 100644 index afdb388f0de..00000000000 Binary files a/research/delf/delf/python/examples/detection_example_1.jpg and /dev/null differ diff --git a/research/delf/delf/python/examples/detection_example_2.jpg b/research/delf/delf/python/examples/detection_example_2.jpg deleted file mode 100644 index 5baf54a8088..00000000000 Binary files a/research/delf/delf/python/examples/detection_example_2.jpg and /dev/null differ diff --git a/research/delf/delf/python/examples/detector.py b/research/delf/delf/python/examples/detector.py deleted file mode 100644 index fd8aef1cf7f..00000000000 --- a/research/delf/delf/python/examples/detector.py +++ /dev/null @@ -1,55 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module to construct object detector function.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - - -def MakeDetector(model_dir): - """Creates a function to detect objects in an image. - - Args: - model_dir: Directory where SavedModel is located. - - Returns: - Function that receives an image and returns detection results. - """ - model = tf.saved_model.load(model_dir) - - # Input and output tensors. - feeds = ['input_images:0'] - fetches = ['detection_boxes:0', 'detection_scores:0', 'detection_classes:0'] - - model = model.prune(feeds=feeds, fetches=fetches) - - def DetectorFn(images): - """Receives an image and returns detected boxes. - - Args: - images: Uint8 array with shape (batch, height, width 3) containing a batch - of RGB images. - - Returns: - Tuple (boxes, scores, class_indices). - """ - boxes, scores, class_indices = model(tf.convert_to_tensor(images)) - - return boxes.numpy(), scores.numpy(), class_indices.numpy() - - return DetectorFn diff --git a/research/delf/delf/python/examples/extract_boxes.py b/research/delf/delf/python/examples/extract_boxes.py deleted file mode 100644 index 1a3b4886a39..00000000000 --- a/research/delf/delf/python/examples/extract_boxes.py +++ /dev/null @@ -1,229 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Extracts bounding boxes from a list of images, saving them to files. - -The images must be in JPG format. The program checks if boxes already -exist, and skips computation for those. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import os -import sys -import time - -from absl import app -import matplotlib.patches as patches -import matplotlib.pyplot as plt -import numpy as np -import tensorflow as tf - -from delf import box_io -from delf import utils -from delf import detector - -cmd_args = None - -# Extension/suffix of produced files. -_BOX_EXT = '.boxes' -_VIZ_SUFFIX = '_viz.jpg' - -# Used for plotting boxes. -_BOX_EDGE_COLORS = ['r', 'y', 'b', 'm', 'k', 'g', 'c', 'w'] - -# Pace to report extraction log. -_STATUS_CHECK_ITERATIONS = 100 - - -def _ReadImageList(list_path): - """Helper function to read image paths. - - Args: - list_path: Path to list of images, one image path per line. - - Returns: - image_paths: List of image paths. - """ - with tf.io.gfile.GFile(list_path, 'r') as f: - image_paths = f.readlines() - image_paths = [entry.rstrip() for entry in image_paths] - return image_paths - - -def _FilterBoxesByScore(boxes, scores, class_indices, score_threshold): - """Filter boxes based on detection scores. - - Boxes with detection score >= score_threshold are returned. - - Args: - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - scores: [N] float array with detection scores. - class_indices: [N] int array with class indices. - score_threshold: Float detection score threshold to use. - - Returns: - selected_boxes: selected `boxes`. - selected_scores: selected `scores`. - selected_class_indices: selected `class_indices`. - """ - selected_boxes = [] - selected_scores = [] - selected_class_indices = [] - for i, box in enumerate(boxes): - if scores[i] >= score_threshold: - selected_boxes.append(box) - selected_scores.append(scores[i]) - selected_class_indices.append(class_indices[i]) - - return np.array(selected_boxes), np.array(selected_scores), np.array( - selected_class_indices) - - -def _PlotBoxesAndSaveImage(image, boxes, output_path): - """Plot boxes on image and save to output path. - - Args: - image: Numpy array containing image. - boxes: [N, 4] float array denoting bounding box coordinates, in format [top, - left, bottom, right]. - output_path: String containing output path. - """ - height = image.shape[0] - width = image.shape[1] - - fig, ax = plt.subplots(1) - ax.imshow(image) - for i, box in enumerate(boxes): - scaled_box = [ - box[0] * height, box[1] * width, box[2] * height, box[3] * width - ] - rect = patches.Rectangle([scaled_box[1], scaled_box[0]], - scaled_box[3] - scaled_box[1], - scaled_box[2] - scaled_box[0], - linewidth=3, - edgecolor=_BOX_EDGE_COLORS[i % - len(_BOX_EDGE_COLORS)], - facecolor='none') - ax.add_patch(rect) - - ax.axis('off') - plt.savefig(output_path, bbox_inches='tight') - plt.close(fig) - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Read list of images. - print('Reading list of images...') - image_paths = _ReadImageList(cmd_args.list_images_path) - num_images = len(image_paths) - print(f'done! Found {num_images} images') - - # Create output directories if necessary. - if not tf.io.gfile.exists(cmd_args.output_dir): - tf.io.gfile.makedirs(cmd_args.output_dir) - if cmd_args.output_viz_dir and not tf.io.gfile.exists( - cmd_args.output_viz_dir): - tf.io.gfile.makedirs(cmd_args.output_viz_dir) - - detector_fn = detector.MakeDetector(cmd_args.detector_path) - - start = time.time() - for i, image_path in enumerate(image_paths): - # Report progress once in a while. - if i == 0: - print('Starting to detect objects in images...') - elif i % _STATUS_CHECK_ITERATIONS == 0: - elapsed = (time.time() - start) - print(f'Processing image {i} out of {num_images}, last ' - f'{_STATUS_CHECK_ITERATIONS} images took {elapsed} seconds') - start = time.time() - - # If descriptor already exists, skip its computation. - base_boxes_filename, _ = os.path.splitext(os.path.basename(image_path)) - out_boxes_filename = base_boxes_filename + _BOX_EXT - out_boxes_fullpath = os.path.join(cmd_args.output_dir, out_boxes_filename) - if tf.io.gfile.exists(out_boxes_fullpath): - print(f'Skipping {image_path}') - continue - - im = np.expand_dims(np.array(utils.RgbLoader(image_paths[i])), 0) - - # Extract and save boxes. - (boxes_out, scores_out, class_indices_out) = detector_fn(im) - (selected_boxes, selected_scores, - selected_class_indices) = _FilterBoxesByScore(boxes_out[0], scores_out[0], - class_indices_out[0], - cmd_args.detector_thresh) - - box_io.WriteToFile(out_boxes_fullpath, selected_boxes, selected_scores, - selected_class_indices) - if cmd_args.output_viz_dir: - out_viz_filename = base_boxes_filename + _VIZ_SUFFIX - out_viz_fullpath = os.path.join(cmd_args.output_viz_dir, out_viz_filename) - _PlotBoxesAndSaveImage(im[0], selected_boxes, out_viz_fullpath) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--detector_path', - type=str, - default='/tmp/d2r_frcnn_20190411/', - help=""" - Path to exported detector model. - """) - parser.add_argument( - '--detector_thresh', - type=float, - default=.0, - help=""" - Detector threshold. Any box with confidence score lower than this is not - returned. - """) - parser.add_argument( - '--list_images_path', - type=str, - default='list_images.txt', - help=""" - Path to list of images to undergo object detection. - """) - parser.add_argument( - '--output_dir', - type=str, - default='test_boxes', - help=""" - Directory where bounding boxes will be written to. Each image's boxes - will be written to a file with same name, and extension replaced by - .boxes. - """) - parser.add_argument( - '--output_viz_dir', - type=str, - default='', - help=""" - Optional. If set, a visualization of the detected boxes overlaid on the - image is produced, and saved to this directory. Each image is saved with - _viz.jpg suffix. - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/examples/extract_features.py b/research/delf/delf/python/examples/extract_features.py deleted file mode 100644 index 1b55cba9fb6..00000000000 --- a/research/delf/delf/python/examples/extract_features.py +++ /dev/null @@ -1,142 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Extracts DELF features from a list of images, saving them to file. - -The images must be in JPG format. The program checks if descriptors already -exist, and skips computation for those. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import os -import sys -import time - -from absl import app -import numpy as np -from six.moves import range -import tensorflow as tf - -from google.protobuf import text_format -from delf import delf_config_pb2 -from delf import feature_io -from delf import utils -from delf import extractor - -cmd_args = None - -# Extension of feature files. -_DELF_EXT = '.delf' - -# Pace to report extraction log. -_STATUS_CHECK_ITERATIONS = 100 - - -def _ReadImageList(list_path): - """Helper function to read image paths. - - Args: - list_path: Path to list of images, one image path per line. - - Returns: - image_paths: List of image paths. - """ - with tf.io.gfile.GFile(list_path, 'r') as f: - image_paths = f.readlines() - image_paths = [entry.rstrip() for entry in image_paths] - return image_paths - - -def main(unused_argv): - # Read list of images. - print('Reading list of images...') - image_paths = _ReadImageList(cmd_args.list_images_path) - num_images = len(image_paths) - print(f'done! Found {num_images} images') - - # Parse DelfConfig proto. - config = delf_config_pb2.DelfConfig() - with tf.io.gfile.GFile(cmd_args.config_path, 'r') as f: - text_format.Merge(f.read(), config) - - # Create output directory if necessary. - if not tf.io.gfile.exists(cmd_args.output_dir): - tf.io.gfile.makedirs(cmd_args.output_dir) - - extractor_fn = extractor.MakeExtractor(config) - - start = time.time() - for i in range(num_images): - # Report progress once in a while. - if i == 0: - print('Starting to extract DELF features from images...') - elif i % _STATUS_CHECK_ITERATIONS == 0: - elapsed = (time.time() - start) - print(f'Processing image {i} out of {num_images}, last ' - f'{_STATUS_CHECK_ITERATIONS} images took {elapsed} seconds') - start = time.time() - - # If descriptor already exists, skip its computation. - out_desc_filename = os.path.splitext(os.path.basename( - image_paths[i]))[0] + _DELF_EXT - out_desc_fullpath = os.path.join(cmd_args.output_dir, out_desc_filename) - if tf.io.gfile.exists(out_desc_fullpath): - print(f'Skipping {image_paths[i]}') - continue - - im = np.array(utils.RgbLoader(image_paths[i])) - - # Extract and save features. - extracted_features = extractor_fn(im) - locations_out = extracted_features['local_features']['locations'] - descriptors_out = extracted_features['local_features']['descriptors'] - feature_scales_out = extracted_features['local_features']['scales'] - attention_out = extracted_features['local_features']['attention'] - - feature_io.WriteToFile(out_desc_fullpath, locations_out, feature_scales_out, - descriptors_out, attention_out) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--config_path', - type=str, - default='delf_config_example.pbtxt', - help=""" - Path to DelfConfig proto text file with configuration to be used for DELF - extraction. - """) - parser.add_argument( - '--list_images_path', - type=str, - default='list_images.txt', - help=""" - Path to list of images whose DELF features will be extracted. - """) - parser.add_argument( - '--output_dir', - type=str, - default='test_features', - help=""" - Directory where DELF features will be written to. Each image's features - will be written to a file with same name, and extension replaced by .delf. - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/examples/extractor.py b/research/delf/delf/python/examples/extractor.py deleted file mode 100644 index a6932b1de58..00000000000 --- a/research/delf/delf/python/examples/extractor.py +++ /dev/null @@ -1,262 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module to construct DELF feature extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from delf import datum_io -from delf import feature_extractor -from delf import utils - -# Minimum dimensions below which features are not extracted (empty -# features are returned). This applies after any resizing is performed. -_MIN_HEIGHT = 10 -_MIN_WIDTH = 10 - - -def MakeExtractor(config): - """Creates a function to extract global and/or local features from an image. - - Args: - config: DelfConfig proto containing the model configuration. - - Returns: - Function that receives an image and returns features. - - Raises: - ValueError: if config is invalid. - """ - # Assert the configuration. - if not config.use_local_features and not config.use_global_features: - raise ValueError('Invalid config: at least one of ' - '{use_local_features, use_global_features} must be True') - - # Load model. - model = tf.saved_model.load(config.model_path) - - # Input image scales to use for extraction. - image_scales_tensor = tf.convert_to_tensor(list(config.image_scales)) - - # Input (feeds) and output (fetches) end-points. These are only needed when - # using a model that was exported using TF1. - feeds = ['input_image:0', 'input_scales:0'] - fetches = [] - - # Custom configuration needed when local features are used. - if config.use_local_features: - # Extra input/output end-points/tensors. - feeds.append('input_abs_thres:0') - feeds.append('input_max_feature_num:0') - fetches.append('boxes:0') - fetches.append('features:0') - fetches.append('scales:0') - fetches.append('scores:0') - score_threshold_tensor = tf.constant( - config.delf_local_config.score_threshold) - max_feature_num_tensor = tf.constant( - config.delf_local_config.max_feature_num) - - # If using PCA, pre-load required parameters. - local_pca_parameters = {} - if config.delf_local_config.use_pca: - local_pca_parameters['mean'] = tf.constant( - datum_io.ReadFromFile( - config.delf_local_config.pca_parameters.mean_path), - dtype=tf.float32) - local_pca_parameters['matrix'] = tf.constant( - datum_io.ReadFromFile( - config.delf_local_config.pca_parameters.projection_matrix_path), - dtype=tf.float32) - local_pca_parameters[ - 'dim'] = config.delf_local_config.pca_parameters.pca_dim - local_pca_parameters['use_whitening'] = ( - config.delf_local_config.pca_parameters.use_whitening) - if config.delf_local_config.pca_parameters.use_whitening: - local_pca_parameters['variances'] = tf.squeeze( - tf.constant( - datum_io.ReadFromFile( - config.delf_local_config.pca_parameters.pca_variances_path), - dtype=tf.float32)) - else: - local_pca_parameters['variances'] = None - - # Custom configuration needed when global features are used. - if config.use_global_features: - # Extra input/output end-points/tensors. - feeds.append('input_global_scales_ind:0') - fetches.append('global_descriptors:0') - if config.delf_global_config.image_scales_ind: - global_scales_ind_tensor = tf.constant( - list(config.delf_global_config.image_scales_ind)) - else: - global_scales_ind_tensor = tf.range(len(config.image_scales)) - - # If using PCA, pre-load required parameters. - global_pca_parameters = {} - if config.delf_global_config.use_pca: - global_pca_parameters['mean'] = tf.constant( - datum_io.ReadFromFile( - config.delf_global_config.pca_parameters.mean_path), - dtype=tf.float32) - global_pca_parameters['matrix'] = tf.constant( - datum_io.ReadFromFile( - config.delf_global_config.pca_parameters.projection_matrix_path), - dtype=tf.float32) - global_pca_parameters[ - 'dim'] = config.delf_global_config.pca_parameters.pca_dim - global_pca_parameters['use_whitening'] = ( - config.delf_global_config.pca_parameters.use_whitening) - if config.delf_global_config.pca_parameters.use_whitening: - global_pca_parameters['variances'] = tf.squeeze( - tf.constant( - datum_io.ReadFromFile(config.delf_global_config.pca_parameters - .pca_variances_path), - dtype=tf.float32)) - else: - global_pca_parameters['variances'] = None - - if not hasattr(config, 'is_tf2_exported') or not config.is_tf2_exported: - model = model.prune(feeds=feeds, fetches=fetches) - - def ExtractorFn(image, resize_factor=1.0): - """Receives an image and returns DELF global and/or local features. - - If image is too small, returns empty features. - - Args: - image: Uint8 array with shape (height, width, 3) containing the RGB image. - resize_factor: Optional float resize factor for the input image. If given, - the maximum and minimum allowed image sizes in the config are scaled by - this factor. - - Returns: - extracted_features: A dict containing the extracted global descriptors - (key 'global_descriptor' mapping to a [D] float array), and/or local - features (key 'local_features' mapping to a dict with keys 'locations', - 'descriptors', 'scales', 'attention'). - """ - resized_image, scale_factors = utils.ResizeImage( - image, config, resize_factor=resize_factor) - - # If the image is too small, returns empty features. - if resized_image.shape[0] < _MIN_HEIGHT or resized_image.shape[ - 1] < _MIN_WIDTH: - extracted_features = {'global_descriptor': np.array([])} - if config.use_local_features: - extracted_features.update({ - 'local_features': { - 'locations': np.array([]), - 'descriptors': np.array([]), - 'scales': np.array([]), - 'attention': np.array([]), - } - }) - return extracted_features - - # Input tensors. - image_tensor = tf.convert_to_tensor(resized_image) - - # Extracted features. - extracted_features = {} - output = None - - if hasattr(config, 'is_tf2_exported') and config.is_tf2_exported: - predict = model.signatures['serving_default'] - if config.use_local_features and config.use_global_features: - output_dict = predict( - input_image=image_tensor, - input_scales=image_scales_tensor, - input_max_feature_num=max_feature_num_tensor, - input_abs_thres=score_threshold_tensor, - input_global_scales_ind=global_scales_ind_tensor) - output = [ - output_dict['boxes'], output_dict['features'], - output_dict['scales'], output_dict['scores'], - output_dict['global_descriptors'] - ] - elif config.use_local_features: - output_dict = predict( - input_image=image_tensor, - input_scales=image_scales_tensor, - input_max_feature_num=max_feature_num_tensor, - input_abs_thres=score_threshold_tensor) - output = [ - output_dict['boxes'], output_dict['features'], - output_dict['scales'], output_dict['scores'] - ] - else: - output_dict = predict( - input_image=image_tensor, - input_scales=image_scales_tensor, - input_global_scales_ind=global_scales_ind_tensor) - output = [output_dict['global_descriptors']] - else: - if config.use_local_features and config.use_global_features: - output = model(image_tensor, image_scales_tensor, - score_threshold_tensor, max_feature_num_tensor, - global_scales_ind_tensor) - elif config.use_local_features: - output = model(image_tensor, image_scales_tensor, - score_threshold_tensor, max_feature_num_tensor) - else: - output = model(image_tensor, image_scales_tensor, - global_scales_ind_tensor) - - # Post-process extracted features: normalize, PCA (optional), pooling. - if config.use_global_features: - raw_global_descriptors = output[-1] - global_descriptors_per_scale = feature_extractor.PostProcessDescriptors( - raw_global_descriptors, config.delf_global_config.use_pca, - global_pca_parameters) - unnormalized_global_descriptor = tf.reduce_sum( - global_descriptors_per_scale, axis=0, name='sum_pooling') - global_descriptor = tf.nn.l2_normalize( - unnormalized_global_descriptor, axis=0, name='final_l2_normalization') - extracted_features.update({ - 'global_descriptor': global_descriptor.numpy(), - }) - - if config.use_local_features: - boxes = output[0] - raw_local_descriptors = output[1] - feature_scales = output[2] - attention_with_extra_dim = output[3] - - attention = tf.reshape(attention_with_extra_dim, - [tf.shape(attention_with_extra_dim)[0]]) - locations, local_descriptors = ( - feature_extractor.DelfFeaturePostProcessing( - boxes, raw_local_descriptors, config.delf_local_config.use_pca, - local_pca_parameters)) - if not config.delf_local_config.use_resized_coordinates: - locations /= scale_factors - - extracted_features.update({ - 'local_features': { - 'locations': locations.numpy(), - 'descriptors': local_descriptors.numpy(), - 'scales': feature_scales.numpy(), - 'attention': attention.numpy(), - } - }) - - return extracted_features - - return ExtractorFn diff --git a/research/delf/delf/python/examples/match_images.py b/research/delf/delf/python/examples/match_images.py deleted file mode 100644 index f14f93f9eb5..00000000000 --- a/research/delf/delf/python/examples/match_images.py +++ /dev/null @@ -1,144 +0,0 @@ -# Lint as: python3 -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Matches two images using their DELF features. - -The matching is done using feature-based nearest-neighbor search, followed by -geometric verification using RANSAC. - -The DELF features can be extracted using the extract_features.py script. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import sys - -from absl import app -import matplotlib -# Needed before pyplot import for matplotlib to work properly. -matplotlib.use('Agg') -import matplotlib.image as mpimg # pylint: disable=g-import-not-at-top -import matplotlib.pyplot as plt -import numpy as np -from scipy import spatial -from skimage import feature -from skimage import measure -from skimage import transform - -from delf import feature_io - -cmd_args = None - -_DISTANCE_THRESHOLD = 0.8 - - -def main(unused_argv): - # Read features. - locations_1, _, descriptors_1, _, _ = feature_io.ReadFromFile( - cmd_args.features_1_path) - num_features_1 = locations_1.shape[0] - print(f"Loaded image 1's {num_features_1} features") - locations_2, _, descriptors_2, _, _ = feature_io.ReadFromFile( - cmd_args.features_2_path) - num_features_2 = locations_2.shape[0] - print(f"Loaded image 2's {num_features_2} features") - - # Find nearest-neighbor matches using a KD tree. - d1_tree = spatial.cKDTree(descriptors_1) - _, indices = d1_tree.query( - descriptors_2, distance_upper_bound=_DISTANCE_THRESHOLD) - - # Select feature locations for putative matches. - locations_2_to_use = np.array([ - locations_2[i,] - for i in range(num_features_2) - if indices[i] != num_features_1 - ]) - locations_1_to_use = np.array([ - locations_1[indices[i],] - for i in range(num_features_2) - if indices[i] != num_features_1 - ]) - - # Perform geometric verification using RANSAC. - _, inliers = measure.ransac((locations_1_to_use, locations_2_to_use), - transform.AffineTransform, - min_samples=3, - residual_threshold=20, - max_trials=1000) - - print(f'Found {sum(inliers)} inliers') - - # Visualize correspondences, and save to file. - _, ax = plt.subplots() - img_1 = mpimg.imread(cmd_args.image_1_path) - img_2 = mpimg.imread(cmd_args.image_2_path) - inlier_idxs = np.nonzero(inliers)[0] - feature.plot_matches( - ax, - img_1, - img_2, - locations_1_to_use, - locations_2_to_use, - np.column_stack((inlier_idxs, inlier_idxs)), - matches_color='b') - ax.axis('off') - ax.set_title('DELF correspondences') - plt.savefig(cmd_args.output_image) - - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.register('type', 'bool', lambda v: v.lower() == 'true') - parser.add_argument( - '--image_1_path', - type=str, - default='test_images/image_1.jpg', - help=""" - Path to test image 1. - """) - parser.add_argument( - '--image_2_path', - type=str, - default='test_images/image_2.jpg', - help=""" - Path to test image 2. - """) - parser.add_argument( - '--features_1_path', - type=str, - default='test_features/image_1.delf', - help=""" - Path to DELF features from image 1. - """) - parser.add_argument( - '--features_2_path', - type=str, - default='test_features/image_2.delf', - help=""" - Path to DELF features from image 2. - """) - parser.add_argument( - '--output_image', - type=str, - default='test_match.png', - help=""" - Path where an image showing the matches will be saved. - """) - cmd_args, unparsed = parser.parse_known_args() - app.run(main=main, argv=[sys.argv[0]] + unparsed) diff --git a/research/delf/delf/python/examples/matched_images_example.jpg b/research/delf/delf/python/examples/matched_images_example.jpg deleted file mode 100644 index bbd0061ac02..00000000000 Binary files a/research/delf/delf/python/examples/matched_images_example.jpg and /dev/null differ diff --git a/research/delf/delf/python/feature_aggregation_extractor.py b/research/delf/delf/python/feature_aggregation_extractor.py deleted file mode 100644 index 29496a0c20c..00000000000 --- a/research/delf/delf/python/feature_aggregation_extractor.py +++ /dev/null @@ -1,472 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Local feature aggregation extraction. - -For more details, please refer to the paper: -"Detect-to-Retrieve: Efficient Regional Aggregation for Image Search", -Proc. CVPR'19 (https://arxiv.org/abs/1812.01584). -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from delf import aggregation_config_pb2 - -_CLUSTER_CENTERS_VAR_NAME = "clusters" -_NORM_SQUARED_TOLERANCE = 1e-12 - -# Aliases for aggregation types. -_VLAD = aggregation_config_pb2.AggregationConfig.VLAD -_ASMK = aggregation_config_pb2.AggregationConfig.ASMK -_ASMK_STAR = aggregation_config_pb2.AggregationConfig.ASMK_STAR - - -class ExtractAggregatedRepresentation(object): - """Class for extraction of aggregated local feature representation. - - Args: - aggregation_config: AggregationConfig object defining type of aggregation to - use. - - Raises: - ValueError: If aggregation type is invalid. - """ - - def __init__(self, aggregation_config): - self._codebook_size = aggregation_config.codebook_size - self._feature_dimensionality = aggregation_config.feature_dimensionality - self._aggregation_type = aggregation_config.aggregation_type - self._feature_batch_size = aggregation_config.feature_batch_size - self._codebook_path = aggregation_config.codebook_path - self._use_regional_aggregation = aggregation_config.use_regional_aggregation - self._use_l2_normalization = aggregation_config.use_l2_normalization - self._num_assignments = aggregation_config.num_assignments - - if self._aggregation_type not in [_VLAD, _ASMK, _ASMK_STAR]: - raise ValueError("Invalid aggregation type: %d" % self._aggregation_type) - - # Load codebook - codebook = tf.Variable( - tf.zeros([self._codebook_size, self._feature_dimensionality], - dtype=tf.float32), - name=_CLUSTER_CENTERS_VAR_NAME) - ckpt = tf.train.Checkpoint(codebook=codebook) - ckpt.restore(self._codebook_path) - - self._codebook = codebook - - def Extract(self, features, num_features_per_region=None): - """Extracts aggregated representation. - - Args: - features: [N, D] float numpy array with N local feature descriptors. - num_features_per_region: Required only if computing regional aggregated - representations, otherwise optional. List of number of features per - region, such that sum(num_features_per_region) = N. It indicates which - features correspond to each region. - - Returns: - aggregated_descriptors: 1-D numpy array. - feature_visual_words: Used only for ASMK/ASMK* aggregation type. 1-D - numpy array denoting visual words corresponding to the - `aggregated_descriptors`. - - Raises: - ValueError: If inputs are misconfigured. - """ - features = tf.cast(features, dtype=tf.float32) - - if num_features_per_region is None: - # Use dummy value since it is unused. - num_features_per_region = [] - else: - num_features_per_region = tf.cast(num_features_per_region, dtype=tf.int32) - if len(num_features_per_region - ) and sum(num_features_per_region) != features.shape[0]: - raise ValueError( - "Incorrect arguments: sum(num_features_per_region) and " - "features.shape[0] are different: %d vs %d" % - (sum(num_features_per_region), features.shape[0])) - - # Extract features based on desired options. - if self._aggregation_type == _VLAD: - # Feature visual words are unused in the case of VLAD, so just return - # dummy constant. - feature_visual_words = tf.constant(-1, dtype=tf.int32) - if self._use_regional_aggregation: - aggregated_descriptors = self._ComputeRvlad( - features, - num_features_per_region, - self._codebook, - use_l2_normalization=self._use_l2_normalization, - num_assignments=self._num_assignments) - else: - aggregated_descriptors = self._ComputeVlad( - features, - self._codebook, - use_l2_normalization=self._use_l2_normalization, - num_assignments=self._num_assignments) - elif (self._aggregation_type == _ASMK or - self._aggregation_type == _ASMK_STAR): - if self._use_regional_aggregation: - (aggregated_descriptors, - feature_visual_words) = self._ComputeRasmk( - features, - num_features_per_region, - self._codebook, - num_assignments=self._num_assignments) - else: - (aggregated_descriptors, - feature_visual_words) = self._ComputeAsmk( - features, - self._codebook, - num_assignments=self._num_assignments) - - feature_visual_words_output = feature_visual_words.numpy() - - # If using ASMK*/RASMK*, binarize the aggregated descriptors. - if self._aggregation_type == _ASMK_STAR: - reshaped_aggregated_descriptors = np.reshape( - aggregated_descriptors, [-1, self._feature_dimensionality]) - packed_descriptors = np.packbits( - reshaped_aggregated_descriptors > 0, axis=1) - aggregated_descriptors_output = np.reshape(packed_descriptors, [-1]) - else: - aggregated_descriptors_output = aggregated_descriptors.numpy() - - return aggregated_descriptors_output, feature_visual_words_output - - def _ComputeVlad(self, - features, - codebook, - use_l2_normalization=True, - num_assignments=1): - """Compute VLAD representation. - - Args: - features: [N, D] float tensor. - codebook: [K, D] float tensor. - use_l2_normalization: If False, does not L2-normalize after aggregation. - num_assignments: Number of visual words to assign a feature to. - - Returns: - vlad: [K*D] float tensor. - """ - - def _ComputeVladEmptyFeatures(): - """Computes VLAD if `features` is empty. - - Returns: - [K*D] all-zeros tensor. - """ - return tf.zeros([self._codebook_size * self._feature_dimensionality], - dtype=tf.float32) - - def _ComputeVladNonEmptyFeatures(): - """Computes VLAD if `features` is not empty. - - Returns: - [K*D] tensor with VLAD descriptor. - """ - num_features = tf.shape(features)[0] - - # Find nearest visual words for each feature. Possibly batch the local - # features to avoid OOM. - if self._feature_batch_size <= 0: - actual_batch_size = num_features - else: - actual_batch_size = self._feature_batch_size - - def _BatchNearestVisualWords(ind, selected_visual_words): - """Compute nearest neighbor visual words for a batch of features. - - Args: - ind: Integer index denoting feature. - selected_visual_words: Partial set of visual words. - - Returns: - output_ind: Next index. - output_selected_visual_words: Updated set of visual words, including - the visual words for the new batch. - """ - # Handle case of last batch, where there may be fewer than - # `actual_batch_size` features. - batch_size_to_use = tf.cond( - tf.greater(ind + actual_batch_size, num_features), - true_fn=lambda: num_features - ind, - false_fn=lambda: actual_batch_size) - - # Denote B = batch_size_to_use. - # K*B x D. - tiled_features = tf.reshape( - tf.tile( - tf.slice(features, [ind, 0], - [batch_size_to_use, self._feature_dimensionality]), - [1, self._codebook_size]), [-1, self._feature_dimensionality]) - # K*B x D. - tiled_codebook = tf.reshape( - tf.tile(tf.reshape(codebook, [1, -1]), [batch_size_to_use, 1]), - [-1, self._feature_dimensionality]) - # B x K. - squared_distances = tf.reshape( - tf.reduce_sum( - tf.math.squared_difference(tiled_features, tiled_codebook), - axis=1), [batch_size_to_use, self._codebook_size]) - # B x K. - nearest_visual_words = tf.argsort(squared_distances) - # B x num_assignments. - batch_selected_visual_words = tf.slice( - nearest_visual_words, [0, 0], [batch_size_to_use, num_assignments]) - selected_visual_words = tf.concat( - [selected_visual_words, batch_selected_visual_words], axis=0) - - return ind + batch_size_to_use, selected_visual_words - - ind_batch = tf.constant(0, dtype=tf.int32) - keep_going = lambda j, selected_visual_words: tf.less(j, num_features) - selected_visual_words = tf.zeros([0, num_assignments], dtype=tf.int32) - _, selected_visual_words = tf.while_loop( - cond=keep_going, - body=_BatchNearestVisualWords, - loop_vars=[ind_batch, selected_visual_words], - shape_invariants=[ - ind_batch.get_shape(), - tf.TensorShape([None, num_assignments]) - ], - parallel_iterations=1, - back_prop=False) - - # Helper function to collect residuals for relevant visual words. - def _ConstructVladFromAssignments(ind, vlad): - """Add contributions of a feature to a VLAD descriptor. - - Args: - ind: Integer index denoting feature. - vlad: Partial VLAD descriptor. - - Returns: - output_ind: Next index (ie, ind+1). - output_vlad: VLAD descriptor updated to take into account contribution - from ind-th feature. - """ - diff = tf.tile( - tf.expand_dims(features[ind], - axis=0), [num_assignments, 1]) - tf.gather( - codebook, selected_visual_words[ind]) - return ind + 1, tf.tensor_scatter_nd_add( - vlad, tf.expand_dims(selected_visual_words[ind], axis=1), diff) - - ind_vlad = tf.constant(0, dtype=tf.int32) - keep_going = lambda j, vlad: tf.less(j, num_features) - vlad = tf.zeros([self._codebook_size, self._feature_dimensionality], - dtype=tf.float32) - _, vlad = tf.while_loop( - cond=keep_going, - body=_ConstructVladFromAssignments, - loop_vars=[ind_vlad, vlad], - back_prop=False) - - vlad = tf.reshape(vlad, - [self._codebook_size * self._feature_dimensionality]) - if use_l2_normalization: - vlad = tf.math.l2_normalize(vlad, epsilon=_NORM_SQUARED_TOLERANCE) - - return vlad - - return tf.cond( - tf.greater(tf.size(features), 0), - true_fn=_ComputeVladNonEmptyFeatures, - false_fn=_ComputeVladEmptyFeatures) - - def _ComputeRvlad(self, - features, - num_features_per_region, - codebook, - use_l2_normalization=False, - num_assignments=1): - """Compute R-VLAD representation. - - Args: - features: [N, D] float tensor. - num_features_per_region: [R] int tensor. Contains number of features per - region, such that sum(num_features_per_region) = N. It indicates which - features correspond to each region. - codebook: [K, D] float tensor. - use_l2_normalization: If True, performs L2-normalization after regional - aggregation; if False (default), performs componentwise division by R - after regional aggregation. - num_assignments: Number of visual words to assign a feature to. - - Returns: - rvlad: [K*D] float tensor. - """ - - def _ComputeRvladEmptyRegions(): - """Computes R-VLAD if `num_features_per_region` is empty. - - Returns: - [K*D] all-zeros tensor. - """ - return tf.zeros([self._codebook_size * self._feature_dimensionality], - dtype=tf.float32) - - def _ComputeRvladNonEmptyRegions(): - """Computes R-VLAD if `num_features_per_region` is not empty. - - Returns: - [K*D] tensor with R-VLAD descriptor. - """ - - # Helper function to compose initial R-VLAD from image regions. - def _ConstructRvladFromVlad(ind, rvlad): - """Add contributions from different regions into R-VLAD. - - Args: - ind: Integer index denoting region. - rvlad: Partial R-VLAD descriptor. - - Returns: - output_ind: Next index (ie, ind+1). - output_rvlad: R-VLAD descriptor updated to take into account - contribution from ind-th region. - """ - return ind + 1, rvlad + self._ComputeVlad( - tf.slice( - features, [tf.reduce_sum(num_features_per_region[:ind]), 0], - [num_features_per_region[ind], self._feature_dimensionality]), - codebook, - num_assignments=num_assignments) - - i = tf.constant(0, dtype=tf.int32) - num_regions = tf.shape(num_features_per_region)[0] - keep_going = lambda j, rvlad: tf.less(j, num_regions) - rvlad = tf.zeros([self._codebook_size * self._feature_dimensionality], - dtype=tf.float32) - _, rvlad = tf.while_loop( - cond=keep_going, - body=_ConstructRvladFromVlad, - loop_vars=[i, rvlad], - back_prop=False, - parallel_iterations=1) - - if use_l2_normalization: - rvlad = tf.math.l2_normalize(rvlad, epsilon=_NORM_SQUARED_TOLERANCE) - else: - rvlad /= tf.cast(num_regions, dtype=tf.float32) - - return rvlad - - return tf.cond( - tf.greater(tf.size(num_features_per_region), 0), - true_fn=_ComputeRvladNonEmptyRegions, - false_fn=_ComputeRvladEmptyRegions) - - def _PerCentroidNormalization(self, unnormalized_vector): - """Perform per-centroid normalization. - - Args: - unnormalized_vector: [KxD] float tensor. - - Returns: - per_centroid_normalized_vector: [KxD] float tensor, with normalized - aggregated residuals. Some residuals may be all-zero. - visual_words: Int tensor containing indices of visual words which are - present for the set of features. - """ - unnormalized_vector = tf.reshape( - unnormalized_vector, - [self._codebook_size, self._feature_dimensionality]) - per_centroid_norms = tf.norm(unnormalized_vector, axis=1) - - visual_words = tf.reshape( - tf.where( - tf.greater(per_centroid_norms, tf.sqrt(_NORM_SQUARED_TOLERANCE))), - [-1]) - - per_centroid_normalized_vector = tf.math.l2_normalize( - unnormalized_vector, axis=1, epsilon=_NORM_SQUARED_TOLERANCE) - - return per_centroid_normalized_vector, visual_words - - def _ComputeAsmk(self, features, codebook, num_assignments=1): - """Compute ASMK representation. - - Args: - features: [N, D] float tensor. - codebook: [K, D] float tensor. - num_assignments: Number of visual words to assign a feature to. - - Returns: - normalized_residuals: 1-dimensional float tensor with concatenated - residuals which are non-zero. Note that the dimensionality is - input-dependent. - visual_words: 1-dimensional int tensor of sorted visual word ids. - Dimensionality is shape(normalized_residuals)[0] / D. - """ - unnormalized_vlad = self._ComputeVlad( - features, - codebook, - use_l2_normalization=False, - num_assignments=num_assignments) - - per_centroid_normalized_vlad, visual_words = self._PerCentroidNormalization( - unnormalized_vlad) - - normalized_residuals = tf.reshape( - tf.gather(per_centroid_normalized_vlad, visual_words), - [tf.shape(visual_words)[0] * self._feature_dimensionality]) - - return normalized_residuals, visual_words - - def _ComputeRasmk(self, - features, - num_features_per_region, - codebook, - num_assignments=1): - """Compute R-ASMK representation. - - Args: - features: [N, D] float tensor. - num_features_per_region: [R] int tensor. Contains number of features per - region, such that sum(num_features_per_region) = N. It indicates which - features correspond to each region. - codebook: [K, D] float tensor. - num_assignments: Number of visual words to assign a feature to. - - Returns: - normalized_residuals: 1-dimensional float tensor with concatenated - residuals which are non-zero. Note that the dimensionality is - input-dependent. - visual_words: 1-dimensional int tensor of sorted visual word ids. - Dimensionality is shape(normalized_residuals)[0] / D. - """ - unnormalized_rvlad = self._ComputeRvlad( - features, - num_features_per_region, - codebook, - use_l2_normalization=False, - num_assignments=num_assignments) - - (per_centroid_normalized_rvlad, - visual_words) = self._PerCentroidNormalization(unnormalized_rvlad) - - normalized_residuals = tf.reshape( - tf.gather(per_centroid_normalized_rvlad, visual_words), - [tf.shape(visual_words)[0] * self._feature_dimensionality]) - - return normalized_residuals, visual_words diff --git a/research/delf/delf/python/feature_aggregation_extractor_test.py b/research/delf/delf/python/feature_aggregation_extractor_test.py deleted file mode 100644 index dfba92a2b1b..00000000000 --- a/research/delf/delf/python/feature_aggregation_extractor_test.py +++ /dev/null @@ -1,494 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for DELF feature aggregation.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import numpy as np -import tensorflow as tf - -from delf import aggregation_config_pb2 -from delf import feature_aggregation_extractor - -FLAGS = flags.FLAGS - - -class FeatureAggregationTest(tf.test.TestCase): - - def _CreateCodebook(self, checkpoint_path): - """Creates codebook used in tests. - - Args: - checkpoint_path: Directory where codebook is saved to. - """ - codebook = tf.Variable( - [[0.5, 0.5], [0.0, 0.0], [1.0, 0.0], [-0.5, -0.5], [0.0, 1.0]], - name='clusters', - dtype=tf.float32) - ckpt = tf.train.Checkpoint(codebook=codebook) - ckpt.write(checkpoint_path) - - def setUp(self): - self._codebook_path = os.path.join(FLAGS.test_tmpdir, 'test_codebook') - self._CreateCodebook(self._codebook_path) - - def testComputeNormalizedVladWorks(self): - # Construct inputs. - # 3 2-D features. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0]], dtype=float) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = True - config.codebook_path = self._codebook_path - config.num_assignments = 1 - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - vlad, extra_output = extractor.Extract(features) - - # Define expected results. - exp_vlad = [ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.316228, 0.316228, 0.632456, 0.632456 - ] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllClose(vlad, exp_vlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeNormalizedVladWithBatchingWorks(self): - # Construct inputs. - # 3 2-D features. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0]], dtype=float) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = True - config.codebook_path = self._codebook_path - config.num_assignments = 1 - config.feature_batch_size = 2 - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - vlad, extra_output = extractor.Extract(features) - - # Define expected results. - exp_vlad = [ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.316228, 0.316228, 0.632456, 0.632456 - ] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllClose(vlad, exp_vlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeUnnormalizedVladWorks(self): - # Construct inputs. - # 3 2-D features. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0]], dtype=float) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = False - config.codebook_path = self._codebook_path - config.num_assignments = 1 - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - vlad, extra_output = extractor.Extract(features) - - # Define expected results. - exp_vlad = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5, 0.5, 1.0, 1.0] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllEqual(vlad, exp_vlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeUnnormalizedVladMultipleAssignmentWorks(self): - # Construct inputs. - # 3 2-D features. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0]], dtype=float) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = False - config.codebook_path = self._codebook_path - config.num_assignments = 3 - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - vlad, extra_output = extractor.Extract(features) - - # Define expected results. - exp_vlad = [1.0, 1.0, 0.0, 0.0, 0.0, 2.0, -0.5, 0.5, 0.0, 0.0] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllEqual(vlad, exp_vlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeVladEmptyFeaturesWorks(self): - # Construct inputs. - # Empty feature array. - features = np.array([[]]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.codebook_path = self._codebook_path - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - vlad, extra_output = extractor.Extract(features) - - # Define expected results. - exp_vlad = np.zeros([10], dtype=float) - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllEqual(vlad, exp_vlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeUnnormalizedRvladWorks(self): - # Construct inputs. - # 4 2-D features: 3 in first region, 1 in second region. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0], [0.0, 2.0]], - dtype=float) - num_features_per_region = np.array([3, 1]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = False - config.codebook_path = self._codebook_path - config.num_assignments = 1 - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - rvlad, extra_output = extractor.Extract(features, num_features_per_region) - - # Define expected results. - exp_rvlad = [ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.158114, 0.158114, 0.316228, 0.816228 - ] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllClose(rvlad, exp_rvlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeNormalizedRvladWorks(self): - # Construct inputs. - # 4 2-D features: 3 in first region, 1 in second region. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0], [0.0, 2.0]], - dtype=float) - num_features_per_region = np.array([3, 1]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = True - config.codebook_path = self._codebook_path - config.num_assignments = 1 - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - rvlad, extra_output = extractor.Extract(features, num_features_per_region) - - # Define expected results. - exp_rvlad = [ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.175011, 0.175011, 0.350021, 0.903453 - ] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllClose(rvlad, exp_rvlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeRvladEmptyRegionsWorks(self): - # Construct inputs. - # Empty feature array. - features = np.array([[]]) - num_features_per_region = np.array([]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.codebook_path = self._codebook_path - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - rvlad, extra_output = extractor.Extract(features, num_features_per_region) - - # Define expected results. - exp_rvlad = np.zeros([10], dtype=float) - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllEqual(rvlad, exp_rvlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeUnnormalizedRvladSomeEmptyRegionsWorks(self): - # Construct inputs. - # 4 2-D features: 0 in first region, 3 in second region, 0 in third region, - # 1 in fourth region. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0], [0.0, 2.0]], - dtype=float) - num_features_per_region = np.array([0, 3, 0, 1]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = False - config.codebook_path = self._codebook_path - config.num_assignments = 1 - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - rvlad, extra_output = extractor.Extract(features, num_features_per_region) - - # Define expected results. - exp_rvlad = [ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.079057, 0.079057, 0.158114, 0.408114 - ] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllClose(rvlad, exp_rvlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeNormalizedRvladSomeEmptyRegionsWorks(self): - # Construct inputs. - # 4 2-D features: 0 in first region, 3 in second region, 0 in third region, - # 1 in fourth region. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0], [0.0, 2.0]], - dtype=float) - num_features_per_region = np.array([0, 3, 0, 1]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.use_l2_normalization = True - config.codebook_path = self._codebook_path - config.num_assignments = 1 - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - rvlad, extra_output = extractor.Extract(features, num_features_per_region) - - # Define expected results. - exp_rvlad = [ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.175011, 0.175011, 0.350021, 0.903453 - ] - exp_extra_output = -1 - - # Compare actual and expected results. - self.assertAllClose(rvlad, exp_rvlad) - self.assertAllEqual(extra_output, exp_extra_output) - - def testComputeRvladMisconfiguredFeatures(self): - # Construct inputs. - # 4 2-D features: 3 in first region, 1 in second region. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0], [0.0, 2.0]], - dtype=float) - # Misconfigured number of features; there are only 4 features, but - # sum(num_features_per_region) = 5. - num_features_per_region = np.array([3, 2]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - config.codebook_path = self._codebook_path - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - with self.assertRaisesRegex( - ValueError, - r'Incorrect arguments: sum\(num_features_per_region\) and ' - r'features.shape\[0\] are different'): - extractor.Extract(features, num_features_per_region) - - def testComputeAsmkWorks(self): - # Construct inputs. - # 3 2-D features. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0]], dtype=float) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK - config.codebook_path = self._codebook_path - config.num_assignments = 1 - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - asmk, visual_words = extractor.Extract(features) - - # Define expected results. - exp_asmk = [-0.707107, 0.707107, 0.707107, 0.707107] - exp_visual_words = [3, 4] - - # Compare actual and expected results. - self.assertAllClose(asmk, exp_asmk) - self.assertAllEqual(visual_words, exp_visual_words) - - def testComputeAsmkStarWorks(self): - # Construct inputs. - # 3 2-D features. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0]], dtype=float) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK_STAR - config.codebook_path = self._codebook_path - config.num_assignments = 1 - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - asmk_star, visual_words = extractor.Extract(features) - - # Define expected results. - exp_asmk_star = [64, 192] - exp_visual_words = [3, 4] - - # Compare actual and expected results. - self.assertAllEqual(asmk_star, exp_asmk_star) - self.assertAllEqual(visual_words, exp_visual_words) - - def testComputeAsmkMultipleAssignmentWorks(self): - # Construct inputs. - # 3 2-D features. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0]], dtype=float) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK - config.codebook_path = self._codebook_path - config.num_assignments = 3 - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - asmk, visual_words = extractor.Extract(features) - - # Define expected results. - exp_asmk = [0.707107, 0.707107, 0.0, 1.0, -0.707107, 0.707107] - exp_visual_words = [0, 2, 3] - - # Compare actual and expected results. - self.assertAllClose(asmk, exp_asmk) - self.assertAllEqual(visual_words, exp_visual_words) - - def testComputeRasmkWorks(self): - # Construct inputs. - # 4 2-D features: 3 in first region, 1 in second region. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0], [0.0, 2.0]], - dtype=float) - num_features_per_region = np.array([3, 1]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK - config.codebook_path = self._codebook_path - config.num_assignments = 1 - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - rasmk, visual_words = extractor.Extract(features, num_features_per_region) - - # Define expected results. - exp_rasmk = [-0.707107, 0.707107, 0.361261, 0.932465] - exp_visual_words = [3, 4] - - # Compare actual and expected results. - self.assertAllClose(rasmk, exp_rasmk) - self.assertAllEqual(visual_words, exp_visual_words) - - def testComputeRasmkStarWorks(self): - # Construct inputs. - # 4 2-D features: 3 in first region, 1 in second region. - features = np.array([[1.0, 0.0], [-1.0, 0.0], [1.0, 2.0], [0.0, 2.0]], - dtype=float) - num_features_per_region = np.array([3, 1]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK_STAR - config.codebook_path = self._codebook_path - config.num_assignments = 1 - config.use_regional_aggregation = True - - # Run tested function. - extractor = feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - rasmk_star, visual_words = extractor.Extract(features, - num_features_per_region) - - # Define expected results. - exp_rasmk_star = [64, 192] - exp_visual_words = [3, 4] - - # Compare actual and expected results. - self.assertAllEqual(rasmk_star, exp_rasmk_star) - self.assertAllEqual(visual_words, exp_visual_words) - - def testComputeUnknownAggregation(self): - # Construct inputs. - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = 0 - config.codebook_path = self._codebook_path - config.use_regional_aggregation = True - - # Run tested function. - with self.assertRaisesRegex(ValueError, 'Invalid aggregation type'): - feature_aggregation_extractor.ExtractAggregatedRepresentation( - config) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/feature_aggregation_similarity.py b/research/delf/delf/python/feature_aggregation_similarity.py deleted file mode 100644 index 991c95c767c..00000000000 --- a/research/delf/delf/python/feature_aggregation_similarity.py +++ /dev/null @@ -1,265 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Local feature aggregation similarity computation. - -For more details, please refer to the paper: -"Detect-to-Retrieve: Efficient Regional Aggregation for Image Search", -Proc. CVPR'19 (https://arxiv.org/abs/1812.01584). -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np - -from delf import aggregation_config_pb2 - -# Aliases for aggregation types. -_VLAD = aggregation_config_pb2.AggregationConfig.VLAD -_ASMK = aggregation_config_pb2.AggregationConfig.ASMK -_ASMK_STAR = aggregation_config_pb2.AggregationConfig.ASMK_STAR - - -class SimilarityAggregatedRepresentation(object): - """Class for computing similarity of aggregated local feature representations. - - Args: - aggregation_config: AggregationConfig object defining type of aggregation to - use. - - Raises: - ValueError: If aggregation type is invalid. - """ - - def __init__(self, aggregation_config): - self._feature_dimensionality = aggregation_config.feature_dimensionality - self._aggregation_type = aggregation_config.aggregation_type - - # Only relevant if using ASMK/ASMK*. Otherwise, ignored. - self._use_l2_normalization = aggregation_config.use_l2_normalization - self._alpha = aggregation_config.alpha - self._tau = aggregation_config.tau - - # Only relevant if using ASMK*. Otherwise, ignored. - self._number_bits = np.array([bin(n).count('1') for n in range(256)]) - - def ComputeSimilarity(self, - aggregated_descriptors_1, - aggregated_descriptors_2, - feature_visual_words_1=None, - feature_visual_words_2=None): - """Computes similarity between aggregated descriptors. - - Args: - aggregated_descriptors_1: 1-D NumPy array. - aggregated_descriptors_2: 1-D NumPy array. - feature_visual_words_1: Used only for ASMK/ASMK* aggregation type. 1-D - sorted NumPy integer array denoting visual words corresponding to - `aggregated_descriptors_1`. - feature_visual_words_2: Used only for ASMK/ASMK* aggregation type. 1-D - sorted NumPy integer array denoting visual words corresponding to - `aggregated_descriptors_2`. - - Returns: - similarity: Float. The larger, the more similar. - - Raises: - ValueError: If aggregation type is invalid. - """ - if self._aggregation_type == _VLAD: - similarity = np.dot(aggregated_descriptors_1, aggregated_descriptors_2) - elif self._aggregation_type == _ASMK: - similarity = self._AsmkSimilarity( - aggregated_descriptors_1, - aggregated_descriptors_2, - feature_visual_words_1, - feature_visual_words_2, - binarized=False) - elif self._aggregation_type == _ASMK_STAR: - similarity = self._AsmkSimilarity( - aggregated_descriptors_1, - aggregated_descriptors_2, - feature_visual_words_1, - feature_visual_words_2, - binarized=True) - else: - raise ValueError('Invalid aggregation type: %d' % self._aggregation_type) - - return similarity - - def _CheckAsmkDimensionality(self, aggregated_descriptors, num_visual_words, - descriptor_name): - """Checks that ASMK dimensionality is as expected. - - Args: - aggregated_descriptors: 1-D NumPy array. - num_visual_words: Integer. - descriptor_name: String. - - Raises: - ValueError: If descriptor dimensionality is incorrect. - """ - if len(aggregated_descriptors - ) / num_visual_words != self._feature_dimensionality: - raise ValueError( - 'Feature dimensionality for aggregated descriptor %s is invalid: %d;' - ' expected %d.' % (descriptor_name, len(aggregated_descriptors) / - num_visual_words, self._feature_dimensionality)) - - def _SigmaFn(self, x): - """Selectivity ASMK/ASMK* similarity function. - - Args: - x: Scalar or 1-D NumPy array. - - Returns: - result: Same type as input, with output of selectivity function. - """ - if np.isscalar(x): - if x > self._tau: - result = np.sign(x) * np.power(np.absolute(x), self._alpha) - else: - result = 0.0 - else: - result = np.zeros_like(x) - above_tau = np.nonzero(x > self._tau) - result[above_tau] = np.sign(x[above_tau]) * np.power( - np.absolute(x[above_tau]), self._alpha) - - return result - - def _BinaryNormalizedInnerProduct(self, descriptors_1, descriptors_2): - """Computes normalized binary inner product. - - Args: - descriptors_1: 1-D NumPy integer array. - descriptors_2: 1-D NumPy integer array. - - Returns: - inner_product: Float. - - Raises: - ValueError: If the dimensionality of descriptors is different. - """ - num_descriptors = len(descriptors_1) - if num_descriptors != len(descriptors_2): - raise ValueError( - 'Descriptors have incompatible dimensionality: %d vs %d' % - (len(descriptors_1), len(descriptors_2))) - - h = 0 - for i in range(num_descriptors): - h += self._number_bits[np.bitwise_xor(descriptors_1[i], descriptors_2[i])] - - # If local feature dimensionality is lower than 8, then use that to compute - # proper binarized inner product. - bits_per_descriptor = min(self._feature_dimensionality, 8) - - total_num_bits = bits_per_descriptor * num_descriptors - - return 1.0 - 2.0 * h / total_num_bits - - def _AsmkSimilarity(self, - aggregated_descriptors_1, - aggregated_descriptors_2, - visual_words_1, - visual_words_2, - binarized=False): - """Compute ASMK-based similarity. - - If `aggregated_descriptors_1` or `aggregated_descriptors_2` is empty, we - return a similarity of -1.0. - - If binarized is True, `aggregated_descriptors_1` and - `aggregated_descriptors_2` must be of type uint8. - - Args: - aggregated_descriptors_1: 1-D NumPy array. - aggregated_descriptors_2: 1-D NumPy array. - visual_words_1: 1-D sorted NumPy integer array denoting visual words - corresponding to `aggregated_descriptors_1`. - visual_words_2: 1-D sorted NumPy integer array denoting visual words - corresponding to `aggregated_descriptors_2`. - binarized: If True, compute ASMK* similarity. - - Returns: - similarity: Float. The larger, the more similar. - - Raises: - ValueError: If input descriptor dimensionality is inconsistent, or if - descriptor type is unsupported. - """ - num_visual_words_1 = len(visual_words_1) - num_visual_words_2 = len(visual_words_2) - - if not num_visual_words_1 or not num_visual_words_2: - return -1.0 - - # Parse dimensionality used per visual word. They must be the same for both - # aggregated descriptors. If using ASMK, they also must be equal to - # self._feature_dimensionality. - if binarized: - if aggregated_descriptors_1.dtype != 'uint8': - raise ValueError('Incorrect input descriptor type: %s' % - aggregated_descriptors_1.dtype) - if aggregated_descriptors_2.dtype != 'uint8': - raise ValueError('Incorrect input descriptor type: %s' % - aggregated_descriptors_2.dtype) - - per_visual_word_dimensionality = int( - len(aggregated_descriptors_1) / num_visual_words_1) - if len(aggregated_descriptors_2 - ) / num_visual_words_2 != per_visual_word_dimensionality: - raise ValueError('ASMK* dimensionality is inconsistent.') - else: - per_visual_word_dimensionality = self._feature_dimensionality - self._CheckAsmkDimensionality(aggregated_descriptors_1, - num_visual_words_1, '1') - self._CheckAsmkDimensionality(aggregated_descriptors_2, - num_visual_words_2, '2') - - aggregated_descriptors_1_reshape = np.reshape( - aggregated_descriptors_1, - [num_visual_words_1, per_visual_word_dimensionality]) - aggregated_descriptors_2_reshape = np.reshape( - aggregated_descriptors_2, - [num_visual_words_2, per_visual_word_dimensionality]) - - # Loop over visual words, compute similarity. - unnormalized_similarity = 0.0 - ind_1 = 0 - ind_2 = 0 - while ind_1 < num_visual_words_1 and ind_2 < num_visual_words_2: - if visual_words_1[ind_1] == visual_words_2[ind_2]: - if binarized: - inner_product = self._BinaryNormalizedInnerProduct( - aggregated_descriptors_1_reshape[ind_1], - aggregated_descriptors_2_reshape[ind_2]) - else: - inner_product = np.dot(aggregated_descriptors_1_reshape[ind_1], - aggregated_descriptors_2_reshape[ind_2]) - unnormalized_similarity += self._SigmaFn(inner_product) - ind_1 += 1 - ind_2 += 1 - elif visual_words_1[ind_1] > visual_words_2[ind_2]: - ind_2 += 1 - else: - ind_1 += 1 - - final_similarity = unnormalized_similarity - if self._use_l2_normalization: - final_similarity /= np.sqrt(num_visual_words_1 * num_visual_words_2) - - return final_similarity diff --git a/research/delf/delf/python/feature_aggregation_similarity_test.py b/research/delf/delf/python/feature_aggregation_similarity_test.py deleted file mode 100644 index e2f01b1d2a7..00000000000 --- a/research/delf/delf/python/feature_aggregation_similarity_test.py +++ /dev/null @@ -1,137 +0,0 @@ -# Copyright 2019 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for DELF feature aggregation similarity.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from delf import aggregation_config_pb2 -from delf import feature_aggregation_similarity - - -class FeatureAggregationSimilarityTest(tf.test.TestCase): - - def testComputeVladSimilarityWorks(self): - # Construct inputs. - vlad_1 = np.array([0, 1, 2, 3, 4]) - vlad_2 = np.array([5, 6, 7, 8, 9]) - config = aggregation_config_pb2.AggregationConfig() - config.aggregation_type = aggregation_config_pb2.AggregationConfig.VLAD - - # Run tested function. - similarity_computer = ( - feature_aggregation_similarity.SimilarityAggregatedRepresentation( - config)) - similarity = similarity_computer.ComputeSimilarity(vlad_1, vlad_2) - - # Define expected results. - exp_similarity = 80 - - # Compare actual and expected results. - self.assertAllEqual(similarity, exp_similarity) - - def testComputeAsmkSimilarityWorks(self): - # Construct inputs. - aggregated_descriptors_1 = np.array([ - 0.0, 0.0, -0.707107, -0.707107, 0.5, 0.866025, 0.816497, 0.577350, 1.0, - 0.0 - ]) - visual_words_1 = np.array([0, 1, 2, 3, 4]) - aggregated_descriptors_2 = np.array( - [0.0, 1.0, 1.0, 0.0, 0.707107, 0.707107]) - visual_words_2 = np.array([1, 2, 4]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK - config.use_l2_normalization = True - - # Run tested function. - similarity_computer = ( - feature_aggregation_similarity.SimilarityAggregatedRepresentation( - config)) - similarity = similarity_computer.ComputeSimilarity( - aggregated_descriptors_1, aggregated_descriptors_2, visual_words_1, - visual_words_2) - - # Define expected results. - exp_similarity = 0.123562 - - # Compare actual and expected results. - self.assertAllClose(similarity, exp_similarity) - - def testComputeAsmkSimilarityNoNormalizationWorks(self): - # Construct inputs. - aggregated_descriptors_1 = np.array([ - 0.0, 0.0, -0.707107, -0.707107, 0.5, 0.866025, 0.816497, 0.577350, 1.0, - 0.0 - ]) - visual_words_1 = np.array([0, 1, 2, 3, 4]) - aggregated_descriptors_2 = np.array( - [0.0, 1.0, 1.0, 0.0, 0.707107, 0.707107]) - visual_words_2 = np.array([1, 2, 4]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK - config.use_l2_normalization = False - - # Run tested function. - similarity_computer = ( - feature_aggregation_similarity.SimilarityAggregatedRepresentation( - config)) - similarity = similarity_computer.ComputeSimilarity( - aggregated_descriptors_1, aggregated_descriptors_2, visual_words_1, - visual_words_2) - - # Define expected results. - exp_similarity = 0.478554 - - # Compare actual and expected results. - self.assertAllClose(similarity, exp_similarity) - - def testComputeAsmkStarSimilarityWorks(self): - # Construct inputs. - aggregated_descriptors_1 = np.array([0, 0, 3, 3, 3], dtype='uint8') - visual_words_1 = np.array([0, 1, 2, 3, 4]) - aggregated_descriptors_2 = np.array([1, 2, 3], dtype='uint8') - visual_words_2 = np.array([1, 2, 4]) - config = aggregation_config_pb2.AggregationConfig() - config.codebook_size = 5 - config.feature_dimensionality = 2 - config.aggregation_type = aggregation_config_pb2.AggregationConfig.ASMK_STAR - config.use_l2_normalization = True - - # Run tested function. - similarity_computer = ( - feature_aggregation_similarity.SimilarityAggregatedRepresentation( - config)) - similarity = similarity_computer.ComputeSimilarity( - aggregated_descriptors_1, aggregated_descriptors_2, visual_words_1, - visual_words_2) - - # Define expected results. - exp_similarity = 0.258199 - - # Compare actual and expected results. - self.assertAllClose(similarity, exp_similarity) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/feature_extractor.py b/research/delf/delf/python/feature_extractor.py deleted file mode 100644 index 9545337f187..00000000000 --- a/research/delf/delf/python/feature_extractor.py +++ /dev/null @@ -1,175 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""DELF feature extractor.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - - -def NormalizePixelValues(image, - pixel_value_offset=128.0, - pixel_value_scale=128.0): - """Normalize image pixel values. - - Args: - image: a uint8 tensor. - pixel_value_offset: a Python float, offset for normalizing pixel values. - pixel_value_scale: a Python float, scale for normalizing pixel values. - - Returns: - image: a float32 tensor of the same shape as the input image. - """ - image = tf.cast(image, dtype=tf.float32) - image = tf.truediv(tf.subtract(image, pixel_value_offset), pixel_value_scale) - return image - - -def CalculateReceptiveBoxes(height, width, rf, stride, padding): - """Calculate receptive boxes for each feature point. - - Args: - height: The height of feature map. - width: The width of feature map. - rf: The receptive field size. - stride: The effective stride between two adjacent feature points. - padding: The effective padding size. - - Returns: - rf_boxes: [N, 4] receptive boxes tensor. Here N equals to height x width. - Each box is represented by [ymin, xmin, ymax, xmax]. - """ - x, y = tf.meshgrid(tf.range(width), tf.range(height)) - coordinates = tf.reshape(tf.stack([y, x], axis=2), [-1, 2]) - # [y,x,y,x] - point_boxes = tf.cast( - tf.concat([coordinates, coordinates], 1), dtype=tf.float32) - bias = [-padding, -padding, -padding + rf - 1, -padding + rf - 1] - rf_boxes = stride * point_boxes + bias - return rf_boxes - - -def CalculateKeypointCenters(boxes): - """Helper function to compute feature centers, from RF boxes. - - Args: - boxes: [N, 4] float tensor. - - Returns: - centers: [N, 2] float tensor. - """ - return tf.divide( - tf.add( - tf.gather(boxes, [0, 1], axis=1), tf.gather(boxes, [2, 3], axis=1)), - 2.0) - - -def ApplyPcaAndWhitening(data, - pca_matrix, - pca_mean, - output_dim, - use_whitening=False, - pca_variances=None): - """Applies PCA/whitening to data. - - Args: - data: [N, dim] float tensor containing data which undergoes PCA/whitening. - pca_matrix: [dim, dim] float tensor PCA matrix, row-major. - pca_mean: [dim] float tensor, mean to subtract before projection. - output_dim: Number of dimensions to use in output data, of type int. - use_whitening: Whether whitening is to be used. - pca_variances: [dim] float tensor containing PCA variances. Only used if - use_whitening is True. - - Returns: - output: [N, output_dim] float tensor with output of PCA/whitening operation. - """ - output = tf.matmul( - tf.subtract(data, pca_mean), - tf.slice(pca_matrix, [0, 0], [output_dim, -1]), - transpose_b=True, - name='pca_matmul') - - # Apply whitening if desired. - if use_whitening: - output = tf.divide( - output, - tf.sqrt(tf.slice(pca_variances, [0], [output_dim])), - name='whitening') - - return output - - -def PostProcessDescriptors(descriptors, use_pca, pca_parameters=None): - """Post-process descriptors. - - Args: - descriptors: [N, input_dim] float tensor. - use_pca: Whether to use PCA. - pca_parameters: Only used if `use_pca` is True. Dict containing PCA - parameter tensors, with keys 'mean', 'matrix', 'dim', 'use_whitening', - 'variances'. - - Returns: - final_descriptors: [N, output_dim] float tensor with descriptors after - normalization and (possibly) PCA/whitening. - """ - # L2-normalize, and if desired apply PCA (followed by L2-normalization). - final_descriptors = tf.nn.l2_normalize( - descriptors, axis=1, name='l2_normalization') - - if use_pca: - # Apply PCA, and whitening if desired. - final_descriptors = ApplyPcaAndWhitening(final_descriptors, - pca_parameters['matrix'], - pca_parameters['mean'], - pca_parameters['dim'], - pca_parameters['use_whitening'], - pca_parameters['variances']) - - # Re-normalize. - final_descriptors = tf.nn.l2_normalize( - final_descriptors, axis=1, name='pca_l2_normalization') - - return final_descriptors - - -def DelfFeaturePostProcessing(boxes, descriptors, use_pca, pca_parameters=None): - """Extract DELF features from input image. - - Args: - boxes: [N, 4] float tensor which denotes the selected receptive box. N is - the number of final feature points which pass through keypoint selection - and NMS steps. - descriptors: [N, input_dim] float tensor. - use_pca: Whether to use PCA. - pca_parameters: Only used if `use_pca` is True. Dict containing PCA - parameter tensors, with keys 'mean', 'matrix', 'dim', 'use_whitening', - 'variances'. - - Returns: - locations: [N, 2] float tensor which denotes the selected keypoint - locations. - final_descriptors: [N, output_dim] float tensor with DELF descriptors after - normalization and (possibly) PCA/whitening. - """ - - # Get center of descriptor boxes, corresponding to feature locations. - locations = CalculateKeypointCenters(boxes) - final_descriptors = PostProcessDescriptors(descriptors, use_pca, - pca_parameters) - - return locations, final_descriptors diff --git a/research/delf/delf/python/feature_extractor_test.py b/research/delf/delf/python/feature_extractor_test.py deleted file mode 100644 index 0caa51c4321..00000000000 --- a/research/delf/delf/python/feature_extractor_test.py +++ /dev/null @@ -1,75 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for DELF feature extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from delf import feature_extractor - - -class FeatureExtractorTest(tf.test.TestCase): - - def testNormalizePixelValues(self): - image = tf.constant( - [[[3, 255, 0], [34, 12, 5]], [[45, 5, 65], [56, 77, 89]]], - dtype=tf.uint8) - normalized_image = feature_extractor.NormalizePixelValues( - image, pixel_value_offset=5.0, pixel_value_scale=2.0) - exp_normalized_image = [[[-1.0, 125.0, -2.5], [14.5, 3.5, 0.0]], - [[20.0, 0.0, 30.0], [25.5, 36.0, 42.0]]] - - self.assertAllEqual(normalized_image, exp_normalized_image) - - def testCalculateReceptiveBoxes(self): - boxes = feature_extractor.CalculateReceptiveBoxes( - height=1, width=2, rf=291, stride=32, padding=145) - exp_boxes = [[-145., -145., 145., 145.], [-145., -113., 145., 177.]] - - self.assertAllEqual(exp_boxes, boxes) - - def testCalculateKeypointCenters(self): - boxes = [[-10.0, 0.0, 11.0, 21.0], [-2.5, 5.0, 18.5, 26.0], - [45.0, -2.5, 66.0, 18.5]] - centers = feature_extractor.CalculateKeypointCenters(boxes) - - exp_centers = [[0.5, 10.5], [8.0, 15.5], [55.5, 8.0]] - - self.assertAllEqual(exp_centers, centers) - - def testPcaWhitening(self): - data = tf.constant([[1.0, 2.0, -2.0], [-5.0, 0.0, 3.0], [-1.0, 2.0, 0.0], - [0.0, 4.0, -1.0]]) - pca_matrix = tf.constant([[2.0, 0.0, -1.0], [0.0, 1.0, 1.0], - [-1.0, 1.0, 3.0]]) - pca_mean = tf.constant([1.0, 2.0, 3.0]) - output_dim = 2 - use_whitening = True - pca_variances = tf.constant([4.0, 1.0]) - - output = feature_extractor.ApplyPcaAndWhitening(data, pca_matrix, pca_mean, - output_dim, use_whitening, - pca_variances) - - exp_output = [[2.5, -5.0], [-6.0, -2.0], [-0.5, -3.0], [1.0, -2.0]] - - self.assertAllEqual(exp_output, output) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/feature_io.py b/research/delf/delf/python/feature_io.py deleted file mode 100644 index 9b68586b854..00000000000 --- a/research/delf/delf/python/feature_io.py +++ /dev/null @@ -1,196 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Python interface for DelfFeatures proto. - -Support read and write of DelfFeatures from/to numpy arrays and file. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf - -from delf import feature_pb2 -from delf import datum_io - - -def ArraysToDelfFeatures(locations, - scales, - descriptors, - attention, - orientations=None): - """Converts DELF features to DelfFeatures proto. - - Args: - locations: [N, 2] float array which denotes the selected keypoint locations. - N is the number of features. - scales: [N] float array with feature scales. - descriptors: [N, depth] float array with DELF descriptors. - attention: [N] float array with attention scores. - orientations: [N] float array with orientations. If None, all orientations - are set to zero. - - Returns: - delf_features: DelfFeatures object. - """ - num_features = len(attention) - assert num_features == locations.shape[0] - assert num_features == len(scales) - assert num_features == descriptors.shape[0] - - if orientations is None: - orientations = np.zeros([num_features], dtype=np.float32) - else: - assert num_features == len(orientations) - - delf_features = feature_pb2.DelfFeatures() - for i in range(num_features): - delf_feature = delf_features.feature.add() - delf_feature.y = locations[i, 0] - delf_feature.x = locations[i, 1] - delf_feature.scale = scales[i] - delf_feature.orientation = orientations[i] - delf_feature.strength = attention[i] - delf_feature.descriptor.CopyFrom(datum_io.ArrayToDatum(descriptors[i,])) - - return delf_features - - -def DelfFeaturesToArrays(delf_features): - """Converts data saved in DelfFeatures to numpy arrays. - - If there are no features, the function returns four empty arrays. - - Args: - delf_features: DelfFeatures object. - - Returns: - locations: [N, 2] float array which denotes the selected keypoint - locations. N is the number of features. - scales: [N] float array with feature scales. - descriptors: [N, depth] float array with DELF descriptors. - attention: [N] float array with attention scores. - orientations: [N] float array with orientations. - """ - num_features = len(delf_features.feature) - if num_features == 0: - return np.array([]), np.array([]), np.array([]), np.array([]), np.array([]) - - # Figure out descriptor dimensionality by parsing first one. - descriptor_dim = len( - datum_io.DatumToArray(delf_features.feature[0].descriptor)) - locations = np.zeros([num_features, 2]) - scales = np.zeros([num_features]) - descriptors = np.zeros([num_features, descriptor_dim]) - attention = np.zeros([num_features]) - orientations = np.zeros([num_features]) - - for i in range(num_features): - delf_feature = delf_features.feature[i] - locations[i, 0] = delf_feature.y - locations[i, 1] = delf_feature.x - scales[i] = delf_feature.scale - descriptors[i,] = datum_io.DatumToArray(delf_feature.descriptor) - attention[i] = delf_feature.strength - orientations[i] = delf_feature.orientation - - return locations, scales, descriptors, attention, orientations - - -def SerializeToString(locations, - scales, - descriptors, - attention, - orientations=None): - """Converts numpy arrays to serialized DelfFeatures. - - Args: - locations: [N, 2] float array which denotes the selected keypoint locations. - N is the number of features. - scales: [N] float array with feature scales. - descriptors: [N, depth] float array with DELF descriptors. - attention: [N] float array with attention scores. - orientations: [N] float array with orientations. If None, all orientations - are set to zero. - - Returns: - Serialized DelfFeatures string. - """ - delf_features = ArraysToDelfFeatures(locations, scales, descriptors, - attention, orientations) - return delf_features.SerializeToString() - - -def ParseFromString(string): - """Converts serialized DelfFeatures string to numpy arrays. - - Args: - string: Serialized DelfFeatures string. - - Returns: - locations: [N, 2] float array which denotes the selected keypoint - locations. N is the number of features. - scales: [N] float array with feature scales. - descriptors: [N, depth] float array with DELF descriptors. - attention: [N] float array with attention scores. - orientations: [N] float array with orientations. - """ - delf_features = feature_pb2.DelfFeatures() - delf_features.ParseFromString(string) - return DelfFeaturesToArrays(delf_features) - - -def ReadFromFile(file_path): - """Helper function to load data from a DelfFeatures format in a file. - - Args: - file_path: Path to file containing data. - - Returns: - locations: [N, 2] float array which denotes the selected keypoint - locations. N is the number of features. - scales: [N] float array with feature scales. - descriptors: [N, depth] float array with DELF descriptors. - attention: [N] float array with attention scores. - orientations: [N] float array with orientations. - """ - with tf.io.gfile.GFile(file_path, 'rb') as f: - return ParseFromString(f.read()) - - -def WriteToFile(file_path, - locations, - scales, - descriptors, - attention, - orientations=None): - """Helper function to write data to a file in DelfFeatures format. - - Args: - file_path: Path to file that will be written. - locations: [N, 2] float array which denotes the selected keypoint locations. - N is the number of features. - scales: [N] float array with feature scales. - descriptors: [N, depth] float array with DELF descriptors. - attention: [N] float array with attention scores. - orientations: [N] float array with orientations. If None, all orientations - are set to zero. - """ - serialized_data = SerializeToString(locations, scales, descriptors, attention, - orientations) - with tf.io.gfile.GFile(file_path, 'w') as f: - f.write(serialized_data) diff --git a/research/delf/delf/python/feature_io_test.py b/research/delf/delf/python/feature_io_test.py deleted file mode 100644 index 8b68d3b241c..00000000000 --- a/research/delf/delf/python/feature_io_test.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for feature_io, the python interface of DelfFeatures.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags -import numpy as np -import tensorflow as tf - -from delf import feature_io - -FLAGS = flags.FLAGS - - -def create_data(): - """Creates data to be used in tests. - - Returns: - locations: [N, 2] float array which denotes the selected keypoint - locations. N is the number of features. - scales: [N] float array with feature scales. - descriptors: [N, depth] float array with DELF descriptors. - attention: [N] float array with attention scores. - orientations: [N] float array with orientations. - """ - locations = np.arange(8, dtype=np.float32).reshape(4, 2) - scales = np.arange(4, dtype=np.float32) - attention = np.arange(4, dtype=np.float32) - orientations = np.arange(4, dtype=np.float32) - descriptors = np.zeros([4, 1024]) - descriptors[0,] = np.arange(1024) - descriptors[1,] = np.zeros([1024]) - descriptors[2,] = np.ones([1024]) - descriptors[3,] = -np.ones([1024]) - - return locations, scales, descriptors, attention, orientations - - -class DelfFeaturesIoTest(tf.test.TestCase): - - def testConversionAndBack(self): - locations, scales, descriptors, attention, orientations = create_data() - - serialized = feature_io.SerializeToString(locations, scales, descriptors, - attention, orientations) - parsed_data = feature_io.ParseFromString(serialized) - - self.assertAllEqual(locations, parsed_data[0]) - self.assertAllEqual(scales, parsed_data[1]) - self.assertAllEqual(descriptors, parsed_data[2]) - self.assertAllEqual(attention, parsed_data[3]) - self.assertAllEqual(orientations, parsed_data[4]) - - def testConversionAndBackNoOrientations(self): - locations, scales, descriptors, attention, _ = create_data() - - serialized = feature_io.SerializeToString(locations, scales, descriptors, - attention) - parsed_data = feature_io.ParseFromString(serialized) - - self.assertAllEqual(locations, parsed_data[0]) - self.assertAllEqual(scales, parsed_data[1]) - self.assertAllEqual(descriptors, parsed_data[2]) - self.assertAllEqual(attention, parsed_data[3]) - self.assertAllEqual(np.zeros([4]), parsed_data[4]) - - def testWriteAndReadToFile(self): - locations, scales, descriptors, attention, orientations = create_data() - - filename = os.path.join(FLAGS.test_tmpdir, 'test.delf') - feature_io.WriteToFile(filename, locations, scales, descriptors, attention, - orientations) - data_read = feature_io.ReadFromFile(filename) - - self.assertAllEqual(locations, data_read[0]) - self.assertAllEqual(scales, data_read[1]) - self.assertAllEqual(descriptors, data_read[2]) - self.assertAllEqual(attention, data_read[3]) - self.assertAllEqual(orientations, data_read[4]) - - def testWriteAndReadToFileEmptyFile(self): - filename = os.path.join(FLAGS.test_tmpdir, 'test.delf') - feature_io.WriteToFile(filename, np.array([]), np.array([]), np.array([]), - np.array([]), np.array([])) - data_read = feature_io.ReadFromFile(filename) - - self.assertAllEqual(np.array([]), data_read[0]) - self.assertAllEqual(np.array([]), data_read[1]) - self.assertAllEqual(np.array([]), data_read[2]) - self.assertAllEqual(np.array([]), data_read[3]) - self.assertAllEqual(np.array([]), data_read[4]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/normalization_layers/__init__.py b/research/delf/delf/python/normalization_layers/__init__.py deleted file mode 100644 index 9064f503de1..00000000000 --- a/research/delf/delf/python/normalization_layers/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== \ No newline at end of file diff --git a/research/delf/delf/python/normalization_layers/normalization.py b/research/delf/delf/python/normalization_layers/normalization.py deleted file mode 100644 index cfb75da7535..00000000000 --- a/research/delf/delf/python/normalization_layers/normalization.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Normalization layer definitions.""" - -import tensorflow as tf - - -class L2Normalization(tf.keras.layers.Layer): - """Normalization layer using L2 norm.""" - - def __init__(self): - """Initialization of the L2Normalization layer.""" - super(L2Normalization, self).__init__() - # A lower bound value for the norm. - self.eps = 1e-6 - - def call(self, x, axis=1): - """Invokes the L2Normalization instance. - - Args: - x: A Tensor. - axis: Dimension along which to normalize. A scalar or a vector of - integers. - - Returns: - norm: A Tensor with the same shape as `x`. - """ - return tf.nn.l2_normalize(x, axis, epsilon=self.eps) diff --git a/research/delf/delf/python/normalization_layers/normalization_test.py b/research/delf/delf/python/normalization_layers/normalization_test.py deleted file mode 100644 index ea302566c69..00000000000 --- a/research/delf/delf/python/normalization_layers/normalization_test.py +++ /dev/null @@ -1,36 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for normalization layers.""" - -import tensorflow as tf - -from delf.python.normalization_layers import normalization - - -class NormalizationsTest(tf.test.TestCase): - - def testL2Normalization(self): - x = tf.constant([-4.0, 0.0, 4.0]) - layer = normalization.L2Normalization() - # Run tested function. - result = layer(x, axis=0) - # Define expected result. - exp_output = [-0.70710677, 0.0, 0.70710677] - # Compare actual and expected. - self.assertAllClose(exp_output, result) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/pooling_layers/__init__.py b/research/delf/delf/python/pooling_layers/__init__.py deleted file mode 100644 index 9064f503de1..00000000000 --- a/research/delf/delf/python/pooling_layers/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== \ No newline at end of file diff --git a/research/delf/delf/python/pooling_layers/pooling.py b/research/delf/delf/python/pooling_layers/pooling.py deleted file mode 100644 index 8244a414b31..00000000000 --- a/research/delf/delf/python/pooling_layers/pooling.py +++ /dev/null @@ -1,194 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Pooling layers definitions.""" - -import tensorflow as tf - - -class MAC(tf.keras.layers.Layer): - """Global max pooling (MAC) layer. - - Maximum Activations of Convolutions (MAC) is simply constructed by - max-pooling over all dimensions per feature map. See - https://arxiv.org/abs/1511.05879 for a reference. - """ - - def call(self, x, axis=None): - """Invokes the MAC pooling instance. - - Args: - x: [B, H, W, D] A float32 Tensor. - axis: Dimensions to reduce. By default, dimensions [1, 2] are reduced. - - Returns: - output: [B, D] A float32 Tensor. - """ - if axis is None: - axis = [1, 2] - return mac(x, axis=axis) - - -class SPoC(tf.keras.layers.Layer): - """Average pooling (SPoC) layer. - - Sum-pooled convolutional features (SPoC) is based on the sum pooling of the - deep features. See https://arxiv.org/pdf/1510.07493.pdf for a reference. - """ - - def call(self, x, axis=None): - """Invokes the SPoC instance. - - Args: - x: [B, H, W, D] A float32 Tensor. - axis: Dimensions to reduce. By default, dimensions [1, 2] are reduced. - - Returns: - output: [B, D] A float32 Tensor. - """ - if axis is None: - axis = [1, 2] - return spoc(x, axis) - - -class GeM(tf.keras.layers.Layer): - """Generalized mean pooling (GeM) layer. - - Generalized Mean Pooling (GeM) computes the generalized mean of each - channel in a tensor. See https://arxiv.org/abs/1711.02512 for a reference. - """ - - def __init__(self, power=3.): - """Initialization of the generalized mean pooling (GeM) layer. - - Args: - power: Float power > 0 is an inverse exponent parameter, used during the - generalized mean pooling computation. Setting this exponent as power > 1 - increases the contrast of the pooled feature map and focuses on the - salient features of the image. GeM is a generalization of the average - pooling commonly used in classification networks (power = 1) and of - spatial max-pooling layer (power = inf). - """ - super(GeM, self).__init__() - self.power = power - self.eps = 1e-6 - - def call(self, x, axis=None): - """Invokes the GeM instance. - - Args: - x: [B, H, W, D] A float32 Tensor. - axis: Dimensions to reduce. By default, dimensions [1, 2] are reduced. - - Returns: - output: [B, D] A float32 Tensor. - """ - if axis is None: - axis = [1, 2] - return gem(x, power=self.power, eps=self.eps, axis=axis) - - -class GeMPooling2D(tf.keras.layers.Layer): - """Generalized mean pooling (GeM) pooling operation for spatial data.""" - - def __init__(self, - power=20., - pool_size=(2, 2), - strides=None, - padding='valid', - data_format='channels_last'): - """Initialization of GeMPooling2D. - - Args: - power: Float, power > 0. is an inverse exponent parameter (GeM power). - pool_size: Integer or tuple of 2 integers, factors by which to downscale - (vertical, horizontal) - strides: Integer, tuple of 2 integers, or None. Strides values. If None, - it will default to `pool_size`. - padding: One of `valid` or `same`. `valid` means no padding. `same` - results in padding evenly to the left/right or up/down of the input such - that output has the same height/width dimension as the input. - data_format: A string, one of `channels_last` (default) or - `channels_first`. The ordering of the dimensions in the inputs. - `channels_last` corresponds to inputs with shape `(batch, height, width, - channels)` while `channels_first` corresponds to inputs with shape - `(batch, channels, height, width)`. - """ - super(GeMPooling2D, self).__init__() - self.power = power - self.eps = 1e-6 - self.pool_size = pool_size - self.strides = strides - self.padding = padding.upper() - data_format_conv = { - 'channels_last': 'NHWC', - 'channels_first': 'NCHW', - } - self.data_format = data_format_conv[data_format] - - def call(self, x): - tmp = tf.pow(x, self.power) - tmp = tf.nn.avg_pool(tmp, self.pool_size, self.strides, self.padding, - self.data_format) - out = tf.pow(tmp, 1. / self.power) - return out - - -def mac(x, axis=None): - """Performs global max pooling (MAC). - - Args: - x: [B, H, W, D] A float32 Tensor. - axis: Dimensions to reduce. By default, dimensions [1, 2] are reduced. - - Returns: - output: [B, D] A float32 Tensor. - """ - if axis is None: - axis = [1, 2] - return tf.reduce_max(x, axis=axis, keepdims=False) - - -def spoc(x, axis=None): - """Performs average pooling (SPoC). - - Args: - x: [B, H, W, D] A float32 Tensor. - axis: Dimensions to reduce. By default, dimensions [1, 2] are reduced. - - Returns: - output: [B, D] A float32 Tensor. - """ - if axis is None: - axis = [1, 2] - return tf.reduce_mean(x, axis=axis, keepdims=False) - - -def gem(x, axis=None, power=3., eps=1e-6): - """Performs generalized mean pooling (GeM). - - Args: - x: [B, H, W, D] A float32 Tensor. - axis: Dimensions to reduce. By default, dimensions [1, 2] are reduced. - power: Float, power > 0 is an inverse exponent parameter (GeM power). - eps: Float, parameter for numerical stability. - - Returns: - output: [B, D] A float32 Tensor. - """ - if axis is None: - axis = [1, 2] - tmp = tf.pow(tf.maximum(x, eps), power) - out = tf.pow(tf.reduce_mean(tmp, axis=axis, keepdims=False), 1. / power) - return out diff --git a/research/delf/delf/python/pooling_layers/pooling_test.py b/research/delf/delf/python/pooling_layers/pooling_test.py deleted file mode 100644 index 78653550e45..00000000000 --- a/research/delf/delf/python/pooling_layers/pooling_test.py +++ /dev/null @@ -1,84 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for pooling layers.""" - -import tensorflow as tf - -from delf.python.pooling_layers import pooling - - -class PoolingsTest(tf.test.TestCase): - - def testMac(self): - x = tf.constant([[[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]]]) - # Run tested function. - result = pooling.mac(x) - # Define expected result. - exp_output = [[6., 7.]] - # Compare actual and expected. - self.assertAllClose(exp_output, result) - - def testSpoc(self): - x = tf.constant([[[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]]]) - # Run tested function. - result = pooling.spoc(x) - # Define expected result. - exp_output = [[3., 4.]] - # Compare actual and expected. - self.assertAllClose(exp_output, result) - - def testGem(self): - x = tf.constant([[[[0., 1.], [2., 3.]], [[4., 5.], [6., 7.]]]]) - # Run tested function. - result = pooling.gem(x, power=3., eps=1e-6) - # Define expected result. - exp_output = [[4.1601677, 4.9866314]] - # Compare actual and expected. - self.assertAllClose(exp_output, result) - - def testGeMPooling2D(self): - # Create a testing tensor. - x = tf.constant([[[1., 2., 3.], - [4., 5., 6.], - [7., 8., 9.]]]) - x = tf.reshape(x, [1, 3, 3, 1]) - - # Checking GeMPooling2D relation to MaxPooling2D for the large values of - # `p`. - max_pool_2d = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), - strides=(1, 1), padding='valid') - out_max = max_pool_2d(x) - gem_pool_2d = pooling.GeMPooling2D(power=30., pool_size=(2, 2), - strides=(1, 1), padding='valid') - out_gem_max = gem_pool_2d(x) - - # Check that for large `p` GeMPooling2D is close to MaxPooling2D. - self.assertAllEqual(out_max, tf.round(out_gem_max)) - - # Checking GeMPooling2D relation to AveragePooling2D for the value - # of `p` = 1. - avg_pool_2d = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), - strides=(1, 1), - padding='valid') - out_avg = avg_pool_2d(x) - gem_pool_2d = pooling.GeMPooling2D(power=1., pool_size=(2, 2), - strides=(1, 1), padding='valid') - out_gem_avg = gem_pool_2d(x) - # Check that for `p` equals 1., GeMPooling2D becomes AveragePooling2D. - self.assertAllEqual(out_avg, out_gem_avg) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/training/README.md b/research/delf/delf/python/training/README.md deleted file mode 100644 index 41ea2a0b47f..00000000000 --- a/research/delf/delf/python/training/README.md +++ /dev/null @@ -1,339 +0,0 @@ -# DELF/DELG Training Instructions - -This README documents the end-to-end process for training a local and/or global -image feature model on the -[Google Landmarks Dataset v2](https://github.com/cvdfoundation/google-landmark) -(GLDv2). This can be achieved following these steps: - -1. Install the DELF Python library. -2. Download the raw images of the GLDv2 dataset. -3. Prepare the training data. -4. Run the training. - -The next sections will cove each of these steps in greater detail. - -## Prerequisites - -Clone the [TensorFlow Model Garden](https://github.com/tensorflow/models) -repository and move into the `models/research/delf/delf/python/training`folder. - -``` -git clone https://github.com/tensorflow/models.git -cd models/research/delf/delf/python/training -``` - -## Install the DELF Library - -To be able to use this code, please follow -[these instructions](../../../INSTALL_INSTRUCTIONS.md) to properly install the -DELF library. - -## Download the GLDv2 Training Data - -The [GLDv2](https://github.com/cvdfoundation/google-landmark) images are grouped -in 3 datasets: TRAIN, INDEX, TEST. Images in each dataset are grouped into -`*.tar` files and individually referenced in `*.csv`files containing training -metadata and licensing information. The number of `*.tar` files per dataset is -as follows: - -* TRAIN: 500 files. -* INDEX: 100 files. -* TEST: 20 files. - -To download the GLDv2 images, run the -[`download_dataset.sh`](./download_dataset.sh) script like in the following -example: - -``` -bash download_dataset.sh 500 100 20 -``` - -The script takes the following parameters, in order: - -* The number of image files from the TRAIN dataset to download (maximum 500). -* The number of image files from the INDEX dataset to download (maximum 100). -* The number of image files from the TEST dataset to download (maximum 20). - -The script downloads the GLDv2 images under the following directory structure: - -* gldv2_dataset/ - * train/ - Contains raw images from the TRAIN dataset. - * index/ - Contains raw images from the INDEX dataset. - * test/ - Contains raw images from the TEST dataset. - -Each of the three folders `gldv2_dataset/train/`, `gldv2_dataset/index/` and -`gldv2_dataset/test/` contains the following: - -* The downloaded `*.tar` files. -* The corresponding MD5 checksum files, `*.txt`. -* The unpacked content of the downloaded files. (*Images are organized in - folders and subfolders based on the first, second and third character in - their file name.*) -* The CSV files containing training and licensing metadata of the downloaded - images. - -*Please note that due to the large size of the GLDv2 dataset, the download can -take up to 12 hours and up to 1 TB of disk space. In order to save bandwidth and -disk space, you may want to start by downloading only the TRAIN dataset, the -only one required for the training, thus saving approximately ~95 GB, the -equivalent of the INDEX and TEST datasets. To further save disk space, the -`*.tar` files can be deleted after downloading and upacking them.* - -## Prepare the Data for Training - -Preparing the data for training consists of creating -[TFRecord](https://www.tensorflow.org/tutorials/load_data/tfrecord) files from -the raw GLDv2 images grouped into TRAIN and VALIDATION splits. The training set -produced contains only the *clean* subset of the GLDv2 dataset. The -[CVPR'20 paper](https://arxiv.org/abs/2004.01804) introducing the GLDv2 dataset -contains a detailed description of the *clean* subset. - -Generating the TFRecord files containing the TRAIN and VALIDATION splits of the -*clean* GLDv2 subset can be achieved by running the -[`build_image_dataset.py`](./build_image_dataset.py) script. Assuming that the -GLDv2 images have been downloaded to the `gldv2_dataset` folder, the script can -be run as follows: - -``` -python3 build_image_dataset.py \ - --train_csv_path=gldv2_dataset/train/train.csv \ - --train_clean_csv_path=gldv2_dataset/train/train_clean.csv \ - --train_directory=gldv2_dataset/train/*/*/*/ \ - --output_directory=gldv2_dataset/tfrecord/ \ - --num_shards=128 \ - --generate_train_validation_splits \ - --validation_split_size=0.2 -``` - -*Please refer to the source code of the -[`build_image_dataset.py`](./build_image_dataset.py) script for a detailed -description of its parameters.* - -The TFRecord files written in the `OUTPUT_DIRECTORY` will be prefixed as -follows: - -* TRAIN split: `train-*` -* VALIDATION split: `validation-*` - -The same script can be used to generate TFRecord files for the TEST split for -post-training evaluation purposes. This can be achieved by adding the -parameters: - -``` ---test_csv_path=gldv2_dataset/train/test.csv \ ---test_directory=gldv2_dataset/test/*/*/*/ \ -``` - -In this scenario, the TFRecord files of the TEST split written in the -`OUTPUT_DIRECTORY` will be named according to the pattern `test-*`. - -*Please note that due to the large size of the GLDv2 dataset, the generation of -the TFRecord files can take up to 12 hours and up to 500 GB of space disk.* - -## Running the Training - -For the training to converge faster, it is possible to initialize the ResNet -backbone with the weights of a pretrained ImageNet model. The ImageNet -checkpoint is available at the following location: -[`http://storage.googleapis.com/delf/resnet50_imagenet_weights.tar.gz`](http://storage.googleapis.com/delf/resnet50_imagenet_weights.tar.gz). -To download and unpack it run the following commands on a Linux box: - -``` -curl -Os http://storage.googleapis.com/delf/resnet50_imagenet_weights.tar.gz -tar -xzvf resnet50_imagenet_weights.tar.gz -``` - -### Training with Local Features - -Assuming the TFRecord files were generated in the `gldv2_dataset/tfrecord/` -directory, running the following command should start training a model and -output the results in the `gldv2_training` directory: - -``` -python3 train.py \ - --train_file_pattern=gldv2_dataset/tfrecord/train* \ - --validation_file_pattern=gldv2_dataset/tfrecord/validation* \ - --imagenet_checkpoint=resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 \ - --dataset_version=gld_v2_clean \ - --logdir=gldv2_training/ -``` - -*NOTE: The `--use_autoencoder` parameter is set by default to `True`, therefore -the model will be by default trained with the autoencoder.* - -### Training with Local and Global Features - -It is also possible to train the model with an improved global features head as -introduced in the [DELG paper](https://arxiv.org/abs/2001.05027). To do this, -specify the additional parameter `--delg_global_features` when launching the -training, like in the following example: - -``` -python3 train.py \ - --train_file_pattern=gldv2_dataset/tfrecord/train* \ - --validation_file_pattern=gldv2_dataset/tfrecord/validation* \ - --imagenet_checkpoint=resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5 \ - --dataset_version=gld_v2_clean \ - --logdir=gldv2_training/ \ - --delg_global_features -``` - -*NOTE: The `--use_autoencoder` parameter is set by default to `True`, therefore -the model will be by default trained with the autoencoder.* - -### Hyperparameter Guidelines - -In order to improve the convergence of the training, the following -hyperparameter values have been tested and validated on the following -infrastructures, the remaining `train.py` flags keeping their **default -values**: -* 8 Tesla P100 GPUs: `--batch_size=256`, `--initial_lr=0.01` -* 4 Tesla P100 GPUs: `--batch_size=128`, `--initial_lr=0.005` - -## Exporting the Trained Model - -Assuming the training output, the TensorFlow checkpoint, is in the -`gldv2_training` directory, running the following commands exports the model. - -### DELF local feature-only model - -This should be used when you are only interested in having a local feature -model. - -``` -python3 model/export_local_model.py \ - --ckpt_path=gldv2_training/delf_weights \ - --export_path=gldv2_model_local -``` - -### DELG global feature-only model - -This should be used when you are only interested in having a global feature -model. - -``` -python3 model/export_global_model.py \ - --ckpt_path=gldv2_training/delf_weights \ - --export_path=gldv2_model_global \ - --delg_global_features -``` - -### DELG local+global feature model - -This should be used when you are interested in jointly extracting local and -global features. - -``` -python3 model/export_local_and_global_model.py \ - --ckpt_path=gldv2_training/delf_weights \ - --export_path=gldv2_model_local_and_global \ - --delg_global_features -``` - -### Kaggle-compatible global feature model - -To export a global feature model in the format required by the -[2020 Landmark Retrieval challenge](https://www.kaggle.com/c/landmark-retrieval-2020), -you can use the following command: - -*NOTE*: this command is helpful to use the model directly in the above-mentioned -Kaggle competition; however, this is a different format than the one required in -this DELF/DELG codebase (ie, if you export the model this way, the commands -found in the [DELG instructions](../delg/DELG_INSTRUCTIONS.md) would not work). -To export the model in a manner compatible to this codebase, use a similar -command as the "DELG global feature-only model" above. - -``` -python3 model/export_global_model.py \ - --ckpt_path=gldv2_training/delf_weights \ - --export_path=gldv2_model_global \ - --input_scales_list=0.70710677,1.0,1.4142135 \ - --multi_scale_pool_type=sum \ - --normalize_global_descriptor -``` - -## Testing the trained model - -### Testing the trained local feature model - -After the trained model has been exported, it can be used to extract DELF -features from 2 images of the same landmark and to perform a matching test -between the 2 images based on the extracted features to validate they represent -the same landmark. - -Start by downloading the Oxford buildings dataset: - -``` -mkdir data && cd data -wget http://www.robots.ox.ac.uk/~vgg/data/oxbuildings/oxbuild_images.tgz -mkdir oxford5k_images oxford5k_features -tar -xvzf oxbuild_images.tgz -C oxford5k_images/ -cd ../ -echo data/oxford5k_images/hertford_000056.jpg >> list_images.txt -echo data/oxford5k_images/oxford_000317.jpg >> list_images.txt -``` - -Make a copy of the -[`delf_config_example.pbtxt`](../examples/delf_config_example.pbtxt) protobuffer -file which configures the DELF feature extraction. Update the file by making the -following changes: - -* set the `model_path` attribute to the directory containing the exported - model, `gldv2_model_local` in this example -* add at the root level the attribute `is_tf2_exported` with the value `true` -* set to `false` the `use_pca` attribute inside `delf_local_config` - -The ensuing file should resemble the following: - -``` -model_path: "gldv2_model_local" -image_scales: .25 -image_scales: .3536 -image_scales: .5 -image_scales: .7071 -image_scales: 1.0 -image_scales: 1.4142 -image_scales: 2.0 -is_tf2_exported: true -delf_local_config { - use_pca: false - max_feature_num: 1000 - score_threshold: 100.0 -} -``` - -Run the following command to extract DELF features for the images -`hertford_000056.jpg` and `oxford_000317.jpg`: - -``` -python3 ../examples/extract_features.py \ - --config_path delf_config_example.pbtxt \ - --list_images_path list_images.txt \ - --output_dir data/oxford5k_features -``` - -Run the following command to perform feature matching between the images -`hertford_000056.jpg` and `oxford_000317.jpg`: - -``` -python3 ../examples/match_images.py \ - --image_1_path data/oxford5k_images/hertford_000056.jpg \ - --image_2_path data/oxford5k_images/oxford_000317.jpg \ - --features_1_path data/oxford5k_features/hertford_000056.delf \ - --features_2_path data/oxford5k_features/oxford_000317.delf \ - --output_image matched_images.png -``` - -The generated image `matched_images.png` should look similar to this one: - -![MatchedImagesDemo](./matched_images_demo.png) - -### Testing the trained global (or global+local) feature model - -Please follow the [DELG instructions](../delg/DELG_INSTRUCTIONS.md). The only -modification should be to pass a different `delf_config_path` when doing feature -extraction, which should point to the newly-trained model. As described in the -[DelfConfig](../../protos/delf_config.proto), you should set the -`use_local_features` and `use_global_features` in the right way, depending on -which feature modalities you are using. Note also that you should set -`is_tf2_exported` to `true`. diff --git a/research/delf/delf/python/training/__init__.py b/research/delf/delf/python/training/__init__.py deleted file mode 100644 index c87f3d895c7..00000000000 --- a/research/delf/delf/python/training/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2020 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Module for DELF training.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=unused-import -from delf.python.training import build_image_dataset -# pylint: enable=unused-import diff --git a/research/delf/delf/python/training/build_image_dataset.py b/research/delf/delf/python/training/build_image_dataset.py deleted file mode 100644 index 23103d49196..00000000000 --- a/research/delf/delf/python/training/build_image_dataset.py +++ /dev/null @@ -1,490 +0,0 @@ -#!/usr/bin/python -# Copyright 2020 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Converts landmark image data to TFRecords file format with Example protos. - -The image data set is expected to reside in JPEG files ends up with '.jpg'. - -This script converts the training and testing data into -a sharded data set consisting of TFRecord files - train_directory/train-00000-of-00128 - train_directory/train-00001-of-00128 - ... - train_directory/train-00127-of-00128 -and - test_directory/test-00000-of-00128 - test_directory/test-00001-of-00128 - ... - test_directory/test-00127-of-00128 -where we have selected 128 shards for both data sets. Each record -within the TFRecord file is a serialized Example proto. The Example proto -contains the following fields: - image/encoded: string containing JPEG encoded image in RGB colorspace - image/height: integer, image height in pixels - image/width: integer, image width in pixels - image/colorspace: string, specifying the colorspace, always 'RGB' - image/channels: integer, specifying the number of channels, always 3 - image/format: string, specifying the format, always 'JPEG' - image/filename: string, the unique id of the image file - e.g. '97c0a12e07ae8dd5' or '650c989dd3493748' -Furthermore, if the data set type is training, it would contain one more field: - image/class/label: integer, the landmark_id from the input training csv file. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import csv -import os - -from absl import app -from absl import flags - -import numpy as np -import pandas as pd -import tensorflow as tf - -FLAGS = flags.FLAGS - -flags.DEFINE_string('train_directory', '/tmp/', 'Training data directory.') -flags.DEFINE_string('test_directory', None, - '(Optional) Testing data directory. Required only if ' - 'test_csv_path is not None.') -flags.DEFINE_string('output_directory', '/tmp/', 'Output data directory.') -flags.DEFINE_string('train_csv_path', '/tmp/train.csv', - 'Training data csv file path.') -flags.DEFINE_string('train_clean_csv_path', None, - ('(Optional) Clean training data csv file path. ' - 'If provided, filters images keeping the ones listed in ' - 'this file. In this case, also outputs a CSV file ' - 'relabeling.csv mapping new labels to old ones.')) -flags.DEFINE_string('test_csv_path', None, - '(Optional) Testing data csv file path. If None or absent,' - 'TFRecords for the images in the test dataset are not' - 'generated') -flags.DEFINE_integer('num_shards', 128, 'Number of shards in output data.') -flags.DEFINE_boolean('generate_train_validation_splits', False, - '(Optional) Whether to split the train dataset into' - 'TRAIN and VALIDATION splits.') -flags.DEFINE_float('validation_split_size', 0.2, - '(Optional) The size of the VALIDATION split as a fraction' - 'of the train dataset.') -flags.DEFINE_integer('seed', 0, - '(Optional) The seed to be used while shuffling the train' - 'dataset when generating the TRAIN and VALIDATION splits.' - 'Recommended for splits reproducibility purposes.') - -_FILE_IDS_KEY = 'file_ids' -_IMAGE_PATHS_KEY = 'image_paths' -_LABELS_KEY = 'labels' -_TEST_SPLIT = 'test' -_TRAIN_SPLIT = 'train' -_VALIDATION_SPLIT = 'validation' - - -def _get_all_image_files_and_labels(name, csv_path, image_dir): - """Process input and get the image file paths, image ids and the labels. - - Args: - name: 'train' or 'test'. - csv_path: path to the Google-landmark Dataset csv Data Sources files. - image_dir: directory that stores downloaded images. - Returns: - image_paths: the paths to all images in the image_dir. - file_ids: the unique ids of images. - labels: the landmark id of all images. When name='test', the returned labels - will be an empty list. - Raises: - ValueError: if input name is not supported. - """ - image_paths = tf.io.gfile.glob(os.path.join(image_dir, '*.jpg')) - file_ids = [os.path.basename(os.path.normpath(f))[:-4] for f in image_paths] - if name == _TRAIN_SPLIT: - with tf.io.gfile.GFile(csv_path, 'rb') as csv_file: - df = pd.read_csv(csv_file) - df = df.set_index('id') - labels = [int(df.loc[fid]['landmark_id']) for fid in file_ids] - elif name == _TEST_SPLIT: - labels = [] - else: - raise ValueError('Unsupported dataset split name: %s' % name) - return image_paths, file_ids, labels - - -def _get_clean_train_image_files_and_labels(csv_path, image_dir): - """Get image file paths, image ids and labels for the clean training split. - - Args: - csv_path: path to the Google-landmark Dataset v2 CSV Data Sources files - of the clean train dataset. Assumes CSV header landmark_id;images. - image_dir: directory that stores downloaded images. - - Returns: - image_paths: the paths to all images in the image_dir. - file_ids: the unique ids of images. - labels: the landmark id of all images. - relabeling: relabeling rules created to replace actual labels with - a continuous set of labels. - """ - # Load the content of the CSV file (landmark_id/label -> images). - with tf.io.gfile.GFile(csv_path, 'rb') as csv_file: - df = pd.read_csv(csv_file) - - # Create the dictionary (key = image_id, value = {label, file_id}). - images = {} - for _, row in df.iterrows(): - label = row['landmark_id'] - for file_id in row['images'].split(' '): - images[file_id] = {} - images[file_id]['label'] = label - images[file_id]['file_id'] = file_id - - # Add the full image path to the dictionary of images. - image_paths = tf.io.gfile.glob(os.path.join(image_dir, '*.jpg')) - for image_path in image_paths: - file_id = os.path.basename(os.path.normpath(image_path))[:-4] - if file_id in images: - images[file_id]['image_path'] = image_path - - # Explode the dictionary into lists (1 per image attribute). - image_paths = [] - file_ids = [] - labels = [] - for _, value in images.items(): - image_paths.append(value['image_path']) - file_ids.append(value['file_id']) - labels.append(value['label']) - - # Relabel image labels to contiguous values. - unique_labels = sorted(set(labels)) - relabeling = {label: index for index, label in enumerate(unique_labels)} - new_labels = [relabeling[label] for label in labels] - return image_paths, file_ids, new_labels, relabeling - - -def _process_image(filename): - """Process a single image file. - - Args: - filename: string, path to an image file e.g., '/path/to/example.jpg'. - - Returns: - image_buffer: string, JPEG encoding of RGB image. - height: integer, image height in pixels. - width: integer, image width in pixels. - Raises: - ValueError: if parsed image has wrong number of dimensions or channels. - """ - # Read the image file. - with tf.io.gfile.GFile(filename, 'rb') as f: - image_data = f.read() - - # Decode the RGB JPEG. - image = tf.io.decode_jpeg(image_data, channels=3) - - # Check that image converted to RGB - if len(image.shape) != 3: - raise ValueError('The parsed image number of dimensions is not 3 but %d' % - (image.shape)) - height = image.shape[0] - width = image.shape[1] - if image.shape[2] != 3: - raise ValueError('The parsed image channels is not 3 but %d' % - (image.shape[2])) - - return image_data, height, width - - -def _int64_feature(value): - """Returns an int64_list from a bool / enum / int / uint.""" - return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) - - -def _bytes_feature(value): - """Returns a bytes_list from a string / byte.""" - return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) - - -def _convert_to_example(file_id, image_buffer, height, width, label=None): - """Build an Example proto for the given inputs. - - Args: - file_id: string, unique id of an image file, e.g., '97c0a12e07ae8dd5'. - image_buffer: string, JPEG encoding of RGB image. - height: integer, image height in pixels. - width: integer, image width in pixels. - label: integer, the landmark id and prediction label. - - Returns: - Example proto. - """ - colorspace = 'RGB' - channels = 3 - image_format = 'JPEG' - features = { - 'image/height': _int64_feature(height), - 'image/width': _int64_feature(width), - 'image/colorspace': _bytes_feature(colorspace.encode('utf-8')), - 'image/channels': _int64_feature(channels), - 'image/format': _bytes_feature(image_format.encode('utf-8')), - 'image/id': _bytes_feature(file_id.encode('utf-8')), - 'image/encoded': _bytes_feature(image_buffer) - } - if label is not None: - features['image/class/label'] = _int64_feature(label) - example = tf.train.Example(features=tf.train.Features(feature=features)) - - return example - - -def _write_tfrecord(output_prefix, image_paths, file_ids, labels): - """Read image files and write image and label data into TFRecord files. - - Args: - output_prefix: string, the prefix of output files, e.g. 'train'. - image_paths: list of strings, the paths to images to be converted. - file_ids: list of strings, the image unique ids. - labels: list of integers, the landmark ids of images. It is an empty list - when output_prefix='test'. - - Raises: - ValueError: if the length of input images, ids and labels don't match - """ - if output_prefix == _TEST_SPLIT: - labels = [None] * len(image_paths) - if not len(image_paths) == len(file_ids) == len(labels): - raise ValueError('length of image_paths, file_ids, labels shoud be the' + - ' same. But they are %d, %d, %d, respectively' % - (len(image_paths), len(file_ids), len(labels))) - - spacing = np.linspace(0, len(image_paths), FLAGS.num_shards + 1, dtype=np.int) - - for shard in range(FLAGS.num_shards): - output_file = os.path.join( - FLAGS.output_directory, - '%s-%.5d-of-%.5d' % (output_prefix, shard, FLAGS.num_shards)) - writer = tf.io.TFRecordWriter(output_file) - print('Processing shard ', shard, ' and writing file ', output_file) - for i in range(spacing[shard], spacing[shard + 1]): - image_buffer, height, width = _process_image(image_paths[i]) - example = _convert_to_example(file_ids[i], image_buffer, height, width, - labels[i]) - writer.write(example.SerializeToString()) - writer.close() - - -def _write_relabeling_rules(relabeling_rules): - """Write to a file the relabeling rules when the clean train dataset is used. - - Args: - relabeling_rules: dictionary of relabeling rules applied when the clean - train dataset is used (key = old_label, value = new_label). - """ - relabeling_file_name = os.path.join(FLAGS.output_directory, - 'relabeling.csv') - with tf.io.gfile.GFile(relabeling_file_name, 'w') as relabeling_file: - csv_writer = csv.writer(relabeling_file, delimiter=',') - csv_writer.writerow(['new_label', 'old_label']) - for old_label, new_label in relabeling_rules.items(): - csv_writer.writerow([new_label, old_label]) - - -def _shuffle_by_columns(np_array, random_state): - """Shuffle the columns of a 2D numpy array. - - Args: - np_array: array to shuffle. - random_state: numpy RandomState to be used for shuffling. - Returns: - The shuffled array. - """ - columns = np_array.shape[1] - columns_indices = np.arange(columns) - random_state.shuffle(columns_indices) - return np_array[:, columns_indices] - - -def _build_train_and_validation_splits(image_paths, file_ids, labels, - validation_split_size, seed): - """Create TRAIN and VALIDATION splits containg all labels in equal proportion. - - Args: - image_paths: list of paths to the image files in the train dataset. - file_ids: list of image file ids in the train dataset. - labels: list of image labels in the train dataset. - validation_split_size: size of the VALIDATION split as a ratio of the train - dataset. - seed: seed to use for shuffling the dataset for reproducibility purposes. - - Returns: - splits : tuple containing the TRAIN and VALIDATION splits. - Raises: - ValueError: if the image attributes arrays don't all have the same length, - which makes the shuffling impossible. - """ - # Ensure all image attribute arrays have the same length. - total_images = len(file_ids) - if not (len(image_paths) == total_images and len(labels) == total_images): - raise ValueError('Inconsistencies between number of file_ids (%d), number ' - 'of image_paths (%d) and number of labels (%d). Cannot' - 'shuffle the train dataset.'% (total_images, - len(image_paths), - len(labels))) - - # Stack all image attributes arrays in a single 2D array of dimensions - # (3, number of images) and group by label the indices of datapoins in the - # image attributes arrays. Explicitly convert label types from 'int' to 'str' - # to avoid implicit conversion during stacking with image_paths and file_ids - # which are 'str'. - labels_str = [str(label) for label in labels] - image_attrs = np.stack((image_paths, file_ids, labels_str)) - image_attrs_idx_by_label = {} - for index, label in enumerate(labels): - if label not in image_attrs_idx_by_label: - image_attrs_idx_by_label[label] = [] - image_attrs_idx_by_label[label].append(index) - - # Create subsets of image attributes by label, shuffle them separately and - # split each subset into TRAIN and VALIDATION splits based on the size of the - # validation split. - splits = { - _VALIDATION_SPLIT: [], - _TRAIN_SPLIT: [] - } - rs = np.random.RandomState(np.random.MT19937(np.random.SeedSequence(seed))) - for label, indexes in image_attrs_idx_by_label.items(): - # Create the subset for the current label. - image_attrs_label = image_attrs[:, indexes] - # Shuffle the current label subset. - image_attrs_label = _shuffle_by_columns(image_attrs_label, rs) - # Split the current label subset into TRAIN and VALIDATION splits and add - # each split to the list of all splits. - images_per_label = image_attrs_label.shape[1] - cutoff_idx = max(1, int(validation_split_size * images_per_label)) - splits[_VALIDATION_SPLIT].append(image_attrs_label[:, 0 : cutoff_idx]) - splits[_TRAIN_SPLIT].append(image_attrs_label[:, cutoff_idx : ]) - - # Concatenate all subsets of image attributes into TRAIN and VALIDATION splits - # and reshuffle them again to ensure variance of labels across batches. - validation_split = _shuffle_by_columns( - np.concatenate(splits[_VALIDATION_SPLIT], axis=1), rs) - train_split = _shuffle_by_columns( - np.concatenate(splits[_TRAIN_SPLIT], axis=1), rs) - - # Unstack the image attribute arrays in the TRAIN and VALIDATION splits and - # convert them back to lists. Convert labels back to 'int' from 'str' - # following the explicit type change from 'str' to 'int' for stacking. - return ( - { - _IMAGE_PATHS_KEY: validation_split[0, :].tolist(), - _FILE_IDS_KEY: validation_split[1, :].tolist(), - _LABELS_KEY: [int(label) for label in validation_split[2, :].tolist()] - }, { - _IMAGE_PATHS_KEY: train_split[0, :].tolist(), - _FILE_IDS_KEY: train_split[1, :].tolist(), - _LABELS_KEY: [int(label) for label in train_split[2, :].tolist()] - }) - - -def _build_train_tfrecord_dataset(csv_path, - clean_csv_path, - image_dir, - generate_train_validation_splits, - validation_split_size, - seed): - """Build a TFRecord dataset for the train split. - - Args: - csv_path: path to the train Google-landmark Dataset csv Data Sources files. - clean_csv_path: path to the Google-landmark Dataset v2 CSV Data Sources - files of the clean train dataset. - image_dir: directory that stores downloaded images. - generate_train_validation_splits: whether to split the test dataset into - TRAIN and VALIDATION splits. - validation_split_size: size of the VALIDATION split as a ratio of the train - dataset. Only used if 'generate_train_validation_splits' is True. - seed: seed to use for shuffling the dataset for reproducibility purposes. - Only used if 'generate_train_validation_splits' is True. - - Returns: - Nothing. After the function call, sharded TFRecord files are materialized. - Raises: - ValueError: if the size of the VALIDATION split is outside (0,1) when TRAIN - and VALIDATION splits need to be generated. - """ - # Make sure the size of the VALIDATION split is inside (0, 1) if we need to - # generate the TRAIN and VALIDATION splits. - if generate_train_validation_splits: - if validation_split_size <= 0 or validation_split_size >= 1: - raise ValueError('Invalid VALIDATION split size. Expected inside (0,1)' - 'but received %f.' % validation_split_size) - - if clean_csv_path: - # Load clean train images and labels and write the relabeling rules. - (image_paths, file_ids, labels, - relabeling_rules) = _get_clean_train_image_files_and_labels(clean_csv_path, - image_dir) - _write_relabeling_rules(relabeling_rules) - else: - # Load all train images. - image_paths, file_ids, labels = _get_all_image_files_and_labels( - _TRAIN_SPLIT, csv_path, image_dir) - - if generate_train_validation_splits: - # Generate the TRAIN and VALIDATION splits and write them to TFRecord. - validation_split, train_split = _build_train_and_validation_splits( - image_paths, file_ids, labels, validation_split_size, seed) - _write_tfrecord(_VALIDATION_SPLIT, - validation_split[_IMAGE_PATHS_KEY], - validation_split[_FILE_IDS_KEY], - validation_split[_LABELS_KEY]) - _write_tfrecord(_TRAIN_SPLIT, - train_split[_IMAGE_PATHS_KEY], - train_split[_FILE_IDS_KEY], - train_split[_LABELS_KEY]) - else: - # Write to TFRecord a single split, TRAIN. - _write_tfrecord(_TRAIN_SPLIT, image_paths, file_ids, labels) - - -def _build_test_tfrecord_dataset(csv_path, image_dir): - """Build a TFRecord dataset for the 'test' split. - - Args: - csv_path: path to the 'test' Google-landmark Dataset csv Data Sources files. - image_dir: directory that stores downloaded images. - - Returns: - Nothing. After the function call, sharded TFRecord files are materialized. - """ - image_paths, file_ids, labels = _get_all_image_files_and_labels( - _TEST_SPLIT, csv_path, image_dir) - _write_tfrecord(_TEST_SPLIT, image_paths, file_ids, labels) - - -def main(unused_argv): - _build_train_tfrecord_dataset(FLAGS.train_csv_path, - FLAGS.train_clean_csv_path, - FLAGS.train_directory, - FLAGS.generate_train_validation_splits, - FLAGS.validation_split_size, - FLAGS.seed) - if FLAGS.test_csv_path is not None: - _build_test_tfrecord_dataset(FLAGS.test_csv_path, FLAGS.test_directory) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/training/download_dataset.sh b/research/delf/delf/python/training/download_dataset.sh deleted file mode 100755 index ecbd905eccd..00000000000 --- a/research/delf/delf/python/training/download_dataset.sh +++ /dev/null @@ -1,161 +0,0 @@ -#!/bin/bash - -# Copyright 2020 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# This script downloads the Google Landmarks v2 dataset. To download the dataset -# run the script like in the following example: -# bash download_dataset.sh 500 100 20 -# -# The script takes the following parameters, in order: -# - number of image files from the TRAIN split to download (maximum 500) -# - number of image files from the INDEX split to download (maximum 100) -# - number of image files from the TEST split to download (maximum 20) - -image_files_train=$1 # Number of image files to download from the TRAIN split -image_files_index=$2 # Number of image files to download from the INDEX split -image_files_test=$3 # Number of image files to download from the TEST split - -splits=("train" "test" "index") -dataset_root_folder=gldv2_dataset - -metadata_url="https://s3.amazonaws.com/google-landmark/metadata" -ground_truth_url="https://s3.amazonaws.com/google-landmark/ground_truth" -csv_train=(${metadata_url}/train.csv ${metadata_url}/train_clean.csv ${metadata_url}/train_attribution.csv ${metadata_url}/train_label_to_category.csv) -csv_index=(${metadata_url}/index.csv ${metadata_url}/index_image_to_landmark.csv ${metadata_url}/index_label_to_category.csv) -csv_test=(${metadata_url}/test.csv ${ground_truth_url}/recognition_solution_v2.1.csv ${ground_truth_url}/retrieval_solution_v2.1.csv) - -images_tar_file_base_url="https://s3.amazonaws.com/google-landmark" -images_md5_file_base_url="https://s3.amazonaws.com/google-landmark/md5sum" -num_processes=6 - -make_folder() { - # Creates a folder and checks if it exists. Exits if folder creation fails. - local folder=$1 - if [ -d "${folder}" ]; then - echo "Folder ${folder} already exists. Skipping folder creation." - else - echo "Creating folder ${folder}." - if mkdir ${folder}; then - echo "Successfully created folder ${folder}." - else - echo "Failed to create folder ${folder}. Exiting." - exit 1 - fi - fi -} - -download_file() { - # Downloads a file from an URL into a specified folder. - local file_url=$1 - local folder=$2 - local file_path="${folder}/`basename ${file_url}`" - echo "Downloading file ${file_url} to folder ${folder}." - pushd . > /dev/null - cd ${folder} - curl -Os ${file_url} - popd > /dev/null -} - -validate_md5_checksum() { - # Validate the MD5 checksum of a downloaded file. - local content_file=$1 - local md5_file=$2 - echo "Checking MD5 checksum of file ${content_file} against ${md5_file}" - if [[ "${OSTYPE}" == "linux-gnu" ]]; then - content_md5=`md5sum ${content_file}` - elif [[ "${OSTYPE}" == "darwin"* ]]; then - content_md5=`md5 -r "${content_file}"` - fi - content_md5=`cut -d' ' -f1<<<"${content_md5}"` - expected_md5=`cut -d' ' -f1<<${max_idx}?${max_idx}:${curr_max_idx})) - for j in $(seq ${i} 1 ${last_idx}); do download_image_file "${split}" "${j}" "${split_folder}" & done - wait - done -} - -download_csv_files() { - # Downloads all medatada CSV files of a split. - local split=$1 - local split_folder=$2 - local csv_list="csv_${split}[*]" - for csv_file in ${!csv_list}; do - download_file "${csv_file}" "${split_folder}" - done -} - -download_split() { - # Downloads all artifacts, metadata CSV files and image files of a single split. - local split=$1 - local split_folder=${dataset_root_folder}/${split} - make_folder "${split_folder}" - download_csv_files "${split}" "${split_folder}" - download_image_files "${split}" "${split_folder}" -} - -download_all_splits() { - # Downloads all artifacts, metadata CSV files and image files of all splits. - make_folder "${dataset_root_folder}" - for split in "${splits[@]}"; do - download_split "$split" - done -} - -download_all_splits - -exit 0 diff --git a/research/delf/delf/python/training/global_features/README.md b/research/delf/delf/python/training/global_features/README.md deleted file mode 100644 index 4ca68316263..00000000000 --- a/research/delf/delf/python/training/global_features/README.md +++ /dev/null @@ -1,174 +0,0 @@ -## Global features: CNN Image Retrieval - - -This Python toolbox implements the training and testing of the approach described in the papers: - -[![Paper](http://img.shields.io/badge/paper-arXiv.2001.05027-B3181B.svg)](https://arxiv.org/abs/1711.02512) - -``` -"Fine-tuning CNN Image Retrieval with No Human Annotation", -Radenović F., Tolias G., Chum O., -TPAMI 2018 -``` - -[![Paper](http://img.shields.io/badge/paper-arXiv.2001.05027-B3181B.svg)](http://arxiv.org/abs/1604.02426) -``` -"CNN Image Retrieval Learns from BoW: Unsupervised Fine-Tuning with Hard Examples", -Radenović F., Tolias G., Chum O., -ECCV 2016 -``` - -Fine-tuned CNNs are used for global feature extraction with the goal of using -those for image retrieval. The networks are trained on the SfM120k -landmark images dataset. - - - -When initializing the network, one of the popular pre-trained architectures - for classification tasks (such as ResNet or VGG) is used as the network’s - backbone. The -fully connected layers of such architectures are discarded, resulting in a fully -convolutional backbone. Then, given an input image of the size [W × H × C], -where C is the number of channels, W and H are image width and height, -respectively; the output is a tensor X with dimensions [W' × H' × K], where -K is the number of feature maps in the last layer. Tensor X -can be considered as a set of the input image’s deep local features. For -deep convolutional features, the simple aggregation approach based on global -pooling arguably provides the best results. This method is fast, has a small -number of parameters, and a low risk of overfitting. Keeping this in mind, -we convert local features to a global descriptor vector using one of the -retrieval system’s global poolings (MAC, SPoC, or GeM). After this stage, -the feature vector is made up of the maximum activation per feature map -with dimensionality equal to K. The final output dimensionality for the most -common networks varies from 512 to 2048, making this image representation -relatively compact. - -Vectors that have been pooled are subsequently L2-normalized. The obtained - representation is then optionally passed through the fully connected -layers before being subjected to a -new L2 re-normalization. The finally produced image representation allows -comparing the resemblance of two images by simply using their inner product. - - -### Install DELF library - -To be able to use this code, please follow -[these instructions](../../../../INSTALL_INSTRUCTIONS.md) to properly install -the DELF library. - -### Usage - -
- Training
- - Navigate (```cd```) to the folder ```[DELF_ROOT/delf/python/training - /global_features].``` - Example training script is located in ```DELF_ROOT/delf/python/training/global_features/train.py```. - ``` - python3 train.py [--arch ARCH] [--batch_size N] [--data_root PATH] - [--debug] [--directory PATH] [--epochs N] [--gpu_id ID] - [--image_size SIZE] [--launch_tensorboard] [--loss LOSS] - [--loss_margin LM] [--lr LR] [--momentum M] [multiscale SCALES] - [--neg_num N] [--optimizer OPTIMIZER] [--pool POOL] [--pool_size N] - [--pretrained] [--precompute_whitening DATASET] [--resume] - [--query_size N] [--test_datasets DATASET] [--test_freq N] - [--test_whiten] [--training_dataset DATASET] [--update_every N] - [--validation_type TYPE] [--weight_decay N] [--whitening] - ``` - - For detailed explanation of the options run: - ``` - python3 train.py -helpfull - ``` - Standard training of our models was run with the following parameters: - ``` -python3 train.py \ ---directory="DESTINATION_PATH" \ ---gpu_ids='0' \ ---data_root="TRAINING_DATA_DIRECTORY" \ ---training_dataset='retrieval-SfM-120k' \ ---test_datasets='roxford5k,rparis6k' \ ---arch='ResNet101' \ ---pool='gem' \ ---whitening=True \ ---debug=True \ ---loss='triplet' \ ---loss_margin=0.85 \ ---optimizer='adam' \ ---lr=5e-7 --neg_num=3 --query_size=2000 \ ---pool_size=20000 --batch_size=5 \ ---image_size=1024 --epochs=100 --test_freq=5 \ ---multiscale='[1, 2**(1/2), 1/2**(1/2)]' -``` - - **Note**: Data and networks used for training and testing are automatically downloaded when using the example training - script (```DELF_ROOT/delf/python/training/global_features/train.py```). - -
- -
-Training logic flow
- -**Initialization phase** - -1. Checking if required datasets are downloaded and automatically download them (both test and train/val) if they are -not present in the data folder. -1. Setting up the logging and creating a logging/checkpoint directory. -1. Initialize model according to the user-provided parameters (architecture -/pooling/whitening/pretrained etc.). -1. Defining loss (contrastive/triplet) according to the user parameters. -1. Defining optimizer (Adam/SGD with learning rate/weight decay/momentum) according to the user parameters. -1. Initializing CheckpointManager and resuming from the latest checkpoint if the resume flag is set. -1. Launching Tensorboard if the flag is set. -1. Initializing training (and validation, if required) datasets. -1. Freezing BatchNorm weights update, since we we do training for one image at a time so the statistics would not be per batch, hence we choose freezing (i.e., using pretrained imagenet statistics). -1. Evaluating the network performance before training (on the test datasets). - -**Training phase** - -The main training loop (for the required number of epochs): -1. Finding the hard negative pairs in the dataset (using the forward pass through the model) -1. Creating the training dataset from generator which changes every epoch. Each - element in the dataset consists of 1 x Positive image, 1 x Query image - , N x Hard negative images (N is specified by the `num_neg` flag), an array - specifying the Positive (-1), Query (0), Negative (1) images. -1. Performing one training step and calculating the final epoch loss. -1. If validation is required, finding hard negatives in the validation set -, which has the same structure as the training set. Performing one validation - step and calculating the loss. -1. Evaluating on the test datasets every `test_freq` epochs. -1. Saving checkpoint (optimizer and the model weights). - -
- -## Exporting the Trained Model - -Assuming the training output, the TensorFlow checkpoint, is located in the -`--directory` path. The following code exports the model: -``` -python3 model/export_CNN_global_model.py \ - [--ckpt_path PATH] [--export_path PATH] [--input_scales_list LIST] - [--multi_scale_pool_type TYPE] [--normalize_global_descriptor BOOL] - [arch ARCHITECTURE] [pool POOLING] [whitening BOOL] -``` -*NOTE:* Path to the checkpoint must include .h5 file. - -## Testing the trained model -After the trained model has been exported, it can be used to extract global -features similarly as for the DELG model. Please follow -[these instructions](https://github.com/tensorflow/models/tree/master/research/delf/delf/python/training#testing-the-trained-model). - -After training the standard training setup for 100 epochs, the - following results are obtained on Roxford and RParis datasets under a single - -scale evaluation: -``` ->> roxford5k: mAP E: 74.88, M: 58.28, H: 30.4 ->> roxford5k: mP@k[1, 5, 10] E: [89.71 84.8 79.07], - M: [91.43 84.67 78.24], - H: [68.57 53.29 43.29] - ->> rparis6k: mAP E: 89.21, M: 73.69, H: 49.1 ->> rparis6k: mP@k[1, 5, 10] E: [98.57 97.43 95.57], - M: [98.57 99.14 98.14], - H: [94.29 90. 87.29] -``` \ No newline at end of file diff --git a/research/delf/delf/python/training/global_features/__init__.py b/research/delf/delf/python/training/global_features/__init__.py deleted file mode 100644 index cd947f3f090..00000000000 --- a/research/delf/delf/python/training/global_features/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Global model training.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function diff --git a/research/delf/delf/python/training/global_features/train.py b/research/delf/delf/python/training/global_features/train.py deleted file mode 100644 index 8558594f62d..00000000000 --- a/research/delf/delf/python/training/global_features/train.py +++ /dev/null @@ -1,362 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Training script for Global Features model.""" - -import math -import os - -from absl import app -from absl import flags -from absl import logging -import numpy as np -import tensorflow as tf -import tensorflow_addons as tfa - -from delf.python.datasets.sfm120k import dataset_download -from delf.python.datasets.sfm120k import sfm120k -from delf.python.training import global_features_utils -from delf.python.training import tensorboard_utils -from delf.python.training.global_features import train_utils -from delf.python.training.losses import ranking_losses -from delf.python.training.model import global_model - -_LOSS_NAMES = ['contrastive', 'triplet'] -_MODEL_NAMES = global_features_utils.get_standard_keras_models() -_OPTIMIZER_NAMES = ['sgd', 'adam'] -_POOL_NAMES = ['mac', 'spoc', 'gem'] -_PRECOMPUTE_WHITEN_NAMES = ['retrieval-SfM-30k', 'retrieval-SfM-120k'] -_TEST_DATASET_NAMES = ['roxford5k', 'rparis6k'] -_TRAINING_DATASET_NAMES = ['retrieval-SfM-120k'] -_VALIDATION_TYPES = ['standard', 'eccv2020'] - -FLAGS = flags.FLAGS - -flags.DEFINE_boolean('debug', False, 'Debug mode.') - -# Export directory, training and val datasets, test datasets. -flags.DEFINE_string('data_root', "data", - 'Absolute path to the folder containing training data.') -flags.DEFINE_string('directory', "data", - 'Destination where trained network should be saved.') -flags.DEFINE_enum('training_dataset', 'retrieval-SfM-120k', - _TRAINING_DATASET_NAMES, 'Training dataset: ' + - ' | '.join(_TRAINING_DATASET_NAMES) + '.') -flags.DEFINE_enum('validation_type', None, _VALIDATION_TYPES, - 'Type of the evaluation to use. Either `None`, `standard` ' - 'or `eccv2020`.') -flags.DEFINE_list('test_datasets', 'roxford5k,rparis6k', - 'Comma separated list of test datasets: ' + - ' | '.join(_TEST_DATASET_NAMES) + '.') -flags.DEFINE_enum('precompute_whitening', None, _PRECOMPUTE_WHITEN_NAMES, - 'Dataset used to learn whitening: ' + - ' | '.join(_PRECOMPUTE_WHITEN_NAMES) + '.') -flags.DEFINE_integer('test_freq', 5, - 'Run test evaluation every N epochs.') -flags.DEFINE_list('multiscale', [1.], - 'Use multiscale vectors for testing, ' + - ' examples: 1 | 1,1/2**(1/2),1/2 | 1,2**(1/2),1/2**(1/2)]. ' - 'Pass as a string of comma separated values.') - -# Network architecture and initialization options. -flags.DEFINE_enum('arch', 'ResNet101', _MODEL_NAMES, - 'Model architecture: ' + ' | '.join(_MODEL_NAMES) + '.') -flags.DEFINE_enum('pool', 'gem', _POOL_NAMES, - 'Pooling options: ' + ' | '.join(_POOL_NAMES) + '.') -flags.DEFINE_bool('whitening', False, - 'Whether to train model with learnable whitening (' - 'linear layer) after the pooling.') -flags.DEFINE_bool('pretrained', True, - 'Whether to initialize model with random weights (' - 'default: pretrained on imagenet).') -flags.DEFINE_enum('loss', 'contrastive', _LOSS_NAMES, - 'Training loss options: ' + ' | '.join(_LOSS_NAMES) + '.') -flags.DEFINE_float('loss_margin', 0.7, 'Loss margin.') - -# train/val options specific for image retrieval learning. -flags.DEFINE_integer('image_size', 1024, - 'Maximum size of longer image side used for training.') -flags.DEFINE_integer('neg_num', 5, 'Number of negative images per train/val ' - 'tuple.') -flags.DEFINE_integer('query_size', 2000, - 'Number of queries randomly drawn per one training epoch.') -flags.DEFINE_integer('pool_size', 20000, - 'Size of the pool for hard negative mining.') - -# Standard training/validation options. -flags.DEFINE_string('gpu_id', '0', 'GPU id used for training.') -flags.DEFINE_integer('epochs', 100, 'Number of total epochs to run.') -flags.DEFINE_integer('batch_size', 5, - 'Number of (q,p,n1,...,nN) tuples in a mini-batch.') -flags.DEFINE_integer('update_every', 1, - 'Update model weights every N batches, used to handle ' - 'relatively large batches, batch_size effectively ' - 'becomes update_every `x` batch_size.') -flags.DEFINE_enum('optimizer', 'adam', _OPTIMIZER_NAMES, - 'Optimizer options: ' + ' | '.join(_OPTIMIZER_NAMES) + '.') -flags.DEFINE_float('lr', 1e-6, 'Initial learning rate.') -flags.DEFINE_float('momentum', 0.9, 'Momentum.') -flags.DEFINE_float('weight_decay', 1e-6, 'Weight decay.') -flags.DEFINE_bool('resume', False, - 'Whether to start from the latest checkpoint in the logdir.') -flags.DEFINE_bool('launch_tensorboard', False, 'Whether to launch tensorboard.') - - -def main(argv): - if len(argv) > 1: - raise RuntimeError('Too many command-line arguments.') - - # Manually check if there are unknown test datasets and if the dataset - # ground truth files are downloaded. - for dataset in FLAGS.test_datasets: - if dataset not in _TEST_DATASET_NAMES: - raise ValueError('Unsupported or unknown test dataset: {}.'.format( - dataset)) - - test_data_config = os.path.join(FLAGS.data_root, - 'gnd_{}.pkl'.format(dataset)) - if not tf.io.gfile.exists(test_data_config): - raise ValueError( - '{} ground truth file at {} not found. Please download it ' - 'according to ' - 'the DELG instructions.'.format(dataset, FLAGS.data_root)) - - # Check if train dataset is downloaded and download it if not found. - dataset_download.download_train(FLAGS.data_root) - - # Creating model export directory if it does not exist. - model_directory = global_features_utils.create_model_directory( - FLAGS.training_dataset, FLAGS.arch, FLAGS.pool, FLAGS.whitening, - FLAGS.pretrained, FLAGS.loss, FLAGS.loss_margin, FLAGS.optimizer, - FLAGS.lr, FLAGS.weight_decay, FLAGS.neg_num, FLAGS.query_size, - FLAGS.pool_size, FLAGS.batch_size, FLAGS.update_every, - FLAGS.image_size, FLAGS.directory) - - # Setting up logging directory, same as where the model is stored. - logging.get_absl_handler().use_absl_log_file('absl_logging', model_directory) - - # Set cuda visible device. - os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu_id - global_features_utils.debug_and_log('>> Num GPUs Available: {}'.format( - len(tf.config.experimental.list_physical_devices('GPU'))), - FLAGS.debug) - - # Set random seeds. - tf.random.set_seed(0) - np.random.seed(0) - - # Initialize the model. - if FLAGS.pretrained: - global_features_utils.debug_and_log( - '>> Using pre-trained model \'{}\''.format(FLAGS.arch)) - else: - global_features_utils.debug_and_log( - '>> Using model from scratch (random weights) \'{}\'.'.format( - FLAGS.arch)) - - model_params = {'architecture': FLAGS.arch, 'pooling': FLAGS.pool, - 'whitening': FLAGS.whitening, 'pretrained': FLAGS.pretrained, - 'data_root': FLAGS.data_root} - model = global_model.GlobalFeatureNet(**model_params) - - # Freeze running mean and std in batch normalization layers. - # We do training one image at a time to improve memory requirements of - # the network; therefore, the computed statistics would not be per a - # batch. Instead, we choose freezing - setting the parameters of all - # batch norm layers in the network to non-trainable (i.e., using original - # imagenet statistics). - for layer in model.feature_extractor.layers: - if isinstance(layer, tf.keras.layers.BatchNormalization): - layer.trainable = False - - global_features_utils.debug_and_log('>> Network initialized.') - - global_features_utils.debug_and_log('>> Loss: {}.'.format(FLAGS.loss)) - # Define the loss function. - if FLAGS.loss == 'contrastive': - criterion = ranking_losses.ContrastiveLoss(margin=FLAGS.loss_margin) - elif FLAGS.loss == 'triplet': - criterion = ranking_losses.TripletLoss(margin=FLAGS.loss_margin) - else: - raise ValueError('Loss {} not available.'.format(FLAGS.loss)) - - # Defining parameters for the training. - # When pre-computing whitening, we run evaluation before the network training - # and the `start_epoch` is set to 0. In other cases, we start from epoch 1. - start_epoch = 1 - exp_decay = math.exp(-0.01) - decay_steps = FLAGS.query_size / FLAGS.batch_size - - # Define learning rate decay schedule. - lr_scheduler = tf.keras.optimizers.schedules.ExponentialDecay( - initial_learning_rate=FLAGS.lr, - decay_steps=decay_steps, - decay_rate=exp_decay) - - # Define the optimizer. - if FLAGS.optimizer == 'sgd': - opt = tfa.optimizers.extend_with_decoupled_weight_decay( - tf.keras.optimizers.SGD) - optimizer = opt(weight_decay=FLAGS.weight_decay, - learning_rate=lr_scheduler, momentum=FLAGS.momentum) - elif FLAGS.optimizer == 'adam': - opt = tfa.optimizers.extend_with_decoupled_weight_decay( - tf.keras.optimizers.Adam) - optimizer = opt(weight_decay=FLAGS.weight_decay, learning_rate=lr_scheduler) - else: - raise ValueError('Optimizer {} not available.'.format(FLAGS.optimizer)) - - # Initializing logging. - writer = tf.summary.create_file_writer(model_directory) - tf.summary.experimental.set_step(1) - - # Setting up the checkpoint manager. - checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model) - manager = tf.train.CheckpointManager( - checkpoint, - model_directory, - max_to_keep=10, - keep_checkpoint_every_n_hours=3) - if FLAGS.resume: - # Restores the checkpoint, if existing. - global_features_utils.debug_and_log('>> Continuing from a checkpoint.') - checkpoint.restore(manager.latest_checkpoint) - - # Launching tensorboard if required. - if FLAGS.launch_tensorboard: - tensorboard = tf.keras.callbacks.TensorBoard(model_directory) - tensorboard.set_model(model=model) - tensorboard_utils.launch_tensorboard(log_dir=model_directory) - - # Log flags used. - global_features_utils.debug_and_log('>> Running training script with:') - global_features_utils.debug_and_log('>> logdir = {}'.format(model_directory)) - - if FLAGS.training_dataset.startswith('retrieval-SfM-120k'): - train_dataset = sfm120k.CreateDataset( - data_root=FLAGS.data_root, - mode='train', - imsize=FLAGS.image_size, - num_negatives=FLAGS.neg_num, - num_queries=FLAGS.query_size, - pool_size=FLAGS.pool_size - ) - if FLAGS.validation_type is not None: - val_dataset = sfm120k.CreateDataset( - data_root=FLAGS.data_root, - mode='val', - imsize=FLAGS.image_size, - num_negatives=FLAGS.neg_num, - num_queries=float('Inf'), - pool_size=float('Inf'), - eccv2020=True if FLAGS.validation_type == 'eccv2020' else False - ) - - train_dataset_output_types = [tf.float32 for i in range(2 + FLAGS.neg_num)] - train_dataset_output_types.append(tf.int32) - - global_features_utils.debug_and_log( - '>> Training the {} network'.format(model_directory)) - global_features_utils.debug_and_log('>> GPU ids: {}'.format(FLAGS.gpu_id)) - - with writer.as_default(): - - # Precompute whitening if needed. - if FLAGS.precompute_whitening is not None: - epoch = 0 - train_utils.test_retrieval( - FLAGS.test_datasets, model, writer=writer, - epoch=epoch, model_directory=model_directory, - precompute_whitening=FLAGS.precompute_whitening, - data_root=FLAGS.data_root, - multiscale=FLAGS.multiscale) - - for epoch in range(start_epoch, FLAGS.epochs + 1): - # Set manual seeds per epoch. - np.random.seed(epoch) - tf.random.set_seed(epoch) - - # Find hard-negatives. - # While hard-positive examples are fixed during the whole training - # process and are randomly chosen from every epoch; hard-negatives - # depend on the current CNN parameters and are re-mined once per epoch. - avg_neg_distance = train_dataset.create_epoch_tuples(model) - - def _train_gen(): - return (inst for inst in train_dataset) - - train_loader = tf.data.Dataset.from_generator( - _train_gen, - output_types=tuple(train_dataset_output_types)) - - loss = train_utils.train_val_one_epoch( - loader=iter(train_loader), model=model, - criterion=criterion, optimizer=optimizer, epoch=epoch, - batch_size=FLAGS.batch_size, query_size=FLAGS.query_size, - neg_num=FLAGS.neg_num, update_every=FLAGS.update_every, - debug=FLAGS.debug) - - # Write a scalar summary. - tf.summary.scalar('train_epoch_loss', loss, step=epoch) - # Forces summary writer to send any buffered data to storage. - writer.flush() - - # Evaluate on validation set. - if FLAGS.validation_type is not None and (epoch % FLAGS.test_freq == 0 or - epoch == 1): - avg_neg_distance = val_dataset.create_epoch_tuples(model, - model_directory) - - def _val_gen(): - return (inst for inst in val_dataset) - - val_loader = tf.data.Dataset.from_generator( - _val_gen, output_types=tuple(train_dataset_output_types)) - - loss = train_utils.train_val_one_epoch( - loader=iter(val_loader), model=model, - criterion=criterion, optimizer=None, - epoch=epoch, train=False, batch_size=FLAGS.batch_size, - query_size=FLAGS.query_size, neg_num=FLAGS.neg_num, - update_every=FLAGS.update_every, debug=FLAGS.debug) - tf.summary.scalar('val_epoch_loss', loss, step=epoch) - writer.flush() - - # Evaluate on test datasets every test_freq epochs. - if epoch == 1 or epoch % FLAGS.test_freq == 0: - train_utils.test_retrieval( - FLAGS.test_datasets, model, writer=writer, epoch=epoch, - model_directory=model_directory, - precompute_whitening=FLAGS.precompute_whitening, - data_root=FLAGS.data_root, multiscale=FLAGS.multiscale) - - # Saving checkpoints and model weights. - try: - save_path = manager.save(checkpoint_number=epoch) - global_features_utils.debug_and_log( - 'Saved ({}) at {}'.format(epoch, save_path)) - - filename = os.path.join(model_directory, - 'checkpoint_epoch_{}.h5'.format(epoch)) - model.save_weights(filename, save_format='h5') - global_features_utils.debug_and_log( - 'Saved weights ({}) at {}'.format(epoch, filename)) - except Exception as ex: - global_features_utils.debug_and_log( - 'Could not save checkpoint: {}'.format(ex)) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/training/global_features/train_utils.py b/research/delf/delf/python/training/global_features/train_utils.py deleted file mode 100644 index 4eb5f80349a..00000000000 --- a/research/delf/delf/python/training/global_features/train_utils.py +++ /dev/null @@ -1,382 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Training utilities for Global Features model.""" - -import os -import pickle -import time - -import numpy as np -import tensorflow as tf - -from delf.python import whiten -from delf.python.datasets.revisited_op import dataset as test_dataset -from delf.python.datasets.sfm120k import sfm120k -from delf.python.training import global_features_utils -from delf.python.training.model import global_model - - -def _compute_loss_and_gradient(criterion, model, input, target, neg_num=5): - """Records gradients and loss through the network. - - Args: - criterion: Loss function. - model: Network for the gradient computation. - input: Tuple of query, positive and negative images. - target: List of indexes to specify queries (-1), positives(1), negatives(0). - neg_num: Integer, number of negatives per a tuple. - - Returns: - loss: Loss for the training step. - gradients: Computed gradients for the network trainable variables. - """ - # Record gradients and loss through the network. - with tf.GradientTape() as tape: - descriptors = tf.zeros(shape=(0, model.meta['outputdim']), dtype=tf.float32) - for img in input: - # Compute descriptor vector for each image. - o = model(tf.expand_dims(img, axis=0), training=True) - descriptors = tf.concat([descriptors, o], 0) - - queries = descriptors[target == -1] - positives = descriptors[target == 1] - negatives = descriptors[target == 0] - - negatives = tf.reshape(negatives, [tf.shape(queries)[0], neg_num, - model.meta['outputdim']]) - # Loss calculation. - loss = criterion(queries, positives, negatives) - - return loss, tape.gradient(loss, model.trainable_variables) - - -def train_val_one_epoch( - loader, model, criterion, optimizer, epoch, train=True, batch_size=5, - query_size=2000, neg_num=5, update_every=1, debug=False): - """Executes either training or validation step based on `train` value. - - Args: - loader: Training/validation iterable dataset. - model: Network to train/validate. - criterion: Loss function. - optimizer: Network optimizer. - epoch: Integer, epoch number. - train: Bool, specifies training or validation phase. - batch_size: Integer, number of (q,p,n1,...,nN) tuples in a mini-batch. - query_size: Integer, number of queries randomly drawn per one training - epoch. - neg_num: Integer, number of negatives per a tuple. - update_every: Integer, update model weights every N batches, used to - handle relatively large batches batch_size effectively becomes - update_every x batch_size. - debug: Bool, whether debug mode is used. - - Returns: - average_epoch_loss: Average epoch loss. - """ - batch_time = global_features_utils.AverageMeter() - data_time = global_features_utils.AverageMeter() - losses = global_features_utils.AverageMeter() - - # Retrieve all trainable variables we defined in the graph. - tvs = model.trainable_variables - accum_grads = [tf.zeros_like(tv.read_value()) for tv in tvs] - - end = time.time() - batch_num = 0 - print_frequency = 10 - all_batch_num = query_size // batch_size - state = 'Train' if train else 'Val' - global_features_utils.debug_and_log('>> {} step:'.format(state)) - - # For every batch in the dataset; Stops when all batches in the dataset have - # been processed. - while True: - data_time.update(time.time() - end) - - if train: - try: - # Train on one batch. - # Each image in the batch is loaded into memory consecutively. - for _ in range(batch_size): - # Because the images are not necessarily of the same size, we can't - # set the batch size with .batch(). - batch = loader.get_next() - input_tuple = batch[0:-1] - target_tuple = batch[-1] - - loss_value, grads = _compute_loss_and_gradient( - criterion, model, input_tuple, target_tuple, neg_num) - losses.update(loss_value) - # Accumulate gradients. - accum_grads += grads - - # Perform weight update if required. - if (batch_num + 1) % update_every == 0 or ( - batch_num + 1) == all_batch_num: - # Do one step for multiple batches. Accumulated gradients are - # used. - optimizer.apply_gradients( - zip(accum_grads, model.trainable_variables)) - accum_grads = [tf.zeros_like(tv.read_value()) for tv in tvs] - # We break when we run out of range, i.e., we exhausted all dataset - # images. - except tf.errors.OutOfRangeError: - break - - else: - # Validate one batch. - # We load full batch into memory. - input = [] - target = [] - try: - for _ in range(batch_size): - # Because the images are not necessarily of the same size, we can't - # set the batch size with .batch(). - batch = loader.get_next() - input.append(batch[0:-1]) - target.append(batch[-1]) - # We break when we run out of range, i.e., we exhausted all dataset - # images. - except tf.errors.OutOfRangeError: - break - - descriptors = tf.zeros(shape=(0, model.meta['outputdim']), - dtype=tf.float32) - - for input_tuple in input: - for img in input_tuple: - # Compute the global descriptor vector. - model_out = model(tf.expand_dims(img, axis=0), training=False) - descriptors = tf.concat([descriptors, model_out], 0) - - # No need to reduce memory consumption (no backward pass): - # Compute loss for the full batch. - queries = descriptors[target == -1] - positives = descriptors[target == 1] - negatives = descriptors[target == 0] - negatives = tf.reshape(negatives, [tf.shape(queries)[0], neg_num, - model.meta['outputdim']]) - loss = criterion(queries, positives, negatives) - - # Record loss. - losses.update(loss / batch_size, batch_size) - - # Measure elapsed time. - batch_time.update(time.time() - end) - end = time.time() - - # Record immediate loss and elapsed time. - if debug and ((batch_num + 1) % print_frequency == 0 or - batch_num == 0 or (batch_num + 1) == all_batch_num): - global_features_utils.debug_and_log( - '>> {0}: [{1} epoch][{2}/{3} batch]\t Time val: {' - 'batch_time.val:.3f} ' - '(Batch Time avg: {batch_time.avg:.3f})\t Data {' - 'data_time.val:.3f} (' - 'Time avg: {data_time.avg:.3f})\t Immediate loss value: {' - 'loss.val:.4f} ' - '(Loss avg: {loss.avg:.4f})'.format( - state, epoch, batch_num + 1, all_batch_num, - batch_time=batch_time, - data_time=data_time, loss=losses), debug=True, log=False) - batch_num += 1 - - return losses.avg - - -def test_retrieval(datasets, net, epoch, writer=None, model_directory=None, - precompute_whitening=None, data_root='data', multiscale=[1.], - test_image_size=1024): - """Testing step. - - Evaluates the network on the provided test datasets by computing single-scale - mAP for easy/medium/hard cases. If `writer` is specified, saves the mAP - values in a tensorboard supported format. - - Args: - datasets: List of dataset names for model testing (from - `_TEST_DATASET_NAMES`). - net: Network to evaluate. - epoch: Integer, epoch number. - writer: Tensorboard writer. - model_directory: String, path to the model directory. - precompute_whitening: Dataset used to learn whitening. If no - precomputation required, then `None`. Only 'retrieval-SfM-30k' and - 'retrieval-SfM-120k' datasets are supported for whitening pre-computation. - data_root: Absolute path to the data folder. - multiscale: List of scales for multiscale testing. - test_image_size: Integer, maximum size of the test images. - """ - global_features_utils.debug_and_log(">> Testing step:") - global_features_utils.debug_and_log( - '>> Evaluating network on test datasets...') - - # Precompute whitening. - if precompute_whitening is not None: - - # If whitening already precomputed, load it and skip the computations. - filename = os.path.join( - model_directory, 'learned_whitening_mP_{}_epoch.pkl'.format(epoch)) - filename_layer = os.path.join( - model_directory, - 'learned_whitening_layer_config_{}_epoch.pkl'.format( - epoch)) - - if tf.io.gfile.exists(filename): - global_features_utils.debug_and_log( - '>> {}: Whitening for this epoch is already precomputed. ' - 'Loading...'.format(precompute_whitening)) - with tf.io.gfile.GFile(filename, 'rb') as learned_whitening_file: - learned_whitening = pickle.load(learned_whitening_file) - - else: - start = time.time() - global_features_utils.debug_and_log( - '>> {}: Learning whitening...'.format(precompute_whitening)) - - # Loading db. - db_root = os.path.join(data_root, 'train', precompute_whitening) - ims_root = os.path.join(db_root, 'ims') - db_filename = os.path.join(db_root, - '{}-whiten.pkl'.format(precompute_whitening)) - with tf.io.gfile.GFile(db_filename, 'rb') as f: - db = pickle.load(f) - images = [sfm120k.id2filename(db['cids'][i], ims_root) for i in - range(len(db['cids']))] - - # Extract whitening vectors. - global_features_utils.debug_and_log( - '>> {}: Extracting...'.format(precompute_whitening)) - wvecs = global_model.extract_global_descriptors_from_list(net, images, - test_image_size) - - # Learning whitening. - global_features_utils.debug_and_log( - '>> {}: Learning...'.format(precompute_whitening)) - wvecs = wvecs.numpy() - mean_vector, projection_matrix = whiten.whitenlearn(wvecs, db['qidxs'], - db['pidxs']) - learned_whitening = {'m': mean_vector, 'P': projection_matrix} - - global_features_utils.debug_and_log( - '>> {}: Elapsed time: {}'.format(precompute_whitening, - global_features_utils.htime( - time.time() - start))) - # Save learned_whitening parameters for a later use. - with tf.io.gfile.GFile(filename, 'wb') as learned_whitening_file: - pickle.dump(learned_whitening, learned_whitening_file) - - # Saving whitening as a layer. - bias = -np.dot(mean_vector.T, projection_matrix.T) - whitening_layer = tf.keras.layers.Dense( - net.meta['outputdim'], - activation=None, - use_bias=True, - kernel_initializer=tf.keras.initializers.Constant( - projection_matrix.T), - bias_initializer=tf.keras.initializers.Constant(bias) - ) - with tf.io.gfile.GFile(filename_layer, 'wb') as learned_whitening_file: - pickle.dump(whitening_layer.get_config(), learned_whitening_file) - else: - learned_whitening = None - - # Evaluate on test datasets. - for dataset in datasets: - start = time.time() - - # Prepare config structure for the test dataset. - cfg = test_dataset.CreateConfigForTestDataset(dataset, - os.path.join(data_root)) - images = [cfg['im_fname'](cfg, i) for i in range(cfg['n'])] - qimages = [cfg['qim_fname'](cfg, i) for i in range(cfg['nq'])] - bounding_boxes = [tuple(cfg['gnd'][i]['bbx']) for i in range(cfg['nq'])] - - # Extract database and query vectors. - global_features_utils.debug_and_log( - '>> {}: Extracting database images...'.format(dataset)) - vecs = global_model.extract_global_descriptors_from_list( - net, images, test_image_size, scales=multiscale) - global_features_utils.debug_and_log( - '>> {}: Extracting query images...'.format(dataset)) - qvecs = global_model.extract_global_descriptors_from_list( - net, qimages, test_image_size, bounding_boxes, - scales=multiscale) - - global_features_utils.debug_and_log('>> {}: Evaluating...'.format(dataset)) - - # Convert the obtained descriptors to numpy. - vecs = vecs.numpy() - qvecs = qvecs.numpy() - - # Search, rank and print test set metrics. - _calculate_metrics_and_export_to_tensorboard(vecs, qvecs, dataset, cfg, - writer, epoch, whiten=False) - - if learned_whitening is not None: - # Whiten the vectors. - mean_vector = learned_whitening['m'] - projection_matrix = learned_whitening['P'] - vecs_lw = whiten.whitenapply(vecs, mean_vector, projection_matrix) - qvecs_lw = whiten.whitenapply(qvecs, mean_vector, projection_matrix) - - # Search, rank, and print. - _calculate_metrics_and_export_to_tensorboard( - vecs_lw, qvecs_lw, dataset, cfg, writer, epoch, whiten=True) - - global_features_utils.debug_and_log( - '>> {}: Elapsed time: {}'.format( - dataset, global_features_utils.htime(time.time() - start))) - - -def _calculate_metrics_and_export_to_tensorboard(vecs, qvecs, dataset, cfg, - writer, epoch, whiten=False): - """ - Calculates metrics and exports them to tensorboard. - - Args: - vecs: Numpy array dataset global descriptors. - qvecs: Numpy array query global descriptors. - dataset: String, one of `_TEST_DATASET_NAMES`. - cfg: Dataset configuration. - writer: Tensorboard writer. - epoch: Integer, epoch number. - whiten: Boolean, whether the metrics are with for whitening used as a - post-processing step. Affects the name of the extracted TensorBoard - metrics. - """ - # Search, rank and print test set metrics. - scores = np.dot(vecs.T, qvecs) - ranks = np.transpose(np.argsort(-scores, axis=0)) - - metrics = global_features_utils.compute_metrics_and_print(dataset, ranks, - cfg['gnd']) - # Save calculated metrics in a tensorboard format. - if writer: - if whiten: - metric_names = ['test_accuracy_whiten_{}_E'.format(dataset), - 'test_accuracy_whiten_{}_M'.format(dataset), - 'test_accuracy_whiten_{}_H'.format(dataset)] - else: - metric_names = ['test_accuracy_{}_E'.format(dataset), - 'test_accuracy_{}_M'.format(dataset), - 'test_accuracy_{}_H'.format(dataset)] - tf.summary.scalar(metric_names[0], metrics[0][0], step=epoch) - tf.summary.scalar(metric_names[1], metrics[1][0], step=epoch) - tf.summary.scalar(metric_names[2], metrics[2][0], step=epoch) - writer.flush() - return None diff --git a/research/delf/delf/python/training/global_features_utils.py b/research/delf/delf/python/training/global_features_utils.py deleted file mode 100644 index 273dabc46de..00000000000 --- a/research/delf/delf/python/training/global_features_utils.py +++ /dev/null @@ -1,221 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utilities for the global model training.""" - -import os - -from absl import logging - -import numpy as np -import tensorflow as tf - -from delf.python.datasets.revisited_op import dataset as revisited_dataset - - -class AverageMeter(): - """Computes and stores the average and current value of loss.""" - - def __init__(self): - """Initialization of the AverageMeter.""" - self.reset() - - def reset(self): - """Resets all the values.""" - self.val = 0 - self.avg = 0 - self.sum = 0 - self.count = 0 - - def update(self, val, n=1): - """Updates values in the AverageMeter. - Args: - val: Float, loss value. - n: Integer, number of instances. - """ - self.val = val - self.sum += val * n - self.count += n - self.avg = self.sum / self.count - - -def compute_metrics_and_print(dataset_name, - sorted_index_ids, - ground_truth, - desired_pr_ranks=None, - log=True): - """Computes and logs ground-truth metrics for Revisited datasets. - Args: - dataset_name: String, name of the dataset. - sorted_index_ids: Integer NumPy array of shape [#queries, #index_images]. - For each query, contains an array denoting the most relevant index images, - sorted from most to least relevant. - ground_truth: List containing ground-truth information for dataset. Each - entry is a dict corresponding to the ground-truth information for a query. - The dict has keys 'ok' and 'junk', mapping to a NumPy array of integers. - desired_pr_ranks: List of integers containing the desired precision/recall - ranks to be reported. E.g., if precision@1/recall@1 and - precision@10/recall@10 are desired, this should be set to [1, 10]. The - largest item should be <= #sorted_index_ids. Default: [1, 5, 10]. - log: Whether to log results using logging.info(). - Returns: - mAP: (metricsE, metricsM, metricsH) Tuple of the metrics for different - levels of complexity. Each metrics is a list containing: - mean_average_precision (float), mean_precisions (NumPy array of - floats, with shape [len(desired_pr_ranks)]), mean_recalls (NumPy array - of floats, with shape [len(desired_pr_ranks)]), average_precisions - (NumPy array of floats, with shape [#queries]), precisions (NumPy array of - floats, with shape [#queries, len(desired_pr_ranks)]), recalls (NumPy - array of floats, with shape [#queries, len(desired_pr_ranks)]). - Raises: - ValueError: If an unknown dataset name is provided as an argument. - """ - if dataset_name not in revisited_dataset.DATASET_NAMES: - raise ValueError('Unknown dataset: {}!'.format(dataset)) - - if desired_pr_ranks is None: - desired_pr_ranks = [1, 5, 10] - - (easy_ground_truth, medium_ground_truth, - hard_ground_truth) = revisited_dataset.ParseEasyMediumHardGroundTruth( - ground_truth) - - metrics_easy = revisited_dataset.ComputeMetrics(sorted_index_ids, - easy_ground_truth, - desired_pr_ranks) - metrics_medium = revisited_dataset.ComputeMetrics(sorted_index_ids, - medium_ground_truth, - desired_pr_ranks) - metrics_hard = revisited_dataset.ComputeMetrics(sorted_index_ids, - hard_ground_truth, - desired_pr_ranks) - - debug_and_log( - '>> {}: mAP E: {}, M: {}, H: {}'.format( - dataset_name, np.around(metrics_easy[0] * 100, decimals=2), - np.around(metrics_medium[0] * 100, decimals=2), - np.around(metrics_hard[0] * 100, decimals=2)), - log=log) - - debug_and_log( - '>> {}: mP@k{} E: {}, M: {}, H: {}'.format( - dataset_name, desired_pr_ranks, - np.around(metrics_easy[1] * 100, decimals=2), - np.around(metrics_medium[1] * 100, decimals=2), - np.around(metrics_hard[1] * 100, decimals=2)), - log=log) - - return metrics_easy, metrics_medium, metrics_hard - - -def htime(time_difference): - """Time formatting function. - Depending on the value of `time_difference` outputs time in an appropriate - time format. - Args: - time_difference: Float, time difference between the two events. - Returns: - time: String representing time in an appropriate time format. - """ - time_difference = round(time_difference) - - days = time_difference // 86400 - hours = time_difference // 3600 % 24 - minutes = time_difference // 60 % 60 - seconds = time_difference % 60 - - if days > 0: - return '{:d}d {:d}h {:d}m {:d}s'.format(days, hours, minutes, seconds) - if hours > 0: - return '{:d}h {:d}m {:d}s'.format(hours, minutes, seconds) - if minutes > 0: - return '{:d}m {:d}s'.format(minutes, seconds) - return '{:d}s'.format(seconds) - - -def debug_and_log(msg, debug=True, log=True, debug_on_the_same_line=False): - """Outputs `msg` to both stdout (if in the debug mode) and the log file. - Args: - msg: String, message to be logged. - debug: Bool, if True, will print `msg` to stdout. - log: Bool, if True, will redirect `msg` to the logfile. - debug_on_the_same_line: Bool, if True, will print `msg` to stdout without a - new line. When using this mode, logging to a logfile is disabled. - """ - if debug_on_the_same_line: - print(msg, end='') - return - if debug: - print(msg) - if log: - logging.info(msg) - - -def get_standard_keras_models(): - """Gets the standard keras model names. - Returns: - model_names: List, names of the standard keras models. - """ - model_names = sorted( - name for name in tf.keras.applications.__dict__ - if not name.startswith('__') and - callable(tf.keras.applications.__dict__[name])) - return model_names - - -def create_model_directory(training_dataset, arch, pool, whitening, pretrained, - loss, loss_margin, optimizer, lr, weight_decay, - neg_num, query_size, pool_size, batch_size, - update_every, image_size, directory): - """Based on the model parameters, creates the model directory. - If the model directory does not exist, the directory is created. - Args: - training_dataset: String, training dataset name. - arch: String, model architecture. - pool: String, pooling option. - whitening: Bool, whether the model is trained with global whitening. - pretrained: Bool, whether the model is initialized with the precomputed - weights. - loss: String, training loss type. - loss_margin: Float, loss margin. - optimizer: Sting, used optimizer. - lr: Float, initial learning rate. - weight_decay: Float, weight decay. - neg_num: Integer, Number of negative images per train/val tuple. - query_size: Integer, number of queries per one training epoch. - pool_size: Integer, size of the pool for hard negative mining. - batch_size: Integer, batch size. - update_every: Integer, frequency of the model weights update. - image_size: Integer, maximum size of longer image side used for training. - directory: String, destination where trained network should be saved. - Returns: - folder: String, path to the model folder. - """ - folder = '{}_{}_{}'.format(training_dataset, arch, pool) - if whitening: - folder += '_whiten' - if not pretrained: - folder += '_notpretrained' - folder += ('_{}_m{:.2f}_{}_lr{:.1e}_wd{:.1e}_nnum{}_qsize{}_psize{}_bsize{}' - '_uevery{}_imsize{}').format(loss, loss_margin, optimizer, lr, - weight_decay, neg_num, query_size, - pool_size, batch_size, update_every, - image_size) - - folder = os.path.join(directory, folder) - debug_and_log( - '>> Creating directory if does not exist:\n>> \'{}\''.format(folder)) - if not os.path.exists(folder): - os.makedirs(folder) - return folder diff --git a/research/delf/delf/python/training/install_delf.sh b/research/delf/delf/python/training/install_delf.sh deleted file mode 100755 index 5e54bf8005c..00000000000 --- a/research/delf/delf/python/training/install_delf.sh +++ /dev/null @@ -1,154 +0,0 @@ -#!/bin/bash - -# Copyright 2020 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# This script installs the DELF package along with its dependencies. To install -# the DELF package run the script like in the following example: -# bash install_delf.sh - -protoc_folder="protoc" -protoc_url="https://github.com/google/protobuf/releases/download/v3.3.0/protoc-3.3.0-linux-x86_64.zip" -tf_slim_git_repo="https://github.com/google-research/tf-slim.git" - -handle_exit_code() { - # Fail gracefully in case of an exit code different than 0. - exit_code=$1 - error_message=$2 - if [ ${exit_code} -ne 0 ]; then - echo "${error_message} Exiting." - exit 1 - fi -} - -install_tensorflow() { - # Install TensorFlow 2.2. - echo "Installing TensorFlow 2.2" - pip3 install --upgrade tensorflow==2.2.0 - pip3 install tensorflow-addons==0.11.2 - local exit_code=$? - handle_exit_code ${exit_code} "Unable to install Tensorflow 2.2." - echo "Installing TensorFlow 2.2 for GPU" - pip3 install --upgrade tensorflow-gpu==2.2.0 - local exit_code=$? - handle_exit_code ${exit_code} "Unable to install Tensorflow for GPU 2.2.0." -} - -install_tf_slim() { - # Install TF-Slim from source. - echo "Installing TF-Slim from source: ${git_repo}" - git clone -b v1.1.0 ${tf_slim_git_repo} - local exit_code=$? - handle_exit_code ${exit_code} "Unable to clone TF-Slim repository ${tf_slim_git_repo}." - pushd . > /dev/null - cd tf-slim - pip3 install . - popd > /dev/null - rm -rf tf-slim -} - -download_protoc() { - # Installs the Protobuf compiler protoc. - echo "Downloading Protobuf compiler from ${protoc_url}" - curl -L -Os ${protoc_url} - local exit_code=$? - handle_exit_code ${exit_code} "Unable to download Protobuf compiler from ${tf_slim_git_repo}." - - mkdir ${protoc_folder} - local protoc_archive=`basename ${protoc_url}` - unzip ${protoc_archive} -d ${protoc_folder} - local exit_code=$? - handle_exit_code ${exit_code} "Unable to unzip Protobuf compiler from ${protoc_archive}." - - rm ${protoc_archive} -} - -compile_delf_protos() { - # Compiles DELF protobufs from tensorflow/models/research/delf using the potoc compiler. - echo "Compiling DELF Protobufs" - PATH_TO_PROTOC="`pwd`/${protoc_folder}" - pushd . > /dev/null - cd ../../.. - ${PATH_TO_PROTOC}/bin/protoc delf/protos/*.proto --python_out=. - local exit_code=$? - handle_exit_code ${exit_code} "Unable to compile DELF Protobufs." - popd > /dev/null -} - -cleanup_protoc() { - # Removes the downloaded Protobuf compiler protoc after the installation of the DELF package. - echo "Cleaning up Protobuf compiler download" - rm -rf ${protoc_folder} -} - -install_python_libraries() { - # Installs Python libraries upon which the DELF package has dependencies. - echo "Installing matplotlib, numpy, scikit-image, scipy and python3-tk" - pip3 install matplotlib numpy scikit-image scipy - local exit_code=$? - handle_exit_code ${exit_code} "Unable to install at least one of: matplotlib numpy scikit-image scipy." - sudo apt-get -y install python3-tk - local exit_code=$? - handle_exit_code ${exit_code} "Unable to install python3-tk." -} - -install_object_detection() { - # Installs the object detection package from tensorflow/models/research. - echo "Installing object detection" - pushd . > /dev/null - cd ../../../.. - export PYTHONPATH=$PYTHONPATH:`pwd` - pip3 install . - local exit_code=$? - handle_exit_code ${exit_code} "Unable to install the object_detection package." - popd > /dev/null -} - -install_delf_package() { - # Installs the DELF package from tensorflow/models/research/delf/delf. - echo "Installing DELF package" - pushd . > /dev/null - cd ../../.. - pip3 install -e . - local exit_code=$? - handle_exit_code ${exit_code} "Unable to install the DELF package." - popd > /dev/null -} - -post_install_check() { - # Checks the DELF package has been successfully installed. - echo "Checking DELF package installation" - python3 -c 'import delf' - local exit_code=$? - handle_exit_code ${exit_code} "DELF package installation check failed." - echo "Installation successful." -} - -install_delf() { - # Orchestrates DELF package installation. - install_tensorflow - install_tf_slim - download_protoc - compile_delf_protos - cleanup_protoc - install_python_libraries - install_object_detection - install_delf_package - post_install_check -} - -install_delf - -exit 0 diff --git a/research/delf/delf/python/training/losses/__init__.py b/research/delf/delf/python/training/losses/__init__.py deleted file mode 100644 index 9064f503de1..00000000000 --- a/research/delf/delf/python/training/losses/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== \ No newline at end of file diff --git a/research/delf/delf/python/training/losses/ranking_losses.py b/research/delf/delf/python/training/losses/ranking_losses.py deleted file mode 100644 index fc7c2844790..00000000000 --- a/research/delf/delf/python/training/losses/ranking_losses.py +++ /dev/null @@ -1,175 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Ranking loss definitions.""" - -import tensorflow as tf - - -class ContrastiveLoss(tf.keras.losses.Loss): - """Contrastive Loss layer. - - Contrastive Loss layer allows to compute contrastive loss for a batch of - images. Implementation based on: https://arxiv.org/abs/1604.02426. - """ - - def __init__(self, margin=0.7, reduction=tf.keras.losses.Reduction.NONE): - """Initialization of Contrastive Loss layer. - - Args: - margin: Float contrastive loss margin. - reduction: Type of loss reduction. - """ - super(ContrastiveLoss, self).__init__(reduction) - self.margin = margin - # Parameter for numerical stability. - self.eps = 1e-6 - - def __call__(self, queries, positives, negatives): - """Invokes the Contrastive Loss instance. - - Args: - queries: [batch_size, dim] Anchor input tensor. - positives: [batch_size, dim] Positive sample input tensor. - negatives: [batch_size, num_neg, dim] Negative sample input tensor. - - Returns: - loss: Scalar tensor. - """ - return contrastive_loss( - queries, positives, negatives, margin=self.margin, eps=self.eps) - - -class TripletLoss(tf.keras.losses.Loss): - """Triplet Loss layer. - - Triplet Loss layer computes triplet loss for a batch of images. Triplet - loss tries to keep all queries closer to positives than to any negatives. - Margin is used to specify when a triplet has become too "easy" and we no - longer want to adjust the weights from it. Differently from the Contrastive - Loss, Triplet Loss uses squared distances when computing the loss. - Implementation based on: https://arxiv.org/abs/1511.07247. - """ - - def __init__(self, margin=0.1, reduction=tf.keras.losses.Reduction.NONE): - """Initialization of Triplet Loss layer. - - Args: - margin: Triplet loss margin. - reduction: Type of loss reduction. - """ - super(TripletLoss, self).__init__(reduction) - self.margin = margin - - def __call__(self, queries, positives, negatives): - """Invokes the Triplet Loss instance. - - Args: - queries: [batch_size, dim] Anchor input tensor. - positives: [batch_size, dim] Positive sample input tensor. - negatives: [batch_size, num_neg, dim] Negative sample input tensor. - - Returns: - loss: Scalar tensor. - """ - return triplet_loss(queries, positives, negatives, margin=self.margin) - - -def contrastive_loss(queries, positives, negatives, margin=0.7, eps=1e-6): - """Calculates Contrastive Loss. - - We expect the `queries`, `positives` and `negatives` to be normalized with - unit length for training stability. The contrastive loss directly - optimizes this distance by encouraging all positive distances to - approach 0, while keeping negative distances above a certain threshold. - - Args: - queries: [batch_size, dim] Anchor input tensor. - positives: [batch_size, dim] Positive sample input tensor. - negatives: [batch_size, num_neg, dim] Negative sample input tensor. - margin: Float contrastive loss loss margin. - eps: Float parameter for numerical stability. - - Returns: - loss: Scalar tensor. - """ - dim = tf.shape(queries)[1] - # Number of `queries`. - batch_size = tf.shape(queries)[0] - # Number of `positives`. - np = tf.shape(positives)[0] - # Number of `negatives`. - num_neg = tf.shape(negatives)[1] - - # Preparing negatives. - stacked_negatives = tf.reshape(negatives, [num_neg * batch_size, dim]) - - # Preparing queries for further loss calculation. - stacked_queries = tf.repeat(queries, num_neg + 1, axis=0) - positives_and_negatives = tf.concat([positives, stacked_negatives], axis=0) - - # Calculate an Euclidean norm for each pair of points. For any positive - # pair of data points this distance should be small, and for - # negative pair it should be large. - distances = tf.norm(stacked_queries - positives_and_negatives + eps, axis=1) - - positives_part = 0.5 * tf.pow(distances[:np], 2.0) - negatives_part = 0.5 * tf.pow( - tf.math.maximum(margin - distances[np:], 0), 2.0) - - # Final contrastive loss calculation. - loss = tf.reduce_sum(tf.concat([positives_part, negatives_part], 0)) - return loss - - -def triplet_loss(queries, positives, negatives, margin=0.1): - """Calculates Triplet Loss. - - Triplet loss tries to keep all queries closer to positives than to any - negatives. Differently from the Contrastive Loss, Triplet Loss uses squared - distances when computing the loss. - - Args: - queries: [batch_size, dim] Anchor input tensor. - positives: [batch_size, dim] Positive sample input tensor. - negatives: [batch_size, num_neg, dim] Negative sample input tensor. - margin: Float triplet loss loss margin. - - Returns: - loss: Scalar tensor. - """ - dim = tf.shape(queries)[1] - # Number of `queries`. - batch_size = tf.shape(queries)[0] - # Number of `negatives`. - num_neg = tf.shape(negatives)[1] - - # Preparing negatives. - stacked_negatives = tf.reshape(negatives, [num_neg * batch_size, dim]) - - # Preparing queries for further loss calculation. - stacked_queries = tf.repeat(queries, num_neg, axis=0) - - # Preparing positives for further loss calculation. - stacked_positives = tf.repeat(positives, num_neg, axis=0) - - # Computes *squared* distances. - distance_positives = tf.reduce_sum( - tf.square(stacked_queries - stacked_positives), axis=1) - distance_negatives = tf.reduce_sum( - tf.square(stacked_queries - stacked_negatives), axis=1) - # Final triplet loss calculation. - loss = tf.reduce_sum( - tf.maximum(distance_positives - distance_negatives + margin, 0.0)) - return loss diff --git a/research/delf/delf/python/training/losses/ranking_losses_test.py b/research/delf/delf/python/training/losses/ranking_losses_test.py deleted file mode 100644 index 8e540ca3ce6..00000000000 --- a/research/delf/delf/python/training/losses/ranking_losses_test.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for Ranking losses.""" - -import tensorflow as tf -from delf.python.training.losses import ranking_losses - - -class RankingLossesTest(tf.test.TestCase): - - def testContrastiveLoss(self): - # Testing the correct numeric value. - queries = tf.math.l2_normalize(tf.constant([[1.0, 2.0, -2.0]])) - positives = tf.math.l2_normalize(tf.constant([[-1.0, 2.0, 0.0]])) - negatives = tf.math.l2_normalize(tf.constant([[[-5.0, 0.0, 3.0]]])) - - result = ranking_losses.contrastive_loss(queries, positives, negatives, - margin=0.7, eps=1e-6) - exp_output = 0.55278635 - self.assertAllClose(exp_output, result) - - def testTripletLossZeroLoss(self): - # Testing the correct numeric value in case if query-positive distance is - # smaller than the query-negative distance. - queries = tf.math.l2_normalize(tf.constant([[1.0, 2.0, -2.0]])) - positives = tf.math.l2_normalize(tf.constant([[-1.0, 2.0, 0.0]])) - negatives = tf.math.l2_normalize(tf.constant([[[-5.0, 0.0, 3.0]]])) - - result = ranking_losses.triplet_loss(queries, positives, negatives, - margin=0.1) - exp_output = 0.0 - self.assertAllClose(exp_output, result) - - def testTripletLossNonZeroLoss(self): - # Testing the correct numeric value in case if query-positive distance is - # bigger than the query-negative distance. - queries = tf.math.l2_normalize(tf.constant([[1.0, 2.0, -2.0]])) - positives = tf.math.l2_normalize(tf.constant([[-5.0, 0.0, 3.0]])) - negatives = tf.math.l2_normalize(tf.constant([[[-1.0, 2.0, 0.0]]])) - - result = ranking_losses.triplet_loss(queries, positives, negatives, - margin=0.1) - exp_output = 2.2520838 - self.assertAllClose(exp_output, result) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/training/matched_images_demo.png b/research/delf/delf/python/training/matched_images_demo.png deleted file mode 100644 index b8a4cc9ac89..00000000000 Binary files a/research/delf/delf/python/training/matched_images_demo.png and /dev/null differ diff --git a/research/delf/delf/python/training/model/__init__.py b/research/delf/delf/python/training/model/__init__.py deleted file mode 100644 index 3fd7e87af35..00000000000 --- a/research/delf/delf/python/training/model/__init__.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2020 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""DELF model module, used for training and exporting.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=unused-import -from delf.python.training.model import delf_model -from delf.python.training.model import delg_model -from delf.python.training.model import export_model_utils -from delf.python.training.model import resnet50 -# pylint: enable=unused-import diff --git a/research/delf/delf/python/training/model/delf_model.py b/research/delf/delf/python/training/model/delf_model.py deleted file mode 100644 index 9d770ba4fd1..00000000000 --- a/research/delf/delf/python/training/model/delf_model.py +++ /dev/null @@ -1,240 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""DELF model implementation based on the following paper. - - Large-Scale Image Retrieval with Attentive Deep Local Features - https://arxiv.org/abs/1612.06321 -""" - -import tensorflow as tf - -from delf.python.training.model import resnet50 as resnet - -layers = tf.keras.layers -reg = tf.keras.regularizers - -_DECAY = 0.0001 - - -class AttentionModel(tf.keras.Model): - """Instantiates attention model. - - Uses two [kernel_size x kernel_size] convolutions and softplus as activation - to compute an attention map with the same resolution as the featuremap. - Features l2-normalized and aggregated using attention probabilites as weights. - The features (targets) to be aggregated can be the input featuremap, or a - different one with the same resolution. - """ - - def __init__(self, kernel_size=1, decay=_DECAY, name='attention'): - """Initialization of attention model. - - Args: - kernel_size: int, kernel size of convolutions. - decay: float, decay for l2 regularization of kernel weights. - name: str, name to identify model. - """ - super(AttentionModel, self).__init__(name=name) - - # First convolutional layer (called with relu activation). - self.conv1 = layers.Conv2D( - 512, - kernel_size, - kernel_regularizer=reg.l2(decay), - padding='same', - name='attn_conv1') - self.bn_conv1 = layers.BatchNormalization(axis=3, name='bn_conv1') - - # Second convolutional layer, with softplus activation. - self.conv2 = layers.Conv2D( - 1, - kernel_size, - kernel_regularizer=reg.l2(decay), - padding='same', - name='attn_conv2') - self.activation_layer = layers.Activation('softplus') - - def call(self, inputs, targets=None, training=True): - x = self.conv1(inputs) - x = self.bn_conv1(x, training=training) - x = tf.nn.relu(x) - - score = self.conv2(x) - prob = self.activation_layer(score) - - # Aggregate inputs if targets is None. - if targets is None: - targets = inputs - - # L2-normalize the featuremap before pooling. - targets = tf.nn.l2_normalize(targets, axis=-1) - feat = tf.reduce_mean(tf.multiply(targets, prob), [1, 2], keepdims=False) - - return feat, prob, score - - -class AutoencoderModel(tf.keras.Model): - """Instantiates the Keras Autoencoder model.""" - - def __init__(self, reduced_dimension, expand_dimension, kernel_size=1, - name='autoencoder'): - """Initialization of Autoencoder model. - - Args: - reduced_dimension: int, the output dimension of the autoencoder layer. - expand_dimension: int, the input dimension of the autoencoder layer. - kernel_size: int or tuple, height and width of the 2D convolution window. - name: str, name to identify model. - """ - super(AutoencoderModel, self).__init__(name=name) - self.conv1 = layers.Conv2D( - reduced_dimension, - kernel_size, - padding='same', - name='autoenc_conv1') - self.conv2 = layers.Conv2D( - expand_dimension, - kernel_size, - activation=tf.keras.activations.relu, - padding='same', - name='autoenc_conv2') - - def call(self, inputs): - dim_reduced_features = self.conv1(inputs) - dim_expanded_features = self.conv2(dim_reduced_features) - return dim_expanded_features, dim_reduced_features - - -class Delf(tf.keras.Model): - """Instantiates Keras DELF model using ResNet50 as backbone. - - This class implements the [DELF](https://arxiv.org/abs/1612.06321) model for - extracting local features from images. The backbone is a ResNet50 network - that extracts featuremaps from both conv_4 and conv_5 layers. Activations - from conv_4 are used to compute an attention map of the same resolution. - """ - - def __init__(self, - block3_strides=True, - name='DELF', - pooling='avg', - gem_power=3.0, - embedding_layer=False, - embedding_layer_dim=2048, - use_dim_reduction=False, - reduced_dimension=128, - dim_expand_channels=1024): - """Initialization of DELF model. - - Args: - block3_strides: bool, whether to add strides to the output of block3. - name: str, name to identify model. - pooling: str, pooling mode for global feature extraction; possible values - are 'None', 'avg', 'max', 'gem.' - gem_power: float, GeM power for GeM pooling. Only used if pooling == - 'gem'. - embedding_layer: bool, whether to create an embedding layer (FC whitening - layer). - embedding_layer_dim: int, size of the embedding layer. - use_dim_reduction: Whether to integrate dimensionality reduction layers. - If True, extra layers are added to reduce the dimensionality of the - extracted features. - reduced_dimension: int, only used if use_dim_reduction is True. The output - dimension of the autoencoder layer. - dim_expand_channels: int, only used if use_dim_reduction is True. The - number of channels of the backbone block used. Default value 1024 is the - number of channels of backbone block 'block3'. - """ - super(Delf, self).__init__(name=name) - - # Backbone using Keras ResNet50. - self.backbone = resnet.ResNet50( - 'channels_last', - name='backbone', - include_top=False, - pooling=pooling, - block3_strides=block3_strides, - average_pooling=False, - gem_power=gem_power, - embedding_layer=embedding_layer, - embedding_layer_dim=embedding_layer_dim) - - # Attention model. - self.attention = AttentionModel(name='attention') - - # Autoencoder model. - self._use_dim_reduction = use_dim_reduction - if self._use_dim_reduction: - self.autoencoder = AutoencoderModel(reduced_dimension, - dim_expand_channels, - name='autoencoder') - - def init_classifiers(self, num_classes, desc_classification=None): - """Define classifiers for training backbone and attention models.""" - self.num_classes = num_classes - if desc_classification is None: - self.desc_classification = layers.Dense( - num_classes, activation=None, kernel_regularizer=None, name='desc_fc') - else: - self.desc_classification = desc_classification - self.attn_classification = layers.Dense( - num_classes, activation=None, kernel_regularizer=None, name='att_fc') - - def global_and_local_forward_pass(self, images, training=True): - """Run a forward to calculate global descriptor and attention prelogits. - - Args: - images: Tensor containing the dataset on which to run the forward pass. - training: Indicator of wether the forward pass is running in training mode - or not. - - Returns: - Global descriptor prelogits, attention prelogits, attention scores, - backbone weights. - """ - backbone_blocks = {} - desc_prelogits = self.backbone.build_call( - images, intermediates_dict=backbone_blocks, training=training) - # Prevent gradients from propagating into the backbone. See DELG paper: - # https://arxiv.org/abs/2001.05027. - block3 = backbone_blocks['block3'] # pytype: disable=key-error - block3 = tf.stop_gradient(block3) - if self._use_dim_reduction: - (dim_expanded_features, dim_reduced_features) = self.autoencoder(block3) - attn_prelogits, attn_scores, _ = self.attention( - block3, - targets=dim_expanded_features, - training=training) - else: - attn_prelogits, attn_scores, _ = self.attention(block3, training=training) - dim_expanded_features = None - dim_reduced_features = None - return (desc_prelogits, attn_prelogits, attn_scores, backbone_blocks, - dim_expanded_features, dim_reduced_features) - - def build_call(self, input_image, training=True): - (global_feature, _, attn_scores, backbone_blocks, _, - dim_reduced_features) = self.global_and_local_forward_pass(input_image, - training) - if self._use_dim_reduction: - features = dim_reduced_features - else: - features = backbone_blocks['block3'] # pytype: disable=key-error - return global_feature, attn_scores, features - - def call(self, input_image, training=True): - _, probs, features = self.build_call(input_image, training=training) - return probs, features diff --git a/research/delf/delf/python/training/model/delf_model_test.py b/research/delf/delf/python/training/model/delf_model_test.py deleted file mode 100644 index 7d5ca44e0c1..00000000000 --- a/research/delf/delf/python/training/model/delf_model_test.py +++ /dev/null @@ -1,108 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the DELF model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl.testing import parameterized -import tensorflow as tf - -from delf.python.training.model import delf_model - - -class DelfTest(tf.test.TestCase, parameterized.TestCase): - - @parameterized.named_parameters( - ('block3_stridesTrue', True), - ('block3_stridesFalse', False), - ) - def test_build_model(self, block3_strides): - image_size = 321 - num_classes = 1000 - batch_size = 2 - input_shape = (batch_size, image_size, image_size, 3) - - model = delf_model.Delf(block3_strides=block3_strides, name='DELF') - model.init_classifiers(num_classes) - - images = tf.random.uniform(input_shape, minval=-1.0, maxval=1.0, seed=0) - blocks = {} - - # Get global feature by pooling block4 features. - desc_prelogits = model.backbone( - images, intermediates_dict=blocks, training=False) - desc_logits = model.desc_classification(desc_prelogits) - self.assertAllEqual(desc_prelogits.shape, (batch_size, 2048)) - self.assertAllEqual(desc_logits.shape, (batch_size, num_classes)) - - features = blocks['block3'] - attn_prelogits, _, _ = model.attention(features) - attn_logits = model.attn_classification(attn_prelogits) - self.assertAllEqual(attn_prelogits.shape, (batch_size, 1024)) - self.assertAllEqual(attn_logits.shape, (batch_size, num_classes)) - - @parameterized.named_parameters( - ('block3_stridesTrue', True), - ('block3_stridesFalse', False), - ) - def test_train_step(self, block3_strides): - - image_size = 321 - num_classes = 1000 - batch_size = 2 - clip_val = 10.0 - input_shape = (batch_size, image_size, image_size, 3) - - model = delf_model.Delf(block3_strides=block3_strides, name='DELF') - model.init_classifiers(num_classes) - - optimizer = tf.keras.optimizers.SGD(learning_rate=0.001, momentum=0.9) - - images = tf.random.uniform(input_shape, minval=0.0, maxval=1.0, seed=0) - labels = tf.random.uniform((batch_size,), - minval=0, - maxval=model.num_classes - 1, - dtype=tf.int64) - - loss_object = tf.keras.losses.SparseCategoricalCrossentropy( - from_logits=True, reduction=tf.keras.losses.Reduction.NONE) - - def compute_loss(labels, predictions): - per_example_loss = loss_object(labels, predictions) - return tf.nn.compute_average_loss( - per_example_loss, global_batch_size=batch_size) - - with tf.GradientTape() as gradient_tape: - (desc_prelogits, attn_prelogits, _, _, _, - _) = model.global_and_local_forward_pass(images) - # Calculate global loss by applying the descriptor classifier. - desc_logits = model.desc_classification(desc_prelogits) - desc_loss = compute_loss(labels, desc_logits) - # Calculate attention loss by applying the attention block classifier. - attn_logits = model.attn_classification(attn_prelogits) - attn_loss = compute_loss(labels, attn_logits) - # Cumulate global loss and attention loss and backpropagate through the - # descriptor layer and attention layer together. - total_loss = desc_loss + attn_loss - gradients = gradient_tape.gradient(total_loss, model.trainable_weights) - clipped, _ = tf.clip_by_global_norm(gradients, clip_norm=clip_val) - optimizer.apply_gradients(zip(clipped, model.trainable_weights)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/training/model/delg_model.py b/research/delf/delf/python/training/model/delg_model.py deleted file mode 100644 index a29161b0581..00000000000 --- a/research/delf/delf/python/training/model/delg_model.py +++ /dev/null @@ -1,178 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""DELG model implementation based on the following paper. - - Unifying Deep Local and Global Features for Image Search - https://arxiv.org/abs/2001.05027 -""" - -import functools -import math - -from absl import logging -import tensorflow as tf - -from delf.python.training.model import delf_model - -layers = tf.keras.layers - - -class Delg(delf_model.Delf): - """Instantiates Keras DELG model using ResNet50 as backbone. - - This class implements the [DELG](https://arxiv.org/abs/2001.05027) model for - extracting local and global features from images. The same attention layer - is trained as in the DELF model. In addition, the extraction of global - features is trained using GeMPooling, a FC whitening layer also called - "embedding layer" and ArcFace loss. - """ - - def __init__(self, - block3_strides=True, - name='DELG', - gem_power=3.0, - embedding_layer_dim=2048, - scale_factor_init=45.25, # sqrt(2048) - arcface_margin=0.1, - use_dim_reduction=False, - reduced_dimension=128, - dim_expand_channels=1024): - """Initialization of DELG model. - - Args: - block3_strides: bool, whether to add strides to the output of block3. - name: str, name to identify model. - gem_power: float, GeM power parameter. - embedding_layer_dim : int, dimension of the embedding layer. - scale_factor_init: float. - arcface_margin: float, ArcFace margin. - use_dim_reduction: Whether to integrate dimensionality reduction layers. - If True, extra layers are added to reduce the dimensionality of the - extracted features. - reduced_dimension: Only used if use_dim_reduction is True, the output - dimension of the dim_reduction layer. - dim_expand_channels: Only used if use_dim_reduction is True, the - number of channels of the backbone block used. Default value 1024 is the - number of channels of backbone block 'block3'. - """ - logging.info('Creating Delg model, gem_power %d, embedding_layer_dim %d', - gem_power, embedding_layer_dim) - super(Delg, self).__init__(block3_strides=block3_strides, - name=name, - pooling='gem', - gem_power=gem_power, - embedding_layer=True, - embedding_layer_dim=embedding_layer_dim, - use_dim_reduction=use_dim_reduction, - reduced_dimension=reduced_dimension, - dim_expand_channels=dim_expand_channels) - self._embedding_layer_dim = embedding_layer_dim - self._scale_factor_init = scale_factor_init - self._arcface_margin = arcface_margin - - def init_classifiers(self, num_classes): - """Define classifiers for training backbone and attention models.""" - logging.info('Initializing Delg backbone and attention models classifiers') - backbone_classifier_func = self._create_backbone_classifier(num_classes) - super(Delg, self).init_classifiers( - num_classes, - desc_classification=backbone_classifier_func) - - def _create_backbone_classifier(self, num_classes): - """Define the classifier for training the backbone model.""" - logging.info('Creating cosine classifier') - self.cosine_weights = tf.Variable( - initial_value=tf.initializers.GlorotUniform()( - shape=[self._embedding_layer_dim, num_classes]), - name='cosine_weights', - trainable=True) - self.scale_factor = tf.Variable(self._scale_factor_init, - name='scale_factor', - trainable=False) - classifier_func = functools.partial(cosine_classifier_logits, - num_classes=num_classes, - cosine_weights=self.cosine_weights, - scale_factor=self.scale_factor, - arcface_margin=self._arcface_margin) - classifier_func.trainable_weights = [self.cosine_weights] - return classifier_func - - -def cosine_classifier_logits(prelogits, - labels, - num_classes, - cosine_weights, - scale_factor, - arcface_margin, - training=True): - """Compute cosine classifier logits using ArFace margin. - - Args: - prelogits: float tensor of shape [batch_size, embedding_layer_dim]. - labels: int tensor of shape [batch_size]. - num_classes: int, number of classes. - cosine_weights: float tensor of shape [embedding_layer_dim, num_classes]. - scale_factor: float. - arcface_margin: float. Only used if greater than zero, and training is True. - training: bool, True if training, False if eval. - - Returns: - logits: Float tensor [batch_size, num_classes]. - """ - # L2-normalize prelogits, then obtain cosine similarity. - normalized_prelogits = tf.math.l2_normalize(prelogits, axis=1) - normalized_weights = tf.math.l2_normalize(cosine_weights, axis=0) - cosine_sim = tf.matmul(normalized_prelogits, normalized_weights) - - # Optionally use ArcFace margin. - if training and arcface_margin > 0.0: - # Reshape labels tensor from [batch_size] to [batch_size, num_classes]. - one_hot_labels = tf.one_hot(labels, num_classes) - cosine_sim = apply_arcface_margin(cosine_sim, - one_hot_labels, - arcface_margin) - - # Apply the scale factor to logits and return. - logits = scale_factor * cosine_sim - return logits - - -def apply_arcface_margin(cosine_sim, one_hot_labels, arcface_margin): - """Applies ArcFace margin to cosine similarity inputs. - - For a reference, see https://arxiv.org/pdf/1801.07698.pdf. ArFace margin is - applied to angles from correct classes (as per the ArcFace paper), and only - if they are <= (pi - margin). Otherwise, applying the margin may actually - improve their cosine similarity. - - Args: - cosine_sim: float tensor with shape [batch_size, num_classes]. - one_hot_labels: int tensor with shape [batch_size, num_classes]. - arcface_margin: float. - - Returns: - cosine_sim_with_margin: Float tensor with shape [batch_size, num_classes]. - """ - theta = tf.acos(cosine_sim, name='acos') - selected_labels = tf.where(tf.greater(theta, math.pi - arcface_margin), - tf.zeros_like(one_hot_labels), - one_hot_labels, - name='selected_labels') - final_theta = tf.where(tf.cast(selected_labels, dtype=tf.bool), - theta + arcface_margin, - theta, - name='final_theta') - return tf.cos(final_theta, name='cosine_sim_with_margin') diff --git a/research/delf/delf/python/training/model/delg_model_test.py b/research/delf/delf/python/training/model/delg_model_test.py deleted file mode 100644 index 3ac2ec5ad24..00000000000 --- a/research/delf/delf/python/training/model/delg_model_test.py +++ /dev/null @@ -1,151 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the DELG model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl.testing import parameterized -import tensorflow as tf - -from delf.python.training.model import delg_model - - -class DelgTest(tf.test.TestCase, parameterized.TestCase): - - @parameterized.named_parameters( - ('block3_stridesTrue', True), - ('block3_stridesFalse', False), - ) - def test_forward_pass(self, block3_strides): - image_size = 321 - num_classes = 1000 - batch_size = 2 - input_shape = (batch_size, image_size, image_size, 3) - local_feature_dim = 64 - feature_map_size = image_size // 16 # reduction factor for resnet50. - if block3_strides: - feature_map_size //= 2 - - model = delg_model.Delg(block3_strides=block3_strides, - use_dim_reduction=True, - reduced_dimension=local_feature_dim) - model.init_classifiers(num_classes) - - images = tf.random.uniform(input_shape, minval=-1.0, maxval=1.0, seed=0) - - # Run a complete forward pass of the model. - global_feature, attn_scores, local_features = model.build_call(images) - - self.assertAllEqual(global_feature.shape, (batch_size, 2048)) - self.assertAllEqual( - attn_scores.shape, - (batch_size, feature_map_size, feature_map_size, 1)) - self.assertAllEqual( - local_features.shape, - (batch_size, feature_map_size, feature_map_size, local_feature_dim)) - - @parameterized.named_parameters( - ('block3_stridesTrue', True), - ('block3_stridesFalse', False), - ) - def test_build_model(self, block3_strides): - image_size = 321 - num_classes = 1000 - batch_size = 2 - input_shape = (batch_size, image_size, image_size, 3) - - model = delg_model.Delg( - block3_strides=block3_strides, - use_dim_reduction=True) - model.init_classifiers(num_classes) - - images = tf.random.uniform(input_shape, minval=-1.0, maxval=1.0, seed=0) - labels = tf.random.uniform((batch_size,), - minval=0, - maxval=model.num_classes - 1, - dtype=tf.int64) - blocks = {} - - desc_prelogits = model.backbone( - images, intermediates_dict=blocks, training=False) - desc_logits = model.desc_classification(desc_prelogits, labels) - self.assertAllEqual(desc_prelogits.shape, (batch_size, 2048)) - self.assertAllEqual(desc_logits.shape, (batch_size, num_classes)) - - features = blocks['block3'] - attn_prelogits, _, _ = model.attention(features) - attn_logits = model.attn_classification(attn_prelogits) - self.assertAllEqual(attn_prelogits.shape, (batch_size, 1024)) - self.assertAllEqual(attn_logits.shape, (batch_size, num_classes)) - - @parameterized.named_parameters( - ('block3_stridesTrue', True), - ('block3_stridesFalse', False), - ) - def test_train_step(self, block3_strides): - image_size = 321 - num_classes = 1000 - batch_size = 2 - clip_val = 10.0 - input_shape = (batch_size, image_size, image_size, 3) - - model = delg_model.Delg( - block3_strides=block3_strides, - use_dim_reduction=True) - model.init_classifiers(num_classes) - - optimizer = tf.keras.optimizers.SGD(learning_rate=0.001, momentum=0.9) - - images = tf.random.uniform(input_shape, minval=0.0, maxval=1.0, seed=0) - labels = tf.random.uniform((batch_size,), - minval=0, - maxval=model.num_classes - 1, - dtype=tf.int64) - - loss_object = tf.keras.losses.SparseCategoricalCrossentropy( - from_logits=True, reduction=tf.keras.losses.Reduction.NONE) - - def compute_loss(labels, predictions): - per_example_loss = loss_object(labels, predictions) - return tf.nn.compute_average_loss( - per_example_loss, global_batch_size=batch_size) - - with tf.GradientTape() as gradient_tape: - (desc_prelogits, attn_prelogits, _, backbone_blocks, - dim_expanded_features, _) = model.global_and_local_forward_pass(images) - # Calculate global loss by applying the descriptor classifier. - desc_logits = model.desc_classification(desc_prelogits, labels) - desc_loss = compute_loss(labels, desc_logits) - # Calculate attention loss by applying the attention block classifier. - attn_logits = model.attn_classification(attn_prelogits) - attn_loss = compute_loss(labels, attn_logits) - # Calculate reconstruction loss between the attention prelogits and the - # backbone. - block3 = tf.stop_gradient(backbone_blocks['block3']) - reconstruction_loss = tf.math.reduce_mean( - tf.keras.losses.MSE(block3, dim_expanded_features)) - # Cumulate global loss and attention loss and backpropagate through the - # descriptor layer and attention layer together. - total_loss = desc_loss + attn_loss + reconstruction_loss - gradients = gradient_tape.gradient(total_loss, model.trainable_weights) - clipped, _ = tf.clip_by_global_norm(gradients, clip_norm=clip_val) - optimizer.apply_gradients(zip(clipped, model.trainable_weights)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/training/model/export_CNN_global.py b/research/delf/delf/python/training/model/export_CNN_global.py deleted file mode 100644 index efdd1fe833d..00000000000 --- a/research/delf/delf/python/training/model/export_CNN_global.py +++ /dev/null @@ -1,173 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Export global CNN feature tensorflow inference model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import app -from absl import flags -import tensorflow as tf - -from delf.python.training.model import global_model -from delf.python.training.model import export_model_utils - -FLAGS = flags.FLAGS - -flags.DEFINE_string('ckpt_path', None, help='Path to saved checkpoint.') -flags.DEFINE_string('export_path', None, - help='Path where model will be exported.') -flags.DEFINE_list( - 'input_scales_list', None, - 'Optional input image scales to use. If None (default), an input ' - 'end-point ' - '"input_scales" is added for the exported model. If not None, the ' - 'specified list of floats will be hard-coded as the desired input ' - 'scales.') -flags.DEFINE_enum( - 'multi_scale_pool_type', 'None', ['None', 'average', 'sum'], - "If 'None' (default), the model is exported with an output end-point " - "'global_descriptors', where the global descriptor for each scale is " - "returned separately. If not 'None', the global descriptor of each " - "scale is" - ' pooled and a 1D global descriptor is returned, with output end-point ' - "'global_descriptor'.") -flags.DEFINE_boolean('normalize_global_descriptor', False, - 'If True, L2-normalizes global descriptor.') -# Network architecture and initialization options. -flags.DEFINE_string('arch', 'ResNet101', - 'model architecture (default: ResNet101)') -flags.DEFINE_string('pool', 'gem', 'pooling options (default: gem)') -flags.DEFINE_boolean('whitening', False, - 'train model with learnable whitening (linear layer) ' - 'after the pooling') - - -def _NormalizeImages(images, *args): - """Normalize pixel values in image. - - Args: - images: `Tensor`, images to normalize. - - Returns: - normalized_images: `Tensor`, normalized images. - """ - tf.keras.applications.imagenet_utils.preprocess_input(images, mode='caffe') - return images - - -class _ExtractModule(tf.Module): - """Helper module to build and save global feature model.""" - - def __init__(self, - multi_scale_pool_type='None', - normalize_global_descriptor=False, - input_scales_tensor=None): - """Initialization of global feature model. - Args: - multi_scale_pool_type: Type of multi-scale pooling to perform. - normalize_global_descriptor: Whether to L2-normalize global - descriptor. - input_scales_tensor: If None, the exported function to be used - should be ExtractFeatures, where an input end-point "input_scales" is - added for the exported model. If not None, the specified 1D tensor of - floats will be hard-coded as the desired input scales, in conjunction - with ExtractFeaturesFixedScales. - """ - self._multi_scale_pool_type = multi_scale_pool_type - self._normalize_global_descriptor = normalize_global_descriptor - if input_scales_tensor is None: - self._input_scales_tensor = [] - else: - self._input_scales_tensor = input_scales_tensor - - self._model = global_model.GlobalFeatureNet( - FLAGS.arch, FLAGS.pool, FLAGS.whitening, pretrained=False) - - def LoadWeights(self, checkpoint_path): - self._model.load_weights(checkpoint_path) - - @tf.function(input_signature=[ - tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, - name='input_image'), - tf.TensorSpec(shape=[None], dtype=tf.float32, name='input_scales'), - tf.TensorSpec(shape=[None], dtype=tf.int32, - name='input_global_scales_ind') - ]) - def ExtractFeatures(self, input_image, input_scales, - input_global_scales_ind): - extracted_features = export_model_utils.ExtractGlobalFeatures( - input_image, - input_scales, - input_global_scales_ind, - lambda x: self._model(x, training=False), - multi_scale_pool_type=self._multi_scale_pool_type, - normalize_global_descriptor=self._normalize_global_descriptor, - normalization_function=_NormalizeImages()) - - named_output_tensors = {} - named_output_tensors['global_descriptors'] = tf.identity( - extracted_features, name='global_descriptors') - return named_output_tensors - - @tf.function(input_signature=[ - tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image') - ]) - def ExtractFeaturesFixedScales(self, input_image): - return self.ExtractFeatures(input_image, self._input_scales_tensor, - tf.range(tf.size(self._input_scales_tensor))) - - -def main(argv): - if len(argv) > 1: - raise app.UsageError('Too many command-line arguments.') - - export_path = FLAGS.export_path - if os.path.exists(export_path): - raise ValueError('export_path %s already exists.' % export_path) - - if FLAGS.input_scales_list is None: - input_scales_tensor = None - else: - input_scales_tensor = tf.constant( - [float(s) for s in FLAGS.input_scales_list], - dtype=tf.float32, - shape=[len(FLAGS.input_scales_list)], - name='input_scales') - module = _ExtractModule(FLAGS.multi_scale_pool_type, - FLAGS.normalize_global_descriptor, - input_scales_tensor) - - # Load the weights. - checkpoint_path = FLAGS.ckpt_path - module.LoadWeights(checkpoint_path) - print('Checkpoint loaded from ', checkpoint_path) - - # Save the module. - if FLAGS.input_scales_list is None: - served_function = module.ExtractFeatures - else: - served_function = module.ExtractFeaturesFixedScales - - tf.saved_model.save( - module, export_path, signatures={'serving_default': served_function}) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/training/model/export_global_model.py b/research/delf/delf/python/training/model/export_global_model.py deleted file mode 100644 index e5f9128a0bf..00000000000 --- a/research/delf/delf/python/training/model/export_global_model.py +++ /dev/null @@ -1,183 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Export global feature tensorflow inference model. - -The exported model may leverage image pyramids for multi-scale processing. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import app -from absl import flags -import tensorflow as tf - -from delf.python.training.model import delf_model -from delf.python.training.model import delg_model -from delf.python.training.model import export_model_utils - -FLAGS = flags.FLAGS - -flags.DEFINE_string('ckpt_path', '/tmp/delf-logdir/delf-weights', - 'Path to saved checkpoint.') -flags.DEFINE_string('export_path', None, 'Path where model will be exported.') -flags.DEFINE_list( - 'input_scales_list', None, - 'Optional input image scales to use. If None (default), an input end-point ' - '"input_scales" is added for the exported model. If not None, the ' - 'specified list of floats will be hard-coded as the desired input scales.') -flags.DEFINE_enum( - 'multi_scale_pool_type', 'None', ['None', 'average', 'sum'], - "If 'None' (default), the model is exported with an output end-point " - "'global_descriptors', where the global descriptor for each scale is " - "returned separately. If not 'None', the global descriptor of each scale is" - ' pooled and a 1D global descriptor is returned, with output end-point ' - "'global_descriptor'.") -flags.DEFINE_boolean('normalize_global_descriptor', False, - 'If True, L2-normalizes global descriptor.') -flags.DEFINE_boolean('delg_global_features', False, - 'Whether the model uses a DELG-like global feature head.') -flags.DEFINE_float( - 'delg_gem_power', 3.0, - 'Power for Generalized Mean pooling. Used only if --delg_global_features' - 'is present.') -flags.DEFINE_integer( - 'delg_embedding_layer_dim', 2048, - 'Size of the FC whitening layer (embedding layer). Used only if' - '--delg_global_features is present.') - - -class _ExtractModule(tf.Module): - """Helper module to build and save global feature model.""" - - def __init__(self, - multi_scale_pool_type='None', - normalize_global_descriptor=False, - input_scales_tensor=None, - delg_global_features=False, - delg_gem_power=3.0, - delg_embedding_layer_dim=2048): - """Initialization of global feature model. - - Args: - multi_scale_pool_type: Type of multi-scale pooling to perform. - normalize_global_descriptor: Whether to L2-normalize global descriptor. - input_scales_tensor: If None, the exported function to be used should be - ExtractFeatures, where an input end-point "input_scales" is added for - the exported model. If not None, the specified 1D tensor of floats will - be hard-coded as the desired input scales, in conjunction with - ExtractFeaturesFixedScales. - delg_global_features: Whether the model uses a DELG-like global feature - head. - delg_gem_power: Power for Generalized Mean pooling in the DELG model. Used - only if 'delg_global_features' is True. - delg_embedding_layer_dim: Size of the FC whitening layer (embedding - layer). Used only if 'delg_global_features' is True. - """ - self._multi_scale_pool_type = multi_scale_pool_type - self._normalize_global_descriptor = normalize_global_descriptor - if input_scales_tensor is None: - self._input_scales_tensor = [] - else: - self._input_scales_tensor = input_scales_tensor - - # Setup the DELF model for extraction. - if delg_global_features: - self._model = delg_model.Delg( - block3_strides=False, - name='DELG', - gem_power=delg_gem_power, - embedding_layer_dim=delg_embedding_layer_dim) - else: - self._model = delf_model.Delf(block3_strides=False, name='DELF') - - def LoadWeights(self, checkpoint_path): - self._model.load_weights(checkpoint_path) - - @tf.function(input_signature=[ - tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image'), - tf.TensorSpec(shape=[None], dtype=tf.float32, name='input_scales'), - tf.TensorSpec( - shape=[None], dtype=tf.int32, name='input_global_scales_ind') - ]) - def ExtractFeatures(self, input_image, input_scales, input_global_scales_ind): - extracted_features = export_model_utils.ExtractGlobalFeatures( - input_image, - input_scales, - input_global_scales_ind, - lambda x: self._model.backbone.build_call(x, training=False), - multi_scale_pool_type=self._multi_scale_pool_type, - normalize_global_descriptor=self._normalize_global_descriptor) - - named_output_tensors = {} - if self._multi_scale_pool_type == 'None': - named_output_tensors['global_descriptors'] = tf.identity( - extracted_features, name='global_descriptors') - else: - named_output_tensors['global_descriptor'] = tf.identity( - extracted_features, name='global_descriptor') - - return named_output_tensors - - @tf.function(input_signature=[ - tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image') - ]) - def ExtractFeaturesFixedScales(self, input_image): - return self.ExtractFeatures(input_image, self._input_scales_tensor, - tf.range(tf.size(self._input_scales_tensor))) - - -def main(argv): - if len(argv) > 1: - raise app.UsageError('Too many command-line arguments.') - - export_path = FLAGS.export_path - if os.path.exists(export_path): - raise ValueError('export_path %s already exists.' % export_path) - - if FLAGS.input_scales_list is None: - input_scales_tensor = None - else: - input_scales_tensor = tf.constant( - [float(s) for s in FLAGS.input_scales_list], - dtype=tf.float32, - shape=[len(FLAGS.input_scales_list)], - name='input_scales') - module = _ExtractModule(FLAGS.multi_scale_pool_type, - FLAGS.normalize_global_descriptor, - input_scales_tensor, FLAGS.delg_global_features, - FLAGS.delg_gem_power, FLAGS.delg_embedding_layer_dim) - - # Load the weights. - checkpoint_path = FLAGS.ckpt_path - module.LoadWeights(checkpoint_path) - print('Checkpoint loaded from ', checkpoint_path) - - # Save the module - if FLAGS.input_scales_list is None: - served_function = module.ExtractFeatures - else: - served_function = module.ExtractFeaturesFixedScales - - tf.saved_model.save( - module, export_path, signatures={'serving_default': served_function}) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/training/model/export_local_and_global_model.py b/research/delf/delf/python/training/model/export_local_and_global_model.py deleted file mode 100644 index a6cee584f87..00000000000 --- a/research/delf/delf/python/training/model/export_local_and_global_model.py +++ /dev/null @@ -1,170 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Export DELG tensorflow inference model. - -The exported model can be used to jointly extract local and global features. It -may use an image pyramid for multi-scale processing, and will include receptive -field calculation and keypoint selection for the local feature head. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import app -from absl import flags -import tensorflow as tf - -from delf.python.training.model import delf_model -from delf.python.training.model import delg_model -from delf.python.training.model import export_model_utils - -FLAGS = flags.FLAGS - -flags.DEFINE_string( - 'ckpt_path', '/tmp/delf-logdir/delf-weights', 'Path to saved checkpoint.') -flags.DEFINE_string('export_path', None, 'Path where model will be exported.') -flags.DEFINE_boolean( - 'delg_global_features', True, - 'Whether the model uses a DELG-like global feature head.') -flags.DEFINE_float( - 'delg_gem_power', 3.0, - 'Power for Generalized Mean pooling. Used only if --delg_global_features' - 'is present.') -flags.DEFINE_integer( - 'delg_embedding_layer_dim', 2048, - 'Size of the FC whitening layer (embedding layer). Used only if' - '--delg_global_features is present.') -flags.DEFINE_boolean( - 'block3_strides', True, - 'Whether to apply strides after block3, used for local feature head.') -flags.DEFINE_float( - 'iou', 1.0, 'IOU for non-max suppression used in local feature head.') -flags.DEFINE_boolean( - 'use_autoencoder', True, - 'Whether the exported model should use an autoencoder.') -flags.DEFINE_float( - 'autoencoder_dimensions', 128, - 'Number of dimensions of the autoencoder. Used only if' - 'use_autoencoder=True.') -flags.DEFINE_float( - 'local_feature_map_channels', 1024, - 'Number of channels at backbone layer used for local feature extraction. ' - 'Default value 1024 is the number of channels of block3. Used only if' - 'use_autoencoder=True.') - - -class _ExtractModule(tf.Module): - """Helper module to build and save DELG model.""" - - def __init__(self, - delg_global_features=True, - delg_gem_power=3.0, - delg_embedding_layer_dim=2048, - block3_strides=True, - iou=1.0): - """Initialization of DELG model. - - Args: - delg_global_features: Whether the model uses a DELG-like global feature - head. - delg_gem_power: Power for Generalized Mean pooling in the DELG model. Used - only if 'delg_global_features' is True. - delg_embedding_layer_dim: Size of the FC whitening layer (embedding - layer). Used only if 'delg_global_features' is True. - block3_strides: bool, whether to add strides to the output of block3. - iou: IOU for non-max suppression. - """ - self._stride_factor = 2.0 if block3_strides else 1.0 - self._iou = iou - - # Setup the DELG model for extraction. - if delg_global_features: - self._model = delg_model.Delg( - block3_strides=block3_strides, - name='DELG', - gem_power=delg_gem_power, - embedding_layer_dim=delg_embedding_layer_dim, - use_dim_reduction=FLAGS.use_autoencoder, - reduced_dimension=FLAGS.autoencoder_dimensions, - dim_expand_channels=FLAGS.local_feature_map_channels) - else: - self._model = delf_model.Delf( - block3_strides=block3_strides, - name='DELF', - use_dim_reduction=FLAGS.use_autoencoder, - reduced_dimension=FLAGS.autoencoder_dimensions, - dim_expand_channels=FLAGS.local_feature_map_channels) - - def LoadWeights(self, checkpoint_path): - self._model.load_weights(checkpoint_path) - - @tf.function(input_signature=[ - tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image'), - tf.TensorSpec(shape=[None], dtype=tf.float32, name='input_scales'), - tf.TensorSpec(shape=(), dtype=tf.int32, name='input_max_feature_num'), - tf.TensorSpec(shape=(), dtype=tf.float32, name='input_abs_thres'), - tf.TensorSpec( - shape=[None], dtype=tf.int32, name='input_global_scales_ind') - ]) - def ExtractFeatures(self, input_image, input_scales, input_max_feature_num, - input_abs_thres, input_global_scales_ind): - extracted_features = export_model_utils.ExtractLocalAndGlobalFeatures( - input_image, input_scales, input_max_feature_num, input_abs_thres, - input_global_scales_ind, self._iou, - lambda x: self._model.build_call(x, training=False), - self._stride_factor) - - named_output_tensors = {} - named_output_tensors['boxes'] = tf.identity( - extracted_features[0], name='boxes') - named_output_tensors['features'] = tf.identity( - extracted_features[1], name='features') - named_output_tensors['scales'] = tf.identity( - extracted_features[2], name='scales') - named_output_tensors['scores'] = tf.identity( - extracted_features[3], name='scores') - named_output_tensors['global_descriptors'] = tf.identity( - extracted_features[4], name='global_descriptors') - return named_output_tensors - - -def main(argv): - if len(argv) > 1: - raise app.UsageError('Too many command-line arguments.') - - export_path = FLAGS.export_path - if os.path.exists(export_path): - raise ValueError(f'Export_path {export_path} already exists. Please ' - 'specify a different path or delete the existing one.') - - module = _ExtractModule(FLAGS.delg_global_features, FLAGS.delg_gem_power, - FLAGS.delg_embedding_layer_dim, FLAGS.block3_strides, - FLAGS.iou) - - # Load the weights. - checkpoint_path = FLAGS.ckpt_path - module.LoadWeights(checkpoint_path) - print('Checkpoint loaded from ', checkpoint_path) - - # Save the module - tf.saved_model.save(module, export_path) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/training/model/export_local_model.py b/research/delf/delf/python/training/model/export_local_model.py deleted file mode 100644 index 767d363ef7e..00000000000 --- a/research/delf/delf/python/training/model/export_local_model.py +++ /dev/null @@ -1,128 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Export DELF tensorflow inference model. - -The exported model may use an image pyramid for multi-scale processing, with -local feature extraction including receptive field calculation and keypoint -selection. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import app -from absl import flags -import tensorflow as tf - -from delf.python.training.model import delf_model -from delf.python.training.model import export_model_utils - -FLAGS = flags.FLAGS - -flags.DEFINE_string( - 'ckpt_path', '/tmp/delf-logdir/delf-weights', 'Path to saved checkpoint.') -flags.DEFINE_string('export_path', None, 'Path where model will be exported.') -flags.DEFINE_boolean( - 'block3_strides', True, 'Whether to apply strides after block3.') -flags.DEFINE_float('iou', 1.0, 'IOU for non-max suppression.') -flags.DEFINE_boolean( - 'use_autoencoder', True, - 'Whether the exported model should use an autoencoder.') -flags.DEFINE_float( - 'autoencoder_dimensions', 128, - 'Number of dimensions of the autoencoder. Used only if' - 'use_autoencoder=True.') -flags.DEFINE_float( - 'local_feature_map_channels', 1024, - 'Number of channels at backbone layer used for local feature extraction. ' - 'Default value 1024 is the number of channels of block3. Used only if' - 'use_autoencoder=True.') - - -class _ExtractModule(tf.Module): - """Helper module to build and save DELF model.""" - - def __init__(self, block3_strides, iou): - """Initialization of DELF model. - - Args: - block3_strides: bool, whether to add strides to the output of block3. - iou: IOU for non-max suppression. - """ - self._stride_factor = 2.0 if block3_strides else 1.0 - self._iou = iou - # Setup the DELF model for extraction. - self._model = delf_model.Delf( - block3_strides=block3_strides, - name='DELF', - use_dim_reduction=FLAGS.use_autoencoder, - reduced_dimension=FLAGS.autoencoder_dimensions, - dim_expand_channels=FLAGS.local_feature_map_channels) - - def LoadWeights(self, checkpoint_path): - self._model.load_weights(checkpoint_path) - - @tf.function(input_signature=[ - tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image'), - tf.TensorSpec(shape=[None], dtype=tf.float32, name='input_scales'), - tf.TensorSpec(shape=(), dtype=tf.int32, name='input_max_feature_num'), - tf.TensorSpec(shape=(), dtype=tf.float32, name='input_abs_thres') - ]) - def ExtractFeatures(self, input_image, input_scales, input_max_feature_num, - input_abs_thres): - - extracted_features = export_model_utils.ExtractLocalFeatures( - input_image, input_scales, input_max_feature_num, input_abs_thres, - self._iou, lambda x: self._model(x, training=False), - self._stride_factor) - - named_output_tensors = {} - named_output_tensors['boxes'] = tf.identity( - extracted_features[0], name='boxes') - named_output_tensors['features'] = tf.identity( - extracted_features[1], name='features') - named_output_tensors['scales'] = tf.identity( - extracted_features[2], name='scales') - named_output_tensors['scores'] = tf.identity( - extracted_features[3], name='scores') - return named_output_tensors - - -def main(argv): - if len(argv) > 1: - raise app.UsageError('Too many command-line arguments.') - - export_path = FLAGS.export_path - if os.path.exists(export_path): - raise ValueError(f'Export_path {export_path} already exists. Please ' - 'specify a different path or delete the existing one.') - - module = _ExtractModule(FLAGS.block3_strides, FLAGS.iou) - - # Load the weights. - checkpoint_path = FLAGS.ckpt_path - module.LoadWeights(checkpoint_path) - print('Checkpoint loaded from ', checkpoint_path) - - # Save the module - tf.saved_model.save(module, export_path) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/training/model/export_model_utils.py b/research/delf/delf/python/training/model/export_model_utils.py deleted file mode 100644 index f5419528ffc..00000000000 --- a/research/delf/delf/python/training/model/export_model_utils.py +++ /dev/null @@ -1,410 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Helper functions for DELF model exporting.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -from delf import feature_extractor -from delf.python.datasets.google_landmarks_dataset import googlelandmarks as gld -from object_detection.core import box_list -from object_detection.core import box_list_ops - - -# TODO(andrearaujo): Rewrite this function to be more similar to -# "ExtractLocalAndGlobalFeatures" below, leveraging autograph to avoid the need -# for tf.while loop. -def ExtractLocalFeatures(image, image_scales, max_feature_num, abs_thres, iou, - attention_model_fn, stride_factor): - """Extract local features for input image. - - Args: - image: image tensor of type tf.uint8 with shape [h, w, channels]. - image_scales: 1D float tensor which contains float scales used for image - pyramid construction. - max_feature_num: int tensor denoting the maximum selected feature points. - abs_thres: float tensor denoting the score threshold for feature selection. - iou: float scalar denoting the iou threshold for NMS. - attention_model_fn: model function. Follows the signature: - * Args: - * `images`: Image tensor which is re-scaled. - * Returns: - * `attention_prob`: attention map after the non-linearity. - * `feature_map`: feature map after ResNet convolution. - stride_factor: integer accounting for striding after block3. - - Returns: - boxes: [N, 4] float tensor which denotes the selected receptive box. N is - the number of final feature points which pass through keypoint selection - and NMS steps. - features: [N, depth] float tensor. - feature_scales: [N] float tensor. It is the inverse of the input image - scales such that larger image scales correspond to larger image regions, - which is compatible with keypoints detected with other techniques, for - example Congas. - scores: [N, 1] float tensor denoting the attention score. - - """ - original_image_shape_float = tf.gather( - tf.dtypes.cast(tf.shape(image), tf.float32), [0, 1]) - - image_tensor = gld.NormalizeImages( - image, pixel_value_offset=128.0, pixel_value_scale=128.0) - image_tensor = tf.expand_dims(image_tensor, 0, name='image/expand_dims') - - # Hard code the feature depth and receptive field parameters for now. - # We need to revisit this once we change the architecture and selected - # convolutional blocks to use as local features. - rf, stride, padding = [291.0, 16.0 * stride_factor, 145.0] - feature_depth = 1024 - - def _ProcessSingleScale(scale_index, boxes, features, scales, scores): - """Resizes the image and run feature extraction and keypoint selection. - - This function will be passed into tf.while_loop() and be called - repeatedly. The input boxes are collected from the previous iteration - [0: scale_index -1]. We get the current scale by - image_scales[scale_index], and run resize image, feature extraction and - keypoint selection. Then we will get a new set of selected_boxes for - current scale. In the end, we concat the previous boxes with current - selected_boxes as the output. - Args: - scale_index: A valid index in the image_scales. - boxes: Box tensor with the shape of [N, 4]. - features: Feature tensor with the shape of [N, depth]. - scales: Scale tensor with the shape of [N]. - scores: Attention score tensor with the shape of [N]. - - Returns: - scale_index: The next scale index for processing. - boxes: Concatenated box tensor with the shape of [K, 4]. K >= N. - features: Concatenated feature tensor with the shape of [K, depth]. - scales: Concatenated scale tensor with the shape of [K]. - scores: Concatenated score tensor with the shape of [K]. - """ - scale = tf.gather(image_scales, scale_index) - new_image_size = tf.dtypes.cast( - tf.round(original_image_shape_float * scale), tf.int32) - resized_image = tf.image.resize(image_tensor, new_image_size) - - attention_prob, feature_map = attention_model_fn(resized_image) - attention_prob = tf.squeeze(attention_prob, axis=[0]) - feature_map = tf.squeeze(feature_map, axis=[0]) - - rf_boxes = feature_extractor.CalculateReceptiveBoxes( - tf.shape(feature_map)[0], - tf.shape(feature_map)[1], rf, stride, padding) - - # Re-project back to the original image space. - rf_boxes = tf.divide(rf_boxes, scale) - attention_prob = tf.reshape(attention_prob, [-1]) - feature_map = tf.reshape(feature_map, [-1, feature_depth]) - - # Use attention score to select feature vectors. - indices = tf.reshape(tf.where(attention_prob >= abs_thres), [-1]) - selected_boxes = tf.gather(rf_boxes, indices) - selected_features = tf.gather(feature_map, indices) - selected_scores = tf.gather(attention_prob, indices) - selected_scales = tf.ones_like(selected_scores, tf.float32) / scale - - # Concat with the previous result from different scales. - boxes = tf.concat([boxes, selected_boxes], 0) - features = tf.concat([features, selected_features], 0) - scales = tf.concat([scales, selected_scales], 0) - scores = tf.concat([scores, selected_scores], 0) - - return scale_index + 1, boxes, features, scales, scores - - output_boxes = tf.zeros([0, 4], dtype=tf.float32) - output_features = tf.zeros([0, feature_depth], dtype=tf.float32) - output_scales = tf.zeros([0], dtype=tf.float32) - output_scores = tf.zeros([0], dtype=tf.float32) - - # Process the first scale separately, the following scales will reuse the - # graph variables. - (_, output_boxes, output_features, output_scales, - output_scores) = _ProcessSingleScale(0, output_boxes, output_features, - output_scales, output_scores) - - i = tf.constant(1, dtype=tf.int32) - num_scales = tf.shape(image_scales)[0] - keep_going = lambda j, b, f, scales, scores: tf.less(j, num_scales) - - (_, output_boxes, output_features, output_scales, - output_scores) = tf.nest.map_structure( - tf.stop_gradient, - tf.while_loop( - cond=keep_going, - body=_ProcessSingleScale, - loop_vars=[ - i, output_boxes, output_features, output_scales, output_scores - ], - shape_invariants=[ - i.get_shape(), - tf.TensorShape([None, 4]), - tf.TensorShape([None, feature_depth]), - tf.TensorShape([None]), - tf.TensorShape([None]) - ])) - - feature_boxes = box_list.BoxList(output_boxes) - feature_boxes.add_field('features', output_features) - feature_boxes.add_field('scales', output_scales) - feature_boxes.add_field('scores', output_scores) - - nms_max_boxes = tf.minimum(max_feature_num, feature_boxes.num_boxes()) - final_boxes = box_list_ops.non_max_suppression(feature_boxes, iou, - nms_max_boxes) - - return final_boxes.get(), final_boxes.get_field( - 'features'), final_boxes.get_field('scales'), tf.expand_dims( - final_boxes.get_field('scores'), 1) - - -@tf.function -def ExtractGlobalFeatures(image, - image_scales, - global_scales_ind, - model_fn, - multi_scale_pool_type='None', - normalize_global_descriptor=False, - normalization_function=gld.NormalizeImages): - """Extract global features for input image. - - Args: - image: image tensor of type tf.uint8 with shape [h, w, channels]. - image_scales: 1D float tensor which contains float scales used for image - pyramid construction. - global_scales_ind: Feature extraction happens only for a subset of - `image_scales`, those with corresponding indices from this tensor. - model_fn: model function. Follows the signature: - * Args: - * `images`: Batched image tensor. - * Returns: - * `global_descriptors`: Global descriptors for input images. - multi_scale_pool_type: If set, the global descriptor of each scale is pooled - and a 1D global descriptor is returned. - normalize_global_descriptor: If True, output global descriptors are - L2-normalized. - normalization_function: Function used for normalization. - - Returns: - global_descriptors: If `multi_scale_pool_type` is 'None', returns a [S, D] - float tensor. S is the number of scales, and D the global descriptor - dimensionality. Each D-dimensional entry is a global descriptor, which may - be L2-normalized depending on `normalize_global_descriptor`. If - `multi_scale_pool_type` is not 'None', returns a [D] float tensor with the - pooled global descriptor. - - """ - original_image_shape_float = tf.gather( - tf.dtypes.cast(tf.shape(image), tf.float32), [0, 1]) - image_tensor = normalization_function( - image, pixel_value_offset=128.0, pixel_value_scale=128.0) - image_tensor = tf.expand_dims(image_tensor, 0, name='image/expand_dims') - - def _ResizeAndExtract(scale_index): - """Helper function to resize image then extract global feature. - - Args: - scale_index: A valid index in image_scales. - - Returns: - global_descriptor: [1,D] tensor denoting the extracted global descriptor. - """ - scale = tf.gather(image_scales, scale_index) - new_image_size = tf.dtypes.cast( - tf.round(original_image_shape_float * scale), tf.int32) - resized_image = tf.image.resize(image_tensor, new_image_size) - global_descriptor = model_fn(resized_image) - return global_descriptor - - # First loop to find initial scale to be used. - num_scales = tf.shape(image_scales)[0] - initial_scale_index = tf.constant(-1, dtype=tf.int32) - for scale_index in tf.range(num_scales): - if tf.reduce_any(tf.equal(global_scales_ind, scale_index)): - initial_scale_index = scale_index - break - - output_global = _ResizeAndExtract(initial_scale_index) - - # Loop over subsequent scales. - for scale_index in tf.range(initial_scale_index + 1, num_scales): - # Allow an undefined number of global feature scales to be extracted. - tf.autograph.experimental.set_loop_options( - shape_invariants=[(output_global, tf.TensorShape([None, None]))]) - - if tf.reduce_any(tf.equal(global_scales_ind, scale_index)): - global_descriptor = _ResizeAndExtract(scale_index) - output_global = tf.concat([output_global, global_descriptor], 0) - - normalization_axis = 1 - if multi_scale_pool_type == 'average': - output_global = tf.reduce_mean( - output_global, - axis=0, - keepdims=False, - name='multi_scale_average_pooling') - normalization_axis = 0 - elif multi_scale_pool_type == 'sum': - output_global = tf.reduce_sum( - output_global, axis=0, keepdims=False, name='multi_scale_sum_pooling') - normalization_axis = 0 - - if normalize_global_descriptor: - output_global = tf.nn.l2_normalize( - output_global, axis=normalization_axis, name='l2_normalization') - - return output_global - - -@tf.function -def ExtractLocalAndGlobalFeatures(image, image_scales, max_feature_num, - abs_thres, global_scales_ind, iou, model_fn, - stride_factor): - """Extract local+global features for input image. - - Args: - image: image tensor of type tf.uint8 with shape [h, w, channels]. - image_scales: 1D float tensor which contains float scales used for image - pyramid construction. - max_feature_num: int tensor denoting the maximum selected feature points. - abs_thres: float tensor denoting the score threshold for feature selection. - global_scales_ind: Global feature extraction happens only for a subset of - `image_scales`, those with corresponding indices from this tensor. - iou: float scalar denoting the iou threshold for NMS. - model_fn: model function. Follows the signature: - * Args: - * `images`: Batched image tensor. - * Returns: - * `global_descriptors`: Global descriptors for input images. - * `attention_prob`: Attention map after the non-linearity. - * `feature_map`: Feature map after ResNet convolution. - stride_factor: integer accounting for striding after block3. - - Returns: - boxes: [N, 4] float tensor which denotes the selected receptive boxes. N is - the number of final feature points which pass through keypoint selection - and NMS steps. - local_descriptors: [N, depth] float tensor. - feature_scales: [N] float tensor. It is the inverse of the input image - scales such that larger image scales correspond to larger image regions, - which is compatible with keypoints detected with other techniques, for - example Congas. - scores: [N, 1] float tensor denoting the attention score. - global_descriptors: [S, D] float tensor, with the global descriptors for - each scale; S is the number of scales, and D the global descriptor - dimensionality. - """ - original_image_shape_float = tf.gather( - tf.dtypes.cast(tf.shape(image), tf.float32), [0, 1]) - image_tensor = gld.NormalizeImages( - image, pixel_value_offset=128.0, pixel_value_scale=128.0) - image_tensor = tf.expand_dims(image_tensor, 0, name='image/expand_dims') - - # Hard code the receptive field parameters for now. - # We need to revisit this once we change the architecture and selected - # convolutional blocks to use as local features. - rf, stride, padding = [291.0, 16.0 * stride_factor, 145.0] - - def _ResizeAndExtract(scale_index): - """Helper function to resize image then extract features. - - Args: - scale_index: A valid index in image_scales. - - Returns: - global_descriptor: [1,D] tensor denoting the extracted global descriptor. - boxes: Box tensor with the shape of [K, 4]. - local_descriptors: Local descriptor tensor with the shape of [K, depth]. - scales: Scale tensor with the shape of [K]. - scores: Score tensor with the shape of [K]. - """ - scale = tf.gather(image_scales, scale_index) - new_image_size = tf.dtypes.cast( - tf.round(original_image_shape_float * scale), tf.int32) - resized_image = tf.image.resize(image_tensor, new_image_size) - global_descriptor, attention_prob, feature_map = model_fn(resized_image) - - attention_prob = tf.squeeze(attention_prob, axis=[0]) - feature_map = tf.squeeze(feature_map, axis=[0]) - - # Compute RF boxes and re-project them to the original image space. - rf_boxes = feature_extractor.CalculateReceptiveBoxes( - tf.shape(feature_map)[0], - tf.shape(feature_map)[1], rf, stride, padding) - rf_boxes = tf.divide(rf_boxes, scale) - - attention_prob = tf.reshape(attention_prob, [-1]) - feature_map = tf.reshape(feature_map, [-1, tf.shape(feature_map)[2]]) - - # Use attention score to select local features. - indices = tf.reshape(tf.where(attention_prob >= abs_thres), [-1]) - boxes = tf.gather(rf_boxes, indices) - local_descriptors = tf.gather(feature_map, indices) - scores = tf.gather(attention_prob, indices) - scales = tf.ones_like(scores, tf.float32) / scale - - return global_descriptor, boxes, local_descriptors, scales, scores - - # TODO(andrearaujo): Currently, a global feature is extracted even for scales - # which are not using it. The obtained result is correct, however feature - # extraction is slower than expected. We should try to fix this in the future. - - # Run first scale. - (output_global_descriptors, output_boxes, output_local_descriptors, - output_scales, output_scores) = _ResizeAndExtract(0) - if not tf.reduce_any(tf.equal(global_scales_ind, 0)): - # If global descriptor is not using the first scale, clear it out. - output_global_descriptors = tf.zeros( - [0, tf.shape(output_global_descriptors)[1]]) - - # Loop over subsequent scales. - num_scales = tf.shape(image_scales)[0] - for scale_index in tf.range(1, num_scales): - # Allow an undefined number of global feature scales to be extracted. - tf.autograph.experimental.set_loop_options( - shape_invariants=[(output_global_descriptors, - tf.TensorShape([None, None]))]) - - (global_descriptor, boxes, local_descriptors, scales, - scores) = _ResizeAndExtract(scale_index) - output_boxes = tf.concat([output_boxes, boxes], 0) - output_local_descriptors = tf.concat( - [output_local_descriptors, local_descriptors], 0) - output_scales = tf.concat([output_scales, scales], 0) - output_scores = tf.concat([output_scores, scores], 0) - if tf.reduce_any(tf.equal(global_scales_ind, scale_index)): - output_global_descriptors = tf.concat( - [output_global_descriptors, global_descriptor], 0) - - feature_boxes = box_list.BoxList(output_boxes) - feature_boxes.add_field('local_descriptors', output_local_descriptors) - feature_boxes.add_field('scales', output_scales) - feature_boxes.add_field('scores', output_scores) - - nms_max_boxes = tf.minimum(max_feature_num, feature_boxes.num_boxes()) - final_boxes = box_list_ops.non_max_suppression(feature_boxes, iou, - nms_max_boxes) - - return (final_boxes.get(), final_boxes.get_field('local_descriptors'), - final_boxes.get_field('scales'), - tf.expand_dims(final_boxes.get_field('scores'), - 1), output_global_descriptors) diff --git a/research/delf/delf/python/training/model/global_model.py b/research/delf/delf/python/training/model/global_model.py deleted file mode 100644 index bfeac376955..00000000000 --- a/research/delf/delf/python/training/model/global_model.py +++ /dev/null @@ -1,285 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""CNN Image Retrieval model implementation based on the following papers: - - [1] Fine-tuning CNN Image Retrieval with No Human Annotation, - Radenović F., Tolias G., Chum O., TPAMI 2018 [arXiv] - https://arxiv.org/abs/1711.02512 - - [2] CNN Image Retrieval Learns from BoW: Unsupervised Fine-Tuning with Hard - Examples, Radenović F., Tolias G., Chum O., ECCV 2016 [arXiv] - https://arxiv.org/abs/1604.02426 -""" - -import os - -import pickle -import tensorflow as tf - -from delf.python.datasets import generic_dataset -from delf.python.normalization_layers import normalization -from delf.python.pooling_layers import pooling as pooling_layers -from delf.python.training import global_features_utils - -# Pre-computed global whitening, for most commonly used architectures. -# Using pre-computed whitening improves the speed of the convergence and the -# performance. -_WHITENING_CONFIG = { - 'ResNet50': 'http://cmp.felk.cvut.cz/cnnimageretrieval_tf' - '/SFM120k_ResNet50_gem_learned_whitening_config.pkl', - 'ResNet101': 'http://cmp.felk.cvut.cz/cnnimageretrieval_tf' - '/SFM120k_ResNet101_gem_learned_whitening_config.pkl', - 'ResNet152': 'http://cmp.felk.cvut.cz/cnnimageretrieval_tf' - '/SFM120k_ResNet152_gem_learned_whitening_config.pkl', - 'VGG19': 'http://cmp.felk.cvut.cz/cnnimageretrieval_tf' - '/SFM120k_VGG19_gem_learned_whitening_config.pkl' -} - -# Possible global pooling layers. -_POOLING = { - 'mac': pooling_layers.MAC, - 'spoc': pooling_layers.SPoC, - 'gem': pooling_layers.GeM -} - -# Output dimensionality for supported architectures. -_OUTPUT_DIM = { - 'VGG16': 512, - 'VGG19': 512, - 'ResNet50': 2048, - 'ResNet101': 2048, - 'ResNet101V2': 2048, - 'ResNet152': 2048, - 'DenseNet121': 1024, - 'DenseNet169': 1664, - 'DenseNet201': 1920, - 'EfficientNetB5': 2048, - 'EfficientNetB7': 2560 -} - - -class GlobalFeatureNet(tf.keras.Model): - """Instantiates global model for image retrieval. - - This class implements the [GlobalFeatureNet]( - https://arxiv.org/abs/1711.02512) for image retrieval. The model uses a - user-defined model as a backbone. - """ - - def __init__(self, architecture='ResNet101', pooling='gem', - whitening=False, pretrained=True, data_root=''): - """GlobalFeatureNet network initialization. - - Args: - architecture: Network backbone. - pooling: Pooling method used 'mac'/'spoc'/'gem'. - whitening: Bool, whether to use whitening. - pretrained: Bool, whether to initialize the network with the weights - pretrained on ImageNet. - data_root: String, path to the data folder where the precomputed - whitening is/will be saved in case `whitening` is True. - - Raises: - ValueError: If `architecture` is not supported. - """ - if architecture not in _OUTPUT_DIM.keys(): - raise ValueError("Architecture {} is not supported.".format(architecture)) - - super(GlobalFeatureNet, self).__init__() - - # Get standard output dimensionality size. - dim = _OUTPUT_DIM[architecture] - - if pretrained: - # Initialize with network pretrained on imagenet. - net_in = getattr(tf.keras.applications, architecture)(include_top=False, - weights="imagenet") - else: - # Initialize with random weights. - net_in = getattr(tf.keras.applications, architecture)(include_top=False, - weights=None) - - # Initialize `feature_extractor`. Take only convolutions for - # `feature_extractor`, always end with ReLU to make last activations - # non-negative. - if architecture.lower().startswith('densenet'): - tmp_model = tf.keras.Sequential() - tmp_model.add(net_in) - net_in = tmp_model - net_in.add(tf.keras.layers.ReLU()) - - # Initialize pooling. - self.pool = _POOLING[pooling]() - - # Initialize whitening. - if whitening: - if pretrained and architecture in _WHITENING_CONFIG: - # If precomputed whitening for the architecture exists, - # the fully-connected layer is going to be initialized according to - # the precomputed layer configuration. - global_features_utils.debug_and_log( - ">> {}: for '{}' custom computed whitening '{}' is used." - .format(os.getcwd(), architecture, - os.path.basename(_WHITENING_CONFIG[architecture]))) - # The layer configuration is downloaded to the `data_root` folder. - whiten_dir = os.path.join(data_root, architecture) - path = tf.keras.utils.get_file(fname=whiten_dir, - origin=_WHITENING_CONFIG[architecture]) - # Whitening configuration is loaded. - with tf.io.gfile.GFile(path, 'rb') as learned_whitening_file: - whitening_config = pickle.load(learned_whitening_file) - # Whitening layer is initialized according to the configuration. - self.whiten = tf.keras.layers.Dense.from_config(whitening_config) - else: - # In case if no precomputed whitening exists for the chosen - # architecture, the fully-connected whitening layer is initialized - # with the random weights. - self.whiten = tf.keras.layers.Dense(dim, activation=None, use_bias=True) - global_features_utils.debug_and_log( - ">> There is either no whitening computed for the " - "used network architecture or pretrained is False," - " random weights are used.") - else: - self.whiten = None - - # Create meta information to be stored in the network. - self.meta = { - 'architecture': architecture, - 'pooling': pooling, - 'whitening': whitening, - 'outputdim': dim - } - - self.feature_extractor = net_in - self.normalize = normalization.L2Normalization() - - def call(self, x, training=False): - """Invokes the GlobalFeatureNet instance. - - Args: - x: [B, H, W, C] Tensor with a batch of images. - training: Indicator of whether the forward pass is running in training - mode or not. - - Returns: - out: [B, out_dim] Global descriptor. - """ - # Forward pass through the fully-convolutional backbone. - o = self.feature_extractor(x, training) - # Pooling. - o = self.pool(o) - # Normalization. - o = self.normalize(o) - - # If whitening exists: the pooled global descriptor is whitened and - # re-normalized. - if self.whiten is not None: - o = self.whiten(o) - o = self.normalize(o) - return o - - def meta_repr(self): - '''Provides high-level information about the network. - - Returns: - meta: string with the information about the network (used - architecture, pooling type, whitening, outputdim). - ''' - tmpstr = '(meta):\n' - tmpstr += '\tarchitecture: {}\n'.format(self.meta['architecture']) - tmpstr += '\tpooling: {}\n'.format(self.meta['pooling']) - tmpstr += '\twhitening: {}\n'.format(self.meta['whitening']) - tmpstr += '\toutputdim: {}\n'.format(self.meta['outputdim']) - return tmpstr - - -def extract_global_descriptors_from_list(net, images, image_size, - bounding_boxes=None, scales=[1.], - multi_scale_power=1., print_freq=10): - """Extracting global descriptors from a list of images. - - Args: - net: Model object, network for the forward pass. - images: Absolute image paths as strings. - image_size: Integer, defines the maximum size of longer image side. - bounding_boxes: List of (x1,y1,x2,y2) tuples to crop the query images. - scales: List of float scales. - multi_scale_power: Float, multi-scale normalization power parameter. - print_freq: Printing frequency for debugging. - - Returns: - descriptors: Global descriptors for the input images. - """ - # Creating dataset loader. - data = generic_dataset.ImagesFromList(root='', image_paths=images, - imsize=image_size, - bounding_boxes=bounding_boxes) - - def _data_gen(): - return (inst for inst in data) - - loader = tf.data.Dataset.from_generator(_data_gen, output_types=(tf.float32)) - loader = loader.batch(1) - - # Extracting vectors. - descriptors = tf.zeros((0, net.meta['outputdim'])) - for i, input in enumerate(loader): - if len(scales) == 1 and scales[0] == 1: - descriptors = tf.concat([descriptors, net(input)], 0) - else: - descriptors = tf.concat( - [descriptors, extract_multi_scale_descriptor( - net, input, scales, multi_scale_power)], 0) - - if (i + 1) % print_freq == 0 or (i + 1) == len(images): - global_features_utils.debug_and_log( - '\r>>>> {}/{} done...'.format((i + 1), len(images)), - debug_on_the_same_line=True) - global_features_utils.debug_and_log('', log=False) - - descriptors = tf.transpose(descriptors, perm=[1, 0]) - return descriptors - - -def extract_multi_scale_descriptor(net, input, scales, multi_scale_power): - """Extracts the global descriptor multi scale. - - Args: - net: Model object, network for the forward pass. - input: [B, H, W, C] input tensor in channel-last (BHWC) configuration. - scales: List of float scales. - multi_scale_power: Float, multi-scale normalization power parameter. - - Returns: - descriptors: Multi-scale global descriptors for the input images. - """ - descriptors = tf.zeros(net.meta['outputdim']) - - for s in scales: - if s == 1: - input_t = input - else: - output_shape = s * tf.shape(input)[1:3].numpy() - input_t = tf.image.resize(input, output_shape, - method='bilinear', - preserve_aspect_ratio=True) - descriptors += tf.pow(net(input_t), multi_scale_power) - - descriptors /= len(scales) - descriptors = tf.pow(descriptors, 1. / multi_scale_power) - descriptors /= tf.norm(descriptors) - - return descriptors diff --git a/research/delf/delf/python/training/model/global_model_test.py b/research/delf/delf/python/training/model/global_model_test.py deleted file mode 100644 index b171a089d57..00000000000 --- a/research/delf/delf/python/training/model/global_model_test.py +++ /dev/null @@ -1,86 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the GlobalFeatureNet backbone.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -from absl import flags - -import numpy as np -from PIL import Image -import tensorflow as tf - -from delf.python.training.model import global_model - -FLAGS = flags.FLAGS - - -class GlobalFeatureNetTest(tf.test.TestCase): - """Tests for the GlobalFeatureNet backbone.""" - - def testInitModel(self): - """Testing GlobalFeatureNet initialization.""" - # Testing GlobalFeatureNet initialization. - model_params = {'architecture': 'ResNet101', 'pooling': 'gem', - 'whitening': False, 'pretrained': True} - model = global_model.GlobalFeatureNet(**model_params) - expected_meta = {'architecture': 'ResNet101', 'pooling': 'gem', - 'whitening': False, 'outputdim': 2048} - self.assertEqual(expected_meta, model.meta) - - def testExtractVectors(self): - """Tests extraction of global descriptors from list.""" - # Initializing network for testing. - model_params = {'architecture': 'ResNet101', 'pooling': 'gem', - 'whitening': False, 'pretrained': True} - model = global_model.GlobalFeatureNet(**model_params) - - # Number of images to be created. - n = 2 - image_paths = [] - - # Create `n` dummy images. - for i in range(n): - dummy_image = np.random.rand(1024, 750, 3) * 255 - img_out = Image.fromarray(dummy_image.astype('uint8')).convert('RGB') - filename = os.path.join(FLAGS.test_tmpdir, 'test_image_{}.jpg'.format(i)) - img_out.save(filename) - image_paths.append(filename) - - descriptors = global_model.extract_global_descriptors_from_list( - model, image_paths, image_size=1024, bounding_boxes=None, - scales=[1., 3.], multi_scale_power=2, print_freq=1) - self.assertAllEqual([2048, 2], tf.shape(descriptors)) - - def testExtractMultiScale(self): - """Tests multi-scale global descriptor extraction.""" - # Initializing network for testing. - model_params = {'architecture': 'ResNet101', 'pooling': 'gem', - 'whitening': False, 'pretrained': True} - model = global_model.GlobalFeatureNet(**model_params) - - input = tf.random.uniform([2, 1024, 750, 3], dtype=tf.float32, seed=0) - descriptors = global_model.extract_multi_scale_descriptor( - model, input, scales=[1., 3.], multi_scale_power=2) - self.assertAllEqual([2, 2048], tf.shape(descriptors)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/training/model/resnet50.py b/research/delf/delf/python/training/model/resnet50.py deleted file mode 100644 index 3718ac5b05f..00000000000 --- a/research/delf/delf/python/training/model/resnet50.py +++ /dev/null @@ -1,460 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""ResNet50 backbone used in DELF model. - -Copied over from tensorflow/python/eager/benchmarks/resnet50/resnet50.py, -because that code does not support dependencies. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -import os -import tempfile - -from absl import logging -import h5py -import tensorflow as tf - -from delf.python.pooling_layers import pooling as pooling_layers - -layers = tf.keras.layers - - -class _IdentityBlock(tf.keras.Model): - """_IdentityBlock is the block that has no conv layer at shortcut. - - Args: - kernel_size: the kernel size of middle conv layer at main path - filters: list of integers, the filters of 3 conv layer at main path - stage: integer, current stage label, used for generating layer names - block: 'a','b'..., current block label, used for generating layer names - data_format: data_format for the input ('channels_first' or - 'channels_last'). - """ - - def __init__(self, kernel_size, filters, stage, block, data_format): - super(_IdentityBlock, self).__init__(name='') - filters1, filters2, filters3 = filters - - conv_name_base = 'res' + str(stage) + block + '_branch' - bn_name_base = 'bn' + str(stage) + block + '_branch' - bn_axis = 1 if data_format == 'channels_first' else 3 - - self.conv2a = layers.Conv2D( - filters1, (1, 1), name=conv_name_base + '2a', data_format=data_format) - self.bn2a = layers.BatchNormalization( - axis=bn_axis, name=bn_name_base + '2a') - - self.conv2b = layers.Conv2D( - filters2, - kernel_size, - padding='same', - data_format=data_format, - name=conv_name_base + '2b') - self.bn2b = layers.BatchNormalization( - axis=bn_axis, name=bn_name_base + '2b') - - self.conv2c = layers.Conv2D( - filters3, (1, 1), name=conv_name_base + '2c', data_format=data_format) - self.bn2c = layers.BatchNormalization( - axis=bn_axis, name=bn_name_base + '2c') - - def call(self, input_tensor, training=False): - x = self.conv2a(input_tensor) - x = self.bn2a(x, training=training) - x = tf.nn.relu(x) - - x = self.conv2b(x) - x = self.bn2b(x, training=training) - x = tf.nn.relu(x) - - x = self.conv2c(x) - x = self.bn2c(x, training=training) - - x += input_tensor - return tf.nn.relu(x) - - -class _ConvBlock(tf.keras.Model): - """_ConvBlock is the block that has a conv layer at shortcut. - - Args: - kernel_size: the kernel size of middle conv layer at main path - filters: list of integers, the filters of 3 conv layer at main path - stage: integer, current stage label, used for generating layer names - block: 'a','b'..., current block label, used for generating layer names - data_format: data_format for the input ('channels_first' or - 'channels_last'). - strides: strides for the convolution. Note that from stage 3, the first - conv layer at main path is with strides=(2,2), and the shortcut should - have strides=(2,2) as well. - """ - - def __init__(self, - kernel_size, - filters, - stage, - block, - data_format, - strides=(2, 2)): - super(_ConvBlock, self).__init__(name='') - filters1, filters2, filters3 = filters - - conv_name_base = 'res' + str(stage) + block + '_branch' - bn_name_base = 'bn' + str(stage) + block + '_branch' - bn_axis = 1 if data_format == 'channels_first' else 3 - - self.conv2a = layers.Conv2D( - filters1, (1, 1), - strides=strides, - name=conv_name_base + '2a', - data_format=data_format) - self.bn2a = layers.BatchNormalization( - axis=bn_axis, name=bn_name_base + '2a') - - self.conv2b = layers.Conv2D( - filters2, - kernel_size, - padding='same', - name=conv_name_base + '2b', - data_format=data_format) - self.bn2b = layers.BatchNormalization( - axis=bn_axis, name=bn_name_base + '2b') - - self.conv2c = layers.Conv2D( - filters3, (1, 1), name=conv_name_base + '2c', data_format=data_format) - self.bn2c = layers.BatchNormalization( - axis=bn_axis, name=bn_name_base + '2c') - - self.conv_shortcut = layers.Conv2D( - filters3, (1, 1), - strides=strides, - name=conv_name_base + '1', - data_format=data_format) - self.bn_shortcut = layers.BatchNormalization( - axis=bn_axis, name=bn_name_base + '1') - - def call(self, input_tensor, training=False): - x = self.conv2a(input_tensor) - x = self.bn2a(x, training=training) - x = tf.nn.relu(x) - - x = self.conv2b(x) - x = self.bn2b(x, training=training) - x = tf.nn.relu(x) - - x = self.conv2c(x) - x = self.bn2c(x, training=training) - - shortcut = self.conv_shortcut(input_tensor) - shortcut = self.bn_shortcut(shortcut, training=training) - - x += shortcut - return tf.nn.relu(x) - - -# pylint: disable=not-callable -class ResNet50(tf.keras.Model): - """Instantiates the ResNet50 architecture. - - Args: - data_format: format for the image. Either 'channels_first' or - 'channels_last'. 'channels_first' is typically faster on GPUs while - 'channels_last' is typically faster on CPUs. See - https://www.tensorflow.org/performance/performance_guide#data_formats - name: Prefix applied to names of variables created in the model. - include_top: whether to include the fully-connected layer at the top of the - network. - pooling: Optional pooling mode for feature extraction when `include_top` is - False. 'None' means that the output of the model will be the 4D tensor - output of the last convolutional layer. 'avg' means that global average - pooling will be applied to the output of the last convolutional layer, and - thus the output of the model will be a 2D tensor. 'max' means that global - max pooling will be applied. 'gem' means GeM pooling will be applied. - block3_strides: whether to add a stride of 2 to block3 to make it compatible - with tf.slim ResNet implementation. - average_pooling: whether to do average pooling of block4 features before - global pooling. - classes: optional number of classes to classify images into, only to be - specified if `include_top` is True. - gem_power: GeM power for GeM pooling. Only used if pooling == 'gem'. - embedding_layer: whether to create an embedding layer (FC whitening layer). - embedding_layer_dim: size of the embedding layer. - - Raises: - ValueError: in case of invalid argument for data_format. - """ - - def __init__(self, - data_format, - name='', - include_top=True, - pooling=None, - block3_strides=False, - average_pooling=True, - classes=1000, - gem_power=3.0, - embedding_layer=False, - embedding_layer_dim=2048): - super(ResNet50, self).__init__(name=name) - - valid_channel_values = ('channels_first', 'channels_last') - if data_format not in valid_channel_values: - raise ValueError('Unknown data_format: %s. Valid values: %s' % - (data_format, valid_channel_values)) - self.include_top = include_top - self.block3_strides = block3_strides - self.average_pooling = average_pooling - self.pooling = pooling - - def conv_block(filters, stage, block, strides=(2, 2)): - return _ConvBlock( - 3, - filters, - stage=stage, - block=block, - data_format=data_format, - strides=strides) - - def id_block(filters, stage, block): - return _IdentityBlock( - 3, filters, stage=stage, block=block, data_format=data_format) - - self.conv1 = layers.Conv2D( - 64, (7, 7), - strides=(2, 2), - data_format=data_format, - padding='same', - name='conv1') - bn_axis = 1 if data_format == 'channels_first' else 3 - self.bn_conv1 = layers.BatchNormalization(axis=bn_axis, name='bn_conv1') - self.max_pool = layers.MaxPooling2D((3, 3), - strides=(2, 2), - data_format=data_format) - - self.l2a = conv_block([64, 64, 256], stage=2, block='a', strides=(1, 1)) - self.l2b = id_block([64, 64, 256], stage=2, block='b') - self.l2c = id_block([64, 64, 256], stage=2, block='c') - - self.l3a = conv_block([128, 128, 512], stage=3, block='a') - self.l3b = id_block([128, 128, 512], stage=3, block='b') - self.l3c = id_block([128, 128, 512], stage=3, block='c') - self.l3d = id_block([128, 128, 512], stage=3, block='d') - - self.l4a = conv_block([256, 256, 1024], stage=4, block='a') - self.l4b = id_block([256, 256, 1024], stage=4, block='b') - self.l4c = id_block([256, 256, 1024], stage=4, block='c') - self.l4d = id_block([256, 256, 1024], stage=4, block='d') - self.l4e = id_block([256, 256, 1024], stage=4, block='e') - self.l4f = id_block([256, 256, 1024], stage=4, block='f') - - # Striding layer that can be used on top of block3 to produce feature maps - # with the same resolution as the TF-Slim implementation. - if self.block3_strides: - self.subsampling_layer = layers.MaxPooling2D((1, 1), - strides=(2, 2), - data_format=data_format) - self.l5a = conv_block([512, 512, 2048], - stage=5, - block='a', - strides=(1, 1)) - else: - self.l5a = conv_block([512, 512, 2048], stage=5, block='a') - self.l5b = id_block([512, 512, 2048], stage=5, block='b') - self.l5c = id_block([512, 512, 2048], stage=5, block='c') - - self.avg_pool = layers.AveragePooling2D((7, 7), - strides=(7, 7), - data_format=data_format) - - if self.include_top: - self.flatten = layers.Flatten() - self.fc1000 = layers.Dense(classes, name='fc1000') - else: - reduction_indices = [1, 2] if data_format == 'channels_last' else [2, 3] - reduction_indices = tf.constant(reduction_indices) - if pooling == 'avg': - self.global_pooling = functools.partial( - tf.reduce_mean, axis=reduction_indices, keepdims=False) - elif pooling == 'max': - self.global_pooling = functools.partial( - tf.reduce_max, axis=reduction_indices, keepdims=False) - elif pooling == 'gem': - logging.info('Adding GeMPooling layer with power %f', gem_power) - self.global_pooling = functools.partial( - pooling_layers.gem, axis=reduction_indices, power=gem_power) - else: - self.global_pooling = None - if embedding_layer: - logging.info('Adding embedding layer with dimension %d', - embedding_layer_dim) - self.embedding_layer = layers.Dense( - embedding_layer_dim, name='embedding_layer') - else: - self.embedding_layer = None - - def build_call(self, inputs, training=True, intermediates_dict=None): - """Building the ResNet50 model. - - Args: - inputs: Images to compute features for. - training: Whether model is in training phase. - intermediates_dict: `None` or dictionary. If not None, accumulate feature - maps from intermediate blocks into the dictionary. "" - - Returns: - Tensor with featuremap. - """ - - x = self.conv1(inputs) - x = self.bn_conv1(x, training=training) - x = tf.nn.relu(x) - if intermediates_dict is not None: - intermediates_dict['block0'] = x - - x = self.max_pool(x) - if intermediates_dict is not None: - intermediates_dict['block0mp'] = x - - # Block 1 (equivalent to "conv2" in Resnet paper). - x = self.l2a(x, training=training) - x = self.l2b(x, training=training) - x = self.l2c(x, training=training) - if intermediates_dict is not None: - intermediates_dict['block1'] = x - - # Block 2 (equivalent to "conv3" in Resnet paper). - x = self.l3a(x, training=training) - x = self.l3b(x, training=training) - x = self.l3c(x, training=training) - x = self.l3d(x, training=training) - if intermediates_dict is not None: - intermediates_dict['block2'] = x - - # Block 3 (equivalent to "conv4" in Resnet paper). - x = self.l4a(x, training=training) - x = self.l4b(x, training=training) - x = self.l4c(x, training=training) - x = self.l4d(x, training=training) - x = self.l4e(x, training=training) - x = self.l4f(x, training=training) - - if self.block3_strides: - x = self.subsampling_layer(x) - if intermediates_dict is not None: - intermediates_dict['block3'] = x - else: - if intermediates_dict is not None: - intermediates_dict['block3'] = x - - x = self.l5a(x, training=training) - x = self.l5b(x, training=training) - x = self.l5c(x, training=training) - - if self.average_pooling: - x = self.avg_pool(x) - if intermediates_dict is not None: - intermediates_dict['block4'] = x - else: - if intermediates_dict is not None: - intermediates_dict['block4'] = x - - if self.include_top: - return self.fc1000(self.flatten(x)) - elif self.global_pooling: - x = self.global_pooling(x) - if self.embedding_layer: - x = self.embedding_layer(x) - return x - else: - return x - - def call(self, inputs, training=True, intermediates_dict=None): - """Call the ResNet50 model. - - Args: - inputs: Images to compute features for. - training: Whether model is in training phase. - intermediates_dict: `None` or dictionary. If not None, accumulate feature - maps from intermediate blocks into the dictionary. "" - - Returns: - Tensor with featuremap. - """ - return self.build_call(inputs, training, intermediates_dict) - - def restore_weights(self, filepath): - """Load pretrained weights. - - This function loads a .h5 file from the filepath with saved model weights - and assigns them to the model. - - Args: - filepath: String, path to the .h5 file - - Raises: - ValueError: if the file referenced by `filepath` does not exist. - """ - if not tf.io.gfile.exists(filepath): - raise ValueError('Unable to load weights from %s. You must provide a' - 'valid file.' % (filepath)) - - # Create a local copy of the weights file for h5py to be able to read it. - local_filename = os.path.basename(filepath) - tmp_filename = os.path.join(tempfile.gettempdir(), local_filename) - tf.io.gfile.copy(filepath, tmp_filename, overwrite=True) - - # Load the content of the weights file. - f = h5py.File(tmp_filename, mode='r') - saved_layer_names = [n.decode('utf8') for n in f.attrs['layer_names']] - - try: - # Iterate through all the layers assuming the max `depth` is 2. - for layer in self.layers: - if hasattr(layer, 'layers'): - for inlayer in layer.layers: - # Make sure the weights are in the saved model, and that we are in - # the innermost layer. - if inlayer.name not in saved_layer_names: - raise ValueError('Layer %s absent from the pretrained weights.' - 'Unable to load its weights.' % (inlayer.name)) - if hasattr(inlayer, 'layers'): - raise ValueError('Layer %s is not a depth 2 layer. Unable to load' - 'its weights.' % (inlayer.name)) - # Assign the weights in the current layer. - g = f[inlayer.name] - weight_names = [n.decode('utf8') for n in g.attrs['weight_names']] - weight_values = [g[weight_name] for weight_name in weight_names] - logging.info('Setting the weights for layer %s', inlayer.name) - inlayer.set_weights(weight_values) - finally: - # Clean up the temporary file. - tf.io.gfile.remove(tmp_filename) - - def log_weights(self): - """Log backbone weights.""" - logging.info('Logging backbone weights') - logging.info('------------------------') - for layer in self.layers: - if hasattr(layer, 'layers'): - for inlayer in layer.layers: - logging.info('Weights for layer: %s, inlayer % s', layer.name, - inlayer.name) - weights = inlayer.get_weights() - logging.info(weights) - else: - logging.info('Layer %s does not have inner layers.', layer.name) diff --git a/research/delf/delf/python/training/tensorboard_utils.py b/research/delf/delf/python/training/tensorboard_utils.py deleted file mode 100644 index f1d5e3f23e5..00000000000 --- a/research/delf/delf/python/training/tensorboard_utils.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utilities for tensorboard.""" - -from tensorboard import program - -from delf.python.training import global_features_utils - - -def launch_tensorboard(log_dir): - """Runs tensorboard with the given `log_dir`. - - Args: - log_dir: String, directory to launch tensorboard in. - """ - tensorboard = program.TensorBoard() - tensorboard.configure(argv=[None, '--logdir', log_dir]) - url = tensorboard.launch() - global_features_utils.debug_and_log("Launching Tensorboard: {}".format(url)) diff --git a/research/delf/delf/python/training/train.py b/research/delf/delf/python/training/train.py deleted file mode 100644 index d21decdd49b..00000000000 --- a/research/delf/delf/python/training/train.py +++ /dev/null @@ -1,558 +0,0 @@ -# Lint as: python3 -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Training script for DELF/G on Google Landmarks Dataset. - -Uses classification loss, with MirroredStrategy, to support running on multiple -GPUs. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import time - -from absl import app -from absl import flags -from absl import logging -import tensorflow as tf -import tensorflow_probability as tfp - -# Placeholder for internal import. Do not remove this line. -from delf.python.datasets.google_landmarks_dataset import googlelandmarks as gld -from delf.python.training.model import delf_model -from delf.python.training.model import delg_model - -FLAGS = flags.FLAGS - -flags.DEFINE_boolean('debug', False, 'Debug mode.') -flags.DEFINE_string('logdir', '/tmp/delf', 'WithTensorBoard logdir.') -flags.DEFINE_string('train_file_pattern', '/tmp/data/train*', - 'File pattern of training dataset files.') -flags.DEFINE_string('validation_file_pattern', '/tmp/data/validation*', - 'File pattern of validation dataset files.') -flags.DEFINE_enum( - 'dataset_version', 'gld_v1', ['gld_v1', 'gld_v2', 'gld_v2_clean'], - 'Google Landmarks dataset version, used to determine the number of ' - 'classes.') -flags.DEFINE_integer('seed', 0, 'Seed to training dataset.') -flags.DEFINE_float('initial_lr', 0.01, 'Initial learning rate.') -flags.DEFINE_integer('batch_size', 32, 'Global batch size.') -flags.DEFINE_integer('max_iters', 500000, 'Maximum iterations.') -flags.DEFINE_boolean('block3_strides', True, 'Whether to use block3_strides.') -flags.DEFINE_boolean('use_augmentation', True, - 'Whether to use ImageNet style augmentation.') -flags.DEFINE_string( - 'imagenet_checkpoint', None, - 'ImageNet checkpoint for ResNet backbone. If None, no checkpoint is used.') -flags.DEFINE_float( - 'attention_loss_weight', 1.0, - 'Weight to apply to the attention loss when calculating the ' - 'total loss of the model.') -flags.DEFINE_boolean('delg_global_features', False, - 'Whether to train a DELG model.') -flags.DEFINE_float( - 'delg_gem_power', 3.0, 'Power for Generalized Mean pooling. Used only if ' - 'delg_global_features=True.') -flags.DEFINE_integer( - 'delg_embedding_layer_dim', 2048, - 'Size of the FC whitening layer (embedding layer). Used only if' - 'delg_global_features:True.') -flags.DEFINE_float( - 'delg_scale_factor_init', 45.25, - 'Initial value of the scaling factor of the cosine logits. The default ' - 'value is sqrt(2048). Used only if delg_global_features=True.') -flags.DEFINE_float('delg_arcface_margin', 0.1, - 'ArcFace margin. Used only if delg_global_features=True.') -flags.DEFINE_integer('image_size', 321, 'Size of each image side to use.') -flags.DEFINE_boolean('use_autoencoder', True, - 'Whether to train an autoencoder.') -flags.DEFINE_float( - 'reconstruction_loss_weight', 10.0, - 'Weight to apply to the reconstruction loss from the autoencoder when' - 'calculating total loss of the model. Used only if use_autoencoder=True.') -flags.DEFINE_float( - 'autoencoder_dimensions', 128, - 'Number of dimensions of the autoencoder. Used only if' - 'use_autoencoder=True.') -flags.DEFINE_float( - 'local_feature_map_channels', 1024, - 'Number of channels at backbone layer used for local feature extraction. ' - 'Default value 1024 is the number of channels of block3. Used only if' - 'use_autoencoder=True.') - - -def _record_accuracy(metric, logits, labels): - """Record accuracy given predicted logits and ground-truth labels.""" - softmax_probabilities = tf.keras.layers.Softmax()(logits) - metric.update_state(labels, softmax_probabilities) - - -def _attention_summaries(scores, global_step): - """Record statistics of the attention score.""" - tf.summary.image( - 'batch_attention', - scores / tf.reduce_max(scores + 1e-3), - step=global_step) - tf.summary.scalar('attention/max', tf.reduce_max(scores), step=global_step) - tf.summary.scalar('attention/min', tf.reduce_min(scores), step=global_step) - tf.summary.scalar('attention/mean', tf.reduce_mean(scores), step=global_step) - tf.summary.scalar( - 'attention/percent_25', - tfp.stats.percentile(scores, 25.0), - step=global_step) - tf.summary.scalar( - 'attention/percent_50', - tfp.stats.percentile(scores, 50.0), - step=global_step) - tf.summary.scalar( - 'attention/percent_75', - tfp.stats.percentile(scores, 75.0), - step=global_step) - - -def create_model(num_classes): - """Define DELF model, and initialize classifiers.""" - if FLAGS.delg_global_features: - model = delg_model.Delg( - block3_strides=FLAGS.block3_strides, - name='DELG', - gem_power=FLAGS.delg_gem_power, - embedding_layer_dim=FLAGS.delg_embedding_layer_dim, - scale_factor_init=FLAGS.delg_scale_factor_init, - arcface_margin=FLAGS.delg_arcface_margin, - use_dim_reduction=FLAGS.use_autoencoder, - reduced_dimension=FLAGS.autoencoder_dimensions, - dim_expand_channels=FLAGS.local_feature_map_channels) - else: - model = delf_model.Delf( - block3_strides=FLAGS.block3_strides, - name='DELF', - use_dim_reduction=FLAGS.use_autoencoder, - reduced_dimension=FLAGS.autoencoder_dimensions, - dim_expand_channels=FLAGS.local_feature_map_channels) - model.init_classifiers(num_classes) - return model - - -def _learning_rate_schedule(global_step_value, max_iters, initial_lr): - """Calculates learning_rate with linear decay. - - Args: - global_step_value: int, global step. - max_iters: int, maximum iterations. - initial_lr: float, initial learning rate. - - Returns: - lr: float, learning rate. - """ - lr = initial_lr * (1.0 - global_step_value / max_iters) - return lr - - -def main(argv): - if len(argv) > 1: - raise app.UsageError('Too many command-line arguments.') - - #------------------------------------------------------------- - # Log flags used. - logging.info('Running training script with\n') - logging.info('logdir= %s', FLAGS.logdir) - logging.info('initial_lr= %f', FLAGS.initial_lr) - logging.info('block3_strides= %s', str(FLAGS.block3_strides)) - - # ------------------------------------------------------------ - # Create the strategy. - strategy = tf.distribute.MirroredStrategy() - logging.info('Number of devices: %d', strategy.num_replicas_in_sync) - if FLAGS.debug: - print('Number of devices:', strategy.num_replicas_in_sync) - - max_iters = FLAGS.max_iters - global_batch_size = FLAGS.batch_size - image_size = FLAGS.image_size - num_eval_batches = int(50000 / global_batch_size) - report_interval = 100 - eval_interval = 1000 - save_interval = 1000 - - initial_lr = FLAGS.initial_lr - - clip_val = tf.constant(10.0) - - if FLAGS.debug: - tf.config.run_functions_eagerly(True) - global_batch_size = 4 - max_iters = 100 - num_eval_batches = 1 - save_interval = 1 - report_interval = 10 - - # Determine the number of classes based on the version of the dataset. - gld_info = gld.GoogleLandmarksInfo() - num_classes = gld_info.num_classes[FLAGS.dataset_version] - - # ------------------------------------------------------------ - # Create the distributed train/validation sets. - train_dataset = gld.CreateDataset( - file_pattern=FLAGS.train_file_pattern, - batch_size=global_batch_size, - image_size=image_size, - augmentation=FLAGS.use_augmentation, - seed=FLAGS.seed) - validation_dataset = gld.CreateDataset( - file_pattern=FLAGS.validation_file_pattern, - batch_size=global_batch_size, - image_size=image_size, - augmentation=False, - seed=FLAGS.seed) - - train_dist_dataset = strategy.experimental_distribute_dataset(train_dataset) - validation_dist_dataset = strategy.experimental_distribute_dataset( - validation_dataset) - - train_iter = iter(train_dist_dataset) - validation_iter = iter(validation_dist_dataset) - - # Create a checkpoint directory to store the checkpoints. - checkpoint_prefix = os.path.join(FLAGS.logdir, 'delf_tf2-ckpt') - - # ------------------------------------------------------------ - # Finally, we do everything in distributed scope. - with strategy.scope(): - # Compute loss. - # Set reduction to `none` so we can do the reduction afterwards and divide - # by global batch size. - loss_object = tf.keras.losses.SparseCategoricalCrossentropy( - from_logits=True, reduction=tf.keras.losses.Reduction.NONE) - - def compute_loss(labels, predictions): - per_example_loss = loss_object(labels, predictions) - return tf.nn.compute_average_loss( - per_example_loss, global_batch_size=global_batch_size) - - # Set up metrics. - desc_validation_loss = tf.keras.metrics.Mean(name='desc_validation_loss') - attn_validation_loss = tf.keras.metrics.Mean(name='attn_validation_loss') - desc_train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy( - name='desc_train_accuracy') - attn_train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy( - name='attn_train_accuracy') - desc_validation_accuracy = tf.keras.metrics.SparseCategoricalAccuracy( - name='desc_validation_accuracy') - attn_validation_accuracy = tf.keras.metrics.SparseCategoricalAccuracy( - name='attn_validation_accuracy') - - # ------------------------------------------------------------ - # Setup DELF model and optimizer. - model = create_model(num_classes) - logging.info('Model, datasets loaded.\nnum_classes= %d', num_classes) - - optimizer = tf.keras.optimizers.SGD(learning_rate=initial_lr, momentum=0.9) - - # Setup summary writer. - summary_writer = tf.summary.create_file_writer( - os.path.join(FLAGS.logdir, 'train_logs'), flush_millis=10000) - - # Setup checkpoint directory. - checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model) - manager = tf.train.CheckpointManager( - checkpoint, - checkpoint_prefix, - max_to_keep=10, - keep_checkpoint_every_n_hours=3) - # Restores the checkpoint, if existing. - checkpoint.restore(manager.latest_checkpoint) - - # ------------------------------------------------------------ - # Train step to run on one GPU. - def train_step(inputs): - """Train one batch.""" - images, labels = inputs - # Temporary workaround to avoid some corrupted labels. - labels = tf.clip_by_value(labels, 0, model.num_classes) - - def _backprop_loss(tape, loss, weights): - """Backpropogate losses using clipped gradients. - - Args: - tape: gradient tape. - loss: scalar Tensor, loss value. - weights: keras model weights. - """ - gradients = tape.gradient(loss, weights) - clipped, _ = tf.clip_by_global_norm(gradients, clip_norm=clip_val) - optimizer.apply_gradients(zip(clipped, weights)) - - # Record gradients and loss through backbone. - with tf.GradientTape() as gradient_tape: - # Make a forward pass to calculate prelogits. - (desc_prelogits, attn_prelogits, attn_scores, backbone_blocks, - dim_expanded_features, _) = model.global_and_local_forward_pass(images) - - # Calculate global loss by applying the descriptor classifier. - if FLAGS.delg_global_features: - desc_logits = model.desc_classification(desc_prelogits, labels) - else: - desc_logits = model.desc_classification(desc_prelogits) - desc_loss = compute_loss(labels, desc_logits) - - # Calculate attention loss by applying the attention block classifier. - attn_logits = model.attn_classification(attn_prelogits) - attn_loss = compute_loss(labels, attn_logits) - - # Calculate reconstruction loss between the attention prelogits and the - # backbone. - if FLAGS.use_autoencoder: - block3 = tf.stop_gradient(backbone_blocks['block3']) - reconstruction_loss = tf.math.reduce_mean( - tf.keras.losses.MSE(block3, dim_expanded_features)) - else: - reconstruction_loss = 0 - - # Cumulate global loss, attention loss and reconstruction loss. - total_loss = ( - desc_loss + FLAGS.attention_loss_weight * attn_loss + - FLAGS.reconstruction_loss_weight * reconstruction_loss) - - # Perform backpropagation through the descriptor and attention layers - # together. Note that this will increment the number of iterations of - # "optimizer". - _backprop_loss(gradient_tape, total_loss, model.trainable_weights) - - # Step number, for summary purposes. - global_step = optimizer.iterations - - # Input image-related summaries. - tf.summary.image('batch_images', (images + 1.0) / 2.0, step=global_step) - tf.summary.scalar( - 'image_range/max', tf.reduce_max(images), step=global_step) - tf.summary.scalar( - 'image_range/min', tf.reduce_min(images), step=global_step) - - # Attention and sparsity summaries. - _attention_summaries(attn_scores, global_step) - activations_zero_fractions = { - 'sparsity/%s' % k: tf.nn.zero_fraction(v) - for k, v in backbone_blocks.items() - } - for k, v in activations_zero_fractions.items(): - tf.summary.scalar(k, v, step=global_step) - - # Scaling factor summary for cosine logits for a DELG model. - if FLAGS.delg_global_features: - tf.summary.scalar( - 'desc/scale_factor', model.scale_factor, step=global_step) - - # Record train accuracies. - _record_accuracy(desc_train_accuracy, desc_logits, labels) - _record_accuracy(attn_train_accuracy, attn_logits, labels) - - return desc_loss, attn_loss, reconstruction_loss - - # ------------------------------------------------------------ - def validation_step(inputs): - """Validate one batch.""" - images, labels = inputs - labels = tf.clip_by_value(labels, 0, model.num_classes) - - # Get descriptor predictions. - blocks = {} - prelogits = model.backbone( - images, intermediates_dict=blocks, training=False) - if FLAGS.delg_global_features: - logits = model.desc_classification(prelogits, labels, training=False) - else: - logits = model.desc_classification(prelogits, training=False) - softmax_probabilities = tf.keras.layers.Softmax()(logits) - - validation_loss = loss_object(labels, logits) - desc_validation_loss.update_state(validation_loss) - desc_validation_accuracy.update_state(labels, softmax_probabilities) - - # Get attention predictions. - block3 = blocks['block3'] # pytype: disable=key-error - prelogits, _, _ = model.attention(block3, training=False) - - logits = model.attn_classification(prelogits, training=False) - softmax_probabilities = tf.keras.layers.Softmax()(logits) - - validation_loss = loss_object(labels, logits) - attn_validation_loss.update_state(validation_loss) - attn_validation_accuracy.update_state(labels, softmax_probabilities) - - return desc_validation_accuracy.result(), attn_validation_accuracy.result( - ) - - # `run` replicates the provided computation and runs it - # with the distributed input. - @tf.function - def distributed_train_step(dataset_inputs): - """Get the actual losses.""" - # Each (desc, attn) is a list of 3 losses - crossentropy, reg, total. - desc_per_replica_loss, attn_per_replica_loss, recon_per_replica_loss = ( - strategy.run(train_step, args=(dataset_inputs,))) - - # Reduce over the replicas. - desc_global_loss = strategy.reduce( - tf.distribute.ReduceOp.SUM, desc_per_replica_loss, axis=None) - attn_global_loss = strategy.reduce( - tf.distribute.ReduceOp.SUM, attn_per_replica_loss, axis=None) - recon_global_loss = strategy.reduce( - tf.distribute.ReduceOp.SUM, recon_per_replica_loss, axis=None) - - return desc_global_loss, attn_global_loss, recon_global_loss - - @tf.function - def distributed_validation_step(dataset_inputs): - return strategy.run(validation_step, args=(dataset_inputs,)) - - # ------------------------------------------------------------ - # *** TRAIN LOOP *** - with summary_writer.as_default(): - record_cond = lambda: tf.equal(optimizer.iterations % report_interval, 0) - with tf.summary.record_if(record_cond): - global_step_value = optimizer.iterations.numpy() - - # TODO(dananghel): try to load pretrained weights at backbone creation. - # Load pretrained weights for ResNet50 trained on ImageNet. - if (FLAGS.imagenet_checkpoint is not None) and (not global_step_value): - logging.info('Attempting to load ImageNet pretrained weights.') - input_batch = next(train_iter) - _, _, _ = distributed_train_step(input_batch) - model.backbone.restore_weights(FLAGS.imagenet_checkpoint) - logging.info('Done.') - else: - logging.info('Skip loading ImageNet pretrained weights.') - if FLAGS.debug: - model.backbone.log_weights() - - last_summary_step_value = None - last_summary_time = None - while global_step_value < max_iters: - # input_batch : images(b, h, w, c), labels(b,). - try: - input_batch = next(train_iter) - except tf.errors.OutOfRangeError: - # Break if we run out of data in the dataset. - logging.info('Stopping training at global step %d, no more data', - global_step_value) - break - - # Set learning rate and run the training step over num_gpu gpus. - optimizer.learning_rate = _learning_rate_schedule( - optimizer.iterations.numpy(), max_iters, initial_lr) - desc_dist_loss, attn_dist_loss, recon_dist_loss = ( - distributed_train_step(input_batch)) - - # Step number, to be used for summary/logging. - global_step = optimizer.iterations - global_step_value = global_step.numpy() - - # LR, losses and accuracies summaries. - tf.summary.scalar( - 'learning_rate', optimizer.learning_rate, step=global_step) - tf.summary.scalar( - 'loss/desc/crossentropy', desc_dist_loss, step=global_step) - tf.summary.scalar( - 'loss/attn/crossentropy', attn_dist_loss, step=global_step) - if FLAGS.use_autoencoder: - tf.summary.scalar( - 'loss/recon/mse', recon_dist_loss, step=global_step) - - tf.summary.scalar( - 'train_accuracy/desc', - desc_train_accuracy.result(), - step=global_step) - tf.summary.scalar( - 'train_accuracy/attn', - attn_train_accuracy.result(), - step=global_step) - - # Summary for number of global steps taken per second. - current_time = time.time() - if (last_summary_step_value is not None and - last_summary_time is not None): - tf.summary.scalar( - 'global_steps_per_sec', - (global_step_value - last_summary_step_value) / - (current_time - last_summary_time), - step=global_step) - if tf.summary.should_record_summaries().numpy(): - last_summary_step_value = global_step_value - last_summary_time = current_time - - # Print to console if running locally. - if FLAGS.debug: - if global_step_value % report_interval == 0: - print(global_step.numpy()) - print('desc:', desc_dist_loss.numpy()) - print('attn:', attn_dist_loss.numpy()) - - # Validate once in {eval_interval*n, n \in N} steps. - if global_step_value % eval_interval == 0: - for i in range(num_eval_batches): - try: - validation_batch = next(validation_iter) - desc_validation_result, attn_validation_result = ( - distributed_validation_step(validation_batch)) - except tf.errors.OutOfRangeError: - logging.info('Stopping eval at batch %d, no more data', i) - break - - # Log validation results to tensorboard. - tf.summary.scalar( - 'validation/desc', desc_validation_result, step=global_step) - tf.summary.scalar( - 'validation/attn', attn_validation_result, step=global_step) - - logging.info('\nValidation(%f)\n', global_step_value) - logging.info(': desc: %f\n', desc_validation_result.numpy()) - logging.info(': attn: %f\n', attn_validation_result.numpy()) - # Print to console. - if FLAGS.debug: - print('Validation: desc:', desc_validation_result.numpy()) - print(' : attn:', attn_validation_result.numpy()) - - # Save checkpoint once (each save_interval*n, n \in N) steps, or if - # this is the last iteration. - # TODO(andrearaujo): save only in one of the two ways. They are - # identical, the only difference is that the manager adds some extra - # prefixes and variables (eg, optimizer variables). - if (global_step_value % save_interval - == 0) or (global_step_value >= max_iters): - save_path = manager.save(checkpoint_number=global_step_value) - logging.info('Saved (%d) at %s', global_step_value, save_path) - - file_path = '%s/delf_weights' % FLAGS.logdir - model.save_weights(file_path, save_format='tf') - logging.info('Saved weights (%d) at %s', global_step_value, - file_path) - - # Reset metrics for next step. - desc_train_accuracy.reset_states() - attn_train_accuracy.reset_states() - desc_validation_loss.reset_states() - attn_validation_loss.reset_states() - desc_validation_accuracy.reset_states() - attn_validation_accuracy.reset_states() - - logging.info('Finished training for %d steps.', max_iters) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/delf/delf/python/utils.py b/research/delf/delf/python/utils.py deleted file mode 100644 index 46b62cbdf31..00000000000 --- a/research/delf/delf/python/utils.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright 2020 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Helper functions for DELF.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -from PIL import Image -from PIL import ImageFile -import tensorflow as tf - -# To avoid PIL crashing for truncated (corrupted) images. -ImageFile.LOAD_TRUNCATED_IMAGES = True - - -def RgbLoader(path): - """Helper function to read image with PIL. - - Args: - path: Path to image to be loaded. - - Returns: - PIL image in RGB format. - """ - with tf.io.gfile.GFile(path, 'rb') as f: - img = Image.open(f) - return img.convert('RGB') - - -def ResizeImage(image, config, resize_factor=1.0): - """Resizes image according to config. - - Args: - image: Uint8 array with shape (height, width, 3). - config: DelfConfig proto containing the model configuration. - resize_factor: Optional float resize factor for the input image. If given, - the maximum and minimum allowed image sizes in `config` are scaled by this - factor. Must be non-negative. - - Returns: - resized_image: Uint8 array with resized image. - scale_factors: 2D float array, with factors used for resizing along height - and width (If upscaling, larger than 1; if downscaling, smaller than 1). - - Raises: - ValueError: If `image` has incorrect number of dimensions/channels. - """ - if resize_factor < 0.0: - raise ValueError('negative resize_factor is not allowed: %f' % - resize_factor) - if image.ndim != 3: - raise ValueError('image has incorrect number of dimensions: %d' % - image.ndims) - height, width, channels = image.shape - - # Take into account resize factor. - max_image_size = resize_factor * config.max_image_size - min_image_size = resize_factor * config.min_image_size - - if channels != 3: - raise ValueError('image has incorrect number of channels: %d' % channels) - - largest_side = max(width, height) - - if max_image_size >= 0 and largest_side > max_image_size: - scale_factor = max_image_size / largest_side - elif min_image_size >= 0 and largest_side < min_image_size: - scale_factor = min_image_size / largest_side - elif config.use_square_images and (height != width): - scale_factor = 1.0 - else: - # No resizing needed, early return. - return image, np.ones(2, dtype=float) - - # Note that new_shape is in (width, height) format (PIL convention), while - # scale_factors are in (height, width) convention (NumPy convention). - if config.use_square_images: - new_shape = (int(round(largest_side * scale_factor)), - int(round(largest_side * scale_factor))) - else: - new_shape = (int(round(width * scale_factor)), - int(round(height * scale_factor))) - - scale_factors = np.array([new_shape[1] / height, new_shape[0] / width], - dtype=float) - - pil_image = Image.fromarray(image) - resized_image = np.array(pil_image.resize(new_shape, resample=Image.BILINEAR)) - - return resized_image, scale_factors diff --git a/research/delf/delf/python/utils_test.py b/research/delf/delf/python/utils_test.py deleted file mode 100644 index a07d86d75d8..00000000000 --- a/research/delf/delf/python/utils_test.py +++ /dev/null @@ -1,103 +0,0 @@ -# Copyright 2020 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for helper utilities.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl.testing import parameterized -import numpy as np -import tensorflow as tf - -from delf import delf_config_pb2 -from delf import utils - - -class UtilsTest(tf.test.TestCase, parameterized.TestCase): - - @parameterized.named_parameters( - ('Max-1Min-1', -1, -1, 1.0, False, [4, 2, 3], [1.0, 1.0]), - ('Max-1Min-1Square', -1, -1, 1.0, True, [4, 4, 3], [1.0, 2.0]), - ('Max2Min-1', 2, -1, 1.0, False, [2, 1, 3], [0.5, 0.5]), - ('Max2Min-1Square', 2, -1, 1.0, True, [2, 2, 3], [0.5, 1.0]), - ('Max8Min-1', 8, -1, 1.0, False, [4, 2, 3], [1.0, 1.0]), - ('Max8Min-1Square', 8, -1, 1.0, True, [4, 4, 3], [1.0, 2.0]), - ('Max-1Min1', -1, 1, 1.0, False, [4, 2, 3], [1.0, 1.0]), - ('Max-1Min1Square', -1, 1, 1.0, True, [4, 4, 3], [1.0, 2.0]), - ('Max-1Min8', -1, 8, 1.0, False, [8, 4, 3], [2.0, 2.0]), - ('Max-1Min8Square', -1, 8, 1.0, True, [8, 8, 3], [2.0, 4.0]), - ('Max16Min8', 16, 8, 1.0, False, [8, 4, 3], [2.0, 2.0]), - ('Max16Min8Square', 16, 8, 1.0, True, [8, 8, 3], [2.0, 4.0]), - ('Max2Min2', 2, 2, 1.0, False, [2, 1, 3], [0.5, 0.5]), - ('Max2Min2Square', 2, 2, 1.0, True, [2, 2, 3], [0.5, 1.0]), - ('Max-1Min-1Factor0.5', -1, -1, 0.5, False, [4, 2, 3], [1.0, 1.0]), - ('Max-1Min-1Factor0.5Square', -1, -1, 0.5, True, [4, 4, 3], [1.0, 2.0]), - ('Max2Min-1Factor2.0', 2, -1, 2.0, False, [4, 2, 3], [1.0, 1.0]), - ('Max2Min-1Factor2.0Square', 2, -1, 2.0, True, [4, 4, 3], [1.0, 2.0]), - ('Max-1Min8Factor0.5', -1, 8, 0.5, False, [4, 2, 3], [1.0, 1.0]), - ('Max-1Min8Factor0.5Square', -1, 8, 0.5, True, [4, 4, 3], [1.0, 2.0]), - ('Max-1Min8Factor0.25', -1, 8, 0.25, False, [4, 2, 3], [1.0, 1.0]), - ('Max-1Min8Factor0.25Square', -1, 8, 0.25, True, [4, 4, 3], [1.0, 2.0]), - ('Max2Min2Factor2.0', 2, 2, 2.0, False, [4, 2, 3], [1.0, 1.0]), - ('Max2Min2Factor2.0Square', 2, 2, 2.0, True, [4, 4, 3], [1.0, 2.0]), - ('Max16Min8Factor0.5', 16, 8, 0.5, False, [4, 2, 3], [1.0, 1.0]), - ('Max16Min8Factor0.5Square', 16, 8, 0.5, True, [4, 4, 3], [1.0, 2.0]), - ) - def testResizeImageWorks(self, max_image_size, min_image_size, resize_factor, - square_output, expected_shape, - expected_scale_factors): - # Construct image of size 4x2x3. - image = np.array([[[0, 0, 0], [1, 1, 1]], [[2, 2, 2], [3, 3, 3]], - [[4, 4, 4], [5, 5, 5]], [[6, 6, 6], [7, 7, 7]]], - dtype='uint8') - - # Set up config. - config = delf_config_pb2.DelfConfig( - max_image_size=max_image_size, - min_image_size=min_image_size, - use_square_images=square_output) - - resized_image, scale_factors = utils.ResizeImage(image, config, - resize_factor) - self.assertAllEqual(resized_image.shape, expected_shape) - self.assertAllClose(scale_factors, expected_scale_factors) - - @parameterized.named_parameters( - ('Max2Min2', 2, 2, 1.0, False, [2, 1, 3], [0.666666, 0.5]), - ('Max2Min2Square', 2, 2, 1.0, True, [2, 2, 3], [0.666666, 1.0]), - ) - def testResizeImageRoundingWorks(self, max_image_size, min_image_size, - resize_factor, square_output, expected_shape, - expected_scale_factors): - # Construct image of size 3x2x3. - image = np.array([[[0, 0, 0], [1, 1, 1]], [[2, 2, 2], [3, 3, 3]], - [[4, 4, 4], [5, 5, 5]]], - dtype='uint8') - - # Set up config. - config = delf_config_pb2.DelfConfig( - max_image_size=max_image_size, - min_image_size=min_image_size, - use_square_images=square_output) - - resized_image, scale_factors = utils.ResizeImage(image, config, - resize_factor) - self.assertAllEqual(resized_image.shape, expected_shape) - self.assertAllClose(scale_factors, expected_scale_factors) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/delf/python/whiten.py b/research/delf/delf/python/whiten.py deleted file mode 100644 index d2c72d9f17e..00000000000 --- a/research/delf/delf/python/whiten.py +++ /dev/null @@ -1,125 +0,0 @@ -# Copyright 2021 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Whitening learning functions.""" - -import os - -import numpy as np - - -def apply_whitening(descriptors, - mean_descriptor_vector, - projection, - output_dim=None): - """Applies the whitening to the descriptors as a post-processing step. - - Args: - descriptors: [N, D] NumPy array of L2-normalized descriptors to be - post-processed. - mean_descriptor_vector: Mean descriptor vector. - projection: Whitening projection matrix. - output_dim: Integer, parameter for the dimensionality reduction. If - `output_dim` is None, the dimensionality reduction is not performed. - - Returns: - descriptors_whitened: [N, output_dim] NumPy array of L2-normalized - descriptors `descriptors` after whitening application. - """ - eps = 1e-6 - if output_dim is None: - output_dim = projection.shape[0] - - descriptors = np.dot(projection[:output_dim, :], - descriptors - mean_descriptor_vector) - descriptors_whitened = descriptors / ( - np.linalg.norm(descriptors, ord=2, axis=0, keepdims=True) + eps) - return descriptors_whitened - - -def learn_whitening(descriptors, qidxs, pidxs): - """Learning the post-processing of fine-tuned descriptor vectors. - - This method of whitening learning leverages the provided labeled data and - uses linear discriminant projections. The projection is decomposed into two - parts: whitening and rotation. The whitening part is the inverse of the - square-root of the intraclass (matching pairs) covariance matrix. The - rotation part is the PCA of the interclass (non-matching pairs) covariance - matrix in the whitened space. The described approach acts as a - post-processing step, equivalently, once the fine-tuning of the CNN is - finished. For more information about the method refer to the section 3.4 - of https://arxiv.org/pdf/1711.02512.pdf. - - Args: - descriptors: [N, D] NumPy array of L2-normalized descriptors. - qidxs: List of query indexes. - pidxs: List of positive pairs indexes. - - Returns: - mean_descriptor_vector: [N, 1] NumPy array, mean descriptor vector. - projection: [N, N] NumPy array, whitening projection matrix. - """ - # Calculating the mean descriptor vector, which is used to perform centering. - mean_descriptor_vector = descriptors[:, qidxs].mean(axis=1, keepdims=True) - # Interclass (matching pairs) difference. - interclass_difference = descriptors[:, qidxs] - descriptors[:, pidxs] - covariance_matrix = ( - np.dot(interclass_difference, interclass_difference.T) / - interclass_difference.shape[1]) - - # Whitening part. - projection = np.linalg.inv(cholesky(covariance_matrix)) - - projected_descriptors = np.dot(projection, - descriptors - mean_descriptor_vector) - non_matching_covariance_matrix = np.dot(projected_descriptors, - projected_descriptors.T) - eigval, eigvec = np.linalg.eig(non_matching_covariance_matrix) - order = eigval.argsort()[::-1] - eigvec = eigvec[:, order] - - # Rotational part. - projection = np.dot(eigvec.T, projection) - return mean_descriptor_vector, projection - - -def cholesky(matrix): - """Cholesky decomposition. - - Cholesky decomposition suitable for non-positive definite matrices: involves - adding a small value `alpha` on the matrix diagonal until the matrix - becomes positive definite. - - Args: - matrix: [K, K] Square matrix to be decomposed. - - Returns: - decomposition: [K, K] Upper-triangular Cholesky factor of `matrix`, - a matrix with real and positive diagonal entries. - """ - alpha = 0 - while True: - try: - # If the input parameter matrix is not positive-definite, - # the decomposition fails and we iteratively add a small value `alpha` on - # the matrix diagonal. - decomposition = np.linalg.cholesky(matrix + alpha * np.eye(*matrix.shape)) - return decomposition - except np.linalg.LinAlgError: - if alpha == 0: - alpha = 1e-10 - else: - alpha *= 10 - print(">>>> {}::cholesky: Matrix is not positive definite, adding {:.0e} " - "on the diagonal".format(os.path.basename(__file__), alpha)) diff --git a/research/delf/delf/python/whiten_test.py b/research/delf/delf/python/whiten_test.py deleted file mode 100644 index 52cc51e65d1..00000000000 --- a/research/delf/delf/python/whiten_test.py +++ /dev/null @@ -1,73 +0,0 @@ -# Lint as: python3 -# Copyright 2021 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for whitening module.""" - -import numpy as np -import tensorflow as tf - -from delf import whiten - - -class WhitenTest(tf.test.TestCase): - - def testApplyWhitening(self): - # Testing the application of the learned whitening. - vectors = np.array([[0.14022471, 0.96360618], [0.37601032, 0.25528411]]) - # Learn whitening for the `vectors`. First element in the `vectors` is - # viewed is the example query and the second element is the corresponding - # positive. - mean_vector, projection = whiten.learn_whitening(vectors, [0], [1]) - # Apply the computed whitening. - whitened_vectors = whiten.apply_whitening(vectors, mean_vector, projection) - expected_whitened_vectors = np.array([[0., 9.99999000e-01], - [0., -2.81240452e-13]]) - # Compare the obtained whitened vectors with the expected result. - self.assertAllClose(whitened_vectors, expected_whitened_vectors) - - def testLearnWhitening(self): - # Testing whitening learning function. - descriptors = np.array([[0.14022471, 0.96360618], [0.37601032, 0.25528411]]) - # Obtain the mean descriptor vector and the projection matrix. - mean_vector, projection = whiten.learn_whitening(descriptors, [0], [1]) - expected_mean_vector = np.array([[0.14022471], [0.37601032]]) - expected_projection = np.array([[1.18894378e+00, -1.74326044e-01], - [1.45071361e+04, 9.89421193e+04]]) - # Check that the both calculated values are close to the expected values. - self.assertAllClose(mean_vector, expected_mean_vector) - self.assertAllClose(projection, expected_projection) - - def testCholeskyPositiveDefinite(self): - # Testing the Cholesky decomposition for the positive definite matrix. - descriptors = np.array([[1, -2j], [2j, 5]]) - output = whiten.cholesky(descriptors) - expected_output = np.array([[1. + 0.j, 0. + 0.j], [0. + 2.j, 1. + 0.j]]) - # Check that the expected output is obtained. - self.assertAllClose(output, expected_output) - # Check that the properties of the Cholesky decomposition are satisfied. - self.assertAllClose(np.matmul(output, output.T.conj()), descriptors) - - def testCholeskyNonPositiveDefinite(self): - # Testing the Cholesky decomposition for a non-positive definite matrix. - input_matrix = np.array([[1., 2.], [-2., 1.]]) - decomposition = whiten.cholesky(input_matrix) - expected_output = np.array([[2., -2.], [-2., 2.]]) - # Check that the properties of the Cholesky decomposition are satisfied. - self.assertAllClose( - np.matmul(decomposition, decomposition.T), expected_output) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/delf/setup.py b/research/delf/setup.py deleted file mode 100644 index f0ec02523ec..00000000000 --- a/research/delf/setup.py +++ /dev/null @@ -1,37 +0,0 @@ -# Copyright 2017 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Setup script for delf.""" - -from setuptools import setup, find_packages - -install_requires = [ - 'absl-py >= 0.7.1', - 'protobuf >= 3.8.0', - 'pandas >= 0.24.2', - 'numpy >= 1.16.1', - 'scipy >= 1.2.2', - 'tensorflow >= 2.2.0', - 'tf_slim >= 1.1', - 'tensorflow_probability >= 0.9.0', -] - -setup( - name='delf', - version='2.0', - include_package_data=True, - packages=find_packages(), - install_requires=install_requires, - description='DELF (DEep Local Features)', -) diff --git a/research/efficient-hrl/README.md b/research/efficient-hrl/README.md deleted file mode 100755 index 6c454c687a3..00000000000 --- a/research/efficient-hrl/README.md +++ /dev/null @@ -1,65 +0,0 @@ -![TensorFlow Requirement: 1.x](https://img.shields.io/badge/TensorFlow%20Requirement-1.x-brightgreen) -![TensorFlow 2 Not Supported](https://img.shields.io/badge/TensorFlow%202%20Not%20Supported-%E2%9C%95-red.svg) - -Code for performing Hierarchical RL based on the following publications: - -"Data-Efficient Hierarchical Reinforcement Learning" by -Ofir Nachum, Shixiang (Shane) Gu, Honglak Lee, and Sergey Levine -(https://arxiv.org/abs/1805.08296). - -"Near-Optimal Representation Learning for Hierarchical Reinforcement Learning" -by Ofir Nachum, Shixiang (Shane) Gu, Honglak Lee, and Sergey Levine -(https://arxiv.org/abs/1810.01257). - - -Requirements: -* TensorFlow (see http://www.tensorflow.org for how to install/upgrade) -* Gin Config (see https://github.com/google/gin-config) -* Tensorflow Agents (see https://github.com/tensorflow/agents) -* OpenAI Gym (see http://gym.openai.com/docs, be sure to install MuJoCo as well) -* NumPy (see http://www.numpy.org/) - - -Quick Start: - -Run a training job based on the original HIRO paper on Ant Maze: - -``` -python scripts/local_train.py test1 hiro_orig ant_maze base_uvf suite -``` - -Run a continuous evaluation job for that experiment: - -``` -python scripts/local_eval.py test1 hiro_orig ant_maze base_uvf suite -``` - -To run the same experiment with online representation learning (the -"Near-Optimal" paper), change `hiro_orig` to `hiro_repr`. -You can also run with `hiro_xy` to run the same experiment with HIRO on only the -xy coordinates of the agent. - -To run on other environments, change `ant_maze` to something else; e.g., -`ant_push_multi`, `ant_fall_multi`, etc. See `context/configs/*` for other options. - - -Basic Code Guide: - -The code for training resides in train.py. The code trains a lower-level policy -(a UVF agent in the code) and a higher-level policy (a MetaAgent in the code) -concurrently. The higher-level policy communicates goals to the lower-level -policy. In the code, this is called a context. Not only does the lower-level -policy act with respect to a context (a higher-level specified goal), but the -higher-level policy also acts with respect to an environment-specified context -(corresponding to the navigation target location associated with the task). -Therefore, in `context/configs/*` you will find both specifications for task setup -as well as goal configurations. Most remaining hyperparameters used for -training/evaluation may be found in `configs/*`. - -NOTE: Not all the code corresponding to the "Near-Optimal" paper is included. -Namely, changes to low-level policy training proposed in the paper (discounting -and auxiliary rewards) are not implemented here. Performance should not change -significantly. - - -Maintained by Ofir Nachum (ofirnachum). diff --git a/research/efficient-hrl/agent.py b/research/efficient-hrl/agent.py deleted file mode 100644 index 0028ddffa0d..00000000000 --- a/research/efficient-hrl/agent.py +++ /dev/null @@ -1,774 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A UVF agent. -""" - -import tensorflow as tf -import gin.tf -from agents import ddpg_agent -# pylint: disable=unused-import -import cond_fn -from utils import utils as uvf_utils -from context import gin_imports -# pylint: enable=unused-import -slim = tf.contrib.slim - - -@gin.configurable -class UvfAgentCore(object): - """Defines basic functions for UVF agent. Must be inherited with an RL agent. - - Used as lower-level agent. - """ - - def __init__(self, - observation_spec, - action_spec, - tf_env, - tf_context, - step_cond_fn=cond_fn.env_transition, - reset_episode_cond_fn=cond_fn.env_restart, - reset_env_cond_fn=cond_fn.false_fn, - metrics=None, - **base_agent_kwargs): - """Constructs a UVF agent. - - Args: - observation_spec: A TensorSpec defining the observations. - action_spec: A BoundedTensorSpec defining the actions. - tf_env: A Tensorflow environment object. - tf_context: A Context class. - step_cond_fn: A function indicating whether to increment the num of steps. - reset_episode_cond_fn: A function indicating whether to restart the - episode, resampling the context. - reset_env_cond_fn: A function indicating whether to perform a manual reset - of the environment. - metrics: A list of functions that evaluate metrics of the agent. - **base_agent_kwargs: A dictionary of parameters for base RL Agent. - Raises: - ValueError: If 'dqda_clipping' is < 0. - """ - self._step_cond_fn = step_cond_fn - self._reset_episode_cond_fn = reset_episode_cond_fn - self._reset_env_cond_fn = reset_env_cond_fn - self.metrics = metrics - - # expose tf_context methods - self.tf_context = tf_context(tf_env=tf_env) - self.set_replay = self.tf_context.set_replay - self.sample_contexts = self.tf_context.sample_contexts - self.compute_rewards = self.tf_context.compute_rewards - self.gamma_index = self.tf_context.gamma_index - self.context_specs = self.tf_context.context_specs - self.context_as_action_specs = self.tf_context.context_as_action_specs - self.init_context_vars = self.tf_context.create_vars - - self.env_observation_spec = observation_spec[0] - merged_observation_spec = (uvf_utils.merge_specs( - (self.env_observation_spec,) + self.context_specs),) - self._context_vars = dict() - self._action_vars = dict() - - self.BASE_AGENT_CLASS.__init__( - self, - observation_spec=merged_observation_spec, - action_spec=action_spec, - **base_agent_kwargs - ) - - def set_meta_agent(self, agent=None): - self._meta_agent = agent - - @property - def meta_agent(self): - return self._meta_agent - - def actor_loss(self, states, actions, rewards, discounts, - next_states): - """Returns the next action for the state. - - Args: - state: A [num_state_dims] tensor representing a state. - context: A list of [num_context_dims] tensor representing a context. - Returns: - A [num_action_dims] tensor representing the action. - """ - return self.BASE_AGENT_CLASS.actor_loss(self, states) - - def action(self, state, context=None): - """Returns the next action for the state. - - Args: - state: A [num_state_dims] tensor representing a state. - context: A list of [num_context_dims] tensor representing a context. - Returns: - A [num_action_dims] tensor representing the action. - """ - merged_state = self.merged_state(state, context) - return self.BASE_AGENT_CLASS.action(self, merged_state) - - def actions(self, state, context=None): - """Returns the next action for the state. - - Args: - state: A [-1, num_state_dims] tensor representing a state. - context: A list of [-1, num_context_dims] tensor representing a context. - Returns: - A [-1, num_action_dims] tensor representing the action. - """ - merged_states = self.merged_states(state, context) - return self.BASE_AGENT_CLASS.actor_net(self, merged_states) - - def log_probs(self, states, actions, state_reprs, contexts=None): - assert contexts is not None - batch_dims = [tf.shape(states)[0], tf.shape(states)[1]] - contexts = self.tf_context.context_multi_transition_fn( - contexts, states=tf.to_float(state_reprs)) - - flat_states = tf.reshape(states, - [batch_dims[0] * batch_dims[1], states.shape[-1]]) - flat_contexts = [tf.reshape(tf.cast(context, states.dtype), - [batch_dims[0] * batch_dims[1], context.shape[-1]]) - for context in contexts] - flat_pred_actions = self.actions(flat_states, flat_contexts) - pred_actions = tf.reshape(flat_pred_actions, - batch_dims + [flat_pred_actions.shape[-1]]) - - error = tf.square(actions - pred_actions) - spec_range = (self._action_spec.maximum - self._action_spec.minimum) / 2 - normalized_error = tf.cast(error, tf.float64) / tf.constant(spec_range) ** 2 - return -normalized_error - - @gin.configurable('uvf_add_noise_fn') - def add_noise_fn(self, action_fn, stddev=1.0, debug=False, - clip=True, global_step=None): - """Returns the action_fn with additive Gaussian noise. - - Args: - action_fn: A callable(`state`, `context`) which returns a - [num_action_dims] tensor representing a action. - stddev: stddev for the Ornstein-Uhlenbeck noise. - debug: Print debug messages. - Returns: - A [num_action_dims] action tensor. - """ - if global_step is not None: - stddev *= tf.maximum( # Decay exploration during training. - tf.train.exponential_decay(1.0, global_step, 1e6, 0.8), 0.5) - def noisy_action_fn(state, context=None): - """Noisy action fn.""" - action = action_fn(state, context) - if debug: - action = uvf_utils.tf_print( - action, [action], - message='[add_noise_fn] pre-noise action', - first_n=100) - noise_dist = tf.distributions.Normal(tf.zeros_like(action), - tf.ones_like(action) * stddev) - noise = noise_dist.sample() - action += noise - if debug: - action = uvf_utils.tf_print( - action, [action], - message='[add_noise_fn] post-noise action', - first_n=100) - if clip: - action = uvf_utils.clip_to_spec(action, self._action_spec) - return action - return noisy_action_fn - - def merged_state(self, state, context=None): - """Returns the merged state from the environment state and contexts. - - Args: - state: A [num_state_dims] tensor representing a state. - context: A list of [num_context_dims] tensor representing a context. - If None, use the internal context. - Returns: - A [num_merged_state_dims] tensor representing the merged state. - """ - if context is None: - context = list(self.context_vars) - state = tf.concat([state,] + context, axis=-1) - self._validate_states(self._batch_state(state)) - return state - - def merged_states(self, states, contexts=None): - """Returns the batch merged state from the batch env state and contexts. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - contexts: A list of [batch_size, num_context_dims] tensor - representing a batch of contexts. If None, - use the internal context. - Returns: - A [batch_size, num_merged_state_dims] tensor representing the batch - of merged states. - """ - if contexts is None: - contexts = [tf.tile(tf.expand_dims(context, axis=0), - (tf.shape(states)[0], 1)) for - context in self.context_vars] - states = tf.concat([states,] + contexts, axis=-1) - self._validate_states(states) - return states - - def unmerged_states(self, merged_states): - """Returns the batch state and contexts from the batch merged state. - - Args: - merged_states: A [batch_size, num_merged_state_dims] tensor - representing a batch of merged states. - Returns: - A [batch_size, num_state_dims] tensor and a list of - [batch_size, num_context_dims] tensors representing the batch state - and contexts respectively. - """ - self._validate_states(merged_states) - num_state_dims = self.env_observation_spec.shape.as_list()[0] - num_context_dims_list = [c.shape.as_list()[0] for c in self.context_specs] - states = merged_states[:, :num_state_dims] - contexts = [] - i = num_state_dims - for num_context_dims in num_context_dims_list: - contexts.append(merged_states[:, i: i+num_context_dims]) - i += num_context_dims - return states, contexts - - def sample_random_actions(self, batch_size=1): - """Return random actions. - - Args: - batch_size: Batch size. - Returns: - A [batch_size, num_action_dims] tensor representing the batch of actions. - """ - actions = tf.concat( - [ - tf.random_uniform( - shape=(batch_size, 1), - minval=self._action_spec.minimum[i], - maxval=self._action_spec.maximum[i]) - for i in range(self._action_spec.shape[0].value) - ], - axis=1) - return actions - - def clip_actions(self, actions): - """Clip actions to spec. - - Args: - actions: A [batch_size, num_action_dims] tensor representing - the batch of actions. - Returns: - A [batch_size, num_action_dims] tensor representing the batch - of clipped actions. - """ - actions = tf.concat( - [ - tf.clip_by_value( - actions[:, i:i+1], - self._action_spec.minimum[i], - self._action_spec.maximum[i]) - for i in range(self._action_spec.shape[0].value) - ], - axis=1) - return actions - - def mix_contexts(self, contexts, insert_contexts, indices): - """Mix two contexts based on indices. - - Args: - contexts: A list of [batch_size, num_context_dims] tensor representing - the batch of contexts. - insert_contexts: A list of [batch_size, num_context_dims] tensor - representing the batch of contexts to be inserted. - indices: A list of a list of integers denoting indices to replace. - Returns: - A list of resulting contexts. - """ - if indices is None: indices = [[]] * len(contexts) - assert len(contexts) == len(indices) - assert all([spec.shape.ndims == 1 for spec in self.context_specs]) - mix_contexts = [] - for contexts_, insert_contexts_, indices_, spec in zip( - contexts, insert_contexts, indices, self.context_specs): - mix_contexts.append( - tf.concat( - [ - insert_contexts_[:, i:i + 1] if i in indices_ else - contexts_[:, i:i + 1] for i in range(spec.shape.as_list()[0]) - ], - axis=1)) - return mix_contexts - - def begin_episode_ops(self, mode, action_fn=None, state=None): - """Returns ops that reset agent at beginning of episodes. - - Args: - mode: a string representing the mode=[train, explore, eval]. - Returns: - A list of ops. - """ - all_ops = [] - for _, action_var in sorted(self._action_vars.items()): - sample_action = self.sample_random_actions(1)[0] - all_ops.append(tf.assign(action_var, sample_action)) - all_ops += self.tf_context.reset(mode=mode, agent=self._meta_agent, - action_fn=action_fn, state=state) - return all_ops - - def cond_begin_episode_op(self, cond, input_vars, mode, meta_action_fn): - """Returns op that resets agent at beginning of episodes. - - A new episode is begun if the cond op evalues to `False`. - - Args: - cond: a Boolean tensor variable. - input_vars: A list of tensor variables. - mode: a string representing the mode=[train, explore, eval]. - Returns: - Conditional begin op. - """ - (state, action, reward, next_state, - state_repr, next_state_repr) = input_vars - def continue_fn(): - """Continue op fn.""" - items = [state, action, reward, next_state, - state_repr, next_state_repr] + list(self.context_vars) - batch_items = [tf.expand_dims(item, 0) for item in items] - (states, actions, rewards, next_states, - state_reprs, next_state_reprs) = batch_items[:6] - context_reward = self.compute_rewards( - mode, state_reprs, actions, rewards, next_state_reprs, - batch_items[6:])[0][0] - context_reward = tf.cast(context_reward, dtype=reward.dtype) - if self.meta_agent is not None: - meta_action = tf.concat(self.context_vars, -1) - items = [state, meta_action, reward, next_state, - state_repr, next_state_repr] + list(self.meta_agent.context_vars) - batch_items = [tf.expand_dims(item, 0) for item in items] - (states, meta_actions, rewards, next_states, - state_reprs, next_state_reprs) = batch_items[:6] - meta_reward = self.meta_agent.compute_rewards( - mode, states, meta_actions, rewards, - next_states, batch_items[6:])[0][0] - meta_reward = tf.cast(meta_reward, dtype=reward.dtype) - else: - meta_reward = tf.constant(0, dtype=reward.dtype) - - with tf.control_dependencies([context_reward, meta_reward]): - step_ops = self.tf_context.step(mode=mode, agent=self._meta_agent, - state=state, - next_state=next_state, - state_repr=state_repr, - next_state_repr=next_state_repr, - action_fn=meta_action_fn) - with tf.control_dependencies(step_ops): - context_reward, meta_reward = map(tf.identity, [context_reward, meta_reward]) - return context_reward, meta_reward - def begin_episode_fn(): - """Begin op fn.""" - begin_ops = self.begin_episode_ops(mode=mode, action_fn=meta_action_fn, state=state) - with tf.control_dependencies(begin_ops): - return tf.zeros_like(reward), tf.zeros_like(reward) - with tf.control_dependencies(input_vars): - cond_begin_episode_op = tf.cond(cond, continue_fn, begin_episode_fn) - return cond_begin_episode_op - - def get_env_base_wrapper(self, env_base, **begin_kwargs): - """Create a wrapper around env_base, with agent-specific begin/end_episode. - - Args: - env_base: A python environment base. - **begin_kwargs: Keyword args for begin_episode_ops. - Returns: - An object with begin_episode() and end_episode(). - """ - begin_ops = self.begin_episode_ops(**begin_kwargs) - return uvf_utils.get_contextual_env_base(env_base, begin_ops) - - def init_action_vars(self, name, i=None): - """Create and return a tensorflow Variable holding an action. - - Args: - name: Name of the variables. - i: Integer id. - Returns: - A [num_action_dims] tensor. - """ - if i is not None: - name += '_%d' % i - assert name not in self._action_vars, ('Conflict! %s is already ' - 'initialized.') % name - self._action_vars[name] = tf.Variable( - self.sample_random_actions(1)[0], name='%s_action' % (name)) - self._validate_actions(tf.expand_dims(self._action_vars[name], 0)) - return self._action_vars[name] - - @gin.configurable('uvf_critic_function') - def critic_function(self, critic_vals, states, critic_fn=None): - """Computes q values based on outputs from the critic net. - - Args: - critic_vals: A tf.float32 [batch_size, ...] tensor representing outputs - from the critic net. - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - critic_fn: A callable that process outputs from critic_net and - outputs a [batch_size] tensor representing q values. - Returns: - A tf.float32 [batch_size] tensor representing q values. - """ - if critic_fn is not None: - env_states, contexts = self.unmerged_states(states) - critic_vals = critic_fn(critic_vals, env_states, contexts) - critic_vals.shape.assert_has_rank(1) - return critic_vals - - def get_action_vars(self, key): - return self._action_vars[key] - - def get_context_vars(self, key): - return self.tf_context.context_vars[key] - - def step_cond_fn(self, *args): - return self._step_cond_fn(self, *args) - - def reset_episode_cond_fn(self, *args): - return self._reset_episode_cond_fn(self, *args) - - def reset_env_cond_fn(self, *args): - return self._reset_env_cond_fn(self, *args) - - @property - def context_vars(self): - return self.tf_context.vars - - -@gin.configurable -class MetaAgentCore(UvfAgentCore): - """Defines basic functions for UVF Meta-agent. Must be inherited with an RL agent. - - Used as higher-level agent. - """ - - def __init__(self, - observation_spec, - action_spec, - tf_env, - tf_context, - sub_context, - step_cond_fn=cond_fn.env_transition, - reset_episode_cond_fn=cond_fn.env_restart, - reset_env_cond_fn=cond_fn.false_fn, - metrics=None, - actions_reg=0., - k=2, - **base_agent_kwargs): - """Constructs a Meta agent. - - Args: - observation_spec: A TensorSpec defining the observations. - action_spec: A BoundedTensorSpec defining the actions. - tf_env: A Tensorflow environment object. - tf_context: A Context class. - step_cond_fn: A function indicating whether to increment the num of steps. - reset_episode_cond_fn: A function indicating whether to restart the - episode, resampling the context. - reset_env_cond_fn: A function indicating whether to perform a manual reset - of the environment. - metrics: A list of functions that evaluate metrics of the agent. - **base_agent_kwargs: A dictionary of parameters for base RL Agent. - Raises: - ValueError: If 'dqda_clipping' is < 0. - """ - self._step_cond_fn = step_cond_fn - self._reset_episode_cond_fn = reset_episode_cond_fn - self._reset_env_cond_fn = reset_env_cond_fn - self.metrics = metrics - self._actions_reg = actions_reg - self._k = k - - # expose tf_context methods - self.tf_context = tf_context(tf_env=tf_env) - self.sub_context = sub_context(tf_env=tf_env) - self.set_replay = self.tf_context.set_replay - self.sample_contexts = self.tf_context.sample_contexts - self.compute_rewards = self.tf_context.compute_rewards - self.gamma_index = self.tf_context.gamma_index - self.context_specs = self.tf_context.context_specs - self.context_as_action_specs = self.tf_context.context_as_action_specs - self.sub_context_as_action_specs = self.sub_context.context_as_action_specs - self.init_context_vars = self.tf_context.create_vars - - self.env_observation_spec = observation_spec[0] - merged_observation_spec = (uvf_utils.merge_specs( - (self.env_observation_spec,) + self.context_specs),) - self._context_vars = dict() - self._action_vars = dict() - - assert len(self.context_as_action_specs) == 1 - self.BASE_AGENT_CLASS.__init__( - self, - observation_spec=merged_observation_spec, - action_spec=self.sub_context_as_action_specs, - **base_agent_kwargs - ) - - @gin.configurable('meta_add_noise_fn') - def add_noise_fn(self, action_fn, stddev=1.0, debug=False, - global_step=None): - noisy_action_fn = super(MetaAgentCore, self).add_noise_fn( - action_fn, stddev, - clip=True, global_step=global_step) - return noisy_action_fn - - def actor_loss(self, states, actions, rewards, discounts, - next_states): - """Returns the next action for the state. - - Args: - state: A [num_state_dims] tensor representing a state. - context: A list of [num_context_dims] tensor representing a context. - Returns: - A [num_action_dims] tensor representing the action. - """ - actions = self.actor_net(states, stop_gradients=False) - regularizer = self._actions_reg * tf.reduce_mean( - tf.reduce_sum(tf.abs(actions[:, self._k:]), -1), 0) - loss = self.BASE_AGENT_CLASS.actor_loss(self, states) - return regularizer + loss - - -@gin.configurable -class UvfAgent(UvfAgentCore, ddpg_agent.TD3Agent): - """A DDPG agent with UVF. - """ - BASE_AGENT_CLASS = ddpg_agent.TD3Agent - ACTION_TYPE = 'continuous' - - def __init__(self, *args, **kwargs): - UvfAgentCore.__init__(self, *args, **kwargs) - - -@gin.configurable -class MetaAgent(MetaAgentCore, ddpg_agent.TD3Agent): - """A DDPG meta-agent. - """ - BASE_AGENT_CLASS = ddpg_agent.TD3Agent - ACTION_TYPE = 'continuous' - - def __init__(self, *args, **kwargs): - MetaAgentCore.__init__(self, *args, **kwargs) - - -@gin.configurable() -def state_preprocess_net( - states, - num_output_dims=2, - states_hidden_layers=(100,), - normalizer_fn=None, - activation_fn=tf.nn.relu, - zero_time=True, - images=False): - """Creates a simple feed forward net for embedding states. - """ - with slim.arg_scope( - [slim.fully_connected], - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - weights_initializer=slim.variance_scaling_initializer( - factor=1.0/3.0, mode='FAN_IN', uniform=True)): - - states_shape = tf.shape(states) - states_dtype = states.dtype - states = tf.to_float(states) - if images: # Zero-out x-y - states *= tf.constant([0.] * 2 + [1.] * (states.shape[-1] - 2), dtype=states.dtype) - if zero_time: - states *= tf.constant([1.] * (states.shape[-1] - 1) + [0.], dtype=states.dtype) - orig_states = states - embed = states - if states_hidden_layers: - embed = slim.stack(embed, slim.fully_connected, states_hidden_layers, - scope='states') - - with slim.arg_scope([slim.fully_connected], - weights_regularizer=None, - weights_initializer=tf.random_uniform_initializer( - minval=-0.003, maxval=0.003)): - embed = slim.fully_connected(embed, num_output_dims, - activation_fn=None, - normalizer_fn=None, - scope='value') - - output = embed - output = tf.cast(output, states_dtype) - return output - - -@gin.configurable() -def action_embed_net( - actions, - states=None, - num_output_dims=2, - hidden_layers=(400, 300), - normalizer_fn=None, - activation_fn=tf.nn.relu, - zero_time=True, - images=False): - """Creates a simple feed forward net for embedding actions. - """ - with slim.arg_scope( - [slim.fully_connected], - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - weights_initializer=slim.variance_scaling_initializer( - factor=1.0/3.0, mode='FAN_IN', uniform=True)): - - actions = tf.to_float(actions) - if states is not None: - if images: # Zero-out x-y - states *= tf.constant([0.] * 2 + [1.] * (states.shape[-1] - 2), dtype=states.dtype) - if zero_time: - states *= tf.constant([1.] * (states.shape[-1] - 1) + [0.], dtype=states.dtype) - actions = tf.concat([actions, tf.to_float(states)], -1) - - embed = actions - if hidden_layers: - embed = slim.stack(embed, slim.fully_connected, hidden_layers, - scope='hidden') - - with slim.arg_scope([slim.fully_connected], - weights_regularizer=None, - weights_initializer=tf.random_uniform_initializer( - minval=-0.003, maxval=0.003)): - embed = slim.fully_connected(embed, num_output_dims, - activation_fn=None, - normalizer_fn=None, - scope='value') - if num_output_dims == 1: - return embed[:, 0, ...] - else: - return embed - - -def huber(x, kappa=0.1): - return (0.5 * tf.square(x) * tf.to_float(tf.abs(x) <= kappa) + - kappa * (tf.abs(x) - 0.5 * kappa) * tf.to_float(tf.abs(x) > kappa) - ) / kappa - - -@gin.configurable() -class StatePreprocess(object): - STATE_PREPROCESS_NET_SCOPE = 'state_process_net' - ACTION_EMBED_NET_SCOPE = 'action_embed_net' - - def __init__(self, trainable=False, - state_preprocess_net=lambda states: states, - action_embed_net=lambda actions, *args, **kwargs: actions, - ndims=None): - self.trainable = trainable - self._scope = tf.get_variable_scope().name - self._ndims = ndims - self._state_preprocess_net = tf.make_template( - self.STATE_PREPROCESS_NET_SCOPE, state_preprocess_net, - create_scope_now_=True) - self._action_embed_net = tf.make_template( - self.ACTION_EMBED_NET_SCOPE, action_embed_net, - create_scope_now_=True) - - def __call__(self, states): - batched = states.get_shape().ndims != 1 - if not batched: - states = tf.expand_dims(states, 0) - embedded = self._state_preprocess_net(states) - if self._ndims is not None: - embedded = embedded[..., :self._ndims] - if not batched: - return embedded[0] - return embedded - - def loss(self, states, next_states, low_actions, low_states): - batch_size = tf.shape(states)[0] - d = int(low_states.shape[1]) - # Sample indices into meta-transition to train on. - probs = 0.99 ** tf.range(d, dtype=tf.float32) - probs *= tf.constant([1.0] * (d - 1) + [1.0 / (1 - 0.99)], - dtype=tf.float32) - probs /= tf.reduce_sum(probs) - index_dist = tf.distributions.Categorical(probs=probs, dtype=tf.int64) - indices = index_dist.sample(batch_size) - batch_size = tf.cast(batch_size, tf.int64) - next_indices = tf.concat( - [tf.range(batch_size, dtype=tf.int64)[:, None], - (1 + indices[:, None]) % d], -1) - new_next_states = tf.where(indices < d - 1, - tf.gather_nd(low_states, next_indices), - next_states) - next_states = new_next_states - - embed1 = tf.to_float(self._state_preprocess_net(states)) - embed2 = tf.to_float(self._state_preprocess_net(next_states)) - action_embed = self._action_embed_net( - tf.layers.flatten(low_actions), states=states) - - tau = 2.0 - fn = lambda z: tau * tf.reduce_sum(huber(z), -1) - all_embed = tf.get_variable('all_embed', [1024, int(embed1.shape[-1])], - initializer=tf.zeros_initializer()) - upd = all_embed.assign(tf.concat([all_embed[batch_size:], embed2], 0)) - with tf.control_dependencies([upd]): - close = 1 * tf.reduce_mean(fn(embed1 + action_embed - embed2)) - prior_log_probs = tf.reduce_logsumexp( - -fn((embed1 + action_embed)[:, None, :] - all_embed[None, :, :]), - axis=-1) - tf.log(tf.to_float(all_embed.shape[0])) - far = tf.reduce_mean(tf.exp(-fn((embed1 + action_embed)[1:] - embed2[:-1]) - - tf.stop_gradient(prior_log_probs[1:]))) - repr_log_probs = tf.stop_gradient( - -fn(embed1 + action_embed - embed2) - prior_log_probs) / tau - return close + far, repr_log_probs, indices - - def get_trainable_vars(self): - return ( - slim.get_trainable_variables( - uvf_utils.join_scope(self._scope, self.STATE_PREPROCESS_NET_SCOPE)) + - slim.get_trainable_variables( - uvf_utils.join_scope(self._scope, self.ACTION_EMBED_NET_SCOPE))) - - -@gin.configurable() -class InverseDynamics(object): - INVERSE_DYNAMICS_NET_SCOPE = 'inverse_dynamics' - - def __init__(self, spec): - self._spec = spec - - def sample(self, states, next_states, num_samples, orig_goals, sc=0.5): - goal_dim = orig_goals.shape[-1] - spec_range = (self._spec.maximum - self._spec.minimum) / 2 * tf.ones([goal_dim]) - loc = tf.cast(next_states - states, tf.float32)[:, :goal_dim] - scale = sc * tf.tile(tf.reshape(spec_range, [1, goal_dim]), - [tf.shape(states)[0], 1]) - dist = tf.distributions.Normal(loc, scale) - if num_samples == 1: - return dist.sample() - samples = tf.concat([dist.sample(num_samples - 2), - tf.expand_dims(loc, 0), - tf.expand_dims(orig_goals, 0)], 0) - return uvf_utils.clip_to_spec(samples, self._spec) diff --git a/research/efficient-hrl/agents/__init__.py b/research/efficient-hrl/agents/__init__.py deleted file mode 100644 index 8b137891791..00000000000 --- a/research/efficient-hrl/agents/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/research/efficient-hrl/agents/circular_buffer.py b/research/efficient-hrl/agents/circular_buffer.py deleted file mode 100644 index 72f90f0de89..00000000000 --- a/research/efficient-hrl/agents/circular_buffer.py +++ /dev/null @@ -1,289 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A circular buffer where each element is a list of tensors. - -Each element of the buffer is a list of tensors. An example use case is a replay -buffer in reinforcement learning, where each element is a list of tensors -representing the state, action, reward etc. - -New elements are added sequentially, and once the buffer is full, we -start overwriting them in a circular fashion. Reading does not remove any -elements, only adding new elements does. -""" - -import collections -import numpy as np -import tensorflow as tf - -import gin.tf - - -@gin.configurable -class CircularBuffer(object): - """A circular buffer where each element is a list of tensors.""" - - def __init__(self, buffer_size=1000, scope='replay_buffer'): - """Circular buffer of list of tensors. - - Args: - buffer_size: (integer) maximum number of tensor lists the buffer can hold. - scope: (string) variable scope for creating the variables. - """ - self._buffer_size = np.int64(buffer_size) - self._scope = scope - self._tensors = collections.OrderedDict() - with tf.variable_scope(self._scope): - self._num_adds = tf.Variable(0, dtype=tf.int64, name='num_adds') - self._num_adds_cs = tf.CriticalSection(name='num_adds') - - @property - def buffer_size(self): - return self._buffer_size - - @property - def scope(self): - return self._scope - - @property - def num_adds(self): - return self._num_adds - - def _create_variables(self, tensors): - with tf.variable_scope(self._scope): - for name in tensors.keys(): - tensor = tensors[name] - self._tensors[name] = tf.get_variable( - name='BufferVariable_' + name, - shape=[self._buffer_size] + tensor.get_shape().as_list(), - dtype=tensor.dtype, - trainable=False) - - def _validate(self, tensors): - """Validate shapes of tensors.""" - if len(tensors) != len(self._tensors): - raise ValueError('Expected tensors to have %d elements. Received %d ' - 'instead.' % (len(self._tensors), len(tensors))) - if self._tensors.keys() != tensors.keys(): - raise ValueError('The keys of tensors should be the always the same.' - 'Received %s instead %s.' % - (tensors.keys(), self._tensors.keys())) - for name, tensor in tensors.items(): - if tensor.get_shape().as_list() != self._tensors[ - name].get_shape().as_list()[1:]: - raise ValueError('Tensor %s has incorrect shape.' % name) - if not tensor.dtype.is_compatible_with(self._tensors[name].dtype): - raise ValueError( - 'Tensor %s has incorrect data type. Expected %s, received %s' % - (name, self._tensors[name].read_value().dtype, tensor.dtype)) - - def add(self, tensors): - """Adds an element (list/tuple/dict of tensors) to the buffer. - - Args: - tensors: (list/tuple/dict of tensors) to be added to the buffer. - Returns: - An add operation that adds the input `tensors` to the buffer. Similar to - an enqueue_op. - Raises: - ValueError: If the shapes and data types of input `tensors' are not the - same across calls to the add function. - """ - return self.maybe_add(tensors, True) - - def maybe_add(self, tensors, condition): - """Adds an element (tensors) to the buffer based on the condition.. - - Args: - tensors: (list/tuple of tensors) to be added to the buffer. - condition: A boolean Tensor controlling whether the tensors would be added - to the buffer or not. - Returns: - An add operation that adds the input `tensors` to the buffer. Similar to - an maybe_enqueue_op. - Raises: - ValueError: If the shapes and data types of input `tensors' are not the - same across calls to the add function. - """ - if not isinstance(tensors, dict): - names = [str(i) for i in range(len(tensors))] - tensors = collections.OrderedDict(zip(names, tensors)) - if not isinstance(tensors, collections.OrderedDict): - tensors = collections.OrderedDict( - sorted(tensors.items(), key=lambda t: t[0])) - if not self._tensors: - self._create_variables(tensors) - else: - self._validate(tensors) - - #@tf.critical_section(self._position_mutex) - def _increment_num_adds(): - # Adding 0 to the num_adds variable is a trick to read the value of the - # variable and return a read-only tensor. Doing this in a critical - # section allows us to capture a snapshot of the variable that will - # not be affected by other threads updating num_adds. - return self._num_adds.assign_add(1) + 0 - def _add(): - num_adds_inc = self._num_adds_cs.execute(_increment_num_adds) - current_pos = tf.mod(num_adds_inc - 1, self._buffer_size) - update_ops = [] - for name in self._tensors.keys(): - update_ops.append( - tf.scatter_update(self._tensors[name], current_pos, tensors[name])) - return tf.group(*update_ops) - - return tf.contrib.framework.smart_cond(condition, _add, tf.no_op) - - def get_random_batch(self, batch_size, keys=None, num_steps=1): - """Samples a batch of tensors from the buffer with replacement. - - Args: - batch_size: (integer) number of elements to sample. - keys: List of keys of tensors to retrieve. If None retrieve all. - num_steps: (integer) length of trajectories to return. If > 1 will return - a list of lists, where each internal list represents a trajectory of - length num_steps. - Returns: - A list of tensors, where each element in the list is a batch sampled from - one of the tensors in the buffer. - Raises: - ValueError: If get_random_batch is called before calling the add function. - tf.errors.InvalidArgumentError: If this operation is executed before any - items are added to the buffer. - """ - if not self._tensors: - raise ValueError('The add function must be called before get_random_batch.') - if keys is None: - keys = self._tensors.keys() - - latest_start_index = self.get_num_adds() - num_steps + 1 - empty_buffer_assert = tf.Assert( - tf.greater(latest_start_index, 0), - ['Not enough elements have been added to the buffer.']) - with tf.control_dependencies([empty_buffer_assert]): - max_index = tf.minimum(self._buffer_size, latest_start_index) - indices = tf.random_uniform( - [batch_size], - minval=0, - maxval=max_index, - dtype=tf.int64) - if num_steps == 1: - return self.gather(indices, keys) - else: - return self.gather_nstep(num_steps, indices, keys) - - def gather(self, indices, keys=None): - """Returns elements at the specified indices from the buffer. - - Args: - indices: (list of integers or rank 1 int Tensor) indices in the buffer to - retrieve elements from. - keys: List of keys of tensors to retrieve. If None retrieve all. - Returns: - A list of tensors, where each element in the list is obtained by indexing - one of the tensors in the buffer. - Raises: - ValueError: If gather is called before calling the add function. - tf.errors.InvalidArgumentError: If indices are bigger than the number of - items in the buffer. - """ - if not self._tensors: - raise ValueError('The add function must be called before calling gather.') - if keys is None: - keys = self._tensors.keys() - with tf.name_scope('Gather'): - index_bound_assert = tf.Assert( - tf.less( - tf.to_int64(tf.reduce_max(indices)), - tf.minimum(self.get_num_adds(), self._buffer_size)), - ['Index out of bounds.']) - with tf.control_dependencies([index_bound_assert]): - indices = tf.convert_to_tensor(indices) - - batch = [] - for key in keys: - batch.append(tf.gather(self._tensors[key], indices, name=key)) - return batch - - def gather_nstep(self, num_steps, indices, keys=None): - """Returns elements at the specified indices from the buffer. - - Args: - num_steps: (integer) length of trajectories to return. - indices: (list of rank num_steps int Tensor) indices in the buffer to - retrieve elements from for multiple trajectories. Each Tensor in the - list represents the indices for a trajectory. - keys: List of keys of tensors to retrieve. If None retrieve all. - Returns: - A list of list-of-tensors, where each element in the list is obtained by - indexing one of the tensors in the buffer. - Raises: - ValueError: If gather is called before calling the add function. - tf.errors.InvalidArgumentError: If indices are bigger than the number of - items in the buffer. - """ - if not self._tensors: - raise ValueError('The add function must be called before calling gather.') - if keys is None: - keys = self._tensors.keys() - with tf.name_scope('Gather'): - index_bound_assert = tf.Assert( - tf.less_equal( - tf.to_int64(tf.reduce_max(indices) + num_steps), - self.get_num_adds()), - ['Trajectory indices go out of bounds.']) - with tf.control_dependencies([index_bound_assert]): - indices = tf.map_fn( - lambda x: tf.mod(tf.range(x, x + num_steps), self._buffer_size), - indices, - dtype=tf.int64) - - batch = [] - for key in keys: - - def SampleTrajectories(trajectory_indices, key=key, - num_steps=num_steps): - trajectory_indices.set_shape([num_steps]) - return tf.gather(self._tensors[key], trajectory_indices, name=key) - - batch.append(tf.map_fn(SampleTrajectories, indices, - dtype=self._tensors[key].dtype)) - return batch - - def get_position(self): - """Returns the position at which the last element was added. - - Returns: - An int tensor representing the index at which the last element was added - to the buffer or -1 if no elements were added. - """ - return tf.cond(self.get_num_adds() < 1, - lambda: self.get_num_adds() - 1, - lambda: tf.mod(self.get_num_adds() - 1, self._buffer_size)) - - def get_num_adds(self): - """Returns the number of additions to the buffer. - - Returns: - An int tensor representing the number of elements that were added. - """ - def num_adds(): - return self._num_adds.value() - - return self._num_adds_cs.execute(num_adds) - - def get_num_tensors(self): - """Returns the number of tensors (slots) in the buffer.""" - return len(self._tensors) diff --git a/research/efficient-hrl/agents/ddpg_agent.py b/research/efficient-hrl/agents/ddpg_agent.py deleted file mode 100644 index 904eb650271..00000000000 --- a/research/efficient-hrl/agents/ddpg_agent.py +++ /dev/null @@ -1,739 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A DDPG/NAF agent. - -Implements the Deep Deterministic Policy Gradient (DDPG) algorithm from -"Continuous control with deep reinforcement learning" - Lilicrap et al. -https://arxiv.org/abs/1509.02971, and the Normalized Advantage Functions (NAF) -algorithm "Continuous Deep Q-Learning with Model-based Acceleration" - Gu et al. -https://arxiv.org/pdf/1603.00748. -""" - -import tensorflow as tf -slim = tf.contrib.slim -import gin.tf -from utils import utils -from agents import ddpg_networks as networks - - -@gin.configurable -class DdpgAgent(object): - """An RL agent that learns using the DDPG algorithm. - - Example usage: - - def critic_net(states, actions): - ... - def actor_net(states, num_action_dims): - ... - - Given a tensorflow environment tf_env, - (of type learning.deepmind.rl.environments.tensorflow.python.tfpyenvironment) - - obs_spec = tf_env.observation_spec() - action_spec = tf_env.action_spec() - - ddpg_agent = agent.DdpgAgent(obs_spec, - action_spec, - actor_net=actor_net, - critic_net=critic_net) - - we can perform actions on the environment as follows: - - state = tf_env.observations()[0] - action = ddpg_agent.actor_net(tf.expand_dims(state, 0))[0, :] - transition_type, reward, discount = tf_env.step([action]) - - Train: - - critic_loss = ddpg_agent.critic_loss(states, actions, rewards, discounts, - next_states) - actor_loss = ddpg_agent.actor_loss(states) - - critic_train_op = slim.learning.create_train_op( - critic_loss, - critic_optimizer, - variables_to_train=ddpg_agent.get_trainable_critic_vars(), - ) - - actor_train_op = slim.learning.create_train_op( - actor_loss, - actor_optimizer, - variables_to_train=ddpg_agent.get_trainable_actor_vars(), - ) - """ - - ACTOR_NET_SCOPE = 'actor_net' - CRITIC_NET_SCOPE = 'critic_net' - TARGET_ACTOR_NET_SCOPE = 'target_actor_net' - TARGET_CRITIC_NET_SCOPE = 'target_critic_net' - - def __init__(self, - observation_spec, - action_spec, - actor_net=networks.actor_net, - critic_net=networks.critic_net, - td_errors_loss=tf.losses.huber_loss, - dqda_clipping=0., - actions_regularizer=0., - target_q_clipping=None, - residual_phi=0.0, - debug_summaries=False): - """Constructs a DDPG agent. - - Args: - observation_spec: A TensorSpec defining the observations. - action_spec: A BoundedTensorSpec defining the actions. - actor_net: A callable that creates the actor network. Must take the - following arguments: states, num_actions. Please see networks.actor_net - for an example. - critic_net: A callable that creates the critic network. Must take the - following arguments: states, actions. Please see networks.critic_net - for an example. - td_errors_loss: A callable defining the loss function for the critic - td error. - dqda_clipping: (float) clips the gradient dqda element-wise between - [-dqda_clipping, dqda_clipping]. Does not perform clipping if - dqda_clipping == 0. - actions_regularizer: A scalar, when positive penalizes the norm of the - actions. This can prevent saturation of actions for the actor_loss. - target_q_clipping: (tuple of floats) clips target q values within - (low, high) values when computing the critic loss. - residual_phi: (float) [0.0, 1.0] Residual algorithm parameter that - interpolates between Q-learning and residual gradient algorithm. - http://www.leemon.com/papers/1995b.pdf - debug_summaries: If True, add summaries to help debug behavior. - Raises: - ValueError: If 'dqda_clipping' is < 0. - """ - self._observation_spec = observation_spec[0] - self._action_spec = action_spec[0] - self._state_shape = tf.TensorShape([None]).concatenate( - self._observation_spec.shape) - self._action_shape = tf.TensorShape([None]).concatenate( - self._action_spec.shape) - self._num_action_dims = self._action_spec.shape.num_elements() - - self._scope = tf.get_variable_scope().name - self._actor_net = tf.make_template( - self.ACTOR_NET_SCOPE, actor_net, create_scope_now_=True) - self._critic_net = tf.make_template( - self.CRITIC_NET_SCOPE, critic_net, create_scope_now_=True) - self._target_actor_net = tf.make_template( - self.TARGET_ACTOR_NET_SCOPE, actor_net, create_scope_now_=True) - self._target_critic_net = tf.make_template( - self.TARGET_CRITIC_NET_SCOPE, critic_net, create_scope_now_=True) - self._td_errors_loss = td_errors_loss - if dqda_clipping < 0: - raise ValueError('dqda_clipping must be >= 0.') - self._dqda_clipping = dqda_clipping - self._actions_regularizer = actions_regularizer - self._target_q_clipping = target_q_clipping - self._residual_phi = residual_phi - self._debug_summaries = debug_summaries - - def _batch_state(self, state): - """Convert state to a batched state. - - Args: - state: Either a list/tuple with an state tensor [num_state_dims]. - Returns: - A tensor [1, num_state_dims] - """ - if isinstance(state, (tuple, list)): - state = state[0] - if state.get_shape().ndims == 1: - state = tf.expand_dims(state, 0) - return state - - def action(self, state): - """Returns the next action for the state. - - Args: - state: A [num_state_dims] tensor representing a state. - Returns: - A [num_action_dims] tensor representing the action. - """ - return self.actor_net(self._batch_state(state), stop_gradients=True)[0, :] - - @gin.configurable('ddpg_sample_action') - def sample_action(self, state, stddev=1.0): - """Returns the action for the state with additive noise. - - Args: - state: A [num_state_dims] tensor representing a state. - stddev: stddev for the Ornstein-Uhlenbeck noise. - Returns: - A [num_action_dims] action tensor. - """ - agent_action = self.action(state) - agent_action += tf.random_normal(tf.shape(agent_action)) * stddev - return utils.clip_to_spec(agent_action, self._action_spec) - - def actor_net(self, states, stop_gradients=False): - """Returns the output of the actor network. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - stop_gradients: (boolean) if true, gradients cannot be propogated through - this operation. - Returns: - A [batch_size, num_action_dims] tensor of actions. - Raises: - ValueError: If `states` does not have the expected dimensions. - """ - self._validate_states(states) - actions = self._actor_net(states, self._action_spec) - if stop_gradients: - actions = tf.stop_gradient(actions) - return actions - - def critic_net(self, states, actions, for_critic_loss=False): - """Returns the output of the critic network. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] tensor representing a batch - of actions. - Returns: - q values: A [batch_size] tensor of q values. - Raises: - ValueError: If `states` or `actions' do not have the expected dimensions. - """ - self._validate_states(states) - self._validate_actions(actions) - return self._critic_net(states, actions, - for_critic_loss=for_critic_loss) - - def target_actor_net(self, states): - """Returns the output of the target actor network. - - The target network is used to compute stable targets for training. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - Returns: - A [batch_size, num_action_dims] tensor of actions. - Raises: - ValueError: If `states` does not have the expected dimensions. - """ - self._validate_states(states) - actions = self._target_actor_net(states, self._action_spec) - return tf.stop_gradient(actions) - - def target_critic_net(self, states, actions, for_critic_loss=False): - """Returns the output of the target critic network. - - The target network is used to compute stable targets for training. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] tensor representing a batch - of actions. - Returns: - q values: A [batch_size] tensor of q values. - Raises: - ValueError: If `states` or `actions' do not have the expected dimensions. - """ - self._validate_states(states) - self._validate_actions(actions) - return tf.stop_gradient( - self._target_critic_net(states, actions, - for_critic_loss=for_critic_loss)) - - def value_net(self, states, for_critic_loss=False): - """Returns the output of the critic evaluated with the actor. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - Returns: - q values: A [batch_size] tensor of q values. - """ - actions = self.actor_net(states) - return self.critic_net(states, actions, - for_critic_loss=for_critic_loss) - - def target_value_net(self, states, for_critic_loss=False): - """Returns the output of the target critic evaluated with the target actor. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - Returns: - q values: A [batch_size] tensor of q values. - """ - target_actions = self.target_actor_net(states) - return self.target_critic_net(states, target_actions, - for_critic_loss=for_critic_loss) - - def critic_loss(self, states, actions, rewards, discounts, - next_states): - """Computes a loss for training the critic network. - - The loss is the mean squared error between the Q value predictions of the - critic and Q values estimated using TD-lambda. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] tensor representing a batch - of actions. - rewards: A [batch_size, ...] tensor representing a batch of rewards, - broadcastable to the critic net output. - discounts: A [batch_size, ...] tensor representing a batch of discounts, - broadcastable to the critic net output. - next_states: A [batch_size, num_state_dims] tensor representing a batch - of next states. - Returns: - A rank-0 tensor representing the critic loss. - Raises: - ValueError: If any of the inputs do not have the expected dimensions, or - if their batch_sizes do not match. - """ - self._validate_states(states) - self._validate_actions(actions) - self._validate_states(next_states) - - target_q_values = self.target_value_net(next_states, for_critic_loss=True) - td_targets = target_q_values * discounts + rewards - if self._target_q_clipping is not None: - td_targets = tf.clip_by_value(td_targets, self._target_q_clipping[0], - self._target_q_clipping[1]) - q_values = self.critic_net(states, actions, for_critic_loss=True) - td_errors = td_targets - q_values - if self._debug_summaries: - gen_debug_td_error_summaries( - target_q_values, q_values, td_targets, td_errors) - - loss = self._td_errors_loss(td_targets, q_values) - - if self._residual_phi > 0.0: # compute residual gradient loss - residual_q_values = self.value_net(next_states, for_critic_loss=True) - residual_td_targets = residual_q_values * discounts + rewards - if self._target_q_clipping is not None: - residual_td_targets = tf.clip_by_value(residual_td_targets, - self._target_q_clipping[0], - self._target_q_clipping[1]) - residual_td_errors = residual_td_targets - q_values - residual_loss = self._td_errors_loss( - residual_td_targets, residual_q_values) - loss = (loss * (1.0 - self._residual_phi) + - residual_loss * self._residual_phi) - return loss - - def actor_loss(self, states): - """Computes a loss for training the actor network. - - Note that output does not represent an actual loss. It is called a loss only - in the sense that its gradient w.r.t. the actor network weights is the - correct gradient for training the actor network, - i.e. dloss/dweights = (dq/da)*(da/dweights) - which is the gradient used in Algorithm 1 of Lilicrap et al. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - Returns: - A rank-0 tensor representing the actor loss. - Raises: - ValueError: If `states` does not have the expected dimensions. - """ - self._validate_states(states) - actions = self.actor_net(states, stop_gradients=False) - critic_values = self.critic_net(states, actions) - q_values = self.critic_function(critic_values, states) - dqda = tf.gradients([q_values], [actions])[0] - dqda_unclipped = dqda - if self._dqda_clipping > 0: - dqda = tf.clip_by_value(dqda, -self._dqda_clipping, self._dqda_clipping) - - actions_norm = tf.norm(actions) - if self._debug_summaries: - with tf.name_scope('dqda'): - tf.summary.scalar('actions_norm', actions_norm) - tf.summary.histogram('dqda', dqda) - tf.summary.histogram('dqda_unclipped', dqda_unclipped) - tf.summary.histogram('actions', actions) - for a in range(self._num_action_dims): - tf.summary.histogram('dqda_unclipped_%d' % a, dqda_unclipped[:, a]) - tf.summary.histogram('dqda_%d' % a, dqda[:, a]) - - actions_norm *= self._actions_regularizer - return slim.losses.mean_squared_error(tf.stop_gradient(dqda + actions), - actions, - scope='actor_loss') + actions_norm - - @gin.configurable('ddpg_critic_function') - def critic_function(self, critic_values, states, weights=None): - """Computes q values based on critic_net outputs, states, and weights. - - Args: - critic_values: A tf.float32 [batch_size, ...] tensor representing outputs - from the critic net. - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - weights: A list or Numpy array or tensor with a shape broadcastable to - `critic_values`. - Returns: - A tf.float32 [batch_size] tensor representing q values. - """ - del states # unused args - if weights is not None: - weights = tf.convert_to_tensor(weights, dtype=critic_values.dtype) - critic_values *= weights - if critic_values.shape.ndims > 1: - critic_values = tf.reduce_sum(critic_values, - range(1, critic_values.shape.ndims)) - critic_values.shape.assert_has_rank(1) - return critic_values - - @gin.configurable('ddpg_update_targets') - def update_targets(self, tau=1.0): - """Performs a soft update of the target network parameters. - - For each weight w_s in the actor/critic networks, and its corresponding - weight w_t in the target actor/critic networks, a soft update is: - w_t = (1- tau) x w_t + tau x ws - - Args: - tau: A float scalar in [0, 1] - Returns: - An operation that performs a soft update of the target network parameters. - Raises: - ValueError: If `tau` is not in [0, 1]. - """ - if tau < 0 or tau > 1: - raise ValueError('Input `tau` should be in [0, 1].') - update_actor = utils.soft_variables_update( - slim.get_trainable_variables( - utils.join_scope(self._scope, self.ACTOR_NET_SCOPE)), - slim.get_trainable_variables( - utils.join_scope(self._scope, self.TARGET_ACTOR_NET_SCOPE)), - tau) - update_critic = utils.soft_variables_update( - slim.get_trainable_variables( - utils.join_scope(self._scope, self.CRITIC_NET_SCOPE)), - slim.get_trainable_variables( - utils.join_scope(self._scope, self.TARGET_CRITIC_NET_SCOPE)), - tau) - return tf.group(update_actor, update_critic, name='update_targets') - - def get_trainable_critic_vars(self): - """Returns a list of trainable variables in the critic network. - - Returns: - A list of trainable variables in the critic network. - """ - return slim.get_trainable_variables( - utils.join_scope(self._scope, self.CRITIC_NET_SCOPE)) - - def get_trainable_actor_vars(self): - """Returns a list of trainable variables in the actor network. - - Returns: - A list of trainable variables in the actor network. - """ - return slim.get_trainable_variables( - utils.join_scope(self._scope, self.ACTOR_NET_SCOPE)) - - def get_critic_vars(self): - """Returns a list of all variables in the critic network. - - Returns: - A list of trainable variables in the critic network. - """ - return slim.get_model_variables( - utils.join_scope(self._scope, self.CRITIC_NET_SCOPE)) - - def get_actor_vars(self): - """Returns a list of all variables in the actor network. - - Returns: - A list of trainable variables in the actor network. - """ - return slim.get_model_variables( - utils.join_scope(self._scope, self.ACTOR_NET_SCOPE)) - - def _validate_states(self, states): - """Raises a value error if `states` does not have the expected shape. - - Args: - states: A tensor. - Raises: - ValueError: If states.shape or states.dtype are not compatible with - observation_spec. - """ - states.shape.assert_is_compatible_with(self._state_shape) - if not states.dtype.is_compatible_with(self._observation_spec.dtype): - raise ValueError('states.dtype={} is not compatible with' - ' observation_spec.dtype={}'.format( - states.dtype, self._observation_spec.dtype)) - - def _validate_actions(self, actions): - """Raises a value error if `actions` does not have the expected shape. - - Args: - actions: A tensor. - Raises: - ValueError: If actions.shape or actions.dtype are not compatible with - action_spec. - """ - actions.shape.assert_is_compatible_with(self._action_shape) - if not actions.dtype.is_compatible_with(self._action_spec.dtype): - raise ValueError('actions.dtype={} is not compatible with' - ' action_spec.dtype={}'.format( - actions.dtype, self._action_spec.dtype)) - - -@gin.configurable -class TD3Agent(DdpgAgent): - """An RL agent that learns using the TD3 algorithm.""" - - ACTOR_NET_SCOPE = 'actor_net' - CRITIC_NET_SCOPE = 'critic_net' - CRITIC_NET2_SCOPE = 'critic_net2' - TARGET_ACTOR_NET_SCOPE = 'target_actor_net' - TARGET_CRITIC_NET_SCOPE = 'target_critic_net' - TARGET_CRITIC_NET2_SCOPE = 'target_critic_net2' - - def __init__(self, - observation_spec, - action_spec, - actor_net=networks.actor_net, - critic_net=networks.critic_net, - td_errors_loss=tf.losses.huber_loss, - dqda_clipping=0., - actions_regularizer=0., - target_q_clipping=None, - residual_phi=0.0, - debug_summaries=False): - """Constructs a TD3 agent. - - Args: - observation_spec: A TensorSpec defining the observations. - action_spec: A BoundedTensorSpec defining the actions. - actor_net: A callable that creates the actor network. Must take the - following arguments: states, num_actions. Please see networks.actor_net - for an example. - critic_net: A callable that creates the critic network. Must take the - following arguments: states, actions. Please see networks.critic_net - for an example. - td_errors_loss: A callable defining the loss function for the critic - td error. - dqda_clipping: (float) clips the gradient dqda element-wise between - [-dqda_clipping, dqda_clipping]. Does not perform clipping if - dqda_clipping == 0. - actions_regularizer: A scalar, when positive penalizes the norm of the - actions. This can prevent saturation of actions for the actor_loss. - target_q_clipping: (tuple of floats) clips target q values within - (low, high) values when computing the critic loss. - residual_phi: (float) [0.0, 1.0] Residual algorithm parameter that - interpolates between Q-learning and residual gradient algorithm. - http://www.leemon.com/papers/1995b.pdf - debug_summaries: If True, add summaries to help debug behavior. - Raises: - ValueError: If 'dqda_clipping' is < 0. - """ - self._observation_spec = observation_spec[0] - self._action_spec = action_spec[0] - self._state_shape = tf.TensorShape([None]).concatenate( - self._observation_spec.shape) - self._action_shape = tf.TensorShape([None]).concatenate( - self._action_spec.shape) - self._num_action_dims = self._action_spec.shape.num_elements() - - self._scope = tf.get_variable_scope().name - self._actor_net = tf.make_template( - self.ACTOR_NET_SCOPE, actor_net, create_scope_now_=True) - self._critic_net = tf.make_template( - self.CRITIC_NET_SCOPE, critic_net, create_scope_now_=True) - self._critic_net2 = tf.make_template( - self.CRITIC_NET2_SCOPE, critic_net, create_scope_now_=True) - self._target_actor_net = tf.make_template( - self.TARGET_ACTOR_NET_SCOPE, actor_net, create_scope_now_=True) - self._target_critic_net = tf.make_template( - self.TARGET_CRITIC_NET_SCOPE, critic_net, create_scope_now_=True) - self._target_critic_net2 = tf.make_template( - self.TARGET_CRITIC_NET2_SCOPE, critic_net, create_scope_now_=True) - self._td_errors_loss = td_errors_loss - if dqda_clipping < 0: - raise ValueError('dqda_clipping must be >= 0.') - self._dqda_clipping = dqda_clipping - self._actions_regularizer = actions_regularizer - self._target_q_clipping = target_q_clipping - self._residual_phi = residual_phi - self._debug_summaries = debug_summaries - - def get_trainable_critic_vars(self): - """Returns a list of trainable variables in the critic network. - NOTE: This gets the vars of both critic networks. - - Returns: - A list of trainable variables in the critic network. - """ - return ( - slim.get_trainable_variables( - utils.join_scope(self._scope, self.CRITIC_NET_SCOPE))) - - def critic_net(self, states, actions, for_critic_loss=False): - """Returns the output of the critic network. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] tensor representing a batch - of actions. - Returns: - q values: A [batch_size] tensor of q values. - Raises: - ValueError: If `states` or `actions' do not have the expected dimensions. - """ - values1 = self._critic_net(states, actions, - for_critic_loss=for_critic_loss) - values2 = self._critic_net2(states, actions, - for_critic_loss=for_critic_loss) - if for_critic_loss: - return values1, values2 - return values1 - - def target_critic_net(self, states, actions, for_critic_loss=False): - """Returns the output of the target critic network. - - The target network is used to compute stable targets for training. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] tensor representing a batch - of actions. - Returns: - q values: A [batch_size] tensor of q values. - Raises: - ValueError: If `states` or `actions' do not have the expected dimensions. - """ - self._validate_states(states) - self._validate_actions(actions) - values1 = tf.stop_gradient( - self._target_critic_net(states, actions, - for_critic_loss=for_critic_loss)) - values2 = tf.stop_gradient( - self._target_critic_net2(states, actions, - for_critic_loss=for_critic_loss)) - if for_critic_loss: - return values1, values2 - return values1 - - def value_net(self, states, for_critic_loss=False): - """Returns the output of the critic evaluated with the actor. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - Returns: - q values: A [batch_size] tensor of q values. - """ - actions = self.actor_net(states) - return self.critic_net(states, actions, - for_critic_loss=for_critic_loss) - - def target_value_net(self, states, for_critic_loss=False): - """Returns the output of the target critic evaluated with the target actor. - - Args: - states: A [batch_size, num_state_dims] tensor representing a batch - of states. - Returns: - q values: A [batch_size] tensor of q values. - """ - target_actions = self.target_actor_net(states) - noise = tf.clip_by_value( - tf.random_normal(tf.shape(target_actions), stddev=0.2), -0.5, 0.5) - values1, values2 = self.target_critic_net( - states, target_actions + noise, - for_critic_loss=for_critic_loss) - values = tf.minimum(values1, values2) - return values, values - - @gin.configurable('td3_update_targets') - def update_targets(self, tau=1.0): - """Performs a soft update of the target network parameters. - - For each weight w_s in the actor/critic networks, and its corresponding - weight w_t in the target actor/critic networks, a soft update is: - w_t = (1- tau) x w_t + tau x ws - - Args: - tau: A float scalar in [0, 1] - Returns: - An operation that performs a soft update of the target network parameters. - Raises: - ValueError: If `tau` is not in [0, 1]. - """ - if tau < 0 or tau > 1: - raise ValueError('Input `tau` should be in [0, 1].') - update_actor = utils.soft_variables_update( - slim.get_trainable_variables( - utils.join_scope(self._scope, self.ACTOR_NET_SCOPE)), - slim.get_trainable_variables( - utils.join_scope(self._scope, self.TARGET_ACTOR_NET_SCOPE)), - tau) - # NOTE: This updates both critic networks. - update_critic = utils.soft_variables_update( - slim.get_trainable_variables( - utils.join_scope(self._scope, self.CRITIC_NET_SCOPE)), - slim.get_trainable_variables( - utils.join_scope(self._scope, self.TARGET_CRITIC_NET_SCOPE)), - tau) - return tf.group(update_actor, update_critic, name='update_targets') - - -def gen_debug_td_error_summaries( - target_q_values, q_values, td_targets, td_errors): - """Generates debug summaries for critic given a set of batch samples. - - Args: - target_q_values: set of predicted next stage values. - q_values: current predicted value for the critic network. - td_targets: discounted target_q_values with added next stage reward. - td_errors: the different between td_targets and q_values. - """ - with tf.name_scope('td_errors'): - tf.summary.histogram('td_targets', td_targets) - tf.summary.histogram('q_values', q_values) - tf.summary.histogram('target_q_values', target_q_values) - tf.summary.histogram('td_errors', td_errors) - with tf.name_scope('td_targets'): - tf.summary.scalar('mean', tf.reduce_mean(td_targets)) - tf.summary.scalar('max', tf.reduce_max(td_targets)) - tf.summary.scalar('min', tf.reduce_min(td_targets)) - with tf.name_scope('q_values'): - tf.summary.scalar('mean', tf.reduce_mean(q_values)) - tf.summary.scalar('max', tf.reduce_max(q_values)) - tf.summary.scalar('min', tf.reduce_min(q_values)) - with tf.name_scope('target_q_values'): - tf.summary.scalar('mean', tf.reduce_mean(target_q_values)) - tf.summary.scalar('max', tf.reduce_max(target_q_values)) - tf.summary.scalar('min', tf.reduce_min(target_q_values)) - with tf.name_scope('td_errors'): - tf.summary.scalar('mean', tf.reduce_mean(td_errors)) - tf.summary.scalar('max', tf.reduce_max(td_errors)) - tf.summary.scalar('min', tf.reduce_min(td_errors)) - tf.summary.scalar('mean_abs', tf.reduce_mean(tf.abs(td_errors))) diff --git a/research/efficient-hrl/agents/ddpg_networks.py b/research/efficient-hrl/agents/ddpg_networks.py deleted file mode 100644 index 63074dfb91c..00000000000 --- a/research/efficient-hrl/agents/ddpg_networks.py +++ /dev/null @@ -1,150 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Sample actor(policy) and critic(q) networks to use with DDPG/NAF agents. - -The DDPG networks are defined in "Section 7: Experiment Details" of -"Continuous control with deep reinforcement learning" - Lilicrap et al. -https://arxiv.org/abs/1509.02971 - -The NAF critic network is based on "Section 4" of "Continuous deep Q-learning -with model-based acceleration" - Gu et al. https://arxiv.org/pdf/1603.00748. -""" - -import tensorflow as tf -slim = tf.contrib.slim -import gin.tf - - -@gin.configurable('ddpg_critic_net') -def critic_net(states, actions, - for_critic_loss=False, - num_reward_dims=1, - states_hidden_layers=(400,), - actions_hidden_layers=None, - joint_hidden_layers=(300,), - weight_decay=0.0001, - normalizer_fn=None, - activation_fn=tf.nn.relu, - zero_obs=False, - images=False): - """Creates a critic that returns q values for the given states and actions. - - Args: - states: (castable to tf.float32) a [batch_size, num_state_dims] tensor - representing a batch of states. - actions: (castable to tf.float32) a [batch_size, num_action_dims] tensor - representing a batch of actions. - num_reward_dims: Number of reward dimensions. - states_hidden_layers: tuple of hidden layers units for states. - actions_hidden_layers: tuple of hidden layers units for actions. - joint_hidden_layers: tuple of hidden layers units after joining states - and actions using tf.concat(). - weight_decay: Weight decay for l2 weights regularizer. - normalizer_fn: Normalizer function, i.e. slim.layer_norm, - activation_fn: Activation function, i.e. tf.nn.relu, slim.leaky_relu, ... - Returns: - A tf.float32 [batch_size] tensor of q values, or a tf.float32 - [batch_size, num_reward_dims] tensor of vector q values if - num_reward_dims > 1. - """ - with slim.arg_scope( - [slim.fully_connected], - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - weights_regularizer=slim.l2_regularizer(weight_decay), - weights_initializer=slim.variance_scaling_initializer( - factor=1.0/3.0, mode='FAN_IN', uniform=True)): - - orig_states = tf.to_float(states) - #states = tf.to_float(states) - states = tf.concat([tf.to_float(states), tf.to_float(actions)], -1) #TD3 - if images or zero_obs: - states *= tf.constant([0.0] * 2 + [1.0] * (states.shape[1] - 2)) #LALA - actions = tf.to_float(actions) - if states_hidden_layers: - states = slim.stack(states, slim.fully_connected, states_hidden_layers, - scope='states') - if actions_hidden_layers: - actions = slim.stack(actions, slim.fully_connected, actions_hidden_layers, - scope='actions') - joint = tf.concat([states, actions], 1) - if joint_hidden_layers: - joint = slim.stack(joint, slim.fully_connected, joint_hidden_layers, - scope='joint') - with slim.arg_scope([slim.fully_connected], - weights_regularizer=None, - weights_initializer=tf.random_uniform_initializer( - minval=-0.003, maxval=0.003)): - value = slim.fully_connected(joint, num_reward_dims, - activation_fn=None, - normalizer_fn=None, - scope='q_value') - if num_reward_dims == 1: - value = tf.reshape(value, [-1]) - if not for_critic_loss and num_reward_dims > 1: - value = tf.reduce_sum( - value * tf.abs(orig_states[:, -num_reward_dims:]), -1) - return value - - -@gin.configurable('ddpg_actor_net') -def actor_net(states, action_spec, - hidden_layers=(400, 300), - normalizer_fn=None, - activation_fn=tf.nn.relu, - zero_obs=False, - images=False): - """Creates an actor that returns actions for the given states. - - Args: - states: (castable to tf.float32) a [batch_size, num_state_dims] tensor - representing a batch of states. - action_spec: (BoundedTensorSpec) A tensor spec indicating the shape - and range of actions. - hidden_layers: tuple of hidden layers units. - normalizer_fn: Normalizer function, i.e. slim.layer_norm, - activation_fn: Activation function, i.e. tf.nn.relu, slim.leaky_relu, ... - Returns: - A tf.float32 [batch_size, num_action_dims] tensor of actions. - """ - - with slim.arg_scope( - [slim.fully_connected], - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - weights_initializer=slim.variance_scaling_initializer( - factor=1.0/3.0, mode='FAN_IN', uniform=True)): - - states = tf.to_float(states) - orig_states = states - if images or zero_obs: # Zero-out x, y position. Hacky. - states *= tf.constant([0.0] * 2 + [1.0] * (states.shape[1] - 2)) - if hidden_layers: - states = slim.stack(states, slim.fully_connected, hidden_layers, - scope='states') - with slim.arg_scope([slim.fully_connected], - weights_initializer=tf.random_uniform_initializer( - minval=-0.003, maxval=0.003)): - actions = slim.fully_connected(states, - action_spec.shape.num_elements(), - scope='actions', - normalizer_fn=None, - activation_fn=tf.nn.tanh) - action_means = (action_spec.maximum + action_spec.minimum) / 2.0 - action_magnitudes = (action_spec.maximum - action_spec.minimum) / 2.0 - actions = action_means + action_magnitudes * actions - - return actions diff --git a/research/efficient-hrl/cond_fn.py b/research/efficient-hrl/cond_fn.py deleted file mode 100644 index cd1a276e136..00000000000 --- a/research/efficient-hrl/cond_fn.py +++ /dev/null @@ -1,244 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Defines many boolean functions indicating when to step and reset. -""" - -import tensorflow as tf -import gin.tf - - -@gin.configurable -def env_transition(agent, state, action, transition_type, environment_steps, - num_episodes): - """True if the transition_type is TRANSITION or FINAL_TRANSITION. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - Returns: - cond: Returns an op that evaluates to true if the transition type is - not RESTARTING - """ - del agent, state, action, num_episodes, environment_steps - cond = tf.logical_not(transition_type) - return cond - - -@gin.configurable -def env_restart(agent, state, action, transition_type, environment_steps, - num_episodes): - """True if the transition_type is RESTARTING. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - Returns: - cond: Returns an op that evaluates to true if the transition type equals - RESTARTING. - """ - del agent, state, action, num_episodes, environment_steps - cond = tf.identity(transition_type) - return cond - - -@gin.configurable -def every_n_steps(agent, - state, - action, - transition_type, - environment_steps, - num_episodes, - n=150): - """True once every n steps. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - n: Return true once every n steps. - Returns: - cond: Returns an op that evaluates to true if environment_steps - equals 0 mod n. We increment the step before checking this condition, so - we do not need to add one to environment_steps. - """ - del agent, state, action, transition_type, num_episodes - cond = tf.equal(tf.mod(environment_steps, n), 0) - return cond - - -@gin.configurable -def every_n_episodes(agent, - state, - action, - transition_type, - environment_steps, - num_episodes, - n=2, - steps_per_episode=None): - """True once every n episodes. - - Specifically, evaluates to True on the 0th step of every nth episode. - Unlike environment_steps, num_episodes starts at 0, so we do want to add - one to ensure it does not reset on the first call. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - n: Return true once every n episodes. - steps_per_episode: How many steps per episode. Needed to determine when a - new episode starts. - Returns: - cond: Returns an op that evaluates to true on the last step of the episode - (i.e. if num_episodes equals 0 mod n). - """ - assert steps_per_episode is not None - del agent, action, transition_type - ant_fell = tf.logical_or(state[2] < 0.2, state[2] > 1.0) - cond = tf.logical_and( - tf.logical_or( - ant_fell, - tf.equal(tf.mod(num_episodes + 1, n), 0)), - tf.equal(tf.mod(environment_steps, steps_per_episode), 0)) - return cond - - -@gin.configurable -def failed_reset_after_n_episodes(agent, - state, - action, - transition_type, - environment_steps, - num_episodes, - steps_per_episode=None, - reset_state=None, - max_dist=1.0, - epsilon=1e-10): - """Every n episodes, returns True if the reset agent fails to return. - - Specifically, evaluates to True if the distance between the state and the - reset state is greater than max_dist at the end of the episode. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - steps_per_episode: How many steps per episode. Needed to determine when a - new episode starts. - reset_state: State to which the reset controller should return. - max_dist: Agent is considered to have successfully reset if its distance - from the reset_state is less than max_dist. - epsilon: small offset to ensure non-negative/zero distance. - Returns: - cond: Returns an op that evaluates to true if num_episodes+1 equals 0 - mod n. We add one to the num_episodes so the environment is not reset after - the 0th step. - """ - assert steps_per_episode is not None - assert reset_state is not None - del agent, state, action, transition_type, num_episodes - dist = tf.sqrt( - tf.reduce_sum(tf.squared_difference(state, reset_state)) + epsilon) - cond = tf.logical_and( - tf.greater(dist, tf.constant(max_dist)), - tf.equal(tf.mod(environment_steps, steps_per_episode), 0)) - return cond - - -@gin.configurable -def q_too_small(agent, - state, - action, - transition_type, - environment_steps, - num_episodes, - q_min=0.5): - """True of q is too small. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - q_min: Returns true if the qval is less than q_min - Returns: - cond: Returns an op that evaluates to true if qval is less than q_min. - """ - del transition_type, environment_steps, num_episodes - state_for_reset_agent = tf.stack(state[:-1], tf.constant([0], dtype=tf.float)) - qval = agent.BASE_AGENT_CLASS.critic_net( - tf.expand_dims(state_for_reset_agent, 0), tf.expand_dims(action, 0))[0, :] - cond = tf.greater(tf.constant(q_min), qval) - return cond - - -@gin.configurable -def true_fn(agent, state, action, transition_type, environment_steps, - num_episodes): - """Returns an op that evaluates to true. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - Returns: - cond: op that always evaluates to True. - """ - del agent, state, action, transition_type, environment_steps, num_episodes - cond = tf.constant(True, dtype=tf.bool) - return cond - - -@gin.configurable -def false_fn(agent, state, action, transition_type, environment_steps, - num_episodes): - """Returns an op that evaluates to false. - - Args: - agent: RL agent. - state: A [num_state_dims] tensor representing a state. - action: Action performed. - transition_type: Type of transition after action - environment_steps: Number of steps performed by environment. - num_episodes: Number of episodes. - Returns: - cond: op that always evaluates to False. - """ - del agent, state, action, transition_type, environment_steps, num_episodes - cond = tf.constant(False, dtype=tf.bool) - return cond diff --git a/research/efficient-hrl/configs/base_uvf.gin b/research/efficient-hrl/configs/base_uvf.gin deleted file mode 100644 index 2f3f47b67a3..00000000000 --- a/research/efficient-hrl/configs/base_uvf.gin +++ /dev/null @@ -1,68 +0,0 @@ -#-*-Python-*- -import gin.tf.external_configurables - -create_maze_env.top_down_view = %IMAGES -## Create the agent -AGENT_CLASS = @UvfAgent -UvfAgent.tf_context = %CONTEXT -UvfAgent.actor_net = @agent/ddpg_actor_net -UvfAgent.critic_net = @agent/ddpg_critic_net -UvfAgent.dqda_clipping = 0.0 -UvfAgent.td_errors_loss = @tf.losses.huber_loss -UvfAgent.target_q_clipping = %TARGET_Q_CLIPPING - -# Create meta agent -META_CLASS = @MetaAgent -MetaAgent.tf_context = %META_CONTEXT -MetaAgent.sub_context = %CONTEXT -MetaAgent.actor_net = @meta/ddpg_actor_net -MetaAgent.critic_net = @meta/ddpg_critic_net -MetaAgent.dqda_clipping = 0.0 -MetaAgent.td_errors_loss = @tf.losses.huber_loss -MetaAgent.target_q_clipping = %TARGET_Q_CLIPPING - -# Create state preprocess -STATE_PREPROCESS_CLASS = @StatePreprocess -StatePreprocess.ndims = %SUBGOAL_DIM -state_preprocess_net.states_hidden_layers = (100, 100) -state_preprocess_net.num_output_dims = %SUBGOAL_DIM -state_preprocess_net.images = %IMAGES -action_embed_net.num_output_dims = %SUBGOAL_DIM -INVERSE_DYNAMICS_CLASS = @InverseDynamics - -# actor_net -ACTOR_HIDDEN_SIZE_1 = 300 -ACTOR_HIDDEN_SIZE_2 = 300 -agent/ddpg_actor_net.hidden_layers = (%ACTOR_HIDDEN_SIZE_1, %ACTOR_HIDDEN_SIZE_2) -agent/ddpg_actor_net.activation_fn = @tf.nn.relu -agent/ddpg_actor_net.zero_obs = %ZERO_OBS -agent/ddpg_actor_net.images = %IMAGES -meta/ddpg_actor_net.hidden_layers = (%ACTOR_HIDDEN_SIZE_1, %ACTOR_HIDDEN_SIZE_2) -meta/ddpg_actor_net.activation_fn = @tf.nn.relu -meta/ddpg_actor_net.zero_obs = False -meta/ddpg_actor_net.images = %IMAGES -# critic_net -CRITIC_HIDDEN_SIZE_1 = 300 -CRITIC_HIDDEN_SIZE_2 = 300 -agent/ddpg_critic_net.states_hidden_layers = (%CRITIC_HIDDEN_SIZE_1,) -agent/ddpg_critic_net.actions_hidden_layers = None -agent/ddpg_critic_net.joint_hidden_layers = (%CRITIC_HIDDEN_SIZE_2,) -agent/ddpg_critic_net.weight_decay = 0.0 -agent/ddpg_critic_net.activation_fn = @tf.nn.relu -agent/ddpg_critic_net.zero_obs = %ZERO_OBS -agent/ddpg_critic_net.images = %IMAGES -meta/ddpg_critic_net.states_hidden_layers = (%CRITIC_HIDDEN_SIZE_1,) -meta/ddpg_critic_net.actions_hidden_layers = None -meta/ddpg_critic_net.joint_hidden_layers = (%CRITIC_HIDDEN_SIZE_2,) -meta/ddpg_critic_net.weight_decay = 0.0 -meta/ddpg_critic_net.activation_fn = @tf.nn.relu -meta/ddpg_critic_net.zero_obs = False -meta/ddpg_critic_net.images = %IMAGES - -tf.losses.huber_loss.delta = 1.0 -# Sample action -uvf_add_noise_fn.stddev = 1.0 -meta_add_noise_fn.stddev = %META_EXPLORE_NOISE -# Update targets -ddpg_update_targets.tau = 0.001 -td3_update_targets.tau = 0.005 diff --git a/research/efficient-hrl/configs/eval_uvf.gin b/research/efficient-hrl/configs/eval_uvf.gin deleted file mode 100644 index 7a58241e06a..00000000000 --- a/research/efficient-hrl/configs/eval_uvf.gin +++ /dev/null @@ -1,14 +0,0 @@ -#-*-Python-*- -# Config eval -evaluate.environment = @create_maze_env() -evaluate.agent_class = %AGENT_CLASS -evaluate.meta_agent_class = %META_CLASS -evaluate.state_preprocess_class = %STATE_PREPROCESS_CLASS -evaluate.num_episodes_eval = 50 -evaluate.num_episodes_videos = 1 -evaluate.gamma = 1.0 -evaluate.eval_interval_secs = 1 -evaluate.generate_videos = False -evaluate.generate_summaries = True -evaluate.eval_modes = %EVAL_MODES -evaluate.max_steps_per_episode = %RESET_EPISODE_PERIOD diff --git a/research/efficient-hrl/configs/train_uvf.gin b/research/efficient-hrl/configs/train_uvf.gin deleted file mode 100644 index 8b02d7a6cb4..00000000000 --- a/research/efficient-hrl/configs/train_uvf.gin +++ /dev/null @@ -1,52 +0,0 @@ -#-*-Python-*- -# Create replay_buffer -agent/CircularBuffer.buffer_size = 200000 -meta/CircularBuffer.buffer_size = 200000 -agent/CircularBuffer.scope = "agent" -meta/CircularBuffer.scope = "meta" - -# Config train -train_uvf.environment = @create_maze_env() -train_uvf.agent_class = %AGENT_CLASS -train_uvf.meta_agent_class = %META_CLASS -train_uvf.state_preprocess_class = %STATE_PREPROCESS_CLASS -train_uvf.inverse_dynamics_class = %INVERSE_DYNAMICS_CLASS -train_uvf.replay_buffer = @agent/CircularBuffer() -train_uvf.meta_replay_buffer = @meta/CircularBuffer() -train_uvf.critic_optimizer = @critic/AdamOptimizer() -train_uvf.actor_optimizer = @actor/AdamOptimizer() -train_uvf.meta_critic_optimizer = @meta_critic/AdamOptimizer() -train_uvf.meta_actor_optimizer = @meta_actor/AdamOptimizer() -train_uvf.repr_optimizer = @repr/AdamOptimizer() -train_uvf.num_episodes_train = 25000 -train_uvf.batch_size = 100 -train_uvf.initial_episodes = 5 -train_uvf.gamma = 0.99 -train_uvf.meta_gamma = 0.99 -train_uvf.reward_scale_factor = 1.0 -train_uvf.target_update_period = 2 -train_uvf.num_updates_per_observation = 1 -train_uvf.num_collect_per_update = 1 -train_uvf.num_collect_per_meta_update = 10 -train_uvf.debug_summaries = False -train_uvf.log_every_n_steps = 1000 -train_uvf.save_policy_every_n_steps =100000 - -# Config Optimizers -critic/AdamOptimizer.learning_rate = 0.001 -critic/AdamOptimizer.beta1 = 0.9 -critic/AdamOptimizer.beta2 = 0.999 -actor/AdamOptimizer.learning_rate = 0.0001 -actor/AdamOptimizer.beta1 = 0.9 -actor/AdamOptimizer.beta2 = 0.999 - -meta_critic/AdamOptimizer.learning_rate = 0.001 -meta_critic/AdamOptimizer.beta1 = 0.9 -meta_critic/AdamOptimizer.beta2 = 0.999 -meta_actor/AdamOptimizer.learning_rate = 0.0001 -meta_actor/AdamOptimizer.beta1 = 0.9 -meta_actor/AdamOptimizer.beta2 = 0.999 - -repr/AdamOptimizer.learning_rate = 0.0001 -repr/AdamOptimizer.beta1 = 0.9 -repr/AdamOptimizer.beta2 = 0.999 diff --git a/research/efficient-hrl/context/__init__.py b/research/efficient-hrl/context/__init__.py deleted file mode 100644 index 8b137891791..00000000000 --- a/research/efficient-hrl/context/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/research/efficient-hrl/context/configs/ant_block.gin b/research/efficient-hrl/context/configs/ant_block.gin deleted file mode 100644 index d5bd4f01e01..00000000000 --- a/research/efficient-hrl/context/configs/ant_block.gin +++ /dev/null @@ -1,67 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntBlock" -ZERO_OBS = False -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-4, -4), (20, 20)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1", "eval2", "eval3"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval1": [@eval1/ConstantSampler], - "eval2": [@eval2/ConstantSampler], - "eval3": [@eval3/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [3, 4] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [16, 0] -eval2/ConstantSampler.value = [16, 16] -eval3/ConstantSampler.value = [0, 16] diff --git a/research/efficient-hrl/context/configs/ant_block_maze.gin b/research/efficient-hrl/context/configs/ant_block_maze.gin deleted file mode 100644 index cebf775be12..00000000000 --- a/research/efficient-hrl/context/configs/ant_block_maze.gin +++ /dev/null @@ -1,67 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntBlockMaze" -ZERO_OBS = False -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-4, -4), (12, 20)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1", "eval2", "eval3"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval1": [@eval1/ConstantSampler], - "eval2": [@eval2/ConstantSampler], - "eval3": [@eval3/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [3, 4] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [8, 0] -eval2/ConstantSampler.value = [8, 16] -eval3/ConstantSampler.value = [0, 16] diff --git a/research/efficient-hrl/context/configs/ant_fall_multi.gin b/research/efficient-hrl/context/configs/ant_fall_multi.gin deleted file mode 100644 index eb89ad0cb16..00000000000 --- a/research/efficient-hrl/context/configs/ant_fall_multi.gin +++ /dev/null @@ -1,62 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntFall" -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-4, -4, 0), (12, 28, 5)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [3] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval1": [@eval1/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1, 2] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [0, 27, 4.5] diff --git a/research/efficient-hrl/context/configs/ant_fall_multi_img.gin b/research/efficient-hrl/context/configs/ant_fall_multi_img.gin deleted file mode 100644 index b54fb7c9196..00000000000 --- a/research/efficient-hrl/context/configs/ant_fall_multi_img.gin +++ /dev/null @@ -1,68 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntFall" -IMAGES = True - -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-4, -4, 0), (12, 28, 5)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [3] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval1": [@eval1/ConstantSampler], -} -meta/Context.context_transition_fn = @task/relative_context_transition_fn -meta/Context.context_multi_transition_fn = @task/relative_context_multi_transition_fn -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1, 2] -task/negative_distance.relative_context = True -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -task/relative_context_transition_fn.k = 3 -task/relative_context_multi_transition_fn.k = 3 -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [0, 27, 0] diff --git a/research/efficient-hrl/context/configs/ant_fall_single.gin b/research/efficient-hrl/context/configs/ant_fall_single.gin deleted file mode 100644 index 56bbc070072..00000000000 --- a/research/efficient-hrl/context/configs/ant_fall_single.gin +++ /dev/null @@ -1,62 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntFall" -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-4, -4, 0), (12, 28, 5)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [3] -meta/Context.samplers = { - "train": [@eval1/ConstantSampler], - "explore": [@eval1/ConstantSampler], - "eval1": [@eval1/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1, 2] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [0, 27, 4.5] diff --git a/research/efficient-hrl/context/configs/ant_maze.gin b/research/efficient-hrl/context/configs/ant_maze.gin deleted file mode 100644 index 3a0b73e30d7..00000000000 --- a/research/efficient-hrl/context/configs/ant_maze.gin +++ /dev/null @@ -1,66 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntMaze" -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-4, -4), (20, 20)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1", "eval2", "eval3"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval1": [@eval1/ConstantSampler], - "eval2": [@eval2/ConstantSampler], - "eval3": [@eval3/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [16, 0] -eval2/ConstantSampler.value = [16, 16] -eval3/ConstantSampler.value = [0, 16] diff --git a/research/efficient-hrl/context/configs/ant_maze_img.gin b/research/efficient-hrl/context/configs/ant_maze_img.gin deleted file mode 100644 index ceed65a0884..00000000000 --- a/research/efficient-hrl/context/configs/ant_maze_img.gin +++ /dev/null @@ -1,72 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntMaze" -IMAGES = True - -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-4, -4), (20, 20)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1", "eval2", "eval3"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval1": [@eval1/ConstantSampler], - "eval2": [@eval2/ConstantSampler], - "eval3": [@eval3/ConstantSampler], -} -meta/Context.context_transition_fn = @task/relative_context_transition_fn -meta/Context.context_multi_transition_fn = @task/relative_context_multi_transition_fn -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1] -task/negative_distance.relative_context = True -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -task/relative_context_transition_fn.k = 2 -task/relative_context_multi_transition_fn.k = 2 -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [16, 0] -eval2/ConstantSampler.value = [16, 16] -eval3/ConstantSampler.value = [0, 16] diff --git a/research/efficient-hrl/context/configs/ant_push_multi.gin b/research/efficient-hrl/context/configs/ant_push_multi.gin deleted file mode 100644 index db9b4ed7bbe..00000000000 --- a/research/efficient-hrl/context/configs/ant_push_multi.gin +++ /dev/null @@ -1,62 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntPush" -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-16, -4), (16, 20)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval2"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval2": [@eval2/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval2/ConstantSampler.value = [0, 19] diff --git a/research/efficient-hrl/context/configs/ant_push_multi_img.gin b/research/efficient-hrl/context/configs/ant_push_multi_img.gin deleted file mode 100644 index abdc43402fc..00000000000 --- a/research/efficient-hrl/context/configs/ant_push_multi_img.gin +++ /dev/null @@ -1,68 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntPush" -IMAGES = True - -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-16, -4), (16, 20)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval2"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval2": [@eval2/ConstantSampler], -} -meta/Context.context_transition_fn = @task/relative_context_transition_fn -meta/Context.context_multi_transition_fn = @task/relative_context_multi_transition_fn -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1] -task/negative_distance.relative_context = True -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -task/relative_context_transition_fn.k = 2 -task/relative_context_multi_transition_fn.k = 2 -MetaAgent.k = %SUBGOAL_DIM - -eval2/ConstantSampler.value = [0, 19] diff --git a/research/efficient-hrl/context/configs/ant_push_single.gin b/research/efficient-hrl/context/configs/ant_push_single.gin deleted file mode 100644 index e85c5dfba4d..00000000000 --- a/research/efficient-hrl/context/configs/ant_push_single.gin +++ /dev/null @@ -1,62 +0,0 @@ -#-*-Python-*- -create_maze_env.env_name = "AntPush" -context_range = (%CONTEXT_RANGE_MIN, %CONTEXT_RANGE_MAX) -meta_context_range = ((-16, -4), (16, 20)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval2"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@eval2/ConstantSampler], - "explore": [@eval2/ConstantSampler], - "eval2": [@eval2/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval2/ConstantSampler.value = [0, 19] diff --git a/research/efficient-hrl/context/configs/default.gin b/research/efficient-hrl/context/configs/default.gin deleted file mode 100644 index 65f91e5292d..00000000000 --- a/research/efficient-hrl/context/configs/default.gin +++ /dev/null @@ -1,12 +0,0 @@ -#-*-Python-*- -ENV_CONTEXT = None -EVAL_MODES = ["eval"] -TARGET_Q_CLIPPING = None -RESET_EPISODE_PERIOD = None -ZERO_OBS = False -CONTEXT_RANGE_MIN = -10 -CONTEXT_RANGE_MAX = 10 -SUBGOAL_DIM = 2 - -uvf/negative_distance.summarize = False -uvf/negative_distance.relative_context = True diff --git a/research/efficient-hrl/context/configs/hiro_orig.gin b/research/efficient-hrl/context/configs/hiro_orig.gin deleted file mode 100644 index e39ba96be7b..00000000000 --- a/research/efficient-hrl/context/configs/hiro_orig.gin +++ /dev/null @@ -1,14 +0,0 @@ -#-*-Python-*- -ENV_CONTEXT = None -EVAL_MODES = ["eval"] -TARGET_Q_CLIPPING = None -RESET_EPISODE_PERIOD = None -ZERO_OBS = True -IMAGES = False -CONTEXT_RANGE_MIN = (-10, -10, -0.5, -1, -1, -1, -1, -0.5, -0.3, -0.5, -0.3, -0.5, -0.3, -0.5, -0.3) -CONTEXT_RANGE_MAX = ( 10, 10, 0.5, 1, 1, 1, 1, 0.5, 0.3, 0.5, 0.3, 0.5, 0.3, 0.5, 0.3) -SUBGOAL_DIM = 15 -META_EXPLORE_NOISE = 1.0 - -uvf/negative_distance.summarize = False -uvf/negative_distance.relative_context = True diff --git a/research/efficient-hrl/context/configs/hiro_repr.gin b/research/efficient-hrl/context/configs/hiro_repr.gin deleted file mode 100644 index a0a8057bd3c..00000000000 --- a/research/efficient-hrl/context/configs/hiro_repr.gin +++ /dev/null @@ -1,18 +0,0 @@ -#-*-Python-*- -ENV_CONTEXT = None -EVAL_MODES = ["eval"] -TARGET_Q_CLIPPING = None -RESET_EPISODE_PERIOD = None -ZERO_OBS = False -IMAGES = False -CONTEXT_RANGE_MIN = -10 -CONTEXT_RANGE_MAX = 10 -SUBGOAL_DIM = 2 -META_EXPLORE_NOISE = 5.0 - -StatePreprocess.trainable = True -StatePreprocess.state_preprocess_net = @state_preprocess_net -StatePreprocess.action_embed_net = @action_embed_net - -uvf/negative_distance.summarize = False -uvf/negative_distance.relative_context = True diff --git a/research/efficient-hrl/context/configs/hiro_xy.gin b/research/efficient-hrl/context/configs/hiro_xy.gin deleted file mode 100644 index f35026c9e24..00000000000 --- a/research/efficient-hrl/context/configs/hiro_xy.gin +++ /dev/null @@ -1,14 +0,0 @@ -#-*-Python-*- -ENV_CONTEXT = None -EVAL_MODES = ["eval"] -TARGET_Q_CLIPPING = None -RESET_EPISODE_PERIOD = None -ZERO_OBS = False -IMAGES = False -CONTEXT_RANGE_MIN = -10 -CONTEXT_RANGE_MAX = 10 -SUBGOAL_DIM = 2 -META_EXPLORE_NOISE = 1.0 - -uvf/negative_distance.summarize = False -uvf/negative_distance.relative_context = True diff --git a/research/efficient-hrl/context/configs/point_maze.gin b/research/efficient-hrl/context/configs/point_maze.gin deleted file mode 100644 index 0ea67d2d5ff..00000000000 --- a/research/efficient-hrl/context/configs/point_maze.gin +++ /dev/null @@ -1,73 +0,0 @@ -#-*-Python-*- -# NOTE: For best training, low-level exploration (uvf_add_noise_fn.stddev) -# should be reduced to around 0.1. -create_maze_env.env_name = "PointMaze" -context_range_min = -10 -context_range_max = 10 -context_range = (%context_range_min, %context_range_max) -meta_context_range = ((-2, -2), (10, 10)) - -RESET_EPISODE_PERIOD = 500 -RESET_ENV_PERIOD = 1 -# End episode every N steps -UvfAgent.reset_episode_cond_fn = @every_n_steps -every_n_steps.n = %RESET_EPISODE_PERIOD -train_uvf.max_steps_per_episode = %RESET_EPISODE_PERIOD -# Do a manual reset every N episodes -UvfAgent.reset_env_cond_fn = @every_n_episodes -every_n_episodes.n = %RESET_ENV_PERIOD -every_n_episodes.steps_per_episode = %RESET_EPISODE_PERIOD - -## Config defaults -EVAL_MODES = ["eval1", "eval2", "eval3"] - -## Config agent -CONTEXT = @agent/Context -META_CONTEXT = @meta/Context - -## Config agent context -agent/Context.context_ranges = [%context_range] -agent/Context.context_shapes = [%SUBGOAL_DIM] -agent/Context.meta_action_every_n = 10 -agent/Context.samplers = { - "train": [@train/DirectionSampler], - "explore": [@train/DirectionSampler], - "eval1": [@uvf_eval1/ConstantSampler], - "eval2": [@uvf_eval2/ConstantSampler], - "eval3": [@uvf_eval3/ConstantSampler], -} - -agent/Context.context_transition_fn = @relative_context_transition_fn -agent/Context.context_multi_transition_fn = @relative_context_multi_transition_fn - -agent/Context.reward_fn = @uvf/negative_distance - -## Config meta context -meta/Context.context_ranges = [%meta_context_range] -meta/Context.context_shapes = [2] -meta/Context.samplers = { - "train": [@train/RandomSampler], - "explore": [@train/RandomSampler], - "eval1": [@eval1/ConstantSampler], - "eval2": [@eval2/ConstantSampler], - "eval3": [@eval3/ConstantSampler], -} -meta/Context.reward_fn = @task/negative_distance - -## Config rewards -task/negative_distance.state_indices = [0, 1] -task/negative_distance.relative_context = False -task/negative_distance.diff = False -task/negative_distance.offset = 0.0 - -## Config samplers -train/RandomSampler.context_range = %meta_context_range -train/DirectionSampler.context_range = %context_range -train/DirectionSampler.k = %SUBGOAL_DIM -relative_context_transition_fn.k = %SUBGOAL_DIM -relative_context_multi_transition_fn.k = %SUBGOAL_DIM -MetaAgent.k = %SUBGOAL_DIM - -eval1/ConstantSampler.value = [8, 0] -eval2/ConstantSampler.value = [8, 8] -eval3/ConstantSampler.value = [0, 8] diff --git a/research/efficient-hrl/context/context.py b/research/efficient-hrl/context/context.py deleted file mode 100644 index 76be00b4966..00000000000 --- a/research/efficient-hrl/context/context.py +++ /dev/null @@ -1,467 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Context for Universal Value Function agents. - -A context specifies a list of contextual variables, each with - own sampling and reward computation methods. - -Examples of contextual variables include - goal states, reward combination vectors, etc. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import numpy as np -import tensorflow as tf -from tf_agents import specs -import gin.tf -from utils import utils as uvf_utils - - -@gin.configurable -class Context(object): - """Base context.""" - VAR_NAME = 'action' - - def __init__(self, - tf_env, - context_ranges=None, - context_shapes=None, - state_indices=None, - variable_indices=None, - gamma_index=None, - settable_context=False, - timers=None, - samplers=None, - reward_weights=None, - reward_fn=None, - random_sampler_mode='random', - normalizers=None, - context_transition_fn=None, - context_multi_transition_fn=None, - meta_action_every_n=None): - self._tf_env = tf_env - self.variable_indices = variable_indices - self.gamma_index = gamma_index - self._settable_context = settable_context - self.timers = timers - self._context_transition_fn = context_transition_fn - self._context_multi_transition_fn = context_multi_transition_fn - self._random_sampler_mode = random_sampler_mode - - # assign specs - self._obs_spec = self._tf_env.observation_spec() - self._context_shapes = tuple([ - shape if shape is not None else self._obs_spec.shape - for shape in context_shapes - ]) - self.context_specs = tuple([ - specs.TensorSpec(dtype=self._obs_spec.dtype, shape=shape) - for shape in self._context_shapes - ]) - if context_ranges is not None: - self.context_ranges = context_ranges - else: - self.context_ranges = [None] * len(self._context_shapes) - - self.context_as_action_specs = tuple([ - specs.BoundedTensorSpec( - shape=shape, - dtype=(tf.float32 if self._obs_spec.dtype in - [tf.float32, tf.float64] else self._obs_spec.dtype), - minimum=context_range[0], - maximum=context_range[-1]) - for shape, context_range in zip(self._context_shapes, self.context_ranges) - ]) - - if state_indices is not None: - self.state_indices = state_indices - else: - self.state_indices = [None] * len(self._context_shapes) - if self.variable_indices is not None and self.n != len( - self.variable_indices): - raise ValueError( - 'variable_indices (%s) must have the same length as contexts (%s).' % - (self.variable_indices, self.context_specs)) - assert self.n == len(self.context_ranges) - assert self.n == len(self.state_indices) - - # assign reward/sampler fns - self._sampler_fns = dict() - self._samplers = dict() - self._reward_fns = dict() - - # assign reward fns - self._add_custom_reward_fns() - reward_weights = reward_weights or None - self._reward_fn = self._make_reward_fn(reward_fn, reward_weights) - - # assign samplers - self._add_custom_sampler_fns() - for mode, sampler_fns in samplers.items(): - self._make_sampler_fn(sampler_fns, mode) - - # create normalizers - if normalizers is None: - self._normalizers = [None] * len(self.context_specs) - else: - self._normalizers = [ - normalizer(tf.zeros(shape=spec.shape, dtype=spec.dtype)) - if normalizer is not None else None - for normalizer, spec in zip(normalizers, self.context_specs) - ] - assert self.n == len(self._normalizers) - - self.meta_action_every_n = meta_action_every_n - - # create vars - self.context_vars = {} - self.timer_vars = {} - self.create_vars(self.VAR_NAME) - self.t = tf.Variable( - tf.zeros(shape=(), dtype=tf.int32), name='num_timer_steps') - - def _add_custom_reward_fns(self): - pass - - def _add_custom_sampler_fns(self): - pass - - def sample_random_contexts(self, batch_size): - """Sample random batch contexts.""" - assert self._random_sampler_mode is not None - return self.sample_contexts(self._random_sampler_mode, batch_size)[0] - - def sample_contexts(self, mode, batch_size, state=None, next_state=None, - **kwargs): - """Sample a batch of contexts. - - Args: - mode: A string representing the mode [`train`, `explore`, `eval`]. - batch_size: Batch size. - Returns: - Two lists of [batch_size, num_context_dims] contexts. - """ - contexts, next_contexts = self._sampler_fns[mode]( - batch_size, state=state, next_state=next_state, - **kwargs) - self._validate_contexts(contexts) - self._validate_contexts(next_contexts) - return contexts, next_contexts - - def compute_rewards(self, mode, states, actions, rewards, next_states, - contexts): - """Compute context-based rewards. - - Args: - mode: A string representing the mode ['uvf', 'task']. - states: A [batch_size, num_state_dims] tensor. - actions: A [batch_size, num_action_dims] tensor. - rewards: A [batch_size] tensor representing unmodified rewards. - next_states: A [batch_size, num_state_dims] tensor. - contexts: A list of [batch_size, num_context_dims] tensors. - Returns: - A [batch_size] tensor representing rewards. - """ - return self._reward_fn(states, actions, rewards, next_states, - contexts) - - def _make_reward_fn(self, reward_fns_list, reward_weights): - """Returns a fn that computes rewards. - - Args: - reward_fns_list: A fn or a list of reward fns. - mode: A string representing the operating mode. - reward_weights: A list of reward weights. - """ - if not isinstance(reward_fns_list, (list, tuple)): - reward_fns_list = [reward_fns_list] - if reward_weights is None: - reward_weights = [1.0] * len(reward_fns_list) - assert len(reward_fns_list) == len(reward_weights) - - reward_fns_list = [ - self._custom_reward_fns[fn] if isinstance(fn, (str,)) else fn - for fn in reward_fns_list - ] - - def reward_fn(*args, **kwargs): - """Returns rewards, discounts.""" - reward_tuples = [ - reward_fn(*args, **kwargs) for reward_fn in reward_fns_list - ] - rewards_list = [reward_tuple[0] for reward_tuple in reward_tuples] - discounts_list = [reward_tuple[1] for reward_tuple in reward_tuples] - ndims = max([r.shape.ndims for r in rewards_list]) - if ndims > 1: # expand reward shapes to allow broadcasting - for i in range(len(rewards_list)): - for _ in range(rewards_list[i].shape.ndims - ndims): - rewards_list[i] = tf.expand_dims(rewards_list[i], axis=-1) - for _ in range(discounts_list[i].shape.ndims - ndims): - discounts_list[i] = tf.expand_dims(discounts_list[i], axis=-1) - rewards = tf.add_n( - [r * tf.to_float(w) for r, w in zip(rewards_list, reward_weights)]) - discounts = discounts_list[0] - for d in discounts_list[1:]: - discounts *= d - - return rewards, discounts - - return reward_fn - - def _make_sampler_fn(self, sampler_cls_list, mode): - """Returns a fn that samples a list of context vars. - - Args: - sampler_cls_list: A list of sampler classes. - mode: A string representing the operating mode. - """ - if not isinstance(sampler_cls_list, (list, tuple)): - sampler_cls_list = [sampler_cls_list] - - self._samplers[mode] = [] - sampler_fns = [] - for spec, sampler in zip(self.context_specs, sampler_cls_list): - if isinstance(sampler, (str,)): - sampler_fn = self._custom_sampler_fns[sampler] - else: - sampler_fn = sampler(context_spec=spec) - self._samplers[mode].append(sampler_fn) - sampler_fns.append(sampler_fn) - - def batch_sampler_fn(batch_size, state=None, next_state=None, **kwargs): - """Sampler fn.""" - contexts_tuples = [ - sampler(batch_size, state=state, next_state=next_state, **kwargs) - for sampler in sampler_fns] - contexts = [c[0] for c in contexts_tuples] - next_contexts = [c[1] for c in contexts_tuples] - contexts = [ - normalizer.update_apply(c) if normalizer is not None else c - for normalizer, c in zip(self._normalizers, contexts) - ] - next_contexts = [ - normalizer.apply(c) if normalizer is not None else c - for normalizer, c in zip(self._normalizers, next_contexts) - ] - return contexts, next_contexts - - self._sampler_fns[mode] = batch_sampler_fn - - def set_env_context_op(self, context, disable_unnormalizer=False): - """Returns a TensorFlow op that sets the environment context. - - Args: - context: A list of context Tensor variables. - disable_unnormalizer: Disable unnormalization. - Returns: - A TensorFlow op that sets the environment context. - """ - ret_val = np.array(1.0, dtype=np.float32) - if not self._settable_context: - return tf.identity(ret_val) - - if not disable_unnormalizer: - context = [ - normalizer.unapply(tf.expand_dims(c, 0))[0] - if normalizer is not None else c - for normalizer, c in zip(self._normalizers, context) - ] - - def set_context_func(*env_context_values): - tf.logging.info('[set_env_context_op] Setting gym environment context.') - # pylint: disable=protected-access - self.gym_env.set_context(*env_context_values) - return ret_val - # pylint: enable=protected-access - - with tf.name_scope('set_env_context'): - set_op = tf.py_func(set_context_func, context, tf.float32, - name='set_env_context_py_func') - set_op.set_shape([]) - return set_op - - def set_replay(self, replay): - """Set replay buffer for samplers. - - Args: - replay: A replay buffer. - """ - for _, samplers in self._samplers.items(): - for sampler in samplers: - sampler.set_replay(replay) - - def get_clip_fns(self): - """Returns a list of clip fns for contexts. - - Returns: - A list of fns that clip context tensors. - """ - clip_fns = [] - for context_range in self.context_ranges: - def clip_fn(var_, range_=context_range): - """Clip a tensor.""" - if range_ is None: - clipped_var = tf.identity(var_) - elif isinstance(range_[0], (int, long, float, list, np.ndarray)): - clipped_var = tf.clip_by_value( - var_, - range_[0], - range_[1],) - else: raise NotImplementedError(range_) - return clipped_var - clip_fns.append(clip_fn) - return clip_fns - - def _validate_contexts(self, contexts): - """Validate if contexts have right specs. - - Args: - contexts: A list of [batch_size, num_context_dim] tensors. - Raises: - ValueError: If shape or dtype mismatches that of spec. - """ - for i, (context, spec) in enumerate(zip(contexts, self.context_specs)): - if context[0].shape != spec.shape: - raise ValueError('contexts[%d] has invalid shape %s wrt spec shape %s' % - (i, context[0].shape, spec.shape)) - if context.dtype != spec.dtype: - raise ValueError('contexts[%d] has invalid dtype %s wrt spec dtype %s' % - (i, context.dtype, spec.dtype)) - - def context_multi_transition_fn(self, contexts, **kwargs): - """Returns multiple future contexts starting from a batch.""" - assert self._context_multi_transition_fn - return self._context_multi_transition_fn(contexts, None, None, **kwargs) - - def step(self, mode, agent=None, action_fn=None, **kwargs): - """Returns [next_contexts..., next_timer] list of ops. - - Args: - mode: a string representing the mode=[train, explore, eval]. - **kwargs: kwargs for context_transition_fn. - Returns: - a list of ops that set the context. - """ - if agent is None: - ops = [] - if self._context_transition_fn is not None: - def sampler_fn(): - samples = self.sample_contexts(mode, 1)[0] - return [s[0] for s in samples] - values = self._context_transition_fn(self.vars, self.t, sampler_fn, **kwargs) - ops += [tf.assign(var, value) for var, value in zip(self.vars, values)] - ops.append(tf.assign_add(self.t, 1)) # increment timer - return ops - else: - ops = agent.tf_context.step(mode, **kwargs) - state = kwargs['state'] - next_state = kwargs['next_state'] - state_repr = kwargs['state_repr'] - next_state_repr = kwargs['next_state_repr'] - with tf.control_dependencies(ops): # Step high level context before computing low level one. - # Get the context transition function output. - values = self._context_transition_fn(self.vars, self.t, None, - state=state_repr, - next_state=next_state_repr) - # Select a new goal every C steps, otherwise use context transition. - low_level_context = [ - tf.cond(tf.equal(self.t % self.meta_action_every_n, 0), - lambda: tf.cast(action_fn(next_state, context=None), tf.float32), - lambda: values)] - ops = [tf.assign(var, value) - for var, value in zip(self.vars, low_level_context)] - with tf.control_dependencies(ops): - return [tf.assign_add(self.t, 1)] # increment timer - return ops - - def reset(self, mode, agent=None, action_fn=None, state=None): - """Returns ops that reset the context. - - Args: - mode: a string representing the mode=[train, explore, eval]. - Returns: - a list of ops that reset the context. - """ - if agent is None: - values = self.sample_contexts(mode=mode, batch_size=1)[0] - if values is None: - return [] - values = [value[0] for value in values] - values[0] = uvf_utils.tf_print( - values[0], - values, - message='context:reset, mode=%s' % mode, - first_n=10, - name='context:reset:%s' % mode) - all_ops = [] - for _, context_vars in sorted(self.context_vars.items()): - ops = [tf.assign(var, value) for var, value in zip(context_vars, values)] - all_ops += ops - all_ops.append(self.set_env_context_op(values)) - all_ops.append(tf.assign(self.t, 0)) # reset timer - return all_ops - else: - ops = agent.tf_context.reset(mode) - # NOTE: The code is currently written in such a way that the higher level - # policy does not provide a low-level context until the second - # observation. Insead, we just zero-out low-level contexts. - for key, context_vars in sorted(self.context_vars.items()): - ops += [tf.assign(var, tf.zeros_like(var)) for var, meta_var in - zip(context_vars, agent.tf_context.context_vars[key])] - - ops.append(tf.assign(self.t, 0)) # reset timer - return ops - - def create_vars(self, name, agent=None): - """Create tf variables for contexts. - - Args: - name: Name of the variables. - Returns: - A list of [num_context_dims] tensors. - """ - if agent is not None: - meta_vars = agent.create_vars(name) - else: - meta_vars = {} - assert name not in self.context_vars, ('Conflict! %s is already ' - 'initialized.') % name - self.context_vars[name] = tuple([ - tf.Variable( - tf.zeros(shape=spec.shape, dtype=spec.dtype), - name='%s_context_%d' % (name, i)) - for i, spec in enumerate(self.context_specs) - ]) - return self.context_vars[name], meta_vars - - @property - def n(self): - return len(self.context_specs) - - @property - def vars(self): - return self.context_vars[self.VAR_NAME] - - # pylint: disable=protected-access - @property - def gym_env(self): - return self._tf_env.pyenv._gym_env - - @property - def tf_env(self): - return self._tf_env - # pylint: enable=protected-access diff --git a/research/efficient-hrl/context/context_transition_functions.py b/research/efficient-hrl/context/context_transition_functions.py deleted file mode 100644 index 70326debde4..00000000000 --- a/research/efficient-hrl/context/context_transition_functions.py +++ /dev/null @@ -1,123 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Context functions. - -Given the current contexts, timer and context sampler, returns new contexts - after an environment step. This can be used to define a high-level policy - that controls contexts as its actions. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf -import gin.tf -import utils as uvf_utils - - -@gin.configurable -def periodic_context_fn(contexts, timer, sampler_fn, period=1): - """Periodically samples contexts. - - Args: - contexts: a list of [num_context_dims] tensor variables representing - current contexts. - timer: a scalar integer tensor variable holding the current time step. - sampler_fn: a sampler function that samples a list of [num_context_dims] - tensors. - period: (integer) period of update. - Returns: - a list of [num_context_dims] tensors. - """ - contexts = list(contexts[:]) # create copy - return tf.cond(tf.mod(timer, period) == 0, sampler_fn, lambda: contexts) - - -@gin.configurable -def timer_context_fn(contexts, - timer, - sampler_fn, - period=1, - timer_index=-1, - debug=False): - """Samples contexts based on timer in contexts. - - Args: - contexts: a list of [num_context_dims] tensor variables representing - current contexts. - timer: a scalar integer tensor variable holding the current time step. - sampler_fn: a sampler function that samples a list of [num_context_dims] - tensors. - period: (integer) period of update; actual period = `period` + 1. - timer_index: (integer) Index of context list that present timer. - debug: (boolean) Print debug messages. - Returns: - a list of [num_context_dims] tensors. - """ - contexts = list(contexts[:]) # create copy - cond = tf.equal(contexts[timer_index][0], 0) - def reset(): - """Sample context and reset the timer.""" - new_contexts = sampler_fn() - new_contexts[timer_index] = tf.zeros_like( - contexts[timer_index]) + period - return new_contexts - def update(): - """Decrement the timer.""" - contexts[timer_index] -= 1 - return contexts - values = tf.cond(cond, reset, update) - if debug: - values[0] = uvf_utils.tf_print( - values[0], - values + [timer], - 'timer_context_fn', - first_n=200, - name='timer_context_fn:contexts') - return values - - -@gin.configurable -def relative_context_transition_fn( - contexts, timer, sampler_fn, - k=2, state=None, next_state=None, - **kwargs): - """Contexts updated to be relative to next state. - """ - contexts = list(contexts[:]) # create copy - assert len(contexts) == 1 - new_contexts = [ - tf.concat( - [contexts[0][:k] + state[:k] - next_state[:k], - contexts[0][k:]], -1)] - return new_contexts - - -@gin.configurable -def relative_context_multi_transition_fn( - contexts, timer, sampler_fn, - k=2, states=None, - **kwargs): - """Given contexts at first state and sequence of states, derives sequence of all contexts. - """ - contexts = list(contexts[:]) # create copy - assert len(contexts) == 1 - contexts = [ - tf.concat( - [tf.expand_dims(contexts[0][:, :k] + states[:, 0, :k], 1) - states[:, :, :k], - contexts[0][:, None, k:] * tf.ones_like(states[:, :, :1])], -1)] - return contexts diff --git a/research/efficient-hrl/context/gin_imports.py b/research/efficient-hrl/context/gin_imports.py deleted file mode 100644 index 94512cef847..00000000000 --- a/research/efficient-hrl/context/gin_imports.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Import gin configurable modules. -""" - -# pylint: disable=unused-import -from context import context -from context import context_transition_functions -from context import gin_utils -from context import rewards_functions -from context import samplers -# pylint: disable=unused-import diff --git a/research/efficient-hrl/context/gin_utils.py b/research/efficient-hrl/context/gin_utils.py deleted file mode 100644 index ab7c1b2d1dd..00000000000 --- a/research/efficient-hrl/context/gin_utils.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Gin configurable utility functions. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import gin.tf - - -@gin.configurable -def gin_sparse_array(size, values, indices, fill_value=0): - arr = np.zeros(size) - arr.fill(fill_value) - arr[indices] = values - return arr - - -@gin.configurable -def gin_sum(values): - result = values[0] - for value in values[1:]: - result += value - return result - - -@gin.configurable -def gin_range(n): - return range(n) diff --git a/research/efficient-hrl/context/rewards_functions.py b/research/efficient-hrl/context/rewards_functions.py deleted file mode 100644 index ab560a7f429..00000000000 --- a/research/efficient-hrl/context/rewards_functions.py +++ /dev/null @@ -1,741 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Reward shaping functions used by Contexts. - - Each reward function should take the following inputs and return new rewards, - and discounts. - - new_rewards, discounts = reward_fn(states, actions, rewards, - next_states, contexts) -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf -import gin.tf - - -def summarize_stats(stats): - """Summarize a dictionary of variables. - - Args: - stats: a dictionary of {name: tensor} to compute stats over. - """ - for name, stat in stats.items(): - mean = tf.reduce_mean(stat) - tf.summary.scalar('mean_%s' % name, mean) - tf.summary.scalar('max_%s' % name, tf.reduce_max(stat)) - tf.summary.scalar('min_%s' % name, tf.reduce_min(stat)) - std = tf.sqrt(tf.reduce_mean(tf.square(stat)) - tf.square(mean) + 1e-10) - tf.summary.scalar('std_%s' % name, std) - tf.summary.histogram(name, stat) - - -def index_states(states, indices): - """Return indexed states. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - indices: (a list of Numpy integer array) Indices of states dimensions - to be mapped. - Returns: - A [batch_size, num_indices] Tensor representing the batch of indexed states. - """ - if indices is None: - return states - indices = tf.constant(indices, dtype=tf.int32) - return tf.gather(states, indices=indices, axis=1) - - -def record_tensor(tensor, indices, stats, name='states'): - """Record specified tensor dimensions into stats. - - Args: - tensor: A [batch_size, num_dims] Tensor. - indices: (a list of integers) Indices of dimensions to record. - stats: A dictionary holding stats. - name: (string) Name of tensor. - """ - if indices is None: - indices = range(tensor.shape.as_list()[1]) - for index in indices: - stats['%s_%02d' % (name, index)] = tensor[:, index] - - -@gin.configurable -def potential_rewards(states, - actions, - rewards, - next_states, - contexts, - gamma=1.0, - reward_fn=None): - """Return the potential-based rewards. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - gamma: Reward discount. - reward_fn: A reward function. - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del actions # unused args - gamma = tf.to_float(gamma) - rewards_tp1, discounts = reward_fn(None, None, rewards, next_states, contexts) - rewards, _ = reward_fn(None, None, rewards, states, contexts) - return -rewards + gamma * rewards_tp1, discounts - - -@gin.configurable -def timed_rewards(states, - actions, - rewards, - next_states, - contexts, - reward_fn=None, - dense=False, - timer_index=-1): - """Return the timed rewards. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - reward_fn: A reward function. - dense: (boolean) Provide dense rewards or sparse rewards at time = 0. - timer_index: (integer) The context list index that specifies timer. - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - assert contexts[timer_index].get_shape().as_list()[1] == 1 - timers = contexts[timer_index][:, 0] - rewards, discounts = reward_fn(states, actions, rewards, next_states, - contexts) - terminates = tf.to_float(timers <= 0) # if terminate set 1, else set 0 - for _ in range(rewards.shape.ndims - 1): - terminates = tf.expand_dims(terminates, axis=-1) - if not dense: - rewards *= terminates # if terminate, return rewards, else return 0 - discounts *= (tf.to_float(1.0) - terminates) - return rewards, discounts - - -@gin.configurable -def reset_rewards(states, - actions, - rewards, - next_states, - contexts, - reset_index=0, - reset_state=None, - reset_reward_function=None, - include_forward_rewards=True, - include_reset_rewards=True): - """Returns the rewards for a forward/reset agent. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - reset_index: (integer) The context list index that specifies reset. - reset_state: Reset state. - reset_reward_function: Reward function for reset step. - include_forward_rewards: Include the rewards from the forward pass. - include_reset_rewards: Include the rewards from the reset pass. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - reset_state = tf.constant( - reset_state, dtype=next_states.dtype, shape=next_states.shape) - reset_states = tf.expand_dims(reset_state, 0) - - def true_fn(): - if include_reset_rewards: - return reset_reward_function(states, actions, rewards, next_states, - [reset_states] + contexts[1:]) - else: - return tf.zeros_like(rewards), tf.ones_like(rewards) - - def false_fn(): - if include_forward_rewards: - return plain_rewards(states, actions, rewards, next_states, contexts) - else: - return tf.zeros_like(rewards), tf.ones_like(rewards) - - rewards, discounts = tf.cond( - tf.cast(contexts[reset_index][0, 0], dtype=tf.bool), true_fn, false_fn) - return rewards, discounts - - -@gin.configurable -def tanh_similarity(states, - actions, - rewards, - next_states, - contexts, - mse_scale=1.0, - state_scales=1.0, - goal_scales=1.0, - summarize=False): - """Returns the similarity between next_states and contexts using tanh and mse. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - mse_scale: A float, to scale mse before tanh. - state_scales: multiplicative scale for (next) states. A scalar or 1D tensor, - must be broadcastable to number of state dimensions. - goal_scales: multiplicative scale for contexts. A scalar or 1D tensor, - must be broadcastable to number of goal dimensions. - summarize: (boolean) enable summary ops. - - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del states, actions, rewards # Unused - mse = tf.reduce_mean(tf.squared_difference(next_states * state_scales, - contexts[0] * goal_scales), -1) - tanh = tf.tanh(mse_scale * mse) - if summarize: - with tf.name_scope('RewardFn/'): - tf.summary.scalar('mean_mse', tf.reduce_mean(mse)) - tf.summary.histogram('mse', mse) - tf.summary.scalar('mean_tanh', tf.reduce_mean(tanh)) - tf.summary.histogram('tanh', tanh) - rewards = tf.to_float(1 - tanh) - return rewards, tf.ones_like(rewards) - - -@gin.configurable -def negative_mse(states, - actions, - rewards, - next_states, - contexts, - state_scales=1.0, - goal_scales=1.0, - summarize=False): - """Returns the negative mean square error between next_states and contexts. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - state_scales: multiplicative scale for (next) states. A scalar or 1D tensor, - must be broadcastable to number of state dimensions. - goal_scales: multiplicative scale for contexts. A scalar or 1D tensor, - must be broadcastable to number of goal dimensions. - summarize: (boolean) enable summary ops. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del states, actions, rewards # Unused - mse = tf.reduce_mean(tf.squared_difference(next_states * state_scales, - contexts[0] * goal_scales), -1) - if summarize: - with tf.name_scope('RewardFn/'): - tf.summary.scalar('mean_mse', tf.reduce_mean(mse)) - tf.summary.histogram('mse', mse) - rewards = tf.to_float(-mse) - return rewards, tf.ones_like(rewards) - - -@gin.configurable -def negative_distance(states, - actions, - rewards, - next_states, - contexts, - state_scales=1.0, - goal_scales=1.0, - reward_scales=1.0, - weight_index=None, - weight_vector=None, - summarize=False, - termination_epsilon=1e-4, - state_indices=None, - goal_indices=None, - vectorize=False, - relative_context=False, - diff=False, - norm='L2', - epsilon=1e-10, - bonus_epsilon=0., #5., - offset=0.0): - """Returns the negative euclidean distance between next_states and contexts. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - state_scales: multiplicative scale for (next) states. A scalar or 1D tensor, - must be broadcastable to number of state dimensions. - goal_scales: multiplicative scale for goals. A scalar or 1D tensor, - must be broadcastable to number of goal dimensions. - reward_scales: multiplicative scale for rewards. A scalar or 1D tensor, - must be broadcastable to number of reward dimensions. - weight_index: (integer) The context list index that specifies weight. - weight_vector: (a number or a list or Numpy array) The weighting vector, - broadcastable to `next_states`. - summarize: (boolean) enable summary ops. - termination_epsilon: terminate if dist is less than this quantity. - state_indices: (a list of integers) list of state indices to select. - goal_indices: (a list of integers) list of goal indices to select. - vectorize: Return a vectorized form. - norm: L1 or L2. - epsilon: small offset to ensure non-negative/zero distance. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del actions, rewards # Unused - stats = {} - record_tensor(next_states, state_indices, stats, 'next_states') - states = index_states(states, state_indices) - next_states = index_states(next_states, state_indices) - goals = index_states(contexts[0], goal_indices) - if relative_context: - goals = states + goals - sq_dists = tf.squared_difference(next_states * state_scales, - goals * goal_scales) - old_sq_dists = tf.squared_difference(states * state_scales, - goals * goal_scales) - record_tensor(sq_dists, None, stats, 'sq_dists') - if weight_vector is not None: - sq_dists *= tf.convert_to_tensor(weight_vector, dtype=next_states.dtype) - old_sq_dists *= tf.convert_to_tensor(weight_vector, dtype=next_states.dtype) - if weight_index is not None: - #sq_dists *= contexts[weight_index] - weights = tf.abs(index_states(contexts[0], weight_index)) - #weights /= tf.reduce_sum(weights, -1, keepdims=True) - sq_dists *= weights - old_sq_dists *= weights - if norm == 'L1': - dist = tf.sqrt(sq_dists + epsilon) - old_dist = tf.sqrt(old_sq_dists + epsilon) - if not vectorize: - dist = tf.reduce_sum(dist, -1) - old_dist = tf.reduce_sum(old_dist, -1) - elif norm == 'L2': - if vectorize: - dist = sq_dists - old_dist = old_sq_dists - else: - dist = tf.reduce_sum(sq_dists, -1) - old_dist = tf.reduce_sum(old_sq_dists, -1) - dist = tf.sqrt(dist + epsilon) # tf.gradients fails when tf.sqrt(-0.0) - old_dist = tf.sqrt(old_dist + epsilon) # tf.gradients fails when tf.sqrt(-0.0) - else: - raise NotImplementedError(norm) - discounts = dist > termination_epsilon - if summarize: - with tf.name_scope('RewardFn/'): - tf.summary.scalar('mean_dist', tf.reduce_mean(dist)) - tf.summary.histogram('dist', dist) - summarize_stats(stats) - bonus = tf.to_float(dist < bonus_epsilon) - dist *= reward_scales - old_dist *= reward_scales - if diff: - return bonus + offset + tf.to_float(old_dist - dist), tf.to_float(discounts) - return bonus + offset + tf.to_float(-dist), tf.to_float(discounts) - - -@gin.configurable -def cosine_similarity(states, - actions, - rewards, - next_states, - contexts, - state_scales=1.0, - goal_scales=1.0, - reward_scales=1.0, - normalize_states=True, - normalize_goals=True, - weight_index=None, - weight_vector=None, - summarize=False, - state_indices=None, - goal_indices=None, - offset=0.0): - """Returns the cosine similarity between next_states - states and contexts. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - state_scales: multiplicative scale for (next) states. A scalar or 1D tensor, - must be broadcastable to number of state dimensions. - goal_scales: multiplicative scale for goals. A scalar or 1D tensor, - must be broadcastable to number of goal dimensions. - reward_scales: multiplicative scale for rewards. A scalar or 1D tensor, - must be broadcastable to number of reward dimensions. - weight_index: (integer) The context list index that specifies weight. - weight_vector: (a number or a list or Numpy array) The weighting vector, - broadcastable to `next_states`. - summarize: (boolean) enable summary ops. - termination_epsilon: terminate if dist is less than this quantity. - state_indices: (a list of integers) list of state indices to select. - goal_indices: (a list of integers) list of goal indices to select. - vectorize: Return a vectorized form. - norm: L1 or L2. - epsilon: small offset to ensure non-negative/zero distance. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del actions, rewards # Unused - stats = {} - record_tensor(next_states, state_indices, stats, 'next_states') - states = index_states(states, state_indices) - next_states = index_states(next_states, state_indices) - goals = index_states(contexts[0], goal_indices) - - if weight_vector is not None: - goals *= tf.convert_to_tensor(weight_vector, dtype=next_states.dtype) - if weight_index is not None: - weights = tf.abs(index_states(contexts[0], weight_index)) - goals *= weights - - direction_vec = next_states - states - if normalize_states: - direction_vec = tf.nn.l2_normalize(direction_vec, -1) - goal_vec = goals - if normalize_goals: - goal_vec = tf.nn.l2_normalize(goal_vec, -1) - - similarity = tf.reduce_sum(goal_vec * direction_vec, -1) - discounts = tf.ones_like(similarity) - return offset + tf.to_float(similarity), tf.to_float(discounts) - - -@gin.configurable -def diff_distance(states, - actions, - rewards, - next_states, - contexts, - state_scales=1.0, - goal_scales=1.0, - reward_scales=1.0, - weight_index=None, - weight_vector=None, - summarize=False, - termination_epsilon=1e-4, - state_indices=None, - goal_indices=None, - norm='L2', - epsilon=1e-10): - """Returns the difference in euclidean distance between states/next_states and contexts. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - state_scales: multiplicative scale for (next) states. A scalar or 1D tensor, - must be broadcastable to number of state dimensions. - goal_scales: multiplicative scale for goals. A scalar or 1D tensor, - must be broadcastable to number of goal dimensions. - reward_scales: multiplicative scale for rewards. A scalar or 1D tensor, - must be broadcastable to number of reward dimensions. - weight_index: (integer) The context list index that specifies weight. - weight_vector: (a number or a list or Numpy array) The weighting vector, - broadcastable to `next_states`. - summarize: (boolean) enable summary ops. - termination_epsilon: terminate if dist is less than this quantity. - state_indices: (a list of integers) list of state indices to select. - goal_indices: (a list of integers) list of goal indices to select. - vectorize: Return a vectorized form. - norm: L1 or L2. - epsilon: small offset to ensure non-negative/zero distance. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del actions, rewards # Unused - stats = {} - record_tensor(next_states, state_indices, stats, 'next_states') - next_states = index_states(next_states, state_indices) - states = index_states(states, state_indices) - goals = index_states(contexts[0], goal_indices) - next_sq_dists = tf.squared_difference(next_states * state_scales, - goals * goal_scales) - sq_dists = tf.squared_difference(states * state_scales, - goals * goal_scales) - record_tensor(sq_dists, None, stats, 'sq_dists') - if weight_vector is not None: - next_sq_dists *= tf.convert_to_tensor(weight_vector, dtype=next_states.dtype) - sq_dists *= tf.convert_to_tensor(weight_vector, dtype=next_states.dtype) - if weight_index is not None: - next_sq_dists *= contexts[weight_index] - sq_dists *= contexts[weight_index] - if norm == 'L1': - next_dist = tf.sqrt(next_sq_dists + epsilon) - dist = tf.sqrt(sq_dists + epsilon) - next_dist = tf.reduce_sum(next_dist, -1) - dist = tf.reduce_sum(dist, -1) - elif norm == 'L2': - next_dist = tf.reduce_sum(next_sq_dists, -1) - next_dist = tf.sqrt(next_dist + epsilon) # tf.gradients fails when tf.sqrt(-0.0) - dist = tf.reduce_sum(sq_dists, -1) - dist = tf.sqrt(dist + epsilon) # tf.gradients fails when tf.sqrt(-0.0) - else: - raise NotImplementedError(norm) - discounts = next_dist > termination_epsilon - if summarize: - with tf.name_scope('RewardFn/'): - tf.summary.scalar('mean_dist', tf.reduce_mean(dist)) - tf.summary.histogram('dist', dist) - summarize_stats(stats) - diff = dist - next_dist - diff *= reward_scales - return tf.to_float(diff), tf.to_float(discounts) - - -@gin.configurable -def binary_indicator(states, - actions, - rewards, - next_states, - contexts, - termination_epsilon=1e-4, - offset=0, - epsilon=1e-10, - state_indices=None, - summarize=False): - """Returns 0/1 by checking if next_states and contexts overlap. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - termination_epsilon: terminate if dist is less than this quantity. - offset: Offset the rewards. - epsilon: small offset to ensure non-negative/zero distance. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del states, actions # unused args - next_states = index_states(next_states, state_indices) - dist = tf.reduce_sum(tf.squared_difference(next_states, contexts[0]), -1) - dist = tf.sqrt(dist + epsilon) - discounts = dist > termination_epsilon - rewards = tf.logical_not(discounts) - rewards = tf.to_float(rewards) + offset - return tf.to_float(rewards), tf.ones_like(tf.to_float(discounts)) #tf.to_float(discounts) - - -@gin.configurable -def plain_rewards(states, actions, rewards, next_states, contexts): - """Returns the given rewards. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del states, actions, next_states, contexts # Unused - return rewards, tf.ones_like(rewards) - - -@gin.configurable -def ctrl_rewards(states, - actions, - rewards, - next_states, - contexts, - reward_scales=1.0): - """Returns the negative control cost. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - reward_scales: multiplicative scale for rewards. A scalar or 1D tensor, - must be broadcastable to number of reward dimensions. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del states, rewards, contexts # Unused - if actions is None: - rewards = tf.to_float(tf.zeros(shape=next_states.shape[:1])) - else: - rewards = -tf.reduce_sum(tf.square(actions), axis=1) - rewards *= reward_scales - rewards = tf.to_float(rewards) - return rewards, tf.ones_like(rewards) - - -@gin.configurable -def diff_rewards( - states, - actions, - rewards, - next_states, - contexts, - state_indices=None, - goal_index=0,): - """Returns (next_states - goals) as a batched vector reward.""" - del states, rewards, actions # Unused - if state_indices is not None: - next_states = index_states(next_states, state_indices) - rewards = tf.to_float(next_states - contexts[goal_index]) - return rewards, tf.ones_like(rewards) - - -@gin.configurable -def state_rewards(states, - actions, - rewards, - next_states, - contexts, - weight_index=None, - state_indices=None, - weight_vector=1.0, - offset_vector=0.0, - summarize=False): - """Returns the rewards that are linear mapping of next_states. - - Args: - states: A [batch_size, num_state_dims] Tensor representing a batch - of states. - actions: A [batch_size, num_action_dims] Tensor representing a batch - of actions. - rewards: A [batch_size] Tensor representing a batch of rewards. - next_states: A [batch_size, num_state_dims] Tensor representing a batch - of next states. - contexts: A list of [batch_size, num_context_dims] Tensor representing - a batch of contexts. - weight_index: (integer) Index of contexts lists that specify weighting. - state_indices: (a list of Numpy integer array) Indices of states dimensions - to be mapped. - weight_vector: (a number or a list or Numpy array) The weighting vector, - broadcastable to `next_states`. - offset_vector: (a number or a list of Numpy array) The off vector. - summarize: (boolean) enable summary ops. - - Returns: - A new tf.float32 [batch_size] rewards Tensor, and - tf.float32 [batch_size] discounts tensor. - """ - del states, actions, rewards # unused args - stats = {} - record_tensor(next_states, state_indices, stats) - next_states = index_states(next_states, state_indices) - weight = tf.constant( - weight_vector, dtype=next_states.dtype, shape=next_states[0].shape) - weights = tf.expand_dims(weight, 0) - offset = tf.constant( - offset_vector, dtype=next_states.dtype, shape=next_states[0].shape) - offsets = tf.expand_dims(offset, 0) - if weight_index is not None: - weights *= contexts[weight_index] - rewards = tf.to_float(tf.reduce_sum(weights * (next_states+offsets), axis=1)) - if summarize: - with tf.name_scope('RewardFn/'): - summarize_stats(stats) - return rewards, tf.ones_like(rewards) diff --git a/research/efficient-hrl/context/samplers.py b/research/efficient-hrl/context/samplers.py deleted file mode 100644 index 15a22df5eb3..00000000000 --- a/research/efficient-hrl/context/samplers.py +++ /dev/null @@ -1,445 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Samplers for Contexts. - - Each sampler class should define __call__(batch_size). -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow as tf -slim = tf.contrib.slim -import gin.tf - - -@gin.configurable -class BaseSampler(object): - """Base sampler.""" - - def __init__(self, context_spec, context_range=None, k=2, scope='sampler'): - """Construct a base sampler. - - Args: - context_spec: A context spec. - context_range: A tuple of (minval, max), where minval, maxval are floats - or Numpy arrays with the same shape as the context. - scope: A string denoting scope. - """ - self._context_spec = context_spec - self._context_range = context_range - self._k = k - self._scope = scope - - def __call__(self, batch_size, **kwargs): - raise NotImplementedError - - def set_replay(self, replay=None): - pass - - def _validate_contexts(self, contexts): - """Validate if contexts have right spec. - - Args: - contexts: A [batch_size, num_contexts_dim] tensor. - Raises: - ValueError: If shape or dtype mismatches that of spec. - """ - if contexts[0].shape != self._context_spec.shape: - raise ValueError('contexts has invalid shape %s wrt spec shape %s' % - (contexts[0].shape, self._context_spec.shape)) - if contexts.dtype != self._context_spec.dtype: - raise ValueError('contexts has invalid dtype %s wrt spec dtype %s' % - (contexts.dtype, self._context_spec.dtype)) - - -@gin.configurable -class ZeroSampler(BaseSampler): - """Zero sampler.""" - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context. - - Args: - batch_size: Batch size. - Returns: - Two [batch_size, num_context_dims] tensors. - """ - contexts = tf.zeros( - dtype=self._context_spec.dtype, - shape=[ - batch_size, - ] + self._context_spec.shape.as_list()) - return contexts, contexts - - -@gin.configurable -class BinarySampler(BaseSampler): - """Binary sampler.""" - - def __init__(self, probs=0.5, *args, **kwargs): - """Constructor.""" - super(BinarySampler, self).__init__(*args, **kwargs) - self._probs = probs - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context.""" - spec = self._context_spec - contexts = tf.random_uniform( - shape=[ - batch_size, - ] + spec.shape.as_list(), dtype=tf.float32) - contexts = tf.cast(tf.greater(contexts, self._probs), dtype=spec.dtype) - return contexts, contexts - - -@gin.configurable -class RandomSampler(BaseSampler): - """Random sampler.""" - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context. - - Args: - batch_size: Batch size. - Returns: - Two [batch_size, num_context_dims] tensors. - """ - spec = self._context_spec - context_range = self._context_range - if isinstance(context_range[0], (int, float)): - contexts = tf.random_uniform( - shape=[ - batch_size, - ] + spec.shape.as_list(), - minval=context_range[0], - maxval=context_range[1], - dtype=spec.dtype) - elif isinstance(context_range[0], (list, tuple, np.ndarray)): - assert len(spec.shape.as_list()) == 1 - assert spec.shape.as_list()[0] == len(context_range[0]) - assert spec.shape.as_list()[0] == len(context_range[1]) - contexts = tf.concat( - [ - tf.random_uniform( - shape=[ - batch_size, 1, - ] + spec.shape.as_list()[1:], - minval=context_range[0][i], - maxval=context_range[1][i], - dtype=spec.dtype) for i in range(spec.shape.as_list()[0]) - ], - axis=1) - else: raise NotImplementedError(context_range) - self._validate_contexts(contexts) - state, next_state = kwargs['state'], kwargs['next_state'] - if state is not None and next_state is not None: - pass - #contexts = tf.concat( - # [tf.random_normal(tf.shape(state[:, :self._k]), dtype=tf.float64) + - # tf.random_shuffle(state[:, :self._k]), - # contexts[:, self._k:]], 1) - - return contexts, contexts - - -@gin.configurable -class ScheduledSampler(BaseSampler): - """Scheduled sampler.""" - - def __init__(self, - scope='default', - values=None, - scheduler='cycle', - scheduler_params=None, - *args, **kwargs): - """Construct sampler. - - Args: - scope: Scope name. - values: A list of numbers or [num_context_dim] Numpy arrays - representing the values to cycle. - scheduler: scheduler type. - scheduler_params: scheduler parameters. - *args: arguments. - **kwargs: keyword arguments. - """ - super(ScheduledSampler, self).__init__(*args, **kwargs) - self._scope = scope - self._values = values - self._scheduler = scheduler - self._scheduler_params = scheduler_params or {} - assert self._values is not None and len( - self._values), 'must provide non-empty values.' - self._n = len(self._values) - # TODO(shanegu): move variable creation outside. resolve tf.cond problem. - self._count = 0 - self._i = tf.Variable( - tf.zeros(shape=(), dtype=tf.int32), - name='%s-scheduled_sampler_%d' % (self._scope, self._count)) - self._values = tf.constant(self._values, dtype=self._context_spec.dtype) - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context. - - Args: - batch_size: Batch size. - Returns: - Two [batch_size, num_context_dims] tensors. - """ - spec = self._context_spec - next_op = self._next(self._i) - with tf.control_dependencies([next_op]): - value = self._values[self._i] - if value.get_shape().as_list(): - values = tf.tile( - tf.expand_dims(value, 0), (batch_size,) + (1,) * spec.shape.ndims) - else: - values = value + tf.zeros( - shape=[ - batch_size, - ] + spec.shape.as_list(), dtype=spec.dtype) - self._validate_contexts(values) - self._count += 1 - return values, values - - def _next(self, i): - """Return op that increments pointer to next value. - - Args: - i: A tensorflow integer variable. - Returns: - Op that increments pointer. - """ - if self._scheduler == 'cycle': - inc = ('inc' in self._scheduler_params and - self._scheduler_params['inc']) or 1 - return tf.assign(i, tf.mod(i+inc, self._n)) - else: - raise NotImplementedError(self._scheduler) - - -@gin.configurable -class ReplaySampler(BaseSampler): - """Replay sampler.""" - - def __init__(self, - prefetch_queue_capacity=2, - override_indices=None, - state_indices=None, - *args, - **kwargs): - """Construct sampler. - - Args: - prefetch_queue_capacity: Capacity for prefetch queue. - override_indices: Override indices. - state_indices: Select certain indices from state dimension. - *args: arguments. - **kwargs: keyword arguments. - """ - super(ReplaySampler, self).__init__(*args, **kwargs) - self._prefetch_queue_capacity = prefetch_queue_capacity - self._override_indices = override_indices - self._state_indices = state_indices - - def set_replay(self, replay): - """Set replay. - - Args: - replay: A replay buffer. - """ - self._replay = replay - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context. - - Args: - batch_size: Batch size. - Returns: - Two [batch_size, num_context_dims] tensors. - """ - batch = self._replay.GetRandomBatch(batch_size) - next_states = batch[4] - if self._prefetch_queue_capacity > 0: - batch_queue = slim.prefetch_queue.prefetch_queue( - [next_states], - capacity=self._prefetch_queue_capacity, - name='%s/batch_context_queue' % self._scope) - next_states = batch_queue.dequeue() - if self._override_indices is not None: - assert self._context_range is not None and isinstance( - self._context_range[0], (int, long, float)) - next_states = tf.concat( - [ - tf.random_uniform( - shape=next_states[:, :1].shape, - minval=self._context_range[0], - maxval=self._context_range[1], - dtype=next_states.dtype) - if i in self._override_indices else next_states[:, i:i + 1] - for i in range(self._context_spec.shape.as_list()[0]) - ], - axis=1) - if self._state_indices is not None: - next_states = tf.concat( - [ - next_states[:, i:i + 1] - for i in range(self._context_spec.shape.as_list()[0]) - ], - axis=1) - self._validate_contexts(next_states) - return next_states, next_states - - -@gin.configurable -class TimeSampler(BaseSampler): - """Time Sampler.""" - - def __init__(self, minval=0, maxval=1, timestep=-1, *args, **kwargs): - """Construct sampler. - - Args: - minval: Min value integer. - maxval: Max value integer. - timestep: Time step between states and next_states. - *args: arguments. - **kwargs: keyword arguments. - """ - super(TimeSampler, self).__init__(*args, **kwargs) - assert self._context_spec.shape.as_list() == [1] - self._minval = minval - self._maxval = maxval - self._timestep = timestep - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context. - - Args: - batch_size: Batch size. - Returns: - Two [batch_size, num_context_dims] tensors. - """ - if self._maxval == self._minval: - contexts = tf.constant( - self._maxval, shape=[batch_size, 1], dtype=tf.int32) - else: - contexts = tf.random_uniform( - shape=[batch_size, 1], - dtype=tf.int32, - maxval=self._maxval, - minval=self._minval) - next_contexts = tf.maximum(contexts + self._timestep, 0) - - return tf.cast( - contexts, dtype=self._context_spec.dtype), tf.cast( - next_contexts, dtype=self._context_spec.dtype) - - -@gin.configurable -class ConstantSampler(BaseSampler): - """Constant sampler.""" - - def __init__(self, value=None, *args, **kwargs): - """Construct sampler. - - Args: - value: A list or Numpy array for values of the constant. - *args: arguments. - **kwargs: keyword arguments. - """ - super(ConstantSampler, self).__init__(*args, **kwargs) - self._value = value - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context. - - Args: - batch_size: Batch size. - Returns: - Two [batch_size, num_context_dims] tensors. - """ - spec = self._context_spec - value_ = tf.constant(self._value, shape=spec.shape, dtype=spec.dtype) - values = tf.tile( - tf.expand_dims(value_, 0), (batch_size,) + (1,) * spec.shape.ndims) - self._validate_contexts(values) - return values, values - - -@gin.configurable -class DirectionSampler(RandomSampler): - """Direction sampler.""" - - def __call__(self, batch_size, **kwargs): - """Sample a batch of context. - - Args: - batch_size: Batch size. - Returns: - Two [batch_size, num_context_dims] tensors. - """ - spec = self._context_spec - context_range = self._context_range - if isinstance(context_range[0], (int, float)): - contexts = tf.random_uniform( - shape=[ - batch_size, - ] + spec.shape.as_list(), - minval=context_range[0], - maxval=context_range[1], - dtype=spec.dtype) - elif isinstance(context_range[0], (list, tuple, np.ndarray)): - assert len(spec.shape.as_list()) == 1 - assert spec.shape.as_list()[0] == len(context_range[0]) - assert spec.shape.as_list()[0] == len(context_range[1]) - contexts = tf.concat( - [ - tf.random_uniform( - shape=[ - batch_size, 1, - ] + spec.shape.as_list()[1:], - minval=context_range[0][i], - maxval=context_range[1][i], - dtype=spec.dtype) for i in range(spec.shape.as_list()[0]) - ], - axis=1) - else: raise NotImplementedError(context_range) - self._validate_contexts(contexts) - if 'sampler_fn' in kwargs: - other_contexts = kwargs['sampler_fn']() - else: - other_contexts = contexts - state, next_state = kwargs['state'], kwargs['next_state'] - if state is not None and next_state is not None: - my_context_range = (np.array(context_range[1]) - np.array(context_range[0])) / 2 * np.ones(spec.shape.as_list()) - contexts = tf.concat( - [0.1 * my_context_range[:self._k] * - tf.random_normal(tf.shape(state[:, :self._k]), dtype=state.dtype) + - tf.random_shuffle(state[:, :self._k]) - state[:, :self._k], - other_contexts[:, self._k:]], 1) - #contexts = tf.Print(contexts, - # [contexts, tf.reduce_max(contexts, 0), - # tf.reduce_min(state, 0), tf.reduce_max(state, 0)], 'contexts', summarize=15) - next_contexts = tf.concat( #LALA - [state[:, :self._k] + contexts[:, :self._k] - next_state[:, :self._k], - other_contexts[:, self._k:]], 1) - next_contexts = contexts #LALA cosine - else: - next_contexts = contexts - return tf.stop_gradient(contexts), tf.stop_gradient(next_contexts) diff --git a/research/efficient-hrl/environments/__init__.py b/research/efficient-hrl/environments/__init__.py deleted file mode 100644 index 8b137891791..00000000000 --- a/research/efficient-hrl/environments/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/research/efficient-hrl/environments/ant.py b/research/efficient-hrl/environments/ant.py deleted file mode 100644 index feab1eef4c5..00000000000 --- a/research/efficient-hrl/environments/ant.py +++ /dev/null @@ -1,141 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Wrapper for creating the ant environment in gym_mujoco.""" - -import math -import numpy as np -import mujoco_py -from gym import utils -from gym.envs.mujoco import mujoco_env - - -def q_inv(a): - return [a[0], -a[1], -a[2], -a[3]] - - -def q_mult(a, b): # multiply two quaternion - w = a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3] - i = a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2] - j = a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1] - k = a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0] - return [w, i, j, k] - - -class AntEnv(mujoco_env.MujocoEnv, utils.EzPickle): - FILE = "ant.xml" - ORI_IND = 3 - - def __init__(self, file_path=None, expose_all_qpos=True, - expose_body_coms=None, expose_body_comvels=None): - self._expose_all_qpos = expose_all_qpos - self._expose_body_coms = expose_body_coms - self._expose_body_comvels = expose_body_comvels - self._body_com_indices = {} - self._body_comvel_indices = {} - - mujoco_env.MujocoEnv.__init__(self, file_path, 5) - utils.EzPickle.__init__(self) - - @property - def physics(self): - # check mujoco version is greater than version 1.50 to call correct physics - # model containing PyMjData object for getting and setting position/velocity - # check https://github.com/openai/mujoco-py/issues/80 for updates to api - if mujoco_py.get_version() >= '1.50': - return self.sim - else: - return self.model - - def _step(self, a): - return self.step(a) - - def step(self, a): - xposbefore = self.get_body_com("torso")[0] - self.do_simulation(a, self.frame_skip) - xposafter = self.get_body_com("torso")[0] - forward_reward = (xposafter - xposbefore) / self.dt - ctrl_cost = .5 * np.square(a).sum() - survive_reward = 1.0 - reward = forward_reward - ctrl_cost + survive_reward - state = self.state_vector() - done = False - ob = self._get_obs() - return ob, reward, done, dict( - reward_forward=forward_reward, - reward_ctrl=-ctrl_cost, - reward_survive=survive_reward) - - def _get_obs(self): - # No cfrc observation - if self._expose_all_qpos: - obs = np.concatenate([ - self.physics.data.qpos.flat[:15], # Ensures only ant obs. - self.physics.data.qvel.flat[:14], - ]) - else: - obs = np.concatenate([ - self.physics.data.qpos.flat[2:15], - self.physics.data.qvel.flat[:14], - ]) - - if self._expose_body_coms is not None: - for name in self._expose_body_coms: - com = self.get_body_com(name) - if name not in self._body_com_indices: - indices = range(len(obs), len(obs) + len(com)) - self._body_com_indices[name] = indices - obs = np.concatenate([obs, com]) - - if self._expose_body_comvels is not None: - for name in self._expose_body_comvels: - comvel = self.get_body_comvel(name) - if name not in self._body_comvel_indices: - indices = range(len(obs), len(obs) + len(comvel)) - self._body_comvel_indices[name] = indices - obs = np.concatenate([obs, comvel]) - return obs - - def reset_model(self): - qpos = self.init_qpos + self.np_random.uniform( - size=self.model.nq, low=-.1, high=.1) - qvel = self.init_qvel + self.np_random.randn(self.model.nv) * .1 - - # Set everything other than ant to original position and 0 velocity. - qpos[15:] = self.init_qpos[15:] - qvel[14:] = 0. - self.set_state(qpos, qvel) - return self._get_obs() - - def viewer_setup(self): - self.viewer.cam.distance = self.model.stat.extent * 0.5 - - def get_ori(self): - ori = [0, 1, 0, 0] - rot = self.physics.data.qpos[self.__class__.ORI_IND:self.__class__.ORI_IND + 4] # take the quaternion - ori = q_mult(q_mult(rot, ori), q_inv(rot))[1:3] # project onto x-y plane - ori = math.atan2(ori[1], ori[0]) - return ori - - def set_xy(self, xy): - qpos = np.copy(self.physics.data.qpos) - qpos[0] = xy[0] - qpos[1] = xy[1] - - qvel = self.physics.data.qvel - self.set_state(qpos, qvel) - - def get_xy(self): - return self.physics.data.qpos[:2] diff --git a/research/efficient-hrl/environments/ant_maze_env.py b/research/efficient-hrl/environments/ant_maze_env.py deleted file mode 100644 index 69a10663f4d..00000000000 --- a/research/efficient-hrl/environments/ant_maze_env.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -from environments.maze_env import MazeEnv -from environments.ant import AntEnv - - -class AntMazeEnv(MazeEnv): - MODEL_CLASS = AntEnv diff --git a/research/efficient-hrl/environments/assets/ant.xml b/research/efficient-hrl/environments/assets/ant.xml deleted file mode 100755 index 5a49d7f52a0..00000000000 --- a/research/efficient-hrl/environments/assets/ant.xml +++ /dev/null @@ -1,81 +0,0 @@ - - - diff --git a/research/efficient-hrl/environments/create_maze_env.py b/research/efficient-hrl/environments/create_maze_env.py deleted file mode 100644 index f6dc4f42190..00000000000 --- a/research/efficient-hrl/environments/create_maze_env.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -from environments.ant_maze_env import AntMazeEnv -from environments.point_maze_env import PointMazeEnv - -import tensorflow as tf -import gin.tf -from tf_agents.environments import gym_wrapper -from tf_agents.environments import tf_py_environment - - -@gin.configurable -def create_maze_env(env_name=None, top_down_view=False): - n_bins = 0 - manual_collision = False - if env_name.startswith('Ego'): - n_bins = 8 - env_name = env_name[3:] - if env_name.startswith('Ant'): - cls = AntMazeEnv - env_name = env_name[3:] - maze_size_scaling = 8 - elif env_name.startswith('Point'): - cls = PointMazeEnv - manual_collision = True - env_name = env_name[5:] - maze_size_scaling = 4 - else: - assert False, 'unknown env %s' % env_name - - maze_id = None - observe_blocks = False - put_spin_near_agent = False - if env_name == 'Maze': - maze_id = 'Maze' - elif env_name == 'Push': - maze_id = 'Push' - elif env_name == 'Fall': - maze_id = 'Fall' - elif env_name == 'Block': - maze_id = 'Block' - put_spin_near_agent = True - observe_blocks = True - elif env_name == 'BlockMaze': - maze_id = 'BlockMaze' - put_spin_near_agent = True - observe_blocks = True - else: - raise ValueError('Unknown maze environment %s' % env_name) - - gym_mujoco_kwargs = { - 'maze_id': maze_id, - 'n_bins': n_bins, - 'observe_blocks': observe_blocks, - 'put_spin_near_agent': put_spin_near_agent, - 'top_down_view': top_down_view, - 'manual_collision': manual_collision, - 'maze_size_scaling': maze_size_scaling - } - gym_env = cls(**gym_mujoco_kwargs) - gym_env.reset() - wrapped_env = gym_wrapper.GymWrapper(gym_env) - return wrapped_env - - -class TFPyEnvironment(tf_py_environment.TFPyEnvironment): - - def __init__(self, *args, **kwargs): - super(TFPyEnvironment, self).__init__(*args, **kwargs) - - def start_collect(self): - pass - - def current_obs(self): - time_step = self.current_time_step() - return time_step.observation[0] # For some reason, there is an extra dim. - - def step(self, actions): - actions = tf.expand_dims(actions, 0) - next_step = super(TFPyEnvironment, self).step(actions) - return next_step.is_last()[0], next_step.reward[0], next_step.discount[0] - - def reset(self): - return super(TFPyEnvironment, self).reset() diff --git a/research/efficient-hrl/environments/maze_env.py b/research/efficient-hrl/environments/maze_env.py deleted file mode 100644 index cf7d1f2dc0a..00000000000 --- a/research/efficient-hrl/environments/maze_env.py +++ /dev/null @@ -1,499 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Adapted from rllab maze_env.py.""" - -import os -import tempfile -import xml.etree.ElementTree as ET -import math -import numpy as np -import gym - -from environments import maze_env_utils - -# Directory that contains mujoco xml files. -MODEL_DIR = 'environments/assets' - - -class MazeEnv(gym.Env): - MODEL_CLASS = None - - MAZE_HEIGHT = None - MAZE_SIZE_SCALING = None - - def __init__( - self, - maze_id=None, - maze_height=0.5, - maze_size_scaling=8, - n_bins=0, - sensor_range=3., - sensor_span=2 * math.pi, - observe_blocks=False, - put_spin_near_agent=False, - top_down_view=False, - manual_collision=False, - *args, - **kwargs): - self._maze_id = maze_id - - model_cls = self.__class__.MODEL_CLASS - if model_cls is None: - raise "MODEL_CLASS unspecified!" - xml_path = os.path.join(MODEL_DIR, model_cls.FILE) - tree = ET.parse(xml_path) - worldbody = tree.find(".//worldbody") - - self.MAZE_HEIGHT = height = maze_height - self.MAZE_SIZE_SCALING = size_scaling = maze_size_scaling - self._n_bins = n_bins - self._sensor_range = sensor_range * size_scaling - self._sensor_span = sensor_span - self._observe_blocks = observe_blocks - self._put_spin_near_agent = put_spin_near_agent - self._top_down_view = top_down_view - self._manual_collision = manual_collision - - self.MAZE_STRUCTURE = structure = maze_env_utils.construct_maze(maze_id=self._maze_id) - self.elevated = any(-1 in row for row in structure) # Elevate the maze to allow for falling. - self.blocks = any( - any(maze_env_utils.can_move(r) for r in row) - for row in structure) # Are there any movable blocks? - - torso_x, torso_y = self._find_robot() - self._init_torso_x = torso_x - self._init_torso_y = torso_y - self._init_positions = [ - (x - torso_x, y - torso_y) - for x, y in self._find_all_robots()] - - self._xy_to_rowcol = lambda x, y: (2 + (y + size_scaling / 2) / size_scaling, - 2 + (x + size_scaling / 2) / size_scaling) - self._view = np.zeros([5, 5, 3]) # walls (immovable), chasms (fall), movable blocks - - height_offset = 0. - if self.elevated: - # Increase initial z-pos of ant. - height_offset = height * size_scaling - torso = tree.find(".//body[@name='torso']") - torso.set('pos', '0 0 %.2f' % (0.75 + height_offset)) - if self.blocks: - # If there are movable blocks, change simulation settings to perform - # better contact detection. - default = tree.find(".//default") - default.find('.//geom').set('solimp', '.995 .995 .01') - - self.movable_blocks = [] - for i in range(len(structure)): - for j in range(len(structure[0])): - struct = structure[i][j] - if struct == 'r' and self._put_spin_near_agent: - struct = maze_env_utils.Move.SpinXY - if self.elevated and struct not in [-1]: - # Create elevated platform. - ET.SubElement( - worldbody, "geom", - name="elevated_%d_%d" % (i, j), - pos="%f %f %f" % (j * size_scaling - torso_x, - i * size_scaling - torso_y, - height / 2 * size_scaling), - size="%f %f %f" % (0.5 * size_scaling, - 0.5 * size_scaling, - height / 2 * size_scaling), - type="box", - material="", - contype="1", - conaffinity="1", - rgba="0.9 0.9 0.9 1", - ) - if struct == 1: # Unmovable block. - # Offset all coordinates so that robot starts at the origin. - ET.SubElement( - worldbody, "geom", - name="block_%d_%d" % (i, j), - pos="%f %f %f" % (j * size_scaling - torso_x, - i * size_scaling - torso_y, - height_offset + - height / 2 * size_scaling), - size="%f %f %f" % (0.5 * size_scaling, - 0.5 * size_scaling, - height / 2 * size_scaling), - type="box", - material="", - contype="1", - conaffinity="1", - rgba="0.4 0.4 0.4 1", - ) - elif maze_env_utils.can_move(struct): # Movable block. - # The "falling" blocks are shrunk slightly and increased in mass to - # ensure that it can fall easily through a gap in the platform blocks. - name = "movable_%d_%d" % (i, j) - self.movable_blocks.append((name, struct)) - falling = maze_env_utils.can_move_z(struct) - spinning = maze_env_utils.can_spin(struct) - x_offset = 0.25 * size_scaling if spinning else 0.0 - y_offset = 0.0 - shrink = 0.1 if spinning else 0.99 if falling else 1.0 - height_shrink = 0.1 if spinning else 1.0 - movable_body = ET.SubElement( - worldbody, "body", - name=name, - pos="%f %f %f" % (j * size_scaling - torso_x + x_offset, - i * size_scaling - torso_y + y_offset, - height_offset + - height / 2 * size_scaling * height_shrink), - ) - ET.SubElement( - movable_body, "geom", - name="block_%d_%d" % (i, j), - pos="0 0 0", - size="%f %f %f" % (0.5 * size_scaling * shrink, - 0.5 * size_scaling * shrink, - height / 2 * size_scaling * height_shrink), - type="box", - material="", - mass="0.001" if falling else "0.0002", - contype="1", - conaffinity="1", - rgba="0.9 0.1 0.1 1" - ) - if maze_env_utils.can_move_x(struct): - ET.SubElement( - movable_body, "joint", - armature="0", - axis="1 0 0", - damping="0.0", - limited="true" if falling else "false", - range="%f %f" % (-size_scaling, size_scaling), - margin="0.01", - name="movable_x_%d_%d" % (i, j), - pos="0 0 0", - type="slide" - ) - if maze_env_utils.can_move_y(struct): - ET.SubElement( - movable_body, "joint", - armature="0", - axis="0 1 0", - damping="0.0", - limited="true" if falling else "false", - range="%f %f" % (-size_scaling, size_scaling), - margin="0.01", - name="movable_y_%d_%d" % (i, j), - pos="0 0 0", - type="slide" - ) - if maze_env_utils.can_move_z(struct): - ET.SubElement( - movable_body, "joint", - armature="0", - axis="0 0 1", - damping="0.0", - limited="true", - range="%f 0" % (-height_offset), - margin="0.01", - name="movable_z_%d_%d" % (i, j), - pos="0 0 0", - type="slide" - ) - if maze_env_utils.can_spin(struct): - ET.SubElement( - movable_body, "joint", - armature="0", - axis="0 0 1", - damping="0.0", - limited="false", - name="spinable_%d_%d" % (i, j), - pos="0 0 0", - type="ball" - ) - - torso = tree.find(".//body[@name='torso']") - geoms = torso.findall(".//geom") - for geom in geoms: - if 'name' not in geom.attrib: - raise Exception("Every geom of the torso must have a name " - "defined") - - _, file_path = tempfile.mkstemp(text=True, suffix='.xml') - tree.write(file_path) - - self.wrapped_env = model_cls(*args, file_path=file_path, **kwargs) - - def get_ori(self): - return self.wrapped_env.get_ori() - - def get_top_down_view(self): - self._view = np.zeros_like(self._view) - - def valid(row, col): - return self._view.shape[0] > row >= 0 and self._view.shape[1] > col >= 0 - - def update_view(x, y, d, row=None, col=None): - if row is None or col is None: - x = x - self._robot_x - y = y - self._robot_y - th = self._robot_ori - - row, col = self._xy_to_rowcol(x, y) - update_view(x, y, d, row=row, col=col) - return - - row, row_frac, col, col_frac = int(row), row % 1, int(col), col % 1 - if row_frac < 0: - row_frac += 1 - if col_frac < 0: - col_frac += 1 - - if valid(row, col): - self._view[row, col, d] += ( - (min(1., row_frac + 0.5) - max(0., row_frac - 0.5)) * - (min(1., col_frac + 0.5) - max(0., col_frac - 0.5))) - if valid(row - 1, col): - self._view[row - 1, col, d] += ( - (max(0., 0.5 - row_frac)) * - (min(1., col_frac + 0.5) - max(0., col_frac - 0.5))) - if valid(row + 1, col): - self._view[row + 1, col, d] += ( - (max(0., row_frac - 0.5)) * - (min(1., col_frac + 0.5) - max(0., col_frac - 0.5))) - if valid(row, col - 1): - self._view[row, col - 1, d] += ( - (min(1., row_frac + 0.5) - max(0., row_frac - 0.5)) * - (max(0., 0.5 - col_frac))) - if valid(row, col + 1): - self._view[row, col + 1, d] += ( - (min(1., row_frac + 0.5) - max(0., row_frac - 0.5)) * - (max(0., col_frac - 0.5))) - if valid(row - 1, col - 1): - self._view[row - 1, col - 1, d] += ( - (max(0., 0.5 - row_frac)) * max(0., 0.5 - col_frac)) - if valid(row - 1, col + 1): - self._view[row - 1, col + 1, d] += ( - (max(0., 0.5 - row_frac)) * max(0., col_frac - 0.5)) - if valid(row + 1, col + 1): - self._view[row + 1, col + 1, d] += ( - (max(0., row_frac - 0.5)) * max(0., col_frac - 0.5)) - if valid(row + 1, col - 1): - self._view[row + 1, col - 1, d] += ( - (max(0., row_frac - 0.5)) * max(0., 0.5 - col_frac)) - - # Draw ant. - robot_x, robot_y = self.wrapped_env.get_body_com("torso")[:2] - self._robot_x = robot_x - self._robot_y = robot_y - self._robot_ori = self.get_ori() - - structure = self.MAZE_STRUCTURE - size_scaling = self.MAZE_SIZE_SCALING - height = self.MAZE_HEIGHT - - # Draw immovable blocks and chasms. - for i in range(len(structure)): - for j in range(len(structure[0])): - if structure[i][j] == 1: # Wall. - update_view(j * size_scaling - self._init_torso_x, - i * size_scaling - self._init_torso_y, - 0) - if structure[i][j] == -1: # Chasm. - update_view(j * size_scaling - self._init_torso_x, - i * size_scaling - self._init_torso_y, - 1) - - # Draw movable blocks. - for block_name, block_type in self.movable_blocks: - block_x, block_y = self.wrapped_env.get_body_com(block_name)[:2] - update_view(block_x, block_y, 2) - - return self._view - - def get_range_sensor_obs(self): - """Returns egocentric range sensor observations of maze.""" - robot_x, robot_y, robot_z = self.wrapped_env.get_body_com("torso")[:3] - ori = self.get_ori() - - structure = self.MAZE_STRUCTURE - size_scaling = self.MAZE_SIZE_SCALING - height = self.MAZE_HEIGHT - - segments = [] - # Get line segments (corresponding to outer boundary) of each immovable - # block or drop-off. - for i in range(len(structure)): - for j in range(len(structure[0])): - if structure[i][j] in [1, -1]: # There's a wall or drop-off. - cx = j * size_scaling - self._init_torso_x - cy = i * size_scaling - self._init_torso_y - x1 = cx - 0.5 * size_scaling - x2 = cx + 0.5 * size_scaling - y1 = cy - 0.5 * size_scaling - y2 = cy + 0.5 * size_scaling - struct_segments = [ - ((x1, y1), (x2, y1)), - ((x2, y1), (x2, y2)), - ((x2, y2), (x1, y2)), - ((x1, y2), (x1, y1)), - ] - for seg in struct_segments: - segments.append(dict( - segment=seg, - type=structure[i][j], - )) - # Get line segments (corresponding to outer boundary) of each movable - # block within the agent's z-view. - for block_name, block_type in self.movable_blocks: - block_x, block_y, block_z = self.wrapped_env.get_body_com(block_name)[:3] - if (block_z + height * size_scaling / 2 >= robot_z and - robot_z >= block_z - height * size_scaling / 2): # Block in view. - x1 = block_x - 0.5 * size_scaling - x2 = block_x + 0.5 * size_scaling - y1 = block_y - 0.5 * size_scaling - y2 = block_y + 0.5 * size_scaling - struct_segments = [ - ((x1, y1), (x2, y1)), - ((x2, y1), (x2, y2)), - ((x2, y2), (x1, y2)), - ((x1, y2), (x1, y1)), - ] - for seg in struct_segments: - segments.append(dict( - segment=seg, - type=block_type, - )) - - sensor_readings = np.zeros((self._n_bins, 3)) # 3 for wall, drop-off, block - for ray_idx in range(self._n_bins): - ray_ori = (ori - self._sensor_span * 0.5 + - (2 * ray_idx + 1.0) / (2 * self._n_bins) * self._sensor_span) - ray_segments = [] - # Get all segments that intersect with ray. - for seg in segments: - p = maze_env_utils.ray_segment_intersect( - ray=((robot_x, robot_y), ray_ori), - segment=seg["segment"]) - if p is not None: - ray_segments.append(dict( - segment=seg["segment"], - type=seg["type"], - ray_ori=ray_ori, - distance=maze_env_utils.point_distance(p, (robot_x, robot_y)), - )) - if len(ray_segments) > 0: - # Find out which segment is intersected first. - first_seg = sorted(ray_segments, key=lambda x: x["distance"])[0] - seg_type = first_seg["type"] - idx = (0 if seg_type == 1 else # Wall. - 1 if seg_type == -1 else # Drop-off. - 2 if maze_env_utils.can_move(seg_type) else # Block. - None) - if first_seg["distance"] <= self._sensor_range: - sensor_readings[ray_idx][idx] = (self._sensor_range - first_seg["distance"]) / self._sensor_range - - return sensor_readings - - def _get_obs(self): - wrapped_obs = self.wrapped_env._get_obs() - if self._top_down_view: - view = [self.get_top_down_view().flat] - else: - view = [] - - if self._observe_blocks: - additional_obs = [] - for block_name, block_type in self.movable_blocks: - additional_obs.append(self.wrapped_env.get_body_com(block_name)) - wrapped_obs = np.concatenate([wrapped_obs[:3]] + additional_obs + - [wrapped_obs[3:]]) - - range_sensor_obs = self.get_range_sensor_obs() - return np.concatenate([wrapped_obs, - range_sensor_obs.flat] + - view + [[self.t * 0.001]]) - - def reset(self): - self.t = 0 - self.trajectory = [] - self.wrapped_env.reset() - if len(self._init_positions) > 1: - xy = random.choice(self._init_positions) - self.wrapped_env.set_xy(xy) - return self._get_obs() - - @property - def viewer(self): - return self.wrapped_env.viewer - - def render(self, *args, **kwargs): - return self.wrapped_env.render(*args, **kwargs) - - @property - def observation_space(self): - shape = self._get_obs().shape - high = np.inf * np.ones(shape) - low = -high - return gym.spaces.Box(low, high) - - @property - def action_space(self): - return self.wrapped_env.action_space - - def _find_robot(self): - structure = self.MAZE_STRUCTURE - size_scaling = self.MAZE_SIZE_SCALING - for i in range(len(structure)): - for j in range(len(structure[0])): - if structure[i][j] == 'r': - return j * size_scaling, i * size_scaling - assert False, 'No robot in maze specification.' - - def _find_all_robots(self): - structure = self.MAZE_STRUCTURE - size_scaling = self.MAZE_SIZE_SCALING - coords = [] - for i in range(len(structure)): - for j in range(len(structure[0])): - if structure[i][j] == 'r': - coords.append((j * size_scaling, i * size_scaling)) - return coords - - def _is_in_collision(self, pos): - x, y = pos - structure = self.MAZE_STRUCTURE - size_scaling = self.MAZE_SIZE_SCALING - for i in range(len(structure)): - for j in range(len(structure[0])): - if structure[i][j] == 1: - minx = j * size_scaling - size_scaling * 0.5 - self._init_torso_x - maxx = j * size_scaling + size_scaling * 0.5 - self._init_torso_x - miny = i * size_scaling - size_scaling * 0.5 - self._init_torso_y - maxy = i * size_scaling + size_scaling * 0.5 - self._init_torso_y - if minx <= x <= maxx and miny <= y <= maxy: - return True - return False - - def step(self, action): - self.t += 1 - if self._manual_collision: - old_pos = self.wrapped_env.get_xy() - inner_next_obs, inner_reward, done, info = self.wrapped_env.step(action) - new_pos = self.wrapped_env.get_xy() - if self._is_in_collision(new_pos): - self.wrapped_env.set_xy(old_pos) - else: - inner_next_obs, inner_reward, done, info = self.wrapped_env.step(action) - next_obs = self._get_obs() - done = False - return next_obs, inner_reward, done, info diff --git a/research/efficient-hrl/environments/maze_env_utils.py b/research/efficient-hrl/environments/maze_env_utils.py deleted file mode 100644 index 4f52509b65a..00000000000 --- a/research/efficient-hrl/environments/maze_env_utils.py +++ /dev/null @@ -1,164 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Adapted from rllab maze_env_utils.py.""" -import numpy as np -import math - - -class Move(object): - X = 11 - Y = 12 - Z = 13 - XY = 14 - XZ = 15 - YZ = 16 - XYZ = 17 - SpinXY = 18 - - -def can_move_x(movable): - return movable in [Move.X, Move.XY, Move.XZ, Move.XYZ, - Move.SpinXY] - - -def can_move_y(movable): - return movable in [Move.Y, Move.XY, Move.YZ, Move.XYZ, - Move.SpinXY] - - -def can_move_z(movable): - return movable in [Move.Z, Move.XZ, Move.YZ, Move.XYZ] - - -def can_spin(movable): - return movable in [Move.SpinXY] - - -def can_move(movable): - return can_move_x(movable) or can_move_y(movable) or can_move_z(movable) - - -def construct_maze(maze_id='Maze'): - if maze_id == 'Maze': - structure = [ - [1, 1, 1, 1, 1], - [1, 'r', 0, 0, 1], - [1, 1, 1, 0, 1], - [1, 0, 0, 0, 1], - [1, 1, 1, 1, 1], - ] - elif maze_id == 'Push': - structure = [ - [1, 1, 1, 1, 1], - [1, 0, 'r', 1, 1], - [1, 0, Move.XY, 0, 1], - [1, 1, 0, 1, 1], - [1, 1, 1, 1, 1], - ] - elif maze_id == 'Fall': - structure = [ - [1, 1, 1, 1], - [1, 'r', 0, 1], - [1, 0, Move.YZ, 1], - [1, -1, -1, 1], - [1, 0, 0, 1], - [1, 1, 1, 1], - ] - elif maze_id == 'Block': - O = 'r' - structure = [ - [1, 1, 1, 1, 1], - [1, O, 0, 0, 1], - [1, 0, 0, 0, 1], - [1, 0, 0, 0, 1], - [1, 1, 1, 1, 1], - ] - elif maze_id == 'BlockMaze': - O = 'r' - structure = [ - [1, 1, 1, 1], - [1, O, 0, 1], - [1, 1, 0, 1], - [1, 0, 0, 1], - [1, 1, 1, 1], - ] - else: - raise NotImplementedError('The provided MazeId %s is not recognized' % maze_id) - - return structure - - -def line_intersect(pt1, pt2, ptA, ptB): - """ - Taken from https://www.cs.hmc.edu/ACM/lectures/intersections.html - - this returns the intersection of Line(pt1,pt2) and Line(ptA,ptB) - """ - - DET_TOLERANCE = 0.00000001 - - # the first line is pt1 + r*(pt2-pt1) - # in component form: - x1, y1 = pt1 - x2, y2 = pt2 - dx1 = x2 - x1 - dy1 = y2 - y1 - - # the second line is ptA + s*(ptB-ptA) - x, y = ptA - xB, yB = ptB - dx = xB - x - dy = yB - y - - DET = (-dx1 * dy + dy1 * dx) - - if math.fabs(DET) < DET_TOLERANCE: return (0, 0, 0, 0, 0) - - # now, the determinant should be OK - DETinv = 1.0 / DET - - # find the scalar amount along the "self" segment - r = DETinv * (-dy * (x - x1) + dx * (y - y1)) - - # find the scalar amount along the input line - s = DETinv * (-dy1 * (x - x1) + dx1 * (y - y1)) - - # return the average of the two descriptions - xi = (x1 + r * dx1 + x + s * dx) / 2.0 - yi = (y1 + r * dy1 + y + s * dy) / 2.0 - return (xi, yi, 1, r, s) - - -def ray_segment_intersect(ray, segment): - """ - Check if the ray originated from (x, y) with direction theta intersects the line segment (x1, y1) -- (x2, y2), - and return the intersection point if there is one - """ - (x, y), theta = ray - # (x1, y1), (x2, y2) = segment - pt1 = (x, y) - len = 1 - pt2 = (x + len * math.cos(theta), y + len * math.sin(theta)) - xo, yo, valid, r, s = line_intersect(pt1, pt2, *segment) - if valid and r >= 0 and 0 <= s <= 1: - return (xo, yo) - return None - - -def point_distance(p1, p2): - x1, y1 = p1 - x2, y2 = p2 - return ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 diff --git a/research/efficient-hrl/environments/point.py b/research/efficient-hrl/environments/point.py deleted file mode 100644 index 9c2fc80bc82..00000000000 --- a/research/efficient-hrl/environments/point.py +++ /dev/null @@ -1,97 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Wrapper for creating the ant environment in gym_mujoco.""" - -import math -import numpy as np -import mujoco_py -from gym import utils -from gym.envs.mujoco import mujoco_env - - -class PointEnv(mujoco_env.MujocoEnv, utils.EzPickle): - FILE = "point.xml" - ORI_IND = 2 - - def __init__(self, file_path=None, expose_all_qpos=True): - self._expose_all_qpos = expose_all_qpos - - mujoco_env.MujocoEnv.__init__(self, file_path, 1) - utils.EzPickle.__init__(self) - - @property - def physics(self): - # check mujoco version is greater than version 1.50 to call correct physics - # model containing PyMjData object for getting and setting position/velocity - # check https://github.com/openai/mujoco-py/issues/80 for updates to api - if mujoco_py.get_version() >= '1.50': - return self.sim - else: - return self.model - - def _step(self, a): - return self.step(a) - - def step(self, action): - action[0] = 0.2 * action[0] - qpos = np.copy(self.physics.data.qpos) - qpos[2] += action[1] - ori = qpos[2] - # compute increment in each direction - dx = math.cos(ori) * action[0] - dy = math.sin(ori) * action[0] - # ensure that the robot is within reasonable range - qpos[0] = np.clip(qpos[0] + dx, -100, 100) - qpos[1] = np.clip(qpos[1] + dy, -100, 100) - qvel = self.physics.data.qvel - self.set_state(qpos, qvel) - for _ in range(0, self.frame_skip): - self.physics.step() - next_obs = self._get_obs() - reward = 0 - done = False - info = {} - return next_obs, reward, done, info - - def _get_obs(self): - if self._expose_all_qpos: - return np.concatenate([ - self.physics.data.qpos.flat[:3], # Only point-relevant coords. - self.physics.data.qvel.flat[:3]]) - return np.concatenate([ - self.physics.data.qpos.flat[2:3], - self.physics.data.qvel.flat[:3]]) - - def reset_model(self): - qpos = self.init_qpos + self.np_random.uniform( - size=self.physics.model.nq, low=-.1, high=.1) - qvel = self.init_qvel + self.np_random.randn(self.physics.model.nv) * .1 - - # Set everything other than point to original position and 0 velocity. - qpos[3:] = self.init_qpos[3:] - qvel[3:] = 0. - self.set_state(qpos, qvel) - return self._get_obs() - - def get_ori(self): - return self.physics.data.qpos[self.__class__.ORI_IND] - - def set_xy(self, xy): - qpos = np.copy(self.physics.data.qpos) - qpos[0] = xy[0] - qpos[1] = xy[1] - - qvel = self.physics.data.qvel diff --git a/research/efficient-hrl/environments/point_maze_env.py b/research/efficient-hrl/environments/point_maze_env.py deleted file mode 100644 index 8d6b8194863..00000000000 --- a/research/efficient-hrl/environments/point_maze_env.py +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -from environments.maze_env import MazeEnv -from environments.point import PointEnv - - -class PointMazeEnv(MazeEnv): - MODEL_CLASS = PointEnv diff --git a/research/efficient-hrl/eval.py b/research/efficient-hrl/eval.py deleted file mode 100644 index 4f5a4b20a53..00000000000 --- a/research/efficient-hrl/eval.py +++ /dev/null @@ -1,460 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Script for evaluating a UVF agent. - -To run locally: See run_eval.py - -To run on borg: See train_eval.borg -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import tensorflow as tf -slim = tf.contrib.slim -import gin.tf -# pylint: disable=unused-import -import agent -import train -from utils import utils as uvf_utils -from utils import eval_utils -from environments import create_maze_env -# pylint: enable=unused-import - -flags = tf.app.flags - -flags.DEFINE_string('eval_dir', None, - 'Directory for writing logs/summaries during eval.') -flags.DEFINE_string('checkpoint_dir', None, - 'Directory containing checkpoints to eval.') -FLAGS = flags.FLAGS - - -def get_evaluate_checkpoint_fn(master, output_dir, eval_step_fns, - model_rollout_fn, gamma, max_steps_per_episode, - num_episodes_eval, num_episodes_videos, - tuner_hook, generate_videos, - generate_summaries, video_settings): - """Returns a function that evaluates a given checkpoint. - - Args: - master: BNS name of the TensorFlow master - output_dir: The output directory to which the metric summaries are written. - eval_step_fns: A dictionary of a functions that return a list of - [state, action, reward, discount, transition_type] tensors, - indexed by summary tag name. - model_rollout_fn: Model rollout fn. - gamma: Discount factor for the reward. - max_steps_per_episode: Maximum steps to run each episode for. - num_episodes_eval: Number of episodes to evaluate and average reward over. - num_episodes_videos: Number of episodes to record for video. - tuner_hook: A callable(average reward, global step) that updates a Vizier - tuner trial. - generate_videos: Whether to generate videos of the agent in action. - generate_summaries: Whether to generate summaries. - video_settings: Settings for generating videos of the agent. - - Returns: - A function that evaluates a checkpoint. - """ - sess = tf.Session(master, graph=tf.get_default_graph()) - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - summary_writer = tf.summary.FileWriter(output_dir) - - def evaluate_checkpoint(checkpoint_path): - """Performs a one-time evaluation of the given checkpoint. - - Args: - checkpoint_path: Checkpoint to evaluate. - Returns: - True if the evaluation process should stop - """ - restore_fn = tf.contrib.framework.assign_from_checkpoint_fn( - checkpoint_path, - uvf_utils.get_all_vars(), - ignore_missing_vars=True, - reshape_variables=False) - assert restore_fn is not None, 'cannot restore %s' % checkpoint_path - restore_fn(sess) - global_step = sess.run(slim.get_global_step()) - should_stop = False - max_reward = -1e10 - max_meta_reward = -1e10 - - for eval_tag, (eval_step, env_base,) in sorted(eval_step_fns.items()): - if hasattr(env_base, 'set_sess'): - env_base.set_sess(sess) # set session - - if generate_summaries: - tf.logging.info( - '[%s] Computing average reward over %d episodes at global step %d.', - eval_tag, num_episodes_eval, global_step) - (average_reward, last_reward, - average_meta_reward, last_meta_reward, average_success, - states, actions) = eval_utils.compute_average_reward( - sess, env_base, eval_step, gamma, max_steps_per_episode, - num_episodes_eval) - tf.logging.info('[%s] Average reward = %f', eval_tag, average_reward) - tf.logging.info('[%s] Last reward = %f', eval_tag, last_reward) - tf.logging.info('[%s] Average meta reward = %f', eval_tag, average_meta_reward) - tf.logging.info('[%s] Last meta reward = %f', eval_tag, last_meta_reward) - tf.logging.info('[%s] Average success = %f', eval_tag, average_success) - if model_rollout_fn is not None: - preds, model_losses = eval_utils.compute_model_loss( - sess, model_rollout_fn, states, actions) - for i, (pred, state, model_loss) in enumerate( - zip(preds, states, model_losses)): - tf.logging.info('[%s] Model rollout step %d: loss=%f', eval_tag, i, - model_loss) - tf.logging.info('[%s] Model rollout step %d: pred=%s', eval_tag, i, - str(pred.tolist())) - tf.logging.info('[%s] Model rollout step %d: state=%s', eval_tag, i, - str(state.tolist())) - - # Report the eval stats to the tuner. - if average_reward > max_reward: - max_reward = average_reward - if average_meta_reward > max_meta_reward: - max_meta_reward = average_meta_reward - - for (tag, value) in [('Reward/average_%s_reward', average_reward), - ('Reward/last_%s_reward', last_reward), - ('Reward/average_%s_meta_reward', average_meta_reward), - ('Reward/last_%s_meta_reward', last_meta_reward), - ('Reward/average_%s_success', average_success)]: - summary_str = tf.Summary(value=[ - tf.Summary.Value( - tag=tag % eval_tag, - simple_value=value) - ]) - summary_writer.add_summary(summary_str, global_step) - summary_writer.flush() - - if generate_videos or should_stop: - # Do a manual reset before generating the video to see the initial - # pose of the robot, towards which the reset controller is moving. - if hasattr(env_base, '_gym_env'): - tf.logging.info('Resetting before recording video') - if hasattr(env_base._gym_env, 'reset_model'): - env_base._gym_env.reset_model() # pylint: disable=protected-access - else: - env_base._gym_env.wrapped_env.reset_model() - video_filename = os.path.join(output_dir, 'videos', - '%s_step_%d.mp4' % (eval_tag, - global_step)) - eval_utils.capture_video(sess, eval_step, env_base, - max_steps_per_episode * num_episodes_videos, - video_filename, video_settings, - reset_every=max_steps_per_episode) - - should_stop = should_stop or (generate_summaries and tuner_hook and - tuner_hook(max_reward, global_step)) - return bool(should_stop) - - return evaluate_checkpoint - - -def get_model_rollout(uvf_agent, tf_env): - """Model rollout function.""" - state_spec = tf_env.observation_spec()[0] - action_spec = tf_env.action_spec()[0] - state_ph = tf.placeholder(dtype=state_spec.dtype, shape=state_spec.shape) - action_ph = tf.placeholder(dtype=action_spec.dtype, shape=action_spec.shape) - - merged_state = uvf_agent.merged_state(state_ph) - diff_value = uvf_agent.critic_net(tf.expand_dims(merged_state, 0), - tf.expand_dims(action_ph, 0))[0] - diff_value = tf.cast(diff_value, dtype=state_ph.dtype) - state_ph.shape.assert_is_compatible_with(diff_value.shape) - next_state = state_ph + diff_value - - def model_rollout_fn(sess, state, action): - return sess.run(next_state, feed_dict={state_ph: state, action_ph: action}) - - return model_rollout_fn - - -def get_eval_step(uvf_agent, - state_preprocess, - tf_env, - action_fn, - meta_action_fn, - environment_steps, - num_episodes, - mode='eval'): - """Get one-step policy/env stepping ops. - - Args: - uvf_agent: A UVF agent. - tf_env: A TFEnvironment. - action_fn: A function to produce actions given current state. - meta_action_fn: A function to produce meta actions given current state. - environment_steps: A variable to count the number of steps in the tf_env. - num_episodes: A variable to count the number of episodes. - mode: a string representing the mode=[train, explore, eval]. - - Returns: - A collect_experience_op that excute an action and store into the - replay_buffer - """ - - tf_env.start_collect() - state = tf_env.current_obs() - action = action_fn(state, context=None) - state_repr = state_preprocess(state) - - action_spec = tf_env.action_spec() - action_ph = tf.placeholder(dtype=action_spec.dtype, shape=action_spec.shape) - with tf.control_dependencies([state]): - transition_type, reward, discount = tf_env.step(action_ph) - - def increment_step(): - return environment_steps.assign_add(1) - - def increment_episode(): - return num_episodes.assign_add(1) - - def no_op_int(): - return tf.constant(0, dtype=tf.int64) - - step_cond = uvf_agent.step_cond_fn(state, action, - transition_type, - environment_steps, num_episodes) - reset_episode_cond = uvf_agent.reset_episode_cond_fn( - state, action, - transition_type, environment_steps, num_episodes) - reset_env_cond = uvf_agent.reset_env_cond_fn(state, action, - transition_type, - environment_steps, num_episodes) - - increment_step_op = tf.cond(step_cond, increment_step, no_op_int) - with tf.control_dependencies([increment_step_op]): - increment_episode_op = tf.cond(reset_episode_cond, increment_episode, - no_op_int) - - with tf.control_dependencies([reward, discount]): - next_state = tf_env.current_obs() - next_state_repr = state_preprocess(next_state) - - with tf.control_dependencies([increment_episode_op]): - post_reward, post_meta_reward = uvf_agent.cond_begin_episode_op( - tf.logical_not(reset_episode_cond), - [state, action_ph, reward, next_state, - state_repr, next_state_repr], - mode=mode, meta_action_fn=meta_action_fn) - - # Important: do manual reset after getting the final reward from the - # unreset environment. - with tf.control_dependencies([post_reward, post_meta_reward]): - cond_reset_op = tf.cond(reset_env_cond, - tf_env.reset, - tf_env.current_time_step) - - # Add a dummy control dependency to force the reset_op to run - with tf.control_dependencies(cond_reset_op): - post_reward, post_meta_reward = map(tf.identity, [post_reward, post_meta_reward]) - - eval_step = [next_state, action_ph, transition_type, post_reward, post_meta_reward, discount, uvf_agent.context_vars, state_repr] - - if callable(action): - def step_fn(sess): - action_value = action(sess) - return sess.run(eval_step, feed_dict={action_ph: action_value}) - else: - action = uvf_utils.clip_to_spec(action, action_spec) - def step_fn(sess): - action_value = sess.run(action) - return sess.run(eval_step, feed_dict={action_ph: action_value}) - - return step_fn - - -@gin.configurable -def evaluate(checkpoint_dir, - eval_dir, - environment=None, - num_bin_actions=3, - agent_class=None, - meta_agent_class=None, - state_preprocess_class=None, - gamma=1.0, - num_episodes_eval=10, - eval_interval_secs=60, - max_number_of_evaluations=None, - checkpoint_timeout=None, - timeout_fn=None, - tuner_hook=None, - generate_videos=False, - generate_summaries=True, - num_episodes_videos=5, - video_settings=None, - eval_modes=('eval',), - eval_model_rollout=False, - policy_save_dir='policy', - checkpoint_range=None, - checkpoint_path=None, - max_steps_per_episode=None, - evaluate_nohrl=False): - """Loads and repeatedly evaluates a checkpointed model at a set interval. - - Args: - checkpoint_dir: The directory where the checkpoints reside. - eval_dir: Directory to save the evaluation summary results. - environment: A BaseEnvironment to evaluate. - num_bin_actions: Number of bins for discretizing continuous actions. - agent_class: An RL agent class. - meta_agent_class: A Meta agent class. - gamma: Discount factor for the reward. - num_episodes_eval: Number of episodes to evaluate and average reward over. - eval_interval_secs: The number of seconds between each evaluation run. - max_number_of_evaluations: The max number of evaluations. If None the - evaluation continues indefinitely. - checkpoint_timeout: The maximum amount of time to wait between checkpoints. - If left as `None`, then the process will wait indefinitely. - timeout_fn: Optional function to call after a timeout. - tuner_hook: A callable that takes the average reward and global step and - updates a Vizier tuner trial. - generate_videos: Whether to generate videos of the agent in action. - generate_summaries: Whether to generate summaries. - num_episodes_videos: Number of episodes to evaluate for generating videos. - video_settings: Settings for generating videos of the agent. - optimal action based on the critic. - eval_modes: A tuple of eval modes. - eval_model_rollout: Evaluate model rollout. - policy_save_dir: Optional sub-directory where the policies are - saved. - checkpoint_range: Optional. If provided, evaluate all checkpoints in - the range. - checkpoint_path: Optional sub-directory specifying which checkpoint to - evaluate. If None, will evaluate the most recent checkpoint. - """ - tf_env = create_maze_env.TFPyEnvironment(environment) - observation_spec = [tf_env.observation_spec()] - action_spec = [tf_env.action_spec()] - - assert max_steps_per_episode, 'max_steps_per_episode need to be set' - - if agent_class.ACTION_TYPE == 'discrete': - assert False - else: - assert agent_class.ACTION_TYPE == 'continuous' - - if meta_agent_class is not None: - assert agent_class.ACTION_TYPE == meta_agent_class.ACTION_TYPE - with tf.variable_scope('meta_agent'): - meta_agent = meta_agent_class( - observation_spec, - action_spec, - tf_env, - ) - else: - meta_agent = None - - with tf.variable_scope('uvf_agent'): - uvf_agent = agent_class( - observation_spec, - action_spec, - tf_env, - ) - uvf_agent.set_meta_agent(agent=meta_agent) - - with tf.variable_scope('state_preprocess'): - state_preprocess = state_preprocess_class() - - # run both actor and critic once to ensure networks are initialized - # and gin configs will be saved - # pylint: disable=protected-access - temp_states = tf.expand_dims( - tf.zeros( - dtype=uvf_agent._observation_spec.dtype, - shape=uvf_agent._observation_spec.shape), 0) - # pylint: enable=protected-access - temp_actions = uvf_agent.actor_net(temp_states) - uvf_agent.critic_net(temp_states, temp_actions) - - # create eval_step_fns for each action function - eval_step_fns = dict() - meta_agent = uvf_agent.meta_agent - for meta in [True] + [False] * evaluate_nohrl: - meta_tag = 'hrl' if meta else 'nohrl' - uvf_agent.set_meta_agent(meta_agent if meta else None) - for mode in eval_modes: - # wrap environment - wrapped_environment = uvf_agent.get_env_base_wrapper( - environment, mode=mode) - action_wrapper = lambda agent_: agent_.action - action_fn = action_wrapper(uvf_agent) - meta_action_fn = action_wrapper(meta_agent) - eval_step_fns['%s_%s' % (mode, meta_tag)] = (get_eval_step( - uvf_agent=uvf_agent, - state_preprocess=state_preprocess, - tf_env=tf_env, - action_fn=action_fn, - meta_action_fn=meta_action_fn, - environment_steps=tf.Variable( - 0, dtype=tf.int64, name='environment_steps'), - num_episodes=tf.Variable(0, dtype=tf.int64, name='num_episodes'), - mode=mode), wrapped_environment,) - - model_rollout_fn = None - if eval_model_rollout: - model_rollout_fn = get_model_rollout(uvf_agent, tf_env) - - tf.train.get_or_create_global_step() - - if policy_save_dir: - checkpoint_dir = os.path.join(checkpoint_dir, policy_save_dir) - - tf.logging.info('Evaluating policies at %s', checkpoint_dir) - tf.logging.info('Running episodes for max %d steps', max_steps_per_episode) - - evaluate_checkpoint_fn = get_evaluate_checkpoint_fn( - '', eval_dir, eval_step_fns, model_rollout_fn, gamma, - max_steps_per_episode, num_episodes_eval, num_episodes_videos, tuner_hook, - generate_videos, generate_summaries, video_settings) - - if checkpoint_path is not None: - checkpoint_path = os.path.join(checkpoint_dir, checkpoint_path) - evaluate_checkpoint_fn(checkpoint_path) - elif checkpoint_range is not None: - model_files = tf.gfile.Glob( - os.path.join(checkpoint_dir, 'model.ckpt-*.index')) - tf.logging.info('Found %s policies at %s', len(model_files), checkpoint_dir) - model_files = { - int(f.split('model.ckpt-', 1)[1].split('.', 1)[0]): - os.path.splitext(f)[0] - for f in model_files - } - model_files = { - k: v - for k, v in model_files.items() - if k >= checkpoint_range[0] and k <= checkpoint_range[1] - } - tf.logging.info('Evaluating %d policies at %s', - len(model_files), checkpoint_dir) - for _, checkpoint_path in sorted(model_files.items()): - evaluate_checkpoint_fn(checkpoint_path) - else: - eval_utils.evaluate_checkpoint_repeatedly( - checkpoint_dir, - evaluate_checkpoint_fn, - eval_interval_secs=eval_interval_secs, - max_number_of_evaluations=max_number_of_evaluations, - checkpoint_timeout=checkpoint_timeout, - timeout_fn=timeout_fn) diff --git a/research/efficient-hrl/run_env.py b/research/efficient-hrl/run_env.py deleted file mode 100644 index 87fad542aea..00000000000 --- a/research/efficient-hrl/run_env.py +++ /dev/null @@ -1,129 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Random policy on an environment.""" - -import tensorflow as tf -import numpy as np -import random - -from environments import create_maze_env - -app = tf.app -flags = tf.flags -logging = tf.logging - -FLAGS = flags.FLAGS - -flags.DEFINE_string('env', 'AntMaze', 'environment name: AntMaze, AntPush, or AntFall') -flags.DEFINE_integer('episode_length', 500, 'episode length') -flags.DEFINE_integer('num_episodes', 50, 'number of episodes') - - -def get_goal_sample_fn(env_name): - if env_name == 'AntMaze': - # NOTE: When evaluating (i.e. the metrics shown in the paper, - # we use the commented out goal sampling function. The uncommented - # one is only used for training. - #return lambda: np.array([0., 16.]) - return lambda: np.random.uniform((-4, -4), (20, 20)) - elif env_name == 'AntPush': - return lambda: np.array([0., 19.]) - elif env_name == 'AntFall': - return lambda: np.array([0., 27., 4.5]) - else: - assert False, 'Unknown env' - - -def get_reward_fn(env_name): - if env_name == 'AntMaze': - return lambda obs, goal: -np.sum(np.square(obs[:2] - goal)) ** 0.5 - elif env_name == 'AntPush': - return lambda obs, goal: -np.sum(np.square(obs[:2] - goal)) ** 0.5 - elif env_name == 'AntFall': - return lambda obs, goal: -np.sum(np.square(obs[:3] - goal)) ** 0.5 - else: - assert False, 'Unknown env' - - -def success_fn(last_reward): - return last_reward > -5.0 - - -class EnvWithGoal(object): - - def __init__(self, base_env, env_name): - self.base_env = base_env - self.goal_sample_fn = get_goal_sample_fn(env_name) - self.reward_fn = get_reward_fn(env_name) - self.goal = None - - def reset(self): - obs = self.base_env.reset() - self.goal = self.goal_sample_fn() - return np.concatenate([obs, self.goal]) - - def step(self, a): - obs, _, done, info = self.base_env.step(a) - reward = self.reward_fn(obs, self.goal) - return np.concatenate([obs, self.goal]), reward, done, info - - @property - def action_space(self): - return self.base_env.action_space - - -def run_environment(env_name, episode_length, num_episodes): - env = EnvWithGoal( - create_maze_env.create_maze_env(env_name).gym, - env_name) - - def action_fn(obs): - action_space = env.action_space - action_space_mean = (action_space.low + action_space.high) / 2.0 - action_space_magn = (action_space.high - action_space.low) / 2.0 - random_action = (action_space_mean + - action_space_magn * - np.random.uniform(low=-1.0, high=1.0, - size=action_space.shape)) - return random_action - - rewards = [] - successes = [] - for ep in range(num_episodes): - rewards.append(0.0) - successes.append(False) - obs = env.reset() - for _ in range(episode_length): - obs, reward, done, _ = env.step(action_fn(obs)) - rewards[-1] += reward - successes[-1] = success_fn(reward) - if done: - break - logging.info('Episode %d reward: %.2f, Success: %d', ep + 1, rewards[-1], successes[-1]) - - logging.info('Average Reward over %d episodes: %.2f', - num_episodes, np.mean(rewards)) - logging.info('Average Success over %d episodes: %.2f', - num_episodes, np.mean(successes)) - - -def main(unused_argv): - logging.set_verbosity(logging.INFO) - run_environment(FLAGS.env, FLAGS.episode_length, FLAGS.num_episodes) - - -if __name__ == '__main__': - app.run() diff --git a/research/efficient-hrl/run_eval.py b/research/efficient-hrl/run_eval.py deleted file mode 100644 index 12f12369c4c..00000000000 --- a/research/efficient-hrl/run_eval.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Script for evaluating a UVF agent. - -To run locally: See scripts/local_eval.py -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -import gin.tf -# pylint: disable=unused-import -import eval as eval_ -# pylint: enable=unused-import - -flags = tf.app.flags -FLAGS = flags.FLAGS - - -def main(_): - tf.logging.set_verbosity(tf.logging.INFO) - assert FLAGS.checkpoint_dir, "Flag 'checkpoint_dir' must be set." - assert FLAGS.eval_dir, "Flag 'eval_dir' must be set." - - if FLAGS.config_file: - for config_file in FLAGS.config_file: - gin.parse_config_file(config_file) - if FLAGS.params: - gin.parse_config(FLAGS.params) - - eval_.evaluate(FLAGS.checkpoint_dir, FLAGS.eval_dir) - - -if __name__ == "__main__": - tf.app.run() diff --git a/research/efficient-hrl/run_train.py b/research/efficient-hrl/run_train.py deleted file mode 100644 index 1d459d60b7f..00000000000 --- a/research/efficient-hrl/run_train.py +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Script for training an RL agent using the UVF algorithm. - -To run locally: See scripts/local_train.py -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow as tf - -import gin.tf -# pylint: enable=unused-import -import train -# pylint: disable=unused-import - -flags = tf.app.flags -FLAGS = flags.FLAGS - - -def main(_): - tf.logging.set_verbosity(tf.logging.INFO) - if FLAGS.config_file: - for config_file in FLAGS.config_file: - gin.parse_config_file(config_file) - if FLAGS.params: - gin.parse_config(FLAGS.params) - - assert FLAGS.train_dir, "Flag 'train_dir' must be set." - return train.train_uvf(FLAGS.train_dir) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/efficient-hrl/scripts/local_eval.py b/research/efficient-hrl/scripts/local_eval.py deleted file mode 100644 index 89ef745a408..00000000000 --- a/research/efficient-hrl/scripts/local_eval.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Script to run run_eval.py locally. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import os -from subprocess import call -import sys - -CONFIGS_PATH = 'configs' -CONTEXT_CONFIGS_PATH = 'context/configs' - -def main(): - bb = './' - base_num_args = 6 - if len(sys.argv) < base_num_args: - print( - "usage: python %s " - " [params...]" - % sys.argv[0]) - sys.exit(0) - exp = sys.argv[1] - context_setting = sys.argv[2] - context = sys.argv[3] - agent = sys.argv[4] - assert sys.argv[5] in ["suite"], "args[5] must be `suite'" - suite = "" - binary = "python {bb}/run_eval{suite}.py ".format(bb=bb, suite=suite) - - h = os.environ["HOME"] - ucp = CONFIGS_PATH - ccp = CONTEXT_CONFIGS_PATH - extra = '' - command_str = ("{binary} " - "--logtostderr " - "--checkpoint_dir={h}/tmp/{context_setting}/{context}/{agent}/{exp}/train " - "--eval_dir={h}/tmp/{context_setting}/{context}/{agent}/{exp}/eval " - "--config_file={ucp}/{agent}.gin " - "--config_file={ucp}/eval_{extra}uvf.gin " - "--config_file={ccp}/{context_setting}.gin " - "--config_file={ccp}/{context}.gin ").format( - h=h, - ucp=ucp, - ccp=ccp, - context_setting=context_setting, - context=context, - agent=agent, - extra=extra, - suite=suite, - exp=exp, - binary=binary) - for extra_arg in sys.argv[base_num_args:]: - command_str += "--params='%s' " % extra_arg - - print(command_str) - call(command_str, shell=True) - - -if __name__ == "__main__": - main() diff --git a/research/efficient-hrl/scripts/local_train.py b/research/efficient-hrl/scripts/local_train.py deleted file mode 100644 index 718c88e8fed..00000000000 --- a/research/efficient-hrl/scripts/local_train.py +++ /dev/null @@ -1,76 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Script to run run_train.py locally. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import os -import random -from subprocess import call -import sys - -CONFIGS_PATH = './configs' -CONTEXT_CONFIGS_PATH = './context/configs' - -def main(): - bb = '.' - base_num_args = 6 - if len(sys.argv) < base_num_args: - print( - "usage: python %s " - " [params...]" - % sys.argv[0]) - sys.exit(0) - exp = sys.argv[1] # Name for experiment, e.g. 'test001' - context_setting = sys.argv[2] # Context setting, e.g. 'hiro_orig' - context = sys.argv[3] # Environment-specific context, e.g. 'ant_maze' - agent = sys.argv[4] # Agent settings, e.g. 'base_uvf' - assert sys.argv[5] in ["suite"], "args[5] must be `suite'" - suite = "" - binary = "python {bb}/run_train{suite}.py ".format(bb=bb, suite=suite) - - h = os.environ["HOME"] - ucp = CONFIGS_PATH - ccp = CONTEXT_CONFIGS_PATH - extra = '' - port = random.randint(2000, 8000) - command_str = ("{binary} " - "--train_dir={h}/tmp/{context_setting}/{context}/{agent}/{exp}/train " - "--config_file={ucp}/{agent}.gin " - "--config_file={ucp}/train_{extra}uvf.gin " - "--config_file={ccp}/{context_setting}.gin " - "--config_file={ccp}/{context}.gin " - "--summarize_gradients=False " - "--save_interval_secs=60 " - "--save_summaries_secs=1 " - "--master=local " - "--alsologtostderr ").format(h=h, ucp=ucp, - context_setting=context_setting, - context=context, ccp=ccp, - suite=suite, agent=agent, extra=extra, - exp=exp, binary=binary, - port=port) - for extra_arg in sys.argv[base_num_args:]: - command_str += "--params='%s' " % extra_arg - - print(command_str) - call(command_str, shell=True) - - -if __name__ == "__main__": - main() diff --git a/research/efficient-hrl/train.py b/research/efficient-hrl/train.py deleted file mode 100644 index a40e81dbec6..00000000000 --- a/research/efficient-hrl/train.py +++ /dev/null @@ -1,670 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Script for training an RL agent using the UVF algorithm. - -To run locally: See run_train.py -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import time -import tensorflow as tf -slim = tf.contrib.slim - -import gin.tf -# pylint: disable=unused-import -import train_utils -import agent as agent_ -from agents import circular_buffer -from utils import utils as uvf_utils -from environments import create_maze_env -# pylint: enable=unused-import - - -flags = tf.app.flags - -FLAGS = flags.FLAGS -flags.DEFINE_string('goal_sample_strategy', 'sample', - 'None, sample, FuN') - -LOAD_PATH = None - - -def collect_experience(tf_env, agent, meta_agent, state_preprocess, - replay_buffer, meta_replay_buffer, - action_fn, meta_action_fn, - environment_steps, num_episodes, num_resets, - episode_rewards, episode_meta_rewards, - store_context, - disable_agent_reset): - """Collect experience in a tf_env into a replay_buffer using action_fn. - - Args: - tf_env: A TFEnvironment. - agent: A UVF agent. - meta_agent: A Meta Agent. - replay_buffer: A Replay buffer to collect experience in. - meta_replay_buffer: A Replay buffer to collect meta agent experience in. - action_fn: A function to produce actions given current state. - meta_action_fn: A function to produce meta actions given current state. - environment_steps: A variable to count the number of steps in the tf_env. - num_episodes: A variable to count the number of episodes. - num_resets: A variable to count the number of resets. - store_context: A boolean to check if store context in replay. - disable_agent_reset: A boolean that disables agent from resetting. - - Returns: - A collect_experience_op that excute an action and store into the - replay_buffers - """ - tf_env.start_collect() - state = tf_env.current_obs() - state_repr = state_preprocess(state) - action = action_fn(state, context=None) - - with tf.control_dependencies([state]): - transition_type, reward, discount = tf_env.step(action) - - def increment_step(): - return environment_steps.assign_add(1) - - def increment_episode(): - return num_episodes.assign_add(1) - - def increment_reset(): - return num_resets.assign_add(1) - - def update_episode_rewards(context_reward, meta_reward, reset): - new_episode_rewards = tf.concat( - [episode_rewards[:1] + context_reward, episode_rewards[1:]], 0) - new_episode_meta_rewards = tf.concat( - [episode_meta_rewards[:1] + meta_reward, - episode_meta_rewards[1:]], 0) - return tf.group( - episode_rewards.assign( - tf.cond(reset, - lambda: tf.concat([[0.], episode_rewards[:-1]], 0), - lambda: new_episode_rewards)), - episode_meta_rewards.assign( - tf.cond(reset, - lambda: tf.concat([[0.], episode_meta_rewards[:-1]], 0), - lambda: new_episode_meta_rewards))) - - def no_op_int(): - return tf.constant(0, dtype=tf.int64) - - step_cond = agent.step_cond_fn(state, action, - transition_type, - environment_steps, num_episodes) - reset_episode_cond = agent.reset_episode_cond_fn( - state, action, - transition_type, environment_steps, num_episodes) - reset_env_cond = agent.reset_env_cond_fn(state, action, - transition_type, - environment_steps, num_episodes) - - increment_step_op = tf.cond(step_cond, increment_step, no_op_int) - increment_episode_op = tf.cond(reset_episode_cond, increment_episode, - no_op_int) - increment_reset_op = tf.cond(reset_env_cond, increment_reset, no_op_int) - increment_op = tf.group(increment_step_op, increment_episode_op, - increment_reset_op) - - with tf.control_dependencies([increment_op, reward, discount]): - next_state = tf_env.current_obs() - next_state_repr = state_preprocess(next_state) - next_reset_episode_cond = tf.logical_or( - agent.reset_episode_cond_fn( - state, action, - transition_type, environment_steps, num_episodes), - tf.equal(discount, 0.0)) - - if store_context: - context = [tf.identity(var) + tf.zeros_like(var) for var in agent.context_vars] - meta_context = [tf.identity(var) + tf.zeros_like(var) for var in meta_agent.context_vars] - else: - context = [] - meta_context = [] - with tf.control_dependencies([next_state] + context + meta_context): - if disable_agent_reset: - collect_experience_ops = [tf.no_op()] # don't reset agent - else: - collect_experience_ops = agent.cond_begin_episode_op( - tf.logical_not(reset_episode_cond), - [state, action, reward, next_state, - state_repr, next_state_repr], - mode='explore', meta_action_fn=meta_action_fn) - context_reward, meta_reward = collect_experience_ops - collect_experience_ops = list(collect_experience_ops) - collect_experience_ops.append( - update_episode_rewards(tf.reduce_sum(context_reward), meta_reward, - reset_episode_cond)) - - meta_action_every_n = agent.tf_context.meta_action_every_n - with tf.control_dependencies(collect_experience_ops): - transition = [state, action, reward, discount, next_state] - - meta_action = tf.to_float( - tf.concat(context, -1)) # Meta agent action is low-level context - - meta_end = tf.logical_and( # End of meta-transition. - tf.equal(agent.tf_context.t % meta_action_every_n, 1), - agent.tf_context.t > 1) - with tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE): - states_var = tf.get_variable('states_var', - [meta_action_every_n, state.shape[-1]], - state.dtype) - actions_var = tf.get_variable('actions_var', - [meta_action_every_n, action.shape[-1]], - action.dtype) - state_var = tf.get_variable('state_var', state.shape, state.dtype) - reward_var = tf.get_variable('reward_var', reward.shape, reward.dtype) - meta_action_var = tf.get_variable('meta_action_var', - meta_action.shape, meta_action.dtype) - meta_context_var = [ - tf.get_variable('meta_context_var%d' % idx, - meta_context[idx].shape, meta_context[idx].dtype) - for idx in range(len(meta_context))] - - actions_var_upd = tf.scatter_update( - actions_var, (agent.tf_context.t - 2) % meta_action_every_n, action) - with tf.control_dependencies([actions_var_upd]): - actions = tf.identity(actions_var) + tf.zeros_like(actions_var) - meta_reward = tf.identity(meta_reward) + tf.zeros_like(meta_reward) - meta_reward = tf.reshape(meta_reward, reward.shape) - - reward = 0.1 * meta_reward - meta_transition = [state_var, meta_action_var, - reward_var + reward, - discount * (1 - tf.to_float(next_reset_episode_cond)), - next_state] - meta_transition.extend([states_var, actions]) - if store_context: # store current and next context into replay - transition += context + list(agent.context_vars) - meta_transition += meta_context_var + list(meta_agent.context_vars) - - meta_step_cond = tf.squeeze(tf.logical_and(step_cond, tf.logical_or(next_reset_episode_cond, meta_end))) - - collect_experience_op = tf.group( - replay_buffer.maybe_add(transition, step_cond), - meta_replay_buffer.maybe_add(meta_transition, meta_step_cond), - ) - - with tf.control_dependencies([collect_experience_op]): - collect_experience_op = tf.cond(reset_env_cond, - tf_env.reset, - tf_env.current_time_step) - - meta_period = tf.equal(agent.tf_context.t % meta_action_every_n, 1) - states_var_upd = tf.scatter_update( - states_var, (agent.tf_context.t - 1) % meta_action_every_n, - next_state) - state_var_upd = tf.assign( - state_var, - tf.cond(meta_period, lambda: next_state, lambda: state_var)) - reward_var_upd = tf.assign( - reward_var, - tf.cond(meta_period, - lambda: tf.zeros_like(reward_var), - lambda: reward_var + reward)) - meta_action = tf.to_float(tf.concat(agent.context_vars, -1)) - meta_action_var_upd = tf.assign( - meta_action_var, - tf.cond(meta_period, lambda: meta_action, lambda: meta_action_var)) - meta_context_var_upd = [ - tf.assign( - meta_context_var[idx], - tf.cond(meta_period, - lambda: meta_agent.context_vars[idx], - lambda: meta_context_var[idx])) - for idx in range(len(meta_context))] - - return tf.group( - collect_experience_op, - states_var_upd, - state_var_upd, - reward_var_upd, - meta_action_var_upd, - *meta_context_var_upd) - - -def sample_best_meta_actions(state_reprs, next_state_reprs, prev_meta_actions, - low_states, low_actions, low_state_reprs, - inverse_dynamics, uvf_agent, k=10): - """Return meta-actions which approximately maximize low-level log-probs.""" - sampled_actions = inverse_dynamics.sample(state_reprs, next_state_reprs, k, prev_meta_actions) - sampled_actions = tf.stop_gradient(sampled_actions) - sampled_log_probs = tf.reshape(uvf_agent.log_probs( - tf.tile(low_states, [k, 1, 1]), - tf.tile(low_actions, [k, 1, 1]), - tf.tile(low_state_reprs, [k, 1, 1]), - [tf.reshape(sampled_actions, [-1, sampled_actions.shape[-1]])]), - [k, low_states.shape[0], - low_states.shape[1], -1]) - fitness = tf.reduce_sum(sampled_log_probs, [2, 3]) - best_actions = tf.argmax(fitness, 0) - actions = tf.gather_nd( - sampled_actions, - tf.stack([best_actions, - tf.range(prev_meta_actions.shape[0], dtype=tf.int64)], -1)) - return actions - - -@gin.configurable -def train_uvf(train_dir, - environment=None, - num_bin_actions=3, - agent_class=None, - meta_agent_class=None, - state_preprocess_class=None, - inverse_dynamics_class=None, - exp_action_wrapper=None, - replay_buffer=None, - meta_replay_buffer=None, - replay_num_steps=1, - meta_replay_num_steps=1, - critic_optimizer=None, - actor_optimizer=None, - meta_critic_optimizer=None, - meta_actor_optimizer=None, - repr_optimizer=None, - relabel_contexts=False, - meta_relabel_contexts=False, - batch_size=64, - repeat_size=0, - num_episodes_train=2000, - initial_episodes=2, - initial_steps=None, - num_updates_per_observation=1, - num_collect_per_update=1, - num_collect_per_meta_update=1, - gamma=1.0, - meta_gamma=1.0, - reward_scale_factor=1.0, - target_update_period=1, - should_stop_early=None, - clip_gradient_norm=0.0, - summarize_gradients=False, - debug_summaries=False, - log_every_n_steps=100, - prefetch_queue_capacity=2, - policy_save_dir='policy', - save_policy_every_n_steps=1000, - save_policy_interval_secs=0, - replay_context_ratio=0.0, - next_state_as_context_ratio=0.0, - state_index=0, - zero_timer_ratio=0.0, - timer_index=-1, - debug=False, - max_policies_to_save=None, - max_steps_per_episode=None, - load_path=LOAD_PATH): - """Train an agent.""" - tf_env = create_maze_env.TFPyEnvironment(environment) - observation_spec = [tf_env.observation_spec()] - action_spec = [tf_env.action_spec()] - - max_steps_per_episode = max_steps_per_episode or tf_env.pyenv.max_episode_steps - - assert max_steps_per_episode, 'max_steps_per_episode need to be set' - - if initial_steps is None: - initial_steps = initial_episodes * max_steps_per_episode - - if agent_class.ACTION_TYPE == 'discrete': - assert False - else: - assert agent_class.ACTION_TYPE == 'continuous' - - assert agent_class.ACTION_TYPE == meta_agent_class.ACTION_TYPE - with tf.variable_scope('meta_agent'): - meta_agent = meta_agent_class( - observation_spec, - action_spec, - tf_env, - debug_summaries=debug_summaries) - meta_agent.set_replay(replay=meta_replay_buffer) - - with tf.variable_scope('uvf_agent'): - uvf_agent = agent_class( - observation_spec, - action_spec, - tf_env, - debug_summaries=debug_summaries) - uvf_agent.set_meta_agent(agent=meta_agent) - uvf_agent.set_replay(replay=replay_buffer) - - with tf.variable_scope('state_preprocess'): - state_preprocess = state_preprocess_class() - - with tf.variable_scope('inverse_dynamics'): - inverse_dynamics = inverse_dynamics_class( - meta_agent.sub_context_as_action_specs[0]) - - # Create counter variables - global_step = tf.contrib.framework.get_or_create_global_step() - num_episodes = tf.Variable(0, dtype=tf.int64, name='num_episodes') - num_resets = tf.Variable(0, dtype=tf.int64, name='num_resets') - num_updates = tf.Variable(0, dtype=tf.int64, name='num_updates') - num_meta_updates = tf.Variable(0, dtype=tf.int64, name='num_meta_updates') - episode_rewards = tf.Variable([0.] * 100, name='episode_rewards') - episode_meta_rewards = tf.Variable([0.] * 100, name='episode_meta_rewards') - - # Create counter variables summaries - train_utils.create_counter_summaries([ - ('environment_steps', global_step), - ('num_episodes', num_episodes), - ('num_resets', num_resets), - ('num_updates', num_updates), - ('num_meta_updates', num_meta_updates), - ('replay_buffer_adds', replay_buffer.get_num_adds()), - ('meta_replay_buffer_adds', meta_replay_buffer.get_num_adds()), - ]) - - tf.summary.scalar('avg_episode_rewards', - tf.reduce_mean(episode_rewards[1:])) - tf.summary.scalar('avg_episode_meta_rewards', - tf.reduce_mean(episode_meta_rewards[1:])) - tf.summary.histogram('episode_rewards', episode_rewards[1:]) - tf.summary.histogram('episode_meta_rewards', episode_meta_rewards[1:]) - - # Create init ops - action_fn = uvf_agent.action - action_fn = uvf_agent.add_noise_fn(action_fn, global_step=None) - meta_action_fn = meta_agent.action - meta_action_fn = meta_agent.add_noise_fn(meta_action_fn, global_step=None) - meta_actions_fn = meta_agent.actions - meta_actions_fn = meta_agent.add_noise_fn(meta_actions_fn, global_step=None) - init_collect_experience_op = collect_experience( - tf_env, - uvf_agent, - meta_agent, - state_preprocess, - replay_buffer, - meta_replay_buffer, - action_fn, - meta_action_fn, - environment_steps=global_step, - num_episodes=num_episodes, - num_resets=num_resets, - episode_rewards=episode_rewards, - episode_meta_rewards=episode_meta_rewards, - store_context=True, - disable_agent_reset=False, - ) - - # Create train ops - collect_experience_op = collect_experience( - tf_env, - uvf_agent, - meta_agent, - state_preprocess, - replay_buffer, - meta_replay_buffer, - action_fn, - meta_action_fn, - environment_steps=global_step, - num_episodes=num_episodes, - num_resets=num_resets, - episode_rewards=episode_rewards, - episode_meta_rewards=episode_meta_rewards, - store_context=True, - disable_agent_reset=False, - ) - - train_op_list = [] - repr_train_op = tf.constant(0.0) - for mode in ['meta', 'nometa']: - if mode == 'meta': - agent = meta_agent - buff = meta_replay_buffer - critic_opt = meta_critic_optimizer - actor_opt = meta_actor_optimizer - relabel = meta_relabel_contexts - num_steps = meta_replay_num_steps - my_gamma = meta_gamma, - n_updates = num_meta_updates - else: - agent = uvf_agent - buff = replay_buffer - critic_opt = critic_optimizer - actor_opt = actor_optimizer - relabel = relabel_contexts - num_steps = replay_num_steps - my_gamma = gamma - n_updates = num_updates - - with tf.name_scope(mode): - batch = buff.get_random_batch(batch_size, num_steps=num_steps) - states, actions, rewards, discounts, next_states = batch[:5] - with tf.name_scope('Reward'): - tf.summary.scalar('average_step_reward', tf.reduce_mean(rewards)) - rewards *= reward_scale_factor - batch_queue = slim.prefetch_queue.prefetch_queue( - [states, actions, rewards, discounts, next_states] + batch[5:], - capacity=prefetch_queue_capacity, - name='batch_queue') - - batch_dequeue = batch_queue.dequeue() - if repeat_size > 0: - batch_dequeue = [ - tf.tile(batch, (repeat_size+1,) + (1,) * (batch.shape.ndims - 1)) - for batch in batch_dequeue - ] - batch_size *= (repeat_size + 1) - states, actions, rewards, discounts, next_states = batch_dequeue[:5] - if mode == 'meta': - low_states = batch_dequeue[5] - low_actions = batch_dequeue[6] - low_state_reprs = state_preprocess(low_states) - state_reprs = state_preprocess(states) - next_state_reprs = state_preprocess(next_states) - - if mode == 'meta': # Re-label meta-action - prev_actions = actions - if FLAGS.goal_sample_strategy == 'None': - pass - elif FLAGS.goal_sample_strategy == 'FuN': - actions = inverse_dynamics.sample(state_reprs, next_state_reprs, 1, prev_actions, sc=0.1) - actions = tf.stop_gradient(actions) - elif FLAGS.goal_sample_strategy == 'sample': - actions = sample_best_meta_actions(state_reprs, next_state_reprs, prev_actions, - low_states, low_actions, low_state_reprs, - inverse_dynamics, uvf_agent, k=10) - else: - assert False - - if state_preprocess.trainable and mode == 'meta': - # Representation learning is based on meta-transitions, but is trained - # along with low-level policy updates. - repr_loss, _, _ = state_preprocess.loss(states, next_states, low_actions, low_states) - repr_train_op = slim.learning.create_train_op( - repr_loss, - repr_optimizer, - global_step=None, - update_ops=None, - summarize_gradients=summarize_gradients, - clip_gradient_norm=clip_gradient_norm, - variables_to_train=state_preprocess.get_trainable_vars(),) - - # Get contexts for training - contexts, next_contexts = agent.sample_contexts( - mode='train', batch_size=batch_size, - state=states, next_state=next_states, - ) - if not relabel: # Re-label context (in the style of TDM or HER). - contexts, next_contexts = ( - batch_dequeue[-2*len(contexts):-1*len(contexts)], - batch_dequeue[-1*len(contexts):]) - - merged_states = agent.merged_states(states, contexts) - merged_next_states = agent.merged_states(next_states, next_contexts) - if mode == 'nometa': - context_rewards, context_discounts = agent.compute_rewards( - 'train', state_reprs, actions, rewards, next_state_reprs, contexts) - elif mode == 'meta': # Meta-agent uses sum of rewards, not context-specific rewards. - _, context_discounts = agent.compute_rewards( - 'train', states, actions, rewards, next_states, contexts) - context_rewards = rewards - - if agent.gamma_index is not None: - context_discounts *= tf.cast( - tf.reshape(contexts[agent.gamma_index], (-1,)), - dtype=context_discounts.dtype) - else: context_discounts *= my_gamma - - critic_loss = agent.critic_loss(merged_states, actions, - context_rewards, context_discounts, - merged_next_states) - - critic_loss = tf.reduce_mean(critic_loss) - - actor_loss = agent.actor_loss(merged_states, actions, - context_rewards, context_discounts, - merged_next_states) - actor_loss *= tf.to_float( # Only update actor every N steps. - tf.equal(n_updates % target_update_period, 0)) - - critic_train_op = slim.learning.create_train_op( - critic_loss, - critic_opt, - global_step=n_updates, - update_ops=None, - summarize_gradients=summarize_gradients, - clip_gradient_norm=clip_gradient_norm, - variables_to_train=agent.get_trainable_critic_vars(),) - critic_train_op = uvf_utils.tf_print( - critic_train_op, [critic_train_op], - message='critic_loss', - print_freq=1000, - name='critic_loss') - train_op_list.append(critic_train_op) - if actor_loss is not None: - actor_train_op = slim.learning.create_train_op( - actor_loss, - actor_opt, - global_step=None, - update_ops=None, - summarize_gradients=summarize_gradients, - clip_gradient_norm=clip_gradient_norm, - variables_to_train=agent.get_trainable_actor_vars(),) - actor_train_op = uvf_utils.tf_print( - actor_train_op, [actor_train_op], - message='actor_loss', - print_freq=1000, - name='actor_loss') - train_op_list.append(actor_train_op) - - assert len(train_op_list) == 4 - # Update targets should happen after the networks have been updated. - with tf.control_dependencies(train_op_list[2:]): - update_targets_op = uvf_utils.periodically( - uvf_agent.update_targets, target_update_period, 'update_targets') - if meta_agent is not None: - with tf.control_dependencies(train_op_list[:2]): - update_meta_targets_op = uvf_utils.periodically( - meta_agent.update_targets, target_update_period, 'update_targets') - - assert_op = tf.Assert( # Hack to get training to stop. - tf.less_equal(global_step, 200 + num_episodes_train * max_steps_per_episode), - [global_step]) - with tf.control_dependencies([update_targets_op, assert_op]): - train_op = tf.add_n(train_op_list[2:], name='post_update_targets') - # Representation training steps on every low-level policy training step. - train_op += repr_train_op - with tf.control_dependencies([update_meta_targets_op, assert_op]): - meta_train_op = tf.add_n(train_op_list[:2], - name='post_update_meta_targets') - - if debug_summaries: - train_.gen_debug_batch_summaries(batch) - slim.summaries.add_histogram_summaries( - uvf_agent.get_trainable_critic_vars(), 'critic_vars') - slim.summaries.add_histogram_summaries( - uvf_agent.get_trainable_actor_vars(), 'actor_vars') - - train_ops = train_utils.TrainOps(train_op, meta_train_op, - collect_experience_op) - - policy_save_path = os.path.join(train_dir, policy_save_dir, 'model.ckpt') - policy_vars = uvf_agent.get_actor_vars() + meta_agent.get_actor_vars() + [ - global_step, num_episodes, num_resets - ] + list(uvf_agent.context_vars) + list(meta_agent.context_vars) + state_preprocess.get_trainable_vars() - # add critic vars, since some test evaluation depends on them - policy_vars += uvf_agent.get_trainable_critic_vars() + meta_agent.get_trainable_critic_vars() - policy_saver = tf.train.Saver( - policy_vars, max_to_keep=max_policies_to_save, sharded=False) - - lowlevel_vars = (uvf_agent.get_actor_vars() + - uvf_agent.get_trainable_critic_vars() + - state_preprocess.get_trainable_vars()) - lowlevel_saver = tf.train.Saver(lowlevel_vars) - - def policy_save_fn(sess): - policy_saver.save( - sess, policy_save_path, global_step=global_step, write_meta_graph=False) - if save_policy_interval_secs > 0: - tf.logging.info( - 'Wait %d secs after save policy.' % save_policy_interval_secs) - time.sleep(save_policy_interval_secs) - - train_step_fn = train_utils.TrainStep( - max_number_of_steps=num_episodes_train * max_steps_per_episode + 100, - num_updates_per_observation=num_updates_per_observation, - num_collect_per_update=num_collect_per_update, - num_collect_per_meta_update=num_collect_per_meta_update, - log_every_n_steps=log_every_n_steps, - policy_save_fn=policy_save_fn, - save_policy_every_n_steps=save_policy_every_n_steps, - should_stop_early=should_stop_early).train_step - - local_init_op = tf.local_variables_initializer() - init_targets_op = tf.group(uvf_agent.update_targets(1.0), - meta_agent.update_targets(1.0)) - - def initialize_training_fn(sess): - """Initialize training function.""" - sess.run(local_init_op) - sess.run(init_targets_op) - if load_path: - tf.logging.info('Restoring low-level from %s' % load_path) - lowlevel_saver.restore(sess, load_path) - global_step_value = sess.run(global_step) - assert global_step_value == 0, 'Global step should be zero.' - collect_experience_call = sess.make_callable( - init_collect_experience_op) - - for _ in range(initial_steps): - collect_experience_call() - - train_saver = tf.train.Saver(max_to_keep=2, sharded=True) - tf.logging.info('train dir: %s', train_dir) - return slim.learning.train( - train_ops, - train_dir, - train_step_fn=train_step_fn, - save_interval_secs=FLAGS.save_interval_secs, - saver=train_saver, - log_every_n_steps=0, - global_step=global_step, - master="", - is_chief=(FLAGS.task == 0), - save_summaries_secs=FLAGS.save_summaries_secs, - init_fn=initialize_training_fn) diff --git a/research/efficient-hrl/train_utils.py b/research/efficient-hrl/train_utils.py deleted file mode 100644 index ae23ef9f095..00000000000 --- a/research/efficient-hrl/train_utils.py +++ /dev/null @@ -1,175 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from collections import namedtuple -import os -import time - -import tensorflow as tf - -import gin.tf - -flags = tf.app.flags - - -flags.DEFINE_multi_string('config_file', None, - 'List of paths to the config files.') -flags.DEFINE_multi_string('params', None, - 'Newline separated list of Gin parameter bindings.') - -flags.DEFINE_string('train_dir', None, - 'Directory for writing logs/summaries during training.') -flags.DEFINE_string('master', 'local', - 'BNS name of the TensorFlow master to use.') -flags.DEFINE_integer('task', 0, 'task id') -flags.DEFINE_integer('save_interval_secs', 300, 'The frequency at which ' - 'checkpoints are saved, in seconds.') -flags.DEFINE_integer('save_summaries_secs', 30, 'The frequency at which ' - 'summaries are saved, in seconds.') -flags.DEFINE_boolean('summarize_gradients', False, - 'Whether to generate gradient summaries.') - -FLAGS = flags.FLAGS - -TrainOps = namedtuple('TrainOps', - ['train_op', 'meta_train_op', 'collect_experience_op']) - - -class TrainStep(object): - """Handles training step.""" - - def __init__(self, - max_number_of_steps=0, - num_updates_per_observation=1, - num_collect_per_update=1, - num_collect_per_meta_update=1, - log_every_n_steps=1, - policy_save_fn=None, - save_policy_every_n_steps=0, - should_stop_early=None): - """Returns a function that is executed at each step of slim training. - - Args: - max_number_of_steps: Optional maximum number of train steps to take. - num_updates_per_observation: Number of updates per observation. - log_every_n_steps: The frequency, in terms of global steps, that the loss - and global step and logged. - policy_save_fn: A tf.Saver().save function to save the policy. - save_policy_every_n_steps: How frequently to save the policy. - should_stop_early: Optional hook to report whether training should stop. - Raises: - ValueError: If policy_save_fn is not provided when - save_policy_every_n_steps > 0. - """ - if save_policy_every_n_steps and policy_save_fn is None: - raise ValueError( - 'policy_save_fn is required when save_policy_every_n_steps > 0') - self.max_number_of_steps = max_number_of_steps - self.num_updates_per_observation = num_updates_per_observation - self.num_collect_per_update = num_collect_per_update - self.num_collect_per_meta_update = num_collect_per_meta_update - self.log_every_n_steps = log_every_n_steps - self.policy_save_fn = policy_save_fn - self.save_policy_every_n_steps = save_policy_every_n_steps - self.should_stop_early = should_stop_early - self.last_global_step_val = 0 - self.train_op_fn = None - self.collect_and_train_fn = None - tf.logging.info('Training for %d max_number_of_steps', - self.max_number_of_steps) - - def train_step(self, sess, train_ops, global_step, _): - """This function will be called at each step of training. - - This represents one step of the DDPG algorithm and can include: - 1. collect a transition - 2. update the target network - 3. train the actor - 4. train the critic - - Args: - sess: A Tensorflow session. - train_ops: A DdpgTrainOps tuple of train ops to run. - global_step: The global step. - - Returns: - A scalar total loss. - A boolean should stop. - """ - start_time = time.time() - if self.train_op_fn is None: - self.train_op_fn = sess.make_callable([train_ops.train_op, global_step]) - self.meta_train_op_fn = sess.make_callable([train_ops.meta_train_op, global_step]) - self.collect_fn = sess.make_callable([train_ops.collect_experience_op, global_step]) - self.collect_and_train_fn = sess.make_callable( - [train_ops.train_op, global_step, train_ops.collect_experience_op]) - self.collect_and_meta_train_fn = sess.make_callable( - [train_ops.meta_train_op, global_step, train_ops.collect_experience_op]) - for _ in range(self.num_collect_per_update - 1): - self.collect_fn() - for _ in range(self.num_updates_per_observation - 1): - self.train_op_fn() - - total_loss, global_step_val, _ = self.collect_and_train_fn() - if (global_step_val // self.num_collect_per_meta_update != - self.last_global_step_val // self.num_collect_per_meta_update): - self.meta_train_op_fn() - - time_elapsed = time.time() - start_time - should_stop = False - if self.max_number_of_steps: - should_stop = global_step_val >= self.max_number_of_steps - if global_step_val != self.last_global_step_val: - if (self.save_policy_every_n_steps and - global_step_val // self.save_policy_every_n_steps != - self.last_global_step_val // self.save_policy_every_n_steps): - self.policy_save_fn(sess) - - if (self.log_every_n_steps and - global_step_val % self.log_every_n_steps == 0): - tf.logging.info( - 'global step %d: loss = %.4f (%.3f sec/step) (%d steps/sec)', - global_step_val, total_loss, time_elapsed, 1 / time_elapsed) - - self.last_global_step_val = global_step_val - stop_early = bool(self.should_stop_early and self.should_stop_early()) - return total_loss, should_stop or stop_early - - -def create_counter_summaries(counters): - """Add named summaries to counters, a list of tuples (name, counter).""" - if counters: - with tf.name_scope('Counters/'): - for name, counter in counters: - tf.summary.scalar(name, counter) - - -def gen_debug_batch_summaries(batch): - """Generates summaries for the sampled replay batch.""" - states, actions, rewards, _, next_states = batch - with tf.name_scope('batch'): - for s in range(states.get_shape()[-1]): - tf.summary.histogram('states_%d' % s, states[:, s]) - for s in range(states.get_shape()[-1]): - tf.summary.histogram('next_states_%d' % s, next_states[:, s]) - for a in range(actions.get_shape()[-1]): - tf.summary.histogram('actions_%d' % a, actions[:, a]) - tf.summary.histogram('rewards', rewards) diff --git a/research/efficient-hrl/utils/__init__.py b/research/efficient-hrl/utils/__init__.py deleted file mode 100644 index 8b137891791..00000000000 --- a/research/efficient-hrl/utils/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/research/efficient-hrl/utils/eval_utils.py b/research/efficient-hrl/utils/eval_utils.py deleted file mode 100644 index c88efc80fe1..00000000000 --- a/research/efficient-hrl/utils/eval_utils.py +++ /dev/null @@ -1,151 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Evaluation utility functions. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import numpy as np -import tensorflow as tf -from collections import namedtuple -logging = tf.logging -import gin.tf - - -@gin.configurable -def evaluate_checkpoint_repeatedly(checkpoint_dir, - evaluate_checkpoint_fn, - eval_interval_secs=600, - max_number_of_evaluations=None, - checkpoint_timeout=None, - timeout_fn=None): - """Evaluates a checkpointed model at a set interval.""" - if max_number_of_evaluations is not None and max_number_of_evaluations <= 0: - raise ValueError( - '`max_number_of_evaluations` must be either None or a positive number.') - - number_of_evaluations = 0 - for checkpoint_path in tf.contrib.training.checkpoints_iterator( - checkpoint_dir, - min_interval_secs=eval_interval_secs, - timeout=checkpoint_timeout, - timeout_fn=timeout_fn): - retries = 3 - for _ in range(retries): - try: - should_stop = evaluate_checkpoint_fn(checkpoint_path) - break - except tf.errors.DataLossError as e: - logging.warn( - 'Encountered a DataLossError while evaluating a checkpoint. This ' - 'can happen when reading a checkpoint before it is fully written. ' - 'Retrying...' - ) - time.sleep(2.0) - - -def compute_model_loss(sess, model_rollout_fn, states, actions): - """Computes model loss.""" - preds, losses = [], [] - preds.append(states[0]) - losses.append(0) - for state, action in zip(states[1:], actions[1:]): - pred = model_rollout_fn(sess, preds[-1], action) - loss = np.sqrt(np.sum((state - pred) ** 2)) - preds.append(pred) - losses.append(loss) - return preds, losses - - -def compute_average_reward(sess, env_base, step_fn, gamma, num_steps, - num_episodes): - """Computes the discounted reward for a given number of steps. - - Args: - sess: The tensorflow session. - env_base: A python environment. - step_fn: A function that takes in `sess` and returns a list of - [state, action, reward, discount, transition_type] values. - gamma: discounting factor to apply to the reward. - num_steps: number of steps to compute the reward over. - num_episodes: number of episodes to average the reward over. - Returns: - average_reward: a scalar of discounted reward. - last_reward: last reward received. - """ - average_reward = 0 - average_last_reward = 0 - average_meta_reward = 0 - average_last_meta_reward = 0 - average_success = 0. - states, actions = None, None - for i in range(num_episodes): - env_base.end_episode() - env_base.begin_episode() - (reward, last_reward, meta_reward, last_meta_reward, - states, actions) = compute_reward( - sess, step_fn, gamma, num_steps) - s_reward = last_meta_reward # Navigation - success = (s_reward > -5.0) # When using diff=False - logging.info('Episode = %d, reward = %s, meta_reward = %f, ' - 'last_reward = %s, last meta_reward = %f, success = %s', - i, reward, meta_reward, last_reward, last_meta_reward, - success) - average_reward += reward - average_last_reward += last_reward - average_meta_reward += meta_reward - average_last_meta_reward += last_meta_reward - average_success += success - average_reward /= num_episodes - average_last_reward /= num_episodes - average_meta_reward /= num_episodes - average_last_meta_reward /= num_episodes - average_success /= num_episodes - return (average_reward, average_last_reward, - average_meta_reward, average_last_meta_reward, - average_success, - states, actions) - - -def compute_reward(sess, step_fn, gamma, num_steps): - """Computes the discounted reward for a given number of steps. - - Args: - sess: The tensorflow session. - step_fn: A function that takes in `sess` and returns a list of - [state, action, reward, discount, transition_type] values. - gamma: discounting factor to apply to the reward. - num_steps: number of steps to compute the reward over. - Returns: - reward: cumulative discounted reward. - last_reward: reward received at final step. - """ - - total_reward = 0 - total_meta_reward = 0 - gamma_step = 1 - states = [] - actions = [] - for _ in range(num_steps): - state, action, transition_type, reward, meta_reward, discount, _, _ = step_fn(sess) - total_reward += reward * gamma_step * discount - total_meta_reward += meta_reward * gamma_step * discount - gamma_step *= gamma - states.append(state) - actions.append(action) - return (total_reward, reward, total_meta_reward, meta_reward, - states, actions) diff --git a/research/efficient-hrl/utils/utils.py b/research/efficient-hrl/utils/utils.py deleted file mode 100644 index e188316c33b..00000000000 --- a/research/efficient-hrl/utils/utils.py +++ /dev/null @@ -1,318 +0,0 @@ -# Copyright 2018 The TensorFlow Authors All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""TensorFlow utility functions. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from copy import deepcopy -import tensorflow as tf -from tf_agents import specs -from tf_agents.utils import common - -_tf_print_counts = dict() -_tf_print_running_sums = dict() -_tf_print_running_counts = dict() -_tf_print_ids = 0 - - -def get_contextual_env_base(env_base, begin_ops=None, end_ops=None): - """Wrap env_base with additional tf ops.""" - # pylint: disable=protected-access - def init(self_, env_base): - self_._env_base = env_base - attribute_list = ["_render_mode", "_gym_env"] - for attribute in attribute_list: - if hasattr(env_base, attribute): - setattr(self_, attribute, getattr(env_base, attribute)) - if hasattr(env_base, "physics"): - self_._physics = env_base.physics - elif hasattr(env_base, "gym"): - class Physics(object): - def render(self, *args, **kwargs): - return env_base.gym.render("rgb_array") - physics = Physics() - self_._physics = physics - self_.physics = physics - def set_sess(self_, sess): - self_._sess = sess - if hasattr(self_._env_base, "set_sess"): - self_._env_base.set_sess(sess) - def begin_episode(self_): - self_._env_base.reset() - if begin_ops is not None: - self_._sess.run(begin_ops) - def end_episode(self_): - self_._env_base.reset() - if end_ops is not None: - self_._sess.run(end_ops) - return type("ContextualEnvBase", (env_base.__class__,), dict( - __init__=init, - set_sess=set_sess, - begin_episode=begin_episode, - end_episode=end_episode, - ))(env_base) - # pylint: enable=protected-access - - -def merge_specs(specs_): - """Merge TensorSpecs. - - Args: - specs_: List of TensorSpecs to be merged. - Returns: - a TensorSpec: a merged TensorSpec. - """ - shape = specs_[0].shape - dtype = specs_[0].dtype - name = specs_[0].name - for spec in specs_[1:]: - assert shape[1:] == spec.shape[1:], "incompatible shapes: %s, %s" % ( - shape, spec.shape) - assert dtype == spec.dtype, "incompatible dtypes: %s, %s" % ( - dtype, spec.dtype) - shape = merge_shapes((shape, spec.shape), axis=0) - return specs.TensorSpec( - shape=shape, - dtype=dtype, - name=name, - ) - - -def merge_shapes(shapes, axis=0): - """Merge TensorShapes. - - Args: - shapes: List of TensorShapes to be merged. - axis: optional, the axis to merge shaped. - Returns: - a TensorShape: a merged TensorShape. - """ - assert len(shapes) > 1 - dims = deepcopy(shapes[0].dims) - for shape in shapes[1:]: - assert shapes[0].ndims == shape.ndims - dims[axis] += shape.dims[axis] - return tf.TensorShape(dims=dims) - - -def get_all_vars(ignore_scopes=None): - """Get all tf variables in scope. - - Args: - ignore_scopes: A list of scope names to ignore. - Returns: - A list of all tf variables in scope. - """ - all_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - all_vars = [var for var in all_vars if ignore_scopes is None or not - any(var.name.startswith(scope) for scope in ignore_scopes)] - return all_vars - - -def clip(tensor, range_=None): - """Return a tf op which clips tensor according to range_. - - Args: - tensor: A Tensor to be clipped. - range_: None, or a tuple representing (minval, maxval) - Returns: - A clipped Tensor. - """ - if range_ is None: - return tf.identity(tensor) - elif isinstance(range_, (tuple, list)): - assert len(range_) == 2 - return tf.clip_by_value(tensor, range_[0], range_[1]) - else: raise NotImplementedError("Unacceptable range input: %r" % range_) - - -def clip_to_bounds(value, minimum, maximum): - """Clips value to be between minimum and maximum. - - Args: - value: (tensor) value to be clipped. - minimum: (numpy float array) minimum value to clip to. - maximum: (numpy float array) maximum value to clip to. - Returns: - clipped_value: (tensor) `value` clipped to between `minimum` and `maximum`. - """ - value = tf.minimum(value, maximum) - return tf.maximum(value, minimum) - - -clip_to_spec = common.clip_to_spec -def _clip_to_spec(value, spec): - """Clips value to a given bounded tensor spec. - - Args: - value: (tensor) value to be clipped. - spec: (BoundedTensorSpec) spec containing min. and max. values for clipping. - Returns: - clipped_value: (tensor) `value` clipped to be compatible with `spec`. - """ - return clip_to_bounds(value, spec.minimum, spec.maximum) - - -join_scope = common.join_scope -def _join_scope(parent_scope, child_scope): - """Joins a parent and child scope using `/`, checking for empty/none. - - Args: - parent_scope: (string) parent/prefix scope. - child_scope: (string) child/suffix scope. - Returns: - joined scope: (string) parent and child scopes joined by /. - """ - if not parent_scope: - return child_scope - if not child_scope: - return parent_scope - return '/'.join([parent_scope, child_scope]) - - -def assign_vars(vars_, values): - """Returns the update ops for assigning a list of vars. - - Args: - vars_: A list of variables. - values: A list of tensors representing new values. - Returns: - A list of update ops for the variables. - """ - return [var.assign(value) for var, value in zip(vars_, values)] - - -def identity_vars(vars_): - """Return the identity ops for a list of tensors. - - Args: - vars_: A list of tensors. - Returns: - A list of identity ops. - """ - return [tf.identity(var) for var in vars_] - - -def tile(var, batch_size=1): - """Return tiled tensor. - - Args: - var: A tensor representing the state. - batch_size: Batch size. - Returns: - A tensor with shape [batch_size,] + var.shape. - """ - batch_var = tf.tile( - tf.expand_dims(var, 0), - (batch_size,) + (1,) * var.get_shape().ndims) - return batch_var - - -def batch_list(vars_list): - """Batch a list of variables. - - Args: - vars_list: A list of tensor variables. - Returns: - A list of tensor variables with additional first dimension. - """ - return [tf.expand_dims(var, 0) for var in vars_list] - - -def tf_print(op, - tensors, - message="", - first_n=-1, - name=None, - sub_messages=None, - print_freq=-1, - include_count=True): - """tf.Print, but to stdout.""" - # TODO(shanegu): `name` is deprecated. Remove from the rest of codes. - global _tf_print_ids - _tf_print_ids += 1 - name = _tf_print_ids - _tf_print_counts[name] = 0 - if print_freq > 0: - _tf_print_running_sums[name] = [0 for _ in tensors] - _tf_print_running_counts[name] = 0 - def print_message(*xs): - """print message fn.""" - _tf_print_counts[name] += 1 - if print_freq > 0: - for i, x in enumerate(xs): - _tf_print_running_sums[name][i] += x - _tf_print_running_counts[name] += 1 - if (print_freq <= 0 or _tf_print_running_counts[name] >= print_freq) and ( - first_n < 0 or _tf_print_counts[name] <= first_n): - for i, x in enumerate(xs): - if print_freq > 0: - del x - x = _tf_print_running_sums[name][i]/_tf_print_running_counts[name] - if sub_messages is None: - sub_message = str(i) - else: - sub_message = sub_messages[i] - log_message = "%s, %s" % (message, sub_message) - if include_count: - log_message += ", count=%d" % _tf_print_counts[name] - tf.logging.info("[%s]: %s" % (log_message, x)) - if print_freq > 0: - for i, x in enumerate(xs): - _tf_print_running_sums[name][i] = 0 - _tf_print_running_counts[name] = 0 - return xs[0] - - print_op = tf.py_func(print_message, tensors, tensors[0].dtype) - with tf.control_dependencies([print_op]): - op = tf.identity(op) - return op - - -periodically = common.periodically -def _periodically(body, period, name='periodically'): - """Periodically performs a tensorflow op.""" - if period is None or period == 0: - return tf.no_op() - - if period < 0: - raise ValueError("period cannot be less than 0.") - - if period == 1: - return body() - - with tf.variable_scope(None, default_name=name): - counter = tf.get_variable( - "counter", - shape=[], - dtype=tf.int64, - trainable=False, - initializer=tf.constant_initializer(period, dtype=tf.int64)) - - def _wrapped_body(): - with tf.control_dependencies([body()]): - return counter.assign(1) - - update = tf.cond( - tf.equal(counter, period), _wrapped_body, - lambda: counter.assign_add(1)) - - return update - -soft_variables_update = common.soft_variables_update diff --git a/research/lfads/README.md b/research/lfads/README.md deleted file mode 100644 index c75b656e474..00000000000 --- a/research/lfads/README.md +++ /dev/null @@ -1,224 +0,0 @@ -![TensorFlow Requirement: 1.x](https://img.shields.io/badge/TensorFlow%20Requirement-1.x-brightgreen) -![TensorFlow 2 Not Supported](https://img.shields.io/badge/TensorFlow%202%20Not%20Supported-%E2%9C%95-red.svg) - -# LFADS - Latent Factor Analysis via Dynamical Systems - -This code implements the model from the paper "[LFADS - Latent Factor Analysis via Dynamical Systems](http://biorxiv.org/content/early/2017/06/20/152884)". It is a sequential variational auto-encoder designed specifically for investigating neuroscience data, but can be applied widely to any time series data. In an unsupervised setting, LFADS is able to decompose time series data into various factors, such as an initial condition, a generative dynamical system, control inputs to that generator, and a low dimensional description of the observed data, called the factors. Additionally, the observation model is a loss on a probability distribution, so when LFADS processes a dataset, a denoised version of the dataset is also created. For example, if the dataset is raw spike counts, then under the negative log-likelihood loss under a Poisson distribution, the denoised data would be the inferred Poisson rates. - - -## Prerequisites - -The code is written in Python 2.7.6. You will also need: - -* **TensorFlow** version 1.5 ([install](https://www.tensorflow.org/install/)) - -* **NumPy, SciPy, Matplotlib** ([install SciPy stack](https://www.scipy.org/install.html), contains all of them) -* **h5py** ([install](https://pypi.python.org/pypi/h5py)) - - -## Getting started - -Before starting, run the following: - -
-$ export PYTHONPATH=$PYTHONPATH:/path/to/your/directory/lfads/
-
- -where "path/to/your/directory" is replaced with the path to the LFADS repository (you can get this path by using the `pwd` command). This allows the nested directories to access modules from their parent directory. - -## Generate synthetic data - -In order to generate the synthetic datasets first, from the top-level lfads directory, run: - -```sh -$ cd synth_data -$ ./run_generate_synth_data.sh -$ cd .. -``` - -These synthetic datasets are provided 1. to gain insight into how the LFADS algorithm operates, and 2. to give reasonable starting points for analyses you might be interested for your own data. - -## Train an LFADS model - -Now that we have our example datasets, we can train some models! To spin up an LFADS model on the synthetic data, run any of the following commands. For the examples that are in the paper, the important hyperparameters are roughly replicated. Most hyperparameters are insensitive to small changes or won't ever be changed unless you want a very fine level of control. In the first example, all hyperparameter flags are enumerated for easy copy-pasting, but for the rest of the examples only the most important flags (~the first 9) are specified for brevity. For a full list of flags, their descriptions, and their default values, refer to the top of `run_lfads.py`. Please see Table 1 in the Online Methods of the associated paper for definitions of the most important hyperparameters. - -```sh -# Run LFADS on chaotic rnn data with no input pulses (g = 1.5) with spiking noise -$ python run_lfads.py --kind=train \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnn_no_inputs \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_no_inputs \ ---co_dim=0 \ ---factors_dim=20 \ ---ext_input_dim=0 \ ---controller_input_lag=1 \ ---output_dist=poisson \ ---do_causal_controller=false \ ---batch_size=128 \ ---learning_rate_init=0.01 \ ---learning_rate_stop=1e-05 \ ---learning_rate_decay_factor=0.95 \ ---learning_rate_n_to_compare=6 \ ---do_reset_learning_rate=false \ ---keep_prob=0.95 \ ---con_dim=128 \ ---gen_dim=200 \ ---ci_enc_dim=128 \ ---ic_dim=64 \ ---ic_enc_dim=128 \ ---ic_prior_var_min=0.1 \ ---gen_cell_input_weight_scale=1.0 \ ---cell_weight_scale=1.0 \ ---do_feed_factors_to_controller=true \ ---kl_start_step=0 \ ---kl_increase_steps=2000 \ ---kl_ic_weight=1.0 \ ---l2_con_scale=0.0 \ ---l2_gen_scale=2000.0 \ ---l2_start_step=0 \ ---l2_increase_steps=2000 \ ---ic_prior_var_scale=0.1 \ ---ic_post_var_min=0.0001 \ ---kl_co_weight=1.0 \ ---prior_ar_nvar=0.1 \ ---cell_clip_value=5.0 \ ---max_ckpt_to_keep_lve=5 \ ---do_train_prior_ar_atau=true \ ---co_prior_var_scale=0.1 \ ---csv_log=fitlog \ ---feedback_factors_or_rates=factors \ ---do_train_prior_ar_nvar=true \ ---max_grad_norm=200.0 \ ---device=gpu:0 \ ---num_steps_for_gen_ic=100000000 \ ---ps_nexamples_to_process=100000000 \ ---checkpoint_name=lfads_vae \ ---temporal_spike_jitter_width=0 \ ---checkpoint_pb_load_name=checkpoint \ ---inject_ext_input_to_gen=false \ ---co_mean_corr_scale=0.0 \ ---gen_cell_rec_weight_scale=1.0 \ ---max_ckpt_to_keep=5 \ ---output_filename_stem="" \ ---ic_prior_var_max=0.1 \ ---prior_ar_atau=10.0 \ ---do_train_io_only=false \ ---do_train_encoder_only=false - -# Run LFADS on chaotic rnn data with no input pulses (g = 1.5) with Gaussian noise -$ python run_lfads.py --kind=train \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=gaussian_chaotic_rnn_no_inputs \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_inputs_g2p5 \ ---co_dim=1 \ ---factors_dim=20 \ ---output_dist=gaussian - - -# Run LFADS on chaotic rnn data with input pulses (g = 2.5) -$ python run_lfads.py --kind=train \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnn_inputs_g2p5 \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_inputs_g2p5 \ ---co_dim=1 \ ---factors_dim=20 \ ---output_dist=poisson - -# Run LFADS on multi-session RNN data -$ python run_lfads.py --kind=train \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnn_multisession \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_multisession \ ---factors_dim=10 \ ---output_dist=poisson - -# Run LFADS on integration to bound model data -$ python run_lfads.py --kind=train \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=itb_rnn \ ---lfads_save_dir=/tmp/lfads_itb_rnn \ ---co_dim=1 \ ---factors_dim=20 \ ---controller_input_lag=0 \ ---output_dist=poisson - -# Run LFADS on chaotic RNN data with labels -$ python run_lfads.py --kind=train \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnns_labeled \ ---lfads_save_dir=/tmp/lfads_chaotic_rnns_labeled \ ---co_dim=0 \ ---factors_dim=20 \ ---controller_input_lag=0 \ ---ext_input_dim=1 \ ---output_dist=poisson - -# Run LFADS on chaotic rnn data with no input pulses (g = 1.5) with Gaussian noise -$ python run_lfads.py --kind=train \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnn_no_inputs \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_no_inputs \ ---co_dim=0 \ ---factors_dim=20 \ ---ext_input_dim=0 \ ---controller_input_lag=1 \ ---output_dist=gaussian \ - - -``` - -**Tip**: If you are running LFADS on GPU and would like to run more than one model concurrently, set the `--allow_gpu_growth=True` flag on each job, otherwise one model will take up the entire GPU for performance purposes. Also, one needs to install the TensorFlow libraries with GPU support. - - -## Visualize a training model - -To visualize training curves and various other metrics while training and LFADS model, run the following command on your model directory. To launch a tensorboard on the chaotic RNN data with input pulses, for example: - -```sh -tensorboard --logdir=/tmp/lfads_chaotic_rnn_inputs_g2p5 -``` - -## Evaluate a trained model - -Once your model is finished training, there are multiple ways you can evaluate -it. Below are some sample commands to evaluate an LFADS model trained on the -chaotic rnn data with input pulses (g = 2.5). The key differences here are -setting the `--kind` flag to the appropriate mode, as well as the -`--checkpoint_pb_load_name` flag to `checkpoint_lve` and the `--batch_size` flag -(if you'd like to make it larger or smaller). All other flags should be the -same as used in training, so that the same model architecture is built. - -```sh -# Take samples from posterior then average (denoising operation) -$ python run_lfads.py --kind=posterior_sample_and_average \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnn_inputs_g2p5 \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_inputs_g2p5 \ ---co_dim=1 \ ---factors_dim=20 \ ---batch_size=1024 \ ---checkpoint_pb_load_name=checkpoint_lve - -# Sample from prior (generation of completely new samples) -$ python run_lfads.py --kind=prior_sample \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnn_inputs_g2p5 \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_inputs_g2p5 \ ---co_dim=1 \ ---factors_dim=20 \ ---batch_size=50 \ ---checkpoint_pb_load_name=checkpoint_lve - -# Write down model parameters -$ python run_lfads.py --kind=write_model_params \ ---data_dir=/tmp/rnn_synth_data_v1.0/ \ ---data_filename_stem=chaotic_rnn_inputs_g2p5 \ ---lfads_save_dir=/tmp/lfads_chaotic_rnn_inputs_g2p5 \ ---co_dim=1 \ ---factors_dim=20 \ ---checkpoint_pb_load_name=checkpoint_lve -``` - -## Contact - -File any issues with the [issue tracker](https://github.com/tensorflow/models/issues). For any questions or problems, this code is maintained by [@sussillo](https://github.com/sussillo) and [@jazcollins](https://github.com/jazcollins). - diff --git a/research/lfads/distributions.py b/research/lfads/distributions.py deleted file mode 100644 index 351d019af2b..00000000000 --- a/research/lfads/distributions.py +++ /dev/null @@ -1,493 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -import numpy as np -import tensorflow as tf -from utils import linear, log_sum_exp - -class Poisson(object): - """Poisson distributon - - Computes the log probability under the model. - - """ - def __init__(self, log_rates): - """ Create Poisson distributions with log_rates parameters. - - Args: - log_rates: a tensor-like list of log rates underlying the Poisson dist. - """ - self.logr = log_rates - - def logp(self, bin_counts): - """Compute the log probability for the counts in the bin, under the model. - - Args: - bin_counts: array-like integer counts - - Returns: - The log-probability under the Poisson models for each element of - bin_counts. - """ - k = tf.to_float(bin_counts) - # log poisson(k, r) = log(r^k * e^(-r) / k!) = k log(r) - r - log k! - # log poisson(k, r=exp(x)) = k * x - exp(x) - lgamma(k + 1) - return k * self.logr - tf.exp(self.logr) - tf.lgamma(k + 1) - - -def diag_gaussian_log_likelihood(z, mu=0.0, logvar=0.0): - """Log-likelihood under a Gaussian distribution with diagonal covariance. - Returns the log-likelihood for each dimension. One should sum the - results for the log-likelihood under the full multidimensional model. - - Args: - z: The value to compute the log-likelihood. - mu: The mean of the Gaussian - logvar: The log variance of the Gaussian. - - Returns: - The log-likelihood under the Gaussian model. - """ - - return -0.5 * (logvar + np.log(2*np.pi) + \ - tf.square((z-mu)/tf.exp(0.5*logvar))) - - -def gaussian_pos_log_likelihood(unused_mean, logvar, noise): - """Gaussian log-likelihood function for a posterior in VAE - - Note: This function is specialized for a posterior distribution, that has the - form of z = mean + sigma * noise. - - Args: - unused_mean: ignore - logvar: The log variance of the distribution - noise: The noise used in the sampling of the posterior. - - Returns: - The log-likelihood under the Gaussian model. - """ - # ln N(z; mean, sigma) = - ln(sigma) - 0.5 ln 2pi - noise^2 / 2 - return - 0.5 * (logvar + np.log(2 * np.pi) + tf.square(noise)) - - -class Gaussian(object): - """Base class for Gaussian distribution classes.""" - pass - - -class DiagonalGaussian(Gaussian): - """Diagonal Gaussian with different constant mean and variances in each - dimension. - """ - - def __init__(self, batch_size, z_size, mean, logvar): - """Create a diagonal gaussian distribution. - - Args: - batch_size: The size of the batch, i.e. 0th dim in 2D tensor of samples. - z_size: The dimension of the distribution, i.e. 1st dim in 2D tensor. - mean: The N-D mean of the distribution. - logvar: The N-D log variance of the diagonal distribution. - """ - size__xz = [None, z_size] - self.mean = mean # bxn already - self.logvar = logvar # bxn already - self.noise = noise = tf.random_normal(tf.shape(logvar)) - self.sample = mean + tf.exp(0.5 * logvar) * noise - mean.set_shape(size__xz) - logvar.set_shape(size__xz) - self.sample.set_shape(size__xz) - - def logp(self, z=None): - """Compute the log-likelihood under the distribution. - - Args: - z (optional): value to compute likelihood for, if None, use sample. - - Returns: - The likelihood of z under the model. - """ - if z is None: - z = self.sample - - # This is needed to make sure that the gradients are simple. - # The value of the function shouldn't change. - if z == self.sample: - return gaussian_pos_log_likelihood(self.mean, self.logvar, self.noise) - - return diag_gaussian_log_likelihood(z, self.mean, self.logvar) - - -class LearnableDiagonalGaussian(Gaussian): - """Diagonal Gaussian whose mean and variance are learned parameters.""" - - def __init__(self, batch_size, z_size, name, mean_init=0.0, - var_init=1.0, var_min=0.0, var_max=1000000.0): - """Create a learnable diagonal gaussian distribution. - - Args: - batch_size: The size of the batch, i.e. 0th dim in 2D tensor of samples. - z_size: The dimension of the distribution, i.e. 1st dim in 2D tensor. - name: prefix name for the mean and log TF variables. - mean_init (optional): The N-D mean initialization of the distribution. - var_init (optional): The N-D variance initialization of the diagonal - distribution. - var_min (optional): The minimum value the learned variance can take in any - dimension. - var_max (optional): The maximum value the learned variance can take in any - dimension. - """ - - size_1xn = [1, z_size] - size__xn = [None, z_size] - size_bx1 = tf.stack([batch_size, 1]) - assert var_init > 0.0, "Problems" - assert var_max >= var_min, "Problems" - assert var_init >= var_min, "Problems" - assert var_max >= var_init, "Problems" - - - z_mean_1xn = tf.get_variable(name=name+"/mean", shape=size_1xn, - initializer=tf.constant_initializer(mean_init)) - self.mean_bxn = mean_bxn = tf.tile(z_mean_1xn, size_bx1) - mean_bxn.set_shape(size__xn) # tile loses shape - - log_var_init = np.log(var_init) - if var_max > var_min: - var_is_trainable = True - else: - var_is_trainable = False - - z_logvar_1xn = \ - tf.get_variable(name=(name+"/logvar"), shape=size_1xn, - initializer=tf.constant_initializer(log_var_init), - trainable=var_is_trainable) - - if var_is_trainable: - z_logit_var_1xn = tf.exp(z_logvar_1xn) - z_var_1xn = tf.nn.sigmoid(z_logit_var_1xn)*(var_max-var_min) + var_min - z_logvar_1xn = tf.log(z_var_1xn) - - logvar_bxn = tf.tile(z_logvar_1xn, size_bx1) - self.logvar_bxn = logvar_bxn - self.noise_bxn = noise_bxn = tf.random_normal(tf.shape(logvar_bxn)) - self.sample_bxn = mean_bxn + tf.exp(0.5 * logvar_bxn) * noise_bxn - - def logp(self, z=None): - """Compute the log-likelihood under the distribution. - - Args: - z (optional): value to compute likelihood for, if None, use sample. - - Returns: - The likelihood of z under the model. - """ - if z is None: - z = self.sample - - # This is needed to make sure that the gradients are simple. - # The value of the function shouldn't change. - if z == self.sample_bxn: - return gaussian_pos_log_likelihood(self.mean_bxn, self.logvar_bxn, - self.noise_bxn) - - return diag_gaussian_log_likelihood(z, self.mean_bxn, self.logvar_bxn) - - @property - def mean(self): - return self.mean_bxn - - @property - def logvar(self): - return self.logvar_bxn - - @property - def sample(self): - return self.sample_bxn - - -class DiagonalGaussianFromInput(Gaussian): - """Diagonal Gaussian whose mean and variance are conditioned on other - variables. - - Note: the parameters to convert from input to the learned mean and log - variance are held in this class. - """ - - def __init__(self, x_bxu, z_size, name, var_min=0.0): - """Create an input dependent diagonal Gaussian distribution. - - Args: - x: The input tensor from which the mean and variance are computed, - via a linear transformation of x. I.e. - mu = Wx + b, log(var) = Mx + c - z_size: The size of the distribution. - name: The name to prefix to learned variables. - var_min (optional): Minimal variance allowed. This is an additional - way to control the amount of information getting through the stochastic - layer. - """ - size_bxn = tf.stack([tf.shape(x_bxu)[0], z_size]) - self.mean_bxn = mean_bxn = linear(x_bxu, z_size, name=(name+"/mean")) - logvar_bxn = linear(x_bxu, z_size, name=(name+"/logvar")) - if var_min > 0.0: - logvar_bxn = tf.log(tf.exp(logvar_bxn) + var_min) - self.logvar_bxn = logvar_bxn - - self.noise_bxn = noise_bxn = tf.random_normal(size_bxn) - self.noise_bxn.set_shape([None, z_size]) - self.sample_bxn = mean_bxn + tf.exp(0.5 * logvar_bxn) * noise_bxn - - def logp(self, z=None): - """Compute the log-likelihood under the distribution. - - Args: - z (optional): value to compute likelihood for, if None, use sample. - - Returns: - The likelihood of z under the model. - """ - - if z is None: - z = self.sample - - # This is needed to make sure that the gradients are simple. - # The value of the function shouldn't change. - if z == self.sample_bxn: - return gaussian_pos_log_likelihood(self.mean_bxn, - self.logvar_bxn, self.noise_bxn) - - return diag_gaussian_log_likelihood(z, self.mean_bxn, self.logvar_bxn) - - @property - def mean(self): - return self.mean_bxn - - @property - def logvar(self): - return self.logvar_bxn - - @property - def sample(self): - return self.sample_bxn - - -class GaussianProcess: - """Base class for Gaussian processes.""" - pass - - -class LearnableAutoRegressive1Prior(GaussianProcess): - """AR(1) model where autocorrelation and process variance are learned - parameters. Assumed zero mean. - - """ - - def __init__(self, batch_size, z_size, - autocorrelation_taus, noise_variances, - do_train_prior_ar_atau, do_train_prior_ar_nvar, - num_steps, name): - """Create a learnable autoregressive (1) process. - - Args: - batch_size: The size of the batch, i.e. 0th dim in 2D tensor of samples. - z_size: The dimension of the distribution, i.e. 1st dim in 2D tensor. - autocorrelation_taus: The auto correlation time constant of the AR(1) - process. - A value of 0 is uncorrelated gaussian noise. - noise_variances: The variance of the additive noise, *not* the process - variance. - do_train_prior_ar_atau: Train or leave as constant, the autocorrelation? - do_train_prior_ar_nvar: Train or leave as constant, the noise variance? - num_steps: Number of steps to run the process. - name: The name to prefix to learned TF variables. - """ - - # Note the use of the plural in all of these quantities. This is intended - # to mark that even though a sample z_t from the posterior is thought of a - # single sample of a multidimensional gaussian, the prior is actually - # thought of as U AR(1) processes, where U is the dimension of the inferred - # input. - size_bx1 = tf.stack([batch_size, 1]) - size__xu = [None, z_size] - # process variance, the variance at time t over all instantiations of AR(1) - # with these parameters. - log_evar_inits_1xu = tf.expand_dims(tf.log(noise_variances), 0) - self.logevars_1xu = logevars_1xu = \ - tf.Variable(log_evar_inits_1xu, name=name+"/logevars", dtype=tf.float32, - trainable=do_train_prior_ar_nvar) - self.logevars_bxu = logevars_bxu = tf.tile(logevars_1xu, size_bx1) - logevars_bxu.set_shape(size__xu) # tile loses shape - - # \tau, which is the autocorrelation time constant of the AR(1) process - log_atau_inits_1xu = tf.expand_dims(tf.log(autocorrelation_taus), 0) - self.logataus_1xu = logataus_1xu = \ - tf.Variable(log_atau_inits_1xu, name=name+"/logatau", dtype=tf.float32, - trainable=do_train_prior_ar_atau) - - # phi in x_t = \mu + phi x_tm1 + \eps - # phi = exp(-1/tau) - # phi = exp(-1/exp(logtau)) - # phi = exp(-exp(-logtau)) - phis_1xu = tf.exp(-tf.exp(-logataus_1xu)) - self.phis_bxu = phis_bxu = tf.tile(phis_1xu, size_bx1) - phis_bxu.set_shape(size__xu) - - # process noise - # pvar = evar / (1- phi^2) - # logpvar = log ( exp(logevar) / (1 - phi^2) ) - # logpvar = logevar - log(1-phi^2) - # logpvar = logevar - (log(1-phi) + log(1+phi)) - self.logpvars_1xu = \ - logevars_1xu - tf.log(1.0-phis_1xu) - tf.log(1.0+phis_1xu) - self.logpvars_bxu = logpvars_bxu = tf.tile(self.logpvars_1xu, size_bx1) - logpvars_bxu.set_shape(size__xu) - - # process mean (zero but included in for completeness) - self.pmeans_bxu = pmeans_bxu = tf.zeros_like(phis_bxu) - - # For sampling from the prior during de-novo generation. - self.means_t = means_t = [None] * num_steps - self.logvars_t = logvars_t = [None] * num_steps - self.samples_t = samples_t = [None] * num_steps - self.gaussians_t = gaussians_t = [None] * num_steps - sample_bxu = tf.zeros_like(phis_bxu) - for t in range(num_steps): - # process variance used here to make process completely stationary - if t == 0: - logvar_pt_bxu = self.logpvars_bxu - else: - logvar_pt_bxu = self.logevars_bxu - - z_mean_pt_bxu = pmeans_bxu + phis_bxu * sample_bxu - gaussians_t[t] = DiagonalGaussian(batch_size, z_size, - mean=z_mean_pt_bxu, - logvar=logvar_pt_bxu) - sample_bxu = gaussians_t[t].sample - samples_t[t] = sample_bxu - logvars_t[t] = logvar_pt_bxu - means_t[t] = z_mean_pt_bxu - - def logp_t(self, z_t_bxu, z_tm1_bxu=None): - """Compute the log-likelihood under the distribution for a given time t, - not the whole sequence. - - Args: - z_t_bxu: sample to compute likelihood for at time t. - z_tm1_bxu (optional): sample condition probability of z_t upon. - - Returns: - The likelihood of p_t under the model at time t. i.e. - p(z_t|z_tm1_bxu) = N(z_tm1_bxu * phis, eps^2) - - """ - if z_tm1_bxu is None: - return diag_gaussian_log_likelihood(z_t_bxu, self.pmeans_bxu, - self.logpvars_bxu) - else: - means_t_bxu = self.pmeans_bxu + self.phis_bxu * z_tm1_bxu - logp_tgtm1_bxu = diag_gaussian_log_likelihood(z_t_bxu, - means_t_bxu, - self.logevars_bxu) - return logp_tgtm1_bxu - - -class KLCost_GaussianGaussian(object): - """log p(x|z) + KL(q||p) terms for Gaussian posterior and Gaussian prior. See - eqn 10 and Appendix B in VAE for latter term, - http://arxiv.org/abs/1312.6114 - - The log p(x|z) term is the reconstruction error under the model. - The KL term represents the penalty for passing information from the encoder - to the decoder. - To sample KL(q||p), we simply sample - ln q - ln p - by drawing samples from q and averaging. - """ - - def __init__(self, zs, prior_zs): - """Create a lower bound in three parts, normalized reconstruction - cost, normalized KL divergence cost, and their sum. - - E_q[ln p(z_i | z_{i+1}) / q(z_i | x) - \int q(z) ln p(z) dz = - 0.5 ln(2pi) - 0.5 \sum (ln(sigma_p^2) + \ - sigma_q^2 / sigma_p^2 + (mean_p - mean_q)^2 / sigma_p^2) - - \int q(z) ln q(z) dz = - 0.5 ln(2pi) - 0.5 \sum (ln(sigma_q^2) + 1) - - Args: - zs: posterior z ~ q(z|x) - prior_zs: prior zs - """ - # L = -KL + log p(x|z), to maximize bound on likelihood - # -L = KL - log p(x|z), to minimize bound on NLL - # so 'KL cost' is postive KL divergence - kl_b = 0.0 - for z, prior_z in zip(zs, prior_zs): - assert isinstance(z, Gaussian) - assert isinstance(prior_z, Gaussian) - # ln(2pi) terms cancel - kl_b += 0.5 * tf.reduce_sum( - prior_z.logvar - z.logvar - + tf.exp(z.logvar - prior_z.logvar) - + tf.square((z.mean - prior_z.mean) / tf.exp(0.5 * prior_z.logvar)) - - 1.0, [1]) - - self.kl_cost_b = kl_b - self.kl_cost = tf.reduce_mean(kl_b) - - -class KLCost_GaussianGaussianProcessSampled(object): - """ log p(x|z) + KL(q||p) terms for Gaussian posterior and Gaussian process - prior via sampling. - - The log p(x|z) term is the reconstruction error under the model. - The KL term represents the penalty for passing information from the encoder - to the decoder. - To sample KL(q||p), we simply sample - ln q - ln p - by drawing samples from q and averaging. - """ - - def __init__(self, post_zs, prior_z_process): - """Create a lower bound in three parts, normalized reconstruction - cost, normalized KL divergence cost, and their sum. - - Args: - post_zs: posterior z ~ q(z|x) - prior_z_process: prior AR(1) process - """ - assert len(post_zs) > 1, "GP is for time, need more than 1 time step." - assert isinstance(prior_z_process, GaussianProcess), "Must use GP." - - # L = -KL + log p(x|z), to maximize bound on likelihood - # -L = KL - log p(x|z), to minimize bound on NLL - # so 'KL cost' is postive KL divergence - z0_bxu = post_zs[0].sample - logq_bxu = post_zs[0].logp(z0_bxu) - logp_bxu = prior_z_process.logp_t(z0_bxu) - z_tm1_bxu = z0_bxu - for z_t in post_zs[1:]: - # posterior is independent in time, prior is not - z_t_bxu = z_t.sample - logq_bxu += z_t.logp(z_t_bxu) - logp_bxu += prior_z_process.logp_t(z_t_bxu, z_tm1_bxu) - z_tm1_bxu = z_t_bxu - - kl_bxu = logq_bxu - logp_bxu - kl_b = tf.reduce_sum(kl_bxu, [1]) - self.kl_cost_b = kl_b - self.kl_cost = tf.reduce_mean(kl_b) diff --git a/research/lfads/lfads.py b/research/lfads/lfads.py deleted file mode 100644 index 925484c62eb..00000000000 --- a/research/lfads/lfads.py +++ /dev/null @@ -1,2170 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -""" -LFADS - Latent Factor Analysis via Dynamical Systems. - -LFADS is an unsupervised method to decompose time series data into -various factors, such as an initial condition, a generative -dynamical system, control inputs to that generator, and a low -dimensional description of the observed data, called the factors. -Additionally, the observations have a noise model (in this case -Poisson), so a denoised version of the observations is also created -(e.g. underlying rates of a Poisson distribution given the observed -event counts). - -The main data structure being passed around is a dataset. This is a dictionary -of data dictionaries. - -DATASET: The top level dictionary is simply name (string -> dictionary). -The nested dictionary is the DATA DICTIONARY, which has the following keys: - 'train_data' and 'valid_data', whose values are the corresponding training - and validation data with shape - ExTxD, E - # examples, T - # time steps, D - # dimensions in data. - The data dictionary also has a few more keys: - 'train_ext_input' and 'valid_ext_input', if there are know external inputs - to the system being modeled, these take on dimensions: - ExTxI, E - # examples, T - # time steps, I = # dimensions in input. - 'alignment_matrix_cxf' - If you are using multiple days data, it's possible - that one can align the channels (see manuscript). If so each dataset will - contain this matrix, which will be used for both the input adapter and the - output adapter for each dataset. These matrices, if provided, must be of - size [data_dim x factors] where data_dim is the number of neurons recorded - on that day, and factors is chosen and set through the '--factors' flag. - 'alignment_bias_c' - See alignment_matrix_cxf. This bias will used to - the offset for the alignment transformation. It will *subtract* off the - bias from the data, so pca style inits can align factors across sessions. - - - If one runs LFADS on data where the true rates are known for some trials, - (say simulated, testing data, as in the example shipped with the paper), then - one can add three more fields for plotting purposes. These are 'train_truth' - and 'valid_truth', and 'conversion_factor'. These have the same dimensions as - 'train_data', and 'valid_data' but represent the underlying rates of the - observations. Finally, if one needs to convert scale for plotting the true - underlying firing rates, there is the 'conversion_factor' key. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - -import numpy as np -import os -import tensorflow as tf -from distributions import LearnableDiagonalGaussian, DiagonalGaussianFromInput -from distributions import diag_gaussian_log_likelihood -from distributions import KLCost_GaussianGaussian, Poisson -from distributions import LearnableAutoRegressive1Prior -from distributions import KLCost_GaussianGaussianProcessSampled - -from utils import init_linear, linear, list_t_bxn_to_tensor_bxtxn, write_data -from utils import log_sum_exp, flatten -from plot_lfads import plot_lfads - - -class GRU(object): - """Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078). - - """ - def __init__(self, num_units, forget_bias=1.0, weight_scale=1.0, - clip_value=np.inf, collections=None): - """Create a GRU object. - - Args: - num_units: Number of units in the GRU. - forget_bias (optional): Hack to help learning. - weight_scale (optional): Weights are scaled by ws/sqrt(#inputs), with - ws being the weight scale. - clip_value (optional): If the recurrent values grow above this value, - clip them. - collections (optional): List of additional collections variables should - belong to. - """ - self._num_units = num_units - self._forget_bias = forget_bias - self._weight_scale = weight_scale - self._clip_value = clip_value - self._collections = collections - - @property - def state_size(self): - return self._num_units - - @property - def output_size(self): - return self._num_units - - @property - def state_multiplier(self): - return 1 - - def output_from_state(self, state): - """Return the output portion of the state.""" - return state - - def __call__(self, inputs, state, scope=None): - """Gated recurrent unit (GRU) function. - - Args: - inputs: A 2D batch x input_dim tensor of inputs. - state: The previous state from the last time step. - scope (optional): TF variable scope for defined GRU variables. - - Returns: - A tuple (state, state), where state is the newly computed state at time t. - It is returned twice to respect an interface that works for LSTMs. - """ - - x = inputs - h = state - if inputs is not None: - xh = tf.concat(axis=1, values=[x, h]) - else: - xh = h - - with tf.variable_scope(scope or type(self).__name__): # "GRU" - with tf.variable_scope("Gates"): # Reset gate and update gate. - # We start with bias of 1.0 to not reset and not update. - r, u = tf.split(axis=1, num_or_size_splits=2, value=linear(xh, - 2 * self._num_units, - alpha=self._weight_scale, - name="xh_2_ru", - collections=self._collections)) - r, u = tf.sigmoid(r), tf.sigmoid(u + self._forget_bias) - with tf.variable_scope("Candidate"): - xrh = tf.concat(axis=1, values=[x, r * h]) - c = tf.tanh(linear(xrh, self._num_units, name="xrh_2_c", - collections=self._collections)) - new_h = u * h + (1 - u) * c - new_h = tf.clip_by_value(new_h, -self._clip_value, self._clip_value) - - return new_h, new_h - - -class GenGRU(object): - """Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078). - - This version is specialized for the generator, but isn't as fast, so - we have two. Note this allows for l2 regularization on the recurrent - weights, but also implicitly rescales the inputs via the 1/sqrt(input) - scaling in the linear helper routine to be large magnitude, if there are - fewer inputs than recurrent state. - - """ - def __init__(self, num_units, forget_bias=1.0, - input_weight_scale=1.0, rec_weight_scale=1.0, clip_value=np.inf, - input_collections=None, recurrent_collections=None): - """Create a GRU object. - - Args: - num_units: Number of units in the GRU. - forget_bias (optional): Hack to help learning. - input_weight_scale (optional): Weights are scaled ws/sqrt(#inputs), with - ws being the weight scale. - rec_weight_scale (optional): Weights are scaled ws/sqrt(#inputs), - with ws being the weight scale. - clip_value (optional): If the recurrent values grow above this value, - clip them. - input_collections (optional): List of additional collections variables - that input->rec weights should belong to. - recurrent_collections (optional): List of additional collections variables - that rec->rec weights should belong to. - """ - self._num_units = num_units - self._forget_bias = forget_bias - self._input_weight_scale = input_weight_scale - self._rec_weight_scale = rec_weight_scale - self._clip_value = clip_value - self._input_collections = input_collections - self._rec_collections = recurrent_collections - - @property - def state_size(self): - return self._num_units - - @property - def output_size(self): - return self._num_units - - @property - def state_multiplier(self): - return 1 - - def output_from_state(self, state): - """Return the output portion of the state.""" - return state - - def __call__(self, inputs, state, scope=None): - """Gated recurrent unit (GRU) function. - - Args: - inputs: A 2D batch x input_dim tensor of inputs. - state: The previous state from the last time step. - scope (optional): TF variable scope for defined GRU variables. - - Returns: - A tuple (state, state), where state is the newly computed state at time t. - It is returned twice to respect an interface that works for LSTMs. - """ - - x = inputs - h = state - with tf.variable_scope(scope or type(self).__name__): # "GRU" - with tf.variable_scope("Gates"): # Reset gate and update gate. - # We start with bias of 1.0 to not reset and not update. - r_x = u_x = 0.0 - if x is not None: - r_x, u_x = tf.split(axis=1, num_or_size_splits=2, value=linear(x, - 2 * self._num_units, - alpha=self._input_weight_scale, - do_bias=False, - name="x_2_ru", - normalized=False, - collections=self._input_collections)) - - r_h, u_h = tf.split(axis=1, num_or_size_splits=2, value=linear(h, - 2 * self._num_units, - do_bias=True, - alpha=self._rec_weight_scale, - name="h_2_ru", - collections=self._rec_collections)) - r = r_x + r_h - u = u_x + u_h - r, u = tf.sigmoid(r), tf.sigmoid(u + self._forget_bias) - - with tf.variable_scope("Candidate"): - c_x = 0.0 - if x is not None: - c_x = linear(x, self._num_units, name="x_2_c", do_bias=False, - alpha=self._input_weight_scale, - normalized=False, - collections=self._input_collections) - c_rh = linear(r*h, self._num_units, name="rh_2_c", do_bias=True, - alpha=self._rec_weight_scale, - collections=self._rec_collections) - c = tf.tanh(c_x + c_rh) - - new_h = u * h + (1 - u) * c - new_h = tf.clip_by_value(new_h, -self._clip_value, self._clip_value) - - return new_h, new_h - - -class LFADS(object): - """LFADS - Latent Factor Analysis via Dynamical Systems. - - LFADS is an unsupervised method to decompose time series data into - various factors, such as an initial condition, a generative - dynamical system, inferred inputs to that generator, and a low - dimensional description of the observed data, called the factors. - Additionally, the observations have a noise model (in this case - Poisson), so a denoised version of the observations is also created - (e.g. underlying rates of a Poisson distribution given the observed - event counts). - """ - - def __init__(self, hps, kind="train", datasets=None): - """Create an LFADS model. - - train - a model for training, sampling of posteriors is used - posterior_sample_and_average - sample from the posterior, this is used - for evaluating the expected value of the outputs of LFADS, given a - specific input, by averaging over multiple samples from the approx - posterior. Also used for the lower bound on the negative - log-likelihood using IWAE error (Importance Weighed Auto-encoder). - This is the denoising operation. - prior_sample - a model for generation - sampling from priors is used - - Args: - hps: The dictionary of hyper parameters. - kind: The type of model to build (see above). - datasets: A dictionary of named data_dictionaries, see top of lfads.py - """ - print("Building graph...") - all_kinds = ['train', 'posterior_sample_and_average', 'posterior_push_mean', - 'prior_sample'] - assert kind in all_kinds, 'Wrong kind' - if hps.feedback_factors_or_rates == "rates": - assert len(hps.dataset_names) == 1, \ - "Multiple datasets not supported for rate feedback." - num_steps = hps.num_steps - ic_dim = hps.ic_dim - co_dim = hps.co_dim - ext_input_dim = hps.ext_input_dim - cell_class = GRU - gen_cell_class = GenGRU - - def makelambda(v): # Used with tf.case - return lambda: v - - # Define the data placeholder, and deal with all parts of the graph - # that are dataset dependent. - self.dataName = tf.placeholder(tf.string, shape=()) - # The batch_size to be inferred from data, as normal. - # Additionally, the data_dim will be inferred as well, allowing for a - # single placeholder for all datasets, regardless of data dimension. - if hps.output_dist == 'poisson': - # Enforce correct dtype - assert np.issubdtype( - datasets[hps.dataset_names[0]]['train_data'].dtype, int), \ - "Data dtype must be int for poisson output distribution" - data_dtype = tf.int32 - elif hps.output_dist == 'gaussian': - assert np.issubdtype( - datasets[hps.dataset_names[0]]['train_data'].dtype, float), \ - "Data dtype must be float for gaussian output dsitribution" - data_dtype = tf.float32 - else: - assert False, "NIY" - self.dataset_ph = dataset_ph = tf.placeholder(data_dtype, - [None, num_steps, None], - name="data") - self.train_step = tf.get_variable("global_step", [], tf.int64, - tf.zeros_initializer(), - trainable=False) - self.hps = hps - ndatasets = hps.ndatasets - factors_dim = hps.factors_dim - self.preds = preds = [None] * ndatasets - self.fns_in_fac_Ws = fns_in_fac_Ws = [None] * ndatasets - self.fns_in_fatcor_bs = fns_in_fac_bs = [None] * ndatasets - self.fns_out_fac_Ws = fns_out_fac_Ws = [None] * ndatasets - self.fns_out_fac_bs = fns_out_fac_bs = [None] * ndatasets - self.datasetNames = dataset_names = hps.dataset_names - self.ext_inputs = ext_inputs = None - - if len(dataset_names) == 1: # single session - if 'alignment_matrix_cxf' in datasets[dataset_names[0]].keys(): - used_in_factors_dim = factors_dim - in_identity_if_poss = False - else: - used_in_factors_dim = hps.dataset_dims[dataset_names[0]] - in_identity_if_poss = True - else: # multisession - used_in_factors_dim = factors_dim - in_identity_if_poss = False - - for d, name in enumerate(dataset_names): - data_dim = hps.dataset_dims[name] - in_mat_cxf = None - in_bias_1xf = None - align_bias_1xc = None - - if datasets and 'alignment_matrix_cxf' in datasets[name].keys(): - dataset = datasets[name] - if hps.do_train_readin: - print("Initializing trainable readin matrix with alignment matrix" \ - " provided for dataset:", name) - else: - print("Setting non-trainable readin matrix to alignment matrix" \ - " provided for dataset:", name) - in_mat_cxf = dataset['alignment_matrix_cxf'].astype(np.float32) - if in_mat_cxf.shape != (data_dim, factors_dim): - raise ValueError("""Alignment matrix must have dimensions %d x %d - (data_dim x factors_dim), but currently has %d x %d."""% - (data_dim, factors_dim, in_mat_cxf.shape[0], - in_mat_cxf.shape[1])) - if datasets and 'alignment_bias_c' in datasets[name].keys(): - dataset = datasets[name] - if hps.do_train_readin: - print("Initializing trainable readin bias with alignment bias " \ - "provided for dataset:", name) - else: - print("Setting non-trainable readin bias to alignment bias " \ - "provided for dataset:", name) - align_bias_c = dataset['alignment_bias_c'].astype(np.float32) - align_bias_1xc = np.expand_dims(align_bias_c, axis=0) - if align_bias_1xc.shape[1] != data_dim: - raise ValueError("""Alignment bias must have dimensions %d - (data_dim), but currently has %d."""% - (data_dim, in_mat_cxf.shape[0])) - if in_mat_cxf is not None and align_bias_1xc is not None: - # (data - alignment_bias) * W_in - # data * W_in - alignment_bias * W_in - # So b = -alignment_bias * W_in to accommodate PCA style offset. - in_bias_1xf = -np.dot(align_bias_1xc, in_mat_cxf) - - if hps.do_train_readin: - # only add to IO transformations collection only if we want it to be - # learnable, because IO_transformations collection will be trained - # when do_train_io_only - collections_readin=['IO_transformations'] - else: - collections_readin=None - - in_fac_lin = init_linear(data_dim, used_in_factors_dim, - do_bias=True, - mat_init_value=in_mat_cxf, - bias_init_value=in_bias_1xf, - identity_if_possible=in_identity_if_poss, - normalized=False, name="x_2_infac_"+name, - collections=collections_readin, - trainable=hps.do_train_readin) - in_fac_W, in_fac_b = in_fac_lin - fns_in_fac_Ws[d] = makelambda(in_fac_W) - fns_in_fac_bs[d] = makelambda(in_fac_b) - - with tf.variable_scope("glm"): - out_identity_if_poss = False - if len(dataset_names) == 1 and \ - factors_dim == hps.dataset_dims[dataset_names[0]]: - out_identity_if_poss = True - for d, name in enumerate(dataset_names): - data_dim = hps.dataset_dims[name] - in_mat_cxf = None - if datasets and 'alignment_matrix_cxf' in datasets[name].keys(): - dataset = datasets[name] - in_mat_cxf = dataset['alignment_matrix_cxf'].astype(np.float32) - - if datasets and 'alignment_bias_c' in datasets[name].keys(): - dataset = datasets[name] - align_bias_c = dataset['alignment_bias_c'].astype(np.float32) - align_bias_1xc = np.expand_dims(align_bias_c, axis=0) - - out_mat_fxc = None - out_bias_1xc = None - if in_mat_cxf is not None: - out_mat_fxc = in_mat_cxf.T - if align_bias_1xc is not None: - out_bias_1xc = align_bias_1xc - - if hps.output_dist == 'poisson': - out_fac_lin = init_linear(factors_dim, data_dim, do_bias=True, - mat_init_value=out_mat_fxc, - bias_init_value=out_bias_1xc, - identity_if_possible=out_identity_if_poss, - normalized=False, - name="fac_2_logrates_"+name, - collections=['IO_transformations']) - out_fac_W, out_fac_b = out_fac_lin - - elif hps.output_dist == 'gaussian': - out_fac_lin_mean = \ - init_linear(factors_dim, data_dim, do_bias=True, - mat_init_value=out_mat_fxc, - bias_init_value=out_bias_1xc, - normalized=False, - name="fac_2_means_"+name, - collections=['IO_transformations']) - out_fac_W_mean, out_fac_b_mean = out_fac_lin_mean - - mat_init_value = np.zeros([factors_dim, data_dim]).astype(np.float32) - bias_init_value = np.ones([1, data_dim]).astype(np.float32) - out_fac_lin_logvar = \ - init_linear(factors_dim, data_dim, do_bias=True, - mat_init_value=mat_init_value, - bias_init_value=bias_init_value, - normalized=False, - name="fac_2_logvars_"+name, - collections=['IO_transformations']) - out_fac_W_mean, out_fac_b_mean = out_fac_lin_mean - out_fac_W_logvar, out_fac_b_logvar = out_fac_lin_logvar - out_fac_W = tf.concat( - axis=1, values=[out_fac_W_mean, out_fac_W_logvar]) - out_fac_b = tf.concat( - axis=1, values=[out_fac_b_mean, out_fac_b_logvar]) - else: - assert False, "NIY" - - preds[d] = tf.equal(tf.constant(name), self.dataName) - data_dim = hps.dataset_dims[name] - fns_out_fac_Ws[d] = makelambda(out_fac_W) - fns_out_fac_bs[d] = makelambda(out_fac_b) - - pf_pairs_in_fac_Ws = zip(preds, fns_in_fac_Ws) - pf_pairs_in_fac_bs = zip(preds, fns_in_fac_bs) - pf_pairs_out_fac_Ws = zip(preds, fns_out_fac_Ws) - pf_pairs_out_fac_bs = zip(preds, fns_out_fac_bs) - - this_in_fac_W = tf.case(pf_pairs_in_fac_Ws, exclusive=True) - this_in_fac_b = tf.case(pf_pairs_in_fac_bs, exclusive=True) - this_out_fac_W = tf.case(pf_pairs_out_fac_Ws, exclusive=True) - this_out_fac_b = tf.case(pf_pairs_out_fac_bs, exclusive=True) - - # External inputs (not changing by dataset, by definition). - if hps.ext_input_dim > 0: - self.ext_input = tf.placeholder(tf.float32, - [None, num_steps, ext_input_dim], - name="ext_input") - else: - self.ext_input = None - ext_input_bxtxi = self.ext_input - - self.keep_prob = keep_prob = tf.placeholder(tf.float32, [], "keep_prob") - self.batch_size = batch_size = int(hps.batch_size) - self.learning_rate = tf.Variable(float(hps.learning_rate_init), - trainable=False, name="learning_rate") - self.learning_rate_decay_op = self.learning_rate.assign( - self.learning_rate * hps.learning_rate_decay_factor) - - # Dropout the data. - dataset_do_bxtxd = tf.nn.dropout(tf.to_float(dataset_ph), keep_prob) - if hps.ext_input_dim > 0: - ext_input_do_bxtxi = tf.nn.dropout(ext_input_bxtxi, keep_prob) - else: - ext_input_do_bxtxi = None - - # ENCODERS - def encode_data(dataset_bxtxd, enc_cell, name, forward_or_reverse, - num_steps_to_encode): - """Encode data for LFADS - Args: - dataset_bxtxd - the data to encode, as a 3 tensor, with dims - time x batch x data dims. - enc_cell: encoder cell - name: name of encoder - forward_or_reverse: string, encode in forward or reverse direction - num_steps_to_encode: number of steps to encode, 0:num_steps_to_encode - Returns: - encoded data as a list with num_steps_to_encode items, in order - """ - if forward_or_reverse == "forward": - dstr = "_fwd" - time_fwd_or_rev = range(num_steps_to_encode) - else: - dstr = "_rev" - time_fwd_or_rev = reversed(range(num_steps_to_encode)) - - with tf.variable_scope(name+"_enc"+dstr, reuse=False): - enc_state = tf.tile( - tf.Variable(tf.zeros([1, enc_cell.state_size]), - name=name+"_enc_t0"+dstr), tf.stack([batch_size, 1])) - enc_state.set_shape([None, enc_cell.state_size]) # tile loses shape - - enc_outs = [None] * num_steps_to_encode - for i, t in enumerate(time_fwd_or_rev): - with tf.variable_scope(name+"_enc"+dstr, reuse=True if i > 0 else None): - dataset_t_bxd = dataset_bxtxd[:,t,:] - in_fac_t_bxf = tf.matmul(dataset_t_bxd, this_in_fac_W) + this_in_fac_b - in_fac_t_bxf.set_shape([None, used_in_factors_dim]) - if ext_input_dim > 0 and not hps.inject_ext_input_to_gen: - ext_input_t_bxi = ext_input_do_bxtxi[:,t,:] - enc_input_t_bxfpe = tf.concat( - axis=1, values=[in_fac_t_bxf, ext_input_t_bxi]) - else: - enc_input_t_bxfpe = in_fac_t_bxf - enc_out, enc_state = enc_cell(enc_input_t_bxfpe, enc_state) - enc_outs[t] = enc_out - - return enc_outs - - # Encode initial condition means and variances - # ([x_T, x_T-1, ... x_0] and [x_0, x_1, ... x_T] -> g0/c0) - self.ic_enc_fwd = [None] * num_steps - self.ic_enc_rev = [None] * num_steps - if ic_dim > 0: - enc_ic_cell = cell_class(hps.ic_enc_dim, - weight_scale=hps.cell_weight_scale, - clip_value=hps.cell_clip_value) - ic_enc_fwd = encode_data(dataset_do_bxtxd, enc_ic_cell, - "ic", "forward", - hps.num_steps_for_gen_ic) - ic_enc_rev = encode_data(dataset_do_bxtxd, enc_ic_cell, - "ic", "reverse", - hps.num_steps_for_gen_ic) - self.ic_enc_fwd = ic_enc_fwd - self.ic_enc_rev = ic_enc_rev - - # Encoder control input means and variances, bi-directional encoding so: - # ([x_T, x_T-1, ..., x_0] and [x_0, x_1 ... x_T] -> u_t) - self.ci_enc_fwd = [None] * num_steps - self.ci_enc_rev = [None] * num_steps - if co_dim > 0: - enc_ci_cell = cell_class(hps.ci_enc_dim, - weight_scale=hps.cell_weight_scale, - clip_value=hps.cell_clip_value) - ci_enc_fwd = encode_data(dataset_do_bxtxd, enc_ci_cell, - "ci", "forward", - hps.num_steps) - if hps.do_causal_controller: - ci_enc_rev = None - else: - ci_enc_rev = encode_data(dataset_do_bxtxd, enc_ci_cell, - "ci", "reverse", - hps.num_steps) - self.ci_enc_fwd = ci_enc_fwd - self.ci_enc_rev = ci_enc_rev - - # STOCHASTIC LATENT VARIABLES, priors and posteriors - # (initial conditions g0, and control inputs, u_t) - # Note that zs represent all the stochastic latent variables. - with tf.variable_scope("z", reuse=False): - self.prior_zs_g0 = None - self.posterior_zs_g0 = None - self.g0s_val = None - if ic_dim > 0: - self.prior_zs_g0 = \ - LearnableDiagonalGaussian(batch_size, ic_dim, name="prior_g0", - mean_init=0.0, - var_min=hps.ic_prior_var_min, - var_init=hps.ic_prior_var_scale, - var_max=hps.ic_prior_var_max) - ic_enc = tf.concat(axis=1, values=[ic_enc_fwd[-1], ic_enc_rev[0]]) - ic_enc = tf.nn.dropout(ic_enc, keep_prob) - self.posterior_zs_g0 = \ - DiagonalGaussianFromInput(ic_enc, ic_dim, "ic_enc_2_post_g0", - var_min=hps.ic_post_var_min) - if kind in ["train", "posterior_sample_and_average", - "posterior_push_mean"]: - zs_g0 = self.posterior_zs_g0 - else: - zs_g0 = self.prior_zs_g0 - if kind in ["train", "posterior_sample_and_average", "prior_sample"]: - self.g0s_val = zs_g0.sample - else: - self.g0s_val = zs_g0.mean - - # Priors for controller, 'co' for controller output - self.prior_zs_co = prior_zs_co = [None] * num_steps - self.posterior_zs_co = posterior_zs_co = [None] * num_steps - self.zs_co = zs_co = [None] * num_steps - self.prior_zs_ar_con = None - if co_dim > 0: - # Controller outputs - autocorrelation_taus = [hps.prior_ar_atau for x in range(hps.co_dim)] - noise_variances = [hps.prior_ar_nvar for x in range(hps.co_dim)] - self.prior_zs_ar_con = prior_zs_ar_con = \ - LearnableAutoRegressive1Prior(batch_size, hps.co_dim, - autocorrelation_taus, - noise_variances, - hps.do_train_prior_ar_atau, - hps.do_train_prior_ar_nvar, - num_steps, "u_prior_ar1") - - # CONTROLLER -> GENERATOR -> RATES - # (u(t) -> gen(t) -> factors(t) -> rates(t) -> p(x_t|z_t) ) - self.controller_outputs = u_t = [None] * num_steps - self.con_ics = con_state = None - self.con_states = con_states = [None] * num_steps - self.con_outs = con_outs = [None] * num_steps - self.gen_inputs = gen_inputs = [None] * num_steps - if co_dim > 0: - # gen_cell_class here for l2 penalty recurrent weights - # didn't split the cell_weight scale here, because I doubt it matters - con_cell = gen_cell_class(hps.con_dim, - input_weight_scale=hps.cell_weight_scale, - rec_weight_scale=hps.cell_weight_scale, - clip_value=hps.cell_clip_value, - recurrent_collections=['l2_con_reg']) - with tf.variable_scope("con", reuse=False): - self.con_ics = tf.tile( - tf.Variable(tf.zeros([1, hps.con_dim*con_cell.state_multiplier]), - name="c0"), - tf.stack([batch_size, 1])) - self.con_ics.set_shape([None, con_cell.state_size]) # tile loses shape - con_states[-1] = self.con_ics - - gen_cell = gen_cell_class(hps.gen_dim, - input_weight_scale=hps.gen_cell_input_weight_scale, - rec_weight_scale=hps.gen_cell_rec_weight_scale, - clip_value=hps.cell_clip_value, - recurrent_collections=['l2_gen_reg']) - with tf.variable_scope("gen", reuse=False): - if ic_dim == 0: - self.gen_ics = tf.tile( - tf.Variable(tf.zeros([1, gen_cell.state_size]), name="g0"), - tf.stack([batch_size, 1])) - else: - self.gen_ics = linear(self.g0s_val, gen_cell.state_size, - identity_if_possible=True, - name="g0_2_gen_ic") - - self.gen_states = gen_states = [None] * num_steps - self.gen_outs = gen_outs = [None] * num_steps - gen_states[-1] = self.gen_ics - gen_outs[-1] = gen_cell.output_from_state(gen_states[-1]) - self.factors = factors = [None] * num_steps - factors[-1] = linear(gen_outs[-1], factors_dim, do_bias=False, - normalized=True, name="gen_2_fac") - - self.rates = rates = [None] * num_steps - # rates[-1] is collected to potentially feed back to controller - with tf.variable_scope("glm", reuse=False): - if hps.output_dist == 'poisson': - log_rates_t0 = tf.matmul(factors[-1], this_out_fac_W) + this_out_fac_b - log_rates_t0.set_shape([None, None]) - rates[-1] = tf.exp(log_rates_t0) # rate - rates[-1].set_shape([None, hps.dataset_dims[hps.dataset_names[0]]]) - elif hps.output_dist == 'gaussian': - mean_n_logvars = tf.matmul(factors[-1],this_out_fac_W) + this_out_fac_b - mean_n_logvars.set_shape([None, None]) - means_t_bxd, logvars_t_bxd = tf.split(axis=1, num_or_size_splits=2, - value=mean_n_logvars) - rates[-1] = means_t_bxd - else: - assert False, "NIY" - - # We support multiple output distributions, for example Poisson, and also - # Gaussian. In these two cases respectively, there are one and two - # parameters (rates vs. mean and variance). So the output_dist_params - # tensor will variable sizes via tf.concat and tf.split, along the 1st - # dimension. So in the case of gaussian, for example, it'll be - # batch x (D+D), where each D dims is the mean, and then variances, - # respectively. For a distribution with 3 parameters, it would be - # batch x (D+D+D). - self.output_dist_params = dist_params = [None] * num_steps - self.log_p_xgz_b = log_p_xgz_b = 0.0 # log P(x|z) - for t in range(num_steps): - # Controller - if co_dim > 0: - # Build inputs for controller - tlag = t - hps.controller_input_lag - if tlag < 0: - con_in_f_t = tf.zeros_like(ci_enc_fwd[0]) - else: - con_in_f_t = ci_enc_fwd[tlag] - if hps.do_causal_controller: - # If controller is causal (wrt to data generation process), then it - # cannot see future data. Thus, excluding ci_enc_rev[t] is obvious. - # Less obvious is the need to exclude factors[t-1]. This arises - # because information flows from g0 through factors to the controller - # input. The g0 encoding is backwards, so we must necessarily exclude - # the factors in order to keep the controller input purely from a - # forward encoding (however unlikely it is that - # g0->factors->controller channel might actually be used in this way). - con_in_list_t = [con_in_f_t] - else: - tlag_rev = t + hps.controller_input_lag - if tlag_rev >= num_steps: - # better than zeros - con_in_r_t = tf.zeros_like(ci_enc_rev[0]) - else: - con_in_r_t = ci_enc_rev[tlag_rev] - con_in_list_t = [con_in_f_t, con_in_r_t] - - if hps.do_feed_factors_to_controller: - if hps.feedback_factors_or_rates == "factors": - con_in_list_t.append(factors[t-1]) - elif hps.feedback_factors_or_rates == "rates": - con_in_list_t.append(rates[t-1]) - else: - assert False, "NIY" - - con_in_t = tf.concat(axis=1, values=con_in_list_t) - con_in_t = tf.nn.dropout(con_in_t, keep_prob) - with tf.variable_scope("con", reuse=True if t > 0 else None): - con_outs[t], con_states[t] = con_cell(con_in_t, con_states[t-1]) - posterior_zs_co[t] = \ - DiagonalGaussianFromInput(con_outs[t], co_dim, - name="con_to_post_co") - if kind == "train": - u_t[t] = posterior_zs_co[t].sample - elif kind == "posterior_sample_and_average": - u_t[t] = posterior_zs_co[t].sample - elif kind == "posterior_push_mean": - u_t[t] = posterior_zs_co[t].mean - else: - u_t[t] = prior_zs_ar_con.samples_t[t] - - # Inputs to the generator (controller output + external input) - if ext_input_dim > 0 and hps.inject_ext_input_to_gen: - ext_input_t_bxi = ext_input_do_bxtxi[:,t,:] - if co_dim > 0: - gen_inputs[t] = tf.concat(axis=1, values=[u_t[t], ext_input_t_bxi]) - else: - gen_inputs[t] = ext_input_t_bxi - else: - gen_inputs[t] = u_t[t] - - # Generator - data_t_bxd = dataset_ph[:,t,:] - with tf.variable_scope("gen", reuse=True if t > 0 else None): - gen_outs[t], gen_states[t] = gen_cell(gen_inputs[t], gen_states[t-1]) - gen_outs[t] = tf.nn.dropout(gen_outs[t], keep_prob) - with tf.variable_scope("gen", reuse=True): # ic defined it above - factors[t] = linear(gen_outs[t], factors_dim, do_bias=False, - normalized=True, name="gen_2_fac") - with tf.variable_scope("glm", reuse=True if t > 0 else None): - if hps.output_dist == 'poisson': - log_rates_t = tf.matmul(factors[t], this_out_fac_W) + this_out_fac_b - log_rates_t.set_shape([None, None]) - rates[t] = dist_params[t] = tf.exp(tf.clip_by_value(log_rates_t, -hps._clip_value, hps._clip_value)) # rates feed back - rates[t].set_shape([None, hps.dataset_dims[hps.dataset_names[0]]]) - loglikelihood_t = Poisson(log_rates_t).logp(data_t_bxd) - - elif hps.output_dist == 'gaussian': - mean_n_logvars = tf.matmul(factors[t],this_out_fac_W) + this_out_fac_b - mean_n_logvars.set_shape([None, None]) - means_t_bxd, logvars_t_bxd = tf.split(axis=1, num_or_size_splits=2, - value=mean_n_logvars) - rates[t] = means_t_bxd # rates feed back to controller - dist_params[t] = tf.concat( - axis=1, values=[means_t_bxd, tf.exp(tf.clip_by_value(logvars_t_bxd, -hps._clip_value, hps._clip_value))]) - loglikelihood_t = \ - diag_gaussian_log_likelihood(data_t_bxd, - means_t_bxd, logvars_t_bxd) - else: - assert False, "NIY" - - log_p_xgz_b += tf.reduce_sum(loglikelihood_t, [1]) - - # Correlation of inferred inputs cost. - self.corr_cost = tf.constant(0.0) - if hps.co_mean_corr_scale > 0.0: - all_sum_corr = [] - for i in range(hps.co_dim): - for j in range(i+1, hps.co_dim): - sum_corr_ij = tf.constant(0.0) - for t in range(num_steps): - u_mean_t = posterior_zs_co[t].mean - sum_corr_ij += u_mean_t[:,i]*u_mean_t[:,j] - all_sum_corr.append(0.5 * tf.square(sum_corr_ij)) - self.corr_cost = tf.reduce_mean(all_sum_corr) # div by batch and by n*(n-1)/2 pairs - - # Variational Lower Bound on posterior, p(z|x), plus reconstruction cost. - # KL and reconstruction costs are normalized only by batch size, not by - # dimension, or by time steps. - kl_cost_g0_b = tf.zeros_like(batch_size, dtype=tf.float32) - kl_cost_co_b = tf.zeros_like(batch_size, dtype=tf.float32) - self.kl_cost = tf.constant(0.0) # VAE KL cost - self.recon_cost = tf.constant(0.0) # VAE reconstruction cost - self.nll_bound_vae = tf.constant(0.0) - self.nll_bound_iwae = tf.constant(0.0) # for eval with IWAE cost. - if kind in ["train", "posterior_sample_and_average", "posterior_push_mean"]: - kl_cost_g0_b = 0.0 - kl_cost_co_b = 0.0 - if ic_dim > 0: - g0_priors = [self.prior_zs_g0] - g0_posts = [self.posterior_zs_g0] - kl_cost_g0_b = KLCost_GaussianGaussian(g0_posts, g0_priors).kl_cost_b - kl_cost_g0_b = hps.kl_ic_weight * kl_cost_g0_b - if co_dim > 0: - kl_cost_co_b = \ - KLCost_GaussianGaussianProcessSampled( - posterior_zs_co, prior_zs_ar_con).kl_cost_b - kl_cost_co_b = hps.kl_co_weight * kl_cost_co_b - - # L = -KL + log p(x|z), to maximize bound on likelihood - # -L = KL - log p(x|z), to minimize bound on NLL - # so 'reconstruction cost' is negative log likelihood - self.recon_cost = - tf.reduce_mean(log_p_xgz_b) - self.kl_cost = tf.reduce_mean(kl_cost_g0_b + kl_cost_co_b) - - lb_on_ll_b = log_p_xgz_b - kl_cost_g0_b - kl_cost_co_b - - # VAE error averages outside the log - self.nll_bound_vae = -tf.reduce_mean(lb_on_ll_b) - - # IWAE error averages inside the log - k = tf.cast(tf.shape(log_p_xgz_b)[0], tf.float32) - iwae_lb_on_ll = -tf.log(k) + log_sum_exp(lb_on_ll_b) - self.nll_bound_iwae = -iwae_lb_on_ll - - # L2 regularization on the generator, normalized by number of parameters. - self.l2_cost = tf.constant(0.0) - if self.hps.l2_gen_scale > 0.0 or self.hps.l2_con_scale > 0.0: - l2_costs = [] - l2_numels = [] - l2_reg_var_lists = [tf.get_collection('l2_gen_reg'), - tf.get_collection('l2_con_reg')] - l2_reg_scales = [self.hps.l2_gen_scale, self.hps.l2_con_scale] - for l2_reg_vars, l2_scale in zip(l2_reg_var_lists, l2_reg_scales): - for v in l2_reg_vars: - numel = tf.reduce_prod(tf.concat(axis=0, values=tf.shape(v))) - numel_f = tf.cast(numel, tf.float32) - l2_numels.append(numel_f) - v_l2 = tf.reduce_sum(v*v) - l2_costs.append(0.5 * l2_scale * v_l2) - self.l2_cost = tf.add_n(l2_costs) / tf.add_n(l2_numels) - - # Compute the cost for training, part of the graph regardless. - # The KL cost can be problematic at the beginning of optimization, - # so we allow an exponential increase in weighting the KL from 0 - # to 1. - self.kl_decay_step = tf.maximum(self.train_step - hps.kl_start_step, 0) - self.l2_decay_step = tf.maximum(self.train_step - hps.l2_start_step, 0) - kl_decay_step_f = tf.cast(self.kl_decay_step, tf.float32) - l2_decay_step_f = tf.cast(self.l2_decay_step, tf.float32) - kl_increase_steps_f = tf.cast(hps.kl_increase_steps, tf.float32) - l2_increase_steps_f = tf.cast(hps.l2_increase_steps, tf.float32) - self.kl_weight = kl_weight = \ - tf.minimum(kl_decay_step_f / kl_increase_steps_f, 1.0) - self.l2_weight = l2_weight = \ - tf.minimum(l2_decay_step_f / l2_increase_steps_f, 1.0) - - self.timed_kl_cost = kl_weight * self.kl_cost - self.timed_l2_cost = l2_weight * self.l2_cost - self.weight_corr_cost = hps.co_mean_corr_scale * self.corr_cost - self.cost = self.recon_cost + self.timed_kl_cost + \ - self.timed_l2_cost + self.weight_corr_cost - - if kind != "train": - # save every so often - self.seso_saver = tf.train.Saver(tf.global_variables(), - max_to_keep=hps.max_ckpt_to_keep) - # lowest validation error - self.lve_saver = tf.train.Saver(tf.global_variables(), - max_to_keep=hps.max_ckpt_to_keep_lve) - - return - - # OPTIMIZATION - # train the io matrices only - if self.hps.do_train_io_only: - self.train_vars = tvars = \ - tf.get_collection('IO_transformations', - scope=tf.get_variable_scope().name) - # train the encoder only - elif self.hps.do_train_encoder_only: - tvars1 = \ - tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, - scope='LFADS/ic_enc_*') - tvars2 = \ - tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, - scope='LFADS/z/ic_enc_*') - - self.train_vars = tvars = tvars1 + tvars2 - # train all variables - else: - self.train_vars = tvars = \ - tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, - scope=tf.get_variable_scope().name) - print("done.") - print("Model Variables (to be optimized): ") - total_params = 0 - for i in range(len(tvars)): - shape = tvars[i].get_shape().as_list() - print(" ", i, tvars[i].name, shape) - total_params += np.prod(shape) - print("Total model parameters: ", total_params) - - grads = tf.gradients(self.cost, tvars) - grads, grad_global_norm = tf.clip_by_global_norm(grads, hps.max_grad_norm) - opt = tf.train.AdamOptimizer(self.learning_rate, beta1=0.9, beta2=0.999, - epsilon=1e-01) - self.grads = grads - self.grad_global_norm = grad_global_norm - self.train_op = opt.apply_gradients( - zip(grads, tvars), global_step=self.train_step) - - self.seso_saver = tf.train.Saver(tf.global_variables(), - max_to_keep=hps.max_ckpt_to_keep) - - # lowest validation error - self.lve_saver = tf.train.Saver(tf.global_variables(), - max_to_keep=hps.max_ckpt_to_keep) - - # SUMMARIES, used only during training. - # example summary - self.example_image = tf.placeholder(tf.float32, shape=[1,None,None,3], - name='image_tensor') - self.example_summ = tf.summary.image("LFADS example", self.example_image, - collections=["example_summaries"]) - - # general training summaries - self.lr_summ = tf.summary.scalar("Learning rate", self.learning_rate) - self.kl_weight_summ = tf.summary.scalar("KL weight", self.kl_weight) - self.l2_weight_summ = tf.summary.scalar("L2 weight", self.l2_weight) - self.corr_cost_summ = tf.summary.scalar("Corr cost", self.weight_corr_cost) - self.grad_global_norm_summ = tf.summary.scalar("Gradient global norm", - self.grad_global_norm) - if hps.co_dim > 0: - self.atau_summ = [None] * hps.co_dim - self.pvar_summ = [None] * hps.co_dim - for c in range(hps.co_dim): - self.atau_summ[c] = \ - tf.summary.scalar("AR Autocorrelation taus " + str(c), - tf.exp(self.prior_zs_ar_con.logataus_1xu[0,c])) - self.pvar_summ[c] = \ - tf.summary.scalar("AR Variances " + str(c), - tf.exp(self.prior_zs_ar_con.logpvars_1xu[0,c])) - - # cost summaries, separated into different collections for - # training vs validation. We make placeholders for these, because - # even though the graph computes these costs on a per-batch basis, - # we want to report the more reliable metric of per-epoch cost. - kl_cost_ph = tf.placeholder(tf.float32, shape=[], name='kl_cost_ph') - self.kl_t_cost_summ = tf.summary.scalar("KL cost (train)", kl_cost_ph, - collections=["train_summaries"]) - self.kl_v_cost_summ = tf.summary.scalar("KL cost (valid)", kl_cost_ph, - collections=["valid_summaries"]) - l2_cost_ph = tf.placeholder(tf.float32, shape=[], name='l2_cost_ph') - self.l2_cost_summ = tf.summary.scalar("L2 cost", l2_cost_ph, - collections=["train_summaries"]) - - recon_cost_ph = tf.placeholder(tf.float32, shape=[], name='recon_cost_ph') - self.recon_t_cost_summ = tf.summary.scalar("Reconstruction cost (train)", - recon_cost_ph, - collections=["train_summaries"]) - self.recon_v_cost_summ = tf.summary.scalar("Reconstruction cost (valid)", - recon_cost_ph, - collections=["valid_summaries"]) - - total_cost_ph = tf.placeholder(tf.float32, shape=[], name='total_cost_ph') - self.cost_t_summ = tf.summary.scalar("Total cost (train)", total_cost_ph, - collections=["train_summaries"]) - self.cost_v_summ = tf.summary.scalar("Total cost (valid)", total_cost_ph, - collections=["valid_summaries"]) - - self.kl_cost_ph = kl_cost_ph - self.l2_cost_ph = l2_cost_ph - self.recon_cost_ph = recon_cost_ph - self.total_cost_ph = total_cost_ph - - # Merged summaries, for easy coding later. - self.merged_examples = tf.summary.merge_all(key="example_summaries") - self.merged_generic = tf.summary.merge_all() # default key is 'summaries' - self.merged_train = tf.summary.merge_all(key="train_summaries") - self.merged_valid = tf.summary.merge_all(key="valid_summaries") - - session = tf.get_default_session() - self.logfile = os.path.join(hps.lfads_save_dir, "lfads_log") - self.writer = tf.summary.FileWriter(self.logfile) - - def build_feed_dict(self, train_name, data_bxtxd, ext_input_bxtxi=None, - keep_prob=None): - """Build the feed dictionary, handles cases where there is no value defined. - - Args: - train_name: The key into the datasets, to set the tf.case statement for - the proper readin / readout matrices. - data_bxtxd: The data tensor. - ext_input_bxtxi (optional): The external input tensor. - keep_prob: The drop out keep probability. - - Returns: - The feed dictionary with TF tensors as keys and data as values, for use - with tf.Session.run() - - """ - feed_dict = {} - B, T, _ = data_bxtxd.shape - feed_dict[self.dataName] = train_name - feed_dict[self.dataset_ph] = data_bxtxd - - if self.ext_input is not None and ext_input_bxtxi is not None: - feed_dict[self.ext_input] = ext_input_bxtxi - - if keep_prob is None: - feed_dict[self.keep_prob] = self.hps.keep_prob - else: - feed_dict[self.keep_prob] = keep_prob - - return feed_dict - - @staticmethod - def get_batch(data_extxd, ext_input_extxi=None, batch_size=None, - example_idxs=None): - """Get a batch of data, either randomly chosen, or specified directly. - - Args: - data_extxd: The data to model, numpy tensors with shape: - # examples x # time steps x # dimensions - ext_input_extxi (optional): The external inputs, numpy tensor with shape: - # examples x # time steps x # external input dimensions - batch_size: The size of the batch to return. - example_idxs (optional): The example indices used to select examples. - - Returns: - A tuple with two parts: - 1. Batched data numpy tensor with shape: - batch_size x # time steps x # dimensions - 2. Batched external input numpy tensor with shape: - batch_size x # time steps x # external input dims - """ - assert batch_size is not None or example_idxs is not None, "Problems" - E, T, D = data_extxd.shape - if example_idxs is None: - example_idxs = np.random.choice(E, batch_size) - - ext_input_bxtxi = None - if ext_input_extxi is not None: - ext_input_bxtxi = ext_input_extxi[example_idxs,:,:] - - return data_extxd[example_idxs,:,:], ext_input_bxtxi - - @staticmethod - def example_idxs_mod_batch_size(nexamples, batch_size): - """Given a number of examples, E, and a batch_size, B, generate indices - [0, 1, 2, ... B-1; - [B, B+1, ... 2*B-1; - ... - ] - returning those indices as a 2-dim tensor shaped like E/B x B. Note that - shape is only correct if E % B == 0. If not, then an extra row is generated - so that the remainder of examples is included. The extra examples are - explicitly to to the zero index (see randomize_example_idxs_mod_batch_size) - for randomized behavior. - - Args: - nexamples: The number of examples to batch up. - batch_size: The size of the batch. - Returns: - 2-dim tensor as described above. - """ - bmrem = batch_size - (nexamples % batch_size) - bmrem_examples = [] - if bmrem < batch_size: - #bmrem_examples = np.zeros(bmrem, dtype=np.int32) - ridxs = np.random.permutation(nexamples)[0:bmrem].astype(np.int32) - bmrem_examples = np.sort(ridxs) - example_idxs = range(nexamples) + list(bmrem_examples) - example_idxs_e_x_edivb = np.reshape(example_idxs, [-1, batch_size]) - return example_idxs_e_x_edivb, bmrem - - @staticmethod - def randomize_example_idxs_mod_batch_size(nexamples, batch_size): - """Indices 1:nexamples, randomized, in 2D form of - shape = (nexamples / batch_size) x batch_size. The remainder - is managed by drawing randomly from 1:nexamples. - - Args: - nexamples: Number of examples to randomize. - batch_size: Number of elements in batch. - - Returns: - The randomized, properly shaped indicies. - """ - assert nexamples > batch_size, "Problems" - bmrem = batch_size - nexamples % batch_size - bmrem_examples = [] - if bmrem < batch_size: - bmrem_examples = np.random.choice(range(nexamples), - size=bmrem, replace=False) - example_idxs = range(nexamples) + list(bmrem_examples) - mixed_example_idxs = np.random.permutation(example_idxs) - example_idxs_e_x_edivb = np.reshape(mixed_example_idxs, [-1, batch_size]) - return example_idxs_e_x_edivb, bmrem - - def shuffle_spikes_in_time(self, data_bxtxd): - """Shuffle the spikes in the temporal dimension. This is useful to - help the LFADS system avoid overfitting to individual spikes or fast - oscillations found in the data that are irrelevant to behavior. A - pure 'tabula rasa' approach would avoid this, but LFADS is sensitive - enough to pick up dynamics that you may not want. - - Args: - data_bxtxd: Numpy array of spike count data to be shuffled. - Returns: - S_bxtxd, a numpy array with the same dimensions and contents as - data_bxtxd, but shuffled appropriately. - - """ - - B, T, N = data_bxtxd.shape - w = self.hps.temporal_spike_jitter_width - - if w == 0: - return data_bxtxd - - max_counts = np.max(data_bxtxd) - S_bxtxd = np.zeros([B,T,N]) - - # Intuitively, shuffle spike occurances, 0 or 1, but since we have counts, - # Do it over and over again up to the max count. - for mc in range(1,max_counts+1): - idxs = np.nonzero(data_bxtxd >= mc) - - data_ones = np.zeros_like(data_bxtxd) - data_ones[data_bxtxd >= mc] = 1 - - nfound = len(idxs[0]) - shuffles_incrs_in_time = np.random.randint(-w, w, size=nfound) - - shuffle_tidxs = idxs[1].copy() - shuffle_tidxs += shuffles_incrs_in_time - - # Reflect on the boundaries to not lose mass. - shuffle_tidxs[shuffle_tidxs < 0] = -shuffle_tidxs[shuffle_tidxs < 0] - shuffle_tidxs[shuffle_tidxs > T-1] = \ - (T-1)-(shuffle_tidxs[shuffle_tidxs > T-1] -(T-1)) - - for iii in zip(idxs[0], shuffle_tidxs, idxs[2]): - S_bxtxd[iii] += 1 - - return S_bxtxd - - def shuffle_and_flatten_datasets(self, datasets, kind='train'): - """Since LFADS supports multiple datasets in the same dynamical model, - we have to be careful to use all the data in a single training epoch. But - since the datasets my have different data dimensionality, we cannot batch - examples from data dictionaries together. Instead, we generate random - batches within each data dictionary, and then randomize these batches - while holding onto the dataname, so that when it's time to feed - the graph, the correct in/out matrices can be selected, per batch. - - Args: - datasets: A dict of data dicts. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - kind: 'train' or 'valid' - - Returns: - A flat list, in which each element is a pair ('name', indices). - """ - batch_size = self.hps.batch_size - ndatasets = len(datasets) - random_example_idxs = {} - epoch_idxs = {} - all_name_example_idx_pairs = [] - kind_data = kind + '_data' - for name, data_dict in datasets.items(): - nexamples, ntime, data_dim = data_dict[kind_data].shape - epoch_idxs[name] = 0 - random_example_idxs, _ = \ - self.randomize_example_idxs_mod_batch_size(nexamples, batch_size) - - epoch_size = random_example_idxs.shape[0] - names = [name] * epoch_size - all_name_example_idx_pairs += zip(names, random_example_idxs) - - np.random.shuffle(all_name_example_idx_pairs) # shuffle in place - - return all_name_example_idx_pairs - - def train_epoch(self, datasets, batch_size=None, do_save_ckpt=True): - """Train the model through the entire dataset once. - - Args: - datasets: A dict of data dicts. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - batch_size (optional): The batch_size to use. - do_save_ckpt (optional): Should the routine save a checkpoint on this - training epoch? - - Returns: - A tuple with 6 float values: - (total cost of the epoch, epoch reconstruction cost, - epoch kl cost, KL weight used this training epoch, - total l2 cost on generator, and the corresponding weight). - """ - ops_to_eval = [self.cost, self.recon_cost, - self.kl_cost, self.kl_weight, - self.l2_cost, self.l2_weight, - self.train_op] - collected_op_values = self.run_epoch(datasets, ops_to_eval, kind="train") - - total_cost = total_recon_cost = total_kl_cost = 0.0 - # normalizing by batch done in distributions.py - epoch_size = len(collected_op_values) - for op_values in collected_op_values: - total_cost += op_values[0] - total_recon_cost += op_values[1] - total_kl_cost += op_values[2] - - kl_weight = collected_op_values[-1][3] - l2_cost = collected_op_values[-1][4] - l2_weight = collected_op_values[-1][5] - - epoch_total_cost = total_cost / epoch_size - epoch_recon_cost = total_recon_cost / epoch_size - epoch_kl_cost = total_kl_cost / epoch_size - - if do_save_ckpt: - session = tf.get_default_session() - checkpoint_path = os.path.join(self.hps.lfads_save_dir, - self.hps.checkpoint_name + '.ckpt') - self.seso_saver.save(session, checkpoint_path, - global_step=self.train_step) - - return epoch_total_cost, epoch_recon_cost, epoch_kl_cost, \ - kl_weight, l2_cost, l2_weight - - - def run_epoch(self, datasets, ops_to_eval, kind="train", batch_size=None, - do_collect=True, keep_prob=None): - """Run the model through the entire dataset once. - - Args: - datasets: A dict of data dicts. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - ops_to_eval: A list of tensorflow operations that will be evaluated in - the tf.session.run() call. - batch_size (optional): The batch_size to use. - do_collect (optional): Should the routine collect all session.run - output as a list, and return it? - keep_prob (optional): The dropout keep probability. - - Returns: - A list of lists, the internal list is the return for the ops for each - session.run() call. The outer list collects over the epoch. - """ - hps = self.hps - all_name_example_idx_pairs = \ - self.shuffle_and_flatten_datasets(datasets, kind) - - kind_data = kind + '_data' - kind_ext_input = kind + '_ext_input' - - total_cost = total_recon_cost = total_kl_cost = 0.0 - session = tf.get_default_session() - epoch_size = len(all_name_example_idx_pairs) - evaled_ops_list = [] - for name, example_idxs in all_name_example_idx_pairs: - data_dict = datasets[name] - data_extxd = data_dict[kind_data] - if hps.output_dist == 'poisson' and hps.temporal_spike_jitter_width > 0: - data_extxd = self.shuffle_spikes_in_time(data_extxd) - - ext_input_extxi = data_dict[kind_ext_input] - data_bxtxd, ext_input_bxtxi = self.get_batch(data_extxd, ext_input_extxi, - example_idxs=example_idxs) - - feed_dict = self.build_feed_dict(name, data_bxtxd, ext_input_bxtxi, - keep_prob=keep_prob) - evaled_ops_np = session.run(ops_to_eval, feed_dict=feed_dict) - if do_collect: - evaled_ops_list.append(evaled_ops_np) - - return evaled_ops_list - - def summarize_all(self, datasets, summary_values): - """Plot and summarize stuff in tensorboard. - - Note that everything done in the current function is otherwise done on - a single, randomly selected dataset (except for summary_values, which are - passed in.) - - Args: - datasets, the dictionary of datasets used in the study. - summary_values: These summary values are created from the training loop, - and so summarize the entire set of datasets. - """ - hps = self.hps - tr_kl_cost = summary_values['tr_kl_cost'] - tr_recon_cost = summary_values['tr_recon_cost'] - tr_total_cost = summary_values['tr_total_cost'] - kl_weight = summary_values['kl_weight'] - l2_weight = summary_values['l2_weight'] - l2_cost = summary_values['l2_cost'] - has_any_valid_set = summary_values['has_any_valid_set'] - i = summary_values['nepochs'] - - session = tf.get_default_session() - train_summ, train_step = session.run([self.merged_train, - self.train_step], - feed_dict={self.l2_cost_ph:l2_cost, - self.kl_cost_ph:tr_kl_cost, - self.recon_cost_ph:tr_recon_cost, - self.total_cost_ph:tr_total_cost}) - self.writer.add_summary(train_summ, train_step) - if has_any_valid_set: - ev_kl_cost = summary_values['ev_kl_cost'] - ev_recon_cost = summary_values['ev_recon_cost'] - ev_total_cost = summary_values['ev_total_cost'] - eval_summ = session.run(self.merged_valid, - feed_dict={self.kl_cost_ph:ev_kl_cost, - self.recon_cost_ph:ev_recon_cost, - self.total_cost_ph:ev_total_cost}) - self.writer.add_summary(eval_summ, train_step) - print("Epoch:%d, step:%d (TRAIN, VALID): total: %.2f, %.2f\ - recon: %.2f, %.2f, kl: %.2f, %.2f, l2: %.5f,\ - kl weight: %.2f, l2 weight: %.2f" % \ - (i, train_step, tr_total_cost, ev_total_cost, - tr_recon_cost, ev_recon_cost, tr_kl_cost, ev_kl_cost, - l2_cost, kl_weight, l2_weight)) - - csv_outstr = "epoch,%d, step,%d, total,%.2f,%.2f, \ - recon,%.2f,%.2f, kl,%.2f,%.2f, l2,%.5f, \ - klweight,%.2f, l2weight,%.2f\n"% \ - (i, train_step, tr_total_cost, ev_total_cost, - tr_recon_cost, ev_recon_cost, tr_kl_cost, ev_kl_cost, - l2_cost, kl_weight, l2_weight) - - else: - print("Epoch:%d, step:%d TRAIN: total: %.2f recon: %.2f, kl: %.2f,\ - l2: %.5f, kl weight: %.2f, l2 weight: %.2f" % \ - (i, train_step, tr_total_cost, tr_recon_cost, tr_kl_cost, - l2_cost, kl_weight, l2_weight)) - csv_outstr = "epoch,%d, step,%d, total,%.2f, recon,%.2f, kl,%.2f, \ - l2,%.5f, klweight,%.2f, l2weight,%.2f\n"% \ - (i, train_step, tr_total_cost, tr_recon_cost, - tr_kl_cost, l2_cost, kl_weight, l2_weight) - - if self.hps.csv_log: - csv_file = os.path.join(self.hps.lfads_save_dir, self.hps.csv_log+'.csv') - with open(csv_file, "a") as myfile: - myfile.write(csv_outstr) - - - def plot_single_example(self, datasets): - """Plot an image relating to a randomly chosen, specific example. We use - posterior sample and average by taking one example, and filling a whole - batch with that example, sample from the posterior, and then average the - quantities. - - """ - hps = self.hps - all_data_names = datasets.keys() - data_name = np.random.permutation(all_data_names)[0] - data_dict = datasets[data_name] - has_valid_set = True if data_dict['valid_data'] is not None else False - cf = 1.0 # plotting concern - - # posterior sample and average here - E, _, _ = data_dict['train_data'].shape - eidx = np.random.choice(E) - example_idxs = eidx * np.ones(hps.batch_size, dtype=np.int32) - - train_data_bxtxd, train_ext_input_bxtxi = \ - self.get_batch(data_dict['train_data'], data_dict['train_ext_input'], - example_idxs=example_idxs) - - truth_train_data_bxtxd = None - if 'train_truth' in data_dict and data_dict['train_truth'] is not None: - truth_train_data_bxtxd, _ = self.get_batch(data_dict['train_truth'], - example_idxs=example_idxs) - cf = data_dict['conversion_factor'] - - # plotter does averaging - train_model_values = self.eval_model_runs_batch(data_name, - train_data_bxtxd, - train_ext_input_bxtxi, - do_average_batch=False) - - train_step = train_model_values['train_steps'] - feed_dict = self.build_feed_dict(data_name, train_data_bxtxd, - train_ext_input_bxtxi, keep_prob=1.0) - - session = tf.get_default_session() - generic_summ = session.run(self.merged_generic, feed_dict=feed_dict) - self.writer.add_summary(generic_summ, train_step) - - valid_data_bxtxd = valid_model_values = valid_ext_input_bxtxi = None - truth_valid_data_bxtxd = None - if has_valid_set: - E, _, _ = data_dict['valid_data'].shape - eidx = np.random.choice(E) - example_idxs = eidx * np.ones(hps.batch_size, dtype=np.int32) - valid_data_bxtxd, valid_ext_input_bxtxi = \ - self.get_batch(data_dict['valid_data'], - data_dict['valid_ext_input'], - example_idxs=example_idxs) - if 'valid_truth' in data_dict and data_dict['valid_truth'] is not None: - truth_valid_data_bxtxd, _ = self.get_batch(data_dict['valid_truth'], - example_idxs=example_idxs) - else: - truth_valid_data_bxtxd = None - - # plotter does averaging - valid_model_values = self.eval_model_runs_batch(data_name, - valid_data_bxtxd, - valid_ext_input_bxtxi, - do_average_batch=False) - - example_image = plot_lfads(train_bxtxd=train_data_bxtxd, - train_model_vals=train_model_values, - train_ext_input_bxtxi=train_ext_input_bxtxi, - train_truth_bxtxd=truth_train_data_bxtxd, - valid_bxtxd=valid_data_bxtxd, - valid_model_vals=valid_model_values, - valid_ext_input_bxtxi=valid_ext_input_bxtxi, - valid_truth_bxtxd=truth_valid_data_bxtxd, - bidx=None, cf=cf, output_dist=hps.output_dist) - example_image = np.expand_dims(example_image, axis=0) - example_summ = session.run(self.merged_examples, - feed_dict={self.example_image : example_image}) - self.writer.add_summary(example_summ) - - def train_model(self, datasets): - """Train the model, print per-epoch information, and save checkpoints. - - Loop over training epochs. The function that actually does the - training is train_epoch. This function iterates over the training - data, one epoch at a time. The learning rate schedule is such - that it will stay the same until the cost goes up in comparison to - the last few values, then it will drop. - - Args: - datasets: A dict of data dicts. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - """ - hps = self.hps - has_any_valid_set = False - for data_dict in datasets.values(): - if data_dict['valid_data'] is not None: - has_any_valid_set = True - break - - session = tf.get_default_session() - lr = session.run(self.learning_rate) - lr_stop = hps.learning_rate_stop - i = -1 - train_costs = [] - valid_costs = [] - ev_total_cost = ev_recon_cost = ev_kl_cost = 0.0 - lowest_ev_cost = np.Inf - while True: - i += 1 - do_save_ckpt = True if i % 10 ==0 else False - tr_total_cost, tr_recon_cost, tr_kl_cost, kl_weight, l2_cost, l2_weight = \ - self.train_epoch(datasets, do_save_ckpt=do_save_ckpt) - - # Evaluate the validation cost, and potentially save. Note that this - # routine will not save a validation checkpoint until the kl weight and - # l2 weights are equal to 1.0. - if has_any_valid_set: - ev_total_cost, ev_recon_cost, ev_kl_cost = \ - self.eval_cost_epoch(datasets, kind='valid') - valid_costs.append(ev_total_cost) - - # > 1 may give more consistent results, but not the actual lowest vae. - # == 1 gives the lowest vae seen so far. - n_lve = 1 - run_avg_lve = np.mean(valid_costs[-n_lve:]) - - # conditions for saving checkpoints: - # KL weight must have finished stepping (>=1.0), AND - # L2 weight must have finished stepping OR L2 is not being used, AND - # the current run has a lower LVE than previous runs AND - # len(valid_costs > n_lve) (not sure what that does) - if kl_weight >= 1.0 and \ - (l2_weight >= 1.0 or \ - (self.hps.l2_gen_scale == 0.0 and self.hps.l2_con_scale == 0.0)) \ - and (len(valid_costs) > n_lve and run_avg_lve < lowest_ev_cost): - - lowest_ev_cost = run_avg_lve - checkpoint_path = os.path.join(self.hps.lfads_save_dir, - self.hps.checkpoint_name + '_lve.ckpt') - self.lve_saver.save(session, checkpoint_path, - global_step=self.train_step, - latest_filename='checkpoint_lve') - - # Plot and summarize. - values = {'nepochs':i, 'has_any_valid_set': has_any_valid_set, - 'tr_total_cost':tr_total_cost, 'ev_total_cost':ev_total_cost, - 'tr_recon_cost':tr_recon_cost, 'ev_recon_cost':ev_recon_cost, - 'tr_kl_cost':tr_kl_cost, 'ev_kl_cost':ev_kl_cost, - 'l2_weight':l2_weight, 'kl_weight':kl_weight, - 'l2_cost':l2_cost} - self.summarize_all(datasets, values) - self.plot_single_example(datasets) - - # Manage learning rate. - train_res = tr_total_cost - n_lr = hps.learning_rate_n_to_compare - if len(train_costs) > n_lr and train_res > np.max(train_costs[-n_lr:]): - _ = session.run(self.learning_rate_decay_op) - lr = session.run(self.learning_rate) - print(" Decreasing learning rate to %f." % lr) - # Force the system to run n_lr times while at this lr. - train_costs.append(np.inf) - else: - train_costs.append(train_res) - - if lr < lr_stop: - print("Stopping optimization based on learning rate criteria.") - break - - def eval_cost_epoch(self, datasets, kind='train', ext_input_extxi=None, - batch_size=None): - """Evaluate the cost of the epoch. - - Args: - data_dict: The dictionary of data (training and validation) used for - training and evaluation of the model, respectively. - - Returns: - a 3 tuple of costs: - (epoch total cost, epoch reconstruction cost, epoch KL cost) - """ - ops_to_eval = [self.cost, self.recon_cost, self.kl_cost] - collected_op_values = self.run_epoch(datasets, ops_to_eval, kind=kind, - keep_prob=1.0) - - total_cost = total_recon_cost = total_kl_cost = 0.0 - # normalizing by batch done in distributions.py - epoch_size = len(collected_op_values) - for op_values in collected_op_values: - total_cost += op_values[0] - total_recon_cost += op_values[1] - total_kl_cost += op_values[2] - - epoch_total_cost = total_cost / epoch_size - epoch_recon_cost = total_recon_cost / epoch_size - epoch_kl_cost = total_kl_cost / epoch_size - - return epoch_total_cost, epoch_recon_cost, epoch_kl_cost - - def eval_model_runs_batch(self, data_name, data_bxtxd, ext_input_bxtxi=None, - do_eval_cost=False, do_average_batch=False): - """Returns all the goodies for the entire model, per batch. - - If data_bxtxd and ext_input_bxtxi can have fewer than batch_size along dim 1 - in which case this handles the padding and truncating automatically - - Args: - data_name: The name of the data dict, to select which in/out matrices - to use. - data_bxtxd: Numpy array training data with shape: - batch_size x # time steps x # dimensions - ext_input_bxtxi: Numpy array training external input with shape: - batch_size x # time steps x # external input dims - do_eval_cost (optional): If true, the IWAE (Importance Weighted - Autoencoder) log likeihood bound, instead of the VAE version. - do_average_batch (optional): average over the batch, useful for getting - good IWAE costs, and model outputs for a single data point. - - Returns: - A dictionary with the outputs of the model decoder, namely: - prior g0 mean, prior g0 variance, approx. posterior mean, approx - posterior mean, the generator initial conditions, the control inputs (if - enabled), the state of the generator, the factors, and the rates. - """ - session = tf.get_default_session() - - # if fewer than batch_size provided, pad to batch_size - hps = self.hps - batch_size = hps.batch_size - E, _, _ = data_bxtxd.shape - if E < hps.batch_size: - data_bxtxd = np.pad(data_bxtxd, ((0, hps.batch_size-E), (0, 0), (0, 0)), - mode='constant', constant_values=0) - if ext_input_bxtxi is not None: - ext_input_bxtxi = np.pad(ext_input_bxtxi, - ((0, hps.batch_size-E), (0, 0), (0, 0)), - mode='constant', constant_values=0) - - feed_dict = self.build_feed_dict(data_name, data_bxtxd, - ext_input_bxtxi, keep_prob=1.0) - - # Non-temporal signals will be batch x dim. - # Temporal signals are list length T with elements batch x dim. - tf_vals = [self.gen_ics, self.gen_states, self.factors, - self.output_dist_params] - tf_vals.append(self.cost) - tf_vals.append(self.nll_bound_vae) - tf_vals.append(self.nll_bound_iwae) - tf_vals.append(self.train_step) # not train_op! - if self.hps.ic_dim > 0: - tf_vals += [self.prior_zs_g0.mean, self.prior_zs_g0.logvar, - self.posterior_zs_g0.mean, self.posterior_zs_g0.logvar] - if self.hps.co_dim > 0: - tf_vals.append(self.controller_outputs) - tf_vals_flat, fidxs = flatten(tf_vals) - - np_vals_flat = session.run(tf_vals_flat, feed_dict=feed_dict) - - ff = 0 - gen_ics = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - gen_states = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - factors = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - out_dist_params = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - costs = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - nll_bound_vaes = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - nll_bound_iwaes = [np_vals_flat[f] for f in fidxs[ff]]; ff +=1 - train_steps = [np_vals_flat[f] for f in fidxs[ff]]; ff +=1 - if self.hps.ic_dim > 0: - prior_g0_mean = [np_vals_flat[f] for f in fidxs[ff]]; ff +=1 - prior_g0_logvar = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - post_g0_mean = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - post_g0_logvar = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - if self.hps.co_dim > 0: - controller_outputs = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - - # [0] are to take out the non-temporal items from lists - gen_ics = gen_ics[0] - costs = costs[0] - nll_bound_vaes = nll_bound_vaes[0] - nll_bound_iwaes = nll_bound_iwaes[0] - train_steps = train_steps[0] - - # Convert to full tensors, not lists of tensors in time dim. - gen_states = list_t_bxn_to_tensor_bxtxn(gen_states) - factors = list_t_bxn_to_tensor_bxtxn(factors) - out_dist_params = list_t_bxn_to_tensor_bxtxn(out_dist_params) - if self.hps.ic_dim > 0: - # select first time point - prior_g0_mean = prior_g0_mean[0] - prior_g0_logvar = prior_g0_logvar[0] - post_g0_mean = post_g0_mean[0] - post_g0_logvar = post_g0_logvar[0] - if self.hps.co_dim > 0: - controller_outputs = list_t_bxn_to_tensor_bxtxn(controller_outputs) - - # slice out the trials in case < batch_size provided - if E < hps.batch_size: - idx = np.arange(E) - gen_ics = gen_ics[idx, :] - gen_states = gen_states[idx, :] - factors = factors[idx, :, :] - out_dist_params = out_dist_params[idx, :, :] - if self.hps.ic_dim > 0: - prior_g0_mean = prior_g0_mean[idx, :] - prior_g0_logvar = prior_g0_logvar[idx, :] - post_g0_mean = post_g0_mean[idx, :] - post_g0_logvar = post_g0_logvar[idx, :] - if self.hps.co_dim > 0: - controller_outputs = controller_outputs[idx, :, :] - - if do_average_batch: - gen_ics = np.mean(gen_ics, axis=0) - gen_states = np.mean(gen_states, axis=0) - factors = np.mean(factors, axis=0) - out_dist_params = np.mean(out_dist_params, axis=0) - if self.hps.ic_dim > 0: - prior_g0_mean = np.mean(prior_g0_mean, axis=0) - prior_g0_logvar = np.mean(prior_g0_logvar, axis=0) - post_g0_mean = np.mean(post_g0_mean, axis=0) - post_g0_logvar = np.mean(post_g0_logvar, axis=0) - if self.hps.co_dim > 0: - controller_outputs = np.mean(controller_outputs, axis=0) - - model_vals = {} - model_vals['gen_ics'] = gen_ics - model_vals['gen_states'] = gen_states - model_vals['factors'] = factors - model_vals['output_dist_params'] = out_dist_params - model_vals['costs'] = costs - model_vals['nll_bound_vaes'] = nll_bound_vaes - model_vals['nll_bound_iwaes'] = nll_bound_iwaes - model_vals['train_steps'] = train_steps - if self.hps.ic_dim > 0: - model_vals['prior_g0_mean'] = prior_g0_mean - model_vals['prior_g0_logvar'] = prior_g0_logvar - model_vals['post_g0_mean'] = post_g0_mean - model_vals['post_g0_logvar'] = post_g0_logvar - if self.hps.co_dim > 0: - model_vals['controller_outputs'] = controller_outputs - - return model_vals - - def eval_model_runs_avg_epoch(self, data_name, data_extxd, - ext_input_extxi=None): - """Returns all the expected value for goodies for the entire model. - - The expected value is taken over hidden (z) variables, namely the initial - conditions and the control inputs. The expected value is approximate, and - accomplished via sampling (batch_size) samples for every examples. - - Args: - data_name: The name of the data dict, to select which in/out matrices - to use. - data_extxd: Numpy array training data with shape: - # examples x # time steps x # dimensions - ext_input_extxi (optional): Numpy array training external input with - shape: # examples x # time steps x # external input dims - - Returns: - A dictionary with the averaged outputs of the model decoder, namely: - prior g0 mean, prior g0 variance, approx. posterior mean, approx - posterior mean, the generator initial conditions, the control inputs (if - enabled), the state of the generator, the factors, and the output - distribution parameters, e.g. (rates or mean and variances). - """ - hps = self.hps - batch_size = hps.batch_size - E, T, D = data_extxd.shape - E_to_process = hps.ps_nexamples_to_process - if E_to_process > E: - E_to_process = E - - if hps.ic_dim > 0: - prior_g0_mean = np.zeros([E_to_process, hps.ic_dim]) - prior_g0_logvar = np.zeros([E_to_process, hps.ic_dim]) - post_g0_mean = np.zeros([E_to_process, hps.ic_dim]) - post_g0_logvar = np.zeros([E_to_process, hps.ic_dim]) - - if hps.co_dim > 0: - controller_outputs = np.zeros([E_to_process, T, hps.co_dim]) - gen_ics = np.zeros([E_to_process, hps.gen_dim]) - gen_states = np.zeros([E_to_process, T, hps.gen_dim]) - factors = np.zeros([E_to_process, T, hps.factors_dim]) - - if hps.output_dist == 'poisson': - out_dist_params = np.zeros([E_to_process, T, D]) - elif hps.output_dist == 'gaussian': - out_dist_params = np.zeros([E_to_process, T, D+D]) - else: - assert False, "NIY" - - costs = np.zeros(E_to_process) - nll_bound_vaes = np.zeros(E_to_process) - nll_bound_iwaes = np.zeros(E_to_process) - train_steps = np.zeros(E_to_process) - for es_idx in range(E_to_process): - print("Running %d of %d." % (es_idx+1, E_to_process)) - example_idxs = es_idx * np.ones(batch_size, dtype=np.int32) - data_bxtxd, ext_input_bxtxi = self.get_batch(data_extxd, - ext_input_extxi, - batch_size=batch_size, - example_idxs=example_idxs) - model_values = self.eval_model_runs_batch(data_name, data_bxtxd, - ext_input_bxtxi, - do_eval_cost=True, - do_average_batch=True) - - if self.hps.ic_dim > 0: - prior_g0_mean[es_idx,:] = model_values['prior_g0_mean'] - prior_g0_logvar[es_idx,:] = model_values['prior_g0_logvar'] - post_g0_mean[es_idx,:] = model_values['post_g0_mean'] - post_g0_logvar[es_idx,:] = model_values['post_g0_logvar'] - gen_ics[es_idx,:] = model_values['gen_ics'] - - if self.hps.co_dim > 0: - controller_outputs[es_idx,:,:] = model_values['controller_outputs'] - gen_states[es_idx,:,:] = model_values['gen_states'] - factors[es_idx,:,:] = model_values['factors'] - out_dist_params[es_idx,:,:] = model_values['output_dist_params'] - costs[es_idx] = model_values['costs'] - nll_bound_vaes[es_idx] = model_values['nll_bound_vaes'] - nll_bound_iwaes[es_idx] = model_values['nll_bound_iwaes'] - train_steps[es_idx] = model_values['train_steps'] - print('bound nll(vae): %.3f, bound nll(iwae): %.3f' \ - % (nll_bound_vaes[es_idx], nll_bound_iwaes[es_idx])) - - model_runs = {} - if self.hps.ic_dim > 0: - model_runs['prior_g0_mean'] = prior_g0_mean - model_runs['prior_g0_logvar'] = prior_g0_logvar - model_runs['post_g0_mean'] = post_g0_mean - model_runs['post_g0_logvar'] = post_g0_logvar - model_runs['gen_ics'] = gen_ics - - if self.hps.co_dim > 0: - model_runs['controller_outputs'] = controller_outputs - model_runs['gen_states'] = gen_states - model_runs['factors'] = factors - model_runs['output_dist_params'] = out_dist_params - model_runs['costs'] = costs - model_runs['nll_bound_vaes'] = nll_bound_vaes - model_runs['nll_bound_iwaes'] = nll_bound_iwaes - model_runs['train_steps'] = train_steps - return model_runs - - def eval_model_runs_push_mean(self, data_name, data_extxd, - ext_input_extxi=None): - """Returns values of interest for the model by pushing the means through - - The mean values for both initial conditions and the control inputs are - pushed through the model instead of sampling (as is done in - eval_model_runs_avg_epoch). - This is a quick and approximate version of estimating these values instead - of sampling from the posterior many times and then averaging those values of - interest. - - Internally, a total of batch_size trials are run through the model at once. - - Args: - data_name: The name of the data dict, to select which in/out matrices - to use. - data_extxd: Numpy array training data with shape: - # examples x # time steps x # dimensions - ext_input_extxi (optional): Numpy array training external input with - shape: # examples x # time steps x # external input dims - - Returns: - A dictionary with the estimated outputs of the model decoder, namely: - prior g0 mean, prior g0 variance, approx. posterior mean, approx - posterior mean, the generator initial conditions, the control inputs (if - enabled), the state of the generator, the factors, and the output - distribution parameters, e.g. (rates or mean and variances). - """ - hps = self.hps - batch_size = hps.batch_size - E, T, D = data_extxd.shape - E_to_process = hps.ps_nexamples_to_process - if E_to_process > E: - print("Setting number of posterior samples to process to : ", E) - E_to_process = E - - if hps.ic_dim > 0: - prior_g0_mean = np.zeros([E_to_process, hps.ic_dim]) - prior_g0_logvar = np.zeros([E_to_process, hps.ic_dim]) - post_g0_mean = np.zeros([E_to_process, hps.ic_dim]) - post_g0_logvar = np.zeros([E_to_process, hps.ic_dim]) - - if hps.co_dim > 0: - controller_outputs = np.zeros([E_to_process, T, hps.co_dim]) - gen_ics = np.zeros([E_to_process, hps.gen_dim]) - gen_states = np.zeros([E_to_process, T, hps.gen_dim]) - factors = np.zeros([E_to_process, T, hps.factors_dim]) - - if hps.output_dist == 'poisson': - out_dist_params = np.zeros([E_to_process, T, D]) - elif hps.output_dist == 'gaussian': - out_dist_params = np.zeros([E_to_process, T, D+D]) - else: - assert False, "NIY" - - costs = np.zeros(E_to_process) - nll_bound_vaes = np.zeros(E_to_process) - nll_bound_iwaes = np.zeros(E_to_process) - train_steps = np.zeros(E_to_process) - - # generator that will yield 0:N in groups of per items, e.g. - # (0:per-1), (per:2*per-1), ..., with the last group containing <= per items - # this will be used to feed per=batch_size trials into the model at a time - def trial_batches(N, per): - for i in range(0, N, per): - yield np.arange(i, min(i+per, N), dtype=np.int32) - - for batch_idx, es_idx in enumerate(trial_batches(E_to_process, - hps.batch_size)): - print("Running trial batch %d with %d trials" % (batch_idx+1, - len(es_idx))) - data_bxtxd, ext_input_bxtxi = self.get_batch(data_extxd, - ext_input_extxi, - batch_size=batch_size, - example_idxs=es_idx) - model_values = self.eval_model_runs_batch(data_name, data_bxtxd, - ext_input_bxtxi, - do_eval_cost=True, - do_average_batch=False) - - if self.hps.ic_dim > 0: - prior_g0_mean[es_idx,:] = model_values['prior_g0_mean'] - prior_g0_logvar[es_idx,:] = model_values['prior_g0_logvar'] - post_g0_mean[es_idx,:] = model_values['post_g0_mean'] - post_g0_logvar[es_idx,:] = model_values['post_g0_logvar'] - gen_ics[es_idx,:] = model_values['gen_ics'] - - if self.hps.co_dim > 0: - controller_outputs[es_idx,:,:] = model_values['controller_outputs'] - gen_states[es_idx,:,:] = model_values['gen_states'] - factors[es_idx,:,:] = model_values['factors'] - out_dist_params[es_idx,:,:] = model_values['output_dist_params'] - - # TODO - # model_values['costs'] and other costs come out as scalars, summed over - # all the trials in the batch. what we want is the per-trial costs - costs[es_idx] = model_values['costs'] - nll_bound_vaes[es_idx] = model_values['nll_bound_vaes'] - nll_bound_iwaes[es_idx] = model_values['nll_bound_iwaes'] - - train_steps[es_idx] = model_values['train_steps'] - - model_runs = {} - if self.hps.ic_dim > 0: - model_runs['prior_g0_mean'] = prior_g0_mean - model_runs['prior_g0_logvar'] = prior_g0_logvar - model_runs['post_g0_mean'] = post_g0_mean - model_runs['post_g0_logvar'] = post_g0_logvar - model_runs['gen_ics'] = gen_ics - - if self.hps.co_dim > 0: - model_runs['controller_outputs'] = controller_outputs - model_runs['gen_states'] = gen_states - model_runs['factors'] = factors - model_runs['output_dist_params'] = out_dist_params - - # You probably do not want the LL associated values when pushing the mean - # instead of sampling. - model_runs['costs'] = costs - model_runs['nll_bound_vaes'] = nll_bound_vaes - model_runs['nll_bound_iwaes'] = nll_bound_iwaes - model_runs['train_steps'] = train_steps - return model_runs - - def write_model_runs(self, datasets, output_fname=None, push_mean=False): - """Run the model on the data in data_dict, and save the computed values. - - LFADS generates a number of outputs for each examples, and these are all - saved. They are: - The mean and variance of the prior of g0. - The mean and variance of approximate posterior of g0. - The control inputs (if enabled). - The initial conditions, g0, for all examples. - The generator states for all time. - The factors for all time. - The output distribution parameters (e.g. rates) for all time. - - Args: - datasets: A dictionary of named data_dictionaries, see top of lfads.py - output_fname: a file name stem for the output files. - push_mean: If False (default), generates batch_size samples for each trial - and averages the results. if True, runs each trial once without noise, - pushing the posterior mean initial conditions and control inputs through - the trained model. False is used for posterior_sample_and_average, True - is used for posterior_push_mean. - """ - hps = self.hps - kind = hps.kind - - for data_name, data_dict in datasets.items(): - data_tuple = [('train', data_dict['train_data'], - data_dict['train_ext_input']), - ('valid', data_dict['valid_data'], - data_dict['valid_ext_input'])] - for data_kind, data_extxd, ext_input_extxi in data_tuple: - if not output_fname: - fname = "model_runs_" + data_name + '_' + data_kind + '_' + kind - else: - fname = output_fname + data_name + '_' + data_kind + '_' + kind - - print("Writing data for %s data and kind %s." % (data_name, data_kind)) - if push_mean: - model_runs = self.eval_model_runs_push_mean(data_name, data_extxd, - ext_input_extxi) - else: - model_runs = self.eval_model_runs_avg_epoch(data_name, data_extxd, - ext_input_extxi) - full_fname = os.path.join(hps.lfads_save_dir, fname) - write_data(full_fname, model_runs, compression='gzip') - print("Done.") - - def write_model_samples(self, dataset_name, output_fname=None): - """Use the prior distribution to generate batch_size number of samples - from the model. - - LFADS generates a number of outputs for each sample, and these are all - saved. They are: - The mean and variance of the prior of g0. - The control inputs (if enabled). - The initial conditions, g0, for all examples. - The generator states for all time. - The factors for all time. - The output distribution parameters (e.g. rates) for all time. - - Args: - dataset_name: The name of the dataset to grab the factors -> rates - alignment matrices from. - output_fname: The name of the file in which to save the generated - samples. - """ - hps = self.hps - batch_size = hps.batch_size - - print("Generating %d samples" % (batch_size)) - tf_vals = [self.factors, self.gen_states, self.gen_ics, - self.cost, self.output_dist_params] - if hps.ic_dim > 0: - tf_vals += [self.prior_zs_g0.mean, self.prior_zs_g0.logvar] - if hps.co_dim > 0: - tf_vals += [self.prior_zs_ar_con.samples_t] - tf_vals_flat, fidxs = flatten(tf_vals) - - session = tf.get_default_session() - feed_dict = {} - feed_dict[self.dataName] = dataset_name - feed_dict[self.keep_prob] = 1.0 - - np_vals_flat = session.run(tf_vals_flat, feed_dict=feed_dict) - - ff = 0 - factors = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - gen_states = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - gen_ics = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - costs = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - output_dist_params = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - if hps.ic_dim > 0: - prior_g0_mean = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - prior_g0_logvar = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - if hps.co_dim > 0: - prior_zs_ar_con = [np_vals_flat[f] for f in fidxs[ff]]; ff += 1 - - # [0] are to take out the non-temporal items from lists - gen_ics = gen_ics[0] - costs = costs[0] - - # Convert to full tensors, not lists of tensors in time dim. - gen_states = list_t_bxn_to_tensor_bxtxn(gen_states) - factors = list_t_bxn_to_tensor_bxtxn(factors) - output_dist_params = list_t_bxn_to_tensor_bxtxn(output_dist_params) - if hps.ic_dim > 0: - prior_g0_mean = prior_g0_mean[0] - prior_g0_logvar = prior_g0_logvar[0] - if hps.co_dim > 0: - prior_zs_ar_con = list_t_bxn_to_tensor_bxtxn(prior_zs_ar_con) - - model_vals = {} - model_vals['gen_ics'] = gen_ics - model_vals['gen_states'] = gen_states - model_vals['factors'] = factors - model_vals['output_dist_params'] = output_dist_params - model_vals['costs'] = costs.reshape(1) - if hps.ic_dim > 0: - model_vals['prior_g0_mean'] = prior_g0_mean - model_vals['prior_g0_logvar'] = prior_g0_logvar - if hps.co_dim > 0: - model_vals['prior_zs_ar_con'] = prior_zs_ar_con - - full_fname = os.path.join(hps.lfads_save_dir, output_fname) - write_data(full_fname, model_vals, compression='gzip') - print("Done.") - - @staticmethod - def eval_model_parameters(use_nested=True, include_strs=None): - """Evaluate and return all of the TF variables in the model. - - Args: - use_nested (optional): For returning values, use a nested dictoinary, based - on variable scoping, or return all variables in a flat dictionary. - include_strs (optional): A list of strings to use as a filter, to reduce the - number of variables returned. A variable name must contain at least one - string in include_strs as a sub-string in order to be returned. - - Returns: - The parameters of the model. This can be in a flat - dictionary, or a nested dictionary, where the nesting is by variable - scope. - """ - all_tf_vars = tf.global_variables() - session = tf.get_default_session() - all_tf_vars_eval = session.run(all_tf_vars) - vars_dict = {} - strs = ["LFADS"] - if include_strs: - strs += include_strs - - for i, (var, var_eval) in enumerate(zip(all_tf_vars, all_tf_vars_eval)): - if any(s in include_strs for s in var.name): - if not isinstance(var_eval, np.ndarray): # for H5PY - print(var.name, """ is not numpy array, saving as numpy array - with value: """, var_eval, type(var_eval)) - e = np.array(var_eval) - print(e, type(e)) - else: - e = var_eval - vars_dict[var.name] = e - - if not use_nested: - return vars_dict - - var_names = vars_dict.keys() - nested_vars_dict = {} - current_dict = nested_vars_dict - for v, var_name in enumerate(var_names): - var_split_name_list = var_name.split('/') - split_name_list_len = len(var_split_name_list) - current_dict = nested_vars_dict - for p, part in enumerate(var_split_name_list): - if p < split_name_list_len - 1: - if part in current_dict: - current_dict = current_dict[part] - else: - current_dict[part] = {} - current_dict = current_dict[part] - else: - current_dict[part] = vars_dict[var_name] - - return nested_vars_dict - - @staticmethod - def spikify_rates(rates_bxtxd): - """Randomly spikify underlying rates according a Poisson distribution - - Args: - rates_bxtxd: A numpy tensor with shape: - - Returns: - A numpy array with the same shape as rates_bxtxd, but with the event - counts. - """ - - B,T,N = rates_bxtxd.shape - assert all([B > 0, N > 0]), "problems" - - # Because the rates are changing, there is nesting - spikes_bxtxd = np.zeros([B,T,N], dtype=np.int32) - for b in range(B): - for t in range(T): - for n in range(N): - rate = rates_bxtxd[b,t,n] - count = np.random.poisson(rate) - spikes_bxtxd[b,t,n] = count - - return spikes_bxtxd diff --git a/research/lfads/plot_lfads.py b/research/lfads/plot_lfads.py deleted file mode 100644 index c4e1a0332ef..00000000000 --- a/research/lfads/plot_lfads.py +++ /dev/null @@ -1,181 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import matplotlib -matplotlib.use('Agg') -from matplotlib import pyplot as plt -import numpy as np -import tensorflow as tf - -def _plot_item(W, name, full_name, nspaces): - plt.figure() - if W.shape == (): - print(name, ": ", W) - elif W.shape[0] == 1: - plt.stem(W.T) - plt.title(full_name) - elif W.shape[1] == 1: - plt.stem(W) - plt.title(full_name) - else: - plt.imshow(np.abs(W), interpolation='nearest', cmap='jet'); - plt.colorbar() - plt.title(full_name) - - -def all_plot(d, full_name="", exclude="", nspaces=0): - """Recursively plot all the LFADS model parameters in the nested - dictionary.""" - for k, v in d.iteritems(): - this_name = full_name+"/"+k - if isinstance(v, dict): - all_plot(v, full_name=this_name, exclude=exclude, nspaces=nspaces+4) - else: - if exclude == "" or exclude not in this_name: - _plot_item(v, name=k, full_name=full_name+"/"+k, nspaces=nspaces+4) - - - -def plot_time_series(vals_bxtxn, bidx=None, n_to_plot=np.inf, scale=1.0, - color='r', title=None): - - if bidx is None: - vals_txn = np.mean(vals_bxtxn, axis=0) - else: - vals_txn = vals_bxtxn[bidx,:,:] - - T, N = vals_txn.shape - if n_to_plot > N: - n_to_plot = N - - plt.plot(vals_txn[:,0:n_to_plot] + scale*np.array(range(n_to_plot)), - color=color, lw=1.0) - plt.axis('tight') - if title: - plt.title(title) - - -def plot_lfads_timeseries(data_bxtxn, model_vals, ext_input_bxtxi=None, - truth_bxtxn=None, bidx=None, output_dist="poisson", - conversion_factor=1.0, subplot_cidx=0, - col_title=None): - - n_to_plot = 10 - scale = 1.0 - nrows = 7 - plt.subplot(nrows,2,1+subplot_cidx) - - if output_dist == 'poisson': - rates = means = conversion_factor * model_vals['output_dist_params'] - plot_time_series(rates, bidx, n_to_plot=n_to_plot, scale=scale, - title=col_title + " rates (LFADS - red, Truth - black)") - elif output_dist == 'gaussian': - means_vars = model_vals['output_dist_params'] - means, vars = np.split(means_vars,2, axis=2) # bxtxn - stds = np.sqrt(vars) - plot_time_series(means, bidx, n_to_plot=n_to_plot, scale=scale, - title=col_title + " means (LFADS - red, Truth - black)") - plot_time_series(means+stds, bidx, n_to_plot=n_to_plot, scale=scale, - color='c') - plot_time_series(means-stds, bidx, n_to_plot=n_to_plot, scale=scale, - color='c') - else: - assert 'NIY' - - - if truth_bxtxn is not None: - plot_time_series(truth_bxtxn, bidx, n_to_plot=n_to_plot, color='k', - scale=scale) - - input_title = "" - if "controller_outputs" in model_vals.keys(): - input_title += " Controller Output" - plt.subplot(nrows,2,3+subplot_cidx) - u_t = model_vals['controller_outputs'][0:-1] - plot_time_series(u_t, bidx, n_to_plot=n_to_plot, color='c', scale=1.0, - title=col_title + input_title) - - if ext_input_bxtxi is not None: - input_title += " External Input" - plot_time_series(ext_input_bxtxi, n_to_plot=n_to_plot, color='b', - scale=scale, title=col_title + input_title) - - plt.subplot(nrows,2,5+subplot_cidx) - plot_time_series(means, bidx, - n_to_plot=n_to_plot, scale=1.0, - title=col_title + " Spikes (LFADS - red, Spikes - black)") - plot_time_series(data_bxtxn, bidx, n_to_plot=n_to_plot, color='k', scale=1.0) - - plt.subplot(nrows,2,7+subplot_cidx) - plot_time_series(model_vals['factors'], bidx, n_to_plot=n_to_plot, color='b', - scale=2.0, title=col_title + " Factors") - - plt.subplot(nrows,2,9+subplot_cidx) - plot_time_series(model_vals['gen_states'], bidx, n_to_plot=n_to_plot, - color='g', scale=1.0, title=col_title + " Generator State") - - if bidx is not None: - data_nxt = data_bxtxn[bidx,:,:].T - params_nxt = model_vals['output_dist_params'][bidx,:,:].T - else: - data_nxt = np.mean(data_bxtxn, axis=0).T - params_nxt = np.mean(model_vals['output_dist_params'], axis=0).T - if output_dist == 'poisson': - means_nxt = params_nxt - elif output_dist == 'gaussian': # (means+vars) x time - means_nxt = np.vsplit(params_nxt,2)[0] # get means - else: - assert "NIY" - - plt.subplot(nrows,2,11+subplot_cidx) - plt.imshow(data_nxt, aspect='auto', interpolation='nearest') - plt.title(col_title + ' Data') - - plt.subplot(nrows,2,13+subplot_cidx) - plt.imshow(means_nxt, aspect='auto', interpolation='nearest') - plt.title(col_title + ' Means') - - -def plot_lfads(train_bxtxd, train_model_vals, - train_ext_input_bxtxi=None, train_truth_bxtxd=None, - valid_bxtxd=None, valid_model_vals=None, - valid_ext_input_bxtxi=None, valid_truth_bxtxd=None, - bidx=None, cf=1.0, output_dist='poisson'): - - # Plotting - f = plt.figure(figsize=(18,20), tight_layout=True) - plot_lfads_timeseries(train_bxtxd, train_model_vals, - train_ext_input_bxtxi, - truth_bxtxn=train_truth_bxtxd, - conversion_factor=cf, bidx=bidx, - output_dist=output_dist, col_title='Train') - plot_lfads_timeseries(valid_bxtxd, valid_model_vals, - valid_ext_input_bxtxi, - truth_bxtxn=valid_truth_bxtxd, - conversion_factor=cf, bidx=bidx, - output_dist=output_dist, - subplot_cidx=1, col_title='Valid') - - # Convert from figure to an numpy array width x height x 3 (last for RGB) - f.canvas.draw() - data = np.fromstring(f.canvas.tostring_rgb(), dtype=np.uint8, sep='') - data_wxhx3 = data.reshape(f.canvas.get_width_height()[::-1] + (3,)) - plt.close() - - return data_wxhx3 diff --git a/research/lfads/run_lfads.py b/research/lfads/run_lfads.py deleted file mode 100755 index bd1c0d5e4de..00000000000 --- a/research/lfads/run_lfads.py +++ /dev/null @@ -1,815 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from lfads import LFADS -import numpy as np -import os -import tensorflow as tf -import re -import utils -import sys -MAX_INT = sys.maxsize - -# Lots of hyperparameters, but most are pretty insensitive. The -# explanation of these hyperparameters is found below, in the flags -# session. - -CHECKPOINT_PB_LOAD_NAME = "checkpoint" -CHECKPOINT_NAME = "lfads_vae" -CSV_LOG = "fitlog" -OUTPUT_FILENAME_STEM = "" -DEVICE = "gpu:0" # "cpu:0", or other gpus, e.g. "gpu:1" -MAX_CKPT_TO_KEEP = 5 -MAX_CKPT_TO_KEEP_LVE = 5 -PS_NEXAMPLES_TO_PROCESS = MAX_INT # if larger than number of examples, process all -EXT_INPUT_DIM = 0 -IC_DIM = 64 -FACTORS_DIM = 50 -IC_ENC_DIM = 128 -GEN_DIM = 200 -GEN_CELL_INPUT_WEIGHT_SCALE = 1.0 -GEN_CELL_REC_WEIGHT_SCALE = 1.0 -CELL_WEIGHT_SCALE = 1.0 -BATCH_SIZE = 128 -LEARNING_RATE_INIT = 0.01 -LEARNING_RATE_DECAY_FACTOR = 0.95 -LEARNING_RATE_STOP = 0.00001 -LEARNING_RATE_N_TO_COMPARE = 6 -INJECT_EXT_INPUT_TO_GEN = False -DO_TRAIN_IO_ONLY = False -DO_TRAIN_ENCODER_ONLY = False -DO_RESET_LEARNING_RATE = False -FEEDBACK_FACTORS_OR_RATES = "factors" -DO_TRAIN_READIN = True - -# Calibrated just above the average value for the rnn synthetic data. -MAX_GRAD_NORM = 200.0 -CELL_CLIP_VALUE = 5.0 -KEEP_PROB = 0.95 -TEMPORAL_SPIKE_JITTER_WIDTH = 0 -OUTPUT_DISTRIBUTION = 'poisson' # 'poisson' or 'gaussian' -NUM_STEPS_FOR_GEN_IC = MAX_INT # set to num_steps if greater than num_steps - -DATA_DIR = "/tmp/rnn_synth_data_v1.0/" -DATA_FILENAME_STEM = "chaotic_rnn_inputs_g1p5" -LFADS_SAVE_DIR = "/tmp/lfads_chaotic_rnn_inputs_g1p5/" -CO_DIM = 1 -DO_CAUSAL_CONTROLLER = False -DO_FEED_FACTORS_TO_CONTROLLER = True -CONTROLLER_INPUT_LAG = 1 -PRIOR_AR_AUTOCORRELATION = 10.0 -PRIOR_AR_PROCESS_VAR = 0.1 -DO_TRAIN_PRIOR_AR_ATAU = True -DO_TRAIN_PRIOR_AR_NVAR = True -CI_ENC_DIM = 128 -CON_DIM = 128 -CO_PRIOR_VAR_SCALE = 0.1 -KL_INCREASE_STEPS = 2000 -L2_INCREASE_STEPS = 2000 -L2_GEN_SCALE = 2000.0 -L2_CON_SCALE = 0.0 -# scale of regularizer on time correlation of inferred inputs -CO_MEAN_CORR_SCALE = 0.0 -KL_IC_WEIGHT = 1.0 -KL_CO_WEIGHT = 1.0 -KL_START_STEP = 0 -L2_START_STEP = 0 -IC_PRIOR_VAR_MIN = 0.1 -IC_PRIOR_VAR_SCALE = 0.1 -IC_PRIOR_VAR_MAX = 0.1 -IC_POST_VAR_MIN = 0.0001 # protection from KL blowing up - -flags = tf.app.flags -flags.DEFINE_string("kind", "train", - "Type of model to build {train, \ - posterior_sample_and_average, \ - posterior_push_mean, \ - prior_sample, write_model_params") -flags.DEFINE_string("output_dist", OUTPUT_DISTRIBUTION, - "Type of output distribution, 'poisson' or 'gaussian'") -flags.DEFINE_boolean("allow_gpu_growth", False, - "If true, only allocate amount of memory needed for \ - Session. Otherwise, use full GPU memory.") - -# DATA -flags.DEFINE_string("data_dir", DATA_DIR, "Data for training") -flags.DEFINE_string("data_filename_stem", DATA_FILENAME_STEM, - "Filename stem for data dictionaries.") -flags.DEFINE_string("lfads_save_dir", LFADS_SAVE_DIR, "model save dir") -flags.DEFINE_string("checkpoint_pb_load_name", CHECKPOINT_PB_LOAD_NAME, - "Name of checkpoint files, use 'checkpoint_lve' for best \ - error") -flags.DEFINE_string("checkpoint_name", CHECKPOINT_NAME, - "Name of checkpoint files (.ckpt appended)") -flags.DEFINE_string("output_filename_stem", OUTPUT_FILENAME_STEM, - "Name of output file (postfix will be added)") -flags.DEFINE_string("device", DEVICE, - "Which device to use (default: \"gpu:0\", can also be \ - \"cpu:0\", \"gpu:1\", etc)") -flags.DEFINE_string("csv_log", CSV_LOG, - "Name of file to keep running log of fit likelihoods, \ - etc (.csv appended)") -flags.DEFINE_integer("max_ckpt_to_keep", MAX_CKPT_TO_KEEP, - "Max # of checkpoints to keep (rolling)") -flags.DEFINE_integer("ps_nexamples_to_process", PS_NEXAMPLES_TO_PROCESS, - "Number of examples to process for posterior sample and \ - average (not number of samples to average over).") -flags.DEFINE_integer("max_ckpt_to_keep_lve", MAX_CKPT_TO_KEEP_LVE, - "Max # of checkpoints to keep for lowest validation error \ - models (rolling)") -flags.DEFINE_integer("ext_input_dim", EXT_INPUT_DIM, "Dimension of external \ -inputs") -flags.DEFINE_integer("num_steps_for_gen_ic", NUM_STEPS_FOR_GEN_IC, - "Number of steps to train the generator initial conditon.") - - -# If there are observed inputs, there are two ways to add that observed -# input to the model. The first is by treating as something to be -# inferred, and thus encoding the observed input via the encoders, and then -# input to the generator via the "inferred inputs" channel. Second, one -# can input the input directly into the generator. This has the downside -# of making the generation process strictly dependent on knowing the -# observed input for any generated trial. -flags.DEFINE_boolean("inject_ext_input_to_gen", - INJECT_EXT_INPUT_TO_GEN, - "Should observed inputs be input to model via encoders, \ - or injected directly into generator?") - -# CELL - -# The combined recurrent and input weights of the encoder and -# controller cells are by default set to scale at ws/sqrt(#inputs), -# with ws=1.0. You can change this scaling with this parameter. -flags.DEFINE_float("cell_weight_scale", CELL_WEIGHT_SCALE, - "Input scaling for input weights in generator.") - - -# GENERATION - -# Note that the dimension of the initial conditions is separated from the -# dimensions of the generator initial conditions (and a linear matrix will -# adapt the shapes if necessary). This is just another way to control -# complexity. In all likelihood, setting the ic dims to the size of the -# generator hidden state is just fine. -flags.DEFINE_integer("ic_dim", IC_DIM, "Dimension of h0") -# Setting the dimensions of the factors to something smaller than the data -# dimension is a way to get a reduced dimensionality representation of your -# data. -flags.DEFINE_integer("factors_dim", FACTORS_DIM, - "Number of factors from generator") -flags.DEFINE_integer("ic_enc_dim", IC_ENC_DIM, - "Cell hidden size, encoder of h0") - -# Controlling the size of the generator is one way to control complexity of -# the dynamics (there is also l2, which will squeeze out unnecessary -# dynamics also). The modern deep learning approach is to make these cells -# as large as tolerable (from a waiting perspective), and then regularize -# them to death with drop out or whatever. I don't know if this is correct -# for the LFADS application or not. -flags.DEFINE_integer("gen_dim", GEN_DIM, - "Cell hidden size, generator.") -# The weights of the generator cell by default set to scale at -# ws/sqrt(#inputs), with ws=1.0. You can change ws for -# the input weights or the recurrent weights with these hyperparameters. -flags.DEFINE_float("gen_cell_input_weight_scale", GEN_CELL_INPUT_WEIGHT_SCALE, - "Input scaling for input weights in generator.") -flags.DEFINE_float("gen_cell_rec_weight_scale", GEN_CELL_REC_WEIGHT_SCALE, - "Input scaling for rec weights in generator.") - -# KL DISTRIBUTIONS -# If you don't know what you are donig here, please leave alone, the -# defaults should be fine for most cases, irregardless of other parameters. -# -# If you don't want the prior variance to be learned, set the -# following values to the same thing: ic_prior_var_min, -# ic_prior_var_scale, ic_prior_var_max. The prior mean will be -# learned regardless. -flags.DEFINE_float("ic_prior_var_min", IC_PRIOR_VAR_MIN, - "Minimum variance in posterior h0 codes.") -flags.DEFINE_float("ic_prior_var_scale", IC_PRIOR_VAR_SCALE, - "Variance of ic prior distribution") -flags.DEFINE_float("ic_prior_var_max", IC_PRIOR_VAR_MAX, - "Maximum variance of IC prior distribution.") -# If you really want to limit the information from encoder to decoder, -# Increase ic_post_var_min above 0.0. -flags.DEFINE_float("ic_post_var_min", IC_POST_VAR_MIN, - "Minimum variance of IC posterior distribution.") -flags.DEFINE_float("co_prior_var_scale", CO_PRIOR_VAR_SCALE, - "Variance of control input prior distribution.") - - -flags.DEFINE_float("prior_ar_atau", PRIOR_AR_AUTOCORRELATION, - "Initial autocorrelation of AR(1) priors.") -flags.DEFINE_float("prior_ar_nvar", PRIOR_AR_PROCESS_VAR, - "Initial noise variance for AR(1) priors.") -flags.DEFINE_boolean("do_train_prior_ar_atau", DO_TRAIN_PRIOR_AR_ATAU, - "Is the value for atau an init, or the constant value?") -flags.DEFINE_boolean("do_train_prior_ar_nvar", DO_TRAIN_PRIOR_AR_NVAR, - "Is the value for noise variance an init, or the constant \ - value?") - -# CONTROLLER -# This parameter critically controls whether or not there is a controller -# (along with controller encoders placed into the LFADS graph. If CO_DIM > -# 1, that means there is a 1 dimensional controller outputs, if equal to 0, -# then no controller. -flags.DEFINE_integer("co_dim", CO_DIM, - "Number of control net outputs (>0 builds that graph).") - -# The controller will be more powerful if it can see the encoding of the entire -# trial. However, this allows the controller to create inferred inputs that are -# acausal with respect to the actual data generation process. E.g. the data -# generator could have an input at time t, but the controller, after seeing the -# entirety of the trial could infer that the input is coming a little before -# time t, because there are no restrictions on the data the controller sees. -# One can force the controller to be causal (with respect to perturbations in -# the data generator) so that it only sees forward encodings of the data at time -# t that originate at times before or at time t. One can also control the data -# the controller sees by using an input lag (forward encoding at time [t-tlag] -# for controller input at time t. The same can be done in the reverse direction -# (controller input at time t from reverse encoding at time [t+tlag], in the -# case of an acausal controller). Setting this lag > 0 (even lag=1) can be a -# powerful way of avoiding very spiky decodes. Finally, one can manually control -# whether the factors at time t-1 are fed to the controller at time t. -# -# If you don't care about any of this, and just want to smooth your data, set -# do_causal_controller = False -# do_feed_factors_to_controller = True -# causal_input_lag = 0 -flags.DEFINE_boolean("do_causal_controller", - DO_CAUSAL_CONTROLLER, - "Restrict the controller create only causal inferred \ - inputs?") -# Strictly speaking, feeding either the factors or the rates to the controller -# violates causality, since the g0 gets to see all the data. This may or may not -# be only a theoretical concern. -flags.DEFINE_boolean("do_feed_factors_to_controller", - DO_FEED_FACTORS_TO_CONTROLLER, - "Should factors[t-1] be input to controller at time t?") -flags.DEFINE_string("feedback_factors_or_rates", FEEDBACK_FACTORS_OR_RATES, - "Feedback the factors or the rates to the controller? \ - Acceptable values: 'factors' or 'rates'.") -flags.DEFINE_integer("controller_input_lag", CONTROLLER_INPUT_LAG, - "Time lag on the encoding to controller t-lag for \ - forward, t+lag for reverse.") - -flags.DEFINE_integer("ci_enc_dim", CI_ENC_DIM, - "Cell hidden size, encoder of control inputs") -flags.DEFINE_integer("con_dim", CON_DIM, - "Cell hidden size, controller") - - -# OPTIMIZATION -flags.DEFINE_integer("batch_size", BATCH_SIZE, - "Batch size to use during training.") -flags.DEFINE_float("learning_rate_init", LEARNING_RATE_INIT, - "Learning rate initial value") -flags.DEFINE_float("learning_rate_decay_factor", LEARNING_RATE_DECAY_FACTOR, - "Learning rate decay, decay by this fraction every so \ - often.") -flags.DEFINE_float("learning_rate_stop", LEARNING_RATE_STOP, - "The lr is adaptively reduced, stop training at this value.") -# Rather put the learning rate on an exponentially decreasiong schedule, -# the current algorithm pays attention to the learning rate, and if it -# isn't regularly decreasing, it will decrease the learning rate. So far, -# it works fine, though it is not perfect. -flags.DEFINE_integer("learning_rate_n_to_compare", LEARNING_RATE_N_TO_COMPARE, - "Number of previous costs current cost has to be worse \ - than, to lower learning rate.") - -# This sets a value, above which, the gradients will be clipped. This hp -# is extremely useful to avoid an infrequent, but highly pathological -# problem whereby the gradient is so large that it destroys the -# optimziation by setting parameters too large, leading to a vicious cycle -# that ends in NaNs. If it's too large, it's useless, if it's too small, -# it essentially becomes the learning rate. It's pretty insensitive, though. -flags.DEFINE_float("max_grad_norm", MAX_GRAD_NORM, - "Max norm of gradient before clipping.") - -# If your optimizations start "NaN-ing out", reduce this value so that -# the values of the network don't grow out of control. Typically, once -# this parameter is set to a reasonable value, one stops having numerical -# problems. -flags.DEFINE_float("cell_clip_value", CELL_CLIP_VALUE, - "Max value recurrent cell can take before being clipped.") - -# This flag is used for an experiment where one sees if training a model with -# many days data can be used to learn the dynamics from a held-out days data. -# If you don't care about that particular experiment, this flag should always be -# false. -flags.DEFINE_boolean("do_train_io_only", DO_TRAIN_IO_ONLY, - "Train only the input (readin) and output (readout) \ - affine functions.") - -# This flag is used for an experiment where one wants to know if the dynamics -# learned by the generator generalize across conditions. In that case, you might -# train up a model on one set of data, and then only further train the encoder -# on another set of data (the conditions to be tested) so that the model is -# forced to use the same dynamics to describe that data. If you don't care about -# that particular experiment, this flag should always be false. -flags.DEFINE_boolean("do_train_encoder_only", DO_TRAIN_ENCODER_ONLY, - "Train only the encoder weights.") - -flags.DEFINE_boolean("do_reset_learning_rate", DO_RESET_LEARNING_RATE, - "Reset the learning rate to initial value.") - - -# for multi-session "stitching" models, the per-session readin matrices map from -# neurons to input factors which are fed into the shared encoder. These are -# initialized by alignment_matrix_cxf and alignment_bias_c in the input .h5 -# files. They can be fixed or made trainable. -flags.DEFINE_boolean("do_train_readin", DO_TRAIN_READIN, "Whether to train the \ - readin matrices and bias vectors. False leaves them fixed \ - at their initial values specified by the alignment \ - matrices and vectors.") - - -# OVERFITTING -# Dropout is done on the input data, on controller inputs (from -# encoder), on outputs from generator to factors. -flags.DEFINE_float("keep_prob", KEEP_PROB, "Dropout keep probability.") -# It appears that the system will happily fit spikes (blessing or -# curse, depending). You may not want this. Jittering the spikes a -# bit will help (-/+ bin size, as specified here). -flags.DEFINE_integer("temporal_spike_jitter_width", - TEMPORAL_SPIKE_JITTER_WIDTH, - "Shuffle spikes around this window.") - -# General note about helping ascribe controller inputs vs dynamics: -# -# If controller is heavily penalized, then it won't have any output. -# If dynamics are heavily penalized, then generator won't make -# dynamics. Note this l2 penalty is only on the recurrent portion of -# the RNNs, as dropout is also available, penalizing the feed-forward -# connections. -flags.DEFINE_float("l2_gen_scale", L2_GEN_SCALE, - "L2 regularization cost for the generator only.") -flags.DEFINE_float("l2_con_scale", L2_CON_SCALE, - "L2 regularization cost for the controller only.") -flags.DEFINE_float("co_mean_corr_scale", CO_MEAN_CORR_SCALE, - "Cost of correlation (thru time)in the means of \ - controller output.") - -# UNDERFITTING -# If the primary task of LFADS is "filtering" of data and not -# generation, then it is possible that the KL penalty is too strong. -# Empirically, we have found this to be the case. So we add a -# hyperparameter in front of the the two KL terms (one for the initial -# conditions to the generator, the other for the controller outputs). -# You should always think of the the default values as 1.0, and that -# leads to a standard VAE formulation whereby the numbers that are -# optimized are a lower-bound on the log-likelihood of the data. When -# these 2 HPs deviate from 1.0, one cannot make any statement about -# what those LL lower bounds mean anymore, and they cannot be compared -# (AFAIK). -flags.DEFINE_float("kl_ic_weight", KL_IC_WEIGHT, - "Strength of KL weight on initial conditions KL penatly.") -flags.DEFINE_float("kl_co_weight", KL_CO_WEIGHT, - "Strength of KL weight on controller output KL penalty.") - -# Sometimes the task can be sufficiently hard to learn that the -# optimizer takes the 'easy route', and simply minimizes the KL -# divergence, setting it to near zero, and the optimization gets -# stuck. These two parameters will help avoid that by by getting the -# optimization to 'latch' on to the main optimization, and only -# turning in the regularizers later. -flags.DEFINE_integer("kl_start_step", KL_START_STEP, - "Start increasing weight after this many steps.") -# training passes, not epochs, increase by 0.5 every kl_increase_steps -flags.DEFINE_integer("kl_increase_steps", KL_INCREASE_STEPS, - "Increase weight of kl cost to avoid local minimum.") -# Same story for l2 regularizer. One wants a simple generator, for scientific -# reasons, but not at the expense of hosing the optimization. -flags.DEFINE_integer("l2_start_step", L2_START_STEP, - "Start increasing l2 weight after this many steps.") -flags.DEFINE_integer("l2_increase_steps", L2_INCREASE_STEPS, - "Increase weight of l2 cost to avoid local minimum.") - -FLAGS = flags.FLAGS - - -def build_model(hps, kind="train", datasets=None): - """Builds a model from either random initialization, or saved parameters. - - Args: - hps: The hyper parameters for the model. - kind: (optional) The kind of model to build. Training vs inference require - different graphs. - datasets: The datasets structure (see top of lfads.py). - - Returns: - an LFADS model. - """ - - build_kind = kind - if build_kind == "write_model_params": - build_kind = "train" - with tf.variable_scope("LFADS", reuse=None): - model = LFADS(hps, kind=build_kind, datasets=datasets) - - if not os.path.exists(hps.lfads_save_dir): - print("Save directory %s does not exist, creating it." % hps.lfads_save_dir) - os.makedirs(hps.lfads_save_dir) - - cp_pb_ln = hps.checkpoint_pb_load_name - cp_pb_ln = 'checkpoint' if cp_pb_ln == "" else cp_pb_ln - if cp_pb_ln == 'checkpoint': - print("Loading latest training checkpoint in: ", hps.lfads_save_dir) - saver = model.seso_saver - elif cp_pb_ln == 'checkpoint_lve': - print("Loading lowest validation checkpoint in: ", hps.lfads_save_dir) - saver = model.lve_saver - else: - print("Loading checkpoint: ", cp_pb_ln, ", in: ", hps.lfads_save_dir) - saver = model.seso_saver - - ckpt = tf.train.get_checkpoint_state(hps.lfads_save_dir, - latest_filename=cp_pb_ln) - - session = tf.get_default_session() - print("ckpt: ", ckpt) - if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): - print("Reading model parameters from %s" % ckpt.model_checkpoint_path) - saver.restore(session, ckpt.model_checkpoint_path) - else: - print("Created model with fresh parameters.") - if kind in ["posterior_sample_and_average", "posterior_push_mean", - "prior_sample", "write_model_params"]: - print("Possible error!!! You are running ", kind, " on a newly \ - initialized model!") - # cannot print ckpt.model_check_point path if no ckpt - print("Are you sure you sure a checkpoint in ", hps.lfads_save_dir, - " exists?") - - tf.global_variables_initializer().run() - - if ckpt: - train_step_str = re.search('-[0-9]+$', ckpt.model_checkpoint_path).group() - else: - train_step_str = '-0' - - fname = 'hyperparameters' + train_step_str + '.txt' - hp_fname = os.path.join(hps.lfads_save_dir, fname) - hps_for_saving = jsonify_dict(hps) - utils.write_data(hp_fname, hps_for_saving, use_json=True) - - return model - - -def jsonify_dict(d): - """Turns python booleans into strings so hps dict can be written in json. - Creates a shallow-copied dictionary first, then accomplishes string - conversion. - - Args: - d: hyperparameter dictionary - - Returns: hyperparameter dictionary with bool's as strings - """ - - d2 = d.copy() # shallow copy is fine by assumption of d being shallow - def jsonify_bool(boolean_value): - if boolean_value: - return "true" - else: - return "false" - - for key in d2.keys(): - if isinstance(d2[key], bool): - d2[key] = jsonify_bool(d2[key]) - return d2 - - -def build_hyperparameter_dict(flags): - """Simple script for saving hyper parameters. Under the hood the - flags structure isn't a dictionary, so it has to be simplified since we - want to be able to view file as text. - - Args: - flags: From tf.app.flags - - Returns: - dictionary of hyper parameters (ignoring other flag types). - """ - d = {} - # Data - d['output_dist'] = flags.output_dist - d['data_dir'] = flags.data_dir - d['lfads_save_dir'] = flags.lfads_save_dir - d['checkpoint_pb_load_name'] = flags.checkpoint_pb_load_name - d['checkpoint_name'] = flags.checkpoint_name - d['output_filename_stem'] = flags.output_filename_stem - d['max_ckpt_to_keep'] = flags.max_ckpt_to_keep - d['max_ckpt_to_keep_lve'] = flags.max_ckpt_to_keep_lve - d['ps_nexamples_to_process'] = flags.ps_nexamples_to_process - d['ext_input_dim'] = flags.ext_input_dim - d['data_filename_stem'] = flags.data_filename_stem - d['device'] = flags.device - d['csv_log'] = flags.csv_log - d['num_steps_for_gen_ic'] = flags.num_steps_for_gen_ic - d['inject_ext_input_to_gen'] = flags.inject_ext_input_to_gen - # Cell - d['cell_weight_scale'] = flags.cell_weight_scale - # Generation - d['ic_dim'] = flags.ic_dim - d['factors_dim'] = flags.factors_dim - d['ic_enc_dim'] = flags.ic_enc_dim - d['gen_dim'] = flags.gen_dim - d['gen_cell_input_weight_scale'] = flags.gen_cell_input_weight_scale - d['gen_cell_rec_weight_scale'] = flags.gen_cell_rec_weight_scale - # KL distributions - d['ic_prior_var_min'] = flags.ic_prior_var_min - d['ic_prior_var_scale'] = flags.ic_prior_var_scale - d['ic_prior_var_max'] = flags.ic_prior_var_max - d['ic_post_var_min'] = flags.ic_post_var_min - d['co_prior_var_scale'] = flags.co_prior_var_scale - d['prior_ar_atau'] = flags.prior_ar_atau - d['prior_ar_nvar'] = flags.prior_ar_nvar - d['do_train_prior_ar_atau'] = flags.do_train_prior_ar_atau - d['do_train_prior_ar_nvar'] = flags.do_train_prior_ar_nvar - # Controller - d['do_causal_controller'] = flags.do_causal_controller - d['controller_input_lag'] = flags.controller_input_lag - d['do_feed_factors_to_controller'] = flags.do_feed_factors_to_controller - d['feedback_factors_or_rates'] = flags.feedback_factors_or_rates - d['co_dim'] = flags.co_dim - d['ci_enc_dim'] = flags.ci_enc_dim - d['con_dim'] = flags.con_dim - d['co_mean_corr_scale'] = flags.co_mean_corr_scale - # Optimization - d['batch_size'] = flags.batch_size - d['learning_rate_init'] = flags.learning_rate_init - d['learning_rate_decay_factor'] = flags.learning_rate_decay_factor - d['learning_rate_stop'] = flags.learning_rate_stop - d['learning_rate_n_to_compare'] = flags.learning_rate_n_to_compare - d['max_grad_norm'] = flags.max_grad_norm - d['cell_clip_value'] = flags.cell_clip_value - d['do_train_io_only'] = flags.do_train_io_only - d['do_train_encoder_only'] = flags.do_train_encoder_only - d['do_reset_learning_rate'] = flags.do_reset_learning_rate - d['do_train_readin'] = flags.do_train_readin - - # Overfitting - d['keep_prob'] = flags.keep_prob - d['temporal_spike_jitter_width'] = flags.temporal_spike_jitter_width - d['l2_gen_scale'] = flags.l2_gen_scale - d['l2_con_scale'] = flags.l2_con_scale - # Underfitting - d['kl_ic_weight'] = flags.kl_ic_weight - d['kl_co_weight'] = flags.kl_co_weight - d['kl_start_step'] = flags.kl_start_step - d['kl_increase_steps'] = flags.kl_increase_steps - d['l2_start_step'] = flags.l2_start_step - d['l2_increase_steps'] = flags.l2_increase_steps - d['_clip_value'] = 80 # bounds the tf.exp to avoid INF - - return d - - -class hps_dict_to_obj(dict): - """Helper class allowing us to access hps dictionary more easily.""" - - def __getattr__(self, key): - if key in self: - return self[key] - else: - assert False, ("%s does not exist." % key) - def __setattr__(self, key, value): - self[key] = value - - -def train(hps, datasets): - """Train the LFADS model. - - Args: - hps: The dictionary of hyperparameters. - datasets: A dictionary of data dictionaries. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - """ - model = build_model(hps, kind="train", datasets=datasets) - if hps.do_reset_learning_rate: - sess = tf.get_default_session() - sess.run(model.learning_rate.initializer) - - model.train_model(datasets) - - -def write_model_runs(hps, datasets, output_fname=None, push_mean=False): - """Run the model on the data in data_dict, and save the computed values. - - LFADS generates a number of outputs for each examples, and these are all - saved. They are: - The mean and variance of the prior of g0. - The mean and variance of approximate posterior of g0. - The control inputs (if enabled) - The initial conditions, g0, for all examples. - The generator states for all time. - The factors for all time. - The rates for all time. - - Args: - hps: The dictionary of hyperparameters. - datasets: A dictionary of data dictionaries. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - output_fname (optional): output filename stem to write the model runs. - push_mean: if False (default), generates batch_size samples for each trial - and averages the results. if True, runs each trial once without noise, - pushing the posterior mean initial conditions and control inputs through - the trained model. False is used for posterior_sample_and_average, True - is used for posterior_push_mean. - """ - model = build_model(hps, kind=hps.kind, datasets=datasets) - model.write_model_runs(datasets, output_fname, push_mean) - - -def write_model_samples(hps, datasets, dataset_name=None, output_fname=None): - """Use the prior distribution to generate samples from the model. - Generates batch_size number of samples (set through FLAGS). - - LFADS generates a number of outputs for each examples, and these are all - saved. They are: - The mean and variance of the prior of g0. - The control inputs (if enabled) - The initial conditions, g0, for all examples. - The generator states for all time. - The factors for all time. - The output distribution parameters (e.g. rates) for all time. - - Args: - hps: The dictionary of hyperparameters. - datasets: A dictionary of data dictionaries. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - dataset_name: The name of the dataset to grab the factors -> rates - alignment matrices from. Only a concern with models trained on - multi-session data. By default, uses the first dataset in the data dict. - output_fname: The name prefix of the file in which to save the generated - samples. - """ - if not output_fname: - output_fname = "model_runs_" + hps.kind - else: - output_fname = output_fname + "model_runs_" + hps.kind - if not dataset_name: - dataset_name = datasets.keys()[0] - else: - if dataset_name not in datasets.keys(): - raise ValueError("Invalid dataset name '%s'."%(dataset_name)) - model = build_model(hps, kind=hps.kind, datasets=datasets) - model.write_model_samples(dataset_name, output_fname) - - -def write_model_parameters(hps, output_fname=None, datasets=None): - """Save all the model parameters - - Save all the parameters to hps.lfads_save_dir. - - Args: - hps: The dictionary of hyperparameters. - output_fname: The prefix of the file in which to save the generated - samples. - datasets: A dictionary of data dictionaries. The dataset dict is simply a - name(string)-> data dictionary mapping (See top of lfads.py). - """ - if not output_fname: - output_fname = "model_params" - else: - output_fname = output_fname + "_model_params" - fname = os.path.join(hps.lfads_save_dir, output_fname) - print("Writing model parameters to: ", fname) - # save the optimizer params as well - model = build_model(hps, kind="write_model_params", datasets=datasets) - model_params = model.eval_model_parameters(use_nested=False, - include_strs="LFADS") - utils.write_data(fname, model_params, compression=None) - print("Done.") - - -def clean_data_dict(data_dict): - """Add some key/value pairs to the data dict, if they are missing. - Args: - data_dict - dictionary containing data for LFADS - Returns: - data_dict with some keys filled in, if they are absent. - """ - - keys = ['train_truth', 'train_ext_input', 'valid_data', - 'valid_truth', 'valid_ext_input', 'valid_train'] - for k in keys: - if k not in data_dict: - data_dict[k] = None - - return data_dict - - -def load_datasets(data_dir, data_filename_stem): - """Load the datasets from a specified directory. - - Example files look like - >data_dir/my_dataset_first_day - >data_dir/my_dataset_second_day - - If my_dataset (filename) stem is in the directory, the read routine will try - and load it. The datasets dictionary will then look like - dataset['first_day'] -> (first day data dictionary) - dataset['second_day'] -> (first day data dictionary) - - Args: - data_dir: The directory from which to load the datasets. - data_filename_stem: The stem of the filename for the datasets. - - Returns: - datasets: a dataset dictionary, with one name->data dictionary pair for - each dataset file. - """ - print("Reading data from ", data_dir) - datasets = utils.read_datasets(data_dir, data_filename_stem) - for k, data_dict in datasets.items(): - datasets[k] = clean_data_dict(data_dict) - - train_total_size = len(data_dict['train_data']) - if train_total_size == 0: - print("Did not load training set.") - else: - print("Found training set with number examples: ", train_total_size) - - valid_total_size = len(data_dict['valid_data']) - if valid_total_size == 0: - print("Did not load validation set.") - else: - print("Found validation set with number examples: ", valid_total_size) - - return datasets - - -def main(_): - """Get this whole shindig off the ground.""" - d = build_hyperparameter_dict(FLAGS) - hps = hps_dict_to_obj(d) # hyper parameters - kind = FLAGS.kind - - # Read the data, if necessary. - train_set = valid_set = None - if kind in ["train", "posterior_sample_and_average", "posterior_push_mean", - "prior_sample", "write_model_params"]: - datasets = load_datasets(hps.data_dir, hps.data_filename_stem) - else: - raise ValueError('Kind {} is not supported.'.format(kind)) - - # infer the dataset names and dataset dimensions from the loaded files - hps.kind = kind # needs to be added here, cuz not saved as hyperparam - hps.dataset_names = [] - hps.dataset_dims = {} - for key in datasets: - hps.dataset_names.append(key) - hps.dataset_dims[key] = datasets[key]['data_dim'] - - # also store down the dimensionality of the data - # - just pull from one set, required to be same for all sets - hps.num_steps = datasets.values()[0]['num_steps'] - hps.ndatasets = len(hps.dataset_names) - - if hps.num_steps_for_gen_ic > hps.num_steps: - hps.num_steps_for_gen_ic = hps.num_steps - - # Build and run the model, for varying purposes. - config = tf.ConfigProto(allow_soft_placement=True, - log_device_placement=False) - if FLAGS.allow_gpu_growth: - config.gpu_options.allow_growth = True - sess = tf.Session(config=config) - with sess.as_default(): - with tf.device(hps.device): - if kind == "train": - train(hps, datasets) - elif kind == "posterior_sample_and_average": - write_model_runs(hps, datasets, hps.output_filename_stem, - push_mean=False) - elif kind == "posterior_push_mean": - write_model_runs(hps, datasets, hps.output_filename_stem, - push_mean=True) - elif kind == "prior_sample": - write_model_samples(hps, datasets, hps.output_filename_stem) - elif kind == "write_model_params": - write_model_parameters(hps, hps.output_filename_stem, datasets) - else: - assert False, ("Kind %s is not implemented. " % kind) - - -if __name__ == "__main__": - tf.app.run() diff --git a/research/lfads/synth_data/generate_chaotic_rnn_data.py b/research/lfads/synth_data/generate_chaotic_rnn_data.py deleted file mode 100644 index 3de72e58b22..00000000000 --- a/research/lfads/synth_data/generate_chaotic_rnn_data.py +++ /dev/null @@ -1,200 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -from __future__ import print_function - -import h5py -import numpy as np -import os -import tensorflow as tf # used for flags here - -from utils import write_datasets -from synthetic_data_utils import add_alignment_projections, generate_data -from synthetic_data_utils import generate_rnn, get_train_n_valid_inds -from synthetic_data_utils import nparray_and_transpose -from synthetic_data_utils import spikify_data, gaussify_data, split_list_by_inds -import matplotlib -import matplotlib.pyplot as plt -import scipy.signal - -matplotlib.rcParams['image.interpolation'] = 'nearest' -DATA_DIR = "rnn_synth_data_v1.0" - -flags = tf.app.flags -flags.DEFINE_string("save_dir", "/tmp/" + DATA_DIR + "/", - "Directory for saving data.") -flags.DEFINE_string("datafile_name", "thits_data", - "Name of data file for input case.") -flags.DEFINE_string("noise_type", "poisson", "Noise type for data.") -flags.DEFINE_integer("synth_data_seed", 5, "Random seed for RNN generation.") -flags.DEFINE_float("T", 1.0, "Time in seconds to generate.") -flags.DEFINE_integer("C", 100, "Number of conditions") -flags.DEFINE_integer("N", 50, "Number of units for the RNN") -flags.DEFINE_integer("S", 50, "Number of sampled units from RNN") -flags.DEFINE_integer("npcs", 10, "Number of PCS for multi-session case.") -flags.DEFINE_float("train_percentage", 4.0/5.0, - "Percentage of train vs validation trials") -flags.DEFINE_integer("nreplications", 40, - "Number of noise replications of the same underlying rates.") -flags.DEFINE_float("g", 1.5, "Complexity of dynamics") -flags.DEFINE_float("x0_std", 1.0, - "Volume from which to pull initial conditions (affects diversity of dynamics.") -flags.DEFINE_float("tau", 0.025, "Time constant of RNN") -flags.DEFINE_float("dt", 0.010, "Time bin") -flags.DEFINE_float("input_magnitude", 20.0, - "For the input case, what is the value of the input?") -flags.DEFINE_float("max_firing_rate", 30.0, "Map 1.0 of RNN to a spikes per second") -FLAGS = flags.FLAGS - - -# Note that with N small, (as it is 25 above), the finite size effects -# will have pretty dramatic effects on the dynamics of the random RNN. -# If you want more complex dynamics, you'll have to run the script a -# lot, or increase N (or g). - -# Getting hard vs. easy data can be a little stochastic, so we set the seed. - -# Pull out some commonly used parameters. -# These are user parameters (configuration) -rng = np.random.RandomState(seed=FLAGS.synth_data_seed) -T = FLAGS.T -C = FLAGS.C -N = FLAGS.N -S = FLAGS.S -input_magnitude = FLAGS.input_magnitude -nreplications = FLAGS.nreplications -E = nreplications * C # total number of trials -# S is the number of measurements in each datasets, w/ each -# dataset having a different set of observations. -ndatasets = N/S # ok if rounded down -train_percentage = FLAGS.train_percentage -ntime_steps = int(T / FLAGS.dt) -# End of user parameters - -rnn = generate_rnn(rng, N, FLAGS.g, FLAGS.tau, FLAGS.dt, FLAGS.max_firing_rate) - -# Check to make sure the RNN is the one we used in the paper. -if N == 50: - assert abs(rnn['W'][0,0] - 0.06239899) < 1e-8, 'Error in random seed?' - rem_check = nreplications * train_percentage - assert abs(rem_check - int(rem_check)) < 1e-8, \ - 'Train percentage * nreplications should be integral number.' - - -# Initial condition generation, and condition label generation. This -# happens outside of the dataset loop, so that all datasets have the -# same conditions, which is similar to a neurophys setup. -condition_number = 0 -x0s = [] -condition_labels = [] -for c in range(C): - x0 = FLAGS.x0_std * rng.randn(N, 1) - x0s.append(np.tile(x0, nreplications)) # replicate x0 nreplications times - # replicate the condition label nreplications times - for ns in range(nreplications): - condition_labels.append(condition_number) - condition_number += 1 -x0s = np.concatenate(x0s, axis=1) - -# Containers for storing data across data. -datasets = {} -for n in range(ndatasets): - print(n+1, " of ", ndatasets) - - # First generate all firing rates. in the next loop, generate all - # replications this allows the random state for rate generation to be - # independent of n_replications. - dataset_name = 'dataset_N' + str(N) + '_S' + str(S) - if S < N: - dataset_name += '_n' + str(n+1) - - # Sample neuron subsets. The assumption is the PC axes of the RNN - # are not unit aligned, so sampling units is adequate to sample all - # the high-variance PCs. - P_sxn = np.eye(S,N) - for m in range(n): - P_sxn = np.roll(P_sxn, S, axis=1) - - if input_magnitude > 0.0: - # time of "hits" randomly chosen between [1/4 and 3/4] of total time - input_times = rng.choice(int(ntime_steps/2), size=[E]) + int(ntime_steps/4) - else: - input_times = None - - rates, x0s, inputs = \ - generate_data(rnn, T=T, E=E, x0s=x0s, P_sxn=P_sxn, - input_magnitude=input_magnitude, - input_times=input_times) - - if FLAGS.noise_type == "poisson": - noisy_data = spikify_data(rates, rng, rnn['dt'], rnn['max_firing_rate']) - elif FLAGS.noise_type == "gaussian": - noisy_data = gaussify_data(rates, rng, rnn['dt'], rnn['max_firing_rate']) - else: - raise ValueError("Only noise types supported are poisson or gaussian") - - # split into train and validation sets - train_inds, valid_inds = get_train_n_valid_inds(E, train_percentage, - nreplications) - - # Split the data, inputs, labels and times into train vs. validation. - rates_train, rates_valid = \ - split_list_by_inds(rates, train_inds, valid_inds) - noisy_data_train, noisy_data_valid = \ - split_list_by_inds(noisy_data, train_inds, valid_inds) - input_train, inputs_valid = \ - split_list_by_inds(inputs, train_inds, valid_inds) - condition_labels_train, condition_labels_valid = \ - split_list_by_inds(condition_labels, train_inds, valid_inds) - input_times_train, input_times_valid = \ - split_list_by_inds(input_times, train_inds, valid_inds) - - # Turn rates, noisy_data, and input into numpy arrays. - rates_train = nparray_and_transpose(rates_train) - rates_valid = nparray_and_transpose(rates_valid) - noisy_data_train = nparray_and_transpose(noisy_data_train) - noisy_data_valid = nparray_and_transpose(noisy_data_valid) - input_train = nparray_and_transpose(input_train) - inputs_valid = nparray_and_transpose(inputs_valid) - - # Note that we put these 'truth' rates and input into this - # structure, the only data that is used in LFADS are the noisy - # data e.g. spike trains. The rest is either for printing or posterity. - data = {'train_truth': rates_train, - 'valid_truth': rates_valid, - 'input_train_truth' : input_train, - 'input_valid_truth' : inputs_valid, - 'train_data' : noisy_data_train, - 'valid_data' : noisy_data_valid, - 'train_percentage' : train_percentage, - 'nreplications' : nreplications, - 'dt' : rnn['dt'], - 'input_magnitude' : input_magnitude, - 'input_times_train' : input_times_train, - 'input_times_valid' : input_times_valid, - 'P_sxn' : P_sxn, - 'condition_labels_train' : condition_labels_train, - 'condition_labels_valid' : condition_labels_valid, - 'conversion_factor': 1.0 / rnn['conversion_factor']} - datasets[dataset_name] = data - -if S < N: - # Note that this isn't necessary for this synthetic example, but - # it's useful to see how the input factor matrices were initialized - # for actual neurophysiology data. - datasets = add_alignment_projections(datasets, npcs=FLAGS.npcs) - -# Write out the datasets. -write_datasets(FLAGS.save_dir, FLAGS.datafile_name, datasets) diff --git a/research/lfads/synth_data/generate_itb_data.py b/research/lfads/synth_data/generate_itb_data.py deleted file mode 100644 index 66bc45d02e9..00000000000 --- a/research/lfads/synth_data/generate_itb_data.py +++ /dev/null @@ -1,209 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -from __future__ import print_function - -import h5py -import numpy as np -import os -from six.moves import xrange -import tensorflow as tf - -from utils import write_datasets -from synthetic_data_utils import normalize_rates -from synthetic_data_utils import get_train_n_valid_inds, nparray_and_transpose -from synthetic_data_utils import spikify_data, split_list_by_inds - -DATA_DIR = "rnn_synth_data_v1.0" - -flags = tf.app.flags -flags.DEFINE_string("save_dir", "/tmp/" + DATA_DIR + "/", - "Directory for saving data.") -flags.DEFINE_string("datafile_name", "itb_rnn", - "Name of data file for input case.") -flags.DEFINE_integer("synth_data_seed", 5, "Random seed for RNN generation.") -flags.DEFINE_float("T", 1.0, "Time in seconds to generate.") -flags.DEFINE_integer("C", 800, "Number of conditions") -flags.DEFINE_integer("N", 50, "Number of units for the RNN") -flags.DEFINE_float("train_percentage", 4.0/5.0, - "Percentage of train vs validation trials") -flags.DEFINE_integer("nreplications", 5, - "Number of spikifications of the same underlying rates.") -flags.DEFINE_float("tau", 0.025, "Time constant of RNN") -flags.DEFINE_float("dt", 0.010, "Time bin") -flags.DEFINE_float("max_firing_rate", 30.0, - "Map 1.0 of RNN to a spikes per second") -flags.DEFINE_float("u_std", 0.25, - "Std dev of input to integration to bound model") -flags.DEFINE_string("checkpoint_path", "SAMPLE_CHECKPOINT", - """Path to directory with checkpoints of model - trained on integration to bound task. Currently this - is a placeholder which tells the code to grab the - checkpoint that is provided with the code - (in /trained_itb/..). If you have your own checkpoint - you would like to restore, you would point it to - that path.""") -FLAGS = flags.FLAGS - - -class IntegrationToBoundModel: - def __init__(self, N): - scale = 0.8 / float(N**0.5) - self.N = N - self.Wh_nxn = tf.Variable(tf.random_normal([N, N], stddev=scale)) - self.b_1xn = tf.Variable(tf.zeros([1, N])) - self.Bu_1xn = tf.Variable(tf.zeros([1, N])) - self.Wro_nxo = tf.Variable(tf.random_normal([N, 1], stddev=scale)) - self.bro_o = tf.Variable(tf.zeros([1])) - - def call(self, h_tm1_bxn, u_bx1): - act_t_bxn = tf.matmul(h_tm1_bxn, self.Wh_nxn) + self.b_1xn + u_bx1 * self.Bu_1xn - h_t_bxn = tf.nn.tanh(act_t_bxn) - z_t = tf.nn.xw_plus_b(h_t_bxn, self.Wro_nxo, self.bro_o) - return z_t, h_t_bxn - -def get_data_batch(batch_size, T, rng, u_std): - u_bxt = rng.randn(batch_size, T) * u_std - running_sum_b = np.zeros([batch_size]) - labels_bxt = np.zeros([batch_size, T]) - for t in xrange(T): - running_sum_b += u_bxt[:, t] - labels_bxt[:, t] += running_sum_b - labels_bxt = np.clip(labels_bxt, -1, 1) - return u_bxt, labels_bxt - - -rng = np.random.RandomState(seed=FLAGS.synth_data_seed) -u_rng = np.random.RandomState(seed=FLAGS.synth_data_seed+1) -T = FLAGS.T -C = FLAGS.C -N = FLAGS.N # must be same N as in trained model (provided example is N = 50) -nreplications = FLAGS.nreplications -E = nreplications * C # total number of trials -train_percentage = FLAGS.train_percentage -ntimesteps = int(T / FLAGS.dt) -batch_size = 1 # gives one example per ntrial - -model = IntegrationToBoundModel(N) -inputs_ph_t = [tf.placeholder(tf.float32, - shape=[None, 1]) for _ in range(ntimesteps)] -state = tf.zeros([batch_size, N]) -saver = tf.train.Saver() - -P_nxn = rng.randn(N,N) / np.sqrt(N) # random projections - -# unroll RNN for T timesteps -outputs_t = [] -states_t = [] - -for inp in inputs_ph_t: - output, state = model.call(state, inp) - outputs_t.append(output) - states_t.append(state) - -with tf.Session() as sess: - # restore the latest model ckpt - if FLAGS.checkpoint_path == "SAMPLE_CHECKPOINT": - dir_path = os.path.dirname(os.path.realpath(__file__)) - model_checkpoint_path = os.path.join(dir_path, "trained_itb/model-65000") - else: - model_checkpoint_path = FLAGS.checkpoint_path - try: - saver.restore(sess, model_checkpoint_path) - print ('Model restored from', model_checkpoint_path) - except: - assert False, ("No checkpoints to restore from, is the path %s correct?" - %model_checkpoint_path) - - # generate data for trials - data_e = [] - u_e = [] - outs_e = [] - for c in range(C): - u_1xt, outs_1xt = get_data_batch(batch_size, ntimesteps, u_rng, FLAGS.u_std) - - feed_dict = {} - for t in xrange(ntimesteps): - feed_dict[inputs_ph_t[t]] = np.reshape(u_1xt[:,t], (batch_size,-1)) - - states_t_bxn, outputs_t_bxn = sess.run([states_t, outputs_t], - feed_dict=feed_dict) - states_nxt = np.transpose(np.squeeze(np.asarray(states_t_bxn))) - outputs_t_bxn = np.squeeze(np.asarray(outputs_t_bxn)) - r_sxt = np.dot(P_nxn, states_nxt) - - for s in xrange(nreplications): - data_e.append(r_sxt) - u_e.append(u_1xt) - outs_e.append(outputs_t_bxn) - - truth_data_e = normalize_rates(data_e, E, N) - -spiking_data_e = spikify_data(truth_data_e, rng, dt=FLAGS.dt, - max_firing_rate=FLAGS.max_firing_rate) -train_inds, valid_inds = get_train_n_valid_inds(E, train_percentage, - nreplications) - -data_train_truth, data_valid_truth = split_list_by_inds(truth_data_e, - train_inds, - valid_inds) -data_train_spiking, data_valid_spiking = split_list_by_inds(spiking_data_e, - train_inds, - valid_inds) - -data_train_truth = nparray_and_transpose(data_train_truth) -data_valid_truth = nparray_and_transpose(data_valid_truth) -data_train_spiking = nparray_and_transpose(data_train_spiking) -data_valid_spiking = nparray_and_transpose(data_valid_spiking) - -# save down the inputs used to generate this data -train_inputs_u, valid_inputs_u = split_list_by_inds(u_e, - train_inds, - valid_inds) -train_inputs_u = nparray_and_transpose(train_inputs_u) -valid_inputs_u = nparray_and_transpose(valid_inputs_u) - -# save down the network outputs (may be useful later) -train_outputs_u, valid_outputs_u = split_list_by_inds(outs_e, - train_inds, - valid_inds) -train_outputs_u = np.array(train_outputs_u) -valid_outputs_u = np.array(valid_outputs_u) - - -data = { 'train_truth': data_train_truth, - 'valid_truth': data_valid_truth, - 'train_data' : data_train_spiking, - 'valid_data' : data_valid_spiking, - 'train_percentage' : train_percentage, - 'nreplications' : nreplications, - 'dt' : FLAGS.dt, - 'u_std' : FLAGS.u_std, - 'max_firing_rate': FLAGS.max_firing_rate, - 'train_inputs_u': train_inputs_u, - 'valid_inputs_u': valid_inputs_u, - 'train_outputs_u': train_outputs_u, - 'valid_outputs_u': valid_outputs_u, - 'conversion_factor' : FLAGS.max_firing_rate/(1.0/FLAGS.dt) } - -# just one dataset here -datasets = {} -dataset_name = 'dataset_N' + str(N) -datasets[dataset_name] = data - -# write out the dataset -write_datasets(FLAGS.save_dir, FLAGS.datafile_name, datasets) -print ('Saved to ', os.path.join(FLAGS.save_dir, - FLAGS.datafile_name + '_' + dataset_name)) diff --git a/research/lfads/synth_data/generate_labeled_rnn_data.py b/research/lfads/synth_data/generate_labeled_rnn_data.py deleted file mode 100644 index 06955854865..00000000000 --- a/research/lfads/synth_data/generate_labeled_rnn_data.py +++ /dev/null @@ -1,147 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -from __future__ import print_function - -import os -import h5py -import numpy as np -from six.moves import xrange - -from synthetic_data_utils import generate_data, generate_rnn -from synthetic_data_utils import get_train_n_valid_inds -from synthetic_data_utils import nparray_and_transpose -from synthetic_data_utils import spikify_data, split_list_by_inds -import tensorflow as tf -from utils import write_datasets - -DATA_DIR = "rnn_synth_data_v1.0" - -flags = tf.app.flags -flags.DEFINE_string("save_dir", "/tmp/" + DATA_DIR + "/", - "Directory for saving data.") -flags.DEFINE_string("datafile_name", "conditioned_rnn_data", - "Name of data file for input case.") -flags.DEFINE_integer("synth_data_seed", 5, "Random seed for RNN generation.") -flags.DEFINE_float("T", 1.0, "Time in seconds to generate.") -flags.DEFINE_integer("C", 400, "Number of conditions") -flags.DEFINE_integer("N", 50, "Number of units for the RNN") -flags.DEFINE_float("train_percentage", 4.0/5.0, - "Percentage of train vs validation trials") -flags.DEFINE_integer("nreplications", 10, - "Number of spikifications of the same underlying rates.") -flags.DEFINE_float("g", 1.5, "Complexity of dynamics") -flags.DEFINE_float("x0_std", 1.0, - "Volume from which to pull initial conditions (affects diversity of dynamics.") -flags.DEFINE_float("tau", 0.025, "Time constant of RNN") -flags.DEFINE_float("dt", 0.010, "Time bin") -flags.DEFINE_float("max_firing_rate", 30.0, "Map 1.0 of RNN to a spikes per second") -FLAGS = flags.FLAGS - -rng = np.random.RandomState(seed=FLAGS.synth_data_seed) -rnn_rngs = [np.random.RandomState(seed=FLAGS.synth_data_seed+1), - np.random.RandomState(seed=FLAGS.synth_data_seed+2)] -T = FLAGS.T -C = FLAGS.C -N = FLAGS.N -nreplications = FLAGS.nreplications -E = nreplications * C -train_percentage = FLAGS.train_percentage -ntimesteps = int(T / FLAGS.dt) - -rnn_a = generate_rnn(rnn_rngs[0], N, FLAGS.g, FLAGS.tau, FLAGS.dt, - FLAGS.max_firing_rate) -rnn_b = generate_rnn(rnn_rngs[1], N, FLAGS.g, FLAGS.tau, FLAGS.dt, - FLAGS.max_firing_rate) -rnns = [rnn_a, rnn_b] - -# pick which RNN is used on each trial -rnn_to_use = rng.randint(2, size=E) -ext_input = np.repeat(np.expand_dims(rnn_to_use, axis=1), ntimesteps, axis=1) -ext_input = np.expand_dims(ext_input, axis=2) # these are "a's" in the paper - -x0s = [] -condition_labels = [] -condition_number = 0 -for c in range(C): - x0 = FLAGS.x0_std * rng.randn(N, 1) - x0s.append(np.tile(x0, nreplications)) - for ns in range(nreplications): - condition_labels.append(condition_number) - condition_number += 1 -x0s = np.concatenate(x0s, axis=1) - -P_nxn = rng.randn(N, N) / np.sqrt(N) - -# generate trials for both RNNs -rates_a, x0s_a, _ = generate_data(rnn_a, T=T, E=E, x0s=x0s, P_sxn=P_nxn, - input_magnitude=0.0, input_times=None) -spikes_a = spikify_data(rates_a, rng, rnn_a['dt'], rnn_a['max_firing_rate']) - -rates_b, x0s_b, _ = generate_data(rnn_b, T=T, E=E, x0s=x0s, P_sxn=P_nxn, - input_magnitude=0.0, input_times=None) -spikes_b = spikify_data(rates_b, rng, rnn_b['dt'], rnn_b['max_firing_rate']) - -# not the best way to do this but E is small enough -rates = [] -spikes = [] -for trial in xrange(E): - if rnn_to_use[trial] == 0: - rates.append(rates_a[trial]) - spikes.append(spikes_a[trial]) - else: - rates.append(rates_b[trial]) - spikes.append(spikes_b[trial]) - -# split into train and validation sets -train_inds, valid_inds = get_train_n_valid_inds(E, train_percentage, - nreplications) - -rates_train, rates_valid = split_list_by_inds(rates, train_inds, valid_inds) -spikes_train, spikes_valid = split_list_by_inds(spikes, train_inds, valid_inds) -condition_labels_train, condition_labels_valid = split_list_by_inds( - condition_labels, train_inds, valid_inds) -ext_input_train, ext_input_valid = split_list_by_inds( - ext_input, train_inds, valid_inds) - -rates_train = nparray_and_transpose(rates_train) -rates_valid = nparray_and_transpose(rates_valid) -spikes_train = nparray_and_transpose(spikes_train) -spikes_valid = nparray_and_transpose(spikes_valid) - -# add train_ext_input and valid_ext input -data = {'train_truth': rates_train, - 'valid_truth': rates_valid, - 'train_data' : spikes_train, - 'valid_data' : spikes_valid, - 'train_ext_input' : np.array(ext_input_train), - 'valid_ext_input': np.array(ext_input_valid), - 'train_percentage' : train_percentage, - 'nreplications' : nreplications, - 'dt' : FLAGS.dt, - 'P_sxn' : P_nxn, - 'condition_labels_train' : condition_labels_train, - 'condition_labels_valid' : condition_labels_valid, - 'conversion_factor': 1.0 / rnn_a['conversion_factor']} - -# just one dataset here -datasets = {} -dataset_name = 'dataset_N' + str(N) -datasets[dataset_name] = data - -# write out the dataset -write_datasets(FLAGS.save_dir, FLAGS.datafile_name, datasets) -print ('Saved to ', os.path.join(FLAGS.save_dir, - FLAGS.datafile_name + '_' + dataset_name)) diff --git a/research/lfads/synth_data/run_generate_synth_data.sh b/research/lfads/synth_data/run_generate_synth_data.sh deleted file mode 100755 index 9ebc8ce2e5e..00000000000 --- a/research/lfads/synth_data/run_generate_synth_data.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash - -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== - -SYNTH_PATH=/tmp/rnn_synth_data_v1.0/ - - echo "Generating chaotic rnn data with no input pulses (g=1.5) with spiking noise" - python generate_chaotic_rnn_data.py --save_dir=$SYNTH_PATH --datafile_name=chaotic_rnn_no_inputs --synth_data_seed=5 --T=1.0 --C=400 --N=50 --S=50 --train_percentage=0.8 --nreplications=10 --g=1.5 --x0_std=1.0 --tau=0.025 --dt=0.01 --input_magnitude=0.0 --max_firing_rate=30.0 --noise_type='poisson' - -echo "Generating chaotic rnn data with no input pulses (g=1.5) with Gaussian noise" -python generate_chaotic_rnn_data.py --save_dir=$SYNTH_PATH --datafile_name=gaussian_chaotic_rnn_no_inputs --synth_data_seed=5 --T=1.0 --C=400 --N=50 --S=50 --train_percentage=0.8 --nreplications=10 --g=1.5 --x0_std=1.0 --tau=0.025 --dt=0.01 --input_magnitude=0.0 --max_firing_rate=30.0 --noise_type='gaussian' - - echo "Generating chaotic rnn data with input pulses (g=1.5)" - python generate_chaotic_rnn_data.py --save_dir=$SYNTH_PATH --datafile_name=chaotic_rnn_inputs_g1p5 --synth_data_seed=5 --T=1.0 --C=400 --N=50 --S=50 --train_percentage=0.8 --nreplications=10 --g=1.5 --x0_std=1.0 --tau=0.025 --dt=0.01 --input_magnitude=20.0 --max_firing_rate=30.0 --noise_type='poisson' - - echo "Generating chaotic rnn data with input pulses (g=2.5)" - python generate_chaotic_rnn_data.py --save_dir=$SYNTH_PATH --datafile_name=chaotic_rnn_inputs_g2p5 --synth_data_seed=5 --T=1.0 --C=400 --N=50 --S=50 --train_percentage=0.8 --nreplications=10 --g=2.5 --x0_std=1.0 --tau=0.025 --dt=0.01 --input_magnitude=20.0 --max_firing_rate=30.0 --noise_type='poisson' - - echo "Generate the multi-session RNN data (no multi-session synth example in paper)" - python generate_chaotic_rnn_data.py --save_dir=$SYNTH_PATH --datafile_name=chaotic_rnn_multisession --synth_data_seed=5 --T=1.0 --C=150 --N=100 --S=20 --npcs=10 --train_percentage=0.8 --nreplications=40 --g=1.5 --x0_std=1.0 --tau=0.025 --dt=0.01 --input_magnitude=0.0 --max_firing_rate=30.0 --noise_type='poisson' - - echo "Generating Integration-to-bound RNN data" - python generate_itb_data.py --save_dir=$SYNTH_PATH --datafile_name=itb_rnn --u_std=0.25 --checkpoint_path=SAMPLE_CHECKPOINT --synth_data_seed=5 --T=1.0 --C=800 --N=50 --train_percentage=0.8 --nreplications=5 --tau=0.025 --dt=0.01 --max_firing_rate=30.0 - - echo "Generating chaotic rnn data with external input labels (no external input labels example in paper)" - python generate_labeled_rnn_data.py --save_dir=$SYNTH_PATH --datafile_name=chaotic_rnns_labeled --synth_data_seed=5 --T=1.0 --C=400 --N=50 --train_percentage=0.8 --nreplications=10 --g=1.5 --x0_std=1.0 --tau=0.025 --dt=0.01 --max_firing_rate=30.0 diff --git a/research/lfads/synth_data/synthetic_data_utils.py b/research/lfads/synth_data/synthetic_data_utils.py deleted file mode 100644 index cc264ee49fd..00000000000 --- a/research/lfads/synth_data/synthetic_data_utils.py +++ /dev/null @@ -1,348 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -from __future__ import print_function - -import h5py -import numpy as np -import os - -from utils import write_datasets -import matplotlib -import matplotlib.pyplot as plt -import scipy.signal - - -def generate_rnn(rng, N, g, tau, dt, max_firing_rate): - """Create a (vanilla) RNN with a bunch of hyper parameters for generating -chaotic data. - Args: - rng: numpy random number generator - N: number of hidden units - g: scaling of recurrent weight matrix in g W, with W ~ N(0,1/N) - tau: time scale of individual unit dynamics - dt: time step for equation updates - max_firing_rate: how to resecale the -1,1 firing rates - Returns: - the dictionary of these parameters, plus some others. -""" - rnn = {} - rnn['N'] = N - rnn['W'] = rng.randn(N,N)/np.sqrt(N) - rnn['Bin'] = rng.randn(N)/np.sqrt(1.0) - rnn['Bin2'] = rng.randn(N)/np.sqrt(1.0) - rnn['b'] = np.zeros(N) - rnn['g'] = g - rnn['tau'] = tau - rnn['dt'] = dt - rnn['max_firing_rate'] = max_firing_rate - mfr = rnn['max_firing_rate'] # spikes / sec - nbins_per_sec = 1.0/rnn['dt'] # bins / sec - # Used for plotting in LFADS - rnn['conversion_factor'] = mfr / nbins_per_sec # spikes / bin - return rnn - - -def generate_data(rnn, T, E, x0s=None, P_sxn=None, input_magnitude=0.0, - input_times=None): - """ Generates data from an randomly initialized RNN. - Args: - rnn: the rnn - T: Time in seconds to run (divided by rnn['dt'] to get steps, rounded down. - E: total number of examples - S: number of samples (subsampling N) - Returns: - A list of length E of NxT tensors of the network being run. - """ - N = rnn['N'] - def run_rnn(rnn, x0, ntime_steps, input_time=None): - rs = np.zeros([N,ntime_steps]) - x_tm1 = x0 - r_tm1 = np.tanh(x0) - tau = rnn['tau'] - dt = rnn['dt'] - alpha = (1.0-dt/tau) - W = dt/tau*rnn['W']*rnn['g'] - Bin = dt/tau*rnn['Bin'] - Bin2 = dt/tau*rnn['Bin2'] - b = dt/tau*rnn['b'] - - us = np.zeros([1, ntime_steps]) - for t in range(ntime_steps): - x_t = alpha*x_tm1 + np.dot(W,r_tm1) + b - if input_time is not None and t == input_time: - us[0,t] = input_magnitude - x_t += Bin * us[0,t] # DCS is this what was used? - r_t = np.tanh(x_t) - x_tm1 = x_t - r_tm1 = r_t - rs[:,t] = r_t - return rs, us - - if P_sxn is None: - P_sxn = np.eye(N) - ntime_steps = int(T / rnn['dt']) - data_e = [] - inputs_e = [] - for e in range(E): - input_time = input_times[e] if input_times is not None else None - r_nxt, u_uxt = run_rnn(rnn, x0s[:,e], ntime_steps, input_time) - r_sxt = np.dot(P_sxn, r_nxt) - inputs_e.append(u_uxt) - data_e.append(r_sxt) - - S = P_sxn.shape[0] - data_e = normalize_rates(data_e, E, S) - - return data_e, x0s, inputs_e - - -def normalize_rates(data_e, E, S): - # Normalization, made more complex because of the P matrices. - # Normalize by min and max in each channel. This normalization will - # cause offset differences between identical rnn runs, but different - # t hits. - for e in range(E): - r_sxt = data_e[e] - for i in range(S): - rmin = np.min(r_sxt[i,:]) - rmax = np.max(r_sxt[i,:]) - assert rmax - rmin != 0, 'Something wrong' - r_sxt[i,:] = (r_sxt[i,:] - rmin)/(rmax-rmin) - data_e[e] = r_sxt - return data_e - - -def spikify_data(data_e, rng, dt=1.0, max_firing_rate=100): - """ Apply spikes to a continuous dataset whose values are between 0.0 and 1.0 - Args: - data_e: nexamples length list of NxT trials - dt: how often the data are sampled - max_firing_rate: the firing rate that is associated with a value of 1.0 - Returns: - spikified_e: a list of length b of the data represented as spikes, - sampled from the underlying poisson process. - """ - - E = len(data_e) - spikes_e = [] - for e in range(E): - data = data_e[e] - N,T = data.shape - data_s = np.zeros([N,T]).astype(np.int) - for n in range(N): - f = data[n,:] - s = rng.poisson(f*max_firing_rate*dt, size=T) - data_s[n,:] = s - spikes_e.append(data_s) - - return spikes_e - - -def gaussify_data(data_e, rng, dt=1.0, max_firing_rate=100): - """ Apply gaussian noise to a continuous dataset whose values are between - 0.0 and 1.0 - - Args: - data_e: nexamples length list of NxT trials - dt: how often the data are sampled - max_firing_rate: the firing rate that is associated with a value of 1.0 - Returns: - gauss_e: a list of length b of the data with noise. - """ - - E = len(data_e) - mfr = max_firing_rate - gauss_e = [] - for e in range(E): - data = data_e[e] - N,T = data.shape - noisy_data = data * mfr + np.random.randn(N,T) * (5.0*mfr) * np.sqrt(dt) - gauss_e.append(noisy_data) - - return gauss_e - - - -def get_train_n_valid_inds(num_trials, train_fraction, nreplications): - """Split the numbers between 0 and num_trials-1 into two portions for - training and validation, based on the train fraction. - Args: - num_trials: the number of trials - train_fraction: (e.g. .80) - nreplications: the number of spiking trials per initial condition - Returns: - a 2-tuple of two lists: the training indices and validation indices - """ - train_inds = [] - valid_inds = [] - for i in range(num_trials): - # This line divides up the trials so that within one initial condition, - # the randomness of spikifying the condition is shared among both - # training and validation data splits. - if (i % nreplications)+1 > train_fraction * nreplications: - valid_inds.append(i) - else: - train_inds.append(i) - - return train_inds, valid_inds - - -def split_list_by_inds(data, inds1, inds2): - """Take the data, a list, and split it up based on the indices in inds1 and - inds2. - Args: - data: the list of data to split - inds1, the first list of indices - inds2, the second list of indices - Returns: a 2-tuple of two lists. - """ - if data is None or len(data) == 0: - return [], [] - else: - dout1 = [data[i] for i in inds1] - dout2 = [data[i] for i in inds2] - return dout1, dout2 - - -def nparray_and_transpose(data_a_b_c): - """Convert the list of items in data to a numpy array, and transpose it - Args: - data: data_asbsc: a nested, nested list of length a, with sublist length - b, with sublist length c. - Returns: - a numpy 3-tensor with dimensions a x c x b -""" - data_axbxc = np.array([datum_b_c for datum_b_c in data_a_b_c]) - data_axcxb = np.transpose(data_axbxc, axes=[0,2,1]) - return data_axcxb - - -def add_alignment_projections(datasets, npcs, ntime=None, nsamples=None): - """Create a matrix that aligns the datasets a bit, under - the assumption that each dataset is observing the same underlying dynamical - system. - - Args: - datasets: The dictionary of dataset structures. - npcs: The number of pcs for each, basically like lfads factors. - nsamples (optional): Number of samples to take for each dataset. - ntime (optional): Number of time steps to take in each sample. - - Returns: - The dataset structures, with the field alignment_matrix_cxf added. - This is # channels x npcs dimension -""" - nchannels_all = 0 - channel_idxs = {} - conditions_all = {} - nconditions_all = 0 - for name, dataset in datasets.items(): - cidxs = np.where(dataset['P_sxn'])[1] # non-zero entries in columns - channel_idxs[name] = [cidxs[0], cidxs[-1]+1] - nchannels_all += cidxs[-1]+1 - cidxs[0] - conditions_all[name] = np.unique(dataset['condition_labels_train']) - - all_conditions_list = \ - np.unique(np.ndarray.flatten(np.array(conditions_all.values()))) - nconditions_all = all_conditions_list.shape[0] - - if ntime is None: - ntime = dataset['train_data'].shape[1] - if nsamples is None: - nsamples = dataset['train_data'].shape[0] - - # In the data workup in the paper, Chethan did intra condition - # averaging, so let's do that here. - avg_data_all = {} - for name, conditions in conditions_all.items(): - dataset = datasets[name] - avg_data_all[name] = {} - for cname in conditions: - td_idxs = np.argwhere(np.array(dataset['condition_labels_train'])==cname) - data = np.squeeze(dataset['train_data'][td_idxs,:,:], axis=1) - avg_data = np.mean(data, axis=0) - avg_data_all[name][cname] = avg_data - - # Visualize this in the morning. - all_data_nxtc = np.zeros([nchannels_all, ntime * nconditions_all]) - for name, dataset in datasets.items(): - cidx_s = channel_idxs[name][0] - cidx_f = channel_idxs[name][1] - for cname in conditions_all[name]: - cidxs = np.argwhere(all_conditions_list == cname) - if cidxs.shape[0] > 0: - cidx = cidxs[0][0] - all_tidxs = np.arange(0, ntime+1) + cidx*ntime - all_data_nxtc[cidx_s:cidx_f, all_tidxs[0]:all_tidxs[-1]] = \ - avg_data_all[name][cname].T - - # A bit of filtering. We don't care about spectral properties, or - # filtering artifacts, simply correlate time steps a bit. - filt_len = 6 - bc_filt = np.ones([filt_len])/float(filt_len) - for c in range(nchannels_all): - all_data_nxtc[c,:] = scipy.signal.filtfilt(bc_filt, [1.0], all_data_nxtc[c,:]) - - # Compute the PCs. - all_data_mean_nx1 = np.mean(all_data_nxtc, axis=1, keepdims=True) - all_data_zm_nxtc = all_data_nxtc - all_data_mean_nx1 - corr_mat_nxn = np.dot(all_data_zm_nxtc, all_data_zm_nxtc.T) - evals_n, evecs_nxn = np.linalg.eigh(corr_mat_nxn) - sidxs = np.flipud(np.argsort(evals_n)) # sort such that 0th is highest - evals_n = evals_n[sidxs] - evecs_nxn = evecs_nxn[:,sidxs] - - # Project all the channels data onto the low-D PCA basis, where - # low-d is the npcs parameter. - all_data_pca_pxtc = np.dot(evecs_nxn[:, 0:npcs].T, all_data_zm_nxtc) - - # Now for each dataset, we regress the channel data onto the top - # pcs, and this will be our alignment matrix for that dataset. - # |B - A*W|^2 - for name, dataset in datasets.items(): - cidx_s = channel_idxs[name][0] - cidx_f = channel_idxs[name][1] - all_data_zm_chxtc = all_data_zm_nxtc[cidx_s:cidx_f,:] # ch for channel - W_chxp, _, _, _ = \ - np.linalg.lstsq(all_data_zm_chxtc.T, all_data_pca_pxtc.T) - dataset['alignment_matrix_cxf'] = W_chxp - alignment_bias_cx1 = all_data_mean_nx1[cidx_s:cidx_f] - dataset['alignment_bias_c'] = np.squeeze(alignment_bias_cx1, axis=1) - - do_debug_plot = False - if do_debug_plot: - pc_vecs = evecs_nxn[:,0:npcs] - ntoplot = 400 - - plt.figure() - plt.plot(np.log10(evals_n), '-x') - plt.figure() - plt.subplot(311) - plt.imshow(all_data_pca_pxtc) - plt.colorbar() - - plt.subplot(312) - plt.imshow(np.dot(W_chxp.T, all_data_zm_chxtc)) - plt.colorbar() - - plt.subplot(313) - plt.imshow(np.dot(all_data_zm_chxtc.T, W_chxp).T - all_data_pca_pxtc) - plt.colorbar() - - import pdb - pdb.set_trace() - - return datasets diff --git a/research/lfads/synth_data/trained_itb/model-65000.data-00000-of-00001 b/research/lfads/synth_data/trained_itb/model-65000.data-00000-of-00001 deleted file mode 100644 index 9459a2a1b72..00000000000 Binary files a/research/lfads/synth_data/trained_itb/model-65000.data-00000-of-00001 and /dev/null differ diff --git a/research/lfads/synth_data/trained_itb/model-65000.index b/research/lfads/synth_data/trained_itb/model-65000.index deleted file mode 100644 index dd9c793acf8..00000000000 Binary files a/research/lfads/synth_data/trained_itb/model-65000.index and /dev/null differ diff --git a/research/lfads/synth_data/trained_itb/model-65000.meta b/research/lfads/synth_data/trained_itb/model-65000.meta deleted file mode 100644 index 07bd2b9688e..00000000000 Binary files a/research/lfads/synth_data/trained_itb/model-65000.meta and /dev/null differ diff --git a/research/lfads/utils.py b/research/lfads/utils.py deleted file mode 100644 index e64825ffc1d..00000000000 --- a/research/lfads/utils.py +++ /dev/null @@ -1,367 +0,0 @@ -# Copyright 2017 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# ============================================================================== -from __future__ import print_function - -import os -import h5py -import json - -import numpy as np -import tensorflow as tf - - -def log_sum_exp(x_k): - """Computes log \sum exp in a numerically stable way. - log ( sum_i exp(x_i) ) - log ( sum_i exp(x_i - m + m) ), with m = max(x_i) - log ( sum_i exp(x_i - m)*exp(m) ) - log ( sum_i exp(x_i - m) + m - - Args: - x_k - k -dimensional list of arguments to log_sum_exp. - - Returns: - log_sum_exp of the arguments. - """ - m = tf.reduce_max(x_k) - x1_k = x_k - m - u_k = tf.exp(x1_k) - z = tf.reduce_sum(u_k) - return tf.log(z) + m - - -def linear(x, out_size, do_bias=True, alpha=1.0, identity_if_possible=False, - normalized=False, name=None, collections=None): - """Linear (affine) transformation, y = x W + b, for a variety of - configurations. - - Args: - x: input The tensor to tranformation. - out_size: The integer size of non-batch output dimension. - do_bias (optional): Add a learnable bias vector to the operation. - alpha (optional): A multiplicative scaling for the weight initialization - of the matrix, in the form \alpha * 1/\sqrt{x.shape[1]}. - identity_if_possible (optional): just return identity, - if x.shape[1] == out_size. - normalized (optional): Option to divide out by the norms of the rows of W. - name (optional): The name prefix to add to variables. - collections (optional): List of additional collections. (Placed in - tf.GraphKeys.GLOBAL_VARIABLES already, so no need for that.) - - Returns: - In the equation, y = x W + b, returns the tensorflow op that yields y. - """ - in_size = int(x.get_shape()[1]) # from Dimension(10) -> 10 - stddev = alpha/np.sqrt(float(in_size)) - mat_init = tf.random_normal_initializer(0.0, stddev) - wname = (name + "/W") if name else "/W" - - if identity_if_possible and in_size == out_size: - # Sometimes linear layers are nothing more than size adapters. - return tf.identity(x, name=(wname+'_ident')) - - W,b = init_linear(in_size, out_size, do_bias=do_bias, alpha=alpha, - normalized=normalized, name=name, collections=collections) - - if do_bias: - return tf.matmul(x, W) + b - else: - return tf.matmul(x, W) - - -def init_linear(in_size, out_size, do_bias=True, mat_init_value=None, - bias_init_value=None, alpha=1.0, identity_if_possible=False, - normalized=False, name=None, collections=None, trainable=True): - """Linear (affine) transformation, y = x W + b, for a variety of - configurations. - - Args: - in_size: The integer size of the non-batc input dimension. [(x),y] - out_size: The integer size of non-batch output dimension. [x,(y)] - do_bias (optional): Add a (learnable) bias vector to the operation, - if false, b will be None - mat_init_value (optional): numpy constant for matrix initialization, if None - , do random, with additional parameters. - alpha (optional): A multiplicative scaling for the weight initialization - of the matrix, in the form \alpha * 1/\sqrt{x.shape[1]}. - identity_if_possible (optional): just return identity, - if x.shape[1] == out_size. - normalized (optional): Option to divide out by the norms of the rows of W. - name (optional): The name prefix to add to variables. - collections (optional): List of additional collections. (Placed in - tf.GraphKeys.GLOBAL_VARIABLES already, so no need for that.) - - Returns: - In the equation, y = x W + b, returns the pair (W, b). - """ - - if mat_init_value is not None and mat_init_value.shape != (in_size, out_size): - raise ValueError( - 'Provided mat_init_value must have shape [%d, %d].'%(in_size, out_size)) - if bias_init_value is not None and bias_init_value.shape != (1,out_size): - raise ValueError( - 'Provided bias_init_value must have shape [1,%d].'%(out_size,)) - - if mat_init_value is None: - stddev = alpha/np.sqrt(float(in_size)) - mat_init = tf.random_normal_initializer(0.0, stddev) - - wname = (name + "/W") if name else "/W" - - if identity_if_possible and in_size == out_size: - return (tf.constant(np.eye(in_size).astype(np.float32)), - tf.zeros(in_size)) - - # Note the use of get_variable vs. tf.Variable. this is because get_variable - # does not allow the initialization of the variable with a value. - if normalized: - w_collections = [tf.GraphKeys.GLOBAL_VARIABLES, "norm-variables"] - if collections: - w_collections += collections - if mat_init_value is not None: - w = tf.Variable(mat_init_value, name=wname, collections=w_collections, - trainable=trainable) - else: - w = tf.get_variable(wname, [in_size, out_size], initializer=mat_init, - collections=w_collections, trainable=trainable) - w = tf.nn.l2_normalize(w, dim=0) # x W, so xW_j = \sum_i x_bi W_ij - else: - w_collections = [tf.GraphKeys.GLOBAL_VARIABLES] - if collections: - w_collections += collections - if mat_init_value is not None: - w = tf.Variable(mat_init_value, name=wname, collections=w_collections, - trainable=trainable) - else: - w = tf.get_variable(wname, [in_size, out_size], initializer=mat_init, - collections=w_collections, trainable=trainable) - b = None - if do_bias: - b_collections = [tf.GraphKeys.GLOBAL_VARIABLES] - if collections: - b_collections += collections - bname = (name + "/b") if name else "/b" - if bias_init_value is None: - b = tf.get_variable(bname, [1, out_size], - initializer=tf.zeros_initializer(), - collections=b_collections, - trainable=trainable) - else: - b = tf.Variable(bias_init_value, name=bname, - collections=b_collections, - trainable=trainable) - - return (w, b) - - -def write_data(data_fname, data_dict, use_json=False, compression=None): - """Write data in HD5F format. - - Args: - data_fname: The filename of teh file in which to write the data. - data_dict: The dictionary of data to write. The keys are strings - and the values are numpy arrays. - use_json (optional): human readable format for simple items - compression (optional): The compression to use for h5py (disabled by - default because the library borks on scalars, otherwise try 'gzip'). - """ - - dir_name = os.path.dirname(data_fname) - if not os.path.exists(dir_name): - os.makedirs(dir_name) - - if use_json: - the_file = open(data_fname,'wb') - json.dump(data_dict, the_file) - the_file.close() - else: - try: - with h5py.File(data_fname, 'w') as hf: - for k, v in data_dict.items(): - clean_k = k.replace('/', '_') - if clean_k is not k: - print('Warning: saving variable with name: ', k, ' as ', clean_k) - else: - print('Saving variable with name: ', clean_k) - hf.create_dataset(clean_k, data=v, compression=compression) - except IOError: - print("Cannot open %s for writing.", data_fname) - raise - - -def read_data(data_fname): - """ Read saved data in HDF5 format. - - Args: - data_fname: The filename of the file from which to read the data. - Returns: - A dictionary whose keys will vary depending on dataset (but should - always contain the keys 'train_data' and 'valid_data') and whose - values are numpy arrays. - """ - - try: - with h5py.File(data_fname, 'r') as hf: - data_dict = {k: np.array(v) for k, v in hf.items()} - return data_dict - except IOError: - print("Cannot open %s for reading." % data_fname) - raise - - -def write_datasets(data_path, data_fname_stem, dataset_dict, compression=None): - """Write datasets in HD5F format. - - This function assumes the dataset_dict is a mapping ( string -> - to data_dict ). It calls write_data for each data dictionary, - post-fixing the data filename with the key of the dataset. - - Args: - data_path: The path to the save directory. - data_fname_stem: The filename stem of the file in which to write the data. - dataset_dict: The dictionary of datasets. The keys are strings - and the values data dictionaries (str -> numpy arrays) associations. - compression (optional): The compression to use for h5py (disabled by - default because the library borks on scalars, otherwise try 'gzip'). - """ - - full_name_stem = os.path.join(data_path, data_fname_stem) - for s, data_dict in dataset_dict.items(): - write_data(full_name_stem + "_" + s, data_dict, compression=compression) - - -def read_datasets(data_path, data_fname_stem): - """Read dataset sin HD5F format. - - This function assumes the dataset_dict is a mapping ( string -> - to data_dict ). It calls write_data for each data dictionary, - post-fixing the data filename with the key of the dataset. - - Args: - data_path: The path to the save directory. - data_fname_stem: The filename stem of the file in which to write the data. - """ - - dataset_dict = {} - fnames = os.listdir(data_path) - - print ('loading data from ' + data_path + ' with stem ' + data_fname_stem) - for fname in fnames: - if fname.startswith(data_fname_stem): - data_dict = read_data(os.path.join(data_path,fname)) - idx = len(data_fname_stem) + 1 - key = fname[idx:] - data_dict['data_dim'] = data_dict['train_data'].shape[2] - data_dict['num_steps'] = data_dict['train_data'].shape[1] - dataset_dict[key] = data_dict - - if len(dataset_dict) == 0: - raise ValueError("Failed to load any datasets, are you sure that the " - "'--data_dir' and '--data_filename_stem' flag values " - "are correct?") - - print (str(len(dataset_dict)) + ' datasets loaded') - return dataset_dict - - -# NUMPY utility functions -def list_t_bxn_to_list_b_txn(values_t_bxn): - """Convert a length T list of BxN numpy tensors of length B list of TxN numpy - tensors. - - Args: - values_t_bxn: The length T list of BxN numpy tensors. - - Returns: - The length B list of TxN numpy tensors. - """ - T = len(values_t_bxn) - B, N = values_t_bxn[0].shape - values_b_txn = [] - for b in range(B): - values_pb_txn = np.zeros([T,N]) - for t in range(T): - values_pb_txn[t,:] = values_t_bxn[t][b,:] - values_b_txn.append(values_pb_txn) - - return values_b_txn - - -def list_t_bxn_to_tensor_bxtxn(values_t_bxn): - """Convert a length T list of BxN numpy tensors to single numpy tensor with - shape BxTxN. - - Args: - values_t_bxn: The length T list of BxN numpy tensors. - - Returns: - values_bxtxn: The BxTxN numpy tensor. - """ - - T = len(values_t_bxn) - B, N = values_t_bxn[0].shape - values_bxtxn = np.zeros([B,T,N]) - for t in range(T): - values_bxtxn[:,t,:] = values_t_bxn[t] - - return values_bxtxn - - -def tensor_bxtxn_to_list_t_bxn(tensor_bxtxn): - """Convert a numpy tensor with shape BxTxN to a length T list of numpy tensors - with shape BxT. - - Args: - tensor_bxtxn: The BxTxN numpy tensor. - - Returns: - A length T list of numpy tensors with shape BxT. - """ - - values_t_bxn = [] - B, T, N = tensor_bxtxn.shape - for t in range(T): - values_t_bxn.append(np.squeeze(tensor_bxtxn[:,t,:])) - - return values_t_bxn - - -def flatten(list_of_lists): - """Takes a list of lists and returns a list of the elements. - - Args: - list_of_lists: List of lists. - - Returns: - flat_list: Flattened list. - flat_list_idxs: Flattened list indices. - """ - flat_list = [] - flat_list_idxs = [] - start_idx = 0 - for item in list_of_lists: - if isinstance(item, list): - flat_list += item - l = len(item) - idxs = range(start_idx, start_idx+l) - start_idx = start_idx+l - else: # a value - flat_list.append(item) - idxs = [start_idx] - start_idx += 1 - flat_list_idxs.append(idxs) - - return flat_list, flat_list_idxs diff --git a/research/lstm_object_detection/README.md b/research/lstm_object_detection/README.md deleted file mode 100644 index a696ba3df30..00000000000 --- a/research/lstm_object_detection/README.md +++ /dev/null @@ -1,40 +0,0 @@ -# Tensorflow Mobile Video Object Detection - -Tensorflow mobile video object detection implementation proposed in the -following papers: - -

- -

- -``` -"Mobile Video Object Detection with Temporally-Aware Feature Maps", -Liu, Mason and Zhu, Menglong, CVPR 2018. -``` -\[[link](http://openaccess.thecvf.com/content_cvpr_2018/papers/Liu_Mobile_Video_Object_CVPR_2018_paper.pdf)\]\[[bibtex]( -https://scholar.googleusercontent.com/scholar.bib?q=info:hq5rcMUUXysJ:scholar.google.com/&output=citation&scisig=AAGBfm0AAAAAXLdwXcU5g_wiMQ40EvbHQ9kTyvfUxffh&scisf=4&ct=citation&cd=-1&hl=en)\] - - -

- -

- -``` -"Looking Fast and Slow: Memory-Guided Mobile Video Object Detection", -Liu, Mason and Zhu, Menglong and White, Marie and Li, Yinxiao and Kalenichenko, Dmitry -``` -\[[link](https://arxiv.org/abs/1903.10172)\]\[[bibtex]( -https://scholar.googleusercontent.com/scholar.bib?q=info:rLqvkztmWYgJ:scholar.google.com/&output=citation&scisig=AAGBfm0AAAAAXLdwNf-LJlm2M1ymQHbq2wYA995MHpJu&scisf=4&ct=citation&cd=-1&hl=en)\] - - -## Maintainers -* masonliuw@gmail.com -* yinxiao@google.com -* menglong@google.com -* yongzhe@google.com -* lzyuan@google.com - - -## Table of Contents - - * Exporting a trained model diff --git a/research/lstm_object_detection/__init__.py b/research/lstm_object_detection/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/builders/__init__.py b/research/lstm_object_detection/builders/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/builders/graph_rewriter_builder.py b/research/lstm_object_detection/builders/graph_rewriter_builder.py deleted file mode 100644 index accced2f0fc..00000000000 --- a/research/lstm_object_detection/builders/graph_rewriter_builder.py +++ /dev/null @@ -1,147 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Custom version for quantized training and evaluation functions. - -The main difference between this and the third_party graph_rewriter_builder.py -is that this version uses experimental_create_training_graph which allows the -customization of freeze_bn_delay. -""" - -import re -import tensorflow.compat.v1 as tf -from tensorflow.contrib import layers as contrib_layers -from tensorflow.contrib import quantize as contrib_quantize -from tensorflow.contrib.quantize.python import common -from tensorflow.contrib.quantize.python import input_to_ops -from tensorflow.contrib.quantize.python import quant_ops -from tensorflow.python.ops import control_flow_ops -from tensorflow.python.ops import math_ops - - -def build(graph_rewriter_config, - quant_overrides_config=None, - is_training=True, - is_export=False): - """Returns a function that modifies default graph based on options. - - Args: - graph_rewriter_config: graph_rewriter_pb2.GraphRewriter proto. - quant_overrides_config: quant_overrides_pb2.QuantOverrides proto. - is_training: whether in training or eval mode. - is_export: whether exporting the graph. - """ - def graph_rewrite_fn(): - """Function to quantize weights and activation of the default graph.""" - if (graph_rewriter_config.quantization.weight_bits != 8 or - graph_rewriter_config.quantization.activation_bits != 8): - raise ValueError('Only 8bit quantization is supported') - - graph = tf.get_default_graph() - - # Insert custom quant ops. - if quant_overrides_config is not None: - input_to_ops_map = input_to_ops.InputToOps(graph) - for q in quant_overrides_config.quant_configs: - producer = graph.get_operation_by_name(q.op_name) - if producer is None: - raise ValueError('Op name does not exist in graph.') - context = _get_context_from_op(producer) - consumers = input_to_ops_map.ConsumerOperations(producer) - if q.fixed_range: - _insert_fixed_quant_op( - context, - q.quant_op_name, - producer, - consumers, - init_min=q.min, - init_max=q.max, - quant_delay=q.delay if is_training else 0) - else: - raise ValueError('Learned ranges are not yet supported.') - - # Quantize the graph by inserting quantize ops for weights and activations - if is_training: - contrib_quantize.experimental_create_training_graph( - input_graph=graph, - quant_delay=graph_rewriter_config.quantization.delay, - freeze_bn_delay=graph_rewriter_config.quantization.delay) - else: - contrib_quantize.experimental_create_eval_graph( - input_graph=graph, - quant_delay=graph_rewriter_config.quantization.delay - if not is_export else 0) - - contrib_layers.summarize_collection('quant_vars') - - return graph_rewrite_fn - - -def _get_context_from_op(op): - """Gets the root context name from the op name.""" - context_re = re.search(r'^(.*)/([^/]+)', op.name) - if context_re: - return context_re.group(1) - return '' - - -def _insert_fixed_quant_op(context, - name, - producer, - consumers, - init_min=-6.0, - init_max=6.0, - quant_delay=None): - """Adds a fake quant op with fixed ranges. - - Args: - context: The parent scope of the op to be quantized. - name: The name of the fake quant op. - producer: The producer op to be quantized. - consumers: The consumer ops to the producer op. - init_min: The minimum range for the fake quant op. - init_max: The maximum range for the fake quant op. - quant_delay: Number of steps to wait before activating the fake quant op. - - Raises: - ValueError: When producer operation is not directly connected to the - consumer operation. - """ - name_prefix = name if not context else context + '/' + name - inputs = producer.outputs[0] - quant = quant_ops.FixedQuantize( - inputs, init_min=init_min, init_max=init_max, scope=name_prefix) - - if quant_delay and quant_delay > 0: - activate_quant = math_ops.greater_equal( - common.CreateOrGetQuantizationStep(), - quant_delay, - name=name_prefix + '/activate_quant') - quant = control_flow_ops.cond( - activate_quant, - lambda: quant, - lambda: inputs, - name=name_prefix + '/delayed_quant') - - if consumers: - tensors_modified_count = common.RerouteTensor( - quant, inputs, can_modify=consumers) - # Some operations can have multiple output tensors going to the same - # consumer. Since consumers is a set, we need to ensure that - # tensors_modified_count is greater than or equal to the length of the set - # of consumers. - if tensors_modified_count < len(consumers): - raise ValueError('No inputs quantized for ops: [%s]' % ', '.join( - [consumer.name for consumer in consumers])) diff --git a/research/lstm_object_detection/builders/graph_rewriter_builder_test.py b/research/lstm_object_detection/builders/graph_rewriter_builder_test.py deleted file mode 100644 index e06a9f5a3d7..00000000000 --- a/research/lstm_object_detection/builders/graph_rewriter_builder_test.py +++ /dev/null @@ -1,117 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for graph_rewriter_builder.""" -import mock -import tensorflow.compat.v1 as tf -from tensorflow.contrib import layers as contrib_layers -from tensorflow.contrib import quantize as contrib_quantize -from tensorflow.python.framework import dtypes -from tensorflow.python.framework import ops -from lstm_object_detection.builders import graph_rewriter_builder -from lstm_object_detection.protos import quant_overrides_pb2 -from object_detection.protos import graph_rewriter_pb2 - - -class QuantizationBuilderTest(tf.test.TestCase): - - def testQuantizationBuilderSetsUpCorrectTrainArguments(self): - with mock.patch.object( - contrib_quantize, - 'experimental_create_training_graph') as mock_quant_fn: - with mock.patch.object(contrib_layers, - 'summarize_collection') as mock_summarize_col: - graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_proto.quantization.delay = 10 - graph_rewriter_proto.quantization.weight_bits = 8 - graph_rewriter_proto.quantization.activation_bits = 8 - graph_rewrite_fn = graph_rewriter_builder.build( - graph_rewriter_proto, is_training=True) - graph_rewrite_fn() - _, kwargs = mock_quant_fn.call_args - self.assertEqual(kwargs['input_graph'], tf.get_default_graph()) - self.assertEqual(kwargs['quant_delay'], 10) - mock_summarize_col.assert_called_with('quant_vars') - - def testQuantizationBuilderSetsUpCorrectEvalArguments(self): - with mock.patch.object(contrib_quantize, - 'experimental_create_eval_graph') as mock_quant_fn: - with mock.patch.object(contrib_layers, - 'summarize_collection') as mock_summarize_col: - graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_proto.quantization.delay = 10 - graph_rewrite_fn = graph_rewriter_builder.build( - graph_rewriter_proto, is_training=False) - graph_rewrite_fn() - _, kwargs = mock_quant_fn.call_args - self.assertEqual(kwargs['input_graph'], tf.get_default_graph()) - mock_summarize_col.assert_called_with('quant_vars') - - def testQuantizationBuilderAddsQuantOverride(self): - graph = ops.Graph() - with graph.as_default(): - self._buildGraph() - - quant_overrides_proto = quant_overrides_pb2.QuantOverrides() - quant_config = quant_overrides_proto.quant_configs.add() - quant_config.op_name = 'test_graph/add_ab' - quant_config.quant_op_name = 'act_quant' - quant_config.fixed_range = True - quant_config.min = 0 - quant_config.max = 6 - quant_config.delay = 100 - - graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_proto.quantization.delay = 10 - graph_rewriter_proto.quantization.weight_bits = 8 - graph_rewriter_proto.quantization.activation_bits = 8 - - graph_rewrite_fn = graph_rewriter_builder.build( - graph_rewriter_proto, - quant_overrides_config=quant_overrides_proto, - is_training=True) - graph_rewrite_fn() - - act_quant_found = False - quant_delay_found = False - for op in graph.get_operations(): - if (quant_config.quant_op_name in op.name and - op.type == 'FakeQuantWithMinMaxArgs'): - act_quant_found = True - min_val = op.get_attr('min') - max_val = op.get_attr('max') - self.assertEqual(min_val, quant_config.min) - self.assertEqual(max_val, quant_config.max) - if ('activate_quant' in op.name and - quant_config.quant_op_name in op.name and op.type == 'Const'): - tensor = op.get_attr('value') - if tensor.int64_val[0] == quant_config.delay: - quant_delay_found = True - - self.assertTrue(act_quant_found) - self.assertTrue(quant_delay_found) - - def _buildGraph(self, scope='test_graph'): - with ops.name_scope(scope): - a = tf.constant(10, dtype=dtypes.float32, name='input_a') - b = tf.constant(20, dtype=dtypes.float32, name='input_b') - ab = tf.add(a, b, name='add_ab') - c = tf.constant(30, dtype=dtypes.float32, name='input_c') - abc = tf.multiply(ab, c, name='mul_ab_c') - return abc - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/configs/lstm_ssd_interleaved_mobilenet_v2_imagenet.config b/research/lstm_object_detection/configs/lstm_ssd_interleaved_mobilenet_v2_imagenet.config deleted file mode 100644 index 536d7d53271..00000000000 --- a/research/lstm_object_detection/configs/lstm_ssd_interleaved_mobilenet_v2_imagenet.config +++ /dev/null @@ -1,239 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# For training on Imagenet Video with LSTM Interleaved Mobilenet V2 - -[lstm_object_detection.protos.lstm_model] { - train_unroll_length: 4 - eval_unroll_length: 4 - lstm_state_depth: 320 - depth_multipliers: 1.4 - depth_multipliers: 0.35 - pre_bottleneck: true - low_res: true - train_interleave_method: 'RANDOM_SKIP_SMALL' - eval_interleave_method: 'SKIP3' -} -model { - ssd { - num_classes: 30 # Num of class for imagenet vid dataset. - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - } - } - similarity_calculator { - iou_similarity { - } - } - anchor_generator { - ssd_anchor_generator { - num_layers: 5 - min_scale: 0.2 - max_scale: 0.95 - aspect_ratios: 1.0 - aspect_ratios: 2.0 - aspect_ratios: 0.5 - aspect_ratios: 3.0 - aspect_ratios: 0.3333 - } - } - image_resizer { - fixed_shape_resizer { - height: 320 - width: 320 - } - } - box_predictor { - convolutional_box_predictor { - min_depth: 0 - max_depth: 0 - num_layers_before_predictor: 3 - use_dropout: false - dropout_keep_probability: 0.8 - kernel_size: 3 - box_code_size: 4 - apply_sigmoid_to_scores: false - use_depthwise: true - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - train: true, - scale: true, - center: true, - decay: 0.9997, - epsilon: 0.001, - } - } - } - } - feature_extractor { - type: 'lstm_ssd_interleaved_mobilenet_v2' - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - train: true, - scale: true, - center: true, - decay: 0.9997, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid { - } - } - localization_loss { - weighted_smooth_l1 { - } - } - hard_example_miner { - num_hard_examples: 3000 - iou_threshold: 0.99 - loss_type: CLASSIFICATION - max_negatives_per_positive: 3 - min_negatives_per_image: 0 - } - classification_weight: 1.0 - localization_weight: 4.0 - } - normalize_loss_by_num_matches: true - post_processing { - batch_non_max_suppression { - score_threshold: -20.0 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - batch_size: 8 - optimizer { - use_moving_average: false - rms_prop_optimizer: { - learning_rate: { - exponential_decay_learning_rate { - initial_learning_rate: 0.002 - decay_steps: 200000 - decay_factor: 0.95 - } - } - momentum_optimizer_value: 0.9 - decay: 0.9 - epsilon: 1.0 - } - } - gradient_clipping_by_norm: 10.0 - batch_queue_capacity: 12 - prefetch_queue_capacity: 4 -} - -train_input_reader: { - shuffle_buffer_size: 32 - queue_capacity: 12 - prefetch_size: 12 - min_after_dequeue: 4 - label_map_path: "path/to/label_map" - external_input_reader { - [lstm_object_detection.protos.GoogleInputReader.google_input_reader] { - tf_record_video_input_reader: { - input_path: '/data/lstm_detection/tfrecords/test.tfrecord' - data_type: TF_SEQUENCE_EXAMPLE - video_length: 4 - } - } - } -} - -eval_config: { - metrics_set: "coco_evaluation_all_frames" - use_moving_averages: true - min_score_threshold: 0.5 - max_num_boxes_to_visualize: 300 - visualize_groundtruth_boxes: true - groundtruth_box_visualization_color: "red" -} - -eval_input_reader { - label_map_path: "path/to/label_map" - shuffle: true - num_epochs: 1 - num_parallel_batches: 1 - num_readers: 1 - external_input_reader { - [lstm_object_detection.protos.GoogleInputReader.google_input_reader] { - tf_record_video_input_reader: { - input_path: "path/to/sequence_example/data" - data_type: TF_SEQUENCE_EXAMPLE - video_length: 10 - } - } - } -} - -eval_input_reader: { - label_map_path: "path/to/label_map" - external_input_reader { - [lstm_object_detection.protos.GoogleInputReader.google_input_reader] { - tf_record_video_input_reader: { - input_path: "path/to/sequence_example/data" - data_type: TF_SEQUENCE_EXAMPLE - video_length: 4 - } - } - } - shuffle: true - num_readers: 1 -} diff --git a/research/lstm_object_detection/configs/lstm_ssd_mobilenet_v1_imagenet.config b/research/lstm_object_detection/configs/lstm_ssd_mobilenet_v1_imagenet.config deleted file mode 100644 index cb357ec17ee..00000000000 --- a/research/lstm_object_detection/configs/lstm_ssd_mobilenet_v1_imagenet.config +++ /dev/null @@ -1,232 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# For training on Imagenet Video with LSTM Mobilenet V1 - -[lstm_object_detection.protos.lstm_model] { - train_unroll_length: 4 - eval_unroll_length: 4 -} - -model { - ssd { - num_classes: 30 # Num of class for imagenet vid dataset. - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - } - } - similarity_calculator { - iou_similarity { - } - } - anchor_generator { - ssd_anchor_generator { - num_layers: 5 - min_scale: 0.2 - max_scale: 0.95 - aspect_ratios: 1.0 - aspect_ratios: 2.0 - aspect_ratios: 0.5 - aspect_ratios: 3.0 - aspect_ratios: 0.3333 - } - } - image_resizer { - fixed_shape_resizer { - height: 256 - width: 256 - } - } - box_predictor { - convolutional_box_predictor { - min_depth: 0 - max_depth: 0 - num_layers_before_predictor: 3 - use_dropout: false - dropout_keep_probability: 0.8 - kernel_size: 3 - box_code_size: 4 - apply_sigmoid_to_scores: false - use_depthwise: true - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - train: true, - scale: true, - center: true, - decay: 0.9997, - epsilon: 0.001, - } - } - } - } - feature_extractor { - type: 'lstm_mobilenet_v1' - min_depth: 16 - depth_multiplier: 1.0 - use_depthwise: true - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - train: true, - scale: true, - center: true, - decay: 0.9997, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid { - } - } - localization_loss { - weighted_smooth_l1 { - } - } - hard_example_miner { - num_hard_examples: 3000 - iou_threshold: 0.99 - loss_type: CLASSIFICATION - max_negatives_per_positive: 3 - min_negatives_per_image: 0 - } - classification_weight: 1.0 - localization_weight: 4.0 - } - normalize_loss_by_num_matches: true - post_processing { - batch_non_max_suppression { - score_threshold: -20.0 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - batch_size: 8 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - ssd_random_crop { - } - } - optimizer { - use_moving_average: false - rms_prop_optimizer: { - learning_rate: { - exponential_decay_learning_rate { - initial_learning_rate: 0.002 - decay_steps: 200000 - decay_factor: 0.95 - } - } - momentum_optimizer_value: 0.9 - decay: 0.9 - epsilon: 1.0 - } - } - - from_detection_checkpoint: true - gradient_clipping_by_norm: 10.0 - batch_queue_capacity: 12 - prefetch_queue_capacity: 4 - fine_tune_checkpoint: "/path/to/checkpoint/" - fine_tune_checkpoint_type: "detection" -} - - -train_input_reader: { - shuffle_buffer_size: 32 - queue_capacity: 12 - prefetch_size: 12 - min_after_dequeue: 4 - label_map_path: "path/to/label_map" - external_input_reader { - [lstm_object_detection.protos.GoogleInputReader.google_input_reader] { - tf_record_video_input_reader: { - input_path: "path/to/sequence_example/data" - data_type: TF_SEQUENCE_EXAMPLE - video_length: 4 - } - } - } -} - -eval_config: { - metrics_set: "coco_evaluation_all_frames" - use_moving_averages: true - min_score_threshold: 0.5 - max_num_boxes_to_visualize: 300 - visualize_groundtruth_boxes: true - groundtruth_box_visualization_color: "red" -} - -eval_input_reader: { - label_map_path: "path/to/label_map" - external_input_reader { - [lstm_object_detection.protos.GoogleInputReader.google_input_reader] { - tf_record_video_input_reader: { - input_path: "path/to/sequence_example/data" - data_type: TF_SEQUENCE_EXAMPLE - video_length: 4 - } - } - } - shuffle: true - num_readers: 1 -} diff --git a/research/lstm_object_detection/eval.py b/research/lstm_object_detection/eval.py deleted file mode 100644 index aac25c1182b..00000000000 --- a/research/lstm_object_detection/eval.py +++ /dev/null @@ -1,108 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Evaluation executable for detection models. - -This executable is used to evaluate DetectionModels. Example usage: - ./eval \ - --logtostderr \ - --checkpoint_dir=path/to/checkpoint_dir \ - --eval_dir=path/to/eval_dir \ - --pipeline_config_path=pipeline_config.pbtxt -""" - -import functools -import os -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from lstm_object_detection import evaluator -from lstm_object_detection import model_builder -from lstm_object_detection.inputs import seq_dataset_builder -from lstm_object_detection.utils import config_util -from object_detection.utils import label_map_util - -tf.logging.set_verbosity(tf.logging.INFO) -flags = tf.app.flags -flags.DEFINE_boolean('eval_training_data', False, - 'If training data should be evaluated for this job.') -flags.DEFINE_string('checkpoint_dir', '', - 'Directory containing checkpoints to evaluate, typically ' - 'set to `train_dir` used in the training job.') -flags.DEFINE_string('eval_dir', '', 'Directory to write eval summaries to.') -flags.DEFINE_string('pipeline_config_path', '', - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file. If provided, other configs are ignored') -flags.DEFINE_boolean('run_once', False, 'Option to only run a single pass of ' - 'evaluation. Overrides the `max_evals` parameter in the ' - 'provided config.') -FLAGS = flags.FLAGS - - -def main(unused_argv): - assert FLAGS.checkpoint_dir, '`checkpoint_dir` is missing.' - assert FLAGS.eval_dir, '`eval_dir` is missing.' - if FLAGS.pipeline_config_path: - configs = config_util.get_configs_from_pipeline_file( - FLAGS.pipeline_config_path) - else: - configs = config_util.get_configs_from_multiple_files( - model_config_path=FLAGS.model_config_path, - eval_config_path=FLAGS.eval_config_path, - eval_input_config_path=FLAGS.input_config_path) - - pipeline_proto = config_util.create_pipeline_proto_from_configs(configs) - config_text = text_format.MessageToString(pipeline_proto) - tf.gfile.MakeDirs(FLAGS.eval_dir) - with tf.gfile.Open(os.path.join(FLAGS.eval_dir, 'pipeline.config'), - 'wb') as f: - f.write(config_text) - - model_config = configs['model'] - lstm_config = configs['lstm_model'] - eval_config = configs['eval_config'] - input_config = configs['eval_input_config'] - - if FLAGS.eval_training_data: - input_config.external_input_reader.CopyFrom( - configs['train_input_config'].external_input_reader) - lstm_config.eval_unroll_length = lstm_config.train_unroll_length - - model_fn = functools.partial( - model_builder.build, - model_config=model_config, - lstm_config=lstm_config, - is_training=False) - - def get_next(config, model_config, lstm_config, unroll_length): - return seq_dataset_builder.build(config, model_config, lstm_config, - unroll_length) - - create_input_dict_fn = functools.partial(get_next, input_config, model_config, - lstm_config, - lstm_config.eval_unroll_length) - - label_map = label_map_util.load_labelmap(input_config.label_map_path) - max_num_classes = max([item.id for item in label_map.item]) - categories = label_map_util.convert_label_map_to_categories( - label_map, max_num_classes) - - if FLAGS.run_once: - eval_config.max_evals = 1 - - evaluator.evaluate(create_input_dict_fn, model_fn, eval_config, categories, - FLAGS.checkpoint_dir, FLAGS.eval_dir) - -if __name__ == '__main__': - tf.app.run() diff --git a/research/lstm_object_detection/evaluator.py b/research/lstm_object_detection/evaluator.py deleted file mode 100644 index 6ed3e476e8e..00000000000 --- a/research/lstm_object_detection/evaluator.py +++ /dev/null @@ -1,337 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Detection model evaluator. - -This file provides a generic evaluation method that can be used to evaluate a -DetectionModel. - -""" - -import tensorflow.compat.v1 as tf -from tensorflow.contrib import tfprof as contrib_tfprof -from lstm_object_detection.metrics import coco_evaluation_all_frames -from object_detection import eval_util -from object_detection.core import prefetcher -from object_detection.core import standard_fields as fields -from object_detection.metrics import coco_evaluation -from object_detection.utils import object_detection_evaluation - - -# A dictionary of metric names to classes that implement the metric. The classes -# in the dictionary must implement -# utils.object_detection_evaluation.DetectionEvaluator interface. -EVAL_METRICS_CLASS_DICT = { - 'pascal_voc_detection_metrics': - object_detection_evaluation.PascalDetectionEvaluator, - 'weighted_pascal_voc_detection_metrics': - object_detection_evaluation.WeightedPascalDetectionEvaluator, - 'pascal_voc_instance_segmentation_metrics': - object_detection_evaluation.PascalInstanceSegmentationEvaluator, - 'weighted_pascal_voc_instance_segmentation_metrics': - object_detection_evaluation.WeightedPascalInstanceSegmentationEvaluator, - 'open_images_detection_metrics': - object_detection_evaluation.OpenImagesDetectionEvaluator, - 'coco_detection_metrics': - coco_evaluation.CocoDetectionEvaluator, - 'coco_mask_metrics': - coco_evaluation.CocoMaskEvaluator, - 'coco_evaluation_all_frames': - coco_evaluation_all_frames.CocoEvaluationAllFrames, -} - -EVAL_DEFAULT_METRIC = 'pascal_voc_detection_metrics' - - -def _create_detection_op(model, input_dict, batch): - """Create detection ops. - - Args: - model: model to perform predictions with. - input_dict: A dict holds input data. - batch: batch size for evaluation. - - Returns: - Detection tensor ops. - """ - video_tensor = tf.stack(list(input_dict[fields.InputDataFields.image])) - preprocessed_video, true_image_shapes = model.preprocess( - tf.to_float(video_tensor)) - if batch is not None: - prediction_dict = model.predict(preprocessed_video, true_image_shapes, - batch) - else: - prediction_dict = model.predict(preprocessed_video, true_image_shapes) - - return model.postprocess(prediction_dict, true_image_shapes) - - -def _extract_prediction_tensors(model, - create_input_dict_fn, - ignore_groundtruth=False): - """Restores the model in a tensorflow session. - - Args: - model: model to perform predictions with. - create_input_dict_fn: function to create input tensor dictionaries. - ignore_groundtruth: whether groundtruth should be ignored. - - - Returns: - tensor_dict: A tensor dictionary with evaluations. - """ - input_dict = create_input_dict_fn() - batch = None - if 'batch' in input_dict: - batch = input_dict.pop('batch') - else: - prefetch_queue = prefetcher.prefetch(input_dict, capacity=500) - input_dict = prefetch_queue.dequeue() - # consistent format for images and videos - for key, value in input_dict.iteritems(): - input_dict[key] = (value,) - - detections = _create_detection_op(model, input_dict, batch) - - # Print out anaylsis of the model. - contrib_tfprof.model_analyzer.print_model_analysis( - tf.get_default_graph(), - tfprof_options=contrib_tfprof.model_analyzer - .TRAINABLE_VARS_PARAMS_STAT_OPTIONS) - contrib_tfprof.model_analyzer.print_model_analysis( - tf.get_default_graph(), - tfprof_options=contrib_tfprof.model_analyzer.FLOAT_OPS_OPTIONS) - - num_frames = len(input_dict[fields.InputDataFields.image]) - ret = [] - for i in range(num_frames): - original_image = tf.expand_dims(input_dict[fields.InputDataFields.image][i], - 0) - groundtruth = None - if not ignore_groundtruth: - groundtruth = { - fields.InputDataFields.groundtruth_boxes: - input_dict[fields.InputDataFields.groundtruth_boxes][i], - fields.InputDataFields.groundtruth_classes: - input_dict[fields.InputDataFields.groundtruth_classes][i], - } - optional_keys = ( - fields.InputDataFields.groundtruth_area, - fields.InputDataFields.groundtruth_is_crowd, - fields.InputDataFields.groundtruth_difficult, - fields.InputDataFields.groundtruth_group_of, - ) - for opt_key in optional_keys: - if opt_key in input_dict: - groundtruth[opt_key] = input_dict[opt_key][i] - if fields.DetectionResultFields.detection_masks in detections: - groundtruth[fields.InputDataFields.groundtruth_instance_masks] = ( - input_dict[fields.InputDataFields.groundtruth_instance_masks][i]) - - detections_frame = { - key: tf.expand_dims(value[i], 0) - for key, value in detections.iteritems() - } - - source_id = ( - batch.key[0] if batch is not None else - input_dict[fields.InputDataFields.source_id][i]) - ret.append( - eval_util.result_dict_for_single_example( - original_image, - source_id, - detections_frame, - groundtruth, - class_agnostic=(fields.DetectionResultFields.detection_classes - not in detections), - scale_to_absolute=True)) - return ret - - -def get_evaluators(eval_config, categories): - """Returns the evaluator class according to eval_config, valid for categories. - - Args: - eval_config: evaluation configurations. - categories: a list of categories to evaluate. - Returns: - An list of instances of DetectionEvaluator. - - Raises: - ValueError: if metric is not in the metric class dictionary. - """ - eval_metric_fn_keys = eval_config.metrics_set - if not eval_metric_fn_keys: - eval_metric_fn_keys = [EVAL_DEFAULT_METRIC] - evaluators_list = [] - for eval_metric_fn_key in eval_metric_fn_keys: - if eval_metric_fn_key not in EVAL_METRICS_CLASS_DICT: - raise ValueError('Metric not found: {}'.format(eval_metric_fn_key)) - else: - evaluators_list.append( - EVAL_METRICS_CLASS_DICT[eval_metric_fn_key](categories=categories)) - return evaluators_list - - -def evaluate(create_input_dict_fn, - create_model_fn, - eval_config, - categories, - checkpoint_dir, - eval_dir, - graph_hook_fn=None): - """Evaluation function for detection models. - - Args: - create_input_dict_fn: a function to create a tensor input dictionary. - create_model_fn: a function that creates a DetectionModel. - eval_config: a eval_pb2.EvalConfig protobuf. - categories: a list of category dictionaries. Each dict in the list should - have an integer 'id' field and string 'name' field. - checkpoint_dir: directory to load the checkpoints to evaluate from. - eval_dir: directory to write evaluation metrics summary to. - graph_hook_fn: Optional function that is called after the training graph is - completely built. This is helpful to perform additional changes to the - training graph such as optimizing batchnorm. The function should modify - the default graph. - - Returns: - metrics: A dictionary containing metric names and values from the latest - run. - """ - - model = create_model_fn() - - if eval_config.ignore_groundtruth and not eval_config.export_path: - tf.logging.fatal('If ignore_groundtruth=True then an export_path is ' - 'required. Aborting!!!') - - tensor_dicts = _extract_prediction_tensors( - model=model, - create_input_dict_fn=create_input_dict_fn, - ignore_groundtruth=eval_config.ignore_groundtruth) - - def _process_batch(tensor_dicts, - sess, - batch_index, - counters, - losses_dict=None): - """Evaluates tensors in tensor_dicts, visualizing the first K examples. - - This function calls sess.run on tensor_dicts, evaluating the original_image - tensor only on the first K examples and visualizing detections overlaid - on this original_image. - - Args: - tensor_dicts: a dictionary of tensors - sess: tensorflow session - batch_index: the index of the batch amongst all batches in the run. - counters: a dictionary holding 'success' and 'skipped' fields which can - be updated to keep track of number of successful and failed runs, - respectively. If these fields are not updated, then the success/skipped - counter values shown at the end of evaluation will be incorrect. - losses_dict: Optional dictonary of scalar loss tensors. Necessary only - for matching function signiture in third_party eval_util.py. - - Returns: - result_dict: a dictionary of numpy arrays - result_losses_dict: a dictionary of scalar losses. This is empty if input - losses_dict is None. Necessary only for matching function signiture in - third_party eval_util.py. - """ - if batch_index % 10 == 0: - tf.logging.info('Running eval ops batch %d', batch_index) - if not losses_dict: - losses_dict = {} - try: - result_dicts, result_losses_dict = sess.run([tensor_dicts, losses_dict]) - counters['success'] += 1 - except tf.errors.InvalidArgumentError: - tf.logging.info('Skipping image') - counters['skipped'] += 1 - return {} - num_images = len(tensor_dicts) - for i in range(num_images): - result_dict = result_dicts[i] - global_step = tf.train.global_step(sess, tf.train.get_global_step()) - tag = 'image-%d' % (batch_index * num_images + i) - if batch_index < eval_config.num_visualizations / num_images: - eval_util.visualize_detection_results( - result_dict, - tag, - global_step, - categories=categories, - summary_dir=eval_dir, - export_dir=eval_config.visualization_export_dir, - show_groundtruth=eval_config.visualize_groundtruth_boxes, - groundtruth_box_visualization_color=eval_config. - groundtruth_box_visualization_color, - min_score_thresh=eval_config.min_score_threshold, - max_num_predictions=eval_config.max_num_boxes_to_visualize, - skip_scores=eval_config.skip_scores, - skip_labels=eval_config.skip_labels, - keep_image_id_for_visualization_export=eval_config. - keep_image_id_for_visualization_export) - if num_images > 1: - return result_dicts, result_losses_dict - else: - return result_dicts[0], result_losses_dict - - variables_to_restore = tf.global_variables() - global_step = tf.train.get_or_create_global_step() - variables_to_restore.append(global_step) - - if graph_hook_fn: - graph_hook_fn() - - if eval_config.use_moving_averages: - variable_averages = tf.train.ExponentialMovingAverage(0.0) - variables_to_restore = variable_averages.variables_to_restore() - for key in variables_to_restore.keys(): - if 'moving_mean' in key: - variables_to_restore[key.replace( - 'moving_mean', 'moving_mean/ExponentialMovingAverage')] = ( - variables_to_restore[key]) - del variables_to_restore[key] - if 'moving_variance' in key: - variables_to_restore[key.replace( - 'moving_variance', 'moving_variance/ExponentialMovingAverage')] = ( - variables_to_restore[key]) - del variables_to_restore[key] - - saver = tf.train.Saver(variables_to_restore) - - def _restore_latest_checkpoint(sess): - latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir) - saver.restore(sess, latest_checkpoint) - - metrics = eval_util.repeated_checkpoint_run( - tensor_dict=tensor_dicts, - summary_dir=eval_dir, - evaluators=get_evaluators(eval_config, categories), - batch_processor=_process_batch, - checkpoint_dirs=[checkpoint_dir], - variables_to_restore=None, - restore_fn=_restore_latest_checkpoint, - num_batches=eval_config.num_examples, - eval_interval_secs=eval_config.eval_interval_secs, - max_number_of_evaluations=(1 if eval_config.ignore_groundtruth else - eval_config.max_evals - if eval_config.max_evals else None), - master=eval_config.eval_master, - save_graph=eval_config.save_graph, - save_graph_dir=(eval_dir if eval_config.save_graph else '')) - - return metrics diff --git a/research/lstm_object_detection/export_tflite_lstd_graph.py b/research/lstm_object_detection/export_tflite_lstd_graph.py deleted file mode 100644 index 7e933fb480d..00000000000 --- a/research/lstm_object_detection/export_tflite_lstd_graph.py +++ /dev/null @@ -1,138 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Exports an LSTM detection model to use with tf-lite. - -Outputs file: -* A tflite compatible frozen graph - $output_directory/tflite_graph.pb - -The exported graph has the following input and output nodes. - -Inputs: -'input_video_tensor': a float32 tensor of shape -[unroll_length, height, width, 3] containing the normalized input image. -Note that the height and width must be compatible with the height and -width configured in the fixed_shape_image resizer options in the pipeline -config proto. - -Outputs: -If add_postprocessing_op is true: frozen graph adds a - TFLite_Detection_PostProcess custom op node has four outputs: - detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box - locations - detection_classes: a float32 tensor of shape [1, num_boxes] - with class indices - detection_scores: a float32 tensor of shape [1, num_boxes] - with class scores - num_boxes: a float32 tensor of size 1 containing the number of detected boxes -else: - the graph has three outputs: - 'raw_outputs/box_encodings': a float32 tensor of shape [1, num_anchors, 4] - containing the encoded box predictions. - 'raw_outputs/class_predictions': a float32 tensor of shape - [1, num_anchors, num_classes] containing the class scores for each anchor - after applying score conversion. - 'anchors': a float32 constant tensor of shape [num_anchors, 4] - containing the anchor boxes. - -Example Usage: --------------- -python lstm_object_detection/export_tflite_lstd_graph.py \ - --pipeline_config_path path/to/lstm_pipeline.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory - -The expected output would be in the directory -path/to/exported_model_directory (which is created if it does not exist) -with contents: - - tflite_graph.pbtxt - - tflite_graph.pb -Config overrides (see the `config_override` flag) are text protobufs -(also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override -certain fields in the provided pipeline_config_path. These are useful for -making small changes to the inference graph that differ from the training or -eval config. - -Example Usage (in which we change the NMS iou_threshold to be 0.5 and -NMS score_threshold to be 0.0): -python lstm_object_detection/export_tflite_lstd_graph.py \ - --pipeline_config_path path/to/lstm_pipeline.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory - --config_override " \ - model{ \ - ssd{ \ - post_processing { \ - batch_non_max_suppression { \ - score_threshold: 0.0 \ - iou_threshold: 0.5 \ - } \ - } \ - } \ - } \ - " -""" - -import tensorflow.compat.v1 as tf - -from lstm_object_detection import export_tflite_lstd_graph_lib -from lstm_object_detection.utils import config_util - -flags = tf.app.flags -flags.DEFINE_string('output_directory', None, 'Path to write outputs.') -flags.DEFINE_string( - 'pipeline_config_path', None, - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file.') -flags.DEFINE_string('trained_checkpoint_prefix', None, 'Checkpoint prefix.') -flags.DEFINE_integer('max_detections', 10, - 'Maximum number of detections (boxes) to show.') -flags.DEFINE_integer('max_classes_per_detection', 1, - 'Maximum number of classes to output per detection box.') -flags.DEFINE_integer( - 'detections_per_class', 100, - 'Number of anchors used per class in Regular Non-Max-Suppression.') -flags.DEFINE_bool('add_postprocessing_op', True, - 'Add TFLite custom op for postprocessing to the graph.') -flags.DEFINE_bool( - 'use_regular_nms', False, - 'Flag to set postprocessing op to use Regular NMS instead of Fast NMS.') -flags.DEFINE_string( - 'config_override', '', 'pipeline_pb2.TrainEvalPipelineConfig ' - 'text proto to override pipeline_config_path.') - -FLAGS = flags.FLAGS - - -def main(argv): - del argv # Unused. - flags.mark_flag_as_required('output_directory') - flags.mark_flag_as_required('pipeline_config_path') - flags.mark_flag_as_required('trained_checkpoint_prefix') - - pipeline_config = config_util.get_configs_from_pipeline_file( - FLAGS.pipeline_config_path) - - export_tflite_lstd_graph_lib.export_tflite_graph( - pipeline_config, - FLAGS.trained_checkpoint_prefix, - FLAGS.output_directory, - FLAGS.add_postprocessing_op, - FLAGS.max_detections, - FLAGS.max_classes_per_detection, - use_regular_nms=FLAGS.use_regular_nms) - - -if __name__ == '__main__': - tf.app.run(main) diff --git a/research/lstm_object_detection/export_tflite_lstd_graph_lib.py b/research/lstm_object_detection/export_tflite_lstd_graph_lib.py deleted file mode 100644 index e066f11b45f..00000000000 --- a/research/lstm_object_detection/export_tflite_lstd_graph_lib.py +++ /dev/null @@ -1,327 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Exports detection models to use with tf-lite. - -See export_tflite_lstd_graph.py for usage. -""" -import os -import tempfile - -import numpy as np -import tensorflow.compat.v1 as tf - -from tensorflow.core.framework import attr_value_pb2 -from tensorflow.core.framework import types_pb2 -from tensorflow.core.protobuf import saver_pb2 -from tensorflow.tools.graph_transforms import TransformGraph -from lstm_object_detection import model_builder -from object_detection import exporter -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import post_processing_builder -from object_detection.core import box_list - -_DEFAULT_NUM_CHANNELS = 3 -_DEFAULT_NUM_COORD_BOX = 4 - - -def get_const_center_size_encoded_anchors(anchors): - """Exports center-size encoded anchors as a constant tensor. - - Args: - anchors: a float32 tensor of shape [num_anchors, 4] containing the anchor - boxes - - Returns: - encoded_anchors: a float32 constant tensor of shape [num_anchors, 4] - containing the anchor boxes. - """ - anchor_boxlist = box_list.BoxList(anchors) - y, x, h, w = anchor_boxlist.get_center_coordinates_and_sizes() - num_anchors = y.get_shape().as_list() - - with tf.Session() as sess: - y_out, x_out, h_out, w_out = sess.run([y, x, h, w]) - encoded_anchors = tf.constant( - np.transpose(np.stack((y_out, x_out, h_out, w_out))), - dtype=tf.float32, - shape=[num_anchors[0], _DEFAULT_NUM_COORD_BOX], - name='anchors') - return encoded_anchors - - -def append_postprocessing_op(frozen_graph_def, - max_detections, - max_classes_per_detection, - nms_score_threshold, - nms_iou_threshold, - num_classes, - scale_values, - detections_per_class=100, - use_regular_nms=False): - """Appends postprocessing custom op. - - Args: - frozen_graph_def: Frozen GraphDef for SSD model after freezing the - checkpoint - max_detections: Maximum number of detections (boxes) to show - max_classes_per_detection: Number of classes to display per detection - nms_score_threshold: Score threshold used in Non-maximal suppression in - post-processing - nms_iou_threshold: Intersection-over-union threshold used in Non-maximal - suppression in post-processing - num_classes: number of classes in SSD detector - scale_values: scale values is a dict with following key-value pairs - {y_scale: 10, x_scale: 10, h_scale: 5, w_scale: 5} that are used in decode - centersize boxes - detections_per_class: In regular NonMaxSuppression, number of anchors used - for NonMaxSuppression per class - use_regular_nms: Flag to set postprocessing op to use Regular NMS instead of - Fast NMS. - - Returns: - transformed_graph_def: Frozen GraphDef with postprocessing custom op - appended - TFLite_Detection_PostProcess custom op node has four outputs: - detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box - locations - detection_classes: a float32 tensor of shape [1, num_boxes] - with class indices - detection_scores: a float32 tensor of shape [1, num_boxes] - with class scores - num_boxes: a float32 tensor of size 1 containing the number of detected - boxes - """ - new_output = frozen_graph_def.node.add() - new_output.op = 'TFLite_Detection_PostProcess' - new_output.name = 'TFLite_Detection_PostProcess' - new_output.attr['_output_quantized'].CopyFrom( - attr_value_pb2.AttrValue(b=True)) - new_output.attr['_output_types'].list.type.extend([ - types_pb2.DT_FLOAT, types_pb2.DT_FLOAT, types_pb2.DT_FLOAT, - types_pb2.DT_FLOAT - ]) - new_output.attr['_support_output_type_float_in_quantized_op'].CopyFrom( - attr_value_pb2.AttrValue(b=True)) - new_output.attr['max_detections'].CopyFrom( - attr_value_pb2.AttrValue(i=max_detections)) - new_output.attr['max_classes_per_detection'].CopyFrom( - attr_value_pb2.AttrValue(i=max_classes_per_detection)) - new_output.attr['nms_score_threshold'].CopyFrom( - attr_value_pb2.AttrValue(f=nms_score_threshold.pop())) - new_output.attr['nms_iou_threshold'].CopyFrom( - attr_value_pb2.AttrValue(f=nms_iou_threshold.pop())) - new_output.attr['num_classes'].CopyFrom( - attr_value_pb2.AttrValue(i=num_classes)) - - new_output.attr['y_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['y_scale'].pop())) - new_output.attr['x_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['x_scale'].pop())) - new_output.attr['h_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['h_scale'].pop())) - new_output.attr['w_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['w_scale'].pop())) - new_output.attr['detections_per_class'].CopyFrom( - attr_value_pb2.AttrValue(i=detections_per_class)) - new_output.attr['use_regular_nms'].CopyFrom( - attr_value_pb2.AttrValue(b=use_regular_nms)) - - new_output.input.extend( - ['raw_outputs/box_encodings', 'raw_outputs/class_predictions', 'anchors']) - # Transform the graph to append new postprocessing op - input_names = [] - output_names = ['TFLite_Detection_PostProcess'] - transforms = ['strip_unused_nodes'] - transformed_graph_def = TransformGraph(frozen_graph_def, input_names, - output_names, transforms) - return transformed_graph_def - - -def export_tflite_graph(pipeline_config, - trained_checkpoint_prefix, - output_dir, - add_postprocessing_op, - max_detections, - max_classes_per_detection, - detections_per_class=100, - use_regular_nms=False, - binary_graph_name='tflite_graph.pb', - txt_graph_name='tflite_graph.pbtxt'): - """Exports a tflite compatible graph and anchors for ssd detection model. - - Anchors are written to a tensor and tflite compatible graph - is written to output_dir/tflite_graph.pb. - - Args: - pipeline_config: Dictionary of configuration objects. Keys are `model`, - `train_config`, `train_input_config`, `eval_config`, `eval_input_config`, - `lstm_model`. Value are the corresponding config objects. - trained_checkpoint_prefix: a file prefix for the checkpoint containing the - trained parameters of the SSD model. - output_dir: A directory to write the tflite graph and anchor file to. - add_postprocessing_op: If add_postprocessing_op is true: frozen graph adds a - TFLite_Detection_PostProcess custom op - max_detections: Maximum number of detections (boxes) to show - max_classes_per_detection: Number of classes to display per detection - detections_per_class: In regular NonMaxSuppression, number of anchors used - for NonMaxSuppression per class - use_regular_nms: Flag to set postprocessing op to use Regular NMS instead of - Fast NMS. - binary_graph_name: Name of the exported graph file in binary format. - txt_graph_name: Name of the exported graph file in text format. - - Raises: - ValueError: if the pipeline config contains models other than ssd or uses an - fixed_shape_resizer and provides a shape as well. - """ - model_config = pipeline_config['model'] - lstm_config = pipeline_config['lstm_model'] - eval_config = pipeline_config['eval_config'] - tf.gfile.MakeDirs(output_dir) - if model_config.WhichOneof('model') != 'ssd': - raise ValueError('Only ssd models are supported in tflite. ' - 'Found {} in config'.format( - model_config.WhichOneof('model'))) - - num_classes = model_config.ssd.num_classes - nms_score_threshold = { - model_config.ssd.post_processing.batch_non_max_suppression.score_threshold - } - nms_iou_threshold = { - model_config.ssd.post_processing.batch_non_max_suppression.iou_threshold - } - scale_values = {} - scale_values['y_scale'] = { - model_config.ssd.box_coder.faster_rcnn_box_coder.y_scale - } - scale_values['x_scale'] = { - model_config.ssd.box_coder.faster_rcnn_box_coder.x_scale - } - scale_values['h_scale'] = { - model_config.ssd.box_coder.faster_rcnn_box_coder.height_scale - } - scale_values['w_scale'] = { - model_config.ssd.box_coder.faster_rcnn_box_coder.width_scale - } - - image_resizer_config = model_config.ssd.image_resizer - image_resizer = image_resizer_config.WhichOneof('image_resizer_oneof') - num_channels = _DEFAULT_NUM_CHANNELS - if image_resizer == 'fixed_shape_resizer': - height = image_resizer_config.fixed_shape_resizer.height - width = image_resizer_config.fixed_shape_resizer.width - if image_resizer_config.fixed_shape_resizer.convert_to_grayscale: - num_channels = 1 - - shape = [lstm_config.eval_unroll_length, height, width, num_channels] - else: - raise ValueError( - 'Only fixed_shape_resizer' - 'is supported with tflite. Found {}'.format( - image_resizer_config.WhichOneof('image_resizer_oneof'))) - - video_tensor = tf.placeholder( - tf.float32, shape=shape, name='input_video_tensor') - - detection_model = model_builder.build( - model_config, lstm_config, is_training=False) - preprocessed_video, true_image_shapes = detection_model.preprocess( - tf.to_float(video_tensor)) - predicted_tensors = detection_model.predict(preprocessed_video, - true_image_shapes) - # predicted_tensors = detection_model.postprocess(predicted_tensors, - # true_image_shapes) - # The score conversion occurs before the post-processing custom op - _, score_conversion_fn = post_processing_builder.build( - model_config.ssd.post_processing) - class_predictions = score_conversion_fn( - predicted_tensors['class_predictions_with_background']) - - with tf.name_scope('raw_outputs'): - # 'raw_outputs/box_encodings': a float32 tensor of shape [1, num_anchors, 4] - # containing the encoded box predictions. Note that these are raw - # predictions and no Non-Max suppression is applied on them and - # no decode center size boxes is applied to them. - tf.identity(predicted_tensors['box_encodings'], name='box_encodings') - # 'raw_outputs/class_predictions': a float32 tensor of shape - # [1, num_anchors, num_classes] containing the class scores for each anchor - # after applying score conversion. - tf.identity(class_predictions, name='class_predictions') - # 'anchors': a float32 tensor of shape - # [4, num_anchors] containing the anchors as a constant node. - tf.identity( - get_const_center_size_encoded_anchors(predicted_tensors['anchors']), - name='anchors') - - # Add global step to the graph, so we know the training step number when we - # evaluate the model. - tf.train.get_or_create_global_step() - - # graph rewriter - is_quantized = ('graph_rewriter' in pipeline_config) - if is_quantized: - graph_rewriter_config = pipeline_config['graph_rewriter'] - graph_rewriter_fn = graph_rewriter_builder.build( - graph_rewriter_config, is_training=False, is_export=True) - graph_rewriter_fn() - - if model_config.ssd.feature_extractor.HasField('fpn'): - exporter.rewrite_nn_resize_op(is_quantized) - - # freeze the graph - saver_kwargs = {} - if eval_config.use_moving_averages: - saver_kwargs['write_version'] = saver_pb2.SaverDef.V1 - moving_average_checkpoint = tempfile.NamedTemporaryFile() - exporter.replace_variable_values_with_moving_averages( - tf.get_default_graph(), trained_checkpoint_prefix, - moving_average_checkpoint.name) - checkpoint_to_use = moving_average_checkpoint.name - else: - checkpoint_to_use = trained_checkpoint_prefix - - saver = tf.train.Saver(**saver_kwargs) - input_saver_def = saver.as_saver_def() - frozen_graph_def = exporter.freeze_graph_with_def_protos( - input_graph_def=tf.get_default_graph().as_graph_def(), - input_saver_def=input_saver_def, - input_checkpoint=checkpoint_to_use, - output_node_names=','.join([ - 'raw_outputs/box_encodings', 'raw_outputs/class_predictions', - 'anchors' - ]), - restore_op_name='save/restore_all', - filename_tensor_name='save/Const:0', - clear_devices=True, - output_graph='', - initializer_nodes='') - - # Add new operation to do post processing in a custom op (TF Lite only) - - if add_postprocessing_op: - transformed_graph_def = append_postprocessing_op( - frozen_graph_def, max_detections, max_classes_per_detection, - nms_score_threshold, nms_iou_threshold, num_classes, scale_values, - detections_per_class, use_regular_nms) - else: - # Return frozen without adding post-processing custom op - transformed_graph_def = frozen_graph_def - - binary_graph = os.path.join(output_dir, binary_graph_name) - with tf.gfile.GFile(binary_graph, 'wb') as f: - f.write(transformed_graph_def.SerializeToString()) - txt_graph = os.path.join(output_dir, txt_graph_name) - with tf.gfile.GFile(txt_graph, 'w') as f: - f.write(str(transformed_graph_def)) diff --git a/research/lstm_object_detection/export_tflite_lstd_model.py b/research/lstm_object_detection/export_tflite_lstd_model.py deleted file mode 100644 index 58c674728b5..00000000000 --- a/research/lstm_object_detection/export_tflite_lstd_model.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Export a LSTD model in tflite format.""" - -import os -from absl import flags -import tensorflow.compat.v1 as tf - -from lstm_object_detection.utils import config_util - -flags.DEFINE_string('export_path', None, 'Path to export model.') -flags.DEFINE_string('frozen_graph_path', None, 'Path to frozen graph.') -flags.DEFINE_string( - 'pipeline_config_path', '', - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config file.') - -FLAGS = flags.FLAGS - - -def main(_): - flags.mark_flag_as_required('export_path') - flags.mark_flag_as_required('frozen_graph_path') - flags.mark_flag_as_required('pipeline_config_path') - - configs = config_util.get_configs_from_pipeline_file( - FLAGS.pipeline_config_path) - lstm_config = configs['lstm_model'] - - input_arrays = ['input_video_tensor'] - output_arrays = [ - 'TFLite_Detection_PostProcess', - 'TFLite_Detection_PostProcess:1', - 'TFLite_Detection_PostProcess:2', - 'TFLite_Detection_PostProcess:3', - ] - input_shapes = { - 'input_video_tensor': [lstm_config.eval_unroll_length, 320, 320, 3], - } - - converter = tf.lite.TFLiteConverter.from_frozen_graph( - FLAGS.frozen_graph_path, - input_arrays, - output_arrays, - input_shapes=input_shapes) - converter.allow_custom_ops = True - tflite_model = converter.convert() - ofilename = os.path.join(FLAGS.export_path) - open(ofilename, 'wb').write(tflite_model) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/lstm_object_detection/g3doc/Interleaved_Intro.png b/research/lstm_object_detection/g3doc/Interleaved_Intro.png deleted file mode 100644 index 2b829c997bc..00000000000 Binary files a/research/lstm_object_detection/g3doc/Interleaved_Intro.png and /dev/null differ diff --git a/research/lstm_object_detection/g3doc/exporting_models.md b/research/lstm_object_detection/g3doc/exporting_models.md deleted file mode 100644 index 7d501d97efd..00000000000 --- a/research/lstm_object_detection/g3doc/exporting_models.md +++ /dev/null @@ -1,49 +0,0 @@ -# Exporting a tflite model from a checkpoint - -Starting from a trained model checkpoint, creating a tflite model requires 2 -steps: - -* exporting a tflite frozen graph from a checkpoint -* exporting a tflite model from a frozen graph - -## Exporting a tflite frozen graph from a checkpoint - -With a candidate checkpoint to export, run the following command from -tensorflow/models/research: - -```bash -# from tensorflow/models/research -PIPELINE_CONFIG_PATH={path to pipeline config} -TRAINED_CKPT_PREFIX=/{path to model.ckpt} -EXPORT_DIR={path to folder that will be used for export} -python lstm_object_detection/export_tflite_lstd_graph.py \ - --pipeline_config_path ${PIPELINE_CONFIG_PATH} \ - --trained_checkpoint_prefix ${TRAINED_CKPT_PREFIX} \ - --output_directory ${EXPORT_DIR} \ - --add_preprocessing_op -``` - -After export, you should see the directory ${EXPORT_DIR} containing the -following files: - -* `tflite_graph.pb` -* `tflite_graph.pbtxt` - -## Exporting a tflite model from a frozen graph - -We then take the exported tflite-compatable tflite model, and convert it to a -TFLite FlatBuffer file by running the following: - -```bash -# from tensorflow/models/research -FROZEN_GRAPH_PATH={path to exported tflite_graph.pb} -EXPORT_PATH={path to filename that will be used for export} -PIPELINE_CONFIG_PATH={path to pipeline config} -python lstm_object_detection/export_tflite_lstd_model.py \ - --export_path ${EXPORT_PATH} \ - --frozen_graph_path ${FROZEN_GRAPH_PATH} \ - --pipeline_config_path ${PIPELINE_CONFIG_PATH} -``` - -After export, you should see the file ${EXPORT_PATH} containing the FlatBuffer -model to be used by an application. diff --git a/research/lstm_object_detection/g3doc/lstm_ssd_intro.png b/research/lstm_object_detection/g3doc/lstm_ssd_intro.png deleted file mode 100644 index fa62eb533b9..00000000000 Binary files a/research/lstm_object_detection/g3doc/lstm_ssd_intro.png and /dev/null differ diff --git a/research/lstm_object_detection/inputs/__init__.py b/research/lstm_object_detection/inputs/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/inputs/seq_dataset_builder.py b/research/lstm_object_detection/inputs/seq_dataset_builder.py deleted file mode 100644 index 55e24820f60..00000000000 --- a/research/lstm_object_detection/inputs/seq_dataset_builder.py +++ /dev/null @@ -1,242 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""tf.data.Dataset builder. - -Creates data sources for DetectionModels from an InputReader config. See -input_reader.proto for options. - -Note: If users wishes to also use their own InputReaders with the Object -Detection configuration framework, they should define their own builder function -that wraps the build function. -""" -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from tensorflow.contrib.training.python.training import sequence_queueing_state_saver as sqss -from lstm_object_detection.inputs import tf_sequence_example_decoder -from lstm_object_detection.protos import input_reader_google_pb2 -from object_detection.core import preprocessor -from object_detection.core import preprocessor_cache -from object_detection.core import standard_fields as fields -from object_detection.protos import input_reader_pb2 -from object_detection.utils import ops as util_ops - -parallel_reader = slim.parallel_reader -# TODO(yinxiao): Make the following variable into configurable proto. -# Padding size for the labeled objects in each frame. Here we assume each -# frame has a total number of objects less than _PADDING_SIZE. -_PADDING_SIZE = 30 - - -def _build_training_batch_dict(batch_sequences_with_states, unroll_length, - batch_size): - """Builds training batch samples. - - Args: - batch_sequences_with_states: A batch_sequences_with_states object. - unroll_length: Unrolled length for LSTM training. - batch_size: Batch size for queue outputs. - - Returns: - A dictionary of tensors based on items in input_reader_config. - """ - seq_tensors_dict = { - fields.InputDataFields.image: [], - fields.InputDataFields.groundtruth_boxes: [], - fields.InputDataFields.groundtruth_classes: [], - 'batch': batch_sequences_with_states, - } - for i in range(unroll_length): - for j in range(batch_size): - filtered_dict = util_ops.filter_groundtruth_with_nan_box_coordinates({ - fields.InputDataFields.groundtruth_boxes: ( - batch_sequences_with_states.sequences['groundtruth_boxes'][j][i]), - fields.InputDataFields.groundtruth_classes: ( - batch_sequences_with_states.sequences['groundtruth_classes'][j][i] - ), - }) - filtered_dict = util_ops.retain_groundtruth_with_positive_classes( - filtered_dict) - seq_tensors_dict[fields.InputDataFields.image].append( - batch_sequences_with_states.sequences['image'][j][i]) - seq_tensors_dict[fields.InputDataFields.groundtruth_boxes].append( - filtered_dict[fields.InputDataFields.groundtruth_boxes]) - seq_tensors_dict[fields.InputDataFields.groundtruth_classes].append( - filtered_dict[fields.InputDataFields.groundtruth_classes]) - seq_tensors_dict[fields.InputDataFields.image] = tuple( - seq_tensors_dict[fields.InputDataFields.image]) - seq_tensors_dict[fields.InputDataFields.groundtruth_boxes] = tuple( - seq_tensors_dict[fields.InputDataFields.groundtruth_boxes]) - seq_tensors_dict[fields.InputDataFields.groundtruth_classes] = tuple( - seq_tensors_dict[fields.InputDataFields.groundtruth_classes]) - - return seq_tensors_dict - - -def build(input_reader_config, - model_config, - lstm_config, - unroll_length, - data_augmentation_options=None, - batch_size=1): - """Builds a tensor dictionary based on the InputReader config. - - Args: - input_reader_config: An input_reader_builder.InputReader object. - model_config: A model.proto object containing the config for the desired - DetectionModel. - lstm_config: LSTM specific configs. - unroll_length: Unrolled length for LSTM training. - data_augmentation_options: A list of tuples, where each tuple contains a - data augmentation function and a dictionary containing arguments and their - values (see preprocessor.py). - batch_size: Batch size for queue outputs. - - Returns: - A dictionary of tensors based on items in the input_reader_config. - - Raises: - ValueError: On invalid input reader proto. - ValueError: If no input paths are specified. - """ - if not isinstance(input_reader_config, input_reader_pb2.InputReader): - raise ValueError('input_reader_config not of type ' - 'input_reader_pb2.InputReader.') - - external_reader_config = input_reader_config.external_input_reader - external_input_reader_config = external_reader_config.Extensions[ - input_reader_google_pb2.GoogleInputReader.google_input_reader] - input_reader_type = external_input_reader_config.WhichOneof('input_reader') - - if input_reader_type == 'tf_record_video_input_reader': - config = external_input_reader_config.tf_record_video_input_reader - reader_type_class = tf.TFRecordReader - else: - raise ValueError( - 'Unsupported reader in input_reader_config: %s' % input_reader_type) - - if not config.input_path: - raise ValueError('At least one input path must be specified in ' - '`input_reader_config`.') - key, value = parallel_reader.parallel_read( - config.input_path[:], # Convert `RepeatedScalarContainer` to list. - reader_class=reader_type_class, - num_epochs=(input_reader_config.num_epochs - if input_reader_config.num_epochs else None), - num_readers=input_reader_config.num_readers, - shuffle=input_reader_config.shuffle, - dtypes=[tf.string, tf.string], - capacity=input_reader_config.queue_capacity, - min_after_dequeue=input_reader_config.min_after_dequeue) - - # TODO(yinxiao): Add loading instance mask option. - decoder = tf_sequence_example_decoder.TFSequenceExampleDecoder() - - keys_to_decode = [ - fields.InputDataFields.image, fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes - ] - tensor_dict = decoder.decode(value, items=keys_to_decode) - - tensor_dict['image'].set_shape([None, None, None, 3]) - tensor_dict['groundtruth_boxes'].set_shape([None, None, 4]) - - height = model_config.ssd.image_resizer.fixed_shape_resizer.height - width = model_config.ssd.image_resizer.fixed_shape_resizer.width - - # If data augmentation is specified in the config file, the preprocessor - # will be called here to augment the data as specified. Most common - # augmentations include horizontal flip and cropping. - if data_augmentation_options: - images_pre = tf.split(tensor_dict['image'], config.video_length, axis=0) - bboxes_pre = tf.split( - tensor_dict['groundtruth_boxes'], config.video_length, axis=0) - labels_pre = tf.split( - tensor_dict['groundtruth_classes'], config.video_length, axis=0) - images_proc, bboxes_proc, labels_proc = [], [], [] - cache = preprocessor_cache.PreprocessorCache() - - for i, _ in enumerate(images_pre): - image_dict = { - fields.InputDataFields.image: - images_pre[i], - fields.InputDataFields.groundtruth_boxes: - tf.squeeze(bboxes_pre[i], axis=0), - fields.InputDataFields.groundtruth_classes: - tf.squeeze(labels_pre[i], axis=0), - } - image_dict = preprocessor.preprocess( - image_dict, - data_augmentation_options, - func_arg_map=preprocessor.get_default_func_arg_map(), - preprocess_vars_cache=cache) - # Pads detection count to _PADDING_SIZE. - image_dict[fields.InputDataFields.groundtruth_boxes] = tf.pad( - image_dict[fields.InputDataFields.groundtruth_boxes], - [[0, _PADDING_SIZE], [0, 0]]) - image_dict[fields.InputDataFields.groundtruth_boxes] = tf.slice( - image_dict[fields.InputDataFields.groundtruth_boxes], [0, 0], - [_PADDING_SIZE, -1]) - image_dict[fields.InputDataFields.groundtruth_classes] = tf.pad( - image_dict[fields.InputDataFields.groundtruth_classes], - [[0, _PADDING_SIZE]]) - image_dict[fields.InputDataFields.groundtruth_classes] = tf.slice( - image_dict[fields.InputDataFields.groundtruth_classes], [0], - [_PADDING_SIZE]) - images_proc.append(image_dict[fields.InputDataFields.image]) - bboxes_proc.append(image_dict[fields.InputDataFields.groundtruth_boxes]) - labels_proc.append(image_dict[fields.InputDataFields.groundtruth_classes]) - tensor_dict['image'] = tf.concat(images_proc, axis=0) - tensor_dict['groundtruth_boxes'] = tf.stack(bboxes_proc, axis=0) - tensor_dict['groundtruth_classes'] = tf.stack(labels_proc, axis=0) - else: - # Pads detection count to _PADDING_SIZE per frame. - tensor_dict['groundtruth_boxes'] = tf.pad( - tensor_dict['groundtruth_boxes'], [[0, 0], [0, _PADDING_SIZE], [0, 0]]) - tensor_dict['groundtruth_boxes'] = tf.slice( - tensor_dict['groundtruth_boxes'], [0, 0, 0], [-1, _PADDING_SIZE, -1]) - tensor_dict['groundtruth_classes'] = tf.pad( - tensor_dict['groundtruth_classes'], [[0, 0], [0, _PADDING_SIZE]]) - tensor_dict['groundtruth_classes'] = tf.slice( - tensor_dict['groundtruth_classes'], [0, 0], [-1, _PADDING_SIZE]) - - tensor_dict['image'], _ = preprocessor.resize_image( - tensor_dict['image'], new_height=height, new_width=width) - - num_steps = config.video_length / unroll_length - - init_states = { - 'lstm_state_c': - tf.zeros([height / 32, width / 32, lstm_config.lstm_state_depth]), - 'lstm_state_h': - tf.zeros([height / 32, width / 32, lstm_config.lstm_state_depth]), - 'lstm_state_step': - tf.constant(num_steps, shape=[]), - } - - batch = sqss.batch_sequences_with_states( - input_key=key, - input_sequences=tensor_dict, - input_context={}, - input_length=None, - initial_states=init_states, - num_unroll=unroll_length, - batch_size=batch_size, - num_threads=batch_size, - make_keys_unique=True, - capacity=batch_size * batch_size) - - return _build_training_batch_dict(batch, unroll_length, batch_size) diff --git a/research/lstm_object_detection/inputs/seq_dataset_builder_test.py b/research/lstm_object_detection/inputs/seq_dataset_builder_test.py deleted file mode 100644 index 4b894d24f71..00000000000 --- a/research/lstm_object_detection/inputs/seq_dataset_builder_test.py +++ /dev/null @@ -1,282 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for dataset_builder.""" - -import os -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from tensorflow.core.example import example_pb2 -from tensorflow.core.example import feature_pb2 -from lstm_object_detection.inputs import seq_dataset_builder -from lstm_object_detection.protos import pipeline_pb2 as internal_pipeline_pb2 -from object_detection.builders import preprocessor_builder -from object_detection.core import standard_fields as fields -from object_detection.protos import input_reader_pb2 -from object_detection.protos import pipeline_pb2 -from object_detection.protos import preprocessor_pb2 - - -class DatasetBuilderTest(tf.test.TestCase): - - def _create_tf_record(self): - path = os.path.join(self.get_temp_dir(), 'tfrecord') - writer = tf.python_io.TFRecordWriter(path) - - image_tensor = np.random.randint(255, size=(16, 16, 3)).astype(np.uint8) - with self.test_session(): - encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).eval() - - sequence_example = example_pb2.SequenceExample( - context=feature_pb2.Features( - feature={ - 'image/format': - feature_pb2.Feature( - bytes_list=feature_pb2.BytesList( - value=['jpeg'.encode('utf-8')])), - 'image/height': - feature_pb2.Feature( - int64_list=feature_pb2.Int64List(value=[16])), - 'image/width': - feature_pb2.Feature( - int64_list=feature_pb2.Int64List(value=[16])), - }), - feature_lists=feature_pb2.FeatureLists( - feature_list={ - 'image/encoded': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - bytes_list=feature_pb2.BytesList( - value=[encoded_jpeg])), - ]), - 'image/object/bbox/xmin': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[0.0])), - ]), - 'image/object/bbox/xmax': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[1.0])) - ]), - 'image/object/bbox/ymin': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[0.0])), - ]), - 'image/object/bbox/ymax': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[1.0])) - ]), - 'image/object/class/label': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - int64_list=feature_pb2.Int64List(value=[2])) - ]), - })) - - writer.write(sequence_example.SerializeToString()) - writer.close() - - return path - - def _get_model_configs_from_proto(self): - """Creates a model text proto for testing. - - Returns: - A dictionary of model configs. - """ - - model_text_proto = """ - [lstm_object_detection.protos.lstm_model] { - train_unroll_length: 4 - eval_unroll_length: 4 - } - model { - ssd { - feature_extractor { - type: 'lstm_mobilenet_v1_fpn' - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - negative_class_weight: 2.0 - box_coder { - faster_rcnn_box_coder { - } - } - matcher { - argmax_matcher { - } - } - similarity_calculator { - iou_similarity { - } - } - anchor_generator { - ssd_anchor_generator { - aspect_ratios: 1.0 - } - } - image_resizer { - fixed_shape_resizer { - height: 32 - width: 32 - } - } - box_predictor { - convolutional_box_predictor { - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - } - normalize_loc_loss_by_codesize: true - loss { - classification_loss { - weighted_softmax { - } - } - localization_loss { - weighted_smooth_l1 { - } - } - } - } - }""" - - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - text_format.Merge(model_text_proto, pipeline_config) - configs = {} - configs['model'] = pipeline_config.model - configs['lstm_model'] = pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model] - - return configs - - def _get_data_augmentation_preprocessor_proto(self): - preprocessor_text_proto = """ - random_horizontal_flip { - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - return preprocessor_proto - - def _create_training_dict(self, tensor_dict): - image_dict = {} - all_dict = {} - all_dict['batch'] = tensor_dict.pop('batch') - for i, _ in enumerate(tensor_dict[fields.InputDataFields.image]): - for key, val in tensor_dict.items(): - image_dict[key] = val[i] - - image_dict[fields.InputDataFields.image] = tf.to_float( - tf.expand_dims(image_dict[fields.InputDataFields.image], 0)) - suffix = str(i) - for key, val in image_dict.items(): - all_dict[key + suffix] = val - return all_dict - - def _get_input_proto(self, input_reader): - return """ - external_input_reader { - [lstm_object_detection.protos.GoogleInputReader.google_input_reader] { - %s: { - input_path: '{0}' - data_type: TF_SEQUENCE_EXAMPLE - video_length: 4 - } - } - } - """ % input_reader - - def test_video_input_reader(self): - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge( - self._get_input_proto('tf_record_video_input_reader'), - input_reader_proto) - - configs = self._get_model_configs_from_proto() - tensor_dict = seq_dataset_builder.build( - input_reader_proto, - configs['model'], - configs['lstm_model'], - unroll_length=1) - - all_dict = self._create_training_dict(tensor_dict) - - self.assertEqual((1, 32, 32, 3), all_dict['image0'].shape) - self.assertEqual(4, all_dict['groundtruth_boxes0'].shape[1]) - - def test_build_with_data_augmentation(self): - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge( - self._get_input_proto('tf_record_video_input_reader'), - input_reader_proto) - - configs = self._get_model_configs_from_proto() - data_augmentation_options = [ - preprocessor_builder.build( - self._get_data_augmentation_preprocessor_proto()) - ] - tensor_dict = seq_dataset_builder.build( - input_reader_proto, - configs['model'], - configs['lstm_model'], - unroll_length=1, - data_augmentation_options=data_augmentation_options) - - all_dict = self._create_training_dict(tensor_dict) - self.assertEqual((1, 32, 32, 3), all_dict['image0'].shape) - self.assertEqual(4, all_dict['groundtruth_boxes0'].shape[1]) - - def test_raises_error_without_input_paths(self): - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - load_instance_masks: true - """ - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - configs = self._get_model_configs_from_proto() - with self.assertRaises(ValueError): - _ = seq_dataset_builder.build( - input_reader_proto, - configs['model'], - configs['lstm_model'], - unroll_length=1) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/inputs/tf_sequence_example_decoder.py b/research/lstm_object_detection/inputs/tf_sequence_example_decoder.py deleted file mode 100644 index def945b3f07..00000000000 --- a/research/lstm_object_detection/inputs/tf_sequence_example_decoder.py +++ /dev/null @@ -1,263 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tensorflow Sequence Example proto decoder. - -A decoder to decode string tensors containing serialized -tensorflow.SequenceExample protos. -""" -import tensorflow.compat.v1 as tf -import tf_slim as slim -from object_detection.core import data_decoder -from object_detection.core import standard_fields as fields - -tfexample_decoder = slim.tfexample_decoder - - -class BoundingBoxSequence(tfexample_decoder.ItemHandler): - """An ItemHandler that concatenates SparseTensors to Bounding Boxes. - """ - - def __init__(self, keys=None, prefix=None, return_dense=True, - default_value=-1.0): - """Initialize the bounding box handler. - - Args: - keys: A list of four key names representing the ymin, xmin, ymax, xmax - in the Example or SequenceExample. - prefix: An optional prefix for each of the bounding box keys in the - Example or SequenceExample. If provided, `prefix` is prepended to each - key in `keys`. - return_dense: if True, returns a dense tensor; if False, returns as - sparse tensor. - default_value: The value used when the `tensor_key` is not found in a - particular `TFExample`. - - Raises: - ValueError: if keys is not `None` and also not a list of exactly 4 keys - """ - if keys is None: - keys = ['ymin', 'xmin', 'ymax', 'xmax'] - elif len(keys) != 4: - raise ValueError('BoundingBoxSequence expects 4 keys but got {}'.format( - len(keys))) - self._prefix = prefix - self._keys = keys - self._full_keys = [prefix + k for k in keys] - self._return_dense = return_dense - self._default_value = default_value - super(BoundingBoxSequence, self).__init__(self._full_keys) - - def tensors_to_item(self, keys_to_tensors): - """Maps the given dictionary of tensors to a concatenated list of bboxes. - - Args: - keys_to_tensors: a mapping of TF-Example keys to parsed tensors. - - Returns: - [time, num_boxes, 4] tensor of bounding box coordinates, in order - [y_min, x_min, y_max, x_max]. Whether the tensor is a SparseTensor - or a dense Tensor is determined by the return_dense parameter. Empty - positions in the sparse tensor are filled with -1.0 values. - """ - sides = [] - for key in self._full_keys: - value = keys_to_tensors[key] - expanded_dims = tf.concat( - [tf.to_int64(tf.shape(value)), - tf.constant([1], dtype=tf.int64)], 0) - side = tf.sparse_reshape(value, expanded_dims) - sides.append(side) - bounding_boxes = tf.sparse_concat(2, sides) - if self._return_dense: - bounding_boxes = tf.sparse_tensor_to_dense( - bounding_boxes, default_value=self._default_value) - return bounding_boxes - - -class TFSequenceExampleDecoder(data_decoder.DataDecoder): - """Tensorflow Sequence Example proto decoder.""" - - def __init__(self): - """Constructor sets keys_to_features and items_to_handlers.""" - self.keys_to_context_features = { - 'image/format': - tf.FixedLenFeature((), tf.string, default_value='jpeg'), - 'image/filename': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/key/sha256': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/source_id': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/height': - tf.FixedLenFeature((), tf.int64, 1), - 'image/width': - tf.FixedLenFeature((), tf.int64, 1), - } - self.keys_to_features = { - 'image/encoded': tf.FixedLenSequenceFeature((), tf.string), - 'bbox/xmin': tf.VarLenFeature(dtype=tf.float32), - 'bbox/xmax': tf.VarLenFeature(dtype=tf.float32), - 'bbox/ymin': tf.VarLenFeature(dtype=tf.float32), - 'bbox/ymax': tf.VarLenFeature(dtype=tf.float32), - 'bbox/label/index': tf.VarLenFeature(dtype=tf.int64), - 'bbox/label/string': tf.VarLenFeature(tf.string), - 'area': tf.VarLenFeature(tf.float32), - 'is_crowd': tf.VarLenFeature(tf.int64), - 'difficult': tf.VarLenFeature(tf.int64), - 'group_of': tf.VarLenFeature(tf.int64), - } - self.items_to_handlers = { - fields.InputDataFields.image: - tfexample_decoder.Image( - image_key='image/encoded', - format_key='image/format', - channels=3, - repeated=True), - fields.InputDataFields.source_id: ( - tfexample_decoder.Tensor('image/source_id')), - fields.InputDataFields.key: ( - tfexample_decoder.Tensor('image/key/sha256')), - fields.InputDataFields.filename: ( - tfexample_decoder.Tensor('image/filename')), - # Object boxes and classes. - fields.InputDataFields.groundtruth_boxes: - BoundingBoxSequence(prefix='bbox/'), - fields.InputDataFields.groundtruth_classes: ( - tfexample_decoder.Tensor('bbox/label/index')), - fields.InputDataFields.groundtruth_area: - tfexample_decoder.Tensor('area'), - fields.InputDataFields.groundtruth_is_crowd: ( - tfexample_decoder.Tensor('is_crowd')), - fields.InputDataFields.groundtruth_difficult: ( - tfexample_decoder.Tensor('difficult')), - fields.InputDataFields.groundtruth_group_of: ( - tfexample_decoder.Tensor('group_of')) - } - - def decode(self, tf_seq_example_string_tensor, items=None): - """Decodes serialized tf.SequenceExample and returns a tensor dictionary. - - Args: - tf_seq_example_string_tensor: A string tensor holding a serialized - tensorflow example proto. - items: The list of items to decode. These must be a subset of the item - keys in self._items_to_handlers. If `items` is left as None, then all - of the items in self._items_to_handlers are decoded. - - Returns: - A dictionary of the following tensors. - fields.InputDataFields.image - 3D uint8 tensor of shape [None, None, seq] - containing image(s). - fields.InputDataFields.source_id - string tensor containing original - image id. - fields.InputDataFields.key - string tensor with unique sha256 hash key. - fields.InputDataFields.filename - string tensor with original dataset - filename. - fields.InputDataFields.groundtruth_boxes - 2D float32 tensor of shape - [None, 4] containing box corners. - fields.InputDataFields.groundtruth_classes - 1D int64 tensor of shape - [None] containing classes for the boxes. - fields.InputDataFields.groundtruth_area - 1D float32 tensor of shape - [None] containing object mask area in pixel squared. - fields.InputDataFields.groundtruth_is_crowd - 1D bool tensor of shape - [None] indicating if the boxes enclose a crowd. - fields.InputDataFields.groundtruth_difficult - 1D bool tensor of shape - [None] indicating if the boxes represent `difficult` instances. - """ - serialized_example = tf.reshape(tf_seq_example_string_tensor, shape=[]) - decoder = TFSequenceExampleDecoderHelper(self.keys_to_context_features, - self.keys_to_features, - self.items_to_handlers) - if not items: - items = decoder.list_items() - tensors = decoder.decode(serialized_example, items=items) - tensor_dict = dict(zip(items, tensors)) - - return tensor_dict - - -class TFSequenceExampleDecoderHelper(data_decoder.DataDecoder): - """A decoder helper class for TensorFlow SequenceExamples. - - To perform this decoding operation, a SequenceExampleDecoder is given a list - of ItemHandlers. Each ItemHandler indicates the set of features. - """ - - def __init__(self, keys_to_context_features, keys_to_sequence_features, - items_to_handlers): - """Constructs the decoder. - - Args: - keys_to_context_features: A dictionary from TF-SequenceExample context - keys to either tf.VarLenFeature or tf.FixedLenFeature instances. - See tensorflow's parsing_ops.py. - keys_to_sequence_features: A dictionary from TF-SequenceExample sequence - keys to either tf.VarLenFeature or tf.FixedLenSequenceFeature instances. - items_to_handlers: A dictionary from items (strings) to ItemHandler - instances. Note that the ItemHandler's are provided the keys that they - use to return the final item Tensors. - Raises: - ValueError: If the same key is present for context features and sequence - features. - """ - unique_keys = set() - unique_keys.update(keys_to_context_features) - unique_keys.update(keys_to_sequence_features) - if len(unique_keys) != ( - len(keys_to_context_features) + len(keys_to_sequence_features)): - # This situation is ambiguous in the decoder's keys_to_tensors variable. - raise ValueError('Context and sequence keys are not unique. \n' - ' Context keys: %s \n Sequence keys: %s' % - (list(keys_to_context_features.keys()), - list(keys_to_sequence_features.keys()))) - self._keys_to_context_features = keys_to_context_features - self._keys_to_sequence_features = keys_to_sequence_features - self._items_to_handlers = items_to_handlers - - def list_items(self): - """Returns keys of items.""" - return self._items_to_handlers.keys() - - def decode(self, serialized_example, items=None): - """Decodes the given serialized TF-SequenceExample. - - Args: - serialized_example: A serialized TF-SequenceExample tensor. - items: The list of items to decode. These must be a subset of the item - keys in self._items_to_handlers. If `items` is left as None, then all - of the items in self._items_to_handlers are decoded. - Returns: - The decoded items, a list of tensor. - """ - context, feature_list = tf.parse_single_sequence_example( - serialized_example, self._keys_to_context_features, - self._keys_to_sequence_features) - # Reshape non-sparse elements just once: - for k in self._keys_to_context_features: - v = self._keys_to_context_features[k] - if isinstance(v, tf.FixedLenFeature): - context[k] = tf.reshape(context[k], v.shape) - if not items: - items = self._items_to_handlers.keys() - outputs = [] - for item in items: - handler = self._items_to_handlers[item] - keys_to_tensors = { - key: context[key] if key in context else feature_list[key] - for key in handler.keys - } - outputs.append(handler.tensors_to_item(keys_to_tensors)) - return outputs diff --git a/research/lstm_object_detection/inputs/tf_sequence_example_decoder_test.py b/research/lstm_object_detection/inputs/tf_sequence_example_decoder_test.py deleted file mode 100644 index dbbb8d3c744..00000000000 --- a/research/lstm_object_detection/inputs/tf_sequence_example_decoder_test.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for lstm_object_detection.tf_sequence_example_decoder.""" - -import numpy as np -import tensorflow.compat.v1 as tf -from tensorflow.core.example import example_pb2 -from tensorflow.core.example import feature_pb2 -from tensorflow.python.framework import dtypes -from tensorflow.python.ops import parsing_ops -from lstm_object_detection.inputs import tf_sequence_example_decoder -from object_detection.core import standard_fields as fields - - -class TFSequenceExampleDecoderTest(tf.test.TestCase): - """Tests for sequence example decoder.""" - - def _EncodeImage(self, image_tensor, encoding_type='jpeg'): - with self.test_session(): - if encoding_type == 'jpeg': - image_encoded = tf.image.encode_jpeg(tf.constant(image_tensor)).eval() - else: - raise ValueError('Invalid encoding type.') - return image_encoded - - def _DecodeImage(self, image_encoded, encoding_type='jpeg'): - with self.test_session(): - if encoding_type == 'jpeg': - image_decoded = tf.image.decode_jpeg(tf.constant(image_encoded)).eval() - else: - raise ValueError('Invalid encoding type.') - return image_decoded - - def testDecodeJpegImageAndBoundingBox(self): - """Test if the decoder can correctly decode the image and bounding box. - - A set of random images (represented as an image tensor) is first decoded as - the groundtrue image. Meanwhile, the image tensor will be encoded and pass - through the sequence example, and then decoded as images. The groundtruth - image and the decoded image are expected to be equal. Similar tests are - also applied to labels such as bounding box. - """ - image_tensor = np.random.randint(256, size=(256, 256, 3)).astype(np.uint8) - encoded_jpeg = self._EncodeImage(image_tensor) - decoded_jpeg = self._DecodeImage(encoded_jpeg) - - sequence_example = example_pb2.SequenceExample( - feature_lists=feature_pb2.FeatureLists( - feature_list={ - 'image/encoded': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - bytes_list=feature_pb2.BytesList( - value=[encoded_jpeg])), - ]), - 'bbox/xmin': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[0.0])), - ]), - 'bbox/xmax': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[1.0])) - ]), - 'bbox/ymin': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[0.0])), - ]), - 'bbox/ymax': - feature_pb2.FeatureList(feature=[ - feature_pb2.Feature( - float_list=feature_pb2.FloatList(value=[1.0])) - ]), - })).SerializeToString() - - example_decoder = tf_sequence_example_decoder.TFSequenceExampleDecoder() - tensor_dict = example_decoder.decode(tf.convert_to_tensor(sequence_example)) - - # Test tensor dict image dimension. - self.assertAllEqual( - (tensor_dict[fields.InputDataFields.image].get_shape().as_list()), - [None, None, None, 3]) - with self.test_session() as sess: - tensor_dict[fields.InputDataFields.image] = tf.squeeze( - tensor_dict[fields.InputDataFields.image]) - tensor_dict[fields.InputDataFields.groundtruth_boxes] = tf.squeeze( - tensor_dict[fields.InputDataFields.groundtruth_boxes]) - tensor_dict = sess.run(tensor_dict) - - # Test decoded image. - self.assertAllEqual(decoded_jpeg, tensor_dict[fields.InputDataFields.image]) - # Test decoded bounding box. - self.assertAllEqual([0.0, 0.0, 1.0, 1.0], - tensor_dict[fields.InputDataFields.groundtruth_boxes]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/lstm/__init__.py b/research/lstm_object_detection/lstm/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/lstm/lstm_cells.py b/research/lstm_object_detection/lstm/lstm_cells.py deleted file mode 100644 index a553073d978..00000000000 --- a/research/lstm_object_detection/lstm/lstm_cells.py +++ /dev/null @@ -1,734 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""BottleneckConvLSTMCell implementation.""" -import functools - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from tensorflow.contrib import rnn as contrib_rnn -from tensorflow.contrib.framework.python.ops import variables as contrib_variables -import lstm_object_detection.lstm.utils as lstm_utils - - -class BottleneckConvLSTMCell(contrib_rnn.RNNCell): - """Basic LSTM recurrent network cell using separable convolutions. - - The implementation is based on: - Mobile Video Object Detection with Temporally-Aware Feature Maps - https://arxiv.org/abs/1711.06368. - - We add forget_bias (default: 1) to the biases of the forget gate in order to - reduce the scale of forgetting in the beginning of the training. - - This LSTM first projects inputs to the size of the output before doing gate - computations. This saves params unless the input is less than a third of the - state size channel-wise. - """ - - def __init__(self, - filter_size, - output_size, - num_units, - forget_bias=1.0, - activation=tf.tanh, - flatten_state=False, - clip_state=False, - output_bottleneck=False, - pre_bottleneck=False, - visualize_gates=False): - """Initializes the basic LSTM cell. - - Args: - filter_size: collection, conv filter size. - output_size: collection, the width/height dimensions of the cell/output. - num_units: int, The number of channels in the LSTM cell. - forget_bias: float, The bias added to forget gates (see above). - activation: Activation function of the inner states. - flatten_state: if True, state tensor will be flattened and stored as a 2-d - tensor. Use for exporting the model to tfmini. - clip_state: if True, clip state between [-6, 6]. - output_bottleneck: if True, the cell bottleneck will be concatenated to - the cell output. - pre_bottleneck: if True, cell assumes that bottlenecking was performing - before the function was called. - visualize_gates: if True, add histogram summaries of all gates and outputs - to tensorboard. - """ - self._filter_size = list(filter_size) - self._output_size = list(output_size) - self._num_units = num_units - self._forget_bias = forget_bias - self._activation = activation - self._viz_gates = visualize_gates - self._flatten_state = flatten_state - self._clip_state = clip_state - self._output_bottleneck = output_bottleneck - self._pre_bottleneck = pre_bottleneck - self._param_count = self._num_units - for dim in self._output_size: - self._param_count *= dim - - @property - def state_size(self): - return contrib_rnn.LSTMStateTuple(self._output_size + [self._num_units], - self._output_size + [self._num_units]) - - @property - def state_size_flat(self): - return contrib_rnn.LSTMStateTuple([self._param_count], [self._param_count]) - - @property - def output_size(self): - return self._output_size + [self._num_units] - - def __call__(self, inputs, state, scope=None): - """Long short-term memory cell (LSTM) with bottlenecking. - - Args: - inputs: Input tensor at the current timestep. - state: Tuple of tensors, the state and output at the previous timestep. - scope: Optional scope. - - Returns: - A tuple where the first element is the LSTM output and the second is - a LSTMStateTuple of the state at the current timestep. - """ - scope = scope or 'conv_lstm_cell' - with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): - c, h = state - - # unflatten state if necessary - if self._flatten_state: - c = tf.reshape(c, [-1] + self.output_size) - h = tf.reshape(h, [-1] + self.output_size) - - # summary of input passed into cell - if self._viz_gates: - slim.summaries.add_histogram_summary(inputs, 'cell_input') - if self._pre_bottleneck: - bottleneck = inputs - else: - bottleneck = slim.separable_conv2d( - tf.concat([inputs, h], 3), - self._num_units, - self._filter_size, - depth_multiplier=1, - activation_fn=self._activation, - normalizer_fn=None, - scope='bottleneck') - - if self._viz_gates: - slim.summaries.add_histogram_summary(bottleneck, 'bottleneck') - - concat = slim.separable_conv2d( - bottleneck, - 4 * self._num_units, - self._filter_size, - depth_multiplier=1, - activation_fn=None, - normalizer_fn=None, - scope='gates') - - i, j, f, o = tf.split(concat, 4, 3) - - new_c = ( - c * tf.sigmoid(f + self._forget_bias) + - tf.sigmoid(i) * self._activation(j)) - if self._clip_state: - new_c = tf.clip_by_value(new_c, -6, 6) - new_h = self._activation(new_c) * tf.sigmoid(o) - # summary of cell output and new state - if self._viz_gates: - slim.summaries.add_histogram_summary(new_h, 'cell_output') - slim.summaries.add_histogram_summary(new_c, 'cell_state') - - output = new_h - if self._output_bottleneck: - output = tf.concat([new_h, bottleneck], axis=3) - - # reflatten state to store it - if self._flatten_state: - new_c = tf.reshape(new_c, [-1, self._param_count]) - new_h = tf.reshape(new_h, [-1, self._param_count]) - - return output, contrib_rnn.LSTMStateTuple(new_c, new_h) - - def init_state(self, state_name, batch_size, dtype, learned_state=False): - """Creates an initial state compatible with this cell. - - Args: - state_name: name of the state tensor - batch_size: model batch size - dtype: dtype for the tensor values i.e. tf.float32 - learned_state: whether the initial state should be learnable. If false, - the initial state is set to all 0's - - Returns: - The created initial state. - """ - state_size = ( - self.state_size_flat if self._flatten_state else self.state_size) - # list of 2 zero tensors or variables tensors, depending on if - # learned_state is true - # pylint: disable=g-long-ternary,g-complex-comprehension - ret_flat = [(contrib_variables.model_variable( - state_name + str(i), - shape=s, - dtype=dtype, - initializer=tf.truncated_normal_initializer(stddev=0.03)) - if learned_state else tf.zeros( - [batch_size] + s, dtype=dtype, name=state_name)) - for i, s in enumerate(state_size)] - - # duplicates initial state across the batch axis if it's learned - if learned_state: - ret_flat = [ - tf.stack([tensor - for i in range(int(batch_size))]) - for tensor in ret_flat - ] - for s, r in zip(state_size, ret_flat): - r.set_shape([None] + s) - return tf.nest.pack_sequence_as(structure=[1, 1], flat_sequence=ret_flat) - - def pre_bottleneck(self, inputs, state, input_index): - """Apply pre-bottleneck projection to inputs. - - Pre-bottleneck operation maps features of different channels into the same - dimension. The purpose of this op is to share the features from both large - and small models in the same LSTM cell. - - Args: - inputs: 4D Tensor with shape [batch_size x width x height x input_size]. - state: 4D Tensor with shape [batch_size x width x height x state_size]. - input_index: integer index indicating which base features the inputs - correspoding to. - - Returns: - inputs: pre-bottlenecked inputs. - Raises: - ValueError: If pre_bottleneck is not set or inputs is not rank 4. - """ - # Sometimes state is a tuple, in which case it cannot be modified, e.g. - # during training, tf.contrib.training.SequenceQueueingStateSaver - # returns the state as a tuple. This should not be an issue since we - # only need to modify state[1] during export, when state should be a - # list. - if len(inputs.shape) != 4: - raise ValueError('Expect rank 4 feature tensor.') - if not self._flatten_state and len(state.shape) != 4: - raise ValueError('Expect rank 4 state tensor.') - if self._flatten_state and len(state.shape) != 2: - raise ValueError('Expect rank 2 state tensor when flatten_state is set.') - - with tf.name_scope(None): - state = tf.identity(state, name='raw_inputs/init_lstm_h') - if self._flatten_state: - batch_size = inputs.shape[0] - height = inputs.shape[1] - width = inputs.shape[2] - state = tf.reshape(state, [batch_size, height, width, -1]) - with tf.variable_scope('conv_lstm_cell', reuse=tf.AUTO_REUSE): - scope_name = 'bottleneck_%d' % input_index - inputs = slim.separable_conv2d( - tf.concat([inputs, state], 3), - self.output_size[-1], - self._filter_size, - depth_multiplier=1, - activation_fn=tf.nn.relu6, - normalizer_fn=None, - scope=scope_name) - # For exporting inference graph, we only mark the first timestep. - with tf.name_scope(None): - inputs = tf.identity( - inputs, name='raw_outputs/base_endpoint_%d' % (input_index + 1)) - return inputs - - -class GroupedConvLSTMCell(contrib_rnn.RNNCell): - """Basic LSTM recurrent network cell using separable convolutions. - - The implementation is based on: https://arxiv.org/abs/1903.10172. - - We add forget_bias (default: 1) to the biases of the forget gate in order to - reduce the scale of forgetting in the beginning of the training. - - This LSTM first projects inputs to the size of the output before doing gate - computations. This saves params unless the input is less than a third of the - state size channel-wise. Computation of bottlenecks and gates is divided - into independent groups for further savings. - """ - - def __init__(self, - filter_size, - output_size, - num_units, - is_training, - forget_bias=1.0, - activation=tf.tanh, - use_batch_norm=False, - flatten_state=False, - groups=4, - clip_state=False, - scale_state=False, - output_bottleneck=False, - pre_bottleneck=False, - is_quantized=False, - visualize_gates=False, - conv_op_overrides=None): - """Initialize the basic LSTM cell. - - Args: - filter_size: collection, conv filter size - output_size: collection, the width/height dimensions of the cell/output - num_units: int, The number of channels in the LSTM cell. - is_training: Whether the LSTM is in training mode. - forget_bias: float, The bias added to forget gates (see above). - activation: Activation function of the inner states. - use_batch_norm: if True, use batch norm after convolution - flatten_state: if True, state tensor will be flattened and stored as a 2-d - tensor. Use for exporting the model to tfmini - groups: Number of groups to split the state into. Must evenly divide - num_units. - clip_state: if True, clips state between [-6, 6]. - scale_state: if True, scales state so that all values are under 6 at all - times. - output_bottleneck: if True, the cell bottleneck will be concatenated to - the cell output. - pre_bottleneck: if True, cell assumes that bottlenecking was performing - before the function was called. - is_quantized: if True, the model is in quantize mode, which requires - quantization friendly concat and separable_conv2d ops. - visualize_gates: if True, add histogram summaries of all gates and outputs - to tensorboard - conv_op_overrides: A list of convolutional operations that override the - 'bottleneck' and 'convolution' layers before lstm gates. If None, the - original implementation of seperable_conv will be used. The length of - the list should be two. - - Raises: - ValueError: when both clip_state and scale_state are enabled. - """ - if clip_state and scale_state: - raise ValueError('clip_state and scale_state cannot both be enabled.') - - self._filter_size = list(filter_size) - self._output_size = list(output_size) - self._num_units = num_units - self._is_training = is_training - self._forget_bias = forget_bias - self._activation = activation - self._use_batch_norm = use_batch_norm - self._viz_gates = visualize_gates - self._flatten_state = flatten_state - self._param_count = self._num_units - self._groups = groups - self._scale_state = scale_state - self._clip_state = clip_state - self._output_bottleneck = output_bottleneck - self._pre_bottleneck = pre_bottleneck - self._is_quantized = is_quantized - for dim in self._output_size: - self._param_count *= dim - self._conv_op_overrides = conv_op_overrides - if self._conv_op_overrides and len(self._conv_op_overrides) != 2: - raise ValueError('Bottleneck and Convolutional layer should be overriden' - 'together') - - @property - def state_size(self): - return contrib_rnn.LSTMStateTuple(self._output_size + [self._num_units], - self._output_size + [self._num_units]) - - @property - def state_size_flat(self): - return contrib_rnn.LSTMStateTuple([self._param_count], [self._param_count]) - - @property - def output_size(self): - return self._output_size + [self._num_units] - - @property - def filter_size(self): - return self._filter_size - - @property - def num_groups(self): - return self._groups - - def __call__(self, inputs, state, scope=None): - """Long short-term memory cell (LSTM) with bottlenecking. - - Includes logic for quantization-aware training. Note that all concats and - activations use fixed ranges unless stated otherwise. - - Args: - inputs: Input tensor at the current timestep. - state: Tuple of tensors, the state at the previous timestep. - scope: Optional scope. - - Returns: - A tuple where the first element is the LSTM output and the second is - a LSTMStateTuple of the state at the current timestep. - """ - scope = scope or 'conv_lstm_cell' - with tf.variable_scope(scope, reuse=tf.AUTO_REUSE): - c, h = state - - # Set nodes to be under raw_inputs/ name scope for tfmini export. - with tf.name_scope(None): - c = tf.identity(c, name='raw_inputs/init_lstm_c') - # When pre_bottleneck is enabled, input h handle is in rnn_decoder.py - if not self._pre_bottleneck: - h = tf.identity(h, name='raw_inputs/init_lstm_h') - - # unflatten state if necessary - if self._flatten_state: - c = tf.reshape(c, [-1] + self.output_size) - h = tf.reshape(h, [-1] + self.output_size) - - c_list = tf.split(c, self._groups, axis=3) - if self._pre_bottleneck: - inputs_list = tf.split(inputs, self._groups, axis=3) - else: - h_list = tf.split(h, self._groups, axis=3) - out_bottleneck = [] - out_c = [] - out_h = [] - # summary of input passed into cell - if self._viz_gates: - slim.summaries.add_histogram_summary(inputs, 'cell_input') - - for k in range(self._groups): - if self._pre_bottleneck: - bottleneck = inputs_list[k] - else: - if self._conv_op_overrides: - bottleneck_fn = self._conv_op_overrides[0] - else: - bottleneck_fn = functools.partial( - lstm_utils.quantizable_separable_conv2d, - kernel_size=self._filter_size, - activation_fn=self._activation) - if self._use_batch_norm: - b_x = bottleneck_fn( - inputs=inputs, - num_outputs=self._num_units // self._groups, - is_quantized=self._is_quantized, - depth_multiplier=1, - normalizer_fn=None, - scope='bottleneck_%d_x' % k) - b_h = bottleneck_fn( - inputs=h_list[k], - num_outputs=self._num_units // self._groups, - is_quantized=self._is_quantized, - depth_multiplier=1, - normalizer_fn=None, - scope='bottleneck_%d_h' % k) - b_x = slim.batch_norm( - b_x, - scale=True, - is_training=self._is_training, - scope='BatchNorm_%d_X' % k) - b_h = slim.batch_norm( - b_h, - scale=True, - is_training=self._is_training, - scope='BatchNorm_%d_H' % k) - bottleneck = b_x + b_h - else: - # All concats use fixed quantization ranges to prevent rescaling - # at inference. Both |inputs| and |h_list| are tensors resulting - # from Relu6 operations so we fix the ranges to [0, 6]. - bottleneck_concat = lstm_utils.quantizable_concat( - [inputs, h_list[k]], - axis=3, - is_training=False, - is_quantized=self._is_quantized, - scope='bottleneck_%d/quantized_concat' % k) - bottleneck = bottleneck_fn( - inputs=bottleneck_concat, - num_outputs=self._num_units // self._groups, - is_quantized=self._is_quantized, - depth_multiplier=1, - normalizer_fn=None, - scope='bottleneck_%d' % k) - - if self._conv_op_overrides: - conv_fn = self._conv_op_overrides[1] - else: - conv_fn = functools.partial( - lstm_utils.quantizable_separable_conv2d, - kernel_size=self._filter_size, - activation_fn=None) - concat = conv_fn( - inputs=bottleneck, - num_outputs=4 * self._num_units // self._groups, - is_quantized=self._is_quantized, - depth_multiplier=1, - normalizer_fn=None, - scope='concat_conv_%d' % k) - - # Since there is no activation in the previous separable conv, we - # quantize here. A starting range of [-6, 6] is used because the - # tensors are input to a Sigmoid function that saturates at these - # ranges. - concat = lstm_utils.quantize_op( - concat, - is_training=self._is_training, - default_min=-6, - default_max=6, - is_quantized=self._is_quantized, - scope='gates_%d/act_quant' % k) - - # i = input_gate, j = new_input, f = forget_gate, o = output_gate - i, j, f, o = tf.split(concat, 4, 3) - - f_add = f + self._forget_bias - f_add = lstm_utils.quantize_op( - f_add, - is_training=self._is_training, - default_min=-6, - default_max=6, - is_quantized=self._is_quantized, - scope='forget_gate_%d/add_quant' % k) - f_act = tf.sigmoid(f_add) - - a = c_list[k] * f_act - a = lstm_utils.quantize_op( - a, - is_training=self._is_training, - is_quantized=self._is_quantized, - scope='forget_gate_%d/mul_quant' % k) - - i_act = tf.sigmoid(i) - - j_act = self._activation(j) - # The quantization range is fixed for the relu6 to ensure that zero - # is exactly representable. - j_act = lstm_utils.fixed_quantize_op( - j_act, - fixed_min=0.0, - fixed_max=6.0, - is_quantized=self._is_quantized, - scope='new_input_%d/act_quant' % k) - - b = i_act * j_act - b = lstm_utils.quantize_op( - b, - is_training=self._is_training, - is_quantized=self._is_quantized, - scope='input_gate_%d/mul_quant' % k) - - new_c = a + b - # The quantization range is fixed to [0, 6] due to an optimization in - # TFLite. The order of operations is as fllows: - # Add -> FakeQuant -> Relu6 -> FakeQuant -> Concat. - # The fakequant ranges to the concat must be fixed to ensure all inputs - # to the concat have the same range, removing the need for rescaling. - # The quantization ranges input to the relu6 are propagated to its - # output. Any mismatch between these two ranges will cause an error. - new_c = lstm_utils.fixed_quantize_op( - new_c, - fixed_min=0.0, - fixed_max=6.0, - is_quantized=self._is_quantized, - scope='new_c_%d/add_quant' % k) - - if not self._is_quantized: - if self._scale_state: - normalizer = tf.maximum(1.0, - tf.reduce_max(new_c, axis=(1, 2, 3)) / 6) - new_c /= tf.reshape(normalizer, [tf.shape(new_c)[0], 1, 1, 1]) - elif self._clip_state: - new_c = tf.clip_by_value(new_c, -6, 6) - - new_c_act = self._activation(new_c) - # The quantization range is fixed for the relu6 to ensure that zero - # is exactly representable. - new_c_act = lstm_utils.fixed_quantize_op( - new_c_act, - fixed_min=0.0, - fixed_max=6.0, - is_quantized=self._is_quantized, - scope='new_c_%d/act_quant' % k) - - o_act = tf.sigmoid(o) - - new_h = new_c_act * o_act - # The quantization range is fixed since it is input to a concat. - # A range of [0, 6] is used since |new_h| is a product of ranges [0, 6] - # and [0, 1]. - new_h_act = lstm_utils.fixed_quantize_op( - new_h, - fixed_min=0.0, - fixed_max=6.0, - is_quantized=self._is_quantized, - scope='new_h_%d/act_quant' % k) - - out_bottleneck.append(bottleneck) - out_c.append(new_c_act) - out_h.append(new_h_act) - - # Since all inputs to the below concats are already quantized, we can use - # a regular concat operation. - new_c = tf.concat(out_c, axis=3) - new_h = tf.concat(out_h, axis=3) - - # |bottleneck| is input to a concat with |new_h|. We must use - # quantizable_concat() with a fixed range that matches |new_h|. - bottleneck = lstm_utils.quantizable_concat( - out_bottleneck, - axis=3, - is_training=False, - is_quantized=self._is_quantized, - scope='out_bottleneck/quantized_concat') - - # summary of cell output and new state - if self._viz_gates: - slim.summaries.add_histogram_summary(new_h, 'cell_output') - slim.summaries.add_histogram_summary(new_c, 'cell_state') - - output = new_h - if self._output_bottleneck: - output = lstm_utils.quantizable_concat( - [new_h, bottleneck], - axis=3, - is_training=False, - is_quantized=self._is_quantized, - scope='new_output/quantized_concat') - - # reflatten state to store it - if self._flatten_state: - new_c = tf.reshape(new_c, [-1, self._param_count], name='lstm_c') - new_h = tf.reshape(new_h, [-1, self._param_count], name='lstm_h') - - # Set nodes to be under raw_outputs/ name scope for tfmini export. - with tf.name_scope(None): - new_c = tf.identity(new_c, name='raw_outputs/lstm_c') - new_h = tf.identity(new_h, name='raw_outputs/lstm_h') - states_and_output = contrib_rnn.LSTMStateTuple(new_c, new_h) - - return output, states_and_output - - def init_state(self, state_name, batch_size, dtype, learned_state=False): - """Creates an initial state compatible with this cell. - - Args: - state_name: name of the state tensor - batch_size: model batch size - dtype: dtype for the tensor values i.e. tf.float32 - learned_state: whether the initial state should be learnable. If false, - the initial state is set to all 0's - - Returns: - ret: the created initial state - """ - state_size = ( - self.state_size_flat if self._flatten_state else self.state_size) - # list of 2 zero tensors or variables tensors, - # depending on if learned_state is true - # pylint: disable=g-long-ternary,g-complex-comprehension - ret_flat = [(contrib_variables.model_variable( - state_name + str(i), - shape=s, - dtype=dtype, - initializer=tf.truncated_normal_initializer(stddev=0.03)) - if learned_state else tf.zeros( - [batch_size] + s, dtype=dtype, name=state_name)) - for i, s in enumerate(state_size)] - - # duplicates initial state across the batch axis if it's learned - if learned_state: - ret_flat = [tf.stack([tensor for i in range(int(batch_size))]) - for tensor in ret_flat] - for s, r in zip(state_size, ret_flat): - r = tf.reshape(r, [-1] + s) - ret = tf.nest.pack_sequence_as(structure=[1, 1], flat_sequence=ret_flat) - return ret - - def pre_bottleneck(self, inputs, state, input_index): - """Apply pre-bottleneck projection to inputs. - - Pre-bottleneck operation maps features of different channels into the same - dimension. The purpose of this op is to share the features from both large - and small models in the same LSTM cell. - - Args: - inputs: 4D Tensor with shape [batch_size x width x height x input_size]. - state: 4D Tensor with shape [batch_size x width x height x state_size]. - input_index: integer index indicating which base features the inputs - correspoding to. - - Returns: - inputs: pre-bottlenecked inputs. - Raises: - ValueError: If pre_bottleneck is not set or inputs is not rank 4. - """ - # Sometimes state is a tuple, in which case it cannot be modified, e.g. - # during training, tf.contrib.training.SequenceQueueingStateSaver - # returns the state as a tuple. This should not be an issue since we - # only need to modify state[1] during export, when state should be a - # list. - if not self._pre_bottleneck: - raise ValueError('Only applied when pre_bottleneck is set to true.') - if len(inputs.shape) != 4: - raise ValueError('Expect a rank 4 feature tensor.') - if not self._flatten_state and len(state.shape) != 4: - raise ValueError('Expect rank 4 state tensor.') - if self._flatten_state and len(state.shape) != 2: - raise ValueError('Expect rank 2 state tensor when flatten_state is set.') - - with tf.name_scope(None): - state = tf.identity( - state, name='raw_inputs/init_lstm_h_%d' % (input_index + 1)) - if self._flatten_state: - batch_size = inputs.shape[0] - height = inputs.shape[1] - width = inputs.shape[2] - state = tf.reshape(state, [batch_size, height, width, -1]) - with tf.variable_scope('conv_lstm_cell', reuse=tf.AUTO_REUSE): - state_split = tf.split(state, self._groups, axis=3) - with tf.variable_scope('bottleneck_%d' % input_index): - bottleneck_out = [] - for k in range(self._groups): - with tf.variable_scope('group_%d' % k): - bottleneck_out.append( - lstm_utils.quantizable_separable_conv2d( - lstm_utils.quantizable_concat( - [inputs, state_split[k]], - axis=3, - is_training=self._is_training, - is_quantized=self._is_quantized, - scope='quantized_concat'), - self.output_size[-1] / self._groups, - self._filter_size, - is_quantized=self._is_quantized, - depth_multiplier=1, - activation_fn=tf.nn.relu6, - normalizer_fn=None, - scope='project')) - inputs = lstm_utils.quantizable_concat( - bottleneck_out, - axis=3, - is_training=self._is_training, - is_quantized=self._is_quantized, - scope='bottleneck_out/quantized_concat') - # For exporting inference graph, we only mark the first timestep. - with tf.name_scope(None): - inputs = tf.identity( - inputs, name='raw_outputs/base_endpoint_%d' % (input_index + 1)) - return inputs diff --git a/research/lstm_object_detection/lstm/lstm_cells_test.py b/research/lstm_object_detection/lstm/lstm_cells_test.py deleted file mode 100644 index b296310194d..00000000000 --- a/research/lstm_object_detection/lstm/lstm_cells_test.py +++ /dev/null @@ -1,412 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for lstm_object_detection.lstm.lstm_cells.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow.compat.v1 as tf - -from lstm_object_detection.lstm import lstm_cells - - -class BottleneckConvLstmCellsTest(tf.test.TestCase): - - def test_run_lstm_cell(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = False - - inputs = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units) - init_state = cell.init_state( - state_name, batch_size, dtype, learned_state) - output, state_tuple = cell(inputs, init_state) - self.assertAllEqual([4, 10, 10, 15], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state_tuple[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state_tuple[1].shape.as_list()) - - def test_run_lstm_cell_with_flattened_state(self): - filter_size = [3, 3] - output_dim = 10 - output_size = [output_dim] * 2 - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = False - - inputs = tf.zeros([batch_size, output_dim, output_dim, 3], dtype=tf.float32) - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - flatten_state=True) - init_state = cell.init_state( - state_name, batch_size, dtype, learned_state) - output, state_tuple = cell(inputs, init_state) - self.assertAllEqual([4, 10, 10, 15], output.shape.as_list()) - self.assertAllEqual([4, 1500], state_tuple[0].shape.as_list()) - self.assertAllEqual([4, 1500], state_tuple[1].shape.as_list()) - - def test_run_lstm_cell_with_output_bottleneck(self): - filter_size = [3, 3] - output_dim = 10 - output_size = [output_dim] * 2 - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = False - - inputs = tf.zeros([batch_size, output_dim, output_dim, 3], dtype=tf.float32) - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - output_bottleneck=True) - init_state = cell.init_state( - state_name, batch_size, dtype, learned_state) - output, state_tuple = cell(inputs, init_state) - self.assertAllEqual([4, 10, 10, 30], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state_tuple[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state_tuple[1].shape.as_list()) - - def test_get_init_state(self): - filter_size = [3, 3] - output_dim = 10 - output_size = [output_dim] * 2 - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = False - - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units) - init_c, init_h = cell.init_state( - state_name, batch_size, dtype, learned_state) - - self.assertEqual(tf.float32, init_c.dtype) - self.assertEqual(tf.float32, init_h.dtype) - with self.test_session() as sess: - init_c_res, init_h_res = sess.run([init_c, init_h]) - self.assertAllClose(np.zeros((4, 10, 10, 15)), init_c_res) - self.assertAllClose(np.zeros((4, 10, 10, 15)), init_h_res) - - def test_get_init_learned_state(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = True - - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units) - init_c, init_h = cell.init_state( - state_name, batch_size, dtype, learned_state) - - self.assertEqual(tf.float32, init_c.dtype) - self.assertEqual(tf.float32, init_h.dtype) - self.assertAllEqual([4, 10, 10, 15], init_c.shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], init_h.shape.as_list()) - - def test_unroll(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - unroll = 10 - learned_state = False - - inputs = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units) - state = cell.init_state( - state_name, batch_size, dtype, learned_state) - for step in range(unroll): - output, state = cell(inputs, state) - self.assertAllEqual([4, 10, 10, 15], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state[1].shape.as_list()) - - def test_prebottleneck(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - unroll = 10 - learned_state = False - - inputs_large = tf.zeros([4, 10, 10, 5], dtype=tf.float32) - inputs_small = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - pre_bottleneck=True) - state = cell.init_state( - state_name, batch_size, dtype, learned_state) - for step in range(unroll): - if step % 2 == 0: - inputs = cell.pre_bottleneck(inputs_large, state[1], 0) - else: - inputs = cell.pre_bottleneck(inputs_small, state[1], 1) - output, state = cell(inputs, state) - self.assertAllEqual([4, 10, 10, 15], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 15], state[1].shape.as_list()) - - def test_flatten_state(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 15 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - unroll = 10 - learned_state = False - - inputs_large = tf.zeros([4, 10, 10, 5], dtype=tf.float32) - inputs_small = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - pre_bottleneck=True, - flatten_state=True) - state = cell.init_state( - state_name, batch_size, dtype, learned_state) - for step in range(unroll): - if step % 2 == 0: - inputs = cell.pre_bottleneck(inputs_large, state[1], 0) - else: - inputs = cell.pre_bottleneck(inputs_small, state[1], 1) - output, state = cell(inputs, state) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output_result, state_result = sess.run([output, state]) - self.assertAllEqual((4, 10, 10, 15), output_result.shape) - self.assertAllEqual((4, 10*10*15), state_result[0].shape) - self.assertAllEqual((4, 10*10*15), state_result[1].shape) - - -class GroupedConvLstmCellsTest(tf.test.TestCase): - - def test_run_lstm_cell(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 16 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = False - - inputs = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.GroupedConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - is_training=True) - init_state = cell.init_state( - state_name, batch_size, dtype, learned_state) - output, state_tuple = cell(inputs, init_state) - self.assertAllEqual([4, 10, 10, 16], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state_tuple[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state_tuple[1].shape.as_list()) - - def test_run_lstm_cell_with_output_bottleneck(self): - filter_size = [3, 3] - output_dim = 10 - output_size = [output_dim] * 2 - num_units = 16 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = False - - inputs = tf.zeros([batch_size, output_dim, output_dim, 3], dtype=tf.float32) - cell = lstm_cells.GroupedConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - is_training=True, - output_bottleneck=True) - init_state = cell.init_state( - state_name, batch_size, dtype, learned_state) - output, state_tuple = cell(inputs, init_state) - self.assertAllEqual([4, 10, 10, 32], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state_tuple[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state_tuple[1].shape.as_list()) - - def test_get_init_state(self): - filter_size = [3, 3] - output_dim = 10 - output_size = [output_dim] * 2 - num_units = 16 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = False - - cell = lstm_cells.GroupedConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - is_training=True) - init_c, init_h = cell.init_state( - state_name, batch_size, dtype, learned_state) - - self.assertEqual(tf.float32, init_c.dtype) - self.assertEqual(tf.float32, init_h.dtype) - with self.test_session() as sess: - init_c_res, init_h_res = sess.run([init_c, init_h]) - self.assertAllClose(np.zeros((4, 10, 10, 16)), init_c_res) - self.assertAllClose(np.zeros((4, 10, 10, 16)), init_h_res) - - def test_get_init_learned_state(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 16 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - learned_state = True - - cell = lstm_cells.GroupedConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - is_training=True) - init_c, init_h = cell.init_state( - state_name, batch_size, dtype, learned_state) - - self.assertEqual(tf.float32, init_c.dtype) - self.assertEqual(tf.float32, init_h.dtype) - self.assertAllEqual([4, 10, 10, 16], init_c.shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], init_h.shape.as_list()) - - def test_unroll(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 16 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - unroll = 10 - learned_state = False - - inputs = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.GroupedConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - is_training=True) - state = cell.init_state( - state_name, batch_size, dtype, learned_state) - for step in range(unroll): - output, state = cell(inputs, state) - self.assertAllEqual([4, 10, 10, 16], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state[1].shape.as_list()) - - def test_prebottleneck(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 16 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - unroll = 10 - learned_state = False - - inputs_large = tf.zeros([4, 10, 10, 5], dtype=tf.float32) - inputs_small = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.GroupedConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - is_training=True, - pre_bottleneck=True) - state = cell.init_state( - state_name, batch_size, dtype, learned_state) - for step in range(unroll): - if step % 2 == 0: - inputs = cell.pre_bottleneck(inputs_large, state[1], 0) - else: - inputs = cell.pre_bottleneck(inputs_small, state[1], 1) - output, state = cell(inputs, state) - self.assertAllEqual([4, 10, 10, 16], output.shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state[0].shape.as_list()) - self.assertAllEqual([4, 10, 10, 16], state[1].shape.as_list()) - - def test_flatten_state(self): - filter_size = [3, 3] - output_size = [10, 10] - num_units = 16 - state_name = 'lstm_state' - batch_size = 4 - dtype = tf.float32 - unroll = 10 - learned_state = False - - inputs_large = tf.zeros([4, 10, 10, 5], dtype=tf.float32) - inputs_small = tf.zeros([4, 10, 10, 3], dtype=tf.float32) - cell = lstm_cells.GroupedConvLSTMCell( - filter_size=filter_size, - output_size=output_size, - num_units=num_units, - is_training=True, - pre_bottleneck=True, - flatten_state=True) - state = cell.init_state( - state_name, batch_size, dtype, learned_state) - for step in range(unroll): - if step % 2 == 0: - inputs = cell.pre_bottleneck(inputs_large, state[1], 0) - else: - inputs = cell.pre_bottleneck(inputs_small, state[1], 1) - output, state = cell(inputs, state) - with self.test_session() as sess: - sess.run(tf.global_variables_initializer()) - output_result, state_result = sess.run([output, state]) - self.assertAllEqual((4, 10, 10, 16), output_result.shape) - self.assertAllEqual((4, 10*10*16), state_result[0].shape) - self.assertAllEqual((4, 10*10*16), state_result[1].shape) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/lstm/rnn_decoder.py b/research/lstm_object_detection/lstm/rnn_decoder.py deleted file mode 100644 index 185ca130396..00000000000 --- a/research/lstm_object_detection/lstm/rnn_decoder.py +++ /dev/null @@ -1,269 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Custom RNN decoder.""" - -import tensorflow.compat.v1 as tf -import lstm_object_detection.lstm.utils as lstm_utils - - -class _NoVariableScope(object): - - def __enter__(self): - return - - def __exit__(self, exc_type, exc_value, traceback): - return False - - -def rnn_decoder(decoder_inputs, - initial_state, - cell, - loop_function=None, - scope=None): - """RNN decoder for the LSTM-SSD model. - - This decoder returns a list of all states, rather than only the final state. - Args: - decoder_inputs: A list of 4D Tensors with shape [batch_size x input_size]. - initial_state: 2D Tensor with shape [batch_size x cell.state_size]. - cell: rnn_cell.RNNCell defining the cell function and size. - loop_function: If not None, this function will be applied to the i-th output - in order to generate the i+1-st input, and decoder_inputs will be ignored, - except for the first element ("GO" symbol). This can be used for decoding, - but also for training to emulate http://arxiv.org/abs/1506.03099. - Signature -- loop_function(prev, i) = next - * prev is a 2D Tensor of shape [batch_size x output_size], - * i is an integer, the step number (when advanced control is needed), - * next is a 2D Tensor of shape [batch_size x input_size]. - scope: optional VariableScope for the created subgraph. - Returns: - A tuple of the form (outputs, state), where: - outputs: A list of the same length as decoder_inputs of 4D Tensors with - shape [batch_size x output_size] containing generated outputs. - states: A list of the same length as decoder_inputs of the state of each - cell at each time-step. It is a 2D Tensor of shape - [batch_size x cell.state_size]. - """ - with tf.variable_scope(scope) if scope else _NoVariableScope(): - state_tuple = initial_state - outputs = [] - states = [] - prev = None - for local_step, decoder_input in enumerate(decoder_inputs): - if loop_function is not None and prev is not None: - with tf.variable_scope('loop_function', reuse=True): - decoder_input = loop_function(prev, local_step) - output, state_tuple = cell(decoder_input, state_tuple) - outputs.append(output) - states.append(state_tuple) - if loop_function is not None: - prev = output - return outputs, states - -def multi_input_rnn_decoder(decoder_inputs, - initial_state, - cell, - sequence_step, - selection_strategy='RANDOM', - is_training=None, - is_quantized=False, - preprocess_fn_list=None, - pre_bottleneck=False, - flatten_state=False, - scope=None): - """RNN decoder for the Interleaved LSTM-SSD model. - - This decoder takes multiple sequences of inputs and selects the input to feed - to the rnn at each timestep using its selection_strategy, which can be random, - learned, or deterministic. - This decoder returns a list of all states, rather than only the final state. - Args: - decoder_inputs: A list of lists of 2D Tensors [batch_size x input_size]. - initial_state: 2D Tensor with shape [batch_size x cell.state_size]. - cell: rnn_cell.RNNCell defining the cell function and size. - sequence_step: Tensor [batch_size] of the step number of the first elements - in the sequence. - selection_strategy: Method for picking the decoder_input to use at each - timestep. Must be 'RANDOM', 'SKIPX' for integer X, where X is the number - of times to use the second input before using the first. - is_training: boolean, whether the network is training. When using learned - selection, attempts exploration if training. - is_quantized: flag to enable/disable quantization mode. - preprocess_fn_list: List of functions accepting two tensor arguments: one - timestep of decoder_inputs and the lstm state. If not None, - decoder_inputs[i] will be updated with preprocess_fn[i] at the start of - each timestep. - pre_bottleneck: if True, use separate bottleneck weights for each sequence. - Useful when input sequences have differing numbers of channels. Final - bottlenecks will have the same dimension. - flatten_state: Whether the LSTM state is flattened. - scope: optional VariableScope for the created subgraph. - Returns: - A tuple of the form (outputs, state), where: - outputs: A list of the same length as decoder_inputs of 2D Tensors with - shape [batch_size x output_size] containing generated outputs. - states: A list of the same length as decoder_inputs of the state of each - cell at each time-step. It is a 2D Tensor of shape - [batch_size x cell.state_size]. - Raises: - ValueError: If selection_strategy is not recognized or unexpected unroll - length. - """ - if flatten_state and len(decoder_inputs[0]) > 1: - raise ValueError('In export mode, unroll length should not be more than 1') - with tf.variable_scope(scope) if scope else _NoVariableScope(): - state_tuple = initial_state - outputs = [] - states = [] - batch_size = decoder_inputs[0][0].shape[0].value - num_sequences = len(decoder_inputs) - sequence_length = len(decoder_inputs[0]) - - for local_step in range(sequence_length): - for sequence_index in range(num_sequences): - if preprocess_fn_list is not None: - decoder_inputs[sequence_index][local_step] = ( - preprocess_fn_list[sequence_index]( - decoder_inputs[sequence_index][local_step], state_tuple[0])) - if pre_bottleneck: - decoder_inputs[sequence_index][local_step] = cell.pre_bottleneck( - inputs=decoder_inputs[sequence_index][local_step], - state=state_tuple[1], - input_index=sequence_index) - - action = generate_action(selection_strategy, local_step, sequence_step, - [batch_size, 1, 1, 1]) - inputs, _ = ( - select_inputs(decoder_inputs, action, local_step, is_training, - is_quantized)) - # Mark base network endpoints under raw_inputs/ - with tf.name_scope(None): - inputs = tf.identity(inputs, 'raw_inputs/base_endpoint') - output, state_tuple_out = cell(inputs, state_tuple) - state_tuple = select_state(state_tuple, state_tuple_out, action) - - outputs.append(output) - states.append(state_tuple) - return outputs, states - - -def generate_action(selection_strategy, local_step, sequence_step, - action_shape): - """Generate current (binary) action based on selection strategy. - - Args: - selection_strategy: Method for picking the decoder_input to use at each - timestep. Must be 'RANDOM', 'SKIPX' for integer X, where X is the number - of times to use the second input before using the first. - local_step: Tensor [batch_size] of the step number within the current - unrolled batch. - sequence_step: Tensor [batch_size] of the step number of the first elements - in the sequence. - action_shape: The shape of action tensor to be generated. - - Returns: - A tensor of shape action_shape, each element is an individual action. - - Raises: - ValueError: if selection_strategy is not supported or if 'SKIP' is not - followed by numerics. - """ - if selection_strategy.startswith('RANDOM'): - action = tf.random.uniform(action_shape, maxval=2, dtype=tf.int32) - action = tf.minimum(action, 1) - - # First step always runs large network. - if local_step == 0 and sequence_step is not None: - action *= tf.minimum( - tf.reshape(tf.cast(sequence_step, tf.int32), action_shape), 1) - elif selection_strategy.startswith('SKIP'): - inter_count = int(selection_strategy[4:]) - if local_step % (inter_count + 1) == 0: - action = tf.zeros(action_shape) - else: - action = tf.ones(action_shape) - else: - raise ValueError('Selection strategy %s not recognized' % - selection_strategy) - return tf.cast(action, tf.int32) - - -def select_inputs(decoder_inputs, action, local_step, is_training, is_quantized, - get_alt_inputs=False): - """Selects sequence from decoder_inputs based on 1D actions. - - Given multiple input batches, creates a single output batch by - selecting from the action[i]-ith input for the i-th batch element. - - Args: - decoder_inputs: A 2-D list of tensor inputs. - action: A tensor of shape [batch_size]. Each element corresponds to an index - of decoder_inputs to choose. - local_step: The current timestep. - is_training: boolean, whether the network is training. When using learned - selection, attempts exploration if training. - is_quantized: flag to enable/disable quantization mode. - get_alt_inputs: Whether the non-chosen inputs should also be returned. - - Returns: - The constructed output. Also outputs the elements that were not chosen - if get_alt_inputs is True, otherwise None. - - Raises: - ValueError: if the decoder inputs contains other than two sequences. - """ - num_seqs = len(decoder_inputs) - if not num_seqs == 2: - raise ValueError('Currently only supports two sets of inputs.') - stacked_inputs = tf.stack( - [decoder_inputs[seq_index][local_step] for seq_index in range(num_seqs)], - axis=-1) - action_index = tf.one_hot(action, num_seqs) - selected_inputs = ( - lstm_utils.quantize_op(stacked_inputs * action_index, is_training, - is_quantized, scope='quant_selected_inputs')) - inputs = tf.reduce_sum(selected_inputs, axis=-1) - inputs_alt = None - # Only works for 2 models. - if get_alt_inputs: - # Reverse of action_index. - action_index_alt = tf.one_hot(action, num_seqs, on_value=0.0, off_value=1.0) - selected_inputs = ( - lstm_utils.quantize_op(stacked_inputs * action_index_alt, is_training, - is_quantized, scope='quant_selected_inputs_alt')) - inputs_alt = tf.reduce_sum(selected_inputs, axis=-1) - return inputs, inputs_alt - -def select_state(previous_state, new_state, action): - """Select state given action. - - Currently only supports binary action. If action is 0, it means the state is - generated from the large model, and thus we will update the state. Otherwise, - if the action is 1, it means the state is generated from the small model, and - in interleaved model, we skip this state update. - - Args: - previous_state: A state tuple representing state from previous step. - new_state: A state tuple representing newly computed state. - action: A tensor the same shape as state. - - Returns: - A state tuple selected based on the given action. - """ - action = tf.cast(action, tf.float32) - state_c = previous_state[0] * action + new_state[0] * (1 - action) - state_h = previous_state[1] * action + new_state[1] * (1 - action) - return (state_c, state_h) diff --git a/research/lstm_object_detection/lstm/rnn_decoder_test.py b/research/lstm_object_detection/lstm/rnn_decoder_test.py deleted file mode 100644 index 480694f6fde..00000000000 --- a/research/lstm_object_detection/lstm/rnn_decoder_test.py +++ /dev/null @@ -1,306 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for lstm_object_detection.lstm.rnn_decoder.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow.compat.v1 as tf - -from tensorflow.contrib import layers as contrib_layers -from tensorflow.contrib import rnn as contrib_rnn -from lstm_object_detection.lstm import rnn_decoder - - -class MockRnnCell(contrib_rnn.RNNCell): - - def __init__(self, input_size, num_units): - self._input_size = input_size - self._num_units = num_units - self._filter_size = [3, 3] - - def __call__(self, inputs, state_tuple): - outputs = tf.concat([inputs, state_tuple[0]], axis=3) - new_state_tuple = (tf.multiply(state_tuple[0], 2), state_tuple[1]) - return outputs, new_state_tuple - - def state_size(self): - return self._num_units - - def output_size(self): - return self._input_size + self._num_units - - def pre_bottleneck(self, inputs, state, input_index): - with tf.variable_scope('bottleneck_%d' % input_index, reuse=tf.AUTO_REUSE): - inputs = contrib_layers.separable_conv2d( - tf.concat([inputs, state], 3), - self._input_size, - self._filter_size, - depth_multiplier=1, - activation_fn=tf.nn.relu6, - normalizer_fn=None) - return inputs - - -class RnnDecoderTest(tf.test.TestCase): - - def test_rnn_decoder_single_unroll(self): - batch_size = 2 - num_unroll = 1 - num_units = 64 - width = 8 - height = 10 - input_channels = 128 - - initial_state = tf.random_normal((batch_size, width, height, num_units)) - inputs = tf.random_normal([batch_size, width, height, input_channels]) - - rnn_cell = MockRnnCell(input_channels, num_units) - outputs, states = rnn_decoder.rnn_decoder( - decoder_inputs=[inputs] * num_unroll, - initial_state=(initial_state, initial_state), - cell=rnn_cell) - - self.assertEqual(len(outputs), num_unroll) - self.assertEqual(len(states), num_unroll) - with tf.Session() as sess: - sess.run(tf.global_variables_initializer()) - results = sess.run((outputs, states, inputs, initial_state)) - outputs_results = results[0] - states_results = results[1] - inputs_results = results[2] - initial_states_results = results[3] - self.assertEqual(outputs_results[0].shape, - (batch_size, width, height, input_channels + num_units)) - self.assertAllEqual( - outputs_results[0], - np.concatenate((inputs_results, initial_states_results), axis=3)) - self.assertEqual(states_results[0][0].shape, - (batch_size, width, height, num_units)) - self.assertEqual(states_results[0][1].shape, - (batch_size, width, height, num_units)) - self.assertAllEqual(states_results[0][0], - np.multiply(initial_states_results, 2.0)) - self.assertAllEqual(states_results[0][1], initial_states_results) - - def test_rnn_decoder_multiple_unroll(self): - batch_size = 2 - num_unroll = 3 - num_units = 64 - width = 8 - height = 10 - input_channels = 128 - - initial_state = tf.random_normal((batch_size, width, height, num_units)) - inputs = tf.random_normal([batch_size, width, height, input_channels]) - - rnn_cell = MockRnnCell(input_channels, num_units) - outputs, states = rnn_decoder.rnn_decoder( - decoder_inputs=[inputs] * num_unroll, - initial_state=(initial_state, initial_state), - cell=rnn_cell) - - self.assertEqual(len(outputs), num_unroll) - self.assertEqual(len(states), num_unroll) - with tf.Session() as sess: - sess.run(tf.global_variables_initializer()) - results = sess.run((outputs, states, inputs, initial_state)) - outputs_results = results[0] - states_results = results[1] - inputs_results = results[2] - initial_states_results = results[3] - for i in range(num_unroll): - previous_state = ([initial_states_results, initial_states_results] - if i == 0 else states_results[i - 1]) - self.assertEqual( - outputs_results[i].shape, - (batch_size, width, height, input_channels + num_units)) - self.assertAllEqual( - outputs_results[i], - np.concatenate((inputs_results, previous_state[0]), axis=3)) - self.assertEqual(states_results[i][0].shape, - (batch_size, width, height, num_units)) - self.assertEqual(states_results[i][1].shape, - (batch_size, width, height, num_units)) - self.assertAllEqual(states_results[i][0], - np.multiply(previous_state[0], 2.0)) - self.assertAllEqual(states_results[i][1], previous_state[1]) - - -class MultiInputRnnDecoderTest(tf.test.TestCase): - - def test_rnn_decoder_single_unroll(self): - batch_size = 2 - num_unroll = 1 - num_units = 12 - width = 8 - height = 10 - input_channels_large = 24 - input_channels_small = 12 - bottleneck_channels = 20 - - initial_state_c = tf.random_normal((batch_size, width, height, num_units)) - initial_state_h = tf.random_normal((batch_size, width, height, num_units)) - initial_state = (initial_state_c, initial_state_h) - inputs_large = tf.random_normal( - [batch_size, width, height, input_channels_large]) - inputs_small = tf.random_normal( - [batch_size, width, height, input_channels_small]) - - rnn_cell = MockRnnCell(bottleneck_channels, num_units) - outputs, states = rnn_decoder.multi_input_rnn_decoder( - decoder_inputs=[[inputs_large] * num_unroll, - [inputs_small] * num_unroll], - initial_state=initial_state, - cell=rnn_cell, - sequence_step=tf.zeros([batch_size]), - pre_bottleneck=True) - - self.assertEqual(len(outputs), num_unroll) - self.assertEqual(len(states), num_unroll) - with tf.Session() as sess: - sess.run(tf.global_variables_initializer()) - results = sess.run( - (outputs, states, inputs_large, inputs_small, initial_state)) - outputs_results = results[0] - states_results = results[1] - initial_states_results = results[4] - self.assertEqual( - outputs_results[0].shape, - (batch_size, width, height, bottleneck_channels + num_units)) - self.assertEqual(states_results[0][0].shape, - (batch_size, width, height, num_units)) - self.assertEqual(states_results[0][1].shape, - (batch_size, width, height, num_units)) - # The first step should always update state. - self.assertAllEqual(states_results[0][0], - np.multiply(initial_states_results[0], 2)) - self.assertAllEqual(states_results[0][1], initial_states_results[1]) - - def test_rnn_decoder_multiple_unroll(self): - batch_size = 2 - num_unroll = 3 - num_units = 12 - width = 8 - height = 10 - input_channels_large = 24 - input_channels_small = 12 - bottleneck_channels = 20 - - initial_state_c = tf.random_normal((batch_size, width, height, num_units)) - initial_state_h = tf.random_normal((batch_size, width, height, num_units)) - initial_state = (initial_state_c, initial_state_h) - inputs_large = tf.random_normal( - [batch_size, width, height, input_channels_large]) - inputs_small = tf.random_normal( - [batch_size, width, height, input_channels_small]) - - rnn_cell = MockRnnCell(bottleneck_channels, num_units) - outputs, states = rnn_decoder.multi_input_rnn_decoder( - decoder_inputs=[[inputs_large] * num_unroll, - [inputs_small] * num_unroll], - initial_state=initial_state, - cell=rnn_cell, - sequence_step=tf.zeros([batch_size]), - pre_bottleneck=True) - - self.assertEqual(len(outputs), num_unroll) - self.assertEqual(len(states), num_unroll) - with tf.Session() as sess: - sess.run(tf.global_variables_initializer()) - results = sess.run( - (outputs, states, inputs_large, inputs_small, initial_state)) - outputs_results = results[0] - states_results = results[1] - initial_states_results = results[4] - - # The first step should always update state. - self.assertAllEqual(states_results[0][0], - np.multiply(initial_states_results[0], 2)) - self.assertAllEqual(states_results[0][1], initial_states_results[1]) - for i in range(num_unroll): - self.assertEqual( - outputs_results[i].shape, - (batch_size, width, height, bottleneck_channels + num_units)) - self.assertEqual(states_results[i][0].shape, - (batch_size, width, height, num_units)) - self.assertEqual(states_results[i][1].shape, - (batch_size, width, height, num_units)) - - def test_rnn_decoder_multiple_unroll_with_skip(self): - batch_size = 2 - num_unroll = 5 - num_units = 12 - width = 8 - height = 10 - input_channels_large = 24 - input_channels_small = 12 - bottleneck_channels = 20 - skip = 2 - - initial_state_c = tf.random_normal((batch_size, width, height, num_units)) - initial_state_h = tf.random_normal((batch_size, width, height, num_units)) - initial_state = (initial_state_c, initial_state_h) - inputs_large = tf.random_normal( - [batch_size, width, height, input_channels_large]) - inputs_small = tf.random_normal( - [batch_size, width, height, input_channels_small]) - - rnn_cell = MockRnnCell(bottleneck_channels, num_units) - outputs, states = rnn_decoder.multi_input_rnn_decoder( - decoder_inputs=[[inputs_large] * num_unroll, - [inputs_small] * num_unroll], - initial_state=initial_state, - cell=rnn_cell, - sequence_step=tf.zeros([batch_size]), - pre_bottleneck=True, - selection_strategy='SKIP%d' % skip) - - self.assertEqual(len(outputs), num_unroll) - self.assertEqual(len(states), num_unroll) - with tf.Session() as sess: - sess.run(tf.global_variables_initializer()) - results = sess.run( - (outputs, states, inputs_large, inputs_small, initial_state)) - outputs_results = results[0] - states_results = results[1] - initial_states_results = results[4] - - for i in range(num_unroll): - self.assertEqual( - outputs_results[i].shape, - (batch_size, width, height, bottleneck_channels + num_units)) - self.assertEqual(states_results[i][0].shape, - (batch_size, width, height, num_units)) - self.assertEqual(states_results[i][1].shape, - (batch_size, width, height, num_units)) - - previous_state = ( - initial_states_results if i == 0 else states_results[i - 1]) - # State only updates during key frames - if i % (skip + 1) == 0: - self.assertAllEqual(states_results[i][0], - np.multiply(previous_state[0], 2)) - self.assertAllEqual(states_results[i][1], previous_state[1]) - else: - self.assertAllEqual(states_results[i][0], previous_state[0]) - self.assertAllEqual(states_results[i][1], previous_state[1]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/lstm/utils.py b/research/lstm_object_detection/lstm/utils.py deleted file mode 100644 index 0c87db4bb20..00000000000 --- a/research/lstm_object_detection/lstm/utils.py +++ /dev/null @@ -1,257 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Quantization related ops for LSTM.""" - -from __future__ import absolute_import -from __future__ import division - -import tensorflow.compat.v1 as tf -from tensorflow.contrib import framework as contrib_framework -from tensorflow.contrib import layers as contrib_layers -from tensorflow.python.training import moving_averages - - -def _quant_var( - name, - initializer_val, - vars_collection=tf.GraphKeys.MOVING_AVERAGE_VARIABLES, -): - """Create an var for storing the min/max quantization range.""" - return contrib_framework.model_variable( - name, - shape=[], - initializer=tf.constant_initializer(initializer_val), - collections=[vars_collection], - trainable=False) - - -def quantizable_concat(inputs, - axis, - is_training, - is_quantized=True, - default_min=0, - default_max=6, - ema_decay=0.999, - scope='quantized_concat'): - """Concat replacement with quantization option. - - Allows concat inputs to share the same min max ranges, - from experimental/gazelle/synthetic/model/tpu/utils.py. - - Args: - inputs: list of tensors to concatenate. - axis: dimension along which to concatenate. - is_training: true if the graph is a training graph. - is_quantized: flag to enable/disable quantization. - default_min: default min value for fake quant op. - default_max: default max value for fake quant op. - ema_decay: the moving average decay for the quantization variables. - scope: Optional scope for variable_scope. - - Returns: - Tensor resulting from concatenation of input tensors - """ - if is_quantized: - with tf.variable_scope(scope): - tf.logging.info('inputs: {}'.format(inputs)) - for t in inputs: - tf.logging.info(t) - - min_var = _quant_var('min', default_min) - max_var = _quant_var('max', default_max) - if not is_training: - # If we are building an eval graph just use the values in the variables. - quant_inputs = [ - tf.fake_quant_with_min_max_vars(t, min_var, max_var) for t in inputs - ] - tf.logging.info('min_val: {}'.format(min_var)) - tf.logging.info('max_val: {}'.format(max_var)) - else: - concat_tensors = tf.concat(inputs, axis=axis) - tf.logging.info('concat_tensors: {}'.format(concat_tensors)) - # TFLite requires that 0.0 is always in the [min; max] range. - range_min = tf.minimum( - tf.reduce_min(concat_tensors), 0.0, name='SafeQuantRangeMin') - range_max = tf.maximum( - tf.reduce_max(concat_tensors), 0.0, name='SafeQuantRangeMax') - # Otherwise we need to keep track of the moving averages of the min and - # of the elements of the input tensor max. - min_val = moving_averages.assign_moving_average( - min_var, - range_min, - ema_decay, - name='AssignMinEma') - max_val = moving_averages.assign_moving_average( - max_var, - range_max, - ema_decay, - name='AssignMaxEma') - tf.logging.info('min_val: {}'.format(min_val)) - tf.logging.info('max_val: {}'.format(max_val)) - quant_inputs = [ - tf.fake_quant_with_min_max_vars(t, min_val, max_val) for t in inputs - ] - tf.logging.info('quant_inputs: {}'.format(quant_inputs)) - outputs = tf.concat(quant_inputs, axis=axis) - tf.logging.info('outputs: {}'.format(outputs)) - else: - outputs = tf.concat(inputs, axis=axis) - return outputs - - -def quantizable_separable_conv2d(inputs, - num_outputs, - kernel_size, - is_quantized=True, - depth_multiplier=1, - stride=1, - activation_fn=tf.nn.relu6, - normalizer_fn=None, - weights_initializer=None, - pointwise_initializer=None, - scope=None): - """Quantization friendly backward compatible separable conv2d. - - This op has the same API is separable_conv2d. The main difference is that an - additional BiasAdd is manually inserted after the depthwise conv, such that - the depthwise bias will not have name conflict with pointwise bias. The - motivation of this op is that quantization script need BiasAdd in order to - recognize the op, in which a native call to separable_conv2d do not create - for the depthwise conv. - - Args: - inputs: A tensor of size [batch_size, height, width, channels]. - num_outputs: The number of pointwise convolution output filters. If is - None, then we skip the pointwise convolution stage. - kernel_size: A list of length 2: [kernel_height, kernel_width] of the - filters. Can be an int if both values are the same. - is_quantized: flag to enable/disable quantization. - depth_multiplier: The number of depthwise convolution output channels for - each input channel. The total number of depthwise convolution output - channels will be equal to num_filters_in * depth_multiplier. - stride: A list of length 2: [stride_height, stride_width], specifying the - depthwise convolution stride. Can be an int if both strides are the same. - activation_fn: Activation function. The default value is a ReLU function. - Explicitly set it to None to skip it and maintain a linear activation. - normalizer_fn: Normalization function to use instead of biases. - weights_initializer: An initializer for the depthwise weights. - pointwise_initializer: An initializer for the pointwise weights. - scope: Optional scope for variable_scope. - - Returns: - Tensor resulting from concatenation of input tensors - """ - if is_quantized: - outputs = contrib_layers.separable_conv2d( - inputs, - None, - kernel_size, - depth_multiplier=depth_multiplier, - stride=1, - activation_fn=None, - normalizer_fn=None, - biases_initializer=None, - weights_initializer=weights_initializer, - pointwise_initializer=None, - scope=scope) - outputs = contrib_layers.bias_add( - outputs, trainable=True, scope='%s_bias' % scope) - outputs = contrib_layers.conv2d( - outputs, - num_outputs, [1, 1], - activation_fn=activation_fn, - stride=stride, - normalizer_fn=normalizer_fn, - weights_initializer=pointwise_initializer, - scope=scope) - else: - outputs = contrib_layers.separable_conv2d( - inputs, - num_outputs, - kernel_size, - depth_multiplier=depth_multiplier, - stride=stride, - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - weights_initializer=weights_initializer, - pointwise_initializer=pointwise_initializer, - scope=scope) - return outputs - - -def quantize_op(inputs, - is_training=True, - is_quantized=True, - default_min=0, - default_max=6, - ema_decay=0.999, - scope='quant'): - """Inserts a fake quantization op after inputs. - - Args: - inputs: A tensor of size [batch_size, height, width, channels]. - is_training: true if the graph is a training graph. - is_quantized: flag to enable/disable quantization. - default_min: default min value for fake quant op. - default_max: default max value for fake quant op. - ema_decay: the moving average decay for the quantization variables. - scope: Optional scope for variable_scope. - - Returns: - Tensor resulting from quantizing the input tensors. - """ - if not is_quantized: - return inputs - - with tf.variable_scope(scope): - min_var = _quant_var('min', default_min) - max_var = _quant_var('max', default_max) - if not is_training: - # Just use variables in the checkpoint. - return tf.fake_quant_with_min_max_vars(inputs, min_var, max_var) - - # While training, collect EMAs of ranges seen, store in min_var, max_var. - # TFLite requires that 0.0 is always in the [min; max] range. - range_min = tf.minimum(tf.reduce_min(inputs), 0.0, 'SafeQuantRangeMin') - # We set the lower_bound of max_range to prevent range collapse. - range_max = tf.maximum(tf.reduce_max(inputs), 1e-5, 'SafeQuantRangeMax') - min_val = moving_averages.assign_moving_average( - min_var, range_min, ema_decay, name='AssignMinEma') - max_val = moving_averages.assign_moving_average( - max_var, range_max, ema_decay, name='AssignMaxEma') - return tf.fake_quant_with_min_max_vars(inputs, min_val, max_val) - - -def fixed_quantize_op(inputs, is_quantized=True, - fixed_min=0.0, fixed_max=6.0, scope='quant'): - """Inserts a fake quantization op with fixed range after inputs. - - Args: - inputs: A tensor of size [batch_size, height, width, channels]. - is_quantized: flag to enable/disable quantization. - fixed_min: fixed min value for fake quant op. - fixed_max: fixed max value for fake quant op. - scope: Optional scope for variable_scope. - - Returns: - Tensor resulting from quantizing the input tensors. - """ - if not is_quantized: - return inputs - - with tf.variable_scope(scope): - # Just use fixed quantization range. - return tf.fake_quant_with_min_max_args(inputs, fixed_min, fixed_max) diff --git a/research/lstm_object_detection/lstm/utils_test.py b/research/lstm_object_detection/lstm/utils_test.py deleted file mode 100644 index f5f5bc75db8..00000000000 --- a/research/lstm_object_detection/lstm/utils_test.py +++ /dev/null @@ -1,149 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for lstm_object_detection.lstm.utils.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf -from lstm_object_detection.lstm import utils - - -class QuantizableUtilsTest(tf.test.TestCase): - - def test_quantizable_concat_is_training(self): - inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32) - inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32) - concat_in_train = utils.quantizable_concat([inputs_1, inputs_2], - axis=3, - is_training=True) - self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list()) - self._check_min_max_ema(tf.get_default_graph()) - self._check_min_max_vars(tf.get_default_graph()) - - def test_quantizable_concat_inference(self): - inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32) - inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32) - concat_in_train = utils.quantizable_concat([inputs_1, inputs_2], - axis=3, - is_training=False) - self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list()) - self._check_no_min_max_ema(tf.get_default_graph()) - self._check_min_max_vars(tf.get_default_graph()) - - def test_quantizable_concat_not_quantized_is_training(self): - inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32) - inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32) - concat_in_train = utils.quantizable_concat([inputs_1, inputs_2], - axis=3, - is_training=True, - is_quantized=False) - self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list()) - self._check_no_min_max_ema(tf.get_default_graph()) - self._check_no_min_max_vars(tf.get_default_graph()) - - def test_quantizable_concat_not_quantized_inference(self): - inputs_1 = tf.zeros([4, 10, 10, 1], dtype=tf.float32) - inputs_2 = tf.ones([4, 10, 10, 2], dtype=tf.float32) - concat_in_train = utils.quantizable_concat([inputs_1, inputs_2], - axis=3, - is_training=False, - is_quantized=False) - self.assertAllEqual([4, 10, 10, 3], concat_in_train.shape.as_list()) - self._check_no_min_max_ema(tf.get_default_graph()) - self._check_no_min_max_vars(tf.get_default_graph()) - - def test_quantize_op_is_training(self): - inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32) - outputs = utils.quantize_op(inputs) - self.assertAllEqual(inputs.shape.as_list(), outputs.shape.as_list()) - self._check_min_max_ema(tf.get_default_graph()) - self._check_min_max_vars(tf.get_default_graph()) - - def test_quantize_op_inference(self): - inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32) - outputs = utils.quantize_op(inputs, is_training=False) - self.assertAllEqual(inputs.shape.as_list(), outputs.shape.as_list()) - self._check_no_min_max_ema(tf.get_default_graph()) - self._check_min_max_vars(tf.get_default_graph()) - - def test_fixed_quantize_op(self): - inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32) - outputs = utils.fixed_quantize_op(inputs) - self.assertAllEqual(inputs.shape.as_list(), outputs.shape.as_list()) - self._check_no_min_max_ema(tf.get_default_graph()) - self._check_no_min_max_vars(tf.get_default_graph()) - - def _check_min_max_vars(self, graph): - op_types = [op.type for op in graph.get_operations()] - self.assertTrue( - any('FakeQuantWithMinMaxVars' in op_type for op_type in op_types)) - - def _check_min_max_ema(self, graph): - op_names = [op.name for op in graph.get_operations()] - self.assertTrue(any('AssignMinEma' in name for name in op_names)) - self.assertTrue(any('AssignMaxEma' in name for name in op_names)) - self.assertTrue(any('SafeQuantRangeMin' in name for name in op_names)) - self.assertTrue(any('SafeQuantRangeMax' in name for name in op_names)) - - def _check_no_min_max_vars(self, graph): - op_types = [op.type for op in graph.get_operations()] - self.assertFalse( - any('FakeQuantWithMinMaxVars' in op_type for op_type in op_types)) - - def _check_no_min_max_ema(self, graph): - op_names = [op.name for op in graph.get_operations()] - self.assertFalse(any('AssignMinEma' in name for name in op_names)) - self.assertFalse(any('AssignMaxEma' in name for name in op_names)) - self.assertFalse(any('SafeQuantRangeMin' in name for name in op_names)) - self.assertFalse(any('SafeQuantRangeMax' in name for name in op_names)) - - -class QuantizableSeparableConv2dTest(tf.test.TestCase): - - def test_quantizable_separable_conv2d(self): - inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32) - num_outputs = 64 - kernel_size = [3, 3] - scope = 'QuantSeparable' - outputs = utils.quantizable_separable_conv2d( - inputs, num_outputs, kernel_size, scope=scope) - self.assertAllEqual([4, 10, 10, num_outputs], outputs.shape.as_list()) - self._check_depthwise_bias_add(tf.get_default_graph(), scope) - - def test_quantizable_separable_conv2d_not_quantized(self): - inputs = tf.zeros([4, 10, 10, 128], dtype=tf.float32) - num_outputs = 64 - kernel_size = [3, 3] - scope = 'QuantSeparable' - outputs = utils.quantizable_separable_conv2d( - inputs, num_outputs, kernel_size, is_quantized=False, scope=scope) - self.assertAllEqual([4, 10, 10, num_outputs], outputs.shape.as_list()) - self._check_no_depthwise_bias_add(tf.get_default_graph(), scope) - - def _check_depthwise_bias_add(self, graph, scope): - op_names = [op.name for op in graph.get_operations()] - self.assertTrue( - any('%s_bias/BiasAdd' % scope in name for name in op_names)) - - def _check_no_depthwise_bias_add(self, graph, scope): - op_names = [op.name for op in graph.get_operations()] - self.assertFalse( - any('%s_bias/BiasAdd' % scope in name for name in op_names)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/meta_architectures/__init__.py b/research/lstm_object_detection/meta_architectures/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch.py b/research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch.py deleted file mode 100644 index 22edc97ee34..00000000000 --- a/research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch.py +++ /dev/null @@ -1,463 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""LSTM SSD Meta-architecture definition. - -General tensorflow implementation of convolutional Multibox/SSD detection -models with LSTM states, for use on video data. This implementation supports -both regular LSTM-SSD and interleaved LSTM-SSD framework. - -See https://arxiv.org/abs/1711.06368 and https://arxiv.org/abs/1903.10172 -for details. -""" -import abc -import re -import tensorflow.compat.v1 as tf - -from object_detection.core import box_list_ops -from object_detection.core import matcher -from object_detection.core import standard_fields as fields -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -class LSTMSSDMetaArch(ssd_meta_arch.SSDMetaArch): - """LSTM Meta-architecture definition.""" - - def __init__(self, - is_training, - anchor_generator, - box_predictor, - box_coder, - feature_extractor, - encode_background_as_zeros, - image_resizer_fn, - non_max_suppression_fn, - score_conversion_fn, - classification_loss, - localization_loss, - classification_loss_weight, - localization_loss_weight, - normalize_loss_by_num_matches, - hard_example_miner, - unroll_length, - target_assigner_instance, - add_summaries=True): - super(LSTMSSDMetaArch, self).__init__( - is_training=is_training, - anchor_generator=anchor_generator, - box_predictor=box_predictor, - box_coder=box_coder, - feature_extractor=feature_extractor, - encode_background_as_zeros=encode_background_as_zeros, - image_resizer_fn=image_resizer_fn, - non_max_suppression_fn=non_max_suppression_fn, - score_conversion_fn=score_conversion_fn, - classification_loss=classification_loss, - localization_loss=localization_loss, - classification_loss_weight=classification_loss_weight, - localization_loss_weight=localization_loss_weight, - normalize_loss_by_num_matches=normalize_loss_by_num_matches, - hard_example_miner=hard_example_miner, - target_assigner_instance=target_assigner_instance, - add_summaries=add_summaries) - self._unroll_length = unroll_length - - @property - def unroll_length(self): - return self._unroll_length - - @unroll_length.setter - def unroll_length(self, unroll_length): - self._unroll_length = unroll_length - - def predict(self, preprocessed_inputs, true_image_shapes, states=None, - state_name='lstm_state', feature_scope=None): - with tf.variable_scope(self._extract_features_scope, - values=[preprocessed_inputs], reuse=tf.AUTO_REUSE): - feature_maps = self._feature_extractor.extract_features( - preprocessed_inputs, states, state_name, - unroll_length=self._unroll_length, scope=feature_scope) - feature_map_spatial_dims = self._get_feature_map_spatial_dims(feature_maps) - image_shape = shape_utils.combined_static_and_dynamic_shape( - preprocessed_inputs) - self._batch_size = preprocessed_inputs.shape[0].value / self._unroll_length - self._states = states - anchors = self._anchor_generator.generate(feature_map_spatial_dims, - im_height=image_shape[1], - im_width=image_shape[2]) - with tf.variable_scope('MultipleGridAnchorGenerator', reuse=tf.AUTO_REUSE): - self._anchors = box_list_ops.concatenate(anchors) - prediction_dict = self._box_predictor.predict( - feature_maps, self._anchor_generator.num_anchors_per_location()) - with tf.variable_scope('Loss', reuse=tf.AUTO_REUSE): - box_encodings = tf.concat(prediction_dict['box_encodings'], axis=1) - if box_encodings.shape.ndims == 4 and box_encodings.shape[2] == 1: - box_encodings = tf.squeeze(box_encodings, axis=2) - class_predictions_with_background = tf.concat( - prediction_dict['class_predictions_with_background'], axis=1) - predictions_dict = { - 'preprocessed_inputs': preprocessed_inputs, - 'box_encodings': box_encodings, - 'class_predictions_with_background': class_predictions_with_background, - 'feature_maps': feature_maps, - 'anchors': self._anchors.get(), - 'states_and_outputs': self._feature_extractor.states_and_outputs, - } - # In cases such as exporting the model, the states is always zero. Thus the - # step should be ignored. - if states is not None: - predictions_dict['step'] = self._feature_extractor.step - return predictions_dict - - def loss(self, prediction_dict, true_image_shapes, scope=None): - """Computes scalar loss tensors with respect to provided groundtruth. - - Calling this function requires that groundtruth tensors have been - provided via the provide_groundtruth function. - - Args: - prediction_dict: a dictionary holding prediction tensors with - 1) box_encodings: 3-D float tensor of shape [batch_size, num_anchors, - box_code_dimension] containing predicted boxes. - 2) class_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, num_classes+1] containing class predictions - (logits) for each of the anchors. Note that this tensor *includes* - background class predictions. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - scope: Optional scope name. - - Returns: - a dictionary mapping loss keys (`localization_loss` and - `classification_loss`) to scalar tensors representing corresponding loss - values. - """ - with tf.name_scope(scope, 'Loss', prediction_dict.values()): - keypoints = None - if self.groundtruth_has_field(fields.BoxListFields.keypoints): - keypoints = self.groundtruth_lists(fields.BoxListFields.keypoints) - weights = None - if self.groundtruth_has_field(fields.BoxListFields.weights): - weights = self.groundtruth_lists(fields.BoxListFields.weights) - (batch_cls_targets, batch_cls_weights, batch_reg_targets, - batch_reg_weights, batch_match) = self._assign_targets( - self.groundtruth_lists(fields.BoxListFields.boxes), - self.groundtruth_lists(fields.BoxListFields.classes), - keypoints, weights) - match_list = [matcher.Match(match) for match in tf.unstack(batch_match)] - if self._add_summaries: - self._summarize_target_assignment( - self.groundtruth_lists(fields.BoxListFields.boxes), match_list) - location_losses = self._localization_loss( - prediction_dict['box_encodings'], - batch_reg_targets, - ignore_nan_targets=True, - weights=batch_reg_weights) - cls_losses = ops.reduce_sum_trailing_dimensions( - self._classification_loss( - prediction_dict['class_predictions_with_background'], - batch_cls_targets, - weights=batch_cls_weights), - ndims=2) - - if self._hard_example_miner: - (loc_loss_list, cls_loss_list) = self._apply_hard_mining( - location_losses, cls_losses, prediction_dict, match_list) - localization_loss = tf.reduce_sum(tf.stack(loc_loss_list)) - classification_loss = tf.reduce_sum(tf.stack(cls_loss_list)) - - if self._add_summaries: - self._hard_example_miner.summarize() - else: - if self._add_summaries: - class_ids = tf.argmax(batch_cls_targets, axis=2) - flattened_class_ids = tf.reshape(class_ids, [-1]) - flattened_classification_losses = tf.reshape(cls_losses, [-1]) - self._summarize_anchor_classification_loss( - flattened_class_ids, flattened_classification_losses) - localization_loss = tf.reduce_sum(location_losses) - classification_loss = tf.reduce_sum(cls_losses) - - # Optionally normalize by number of positive matches - normalizer = tf.constant(1.0, dtype=tf.float32) - if self._normalize_loss_by_num_matches: - normalizer = tf.maximum(tf.to_float(tf.reduce_sum(batch_reg_weights)), - 1.0) - - with tf.name_scope('localization_loss'): - localization_loss_normalizer = normalizer - if self._normalize_loc_loss_by_codesize: - localization_loss_normalizer *= self._box_coder.code_size - localization_loss = ((self._localization_loss_weight / ( - localization_loss_normalizer)) * localization_loss) - with tf.name_scope('classification_loss'): - classification_loss = ((self._classification_loss_weight / normalizer) * - classification_loss) - - loss_dict = { - 'localization_loss': localization_loss, - 'classification_loss': classification_loss - } - return loss_dict - - def restore_map(self, fine_tune_checkpoint_type='lstm'): - """Returns a map of variables to load from a foreign checkpoint. - - See parent class for details. - - Args: - fine_tune_checkpoint_type: the type of checkpoint to restore from, either - SSD/LSTM detection checkpoint (with compatible variable names) - classification checkpoint for initialization prior to training. - Available options: `classification`, `detection`, `interleaved`, - and `lstm`. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - Raises: - ValueError: if fine_tune_checkpoint_type is not among - `classification`/`detection`/`interleaved`/`lstm`. - """ - if fine_tune_checkpoint_type not in [ - 'classification', 'detection', 'interleaved', 'lstm', - 'interleaved_pretrain' - ]: - raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format( - fine_tune_checkpoint_type)) - - self._restored_networks += 1 - base_network_scope = self.get_base_network_scope() - if base_network_scope: - scope_to_replace = '{0}_{1}'.format(base_network_scope, - self._restored_networks) - - interleaved_model = False - for variable in tf.global_variables(): - if scope_to_replace in variable.op.name: - interleaved_model = True - break - - variables_to_restore = {} - for variable in tf.global_variables(): - var_name = variable.op.name - if 'global_step' in var_name: - continue - - # Remove FeatureExtractor prefix for classification checkpoints. - if (fine_tune_checkpoint_type == 'classification' or - fine_tune_checkpoint_type == 'interleaved_pretrain'): - var_name = ( - re.split('^' + self._extract_features_scope + '/', var_name)[-1]) - - # When loading from single frame detection checkpoints, we need to - # remap FeatureMaps variable names. - if ('FeatureMaps' in var_name and - fine_tune_checkpoint_type == 'detection'): - var_name = var_name.replace('FeatureMaps', - self.get_base_network_scope()) - - # Load interleaved checkpoint specifically. - if interleaved_model: # Interleaved LSTD. - if 'interleaved' in fine_tune_checkpoint_type: - variables_to_restore[var_name] = variable - else: - # Restore non-base layers from the first checkpoint only. - if self._restored_networks == 1: - if base_network_scope + '_' not in var_name: # LSTM and FeatureMap - variables_to_restore[var_name] = variable - if scope_to_replace in var_name: - var_name = var_name.replace(scope_to_replace, base_network_scope) - variables_to_restore[var_name] = variable - else: - # Restore from the first model of interleaved checkpoints - if 'interleaved' in fine_tune_checkpoint_type: - var_name = var_name.replace(self.get_base_network_scope(), - self.get_base_network_scope() + '_1', 1) - - variables_to_restore[var_name] = variable - - return variables_to_restore - - def get_base_network_scope(self): - """Returns the variable scope of the base network. - - Returns: - The variable scope of the feature extractor base network, e.g. MobilenetV1 - """ - return self._feature_extractor.get_base_network_scope() - - -class LSTMSSDFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """LSTM SSD Meta-architecture Feature Extractor definition.""" - - __metaclass__ = abc.ABCMeta - - @property - def clip_state(self): - return self._clip_state - - @clip_state.setter - def clip_state(self, clip_state): - self._clip_state = clip_state - - @property - def depth_multipliers(self): - return self._depth_multipliers - - @depth_multipliers.setter - def depth_multipliers(self, depth_multipliers): - self._depth_multipliers = depth_multipliers - - @property - def lstm_state_depth(self): - return self._lstm_state_depth - - @lstm_state_depth.setter - def lstm_state_depth(self, lstm_state_depth): - self._lstm_state_depth = lstm_state_depth - - @property - def is_quantized(self): - return self._is_quantized - - @is_quantized.setter - def is_quantized(self, is_quantized): - self._is_quantized = is_quantized - - @property - def interleaved(self): - return False - - @property - def states_and_outputs(self): - """LSTM states and outputs. - - This variable includes both LSTM states {C_t} and outputs {h_t}. - - Returns: - states_and_outputs: A list of 4-D float tensors, including the lstm state - and output at each timestep. - """ - return self._states_out - - @property - def step(self): - return self._step - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def get_base_network_scope(self): - """Returns the variable scope of the base network. - - Returns: - The variable scope of the base network, e.g. MobilenetV1 - """ - return self._base_network_scope - - @abc.abstractmethod - def create_lstm_cell(self, batch_size, output_size, state_saver, state_name): - """Create the LSTM cell, and initialize state if necessary. - - Args: - batch_size: input batch size. - output_size: output size of the lstm cell, [width, height]. - state_saver: a state saver object with methods `state` and `save_state`. - state_name: string, the name to use with the state_saver. - Returns: - lstm_cell: the lstm cell unit. - init_state: initial state representations. - step: the step - """ - pass - - -class LSTMSSDInterleavedFeatureExtractor(LSTMSSDFeatureExtractor): - """LSTM SSD Meta-architecture Interleaved Feature Extractor definition.""" - - __metaclass__ = abc.ABCMeta - - @property - def pre_bottleneck(self): - return self._pre_bottleneck - - @pre_bottleneck.setter - def pre_bottleneck(self, pre_bottleneck): - self._pre_bottleneck = pre_bottleneck - - @property - def low_res(self): - return self._low_res - - @low_res.setter - def low_res(self, low_res): - self._low_res = low_res - - @property - def interleaved(self): - return True - - @property - def interleave_method(self): - return self._interleave_method - - @interleave_method.setter - def interleave_method(self, interleave_method): - self._interleave_method = interleave_method - - @abc.abstractmethod - def extract_base_features_large(self, preprocessed_inputs): - """Extract the large base model features. - - Args: - preprocessed_inputs: preprocessed input images of shape: - [batch, width, height, depth]. - - Returns: - net: the last feature map created from the base feature extractor. - end_points: a dictionary of feature maps created. - """ - pass - - @abc.abstractmethod - def extract_base_features_small(self, preprocessed_inputs): - """Extract the small base model features. - - Args: - preprocessed_inputs: preprocessed input images of shape: - [batch, width, height, depth]. - - Returns: - net: the last feature map created from the base feature extractor. - end_points: a dictionary of feature maps created. - """ - pass diff --git a/research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch_test.py b/research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch_test.py deleted file mode 100644 index 03e8a127460..00000000000 --- a/research/lstm_object_detection/meta_architectures/lstm_ssd_meta_arch_test.py +++ /dev/null @@ -1,320 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for meta_architectures.lstm_ssd_meta_arch.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools - -import numpy as np -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from lstm_object_detection.lstm import lstm_cells -from lstm_object_detection.meta_architectures import lstm_ssd_meta_arch -from object_detection.core import anchor_generator -from object_detection.core import box_list -from object_detection.core import losses -from object_detection.core import post_processing -from object_detection.core import region_similarity_calculator as sim_calc -from object_detection.core import standard_fields as fields -from object_detection.core import target_assigner -from object_detection.models import feature_map_generators -from object_detection.utils import test_case -from object_detection.utils import test_utils - - -MAX_TOTAL_NUM_BOXES = 5 -NUM_CLASSES = 1 - - -class FakeLSTMFeatureExtractor( - lstm_ssd_meta_arch.LSTMSSDFeatureExtractor): - - def __init__(self): - super(FakeLSTMFeatureExtractor, self).__init__( - is_training=True, - depth_multiplier=1.0, - min_depth=0, - pad_to_multiple=1, - conv_hyperparams_fn=self.scope_fn) - self._lstm_state_depth = 256 - - def scope_fn(self): - with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu6) as sc: - return sc - - def create_lstm_cell(self): - pass - - def extract_features(self, preprocessed_inputs, state_saver=None, - state_name='lstm_state', unroll_length=5, scope=None): - with tf.variable_scope('mock_model'): - net = slim.conv2d(inputs=preprocessed_inputs, num_outputs=32, - kernel_size=1, scope='layer1') - image_features = {'last_layer': net} - - self._states_out = {} - feature_map_layout = { - 'from_layer': ['last_layer'], - 'layer_depth': [-1], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=(self._depth_multiplier), - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - return list(feature_maps.values()) - - -class FakeLSTMInterleavedFeatureExtractor( - lstm_ssd_meta_arch.LSTMSSDInterleavedFeatureExtractor): - - def __init__(self): - super(FakeLSTMInterleavedFeatureExtractor, self).__init__( - is_training=True, - depth_multiplier=1.0, - min_depth=0, - pad_to_multiple=1, - conv_hyperparams_fn=self.scope_fn) - self._lstm_state_depth = 256 - - def scope_fn(self): - with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu6) as sc: - return sc - - def create_lstm_cell(self): - pass - - def extract_base_features_large(self, preprocessed_inputs): - with tf.variable_scope('base_large'): - net = slim.conv2d(inputs=preprocessed_inputs, num_outputs=32, - kernel_size=1, scope='layer1') - return net - - def extract_base_features_small(self, preprocessed_inputs): - with tf.variable_scope('base_small'): - net = slim.conv2d(inputs=preprocessed_inputs, num_outputs=32, - kernel_size=1, scope='layer1') - return net - - def extract_features(self, preprocessed_inputs, state_saver=None, - state_name='lstm_state', unroll_length=5, scope=None): - with tf.variable_scope('mock_model'): - net_large = self.extract_base_features_large(preprocessed_inputs) - net_small = self.extract_base_features_small(preprocessed_inputs) - net = slim.conv2d( - inputs=tf.concat([net_large, net_small], axis=3), - num_outputs=32, - kernel_size=1, - scope='layer1') - image_features = {'last_layer': net} - - self._states_out = {} - feature_map_layout = { - 'from_layer': ['last_layer'], - 'layer_depth': [-1], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=(self._depth_multiplier), - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - return list(feature_maps.values()) - - -class MockAnchorGenerator2x2(anchor_generator.AnchorGenerator): - """Sets up a simple 2x2 anchor grid on the unit square.""" - - def name_scope(self): - return 'MockAnchorGenerator' - - def num_anchors_per_location(self): - return [1] - - def _generate(self, feature_map_shape_list, im_height, im_width): - return [box_list.BoxList( - tf.constant([[0, 0, .5, .5], - [0, .5, .5, 1], - [.5, 0, 1, .5], - [1., 1., 1.5, 1.5] # Anchor that is outside clip_window. - ], tf.float32))] - - def num_anchors(self): - return 4 - - -class LSTMSSDMetaArchTest(test_case.TestCase): - - def _create_model(self, - interleaved=False, - apply_hard_mining=True, - normalize_loc_loss_by_codesize=False, - add_background_class=True, - random_example_sampling=False, - use_expected_classification_loss_under_sampling=False, - min_num_negative_samples=1, - desired_negative_sampling_ratio=3, - unroll_length=1): - num_classes = NUM_CLASSES - is_training = False - mock_anchor_generator = MockAnchorGenerator2x2() - mock_box_predictor = test_utils.MockBoxPredictor(is_training, num_classes) - mock_box_coder = test_utils.MockBoxCoder() - if interleaved: - fake_feature_extractor = FakeLSTMInterleavedFeatureExtractor() - else: - fake_feature_extractor = FakeLSTMFeatureExtractor() - mock_matcher = test_utils.MockMatcher() - region_similarity_calculator = sim_calc.IouSimilarity() - encode_background_as_zeros = False - def image_resizer_fn(image): - return [tf.identity(image), tf.shape(image)] - - classification_loss = losses.WeightedSigmoidClassificationLoss() - localization_loss = losses.WeightedSmoothL1LocalizationLoss() - non_max_suppression_fn = functools.partial( - post_processing.batch_multiclass_non_max_suppression, - score_thresh=-20.0, - iou_thresh=1.0, - max_size_per_class=5, - max_total_size=MAX_TOTAL_NUM_BOXES) - classification_loss_weight = 1.0 - localization_loss_weight = 1.0 - negative_class_weight = 1.0 - normalize_loss_by_num_matches = False - - hard_example_miner = None - if apply_hard_mining: - # This hard example miner is expected to be a no-op. - hard_example_miner = losses.HardExampleMiner( - num_hard_examples=None, - iou_threshold=1.0) - - target_assigner_instance = target_assigner.TargetAssigner( - region_similarity_calculator, - mock_matcher, - mock_box_coder, - negative_class_weight=negative_class_weight) - - code_size = 4 - model = lstm_ssd_meta_arch.LSTMSSDMetaArch( - is_training=is_training, - anchor_generator=mock_anchor_generator, - box_predictor=mock_box_predictor, - box_coder=mock_box_coder, - feature_extractor=fake_feature_extractor, - encode_background_as_zeros=encode_background_as_zeros, - image_resizer_fn=image_resizer_fn, - non_max_suppression_fn=non_max_suppression_fn, - score_conversion_fn=tf.identity, - classification_loss=classification_loss, - localization_loss=localization_loss, - classification_loss_weight=classification_loss_weight, - localization_loss_weight=localization_loss_weight, - normalize_loss_by_num_matches=normalize_loss_by_num_matches, - hard_example_miner=hard_example_miner, - unroll_length=unroll_length, - target_assigner_instance=target_assigner_instance, - add_summaries=False) - return model, num_classes, mock_anchor_generator.num_anchors(), code_size - - def _get_value_for_matching_key(self, dictionary, suffix): - for key in dictionary.keys(): - if key.endswith(suffix): - return dictionary[key] - raise ValueError('key not found {}'.format(suffix)) - - def test_predict_returns_correct_items_and_sizes(self): - batch_size = 3 - height = width = 2 - num_unroll = 1 - - graph = tf.Graph() - with graph.as_default(): - model, num_classes, num_anchors, code_size = self._create_model() - preprocessed_images = tf.random_uniform( - [batch_size * num_unroll, height, width, 3], - minval=-1., - maxval=1.) - true_image_shapes = tf.tile( - [[height, width, 3]], [batch_size, 1]) - prediction_dict = model.predict(preprocessed_images, true_image_shapes) - - - self.assertIn('preprocessed_inputs', prediction_dict) - self.assertIn('box_encodings', prediction_dict) - self.assertIn('class_predictions_with_background', prediction_dict) - self.assertIn('feature_maps', prediction_dict) - self.assertIn('anchors', prediction_dict) - self.assertAllEqual( - [batch_size * num_unroll, height, width, 3], - prediction_dict['preprocessed_inputs'].shape.as_list()) - self.assertAllEqual( - [batch_size * num_unroll, num_anchors, code_size], - prediction_dict['box_encodings'].shape.as_list()) - self.assertAllEqual( - [batch_size * num_unroll, num_anchors, num_classes + 1], - prediction_dict['class_predictions_with_background'].shape.as_list()) - self.assertAllEqual( - [num_anchors, code_size], - prediction_dict['anchors'].shape.as_list()) - - def test_interleaved_predict_returns_correct_items_and_sizes(self): - batch_size = 3 - height = width = 2 - num_unroll = 1 - - graph = tf.Graph() - with graph.as_default(): - model, num_classes, num_anchors, code_size = self._create_model( - interleaved=True) - preprocessed_images = tf.random_uniform( - [batch_size * num_unroll, height, width, 3], - minval=-1., - maxval=1.) - true_image_shapes = tf.tile( - [[height, width, 3]], [batch_size, 1]) - prediction_dict = model.predict(preprocessed_images, true_image_shapes) - - self.assertIn('preprocessed_inputs', prediction_dict) - self.assertIn('box_encodings', prediction_dict) - self.assertIn('class_predictions_with_background', prediction_dict) - self.assertIn('feature_maps', prediction_dict) - self.assertIn('anchors', prediction_dict) - self.assertAllEqual( - [batch_size * num_unroll, height, width, 3], - prediction_dict['preprocessed_inputs'].shape.as_list()) - self.assertAllEqual( - [batch_size * num_unroll, num_anchors, code_size], - prediction_dict['box_encodings'].shape.as_list()) - self.assertAllEqual( - [batch_size * num_unroll, num_anchors, num_classes + 1], - prediction_dict['class_predictions_with_background'].shape.as_list()) - self.assertAllEqual( - [num_anchors, code_size], - prediction_dict['anchors'].shape.as_list()) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/metrics/__init__.py b/research/lstm_object_detection/metrics/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/metrics/coco_evaluation_all_frames.py b/research/lstm_object_detection/metrics/coco_evaluation_all_frames.py deleted file mode 100644 index 8e6d336cbf7..00000000000 --- a/research/lstm_object_detection/metrics/coco_evaluation_all_frames.py +++ /dev/null @@ -1,124 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Class for evaluating video object detections with COCO metrics.""" - -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields -from object_detection.metrics import coco_evaluation -from object_detection.metrics import coco_tools - - -class CocoEvaluationAllFrames(coco_evaluation.CocoDetectionEvaluator): - """Class to evaluate COCO detection metrics for frame sequences. - - The class overrides two functions: add_single_ground_truth_image_info and - add_single_detected_image_info. - - For the evaluation of sequence video detection, by iterating through the - entire groundtruth_dict, all the frames in the unrolled frames in one LSTM - training sample are considered. Therefore, both groundtruth and detection - results of all frames are added for the evaluation. This is used when all the - frames are labeled in the video object detection training job. - """ - - def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): - """Add groundtruth results of all frames to the eval pipeline. - - This method overrides the function defined in the base class. - - Args: - image_id: A unique string/integer identifier for the image. - groundtruth_dict: A list of dictionary containing - - InputDataFields.groundtruth_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - InputDataFields.groundtruth_classes: integer numpy array of shape - [num_boxes] containing 1-indexed groundtruth classes for the boxes. - InputDataFields.groundtruth_is_crowd (optional): integer numpy array of - shape [num_boxes] containing iscrowd flag for groundtruth boxes. - """ - for idx, gt in enumerate(groundtruth_dict): - if not gt: - continue - - image_frame_id = '{}_{}'.format(image_id, idx) - if image_frame_id in self._image_ids: - tf.logging.warning( - 'Ignoring ground truth with image id %s since it was ' - 'previously added', image_frame_id) - continue - - self._groundtruth_list.extend( - coco_tools.ExportSingleImageGroundtruthToCoco( - image_id=image_frame_id, - next_annotation_id=self._annotation_id, - category_id_set=self._category_id_set, - groundtruth_boxes=gt[ - standard_fields.InputDataFields.groundtruth_boxes], - groundtruth_classes=gt[ - standard_fields.InputDataFields.groundtruth_classes])) - self._annotation_id += ( - gt[standard_fields.InputDataFields.groundtruth_boxes].shape[0]) - - # Boolean to indicate whether a detection has been added for this image. - self._image_ids[image_frame_id] = False - - def add_single_detected_image_info(self, image_id, detections_dict): - """Add detection results of all frames to the eval pipeline. - - This method overrides the function defined in the base class. - - Args: - image_id: A unique string/integer identifier for the image. - detections_dict: A list of dictionary containing - - DetectionResultFields.detection_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` detection boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - DetectionResultFields.detection_scores: float32 numpy array of shape - [num_boxes] containing detection scores for the boxes. - DetectionResultFields.detection_classes: integer numpy array of shape - [num_boxes] containing 1-indexed detection classes for the boxes. - - Raises: - ValueError: If groundtruth for the image_id is not available. - """ - for idx, det in enumerate(detections_dict): - if not det: - continue - - image_frame_id = '{}_{}'.format(image_id, idx) - if image_frame_id not in self._image_ids: - raise ValueError( - 'Missing groundtruth for image-frame id: {}'.format(image_frame_id)) - - if self._image_ids[image_frame_id]: - tf.logging.warning( - 'Ignoring detection with image id %s since it was ' - 'previously added', image_frame_id) - continue - - self._detection_boxes_list.extend( - coco_tools.ExportSingleImageDetectionBoxesToCoco( - image_id=image_frame_id, - category_id_set=self._category_id_set, - detection_boxes=det[ - standard_fields.DetectionResultFields.detection_boxes], - detection_scores=det[ - standard_fields.DetectionResultFields.detection_scores], - detection_classes=det[ - standard_fields.DetectionResultFields.detection_classes])) - self._image_ids[image_frame_id] = True diff --git a/research/lstm_object_detection/metrics/coco_evaluation_all_frames_test.py b/research/lstm_object_detection/metrics/coco_evaluation_all_frames_test.py deleted file mode 100644 index 9c1e7b7546b..00000000000 --- a/research/lstm_object_detection/metrics/coco_evaluation_all_frames_test.py +++ /dev/null @@ -1,156 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for video_object_detection.metrics.coco_video_evaluation.""" - -import numpy as np -import tensorflow.compat.v1 as tf -from lstm_object_detection.metrics import coco_evaluation_all_frames -from object_detection.core import standard_fields - - -class CocoEvaluationAllFramesTest(tf.test.TestCase): - - def testGroundtruthAndDetectionsDisagreeOnAllFrames(self): - """Tests that mAP is calculated on several different frame results.""" - category_list = [{'id': 0, 'name': 'dog'}, {'id': 1, 'name': 'cat'}] - video_evaluator = coco_evaluation_all_frames.CocoEvaluationAllFrames( - category_list) - video_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict=[{ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]) - }, { - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]) - }]) - video_evaluator.add_single_detected_image_info( - image_id='image1', - # A different groundtruth box on the frame other than the last one. - detections_dict=[{ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }, { - standard_fields.DetectionResultFields.detection_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }]) - - metrics = video_evaluator.evaluate() - self.assertNotEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - - def testGroundtruthAndDetections(self): - """Tests that mAP is calculated correctly on GT and Detections.""" - category_list = [{'id': 0, 'name': 'dog'}, {'id': 1, 'name': 'cat'}] - video_evaluator = coco_evaluation_all_frames.CocoEvaluationAllFrames( - category_list) - video_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict=[{ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]) - }]) - video_evaluator.add_single_ground_truth_image_info( - image_id='image2', - groundtruth_dict=[{ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]) - }]) - video_evaluator.add_single_ground_truth_image_info( - image_id='image3', - groundtruth_dict=[{ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 100., 100., 120.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]) - }]) - video_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict=[{ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }]) - video_evaluator.add_single_detected_image_info( - image_id='image2', - detections_dict=[{ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }]) - video_evaluator.add_single_detected_image_info( - image_id='image3', - detections_dict=[{ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[50., 100., 100., 120.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }]) - metrics = video_evaluator.evaluate() - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - - def testMissingDetectionResults(self): - """Tests if groundtrue is missing, raises ValueError.""" - category_list = [{'id': 0, 'name': 'dog'}] - video_evaluator = coco_evaluation_all_frames.CocoEvaluationAllFrames( - category_list) - video_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict=[{ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]) - }]) - with self.assertRaisesRegexp(ValueError, - r'Missing groundtruth for image-frame id:.*'): - video_evaluator.add_single_detected_image_info( - image_id='image3', - detections_dict=[{ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/model_builder.py b/research/lstm_object_detection/model_builder.py deleted file mode 100644 index d622558cf75..00000000000 --- a/research/lstm_object_detection/model_builder.py +++ /dev/null @@ -1,192 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A function to build a DetectionModel from configuration.""" -from lstm_object_detection.meta_architectures import lstm_ssd_meta_arch -from lstm_object_detection.models import lstm_ssd_interleaved_mobilenet_v2_feature_extractor -from lstm_object_detection.models import lstm_ssd_mobilenet_v1_feature_extractor -from object_detection.builders import anchor_generator_builder -from object_detection.builders import box_coder_builder -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.builders import image_resizer_builder -from object_detection.builders import losses_builder -from object_detection.builders import matcher_builder -from object_detection.builders import model_builder -from object_detection.builders import post_processing_builder -from object_detection.builders import region_similarity_calculator_builder as sim_calc -from object_detection.core import target_assigner - -model_builder.SSD_FEATURE_EXTRACTOR_CLASS_MAP.update({ - 'lstm_ssd_mobilenet_v1': - lstm_ssd_mobilenet_v1_feature_extractor - .LSTMSSDMobileNetV1FeatureExtractor, - 'lstm_ssd_interleaved_mobilenet_v2': - lstm_ssd_interleaved_mobilenet_v2_feature_extractor - .LSTMSSDInterleavedMobilenetV2FeatureExtractor, -}) -SSD_FEATURE_EXTRACTOR_CLASS_MAP = model_builder.SSD_FEATURE_EXTRACTOR_CLASS_MAP - - -def build(model_config, lstm_config, is_training): - """Builds a DetectionModel based on the model config. - - Args: - model_config: A model.proto object containing the config for the desired - DetectionModel. - lstm_config: LstmModel config proto that specifies LSTM train/eval configs. - is_training: True if this model is being built for training purposes. - - Returns: - DetectionModel based on the config. - - Raises: - ValueError: On invalid meta architecture or model. - """ - return _build_lstm_model(model_config.ssd, lstm_config, is_training) - - -def _build_lstm_feature_extractor(feature_extractor_config, - is_training, - lstm_config, - reuse_weights=None): - """Builds a ssd_meta_arch.SSDFeatureExtractor based on config. - - Args: - feature_extractor_config: A SSDFeatureExtractor proto config from ssd.proto. - is_training: True if this feature extractor is being built for training. - lstm_config: LSTM-SSD specific configs. - reuse_weights: If the feature extractor should reuse weights. - - Returns: - ssd_meta_arch.SSDFeatureExtractor based on config. - - Raises: - ValueError: On invalid feature extractor type. - """ - - feature_type = feature_extractor_config.type - depth_multiplier = feature_extractor_config.depth_multiplier - min_depth = feature_extractor_config.min_depth - pad_to_multiple = feature_extractor_config.pad_to_multiple - use_explicit_padding = feature_extractor_config.use_explicit_padding - use_depthwise = feature_extractor_config.use_depthwise - conv_hyperparams = hyperparams_builder.build( - feature_extractor_config.conv_hyperparams, is_training) - override_base_feature_extractor_hyperparams = ( - feature_extractor_config.override_base_feature_extractor_hyperparams) - - if feature_type not in SSD_FEATURE_EXTRACTOR_CLASS_MAP: - raise ValueError('Unknown ssd feature_extractor: {}'.format(feature_type)) - - feature_extractor_class = SSD_FEATURE_EXTRACTOR_CLASS_MAP[feature_type] - feature_extractor = feature_extractor_class( - is_training, depth_multiplier, min_depth, pad_to_multiple, - conv_hyperparams, reuse_weights, use_explicit_padding, use_depthwise, - override_base_feature_extractor_hyperparams) - - # Extra configs for LSTM-SSD. - feature_extractor.lstm_state_depth = lstm_config.lstm_state_depth - feature_extractor.flatten_state = lstm_config.flatten_state - feature_extractor.clip_state = lstm_config.clip_state - feature_extractor.scale_state = lstm_config.scale_state - feature_extractor.is_quantized = lstm_config.is_quantized - feature_extractor.low_res = lstm_config.low_res - # Extra configs for interleaved LSTM-SSD. - if 'interleaved' in feature_extractor_config.type: - feature_extractor.pre_bottleneck = lstm_config.pre_bottleneck - feature_extractor.depth_multipliers = lstm_config.depth_multipliers - if is_training: - feature_extractor.interleave_method = lstm_config.train_interleave_method - else: - feature_extractor.interleave_method = lstm_config.eval_interleave_method - return feature_extractor - - -def _build_lstm_model(ssd_config, lstm_config, is_training): - """Builds an LSTM detection model based on the model config. - - Args: - ssd_config: A ssd.proto object containing the config for the desired - LSTMSSDMetaArch. - lstm_config: LstmModel config proto that specifies LSTM train/eval configs. - is_training: True if this model is being built for training purposes. - - Returns: - LSTMSSDMetaArch based on the config. - Raises: - ValueError: If ssd_config.type is not recognized (i.e. not registered in - model_class_map), or if lstm_config.interleave_strategy is not recognized. - ValueError: If unroll_length is not specified in the config file. - """ - feature_extractor = _build_lstm_feature_extractor( - ssd_config.feature_extractor, is_training, lstm_config) - - box_coder = box_coder_builder.build(ssd_config.box_coder) - matcher = matcher_builder.build(ssd_config.matcher) - region_similarity_calculator = sim_calc.build( - ssd_config.similarity_calculator) - - num_classes = ssd_config.num_classes - ssd_box_predictor = box_predictor_builder.build(hyperparams_builder.build, - ssd_config.box_predictor, - is_training, num_classes) - anchor_generator = anchor_generator_builder.build(ssd_config.anchor_generator) - image_resizer_fn = image_resizer_builder.build(ssd_config.image_resizer) - non_max_suppression_fn, score_conversion_fn = post_processing_builder.build( - ssd_config.post_processing) - (classification_loss, localization_loss, classification_weight, - localization_weight, miner, _, _) = losses_builder.build(ssd_config.loss) - - normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches - encode_background_as_zeros = ssd_config.encode_background_as_zeros - negative_class_weight = ssd_config.negative_class_weight - - # Extra configs for lstm unroll length. - unroll_length = None - if 'lstm' in ssd_config.feature_extractor.type: - if is_training: - unroll_length = lstm_config.train_unroll_length - else: - unroll_length = lstm_config.eval_unroll_length - if unroll_length is None: - raise ValueError('No unroll length found in the config file') - - target_assigner_instance = target_assigner.TargetAssigner( - region_similarity_calculator, - matcher, - box_coder, - negative_class_weight=negative_class_weight) - - lstm_model = lstm_ssd_meta_arch.LSTMSSDMetaArch( - is_training=is_training, - anchor_generator=anchor_generator, - box_predictor=ssd_box_predictor, - box_coder=box_coder, - feature_extractor=feature_extractor, - encode_background_as_zeros=encode_background_as_zeros, - image_resizer_fn=image_resizer_fn, - non_max_suppression_fn=non_max_suppression_fn, - score_conversion_fn=score_conversion_fn, - classification_loss=classification_loss, - localization_loss=localization_loss, - classification_loss_weight=classification_weight, - localization_loss_weight=localization_weight, - normalize_loss_by_num_matches=normalize_loss_by_num_matches, - hard_example_miner=miner, - unroll_length=unroll_length, - target_assigner_instance=target_assigner_instance) - - return lstm_model diff --git a/research/lstm_object_detection/model_builder_test.py b/research/lstm_object_detection/model_builder_test.py deleted file mode 100644 index 9d64b537cdc..00000000000 --- a/research/lstm_object_detection/model_builder_test.py +++ /dev/null @@ -1,302 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for lstm_object_detection.tensorflow.model_builder.""" - -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from lstm_object_detection import model_builder -from lstm_object_detection.meta_architectures import lstm_ssd_meta_arch -from lstm_object_detection.protos import pipeline_pb2 as internal_pipeline_pb2 -from object_detection.protos import pipeline_pb2 - - -class ModelBuilderTest(tf.test.TestCase): - - def create_train_model(self, model_config, lstm_config): - """Builds a DetectionModel based on the model config. - - Args: - model_config: A model.proto object containing the config for the desired - DetectionModel. - lstm_config: LstmModel config proto that specifies LSTM train/eval - configs. - - Returns: - DetectionModel based on the config. - """ - return model_builder.build(model_config, lstm_config, is_training=True) - - def create_eval_model(self, model_config, lstm_config): - """Builds a DetectionModel based on the model config. - - Args: - model_config: A model.proto object containing the config for the desired - DetectionModel. - lstm_config: LstmModel config proto that specifies LSTM train/eval - configs. - - Returns: - DetectionModel based on the config. - """ - return model_builder.build(model_config, lstm_config, is_training=False) - - def get_model_configs_from_proto(self): - """Creates a model text proto for testing. - - Returns: - A dictionary of model configs. - """ - - model_text_proto = """ - [lstm_object_detection.protos.lstm_model] { - train_unroll_length: 4 - eval_unroll_length: 4 - } - model { - ssd { - feature_extractor { - type: 'lstm_ssd_mobilenet_v1' - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - negative_class_weight: 2.0 - box_coder { - faster_rcnn_box_coder { - } - } - matcher { - argmax_matcher { - } - } - similarity_calculator { - iou_similarity { - } - } - anchor_generator { - ssd_anchor_generator { - aspect_ratios: 1.0 - } - } - image_resizer { - fixed_shape_resizer { - height: 320 - width: 320 - } - } - box_predictor { - convolutional_box_predictor { - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - } - normalize_loc_loss_by_codesize: true - loss { - classification_loss { - weighted_softmax { - } - } - localization_loss { - weighted_smooth_l1 { - } - } - } - } - }""" - - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - text_format.Merge(model_text_proto, pipeline_config) - - configs = {} - configs['model'] = pipeline_config.model - configs['lstm_model'] = pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model] - - return configs - - def get_interleaved_model_configs_from_proto(self): - """Creates an interleaved model text proto for testing. - - Returns: - A dictionary of model configs. - """ - - model_text_proto = """ - [lstm_object_detection.protos.lstm_model] { - train_unroll_length: 4 - eval_unroll_length: 10 - lstm_state_depth: 320 - depth_multipliers: 1.4 - depth_multipliers: 0.35 - pre_bottleneck: true - low_res: true - train_interleave_method: 'RANDOM_SKIP_SMALL' - eval_interleave_method: 'SKIP3' - } - model { - ssd { - feature_extractor { - type: 'lstm_ssd_interleaved_mobilenet_v2' - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - negative_class_weight: 2.0 - box_coder { - faster_rcnn_box_coder { - } - } - matcher { - argmax_matcher { - } - } - similarity_calculator { - iou_similarity { - } - } - anchor_generator { - ssd_anchor_generator { - aspect_ratios: 1.0 - } - } - image_resizer { - fixed_shape_resizer { - height: 320 - width: 320 - } - } - box_predictor { - convolutional_box_predictor { - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - } - normalize_loc_loss_by_codesize: true - loss { - classification_loss { - weighted_softmax { - } - } - localization_loss { - weighted_smooth_l1 { - } - } - } - } - }""" - - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - text_format.Merge(model_text_proto, pipeline_config) - - configs = {} - configs['model'] = pipeline_config.model - configs['lstm_model'] = pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model] - - return configs - - def test_model_creation_from_valid_configs(self): - configs = self.get_model_configs_from_proto() - # Test model properties. - self.assertEqual(configs['model'].ssd.negative_class_weight, 2.0) - self.assertTrue(configs['model'].ssd.normalize_loc_loss_by_codesize) - self.assertEqual(configs['model'].ssd.feature_extractor.type, - 'lstm_ssd_mobilenet_v1') - - model = self.create_train_model(configs['model'], configs['lstm_model']) - # Test architechture type. - self.assertIsInstance(model, lstm_ssd_meta_arch.LSTMSSDMetaArch) - # Test LSTM unroll length. - self.assertEqual(model.unroll_length, 4) - - model = self.create_eval_model(configs['model'], configs['lstm_model']) - # Test architechture type. - self.assertIsInstance(model, lstm_ssd_meta_arch.LSTMSSDMetaArch) - # Test LSTM configs. - self.assertEqual(model.unroll_length, 4) - - def test_interleaved_model_creation_from_valid_configs(self): - configs = self.get_interleaved_model_configs_from_proto() - # Test model properties. - self.assertEqual(configs['model'].ssd.negative_class_weight, 2.0) - self.assertTrue(configs['model'].ssd.normalize_loc_loss_by_codesize) - self.assertEqual(configs['model'].ssd.feature_extractor.type, - 'lstm_ssd_interleaved_mobilenet_v2') - - model = self.create_train_model(configs['model'], configs['lstm_model']) - # Test architechture type. - self.assertIsInstance(model, lstm_ssd_meta_arch.LSTMSSDMetaArch) - # Test LSTM configs. - self.assertEqual(model.unroll_length, 4) - self.assertEqual(model._feature_extractor.lstm_state_depth, 320) - self.assertAllClose(model._feature_extractor.depth_multipliers, (1.4, 0.35)) - self.assertTrue(model._feature_extractor.pre_bottleneck) - self.assertTrue(model._feature_extractor.low_res) - self.assertEqual(model._feature_extractor.interleave_method, - 'RANDOM_SKIP_SMALL') - - model = self.create_eval_model(configs['model'], configs['lstm_model']) - # Test architechture type. - self.assertIsInstance(model, lstm_ssd_meta_arch.LSTMSSDMetaArch) - # Test LSTM configs. - self.assertEqual(model.unroll_length, 10) - self.assertEqual(model._feature_extractor.lstm_state_depth, 320) - self.assertAllClose(model._feature_extractor.depth_multipliers, (1.4, 0.35)) - self.assertTrue(model._feature_extractor.pre_bottleneck) - self.assertTrue(model._feature_extractor.low_res) - self.assertEqual(model._feature_extractor.interleave_method, 'SKIP3') - - def test_model_creation_from_invalid_configs(self): - configs = self.get_model_configs_from_proto() - # Test model build failure with wrong input configs. - with self.assertRaises(AttributeError): - _ = self.create_train_model(configs['model'], configs['model']) - with self.assertRaises(AttributeError): - _ = self.create_eval_model(configs['model'], configs['model']) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/models/__init__.py b/research/lstm_object_detection/models/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor.py b/research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor.py deleted file mode 100644 index 5a2d4bd0bdc..00000000000 --- a/research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor.py +++ /dev/null @@ -1,298 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""LSTDInterleavedFeatureExtractor which interleaves multiple MobileNet V2.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from tensorflow.python.framework import ops as tf_ops -from lstm_object_detection.lstm import lstm_cells -from lstm_object_detection.lstm import rnn_decoder -from lstm_object_detection.meta_architectures import lstm_ssd_meta_arch -from lstm_object_detection.models import mobilenet_defs -from object_detection.models import feature_map_generators -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets.mobilenet import mobilenet -from nets.mobilenet import mobilenet_v2 - - -class LSTMSSDInterleavedMobilenetV2FeatureExtractor( - lstm_ssd_meta_arch.LSTMSSDInterleavedFeatureExtractor): - """LSTM-SSD Interleaved Feature Extractor using MobilenetV2 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=True, - override_base_feature_extractor_hyperparams=False): - """Interleaved Feature Extractor for LSTD Models with MobileNet v2. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is True. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(LSTMSSDInterleavedMobilenetV2FeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - # RANDOM_SKIP_SMALL means the training policy is random and the small model - # does not update state during training. - if self._is_training: - self._interleave_method = 'RANDOM_SKIP_SMALL' - else: - self._interleave_method = 'SKIP9' - - self._flatten_state = False - self._scale_state = False - self._clip_state = True - self._pre_bottleneck = True - self._feature_map_layout = { - 'from_layer': ['layer_19', '', '', '', ''], - 'layer_depth': [-1, 256, 256, 256, 256], - 'use_depthwise': self._use_depthwise, - 'use_explicit_padding': self._use_explicit_padding, - } - self._low_res = True - self._base_network_scope = 'MobilenetV2' - - def extract_base_features_large(self, preprocessed_inputs): - """Extract the large base model features. - - Variables are created under the scope of /MobilenetV2_1/ - - Args: - preprocessed_inputs: preprocessed input images of shape: - [batch, width, height, depth]. - - Returns: - net: the last feature map created from the base feature extractor. - end_points: a dictionary of feature maps created. - """ - scope_name = self._base_network_scope + '_1' - with tf.variable_scope(scope_name, reuse=self._reuse_weights) as base_scope: - net, end_points = mobilenet_v2.mobilenet_base( - preprocessed_inputs, - depth_multiplier=self._depth_multipliers[0], - conv_defs=mobilenet_defs.mobilenet_v2_lite_def( - is_quantized=self._is_quantized), - use_explicit_padding=self._use_explicit_padding, - scope=base_scope) - return net, end_points - - def extract_base_features_small(self, preprocessed_inputs): - """Extract the small base model features. - - Variables are created under the scope of /MobilenetV2_2/ - - Args: - preprocessed_inputs: preprocessed input images of shape: - [batch, width, height, depth]. - - Returns: - net: the last feature map created from the base feature extractor. - end_points: a dictionary of feature maps created. - """ - scope_name = self._base_network_scope + '_2' - with tf.variable_scope(scope_name, reuse=self._reuse_weights) as base_scope: - if self._low_res: - height_small = preprocessed_inputs.get_shape().as_list()[1] // 2 - width_small = preprocessed_inputs.get_shape().as_list()[2] // 2 - inputs_small = tf.image.resize_images(preprocessed_inputs, - [height_small, width_small]) - # Create end point handle for tflite deployment. - with tf.name_scope(None): - inputs_small = tf.identity( - inputs_small, name='normalized_input_image_tensor_small') - else: - inputs_small = preprocessed_inputs - net, end_points = mobilenet_v2.mobilenet_base( - inputs_small, - depth_multiplier=self._depth_multipliers[1], - conv_defs=mobilenet_defs.mobilenet_v2_lite_def( - is_quantized=self._is_quantized, low_res=self._low_res), - use_explicit_padding=self._use_explicit_padding, - scope=base_scope) - return net, end_points - - def create_lstm_cell(self, batch_size, output_size, state_saver, state_name, - dtype=tf.float32): - """Create the LSTM cell, and initialize state if necessary. - - Args: - batch_size: input batch size. - output_size: output size of the lstm cell, [width, height]. - state_saver: a state saver object with methods `state` and `save_state`. - state_name: string, the name to use with the state_saver. - dtype: dtype to initialize lstm state. - - Returns: - lstm_cell: the lstm cell unit. - init_state: initial state representations. - step: the step - """ - lstm_cell = lstm_cells.GroupedConvLSTMCell( - filter_size=(3, 3), - output_size=output_size, - num_units=max(self._min_depth, self._lstm_state_depth), - is_training=self._is_training, - activation=tf.nn.relu6, - flatten_state=self._flatten_state, - scale_state=self._scale_state, - clip_state=self._clip_state, - output_bottleneck=True, - pre_bottleneck=self._pre_bottleneck, - is_quantized=self._is_quantized, - visualize_gates=False) - - if state_saver is None: - init_state = lstm_cell.init_state('lstm_state', batch_size, dtype) - step = None - else: - step = state_saver.state(state_name + '_step') - c = state_saver.state(state_name + '_c') - h = state_saver.state(state_name + '_h') - c.set_shape([batch_size] + c.get_shape().as_list()[1:]) - h.set_shape([batch_size] + h.get_shape().as_list()[1:]) - init_state = (c, h) - return lstm_cell, init_state, step - - def extract_features(self, preprocessed_inputs, state_saver=None, - state_name='lstm_state', unroll_length=10, scope=None): - """Extract features from preprocessed inputs. - - The features include the base network features, lstm features and SSD - features, organized in the following name scope: - - /MobilenetV2_1/... - /MobilenetV2_2/... - /LSTM/... - /FeatureMap/... - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of consecutive frames from video clips. - state_saver: A state saver object with methods `state` and `save_state`. - state_name: Python string, the name to use with the state_saver. - unroll_length: number of steps to unroll the lstm. - scope: Scope for the base network of the feature extractor. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - Raises: - ValueError: if interleave_method not recognized or large and small base - network output feature maps of different sizes. - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - preprocessed_inputs = ops.pad_to_multiple( - preprocessed_inputs, self._pad_to_multiple) - batch_size = preprocessed_inputs.shape[0].value // unroll_length - batch_axis = 0 - nets = [] - - # Batch processing of mobilenet features. - with slim.arg_scope(mobilenet_v2.training_scope( - is_training=self._is_training, - bn_decay=0.9997)), \ - slim.arg_scope([mobilenet.depth_multiplier], - min_depth=self._min_depth, divisible_by=8): - # Big model. - net, _ = self.extract_base_features_large(preprocessed_inputs) - nets.append(net) - large_base_feature_shape = net.shape - - # Small models - net, _ = self.extract_base_features_small(preprocessed_inputs) - nets.append(net) - small_base_feature_shape = net.shape - if not (large_base_feature_shape[1] == small_base_feature_shape[1] and - large_base_feature_shape[2] == small_base_feature_shape[2]): - raise ValueError('Large and Small base network feature map dimension ' - 'not equal!') - - with slim.arg_scope(self._conv_hyperparams_fn()): - with tf.variable_scope('LSTM', reuse=self._reuse_weights): - output_size = (large_base_feature_shape[1], large_base_feature_shape[2]) - lstm_cell, init_state, step = self.create_lstm_cell( - batch_size, output_size, state_saver, state_name, - dtype=preprocessed_inputs.dtype) - - nets_seq = [ - tf.split(net, unroll_length, axis=batch_axis) for net in nets - ] - - net_seq, states_out = rnn_decoder.multi_input_rnn_decoder( - nets_seq, - init_state, - lstm_cell, - step, - selection_strategy=self._interleave_method, - is_training=self._is_training, - is_quantized=self._is_quantized, - pre_bottleneck=self._pre_bottleneck, - flatten_state=self._flatten_state, - scope=None) - self._states_out = states_out - - image_features = {} - if state_saver is not None: - self._step = state_saver.state(state_name + '_step') - batcher_ops = [ - state_saver.save_state(state_name + '_c', states_out[-1][0]), - state_saver.save_state(state_name + '_h', states_out[-1][1]), - state_saver.save_state(state_name + '_step', self._step + 1)] - with tf_ops.control_dependencies(batcher_ops): - image_features['layer_19'] = tf.concat(net_seq, 0) - else: - image_features['layer_19'] = tf.concat(net_seq, 0) - - # SSD layers. - with tf.variable_scope('FeatureMap'): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=self._feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features, - pool_residual=True) - return list(feature_maps.values()) diff --git a/research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor_test.py b/research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor_test.py deleted file mode 100644 index b285f0e4441..00000000000 --- a/research/lstm_object_detection/models/lstm_ssd_interleaved_mobilenet_v2_feature_extractor_test.py +++ /dev/null @@ -1,352 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for lstm_ssd_interleaved_mobilenet_v2_feature_extractor.""" - -import numpy as np -import tensorflow.compat.v1 as tf -import tf_slim as slim -from tensorflow.contrib import training as contrib_training - -from lstm_object_detection.models import lstm_ssd_interleaved_mobilenet_v2_feature_extractor -from object_detection.models import ssd_feature_extractor_test - - -class LSTMSSDInterleavedMobilenetV2FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - is_quantized=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - is_quantized: whether to quantize the graph. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - def conv_hyperparams_fn(): - with slim.arg_scope([slim.conv2d], normalizer_fn=slim.batch_norm), \ - slim.arg_scope([slim.batch_norm], is_training=False) as sc: - return sc - feature_extractor = ( - lstm_ssd_interleaved_mobilenet_v2_feature_extractor - .LSTMSSDInterleavedMobilenetV2FeatureExtractor(False, depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn)) - feature_extractor.lstm_state_depth = int(320 * depth_multiplier) - feature_extractor.depth_multipliers = [ - depth_multiplier, depth_multiplier / 4.0 - ] - feature_extractor.is_quantized = is_quantized - return feature_extractor - - def test_feature_extractor_construct_with_expected_params(self): - def conv_hyperparams_fn(): - with (slim.arg_scope([slim.conv2d], normalizer_fn=slim.batch_norm) and - slim.arg_scope([slim.batch_norm], decay=0.97, epsilon=1e-3)) as sc: - return sc - - params = { - 'is_training': True, - 'depth_multiplier': .55, - 'min_depth': 9, - 'pad_to_multiple': 3, - 'conv_hyperparams_fn': conv_hyperparams_fn, - 'reuse_weights': False, - 'use_explicit_padding': True, - 'use_depthwise': False, - 'override_base_feature_extractor_hyperparams': True} - - feature_extractor = ( - lstm_ssd_interleaved_mobilenet_v2_feature_extractor - .LSTMSSDInterleavedMobilenetV2FeatureExtractor(**params)) - - self.assertEqual(params['is_training'], - feature_extractor._is_training) - self.assertEqual(params['depth_multiplier'], - feature_extractor._depth_multiplier) - self.assertEqual(params['min_depth'], - feature_extractor._min_depth) - self.assertEqual(params['pad_to_multiple'], - feature_extractor._pad_to_multiple) - self.assertEqual(params['conv_hyperparams_fn'], - feature_extractor._conv_hyperparams_fn) - self.assertEqual(params['reuse_weights'], - feature_extractor._reuse_weights) - self.assertEqual(params['use_explicit_padding'], - feature_extractor._use_explicit_padding) - self.assertEqual(params['use_depthwise'], - feature_extractor._use_depthwise) - self.assertEqual(params['override_base_feature_extractor_hyperparams'], - (feature_extractor. - _override_base_feature_extractor_hyperparams)) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 4, 4, 640), - (2, 2, 2, 256), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_unroll10(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(10, 4, 4, 640), - (10, 2, 2, 256), (10, 1, 1, 256), - (10, 1, 1, 256), (10, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 10, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, unroll_length=10) - - def test_extract_features_returns_correct_shapes_320(self): - image_height = 320 - image_width = 320 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 10, 10, 640), - (2, 5, 5, 256), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth(self): - image_height = 320 - image_width = 320 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 10, 10, 64), - (2, 5, 5, 32), (2, 3, 3, 32), - (2, 2, 2, 32), (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 10, 10, 640), - (2, 5, 5, 256), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_names = ['MobilenetV2', 'LSTM', 'FeatureMap'] - self.check_feature_extractor_variables_under_scopes( - depth_multiplier, pad_to_multiple, scope_names) - - def test_has_fused_batchnorm(self): - image_height = 40 - image_width = 40 - depth_multiplier = 1 - pad_to_multiple = 32 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image, unroll_length=1) - self.assertTrue(any(op.type.startswith('FusedBatchNorm') - for op in tf.get_default_graph().get_operations())) - - def test_variables_for_tflite(self): - image_height = 40 - image_width = 40 - depth_multiplier = 1 - pad_to_multiple = 32 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - tflite_unsupported = ['SquaredDifference'] - _ = feature_extractor.extract_features(preprocessed_image, unroll_length=1) - self.assertFalse(any(op.type in tflite_unsupported - for op in tf.get_default_graph().get_operations())) - - def test_output_nodes_for_tflite(self): - image_height = 64 - image_width = 64 - depth_multiplier = 1.0 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image, unroll_length=1) - - tflite_nodes = [ - 'raw_inputs/init_lstm_c', - 'raw_inputs/init_lstm_h', - 'raw_inputs/base_endpoint', - 'raw_outputs/lstm_c', - 'raw_outputs/lstm_h', - 'raw_outputs/base_endpoint_1', - 'raw_outputs/base_endpoint_2' - ] - ops_names = [op.name for op in tf.get_default_graph().get_operations()] - for node in tflite_nodes: - self.assertTrue(any(node in s for s in ops_names)) - - def test_fixed_concat_nodes(self): - image_height = 64 - image_width = 64 - depth_multiplier = 1.0 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, is_quantized=True) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image, unroll_length=1) - - concat_nodes = [ - 'MobilenetV2_1/expanded_conv_16/project/Relu6', - 'MobilenetV2_2/expanded_conv_16/project/Relu6' - ] - ops_names = [op.name for op in tf.get_default_graph().get_operations()] - for node in concat_nodes: - self.assertTrue(any(node in s for s in ops_names)) - - def test_lstm_states(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - state_channel = 320 - init_state1 = { - 'lstm_state_c': tf.zeros( - [image_height // 32, image_width // 32, state_channel]), - 'lstm_state_h': tf.zeros( - [image_height // 32, image_width // 32, state_channel]), - 'lstm_state_step': tf.zeros([1]) - } - init_state2 = { - 'lstm_state_c': tf.random_uniform( - [image_height // 32, image_width // 32, state_channel]), - 'lstm_state_h': tf.random_uniform( - [image_height // 32, image_width // 32, state_channel]), - 'lstm_state_step': tf.zeros([1]) - } - seq = {'dummy': tf.random_uniform([2, 1, 1, 1])} - stateful_reader1 = contrib_training.SequenceQueueingStateSaver( - batch_size=1, - num_unroll=1, - input_length=2, - input_key='', - input_sequences=seq, - input_context={}, - initial_states=init_state1, - capacity=1) - stateful_reader2 = contrib_training.SequenceQueueingStateSaver( - batch_size=1, - num_unroll=1, - input_length=2, - input_key='', - input_sequences=seq, - input_context={}, - initial_states=init_state2, - capacity=1) - image = tf.random_uniform([1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - with tf.variable_scope('zero_state'): - feature_maps1 = feature_extractor.extract_features( - image, stateful_reader1.next_batch, unroll_length=1) - with tf.variable_scope('random_state'): - feature_maps2 = feature_extractor.extract_features( - image, stateful_reader2.next_batch, unroll_length=1) - with tf.Session() as sess: - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - sess.run(tf.get_collection(tf.GraphKeys.TABLE_INITIALIZERS)) - sess.run([stateful_reader1.prefetch_op, stateful_reader2.prefetch_op]) - maps1, maps2 = sess.run([feature_maps1, feature_maps2]) - state = sess.run(stateful_reader1.next_batch.state('lstm_state_c')) - # feature maps should be different because states are different - self.assertFalse(np.all(np.equal(maps1[0], maps2[0]))) - # state should no longer be zero after update - self.assertTrue(state.any()) - - def check_extract_features_returns_correct_shape( - self, batch_size, image_height, image_width, depth_multiplier, - pad_to_multiple, expected_feature_map_shapes, unroll_length=1): - def graph_fn(image_tensor): - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - feature_maps = feature_extractor.extract_features( - image_tensor, unroll_length=unroll_length) - return feature_maps - - image_tensor = np.random.rand(batch_size, image_height, image_width, - 3).astype(np.float32) - feature_maps = self.execute(graph_fn, [image_tensor]) - for feature_map, expected_shape in zip( - feature_maps, expected_feature_map_shapes): - self.assertAllEqual(feature_map.shape, expected_shape) - - def check_feature_extractor_variables_under_scopes( - self, depth_multiplier, pad_to_multiple, scope_names): - g = tf.Graph() - with g.as_default(): - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple) - preprocessed_inputs = tf.placeholder(tf.float32, (4, 320, 320, 3)) - feature_extractor.extract_features( - preprocessed_inputs, unroll_length=1) - variables = g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - for variable in variables: - self.assertTrue( - any([ - variable.name.startswith(scope_name) - for scope_name in scope_names - ]), 'Variable name: ' + variable.name + - ' is not under any provided scopes: ' + ','.join(scope_names)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor.py b/research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor.py deleted file mode 100644 index cccf740aadd..00000000000 --- a/research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor.py +++ /dev/null @@ -1,211 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""LSTMSSDFeatureExtractor for MobilenetV1 features.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim -from tensorflow.python.framework import ops as tf_ops -from lstm_object_detection.lstm import lstm_cells -from lstm_object_detection.lstm import rnn_decoder -from lstm_object_detection.meta_architectures import lstm_ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import mobilenet_v1 - - -class LSTMSSDMobileNetV1FeatureExtractor( - lstm_ssd_meta_arch.LSTMSSDFeatureExtractor): - """LSTM Feature Extractor using MobilenetV1 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=True, - override_base_feature_extractor_hyperparams=False, - lstm_state_depth=256): - """Initializes instance of MobileNetV1 Feature Extractor for LSTMSSD Models. - - Args: - is_training: A boolean whether the network is in training mode. - depth_multiplier: A float depth multiplier for feature extractor. - min_depth: A number representing minimum feature extractor depth. - pad_to_multiple: The nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is True. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - lstm_state_depth: An integter of the depth of the lstm state. - """ - super(LSTMSSDMobileNetV1FeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - self._feature_map_layout = { - 'from_layer': ['Conv2d_13_pointwise_lstm', '', '', '', ''], - 'layer_depth': [-1, 512, 256, 256, 128], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - self._base_network_scope = 'MobilenetV1' - self._lstm_state_depth = lstm_state_depth - - def create_lstm_cell(self, batch_size, output_size, state_saver, state_name, - dtype=tf.float32): - """Create the LSTM cell, and initialize state if necessary. - - Args: - batch_size: input batch size. - output_size: output size of the lstm cell, [width, height]. - state_saver: a state saver object with methods `state` and `save_state`. - state_name: string, the name to use with the state_saver. - dtype: dtype to initialize lstm state. - - Returns: - lstm_cell: the lstm cell unit. - init_state: initial state representations. - step: the step - """ - lstm_cell = lstm_cells.BottleneckConvLSTMCell( - filter_size=(3, 3), - output_size=output_size, - num_units=max(self._min_depth, self._lstm_state_depth), - activation=tf.nn.relu6, - visualize_gates=False) - - if state_saver is None: - init_state = lstm_cell.init_state(state_name, batch_size, dtype) - step = None - else: - step = state_saver.state(state_name + '_step') - c = state_saver.state(state_name + '_c') - h = state_saver.state(state_name + '_h') - init_state = (c, h) - return lstm_cell, init_state, step - - def extract_features(self, - preprocessed_inputs, - state_saver=None, - state_name='lstm_state', - unroll_length=5, - scope=None): - """Extracts features from preprocessed inputs. - - The features include the base network features, lstm features and SSD - features, organized in the following name scope: - - /MobilenetV1/... - /LSTM/... - /FeatureMaps/... - - Args: - preprocessed_inputs: A [batch, height, width, channels] float tensor - representing a batch of consecutive frames from video clips. - state_saver: A state saver object with methods `state` and `save_state`. - state_name: A python string for the name to use with the state_saver. - unroll_length: The number of steps to unroll the lstm. - scope: The scope for the base network of the feature extractor. - - Returns: - A list of tensors where the ith tensor has shape [batch, height_i, - width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - with slim.arg_scope( - mobilenet_v1.mobilenet_v1_arg_scope(is_training=self._is_training)): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams else - context_manager.IdentityContextManager()): - with slim.arg_scope([slim.batch_norm], fused=False): - # Base network. - with tf.variable_scope( - scope, self._base_network_scope, - reuse=self._reuse_weights) as scope: - net, image_features = mobilenet_v1.mobilenet_v1_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='Conv2d_13_pointwise', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - scope=scope) - - with slim.arg_scope(self._conv_hyperparams_fn()): - with slim.arg_scope( - [slim.batch_norm], fused=False, is_training=self._is_training): - # ConvLSTM layers. - batch_size = net.shape[0].value // unroll_length - with tf.variable_scope('LSTM', reuse=self._reuse_weights) as lstm_scope: - lstm_cell, init_state, _ = self.create_lstm_cell( - batch_size, - (net.shape[1].value, net.shape[2].value), - state_saver, - state_name, - dtype=preprocessed_inputs.dtype) - net_seq = list(tf.split(net, unroll_length)) - - # Identities added for inputing state tensors externally. - c_ident = tf.identity(init_state[0], name='lstm_state_in_c') - h_ident = tf.identity(init_state[1], name='lstm_state_in_h') - init_state = (c_ident, h_ident) - - net_seq, states_out = rnn_decoder.rnn_decoder( - net_seq, init_state, lstm_cell, scope=lstm_scope) - batcher_ops = None - self._states_out = states_out - if state_saver is not None: - self._step = state_saver.state('%s_step' % state_name) - batcher_ops = [ - state_saver.save_state('%s_c' % state_name, states_out[-1][0]), - state_saver.save_state('%s_h' % state_name, states_out[-1][1]), - state_saver.save_state('%s_step' % state_name, self._step + 1) - ] - with tf_ops.control_dependencies(batcher_ops): - image_features['Conv2d_13_pointwise_lstm'] = tf.concat(net_seq, 0) - - # Identities added for reading output states, to be reused externally. - tf.identity(states_out[-1][0], name='lstm_state_out_c') - tf.identity(states_out[-1][1], name='lstm_state_out_h') - - # SSD layers. - with tf.variable_scope('FeatureMaps', reuse=self._reuse_weights): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=self._feature_map_layout, - depth_multiplier=(self._depth_multiplier), - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) diff --git a/research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor_test.py b/research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor_test.py deleted file mode 100644 index 56ad2745dae..00000000000 --- a/research/lstm_object_detection/models/lstm_ssd_mobilenet_v1_feature_extractor_test.py +++ /dev/null @@ -1,179 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for models.lstm_ssd_mobilenet_v1_feature_extractor.""" - -import numpy as np -import tensorflow.compat.v1 as tf -import tf_slim as slim -from tensorflow.contrib import training as contrib_training - -from lstm_object_detection.models import lstm_ssd_mobilenet_v1_feature_extractor as feature_extractor -from object_detection.models import ssd_feature_extractor_test - - -class LstmSsdMobilenetV1FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier=1.0, - pad_to_multiple=1, - is_training=True, - use_explicit_padding=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: A float depth multiplier for feature extractor. - pad_to_multiple: The nearest multiple to zero pad the input height and - width dimensions to. - is_training: A boolean whether the network is in training mode. - use_explicit_padding: A boolean whether to use explicit padding. - - Returns: - An lstm_ssd_meta_arch.LSTMSSDMobileNetV1FeatureExtractor object. - """ - min_depth = 32 - extractor = ( - feature_extractor.LSTMSSDMobileNetV1FeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - extractor.lstm_state_depth = int(256 * depth_multiplier) - return extractor - - def test_feature_extractor_construct_with_expected_params(self): - def conv_hyperparams_fn(): - with (slim.arg_scope([slim.conv2d], normalizer_fn=slim.batch_norm) and - slim.arg_scope([slim.batch_norm], decay=0.97, epsilon=1e-3)) as sc: - return sc - - params = { - 'is_training': True, - 'depth_multiplier': .55, - 'min_depth': 9, - 'pad_to_multiple': 3, - 'conv_hyperparams_fn': conv_hyperparams_fn, - 'reuse_weights': False, - 'use_explicit_padding': True, - 'use_depthwise': False, - 'override_base_feature_extractor_hyperparams': True} - - extractor = ( - feature_extractor.LSTMSSDMobileNetV1FeatureExtractor(**params)) - - self.assertEqual(params['is_training'], - extractor._is_training) - self.assertEqual(params['depth_multiplier'], - extractor._depth_multiplier) - self.assertEqual(params['min_depth'], - extractor._min_depth) - self.assertEqual(params['pad_to_multiple'], - extractor._pad_to_multiple) - self.assertEqual(params['conv_hyperparams_fn'], - extractor._conv_hyperparams_fn) - self.assertEqual(params['reuse_weights'], - extractor._reuse_weights) - self.assertEqual(params['use_explicit_padding'], - extractor._use_explicit_padding) - self.assertEqual(params['use_depthwise'], - extractor._use_depthwise) - self.assertEqual(params['override_base_feature_extractor_hyperparams'], - (extractor. - _override_base_feature_extractor_hyperparams)) - - def test_extract_features_returns_correct_shapes_256(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - batch_size = 5 - expected_feature_map_shape = [(batch_size, 8, 8, 256), (batch_size, 4, 4, - 512), - (batch_size, 2, 2, 256), (batch_size, 1, 1, - 256)] - self.check_extract_features_returns_correct_shape( - batch_size, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - batch_size, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True) - - def test_preprocess_returns_correct_value_range(self): - test_image = np.random.rand(5, 128, 128, 3) - extractor = self._create_feature_extractor() - preprocessed_image = extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - scope_name = 'MobilenetV1' - g = tf.Graph() - with g.as_default(): - preprocessed_inputs = tf.placeholder(tf.float32, (5, 256, 256, 3)) - extractor = self._create_feature_extractor() - extractor.extract_features(preprocessed_inputs) - variables = g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - find_scope = False - for variable in variables: - if scope_name in variable.name: - find_scope = True - break - self.assertTrue(find_scope) - - def test_lstm_non_zero_state(self): - init_state = { - 'lstm_state_c': tf.zeros([8, 8, 256]), - 'lstm_state_h': tf.zeros([8, 8, 256]), - 'lstm_state_step': tf.zeros([1]) - } - seq = {'test': tf.random_uniform([3, 1, 1, 1])} - stateful_reader = contrib_training.SequenceQueueingStateSaver( - batch_size=1, - num_unroll=1, - input_length=2, - input_key='', - input_sequences=seq, - input_context={}, - initial_states=init_state, - capacity=1) - extractor = self._create_feature_extractor() - image = tf.random_uniform([5, 256, 256, 3]) - with tf.variable_scope('zero_state'): - feature_map = extractor.extract_features( - image, stateful_reader.next_batch) - with tf.Session() as sess: - sess.run(tf.global_variables_initializer()) - sess.run([stateful_reader.prefetch_op]) - _ = sess.run([feature_map]) - # Update states with the next batch. - state = sess.run(stateful_reader.next_batch.state('lstm_state_c')) - # State should no longer be zero after update. - self.assertTrue(state.any()) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/models/mobilenet_defs.py b/research/lstm_object_detection/models/mobilenet_defs.py deleted file mode 100644 index 4f984240215..00000000000 --- a/research/lstm_object_detection/models/mobilenet_defs.py +++ /dev/null @@ -1,142 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Definitions for modified MobileNet models used in LSTD.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim -from nets import mobilenet_v1 -from nets.mobilenet import conv_blocks as mobilenet_convs -from nets.mobilenet import mobilenet - - -def mobilenet_v1_lite_def(depth_multiplier, low_res=False): - """Conv definitions for a lite MobileNet v1 model. - - Args: - depth_multiplier: float depth multiplier for MobileNet. - low_res: An option of low-res conv input for interleave model. - - Returns: - Array of convolutions. - - Raises: - ValueError: On invalid channels with provided depth multiplier. - """ - conv = mobilenet_v1.Conv - sep_conv = mobilenet_v1.DepthSepConv - - def _find_target_depth(original, depth_multiplier): - # Find the target depth such that: - # int(target * depth_multiplier) == original - pseudo_target = int(original / depth_multiplier) - for target in range(pseudo_target - 1, pseudo_target + 2): - if int(target * depth_multiplier) == original: - return target - raise ValueError('Cannot have %d channels with depth multiplier %0.2f' % - (original, depth_multiplier)) - - return [ - conv(kernel=[3, 3], stride=2, depth=32), - sep_conv(kernel=[3, 3], stride=1, depth=64), - sep_conv(kernel=[3, 3], stride=2, depth=128), - sep_conv(kernel=[3, 3], stride=1, depth=128), - sep_conv(kernel=[3, 3], stride=2, depth=256), - sep_conv(kernel=[3, 3], stride=1, depth=256), - sep_conv(kernel=[3, 3], stride=2, depth=512), - sep_conv(kernel=[3, 3], stride=1, depth=512), - sep_conv(kernel=[3, 3], stride=1, depth=512), - sep_conv(kernel=[3, 3], stride=1, depth=512), - sep_conv(kernel=[3, 3], stride=1, depth=512), - sep_conv(kernel=[3, 3], stride=1, depth=512), - sep_conv(kernel=[3, 3], stride=1 if low_res else 2, depth=1024), - sep_conv( - kernel=[3, 3], - stride=1, - depth=int(_find_target_depth(1024, depth_multiplier))) - ] - - -def mobilenet_v2_lite_def(reduced=False, is_quantized=False, low_res=False): - """Conv definitions for a lite MobileNet v2 model. - - Args: - reduced: Determines the scaling factor for expanded conv. If True, a factor - of 6 is used. If False, a factor of 3 is used. - is_quantized: Whether the model is trained in quantized mode. - low_res: Whether the input to the model is of half resolution. - - Returns: - Array of convolutions. - """ - expanded_conv = mobilenet_convs.expanded_conv - expand_input = mobilenet_convs.expand_input_by_factor - op = mobilenet.op - return dict( - defaults={ - # Note: these parameters of batch norm affect the architecture - # that's why they are here and not in training_scope. - (slim.batch_norm,): { - 'center': True, - 'scale': True - }, - (slim.conv2d, slim.fully_connected, slim.separable_conv2d): { - 'normalizer_fn': slim.batch_norm, - 'activation_fn': tf.nn.relu6 - }, - (expanded_conv,): { - 'expansion_size': expand_input(6), - 'split_expansion': 1, - 'normalizer_fn': slim.batch_norm, - 'residual': True - }, - (slim.conv2d, slim.separable_conv2d): { - 'padding': 'SAME' - } - }, - spec=[ - op(slim.conv2d, stride=2, num_outputs=32, kernel_size=[3, 3]), - op(expanded_conv, - expansion_size=expand_input(1, divisible_by=1), - num_outputs=16), - op(expanded_conv, - expansion_size=(expand_input(3, divisible_by=1) - if reduced else expand_input(6)), - stride=2, - num_outputs=24), - op(expanded_conv, - expansion_size=(expand_input(3, divisible_by=1) - if reduced else expand_input(6)), - stride=1, - num_outputs=24), - op(expanded_conv, stride=2, num_outputs=32), - op(expanded_conv, stride=1, num_outputs=32), - op(expanded_conv, stride=1, num_outputs=32), - op(expanded_conv, stride=2, num_outputs=64), - op(expanded_conv, stride=1, num_outputs=64), - op(expanded_conv, stride=1, num_outputs=64), - op(expanded_conv, stride=1, num_outputs=64), - op(expanded_conv, stride=1, num_outputs=96), - op(expanded_conv, stride=1, num_outputs=96), - op(expanded_conv, stride=1, num_outputs=96), - op(expanded_conv, stride=1 if low_res else 2, num_outputs=160), - op(expanded_conv, stride=1, num_outputs=160), - op(expanded_conv, stride=1, num_outputs=160), - op(expanded_conv, - stride=1, - num_outputs=320, - project_activation_fn=(tf.nn.relu6 - if is_quantized else tf.identity)) - ], - ) diff --git a/research/lstm_object_detection/models/mobilenet_defs_test.py b/research/lstm_object_detection/models/mobilenet_defs_test.py deleted file mode 100644 index f1b5bda504b..00000000000 --- a/research/lstm_object_detection/models/mobilenet_defs_test.py +++ /dev/null @@ -1,136 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for lstm_object_detection.models.mobilenet_defs.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf -from lstm_object_detection.models import mobilenet_defs -from nets import mobilenet_v1 -from nets.mobilenet import mobilenet_v2 - - -class MobilenetV1DefsTest(tf.test.TestCase): - - def test_mobilenet_v1_lite_def(self): - net, _ = mobilenet_v1.mobilenet_v1_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - final_endpoint='Conv2d_13_pointwise', - min_depth=8, - depth_multiplier=1.0, - conv_defs=mobilenet_defs.mobilenet_v1_lite_def(1.0), - use_explicit_padding=True, - scope='MobilenetV1') - self.assertEqual(net.get_shape().as_list(), [10, 10, 10, 1024]) - - def test_mobilenet_v1_lite_def_depthmultiplier_half(self): - net, _ = mobilenet_v1.mobilenet_v1_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - final_endpoint='Conv2d_13_pointwise', - min_depth=8, - depth_multiplier=0.5, - conv_defs=mobilenet_defs.mobilenet_v1_lite_def(0.5), - use_explicit_padding=True, - scope='MobilenetV1') - self.assertEqual(net.get_shape().as_list(), [10, 10, 10, 1024]) - - def test_mobilenet_v1_lite_def_depthmultiplier_2x(self): - net, _ = mobilenet_v1.mobilenet_v1_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - final_endpoint='Conv2d_13_pointwise', - min_depth=8, - depth_multiplier=2.0, - conv_defs=mobilenet_defs.mobilenet_v1_lite_def(2.0), - use_explicit_padding=True, - scope='MobilenetV1') - self.assertEqual(net.get_shape().as_list(), [10, 10, 10, 1024]) - - def test_mobilenet_v1_lite_def_low_res(self): - net, _ = mobilenet_v1.mobilenet_v1_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - final_endpoint='Conv2d_13_pointwise', - min_depth=8, - depth_multiplier=1.0, - conv_defs=mobilenet_defs.mobilenet_v1_lite_def(1.0, low_res=True), - use_explicit_padding=True, - scope='MobilenetV1') - self.assertEqual(net.get_shape().as_list(), [10, 20, 20, 1024]) - - -class MobilenetV2DefsTest(tf.test.TestCase): - - def test_mobilenet_v2_lite_def(self): - net, features = mobilenet_v2.mobilenet_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - min_depth=8, - depth_multiplier=1.0, - conv_defs=mobilenet_defs.mobilenet_v2_lite_def(), - use_explicit_padding=True, - scope='MobilenetV2') - self.assertEqual(net.get_shape().as_list(), [10, 10, 10, 320]) - self._assert_contains_op('MobilenetV2/expanded_conv_16/project/Identity') - self.assertEqual( - features['layer_3/expansion_output'].get_shape().as_list(), - [10, 160, 160, 96]) - self.assertEqual( - features['layer_4/expansion_output'].get_shape().as_list(), - [10, 80, 80, 144]) - - def test_mobilenet_v2_lite_def_is_quantized(self): - net, _ = mobilenet_v2.mobilenet_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - min_depth=8, - depth_multiplier=1.0, - conv_defs=mobilenet_defs.mobilenet_v2_lite_def(is_quantized=True), - use_explicit_padding=True, - scope='MobilenetV2') - self.assertEqual(net.get_shape().as_list(), [10, 10, 10, 320]) - self._assert_contains_op('MobilenetV2/expanded_conv_16/project/Relu6') - - def test_mobilenet_v2_lite_def_low_res(self): - net, _ = mobilenet_v2.mobilenet_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - min_depth=8, - depth_multiplier=1.0, - conv_defs=mobilenet_defs.mobilenet_v2_lite_def(low_res=True), - use_explicit_padding=True, - scope='MobilenetV2') - self.assertEqual(net.get_shape().as_list(), [10, 20, 20, 320]) - - def test_mobilenet_v2_lite_def_reduced(self): - net, features = mobilenet_v2.mobilenet_base( - tf.placeholder(tf.float32, (10, 320, 320, 3)), - min_depth=8, - depth_multiplier=1.0, - conv_defs=mobilenet_defs.mobilenet_v2_lite_def(reduced=True), - use_explicit_padding=True, - scope='MobilenetV2') - self.assertEqual(net.get_shape().as_list(), [10, 10, 10, 320]) - self.assertEqual( - features['layer_3/expansion_output'].get_shape().as_list(), - [10, 160, 160, 48]) - self.assertEqual( - features['layer_4/expansion_output'].get_shape().as_list(), - [10, 80, 80, 72]) - - def _assert_contains_op(self, op_name): - op_names = [op.name for op in tf.get_default_graph().get_operations()] - self.assertIn(op_name, op_names) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/lstm_object_detection/protos/__init__.py b/research/lstm_object_detection/protos/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/protos/input_reader_google.proto b/research/lstm_object_detection/protos/input_reader_google.proto deleted file mode 100644 index 2c494a62e97..00000000000 --- a/research/lstm_object_detection/protos/input_reader_google.proto +++ /dev/null @@ -1,32 +0,0 @@ -syntax = "proto2"; - -package lstm_object_detection.protos; - -import "object_detection/protos/input_reader.proto"; - -message GoogleInputReader { - extend object_detection.protos.ExternalInputReader { - optional GoogleInputReader google_input_reader = 444; - } - - oneof input_reader { - TFRecordVideoInputReader tf_record_video_input_reader = 1; - } -} - -message TFRecordVideoInputReader { - // Path(s) to tfrecords of input data. - repeated string input_path = 1; - - enum DataType { - UNSPECIFIED = 0; - TF_EXAMPLE = 1; - TF_SEQUENCE_EXAMPLE = 2; - } - optional DataType data_type = 2 [default=TF_SEQUENCE_EXAMPLE]; - - // Length of the video sequence. All the input video sequence should have the - // same length in frames, e.g. 5 frames. - optional int32 video_length = 3; -} - diff --git a/research/lstm_object_detection/protos/pipeline.proto b/research/lstm_object_detection/protos/pipeline.proto deleted file mode 100644 index 10dd652554a..00000000000 --- a/research/lstm_object_detection/protos/pipeline.proto +++ /dev/null @@ -1,69 +0,0 @@ -syntax = "proto2"; - -package lstm_object_detection.protos; - -import "object_detection/protos/pipeline.proto"; -import "lstm_object_detection/protos/quant_overrides.proto"; - -extend object_detection.protos.TrainEvalPipelineConfig { - optional LstmModel lstm_model = 205743444; - optional QuantOverrides quant_overrides = 246059837; -} - -// Message for extra fields needed for configuring LSTM model. -message LstmModel { - // Unroll length for training LSTMs. - optional int32 train_unroll_length = 1; - - // Unroll length for evaluating LSTMs. - optional int32 eval_unroll_length = 2; - - // Depth of the lstm feature map. - optional int32 lstm_state_depth = 3 [default = 256]; - - // Depth multipliers for multiple feature extractors. Used for interleaved - // or ensemble model. - repeated float depth_multipliers = 4; - - // Specifies how models are interleaved when multiple feature extractors are - // used during training. Must be in ['RANDOM', 'RANDOM_SKIP_SMALL']. - optional string train_interleave_method = 5 [default = 'RANDOM']; - - // Specifies how models are interleaved when multiple feature extractors are - // used during training. Must be in ['RANDOM', 'RANDOM_SKIP', 'SKIPK']. - optional string eval_interleave_method = 6 [default = 'SKIP9']; - - // The stride of the lstm state. - optional int32 lstm_state_stride = 7 [default = 32]; - - // Whether to flattern LSTM state and output. Note that this is typically - // intended only to be modified internally by export_tfmini_lstd_graph_lib - // to support flatten state for tfmini/tflite. Do not set this field in - // the pipeline config file unless necessary. - optional bool flatten_state = 8 [default = false]; - - // Whether to apply bottleneck layer before going into LSTM gates. This - // allows multiple feature extractors to use separate bottleneck layers - // instead of sharing the same one so that different base model output - // feature dimensions are not forced to be the same. - // For example: - // Model 1 outputs feature map f_1 of depth d_1. - // Model 2 outputs feature map f_2 of depth d_2. - // Pre-bottlenecking allows lstm input to be either: - // conv(concat([f_1, h])) or conv(concat([f_2, h])). - optional bool pre_bottleneck = 9 [default = false]; - - // Normalize LSTM state, default false. - optional bool scale_state = 10 [default = false]; - - // Clip LSTM state at [0, 6], default true. - optional bool clip_state = 11 [default = true]; - - // If the model is in quantized training. This field does NOT need to be set - // manually. Instead, it will be overridden by configs in graph_rewriter. - optional bool is_quantized = 12 [default = false]; - - // Downsample input image when using the smaller network in interleaved - // models, default false. - optional bool low_res = 13 [default = false]; -} diff --git a/research/lstm_object_detection/protos/quant_overrides.proto b/research/lstm_object_detection/protos/quant_overrides.proto deleted file mode 100644 index 9dc0eaf86e5..00000000000 --- a/research/lstm_object_detection/protos/quant_overrides.proto +++ /dev/null @@ -1,40 +0,0 @@ -syntax = "proto2"; - -package lstm_object_detection.protos; - -// Message to override default quantization behavior. -message QuantOverrides { - repeated QuantConfig quant_configs = 1; -} - -// Parameters to manually create fake quant ops outside of the generic -// tensorflow/contrib/quantize/python/quantize.py script. This may be -// used to override default behaviour or quantize ops not already supported. -message QuantConfig { - // The name of the op to add a fake quant op to. - required string op_name = 1; - - // The name of the fake quant op. - required string quant_op_name = 2; - - // Whether the fake quant op uses fixed ranges. Otherwise, learned moving - // average ranges are used. - required bool fixed_range = 3 [default = false]; - - // The intitial minimum value of the range. - optional float min = 4 [default = -6]; - - // The initial maximum value of the range. - optional float max = 5 [default = 6]; - - // Number of steps to delay before quantization takes effect during training. - optional int32 delay = 6 [default = 500000]; - - // Number of bits to use for quantizing weights. - // Only 8 bit is supported for now. - optional int32 weight_bits = 7 [default = 8]; - - // Number of bits to use for quantizing activations. - // Only 8 bit is supported for now. - optional int32 activation_bits = 8 [default = 8]; -} diff --git a/research/lstm_object_detection/test_tflite_model.py b/research/lstm_object_detection/test_tflite_model.py deleted file mode 100644 index a8b5e15e210..00000000000 --- a/research/lstm_object_detection/test_tflite_model.py +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Test a tflite model using random input data.""" - -from __future__ import print_function -from absl import flags -import numpy as np -import tensorflow.compat.v1 as tf - -flags.DEFINE_string('model_path', None, 'Path to model.') -FLAGS = flags.FLAGS - - -def main(_): - - flags.mark_flag_as_required('model_path') - - # Load TFLite model and allocate tensors. - interpreter = tf.lite.Interpreter(model_path=FLAGS.model_path) - interpreter.allocate_tensors() - - # Get input and output tensors. - input_details = interpreter.get_input_details() - print('input_details:', input_details) - output_details = interpreter.get_output_details() - print('output_details:', output_details) - - # Test model on random input data. - input_shape = input_details[0]['shape'] - # change the following line to feed into your own data. - input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32) - interpreter.set_tensor(input_details[0]['index'], input_data) - - interpreter.invoke() - output_data = interpreter.get_tensor(output_details[0]['index']) - print(output_data) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/lstm_object_detection/tflite/BUILD b/research/lstm_object_detection/tflite/BUILD deleted file mode 100644 index 66068925da4..00000000000 --- a/research/lstm_object_detection/tflite/BUILD +++ /dev/null @@ -1,81 +0,0 @@ -package( - default_visibility = ["//visibility:public"], -) - -licenses(["notice"]) - -cc_library( - name = "mobile_ssd_client", - srcs = ["mobile_ssd_client.cc"], - hdrs = ["mobile_ssd_client.h"], - deps = [ - "//protos:box_encodings_cc_proto", - "//protos:detections_cc_proto", - "//protos:labelmap_cc_proto", - "//protos:mobile_ssd_client_options_cc_proto", - "//utils:conversion_utils", - "//utils:ssd_utils", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/memory", - "@com_google_absl//absl/types:span", - "@com_google_glog//:glog", - "@gemmlowp", - ], -) - -config_setting( - name = "enable_edgetpu", - define_values = {"enable_edgetpu": "true"}, - visibility = ["//visibility:public"], -) - -cc_library( - name = "mobile_ssd_tflite_client", - srcs = ["mobile_ssd_tflite_client.cc"], - hdrs = ["mobile_ssd_tflite_client.h"], - defines = select({ - "//conditions:default": [], - "enable_edgetpu": ["ENABLE_EDGETPU"], - }), - deps = [ - ":mobile_ssd_client", - "@com_google_glog//:glog", - "@com_google_absl//absl/memory", - "@org_tensorflow//tensorflow/lite:arena_planner", - "@org_tensorflow//tensorflow/lite:framework", - "@org_tensorflow//tensorflow/lite/delegates/nnapi:nnapi_delegate", - "@org_tensorflow//tensorflow/lite/kernels:builtin_ops", - "//protos:anchor_generation_options_cc_proto", - "//utils:file_utils", - "//utils:ssd_utils", - ] + select({ - "//conditions:default": [], - "enable_edgetpu": [ - "@libedgetpu//libedgetpu:header", - ], - }), - alwayslink = 1, -) - -cc_library( - name = "mobile_lstd_tflite_client", - srcs = ["mobile_lstd_tflite_client.cc"], - hdrs = ["mobile_lstd_tflite_client.h"], - defines = select({ - "//conditions:default": [], - "enable_edgetpu": ["ENABLE_EDGETPU"], - }), - deps = [ - ":mobile_ssd_client", - ":mobile_ssd_tflite_client", - "@com_google_glog//:glog", - "@com_google_absl//absl/base:core_headers", - "@org_tensorflow//tensorflow/lite/kernels:builtin_ops", - ] + select({ - "//conditions:default": [], - "enable_edgetpu": [ - "@libedgetpu//libedgetpu:header", - ], - }), - alwayslink = 1, -) diff --git a/research/lstm_object_detection/tflite/WORKSPACE b/research/lstm_object_detection/tflite/WORKSPACE deleted file mode 100644 index 3bce3814f36..00000000000 --- a/research/lstm_object_detection/tflite/WORKSPACE +++ /dev/null @@ -1,133 +0,0 @@ -workspace(name = "lstm_object_detection") - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") -load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") - -http_archive( - name = "bazel_skylib", - sha256 = "bbccf674aa441c266df9894182d80de104cabd19be98be002f6d478aaa31574d", - strip_prefix = "bazel-skylib-2169ae1c374aab4a09aa90e65efe1a3aad4e279b", - urls = ["https://github.com/bazelbuild/bazel-skylib/archive/2169ae1c374aab4a09aa90e65efe1a3aad4e279b.tar.gz"], -) -load("@bazel_skylib//lib:versions.bzl", "versions") -versions.check(minimum_bazel_version = "0.23.0") - -# ABSL cpp library. -http_archive( - name = "com_google_absl", - urls = [ - "https://github.com/abseil/abseil-cpp/archive/a02f62f456f2c4a7ecf2be3104fe0c6e16fbad9a.tar.gz", - ], - sha256 = "d437920d1434c766d22e85773b899c77c672b8b4865d5dc2cd61a29fdff3cf03", - strip_prefix = "abseil-cpp-a02f62f456f2c4a7ecf2be3104fe0c6e16fbad9a", -) - -http_archive( - name = "rules_cc", - strip_prefix = "rules_cc-master", - urls = ["https://github.com/bazelbuild/rules_cc/archive/master.zip"], -) - -# GoogleTest/GoogleMock framework. Used by most unit-tests. -http_archive( - name = "com_google_googletest", - urls = ["https://github.com/google/googletest/archive/master.zip"], - strip_prefix = "googletest-master", -) - -# gflags needed by glog -http_archive( - name = "com_github_gflags_gflags", - sha256 = "6e16c8bc91b1310a44f3965e616383dbda48f83e8c1eaa2370a215057b00cabe", - strip_prefix = "gflags-77592648e3f3be87d6c7123eb81cbad75f9aef5a", - urls = [ - "https://mirror.bazel.build/github.com/gflags/gflags/archive/77592648e3f3be87d6c7123eb81cbad75f9aef5a.tar.gz", - "https://github.com/gflags/gflags/archive/77592648e3f3be87d6c7123eb81cbad75f9aef5a.tar.gz", - ], -) - -# glog -http_archive( - name = "com_google_glog", - sha256 = "f28359aeba12f30d73d9e4711ef356dc842886968112162bc73002645139c39c", - strip_prefix = "glog-0.4.0", - urls = ["https://github.com/google/glog/archive/v0.4.0.tar.gz"], -) - -http_archive( - name = "zlib", - build_file = "@com_google_protobuf//:third_party/zlib.BUILD", - sha256 = "c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1", - strip_prefix = "zlib-1.2.11", - urls = ["https://zlib.net/zlib-1.2.11.tar.gz"], -) - -http_archive( - name = "gemmlowp", - sha256 = "6678b484d929f2d0d3229d8ac4e3b815a950c86bb9f17851471d143f6d4f7834", - strip_prefix = "gemmlowp-12fed0cd7cfcd9e169bf1925bc3a7a58725fdcc3", - urls = [ - "http://mirror.tensorflow.org/github.com/google/gemmlowp/archive/12fed0cd7cfcd9e169bf1925bc3a7a58725fdcc3.zip", - "https://github.com/google/gemmlowp/archive/12fed0cd7cfcd9e169bf1925bc3a7a58725fdcc3.zip", - ], -) - -#----------------------------------------------------------------------------- -# proto -#----------------------------------------------------------------------------- -# proto_library, cc_proto_library and java_proto_library rules implicitly depend -# on @com_google_protobuf//:proto, @com_google_protobuf//:cc_toolchain and -# @com_google_protobuf//:java_toolchain, respectively. -# This statement defines the @com_google_protobuf repo. -http_archive( - name = "com_google_protobuf", - strip_prefix = "protobuf-3.8.0", - urls = ["https://github.com/google/protobuf/archive/v3.8.0.zip"], - sha256 = "1e622ce4b84b88b6d2cdf1db38d1a634fe2392d74f0b7b74ff98f3a51838ee53", -) - -# java_lite_proto_library rules implicitly depend on -# @com_google_protobuf_javalite//:javalite_toolchain, which is the JavaLite proto -# runtime (base classes and common utilities). -http_archive( - name = "com_google_protobuf_javalite", - strip_prefix = "protobuf-384989534b2246d413dbcd750744faab2607b516", - urls = ["https://github.com/google/protobuf/archive/384989534b2246d413dbcd750744faab2607b516.zip"], - sha256 = "79d102c61e2a479a0b7e5fc167bcfaa4832a0c6aad4a75fa7da0480564931bcc", -) - -# -# http_archive( -# name = "com_google_protobuf", -# strip_prefix = "protobuf-master", -# urls = ["https://github.com/protocolbuffers/protobuf/archive/master.zip"], -# ) - -# Needed by TensorFlow -http_archive( - name = "io_bazel_rules_closure", - sha256 = "e0a111000aeed2051f29fcc7a3f83be3ad8c6c93c186e64beb1ad313f0c7f9f9", - strip_prefix = "rules_closure-cf1e44edb908e9616030cc83d085989b8e6cd6df", - urls = [ - "http://mirror.tensorflow.org/github.com/bazelbuild/rules_closure/archive/cf1e44edb908e9616030cc83d085989b8e6cd6df.tar.gz", - "https://github.com/bazelbuild/rules_closure/archive/cf1e44edb908e9616030cc83d085989b8e6cd6df.tar.gz", # 2019-04-04 - ], -) - - -# TensorFlow r1.14-rc0 -http_archive( - name = "org_tensorflow", - strip_prefix = "tensorflow-1.14.0-rc0", - sha256 = "76404a6157a45e8d7a07e4f5690275256260130145924c2a7c73f6eda2a3de10", - urls = ["https://github.com/tensorflow/tensorflow/archive/v1.14.0-rc0.zip"], -) - -load("@org_tensorflow//tensorflow:workspace.bzl", "tf_workspace") -tf_workspace(tf_repo_name = "org_tensorflow") - -git_repository( - name = "libedgetpu", - remote = "sso://coral.googlesource.com/edgetpu-native", - commit = "83e47d1bcf22686fae5150ebb99281f6134ef062", -) diff --git a/research/lstm_object_detection/tflite/mobile_lstd_tflite_client.cc b/research/lstm_object_detection/tflite/mobile_lstd_tflite_client.cc deleted file mode 100644 index 05a7bbac1b5..00000000000 --- a/research/lstm_object_detection/tflite/mobile_lstd_tflite_client.cc +++ /dev/null @@ -1,261 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "mobile_lstd_tflite_client.h" - -#include - -namespace lstm_object_detection { -namespace tflite { - -std::unique_ptr MobileLSTDTfLiteClient::Create() { - auto client = absl::make_unique(); - if (!client->InitializeClient(CreateDefaultOptions())) { - LOG(ERROR) << "Failed to initialize client"; - return nullptr; - } - return client; -} - -protos::ClientOptions MobileLSTDTfLiteClient::CreateDefaultOptions() { - const int kMaxDetections = 100; - const int kClassesPerDetection = 1; - const double kScoreThreshold = -2.0; - const double kIouThreshold = 0.5; - - protos::ClientOptions options; - options.set_max_detections(kMaxDetections); - options.set_max_categories(kClassesPerDetection); - options.set_score_threshold(kScoreThreshold); - options.set_iou_threshold(kIouThreshold); - options.set_agnostic_mode(false); - options.set_quantize(false); - options.set_num_keypoints(0); - - return options; -} - -std::unique_ptr MobileLSTDTfLiteClient::Create( - const protos::ClientOptions& options) { - auto client = absl::make_unique(); - if (!client->InitializeClient(options)) { - LOG(ERROR) << "Failed to initialize client"; - return nullptr; - } - return client; -} - -bool MobileLSTDTfLiteClient::InitializeInterpreter( - const protos::ClientOptions& options) { - if (options.prefer_nnapi_delegate()) { - LOG(ERROR) << "NNAPI not supported."; - return false; - } else { - interpreter_->UseNNAPI(false); - } - -#ifdef ENABLE_EDGETPU - interpreter_->SetExternalContext(kTfLiteEdgeTpuContext, - edge_tpu_context_.get()); -#endif - - // Inputs are: normalized_input_image_tensor, raw_inputs/init_lstm_c, - // raw_inputs/init_lstm_h - if (interpreter_->inputs().size() != 3) { - LOG(ERROR) << "Invalid number of interpreter inputs: " << - interpreter_->inputs().size(); - return false; - } - - const std::vector input_tensor_indices = interpreter_->inputs(); - const TfLiteTensor& input_lstm_c = - *interpreter_->tensor(input_tensor_indices[1]); - if (input_lstm_c.dims->size != 4) { - LOG(ERROR) << "Invalid input lstm_c dimensions: " << - input_lstm_c.dims->size; - return false; - } - if (input_lstm_c.dims->data[0] != 1) { - LOG(ERROR) << "Invalid input lstm_c batch size: " << - input_lstm_c.dims->data[0]; - return false; - } - lstm_state_width_ = input_lstm_c.dims->data[1]; - lstm_state_height_ = input_lstm_c.dims->data[2]; - lstm_state_depth_ = input_lstm_c.dims->data[3]; - lstm_state_size_ = lstm_state_width_ * lstm_state_height_ * lstm_state_depth_; - - const TfLiteTensor& input_lstm_h = - *interpreter_->tensor(input_tensor_indices[2]); - if (!ValidateStateTensor(input_lstm_h, "input lstm_h")) { - return false; - } - - // Outputs are: - // TFLite_Detection_PostProcess, - // TFLite_Detection_PostProcess:1, - // TFLite_Detection_PostProcess:2, - // TFLite_Detection_PostProcess:3, - // raw_outputs/lstm_c, raw_outputs/lstm_h - if (interpreter_->outputs().size() != 6) { - LOG(ERROR) << "Invalid number of interpreter outputs: " << - interpreter_->outputs().size(); - return false; - } - - const std::vector output_tensor_indices = interpreter_->outputs(); - const TfLiteTensor& output_lstm_c = - *interpreter_->tensor(output_tensor_indices[4]); - if (!ValidateStateTensor(output_lstm_c, "output lstm_c")) { - return false; - } - const TfLiteTensor& output_lstm_h = - *interpreter_->tensor(output_tensor_indices[5]); - if (!ValidateStateTensor(output_lstm_h, "output lstm_h")) { - return false; - } - - // Initialize state with all zeroes. - lstm_c_data_.resize(lstm_state_size_); - lstm_h_data_.resize(lstm_state_size_); - lstm_c_data_uint8_.resize(lstm_state_size_); - lstm_h_data_uint8_.resize(lstm_state_size_); - - if (interpreter_->AllocateTensors() != kTfLiteOk) { - LOG(ERROR) << "Failed to allocate tensors"; - return false; - } - - return true; -} - -bool MobileLSTDTfLiteClient::ValidateStateTensor(const TfLiteTensor& tensor, - const std::string& name) { - if (tensor.dims->size != 4) { - LOG(ERROR) << "Invalid " << name << " dimensions: " << tensor.dims->size; - return false; - } - if (tensor.dims->data[0] != 1) { - LOG(ERROR) << "Invalid " << name << " batch size: " << tensor.dims->data[0]; - return false; - } - if (tensor.dims->data[1] != lstm_state_width_ || - tensor.dims->data[2] != lstm_state_height_ || - tensor.dims->data[3] != lstm_state_depth_) { - LOG(ERROR) << "Invalid " << name << " dimensions: [" << - tensor.dims->data[0] << ", " << tensor.dims->data[1] << ", " << - tensor.dims->data[2] << ", " << tensor.dims->data[3] << "]"; - return false; - } - return true; -} - -bool MobileLSTDTfLiteClient::ComputeOutputLayerCount() { - // Outputs are: raw_outputs/box_encodings, raw_outputs/class_predictions, - // raw_outputs/lstm_c, raw_outputs/lstm_h - CHECK_EQ(interpreter_->outputs().size(), 4); - num_output_layers_ = 1; - return true; -} - -bool MobileLSTDTfLiteClient::FloatInference(const uint8_t* input_data) { - // Inputs are: normalized_input_image_tensor, raw_inputs/init_lstm_c, - // raw_inputs/init_lstm_h - CHECK(input_data) << "Input data cannot be null."; - float* input = interpreter_->typed_input_tensor(0); - CHECK(input) << "Input tensor cannot be null."; - // Normalize the uint8 input image with mean_value_, std_value_. - NormalizeInputImage(input_data, input); - - // Copy input LSTM state into TFLite's input tensors. - float* lstm_c_input = interpreter_->typed_input_tensor(1); - CHECK(lstm_c_input) << "Input lstm_c tensor cannot be null."; - std::copy(lstm_c_data_.begin(), lstm_c_data_.end(), lstm_c_input); - - float* lstm_h_input = interpreter_->typed_input_tensor(2); - CHECK(lstm_h_input) << "Input lstm_h tensor cannot be null."; - std::copy(lstm_h_data_.begin(), lstm_h_data_.end(), lstm_h_input); - - // Run inference on inputs. - CHECK_EQ(interpreter_->Invoke(), kTfLiteOk) << "Invoking interpreter failed."; - - // Copy LSTM state out of TFLite's output tensors. - // Outputs are: raw_outputs/box_encodings, raw_outputs/class_predictions, - // raw_outputs/lstm_c, raw_outputs/lstm_h - float* lstm_c_output = interpreter_->typed_output_tensor(2); - CHECK(lstm_c_output) << "Output lstm_c tensor cannot be null."; - std::copy(lstm_c_output, lstm_c_output + lstm_state_size_, - lstm_c_data_.begin()); - - float* lstm_h_output = interpreter_->typed_output_tensor(3); - CHECK(lstm_h_output) << "Output lstm_h tensor cannot be null."; - std::copy(lstm_h_output, lstm_h_output + lstm_state_size_, - lstm_h_data_.begin()); - return true; -} - -bool MobileLSTDTfLiteClient::QuantizedInference(const uint8_t* input_data) { - // Inputs are: normalized_input_image_tensor, raw_inputs/init_lstm_c, - // raw_inputs/init_lstm_h - CHECK(input_data) << "Input data cannot be null."; - uint8_t* input = interpreter_->typed_input_tensor(0); - CHECK(input) << "Input tensor cannot be null."; - memcpy(input, input_data, input_size_); - - // Copy input LSTM state into TFLite's input tensors. - uint8_t* lstm_c_input = interpreter_->typed_input_tensor(1); - CHECK(lstm_c_input) << "Input lstm_c tensor cannot be null."; - std::copy(lstm_c_data_uint8_.begin(), lstm_c_data_uint8_.end(), lstm_c_input); - - uint8_t* lstm_h_input = interpreter_->typed_input_tensor(2); - CHECK(lstm_h_input) << "Input lstm_h tensor cannot be null."; - std::copy(lstm_h_data_uint8_.begin(), lstm_h_data_uint8_.end(), lstm_h_input); - - // Run inference on inputs. - CHECK_EQ(interpreter_->Invoke(), kTfLiteOk) << "Invoking interpreter failed."; - - // Copy LSTM state out of TFLite's output tensors. - // Outputs are: - // TFLite_Detection_PostProcess, - // TFLite_Detection_PostProcess:1, - // TFLite_Detection_PostProcess:2, - // TFLite_Detection_PostProcess:3, - // raw_outputs/lstm_c, raw_outputs/lstm_h - uint8_t* lstm_c_output = interpreter_->typed_output_tensor(4); - CHECK(lstm_c_output) << "Output lstm_c tensor cannot be null."; - std::copy(lstm_c_output, lstm_c_output + lstm_state_size_, - lstm_c_data_uint8_.begin()); - - uint8_t* lstm_h_output = interpreter_->typed_output_tensor(5); - CHECK(lstm_h_output) << "Output lstm_h tensor cannot be null."; - std::copy(lstm_h_output, lstm_h_output + lstm_state_size_, - lstm_h_data_uint8_.begin()); - return true; -} - -bool MobileLSTDTfLiteClient::Inference(const uint8_t* input_data) { - if (input_data == nullptr) { - LOG(ERROR) << "input_data cannot be null for inference."; - return false; - } - if (IsQuantizedModel()) - return QuantizedInference(input_data); - else - return FloatInference(input_data); - return true; -} - -} // namespace tflite -} // namespace lstm_object_detection diff --git a/research/lstm_object_detection/tflite/mobile_lstd_tflite_client.h b/research/lstm_object_detection/tflite/mobile_lstd_tflite_client.h deleted file mode 100644 index e4f16bc945a..00000000000 --- a/research/lstm_object_detection/tflite/mobile_lstd_tflite_client.h +++ /dev/null @@ -1,74 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_LSTD_TFLITE_CLIENT_H_ -#define TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_LSTD_TFLITE_CLIENT_H_ - -#include -#include - -#include -#include "mobile_ssd_client.h" -#include "mobile_ssd_tflite_client.h" - -namespace lstm_object_detection { -namespace tflite { - -// Client for LSTD MobileNet TfLite model. -class MobileLSTDTfLiteClient : public MobileSSDTfLiteClient { - public: - MobileLSTDTfLiteClient() = default; - // Create with default options. - static std::unique_ptr Create(); - static std::unique_ptr Create( - const protos::ClientOptions& options); - ~MobileLSTDTfLiteClient() override = default; - static protos::ClientOptions CreateDefaultOptions(); - - protected: - bool InitializeInterpreter(const protos::ClientOptions& options) override; - bool ComputeOutputLayerCount() override; - bool Inference(const uint8_t* input_data) override; - - private: - // MobileLSTDTfLiteClient is neither copyable nor movable. - MobileLSTDTfLiteClient(const MobileLSTDTfLiteClient&) = delete; - MobileLSTDTfLiteClient& operator=(const MobileLSTDTfLiteClient&) = delete; - - bool ValidateStateTensor(const TfLiteTensor& tensor, const std::string& name); - - // Helper functions used by Inference functions. - bool FloatInference(const uint8_t* input_data); - bool QuantizedInference(const uint8_t* input_data); - - // LSTM model parameters. - int lstm_state_width_ = 0; - int lstm_state_height_ = 0; - int lstm_state_depth_ = 0; - int lstm_state_size_ = 0; - - // LSTM state stored between float inference runs. - std::vector lstm_c_data_; - std::vector lstm_h_data_; - - // LSTM state stored between uint8 inference runs. - std::vector lstm_c_data_uint8_; - std::vector lstm_h_data_uint8_; -}; - -} // namespace tflite -} // namespace lstm_object_detection - -#endif // TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_LSTD_TFLITE_CLIENT_H_ diff --git a/research/lstm_object_detection/tflite/mobile_ssd_client.cc b/research/lstm_object_detection/tflite/mobile_ssd_client.cc deleted file mode 100644 index 27bf70109e4..00000000000 --- a/research/lstm_object_detection/tflite/mobile_ssd_client.cc +++ /dev/null @@ -1,209 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "mobile_ssd_client.h" - -#include - -#include - -#include -#include "absl/memory/memory.h" -#include "utils/conversion_utils.h" -#include "utils/ssd_utils.h" - -namespace lstm_object_detection { -namespace tflite { - -bool MobileSSDClient::InitializeClient(const protos::ClientOptions& options) { - options_ = options; - return true; -} - -bool MobileSSDClient::Detect(const uint8_t* pixels, int width, int height, - int bytes_per_pixel, int bytes_per_row, - protos::DetectionResults* detections) { - SetInputDims(width, height); - // Grayscale input images are only compatible with grayscale models, and - // color input images are only compatible with color models. - CHECK((bytes_per_pixel == 1 && input_depth_ == 1) || - (bytes_per_pixel >= 3 && input_depth_ >= 3)); - if (HasPadding(width, height, bytes_per_pixel, bytes_per_row)) { - std::vector unpadded_pixels = - RemovePadding(pixels, width, height, bytes_per_pixel, bytes_per_row); - return Detect(&unpadded_pixels[0], detections); - } else { - return Detect(pixels, detections); - } -} - -bool MobileSSDClient::Detect(const uint8_t* pixels, - protos::DetectionResults* detections) { - return BatchDetect(pixels, 1, absl::MakeSpan(&detections, 1)); -} - -bool MobileSSDClient::BatchDetect( - const uint8_t* pixels, int batch_size, - absl::Span detections) { - if (detections.size() != batch_size) { - LOG(ERROR) << "Batch size does not match output cardinality."; - return false; - } - if (batch_size != batch_size_) { - if (!SetBatchSize(batch_size)) { - LOG(ERROR) << "Couldn't set batch size."; - return false; - } - } - if (!Inference(pixels)) { - LOG(ERROR) << "Couldn't inference."; - return false; - } - for (int batch = 0; batch < batch_size; ++batch) { - if (RequiresPostProcessing()) { - LOG(ERROR) << "Post Processing not supported."; - return false; - } else { - if (!NoPostProcessNoAnchors(detections[batch])) { - LOG(ERROR) << "NoPostProcessNoAnchors failed."; - return false; - } - } - } - - return true; -} - -bool MobileSSDClient::SetBatchSize(int batch_size) { - batch_size_ = batch_size; - AllocateBuffers(); - if (batch_size != 1) { - LOG(ERROR) - << "Only single batch inference supported by default. All child " - "classes that support batched inference should override this method " - "and not return an error if the batch size is supported. (E.g. " - "MobileSSDTfLiteClient)."; - return false; - } - return true; -} - -bool MobileSSDClient::NoPostProcessNoAnchors( - protos::DetectionResults* detections) { - LOG(ERROR) << "not yet implemented"; - return false; -} - -bool MobileSSDClient::RequiresPostProcessing() const { - return anchors_.y_size() > 0; -} - -void MobileSSDClient::SetInputDims(int width, int height) { - CHECK_EQ(width, input_width_); - CHECK_EQ(height, input_height_); -} - -int MobileSSDClient::GetNumberOfLabels() const { return labelmap_.item_size(); } - -std::string MobileSSDClient::GetLabelDisplayName(const int class_index) const { - if (class_index < 0 || class_index >= GetNumberOfLabels()) { - return ""; - } - return labelmap_.item(class_index).display_name(); -} - -std::string MobileSSDClient::GetLabelName(const int class_index) const { - if (class_index < 0 || class_index >= GetNumberOfLabels()) { - return ""; - } - return labelmap_.item(class_index).name(); -} - -int MobileSSDClient::GetLabelId(const int class_index) const { - if (class_index < 0 || class_index >= GetNumberOfLabels() || - !labelmap_.item(class_index).has_id()) { - return -1; - } - return labelmap_.item(class_index).id(); -} - -void MobileSSDClient::SetLabelDisplayNameInResults( - protos::DetectionResults* detections) { - for (auto& det : *detections->mutable_detection()) { - for (const auto& class_index : det.class_index()) { - det.add_display_name(GetLabelDisplayName(class_index)); - } - } -} - -void MobileSSDClient::SetLabelNameInResults( - protos::DetectionResults* detections) { - for (auto& det : *detections->mutable_detection()) { - for (const auto& class_index : det.class_index()) { - det.add_class_name(GetLabelName(class_index)); - } - } -} - -void MobileSSDClient::InitParams(const bool agnostic_mode, - const bool quantize, - const int num_keypoints) { - num_keypoints_ = num_keypoints; - code_size_ = 4 + 2 * num_keypoints; - num_boxes_ = output_locations_size_ / code_size_; - if (agnostic_mode) { - num_classes_ = output_scores_size_ / num_boxes_; - } else { - num_classes_ = (output_scores_size_ / num_boxes_) - 1; - } - quantize_ = quantize; - AllocateBuffers(); -} - -void MobileSSDClient::AllocateBuffers() { - // Allocate the output vectors - output_locations_.resize(output_locations_size_ * batch_size_); - output_scores_.resize(output_scores_size_ * batch_size_); - - if (quantize_) { - quantized_output_pointers_ = - absl::make_unique>>>( - batch_size_ * num_output_layers_ * 2); - for (int batch = 0; batch < batch_size_; ++batch) { - for (int i = 0; i < num_output_layers_; ++i) { - quantized_output_pointers_->at(2 * (i + batch * num_output_layers_)) = - absl::make_unique>(output_locations_sizes_[i]); - quantized_output_pointers_->at(2 * (i + batch * num_output_layers_) + - 1) = - absl::make_unique>(output_scores_sizes_[i]); - } - } - - quantized_output_pointers_array_.reset( - new uint8_t*[batch_size_ * num_output_layers_ * 2]); - for (int i = 0; i < batch_size_ * num_output_layers_ * 2; ++i) { - quantized_output_pointers_array_[i] = - quantized_output_pointers_->at(i)->data(); - } - - gemm_context_.set_max_num_threads(1); - } else { - output_pointers_[0] = output_locations_.data(); - output_pointers_[1] = output_scores_.data(); - } -} - -} // namespace tflite -} // namespace lstm_object_detection diff --git a/research/lstm_object_detection/tflite/mobile_ssd_client.h b/research/lstm_object_detection/tflite/mobile_ssd_client.h deleted file mode 100644 index 609bf5c9820..00000000000 --- a/research/lstm_object_detection/tflite/mobile_ssd_client.h +++ /dev/null @@ -1,216 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_SSD_CLIENT_H_ -#define TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_SSD_CLIENT_H_ - -#include -#include - -#include -#include "absl/types/span.h" -#include "public/gemmlowp.h" -#include "protos/box_encodings.pb.h" -#include "protos/detections.pb.h" -#include "protos/labelmap.pb.h" -#include "protos/mobile_ssd_client_options.pb.h" - -namespace lstm_object_detection { -namespace tflite { - -// MobileSSDClient base class. Not thread-safe. -class MobileSSDClient { - public: - MobileSSDClient() = default; - virtual ~MobileSSDClient() = default; - - // Runs detection on the image represented by 'pixels', described by the - // associated 'width', 'height', 'bytes_per_pixel' and 'bytes_per_row'. All - // these integers must be positive, 'bytes_per_row' must be sufficiently - // large, and for 'bytes_per_pixel' only values 1, 3, 4 may be passed. - // Depending on the implementation most combinations may not be allowed. - bool Detect(const uint8_t* pixels, int width, int height, int bytes_per_pixel, - int bytes_per_row, protos::DetectionResults* detections); - - // Same as before, but a contiguous bytewise encoding of 'pixels' is assumed. - // That encoding can be assigned directly to the input layer of the neural - // network. - bool Detect(const uint8_t* pixels, protos::DetectionResults* detections); - - // Runs batched inference on the provided buffer. "pixels" is assumed to be a - // continuous buffer of width * height * depth * batch_size pixels. It will - // populate the detections result with batch_size DetectionResults where the - // first result corresponds to the first image contained within the pixels - // block. Note that not all models generalize correctly to multi-batch - // inference and in some cases the addition of extra batches may corrupt the - // output on the model. For example, if a network performs operations across - // batches, BatchDetect([A, B]) may not equal [Detect(A), Detect(B)]. - bool BatchDetect(const uint8_t* pixels, int batch_size, - absl::Span detections); - - // Sets the dimensions of the input image on the fly, to be effective for the - // next Detect() call. - void SetInputDims(int width, int height); - - // Returns the width of the input image which is always positive. Usually a - // constant or the width last set via 'SetInputDims()'. - int GetInputWidth() const { return input_width_; } - - // Returns the height of the input image which is always positive. Usually a - // constant or the width last set via 'SetInputDims()'. - int GetInputHeight() const { return input_height_; } - - // Returns the depth of the input image, which is the same as bytes per pixel. - // This will be 3 (for RGB images), 4 (for RGBA images), or 1 (for grayscale - // images). - int GetInputDepth() const { return input_depth_; } - - // Returns the number of possible detection labels or classes. If - // agnostic_mode is on, then this method must return 1. - int GetNumberOfLabels() const; - - // Returns human readable class labels given predicted class index. The range - // of 'label_index' is determined by 'GetNumberOfLabels()'. Returns an empty - // string if the label display name is undefined or 'label_index' is out of - // range. - std::string GetLabelDisplayName(const int class_index) const; - - // Returns Knowledge Graph MID class labels given predicted class index. The - // range of 'label_index' is determined by 'GetNumberOfLabels()'. Returns an - // empty string if the label name is undefined or 'label_index' is out of - // range. - std::string GetLabelName(const int class_index) const; - - // Returns the class/label ID for a given predicted class index. The range of - // 'label_index' is determined by 'GetNumberOfLabels()'. Returns -1 in case - // 'label_index' is out of range. - int GetLabelId(const int class_index) const; - - // Explicitly sets human readable string class name to each detection using - // the `display_name` field. - void SetLabelDisplayNameInResults(protos::DetectionResults* detections); - - // Explicitly sets string class name to each detection using the `class_name` - // fields. - void SetLabelNameInResults(protos::DetectionResults* detections); - - protected: - // Initializes the client from options. - virtual bool InitializeClient(const protos::ClientOptions& options); - - // Initializes various model specific parameters. - virtual void InitParams() { - InitParams(false, false, 0); - } - - virtual void InitParams(const bool agnostic_mode, - const bool quantize, - const int num_keypoints); - - virtual void InitParams(const bool agnostic_mode, const bool quantize, - const int num_keypoints, - const protos::BoxCoder& coder) { - InitParams(agnostic_mode, quantize, num_keypoints); - *options_.mutable_box_coder() = coder; - } - - virtual void AllocateBuffers(); - - // Sets the batch size of inference. If reimplmented, overrider is responsible - // for calling parent (the returned status code may be ignored). - virtual bool SetBatchSize(int batch_size); - - // Perform client specific inference on input_data. - virtual bool Inference(const uint8_t* input_data) = 0; - - // Directly populates the results when no post-processing should take place - // and no anchors are present. This is only possible when the TensorFlow - // graph contains the customized post-processing ops. - virtual bool NoPostProcessNoAnchors(protos::DetectionResults* detections); - - // Returns true iff the model returns raw output and needs its results - // post-processed (including non-maximum suppression). If false then anchors - // do not need to be present, LoadAnchors() can be implemented empty. Note - // that almost all models around require post-processing. - bool RequiresPostProcessing() const; - - // Load client specific labelmap proto file. - virtual void LoadLabelMap() = 0; - - // Anchors for the model. - protos::CenterSizeEncoding anchors_; - // Labelmap for the model. - protos::StringIntLabelMapProto labelmap_; - // Options for the model. - protos::ClientOptions options_; - - // Buffers for storing the model predictions - float* output_pointers_[2]; - // The dimension of output_locations is [batch_size x num_anchors x 4] - std::vector output_locations_; - // The dimension of output_scores is: - // If background class is included: - // [batch_size x num_anchors x (num_classes + 1)] - // If background class is NOT included: - // [batch_size x num_anchors x num_classes] - std::vector output_scores_; - void* transient_data_; - - // Total location and score sizes. - int output_locations_size_; - int output_scores_size_; - // Output location and score sizes for each output layer. - std::vector output_locations_sizes_; - std::vector output_scores_sizes_; - - // Preproccessing related parameters - float mean_value_; - float std_value_; - std::vector location_zero_points_; - std::vector location_scales_; - std::vector score_zero_points_; - std::vector score_scales_; - - int num_output_layers_ = 1; - - // Model related parameters - int input_size_; - int num_classes_; - int num_boxes_; - int input_width_; - int input_height_; - int input_depth_ = 3; // Default value is set for backward compatibility. - int code_size_; - - int batch_size_ = 1; // Default value is set for backwards compatibility. - - // The number of keypoints by detection. Specific to faces for now. - int num_keypoints_; - // Whether to use the quantized model. - bool quantize_; - // The indices of restricted classes (empty if none was passed in the config). - std::vector restricted_class_indices_; - - // Buffers for storing quantized model predictions - std::unique_ptr>>> - quantized_output_pointers_; - std::unique_ptr quantized_output_pointers_array_; - gemmlowp::GemmContext gemm_context_; -}; - -} // namespace tflite -} // namespace lstm_object_detection - -#endif // TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_SSD_CLIENT_H_ diff --git a/research/lstm_object_detection/tflite/mobile_ssd_tflite_client.cc b/research/lstm_object_detection/tflite/mobile_ssd_tflite_client.cc deleted file mode 100644 index f2b70a66395..00000000000 --- a/research/lstm_object_detection/tflite/mobile_ssd_tflite_client.cc +++ /dev/null @@ -1,579 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "mobile_ssd_tflite_client.h" - -#include -#include "tensorflow/lite/arena_planner.h" -#include "tensorflow/lite/context.h" -#include "tensorflow/lite/kernels/register.h" -#include "utils/file_utils.h" -#include "utils/ssd_utils.h" - -namespace lstm_object_detection { -namespace tflite { - -namespace { - -constexpr int kInputBatch = 1; -constexpr int kInputDepth = 1; -constexpr int kNumBoundingBoxCoordinates = 4; // xmin, ymin, width, height -constexpr int GetBoxIndex(const int layer) { return (2 * layer); } -constexpr int GetScoreIndex(const int layer) { return (2 * layer + 1); } - -} // namespace - -MobileSSDTfLiteClient::MobileSSDTfLiteClient() {} - -std::unique_ptr<::tflite::MutableOpResolver> -MobileSSDTfLiteClient::CreateOpResolver() { - return absl::make_unique<::tflite::ops::builtin::BuiltinOpResolver>(); -} - -bool MobileSSDTfLiteClient::InitializeClient( - const protos::ClientOptions& options) { - if (!MobileSSDClient::InitializeClient(options)) { - return false; - } - if (options.has_external_files()) { - if (options.external_files().model_file_name().empty() && - options.external_files().model_file_content().empty()) { - LOG(ERROR) - << "MobileSSDClient: both `external_files.model_file_name` and " - "`external_files.model_file_content` are empty which is invalid."; - } - if (!options_.external_files().model_file_content().empty()) { - model_ = ::tflite::FlatBufferModel::BuildFromBuffer( - options_.external_files().model_file_content().data(), - options_.external_files().model_file_content().size()); - } else { - const char* tflite_model_filename = reinterpret_cast( - options_.external_files().model_file_name().c_str()); - - model_ = ::tflite::FlatBufferModel::BuildFromFile(tflite_model_filename); - } - } else { - LOG(ERROR) << "Embedded model is not supported."; - return false; - } - if (!model_) { - LOG(ERROR) << "Failed to load model"; - return false; - } - - LoadLabelMap(); - - resolver_ = CreateOpResolver(); - -#ifdef ENABLE_EDGETPU - edge_tpu_context_ = - edgetpu::EdgeTpuManager::GetSingleton()->NewEdgeTpuContext(); - resolver_->AddCustom(edgetpu::kCustomOp, edgetpu::RegisterCustomOp()); -#endif - - ::tflite::InterpreterBuilder(*model_, *resolver_)(&interpreter_); - if (!interpreter_) { - LOG(ERROR) << "Failed to build interpreter"; - return false; - } - - if (!InitializeInterpreter(options)) { - LOG(ERROR) << "Failed to initialize interpreter"; - return false; - } - - if (RequiresPostProcessing() && !ComputeOutputSize()) { - LOG(ERROR) << "Failed to compute output size"; - return false; - } - - // Initializes number of boxes, number of keypoints, quantized model flag and - // allocates output arrays based on output size computed by - // ComputeOutputSize() - agnostic_mode_ = options.agnostic_mode(); - if (!restricted_class_indices_.empty()) { - LOG(ERROR) << "Restricted class unsupported."; - return false; - } - // Default num_keypoints will be overridden by value specified by - // GetNumberOfKeypoints() - const int num_keypoints = GetNumberOfKeypoints(); - - // Other parameters are not needed and do not make sense when the model - // contains the post-processing ops. Avoid init altogether in this case. - if (RequiresPostProcessing()) { - InitParams(IsAgnosticMode(), IsQuantizedModel(), num_keypoints, - GetBoxCoder()); - } - - SetImageNormalizationParams(); - // Getting shape of input tensors. This also checks for size consistency with - // anchors. It also makes input_width_ and input_height_ available to - // LoadAnchors - if (!SetInputShape()) { - LOG(ERROR) << "Failed to set input shape"; - return false; - } - - // Output sizes are compared to expect sizes based on number of anchors, - // number of classes, number of key points and number of values used to - // represent a bounding box. - if (RequiresPostProcessing() && !CheckOutputSizes()) { - LOG(ERROR) << "Check for output size failed"; - return false; - } - - SetZeroPointsAndScaleFactors(quantize_); - - LOG(INFO) << "Model initialized:" - << " input_size: " << input_size_ - << ", output_locations_size: " << output_locations_size_ - << ", preprocessing mean value: " << mean_value_ - << ", preprocessing std value: " << std_value_; - - return true; -} - -void MobileSSDTfLiteClient::SetImageNormalizationParams() { - mean_value_ = 127.5f; - std_value_ = 127.5f; -} - -int MobileSSDTfLiteClient::GetNumberOfKeypoints() const { - return options_.num_keypoints(); -} - -bool MobileSSDTfLiteClient::SetInputShape() { - // inputs() maps the input tensor index to the index TFLite's tensors - const int input_tensor_index = interpreter_->inputs()[0]; - const TfLiteTensor* input_tensor = interpreter_->tensor(input_tensor_index); - if ((input_tensor->type != kTfLiteUInt8) && - (input_tensor->type != kTfLiteFloat32)) { - LOG(ERROR) << "Unsupported tensor input type: " << input_tensor->type; - return false; - } - if (input_tensor->dims->size != 4) { - LOG(ERROR) << "Expected input tensor dimension size to be 4, got " - << input_tensor->dims->size; - return false; - } - input_depth_ = input_tensor->dims->data[3]; - input_width_ = input_tensor->dims->data[2]; - input_height_ = input_tensor->dims->data[1]; - input_size_ = input_height_ * input_width_ * input_depth_ * batch_size_; - return true; -} - -bool MobileSSDTfLiteClient::InitializeInterpreter( - const protos::ClientOptions& options) { - if (options.prefer_nnapi_delegate()) { - LOG(ERROR) << "NNAPI not supported."; - return false; - } - interpreter_->UseNNAPI(false); - -#ifdef ENABLE_EDGETPU - interpreter_->SetExternalContext(kTfLiteEdgeTpuContext, - edge_tpu_context_.get()); -#endif - - if (options.num_threads() > 0) { - interpreter_->SetNumThreads(options.num_threads()); - } - - if (interpreter_->inputs().size() != 1) { - LOG(ERROR) << "Invalid number of interpreter inputs: " - << interpreter_->inputs().size(); - return false; - } - - if (interpreter_->AllocateTensors() != kTfLiteOk) { - LOG(ERROR) << "Failed to allocate tensors!"; - return false; - } - return true; -} - -bool MobileSSDTfLiteClient::CheckOutputSizes() { - int expected_output_locations_size = - anchors_.y_size() * (kNumBoundingBoxCoordinates + 2 * num_keypoints_); - if (output_locations_size_ != expected_output_locations_size) { - LOG(ERROR) - << "The dimension of output_locations must be [num_anchors x 4]. Got " - << output_locations_size_ << " but expected " - << expected_output_locations_size; - return false; - } - - // Include background class score when not in agnostic mode - int expected_output_scores_size = - anchors_.y_size() * (labelmap_.item_size() + (IsAgnosticMode() ? 0 : 1)); - if (output_scores_size_ != expected_output_scores_size) { - LOG(ERROR) - << "The dimension of output_scores is: " - "[num_anchors x (num_classes + 1)] if background class is included. " - "[num_anchors x num_classes] if background class is not included. " - "Got " - << output_scores_size_ << " but expected " - << expected_output_scores_size; - return false; - } - return true; -} - -bool MobileSSDTfLiteClient::IsQuantizedModel() const { - const int input_tensor_index = interpreter_->inputs()[0]; - const TfLiteTensor* input_tensor = interpreter_->tensor(input_tensor_index); - return input_tensor->type == kTfLiteUInt8; -} - -void MobileSSDTfLiteClient::SetZeroPointsAndScaleFactors( - bool is_quantized_model) { - // Sets initial scale to 1 and zero_points to 0. These values are only - // written over in quantized model case. - location_zero_points_.assign(num_output_layers_, 0); - location_scales_.assign(num_output_layers_, 1); - score_zero_points_.assign(num_output_layers_, 0); - score_scales_.assign(num_output_layers_, 1); - - // Set scale and zero_point for quantized model - if (is_quantized_model) { - for (int layer = 0; layer < num_output_layers_; ++layer) { - const int location_tensor_index = - interpreter_->outputs()[GetBoxIndex(layer)]; - const TfLiteTensor* location_tensor = - interpreter_->tensor(location_tensor_index); - - location_zero_points_[layer] = location_tensor->params.zero_point; - location_scales_[layer] = location_tensor->params.scale; - - // Class Scores - const int score_tensor_index = - interpreter_->outputs()[GetScoreIndex(layer)]; - const TfLiteTensor* score_tensor = - interpreter_->tensor(score_tensor_index); - - score_zero_points_[layer] = score_tensor->params.zero_point; - score_scales_[layer] = score_tensor->params.scale; - } - } -} - -bool MobileSSDTfLiteClient::ComputeOutputLocationsSize( - const TfLiteTensor* location_tensor, int layer) { - const int location_tensor_size = location_tensor->dims->size; - if (location_tensor_size == 3) { - const int location_code_size = location_tensor->dims->data[2]; - const int location_num_anchors = location_tensor->dims->data[1]; - output_locations_sizes_[layer] = location_code_size * location_num_anchors; - } else if (location_tensor_size == 4) { - const int location_depth = location_tensor->dims->data[3]; - const int location_width = location_tensor->dims->data[2]; - const int location_height = location_tensor->dims->data[1]; - output_locations_sizes_[layer] = - location_depth * location_width * location_height; - } else { - LOG(ERROR) << "Expected location_tensor_size of 3 or 4, got " - << location_tensor_size; - return false; - } - return true; -} - -bool MobileSSDTfLiteClient::ComputeOutputScoresSize( - const TfLiteTensor* score_tensor, int layer) { - const int score_tensor_size = score_tensor->dims->size; - if (score_tensor_size == 3) { - const int score_num_classes = score_tensor->dims->data[2]; - const int score_num_anchors = score_tensor->dims->data[1]; - output_scores_sizes_[layer] = score_num_classes * score_num_anchors; - } else if (score_tensor_size == 4) { - const int score_depth = score_tensor->dims->data[3]; - const int score_width = score_tensor->dims->data[2]; - const int score_height = score_tensor->dims->data[1]; - output_scores_sizes_[layer] = score_depth * score_width * score_height; - } else { - LOG(ERROR) << "Expected score_tensor_size of 3 or 4, got " - << score_tensor_size; - return false; - } - return true; -} - -bool MobileSSDTfLiteClient::ComputeOutputLayerCount() { - // Compute number of layers in the output model - const int num_outputs = interpreter_->outputs().size(); - if (num_outputs == 0) { - LOG(ERROR) << "Number of outputs cannot be zero."; - return false; - } - if (num_outputs % 2 != 0) { - LOG(ERROR) << "Number of outputs must be evenly divisible by 2. Actual " - "number of outputs: " - << num_outputs; - return false; - } - num_output_layers_ = num_outputs / 2; - return true; -} - -bool MobileSSDTfLiteClient::ComputeOutputSize() { - if (!ComputeOutputLayerCount()) { - return false; - } - - // Allocate output arrays for box location and class scores - output_locations_sizes_.resize(num_output_layers_); - output_scores_sizes_.resize(num_output_layers_); - output_locations_size_ = 0; - output_scores_size_ = 0; - // This loop calculates the total size of data occupied by the output as well - // as the size for everylayer of the model. For quantized case, it also stores - // the offset and scale factor needed to transform the data back to floating - // point values. - for (int layer = 0; layer < num_output_layers_; ++layer) { - // Calculate sizes of Box locations output - const int location_tensor_index = - interpreter_->outputs()[GetBoxIndex(layer)]; - const TfLiteTensor* location_tensor = - interpreter_->tensor(location_tensor_index); - if (!ComputeOutputLocationsSize(location_tensor, layer)) { - return false; - } - output_locations_size_ += output_locations_sizes_[layer]; - - // Class Scores - const int score_tensor_index = - interpreter_->outputs()[GetScoreIndex(layer)]; - const TfLiteTensor* score_tensor = interpreter_->tensor(score_tensor_index); - if (!ComputeOutputScoresSize(score_tensor, layer)) { - return false; - } - output_scores_size_ += output_scores_sizes_[layer]; - } - return true; -} - -void MobileSSDTfLiteClient::NormalizeInputImage(const uint8_t* input_data, - float* normalized_input_data) { - float reciprocal_std_value_ = (1.0f / std_value_); - for (int i = 0; i < input_size_; i++, input_data++, normalized_input_data++) { - *normalized_input_data = - reciprocal_std_value_ * (static_cast(*input_data) - mean_value_); - } -} - -void MobileSSDTfLiteClient::GetOutputBoxesAndScoreTensorsFromFloat() { - float* output_score_pointer = output_scores_.data(); - float* output_location_pointer = output_locations_.data(); - for (int batch = 0; batch < batch_size_; ++batch) { - for (int layer = 0; layer < num_output_layers_; ++layer) { - // Write output location data - const float* location_data = - interpreter_->typed_output_tensor(GetBoxIndex(layer)) + - batch * output_locations_sizes_[layer]; - memcpy(output_location_pointer, location_data, - output_locations_sizes_[layer] * sizeof(float)); - output_location_pointer += output_locations_sizes_[layer]; - - // Write output class scores - const float* score_data = - interpreter_->typed_output_tensor(GetScoreIndex(layer)) + - batch * output_scores_sizes_[layer]; - memcpy(output_score_pointer, score_data, - output_scores_sizes_[layer] * sizeof(float)); - output_score_pointer += output_scores_sizes_[layer]; - } - } -} - -void MobileSSDTfLiteClient::GetOutputBoxesAndScoreTensorsFromUInt8() { - // The box locations and score are now convert back to floating point from - // their quantized version by shifting and scaling the output tensors on an - // element-wise basis - auto output_score_it = output_scores_.begin(); - auto output_location_it = output_locations_.begin(); - for (int batch = 0; batch < batch_size_; ++batch) { - for (int layer = 0; layer < num_output_layers_; ++layer) { - // Write output location data - const auto location_scale = location_scales_[layer]; - const auto location_zero_point = location_zero_points_[layer]; - const auto* location_data = - interpreter_->typed_output_tensor(GetBoxIndex(layer)); - for (int j = 0; j < output_locations_sizes_[layer]; - ++j, ++output_location_it) { - *output_location_it = - location_scale * - (static_cast( - location_data[j + batch * output_locations_sizes_[layer]]) - - location_zero_point); - } - - // write output class scores - const auto score_scale = score_scales_[layer]; - const auto score_zero_point = score_zero_points_[layer]; - const auto* score_data = - interpreter_->typed_output_tensor(GetScoreIndex(layer)); - for (int j = 0; j < output_scores_sizes_[layer]; ++j, ++output_score_it) { - *output_score_it = - score_scale * - (static_cast( - score_data[j + batch * output_scores_sizes_[layer]]) - - score_zero_point); - } - } - } -} - -bool MobileSSDTfLiteClient::FloatInference(const uint8_t* input_data) { - auto* input = interpreter_->typed_input_tensor(0); - if (input == nullptr) { - LOG(ERROR) << "Input tensor cannot be null for inference."; - return false; - } - // The non-quantized model assumes float input - // So we normalize the uint8 input image using mean_value_ - // and std_value_ - NormalizeInputImage(input_data, input); - // Applies model to the data. The data will be store in the output tensors - if (interpreter_->Invoke() != kTfLiteOk) { - LOG(ERROR) << "Invoking interpreter resulted in non-okay status."; - return false; - } - // Parse outputs - if (RequiresPostProcessing()) { - GetOutputBoxesAndScoreTensorsFromFloat(); - } - return true; -} - -bool MobileSSDTfLiteClient::QuantizedInference(const uint8_t* input_data) { - auto* input = interpreter_->typed_input_tensor(0); - if (input == nullptr) { - LOG(ERROR) << "Input tensor cannot be null for inference."; - return false; - } - memcpy(input, input_data, input_size_); - - // Applies model to the data. The data will be store in the output tensors - if (interpreter_->Invoke() != kTfLiteOk) { - LOG(ERROR) << "Invoking interpreter resulted in non-okay status."; - return false; - } - // Parse outputs - if (RequiresPostProcessing()) { - GetOutputBoxesAndScoreTensorsFromUInt8(); - } - return true; -} - -bool MobileSSDTfLiteClient::Inference(const uint8_t* input_data) { - if (input_data == nullptr) { - LOG(ERROR) << "input_data cannot be null for inference."; - return false; - } - if (IsQuantizedModel()) - return QuantizedInference(input_data); - else - return FloatInference(input_data); - return true; -} - -bool MobileSSDTfLiteClient::NoPostProcessNoAnchors( - protos::DetectionResults* detections) { - const float* boxes = interpreter_->typed_output_tensor(0); - const float* classes = interpreter_->typed_output_tensor(1); - const float* confidences = interpreter_->typed_output_tensor(2); - int num_detections = - static_cast(interpreter_->typed_output_tensor(3)[0]); - int max_detections = options_.max_detections() > 0 ? options_.max_detections() - : num_detections; - - std::vector sorted_indices; - sorted_indices.resize(num_detections); - for (int i = 0; i < num_detections; ++i) sorted_indices[i] = i; - std::sort(sorted_indices.begin(), sorted_indices.end(), - [&confidences](const int i, const int j) { - return confidences[i] > confidences[j]; - }); - - for (int i = 0; - i < num_detections && detections->detection_size() < max_detections; - ++i) { - const int index = sorted_indices[i]; - if (confidences[index] < options_.score_threshold()) { - break; - } - const int class_index = classes[index]; - protos::Detection* detection = detections->add_detection(); - detection->add_score(confidences[index]); - detection->add_class_index(class_index); - // For some reason it is not OK to add class/label names here, they appear - // to mess up the drishti graph. - // detection->add_display_name(GetLabelDisplayName(class_index)); - // detection->add_class_name(GetLabelName(class_index)); - - protos::BoxCornerEncoding* box = detection->mutable_box(); - box->add_ymin(boxes[4 * index]); - box->add_xmin(boxes[4 * index + 1]); - box->add_ymax(boxes[4 * index + 2]); - box->add_xmax(boxes[4 * index + 3]); - } - return true; -} - -bool MobileSSDTfLiteClient::SetBatchSize(int batch_size) { - if (!this->MobileSSDClient::SetBatchSize(batch_size)) { - LOG(ERROR) << "Error in SetBatchSize()"; - return false; - } - input_size_ = input_height_ * input_width_ * input_depth_ * batch_size_; - - for (int input : interpreter_->inputs()) { - auto* old_dims = interpreter_->tensor(input)->dims; - std::vector new_dims(old_dims->data, old_dims->data + old_dims->size); - new_dims[0] = batch_size; - if (interpreter_->ResizeInputTensor(input, new_dims) != kTfLiteOk) { - LOG(ERROR) << "Unable to resize input for new batch size"; - return false; - } - } - - if (interpreter_->AllocateTensors() != kTfLiteOk) { - LOG(ERROR) << "Unable to reallocate tensors"; - return false; - } - - return true; -} - -void MobileSSDTfLiteClient::LoadLabelMap() { - if (options_.has_external_files()) { - if (options_.external_files().has_label_map_file_content() || - options_.external_files().has_label_map_file_name()) { - CHECK(LoadLabelMapFromFileOrBytes( - options_.external_files().label_map_file_name(), - options_.external_files().label_map_file_content(), &labelmap_)); - } else { - LOG(ERROR) << "MobileSSDTfLiteClient: both " - "'external_files.label_map_file_content` and " - "'external_files.label_map_file_name` are empty" - " which is invalid."; - } - } -} - -} // namespace tflite -} // namespace lstm_object_detection diff --git a/research/lstm_object_detection/tflite/mobile_ssd_tflite_client.h b/research/lstm_object_detection/tflite/mobile_ssd_tflite_client.h deleted file mode 100644 index 40af225549c..00000000000 --- a/research/lstm_object_detection/tflite/mobile_ssd_tflite_client.h +++ /dev/null @@ -1,115 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_SSD_TFLITE_CLIENT_H_ -#define TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_SSD_TFLITE_CLIENT_H_ - -#include -#include - -#include "absl/memory/memory.h" -#include "tensorflow/lite/delegates/nnapi/nnapi_delegate.h" -#include "tensorflow/lite/interpreter.h" -#include "tensorflow/lite/model.h" -#include "mobile_ssd_client.h" -#include "protos/anchor_generation_options.pb.h" -#ifdef ENABLE_EDGETPU -#include "libedgetpu/edgetpu.h" -#endif // ENABLE_EDGETPU - -namespace lstm_object_detection { -namespace tflite { - -class MobileSSDTfLiteClient : public MobileSSDClient { - public: - MobileSSDTfLiteClient(); - explicit MobileSSDTfLiteClient( - std::unique_ptr<::tflite::OpResolver> resolver); - ~MobileSSDTfLiteClient() override = default; - - protected: - // By default CreateOpResolver will create - // tflite::ops::builtin::BuiltinOpResolver. Overriding the function allows the - // client to use custom op resolvers. - virtual std::unique_ptr<::tflite::MutableOpResolver> CreateOpResolver(); - - bool InitializeClient(const protos::ClientOptions& options) override; - - virtual bool InitializeInterpreter(const protos::ClientOptions& options); - virtual bool ComputeOutputLayerCount(); - - bool Inference(const uint8_t* input_data) override; - - bool NoPostProcessNoAnchors(protos::DetectionResults* detections) override; - - // Use with caution. Not all models work correctly when resized to larger - // batch sizes. This will resize the input tensor to have the given batch size - // and propagate the batch dimension throughout the graph. - bool SetBatchSize(int batch_size) override; - - // This can be overridden in a subclass to load label map from file - void LoadLabelMap() override; - - // This can be overridden in a subclass to return customized box coder. - virtual const protos::BoxCoder GetBoxCoder() { return protos::BoxCoder(); } - - virtual void SetImageNormalizationParams(); - void NormalizeInputImage(const uint8_t* input_data, - float* normalized_input_data); - void GetOutputBoxesAndScoreTensorsFromFloat(); - - virtual bool IsQuantizedModel() const; - -#ifdef ENABLE_EDGETPU - std::unique_ptr edge_tpu_context_; -#endif - - std::unique_ptr<::tflite::FlatBufferModel> model_; - std::unique_ptr<::tflite::MutableOpResolver> resolver_; - std::unique_ptr<::tflite::Interpreter> interpreter_; - - private: - // MobileSSDTfLiteClient is neither copyable nor movable. - MobileSSDTfLiteClient(const MobileSSDTfLiteClient&) = delete; - MobileSSDTfLiteClient& operator=(const MobileSSDTfLiteClient&) = delete; - - // Helper functions used by Initialize Client. - virtual int GetNumberOfKeypoints() const; - - // Returns true if the client is in class-agnostic mode. This function can be - // overridden in a subclass to return an ad-hoc value (e.g. hard-coded). - virtual bool IsAgnosticMode() const { return agnostic_mode_; } - bool CheckOutputSizes(); - bool ComputeOutputSize(); - bool SetInputShape(); - void SetZeroPointsAndScaleFactors(bool is_quantized_model); - bool ComputeOutputLocationsSize(const TfLiteTensor* location_tensor, - int layer); - bool ComputeOutputScoresSize(const TfLiteTensor* score_tensor, int layer); - - // The agnostic_mode_ field should never be directly read. Always use its - // virtual accessor method: IsAgnosticMode(). - bool agnostic_mode_; - - // Helper functions used by Inference functions - bool FloatInference(const uint8_t* input_data); - bool QuantizedInference(const uint8_t* input_data); - void GetOutputBoxesAndScoreTensorsFromUInt8(); -}; - -} // namespace tflite -} // namespace lstm_object_detection - -#endif // TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_MOBILE_SSD_TFLITE_CLIENT_H_ diff --git a/research/lstm_object_detection/tflite/protos/BUILD b/research/lstm_object_detection/tflite/protos/BUILD deleted file mode 100644 index 80d50ed7ec8..00000000000 --- a/research/lstm_object_detection/tflite/protos/BUILD +++ /dev/null @@ -1,61 +0,0 @@ -package( - default_visibility = ["//visibility:public"], -) - -licenses(["notice"]) - -proto_library( - name = "box_encodings_proto", - srcs = ["box_encodings.proto"], -) - -cc_proto_library( - name = "box_encodings_cc_proto", - deps = [":box_encodings_proto"], -) - -proto_library( - name = "detections_proto", - srcs = ["detections.proto"], - deps = [":box_encodings_proto"], -) - -cc_proto_library( - name = "detections_cc_proto", - deps = [":detections_proto"], -) - -proto_library( - name = "labelmap_proto", - srcs = ["labelmap.proto"], -) - -cc_proto_library( - name = "labelmap_cc_proto", - deps = [":labelmap_proto"], -) - -proto_library( - name = "mobile_ssd_client_options_proto", - srcs = ["mobile_ssd_client_options.proto"], - deps = [ - ":anchor_generation_options_proto", - ":box_encodings_proto", - ":labelmap_proto", - ], -) - -cc_proto_library( - name = "mobile_ssd_client_options_cc_proto", - deps = [":mobile_ssd_client_options_proto"], -) - -proto_library( - name = "anchor_generation_options_proto", - srcs = ["anchor_generation_options.proto"], -) - -cc_proto_library( - name = "anchor_generation_options_cc_proto", - deps = [":anchor_generation_options_proto"], -) diff --git a/research/lstm_object_detection/tflite/protos/anchor_generation_options.proto b/research/lstm_object_detection/tflite/protos/anchor_generation_options.proto deleted file mode 100644 index d164c239f93..00000000000 --- a/research/lstm_object_detection/tflite/protos/anchor_generation_options.proto +++ /dev/null @@ -1,53 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -syntax = "proto2"; - -package lstm_object_detection.tflite.protos; - - - -// This is dervice from TensorFlow's SsdAnchorGenerator proto that is used to -// configures TensorFlow's anchor generator. -// object_detection/protos/ssd_anchor_generator.proto -message AnchorGenerationOptions { - // The input image width in pixels - optional int32 image_width = 1; - - // The input image height in pixels - optional int32 image_height = 2; - - // The base anchor width in pixels - optional int32 base_anchor_width = 3; - - // The base anchor height in pixels - optional int32 base_anchor_height = 4; - - // The minimum anchor scaling (should be < 1.0) - optional float min_anchor_scale = 5; - - // The maximum anchor scaling - optional float max_anchor_scale = 6; - - // List of aspect ratios to generate anchors for. Aspect ratio is specified as - // (width/height) - repeated float anchor_aspect_ratios = 7 [packed = true]; - - // List of strides in pixels for each layer - repeated int32 anchor_strides = 8 [packed = true]; - - // List of offset in pixels for each layer - repeated int32 anchor_offsets = 9 [packed = true]; -} diff --git a/research/lstm_object_detection/tflite/protos/box_encodings.proto b/research/lstm_object_detection/tflite/protos/box_encodings.proto deleted file mode 100644 index d701914e93d..00000000000 --- a/research/lstm_object_detection/tflite/protos/box_encodings.proto +++ /dev/null @@ -1,97 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -syntax = "proto2"; - -package lstm_object_detection.tflite.protos; - -// The bounding box representation by center location and width/height. -// Also includes optional keypoint coordinates. -// It is a default representation in modern object detection systems. -message CenterSizeEncoding { - // Encoded anchor box center. - repeated float y = 1; - repeated float x = 2; - - // Encoded anchor box height. - repeated float h = 3; - - // Encoded anchor box width. - repeated float w = 4; - - // Encoded keypoint coordinates. - repeated float keypoint_y = 5; - repeated float keypoint_x = 6; -} - -// The scaling factors for decoding predicted offsets with CenterSizeEncoding. -// For example, given a prediction and an anchor in CenterSizeEncoding, the -// decoded location is: -// y = prediction.y / coder.y_scale() * anchor.h + anchor.y; -// x = prediction.x / coder.x_scale() * anchor.w + anchor.x; -// h = exp(prediction.h / coder.h_scale()) * anchor.h; -// w = exp(prediction.w / coder.w_scale()) * anchor.w; -// keypoint_y = prediction.keypoint_y / coder.keypoint_y_scale() * anchor.h -// + anchor.y; -// keypoint_x = prediction.keypoint_x / coder.keypoint_x_scale() * anchor.w -// + anchor.x; -// See mobile_ssd::DecodeCenterSizeBoxes for more details. -// This coder is compatible with models trained using -// object_detection.protos.FasterRcnnBoxCoder and -// object_detection.protos.KeypointBoxCoder. -message CenterSizeOffsetCoder { - // Scale factor for encoded box center offset. - optional float y_scale = 1 [default = 10.0]; - optional float x_scale = 2 [default = 10.0]; - - // Scale factor for encoded box height offset. - optional float h_scale = 3 [default = 5.0]; - - // Scale factor for encoded box width offset. - optional float w_scale = 4 [default = 5.0]; - - // Scale factor for encoded keypoint coordinate offset. - optional float keypoint_y_scale = 5 [default = 10.0]; - optional float keypoint_x_scale = 6 [default = 10.0]; -} - -// The canonical representation of bounding box. -message BoxCornerEncoding { - // Box corners. - repeated float ymin = 1; - repeated float xmin = 2; - repeated float ymax = 3; - repeated float xmax = 4; - - // Keypoint coordinates. - repeated float keypoint_y = 5; - repeated float keypoint_x = 6; -} - -// The scaling value used to adjust predicted bounding box corners. -// For example, given a prediction in BoxCornerEncoding and an anchor in -// CenterSizeEncoding, the decoded location is: -// ymin = prediction.ymin * coder.stddev + anchor.y - anchor.h / 2 -// xmin = prediction.xmin * coder.stddev + anchor.x - anchor.w / 2 -// ymax = prediction.ymax * coder.stddev + anchor.y + anchor.h / 2 -// xmax = prediction.xmax * coder.stddev + anchor.x + anchor.w / 2 -// This coder doesn't support keypoints. -// See mobile_ssd::DecodeBoxCornerBoxes for more details. -// This coder is compatible with models trained using -// object_detection.protos.MeanStddevBoxCoder. -message BoxCornerOffsetCoder { - // The standard deviation used to encode and decode boxes. - optional float stddev = 1 [default = 0.01]; -} diff --git a/research/lstm_object_detection/tflite/protos/detections.proto b/research/lstm_object_detection/tflite/protos/detections.proto deleted file mode 100644 index 7dc46a1990e..00000000000 --- a/research/lstm_object_detection/tflite/protos/detections.proto +++ /dev/null @@ -1,39 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -syntax = "proto2"; - -package lstm_object_detection.tflite.protos; - -import "protos/box_encodings.proto"; - -// DetectionResults is a list of Detection. -message DetectionResults { - repeated Detection detection = 1; -} - -// Detection consists of a bounding box, class confidences and indices. -message Detection { - // Each detection message consists of only one bounding box. - optional BoxCornerEncoding box = 1; - // A box can be associated with multiple confidences for multiple classes. - repeated float score = 2; - repeated int32 class_index = 3; - // Optional, for readability and easier access for external modules. - // A unique name that identifies the class, e.g. a MID. - repeated string class_name = 4; - // A human readable name of the class. - repeated string display_name = 5; -} diff --git a/research/lstm_object_detection/tflite/protos/labelmap.proto b/research/lstm_object_detection/tflite/protos/labelmap.proto deleted file mode 100644 index 1c99931ff9d..00000000000 --- a/research/lstm_object_detection/tflite/protos/labelmap.proto +++ /dev/null @@ -1,67 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// This proto defines the labelmap used in the detection models, which maps -// the numerical class index outputs to KG mid or human readable string of -// object class name. -// -// An example labelmap looks like the following: -// item { -// name: "/m/0frqm" -// id: 1 -// display_name: "Envelope" -// } -// item { -// name: "/m/02dl1y" -// id: 2 -// display_name: "Hat" -// } -// item { -// name: "/m/01krhy" -// id: 3 -// display_name: "Tiara" -// } - -syntax = "proto2"; - -package lstm_object_detection.tflite.protos; - - - -message StringIntLabelMapItem { - optional string name = 1; - optional int32 id = 2; - repeated float embedding = 3 [packed = true]; - optional string display_name = 4; - // Optional list of children used to represent a hierarchy. - // - // E.g.: - // - // item { - // name: "/m/02xwb" # Fruit - // child_name: "/m/014j1m" # Apple - // child_name: "/m/0388q" # Grape - // ... - // } - // item { - // name: "/m/014j1m" # Apple - // ... - // } - repeated string child_name = 5; -} - -message StringIntLabelMapProto { - repeated StringIntLabelMapItem item = 1; -} diff --git a/research/lstm_object_detection/tflite/protos/mobile_ssd_client_options.proto b/research/lstm_object_detection/tflite/protos/mobile_ssd_client_options.proto deleted file mode 100644 index 0fbd07574f7..00000000000 --- a/research/lstm_object_detection/tflite/protos/mobile_ssd_client_options.proto +++ /dev/null @@ -1,122 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -syntax = "proto2"; - -package lstm_object_detection.tflite.protos; - -import "protos/anchor_generation_options.proto"; -import "protos/box_encodings.proto"; - -// Next ID: 17 -message ClientOptions { - // The name of the Mobile SSD Client. - optional string mobile_ssd_client_name = 1; - - // The maximum number of detections to return. - optional uint32 max_detections = 2 [default = 10]; - - // The maximum number of categories to return per detection. - optional uint32 max_categories = 3 [default = 1]; - - // The global score threshold below which detections are rejected. - optional float score_threshold = 4 [default = 0.0]; - - // The threshold on intersection-over-union used by non-maxima suppression. - optional float iou_threshold = 5 [default = 0.3]; - - // Optional allowlist of class names. If non-empty, detections whose class - // name is not in this set will be filtered out. Duplicate or unknown class - // names are ignored. - repeated string class_name_allowlist = 6; - - // SSD in single class agnostic model. - optional bool agnostic_mode = 7 [default = false]; - - // Fully convolutional mode, which requires on-the-fly anchor generation. - optional bool fully_conv = 8 [default = false]; - - // Quantized model. - optional bool quantize = 9 [default = false]; - - // Number of keypoints. - optional uint32 num_keypoints = 10 [default = 0]; - - // Optional anchor generations options. This can be used to generate - // anchors for an SSD model. It is utilized in - // MobileSSDTfLiteClient::LoadAnchors() - optional AnchorGenerationOptions anchor_generation_options = 12; - - // Optional box coder specifications. This can be used for models trained - // with a customized box coder. If unspecified, it will use - // CenterSizeOffsetCoder and its default parameters. - optional BoxCoder box_coder = 13; - - // The external model files used to create the detector. - // This is an alternative to registered models, where you specify external - // model via the following: - // - model using model_file_name or model_file_content - // - labelmap using label_map_file_content - // - anchors using anchor_generation_options,proto (TODO: add support for - // filename as well) - optional ExternalFiles external_files = 16; - - message ExternalFiles { - // Path to the model file in FlatBuffer format. - optional string model_file_name = 1; - - // Content of the model file. If provided, this takes precedence over the - // model_file_name field. - optional bytes model_file_content = 2; - - // Path to the label map file. - optional string label_map_file_name = 4; - - // Content of the label map file. If provided, this takes precedence over - // the label_map_file_name field. - optional bytes label_map_file_content = 3; - - // Path to the anchor file. - optional string anchor_file_name = 5; - - // Content of the anchor file. If provided, this takes precedence over - // the anchor_file_name field. - optional bytes anchor_file_content = 6; - } - - // Whether to use NNAPI delegate for hardware acceleration. - // If it fails, it will fall back to the normal CPU execution. - optional bool prefer_nnapi_delegate = 14; - - // Number of threads to be used by TFlite interpreter for SSD inference. Does - // single-threaded inference by default. - optional int32 num_threads = 15 [default = 1]; - - extensions 1000 to max; -} - -message BoxCoder { - oneof box_coder_oneof { - CenterSizeOffsetCoder center_size_offset_coder = 1; - BoxCornerOffsetCoder box_corner_offset_coder = 2; - } -} - -message ModelData { - oneof source { - string model_file = 1; - bytes embedded_model = 2; - } -} diff --git a/research/lstm_object_detection/tflite/protos/proto_config.asciipb b/research/lstm_object_detection/tflite/protos/proto_config.asciipb deleted file mode 100644 index e01dc7c4808..00000000000 --- a/research/lstm_object_detection/tflite/protos/proto_config.asciipb +++ /dev/null @@ -1,5 +0,0 @@ -# This file necessary for Portable Proto library -allow_all: true - -# Other configuration options: -optimize_mode: LITE_RUNTIME diff --git a/research/lstm_object_detection/tflite/utils/BUILD b/research/lstm_object_detection/tflite/utils/BUILD deleted file mode 100644 index 6a6d19b8e13..00000000000 --- a/research/lstm_object_detection/tflite/utils/BUILD +++ /dev/null @@ -1,48 +0,0 @@ -package( - default_visibility = ["//visibility:public"], -) - -licenses(["notice"]) - -cc_library( - name = "conversion_utils", - srcs = ["conversion_utils.cc"], - hdrs = ["conversion_utils.h"], - deps = [ - "@com_google_absl//absl/base:core_headers", - "@com_google_glog//:glog", - ], -) - -cc_test( - name = "conversion_utils_test", - srcs = ["conversion_utils_test.cc"], - deps = [ - ":conversion_utils", - "@com_google_googletest//:gtest_main", - ], -) - -cc_library( - name = "ssd_utils", - srcs = ["ssd_utils.cc"], - hdrs = ["ssd_utils.h"], - deps = [ - "//protos:anchor_generation_options_cc_proto", - "//protos:box_encodings_cc_proto", - "//protos:detections_cc_proto", - "@com_google_absl//absl/strings", - "@com_google_glog//:glog", - ], -) - -cc_library( - name = "file_utils", - srcs = ["file_utils.cc"], - hdrs = ["file_utils.h"], - deps = [ - "//protos:labelmap_cc_proto", - "@com_google_absl//absl/strings", - "@com_google_glog//:glog", - ], -) diff --git a/research/lstm_object_detection/tflite/utils/conversion_utils.cc b/research/lstm_object_detection/tflite/utils/conversion_utils.cc deleted file mode 100644 index 072d2ba1853..00000000000 --- a/research/lstm_object_detection/tflite/utils/conversion_utils.cc +++ /dev/null @@ -1,65 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "utils/conversion_utils.h" - -#include - -namespace lstm_object_detection { -namespace tflite { - -bool HasPadding(int width, int height, int bytes_per_pixel, int bytes_per_row) { - CHECK_LT(0, width); - CHECK_LT(0, height); - CHECK(bytes_per_pixel == 1 || bytes_per_pixel == 3 || bytes_per_pixel == 4); - CHECK_LE(width * bytes_per_pixel, bytes_per_row); - - if (bytes_per_pixel == 4) { - return true; - } - return (width * bytes_per_pixel < bytes_per_row); -} - -std::vector RemovePadding(const uint8_t* image_data, int width, - int height, int bytes_per_pixel, - int bytes_per_row) { - CHECK_LT(0, width); - CHECK_LT(0, height); - CHECK(bytes_per_pixel == 1 || bytes_per_pixel == 3 || bytes_per_pixel == 4); - CHECK_LE(width * bytes_per_pixel, bytes_per_row); - - const int unpadded_bytes_per_pixel = (bytes_per_pixel == 1 ? 1 : 3); - const int pixel_padding = (bytes_per_pixel == 4 ? 1 : 0); - std::vector unpadded_image_data(width * height * - unpadded_bytes_per_pixel); - - const uint8_t* row_ptr = image_data; - int index = 0; - for (int y = 0; y < height; ++y) { - const uint8_t* ptr = row_ptr; - for (int x = 0; x < width; ++x) { - for (int d = 0; d < unpadded_bytes_per_pixel; ++d) { - unpadded_image_data[index++] = *ptr++; - } - ptr += pixel_padding; - } - row_ptr += bytes_per_row; - } - - return unpadded_image_data; -} - -} // namespace tflite -} // namespace lstm_object_detection diff --git a/research/lstm_object_detection/tflite/utils/conversion_utils.h b/research/lstm_object_detection/tflite/utils/conversion_utils.h deleted file mode 100644 index 964a38103dd..00000000000 --- a/research/lstm_object_detection/tflite/utils/conversion_utils.h +++ /dev/null @@ -1,47 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -// Lightweight utilities related to conversion of input images. - -#ifndef TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_CONVERSION_UTILS_H_ -#define TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_CONVERSION_UTILS_H_ - -#include - -#include - -namespace lstm_object_detection { -namespace tflite { - -// Finds out whether a call to 'RemovePadding()' is needed to process the given -// pixel data constellation in order to make it suitable for model input layer. -// All integers must be positive, 'bytes_per_row' must be sufficiently large, -// and for 'bytes_per_pixel' only values 1, 3, 4 may be passed and implies a -// grayscale, RGB, or RGBA image. Returns true iff excessive bytes exist in the -// associated pixel data. -bool HasPadding(int width, int height, int bytes_per_pixel, int bytes_per_row); - -// Removes padding at the pixel and row level of pixel data which is stored in -// the usual row major order ("interleaved"). Produces pixel data which is -// suitable for model input layer. If 'HasPadding()' is false then this -// function will return an identical copy of 'image'. For restrictions on the -// integer parameters see comment on 'HasPadding()'. -std::vector RemovePadding(const uint8_t* image, int width, int height, - int bytes_per_pixel, int bytes_per_row); - -} // namespace tflite -} // namespace lstm_object_detection - -#endif // TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_CONVERSION_UTILS_H_ diff --git a/research/lstm_object_detection/tflite/utils/conversion_utils_test.cc b/research/lstm_object_detection/tflite/utils/conversion_utils_test.cc deleted file mode 100644 index 97ddf3c7dff..00000000000 --- a/research/lstm_object_detection/tflite/utils/conversion_utils_test.cc +++ /dev/null @@ -1,163 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "utils/conversion_utils.h" - -#include - -#include -#include -#include "gtest/gtest.h" - -using testing::ContainerEq; - -namespace lstm_object_detection { -namespace tflite { -namespace { - -TEST(ConversionUtilsTests, HasPaddingNonPositiveDimensions) { - EXPECT_DEATH(HasPadding(/* width= */ 0, /* height= */ 4, - /* bytes_per_pixel= */ 4, /* bytes_per_row= */ 12), - ""); - EXPECT_DEATH(HasPadding(/* width= */ 3, /* height= */ 0, - /* bytes_per_pixel= */ 4, /* bytes_per_row= */ 12), - ""); -} - -TEST(ConversionUtilsTests, HasPaddingIllegalDepth) { - for (int bytes_per_pixel : {-1, 0, 2, 5, 6}) { - EXPECT_DEATH(HasPadding(/* width= */ 3, /* height= */ 4, bytes_per_pixel, - /* bytes_per_row= */ 12), - ""); - } -} - -TEST(ConversionUtilsTests, HasPaddingWithRGBAImage) { - const int kWidth = 3; - const int kHeight = 4; - const int kBytesPerPixel = 4; - EXPECT_DEATH( - HasPadding(kWidth, kHeight, kBytesPerPixel, /* bytes_per_row= */ 11), ""); - EXPECT_TRUE( - HasPadding(kWidth, kHeight, kBytesPerPixel, /* bytes_per_row= */ 12)); - EXPECT_TRUE( - HasPadding(kWidth, kHeight, kBytesPerPixel, /* bytes_per_row= */ 13)); -} - -TEST(ConversionUtilsTests, HasPaddingWithRGBImage) { - const int kWidth = 3; - const int kHeight = 4; - const int kBytesPerPixel = 3; - EXPECT_DEATH( - HasPadding(kWidth, kHeight, kBytesPerPixel, /* bytes_per_row= */ 8), ""); - EXPECT_FALSE( - HasPadding(kWidth, kHeight, kBytesPerPixel, /* bytes_per_row= */ 9)); - EXPECT_TRUE( - HasPadding(kWidth, kHeight, kBytesPerPixel, /* bytes_per_row= */ 10)); -} - -TEST(ConversionUtilsTests, HasPaddingWithGrayscaleImage) { - const int kWidth = 3; - const int kHeight = 4; - const int kBytesPerPixel = 1; - EXPECT_DEATH( - HasPadding(kWidth, kHeight, kBytesPerPixel, - /* bytes_per_row= */ 2), ""); - EXPECT_FALSE( - HasPadding(kWidth, kHeight, kBytesPerPixel, - /* bytes_per_row= */ 3)); - EXPECT_TRUE( - HasPadding(kWidth, kHeight, kBytesPerPixel, - /* bytes_per_row= */ 4)); -} - -TEST(ConversionUtilsTests, RemovePaddingWithRGBAImage) { - constexpr int kWidth = 4; - constexpr int kHeight = 2; - constexpr int kBytesPerPixel = 4; - constexpr int kStride = kBytesPerPixel * kWidth * sizeof(uint8_t); - const std::vector kImageData{ - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, - 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36}; - ASSERT_EQ(kHeight * kStride, kImageData.size()); - - std::vector actual = - RemovePadding(&kImageData[0], kWidth, kHeight, kBytesPerPixel, kStride); - - const std::vector kExpected = { - 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, - 21, 22, 23, 25, 26, 27, 29, 30, 31, 33, 34, 35, - }; - EXPECT_EQ(3 * kWidth * kHeight, actual.size()); - EXPECT_THAT(actual, ContainerEq(kExpected)); -} - -TEST(ConversionUtilsTests, RemovePaddingWithRGBImage) { - constexpr int kWidth = 4; - constexpr int kHeight = 2; - constexpr int kBytesPerPixel = 3; - constexpr int kBytesPerRow = kBytesPerPixel * kWidth * sizeof(uint8_t); - const std::vector kImageData{1, 2, 3, 5, 6, 7, 9, 10, - 11, 13, 14, 15, 21, 22, 23, 25, - 26, 27, 29, 30, 31, 33, 34, 35}; - ASSERT_EQ(kHeight * kBytesPerRow, kImageData.size()); - - std::vector actual = RemovePadding(&kImageData[0], kWidth, kHeight, - kBytesPerPixel, kBytesPerRow); - - EXPECT_EQ(3 * kWidth * kHeight, actual.size()); - EXPECT_THAT(actual, ContainerEq(kImageData)); -} - -TEST(ConversionUtilsTests, RemovePaddingWithGrayscaleImage) { - constexpr int kWidth = 8; - constexpr int kHeight = 2; - constexpr int kBytesPerPixel = 1; - constexpr int kBytesPerRow = kBytesPerPixel * kWidth * sizeof(uint8_t); - const std::vector kImageData{ - 1, 2, 3, 4, 5, 6, 7, 8, 21, 22, 23, 24, 25, 26, 27, 28, - }; - ASSERT_EQ(kHeight * kBytesPerRow, kImageData.size()); - - std::vector actual = RemovePadding(&kImageData[0], kWidth, kHeight, - kBytesPerPixel, kBytesPerRow); - - EXPECT_EQ(kWidth * kHeight, actual.size()); - EXPECT_THAT(actual, ContainerEq(kImageData)); -} - -TEST(ConversionUtilsTests, RemovePaddingWithPadding) { - constexpr int kWidth = 8; - constexpr int kHeight = 2; - constexpr int kBytesPerPixel = 1; - // Pad each row with two bytes. - constexpr int kBytesPerRow = kBytesPerPixel * (kWidth + 2) * sizeof(uint8_t); - const std::vector kImageData{1, 2, 3, 4, 5, 6, 7, 8, 21, 22, - 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}; - ASSERT_EQ(kHeight * kBytesPerRow, kImageData.size()); - - std::vector actual = RemovePadding(&kImageData[0], kWidth, kHeight, - kBytesPerPixel, kBytesPerRow); - - const std::vector kExpected = { - 1, 2, 3, 4, 5, 6, 7, 8, 23, 24, 25, 26, 27, 28, 29, 30, - }; - EXPECT_EQ(kWidth * kHeight, actual.size()); - EXPECT_THAT(actual, ContainerEq(kExpected)); -} - -} // namespace -} // namespace tflite -} // namespace lstm_object_detection diff --git a/research/lstm_object_detection/tflite/utils/file_utils.cc b/research/lstm_object_detection/tflite/utils/file_utils.cc deleted file mode 100644 index e1c86ab2e36..00000000000 --- a/research/lstm_object_detection/tflite/utils/file_utils.cc +++ /dev/null @@ -1,55 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "utils/file_utils.h" - -#include - -#include - -namespace lstm_object_detection { -namespace tflite { - -std::string ReadFileToString(absl::string_view filename) { - std::ifstream file(filename.data(), std::ios::binary | std::ios::ate); - CHECK(file.is_open()); - int filesize = file.tellg(); - std::string result; - result.resize(filesize); - CHECK_EQ(result.size(), filesize); - file.seekg(0); - CHECK(file.read(&(result)[0], filesize)); - file.close(); - return result; -} - -bool LoadLabelMapFromFileOrBytes(const std::string& labelmap_file, - const std::string& labelmap_bytes, - protos::StringIntLabelMapProto* labelmap) { - if (!labelmap_bytes.empty()) { - CHECK(labelmap->ParseFromString(labelmap_bytes)); - } else { - if (labelmap_file.empty()) { - LOG(ERROR) << "labelmap file empty."; - return false; - } - const std::string proto_bytes = ReadFileToString(labelmap_file); - CHECK(labelmap->ParseFromString(proto_bytes)); - } - return true; -} - -} // namespace tflite -} // namespace lstm_object_detection diff --git a/research/lstm_object_detection/tflite/utils/file_utils.h b/research/lstm_object_detection/tflite/utils/file_utils.h deleted file mode 100644 index 8eea3a10d10..00000000000 --- a/research/lstm_object_detection/tflite/utils/file_utils.h +++ /dev/null @@ -1,38 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_FILE_UTILS_H_ -#define TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_FILE_UTILS_H_ - -#include - -#include "absl/strings/string_view.h" -#include "protos/labelmap.pb.h" - -namespace lstm_object_detection { -namespace tflite { - -std::string ReadFileToString(absl::string_view filename); - -// Load labelmap from a binary proto file or bytes string. -// labelmap_bytes takes precedence over labelmap_file. -bool LoadLabelMapFromFileOrBytes(const std::string& labelmap_file, - const std::string& labelmap_bytes, - protos::StringIntLabelMapProto* labelmap); - -} // namespace tflite -} // namespace lstm_object_detection - -#endif // TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_FILE_UTILS_H_ diff --git a/research/lstm_object_detection/tflite/utils/ssd_utils.cc b/research/lstm_object_detection/tflite/utils/ssd_utils.cc deleted file mode 100644 index fdae8efee04..00000000000 --- a/research/lstm_object_detection/tflite/utils/ssd_utils.cc +++ /dev/null @@ -1,537 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#include "utils/ssd_utils.h" - -#include - -#include - -#include -#include "absl/strings/str_cat.h" - -namespace lstm_object_detection { -namespace tflite { -namespace { -using protos::AnchorGenerationOptions; -using protos::BoxCornerEncoding; -using protos::BoxCornerOffsetCoder; -using protos::CenterSizeEncoding; -using protos::CenterSizeOffsetCoder; -using protos::DetectionResults; - -void DecreasingArgSort(const std::vector& values, - std::vector* indices) { - indices->resize(values.size()); - for (int i = 0; i < values.size(); ++i) (*indices)[i] = i; - std::sort( - indices->begin(), indices->end(), - [&values](const int i, const int j) { return values[i] > values[j]; }); -} - -void DecreasingPartialArgSort(const float* values, int num_values, - int num_to_sort, int* indices) { - for (int i = 0; i < num_values; ++i) { - indices[i] = i; - } - std::partial_sort( - indices, indices + num_to_sort, indices + num_values, - [&values](const int i, const int j) { return values[i] > values[j]; }); -} - -// The row index offset is 1 if background class is included and 0 otherwise. -int GetLabelOffset(const int num_boxes, - const int num_classes, - const int score_size) { - const int label_offset = score_size / num_boxes - num_classes; - CHECK_EQ(score_size, (num_classes + label_offset) * num_boxes); - return label_offset; -} - -void ApplyThreshold(const std::vector& values, - const float threshold, - std::vector* keep_values, - std::vector* keep_indices) { - for (int i = 0; i < values.size(); i++) { - if (values[i] >= threshold) { - keep_values->emplace_back(values[i]); - keep_indices->emplace_back(i); - } - } -} - -void ValidateBoxes(const BoxCornerEncoding& boxes) { - const int num_boxes = boxes.ymin_size(); - CHECK_EQ(num_boxes, boxes.ymax_size()); - CHECK_EQ(num_boxes, boxes.xmin_size()); - CHECK_EQ(num_boxes, boxes.xmax_size()); - - for (int i = 0; i < num_boxes; ++i) { - CHECK_GE(boxes.ymax(i), boxes.ymin(i)); - CHECK_GE(boxes.xmax(i), boxes.xmin(i)); - } -} -} // namespace - - -void DecodeBoxCornerBoxes(const BoxCornerEncoding& predictions, - const CenterSizeEncoding& anchors, - const BoxCornerOffsetCoder& coder, - BoxCornerEncoding* decoded_boxes) { - const int num_boxes = predictions.ymin_size(); - CHECK_EQ(num_boxes, anchors.y_size()); - CHECK_EQ(predictions.keypoint_y_size(), 0) - << "BoxCornerOffsetCoder doesn't work with keypoints."; - - float ymin, xmin, ymax, xmax; - for (int i = 0; i < num_boxes; ++i) { - ymin = predictions.ymin(i) * coder.stddev() + - (anchors.y(i) - anchors.h(i) / 2); - xmin = predictions.xmin(i) * coder.stddev() + - (anchors.x(i) - anchors.w(i) / 2); - ymax = predictions.ymax(i) * coder.stddev() + - (anchors.y(i) + anchors.h(i) / 2); - xmax = predictions.xmax(i) * coder.stddev() + - (anchors.x(i) + anchors.w(i) / 2); - - decoded_boxes->add_ymin(ymin); - decoded_boxes->add_xmin(xmin); - decoded_boxes->add_ymax(std::max(ymax, ymin)); - decoded_boxes->add_xmax(std::max(xmax, xmin)); - } -} - -void DecodeCenterSizeBoxes(const CenterSizeEncoding& predictions, - const CenterSizeEncoding& anchors, - const CenterSizeOffsetCoder& coder, - BoxCornerEncoding* decoded_boxes) { - CHECK_EQ(predictions.y_size(), anchors.y_size()); - const int num_boxes = predictions.y_size(); - const int num_keypoints = predictions.keypoint_y_size() / num_boxes; - float ycenter, xcenter, h, w, ymin, xmin, ymax, xmax; - for (int i = 0; i < num_boxes; ++i) { - ycenter = predictions.y(i) / coder.y_scale() * anchors.h(i) + anchors.y(i); - xcenter = predictions.x(i) / coder.x_scale() * anchors.w(i) + anchors.x(i); - h = std::exp(predictions.h(i) / coder.h_scale()) * anchors.h(i); - w = std::exp(predictions.w(i) / coder.w_scale()) * anchors.w(i); - - ymin = ycenter - h / 2.; - xmin = xcenter - w / 2.; - ymax = ycenter + h / 2.; - xmax = xcenter + w / 2.; - - decoded_boxes->add_ymin(ymin); - decoded_boxes->add_xmin(xmin); - decoded_boxes->add_ymax(ymax); - decoded_boxes->add_xmax(xmax); - - // keypoints - for (int j = 0; j < num_keypoints; ++j) { - float keypoint_y = predictions.keypoint_y(num_keypoints * i + j) / - coder.keypoint_y_scale() * anchors.h(i) + anchors.y(i); - float keypoint_x = predictions.keypoint_x(num_keypoints * i + j) / - coder.keypoint_x_scale() * anchors.w(i) + anchors.x(i); - decoded_boxes->add_keypoint_y(keypoint_y); - decoded_boxes->add_keypoint_x(keypoint_x); - } - } -} - -float ComputeIOU(const BoxCornerEncoding& boxes, const int i, const int j) { - const float area_i = - (boxes.ymax(i) - boxes.ymin(i)) * (boxes.xmax(i) - boxes.xmin(i)); - const float area_j = - (boxes.ymax(j) - boxes.ymin(j)) * (boxes.xmax(j) - boxes.xmin(j)); - if (area_i <= 0 || area_j <= 0) return 0.0; - const float intersection_ymin = std::max(boxes.ymin(i), boxes.ymin(j)); - const float intersection_xmin = std::max(boxes.xmin(i), boxes.xmin(j)); - const float intersection_ymax = std::min(boxes.ymax(i), boxes.ymax(j)); - const float intersection_xmax = std::min(boxes.xmax(i), boxes.xmax(j)); - const float intersection_area = - std::max(intersection_ymax - intersection_ymin, 0.0) * - std::max(intersection_xmax - intersection_xmin, 0.0); - return intersection_area / (area_i + area_j - intersection_area); -} - -void NonMaxSuppressionMultiClass(const BoxCornerEncoding& boxes, - const std::vector& scores, - const int num_classes, - const int max_detection_per_class, - const float score_threshold, - const float iou_threshold, - DetectionResults* detections) { - const int num_boxes = boxes.ymin_size(); - const int num_keypoints = boxes.keypoint_y_size() / num_boxes; - // The row index offset is 1 if the background class is included. - const int label_offset = - GetLabelOffset(num_boxes, num_classes, scores.size()); - - detections->Clear(); - std::vector selected; - std::vector class_scores; - class_scores.resize(num_boxes); - // For each class, perform non-max suppression. - for (int col = 0; col < num_classes; col++) { - for (int row = 0; row < num_boxes; row++) { - class_scores[row] = - scores[row * (num_classes + label_offset) + col + label_offset]; - } - NonMaxSuppression(boxes, class_scores, max_detection_per_class, - score_threshold, iou_threshold, &selected); - for (const auto& selected_index : selected) { - auto* new_detection = detections->add_detection(); - auto* new_detection_box = new_detection->mutable_box(); - new_detection_box->add_ymin(boxes.ymin(selected_index)); - new_detection_box->add_xmin(boxes.xmin(selected_index)); - new_detection_box->add_ymax(boxes.ymax(selected_index)); - new_detection_box->add_xmax(boxes.xmax(selected_index)); - new_detection->add_score(class_scores[selected_index]); - new_detection->add_class_index(col); - for (int i = 0; i < num_keypoints; ++i) { - new_detection_box->add_keypoint_y(boxes.keypoint_y( - selected_index * num_keypoints + i)); - new_detection_box->add_keypoint_x(boxes.keypoint_x( - selected_index * num_keypoints + i)); - } - } - } -} - -void NonMaxSuppressionMultiClassFast( - const BoxCornerEncoding& boxes, const std::vector& scores, - const int num_classes, const int max_detection, const int max_category, - const float score_threshold, const float iou_threshold, - DetectionResults* detections) { - const int num_boxes = boxes.ymin_size(); - const int num_keypoints = boxes.keypoint_y_size() / num_boxes; - const int label_offset = - GetLabelOffset(num_boxes, num_classes, scores.size()); - - int num_category = std::min(max_category, num_classes); - detections->Clear(); - std::vector max_scores; - max_scores.resize(num_boxes); - std::vector sorted_class_indices; - sorted_class_indices.resize(num_boxes * num_classes); - for (int row = 0; row < num_boxes; row++) { - const float* box_scores = - scores.data() + row * (num_classes + label_offset) + label_offset; - int* class_indices = sorted_class_indices.data() + row * num_classes; - DecreasingPartialArgSort(box_scores, num_classes, num_category, - class_indices); - max_scores[row] = box_scores[class_indices[0]]; - } - // Perform non-max suppression on max scores - std::vector selected; - NonMaxSuppression(boxes, max_scores, max_detection, score_threshold, - iou_threshold, &selected); - for (const auto& selected_index : selected) { - auto* new_detection = detections->add_detection(); - auto* new_detection_box = new_detection->mutable_box(); - new_detection_box->add_ymin(boxes.ymin(selected_index)); - new_detection_box->add_xmin(boxes.xmin(selected_index)); - new_detection_box->add_ymax(boxes.ymax(selected_index)); - new_detection_box->add_xmax(boxes.xmax(selected_index)); - const float* box_scores = scores.data() + - selected_index * (num_classes + label_offset) + - label_offset; - const int* class_indices = - sorted_class_indices.data() + selected_index * num_classes; - for (int i = 0; i < num_category; ++i) { - new_detection->add_score(box_scores[class_indices[i]]); - new_detection->add_class_index(class_indices[i]); - } - for (int i = 0; i < num_keypoints; ++i) { - new_detection_box->add_keypoint_y(boxes.keypoint_y( - selected_index * num_keypoints + i)); - new_detection_box->add_keypoint_x(boxes.keypoint_x( - selected_index * num_keypoints + i)); - } - } -} - -void NonMaxSuppressionMultiClassRestrict( - std::vector restricted_class_indices, const BoxCornerEncoding& boxes, - const std::vector& scores, const int num_classes, - const int max_detection, const int max_category, - const float score_threshold, const float iou_threshold, - DetectionResults* detections) { - int num_boxes = boxes.ymin_size(); - const int label_offset = - GetLabelOffset(num_boxes, num_classes, scores.size()); - // Slice the score matrix along columns to extract the scores of the - // restricted classes. - int restricted_num_classes = restricted_class_indices.size(); - std::vector restricted_scores; - restricted_scores.reserve(num_boxes * restricted_num_classes); - for (int i = 0; i < num_boxes; ++i) { - for (int index : restricted_class_indices) { - CHECK(index >= 0 && index < num_classes + label_offset); - restricted_scores.push_back( - scores[i * (num_classes + label_offset) + index + label_offset]); - } - } - // Apply non-maxima suppression to the sliced score matrix. - NonMaxSuppressionMultiClassFast( - boxes, restricted_scores, restricted_num_classes, max_detection, - max_category, score_threshold, iou_threshold, detections); - // Resulting indices are based on score matrix column index: remap to the - // original class indices. - for (auto& detection : *detections->mutable_detection()) { - for (int i = 0; i < detection.class_index_size(); ++i) { - detection.set_class_index( - i, restricted_class_indices[detection.class_index(i)]); - } - } -} - -void NonMaxSuppression(const BoxCornerEncoding& boxes, - const std::vector& scores, - const int max_detection, const float score_threshold, - const float iou_threshold, std::vector* selected) { - CHECK_EQ(boxes.ymin_size(), scores.size()) - << "The number of bounding boxes and scores does not match."; - CHECK_GT(max_detection, 0) << "Maximum detections should be positive."; - CHECK_GT(iou_threshold, 0.0) << "iou_threshold should be positive."; - CHECK_LT(iou_threshold, 1.0) << "iou_threshold should be less than 1."; - ValidateBoxes(boxes); - - // threshold scores - std::vector keep_indices; - std::vector keep_scores; - ApplyThreshold(scores, score_threshold, &keep_scores, &keep_indices); - - std::vector sorted_indices; - DecreasingArgSort(keep_scores, &sorted_indices); - - const int num_boxes = keep_scores.size(); - const int output_size = std::min(num_boxes, max_detection); - std::vector active(num_boxes, true); - selected->clear(); - int num_active = active.size(); - for (int i = 0; i < num_boxes; ++i) { - if (num_active == 0 || selected->size() >= output_size) break; - if (active[i]) { - selected->push_back(keep_indices[sorted_indices[i]]); - active[i] = false; - num_active--; - } else { - continue; - } - for (int j = i + 1; j < num_boxes; ++j) { - if (active[j]) { - float iou = ComputeIOU(boxes, keep_indices[sorted_indices[i]], - keep_indices[sorted_indices[j]]); - if (iou > iou_threshold) { - active[j] = false; - num_active--; - } - } - } - } -} - -void NormalizeDetectionBoxes(const int width, const int height, - DetectionResults* boxes) { - for (auto& det : *boxes->mutable_detection()) { - auto *box = det.mutable_box(); - box->set_ymin(0, box->ymin(0) / height); - box->set_ymax(0, box->ymax(0) / height); - box->set_xmin(0, box->xmin(0) / width); - box->set_xmax(0, box->xmax(0) / width); - const int num_keypoints = box->keypoint_y_size(); - for (int i = 0; i < num_keypoints; ++i) { - box->set_keypoint_y(i, box->keypoint_y(i) / height); - box->set_keypoint_x(i, box->keypoint_x(i) / width); - } - } -} - -void DenormalizeDetectionBoxes(const int width, const int height, - DetectionResults* boxes) { - for (auto& det : *boxes->mutable_detection()) { - auto* box = det.mutable_box(); - box->set_ymin(0, box->ymin(0) * (height - 1)); - box->set_ymax(0, box->ymax(0) * (height - 1)); - box->set_xmin(0, box->xmin(0) * (width - 1)); - box->set_xmax(0, box->xmax(0) * (width - 1)); - const int num_keypoints = box->keypoint_y_size(); - for (int i = 0; i < num_keypoints; ++i) { - box->set_keypoint_y(i, box->keypoint_y(i) * (height - 1)); - box->set_keypoint_x(i, box->keypoint_x(i) * (width - 1)); - } - } -} - -void ClampBoxCoordinates(DetectionResults* boxes) { - for (auto& detection : *boxes->mutable_detection()) { - auto* box = detection.mutable_box(); - box->set_ymin(0, std::max(0.f, box->ymin(0))); - box->set_ymax(0, std::min(1.f, box->ymax(0))); - box->set_xmin(0, std::max(0.f, box->xmin(0))); - box->set_xmax(0, std::min(1.f, box->xmax(0))); - } -} - -bool GenerateSsdAnchors(const AnchorGenerationOptions& options, - CenterSizeEncoding* anchors) { - const int base_anchor_width = options.base_anchor_width(); - const int base_anchor_height = options.base_anchor_height(); - const float min_anchor_scale = options.min_anchor_scale(); - const float max_anchor_scale = options.max_anchor_scale(); - - const float* aspect_ratios_ptr = options.anchor_aspect_ratios().data(); - const int num_aspect_ratios = options.anchor_aspect_ratios_size(); - const std::vector anchor_aspect_ratios( - aspect_ratios_ptr, aspect_ratios_ptr + num_aspect_ratios); - - const int* strides_ptr = options.anchor_strides().data(); - const int num_strides = options.anchor_strides_size(); - const std::vector anchor_strides(strides_ptr, strides_ptr + num_strides); - - // Must set both image width and height or neither - CHECK_EQ(options.has_image_width(), options.has_image_height()); - - if (options.has_image_width() && options.has_image_height()) { - const int* offsets_ptr = options.anchor_offsets().data(); - const int num_offsets = options.anchor_offsets_size(); - const std::vector anchor_offsets(offsets_ptr, - offsets_ptr + num_offsets); - return GenerateSsdAnchors( - options.image_width(), options.image_height(), base_anchor_width, - base_anchor_height, min_anchor_scale, max_anchor_scale, - anchor_aspect_ratios, anchor_strides, anchor_offsets, anchors); - } - return GenerateSsdAnchors(base_anchor_width, base_anchor_height, - min_anchor_scale, max_anchor_scale, - anchor_aspect_ratios, anchor_strides, anchors); -} - -bool GenerateSsdAnchors(int input_width, int input_height, float min_scale, - float max_scale, - const std::vector& aspect_ratios, - const std::vector& anchor_strides, - CenterSizeEncoding* anchors) { - int num_layers = anchor_strides.size(); - std::vector anchor_offsets(num_layers); - for (int i = 0; i < num_layers; ++i) { - anchor_offsets[i] = (anchor_strides[i] + 1) / 2; - } - return GenerateSsdAnchors(input_width, - input_height, - input_width, - input_height, - min_scale, - max_scale, - aspect_ratios, - anchor_strides, - anchor_offsets, - anchors); -} - -bool GenerateSsdAnchors(int input_width, int input_height, - int base_anchor_width, int base_anchor_height, - float min_scale, float max_scale, - const std::vector& aspect_ratios, - const std::vector& anchor_strides, - const std::vector& anchor_offsets, - CenterSizeEncoding* anchors) { - constexpr float kSqrt2 = 1.414213562f; - int num_layers = anchor_strides.size(); - if (num_layers != anchor_offsets.size()) { - LOG(ERROR) << absl::StrCat("The size of anchor strides (", - anchor_strides.size(), - ") and anchor " - "offsets (", - anchor_offsets.size(), ") must be the same."); - return false; - } - std::vector scales(num_layers); - // Populate scales. - for (int i = 0; i < num_layers; ++i) { - scales[i] = min_scale + (max_scale - min_scale) * i / (num_layers - 1); - } - // Populate square roots of aspect ratios. - int num_aspect_ratios = aspect_ratios.size(); - std::vector sqrt_aspect_ratios(num_aspect_ratios); - for (int i = 0; i < num_aspect_ratios; ++i) { - sqrt_aspect_ratios[i] = std::sqrt(aspect_ratios[i]); - } - // Generate anchors. - float normalized_width = static_cast(base_anchor_width) / input_width; - float normalized_height = - static_cast(base_anchor_height) / input_height; - anchors->Clear(); - for (int i = 0; i < num_layers; ++i) { - float scale = scales[i]; - float next_scale; - if (i == num_layers - 1) { - next_scale = 1.0; - } else { - next_scale = scales[i + 1]; - } - float interpolated_scale = std::sqrt(scale * next_scale); - float normalized_scale_width = scale * normalized_width; - float normalized_scale_height = scale * normalized_height; - int anchor_map_height = - (input_height + anchor_strides[i] - 1) / anchor_strides[i]; - int anchor_map_width = - (input_width + anchor_strides[i] - 1) / anchor_strides[i]; - for (int anchor_idx_y = 0; anchor_idx_y < anchor_map_height; - ++anchor_idx_y) { - float y = static_cast( - anchor_offsets[i] + anchor_strides[i] * anchor_idx_y) / input_height; - for (int anchor_idx_x = 0; anchor_idx_x < anchor_map_width; - ++anchor_idx_x) { - float x = static_cast( - anchor_offsets[i] + anchor_strides[i] * anchor_idx_x) / input_width; - if (i == 0) { - // Scale: 0.1, Aspect Ratio: 1.0 - anchors->add_x(x); - anchors->add_y(y); - anchors->add_w(0.1 * normalized_width); - anchors->add_h(0.1 * normalized_height); - // Scale: scale, Aspect Ratio: 2.0 - anchors->add_x(x); - anchors->add_y(y); - anchors->add_w(normalized_scale_width * kSqrt2); - anchors->add_h(normalized_scale_height / kSqrt2); - // Scale: scale, Aspect Ratio: 0.5 - anchors->add_x(x); - anchors->add_y(y); - anchors->add_w(normalized_scale_width / kSqrt2); - anchors->add_h(normalized_scale_height * kSqrt2); - continue; - } - for (int j = 0; j < num_aspect_ratios; ++j) { - // Scale: scale, Aspect Ratio: aspect_ratio - anchors->add_x(x); - anchors->add_y(y); - anchors->add_w(normalized_scale_width * sqrt_aspect_ratios[j]); - anchors->add_h(normalized_scale_height / sqrt_aspect_ratios[j]); - } - // Interpolated anchors - anchors->add_x(x); - anchors->add_y(y); - anchors->add_w(interpolated_scale * normalized_width); - anchors->add_h(interpolated_scale * normalized_height); - } - } - } - return true; -} - -} // namespace tflite -} // namespace lstm_object_detection diff --git a/research/lstm_object_detection/tflite/utils/ssd_utils.h b/research/lstm_object_detection/tflite/utils/ssd_utils.h deleted file mode 100644 index a8efc00d3ea..00000000000 --- a/research/lstm_object_detection/tflite/utils/ssd_utils.h +++ /dev/null @@ -1,119 +0,0 @@ -/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -==============================================================================*/ - -#ifndef TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_SSD_UTILS_H_ -#define TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_SSD_UTILS_H_ - -#include "protos/anchor_generation_options.pb.h" -#include "protos/box_encodings.pb.h" -#include "protos/detections.pb.h" - -namespace lstm_object_detection { -namespace tflite { - -// Decodes bounding boxes using CenterSizeOffsetCoder given network -// predictions and anchor encodings. -void DecodeCenterSizeBoxes(const protos::CenterSizeEncoding& predictions, - const protos::CenterSizeEncoding& anchors, - const protos::CenterSizeOffsetCoder& coder, - protos::BoxCornerEncoding* decoded_boxes); - -// Decodes bounding boxes using BoxCornerOffsetCoder given network -// predictions and anchor encodings. -void DecodeBoxCornerBoxes(const protos::BoxCornerEncoding& predictions, - const protos::CenterSizeEncoding& anchors, - const protos::BoxCornerOffsetCoder& coder, - protos::BoxCornerEncoding* decoded_boxes); - -// Computes IOU overlap between two bounding boxes. -float ComputeIOU(const protos::BoxCornerEncoding& boxes, const int i, - const int j); - -// Performs Non-max suppression (multi-class) on a list of bounding boxes -// and prediction scores. -void NonMaxSuppressionMultiClass(const protos::BoxCornerEncoding& boxes, - const std::vector& scores, - const int num_classes, - const int max_detection_per_class, - const float score_threshold, - const float iou_threshold, - protos::DetectionResults* detections); - -// A fast (but not exact) version of non-max suppression (multi-class). -// Instead of computing per class non-max suppression, anchor-wise class -// maximum is computed on a list of bounding boxes and scores. This means -// that different classes can suppress each other. -void NonMaxSuppressionMultiClassFast( - const protos::BoxCornerEncoding& boxes, const std::vector& scores, - const int num_classes, const int max_detection, const int max_category, - const float score_threshold, const float iou_threshold, - protos::DetectionResults* detections); - -// Similar to NonMaxSuppressionMultiClassFast, but restricts the results to -// the provided list of class indices. This effectively filters out any class -// whose index is not in this allowlist. -void NonMaxSuppressionMultiClassRestrict( - std::vector restricted_class_indices, - const protos::BoxCornerEncoding& boxes, const std::vector& scores, - const int num_classes, const int max_detection, const int max_category, - const float score_threshold, const float iou_threshold, - protos::DetectionResults* detections); - -// Performs Non-max suppression (single class) on a list of bounding boxes -// and scores. The function implements a modified version of: -// third_party/tensorflow/core/kernels/non_max_suppression_op.cc -void NonMaxSuppression(const protos::BoxCornerEncoding& boxes, - const std::vector& scores, - const int max_detection, const float score_threshold, - const float iou_threshold, - std::vector* selected_indices); - -// Normalizes output bounding boxes such that the coordinates are in [0, 1]. -void NormalizeDetectionBoxes(const int width, const int height, - protos::DetectionResults* boxes); - -// Denormalizes output bounding boxes so that the coordinates are scaled to -// the absolute width and height. -void DenormalizeDetectionBoxes(const int width, const int height, - protos::DetectionResults* boxes); - -// Clamps detection box coordinates to be between [0, 1]. -void ClampBoxCoordinates(protos::DetectionResults* boxes); - -// Generates SSD anchors for the given input and anchor parameters. These -// methods generate the anchors described in https://arxiv.org/abs/1512.02325 -// and is similar to the anchor generation logic in -// //third_party/tensorflow_models/ -// object_detection/anchor_generators/multiple_grid_anchor_generator.py. -bool GenerateSsdAnchors(int input_width, int input_height, float min_scale, - float max_scale, - const std::vector& aspect_ratios, - const std::vector& anchor_strides, - protos::CenterSizeEncoding* anchors); - -bool GenerateSsdAnchors(int input_width, int input_height, - int base_anchor_width, int base_anchor_height, - float min_scale, float max_scale, - const std::vector& aspect_ratios, - const std::vector& anchor_strides, - const std::vector& anchor_offsets, - protos::CenterSizeEncoding* anchors); - -bool GenerateSsdAnchors(const protos::AnchorGenerationOptions& options, - protos::CenterSizeEncoding* anchors); -} // namespace tflite -} // namespace lstm_object_detection - -#endif // TENSORFLOW_MODELS_LSTM_OBJECT_DETECTION_TFLITE_UTILS_SSD_UTILS_H_ diff --git a/research/lstm_object_detection/train.py b/research/lstm_object_detection/train.py deleted file mode 100644 index 7a3dfbc5d38..00000000000 --- a/research/lstm_object_detection/train.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Training executable for detection models. - -This executable is used to train DetectionModels. There are two ways of -configuring the training job: - -1) A single pipeline_pb2.TrainEvalPipelineConfig configuration file -can be specified by --pipeline_config_path. - -Example usage: - ./train \ - --logtostderr \ - --train_dir=path/to/train_dir \ - --pipeline_config_path=pipeline_config.pbtxt - -2) Three configuration files can be provided: a model_pb2.DetectionModel -configuration file to define what type of DetectionModel is being trained, an -input_reader_pb2.InputReader file to specify what training data will be used and -a train_pb2.TrainConfig file to configure training parameters. - -Example usage: - ./train \ - --logtostderr \ - --train_dir=path/to/train_dir \ - --model_config_path=model_config.pbtxt \ - --train_config_path=train_config.pbtxt \ - --input_config_path=train_input_config.pbtxt - -""" - -import functools -import json -import os -from absl import flags -import tensorflow.compat.v1 as tf -from lstm_object_detection import model_builder -from lstm_object_detection import trainer -from lstm_object_detection.inputs import seq_dataset_builder -from lstm_object_detection.utils import config_util -from object_detection.builders import preprocessor_builder - -flags.DEFINE_string('master', '', 'Name of the TensorFlow master to use.') -flags.DEFINE_integer('task', 0, 'task id') -flags.DEFINE_integer('num_clones', 1, 'Number of clones to deploy per worker.') -flags.DEFINE_boolean( - 'clone_on_cpu', False, - 'Force clones to be deployed on CPU. Note that even if ' - 'set to False (allowing ops to run on gpu), some ops may ' - 'still be run on the CPU if they have no GPU kernel.') -flags.DEFINE_integer('worker_replicas', 1, 'Number of worker+trainer ' - 'replicas.') -flags.DEFINE_integer( - 'ps_tasks', 0, 'Number of parameter server tasks. If None, does not use ' - 'a parameter server.') -flags.DEFINE_string( - 'train_dir', '', - 'Directory to save the checkpoints and training summaries.') - -flags.DEFINE_string( - 'pipeline_config_path', '', - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file. If provided, other configs are ignored') - -flags.DEFINE_string('train_config_path', '', - 'Path to a train_pb2.TrainConfig config file.') -flags.DEFINE_string('input_config_path', '', - 'Path to an input_reader_pb2.InputReader config file.') -flags.DEFINE_string('model_config_path', '', - 'Path to a model_pb2.DetectionModel config file.') - -FLAGS = flags.FLAGS - - -def main(_): - assert FLAGS.train_dir, '`train_dir` is missing.' - if FLAGS.task == 0: - tf.gfile.MakeDirs(FLAGS.train_dir) - if FLAGS.pipeline_config_path: - configs = config_util.get_configs_from_pipeline_file( - FLAGS.pipeline_config_path) - if FLAGS.task == 0: - tf.gfile.Copy( - FLAGS.pipeline_config_path, - os.path.join(FLAGS.train_dir, 'pipeline.config'), - overwrite=True) - else: - configs = config_util.get_configs_from_multiple_files( - model_config_path=FLAGS.model_config_path, - train_config_path=FLAGS.train_config_path, - train_input_config_path=FLAGS.input_config_path) - if FLAGS.task == 0: - for name, config in [('model.config', FLAGS.model_config_path), - ('train.config', FLAGS.train_config_path), - ('input.config', FLAGS.input_config_path)]: - tf.gfile.Copy( - config, os.path.join(FLAGS.train_dir, name), overwrite=True) - - model_config = configs['model'] - lstm_config = configs['lstm_model'] - train_config = configs['train_config'] - input_config = configs['train_input_config'] - - model_fn = functools.partial( - model_builder.build, - model_config=model_config, - lstm_config=lstm_config, - is_training=True) - - def get_next(config, model_config, lstm_config, unroll_length): - data_augmentation_options = [ - preprocessor_builder.build(step) - for step in train_config.data_augmentation_options - ] - return seq_dataset_builder.build( - config, - model_config, - lstm_config, - unroll_length, - data_augmentation_options, - batch_size=train_config.batch_size) - - create_input_dict_fn = functools.partial(get_next, input_config, model_config, - lstm_config, - lstm_config.train_unroll_length) - - env = json.loads(os.environ.get('TF_CONFIG', '{}')) - cluster_data = env.get('cluster', None) - cluster = tf.train.ClusterSpec(cluster_data) if cluster_data else None - task_data = env.get('task', None) or {'type': 'master', 'index': 0} - task_info = type('TaskSpec', (object,), task_data) - - # Parameters for a single worker. - ps_tasks = 0 - worker_replicas = 1 - worker_job_name = 'lonely_worker' - task = 0 - is_chief = True - master = '' - - if cluster_data and 'worker' in cluster_data: - # Number of total worker replicas include "worker"s and the "master". - worker_replicas = len(cluster_data['worker']) + 1 - if cluster_data and 'ps' in cluster_data: - ps_tasks = len(cluster_data['ps']) - - if worker_replicas > 1 and ps_tasks < 1: - raise ValueError('At least 1 ps task is needed for distributed training.') - - if worker_replicas >= 1 and ps_tasks > 0: - # Set up distributed training. - server = tf.train.Server( - tf.train.ClusterSpec(cluster), - protocol='grpc', - job_name=task_info.type, - task_index=task_info.index) - if task_info.type == 'ps': - server.join() - return - - worker_job_name = '%s/task:%d' % (task_info.type, task_info.index) - task = task_info.index - is_chief = (task_info.type == 'master') - master = server.target - - trainer.train(create_input_dict_fn, model_fn, train_config, master, task, - FLAGS.num_clones, worker_replicas, FLAGS.clone_on_cpu, ps_tasks, - worker_job_name, is_chief, FLAGS.train_dir) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/lstm_object_detection/trainer.py b/research/lstm_object_detection/trainer.py deleted file mode 100644 index 17ae96c8f2c..00000000000 --- a/research/lstm_object_detection/trainer.py +++ /dev/null @@ -1,414 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Detection model trainer. - -This file provides a generic training method that can be used to train a -DetectionModel. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -import six -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.builders import optimizer_builder -from object_detection.core import standard_fields as fields -from object_detection.utils import ops as util_ops -from object_detection.utils import variables_helper -from deployment import model_deploy - - -def create_input_queue(create_tensor_dict_fn): - """Sets up reader, prefetcher and returns input queue. - - Args: - create_tensor_dict_fn: function to create tensor dictionary. - - Returns: - all_dict: A dictionary holds tensors for images, boxes, and targets. - """ - tensor_dict = create_tensor_dict_fn() - all_dict = {} - - num_images = len(tensor_dict[fields.InputDataFields.image]) - all_dict['batch'] = tensor_dict['batch'] - del tensor_dict['batch'] - - for i in range(num_images): - suffix = str(i) - for key, val in tensor_dict.items(): - all_dict[key + suffix] = val[i] - - all_dict[fields.InputDataFields.image + suffix] = tf.to_float( - tf.expand_dims(all_dict[fields.InputDataFields.image + suffix], 0)) - - return all_dict - - -def get_inputs(input_queue, num_classes, merge_multiple_label_boxes=False): - """Dequeues batch and constructs inputs to object detection model. - - Args: - input_queue: BatchQueue object holding enqueued tensor_dicts. - num_classes: Number of classes. - merge_multiple_label_boxes: Whether to merge boxes with multiple labels - or not. Defaults to false. Merged boxes are represented with a single - box and a k-hot encoding of the multiple labels associated with the - boxes. - - Returns: - images: a list of 3-D float tensor of images. - image_keys: a list of string keys for the images. - locations: a list of tensors of shape [num_boxes, 4] containing the corners - of the groundtruth boxes. - classes: a list of padded one-hot tensors containing target classes. - masks: a list of 3-D float tensors of shape [num_boxes, image_height, - image_width] containing instance masks for objects if present in the - input_queue. Else returns None. - keypoints: a list of 3-D float tensors of shape [num_boxes, num_keypoints, - 2] containing keypoints for objects if present in the - input queue. Else returns None. - """ - read_data_list = input_queue - label_id_offset = 1 - - def extract_images_and_targets(read_data): - """Extract images and targets from the input dict.""" - suffix = 0 - - images = [] - keys = [] - locations = [] - classes = [] - masks = [] - keypoints = [] - - while fields.InputDataFields.image + str(suffix) in read_data: - image = read_data[fields.InputDataFields.image + str(suffix)] - key = '' - if fields.InputDataFields.source_id in read_data: - key = read_data[fields.InputDataFields.source_id + str(suffix)] - location_gt = ( - read_data[fields.InputDataFields.groundtruth_boxes + str(suffix)]) - classes_gt = tf.cast( - read_data[fields.InputDataFields.groundtruth_classes + str(suffix)], - tf.int32) - classes_gt -= label_id_offset - masks_gt = read_data.get( - fields.InputDataFields.groundtruth_instance_masks + str(suffix)) - keypoints_gt = read_data.get( - fields.InputDataFields.groundtruth_keypoints + str(suffix)) - - if merge_multiple_label_boxes: - location_gt, classes_gt, _ = util_ops.merge_boxes_with_multiple_labels( - location_gt, classes_gt, num_classes) - else: - classes_gt = util_ops.padded_one_hot_encoding( - indices=classes_gt, depth=num_classes, left_pad=0) - - # Batch read input data and groundtruth. Images and locations, classes by - # default should have the same number of items. - images.append(image) - keys.append(key) - locations.append(location_gt) - classes.append(classes_gt) - masks.append(masks_gt) - keypoints.append(keypoints_gt) - - suffix += 1 - - return (images, keys, locations, classes, masks, keypoints) - - return extract_images_and_targets(read_data_list) - - -def _create_losses(input_queue, create_model_fn, train_config): - """Creates loss function for a DetectionModel. - - Args: - input_queue: BatchQueue object holding enqueued tensor_dicts. - create_model_fn: A function to create the DetectionModel. - train_config: a train_pb2.TrainConfig protobuf. - """ - - detection_model = create_model_fn() - (images, _, groundtruth_boxes_list, groundtruth_classes_list, - groundtruth_masks_list, groundtruth_keypoints_list) = get_inputs( - input_queue, detection_model.num_classes, - train_config.merge_multiple_label_boxes) - - preprocessed_images = [] - true_image_shapes = [] - for image in images: - resized_image, true_image_shape = detection_model.preprocess(image) - preprocessed_images.append(resized_image) - true_image_shapes.append(true_image_shape) - - images = tf.concat(preprocessed_images, 0) - true_image_shapes = tf.concat(true_image_shapes, 0) - - if any(mask is None for mask in groundtruth_masks_list): - groundtruth_masks_list = None - if any(keypoints is None for keypoints in groundtruth_keypoints_list): - groundtruth_keypoints_list = None - - detection_model.provide_groundtruth( - groundtruth_boxes_list, groundtruth_classes_list, groundtruth_masks_list, - groundtruth_keypoints_list) - prediction_dict = detection_model.predict(images, true_image_shapes, - input_queue['batch']) - - losses_dict = detection_model.loss(prediction_dict, true_image_shapes) - for loss_tensor in losses_dict.values(): - tf.losses.add_loss(loss_tensor) - - -def get_restore_checkpoint_ops(restore_checkpoints, detection_model, - train_config): - """Restore checkpoint from saved checkpoints. - - Args: - restore_checkpoints: loaded checkpoints. - detection_model: Object detection model built from config file. - train_config: a train_pb2.TrainConfig protobuf. - - Returns: - restorers: A list ops to init the model from checkpoints. - - """ - restorers = [] - vars_restored = [] - for restore_checkpoint in restore_checkpoints: - var_map = detection_model.restore_map( - fine_tune_checkpoint_type=train_config.fine_tune_checkpoint_type) - available_var_map = ( - variables_helper.get_variables_available_in_checkpoint( - var_map, restore_checkpoint)) - for var_name, var in six.iteritems(available_var_map): - if var in vars_restored: - tf.logging.info('Variable %s contained in multiple checkpoints', - var.op.name) - del available_var_map[var_name] - else: - vars_restored.append(var) - - # Initialize from ExponentialMovingAverages if possible. - available_ema_var_map = {} - ckpt_reader = tf.train.NewCheckpointReader(restore_checkpoint) - ckpt_vars_to_shape_map = ckpt_reader.get_variable_to_shape_map() - for var_name, var in six.iteritems(available_var_map): - var_name_ema = var_name + '/ExponentialMovingAverage' - if var_name_ema in ckpt_vars_to_shape_map: - available_ema_var_map[var_name_ema] = var - else: - available_ema_var_map[var_name] = var - available_var_map = available_ema_var_map - init_saver = tf.train.Saver(available_var_map) - if list(available_var_map.keys()): - restorers.append(init_saver) - else: - tf.logging.info('WARNING: Checkpoint %s has no restorable variables', - restore_checkpoint) - - return restorers - - -def train(create_tensor_dict_fn, - create_model_fn, - train_config, - master, - task, - num_clones, - worker_replicas, - clone_on_cpu, - ps_tasks, - worker_job_name, - is_chief, - train_dir, - graph_hook_fn=None): - """Training function for detection models. - - Args: - create_tensor_dict_fn: a function to create a tensor input dictionary. - create_model_fn: a function that creates a DetectionModel and generates - losses. - train_config: a train_pb2.TrainConfig protobuf. - master: BNS name of the TensorFlow master to use. - task: The task id of this training instance. - num_clones: The number of clones to run per machine. - worker_replicas: The number of work replicas to train with. - clone_on_cpu: True if clones should be forced to run on CPU. - ps_tasks: Number of parameter server tasks. - worker_job_name: Name of the worker job. - is_chief: Whether this replica is the chief replica. - train_dir: Directory to write checkpoints and training summaries to. - graph_hook_fn: Optional function that is called after the training graph is - completely built. This is helpful to perform additional changes to the - training graph such as optimizing batchnorm. The function should modify - the default graph. - """ - - detection_model = create_model_fn() - - with tf.Graph().as_default(): - # Build a configuration specifying multi-GPU and multi-replicas. - deploy_config = model_deploy.DeploymentConfig( - num_clones=num_clones, - clone_on_cpu=clone_on_cpu, - replica_id=task, - num_replicas=worker_replicas, - num_ps_tasks=ps_tasks, - worker_job_name=worker_job_name) - - # Place the global step on the device storing the variables. - with tf.device(deploy_config.variables_device()): - global_step = slim.create_global_step() - - with tf.device(deploy_config.inputs_device()): - input_queue = create_input_queue(create_tensor_dict_fn) - - # Gather initial summaries. - # TODO(rathodv): See if summaries can be added/extracted from global tf - # collections so that they don't have to be passed around. - summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) - global_summaries = set([]) - - model_fn = functools.partial( - _create_losses, - create_model_fn=create_model_fn, - train_config=train_config) - clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) - first_clone_scope = clones[0].scope - - # Gather update_ops from the first clone. These contain, for example, - # the updates for the batch_norm variables created by model_fn. - update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) - - with tf.device(deploy_config.optimizer_device()): - training_optimizer, optimizer_summary_vars = optimizer_builder.build( - train_config.optimizer) - for var in optimizer_summary_vars: - tf.summary.scalar(var.op.name, var) - - sync_optimizer = None - if train_config.sync_replicas: - training_optimizer = tf.train.SyncReplicasOptimizer( - training_optimizer, - replicas_to_aggregate=train_config.replicas_to_aggregate, - total_num_replicas=train_config.worker_replicas) - sync_optimizer = training_optimizer - - # Create ops required to initialize the model from a given checkpoint. - init_fn = None - if train_config.fine_tune_checkpoint: - restore_checkpoints = [ - path.strip() for path in train_config.fine_tune_checkpoint.split(',') - ] - - restorers = get_restore_checkpoint_ops(restore_checkpoints, - detection_model, train_config) - - def initializer_fn(sess): - for i, restorer in enumerate(restorers): - restorer.restore(sess, restore_checkpoints[i]) - - init_fn = initializer_fn - - with tf.device(deploy_config.optimizer_device()): - regularization_losses = ( - None if train_config.add_regularization_loss else []) - total_loss, grads_and_vars = model_deploy.optimize_clones( - clones, - training_optimizer, - regularization_losses=regularization_losses) - total_loss = tf.check_numerics(total_loss, 'LossTensor is inf or nan.') - - # Optionally multiply bias gradients by train_config.bias_grad_multiplier. - if train_config.bias_grad_multiplier: - biases_regex_list = ['.*/biases'] - grads_and_vars = variables_helper.multiply_gradients_matching_regex( - grads_and_vars, - biases_regex_list, - multiplier=train_config.bias_grad_multiplier) - - # Optionally clip gradients - if train_config.gradient_clipping_by_norm > 0: - with tf.name_scope('clip_grads'): - grads_and_vars = slim.learning.clip_gradient_norms( - grads_and_vars, train_config.gradient_clipping_by_norm) - - moving_average_variables = slim.get_model_variables() - variable_averages = tf.train.ExponentialMovingAverage(0.9999, global_step) - update_ops.append(variable_averages.apply(moving_average_variables)) - - # Create gradient updates. - grad_updates = training_optimizer.apply_gradients( - grads_and_vars, global_step=global_step) - update_ops.append(grad_updates) - update_op = tf.group(*update_ops, name='update_barrier') - with tf.control_dependencies([update_op]): - train_tensor = tf.identity(total_loss, name='train_op') - - if graph_hook_fn: - with tf.device(deploy_config.variables_device()): - graph_hook_fn() - - # Add summaries. - for model_var in slim.get_model_variables(): - global_summaries.add(tf.summary.histogram(model_var.op.name, model_var)) - for loss_tensor in tf.losses.get_losses(): - global_summaries.add(tf.summary.scalar(loss_tensor.op.name, loss_tensor)) - global_summaries.add( - tf.summary.scalar('TotalLoss', tf.losses.get_total_loss())) - - # Add the summaries from the first clone. These contain the summaries - # created by model_fn and either optimize_clones() or _gather_clone_loss(). - summaries |= set( - tf.get_collection(tf.GraphKeys.SUMMARIES, first_clone_scope)) - summaries |= set(tf.get_collection(tf.GraphKeys.SUMMARIES, 'critic_loss')) - summaries |= global_summaries - - # Merge all summaries together. - summary_op = tf.summary.merge(list(summaries), name='summary_op') - - # Soft placement allows placing on CPU ops without GPU implementation. - session_config = tf.ConfigProto( - allow_soft_placement=True, log_device_placement=False) - - # Save checkpoints regularly. - keep_checkpoint_every_n_hours = train_config.keep_checkpoint_every_n_hours - saver = tf.train.Saver( - keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours) - - slim.learning.train( - train_tensor, - logdir=train_dir, - master=master, - is_chief=is_chief, - session_config=session_config, - startup_delay_steps=train_config.startup_delay_steps, - init_fn=init_fn, - summary_op=summary_op, - number_of_steps=(train_config.num_steps - if train_config.num_steps else None), - save_summaries_secs=120, - sync_optimizer=sync_optimizer, - saver=saver) diff --git a/research/lstm_object_detection/utils/__init__.py b/research/lstm_object_detection/utils/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/lstm_object_detection/utils/config_util.py b/research/lstm_object_detection/utils/config_util.py deleted file mode 100644 index d46d2d703c4..00000000000 --- a/research/lstm_object_detection/utils/config_util.py +++ /dev/null @@ -1,106 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Added functionality to load from pipeline config for lstm framework.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from lstm_object_detection.protos import input_reader_google_pb2 # pylint: disable=unused-import -from lstm_object_detection.protos import pipeline_pb2 as internal_pipeline_pb2 -from object_detection.protos import pipeline_pb2 -from object_detection.utils import config_util - - -def get_configs_from_pipeline_file(pipeline_config_path): - """Reads configuration from a pipeline_pb2.TrainEvalPipelineConfig. - - Args: - pipeline_config_path: Path to pipeline_pb2.TrainEvalPipelineConfig text - proto. - - Returns: - Dictionary of configuration objects. Keys are `model`, `train_config`, - `train_input_config`, `eval_config`, `eval_input_config`, `lstm_model`. - Value are the corresponding config objects. - """ - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - with tf.gfile.GFile(pipeline_config_path, "r") as f: - proto_str = f.read() - text_format.Merge(proto_str, pipeline_config) - configs = config_util.get_configs_from_pipeline_file(pipeline_config_path) - if pipeline_config.HasExtension(internal_pipeline_pb2.lstm_model): - configs["lstm_model"] = pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model] - return configs - - -def create_pipeline_proto_from_configs(configs): - """Creates a pipeline_pb2.TrainEvalPipelineConfig from configs dictionary. - - This function nearly performs the inverse operation of - get_configs_from_pipeline_file(). Instead of returning a file path, it returns - a `TrainEvalPipelineConfig` object. - - Args: - configs: Dictionary of configs. See get_configs_from_pipeline_file(). - - Returns: - A fully populated pipeline_pb2.TrainEvalPipelineConfig. - """ - pipeline_config = config_util.create_pipeline_proto_from_configs(configs) - if "lstm_model" in configs: - pipeline_config.Extensions[internal_pipeline_pb2.lstm_model].CopyFrom( - configs["lstm_model"]) - return pipeline_config - - -def get_configs_from_multiple_files(model_config_path="", - train_config_path="", - train_input_config_path="", - eval_config_path="", - eval_input_config_path="", - lstm_config_path=""): - """Reads training configuration from multiple config files. - - Args: - model_config_path: Path to model_pb2.DetectionModel. - train_config_path: Path to train_pb2.TrainConfig. - train_input_config_path: Path to input_reader_pb2.InputReader. - eval_config_path: Path to eval_pb2.EvalConfig. - eval_input_config_path: Path to input_reader_pb2.InputReader. - lstm_config_path: Path to pipeline_pb2.LstmModel. - - Returns: - Dictionary of configuration objects. Keys are `model`, `train_config`, - `train_input_config`, `eval_config`, `eval_input_config`, `lstm_model`. - Key/Values are returned only for valid (non-empty) strings. - """ - configs = config_util.get_configs_from_multiple_files( - model_config_path=model_config_path, - train_config_path=train_config_path, - train_input_config_path=train_input_config_path, - eval_config_path=eval_config_path, - eval_input_config_path=eval_input_config_path) - if lstm_config_path: - lstm_config = internal_pipeline_pb2.LstmModel() - with tf.gfile.GFile(lstm_config_path, "r") as f: - text_format.Merge(f.read(), lstm_config) - configs["lstm_model"] = lstm_config - return configs diff --git a/research/lstm_object_detection/utils/config_util_test.py b/research/lstm_object_detection/utils/config_util_test.py deleted file mode 100644 index 0bcbc39b421..00000000000 --- a/research/lstm_object_detection/utils/config_util_test.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.utils.config_util.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from lstm_object_detection.protos import pipeline_pb2 as internal_pipeline_pb2 -from lstm_object_detection.utils import config_util -from object_detection.protos import pipeline_pb2 - - -def _write_config(config, config_path): - """Writes a config object to disk.""" - config_text = text_format.MessageToString(config) - with tf.gfile.Open(config_path, "wb") as f: - f.write(config_text) - - -class ConfigUtilTest(tf.test.TestCase): - - def test_get_configs_from_pipeline_file(self): - """Test that proto configs can be read from pipeline config file.""" - pipeline_config_path = os.path.join(self.get_temp_dir(), "pipeline.config") - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.model.ssd.num_classes = 10 - pipeline_config.train_config.batch_size = 32 - pipeline_config.train_input_reader.label_map_path = "path/to/label_map" - pipeline_config.eval_config.num_examples = 20 - pipeline_config.eval_input_reader.add().queue_capacity = 100 - - pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model].train_unroll_length = 5 - pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model].eval_unroll_length = 10 - - _write_config(pipeline_config, pipeline_config_path) - - configs = config_util.get_configs_from_pipeline_file(pipeline_config_path) - self.assertProtoEquals(pipeline_config.model, configs["model"]) - self.assertProtoEquals(pipeline_config.train_config, - configs["train_config"]) - self.assertProtoEquals(pipeline_config.train_input_reader, - configs["train_input_config"]) - self.assertProtoEquals(pipeline_config.eval_config, configs["eval_config"]) - self.assertProtoEquals(pipeline_config.eval_input_reader, - configs["eval_input_configs"]) - self.assertProtoEquals( - pipeline_config.Extensions[internal_pipeline_pb2.lstm_model], - configs["lstm_model"]) - - def test_create_pipeline_proto_from_configs(self): - """Tests that proto can be reconstructed from configs dictionary.""" - pipeline_config_path = os.path.join(self.get_temp_dir(), "pipeline.config") - - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.model.ssd.num_classes = 10 - pipeline_config.train_config.batch_size = 32 - pipeline_config.train_input_reader.label_map_path = "path/to/label_map" - pipeline_config.eval_config.num_examples = 20 - pipeline_config.eval_input_reader.add().queue_capacity = 100 - - pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model].train_unroll_length = 5 - pipeline_config.Extensions[ - internal_pipeline_pb2.lstm_model].eval_unroll_length = 10 - _write_config(pipeline_config, pipeline_config_path) - - configs = config_util.get_configs_from_pipeline_file(pipeline_config_path) - pipeline_config_reconstructed = ( - config_util.create_pipeline_proto_from_configs(configs)) - self.assertEqual(pipeline_config, pipeline_config_reconstructed) - - -if __name__ == "__main__": - tf.test.main() diff --git a/research/marco/Automated_Marco.py b/research/marco/Automated_Marco.py deleted file mode 100644 index d0b50d336a9..00000000000 --- a/research/marco/Automated_Marco.py +++ /dev/null @@ -1,72 +0,0 @@ -#!/usr/bin/python -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -import tensorflow as tf -import csv -import os -import argparse - - -""" -usage: -Processes all .jpg, .png, .bmp and .gif files found in the specified directory and its subdirectories. - --PATH ( Path to directory of images or path to directory with subdirectory of images). e.g Path/To/Directory/ - --Model_PATH path to the tensorflow model -""" - - -parser = argparse.ArgumentParser(description='Crystal Detection Program') - - -parser.add_argument('--PATH', type=str, help='path to image directory. Recursively finds all image files in directory and sub directories') # path to image directory or containing sub directories. -parser.add_argument('--MODEL_PATH', type=str, default='./savedmodel',help='the file path to the tensorflow model ') -args = vars(parser.parse_args()) -PATH = args['PATH'] -model_path = args['MODEL_PATH'] - - -crystal_images = [os.path.join(dp, f) for dp, dn, filenames in os.walk(PATH) for f in filenames if os.path.splitext(f)[1] in ['.jpg','png','bmp','gif']] -size = len(crystal_images) - -def load_images(file_list): - for i in file_list: - files = open(i,'rb') - yield {"image_bytes":[files.read()]},i - - - -iterator = load_images(crystal_images) - -with open(PATH +'results.csv', 'w') as csvfile: - Writer = csv.writer(csvfile, delimiter=' ',quotechar=' ', quoting=csv.QUOTE_MINIMAL) - - predicter= tf.contrib.predictor.from_saved_model(model_path) - dic = {} - - - k = 0 - for _ in range(size): - - data,name = next(iterator) - results = predicter(data) - - vals =results['scores'][0] - classes = results['classes'][0] - dictionary = dict(zip(classes,vals)) - - print('Image path: '+ name+' Crystal: '+str(dictionary[b'Crystals'])+' Other: '+ str(dictionary[b'Other'])+' Precipitate: '+ str(dictionary[b'Precipitate'])+' Clear: '+ str(dictionary[b'Clear'])) - Writer.writerow(['Image path: '+ name,'Crystal: '+str(dictionary[b'Crystals']),'Other: '+ str(dictionary[b'Other']),'Precipitate: '+ str(dictionary[b'Precipitate']),'Clear: '+ str(dictionary[b'Clear'])]) - diff --git a/research/marco/README.md b/research/marco/README.md deleted file mode 100644 index d6c0b15c976..00000000000 --- a/research/marco/README.md +++ /dev/null @@ -1,81 +0,0 @@ -![TensorFlow Requirement: 1.x](https://img.shields.io/badge/TensorFlow%20Requirement-1.x-brightgreen) -![TensorFlow 2 Not Supported](https://img.shields.io/badge/TensorFlow%202%20Not%20Supported-%E2%9C%95-red.svg) - -Automating the Evaluation of Crystallization Experiments -======================================================== - -This is a pretrained model described in the paper: - -[Classification of crystallization outcomes using deep convolutional neural networks](https://arxiv.org/abs/1803.10342). - -This model takes images of crystallization experiments as an input: - -crystal sample - -It classifies it as belonging to one of four categories: crystals, precipitate, clear, or 'others'. - -The model is a variant of [Inception-v3](https://arxiv.org/abs/1512.00567) trained on data from the [MARCO](http://marco.ccr.buffalo.edu) repository. - -Model ------ - -The model can be downloaded from: - -https://storage.googleapis.com/marco-168219-model/savedmodel.zip - -Example -------- - -1. Install TensorFlow and the [Google Cloud SDK](https://cloud.google.com/sdk/gcloud/). - -2. Download and unzip the model: - - ```bash - unzip savedmodel.zip - ``` - -3. A sample image can be downloaded from: - - https://storage.googleapis.com/marco-168219-model/002s_C6_ImagerDefaults_9.jpg - - Convert your image into a JSON request using: - - ```bash - python jpeg2json.py 002s_C6_ImagerDefaults_9.jpg > request.json - ``` - -4. To issue a prediction, run: - - ```bash - gcloud ml-engine local predict --model-dir=savedmodel --json-instances=request.json - ``` - -The request should return normalized scores for each class: - -
-CLASSES                                            SCORES
-[u'Crystals', u'Other', u'Precipitate', u'Clear']  [0.926338255405426, 0.026199858635663986, 0.026074528694152832, 0.021387407556176186]
-
- -CloudML Endpoint ----------------- - -The model can also be accessed on [Google CloudML](https://cloud.google.com/ml-engine/) by issuing: - -```bash -gcloud ml-engine predict --model marco_168219_model --json-instances request.json -``` - -Ask the author for access privileges to the CloudML instance. - -Note ----- - -`002s_C6_ImagerDefaults_9.jpg` is a sample from the -[MARCO](http://marco.ccr.buffalo.edu) repository, contributed to the dataset under the [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/) license. - -Author ------- - -[Vincent Vanhoucke](mailto:vanhoucke@google.com) (github: vincentvanhoucke) - diff --git a/research/marco/jpeg2json.py b/research/marco/jpeg2json.py deleted file mode 100644 index db795e05bef..00000000000 --- a/research/marco/jpeg2json.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/python -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""jpeg2json.py: Converts a JPEG image into a json request to CloudML. - -Usage: -python jpeg2json.py 002s_C6_ImagerDefaults_9.jpg > request.json - -See: -https://cloud.google.com/ml-engine/docs/concepts/prediction-overview#online_prediction_input_data -""" - -import base64 -import sys - - -def to_json(data): - return '{"image_bytes":{"b64": "%s"}}' % base64.b64encode(data) - - -if __name__ == '__main__': - file = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin - print(to_json(file.read())) diff --git a/research/marco/request.json b/research/marco/request.json deleted file mode 100644 index 338a00917b3..00000000000 --- a/research/marco/request.json +++ /dev/null @@ -1 +0,0 @@ -{"image_bytes":{"b64": "/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAPABQADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAEC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B/fEVz95rEsxJaVjz61gPMe5qJpfeh1EthcvcvTXe7r1qs8x9s1VeYCommrKVRspaFsyn1phmweTVJ5ielRF2JrJu4+YuPcHtVd5iT1JqHJpeeM80hCliaQHNCjmnChsQjAU085wKU00ZBIpXGN9aO49KdjvTggNIBmOeKCOhqZV60w/eoAYBSgHNOC54py0AJgdTR5nFOYflTT34oAjY5PNM705854pg5PvQA/IxxTCaOe1IRigAJNJk96OtNPBoAUnNGOODTcUvI6UwE7AdKUdaQg7qeOg70gF6dqOKKCSeo70AMoxznmnH0pO/NADD70o60EZGaVQfxoAUZ7Zp2MjmkApQOOKAEIoIp2KMZNAAOnSpFGDSDtUirwaAJY+RU7EhcUyEDb71OsYbn8qVgGQRb26Vq2toSen6UafbBsciun0uwVsA4oZaRnWWnFnG1evpXWaR4fLbWcVqaPpaggkCuusLNRjOKTZrGCKOn6UEQDb0rbt7PC/1q7BCqrwPwq9CgHQfhVRia2K9pahCMj3rRij65HHaiMDPGKmXjGRWti0AXHHfFPQelJt5BzUiD86pIaDpTwaQ9aVOfoKuwx4+gNAznnpQDnPvS9qaHcQjn2pp6c04/rSelMBjdM1Xl6HFWSOKrTcqRipZEilLiqkwOKtyfSqk2fTNIxkVH7ZNVpjVhz6iqkpOKlowkVH4bHrzT4+ue1MkxnJHFSR/hQzBmhb8gVoRHKjms63NX4SR0qTnkXoj2OfpVyI5z6VRgIBOTV2LmqZmkf/2Q=="}} diff --git a/research/nst_blogpost/4_Neural_Style_Transfer_with_Eager_Execution.ipynb b/research/nst_blogpost/4_Neural_Style_Transfer_with_Eager_Execution.ipynb deleted file mode 100644 index e91e17585f3..00000000000 --- a/research/nst_blogpost/4_Neural_Style_Transfer_with_Eager_Execution.ipynb +++ /dev/null @@ -1,1203 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "Neural Style Transfer with Eager Execution", - "version": "0.3.2", - "provenance": [], - "private_outputs": true, - "collapsed_sections": [], - "toc_visible": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "accelerator": "GPU" - }, - "cells": [ - { - "metadata": { - "id": "jo5PziEC4hWs", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "# Neural Style Transfer with tf.keras\n", - "\n", - "\n", - " \n", - " \n", - "
\n", - " Run in Google Colab\n", - " \n", - " View source on GitHub\n", - "
" - ] - }, - { - "metadata": { - "id": "aDyGj8DmXCJI", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Overview\n", - "\n", - "In this tutorial, we will learn how to use deep learning to compose images in the style of another image (ever wish you could paint like Picasso or Van Gogh?). This is known as **neural style transfer**! This is a technique outlined in [Leon A. Gatys' paper, A Neural Algorithm of Artistic Style](https://arxiv.org/abs/1508.06576), which is a great read, and you should definitely check it out. \n", - "\n", - "But, what is neural style transfer?\n", - "\n", - "Neural style transfer is an optimization technique used to take three images, a **content** image, a **style reference** image (such as an artwork by a famous painter), and the **input** image you want to style -- and blend them together such that the input image is transformed to look like the content image, but “painted” in the style of the style image.\n", - "\n", - "\n", - "For example, let’s take an image of this turtle and Katsushika Hokusai's *The Great Wave off Kanagawa*:\n", - "\n", - "\"Drawing\"\n", - "\"Drawing\"\n", - "\n", - "[Image of Green Sea Turtle](https://commons.wikimedia.org/wiki/File:Green_Sea_Turtle_grazing_seagrass.jpg)\n", - "-By P.Lindgren [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)], from Wikimedia Commons\n", - "\n", - "\n", - "Now how would it look like if Hokusai decided to paint the picture of this Turtle exclusively with this style? Something like this?\n", - "\n", - "\"Drawing\"\n", - "\n", - "Is this magic or just deep learning? Fortunately, this doesn’t involve any witchcraft: style transfer is a fun and interesting technique that showcases the capabilities and internal representations of neural networks. \n", - "\n", - "The principle of neural style transfer is to define two distance functions, one that describes how different the content of two images are , $L_{content}$, and one that describes the difference between two images in terms of their style, $L_{style}$. Then, given three images, a desired style image, a desired content image, and the input image (initialized with the content image), we try to transform the input image to minimize the content distance with the content image and its style distance with the style image. \n", - "In summary, we’ll take the base input image, a content image that we want to match, and the style image that we want to match. We’ll transform the base input image by minimizing the content and style distances (losses) with backpropagation, creating an image that matches the content of the content image and the style of the style image. \n", - "\n", - "### Specific concepts that will be covered:\n", - "In the process, we will build practical experience and develop intuition around the following concepts\n", - "\n", - "* **Eager Execution** - use TensorFlow's imperative programming environment that evaluates operations immediately \n", - " * [Learn more about eager execution](https://www.tensorflow.org/programmers_guide/eager)\n", - " * [See it in action](https://www.tensorflow.org/get_started/eager)\n", - "* ** Using [Functional API](https://keras.io/getting-started/functional-api-guide/) to define a model** - we'll build a subset of our model that will give us access to the necessary intermediate activations using the Functional API \n", - "* **Leveraging feature maps of a pretrained model** - Learn how to use pretrained models and their feature maps \n", - "* **Create custom training loops** - we'll examine how to set up an optimizer to minimize a given loss with respect to input parameters\n", - "\n", - "### We will follow the general steps to perform style transfer:\n", - "\n", - "1. Visualize data\n", - "2. Basic Preprocessing/preparing our data\n", - "3. Set up loss functions \n", - "4. Create model\n", - "5. Optimize for loss function\n", - "\n", - "**Audience:** This post is geared towards intermediate users who are comfortable with basic machine learning concepts. To get the most out of this post, you should: \n", - "* Read [Gatys' paper](https://arxiv.org/abs/1508.06576) - we'll explain along the way, but the paper will provide a more thorough understanding of the task\n", - "* [Understand reducing loss with gradient descent](https://developers.google.com/machine-learning/crash-course/reducing-loss/gradient-descent)\n", - "\n", - "**Time Estimated**: 30 min\n" - ] - }, - { - "metadata": { - "id": "U8ajP_u73s6m", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Setup\n", - "\n", - "### Download Images" - ] - }, - { - "metadata": { - "id": "riWE_b8k3s6o", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "import os\n", - "img_dir = '/tmp/nst'\n", - "if not os.path.exists(img_dir):\n", - " os.makedirs(img_dir)\n", - "!wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/d/d7/Green_Sea_Turtle_grazing_seagrass.jpg\n", - "!wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/0/0a/The_Great_Wave_off_Kanagawa.jpg\n", - "!wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/b/b4/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg\n", - "!wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/0/00/Tuebingen_Neckarfront.jpg\n", - "!wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/6/68/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg\n", - "!wget --quiet -P /tmp/nst/ https://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg/1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "eqxUicSPUOP6", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Import and configure modules" - ] - }, - { - "metadata": { - "id": "sc1OLbOWhPCO", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "import matplotlib.pyplot as plt\n", - "import matplotlib as mpl\n", - "mpl.rcParams['figure.figsize'] = (10,10)\n", - "mpl.rcParams['axes.grid'] = False\n", - "\n", - "import numpy as np\n", - "from PIL import Image\n", - "import time\n", - "import functools" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "RYEjlrYk3s6w", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "%tensorflow_version 1.x\n", - "import tensorflow as tf\n", - "\n", - "from tensorflow.keras.utils import image_dataset_from_directory as kp_image\n", - "from tensorflow.python.keras import models \n", - "from tensorflow.python.keras import losses\n", - "from tensorflow.python.keras import layers\n", - "from tensorflow.python.keras import backend as K" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "IOiGrIV1iERH", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "# Set up some global values here\n", - "content_path = '/tmp/nst/Green_Sea_Turtle_grazing_seagrass.jpg'\n", - "style_path = '/tmp/nst/The_Great_Wave_off_Kanagawa.jpg'" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "xE4Yt8nArTeR", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Visualize the input" - ] - }, - { - "metadata": { - "id": "3TLljcwv5qZs", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def load_img(path_to_img):\n", - " max_dim = 512\n", - " img = Image.open(path_to_img)\n", - " long = max(img.size)\n", - " scale = max_dim/long\n", - " img = img.resize((round(img.size[0]*scale), round(img.size[1]*scale)), Image.ANTIALIAS)\n", - " \n", - " img = kp_image.img_to_array(img)\n", - " \n", - " # We need to broadcast the image array such that it has a batch dimension \n", - " img = np.expand_dims(img, axis=0)\n", - " return img" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "vupl0CI18aAG", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def imshow(img, title=None):\n", - " # Remove the batch dimension\n", - " out = np.squeeze(img, axis=0)\n", - " # Normalize for display \n", - " out = out.astype('uint8')\n", - " plt.imshow(out)\n", - " if title is not None:\n", - " plt.title(title)\n", - " plt.imshow(out)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "2yAlRzJZrWM3", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "These are input content and style images. We hope to \"create\" an image with the content of our content image, but with the style of the style image. " - ] - }, - { - "metadata": { - "id": "_UWQmeEaiKkP", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "plt.figure(figsize=(10,10))\n", - "\n", - "content = load_img(content_path).astype('uint8')\n", - "style = load_img(style_path).astype('uint8')\n", - "\n", - "plt.subplot(1, 2, 1)\n", - "imshow(content, 'Content Image')\n", - "\n", - "plt.subplot(1, 2, 2)\n", - "imshow(style, 'Style Image')\n", - "plt.show()" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "7qMVNvEsK-_D", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Prepare the data\n", - "Let's create methods that will allow us to load and preprocess our images easily. We perform the same preprocessing process as are expected according to the VGG training process. VGG networks are trained on image with each channel normalized by `mean = [103.939, 116.779, 123.68]`and with channels BGR." - ] - }, - { - "metadata": { - "id": "hGwmTwJNmv2a", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def load_and_process_img(path_to_img):\n", - " img = load_img(path_to_img)\n", - " img = tf.keras.applications.vgg19.preprocess_input(img)\n", - " return img" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "xCgooqs6tAka", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "In order to view the outputs of our optimization, we are required to perform the inverse preprocessing step. Furthermore, since our optimized image may take its values anywhere between $- \\infty$ and $\\infty$, we must clip to maintain our values from within the 0-255 range. " - ] - }, - { - "metadata": { - "id": "mjzlKRQRs_y2", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def deprocess_img(processed_img):\n", - " x = processed_img.copy()\n", - " if len(x.shape) == 4:\n", - " x = np.squeeze(x, 0)\n", - " assert len(x.shape) == 3, (\"Input to deprocess image must be an image of \"\n", - " \"dimension [1, height, width, channel] or [height, width, channel]\")\n", - " if len(x.shape) != 3:\n", - " raise ValueError(\"Invalid input to deprocessing image\")\n", - " \n", - " # perform the inverse of the preprocessing step\n", - " x[:, :, 0] += 103.939\n", - " x[:, :, 1] += 116.779\n", - " x[:, :, 2] += 123.68\n", - " x = x[:, :, ::-1]\n", - "\n", - " x = np.clip(x, 0, 255).astype('uint8')\n", - " return x" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "GEwZ7FlwrjoZ", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Define content and style representations\n", - "In order to get both the content and style representations of our image, we will look at some intermediate layers within our model. As we go deeper into the model, these intermediate layers represent higher and higher order features. In this case, we are using the network architecture VGG19, a pretrained image classification network. These intermediate layers are necessary to define the representation of content and style from our images. For an input image, we will try to match the corresponding style and content target representations at these intermediate layers. \n", - "\n", - "#### Why intermediate layers?\n", - "\n", - "You may be wondering why these intermediate outputs within our pretrained image classification network allow us to define style and content representations. At a high level, this phenomenon can be explained by the fact that in order for a network to perform image classification (which our network has been trained to do), it must understand the image. This involves taking the raw image as input pixels and building an internal representation through transformations that turn the raw image pixels into a complex understanding of the features present within the image. This is also partly why convolutional neural networks are able to generalize well: they’re able to capture the invariances and defining features within classes (e.g., cats vs. dogs) that are agnostic to background noise and other nuisances. Thus, somewhere between where the raw image is fed in and the classification label is output, the model serves as a complex feature extractor; hence by accessing intermediate layers, we’re able to describe the content and style of input images. \n", - "\n", - "\n", - "Specifically we’ll pull out these intermediate layers from our network: \n" - ] - }, - { - "metadata": { - "id": "N4-8eUp_Kc-j", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "# Content layer where will pull our feature maps\n", - "content_layers = ['block5_conv2'] \n", - "\n", - "# Style layer we are interested in\n", - "style_layers = ['block1_conv1',\n", - " 'block2_conv1',\n", - " 'block3_conv1', \n", - " 'block4_conv1', \n", - " 'block5_conv1'\n", - " ]\n", - "\n", - "num_content_layers = len(content_layers)\n", - "num_style_layers = len(style_layers)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "Jt3i3RRrJiOX", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Build the Model \n", - "In this case, we load [VGG19](https://keras.io/applications/#vgg19), and feed in our input tensor to the model. This will allow us to extract the feature maps (and subsequently the content and style representations) of the content, style, and generated images.\n", - "\n", - "We use VGG19, as suggested in the paper. In addition, since VGG19 is a relatively simple model (compared with ResNet, Inception, etc) the feature maps actually work better for style transfer. " - ] - }, - { - "metadata": { - "id": "v9AnzEUU6hhx", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "In order to access the intermediate layers corresponding to our style and content feature maps, we get the corresponding outputs and using the Keras [**Functional API**](https://keras.io/getting-started/functional-api-guide/), we define our model with the desired output activations. \n", - "\n", - "With the Functional API defining a model simply involves defining the input and output: \n", - "\n", - "`model = Model(inputs, outputs)`" - ] - }, - { - "metadata": { - "id": "nfec6MuMAbPx", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def get_model():\n", - " \"\"\" Creates our model with access to intermediate layers. \n", - " \n", - " This function will load the VGG19 model and access the intermediate layers. \n", - " These layers will then be used to create a new model that will take input image\n", - " and return the outputs from these intermediate layers from the VGG model. \n", - " \n", - " Returns:\n", - " returns a keras model that takes image inputs and outputs the style and \n", - " content intermediate layers. \n", - " \"\"\"\n", - " # Load our model. We load pretrained VGG, trained on imagenet data\n", - " vgg = tf.keras.applications.vgg19.VGG19(include_top=False, weights='imagenet')\n", - " vgg.trainable = False\n", - " # Get output layers corresponding to style and content layers \n", - " style_outputs = [vgg.get_layer(name).output for name in style_layers]\n", - " content_outputs = [vgg.get_layer(name).output for name in content_layers]\n", - " model_outputs = style_outputs + content_outputs\n", - " # Build model \n", - " return models.Model(vgg.input, model_outputs)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "kl6eFGa7-OtV", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "In the above code snippet, we’ll load our pretrained image classification network. Then we grab the layers of interest as we defined earlier. Then we define a Model by setting the model’s inputs to an image and the outputs to the outputs of the style and content layers. In other words, we created a model that will take an input image and output the content and style intermediate layers! \n" - ] - }, - { - "metadata": { - "id": "vJdYvJTZ4bdS", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Define and create our loss functions (content and style distances)" - ] - }, - { - "metadata": { - "id": "F2Hcepii7_qh", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Content Loss" - ] - }, - { - "metadata": { - "id": "1FvH-gwXi4nq", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "Our content loss definition is actually quite simple. We’ll pass the network both the desired content image and our base input image. This will return the intermediate layer outputs (from the layers defined above) from our model. Then we simply take the euclidean distance between the two intermediate representations of those images. \n", - "\n", - "More formally, content loss is a function that describes the distance of content from our output image $x$ and our content image, $p$. Let $C_{nn}$ be a pre-trained deep convolutional neural network. Again, in this case we use [VGG19](https://keras.io/applications/#vgg19). Let $X$ be any image, then $C_{nn}(X)$ is the network fed by X. Let $F^l_{ij}(x) \\in C_{nn}(x)$ and $P^l_{ij}(p) \\in C_{nn}(p)$ describe the respective intermediate feature representation of the network with inputs $x$ and $p$ at layer $l$. Then we describe the content distance (loss) formally as: $$L^l_{content}(p, x) = \\sum_{i, j} (F^l_{ij}(x) - P^l_{ij}(p))^2$$\n", - "\n", - "We perform backpropagation in the usual way such that we minimize this content loss. We thus change the initial image until it generates a similar response in a certain layer (defined in content_layer) as the original content image.\n", - "\n", - "This can be implemented quite simply. Again it will take as input the feature maps at a layer L in a network fed by x, our input image, and p, our content image, and return the content distance.\n", - "\n" - ] - }, - { - "metadata": { - "id": "6KsbqPA8J9DY", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Computing content loss\n", - "We will actually add our content losses at each desired layer. This way, each iteration when we feed our input image through the model (which in eager is simply `model(input_image)`!) all the content losses through the model will be properly compute and because we are executing eagerly, all the gradients will be computed. " - ] - }, - { - "metadata": { - "id": "d2mf7JwRMkCd", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def get_content_loss(base_content, target):\n", - " return tf.reduce_mean(tf.square(base_content - target))" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "lGUfttK9F8d5", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Style Loss" - ] - }, - { - "metadata": { - "id": "I6XtkGK_YGD1", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "Computing style loss is a bit more involved, but follows the same principle, this time feeding our network the base input image and the style image. However, instead of comparing the raw intermediate outputs of the base input image and the style image, we instead compare the Gram matrices of the two outputs. \n", - "\n", - "Mathematically, we describe the style loss of the base input image, $x$, and the style image, $a$, as the distance between the style representation (the gram matrices) of these images. We describe the style representation of an image as the correlation between different filter responses given by the Gram matrix $G^l$, where $G^l_{ij}$ is the inner product between the vectorized feature map $i$ and $j$ in layer $l$. We can see that $G^l_{ij}$ generated over the feature map for a given image represents the correlation between feature maps $i$ and $j$. \n", - "\n", - "To generate a style for our base input image, we perform gradient descent from the content image to transform it into an image that matches the style representation of the original image. We do so by minimizing the mean squared distance between the feature correlation map of the style image and the input image. The contribution of each layer to the total style loss is described by\n", - "$$E_l = \\frac{1}{4N_l^2M_l^2} \\sum_{i,j}(G^l_{ij} - A^l_{ij})^2$$\n", - "\n", - "where $G^l_{ij}$ and $A^l_{ij}$ are the respective style representation in layer $l$ of $x$ and $a$. $N_l$ describes the number of feature maps, each of size $M_l = height * width$. Thus, the total style loss across each layer is \n", - "$$L_{style}(a, x) = \\sum_{l \\in L} w_l E_l$$\n", - "where we weight the contribution of each layer's loss by some factor $w_l$. In our case, we weight each layer equally ($w_l =\\frac{1}{|L|}$)" - ] - }, - { - "metadata": { - "id": "F21Hm61yLKk5", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Computing style loss\n", - "Again, we implement our loss as a distance metric . " - ] - }, - { - "metadata": { - "id": "N7MOqwKLLke8", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def gram_matrix(input_tensor):\n", - " # We make the image channels first \n", - " channels = int(input_tensor.shape[-1])\n", - " a = tf.reshape(input_tensor, [-1, channels])\n", - " n = tf.shape(a)[0]\n", - " gram = tf.matmul(a, a, transpose_a=True)\n", - " return gram / tf.cast(n, tf.float32)\n", - "\n", - "def get_style_loss(base_style, gram_target):\n", - " \"\"\"Expects two images of dimension h, w, c\"\"\"\n", - " # height, width, num filters of each layer\n", - " # We scale the loss at a given layer by the size of the feature map and the number of filters\n", - " height, width, channels = base_style.get_shape().as_list()\n", - " gram_style = gram_matrix(base_style)\n", - " \n", - " return tf.reduce_mean(tf.square(gram_style - gram_target))# / (4. * (channels ** 2) * (width * height) ** 2)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "pXIUX6czZABh", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Apply style transfer to our images\n" - ] - }, - { - "metadata": { - "id": "y9r8Lyjb_m0u", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Run Gradient Descent \n", - "If you aren't familiar with gradient descent/backpropagation or need a refresher, you should definitely check out this [awesome resource](https://developers.google.com/machine-learning/crash-course/reducing-loss/gradient-descent).\n", - "\n", - "In this case, we use the [Adam](https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/Adam)* optimizer in order to minimize our loss. We iteratively update our output image such that it minimizes our loss: we don't update the weights associated with our network, but instead we train our input image to minimize loss. In order to do this, we must know how we calculate our loss and gradients. \n", - "\n", - "\\* Note that L-BFGS, which if you are familiar with this algorithm is recommended, isn’t used in this tutorial because a primary motivation behind this tutorial was to illustrate best practices with eager execution, and, by using Adam, we can demonstrate the autograd/gradient tape functionality with custom training loops.\n" - ] - }, - { - "metadata": { - "id": "-kGzV6LTp4CU", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "We’ll define a little helper function that will load our content and style image, feed them forward through our network, which will then output the content and style feature representations from our model. " - ] - }, - { - "metadata": { - "id": "O-lj5LxgtmnI", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def get_feature_representations(model, content_path, style_path):\n", - " \"\"\"Helper function to compute our content and style feature representations.\n", - "\n", - " This function will simply load and preprocess both the content and style \n", - " images from their path. Then it will feed them through the network to obtain\n", - " the outputs of the intermediate layers. \n", - " \n", - " Arguments:\n", - " model: The model that we are using.\n", - " content_path: The path to the content image.\n", - " style_path: The path to the style image\n", - " \n", - " Returns:\n", - " returns the style features and the content features. \n", - " \"\"\"\n", - " # Load our images in \n", - " content_image = load_and_process_img(content_path)\n", - " style_image = load_and_process_img(style_path)\n", - " \n", - " # batch compute content and style features\n", - " style_outputs = model(style_image)\n", - " content_outputs = model(content_image)\n", - " \n", - " \n", - " # Get the style and content feature representations from our model \n", - " style_features = [style_layer[0] for style_layer in style_outputs[:num_style_layers]]\n", - " content_features = [content_layer[0] for content_layer in content_outputs[num_style_layers:]]\n", - " return style_features, content_features" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "3DopXw7-lFHa", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Computing the loss and gradients\n", - "Here we use [**tf.GradientTape**](https://www.tensorflow.org/programmers_guide/eager#computing_gradients) to compute the gradient. It allows us to take advantage of the automatic differentiation available by tracing operations for computing the gradient later. It records the operations during the forward pass and then is able to compute the gradient of our loss function with respect to our input image for the backwards pass." - ] - }, - { - "metadata": { - "id": "oVDhSo8iJunf", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def compute_loss(model, loss_weights, init_image, gram_style_features, content_features):\n", - " \"\"\"This function will compute the loss total loss.\n", - " \n", - " Arguments:\n", - " model: The model that will give us access to the intermediate layers\n", - " loss_weights: The weights of each contribution of each loss function. \n", - " (style weight, content weight, and total variation weight)\n", - " init_image: Our initial base image. This image is what we are updating with \n", - " our optimization process. We apply the gradients wrt the loss we are \n", - " calculating to this image.\n", - " gram_style_features: Precomputed gram matrices corresponding to the \n", - " defined style layers of interest.\n", - " content_features: Precomputed outputs from defined content layers of \n", - " interest.\n", - " \n", - " Returns:\n", - " returns the total loss, style loss, content loss, and total variational loss\n", - " \"\"\"\n", - " style_weight, content_weight = loss_weights\n", - " \n", - " # Feed our init image through our model. This will give us the content and \n", - " # style representations at our desired layers. Since we're using eager\n", - " # our model is callable just like any other function!\n", - " model_outputs = model(init_image)\n", - " \n", - " style_output_features = model_outputs[:num_style_layers]\n", - " content_output_features = model_outputs[num_style_layers:]\n", - " \n", - " style_score = 0\n", - " content_score = 0\n", - "\n", - " # Accumulate style losses from all layers\n", - " # Here, we equally weight each contribution of each loss layer\n", - " weight_per_style_layer = 1.0 / float(num_style_layers)\n", - " for target_style, comb_style in zip(gram_style_features, style_output_features):\n", - " style_score += weight_per_style_layer * get_style_loss(comb_style[0], target_style)\n", - " \n", - " # Accumulate content losses from all layers \n", - " weight_per_content_layer = 1.0 / float(num_content_layers)\n", - " for target_content, comb_content in zip(content_features, content_output_features):\n", - " content_score += weight_per_content_layer* get_content_loss(comb_content[0], target_content)\n", - " \n", - " style_score *= style_weight\n", - " content_score *= content_weight\n", - "\n", - " # Get total loss\n", - " loss = style_score + content_score \n", - " return loss, style_score, content_score" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "r5XTvbP6nJQa", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "Then computing the gradients is easy:" - ] - }, - { - "metadata": { - "id": "fwzYeOqOUH9_", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def compute_grads(cfg):\n", - " with tf.GradientTape() as tape: \n", - " all_loss = compute_loss(**cfg)\n", - " # Compute gradients wrt input image\n", - " total_loss = all_loss[0]\n", - " return tape.gradient(total_loss, cfg['init_image']), all_loss" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "T9yKu2PLlBIE", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Optimization loop" - ] - }, - { - "metadata": { - "id": "pj_enNo6tACQ", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "import IPython.display\n", - "\n", - "def run_style_transfer(content_path, \n", - " style_path,\n", - " num_iterations=1000,\n", - " content_weight=1e3, \n", - " style_weight=1e-2): \n", - " # We don't need to (or want to) train any layers of our model, so we set their\n", - " # trainable to false. \n", - " model = get_model() \n", - " for layer in model.layers:\n", - " layer.trainable = False\n", - " \n", - " # Get the style and content feature representations (from our specified intermediate layers) \n", - " style_features, content_features = get_feature_representations(model, content_path, style_path)\n", - " gram_style_features = [gram_matrix(style_feature) for style_feature in style_features]\n", - " \n", - " # Set initial image\n", - " init_image = load_and_process_img(content_path)\n", - " init_image = tf.Variable(init_image, dtype=tf.float32)\n", - " # Create our optimizer\n", - " opt = tf.optimizers.Adam(learning_rate=5, epsilon=1e-1)\n", - "\n", - " # For displaying intermediate images \n", - " iter_count = 1\n", - " \n", - " # Store our best result\n", - " best_loss, best_img = float('inf'), None\n", - " \n", - " # Create a nice config \n", - " loss_weights = (style_weight, content_weight)\n", - " cfg = {\n", - " 'model': model,\n", - " 'loss_weights': loss_weights,\n", - " 'init_image': init_image,\n", - " 'gram_style_features': gram_style_features,\n", - " 'content_features': content_features\n", - " }\n", - " \n", - " # For displaying\n", - " num_rows = 2\n", - " num_cols = 5\n", - " display_interval = num_iterations/(num_rows*num_cols)\n", - " start_time = time.time()\n", - " global_start = time.time()\n", - " \n", - " norm_means = np.array([103.939, 116.779, 123.68])\n", - " min_vals = -norm_means\n", - " max_vals = 255 - norm_means \n", - " \n", - " imgs = []\n", - " for i in range(num_iterations):\n", - " grads, all_loss = compute_grads(cfg)\n", - " loss, style_score, content_score = all_loss\n", - " opt.apply_gradients([(grads, init_image)])\n", - " clipped = tf.clip_by_value(init_image, min_vals, max_vals)\n", - " init_image.assign(clipped)\n", - " end_time = time.time() \n", - " \n", - " if loss < best_loss:\n", - " # Update best loss and best image from total loss. \n", - " best_loss = loss\n", - " best_img = deprocess_img(init_image.numpy())\n", - "\n", - " if i % display_interval== 0:\n", - " start_time = time.time()\n", - " \n", - " # Use the .numpy() method to get the concrete numpy array\n", - " plot_img = init_image.numpy()\n", - " plot_img = deprocess_img(plot_img)\n", - " imgs.append(plot_img)\n", - " IPython.display.clear_output(wait=True)\n", - " IPython.display.display_png(Image.fromarray(plot_img))\n", - " print('Iteration: {}'.format(i)) \n", - " print('Total loss: {:.4e}, ' \n", - " 'style loss: {:.4e}, '\n", - " 'content loss: {:.4e}, '\n", - " 'time: {:.4f}s'.format(loss, style_score, content_score, time.time() - start_time))\n", - " print('Total time: {:.4f}s'.format(time.time() - global_start))\n", - " IPython.display.clear_output(wait=True)\n", - " plt.figure(figsize=(14,4))\n", - " for i,img in enumerate(imgs):\n", - " plt.subplot(num_rows,num_cols,i+1)\n", - " plt.imshow(img)\n", - " plt.xticks([])\n", - " plt.yticks([])\n", - " \n", - " return best_img, best_loss " - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "vSVMx4burydi", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "best, best_loss = run_style_transfer(content_path, \n", - " style_path, num_iterations=1000)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "dzJTObpsO3TZ", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "Image.fromarray(best)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "dCXQ9vSnQbDy", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "To download the image from Colab uncomment the following code:" - ] - }, - { - "metadata": { - "id": "SSH6OpyyQn7w", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "#from google.colab import files\n", - "#final_img = Image.fromarray(best)\n", - "#final_img.save('wave_turtle.png')\n", - "#files.download('wave_turtle.png')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "LwiZfCW0AZwt", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Visualize outputs\n", - "We \"deprocess\" the output image in order to remove the processing that was applied to it. " - ] - }, - { - "metadata": { - "id": "lqTQN1PjulV9", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "def show_results(best_img, content_path, style_path, show_large_final=True):\n", - " plt.figure(figsize=(10, 5))\n", - " content = load_img(content_path) \n", - " style = load_img(style_path)\n", - "\n", - " plt.subplot(1, 2, 1)\n", - " imshow(content, 'Content Image')\n", - "\n", - " plt.subplot(1, 2, 2)\n", - " imshow(style, 'Style Image')\n", - "\n", - " if show_large_final: \n", - " plt.figure(figsize=(10, 10))\n", - "\n", - " plt.imshow(best_img)\n", - " plt.title('Output Image')\n", - " plt.show()" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "i6d6O50Yvs6a", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "show_results(best, content_path, style_path)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "tyGMmWh2Pss8", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Try it on other images\n", - "Image of Tuebingen \n", - "\n", - "Photo By: Andreas Praefcke [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY 3.0 (https://creativecommons.org/licenses/by/3.0)], from Wikimedia Commons" - ] - }, - { - "metadata": { - "id": "x2TePU39k9lb", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Starry night + Tuebingen" - ] - }, - { - "metadata": { - "id": "ES9dC6ZyJBD2", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "best_starry_night, best_loss = run_style_transfer('/tmp/nst/Tuebingen_Neckarfront.jpg',\n", - " '/tmp/nst/1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "X8w8WLkKvzXu", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "show_results(best_starry_night, '/tmp/nst/Tuebingen_Neckarfront.jpg',\n", - " '/tmp/nst/1024px-Van_Gogh_-_Starry_Night_-_Google_Art_Project.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "QcXwvViek4Br", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Pillars of Creation + Tuebingen" - ] - }, - { - "metadata": { - "id": "vJ3u2U-gGmgP", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "best_poc_tubingen, best_loss = run_style_transfer('/tmp/nst/Tuebingen_Neckarfront.jpg', \n", - " '/tmp/nst/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "pQUq3KxpGv2O", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "show_results(best_poc_tubingen, \n", - " '/tmp/nst/Tuebingen_Neckarfront.jpg',\n", - " '/tmp/nst/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "bTZdTOdW3s8H", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Kandinsky Composition 7 + Tuebingen" - ] - }, - { - "metadata": { - "id": "bt9mbQfl7exl", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "best_kandinsky_tubingen, best_loss = run_style_transfer('/tmp/nst/Tuebingen_Neckarfront.jpg', \n", - " '/tmp/nst/Vassily_Kandinsky,_1913_-_Composition_7.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "Qnz8HeXSXg6P", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "show_results(best_kandinsky_tubingen, \n", - " '/tmp/nst/Tuebingen_Neckarfront.jpg',\n", - " '/tmp/nst/Vassily_Kandinsky,_1913_-_Composition_7.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "cg68lW2A3s8N", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "### Pillars of Creation + Sea Turtle" - ] - }, - { - "metadata": { - "id": "dl0DUot_bFST", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "best_poc_turtle, best_loss = run_style_transfer('/tmp/nst/Green_Sea_Turtle_grazing_seagrass.jpg', \n", - " '/tmp/nst/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "UzJfE0I1bQn8", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "show_results(best_poc_turtle, \n", - " '/tmp/nst/Green_Sea_Turtle_grazing_seagrass.jpg',\n", - " '/tmp/nst/Pillars_of_creation_2014_HST_WFC3-UVIS_full-res_denoised.jpg')" - ], - "execution_count": 0, - "outputs": [] - }, - { - "metadata": { - "id": "sElaeNX-4Vnc", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "## Key Takeaways\n", - "\n", - "### What we covered:\n", - "\n", - "* We built several different loss functions and used backpropagation to transform our input image in order to minimize these losses\n", - " * In order to do this we had to load in a **pretrained model** and use its learned feature maps to describe the content and style representation of our images.\n", - " * Our main loss functions were primarily computing the distance in terms of these different representations\n", - "* We implemented this with a custom model and **eager execution**\n", - " * We built our custom model with the Functional API \n", - " * Eager execution allows us to dynamically work with tensors, using a natural python control flow\n", - " * We manipulated tensors directly, which makes debugging and working with tensors easier. \n", - "* We iteratively updated our image by applying our optimizers update rules using **tf.gradient**. The optimizer minimized a given loss with respect to our input image. " - ] - }, - { - "metadata": { - "id": "U-y02GWonqnD", - "colab_type": "text" - }, - "cell_type": "markdown", - "source": [ - "\n", - "**[Image of Tuebingen](https://commons.wikimedia.org/wiki/File:Tuebingen_Neckarfront.jpg)** \n", - "Photo By: Andreas Praefcke [GFDL (http://www.gnu.org/copyleft/fdl.html) or CC BY 3.0 (https://creativecommons.org/licenses/by/3.0)], from Wikimedia Commons\n", - "\n", - "**[Image of Green Sea Turtle](https://commons.wikimedia.org/wiki/File:Green_Sea_Turtle_grazing_seagrass.jpg)**\n", - "By P.Lindgren [CC BY-SA 3.0 (https://creativecommons.org/licenses/by-sa/3.0)], from Wikimedia Commons\n", - "\n" - ] - }, - { - "metadata": { - "id": "IpUD9W6ZkeyM", - "colab_type": "code", - "colab": {} - }, - "cell_type": "code", - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - } - ] -} diff --git a/research/nst_blogpost/Green_Sea_Turtle_grazing_seagrass.jpg b/research/nst_blogpost/Green_Sea_Turtle_grazing_seagrass.jpg deleted file mode 100644 index c98791b8ace..00000000000 Binary files a/research/nst_blogpost/Green_Sea_Turtle_grazing_seagrass.jpg and /dev/null differ diff --git a/research/nst_blogpost/The_Great_Wave_off_Kanagawa.jpg b/research/nst_blogpost/The_Great_Wave_off_Kanagawa.jpg deleted file mode 100644 index 5d3475c9e5a..00000000000 Binary files a/research/nst_blogpost/The_Great_Wave_off_Kanagawa.jpg and /dev/null differ diff --git a/research/nst_blogpost/wave_turtle.png b/research/nst_blogpost/wave_turtle.png deleted file mode 100644 index d15ee0fc9f0..00000000000 Binary files a/research/nst_blogpost/wave_turtle.png and /dev/null differ diff --git a/research/object_detection/CONTRIBUTING.md b/research/object_detection/CONTRIBUTING.md deleted file mode 100644 index 8073982f4ad..00000000000 --- a/research/object_detection/CONTRIBUTING.md +++ /dev/null @@ -1,13 +0,0 @@ -# Contributing to the TensorFlow Object Detection API - -Patches to TensorFlow Object Detection API are welcome! - -We require contributors to fill out either the individual or corporate -Contributor License Agreement (CLA). - - * 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). - * 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). - -Please follow the -[TensorFlow contributing guidelines](https://github.com/tensorflow/tensorflow/blob/master/CONTRIBUTING.md) -when submitting pull requests. diff --git a/research/object_detection/README.md b/research/object_detection/README.md deleted file mode 100644 index 0793a8d32ab..00000000000 --- a/research/object_detection/README.md +++ /dev/null @@ -1,244 +0,0 @@ -# TensorFlow Object Detection API -[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) - -## Deprecation - -*Note to our users*: the Tensorflow Object Detection API is no longer being -maintained to be compatible with new versions of external dependencies -(from pip, apt-get etc.). Any changes that follow are meant for internal -maintenance. We may use the OD API to release projects in the future, -in which case we will provide full install instructions or Docker images. -We encourage users seeking an actively maintained detection / segmentation -codebase to consider [TF-Vision](https://github.com/tensorflow/models/tree/master/official/vision) -or [scenic](https://github.com/google-research/scenic). We have preserved -the original install instructions below in case anyone wants to try out old -models or scripts. - -Creating accurate machine learning models capable of localizing and identifying -multiple objects in a single image remains a core challenge in computer vision. -The TensorFlow Object Detection API is an open source framework built on top of -TensorFlow that makes it easy to construct, train and deploy object detection -models. At Google we’ve certainly found this codebase to be useful for our -computer vision needs, and we hope that you will as well.

-

-If you use the TensorFlow Object -Detection API for a research publication, please consider citing: - -``` -"Speed/accuracy trade-offs for modern convolutional object detectors." -Huang J, Rathod V, Sun C, Zhu M, Korattikara A, Fathi A, Fischer I, Wojna Z, -Song Y, Guadarrama S, Murphy K, CVPR 2017 -``` - -\[[link](https://arxiv.org/abs/1611.10012)\]\[[bibtex](https://scholar.googleusercontent.com/scholar.bib?q=info:l291WsrB-hQJ:scholar.google.com/&output=citation&scisig=AAGBfm0AAAAAWUIIlnPZ_L9jxvPwcC49kDlELtaeIyU-&scisf=4&ct=citation&cd=-1&hl=en&scfhb=1)\] - -

- -

- -## Support for TensorFlow 2 and 1 -The TensorFlow Object Detection API supports both TensorFlow 2 (TF2) and -TensorFlow 1 (TF1). A majority of the modules in the library are both TF1 and -TF2 compatible. In cases where they are not, we provide two versions. - -Although we will continue to maintain the TF1 models and provide support, we -encourage users to try the Object Detection API with TF2 for the following -reasons: - -* We provide new architectures supported in TF2 only and we will continue to - develop in TF2 going forward. - -* The popular models we ported from TF1 to TF2 achieve the same performance. - -* A single training and evaluation binary now supports both GPU and TPU - distribution strategies making it possible to train models with synchronous - SGD by default. - -* Eager execution with new binaries makes debugging easy! - -Finally, if are an existing user of the Object Detection API we have retained -the same config language you are familiar with and ensured that the -TF2 training/eval binary takes the same arguments as our TF1 binaries. - -Note: The models we provide in [TF2 Zoo](g3doc/tf2_detection_zoo.md) and -[TF1 Zoo](g3doc/tf1_detection_zoo.md) are specific to the TensorFlow major -version and are not interoperable. - -Please select one of the links below for TensorFlow version-specific -documentation of the Object Detection API: - - -### Tensorflow 2.x - * - Object Detection API TensorFlow 2
- * - TensorFlow 2 Model Zoo
- -### Tensorflow 1.x - * - Object Detection API TensorFlow 1
- * - TensorFlow 1 Model Zoo
- - -## Whats New - -### SpaghettiNet for Edge TPU - -We have released SpaghettiNet models optimized for the Edge TPU in the [Google Tensor SoC](https://blog.google/products/pixel/google-tensor-debuts-new-pixel-6-fall/). - -SpaghettiNet models are derived from a TuNAS search space that incorporates -group convolution based [Inverted Bottleneck](https://arxiv.org/abs/1801.04381) blocks. -The backbone and detection head are connected through [MnasFPN](https://arxiv.org/abs/1912.01106)-style feature map -merging and searched jointly. - -When compared to MobileDet-EdgeTPU, SpaghettiNet models achieve +2.2% mAP -(absolute) on COCO17 at the same latency. They also consume <70% of the energy -used by MobileDet-EdgeTPU to achieve the same accuracy. - -Sample config available [here](configs/tf1/ssd_spaghettinet_edgetpu_320x320_coco17_sync_4x4.config). - -Thanks to contributors: Marie White, Hao Xu, Hanxiao Liu and Suyog Gupta. - -### DeepMAC architecture - -We have released our new architecture, **DeepMAC**, designed for partially -supervised instance segmentation. DeepMAC stands for Deep Mask-heads -Above CenterNet, and is based on our CenterNet implementation. In our -[paper](https://arxiv.org/abs/2104.00613) we show that DeepMAC achieves -state-of-the-art results for the partially supervised instance segmentation -task without using any specialty modules or losses; just better mask-head -architectures. The findings from our paper are not specific to CenterNet and -can also be applied to Mask R-CNN or without any detector at all. -Please see links below for more details - -* [DeepMAC documentation](g3doc/deepmac.md). -* [Mask RCNN code](https://github.com/tensorflow/models/tree/master/official/vision/beta/projects/deepmac_maskrcnn) - in TF Model garden code base. -* [DeepMAC Colab](./colab_tutorials/deepmac_colab.ipynb) that lets you run a - pre-trained DeepMAC model on user-specified boxes. Note that you are not - restricted to COCO classes! -* Project website - [git.io/deepmac](https://git.io/deepmac) - -Thanks to contributors: Vighnesh Birodkar, Zhichao Lu, Siyang Li, - Vivek Rathod, Jonathan Huang - - -### Mobile Inference for TF2 models - -TF2 OD API models can now be converted to TensorFlow Lite! Only SSD models -currently supported. See documentation. - -**Thanks to contributors**: Sachin Joglekar - -### TensorFlow 2 Support - -We are happy to announce that the TF OD API officially supports TF2! Our release -includes: - -* New binaries for train/eval/export that are designed to run in eager mode. -* A suite of TF2 compatible (Keras-based) models; this includes migrations of - our most popular TF1.x models (e.g., SSD with MobileNet, RetinaNet, - Faster R-CNN, Mask R-CNN), as well as a few new architectures for which we - will only maintain TF2 implementations: - - 1. CenterNet - a simple and effective anchor-free architecture based on - the recent [Objects as Points](https://arxiv.org/abs/1904.07850) paper by - Zhou et al. - 2. [EfficientDet](https://arxiv.org/abs/1911.09070) - a recent family of - SOTA models discovered with the help of Neural Architecture Search. - -* COCO pre-trained weights for all of the models provided as TF2 style - object-based checkpoints. -* Access to [Distribution Strategies](https://www.tensorflow.org/guide/distributed_training) - for distributed training --- our model are designed to be trainable using sync - multi-GPU and TPU platforms. -* Colabs demo’ing eager mode training and inference. - -See our release blogpost [here](https://blog.tensorflow.org/2020/07/tensorflow-2-meets-object-detection-api.html). -If you are an existing user of the TF OD API using TF 1.x, don’t worry, we’ve -got you covered. - -**Thanks to contributors**: Akhil Chinnakotla, Allen Lavoie, Anirudh Vegesana, -Anjali Sridhar, Austin Myers, Dan Kondratyuk, David Ross, Derek Chow, Jaeyoun -Kim, Jing Li, Jonathan Huang, Jordi Pont-Tuset, Karmel Allison, Kathy Ruan, -Kaushik Shivakumar, Lu He, Mingxing Tan, Pengchong Jin, Ronny Votel, Sara Beery, -Sergi Caelles Prat, Shan Yang, Sudheendra Vijayanarasimhan, Tina Tian, Tomer -Kaftan, Vighnesh Birodkar, Vishnu Banna, Vivek Rathod, Yanhui Liang, Yiming Shi, -Yixin Shi, Yu-hui Chen, Zhichao Lu. - -### MobileDet GPU - -We have released SSDLite with MobileDet GPU backbone, which achieves 17% mAP -higher than the MobileNetV2 SSDLite (27.5 mAP vs 23.5 mAP) on a NVIDIA Jetson -Xavier at comparable latency (3.2ms vs 3.3ms). - -Along with the model definition, we are also releasing model checkpoints trained -on the COCO dataset. - -Thanks to contributors: Yongzhe Wang, Bo Chen, Hanxiao Liu, Le An -(NVIDIA), Yu-Te Cheng (NVIDIA), Oliver Knieps (NVIDIA), and Josh Park (NVIDIA). - -### Context R-CNN - -We have released [Context R-CNN](https://arxiv.org/abs/1912.03538), a model that -uses attention to incorporate contextual information images (e.g. from -temporally nearby frames taken by a static camera) in order to improve accuracy. -Importantly, these contextual images need not be labeled. - -* When applied to a challenging wildlife detection dataset - ([Snapshot Serengeti](http://lila.science/datasets/snapshot-serengeti)), - Context R-CNN with context from up to a month of images outperforms a - single-frame baseline by 17.9% mAP, and outperforms S3D (a 3d convolution - based baseline) by 11.2% mAP. -* Context R-CNN leverages temporal context from the unlabeled frames of a - novel camera deployment to improve performance at that camera, boosting - model generalizeability. - -Read about Context R-CNN on the Google AI blog -[here](https://ai.googleblog.com/2020/06/leveraging-temporal-context-for-object.html). - -We have provided code for generating data with associated context -[here](g3doc/context_rcnn.md), and a sample config for a Context R-CNN model -[here](samples/configs/context_rcnn_resnet101_snapshot_serengeti_sync.config). - -Snapshot Serengeti-trained Faster R-CNN and Context R-CNN models can be found in -the -[model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md#snapshot-serengeti-camera-trap-trained-models). - -A colab demonstrating Context R-CNN is provided -[here](colab_tutorials/context_rcnn_tutorial.ipynb). - -Thanks to contributors: Sara Beery, Jonathan Huang, Guanhang Wu, Vivek -Rathod, Ronny Votel, Zhichao Lu, David Ross, Pietro Perona, Tanya Birch, and the -Wildlife Insights AI Team. - -## Release Notes -See [notes](g3doc/release_notes.md) for all past releases. - -## Getting Help - -To get help with issues you may encounter using the TensorFlow Object Detection -API, create a new question on [StackOverflow](https://stackoverflow.com/) with -the tags "tensorflow" and "object-detection". - -Please report bugs (actually broken code, not usage questions) to the -tensorflow/models GitHub -[issue tracker](https://github.com/tensorflow/models/issues), prefixing the -issue name with "object_detection". - -Please check the [FAQ](g3doc/faq.md) for frequently asked questions before -reporting an issue. - -## Maintainers - -* Jonathan Huang ([@GitHub jch1](https://github.com/jch1)) -* Vivek Rathod ([@GitHub tombstone](https://github.com/tombstone)) -* Vighnesh Birodkar ([@GitHub vighneshbirodkar](https://github.com/vighneshbirodkar)) -* Austin Myers ([@GitHub austin-myers](https://github.com/austin-myers)) -* Zhichao Lu ([@GitHub pkulzc](https://github.com/pkulzc)) -* Ronny Votel ([@GitHub ronnyvotel](https://github.com/ronnyvotel)) -* Yu-hui Chen ([@GitHub yuhuichen1015](https://github.com/yuhuichen1015)) -* Derek Chow ([@GitHub derekjchow](https://github.com/derekjchow)) diff --git a/research/object_detection/__init__.py b/research/object_detection/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/anchor_generators/__init__.py b/research/object_detection/anchor_generators/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/anchor_generators/flexible_grid_anchor_generator.py b/research/object_detection/anchor_generators/flexible_grid_anchor_generator.py deleted file mode 100644 index 0f340cc945e..00000000000 --- a/research/object_detection/anchor_generators/flexible_grid_anchor_generator.py +++ /dev/null @@ -1,134 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Generates grid anchors on the fly corresponding to multiple CNN layers.""" - -import tensorflow.compat.v1 as tf - -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.core import anchor_generator -from object_detection.core import box_list_ops - - -class FlexibleGridAnchorGenerator(anchor_generator.AnchorGenerator): - """Generate a grid of anchors for multiple CNN layers of different scale.""" - - def __init__(self, base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=True): - """Constructs a FlexibleGridAnchorGenerator. - - This generator is more flexible than the multiple_grid_anchor_generator - and multiscale_grid_anchor_generator, and can generate any of the anchors - that they can generate, plus additional anchor configurations. In - particular, it allows the explicit specification of scale and aspect ratios - at each layer without making any assumptions between the relationship - between scales and aspect ratios between layers. - - Args: - base_sizes: list of tuples of anchor base sizes. For example, setting - base_sizes=[(1, 2, 3), (4, 5)] means that we want 3 anchors at each - grid point on the first layer with the base sizes of 1, 2, and 3, and 2 - anchors at each grid point on the second layer with the base sizes of - 4 and 5. - aspect_ratios: list or tuple of aspect ratios. For example, setting - aspect_ratios=[(1.0, 2.0, 0.5), (1.0, 2.0)] means that we want 3 anchors - at each grid point on the first layer with aspect ratios of 1.0, 2.0, - and 0.5, and 2 anchors at each grid point on the sercond layer with the - base sizes of 1.0 and 2.0. - anchor_strides: list of pairs of strides in pixels (in y and x directions - respectively). For example, setting anchor_strides=[(25, 25), (50, 50)] - means that we want the anchors corresponding to the first layer to be - strided by 25 pixels and those in the second layer to be strided by 50 - pixels in both y and x directions. - anchor_offsets: list of pairs of offsets in pixels (in y and x directions - respectively). The offset specifies where we want the center of the - (0, 0)-th anchor to lie for each layer. For example, setting - anchor_offsets=[(10, 10), (20, 20)]) means that we want the - (0, 0)-th anchor of the first layer to lie at (10, 10) in pixel space - and likewise that we want the (0, 0)-th anchor of the second layer to - lie at (25, 25) in pixel space. - normalize_coordinates: whether to produce anchors in normalized - coordinates. (defaults to True). - """ - self._base_sizes = base_sizes - self._aspect_ratios = aspect_ratios - self._anchor_strides = anchor_strides - self._anchor_offsets = anchor_offsets - self._normalize_coordinates = normalize_coordinates - - def name_scope(self): - return 'FlexibleGridAnchorGenerator' - - def num_anchors_per_location(self): - """Returns the number of anchors per spatial location. - - Returns: - a list of integers, one for each expected feature map to be passed to - the Generate function. - """ - return [len(size) for size in self._base_sizes] - - def _generate(self, feature_map_shape_list, im_height=1, im_width=1): - """Generates a collection of bounding boxes to be used as anchors. - - Currently we require the input image shape to be statically defined. That - is, im_height and im_width should be integers rather than tensors. - - Args: - feature_map_shape_list: list of pairs of convnet layer resolutions in the - format [(height_0, width_0), (height_1, width_1), ...]. For example, - setting feature_map_shape_list=[(8, 8), (7, 7)] asks for anchors that - correspond to an 8x8 layer followed by a 7x7 layer. - im_height: the height of the image to generate the grid for. If both - im_height and im_width are 1, anchors can only be generated in - absolute coordinates. - im_width: the width of the image to generate the grid for. If both - im_height and im_width are 1, anchors can only be generated in - absolute coordinates. - - Returns: - boxes_list: a list of BoxLists each holding anchor boxes corresponding to - the input feature map shapes. - Raises: - ValueError: if im_height and im_width are 1, but normalized coordinates - were requested. - """ - anchor_grid_list = [] - for (feat_shape, base_sizes, aspect_ratios, anchor_stride, anchor_offset - ) in zip(feature_map_shape_list, self._base_sizes, self._aspect_ratios, - self._anchor_strides, self._anchor_offsets): - anchor_grid = grid_anchor_generator.tile_anchors( - feat_shape[0], - feat_shape[1], - tf.cast(tf.convert_to_tensor(base_sizes), dtype=tf.float32), - tf.cast(tf.convert_to_tensor(aspect_ratios), dtype=tf.float32), - tf.constant([1.0, 1.0]), - tf.cast(tf.convert_to_tensor(anchor_stride), dtype=tf.float32), - tf.cast(tf.convert_to_tensor(anchor_offset), dtype=tf.float32)) - num_anchors = anchor_grid.num_boxes_static() - if num_anchors is None: - num_anchors = anchor_grid.num_boxes() - anchor_indices = tf.zeros([num_anchors]) - anchor_grid.add_field('feature_map_index', anchor_indices) - if self._normalize_coordinates: - if im_height == 1 or im_width == 1: - raise ValueError( - 'Normalized coordinates were requested upon construction of the ' - 'FlexibleGridAnchorGenerator, but a subsequent call to ' - 'generate did not supply dimension information.') - anchor_grid = box_list_ops.to_normalized_coordinates( - anchor_grid, im_height, im_width, check_range=False) - anchor_grid_list.append(anchor_grid) - - return anchor_grid_list diff --git a/research/object_detection/anchor_generators/flexible_grid_anchor_generator_test.py b/research/object_detection/anchor_generators/flexible_grid_anchor_generator_test.py deleted file mode 100644 index bab34b75018..00000000000 --- a/research/object_detection/anchor_generators/flexible_grid_anchor_generator_test.py +++ /dev/null @@ -1,292 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for anchor_generators.flexible_grid_anchor_generator_test.py.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.anchor_generators import flexible_grid_anchor_generator as fg -from object_detection.utils import test_case - - -class FlexibleGridAnchorGeneratorTest(test_case.TestCase): - - def test_construct_single_anchor(self): - def graph_fn(): - anchor_strides = [(32, 32),] - anchor_offsets = [(16, 16),] - base_sizes = [(128.0,)] - aspect_ratios = [(1.0,)] - im_height = 64 - im_width = 64 - feature_map_shape_list = [(2, 2)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - anchor_corners_out = self.execute(graph_fn, []) - exp_anchor_corners = [[-48, -48, 80, 80], - [-48, -16, 80, 112], - [-16, -48, 112, 80], - [-16, -16, 112, 112]] - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_unit_dimensions(self): - def graph_fn(): - anchor_strides = [(32, 32),] - anchor_offsets = [(16, 16),] - base_sizes = [(32.0,)] - aspect_ratios = [(1.0,)] - im_height = 1 - im_width = 1 - feature_map_shape_list = [(2, 2)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - # Positive offsets are produced. - exp_anchor_corners = [[0, 0, 32, 32], - [0, 32, 32, 64], - [32, 0, 64, 32], - [32, 32, 64, 64]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_normalized_anchors_fails_with_unit_dimensions(self): - anchor_generator = fg.FlexibleGridAnchorGenerator( - [(32.0,)], [(1.0,)], [(32, 32),], [(16, 16),], - normalize_coordinates=True) - with self.assertRaisesRegexp(ValueError, 'Normalized coordinates'): - anchor_generator.generate( - feature_map_shape_list=[(2, 2)], im_height=1, im_width=1) - - def test_construct_single_anchor_in_normalized_coordinates(self): - def graph_fn(): - anchor_strides = [(32, 32),] - anchor_offsets = [(16, 16),] - base_sizes = [(128.0,)] - aspect_ratios = [(1.0,)] - im_height = 64 - im_width = 128 - feature_map_shape_list = [(2, 2)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=True) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - exp_anchor_corners = [[-48./64, -48./128, 80./64, 80./128], - [-48./64, -16./128, 80./64, 112./128], - [-16./64, -48./128, 112./64, 80./128], - [-16./64, -16./128, 112./64, 112./128]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_num_anchors_per_location(self): - anchor_strides = [(32, 32), (64, 64)] - anchor_offsets = [(16, 16), (32, 32)] - base_sizes = [(32.0, 64.0, 96.0, 32.0, 64.0, 96.0), - (64.0, 128.0, 172.0, 64.0, 128.0, 172.0)] - aspect_ratios = [(1.0, 1.0, 1.0, 2.0, 2.0, 2.0), - (1.0, 1.0, 1.0, 2.0, 2.0, 2.0)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - self.assertEqual(anchor_generator.num_anchors_per_location(), [6, 6]) - - def test_construct_single_anchor_dynamic_size(self): - def graph_fn(): - anchor_strides = [(32, 32),] - anchor_offsets = [(0, 0),] - base_sizes = [(128.0,)] - aspect_ratios = [(1.0,)] - im_height = tf.constant(64) - im_width = tf.constant(64) - feature_map_shape_list = [(2, 2)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - # Zero offsets are used. - exp_anchor_corners = [[-64, -64, 64, 64], - [-64, -32, 64, 96], - [-32, -64, 96, 64], - [-32, -32, 96, 96]] - anchor_corners_out = self.execute_cpu(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_with_odd_input_dimension(self): - - def graph_fn(): - anchor_strides = [(32, 32),] - anchor_offsets = [(0, 0),] - base_sizes = [(128.0,)] - aspect_ratios = [(1.0,)] - im_height = 65 - im_width = 65 - feature_map_shape_list = [(3, 3)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return (anchor_corners,) - anchor_corners_out = self.execute(graph_fn, []) - exp_anchor_corners = [[-64, -64, 64, 64], - [-64, -32, 64, 96], - [-64, 0, 64, 128], - [-32, -64, 96, 64], - [-32, -32, 96, 96], - [-32, 0, 96, 128], - [0, -64, 128, 64], - [0, -32, 128, 96], - [0, 0, 128, 128]] - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_on_two_feature_maps(self): - - def graph_fn(): - anchor_strides = [(32, 32), (64, 64)] - anchor_offsets = [(16, 16), (32, 32)] - base_sizes = [(128.0,), (256.0,)] - aspect_ratios = [(1.0,), (1.0,)] - im_height = 64 - im_width = 64 - feature_map_shape_list = [(2, 2), (1, 1)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - - anchor_corners_out = np.concatenate(self.execute(graph_fn, []), axis=0) - exp_anchor_corners = [[-48, -48, 80, 80], - [-48, -16, 80, 112], - [-16, -48, 112, 80], - [-16, -16, 112, 112], - [-96, -96, 160, 160]] - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_with_two_scales_per_octave(self): - - def graph_fn(): - anchor_strides = [(64, 64),] - anchor_offsets = [(32, 32),] - base_sizes = [(256.0, 362.03867)] - aspect_ratios = [(1.0, 1.0)] - im_height = 64 - im_width = 64 - feature_map_shape_list = [(1, 1)] - - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - # There are 4 set of anchors in this configuration. The order is: - # [[2**0.0 intermediate scale + 1.0 aspect], - # [2**0.5 intermediate scale + 1.0 aspect]] - exp_anchor_corners = [[-96., -96., 160., 160.], - [-149.0193, -149.0193, 213.0193, 213.0193]] - - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_with_two_scales_per_octave_and_aspect(self): - def graph_fn(): - anchor_strides = [(64, 64),] - anchor_offsets = [(32, 32),] - base_sizes = [(256.0, 362.03867, 256.0, 362.03867)] - aspect_ratios = [(1.0, 1.0, 2.0, 2.0)] - im_height = 64 - im_width = 64 - feature_map_shape_list = [(1, 1)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - # There are 4 set of anchors in this configuration. The order is: - # [[2**0.0 intermediate scale + 1.0 aspect], - # [2**0.5 intermediate scale + 1.0 aspect], - # [2**0.0 intermediate scale + 2.0 aspect], - # [2**0.5 intermediate scale + 2.0 aspect]] - - exp_anchor_corners = [[-96., -96., 160., 160.], - [-149.0193, -149.0193, 213.0193, 213.0193], - [-58.50967, -149.0193, 122.50967, 213.0193], - [-96., -224., 160., 288.]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchors_on_feature_maps_with_dynamic_shape(self): - - def graph_fn(feature_map1_height, feature_map1_width, feature_map2_height, - feature_map2_width): - anchor_strides = [(32, 32), (64, 64)] - anchor_offsets = [(16, 16), (32, 32)] - base_sizes = [(128.0,), (256.0,)] - aspect_ratios = [(1.0,), (1.0,)] - im_height = 64 - im_width = 64 - feature_map_shape_list = [(feature_map1_height, feature_map1_width), - (feature_map2_height, feature_map2_width)] - anchor_generator = fg.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, anchor_strides, anchor_offsets, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - - anchor_corners_out = np.concatenate( - self.execute_cpu(graph_fn, [ - np.array(2, dtype=np.int32), - np.array(2, dtype=np.int32), - np.array(1, dtype=np.int32), - np.array(1, dtype=np.int32) - ]), - axis=0) - exp_anchor_corners = [[-48, -48, 80, 80], - [-48, -16, 80, 112], - [-16, -48, 112, 80], - [-16, -16, 112, 112], - [-96, -96, 160, 160]] - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/anchor_generators/grid_anchor_generator.py b/research/object_detection/anchor_generators/grid_anchor_generator.py deleted file mode 100644 index a31bc87996d..00000000000 --- a/research/object_detection/anchor_generators/grid_anchor_generator.py +++ /dev/null @@ -1,213 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Generates grid anchors on the fly as used in Faster RCNN. - -Generates grid anchors on the fly as described in: -"Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks" -Shaoqing Ren, Kaiming He, Ross Girshick, and Jian Sun. -""" - -import tensorflow.compat.v1 as tf - -from object_detection.core import anchor_generator -from object_detection.core import box_list -from object_detection.utils import ops - - -class GridAnchorGenerator(anchor_generator.AnchorGenerator): - """Generates a grid of anchors at given scales and aspect ratios.""" - - def __init__(self, - scales=(0.5, 1.0, 2.0), - aspect_ratios=(0.5, 1.0, 2.0), - base_anchor_size=None, - anchor_stride=None, - anchor_offset=None): - """Constructs a GridAnchorGenerator. - - Args: - scales: a list of (float) scales, default=(0.5, 1.0, 2.0) - aspect_ratios: a list of (float) aspect ratios, default=(0.5, 1.0, 2.0) - base_anchor_size: base anchor size as height, width ( - (length-2 float32 list or tensor, default=[256, 256]) - anchor_stride: difference in centers between base anchors for adjacent - grid positions (length-2 float32 list or tensor, - default=[16, 16]) - anchor_offset: center of the anchor with scale and aspect ratio 1 for the - upper left element of the grid, this should be zero for - feature networks with only VALID padding and even receptive - field size, but may need additional calculation if other - padding is used (length-2 float32 list or tensor, - default=[0, 0]) - """ - # Handle argument defaults - if base_anchor_size is None: - base_anchor_size = [256, 256] - if anchor_stride is None: - anchor_stride = [16, 16] - if anchor_offset is None: - anchor_offset = [0, 0] - - self._scales = scales - self._aspect_ratios = aspect_ratios - self._base_anchor_size = base_anchor_size - self._anchor_stride = anchor_stride - self._anchor_offset = anchor_offset - - def name_scope(self): - return 'GridAnchorGenerator' - - def num_anchors_per_location(self): - """Returns the number of anchors per spatial location. - - Returns: - a list of integers, one for each expected feature map to be passed to - the `generate` function. - """ - return [len(self._scales) * len(self._aspect_ratios)] - - def _generate(self, feature_map_shape_list): - """Generates a collection of bounding boxes to be used as anchors. - - Args: - feature_map_shape_list: list of pairs of convnet layer resolutions in the - format [(height_0, width_0)]. For example, setting - feature_map_shape_list=[(8, 8)] asks for anchors that correspond - to an 8x8 layer. For this anchor generator, only lists of length 1 are - allowed. - - Returns: - boxes_list: a list of BoxLists each holding anchor boxes corresponding to - the input feature map shapes. - - Raises: - ValueError: if feature_map_shape_list, box_specs_list do not have the same - length. - ValueError: if feature_map_shape_list does not consist of pairs of - integers - """ - if not (isinstance(feature_map_shape_list, list) - and len(feature_map_shape_list) == 1): - raise ValueError('feature_map_shape_list must be a list of length 1.') - if not all([isinstance(list_item, tuple) and len(list_item) == 2 - for list_item in feature_map_shape_list]): - raise ValueError('feature_map_shape_list must be a list of pairs.') - - # Create constants in init_scope so they can be created in tf.functions - # and accessed from outside of the function. - with tf.init_scope(): - self._base_anchor_size = tf.cast(tf.convert_to_tensor( - self._base_anchor_size), dtype=tf.float32) - self._anchor_stride = tf.cast(tf.convert_to_tensor( - self._anchor_stride), dtype=tf.float32) - self._anchor_offset = tf.cast(tf.convert_to_tensor( - self._anchor_offset), dtype=tf.float32) - - grid_height, grid_width = feature_map_shape_list[0] - scales_grid, aspect_ratios_grid = ops.meshgrid(self._scales, - self._aspect_ratios) - scales_grid = tf.reshape(scales_grid, [-1]) - aspect_ratios_grid = tf.reshape(aspect_ratios_grid, [-1]) - anchors = tile_anchors(grid_height, - grid_width, - scales_grid, - aspect_ratios_grid, - self._base_anchor_size, - self._anchor_stride, - self._anchor_offset) - - num_anchors = anchors.num_boxes_static() - if num_anchors is None: - num_anchors = anchors.num_boxes() - anchor_indices = tf.zeros([num_anchors]) - anchors.add_field('feature_map_index', anchor_indices) - return [anchors] - - -def tile_anchors(grid_height, - grid_width, - scales, - aspect_ratios, - base_anchor_size, - anchor_stride, - anchor_offset): - """Create a tiled set of anchors strided along a grid in image space. - - This op creates a set of anchor boxes by placing a "basis" collection of - boxes with user-specified scales and aspect ratios centered at evenly - distributed points along a grid. The basis collection is specified via the - scale and aspect_ratios arguments. For example, setting scales=[.1, .2, .2] - and aspect ratios = [2,2,1/2] means that we create three boxes: one with scale - .1, aspect ratio 2, one with scale .2, aspect ratio 2, and one with scale .2 - and aspect ratio 1/2. Each box is multiplied by "base_anchor_size" before - placing it over its respective center. - - Grid points are specified via grid_height, grid_width parameters as well as - the anchor_stride and anchor_offset parameters. - - Args: - grid_height: size of the grid in the y direction (int or int scalar tensor) - grid_width: size of the grid in the x direction (int or int scalar tensor) - scales: a 1-d (float) tensor representing the scale of each box in the - basis set. - aspect_ratios: a 1-d (float) tensor representing the aspect ratio of each - box in the basis set. The length of the scales and aspect_ratios tensors - must be equal. - base_anchor_size: base anchor size as [height, width] - (float tensor of shape [2]) - anchor_stride: difference in centers between base anchors for adjacent grid - positions (float tensor of shape [2]) - anchor_offset: center of the anchor with scale and aspect ratio 1 for the - upper left element of the grid, this should be zero for - feature networks with only VALID padding and even receptive - field size, but may need some additional calculation if other - padding is used (float tensor of shape [2]) - Returns: - a BoxList holding a collection of N anchor boxes - """ - ratio_sqrts = tf.sqrt(aspect_ratios) - heights = scales / ratio_sqrts * base_anchor_size[0] - widths = scales * ratio_sqrts * base_anchor_size[1] - - # Get a grid of box centers - y_centers = tf.cast(tf.range(grid_height), dtype=tf.float32) - y_centers = y_centers * anchor_stride[0] + anchor_offset[0] - x_centers = tf.cast(tf.range(grid_width), dtype=tf.float32) - x_centers = x_centers * anchor_stride[1] + anchor_offset[1] - x_centers, y_centers = ops.meshgrid(x_centers, y_centers) - - widths_grid, x_centers_grid = ops.meshgrid(widths, x_centers) - heights_grid, y_centers_grid = ops.meshgrid(heights, y_centers) - bbox_centers = tf.stack([y_centers_grid, x_centers_grid], axis=3) - bbox_sizes = tf.stack([heights_grid, widths_grid], axis=3) - bbox_centers = tf.reshape(bbox_centers, [-1, 2]) - bbox_sizes = tf.reshape(bbox_sizes, [-1, 2]) - bbox_corners = _center_size_bbox_to_corners_bbox(bbox_centers, bbox_sizes) - return box_list.BoxList(bbox_corners) - - -def _center_size_bbox_to_corners_bbox(centers, sizes): - """Converts bbox center-size representation to corners representation. - - Args: - centers: a tensor with shape [N, 2] representing bounding box centers - sizes: a tensor with shape [N, 2] representing bounding boxes - - Returns: - corners: tensor with shape [N, 4] representing bounding boxes in corners - representation - """ - return tf.concat([centers - .5 * sizes, centers + .5 * sizes], 1) diff --git a/research/object_detection/anchor_generators/grid_anchor_generator_test.py b/research/object_detection/anchor_generators/grid_anchor_generator_test.py deleted file mode 100644 index 292076ea1e9..00000000000 --- a/research/object_detection/anchor_generators/grid_anchor_generator_test.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.grid_anchor_generator.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.utils import test_case - - -class GridAnchorGeneratorTest(test_case.TestCase): - - def test_construct_single_anchor(self): - """Builds a 1x1 anchor grid to test the size of the output boxes.""" - def graph_fn(): - scales = [0.5, 1.0, 2.0] - aspect_ratios = [0.25, 1.0, 4.0] - anchor_offset = [7, -3] - anchor_generator = grid_anchor_generator.GridAnchorGenerator( - scales, aspect_ratios, anchor_offset=anchor_offset) - anchors_list = anchor_generator.generate(feature_map_shape_list=[(1, 1)]) - anchor_corners = anchors_list[0].get() - return (anchor_corners,) - exp_anchor_corners = [[-121, -35, 135, 29], [-249, -67, 263, 61], - [-505, -131, 519, 125], [-57, -67, 71, 61], - [-121, -131, 135, 125], [-249, -259, 263, 253], - [-25, -131, 39, 125], [-57, -259, 71, 253], - [-121, -515, 135, 509]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_anchor_grid(self): - def graph_fn(): - base_anchor_size = [10, 10] - anchor_stride = [19, 19] - anchor_offset = [0, 0] - scales = [0.5, 1.0, 2.0] - aspect_ratios = [1.0] - - anchor_generator = grid_anchor_generator.GridAnchorGenerator( - scales, - aspect_ratios, - base_anchor_size=base_anchor_size, - anchor_stride=anchor_stride, - anchor_offset=anchor_offset) - - anchors_list = anchor_generator.generate(feature_map_shape_list=[(2, 2)]) - anchor_corners = anchors_list[0].get() - return (anchor_corners,) - exp_anchor_corners = [[-2.5, -2.5, 2.5, 2.5], [-5., -5., 5., 5.], - [-10., -10., 10., 10.], [-2.5, 16.5, 2.5, 21.5], - [-5., 14., 5, 24], [-10., 9., 10, 29], - [16.5, -2.5, 21.5, 2.5], [14., -5., 24, 5], - [9., -10., 29, 10], [16.5, 16.5, 21.5, 21.5], - [14., 14., 24, 24], [9., 9., 29, 29]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_anchor_grid_with_dynamic_feature_map_shapes(self): - def graph_fn(feature_map_height, feature_map_width): - base_anchor_size = [10, 10] - anchor_stride = [19, 19] - anchor_offset = [0, 0] - scales = [0.5, 1.0, 2.0] - aspect_ratios = [1.0] - anchor_generator = grid_anchor_generator.GridAnchorGenerator( - scales, - aspect_ratios, - base_anchor_size=base_anchor_size, - anchor_stride=anchor_stride, - anchor_offset=anchor_offset) - - anchors_list = anchor_generator.generate( - feature_map_shape_list=[(feature_map_height, feature_map_width)]) - anchor_corners = anchors_list[0].get() - return (anchor_corners,) - - exp_anchor_corners = [[-2.5, -2.5, 2.5, 2.5], [-5., -5., 5., 5.], - [-10., -10., 10., 10.], [-2.5, 16.5, 2.5, 21.5], - [-5., 14., 5, 24], [-10., 9., 10, 29], - [16.5, -2.5, 21.5, 2.5], [14., -5., 24, 5], - [9., -10., 29, 10], [16.5, 16.5, 21.5, 21.5], - [14., 14., 24, 24], [9., 9., 29, 29]] - anchor_corners_out = self.execute_cpu(graph_fn, - [np.array(2, dtype=np.int32), - np.array(2, dtype=np.int32)]) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/anchor_generators/multiple_grid_anchor_generator.py b/research/object_detection/anchor_generators/multiple_grid_anchor_generator.py deleted file mode 100644 index 5da24d4192c..00000000000 --- a/research/object_detection/anchor_generators/multiple_grid_anchor_generator.py +++ /dev/null @@ -1,342 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Generates grid anchors on the fly corresponding to multiple CNN layers. - -Generates grid anchors on the fly corresponding to multiple CNN layers as -described in: -"SSD: Single Shot MultiBox Detector" -Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, -Cheng-Yang Fu, Alexander C. Berg -(see Section 2.2: Choosing scales and aspect ratios for default boxes) -""" - -import numpy as np - -import tensorflow.compat.v1 as tf - -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.core import anchor_generator -from object_detection.core import box_list_ops - - -class MultipleGridAnchorGenerator(anchor_generator.AnchorGenerator): - """Generate a grid of anchors for multiple CNN layers.""" - - def __init__(self, - box_specs_list, - base_anchor_size=None, - anchor_strides=None, - anchor_offsets=None, - clip_window=None): - """Constructs a MultipleGridAnchorGenerator. - - To construct anchors, at multiple grid resolutions, one must provide a - list of feature_map_shape_list (e.g., [(8, 8), (4, 4)]), and for each grid - size, a corresponding list of (scale, aspect ratio) box specifications. - - For example: - box_specs_list = [[(.1, 1.0), (.1, 2.0)], # for 8x8 grid - [(.2, 1.0), (.3, 1.0), (.2, 2.0)]] # for 4x4 grid - - To support the fully convolutional setting, we pass grid sizes in at - generation time, while scale and aspect ratios are fixed at construction - time. - - Args: - box_specs_list: list of list of (scale, aspect ratio) pairs with the - outside list having the same number of entries as feature_map_shape_list - (which is passed in at generation time). - base_anchor_size: base anchor size as [height, width] - (length-2 float numpy or Tensor, default=[1.0, 1.0]). - The height and width values are normalized to the - minimum dimension of the input height and width, so that - when the base anchor height equals the base anchor - width, the resulting anchor is square even if the input - image is not square. - anchor_strides: list of pairs of strides in pixels (in y and x directions - respectively). For example, setting anchor_strides=[(25, 25), (50, 50)] - means that we want the anchors corresponding to the first layer to be - strided by 25 pixels and those in the second layer to be strided by 50 - pixels in both y and x directions. If anchor_strides=None, they are set - to be the reciprocal of the corresponding feature map shapes. - anchor_offsets: list of pairs of offsets in pixels (in y and x directions - respectively). The offset specifies where we want the center of the - (0, 0)-th anchor to lie for each layer. For example, setting - anchor_offsets=[(10, 10), (20, 20)]) means that we want the - (0, 0)-th anchor of the first layer to lie at (10, 10) in pixel space - and likewise that we want the (0, 0)-th anchor of the second layer to - lie at (25, 25) in pixel space. If anchor_offsets=None, then they are - set to be half of the corresponding anchor stride. - clip_window: a tensor of shape [4] specifying a window to which all - anchors should be clipped. If clip_window is None, then no clipping - is performed. - - Raises: - ValueError: if box_specs_list is not a list of list of pairs - ValueError: if clip_window is not either None or a tensor of shape [4] - """ - if isinstance(box_specs_list, list) and all( - [isinstance(list_item, list) for list_item in box_specs_list]): - self._box_specs = box_specs_list - else: - raise ValueError('box_specs_list is expected to be a ' - 'list of lists of pairs') - if base_anchor_size is None: - base_anchor_size = [256, 256] - self._base_anchor_size = base_anchor_size - self._anchor_strides = anchor_strides - self._anchor_offsets = anchor_offsets - if clip_window is not None and clip_window.get_shape().as_list() != [4]: - raise ValueError('clip_window must either be None or a shape [4] tensor') - self._clip_window = clip_window - self._scales = [] - self._aspect_ratios = [] - for box_spec in self._box_specs: - if not all([isinstance(entry, tuple) and len(entry) == 2 - for entry in box_spec]): - raise ValueError('box_specs_list is expected to be a ' - 'list of lists of pairs') - scales, aspect_ratios = zip(*box_spec) - self._scales.append(scales) - self._aspect_ratios.append(aspect_ratios) - - for arg, arg_name in zip([self._anchor_strides, self._anchor_offsets], - ['anchor_strides', 'anchor_offsets']): - if arg and not (isinstance(arg, list) and - len(arg) == len(self._box_specs)): - raise ValueError('%s must be a list with the same length ' - 'as self._box_specs' % arg_name) - if arg and not all([ - isinstance(list_item, tuple) and len(list_item) == 2 - for list_item in arg - ]): - raise ValueError('%s must be a list of pairs.' % arg_name) - - def name_scope(self): - return 'MultipleGridAnchorGenerator' - - def num_anchors_per_location(self): - """Returns the number of anchors per spatial location. - - Returns: - a list of integers, one for each expected feature map to be passed to - the Generate function. - """ - return [len(box_specs) for box_specs in self._box_specs] - - def _generate(self, feature_map_shape_list, im_height=1, im_width=1): - """Generates a collection of bounding boxes to be used as anchors. - - The number of anchors generated for a single grid with shape MxM where we - place k boxes over each grid center is k*M^2 and thus the total number of - anchors is the sum over all grids. In our box_specs_list example - (see the constructor docstring), we would place two boxes over each grid - point on an 8x8 grid and three boxes over each grid point on a 4x4 grid and - thus end up with 2*8^2 + 3*4^2 = 176 anchors in total. The layout of the - output anchors follows the order of how the grid sizes and box_specs are - specified (with box_spec index varying the fastest, followed by width - index, then height index, then grid index). - - Args: - feature_map_shape_list: list of pairs of convnet layer resolutions in the - format [(height_0, width_0), (height_1, width_1), ...]. For example, - setting feature_map_shape_list=[(8, 8), (7, 7)] asks for anchors that - correspond to an 8x8 layer followed by a 7x7 layer. - im_height: the height of the image to generate the grid for. If both - im_height and im_width are 1, the generated anchors default to - absolute coordinates, otherwise normalized coordinates are produced. - im_width: the width of the image to generate the grid for. If both - im_height and im_width are 1, the generated anchors default to - absolute coordinates, otherwise normalized coordinates are produced. - - Returns: - boxes_list: a list of BoxLists each holding anchor boxes corresponding to - the input feature map shapes. - - Raises: - ValueError: if feature_map_shape_list, box_specs_list do not have the same - length. - ValueError: if feature_map_shape_list does not consist of pairs of - integers - """ - if not (isinstance(feature_map_shape_list, list) - and len(feature_map_shape_list) == len(self._box_specs)): - raise ValueError('feature_map_shape_list must be a list with the same ' - 'length as self._box_specs') - if not all([isinstance(list_item, tuple) and len(list_item) == 2 - for list_item in feature_map_shape_list]): - raise ValueError('feature_map_shape_list must be a list of pairs.') - - im_height = tf.cast(im_height, dtype=tf.float32) - im_width = tf.cast(im_width, dtype=tf.float32) - - if not self._anchor_strides: - anchor_strides = [(1.0 / tf.cast(pair[0], dtype=tf.float32), - 1.0 / tf.cast(pair[1], dtype=tf.float32)) - for pair in feature_map_shape_list] - else: - anchor_strides = [(tf.cast(stride[0], dtype=tf.float32) / im_height, - tf.cast(stride[1], dtype=tf.float32) / im_width) - for stride in self._anchor_strides] - if not self._anchor_offsets: - anchor_offsets = [(0.5 * stride[0], 0.5 * stride[1]) - for stride in anchor_strides] - else: - anchor_offsets = [(tf.cast(offset[0], dtype=tf.float32) / im_height, - tf.cast(offset[1], dtype=tf.float32) / im_width) - for offset in self._anchor_offsets] - - for arg, arg_name in zip([anchor_strides, anchor_offsets], - ['anchor_strides', 'anchor_offsets']): - if not (isinstance(arg, list) and len(arg) == len(self._box_specs)): - raise ValueError('%s must be a list with the same length ' - 'as self._box_specs' % arg_name) - if not all([isinstance(list_item, tuple) and len(list_item) == 2 - for list_item in arg]): - raise ValueError('%s must be a list of pairs.' % arg_name) - - anchor_grid_list = [] - min_im_shape = tf.minimum(im_height, im_width) - scale_height = min_im_shape / im_height - scale_width = min_im_shape / im_width - if not tf.is_tensor(self._base_anchor_size): - base_anchor_size = [ - scale_height * tf.constant(self._base_anchor_size[0], - dtype=tf.float32), - scale_width * tf.constant(self._base_anchor_size[1], - dtype=tf.float32) - ] - else: - base_anchor_size = [ - scale_height * self._base_anchor_size[0], - scale_width * self._base_anchor_size[1] - ] - for feature_map_index, (grid_size, scales, aspect_ratios, stride, - offset) in enumerate( - zip(feature_map_shape_list, self._scales, - self._aspect_ratios, anchor_strides, - anchor_offsets)): - tiled_anchors = grid_anchor_generator.tile_anchors( - grid_height=grid_size[0], - grid_width=grid_size[1], - scales=scales, - aspect_ratios=aspect_ratios, - base_anchor_size=base_anchor_size, - anchor_stride=stride, - anchor_offset=offset) - if self._clip_window is not None: - tiled_anchors = box_list_ops.clip_to_window( - tiled_anchors, self._clip_window, filter_nonoverlapping=False) - num_anchors_in_layer = tiled_anchors.num_boxes_static() - if num_anchors_in_layer is None: - num_anchors_in_layer = tiled_anchors.num_boxes() - anchor_indices = feature_map_index * tf.ones([num_anchors_in_layer]) - tiled_anchors.add_field('feature_map_index', anchor_indices) - anchor_grid_list.append(tiled_anchors) - - return anchor_grid_list - - -def create_ssd_anchors(num_layers=6, - min_scale=0.2, - max_scale=0.95, - scales=None, - aspect_ratios=(1.0, 2.0, 3.0, 1.0 / 2, 1.0 / 3), - interpolated_scale_aspect_ratio=1.0, - base_anchor_size=None, - anchor_strides=None, - anchor_offsets=None, - reduce_boxes_in_lowest_layer=True): - """Creates MultipleGridAnchorGenerator for SSD anchors. - - This function instantiates a MultipleGridAnchorGenerator that reproduces - ``default box`` construction proposed by Liu et al in the SSD paper. - See Section 2.2 for details. Grid sizes are assumed to be passed in - at generation time from finest resolution to coarsest resolution --- this is - used to (linearly) interpolate scales of anchor boxes corresponding to the - intermediate grid sizes. - - Anchors that are returned by calling the `generate` method on the returned - MultipleGridAnchorGenerator object are always in normalized coordinates - and clipped to the unit square: (i.e. all coordinates lie in [0, 1]x[0, 1]). - - Args: - num_layers: integer number of grid layers to create anchors for (actual - grid sizes passed in at generation time) - min_scale: scale of anchors corresponding to finest resolution (float) - max_scale: scale of anchors corresponding to coarsest resolution (float) - scales: As list of anchor scales to use. When not None and not empty, - min_scale and max_scale are not used. - aspect_ratios: list or tuple of (float) aspect ratios to place on each - grid point. - interpolated_scale_aspect_ratio: An additional anchor is added with this - aspect ratio and a scale interpolated between the scale for a layer - and the scale for the next layer (1.0 for the last layer). - This anchor is not included if this value is 0. - base_anchor_size: base anchor size as [height, width]. - The height and width values are normalized to the minimum dimension of the - input height and width, so that when the base anchor height equals the - base anchor width, the resulting anchor is square even if the input image - is not square. - anchor_strides: list of pairs of strides in pixels (in y and x directions - respectively). For example, setting anchor_strides=[(25, 25), (50, 50)] - means that we want the anchors corresponding to the first layer to be - strided by 25 pixels and those in the second layer to be strided by 50 - pixels in both y and x directions. If anchor_strides=None, they are set to - be the reciprocal of the corresponding feature map shapes. - anchor_offsets: list of pairs of offsets in pixels (in y and x directions - respectively). The offset specifies where we want the center of the - (0, 0)-th anchor to lie for each layer. For example, setting - anchor_offsets=[(10, 10), (20, 20)]) means that we want the - (0, 0)-th anchor of the first layer to lie at (10, 10) in pixel space - and likewise that we want the (0, 0)-th anchor of the second layer to lie - at (25, 25) in pixel space. If anchor_offsets=None, then they are set to - be half of the corresponding anchor stride. - reduce_boxes_in_lowest_layer: a boolean to indicate whether the fixed 3 - boxes per location is used in the lowest layer. - - Returns: - a MultipleGridAnchorGenerator - """ - if base_anchor_size is None: - base_anchor_size = [1.0, 1.0] - box_specs_list = [] - if scales is None or not scales: - scales = [min_scale + (max_scale - min_scale) * i / (num_layers - 1) - for i in range(num_layers)] + [1.0] - else: - # Add 1.0 to the end, which will only be used in scale_next below and used - # for computing an interpolated scale for the largest scale in the list. - scales += [1.0] - - for layer, scale, scale_next in zip( - range(num_layers), scales[:-1], scales[1:]): - layer_box_specs = [] - if layer == 0 and reduce_boxes_in_lowest_layer: - layer_box_specs = [(0.1, 1.0), (scale, 2.0), (scale, 0.5)] - else: - for aspect_ratio in aspect_ratios: - layer_box_specs.append((scale, aspect_ratio)) - # Add one more anchor, with a scale between the current scale, and the - # scale for the next layer, with a specified aspect ratio (1.0 by - # default). - if interpolated_scale_aspect_ratio > 0.0: - layer_box_specs.append((np.sqrt(scale*scale_next), - interpolated_scale_aspect_ratio)) - box_specs_list.append(layer_box_specs) - - return MultipleGridAnchorGenerator(box_specs_list, base_anchor_size, - anchor_strides, anchor_offsets) diff --git a/research/object_detection/anchor_generators/multiple_grid_anchor_generator_test.py b/research/object_detection/anchor_generators/multiple_grid_anchor_generator_test.py deleted file mode 100644 index 1d4cf19b56c..00000000000 --- a/research/object_detection/anchor_generators/multiple_grid_anchor_generator_test.py +++ /dev/null @@ -1,289 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for anchor_generators.multiple_grid_anchor_generator_test.py.""" - -import numpy as np - -import tensorflow.compat.v1 as tf - -from object_detection.anchor_generators import multiple_grid_anchor_generator as ag -from object_detection.utils import test_case - - -class MultipleGridAnchorGeneratorTest(test_case.TestCase): - - def test_construct_single_anchor_grid(self): - """Builds a 1x1 anchor grid to test the size of the output boxes.""" - def graph_fn(): - - box_specs_list = [[(.5, .25), (1.0, .25), (2.0, .25), - (.5, 1.0), (1.0, 1.0), (2.0, 1.0), - (.5, 4.0), (1.0, 4.0), (2.0, 4.0)]] - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([256, 256], dtype=tf.float32), - anchor_strides=[(16, 16)], - anchor_offsets=[(7, -3)]) - anchors_list = anchor_generator.generate(feature_map_shape_list=[(1, 1)]) - return anchors_list[0].get() - exp_anchor_corners = [[-121, -35, 135, 29], [-249, -67, 263, 61], - [-505, -131, 519, 125], [-57, -67, 71, 61], - [-121, -131, 135, 125], [-249, -259, 263, 253], - [-25, -131, 39, 125], [-57, -259, 71, 253], - [-121, -515, 135, 509]] - - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_anchor_grid(self): - def graph_fn(): - box_specs_list = [[(0.5, 1.0), (1.0, 1.0), (2.0, 1.0)]] - - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([10, 10], dtype=tf.float32), - anchor_strides=[(19, 19)], - anchor_offsets=[(0, 0)]) - anchors_list = anchor_generator.generate(feature_map_shape_list=[(2, 2)]) - return anchors_list[0].get() - exp_anchor_corners = [[-2.5, -2.5, 2.5, 2.5], [-5., -5., 5., 5.], - [-10., -10., 10., 10.], [-2.5, 16.5, 2.5, 21.5], - [-5., 14., 5, 24], [-10., 9., 10, 29], - [16.5, -2.5, 21.5, 2.5], [14., -5., 24, 5], - [9., -10., 29, 10], [16.5, 16.5, 21.5, 21.5], - [14., 14., 24, 24], [9., 9., 29, 29]] - - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_anchor_grid_non_square(self): - - def graph_fn(): - box_specs_list = [[(1.0, 1.0)]] - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, base_anchor_size=tf.constant([1, 1], - dtype=tf.float32)) - anchors_list = anchor_generator.generate(feature_map_shape_list=[( - tf.constant(1, dtype=tf.int32), tf.constant(2, dtype=tf.int32))]) - return anchors_list[0].get() - - exp_anchor_corners = [[0., -0.25, 1., 0.75], [0., 0.25, 1., 1.25]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_dynamic_size_anchor_grid(self): - - def graph_fn(height, width): - box_specs_list = [[(1.0, 1.0)]] - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, base_anchor_size=tf.constant([1, 1], - dtype=tf.float32)) - anchors_list = anchor_generator.generate(feature_map_shape_list=[(height, - width)]) - return anchors_list[0].get() - - exp_anchor_corners = [[0., -0.25, 1., 0.75], [0., 0.25, 1., 1.25]] - - anchor_corners_out = self.execute_cpu(graph_fn, - [np.array(1, dtype=np.int32), - np.array(2, dtype=np.int32)]) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_anchor_grid_normalized(self): - def graph_fn(): - box_specs_list = [[(1.0, 1.0)]] - - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, base_anchor_size=tf.constant([1, 1], - dtype=tf.float32)) - anchors_list = anchor_generator.generate( - feature_map_shape_list=[(tf.constant(1, dtype=tf.int32), tf.constant( - 2, dtype=tf.int32))], - im_height=320, - im_width=640) - return anchors_list[0].get() - - exp_anchor_corners = [[0., 0., 1., 0.5], [0., 0.5, 1., 1.]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_multiple_grids(self): - - def graph_fn(): - box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)], - [(1.0, 1.0), (1.0, 0.5)]] - - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - anchor_strides=[(.25, .25), (.5, .5)], - anchor_offsets=[(.125, .125), (.25, .25)]) - anchors_list = anchor_generator.generate(feature_map_shape_list=[(4, 4), ( - 2, 2)]) - return [anchors.get() for anchors in anchors_list] - # height and width of box with .5 aspect ratio - h = np.sqrt(2) - w = 1.0/np.sqrt(2) - exp_small_grid_corners = [[-.25, -.25, .75, .75], - [.25-.5*h, .25-.5*w, .25+.5*h, .25+.5*w], - [-.25, .25, .75, 1.25], - [.25-.5*h, .75-.5*w, .25+.5*h, .75+.5*w], - [.25, -.25, 1.25, .75], - [.75-.5*h, .25-.5*w, .75+.5*h, .25+.5*w], - [.25, .25, 1.25, 1.25], - [.75-.5*h, .75-.5*w, .75+.5*h, .75+.5*w]] - # only test first entry of larger set of anchors - exp_big_grid_corners = [[.125-.5, .125-.5, .125+.5, .125+.5], - [.125-1.0, .125-1.0, .125+1.0, .125+1.0], - [.125-.5*h, .125-.5*w, .125+.5*h, .125+.5*w],] - - anchor_corners_out = np.concatenate(self.execute(graph_fn, []), axis=0) - self.assertEqual(anchor_corners_out.shape, (56, 4)) - big_grid_corners = anchor_corners_out[0:3, :] - small_grid_corners = anchor_corners_out[48:, :] - self.assertAllClose(small_grid_corners, exp_small_grid_corners) - self.assertAllClose(big_grid_corners, exp_big_grid_corners) - - def test_construct_multiple_grids_with_clipping(self): - - def graph_fn(): - box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)], - [(1.0, 1.0), (1.0, 0.5)]] - - clip_window = tf.constant([0, 0, 1, 1], dtype=tf.float32) - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - clip_window=clip_window) - anchors_list = anchor_generator.generate(feature_map_shape_list=[(4, 4), ( - 2, 2)]) - return [anchors.get() for anchors in anchors_list] - # height and width of box with .5 aspect ratio - h = np.sqrt(2) - w = 1.0/np.sqrt(2) - exp_small_grid_corners = [[0, 0, .75, .75], - [0, 0, .25+.5*h, .25+.5*w], - [0, .25, .75, 1], - [0, .75-.5*w, .25+.5*h, 1], - [.25, 0, 1, .75], - [.75-.5*h, 0, 1, .25+.5*w], - [.25, .25, 1, 1], - [.75-.5*h, .75-.5*w, 1, 1]] - - anchor_corners_out = np.concatenate(self.execute(graph_fn, []), axis=0) - small_grid_corners = anchor_corners_out[48:, :] - self.assertAllClose(small_grid_corners, exp_small_grid_corners) - - def test_invalid_box_specs(self): - # not all box specs are pairs - box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)], - [(1.0, 1.0), (1.0, 0.5, .3)]] - with self.assertRaises(ValueError): - ag.MultipleGridAnchorGenerator(box_specs_list) - - # box_specs_list is not a list of lists - box_specs_list = [(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)] - with self.assertRaises(ValueError): - ag.MultipleGridAnchorGenerator(box_specs_list) - - def test_invalid_generate_arguments(self): - box_specs_list = [[(1.0, 1.0), (2.0, 1.0), (1.0, 0.5)], - [(1.0, 1.0), (1.0, 0.5)]] - - # incompatible lengths with box_specs_list - with self.assertRaises(ValueError): - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - anchor_strides=[(.25, .25)], - anchor_offsets=[(.125, .125), (.25, .25)]) - anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)]) - with self.assertRaises(ValueError): - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - anchor_strides=[(.25, .25), (.5, .5)], - anchor_offsets=[(.125, .125), (.25, .25)]) - anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2), (1, 1)]) - with self.assertRaises(ValueError): - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - anchor_strides=[(.5, .5)], - anchor_offsets=[(.25, .25)]) - anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)]) - - # not pairs - with self.assertRaises(ValueError): - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - anchor_strides=[(.25, .25), (.5, .5)], - anchor_offsets=[(.125, .125), (.25, .25)]) - anchor_generator.generate(feature_map_shape_list=[(4, 4, 4), (2, 2)]) - with self.assertRaises(ValueError): - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - anchor_strides=[(.25, .25, .1), (.5, .5)], - anchor_offsets=[(.125, .125), (.25, .25)]) - anchor_generator.generate(feature_map_shape_list=[(4, 4), (2, 2)]) - with self.assertRaises(ValueError): - anchor_generator = ag.MultipleGridAnchorGenerator( - box_specs_list, - base_anchor_size=tf.constant([1.0, 1.0], dtype=tf.float32), - anchor_strides=[(.25, .25), (.5, .5)], - anchor_offsets=[(.125, .125), (.25, .25)]) - anchor_generator.generate(feature_map_shape_list=[(4), (2, 2)]) - - -class CreateSSDAnchorsTest(test_case.TestCase): - - def test_create_ssd_anchors_returns_correct_shape(self): - - def graph_fn1(): - anchor_generator = ag.create_ssd_anchors( - num_layers=6, - min_scale=0.2, - max_scale=0.95, - aspect_ratios=(1.0, 2.0, 3.0, 1.0 / 2, 1.0 / 3), - reduce_boxes_in_lowest_layer=True) - - feature_map_shape_list = [(38, 38), (19, 19), (10, 10), - (5, 5), (3, 3), (1, 1)] - anchors_list = anchor_generator.generate( - feature_map_shape_list=feature_map_shape_list) - return [anchors.get() for anchors in anchors_list] - anchor_corners_out = np.concatenate(self.execute(graph_fn1, []), axis=0) - self.assertEqual(anchor_corners_out.shape, (7308, 4)) - - def graph_fn2(): - anchor_generator = ag.create_ssd_anchors( - num_layers=6, min_scale=0.2, max_scale=0.95, - aspect_ratios=(1.0, 2.0, 3.0, 1.0/2, 1.0/3), - reduce_boxes_in_lowest_layer=False) - - feature_map_shape_list = [(38, 38), (19, 19), (10, 10), - (5, 5), (3, 3), (1, 1)] - anchors_list = anchor_generator.generate( - feature_map_shape_list=feature_map_shape_list) - return [anchors.get() for anchors in anchors_list] - anchor_corners_out = np.concatenate(self.execute(graph_fn2, []), axis=0) - self.assertEqual(anchor_corners_out.shape, (11640, 4)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/anchor_generators/multiscale_grid_anchor_generator.py b/research/object_detection/anchor_generators/multiscale_grid_anchor_generator.py deleted file mode 100644 index a3244e1b196..00000000000 --- a/research/object_detection/anchor_generators/multiscale_grid_anchor_generator.py +++ /dev/null @@ -1,152 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Generates grid anchors on the fly corresponding to multiple CNN layers. - -Generates grid anchors on the fly corresponding to multiple CNN layers as -described in: -"Focal Loss for Dense Object Detection" (https://arxiv.org/abs/1708.02002) -T.-Y. Lin, P. Goyal, R. Girshick, K. He, P. Dollar -""" - -import tensorflow.compat.v1 as tf - -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.core import anchor_generator -from object_detection.core import box_list_ops - - -class MultiscaleGridAnchorGenerator(anchor_generator.AnchorGenerator): - """Generate a grid of anchors for multiple CNN layers of different scale.""" - - def __init__(self, min_level, max_level, anchor_scale, aspect_ratios, - scales_per_octave, normalize_coordinates=True): - """Constructs a MultiscaleGridAnchorGenerator. - - To construct anchors, at multiple scale resolutions, one must provide a - the minimum level and maximum levels on a scale pyramid. To define the size - of anchor, the anchor scale is provided to decide the size relatively to the - stride of the corresponding feature map. The generator allows one pixel - location on feature map maps to multiple anchors, that have different aspect - ratios and intermediate scales. - - Args: - min_level: minimum level in feature pyramid. - max_level: maximum level in feature pyramid. - anchor_scale: anchor scale and feature stride define the size of the base - anchor on an image. For example, given a feature pyramid with strides - [2^3, ..., 2^7] and anchor scale 4. The base anchor size is - 4 * [2^3, ..., 2^7]. - aspect_ratios: list or tuple of (float) aspect ratios to place on each - grid point. - scales_per_octave: integer number of intermediate scales per scale octave. - normalize_coordinates: whether to produce anchors in normalized - coordinates. (defaults to True). - """ - self._anchor_grid_info = [] - self._aspect_ratios = aspect_ratios - self._scales_per_octave = scales_per_octave - self._normalize_coordinates = normalize_coordinates - - scales = [2**(float(scale) / scales_per_octave) - for scale in range(scales_per_octave)] - aspects = list(aspect_ratios) - - for level in range(min_level, max_level + 1): - anchor_stride = [2**level, 2**level] - base_anchor_size = [2**level * anchor_scale, 2**level * anchor_scale] - self._anchor_grid_info.append({ - 'level': level, - 'info': [scales, aspects, base_anchor_size, anchor_stride] - }) - - def name_scope(self): - return 'MultiscaleGridAnchorGenerator' - - def num_anchors_per_location(self): - """Returns the number of anchors per spatial location. - - Returns: - a list of integers, one for each expected feature map to be passed to - the Generate function. - """ - return len(self._anchor_grid_info) * [ - len(self._aspect_ratios) * self._scales_per_octave] - - def _generate(self, feature_map_shape_list, im_height=1, im_width=1): - """Generates a collection of bounding boxes to be used as anchors. - - For training, we require the input image shape to be statically defined. - That is, im_height and im_width should be integers rather than tensors. - For inference, im_height and im_width can be either integers (for fixed - image size), or tensors (for arbitrary image size). - - Args: - feature_map_shape_list: list of pairs of convnet layer resolutions in the - format [(height_0, width_0), (height_1, width_1), ...]. For example, - setting feature_map_shape_list=[(8, 8), (7, 7)] asks for anchors that - correspond to an 8x8 layer followed by a 7x7 layer. - im_height: the height of the image to generate the grid for. If both - im_height and im_width are 1, anchors can only be generated in - absolute coordinates. - im_width: the width of the image to generate the grid for. If both - im_height and im_width are 1, anchors can only be generated in - absolute coordinates. - - Returns: - boxes_list: a list of BoxLists each holding anchor boxes corresponding to - the input feature map shapes. - Raises: - ValueError: if im_height and im_width are not integers. - ValueError: if im_height and im_width are 1, but normalized coordinates - were requested. - """ - anchor_grid_list = [] - for feat_shape, grid_info in zip(feature_map_shape_list, - self._anchor_grid_info): - # TODO(rathodv) check the feature_map_shape_list is consistent with - # self._anchor_grid_info - level = grid_info['level'] - stride = 2**level - scales, aspect_ratios, base_anchor_size, anchor_stride = grid_info['info'] - feat_h = feat_shape[0] - feat_w = feat_shape[1] - anchor_offset = [0, 0] - if isinstance(im_height, int) and isinstance(im_width, int): - if im_height % 2.0**level == 0 or im_height == 1: - anchor_offset[0] = stride / 2.0 - if im_width % 2.0**level == 0 or im_width == 1: - anchor_offset[1] = stride / 2.0 - if tf.is_tensor(im_height) and tf.is_tensor(im_width): - anchor_offset[0] = stride / 2.0 - anchor_offset[1] = stride / 2.0 - ag = grid_anchor_generator.GridAnchorGenerator( - scales, - aspect_ratios, - base_anchor_size=base_anchor_size, - anchor_stride=anchor_stride, - anchor_offset=anchor_offset) - (anchor_grid,) = ag.generate(feature_map_shape_list=[(feat_h, feat_w)]) - - if self._normalize_coordinates: - if im_height == 1 or im_width == 1: - raise ValueError( - 'Normalized coordinates were requested upon construction of the ' - 'MultiscaleGridAnchorGenerator, but a subsequent call to ' - 'generate did not supply dimension information.') - anchor_grid = box_list_ops.to_normalized_coordinates( - anchor_grid, im_height, im_width, check_range=False) - anchor_grid_list.append(anchor_grid) - - return anchor_grid_list diff --git a/research/object_detection/anchor_generators/multiscale_grid_anchor_generator_test.py b/research/object_detection/anchor_generators/multiscale_grid_anchor_generator_test.py deleted file mode 100644 index 82aa8d1df0b..00000000000 --- a/research/object_detection/anchor_generators/multiscale_grid_anchor_generator_test.py +++ /dev/null @@ -1,308 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for anchor_generators.multiscale_grid_anchor_generator_test.py.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.anchor_generators import multiscale_grid_anchor_generator as mg -from object_detection.utils import test_case - - -class MultiscaleGridAnchorGeneratorTest(test_case.TestCase): - - def test_construct_single_anchor(self): - def graph_fn(): - min_level = 5 - max_level = 5 - anchor_scale = 4.0 - aspect_ratios = [1.0] - scales_per_octave = 1 - im_height = 64 - im_width = 64 - feature_map_shape_list = [(2, 2)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - - exp_anchor_corners = [[-48, -48, 80, 80], - [-48, -16, 80, 112], - [-16, -48, 112, 80], - [-16, -16, 112, 112]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_unit_dimensions(self): - def graph_fn(): - min_level = 5 - max_level = 5 - anchor_scale = 1.0 - aspect_ratios = [1.0] - scales_per_octave = 1 - im_height = 1 - im_width = 1 - feature_map_shape_list = [(2, 2)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - - # Positive offsets are produced. - exp_anchor_corners = [[0, 0, 32, 32], - [0, 32, 32, 64], - [32, 0, 64, 32], - [32, 32, 64, 64]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_normalized_anchors_fails_with_unit_dimensions(self): - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level=5, max_level=5, anchor_scale=1.0, aspect_ratios=[1.0], - scales_per_octave=1, normalize_coordinates=True) - with self.assertRaisesRegexp(ValueError, 'Normalized coordinates'): - anchor_generator.generate( - feature_map_shape_list=[(2, 2)], im_height=1, im_width=1) - - def test_construct_single_anchor_in_normalized_coordinates(self): - def graph_fn(): - min_level = 5 - max_level = 5 - anchor_scale = 4.0 - aspect_ratios = [1.0] - scales_per_octave = 1 - im_height = 64 - im_width = 128 - feature_map_shape_list = [(2, 2)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=True) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - - exp_anchor_corners = [[-48./64, -48./128, 80./64, 80./128], - [-48./64, -16./128, 80./64, 112./128], - [-16./64, -48./128, 112./64, 80./128], - [-16./64, -16./128, 112./64, 112./128]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_num_anchors_per_location(self): - min_level = 5 - max_level = 6 - anchor_scale = 4.0 - aspect_ratios = [1.0, 2.0] - scales_per_octave = 3 - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - self.assertEqual(anchor_generator.num_anchors_per_location(), [6, 6]) - - def test_construct_single_anchor_dynamic_size(self): - def graph_fn(): - min_level = 5 - max_level = 5 - anchor_scale = 4.0 - aspect_ratios = [1.0] - scales_per_octave = 1 - im_height = tf.constant(64) - im_width = tf.constant(64) - feature_map_shape_list = [(2, 2)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return anchor_corners - - exp_anchor_corners = [[-64, -64, 64, 64], - [-64, -32, 64, 96], - [-32, -64, 96, 64], - [-32, -32, 96, 96]] - # Add anchor offset. - anchor_offset = 2.0**5 / 2.0 - exp_anchor_corners = [ - [b + anchor_offset for b in a] for a in exp_anchor_corners - ] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_with_odd_input_dimension(self): - - def graph_fn(): - min_level = 5 - max_level = 5 - anchor_scale = 4.0 - aspect_ratios = [1.0] - scales_per_octave = 1 - im_height = 65 - im_width = 65 - feature_map_shape_list = [(3, 3)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate( - feature_map_shape_list, im_height=im_height, im_width=im_width) - anchor_corners = anchors_list[0].get() - return (anchor_corners,) - anchor_corners_out = self.execute(graph_fn, []) - exp_anchor_corners = [[-64, -64, 64, 64], - [-64, -32, 64, 96], - [-64, 0, 64, 128], - [-32, -64, 96, 64], - [-32, -32, 96, 96], - [-32, 0, 96, 128], - [0, -64, 128, 64], - [0, -32, 128, 96], - [0, 0, 128, 128]] - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_on_two_feature_maps(self): - - def graph_fn(): - min_level = 5 - max_level = 6 - anchor_scale = 4.0 - aspect_ratios = [1.0] - scales_per_octave = 1 - im_height = 64 - im_width = 64 - feature_map_shape_list = [(2, 2), (1, 1)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - - anchor_corners_out = np.concatenate(self.execute(graph_fn, []), axis=0) - exp_anchor_corners = [[-48, -48, 80, 80], - [-48, -16, 80, 112], - [-16, -48, 112, 80], - [-16, -16, 112, 112], - [-96, -96, 160, 160]] - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_with_two_scales_per_octave(self): - - def graph_fn(): - min_level = 6 - max_level = 6 - anchor_scale = 4.0 - aspect_ratios = [1.0] - scales_per_octave = 2 - im_height = 64 - im_width = 64 - feature_map_shape_list = [(1, 1)] - - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - # There are 4 set of anchors in this configuration. The order is: - # [[2**0.0 intermediate scale + 1.0 aspect], - # [2**0.5 intermediate scale + 1.0 aspect]] - exp_anchor_corners = [[-96., -96., 160., 160.], - [-149.0193, -149.0193, 213.0193, 213.0193]] - - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchor_with_two_scales_per_octave_and_aspect(self): - def graph_fn(): - min_level = 6 - max_level = 6 - anchor_scale = 4.0 - aspect_ratios = [1.0, 2.0] - scales_per_octave = 2 - im_height = 64 - im_width = 64 - feature_map_shape_list = [(1, 1)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - # There are 4 set of anchors in this configuration. The order is: - # [[2**0.0 intermediate scale + 1.0 aspect], - # [2**0.5 intermediate scale + 1.0 aspect], - # [2**0.0 intermediate scale + 2.0 aspect], - # [2**0.5 intermediate scale + 2.0 aspect]] - - exp_anchor_corners = [[-96., -96., 160., 160.], - [-149.0193, -149.0193, 213.0193, 213.0193], - [-58.50967, -149.0193, 122.50967, 213.0193], - [-96., -224., 160., 288.]] - anchor_corners_out = self.execute(graph_fn, []) - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - def test_construct_single_anchors_on_feature_maps_with_dynamic_shape(self): - - def graph_fn(feature_map1_height, feature_map1_width, feature_map2_height, - feature_map2_width): - min_level = 5 - max_level = 6 - anchor_scale = 4.0 - aspect_ratios = [1.0] - scales_per_octave = 1 - im_height = 64 - im_width = 64 - feature_map_shape_list = [(feature_map1_height, feature_map1_width), - (feature_map2_height, feature_map2_width)] - anchor_generator = mg.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates=False) - anchors_list = anchor_generator.generate(feature_map_shape_list, - im_height=im_height, - im_width=im_width) - anchor_corners = [anchors.get() for anchors in anchors_list] - return anchor_corners - - anchor_corners_out = np.concatenate( - self.execute_cpu(graph_fn, [ - np.array(2, dtype=np.int32), - np.array(2, dtype=np.int32), - np.array(1, dtype=np.int32), - np.array(1, dtype=np.int32) - ]), - axis=0) - exp_anchor_corners = [[-48, -48, 80, 80], - [-48, -16, 80, 112], - [-16, -48, 112, 80], - [-16, -16, 112, 112], - [-96, -96, 160, 160]] - self.assertAllClose(anchor_corners_out, exp_anchor_corners) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/box_coders/__init__.py b/research/object_detection/box_coders/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/box_coders/faster_rcnn_box_coder.py b/research/object_detection/box_coders/faster_rcnn_box_coder.py deleted file mode 100644 index e06c1b12d2c..00000000000 --- a/research/object_detection/box_coders/faster_rcnn_box_coder.py +++ /dev/null @@ -1,118 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Faster RCNN box coder. - -Faster RCNN box coder follows the coding schema described below: - ty = (y - ya) / ha - tx = (x - xa) / wa - th = log(h / ha) - tw = log(w / wa) - where x, y, w, h denote the box's center coordinates, width and height - respectively. Similarly, xa, ya, wa, ha denote the anchor's center - coordinates, width and height. tx, ty, tw and th denote the anchor-encoded - center, width and height respectively. - - See http://arxiv.org/abs/1506.01497 for details. -""" - -import tensorflow.compat.v1 as tf - -from object_detection.core import box_coder -from object_detection.core import box_list - -EPSILON = 1e-8 - - -class FasterRcnnBoxCoder(box_coder.BoxCoder): - """Faster RCNN box coder.""" - - def __init__(self, scale_factors=None): - """Constructor for FasterRcnnBoxCoder. - - Args: - scale_factors: List of 4 positive scalars to scale ty, tx, th and tw. - If set to None, does not perform scaling. For Faster RCNN, - the open-source implementation recommends using [10.0, 10.0, 5.0, 5.0]. - """ - if scale_factors: - assert len(scale_factors) == 4 - for scalar in scale_factors: - assert scalar > 0 - self._scale_factors = scale_factors - - @property - def code_size(self): - return 4 - - def _encode(self, boxes, anchors): - """Encode a box collection with respect to anchor collection. - - Args: - boxes: BoxList holding N boxes to be encoded. - anchors: BoxList of anchors. - - Returns: - a tensor representing N anchor-encoded boxes of the format - [ty, tx, th, tw]. - """ - # Convert anchors to the center coordinate representation. - ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes() - ycenter, xcenter, h, w = boxes.get_center_coordinates_and_sizes() - # Avoid NaN in division and log below. - ha += EPSILON - wa += EPSILON - h += EPSILON - w += EPSILON - - tx = (xcenter - xcenter_a) / wa - ty = (ycenter - ycenter_a) / ha - tw = tf.log(w / wa) - th = tf.log(h / ha) - # Scales location targets as used in paper for joint training. - if self._scale_factors: - ty *= self._scale_factors[0] - tx *= self._scale_factors[1] - th *= self._scale_factors[2] - tw *= self._scale_factors[3] - return tf.transpose(tf.stack([ty, tx, th, tw])) - - def _decode(self, rel_codes, anchors): - """Decode relative codes to boxes. - - Args: - rel_codes: a tensor representing N anchor-encoded boxes. - anchors: BoxList of anchors. - - Returns: - boxes: BoxList holding N bounding boxes. - """ - ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes() - - ty, tx, th, tw = tf.unstack(tf.transpose(rel_codes)) - if self._scale_factors: - ty /= self._scale_factors[0] - tx /= self._scale_factors[1] - th /= self._scale_factors[2] - tw /= self._scale_factors[3] - w = tf.exp(tw) * wa - h = tf.exp(th) * ha - ycenter = ty * ha + ycenter_a - xcenter = tx * wa + xcenter_a - ymin = ycenter - h / 2. - xmin = xcenter - w / 2. - ymax = ycenter + h / 2. - xmax = xcenter + w / 2. - return box_list.BoxList(tf.transpose(tf.stack([ymin, xmin, ymax, xmax]))) diff --git a/research/object_detection/box_coders/faster_rcnn_box_coder_test.py b/research/object_detection/box_coders/faster_rcnn_box_coder_test.py deleted file mode 100644 index 1cd48279af9..00000000000 --- a/research/object_detection/box_coders/faster_rcnn_box_coder_test.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.box_coder.faster_rcnn_box_coder.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.box_coders import faster_rcnn_box_coder -from object_detection.core import box_list -from object_detection.utils import test_case - - -class FasterRcnnBoxCoderTest(test_case.TestCase): - - def test_get_correct_relative_codes_after_encoding(self): - boxes = np.array([[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]], - np.float32) - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - expected_rel_codes = [[-0.5, -0.416666, -0.405465, -0.182321], - [-0.083333, -0.222222, -0.693147, -1.098612]] - def graph_fn(boxes, anchors): - boxes = box_list.BoxList(boxes) - anchors = box_list.BoxList(anchors) - coder = faster_rcnn_box_coder.FasterRcnnBoxCoder() - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - def test_get_correct_relative_codes_after_encoding_with_scaling(self): - boxes = np.array([[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]], - np.float32) - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - expected_rel_codes = [[-1., -1.25, -1.62186, -0.911608], - [-0.166667, -0.666667, -2.772588, -5.493062]] - def graph_fn(boxes, anchors): - scale_factors = [2, 3, 4, 5] - boxes = box_list.BoxList(boxes) - anchors = box_list.BoxList(anchors) - coder = faster_rcnn_box_coder.FasterRcnnBoxCoder( - scale_factors=scale_factors) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - def test_get_correct_boxes_after_decoding(self): - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - rel_codes = np.array([[-0.5, -0.416666, -0.405465, -0.182321], - [-0.083333, -0.222222, -0.693147, -1.098612]], - np.float32) - expected_boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]] - def graph_fn(rel_codes, anchors): - anchors = box_list.BoxList(anchors) - coder = faster_rcnn_box_coder.FasterRcnnBoxCoder() - boxes = coder.decode(rel_codes, anchors) - return boxes.get() - boxes_out = self.execute(graph_fn, [rel_codes, anchors]) - self.assertAllClose(boxes_out, expected_boxes, rtol=1e-04, - atol=1e-04) - - def test_get_correct_boxes_after_decoding_with_scaling(self): - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - rel_codes = np.array([[-1., -1.25, -1.62186, -0.911608], - [-0.166667, -0.666667, -2.772588, -5.493062]], - np.float32) - expected_boxes = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]] - def graph_fn(rel_codes, anchors): - scale_factors = [2, 3, 4, 5] - anchors = box_list.BoxList(anchors) - coder = faster_rcnn_box_coder.FasterRcnnBoxCoder( - scale_factors=scale_factors) - boxes = coder.decode(rel_codes, anchors).get() - return boxes - boxes_out = self.execute(graph_fn, [rel_codes, anchors]) - self.assertAllClose(expected_boxes, boxes_out, rtol=1e-04, - atol=1e-04) - - def test_very_small_Width_nan_after_encoding(self): - boxes = np.array([[10.0, 10.0, 10.0000001, 20.0]], np.float32) - anchors = np.array([[15.0, 12.0, 30.0, 18.0]], np.float32) - expected_rel_codes = [[-0.833333, 0., -21.128731, 0.510826]] - def graph_fn(boxes, anchors): - boxes = box_list.BoxList(boxes) - anchors = box_list.BoxList(anchors) - coder = faster_rcnn_box_coder.FasterRcnnBoxCoder() - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/box_coders/keypoint_box_coder.py b/research/object_detection/box_coders/keypoint_box_coder.py deleted file mode 100644 index 7bb4bf8b184..00000000000 --- a/research/object_detection/box_coders/keypoint_box_coder.py +++ /dev/null @@ -1,173 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Keypoint box coder. - -The keypoint box coder follows the coding schema described below (this is -similar to the FasterRcnnBoxCoder, except that it encodes keypoints in addition -to box coordinates): - ty = (y - ya) / ha - tx = (x - xa) / wa - th = log(h / ha) - tw = log(w / wa) - tky0 = (ky0 - ya) / ha - tkx0 = (kx0 - xa) / wa - tky1 = (ky1 - ya) / ha - tkx1 = (kx1 - xa) / wa - ... - where x, y, w, h denote the box's center coordinates, width and height - respectively. Similarly, xa, ya, wa, ha denote the anchor's center - coordinates, width and height. tx, ty, tw and th denote the anchor-encoded - center, width and height respectively. ky0, kx0, ky1, kx1, ... denote the - keypoints' coordinates, and tky0, tkx0, tky1, tkx1, ... denote the - anchor-encoded keypoint coordinates. -""" - -import tensorflow.compat.v1 as tf - -from object_detection.core import box_coder -from object_detection.core import box_list -from object_detection.core import standard_fields as fields - -EPSILON = 1e-8 - - -class KeypointBoxCoder(box_coder.BoxCoder): - """Keypoint box coder.""" - - def __init__(self, num_keypoints, scale_factors=None): - """Constructor for KeypointBoxCoder. - - Args: - num_keypoints: Number of keypoints to encode/decode. - scale_factors: List of 4 positive scalars to scale ty, tx, th and tw. - In addition to scaling ty and tx, the first 2 scalars are used to scale - the y and x coordinates of the keypoints as well. If set to None, does - not perform scaling. - """ - self._num_keypoints = num_keypoints - - if scale_factors: - assert len(scale_factors) == 4 - for scalar in scale_factors: - assert scalar > 0 - self._scale_factors = scale_factors - self._keypoint_scale_factors = None - if scale_factors is not None: - self._keypoint_scale_factors = tf.expand_dims( - tf.tile([ - tf.cast(scale_factors[0], dtype=tf.float32), - tf.cast(scale_factors[1], dtype=tf.float32) - ], [num_keypoints]), 1) - - @property - def code_size(self): - return 4 + self._num_keypoints * 2 - - def _encode(self, boxes, anchors): - """Encode a box and keypoint collection with respect to anchor collection. - - Args: - boxes: BoxList holding N boxes and keypoints to be encoded. Boxes are - tensors with the shape [N, 4], and keypoints are tensors with the shape - [N, num_keypoints, 2]. - anchors: BoxList of anchors. - - Returns: - a tensor representing N anchor-encoded boxes of the format - [ty, tx, th, tw, tky0, tkx0, tky1, tkx1, ...] where tky0 and tkx0 - represent the y and x coordinates of the first keypoint, tky1 and tkx1 - represent the y and x coordinates of the second keypoint, and so on. - """ - # Convert anchors to the center coordinate representation. - ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes() - ycenter, xcenter, h, w = boxes.get_center_coordinates_and_sizes() - keypoints = boxes.get_field(fields.BoxListFields.keypoints) - keypoints = tf.transpose(tf.reshape(keypoints, - [-1, self._num_keypoints * 2])) - num_boxes = boxes.num_boxes() - - # Avoid NaN in division and log below. - ha += EPSILON - wa += EPSILON - h += EPSILON - w += EPSILON - - tx = (xcenter - xcenter_a) / wa - ty = (ycenter - ycenter_a) / ha - tw = tf.log(w / wa) - th = tf.log(h / ha) - - tiled_anchor_centers = tf.tile( - tf.stack([ycenter_a, xcenter_a]), [self._num_keypoints, 1]) - tiled_anchor_sizes = tf.tile( - tf.stack([ha, wa]), [self._num_keypoints, 1]) - tkeypoints = (keypoints - tiled_anchor_centers) / tiled_anchor_sizes - - # Scales location targets as used in paper for joint training. - if self._scale_factors: - ty *= self._scale_factors[0] - tx *= self._scale_factors[1] - th *= self._scale_factors[2] - tw *= self._scale_factors[3] - tkeypoints *= tf.tile(self._keypoint_scale_factors, [1, num_boxes]) - - tboxes = tf.stack([ty, tx, th, tw]) - return tf.transpose(tf.concat([tboxes, tkeypoints], 0)) - - def _decode(self, rel_codes, anchors): - """Decode relative codes to boxes and keypoints. - - Args: - rel_codes: a tensor with shape [N, 4 + 2 * num_keypoints] representing N - anchor-encoded boxes and keypoints - anchors: BoxList of anchors. - - Returns: - boxes: BoxList holding N bounding boxes and keypoints. - """ - ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes() - - num_codes = tf.shape(rel_codes)[0] - result = tf.unstack(tf.transpose(rel_codes)) - ty, tx, th, tw = result[:4] - tkeypoints = result[4:] - if self._scale_factors: - ty /= self._scale_factors[0] - tx /= self._scale_factors[1] - th /= self._scale_factors[2] - tw /= self._scale_factors[3] - tkeypoints /= tf.tile(self._keypoint_scale_factors, [1, num_codes]) - - w = tf.exp(tw) * wa - h = tf.exp(th) * ha - ycenter = ty * ha + ycenter_a - xcenter = tx * wa + xcenter_a - ymin = ycenter - h / 2. - xmin = xcenter - w / 2. - ymax = ycenter + h / 2. - xmax = xcenter + w / 2. - decoded_boxes_keypoints = box_list.BoxList( - tf.transpose(tf.stack([ymin, xmin, ymax, xmax]))) - - tiled_anchor_centers = tf.tile( - tf.stack([ycenter_a, xcenter_a]), [self._num_keypoints, 1]) - tiled_anchor_sizes = tf.tile( - tf.stack([ha, wa]), [self._num_keypoints, 1]) - keypoints = tkeypoints * tiled_anchor_sizes + tiled_anchor_centers - keypoints = tf.reshape(tf.transpose(keypoints), - [-1, self._num_keypoints, 2]) - decoded_boxes_keypoints.add_field(fields.BoxListFields.keypoints, keypoints) - return decoded_boxes_keypoints diff --git a/research/object_detection/box_coders/keypoint_box_coder_test.py b/research/object_detection/box_coders/keypoint_box_coder_test.py deleted file mode 100644 index 5748255c825..00000000000 --- a/research/object_detection/box_coders/keypoint_box_coder_test.py +++ /dev/null @@ -1,151 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.box_coder.keypoint_box_coder.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.box_coders import keypoint_box_coder -from object_detection.core import box_list -from object_detection.core import standard_fields as fields -from object_detection.utils import test_case - - -class KeypointBoxCoderTest(test_case.TestCase): - - def test_get_correct_relative_codes_after_encoding(self): - boxes = np.array([[10., 10., 20., 15.], - [0.2, 0.1, 0.5, 0.4]], np.float32) - keypoints = np.array([[[15., 12.], [10., 15.]], - [[0.5, 0.3], [0.2, 0.4]]], np.float32) - num_keypoints = len(keypoints[0]) - anchors = np.array([[15., 12., 30., 18.], - [0.1, 0.0, 0.7, 0.9]], np.float32) - expected_rel_codes = [ - [-0.5, -0.416666, -0.405465, -0.182321, - -0.5, -0.5, -0.833333, 0.], - [-0.083333, -0.222222, -0.693147, -1.098612, - 0.166667, -0.166667, -0.333333, -0.055556] - ] - def graph_fn(boxes, keypoints, anchors): - boxes = box_list.BoxList(boxes) - boxes.add_field(fields.BoxListFields.keypoints, keypoints) - anchors = box_list.BoxList(anchors) - coder = keypoint_box_coder.KeypointBoxCoder(num_keypoints) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, keypoints, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - def test_get_correct_relative_codes_after_encoding_with_scaling(self): - boxes = np.array([[10., 10., 20., 15.], - [0.2, 0.1, 0.5, 0.4]], np.float32) - keypoints = np.array([[[15., 12.], [10., 15.]], - [[0.5, 0.3], [0.2, 0.4]]], np.float32) - num_keypoints = len(keypoints[0]) - anchors = np.array([[15., 12., 30., 18.], - [0.1, 0.0, 0.7, 0.9]], np.float32) - expected_rel_codes = [ - [-1., -1.25, -1.62186, -0.911608, - -1.0, -1.5, -1.666667, 0.], - [-0.166667, -0.666667, -2.772588, -5.493062, - 0.333333, -0.5, -0.666667, -0.166667] - ] - def graph_fn(boxes, keypoints, anchors): - scale_factors = [2, 3, 4, 5] - boxes = box_list.BoxList(boxes) - boxes.add_field(fields.BoxListFields.keypoints, keypoints) - anchors = box_list.BoxList(anchors) - coder = keypoint_box_coder.KeypointBoxCoder( - num_keypoints, scale_factors=scale_factors) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, keypoints, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - def test_get_correct_boxes_after_decoding(self): - anchors = np.array([[15., 12., 30., 18.], - [0.1, 0.0, 0.7, 0.9]], np.float32) - rel_codes = np.array([ - [-0.5, -0.416666, -0.405465, -0.182321, - -0.5, -0.5, -0.833333, 0.], - [-0.083333, -0.222222, -0.693147, -1.098612, - 0.166667, -0.166667, -0.333333, -0.055556] - ], np.float32) - expected_boxes = [[10., 10., 20., 15.], - [0.2, 0.1, 0.5, 0.4]] - expected_keypoints = [[[15., 12.], [10., 15.]], - [[0.5, 0.3], [0.2, 0.4]]] - num_keypoints = len(expected_keypoints[0]) - def graph_fn(rel_codes, anchors): - anchors = box_list.BoxList(anchors) - coder = keypoint_box_coder.KeypointBoxCoder(num_keypoints) - boxes = coder.decode(rel_codes, anchors) - return boxes.get(), boxes.get_field(fields.BoxListFields.keypoints) - boxes_out, keypoints_out = self.execute(graph_fn, [rel_codes, anchors]) - self.assertAllClose(keypoints_out, expected_keypoints, rtol=1e-04, - atol=1e-04) - self.assertAllClose(boxes_out, expected_boxes, rtol=1e-04, - atol=1e-04) - - def test_get_correct_boxes_after_decoding_with_scaling(self): - anchors = np.array([[15., 12., 30., 18.], - [0.1, 0.0, 0.7, 0.9]], np.float32) - rel_codes = np.array([ - [-1., -1.25, -1.62186, -0.911608, - -1.0, -1.5, -1.666667, 0.], - [-0.166667, -0.666667, -2.772588, -5.493062, - 0.333333, -0.5, -0.666667, -0.166667] - ], np.float32) - expected_boxes = [[10., 10., 20., 15.], - [0.2, 0.1, 0.5, 0.4]] - expected_keypoints = [[[15., 12.], [10., 15.]], - [[0.5, 0.3], [0.2, 0.4]]] - num_keypoints = len(expected_keypoints[0]) - def graph_fn(rel_codes, anchors): - scale_factors = [2, 3, 4, 5] - anchors = box_list.BoxList(anchors) - coder = keypoint_box_coder.KeypointBoxCoder( - num_keypoints, scale_factors=scale_factors) - boxes = coder.decode(rel_codes, anchors) - return boxes.get(), boxes.get_field(fields.BoxListFields.keypoints) - boxes_out, keypoints_out = self.execute(graph_fn, [rel_codes, anchors]) - self.assertAllClose(keypoints_out, expected_keypoints, rtol=1e-04, - atol=1e-04) - self.assertAllClose(boxes_out, expected_boxes, rtol=1e-04, - atol=1e-04) - - def test_very_small_width_nan_after_encoding(self): - boxes = np.array([[10., 10., 10.0000001, 20.]], np.float32) - keypoints = np.array([[[10., 10.], [10.0000001, 20.]]], np.float32) - anchors = np.array([[15., 12., 30., 18.]], np.float32) - expected_rel_codes = [[-0.833333, 0., -21.128731, 0.510826, - -0.833333, -0.833333, -0.833333, 0.833333]] - def graph_fn(boxes, keypoints, anchors): - boxes = box_list.BoxList(boxes) - boxes.add_field(fields.BoxListFields.keypoints, keypoints) - anchors = box_list.BoxList(anchors) - coder = keypoint_box_coder.KeypointBoxCoder(2) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, keypoints, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/box_coders/mean_stddev_box_coder.py b/research/object_detection/box_coders/mean_stddev_box_coder.py deleted file mode 100644 index 256f53fd036..00000000000 --- a/research/object_detection/box_coders/mean_stddev_box_coder.py +++ /dev/null @@ -1,79 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Mean stddev box coder. - -This box coder use the following coding schema to encode boxes: -rel_code = (box_corner - anchor_corner_mean) / anchor_corner_stddev. -""" -from object_detection.core import box_coder -from object_detection.core import box_list - - -class MeanStddevBoxCoder(box_coder.BoxCoder): - """Mean stddev box coder.""" - - def __init__(self, stddev=0.01): - """Constructor for MeanStddevBoxCoder. - - Args: - stddev: The standard deviation used to encode and decode boxes. - """ - self._stddev = stddev - - @property - def code_size(self): - return 4 - - def _encode(self, boxes, anchors): - """Encode a box collection with respect to anchor collection. - - Args: - boxes: BoxList holding N boxes to be encoded. - anchors: BoxList of N anchors. - - Returns: - a tensor representing N anchor-encoded boxes - - Raises: - ValueError: if the anchors still have deprecated stddev field. - """ - box_corners = boxes.get() - if anchors.has_field('stddev'): - raise ValueError("'stddev' is a parameter of MeanStddevBoxCoder and " - "should not be specified in the box list.") - means = anchors.get() - return (box_corners - means) / self._stddev - - def _decode(self, rel_codes, anchors): - """Decode. - - Args: - rel_codes: a tensor representing N anchor-encoded boxes. - anchors: BoxList of anchors. - - Returns: - boxes: BoxList holding N bounding boxes - - Raises: - ValueError: if the anchors still have deprecated stddev field and expects - the decode method to use stddev value from that field. - """ - means = anchors.get() - if anchors.has_field('stddev'): - raise ValueError("'stddev' is a parameter of MeanStddevBoxCoder and " - "should not be specified in the box list.") - box_corners = rel_codes * self._stddev + means - return box_list.BoxList(box_corners) diff --git a/research/object_detection/box_coders/mean_stddev_box_coder_test.py b/research/object_detection/box_coders/mean_stddev_box_coder_test.py deleted file mode 100644 index d94fff1187d..00000000000 --- a/research/object_detection/box_coders/mean_stddev_box_coder_test.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.box_coder.mean_stddev_boxcoder.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.box_coders import mean_stddev_box_coder -from object_detection.core import box_list -from object_detection.utils import test_case - - -class MeanStddevBoxCoderTest(test_case.TestCase): - - def testGetCorrectRelativeCodesAfterEncoding(self): - boxes = np.array([[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]], np.float32) - anchors = np.array([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]], np.float32) - expected_rel_codes = [[0.0, 0.0, 0.0, 0.0], [-5.0, -5.0, -5.0, -3.0]] - - def graph_fn(boxes, anchors): - anchors = box_list.BoxList(anchors) - boxes = box_list.BoxList(boxes) - coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - - rel_codes_out = self.execute(graph_fn, [boxes, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - def testGetCorrectBoxesAfterDecoding(self): - rel_codes = np.array([[0.0, 0.0, 0.0, 0.0], [-5.0, -5.0, -5.0, -3.0]], - np.float32) - expected_box_corners = [[0.0, 0.0, 0.5, 0.5], [0.0, 0.0, 0.5, 0.5]] - anchors = np.array([[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 0.8]], np.float32) - - def graph_fn(rel_codes, anchors): - anchors = box_list.BoxList(anchors) - coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - decoded_boxes = coder.decode(rel_codes, anchors).get() - return decoded_boxes - - decoded_boxes_out = self.execute(graph_fn, [rel_codes, anchors]) - self.assertAllClose(decoded_boxes_out, expected_box_corners, rtol=1e-04, - atol=1e-04) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/box_coders/square_box_coder.py b/research/object_detection/box_coders/square_box_coder.py deleted file mode 100644 index 859320fd502..00000000000 --- a/research/object_detection/box_coders/square_box_coder.py +++ /dev/null @@ -1,126 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Square box coder. - -Square box coder follows the coding schema described below: -l = sqrt(h * w) -la = sqrt(ha * wa) -ty = (y - ya) / la -tx = (x - xa) / la -tl = log(l / la) -where x, y, w, h denote the box's center coordinates, width, and height, -respectively. Similarly, xa, ya, wa, ha denote the anchor's center -coordinates, width and height. tx, ty, tl denote the anchor-encoded -center, and length, respectively. Because the encoded box is a square, only -one length is encoded. - -This has shown to provide performance improvements over the Faster RCNN box -coder when the objects being detected tend to be square (e.g. faces) and when -the input images are not distorted via resizing. -""" - -import tensorflow.compat.v1 as tf - -from object_detection.core import box_coder -from object_detection.core import box_list - -EPSILON = 1e-8 - - -class SquareBoxCoder(box_coder.BoxCoder): - """Encodes a 3-scalar representation of a square box.""" - - def __init__(self, scale_factors=None): - """Constructor for SquareBoxCoder. - - Args: - scale_factors: List of 3 positive scalars to scale ty, tx, and tl. - If set to None, does not perform scaling. For faster RCNN, - the open-source implementation recommends using [10.0, 10.0, 5.0]. - - Raises: - ValueError: If scale_factors is not length 3 or contains values less than - or equal to 0. - """ - if scale_factors: - if len(scale_factors) != 3: - raise ValueError('The argument scale_factors must be a list of length ' - '3.') - if any(scalar <= 0 for scalar in scale_factors): - raise ValueError('The values in scale_factors must all be greater ' - 'than 0.') - self._scale_factors = scale_factors - - @property - def code_size(self): - return 3 - - def _encode(self, boxes, anchors): - """Encodes a box collection with respect to an anchor collection. - - Args: - boxes: BoxList holding N boxes to be encoded. - anchors: BoxList of anchors. - - Returns: - a tensor representing N anchor-encoded boxes of the format - [ty, tx, tl]. - """ - # Convert anchors to the center coordinate representation. - ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes() - la = tf.sqrt(ha * wa) - ycenter, xcenter, h, w = boxes.get_center_coordinates_and_sizes() - l = tf.sqrt(h * w) - # Avoid NaN in division and log below. - la += EPSILON - l += EPSILON - - tx = (xcenter - xcenter_a) / la - ty = (ycenter - ycenter_a) / la - tl = tf.log(l / la) - # Scales location targets for joint training. - if self._scale_factors: - ty *= self._scale_factors[0] - tx *= self._scale_factors[1] - tl *= self._scale_factors[2] - return tf.transpose(tf.stack([ty, tx, tl])) - - def _decode(self, rel_codes, anchors): - """Decodes relative codes to boxes. - - Args: - rel_codes: a tensor representing N anchor-encoded boxes. - anchors: BoxList of anchors. - - Returns: - boxes: BoxList holding N bounding boxes. - """ - ycenter_a, xcenter_a, ha, wa = anchors.get_center_coordinates_and_sizes() - la = tf.sqrt(ha * wa) - - ty, tx, tl = tf.unstack(tf.transpose(rel_codes)) - if self._scale_factors: - ty /= self._scale_factors[0] - tx /= self._scale_factors[1] - tl /= self._scale_factors[2] - l = tf.exp(tl) * la - ycenter = ty * la + ycenter_a - xcenter = tx * la + xcenter_a - ymin = ycenter - l / 2. - xmin = xcenter - l / 2. - ymax = ycenter + l / 2. - xmax = xcenter + l / 2. - return box_list.BoxList(tf.transpose(tf.stack([ymin, xmin, ymax, xmax]))) diff --git a/research/object_detection/box_coders/square_box_coder_test.py b/research/object_detection/box_coders/square_box_coder_test.py deleted file mode 100644 index e6bdcb245dc..00000000000 --- a/research/object_detection/box_coders/square_box_coder_test.py +++ /dev/null @@ -1,114 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.box_coder.square_box_coder.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.box_coders import square_box_coder -from object_detection.core import box_list -from object_detection.utils import test_case - - -class SquareBoxCoderTest(test_case.TestCase): - - def test_correct_relative_codes_with_default_scale(self): - boxes = np.array([[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]], - np.float32) - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - expected_rel_codes = [[-0.790569, -0.263523, -0.293893], - [-0.068041, -0.272166, -0.89588]] - def graph_fn(boxes, anchors): - scale_factors = None - boxes = box_list.BoxList(boxes) - anchors = box_list.BoxList(anchors) - coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - def test_correct_relative_codes_with_non_default_scale(self): - boxes = np.array([[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]], - np.float32) - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - expected_rel_codes = [[-1.581139, -0.790569, -1.175573], - [-0.136083, -0.816497, -3.583519]] - def graph_fn(boxes, anchors): - scale_factors = [2, 3, 4] - boxes = box_list.BoxList(boxes) - anchors = box_list.BoxList(anchors) - coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-03, - atol=1e-03) - - def test_correct_relative_codes_with_small_width(self): - boxes = np.array([[10.0, 10.0, 10.0000001, 20.0]], np.float32) - anchors = np.array([[15.0, 12.0, 30.0, 18.0]], np.float32) - expected_rel_codes = [[-1.317616, 0., -20.670586]] - def graph_fn(boxes, anchors): - scale_factors = None - boxes = box_list.BoxList(boxes) - anchors = box_list.BoxList(anchors) - coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) - rel_codes = coder.encode(boxes, anchors) - return rel_codes - rel_codes_out = self.execute(graph_fn, [boxes, anchors]) - self.assertAllClose(rel_codes_out, expected_rel_codes, rtol=1e-04, - atol=1e-04) - - def test_correct_boxes_with_default_scale(self): - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - rel_codes = np.array([[-0.5, -0.416666, -0.405465], - [-0.083333, -0.222222, -0.693147]], np.float32) - expected_boxes = [[14.594306, 7.884875, 20.918861, 14.209432], - [0.155051, 0.102989, 0.522474, 0.470412]] - def graph_fn(rel_codes, anchors): - scale_factors = None - anchors = box_list.BoxList(anchors) - coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) - boxes = coder.decode(rel_codes, anchors).get() - return boxes - boxes_out = self.execute(graph_fn, [rel_codes, anchors]) - self.assertAllClose(boxes_out, expected_boxes, rtol=1e-04, - atol=1e-04) - - def test_correct_boxes_with_non_default_scale(self): - anchors = np.array([[15.0, 12.0, 30.0, 18.0], [0.1, 0.0, 0.7, 0.9]], - np.float32) - rel_codes = np.array( - [[-1., -1.25, -1.62186], [-0.166667, -0.666667, -2.772588]], np.float32) - expected_boxes = [[14.594306, 7.884875, 20.918861, 14.209432], - [0.155051, 0.102989, 0.522474, 0.470412]] - def graph_fn(rel_codes, anchors): - scale_factors = [2, 3, 4] - anchors = box_list.BoxList(anchors) - coder = square_box_coder.SquareBoxCoder(scale_factors=scale_factors) - boxes = coder.decode(rel_codes, anchors).get() - return boxes - boxes_out = self.execute(graph_fn, [rel_codes, anchors]) - self.assertAllClose(boxes_out, expected_boxes, rtol=1e-04, - atol=1e-04) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/__init__.py b/research/object_detection/builders/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/builders/anchor_generator_builder.py b/research/object_detection/builders/anchor_generator_builder.py deleted file mode 100644 index e85f2c2c1b5..00000000000 --- a/research/object_detection/builders/anchor_generator_builder.py +++ /dev/null @@ -1,115 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A function to build an object detection anchor generator from config.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from six.moves import zip -from object_detection.anchor_generators import flexible_grid_anchor_generator -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.anchor_generators import multiple_grid_anchor_generator -from object_detection.anchor_generators import multiscale_grid_anchor_generator -from object_detection.protos import anchor_generator_pb2 - - -def build(anchor_generator_config): - """Builds an anchor generator based on the config. - - Args: - anchor_generator_config: An anchor_generator.proto object containing the - config for the desired anchor generator. - - Returns: - Anchor generator based on the config. - - Raises: - ValueError: On empty anchor generator proto. - """ - if not isinstance(anchor_generator_config, - anchor_generator_pb2.AnchorGenerator): - raise ValueError('anchor_generator_config not of type ' - 'anchor_generator_pb2.AnchorGenerator') - if anchor_generator_config.WhichOneof( - 'anchor_generator_oneof') == 'grid_anchor_generator': - grid_anchor_generator_config = anchor_generator_config.grid_anchor_generator - return grid_anchor_generator.GridAnchorGenerator( - scales=[float(scale) for scale in grid_anchor_generator_config.scales], - aspect_ratios=[float(aspect_ratio) - for aspect_ratio - in grid_anchor_generator_config.aspect_ratios], - base_anchor_size=[grid_anchor_generator_config.height, - grid_anchor_generator_config.width], - anchor_stride=[grid_anchor_generator_config.height_stride, - grid_anchor_generator_config.width_stride], - anchor_offset=[grid_anchor_generator_config.height_offset, - grid_anchor_generator_config.width_offset]) - elif anchor_generator_config.WhichOneof( - 'anchor_generator_oneof') == 'ssd_anchor_generator': - ssd_anchor_generator_config = anchor_generator_config.ssd_anchor_generator - anchor_strides = None - if ssd_anchor_generator_config.height_stride: - anchor_strides = list( - zip(ssd_anchor_generator_config.height_stride, - ssd_anchor_generator_config.width_stride)) - anchor_offsets = None - if ssd_anchor_generator_config.height_offset: - anchor_offsets = list( - zip(ssd_anchor_generator_config.height_offset, - ssd_anchor_generator_config.width_offset)) - return multiple_grid_anchor_generator.create_ssd_anchors( - num_layers=ssd_anchor_generator_config.num_layers, - min_scale=ssd_anchor_generator_config.min_scale, - max_scale=ssd_anchor_generator_config.max_scale, - scales=[float(scale) for scale in ssd_anchor_generator_config.scales], - aspect_ratios=ssd_anchor_generator_config.aspect_ratios, - interpolated_scale_aspect_ratio=( - ssd_anchor_generator_config.interpolated_scale_aspect_ratio), - base_anchor_size=[ - ssd_anchor_generator_config.base_anchor_height, - ssd_anchor_generator_config.base_anchor_width - ], - anchor_strides=anchor_strides, - anchor_offsets=anchor_offsets, - reduce_boxes_in_lowest_layer=( - ssd_anchor_generator_config.reduce_boxes_in_lowest_layer)) - elif anchor_generator_config.WhichOneof( - 'anchor_generator_oneof') == 'multiscale_anchor_generator': - cfg = anchor_generator_config.multiscale_anchor_generator - return multiscale_grid_anchor_generator.MultiscaleGridAnchorGenerator( - cfg.min_level, - cfg.max_level, - cfg.anchor_scale, - [float(aspect_ratio) for aspect_ratio in cfg.aspect_ratios], - cfg.scales_per_octave, - cfg.normalize_coordinates - ) - elif anchor_generator_config.WhichOneof( - 'anchor_generator_oneof') == 'flexible_grid_anchor_generator': - cfg = anchor_generator_config.flexible_grid_anchor_generator - base_sizes = [] - aspect_ratios = [] - strides = [] - offsets = [] - for anchor_grid in cfg.anchor_grid: - base_sizes.append(tuple(anchor_grid.base_sizes)) - aspect_ratios.append(tuple(anchor_grid.aspect_ratios)) - strides.append((anchor_grid.height_stride, anchor_grid.width_stride)) - offsets.append((anchor_grid.height_offset, anchor_grid.width_offset)) - return flexible_grid_anchor_generator.FlexibleGridAnchorGenerator( - base_sizes, aspect_ratios, strides, offsets, cfg.normalize_coordinates) - else: - raise ValueError('Empty anchor generator.') diff --git a/research/object_detection/builders/anchor_generator_builder_test.py b/research/object_detection/builders/anchor_generator_builder_test.py deleted file mode 100644 index 45eae10a691..00000000000 --- a/research/object_detection/builders/anchor_generator_builder_test.py +++ /dev/null @@ -1,338 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for anchor_generator_builder.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math - -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.anchor_generators import flexible_grid_anchor_generator -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.anchor_generators import multiple_grid_anchor_generator -from object_detection.anchor_generators import multiscale_grid_anchor_generator -from object_detection.builders import anchor_generator_builder -from object_detection.protos import anchor_generator_pb2 - - -class AnchorGeneratorBuilderTest(tf.test.TestCase): - - def assert_almost_list_equal(self, expected_list, actual_list, delta=None): - self.assertEqual(len(expected_list), len(actual_list)) - for expected_item, actual_item in zip(expected_list, actual_list): - self.assertAlmostEqual(expected_item, actual_item, delta=delta) - - def test_build_grid_anchor_generator_with_defaults(self): - anchor_generator_text_proto = """ - grid_anchor_generator { - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - grid_anchor_generator.GridAnchorGenerator) - self.assertListEqual(anchor_generator_object._scales, []) - self.assertListEqual(anchor_generator_object._aspect_ratios, []) - self.assertAllEqual(anchor_generator_object._anchor_offset, [0, 0]) - self.assertAllEqual(anchor_generator_object._anchor_stride, [16, 16]) - self.assertAllEqual(anchor_generator_object._base_anchor_size, [256, 256]) - - def test_build_grid_anchor_generator_with_non_default_parameters(self): - anchor_generator_text_proto = """ - grid_anchor_generator { - height: 128 - width: 512 - height_stride: 10 - width_stride: 20 - height_offset: 30 - width_offset: 40 - scales: [0.4, 2.2] - aspect_ratios: [0.3, 4.5] - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - grid_anchor_generator.GridAnchorGenerator) - self.assert_almost_list_equal(anchor_generator_object._scales, - [0.4, 2.2]) - self.assert_almost_list_equal(anchor_generator_object._aspect_ratios, - [0.3, 4.5]) - self.assertAllEqual(anchor_generator_object._anchor_offset, [30, 40]) - self.assertAllEqual(anchor_generator_object._anchor_stride, [10, 20]) - self.assertAllEqual(anchor_generator_object._base_anchor_size, [128, 512]) - - def test_build_ssd_anchor_generator_with_defaults(self): - anchor_generator_text_proto = """ - ssd_anchor_generator { - aspect_ratios: [1.0] - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - multiple_grid_anchor_generator. - MultipleGridAnchorGenerator) - for actual_scales, expected_scales in zip( - list(anchor_generator_object._scales), - [(0.1, 0.2, 0.2), - (0.35, 0.418), - (0.499, 0.570), - (0.649, 0.721), - (0.799, 0.871), - (0.949, 0.974)]): - self.assert_almost_list_equal(expected_scales, actual_scales, delta=1e-2) - for actual_aspect_ratio, expected_aspect_ratio in zip( - list(anchor_generator_object._aspect_ratios), - [(1.0, 2.0, 0.5)] + 5 * [(1.0, 1.0)]): - self.assert_almost_list_equal(expected_aspect_ratio, actual_aspect_ratio) - self.assertAllClose(anchor_generator_object._base_anchor_size, [1.0, 1.0]) - - def test_build_ssd_anchor_generator_with_custom_scales(self): - anchor_generator_text_proto = """ - ssd_anchor_generator { - aspect_ratios: [1.0] - scales: [0.1, 0.15, 0.2, 0.4, 0.6, 0.8] - reduce_boxes_in_lowest_layer: false - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - multiple_grid_anchor_generator. - MultipleGridAnchorGenerator) - for actual_scales, expected_scales in zip( - list(anchor_generator_object._scales), - [(0.1, math.sqrt(0.1 * 0.15)), - (0.15, math.sqrt(0.15 * 0.2)), - (0.2, math.sqrt(0.2 * 0.4)), - (0.4, math.sqrt(0.4 * 0.6)), - (0.6, math.sqrt(0.6 * 0.8)), - (0.8, math.sqrt(0.8 * 1.0))]): - self.assert_almost_list_equal(expected_scales, actual_scales, delta=1e-2) - - def test_build_ssd_anchor_generator_with_custom_interpolated_scale(self): - anchor_generator_text_proto = """ - ssd_anchor_generator { - aspect_ratios: [0.5] - interpolated_scale_aspect_ratio: 0.5 - reduce_boxes_in_lowest_layer: false - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - multiple_grid_anchor_generator. - MultipleGridAnchorGenerator) - for actual_aspect_ratio, expected_aspect_ratio in zip( - list(anchor_generator_object._aspect_ratios), - 6 * [(0.5, 0.5)]): - self.assert_almost_list_equal(expected_aspect_ratio, actual_aspect_ratio) - - def test_build_ssd_anchor_generator_without_reduced_boxes(self): - anchor_generator_text_proto = """ - ssd_anchor_generator { - aspect_ratios: [1.0] - reduce_boxes_in_lowest_layer: false - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - multiple_grid_anchor_generator. - MultipleGridAnchorGenerator) - - for actual_scales, expected_scales in zip( - list(anchor_generator_object._scales), - [(0.2, 0.264), - (0.35, 0.418), - (0.499, 0.570), - (0.649, 0.721), - (0.799, 0.871), - (0.949, 0.974)]): - self.assert_almost_list_equal(expected_scales, actual_scales, delta=1e-2) - - for actual_aspect_ratio, expected_aspect_ratio in zip( - list(anchor_generator_object._aspect_ratios), - 6 * [(1.0, 1.0)]): - self.assert_almost_list_equal(expected_aspect_ratio, actual_aspect_ratio) - - self.assertAllClose(anchor_generator_object._base_anchor_size, [1.0, 1.0]) - - def test_build_ssd_anchor_generator_with_non_default_parameters(self): - anchor_generator_text_proto = """ - ssd_anchor_generator { - num_layers: 2 - min_scale: 0.3 - max_scale: 0.8 - aspect_ratios: [2.0] - height_stride: 16 - height_stride: 32 - width_stride: 20 - width_stride: 30 - height_offset: 8 - height_offset: 16 - width_offset: 0 - width_offset: 10 - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - multiple_grid_anchor_generator. - MultipleGridAnchorGenerator) - - for actual_scales, expected_scales in zip( - list(anchor_generator_object._scales), - [(0.1, 0.3, 0.3), (0.8, 0.894)]): - self.assert_almost_list_equal(expected_scales, actual_scales, delta=1e-2) - - for actual_aspect_ratio, expected_aspect_ratio in zip( - list(anchor_generator_object._aspect_ratios), - [(1.0, 2.0, 0.5), (2.0, 1.0)]): - self.assert_almost_list_equal(expected_aspect_ratio, actual_aspect_ratio) - - for actual_strides, expected_strides in zip( - list(anchor_generator_object._anchor_strides), [(16, 20), (32, 30)]): - self.assert_almost_list_equal(expected_strides, actual_strides) - - for actual_offsets, expected_offsets in zip( - list(anchor_generator_object._anchor_offsets), [(8, 0), (16, 10)]): - self.assert_almost_list_equal(expected_offsets, actual_offsets) - - self.assertAllClose(anchor_generator_object._base_anchor_size, [1.0, 1.0]) - - def test_raise_value_error_on_empty_anchor_genertor(self): - anchor_generator_text_proto = """ - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - with self.assertRaises(ValueError): - anchor_generator_builder.build(anchor_generator_proto) - - def test_build_multiscale_anchor_generator_custom_aspect_ratios(self): - anchor_generator_text_proto = """ - multiscale_anchor_generator { - aspect_ratios: [1.0] - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - multiscale_grid_anchor_generator. - MultiscaleGridAnchorGenerator) - for level, anchor_grid_info in zip( - range(3, 8), anchor_generator_object._anchor_grid_info): - self.assertEqual(set(anchor_grid_info.keys()), set(['level', 'info'])) - self.assertTrue(level, anchor_grid_info['level']) - self.assertEqual(len(anchor_grid_info['info']), 4) - self.assertAllClose(anchor_grid_info['info'][0], [2**0, 2**0.5]) - self.assertTrue(anchor_grid_info['info'][1], 1.0) - self.assertAllClose(anchor_grid_info['info'][2], - [4.0 * 2**level, 4.0 * 2**level]) - self.assertAllClose(anchor_grid_info['info'][3], [2**level, 2**level]) - self.assertTrue(anchor_generator_object._normalize_coordinates) - - def test_build_multiscale_anchor_generator_with_anchors_in_pixel_coordinates( - self): - anchor_generator_text_proto = """ - multiscale_anchor_generator { - aspect_ratios: [1.0] - normalize_coordinates: false - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - multiscale_grid_anchor_generator. - MultiscaleGridAnchorGenerator) - self.assertFalse(anchor_generator_object._normalize_coordinates) - - def test_build_flexible_anchor_generator(self): - anchor_generator_text_proto = """ - flexible_grid_anchor_generator { - anchor_grid { - base_sizes: [1.5] - aspect_ratios: [1.0] - height_stride: 16 - width_stride: 20 - height_offset: 8 - width_offset: 9 - } - anchor_grid { - base_sizes: [1.0, 2.0] - aspect_ratios: [1.0, 0.5] - height_stride: 32 - width_stride: 30 - height_offset: 10 - width_offset: 11 - } - } - """ - anchor_generator_proto = anchor_generator_pb2.AnchorGenerator() - text_format.Merge(anchor_generator_text_proto, anchor_generator_proto) - anchor_generator_object = anchor_generator_builder.build( - anchor_generator_proto) - self.assertIsInstance(anchor_generator_object, - flexible_grid_anchor_generator. - FlexibleGridAnchorGenerator) - - for actual_base_sizes, expected_base_sizes in zip( - list(anchor_generator_object._base_sizes), [(1.5,), (1.0, 2.0)]): - self.assert_almost_list_equal(expected_base_sizes, actual_base_sizes) - - for actual_aspect_ratios, expected_aspect_ratios in zip( - list(anchor_generator_object._aspect_ratios), [(1.0,), (1.0, 0.5)]): - self.assert_almost_list_equal(expected_aspect_ratios, - actual_aspect_ratios) - - for actual_strides, expected_strides in zip( - list(anchor_generator_object._anchor_strides), [(16, 20), (32, 30)]): - self.assert_almost_list_equal(expected_strides, actual_strides) - - for actual_offsets, expected_offsets in zip( - list(anchor_generator_object._anchor_offsets), [(8, 9), (10, 11)]): - self.assert_almost_list_equal(expected_offsets, actual_offsets) - - self.assertTrue(anchor_generator_object._normalize_coordinates) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/box_coder_builder.py b/research/object_detection/builders/box_coder_builder.py deleted file mode 100644 index cc13d5a2f01..00000000000 --- a/research/object_detection/builders/box_coder_builder.py +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A function to build an object detection box coder from configuration.""" -from object_detection.box_coders import faster_rcnn_box_coder -from object_detection.box_coders import keypoint_box_coder -from object_detection.box_coders import mean_stddev_box_coder -from object_detection.box_coders import square_box_coder -from object_detection.protos import box_coder_pb2 - - -def build(box_coder_config): - """Builds a box coder object based on the box coder config. - - Args: - box_coder_config: A box_coder.proto object containing the config for the - desired box coder. - - Returns: - BoxCoder based on the config. - - Raises: - ValueError: On empty box coder proto. - """ - if not isinstance(box_coder_config, box_coder_pb2.BoxCoder): - raise ValueError('box_coder_config not of type box_coder_pb2.BoxCoder.') - - if box_coder_config.WhichOneof('box_coder_oneof') == 'faster_rcnn_box_coder': - return faster_rcnn_box_coder.FasterRcnnBoxCoder(scale_factors=[ - box_coder_config.faster_rcnn_box_coder.y_scale, - box_coder_config.faster_rcnn_box_coder.x_scale, - box_coder_config.faster_rcnn_box_coder.height_scale, - box_coder_config.faster_rcnn_box_coder.width_scale - ]) - if box_coder_config.WhichOneof('box_coder_oneof') == 'keypoint_box_coder': - return keypoint_box_coder.KeypointBoxCoder( - box_coder_config.keypoint_box_coder.num_keypoints, - scale_factors=[ - box_coder_config.keypoint_box_coder.y_scale, - box_coder_config.keypoint_box_coder.x_scale, - box_coder_config.keypoint_box_coder.height_scale, - box_coder_config.keypoint_box_coder.width_scale - ]) - if (box_coder_config.WhichOneof('box_coder_oneof') == - 'mean_stddev_box_coder'): - return mean_stddev_box_coder.MeanStddevBoxCoder( - stddev=box_coder_config.mean_stddev_box_coder.stddev) - if box_coder_config.WhichOneof('box_coder_oneof') == 'square_box_coder': - return square_box_coder.SquareBoxCoder(scale_factors=[ - box_coder_config.square_box_coder.y_scale, - box_coder_config.square_box_coder.x_scale, - box_coder_config.square_box_coder.length_scale - ]) - raise ValueError('Empty box coder.') diff --git a/research/object_detection/builders/box_coder_builder_test.py b/research/object_detection/builders/box_coder_builder_test.py deleted file mode 100644 index 5db9947cb64..00000000000 --- a/research/object_detection/builders/box_coder_builder_test.py +++ /dev/null @@ -1,136 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for box_coder_builder.""" - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.box_coders import faster_rcnn_box_coder -from object_detection.box_coders import keypoint_box_coder -from object_detection.box_coders import mean_stddev_box_coder -from object_detection.box_coders import square_box_coder -from object_detection.builders import box_coder_builder -from object_detection.protos import box_coder_pb2 - - -class BoxCoderBuilderTest(tf.test.TestCase): - - def test_build_faster_rcnn_box_coder_with_defaults(self): - box_coder_text_proto = """ - faster_rcnn_box_coder { - } - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - box_coder_object = box_coder_builder.build(box_coder_proto) - self.assertIsInstance(box_coder_object, - faster_rcnn_box_coder.FasterRcnnBoxCoder) - self.assertEqual(box_coder_object._scale_factors, [10.0, 10.0, 5.0, 5.0]) - - def test_build_faster_rcnn_box_coder_with_non_default_parameters(self): - box_coder_text_proto = """ - faster_rcnn_box_coder { - y_scale: 6.0 - x_scale: 3.0 - height_scale: 7.0 - width_scale: 8.0 - } - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - box_coder_object = box_coder_builder.build(box_coder_proto) - self.assertIsInstance(box_coder_object, - faster_rcnn_box_coder.FasterRcnnBoxCoder) - self.assertEqual(box_coder_object._scale_factors, [6.0, 3.0, 7.0, 8.0]) - - def test_build_keypoint_box_coder_with_defaults(self): - box_coder_text_proto = """ - keypoint_box_coder { - } - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - box_coder_object = box_coder_builder.build(box_coder_proto) - self.assertIsInstance(box_coder_object, keypoint_box_coder.KeypointBoxCoder) - self.assertEqual(box_coder_object._scale_factors, [10.0, 10.0, 5.0, 5.0]) - - def test_build_keypoint_box_coder_with_non_default_parameters(self): - box_coder_text_proto = """ - keypoint_box_coder { - num_keypoints: 6 - y_scale: 6.0 - x_scale: 3.0 - height_scale: 7.0 - width_scale: 8.0 - } - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - box_coder_object = box_coder_builder.build(box_coder_proto) - self.assertIsInstance(box_coder_object, keypoint_box_coder.KeypointBoxCoder) - self.assertEqual(box_coder_object._num_keypoints, 6) - self.assertEqual(box_coder_object._scale_factors, [6.0, 3.0, 7.0, 8.0]) - - def test_build_mean_stddev_box_coder(self): - box_coder_text_proto = """ - mean_stddev_box_coder { - } - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - box_coder_object = box_coder_builder.build(box_coder_proto) - self.assertTrue( - isinstance(box_coder_object, - mean_stddev_box_coder.MeanStddevBoxCoder)) - - def test_build_square_box_coder_with_defaults(self): - box_coder_text_proto = """ - square_box_coder { - } - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - box_coder_object = box_coder_builder.build(box_coder_proto) - self.assertTrue( - isinstance(box_coder_object, square_box_coder.SquareBoxCoder)) - self.assertEqual(box_coder_object._scale_factors, [10.0, 10.0, 5.0]) - - def test_build_square_box_coder_with_non_default_parameters(self): - box_coder_text_proto = """ - square_box_coder { - y_scale: 6.0 - x_scale: 3.0 - length_scale: 7.0 - } - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - box_coder_object = box_coder_builder.build(box_coder_proto) - self.assertTrue( - isinstance(box_coder_object, square_box_coder.SquareBoxCoder)) - self.assertEqual(box_coder_object._scale_factors, [6.0, 3.0, 7.0]) - - def test_raise_error_on_empty_box_coder(self): - box_coder_text_proto = """ - """ - box_coder_proto = box_coder_pb2.BoxCoder() - text_format.Merge(box_coder_text_proto, box_coder_proto) - with self.assertRaises(ValueError): - box_coder_builder.build(box_coder_proto) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/box_predictor_builder.py b/research/object_detection/builders/box_predictor_builder.py deleted file mode 100644 index d0f994c8eca..00000000000 --- a/research/object_detection/builders/box_predictor_builder.py +++ /dev/null @@ -1,992 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Function to build box predictor from configuration.""" - -import collections -import tensorflow.compat.v1 as tf -from object_detection.predictors import convolutional_box_predictor -from object_detection.predictors import convolutional_keras_box_predictor -from object_detection.predictors import mask_rcnn_box_predictor -from object_detection.predictors import mask_rcnn_keras_box_predictor -from object_detection.predictors import rfcn_box_predictor -from object_detection.predictors import rfcn_keras_box_predictor -from object_detection.predictors.heads import box_head -from object_detection.predictors.heads import class_head -from object_detection.predictors.heads import keras_box_head -from object_detection.predictors.heads import keras_class_head -from object_detection.predictors.heads import keras_mask_head -from object_detection.predictors.heads import mask_head -from object_detection.protos import box_predictor_pb2 - - -def build_convolutional_box_predictor(is_training, - num_classes, - conv_hyperparams_fn, - min_depth, - max_depth, - num_layers_before_predictor, - use_dropout, - dropout_keep_prob, - kernel_size, - box_code_size, - apply_sigmoid_to_scores=False, - add_background_class=True, - class_prediction_bias_init=0.0, - use_depthwise=False, - box_encodings_clip_range=None): - """Builds the ConvolutionalBoxPredictor from the arguments. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - conv_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for convolution ops. - min_depth: Minimum feature depth prior to predicting box encodings - and class predictions. - max_depth: Maximum feature depth prior to predicting box encodings - and class predictions. If max_depth is set to 0, no additional - feature map will be inserted before location and class predictions. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - box_code_size: Size of encoding for each box. - apply_sigmoid_to_scores: If True, apply the sigmoid on the output - class_predictions. - add_background_class: Whether to add an implicit background class. - class_prediction_bias_init: Constant value to initialize bias of the last - conv2d layer before class prediction. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - box_encodings_clip_range: Min and max values for clipping the box_encodings. - - Returns: - A ConvolutionalBoxPredictor class. - """ - box_prediction_head = box_head.ConvolutionalBoxHead( - is_training=is_training, - box_code_size=box_code_size, - kernel_size=kernel_size, - use_depthwise=use_depthwise, - box_encodings_clip_range=box_encodings_clip_range) - class_prediction_head = class_head.ConvolutionalClassHead( - is_training=is_training, - num_class_slots=num_classes + 1 if add_background_class else num_classes, - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob, - kernel_size=kernel_size, - apply_sigmoid_to_scores=apply_sigmoid_to_scores, - class_prediction_bias_init=class_prediction_bias_init, - use_depthwise=use_depthwise) - other_heads = {} - return convolutional_box_predictor.ConvolutionalBoxPredictor( - is_training=is_training, - num_classes=num_classes, - box_prediction_head=box_prediction_head, - class_prediction_head=class_prediction_head, - other_heads=other_heads, - conv_hyperparams_fn=conv_hyperparams_fn, - num_layers_before_predictor=num_layers_before_predictor, - min_depth=min_depth, - max_depth=max_depth) - - -def build_convolutional_keras_box_predictor(is_training, - num_classes, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - num_predictions_per_location_list, - min_depth, - max_depth, - num_layers_before_predictor, - use_dropout, - dropout_keep_prob, - kernel_size, - box_code_size, - add_background_class=True, - class_prediction_bias_init=0.0, - use_depthwise=False, - box_encodings_clip_range=None, - name='BoxPredictor'): - """Builds the Keras ConvolutionalBoxPredictor from the arguments. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - num_predictions_per_location_list: A list of integers representing the - number of box predictions to be made per spatial location for each - feature map. - min_depth: Minimum feature depth prior to predicting box encodings - and class predictions. - max_depth: Maximum feature depth prior to predicting box encodings - and class predictions. If max_depth is set to 0, no additional - feature map will be inserted before location and class predictions. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - box_code_size: Size of encoding for each box. - add_background_class: Whether to add an implicit background class. - class_prediction_bias_init: constant value to initialize bias of the last - conv2d layer before class prediction. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - box_encodings_clip_range: Min and max values for clipping the box_encodings. - name: A string name scope to assign to the box predictor. If `None`, Keras - will auto-generate one from the class name. - - Returns: - A Keras ConvolutionalBoxPredictor class. - """ - box_prediction_heads = [] - class_prediction_heads = [] - other_heads = {} - - for stack_index, num_predictions_per_location in enumerate( - num_predictions_per_location_list): - box_prediction_heads.append( - keras_box_head.ConvolutionalBoxHead( - is_training=is_training, - box_code_size=box_code_size, - kernel_size=kernel_size, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - num_predictions_per_location=num_predictions_per_location, - use_depthwise=use_depthwise, - box_encodings_clip_range=box_encodings_clip_range, - name='ConvolutionalBoxHead_%d' % stack_index)) - class_prediction_heads.append( - keras_class_head.ConvolutionalClassHead( - is_training=is_training, - num_class_slots=( - num_classes + 1 if add_background_class else num_classes), - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob, - kernel_size=kernel_size, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - num_predictions_per_location=num_predictions_per_location, - class_prediction_bias_init=class_prediction_bias_init, - use_depthwise=use_depthwise, - name='ConvolutionalClassHead_%d' % stack_index)) - - return convolutional_keras_box_predictor.ConvolutionalBoxPredictor( - is_training=is_training, - num_classes=num_classes, - box_prediction_heads=box_prediction_heads, - class_prediction_heads=class_prediction_heads, - other_heads=other_heads, - conv_hyperparams=conv_hyperparams, - num_layers_before_predictor=num_layers_before_predictor, - min_depth=min_depth, - max_depth=max_depth, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - name=name) - - -def build_weight_shared_convolutional_box_predictor( - is_training, - num_classes, - conv_hyperparams_fn, - depth, - num_layers_before_predictor, - box_code_size, - kernel_size=3, - add_background_class=True, - class_prediction_bias_init=0.0, - use_dropout=False, - dropout_keep_prob=0.8, - share_prediction_tower=False, - apply_batch_norm=True, - use_depthwise=False, - score_converter_fn=tf.identity, - box_encodings_clip_range=None, - keyword_args=None): - """Builds and returns a WeightSharedConvolutionalBoxPredictor class. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - conv_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for convolution ops. - depth: depth of conv layers. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - box_code_size: Size of encoding for each box. - kernel_size: Size of final convolution kernel. - add_background_class: Whether to add an implicit background class. - class_prediction_bias_init: constant value to initialize bias of the last - conv2d layer before class prediction. - use_dropout: Whether to apply dropout to class prediction head. - dropout_keep_prob: Probability of keeping activiations. - share_prediction_tower: Whether to share the multi-layer tower between box - prediction and class prediction heads. - apply_batch_norm: Whether to apply batch normalization to conv layers in - this predictor. - use_depthwise: Whether to use depthwise separable conv2d instead of conv2d. - score_converter_fn: Callable score converter to perform elementwise op on - class scores. - box_encodings_clip_range: Min and max values for clipping the box_encodings. - keyword_args: A dictionary with additional args. - - Returns: - A WeightSharedConvolutionalBoxPredictor class. - """ - box_prediction_head = box_head.WeightSharedConvolutionalBoxHead( - box_code_size=box_code_size, - kernel_size=kernel_size, - use_depthwise=use_depthwise, - box_encodings_clip_range=box_encodings_clip_range) - class_prediction_head = ( - class_head.WeightSharedConvolutionalClassHead( - num_class_slots=( - num_classes + 1 if add_background_class else num_classes), - kernel_size=kernel_size, - class_prediction_bias_init=class_prediction_bias_init, - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob, - use_depthwise=use_depthwise, - score_converter_fn=score_converter_fn)) - other_heads = {} - return convolutional_box_predictor.WeightSharedConvolutionalBoxPredictor( - is_training=is_training, - num_classes=num_classes, - box_prediction_head=box_prediction_head, - class_prediction_head=class_prediction_head, - other_heads=other_heads, - conv_hyperparams_fn=conv_hyperparams_fn, - depth=depth, - num_layers_before_predictor=num_layers_before_predictor, - kernel_size=kernel_size, - apply_batch_norm=apply_batch_norm, - share_prediction_tower=share_prediction_tower, - use_depthwise=use_depthwise) - - -def build_weight_shared_convolutional_keras_box_predictor( - is_training, - num_classes, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - num_predictions_per_location_list, - depth, - num_layers_before_predictor, - box_code_size, - kernel_size=3, - add_background_class=True, - class_prediction_bias_init=0.0, - use_dropout=False, - dropout_keep_prob=0.8, - share_prediction_tower=False, - apply_batch_norm=True, - use_depthwise=False, - apply_conv_hyperparams_to_heads=False, - apply_conv_hyperparams_pointwise=False, - score_converter_fn=tf.identity, - box_encodings_clip_range=None, - name='WeightSharedConvolutionalBoxPredictor', - keyword_args=None): - """Builds the Keras WeightSharedConvolutionalBoxPredictor from the arguments. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - num_predictions_per_location_list: A list of integers representing the - number of box predictions to be made per spatial location for each - feature map. - depth: depth of conv layers. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - box_code_size: Size of encoding for each box. - kernel_size: Size of final convolution kernel. - add_background_class: Whether to add an implicit background class. - class_prediction_bias_init: constant value to initialize bias of the last - conv2d layer before class prediction. - use_dropout: Whether to apply dropout to class prediction head. - dropout_keep_prob: Probability of keeping activiations. - share_prediction_tower: Whether to share the multi-layer tower between box - prediction and class prediction heads. - apply_batch_norm: Whether to apply batch normalization to conv layers in - this predictor. - use_depthwise: Whether to use depthwise separable conv2d instead of conv2d. - apply_conv_hyperparams_to_heads: Whether to apply conv_hyperparams to - depthwise seperable convolution layers in the box and class heads. By - default, the conv_hyperparams are only applied to layers in the predictor - tower when using depthwise separable convolutions. - apply_conv_hyperparams_pointwise: Whether to apply the conv_hyperparams to - the pointwise_initializer and pointwise_regularizer when using depthwise - separable convolutions. By default, conv_hyperparams are only applied to - the depthwise initializer and regularizer when use_depthwise is true. - score_converter_fn: Callable score converter to perform elementwise op on - class scores. - box_encodings_clip_range: Min and max values for clipping the box_encodings. - name: A string name scope to assign to the box predictor. If `None`, Keras - will auto-generate one from the class name. - keyword_args: A dictionary with additional args. - - Returns: - A Keras WeightSharedConvolutionalBoxPredictor class. - """ - if len(set(num_predictions_per_location_list)) > 1: - raise ValueError('num predictions per location must be same for all' - 'feature maps, found: {}'.format( - num_predictions_per_location_list)) - num_predictions_per_location = num_predictions_per_location_list[0] - - box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( - box_code_size=box_code_size, - kernel_size=kernel_size, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=num_predictions_per_location, - use_depthwise=use_depthwise, - apply_conv_hyperparams_to_heads=apply_conv_hyperparams_to_heads, - box_encodings_clip_range=box_encodings_clip_range, - name='WeightSharedConvolutionalBoxHead') - class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead( - num_class_slots=( - num_classes + 1 if add_background_class else num_classes), - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob, - kernel_size=kernel_size, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=num_predictions_per_location, - class_prediction_bias_init=class_prediction_bias_init, - use_depthwise=use_depthwise, - apply_conv_hyperparams_to_heads=apply_conv_hyperparams_to_heads, - score_converter_fn=score_converter_fn, - name='WeightSharedConvolutionalClassHead') - other_heads = {} - - return ( - convolutional_keras_box_predictor.WeightSharedConvolutionalBoxPredictor( - is_training=is_training, - num_classes=num_classes, - box_prediction_head=box_prediction_head, - class_prediction_head=class_prediction_head, - other_heads=other_heads, - conv_hyperparams=conv_hyperparams, - depth=depth, - num_layers_before_predictor=num_layers_before_predictor, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - kernel_size=kernel_size, - apply_batch_norm=apply_batch_norm, - share_prediction_tower=share_prediction_tower, - use_depthwise=use_depthwise, - apply_conv_hyperparams_pointwise=apply_conv_hyperparams_pointwise, - name=name)) - - - - -def build_mask_rcnn_keras_box_predictor(is_training, - num_classes, - fc_hyperparams, - freeze_batchnorm, - use_dropout, - dropout_keep_prob, - box_code_size, - add_background_class=True, - share_box_across_classes=False, - predict_instance_masks=False, - conv_hyperparams=None, - mask_height=14, - mask_width=14, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=False, - convolve_then_upsample_masks=False): - """Builds and returns a MaskRCNNKerasBoxPredictor class. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - fc_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for fully connected dense ops. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - box_code_size: Size of encoding for each box. - add_background_class: Whether to add an implicit background class. - share_box_across_classes: Whether to share boxes across classes rather - than use a different box for each class. - predict_instance_masks: If True, will add a third stage mask prediction - to the returned class. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - mask_height: Desired output mask height. The default value is 14. - mask_width: Desired output mask width. The default value is 14. - mask_prediction_num_conv_layers: Number of convolution layers applied to - the image_features in mask prediction branch. - mask_prediction_conv_depth: The depth for the first conv2d_transpose op - applied to the image_features in the mask prediction branch. If set - to 0, the depth of the convolution layers will be automatically chosen - based on the number of object classes and the number of channels in the - image features. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - convolve_then_upsample_masks: Whether to apply convolutions on mask - features before upsampling using nearest neighbor resizing. Otherwise, - mask features are resized to [`mask_height`, `mask_width`] using - bilinear resizing before applying convolutions. - - Returns: - A MaskRCNNKerasBoxPredictor class. - """ - box_prediction_head = keras_box_head.MaskRCNNBoxHead( - is_training=is_training, - num_classes=num_classes, - fc_hyperparams=fc_hyperparams, - freeze_batchnorm=freeze_batchnorm, - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob, - box_code_size=box_code_size, - share_box_across_classes=share_box_across_classes) - class_prediction_head = keras_class_head.MaskRCNNClassHead( - is_training=is_training, - num_class_slots=num_classes + 1 if add_background_class else num_classes, - fc_hyperparams=fc_hyperparams, - freeze_batchnorm=freeze_batchnorm, - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob) - third_stage_heads = {} - if predict_instance_masks: - third_stage_heads[ - mask_rcnn_box_predictor. - MASK_PREDICTIONS] = keras_mask_head.MaskRCNNMaskHead( - is_training=is_training, - num_classes=num_classes, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - mask_height=mask_height, - mask_width=mask_width, - mask_prediction_num_conv_layers=mask_prediction_num_conv_layers, - mask_prediction_conv_depth=mask_prediction_conv_depth, - masks_are_class_agnostic=masks_are_class_agnostic, - convolve_then_upsample=convolve_then_upsample_masks) - return mask_rcnn_keras_box_predictor.MaskRCNNKerasBoxPredictor( - is_training=is_training, - num_classes=num_classes, - freeze_batchnorm=freeze_batchnorm, - box_prediction_head=box_prediction_head, - class_prediction_head=class_prediction_head, - third_stage_heads=third_stage_heads) - - -def build_mask_rcnn_box_predictor(is_training, - num_classes, - fc_hyperparams_fn, - use_dropout, - dropout_keep_prob, - box_code_size, - add_background_class=True, - share_box_across_classes=False, - predict_instance_masks=False, - conv_hyperparams_fn=None, - mask_height=14, - mask_width=14, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=False, - convolve_then_upsample_masks=False): - """Builds and returns a MaskRCNNBoxPredictor class. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - fc_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for fully connected ops. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - box_code_size: Size of encoding for each box. - add_background_class: Whether to add an implicit background class. - share_box_across_classes: Whether to share boxes across classes rather - than use a different box for each class. - predict_instance_masks: If True, will add a third stage mask prediction - to the returned class. - conv_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for convolution ops. - mask_height: Desired output mask height. The default value is 14. - mask_width: Desired output mask width. The default value is 14. - mask_prediction_num_conv_layers: Number of convolution layers applied to - the image_features in mask prediction branch. - mask_prediction_conv_depth: The depth for the first conv2d_transpose op - applied to the image_features in the mask prediction branch. If set - to 0, the depth of the convolution layers will be automatically chosen - based on the number of object classes and the number of channels in the - image features. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - convolve_then_upsample_masks: Whether to apply convolutions on mask - features before upsampling using nearest neighbor resizing. Otherwise, - mask features are resized to [`mask_height`, `mask_width`] using - bilinear resizing before applying convolutions. - - Returns: - A MaskRCNNBoxPredictor class. - """ - box_prediction_head = box_head.MaskRCNNBoxHead( - is_training=is_training, - num_classes=num_classes, - fc_hyperparams_fn=fc_hyperparams_fn, - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob, - box_code_size=box_code_size, - share_box_across_classes=share_box_across_classes) - class_prediction_head = class_head.MaskRCNNClassHead( - is_training=is_training, - num_class_slots=num_classes + 1 if add_background_class else num_classes, - fc_hyperparams_fn=fc_hyperparams_fn, - use_dropout=use_dropout, - dropout_keep_prob=dropout_keep_prob) - third_stage_heads = {} - if predict_instance_masks: - third_stage_heads[ - mask_rcnn_box_predictor. - MASK_PREDICTIONS] = mask_head.MaskRCNNMaskHead( - num_classes=num_classes, - conv_hyperparams_fn=conv_hyperparams_fn, - mask_height=mask_height, - mask_width=mask_width, - mask_prediction_num_conv_layers=mask_prediction_num_conv_layers, - mask_prediction_conv_depth=mask_prediction_conv_depth, - masks_are_class_agnostic=masks_are_class_agnostic, - convolve_then_upsample=convolve_then_upsample_masks) - return mask_rcnn_box_predictor.MaskRCNNBoxPredictor( - is_training=is_training, - num_classes=num_classes, - box_prediction_head=box_prediction_head, - class_prediction_head=class_prediction_head, - third_stage_heads=third_stage_heads) - - -def build_score_converter(score_converter_config, is_training): - """Builds score converter based on the config. - - Builds one of [tf.identity, tf.sigmoid] score converters based on the config - and whether the BoxPredictor is for training or inference. - - Args: - score_converter_config: - box_predictor_pb2.WeightSharedConvolutionalBoxPredictor.score_converter. - is_training: Indicates whether the BoxPredictor is in training mode. - - Returns: - Callable score converter op. - - Raises: - ValueError: On unknown score converter. - """ - if score_converter_config == ( - box_predictor_pb2.WeightSharedConvolutionalBoxPredictor.IDENTITY): - return tf.identity - if score_converter_config == ( - box_predictor_pb2.WeightSharedConvolutionalBoxPredictor.SIGMOID): - return tf.identity if is_training else tf.sigmoid - raise ValueError('Unknown score converter.') - - -BoxEncodingsClipRange = collections.namedtuple('BoxEncodingsClipRange', - ['min', 'max']) - - -def build(argscope_fn, box_predictor_config, is_training, num_classes, - add_background_class=True): - """Builds box predictor based on the configuration. - - Builds box predictor based on the configuration. See box_predictor.proto for - configurable options. Also, see box_predictor.py for more details. - - Args: - argscope_fn: A function that takes the following inputs: - * hyperparams_pb2.Hyperparams proto - * a boolean indicating if the model is in training mode. - and returns a tf slim argscope for Conv and FC hyperparameters. - box_predictor_config: box_predictor_pb2.BoxPredictor proto containing - configuration. - is_training: Whether the models is in training mode. - num_classes: Number of classes to predict. - add_background_class: Whether to add an implicit background class. - - Returns: - box_predictor: box_predictor.BoxPredictor object. - - Raises: - ValueError: On unknown box predictor. - """ - if not isinstance(box_predictor_config, box_predictor_pb2.BoxPredictor): - raise ValueError('box_predictor_config not of type ' - 'box_predictor_pb2.BoxPredictor.') - - box_predictor_oneof = box_predictor_config.WhichOneof('box_predictor_oneof') - - if box_predictor_oneof == 'convolutional_box_predictor': - config_box_predictor = box_predictor_config.convolutional_box_predictor - conv_hyperparams_fn = argscope_fn(config_box_predictor.conv_hyperparams, - is_training) - # Optionally apply clipping to box encodings, when box_encodings_clip_range - # is set. - box_encodings_clip_range = None - if config_box_predictor.HasField('box_encodings_clip_range'): - box_encodings_clip_range = BoxEncodingsClipRange( - min=config_box_predictor.box_encodings_clip_range.min, - max=config_box_predictor.box_encodings_clip_range.max) - return build_convolutional_box_predictor( - is_training=is_training, - num_classes=num_classes, - add_background_class=add_background_class, - conv_hyperparams_fn=conv_hyperparams_fn, - use_dropout=config_box_predictor.use_dropout, - dropout_keep_prob=config_box_predictor.dropout_keep_probability, - box_code_size=config_box_predictor.box_code_size, - kernel_size=config_box_predictor.kernel_size, - num_layers_before_predictor=( - config_box_predictor.num_layers_before_predictor), - min_depth=config_box_predictor.min_depth, - max_depth=config_box_predictor.max_depth, - apply_sigmoid_to_scores=config_box_predictor.apply_sigmoid_to_scores, - class_prediction_bias_init=( - config_box_predictor.class_prediction_bias_init), - use_depthwise=config_box_predictor.use_depthwise, - box_encodings_clip_range=box_encodings_clip_range) - - if box_predictor_oneof == 'weight_shared_convolutional_box_predictor': - config_box_predictor = ( - box_predictor_config.weight_shared_convolutional_box_predictor) - conv_hyperparams_fn = argscope_fn(config_box_predictor.conv_hyperparams, - is_training) - apply_batch_norm = config_box_predictor.conv_hyperparams.HasField( - 'batch_norm') - # During training phase, logits are used to compute the loss. Only apply - # sigmoid at inference to make the inference graph TPU friendly. - score_converter_fn = build_score_converter( - config_box_predictor.score_converter, is_training) - # Optionally apply clipping to box encodings, when box_encodings_clip_range - # is set. - box_encodings_clip_range = None - if config_box_predictor.HasField('box_encodings_clip_range'): - box_encodings_clip_range = BoxEncodingsClipRange( - min=config_box_predictor.box_encodings_clip_range.min, - max=config_box_predictor.box_encodings_clip_range.max) - keyword_args = None - - return build_weight_shared_convolutional_box_predictor( - is_training=is_training, - num_classes=num_classes, - add_background_class=add_background_class, - conv_hyperparams_fn=conv_hyperparams_fn, - depth=config_box_predictor.depth, - num_layers_before_predictor=( - config_box_predictor.num_layers_before_predictor), - box_code_size=config_box_predictor.box_code_size, - kernel_size=config_box_predictor.kernel_size, - class_prediction_bias_init=( - config_box_predictor.class_prediction_bias_init), - use_dropout=config_box_predictor.use_dropout, - dropout_keep_prob=config_box_predictor.dropout_keep_probability, - share_prediction_tower=config_box_predictor.share_prediction_tower, - apply_batch_norm=apply_batch_norm, - use_depthwise=config_box_predictor.use_depthwise, - score_converter_fn=score_converter_fn, - box_encodings_clip_range=box_encodings_clip_range, - keyword_args=keyword_args) - - - if box_predictor_oneof == 'mask_rcnn_box_predictor': - config_box_predictor = box_predictor_config.mask_rcnn_box_predictor - fc_hyperparams_fn = argscope_fn(config_box_predictor.fc_hyperparams, - is_training) - conv_hyperparams_fn = None - if config_box_predictor.HasField('conv_hyperparams'): - conv_hyperparams_fn = argscope_fn( - config_box_predictor.conv_hyperparams, is_training) - return build_mask_rcnn_box_predictor( - is_training=is_training, - num_classes=num_classes, - add_background_class=add_background_class, - fc_hyperparams_fn=fc_hyperparams_fn, - use_dropout=config_box_predictor.use_dropout, - dropout_keep_prob=config_box_predictor.dropout_keep_probability, - box_code_size=config_box_predictor.box_code_size, - share_box_across_classes=( - config_box_predictor.share_box_across_classes), - predict_instance_masks=config_box_predictor.predict_instance_masks, - conv_hyperparams_fn=conv_hyperparams_fn, - mask_height=config_box_predictor.mask_height, - mask_width=config_box_predictor.mask_width, - mask_prediction_num_conv_layers=( - config_box_predictor.mask_prediction_num_conv_layers), - mask_prediction_conv_depth=( - config_box_predictor.mask_prediction_conv_depth), - masks_are_class_agnostic=( - config_box_predictor.masks_are_class_agnostic), - convolve_then_upsample_masks=( - config_box_predictor.convolve_then_upsample_masks)) - - if box_predictor_oneof == 'rfcn_box_predictor': - config_box_predictor = box_predictor_config.rfcn_box_predictor - conv_hyperparams_fn = argscope_fn(config_box_predictor.conv_hyperparams, - is_training) - box_predictor_object = rfcn_box_predictor.RfcnBoxPredictor( - is_training=is_training, - num_classes=num_classes, - conv_hyperparams_fn=conv_hyperparams_fn, - crop_size=[config_box_predictor.crop_height, - config_box_predictor.crop_width], - num_spatial_bins=[config_box_predictor.num_spatial_bins_height, - config_box_predictor.num_spatial_bins_width], - depth=config_box_predictor.depth, - box_code_size=config_box_predictor.box_code_size) - return box_predictor_object - raise ValueError('Unknown box predictor: {}'.format(box_predictor_oneof)) - - -def build_keras(hyperparams_fn, freeze_batchnorm, inplace_batchnorm_update, - num_predictions_per_location_list, box_predictor_config, - is_training, num_classes, add_background_class=True): - """Builds a Keras-based box predictor based on the configuration. - - Builds Keras-based box predictor based on the configuration. - See box_predictor.proto for configurable options. Also, see box_predictor.py - for more details. - - Args: - hyperparams_fn: A function that takes a hyperparams_pb2.Hyperparams - proto and returns a `hyperparams_builder.KerasLayerHyperparams` - for Conv or FC hyperparameters. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - num_predictions_per_location_list: A list of integers representing the - number of box predictions to be made per spatial location for each - feature map. - box_predictor_config: box_predictor_pb2.BoxPredictor proto containing - configuration. - is_training: Whether the models is in training mode. - num_classes: Number of classes to predict. - add_background_class: Whether to add an implicit background class. - - Returns: - box_predictor: box_predictor.KerasBoxPredictor object. - - Raises: - ValueError: On unknown box predictor, or one with no Keras box predictor. - """ - if not isinstance(box_predictor_config, box_predictor_pb2.BoxPredictor): - raise ValueError('box_predictor_config not of type ' - 'box_predictor_pb2.BoxPredictor.') - - box_predictor_oneof = box_predictor_config.WhichOneof('box_predictor_oneof') - - if box_predictor_oneof == 'convolutional_box_predictor': - config_box_predictor = box_predictor_config.convolutional_box_predictor - conv_hyperparams = hyperparams_fn( - config_box_predictor.conv_hyperparams) - # Optionally apply clipping to box encodings, when box_encodings_clip_range - # is set. - box_encodings_clip_range = None - if config_box_predictor.HasField('box_encodings_clip_range'): - box_encodings_clip_range = BoxEncodingsClipRange( - min=config_box_predictor.box_encodings_clip_range.min, - max=config_box_predictor.box_encodings_clip_range.max) - - return build_convolutional_keras_box_predictor( - is_training=is_training, - num_classes=num_classes, - add_background_class=add_background_class, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - num_predictions_per_location_list=num_predictions_per_location_list, - use_dropout=config_box_predictor.use_dropout, - dropout_keep_prob=config_box_predictor.dropout_keep_probability, - box_code_size=config_box_predictor.box_code_size, - kernel_size=config_box_predictor.kernel_size, - num_layers_before_predictor=( - config_box_predictor.num_layers_before_predictor), - min_depth=config_box_predictor.min_depth, - max_depth=config_box_predictor.max_depth, - class_prediction_bias_init=( - config_box_predictor.class_prediction_bias_init), - use_depthwise=config_box_predictor.use_depthwise, - box_encodings_clip_range=box_encodings_clip_range) - - if box_predictor_oneof == 'weight_shared_convolutional_box_predictor': - config_box_predictor = ( - box_predictor_config.weight_shared_convolutional_box_predictor) - conv_hyperparams = hyperparams_fn(config_box_predictor.conv_hyperparams) - apply_batch_norm = config_box_predictor.conv_hyperparams.HasField( - 'batch_norm') - # During training phase, logits are used to compute the loss. Only apply - # sigmoid at inference to make the inference graph TPU friendly. This is - # required because during TPU inference, model.postprocess is not called. - score_converter_fn = build_score_converter( - config_box_predictor.score_converter, is_training) - # Optionally apply clipping to box encodings, when box_encodings_clip_range - # is set. - box_encodings_clip_range = None - if config_box_predictor.HasField('box_encodings_clip_range'): - box_encodings_clip_range = BoxEncodingsClipRange( - min=config_box_predictor.box_encodings_clip_range.min, - max=config_box_predictor.box_encodings_clip_range.max) - keyword_args = None - - return build_weight_shared_convolutional_keras_box_predictor( - is_training=is_training, - num_classes=num_classes, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - num_predictions_per_location_list=num_predictions_per_location_list, - depth=config_box_predictor.depth, - num_layers_before_predictor=( - config_box_predictor.num_layers_before_predictor), - box_code_size=config_box_predictor.box_code_size, - kernel_size=config_box_predictor.kernel_size, - add_background_class=add_background_class, - class_prediction_bias_init=( - config_box_predictor.class_prediction_bias_init), - use_dropout=config_box_predictor.use_dropout, - dropout_keep_prob=config_box_predictor.dropout_keep_probability, - share_prediction_tower=config_box_predictor.share_prediction_tower, - apply_batch_norm=apply_batch_norm, - use_depthwise=config_box_predictor.use_depthwise, - apply_conv_hyperparams_to_heads=( - config_box_predictor.apply_conv_hyperparams_to_heads), - apply_conv_hyperparams_pointwise=( - config_box_predictor.apply_conv_hyperparams_pointwise), - score_converter_fn=score_converter_fn, - box_encodings_clip_range=box_encodings_clip_range, - keyword_args=keyword_args) - - if box_predictor_oneof == 'mask_rcnn_box_predictor': - config_box_predictor = box_predictor_config.mask_rcnn_box_predictor - fc_hyperparams = hyperparams_fn(config_box_predictor.fc_hyperparams) - conv_hyperparams = None - if config_box_predictor.HasField('conv_hyperparams'): - conv_hyperparams = hyperparams_fn( - config_box_predictor.conv_hyperparams) - return build_mask_rcnn_keras_box_predictor( - is_training=is_training, - num_classes=num_classes, - add_background_class=add_background_class, - fc_hyperparams=fc_hyperparams, - freeze_batchnorm=freeze_batchnorm, - use_dropout=config_box_predictor.use_dropout, - dropout_keep_prob=config_box_predictor.dropout_keep_probability, - box_code_size=config_box_predictor.box_code_size, - share_box_across_classes=( - config_box_predictor.share_box_across_classes), - predict_instance_masks=config_box_predictor.predict_instance_masks, - conv_hyperparams=conv_hyperparams, - mask_height=config_box_predictor.mask_height, - mask_width=config_box_predictor.mask_width, - mask_prediction_num_conv_layers=( - config_box_predictor.mask_prediction_num_conv_layers), - mask_prediction_conv_depth=( - config_box_predictor.mask_prediction_conv_depth), - masks_are_class_agnostic=( - config_box_predictor.masks_are_class_agnostic), - convolve_then_upsample_masks=( - config_box_predictor.convolve_then_upsample_masks)) - - if box_predictor_oneof == 'rfcn_box_predictor': - config_box_predictor = box_predictor_config.rfcn_box_predictor - conv_hyperparams = hyperparams_fn(config_box_predictor.conv_hyperparams) - box_predictor_object = rfcn_keras_box_predictor.RfcnKerasBoxPredictor( - is_training=is_training, - num_classes=num_classes, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - crop_size=[config_box_predictor.crop_height, - config_box_predictor.crop_width], - num_spatial_bins=[config_box_predictor.num_spatial_bins_height, - config_box_predictor.num_spatial_bins_width], - depth=config_box_predictor.depth, - box_code_size=config_box_predictor.box_code_size) - return box_predictor_object - - raise ValueError( - 'Unknown box predictor for Keras: {}'.format(box_predictor_oneof)) diff --git a/research/object_detection/builders/box_predictor_builder_test.py b/research/object_detection/builders/box_predictor_builder_test.py deleted file mode 100644 index 5f44b0f3120..00000000000 --- a/research/object_detection/builders/box_predictor_builder_test.py +++ /dev/null @@ -1,666 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for box_predictor_builder.""" - -import unittest -from unittest import mock # pylint: disable=g-importing-member -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.predictors import mask_rcnn_box_predictor -from object_detection.protos import box_predictor_pb2 -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only Tests.') -class ConvolutionalBoxPredictorBuilderTest(tf.test.TestCase): - - def test_box_predictor_calls_conv_argscope_fn(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - weight: 0.0003 - } - } - initializer { - truncated_normal_initializer { - mean: 0.0 - stddev: 0.3 - } - } - activation: RELU_6 - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - box_predictor_proto.convolutional_box_predictor.conv_hyperparams.CopyFrom( - hyperparams_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=False, - num_classes=10) - (conv_hyperparams_actual, is_training) = box_predictor._conv_hyperparams_fn - self.assertAlmostEqual((hyperparams_proto.regularizer. - l1_regularizer.weight), - (conv_hyperparams_actual.regularizer.l1_regularizer. - weight)) - self.assertAlmostEqual((hyperparams_proto.initializer. - truncated_normal_initializer.stddev), - (conv_hyperparams_actual.initializer. - truncated_normal_initializer.stddev)) - self.assertAlmostEqual((hyperparams_proto.initializer. - truncated_normal_initializer.mean), - (conv_hyperparams_actual.initializer. - truncated_normal_initializer.mean)) - self.assertEqual(hyperparams_proto.activation, - conv_hyperparams_actual.activation) - self.assertFalse(is_training) - - def test_construct_non_default_conv_box_predictor(self): - box_predictor_text_proto = """ - convolutional_box_predictor { - min_depth: 2 - max_depth: 16 - num_layers_before_predictor: 2 - use_dropout: false - dropout_keep_probability: 0.4 - kernel_size: 3 - box_code_size: 3 - apply_sigmoid_to_scores: true - class_prediction_bias_init: 4.0 - use_depthwise: true - } - """ - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - box_predictor_proto.convolutional_box_predictor.conv_hyperparams.CopyFrom( - hyperparams_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=False, - num_classes=10, - add_background_class=False) - class_head = box_predictor._class_prediction_head - self.assertEqual(box_predictor._min_depth, 2) - self.assertEqual(box_predictor._max_depth, 16) - self.assertEqual(box_predictor._num_layers_before_predictor, 2) - self.assertFalse(class_head._use_dropout) - self.assertAlmostEqual(class_head._dropout_keep_prob, 0.4) - self.assertTrue(class_head._apply_sigmoid_to_scores) - self.assertAlmostEqual(class_head._class_prediction_bias_init, 4.0) - self.assertEqual(class_head._num_class_slots, 10) - self.assertEqual(box_predictor.num_classes, 10) - self.assertFalse(box_predictor._is_training) - self.assertTrue(class_head._use_depthwise) - - def test_construct_default_conv_box_predictor(self): - box_predictor_text_proto = """ - convolutional_box_predictor { - conv_hyperparams { - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - }""" - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=hyperparams_builder.build, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - class_head = box_predictor._class_prediction_head - self.assertEqual(box_predictor._min_depth, 0) - self.assertEqual(box_predictor._max_depth, 0) - self.assertEqual(box_predictor._num_layers_before_predictor, 0) - self.assertTrue(class_head._use_dropout) - self.assertAlmostEqual(class_head._dropout_keep_prob, 0.8) - self.assertFalse(class_head._apply_sigmoid_to_scores) - self.assertEqual(class_head._num_class_slots, 91) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertFalse(class_head._use_depthwise) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only Tests.') -class WeightSharedConvolutionalBoxPredictorBuilderTest(tf.test.TestCase): - - def test_box_predictor_calls_conv_argscope_fn(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - weight: 0.0003 - } - } - initializer { - truncated_normal_initializer { - mean: 0.0 - stddev: 0.3 - } - } - activation: RELU_6 - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - (box_predictor_proto.weight_shared_convolutional_box_predictor - .conv_hyperparams.CopyFrom(hyperparams_proto)) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=False, - num_classes=10) - (conv_hyperparams_actual, is_training) = box_predictor._conv_hyperparams_fn - self.assertAlmostEqual((hyperparams_proto.regularizer. - l1_regularizer.weight), - (conv_hyperparams_actual.regularizer.l1_regularizer. - weight)) - self.assertAlmostEqual((hyperparams_proto.initializer. - truncated_normal_initializer.stddev), - (conv_hyperparams_actual.initializer. - truncated_normal_initializer.stddev)) - self.assertAlmostEqual((hyperparams_proto.initializer. - truncated_normal_initializer.mean), - (conv_hyperparams_actual.initializer. - truncated_normal_initializer.mean)) - self.assertEqual(hyperparams_proto.activation, - conv_hyperparams_actual.activation) - self.assertFalse(is_training) - - def test_construct_non_default_conv_box_predictor(self): - box_predictor_text_proto = """ - weight_shared_convolutional_box_predictor { - depth: 2 - num_layers_before_predictor: 2 - kernel_size: 7 - box_code_size: 3 - class_prediction_bias_init: 4.0 - } - """ - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - (box_predictor_proto.weight_shared_convolutional_box_predictor. - conv_hyperparams.CopyFrom(hyperparams_proto)) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=False, - num_classes=10, - add_background_class=False) - class_head = box_predictor._class_prediction_head - self.assertEqual(box_predictor._depth, 2) - self.assertEqual(box_predictor._num_layers_before_predictor, 2) - self.assertAlmostEqual(class_head._class_prediction_bias_init, 4.0) - self.assertEqual(box_predictor.num_classes, 10) - self.assertFalse(box_predictor._is_training) - self.assertEqual(box_predictor._apply_batch_norm, False) - - def test_construct_non_default_depthwise_conv_box_predictor(self): - box_predictor_text_proto = """ - weight_shared_convolutional_box_predictor { - depth: 2 - num_layers_before_predictor: 2 - kernel_size: 7 - box_code_size: 3 - class_prediction_bias_init: 4.0 - use_depthwise: true - } - """ - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - (box_predictor_proto.weight_shared_convolutional_box_predictor. - conv_hyperparams.CopyFrom(hyperparams_proto)) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=False, - num_classes=10, - add_background_class=False) - class_head = box_predictor._class_prediction_head - self.assertEqual(box_predictor._depth, 2) - self.assertEqual(box_predictor._num_layers_before_predictor, 2) - self.assertEqual(box_predictor._apply_batch_norm, False) - self.assertEqual(box_predictor._use_depthwise, True) - self.assertAlmostEqual(class_head._class_prediction_bias_init, 4.0) - self.assertEqual(box_predictor.num_classes, 10) - self.assertFalse(box_predictor._is_training) - - def test_construct_default_conv_box_predictor(self): - box_predictor_text_proto = """ - weight_shared_convolutional_box_predictor { - conv_hyperparams { - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - }""" - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=hyperparams_builder.build, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - self.assertEqual(box_predictor._depth, 0) - self.assertEqual(box_predictor._num_layers_before_predictor, 0) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_predictor._apply_batch_norm, False) - - def test_construct_default_conv_box_predictor_with_batch_norm(self): - box_predictor_text_proto = """ - weight_shared_convolutional_box_predictor { - conv_hyperparams { - regularizer { - l1_regularizer { - } - } - batch_norm { - train: true - } - initializer { - truncated_normal_initializer { - } - } - } - }""" - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=hyperparams_builder.build, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - self.assertEqual(box_predictor._depth, 0) - self.assertEqual(box_predictor._num_layers_before_predictor, 0) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_predictor._apply_batch_norm, True) - - - - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only Tests.') -class MaskRCNNBoxPredictorBuilderTest(tf.test.TestCase): - - def test_box_predictor_builder_calls_fc_argscope_fn(self): - fc_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - weight: 0.0003 - } - } - initializer { - truncated_normal_initializer { - mean: 0.0 - stddev: 0.3 - } - } - activation: RELU_6 - op: FC - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(fc_hyperparams_text_proto, hyperparams_proto) - box_predictor_proto = box_predictor_pb2.BoxPredictor() - box_predictor_proto.mask_rcnn_box_predictor.fc_hyperparams.CopyFrom( - hyperparams_proto) - mock_argscope_fn = mock.Mock(return_value='arg_scope') - box_predictor = box_predictor_builder.build( - argscope_fn=mock_argscope_fn, - box_predictor_config=box_predictor_proto, - is_training=False, - num_classes=10) - mock_argscope_fn.assert_called_with(hyperparams_proto, False) - self.assertEqual(box_predictor._box_prediction_head._fc_hyperparams_fn, - 'arg_scope') - self.assertEqual(box_predictor._class_prediction_head._fc_hyperparams_fn, - 'arg_scope') - - def test_non_default_mask_rcnn_box_predictor(self): - fc_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU_6 - op: FC - """ - box_predictor_text_proto = """ - mask_rcnn_box_predictor { - use_dropout: true - dropout_keep_probability: 0.8 - box_code_size: 3 - share_box_across_classes: true - } - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(fc_hyperparams_text_proto, hyperparams_proto) - def mock_fc_argscope_builder(fc_hyperparams_arg, is_training): - return (fc_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - box_predictor_proto.mask_rcnn_box_predictor.fc_hyperparams.CopyFrom( - hyperparams_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_fc_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - box_head = box_predictor._box_prediction_head - class_head = box_predictor._class_prediction_head - self.assertTrue(box_head._use_dropout) - self.assertTrue(class_head._use_dropout) - self.assertAlmostEqual(box_head._dropout_keep_prob, 0.8) - self.assertAlmostEqual(class_head._dropout_keep_prob, 0.8) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_head._box_code_size, 3) - self.assertEqual(box_head._share_box_across_classes, True) - - def test_build_default_mask_rcnn_box_predictor(self): - box_predictor_proto = box_predictor_pb2.BoxPredictor() - box_predictor_proto.mask_rcnn_box_predictor.fc_hyperparams.op = ( - hyperparams_pb2.Hyperparams.FC) - box_predictor = box_predictor_builder.build( - argscope_fn=mock.Mock(return_value='arg_scope'), - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - box_head = box_predictor._box_prediction_head - class_head = box_predictor._class_prediction_head - self.assertFalse(box_head._use_dropout) - self.assertFalse(class_head._use_dropout) - self.assertAlmostEqual(box_head._dropout_keep_prob, 0.5) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_head._box_code_size, 4) - self.assertEqual(len(box_predictor._third_stage_heads.keys()), 0) - - def test_build_box_predictor_with_mask_branch(self): - box_predictor_proto = box_predictor_pb2.BoxPredictor() - box_predictor_proto.mask_rcnn_box_predictor.fc_hyperparams.op = ( - hyperparams_pb2.Hyperparams.FC) - box_predictor_proto.mask_rcnn_box_predictor.conv_hyperparams.op = ( - hyperparams_pb2.Hyperparams.CONV) - box_predictor_proto.mask_rcnn_box_predictor.predict_instance_masks = True - box_predictor_proto.mask_rcnn_box_predictor.mask_prediction_conv_depth = 512 - box_predictor_proto.mask_rcnn_box_predictor.mask_height = 16 - box_predictor_proto.mask_rcnn_box_predictor.mask_width = 16 - mock_argscope_fn = mock.Mock(return_value='arg_scope') - box_predictor = box_predictor_builder.build( - argscope_fn=mock_argscope_fn, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - mock_argscope_fn.assert_has_calls( - [mock.call(box_predictor_proto.mask_rcnn_box_predictor.fc_hyperparams, - True), - mock.call(box_predictor_proto.mask_rcnn_box_predictor.conv_hyperparams, - True)], any_order=True) - box_head = box_predictor._box_prediction_head - class_head = box_predictor._class_prediction_head - third_stage_heads = box_predictor._third_stage_heads - self.assertFalse(box_head._use_dropout) - self.assertFalse(class_head._use_dropout) - self.assertAlmostEqual(box_head._dropout_keep_prob, 0.5) - self.assertAlmostEqual(class_head._dropout_keep_prob, 0.5) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_head._box_code_size, 4) - self.assertIn( - mask_rcnn_box_predictor.MASK_PREDICTIONS, third_stage_heads) - self.assertEqual( - third_stage_heads[mask_rcnn_box_predictor.MASK_PREDICTIONS] - ._mask_prediction_conv_depth, 512) - - def test_build_box_predictor_with_convlve_then_upsample_masks(self): - box_predictor_proto = box_predictor_pb2.BoxPredictor() - box_predictor_proto.mask_rcnn_box_predictor.fc_hyperparams.op = ( - hyperparams_pb2.Hyperparams.FC) - box_predictor_proto.mask_rcnn_box_predictor.conv_hyperparams.op = ( - hyperparams_pb2.Hyperparams.CONV) - box_predictor_proto.mask_rcnn_box_predictor.predict_instance_masks = True - box_predictor_proto.mask_rcnn_box_predictor.mask_prediction_conv_depth = 512 - box_predictor_proto.mask_rcnn_box_predictor.mask_height = 24 - box_predictor_proto.mask_rcnn_box_predictor.mask_width = 24 - box_predictor_proto.mask_rcnn_box_predictor.convolve_then_upsample_masks = ( - True) - - mock_argscope_fn = mock.Mock(return_value='arg_scope') - box_predictor = box_predictor_builder.build( - argscope_fn=mock_argscope_fn, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - mock_argscope_fn.assert_has_calls( - [mock.call(box_predictor_proto.mask_rcnn_box_predictor.fc_hyperparams, - True), - mock.call(box_predictor_proto.mask_rcnn_box_predictor.conv_hyperparams, - True)], any_order=True) - box_head = box_predictor._box_prediction_head - class_head = box_predictor._class_prediction_head - third_stage_heads = box_predictor._third_stage_heads - self.assertFalse(box_head._use_dropout) - self.assertFalse(class_head._use_dropout) - self.assertAlmostEqual(box_head._dropout_keep_prob, 0.5) - self.assertAlmostEqual(class_head._dropout_keep_prob, 0.5) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_head._box_code_size, 4) - self.assertIn( - mask_rcnn_box_predictor.MASK_PREDICTIONS, third_stage_heads) - self.assertEqual( - third_stage_heads[mask_rcnn_box_predictor.MASK_PREDICTIONS] - ._mask_prediction_conv_depth, 512) - self.assertTrue(third_stage_heads[mask_rcnn_box_predictor.MASK_PREDICTIONS] - ._convolve_then_upsample) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only Tests.') -class RfcnBoxPredictorBuilderTest(tf.test.TestCase): - - def test_box_predictor_calls_fc_argscope_fn(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - weight: 0.0003 - } - } - initializer { - truncated_normal_initializer { - mean: 0.0 - stddev: 0.3 - } - } - activation: RELU_6 - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - box_predictor_proto.rfcn_box_predictor.conv_hyperparams.CopyFrom( - hyperparams_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=False, - num_classes=10) - (conv_hyperparams_actual, is_training) = box_predictor._conv_hyperparams_fn - self.assertAlmostEqual((hyperparams_proto.regularizer. - l1_regularizer.weight), - (conv_hyperparams_actual.regularizer.l1_regularizer. - weight)) - self.assertAlmostEqual((hyperparams_proto.initializer. - truncated_normal_initializer.stddev), - (conv_hyperparams_actual.initializer. - truncated_normal_initializer.stddev)) - self.assertAlmostEqual((hyperparams_proto.initializer. - truncated_normal_initializer.mean), - (conv_hyperparams_actual.initializer. - truncated_normal_initializer.mean)) - self.assertEqual(hyperparams_proto.activation, - conv_hyperparams_actual.activation) - self.assertFalse(is_training) - - def test_non_default_rfcn_box_predictor(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU_6 - """ - box_predictor_text_proto = """ - rfcn_box_predictor { - num_spatial_bins_height: 4 - num_spatial_bins_width: 4 - depth: 4 - box_code_size: 3 - crop_height: 16 - crop_width: 16 - } - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(box_predictor_text_proto, box_predictor_proto) - box_predictor_proto.rfcn_box_predictor.conv_hyperparams.CopyFrom( - hyperparams_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_predictor._box_code_size, 3) - self.assertEqual(box_predictor._num_spatial_bins, [4, 4]) - self.assertEqual(box_predictor._crop_size, [16, 16]) - - def test_default_rfcn_box_predictor(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU_6 - """ - hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, hyperparams_proto) - def mock_conv_argscope_builder(conv_hyperparams_arg, is_training): - return (conv_hyperparams_arg, is_training) - - box_predictor_proto = box_predictor_pb2.BoxPredictor() - box_predictor_proto.rfcn_box_predictor.conv_hyperparams.CopyFrom( - hyperparams_proto) - box_predictor = box_predictor_builder.build( - argscope_fn=mock_conv_argscope_builder, - box_predictor_config=box_predictor_proto, - is_training=True, - num_classes=90) - self.assertEqual(box_predictor.num_classes, 90) - self.assertTrue(box_predictor._is_training) - self.assertEqual(box_predictor._box_code_size, 4) - self.assertEqual(box_predictor._num_spatial_bins, [3, 3]) - self.assertEqual(box_predictor._crop_size, [12, 12]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/calibration_builder.py b/research/object_detection/builders/calibration_builder.py deleted file mode 100644 index 4adc170d3f1..00000000000 --- a/research/object_detection/builders/calibration_builder.py +++ /dev/null @@ -1,250 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tensorflow ops to calibrate class predictions and background class.""" - -import tensorflow.compat.v1 as tf -from object_detection.utils import shape_utils - - -def _find_interval_containing_new_value(x, new_value): - """Find the index of x (ascending-ordered) after which new_value occurs.""" - new_value_shape = shape_utils.combined_static_and_dynamic_shape(new_value)[0] - x_shape = shape_utils.combined_static_and_dynamic_shape(x)[0] - compare = tf.cast(tf.reshape(new_value, shape=(new_value_shape, 1)) >= - tf.reshape(x, shape=(1, x_shape)), - dtype=tf.int32) - diff = compare[:, 1:] - compare[:, :-1] - interval_idx = tf.argmin(diff, axis=1) - return interval_idx - - -def _tf_linear_interp1d(x_to_interpolate, fn_x, fn_y): - """Tensorflow implementation of 1d linear interpolation. - - Args: - x_to_interpolate: tf.float32 Tensor of shape (num_examples,) over which 1d - linear interpolation is performed. - fn_x: Monotonically-increasing, non-repeating tf.float32 Tensor of shape - (length,) used as the domain to approximate a function. - fn_y: tf.float32 Tensor of shape (length,) used as the range to approximate - a function. - - Returns: - tf.float32 Tensor of shape (num_examples,) - """ - x_pad = tf.concat([fn_x[:1] - 1, fn_x, fn_x[-1:] + 1], axis=0) - y_pad = tf.concat([fn_y[:1], fn_y, fn_y[-1:]], axis=0) - interval_idx = _find_interval_containing_new_value(x_pad, x_to_interpolate) - - # Interpolate - alpha = ( - (x_to_interpolate - tf.gather(x_pad, interval_idx)) / - (tf.gather(x_pad, interval_idx + 1) - tf.gather(x_pad, interval_idx))) - interpolation = ((1 - alpha) * tf.gather(y_pad, interval_idx) + - alpha * tf.gather(y_pad, interval_idx + 1)) - - return interpolation - - -def _function_approximation_proto_to_tf_tensors(x_y_pairs_message): - """Extracts (x,y) pairs from a XYPairs message. - - Args: - x_y_pairs_message: calibration_pb2..XYPairs proto - Returns: - tf_x: tf.float32 tensor of shape (number_xy_pairs,) for function domain. - tf_y: tf.float32 tensor of shape (number_xy_pairs,) for function range. - """ - tf_x = tf.convert_to_tensor([x_y_pair.x - for x_y_pair - in x_y_pairs_message.x_y_pair], - dtype=tf.float32) - tf_y = tf.convert_to_tensor([x_y_pair.y - for x_y_pair - in x_y_pairs_message.x_y_pair], - dtype=tf.float32) - return tf_x, tf_y - - -def _get_class_id_function_dict(calibration_config): - """Create a dictionary mapping class id to function approximations. - - Args: - calibration_config: calibration_pb2 proto containing - id_function_approximations. - Returns: - Dictionary mapping a class id to a tuple of TF tensors to be used for - function approximation. - """ - class_id_function_dict = {} - class_id_xy_pairs_map = ( - calibration_config.class_id_function_approximations.class_id_xy_pairs_map) - for class_id in class_id_xy_pairs_map: - class_id_function_dict[class_id] = ( - _function_approximation_proto_to_tf_tensors( - class_id_xy_pairs_map[class_id])) - - return class_id_function_dict - - -def build(calibration_config): - """Returns a function that calibrates Tensorflow model scores. - - All returned functions are expected to apply positive monotonic - transformations to inputs (i.e. score ordering is strictly preserved or - adjacent scores are mapped to the same score, but an input of lower value - should never be exceed an input of higher value after transformation). For - class-agnostic calibration, positive monotonicity should hold across all - scores. In class-specific cases, positive monotonicity should hold within each - class. - - Args: - calibration_config: calibration_pb2.CalibrationConfig proto. - Returns: - Function that that accepts class_predictions_with_background and calibrates - the output based on calibration_config's parameters. - Raises: - ValueError: No calibration builder defined for "Oneof" in - calibration_config. - """ - - # Linear Interpolation (usually used as a result of calibration via - # isotonic regression). - if calibration_config.WhichOneof('calibrator') == 'function_approximation': - - def calibration_fn(class_predictions_with_background): - """Calibrate predictions via 1-d linear interpolation. - - Predictions scores are linearly interpolated based on a class-agnostic - function approximation. Note that the 0-indexed background class is also - transformed. - - Args: - class_predictions_with_background: tf.float32 tensor of shape - [batch_size, num_anchors, num_classes + 1] containing scores on the - interval [0,1]. This is usually produced by a sigmoid or softmax layer - and the result of calling the `predict` method of a detection model. - - Returns: - tf.float32 tensor of the same shape as the input with values on the - interval [0, 1]. - """ - # Flattening Tensors and then reshaping at the end. - flat_class_predictions_with_background = tf.reshape( - class_predictions_with_background, shape=[-1]) - fn_x, fn_y = _function_approximation_proto_to_tf_tensors( - calibration_config.function_approximation.x_y_pairs) - updated_scores = _tf_linear_interp1d( - flat_class_predictions_with_background, fn_x, fn_y) - - # Un-flatten the scores - original_detections_shape = shape_utils.combined_static_and_dynamic_shape( - class_predictions_with_background) - calibrated_class_predictions_with_background = tf.reshape( - updated_scores, - shape=original_detections_shape, - name='calibrate_scores') - return calibrated_class_predictions_with_background - - elif (calibration_config.WhichOneof('calibrator') == - 'class_id_function_approximations'): - - def calibration_fn(class_predictions_with_background): - """Calibrate predictions per class via 1-d linear interpolation. - - Prediction scores are linearly interpolated with class-specific function - approximations. Note that after calibration, an anchor's class scores will - not necessarily sum to 1, and score ordering may change, depending on each - class' calibration parameters. - - Args: - class_predictions_with_background: tf.float32 tensor of shape - [batch_size, num_anchors, num_classes + 1] containing scores on the - interval [0,1]. This is usually produced by a sigmoid or softmax layer - and the result of calling the `predict` method of a detection model. - - Returns: - tf.float32 tensor of the same shape as the input with values on the - interval [0, 1]. - - Raises: - KeyError: Calibration parameters are not present for a class. - """ - class_id_function_dict = _get_class_id_function_dict(calibration_config) - - # Tensors are split by class and then recombined at the end to recover - # the input's original shape. If a class id does not have calibration - # parameters, it is left unchanged. - class_tensors = tf.unstack(class_predictions_with_background, axis=-1) - calibrated_class_tensors = [] - for class_id, class_tensor in enumerate(class_tensors): - flat_class_tensor = tf.reshape(class_tensor, shape=[-1]) - if class_id in class_id_function_dict: - output_tensor = _tf_linear_interp1d( - x_to_interpolate=flat_class_tensor, - fn_x=class_id_function_dict[class_id][0], - fn_y=class_id_function_dict[class_id][1]) - else: - tf.logging.info( - 'Calibration parameters for class id `%d` not not found', - class_id) - output_tensor = flat_class_tensor - calibrated_class_tensors.append(output_tensor) - - combined_calibrated_tensor = tf.stack(calibrated_class_tensors, axis=1) - input_shape = shape_utils.combined_static_and_dynamic_shape( - class_predictions_with_background) - calibrated_class_predictions_with_background = tf.reshape( - combined_calibrated_tensor, - shape=input_shape, - name='calibrate_scores') - return calibrated_class_predictions_with_background - - elif (calibration_config.WhichOneof('calibrator') == - 'temperature_scaling_calibration'): - - def calibration_fn(class_predictions_with_background): - """Calibrate predictions via temperature scaling. - - Predictions logits scores are scaled by the temperature scaler. Note that - the 0-indexed background class is also transformed. - - Args: - class_predictions_with_background: tf.float32 tensor of shape - [batch_size, num_anchors, num_classes + 1] containing logits scores. - This is usually produced before a sigmoid or softmax layer. - - Returns: - tf.float32 tensor of the same shape as the input. - - Raises: - ValueError: If temperature scaler is of incorrect value. - """ - scaler = calibration_config.temperature_scaling_calibration.scaler - if scaler <= 0: - raise ValueError('The scaler in temperature scaling must be positive.') - calibrated_class_predictions_with_background = tf.math.divide( - class_predictions_with_background, - scaler, - name='calibrate_score') - return calibrated_class_predictions_with_background - - # TODO(zbeaver): Add sigmoid calibration. - else: - raise ValueError('No calibration builder defined for "Oneof" in ' - 'calibration_config.') - - return calibration_fn diff --git a/research/object_detection/builders/calibration_builder_test.py b/research/object_detection/builders/calibration_builder_test.py deleted file mode 100644 index 9c00ebb02af..00000000000 --- a/research/object_detection/builders/calibration_builder_test.py +++ /dev/null @@ -1,232 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for calibration_builder.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import numpy as np -from scipy import interpolate -from six.moves import zip -import tensorflow.compat.v1 as tf -from object_detection.builders import calibration_builder -from object_detection.protos import calibration_pb2 -from object_detection.utils import test_case - - -class CalibrationBuilderTest(test_case.TestCase): - - def test_tf_linear_interp1d_map(self): - """Tests TF linear interpolation mapping to a single number.""" - def graph_fn(): - tf_x = tf.constant([0., 0.5, 1.]) - tf_y = tf.constant([0.5, 0.5, 0.5]) - new_x = tf.constant([0., 0.25, 0.5, 0.75, 1.]) - tf_map_outputs = calibration_builder._tf_linear_interp1d( - new_x, tf_x, tf_y) - return tf_map_outputs - tf_map_outputs_np = self.execute(graph_fn, []) - self.assertAllClose(tf_map_outputs_np, [0.5, 0.5, 0.5, 0.5, 0.5]) - - def test_tf_linear_interp1d_interpolate(self): - """Tests TF 1d linear interpolation not mapping to a single number.""" - def graph_fn(): - tf_x = tf.constant([0., 0.5, 1.]) - tf_y = tf.constant([0.6, 0.7, 1.0]) - new_x = tf.constant([0., 0.25, 0.5, 0.75, 1.]) - tf_interpolate_outputs = calibration_builder._tf_linear_interp1d( - new_x, tf_x, tf_y) - return tf_interpolate_outputs - tf_interpolate_outputs_np = self.execute(graph_fn, []) - self.assertAllClose(tf_interpolate_outputs_np, [0.6, 0.65, 0.7, 0.85, 1.]) - - @staticmethod - def _get_scipy_interp1d(new_x, x, y): - """Helper performing 1d linear interpolation using SciPy.""" - interpolation1d_fn = interpolate.interp1d(x, y) - return interpolation1d_fn(new_x) - - def _get_tf_interp1d(self, new_x, x, y): - """Helper performing 1d linear interpolation using Tensorflow.""" - def graph_fn(): - tf_interp_outputs = calibration_builder._tf_linear_interp1d( - tf.convert_to_tensor(new_x, dtype=tf.float32), - tf.convert_to_tensor(x, dtype=tf.float32), - tf.convert_to_tensor(y, dtype=tf.float32)) - return tf_interp_outputs - np_tf_interp_outputs = self.execute(graph_fn, []) - return np_tf_interp_outputs - - def test_tf_linear_interp1d_against_scipy_map(self): - """Tests parity of TF linear interpolation with SciPy for simple mapping.""" - length = 10 - np_x = np.linspace(0, 1, length) - - # Mapping all numbers to 0.5 - np_y_map = np.repeat(0.5, length) - - # Scipy and TF interpolations - test_data_np = np.linspace(0, 1, length * 10) - scipy_map_outputs = self._get_scipy_interp1d(test_data_np, np_x, np_y_map) - np_tf_map_outputs = self._get_tf_interp1d(test_data_np, np_x, np_y_map) - self.assertAllClose(scipy_map_outputs, np_tf_map_outputs) - - def test_tf_linear_interp1d_against_scipy_interpolate(self): - """Tests parity of TF linear interpolation with SciPy.""" - length = 10 - np_x = np.linspace(0, 1, length) - - # Requires interpolation over 0.5 to 1 domain - np_y_interp = np.linspace(0.5, 1, length) - - # Scipy interpolation for comparison - test_data_np = np.linspace(0, 1, length * 10) - scipy_interp_outputs = self._get_scipy_interp1d(test_data_np, np_x, - np_y_interp) - np_tf_interp_outputs = self._get_tf_interp1d(test_data_np, np_x, - np_y_interp) - self.assertAllClose(scipy_interp_outputs, np_tf_interp_outputs) - - @staticmethod - def _add_function_approximation_to_calibration_proto(calibration_proto, - x_array, y_array, - class_id): - """Adds a function approximation to calibration proto for a class id.""" - # Per-class calibration. - if class_id is not None: - function_approximation = ( - calibration_proto.class_id_function_approximations - .class_id_xy_pairs_map[class_id]) - # Class-agnostic calibration. - else: - function_approximation = ( - calibration_proto.function_approximation.x_y_pairs) - - for x, y in zip(x_array, y_array): - x_y_pair_message = function_approximation.x_y_pair.add() - x_y_pair_message.x = x - x_y_pair_message.y = y - - def test_class_agnostic_function_approximation(self): - """Tests that calibration produces correct class-agnostic values.""" - # Generate fake calibration proto. For this interpolation, any input on - # [0.0, 0.5] should be divided by 2 and any input on (0.5, 1.0] should have - # 0.25 subtracted from it. - class_agnostic_x = np.asarray([0.0, 0.5, 1.0]) - class_agnostic_y = np.asarray([0.0, 0.25, 0.75]) - calibration_config = calibration_pb2.CalibrationConfig() - self._add_function_approximation_to_calibration_proto( - calibration_config, class_agnostic_x, class_agnostic_y, class_id=None) - - def graph_fn(): - calibration_fn = calibration_builder.build(calibration_config) - # batch_size = 2, num_classes = 2, num_anchors = 2. - class_predictions_with_background = tf.constant( - [[[0.1, 0.2, 0.3], - [0.4, 0.5, 0.0]], - [[0.6, 0.7, 0.8], - [0.9, 1.0, 1.0]]], dtype=tf.float32) - - # Everything should map to 0.5 if classes are ignored. - calibrated_scores = calibration_fn(class_predictions_with_background) - return calibrated_scores - calibrated_scores_np = self.execute(graph_fn, []) - self.assertAllClose(calibrated_scores_np, [[[0.05, 0.1, 0.15], - [0.2, 0.25, 0.0]], - [[0.35, 0.45, 0.55], - [0.65, 0.75, 0.75]]]) - - def test_multiclass_function_approximations(self): - """Tests that calibration produces correct multiclass values.""" - # Background class (0-index) maps all predictions to 0.5. - class_0_x = np.asarray([0.0, 0.5, 1.0]) - class_0_y = np.asarray([0.5, 0.5, 0.5]) - calibration_config = calibration_pb2.CalibrationConfig() - self._add_function_approximation_to_calibration_proto( - calibration_config, class_0_x, class_0_y, class_id=0) - - # Class id 1 will interpolate using these values. - class_1_x = np.asarray([0.0, 0.2, 1.0]) - class_1_y = np.asarray([0.0, 0.6, 1.0]) - self._add_function_approximation_to_calibration_proto( - calibration_config, class_1_x, class_1_y, class_id=1) - - def graph_fn(): - calibration_fn = calibration_builder.build(calibration_config) - # batch_size = 2, num_classes = 2, num_anchors = 2. - class_predictions_with_background = tf.constant( - [[[0.1, 0.2], [0.9, 0.1]], - [[0.6, 0.4], [0.08, 0.92]]], - dtype=tf.float32) - calibrated_scores = calibration_fn(class_predictions_with_background) - return calibrated_scores - calibrated_scores_np = self.execute(graph_fn, []) - self.assertAllClose(calibrated_scores_np, [[[0.5, 0.6], [0.5, 0.3]], - [[0.5, 0.7], [0.5, 0.96]]]) - - def test_temperature_scaling(self): - """Tests that calibration produces correct temperature scaling values.""" - calibration_config = calibration_pb2.CalibrationConfig() - calibration_config.temperature_scaling_calibration.scaler = 2.0 - - def graph_fn(): - calibration_fn = calibration_builder.build(calibration_config) - # batch_size = 2, num_classes = 2, num_anchors = 2. - class_predictions_with_background = tf.constant( - [[[0.1, 0.2, 0.3], [0.4, 0.5, 0.0]], - [[0.6, 0.7, 0.8], [0.9, 1.0, 1.0]]], - dtype=tf.float32) - calibrated_scores = calibration_fn(class_predictions_with_background) - return calibrated_scores - calibrated_scores_np = self.execute(graph_fn, []) - self.assertAllClose(calibrated_scores_np, - [[[0.05, 0.1, 0.15], [0.2, 0.25, 0.0]], - [[0.3, 0.35, 0.4], [0.45, 0.5, 0.5]]]) - - def test_temperature_scaling_incorrect_value_error(self): - calibration_config = calibration_pb2.CalibrationConfig() - calibration_config.temperature_scaling_calibration.scaler = 0 - - calibration_fn = calibration_builder.build(calibration_config) - class_predictions_with_background = tf.constant( - [[[0.1, 0.2, 0.3]]], dtype=tf.float32) - with self.assertRaises(ValueError): - calibration_fn(class_predictions_with_background) - - def test_skips_class_when_calibration_parameters_not_present(self): - """Tests that graph fails when parameters not present for all classes.""" - # Only adding calibration parameters for class id = 0, even though class id - # 1 is present in the data. - class_0_x = np.asarray([0.0, 0.5, 1.0]) - class_0_y = np.asarray([0.5, 0.5, 0.5]) - calibration_config = calibration_pb2.CalibrationConfig() - self._add_function_approximation_to_calibration_proto( - calibration_config, class_0_x, class_0_y, class_id=0) - def graph_fn(): - calibration_fn = calibration_builder.build(calibration_config) - # batch_size = 2, num_classes = 2, num_anchors = 2. - class_predictions_with_background = tf.constant( - [[[0.1, 0.2], [0.9, 0.1]], - [[0.6, 0.4], [0.08, 0.92]]], - dtype=tf.float32) - calibrated_scores = calibration_fn(class_predictions_with_background) - return calibrated_scores - calibrated_scores_np = self.execute(graph_fn, []) - self.assertAllClose(calibrated_scores_np, [[[0.5, 0.2], [0.5, 0.1]], - [[0.5, 0.4], [0.5, 0.92]]]) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/dataset_builder.py b/research/object_detection/builders/dataset_builder.py deleted file mode 100644 index cdc9cc72a41..00000000000 --- a/research/object_detection/builders/dataset_builder.py +++ /dev/null @@ -1,264 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""tf.data.Dataset builder. - -Creates data sources for DetectionModels from an InputReader config. See -input_reader.proto for options. - -Note: If users wishes to also use their own InputReaders with the Object -Detection configuration framework, they should define their own builder function -that wraps the build function. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -import math -import tensorflow.compat.v1 as tf - -from object_detection.builders import decoder_builder -from object_detection.protos import input_reader_pb2 - - -def make_initializable_iterator(dataset): - """Creates an iterator, and initializes tables. - - This is useful in cases where make_one_shot_iterator wouldn't work because - the graph contains a hash table that needs to be initialized. - - Args: - dataset: A `tf.data.Dataset` object. - - Returns: - A `tf.data.Iterator`. - """ - iterator = dataset.make_initializable_iterator() - tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer) - return iterator - - -def _read_dataset_internal(file_read_func, - input_files, - num_readers, - config, - filename_shard_fn=None): - """Reads a dataset, and handles repetition and shuffling. - - Args: - file_read_func: Function to use in tf_data.parallel_interleave, to read - every individual file into a tf.data.Dataset. - input_files: A list of file paths to read. - num_readers: Number of readers to use. - config: A input_reader_builder.InputReader object. - filename_shard_fn: optional, A function used to shard filenames across - replicas. This function takes as input a TF dataset of filenames and is - expected to return its sharded version. It is useful when the dataset is - being loaded on one of possibly many replicas and we want to evenly shard - the files between the replicas. - - Returns: - A tf.data.Dataset of (undecoded) tf-records based on config. - - Raises: - RuntimeError: If no files are found at the supplied path(s). - """ - filenames = tf.gfile.Glob(input_files) - tf.logging.info('Reading record datasets for input file: %s' % input_files) - tf.logging.info('Number of filenames to read: %s' % len(filenames)) - if not filenames: - raise RuntimeError('Did not find any input files matching the glob pattern ' - '{}'.format(input_files)) - if num_readers > len(filenames): - num_readers = len(filenames) - tf.logging.warning('num_readers has been reduced to %d to match input file ' - 'shards.' % num_readers) - filename_dataset = tf.data.Dataset.from_tensor_slices(filenames) - if config.shuffle: - filename_dataset = filename_dataset.shuffle( - config.filenames_shuffle_buffer_size) - elif num_readers > 1: - tf.logging.warning('`shuffle` is false, but the input data stream is ' - 'still slightly shuffled since `num_readers` > 1.') - if filename_shard_fn: - filename_dataset = filename_shard_fn(filename_dataset) - - filename_dataset = filename_dataset.repeat(config.num_epochs or None) - records_dataset = filename_dataset.apply( - tf.data.experimental.parallel_interleave( - file_read_func, - cycle_length=num_readers, - block_length=config.read_block_length, - sloppy=config.shuffle)) - if config.shuffle: - records_dataset = records_dataset.shuffle(config.shuffle_buffer_size) - return records_dataset - - -def read_dataset(file_read_func, input_files, config, filename_shard_fn=None): - """Reads multiple datasets with sampling. - - Args: - file_read_func: Function to use in tf_data.parallel_interleave, to read - every individual file into a tf.data.Dataset. - input_files: A list of file paths to read. - config: A input_reader_builder.InputReader object. - filename_shard_fn: optional, A function used to shard filenames across - replicas. This function takes as input a TF dataset of filenames and is - expected to return its sharded version. It is useful when the dataset is - being loaded on one of possibly many replicas and we want to evenly shard - the files between the replicas. - - Returns: - A tf.data.Dataset of (undecoded) tf-records based on config. - - Raises: - RuntimeError: If no files are found at the supplied path(s). - """ - if config.sample_from_datasets_weights: - tf.logging.info('Reading weighted datasets: %s' % input_files) - if len(input_files) != len(config.sample_from_datasets_weights): - raise ValueError('Expected the number of input files to be the same as ' - 'the number of dataset sample weights. But got ' - '[input_files, sample_from_datasets_weights]: [' + - input_files + ', ' + - str(config.sample_from_datasets_weights) + ']') - tf.logging.info('Sampling from datasets %s with weights %s' % - (input_files, config.sample_from_datasets_weights)) - records_datasets = [] - dataset_weights = [] - for i, input_file in enumerate(input_files): - weight = config.sample_from_datasets_weights[i] - num_readers = math.ceil(config.num_readers * - weight / - sum(config.sample_from_datasets_weights)) - tf.logging.info( - 'Num readers for dataset [%s]: %d', input_file, num_readers) - if num_readers == 0: - tf.logging.info('Skipping dataset due to zero weights: %s', input_file) - continue - tf.logging.info( - 'Num readers for dataset [%s]: %d', input_file, num_readers) - records_dataset = _read_dataset_internal(file_read_func, [input_file], - num_readers, config, - filename_shard_fn) - dataset_weights.append(weight) - records_datasets.append(records_dataset) - return tf.data.experimental.sample_from_datasets(records_datasets, - dataset_weights) - else: - tf.logging.info('Reading unweighted datasets: %s' % input_files) - return _read_dataset_internal(file_read_func, input_files, - config.num_readers, config, filename_shard_fn) - - -def shard_function_for_context(input_context): - """Returns a function that shards filenames based on the input context.""" - - if input_context is None: - return None - - def shard_fn(dataset): - return dataset.shard( - input_context.num_input_pipelines, input_context.input_pipeline_id) - - return shard_fn - - -def build(input_reader_config, batch_size=None, transform_input_data_fn=None, - input_context=None, reduce_to_frame_fn=None): - """Builds a tf.data.Dataset. - - Builds a tf.data.Dataset by applying the `transform_input_data_fn` on all - records. Applies a padded batch to the resulting dataset. - - Args: - input_reader_config: A input_reader_pb2.InputReader object. - batch_size: Batch size. If batch size is None, no batching is performed. - transform_input_data_fn: Function to apply transformation to all records, - or None if no extra decoding is required. - input_context: optional, A tf.distribute.InputContext object used to - shard filenames and compute per-replica batch_size when this function - is being called per-replica. - reduce_to_frame_fn: Function that extracts frames from tf.SequenceExample - type input data. - - Returns: - A tf.data.Dataset based on the input_reader_config. - - Raises: - ValueError: On invalid input reader proto. - ValueError: If no input paths are specified. - """ - if not isinstance(input_reader_config, input_reader_pb2.InputReader): - raise ValueError('input_reader_config not of type ' - 'input_reader_pb2.InputReader.') - - decoder = decoder_builder.build(input_reader_config) - - if input_reader_config.WhichOneof('input_reader') == 'tf_record_input_reader': - config = input_reader_config.tf_record_input_reader - if not config.input_path: - raise ValueError('At least one input path must be specified in ' - '`input_reader_config`.') - def dataset_map_fn(dataset, fn_to_map, batch_size=None, - input_reader_config=None): - """Handles whether or not to use the legacy map function. - - Args: - dataset: A tf.Dataset. - fn_to_map: The function to be mapped for that dataset. - batch_size: Batch size. If batch size is None, no batching is performed. - input_reader_config: A input_reader_pb2.InputReader object. - - Returns: - A tf.data.Dataset mapped with fn_to_map. - """ - if hasattr(dataset, 'map_with_legacy_function'): - if batch_size: - num_parallel_calls = batch_size * ( - input_reader_config.num_parallel_batches) - else: - num_parallel_calls = input_reader_config.num_parallel_map_calls - dataset = dataset.map_with_legacy_function( - fn_to_map, num_parallel_calls=num_parallel_calls) - else: - dataset = dataset.map(fn_to_map, tf.data.experimental.AUTOTUNE) - return dataset - shard_fn = shard_function_for_context(input_context) - if input_context is not None: - batch_size = input_context.get_per_replica_batch_size(batch_size) - dataset = read_dataset( - functools.partial(tf.data.TFRecordDataset, buffer_size=8 * 1000 * 1000), - config.input_path[:], input_reader_config, filename_shard_fn=shard_fn) - if input_reader_config.sample_1_of_n_examples > 1: - dataset = dataset.shard(input_reader_config.sample_1_of_n_examples, 0) - # TODO(rathodv): make batch size a required argument once the old binaries - # are deleted. - dataset = dataset_map_fn(dataset, decoder.decode, batch_size, - input_reader_config) - if reduce_to_frame_fn: - dataset = reduce_to_frame_fn(dataset, dataset_map_fn, batch_size, - input_reader_config) - if transform_input_data_fn is not None: - dataset = dataset_map_fn(dataset, transform_input_data_fn, - batch_size, input_reader_config) - if batch_size: - dataset = dataset.batch(batch_size, - drop_remainder=input_reader_config.drop_remainder) - dataset = dataset.prefetch(input_reader_config.num_prefetch_batches) - return dataset - - raise ValueError('Unsupported input_reader_config.') diff --git a/research/object_detection/builders/dataset_builder_test.py b/research/object_detection/builders/dataset_builder_test.py deleted file mode 100644 index 7dd4dc67fb3..00000000000 --- a/research/object_detection/builders/dataset_builder_test.py +++ /dev/null @@ -1,737 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for dataset_builder.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import numpy as np -from six.moves import range -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import dataset_builder -from object_detection.core import standard_fields as fields -from object_detection.dataset_tools import seq_example_util -from object_detection.protos import input_reader_pb2 -from object_detection.utils import dataset_util -from object_detection.utils import test_case - -# pylint: disable=g-import-not-at-top -try: - from tensorflow.contrib import lookup as contrib_lookup -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - - -def get_iterator_next_for_testing(dataset, is_tf2): - iterator = dataset.make_initializable_iterator() - if not is_tf2: - tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer) - return iterator.get_next() - - -def _get_labelmap_path(): - """Returns an absolute path to label map file.""" - parent_path = os.path.dirname(tf.resource_loader.get_data_files_path()) - return os.path.join(parent_path, 'data', - 'pet_label_map.pbtxt') - - -class DatasetBuilderTest(test_case.TestCase): - - def create_tf_record(self, has_additional_channels=False, num_shards=1, - num_examples_per_shard=1): - - def dummy_jpeg_fn(): - image_tensor = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8) - additional_channels_tensor = np.random.randint( - 255, size=(4, 5, 1)).astype(np.uint8) - encoded_jpeg = tf.image.encode_jpeg(image_tensor) - encoded_additional_channels_jpeg = tf.image.encode_jpeg( - additional_channels_tensor) - - return encoded_jpeg, encoded_additional_channels_jpeg - - encoded_jpeg, encoded_additional_channels_jpeg = self.execute( - dummy_jpeg_fn, []) - - tmp_dir = self.get_temp_dir() - flat_mask = (4 * 5) * [1.0] - - for i in range(num_shards): - path = os.path.join(tmp_dir, '%05d.tfrecord' % i) - writer = tf.python_io.TFRecordWriter(path) - - for j in range(num_examples_per_shard): - if num_shards > 1: - source_id = (str(i) + '_' + str(j)).encode() - else: - source_id = str(j).encode() - - features = { - 'image/source_id': dataset_util.bytes_feature(source_id), - 'image/encoded': dataset_util.bytes_feature(encoded_jpeg), - 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/height': dataset_util.int64_feature(4), - 'image/width': dataset_util.int64_feature(5), - 'image/object/bbox/xmin': dataset_util.float_list_feature([0.0]), - 'image/object/bbox/xmax': dataset_util.float_list_feature([1.0]), - 'image/object/bbox/ymin': dataset_util.float_list_feature([0.0]), - 'image/object/bbox/ymax': dataset_util.float_list_feature([1.0]), - 'image/object/class/label': dataset_util.int64_list_feature([2]), - 'image/object/mask': dataset_util.float_list_feature(flat_mask), - } - - if has_additional_channels: - additional_channels_key = 'image/additional_channels/encoded' - features[additional_channels_key] = dataset_util.bytes_list_feature( - [encoded_additional_channels_jpeg] * 2) - - example = tf.train.Example(features=tf.train.Features(feature=features)) - writer.write(example.SerializeToString()) - - writer.close() - - return os.path.join(self.get_temp_dir(), '?????.tfrecord') - - def _make_random_serialized_jpeg_images(self, num_frames, image_height, - image_width): - def graph_fn(): - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - images_list = tf.unstack(images, axis=0) - encoded_images_list = [tf.io.encode_jpeg(image) for image in images_list] - return encoded_images_list - - encoded_images = self.execute(graph_fn, []) - return encoded_images - - def create_tf_record_sequence_example(self): - path = os.path.join(self.get_temp_dir(), 'seq_tfrecord') - writer = tf.python_io.TFRecordWriter(path) - - num_frames = 4 - image_height = 4 - image_width = 5 - image_source_ids = [str(i) for i in range(num_frames)] - with self.test_session(): - encoded_images = self._make_random_serialized_jpeg_images( - num_frames, image_height, image_width) - sequence_example_serialized = seq_example_util.make_sequence_example( - dataset_name='video_dataset', - video_id='video', - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_source_ids=image_source_ids, - image_format='JPEG', - is_annotated=[[1], [1], [1], [1]], - bboxes=[ - [[]], # Frame 0. - [[0., 0., 1., 1.]], # Frame 1. - [[0., 0., 1., 1.], - [0.1, 0.1, 0.2, 0.2]], # Frame 2. - [[]], # Frame 3. - ], - label_strings=[ - [], # Frame 0. - ['Abyssinian'], # Frame 1. - ['Abyssinian', 'american_bulldog'], # Frame 2. - [], # Frame 3 - ]).SerializeToString() - writer.write(sequence_example_serialized) - writer.close() - return path - - def test_build_tf_record_input_reader(self): - tf_record_path = self.create_tf_record() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - def graph_fn(): - return get_iterator_next_for_testing( - dataset_builder.build(input_reader_proto, batch_size=1), - self.is_tf2()) - - output_dict = self.execute(graph_fn, []) - - self.assertNotIn( - fields.InputDataFields.groundtruth_instance_masks, output_dict) - self.assertEqual((1, 4, 5, 3), - output_dict[fields.InputDataFields.image].shape) - self.assertAllEqual([[2]], - output_dict[fields.InputDataFields.groundtruth_classes]) - self.assertEqual( - (1, 1, 4), output_dict[fields.InputDataFields.groundtruth_boxes].shape) - self.assertAllEqual( - [0.0, 0.0, 1.0, 1.0], - output_dict[fields.InputDataFields.groundtruth_boxes][0][0]) - - def get_mock_reduce_to_frame_fn(self): - def mock_reduce_to_frame_fn(dataset, dataset_map_fn, batch_size, config): - def get_frame(tensor_dict): - out_tensor_dict = {} - out_tensor_dict[fields.InputDataFields.source_id] = ( - tensor_dict[fields.InputDataFields.source_id][0]) - return out_tensor_dict - return dataset_map_fn(dataset, get_frame, batch_size, config) - return mock_reduce_to_frame_fn - - def test_build_tf_record_input_reader_sequence_example_train(self): - tf_record_path = self.create_tf_record_sequence_example() - label_map_path = _get_labelmap_path() - input_type = 'TF_SEQUENCE_EXAMPLE' - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - input_type: {1} - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path, input_type) - input_reader_proto = input_reader_pb2.InputReader() - input_reader_proto.label_map_path = label_map_path - text_format.Merge(input_reader_text_proto, input_reader_proto) - reduce_to_frame_fn = self.get_mock_reduce_to_frame_fn() - - def graph_fn(): - return get_iterator_next_for_testing( - dataset_builder.build(input_reader_proto, batch_size=1, - reduce_to_frame_fn=reduce_to_frame_fn), - self.is_tf2()) - - output_dict = self.execute(graph_fn, []) - - self.assertEqual((1,), - output_dict[fields.InputDataFields.source_id].shape) - - def test_build_tf_record_input_reader_sequence_example_test(self): - tf_record_path = self.create_tf_record_sequence_example() - input_type = 'TF_SEQUENCE_EXAMPLE' - label_map_path = _get_labelmap_path() - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - input_type: {1} - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path, input_type) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - input_reader_proto.label_map_path = label_map_path - reduce_to_frame_fn = self.get_mock_reduce_to_frame_fn() - def graph_fn(): - return get_iterator_next_for_testing( - dataset_builder.build(input_reader_proto, batch_size=1, - reduce_to_frame_fn=reduce_to_frame_fn), - self.is_tf2()) - - output_dict = self.execute(graph_fn, []) - - self.assertEqual((1,), - output_dict[fields.InputDataFields.source_id].shape) - - def test_build_tf_record_input_reader_and_load_instance_masks(self): - tf_record_path = self.create_tf_record() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - load_instance_masks: true - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - def graph_fn(): - return get_iterator_next_for_testing( - dataset_builder.build(input_reader_proto, batch_size=1), - self.is_tf2() - ) - - output_dict = self.execute(graph_fn, []) - self.assertAllEqual( - (1, 1, 4, 5), - output_dict[fields.InputDataFields.groundtruth_instance_masks].shape) - - def test_build_tf_record_input_reader_with_batch_size_two(self): - tf_record_path = self.create_tf_record() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - def one_hot_class_encoding_fn(tensor_dict): - tensor_dict[fields.InputDataFields.groundtruth_classes] = tf.one_hot( - tensor_dict[fields.InputDataFields.groundtruth_classes] - 1, depth=3) - return tensor_dict - - def graph_fn(): - return dataset_builder.make_initializable_iterator( - dataset_builder.build( - input_reader_proto, - transform_input_data_fn=one_hot_class_encoding_fn, - batch_size=2)).get_next() - - output_dict = self.execute(graph_fn, []) - - self.assertAllEqual([2, 4, 5, 3], - output_dict[fields.InputDataFields.image].shape) - self.assertAllEqual( - [2, 1, 3], - output_dict[fields.InputDataFields.groundtruth_classes].shape) - self.assertAllEqual( - [2, 1, 4], output_dict[fields.InputDataFields.groundtruth_boxes].shape) - self.assertAllEqual([[[0.0, 0.0, 1.0, 1.0]], [[0.0, 0.0, 1.0, 1.0]]], - output_dict[fields.InputDataFields.groundtruth_boxes]) - - def test_build_tf_record_input_reader_with_batch_size_two_and_masks(self): - tf_record_path = self.create_tf_record() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - load_instance_masks: true - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - def one_hot_class_encoding_fn(tensor_dict): - tensor_dict[fields.InputDataFields.groundtruth_classes] = tf.one_hot( - tensor_dict[fields.InputDataFields.groundtruth_classes] - 1, depth=3) - return tensor_dict - - def graph_fn(): - return dataset_builder.make_initializable_iterator( - dataset_builder.build( - input_reader_proto, - transform_input_data_fn=one_hot_class_encoding_fn, - batch_size=2)).get_next() - - output_dict = self.execute(graph_fn, []) - - self.assertAllEqual( - [2, 1, 4, 5], - output_dict[fields.InputDataFields.groundtruth_instance_masks].shape) - - def test_raises_error_with_no_input_paths(self): - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - load_instance_masks: true - """ - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - with self.assertRaises(ValueError): - dataset_builder.build(input_reader_proto, batch_size=1) - - def test_sample_all_data(self): - tf_record_path = self.create_tf_record(num_examples_per_shard=2) - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - sample_1_of_n_examples: 1 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - def graph_fn(): - dataset = dataset_builder.build(input_reader_proto, batch_size=1) - sample1_ds = dataset.take(1) - sample2_ds = dataset.skip(1) - iter1 = dataset_builder.make_initializable_iterator(sample1_ds) - iter2 = dataset_builder.make_initializable_iterator(sample2_ds) - - return iter1.get_next(), iter2.get_next() - - output_dict1, output_dict2 = self.execute(graph_fn, []) - self.assertAllEqual([b'0'], output_dict1[fields.InputDataFields.source_id]) - self.assertEqual([b'1'], output_dict2[fields.InputDataFields.source_id]) - - def test_sample_one_of_n_shards(self): - tf_record_path = self.create_tf_record(num_examples_per_shard=4) - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - sample_1_of_n_examples: 2 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - def graph_fn(): - dataset = dataset_builder.build(input_reader_proto, batch_size=1) - sample1_ds = dataset.take(1) - sample2_ds = dataset.skip(1) - iter1 = dataset_builder.make_initializable_iterator(sample1_ds) - iter2 = dataset_builder.make_initializable_iterator(sample2_ds) - - return iter1.get_next(), iter2.get_next() - - output_dict1, output_dict2 = self.execute(graph_fn, []) - self.assertAllEqual([b'0'], output_dict1[fields.InputDataFields.source_id]) - self.assertEqual([b'2'], output_dict2[fields.InputDataFields.source_id]) - - def test_no_input_context(self): - """Test that all samples are read with no input context given.""" - tf_record_path = self.create_tf_record(num_examples_per_shard=16, - num_shards=2) - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - num_epochs: 1 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - for i in range(4): - - # pylint:disable=cell-var-from-loop - def graph_fn(): - dataset = dataset_builder.build(input_reader_proto, batch_size=8) - dataset = dataset.skip(i) - return get_iterator_next_for_testing(dataset, self.is_tf2()) - - batch = self.execute(graph_fn, []) - self.assertEqual(batch['image'].shape, (8, 4, 5, 3)) - - def graph_fn_last_batch(): - dataset = dataset_builder.build(input_reader_proto, batch_size=8) - dataset = dataset.skip(4) - return get_iterator_next_for_testing(dataset, self.is_tf2()) - - self.assertRaises(tf.errors.OutOfRangeError, self.execute, - compute_fn=graph_fn_last_batch, inputs=[]) - - def test_with_input_context(self): - """Test that a subset is read with input context given.""" - tf_record_path = self.create_tf_record(num_examples_per_shard=16, - num_shards=2) - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - num_epochs: 1 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - - input_context = tf.distribute.InputContext( - num_input_pipelines=2, input_pipeline_id=0, num_replicas_in_sync=4 - ) - - for i in range(8): - - # pylint:disable=cell-var-from-loop - def graph_fn(): - - dataset = dataset_builder.build(input_reader_proto, batch_size=8, - input_context=input_context) - dataset = dataset.skip(i) - return get_iterator_next_for_testing(dataset, self.is_tf2()) - - batch = self.execute(graph_fn, []) - self.assertEqual(batch['image'].shape, (2, 4, 5, 3)) - - def graph_fn_last_batch(): - dataset = dataset_builder.build(input_reader_proto, batch_size=8, - input_context=input_context) - dataset = dataset.skip(8) - return get_iterator_next_for_testing(dataset, self.is_tf2()) - - self.assertRaises(tf.errors.OutOfRangeError, self.execute, - compute_fn=graph_fn_last_batch, inputs=[]) - - -class ReadDatasetTest(test_case.TestCase): - - def setUp(self): - self._path_template = os.path.join(self.get_temp_dir(), 'examples_%s.txt') - - for i in range(5): - path = self._path_template % i - with tf.gfile.Open(path, 'wb') as f: - f.write('\n'.join([str(i + 1), str((i + 1) * 10)])) - - self._shuffle_path_template = os.path.join(self.get_temp_dir(), - 'shuffle_%s.txt') - for i in range(2): - path = self._shuffle_path_template % i - with tf.gfile.Open(path, 'wb') as f: - f.write('\n'.join([str(i)] * 5)) - - super(ReadDatasetTest, self).setUp() - - def _get_dataset_next(self, files, config, batch_size, num_batches_skip=0): - - def decode_func(value): - return [tf.string_to_number(value, out_type=tf.int32)] - - dataset = dataset_builder.read_dataset(tf.data.TextLineDataset, files, - config) - dataset = dataset.map(decode_func) - dataset = dataset.batch(batch_size) - - if num_batches_skip > 0: - dataset = dataset.skip(num_batches_skip) - - return get_iterator_next_for_testing(dataset, self.is_tf2()) - - def _assert_item_count(self, data, item, percentage): - self.assertAlmostEqual(data.count(item)/len(data), percentage, places=1) - - def test_make_initializable_iterator_with_hashTable(self): - - def graph_fn(): - keys = [1, 0, -1] - dataset = tf.data.Dataset.from_tensor_slices([[1, 2, -1, 5]]) - try: - # Dynamically try to load the tf v2 lookup, falling back to contrib - lookup = tf.compat.v2.lookup - hash_table_class = tf.compat.v2.lookup.StaticHashTable - except AttributeError: - lookup = contrib_lookup - hash_table_class = contrib_lookup.HashTable - table = hash_table_class( - initializer=lookup.KeyValueTensorInitializer( - keys=keys, values=list(reversed(keys))), - default_value=100) - dataset = dataset.map(table.lookup) - return dataset_builder.make_initializable_iterator(dataset).get_next() - - result = self.execute(graph_fn, []) - self.assertAllEqual(result, [-1, 100, 1, 100]) - - def test_read_dataset_sample_from_datasets_weights_equal_weight(self): - """Ensure that the files' values are equally-weighted.""" - config = input_reader_pb2.InputReader() - config.num_readers = 2 - config.shuffle = False - config.sample_from_datasets_weights.extend([0.5, 0.5]) - - def graph_fn(): - return self._get_dataset_next( - [self._path_template % '0', self._path_template % '1'], - config, - batch_size=1000) - - data = list(self.execute(graph_fn, [])) - self.assertEqual(len(data), 1000) - self._assert_item_count(data, 1, 0.25) - self._assert_item_count(data, 10, 0.25) - self._assert_item_count(data, 2, 0.25) - self._assert_item_count(data, 20, 0.25) - - def test_read_dataset_sample_from_datasets_weights_non_normalized(self): - """Ensure that the values are equally-weighted when not normalized.""" - config = input_reader_pb2.InputReader() - config.num_readers = 2 - config.shuffle = False - # Values are not normalized to sum to 1. In this case, it's a 50/50 split - # with each dataset having weight of 1. - config.sample_from_datasets_weights.extend([1, 1]) - - def graph_fn(): - return self._get_dataset_next( - [self._path_template % '0', self._path_template % '1'], - config, - batch_size=1000) - - data = list(self.execute(graph_fn, [])) - self.assertEqual(len(data), 1000) - self._assert_item_count(data, 1, 0.25) - self._assert_item_count(data, 10, 0.25) - self._assert_item_count(data, 2, 0.25) - self._assert_item_count(data, 20, 0.25) - - def test_read_dataset_sample_from_datasets_weights_zero_weight(self): - """Ensure that the files' values are equally-weighted.""" - config = input_reader_pb2.InputReader() - config.num_readers = 2 - config.shuffle = False - config.sample_from_datasets_weights.extend([1.0, 0.0]) - - def graph_fn(): - return self._get_dataset_next( - [self._path_template % '0', self._path_template % '1'], - config, - batch_size=1000) - - data = list(self.execute(graph_fn, [])) - self.assertEqual(len(data), 1000) - self._assert_item_count(data, 1, 0.5) - self._assert_item_count(data, 10, 0.5) - self._assert_item_count(data, 2, 0.0) - self._assert_item_count(data, 20, 0.0) - - def test_read_dataset_sample_from_datasets_weights_unbalanced(self): - """Ensure that the files' values are equally-weighted.""" - config = input_reader_pb2.InputReader() - config.num_readers = 2 - config.shuffle = False - config.sample_from_datasets_weights.extend([0.1, 0.9]) - - def graph_fn(): - return self._get_dataset_next( - [self._path_template % '0', self._path_template % '1'], - config, - batch_size=1000) - - data = list(self.execute(graph_fn, [])) - self.assertEqual(len(data), 1000) - self._assert_item_count(data, 1, 0.05) - self._assert_item_count(data, 10, 0.05) - self._assert_item_count(data, 2, 0.45) - self._assert_item_count(data, 20, 0.45) - - def test_read_dataset(self): - config = input_reader_pb2.InputReader() - config.num_readers = 1 - config.shuffle = False - - def graph_fn(): - return self._get_dataset_next( - [self._path_template % '*'], config, batch_size=20) - - data = self.execute(graph_fn, []) - # Note that the execute function extracts single outputs if the return - # value is of size 1. - self.assertCountEqual( - data, [ - 1, 10, 2, 20, 3, 30, 4, 40, 5, 50, 1, 10, 2, 20, 3, 30, 4, 40, 5, - 50 - ]) - - def test_reduce_num_reader(self): - config = input_reader_pb2.InputReader() - config.num_readers = 10 - config.shuffle = False - - def graph_fn(): - return self._get_dataset_next( - [self._path_template % '*'], config, batch_size=20) - - data = self.execute(graph_fn, []) - # Note that the execute function extracts single outputs if the return - # value is of size 1. - self.assertCountEqual( - data, [ - 1, 10, 2, 20, 3, 30, 4, 40, 5, 50, 1, 10, 2, 20, 3, 30, 4, 40, 5, - 50 - ]) - - def test_enable_shuffle(self): - config = input_reader_pb2.InputReader() - config.num_readers = 1 - config.shuffle = True - - tf.set_random_seed(1) # Set graph level seed. - - def graph_fn(): - return self._get_dataset_next( - [self._shuffle_path_template % '*'], config, batch_size=10) - expected_non_shuffle_output = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] - data = self.execute(graph_fn, []) - - self.assertTrue( - np.any(np.not_equal(data, expected_non_shuffle_output))) - - def test_disable_shuffle_(self): - config = input_reader_pb2.InputReader() - config.num_readers = 1 - config.shuffle = False - - def graph_fn(): - return self._get_dataset_next( - [self._shuffle_path_template % '*'], config, batch_size=10) - expected_non_shuffle_output1 = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1] - expected_non_shuffle_output2 = [1, 1, 1, 1, 1, 0, 0, 0, 0, 0] - - # Note that the execute function extracts single outputs if the return - # value is of size 1. - data = self.execute(graph_fn, []) - self.assertTrue(all(data == expected_non_shuffle_output1) or - all(data == expected_non_shuffle_output2)) - - def test_read_dataset_single_epoch(self): - config = input_reader_pb2.InputReader() - config.num_epochs = 1 - config.num_readers = 1 - config.shuffle = False - - def graph_fn(): - return self._get_dataset_next( - [self._path_template % '0'], config, batch_size=30) - - data = self.execute(graph_fn, []) - - # Note that the execute function extracts single outputs if the return - # value is of size 1. - self.assertAllEqual(data, [1, 10]) - - # First batch will retrieve as much as it can, second batch will fail. - def graph_fn_second_batch(): - return self._get_dataset_next( - [self._path_template % '0'], config, batch_size=30, - num_batches_skip=1) - - self.assertRaises(tf.errors.OutOfRangeError, self.execute, - compute_fn=graph_fn_second_batch, inputs=[]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/decoder_builder.py b/research/object_detection/builders/decoder_builder.py deleted file mode 100644 index 6fb6201d71b..00000000000 --- a/research/object_detection/builders/decoder_builder.py +++ /dev/null @@ -1,74 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""DataDecoder builder. - -Creates DataDecoders from InputReader configs. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from object_detection.data_decoders import tf_example_decoder -from object_detection.data_decoders import tf_sequence_example_decoder -from object_detection.protos import input_reader_pb2 - - -def build(input_reader_config): - """Builds a DataDecoder based only on the open source config proto. - - Args: - input_reader_config: An input_reader_pb2.InputReader object. - - Returns: - A DataDecoder based on the input_reader_config. - - Raises: - ValueError: On invalid input reader proto. - """ - if not isinstance(input_reader_config, input_reader_pb2.InputReader): - raise ValueError('input_reader_config not of type ' - 'input_reader_pb2.InputReader.') - - if input_reader_config.WhichOneof('input_reader') == 'tf_record_input_reader': - label_map_proto_file = None - if input_reader_config.HasField('label_map_path'): - label_map_proto_file = input_reader_config.label_map_path - input_type = input_reader_config.input_type - if input_type == input_reader_pb2.InputType.Value('TF_EXAMPLE'): - decoder = tf_example_decoder.TfExampleDecoder( - load_instance_masks=input_reader_config.load_instance_masks, - load_multiclass_scores=input_reader_config.load_multiclass_scores, - load_context_features=input_reader_config.load_context_features, - instance_mask_type=input_reader_config.mask_type, - label_map_proto_file=label_map_proto_file, - use_display_name=input_reader_config.use_display_name, - num_additional_channels=input_reader_config.num_additional_channels, - num_keypoints=input_reader_config.num_keypoints, - expand_hierarchy_labels=input_reader_config.expand_labels_hierarchy, - load_dense_pose=input_reader_config.load_dense_pose, - load_track_id=input_reader_config.load_track_id, - load_keypoint_depth_features=input_reader_config - .load_keypoint_depth_features) - return decoder - elif input_type == input_reader_pb2.InputType.Value('TF_SEQUENCE_EXAMPLE'): - decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder( - label_map_proto_file=label_map_proto_file, - load_context_features=input_reader_config.load_context_features, - load_context_image_ids=input_reader_config.load_context_image_ids) - return decoder - raise ValueError('Unsupported input_type in config.') - - raise ValueError('Unsupported input_reader_config.') diff --git a/research/object_detection/builders/decoder_builder_test.py b/research/object_detection/builders/decoder_builder_test.py deleted file mode 100644 index 886a41b5666..00000000000 --- a/research/object_detection/builders/decoder_builder_test.py +++ /dev/null @@ -1,216 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for decoder_builder.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import decoder_builder -from object_detection.core import standard_fields as fields -from object_detection.dataset_tools import seq_example_util -from object_detection.protos import input_reader_pb2 -from object_detection.utils import dataset_util -from object_detection.utils import test_case - - -def _get_labelmap_path(): - """Returns an absolute path to label map file.""" - parent_path = os.path.dirname(tf.resource_loader.get_data_files_path()) - return os.path.join(parent_path, 'data', - 'pet_label_map.pbtxt') - - -class DecoderBuilderTest(test_case.TestCase): - - def _make_serialized_tf_example(self, has_additional_channels=False): - image_tensor_np = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8) - additional_channels_tensor_np = np.random.randint( - 255, size=(4, 5, 1)).astype(np.uint8) - flat_mask = (4 * 5) * [1.0] - def graph_fn(image_tensor): - encoded_jpeg = tf.image.encode_jpeg(image_tensor) - return encoded_jpeg - encoded_jpeg = self.execute_cpu(graph_fn, [image_tensor_np]) - encoded_additional_channels_jpeg = self.execute_cpu( - graph_fn, [additional_channels_tensor_np]) - - features = { - 'image/source_id': dataset_util.bytes_feature('0'.encode()), - 'image/encoded': dataset_util.bytes_feature(encoded_jpeg), - 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/height': dataset_util.int64_feature(4), - 'image/width': dataset_util.int64_feature(5), - 'image/object/bbox/xmin': dataset_util.float_list_feature([0.0]), - 'image/object/bbox/xmax': dataset_util.float_list_feature([1.0]), - 'image/object/bbox/ymin': dataset_util.float_list_feature([0.0]), - 'image/object/bbox/ymax': dataset_util.float_list_feature([1.0]), - 'image/object/class/label': dataset_util.int64_list_feature([2]), - 'image/object/mask': dataset_util.float_list_feature(flat_mask), - 'image/object/keypoint/x': dataset_util.float_list_feature([1.0, 1.0]), - 'image/object/keypoint/y': dataset_util.float_list_feature([1.0, 1.0]) - } - if has_additional_channels: - additional_channels_key = 'image/additional_channels/encoded' - features[additional_channels_key] = dataset_util.bytes_list_feature( - [encoded_additional_channels_jpeg] * 2) - example = tf.train.Example(features=tf.train.Features(feature=features)) - return example.SerializeToString() - - def _make_random_serialized_jpeg_images(self, num_frames, image_height, - image_width): - def graph_fn(): - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - images_list = tf.unstack(images, axis=0) - encoded_images = [tf.io.encode_jpeg(image) for image in images_list] - return encoded_images - return self.execute_cpu(graph_fn, []) - - def _make_serialized_tf_sequence_example(self): - num_frames = 4 - image_height = 20 - image_width = 30 - image_source_ids = [str(i) for i in range(num_frames)] - encoded_images = self._make_random_serialized_jpeg_images( - num_frames, image_height, image_width) - sequence_example_serialized = seq_example_util.make_sequence_example( - dataset_name='video_dataset', - video_id='video', - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_source_ids=image_source_ids, - image_format='JPEG', - is_annotated=[[1], [1], [1], [1]], - bboxes=[ - [[]], # Frame 0. - [[0., 0., 1., 1.]], # Frame 1. - [[0., 0., 1., 1.], - [0.1, 0.1, 0.2, 0.2]], # Frame 2. - [[]], # Frame 3. - ], - label_strings=[ - [], # Frame 0. - ['Abyssinian'], # Frame 1. - ['Abyssinian', 'american_bulldog'], # Frame 2. - [], # Frame 3 - ]).SerializeToString() - return sequence_example_serialized - - def test_build_tf_record_input_reader(self): - input_reader_text_proto = 'tf_record_input_reader {}' - input_reader_proto = input_reader_pb2.InputReader() - text_format.Parse(input_reader_text_proto, input_reader_proto) - - decoder = decoder_builder.build(input_reader_proto) - serialized_seq_example = self._make_serialized_tf_example() - def graph_fn(): - tensor_dict = decoder.decode(serialized_seq_example) - return (tensor_dict[fields.InputDataFields.image], - tensor_dict[fields.InputDataFields.groundtruth_classes], - tensor_dict[fields.InputDataFields.groundtruth_boxes]) - - (image, groundtruth_classes, - groundtruth_boxes) = self.execute_cpu(graph_fn, []) - self.assertEqual((4, 5, 3), image.shape) - self.assertAllEqual([2], groundtruth_classes) - self.assertEqual((1, 4), groundtruth_boxes.shape) - self.assertAllEqual([0.0, 0.0, 1.0, 1.0], groundtruth_boxes[0]) - - def test_build_tf_record_input_reader_sequence_example(self): - label_map_path = _get_labelmap_path() - input_reader_text_proto = """ - input_type: TF_SEQUENCE_EXAMPLE - tf_record_input_reader {} - """ - input_reader_proto = input_reader_pb2.InputReader() - input_reader_proto.label_map_path = label_map_path - text_format.Parse(input_reader_text_proto, input_reader_proto) - - serialized_seq_example = self._make_serialized_tf_sequence_example() - def graph_fn(): - decoder = decoder_builder.build(input_reader_proto) - tensor_dict = decoder.decode(serialized_seq_example) - return (tensor_dict[fields.InputDataFields.image], - tensor_dict[fields.InputDataFields.groundtruth_classes], - tensor_dict[fields.InputDataFields.groundtruth_boxes], - tensor_dict[fields.InputDataFields.num_groundtruth_boxes]) - (actual_image, actual_groundtruth_classes, actual_groundtruth_boxes, - actual_num_groundtruth_boxes) = self.execute_cpu(graph_fn, []) - expected_groundtruth_classes = [[-1, -1], [1, -1], [1, 2], [-1, -1]] - expected_groundtruth_boxes = [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 1.0, 1.0], [0.1, 0.1, 0.2, 0.2]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]] - expected_num_groundtruth_boxes = [0, 1, 2, 0] - - # Sequence example images are encoded. - self.assertEqual((4,), actual_image.shape) - self.assertAllEqual(expected_groundtruth_classes, - actual_groundtruth_classes) - self.assertAllClose(expected_groundtruth_boxes, - actual_groundtruth_boxes) - self.assertAllClose( - expected_num_groundtruth_boxes, actual_num_groundtruth_boxes) - - def test_build_tf_record_input_reader_and_load_instance_masks(self): - input_reader_text_proto = """ - load_instance_masks: true - tf_record_input_reader {} - """ - input_reader_proto = input_reader_pb2.InputReader() - text_format.Parse(input_reader_text_proto, input_reader_proto) - - decoder = decoder_builder.build(input_reader_proto) - serialized_seq_example = self._make_serialized_tf_example() - def graph_fn(): - tensor_dict = decoder.decode(serialized_seq_example) - return tensor_dict[fields.InputDataFields.groundtruth_instance_masks] - masks = self.execute_cpu(graph_fn, []) - self.assertAllEqual((1, 4, 5), masks.shape) - - def test_build_tf_record_input_reader_and_load_keypoint_depth(self): - input_reader_text_proto = """ - load_keypoint_depth_features: true - num_keypoints: 2 - tf_record_input_reader {} - """ - input_reader_proto = input_reader_pb2.InputReader() - text_format.Parse(input_reader_text_proto, input_reader_proto) - - decoder = decoder_builder.build(input_reader_proto) - serialized_example = self._make_serialized_tf_example() - - def graph_fn(): - tensor_dict = decoder.decode(serialized_example) - return (tensor_dict[fields.InputDataFields.groundtruth_keypoint_depths], - tensor_dict[ - fields.InputDataFields.groundtruth_keypoint_depth_weights]) - - (kpts_depths, kpts_depth_weights) = self.execute_cpu(graph_fn, []) - self.assertAllEqual((1, 2), kpts_depths.shape) - self.assertAllEqual((1, 2), kpts_depth_weights.shape) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/graph_rewriter_builder.py b/research/object_detection/builders/graph_rewriter_builder.py deleted file mode 100644 index 9cbeb4a1f68..00000000000 --- a/research/object_detection/builders/graph_rewriter_builder.py +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Functions for quantized training and evaluation.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim -# pylint: disable=g-import-not-at-top -try: - from tensorflow.contrib import quantize as contrib_quantize -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - - -def build(graph_rewriter_config, is_training): - """Returns a function that modifies default graph based on options. - - Args: - graph_rewriter_config: graph_rewriter_pb2.GraphRewriter proto. - is_training: whether in training of eval mode. - """ - def graph_rewrite_fn(): - """Function to quantize weights and activation of the default graph.""" - if (graph_rewriter_config.quantization.weight_bits != 8 or - graph_rewriter_config.quantization.activation_bits != 8): - raise ValueError('Only 8bit quantization is supported') - - # Quantize the graph by inserting quantize ops for weights and activations - if is_training: - contrib_quantize.experimental_create_training_graph( - input_graph=tf.get_default_graph(), - quant_delay=graph_rewriter_config.quantization.delay - ) - else: - contrib_quantize.experimental_create_eval_graph( - input_graph=tf.get_default_graph() - ) - slim.summarize_collection('quant_vars') - - return graph_rewrite_fn diff --git a/research/object_detection/builders/graph_rewriter_builder_tf1_test.py b/research/object_detection/builders/graph_rewriter_builder_tf1_test.py deleted file mode 100644 index bec3cf8348f..00000000000 --- a/research/object_detection/builders/graph_rewriter_builder_tf1_test.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for graph_rewriter_builder.""" -import unittest -from unittest import mock # pylint: disable=g-importing-member -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.builders import graph_rewriter_builder -from object_detection.protos import graph_rewriter_pb2 -from object_detection.utils import tf_version - - -if tf_version.is_tf1(): - from tensorflow.contrib import quantize as contrib_quantize # pylint: disable=g-import-not-at-top - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class QuantizationBuilderTest(tf.test.TestCase): - - def testQuantizationBuilderSetsUpCorrectTrainArguments(self): - with mock.patch.object( - contrib_quantize, - 'experimental_create_training_graph') as mock_quant_fn: - with mock.patch.object(slim, - 'summarize_collection') as mock_summarize_col: - graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_proto.quantization.delay = 10 - graph_rewriter_proto.quantization.weight_bits = 8 - graph_rewriter_proto.quantization.activation_bits = 8 - graph_rewrite_fn = graph_rewriter_builder.build( - graph_rewriter_proto, is_training=True) - graph_rewrite_fn() - _, kwargs = mock_quant_fn.call_args - self.assertEqual(kwargs['input_graph'], tf.get_default_graph()) - self.assertEqual(kwargs['quant_delay'], 10) - mock_summarize_col.assert_called_with('quant_vars') - - def testQuantizationBuilderSetsUpCorrectEvalArguments(self): - with mock.patch.object(contrib_quantize, - 'experimental_create_eval_graph') as mock_quant_fn: - with mock.patch.object(slim, - 'summarize_collection') as mock_summarize_col: - graph_rewriter_proto = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_proto.quantization.delay = 10 - graph_rewrite_fn = graph_rewriter_builder.build( - graph_rewriter_proto, is_training=False) - graph_rewrite_fn() - _, kwargs = mock_quant_fn.call_args - self.assertEqual(kwargs['input_graph'], tf.get_default_graph()) - mock_summarize_col.assert_called_with('quant_vars') - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/hyperparams_builder.py b/research/object_detection/builders/hyperparams_builder.py deleted file mode 100644 index 9fdf4450abd..00000000000 --- a/research/object_detection/builders/hyperparams_builder.py +++ /dev/null @@ -1,473 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Builder function to construct tf-slim arg_scope for convolution, fc ops.""" -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.core import freezable_batch_norm -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import context_manager -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top -if tf_version.is_tf2(): - from object_detection.core import freezable_sync_batch_norm -# pylint: enable=g-import-not-at-top - - -class KerasLayerHyperparams(object): - """ - A hyperparameter configuration object for Keras layers used in - Object Detection models. - """ - - def __init__(self, hyperparams_config): - """Builds keras hyperparameter config for layers based on the proto config. - - It automatically converts from Slim layer hyperparameter configs to - Keras layer hyperparameters. Namely, it: - - Builds Keras initializers/regularizers instead of Slim ones - - sets weights_regularizer/initializer to kernel_regularizer/initializer - - converts batchnorm decay to momentum - - converts Slim l2 regularizer weights to the equivalent Keras l2 weights - - Contains a hyperparameter configuration for ops that specifies kernel - initializer, kernel regularizer, activation. Also contains parameters for - batch norm operators based on the configuration. - - Note that if the batch_norm parameters are not specified in the config - (i.e. left to default) then batch norm is excluded from the config. - - Args: - hyperparams_config: hyperparams.proto object containing - hyperparameters. - - Raises: - ValueError: if hyperparams_config is not of type hyperparams.Hyperparams. - """ - if not isinstance(hyperparams_config, - hyperparams_pb2.Hyperparams): - raise ValueError('hyperparams_config not of type ' - 'hyperparams_pb.Hyperparams.') - - self._batch_norm_params = None - self._use_sync_batch_norm = False - if hyperparams_config.HasField('batch_norm'): - self._batch_norm_params = _build_keras_batch_norm_params( - hyperparams_config.batch_norm) - elif hyperparams_config.HasField('sync_batch_norm'): - self._use_sync_batch_norm = True - self._batch_norm_params = _build_keras_batch_norm_params( - hyperparams_config.sync_batch_norm) - - self._force_use_bias = hyperparams_config.force_use_bias - self._activation_fn = _build_activation_fn(hyperparams_config.activation) - # TODO(kaftan): Unclear if these kwargs apply to separable & depthwise conv - # (Those might use depthwise_* instead of kernel_*) - # We should probably switch to using build_conv2d_layer and - # build_depthwise_conv2d_layer methods instead. - self._op_params = { - 'kernel_regularizer': _build_keras_regularizer( - hyperparams_config.regularizer), - 'kernel_initializer': _build_initializer( - hyperparams_config.initializer, build_for_keras=True), - 'activation': _build_activation_fn(hyperparams_config.activation) - } - - def use_batch_norm(self): - return self._batch_norm_params is not None - - def use_sync_batch_norm(self): - return self._use_sync_batch_norm - - def force_use_bias(self): - return self._force_use_bias - - def use_bias(self): - return (self._force_use_bias or not - (self.use_batch_norm() and self.batch_norm_params()['center'])) - - def batch_norm_params(self, **overrides): - """Returns a dict containing batchnorm layer construction hyperparameters. - - Optionally overrides values in the batchnorm hyperparam dict. Overrides - only apply to individual calls of this method, and do not affect - future calls. - - Args: - **overrides: keyword arguments to override in the hyperparams dictionary - - Returns: dict containing the layer construction keyword arguments, with - values overridden by the `overrides` keyword arguments. - """ - if self._batch_norm_params is None: - new_batch_norm_params = dict() - else: - new_batch_norm_params = self._batch_norm_params.copy() - new_batch_norm_params.update(overrides) - return new_batch_norm_params - - def build_batch_norm(self, training=None, **overrides): - """Returns a Batch Normalization layer with the appropriate hyperparams. - - If the hyperparams are configured to not use batch normalization, - this will return a Keras Lambda layer that only applies tf.Identity, - without doing any normalization. - - Optionally overrides values in the batch_norm hyperparam dict. Overrides - only apply to individual calls of this method, and do not affect - future calls. - - Args: - training: if True, the normalization layer will normalize using the batch - statistics. If False, the normalization layer will be frozen and will - act as if it is being used for inference. If None, the layer - will look up the Keras learning phase at `call` time to decide what to - do. - **overrides: batch normalization construction args to override from the - batch_norm hyperparams dictionary. - - Returns: Either a FreezableBatchNorm layer (if use_batch_norm() is True), - or a Keras Lambda layer that applies the identity (if use_batch_norm() - is False) - """ - if self.use_batch_norm(): - if self._use_sync_batch_norm: - return freezable_sync_batch_norm.FreezableSyncBatchNorm( - training=training, **self.batch_norm_params(**overrides)) - else: - return freezable_batch_norm.FreezableBatchNorm( - training=training, **self.batch_norm_params(**overrides)) - else: - return tf.keras.layers.Lambda(tf.identity) - - def build_activation_layer(self, name='activation'): - """Returns a Keras layer that applies the desired activation function. - - Args: - name: The name to assign the Keras layer. - Returns: A Keras lambda layer that applies the activation function - specified in the hyperparam config, or applies the identity if the - activation function is None. - """ - if self._activation_fn: - return tf.keras.layers.Lambda(self._activation_fn, name=name) - else: - return tf.keras.layers.Lambda(tf.identity, name=name) - - def get_regularizer_weight(self): - """Returns the l1 or l2 regularizer weight. - - Returns: A float value corresponding to the l1 or l2 regularization weight, - or None if neither l1 or l2 regularization is defined. - """ - regularizer = self._op_params['kernel_regularizer'] - if hasattr(regularizer, 'l1'): - return float(regularizer.l1) - elif hasattr(regularizer, 'l2'): - return float(regularizer.l2) - else: - return None - - def params(self, include_activation=False, **overrides): - """Returns a dict containing the layer construction hyperparameters to use. - - Optionally overrides values in the returned dict. Overrides - only apply to individual calls of this method, and do not affect - future calls. - - Args: - include_activation: If False, activation in the returned dictionary will - be set to `None`, and the activation must be applied via a separate - layer created by `build_activation_layer`. If True, `activation` in the - output param dictionary will be set to the activation function - specified in the hyperparams config. - **overrides: keyword arguments to override in the hyperparams dictionary. - - Returns: dict containing the layer construction keyword arguments, with - values overridden by the `overrides` keyword arguments. - """ - new_params = self._op_params.copy() - new_params['activation'] = None - if include_activation: - new_params['activation'] = self._activation_fn - new_params['use_bias'] = self.use_bias() - new_params.update(**overrides) - return new_params - - -def build(hyperparams_config, is_training): - """Builds tf-slim arg_scope for convolution ops based on the config. - - Returns an arg_scope to use for convolution ops containing weights - initializer, weights regularizer, activation function, batch norm function - and batch norm parameters based on the configuration. - - Note that if no normalization parameters are specified in the config, - (i.e. left to default) then both batch norm and group norm are excluded - from the arg_scope. - - The batch norm parameters are set for updates based on `is_training` argument - and conv_hyperparams_config.batch_norm.train parameter. During training, they - are updated only if batch_norm.train parameter is true. However, during eval, - no updates are made to the batch norm variables. In both cases, their current - values are used during forward pass. - - Args: - hyperparams_config: hyperparams.proto object containing - hyperparameters. - is_training: Whether the network is in training mode. - - Returns: - arg_scope_fn: A function to construct tf-slim arg_scope containing - hyperparameters for ops. - - Raises: - ValueError: if hyperparams_config is not of type hyperparams.Hyperparams. - """ - if not isinstance(hyperparams_config, - hyperparams_pb2.Hyperparams): - raise ValueError('hyperparams_config not of type ' - 'hyperparams_pb.Hyperparams.') - - if hyperparams_config.force_use_bias: - raise ValueError('Hyperparams force_use_bias only supported by ' - 'KerasLayerHyperparams.') - - if hyperparams_config.HasField('sync_batch_norm'): - raise ValueError('Hyperparams sync_batch_norm only supported by ' - 'KerasLayerHyperparams.') - - normalizer_fn = None - batch_norm_params = None - if hyperparams_config.HasField('batch_norm'): - normalizer_fn = slim.batch_norm - batch_norm_params = _build_batch_norm_params( - hyperparams_config.batch_norm, is_training) - if hyperparams_config.HasField('group_norm'): - normalizer_fn = slim.group_norm - affected_ops = [slim.conv2d, slim.separable_conv2d, slim.conv2d_transpose] - if hyperparams_config.HasField('op') and ( - hyperparams_config.op == hyperparams_pb2.Hyperparams.FC): - affected_ops = [slim.fully_connected] - def scope_fn(): - with (slim.arg_scope([slim.batch_norm], **batch_norm_params) - if batch_norm_params is not None else - context_manager.IdentityContextManager()): - with slim.arg_scope( - affected_ops, - weights_regularizer=_build_slim_regularizer( - hyperparams_config.regularizer), - weights_initializer=_build_initializer( - hyperparams_config.initializer), - activation_fn=_build_activation_fn(hyperparams_config.activation), - normalizer_fn=normalizer_fn) as sc: - return sc - - return scope_fn - - -def _build_activation_fn(activation_fn): - """Builds a callable activation from config. - - Args: - activation_fn: hyperparams_pb2.Hyperparams.activation - - Returns: - Callable activation function. - - Raises: - ValueError: On unknown activation function. - """ - if activation_fn == hyperparams_pb2.Hyperparams.NONE: - return None - if activation_fn == hyperparams_pb2.Hyperparams.RELU: - return tf.nn.relu - if activation_fn == hyperparams_pb2.Hyperparams.RELU_6: - return tf.nn.relu6 - if activation_fn == hyperparams_pb2.Hyperparams.SWISH: - return tf.nn.swish - raise ValueError('Unknown activation function: {}'.format(activation_fn)) - - -def _build_slim_regularizer(regularizer): - """Builds a tf-slim regularizer from config. - - Args: - regularizer: hyperparams_pb2.Hyperparams.regularizer proto. - - Returns: - tf-slim regularizer. - - Raises: - ValueError: On unknown regularizer. - """ - regularizer_oneof = regularizer.WhichOneof('regularizer_oneof') - if regularizer_oneof == 'l1_regularizer': - return slim.l1_regularizer(scale=float(regularizer.l1_regularizer.weight)) - if regularizer_oneof == 'l2_regularizer': - return slim.l2_regularizer(scale=float(regularizer.l2_regularizer.weight)) - if regularizer_oneof is None: - return None - raise ValueError('Unknown regularizer function: {}'.format(regularizer_oneof)) - - -def _build_keras_regularizer(regularizer): - """Builds a keras regularizer from config. - - Args: - regularizer: hyperparams_pb2.Hyperparams.regularizer proto. - - Returns: - Keras regularizer. - - Raises: - ValueError: On unknown regularizer. - """ - regularizer_oneof = regularizer.WhichOneof('regularizer_oneof') - if regularizer_oneof == 'l1_regularizer': - return tf.keras.regularizers.l1(float(regularizer.l1_regularizer.weight)) - if regularizer_oneof == 'l2_regularizer': - # The Keras L2 regularizer weight differs from the Slim L2 regularizer - # weight by a factor of 2 - return tf.keras.regularizers.l2( - float(regularizer.l2_regularizer.weight * 0.5)) - if regularizer_oneof is None: - return None - raise ValueError('Unknown regularizer function: {}'.format(regularizer_oneof)) - - -def _build_initializer(initializer, build_for_keras=False): - """Build a tf initializer from config. - - Args: - initializer: hyperparams_pb2.Hyperparams.regularizer proto. - build_for_keras: Whether the initializers should be built for Keras - operators. If false builds for Slim. - - Returns: - tf initializer or string corresponding to the tf keras initializer name. - - Raises: - ValueError: On unknown initializer. - """ - initializer_oneof = initializer.WhichOneof('initializer_oneof') - if initializer_oneof == 'truncated_normal_initializer': - return tf.truncated_normal_initializer( - mean=initializer.truncated_normal_initializer.mean, - stddev=initializer.truncated_normal_initializer.stddev) - if initializer_oneof == 'random_normal_initializer': - return tf.random_normal_initializer( - mean=initializer.random_normal_initializer.mean, - stddev=initializer.random_normal_initializer.stddev) - if initializer_oneof == 'variance_scaling_initializer': - enum_descriptor = (hyperparams_pb2.VarianceScalingInitializer. - DESCRIPTOR.enum_types_by_name['Mode']) - mode = enum_descriptor.values_by_number[initializer. - variance_scaling_initializer. - mode].name - if build_for_keras: - if initializer.variance_scaling_initializer.uniform: - return tf.variance_scaling_initializer( - scale=initializer.variance_scaling_initializer.factor, - mode=mode.lower(), - distribution='uniform') - else: - # In TF 1.9 release and earlier, the truncated_normal distribution was - # not supported correctly. So, in these earlier versions of tensorflow, - # the ValueError will be raised, and we manually truncate the - # distribution scale. - # - # It is insufficient to just set distribution to `normal` from the - # start, because the `normal` distribution in newer Tensorflow versions - # creates a truncated distribution, whereas it created untruncated - # distributions in older versions. - try: - return tf.variance_scaling_initializer( - scale=initializer.variance_scaling_initializer.factor, - mode=mode.lower(), - distribution='truncated_normal') - except ValueError: - truncate_constant = 0.87962566103423978 - truncated_scale = initializer.variance_scaling_initializer.factor / ( - truncate_constant * truncate_constant - ) - return tf.variance_scaling_initializer( - scale=truncated_scale, - mode=mode.lower(), - distribution='normal') - - else: - return slim.variance_scaling_initializer( - factor=initializer.variance_scaling_initializer.factor, - mode=mode, - uniform=initializer.variance_scaling_initializer.uniform) - if initializer_oneof == 'keras_initializer_by_name': - if build_for_keras: - return initializer.keras_initializer_by_name - else: - raise ValueError( - 'Unsupported non-Keras usage of keras_initializer_by_name: {}'.format( - initializer.keras_initializer_by_name)) - if initializer_oneof is None: - return None - raise ValueError('Unknown initializer function: {}'.format( - initializer_oneof)) - - -def _build_batch_norm_params(batch_norm, is_training): - """Build a dictionary of batch_norm params from config. - - Args: - batch_norm: hyperparams_pb2.ConvHyperparams.batch_norm proto. - is_training: Whether the models is in training mode. - - Returns: - A dictionary containing batch_norm parameters. - """ - batch_norm_params = { - 'decay': batch_norm.decay, - 'center': batch_norm.center, - 'scale': batch_norm.scale, - 'epsilon': batch_norm.epsilon, - # Remove is_training parameter from here and deprecate it in the proto - # once we refactor Faster RCNN models to set is_training through an outer - # arg_scope in the meta architecture. - 'is_training': is_training and batch_norm.train, - } - return batch_norm_params - - -def _build_keras_batch_norm_params(batch_norm): - """Build a dictionary of Keras BatchNormalization params from config. - - Args: - batch_norm: hyperparams_pb2.ConvHyperparams.batch_norm proto. - - Returns: - A dictionary containing Keras BatchNormalization parameters. - """ - # Note: Although decay is defined to be 1 - momentum in batch_norm, - # decay in the slim batch_norm layers was erroneously defined and is - # actually the same as momentum in the Keras batch_norm layers. - # For context, see: github.com/keras-team/keras/issues/6839 - batch_norm_params = { - 'momentum': batch_norm.decay, - 'center': batch_norm.center, - 'scale': batch_norm.scale, - 'epsilon': batch_norm.epsilon, - } - return batch_norm_params diff --git a/research/object_detection/builders/hyperparams_builder_test.py b/research/object_detection/builders/hyperparams_builder_test.py deleted file mode 100644 index 3bf4a258b6d..00000000000 --- a/research/object_detection/builders/hyperparams_builder_test.py +++ /dev/null @@ -1,1054 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests object_detection.core.hyperparams_builder.""" - -import unittest -import numpy as np -import tensorflow.compat.v1 as tf -import tf_slim as slim -from google.protobuf import text_format - -from object_detection.builders import hyperparams_builder -from object_detection.core import freezable_batch_norm -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import tf_version - - -def _get_scope_key(op): - return getattr(op, '_key_op', str(op)) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only tests.') -class HyperparamsBuilderTest(tf.test.TestCase): - - def test_default_arg_scope_has_conv2d_op(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - self.assertIn(_get_scope_key(slim.conv2d), scope) - - def test_default_arg_scope_has_separable_conv2d_op(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - self.assertIn(_get_scope_key(slim.separable_conv2d), scope) - - def test_default_arg_scope_has_conv2d_transpose_op(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - self.assertIn(_get_scope_key(slim.conv2d_transpose), scope) - - def test_explicit_fc_op_arg_scope_has_fully_connected_op(self): - conv_hyperparams_text_proto = """ - op: FC - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - self.assertIn(_get_scope_key(slim.fully_connected), scope) - - def test_separable_conv2d_and_conv2d_and_transpose_have_same_parameters(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - kwargs_1, kwargs_2, kwargs_3 = scope.values() - self.assertDictEqual(kwargs_1, kwargs_2) - self.assertDictEqual(kwargs_1, kwargs_3) - - def test_return_l1_regularized_weights(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - weight: 0.5 - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = list(scope.values())[0] - regularizer = conv_scope_arguments['weights_regularizer'] - weights = np.array([1., -1, 4., 2.]) - with self.test_session() as sess: - result = sess.run(regularizer(tf.constant(weights))) - self.assertAllClose(np.abs(weights).sum() * 0.5, result) - - def test_return_l2_regularizer_weights(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - weight: 0.42 - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - - regularizer = conv_scope_arguments['weights_regularizer'] - weights = np.array([1., -1, 4., 2.]) - with self.test_session() as sess: - result = sess.run(regularizer(tf.constant(weights))) - self.assertAllClose(np.power(weights, 2).sum() / 2.0 * 0.42, result) - - def test_return_non_default_batch_norm_params_with_train_during_train(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - decay: 0.7 - center: false - scale: true - epsilon: 0.03 - train: true - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['normalizer_fn'], slim.batch_norm) - batch_norm_params = scope[_get_scope_key(slim.batch_norm)] - self.assertAlmostEqual(batch_norm_params['decay'], 0.7) - self.assertAlmostEqual(batch_norm_params['epsilon'], 0.03) - self.assertFalse(batch_norm_params['center']) - self.assertTrue(batch_norm_params['scale']) - self.assertTrue(batch_norm_params['is_training']) - - def test_return_batch_norm_params_with_notrain_during_eval(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - decay: 0.7 - center: false - scale: true - epsilon: 0.03 - train: true - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=False) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['normalizer_fn'], slim.batch_norm) - batch_norm_params = scope[_get_scope_key(slim.batch_norm)] - self.assertAlmostEqual(batch_norm_params['decay'], 0.7) - self.assertAlmostEqual(batch_norm_params['epsilon'], 0.03) - self.assertFalse(batch_norm_params['center']) - self.assertTrue(batch_norm_params['scale']) - self.assertFalse(batch_norm_params['is_training']) - - def test_return_batch_norm_params_with_notrain_when_train_is_false(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - decay: 0.7 - center: false - scale: true - epsilon: 0.03 - train: false - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['normalizer_fn'], slim.batch_norm) - batch_norm_params = scope[_get_scope_key(slim.batch_norm)] - self.assertAlmostEqual(batch_norm_params['decay'], 0.7) - self.assertAlmostEqual(batch_norm_params['epsilon'], 0.03) - self.assertFalse(batch_norm_params['center']) - self.assertTrue(batch_norm_params['scale']) - self.assertFalse(batch_norm_params['is_training']) - - def test_do_not_use_batch_norm_if_default(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['normalizer_fn'], None) - - def test_use_none_activation(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: NONE - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['activation_fn'], None) - - def test_use_relu_activation(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['activation_fn'], tf.nn.relu) - - def test_use_relu_6_activation(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU_6 - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['activation_fn'], tf.nn.relu6) - - def test_use_swish_activation(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: SWISH - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - self.assertEqual(conv_scope_arguments['activation_fn'], tf.nn.swish) - - def _assert_variance_in_range(self, initializer, shape, variance, - tol=1e-2): - with tf.Graph().as_default() as g: - with self.test_session(graph=g) as sess: - var = tf.get_variable( - name='test', - shape=shape, - dtype=tf.float32, - initializer=initializer) - sess.run(tf.global_variables_initializer()) - values = sess.run(var) - self.assertAllClose(np.var(values), variance, tol, tol) - - def test_variance_in_range_with_variance_scaling_initializer_fan_in(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_IN - uniform: false - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - initializer = conv_scope_arguments['weights_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=2. / 100.) - - def test_variance_in_range_with_variance_scaling_initializer_fan_out(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_OUT - uniform: false - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - initializer = conv_scope_arguments['weights_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=2. / 40.) - - def test_variance_in_range_with_variance_scaling_initializer_fan_avg(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_AVG - uniform: false - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - initializer = conv_scope_arguments['weights_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=4. / (100. + 40.)) - - def test_variance_in_range_with_variance_scaling_initializer_uniform(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_IN - uniform: true - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - initializer = conv_scope_arguments['weights_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=2. / 100.) - - def test_variance_in_range_with_truncated_normal_initializer(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - mean: 0.0 - stddev: 0.8 - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - initializer = conv_scope_arguments['weights_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=0.49, tol=1e-1) - - def test_variance_in_range_with_random_normal_initializer(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - random_normal_initializer { - mean: 0.0 - stddev: 0.8 - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - scope_fn = hyperparams_builder.build(conv_hyperparams_proto, - is_training=True) - scope = scope_fn() - conv_scope_arguments = scope[_get_scope_key(slim.conv2d)] - initializer = conv_scope_arguments['weights_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=0.64, tol=1e-1) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only tests.') -class KerasHyperparamsBuilderTest(tf.test.TestCase): - - def _assert_variance_in_range(self, initializer, shape, variance, - tol=1e-2): - var = tf.Variable(initializer(shape=shape, dtype=tf.float32)) - self.assertAllClose(np.var(var.numpy()), variance, tol, tol) - - def test_return_l1_regularized_weights_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - weight: 0.5 - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - regularizer = keras_config.params()['kernel_regularizer'] - weights = np.array([1., -1, 4., 2.]) - result = regularizer(tf.constant(weights)).numpy() - self.assertAllClose(np.abs(weights).sum() * 0.5, result) - - def test_return_l2_regularized_weights_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - weight: 0.42 - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - regularizer = keras_config.params()['kernel_regularizer'] - weights = np.array([1., -1, 4., 2.]) - result = regularizer(tf.constant(weights)).numpy() - self.assertAllClose(np.power(weights, 2).sum() / 2.0 * 0.42, result) - - def test_return_l1_regularizer_weight_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l1_regularizer { - weight: 0.5 - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Parse(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - regularizer_weight = keras_config.get_regularizer_weight() - self.assertIsInstance(regularizer_weight, float) - self.assertAlmostEqual(regularizer_weight, 0.5) - - def test_return_l2_regularizer_weight_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - weight: 0.5 - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Parse(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - regularizer_weight = keras_config.get_regularizer_weight() - self.assertIsInstance(regularizer_weight, float) - self.assertAlmostEqual(regularizer_weight, 0.25) - - def test_return_undefined_regularizer_weight_keras(self): - conv_hyperparams_text_proto = """ - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Parse(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - regularizer_weight = keras_config.get_regularizer_weight() - self.assertIsNone(regularizer_weight) - - def test_return_non_default_batch_norm_params_keras( - self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - decay: 0.7 - center: false - scale: true - epsilon: 0.03 - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - self.assertTrue(keras_config.use_batch_norm()) - batch_norm_params = keras_config.batch_norm_params() - self.assertAlmostEqual(batch_norm_params['momentum'], 0.7) - self.assertAlmostEqual(batch_norm_params['epsilon'], 0.03) - self.assertFalse(batch_norm_params['center']) - self.assertTrue(batch_norm_params['scale']) - - batch_norm_layer = keras_config.build_batch_norm() - self.assertIsInstance(batch_norm_layer, - freezable_batch_norm.FreezableBatchNorm) - - def test_return_non_default_batch_norm_params_keras_override( - self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - decay: 0.7 - center: false - scale: true - epsilon: 0.03 - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - self.assertTrue(keras_config.use_batch_norm()) - batch_norm_params = keras_config.batch_norm_params(momentum=0.4) - self.assertAlmostEqual(batch_norm_params['momentum'], 0.4) - self.assertAlmostEqual(batch_norm_params['epsilon'], 0.03) - self.assertFalse(batch_norm_params['center']) - self.assertTrue(batch_norm_params['scale']) - - def test_do_not_use_batch_norm_if_default_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - self.assertFalse(keras_config.use_batch_norm()) - self.assertEqual(keras_config.batch_norm_params(), {}) - - # The batch norm builder should build an identity Lambda layer - identity_layer = keras_config.build_batch_norm() - self.assertIsInstance(identity_layer, - tf.keras.layers.Lambda) - - def test_do_not_use_bias_if_batch_norm_center_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - decay: 0.7 - center: true - scale: true - epsilon: 0.03 - train: true - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - self.assertTrue(keras_config.use_batch_norm()) - batch_norm_params = keras_config.batch_norm_params() - self.assertTrue(batch_norm_params['center']) - self.assertTrue(batch_norm_params['scale']) - hyperparams = keras_config.params() - self.assertFalse(hyperparams['use_bias']) - - def test_force_use_bias_if_batch_norm_center_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - decay: 0.7 - center: true - scale: true - epsilon: 0.03 - train: true - } - force_use_bias: true - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - - self.assertTrue(keras_config.use_batch_norm()) - batch_norm_params = keras_config.batch_norm_params() - self.assertTrue(batch_norm_params['center']) - self.assertTrue(batch_norm_params['scale']) - hyperparams = keras_config.params() - self.assertTrue(hyperparams['use_bias']) - - def test_use_none_activation_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: NONE - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - self.assertIsNone(keras_config.params()['activation']) - self.assertIsNone( - keras_config.params(include_activation=True)['activation']) - activation_layer = keras_config.build_activation_layer() - self.assertIsInstance(activation_layer, tf.keras.layers.Lambda) - self.assertEqual(activation_layer.function, tf.identity) - - def test_use_relu_activation_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - self.assertIsNone(keras_config.params()['activation']) - self.assertEqual( - keras_config.params(include_activation=True)['activation'], tf.nn.relu) - activation_layer = keras_config.build_activation_layer() - self.assertIsInstance(activation_layer, tf.keras.layers.Lambda) - self.assertEqual(activation_layer.function, tf.nn.relu) - - def test_use_relu_6_activation_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU_6 - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - self.assertIsNone(keras_config.params()['activation']) - self.assertEqual( - keras_config.params(include_activation=True)['activation'], tf.nn.relu6) - activation_layer = keras_config.build_activation_layer() - self.assertIsInstance(activation_layer, tf.keras.layers.Lambda) - self.assertEqual(activation_layer.function, tf.nn.relu6) - - def test_use_swish_activation_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: SWISH - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - self.assertIsNone(keras_config.params()['activation']) - self.assertEqual( - keras_config.params(include_activation=True)['activation'], tf.nn.swish) - activation_layer = keras_config.build_activation_layer() - self.assertIsInstance(activation_layer, tf.keras.layers.Lambda) - self.assertEqual(activation_layer.function, tf.nn.swish) - - def test_override_activation_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - activation: RELU_6 - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - new_params = keras_config.params(activation=tf.nn.relu) - self.assertEqual(new_params['activation'], tf.nn.relu) - - def test_variance_in_range_with_variance_scaling_initializer_fan_in_keras( - self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_IN - uniform: false - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - initializer = keras_config.params()['kernel_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=2. / 100.) - - def test_variance_in_range_with_variance_scaling_initializer_fan_out_keras( - self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_OUT - uniform: false - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - initializer = keras_config.params()['kernel_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=2. / 40.) - - def test_variance_in_range_with_variance_scaling_initializer_fan_avg_keras( - self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_AVG - uniform: false - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - initializer = keras_config.params()['kernel_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=4. / (100. + 40.)) - - def test_variance_in_range_with_variance_scaling_initializer_uniform_keras( - self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - variance_scaling_initializer { - factor: 2.0 - mode: FAN_IN - uniform: true - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - initializer = keras_config.params()['kernel_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=2. / 100.) - - def test_variance_in_range_with_truncated_normal_initializer_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - mean: 0.0 - stddev: 0.8 - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - initializer = keras_config.params()['kernel_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=0.49, tol=1e-1) - - def test_variance_in_range_with_random_normal_initializer_keras(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - random_normal_initializer { - mean: 0.0 - stddev: 0.8 - } - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - initializer = keras_config.params()['kernel_initializer'] - self._assert_variance_in_range(initializer, shape=[100, 40], - variance=0.64, tol=1e-1) - - def test_keras_initializer_by_name(self): - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - keras_initializer_by_name: "glorot_uniform" - } - """ - conv_hyperparams_proto = hyperparams_pb2.Hyperparams() - text_format.Parse(conv_hyperparams_text_proto, conv_hyperparams_proto) - keras_config = hyperparams_builder.KerasLayerHyperparams( - conv_hyperparams_proto) - initializer_arg = keras_config.params()['kernel_initializer'] - conv_layer = tf.keras.layers.Conv2D( - filters=16, kernel_size=3, **keras_config.params()) - self.assertEqual(initializer_arg, 'glorot_uniform') - self.assertIsInstance(conv_layer.kernel_initializer, - type(tf.keras.initializers.get('glorot_uniform'))) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/image_resizer_builder.py b/research/object_detection/builders/image_resizer_builder.py deleted file mode 100644 index 1a3f096f178..00000000000 --- a/research/object_detection/builders/image_resizer_builder.py +++ /dev/null @@ -1,187 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Builder function for image resizing operations.""" -import functools -import tensorflow.compat.v1 as tf - -from object_detection.core import preprocessor -from object_detection.protos import image_resizer_pb2 - - -def _tf_resize_method(resize_method): - """Maps image resize method from enumeration type to TensorFlow. - - Args: - resize_method: The resize_method attribute of keep_aspect_ratio_resizer or - fixed_shape_resizer. - - Returns: - method: The corresponding TensorFlow ResizeMethod. - - Raises: - ValueError: if `resize_method` is of unknown type. - """ - dict_method = { - image_resizer_pb2.BILINEAR: - tf.image.ResizeMethod.BILINEAR, - image_resizer_pb2.NEAREST_NEIGHBOR: - tf.image.ResizeMethod.NEAREST_NEIGHBOR, - image_resizer_pb2.BICUBIC: - tf.image.ResizeMethod.BICUBIC, - image_resizer_pb2.AREA: - tf.image.ResizeMethod.AREA - } - if resize_method in dict_method: - return dict_method[resize_method] - else: - raise ValueError('Unknown resize_method') - - -def build(image_resizer_config): - """Builds callable for image resizing operations. - - Args: - image_resizer_config: image_resizer.proto object containing parameters for - an image resizing operation. - - Returns: - image_resizer_fn: Callable for image resizing. This callable always takes - a rank-3 image tensor (corresponding to a single image) and returns a - rank-3 image tensor, possibly with new spatial dimensions. - - Raises: - ValueError: if `image_resizer_config` is of incorrect type. - ValueError: if `image_resizer_config.image_resizer_oneof` is of expected - type. - ValueError: if min_dimension > max_dimension when keep_aspect_ratio_resizer - is used. - """ - if not isinstance(image_resizer_config, image_resizer_pb2.ImageResizer): - raise ValueError('image_resizer_config not of type ' - 'image_resizer_pb2.ImageResizer.') - - image_resizer_oneof = image_resizer_config.WhichOneof('image_resizer_oneof') - if image_resizer_oneof == 'keep_aspect_ratio_resizer': - keep_aspect_ratio_config = image_resizer_config.keep_aspect_ratio_resizer - if not (keep_aspect_ratio_config.min_dimension <= - keep_aspect_ratio_config.max_dimension): - raise ValueError('min_dimension > max_dimension') - method = _tf_resize_method(keep_aspect_ratio_config.resize_method) - per_channel_pad_value = (0, 0, 0) - if keep_aspect_ratio_config.per_channel_pad_value: - per_channel_pad_value = tuple(keep_aspect_ratio_config. - per_channel_pad_value) - image_resizer_fn = functools.partial( - preprocessor.resize_to_range, - min_dimension=keep_aspect_ratio_config.min_dimension, - max_dimension=keep_aspect_ratio_config.max_dimension, - method=method, - pad_to_max_dimension=keep_aspect_ratio_config.pad_to_max_dimension, - per_channel_pad_value=per_channel_pad_value) - if not keep_aspect_ratio_config.convert_to_grayscale: - return image_resizer_fn - elif image_resizer_oneof == 'fixed_shape_resizer': - fixed_shape_resizer_config = image_resizer_config.fixed_shape_resizer - method = _tf_resize_method(fixed_shape_resizer_config.resize_method) - image_resizer_fn = functools.partial( - preprocessor.resize_image, - new_height=fixed_shape_resizer_config.height, - new_width=fixed_shape_resizer_config.width, - method=method) - if not fixed_shape_resizer_config.convert_to_grayscale: - return image_resizer_fn - elif image_resizer_oneof == 'identity_resizer': - def image_resizer_fn(image, masks=None, **kwargs): - del kwargs - if masks is None: - return [image, tf.shape(image)] - else: - return [image, masks, tf.shape(image)] - return image_resizer_fn - elif image_resizer_oneof == 'conditional_shape_resizer': - conditional_shape_resize_config = ( - image_resizer_config.conditional_shape_resizer) - method = _tf_resize_method(conditional_shape_resize_config.resize_method) - - if conditional_shape_resize_config.condition == ( - image_resizer_pb2.ConditionalShapeResizer.GREATER): - image_resizer_fn = functools.partial( - preprocessor.resize_to_max_dimension, - max_dimension=conditional_shape_resize_config.size_threshold, - method=method) - - elif conditional_shape_resize_config.condition == ( - image_resizer_pb2.ConditionalShapeResizer.SMALLER): - image_resizer_fn = functools.partial( - preprocessor.resize_to_min_dimension, - min_dimension=conditional_shape_resize_config.size_threshold, - method=method) - else: - raise ValueError( - 'Invalid image resizer condition option for ' - 'ConditionalShapeResizer: \'%s\'.' - % conditional_shape_resize_config.condition) - if not conditional_shape_resize_config.convert_to_grayscale: - return image_resizer_fn - elif image_resizer_oneof == 'pad_to_multiple_resizer': - pad_to_multiple_resizer_config = ( - image_resizer_config.pad_to_multiple_resizer) - - if pad_to_multiple_resizer_config.multiple < 0: - raise ValueError('`multiple` for pad_to_multiple_resizer should be > 0.') - - else: - image_resizer_fn = functools.partial( - preprocessor.resize_pad_to_multiple, - multiple=pad_to_multiple_resizer_config.multiple) - - if not pad_to_multiple_resizer_config.convert_to_grayscale: - return image_resizer_fn - else: - raise ValueError( - 'Invalid image resizer option: \'%s\'.' % image_resizer_oneof) - - def grayscale_image_resizer(image, masks=None): - """Convert to grayscale before applying image_resizer_fn. - - Args: - image: A 3D tensor of shape [height, width, 3] - masks: (optional) rank 3 float32 tensor with shape [num_instances, height, - width] containing instance masks. - - Returns: - Note that the position of the resized_image_shape changes based on whether - masks are present. - resized_image: A 3D tensor of shape [new_height, new_width, 1], - where the image has been resized (with bilinear interpolation) so that - min(new_height, new_width) == min_dimension or - max(new_height, new_width) == max_dimension. - resized_masks: If masks is not None, also outputs masks. A 3D tensor of - shape [num_instances, new_height, new_width]. - resized_image_shape: A 1D tensor of shape [3] containing shape of the - resized image. - """ - # image_resizer_fn returns [resized_image, resized_image_shape] if - # mask==None, otherwise it returns - # [resized_image, resized_mask, resized_image_shape]. In either case, we - # only deal with first and last element of the returned list. - retval = image_resizer_fn(image, masks) - resized_image = retval[0] - resized_image_shape = retval[-1] - retval[0] = preprocessor.rgb_to_gray(resized_image) - retval[-1] = tf.concat([resized_image_shape[:-1], [1]], 0) - return retval - - return functools.partial(grayscale_image_resizer) diff --git a/research/object_detection/builders/image_resizer_builder_test.py b/research/object_detection/builders/image_resizer_builder_test.py deleted file mode 100644 index dfc456eab1d..00000000000 --- a/research/object_detection/builders/image_resizer_builder_test.py +++ /dev/null @@ -1,243 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.builders.image_resizer_builder.""" -import numpy as np -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from object_detection.builders import image_resizer_builder -from object_detection.protos import image_resizer_pb2 -from object_detection.utils import test_case - - -class ImageResizerBuilderTest(test_case.TestCase): - - def _shape_of_resized_random_image_given_text_proto(self, input_shape, - text_proto): - image_resizer_config = image_resizer_pb2.ImageResizer() - text_format.Merge(text_proto, image_resizer_config) - image_resizer_fn = image_resizer_builder.build(image_resizer_config) - def graph_fn(): - images = tf.cast( - tf.random_uniform(input_shape, minval=0, maxval=255, dtype=tf.int32), - dtype=tf.float32) - resized_images, _ = image_resizer_fn(images) - return resized_images - return self.execute_cpu(graph_fn, []).shape - - def test_build_keep_aspect_ratio_resizer_returns_expected_shape(self): - image_resizer_text_proto = """ - keep_aspect_ratio_resizer { - min_dimension: 10 - max_dimension: 20 - } - """ - input_shape = (50, 25, 3) - expected_output_shape = (20, 10, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_build_keep_aspect_ratio_resizer_grayscale(self): - image_resizer_text_proto = """ - keep_aspect_ratio_resizer { - min_dimension: 10 - max_dimension: 20 - convert_to_grayscale: true - } - """ - input_shape = (50, 25, 3) - expected_output_shape = (20, 10, 1) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_build_keep_aspect_ratio_resizer_with_padding(self): - image_resizer_text_proto = """ - keep_aspect_ratio_resizer { - min_dimension: 10 - max_dimension: 20 - pad_to_max_dimension: true - per_channel_pad_value: 3 - per_channel_pad_value: 4 - per_channel_pad_value: 5 - } - """ - input_shape = (50, 25, 3) - expected_output_shape = (20, 20, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_built_fixed_shape_resizer_returns_expected_shape(self): - image_resizer_text_proto = """ - fixed_shape_resizer { - height: 10 - width: 20 - } - """ - input_shape = (50, 25, 3) - expected_output_shape = (10, 20, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_built_fixed_shape_resizer_grayscale(self): - image_resizer_text_proto = """ - fixed_shape_resizer { - height: 10 - width: 20 - convert_to_grayscale: true - } - """ - input_shape = (50, 25, 3) - expected_output_shape = (10, 20, 1) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_identity_resizer_returns_expected_shape(self): - image_resizer_text_proto = """ - identity_resizer { - } - """ - input_shape = (10, 20, 3) - expected_output_shape = (10, 20, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_raises_error_on_invalid_input(self): - invalid_input = 'invalid_input' - with self.assertRaises(ValueError): - image_resizer_builder.build(invalid_input) - - def _resized_image_given_text_proto(self, image, text_proto): - image_resizer_config = image_resizer_pb2.ImageResizer() - text_format.Merge(text_proto, image_resizer_config) - image_resizer_fn = image_resizer_builder.build(image_resizer_config) - def graph_fn(image): - resized_image, _ = image_resizer_fn(image) - return resized_image - return self.execute_cpu(graph_fn, [image]) - - def test_fixed_shape_resizer_nearest_neighbor_method(self): - image_resizer_text_proto = """ - fixed_shape_resizer { - height: 1 - width: 1 - resize_method: NEAREST_NEIGHBOR - } - """ - image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) - image = np.expand_dims(image, axis=2) - image = np.tile(image, (1, 1, 3)) - image = np.expand_dims(image, axis=0) - resized_image = self._resized_image_given_text_proto( - image, image_resizer_text_proto) - vals = np.unique(resized_image).tolist() - self.assertEqual(len(vals), 1) - self.assertEqual(vals[0], 1) - - def test_build_conditional_shape_resizer_greater_returns_expected_shape(self): - image_resizer_text_proto = """ - conditional_shape_resizer { - condition: GREATER - size_threshold: 30 - } - """ - input_shape = (60, 30, 3) - expected_output_shape = (30, 15, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_build_conditional_shape_resizer_same_shape_with_no_resize(self): - image_resizer_text_proto = """ - conditional_shape_resizer { - condition: GREATER - size_threshold: 30 - } - """ - input_shape = (15, 15, 3) - expected_output_shape = (15, 15, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_build_conditional_shape_resizer_smaller_returns_expected_shape(self): - image_resizer_text_proto = """ - conditional_shape_resizer { - condition: SMALLER - size_threshold: 30 - } - """ - input_shape = (30, 15, 3) - expected_output_shape = (60, 30, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_build_conditional_shape_resizer_grayscale(self): - image_resizer_text_proto = """ - conditional_shape_resizer { - condition: GREATER - size_threshold: 30 - convert_to_grayscale: true - } - """ - input_shape = (60, 30, 3) - expected_output_shape = (30, 15, 1) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_build_conditional_shape_resizer_error_on_invalid_condition(self): - invalid_image_resizer_text_proto = """ - conditional_shape_resizer { - condition: INVALID - size_threshold: 30 - } - """ - with self.assertRaises(ValueError): - image_resizer_builder.build(invalid_image_resizer_text_proto) - - def test_build_pad_to_multiple_resizer(self): - """Test building a pad_to_multiple_resizer from proto.""" - image_resizer_text_proto = """ - pad_to_multiple_resizer { - multiple: 32 - } - """ - input_shape = (60, 30, 3) - expected_output_shape = (64, 32, 3) - output_shape = self._shape_of_resized_random_image_given_text_proto( - input_shape, image_resizer_text_proto) - self.assertEqual(output_shape, expected_output_shape) - - def test_build_pad_to_multiple_resizer_invalid_multiple(self): - """Test that building a pad_to_multiple_resizer errors with invalid multiple.""" - - image_resizer_text_proto = """ - pad_to_multiple_resizer { - multiple: -10 - } - """ - - with self.assertRaises(ValueError): - image_resizer_builder.build(image_resizer_text_proto) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/input_reader_builder.py b/research/object_detection/builders/input_reader_builder.py deleted file mode 100644 index ea8137d9e4f..00000000000 --- a/research/object_detection/builders/input_reader_builder.py +++ /dev/null @@ -1,91 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Input reader builder. - -Creates data sources for DetectionModels from an InputReader config. See -input_reader.proto for options. - -Note: If users wishes to also use their own InputReaders with the Object -Detection configuration framework, they should define their own builder function -that wraps the build function. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.data_decoders import tf_example_decoder -from object_detection.data_decoders import tf_sequence_example_decoder -from object_detection.protos import input_reader_pb2 - -parallel_reader = slim.parallel_reader - - -def build(input_reader_config): - """Builds a tensor dictionary based on the InputReader config. - - Args: - input_reader_config: A input_reader_pb2.InputReader object. - - Returns: - A tensor dict based on the input_reader_config. - - Raises: - ValueError: On invalid input reader proto. - ValueError: If no input paths are specified. - """ - if not isinstance(input_reader_config, input_reader_pb2.InputReader): - raise ValueError('input_reader_config not of type ' - 'input_reader_pb2.InputReader.') - - if input_reader_config.WhichOneof('input_reader') == 'tf_record_input_reader': - config = input_reader_config.tf_record_input_reader - if not config.input_path: - raise ValueError('At least one input path must be specified in ' - '`input_reader_config`.') - _, string_tensor = parallel_reader.parallel_read( - config.input_path[:], # Convert `RepeatedScalarContainer` to list. - reader_class=tf.TFRecordReader, - num_epochs=(input_reader_config.num_epochs - if input_reader_config.num_epochs else None), - num_readers=input_reader_config.num_readers, - shuffle=input_reader_config.shuffle, - dtypes=[tf.string, tf.string], - capacity=input_reader_config.queue_capacity, - min_after_dequeue=input_reader_config.min_after_dequeue) - - label_map_proto_file = None - if input_reader_config.HasField('label_map_path'): - label_map_proto_file = input_reader_config.label_map_path - input_type = input_reader_config.input_type - if input_type == input_reader_pb2.InputType.Value('TF_EXAMPLE'): - decoder = tf_example_decoder.TfExampleDecoder( - load_instance_masks=input_reader_config.load_instance_masks, - instance_mask_type=input_reader_config.mask_type, - label_map_proto_file=label_map_proto_file, - load_context_features=input_reader_config.load_context_features) - return decoder.decode(string_tensor) - elif input_type == input_reader_pb2.InputType.Value('TF_SEQUENCE_EXAMPLE'): - decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder( - label_map_proto_file=label_map_proto_file, - load_context_features=input_reader_config.load_context_features, - load_context_image_ids=input_reader_config.load_context_image_ids) - return decoder.decode(string_tensor) - raise ValueError('Unsupported input_type.') - raise ValueError('Unsupported input_reader_config.') diff --git a/research/object_detection/builders/input_reader_builder_tf1_test.py b/research/object_detection/builders/input_reader_builder_tf1_test.py deleted file mode 100644 index 6049128b03f..00000000000 --- a/research/object_detection/builders/input_reader_builder_tf1_test.py +++ /dev/null @@ -1,306 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for input_reader_builder.""" - -import os -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import input_reader_builder -from object_detection.core import standard_fields as fields -from object_detection.dataset_tools import seq_example_util -from object_detection.protos import input_reader_pb2 -from object_detection.utils import dataset_util -from object_detection.utils import tf_version - - -def _get_labelmap_path(): - """Returns an absolute path to label map file.""" - parent_path = os.path.dirname(tf.resource_loader.get_data_files_path()) - return os.path.join(parent_path, 'data', - 'pet_label_map.pbtxt') - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class InputReaderBuilderTest(tf.test.TestCase): - - def create_tf_record(self): - path = os.path.join(self.get_temp_dir(), 'tfrecord') - writer = tf.python_io.TFRecordWriter(path) - - image_tensor = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8) - flat_mask = (4 * 5) * [1.0] - with self.test_session(): - encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).eval() - example = tf.train.Example(features=tf.train.Features(feature={ - 'image/encoded': dataset_util.bytes_feature(encoded_jpeg), - 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/height': dataset_util.int64_feature(4), - 'image/width': dataset_util.int64_feature(5), - 'image/object/bbox/xmin': dataset_util.float_list_feature([0.0]), - 'image/object/bbox/xmax': dataset_util.float_list_feature([1.0]), - 'image/object/bbox/ymin': dataset_util.float_list_feature([0.0]), - 'image/object/bbox/ymax': dataset_util.float_list_feature([1.0]), - 'image/object/class/label': dataset_util.int64_list_feature([2]), - 'image/object/mask': dataset_util.float_list_feature(flat_mask), - })) - writer.write(example.SerializeToString()) - writer.close() - - return path - - def _make_random_serialized_jpeg_images(self, num_frames, image_height, - image_width): - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - images_list = tf.unstack(images, axis=0) - encoded_images_list = [tf.io.encode_jpeg(image) for image in images_list] - with tf.Session() as sess: - encoded_images = sess.run(encoded_images_list) - return encoded_images - - def create_tf_record_sequence_example(self): - path = os.path.join(self.get_temp_dir(), 'tfrecord') - writer = tf.python_io.TFRecordWriter(path) - num_frames = 4 - image_height = 20 - image_width = 30 - image_source_ids = [str(i) for i in range(num_frames)] - with self.test_session(): - encoded_images = self._make_random_serialized_jpeg_images( - num_frames, image_height, image_width) - sequence_example_serialized = seq_example_util.make_sequence_example( - dataset_name='video_dataset', - video_id='video', - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_source_ids=image_source_ids, - image_format='JPEG', - is_annotated=[[1], [1], [1], [1]], - bboxes=[ - [[]], # Frame 0. - [[0., 0., 1., 1.]], # Frame 1. - [[0., 0., 1., 1.], - [0.1, 0.1, 0.2, 0.2]], # Frame 2. - [[]], # Frame 3. - ], - label_strings=[ - [], # Frame 0. - ['Abyssinian'], # Frame 1. - ['Abyssinian', 'american_bulldog'], # Frame 2. - [], # Frame 3 - ]).SerializeToString() - - writer.write(sequence_example_serialized) - writer.close() - - return path - - def create_tf_record_with_context(self): - path = os.path.join(self.get_temp_dir(), 'tfrecord') - writer = tf.python_io.TFRecordWriter(path) - - image_tensor = np.random.randint(255, size=(4, 5, 3)).astype(np.uint8) - flat_mask = (4 * 5) * [1.0] - context_features = (10 * 3) * [1.0] - with self.test_session(): - encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).eval() - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/height': - dataset_util.int64_feature(4), - 'image/width': - dataset_util.int64_feature(5), - 'image/object/bbox/xmin': - dataset_util.float_list_feature([0.0]), - 'image/object/bbox/xmax': - dataset_util.float_list_feature([1.0]), - 'image/object/bbox/ymin': - dataset_util.float_list_feature([0.0]), - 'image/object/bbox/ymax': - dataset_util.float_list_feature([1.0]), - 'image/object/class/label': - dataset_util.int64_list_feature([2]), - 'image/object/mask': - dataset_util.float_list_feature(flat_mask), - 'image/context_features': - dataset_util.float_list_feature(context_features), - 'image/context_feature_length': - dataset_util.int64_list_feature([10]), - })) - writer.write(example.SerializeToString()) - writer.close() - - return path - - def test_build_tf_record_input_reader(self): - tf_record_path = self.create_tf_record() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - tensor_dict = input_reader_builder.build(input_reader_proto) - - with tf.train.MonitoredSession() as sess: - output_dict = sess.run(tensor_dict) - - self.assertNotIn(fields.InputDataFields.groundtruth_instance_masks, - output_dict) - self.assertEqual((4, 5, 3), output_dict[fields.InputDataFields.image].shape) - self.assertEqual([2], - output_dict[fields.InputDataFields.groundtruth_classes]) - self.assertEqual( - (1, 4), output_dict[fields.InputDataFields.groundtruth_boxes].shape) - self.assertAllEqual( - [0.0, 0.0, 1.0, 1.0], - output_dict[fields.InputDataFields.groundtruth_boxes][0]) - - def test_build_tf_record_input_reader_sequence_example(self): - tf_record_path = self.create_tf_record_sequence_example() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - input_type: TF_SEQUENCE_EXAMPLE - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - input_reader_proto.label_map_path = _get_labelmap_path() - text_format.Merge(input_reader_text_proto, input_reader_proto) - tensor_dict = input_reader_builder.build(input_reader_proto) - - with tf.train.MonitoredSession() as sess: - output_dict = sess.run(tensor_dict) - - expected_groundtruth_classes = [[-1, -1], [1, -1], [1, 2], [-1, -1]] - expected_groundtruth_boxes = [[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 1.0, 1.0], [0.1, 0.1, 0.2, 0.2]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]] - expected_num_groundtruth_boxes = [0, 1, 2, 0] - - self.assertNotIn( - fields.InputDataFields.groundtruth_instance_masks, output_dict) - # sequence example images are encoded - self.assertEqual((4,), output_dict[fields.InputDataFields.image].shape) - self.assertAllEqual(expected_groundtruth_classes, - output_dict[fields.InputDataFields.groundtruth_classes]) - self.assertEqual( - (4, 2, 4), output_dict[fields.InputDataFields.groundtruth_boxes].shape) - self.assertAllClose(expected_groundtruth_boxes, - output_dict[fields.InputDataFields.groundtruth_boxes]) - self.assertAllClose( - expected_num_groundtruth_boxes, - output_dict[fields.InputDataFields.num_groundtruth_boxes]) - - def test_build_tf_record_input_reader_with_context(self): - tf_record_path = self.create_tf_record_with_context() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - input_reader_proto.load_context_features = True - tensor_dict = input_reader_builder.build(input_reader_proto) - - with tf.train.MonitoredSession() as sess: - output_dict = sess.run(tensor_dict) - - self.assertNotIn(fields.InputDataFields.groundtruth_instance_masks, - output_dict) - self.assertEqual((4, 5, 3), output_dict[fields.InputDataFields.image].shape) - self.assertEqual([2], - output_dict[fields.InputDataFields.groundtruth_classes]) - self.assertEqual( - (1, 4), output_dict[fields.InputDataFields.groundtruth_boxes].shape) - self.assertAllEqual( - [0.0, 0.0, 1.0, 1.0], - output_dict[fields.InputDataFields.groundtruth_boxes][0]) - self.assertAllEqual( - [0.0, 0.0, 1.0, 1.0], - output_dict[fields.InputDataFields.groundtruth_boxes][0]) - self.assertAllEqual( - (3, 10), output_dict[fields.InputDataFields.context_features].shape) - self.assertAllEqual( - (10), output_dict[fields.InputDataFields.context_feature_length]) - - def test_build_tf_record_input_reader_and_load_instance_masks(self): - tf_record_path = self.create_tf_record() - - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - load_instance_masks: true - tf_record_input_reader {{ - input_path: '{0}' - }} - """.format(tf_record_path) - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - tensor_dict = input_reader_builder.build(input_reader_proto) - - with tf.train.MonitoredSession() as sess: - output_dict = sess.run(tensor_dict) - - self.assertEqual((4, 5, 3), output_dict[fields.InputDataFields.image].shape) - self.assertEqual([2], - output_dict[fields.InputDataFields.groundtruth_classes]) - self.assertEqual( - (1, 4), output_dict[fields.InputDataFields.groundtruth_boxes].shape) - self.assertAllEqual( - [0.0, 0.0, 1.0, 1.0], - output_dict[fields.InputDataFields.groundtruth_boxes][0]) - self.assertAllEqual( - (1, 4, 5), - output_dict[fields.InputDataFields.groundtruth_instance_masks].shape) - - def test_raises_error_with_no_input_paths(self): - input_reader_text_proto = """ - shuffle: false - num_readers: 1 - load_instance_masks: true - """ - input_reader_proto = input_reader_pb2.InputReader() - text_format.Merge(input_reader_text_proto, input_reader_proto) - with self.assertRaises(ValueError): - input_reader_builder.build(input_reader_proto) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/losses_builder.py b/research/object_detection/builders/losses_builder.py deleted file mode 100644 index a73fff385b3..00000000000 --- a/research/object_detection/builders/losses_builder.py +++ /dev/null @@ -1,270 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A function to build localization and classification losses from config.""" - -import functools -from object_detection.core import balanced_positive_negative_sampler as sampler -from object_detection.core import losses -from object_detection.protos import losses_pb2 -from object_detection.utils import ops - - -def build(loss_config): - """Build losses based on the config. - - Builds classification, localization losses and optionally a hard example miner - based on the config. - - Args: - loss_config: A losses_pb2.Loss object. - - Returns: - classification_loss: Classification loss object. - localization_loss: Localization loss object. - classification_weight: Classification loss weight. - localization_weight: Localization loss weight. - hard_example_miner: Hard example miner object. - random_example_sampler: BalancedPositiveNegativeSampler object. - - Raises: - ValueError: If hard_example_miner is used with sigmoid_focal_loss. - ValueError: If random_example_sampler is getting non-positive value as - desired positive example fraction. - """ - classification_loss = _build_classification_loss( - loss_config.classification_loss) - localization_loss = _build_localization_loss( - loss_config.localization_loss) - classification_weight = loss_config.classification_weight - localization_weight = loss_config.localization_weight - hard_example_miner = None - if loss_config.HasField('hard_example_miner'): - if (loss_config.classification_loss.WhichOneof('classification_loss') == - 'weighted_sigmoid_focal'): - raise ValueError('HardExampleMiner should not be used with sigmoid focal ' - 'loss') - hard_example_miner = build_hard_example_miner( - loss_config.hard_example_miner, - classification_weight, - localization_weight) - random_example_sampler = None - if loss_config.HasField('random_example_sampler'): - if loss_config.random_example_sampler.positive_sample_fraction <= 0: - raise ValueError('RandomExampleSampler should not use non-positive' - 'value as positive sample fraction.') - random_example_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=loss_config.random_example_sampler. - positive_sample_fraction) - - if loss_config.expected_loss_weights == loss_config.NONE: - expected_loss_weights_fn = None - elif loss_config.expected_loss_weights == loss_config.EXPECTED_SAMPLING: - expected_loss_weights_fn = functools.partial( - ops.expected_classification_loss_by_expected_sampling, - min_num_negative_samples=loss_config.min_num_negative_samples, - desired_negative_sampling_ratio=loss_config - .desired_negative_sampling_ratio) - elif (loss_config.expected_loss_weights == loss_config - .REWEIGHTING_UNMATCHED_ANCHORS): - expected_loss_weights_fn = functools.partial( - ops.expected_classification_loss_by_reweighting_unmatched_anchors, - min_num_negative_samples=loss_config.min_num_negative_samples, - desired_negative_sampling_ratio=loss_config - .desired_negative_sampling_ratio) - else: - raise ValueError('Not a valid value for expected_classification_loss.') - - return (classification_loss, localization_loss, classification_weight, - localization_weight, hard_example_miner, random_example_sampler, - expected_loss_weights_fn) - - -def build_hard_example_miner(config, - classification_weight, - localization_weight): - """Builds hard example miner based on the config. - - Args: - config: A losses_pb2.HardExampleMiner object. - classification_weight: Classification loss weight. - localization_weight: Localization loss weight. - - Returns: - Hard example miner. - - """ - loss_type = None - if config.loss_type == losses_pb2.HardExampleMiner.BOTH: - loss_type = 'both' - if config.loss_type == losses_pb2.HardExampleMiner.CLASSIFICATION: - loss_type = 'cls' - if config.loss_type == losses_pb2.HardExampleMiner.LOCALIZATION: - loss_type = 'loc' - - max_negatives_per_positive = None - num_hard_examples = None - if config.max_negatives_per_positive > 0: - max_negatives_per_positive = config.max_negatives_per_positive - if config.num_hard_examples > 0: - num_hard_examples = config.num_hard_examples - hard_example_miner = losses.HardExampleMiner( - num_hard_examples=num_hard_examples, - iou_threshold=config.iou_threshold, - loss_type=loss_type, - cls_loss_weight=classification_weight, - loc_loss_weight=localization_weight, - max_negatives_per_positive=max_negatives_per_positive, - min_negatives_per_image=config.min_negatives_per_image) - return hard_example_miner - - -def build_faster_rcnn_classification_loss(loss_config): - """Builds a classification loss for Faster RCNN based on the loss config. - - Args: - loss_config: A losses_pb2.ClassificationLoss object. - - Returns: - Loss based on the config. - - Raises: - ValueError: On invalid loss_config. - """ - if not isinstance(loss_config, losses_pb2.ClassificationLoss): - raise ValueError('loss_config not of type losses_pb2.ClassificationLoss.') - - loss_type = loss_config.WhichOneof('classification_loss') - - if loss_type == 'weighted_sigmoid': - return losses.WeightedSigmoidClassificationLoss() - if loss_type == 'weighted_softmax': - config = loss_config.weighted_softmax - return losses.WeightedSoftmaxClassificationLoss( - logit_scale=config.logit_scale) - if loss_type == 'weighted_logits_softmax': - config = loss_config.weighted_logits_softmax - return losses.WeightedSoftmaxClassificationAgainstLogitsLoss( - logit_scale=config.logit_scale) - if loss_type == 'weighted_sigmoid_focal': - config = loss_config.weighted_sigmoid_focal - alpha = None - if config.HasField('alpha'): - alpha = config.alpha - return losses.SigmoidFocalClassificationLoss( - gamma=config.gamma, - alpha=alpha) - - # By default, Faster RCNN second stage classifier uses Softmax loss - # with anchor-wise outputs. - config = loss_config.weighted_softmax - return losses.WeightedSoftmaxClassificationLoss( - logit_scale=config.logit_scale) - - -def _build_localization_loss(loss_config): - """Builds a localization loss based on the loss config. - - Args: - loss_config: A losses_pb2.LocalizationLoss object. - - Returns: - Loss based on the config. - - Raises: - ValueError: On invalid loss_config. - """ - if not isinstance(loss_config, losses_pb2.LocalizationLoss): - raise ValueError('loss_config not of type losses_pb2.LocalizationLoss.') - - loss_type = loss_config.WhichOneof('localization_loss') - - if loss_type == 'weighted_l2': - return losses.WeightedL2LocalizationLoss() - - if loss_type == 'weighted_smooth_l1': - return losses.WeightedSmoothL1LocalizationLoss( - loss_config.weighted_smooth_l1.delta) - - if loss_type == 'weighted_iou': - return losses.WeightedIOULocalizationLoss() - - if loss_type == 'l1_localization_loss': - return losses.L1LocalizationLoss() - - if loss_type == 'weighted_giou': - return losses.WeightedGIOULocalizationLoss() - - raise ValueError('Empty loss config.') - - -def _build_classification_loss(loss_config): - """Builds a classification loss based on the loss config. - - Args: - loss_config: A losses_pb2.ClassificationLoss object. - - Returns: - Loss based on the config. - - Raises: - ValueError: On invalid loss_config. - """ - if not isinstance(loss_config, losses_pb2.ClassificationLoss): - raise ValueError('loss_config not of type losses_pb2.ClassificationLoss.') - - loss_type = loss_config.WhichOneof('classification_loss') - - if loss_type == 'weighted_sigmoid': - return losses.WeightedSigmoidClassificationLoss() - - elif loss_type == 'weighted_sigmoid_focal': - config = loss_config.weighted_sigmoid_focal - alpha = None - if config.HasField('alpha'): - alpha = config.alpha - return losses.SigmoidFocalClassificationLoss( - gamma=config.gamma, - alpha=alpha) - - elif loss_type == 'weighted_softmax': - config = loss_config.weighted_softmax - return losses.WeightedSoftmaxClassificationLoss( - logit_scale=config.logit_scale) - - elif loss_type == 'weighted_logits_softmax': - config = loss_config.weighted_logits_softmax - return losses.WeightedSoftmaxClassificationAgainstLogitsLoss( - logit_scale=config.logit_scale) - - elif loss_type == 'bootstrapped_sigmoid': - config = loss_config.bootstrapped_sigmoid - return losses.BootstrappedSigmoidClassificationLoss( - alpha=config.alpha, - bootstrap_type=('hard' if config.hard_bootstrap else 'soft')) - - elif loss_type == 'penalty_reduced_logistic_focal_loss': - config = loss_config.penalty_reduced_logistic_focal_loss - return losses.PenaltyReducedLogisticFocalLoss( - alpha=config.alpha, beta=config.beta) - - elif loss_type == 'weighted_dice_classification_loss': - config = loss_config.weighted_dice_classification_loss - return losses.WeightedDiceClassificationLoss( - squared_normalization=config.squared_normalization, - is_prediction_probability=config.is_prediction_probability) - - else: - raise ValueError('Empty loss config.') diff --git a/research/object_detection/builders/losses_builder_test.py b/research/object_detection/builders/losses_builder_test.py deleted file mode 100644 index 07c01653b20..00000000000 --- a/research/object_detection/builders/losses_builder_test.py +++ /dev/null @@ -1,614 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for losses_builder.""" - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import losses_builder -from object_detection.core import losses -from object_detection.protos import losses_pb2 -from object_detection.utils import ops - - -class LocalizationLossBuilderTest(tf.test.TestCase): - - def test_build_weighted_l2_localization_loss(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, localization_loss, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(localization_loss, - losses.WeightedL2LocalizationLoss) - - def test_build_weighted_smooth_l1_localization_loss_default_delta(self): - losses_text_proto = """ - localization_loss { - weighted_smooth_l1 { - } - } - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, localization_loss, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(localization_loss, - losses.WeightedSmoothL1LocalizationLoss) - self.assertAlmostEqual(localization_loss._delta, 1.0) - - def test_build_weighted_smooth_l1_localization_loss_non_default_delta(self): - losses_text_proto = """ - localization_loss { - weighted_smooth_l1 { - delta: 0.1 - } - } - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, localization_loss, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(localization_loss, - losses.WeightedSmoothL1LocalizationLoss) - self.assertAlmostEqual(localization_loss._delta, 0.1) - - def test_build_weighted_iou_localization_loss(self): - losses_text_proto = """ - localization_loss { - weighted_iou { - } - } - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, localization_loss, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(localization_loss, - losses.WeightedIOULocalizationLoss) - - def test_build_weighted_giou_localization_loss(self): - losses_text_proto = """ - localization_loss { - weighted_giou { - } - } - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, localization_loss, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(localization_loss, - losses.WeightedGIOULocalizationLoss) - - def test_anchorwise_output(self): - losses_text_proto = """ - localization_loss { - weighted_smooth_l1 { - } - } - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, localization_loss, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(localization_loss, - losses.WeightedSmoothL1LocalizationLoss) - predictions = tf.constant([[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]]) - targets = tf.constant([[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]]) - weights = tf.constant([[1.0, 1.0]]) - loss = localization_loss(predictions, targets, weights=weights) - self.assertEqual(loss.shape, [1, 2]) - - def test_raise_error_on_empty_localization_config(self): - losses_text_proto = """ - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - with self.assertRaises(ValueError): - losses_builder._build_localization_loss(losses_proto) - - - -class ClassificationLossBuilderTest(tf.test.TestCase): - - def test_build_weighted_sigmoid_classification_loss(self): - losses_text_proto = """ - classification_loss { - weighted_sigmoid { - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedSigmoidClassificationLoss) - - def test_build_weighted_sigmoid_focal_classification_loss(self): - losses_text_proto = """ - classification_loss { - weighted_sigmoid_focal { - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.SigmoidFocalClassificationLoss) - self.assertAlmostEqual(classification_loss._alpha, None) - self.assertAlmostEqual(classification_loss._gamma, 2.0) - - def test_build_weighted_sigmoid_focal_loss_non_default(self): - losses_text_proto = """ - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 3.0 - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.SigmoidFocalClassificationLoss) - self.assertAlmostEqual(classification_loss._alpha, 0.25) - self.assertAlmostEqual(classification_loss._gamma, 3.0) - - def test_build_weighted_softmax_classification_loss(self): - losses_text_proto = """ - classification_loss { - weighted_softmax { - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedSoftmaxClassificationLoss) - - def test_build_weighted_logits_softmax_classification_loss(self): - losses_text_proto = """ - classification_loss { - weighted_logits_softmax { - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance( - classification_loss, - losses.WeightedSoftmaxClassificationAgainstLogitsLoss) - - def test_build_weighted_softmax_classification_loss_with_logit_scale(self): - losses_text_proto = """ - classification_loss { - weighted_softmax { - logit_scale: 2.0 - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedSoftmaxClassificationLoss) - - def test_build_bootstrapped_sigmoid_classification_loss(self): - losses_text_proto = """ - classification_loss { - bootstrapped_sigmoid { - alpha: 0.5 - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.BootstrappedSigmoidClassificationLoss) - - def test_anchorwise_output(self): - losses_text_proto = """ - classification_loss { - weighted_sigmoid { - anchorwise_output: true - } - } - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedSigmoidClassificationLoss) - predictions = tf.constant([[[0.0, 1.0, 0.0], [0.0, 0.5, 0.5]]]) - targets = tf.constant([[[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]]) - weights = tf.constant([[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]) - loss = classification_loss(predictions, targets, weights=weights) - self.assertEqual(loss.shape, [1, 2, 3]) - - def test_raise_error_on_empty_config(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - with self.assertRaises(ValueError): - losses_builder.build(losses_proto) - - def test_build_penalty_reduced_logistic_focal_loss(self): - losses_text_proto = """ - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - localization_loss { - l1_localization_loss { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.PenaltyReducedLogisticFocalLoss) - self.assertAlmostEqual(classification_loss._alpha, 2.0) - self.assertAlmostEqual(classification_loss._beta, 4.0) - - def test_build_dice_loss(self): - losses_text_proto = """ - classification_loss { - weighted_dice_classification_loss { - squared_normalization: true - } - } - localization_loss { - l1_localization_loss { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss, _, _, _, _, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedDiceClassificationLoss) - assert classification_loss._squared_normalization - - -class HardExampleMinerBuilderTest(tf.test.TestCase): - - def test_do_not_build_hard_example_miner_by_default(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, _, _, _, hard_example_miner, _, _ = losses_builder.build(losses_proto) - self.assertEqual(hard_example_miner, None) - - def test_build_hard_example_miner_for_classification_loss(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - hard_example_miner { - loss_type: CLASSIFICATION - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, _, _, _, hard_example_miner, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(hard_example_miner, losses.HardExampleMiner) - self.assertEqual(hard_example_miner._loss_type, 'cls') - - def test_build_hard_example_miner_for_localization_loss(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - hard_example_miner { - loss_type: LOCALIZATION - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, _, _, _, hard_example_miner, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(hard_example_miner, losses.HardExampleMiner) - self.assertEqual(hard_example_miner._loss_type, 'loc') - - def test_build_hard_example_miner_with_non_default_values(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - hard_example_miner { - num_hard_examples: 32 - iou_threshold: 0.5 - loss_type: LOCALIZATION - max_negatives_per_positive: 10 - min_negatives_per_image: 3 - } - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - _, _, _, _, hard_example_miner, _, _ = losses_builder.build(losses_proto) - self.assertIsInstance(hard_example_miner, losses.HardExampleMiner) - self.assertEqual(hard_example_miner._num_hard_examples, 32) - self.assertAlmostEqual(hard_example_miner._iou_threshold, 0.5) - self.assertEqual(hard_example_miner._max_negatives_per_positive, 10) - self.assertEqual(hard_example_miner._min_negatives_per_image, 3) - - -class LossBuilderTest(tf.test.TestCase): - - def test_build_all_loss_parameters(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - hard_example_miner { - } - classification_weight: 0.8 - localization_weight: 0.2 - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - (classification_loss, localization_loss, classification_weight, - localization_weight, hard_example_miner, _, - _) = losses_builder.build(losses_proto) - self.assertIsInstance(hard_example_miner, losses.HardExampleMiner) - self.assertIsInstance(classification_loss, - losses.WeightedSoftmaxClassificationLoss) - self.assertIsInstance(localization_loss, - losses.WeightedL2LocalizationLoss) - self.assertAlmostEqual(classification_weight, 0.8) - self.assertAlmostEqual(localization_weight, 0.2) - - def test_build_expected_sampling(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - hard_example_miner { - } - classification_weight: 0.8 - localization_weight: 0.2 - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - (classification_loss, localization_loss, classification_weight, - localization_weight, hard_example_miner, _, - _) = losses_builder.build(losses_proto) - self.assertIsInstance(hard_example_miner, losses.HardExampleMiner) - self.assertIsInstance(classification_loss, - losses.WeightedSoftmaxClassificationLoss) - self.assertIsInstance(localization_loss, losses.WeightedL2LocalizationLoss) - self.assertAlmostEqual(classification_weight, 0.8) - self.assertAlmostEqual(localization_weight, 0.2) - - - def test_build_reweighting_unmatched_anchors(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_softmax { - } - } - hard_example_miner { - } - classification_weight: 0.8 - localization_weight: 0.2 - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - (classification_loss, localization_loss, classification_weight, - localization_weight, hard_example_miner, _, - _) = losses_builder.build(losses_proto) - self.assertIsInstance(hard_example_miner, losses.HardExampleMiner) - self.assertIsInstance(classification_loss, - losses.WeightedSoftmaxClassificationLoss) - self.assertIsInstance(localization_loss, losses.WeightedL2LocalizationLoss) - self.assertAlmostEqual(classification_weight, 0.8) - self.assertAlmostEqual(localization_weight, 0.2) - - def test_raise_error_when_both_focal_loss_and_hard_example_miner(self): - losses_text_proto = """ - localization_loss { - weighted_l2 { - } - } - classification_loss { - weighted_sigmoid_focal { - } - } - hard_example_miner { - } - classification_weight: 0.8 - localization_weight: 0.2 - """ - losses_proto = losses_pb2.Loss() - text_format.Merge(losses_text_proto, losses_proto) - with self.assertRaises(ValueError): - losses_builder.build(losses_proto) - - -class FasterRcnnClassificationLossBuilderTest(tf.test.TestCase): - - def test_build_sigmoid_loss(self): - losses_text_proto = """ - weighted_sigmoid { - } - """ - losses_proto = losses_pb2.ClassificationLoss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss = losses_builder.build_faster_rcnn_classification_loss( - losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedSigmoidClassificationLoss) - - def test_build_softmax_loss(self): - losses_text_proto = """ - weighted_softmax { - } - """ - losses_proto = losses_pb2.ClassificationLoss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss = losses_builder.build_faster_rcnn_classification_loss( - losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedSoftmaxClassificationLoss) - - def test_build_logits_softmax_loss(self): - losses_text_proto = """ - weighted_logits_softmax { - } - """ - losses_proto = losses_pb2.ClassificationLoss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss = losses_builder.build_faster_rcnn_classification_loss( - losses_proto) - self.assertTrue( - isinstance(classification_loss, - losses.WeightedSoftmaxClassificationAgainstLogitsLoss)) - - def test_build_sigmoid_focal_loss(self): - losses_text_proto = """ - weighted_sigmoid_focal { - } - """ - losses_proto = losses_pb2.ClassificationLoss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss = losses_builder.build_faster_rcnn_classification_loss( - losses_proto) - self.assertIsInstance(classification_loss, - losses.SigmoidFocalClassificationLoss) - - def test_build_softmax_loss_by_default(self): - losses_text_proto = """ - """ - losses_proto = losses_pb2.ClassificationLoss() - text_format.Merge(losses_text_proto, losses_proto) - classification_loss = losses_builder.build_faster_rcnn_classification_loss( - losses_proto) - self.assertIsInstance(classification_loss, - losses.WeightedSoftmaxClassificationLoss) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/matcher_builder.py b/research/object_detection/builders/matcher_builder.py deleted file mode 100644 index 086f74b5c45..00000000000 --- a/research/object_detection/builders/matcher_builder.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A function to build an object detection matcher from configuration.""" - -from object_detection.matchers import argmax_matcher -from object_detection.protos import matcher_pb2 -from object_detection.utils import tf_version - -if tf_version.is_tf1(): - from object_detection.matchers import bipartite_matcher # pylint: disable=g-import-not-at-top - - -def build(matcher_config): - """Builds a matcher object based on the matcher config. - - Args: - matcher_config: A matcher.proto object containing the config for the desired - Matcher. - - Returns: - Matcher based on the config. - - Raises: - ValueError: On empty matcher proto. - """ - if not isinstance(matcher_config, matcher_pb2.Matcher): - raise ValueError('matcher_config not of type matcher_pb2.Matcher.') - if matcher_config.WhichOneof('matcher_oneof') == 'argmax_matcher': - matcher = matcher_config.argmax_matcher - matched_threshold = unmatched_threshold = None - if not matcher.ignore_thresholds: - matched_threshold = matcher.matched_threshold - unmatched_threshold = matcher.unmatched_threshold - return argmax_matcher.ArgMaxMatcher( - matched_threshold=matched_threshold, - unmatched_threshold=unmatched_threshold, - negatives_lower_than_unmatched=matcher.negatives_lower_than_unmatched, - force_match_for_each_row=matcher.force_match_for_each_row, - use_matmul_gather=matcher.use_matmul_gather) - if matcher_config.WhichOneof('matcher_oneof') == 'bipartite_matcher': - if tf_version.is_tf2(): - raise ValueError('bipartite_matcher is not supported in TF 2.X') - matcher = matcher_config.bipartite_matcher - return bipartite_matcher.GreedyBipartiteMatcher(matcher.use_matmul_gather) - raise ValueError('Empty matcher.') diff --git a/research/object_detection/builders/matcher_builder_test.py b/research/object_detection/builders/matcher_builder_test.py deleted file mode 100644 index cfa55ff94fb..00000000000 --- a/research/object_detection/builders/matcher_builder_test.py +++ /dev/null @@ -1,105 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for matcher_builder.""" - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import matcher_builder -from object_detection.matchers import argmax_matcher -from object_detection.protos import matcher_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - -if tf_version.is_tf1(): - from object_detection.matchers import bipartite_matcher # pylint: disable=g-import-not-at-top - - -class MatcherBuilderTest(test_case.TestCase): - - def test_build_arg_max_matcher_with_defaults(self): - matcher_text_proto = """ - argmax_matcher { - } - """ - matcher_proto = matcher_pb2.Matcher() - text_format.Merge(matcher_text_proto, matcher_proto) - matcher_object = matcher_builder.build(matcher_proto) - self.assertIsInstance(matcher_object, argmax_matcher.ArgMaxMatcher) - self.assertAlmostEqual(matcher_object._matched_threshold, 0.5) - self.assertAlmostEqual(matcher_object._unmatched_threshold, 0.5) - self.assertTrue(matcher_object._negatives_lower_than_unmatched) - self.assertFalse(matcher_object._force_match_for_each_row) - - def test_build_arg_max_matcher_without_thresholds(self): - matcher_text_proto = """ - argmax_matcher { - ignore_thresholds: true - } - """ - matcher_proto = matcher_pb2.Matcher() - text_format.Merge(matcher_text_proto, matcher_proto) - matcher_object = matcher_builder.build(matcher_proto) - self.assertIsInstance(matcher_object, argmax_matcher.ArgMaxMatcher) - self.assertEqual(matcher_object._matched_threshold, None) - self.assertEqual(matcher_object._unmatched_threshold, None) - self.assertTrue(matcher_object._negatives_lower_than_unmatched) - self.assertFalse(matcher_object._force_match_for_each_row) - - def test_build_arg_max_matcher_with_non_default_parameters(self): - matcher_text_proto = """ - argmax_matcher { - matched_threshold: 0.7 - unmatched_threshold: 0.3 - negatives_lower_than_unmatched: false - force_match_for_each_row: true - use_matmul_gather: true - } - """ - matcher_proto = matcher_pb2.Matcher() - text_format.Merge(matcher_text_proto, matcher_proto) - matcher_object = matcher_builder.build(matcher_proto) - self.assertIsInstance(matcher_object, argmax_matcher.ArgMaxMatcher) - self.assertAlmostEqual(matcher_object._matched_threshold, 0.7) - self.assertAlmostEqual(matcher_object._unmatched_threshold, 0.3) - self.assertFalse(matcher_object._negatives_lower_than_unmatched) - self.assertTrue(matcher_object._force_match_for_each_row) - self.assertTrue(matcher_object._use_matmul_gather) - - def test_build_bipartite_matcher(self): - if tf_version.is_tf2(): - self.skipTest('BipartiteMatcher unsupported in TF 2.X. Skipping.') - matcher_text_proto = """ - bipartite_matcher { - } - """ - matcher_proto = matcher_pb2.Matcher() - text_format.Merge(matcher_text_proto, matcher_proto) - matcher_object = matcher_builder.build(matcher_proto) - self.assertIsInstance(matcher_object, - bipartite_matcher.GreedyBipartiteMatcher) - - def test_raise_error_on_empty_matcher(self): - matcher_text_proto = """ - """ - matcher_proto = matcher_pb2.Matcher() - text_format.Merge(matcher_text_proto, matcher_proto) - with self.assertRaises(ValueError): - matcher_builder.build(matcher_proto) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/model_builder.py b/research/object_detection/builders/model_builder.py deleted file mode 100644 index 83fba9c4392..00000000000 --- a/research/object_detection/builders/model_builder.py +++ /dev/null @@ -1,1263 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A function to build a DetectionModel from configuration.""" - -import functools -import sys - -from absl import logging - -from object_detection.builders import anchor_generator_builder -from object_detection.builders import box_coder_builder -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.builders import image_resizer_builder -from object_detection.builders import losses_builder -from object_detection.builders import matcher_builder -from object_detection.builders import post_processing_builder -from object_detection.builders import region_similarity_calculator_builder as sim_calc -from object_detection.core import balanced_positive_negative_sampler as sampler -from object_detection.core import post_processing -from object_detection.core import target_assigner -from object_detection.meta_architectures import center_net_meta_arch -from object_detection.meta_architectures import context_rcnn_meta_arch -from object_detection.meta_architectures import deepmac_meta_arch -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.meta_architectures import rfcn_meta_arch -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.predictors.heads import mask_head -from object_detection.protos import losses_pb2 -from object_detection.protos import model_pb2 -from object_detection.utils import label_map_util -from object_detection.utils import ops -from object_detection.utils import spatial_transform_ops as spatial_ops -from object_detection.utils import tf_version - -## Feature Extractors for TF -## This section conditionally imports different feature extractors based on the -## Tensorflow version. -## -# pylint: disable=g-import-not-at-top -if tf_version.is_tf2(): - from object_detection.models import center_net_hourglass_feature_extractor - from object_detection.models import center_net_mobilenet_v2_feature_extractor - from object_detection.models import center_net_mobilenet_v2_fpn_feature_extractor - from object_detection.models import center_net_resnet_feature_extractor - from object_detection.models import center_net_resnet_v1_fpn_feature_extractor - from object_detection.models import faster_rcnn_inception_resnet_v2_keras_feature_extractor as frcnn_inc_res_keras - from object_detection.models import faster_rcnn_resnet_keras_feature_extractor as frcnn_resnet_keras - from object_detection.models import ssd_resnet_v1_fpn_keras_feature_extractor as ssd_resnet_v1_fpn_keras - from object_detection.models import faster_rcnn_resnet_v1_fpn_keras_feature_extractor as frcnn_resnet_fpn_keras - from object_detection.models.ssd_mobilenet_v1_fpn_keras_feature_extractor import SSDMobileNetV1FpnKerasFeatureExtractor - from object_detection.models.ssd_mobilenet_v1_keras_feature_extractor import SSDMobileNetV1KerasFeatureExtractor - from object_detection.models.ssd_mobilenet_v2_fpn_keras_feature_extractor import SSDMobileNetV2FpnKerasFeatureExtractor - from object_detection.models.ssd_mobilenet_v2_keras_feature_extractor import SSDMobileNetV2KerasFeatureExtractor - from object_detection.predictors import rfcn_keras_box_predictor - if sys.version_info[0] >= 3: - from object_detection.models import ssd_efficientnet_bifpn_feature_extractor as ssd_efficientnet_bifpn - -if tf_version.is_tf1(): - from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res - from object_detection.models import faster_rcnn_inception_v2_feature_extractor as frcnn_inc_v2 - from object_detection.models import faster_rcnn_nas_feature_extractor as frcnn_nas - from object_detection.models import faster_rcnn_pnas_feature_extractor as frcnn_pnas - from object_detection.models import faster_rcnn_resnet_v1_feature_extractor as frcnn_resnet_v1 - from object_detection.models import ssd_resnet_v1_fpn_feature_extractor as ssd_resnet_v1_fpn - from object_detection.models import ssd_resnet_v1_ppn_feature_extractor as ssd_resnet_v1_ppn - from object_detection.models.embedded_ssd_mobilenet_v1_feature_extractor import EmbeddedSSDMobileNetV1FeatureExtractor - from object_detection.models.ssd_inception_v2_feature_extractor import SSDInceptionV2FeatureExtractor - from object_detection.models.ssd_mobilenet_v2_fpn_feature_extractor import SSDMobileNetV2FpnFeatureExtractor - from object_detection.models.ssd_mobilenet_v2_mnasfpn_feature_extractor import SSDMobileNetV2MnasFPNFeatureExtractor - from object_detection.models.ssd_inception_v3_feature_extractor import SSDInceptionV3FeatureExtractor - from object_detection.models.ssd_mobilenet_edgetpu_feature_extractor import SSDMobileNetEdgeTPUFeatureExtractor - from object_detection.models.ssd_mobilenet_v1_feature_extractor import SSDMobileNetV1FeatureExtractor - from object_detection.models.ssd_mobilenet_v1_fpn_feature_extractor import SSDMobileNetV1FpnFeatureExtractor - from object_detection.models.ssd_mobilenet_v1_ppn_feature_extractor import SSDMobileNetV1PpnFeatureExtractor - from object_detection.models.ssd_mobilenet_v2_feature_extractor import SSDMobileNetV2FeatureExtractor - from object_detection.models.ssd_mobilenet_v3_feature_extractor import SSDMobileNetV3LargeFeatureExtractor - from object_detection.models.ssd_mobilenet_v3_feature_extractor import SSDMobileNetV3SmallFeatureExtractor - from object_detection.models.ssd_mobilenet_v3_feature_extractor import SSDMobileNetV3SmallPrunedFeatureExtractor - from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetCPUFeatureExtractor - from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetDSPFeatureExtractor - from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetEdgeTPUFeatureExtractor - from object_detection.models.ssd_mobiledet_feature_extractor import SSDMobileDetGPUFeatureExtractor - from object_detection.models.ssd_spaghettinet_feature_extractor import SSDSpaghettinetFeatureExtractor - from object_detection.models.ssd_pnasnet_feature_extractor import SSDPNASNetFeatureExtractor - from object_detection.predictors import rfcn_box_predictor -# pylint: enable=g-import-not-at-top - -if tf_version.is_tf2(): - SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP = { - 'ssd_mobilenet_v1_keras': SSDMobileNetV1KerasFeatureExtractor, - 'ssd_mobilenet_v1_fpn_keras': SSDMobileNetV1FpnKerasFeatureExtractor, - 'ssd_mobilenet_v2_keras': SSDMobileNetV2KerasFeatureExtractor, - 'ssd_mobilenet_v2_fpn_keras': SSDMobileNetV2FpnKerasFeatureExtractor, - 'ssd_resnet50_v1_fpn_keras': - ssd_resnet_v1_fpn_keras.SSDResNet50V1FpnKerasFeatureExtractor, - 'ssd_resnet101_v1_fpn_keras': - ssd_resnet_v1_fpn_keras.SSDResNet101V1FpnKerasFeatureExtractor, - 'ssd_resnet152_v1_fpn_keras': - ssd_resnet_v1_fpn_keras.SSDResNet152V1FpnKerasFeatureExtractor, - 'ssd_efficientnet-b0_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB0BiFPNKerasFeatureExtractor, - 'ssd_efficientnet-b1_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB1BiFPNKerasFeatureExtractor, - 'ssd_efficientnet-b2_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB2BiFPNKerasFeatureExtractor, - 'ssd_efficientnet-b3_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB3BiFPNKerasFeatureExtractor, - 'ssd_efficientnet-b4_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB4BiFPNKerasFeatureExtractor, - 'ssd_efficientnet-b5_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB5BiFPNKerasFeatureExtractor, - 'ssd_efficientnet-b6_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB6BiFPNKerasFeatureExtractor, - 'ssd_efficientnet-b7_bifpn_keras': - ssd_efficientnet_bifpn.SSDEfficientNetB7BiFPNKerasFeatureExtractor, - } - - FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP = { - 'faster_rcnn_resnet50_keras': - frcnn_resnet_keras.FasterRCNNResnet50KerasFeatureExtractor, - 'faster_rcnn_resnet101_keras': - frcnn_resnet_keras.FasterRCNNResnet101KerasFeatureExtractor, - 'faster_rcnn_resnet152_keras': - frcnn_resnet_keras.FasterRCNNResnet152KerasFeatureExtractor, - 'faster_rcnn_inception_resnet_v2_keras': - frcnn_inc_res_keras.FasterRCNNInceptionResnetV2KerasFeatureExtractor, - 'faster_rcnn_resnet50_fpn_keras': - frcnn_resnet_fpn_keras.FasterRCNNResnet50FpnKerasFeatureExtractor, - 'faster_rcnn_resnet101_fpn_keras': - frcnn_resnet_fpn_keras.FasterRCNNResnet101FpnKerasFeatureExtractor, - 'faster_rcnn_resnet152_fpn_keras': - frcnn_resnet_fpn_keras.FasterRCNNResnet152FpnKerasFeatureExtractor, - } - - CENTER_NET_EXTRACTOR_FUNCTION_MAP = { - 'resnet_v2_50': - center_net_resnet_feature_extractor.resnet_v2_50, - 'resnet_v2_101': - center_net_resnet_feature_extractor.resnet_v2_101, - 'resnet_v1_18_fpn': - center_net_resnet_v1_fpn_feature_extractor.resnet_v1_18_fpn, - 'resnet_v1_34_fpn': - center_net_resnet_v1_fpn_feature_extractor.resnet_v1_34_fpn, - 'resnet_v1_50_fpn': - center_net_resnet_v1_fpn_feature_extractor.resnet_v1_50_fpn, - 'resnet_v1_101_fpn': - center_net_resnet_v1_fpn_feature_extractor.resnet_v1_101_fpn, - 'hourglass_10': - center_net_hourglass_feature_extractor.hourglass_10, - 'hourglass_20': - center_net_hourglass_feature_extractor.hourglass_20, - 'hourglass_32': - center_net_hourglass_feature_extractor.hourglass_32, - 'hourglass_52': - center_net_hourglass_feature_extractor.hourglass_52, - 'hourglass_104': - center_net_hourglass_feature_extractor.hourglass_104, - 'mobilenet_v2': - center_net_mobilenet_v2_feature_extractor.mobilenet_v2, - 'mobilenet_v2_fpn': - center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn, - 'mobilenet_v2_fpn_sep_conv': - center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn, - } - - FEATURE_EXTRACTOR_MAPS = [ - CENTER_NET_EXTRACTOR_FUNCTION_MAP, - FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP, - SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP - ] - -if tf_version.is_tf1(): - SSD_FEATURE_EXTRACTOR_CLASS_MAP = { - 'ssd_inception_v2': - SSDInceptionV2FeatureExtractor, - 'ssd_inception_v3': - SSDInceptionV3FeatureExtractor, - 'ssd_mobilenet_v1': - SSDMobileNetV1FeatureExtractor, - 'ssd_mobilenet_v1_fpn': - SSDMobileNetV1FpnFeatureExtractor, - 'ssd_mobilenet_v1_ppn': - SSDMobileNetV1PpnFeatureExtractor, - 'ssd_mobilenet_v2': - SSDMobileNetV2FeatureExtractor, - 'ssd_mobilenet_v2_fpn': - SSDMobileNetV2FpnFeatureExtractor, - 'ssd_mobilenet_v2_mnasfpn': - SSDMobileNetV2MnasFPNFeatureExtractor, - 'ssd_mobilenet_v3_large': - SSDMobileNetV3LargeFeatureExtractor, - 'ssd_mobilenet_v3_small': - SSDMobileNetV3SmallFeatureExtractor, - 'ssd_mobilenet_v3_small_pruned': - SSDMobileNetV3SmallPrunedFeatureExtractor, - 'ssd_mobilenet_edgetpu': - SSDMobileNetEdgeTPUFeatureExtractor, - 'ssd_resnet50_v1_fpn': - ssd_resnet_v1_fpn.SSDResnet50V1FpnFeatureExtractor, - 'ssd_resnet101_v1_fpn': - ssd_resnet_v1_fpn.SSDResnet101V1FpnFeatureExtractor, - 'ssd_resnet152_v1_fpn': - ssd_resnet_v1_fpn.SSDResnet152V1FpnFeatureExtractor, - 'ssd_resnet50_v1_ppn': - ssd_resnet_v1_ppn.SSDResnet50V1PpnFeatureExtractor, - 'ssd_resnet101_v1_ppn': - ssd_resnet_v1_ppn.SSDResnet101V1PpnFeatureExtractor, - 'ssd_resnet152_v1_ppn': - ssd_resnet_v1_ppn.SSDResnet152V1PpnFeatureExtractor, - 'embedded_ssd_mobilenet_v1': - EmbeddedSSDMobileNetV1FeatureExtractor, - 'ssd_pnasnet': - SSDPNASNetFeatureExtractor, - 'ssd_mobiledet_cpu': - SSDMobileDetCPUFeatureExtractor, - 'ssd_mobiledet_dsp': - SSDMobileDetDSPFeatureExtractor, - 'ssd_mobiledet_edgetpu': - SSDMobileDetEdgeTPUFeatureExtractor, - 'ssd_mobiledet_gpu': - SSDMobileDetGPUFeatureExtractor, - 'ssd_spaghettinet': - SSDSpaghettinetFeatureExtractor, - } - - FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP = { - 'faster_rcnn_nas': - frcnn_nas.FasterRCNNNASFeatureExtractor, - 'faster_rcnn_pnas': - frcnn_pnas.FasterRCNNPNASFeatureExtractor, - 'faster_rcnn_inception_resnet_v2': - frcnn_inc_res.FasterRCNNInceptionResnetV2FeatureExtractor, - 'faster_rcnn_inception_v2': - frcnn_inc_v2.FasterRCNNInceptionV2FeatureExtractor, - 'faster_rcnn_resnet50': - frcnn_resnet_v1.FasterRCNNResnet50FeatureExtractor, - 'faster_rcnn_resnet101': - frcnn_resnet_v1.FasterRCNNResnet101FeatureExtractor, - 'faster_rcnn_resnet152': - frcnn_resnet_v1.FasterRCNNResnet152FeatureExtractor, - } - - CENTER_NET_EXTRACTOR_FUNCTION_MAP = {} - - FEATURE_EXTRACTOR_MAPS = [ - SSD_FEATURE_EXTRACTOR_CLASS_MAP, - FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP, - CENTER_NET_EXTRACTOR_FUNCTION_MAP - ] - - -def _check_feature_extractor_exists(feature_extractor_type): - feature_extractors = set().union(*FEATURE_EXTRACTOR_MAPS) - if feature_extractor_type not in feature_extractors: - tf_version_str = '2' if tf_version.is_tf2() else '1' - raise ValueError( - '{} is not supported for tf version {}. See `model_builder.py` for ' - 'features extractors compatible with different versions of ' - 'Tensorflow'.format(feature_extractor_type, tf_version_str)) - - -def _build_ssd_feature_extractor(feature_extractor_config, - is_training, - freeze_batchnorm, - reuse_weights=None): - """Builds a ssd_meta_arch.SSDFeatureExtractor based on config. - - Args: - feature_extractor_config: A SSDFeatureExtractor proto config from ssd.proto. - is_training: True if this feature extractor is being built for training. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - reuse_weights: if the feature extractor should reuse weights. - - Returns: - ssd_meta_arch.SSDFeatureExtractor based on config. - - Raises: - ValueError: On invalid feature extractor type. - """ - feature_type = feature_extractor_config.type - depth_multiplier = feature_extractor_config.depth_multiplier - min_depth = feature_extractor_config.min_depth - pad_to_multiple = feature_extractor_config.pad_to_multiple - use_explicit_padding = feature_extractor_config.use_explicit_padding - use_depthwise = feature_extractor_config.use_depthwise - - is_keras = tf_version.is_tf2() - if is_keras: - conv_hyperparams = hyperparams_builder.KerasLayerHyperparams( - feature_extractor_config.conv_hyperparams) - else: - conv_hyperparams = hyperparams_builder.build( - feature_extractor_config.conv_hyperparams, is_training) - override_base_feature_extractor_hyperparams = ( - feature_extractor_config.override_base_feature_extractor_hyperparams) - - if not is_keras and feature_type not in SSD_FEATURE_EXTRACTOR_CLASS_MAP: - raise ValueError('Unknown ssd feature_extractor: {}'.format(feature_type)) - - if is_keras: - feature_extractor_class = SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP[ - feature_type] - else: - feature_extractor_class = SSD_FEATURE_EXTRACTOR_CLASS_MAP[feature_type] - kwargs = { - 'is_training': - is_training, - 'depth_multiplier': - depth_multiplier, - 'min_depth': - min_depth, - 'pad_to_multiple': - pad_to_multiple, - 'use_explicit_padding': - use_explicit_padding, - 'use_depthwise': - use_depthwise, - 'override_base_feature_extractor_hyperparams': - override_base_feature_extractor_hyperparams - } - - if feature_extractor_config.HasField('replace_preprocessor_with_placeholder'): - kwargs.update({ - 'replace_preprocessor_with_placeholder': - feature_extractor_config.replace_preprocessor_with_placeholder - }) - - if feature_extractor_config.HasField('num_layers'): - kwargs.update({'num_layers': feature_extractor_config.num_layers}) - - if is_keras: - kwargs.update({ - 'conv_hyperparams': conv_hyperparams, - 'inplace_batchnorm_update': False, - 'freeze_batchnorm': freeze_batchnorm - }) - else: - kwargs.update({ - 'conv_hyperparams_fn': conv_hyperparams, - 'reuse_weights': reuse_weights, - }) - - - if feature_extractor_config.HasField('spaghettinet_arch_name'): - kwargs.update({ - 'spaghettinet_arch_name': - feature_extractor_config.spaghettinet_arch_name, - }) - - if feature_extractor_config.HasField('fpn'): - kwargs.update({ - 'fpn_min_level': - feature_extractor_config.fpn.min_level, - 'fpn_max_level': - feature_extractor_config.fpn.max_level, - 'additional_layer_depth': - feature_extractor_config.fpn.additional_layer_depth, - }) - - if feature_extractor_config.HasField('bifpn'): - kwargs.update({ - 'bifpn_min_level': - feature_extractor_config.bifpn.min_level, - 'bifpn_max_level': - feature_extractor_config.bifpn.max_level, - 'bifpn_num_iterations': - feature_extractor_config.bifpn.num_iterations, - 'bifpn_num_filters': - feature_extractor_config.bifpn.num_filters, - 'bifpn_combine_method': - feature_extractor_config.bifpn.combine_method, - 'use_native_resize_op': - feature_extractor_config.bifpn.use_native_resize_op, - }) - - return feature_extractor_class(**kwargs) - - -def _build_ssd_model(ssd_config, is_training, add_summaries): - """Builds an SSD detection model based on the model config. - - Args: - ssd_config: A ssd.proto object containing the config for the desired - SSDMetaArch. - is_training: True if this model is being built for training purposes. - add_summaries: Whether to add tf summaries in the model. - Returns: - SSDMetaArch based on the config. - - Raises: - ValueError: If ssd_config.type is not recognized (i.e. not registered in - model_class_map). - """ - num_classes = ssd_config.num_classes - _check_feature_extractor_exists(ssd_config.feature_extractor.type) - - # Feature extractor - feature_extractor = _build_ssd_feature_extractor( - feature_extractor_config=ssd_config.feature_extractor, - freeze_batchnorm=ssd_config.freeze_batchnorm, - is_training=is_training) - - box_coder = box_coder_builder.build(ssd_config.box_coder) - matcher = matcher_builder.build(ssd_config.matcher) - region_similarity_calculator = sim_calc.build( - ssd_config.similarity_calculator) - encode_background_as_zeros = ssd_config.encode_background_as_zeros - negative_class_weight = ssd_config.negative_class_weight - anchor_generator = anchor_generator_builder.build( - ssd_config.anchor_generator) - if feature_extractor.is_keras_model: - ssd_box_predictor = box_predictor_builder.build_keras( - hyperparams_fn=hyperparams_builder.KerasLayerHyperparams, - freeze_batchnorm=ssd_config.freeze_batchnorm, - inplace_batchnorm_update=False, - num_predictions_per_location_list=anchor_generator - .num_anchors_per_location(), - box_predictor_config=ssd_config.box_predictor, - is_training=is_training, - num_classes=num_classes, - add_background_class=ssd_config.add_background_class) - else: - ssd_box_predictor = box_predictor_builder.build( - hyperparams_builder.build, ssd_config.box_predictor, is_training, - num_classes, ssd_config.add_background_class) - image_resizer_fn = image_resizer_builder.build(ssd_config.image_resizer) - non_max_suppression_fn, score_conversion_fn = post_processing_builder.build( - ssd_config.post_processing) - (classification_loss, localization_loss, classification_weight, - localization_weight, hard_example_miner, random_example_sampler, - expected_loss_weights_fn) = losses_builder.build(ssd_config.loss) - normalize_loss_by_num_matches = ssd_config.normalize_loss_by_num_matches - normalize_loc_loss_by_codesize = ssd_config.normalize_loc_loss_by_codesize - - equalization_loss_config = ops.EqualizationLossConfig( - weight=ssd_config.loss.equalization_loss.weight, - exclude_prefixes=ssd_config.loss.equalization_loss.exclude_prefixes) - - target_assigner_instance = target_assigner.TargetAssigner( - region_similarity_calculator, - matcher, - box_coder, - negative_class_weight=negative_class_weight) - - ssd_meta_arch_fn = ssd_meta_arch.SSDMetaArch - kwargs = {} - - return ssd_meta_arch_fn( - is_training=is_training, - anchor_generator=anchor_generator, - box_predictor=ssd_box_predictor, - box_coder=box_coder, - feature_extractor=feature_extractor, - encode_background_as_zeros=encode_background_as_zeros, - image_resizer_fn=image_resizer_fn, - non_max_suppression_fn=non_max_suppression_fn, - score_conversion_fn=score_conversion_fn, - classification_loss=classification_loss, - localization_loss=localization_loss, - classification_loss_weight=classification_weight, - localization_loss_weight=localization_weight, - normalize_loss_by_num_matches=normalize_loss_by_num_matches, - hard_example_miner=hard_example_miner, - target_assigner_instance=target_assigner_instance, - add_summaries=add_summaries, - normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize, - freeze_batchnorm=ssd_config.freeze_batchnorm, - inplace_batchnorm_update=ssd_config.inplace_batchnorm_update, - add_background_class=ssd_config.add_background_class, - explicit_background_class=ssd_config.explicit_background_class, - random_example_sampler=random_example_sampler, - expected_loss_weights_fn=expected_loss_weights_fn, - use_confidences_as_targets=ssd_config.use_confidences_as_targets, - implicit_example_weight=ssd_config.implicit_example_weight, - equalization_loss_config=equalization_loss_config, - return_raw_detections_during_predict=( - ssd_config.return_raw_detections_during_predict), - **kwargs) - - -def _build_faster_rcnn_feature_extractor( - feature_extractor_config, is_training, reuse_weights=True, - inplace_batchnorm_update=False): - """Builds a faster_rcnn_meta_arch.FasterRCNNFeatureExtractor based on config. - - Args: - feature_extractor_config: A FasterRcnnFeatureExtractor proto config from - faster_rcnn.proto. - is_training: True if this feature extractor is being built for training. - reuse_weights: if the feature extractor should reuse weights. - inplace_batchnorm_update: Whether to update batch_norm inplace during - training. This is required for batch norm to work correctly on TPUs. When - this is false, user must add a control dependency on - tf.GraphKeys.UPDATE_OPS for train/loss op in order to update the batch - norm moving average parameters. - - Returns: - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor based on config. - - Raises: - ValueError: On invalid feature extractor type. - """ - if inplace_batchnorm_update: - raise ValueError('inplace batchnorm updates not supported.') - feature_type = feature_extractor_config.type - first_stage_features_stride = ( - feature_extractor_config.first_stage_features_stride) - batch_norm_trainable = feature_extractor_config.batch_norm_trainable - - if feature_type not in FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP: - raise ValueError('Unknown Faster R-CNN feature_extractor: {}'.format( - feature_type)) - feature_extractor_class = FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP[ - feature_type] - return feature_extractor_class( - is_training, first_stage_features_stride, - batch_norm_trainable, reuse_weights=reuse_weights) - - -def _build_faster_rcnn_keras_feature_extractor( - feature_extractor_config, is_training, - inplace_batchnorm_update=False): - """Builds a faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor from config. - - Args: - feature_extractor_config: A FasterRcnnFeatureExtractor proto config from - faster_rcnn.proto. - is_training: True if this feature extractor is being built for training. - inplace_batchnorm_update: Whether to update batch_norm inplace during - training. This is required for batch norm to work correctly on TPUs. When - this is false, user must add a control dependency on - tf.GraphKeys.UPDATE_OPS for train/loss op in order to update the batch - norm moving average parameters. - - Returns: - faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor based on config. - - Raises: - ValueError: On invalid feature extractor type. - """ - if inplace_batchnorm_update: - raise ValueError('inplace batchnorm updates not supported.') - feature_type = feature_extractor_config.type - first_stage_features_stride = ( - feature_extractor_config.first_stage_features_stride) - batch_norm_trainable = feature_extractor_config.batch_norm_trainable - - if feature_type not in FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP: - raise ValueError('Unknown Faster R-CNN feature_extractor: {}'.format( - feature_type)) - feature_extractor_class = FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP[ - feature_type] - - kwargs = {} - - if feature_extractor_config.HasField('conv_hyperparams'): - kwargs.update({ - 'conv_hyperparams': - hyperparams_builder.KerasLayerHyperparams( - feature_extractor_config.conv_hyperparams), - 'override_base_feature_extractor_hyperparams': - feature_extractor_config.override_base_feature_extractor_hyperparams - }) - - if feature_extractor_config.HasField('fpn'): - kwargs.update({ - 'fpn_min_level': - feature_extractor_config.fpn.min_level, - 'fpn_max_level': - feature_extractor_config.fpn.max_level, - 'additional_layer_depth': - feature_extractor_config.fpn.additional_layer_depth, - }) - - return feature_extractor_class( - is_training, first_stage_features_stride, - batch_norm_trainable, **kwargs) - - -def _build_faster_rcnn_model(frcnn_config, is_training, add_summaries): - """Builds a Faster R-CNN or R-FCN detection model based on the model config. - - Builds R-FCN model if the second_stage_box_predictor in the config is of type - `rfcn_box_predictor` else builds a Faster R-CNN model. - - Args: - frcnn_config: A faster_rcnn.proto object containing the config for the - desired FasterRCNNMetaArch or RFCNMetaArch. - is_training: True if this model is being built for training purposes. - add_summaries: Whether to add tf summaries in the model. - - Returns: - FasterRCNNMetaArch based on the config. - - Raises: - ValueError: If frcnn_config.type is not recognized (i.e. not registered in - model_class_map). - """ - num_classes = frcnn_config.num_classes - image_resizer_fn = image_resizer_builder.build(frcnn_config.image_resizer) - _check_feature_extractor_exists(frcnn_config.feature_extractor.type) - is_keras = tf_version.is_tf2() - - if is_keras: - feature_extractor = _build_faster_rcnn_keras_feature_extractor( - frcnn_config.feature_extractor, is_training, - inplace_batchnorm_update=frcnn_config.inplace_batchnorm_update) - else: - feature_extractor = _build_faster_rcnn_feature_extractor( - frcnn_config.feature_extractor, is_training, - inplace_batchnorm_update=frcnn_config.inplace_batchnorm_update) - - number_of_stages = frcnn_config.number_of_stages - first_stage_anchor_generator = anchor_generator_builder.build( - frcnn_config.first_stage_anchor_generator) - - first_stage_target_assigner = target_assigner.create_target_assigner( - 'FasterRCNN', - 'proposal', - use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher) - first_stage_atrous_rate = frcnn_config.first_stage_atrous_rate - if is_keras: - first_stage_box_predictor_arg_scope_fn = ( - hyperparams_builder.KerasLayerHyperparams( - frcnn_config.first_stage_box_predictor_conv_hyperparams)) - else: - first_stage_box_predictor_arg_scope_fn = hyperparams_builder.build( - frcnn_config.first_stage_box_predictor_conv_hyperparams, is_training) - first_stage_box_predictor_kernel_size = ( - frcnn_config.first_stage_box_predictor_kernel_size) - first_stage_box_predictor_depth = frcnn_config.first_stage_box_predictor_depth - first_stage_minibatch_size = frcnn_config.first_stage_minibatch_size - use_static_shapes = frcnn_config.use_static_shapes and ( - frcnn_config.use_static_shapes_for_eval or is_training) - first_stage_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=frcnn_config.first_stage_positive_balance_fraction, - is_static=(frcnn_config.use_static_balanced_label_sampler and - use_static_shapes)) - first_stage_max_proposals = frcnn_config.first_stage_max_proposals - if (frcnn_config.first_stage_nms_iou_threshold < 0 or - frcnn_config.first_stage_nms_iou_threshold > 1.0): - raise ValueError('iou_threshold not in [0, 1.0].') - if (is_training and frcnn_config.second_stage_batch_size > - first_stage_max_proposals): - raise ValueError('second_stage_batch_size should be no greater than ' - 'first_stage_max_proposals.') - first_stage_non_max_suppression_fn = functools.partial( - post_processing.batch_multiclass_non_max_suppression, - score_thresh=frcnn_config.first_stage_nms_score_threshold, - iou_thresh=frcnn_config.first_stage_nms_iou_threshold, - max_size_per_class=frcnn_config.first_stage_max_proposals, - max_total_size=frcnn_config.first_stage_max_proposals, - use_static_shapes=use_static_shapes, - use_partitioned_nms=frcnn_config.use_partitioned_nms_in_first_stage, - use_combined_nms=frcnn_config.use_combined_nms_in_first_stage) - first_stage_loc_loss_weight = ( - frcnn_config.first_stage_localization_loss_weight) - first_stage_obj_loss_weight = frcnn_config.first_stage_objectness_loss_weight - - initial_crop_size = frcnn_config.initial_crop_size - maxpool_kernel_size = frcnn_config.maxpool_kernel_size - maxpool_stride = frcnn_config.maxpool_stride - - second_stage_target_assigner = target_assigner.create_target_assigner( - 'FasterRCNN', - 'detection', - use_matmul_gather=frcnn_config.use_matmul_gather_in_matcher) - if is_keras: - second_stage_box_predictor = box_predictor_builder.build_keras( - hyperparams_builder.KerasLayerHyperparams, - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[1], - box_predictor_config=frcnn_config.second_stage_box_predictor, - is_training=is_training, - num_classes=num_classes) - else: - second_stage_box_predictor = box_predictor_builder.build( - hyperparams_builder.build, - frcnn_config.second_stage_box_predictor, - is_training=is_training, - num_classes=num_classes) - second_stage_batch_size = frcnn_config.second_stage_batch_size - second_stage_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=frcnn_config.second_stage_balance_fraction, - is_static=(frcnn_config.use_static_balanced_label_sampler and - use_static_shapes)) - (second_stage_non_max_suppression_fn, second_stage_score_conversion_fn - ) = post_processing_builder.build(frcnn_config.second_stage_post_processing) - second_stage_localization_loss_weight = ( - frcnn_config.second_stage_localization_loss_weight) - second_stage_classification_loss = ( - losses_builder.build_faster_rcnn_classification_loss( - frcnn_config.second_stage_classification_loss)) - second_stage_classification_loss_weight = ( - frcnn_config.second_stage_classification_loss_weight) - second_stage_mask_prediction_loss_weight = ( - frcnn_config.second_stage_mask_prediction_loss_weight) - - hard_example_miner = None - if frcnn_config.HasField('hard_example_miner'): - hard_example_miner = losses_builder.build_hard_example_miner( - frcnn_config.hard_example_miner, - second_stage_classification_loss_weight, - second_stage_localization_loss_weight) - - crop_and_resize_fn = ( - spatial_ops.multilevel_matmul_crop_and_resize - if frcnn_config.use_matmul_crop_and_resize - else spatial_ops.multilevel_native_crop_and_resize) - clip_anchors_to_image = ( - frcnn_config.clip_anchors_to_image) - - common_kwargs = { - 'is_training': - is_training, - 'num_classes': - num_classes, - 'image_resizer_fn': - image_resizer_fn, - 'feature_extractor': - feature_extractor, - 'number_of_stages': - number_of_stages, - 'first_stage_anchor_generator': - first_stage_anchor_generator, - 'first_stage_target_assigner': - first_stage_target_assigner, - 'first_stage_atrous_rate': - first_stage_atrous_rate, - 'first_stage_box_predictor_arg_scope_fn': - first_stage_box_predictor_arg_scope_fn, - 'first_stage_box_predictor_kernel_size': - first_stage_box_predictor_kernel_size, - 'first_stage_box_predictor_depth': - first_stage_box_predictor_depth, - 'first_stage_minibatch_size': - first_stage_minibatch_size, - 'first_stage_sampler': - first_stage_sampler, - 'first_stage_non_max_suppression_fn': - first_stage_non_max_suppression_fn, - 'first_stage_max_proposals': - first_stage_max_proposals, - 'first_stage_localization_loss_weight': - first_stage_loc_loss_weight, - 'first_stage_objectness_loss_weight': - first_stage_obj_loss_weight, - 'second_stage_target_assigner': - second_stage_target_assigner, - 'second_stage_batch_size': - second_stage_batch_size, - 'second_stage_sampler': - second_stage_sampler, - 'second_stage_non_max_suppression_fn': - second_stage_non_max_suppression_fn, - 'second_stage_score_conversion_fn': - second_stage_score_conversion_fn, - 'second_stage_localization_loss_weight': - second_stage_localization_loss_weight, - 'second_stage_classification_loss': - second_stage_classification_loss, - 'second_stage_classification_loss_weight': - second_stage_classification_loss_weight, - 'hard_example_miner': - hard_example_miner, - 'add_summaries': - add_summaries, - 'crop_and_resize_fn': - crop_and_resize_fn, - 'clip_anchors_to_image': - clip_anchors_to_image, - 'use_static_shapes': - use_static_shapes, - 'resize_masks': - frcnn_config.resize_masks, - 'return_raw_detections_during_predict': - frcnn_config.return_raw_detections_during_predict, - 'output_final_box_features': - frcnn_config.output_final_box_features, - 'output_final_box_rpn_features': - frcnn_config.output_final_box_rpn_features, - } - - if ((not is_keras and isinstance(second_stage_box_predictor, - rfcn_box_predictor.RfcnBoxPredictor)) or - (is_keras and - isinstance(second_stage_box_predictor, - rfcn_keras_box_predictor.RfcnKerasBoxPredictor))): - return rfcn_meta_arch.RFCNMetaArch( - second_stage_rfcn_box_predictor=second_stage_box_predictor, - **common_kwargs) - elif frcnn_config.HasField('context_config'): - context_config = frcnn_config.context_config - common_kwargs.update({ - 'attention_bottleneck_dimension': - context_config.attention_bottleneck_dimension, - 'attention_temperature': - context_config.attention_temperature, - 'use_self_attention': - context_config.use_self_attention, - 'use_long_term_attention': - context_config.use_long_term_attention, - 'self_attention_in_sequence': - context_config.self_attention_in_sequence, - 'num_attention_heads': - context_config.num_attention_heads, - 'num_attention_layers': - context_config.num_attention_layers, - 'attention_position': - context_config.attention_position - }) - return context_rcnn_meta_arch.ContextRCNNMetaArch( - initial_crop_size=initial_crop_size, - maxpool_kernel_size=maxpool_kernel_size, - maxpool_stride=maxpool_stride, - second_stage_mask_rcnn_box_predictor=second_stage_box_predictor, - second_stage_mask_prediction_loss_weight=( - second_stage_mask_prediction_loss_weight), - **common_kwargs) - else: - return faster_rcnn_meta_arch.FasterRCNNMetaArch( - initial_crop_size=initial_crop_size, - maxpool_kernel_size=maxpool_kernel_size, - maxpool_stride=maxpool_stride, - second_stage_mask_rcnn_box_predictor=second_stage_box_predictor, - second_stage_mask_prediction_loss_weight=( - second_stage_mask_prediction_loss_weight), - **common_kwargs) - -EXPERIMENTAL_META_ARCH_BUILDER_MAP = { -} - - -def _build_experimental_model(config, is_training, add_summaries=True): - return EXPERIMENTAL_META_ARCH_BUILDER_MAP[config.name]( - is_training, add_summaries) - - -# The class ID in the groundtruth/model architecture is usually 0-based while -# the ID in the label map is 1-based. The offset is used to convert between the -# the two. -CLASS_ID_OFFSET = 1 -KEYPOINT_STD_DEV_DEFAULT = 1.0 - - -def keypoint_proto_to_params(kp_config, keypoint_map_dict): - """Converts CenterNet.KeypointEstimation proto to parameter namedtuple.""" - label_map_item = keypoint_map_dict[kp_config.keypoint_class_name] - - classification_loss, localization_loss, _, _, _, _, _ = ( - losses_builder.build(kp_config.loss)) - - keypoint_indices = [ - keypoint.id for keypoint in label_map_item.keypoints - ] - keypoint_labels = [ - keypoint.label for keypoint in label_map_item.keypoints - ] - keypoint_std_dev_dict = { - label: KEYPOINT_STD_DEV_DEFAULT for label in keypoint_labels - } - if kp_config.keypoint_label_to_std: - for label, value in kp_config.keypoint_label_to_std.items(): - keypoint_std_dev_dict[label] = value - keypoint_std_dev = [keypoint_std_dev_dict[label] for label in keypoint_labels] - if kp_config.HasField('heatmap_head_params'): - heatmap_head_num_filters = list(kp_config.heatmap_head_params.num_filters) - heatmap_head_kernel_sizes = list(kp_config.heatmap_head_params.kernel_sizes) - else: - heatmap_head_num_filters = [256] - heatmap_head_kernel_sizes = [3] - if kp_config.HasField('offset_head_params'): - offset_head_num_filters = list(kp_config.offset_head_params.num_filters) - offset_head_kernel_sizes = list(kp_config.offset_head_params.kernel_sizes) - else: - offset_head_num_filters = [256] - offset_head_kernel_sizes = [3] - if kp_config.HasField('regress_head_params'): - regress_head_num_filters = list(kp_config.regress_head_params.num_filters) - regress_head_kernel_sizes = list( - kp_config.regress_head_params.kernel_sizes) - else: - regress_head_num_filters = [256] - regress_head_kernel_sizes = [3] - return center_net_meta_arch.KeypointEstimationParams( - task_name=kp_config.task_name, - class_id=label_map_item.id - CLASS_ID_OFFSET, - keypoint_indices=keypoint_indices, - classification_loss=classification_loss, - localization_loss=localization_loss, - keypoint_labels=keypoint_labels, - keypoint_std_dev=keypoint_std_dev, - task_loss_weight=kp_config.task_loss_weight, - keypoint_regression_loss_weight=kp_config.keypoint_regression_loss_weight, - keypoint_heatmap_loss_weight=kp_config.keypoint_heatmap_loss_weight, - keypoint_offset_loss_weight=kp_config.keypoint_offset_loss_weight, - heatmap_bias_init=kp_config.heatmap_bias_init, - keypoint_candidate_score_threshold=( - kp_config.keypoint_candidate_score_threshold), - num_candidates_per_keypoint=kp_config.num_candidates_per_keypoint, - peak_max_pool_kernel_size=kp_config.peak_max_pool_kernel_size, - unmatched_keypoint_score=kp_config.unmatched_keypoint_score, - box_scale=kp_config.box_scale, - candidate_search_scale=kp_config.candidate_search_scale, - candidate_ranking_mode=kp_config.candidate_ranking_mode, - offset_peak_radius=kp_config.offset_peak_radius, - per_keypoint_offset=kp_config.per_keypoint_offset, - predict_depth=kp_config.predict_depth, - per_keypoint_depth=kp_config.per_keypoint_depth, - keypoint_depth_loss_weight=kp_config.keypoint_depth_loss_weight, - score_distance_offset=kp_config.score_distance_offset, - clip_out_of_frame_keypoints=kp_config.clip_out_of_frame_keypoints, - rescore_instances=kp_config.rescore_instances, - heatmap_head_num_filters=heatmap_head_num_filters, - heatmap_head_kernel_sizes=heatmap_head_kernel_sizes, - offset_head_num_filters=offset_head_num_filters, - offset_head_kernel_sizes=offset_head_kernel_sizes, - regress_head_num_filters=regress_head_num_filters, - regress_head_kernel_sizes=regress_head_kernel_sizes, - score_distance_multiplier=kp_config.score_distance_multiplier, - std_dev_multiplier=kp_config.std_dev_multiplier, - rescoring_threshold=kp_config.rescoring_threshold, - gaussian_denom_ratio=kp_config.gaussian_denom_ratio, - argmax_postprocessing=kp_config.argmax_postprocessing) - - -def object_detection_proto_to_params(od_config): - """Converts CenterNet.ObjectDetection proto to parameter namedtuple.""" - loss = losses_pb2.Loss() - # Add dummy classification loss to avoid the loss_builder throwing error. - # TODO(yuhuic): update the loss builder to take the classification loss - # directly. - loss.classification_loss.weighted_sigmoid.CopyFrom( - losses_pb2.WeightedSigmoidClassificationLoss()) - loss.localization_loss.CopyFrom(od_config.localization_loss) - _, localization_loss, _, _, _, _, _ = (losses_builder.build(loss)) - if od_config.HasField('scale_head_params'): - scale_head_num_filters = list(od_config.scale_head_params.num_filters) - scale_head_kernel_sizes = list(od_config.scale_head_params.kernel_sizes) - else: - scale_head_num_filters = [256] - scale_head_kernel_sizes = [3] - if od_config.HasField('offset_head_params'): - offset_head_num_filters = list(od_config.offset_head_params.num_filters) - offset_head_kernel_sizes = list(od_config.offset_head_params.kernel_sizes) - else: - offset_head_num_filters = [256] - offset_head_kernel_sizes = [3] - return center_net_meta_arch.ObjectDetectionParams( - localization_loss=localization_loss, - scale_loss_weight=od_config.scale_loss_weight, - offset_loss_weight=od_config.offset_loss_weight, - task_loss_weight=od_config.task_loss_weight, - scale_head_num_filters=scale_head_num_filters, - scale_head_kernel_sizes=scale_head_kernel_sizes, - offset_head_num_filters=offset_head_num_filters, - offset_head_kernel_sizes=offset_head_kernel_sizes) - - -def object_center_proto_to_params(oc_config): - """Converts CenterNet.ObjectCenter proto to parameter namedtuple.""" - loss = losses_pb2.Loss() - # Add dummy localization loss to avoid the loss_builder throwing error. - # TODO(yuhuic): update the loss builder to take the localization loss - # directly. - loss.localization_loss.weighted_l2.CopyFrom( - losses_pb2.WeightedL2LocalizationLoss()) - loss.classification_loss.CopyFrom(oc_config.classification_loss) - classification_loss, _, _, _, _, _, _ = (losses_builder.build(loss)) - keypoint_weights_for_center = [] - if oc_config.keypoint_weights_for_center: - keypoint_weights_for_center = list(oc_config.keypoint_weights_for_center) - - if oc_config.HasField('center_head_params'): - center_head_num_filters = list(oc_config.center_head_params.num_filters) - center_head_kernel_sizes = list(oc_config.center_head_params.kernel_sizes) - else: - center_head_num_filters = [256] - center_head_kernel_sizes = [3] - return center_net_meta_arch.ObjectCenterParams( - classification_loss=classification_loss, - object_center_loss_weight=oc_config.object_center_loss_weight, - heatmap_bias_init=oc_config.heatmap_bias_init, - min_box_overlap_iou=oc_config.min_box_overlap_iou, - max_box_predictions=oc_config.max_box_predictions, - use_labeled_classes=oc_config.use_labeled_classes, - keypoint_weights_for_center=keypoint_weights_for_center, - center_head_num_filters=center_head_num_filters, - center_head_kernel_sizes=center_head_kernel_sizes, - peak_max_pool_kernel_size=oc_config.peak_max_pool_kernel_size) - - -def mask_proto_to_params(mask_config): - """Converts CenterNet.MaskEstimation proto to parameter namedtuple.""" - loss = losses_pb2.Loss() - # Add dummy localization loss to avoid the loss_builder throwing error. - loss.localization_loss.weighted_l2.CopyFrom( - losses_pb2.WeightedL2LocalizationLoss()) - loss.classification_loss.CopyFrom(mask_config.classification_loss) - classification_loss, _, _, _, _, _, _ = (losses_builder.build(loss)) - if mask_config.HasField('mask_head_params'): - mask_head_num_filters = list(mask_config.mask_head_params.num_filters) - mask_head_kernel_sizes = list(mask_config.mask_head_params.kernel_sizes) - else: - mask_head_num_filters = [256] - mask_head_kernel_sizes = [3] - return center_net_meta_arch.MaskParams( - classification_loss=classification_loss, - task_loss_weight=mask_config.task_loss_weight, - mask_height=mask_config.mask_height, - mask_width=mask_config.mask_width, - score_threshold=mask_config.score_threshold, - heatmap_bias_init=mask_config.heatmap_bias_init, - mask_head_num_filters=mask_head_num_filters, - mask_head_kernel_sizes=mask_head_kernel_sizes) - - -def densepose_proto_to_params(densepose_config): - """Converts CenterNet.DensePoseEstimation proto to parameter namedtuple.""" - classification_loss, localization_loss, _, _, _, _, _ = ( - losses_builder.build(densepose_config.loss)) - return center_net_meta_arch.DensePoseParams( - class_id=densepose_config.class_id, - classification_loss=classification_loss, - localization_loss=localization_loss, - part_loss_weight=densepose_config.part_loss_weight, - coordinate_loss_weight=densepose_config.coordinate_loss_weight, - num_parts=densepose_config.num_parts, - task_loss_weight=densepose_config.task_loss_weight, - upsample_to_input_res=densepose_config.upsample_to_input_res, - heatmap_bias_init=densepose_config.heatmap_bias_init) - - -def tracking_proto_to_params(tracking_config): - """Converts CenterNet.TrackEstimation proto to parameter namedtuple.""" - loss = losses_pb2.Loss() - # Add dummy localization loss to avoid the loss_builder throwing error. - # TODO(yuhuic): update the loss builder to take the localization loss - # directly. - loss.localization_loss.weighted_l2.CopyFrom( - losses_pb2.WeightedL2LocalizationLoss()) - loss.classification_loss.CopyFrom(tracking_config.classification_loss) - classification_loss, _, _, _, _, _, _ = losses_builder.build(loss) - return center_net_meta_arch.TrackParams( - num_track_ids=tracking_config.num_track_ids, - reid_embed_size=tracking_config.reid_embed_size, - classification_loss=classification_loss, - num_fc_layers=tracking_config.num_fc_layers, - task_loss_weight=tracking_config.task_loss_weight) - - -def temporal_offset_proto_to_params(temporal_offset_config): - """Converts CenterNet.TemporalOffsetEstimation proto to param-tuple.""" - loss = losses_pb2.Loss() - # Add dummy classification loss to avoid the loss_builder throwing error. - # TODO(yuhuic): update the loss builder to take the classification loss - # directly. - loss.classification_loss.weighted_sigmoid.CopyFrom( - losses_pb2.WeightedSigmoidClassificationLoss()) - loss.localization_loss.CopyFrom(temporal_offset_config.localization_loss) - _, localization_loss, _, _, _, _, _ = losses_builder.build(loss) - return center_net_meta_arch.TemporalOffsetParams( - localization_loss=localization_loss, - task_loss_weight=temporal_offset_config.task_loss_weight) - - -def _build_center_net_model(center_net_config, is_training, add_summaries): - """Build a CenterNet detection model. - - Args: - center_net_config: A CenterNet proto object with model configuration. - is_training: True if this model is being built for training purposes. - add_summaries: Whether to add tf summaries in the model. - - Returns: - CenterNetMetaArch based on the config. - - """ - - image_resizer_fn = image_resizer_builder.build( - center_net_config.image_resizer) - _check_feature_extractor_exists(center_net_config.feature_extractor.type) - feature_extractor = _build_center_net_feature_extractor( - center_net_config.feature_extractor, is_training) - object_center_params = object_center_proto_to_params( - center_net_config.object_center_params) - - object_detection_params = None - if center_net_config.HasField('object_detection_task'): - object_detection_params = object_detection_proto_to_params( - center_net_config.object_detection_task) - - if center_net_config.HasField('deepmac_mask_estimation'): - logging.warn(('Building experimental DeepMAC meta-arch.' - ' Some features may be omitted.')) - deepmac_params = deepmac_meta_arch.deepmac_proto_to_params( - center_net_config.deepmac_mask_estimation) - return deepmac_meta_arch.DeepMACMetaArch( - is_training=is_training, - add_summaries=add_summaries, - num_classes=center_net_config.num_classes, - feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=object_center_params, - object_detection_params=object_detection_params, - deepmac_params=deepmac_params) - - keypoint_params_dict = None - if center_net_config.keypoint_estimation_task: - label_map_proto = label_map_util.load_labelmap( - center_net_config.keypoint_label_map_path) - keypoint_map_dict = { - item.name: item for item in label_map_proto.item if item.keypoints - } - keypoint_params_dict = {} - keypoint_class_id_set = set() - all_keypoint_indices = [] - for task in center_net_config.keypoint_estimation_task: - kp_params = keypoint_proto_to_params(task, keypoint_map_dict) - keypoint_params_dict[task.task_name] = kp_params - all_keypoint_indices.extend(kp_params.keypoint_indices) - if kp_params.class_id in keypoint_class_id_set: - raise ValueError(('Multiple keypoint tasks map to the same class id is ' - 'not allowed: %d' % kp_params.class_id)) - else: - keypoint_class_id_set.add(kp_params.class_id) - if len(all_keypoint_indices) > len(set(all_keypoint_indices)): - raise ValueError('Some keypoint indices are used more than once.') - - mask_params = None - if center_net_config.HasField('mask_estimation_task'): - mask_params = mask_proto_to_params(center_net_config.mask_estimation_task) - - densepose_params = None - if center_net_config.HasField('densepose_estimation_task'): - densepose_params = densepose_proto_to_params( - center_net_config.densepose_estimation_task) - - track_params = None - if center_net_config.HasField('track_estimation_task'): - track_params = tracking_proto_to_params( - center_net_config.track_estimation_task) - - temporal_offset_params = None - if center_net_config.HasField('temporal_offset_task'): - temporal_offset_params = temporal_offset_proto_to_params( - center_net_config.temporal_offset_task) - non_max_suppression_fn = None - if center_net_config.HasField('post_processing'): - non_max_suppression_fn, _ = post_processing_builder.build( - center_net_config.post_processing) - - return center_net_meta_arch.CenterNetMetaArch( - is_training=is_training, - add_summaries=add_summaries, - num_classes=center_net_config.num_classes, - feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=object_center_params, - object_detection_params=object_detection_params, - keypoint_params_dict=keypoint_params_dict, - mask_params=mask_params, - densepose_params=densepose_params, - track_params=track_params, - temporal_offset_params=temporal_offset_params, - use_depthwise=center_net_config.use_depthwise, - compute_heatmap_sparse=center_net_config.compute_heatmap_sparse, - non_max_suppression_fn=non_max_suppression_fn, - output_prediction_dict=center_net_config.output_prediction_dict) - - -def _build_center_net_feature_extractor(feature_extractor_config, is_training): - """Build a CenterNet feature extractor from the given config.""" - - if feature_extractor_config.type not in CENTER_NET_EXTRACTOR_FUNCTION_MAP: - raise ValueError('\'{}\' is not a known CenterNet feature extractor type' - .format(feature_extractor_config.type)) - # For backwards compatibility: - use_separable_conv = ( - feature_extractor_config.use_separable_conv or - feature_extractor_config.type == 'mobilenet_v2_fpn_sep_conv') - kwargs = { - 'channel_means': - list(feature_extractor_config.channel_means), - 'channel_stds': - list(feature_extractor_config.channel_stds), - 'bgr_ordering': - feature_extractor_config.bgr_ordering, - } - if feature_extractor_config.HasField('depth_multiplier'): - kwargs.update({ - 'depth_multiplier': feature_extractor_config.depth_multiplier, - }) - if feature_extractor_config.HasField('use_separable_conv'): - kwargs.update({ - 'use_separable_conv': use_separable_conv, - }) - if feature_extractor_config.HasField('upsampling_interpolation'): - kwargs.update({ - 'upsampling_interpolation': - feature_extractor_config.upsampling_interpolation, - }) - if feature_extractor_config.HasField('use_depthwise'): - kwargs.update({ - 'use_depthwise': feature_extractor_config.use_depthwise, - }) - - - return CENTER_NET_EXTRACTOR_FUNCTION_MAP[feature_extractor_config.type]( - **kwargs) - - -META_ARCH_BUILDER_MAP = { - 'ssd': _build_ssd_model, - 'faster_rcnn': _build_faster_rcnn_model, - 'experimental_model': _build_experimental_model, - 'center_net': _build_center_net_model -} - - -def build(model_config, is_training, add_summaries=True): - """Builds a DetectionModel based on the model config. - - Args: - model_config: A model.proto object containing the config for the desired - DetectionModel. - is_training: True if this model is being built for training purposes. - add_summaries: Whether to add tensorflow summaries in the model graph. - Returns: - DetectionModel based on the config. - - Raises: - ValueError: On invalid meta architecture or model. - """ - if not isinstance(model_config, model_pb2.DetectionModel): - raise ValueError('model_config not of type model_pb2.DetectionModel.') - - meta_architecture = model_config.WhichOneof('model') - - if meta_architecture not in META_ARCH_BUILDER_MAP: - raise ValueError('Unknown meta architecture: {}'.format(meta_architecture)) - else: - build_func = META_ARCH_BUILDER_MAP[meta_architecture] - return build_func(getattr(model_config, meta_architecture), is_training, - add_summaries) diff --git a/research/object_detection/builders/model_builder_test.py b/research/object_detection/builders/model_builder_test.py deleted file mode 100644 index fc6942acfd1..00000000000 --- a/research/object_detection/builders/model_builder_test.py +++ /dev/null @@ -1,355 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.models.model_builder.""" - -from absl.testing import parameterized - -from google.protobuf import text_format -from object_detection.builders import model_builder -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.meta_architectures import rfcn_meta_arch -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.protos import hyperparams_pb2 -from object_detection.protos import losses_pb2 -from object_detection.protos import model_pb2 -from object_detection.utils import test_case - - -class ModelBuilderTest(test_case.TestCase, parameterized.TestCase): - - def default_ssd_feature_extractor(self): - raise NotImplementedError - - def default_faster_rcnn_feature_extractor(self): - raise NotImplementedError - - def ssd_feature_extractors(self): - raise NotImplementedError - - def get_override_base_feature_extractor_hyperparams(self, extractor_type): - raise NotImplementedError - - def faster_rcnn_feature_extractors(self): - raise NotImplementedError - - def create_model(self, model_config, is_training=True): - """Builds a DetectionModel based on the model config. - - Args: - model_config: A model.proto object containing the config for the desired - DetectionModel. - is_training: True if this model is being built for training purposes. - - Returns: - DetectionModel based on the config. - """ - return model_builder.build(model_config, is_training=is_training) - - def create_default_ssd_model_proto(self): - """Creates a DetectionModel proto with ssd model fields populated.""" - model_text_proto = """ - ssd { - feature_extractor { - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - box_coder { - faster_rcnn_box_coder { - } - } - matcher { - argmax_matcher { - } - } - similarity_calculator { - iou_similarity { - } - } - anchor_generator { - ssd_anchor_generator { - aspect_ratios: 1.0 - } - } - image_resizer { - fixed_shape_resizer { - height: 320 - width: 320 - } - } - box_predictor { - convolutional_box_predictor { - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - } - loss { - classification_loss { - weighted_softmax { - } - } - localization_loss { - weighted_smooth_l1 { - } - } - } - }""" - model_proto = model_pb2.DetectionModel() - text_format.Merge(model_text_proto, model_proto) - model_proto.ssd.feature_extractor.type = (self. - default_ssd_feature_extractor()) - return model_proto - - def create_default_faster_rcnn_model_proto(self): - """Creates a DetectionModel proto with FasterRCNN model fields populated.""" - model_text_proto = """ - faster_rcnn { - inplace_batchnorm_update: false - num_classes: 3 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 600 - max_dimension: 1024 - } - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - conv_hyperparams { - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - } - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.01 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - }""" - model_proto = model_pb2.DetectionModel() - text_format.Merge(model_text_proto, model_proto) - (model_proto.faster_rcnn.feature_extractor.type - ) = self.default_faster_rcnn_feature_extractor() - return model_proto - - def test_create_ssd_models_from_config(self): - model_proto = self.create_default_ssd_model_proto() - for extractor_type, extractor_class in self.ssd_feature_extractors().items( - ): - model_proto.ssd.feature_extractor.type = extractor_type - model_proto.ssd.feature_extractor.override_base_feature_extractor_hyperparams = ( - self.get_override_base_feature_extractor_hyperparams(extractor_type)) - model = model_builder.build(model_proto, is_training=True) - self.assertIsInstance(model, ssd_meta_arch.SSDMetaArch) - self.assertIsInstance(model._feature_extractor, extractor_class) - - def test_create_ssd_fpn_model_from_config(self): - model_proto = self.create_default_ssd_model_proto() - model_proto.ssd.feature_extractor.fpn.min_level = 3 - model_proto.ssd.feature_extractor.fpn.max_level = 7 - model = model_builder.build(model_proto, is_training=True) - self.assertEqual(model._feature_extractor._fpn_min_level, 3) - self.assertEqual(model._feature_extractor._fpn_max_level, 7) - - - @parameterized.named_parameters( - { - 'testcase_name': 'mask_rcnn_with_matmul', - 'use_matmul_crop_and_resize': False, - 'enable_mask_prediction': True - }, - { - 'testcase_name': 'mask_rcnn_without_matmul', - 'use_matmul_crop_and_resize': True, - 'enable_mask_prediction': True - }, - { - 'testcase_name': 'faster_rcnn_with_matmul', - 'use_matmul_crop_and_resize': False, - 'enable_mask_prediction': False - }, - { - 'testcase_name': 'faster_rcnn_without_matmul', - 'use_matmul_crop_and_resize': True, - 'enable_mask_prediction': False - }, - ) - def test_create_faster_rcnn_models_from_config(self, - use_matmul_crop_and_resize, - enable_mask_prediction): - model_proto = self.create_default_faster_rcnn_model_proto() - faster_rcnn_config = model_proto.faster_rcnn - faster_rcnn_config.use_matmul_crop_and_resize = use_matmul_crop_and_resize - if enable_mask_prediction: - faster_rcnn_config.second_stage_mask_prediction_loss_weight = 3.0 - mask_predictor_config = ( - faster_rcnn_config.second_stage_box_predictor.mask_rcnn_box_predictor) - mask_predictor_config.predict_instance_masks = True - - for extractor_type, extractor_class in ( - self.faster_rcnn_feature_extractors().items()): - faster_rcnn_config.feature_extractor.type = extractor_type - model = model_builder.build(model_proto, is_training=True) - self.assertIsInstance(model, faster_rcnn_meta_arch.FasterRCNNMetaArch) - self.assertIsInstance(model._feature_extractor, extractor_class) - if enable_mask_prediction: - self.assertAlmostEqual(model._second_stage_mask_loss_weight, 3.0) - - def test_create_faster_rcnn_model_from_config_with_example_miner(self): - model_proto = self.create_default_faster_rcnn_model_proto() - model_proto.faster_rcnn.hard_example_miner.num_hard_examples = 64 - model = model_builder.build(model_proto, is_training=True) - self.assertIsNotNone(model._hard_example_miner) - - def test_create_rfcn_model_from_config(self): - model_proto = self.create_default_faster_rcnn_model_proto() - rfcn_predictor_config = ( - model_proto.faster_rcnn.second_stage_box_predictor.rfcn_box_predictor) - rfcn_predictor_config.conv_hyperparams.op = hyperparams_pb2.Hyperparams.CONV - for extractor_type, extractor_class in ( - self.faster_rcnn_feature_extractors().items()): - model_proto.faster_rcnn.feature_extractor.type = extractor_type - model = model_builder.build(model_proto, is_training=True) - self.assertIsInstance(model, rfcn_meta_arch.RFCNMetaArch) - self.assertIsInstance(model._feature_extractor, extractor_class) - - @parameterized.parameters(True, False) - def test_create_faster_rcnn_from_config_with_crop_feature( - self, output_final_box_features): - model_proto = self.create_default_faster_rcnn_model_proto() - model_proto.faster_rcnn.output_final_box_features = ( - output_final_box_features) - _ = model_builder.build(model_proto, is_training=True) - - def test_invalid_model_config_proto(self): - model_proto = '' - with self.assertRaisesRegex( - ValueError, 'model_config not of type model_pb2.DetectionModel.'): - model_builder.build(model_proto, is_training=True) - - def test_unknown_meta_architecture(self): - model_proto = model_pb2.DetectionModel() - with self.assertRaisesRegex(ValueError, 'Unknown meta architecture'): - model_builder.build(model_proto, is_training=True) - - def test_unknown_ssd_feature_extractor(self): - model_proto = self.create_default_ssd_model_proto() - model_proto.ssd.feature_extractor.type = 'unknown_feature_extractor' - with self.assertRaises(ValueError): - model_builder.build(model_proto, is_training=True) - - def test_unknown_faster_rcnn_feature_extractor(self): - model_proto = self.create_default_faster_rcnn_model_proto() - model_proto.faster_rcnn.feature_extractor.type = 'unknown_feature_extractor' - with self.assertRaises(ValueError): - model_builder.build(model_proto, is_training=True) - - def test_invalid_first_stage_nms_iou_threshold(self): - model_proto = self.create_default_faster_rcnn_model_proto() - model_proto.faster_rcnn.first_stage_nms_iou_threshold = 1.1 - with self.assertRaisesRegex(ValueError, - r'iou_threshold not in \[0, 1\.0\]'): - model_builder.build(model_proto, is_training=True) - model_proto.faster_rcnn.first_stage_nms_iou_threshold = -0.1 - with self.assertRaisesRegex(ValueError, - r'iou_threshold not in \[0, 1\.0\]'): - model_builder.build(model_proto, is_training=True) - - def test_invalid_second_stage_batch_size(self): - model_proto = self.create_default_faster_rcnn_model_proto() - model_proto.faster_rcnn.first_stage_max_proposals = 1 - model_proto.faster_rcnn.second_stage_batch_size = 2 - with self.assertRaisesRegex( - ValueError, 'second_stage_batch_size should be no greater ' - 'than first_stage_max_proposals.'): - model_builder.build(model_proto, is_training=True) - - def test_invalid_faster_rcnn_batchnorm_update(self): - model_proto = self.create_default_faster_rcnn_model_proto() - model_proto.faster_rcnn.inplace_batchnorm_update = True - with self.assertRaisesRegex(ValueError, - 'inplace batchnorm updates not supported'): - model_builder.build(model_proto, is_training=True) - - def test_create_experimental_model(self): - - model_text_proto = """ - experimental_model { - name: 'model42' - }""" - - build_func = lambda *args: 42 - model_builder.EXPERIMENTAL_META_ARCH_BUILDER_MAP['model42'] = build_func - model_proto = model_pb2.DetectionModel() - text_format.Merge(model_text_proto, model_proto) - - self.assertEqual(model_builder.build(model_proto, is_training=True), 42) diff --git a/research/object_detection/builders/model_builder_tf1_test.py b/research/object_detection/builders/model_builder_tf1_test.py deleted file mode 100644 index 25c2740f07b..00000000000 --- a/research/object_detection/builders/model_builder_tf1_test.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for model_builder under TensorFlow 1.X.""" -import unittest -from absl.testing import parameterized -import tensorflow.compat.v1 as tf - -from object_detection.builders import model_builder -from object_detection.builders import model_builder_test -from object_detection.meta_architectures import context_rcnn_meta_arch -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.protos import losses_pb2 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ModelBuilderTF1Test(model_builder_test.ModelBuilderTest): - - def default_ssd_feature_extractor(self): - return 'ssd_resnet50_v1_fpn' - - def default_faster_rcnn_feature_extractor(self): - return 'faster_rcnn_resnet101' - - def ssd_feature_extractors(self): - return model_builder.SSD_FEATURE_EXTRACTOR_CLASS_MAP - - def get_override_base_feature_extractor_hyperparams(self, extractor_type): - return extractor_type in {'ssd_inception_v2', 'ssd_inception_v3'} - - def faster_rcnn_feature_extractors(self): - return model_builder.FASTER_RCNN_FEATURE_EXTRACTOR_CLASS_MAP - - - @parameterized.parameters(True, False) - def test_create_context_rcnn_from_config_with_params(self, is_training): - model_proto = self.create_default_faster_rcnn_model_proto() - model_proto.faster_rcnn.context_config.attention_bottleneck_dimension = 10 - model_proto.faster_rcnn.context_config.attention_temperature = 0.5 - model = model_builder.build(model_proto, is_training=is_training) - self.assertIsInstance(model, context_rcnn_meta_arch.ContextRCNNMetaArch) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/model_builder_tf2_test.py b/research/object_detection/builders/model_builder_tf2_test.py deleted file mode 100644 index ea045b23d50..00000000000 --- a/research/object_detection/builders/model_builder_tf2_test.py +++ /dev/null @@ -1,583 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for model_builder under TensorFlow 2.X.""" - -import os -import unittest - -from absl.testing import parameterized -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import model_builder -from object_detection.builders import model_builder_test -from object_detection.core import losses -from object_detection.meta_architectures import deepmac_meta_arch -from object_detection.models import center_net_hourglass_feature_extractor -from object_detection.models.keras_models import hourglass_network -from object_detection.protos import center_net_pb2 -from object_detection.protos import model_pb2 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ModelBuilderTF2Test( - model_builder_test.ModelBuilderTest, parameterized.TestCase): - - def default_ssd_feature_extractor(self): - return 'ssd_resnet50_v1_fpn_keras' - - def default_faster_rcnn_feature_extractor(self): - return 'faster_rcnn_resnet101_keras' - - def ssd_feature_extractors(self): - return model_builder.SSD_KERAS_FEATURE_EXTRACTOR_CLASS_MAP - - def get_override_base_feature_extractor_hyperparams(self, extractor_type): - return extractor_type in {} - - def faster_rcnn_feature_extractors(self): - return model_builder.FASTER_RCNN_KERAS_FEATURE_EXTRACTOR_CLASS_MAP - - def get_fake_label_map_file_path(self): - keypoint_spec_text = """ - item { - name: "/m/01g317" - id: 1 - display_name: "person" - keypoints { - id: 0 - label: 'nose' - } - keypoints { - id: 1 - label: 'left_shoulder' - } - keypoints { - id: 2 - label: 'right_shoulder' - } - keypoints { - id: 3 - label: 'hip' - } - } - """ - keypoint_label_map_path = os.path.join( - self.get_temp_dir(), 'keypoint_label_map') - with tf.gfile.Open(keypoint_label_map_path, 'wb') as f: - f.write(keypoint_spec_text) - return keypoint_label_map_path - - def get_fake_keypoint_proto(self, customize_head_params=False): - task_proto_txt = """ - task_name: "human_pose" - task_loss_weight: 0.9 - keypoint_regression_loss_weight: 1.0 - keypoint_heatmap_loss_weight: 0.1 - keypoint_offset_loss_weight: 0.5 - heatmap_bias_init: 2.14 - keypoint_class_name: "/m/01g317" - loss { - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 3.0 - beta: 4.0 - } - } - localization_loss { - l1_localization_loss { - } - } - } - keypoint_label_to_std { - key: "nose" - value: 0.3 - } - keypoint_label_to_std { - key: "hip" - value: 0.0 - } - keypoint_candidate_score_threshold: 0.3 - num_candidates_per_keypoint: 12 - peak_max_pool_kernel_size: 5 - unmatched_keypoint_score: 0.05 - box_scale: 1.7 - candidate_search_scale: 0.2 - candidate_ranking_mode: "score_distance_ratio" - offset_peak_radius: 3 - per_keypoint_offset: true - predict_depth: true - per_keypoint_depth: true - keypoint_depth_loss_weight: 0.3 - score_distance_multiplier: 11.0 - std_dev_multiplier: 2.8 - rescoring_threshold: 0.5 - gaussian_denom_ratio: 0.3 - argmax_postprocessing: True - """ - if customize_head_params: - task_proto_txt += """ - heatmap_head_params { - num_filters: 64 - num_filters: 32 - kernel_sizes: 5 - kernel_sizes: 3 - } - offset_head_params { - num_filters: 128 - num_filters: 64 - kernel_sizes: 5 - kernel_sizes: 3 - } - """ - config = text_format.Merge(task_proto_txt, - center_net_pb2.CenterNet.KeypointEstimation()) - return config - - def get_fake_object_center_proto(self, customize_head_params=False): - proto_txt = """ - object_center_loss_weight: 0.5 - heatmap_bias_init: 3.14 - min_box_overlap_iou: 0.2 - max_box_predictions: 15 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 3.0 - beta: 4.0 - } - } - peak_max_pool_kernel_size: 5 - """ - if customize_head_params: - proto_txt += """ - center_head_params { - num_filters: 64 - num_filters: 32 - kernel_sizes: 5 - kernel_sizes: 3 - } - """ - return text_format.Merge(proto_txt, - center_net_pb2.CenterNet.ObjectCenterParams()) - - def get_fake_object_center_from_keypoints_proto(self): - proto_txt = """ - object_center_loss_weight: 0.5 - heatmap_bias_init: 3.14 - min_box_overlap_iou: 0.2 - max_box_predictions: 15 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 3.0 - beta: 4.0 - } - } - keypoint_weights_for_center: 1.0 - keypoint_weights_for_center: 0.0 - keypoint_weights_for_center: 1.0 - keypoint_weights_for_center: 0.0 - """ - return text_format.Merge(proto_txt, - center_net_pb2.CenterNet.ObjectCenterParams()) - - def get_fake_object_detection_proto(self, customize_head_params=False): - proto_txt = """ - task_loss_weight: 0.5 - offset_loss_weight: 0.1 - scale_loss_weight: 0.2 - localization_loss { - l1_localization_loss { - } - } - """ - if customize_head_params: - proto_txt += """ - scale_head_params { - num_filters: 128 - num_filters: 64 - kernel_sizes: 5 - kernel_sizes: 3 - } - """ - return text_format.Merge(proto_txt, - center_net_pb2.CenterNet.ObjectDetection()) - - def get_fake_mask_proto(self, customize_head_params=False): - proto_txt = """ - task_loss_weight: 0.7 - classification_loss { - weighted_softmax {} - } - mask_height: 8 - mask_width: 8 - score_threshold: 0.7 - heatmap_bias_init: -2.0 - """ - if customize_head_params: - proto_txt += """ - mask_head_params { - num_filters: 128 - num_filters: 64 - kernel_sizes: 5 - kernel_sizes: 3 - } - """ - return text_format.Merge(proto_txt, - center_net_pb2.CenterNet.MaskEstimation()) - - def get_fake_densepose_proto(self): - proto_txt = """ - task_loss_weight: 0.5 - class_id: 0 - loss { - classification_loss { - weighted_softmax {} - } - localization_loss { - l1_localization_loss { - } - } - } - num_parts: 24 - part_loss_weight: 1.0 - coordinate_loss_weight: 2.0 - upsample_to_input_res: true - heatmap_bias_init: -2.0 - """ - return text_format.Merge(proto_txt, - center_net_pb2.CenterNet.DensePoseEstimation()) - - @parameterized.parameters( - {'customize_head_params': True}, - {'customize_head_params': False} - ) - def test_create_center_net_model(self, customize_head_params): - """Test building a CenterNet model from proto txt.""" - proto_txt = """ - center_net { - num_classes: 10 - feature_extractor { - type: "hourglass_52" - channel_stds: [4, 5, 6] - bgr_ordering: true - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - } - """ - # Set up the configuration proto. - config = text_format.Merge(proto_txt, model_pb2.DetectionModel()) - config.center_net.object_center_params.CopyFrom( - self.get_fake_object_center_proto( - customize_head_params=customize_head_params)) - config.center_net.object_detection_task.CopyFrom( - self.get_fake_object_detection_proto( - customize_head_params=customize_head_params)) - config.center_net.keypoint_estimation_task.append( - self.get_fake_keypoint_proto( - customize_head_params=customize_head_params)) - config.center_net.keypoint_label_map_path = ( - self.get_fake_label_map_file_path()) - config.center_net.mask_estimation_task.CopyFrom( - self.get_fake_mask_proto( - customize_head_params=customize_head_params)) - config.center_net.densepose_estimation_task.CopyFrom( - self.get_fake_densepose_proto()) - - # Build the model from the configuration. - model = model_builder.build(config, is_training=True) - - # Check object center related parameters. - self.assertEqual(model._num_classes, 10) - self.assertIsInstance(model._center_params.classification_loss, - losses.PenaltyReducedLogisticFocalLoss) - self.assertEqual(model._center_params.classification_loss._alpha, 3.0) - self.assertEqual(model._center_params.classification_loss._beta, 4.0) - self.assertAlmostEqual(model._center_params.min_box_overlap_iou, 0.2) - self.assertAlmostEqual( - model._center_params.heatmap_bias_init, 3.14, places=4) - self.assertEqual(model._center_params.max_box_predictions, 15) - if customize_head_params: - self.assertEqual(model._center_params.center_head_num_filters, [64, 32]) - self.assertEqual(model._center_params.center_head_kernel_sizes, [5, 3]) - else: - self.assertEqual(model._center_params.center_head_num_filters, [256]) - self.assertEqual(model._center_params.center_head_kernel_sizes, [3]) - self.assertEqual(model._center_params.peak_max_pool_kernel_size, 5) - - # Check object detection related parameters. - self.assertAlmostEqual(model._od_params.offset_loss_weight, 0.1) - self.assertAlmostEqual(model._od_params.scale_loss_weight, 0.2) - self.assertAlmostEqual(model._od_params.task_loss_weight, 0.5) - self.assertIsInstance(model._od_params.localization_loss, - losses.L1LocalizationLoss) - self.assertEqual(model._od_params.offset_head_num_filters, [256]) - self.assertEqual(model._od_params.offset_head_kernel_sizes, [3]) - if customize_head_params: - self.assertEqual(model._od_params.scale_head_num_filters, [128, 64]) - self.assertEqual(model._od_params.scale_head_kernel_sizes, [5, 3]) - else: - self.assertEqual(model._od_params.scale_head_num_filters, [256]) - self.assertEqual(model._od_params.scale_head_kernel_sizes, [3]) - - # Check keypoint estimation related parameters. - kp_params = model._kp_params_dict['human_pose'] - self.assertAlmostEqual(kp_params.task_loss_weight, 0.9) - self.assertAlmostEqual(kp_params.keypoint_regression_loss_weight, 1.0) - self.assertAlmostEqual(kp_params.keypoint_offset_loss_weight, 0.5) - self.assertAlmostEqual(kp_params.heatmap_bias_init, 2.14, places=4) - self.assertEqual(kp_params.classification_loss._alpha, 3.0) - self.assertEqual(kp_params.keypoint_indices, [0, 1, 2, 3]) - self.assertEqual(kp_params.keypoint_labels, - ['nose', 'left_shoulder', 'right_shoulder', 'hip']) - self.assertAllClose(kp_params.keypoint_std_dev, [0.3, 1.0, 1.0, 0.0]) - self.assertEqual(kp_params.classification_loss._beta, 4.0) - self.assertIsInstance(kp_params.localization_loss, - losses.L1LocalizationLoss) - self.assertAlmostEqual(kp_params.keypoint_candidate_score_threshold, 0.3) - self.assertEqual(kp_params.num_candidates_per_keypoint, 12) - self.assertEqual(kp_params.peak_max_pool_kernel_size, 5) - self.assertAlmostEqual(kp_params.unmatched_keypoint_score, 0.05) - self.assertAlmostEqual(kp_params.box_scale, 1.7) - self.assertAlmostEqual(kp_params.candidate_search_scale, 0.2) - self.assertEqual(kp_params.candidate_ranking_mode, 'score_distance_ratio') - self.assertEqual(kp_params.offset_peak_radius, 3) - self.assertEqual(kp_params.per_keypoint_offset, True) - self.assertEqual(kp_params.predict_depth, True) - self.assertEqual(kp_params.per_keypoint_depth, True) - self.assertAlmostEqual(kp_params.keypoint_depth_loss_weight, 0.3) - self.assertAlmostEqual(kp_params.score_distance_multiplier, 11.0) - self.assertAlmostEqual(kp_params.std_dev_multiplier, 2.8) - self.assertAlmostEqual(kp_params.rescoring_threshold, 0.5) - if customize_head_params: - # Set by the config. - self.assertEqual(kp_params.heatmap_head_num_filters, [64, 32]) - self.assertEqual(kp_params.heatmap_head_kernel_sizes, [5, 3]) - self.assertEqual(kp_params.offset_head_num_filters, [128, 64]) - self.assertEqual(kp_params.offset_head_kernel_sizes, [5, 3]) - else: - # Default values: - self.assertEqual(kp_params.heatmap_head_num_filters, [256]) - self.assertEqual(kp_params.heatmap_head_kernel_sizes, [3]) - self.assertEqual(kp_params.offset_head_num_filters, [256]) - self.assertEqual(kp_params.offset_head_kernel_sizes, [3]) - self.assertAlmostEqual(kp_params.gaussian_denom_ratio, 0.3) - self.assertEqual(kp_params.argmax_postprocessing, True) - - # Check mask related parameters. - self.assertAlmostEqual(model._mask_params.task_loss_weight, 0.7) - self.assertIsInstance(model._mask_params.classification_loss, - losses.WeightedSoftmaxClassificationLoss) - self.assertEqual(model._mask_params.mask_height, 8) - self.assertEqual(model._mask_params.mask_width, 8) - self.assertAlmostEqual(model._mask_params.score_threshold, 0.7) - self.assertAlmostEqual( - model._mask_params.heatmap_bias_init, -2.0, places=4) - if customize_head_params: - self.assertEqual(model._mask_params.mask_head_num_filters, [128, 64]) - self.assertEqual(model._mask_params.mask_head_kernel_sizes, [5, 3]) - else: - self.assertEqual(model._mask_params.mask_head_num_filters, [256]) - self.assertEqual(model._mask_params.mask_head_kernel_sizes, [3]) - - # Check DensePose related parameters. - self.assertEqual(model._densepose_params.class_id, 0) - self.assertIsInstance(model._densepose_params.classification_loss, - losses.WeightedSoftmaxClassificationLoss) - self.assertIsInstance(model._densepose_params.localization_loss, - losses.L1LocalizationLoss) - self.assertAlmostEqual(model._densepose_params.part_loss_weight, 1.0) - self.assertAlmostEqual(model._densepose_params.coordinate_loss_weight, 2.0) - self.assertEqual(model._densepose_params.num_parts, 24) - self.assertAlmostEqual(model._densepose_params.task_loss_weight, 0.5) - self.assertTrue(model._densepose_params.upsample_to_input_res) - self.assertEqual(model._densepose_params.upsample_method, 'bilinear') - self.assertAlmostEqual( - model._densepose_params.heatmap_bias_init, -2.0, places=4) - - # Check feature extractor parameters. - self.assertIsInstance( - model._feature_extractor, center_net_hourglass_feature_extractor - .CenterNetHourglassFeatureExtractor) - self.assertAllClose(model._feature_extractor._channel_means, [0, 0, 0]) - self.assertAllClose(model._feature_extractor._channel_stds, [4, 5, 6]) - self.assertTrue(model._feature_extractor._bgr_ordering) - backbone = model._feature_extractor._network - self.assertIsInstance(backbone, hourglass_network.HourglassNetwork) - self.assertTrue(backbone.num_hourglasses, 1) - - def test_create_center_net_model_from_keypoints(self): - """Test building a CenterNet model from proto txt.""" - proto_txt = """ - center_net { - num_classes: 10 - feature_extractor { - type: "hourglass_52" - channel_stds: [4, 5, 6] - bgr_ordering: true - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - } - """ - # Set up the configuration proto. - config = text_format.Parse(proto_txt, model_pb2.DetectionModel()) - # Only add object center and keypoint estimation configs here. - config.center_net.object_center_params.CopyFrom( - self.get_fake_object_center_from_keypoints_proto()) - config.center_net.keypoint_estimation_task.append( - self.get_fake_keypoint_proto()) - config.center_net.keypoint_label_map_path = ( - self.get_fake_label_map_file_path()) - - # Build the model from the configuration. - model = model_builder.build(config, is_training=True) - - # Check object center related parameters. - self.assertEqual(model._num_classes, 10) - self.assertEqual(model._center_params.keypoint_weights_for_center, - [1.0, 0.0, 1.0, 0.0]) - - # Check keypoint estimation related parameters. - kp_params = model._kp_params_dict['human_pose'] - self.assertAlmostEqual(kp_params.task_loss_weight, 0.9) - self.assertEqual(kp_params.keypoint_indices, [0, 1, 2, 3]) - self.assertEqual(kp_params.keypoint_labels, - ['nose', 'left_shoulder', 'right_shoulder', 'hip']) - - def test_create_center_net_model_mobilenet(self): - """Test building a CenterNet model using bilinear interpolation.""" - proto_txt = """ - center_net { - num_classes: 10 - feature_extractor { - type: "mobilenet_v2_fpn" - depth_multiplier: 2.0 - use_separable_conv: true - upsampling_interpolation: "bilinear" - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - } - """ - # Set up the configuration proto. - config = text_format.Parse(proto_txt, model_pb2.DetectionModel()) - # Only add object center and keypoint estimation configs here. - config.center_net.object_center_params.CopyFrom( - self.get_fake_object_center_from_keypoints_proto()) - config.center_net.keypoint_estimation_task.append( - self.get_fake_keypoint_proto()) - config.center_net.keypoint_label_map_path = ( - self.get_fake_label_map_file_path()) - - # Build the model from the configuration. - model = model_builder.build(config, is_training=True) - - feature_extractor = model._feature_extractor - # Verify the upsampling layers in the FPN use 'bilinear' interpolation. - fpn = feature_extractor.get_layer('model_1') - num_up_sampling2d_layers = 0 - for layer in fpn.layers: - if 'up_sampling2d' in layer.name: - num_up_sampling2d_layers += 1 - self.assertEqual('bilinear', layer.interpolation) - # Verify that there are up_sampling2d layers. - self.assertGreater(num_up_sampling2d_layers, 0) - - # Verify that the FPN ops uses separable conv. - for layer in fpn.layers: - # Convolution layers with kernel size not equal to (1, 1) should be - # separable 2D convolutions. - if 'conv' in layer.name and layer.kernel_size != (1, 1): - self.assertIsInstance(layer, tf.keras.layers.SeparableConv2D) - - # Verify that the backbone indeed double the number of channel according to - # the depthmultiplier. - backbone = feature_extractor.get_layer('model') - first_conv = backbone.get_layer('Conv1') - # Note that the first layer typically has 32 filters, but this model has - # a depth multiplier of 2. - self.assertEqual(64, first_conv.filters) - - def test_create_center_net_deepmac(self): - """Test building a CenterNet DeepMAC model.""" - - proto_txt = """ - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_52" - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - - deepmac_mask_estimation { - classification_loss { - weighted_sigmoid {} - } - } - } - """ - # Set up the configuration proto. - config = text_format.Parse(proto_txt, model_pb2.DetectionModel()) - - # Build the model from the configuration. - model = model_builder.build(config, is_training=True) - self.assertIsInstance(model, deepmac_meta_arch.DeepMACMetaArch) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/optimizer_builder.py b/research/object_detection/builders/optimizer_builder.py deleted file mode 100644 index f24747aa9ba..00000000000 --- a/research/object_detection/builders/optimizer_builder.py +++ /dev/null @@ -1,213 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Functions to build DetectionModel training optimizers.""" - -import tensorflow.compat.v1 as tf - -from object_detection.utils import learning_schedules -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top -if tf_version.is_tf2(): - from official.modeling.optimization import ema_optimizer -# pylint: enable=g-import-not-at-top - -try: - from tensorflow.contrib import opt as tf_opt # pylint: disable=g-import-not-at-top -except: # pylint: disable=bare-except - pass - - -def build_optimizers_tf_v1(optimizer_config, global_step=None): - """Create a TF v1 compatible optimizer based on config. - - Args: - optimizer_config: A Optimizer proto message. - global_step: A variable representing the current step. - If None, defaults to tf.train.get_or_create_global_step() - - Returns: - An optimizer and a list of variables for summary. - - Raises: - ValueError: when using an unsupported input data type. - """ - optimizer_type = optimizer_config.WhichOneof('optimizer') - optimizer = None - - summary_vars = [] - if optimizer_type == 'rms_prop_optimizer': - config = optimizer_config.rms_prop_optimizer - learning_rate = _create_learning_rate(config.learning_rate, - global_step=global_step) - summary_vars.append(learning_rate) - optimizer = tf.train.RMSPropOptimizer( - learning_rate, - decay=config.decay, - momentum=config.momentum_optimizer_value, - epsilon=config.epsilon) - - if optimizer_type == 'momentum_optimizer': - config = optimizer_config.momentum_optimizer - learning_rate = _create_learning_rate(config.learning_rate, - global_step=global_step) - summary_vars.append(learning_rate) - optimizer = tf.train.MomentumOptimizer( - learning_rate, - momentum=config.momentum_optimizer_value) - - if optimizer_type == 'adam_optimizer': - config = optimizer_config.adam_optimizer - learning_rate = _create_learning_rate(config.learning_rate, - global_step=global_step) - summary_vars.append(learning_rate) - optimizer = tf.train.AdamOptimizer(learning_rate, epsilon=config.epsilon) - - - if optimizer is None: - raise ValueError('Optimizer %s not supported.' % optimizer_type) - - if optimizer_config.use_moving_average: - optimizer = tf_opt.MovingAverageOptimizer( - optimizer, average_decay=optimizer_config.moving_average_decay) - - return optimizer, summary_vars - - -def build_optimizers_tf_v2(optimizer_config, global_step=None): - """Create a TF v2 compatible optimizer based on config. - - Args: - optimizer_config: A Optimizer proto message. - global_step: A variable representing the current step. - If None, defaults to tf.train.get_or_create_global_step() - - Returns: - An optimizer and a list of variables for summary. - - Raises: - ValueError: when using an unsupported input data type. - """ - optimizer_type = optimizer_config.WhichOneof('optimizer') - optimizer = None - - summary_vars = [] - if optimizer_type == 'rms_prop_optimizer': - config = optimizer_config.rms_prop_optimizer - learning_rate = _create_learning_rate(config.learning_rate, - global_step=global_step) - summary_vars.append(learning_rate) - optimizer = tf.keras.optimizers.RMSprop( - learning_rate, - decay=config.decay, - momentum=config.momentum_optimizer_value, - epsilon=config.epsilon) - - if optimizer_type == 'momentum_optimizer': - config = optimizer_config.momentum_optimizer - learning_rate = _create_learning_rate(config.learning_rate, - global_step=global_step) - summary_vars.append(learning_rate) - optimizer = tf.keras.optimizers.SGD( - learning_rate, - momentum=config.momentum_optimizer_value) - - if optimizer_type == 'adam_optimizer': - config = optimizer_config.adam_optimizer - learning_rate = _create_learning_rate(config.learning_rate, - global_step=global_step) - summary_vars.append(learning_rate) - optimizer = tf.keras.optimizers.Adam(learning_rate, epsilon=config.epsilon) - - if optimizer is None: - raise ValueError('Optimizer %s not supported.' % optimizer_type) - - if optimizer_config.use_moving_average: - optimizer = ema_optimizer.ExponentialMovingAverage( - optimizer=optimizer, - average_decay=optimizer_config.moving_average_decay) - - return optimizer, summary_vars - - -def build(config, global_step=None): - - if tf.executing_eagerly(): - return build_optimizers_tf_v2(config, global_step) - else: - return build_optimizers_tf_v1(config, global_step) - - -def _create_learning_rate(learning_rate_config, global_step=None): - """Create optimizer learning rate based on config. - - Args: - learning_rate_config: A LearningRate proto message. - global_step: A variable representing the current step. - If None, defaults to tf.train.get_or_create_global_step() - - Returns: - A learning rate. - - Raises: - ValueError: when using an unsupported input data type. - """ - if global_step is None: - global_step = tf.train.get_or_create_global_step() - learning_rate = None - learning_rate_type = learning_rate_config.WhichOneof('learning_rate') - if learning_rate_type == 'constant_learning_rate': - config = learning_rate_config.constant_learning_rate - learning_rate = tf.constant(config.learning_rate, dtype=tf.float32, - name='learning_rate') - - if learning_rate_type == 'exponential_decay_learning_rate': - config = learning_rate_config.exponential_decay_learning_rate - learning_rate = learning_schedules.exponential_decay_with_burnin( - global_step, - config.initial_learning_rate, - config.decay_steps, - config.decay_factor, - burnin_learning_rate=config.burnin_learning_rate, - burnin_steps=config.burnin_steps, - min_learning_rate=config.min_learning_rate, - staircase=config.staircase) - - if learning_rate_type == 'manual_step_learning_rate': - config = learning_rate_config.manual_step_learning_rate - if not config.schedule: - raise ValueError('Empty learning rate schedule.') - learning_rate_step_boundaries = [x.step for x in config.schedule] - learning_rate_sequence = [config.initial_learning_rate] - learning_rate_sequence += [x.learning_rate for x in config.schedule] - learning_rate = learning_schedules.manual_stepping( - global_step, learning_rate_step_boundaries, - learning_rate_sequence, config.warmup) - - if learning_rate_type == 'cosine_decay_learning_rate': - config = learning_rate_config.cosine_decay_learning_rate - learning_rate = learning_schedules.cosine_decay_with_warmup( - global_step, - config.learning_rate_base, - config.total_steps, - config.warmup_learning_rate, - config.warmup_steps, - config.hold_base_rate_steps) - - if learning_rate is None: - raise ValueError('Learning_rate %s not supported.' % learning_rate_type) - - return learning_rate diff --git a/research/object_detection/builders/optimizer_builder_tf1_test.py b/research/object_detection/builders/optimizer_builder_tf1_test.py deleted file mode 100644 index 988fa84480b..00000000000 --- a/research/object_detection/builders/optimizer_builder_tf1_test.py +++ /dev/null @@ -1,223 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for optimizer_builder.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -import six -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import optimizer_builder -from object_detection.protos import optimizer_pb2 -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top -if tf_version.is_tf1(): - from tensorflow.contrib import opt as contrib_opt -# pylint: enable=g-import-not-at-top - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class LearningRateBuilderTest(tf.test.TestCase): - - def testBuildConstantLearningRate(self): - learning_rate_text_proto = """ - constant_learning_rate { - learning_rate: 0.004 - } - """ - learning_rate_proto = optimizer_pb2.LearningRate() - text_format.Merge(learning_rate_text_proto, learning_rate_proto) - learning_rate = optimizer_builder._create_learning_rate( - learning_rate_proto) - self.assertTrue( - six.ensure_str(learning_rate.op.name).endswith('learning_rate')) - with self.test_session(): - learning_rate_out = learning_rate.eval() - self.assertAlmostEqual(learning_rate_out, 0.004) - - def testBuildExponentialDecayLearningRate(self): - learning_rate_text_proto = """ - exponential_decay_learning_rate { - initial_learning_rate: 0.004 - decay_steps: 99999 - decay_factor: 0.85 - staircase: false - } - """ - learning_rate_proto = optimizer_pb2.LearningRate() - text_format.Merge(learning_rate_text_proto, learning_rate_proto) - learning_rate = optimizer_builder._create_learning_rate( - learning_rate_proto) - self.assertTrue( - six.ensure_str(learning_rate.op.name).endswith('learning_rate')) - self.assertIsInstance(learning_rate, tf.Tensor) - - def testBuildManualStepLearningRate(self): - learning_rate_text_proto = """ - manual_step_learning_rate { - initial_learning_rate: 0.002 - schedule { - step: 100 - learning_rate: 0.006 - } - schedule { - step: 90000 - learning_rate: 0.00006 - } - warmup: true - } - """ - learning_rate_proto = optimizer_pb2.LearningRate() - text_format.Merge(learning_rate_text_proto, learning_rate_proto) - learning_rate = optimizer_builder._create_learning_rate( - learning_rate_proto) - self.assertIsInstance(learning_rate, tf.Tensor) - - def testBuildCosineDecayLearningRate(self): - learning_rate_text_proto = """ - cosine_decay_learning_rate { - learning_rate_base: 0.002 - total_steps: 20000 - warmup_learning_rate: 0.0001 - warmup_steps: 1000 - hold_base_rate_steps: 20000 - } - """ - learning_rate_proto = optimizer_pb2.LearningRate() - text_format.Merge(learning_rate_text_proto, learning_rate_proto) - learning_rate = optimizer_builder._create_learning_rate( - learning_rate_proto) - self.assertIsInstance(learning_rate, tf.Tensor) - - def testRaiseErrorOnEmptyLearningRate(self): - learning_rate_text_proto = """ - """ - learning_rate_proto = optimizer_pb2.LearningRate() - text_format.Merge(learning_rate_text_proto, learning_rate_proto) - with self.assertRaises(ValueError): - optimizer_builder._create_learning_rate(learning_rate_proto) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class OptimizerBuilderTest(tf.test.TestCase): - - def testBuildRMSPropOptimizer(self): - optimizer_text_proto = """ - rms_prop_optimizer: { - learning_rate: { - exponential_decay_learning_rate { - initial_learning_rate: 0.004 - decay_steps: 800720 - decay_factor: 0.95 - } - } - momentum_optimizer_value: 0.9 - decay: 0.9 - epsilon: 1.0 - } - use_moving_average: false - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, tf.train.RMSPropOptimizer) - - def testBuildMomentumOptimizer(self): - optimizer_text_proto = """ - momentum_optimizer: { - learning_rate: { - constant_learning_rate { - learning_rate: 0.001 - } - } - momentum_optimizer_value: 0.99 - } - use_moving_average: false - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, tf.train.MomentumOptimizer) - - def testBuildAdamOptimizer(self): - optimizer_text_proto = """ - adam_optimizer: { - epsilon: 1e-6 - learning_rate: { - constant_learning_rate { - learning_rate: 0.002 - } - } - } - use_moving_average: false - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, tf.train.AdamOptimizer) - - def testBuildMovingAverageOptimizer(self): - optimizer_text_proto = """ - adam_optimizer: { - learning_rate: { - constant_learning_rate { - learning_rate: 0.002 - } - } - } - use_moving_average: True - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, contrib_opt.MovingAverageOptimizer) - - def testBuildMovingAverageOptimizerWithNonDefaultDecay(self): - optimizer_text_proto = """ - adam_optimizer: { - learning_rate: { - constant_learning_rate { - learning_rate: 0.002 - } - } - } - use_moving_average: True - moving_average_decay: 0.2 - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, contrib_opt.MovingAverageOptimizer) - # TODO(rathodv): Find a way to not depend on the private members. - self.assertAlmostEqual(optimizer._ema._decay, 0.2) - - def testBuildEmptyOptimizer(self): - optimizer_text_proto = """ - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - with self.assertRaises(ValueError): - optimizer_builder.build(optimizer_proto) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/optimizer_builder_tf2_test.py b/research/object_detection/builders/optimizer_builder_tf2_test.py deleted file mode 100644 index 5ae125fa048..00000000000 --- a/research/object_detection/builders/optimizer_builder_tf2_test.py +++ /dev/null @@ -1,104 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for optimizer_builder.""" -import unittest -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import optimizer_builder -from object_detection.protos import optimizer_pb2 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class OptimizerBuilderV2Test(tf.test.TestCase): - """Test building optimizers in V2 mode.""" - - def testBuildRMSPropOptimizer(self): - optimizer_text_proto = """ - rms_prop_optimizer: { - learning_rate: { - exponential_decay_learning_rate { - initial_learning_rate: 0.004 - decay_steps: 800720 - decay_factor: 0.95 - } - } - momentum_optimizer_value: 0.9 - decay: 0.9 - epsilon: 1.0 - } - use_moving_average: false - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, tf.keras.optimizers.RMSprop) - - def testBuildMomentumOptimizer(self): - optimizer_text_proto = """ - momentum_optimizer: { - learning_rate: { - constant_learning_rate { - learning_rate: 0.001 - } - } - momentum_optimizer_value: 0.99 - } - use_moving_average: false - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, tf.keras.optimizers.SGD) - - def testBuildAdamOptimizer(self): - optimizer_text_proto = """ - adam_optimizer: { - learning_rate: { - constant_learning_rate { - learning_rate: 0.002 - } - } - } - use_moving_average: false - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, tf.keras.optimizers.Adam) - - def testBuildMovingAverageOptimizer(self): - optimizer_text_proto = """ - adam_optimizer: { - learning_rate: { - constant_learning_rate { - learning_rate: 0.002 - } - } - } - use_moving_average: True - """ - optimizer_proto = optimizer_pb2.Optimizer() - text_format.Merge(optimizer_text_proto, optimizer_proto) - optimizer, _ = optimizer_builder.build(optimizer_proto) - self.assertIsInstance(optimizer, tf.keras.optimizers.Optimizer) - - -if __name__ == '__main__': - tf.enable_v2_behavior() - tf.test.main() diff --git a/research/object_detection/builders/post_processing_builder.py b/research/object_detection/builders/post_processing_builder.py deleted file mode 100644 index c61f6891e29..00000000000 --- a/research/object_detection/builders/post_processing_builder.py +++ /dev/null @@ -1,183 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Builder function for post processing operations.""" -import functools - -import tensorflow.compat.v1 as tf -from object_detection.builders import calibration_builder -from object_detection.core import post_processing -from object_detection.protos import post_processing_pb2 - - -def build(post_processing_config): - """Builds callables for post-processing operations. - - Builds callables for non-max suppression, score conversion, and (optionally) - calibration based on the configuration. - - Non-max suppression callable takes `boxes`, `scores`, and optionally - `clip_window`, `parallel_iterations` `masks, and `scope` as inputs. It returns - `nms_boxes`, `nms_scores`, `nms_classes` `nms_masks` and `num_detections`. See - post_processing.batch_multiclass_non_max_suppression for the type and shape - of these tensors. - - Score converter callable should be called with `input` tensor. The callable - returns the output from one of 3 tf operations based on the configuration - - tf.identity, tf.sigmoid or tf.nn.softmax. If a calibration config is provided, - score_converter also applies calibration transformations, as defined in - calibration_builder.py. See tensorflow documentation for argument and return - value descriptions. - - Args: - post_processing_config: post_processing.proto object containing the - parameters for the post-processing operations. - - Returns: - non_max_suppressor_fn: Callable for non-max suppression. - score_converter_fn: Callable for score conversion. - - Raises: - ValueError: if the post_processing_config is of incorrect type. - """ - if not isinstance(post_processing_config, post_processing_pb2.PostProcessing): - raise ValueError('post_processing_config not of type ' - 'post_processing_pb2.Postprocessing.') - non_max_suppressor_fn = _build_non_max_suppressor( - post_processing_config.batch_non_max_suppression) - score_converter_fn = _build_score_converter( - post_processing_config.score_converter, - post_processing_config.logit_scale) - if post_processing_config.HasField('calibration_config'): - score_converter_fn = _build_calibrated_score_converter( - score_converter_fn, - post_processing_config.calibration_config) - return non_max_suppressor_fn, score_converter_fn - - -def _build_non_max_suppressor(nms_config): - """Builds non-max suppresson based on the nms config. - - Args: - nms_config: post_processing_pb2.PostProcessing.BatchNonMaxSuppression proto. - - Returns: - non_max_suppressor_fn: Callable non-max suppressor. - - Raises: - ValueError: On incorrect iou_threshold or on incompatible values of - max_total_detections and max_detections_per_class or on negative - soft_nms_sigma. - """ - if nms_config.iou_threshold < 0 or nms_config.iou_threshold > 1.0: - raise ValueError('iou_threshold not in [0, 1.0].') - if nms_config.max_detections_per_class > nms_config.max_total_detections: - raise ValueError('max_detections_per_class should be no greater than ' - 'max_total_detections.') - if nms_config.soft_nms_sigma < 0.0: - raise ValueError('soft_nms_sigma should be non-negative.') - if nms_config.use_combined_nms and nms_config.use_class_agnostic_nms: - raise ValueError('combined_nms does not support class_agnostic_nms.') - non_max_suppressor_fn = functools.partial( - post_processing.batch_multiclass_non_max_suppression, - score_thresh=nms_config.score_threshold, - iou_thresh=nms_config.iou_threshold, - max_size_per_class=nms_config.max_detections_per_class, - max_total_size=nms_config.max_total_detections, - use_static_shapes=nms_config.use_static_shapes, - use_class_agnostic_nms=nms_config.use_class_agnostic_nms, - max_classes_per_detection=nms_config.max_classes_per_detection, - soft_nms_sigma=nms_config.soft_nms_sigma, - use_partitioned_nms=nms_config.use_partitioned_nms, - use_combined_nms=nms_config.use_combined_nms, - change_coordinate_frame=nms_config.change_coordinate_frame, - use_hard_nms=nms_config.use_hard_nms, - use_cpu_nms=nms_config.use_cpu_nms) - - return non_max_suppressor_fn - - -def _score_converter_fn_with_logit_scale(tf_score_converter_fn, logit_scale): - """Create a function to scale logits then apply a Tensorflow function.""" - def score_converter_fn(logits): - scaled_logits = tf.multiply(logits, 1.0 / logit_scale, name='scale_logits') - return tf_score_converter_fn(scaled_logits, name='convert_scores') - score_converter_fn.__name__ = '%s_with_logit_scale' % ( - tf_score_converter_fn.__name__) - return score_converter_fn - - -def _build_score_converter(score_converter_config, logit_scale): - """Builds score converter based on the config. - - Builds one of [tf.identity, tf.sigmoid, tf.softmax] score converters based on - the config. - - Args: - score_converter_config: post_processing_pb2.PostProcessing.score_converter. - logit_scale: temperature to use for SOFTMAX score_converter. - - Returns: - Callable score converter op. - - Raises: - ValueError: On unknown score converter. - """ - if score_converter_config == post_processing_pb2.PostProcessing.IDENTITY: - return _score_converter_fn_with_logit_scale(tf.identity, logit_scale) - if score_converter_config == post_processing_pb2.PostProcessing.SIGMOID: - return _score_converter_fn_with_logit_scale(tf.sigmoid, logit_scale) - if score_converter_config == post_processing_pb2.PostProcessing.SOFTMAX: - return _score_converter_fn_with_logit_scale(tf.nn.softmax, logit_scale) - raise ValueError('Unknown score converter.') - - -def _build_calibrated_score_converter(score_converter_fn, calibration_config): - """Wraps a score_converter_fn, adding a calibration step. - - Builds a score converter function with a calibration transformation according - to calibration_builder.py. The score conversion function may be applied before - or after the calibration transformation, depending on the calibration method. - If the method is temperature scaling, the score conversion is - after the calibration transformation. Otherwise, the score conversion is - before the calibration transformation. Calibration applies positive monotonic - transformations to inputs (i.e. score ordering is strictly preserved or - adjacent scores are mapped to the same score). When calibration is - class-agnostic, the highest-scoring class remains unchanged, unless two - adjacent scores are mapped to the same value and one class arbitrarily - selected to break the tie. In per-class calibration, it's possible (though - rare in practice) that the highest-scoring class will change, since positive - monotonicity is only required to hold within each class. - - Args: - score_converter_fn: callable that takes logit scores as input. - calibration_config: post_processing_pb2.PostProcessing.calibration_config. - - Returns: - Callable calibrated score coverter op. - """ - calibration_fn = calibration_builder.build(calibration_config) - def calibrated_score_converter_fn(logits): - if (calibration_config.WhichOneof('calibrator') == - 'temperature_scaling_calibration'): - calibrated_logits = calibration_fn(logits) - return score_converter_fn(calibrated_logits) - else: - converted_logits = score_converter_fn(logits) - return calibration_fn(converted_logits) - - calibrated_score_converter_fn.__name__ = ( - 'calibrate_with_%s' % calibration_config.WhichOneof('calibrator')) - return calibrated_score_converter_fn diff --git a/research/object_detection/builders/post_processing_builder_test.py b/research/object_detection/builders/post_processing_builder_test.py deleted file mode 100644 index b7383c92f99..00000000000 --- a/research/object_detection/builders/post_processing_builder_test.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for post_processing_builder.""" - -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from object_detection.builders import post_processing_builder -from object_detection.protos import post_processing_pb2 -from object_detection.utils import test_case - - -class PostProcessingBuilderTest(test_case.TestCase): - - def test_build_non_max_suppressor_with_correct_parameters(self): - post_processing_text_proto = """ - batch_non_max_suppression { - score_threshold: 0.7 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - soft_nms_sigma: 0.4 - } - """ - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - non_max_suppressor, _ = post_processing_builder.build( - post_processing_config) - self.assertEqual(non_max_suppressor.keywords['max_size_per_class'], 100) - self.assertEqual(non_max_suppressor.keywords['max_total_size'], 300) - self.assertAlmostEqual(non_max_suppressor.keywords['score_thresh'], 0.7) - self.assertAlmostEqual(non_max_suppressor.keywords['iou_thresh'], 0.6) - self.assertAlmostEqual(non_max_suppressor.keywords['soft_nms_sigma'], 0.4) - - def test_build_non_max_suppressor_with_correct_parameters_classagnostic_nms( - self): - post_processing_text_proto = """ - batch_non_max_suppression { - score_threshold: 0.7 - iou_threshold: 0.6 - max_detections_per_class: 10 - max_total_detections: 300 - use_class_agnostic_nms: True - max_classes_per_detection: 1 - } - """ - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - non_max_suppressor, _ = post_processing_builder.build( - post_processing_config) - self.assertEqual(non_max_suppressor.keywords['max_size_per_class'], 10) - self.assertEqual(non_max_suppressor.keywords['max_total_size'], 300) - self.assertEqual(non_max_suppressor.keywords['max_classes_per_detection'], - 1) - self.assertEqual(non_max_suppressor.keywords['use_class_agnostic_nms'], - True) - self.assertAlmostEqual(non_max_suppressor.keywords['score_thresh'], 0.7) - self.assertAlmostEqual(non_max_suppressor.keywords['iou_thresh'], 0.6) - - def test_build_identity_score_converter(self): - post_processing_text_proto = """ - score_converter: IDENTITY - """ - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - _, score_converter = post_processing_builder.build( - post_processing_config) - self.assertEqual(score_converter.__name__, 'identity_with_logit_scale') - def graph_fn(): - inputs = tf.constant([1, 1], tf.float32) - outputs = score_converter(inputs) - return outputs - converted_scores = self.execute_cpu(graph_fn, []) - self.assertAllClose(converted_scores, [1, 1]) - - def test_build_identity_score_converter_with_logit_scale(self): - post_processing_text_proto = """ - score_converter: IDENTITY - logit_scale: 2.0 - """ - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - _, score_converter = post_processing_builder.build(post_processing_config) - self.assertEqual(score_converter.__name__, 'identity_with_logit_scale') - - def graph_fn(): - inputs = tf.constant([1, 1], tf.float32) - outputs = score_converter(inputs) - return outputs - converted_scores = self.execute_cpu(graph_fn, []) - self.assertAllClose(converted_scores, [.5, .5]) - - def test_build_sigmoid_score_converter(self): - post_processing_text_proto = """ - score_converter: SIGMOID - """ - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - _, score_converter = post_processing_builder.build(post_processing_config) - self.assertEqual(score_converter.__name__, 'sigmoid_with_logit_scale') - - def test_build_softmax_score_converter(self): - post_processing_text_proto = """ - score_converter: SOFTMAX - """ - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - _, score_converter = post_processing_builder.build(post_processing_config) - self.assertEqual(score_converter.__name__, 'softmax_with_logit_scale') - - def test_build_softmax_score_converter_with_temperature(self): - post_processing_text_proto = """ - score_converter: SOFTMAX - logit_scale: 2.0 - """ - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - _, score_converter = post_processing_builder.build(post_processing_config) - self.assertEqual(score_converter.__name__, 'softmax_with_logit_scale') - - def test_build_calibrator_with_nonempty_config(self): - """Test that identity function used when no calibration_config specified.""" - # Calibration config maps all scores to 0.5. - post_processing_text_proto = """ - score_converter: SOFTMAX - calibration_config { - function_approximation { - x_y_pairs { - x_y_pair { - x: 0.0 - y: 0.5 - } - x_y_pair { - x: 1.0 - y: 0.5 - }}}}""" - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - _, calibrated_score_conversion_fn = post_processing_builder.build( - post_processing_config) - self.assertEqual(calibrated_score_conversion_fn.__name__, - 'calibrate_with_function_approximation') - - def graph_fn(): - input_scores = tf.constant([1, 1], tf.float32) - outputs = calibrated_score_conversion_fn(input_scores) - return outputs - calibrated_scores = self.execute_cpu(graph_fn, []) - self.assertAllClose(calibrated_scores, [0.5, 0.5]) - - def test_build_temperature_scaling_calibrator(self): - post_processing_text_proto = """ - score_converter: SOFTMAX - calibration_config { - temperature_scaling_calibration { - scaler: 2.0 - }}""" - post_processing_config = post_processing_pb2.PostProcessing() - text_format.Merge(post_processing_text_proto, post_processing_config) - _, calibrated_score_conversion_fn = post_processing_builder.build( - post_processing_config) - self.assertEqual(calibrated_score_conversion_fn.__name__, - 'calibrate_with_temperature_scaling_calibration') - - def graph_fn(): - input_scores = tf.constant([1, 1], tf.float32) - outputs = calibrated_score_conversion_fn(input_scores) - return outputs - calibrated_scores = self.execute_cpu(graph_fn, []) - self.assertAllClose(calibrated_scores, [0.5, 0.5]) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/preprocessor_builder.py b/research/object_detection/builders/preprocessor_builder.py deleted file mode 100644 index 6dc0e189c22..00000000000 --- a/research/object_detection/builders/preprocessor_builder.py +++ /dev/null @@ -1,443 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Builder for preprocessing steps.""" - -import tensorflow.compat.v1 as tf - -from object_detection.core import preprocessor -from object_detection.protos import preprocessor_pb2 - - -def _get_step_config_from_proto(preprocessor_step_config, step_name): - """Returns the value of a field named step_name from proto. - - Args: - preprocessor_step_config: A preprocessor_pb2.PreprocessingStep object. - step_name: Name of the field to get value from. - - Returns: - result_dict: a sub proto message from preprocessor_step_config which will be - later converted to a dictionary. - - Raises: - ValueError: If field does not exist in proto. - """ - for field, value in preprocessor_step_config.ListFields(): - if field.name == step_name: - return value - - raise ValueError('Could not get field %s from proto!' % step_name) - - -def _get_dict_from_proto(config): - """Helper function to put all proto fields into a dictionary. - - For many preprocessing steps, there's an trivial 1-1 mapping from proto fields - to function arguments. This function automatically populates a dictionary with - the arguments from the proto. - - Protos that CANNOT be trivially populated include: - * nested messages. - * steps that check if an optional field is set (ie. where None != 0). - * protos that don't map 1-1 to arguments (ie. list should be reshaped). - * fields requiring additional validation (ie. repeated field has n elements). - - Args: - config: A protobuf object that does not violate the conditions above. - - Returns: - result_dict: |config| converted into a python dictionary. - """ - result_dict = {} - for field, value in config.ListFields(): - result_dict[field.name] = value - return result_dict - - -# A map from a PreprocessingStep proto config field name to the preprocessing -# function that should be used. The PreprocessingStep proto should be parsable -# with _get_dict_from_proto. -PREPROCESSING_FUNCTION_MAP = { - 'normalize_image': - preprocessor.normalize_image, - 'random_pixel_value_scale': - preprocessor.random_pixel_value_scale, - 'random_image_scale': - preprocessor.random_image_scale, - 'random_rgb_to_gray': - preprocessor.random_rgb_to_gray, - 'random_adjust_brightness': - preprocessor.random_adjust_brightness, - 'random_adjust_contrast': - preprocessor.random_adjust_contrast, - 'random_adjust_hue': - preprocessor.random_adjust_hue, - 'random_adjust_saturation': - preprocessor.random_adjust_saturation, - 'random_distort_color': - preprocessor.random_distort_color, - 'random_crop_to_aspect_ratio': - preprocessor.random_crop_to_aspect_ratio, - 'random_black_patches': - preprocessor.random_black_patches, - 'random_jpeg_quality': - preprocessor.random_jpeg_quality, - 'random_downscale_to_target_pixels': - preprocessor.random_downscale_to_target_pixels, - 'random_patch_gaussian': - preprocessor.random_patch_gaussian, - 'rgb_to_gray': - preprocessor.rgb_to_gray, - 'scale_boxes_to_pixel_coordinates': - (preprocessor.scale_boxes_to_pixel_coordinates), - 'subtract_channel_mean': - preprocessor.subtract_channel_mean, - 'convert_class_logits_to_softmax': - preprocessor.convert_class_logits_to_softmax, - 'adjust_gamma': - preprocessor.adjust_gamma, -} - - -# A map to convert from preprocessor_pb2.ResizeImage.Method enum to -# tf.image.ResizeMethod. -RESIZE_METHOD_MAP = { - preprocessor_pb2.ResizeImage.AREA: tf.image.ResizeMethod.AREA, - preprocessor_pb2.ResizeImage.BICUBIC: tf.image.ResizeMethod.BICUBIC, - preprocessor_pb2.ResizeImage.BILINEAR: tf.image.ResizeMethod.BILINEAR, - preprocessor_pb2.ResizeImage.NEAREST_NEIGHBOR: ( - tf.image.ResizeMethod.NEAREST_NEIGHBOR), -} - - -def get_random_jitter_kwargs(proto): - return { - 'ratio': - proto.ratio, - 'jitter_mode': - preprocessor_pb2.RandomJitterBoxes.JitterMode.Name(proto.jitter_mode - ).lower() - } - - -def build(preprocessor_step_config): - """Builds preprocessing step based on the configuration. - - Args: - preprocessor_step_config: PreprocessingStep configuration proto. - - Returns: - function, argmap: A callable function and an argument map to call function - with. - - Raises: - ValueError: On invalid configuration. - """ - step_type = preprocessor_step_config.WhichOneof('preprocessing_step') - - if step_type in PREPROCESSING_FUNCTION_MAP: - preprocessing_function = PREPROCESSING_FUNCTION_MAP[step_type] - step_config = _get_step_config_from_proto(preprocessor_step_config, - step_type) - function_args = _get_dict_from_proto(step_config) - return (preprocessing_function, function_args) - - if step_type == 'random_horizontal_flip': - config = preprocessor_step_config.random_horizontal_flip - return (preprocessor.random_horizontal_flip, - { - 'keypoint_flip_permutation': tuple( - config.keypoint_flip_permutation) or None, - 'probability': config.probability or None, - }) - - if step_type == 'random_vertical_flip': - config = preprocessor_step_config.random_vertical_flip - return (preprocessor.random_vertical_flip, - { - 'keypoint_flip_permutation': tuple( - config.keypoint_flip_permutation) or None, - 'probability': config.probability or None, - }) - - if step_type == 'random_rotation90': - config = preprocessor_step_config.random_rotation90 - return (preprocessor.random_rotation90, - { - 'keypoint_rot_permutation': tuple( - config.keypoint_rot_permutation) or None, - 'probability': config.probability or None, - }) - - if step_type == 'random_crop_image': - config = preprocessor_step_config.random_crop_image - return (preprocessor.random_crop_image, - { - 'min_object_covered': config.min_object_covered, - 'aspect_ratio_range': (config.min_aspect_ratio, - config.max_aspect_ratio), - 'area_range': (config.min_area, config.max_area), - 'overlap_thresh': config.overlap_thresh, - 'clip_boxes': config.clip_boxes, - 'random_coef': config.random_coef, - }) - - if step_type == 'random_pad_image': - config = preprocessor_step_config.random_pad_image - min_image_size = None - if (config.HasField('min_image_height') != - config.HasField('min_image_width')): - raise ValueError('min_image_height and min_image_width should be either ' - 'both set or both unset.') - if config.HasField('min_image_height'): - min_image_size = (config.min_image_height, config.min_image_width) - - max_image_size = None - if (config.HasField('max_image_height') != - config.HasField('max_image_width')): - raise ValueError('max_image_height and max_image_width should be either ' - 'both set or both unset.') - if config.HasField('max_image_height'): - max_image_size = (config.max_image_height, config.max_image_width) - - pad_color = config.pad_color or None - if pad_color: - if len(pad_color) != 3: - tf.logging.warn('pad_color should have 3 elements (RGB) if set!') - - pad_color = tf.cast([x for x in config.pad_color], dtype=tf.float32) - return (preprocessor.random_pad_image, - { - 'min_image_size': min_image_size, - 'max_image_size': max_image_size, - 'pad_color': pad_color, - }) - - if step_type == 'random_absolute_pad_image': - config = preprocessor_step_config.random_absolute_pad_image - - max_height_padding = config.max_height_padding or 1 - max_width_padding = config.max_width_padding or 1 - - pad_color = config.pad_color or None - if pad_color: - if len(pad_color) != 3: - tf.logging.warn('pad_color should have 3 elements (RGB) if set!') - - pad_color = tf.cast([x for x in config.pad_color], dtype=tf.float32) - - return (preprocessor.random_absolute_pad_image, - { - 'max_height_padding': max_height_padding, - 'max_width_padding': max_width_padding, - 'pad_color': pad_color, - }) - if step_type == 'random_crop_pad_image': - config = preprocessor_step_config.random_crop_pad_image - min_padded_size_ratio = config.min_padded_size_ratio - if min_padded_size_ratio and len(min_padded_size_ratio) != 2: - raise ValueError('min_padded_size_ratio should have 2 elements if set!') - max_padded_size_ratio = config.max_padded_size_ratio - if max_padded_size_ratio and len(max_padded_size_ratio) != 2: - raise ValueError('max_padded_size_ratio should have 2 elements if set!') - pad_color = config.pad_color or None - if pad_color: - if len(pad_color) != 3: - tf.logging.warn('pad_color should have 3 elements (RGB) if set!') - - pad_color = tf.cast([x for x in config.pad_color], dtype=tf.float32) - - kwargs = { - 'min_object_covered': config.min_object_covered, - 'aspect_ratio_range': (config.min_aspect_ratio, - config.max_aspect_ratio), - 'area_range': (config.min_area, config.max_area), - 'overlap_thresh': config.overlap_thresh, - 'clip_boxes': config.clip_boxes, - 'random_coef': config.random_coef, - 'pad_color': pad_color, - } - if min_padded_size_ratio: - kwargs['min_padded_size_ratio'] = tuple(min_padded_size_ratio) - if max_padded_size_ratio: - kwargs['max_padded_size_ratio'] = tuple(max_padded_size_ratio) - return (preprocessor.random_crop_pad_image, kwargs) - - if step_type == 'random_resize_method': - config = preprocessor_step_config.random_resize_method - return (preprocessor.random_resize_method, - { - 'target_size': [config.target_height, config.target_width], - }) - - if step_type == 'resize_image': - config = preprocessor_step_config.resize_image - method = RESIZE_METHOD_MAP[config.method] - return (preprocessor.resize_image, - { - 'new_height': config.new_height, - 'new_width': config.new_width, - 'method': method - }) - - if step_type == 'random_self_concat_image': - config = preprocessor_step_config.random_self_concat_image - return (preprocessor.random_self_concat_image, { - 'concat_vertical_probability': config.concat_vertical_probability, - 'concat_horizontal_probability': config.concat_horizontal_probability - }) - - if step_type == 'ssd_random_crop': - config = preprocessor_step_config.ssd_random_crop - if config.operations: - min_object_covered = [op.min_object_covered for op in config.operations] - aspect_ratio_range = [(op.min_aspect_ratio, op.max_aspect_ratio) - for op in config.operations] - area_range = [(op.min_area, op.max_area) for op in config.operations] - overlap_thresh = [op.overlap_thresh for op in config.operations] - clip_boxes = [op.clip_boxes for op in config.operations] - random_coef = [op.random_coef for op in config.operations] - return (preprocessor.ssd_random_crop, - { - 'min_object_covered': min_object_covered, - 'aspect_ratio_range': aspect_ratio_range, - 'area_range': area_range, - 'overlap_thresh': overlap_thresh, - 'clip_boxes': clip_boxes, - 'random_coef': random_coef, - }) - return (preprocessor.ssd_random_crop, {}) - - if step_type == 'autoaugment_image': - config = preprocessor_step_config.autoaugment_image - return (preprocessor.autoaugment_image, { - 'policy_name': config.policy_name, - }) - - if step_type == 'drop_label_probabilistically': - config = preprocessor_step_config.drop_label_probabilistically - return (preprocessor.drop_label_probabilistically, { - 'dropped_label': config.label, - 'drop_probability': config.drop_probability, - }) - - if step_type == 'remap_labels': - config = preprocessor_step_config.remap_labels - return (preprocessor.remap_labels, { - 'original_labels': config.original_labels, - 'new_label': config.new_label - }) - - if step_type == 'ssd_random_crop_pad': - config = preprocessor_step_config.ssd_random_crop_pad - if config.operations: - min_object_covered = [op.min_object_covered for op in config.operations] - aspect_ratio_range = [(op.min_aspect_ratio, op.max_aspect_ratio) - for op in config.operations] - area_range = [(op.min_area, op.max_area) for op in config.operations] - overlap_thresh = [op.overlap_thresh for op in config.operations] - clip_boxes = [op.clip_boxes for op in config.operations] - random_coef = [op.random_coef for op in config.operations] - min_padded_size_ratio = [tuple(op.min_padded_size_ratio) - for op in config.operations] - max_padded_size_ratio = [tuple(op.max_padded_size_ratio) - for op in config.operations] - pad_color = [(op.pad_color_r, op.pad_color_g, op.pad_color_b) - for op in config.operations] - return (preprocessor.ssd_random_crop_pad, - { - 'min_object_covered': min_object_covered, - 'aspect_ratio_range': aspect_ratio_range, - 'area_range': area_range, - 'overlap_thresh': overlap_thresh, - 'clip_boxes': clip_boxes, - 'random_coef': random_coef, - 'min_padded_size_ratio': min_padded_size_ratio, - 'max_padded_size_ratio': max_padded_size_ratio, - 'pad_color': pad_color, - }) - return (preprocessor.ssd_random_crop_pad, {}) - - if step_type == 'ssd_random_crop_fixed_aspect_ratio': - config = preprocessor_step_config.ssd_random_crop_fixed_aspect_ratio - if config.operations: - min_object_covered = [op.min_object_covered for op in config.operations] - area_range = [(op.min_area, op.max_area) for op in config.operations] - overlap_thresh = [op.overlap_thresh for op in config.operations] - clip_boxes = [op.clip_boxes for op in config.operations] - random_coef = [op.random_coef for op in config.operations] - return (preprocessor.ssd_random_crop_fixed_aspect_ratio, - { - 'min_object_covered': min_object_covered, - 'aspect_ratio': config.aspect_ratio, - 'area_range': area_range, - 'overlap_thresh': overlap_thresh, - 'clip_boxes': clip_boxes, - 'random_coef': random_coef, - }) - return (preprocessor.ssd_random_crop_fixed_aspect_ratio, {}) - - if step_type == 'ssd_random_crop_pad_fixed_aspect_ratio': - config = preprocessor_step_config.ssd_random_crop_pad_fixed_aspect_ratio - kwargs = {} - aspect_ratio = config.aspect_ratio - if aspect_ratio: - kwargs['aspect_ratio'] = aspect_ratio - min_padded_size_ratio = config.min_padded_size_ratio - if min_padded_size_ratio: - if len(min_padded_size_ratio) != 2: - raise ValueError('min_padded_size_ratio should have 2 elements if set!') - kwargs['min_padded_size_ratio'] = tuple(min_padded_size_ratio) - max_padded_size_ratio = config.max_padded_size_ratio - if max_padded_size_ratio: - if len(max_padded_size_ratio) != 2: - raise ValueError('max_padded_size_ratio should have 2 elements if set!') - kwargs['max_padded_size_ratio'] = tuple(max_padded_size_ratio) - if config.operations: - kwargs['min_object_covered'] = [op.min_object_covered - for op in config.operations] - kwargs['aspect_ratio_range'] = [(op.min_aspect_ratio, op.max_aspect_ratio) - for op in config.operations] - kwargs['area_range'] = [(op.min_area, op.max_area) - for op in config.operations] - kwargs['overlap_thresh'] = [op.overlap_thresh for op in config.operations] - kwargs['clip_boxes'] = [op.clip_boxes for op in config.operations] - kwargs['random_coef'] = [op.random_coef for op in config.operations] - return (preprocessor.ssd_random_crop_pad_fixed_aspect_ratio, kwargs) - - if step_type == 'random_square_crop_by_scale': - config = preprocessor_step_config.random_square_crop_by_scale - return preprocessor.random_square_crop_by_scale, { - 'scale_min': config.scale_min, - 'scale_max': config.scale_max, - 'max_border': config.max_border, - 'num_scales': config.num_scales - } - - if step_type == 'random_scale_crop_and_pad_to_square': - config = preprocessor_step_config.random_scale_crop_and_pad_to_square - return preprocessor.random_scale_crop_and_pad_to_square, { - 'scale_min': config.scale_min, - 'scale_max': config.scale_max, - 'output_size': config.output_size, - } - - - if step_type == 'random_jitter_boxes': - config = preprocessor_step_config.random_jitter_boxes - kwargs = get_random_jitter_kwargs(config) - return preprocessor.random_jitter_boxes, kwargs - raise ValueError('Unknown preprocessing step.') diff --git a/research/object_detection/builders/preprocessor_builder_test.py b/research/object_detection/builders/preprocessor_builder_test.py deleted file mode 100644 index 5579bba0a32..00000000000 --- a/research/object_detection/builders/preprocessor_builder_test.py +++ /dev/null @@ -1,773 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for preprocessor_builder.""" - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import preprocessor_builder -from object_detection.core import preprocessor -from object_detection.protos import preprocessor_pb2 - - -class PreprocessorBuilderTest(tf.test.TestCase): - - def assert_dictionary_close(self, dict1, dict2): - """Helper to check if two dicts with floatst or integers are close.""" - self.assertEqual(sorted(dict1.keys()), sorted(dict2.keys())) - for key in dict1: - value = dict1[key] - if isinstance(value, float): - self.assertAlmostEqual(value, dict2[key]) - else: - self.assertEqual(value, dict2[key]) - - def test_build_normalize_image(self): - preprocessor_text_proto = """ - normalize_image { - original_minval: 0.0 - original_maxval: 255.0 - target_minval: -1.0 - target_maxval: 1.0 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.normalize_image) - self.assertEqual(args, { - 'original_minval': 0.0, - 'original_maxval': 255.0, - 'target_minval': -1.0, - 'target_maxval': 1.0, - }) - - def test_build_random_horizontal_flip(self): - preprocessor_text_proto = """ - random_horizontal_flip { - keypoint_flip_permutation: 1 - keypoint_flip_permutation: 0 - keypoint_flip_permutation: 2 - keypoint_flip_permutation: 3 - keypoint_flip_permutation: 5 - keypoint_flip_permutation: 4 - probability: 0.5 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_horizontal_flip) - self.assertEqual(args, {'keypoint_flip_permutation': (1, 0, 2, 3, 5, 4), - 'probability': 0.5}) - - def test_build_random_vertical_flip(self): - preprocessor_text_proto = """ - random_vertical_flip { - keypoint_flip_permutation: 1 - keypoint_flip_permutation: 0 - keypoint_flip_permutation: 2 - keypoint_flip_permutation: 3 - keypoint_flip_permutation: 5 - keypoint_flip_permutation: 4 - probability: 0.5 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_vertical_flip) - self.assertEqual(args, {'keypoint_flip_permutation': (1, 0, 2, 3, 5, 4), - 'probability': 0.5}) - - def test_build_random_rotation90(self): - preprocessor_text_proto = """ - random_rotation90 { - keypoint_rot_permutation: 3 - keypoint_rot_permutation: 0 - keypoint_rot_permutation: 1 - keypoint_rot_permutation: 2 - probability: 0.5 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_rotation90) - self.assertEqual(args, {'keypoint_rot_permutation': (3, 0, 1, 2), - 'probability': 0.5}) - - def test_build_random_pixel_value_scale(self): - preprocessor_text_proto = """ - random_pixel_value_scale { - minval: 0.8 - maxval: 1.2 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_pixel_value_scale) - self.assert_dictionary_close(args, {'minval': 0.8, 'maxval': 1.2}) - - def test_build_random_image_scale(self): - preprocessor_text_proto = """ - random_image_scale { - min_scale_ratio: 0.8 - max_scale_ratio: 2.2 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_image_scale) - self.assert_dictionary_close(args, {'min_scale_ratio': 0.8, - 'max_scale_ratio': 2.2}) - - def test_build_random_rgb_to_gray(self): - preprocessor_text_proto = """ - random_rgb_to_gray { - probability: 0.8 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_rgb_to_gray) - self.assert_dictionary_close(args, {'probability': 0.8}) - - def test_build_random_adjust_brightness(self): - preprocessor_text_proto = """ - random_adjust_brightness { - max_delta: 0.2 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_adjust_brightness) - self.assert_dictionary_close(args, {'max_delta': 0.2}) - - def test_build_random_adjust_contrast(self): - preprocessor_text_proto = """ - random_adjust_contrast { - min_delta: 0.7 - max_delta: 1.1 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_adjust_contrast) - self.assert_dictionary_close(args, {'min_delta': 0.7, 'max_delta': 1.1}) - - def test_build_random_adjust_hue(self): - preprocessor_text_proto = """ - random_adjust_hue { - max_delta: 0.01 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_adjust_hue) - self.assert_dictionary_close(args, {'max_delta': 0.01}) - - def test_build_random_adjust_saturation(self): - preprocessor_text_proto = """ - random_adjust_saturation { - min_delta: 0.75 - max_delta: 1.15 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_adjust_saturation) - self.assert_dictionary_close(args, {'min_delta': 0.75, 'max_delta': 1.15}) - - def test_build_random_distort_color(self): - preprocessor_text_proto = """ - random_distort_color { - color_ordering: 1 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_distort_color) - self.assertEqual(args, {'color_ordering': 1}) - - def test_build_random_jitter_boxes(self): - preprocessor_text_proto = """ - random_jitter_boxes { - ratio: 0.1 - jitter_mode: SHRINK - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_jitter_boxes) - self.assert_dictionary_close(args, {'ratio': 0.1, 'jitter_mode': 'shrink'}) - - def test_build_random_crop_image(self): - preprocessor_text_proto = """ - random_crop_image { - min_object_covered: 0.75 - min_aspect_ratio: 0.75 - max_aspect_ratio: 1.5 - min_area: 0.25 - max_area: 0.875 - overlap_thresh: 0.5 - clip_boxes: False - random_coef: 0.125 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_crop_image) - self.assertEqual(args, { - 'min_object_covered': 0.75, - 'aspect_ratio_range': (0.75, 1.5), - 'area_range': (0.25, 0.875), - 'overlap_thresh': 0.5, - 'clip_boxes': False, - 'random_coef': 0.125, - }) - - def test_build_random_pad_image(self): - preprocessor_text_proto = """ - random_pad_image { - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_pad_image) - self.assertEqual(args, { - 'min_image_size': None, - 'max_image_size': None, - 'pad_color': None, - }) - - def test_build_random_absolute_pad_image(self): - preprocessor_text_proto = """ - random_absolute_pad_image { - max_height_padding: 50 - max_width_padding: 100 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_absolute_pad_image) - self.assertEqual(args, { - 'max_height_padding': 50, - 'max_width_padding': 100, - 'pad_color': None, - }) - - def test_build_random_crop_pad_image(self): - preprocessor_text_proto = """ - random_crop_pad_image { - min_object_covered: 0.75 - min_aspect_ratio: 0.75 - max_aspect_ratio: 1.5 - min_area: 0.25 - max_area: 0.875 - overlap_thresh: 0.5 - clip_boxes: False - random_coef: 0.125 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_crop_pad_image) - self.assertEqual(args, { - 'min_object_covered': 0.75, - 'aspect_ratio_range': (0.75, 1.5), - 'area_range': (0.25, 0.875), - 'overlap_thresh': 0.5, - 'clip_boxes': False, - 'random_coef': 0.125, - 'pad_color': None, - }) - - def test_build_random_crop_pad_image_with_optional_parameters(self): - preprocessor_text_proto = """ - random_crop_pad_image { - min_object_covered: 0.75 - min_aspect_ratio: 0.75 - max_aspect_ratio: 1.5 - min_area: 0.25 - max_area: 0.875 - overlap_thresh: 0.5 - clip_boxes: False - random_coef: 0.125 - min_padded_size_ratio: 0.5 - min_padded_size_ratio: 0.75 - max_padded_size_ratio: 0.5 - max_padded_size_ratio: 0.75 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_crop_pad_image) - self.assertEqual(args, { - 'min_object_covered': 0.75, - 'aspect_ratio_range': (0.75, 1.5), - 'area_range': (0.25, 0.875), - 'overlap_thresh': 0.5, - 'clip_boxes': False, - 'random_coef': 0.125, - 'min_padded_size_ratio': (0.5, 0.75), - 'max_padded_size_ratio': (0.5, 0.75), - 'pad_color': None, - }) - - def test_build_random_crop_to_aspect_ratio(self): - preprocessor_text_proto = """ - random_crop_to_aspect_ratio { - aspect_ratio: 0.85 - overlap_thresh: 0.35 - clip_boxes: False - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_crop_to_aspect_ratio) - self.assert_dictionary_close(args, {'aspect_ratio': 0.85, - 'overlap_thresh': 0.35, - 'clip_boxes': False}) - - def test_build_random_black_patches(self): - preprocessor_text_proto = """ - random_black_patches { - max_black_patches: 20 - probability: 0.95 - size_to_image_ratio: 0.12 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_black_patches) - self.assert_dictionary_close(args, {'max_black_patches': 20, - 'probability': 0.95, - 'size_to_image_ratio': 0.12}) - - def test_build_random_jpeg_quality(self): - preprocessor_text_proto = """ - random_jpeg_quality { - random_coef: 0.5 - min_jpeg_quality: 40 - max_jpeg_quality: 90 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Parse(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_jpeg_quality) - self.assert_dictionary_close(args, {'random_coef': 0.5, - 'min_jpeg_quality': 40, - 'max_jpeg_quality': 90}) - - def test_build_random_downscale_to_target_pixels(self): - preprocessor_text_proto = """ - random_downscale_to_target_pixels { - random_coef: 0.5 - min_target_pixels: 200 - max_target_pixels: 900 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Parse(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_downscale_to_target_pixels) - self.assert_dictionary_close(args, { - 'random_coef': 0.5, - 'min_target_pixels': 200, - 'max_target_pixels': 900 - }) - - def test_build_random_patch_gaussian(self): - preprocessor_text_proto = """ - random_patch_gaussian { - random_coef: 0.5 - min_patch_size: 10 - max_patch_size: 300 - min_gaussian_stddev: 0.2 - max_gaussian_stddev: 1.5 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Parse(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_patch_gaussian) - self.assert_dictionary_close(args, { - 'random_coef': 0.5, - 'min_patch_size': 10, - 'max_patch_size': 300, - 'min_gaussian_stddev': 0.2, - 'max_gaussian_stddev': 1.5 - }) - - def test_auto_augment_image(self): - preprocessor_text_proto = """ - autoaugment_image { - policy_name: 'v0' - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.autoaugment_image) - self.assert_dictionary_close(args, {'policy_name': 'v0'}) - - def test_drop_label_probabilistically(self): - preprocessor_text_proto = """ - drop_label_probabilistically{ - label: 2 - drop_probability: 0.5 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.drop_label_probabilistically) - self.assert_dictionary_close(args, { - 'dropped_label': 2, - 'drop_probability': 0.5 - }) - - def test_remap_labels(self): - preprocessor_text_proto = """ - remap_labels{ - original_labels: 1 - original_labels: 2 - new_label: 3 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.remap_labels) - self.assert_dictionary_close(args, { - 'original_labels': [1, 2], - 'new_label': 3 - }) - - def test_build_random_resize_method(self): - preprocessor_text_proto = """ - random_resize_method { - target_height: 75 - target_width: 100 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_resize_method) - self.assert_dictionary_close(args, {'target_size': [75, 100]}) - - def test_build_scale_boxes_to_pixel_coordinates(self): - preprocessor_text_proto = """ - scale_boxes_to_pixel_coordinates {} - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.scale_boxes_to_pixel_coordinates) - self.assertEqual(args, {}) - - def test_build_resize_image(self): - preprocessor_text_proto = """ - resize_image { - new_height: 75 - new_width: 100 - method: BICUBIC - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.resize_image) - self.assertEqual(args, {'new_height': 75, - 'new_width': 100, - 'method': tf.image.ResizeMethod.BICUBIC}) - - def test_build_rgb_to_gray(self): - preprocessor_text_proto = """ - rgb_to_gray {} - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.rgb_to_gray) - self.assertEqual(args, {}) - - def test_build_subtract_channel_mean(self): - preprocessor_text_proto = """ - subtract_channel_mean { - means: [1.0, 2.0, 3.0] - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.subtract_channel_mean) - self.assertEqual(args, {'means': [1.0, 2.0, 3.0]}) - - def test_random_self_concat_image(self): - preprocessor_text_proto = """ - random_self_concat_image { - concat_vertical_probability: 0.5 - concat_horizontal_probability: 0.25 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_self_concat_image) - self.assertEqual(args, {'concat_vertical_probability': 0.5, - 'concat_horizontal_probability': 0.25}) - - def test_build_ssd_random_crop(self): - preprocessor_text_proto = """ - ssd_random_crop { - operations { - min_object_covered: 0.0 - min_aspect_ratio: 0.875 - max_aspect_ratio: 1.125 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.0 - clip_boxes: False - random_coef: 0.375 - } - operations { - min_object_covered: 0.25 - min_aspect_ratio: 0.75 - max_aspect_ratio: 1.5 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.25 - clip_boxes: True - random_coef: 0.375 - } - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.ssd_random_crop) - self.assertEqual(args, {'min_object_covered': [0.0, 0.25], - 'aspect_ratio_range': [(0.875, 1.125), (0.75, 1.5)], - 'area_range': [(0.5, 1.0), (0.5, 1.0)], - 'overlap_thresh': [0.0, 0.25], - 'clip_boxes': [False, True], - 'random_coef': [0.375, 0.375]}) - - def test_build_ssd_random_crop_empty_operations(self): - preprocessor_text_proto = """ - ssd_random_crop { - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.ssd_random_crop) - self.assertEqual(args, {}) - - def test_build_ssd_random_crop_pad(self): - preprocessor_text_proto = """ - ssd_random_crop_pad { - operations { - min_object_covered: 0.0 - min_aspect_ratio: 0.875 - max_aspect_ratio: 1.125 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.0 - clip_boxes: False - random_coef: 0.375 - min_padded_size_ratio: [1.0, 1.0] - max_padded_size_ratio: [2.0, 2.0] - pad_color_r: 0.5 - pad_color_g: 0.5 - pad_color_b: 0.5 - } - operations { - min_object_covered: 0.25 - min_aspect_ratio: 0.75 - max_aspect_ratio: 1.5 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.25 - clip_boxes: True - random_coef: 0.375 - min_padded_size_ratio: [1.0, 1.0] - max_padded_size_ratio: [2.0, 2.0] - pad_color_r: 0.5 - pad_color_g: 0.5 - pad_color_b: 0.5 - } - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.ssd_random_crop_pad) - self.assertEqual(args, {'min_object_covered': [0.0, 0.25], - 'aspect_ratio_range': [(0.875, 1.125), (0.75, 1.5)], - 'area_range': [(0.5, 1.0), (0.5, 1.0)], - 'overlap_thresh': [0.0, 0.25], - 'clip_boxes': [False, True], - 'random_coef': [0.375, 0.375], - 'min_padded_size_ratio': [(1.0, 1.0), (1.0, 1.0)], - 'max_padded_size_ratio': [(2.0, 2.0), (2.0, 2.0)], - 'pad_color': [(0.5, 0.5, 0.5), (0.5, 0.5, 0.5)]}) - - def test_build_ssd_random_crop_fixed_aspect_ratio(self): - preprocessor_text_proto = """ - ssd_random_crop_fixed_aspect_ratio { - operations { - min_object_covered: 0.0 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.0 - clip_boxes: False - random_coef: 0.375 - } - operations { - min_object_covered: 0.25 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.25 - clip_boxes: True - random_coef: 0.375 - } - aspect_ratio: 0.875 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.ssd_random_crop_fixed_aspect_ratio) - self.assertEqual(args, {'min_object_covered': [0.0, 0.25], - 'aspect_ratio': 0.875, - 'area_range': [(0.5, 1.0), (0.5, 1.0)], - 'overlap_thresh': [0.0, 0.25], - 'clip_boxes': [False, True], - 'random_coef': [0.375, 0.375]}) - - def test_build_ssd_random_crop_pad_fixed_aspect_ratio(self): - preprocessor_text_proto = """ - ssd_random_crop_pad_fixed_aspect_ratio { - operations { - min_object_covered: 0.0 - min_aspect_ratio: 0.875 - max_aspect_ratio: 1.125 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.0 - clip_boxes: False - random_coef: 0.375 - } - operations { - min_object_covered: 0.25 - min_aspect_ratio: 0.75 - max_aspect_ratio: 1.5 - min_area: 0.5 - max_area: 1.0 - overlap_thresh: 0.25 - clip_boxes: True - random_coef: 0.375 - } - aspect_ratio: 0.875 - min_padded_size_ratio: [1.0, 1.0] - max_padded_size_ratio: [2.0, 2.0] - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, - preprocessor.ssd_random_crop_pad_fixed_aspect_ratio) - self.assertEqual(args, {'min_object_covered': [0.0, 0.25], - 'aspect_ratio': 0.875, - 'aspect_ratio_range': [(0.875, 1.125), (0.75, 1.5)], - 'area_range': [(0.5, 1.0), (0.5, 1.0)], - 'overlap_thresh': [0.0, 0.25], - 'clip_boxes': [False, True], - 'random_coef': [0.375, 0.375], - 'min_padded_size_ratio': (1.0, 1.0), - 'max_padded_size_ratio': (2.0, 2.0)}) - - def test_build_normalize_image_convert_class_logits_to_softmax(self): - preprocessor_text_proto = """ - convert_class_logits_to_softmax { - temperature: 2 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.convert_class_logits_to_softmax) - self.assertEqual(args, {'temperature': 2}) - - def test_random_crop_by_scale(self): - preprocessor_text_proto = """ - random_square_crop_by_scale { - scale_min: 0.25 - scale_max: 2.0 - num_scales: 8 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Merge(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.random_square_crop_by_scale) - self.assertEqual(args, { - 'scale_min': 0.25, - 'scale_max': 2.0, - 'num_scales': 8, - 'max_border': 128 - }) - - def test_adjust_gamma(self): - preprocessor_text_proto = """ - adjust_gamma { - gamma: 2.2 - gain: 2.0 - } - """ - preprocessor_proto = preprocessor_pb2.PreprocessingStep() - text_format.Parse(preprocessor_text_proto, preprocessor_proto) - function, args = preprocessor_builder.build(preprocessor_proto) - self.assertEqual(function, preprocessor.adjust_gamma) - self.assert_dictionary_close(args, {'gamma': 2.2, 'gain': 2.0}) - - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/region_similarity_calculator_builder.py b/research/object_detection/builders/region_similarity_calculator_builder.py deleted file mode 100644 index 8f35087ff40..00000000000 --- a/research/object_detection/builders/region_similarity_calculator_builder.py +++ /dev/null @@ -1,59 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Builder for region similarity calculators.""" - -from object_detection.core import region_similarity_calculator -from object_detection.protos import region_similarity_calculator_pb2 - - -def build(region_similarity_calculator_config): - """Builds region similarity calculator based on the configuration. - - Builds one of [IouSimilarity, IoaSimilarity, NegSqDistSimilarity] objects. See - core/region_similarity_calculator.proto for details. - - Args: - region_similarity_calculator_config: RegionSimilarityCalculator - configuration proto. - - Returns: - region_similarity_calculator: RegionSimilarityCalculator object. - - Raises: - ValueError: On unknown region similarity calculator. - """ - - if not isinstance( - region_similarity_calculator_config, - region_similarity_calculator_pb2.RegionSimilarityCalculator): - raise ValueError( - 'region_similarity_calculator_config not of type ' - 'region_similarity_calculator_pb2.RegionsSimilarityCalculator') - - similarity_calculator = region_similarity_calculator_config.WhichOneof( - 'region_similarity') - if similarity_calculator == 'iou_similarity': - return region_similarity_calculator.IouSimilarity() - if similarity_calculator == 'ioa_similarity': - return region_similarity_calculator.IoaSimilarity() - if similarity_calculator == 'neg_sq_dist_similarity': - return region_similarity_calculator.NegSqDistSimilarity() - if similarity_calculator == 'thresholded_iou_similarity': - return region_similarity_calculator.ThresholdedIouSimilarity( - region_similarity_calculator_config.thresholded_iou_similarity - .iou_threshold) - - raise ValueError('Unknown region similarity calculator.') diff --git a/research/object_detection/builders/region_similarity_calculator_builder_test.py b/research/object_detection/builders/region_similarity_calculator_builder_test.py deleted file mode 100644 index da72e7360ee..00000000000 --- a/research/object_detection/builders/region_similarity_calculator_builder_test.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for region_similarity_calculator_builder.""" - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import region_similarity_calculator_builder -from object_detection.core import region_similarity_calculator -from object_detection.protos import region_similarity_calculator_pb2 as sim_calc_pb2 - - -class RegionSimilarityCalculatorBuilderTest(tf.test.TestCase): - - def testBuildIoaSimilarityCalculator(self): - similarity_calc_text_proto = """ - ioa_similarity { - } - """ - similarity_calc_proto = sim_calc_pb2.RegionSimilarityCalculator() - text_format.Merge(similarity_calc_text_proto, similarity_calc_proto) - similarity_calc = region_similarity_calculator_builder.build( - similarity_calc_proto) - self.assertTrue(isinstance(similarity_calc, - region_similarity_calculator.IoaSimilarity)) - - def testBuildIouSimilarityCalculator(self): - similarity_calc_text_proto = """ - iou_similarity { - } - """ - similarity_calc_proto = sim_calc_pb2.RegionSimilarityCalculator() - text_format.Merge(similarity_calc_text_proto, similarity_calc_proto) - similarity_calc = region_similarity_calculator_builder.build( - similarity_calc_proto) - self.assertTrue(isinstance(similarity_calc, - region_similarity_calculator.IouSimilarity)) - - def testBuildNegSqDistSimilarityCalculator(self): - similarity_calc_text_proto = """ - neg_sq_dist_similarity { - } - """ - similarity_calc_proto = sim_calc_pb2.RegionSimilarityCalculator() - text_format.Merge(similarity_calc_text_proto, similarity_calc_proto) - similarity_calc = region_similarity_calculator_builder.build( - similarity_calc_proto) - self.assertTrue(isinstance(similarity_calc, - region_similarity_calculator. - NegSqDistSimilarity)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/builders/target_assigner_builder.py b/research/object_detection/builders/target_assigner_builder.py deleted file mode 100644 index f6434f653c8..00000000000 --- a/research/object_detection/builders/target_assigner_builder.py +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A function to build an object detection box coder from configuration.""" -from object_detection.builders import box_coder_builder -from object_detection.builders import matcher_builder -from object_detection.builders import region_similarity_calculator_builder -from object_detection.core import target_assigner - - -def build(target_assigner_config): - """Builds a TargetAssigner object based on the config. - - Args: - target_assigner_config: A target_assigner proto message containing config - for the desired target assigner. - - Returns: - TargetAssigner object based on the config. - """ - matcher_instance = matcher_builder.build(target_assigner_config.matcher) - similarity_calc_instance = region_similarity_calculator_builder.build( - target_assigner_config.similarity_calculator) - box_coder = box_coder_builder.build(target_assigner_config.box_coder) - return target_assigner.TargetAssigner( - matcher=matcher_instance, - similarity_calc=similarity_calc_instance, - box_coder_instance=box_coder) diff --git a/research/object_detection/builders/target_assigner_builder_test.py b/research/object_detection/builders/target_assigner_builder_test.py deleted file mode 100644 index 27960021484..00000000000 --- a/research/object_detection/builders/target_assigner_builder_test.py +++ /dev/null @@ -1,50 +0,0 @@ -"""Tests for google3.third_party.tensorflow_models.object_detection.builders.target_assigner_builder.""" -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - - -from object_detection.builders import target_assigner_builder -from object_detection.core import target_assigner -from object_detection.protos import target_assigner_pb2 - - -class TargetAssignerBuilderTest(tf.test.TestCase): - - def test_build_a_target_assigner(self): - target_assigner_text_proto = """ - matcher { - argmax_matcher {matched_threshold: 0.5} - } - similarity_calculator { - iou_similarity {} - } - box_coder { - faster_rcnn_box_coder {} - } - """ - target_assigner_proto = target_assigner_pb2.TargetAssigner() - text_format.Merge(target_assigner_text_proto, target_assigner_proto) - target_assigner_instance = target_assigner_builder.build( - target_assigner_proto) - self.assertIsInstance(target_assigner_instance, - target_assigner.TargetAssigner) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/colab_tutorials/centernet_on_device.ipynb b/research/object_detection/colab_tutorials/centernet_on_device.ipynb deleted file mode 100644 index e492f80df0f..00000000000 --- a/research/object_detection/colab_tutorials/centernet_on_device.ipynb +++ /dev/null @@ -1,762 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "centernet_on_mobile.ipynb", - "provenance": [], - "collapsed_sections": [], - "toc_visible": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "pDIqEDDxWAh2" - }, - "source": [ - "#Introduction\r\n", - "\r\n", - "Welcome to the **CenterNet on-device with TensorFlow Lite** Colab. Here, we demonstrate how you can run a mobile-optimized version of the [CenterNet](https://arxiv.org/abs/1904.08189) architecture with [TensorFlow Lite](https://www.tensorflow.org/lite) (a.k.a. TFLite). \r\n", - "\r\n", - "Users can use this notebook as a reference for obtaining TFLite version of CenterNet for *Object Detection* or [*Keypoint detection*](https://cocodataset.org/#keypoints-2020). The code also shows how to perform pre-/post-processing & inference with TFLite's Python API.\r\n", - "\r\n", - "**NOTE:** CenterNet support in TFLite is still experimental, and currently works with floating-point inference only." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3LQWTJ-BWzmW" - }, - "source": [ - "# Set Up" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gx84EpH7INPj" - }, - "source": [ - "## Libraries & Imports" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "EU_hXi7IW9QC" - }, - "source": [ - "!pip install tf-nightly" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "ZTU9_JcOZz-J" - }, - "source": [ - "import os\r\n", - "import pathlib\r\n", - "\r\n", - "# Clone the tensorflow models repository if it doesn't already exist\r\n", - "if \"models\" in pathlib.Path.cwd().parts:\r\n", - " while \"models\" in pathlib.Path.cwd().parts:\r\n", - " os.chdir('..')\r\n", - "elif not pathlib.Path('models').exists():\r\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "laJxis1WZ2xj" - }, - "source": [ - "# Install the Object Detection API\r\n", - "%%bash\r\n", - "cd models/research/\r\n", - "protoc object_detection/protos/*.proto --python_out=.\r\n", - "cp object_detection/packages/tf2/setup.py .\r\n", - "python -m pip install ." - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "je0LrJNjDsk9" - }, - "source": [ - "import matplotlib\r\n", - "import matplotlib.pyplot as plt\r\n", - "\r\n", - "import os\r\n", - "import random\r\n", - "import io\r\n", - "import imageio\r\n", - "import glob\r\n", - "import scipy.misc\r\n", - "import numpy as np\r\n", - "from six import BytesIO\r\n", - "from PIL import Image, ImageDraw, ImageFont\r\n", - "from IPython.display import display, Javascript\r\n", - "from IPython.display import Image as IPyImage\r\n", - "\r\n", - "import tensorflow as tf\r\n", - "\r\n", - "from object_detection.utils import label_map_util\r\n", - "from object_detection.utils import config_util\r\n", - "from object_detection.utils import visualization_utils as viz_utils\r\n", - "from object_detection.utils import colab_utils\r\n", - "from object_detection.utils import config_util\r\n", - "from object_detection.builders import model_builder\r\n", - "\r\n", - "%matplotlib inline" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "O5IXwbhhH0bs" - }, - "source": [ - "## Test Image from COCO\r\n", - "\r\n", - "We use a sample image from the COCO'17 validation dataset that contains people, to showcase inference with CenterNet." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "h-JuG84HDvm-" - }, - "source": [ - "# Download COCO'17 validation set for test image\r\n", - "%%bash\r\n", - "mkdir -p coco && cd coco\r\n", - "wget -q -N http://images.cocodataset.org/zips/val2017.zip\r\n", - "unzip -q -o val2017.zip && rm *.zip\r\n", - "cd .." - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "peX5mPGmEj8s" - }, - "source": [ - "# Print the image we are going to test on as a sanity check.\r\n", - "\r\n", - "def load_image_into_numpy_array(path):\r\n", - " \"\"\"Load an image from file into a numpy array.\r\n", - "\r\n", - " Puts image into numpy array to feed into tensorflow graph.\r\n", - " Note that by convention we put it into a numpy array with shape\r\n", - " (height, width, channels), where channels=3 for RGB.\r\n", - "\r\n", - " Args:\r\n", - " path: a file path.\r\n", - "\r\n", - " Returns:\r\n", - " uint8 numpy array with shape (img_height, img_width, 3)\r\n", - " \"\"\"\r\n", - " img_data = tf.io.gfile.GFile(path, 'rb').read()\r\n", - " image = Image.open(BytesIO(img_data))\r\n", - " (im_width, im_height) = image.size\r\n", - " return np.array(image.getdata()).reshape(\r\n", - " (im_height, im_width, 3)).astype(np.uint8)\r\n", - "\r\n", - "image_path = 'coco/val2017/000000013729.jpg'\r\n", - "plt.figure(figsize = (30, 20))\r\n", - "plt.imshow(load_image_into_numpy_array(image_path))" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6cqOdvfrR1vW" - }, - "source": [ - "## Utilities for Inference\r\n", - "\r\n", - "The `detect` function shown below describes how input and output tensors from CenterNet (obtained in subsequent sections) can be processed. This logic can be ported to other languages depending on your application (for e.g. to Java for Android apps)." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "13Ouc2C3RyqR" - }, - "source": [ - "def detect(interpreter, input_tensor, include_keypoint=False):\r\n", - " \"\"\"Run detection on an input image.\r\n", - "\r\n", - " Args:\r\n", - " interpreter: tf.lite.Interpreter\r\n", - " input_tensor: A [1, height, width, 3] Tensor of type tf.float32.\r\n", - " Note that height and width can be anything since the image will be\r\n", - " immediately resized according to the needs of the model within this\r\n", - " function.\r\n", - " include_keypoint: True if model supports keypoints output. See\r\n", - " https://cocodataset.org/#keypoints-2020\r\n", - "\r\n", - " Returns:\r\n", - " A sequence containing the following output tensors:\r\n", - " boxes: a numpy array of shape [N, 4]\r\n", - " classes: a numpy array of shape [N]. Note that class indices are \r\n", - " 1-based, and match the keys in the label map.\r\n", - " scores: a numpy array of shape [N] or None. If scores=None, then\r\n", - " this function assumes that the boxes to be plotted are groundtruth\r\n", - " boxes and plot all boxes as black with no classes or scores.\r\n", - " category_index: a dict containing category dictionaries (each holding\r\n", - " category index `id` and category name `name`) keyed by category \r\n", - " indices.\r\n", - " If include_keypoints is True, the following are also returned:\r\n", - " keypoints: (optional) a numpy array of shape [N, 17, 2] representing\r\n", - " the yx-coordinates of the detection 17 COCO human keypoints\r\n", - " (https://cocodataset.org/#keypoints-2020) in normalized image frame\r\n", - " (i.e. [0.0, 1.0]). \r\n", - " keypoint_scores: (optional) a numpy array of shape [N, 17] representing the\r\n", - " keypoint prediction confidence scores.\r\n", - " \"\"\"\r\n", - " input_details = interpreter.get_input_details()\r\n", - " output_details = interpreter.get_output_details()\r\n", - "\r\n", - " interpreter.set_tensor(input_details[0]['index'], input_tensor.numpy())\r\n", - "\r\n", - " interpreter.invoke()\r\n", - "\r\n", - " scores = interpreter.get_tensor(output_details[0]['index'])\r\n", - " boxes = interpreter.get_tensor(output_details[1]['index'])\r\n", - " num_detections = interpreter.get_tensor(output_details[2]['index'])\r\n", - " classes = interpreter.get_tensor(output_details[3]['index'])\r\n", - "\r\n", - " if include_keypoint:\r\n", - " kpts = interpreter.get_tensor(output_details[4]['index'])\r\n", - " kpts_scores = interpreter.get_tensor(output_details[5]['index'])\r\n", - " return boxes, classes, scores, num_detections, kpts, kpts_scores\r\n", - " else:\r\n", - " return boxes, classes, scores, num_detections\r\n", - "\r\n", - "# Utility for visualizing results\r\n", - "def plot_detections(image_np,\r\n", - " boxes,\r\n", - " classes,\r\n", - " scores,\r\n", - " category_index,\r\n", - " keypoints=None,\r\n", - " keypoint_scores=None,\r\n", - " figsize=(12, 16),\r\n", - " image_name=None):\r\n", - " \"\"\"Wrapper function to visualize detections.\r\n", - "\r\n", - " Args:\r\n", - " image_np: uint8 numpy array with shape (img_height, img_width, 3)\r\n", - " boxes: a numpy array of shape [N, 4]\r\n", - " classes: a numpy array of shape [N]. Note that class indices are 1-based,\r\n", - " and match the keys in the label map.\r\n", - " scores: a numpy array of shape [N] or None. If scores=None, then\r\n", - " this function assumes that the boxes to be plotted are groundtruth\r\n", - " boxes and plot all boxes as black with no classes or scores.\r\n", - " category_index: a dict containing category dictionaries (each holding\r\n", - " category index `id` and category name `name`) keyed by category indices.\r\n", - " keypoints: (optional) a numpy array of shape [N, 17, 2] representing the \r\n", - " yx-coordinates of the detection 17 COCO human keypoints\r\n", - " (https://cocodataset.org/#keypoints-2020) in normalized image frame\r\n", - " (i.e. [0.0, 1.0]). \r\n", - " keypoint_scores: (optional) anumpy array of shape [N, 17] representing the\r\n", - " keypoint prediction confidence scores.\r\n", - " figsize: size for the figure.\r\n", - " image_name: a name for the image file.\r\n", - " \"\"\"\r\n", - "\r\n", - " keypoint_edges = [(0, 1),\r\n", - " (0, 2),\r\n", - " (1, 3),\r\n", - " (2, 4),\r\n", - " (0, 5),\r\n", - " (0, 6),\r\n", - " (5, 7),\r\n", - " (7, 9),\r\n", - " (6, 8),\r\n", - " (8, 10),\r\n", - " (5, 6),\r\n", - " (5, 11),\r\n", - " (6, 12),\r\n", - " (11, 12),\r\n", - " (11, 13),\r\n", - " (13, 15),\r\n", - " (12, 14),\r\n", - " (14, 16)]\r\n", - " image_np_with_annotations = image_np.copy()\r\n", - " # Only visualize objects that get a score > 0.3.\r\n", - " viz_utils.visualize_boxes_and_labels_on_image_array(\r\n", - " image_np_with_annotations,\r\n", - " boxes,\r\n", - " classes,\r\n", - " scores,\r\n", - " category_index,\r\n", - " keypoints=keypoints,\r\n", - " keypoint_scores=keypoint_scores,\r\n", - " keypoint_edges=keypoint_edges,\r\n", - " use_normalized_coordinates=True,\r\n", - " min_score_thresh=0.3)\r\n", - " if image_name:\r\n", - " plt.imsave(image_name, image_np_with_annotations)\r\n", - " else:\r\n", - " return image_np_with_annotations" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3cNYi8HuIWzO" - }, - "source": [ - "# Object Detection" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "azdDCdWQMSoH" - }, - "source": [ - "## Download Model from Detection Zoo\r\n", - "\r\n", - "**NOTE:** Not all CenterNet models from the [TF2 Detection Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md) work with TFLite, only the [MobileNet-based version](http://download.tensorflow.org/models/object_detection/tf2/20210210/centernet_mobilenetv2fpn_512x512_coco17_od.tar.gz) does.\r\n" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "Sywt8MKzIeOi" - }, - "source": [ - "# Get mobile-friendly CenterNet for Object Detection\r\n", - "# See TensorFlow 2 Detection Model Zoo for more details:\r\n", - "# https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md\r\n", - "\r\n", - "%%bash\r\n", - "wget http://download.tensorflow.org/models/object_detection/tf2/20210210/centernet_mobilenetv2fpn_512x512_coco17_od.tar.gz\r\n", - "tar -xf centernet_mobilenetv2fpn_512x512_coco17_od.tar.gz\r\n", - "rm centernet_mobilenetv2fpn_512x512_coco17_od.tar.gz*" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "MiRrVpTnLvsk" - }, - "source": [ - "Now that we have downloaded the CenterNet model that uses MobileNet as a backbone, we can obtain a TensorFlow Lite model from it. \r\n", - "\r\n", - "The downloaded archive already contains `model.tflite` that works with TensorFlow Lite, but we re-generate the model in the next sub-section to account for cases where you might re-train the model on your own dataset (with corresponding changes to `pipeline.config` & `checkpoint` directory)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "jT0bruuxM496" - }, - "source": [ - "## Generate TensorFlow Lite Model\r\n", - "\r\n", - "First, we invoke `export_tflite_graph_tf2.py` to generate a TFLite-friendly intermediate SavedModel. This will then be passed to the TensorFlow Lite Converter for generating the final model.\r\n", - "\r\n", - "This is similar to what we do for [SSD architectures](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_on_mobile_tf2.md)." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "jpcCjiQ_JrU5", - "collapsed": true - }, - "source": [ - "%%bash\r\n", - "# Export the intermediate SavedModel that outputs 10 detections & takes in an \r\n", - "# image of dim 320x320.\r\n", - "# Modify these parameters according to your needs.\r\n", - "\r\n", - "python models/research/object_detection/export_tflite_graph_tf2.py \\\r\n", - " --pipeline_config_path=centernet_mobilenetv2_fpn_od/pipeline.config \\\r\n", - " --trained_checkpoint_dir=centernet_mobilenetv2_fpn_od/checkpoint \\\r\n", - " --output_directory=centernet_mobilenetv2_fpn_od/tflite \\\r\n", - " --centernet_include_keypoints=false \\\r\n", - " --max_detections=10 \\\r\n", - " --config_override=\" \\\r\n", - " model{ \\\r\n", - " center_net { \\\r\n", - " image_resizer { \\\r\n", - " fixed_shape_resizer { \\\r\n", - " height: 320 \\\r\n", - " width: 320 \\\r\n", - " } \\\r\n", - " } \\\r\n", - " } \\\r\n", - " }\"" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "zhhP6HL8PUGq" - }, - "source": [ - "# Generate TensorFlow Lite model using the converter.\r\n", - "%%bash\r\n", - "tflite_convert --output_file=centernet_mobilenetv2_fpn_od/model.tflite \\\r\n", - " --saved_model_dir=centernet_mobilenetv2_fpn_od/tflite/saved_model" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gj1Q_e_2Rn5i" - }, - "source": [ - "## TensorFlow Lite Inference\r\n", - "\r\n", - "Use a TensorFlow Lite Interpreter to detect objects in the test image." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "uV9t9icURsei" - }, - "source": [ - "%matplotlib inline\r\n", - "\r\n", - "# Load the TFLite model and allocate tensors.\r\n", - "model_path = 'centernet_mobilenetv2_fpn_od/model.tflite'\r\n", - "label_map_path = 'centernet_mobilenetv2_fpn_od/label_map.txt'\r\n", - "image_path = 'coco/val2017/000000013729.jpg'\r\n", - "\r\n", - "# Initialize TensorFlow Lite Interpreter.\r\n", - "interpreter = tf.lite.Interpreter(model_path=model_path)\r\n", - "interpreter.allocate_tensors()\r\n", - "\r\n", - "# Label map can be used to figure out what class ID maps to what\r\n", - "# label. `label_map.txt` is human-readable.\r\n", - "category_index = label_map_util.create_category_index_from_labelmap(\r\n", - " label_map_path)\r\n", - "\r\n", - "label_id_offset = 1\r\n", - "\r\n", - "image = tf.io.read_file(image_path)\r\n", - "image = tf.compat.v1.image.decode_jpeg(image)\r\n", - "image = tf.expand_dims(image, axis=0)\r\n", - "image_numpy = image.numpy()\r\n", - "\r\n", - "input_tensor = tf.convert_to_tensor(image_numpy, dtype=tf.float32)\r\n", - "# Note that CenterNet doesn't require any pre-processing except resizing to the\r\n", - "# input size that the TensorFlow Lite Interpreter was generated with.\r\n", - "input_tensor = tf.image.resize(input_tensor, (320, 320))\r\n", - "boxes, classes, scores, num_detections = detect(interpreter, input_tensor)\r\n", - "\r\n", - "vis_image = plot_detections(\r\n", - " image_numpy[0],\r\n", - " boxes[0],\r\n", - " classes[0].astype(np.uint32) + label_id_offset,\r\n", - " scores[0],\r\n", - " category_index)\r\n", - "plt.figure(figsize = (30, 20))\r\n", - "plt.imshow(vis_image)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "DefXu4JXVxPD" - }, - "source": [ - "# Keypoints\r\n", - "\r\n", - "Unlike SSDs, CenterNet also supports COCO [Keypoint detection](https://cocodataset.org/#keypoints-2020). To be more specific, the 'keypoints' version of CenterNet shown here provides keypoints as a `[N, 17, 2]`-shaped tensor representing the (normalized) yx-coordinates of 17 COCO human keypoints.\r\n", - "\r\n", - "See the `detect()` function in the **Utilities for Inference** section to better understand the keypoints output." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "xu47DkrDV18O" - }, - "source": [ - "## Download Model from Detection Zoo\r\n", - "\r\n", - "**NOTE:** Not all CenterNet models from the [TF2 Detection Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md) work with TFLite, only the [MobileNet-based version](http://download.tensorflow.org/models/object_detection/tf2/20210210/centernet_mobilenetv2fpn_512x512_coco17_od.tar.gz) does." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "sd7f64WjWD7z" - }, - "source": [ - "# Get mobile-friendly CenterNet for Keypoint detection task.\r\n", - "# See TensorFlow 2 Detection Model Zoo for more details:\r\n", - "# https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md\r\n", - "\r\n", - "%%bash\r\n", - "wget http://download.tensorflow.org/models/object_detection/tf2/20210210/centernet_mobilenetv2fpn_512x512_coco17_kpts.tar.gz\r\n", - "tar -xf centernet_mobilenetv2fpn_512x512_coco17_kpts.tar.gz\r\n", - "rm centernet_mobilenetv2fpn_512x512_coco17_kpts.tar.gz*" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "NSFc-xSLX1ZC" - }, - "source": [ - "## Generate TensorFlow Lite Model\r\n", - "\r\n", - "As before, we leverage `export_tflite_graph_tf2.py` to generate a TFLite-friendly intermediate SavedModel. This will then be passed to the TFLite converter to generating the final model.\r\n", - "\r\n", - "Note that we need to include an additional `keypoint_label_map_path` parameter for exporting the keypoints outputs." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "8kEhwYynX-cD" - }, - "source": [ - "%%bash\r\n", - "# Export the intermediate SavedModel that outputs 10 detections & takes in an \r\n", - "# image of dim 320x320.\r\n", - "# Modify these parameters according to your needs.\r\n", - "\r\n", - "python models/research/object_detection/export_tflite_graph_tf2.py \\\r\n", - " --pipeline_config_path=centernet_mobilenetv2_fpn_kpts/pipeline.config \\\r\n", - " --trained_checkpoint_dir=centernet_mobilenetv2_fpn_kpts/checkpoint \\\r\n", - " --output_directory=centernet_mobilenetv2_fpn_kpts/tflite \\\r\n", - " --centernet_include_keypoints=true \\\r\n", - " --keypoint_label_map_path=centernet_mobilenetv2_fpn_kpts/label_map.txt \\\r\n", - " --max_detections=10 \\\r\n", - " --config_override=\" \\\r\n", - " model{ \\\r\n", - " center_net { \\\r\n", - " image_resizer { \\\r\n", - " fixed_shape_resizer { \\\r\n", - " height: 320 \\\r\n", - " width: 320 \\\r\n", - " } \\\r\n", - " } \\\r\n", - " } \\\r\n", - " }\"" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "TJtsyMlLY1DU" - }, - "source": [ - "# Generate TensorFlow Lite model using the converter.\r\n", - "\r\n", - "%%bash\r\n", - "tflite_convert --output_file=centernet_mobilenetv2_fpn_kpts/model.tflite \\\r\n", - " --saved_model_dir=centernet_mobilenetv2_fpn_kpts/tflite/saved_model" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "nJCxPBjYZSk6" - }, - "source": [ - "## TensorFlow Lite Inference\r\n", - "\r\n", - "Use a TensorFlow Lite Interpreter to detect people & their keypoints in the test image." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "F2GpD7r8ZUzx" - }, - "source": [ - "%matplotlib inline\r\n", - "\r\n", - "# Load the TFLite model and allocate tensors.\r\n", - "model_path = 'centernet_mobilenetv2_fpn_kpts/model.tflite'\r\n", - "image_path = 'coco/val2017/000000013729.jpg'\r\n", - "\r\n", - "# Initialize TensorFlow Lite Interpreter.\r\n", - "interpreter = tf.lite.Interpreter(model_path=model_path)\r\n", - "interpreter.allocate_tensors()\r\n", - "\r\n", - "# Keypoints are only relevant for people, so we only care about that\r\n", - "# category Id here.\r\n", - "category_index = {1: {'id': 1, 'name': 'person'}}\r\n", - "\r\n", - "label_id_offset = 1\r\n", - "\r\n", - "image = tf.io.read_file(image_path)\r\n", - "image = tf.compat.v1.image.decode_jpeg(image)\r\n", - "image = tf.expand_dims(image, axis=0)\r\n", - "image_numpy = image.numpy()\r\n", - "\r\n", - "input_tensor = tf.convert_to_tensor(image_numpy, dtype=tf.float32)\r\n", - "# Note that CenterNet doesn't require any pre-processing except resizing to\r\n", - "# input size that the TensorFlow Lite Interpreter was generated with.\r\n", - "input_tensor = tf.image.resize(input_tensor, (320, 320))\r\n", - "(boxes, classes, scores, num_detections, kpts, kpts_scores) = detect(\r\n", - " interpreter, input_tensor, include_keypoint=True)\r\n", - "\r\n", - "vis_image = plot_detections(\r\n", - " image_numpy[0],\r\n", - " boxes[0],\r\n", - " classes[0].astype(np.uint32) + label_id_offset,\r\n", - " scores[0],\r\n", - " category_index,\r\n", - " keypoints=kpts[0],\r\n", - " keypoint_scores=kpts_scores[0])\r\n", - "plt.figure(figsize = (30, 20))\r\n", - "plt.imshow(vis_image)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "59Y3q6AC9C7s" - }, - "source": [ - "# Running On Mobile\r\n", - "\r\n", - "As mentioned earlier, both the above models can be run on mobile phones with TensorFlow Lite. See our [**inference documentation**](https://www.tensorflow.org/lite/guide/inference) for general guidelines on platform-specific APIs & leveraging hardware acceleration. Both the object-detection & keypoint-detection versions of CenterNet are compatible with our [GPU delegate](https://www.tensorflow.org/lite/performance/gpu). *We are working on developing quantized versions of this model.*\r\n", - "\r\n", - "To leverage *object-detection* in your Android app, the simplest way is to use TFLite's [**ObjectDetector Task API**](https://www.tensorflow.org/lite/inference_with_metadata/task_library/object_detector). It is a high-level API that encapsulates complex but common image processing and post processing logic. Inference can be done in 5 lines of code. It is supported in Java for Android and C++ for native code. *We are working on building the Swift API for iOS, as well as the support for the keypoint-detection model.*\r\n", - "\r\n", - "To use the Task API, the model needs to be packed with [TFLite Metadata](https://www.tensorflow.org/lite/convert/metadata). This metadata helps the inference code perform the correct pre & post processing as required by the model. Use the following code to create the metadata." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "8T_qzv6lDN_a" - }, - "source": [ - "!pip install tflite_support_nightly" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "CTZhBmfWDQ3z" - }, - "source": [ - "from tflite_support.metadata_writers import object_detector\n", - "from tflite_support.metadata_writers import writer_utils\n", - "\n", - "ObjectDetectorWriter = object_detector.MetadataWriter\n", - "\n", - "_MODEL_PATH = \"centernet_mobilenetv2_fpn_od/model.tflite\"\n", - "_SAVE_TO_PATH = \"centernet_mobilenetv2_fpn_od/model_with_metadata.tflite\"\n", - "_LABEL_PATH = \"centernet_mobilenetv2_fpn_od/tflite_label_map.txt\"\n", - "\n", - "# We need to convert Detection API's labelmap into what the Task API needs:\n", - "# a txt file with one class name on each line from index 0 to N.\n", - "# The first '0' class indicates the background.\n", - "# This code assumes COCO detection which has 90 classes, you can write a label\n", - "# map file for your model if re-trained.\n", - "od_label_map_path = 'centernet_mobilenetv2_fpn_od/label_map.txt'\n", - "category_index = label_map_util.create_category_index_from_labelmap(\n", - " label_map_path)\n", - "f = open(_LABEL_PATH, 'w')\n", - "for class_id in range(1, 91):\n", - " if class_id not in category_index:\n", - " f.write('???\\n')\n", - " continue\n", - " name = category_index[class_id]['name']\n", - " f.write(name+'\\n')\n", - "f.close()\n", - "\n", - "writer = ObjectDetectorWriter.create_for_inference(\n", - " writer_utils.load_file(_MODEL_PATH), input_norm_mean=[0], \n", - " input_norm_std=[1], label_file_paths=[_LABEL_PATH])\n", - "writer_utils.save_file(writer.populate(), _SAVE_TO_PATH)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "b2tc7awzDUHr" - }, - "source": [ - "Visualize the metadata just created by the following code:" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "_SRqVdZNDYF1" - }, - "source": [ - "from tflite_support import metadata\n", - "\n", - "displayer = metadata.MetadataDisplayer.with_model_file(_SAVE_TO_PATH)\n", - "print(\"Metadata populated:\")\n", - "print(displayer.get_metadata_json())\n", - "print(\"=============================\")\n", - "print(\"Associated file(s) populated:\")\n", - "print(displayer.get_packed_associated_file_list())" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "SPUNsg9eDjWT" - }, - "source": [ - "See more information about *object-detection* models from our [public documentation](https://www.tensorflow.org/lite/examples/object_detection/overview). The [Object Detection example app](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection) is a good starting point for integrating that model into your Android and iOS app. You can find [examples](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android#switch-between-inference-solutions-task-library-vs-tflite-interpreter) of using both the TFLite Task Library and TFLite Interpreter API." - ] - } - ] -} diff --git a/research/object_detection/colab_tutorials/context_rcnn_tutorial.ipynb b/research/object_detection/colab_tutorials/context_rcnn_tutorial.ipynb deleted file mode 100644 index b735cfbcea0..00000000000 --- a/research/object_detection/colab_tutorials/context_rcnn_tutorial.ipynb +++ /dev/null @@ -1,1500 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "accelerator": "GPU", - "colab": { - "name": "context_rcnn_tutorial.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "jZc1kMel3sZP", - "colab_type": "text" - }, - "source": [ - "# Context R-CNN Demo\n", - "\n", - "
\n", - " \n", - " Run in Google Colab\n", - " \n", - "\n", - " \n", - " View source on GitHub\n", - "
\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XuHWvdag3_b9", - "colab_type": "text" - }, - "source": [ - " This notebook will walk you step by step through the process of using a pre-trained model to build up a contextual memory bank for a set of images, and then detect objects in those images+context using [Context R-CNN](https://arxiv.org/abs/1912.03538)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "u0e-OOtn4hQ8", - "colab_type": "text" - }, - "source": [ - "# Setup" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "w-UrhxBw4iLA", - "colab_type": "text" - }, - "source": [ - "Important: If you're running on a local machine, be sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md). This notebook includes only what's necessary to run in Colab." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "SAqMxS4V4lqS", - "colab_type": "text" - }, - "source": [ - "### Install" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "BPkovrxF4o8n", - "colab_type": "code", - "outputId": "e1b8debc-ab73-4b3e-9e44-c86446c7cda1", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 785 - } - }, - "source": [ - "!pip install -U --pre tensorflow==\"2.*\"\n", - "!pip install tf_slim" - ], - "execution_count": 1, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Requirement already up-to-date: tensorflow==2.* in /usr/local/lib/python3.6/dist-packages (2.2.0)\n", - "Requirement already satisfied, skipping upgrade: scipy==1.4.1; python_version >= \"3\" in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.4.1)\n", - "Requirement already satisfied, skipping upgrade: protobuf>=3.8.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (3.10.0)\n", - "Requirement already satisfied, skipping upgrade: h5py<2.11.0,>=2.10.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (2.10.0)\n", - "Requirement already satisfied, skipping upgrade: opt-einsum>=2.3.2 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (3.2.1)\n", - "Requirement already satisfied, skipping upgrade: numpy<2.0,>=1.16.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.18.5)\n", - "Requirement already satisfied, skipping upgrade: wheel>=0.26; python_version >= \"3\" in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (0.34.2)\n", - "Requirement already satisfied, skipping upgrade: absl-py>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (0.9.0)\n", - "Requirement already satisfied, skipping upgrade: tensorflow-estimator<2.3.0,>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (2.2.0)\n", - "Requirement already satisfied, skipping upgrade: google-pasta>=0.1.8 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (0.2.0)\n", - "Requirement already satisfied, skipping upgrade: grpcio>=1.8.6 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.29.0)\n", - "Requirement already satisfied, skipping upgrade: tensorboard<2.3.0,>=2.2.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (2.2.2)\n", - "Requirement already satisfied, skipping upgrade: gast==0.3.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (0.3.3)\n", - "Requirement already satisfied, skipping upgrade: astunparse==1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.6.3)\n", - "Requirement already satisfied, skipping upgrade: keras-preprocessing>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.1.2)\n", - "Requirement already satisfied, skipping upgrade: termcolor>=1.1.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.1.0)\n", - "Requirement already satisfied, skipping upgrade: six>=1.12.0 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.12.0)\n", - "Requirement already satisfied, skipping upgrade: wrapt>=1.11.1 in /usr/local/lib/python3.6/dist-packages (from tensorflow==2.*) (1.12.1)\n", - "Requirement already satisfied, skipping upgrade: setuptools in /usr/local/lib/python3.6/dist-packages (from protobuf>=3.8.0->tensorflow==2.*) (47.1.1)\n", - "Requirement already satisfied, skipping upgrade: google-auth<2,>=1.6.3 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (1.7.2)\n", - "Requirement already satisfied, skipping upgrade: werkzeug>=0.11.15 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (1.0.1)\n", - "Requirement already satisfied, skipping upgrade: requests<3,>=2.21.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (2.23.0)\n", - "Requirement already satisfied, skipping upgrade: google-auth-oauthlib<0.5,>=0.4.1 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (0.4.1)\n", - "Requirement already satisfied, skipping upgrade: markdown>=2.6.8 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (3.2.2)\n", - "Requirement already satisfied, skipping upgrade: tensorboard-plugin-wit>=1.6.0 in /usr/local/lib/python3.6/dist-packages (from tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (1.6.0.post3)\n", - "Requirement already satisfied, skipping upgrade: cachetools<3.2,>=2.0.0 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (3.1.1)\n", - "Requirement already satisfied, skipping upgrade: rsa<4.1,>=3.1.4 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (4.0)\n", - "Requirement already satisfied, skipping upgrade: pyasn1-modules>=0.2.1 in /usr/local/lib/python3.6/dist-packages (from google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (0.2.8)\n", - "Requirement already satisfied, skipping upgrade: idna<3,>=2.5 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (2.9)\n", - "Requirement already satisfied, skipping upgrade: chardet<4,>=3.0.2 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (3.0.4)\n", - "Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (2020.4.5.1)\n", - "Requirement already satisfied, skipping upgrade: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.6/dist-packages (from requests<3,>=2.21.0->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (1.24.3)\n", - "Requirement already satisfied, skipping upgrade: requests-oauthlib>=0.7.0 in /usr/local/lib/python3.6/dist-packages (from google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (1.3.0)\n", - "Requirement already satisfied, skipping upgrade: importlib-metadata; python_version < \"3.8\" in /usr/local/lib/python3.6/dist-packages (from markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (1.6.0)\n", - "Requirement already satisfied, skipping upgrade: pyasn1>=0.1.3 in /usr/local/lib/python3.6/dist-packages (from rsa<4.1,>=3.1.4->google-auth<2,>=1.6.3->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (0.4.8)\n", - "Requirement already satisfied, skipping upgrade: oauthlib>=3.0.0 in /usr/local/lib/python3.6/dist-packages (from requests-oauthlib>=0.7.0->google-auth-oauthlib<0.5,>=0.4.1->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (3.1.0)\n", - "Requirement already satisfied, skipping upgrade: zipp>=0.5 in /usr/local/lib/python3.6/dist-packages (from importlib-metadata; python_version < \"3.8\"->markdown>=2.6.8->tensorboard<2.3.0,>=2.2.0->tensorflow==2.*) (3.1.0)\n", - "Collecting tf_slim\n", - "\u001b[?25l Downloading https://files.pythonhosted.org/packages/02/97/b0f4a64df018ca018cc035d44f2ef08f91e2e8aa67271f6f19633a015ff7/tf_slim-1.1.0-py2.py3-none-any.whl (352kB)\n", - "\u001b[K |████████████████████████████████| 358kB 2.8MB/s \n", - "\u001b[?25hRequirement already satisfied: absl-py>=0.2.2 in /usr/local/lib/python3.6/dist-packages (from tf_slim) (0.9.0)\n", - "Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from absl-py>=0.2.2->tf_slim) (1.12.0)\n", - "Installing collected packages: tf-slim\n", - "Successfully installed tf-slim-1.1.0\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "zpKF8a2x4tec", - "colab_type": "text" - }, - "source": [ - "Make sure you have `pycocotools` installed" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "owcrp0AW4uCg", - "colab_type": "code", - "outputId": "001148a8-b0a8-43a1-f6df-225d86d90b8f", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "!pip install pycocotools" - ], - "execution_count": 2, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Requirement already satisfied: pycocotools in /usr/local/lib/python3.6/dist-packages (2.0.0)\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "wHFSRVaO4wuq", - "colab_type": "text" - }, - "source": [ - "Get `tensorflow/models` or `cd` to parent directory of the repository." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "E0ZuGKoi4wTn", - "colab_type": "code", - "outputId": "2b5d93cb-3548-4347-9b76-ce12bea44a56", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 136 - } - }, - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ], - "execution_count": 3, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Cloning into 'models'...\n", - "remote: Enumerating objects: 2694, done.\u001b[K\n", - "remote: Counting objects: 100% (2694/2694), done.\u001b[K\n", - "remote: Compressing objects: 100% (2370/2370), done.\u001b[K\n", - "remote: Total 2694 (delta 520), reused 1332 (delta 290), pack-reused 0\u001b[K\n", - "Receiving objects: 100% (2694/2694), 34.10 MiB | 29.32 MiB/s, done.\n", - "Resolving deltas: 100% (520/520), done.\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "GkqRm-WY47MR", - "colab_type": "text" - }, - "source": [ - "Compile protobufs and install the object_detection package" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "62Dn1_YU45O2", - "colab_type": "code", - "outputId": "439166dd-6202-4ff9-897d-100a35ae5af5", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 54 - } - }, - "source": [ - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=." - ], - "execution_count": 4, - "outputs": [ - { - "output_type": "stream", - "text": [ - "object_detection/protos/input_reader.proto: warning: Import object_detection/protos/image_resizer.proto but not used.\n" - ], - "name": "stderr" - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "83kNiD-24-ZB", - "colab_type": "code", - "outputId": "aa148939-7dcc-4fbd-ea48-41236523712c", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 343 - } - }, - "source": [ - "%%bash \n", - "cd models/research\n", - "pip install ." - ], - "execution_count": 5, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Processing /content/models/research\n", - "Requirement already satisfied: Pillow>=1.0 in /usr/local/lib/python3.6/dist-packages (from object-detection==0.1) (7.0.0)\n", - "Requirement already satisfied: Matplotlib>=2.1 in /usr/local/lib/python3.6/dist-packages (from object-detection==0.1) (3.2.1)\n", - "Requirement already satisfied: Cython>=0.28.1 in /usr/local/lib/python3.6/dist-packages (from object-detection==0.1) (0.29.19)\n", - "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.6/dist-packages (from Matplotlib>=2.1->object-detection==0.1) (0.10.0)\n", - "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.6/dist-packages (from Matplotlib>=2.1->object-detection==0.1) (2.4.7)\n", - "Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.6/dist-packages (from Matplotlib>=2.1->object-detection==0.1) (1.18.5)\n", - "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.6/dist-packages (from Matplotlib>=2.1->object-detection==0.1) (2.8.1)\n", - "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.6/dist-packages (from Matplotlib>=2.1->object-detection==0.1) (1.2.0)\n", - "Requirement already satisfied: six in /usr/local/lib/python3.6/dist-packages (from cycler>=0.10->Matplotlib>=2.1->object-detection==0.1) (1.12.0)\n", - "Building wheels for collected packages: object-detection\n", - " Building wheel for object-detection (setup.py): started\n", - " Building wheel for object-detection (setup.py): finished with status 'done'\n", - " Created wheel for object-detection: filename=object_detection-0.1-cp36-none-any.whl size=1141324 sha256=1dff68de415a4ccc3af0e20b8f409a73d147d79720a713dcdc30f9bc8d4ab3a2\n", - " Stored in directory: /tmp/pip-ephem-wheel-cache-rlyj8yrw/wheels/94/49/4b/39b051683087a22ef7e80ec52152a27249d1a644ccf4e442ea\n", - "Successfully built object-detection\n", - "Installing collected packages: object-detection\n", - "Successfully installed object-detection-0.1\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "LBdjK2G5ywuc" - }, - "source": [ - "### Imports" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "hV4P5gyTWKMI", - "colab": {} - }, - "source": [ - "import numpy as np\n", - "import os\n", - "import six\n", - "import six.moves.urllib as urllib\n", - "import sys\n", - "import tarfile\n", - "import tensorflow as tf\n", - "import zipfile\n", - "import pathlib\n", - "import json\n", - "import datetime\n", - "import matplotlib.pyplot as plt\n", - "\n", - "from collections import defaultdict\n", - "from io import StringIO\n", - "from matplotlib import pyplot as plt\n", - "from PIL import Image\n", - "from IPython.display import display" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "r5FNuiRPWKMN" - }, - "source": [ - "Import the object detection module." - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "4-IMl4b6BdGO", - "colab": {} - }, - "source": [ - "from object_detection.utils import ops as utils_ops\n", - "from object_detection.utils import label_map_util\n", - "from object_detection.utils import visualization_utils as vis_utils" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "RYPCiag2iz_q" - }, - "source": [ - "Patches:" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "mF-YlMl8c_bM", - "colab": {} - }, - "source": [ - "# patch tf1 into `utils.ops`\n", - "utils_ops.tf = tf.compat.v1\n", - "\n", - "# Patch the location of gfile\n", - "tf.gfile = tf.io.gfile" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "cfn_tRFOWKMO" - }, - "source": [ - "# Model preparation " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "7ai8pLZZWKMS" - }, - "source": [ - "## Loader" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "zm8xp-0eoItE", - "colab": {} - }, - "source": [ - "def load_model(model_name):\n", - " base_url = 'http://download.tensorflow.org/models/object_detection/'\n", - " model_file = model_name + '.tar.gz'\n", - " model_dir = tf.keras.utils.get_file(\n", - " fname=model_name,\n", - " origin=base_url + model_file,\n", - " untar=True)\n", - "\n", - " model_dir = pathlib.Path(model_dir)/\"saved_model\"\n", - " model = tf.saved_model.load(str(model_dir))\n", - " model = model.signatures['serving_default']\n", - "\n", - " return model" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "_1MVVTcLWKMW" - }, - "source": [ - "## Loading label map\n", - "Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `zebra`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "hDbpHkiWWKMX", - "colab": {} - }, - "source": [ - "# List of the strings that is used to add correct label for each box.\n", - "PATH_TO_LABELS = 'models/research/object_detection/data/snapshot_serengeti_label_map.pbtxt'\n", - "category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=False)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "oVU3U_J6IJVb" - }, - "source": [ - "We will test on a context group of images from one month at one camera from the Snapshot Serengeti val split defined on [LILA.science](http://lila.science/datasets/snapshot-serengeti), which was not seen during model training:\n", - "\n" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "jG-zn5ykWKMd", - "outputId": "c7bbbb2f-0f6e-4380-fd92-c88c088bd766", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 85 - } - }, - "source": [ - "# If you want to test the code with your images, just add path to the images to\n", - "# the TEST_IMAGE_PATHS.\n", - "PATH_TO_TEST_IMAGES_DIR = pathlib.Path('models/research/object_detection/test_images/snapshot_serengeti')\n", - "TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob(\"*.jpeg\")))\n", - "TEST_IMAGE_PATHS" - ], - "execution_count": 11, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "[PosixPath('models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0038.jpeg'),\n", - " PosixPath('models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0039.jpeg'),\n", - " PosixPath('models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0040.jpeg'),\n", - " PosixPath('models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0041.jpeg')]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 11 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "oBcQzptnQ-x6", - "colab_type": "text" - }, - "source": [ - "Load the metadata for each image" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "ZLLINOHcQ-An", - "colab_type": "code", - "colab": {} - }, - "source": [ - "test_data_json = 'models/research/object_detection/test_images/snapshot_serengeti/context_rcnn_demo_metadata.json'\n", - "with open(test_data_json, 'r') as f:\n", - " test_metadata = json.load(f)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "BgGTPHhkOAel", - "colab_type": "code", - "outputId": "1421a32a-c208-498f-931f-1bfeb25d6488", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 105 - } - }, - "source": [ - "image_id_to_datetime = {im['id']:im['date_captured'] for im in test_metadata['images']}\n", - "image_path_to_id = {im['file_name']: im['id'] \n", - " for im in test_metadata['images']}\n", - "image_path_to_id" - ], - "execution_count": 13, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0038.jpeg': 'S1/E03/E03_R3/S1_E03_R3_PICT0038',\n", - " 'models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0039.jpeg': 'S1/E03/E03_R3/S1_E03_R3_PICT0039',\n", - " 'models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0040.jpeg': 'S1/E03/E03_R3/S1_E03_R3_PICT0040',\n", - " 'models/research/object_detection/test_images/snapshot_serengeti/S1_E03_R3_PICT0041.jpeg': 'S1/E03/E03_R3/S1_E03_R3_PICT0041'}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 13 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "H0_1AGhrWKMc" - }, - "source": [ - "# Generate Context Features for each image" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "kt3_pPQOj7ii", - "colab_type": "code", - "outputId": "fc72e978-f576-43f4-bcf1-3eb49fef5726", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 88 - } - }, - "source": [ - "faster_rcnn_model_name = 'faster_rcnn_resnet101_snapshot_serengeti_2020_06_10'\n", - "faster_rcnn_model = load_model(faster_rcnn_model_name)" - ], - "execution_count": 14, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Downloading data from http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_snapshot_serengeti_2020_06_10.tar.gz\n", - "588832768/588829839 [==============================] - 3s 0us/step\n", - "INFO:tensorflow:Saver not created because there are no variables in the graph to restore\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "k6Clkv_mBo_U", - "colab_type": "text" - }, - "source": [ - "Check the model's input signature, it expects a batch of 3-color images of type uint8." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "H1qNlFESBsTR", - "colab_type": "code", - "outputId": "9b8b84e0-d7a8-4ec9-d6e0-22d574cb6209", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - } - }, - "source": [ - "faster_rcnn_model.inputs" - ], - "execution_count": 15, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "[]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 15 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "eYS8KpRCBtBH", - "colab_type": "text" - }, - "source": [ - "And it returns several outputs. Note this model has been exported with additional output 'detection_features' which will be used to build the contextual memory bank." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "5M-1yxgfkmQl", - "colab_type": "code", - "outputId": "1da98c3b-79c5-4d19-d64c-3e9dbadc97c0", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 153 - } - }, - "source": [ - "faster_rcnn_model.output_dtypes" - ], - "execution_count": 16, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'detection_boxes': tf.float32,\n", - " 'detection_classes': tf.float32,\n", - " 'detection_features': tf.float32,\n", - " 'detection_multiclass_scores': tf.float32,\n", - " 'detection_scores': tf.float32,\n", - " 'num_detections': tf.float32,\n", - " 'raw_detection_boxes': tf.float32,\n", - " 'raw_detection_scores': tf.float32}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 16 - } - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "zVjNFFNIDCst", - "colab_type": "code", - "outputId": "edb46db0-05fb-4952-bc88-db09d7811b01", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 153 - } - }, - "source": [ - "faster_rcnn_model.output_shapes" - ], - "execution_count": 17, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'detection_boxes': TensorShape([None, 300, 4]),\n", - " 'detection_classes': TensorShape([None, 300]),\n", - " 'detection_features': TensorShape([None, None, None, None, None]),\n", - " 'detection_multiclass_scores': TensorShape([None, 300, 49]),\n", - " 'detection_scores': TensorShape([None, 300]),\n", - " 'num_detections': TensorShape([None]),\n", - " 'raw_detection_boxes': TensorShape([None, 300, 4]),\n", - " 'raw_detection_scores': TensorShape([None, 300, 49])}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 17 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "JP5qZ7sXJpwG" - }, - "source": [ - "Add a wrapper function to call the model, and cleanup the outputs:" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "ajmR_exWyN76", - "colab": {} - }, - "source": [ - "def run_inference_for_single_image(model, image):\n", - " '''Run single image through tensorflow object detection saved_model.\n", - "\n", - " This function runs a saved_model on a (single) provided image and returns\n", - " inference results in numpy arrays.\n", - "\n", - " Args:\n", - " model: tensorflow saved_model. This model can be obtained using \n", - " export_inference_graph.py.\n", - " image: uint8 numpy array with shape (img_height, img_width, 3)\n", - "\n", - " Returns:\n", - " output_dict: a dictionary holding the following entries:\n", - " `num_detections`: an integer\n", - " `detection_boxes`: a numpy (float32) array of shape [N, 4]\n", - " `detection_classes`: a numpy (uint8) array of shape [N]\n", - " `detection_scores`: a numpy (float32) array of shape [N]\n", - " `detection_features`: a numpy (float32) array of shape [N, 7, 7, 2048]\n", - " '''\n", - " image = np.asarray(image)\n", - " # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.\n", - " input_tensor = tf.convert_to_tensor(image)\n", - " # The model expects a batch of images, so add an axis with `tf.newaxis`.\n", - " input_tensor = input_tensor[tf.newaxis,...]\n", - "\n", - " # Run inference\n", - " output_dict = model(input_tensor)\n", - " # All outputs are batches tensors.\n", - " # Convert to numpy arrays, and take index [0] to remove the batch dimension.\n", - " # We're only interested in the first num_detections.\n", - " num_dets = output_dict.pop('num_detections')\n", - " num_detections = int(num_dets)\n", - " for key,value in output_dict.items():\n", - " output_dict[key] = value[0, :num_detections].numpy() \n", - " output_dict['num_detections'] = num_detections\n", - "\n", - " # detection_classes should be ints.\n", - " output_dict['detection_classes'] = output_dict['detection_classes'].astype(\n", - " np.int64)\n", - " return output_dict" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "un5SXxIxMaaV", - "colab_type": "text" - }, - "source": [ - "Functions for embedding context features" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "qvtvAZFDMoTM", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def embed_date_captured(date_captured):\n", - " \"\"\"Encodes the datetime of the image.\n", - "\n", - " Takes a datetime object and encodes it into a normalized embedding of shape \n", - " [5], using hard-coded normalization factors for year, month, day, hour,\n", - " minute.\n", - "\n", - " Args:\n", - " date_captured: A datetime object.\n", - "\n", - " Returns:\n", - " A numpy float32 embedding of shape [5].\n", - " \"\"\"\n", - " embedded_date_captured = []\n", - " month_max = 12.0\n", - " day_max = 31.0\n", - " hour_max = 24.0\n", - " minute_max = 60.0\n", - " min_year = 1990.0\n", - " max_year = 2030.0\n", - "\n", - " year = (date_captured.year-min_year)/float(max_year-min_year)\n", - " embedded_date_captured.append(year)\n", - "\n", - " month = (date_captured.month-1)/month_max\n", - " embedded_date_captured.append(month)\n", - "\n", - " day = (date_captured.day-1)/day_max\n", - " embedded_date_captured.append(day)\n", - "\n", - " hour = date_captured.hour/hour_max\n", - " embedded_date_captured.append(hour)\n", - "\n", - " minute = date_captured.minute/minute_max\n", - " embedded_date_captured.append(minute)\n", - "\n", - " return np.asarray(embedded_date_captured)" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "xN8k5daOOA7b", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def embed_position_and_size(box):\n", - " \"\"\"Encodes the bounding box of the object of interest.\n", - "\n", - " Takes a bounding box and encodes it into a normalized embedding of shape \n", - " [4] - the center point (x,y) and width and height of the box.\n", - "\n", - " Args:\n", - " box: A bounding box, formatted as [ymin, xmin, ymax, xmax].\n", - "\n", - " Returns:\n", - " A numpy float32 embedding of shape [4].\n", - " \"\"\"\n", - " ymin = box[0]\n", - " xmin = box[1]\n", - " ymax = box[2]\n", - " xmax = box[3]\n", - " w = xmax - xmin\n", - " h = ymax - ymin\n", - " x = xmin + w / 2.0\n", - " y = ymin + h / 2.0\n", - " return np.asarray([x, y, w, h])" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "lJe2qy8HPc6Z", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def get_context_feature_embedding(date_captured, detection_boxes,\n", - " detection_features, detection_scores):\n", - " \"\"\"Extracts representative feature embedding for a given input image.\n", - "\n", - " Takes outputs of a detection model and focuses on the highest-confidence\n", - " detected object. Starts with detection_features and uses average pooling to\n", - " remove the spatial dimensions, then appends an embedding of the box position\n", - " and size, and an embedding of the date and time the image was captured,\n", - " returning a one-dimensional representation of the object.\n", - "\n", - " Args:\n", - " date_captured: A datetime string of format '%Y-%m-%d %H:%M:%S'.\n", - " detection_features: A numpy (float32) array of shape [N, 7, 7, 2048].\n", - " detection_boxes: A numpy (float32) array of shape [N, 4].\n", - " detection_scores: A numpy (float32) array of shape [N].\n", - "\n", - " Returns:\n", - " A numpy float32 embedding of shape [2057].\n", - " \"\"\"\n", - " date_captured = datetime.datetime.strptime(date_captured,'%Y-%m-%d %H:%M:%S')\n", - " temporal_embedding = embed_date_captured(date_captured)\n", - " embedding = detection_features[0]\n", - " pooled_embedding = np.mean(np.mean(embedding, axis=1), axis=0)\n", - " box = detection_boxes[0]\n", - " position_embedding = embed_position_and_size(box)\n", - " bb_embedding = np.concatenate((pooled_embedding, position_embedding))\n", - " embedding = np.expand_dims(np.concatenate((bb_embedding,temporal_embedding)),\n", - " axis=0)\n", - " score = detection_scores[0]\n", - " return embedding, score" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "z1wq0LVyMRR_" - }, - "source": [ - "Run it on each test image and use the output detection features and metadata to build up a context feature bank:" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "DWh_1zz6aqxs", - "colab": {} - }, - "source": [ - "def run_inference(model, image_path, date_captured, resize_image=True):\n", - " \"\"\"Runs inference over a single input image and extracts contextual features.\n", - "\n", - " Args:\n", - " model: A tensorflow saved_model object.\n", - " image_path: Absolute path to the input image.\n", - " date_captured: A datetime string of format '%Y-%m-%d %H:%M:%S'.\n", - " resize_image: Whether to resize the input image before running inference.\n", - "\n", - " Returns:\n", - " context_feature: A numpy float32 array of shape [2057].\n", - " score: A numpy float32 object score for the embedded object.\n", - " output_dict: The saved_model output dictionary for the image.\n", - " \"\"\"\n", - " with open(image_path,'rb') as f:\n", - " image = Image.open(f)\n", - " if resize_image:\n", - " image.thumbnail((640,640),Image.ANTIALIAS)\n", - " image_np = np.array(image)\n", - "\n", - " # Actual detection.\n", - " output_dict = run_inference_for_single_image(model, image_np)\n", - "\n", - " context_feature, score = get_context_feature_embedding(\n", - " date_captured, output_dict['detection_boxes'],\n", - " output_dict['detection_features'], output_dict['detection_scores'])\n", - " return context_feature, score, output_dict" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "3a5wMHN8WKMh", - "colab": {} - }, - "source": [ - "context_features = []\n", - "scores = []\n", - "faster_rcnn_results = {}\n", - "for image_path in TEST_IMAGE_PATHS:\n", - " image_id = image_path_to_id[str(image_path)]\n", - " date_captured = image_id_to_datetime[image_id]\n", - " context_feature, score, results = run_inference(\n", - " faster_rcnn_model, image_path, date_captured)\n", - " faster_rcnn_results[image_id] = results\n", - " context_features.append(context_feature)\n", - " scores.append(score)\n", - "\n", - "# Concatenate all extracted context embeddings into a contextual memory bank.\n", - "context_features_matrix = np.concatenate(context_features, axis=0)\n" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "DsspMPX3Cssg" - }, - "source": [ - "## Run Detection With Context" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "f7aOtOlebK7h" - }, - "source": [ - "Load a context r-cnn object detection model:" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "1XNT0wxybKR6", - "outputId": "cc5b0677-cf16-46c2-9ae5-32681725f856", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 88 - } - }, - "source": [ - "context_rcnn_model_name = 'context_rcnn_resnet101_snapshot_serengeti_2020_06_10'\n", - "context_rcnn_model = load_model(context_rcnn_model_name)\n" - ], - "execution_count": 24, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Downloading data from http://download.tensorflow.org/models/object_detection/context_rcnn_resnet101_snapshot_serengeti_2020_06_10.tar.gz\n", - "724664320/724658931 [==============================] - 3s 0us/step\n", - "INFO:tensorflow:Saver not created because there are no variables in the graph to restore\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "G6IGGtGqBH6y", - "colab_type": "text" - }, - "source": [ - "We need to define the expected context padding size for the\n", - "model, this must match the definition in the model config (max_num_context_features)." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "4oh9XNLBjkTL", - "colab_type": "code", - "colab": {} - }, - "source": [ - "context_padding_size = 2000" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "yN1AYfAEJIGp" - }, - "source": [ - "Check the model's input signature, it expects a batch of 3-color images of type uint8, plus context_features padded to the maximum context feature size for this model (2000) and valid_context_size to represent the non-padded context features: " - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "CK4cnry6wsHY", - "outputId": "d77af014-769f-4e20-b4ac-bfdd40502128", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - } - }, - "source": [ - "context_rcnn_model.inputs" - ], - "execution_count": 26, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "[,\n", - " ,\n", - " ]" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 26 - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "Q8u3BjpMJXZF" - }, - "source": [ - "And returns several outputs:" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "oLSZpfaYwuSk", - "outputId": "63a3903f-529b-41f9-b742-9b81c4c5e096", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 136 - } - }, - "source": [ - "context_rcnn_model.output_dtypes" - ], - "execution_count": 27, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'detection_boxes': tf.float32,\n", - " 'detection_classes': tf.float32,\n", - " 'detection_multiclass_scores': tf.float32,\n", - " 'detection_scores': tf.float32,\n", - " 'num_detections': tf.float32,\n", - " 'raw_detection_boxes': tf.float32,\n", - " 'raw_detection_scores': tf.float32}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 27 - } - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "FZyKUJeuxvpT", - "outputId": "d2feeaba-2bb2-4779-a96a-94a8a0aff362", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 136 - } - }, - "source": [ - "context_rcnn_model.output_shapes" - ], - "execution_count": 28, - "outputs": [ - { - "output_type": "execute_result", - "data": { - "text/plain": [ - "{'detection_boxes': TensorShape([1, 300, 4]),\n", - " 'detection_classes': TensorShape([1, 300]),\n", - " 'detection_multiclass_scores': TensorShape([1, 300, 49]),\n", - " 'detection_scores': TensorShape([1, 300]),\n", - " 'num_detections': TensorShape([1]),\n", - " 'raw_detection_boxes': TensorShape([1, 300, 4]),\n", - " 'raw_detection_scores': TensorShape([1, 300, 49])}" - ] - }, - "metadata": { - "tags": [] - }, - "execution_count": 28 - } - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "CzkVv_n2MxKC", - "colab": {} - }, - "source": [ - "def run_context_rcnn_inference_for_single_image(\n", - " model, image, context_features, context_padding_size):\n", - " '''Run single image through a Context R-CNN saved_model.\n", - "\n", - " This function runs a saved_model on a (single) provided image and provided \n", - " contextual features and returns inference results in numpy arrays.\n", - "\n", - " Args:\n", - " model: tensorflow Context R-CNN saved_model. This model can be obtained\n", - " using export_inference_graph.py and setting side_input fields. \n", - " Example export call - \n", - " python export_inference_graph.py \\\n", - " --input_type image_tensor \\\n", - " --pipeline_config_path /path/to/context_rcnn_model.config \\\n", - " --trained_checkpoint_prefix /path/to/context_rcnn_model.ckpt \\\n", - " --output_directory /path/to/output_dir \\\n", - " --use_side_inputs True \\\n", - " --side_input_shapes 1,2000,2057/1 \\\n", - " --side_input_names context_features,valid_context_size \\\n", - " --side_input_types float,int \\\n", - " --input_shape 1,-1,-1,3\n", - "\n", - " image: uint8 numpy array with shape (img_height, img_width, 3)\n", - " context_features: A numpy float32 contextual memory bank of shape \n", - " [num_context_examples, 2057]\n", - " context_padding_size: The amount of expected padding in the contextual\n", - " memory bank, defined in the Context R-CNN config as \n", - " max_num_context_features.\n", - "\n", - " Returns:\n", - " output_dict: a dictionary holding the following entries:\n", - " `num_detections`: an integer\n", - " `detection_boxes`: a numpy (float32) array of shape [N, 4]\n", - " `detection_classes`: a numpy (uint8) array of shape [N]\n", - " `detection_scores`: a numpy (float32) array of shape [N]\n", - " '''\n", - " image = np.asarray(image)\n", - " # The input image needs to be a tensor, convert it using \n", - " # `tf.convert_to_tensor`.\n", - " image_tensor = tf.convert_to_tensor(\n", - " image, name='image_tensor')[tf.newaxis,...]\n", - "\n", - " context_features = np.asarray(context_features)\n", - " valid_context_size = context_features.shape[0]\n", - " valid_context_size_tensor = tf.convert_to_tensor(\n", - " valid_context_size, name='valid_context_size')[tf.newaxis,...]\n", - " padded_context_features = np.pad(\n", - " context_features,\n", - " ((0,context_padding_size-valid_context_size),(0,0)), mode='constant')\n", - " padded_context_features_tensor = tf.convert_to_tensor(\n", - " padded_context_features,\n", - " name='context_features',\n", - " dtype=tf.float32)[tf.newaxis,...]\n", - "\n", - " # Run inference\n", - " output_dict = model(\n", - " inputs=image_tensor,\n", - " context_features=padded_context_features_tensor,\n", - " valid_context_size=valid_context_size_tensor)\n", - " # All outputs are batches tensors.\n", - " # Convert to numpy arrays, and take index [0] to remove the batch dimension.\n", - " # We're only interested in the first num_detections.\n", - " num_dets = output_dict.pop('num_detections')\n", - " num_detections = int(num_dets)\n", - " for key,value in output_dict.items():\n", - " output_dict[key] = value[0, :num_detections].numpy() \n", - " output_dict['num_detections'] = num_detections\n", - "\n", - " # detection_classes should be ints.\n", - " output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)\n", - " return output_dict" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "0FqVkR3Agc6U", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def show_context_rcnn_inference(\n", - " model, image_path, context_features, faster_rcnn_output_dict,\n", - " context_padding_size, resize_image=True):\n", - " \"\"\"Runs inference over a single input image and visualizes Faster R-CNN vs. \n", - " Context R-CNN results.\n", - "\n", - " Args:\n", - " model: A tensorflow saved_model object.\n", - " image_path: Absolute path to the input image.\n", - " context_features: A numpy float32 contextual memory bank of shape \n", - " [num_context_examples, 2057]\n", - " faster_rcnn_output_dict: The output_dict corresponding to this input image\n", - " from the single-frame Faster R-CNN model, which was previously used to\n", - " build the memory bank.\n", - " context_padding_size: The amount of expected padding in the contextual\n", - " memory bank, defined in the Context R-CNN config as \n", - " max_num_context_features.\n", - " resize_image: Whether to resize the input image before running inference.\n", - "\n", - " Returns:\n", - " context_rcnn_image_np: Numpy image array showing Context R-CNN Results.\n", - " faster_rcnn_image_np: Numpy image array showing Faster R-CNN Results.\n", - " \"\"\"\n", - "\n", - " # the array based representation of the image will be used later in order to prepare the\n", - " # result image with boxes and labels on it.\n", - " with open(image_path,'rb') as f:\n", - " image = Image.open(f)\n", - " if resize_image:\n", - " image.thumbnail((640,640),Image.ANTIALIAS)\n", - " image_np = np.array(image)\n", - " image.thumbnail((400,400),Image.ANTIALIAS)\n", - " context_rcnn_image_np = np.array(image)\n", - " \n", - " faster_rcnn_image_np = np.copy(context_rcnn_image_np)\n", - "\n", - " # Actual detection.\n", - " output_dict = run_context_rcnn_inference_for_single_image(\n", - " model, image_np, context_features, context_padding_size)\n", - "\n", - " # Visualization of the results of a context_rcnn detection.\n", - " vis_utils.visualize_boxes_and_labels_on_image_array(\n", - " context_rcnn_image_np,\n", - " output_dict['detection_boxes'],\n", - " output_dict['detection_classes'],\n", - " output_dict['detection_scores'],\n", - " category_index,\n", - " use_normalized_coordinates=True,\n", - " line_thickness=2)\n", - " \n", - " # Visualization of the results of a faster_rcnn detection.\n", - " vis_utils.visualize_boxes_and_labels_on_image_array(\n", - " faster_rcnn_image_np,\n", - " faster_rcnn_output_dict['detection_boxes'],\n", - " faster_rcnn_output_dict['detection_classes'],\n", - " faster_rcnn_output_dict['detection_scores'],\n", - " category_index,\n", - " use_normalized_coordinates=True,\n", - " line_thickness=2)\n", - " return context_rcnn_image_np, faster_rcnn_image_np" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3cYa2B8uAYx0", - "colab_type": "text" - }, - "source": [ - "Define Matplotlib parameters for pretty visualizations" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "9F8okR1uAQ0T", - "colab_type": "code", - "colab": {} - }, - "source": [ - "%matplotlib inline\n", - "plt.rcParams['axes.grid'] = False\n", - "plt.rcParams['xtick.labelsize'] = False\n", - "plt.rcParams['ytick.labelsize'] = False\n", - "plt.rcParams['xtick.top'] = False\n", - "plt.rcParams['xtick.bottom'] = False\n", - "plt.rcParams['ytick.left'] = False\n", - "plt.rcParams['ytick.right'] = False\n", - "plt.rcParams['figure.figsize'] = [15,10]" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "YGj7nXXQAaQ7", - "colab_type": "text" - }, - "source": [ - "Run Context R-CNN inference and compare results to Faster R-CNN" - ] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "vQ2Sj2VIOZLA", - "outputId": "1c043894-09e5-4c9f-a99d-ae21d6e72d0c", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 - } - }, - "source": [ - "for image_path in TEST_IMAGE_PATHS:\n", - " image_id = image_path_to_id[str(image_path)]\n", - " faster_rcnn_output_dict = faster_rcnn_results[image_id]\n", - " context_rcnn_image, faster_rcnn_image = show_context_rcnn_inference(\n", - " context_rcnn_model, image_path, context_features_matrix,\n", - " faster_rcnn_output_dict, context_padding_size)\n", - " plt.subplot(1,2,1)\n", - " plt.imshow(faster_rcnn_image)\n", - " plt.title('Faster R-CNN')\n", - " plt.subplot(1,2,2)\n", - " plt.imshow(context_rcnn_image)\n", - " plt.title('Context R-CNN')\n", - " plt.show()" - ], - "execution_count": 32, - "outputs": [ - { - "output_type": "display_data", - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAFDCAYAAAAu+g+jAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nOx9d5xsWVnt2tXVfe8dYJhAGEYFRAxk8JFBER9ZUBAFFRQkCKiAJAcQRMkgUQUlKAxZFCQHA2EMAyoITwUe8BjCEGeGGSbduV3Vtd8fp77qVavWPtV9b0+4t7/v9+tfV52zw5fXPt/Z51SptSIpKSkpKSkpKSkpKSlp52lwaTOQlJSUlJSUlJSUlJR0pFJecCUlJSUlJSUlJSUlJV1MlBdcSUlJSUlJSUlJSUlJFxPlBVdSUlJSUlJSUlJSUtLFRHnBlZ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r1ci1q8CyVSti9ezeee+45+d5TTz0VBoMBzz77LF544QWMjo7irLPOwvr162G321EoFDA4OIinn34aGzduRG1tLbLZLJ599lk8+eSTMBqNWL58OTZt2iT1dg5HIcvn83j++efxyCOPoFQqYcmSJfjCF74gdVR6enqwZ88etLW1Ye7cuQf9vFQqhUcffRS/+93vAABnnXUWLrrooqqaMk8++SRcLhfOO+88Ie2J18MEB3v27MHbb7+Nyy+/HEajEYODg3jmmWeqiOTkk0/G/PnzdQz6I0JzpOZIjWMHmiM1R04HM/rAxSwobW1tuPbaaxGPx9He3o5QKCTpb/m6XC6Hp59+GiaTCTfddBPq6urwwAMPYM+ePSiXy+jr68Mvf/lL7Ny5E263G+vXr8fatWuxa9cuPProo+jo6MCiRYtw2WWXYevWrdixYwe+/vWvw2az4fbbb0dbWxuuvPJKPPTQQ3jhhRdQLBZx2mmn4ZJLLsHAwAC+973voba2Ful0GuFwGLW1tfjf//1f7N+/HzabDXfeeSduuOEGrFq1SjLIEHv37sUvf/lLvPfee4hEIrjwwguxbNkyvPnmm3jwwQcRjUaxcuVKnHLKKXjqqaewf/9+GAwGBAIB9PX14ZxzzsEXv/hFKdimofFJRaVSQUdHB/7t3/4N+/btQ1NTE37605/C6/XizDPPxK233gqr1YqFCxfim9/8JrLZLK677rqq+hXAuIq1d+9efP/730dvby8aGhrws5/9DE6nE3PmzEFfXx8ymQx6enrw1FNP4dprr8X69evl/IqqYvX19eH222/Hjh07UFdXh//6r//CddddB5PJhDvvvBORSATpdBrf/e530draivnz5+O5557Dk08+iSeeeAJr1qxBKBTCtm3bcPPNNyMSiSAUCuEHP/gB/H4/zj///EMGWCr0O3fuxLXXXouGhgbMnj0b//Iv/wKPx4OLLroIL7/8Mv77v/8bu3fvxvXXX485c+YAADKZDHbt2oVQKITGxkaYzWY88cQTuPHGG3HOOecgk8ngpptuQktLC1atWoVcLofnn38et956K0477TSsWbMGDocDsVgM7e3tCIfDiEQiMBqN6O3txcsvv4x///d/h9FoxGWXXQaDwYC9e/filltuwYoVKxAIBGC1WlFTU4P58+cf2QGkUQXNkZojNY4taI48eLtojqzGjD5wcVuOhRHnz5+PHTt2SEE2vsZgGK9P0N3dLQUaAeCaa66Bz+dDoVDA3XffjZ/85Cc444wz8MYbb6CzsxMejwcPPPAAXnnlFTQ3N+Pee++V7cOHH34Yp59+OhwOB371q1/hBz/4AZ555hl84xvfgM/nAwBs3boVjY2NqK2txf333w+LxYITTzwRZ5xxhqR5JSGq26yqLzSXy+GOO+7A448/js9+9rN48cUX0dXVheuvvx4//OEP8c4776C1tRU///nPsXPnTrz++uuiNhaLRRQKBezYsQPr169HOByeyebX0JgxqPN1x44d2LVrFzZv3owLL7wQf/3Xf43f/va3aG1txb59+/CP//iP+MIXvoDnnnsO9957L6677roPBGIqgLt378bXv/51nHbaabjpppvwyiuv4KyzzsI//MM/wGKx4Kc//SleeuklnHnmmQf1Tw8ODuKNN97AFVdcgeXLl+Pv//7v8bvf/U5S1958880ol8v40pe+hBdffBGtra3o6urCtm3bJCVzsVjEI488gkwmgx/+8Icol8u46qqr8Mwzz2Dt2rWSAvhghMIdiAceeAADAwP4zW9+g+bmZrz99tt46KGHcO6556K3txfvvfeeFDbl5w0NDeFnP/sZTj31VFx88cUwGAz4xS9+gbq6Ovz4xz/G4OAgVq1ahcceewwrV65EV1cX7rnnHkSj0arCw+3t7fj5z3+OtWvX4txzz4XVakU6nUZHRwd27tyJJUuWyPWz5s2mTZsQDAYRiUQwb948ANpO+MeE5kjNkRrHBjRHao6cLg5eEvmjfOj/+1ZZ7I5koR7yY3594EBF++effx4vvfQSxsbGcN9992HJkiW46667cP3112P+/PnYuXMntm3bhrVr1+I73/kO5syZg6eeegrz5s3DrFmz8Nhjj+HXv/41amtrsWHDBvz2t79FLBZDQ0ODWBrefPNNGAwGuFwunH322bjjjjtw5ZVX4rLLLsNXvvIVzJ49GwsWLMC3vvUtLF26tEphIwk+9dRTWL16Ne666y781V/9FSKRCF599VW8+OKLOOuss/BP//RPmDt3L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myKl/f6jP0tD4JENz5KHb5njiyGXLluH111+f9A0z+sCloaFx7EENmmpmMf5NPYirprWe7HPUn4mvnSwwHuozCNUuwEPIE4Mlr39ikciJ3zud4MrrOFgb8G8Tf6eC1z1Z26jv4fvUtp/4OQStaRPbV/2MD/Ow9f/v1Q9c04DmSA2NYxuaI6dum+OJI6d64JrxtPAaGhrHFhjsJvq1GYgON13qZKreVJ8/1edMFgQPdT3q+/idBoOh6vUf5gHkUJ9xsHab7Hfq3yZrp0N9zmTfeaj+09DQ0ND48NAceejr0RypH7g0NDQOAwcLPNMNSB/1cw71uqn+Pp1gfLj4KNczndd+1HbWD1caGhoaRw6aI2f+eqbz2qOBI3VlQQ0NDQ0NDQ0NDQ0NjSOEj3yGy2AwDAHYPzOXo6GhoaHxCUdLpVKp/bgv4miB5kgNDQ2N4wYH5ceP/MCloaGhoaGhoaGhoaGhMTm0pVBDQ0NDQ0NDQ0NDQ+MIQT9waWhoaGhoaGhoaGhoHCHoBy4NDQ0NDQ0NDQ0NDY0jBP3ApaGhoaGhoaGhoaGhcYSgH7g0NDQ0NDQ0NDQ0NDSOEPQDl4aGhoaGhoaGhoaGxhHCR37gMhgMK2fiQj4uGAyGFQaDIfxxX8eHgcFgWHkUX/tnDAZD4OO+jg8Lg8Gw6ihu+88e5W1/+tHa9sDR3/4a08PRzJGaHz8+HM0ceTTzI3D0x+ijmSOP9rafCroOl4aGhoaGhoaGhoaGxhGCthRqaGhoaGhoaGhoaGgcIegHLg0NDQ0NDQ0NDQ0NjSME/cCloaGhoaGhoaGhoaFxhKAfuDQ0NDQ0NDQ0NDQ0NI4Q9AOXhoaGhoaGhoaGhobGEcL/AXSwPC8Odq4zAAAAAElFTkSuQmCC\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "tags": [], - "needs_background": "light" - } - }, - { - "output_type": "display_data", - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAFDCAYAAAAu+g+jAAAABHNCSVQICAgIfAhkiAAAAA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c/xpXuo7c6oL91/50cuu+e6kZ+DV0nr/vu5RAUmtZd0Hsrvz/whKt/sEYyzIUNa2TfdXMxaWRfE64B3xaeYZgLCzJ23eO1yRDJt6Q9VTny/56q/L7K6ckInup+5NfRd0qS1OXzP2YCcqoyequ3nn4n/1tP9XSqcnr6Tvmz9lU+wzAM039YI099P6yRPOFiGOY06M3w9NcgnWk5p/pcX3/vjzE+Xc7kfvrz2TOtZ55cMQzDnD1YIwf+fvrz2fNBI/lkQYZhGIZhGIZhmLPEGedwSZLUBKBqYG6HYRiGOcfJSiQSKf/rmzhfYI1kGIa5aOhVH894wsUwDMMwDMMwDMP0DIcUMgzDMAzDMAzDnCV4wsUwDMMwDMMwDHOW4AkXwzAMwzAMwzDMWYInXAzDMAzDMAzDMGcJnnAxDMMwDMMwDMOcJXjCxTAMwzAMwzAMc5Y44wmXJEmXD8SN/K+QJKlEkqTU//V9/BgkSbr8PL73KyVJSv5f38ePRZKksedx3f+/87zux5+vdQ+c//XP9I/zWSNZH/93nM8aeT7rI3D+2+jzWSPP97rvCz6Hi2EYhmEYhmEY5izBIYUMwzAMwzAMwzBnCZ5wMQzDMAzDMAzDnCV4wsUwDMMwDMMwDHOW4AkXwzAMwzAMwzDMWYInXAzDMAzDMAzDMGeJ/w9aiLv/t+87XAAAAABJRU5ErkJggg==\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "tags": [], - "needs_background": "light" - } - }, - { - "output_type": "display_data", - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAFDCAYAAAAu+g+jAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nOy9ebht21nW+Y3dnHNvSEsSQqNRkBIsKEQEAS1ERR+KVAC1RCwCCFEioKKGLtIEiCARpC2waKURKjRGKXrFQv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mnFn3c7Pgs0TZt1Frf9Ek3TCj7vdnxWaJo2+yzu+0vP8r6fe7b2PXD297/CqeFs5kjFj58fzmaOPJv5ETj7bfTZzJFne98PB1WHS0FBQUFBQUFBQUFB4QxBuRQqKCgoKCgoKCgoKCicIagNl4KCgoKCgoKCgoKCwhmC2nApKCgoKCgoKCgoKCicIagNl4KCgoKCgoKCgoKCwhmC2nApKCgoKCgoKCgoKCicIfw/kWFA/Wu0KJ0AAAAASUVORK5CYII=\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "tags": [], - "needs_background": "light" - } - }, - { - "output_type": "display_data", - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAA1wAAAFDCAYAAAAu+g+jAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nOx9ebxkV1X1OvXqdaczJyQkBAIhJjIqIjP4McmMCCICn8woyCwKCEKYEUGRWWSIgIg4MAUhIHyCgAhBGQVMMAlJCEkgA+mETne/96rqfn/c2vVWrbfOrXrpF+hh7186r+rec/bZe5+99zp333NvlaZpkJSUlJSUlJSUlJSUlLTx1PtZC5CUlJSUlJSUlJSUlLS3Ul5wJSUlJSUlJSUlJSUlXUOUF1xJSUlJSUlJSUlJSUnXEOUFV1JSUlJSUlJSUlJS0jVEecGVlJSUlJSUlJSUlJR0DVFecCUlJSUlJSUlJSUlJV1DlBdcSUlJSUlJSUlJSUlJ1xDlBVfSbk2llHNLKTtKKdvo3zFXk9djSylf2GD5HltKGY7lurKU8s1Syq/N6HNwKeX1pZTvj/udPf5+xPj8uaWUi0spB1Cf3y2lfJa+N6WUb5VSenTsFaWUd2+kfklJSUlJuweVUn67lPKVMW5cVEr5RCnlVzaA77tLKa/YIBln8hrj11VjPS4opby2lLIwo09V91LKS8Y8H0rt++Njx5Fc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3bThzPHHmwA9cxqcOloaFx/EA1yPv798PN3nOw5xzKH3z05/dnBA/VztFkQWJU2/91FfXRbT+YoT7Qv+/vGfvrl8Np4/5uE77OczQ0NDQ0Dg7NkYeG5kh94NLQ0DgMHKlhPFbPOdTnDvbzA5HM0WAsDfLBCHKsnqehoaGhMfbQHPn12nO0z/o2caSuLKihoaGhoaGhoaGhoXGMcNQxXIZhxAB0jE1zNDQ0NDS+4RhfqVQif+xGfFugOVJDQ0PjhMEB+fGoD1waGhoaGhoaGhoaGhoa+4d2KdTQ0NDQ0NDQ0NDQ0DhG0AcuDQ0NDQ0NDQ0NDQ2NYwR94NLQ0NDQ0NDQ0NDQ0DhG0AcuDQ0NDQ0NDQ0NDQ2NYwR94NLQ0NDQ0NDQ0NDQ0DhG0AcuDQ0NDQ0NDQ0NDQ2NY4SjPnAZhnH6WDTkjwXDMOYZhtHwx27H14FhGKd/i9t+tmEYgT92O74uDMM441vc9+d8y/v+rG9r3wPf/v7XODJ8mzlS8+MfD99mjvw28yPw7bfR32aO/Lb3/cGg63BpaGhoaGhoaGhoaGgcI2iXQg0NDQ0NDQ0NDQ0NjWMEfeDS0NDQ0NDQ0NDQ0NA4RtAHLg0NDQ0NDQ0NDQ0NjWMEfeDS0NDQ0NDQ0NDQ0NA4RtAHLg0NDQ0NDQ0NDQ0NjWOE/weewGggFgupFgAAAABJRU5ErkJggg==\n", - "text/plain": [ - "
" - ] - }, - "metadata": { - "tags": [], - "needs_background": "light" - } - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "lMombPr0GF9a", - "colab_type": "text" - }, - "source": [ - "The images used in this demo are from the [Snapshot Serengeti dataset](http://lila.science/datasets/snapshot-serengeti), and released under the [Community Data License Agreement (permissive variant)](https://cdla.io/permissive-1-0/)." - ] - } - ] -} \ No newline at end of file diff --git a/research/object_detection/colab_tutorials/convert_odt_model_to_TFLite.ipynb b/research/object_detection/colab_tutorials/convert_odt_model_to_TFLite.ipynb deleted file mode 100644 index 37f0ab841e4..00000000000 --- a/research/object_detection/colab_tutorials/convert_odt_model_to_TFLite.ipynb +++ /dev/null @@ -1,413 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "RD3uxzaJweYr" - }, - "source": [ - "##### Copyright 2021 The TensorFlow Authors." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "cellView": "form", - "id": "C-vBUz5IhJs8" - }, - "outputs": [], - "source": [ - "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", - "# you may not use this file except in compliance with the License.\n", - "# You may obtain a copy of the License at\n", - "#\n", - "# https://www.apache.org/licenses/LICENSE-2.0\n", - "#\n", - "# Unless required by applicable law or agreed to in writing, software\n", - "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", - "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", - "# See the License for the specific language governing permissions and\n", - "# limitations under the License." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "pHTibyMehTvH" - }, - "source": [ - "# Tutorial: Convert models trained using TensorFlow Object Detection API to TensorFlow Lite\n", - "\n", - "This tutorial demonstrate these steps:\n", - "* Convert TensorFlow models trained using the TensorFlow Object Detection API to [TensorFlow Lite](https://www.tensorflow.org/lite).\n", - "* Add the required metadata using [TFLite Metadata Writer API](https://www.tensorflow.org/lite/convert/metadata_writer_tutorial#object_detectors). This will make the TFLite model compatible with [TFLite Task Library](https://www.tensorflow.org/lite/inference_with_metadata/task_library/object_detector), so that the model can be integrated in mobile apps in 3 lines of code." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "QIR1IFpnLJJA" - }, - "source": [ - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.sandbox.google.com/github/tensorflow/models/blob/master/research/object_detection/colab_tutorials/convert_odt_model_to_TFLite.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/models/blob/master/research/object_detection/colab_tutorials/convert_odt_model_to_TFLite.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Ok_Rpv7XNaFJ" - }, - "source": [ - "## Preparation" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "t7CAW5C1cmel" - }, - "source": [ - "### Install the TFLite Support Library" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "DwtFa0jSnNU4" - }, - "outputs": [], - "source": [ - "!pip install -q tflite_support" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "XRfJR9QXctAR" - }, - "source": [ - "### Install the TensorFlow Object Detection API\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "7PP2P5XAqeI5" - }, - "outputs": [], - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "# Clone the tensorflow models repository if it doesn't already exist\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "bP6SSh6zqi07" - }, - "outputs": [], - "source": [ - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=.\n", - "cp object_detection/packages/tf2/setup.py .\n", - "pip install -q ." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "i0to7aXKc0O9" - }, - "source": [ - "### Import the necessary libraries" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "4M8CC1PgqnSf" - }, - "outputs": [], - "source": [ - "import matplotlib\n", - "import matplotlib.pyplot as plt\n", - "\n", - "import os\n", - "import random\n", - "import io\n", - "import imageio\n", - "import glob\n", - "import scipy.misc\n", - "import numpy as np\n", - "from six import BytesIO\n", - "from PIL import Image, ImageDraw, ImageFont\n", - "from IPython.display import display, Javascript\n", - "from IPython.display import Image as IPyImage\n", - "\n", - "import tensorflow as tf\n", - "\n", - "from object_detection.utils import label_map_util\n", - "from object_detection.utils import config_util\n", - "from object_detection.utils import visualization_utils as viz_utils\n", - "from object_detection.utils import colab_utils\n", - "from object_detection.utils import config_util\n", - "from object_detection.builders import model_builder\n", - "\n", - "%matplotlib inline" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "s9WIOOMTNti5" - }, - "source": [ - "## Download a pretrained model from Model Zoo\n", - "\n", - "In this tutorial, we demonstrate converting a pretrained model `SSD MobileNet V2 FPNLite 640x640` in the [TensorFlow 2 Model Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md). You can replace the model with your own model and the rest will work the same." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "TIY3cxDgsxuZ" - }, - "outputs": [], - "source": [ - "!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.tar.gz\n", - "!tar -xf ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.tar.gz\n", - "!rm ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.tar.gz" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0gV8vr6nN-z9" - }, - "source": [ - "## Generate TensorFlow Lite Model" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Z8FjeSmmxpXz" - }, - "source": [ - "### Step 1: Export TFLite inference graph\n", - "\n", - "First, we invoke `export_tflite_graph_tf2.py` to generate a TFLite-friendly intermediate SavedModel. This will then be passed to the TensorFlow Lite Converter for generating the final model.\n", - "\n", - "Use `--help` with the above script to get the full list of supported parameters.\n", - "These can fine-tune accuracy and speed for your model." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ChfN-tzBXqko" - }, - "outputs": [], - "source": [ - "!python models/research/object_detection/export_tflite_graph_tf2.py \\\n", - " --trained_checkpoint_dir {'ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/checkpoint'} \\\n", - " --output_directory {'ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite'} \\\n", - " --pipeline_config_path {'ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/pipeline.config'}" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "IPr06cZ3OY3H" - }, - "source": [ - "### Step 2: Convert to TFLite\n", - "\n", - "Use the [TensorFlow Lite Converter](https://www.tensorflow.org/lite/convert) to\n", - "convert the `SavedModel` to TFLite. Note that you need to use `from_saved_model`\n", - "for TFLite conversion with the Python API.\n", - "\n", - "You can also leverage\n", - "[Post-training Quantization](https://www.tensorflow.org/lite/performance/post_training_quantization)\n", - "to\n", - "[optimize performance](https://www.tensorflow.org/lite/performance/model_optimization)\n", - "and obtain a smaller model. In this tutorial, we use the [dynamic range quantization](https://www.tensorflow.org/lite/performance/post_training_quant)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "JMpy3Rlpq-Yq" - }, - "outputs": [], - "source": [ - "_TFLITE_MODEL_PATH = \"ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/model.tflite\"\n", - "\n", - "converter = tf.lite.TFLiteConverter.from_saved_model('ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite/saved_model')\n", - "converter.optimizations = [tf.lite.Optimize.DEFAULT]\n", - "tflite_model = converter.convert()\n", - "\n", - "with open(_TFLITE_MODEL_PATH, 'wb') as f:\n", - " f.write(tflite_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "fyjlnmaEOtKp" - }, - "source": [ - "### Step 3: Add Metadata\n", - "\n", - "The model needs to be packed with [TFLite Metadata](https://www.tensorflow.org/lite/convert/metadata) to enable easy integration into mobile apps using the [TFLite Task Library](https://www.tensorflow.org/lite/inference_with_metadata/task_library/object_detector). This metadata helps the inference code perform the correct pre \u0026 post processing as required by the model. Use the following code to create the metadata." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "-ecGLG_Ovjcr" - }, - "outputs": [], - "source": [ - "# Download the COCO dataset label map that was used to trained the SSD MobileNet V2 FPNLite 640x640 model\n", - "!wget https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/mscoco_label_map.pbtxt -q\n", - "\n", - "# We need to convert the Object Detection API's labelmap into what the Task API needs:\n", - "# a txt file with one class name on each line from index 0 to N.\n", - "# The first '0' class indicates the background.\n", - "# This code assumes COCO detection which has 90 classes, you can write a label\n", - "# map file for your model if re-trained.\n", - "_ODT_LABEL_MAP_PATH = 'mscoco_label_map.pbtxt'\n", - "_TFLITE_LABEL_PATH = \"ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/tflite_label_map.txt\"\n", - "\n", - "category_index = label_map_util.create_category_index_from_labelmap(\n", - " _ODT_LABEL_MAP_PATH)\n", - "f = open(_TFLITE_LABEL_PATH, 'w')\n", - "for class_id in range(1, 91):\n", - " if class_id not in category_index:\n", - " f.write('???\\n')\n", - " continue\n", - " name = category_index[class_id]['name']\n", - " f.write(name+'\\n')\n", - "f.close()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "YJSyXq5Qss9X" - }, - "source": [ - "Then we'll add the label map and other necessary metadata (e.g. normalization config) to the TFLite model.\n", - "\n", - "As the `SSD MobileNet V2 FPNLite 640x640` model take input image with pixel value in the range of [-1..1] ([code](https://github.com/tensorflow/models/blob/b09e75828e2c65ead9e624a5c7afed8d214247aa/research/object_detection/models/ssd_mobilenet_v2_keras_feature_extractor.py#L132)), we need to set `norm_mean = 127.5` and `norm_std = 127.5`. See this [documentation](https://www.tensorflow.org/lite/convert/metadata#normalization_and_quantization_parameters) for more details on the normalization parameters." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "CRQpfDAWsPeK" - }, - "outputs": [], - "source": [ - "from tflite_support.metadata_writers import object_detector\n", - "from tflite_support.metadata_writers import writer_utils\n", - "\n", - "_TFLITE_MODEL_WITH_METADATA_PATH = \"ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/model_with_metadata.tflite\"\n", - "\n", - "writer = object_detector.MetadataWriter.create_for_inference(\n", - " writer_utils.load_file(_TFLITE_MODEL_PATH), input_norm_mean=[127.5], \n", - " input_norm_std=[127.5], label_file_paths=[_TFLITE_LABEL_PATH])\n", - "writer_utils.save_file(writer.populate(), _TFLITE_MODEL_WITH_METADATA_PATH)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "YFEAjRBdPCQb" - }, - "source": [ - "Optional: Print out the metadata added to the TFLite model." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "FT3-38PJsSOt" - }, - "outputs": [], - "source": [ - "from tflite_support import metadata\n", - "\n", - "displayer = metadata.MetadataDisplayer.with_model_file(_TFLITE_MODEL_WITH_METADATA_PATH)\n", - "print(\"Metadata populated:\")\n", - "print(displayer.get_metadata_json())\n", - "print(\"=============================\")\n", - "print(\"Associated file(s) populated:\")\n", - "print(displayer.get_packed_associated_file_list())" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "l7zVslTRnEHX" - }, - "source": [ - "The TFLite model now can be integrated into a mobile app using the TFLite Task Library. See the [documentation](https://www.tensorflow.org/lite/inference_with_metadata/task_library/object_detector) for more details." - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "Convert TF Object Detection API model to TFLite.ipynb", - "private_outputs": true, - "provenance": [ - { - "file_id": "1R4_y-u14YTdvBzhmvC0HQwh3HkcCN2Bd", - "timestamp": 1623114733432 - }, - { - "file_id": "1Rey5kAzNQhJ77tsXGjhcAV0UZ6du0Sla", - "timestamp": 1622897882140 - } - ], - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - }, - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/research/object_detection/colab_tutorials/deepmac_colab.ipynb b/research/object_detection/colab_tutorials/deepmac_colab.ipynb deleted file mode 100644 index d0458a33d53..00000000000 --- a/research/object_detection/colab_tutorials/deepmac_colab.ipynb +++ /dev/null @@ -1,344 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "deepmac_colab.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - }, - "accelerator": "GPU" - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "P-esW81yhfCN" - }, - "source": [ - "# Novel class segmentation demo with Deep-MAC\n", - "\n", - "Welcome to the Novel class segmentation (with Deep-MAC) demo --- this colab loads a Deep-MAC model and tests it interactively with user-specified boxes. Deep-MAC was only trained to detect and segment COCO classes, but generalizes well when segmenting within user-specified boxes of unseen classes.\n", - "\n", - "Estimated time to run through this colab (with GPU): 10-15 minutes.\n", - "Note that the bulk of this time is in installing Tensorflow and downloading\n", - "the checkpoint then running inference for the first time. Once you've done\n", - "all that, running on new images is very fast." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Kq1eGNssiW31" - }, - "source": [ - "# Prerequisites\n", - "\n", - "Please change runtime to GPU." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "UT7N0HJhiRKr" - }, - "source": [ - "# Installation and Imports\n", - "\n", - "This takes 3-4 minutes." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "nNdls0Pe0UPK" - }, - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "# Clone the tensorflow models repository if it doesn't already exist\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models\n" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "WwjV9clX0n7S" - }, - "source": [ - "# Install the Object Detection API\n", - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=.\n", - "cp object_detection/packages/tf2/setup.py .\n", - "\n", - "# The latest tf-models-official installs tensorflow 2.9 which has an\n", - "# incompatible CuDNN dependency. Here we restrict ourselves to versions 2.8 and\n", - "# below.\n", - "python -m pip install \"tf-models-official<=2.8\" ." - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "sfrrno2L0sRR" - }, - "source": [ - "import glob\n", - "import io\n", - "import logging\n", - "import os\n", - "import random\n", - "import warnings\n", - "\n", - "import imageio\n", - "from IPython.display import display, Javascript\n", - "from IPython.display import Image as IPyImage\n", - "import matplotlib\n", - "from matplotlib import patches\n", - "import matplotlib.pyplot as plt\n", - "import numpy as np\n", - "from object_detection.utils import colab_utils\n", - "from object_detection.utils import ops\n", - "from object_detection.utils import visualization_utils as viz_utils\n", - "from PIL import Image, ImageDraw, ImageFont\n", - "import scipy.misc\n", - "from six import BytesIO\n", - "from skimage import color\n", - "from skimage import transform\n", - "from skimage import util\n", - "from skimage.color import rgb_colors\n", - "import tensorflow as tf\n", - "\n", - "%matplotlib inline\n", - "\n", - "COLORS = ([rgb_colors.cyan, rgb_colors.orange, rgb_colors.pink,\n", - " rgb_colors.purple, rgb_colors.limegreen , rgb_colors.crimson] +\n", - " [(color) for (name, color) in color.color_dict.items()])\n", - "random.shuffle(COLORS)\n", - "\n", - "logging.disable(logging.WARNING)\n", - "\n", - "\n", - "def read_image(path):\n", - " \"\"\"Read an image and optionally resize it for better plotting.\"\"\"\n", - " with tf.io.gfile.GFile(path, 'rb') as f:\n", - " img = Image.open(f)\n", - " return np.array(img, dtype=np.uint8)\n", - "\n", - "\n", - "def resize_for_display(image, max_height=600):\n", - " height, width, _ = image.shape\n", - " width = int(width * max_height / height)\n", - " with warnings.catch_warnings():\n", - " warnings.simplefilter(\"ignore\", UserWarning)\n", - " return util.img_as_ubyte(transform.resize(image, (height, width)))\n", - "\n", - "\n", - "def get_mask_prediction_function(model):\n", - " \"\"\"Get single image mask prediction function using a model.\"\"\"\n", - "\n", - " @tf.function\n", - " def predict_masks(image, boxes):\n", - " height, width, _ = image.shape.as_list()\n", - " batch = image[tf.newaxis]\n", - " boxes = boxes[tf.newaxis]\n", - "\n", - " detections = model(batch, boxes)\n", - " masks = detections['detection_masks']\n", - "\n", - " return ops.reframe_box_masks_to_image_masks(masks[0], boxes[0],\n", - " height, width)\n", - "\n", - " return predict_masks\n", - "\n", - "\n", - "def plot_image_annotations(image, boxes, masks, darken_image=0.5):\n", - " fig, ax = plt.subplots(figsize=(16, 12))\n", - " ax.set_axis_off()\n", - " image = (image * darken_image).astype(np.uint8)\n", - " ax.imshow(image)\n", - "\n", - " height, width, _ = image.shape\n", - "\n", - " num_colors = len(COLORS)\n", - " color_index = 0\n", - "\n", - " for box, mask in zip(boxes, masks):\n", - " ymin, xmin, ymax, xmax = box\n", - " ymin *= height\n", - " ymax *= height\n", - " xmin *= width\n", - " xmax *= width\n", - "\n", - " color = COLORS[color_index]\n", - " color = np.array(color)\n", - " rect = patches.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,\n", - " linewidth=2.5, edgecolor=color, facecolor='none')\n", - " ax.add_patch(rect)\n", - " mask = (mask > 0.5).astype(np.float32)\n", - " color_image = np.ones_like(image) * color[np.newaxis, np.newaxis, :]\n", - " color_and_mask = np.concatenate(\n", - " [color_image, mask[:, :, np.newaxis]], axis=2)\n", - "\n", - " ax.imshow(color_and_mask, alpha=0.5)\n", - "\n", - " color_index = (color_index + 1) % num_colors\n", - "\n", - " return ax" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ry9yq8zsi0Gg" - }, - "source": [ - "# Load Deep-MAC Model\n", - "\n", - "This can take up to 5 minutes." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "PZ-wnbYu05K8" - }, - "source": [ - "print('Downloading and untarring model')\n", - "!wget http://download.tensorflow.org/models/object_detection/tf2/20210329/deepmac_1024x1024_coco17.tar.gz\n", - "!cp deepmac_1024x1024_coco17.tar.gz models/research/object_detection/test_data/\n", - "!tar -xzf models/research/object_detection/test_data/deepmac_1024x1024_coco17.tar.gz\n", - "!mv deepmac_1024x1024_coco17 models/research/object_detection/test_data/\n", - "model_path = 'models/research/object_detection/test_data/deepmac_1024x1024_coco17/saved_model'\n", - "\n", - "print('Loading SavedModel')\n", - "model = tf.keras.models.load_model(model_path)\n", - "prediction_function = get_mask_prediction_function(model)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ilXkYOB_NUSc" - }, - "source": [ - "# Load image" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "txj4UkoDNaOq" - }, - "source": [ - "image_path = 'models/research/object_detection/test_images/image3.jpg'\n", - "image = read_image(image_path)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "zyhudgYUjcvE" - }, - "source": [ - "# Annotate an image with one or more boxes\n", - "\n", - "This model is trained on COCO categories, but we encourage you to try segmenting\n", - "anything you want!\n", - "\n", - "Don't forget to hit **submit** when done." - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "aZvY4At0074j" - }, - "source": [ - "display_image = resize_for_display(image)\n", - "\n", - "boxes_list = []\n", - "colab_utils.annotate([display_image], boxes_list)" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gUUG7NPBJMoa" - }, - "source": [ - "# In case you didn't want to label...\n", - "\n", - "Run this cell only if you didn't annotate anything above and would prefer to just use our preannotated boxes. Don't forget to uncomment.\n", - "\n" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "lupqTv1HJK5K" - }, - "source": [ - "# boxes_list = [np.array([[0.000, 0.160, 0.362, 0.812],\n", - "# [0.340, 0.286, 0.472, 0.619],\n", - "# [0.437, 0.008, 0.650, 0.263],\n", - "# [0.382, 0.003, 0.538, 0.594],\n", - "# [0.518, 0.444, 0.625,0.554]], dtype=np.float32)]" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Ak1WO93NjvN-" - }, - "source": [ - "# Visualize mask predictions" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "vdzuKnpj1A3L" - }, - "source": [ - "\n", - "%matplotlib inline\n", - "\n", - "boxes = boxes_list[0]\n", - "masks = prediction_function(tf.convert_to_tensor(image),\n", - " tf.convert_to_tensor(boxes, dtype=tf.float32))\n", - "plot_image_annotations(image, boxes, masks.numpy())\n", - "plt.show()" - ], - "execution_count": null, - "outputs": [] - } - ] -} \ No newline at end of file diff --git a/research/object_detection/colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb b/research/object_detection/colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb deleted file mode 100644 index a779528fa76..00000000000 --- a/research/object_detection/colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb +++ /dev/null @@ -1,685 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "rOvvWAVTkMR7" - }, - "source": [ - "# Eager Few Shot Object Detection Colab\n", - "\n", - "Welcome to the Eager Few Shot Object Detection Colab --- in this colab we demonstrate fine tuning of a (TF2 friendly) RetinaNet architecture on very few examples of a novel class after initializing from a pre-trained COCO checkpoint.\n", - "Training runs in eager mode.\n", - "\n", - "Estimated time to run through this colab (with GPU): \u003c 5 minutes." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "vPs64QA1Zdov" - }, - "source": [ - "## Imports" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "LBZ9VWZZFUCT" - }, - "outputs": [], - "source": [ - "!pip install -U --pre tensorflow==\"2.2.0\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "oi28cqGGFWnY" - }, - "outputs": [], - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "# Clone the tensorflow models repository if it doesn't already exist\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "NwdsBdGhFanc" - }, - "outputs": [], - "source": [ - "# Install the Object Detection API\n", - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=.\n", - "cp object_detection/packages/tf2/setup.py .\n", - "python -m pip install ." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "uZcqD4NLdnf4" - }, - "outputs": [], - "source": [ - "import matplotlib\n", - "import matplotlib.pyplot as plt\n", - "\n", - "import os\n", - "import random\n", - "import io\n", - "import imageio\n", - "import glob\n", - "import scipy.misc\n", - "import numpy as np\n", - "from six import BytesIO\n", - "from PIL import Image, ImageDraw, ImageFont\n", - "from IPython.display import display, Javascript\n", - "from IPython.display import Image as IPyImage\n", - "\n", - "import tensorflow as tf\n", - "\n", - "from object_detection.utils import label_map_util\n", - "from object_detection.utils import config_util\n", - "from object_detection.utils import visualization_utils as viz_utils\n", - "from object_detection.utils import colab_utils\n", - "from object_detection.builders import model_builder\n", - "\n", - "%matplotlib inline" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "IogyryF2lFBL" - }, - "source": [ - "# Utilities" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "-y9R0Xllefec" - }, - "outputs": [], - "source": [ - "def load_image_into_numpy_array(path):\n", - " \"\"\"Load an image from file into a numpy array.\n", - "\n", - " Puts image into numpy array to feed into tensorflow graph.\n", - " Note that by convention we put it into a numpy array with shape\n", - " (height, width, channels), where channels=3 for RGB.\n", - "\n", - " Args:\n", - " path: a file path.\n", - "\n", - " Returns:\n", - " uint8 numpy array with shape (img_height, img_width, 3)\n", - " \"\"\"\n", - " img_data = tf.io.gfile.GFile(path, 'rb').read()\n", - " image = Image.open(BytesIO(img_data))\n", - " (im_width, im_height) = image.size\n", - " return np.array(image.getdata()).reshape(\n", - " (im_height, im_width, 3)).astype(np.uint8)\n", - "\n", - "def plot_detections(image_np,\n", - " boxes,\n", - " classes,\n", - " scores,\n", - " category_index,\n", - " figsize=(12, 16),\n", - " image_name=None):\n", - " \"\"\"Wrapper function to visualize detections.\n", - "\n", - " Args:\n", - " image_np: uint8 numpy array with shape (img_height, img_width, 3)\n", - " boxes: a numpy array of shape [N, 4]\n", - " classes: a numpy array of shape [N]. Note that class indices are 1-based,\n", - " and match the keys in the label map.\n", - " scores: a numpy array of shape [N] or None. If scores=None, then\n", - " this function assumes that the boxes to be plotted are groundtruth\n", - " boxes and plot all boxes as black with no classes or scores.\n", - " category_index: a dict containing category dictionaries (each holding\n", - " category index `id` and category name `name`) keyed by category indices.\n", - " figsize: size for the figure.\n", - " image_name: a name for the image file.\n", - " \"\"\"\n", - " image_np_with_annotations = image_np.copy()\n", - " viz_utils.visualize_boxes_and_labels_on_image_array(\n", - " image_np_with_annotations,\n", - " boxes,\n", - " classes,\n", - " scores,\n", - " category_index,\n", - " use_normalized_coordinates=True,\n", - " min_score_thresh=0.8)\n", - " if image_name:\n", - " plt.imsave(image_name, image_np_with_annotations)\n", - " else:\n", - " plt.imshow(image_np_with_annotations)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "sSaXL28TZfk1" - }, - "source": [ - "# Rubber Ducky data\n", - "\n", - "We will start with some toy (literally) data consisting of 5 images of a rubber\n", - "ducky. Note that the [coco](https://cocodataset.org/#explore) dataset contains a number of animals, but notably, it does *not* contain rubber duckies (or even ducks for that matter), so this is a novel class." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "SQy3ND7EpFQM" - }, - "outputs": [], - "source": [ - "# Load images and visualize\n", - "train_image_dir = 'models/research/object_detection/test_images/ducky/train/'\n", - "train_images_np = []\n", - "for i in range(1, 6):\n", - " image_path = os.path.join(train_image_dir, 'robertducky' + str(i) + '.jpg')\n", - " train_images_np.append(load_image_into_numpy_array(image_path))\n", - "\n", - "plt.rcParams['axes.grid'] = False\n", - "plt.rcParams['xtick.labelsize'] = False\n", - "plt.rcParams['ytick.labelsize'] = False\n", - "plt.rcParams['xtick.top'] = False\n", - "plt.rcParams['xtick.bottom'] = False\n", - "plt.rcParams['ytick.left'] = False\n", - "plt.rcParams['ytick.right'] = False\n", - "plt.rcParams['figure.figsize'] = [14, 7]\n", - "\n", - "for idx, train_image_np in enumerate(train_images_np):\n", - " plt.subplot(2, 3, idx+1)\n", - " plt.imshow(train_image_np)\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "cbKXmQoxcUgE" - }, - "source": [ - "# Annotate images with bounding boxes\n", - "\n", - "In this cell you will annotate the rubber duckies --- draw a box around the rubber ducky in each image; click `next image` to go to the next image and `submit` when there are no more images.\n", - "\n", - "If you'd like to skip the manual annotation step, we totally understand. In this case, simply skip this cell and run the next cell instead, where we've prepopulated the groundtruth with pre-annotated bounding boxes.\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "-nEDRoUEcUgL" - }, - "outputs": [], - "source": [ - "gt_boxes = []\n", - "colab_utils.annotate(train_images_np, box_storage_pointer=gt_boxes)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "wTP9AFqecUgS" - }, - "source": [ - "# In case you didn't want to label...\n", - "\n", - "Run this cell only if you didn't annotate anything above and\n", - "would prefer to just use our preannotated boxes. Don't forget\n", - "to uncomment." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "wIAT6ZUmdHOC" - }, - "outputs": [], - "source": [ - "# gt_boxes = [\n", - "# np.array([[0.436, 0.591, 0.629, 0.712]], dtype=np.float32),\n", - "# np.array([[0.539, 0.583, 0.73, 0.71]], dtype=np.float32),\n", - "# np.array([[0.464, 0.414, 0.626, 0.548]], dtype=np.float32),\n", - "# np.array([[0.313, 0.308, 0.648, 0.526]], dtype=np.float32),\n", - "# np.array([[0.256, 0.444, 0.484, 0.629]], dtype=np.float32)\n", - "# ]" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "Dqb_yjAo3cO_" - }, - "source": [ - "# Prepare data for training\n", - "\n", - "Below we add the class annotations (for simplicity, we assume a single class in this colab; though it should be straightforward to extend this to handle multiple classes). We also convert everything to the format that the training\n", - "loop below expects (e.g., everything converted to tensors, classes converted to one-hot representations, etc.)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "HWBqFVMcweF-" - }, - "outputs": [], - "source": [ - "\n", - "# By convention, our non-background classes start counting at 1. Given\n", - "# that we will be predicting just one class, we will therefore assign it a\n", - "# `class id` of 1.\n", - "duck_class_id = 1\n", - "num_classes = 1\n", - "\n", - "category_index = {duck_class_id: {'id': duck_class_id, 'name': 'rubber_ducky'}}\n", - "\n", - "# Convert class labels to one-hot; convert everything to tensors.\n", - "# The `label_id_offset` here shifts all classes by a certain number of indices;\n", - "# we do this here so that the model receives one-hot labels where non-background\n", - "# classes start counting at the zeroth index. This is ordinarily just handled\n", - "# automatically in our training binaries, but we need to reproduce it here.\n", - "label_id_offset = 1\n", - "train_image_tensors = []\n", - "gt_classes_one_hot_tensors = []\n", - "gt_box_tensors = []\n", - "for (train_image_np, gt_box_np) in zip(\n", - " train_images_np, gt_boxes):\n", - " train_image_tensors.append(tf.expand_dims(tf.convert_to_tensor(\n", - " train_image_np, dtype=tf.float32), axis=0))\n", - " gt_box_tensors.append(tf.convert_to_tensor(gt_box_np, dtype=tf.float32))\n", - " zero_indexed_groundtruth_classes = tf.convert_to_tensor(\n", - " np.ones(shape=[gt_box_np.shape[0]], dtype=np.int32) - label_id_offset)\n", - " gt_classes_one_hot_tensors.append(tf.one_hot(\n", - " zero_indexed_groundtruth_classes, num_classes))\n", - "print('Done prepping data.')\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "b3_Z3mJWN9KJ" - }, - "source": [ - "# Let's just visualize the rubber duckies as a sanity check\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "YBD6l-E4N71y" - }, - "outputs": [], - "source": [ - "dummy_scores = np.array([1.0], dtype=np.float32) # give boxes a score of 100%\n", - "\n", - "plt.figure(figsize=(30, 15))\n", - "for idx in range(5):\n", - " plt.subplot(2, 3, idx+1)\n", - " plot_detections(\n", - " train_images_np[idx],\n", - " gt_boxes[idx],\n", - " np.ones(shape=[gt_boxes[idx].shape[0]], dtype=np.int32),\n", - " dummy_scores, category_index)\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "ghDAsqfoZvPh" - }, - "source": [ - "# Create model and restore weights for all but last layer\n", - "\n", - "In this cell we build a single stage detection architecture (RetinaNet) and restore all but the classification layer at the top (which will be automatically randomly initialized).\n", - "\n", - "For simplicity, we have hardcoded a number of things in this colab for the specific RetinaNet architecture at hand (including assuming that the image size will always be 640x640), however it is not difficult to generalize to other model configurations." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "9J16r3NChD-7" - }, - "outputs": [], - "source": [ - "# Download the checkpoint and put it into models/research/object_detection/test_data/\n", - "\n", - "!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.tar.gz\n", - "!tar -xf ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.tar.gz\n", - "!mv ssd_resnet50_v1_fpn_640x640_coco17_tpu-8/checkpoint models/research/object_detection/test_data/" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "RyT4BUbaMeG-" - }, - "outputs": [], - "source": [ - "tf.keras.backend.clear_session()\n", - "\n", - "print('Building model and restoring weights for fine-tuning...', flush=True)\n", - "num_classes = 1\n", - "pipeline_config = 'models/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.config'\n", - "checkpoint_path = 'models/research/object_detection/test_data/checkpoint/ckpt-0'\n", - "\n", - "# Load pipeline config and build a detection model.\n", - "#\n", - "# Since we are working off of a COCO architecture which predicts 90\n", - "# class slots by default, we override the `num_classes` field here to be just\n", - "# one (for our new rubber ducky class).\n", - "configs = config_util.get_configs_from_pipeline_file(pipeline_config)\n", - "model_config = configs['model']\n", - "model_config.ssd.num_classes = num_classes\n", - "model_config.ssd.freeze_batchnorm = True\n", - "detection_model = model_builder.build(\n", - " model_config=model_config, is_training=True)\n", - "\n", - "# Set up object-based checkpoint restore --- RetinaNet has two prediction\n", - "# `heads` --- one for classification, the other for box regression. We will\n", - "# restore the box regression head but initialize the classification head\n", - "# from scratch (we show the omission below by commenting out the line that\n", - "# we would add if we wanted to restore both heads)\n", - "fake_box_predictor = tf.compat.v2.train.Checkpoint(\n", - " _base_tower_layers_for_heads=detection_model._box_predictor._base_tower_layers_for_heads,\n", - " # _prediction_heads=detection_model._box_predictor._prediction_heads,\n", - " # (i.e., the classification head that we *will not* restore)\n", - " _box_prediction_head=detection_model._box_predictor._box_prediction_head,\n", - " )\n", - "fake_model = tf.compat.v2.train.Checkpoint(\n", - " _feature_extractor=detection_model._feature_extractor,\n", - " _box_predictor=fake_box_predictor)\n", - "ckpt = tf.compat.v2.train.Checkpoint(model=fake_model)\n", - "ckpt.restore(checkpoint_path).expect_partial()\n", - "\n", - "# Run model through a dummy image so that variables are created\n", - "image, shapes = detection_model.preprocess(tf.zeros([1, 640, 640, 3]))\n", - "prediction_dict = detection_model.predict(image, shapes)\n", - "_ = detection_model.postprocess(prediction_dict, shapes)\n", - "print('Weights restored!')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "pCkWmdoZZ0zJ" - }, - "source": [ - "# Eager mode custom training loop\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "nyHoF4mUrv5-" - }, - "outputs": [], - "source": [ - "tf.keras.backend.set_learning_phase(True)\n", - "\n", - "# These parameters can be tuned; since our training set has 5 images\n", - "# it doesn't make sense to have a much larger batch size, though we could\n", - "# fit more examples in memory if we wanted to.\n", - "batch_size = 4\n", - "learning_rate = 0.01\n", - "num_batches = 100\n", - "\n", - "# Select variables in top layers to fine-tune.\n", - "trainable_variables = detection_model.trainable_variables\n", - "to_fine_tune = []\n", - "prefixes_to_train = [\n", - " 'WeightSharedConvolutionalBoxPredictor/WeightSharedConvolutionalBoxHead',\n", - " 'WeightSharedConvolutionalBoxPredictor/WeightSharedConvolutionalClassHead']\n", - "for var in trainable_variables:\n", - " if any([var.name.startswith(prefix) for prefix in prefixes_to_train]):\n", - " to_fine_tune.append(var)\n", - "\n", - "# Set up forward + backward pass for a single train step.\n", - "def get_model_train_step_function(model, optimizer, vars_to_fine_tune):\n", - " \"\"\"Get a tf.function for training step.\"\"\"\n", - "\n", - " # Use tf.function for a bit of speed.\n", - " # Comment out the tf.function decorator if you want the inside of the\n", - " # function to run eagerly.\n", - " @tf.function\n", - " def train_step_fn(image_tensors,\n", - " groundtruth_boxes_list,\n", - " groundtruth_classes_list):\n", - " \"\"\"A single training iteration.\n", - "\n", - " Args:\n", - " image_tensors: A list of [1, height, width, 3] Tensor of type tf.float32.\n", - " Note that the height and width can vary across images, as they are\n", - " reshaped within this function to be 640x640.\n", - " groundtruth_boxes_list: A list of Tensors of shape [N_i, 4] with type\n", - " tf.float32 representing groundtruth boxes for each image in the batch.\n", - " groundtruth_classes_list: A list of Tensors of shape [N_i, num_classes]\n", - " with type tf.float32 representing groundtruth boxes for each image in\n", - " the batch.\n", - "\n", - " Returns:\n", - " A scalar tensor representing the total loss for the input batch.\n", - " \"\"\"\n", - " shapes = tf.constant(batch_size * [[640, 640, 3]], dtype=tf.int32)\n", - " model.provide_groundtruth(\n", - " groundtruth_boxes_list=groundtruth_boxes_list,\n", - " groundtruth_classes_list=groundtruth_classes_list)\n", - " with tf.GradientTape() as tape:\n", - " preprocessed_images = tf.concat(\n", - " [detection_model.preprocess(image_tensor)[0]\n", - " for image_tensor in image_tensors], axis=0)\n", - " prediction_dict = model.predict(preprocessed_images, shapes)\n", - " losses_dict = model.loss(prediction_dict, shapes)\n", - " total_loss = losses_dict['Loss/localization_loss'] + losses_dict['Loss/classification_loss']\n", - " gradients = tape.gradient(total_loss, vars_to_fine_tune)\n", - " optimizer.apply_gradients(zip(gradients, vars_to_fine_tune))\n", - " return total_loss\n", - "\n", - " return train_step_fn\n", - "\n", - "optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate, momentum=0.9)\n", - "train_step_fn = get_model_train_step_function(\n", - " detection_model, optimizer, to_fine_tune)\n", - "\n", - "print('Start fine-tuning!', flush=True)\n", - "for idx in range(num_batches):\n", - " # Grab keys for a random subset of examples\n", - " all_keys = list(range(len(train_images_np)))\n", - " random.shuffle(all_keys)\n", - " example_keys = all_keys[:batch_size]\n", - "\n", - " # Note that we do not do data augmentation in this demo. If you want a\n", - " # a fun exercise, we recommend experimenting with random horizontal flipping\n", - " # and random cropping :)\n", - " gt_boxes_list = [gt_box_tensors[key] for key in example_keys]\n", - " gt_classes_list = [gt_classes_one_hot_tensors[key] for key in example_keys]\n", - " image_tensors = [train_image_tensors[key] for key in example_keys]\n", - "\n", - " # Training step (forward pass + backwards pass)\n", - " total_loss = train_step_fn(image_tensors, gt_boxes_list, gt_classes_list)\n", - "\n", - " if idx % 10 == 0:\n", - " print('batch ' + str(idx) + ' of ' + str(num_batches)\n", - " + ', loss=' + str(total_loss.numpy()), flush=True)\n", - "\n", - "print('Done fine-tuning!')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "WHlXL1x_Z3tc" - }, - "source": [ - "# Load test images and run inference with new model!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "WcE6OwrHQJya" - }, - "outputs": [], - "source": [ - "test_image_dir = 'models/research/object_detection/test_images/ducky/test/'\n", - "test_images_np = []\n", - "for i in range(1, 50):\n", - " image_path = os.path.join(test_image_dir, 'out' + str(i) + '.jpg')\n", - " test_images_np.append(np.expand_dims(\n", - " load_image_into_numpy_array(image_path), axis=0))\n", - "\n", - "# Again, uncomment this decorator if you want to run inference eagerly\n", - "@tf.function\n", - "def detect(input_tensor):\n", - " \"\"\"Run detection on an input image.\n", - "\n", - " Args:\n", - " input_tensor: A [1, height, width, 3] Tensor of type tf.float32.\n", - " Note that height and width can be anything since the image will be\n", - " immediately resized according to the needs of the model within this\n", - " function.\n", - "\n", - " Returns:\n", - " A dict containing 3 Tensors (`detection_boxes`, `detection_classes`,\n", - " and `detection_scores`).\n", - " \"\"\"\n", - " preprocessed_image, shapes = detection_model.preprocess(input_tensor)\n", - " prediction_dict = detection_model.predict(preprocessed_image, shapes)\n", - " return detection_model.postprocess(prediction_dict, shapes)\n", - "\n", - "# Note that the first frame will trigger tracing of the tf.function, which will\n", - "# take some time, after which inference should be fast.\n", - "\n", - "label_id_offset = 1\n", - "for i in range(len(test_images_np)):\n", - " input_tensor = tf.convert_to_tensor(test_images_np[i], dtype=tf.float32)\n", - " detections = detect(input_tensor)\n", - "\n", - " plot_detections(\n", - " test_images_np[i][0],\n", - " detections['detection_boxes'][0].numpy(),\n", - " detections['detection_classes'][0].numpy().astype(np.uint32)\n", - " + label_id_offset,\n", - " detections['detection_scores'][0].numpy(),\n", - " category_index, figsize=(15, 20), image_name=\"gif_frame_\" + ('%02d' % i) + \".jpg\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "RW1FrT2iNnpy" - }, - "outputs": [], - "source": [ - "imageio.plugins.freeimage.download()\n", - "\n", - "anim_file = 'duckies_test.gif'\n", - "\n", - "filenames = glob.glob('gif_frame_*.jpg')\n", - "filenames = sorted(filenames)\n", - "last = -1\n", - "images = []\n", - "for filename in filenames:\n", - " image = imageio.imread(filename)\n", - " images.append(image)\n", - "\n", - "imageio.mimsave(anim_file, images, 'GIF-FI', fps=5)\n", - "\n", - "display(IPyImage(open(anim_file, 'rb').read()))" - ] - } - ], - "metadata": { - "accelerator": "GPU", - "colab": { - "collapsed_sections": [], - "name": "interactive_eager_few_shot_od_training_colab.ipynb", - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/research/object_detection/colab_tutorials/eager_few_shot_od_training_tflite.ipynb b/research/object_detection/colab_tutorials/eager_few_shot_od_training_tflite.ipynb deleted file mode 100644 index b47d4bdb4f1..00000000000 --- a/research/object_detection/colab_tutorials/eager_few_shot_od_training_tflite.ipynb +++ /dev/null @@ -1,730 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "rOvvWAVTkMR7" - }, - "source": [ - "# Introduction\n", - "\n", - "Welcome to the **Few Shot Object Detection for TensorFlow Lite** Colab. Here, we demonstrate fine tuning of a SSD architecture (pre-trained on COCO) on very few examples of a *novel* class. We will then generate a (downloadable) TensorFlow Lite model for on-device inference.\n", - "\n", - "**NOTE:** This Colab is meant for the few-shot detection use-case. To train a model on a large dataset, please follow the [TF2 training](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_training_and_evaluation.md#training) documentation and then [convert](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_on_mobile_tf2.md) the model to TensorFlow Lite." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3U2sv0upw04O" - }, - "source": [ - "# Set Up" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "vPs64QA1Zdov" - }, - "source": [ - "## Imports" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "H0rKBV4uZacD" - }, - "outputs": [], - "source": [ - "# Support for TF2 models was added after TF 2.3.\n", - "!pip install tf-nightly" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "oi28cqGGFWnY" - }, - "outputs": [], - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "# Clone the tensorflow models repository if it doesn't already exist\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "NwdsBdGhFanc" - }, - "outputs": [], - "source": [ - "# Install the Object Detection API\n", - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=.\n", - "cp object_detection/packages/tf2/setup.py .\n", - "python -m pip install ." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "uZcqD4NLdnf4" - }, - "outputs": [], - "source": [ - "import matplotlib\n", - "import matplotlib.pyplot as plt\n", - "\n", - "import os\n", - "import random\n", - "import io\n", - "import imageio\n", - "import glob\n", - "import scipy.misc\n", - "import numpy as np\n", - "from six import BytesIO\n", - "from PIL import Image, ImageDraw, ImageFont\n", - "from IPython.display import display, Javascript\n", - "from IPython.display import Image as IPyImage\n", - "\n", - "import tensorflow as tf\n", - "\n", - "from object_detection.utils import label_map_util\n", - "from object_detection.utils import config_util\n", - "from object_detection.utils import visualization_utils as viz_utils\n", - "from object_detection.utils import colab_utils\n", - "from object_detection.utils import config_util\n", - "from object_detection.builders import model_builder\n", - "\n", - "%matplotlib inline" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "IogyryF2lFBL" - }, - "source": [ - "##Utilities" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "-y9R0Xllefec" - }, - "outputs": [], - "source": [ - "def load_image_into_numpy_array(path):\n", - " \"\"\"Load an image from file into a numpy array.\n", - "\n", - " Puts image into numpy array to feed into tensorflow graph.\n", - " Note that by convention we put it into a numpy array with shape\n", - " (height, width, channels), where channels=3 for RGB.\n", - "\n", - " Args:\n", - " path: a file path.\n", - "\n", - " Returns:\n", - " uint8 numpy array with shape (img_height, img_width, 3)\n", - " \"\"\"\n", - " img_data = tf.io.gfile.GFile(path, 'rb').read()\n", - " image = Image.open(BytesIO(img_data))\n", - " (im_width, im_height) = image.size\n", - " return np.array(image.getdata()).reshape(\n", - " (im_height, im_width, 3)).astype(np.uint8)\n", - "\n", - "def plot_detections(image_np,\n", - " boxes,\n", - " classes,\n", - " scores,\n", - " category_index,\n", - " figsize=(12, 16),\n", - " image_name=None):\n", - " \"\"\"Wrapper function to visualize detections.\n", - "\n", - " Args:\n", - " image_np: uint8 numpy array with shape (img_height, img_width, 3)\n", - " boxes: a numpy array of shape [N, 4]\n", - " classes: a numpy array of shape [N]. Note that class indices are 1-based,\n", - " and match the keys in the label map.\n", - " scores: a numpy array of shape [N] or None. If scores=None, then\n", - " this function assumes that the boxes to be plotted are groundtruth\n", - " boxes and plot all boxes as black with no classes or scores.\n", - " category_index: a dict containing category dictionaries (each holding\n", - " category index `id` and category name `name`) keyed by category indices.\n", - " figsize: size for the figure.\n", - " image_name: a name for the image file.\n", - " \"\"\"\n", - " image_np_with_annotations = image_np.copy()\n", - " viz_utils.visualize_boxes_and_labels_on_image_array(\n", - " image_np_with_annotations,\n", - " boxes,\n", - " classes,\n", - " scores,\n", - " category_index,\n", - " use_normalized_coordinates=True,\n", - " min_score_thresh=0.8)\n", - " if image_name:\n", - " plt.imsave(image_name, image_np_with_annotations)\n", - " else:\n", - " plt.imshow(image_np_with_annotations)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "sSaXL28TZfk1" - }, - "source": [ - "## Rubber Ducky data\n", - "\n", - "We will start with some toy data consisting of 5 images of a rubber\n", - "ducky. Note that the [COCO](https://cocodataset.org/#explore) dataset contains a number of animals, but notably, it does *not* contain rubber duckies (or even ducks for that matter), so this is a novel class." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "SQy3ND7EpFQM" - }, - "outputs": [], - "source": [ - "# Load images and visualize\n", - "train_image_dir = 'models/research/object_detection/test_images/ducky/train/'\n", - "train_images_np = []\n", - "for i in range(1, 6):\n", - " image_path = os.path.join(train_image_dir, 'robertducky' + str(i) + '.jpg')\n", - " train_images_np.append(load_image_into_numpy_array(image_path))\n", - "\n", - "plt.rcParams['axes.grid'] = False\n", - "plt.rcParams['xtick.labelsize'] = False\n", - "plt.rcParams['ytick.labelsize'] = False\n", - "plt.rcParams['xtick.top'] = False\n", - "plt.rcParams['xtick.bottom'] = False\n", - "plt.rcParams['ytick.left'] = False\n", - "plt.rcParams['ytick.right'] = False\n", - "plt.rcParams['figure.figsize'] = [14, 7]\n", - "\n", - "for idx, train_image_np in enumerate(train_images_np):\n", - " plt.subplot(2, 3, idx+1)\n", - " plt.imshow(train_image_np)\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "LbOe9Ym7xMGV" - }, - "source": [ - "# Transfer Learning\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Dqb_yjAo3cO_" - }, - "source": [ - "## Data Preparation\n", - "\n", - "First, we populate the groundtruth with pre-annotated bounding boxes.\n", - "\n", - "We then add the class annotations (for simplicity, we assume a single 'Duck' class in this colab; though it should be straightforward to extend this to handle multiple classes). We also convert everything to the format that the training\n", - "loop below expects (e.g., everything converted to tensors, classes converted to one-hot representations, etc.)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "wIAT6ZUmdHOC" - }, - "outputs": [], - "source": [ - "gt_boxes = [\n", - " np.array([[0.436, 0.591, 0.629, 0.712]], dtype=np.float32),\n", - " np.array([[0.539, 0.583, 0.73, 0.71]], dtype=np.float32),\n", - " np.array([[0.464, 0.414, 0.626, 0.548]], dtype=np.float32),\n", - " np.array([[0.313, 0.308, 0.648, 0.526]], dtype=np.float32),\n", - " np.array([[0.256, 0.444, 0.484, 0.629]], dtype=np.float32)\n", - "]\n", - "\n", - "# By convention, our non-background classes start counting at 1. Given\n", - "# that we will be predicting just one class, we will therefore assign it a\n", - "# `class id` of 1.\n", - "duck_class_id = 1\n", - "num_classes = 1\n", - "\n", - "category_index = {duck_class_id: {'id': duck_class_id, 'name': 'rubber_ducky'}}\n", - "\n", - "# Convert class labels to one-hot; convert everything to tensors.\n", - "# The `label_id_offset` here shifts all classes by a certain number of indices;\n", - "# we do this here so that the model receives one-hot labels where non-background\n", - "# classes start counting at the zeroth index. This is ordinarily just handled\n", - "# automatically in our training binaries, but we need to reproduce it here.\n", - "label_id_offset = 1\n", - "train_image_tensors = []\n", - "gt_classes_one_hot_tensors = []\n", - "gt_box_tensors = []\n", - "for (train_image_np, gt_box_np) in zip(\n", - " train_images_np, gt_boxes):\n", - " train_image_tensors.append(tf.expand_dims(tf.convert_to_tensor(\n", - " train_image_np, dtype=tf.float32), axis=0))\n", - " gt_box_tensors.append(tf.convert_to_tensor(gt_box_np, dtype=tf.float32))\n", - " zero_indexed_groundtruth_classes = tf.convert_to_tensor(\n", - " np.ones(shape=[gt_box_np.shape[0]], dtype=np.int32) - label_id_offset)\n", - " gt_classes_one_hot_tensors.append(tf.one_hot(\n", - " zero_indexed_groundtruth_classes, num_classes))\n", - "print('Done prepping data.')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "b3_Z3mJWN9KJ" - }, - "source": [ - "Let's just visualize the rubber duckies as a sanity check\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "YBD6l-E4N71y" - }, - "outputs": [], - "source": [ - "dummy_scores = np.array([1.0], dtype=np.float32) # give boxes a score of 100%\n", - "\n", - "plt.figure(figsize=(30, 15))\n", - "for idx in range(5):\n", - " plt.subplot(2, 3, idx+1)\n", - " plot_detections(\n", - " train_images_np[idx],\n", - " gt_boxes[idx],\n", - " np.ones(shape=[gt_boxes[idx].shape[0]], dtype=np.int32),\n", - " dummy_scores, category_index)\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ghDAsqfoZvPh" - }, - "source": [ - "## Load mobile-friendly model\n", - "\n", - "In this cell we build a mobile-friendly single-stage detection architecture (SSD MobileNet V2 FPN-Lite) and restore all but the classification layer at the top (which will be randomly initialized).\n", - "\n", - "**NOTE**: TensorFlow Lite only supports SSD models for now.\n", - "\n", - "For simplicity, we have hardcoded a number of things in this colab for the specific SSD architecture at hand (including assuming that the image size will always be 320x320), however it is not difficult to generalize to other model configurations (`pipeline.config` in the zip downloaded from the [Model Zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.)).\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "9J16r3NChD-7" - }, - "outputs": [], - "source": [ - "# Download the checkpoint and put it into models/research/object_detection/test_data/\n", - "\n", - "!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz\n", - "!tar -xf ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz\n", - "!if [ -d \"models/research/object_detection/test_data/checkpoint\" ]; then rm -Rf models/research/object_detection/test_data/checkpoint; fi\n", - "!mkdir models/research/object_detection/test_data/checkpoint\n", - "!mv ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/checkpoint models/research/object_detection/test_data/" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "RyT4BUbaMeG-" - }, - "outputs": [], - "source": [ - "tf.keras.backend.clear_session()\n", - "\n", - "print('Building model and restoring weights for fine-tuning...', flush=True)\n", - "num_classes = 1\n", - "pipeline_config = 'models/research/object_detection/configs/tf2/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config'\n", - "checkpoint_path = 'models/research/object_detection/test_data/checkpoint/ckpt-0'\n", - "\n", - "# This will be where we save checkpoint \u0026 config for TFLite conversion later.\n", - "output_directory = 'output/'\n", - "output_checkpoint_dir = os.path.join(output_directory, 'checkpoint')\n", - "\n", - "# Load pipeline config and build a detection model.\n", - "#\n", - "# Since we are working off of a COCO architecture which predicts 90\n", - "# class slots by default, we override the `num_classes` field here to be just\n", - "# one (for our new rubber ducky class).\n", - "configs = config_util.get_configs_from_pipeline_file(pipeline_config)\n", - "model_config = configs['model']\n", - "model_config.ssd.num_classes = num_classes\n", - "model_config.ssd.freeze_batchnorm = True\n", - "detection_model = model_builder.build(\n", - " model_config=model_config, is_training=True)\n", - "# Save new pipeline config\n", - "pipeline_proto = config_util.create_pipeline_proto_from_configs(configs)\n", - "config_util.save_pipeline_config(pipeline_proto, output_directory)\n", - "\n", - "# Set up object-based checkpoint restore --- SSD has two prediction\n", - "# `heads` --- one for classification, the other for box regression. We will\n", - "# restore the box regression head but initialize the classification head\n", - "# from scratch (we show the omission below by commenting out the line that\n", - "# we would add if we wanted to restore both heads)\n", - "fake_box_predictor = tf.compat.v2.train.Checkpoint(\n", - " _base_tower_layers_for_heads=detection_model._box_predictor._base_tower_layers_for_heads,\n", - " # _prediction_heads=detection_model._box_predictor._prediction_heads,\n", - " # (i.e., the classification head that we *will not* restore)\n", - " _box_prediction_head=detection_model._box_predictor._box_prediction_head,\n", - " )\n", - "fake_model = tf.compat.v2.train.Checkpoint(\n", - " _feature_extractor=detection_model._feature_extractor,\n", - " _box_predictor=fake_box_predictor)\n", - "ckpt = tf.compat.v2.train.Checkpoint(model=fake_model)\n", - "ckpt.restore(checkpoint_path).expect_partial()\n", - "\n", - "# To save checkpoint for TFLite conversion.\n", - "exported_ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)\n", - "ckpt_manager = tf.train.CheckpointManager(\n", - " exported_ckpt, output_checkpoint_dir, max_to_keep=1)\n", - "\n", - "# Run model through a dummy image so that variables are created\n", - "image, shapes = detection_model.preprocess(tf.zeros([1, 320, 320, 3]))\n", - "prediction_dict = detection_model.predict(image, shapes)\n", - "_ = detection_model.postprocess(prediction_dict, shapes)\n", - "print('Weights restored!')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "pCkWmdoZZ0zJ" - }, - "source": [ - "## Eager training loop (Fine-tuning)\n", - "\n", - "Some of the parameters in this block have been set empirically: for example, `learning_rate`, `num_batches` \u0026 `momentum` for SGD. These are just a starting point, you will have to tune these for your data \u0026 model architecture to get the best results.\n", - "\n", - "\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "nyHoF4mUrv5-" - }, - "outputs": [], - "source": [ - "tf.keras.backend.set_learning_phase(True)\n", - "\n", - "# These parameters can be tuned; since our training set has 5 images\n", - "# it doesn't make sense to have a much larger batch size, though we could\n", - "# fit more examples in memory if we wanted to.\n", - "batch_size = 5\n", - "learning_rate = 0.15\n", - "num_batches = 1000\n", - "\n", - "# Select variables in top layers to fine-tune.\n", - "trainable_variables = detection_model.trainable_variables\n", - "to_fine_tune = []\n", - "prefixes_to_train = [\n", - " 'WeightSharedConvolutionalBoxPredictor/WeightSharedConvolutionalBoxHead',\n", - " 'WeightSharedConvolutionalBoxPredictor/WeightSharedConvolutionalClassHead']\n", - "for var in trainable_variables:\n", - " if any([var.name.startswith(prefix) for prefix in prefixes_to_train]):\n", - " to_fine_tune.append(var)\n", - "\n", - "# Set up forward + backward pass for a single train step.\n", - "def get_model_train_step_function(model, optimizer, vars_to_fine_tune):\n", - " \"\"\"Get a tf.function for training step.\"\"\"\n", - "\n", - " # Use tf.function for a bit of speed.\n", - " # Comment out the tf.function decorator if you want the inside of the\n", - " # function to run eagerly.\n", - " @tf.function\n", - " def train_step_fn(image_tensors,\n", - " groundtruth_boxes_list,\n", - " groundtruth_classes_list):\n", - " \"\"\"A single training iteration.\n", - "\n", - " Args:\n", - " image_tensors: A list of [1, height, width, 3] Tensor of type tf.float32.\n", - " Note that the height and width can vary across images, as they are\n", - " reshaped within this function to be 320x320.\n", - " groundtruth_boxes_list: A list of Tensors of shape [N_i, 4] with type\n", - " tf.float32 representing groundtruth boxes for each image in the batch.\n", - " groundtruth_classes_list: A list of Tensors of shape [N_i, num_classes]\n", - " with type tf.float32 representing groundtruth boxes for each image in\n", - " the batch.\n", - "\n", - " Returns:\n", - " A scalar tensor representing the total loss for the input batch.\n", - " \"\"\"\n", - " shapes = tf.constant(batch_size * [[320, 320, 3]], dtype=tf.int32)\n", - " model.provide_groundtruth(\n", - " groundtruth_boxes_list=groundtruth_boxes_list,\n", - " groundtruth_classes_list=groundtruth_classes_list)\n", - " with tf.GradientTape() as tape:\n", - " preprocessed_images = tf.concat(\n", - " [detection_model.preprocess(image_tensor)[0]\n", - " for image_tensor in image_tensors], axis=0)\n", - " prediction_dict = model.predict(preprocessed_images, shapes)\n", - " losses_dict = model.loss(prediction_dict, shapes)\n", - " total_loss = losses_dict['Loss/localization_loss'] + losses_dict['Loss/classification_loss']\n", - " gradients = tape.gradient(total_loss, vars_to_fine_tune)\n", - " optimizer.apply_gradients(zip(gradients, vars_to_fine_tune))\n", - " return total_loss\n", - "\n", - " return train_step_fn\n", - "\n", - "optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate, momentum=0.9)\n", - "train_step_fn = get_model_train_step_function(\n", - " detection_model, optimizer, to_fine_tune)\n", - "\n", - "print('Start fine-tuning!', flush=True)\n", - "for idx in range(num_batches):\n", - " # Grab keys for a random subset of examples\n", - " all_keys = list(range(len(train_images_np)))\n", - " random.shuffle(all_keys)\n", - " example_keys = all_keys[:batch_size]\n", - "\n", - " # Note that we do not do data augmentation in this demo. If you want a\n", - " # a fun exercise, we recommend experimenting with random horizontal flipping\n", - " # and random cropping :)\n", - " gt_boxes_list = [gt_box_tensors[key] for key in example_keys]\n", - " gt_classes_list = [gt_classes_one_hot_tensors[key] for key in example_keys]\n", - " image_tensors = [train_image_tensors[key] for key in example_keys]\n", - "\n", - " # Training step (forward pass + backwards pass)\n", - " total_loss = train_step_fn(image_tensors, gt_boxes_list, gt_classes_list)\n", - "\n", - " if idx % 100 == 0:\n", - " print('batch ' + str(idx) + ' of ' + str(num_batches)\n", - " + ', loss=' + str(total_loss.numpy()), flush=True)\n", - "\n", - "print('Done fine-tuning!')\n", - "\n", - "ckpt_manager.save()\n", - "print('Checkpoint saved!')" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cYk1_9Fc2lZO" - }, - "source": [ - "# Export \u0026 run with TensorFlow Lite\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "y0nsDVEd9SuX" - }, - "source": [ - "## Model Conversion\n", - "\n", - "First, we invoke the `export_tflite_graph_tf2.py` script to generate a TFLite-friendly intermediate SavedModel. This will then be passed to the TensorFlow Lite Converter for generating the final model.\n", - "\n", - "To know more about this process, please look at [this documentation](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/running_on_mobile_tf2.md)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "dyrqHSQQ7WKE" - }, - "outputs": [], - "source": [ - "%%bash\n", - "python models/research/object_detection/export_tflite_graph_tf2.py \\\n", - " --pipeline_config_path output/pipeline.config \\\n", - " --trained_checkpoint_dir output/checkpoint \\\n", - " --output_directory tflite" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "m5hjPyR78bgs" - }, - "outputs": [], - "source": [ - "!tflite_convert --saved_model_dir=tflite/saved_model --output_file=tflite/model.tflite" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "WHlXL1x_Z3tc" - }, - "source": [ - "## Test .tflite model" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "WcE6OwrHQJya" - }, - "outputs": [], - "source": [ - "test_image_dir = 'models/research/object_detection/test_images/ducky/test/'\n", - "test_images_np = []\n", - "for i in range(1, 50):\n", - " image_path = os.path.join(test_image_dir, 'out' + str(i) + '.jpg')\n", - " test_images_np.append(np.expand_dims(\n", - " load_image_into_numpy_array(image_path), axis=0))\n", - "\n", - "# Again, uncomment this decorator if you want to run inference eagerly\n", - "def detect(interpreter, input_tensor):\n", - " \"\"\"Run detection on an input image.\n", - "\n", - " Args:\n", - " interpreter: tf.lite.Interpreter\n", - " input_tensor: A [1, height, width, 3] Tensor of type tf.float32.\n", - " Note that height and width can be anything since the image will be\n", - " immediately resized according to the needs of the model within this\n", - " function.\n", - "\n", - " Returns:\n", - " A dict containing 3 Tensors (`detection_boxes`, `detection_classes`,\n", - " and `detection_scores`).\n", - " \"\"\"\n", - " input_details = interpreter.get_input_details()\n", - " output_details = interpreter.get_output_details()\n", - "\n", - " # We use the original model for pre-processing, since the TFLite model doesn't\n", - " # include pre-processing.\n", - " preprocessed_image, shapes = detection_model.preprocess(input_tensor)\n", - " interpreter.set_tensor(input_details[0]['index'], preprocessed_image.numpy())\n", - "\n", - " interpreter.invoke()\n", - "\n", - " boxes = interpreter.get_tensor(output_details[0]['index'])\n", - " classes = interpreter.get_tensor(output_details[1]['index'])\n", - " scores = interpreter.get_tensor(output_details[2]['index'])\n", - " return boxes, classes, scores\n", - "\n", - "# Load the TFLite model and allocate tensors.\n", - "interpreter = tf.lite.Interpreter(model_path=\"tflite/model.tflite\")\n", - "interpreter.allocate_tensors()\n", - "\n", - "# Note that the first frame will trigger tracing of the tf.function, which will\n", - "# take some time, after which inference should be fast.\n", - "\n", - "label_id_offset = 1\n", - "for i in range(len(test_images_np)):\n", - " input_tensor = tf.convert_to_tensor(test_images_np[i], dtype=tf.float32)\n", - " boxes, classes, scores = detect(interpreter, input_tensor)\n", - "\n", - " plot_detections(\n", - " test_images_np[i][0],\n", - " boxes[0],\n", - " classes[0].astype(np.uint32) + label_id_offset,\n", - " scores[0],\n", - " category_index, figsize=(15, 20), image_name=\"gif_frame_\" + ('%02d' % i) + \".jpg\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ZkMPOSQE0x8C" - }, - "outputs": [], - "source": [ - "imageio.plugins.freeimage.download()\n", - "\n", - "anim_file = 'duckies_test.gif'\n", - "\n", - "filenames = glob.glob('gif_frame_*.jpg')\n", - "filenames = sorted(filenames)\n", - "last = -1\n", - "images = []\n", - "for filename in filenames:\n", - " image = imageio.imread(filename)\n", - " images.append(image)\n", - "\n", - "imageio.mimsave(anim_file, images, 'GIF-FI', fps=5)\n", - "\n", - "display(IPyImage(open(anim_file, 'rb').read()))" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "yzaHWsS58_PQ" - }, - "source": [ - "## (Optional) Download model\n", - "\n", - "This model can be run on-device with **TensorFlow Lite**. Look at [our SSD model signature](https://www.tensorflow.org/lite/models/object_detection/overview#uses_and_limitations) to understand how to interpret the model IO tensors. Our [Object Detection example](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection) is a good starting point for integrating the model into your mobile app.\n", - "\n", - "Refer to TFLite's [inference documentation](https://www.tensorflow.org/lite/guide/inference) for more details." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "gZ6vac3RAY3j" - }, - "outputs": [], - "source": [ - "from google.colab import files\n", - "files.download('tflite/model.tflite') " - ] - } - ], - "metadata": { - "accelerator": "GPU", - "colab": { - "collapsed_sections": [], - "name": "eager_few_shot_od_training_tflite.ipynb", - "provenance": [], - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/research/object_detection/colab_tutorials/generate_ssd_anchor_box_aspect_ratios_using_k_means_clustering.ipynb b/research/object_detection/colab_tutorials/generate_ssd_anchor_box_aspect_ratios_using_k_means_clustering.ipynb deleted file mode 100644 index 529e92c346c..00000000000 --- a/research/object_detection/colab_tutorials/generate_ssd_anchor_box_aspect_ratios_using_k_means_clustering.ipynb +++ /dev/null @@ -1,465 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "qENhcLrkK9hX" - }, - "source": [ - "# Generate SSD anchor box aspect ratios using k-means clustering\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "KD164da8WQ0U" - }, - "source": [ - "Many object detection models use anchor boxes as a region-sampling strategy, so that during training, the model learns to match one of several pre-defined anchor boxes to the ground truth bounding boxes. To optimize the accuracy and efficiency of your object detection model, it's helpful if you tune these anchor boxes to fit your model dataset, because the configuration files that comes with TensorFlow's trained checkpoints include aspect ratios that are intended to cover a very broad set of objects.\n", - "\n", - "So in this notebook tutorial, you'll learn how to discover a set of aspect ratios that are custom-fit for your dataset, as discovered through k-means clustering of all the ground-truth bounding-box ratios.\n", - "\n", - "For demonstration purpsoses, we're using a subset of the [PETS dataset](https://www.robots.ox.ac.uk/~vgg/data/pets/) (cats and dogs), which matches some other model training tutorials out there (such as [this one for the Edge TPU](https://colab.sandbox.google.com/github/google-coral/tutorials/blob/master/retrain_ssdlite_mobiledet_qat_tf1.ipynb#scrollTo=LvEMJSafnyEC)), but you can use this script with a different dataset, and we'll show how to tune it to meet your model's goals, including how to optimize speed over accuracy or accuracy over speed.\n", - "\n", - "The result of this notebook is a new [pipeline `.config` file](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/configuring_jobs.md) that you can copy into your model training script. With the new customized anchor box configuration, you should observe a faster training pipeline and slightly improved model accuracy.\n", - "\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cNBjMwIvCrhf" - }, - "source": [ - "## Get the required libraries" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "hCQlBGJkZTR2" - }, - "outputs": [], - "source": [ - "import tensorflow as tf" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "aw-Ba-5RUhMs" - }, - "outputs": [], - "source": [ - "# Install the tensorflow Object Detection API...\n", - "# If you're running this offline, you also might need to install the protobuf-compiler:\n", - "# apt-get install protobuf-compiler\n", - "\n", - "! git clone -n https://github.com/tensorflow/models.git\n", - "%cd models\n", - "!git checkout 461b3587ef38b42cda151fa3b7d37706d77e4244\n", - "%cd research\n", - "! protoc object_detection/protos/*.proto --python_out=.\n", - "\n", - "# Install TensorFlow Object Detection API\n", - "%cp object_detection/packages/tf2/setup.py .\n", - "! python -m pip install --upgrade pip\n", - "! python -m pip install --use-feature=2020-resolver .\n", - "\n", - "# Test the installation\n", - "! python object_detection/builders/model_builder_tf2_test.py" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "InjvvtaMECr9" - }, - "source": [ - "## Prepare the dataset" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "T62-oddjEH8r" - }, - "source": [ - "Although this notebook does not perform model training, you need to use the same dataset here that you'll use when training the model.\n", - "\n", - "To find the best anchor box ratios, you should use all of your training dataset (or as much of it as is reasonable). That's because, as mentioned in the introduction, you want to measure the precise variety of images that you expect your model to encounter—anything less and the anchor boxes might not cover the variety of objects you model encounters, so it might have weak accuracy. (Whereas the alternative, in which the ratios are based on data that is beyond the scope of your model's application, usually creates an inefficient model that can also have weaker accuracy.)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "sKYfhq7CKZ4B" - }, - "outputs": [], - "source": [ - "%mkdir /content/dataset\n", - "%cd /content/dataset\n", - "! wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz\n", - "! wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz\n", - "! tar zxf images.tar.gz\n", - "! tar zxf annotations.tar.gz\n", - "\n", - "XML_PATH = '/content/dataset/annotations/xmls'" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "44vtL0nsAqXg" - }, - "source": [ - "Because the following k-means script will process all XML annotations, we want to reduce the PETS dataset to include only the cats and dogs used to train the model (in [this training notebook](https://colab.sandbox.google.com/github/google-coral/tutorials/blob/master/retrain_ssdlite_mobiledet_qat_tf1.ipynb)). So we delete all annotation files that are **not** Abyssinian or American bulldog:\n", - "\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ih48zFbl6jM7" - }, - "outputs": [], - "source": [ - "! (cd /content/dataset/annotations/xmls/ \u0026\u0026 \\\n", - " find . ! \\( -name 'Abyssinian*' -o -name 'american_bulldog*' \\) -type f -exec rm -f {} \\; )" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "KG8uraCK-RSM" - }, - "source": [ - "### Upload your own dataset" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "m0bh_iKD-Xz4" - }, - "source": [ - "To generate the anchor box ratios for your own dataset, upload a ZIP file with your annotation files (click the **Files** tab on the left, and drag-drop your ZIP file there), and then uncomment the following code to unzip it and specify the path to the directory with your annotation files:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "M0j_vWDR3WkK" - }, - "outputs": [], - "source": [ - "# %cd /content/\n", - "# !unzip dataset.zip\n", - "\n", - "# XML_PATH = '/content/dataset/annotations/xmls'" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Cs_71ZXMOctb" - }, - "source": [ - "## Find the aspect ratios using k-means" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "R3k5WrMYHPyL" - }, - "source": [ - "We are trying to find a group of aspect ratios that overlap the majority of object shapes in the dataset. We do that by finding common clusters of bounding boxes of the dataset, using the k-means clustering algorithm to find centroids of these clusters.\n", - "\n", - "To help with this, we need to calculate following:\n", - "\n", - "+ The k-means cluster centroids of the given bounding boxes\n", - "(see the `kmeans_aspect_ratios()` function below).\n", - "\n", - "+ The average intersection of bounding boxes with given aspect ratios.\n", - "(see the `average_iou()` function below).\n", - "This does not affect the outcome of the final box ratios, but serves as a useful metric for you to decide whether the selected boxes are effective and whether you want to try with more/fewer aspect ratios. (We'll discuss this score more below.)\n", - "\n", - "**NOTE:**\n", - "The term \"centroid\" used here refers to the center of the k-means cluster (the boxes (height,width) vector)." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "vCB8Dfs0Xlyv" - }, - "outputs": [], - "source": [ - "import sys\n", - "import os\n", - "import numpy as np\n", - "import xml.etree.ElementTree as ET\n", - "\n", - "from sklearn.cluster import KMeans\n", - "\n", - "def xml_to_boxes(path, rescale_width=None, rescale_height=None):\n", - " \"\"\"Extracts bounding-box widths and heights from ground-truth dataset.\n", - "\n", - " Args:\n", - " path : Path to .xml annotation files for your dataset.\n", - " rescale_width : Scaling factor to rescale width of bounding box.\n", - " rescale_height : Scaling factor to rescale height of bounding box.\n", - "\n", - " Returns:\n", - " bboxes : A numpy array with pairs of box dimensions as [width, height].\n", - " \"\"\"\n", - "\n", - " xml_list = []\n", - " filenames = os.listdir(os.path.join(path))\n", - " filenames = [os.path.join(path, f) for f in filenames if (f.endswith('.xml'))]\n", - " for xml_file in filenames:\n", - " tree = ET.parse(xml_file)\n", - " root = tree.getroot()\n", - " for member in root.findall('object'):\n", - " bndbox = member.find('bndbox')\n", - " bbox_width = int(bndbox.find('xmax').text) - int(bndbox.find('xmin').text)\n", - " bbox_height = int(bndbox.find('ymax').text) - int(bndbox.find('ymin').text)\n", - " if rescale_width and rescale_height:\n", - " size = root.find('size')\n", - " bbox_width = bbox_width * (rescale_width / int(size.find('width').text))\n", - " bbox_height = bbox_height * (rescale_height / int(size.find('height').text))\n", - " xml_list.append([bbox_width, bbox_height])\n", - " bboxes = np.array(xml_list)\n", - " return bboxes\n", - "\n", - "\n", - "def average_iou(bboxes, anchors):\n", - " \"\"\"Calculates the Intersection over Union (IoU) between bounding boxes and\n", - " anchors.\n", - "\n", - " Args:\n", - " bboxes : Array of bounding boxes in [width, height] format.\n", - " anchors : Array of aspect ratios [n, 2] format.\n", - "\n", - " Returns:\n", - " avg_iou_perc : A Float value, average of IOU scores from each aspect ratio\n", - " \"\"\"\n", - " intersection_width = np.minimum(anchors[:, [0]], bboxes[:, 0]).T\n", - " intersection_height = np.minimum(anchors[:, [1]], bboxes[:, 1]).T\n", - "\n", - " if np.any(intersection_width == 0) or np.any(intersection_height == 0):\n", - " raise ValueError(\"Some boxes have zero size.\")\n", - "\n", - " intersection_area = intersection_width * intersection_height\n", - " boxes_area = np.prod(bboxes, axis=1, keepdims=True)\n", - " anchors_area = np.prod(anchors, axis=1, keepdims=True).T\n", - " union_area = boxes_area + anchors_area - intersection_area\n", - " avg_iou_perc = np.mean(np.max(intersection_area / union_area, axis=1)) * 100\n", - "\n", - " return avg_iou_perc\n", - "\n", - "def kmeans_aspect_ratios(bboxes, kmeans_max_iter, num_aspect_ratios):\n", - " \"\"\"Calculate the centroid of bounding boxes clusters using Kmeans algorithm.\n", - "\n", - " Args:\n", - " bboxes : Array of bounding boxes in [width, height] format.\n", - " kmeans_max_iter : Maximum number of iterations to find centroids.\n", - " num_aspect_ratios : Number of centroids to optimize kmeans.\n", - "\n", - " Returns:\n", - " aspect_ratios : Centroids of cluster (optmised for dataset).\n", - " avg_iou_prec : Average score of bboxes intersecting with new aspect ratios.\n", - " \"\"\"\n", - "\n", - " assert len(bboxes), \"You must provide bounding boxes\"\n", - "\n", - " normalized_bboxes = bboxes / np.sqrt(bboxes.prod(axis=1, keepdims=True))\n", - " \n", - " # Using kmeans to find centroids of the width/height clusters\n", - " kmeans = KMeans(\n", - " init='random', n_clusters=num_aspect_ratios, random_state=0, max_iter=kmeans_max_iter)\n", - " kmeans.fit(X=normalized_bboxes)\n", - " ar = kmeans.cluster_centers_\n", - "\n", - " assert len(ar), \"Unable to find k-means centroid, try increasing kmeans_max_iter.\"\n", - "\n", - " avg_iou_perc = average_iou(normalized_bboxes, ar)\n", - "\n", - " if not np.isfinite(avg_iou_perc):\n", - " sys.exit(\"Failed to get aspect ratios due to numerical errors in k-means\")\n", - "\n", - " aspect_ratios = [w/h for w,h in ar]\n", - "\n", - " return aspect_ratios, avg_iou_perc" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "eU2SuLvu55Ds" - }, - "source": [ - "In the next code block, we'll call the above functions to discover the ideal anchor box aspect ratios.\n", - "\n", - "You can tune the parameters below to suit your performance objectives.\n", - "\n", - "Most importantly, you should consider the number of aspect ratios you want to generate. At opposite ends of the decision spectrum, there are two objectives you might seek:\n", - "\n", - "1. **Low accuracy and fast inference**: Try 2-3 aspect ratios. \n", - " * This is if your application is okay with accuracy or confidence scores around/below 80%.\n", - " * The average IOU score (from `avg_iou_perc`) will be around 70-85.\n", - " * This reduces the model's overall computations during inference, which makes inference faster.\n", - "\n", - "2. **High accuracy and slow inference**: Try 5-6 aspect ratios.\n", - " * This is if your application requires accuracy or confidence scores around 95%.\n", - " * The average IOU score (from `avg_iou_perc`) should be over 95.\n", - " * This increases the model's overall computations during inference, which makes inference slower.\n", - "\n", - "The initial configuration below aims somewhere in between: it searches for 4 aspect ratios.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "cNw-vX3nfl1g" - }, - "outputs": [], - "source": [ - "# Tune this based on your accuracy/speed goals as described above\n", - "num_aspect_ratios = 4 # can be [2,3,4,5,6]\n", - "\n", - "# Tune the iterations based on the size and distribution of your dataset\n", - "# You can check avg_iou_prec every 100 iterations to see how centroids converge\n", - "kmeans_max_iter = 500\n", - "\n", - "# These should match the training pipeline config ('fixed_shape_resizer' param)\n", - "width = 320\n", - "height = 320\n", - "\n", - "# Get the ground-truth bounding boxes for our dataset\n", - "bboxes = xml_to_boxes(path=XML_PATH, rescale_width=width, rescale_height=height)\n", - "\n", - "aspect_ratios, avg_iou_perc = kmeans_aspect_ratios(\n", - " bboxes=bboxes,\n", - " kmeans_max_iter=kmeans_max_iter,\n", - " num_aspect_ratios=num_aspect_ratios)\n", - "\n", - "aspect_ratios = sorted(aspect_ratios)\n", - "\n", - "print('Aspect ratios generated:', [round(ar,2) for ar in aspect_ratios])\n", - "print('Average IOU with anchors:', avg_iou_perc)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0xHqOpuxgmD0" - }, - "source": [ - "## Generate a new pipeline config file" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "ZB6jqVT6gpmT" - }, - "source": [ - "That's it. Now we just need the `.config` file your model started with, and we'll merge the new `ssd_anchor_generator` properties into it." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "AlMffd3rgKW2" - }, - "outputs": [], - "source": [ - "import tensorflow as tf\n", - "from google.protobuf import text_format\n", - "from object_detection.protos import pipeline_pb2\n", - "\n", - "pipeline = pipeline_pb2.TrainEvalPipelineConfig()\n", - "config_path = '/content/models/research/object_detection/samples/configs/ssdlite_mobiledet_edgetpu_320x320_coco_sync_4x4.config'\n", - "pipeline_save = '/content/ssdlite_mobiledet_edgetpu_320x320_custom_aspect_ratios.config'\n", - "with tf.io.gfile.GFile(config_path, \"r\") as f:\n", - " proto_str = f.read()\n", - " text_format.Merge(proto_str, pipeline)\n", - "pipeline.model.ssd.num_classes = 2\n", - "while pipeline.model.ssd.anchor_generator.ssd_anchor_generator.aspect_ratios:\n", - " pipeline.model.ssd.anchor_generator.ssd_anchor_generator.aspect_ratios.pop()\n", - "\n", - "for i in range(len(aspect_ratios)):\n", - " pipeline.model.ssd.anchor_generator.ssd_anchor_generator.aspect_ratios.append(aspect_ratios[i])\n", - "\n", - "config_text = text_format.MessageToString(pipeline)\n", - "with tf.io.gfile.GFile(pipeline_save, \"wb\") as f:\n", - " f.write(config_text)\n", - "# Check for updated aspect ratios in the config\n", - "!cat /content/ssdlite_mobiledet_edgetpu_320x320_custom_aspect_ratios.config" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3kzWdu7ai1om" - }, - "source": [ - "## Summary and next steps" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "FltDhShbi06h" - }, - "source": [ - "If you look at the new `.config` file printed above, you'll find the `anchor_generator` specification, which includes the new `aspect_ratio` values that we generated with the k-means code above.\n", - "\n", - "The original config file ([`ssdlite_mobiledet_edgetpu_320x320_coco_sync_4x4.config`](https://github.com/tensorflow/models/blob/master/research/object_detection/samples/configs/ssd_mobilenet_v1_pets.config)) did have some default anchor box aspect ratios already, but we've replaced those with values that are optimized for our dataset. These new anchor boxes should improve the model accuracy (compared to the default anchors) and speed up the training process.\n", - "\n", - "If you want to use this configuration to train a model, then check out this tutorial to [retrain MobileDet for the Coral Edge TPU](https://colab.sandbox.google.com/github/google-coral/tutorials/blob/master/retrain_ssdlite_mobiledet_qat_tf1.ipynb), which uses this exact cats/dogs dataset. Just copy the `.config` file printed above and add it to that training notebook. (Or download the file from the **Files** panel on the left side of the Colab UI: it's called `ssdlite_mobiledet_edgetpu_320x320_custom_aspect_ratios.config`.)\n", - "\n", - "For more information about the pipeline configuration file, read [Configuring the Object Detection Training Pipeline](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/configuring_jobs.md).\n", - "\n", - "### About anchor scales...\n", - "\n", - "This notebook is focused on anchor box aspect ratios because that's often the most difficult to tune for each dataset. But you should also consider different configurations for the anchor box scales, which specify the number of different anchor box sizes and their min/max sizes—which affects how well your model detects objects of varying sizes.\n", - "\n", - "Tuning the anchor scales is much easier to do by hand, by estimating the min/max sizes you expect the model to encounter in your application environment. Just like when choosing the number of aspect ratios above, the number of different box sizes also affects your model accuracy and speed (using more box scales is more accurate, but also slower).\n", - "\n", - "You can also read more about anchor scales in [Configuring the Object Detection Training Pipeline](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/configuring_jobs.md).\n", - "\n" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "Generate_SSD_anchor_box_aspect_ratios_using_k_means_clustering.ipynb", - "provenance": [], - "toc_visible": true - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - }, - "language_info": { - "name": "python" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/research/object_detection/colab_tutorials/inference_from_saved_model_tf2_colab.ipynb b/research/object_detection/colab_tutorials/inference_from_saved_model_tf2_colab.ipynb deleted file mode 100644 index 1e88f4c5d52..00000000000 --- a/research/object_detection/colab_tutorials/inference_from_saved_model_tf2_colab.ipynb +++ /dev/null @@ -1,313 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "inference_from_saved_model_tf2_colab.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "cT5cdSLPX0ui" - }, - "source": [ - "# Intro to Object Detection Colab\n", - "\n", - "Welcome to the object detection colab! This demo will take you through the steps of running an \"out-of-the-box\" detection model in SavedModel format on a collection of images.\n", - "\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "vPs64QA1Zdov" - }, - "source": [ - "Imports" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "OBzb04bdNGM8", - "colab_type": "code", - "colab": {} - }, - "source": [ - "!pip install -U --pre tensorflow==\"2.2.0\"" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "NgSXyvKSNHIl", - "colab_type": "code", - "colab": {} - }, - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "# Clone the tensorflow models repository if it doesn't already exist\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "rhpPgW7TNLs6", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Install the Object Detection API\n", - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=.\n", - "cp object_detection/packages/tf2/setup.py .\n", - "python -m pip install ." - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "yn5_uV1HLvaz", - "colab": {} - }, - "source": [ - "import io\n", - "import os\n", - "import scipy.misc\n", - "import numpy as np\n", - "import six\n", - "import time\n", - "\n", - "from six import BytesIO\n", - "\n", - "import matplotlib\n", - "import matplotlib.pyplot as plt\n", - "from PIL import Image, ImageDraw, ImageFont\n", - "\n", - "import tensorflow as tf\n", - "from object_detection.utils import visualization_utils as viz_utils\n", - "\n", - "%matplotlib inline" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "-y9R0Xllefec", - "colab": {} - }, - "source": [ - "def load_image_into_numpy_array(path):\n", - " \"\"\"Load an image from file into a numpy array.\n", - "\n", - " Puts image into numpy array to feed into tensorflow graph.\n", - " Note that by convention we put it into a numpy array with shape\n", - " (height, width, channels), where channels=3 for RGB.\n", - "\n", - " Args:\n", - " path: a file path (this can be local or on colossus)\n", - "\n", - " Returns:\n", - " uint8 numpy array with shape (img_height, img_width, 3)\n", - " \"\"\"\n", - " img_data = tf.io.gfile.GFile(path, 'rb').read()\n", - " image = Image.open(BytesIO(img_data))\n", - " (im_width, im_height) = image.size\n", - " return np.array(image.getdata()).reshape(\n", - " (im_height, im_width, 3)).astype(np.uint8)\n", - "\n", - "# Load the COCO Label Map\n", - "category_index = {\n", - " 1: {'id': 1, 'name': 'person'},\n", - " 2: {'id': 2, 'name': 'bicycle'},\n", - " 3: {'id': 3, 'name': 'car'},\n", - " 4: {'id': 4, 'name': 'motorcycle'},\n", - " 5: {'id': 5, 'name': 'airplane'},\n", - " 6: {'id': 6, 'name': 'bus'},\n", - " 7: {'id': 7, 'name': 'train'},\n", - " 8: {'id': 8, 'name': 'truck'},\n", - " 9: {'id': 9, 'name': 'boat'},\n", - " 10: {'id': 10, 'name': 'traffic light'},\n", - " 11: {'id': 11, 'name': 'fire hydrant'},\n", - " 13: {'id': 13, 'name': 'stop sign'},\n", - " 14: {'id': 14, 'name': 'parking meter'},\n", - " 15: {'id': 15, 'name': 'bench'},\n", - " 16: {'id': 16, 'name': 'bird'},\n", - " 17: {'id': 17, 'name': 'cat'},\n", - " 18: {'id': 18, 'name': 'dog'},\n", - " 19: {'id': 19, 'name': 'horse'},\n", - " 20: {'id': 20, 'name': 'sheep'},\n", - " 21: {'id': 21, 'name': 'cow'},\n", - " 22: {'id': 22, 'name': 'elephant'},\n", - " 23: {'id': 23, 'name': 'bear'},\n", - " 24: {'id': 24, 'name': 'zebra'},\n", - " 25: {'id': 25, 'name': 'giraffe'},\n", - " 27: {'id': 27, 'name': 'backpack'},\n", - " 28: {'id': 28, 'name': 'umbrella'},\n", - " 31: {'id': 31, 'name': 'handbag'},\n", - " 32: {'id': 32, 'name': 'tie'},\n", - " 33: {'id': 33, 'name': 'suitcase'},\n", - " 34: {'id': 34, 'name': 'frisbee'},\n", - " 35: {'id': 35, 'name': 'skis'},\n", - " 36: {'id': 36, 'name': 'snowboard'},\n", - " 37: {'id': 37, 'name': 'sports ball'},\n", - " 38: {'id': 38, 'name': 'kite'},\n", - " 39: {'id': 39, 'name': 'baseball bat'},\n", - " 40: {'id': 40, 'name': 'baseball glove'},\n", - " 41: {'id': 41, 'name': 'skateboard'},\n", - " 42: {'id': 42, 'name': 'surfboard'},\n", - " 43: {'id': 43, 'name': 'tennis racket'},\n", - " 44: {'id': 44, 'name': 'bottle'},\n", - " 46: {'id': 46, 'name': 'wine glass'},\n", - " 47: {'id': 47, 'name': 'cup'},\n", - " 48: {'id': 48, 'name': 'fork'},\n", - " 49: {'id': 49, 'name': 'knife'},\n", - " 50: {'id': 50, 'name': 'spoon'},\n", - " 51: {'id': 51, 'name': 'bowl'},\n", - " 52: {'id': 52, 'name': 'banana'},\n", - " 53: {'id': 53, 'name': 'apple'},\n", - " 54: {'id': 54, 'name': 'sandwich'},\n", - " 55: {'id': 55, 'name': 'orange'},\n", - " 56: {'id': 56, 'name': 'broccoli'},\n", - " 57: {'id': 57, 'name': 'carrot'},\n", - " 58: {'id': 58, 'name': 'hot dog'},\n", - " 59: {'id': 59, 'name': 'pizza'},\n", - " 60: {'id': 60, 'name': 'donut'},\n", - " 61: {'id': 61, 'name': 'cake'},\n", - " 62: {'id': 62, 'name': 'chair'},\n", - " 63: {'id': 63, 'name': 'couch'},\n", - " 64: {'id': 64, 'name': 'potted plant'},\n", - " 65: {'id': 65, 'name': 'bed'},\n", - " 67: {'id': 67, 'name': 'dining table'},\n", - " 70: {'id': 70, 'name': 'toilet'},\n", - " 72: {'id': 72, 'name': 'tv'},\n", - " 73: {'id': 73, 'name': 'laptop'},\n", - " 74: {'id': 74, 'name': 'mouse'},\n", - " 75: {'id': 75, 'name': 'remote'},\n", - " 76: {'id': 76, 'name': 'keyboard'},\n", - " 77: {'id': 77, 'name': 'cell phone'},\n", - " 78: {'id': 78, 'name': 'microwave'},\n", - " 79: {'id': 79, 'name': 'oven'},\n", - " 80: {'id': 80, 'name': 'toaster'},\n", - " 81: {'id': 81, 'name': 'sink'},\n", - " 82: {'id': 82, 'name': 'refrigerator'},\n", - " 84: {'id': 84, 'name': 'book'},\n", - " 85: {'id': 85, 'name': 'clock'},\n", - " 86: {'id': 86, 'name': 'vase'},\n", - " 87: {'id': 87, 'name': 'scissors'},\n", - " 88: {'id': 88, 'name': 'teddy bear'},\n", - " 89: {'id': 89, 'name': 'hair drier'},\n", - " 90: {'id': 90, 'name': 'toothbrush'},\n", - "}" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "QwcBC2TlPSwg", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Download the saved model and put it into models/research/object_detection/test_data/\n", - "!wget http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d5_coco17_tpu-32.tar.gz\n", - "!tar -xf efficientdet_d5_coco17_tpu-32.tar.gz\n", - "!mv efficientdet_d5_coco17_tpu-32/ models/research/object_detection/test_data/" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "Z2p-PmKLYCVU", - "colab": {} - }, - "source": [ - "start_time = time.time()\n", - "tf.keras.backend.clear_session()\n", - "detect_fn = tf.saved_model.load('models/research/object_detection/test_data/efficientdet_d5_coco17_tpu-32/saved_model/')\n", - "end_time = time.time()\n", - "elapsed_time = end_time - start_time\n", - "print('Elapsed time: ' + str(elapsed_time) + 's')" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "colab_type": "code", - "id": "vukkhd5-9NSL", - "colab": {} - }, - "source": [ - "import time\n", - "\n", - "image_dir = 'models/research/object_detection/test_images'\n", - "\n", - "elapsed = []\n", - "for i in range(2):\n", - " image_path = os.path.join(image_dir, 'image' + str(i + 1) + '.jpg')\n", - " image_np = load_image_into_numpy_array(image_path)\n", - " input_tensor = np.expand_dims(image_np, 0)\n", - " start_time = time.time()\n", - " detections = detect_fn(input_tensor)\n", - " end_time = time.time()\n", - " elapsed.append(end_time - start_time)\n", - "\n", - " plt.rcParams['figure.figsize'] = [42, 21]\n", - " label_id_offset = 1\n", - " image_np_with_detections = image_np.copy()\n", - " viz_utils.visualize_boxes_and_labels_on_image_array(\n", - " image_np_with_detections,\n", - " detections['detection_boxes'][0].numpy(),\n", - " detections['detection_classes'][0].numpy().astype(np.int32),\n", - " detections['detection_scores'][0].numpy(),\n", - " category_index,\n", - " use_normalized_coordinates=True,\n", - " max_boxes_to_draw=200,\n", - " min_score_thresh=.40,\n", - " agnostic_mode=False)\n", - " plt.subplot(2, 1, i+1)\n", - " plt.imshow(image_np_with_detections)\n", - "\n", - "mean_elapsed = sum(elapsed) / float(len(elapsed))\n", - "print('Elapsed time: ' + str(mean_elapsed) + ' second per image')" - ], - "execution_count": null, - "outputs": [] - } - ] -} \ No newline at end of file diff --git a/research/object_detection/colab_tutorials/inference_tf2_colab.ipynb b/research/object_detection/colab_tutorials/inference_tf2_colab.ipynb deleted file mode 100644 index 6b5cfaa787f..00000000000 --- a/research/object_detection/colab_tutorials/inference_tf2_colab.ipynb +++ /dev/null @@ -1,470 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "rOvvWAVTkMR7" - }, - "source": [ - "# Intro to Object Detection Colab\n", - "\n", - "Welcome to the object detection colab! This demo will take you through the steps of running an \"out-of-the-box\" detection model on a collection of images." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "vPs64QA1Zdov" - }, - "source": [ - "## Imports and Setup" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "LBZ9VWZZFUCT" - }, - "outputs": [], - "source": [ - "!pip install -U --pre tensorflow==\"2.2.0\"" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "oi28cqGGFWnY" - }, - "outputs": [], - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "# Clone the tensorflow models repository if it doesn't already exist\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "NwdsBdGhFanc" - }, - "outputs": [], - "source": [ - "# Install the Object Detection API\n", - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=.\n", - "cp object_detection/packages/tf2/setup.py .\n", - "python -m pip install ." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "yn5_uV1HLvaz" - }, - "outputs": [], - "source": [ - "import matplotlib\n", - "import matplotlib.pyplot as plt\n", - "\n", - "import io\n", - "import scipy.misc\n", - "import numpy as np\n", - "from six import BytesIO\n", - "from PIL import Image, ImageDraw, ImageFont\n", - "\n", - "import tensorflow as tf\n", - "\n", - "from object_detection.utils import label_map_util\n", - "from object_detection.utils import config_util\n", - "from object_detection.utils import visualization_utils as viz_utils\n", - "from object_detection.builders import model_builder\n", - "\n", - "%matplotlib inline" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "IogyryF2lFBL" - }, - "source": [ - "## Utilities" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "-y9R0Xllefec" - }, - "outputs": [], - "source": [ - "def load_image_into_numpy_array(path):\n", - " \"\"\"Load an image from file into a numpy array.\n", - "\n", - " Puts image into numpy array to feed into tensorflow graph.\n", - " Note that by convention we put it into a numpy array with shape\n", - " (height, width, channels), where channels=3 for RGB.\n", - "\n", - " Args:\n", - " path: the file path to the image\n", - "\n", - " Returns:\n", - " uint8 numpy array with shape (img_height, img_width, 3)\n", - " \"\"\"\n", - " img_data = tf.io.gfile.GFile(path, 'rb').read()\n", - " image = Image.open(BytesIO(img_data))\n", - " (im_width, im_height) = image.size\n", - " return np.array(image.getdata()).reshape(\n", - " (im_height, im_width, 3)).astype(np.uint8)\n", - "\n", - "def get_keypoint_tuples(eval_config):\n", - " \"\"\"Return a tuple list of keypoint edges from the eval config.\n", - " \n", - " Args:\n", - " eval_config: an eval config containing the keypoint edges\n", - " \n", - " Returns:\n", - " a list of edge tuples, each in the format (start, end)\n", - " \"\"\"\n", - " tuple_list = []\n", - " kp_list = eval_config.keypoint_edge\n", - " for edge in kp_list:\n", - " tuple_list.append((edge.start, edge.end))\n", - " return tuple_list" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "R4YjnOjME1gy" - }, - "outputs": [], - "source": [ - "# @title Choose the model to use, then evaluate the cell.\n", - "MODELS = {'centernet_with_keypoints': 'centernet_hg104_512x512_kpts_coco17_tpu-32', 'centernet_without_keypoints': 'centernet_hg104_512x512_coco17_tpu-8'}\n", - "\n", - "model_display_name = 'centernet_with_keypoints' # @param ['centernet_with_keypoints', 'centernet_without_keypoints']\n", - "model_name = MODELS[model_display_name]" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "6917xnUSlp9x" - }, - "source": [ - "### Build a detection model and load pre-trained model weights\n", - "\n", - "This sometimes takes a little while, please be patient!" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "ctPavqlyPuU_" - }, - "outputs": [], - "source": [ - "# Download the checkpoint and put it into models/research/object_detection/test_data/\n", - "\n", - "if model_display_name == 'centernet_with_keypoints':\n", - " !wget http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_hg104_512x512_kpts_coco17_tpu-32.tar.gz\n", - " !tar -xf centernet_hg104_512x512_kpts_coco17_tpu-32.tar.gz\n", - " !mv centernet_hg104_512x512_kpts_coco17_tpu-32/checkpoint models/research/object_detection/test_data/\n", - "else:\n", - " !wget http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_hg104_512x512_coco17_tpu-8.tar.gz\n", - " !tar -xf centernet_hg104_512x512_coco17_tpu-8.tar.gz\n", - " !mv centernet_hg104_512x512_coco17_tpu-8/checkpoint models/research/object_detection/test_data/" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "4cni4SSocvP_" - }, - "outputs": [], - "source": [ - "pipeline_config = os.path.join('models/research/object_detection/configs/tf2/',\n", - " model_name + '.config')\n", - "model_dir = 'models/research/object_detection/test_data/checkpoint/'\n", - "\n", - "# Load pipeline config and build a detection model\n", - "configs = config_util.get_configs_from_pipeline_file(pipeline_config)\n", - "model_config = configs['model']\n", - "detection_model = model_builder.build(\n", - " model_config=model_config, is_training=False)\n", - "\n", - "# Restore checkpoint\n", - "ckpt = tf.compat.v2.train.Checkpoint(\n", - " model=detection_model)\n", - "ckpt.restore(os.path.join(model_dir, 'ckpt-0')).expect_partial()\n", - "\n", - "def get_model_detection_function(model):\n", - " \"\"\"Get a tf.function for detection.\"\"\"\n", - "\n", - " @tf.function\n", - " def detect_fn(image):\n", - " \"\"\"Detect objects in image.\"\"\"\n", - "\n", - " image, shapes = model.preprocess(image)\n", - " prediction_dict = model.predict(image, shapes)\n", - " detections = model.postprocess(prediction_dict, shapes)\n", - "\n", - " return detections, prediction_dict, tf.reshape(shapes, [-1])\n", - "\n", - " return detect_fn\n", - "\n", - "detect_fn = get_model_detection_function(detection_model)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "NKtD0IeclbL5" - }, - "source": [ - "# Load label map data (for plotting).\n", - "\n", - "Label maps correspond index numbers to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "5mucYUS6exUJ" - }, - "outputs": [], - "source": [ - "label_map_path = configs['eval_input_config'].label_map_path\n", - "label_map = label_map_util.load_labelmap(label_map_path)\n", - "categories = label_map_util.convert_label_map_to_categories(\n", - " label_map,\n", - " max_num_classes=label_map_util.get_max_label_map_index(label_map),\n", - " use_display_name=True)\n", - "category_index = label_map_util.create_category_index(categories)\n", - "label_map_dict = label_map_util.get_label_map_dict(label_map, use_display_name=True)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "RLusV1o-mAx8" - }, - "source": [ - "### Putting everything together!\n", - "\n", - "Run the below code which loads an image, runs it through the detection model and visualizes the detection results, including the keypoints.\n", - "\n", - "Note that this will take a long time (several minutes) the first time you run this code due to tf.function's trace-compilation --- on subsequent runs (e.g. on new images), things will be faster.\n", - "\n", - "Here are some simple things to try out if you are curious:\n", - "* Try running inference on your own images (local paths work)\n", - "* Modify some of the input images and see if detection still works. Some simple things to try out here (just uncomment the relevant portions of code) include flipping the image horizontally, or converting to grayscale (note that we still expect the input image to have 3 channels).\n", - "* Print out `detections['detection_boxes']` and try to match the box locations to the boxes in the image. Notice that coordinates are given in normalized form (i.e., in the interval [0, 1]).\n", - "* Set min_score_thresh to other values (between 0 and 1) to allow more detections in or to filter out more detections.\n", - "\n", - "Note that you can run this cell repeatedly without rerunning earlier cells.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "vr_Fux-gfaG9" - }, - "outputs": [], - "source": [ - "image_dir = 'models/research/object_detection/test_images/'\n", - "image_path = os.path.join(image_dir, 'image2.jpg')\n", - "image_np = load_image_into_numpy_array(image_path)\n", - "\n", - "# Things to try:\n", - "# Flip horizontally\n", - "# image_np = np.fliplr(image_np).copy()\n", - "\n", - "# Convert image to grayscale\n", - "# image_np = np.tile(\n", - "# np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)\n", - "\n", - "input_tensor = tf.convert_to_tensor(\n", - " np.expand_dims(image_np, 0), dtype=tf.float32)\n", - "detections, predictions_dict, shapes = detect_fn(input_tensor)\n", - "\n", - "label_id_offset = 1\n", - "image_np_with_detections = image_np.copy()\n", - "\n", - "# Use keypoints if available in detections\n", - "keypoints, keypoint_scores = None, None\n", - "if 'detection_keypoints' in detections:\n", - " keypoints = detections['detection_keypoints'][0].numpy()\n", - " keypoint_scores = detections['detection_keypoint_scores'][0].numpy()\n", - "\n", - "viz_utils.visualize_boxes_and_labels_on_image_array(\n", - " image_np_with_detections,\n", - " detections['detection_boxes'][0].numpy(),\n", - " (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),\n", - " detections['detection_scores'][0].numpy(),\n", - " category_index,\n", - " use_normalized_coordinates=True,\n", - " max_boxes_to_draw=200,\n", - " min_score_thresh=.30,\n", - " agnostic_mode=False,\n", - " keypoints=keypoints,\n", - " keypoint_scores=keypoint_scores,\n", - " keypoint_edges=get_keypoint_tuples(configs['eval_config']))\n", - "\n", - "plt.figure(figsize=(12,16))\n", - "plt.imshow(image_np_with_detections)\n", - "plt.show()" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "colab_type": "text", - "id": "lYnOxprty3TD" - }, - "source": [ - "## Digging into the model's intermediate predictions\n", - "\n", - "For this part we will assume that the detection model is a CenterNet model following Zhou et al (https://arxiv.org/abs/1904.07850). And more specifically, we will assume that `detection_model` is of type `meta_architectures.center_net_meta_arch.CenterNetMetaArch`.\n", - "\n", - "As one of its intermediate predictions, CenterNet produces a heatmap of box centers for each class (for example, it will produce a heatmap whose size is proportional to that of the image that lights up at the center of each, e.g., \"zebra\"). In the following, we will visualize these intermediate class center heatmap predictions." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "colab": {}, - "colab_type": "code", - "id": "xBgYgSGMhHVi" - }, - "outputs": [], - "source": [ - "if detection_model.__class__.__name__ != 'CenterNetMetaArch':\n", - " raise AssertionError('The meta-architecture for this section '\n", - " 'is assumed to be CenterNetMetaArch!')\n", - "\n", - "def get_heatmap(predictions_dict, class_name):\n", - " \"\"\"Grabs class center logits and apply inverse logit transform.\n", - "\n", - " Args:\n", - " predictions_dict: dictionary of tensors containing a `object_center`\n", - " field of shape [1, heatmap_width, heatmap_height, num_classes]\n", - " class_name: string name of category (e.g., `horse`)\n", - "\n", - " Returns:\n", - " heatmap: 2d Tensor heatmap representing heatmap of centers for a given class\n", - " (For CenterNet, this is 128x128 or 256x256) with values in [0,1]\n", - " \"\"\"\n", - " class_index = label_map_dict[class_name]\n", - " class_center_logits = predictions_dict['object_center'][0]\n", - " class_center_logits = class_center_logits[0][\n", - " :, :, class_index - label_id_offset]\n", - " heatmap = tf.exp(class_center_logits) / (tf.exp(class_center_logits) + 1)\n", - " return heatmap\n", - "\n", - "def unpad_heatmap(heatmap, image_np):\n", - " \"\"\"Reshapes/unpads heatmap appropriately.\n", - "\n", - " Reshapes/unpads heatmap appropriately to match image_np.\n", - "\n", - " Args:\n", - " heatmap: Output of `get_heatmap`, a 2d Tensor\n", - " image_np: uint8 numpy array with shape (img_height, img_width, 3). Note\n", - " that due to padding, the relationship between img_height and img_width\n", - " might not be a simple scaling.\n", - "\n", - " Returns:\n", - " resized_heatmap_unpadded: a resized heatmap (2d Tensor) that is the same\n", - " size as `image_np`\n", - " \"\"\"\n", - " heatmap = tf.tile(tf.expand_dims(heatmap, 2), [1, 1, 3]) * 255\n", - " pre_strided_size = detection_model._stride * heatmap.shape[0]\n", - " resized_heatmap = tf.image.resize(\n", - " heatmap, [pre_strided_size, pre_strided_size],\n", - " method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)\n", - " resized_heatmap_unpadded = tf.slice(resized_heatmap, begin=[0,0,0], size=shapes)\n", - " return tf.image.resize(\n", - " resized_heatmap_unpadded,\n", - " [image_np.shape[0], image_np.shape[1]],\n", - " method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)[:,:,0]\n", - "\n", - "\n", - "class_name = 'kite'\n", - "heatmap = get_heatmap(predictions_dict, class_name)\n", - "resized_heatmap_unpadded = unpad_heatmap(heatmap, image_np)\n", - "plt.figure(figsize=(12,16))\n", - "plt.imshow(image_np_with_detections)\n", - "plt.imshow(resized_heatmap_unpadded, alpha=0.7,vmin=0, vmax=160, cmap='viridis')\n", - "plt.title('Object center heatmap (class: ' + class_name + ')')\n", - "plt.show()\n", - "\n", - "class_name = 'person'\n", - "heatmap = get_heatmap(predictions_dict, class_name)\n", - "resized_heatmap_unpadded = unpad_heatmap(heatmap, image_np)\n", - "plt.figure(figsize=(12,16))\n", - "plt.imshow(image_np_with_detections)\n", - "plt.imshow(resized_heatmap_unpadded, alpha=0.7,vmin=0, vmax=160, cmap='viridis')\n", - "plt.title('Object center heatmap (class: ' + class_name + ')')\n", - "plt.show()" - ] - } - ], - "metadata": { - "colab": { - "collapsed_sections": [], - "name": "inference_tf2_colab.ipynb", - "provenance": [] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/research/object_detection/colab_tutorials/object_detection_tutorial.ipynb b/research/object_detection/colab_tutorials/object_detection_tutorial.ipynb deleted file mode 100644 index 2c62740d2ff..00000000000 --- a/research/object_detection/colab_tutorials/object_detection_tutorial.ipynb +++ /dev/null @@ -1,635 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "metadata": { - "id": "V8-yl-s-WKMG" - }, - "source": [ - "# Object Detection API Demo\n", - "\n", - "\u003ctable align=\"left\"\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://colab.sandbox.google.com/github/tensorflow/models/blob/master/research/object_detection/colab_tutorials/object_detection_tutorial.ipynb\"\u003e\n", - " \u003cimg src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" /\u003eRun in Google Colab\n", - " \u003c/a\u003e\n", - "\u003c/td\u003e\u003ctd\u003e\n", - " \u003ca target=\"_blank\" href=\"https://github.com/tensorflow/models/blob/master/research/object_detection/colab_tutorials/object_detection_tutorial.ipynb\"\u003e\n", - " \u003cimg width=32px src=\"https://www.tensorflow.org/images/GitHub-Mark-32px.png\" /\u003eView source on GitHub\u003c/a\u003e\n", - "\u003c/td\u003e\u003c/table\u003e" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "3cIrseUv6WKz" - }, - "source": [ - "Welcome to the [Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection). This notebook will walk you step by step through the process of using a pre-trained model to detect objects in an image." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "VrJaG0cYN9yh" - }, - "source": [ - "\u003e **Important**: This tutorial is to help you through the first step towards using [Object Detection API](https://github.com/tensorflow/models/tree/master/research/object_detection) to build models. If you just just need an off the shelf model that does the job, see the [TFHub object detection example](https://colab.sandbox.google.com/github/tensorflow/hub/blob/master/examples/colab/object_detection.ipynb)." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "kFSqkTCdWKMI" - }, - "source": [ - "# Setup" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "awjrpqy-6MaQ" - }, - "source": [ - "Important: If you're running on a local machine, be sure to follow the [installation instructions](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md). This notebook includes only what's necessary to run in Colab." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "p3UGXxUii5Ym" - }, - "source": [ - "### Install" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "hGL97-GXjSUw" - }, - "outputs": [], - "source": [ - "!pip install -U --pre tensorflow==\"2.*\"\n", - "!pip install tf_slim" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "n_ap_s9ajTHH" - }, - "source": [ - "Make sure you have `pycocotools` installed" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "Bg8ZyA47i3pY" - }, - "outputs": [], - "source": [ - "!pip install pycocotools" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "-vsOL3QR6kqs" - }, - "source": [ - "Get `tensorflow/models` or `cd` to parent directory of the repository." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ykA0c-om51s1" - }, - "outputs": [], - "source": [ - "import os\n", - "import pathlib\n", - "\n", - "\n", - "if \"models\" in pathlib.Path.cwd().parts:\n", - " while \"models\" in pathlib.Path.cwd().parts:\n", - " os.chdir('..')\n", - "elif not pathlib.Path('models').exists():\n", - " !git clone --depth 1 https://github.com/tensorflow/models" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "O219m6yWAj9l" - }, - "source": [ - "Compile protobufs and install the object_detection package" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "PY41vdYYNlXc" - }, - "outputs": [], - "source": [ - "%%bash\n", - "cd models/research/\n", - "protoc object_detection/protos/*.proto --python_out=." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "s62yJyQUcYbp" - }, - "outputs": [], - "source": [ - "%%bash \n", - "cd models/research\n", - "pip install ." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "LBdjK2G5ywuc" - }, - "source": [ - "### Imports" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "hV4P5gyTWKMI" - }, - "outputs": [], - "source": [ - "import numpy as np\n", - "import os\n", - "import six.moves.urllib as urllib\n", - "import sys\n", - "import tarfile\n", - "import tensorflow as tf\n", - "import zipfile\n", - "\n", - "from collections import defaultdict\n", - "from io import StringIO\n", - "from matplotlib import pyplot as plt\n", - "from PIL import Image\n", - "from IPython.display import display" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "r5FNuiRPWKMN" - }, - "source": [ - "Import the object detection module." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "4-IMl4b6BdGO" - }, - "outputs": [], - "source": [ - "from object_detection.utils import ops as utils_ops\n", - "from object_detection.utils import label_map_util\n", - "from object_detection.utils import visualization_utils as vis_util" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "RYPCiag2iz_q" - }, - "source": [ - "Patches:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "mF-YlMl8c_bM" - }, - "outputs": [], - "source": [ - "# patch tf1 into `utils.ops`\n", - "utils_ops.tf = tf.compat.v1\n", - "\n", - "# Patch the location of gfile\n", - "tf.gfile = tf.io.gfile" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "cfn_tRFOWKMO" - }, - "source": [ - "# Model preparation " - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "X_sEBLpVWKMQ" - }, - "source": [ - "## Variables\n", - "\n", - "Any model exported using the `export_inference_graph.py` tool can be loaded here simply by changing the path.\n", - "\n", - "By default we use an \"SSD with Mobilenet\" model here. See the [detection model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md) for a list of other models that can be run out-of-the-box with varying speeds and accuracies." - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "7ai8pLZZWKMS" - }, - "source": [ - "## Loader" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "zm8xp-0eoItE" - }, - "outputs": [], - "source": [ - "def load_model(model_name):\n", - " base_url = 'http://download.tensorflow.org/models/object_detection/'\n", - " model_file = model_name + '.tar.gz'\n", - " model_dir = tf.keras.utils.get_file(\n", - " fname=model_name, \n", - " origin=base_url + model_file,\n", - " untar=True)\n", - "\n", - " model_dir = pathlib.Path(model_dir)/\"saved_model\"\n", - "\n", - " model = tf.saved_model.load(str(model_dir))\n", - "\n", - " return model" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "_1MVVTcLWKMW" - }, - "source": [ - "## Loading label map\n", - "Label maps map indices to category names, so that when our convolution network predicts `5`, we know that this corresponds to `airplane`. Here we use internal utility functions, but anything that returns a dictionary mapping integers to appropriate string labels would be fine" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "hDbpHkiWWKMX" - }, - "outputs": [], - "source": [ - "# List of the strings that is used to add correct label for each box.\n", - "PATH_TO_LABELS = 'models/research/object_detection/data/mscoco_label_map.pbtxt'\n", - "category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "oVU3U_J6IJVb" - }, - "source": [ - "For the sake of simplicity we will test on 2 images:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "jG-zn5ykWKMd" - }, - "outputs": [], - "source": [ - "# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.\n", - "PATH_TO_TEST_IMAGES_DIR = pathlib.Path('models/research/object_detection/test_images')\n", - "TEST_IMAGE_PATHS = sorted(list(PATH_TO_TEST_IMAGES_DIR.glob(\"*.jpg\")))\n", - "TEST_IMAGE_PATHS" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "H0_1AGhrWKMc" - }, - "source": [ - "# Detection" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "f7aOtOlebK7h" - }, - "source": [ - "Load an object detection model:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "1XNT0wxybKR6" - }, - "outputs": [], - "source": [ - "model_name = 'ssd_mobilenet_v1_coco_2017_11_17'\n", - "detection_model = load_model(model_name)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "yN1AYfAEJIGp" - }, - "source": [ - "Check the model's input signature, it expects a batch of 3-color images of type uint8:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "CK4cnry6wsHY" - }, - "outputs": [], - "source": [ - "print(detection_model.signatures['serving_default'].inputs)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "Q8u3BjpMJXZF" - }, - "source": [ - "And returns several outputs:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "oLSZpfaYwuSk" - }, - "outputs": [], - "source": [ - "detection_model.signatures['serving_default'].output_dtypes" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "FZyKUJeuxvpT" - }, - "outputs": [], - "source": [ - "detection_model.signatures['serving_default'].output_shapes" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "JP5qZ7sXJpwG" - }, - "source": [ - "Add a wrapper function to call the model, and cleanup the outputs:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "ajmR_exWyN76" - }, - "outputs": [], - "source": [ - "def run_inference_for_single_image(model, image):\n", - " image = np.asarray(image)\n", - " # The input needs to be a tensor, convert it using `tf.convert_to_tensor`.\n", - " input_tensor = tf.convert_to_tensor(image)\n", - " # The model expects a batch of images, so add an axis with `tf.newaxis`.\n", - " input_tensor = input_tensor[tf.newaxis,...]\n", - "\n", - " # Run inference\n", - " model_fn = model.signatures['serving_default']\n", - " output_dict = model_fn(input_tensor)\n", - "\n", - " # All outputs are batches tensors.\n", - " # Convert to numpy arrays, and take index [0] to remove the batch dimension.\n", - " # We're only interested in the first num_detections.\n", - " num_detections = int(output_dict.pop('num_detections'))\n", - " output_dict = {key:value[0, :num_detections].numpy() \n", - " for key,value in output_dict.items()}\n", - " output_dict['num_detections'] = num_detections\n", - "\n", - " # detection_classes should be ints.\n", - " output_dict['detection_classes'] = output_dict['detection_classes'].astype(np.int64)\n", - " \n", - " # Handle models with masks:\n", - " if 'detection_masks' in output_dict:\n", - " # Reframe the the bbox mask to the image size.\n", - " detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(\n", - " output_dict['detection_masks'], output_dict['detection_boxes'],\n", - " image.shape[0], image.shape[1]) \n", - " detection_masks_reframed = tf.cast(detection_masks_reframed \u003e 0.5,\n", - " tf.uint8)\n", - " output_dict['detection_masks_reframed'] = detection_masks_reframed.numpy()\n", - " \n", - " return output_dict" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "z1wq0LVyMRR_" - }, - "source": [ - "Run it on each test image and show the results:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "DWh_1zz6aqxs" - }, - "outputs": [], - "source": [ - "def show_inference(model, image_path):\n", - " # the array based representation of the image will be used later in order to prepare the\n", - " # result image with boxes and labels on it.\n", - " image_np = np.array(Image.open(image_path))\n", - " # Actual detection.\n", - " output_dict = run_inference_for_single_image(model, image_np)\n", - " # Visualization of the results of a detection.\n", - " vis_util.visualize_boxes_and_labels_on_image_array(\n", - " image_np,\n", - " output_dict['detection_boxes'],\n", - " output_dict['detection_classes'],\n", - " output_dict['detection_scores'],\n", - " category_index,\n", - " instance_masks=output_dict.get('detection_masks_reframed', None),\n", - " use_normalized_coordinates=True,\n", - " line_thickness=8)\n", - "\n", - " display(Image.fromarray(image_np))" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "3a5wMHN8WKMh" - }, - "outputs": [], - "source": [ - "for image_path in TEST_IMAGE_PATHS:\n", - " show_inference(detection_model, image_path)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "DsspMPX3Cssg" - }, - "source": [ - "## Instance Segmentation" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "CzkVv_n2MxKC" - }, - "outputs": [], - "source": [ - "model_name = \"mask_rcnn_inception_resnet_v2_atrous_coco_2018_01_28\"\n", - "masking_model = load_model(model_name)" - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "0S7aZi8ZOhVV" - }, - "source": [ - "The instance segmentation model includes a `detection_masks` output:" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "vQ2Sj2VIOZLA" - }, - "outputs": [], - "source": [ - "masking_model.output_shapes" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "id": "AS57rZlnNL7W" - }, - "outputs": [], - "source": [ - "for image_path in TEST_IMAGE_PATHS:\n", - " show_inference(masking_model, image_path)" - ] - } - ], - "metadata": { - "accelerator": "GPU", - "colab": { - "collapsed_sections": [], - "last_runtime": { - "build_target": "//learning/brain/python/client:colab_notebook", - "kind": "private" - }, - "name": "object_detection_tutorial.ipynb", - "private_outputs": true, - "provenance": [ - { - "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/colab_tutorials/object_detection_tutorial.ipynb", - "timestamp": 1594335690840 - }, - { - "file_id": "1LNYL6Zsn9Xlil2CVNOTsgDZQSBKeOjCh", - "timestamp": 1566498233247 - }, - { - "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc", - "timestamp": 1566488313397 - }, - { - "file_id": "/piper/depot/google3/third_party/py/tensorflow_docs/g3doc/en/r2/tutorials/generative/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc", - "timestamp": 1566145894046 - }, - { - "file_id": "1nBPoWynOV0auSIy40eQcBIk9C6YRSkI8", - "timestamp": 1566145841085 - }, - { - "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc", - "timestamp": 1556295408037 - }, - { - "file_id": "1layerger-51XwWOwYMY_5zHaCavCeQkO", - "timestamp": 1556214267924 - }, - { - "file_id": "/piper/depot/google3/third_party/tensorflow_models/object_detection/object_detection_tutorial.ipynb?workspaceId=markdaoust:copybara_AFABFE845DCD573AD3D43A6BAFBE77D4_0::citc", - "timestamp": 1556207836484 - }, - { - "file_id": "1w6mqQiNV3liPIX70NOgitOlDF1_4sRMw", - "timestamp": 1556154824101 - }, - { - "file_id": "https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb", - "timestamp": 1556150293326 - } - ] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, - "nbformat": 4, - "nbformat_minor": 0 -} diff --git a/research/object_detection/configs/tf1/ssd_spaghettinet_edgetpu_320x320_coco17_sync_4x4.config b/research/object_detection/configs/tf1/ssd_spaghettinet_edgetpu_320x320_coco17_sync_4x4.config deleted file mode 100644 index cea3117ef4f..00000000000 --- a/research/object_detection/configs/tf1/ssd_spaghettinet_edgetpu_320x320_coco17_sync_4x4.config +++ /dev/null @@ -1,196 +0,0 @@ -# SpaghettiNet Feature Extractor optimized for EdgeTPU. -# Trained on COCO17 from scratch. -# -# spaghettinet_edgetpu_s -# Achieves 26.2% mAP on COCO17 at 400k steps. -# 1.31ms Edge TPU latency at 1 billion MACs, 3.4 million params. -# -# spaghettinet_edgetpu_m -# Achieves 27.4% mAP on COCO17 at 400k steps. -# 1.55ms Edge TPU latency at 1.25 billion MACs, 4.1 million params. -# -# spaghettinet_edgetpu_l -# Achieves 28.02% mAP on COCO17 at 400k steps. -# 1.75ms Edge TPU latency at 1.57 billion MACs, 5.7 million params. -# -# TPU-compatible. - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - ssd_anchor_generator { - num_layers: 5 - min_scale: 0.2 - max_scale: 0.95 - aspect_ratios: 1.0 - aspect_ratios: 2.0 - aspect_ratios: 0.5 - aspect_ratios: 3.0 - aspect_ratios: 0.3333333 - } - } - image_resizer { - fixed_shape_resizer { - height: 320 - width: 320 - } - } - box_predictor { - convolutional_box_predictor { - min_depth: 0 - max_depth: 0 - num_layers_before_predictor: 0 - use_dropout: false - dropout_keep_probability: 0.8 - kernel_size: 3 - use_depthwise: true - box_code_size: 4 - apply_sigmoid_to_scores: false - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - train: true, - scale: true, - center: true, - decay: 0.97, - epsilon: 0.001, - } - } - } - } - feature_extractor { - type: 'ssd_spaghettinet' - # 3 architectures are supported and performance for each is listed at the top of this config file. - #spaghettinet_arch_name: 'spaghettinet_edgetpu_s' - spaghettinet_arch_name: 'spaghettinet_edgetpu_m' - #spaghettinet_arch_name: 'spaghettinet_edgetpu_l' - use_explicit_padding: false - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.75, - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - delta: 1.0 - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - use_static_shapes: true - } - score_converter: SIGMOID - } - } -} - -train_config: { - batch_size: 512 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 32 - num_steps: 400000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - ssd_random_crop { - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 0.8 - total_steps: 400000 - warmup_learning_rate: 0.13333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} - -graph_rewriter { - quantization { - delay: 40000 - weight_bits: 8 - activation_bits: 8 - } -} diff --git a/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_coco_tpu-128.config b/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_coco_tpu-128.config deleted file mode 100644 index 54cfecc8471..00000000000 --- a/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_coco_tpu-128.config +++ /dev/null @@ -1,210 +0,0 @@ -# DeepMAC meta architecture from the "The surprising impact of mask-head -# architecture on novel class segmentation" [1] paper with an Hourglass-100[2] -# mask head. This config is trained on all COCO classes and achieves a -# mask mAP of 39.4% on the COCO testdev-2017 set. -# [1]: https://arxiv.org/abs/2104.00613 -# [2]: https://arxiv.org/abs/1904.07850 - -# Train on TPU-128 - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - bgr_ordering: true - channel_means: [104.01362025, 114.03422265, 119.9165958 ] - channel_stds: [73.6027665 , 69.89082075, 70.9150767 ] - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1024 - max_dimension: 1024 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - - deepmac_mask_estimation { - dim: 32 - task_loss_weight: 5.0 - pixel_embedding_dim: 16 - mask_size: 32 - use_xy: true - use_instance_embedding: true - network_type: "hourglass100" - classification_loss { - weighted_sigmoid {} - } - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 50000 - - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 50000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-51" - fine_tune_checkpoint_type: "detection" -} - -train_input_reader: { - load_instance_masks: true - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - mask_type: PNG_MASKS - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - metrics_set: "coco_mask_metrics" - include_metrics_per_category: true - use_moving_averages: false - batch_size: 1; - super_categories { - key: "VOC" - value: "person,bicycle,car,motorcycle,airplane,bus,train,boat,bird,cat," - "dog,horse,sheep,cow,bottle,chair,couch,potted plant,dining table,tv" - } - super_categories { - key: "NonVOC" - value: "truck,traffic light,fire hydrant,stop sign,parking meter,bench," - "elephant,bear,zebra,giraffe,backpack,umbrella,handbag,tie,suitcase," - "frisbee,skis,snowboard,sports ball,kite,baseball bat,baseball glove," - "skateboard,surfboard,tennis racket,wine glass,cup,fork,knife,spoon,bowl," - "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut,cake,bed," - "toilet,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator,book,clock,vase,scissors,teddy bear,hair drier," - "toothbrush" - } - super_categories { - key: "person" - value: "person" - } - super_categories { - key: "vehicle" - value: "bicycle,car,motorcycle,airplane,bus,train,truck,boat" - } - super_categories { - key: "outdoor" - value: "traffic light,fire hydrant,stop sign,parking meter,bench" - } - super_categories { - key: "animal" - value: "bird,cat,dog,horse,sheep,cow,elephant,bear,zebra,giraffe" - } - super_categories { - key: "accessory" - value: "backpack,umbrella,handbag,tie,suitcase" - } - super_categories { - key: "sports" - value: "frisbee,skis,snowboard,sports ball,kite,baseball bat," - "baseball glove,skateboard,surfboard,tennis racket" - } - super_categories { - key: "kitchen" - value: "bottle,wine glass,cup,fork,knife,spoon,bowl" - } - super_categories { - key: "food" - value: "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut," - "cake" - } - super_categories { - key: "furniture" - value: "chair,couch,potted plant,bed,dining table,toilet" - } - super_categories { - key: "electronic" - value: "tv,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator" - } - super_categories { - key: "indoor" - value: "book,clock,vase,scissors,teddy bear,hair drier,toothbrush" - } -} - -eval_input_reader: { - load_instance_masks: true - mask_type: PNG_MASKS - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_non_voc_only_tpu-128.config b/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_non_voc_only_tpu-128.config deleted file mode 100644 index 50cbb6f145c..00000000000 --- a/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_non_voc_only_tpu-128.config +++ /dev/null @@ -1,273 +0,0 @@ -# DeepMAC meta architecture from the "The surprising impact of mask-head -# architecture on novel class segmentation" [1] paper with an Hourglass-100[2] -# mask head. This config is trained on masks from the non-VOC classes and -# achieves a mask mAP of 39.1% on the VOC classes. -# [1]: https://arxiv.org/abs/2104.00613 -# [2]: https://arxiv.org/abs/1904.07850 - -# Train on TPU-128 - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - bgr_ordering: true - channel_means: [104.01362025, 114.03422265, 119.9165958 ] - channel_stds: [73.6027665 , 69.89082075, 70.9150767 ] - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1024 - max_dimension: 1024 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - - deepmac_mask_estimation { - dim: 32 - task_loss_weight: 5.0 - pixel_embedding_dim: 16 - mask_size: 32 - use_xy: true - use_instance_embedding: true - network_type: "hourglass100" - classification_loss { - weighted_sigmoid {} - } - - allowed_masked_classes_ids: [ - 8, - 10, - 11, - 13, - 14, - 15, - 22, - 23, - 24, - 25, - 27, - 28, - 31, - 32, - 33, - 34, - 35, - 36, - 37, - 38, - 39, - 40, - 41, - 42, - 43, - 46, - 47, - 48, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 58, - 59, - 60, - 61, - 65, - 70, - 73, - 74, - 75, - 76, - 77, - 78, - 79, - 80, - 81, - 82, - 84, - 85, - 86, - 87, - 88, - 89, - 90 - ] - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 50000 - - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 50000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-51" - fine_tune_checkpoint_type: "detection" -} - -train_input_reader: { - load_instance_masks: true - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - mask_type: PNG_MASKS - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - metrics_set: "coco_mask_metrics" - include_metrics_per_category: true - use_moving_averages: false - batch_size: 1; - super_categories { - key: "VOC" - value: "person,bicycle,car,motorcycle,airplane,bus,train,boat,bird,cat," - "dog,horse,sheep,cow,bottle,chair,couch,potted plant,dining table,tv" - } - super_categories { - key: "NonVOC" - value: "truck,traffic light,fire hydrant,stop sign,parking meter,bench," - "elephant,bear,zebra,giraffe,backpack,umbrella,handbag,tie,suitcase," - "frisbee,skis,snowboard,sports ball,kite,baseball bat,baseball glove," - "skateboard,surfboard,tennis racket,wine glass,cup,fork,knife,spoon,bowl," - "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut,cake,bed," - "toilet,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator,book,clock,vase,scissors,teddy bear,hair drier," - "toothbrush" - } - super_categories { - key: "person" - value: "person" - } - super_categories { - key: "vehicle" - value: "bicycle,car,motorcycle,airplane,bus,train,truck,boat" - } - super_categories { - key: "outdoor" - value: "traffic light,fire hydrant,stop sign,parking meter,bench" - } - super_categories { - key: "animal" - value: "bird,cat,dog,horse,sheep,cow,elephant,bear,zebra,giraffe" - } - super_categories { - key: "accessory" - value: "backpack,umbrella,handbag,tie,suitcase" - } - super_categories { - key: "sports" - value: "frisbee,skis,snowboard,sports ball,kite,baseball bat," - "baseball glove,skateboard,surfboard,tennis racket" - } - super_categories { - key: "kitchen" - value: "bottle,wine glass,cup,fork,knife,spoon,bowl" - } - super_categories { - key: "food" - value: "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut," - "cake" - } - super_categories { - key: "furniture" - value: "chair,couch,potted plant,bed,dining table,toilet" - } - super_categories { - key: "electronic" - value: "tv,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator" - } - super_categories { - key: "indoor" - value: "book,clock,vase,scissors,teddy bear,hair drier,toothbrush" - } -} - -eval_input_reader: { - load_instance_masks: true - mask_type: PNG_MASKS - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_voc_only_tpu-128.config b/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_voc_only_tpu-128.config deleted file mode 100644 index 2c99cb7e2c2..00000000000 --- a/research/object_detection/configs/tf2/center_net_deepmac_1024x1024_voc_only_tpu-128.config +++ /dev/null @@ -1,234 +0,0 @@ -# DeepMAC meta architecture from the "The surprising impact of mask-head -# architecture on novel class segmentation" [1] paper with an Hourglass-100[2] -# mask head. This config is only trained on masks from the VOC classes in COCO -# and achieves a mask mAP of 35.5% on non-VOC classes. -# [1]: https://arxiv.org/abs/2104.00613 -# [2]: https://arxiv.org/abs/1904.07850 - -# Train on TPU-128 - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - bgr_ordering: true - channel_means: [104.01362025, 114.03422265, 119.9165958 ] - channel_stds: [73.6027665 , 69.89082075, 70.9150767 ] - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1024 - max_dimension: 1024 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - - deepmac_mask_estimation { - dim: 32 - task_loss_weight: 5.0 - pixel_embedding_dim: 16 - mask_size: 32 - use_xy: true - use_instance_embedding: true - network_type: "hourglass100" - classification_loss { - weighted_sigmoid {} - } - - allowed_masked_classes_ids: [ - 1, # person - 2, # bicycle - 3, # car - 4, # motorcycle/motorbike - 5, # airplane/aeroplane, - 6, # bus - 7, # train - 9, # boat - 16, # bird - 17, # cat - 18, # dog - 19, # horse - 20, # sheep - 21, # cow - 44, # bottle - 62, # chair - 63, # couch/sofa - 64, # potted plant - 67, # dining table - 72 # tvmonitor - ] - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 50000 - - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 50000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-51" - fine_tune_checkpoint_type: "detection" -} - -train_input_reader: { - load_instance_masks: true - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - mask_type: PNG_MASKS - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - metrics_set: "coco_mask_metrics" - include_metrics_per_category: true - use_moving_averages: false - batch_size: 1; - super_categories { - key: "VOC" - value: "person,bicycle,car,motorcycle,airplane,bus,train,boat,bird,cat," - "dog,horse,sheep,cow,bottle,chair,couch,potted plant,dining table,tv" - } - super_categories { - key: "NonVOC" - value: "truck,traffic light,fire hydrant,stop sign,parking meter,bench," - "elephant,bear,zebra,giraffe,backpack,umbrella,handbag,tie,suitcase," - "frisbee,skis,snowboard,sports ball,kite,baseball bat,baseball glove," - "skateboard,surfboard,tennis racket,wine glass,cup,fork,knife,spoon,bowl," - "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut,cake,bed," - "toilet,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator,book,clock,vase,scissors,teddy bear,hair drier," - "toothbrush" - } - super_categories { - key: "person" - value: "person" - } - super_categories { - key: "vehicle" - value: "bicycle,car,motorcycle,airplane,bus,train,truck,boat" - } - super_categories { - key: "outdoor" - value: "traffic light,fire hydrant,stop sign,parking meter,bench" - } - super_categories { - key: "animal" - value: "bird,cat,dog,horse,sheep,cow,elephant,bear,zebra,giraffe" - } - super_categories { - key: "accessory" - value: "backpack,umbrella,handbag,tie,suitcase" - } - super_categories { - key: "sports" - value: "frisbee,skis,snowboard,sports ball,kite,baseball bat," - "baseball glove,skateboard,surfboard,tennis racket" - } - super_categories { - key: "kitchen" - value: "bottle,wine glass,cup,fork,knife,spoon,bowl" - } - super_categories { - key: "food" - value: "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut," - "cake" - } - super_categories { - key: "furniture" - value: "chair,couch,potted plant,bed,dining table,toilet" - } - super_categories { - key: "electronic" - value: "tv,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator" - } - super_categories { - key: "indoor" - value: "book,clock,vase,scissors,teddy bear,hair drier,toothbrush" - } -} - -eval_input_reader: { - load_instance_masks: true - mask_type: PNG_MASKS - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} - diff --git a/research/object_detection/configs/tf2/center_net_deepmac_512x512_voc_only_tpu-32.config b/research/object_detection/configs/tf2/center_net_deepmac_512x512_voc_only_tpu-32.config deleted file mode 100644 index b604eeddd08..00000000000 --- a/research/object_detection/configs/tf2/center_net_deepmac_512x512_voc_only_tpu-32.config +++ /dev/null @@ -1,233 +0,0 @@ -# DeepMAC meta architecture from the "The surprising impact of mask-head -# architecture on novel class segmentation" [1] paper with an Hourglass-52[2] -# mask head. This config is only trained on masks from the VOC classes in COCO -# and achieves a mask mAP of 32.5% on non-VOC classes. -# [1]: https://arxiv.org/abs/2104.00613 - -# Train on TPU-32 - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - bgr_ordering: true - channel_means: [104.01362025, 114.03422265, 119.9165958 ] - channel_stds: [73.6027665 , 69.89082075, 70.9150767 ] - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - - deepmac_mask_estimation { - dim: 32 - task_loss_weight: 5.0 - pixel_embedding_dim: 16 - mask_size: 32 - use_xy: true - use_instance_embedding: true - network_type: "hourglass52" - classification_loss { - weighted_sigmoid {} - } - use_only_last_stage: true - - allowed_masked_classes_ids: [ - 1, # person - 2, # bicycle - 3, # car - 4, # motorcycle/motorbike - 5, # airplane/aeroplane, - 6, # bus - 7, # train - 9, # boat - 16, # bird - 17, # cat - 18, # dog - 19, # horse - 20, # sheep - 21, # cow - 44, # bottle - 62, # chair - 63, # couch/sofa - 64, # potted plant - 67, # dining table - 72 # tvmonitor - ] - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 50000 - - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 50000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-1" - fine_tune_checkpoint_type: "detection" -} - -train_input_reader: { - load_instance_masks: true - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - mask_type: PNG_MASKS - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - metrics_set: "coco_mask_metrics" - include_metrics_per_category: true - use_moving_averages: false - batch_size: 1; - super_categories { - key: "VOC" - value: "person,bicycle,car,motorcycle,airplane,bus,train,boat,bird,cat," - "dog,horse,sheep,cow,bottle,chair,couch,potted plant,dining table,tv" - } - super_categories { - key: "NonVOC" - value: "truck,traffic light,fire hydrant,stop sign,parking meter,bench," - "elephant,bear,zebra,giraffe,backpack,umbrella,handbag,tie,suitcase," - "frisbee,skis,snowboard,sports ball,kite,baseball bat,baseball glove," - "skateboard,surfboard,tennis racket,wine glass,cup,fork,knife,spoon,bowl," - "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut,cake,bed," - "toilet,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator,book,clock,vase,scissors,teddy bear,hair drier," - "toothbrush" - } - super_categories { - key: "person" - value: "person" - } - super_categories { - key: "vehicle" - value: "bicycle,car,motorcycle,airplane,bus,train,truck,boat" - } - super_categories { - key: "outdoor" - value: "traffic light,fire hydrant,stop sign,parking meter,bench" - } - super_categories { - key: "animal" - value: "bird,cat,dog,horse,sheep,cow,elephant,bear,zebra,giraffe" - } - super_categories { - key: "accessory" - value: "backpack,umbrella,handbag,tie,suitcase" - } - super_categories { - key: "sports" - value: "frisbee,skis,snowboard,sports ball,kite,baseball bat," - "baseball glove,skateboard,surfboard,tennis racket" - } - super_categories { - key: "kitchen" - value: "bottle,wine glass,cup,fork,knife,spoon,bowl" - } - super_categories { - key: "food" - value: "banana,apple,sandwich,orange,broccoli,carrot,hot dog,pizza,donut," - "cake" - } - super_categories { - key: "furniture" - value: "chair,couch,potted plant,bed,dining table,toilet" - } - super_categories { - key: "electronic" - value: "tv,laptop,mouse,remote,keyboard,cell phone,microwave,oven,toaster," - "sink,refrigerator" - } - super_categories { - key: "indoor" - value: "book,clock,vase,scissors,teddy bear,hair drier,toothbrush" - } -} - -eval_input_reader: { - load_instance_masks: true - mask_type: PNG_MASKS - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/centernet_hourglass104_1024x1024_coco17_tpu-32.config b/research/object_detection/configs/tf2/centernet_hourglass104_1024x1024_coco17_tpu-32.config deleted file mode 100644 index c0a90ef44c9..00000000000 --- a/research/object_detection/configs/tf2/centernet_hourglass104_1024x1024_coco17_tpu-32.config +++ /dev/null @@ -1,129 +0,0 @@ -# CenterNet meta-architecture from the "Objects as Points" [2] paper with the -# hourglass[1] backbone. -# [1]: https://arxiv.org/abs/1603.06937 -# [2]: https://arxiv.org/abs/1904.07850 -# Trained on COCO, initialized from Extremenet Detection checkpoint -# Train on TPU-32 v3 -# -# Achieves 44.6 mAP on COCO17 Val - - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - bgr_ordering: true - channel_means: [104.01362025, 114.03422265, 119.9165958 ] - channel_stds: [73.6027665 , 69.89082075, 70.9150767 ] - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1024 - max_dimension: 1024 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 50000 - - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 50000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-1" - fine_tune_checkpoint_type: "detection" -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/centernet_hourglass104_1024x1024_kpts_coco17_tpu-32.config b/research/object_detection/configs/tf2/centernet_hourglass104_1024x1024_kpts_coco17_tpu-32.config deleted file mode 100644 index da7136f15db..00000000000 --- a/research/object_detection/configs/tf2/centernet_hourglass104_1024x1024_kpts_coco17_tpu-32.config +++ /dev/null @@ -1,374 +0,0 @@ -# CenterNet meta-architecture from the "Objects as Points" [2] paper with the -# hourglass[1] backbone. This config achieves an mAP of 42.8/64.5 +/- 0.16 on -# COCO 17 (averaged over 5 runs). This config is TPU compatible. -# [1]: https://arxiv.org/abs/1603.06937 -# [2]: https://arxiv.org/abs/1904.07850 - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - channel_means: 104.01361846923828 - channel_means: 114.03422546386719 - channel_means: 119.91659545898438 - channel_stds: 73.60276794433594 - channel_stds: 69.89082336425781 - channel_stds: 70.91507720947266 - bgr_ordering: true - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1024 - max_dimension: 1024 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.10000000149011612 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - min_box_overlap_iou: 0.699999988079071 - max_box_predictions: 100 - } - keypoint_label_map_path: "PATH_TO_BE_CONFIGURED" - keypoint_estimation_task { - task_name: "human_pose" - task_loss_weight: 1.0 - loss { - localization_loss { - l1_localization_loss { - } - } - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - keypoint_class_name: "/m/01g317" - keypoint_label_to_std { - key: "left_ankle" - value: 0.8899999856948853 - } - keypoint_label_to_std { - key: "left_ear" - value: 0.3499999940395355 - } - keypoint_label_to_std { - key: "left_elbow" - value: 0.7200000286102295 - } - keypoint_label_to_std { - key: "left_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "left_hip" - value: 1.0700000524520874 - } - keypoint_label_to_std { - key: "left_knee" - value: 0.8899999856948853 - } - keypoint_label_to_std { - key: "left_shoulder" - value: 0.7900000214576721 - } - keypoint_label_to_std { - key: "left_wrist" - value: 0.6200000047683716 - } - keypoint_label_to_std { - key: "nose" - value: 0.25999999046325684 - } - keypoint_label_to_std { - key: "right_ankle" - value: 0.8899999856948853 - } - keypoint_label_to_std { - key: "right_ear" - value: 0.3499999940395355 - } - keypoint_label_to_std { - key: "right_elbow" - value: 0.7200000286102295 - } - keypoint_label_to_std { - key: "right_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "right_hip" - value: 1.0700000524520874 - } - keypoint_label_to_std { - key: "right_knee" - value: 0.8899999856948853 - } - keypoint_label_to_std { - key: "right_shoulder" - value: 0.7900000214576721 - } - keypoint_label_to_std { - key: "right_wrist" - value: 0.6200000047683716 - } - keypoint_regression_loss_weight: 0.10000000149011612 - keypoint_heatmap_loss_weight: 1.0 - keypoint_offset_loss_weight: 1.0 - offset_peak_radius: 3 - per_keypoint_offset: true - } - } -} -train_config { - batch_size: 128 - data_augmentation_options { - random_horizontal_flip { - keypoint_flip_permutation: 0 - keypoint_flip_permutation: 2 - keypoint_flip_permutation: 1 - keypoint_flip_permutation: 4 - keypoint_flip_permutation: 3 - keypoint_flip_permutation: 6 - keypoint_flip_permutation: 5 - keypoint_flip_permutation: 8 - keypoint_flip_permutation: 7 - keypoint_flip_permutation: 10 - keypoint_flip_permutation: 9 - keypoint_flip_permutation: 12 - keypoint_flip_permutation: 11 - keypoint_flip_permutation: 14 - keypoint_flip_permutation: 13 - keypoint_flip_permutation: 16 - keypoint_flip_permutation: 15 - } - } - data_augmentation_options { - random_adjust_hue { - } - } - data_augmentation_options { - random_adjust_contrast { - } - } - data_augmentation_options { - random_adjust_saturation { - } - } - data_augmentation_options { - random_adjust_brightness { - } - } - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6000000238418579 - scale_max: 1.2999999523162842 - } - } - optimizer { - adam_optimizer { - learning_rate { - cosine_decay_learning_rate { - learning_rate_base: 0.0010000000474974513 - total_steps: 250000 - warmup_learning_rate: 0.0002500000118743628 - warmup_steps: 5000 - } - } - epsilon: 1.0000000116860974e-07 - } - use_moving_average: false - } - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED" - num_steps: 250000 - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - fine_tune_checkpoint_type: "detection" - fine_tune_checkpoint_version: V2 -} -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } - num_keypoints: 17 -} -eval_config { - num_visualizations: 10 - metrics_set: "coco_detection_metrics" - use_moving_averages: false - min_score_threshold: 0.20000000298023224 - max_num_boxes_to_visualize: 20 - batch_size: 1 - parameterized_metric { - coco_keypoint_metrics { - class_label: "person" - keypoint_label_to_sigmas { - key: "left_ankle" - value: 0.08900000154972076 - } - keypoint_label_to_sigmas { - key: "left_ear" - value: 0.03500000014901161 - } - keypoint_label_to_sigmas { - key: "left_elbow" - value: 0.07199999690055847 - } - keypoint_label_to_sigmas { - key: "left_eye" - value: 0.02500000037252903 - } - keypoint_label_to_sigmas { - key: "left_hip" - value: 0.10700000077486038 - } - keypoint_label_to_sigmas { - key: "left_knee" - value: 0.08699999749660492 - } - keypoint_label_to_sigmas { - key: "left_shoulder" - value: 0.07900000363588333 - } - keypoint_label_to_sigmas { - key: "left_wrist" - value: 0.06199999898672104 - } - keypoint_label_to_sigmas { - key: "nose" - value: 0.026000000536441803 - } - keypoint_label_to_sigmas { - key: "right_ankle" - value: 0.08900000154972076 - } - keypoint_label_to_sigmas { - key: "right_ear" - value: 0.03500000014901161 - } - keypoint_label_to_sigmas { - key: "right_elbow" - value: 0.07199999690055847 - } - keypoint_label_to_sigmas { - key: "right_eye" - value: 0.02500000037252903 - } - keypoint_label_to_sigmas { - key: "right_hip" - value: 0.10700000077486038 - } - keypoint_label_to_sigmas { - key: "right_knee" - value: 0.08699999749660492 - } - keypoint_label_to_sigmas { - key: "right_shoulder" - value: 0.07900000363588333 - } - keypoint_label_to_sigmas { - key: "right_wrist" - value: 0.06199999898672104 - } - } - } - keypoint_edge { - start: 0 - end: 1 - } - keypoint_edge { - start: 0 - end: 2 - } - keypoint_edge { - start: 1 - end: 3 - } - keypoint_edge { - start: 2 - end: 4 - } - keypoint_edge { - start: 0 - end: 5 - } - keypoint_edge { - start: 0 - end: 6 - } - keypoint_edge { - start: 5 - end: 7 - } - keypoint_edge { - start: 7 - end: 9 - } - keypoint_edge { - start: 6 - end: 8 - } - keypoint_edge { - start: 8 - end: 10 - } - keypoint_edge { - start: 5 - end: 6 - } - keypoint_edge { - start: 5 - end: 11 - } - keypoint_edge { - start: 6 - end: 12 - } - keypoint_edge { - start: 11 - end: 12 - } - keypoint_edge { - start: 11 - end: 13 - } - keypoint_edge { - start: 13 - end: 15 - } - keypoint_edge { - start: 12 - end: 14 - } - keypoint_edge { - start: 14 - end: 16 - } -} -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } - num_keypoints: 17 -} diff --git a/research/object_detection/configs/tf2/centernet_hourglass104_512x512_coco17_tpu-8.config b/research/object_detection/configs/tf2/centernet_hourglass104_512x512_coco17_tpu-8.config deleted file mode 100644 index 9e38d98939b..00000000000 --- a/research/object_detection/configs/tf2/centernet_hourglass104_512x512_coco17_tpu-8.config +++ /dev/null @@ -1,143 +0,0 @@ -# CenterNet meta-architecture from the "Objects as Points" [2] paper with the -# hourglass[1] backbone. -# [1]: https://arxiv.org/abs/1603.06937 -# [2]: https://arxiv.org/abs/1904.07850 -# Trained on COCO, initialized from Extremenet Detection checkpoint -# Train on TPU-8 -# -# Achieves 41.9 mAP on COCO17 Val - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - bgr_ordering: true - channel_means: [104.01362025, 114.03422265, 119.9165958 ] - channel_stds: [73.6027665 , 69.89082075, 70.9150767 ] - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 140000 - - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_crop_image { - min_aspect_ratio: 0.5 - max_aspect_ratio: 1.7 - random_coef: 0.25 - } - } - - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_absolute_pad_image { - max_height_padding: 200 - max_width_padding: 200 - pad_color: [0, 0, 0] - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - manual_step_learning_rate { - initial_learning_rate: 1e-3 - schedule { - step: 90000 - learning_rate: 1e-4 - } - schedule { - step: 120000 - learning_rate: 1e-5 - } - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-1" - fine_tune_checkpoint_type: "detection" -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/centernet_hourglass104_512x512_kpts_coco17_tpu-32.config b/research/object_detection/configs/tf2/centernet_hourglass104_512x512_kpts_coco17_tpu-32.config deleted file mode 100644 index ce5652895f9..00000000000 --- a/research/object_detection/configs/tf2/centernet_hourglass104_512x512_kpts_coco17_tpu-32.config +++ /dev/null @@ -1,395 +0,0 @@ -# CenterNet meta-architecture from the "Objects as Points" [2] paper with the -# hourglass[1] backbone. This config achieves an mAP of 40.0/61.4 +/- 0.16 on -# COCO 17 (averaged over 5 runs). This config is TPU compatible. -# [1]: https://arxiv.org/abs/1603.06937 -# [2]: https://arxiv.org/abs/1904.07850 - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "hourglass_104" - bgr_ordering: true - channel_means: [104.01362025, 114.03422265, 119.9165958 ] - channel_stds: [73.6027665 , 69.89082075, 70.9150767 ] - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - - keypoint_label_map_path: "PATH_TO_BE_CONFIGURED" - keypoint_estimation_task { - task_name: "human_pose" - task_loss_weight: 1.0 - loss { - localization_loss { - l1_localization_loss { - } - } - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - keypoint_class_name: "/m/01g317" - keypoint_label_to_std { - key: "left_ankle" - value: 0.89 - } - keypoint_label_to_std { - key: "left_ear" - value: 0.35 - } - keypoint_label_to_std { - key: "left_elbow" - value: 0.72 - } - keypoint_label_to_std { - key: "left_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "left_hip" - value: 1.07 - } - keypoint_label_to_std { - key: "left_knee" - value: 0.89 - } - keypoint_label_to_std { - key: "left_shoulder" - value: 0.79 - } - keypoint_label_to_std { - key: "left_wrist" - value: 0.62 - } - keypoint_label_to_std { - key: "nose" - value: 0.26 - } - keypoint_label_to_std { - key: "right_ankle" - value: 0.89 - } - keypoint_label_to_std { - key: "right_ear" - value: 0.35 - } - keypoint_label_to_std { - key: "right_elbow" - value: 0.72 - } - keypoint_label_to_std { - key: "right_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "right_hip" - value: 1.07 - } - keypoint_label_to_std { - key: "right_knee" - value: 0.89 - } - keypoint_label_to_std { - key: "right_shoulder" - value: 0.79 - } - keypoint_label_to_std { - key: "right_wrist" - value: 0.62 - } - keypoint_regression_loss_weight: 0.1 - keypoint_heatmap_loss_weight: 1.0 - keypoint_offset_loss_weight: 1.0 - offset_peak_radius: 3 - per_keypoint_offset: true - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 250000 - - data_augmentation_options { - random_horizontal_flip { - keypoint_flip_permutation: 0 - keypoint_flip_permutation: 2 - keypoint_flip_permutation: 1 - keypoint_flip_permutation: 4 - keypoint_flip_permutation: 3 - keypoint_flip_permutation: 6 - keypoint_flip_permutation: 5 - keypoint_flip_permutation: 8 - keypoint_flip_permutation: 7 - keypoint_flip_permutation: 10 - keypoint_flip_permutation: 9 - keypoint_flip_permutation: 12 - keypoint_flip_permutation: 11 - keypoint_flip_permutation: 14 - keypoint_flip_permutation: 13 - keypoint_flip_permutation: 16 - keypoint_flip_permutation: 15 - } - } - - data_augmentation_options { - random_crop_image { - min_aspect_ratio: 0.5 - max_aspect_ratio: 1.7 - random_coef: 0.25 - } - } - - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_absolute_pad_image { - max_height_padding: 200 - max_width_padding: 200 - pad_color: [0, 0, 0] - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 250000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED" - fine_tune_checkpoint_type: "detection" -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } - num_keypoints: 17 -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - num_visualizations: 10 - max_num_boxes_to_visualize: 20 - min_score_threshold: 0.2 - batch_size: 1; - parameterized_metric { - coco_keypoint_metrics { - class_label: "person" - keypoint_label_to_sigmas { - key: "nose" - value: 0.026 - } - keypoint_label_to_sigmas { - key: "left_eye" - value: 0.025 - } - keypoint_label_to_sigmas { - key: "right_eye" - value: 0.025 - } - keypoint_label_to_sigmas { - key: "left_ear" - value: 0.035 - } - keypoint_label_to_sigmas { - key: "right_ear" - value: 0.035 - } - keypoint_label_to_sigmas { - key: "left_shoulder" - value: 0.079 - } - keypoint_label_to_sigmas { - key: "right_shoulder" - value: 0.079 - } - keypoint_label_to_sigmas { - key: "left_elbow" - value: 0.072 - } - keypoint_label_to_sigmas { - key: "right_elbow" - value: 0.072 - } - keypoint_label_to_sigmas { - key: "left_wrist" - value: 0.062 - } - keypoint_label_to_sigmas { - key: "right_wrist" - value: 0.062 - } - keypoint_label_to_sigmas { - key: "left_hip" - value: 0.107 - } - keypoint_label_to_sigmas { - key: "right_hip" - value: 0.107 - } - keypoint_label_to_sigmas { - key: "left_knee" - value: 0.087 - } - keypoint_label_to_sigmas { - key: "right_knee" - value: 0.087 - } - keypoint_label_to_sigmas { - key: "left_ankle" - value: 0.089 - } - keypoint_label_to_sigmas { - key: "right_ankle" - value: 0.089 - } - } - } - # Provide the edges to connect the keypoints. The setting is suitable for - # COCO's 17 human pose keypoints. - keypoint_edge { # nose-left eye - start: 0 - end: 1 - } - keypoint_edge { # nose-right eye - start: 0 - end: 2 - } - keypoint_edge { # left eye-left ear - start: 1 - end: 3 - } - keypoint_edge { # right eye-right ear - start: 2 - end: 4 - } - keypoint_edge { # nose-left shoulder - start: 0 - end: 5 - } - keypoint_edge { # nose-right shoulder - start: 0 - end: 6 - } - keypoint_edge { # left shoulder-left elbow - start: 5 - end: 7 - } - keypoint_edge { # left elbow-left wrist - start: 7 - end: 9 - } - keypoint_edge { # right shoulder-right elbow - start: 6 - end: 8 - } - keypoint_edge { # right elbow-right wrist - start: 8 - end: 10 - } - keypoint_edge { # left shoulder-right shoulder - start: 5 - end: 6 - } - keypoint_edge { # left shoulder-left hip - start: 5 - end: 11 - } - keypoint_edge { # right shoulder-right hip - start: 6 - end: 12 - } - keypoint_edge { # left hip-right hip - start: 11 - end: 12 - } - keypoint_edge { # left hip-left knee - start: 11 - end: 13 - } - keypoint_edge { # left knee-left ankle - start: 13 - end: 15 - } - keypoint_edge { # right hip-right knee - start: 12 - end: 14 - } - keypoint_edge { # right knee-right ankle - start: 14 - end: 16 - } -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } - num_keypoints: 17 -} - diff --git a/research/object_detection/configs/tf2/centernet_resnet101_v1_fpn_512x512_coco17_tpu-8.config b/research/object_detection/configs/tf2/centernet_resnet101_v1_fpn_512x512_coco17_tpu-8.config deleted file mode 100644 index 2bb7f07ce5e..00000000000 --- a/research/object_detection/configs/tf2/centernet_resnet101_v1_fpn_512x512_coco17_tpu-8.config +++ /dev/null @@ -1,141 +0,0 @@ -# CenterNet meta-architecture from the "Objects as Points" [1] paper -# with the ResNet-v1-101 FPN backbone. -# [1]: https://arxiv.org/abs/1904.07850 - -# Train on TPU-8 -# -# Achieves 34.18 mAP on COCO17 Val - - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "resnet_v2_101" - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 140000 - - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_crop_image { - min_aspect_ratio: 0.5 - max_aspect_ratio: 1.7 - random_coef: 0.25 - } - } - - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_absolute_pad_image { - max_height_padding: 200 - max_width_padding: 200 - pad_color: [0, 0, 0] - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - manual_step_learning_rate { - initial_learning_rate: 1e-3 - schedule { - step: 90000 - learning_rate: 1e-4 - } - schedule { - step: 120000 - learning_rate: 1e-5 - } - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/weights-1" - fine_tune_checkpoint_type: "classification" -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} - diff --git a/research/object_detection/configs/tf2/centernet_resnet50_v1_fpn_512x512_kpts_coco17_tpu-8.config b/research/object_detection/configs/tf2/centernet_resnet50_v1_fpn_512x512_kpts_coco17_tpu-8.config deleted file mode 100644 index ad25d5c347d..00000000000 --- a/research/object_detection/configs/tf2/centernet_resnet50_v1_fpn_512x512_kpts_coco17_tpu-8.config +++ /dev/null @@ -1,392 +0,0 @@ -# CenterNet meta-architecture from the "Objects as Points" [1] paper -# with the ResNet-v1-50 backbone. The ResNet backbone has a few differences -# as compared to the one mentioned in the paper, hence the performance is -# slightly worse. This config is TPU comptatible. -# [1]: https://arxiv.org/abs/1904.07850 -# - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "resnet_v1_50_fpn" - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - keypoint_label_map_path: "PATH_TO_BE_CONFIGURED" - keypoint_estimation_task { - task_name: "human_pose" - task_loss_weight: 1.0 - loss { - localization_loss { - l1_localization_loss { - } - } - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - keypoint_class_name: "/m/01g317" - keypoint_label_to_std { - key: "left_ankle" - value: 0.89 - } - keypoint_label_to_std { - key: "left_ear" - value: 0.35 - } - keypoint_label_to_std { - key: "left_elbow" - value: 0.72 - } - keypoint_label_to_std { - key: "left_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "left_hip" - value: 1.07 - } - keypoint_label_to_std { - key: "left_knee" - value: 0.89 - } - keypoint_label_to_std { - key: "left_shoulder" - value: 0.79 - } - keypoint_label_to_std { - key: "left_wrist" - value: 0.62 - } - keypoint_label_to_std { - key: "nose" - value: 0.26 - } - keypoint_label_to_std { - key: "right_ankle" - value: 0.89 - } - keypoint_label_to_std { - key: "right_ear" - value: 0.35 - } - keypoint_label_to_std { - key: "right_elbow" - value: 0.72 - } - keypoint_label_to_std { - key: "right_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "right_hip" - value: 1.07 - } - keypoint_label_to_std { - key: "right_knee" - value: 0.89 - } - keypoint_label_to_std { - key: "right_shoulder" - value: 0.79 - } - keypoint_label_to_std { - key: "right_wrist" - value: 0.62 - } - keypoint_regression_loss_weight: 0.1 - keypoint_heatmap_loss_weight: 1.0 - keypoint_offset_loss_weight: 1.0 - offset_peak_radius: 3 - per_keypoint_offset: true - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 250000 - - data_augmentation_options { - random_horizontal_flip { - keypoint_flip_permutation: 0 - keypoint_flip_permutation: 2 - keypoint_flip_permutation: 1 - keypoint_flip_permutation: 4 - keypoint_flip_permutation: 3 - keypoint_flip_permutation: 6 - keypoint_flip_permutation: 5 - keypoint_flip_permutation: 8 - keypoint_flip_permutation: 7 - keypoint_flip_permutation: 10 - keypoint_flip_permutation: 9 - keypoint_flip_permutation: 12 - keypoint_flip_permutation: 11 - keypoint_flip_permutation: 14 - keypoint_flip_permutation: 13 - keypoint_flip_permutation: 16 - keypoint_flip_permutation: 15 - } - } - - data_augmentation_options { - random_crop_image { - min_aspect_ratio: 0.5 - max_aspect_ratio: 1.7 - random_coef: 0.25 - } - } - - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_absolute_pad_image { - max_height_padding: 200 - max_width_padding: 200 - pad_color: [0, 0, 0] - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 250000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED" - fine_tune_checkpoint_type: "classification" -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } - num_keypoints: 17 -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - num_visualizations: 10 - max_num_boxes_to_visualize: 20 - min_score_threshold: 0.2 - batch_size: 1; - parameterized_metric { - coco_keypoint_metrics { - class_label: "person" - keypoint_label_to_sigmas { - key: "nose" - value: 0.026 - } - keypoint_label_to_sigmas { - key: "left_eye" - value: 0.025 - } - keypoint_label_to_sigmas { - key: "right_eye" - value: 0.025 - } - keypoint_label_to_sigmas { - key: "left_ear" - value: 0.035 - } - keypoint_label_to_sigmas { - key: "right_ear" - value: 0.035 - } - keypoint_label_to_sigmas { - key: "left_shoulder" - value: 0.079 - } - keypoint_label_to_sigmas { - key: "right_shoulder" - value: 0.079 - } - keypoint_label_to_sigmas { - key: "left_elbow" - value: 0.072 - } - keypoint_label_to_sigmas { - key: "right_elbow" - value: 0.072 - } - keypoint_label_to_sigmas { - key: "left_wrist" - value: 0.062 - } - keypoint_label_to_sigmas { - key: "right_wrist" - value: 0.062 - } - keypoint_label_to_sigmas { - key: "left_hip" - value: 0.107 - } - keypoint_label_to_sigmas { - key: "right_hip" - value: 0.107 - } - keypoint_label_to_sigmas { - key: "left_knee" - value: 0.087 - } - keypoint_label_to_sigmas { - key: "right_knee" - value: 0.087 - } - keypoint_label_to_sigmas { - key: "left_ankle" - value: 0.089 - } - keypoint_label_to_sigmas { - key: "right_ankle" - value: 0.089 - } - } - } - # Provide the edges to connect the keypoints. The setting is suitable for - # COCO's 17 human pose keypoints. - keypoint_edge { # nose-left eye - start: 0 - end: 1 - } - keypoint_edge { # nose-right eye - start: 0 - end: 2 - } - keypoint_edge { # left eye-left ear - start: 1 - end: 3 - } - keypoint_edge { # right eye-right ear - start: 2 - end: 4 - } - keypoint_edge { # nose-left shoulder - start: 0 - end: 5 - } - keypoint_edge { # nose-right shoulder - start: 0 - end: 6 - } - keypoint_edge { # left shoulder-left elbow - start: 5 - end: 7 - } - keypoint_edge { # left elbow-left wrist - start: 7 - end: 9 - } - keypoint_edge { # right shoulder-right elbow - start: 6 - end: 8 - } - keypoint_edge { # right elbow-right wrist - start: 8 - end: 10 - } - keypoint_edge { # left shoulder-right shoulder - start: 5 - end: 6 - } - keypoint_edge { # left shoulder-left hip - start: 5 - end: 11 - } - keypoint_edge { # right shoulder-right hip - start: 6 - end: 12 - } - keypoint_edge { # left hip-right hip - start: 11 - end: 12 - } - keypoint_edge { # left hip-left knee - start: 11 - end: 13 - } - keypoint_edge { # left knee-left ankle - start: 13 - end: 15 - } - keypoint_edge { # right hip-right knee - start: 12 - end: 14 - } - keypoint_edge { # right knee-right ankle - start: 14 - end: 16 - } -} -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } - num_keypoints: 17 -} diff --git a/research/object_detection/configs/tf2/centernet_resnet50_v2_512x512_kpts_coco17_tpu-8.config b/research/object_detection/configs/tf2/centernet_resnet50_v2_512x512_kpts_coco17_tpu-8.config deleted file mode 100644 index 3067ed417b1..00000000000 --- a/research/object_detection/configs/tf2/centernet_resnet50_v2_512x512_kpts_coco17_tpu-8.config +++ /dev/null @@ -1,393 +0,0 @@ -# CenterNet meta-architecture from the "Objects as Points" [1] paper -# with the ResNet-v2-50 backbone. The ResNet backbone has a few differences -# as compared to the one mentioned in the paper, hence the performance is -# slightly worse. This config is TPU comptatible. -# [1]: https://arxiv.org/abs/1904.07850 - -model { - center_net { - num_classes: 90 - feature_extractor { - type: "resnet_v2_50" - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - object_detection_task { - task_loss_weight: 1.0 - offset_loss_weight: 1.0 - scale_loss_weight: 0.1 - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - object_center_loss_weight: 1.0 - min_box_overlap_iou: 0.7 - max_box_predictions: 100 - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - - keypoint_label_map_path: "PATH_TO_BE_CONFIGURED" - keypoint_estimation_task { - task_name: "human_pose" - task_loss_weight: 1.0 - loss { - localization_loss { - l1_localization_loss { - } - } - classification_loss { - penalty_reduced_logistic_focal_loss { - alpha: 2.0 - beta: 4.0 - } - } - } - keypoint_class_name: "/m/01g317" - keypoint_label_to_std { - key: "left_ankle" - value: 0.89 - } - keypoint_label_to_std { - key: "left_ear" - value: 0.35 - } - keypoint_label_to_std { - key: "left_elbow" - value: 0.72 - } - keypoint_label_to_std { - key: "left_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "left_hip" - value: 1.07 - } - keypoint_label_to_std { - key: "left_knee" - value: 0.89 - } - keypoint_label_to_std { - key: "left_shoulder" - value: 0.79 - } - keypoint_label_to_std { - key: "left_wrist" - value: 0.62 - } - keypoint_label_to_std { - key: "nose" - value: 0.26 - } - keypoint_label_to_std { - key: "right_ankle" - value: 0.89 - } - keypoint_label_to_std { - key: "right_ear" - value: 0.35 - } - keypoint_label_to_std { - key: "right_elbow" - value: 0.72 - } - keypoint_label_to_std { - key: "right_eye" - value: 0.25 - } - keypoint_label_to_std { - key: "right_hip" - value: 1.07 - } - keypoint_label_to_std { - key: "right_knee" - value: 0.89 - } - keypoint_label_to_std { - key: "right_shoulder" - value: 0.79 - } - keypoint_label_to_std { - key: "right_wrist" - value: 0.62 - } - keypoint_regression_loss_weight: 0.1 - keypoint_heatmap_loss_weight: 1.0 - keypoint_offset_loss_weight: 1.0 - offset_peak_radius: 3 - per_keypoint_offset: true - } - } -} - -train_config: { - - batch_size: 128 - num_steps: 250000 - - data_augmentation_options { - random_horizontal_flip { - keypoint_flip_permutation: 0 - keypoint_flip_permutation: 2 - keypoint_flip_permutation: 1 - keypoint_flip_permutation: 4 - keypoint_flip_permutation: 3 - keypoint_flip_permutation: 6 - keypoint_flip_permutation: 5 - keypoint_flip_permutation: 8 - keypoint_flip_permutation: 7 - keypoint_flip_permutation: 10 - keypoint_flip_permutation: 9 - keypoint_flip_permutation: 12 - keypoint_flip_permutation: 11 - keypoint_flip_permutation: 14 - keypoint_flip_permutation: 13 - keypoint_flip_permutation: 16 - keypoint_flip_permutation: 15 - } - } - - data_augmentation_options { - random_crop_image { - min_aspect_ratio: 0.5 - max_aspect_ratio: 1.7 - random_coef: 0.25 - } - } - - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_adjust_brightness { - } - } - - data_augmentation_options { - random_absolute_pad_image { - max_height_padding: 200 - max_width_padding: 200 - pad_color: [0, 0, 0] - } - } - - optimizer { - adam_optimizer: { - epsilon: 1e-7 # Match tf.keras.optimizers.Adam's default. - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 1e-3 - total_steps: 250000 - warmup_learning_rate: 2.5e-4 - warmup_steps: 5000 - } - } - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED" - fine_tune_checkpoint_type: "classification" -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } - num_keypoints: 17 -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - num_visualizations: 10 - max_num_boxes_to_visualize: 20 - min_score_threshold: 0.2 - batch_size: 1; - parameterized_metric { - coco_keypoint_metrics { - class_label: "person" - keypoint_label_to_sigmas { - key: "nose" - value: 0.026 - } - keypoint_label_to_sigmas { - key: "left_eye" - value: 0.025 - } - keypoint_label_to_sigmas { - key: "right_eye" - value: 0.025 - } - keypoint_label_to_sigmas { - key: "left_ear" - value: 0.035 - } - keypoint_label_to_sigmas { - key: "right_ear" - value: 0.035 - } - keypoint_label_to_sigmas { - key: "left_shoulder" - value: 0.079 - } - keypoint_label_to_sigmas { - key: "right_shoulder" - value: 0.079 - } - keypoint_label_to_sigmas { - key: "left_elbow" - value: 0.072 - } - keypoint_label_to_sigmas { - key: "right_elbow" - value: 0.072 - } - keypoint_label_to_sigmas { - key: "left_wrist" - value: 0.062 - } - keypoint_label_to_sigmas { - key: "right_wrist" - value: 0.062 - } - keypoint_label_to_sigmas { - key: "left_hip" - value: 0.107 - } - keypoint_label_to_sigmas { - key: "right_hip" - value: 0.107 - } - keypoint_label_to_sigmas { - key: "left_knee" - value: 0.087 - } - keypoint_label_to_sigmas { - key: "right_knee" - value: 0.087 - } - keypoint_label_to_sigmas { - key: "left_ankle" - value: 0.089 - } - keypoint_label_to_sigmas { - key: "right_ankle" - value: 0.089 - } - } - } - # Provide the edges to connect the keypoints. The setting is suitable for - # COCO's 17 human pose keypoints. - keypoint_edge { # nose-left eye - start: 0 - end: 1 - } - keypoint_edge { # nose-right eye - start: 0 - end: 2 - } - keypoint_edge { # left eye-left ear - start: 1 - end: 3 - } - keypoint_edge { # right eye-right ear - start: 2 - end: 4 - } - keypoint_edge { # nose-left shoulder - start: 0 - end: 5 - } - keypoint_edge { # nose-right shoulder - start: 0 - end: 6 - } - keypoint_edge { # left shoulder-left elbow - start: 5 - end: 7 - } - keypoint_edge { # left elbow-left wrist - start: 7 - end: 9 - } - keypoint_edge { # right shoulder-right elbow - start: 6 - end: 8 - } - keypoint_edge { # right elbow-right wrist - start: 8 - end: 10 - } - keypoint_edge { # left shoulder-right shoulder - start: 5 - end: 6 - } - keypoint_edge { # left shoulder-left hip - start: 5 - end: 11 - } - keypoint_edge { # right shoulder-right hip - start: 6 - end: 12 - } - keypoint_edge { # left hip-right hip - start: 11 - end: 12 - } - keypoint_edge { # left hip-left knee - start: 11 - end: 13 - } - keypoint_edge { # left knee-left ankle - start: 13 - end: 15 - } - keypoint_edge { # right hip-right knee - start: 12 - end: 14 - } - keypoint_edge { # right knee-right ankle - start: 14 - end: 16 - } -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } - num_keypoints: 17 -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_1024x1024_coco17_tpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_1024x1024_coco17_tpu-8.config deleted file mode 100644 index c38f6b9e214..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_1024x1024_coco17_tpu-8.config +++ /dev/null @@ -1,166 +0,0 @@ -# Faster R-CNN with Resnet-101 (v1), -# w/high res inputs, long training schedule -# Trained on COCO, initialized from Imagenet classification checkpoint -# -# Train on TPU-8 -# -# Achieves 37.1 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - fixed_shape_resizer { - width: 1024 - height: 1024 - } - } - feature_extractor { - type: 'faster_rcnn_resnet101_keras' - batch_norm_trainable: true - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - share_box_across_classes: true - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - use_static_shapes: true - use_matmul_crop_and_resize: true - clip_anchors_to_image: true - use_static_balanced_label_sampler: true - use_matmul_gather_in_matcher: true - } -} - -train_config: { - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 100000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 100000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet101.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - use_bfloat16: true # works only on TPUs -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_640x640_coco17_tpu-8.config deleted file mode 100644 index af07c7df627..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,145 +0,0 @@ -# Faster R-CNN with Resnet-50 (v1) -# Trained on COCO, initialized from Imagenet classification checkpoint -# -# Train on TPU-8 -# -# Achieves 31.8 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 640 - max_dimension: 640 - pad_to_max_dimension: true - } - } - feature_extractor { - type: 'faster_rcnn_resnet101_keras' - batch_norm_trainable: true - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - share_box_across_classes: true - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - use_static_shapes: true - use_matmul_crop_and_resize: true - clip_anchors_to_image: true - use_static_balanced_label_sampler: true - use_matmul_gather_in_matcher: true - } -} - -train_config: { - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 25000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet101.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - use_bfloat16: true # works only on TPUs -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8.config deleted file mode 100644 index 8eb4da02f59..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8.config +++ /dev/null @@ -1,154 +0,0 @@ -# Faster R-CNN with Resnet-101 (v1), -# Initialized from Imagenet classification checkpoint -# -# Train on GPU-8 -# -# Achieves 36.6 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 800 - max_dimension: 1333 - pad_to_max_dimension: true - } - } - feature_extractor { - type: 'faster_rcnn_resnet101_keras' - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - } -} - -train_config: { - batch_size: 16 - num_steps: 200000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 0.01 - total_steps: 200000 - warmup_learning_rate: 0.0 - warmup_steps: 5000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - gradient_clipping_by_norm: 10.0 - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet101.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_1024x1024_coco17_tpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_1024x1024_coco17_tpu-8.config deleted file mode 100644 index 034667ffe38..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_1024x1024_coco17_tpu-8.config +++ /dev/null @@ -1,166 +0,0 @@ -# Faster R-CNN with Resnet-152 (v1) -# w/high res inputs, long training schedule -# Trained on COCO, initialized from Imagenet classification checkpoint -# -# Train on TPU-8 -# -# Achieves 37.6 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - fixed_shape_resizer { - width: 1024 - height: 1024 - } - } - feature_extractor { - type: 'faster_rcnn_resnet152_keras' - batch_norm_trainable: true - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - share_box_across_classes: true - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - use_static_shapes: true - use_matmul_crop_and_resize: true - clip_anchors_to_image: true - use_static_balanced_label_sampler: true - use_matmul_gather_in_matcher: true - } -} - -train_config: { - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 100000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 100000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet152.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - use_bfloat16: true # works only on TPUs -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_640x640_coco17_tpu-8.config deleted file mode 100644 index 525c4ac456a..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,145 +0,0 @@ -# Faster R-CNN with Resnet-152 (v1) -# Trained on COCO, initialized from Imagenet classification checkpoint -# -# Train on TPU-8 -# -# Achieves 32.4 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 640 - max_dimension: 640 - pad_to_max_dimension: true - } - } - feature_extractor { - type: 'faster_rcnn_resnet152_keras' - batch_norm_trainable: true - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - share_box_across_classes: true - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - use_static_shapes: true - use_matmul_crop_and_resize: true - clip_anchors_to_image: true - use_static_balanced_label_sampler: true - use_matmul_gather_in_matcher: true - } -} - -train_config: { - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 25000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet152.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - use_bfloat16: true # works only on TPUs -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8.config deleted file mode 100644 index 8d1879f7b9b..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8.config +++ /dev/null @@ -1,154 +0,0 @@ -# Faster R-CNN with Resnet-152 (v1), -# Initialized from Imagenet classification checkpoint -# -# Train on GPU-8 -# -# Achieves 37.3 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 800 - max_dimension: 1333 - pad_to_max_dimension: true - } - } - feature_extractor { - type: 'faster_rcnn_resnet152_keras' - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - } -} - -train_config: { - batch_size: 16 - num_steps: 200000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 0.01 - total_steps: 200000 - warmup_learning_rate: 0.0 - warmup_steps: 5000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - gradient_clipping_by_norm: 10.0 - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet152.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_1024x1024_coco17_tpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_1024x1024_coco17_tpu-8.config deleted file mode 100644 index b6e590ee717..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_1024x1024_coco17_tpu-8.config +++ /dev/null @@ -1,166 +0,0 @@ -# Faster R-CNN with Resnet-50 (v1), -# w/high res inputs, long training schedule -# Trained on COCO, initialized from Imagenet classification checkpoint -# -# Train on TPU-8 -# -# Achieves 31.0 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - fixed_shape_resizer { - width: 1024 - height: 1024 - } - } - feature_extractor { - type: 'faster_rcnn_resnet50_keras' - batch_norm_trainable: true - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - share_box_across_classes: true - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - use_static_shapes: true - use_matmul_crop_and_resize: true - clip_anchors_to_image: true - use_static_balanced_label_sampler: true - use_matmul_gather_in_matcher: true - } -} - -train_config: { - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 100000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 100000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet50.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - use_bfloat16: true # works only on TPUs -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.config deleted file mode 100644 index c8601c6fed1..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,145 +0,0 @@ -# Faster R-CNN with Resnet-50 (v1) with 640x640 input resolution -# Trained on COCO, initialized from Imagenet classification checkpoint -# -# Train on TPU-8 -# -# Achieves 29.3 mAP on COCO17 Val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 640 - max_dimension: 640 - pad_to_max_dimension: true - } - } - feature_extractor { - type: 'faster_rcnn_resnet50_keras' - batch_norm_trainable: true - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - share_box_across_classes: true - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - use_static_shapes: true - use_matmul_crop_and_resize: true - clip_anchors_to_image: true - use_static_balanced_label_sampler: true - use_matmul_gather_in_matcher: true - } -} - -train_config: { - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 25000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet50.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - use_bfloat16: true # works only on TPUs -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_800x1333_coco17_gpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_800x1333_coco17_gpu-8.config deleted file mode 100644 index 264be5f0b79..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_800x1333_coco17_gpu-8.config +++ /dev/null @@ -1,154 +0,0 @@ -# Faster R-CNN with Resnet-50 (v1), -# Initialized from Imagenet classification checkpoint -# -# Train on GPU-8 -# -# Achieves 31.4 mAP on COCO17 val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 800 - max_dimension: 1333 - pad_to_max_dimension: true - } - } - feature_extractor { - type: 'faster_rcnn_resnet50_keras' - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - } -} - -train_config: { - batch_size: 16 - num_steps: 200000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 0.01 - total_steps: 200000 - warmup_learning_rate: 0.0 - warmup_steps: 5000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - gradient_clipping_by_norm: 10.0 - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet50.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - data_augmentation_options { - random_adjust_hue { - } - } - - data_augmentation_options { - random_adjust_contrast { - } - } - - data_augmentation_options { - random_adjust_saturation { - } - } - - data_augmentation_options { - random_square_crop_by_scale { - scale_min: 0.6 - scale_max: 1.3 - } - } -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_fpn_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_fpn_640x640_coco17_tpu-8.config deleted file mode 100644 index acb5a91359b..00000000000 --- a/research/object_detection/configs/tf2/faster_rcnn_resnet50_v1_fpn_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,173 +0,0 @@ -# Faster RCNN with Resnet 50 v1 FPN feature extractor. -# See Lin et al, https://arxiv.org/abs/1612.03144 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 31.4 mAP on COCO17 Val - -model { - faster_rcnn { - num_classes: 90 - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 640 - max_dimension: 640 - pad_to_max_dimension: true - } - } - feature_extractor { - type: 'faster_rcnn_resnet50_fpn_keras' - batch_norm_trainable: true - fpn { - min_level: 2 - max_level: 6 - } - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - first_stage_anchor_generator { - multiscale_anchor_generator { - min_level: 2 - max_level: 6 - # According to the origial paper the value should be 8.0 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - # According to the original paper the value should be 1 - scales_per_octave: 2 - normalize_coordinates: false - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - # According to the origial paper, value should be 7. - initial_crop_size: 14 - maxpool_kernel_size: 2 - maxpool_stride: 2 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 300 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - use_static_shapes: true - use_matmul_crop_and_resize: true - clip_anchors_to_image: true - use_static_balanced_label_sampler: true - use_matmul_gather_in_matcher: true - } -} - -train_config: { - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 25000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 0.04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet50.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } - - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false - use_bfloat16: true -} - -train_input_reader: { - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8.config b/research/object_detection/configs/tf2/mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8.config deleted file mode 100644 index 974c1d1710b..00000000000 --- a/research/object_detection/configs/tf2/mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8.config +++ /dev/null @@ -1,160 +0,0 @@ -# Mask R-CNN with Inception Resnet v2 (no atrous) -# Sync-trained on COCO (with 8 GPUs) with batch size 16 (1024x1024 resolution) -# Initialized from Imagenet classification checkpoint -# -# Train on GPU-8 -# -# Achieves 40.4 box mAP and 35.5 mask mAP on COCO17 val - -model { - faster_rcnn { - number_of_stages: 3 - num_classes: 90 - image_resizer { - fixed_shape_resizer { - height: 1024 - width: 1024 - } - } - feature_extractor { - type: 'faster_rcnn_inception_resnet_v2_keras' - } - first_stage_anchor_generator { - grid_anchor_generator { - scales: [0.25, 0.5, 1.0, 2.0] - aspect_ratios: [0.5, 1.0, 2.0] - height_stride: 16 - width_stride: 16 - } - } - first_stage_box_predictor_conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - first_stage_nms_score_threshold: 0.0 - first_stage_nms_iou_threshold: 0.7 - first_stage_max_proposals: 300 - first_stage_localization_loss_weight: 2.0 - first_stage_objectness_loss_weight: 1.0 - initial_crop_size: 17 - maxpool_kernel_size: 1 - maxpool_stride: 1 - second_stage_box_predictor { - mask_rcnn_box_predictor { - use_dropout: false - dropout_keep_probability: 1.0 - fc_hyperparams { - op: FC - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - mask_height: 33 - mask_width: 33 - mask_prediction_conv_depth: 0 - mask_prediction_num_conv_layers: 4 - conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - predict_instance_masks: true - } - } - second_stage_post_processing { - batch_non_max_suppression { - score_threshold: 0.0 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SOFTMAX - } - second_stage_localization_loss_weight: 2.0 - second_stage_classification_loss_weight: 1.0 - second_stage_mask_prediction_loss_weight: 4.0 - resize_masks: false - } -} - -train_config: { - batch_size: 16 - num_steps: 200000 - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 0.008 - total_steps: 200000 - warmup_learning_rate: 0.0 - warmup_steps: 5000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - gradient_clipping_by_norm: 10.0 - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/inception_resnet_v2.ckpt-1" - fine_tune_checkpoint_type: "classification" - data_augmentation_options { - random_horizontal_flip { - } - } -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } - load_instance_masks: true - mask_type: PNG_MASKS -} - -eval_config: { - metrics_set: "coco_detection_metrics" - metrics_set: "coco_mask_metrics" - eval_instance_masks: true - use_moving_averages: false - batch_size: 1 - include_metrics_per_category: true -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } - load_instance_masks: true - mask_type: PNG_MASKS -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d0_512x512_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_efficientdet_d0_512x512_coco17_tpu-8.config deleted file mode 100644 index ffcd461f77f..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d0_512x512_coco17_tpu-8.config +++ /dev/null @@ -1,199 +0,0 @@ - # SSD with EfficientNet-b0 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d0). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b0 checkpoint. -# -# Train on TPU-8 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 512 - max_dimension: 512 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 64 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 3 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b0_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 3 - num_filters: 64 - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 512 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d1_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_efficientdet_d1_640x640_coco17_tpu-8.config deleted file mode 100644 index 5eacfeda854..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d1_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,199 +0,0 @@ - # SSD with EfficientNet-b1 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d1). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b1 checkpoint. -# -# Train on TPU-8 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 640 - max_dimension: 640 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 88 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 3 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b1_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 4 - num_filters: 88 - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 640 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d2_768x768_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_efficientdet_d2_768x768_coco17_tpu-8.config deleted file mode 100644 index d2ca75d468c..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d2_768x768_coco17_tpu-8.config +++ /dev/null @@ -1,199 +0,0 @@ - # SSD with EfficientNet-b2 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d2). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b2 checkpoint. -# -# Train on TPU-8 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 768 - max_dimension: 768 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 112 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 3 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b2_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 5 - num_filters: 112 - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 768 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d3_896x896_coco17_tpu-32.config b/research/object_detection/configs/tf2/ssd_efficientdet_d3_896x896_coco17_tpu-32.config deleted file mode 100644 index b072d13a89f..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d3_896x896_coco17_tpu-32.config +++ /dev/null @@ -1,199 +0,0 @@ - # SSD with EfficientNet-b3 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d3). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b3 checkpoint. -# -# Train on TPU-32 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 896 - max_dimension: 896 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 160 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b3_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 6 - num_filters: 160 - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 896 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d4_1024x1024_coco17_tpu-32.config b/research/object_detection/configs/tf2/ssd_efficientdet_d4_1024x1024_coco17_tpu-32.config deleted file mode 100644 index b13b2d46974..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d4_1024x1024_coco17_tpu-32.config +++ /dev/null @@ -1,199 +0,0 @@ - # SSD with EfficientNet-b4 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d4). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b4 checkpoint. -# -# Train on TPU-32 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1024 - max_dimension: 1024 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 224 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b4_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 7 - num_filters: 224 - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 1024 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d5_1280x1280_coco17_tpu-32.config b/research/object_detection/configs/tf2/ssd_efficientdet_d5_1280x1280_coco17_tpu-32.config deleted file mode 100644 index bcb33d50300..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d5_1280x1280_coco17_tpu-32.config +++ /dev/null @@ -1,199 +0,0 @@ - # SSD with EfficientNet-b5 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d5). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b5 checkpoint. -# -# Train on TPU-32 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1280 - max_dimension: 1280 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 288 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b5_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 7 - num_filters: 288 - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 1280 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d6_1408x1408_coco17_tpu-32.config b/research/object_detection/configs/tf2/ssd_efficientdet_d6_1408x1408_coco17_tpu-32.config deleted file mode 100644 index 1f24607431c..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d6_1408x1408_coco17_tpu-32.config +++ /dev/null @@ -1,201 +0,0 @@ - # SSD with EfficientNet-b6 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d6). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b6 checkpoint. -# -# Train on TPU-32 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1408 - max_dimension: 1408 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 384 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 5 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b6_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 8 - num_filters: 384 - # Use unweighted sum for stability. - combine_method: 'sum' - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 1408 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_efficientdet_d7_1536x1536_coco17_tpu-32.config b/research/object_detection/configs/tf2/ssd_efficientdet_d7_1536x1536_coco17_tpu-32.config deleted file mode 100644 index 81954aa8bdd..00000000000 --- a/research/object_detection/configs/tf2/ssd_efficientdet_d7_1536x1536_coco17_tpu-32.config +++ /dev/null @@ -1,201 +0,0 @@ - # SSD with EfficientNet-b6 + BiFPN feature extractor, -# shared box predictor and focal loss (a.k.a EfficientDet-d7). -# See EfficientDet, Tan et al, https://arxiv.org/abs/1911.09070 -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from an EfficientNet-b6 checkpoint. -# -# Train on TPU-32 - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - add_background_class: false - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 3 - } - } - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 1536 - max_dimension: 1536 - pad_to_max_dimension: true - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 384 - class_prediction_bias_init: -4.6 - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true - decay: 0.99 - epsilon: 0.001 - } - } - num_layers_before_predictor: 5 - kernel_size: 3 - use_depthwise: true - } - } - feature_extractor { - type: 'ssd_efficientnet-b6_bifpn_keras' - bifpn { - min_level: 3 - max_level: 7 - num_iterations: 8 - num_filters: 384 - # Use unweighted sum for stability. - combine_method: 'sum' - } - conv_hyperparams { - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - } - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 1.5 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.5 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/ckpt-0" - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 300000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_scale_crop_and_pad_to_square { - output_size: 1536 - scale_min: 0.1 - scale_max: 2.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: 8e-2 - total_steps: 300000 - warmup_learning_rate: .001 - warmup_steps: 2500 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BEE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8.config deleted file mode 100644 index 3cfe304f171..00000000000 --- a/research/object_detection/configs/tf2/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Mobilenet v1 FPN feature extractor, shared box predictor and focal -# loss (a.k.a Retinanet). -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 29.1 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 640 - width: 640 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 256 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_mobilenet_v1_fpn_keras' - fpn { - min_level: 3 - max_level: 7 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/mobilenet_v1.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 25000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false - batch_size: 1; -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_mobilenet_v2_320x320_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_mobilenet_v2_320x320_coco17_tpu-8.config deleted file mode 100644 index dc3a4a7f3e7..00000000000 --- a/research/object_detection/configs/tf2/ssd_mobilenet_v2_320x320_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Mobilenet v2 -# Trained on COCO17, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 22.2 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - ssd_anchor_generator { - num_layers: 6 - min_scale: 0.2 - max_scale: 0.95 - aspect_ratios: 1.0 - aspect_ratios: 2.0 - aspect_ratios: 0.5 - aspect_ratios: 3.0 - aspect_ratios: 0.3333 - } - } - image_resizer { - fixed_shape_resizer { - height: 300 - width: 300 - } - } - box_predictor { - convolutional_box_predictor { - min_depth: 0 - max_depth: 0 - num_layers_before_predictor: 0 - use_dropout: false - dropout_keep_probability: 0.8 - kernel_size: 1 - box_code_size: 4 - apply_sigmoid_to_scores: false - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - train: true, - scale: true, - center: true, - decay: 0.97, - epsilon: 0.001, - } - } - } - } - feature_extractor { - type: 'ssd_mobilenet_v2_keras' - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - train: true, - scale: true, - center: true, - decay: 0.97, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.75, - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - delta: 1.0 - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/mobilenet_v2.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 512 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 50000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - ssd_random_crop { - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .8 - total_steps: 50000 - warmup_learning_rate: 0.13333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config deleted file mode 100644 index 656e324c5d9..00000000000 --- a/research/object_detection/configs/tf2/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.config +++ /dev/null @@ -1,201 +0,0 @@ -# SSD with Mobilenet v2 FPN-lite (go/fpn-lite) feature extractor, shared box -# predictor and focal loss (a mobile version of Retinanet). -# Retinanet: see Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 22.2 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 320 - width: 320 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 128 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - share_prediction_tower: true - use_depthwise: true - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_mobilenet_v2_fpn_keras' - use_depthwise: true - fpn { - min_level: 3 - max_level: 7 - additional_layer_depth: 128 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/mobilenet_v2.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 50000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .08 - total_steps: 50000 - warmup_learning_rate: .026666 - warmup_steps: 1000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} - diff --git a/research/object_detection/configs/tf2/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.config deleted file mode 100644 index 5e4bca1688c..00000000000 --- a/research/object_detection/configs/tf2/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,201 +0,0 @@ -# SSD with Mobilenet v2 FPN-lite (go/fpn-lite) feature extractor, shared box -# predictor and focal loss (a mobile version of Retinanet). -# Retinanet: see Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 28.2 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 640 - width: 640 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 128 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - share_prediction_tower: true - use_depthwise: true - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_mobilenet_v2_fpn_keras' - use_depthwise: true - fpn { - min_level: 3 - max_level: 7 - additional_layer_depth: 128 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/mobilenet_v2.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 128 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - num_steps: 50000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .08 - total_steps: 50000 - warmup_learning_rate: .026666 - warmup_steps: 1000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} - diff --git a/research/object_detection/configs/tf2/ssd_resnet101_v1_fpn_1024x1024_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_resnet101_v1_fpn_1024x1024_coco17_tpu-8.config deleted file mode 100644 index 015617ba444..00000000000 --- a/research/object_detection/configs/tf2/ssd_resnet101_v1_fpn_1024x1024_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Resnet 101 v1 FPN feature extractor, shared box predictor and focal -# loss (a.k.a Retinanet). -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 39.5 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 1024 - width: 1024 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 256 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_resnet101_v1_fpn_keras' - fpn { - min_level: 3 - max_level: 7 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet101.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 100000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 100000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.config deleted file mode 100644 index 37e9b9b632c..00000000000 --- a/research/object_detection/configs/tf2/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Resnet 101 v1 FPN feature extractor, shared box predictor and focal -# loss (a.k.a Retinanet). -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 35.4 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 640 - width: 640 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 256 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_resnet101_v1_fpn_keras' - fpn { - min_level: 3 - max_level: 7 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet101.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 25000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_resnet152_v1_fpn_1024x1024_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_resnet152_v1_fpn_1024x1024_coco17_tpu-8.config deleted file mode 100644 index 9dbc06e3d72..00000000000 --- a/research/object_detection/configs/tf2/ssd_resnet152_v1_fpn_1024x1024_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Resnet 152 v1 FPN feature extractor, shared box predictor and focal -# loss (a.k.a Retinanet). -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 39.6 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 1024 - width: 1024 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 256 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_resnet152_v1_fpn_keras' - fpn { - min_level: 3 - max_level: 7 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet152.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 100000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 100000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_resnet152_v1_fpn_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_resnet152_v1_fpn_640x640_coco17_tpu-8.config deleted file mode 100644 index aa99f0a115e..00000000000 --- a/research/object_detection/configs/tf2/ssd_resnet152_v1_fpn_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Resnet 152 v1 FPN feature extractor, shared box predictor and focal -# loss (a.k.a Retinanet). -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 35.6 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 640 - width: 640 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 256 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_resnet152_v1_fpn_keras' - fpn { - min_level: 3 - max_level: 7 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet152.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 25000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_1024x1024_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_1024x1024_coco17_tpu-8.config deleted file mode 100644 index e1575a00299..00000000000 --- a/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_1024x1024_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Resnet 50 v1 FPN feature extractor, shared box predictor and focal -# loss (a.k.a Retinanet). -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 38.3 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 1024 - width: 1024 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 256 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_resnet50_v1_fpn_keras' - fpn { - min_level: 3 - max_level: 7 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet50.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 100000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 100000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.config b/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.config deleted file mode 100644 index 7164144b730..00000000000 --- a/research/object_detection/configs/tf2/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.config +++ /dev/null @@ -1,197 +0,0 @@ -# SSD with Resnet 50 v1 FPN feature extractor, shared box predictor and focal -# loss (a.k.a Retinanet). -# See Lin et al, https://arxiv.org/abs/1708.02002 -# Trained on COCO, initialized from Imagenet classification checkpoint -# Train on TPU-8 -# -# Achieves 34.3 mAP on COCO17 Val - -model { - ssd { - inplace_batchnorm_update: true - freeze_batchnorm: false - num_classes: 90 - box_coder { - faster_rcnn_box_coder { - y_scale: 10.0 - x_scale: 10.0 - height_scale: 5.0 - width_scale: 5.0 - } - } - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - similarity_calculator { - iou_similarity { - } - } - encode_background_as_zeros: true - anchor_generator { - multiscale_anchor_generator { - min_level: 3 - max_level: 7 - anchor_scale: 4.0 - aspect_ratios: [1.0, 2.0, 0.5] - scales_per_octave: 2 - } - } - image_resizer { - fixed_shape_resizer { - height: 640 - width: 640 - } - } - box_predictor { - weight_shared_convolutional_box_predictor { - depth: 256 - class_prediction_bias_init: -4.6 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - num_layers_before_predictor: 4 - kernel_size: 3 - } - } - feature_extractor { - type: 'ssd_resnet50_v1_fpn_keras' - fpn { - min_level: 3 - max_level: 7 - } - min_depth: 16 - depth_multiplier: 1.0 - conv_hyperparams { - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - } - override_base_feature_extractor_hyperparams: true - } - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - normalize_loss_by_num_matches: true - normalize_loc_loss_by_codesize: true - post_processing { - batch_non_max_suppression { - score_threshold: 1e-8 - iou_threshold: 0.6 - max_detections_per_class: 100 - max_total_detections: 100 - } - score_converter: SIGMOID - } - } -} - -train_config: { - fine_tune_checkpoint_version: V2 - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/resnet50.ckpt-1" - fine_tune_checkpoint_type: "classification" - batch_size: 64 - sync_replicas: true - startup_delay_steps: 0 - replicas_to_aggregate: 8 - use_bfloat16: true - num_steps: 25000 - data_augmentation_options { - random_horizontal_flip { - } - } - data_augmentation_options { - random_crop_image { - min_object_covered: 0.0 - min_aspect_ratio: 0.75 - max_aspect_ratio: 3.0 - min_area: 0.75 - max_area: 1.0 - overlap_thresh: 0.0 - } - } - optimizer { - momentum_optimizer: { - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - max_number_of_boxes: 100 - unpad_groundtruth_tensors: false -} - -train_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/train2017-?????-of-00256.tfrecord" - } -} - -eval_config: { - metrics_set: "coco_detection_metrics" - use_moving_averages: false -} - -eval_input_reader: { - label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt" - shuffle: false - num_epochs: 1 - tf_record_input_reader { - input_path: "PATH_TO_BE_CONFIGURED/val2017-?????-of-00032.tfrecord" - } -} diff --git a/research/object_detection/core/__init__.py b/research/object_detection/core/__init__.py deleted file mode 100644 index 8b137891791..00000000000 --- a/research/object_detection/core/__init__.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/research/object_detection/core/anchor_generator.py b/research/object_detection/core/anchor_generator.py deleted file mode 100644 index e896550a7e9..00000000000 --- a/research/object_detection/core/anchor_generator.py +++ /dev/null @@ -1,145 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base anchor generator. - -The job of the anchor generator is to create (or load) a collection -of bounding boxes to be used as anchors. - -Generated anchors are assumed to match some convolutional grid or list of grid -shapes. For example, we might want to generate anchors matching an 8x8 -feature map and a 4x4 feature map. If we place 3 anchors per grid location -on the first feature map and 6 anchors per grid location on the second feature -map, then 3*8*8 + 6*4*4 = 288 anchors are generated in total. - -To support fully convolutional settings, feature map shapes are passed -dynamically at generation time. The number of anchors to place at each location -is static --- implementations of AnchorGenerator must always be able return -the number of anchors that it uses per location for each feature map. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from abc import ABCMeta -from abc import abstractmethod - -import six -import tensorflow.compat.v1 as tf - - -class AnchorGenerator(six.with_metaclass(ABCMeta, object)): - """Abstract base class for anchor generators.""" - - @abstractmethod - def name_scope(self): - """Name scope. - - Must be defined by implementations. - - Returns: - a string representing the name scope of the anchor generation operation. - """ - pass - - @property - def check_num_anchors(self): - """Whether to dynamically check the number of anchors generated. - - Can be overridden by implementations that would like to disable this - behavior. - - Returns: - a boolean controlling whether the Generate function should dynamically - check the number of anchors generated against the mathematically - expected number of anchors. - """ - return True - - @abstractmethod - def num_anchors_per_location(self): - """Returns the number of anchors per spatial location. - - Returns: - a list of integers, one for each expected feature map to be passed to - the `generate` function. - """ - pass - - def generate(self, feature_map_shape_list, **params): - """Generates a collection of bounding boxes to be used as anchors. - - TODO(rathodv): remove **params from argument list and make stride and - offsets (for multiple_grid_anchor_generator) constructor arguments. - - Args: - feature_map_shape_list: list of (height, width) pairs in the format - [(height_0, width_0), (height_1, width_1), ...] that the generated - anchors must align with. Pairs can be provided as 1-dimensional - integer tensors of length 2 or simply as tuples of integers. - **params: parameters for anchor generation op - - Returns: - boxes_list: a list of BoxLists each holding anchor boxes corresponding to - the input feature map shapes. - - Raises: - ValueError: if the number of feature map shapes does not match the length - of NumAnchorsPerLocation. - """ - if self.check_num_anchors and ( - len(feature_map_shape_list) != len(self.num_anchors_per_location())): - raise ValueError('Number of feature maps is expected to equal the length ' - 'of `num_anchors_per_location`.') - with tf.name_scope(self.name_scope()): - anchors_list = self._generate(feature_map_shape_list, **params) - if self.check_num_anchors: - for item in anchors_list: - item.set(tf.identity(item.get())) - - return anchors_list - - @abstractmethod - def _generate(self, feature_map_shape_list, **params): - """To be overridden by implementations. - - Args: - feature_map_shape_list: list of (height, width) pairs in the format - [(height_0, width_0), (height_1, width_1), ...] that the generated - anchors must align with. - **params: parameters for anchor generation op - - Returns: - boxes_list: a list of BoxList, each holding a collection of N anchor - boxes. - """ - pass - - def anchor_index_to_feature_map_index(self, boxlist_list): - """Returns a 1-D array of feature map indices for each anchor. - - Args: - boxlist_list: a list of Boxlist, each holding a collection of N anchor - boxes. This list is produced in self.generate(). - - Returns: - A [num_anchors] integer array, where each element indicates which feature - map index the anchor belongs to. - """ - feature_map_indices_list = [] - for i, boxes in enumerate(boxlist_list): - feature_map_indices_list.append( - i * tf.ones([boxes.num_boxes()], dtype=tf.int32)) - return tf.concat(feature_map_indices_list, axis=0) diff --git a/research/object_detection/core/balanced_positive_negative_sampler.py b/research/object_detection/core/balanced_positive_negative_sampler.py deleted file mode 100644 index 6e09537d20e..00000000000 --- a/research/object_detection/core/balanced_positive_negative_sampler.py +++ /dev/null @@ -1,262 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Class to subsample minibatches by balancing positives and negatives. - -Subsamples minibatches based on a pre-specified positive fraction in range -[0,1]. The class presumes there are many more negatives than positive examples: -if the desired batch_size cannot be achieved with the pre-specified positive -fraction, it fills the rest with negative examples. If this is not sufficient -for obtaining the desired batch_size, it returns fewer examples. - -The main function to call is Subsample(self, indicator, labels). For convenience -one can also call SubsampleWeights(self, weights, labels) which is defined in -the minibatch_sampler base class. - -When is_static is True, it implements a method that guarantees static shapes. -It also ensures the length of output of the subsample is always batch_size, even -when number of examples set to True in indicator is less than batch_size. -""" - -import tensorflow.compat.v1 as tf - -from object_detection.core import minibatch_sampler - - -class BalancedPositiveNegativeSampler(minibatch_sampler.MinibatchSampler): - """Subsamples minibatches to a desired balance of positives and negatives.""" - - def __init__(self, positive_fraction=0.5, is_static=False): - """Constructs a minibatch sampler. - - Args: - positive_fraction: desired fraction of positive examples (scalar in [0,1]) - in the batch. - is_static: If True, uses an implementation with static shape guarantees. - - Raises: - ValueError: if positive_fraction < 0, or positive_fraction > 1 - """ - if positive_fraction < 0 or positive_fraction > 1: - raise ValueError('positive_fraction should be in range [0,1]. ' - 'Received: %s.' % positive_fraction) - self._positive_fraction = positive_fraction - self._is_static = is_static - - def _get_num_pos_neg_samples(self, sorted_indices_tensor, sample_size): - """Counts the number of positives and negatives numbers to be sampled. - - Args: - sorted_indices_tensor: A sorted int32 tensor of shape [N] which contains - the signed indices of the examples where the sign is based on the label - value. The examples that cannot be sampled are set to 0. It samples - atmost sample_size*positive_fraction positive examples and remaining - from negative examples. - sample_size: Size of subsamples. - - Returns: - A tuple containing the number of positive and negative labels in the - subsample. - """ - input_length = tf.shape(sorted_indices_tensor)[0] - valid_positive_index = tf.greater(sorted_indices_tensor, - tf.zeros(input_length, tf.int32)) - num_sampled_pos = tf.reduce_sum(tf.cast(valid_positive_index, tf.int32)) - max_num_positive_samples = tf.constant( - int(sample_size * self._positive_fraction), tf.int32) - num_positive_samples = tf.minimum(max_num_positive_samples, num_sampled_pos) - num_negative_samples = tf.constant(sample_size, - tf.int32) - num_positive_samples - - return num_positive_samples, num_negative_samples - - def _get_values_from_start_and_end(self, input_tensor, num_start_samples, - num_end_samples, total_num_samples): - """slices num_start_samples and last num_end_samples from input_tensor. - - Args: - input_tensor: An int32 tensor of shape [N] to be sliced. - num_start_samples: Number of examples to be sliced from the beginning - of the input tensor. - num_end_samples: Number of examples to be sliced from the end of the - input tensor. - total_num_samples: Sum of is num_start_samples and num_end_samples. This - should be a scalar. - - Returns: - A tensor containing the first num_start_samples and last num_end_samples - from input_tensor. - - """ - input_length = tf.shape(input_tensor)[0] - start_positions = tf.less(tf.range(input_length), num_start_samples) - end_positions = tf.greater_equal( - tf.range(input_length), input_length - num_end_samples) - selected_positions = tf.logical_or(start_positions, end_positions) - selected_positions = tf.cast(selected_positions, tf.float32) - indexed_positions = tf.multiply(tf.cumsum(selected_positions), - selected_positions) - one_hot_selector = tf.one_hot(tf.cast(indexed_positions, tf.int32) - 1, - total_num_samples, - dtype=tf.float32) - return tf.cast(tf.tensordot(tf.cast(input_tensor, tf.float32), - one_hot_selector, axes=[0, 0]), tf.int32) - - def _static_subsample(self, indicator, batch_size, labels): - """Returns subsampled minibatch. - - Args: - indicator: boolean tensor of shape [N] whose True entries can be sampled. - N should be a complie time constant. - batch_size: desired batch size. This scalar cannot be None. - labels: boolean tensor of shape [N] denoting positive(=True) and negative - (=False) examples. N should be a complie time constant. - - Returns: - sampled_idx_indicator: boolean tensor of shape [N], True for entries which - are sampled. It ensures the length of output of the subsample is always - batch_size, even when number of examples set to True in indicator is - less than batch_size. - - Raises: - ValueError: if labels and indicator are not 1D boolean tensors. - """ - # Check if indicator and labels have a static size. - if not indicator.shape.is_fully_defined(): - raise ValueError('indicator must be static in shape when is_static is' - 'True') - if not labels.shape.is_fully_defined(): - raise ValueError('labels must be static in shape when is_static is' - 'True') - if not isinstance(batch_size, int): - raise ValueError('batch_size has to be an integer when is_static is' - 'True.') - - input_length = tf.shape(indicator)[0] - - # Set the number of examples set True in indicator to be at least - # batch_size. - num_true_sampled = tf.reduce_sum(tf.cast(indicator, tf.float32)) - additional_false_sample = tf.less_equal( - tf.cumsum(tf.cast(tf.logical_not(indicator), tf.float32)), - batch_size - num_true_sampled) - indicator = tf.logical_or(indicator, additional_false_sample) - - # Shuffle indicator and label. Need to store the permutation to restore the - # order post sampling. - permutation = tf.random_shuffle(tf.range(input_length)) - indicator = tf.gather(indicator, permutation, axis=0) - labels = tf.gather(labels, permutation, axis=0) - - # index (starting from 1) when indicator is True, 0 when False - indicator_idx = tf.where( - indicator, tf.range(1, input_length + 1), - tf.zeros(input_length, tf.int32)) - - # Replace -1 for negative, +1 for positive labels - signed_label = tf.where( - labels, tf.ones(input_length, tf.int32), - tf.scalar_mul(-1, tf.ones(input_length, tf.int32))) - # negative of index for negative label, positive index for positive label, - # 0 when indicator is False. - signed_indicator_idx = tf.multiply(indicator_idx, signed_label) - sorted_signed_indicator_idx = tf.nn.top_k( - signed_indicator_idx, input_length, sorted=True).values - - [num_positive_samples, - num_negative_samples] = self._get_num_pos_neg_samples( - sorted_signed_indicator_idx, batch_size) - - sampled_idx = self._get_values_from_start_and_end( - sorted_signed_indicator_idx, num_positive_samples, - num_negative_samples, batch_size) - - # Shift the indices to start from 0 and remove any samples that are set as - # False. - sampled_idx = tf.abs(sampled_idx) - tf.ones(batch_size, tf.int32) - sampled_idx = tf.multiply( - tf.cast(tf.greater_equal(sampled_idx, tf.constant(0)), tf.int32), - sampled_idx) - - sampled_idx_indicator = tf.cast(tf.reduce_sum( - tf.one_hot(sampled_idx, depth=input_length), - axis=0), tf.bool) - - # project back the order based on stored permutations - idx_indicator = tf.scatter_nd( - tf.expand_dims(permutation, -1), sampled_idx_indicator, - shape=(input_length,)) - return idx_indicator - - def subsample(self, indicator, batch_size, labels, scope=None): - """Returns subsampled minibatch. - - Args: - indicator: boolean tensor of shape [N] whose True entries can be sampled. - batch_size: desired batch size. If None, keeps all positive samples and - randomly selects negative samples so that the positive sample fraction - matches self._positive_fraction. It cannot be None is is_static is True. - labels: boolean tensor of shape [N] denoting positive(=True) and negative - (=False) examples. - scope: name scope. - - Returns: - sampled_idx_indicator: boolean tensor of shape [N], True for entries which - are sampled. - - Raises: - ValueError: if labels and indicator are not 1D boolean tensors. - """ - if len(indicator.get_shape().as_list()) != 1: - raise ValueError('indicator must be 1 dimensional, got a tensor of ' - 'shape %s' % indicator.get_shape()) - if len(labels.get_shape().as_list()) != 1: - raise ValueError('labels must be 1 dimensional, got a tensor of ' - 'shape %s' % labels.get_shape()) - if labels.dtype != tf.bool: - raise ValueError('labels should be of type bool. Received: %s' % - labels.dtype) - if indicator.dtype != tf.bool: - raise ValueError('indicator should be of type bool. Received: %s' % - indicator.dtype) - with tf.name_scope(scope, 'BalancedPositiveNegativeSampler'): - if self._is_static: - return self._static_subsample(indicator, batch_size, labels) - - else: - # Only sample from indicated samples - negative_idx = tf.logical_not(labels) - positive_idx = tf.logical_and(labels, indicator) - negative_idx = tf.logical_and(negative_idx, indicator) - - # Sample positive and negative samples separately - if batch_size is None: - max_num_pos = tf.reduce_sum(tf.cast(positive_idx, dtype=tf.int32)) - else: - max_num_pos = int(self._positive_fraction * batch_size) - sampled_pos_idx = self.subsample_indicator(positive_idx, max_num_pos) - num_sampled_pos = tf.reduce_sum(tf.cast(sampled_pos_idx, tf.int32)) - if batch_size is None: - negative_positive_ratio = ( - 1 - self._positive_fraction) / self._positive_fraction - max_num_neg = tf.cast( - negative_positive_ratio * - tf.cast(num_sampled_pos, dtype=tf.float32), - dtype=tf.int32) - else: - max_num_neg = batch_size - num_sampled_pos - sampled_neg_idx = self.subsample_indicator(negative_idx, max_num_neg) - - return tf.logical_or(sampled_pos_idx, sampled_neg_idx) diff --git a/research/object_detection/core/balanced_positive_negative_sampler_test.py b/research/object_detection/core/balanced_positive_negative_sampler_test.py deleted file mode 100644 index 97cc8c0bb03..00000000000 --- a/research/object_detection/core/balanced_positive_negative_sampler_test.py +++ /dev/null @@ -1,212 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.balanced_positive_negative_sampler.""" - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import balanced_positive_negative_sampler -from object_detection.utils import test_case - - -class BalancedPositiveNegativeSamplerTest(test_case.TestCase): - - def test_subsample_all_examples(self): - if self.has_tpu(): return - numpy_labels = np.random.permutation(300) - indicator = np.array(np.ones(300) == 1, bool) - numpy_labels = (numpy_labels - 200) > 0 - - labels = np.array(numpy_labels, bool) - - def graph_fn(indicator, labels): - sampler = ( - balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) - return sampler.subsample(indicator, 64, labels) - - is_sampled = self.execute_cpu(graph_fn, [indicator, labels]) - self.assertEqual(sum(is_sampled), 64) - self.assertEqual(sum(np.logical_and(numpy_labels, is_sampled)), 32) - self.assertEqual(sum(np.logical_and( - np.logical_not(numpy_labels), is_sampled)), 32) - - def test_subsample_all_examples_static(self): - if not self.has_tpu(): return - numpy_labels = np.random.permutation(300) - indicator = np.array(np.ones(300) == 1, bool) - numpy_labels = (numpy_labels - 200) > 0 - - labels = np.array(numpy_labels, bool) - - def graph_fn(indicator, labels): - sampler = ( - balanced_positive_negative_sampler.BalancedPositiveNegativeSampler( - is_static=True)) - return sampler.subsample(indicator, 64, labels) - - is_sampled = self.execute_tpu(graph_fn, [indicator, labels]) - self.assertEqual(sum(is_sampled), 64) - self.assertEqual(sum(np.logical_and(numpy_labels, is_sampled)), 32) - self.assertEqual(sum(np.logical_and( - np.logical_not(numpy_labels), is_sampled)), 32) - - def test_subsample_selection(self): - if self.has_tpu(): return - # Test random sampling when only some examples can be sampled: - # 100 samples, 20 positives, 10 positives cannot be sampled. - numpy_labels = np.arange(100) - numpy_indicator = numpy_labels < 90 - indicator = np.array(numpy_indicator, bool) - numpy_labels = (numpy_labels - 80) >= 0 - - labels = np.array(numpy_labels, bool) - - def graph_fn(indicator, labels): - sampler = ( - balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) - return sampler.subsample(indicator, 64, labels) - - is_sampled = self.execute_cpu(graph_fn, [indicator, labels]) - self.assertEqual(sum(is_sampled), 64) - self.assertEqual(sum(np.logical_and(numpy_labels, is_sampled)), 10) - self.assertEqual(sum(np.logical_and( - np.logical_not(numpy_labels), is_sampled)), 54) - self.assertAllEqual(is_sampled, np.logical_and(is_sampled, numpy_indicator)) - - def test_subsample_selection_static(self): - if not self.has_tpu(): return - # Test random sampling when only some examples can be sampled: - # 100 samples, 20 positives, 10 positives cannot be sampled. - numpy_labels = np.arange(100) - numpy_indicator = numpy_labels < 90 - indicator = np.array(numpy_indicator, bool) - numpy_labels = (numpy_labels - 80) >= 0 - - labels = np.array(numpy_labels, bool) - - def graph_fn(indicator, labels): - sampler = ( - balanced_positive_negative_sampler.BalancedPositiveNegativeSampler( - is_static=True)) - return sampler.subsample(indicator, 64, labels) - - is_sampled = self.execute_tpu(graph_fn, [indicator, labels]) - self.assertEqual(sum(is_sampled), 64) - self.assertEqual(sum(np.logical_and(numpy_labels, is_sampled)), 10) - self.assertEqual(sum(np.logical_and( - np.logical_not(numpy_labels), is_sampled)), 54) - self.assertAllEqual(is_sampled, np.logical_and(is_sampled, numpy_indicator)) - - def test_subsample_selection_larger_batch_size(self): - if self.has_tpu(): return - # Test random sampling when total number of examples that can be sampled are - # less than batch size: - # 100 samples, 50 positives, 40 positives cannot be sampled, batch size 64. - # It should still return 64 samples, with 4 of them that couldn't have been - # sampled. - numpy_labels = np.arange(100) - numpy_indicator = numpy_labels < 60 - indicator = np.array(numpy_indicator, bool) - numpy_labels = (numpy_labels - 50) >= 0 - - labels = np.array(numpy_labels, bool) - - def graph_fn(indicator, labels): - sampler = ( - balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) - return sampler.subsample(indicator, 64, labels) - - is_sampled = self.execute_cpu(graph_fn, [indicator, labels]) - self.assertEqual(sum(is_sampled), 60) - self.assertGreaterEqual(sum(np.logical_and(numpy_labels, is_sampled)), 10) - self.assertGreaterEqual( - sum(np.logical_and(np.logical_not(numpy_labels), is_sampled)), 50) - self.assertEqual(sum(np.logical_and(is_sampled, numpy_indicator)), 60) - - def test_subsample_selection_larger_batch_size_static(self): - if not self.has_tpu(): return - # Test random sampling when total number of examples that can be sampled are - # less than batch size: - # 100 samples, 50 positives, 40 positives cannot be sampled, batch size 64. - # It should still return 64 samples, with 4 of them that couldn't have been - # sampled. - numpy_labels = np.arange(100) - numpy_indicator = numpy_labels < 60 - indicator = np.array(numpy_indicator, bool) - numpy_labels = (numpy_labels - 50) >= 0 - - labels = np.array(numpy_labels, bool) - - def graph_fn(indicator, labels): - sampler = ( - balanced_positive_negative_sampler.BalancedPositiveNegativeSampler( - is_static=True)) - return sampler.subsample(indicator, 64, labels) - - is_sampled = self.execute_tpu(graph_fn, [indicator, labels]) - self.assertEqual(sum(is_sampled), 64) - self.assertGreaterEqual(sum(np.logical_and(numpy_labels, is_sampled)), 10) - self.assertGreaterEqual( - sum(np.logical_and(np.logical_not(numpy_labels), is_sampled)), 50) - self.assertEqual(sum(np.logical_and(is_sampled, numpy_indicator)), 60) - - def test_subsample_selection_no_batch_size(self): - if self.has_tpu(): return - # Test random sampling when only some examples can be sampled: - # 1000 samples, 6 positives (5 can be sampled). - numpy_labels = np.arange(1000) - numpy_indicator = numpy_labels < 999 - numpy_labels = (numpy_labels - 994) >= 0 - - def graph_fn(indicator, labels): - sampler = (balanced_positive_negative_sampler. - BalancedPositiveNegativeSampler(0.01)) - is_sampled = sampler.subsample(indicator, None, labels) - return is_sampled - is_sampled_out = self.execute_cpu(graph_fn, [numpy_indicator, numpy_labels]) - self.assertEqual(sum(is_sampled_out), 500) - self.assertEqual(sum(np.logical_and(numpy_labels, is_sampled_out)), 5) - self.assertEqual(sum(np.logical_and( - np.logical_not(numpy_labels), is_sampled_out)), 495) - self.assertAllEqual(is_sampled_out, np.logical_and(is_sampled_out, - numpy_indicator)) - - def test_subsample_selection_no_batch_size_static(self): - labels = tf.constant([[True, False, False]]) - indicator = tf.constant([True, False, True]) - sampler = ( - balanced_positive_negative_sampler.BalancedPositiveNegativeSampler()) - with self.assertRaises(ValueError): - sampler.subsample(indicator, None, labels) - - def test_raises_error_with_incorrect_label_shape(self): - labels = tf.constant([[True, False, False]]) - indicator = tf.constant([True, False, True]) - sampler = (balanced_positive_negative_sampler. - BalancedPositiveNegativeSampler()) - with self.assertRaises(ValueError): - sampler.subsample(indicator, 64, labels) - - def test_raises_error_with_incorrect_indicator_shape(self): - labels = tf.constant([True, False, False]) - indicator = tf.constant([[True, False, True]]) - sampler = (balanced_positive_negative_sampler. - BalancedPositiveNegativeSampler()) - with self.assertRaises(ValueError): - sampler.subsample(indicator, 64, labels) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/batch_multiclass_nms_test.py b/research/object_detection/core/batch_multiclass_nms_test.py deleted file mode 100644 index 06f17103b2b..00000000000 --- a/research/object_detection/core/batch_multiclass_nms_test.py +++ /dev/null @@ -1,686 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for google3.third_party.tensorflow_models.object_detection.core.batch_multiclass_nms.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from absl.testing import parameterized -import numpy as np -from six.moves import range -import tensorflow.compat.v1 as tf -from object_detection.core import post_processing -from object_detection.utils import test_case - - -class BatchMulticlassNonMaxSuppressionTest(test_case.TestCase, - parameterized.TestCase): - - def test_batch_multiclass_nms_with_batch_size_1(self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]], - [[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = [[[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 999, 2, 1004], - [0, 100, 1, 101]]] - exp_nms_scores = [[.95, .9, .85, .3]] - exp_nms_classes = [[0, 0, 1, 0]] - def graph_fn(boxes, scores): - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, - max_total_size=max_output_size) - self.assertIsNone(nmsed_masks) - self.assertIsNone(nmsed_additional_fields) - return (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) - - (nmsed_boxes, nmsed_scores, nmsed_classes, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores]) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertEqual(num_detections, [4]) - - def test_batch_iou_with_negative_data(self): - def graph_fn(): - boxes = tf.constant([[[0, -0.01, 0.1, 1.1], [0, 0.2, 0.2, 5.0], - [0, -0.01, 0.1, 1.], [-1, -1, -1, -1]]], tf.float32) - iou = post_processing.batch_iou(boxes, boxes) - return iou - iou = self.execute_cpu(graph_fn, []) - expected_iou = [[[0.99999994, 0.0917431, 0.9099099, -1.], - [0.0917431, 1., 0.08154944, -1.], - [0.9099099, 0.08154944, 1., -1.], [-1., -1., -1., -1.]]] - self.assertAllClose(iou, expected_iou) - - @parameterized.parameters(False, True) - def test_batch_multiclass_nms_with_batch_size_2(self, use_dynamic_map_fn): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = np.array([[[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 999, 2, 1004], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101], - [0, 0, 0, 0]]]) - exp_nms_scores = np.array([[.95, .9, 0, 0], - [.85, .5, .3, 0]]) - exp_nms_classes = np.array([[0, 0, 0, 0], - [1, 0, 0, 0]]) - def graph_fn(boxes, scores): - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, - max_total_size=max_output_size, - use_dynamic_map_fn=use_dynamic_map_fn) - self.assertIsNone(nmsed_masks) - self.assertIsNone(nmsed_additional_fields) - # Check static shapes - self.assertAllEqual(nmsed_boxes.shape.as_list(), - exp_nms_corners.shape) - self.assertAllEqual(nmsed_scores.shape.as_list(), - exp_nms_scores.shape) - self.assertAllEqual(nmsed_classes.shape.as_list(), - exp_nms_classes.shape) - self.assertEqual(num_detections.shape.as_list(), [2]) - return (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) - - (nmsed_boxes, nmsed_scores, nmsed_classes, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores]) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertAllClose(num_detections, [2, 3]) - - def test_batch_multiclass_nms_with_per_batch_clip_window(self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - clip_window = np.array([0., 0., 200., 200.], np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = np.array([[[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 10.1, 1, 11.1], - [0, 100, 1, 101], - [0, 0, 0, 0], - [0, 0, 0, 0]]]) - exp_nms_scores = np.array([[.95, .9, 0, 0], - [.5, .3, 0, 0]]) - exp_nms_classes = np.array([[0, 0, 0, 0], - [0, 0, 0, 0]]) - def graph_fn(boxes, scores, clip_window): - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, max_total_size=max_output_size, - clip_window=clip_window) - self.assertIsNone(nmsed_masks) - self.assertIsNone(nmsed_additional_fields) - # Check static shapes - self.assertAllEqual(nmsed_boxes.shape.as_list(), - exp_nms_corners.shape) - self.assertAllEqual(nmsed_scores.shape.as_list(), - exp_nms_scores.shape) - self.assertAllEqual(nmsed_classes.shape.as_list(), - exp_nms_classes.shape) - self.assertEqual(num_detections.shape.as_list(), [2]) - return nmsed_boxes, nmsed_scores, nmsed_classes, num_detections - - (nmsed_boxes, nmsed_scores, nmsed_classes, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores, clip_window]) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertAllClose(num_detections, [2, 2]) - - def test_batch_multiclass_nms_with_per_image_clip_window(self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - clip_window = np.array([[0., 0., 5., 5.], - [0., 0., 200., 200.]], np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = np.array([[[0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 10.1, 1, 11.1], - [0, 100, 1, 101], - [0, 0, 0, 0], - [0, 0, 0, 0]]]) - exp_nms_scores = np.array([[.9, 0., 0., 0.], - [.5, .3, 0, 0]]) - exp_nms_classes = np.array([[0, 0, 0, 0], - [0, 0, 0, 0]]) - - def graph_fn(boxes, scores, clip_window): - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, max_total_size=max_output_size, - clip_window=clip_window) - self.assertIsNone(nmsed_masks) - self.assertIsNone(nmsed_additional_fields) - # Check static shapes - self.assertAllEqual(nmsed_boxes.shape.as_list(), - exp_nms_corners.shape) - self.assertAllEqual(nmsed_scores.shape.as_list(), - exp_nms_scores.shape) - self.assertAllEqual(nmsed_classes.shape.as_list(), - exp_nms_classes.shape) - self.assertEqual(num_detections.shape.as_list(), [2]) - return nmsed_boxes, nmsed_scores, nmsed_classes, num_detections - - (nmsed_boxes, nmsed_scores, nmsed_classes, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores, clip_window]) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertAllClose(num_detections, [1, 2]) - - def test_batch_multiclass_nms_with_masks(self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - masks = np.array([[[[[0, 1], [2, 3]], [[1, 2], [3, 4]]], - [[[2, 3], [4, 5]], [[3, 4], [5, 6]]], - [[[4, 5], [6, 7]], [[5, 6], [7, 8]]], - [[[6, 7], [8, 9]], [[7, 8], [9, 10]]]], - [[[[8, 9], [10, 11]], [[9, 10], [11, 12]]], - [[[10, 11], [12, 13]], [[11, 12], [13, 14]]], - [[[12, 13], [14, 15]], [[13, 14], [15, 16]]], - [[[14, 15], [16, 17]], [[15, 16], [17, 18]]]]], - np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = np.array([[[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 999, 2, 1004], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101], - [0, 0, 0, 0]]]) - exp_nms_scores = np.array([[.95, .9, 0, 0], - [.85, .5, .3, 0]]) - exp_nms_classes = np.array([[0, 0, 0, 0], - [1, 0, 0, 0]]) - exp_nms_masks = np.array([[[[6, 7], [8, 9]], - [[0, 1], [2, 3]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]], - [[[13, 14], [15, 16]], - [[8, 9], [10, 11]], - [[10, 11], [12, 13]], - [[0, 0], [0, 0]]]]) - - def graph_fn(boxes, scores, masks): - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, max_total_size=max_output_size, - masks=masks) - self.assertIsNone(nmsed_additional_fields) - # Check static shapes - self.assertAllEqual(nmsed_boxes.shape.as_list(), exp_nms_corners.shape) - self.assertAllEqual(nmsed_scores.shape.as_list(), exp_nms_scores.shape) - self.assertAllEqual(nmsed_classes.shape.as_list(), exp_nms_classes.shape) - self.assertAllEqual(nmsed_masks.shape.as_list(), exp_nms_masks.shape) - self.assertEqual(num_detections.shape.as_list(), [2]) - return (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - num_detections) - - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores, masks]) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertAllClose(num_detections, [2, 3]) - self.assertAllClose(nmsed_masks, exp_nms_masks) - - def test_batch_multiclass_nms_with_additional_fields(self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - keypoints = np.array( - [[[[6, 7], [8, 9]], - [[0, 1], [2, 3]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]], - [[[13, 14], [15, 16]], - [[8, 9], [10, 11]], - [[10, 11], [12, 13]], - [[0, 0], [0, 0]]]], - np.float32) - size = np.array( - [[[[6], [8]], [[0], [2]], [[0], [0]], [[0], [0]]], - [[[13], [15]], [[8], [10]], [[10], [12]], [[0], [0]]]], np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = np.array([[[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 999, 2, 1004], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101], - [0, 0, 0, 0]]]) - exp_nms_scores = np.array([[.95, .9, 0, 0], - [.85, .5, .3, 0]]) - exp_nms_classes = np.array([[0, 0, 0, 0], - [1, 0, 0, 0]]) - exp_nms_additional_fields = { - 'keypoints': np.array([[[[0, 0], [0, 0]], - [[6, 7], [8, 9]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]], - [[[10, 11], [12, 13]], - [[13, 14], [15, 16]], - [[8, 9], [10, 11]], - [[0, 0], [0, 0]]]]) - } - exp_nms_additional_fields['size'] = np.array([[[[0], [0]], [[6], [8]], - [[0], [0]], [[0], [0]]], - [[[10], [12]], [[13], [15]], - [[8], [10]], [[0], [0]]]]) - - def graph_fn(boxes, scores, keypoints, size): - additional_fields = {'keypoints': keypoints, 'size': size} - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, max_total_size=max_output_size, - additional_fields=additional_fields) - self.assertIsNone(nmsed_masks) - # Check static shapes - self.assertAllEqual(nmsed_boxes.shape.as_list(), exp_nms_corners.shape) - self.assertAllEqual(nmsed_scores.shape.as_list(), exp_nms_scores.shape) - self.assertAllEqual(nmsed_classes.shape.as_list(), exp_nms_classes.shape) - self.assertEqual(len(nmsed_additional_fields), - len(exp_nms_additional_fields)) - for key in exp_nms_additional_fields: - self.assertAllEqual(nmsed_additional_fields[key].shape.as_list(), - exp_nms_additional_fields[key].shape) - self.assertEqual(num_detections.shape.as_list(), [2]) - return (nmsed_boxes, nmsed_scores, nmsed_classes, - nmsed_additional_fields['keypoints'], - nmsed_additional_fields['size'], - num_detections) - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_keypoints, nmsed_size, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores, keypoints, - size]) - - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertAllClose(nmsed_keypoints, - exp_nms_additional_fields['keypoints']) - self.assertAllClose(nmsed_size, - exp_nms_additional_fields['size']) - self.assertAllClose(num_detections, [2, 3]) - - def test_batch_multiclass_nms_with_masks_and_num_valid_boxes(self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - masks = np.array([[[[[0, 1], [2, 3]], [[1, 2], [3, 4]]], - [[[2, 3], [4, 5]], [[3, 4], [5, 6]]], - [[[4, 5], [6, 7]], [[5, 6], [7, 8]]], - [[[6, 7], [8, 9]], [[7, 8], [9, 10]]]], - [[[[8, 9], [10, 11]], [[9, 10], [11, 12]]], - [[[10, 11], [12, 13]], [[11, 12], [13, 14]]], - [[[12, 13], [14, 15]], [[13, 14], [15, 16]]], - [[[14, 15], [16, 17]], [[15, 16], [17, 18]]]]], - np.float32) - num_valid_boxes = np.array([1, 1], np.int32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = [[[0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 10.1, 1, 11.1], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]] - exp_nms_scores = [[.9, 0, 0, 0], - [.5, 0, 0, 0]] - exp_nms_classes = [[0, 0, 0, 0], - [0, 0, 0, 0]] - exp_nms_masks = [[[[0, 1], [2, 3]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]], - [[[8, 9], [10, 11]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]]] - - def graph_fn(boxes, scores, masks, num_valid_boxes): - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, max_total_size=max_output_size, - masks=masks, num_valid_boxes=num_valid_boxes) - self.assertIsNone(nmsed_additional_fields) - return (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - num_detections) - - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores, masks, - num_valid_boxes]) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertAllClose(num_detections, [1, 1]) - self.assertAllClose(nmsed_masks, exp_nms_masks) - - def test_batch_multiclass_nms_with_additional_fields_and_num_valid_boxes( - self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], np.float32) - keypoints = np.array( - [[[[6, 7], [8, 9]], - [[0, 1], [2, 3]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]], - [[[13, 14], [15, 16]], - [[8, 9], [10, 11]], - [[10, 11], [12, 13]], - [[0, 0], [0, 0]]]], - np.float32) - size = np.array( - [[[[7], [9]], [[1], [3]], [[0], [0]], [[0], [0]]], - [[[14], [16]], [[9], [11]], [[11], [13]], [[0], [0]]]], np.float32) - - num_valid_boxes = np.array([1, 1], np.int32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = [[[0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 10.1, 1, 11.1], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]] - exp_nms_scores = [[.9, 0, 0, 0], - [.5, 0, 0, 0]] - exp_nms_classes = [[0, 0, 0, 0], - [0, 0, 0, 0]] - exp_nms_additional_fields = { - 'keypoints': np.array([[[[6, 7], [8, 9]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]], - [[[13, 14], [15, 16]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]], - [[0, 0], [0, 0]]]]) - } - - exp_nms_additional_fields['size'] = np.array([[[[7], [9]], [[0], [0]], - [[0], [0]], [[0], [0]]], - [[[14], [16]], [[0], [0]], - [[0], [0]], [[0], [0]]]]) - def graph_fn(boxes, scores, keypoints, size, num_valid_boxes): - additional_fields = {'keypoints': keypoints, 'size': size} - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, max_total_size=max_output_size, - num_valid_boxes=num_valid_boxes, - additional_fields=additional_fields) - self.assertIsNone(nmsed_masks) - return (nmsed_boxes, nmsed_scores, nmsed_classes, - nmsed_additional_fields['keypoints'], - nmsed_additional_fields['size'], num_detections) - - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_keypoints, nmsed_size, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores, keypoints, - size, num_valid_boxes]) - - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertAllClose(nmsed_keypoints, - exp_nms_additional_fields['keypoints']) - self.assertAllClose(nmsed_size, - exp_nms_additional_fields['size']) - self.assertAllClose(num_detections, [1, 1]) - - def test_combined_nms_with_batch_size_2(self): - """Test use_combined_nms.""" - boxes = np.array([[[[0, 0, 0.1, 0.1], [0, 0, 0.1, 0.1]], - [[0, 0.01, 1, 0.11], [0, 0.6, 0.1, 0.7]], - [[0, -0.01, 0.1, 0.09], [0, -0.1, 0.1, 0.09]], - [[0, 0.11, 0.1, 0.2], [0, 0.11, 0.1, 0.2]]], - [[[0, 0, 0.2, 0.2], [0, 0, 0.2, 0.2]], - [[0, 0.02, 0.2, 0.22], [0, 0.02, 0.2, 0.22]], - [[0, -0.02, 0.2, 0.19], [0, -0.02, 0.2, 0.19]], - [[0, 0.21, 0.2, 0.3], [0, 0.21, 0.2, 0.3]]]], - np.float32) - scores = np.array([[[.1, 0.9], [.75, 0.8], - [.6, 0.3], [0.95, 0.1]], - [[.1, 0.9], [.75, 0.8], - [.6, .3], [.95, .1]]], np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 3 - - exp_nms_corners = np.array([[[0, 0.11, 0.1, 0.2], - [0, 0, 0.1, 0.1], - [0, 0.6, 0.1, 0.7]], - [[0, 0.21, 0.2, 0.3], - [0, 0, 0.2, 0.2], - [0, 0.02, 0.2, 0.22]]]) - exp_nms_scores = np.array([[.95, .9, 0.8], - [.95, .9, .75]]) - exp_nms_classes = np.array([[0, 1, 1], - [0, 1, 0]]) - - def graph_fn(boxes, scores): - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, max_total_size=max_output_size, - use_static_shapes=True, - use_combined_nms=True) - self.assertIsNone(nmsed_masks) - self.assertIsNone(nmsed_additional_fields) - return (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) - - (nmsed_boxes, nmsed_scores, nmsed_classes, - num_detections) = self.execute_cpu(graph_fn, [boxes, scores]) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertListEqual(num_detections.tolist(), [3, 3]) - - def test_batch_multiclass_nms_with_use_static_shapes(self): - boxes = np.array([[[[0, 0, 1, 1], [0, 0, 4, 5]], - [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], - [[0, 10, 1, 11], [0, 10, 1, 11]]], - [[[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]]], - np.float32) - scores = np.array([[[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0]], - [[.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]]], - np.float32) - clip_window = np.array([[0., 0., 5., 5.], - [0., 0., 200., 200.]], - np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = np.array([[[0, 0, 1, 1], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 10.1, 1, 11.1], - [0, 100, 1, 101], - [0, 0, 0, 0], - [0, 0, 0, 0]]]) - exp_nms_scores = np.array([[.9, 0., 0., 0.], - [.5, .3, 0, 0]]) - exp_nms_classes = np.array([[0, 0, 0, 0], - [0, 0, 0, 0]]) - - def graph_fn(boxes, scores, clip_window): - (nmsed_boxes, nmsed_scores, nmsed_classes, _, _, num_detections - ) = post_processing.batch_multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, - max_size_per_class=max_output_size, clip_window=clip_window, - use_static_shapes=True) - return nmsed_boxes, nmsed_scores, nmsed_classes, num_detections - - (nmsed_boxes, nmsed_scores, nmsed_classes, - num_detections) = self.execute(graph_fn, [boxes, scores, clip_window]) - for i in range(len(num_detections)): - self.assertAllClose(nmsed_boxes[i, 0:num_detections[i]], - exp_nms_corners[i, 0:num_detections[i]]) - self.assertAllClose(nmsed_scores[i, 0:num_detections[i]], - exp_nms_scores[i, 0:num_detections[i]]) - self.assertAllClose(nmsed_classes[i, 0:num_detections[i]], - exp_nms_classes[i, 0:num_detections[i]]) - self.assertAllClose(num_detections, [1, 2]) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/batcher.py b/research/object_detection/core/batcher.py deleted file mode 100644 index 26832e30efa..00000000000 --- a/research/object_detection/core/batcher.py +++ /dev/null @@ -1,141 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Provides functions to batch a dictionary of input tensors.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections - -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.core import prefetcher - -rt_shape_str = '_runtime_shapes' - - -class BatchQueue(object): - """BatchQueue class. - - This class creates a batch queue to asynchronously enqueue tensors_dict. - It also adds a FIFO prefetcher so that the batches are readily available - for the consumers. Dequeue ops for a BatchQueue object can be created via - the Dequeue method which evaluates to a batch of tensor_dict. - - Example input pipeline with batching: - ------------------------------------ - key, string_tensor = slim.parallel_reader.parallel_read(...) - tensor_dict = decoder.decode(string_tensor) - tensor_dict = preprocessor.preprocess(tensor_dict, ...) - batch_queue = batcher.BatchQueue(tensor_dict, - batch_size=32, - batch_queue_capacity=2000, - num_batch_queue_threads=8, - prefetch_queue_capacity=20) - tensor_dict = batch_queue.dequeue() - outputs = Model(tensor_dict) - ... - ----------------------------------- - - Notes: - ----- - This class batches tensors of unequal sizes by zero padding and unpadding - them after generating a batch. This can be computationally expensive when - batching tensors (such as images) that are of vastly different sizes. So it is - recommended that the shapes of such tensors be fully defined in tensor_dict - while other lightweight tensors such as bounding box corners and class labels - can be of varying sizes. Use either crop or resize operations to fully define - the shape of an image in tensor_dict. - - It is also recommended to perform any preprocessing operations on tensors - before passing to BatchQueue and subsequently calling the Dequeue method. - - Another caveat is that this class does not read the last batch if it is not - full. The current implementation makes it hard to support that use case. So, - for evaluation, when it is critical to run all the examples through your - network use the input pipeline example mentioned in core/prefetcher.py. - """ - - def __init__(self, tensor_dict, batch_size, batch_queue_capacity, - num_batch_queue_threads, prefetch_queue_capacity): - """Constructs a batch queue holding tensor_dict. - - Args: - tensor_dict: dictionary of tensors to batch. - batch_size: batch size. - batch_queue_capacity: max capacity of the queue from which the tensors are - batched. - num_batch_queue_threads: number of threads to use for batching. - prefetch_queue_capacity: max capacity of the queue used to prefetch - assembled batches. - """ - # Remember static shapes to set shapes of batched tensors. - static_shapes = collections.OrderedDict( - {key: tensor.get_shape() for key, tensor in tensor_dict.items()}) - # Remember runtime shapes to unpad tensors after batching. - runtime_shapes = collections.OrderedDict( - {(key + rt_shape_str): tf.shape(tensor) - for key, tensor in tensor_dict.items()}) - - all_tensors = tensor_dict - all_tensors.update(runtime_shapes) - batched_tensors = tf.train.batch( - all_tensors, - capacity=batch_queue_capacity, - batch_size=batch_size, - dynamic_pad=True, - num_threads=num_batch_queue_threads) - - self._queue = prefetcher.prefetch(batched_tensors, - prefetch_queue_capacity) - self._static_shapes = static_shapes - self._batch_size = batch_size - - def dequeue(self): - """Dequeues a batch of tensor_dict from the BatchQueue. - - TODO: use allow_smaller_final_batch to allow running over the whole eval set - - Returns: - A list of tensor_dicts of the requested batch_size. - """ - batched_tensors = self._queue.dequeue() - # Separate input tensors from tensors containing their runtime shapes. - tensors = {} - shapes = {} - for key, batched_tensor in batched_tensors.items(): - unbatched_tensor_list = tf.unstack(batched_tensor) - for i, unbatched_tensor in enumerate(unbatched_tensor_list): - if rt_shape_str in key: - shapes[(key[:-len(rt_shape_str)], i)] = unbatched_tensor - else: - tensors[(key, i)] = unbatched_tensor - - # Undo that padding using shapes and create a list of size `batch_size` that - # contains tensor dictionaries. - tensor_dict_list = [] - batch_size = self._batch_size - for batch_id in range(batch_size): - tensor_dict = {} - for key in self._static_shapes: - tensor_dict[key] = tf.slice(tensors[(key, batch_id)], - tf.zeros_like(shapes[(key, batch_id)]), - shapes[(key, batch_id)]) - tensor_dict[key].set_shape(self._static_shapes[key]) - tensor_dict_list.append(tensor_dict) - - return tensor_dict_list diff --git a/research/object_detection/core/batcher_tf1_test.py b/research/object_detection/core/batcher_tf1_test.py deleted file mode 100644 index 1688b87cdf0..00000000000 --- a/research/object_detection/core/batcher_tf1_test.py +++ /dev/null @@ -1,165 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.batcher.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -import numpy as np -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.core import batcher -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class BatcherTest(tf.test.TestCase): - - def test_batch_and_unpad_2d_tensors_of_different_sizes_in_1st_dimension(self): - with self.test_session() as sess: - batch_size = 3 - num_batches = 2 - examples = tf.Variable(tf.constant(2, dtype=tf.int32)) - counter = examples.count_up_to(num_batches * batch_size + 2) - boxes = tf.tile( - tf.reshape(tf.range(4), [1, 4]), tf.stack([counter, tf.constant(1)])) - batch_queue = batcher.BatchQueue( - tensor_dict={'boxes': boxes}, - batch_size=batch_size, - batch_queue_capacity=100, - num_batch_queue_threads=1, - prefetch_queue_capacity=100) - batch = batch_queue.dequeue() - - for tensor_dict in batch: - for tensor in tensor_dict.values(): - self.assertAllEqual([None, 4], tensor.get_shape().as_list()) - - tf.initialize_all_variables().run() - with slim.queues.QueueRunners(sess): - i = 2 - for _ in range(num_batches): - batch_np = sess.run(batch) - for tensor_dict in batch_np: - for tensor in tensor_dict.values(): - self.assertAllEqual(tensor, np.tile(np.arange(4), (i, 1))) - i += 1 - with self.assertRaises(tf.errors.OutOfRangeError): - sess.run(batch) - - def test_batch_and_unpad_2d_tensors_of_different_sizes_in_all_dimensions( - self): - with self.test_session() as sess: - batch_size = 3 - num_batches = 2 - examples = tf.Variable(tf.constant(2, dtype=tf.int32)) - counter = examples.count_up_to(num_batches * batch_size + 2) - image = tf.reshape( - tf.range(counter * counter), tf.stack([counter, counter])) - batch_queue = batcher.BatchQueue( - tensor_dict={'image': image}, - batch_size=batch_size, - batch_queue_capacity=100, - num_batch_queue_threads=1, - prefetch_queue_capacity=100) - batch = batch_queue.dequeue() - - for tensor_dict in batch: - for tensor in tensor_dict.values(): - self.assertAllEqual([None, None], tensor.get_shape().as_list()) - - tf.initialize_all_variables().run() - with slim.queues.QueueRunners(sess): - i = 2 - for _ in range(num_batches): - batch_np = sess.run(batch) - for tensor_dict in batch_np: - for tensor in tensor_dict.values(): - self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i))) - i += 1 - with self.assertRaises(tf.errors.OutOfRangeError): - sess.run(batch) - - def test_batch_and_unpad_2d_tensors_of_same_size_in_all_dimensions(self): - with self.test_session() as sess: - batch_size = 3 - num_batches = 2 - examples = tf.Variable(tf.constant(1, dtype=tf.int32)) - counter = examples.count_up_to(num_batches * batch_size + 1) - image = tf.reshape(tf.range(1, 13), [4, 3]) * counter - batch_queue = batcher.BatchQueue( - tensor_dict={'image': image}, - batch_size=batch_size, - batch_queue_capacity=100, - num_batch_queue_threads=1, - prefetch_queue_capacity=100) - batch = batch_queue.dequeue() - - for tensor_dict in batch: - for tensor in tensor_dict.values(): - self.assertAllEqual([4, 3], tensor.get_shape().as_list()) - - tf.initialize_all_variables().run() - with slim.queues.QueueRunners(sess): - i = 1 - for _ in range(num_batches): - batch_np = sess.run(batch) - for tensor_dict in batch_np: - for tensor in tensor_dict.values(): - self.assertAllEqual(tensor, np.arange(1, 13).reshape((4, 3)) * i) - i += 1 - with self.assertRaises(tf.errors.OutOfRangeError): - sess.run(batch) - - def test_batcher_when_batch_size_is_one(self): - with self.test_session() as sess: - batch_size = 1 - num_batches = 2 - examples = tf.Variable(tf.constant(2, dtype=tf.int32)) - counter = examples.count_up_to(num_batches * batch_size + 2) - image = tf.reshape( - tf.range(counter * counter), tf.stack([counter, counter])) - batch_queue = batcher.BatchQueue( - tensor_dict={'image': image}, - batch_size=batch_size, - batch_queue_capacity=100, - num_batch_queue_threads=1, - prefetch_queue_capacity=100) - batch = batch_queue.dequeue() - - for tensor_dict in batch: - for tensor in tensor_dict.values(): - self.assertAllEqual([None, None], tensor.get_shape().as_list()) - - tf.initialize_all_variables().run() - with slim.queues.QueueRunners(sess): - i = 2 - for _ in range(num_batches): - batch_np = sess.run(batch) - for tensor_dict in batch_np: - for tensor in tensor_dict.values(): - self.assertAllEqual(tensor, np.arange(i * i).reshape((i, i))) - i += 1 - with self.assertRaises(tf.errors.OutOfRangeError): - sess.run(batch) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/box_coder.py b/research/object_detection/core/box_coder.py deleted file mode 100644 index c6e54a44033..00000000000 --- a/research/object_detection/core/box_coder.py +++ /dev/null @@ -1,158 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base box coder. - -Box coders convert between coordinate frames, namely image-centric -(with (0,0) on the top left of image) and anchor-centric (with (0,0) being -defined by a specific anchor). - -Users of a BoxCoder can call two methods: - encode: which encodes a box with respect to a given anchor - (or rather, a tensor of boxes wrt a corresponding tensor of anchors) and - decode: which inverts this encoding with a decode operation. -In both cases, the arguments are assumed to be in 1-1 correspondence already; -it is not the job of a BoxCoder to perform matching. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from abc import ABCMeta -from abc import abstractmethod -from abc import abstractproperty - -import six -import tensorflow.compat.v1 as tf - -from object_detection.utils import shape_utils - - -# Box coder types. -FASTER_RCNN = 'faster_rcnn' -KEYPOINT = 'keypoint' -MEAN_STDDEV = 'mean_stddev' -SQUARE = 'square' - - -class BoxCoder(six.with_metaclass(ABCMeta, object)): - """Abstract base class for box coder.""" - - @abstractproperty - def code_size(self): - """Return the size of each code. - - This number is a constant and should agree with the output of the `encode` - op (e.g. if rel_codes is the output of self.encode(...), then it should have - shape [N, code_size()]). This abstractproperty should be overridden by - implementations. - - Returns: - an integer constant - """ - pass - - def encode(self, boxes, anchors): - """Encode a box list relative to an anchor collection. - - Args: - boxes: BoxList holding N boxes to be encoded - anchors: BoxList of N anchors - - Returns: - a tensor representing N relative-encoded boxes - """ - with tf.name_scope('Encode'): - return self._encode(boxes, anchors) - - def decode(self, rel_codes, anchors): - """Decode boxes that are encoded relative to an anchor collection. - - Args: - rel_codes: a tensor representing N relative-encoded boxes - anchors: BoxList of anchors - - Returns: - boxlist: BoxList holding N boxes encoded in the ordinary way (i.e., - with corners y_min, x_min, y_max, x_max) - """ - with tf.name_scope('Decode'): - return self._decode(rel_codes, anchors) - - @abstractmethod - def _encode(self, boxes, anchors): - """Method to be overriden by implementations. - - Args: - boxes: BoxList holding N boxes to be encoded - anchors: BoxList of N anchors - - Returns: - a tensor representing N relative-encoded boxes - """ - pass - - @abstractmethod - def _decode(self, rel_codes, anchors): - """Method to be overriden by implementations. - - Args: - rel_codes: a tensor representing N relative-encoded boxes - anchors: BoxList of anchors - - Returns: - boxlist: BoxList holding N boxes encoded in the ordinary way (i.e., - with corners y_min, x_min, y_max, x_max) - """ - pass - - -def batch_decode(encoded_boxes, box_coder, anchors): - """Decode a batch of encoded boxes. - - This op takes a batch of encoded bounding boxes and transforms - them to a batch of bounding boxes specified by their corners in - the order of [y_min, x_min, y_max, x_max]. - - Args: - encoded_boxes: a float32 tensor of shape [batch_size, num_anchors, - code_size] representing the location of the objects. - box_coder: a BoxCoder object. - anchors: a BoxList of anchors used to encode `encoded_boxes`. - - Returns: - decoded_boxes: a float32 tensor of shape [batch_size, num_anchors, - coder_size] representing the corners of the objects in the order - of [y_min, x_min, y_max, x_max]. - - Raises: - ValueError: if batch sizes of the inputs are inconsistent, or if - the number of anchors inferred from encoded_boxes and anchors are - inconsistent. - """ - encoded_boxes.get_shape().assert_has_rank(3) - if (shape_utils.get_dim_as_int(encoded_boxes.get_shape()[1]) - != anchors.num_boxes_static()): - raise ValueError('The number of anchors inferred from encoded_boxes' - ' and anchors are inconsistent: shape[1] of encoded_boxes' - ' %s should be equal to the number of anchors: %s.' % - (shape_utils.get_dim_as_int(encoded_boxes.get_shape()[1]), - anchors.num_boxes_static())) - - decoded_boxes = tf.stack([ - box_coder.decode(boxes, anchors).get() - for boxes in tf.unstack(encoded_boxes) - ]) - return decoded_boxes diff --git a/research/object_detection/core/box_coder_test.py b/research/object_detection/core/box_coder_test.py deleted file mode 100644 index 52765a9d06c..00000000000 --- a/research/object_detection/core/box_coder_test.py +++ /dev/null @@ -1,62 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.box_coder.""" -import tensorflow.compat.v1 as tf - -from object_detection.core import box_coder -from object_detection.core import box_list -from object_detection.utils import test_case - - -class MockBoxCoder(box_coder.BoxCoder): - """Test BoxCoder that encodes/decodes using the multiply-by-two function.""" - - def code_size(self): - return 4 - - def _encode(self, boxes, anchors): - return 2.0 * boxes.get() - - def _decode(self, rel_codes, anchors): - return box_list.BoxList(rel_codes / 2.0) - - -class BoxCoderTest(test_case.TestCase): - - def test_batch_decode(self): - - expected_boxes = [[[0.0, 0.1, 0.5, 0.6], [0.5, 0.6, 0.7, 0.8]], - [[0.1, 0.2, 0.3, 0.4], [0.7, 0.8, 0.9, 1.0]]] - - def graph_fn(): - mock_anchor_corners = tf.constant( - [[0, 0.1, 0.2, 0.3], [0.2, 0.4, 0.4, 0.6]], tf.float32) - mock_anchors = box_list.BoxList(mock_anchor_corners) - mock_box_coder = MockBoxCoder() - - encoded_boxes_list = [mock_box_coder.encode( - box_list.BoxList(tf.constant(boxes)), mock_anchors) - for boxes in expected_boxes] - encoded_boxes = tf.stack(encoded_boxes_list) - decoded_boxes = box_coder.batch_decode( - encoded_boxes, mock_box_coder, mock_anchors) - return decoded_boxes - decoded_boxes_result = self.execute(graph_fn, []) - self.assertAllClose(expected_boxes, decoded_boxes_result) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/box_list.py b/research/object_detection/core/box_list.py deleted file mode 100644 index 7b6b97e995f..00000000000 --- a/research/object_detection/core/box_list.py +++ /dev/null @@ -1,210 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Bounding Box List definition. - -BoxList represents a list of bounding boxes as tensorflow -tensors, where each bounding box is represented as a row of 4 numbers, -[y_min, x_min, y_max, x_max]. It is assumed that all bounding boxes -within a given list correspond to a single image. See also -box_list_ops.py for common box related operations (such as area, iou, etc). - -Optionally, users can add additional related fields (such as weights). -We assume the following things to be true about fields: -* they correspond to boxes in the box_list along the 0th dimension -* they have inferrable rank at graph construction time -* all dimensions except for possibly the 0th can be inferred - (i.e., not None) at graph construction time. - -Some other notes: - * Following tensorflow conventions, we use height, width ordering, - and correspondingly, y,x (or ymin, xmin, ymax, xmax) ordering - * Tensors are always provided as (flat) [N, 4] tensors. -""" - -import tensorflow.compat.v1 as tf - -from object_detection.utils import shape_utils - - -class BoxList(object): - """Box collection.""" - - def __init__(self, boxes): - """Constructs box collection. - - Args: - boxes: a tensor of shape [N, 4] representing box corners - - Raises: - ValueError: if invalid dimensions for bbox data or if bbox data is not in - float32 format. - """ - if len(boxes.get_shape()) != 2 or boxes.get_shape()[-1] != 4: - raise ValueError('Invalid dimensions for box data: {}'.format( - boxes.shape)) - if boxes.dtype != tf.float32: - raise ValueError('Invalid tensor type: should be tf.float32') - self.data = {'boxes': boxes} - - def num_boxes(self): - """Returns number of boxes held in collection. - - Returns: - a tensor representing the number of boxes held in the collection. - """ - return tf.shape(self.data['boxes'])[0] - - def num_boxes_static(self): - """Returns number of boxes held in collection. - - This number is inferred at graph construction time rather than run-time. - - Returns: - Number of boxes held in collection (integer) or None if this is not - inferrable at graph construction time. - """ - return shape_utils.get_dim_as_int(self.data['boxes'].get_shape()[0]) - - def get_all_fields(self): - """Returns all fields.""" - return self.data.keys() - - def get_extra_fields(self): - """Returns all non-box fields (i.e., everything not named 'boxes').""" - return [k for k in self.data.keys() if k != 'boxes'] - - def add_field(self, field, field_data): - """Add field to box list. - - This method can be used to add related box data such as - weights/labels, etc. - - Args: - field: a string key to access the data via `get` - field_data: a tensor containing the data to store in the BoxList - """ - self.data[field] = field_data - - def has_field(self, field): - return field in self.data - - def get(self): - """Convenience function for accessing box coordinates. - - Returns: - a tensor with shape [N, 4] representing box coordinates. - """ - return self.get_field('boxes') - - def set(self, boxes): - """Convenience function for setting box coordinates. - - Args: - boxes: a tensor of shape [N, 4] representing box corners - - Raises: - ValueError: if invalid dimensions for bbox data - """ - if len(boxes.get_shape()) != 2 or boxes.get_shape()[-1] != 4: - raise ValueError('Invalid dimensions for box data.') - self.data['boxes'] = boxes - - def get_field(self, field): - """Accesses a box collection and associated fields. - - This function returns specified field with object; if no field is specified, - it returns the box coordinates. - - Args: - field: this optional string parameter can be used to specify - a related field to be accessed. - - Returns: - a tensor representing the box collection or an associated field. - - Raises: - ValueError: if invalid field - """ - if not self.has_field(field): - raise ValueError('field ' + str(field) + ' does not exist') - return self.data[field] - - def set_field(self, field, value): - """Sets the value of a field. - - Updates the field of a box_list with a given value. - - Args: - field: (string) name of the field to set value. - value: the value to assign to the field. - - Raises: - ValueError: if the box_list does not have specified field. - """ - if not self.has_field(field): - raise ValueError('field %s does not exist' % field) - self.data[field] = value - - def get_center_coordinates_and_sizes(self, scope=None): - """Computes the center coordinates, height and width of the boxes. - - Args: - scope: name scope of the function. - - Returns: - a list of 4 1-D tensors [ycenter, xcenter, height, width]. - """ - with tf.name_scope(scope, 'get_center_coordinates_and_sizes'): - box_corners = self.get() - ymin, xmin, ymax, xmax = tf.unstack(tf.transpose(box_corners)) - width = xmax - xmin - height = ymax - ymin - ycenter = ymin + height / 2. - xcenter = xmin + width / 2. - return [ycenter, xcenter, height, width] - - def transpose_coordinates(self, scope=None): - """Transpose the coordinate representation in a boxlist. - - Args: - scope: name scope of the function. - """ - with tf.name_scope(scope, 'transpose_coordinates'): - y_min, x_min, y_max, x_max = tf.split( - value=self.get(), num_or_size_splits=4, axis=1) - self.set(tf.concat([x_min, y_min, x_max, y_max], 1)) - - def as_tensor_dict(self, fields=None): - """Retrieves specified fields as a dictionary of tensors. - - Args: - fields: (optional) list of fields to return in the dictionary. - If None (default), all fields are returned. - - Returns: - tensor_dict: A dictionary of tensors specified by fields. - - Raises: - ValueError: if specified field is not contained in boxlist. - """ - tensor_dict = {} - if fields is None: - fields = self.get_all_fields() - for field in fields: - if not self.has_field(field): - raise ValueError('boxlist must contain all specified fields') - tensor_dict[field] = self.get_field(field) - return tensor_dict diff --git a/research/object_detection/core/box_list_ops.py b/research/object_detection/core/box_list_ops.py deleted file mode 100644 index cb457b72844..00000000000 --- a/research/object_detection/core/box_list_ops.py +++ /dev/null @@ -1,1213 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Bounding Box List operations. - -Example box operations that are supported: - * areas: compute bounding box areas - * iou: pairwise intersection-over-union scores - * sq_dist: pairwise distances between bounding boxes - -Whenever box_list_ops functions output a BoxList, the fields of the incoming -BoxList are retained unless documented otherwise. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.core import box_list -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -class SortOrder(object): - """Enum class for sort order. - - Attributes: - ascend: ascend order. - descend: descend order. - """ - ascend = 1 - descend = 2 - - -def area(boxlist, scope=None): - """Computes area of boxes. - - Args: - boxlist: BoxList holding N boxes - scope: name scope. - - Returns: - a tensor with shape [N] representing box areas. - """ - with tf.name_scope(scope, 'Area'): - y_min, x_min, y_max, x_max = tf.split( - value=boxlist.get(), num_or_size_splits=4, axis=1) - return tf.squeeze((y_max - y_min) * (x_max - x_min), [1]) - - -def height_width(boxlist, scope=None): - """Computes height and width of boxes in boxlist. - - Args: - boxlist: BoxList holding N boxes - scope: name scope. - - Returns: - Height: A tensor with shape [N] representing box heights. - Width: A tensor with shape [N] representing box widths. - """ - with tf.name_scope(scope, 'HeightWidth'): - y_min, x_min, y_max, x_max = tf.split( - value=boxlist.get(), num_or_size_splits=4, axis=1) - return tf.squeeze(y_max - y_min, [1]), tf.squeeze(x_max - x_min, [1]) - - -def scale(boxlist, y_scale, x_scale, scope=None): - """scale box coordinates in x and y dimensions. - - Args: - boxlist: BoxList holding N boxes - y_scale: (float) scalar tensor - x_scale: (float) scalar tensor - scope: name scope. - - Returns: - boxlist: BoxList holding N boxes - """ - with tf.name_scope(scope, 'Scale'): - y_scale = tf.cast(y_scale, tf.float32) - x_scale = tf.cast(x_scale, tf.float32) - y_min, x_min, y_max, x_max = tf.split( - value=boxlist.get(), num_or_size_splits=4, axis=1) - y_min = y_scale * y_min - y_max = y_scale * y_max - x_min = x_scale * x_min - x_max = x_scale * x_max - scaled_boxlist = box_list.BoxList( - tf.concat([y_min, x_min, y_max, x_max], 1)) - return _copy_extra_fields(scaled_boxlist, boxlist) - - -def scale_height_width(boxlist, y_scale, x_scale, scope=None): - """Scale the height and width of boxes, leaving centers unchanged. - - Args: - boxlist: BoxList holding N boxes - y_scale: (float) scalar tensor - x_scale: (float) scalar tensor - scope: name scope. - - Returns: - boxlist: BoxList holding N boxes - """ - with tf.name_scope(scope, 'ScaleHeightWidth'): - y_scale = tf.cast(y_scale, tf.float32) - x_scale = tf.cast(x_scale, tf.float32) - yc, xc, height_orig, width_orig = boxlist.get_center_coordinates_and_sizes() - y_min = yc - 0.5 * y_scale * height_orig - y_max = yc + 0.5 * y_scale * height_orig - x_min = xc - 0.5 * x_scale * width_orig - x_max = xc + 0.5 * x_scale * width_orig - scaled_boxlist = box_list.BoxList( - tf.stack([y_min, x_min, y_max, x_max], 1)) - return _copy_extra_fields(scaled_boxlist, boxlist) - - -def clip_to_window(boxlist, window, filter_nonoverlapping=True, scope=None): - """Clip bounding boxes to a window. - - This op clips any input bounding boxes (represented by bounding box - corners) to a window, optionally filtering out boxes that do not - overlap at all with the window. - - Args: - boxlist: BoxList holding M_in boxes - window: a tensor of shape [4] representing the [y_min, x_min, y_max, x_max] - window to which the op should clip boxes. - filter_nonoverlapping: whether to filter out boxes that do not overlap at - all with the window. - scope: name scope. - - Returns: - a BoxList holding M_out boxes where M_out <= M_in - """ - with tf.name_scope(scope, 'ClipToWindow'): - y_min, x_min, y_max, x_max = tf.split( - value=boxlist.get(), num_or_size_splits=4, axis=1) - win_y_min = window[0] - win_x_min = window[1] - win_y_max = window[2] - win_x_max = window[3] - y_min_clipped = tf.maximum(tf.minimum(y_min, win_y_max), win_y_min) - y_max_clipped = tf.maximum(tf.minimum(y_max, win_y_max), win_y_min) - x_min_clipped = tf.maximum(tf.minimum(x_min, win_x_max), win_x_min) - x_max_clipped = tf.maximum(tf.minimum(x_max, win_x_max), win_x_min) - clipped = box_list.BoxList( - tf.concat([y_min_clipped, x_min_clipped, y_max_clipped, x_max_clipped], - 1)) - clipped = _copy_extra_fields(clipped, boxlist) - if filter_nonoverlapping: - areas = area(clipped) - nonzero_area_indices = tf.cast( - tf.reshape(tf.where(tf.greater(areas, 0.0)), [-1]), tf.int32) - clipped = gather(clipped, nonzero_area_indices) - return clipped - - -def prune_outside_window(boxlist, window, scope=None): - """Prunes bounding boxes that fall outside a given window. - - This function prunes bounding boxes that even partially fall outside the given - window. See also clip_to_window which only prunes bounding boxes that fall - completely outside the window, and clips any bounding boxes that partially - overflow. - - Args: - boxlist: a BoxList holding M_in boxes. - window: a float tensor of shape [4] representing [ymin, xmin, ymax, xmax] - of the window - scope: name scope. - - Returns: - pruned_corners: a tensor with shape [M_out, 4] where M_out <= M_in - valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes - in the input tensor. - """ - with tf.name_scope(scope, 'PruneOutsideWindow'): - y_min, x_min, y_max, x_max = tf.split( - value=boxlist.get(), num_or_size_splits=4, axis=1) - win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window) - coordinate_violations = tf.concat([ - tf.less(y_min, win_y_min), tf.less(x_min, win_x_min), - tf.greater(y_max, win_y_max), tf.greater(x_max, win_x_max) - ], 1) - valid_indices = tf.reshape( - tf.where(tf.logical_not(tf.reduce_any(coordinate_violations, 1))), [-1]) - return gather(boxlist, valid_indices), valid_indices - - -def prune_completely_outside_window(boxlist, window, scope=None): - """Prunes bounding boxes that fall completely outside of the given window. - - The function clip_to_window prunes bounding boxes that fall - completely outside the window, but also clips any bounding boxes that - partially overflow. This function does not clip partially overflowing boxes. - - Args: - boxlist: a BoxList holding M_in boxes. - window: a float tensor of shape [4] representing [ymin, xmin, ymax, xmax] - of the window - scope: name scope. - - Returns: - pruned_boxlist: a new BoxList with all bounding boxes partially or fully in - the window. - valid_indices: a tensor with shape [M_out] indexing the valid bounding boxes - in the input tensor. - """ - with tf.name_scope(scope, 'PruneCompleteleyOutsideWindow'): - y_min, x_min, y_max, x_max = tf.split( - value=boxlist.get(), num_or_size_splits=4, axis=1) - win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window) - coordinate_violations = tf.concat([ - tf.greater_equal(y_min, win_y_max), tf.greater_equal(x_min, win_x_max), - tf.less_equal(y_max, win_y_min), tf.less_equal(x_max, win_x_min) - ], 1) - valid_indices = tf.reshape( - tf.where(tf.logical_not(tf.reduce_any(coordinate_violations, 1))), [-1]) - return gather(boxlist, valid_indices), valid_indices - - -def intersection(boxlist1, boxlist2, scope=None): - """Compute pairwise intersection areas between boxes. - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding M boxes - scope: name scope. - - Returns: - a tensor with shape [N, M] representing pairwise intersections - """ - with tf.name_scope(scope, 'Intersection'): - y_min1, x_min1, y_max1, x_max1 = tf.split( - value=boxlist1.get(), num_or_size_splits=4, axis=1) - y_min2, x_min2, y_max2, x_max2 = tf.split( - value=boxlist2.get(), num_or_size_splits=4, axis=1) - all_pairs_min_ymax = tf.minimum(y_max1, tf.transpose(y_max2)) - all_pairs_max_ymin = tf.maximum(y_min1, tf.transpose(y_min2)) - intersect_heights = tf.maximum(0.0, all_pairs_min_ymax - all_pairs_max_ymin) - all_pairs_min_xmax = tf.minimum(x_max1, tf.transpose(x_max2)) - all_pairs_max_xmin = tf.maximum(x_min1, tf.transpose(x_min2)) - intersect_widths = tf.maximum(0.0, all_pairs_min_xmax - all_pairs_max_xmin) - return intersect_heights * intersect_widths - - -def matched_intersection(boxlist1, boxlist2, scope=None): - """Compute intersection areas between corresponding boxes in two boxlists. - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding N boxes - scope: name scope. - - Returns: - a tensor with shape [N] representing pairwise intersections - """ - with tf.name_scope(scope, 'MatchedIntersection'): - y_min1, x_min1, y_max1, x_max1 = tf.split( - value=boxlist1.get(), num_or_size_splits=4, axis=1) - y_min2, x_min2, y_max2, x_max2 = tf.split( - value=boxlist2.get(), num_or_size_splits=4, axis=1) - min_ymax = tf.minimum(y_max1, y_max2) - max_ymin = tf.maximum(y_min1, y_min2) - intersect_heights = tf.maximum(0.0, min_ymax - max_ymin) - min_xmax = tf.minimum(x_max1, x_max2) - max_xmin = tf.maximum(x_min1, x_min2) - intersect_widths = tf.maximum(0.0, min_xmax - max_xmin) - return tf.reshape(intersect_heights * intersect_widths, [-1]) - - -def iou(boxlist1, boxlist2, scope=None): - """Computes pairwise intersection-over-union between box collections. - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding M boxes - scope: name scope. - - Returns: - a tensor with shape [N, M] representing pairwise iou scores. - """ - with tf.name_scope(scope, 'IOU'): - intersections = intersection(boxlist1, boxlist2) - areas1 = area(boxlist1) - areas2 = area(boxlist2) - unions = ( - tf.expand_dims(areas1, 1) + tf.expand_dims(areas2, 0) - intersections) - return tf.where( - tf.equal(intersections, 0.0), - tf.zeros_like(intersections), tf.truediv(intersections, unions)) - - -def l1(boxlist1, boxlist2, scope=None): - """Computes l1 loss (pairwise) between two boxlists. - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding M boxes - scope: name scope. - - Returns: - a tensor with shape [N, M] representing the pairwise L1 loss. - """ - with tf.name_scope(scope, 'PairwiseL1'): - ycenter1, xcenter1, h1, w1 = boxlist1.get_center_coordinates_and_sizes() - ycenter2, xcenter2, h2, w2 = boxlist2.get_center_coordinates_and_sizes() - ycenters = tf.abs(tf.expand_dims(ycenter2, axis=0) - tf.expand_dims( - tf.transpose(ycenter1), axis=1)) - xcenters = tf.abs(tf.expand_dims(xcenter2, axis=0) - tf.expand_dims( - tf.transpose(xcenter1), axis=1)) - heights = tf.abs(tf.expand_dims(h2, axis=0) - tf.expand_dims( - tf.transpose(h1), axis=1)) - widths = tf.abs(tf.expand_dims(w2, axis=0) - tf.expand_dims( - tf.transpose(w1), axis=1)) - return ycenters + xcenters + heights + widths - - -def giou(boxlist1, boxlist2, scope=None): - """Computes pairwise generalized IOU between two boxlists. - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding M boxes - scope: name scope. - - Returns: - a tensor with shape [N, M] representing the pairwise GIoU loss. - """ - with tf.name_scope(scope, 'PairwiseGIoU'): - n = boxlist1.num_boxes() - m = boxlist2.num_boxes() - boxes1 = tf.repeat(boxlist1.get(), repeats=m, axis=0) - boxes2 = tf.tile(boxlist2.get(), multiples=[n, 1]) - return tf.reshape(ops.giou(boxes1, boxes2), [n, m]) - - -def matched_iou(boxlist1, boxlist2, scope=None): - """Compute intersection-over-union between corresponding boxes in boxlists. - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding N boxes - scope: name scope. - - Returns: - a tensor with shape [N] representing pairwise iou scores. - """ - with tf.name_scope(scope, 'MatchedIOU'): - intersections = matched_intersection(boxlist1, boxlist2) - areas1 = area(boxlist1) - areas2 = area(boxlist2) - unions = areas1 + areas2 - intersections - return tf.where( - tf.equal(intersections, 0.0), - tf.zeros_like(intersections), tf.truediv(intersections, unions)) - - -def ioa(boxlist1, boxlist2, scope=None): - """Computes pairwise intersection-over-area between box collections. - - intersection-over-area (IOA) between two boxes box1 and box2 is defined as - their intersection area over box2's area. Note that ioa is not symmetric, - that is, ioa(box1, box2) != ioa(box2, box1). - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding M boxes - scope: name scope. - - Returns: - a tensor with shape [N, M] representing pairwise ioa scores. - """ - with tf.name_scope(scope, 'IOA'): - intersections = intersection(boxlist1, boxlist2) - areas = tf.expand_dims(area(boxlist2), 0) - return tf.truediv(intersections, areas) - - -def prune_non_overlapping_boxes( - boxlist1, boxlist2, min_overlap=0.0, scope=None): - """Prunes the boxes in boxlist1 that overlap less than thresh with boxlist2. - - For each box in boxlist1, we want its IOA to be more than minoverlap with - at least one of the boxes in boxlist2. If it does not, we remove it. - - Args: - boxlist1: BoxList holding N boxes. - boxlist2: BoxList holding M boxes. - min_overlap: Minimum required overlap between boxes, to count them as - overlapping. - scope: name scope. - - Returns: - new_boxlist1: A pruned boxlist with size [N', 4]. - keep_inds: A tensor with shape [N'] indexing kept bounding boxes in the - first input BoxList `boxlist1`. - """ - with tf.name_scope(scope, 'PruneNonOverlappingBoxes'): - ioa_ = ioa(boxlist2, boxlist1) # [M, N] tensor - ioa_ = tf.reduce_max(ioa_, reduction_indices=[0]) # [N] tensor - keep_bool = tf.greater_equal(ioa_, tf.constant(min_overlap)) - keep_inds = tf.squeeze(tf.where(keep_bool), axis=[1]) - new_boxlist1 = gather(boxlist1, keep_inds) - return new_boxlist1, keep_inds - - -def prune_small_boxes(boxlist, min_side, scope=None): - """Prunes small boxes in the boxlist which have a side smaller than min_side. - - Args: - boxlist: BoxList holding N boxes. - min_side: Minimum width AND height of box to survive pruning. - scope: name scope. - - Returns: - A pruned boxlist. - """ - with tf.name_scope(scope, 'PruneSmallBoxes'): - height, width = height_width(boxlist) - is_valid = tf.logical_and(tf.greater_equal(width, min_side), - tf.greater_equal(height, min_side)) - return gather(boxlist, tf.reshape(tf.where(is_valid), [-1])) - - -def change_coordinate_frame(boxlist, window, scope=None): - """Change coordinate frame of the boxlist to be relative to window's frame. - - Given a window of the form [ymin, xmin, ymax, xmax], - changes bounding box coordinates from boxlist to be relative to this window - (e.g., the min corner maps to (0,0) and the max corner maps to (1,1)). - - An example use case is data augmentation: where we are given groundtruth - boxes (boxlist) and would like to randomly crop the image to some - window (window). In this case we need to change the coordinate frame of - each groundtruth box to be relative to this new window. - - Args: - boxlist: A BoxList object holding N boxes. - window: A rank 1 tensor [4]. - scope: name scope. - - Returns: - Returns a BoxList object with N boxes. - """ - with tf.name_scope(scope, 'ChangeCoordinateFrame'): - win_height = window[2] - window[0] - win_width = window[3] - window[1] - boxlist_new = scale(box_list.BoxList( - boxlist.get() - [window[0], window[1], window[0], window[1]]), - 1.0 / win_height, 1.0 / win_width) - boxlist_new = _copy_extra_fields(boxlist_new, boxlist) - return boxlist_new - - -def sq_dist(boxlist1, boxlist2, scope=None): - """Computes the pairwise squared distances between box corners. - - This op treats each box as if it were a point in a 4d Euclidean space and - computes pairwise squared distances. - - Mathematically, we are given two matrices of box coordinates X and Y, - where X(i,:) is the i'th row of X, containing the 4 numbers defining the - corners of the i'th box in boxlist1. Similarly Y(j,:) corresponds to - boxlist2. We compute - Z(i,j) = ||X(i,:) - Y(j,:)||^2 - = ||X(i,:)||^2 + ||Y(j,:)||^2 - 2 X(i,:)' * Y(j,:), - - Args: - boxlist1: BoxList holding N boxes - boxlist2: BoxList holding M boxes - scope: name scope. - - Returns: - a tensor with shape [N, M] representing pairwise distances - """ - with tf.name_scope(scope, 'SqDist'): - sqnorm1 = tf.reduce_sum(tf.square(boxlist1.get()), 1, keep_dims=True) - sqnorm2 = tf.reduce_sum(tf.square(boxlist2.get()), 1, keep_dims=True) - innerprod = tf.matmul(boxlist1.get(), boxlist2.get(), - transpose_a=False, transpose_b=True) - return sqnorm1 + tf.transpose(sqnorm2) - 2.0 * innerprod - - -def boolean_mask(boxlist, indicator, fields=None, scope=None, - use_static_shapes=False, indicator_sum=None): - """Select boxes from BoxList according to indicator and return new BoxList. - - `boolean_mask` returns the subset of boxes that are marked as "True" by the - indicator tensor. By default, `boolean_mask` returns boxes corresponding to - the input index list, as well as all additional fields stored in the boxlist - (indexing into the first dimension). However one can optionally only draw - from a subset of fields. - - Args: - boxlist: BoxList holding N boxes - indicator: a rank-1 boolean tensor - fields: (optional) list of fields to also gather from. If None (default), - all fields are gathered from. Pass an empty fields list to only gather - the box coordinates. - scope: name scope. - use_static_shapes: Whether to use an implementation with static shape - gurantees. - indicator_sum: An integer containing the sum of `indicator` vector. Only - required if `use_static_shape` is True. - - Returns: - subboxlist: a BoxList corresponding to the subset of the input BoxList - specified by indicator - Raises: - ValueError: if `indicator` is not a rank-1 boolean tensor. - """ - with tf.name_scope(scope, 'BooleanMask'): - if indicator.shape.ndims != 1: - raise ValueError('indicator should have rank 1') - if indicator.dtype != tf.bool: - raise ValueError('indicator should be a boolean tensor') - if use_static_shapes: - if not (indicator_sum and isinstance(indicator_sum, int)): - raise ValueError('`indicator_sum` must be a of type int') - selected_positions = tf.cast(indicator, dtype=tf.float32) - indexed_positions = tf.cast( - tf.multiply( - tf.cumsum(selected_positions), selected_positions), - dtype=tf.int32) - one_hot_selector = tf.one_hot( - indexed_positions - 1, indicator_sum, dtype=tf.float32) - sampled_indices = tf.cast( - tf.tensordot( - tf.cast(tf.range(tf.shape(indicator)[0]), dtype=tf.float32), - one_hot_selector, - axes=[0, 0]), - dtype=tf.int32) - return gather(boxlist, sampled_indices, use_static_shapes=True) - else: - subboxlist = box_list.BoxList(tf.boolean_mask(boxlist.get(), indicator)) - if fields is None: - fields = boxlist.get_extra_fields() - for field in fields: - if not boxlist.has_field(field): - raise ValueError('boxlist must contain all specified fields') - subfieldlist = tf.boolean_mask(boxlist.get_field(field), indicator) - subboxlist.add_field(field, subfieldlist) - return subboxlist - - -def gather(boxlist, indices, fields=None, scope=None, use_static_shapes=False): - """Gather boxes from BoxList according to indices and return new BoxList. - - By default, `gather` returns boxes corresponding to the input index list, as - well as all additional fields stored in the boxlist (indexing into the - first dimension). However one can optionally only gather from a - subset of fields. - - Args: - boxlist: BoxList holding N boxes - indices: a rank-1 tensor of type int32 / int64 - fields: (optional) list of fields to also gather from. If None (default), - all fields are gathered from. Pass an empty fields list to only gather - the box coordinates. - scope: name scope. - use_static_shapes: Whether to use an implementation with static shape - gurantees. - - Returns: - subboxlist: a BoxList corresponding to the subset of the input BoxList - specified by indices - Raises: - ValueError: if specified field is not contained in boxlist or if the - indices are not of type int32 - """ - with tf.name_scope(scope, 'Gather'): - if len(indices.shape.as_list()) != 1: - raise ValueError('indices should have rank 1') - if indices.dtype != tf.int32 and indices.dtype != tf.int64: - raise ValueError('indices should be an int32 / int64 tensor') - gather_op = tf.gather - if use_static_shapes: - gather_op = ops.matmul_gather_on_zeroth_axis - subboxlist = box_list.BoxList(gather_op(boxlist.get(), indices)) - if fields is None: - fields = boxlist.get_extra_fields() - fields += ['boxes'] - for field in fields: - if not boxlist.has_field(field): - raise ValueError('boxlist must contain all specified fields') - subfieldlist = gather_op(boxlist.get_field(field), indices) - subboxlist.add_field(field, subfieldlist) - return subboxlist - - -def concatenate(boxlists, fields=None, scope=None): - """Concatenate list of BoxLists. - - This op concatenates a list of input BoxLists into a larger BoxList. It also - handles concatenation of BoxList fields as long as the field tensor shapes - are equal except for the first dimension. - - Args: - boxlists: list of BoxList objects - fields: optional list of fields to also concatenate. By default, all - fields from the first BoxList in the list are included in the - concatenation. - scope: name scope. - - Returns: - a BoxList with number of boxes equal to - sum([boxlist.num_boxes() for boxlist in BoxList]) - Raises: - ValueError: if boxlists is invalid (i.e., is not a list, is empty, or - contains non BoxList objects), or if requested fields are not contained in - all boxlists - """ - with tf.name_scope(scope, 'Concatenate'): - if not isinstance(boxlists, list): - raise ValueError('boxlists should be a list') - if not boxlists: - raise ValueError('boxlists should have nonzero length') - for boxlist in boxlists: - if not isinstance(boxlist, box_list.BoxList): - raise ValueError('all elements of boxlists should be BoxList objects') - concatenated = box_list.BoxList( - tf.concat([boxlist.get() for boxlist in boxlists], 0)) - if fields is None: - fields = boxlists[0].get_extra_fields() - for field in fields: - first_field_shape = boxlists[0].get_field(field).get_shape().as_list() - first_field_shape[0] = -1 - if None in first_field_shape: - raise ValueError('field %s must have fully defined shape except for the' - ' 0th dimension.' % field) - for boxlist in boxlists: - if not boxlist.has_field(field): - raise ValueError('boxlist must contain all requested fields') - field_shape = boxlist.get_field(field).get_shape().as_list() - field_shape[0] = -1 - if field_shape != first_field_shape: - raise ValueError('field %s must have same shape for all boxlists ' - 'except for the 0th dimension.' % field) - concatenated_field = tf.concat( - [boxlist.get_field(field) for boxlist in boxlists], 0) - concatenated.add_field(field, concatenated_field) - return concatenated - - -def sort_by_field(boxlist, field, order=SortOrder.descend, scope=None): - """Sort boxes and associated fields according to a scalar field. - - A common use case is reordering the boxes according to descending scores. - - Args: - boxlist: BoxList holding N boxes. - field: A BoxList field for sorting and reordering the BoxList. - order: (Optional) descend or ascend. Default is descend. - scope: name scope. - - Returns: - sorted_boxlist: A sorted BoxList with the field in the specified order. - - Raises: - ValueError: if specified field does not exist - ValueError: if the order is not either descend or ascend - """ - with tf.name_scope(scope, 'SortByField'): - if order != SortOrder.descend and order != SortOrder.ascend: - raise ValueError('Invalid sort order') - - field_to_sort = boxlist.get_field(field) - if len(field_to_sort.shape.as_list()) != 1: - raise ValueError('Field should have rank 1') - - num_boxes = boxlist.num_boxes() - num_entries = tf.size(field_to_sort) - length_assert = tf.Assert( - tf.equal(num_boxes, num_entries), - ['Incorrect field size: actual vs expected.', num_entries, num_boxes]) - - with tf.control_dependencies([length_assert]): - _, sorted_indices = tf.nn.top_k(field_to_sort, num_boxes, sorted=True) - - if order == SortOrder.ascend: - sorted_indices = tf.reverse_v2(sorted_indices, [0]) - - return gather(boxlist, sorted_indices) - - -def visualize_boxes_in_image(image, boxlist, normalized=False, scope=None): - """Overlay bounding box list on image. - - Currently this visualization plots a 1 pixel thick red bounding box on top - of the image. Note that tf.image.draw_bounding_boxes essentially is - 1 indexed. - - Args: - image: an image tensor with shape [height, width, 3] - boxlist: a BoxList - normalized: (boolean) specify whether corners are to be interpreted - as absolute coordinates in image space or normalized with respect to the - image size. - scope: name scope. - - Returns: - image_and_boxes: an image tensor with shape [height, width, 3] - """ - with tf.name_scope(scope, 'VisualizeBoxesInImage'): - if not normalized: - height, width, _ = tf.unstack(tf.shape(image)) - boxlist = scale(boxlist, - 1.0 / tf.cast(height, tf.float32), - 1.0 / tf.cast(width, tf.float32)) - corners = tf.expand_dims(boxlist.get(), 0) - image = tf.expand_dims(image, 0) - return tf.squeeze(tf.image.draw_bounding_boxes(image, corners), [0]) - - -def filter_field_value_equals(boxlist, field, value, scope=None): - """Filter to keep only boxes with field entries equal to the given value. - - Args: - boxlist: BoxList holding N boxes. - field: field name for filtering. - value: scalar value. - scope: name scope. - - Returns: - a BoxList holding M boxes where M <= N - - Raises: - ValueError: if boxlist not a BoxList object or if it does not have - the specified field. - """ - with tf.name_scope(scope, 'FilterFieldValueEquals'): - if not isinstance(boxlist, box_list.BoxList): - raise ValueError('boxlist must be a BoxList') - if not boxlist.has_field(field): - raise ValueError('boxlist must contain the specified field') - filter_field = boxlist.get_field(field) - gather_index = tf.reshape(tf.where(tf.equal(filter_field, value)), [-1]) - return gather(boxlist, gather_index) - - -def filter_greater_than(boxlist, thresh, scope=None): - """Filter to keep only boxes with score exceeding a given threshold. - - This op keeps the collection of boxes whose corresponding scores are - greater than the input threshold. - - TODO(jonathanhuang): Change function name to filter_scores_greater_than - - Args: - boxlist: BoxList holding N boxes. Must contain a 'scores' field - representing detection scores. - thresh: scalar threshold - scope: name scope. - - Returns: - a BoxList holding M boxes where M <= N - - Raises: - ValueError: if boxlist not a BoxList object or if it does not - have a scores field - """ - with tf.name_scope(scope, 'FilterGreaterThan'): - if not isinstance(boxlist, box_list.BoxList): - raise ValueError('boxlist must be a BoxList') - if not boxlist.has_field('scores'): - raise ValueError('input boxlist must have \'scores\' field') - scores = boxlist.get_field('scores') - if len(scores.shape.as_list()) > 2: - raise ValueError('Scores should have rank 1 or 2') - if len(scores.shape.as_list()) == 2 and scores.shape.as_list()[1] != 1: - raise ValueError('Scores should have rank 1 or have shape ' - 'consistent with [None, 1]') - high_score_indices = tf.cast(tf.reshape( - tf.where(tf.greater(scores, thresh)), - [-1]), tf.int32) - return gather(boxlist, high_score_indices) - - -def non_max_suppression(boxlist, thresh, max_output_size, scope=None): - """Non maximum suppression. - - This op greedily selects a subset of detection bounding boxes, pruning - away boxes that have high IOU (intersection over union) overlap (> thresh) - with already selected boxes. Note that this only works for a single class --- - to apply NMS to multi-class predictions, use MultiClassNonMaxSuppression. - - Args: - boxlist: BoxList holding N boxes. Must contain a 'scores' field - representing detection scores. - thresh: scalar threshold - max_output_size: maximum number of retained boxes - scope: name scope. - - Returns: - a BoxList holding M boxes where M <= max_output_size - Raises: - ValueError: if thresh is not in [0, 1] - """ - with tf.name_scope(scope, 'NonMaxSuppression'): - if not 0 <= thresh <= 1.0: - raise ValueError('thresh must be between 0 and 1') - if not isinstance(boxlist, box_list.BoxList): - raise ValueError('boxlist must be a BoxList') - if not boxlist.has_field('scores'): - raise ValueError('input boxlist must have \'scores\' field') - selected_indices = tf.image.non_max_suppression( - boxlist.get(), boxlist.get_field('scores'), - max_output_size, iou_threshold=thresh) - return gather(boxlist, selected_indices) - - -def _copy_extra_fields(boxlist_to_copy_to, boxlist_to_copy_from): - """Copies the extra fields of boxlist_to_copy_from to boxlist_to_copy_to. - - Args: - boxlist_to_copy_to: BoxList to which extra fields are copied. - boxlist_to_copy_from: BoxList from which fields are copied. - - Returns: - boxlist_to_copy_to with extra fields. - """ - for field in boxlist_to_copy_from.get_extra_fields(): - boxlist_to_copy_to.add_field(field, boxlist_to_copy_from.get_field(field)) - return boxlist_to_copy_to - - -def to_normalized_coordinates(boxlist, height, width, - check_range=True, scope=None): - """Converts absolute box coordinates to normalized coordinates in [0, 1]. - - Usually one uses the dynamic shape of the image or conv-layer tensor: - boxlist = box_list_ops.to_normalized_coordinates(boxlist, - tf.shape(images)[1], - tf.shape(images)[2]), - - This function raises an assertion failed error at graph execution time when - the maximum coordinate is smaller than 1.01 (which means that coordinates are - already normalized). The value 1.01 is to deal with small rounding errors. - - Args: - boxlist: BoxList with coordinates in terms of pixel-locations. - height: Maximum value for height of absolute box coordinates. - width: Maximum value for width of absolute box coordinates. - check_range: If True, checks if the coordinates are normalized or not. - scope: name scope. - - Returns: - boxlist with normalized coordinates in [0, 1]. - """ - with tf.name_scope(scope, 'ToNormalizedCoordinates'): - height = tf.cast(height, tf.float32) - width = tf.cast(width, tf.float32) - - if check_range: - max_val = tf.reduce_max(boxlist.get()) - max_assert = tf.Assert(tf.greater(max_val, 1.01), - ['max value is lower than 1.01: ', max_val]) - with tf.control_dependencies([max_assert]): - width = tf.identity(width) - - return scale(boxlist, 1 / height, 1 / width) - - -def to_absolute_coordinates(boxlist, - height, - width, - check_range=True, - maximum_normalized_coordinate=1.1, - scope=None): - """Converts normalized box coordinates to absolute pixel coordinates. - - This function raises an assertion failed error when the maximum box coordinate - value is larger than maximum_normalized_coordinate (in which case coordinates - are already absolute). - - Args: - boxlist: BoxList with coordinates in range [0, 1]. - height: Maximum value for height of absolute box coordinates. - width: Maximum value for width of absolute box coordinates. - check_range: If True, checks if the coordinates are normalized or not. - maximum_normalized_coordinate: Maximum coordinate value to be considered - as normalized, default to 1.1. - scope: name scope. - - Returns: - boxlist with absolute coordinates in terms of the image size. - - """ - with tf.name_scope(scope, 'ToAbsoluteCoordinates'): - height = tf.cast(height, tf.float32) - width = tf.cast(width, tf.float32) - - # Ensure range of input boxes is correct. - if check_range: - box_maximum = tf.reduce_max(boxlist.get()) - max_assert = tf.Assert( - tf.greater_equal(maximum_normalized_coordinate, box_maximum), - ['maximum box coordinate value is larger ' - 'than %f: ' % maximum_normalized_coordinate, box_maximum]) - with tf.control_dependencies([max_assert]): - width = tf.identity(width) - - return scale(boxlist, height, width) - - -def refine_boxes_multi_class(pool_boxes, - num_classes, - nms_iou_thresh, - nms_max_detections, - voting_iou_thresh=0.5): - """Refines a pool of boxes using non max suppression and box voting. - - Box refinement is done independently for each class. - - Args: - pool_boxes: (BoxList) A collection of boxes to be refined. pool_boxes must - have a rank 1 'scores' field and a rank 1 'classes' field. - num_classes: (int scalar) Number of classes. - nms_iou_thresh: (float scalar) iou threshold for non max suppression (NMS). - nms_max_detections: (int scalar) maximum output size for NMS. - voting_iou_thresh: (float scalar) iou threshold for box voting. - - Returns: - BoxList of refined boxes. - - Raises: - ValueError: if - a) nms_iou_thresh or voting_iou_thresh is not in [0, 1]. - b) pool_boxes is not a BoxList. - c) pool_boxes does not have a scores and classes field. - """ - if not 0.0 <= nms_iou_thresh <= 1.0: - raise ValueError('nms_iou_thresh must be between 0 and 1') - if not 0.0 <= voting_iou_thresh <= 1.0: - raise ValueError('voting_iou_thresh must be between 0 and 1') - if not isinstance(pool_boxes, box_list.BoxList): - raise ValueError('pool_boxes must be a BoxList') - if not pool_boxes.has_field('scores'): - raise ValueError('pool_boxes must have a \'scores\' field') - if not pool_boxes.has_field('classes'): - raise ValueError('pool_boxes must have a \'classes\' field') - - refined_boxes = [] - for i in range(num_classes): - boxes_class = filter_field_value_equals(pool_boxes, 'classes', i) - refined_boxes_class = refine_boxes(boxes_class, nms_iou_thresh, - nms_max_detections, voting_iou_thresh) - refined_boxes.append(refined_boxes_class) - return sort_by_field(concatenate(refined_boxes), 'scores') - - -def refine_boxes(pool_boxes, - nms_iou_thresh, - nms_max_detections, - voting_iou_thresh=0.5): - """Refines a pool of boxes using non max suppression and box voting. - - Args: - pool_boxes: (BoxList) A collection of boxes to be refined. pool_boxes must - have a rank 1 'scores' field. - nms_iou_thresh: (float scalar) iou threshold for non max suppression (NMS). - nms_max_detections: (int scalar) maximum output size for NMS. - voting_iou_thresh: (float scalar) iou threshold for box voting. - - Returns: - BoxList of refined boxes. - - Raises: - ValueError: if - a) nms_iou_thresh or voting_iou_thresh is not in [0, 1]. - b) pool_boxes is not a BoxList. - c) pool_boxes does not have a scores field. - """ - if not 0.0 <= nms_iou_thresh <= 1.0: - raise ValueError('nms_iou_thresh must be between 0 and 1') - if not 0.0 <= voting_iou_thresh <= 1.0: - raise ValueError('voting_iou_thresh must be between 0 and 1') - if not isinstance(pool_boxes, box_list.BoxList): - raise ValueError('pool_boxes must be a BoxList') - if not pool_boxes.has_field('scores'): - raise ValueError('pool_boxes must have a \'scores\' field') - - nms_boxes = non_max_suppression( - pool_boxes, nms_iou_thresh, nms_max_detections) - return box_voting(nms_boxes, pool_boxes, voting_iou_thresh) - - -def box_voting(selected_boxes, pool_boxes, iou_thresh=0.5): - """Performs box voting as described in S. Gidaris and N. Komodakis, ICCV 2015. - - Performs box voting as described in 'Object detection via a multi-region & - semantic segmentation-aware CNN model', Gidaris and Komodakis, ICCV 2015. For - each box 'B' in selected_boxes, we find the set 'S' of boxes in pool_boxes - with iou overlap >= iou_thresh. The location of B is set to the weighted - average location of boxes in S (scores are used for weighting). And the score - of B is set to the average score of boxes in S. - - Args: - selected_boxes: BoxList containing a subset of boxes in pool_boxes. These - boxes are usually selected from pool_boxes using non max suppression. - pool_boxes: BoxList containing a set of (possibly redundant) boxes. - iou_thresh: (float scalar) iou threshold for matching boxes in - selected_boxes and pool_boxes. - - Returns: - BoxList containing averaged locations and scores for each box in - selected_boxes. - - Raises: - ValueError: if - a) selected_boxes or pool_boxes is not a BoxList. - b) if iou_thresh is not in [0, 1]. - c) pool_boxes does not have a scores field. - """ - if not 0.0 <= iou_thresh <= 1.0: - raise ValueError('iou_thresh must be between 0 and 1') - if not isinstance(selected_boxes, box_list.BoxList): - raise ValueError('selected_boxes must be a BoxList') - if not isinstance(pool_boxes, box_list.BoxList): - raise ValueError('pool_boxes must be a BoxList') - if not pool_boxes.has_field('scores'): - raise ValueError('pool_boxes must have a \'scores\' field') - - iou_ = iou(selected_boxes, pool_boxes) - match_indicator = tf.cast(tf.greater(iou_, iou_thresh), dtype=tf.float32) - num_matches = tf.reduce_sum(match_indicator, 1) - # TODO(kbanoop): Handle the case where some boxes in selected_boxes do not - # match to any boxes in pool_boxes. For such boxes without any matches, we - # should return the original boxes without voting. - match_assert = tf.Assert( - tf.reduce_all(tf.greater(num_matches, 0)), - ['Each box in selected_boxes must match with at least one box ' - 'in pool_boxes.']) - - scores = tf.expand_dims(pool_boxes.get_field('scores'), 1) - scores_assert = tf.Assert( - tf.reduce_all(tf.greater_equal(scores, 0)), - ['Scores must be non negative.']) - - with tf.control_dependencies([scores_assert, match_assert]): - sum_scores = tf.matmul(match_indicator, scores) - averaged_scores = tf.reshape(sum_scores, [-1]) / num_matches - - box_locations = tf.matmul(match_indicator, - pool_boxes.get() * scores) / sum_scores - averaged_boxes = box_list.BoxList(box_locations) - _copy_extra_fields(averaged_boxes, selected_boxes) - averaged_boxes.add_field('scores', averaged_scores) - return averaged_boxes - - -def pad_or_clip_box_list(boxlist, num_boxes, scope=None): - """Pads or clips all fields of a BoxList. - - Args: - boxlist: A BoxList with arbitrary of number of boxes. - num_boxes: First num_boxes in boxlist are kept. - The fields are zero-padded if num_boxes is bigger than the - actual number of boxes. - scope: name scope. - - Returns: - BoxList with all fields padded or clipped. - """ - with tf.name_scope(scope, 'PadOrClipBoxList'): - subboxlist = box_list.BoxList(shape_utils.pad_or_clip_tensor( - boxlist.get(), num_boxes)) - for field in boxlist.get_extra_fields(): - subfield = shape_utils.pad_or_clip_tensor( - boxlist.get_field(field), num_boxes) - subboxlist.add_field(field, subfield) - return subboxlist - - -def select_random_box(boxlist, - default_box=None, - seed=None, - scope=None): - """Selects a random bounding box from a `BoxList`. - - Args: - boxlist: A BoxList. - default_box: A [1, 4] float32 tensor. If no boxes are present in `boxlist`, - this default box will be returned. If None, will use a default box of - [[-1., -1., -1., -1.]]. - seed: Random seed. - scope: Name scope. - - Returns: - bbox: A [1, 4] tensor with a random bounding box. - valid: A bool tensor indicating whether a valid bounding box is returned - (True) or whether the default box is returned (False). - """ - with tf.name_scope(scope, 'SelectRandomBox'): - bboxes = boxlist.get() - combined_shape = shape_utils.combined_static_and_dynamic_shape(bboxes) - number_of_boxes = combined_shape[0] - default_box = default_box or tf.constant([[-1., -1., -1., -1.]]) - - def select_box(): - random_index = tf.random_uniform([], - maxval=number_of_boxes, - dtype=tf.int32, - seed=seed) - return tf.expand_dims(bboxes[random_index], axis=0), tf.constant(True) - - return tf.cond( - tf.greater_equal(number_of_boxes, 1), - true_fn=select_box, - false_fn=lambda: (default_box, tf.constant(False))) - - -def get_minimal_coverage_box(boxlist, - default_box=None, - scope=None): - """Creates a single bounding box which covers all boxes in the boxlist. - - Args: - boxlist: A Boxlist. - default_box: A [1, 4] float32 tensor. If no boxes are present in `boxlist`, - this default box will be returned. If None, will use a default box of - [[0., 0., 1., 1.]]. - scope: Name scope. - - Returns: - A [1, 4] float32 tensor with a bounding box that tightly covers all the - boxes in the box list. If the boxlist does not contain any boxes, the - default box is returned. - """ - with tf.name_scope(scope, 'CreateCoverageBox'): - num_boxes = boxlist.num_boxes() - - def coverage_box(bboxes): - y_min, x_min, y_max, x_max = tf.split( - value=bboxes, num_or_size_splits=4, axis=1) - y_min_coverage = tf.reduce_min(y_min, axis=0) - x_min_coverage = tf.reduce_min(x_min, axis=0) - y_max_coverage = tf.reduce_max(y_max, axis=0) - x_max_coverage = tf.reduce_max(x_max, axis=0) - return tf.stack( - [y_min_coverage, x_min_coverage, y_max_coverage, x_max_coverage], - axis=1) - - default_box = default_box or tf.constant([[0., 0., 1., 1.]]) - return tf.cond( - tf.greater_equal(num_boxes, 1), - true_fn=lambda: coverage_box(boxlist.get()), - false_fn=lambda: default_box) - - -def sample_boxes_by_jittering(boxlist, - num_boxes_to_sample, - stddev=0.1, - scope=None): - """Samples num_boxes_to_sample boxes by jittering around boxlist boxes. - - It is possible that this function might generate boxes with size 0. The larger - the stddev, this is more probable. For a small stddev of 0.1 this probability - is very small. - - Args: - boxlist: A boxlist containing N boxes in normalized coordinates. - num_boxes_to_sample: A positive integer containing the number of boxes to - sample. - stddev: Standard deviation. This is used to draw random offsets for the - box corners from a normal distribution. The offset is multiplied by the - box size so will be larger in terms of pixels for larger boxes. - scope: Name scope. - - Returns: - sampled_boxlist: A boxlist containing num_boxes_to_sample boxes in - normalized coordinates. - """ - with tf.name_scope(scope, 'SampleBoxesByJittering'): - num_boxes = boxlist.num_boxes() - box_indices = tf.random_uniform( - [num_boxes_to_sample], - minval=0, - maxval=num_boxes, - dtype=tf.int32) - sampled_boxes = tf.gather(boxlist.get(), box_indices) - sampled_boxes_height = sampled_boxes[:, 2] - sampled_boxes[:, 0] - sampled_boxes_width = sampled_boxes[:, 3] - sampled_boxes[:, 1] - rand_miny_gaussian = tf.random_normal([num_boxes_to_sample], stddev=stddev) - rand_minx_gaussian = tf.random_normal([num_boxes_to_sample], stddev=stddev) - rand_maxy_gaussian = tf.random_normal([num_boxes_to_sample], stddev=stddev) - rand_maxx_gaussian = tf.random_normal([num_boxes_to_sample], stddev=stddev) - miny = rand_miny_gaussian * sampled_boxes_height + sampled_boxes[:, 0] - minx = rand_minx_gaussian * sampled_boxes_width + sampled_boxes[:, 1] - maxy = rand_maxy_gaussian * sampled_boxes_height + sampled_boxes[:, 2] - maxx = rand_maxx_gaussian * sampled_boxes_width + sampled_boxes[:, 3] - maxy = tf.maximum(miny, maxy) - maxx = tf.maximum(minx, maxx) - sampled_boxes = tf.stack([miny, minx, maxy, maxx], axis=1) - sampled_boxes = tf.maximum(tf.minimum(sampled_boxes, 1.0), 0.0) - return box_list.BoxList(sampled_boxes) diff --git a/research/object_detection/core/box_list_ops_test.py b/research/object_detection/core/box_list_ops_test.py deleted file mode 100644 index 3ac642c6c47..00000000000 --- a/research/object_detection/core/box_list_ops_test.py +++ /dev/null @@ -1,1104 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.box_list_ops.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.utils import test_case - - -class BoxListOpsTest(test_case.TestCase): - """Tests for common bounding box operations.""" - - def test_area(self): - def graph_fn(): - corners = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]]) - boxes = box_list.BoxList(corners) - areas = box_list_ops.area(boxes) - return areas - areas_out = self.execute(graph_fn, []) - exp_output = [200.0, 4.0] - self.assertAllClose(areas_out, exp_output) - - def test_height_width(self): - def graph_fn(): - corners = tf.constant([[0.0, 0.0, 10.0, 20.0], [1.0, 2.0, 3.0, 4.0]]) - boxes = box_list.BoxList(corners) - return box_list_ops.height_width(boxes) - heights_out, widths_out = self.execute(graph_fn, []) - exp_output_heights = [10., 2.] - exp_output_widths = [20., 2.] - self.assertAllClose(heights_out, exp_output_heights) - self.assertAllClose(widths_out, exp_output_widths) - - def test_scale(self): - def graph_fn(): - corners = tf.constant([[0, 0, 100, 200], [50, 120, 100, 140]], - dtype=tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('extra_data', tf.constant([[1], [2]])) - - y_scale = tf.constant(1.0/100) - x_scale = tf.constant(1.0/200) - scaled_boxes = box_list_ops.scale(boxes, y_scale, x_scale) - return scaled_boxes.get(), scaled_boxes.get_field('extra_data') - scaled_corners_out, extra_data_out = self.execute(graph_fn, []) - exp_output = [[0, 0, 1, 1], [0.5, 0.6, 1.0, 0.7]] - self.assertAllClose(scaled_corners_out, exp_output) - self.assertAllEqual(extra_data_out, [[1], [2]]) - - def test_scale_height_width(self): - def graph_fn(): - corners = tf.constant([[-10, -20, 10, 20], [0, 100, 100, 200]], - dtype=tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('extra_data', tf.constant([[1], [2]])) - - y_scale = tf.constant(2.) - x_scale = tf.constant(0.5) - scaled_boxes = box_list_ops.scale_height_width(boxes, y_scale, x_scale) - return scaled_boxes.get(), scaled_boxes.get_field('extra_data') - exp_output = [ - [-20., -10, 20., 10], - [-50., 125, 150., 175.]] - scaled_corners_out, extra_data_out = self.execute(graph_fn, []) - self.assertAllClose(scaled_corners_out, exp_output) - self.assertAllEqual(extra_data_out, [[1], [2]]) - - def test_clip_to_window_filter_boxes_which_fall_outside_the_window( - self): - def graph_fn(): - window = tf.constant([0, 0, 9, 14], tf.float32) - corners = tf.constant([[5.0, 5.0, 6.0, 6.0], - [-1.0, -2.0, 4.0, 5.0], - [2.0, 3.0, 5.0, 9.0], - [0.0, 0.0, 9.0, 14.0], - [-100.0, -100.0, 300.0, 600.0], - [-10.0, -10.0, -9.0, -9.0]]) - boxes = box_list.BoxList(corners) - boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) - pruned = box_list_ops.clip_to_window( - boxes, window, filter_nonoverlapping=True) - return pruned.get(), pruned.get_field('extra_data') - exp_output = [[5.0, 5.0, 6.0, 6.0], [0.0, 0.0, 4.0, 5.0], - [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], - [0.0, 0.0, 9.0, 14.0]] - pruned_output, extra_data_out = self.execute_cpu(graph_fn, []) - self.assertAllClose(pruned_output, exp_output) - self.assertAllEqual(extra_data_out, [[1], [2], [3], [4], [5]]) - - def test_clip_to_window_without_filtering_boxes_which_fall_outside_the_window( - self): - def graph_fn(): - window = tf.constant([0, 0, 9, 14], tf.float32) - corners = tf.constant([[5.0, 5.0, 6.0, 6.0], - [-1.0, -2.0, 4.0, 5.0], - [2.0, 3.0, 5.0, 9.0], - [0.0, 0.0, 9.0, 14.0], - [-100.0, -100.0, 300.0, 600.0], - [-10.0, -10.0, -9.0, -9.0]]) - boxes = box_list.BoxList(corners) - boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) - pruned = box_list_ops.clip_to_window( - boxes, window, filter_nonoverlapping=False) - return pruned.get(), pruned.get_field('extra_data') - pruned_output, extra_data_out = self.execute(graph_fn, []) - exp_output = [[5.0, 5.0, 6.0, 6.0], [0.0, 0.0, 4.0, 5.0], - [2.0, 3.0, 5.0, 9.0], [0.0, 0.0, 9.0, 14.0], - [0.0, 0.0, 9.0, 14.0], [0.0, 0.0, 0.0, 0.0]] - self.assertAllClose(pruned_output, exp_output) - self.assertAllEqual(extra_data_out, [[1], [2], [3], [4], [5], [6]]) - - def test_prune_outside_window_filters_boxes_which_fall_outside_the_window( - self): - def graph_fn(): - window = tf.constant([0, 0, 9, 14], tf.float32) - corners = tf.constant([[5.0, 5.0, 6.0, 6.0], - [-1.0, -2.0, 4.0, 5.0], - [2.0, 3.0, 5.0, 9.0], - [0.0, 0.0, 9.0, 14.0], - [-10.0, -10.0, -9.0, -9.0], - [-100.0, -100.0, 300.0, 600.0]]) - boxes = box_list.BoxList(corners) - boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) - pruned, keep_indices = box_list_ops.prune_outside_window(boxes, window) - return pruned.get(), pruned.get_field('extra_data'), keep_indices - pruned_output, extra_data_out, keep_indices_out = self.execute_cpu(graph_fn, - []) - exp_output = [[5.0, 5.0, 6.0, 6.0], - [2.0, 3.0, 5.0, 9.0], - [0.0, 0.0, 9.0, 14.0]] - self.assertAllClose(pruned_output, exp_output) - self.assertAllEqual(keep_indices_out, [0, 2, 3]) - self.assertAllEqual(extra_data_out, [[1], [3], [4]]) - - def test_prune_completely_outside_window(self): - def graph_fn(): - window = tf.constant([0, 0, 9, 14], tf.float32) - corners = tf.constant([[5.0, 5.0, 6.0, 6.0], - [-1.0, -2.0, 4.0, 5.0], - [2.0, 3.0, 5.0, 9.0], - [0.0, 0.0, 9.0, 14.0], - [-10.0, -10.0, -9.0, -9.0], - [-100.0, -100.0, 300.0, 600.0]]) - boxes = box_list.BoxList(corners) - boxes.add_field('extra_data', tf.constant([[1], [2], [3], [4], [5], [6]])) - pruned, keep_indices = box_list_ops.prune_completely_outside_window( - boxes, window) - return pruned.get(), pruned.get_field('extra_data'), keep_indices - pruned_output, extra_data_out, keep_indices_out = self.execute(graph_fn, []) - exp_output = [[5.0, 5.0, 6.0, 6.0], - [-1.0, -2.0, 4.0, 5.0], - [2.0, 3.0, 5.0, 9.0], - [0.0, 0.0, 9.0, 14.0], - [-100.0, -100.0, 300.0, 600.0]] - self.assertAllClose(pruned_output, exp_output) - self.assertAllEqual(keep_indices_out, [0, 1, 2, 3, 5]) - self.assertAllEqual(extra_data_out, [[1], [2], [3], [4], [6]]) - - def test_prune_completely_outside_window_with_empty_boxlist(self): - def graph_fn(): - window = tf.constant([0, 0, 9, 14], tf.float32) - corners = tf.zeros(shape=[0, 4], dtype=tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('extra_data', tf.zeros(shape=[0], dtype=tf.int32)) - pruned, keep_indices = box_list_ops.prune_completely_outside_window( - boxes, window) - pruned_boxes = pruned.get() - extra = pruned.get_field('extra_data') - return pruned_boxes, extra, keep_indices - - pruned_boxes_out, extra_out, keep_indices_out = self.execute(graph_fn, []) - exp_pruned_boxes = np.zeros(shape=[0, 4], dtype=np.float32) - exp_extra = np.zeros(shape=[0], dtype=np.int32) - self.assertAllClose(exp_pruned_boxes, pruned_boxes_out) - self.assertAllEqual([], keep_indices_out) - self.assertAllEqual(exp_extra, extra_out) - - def test_intersection(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - intersect = box_list_ops.intersection(boxes1, boxes2) - return intersect - exp_output = [[2.0, 0.0, 6.0], [1.0, 0.0, 5.0]] - intersect_out = self.execute(graph_fn, []) - self.assertAllClose(intersect_out, exp_output) - - def test_matched_intersection(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - intersect = box_list_ops.matched_intersection(boxes1, boxes2) - return intersect - exp_output = [2.0, 0.0] - intersect_out = self.execute(graph_fn, []) - self.assertAllClose(intersect_out, exp_output) - - def test_iou(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - iou = box_list_ops.iou(boxes1, boxes2) - return iou - exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0], [1.0 / 16.0, 0.0, 5.0 / 400.0]] - iou_output = self.execute(graph_fn, []) - self.assertAllClose(iou_output, exp_output) - - def test_l1(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - l1 = box_list_ops.l1(boxes1, boxes2) - return l1 - exp_output = [[5.0, 22.5, 45.5], [8.5, 19.0, 40.0]] - l1_output = self.execute(graph_fn, []) - self.assertAllClose(l1_output, exp_output) - - def test_giou(self): - def graph_fn(): - corners1 = tf.constant([[5.0, 7.0, 7.0, 9.0]]) - corners2 = tf.constant([[5.0, 7.0, 7.0, 9.0], [5.0, 11.0, 7.0, 13.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - giou = box_list_ops.giou(boxes1, boxes2) - return giou - exp_output = [[1.0, -1.0 / 3.0]] - giou_output = self.execute(graph_fn, []) - self.assertAllClose(giou_output, exp_output) - - def test_matched_iou(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - iou = box_list_ops.matched_iou(boxes1, boxes2) - return iou - exp_output = [2.0 / 16.0, 0] - iou_output = self.execute(graph_fn, []) - self.assertAllClose(iou_output, exp_output) - - def test_iouworks_on_empty_inputs(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - boxes_empty = box_list.BoxList(tf.zeros((0, 4))) - iou_empty_1 = box_list_ops.iou(boxes1, boxes_empty) - iou_empty_2 = box_list_ops.iou(boxes_empty, boxes2) - iou_empty_3 = box_list_ops.iou(boxes_empty, boxes_empty) - return iou_empty_1, iou_empty_2, iou_empty_3 - iou_output_1, iou_output_2, iou_output_3 = self.execute(graph_fn, []) - self.assertAllEqual(iou_output_1.shape, (2, 0)) - self.assertAllEqual(iou_output_2.shape, (0, 3)) - self.assertAllEqual(iou_output_3.shape, (0, 0)) - - def test_ioa(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - ioa_1 = box_list_ops.ioa(boxes1, boxes2) - ioa_2 = box_list_ops.ioa(boxes2, boxes1) - return ioa_1, ioa_2 - exp_output_1 = [[2.0 / 12.0, 0, 6.0 / 400.0], - [1.0 / 12.0, 0.0, 5.0 / 400.0]] - exp_output_2 = [[2.0 / 6.0, 1.0 / 5.0], - [0, 0], - [6.0 / 6.0, 5.0 / 5.0]] - ioa_output_1, ioa_output_2 = self.execute(graph_fn, []) - self.assertAllClose(ioa_output_1, exp_output_1) - self.assertAllClose(ioa_output_2, exp_output_2) - - def test_prune_non_overlapping_boxes(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - minoverlap = 0.5 - - exp_output_1 = boxes1 - exp_output_2 = box_list.BoxList(tf.constant(0.0, shape=[0, 4])) - output_1, keep_indices_1 = box_list_ops.prune_non_overlapping_boxes( - boxes1, boxes2, min_overlap=minoverlap) - output_2, keep_indices_2 = box_list_ops.prune_non_overlapping_boxes( - boxes2, boxes1, min_overlap=minoverlap) - return (output_1.get(), keep_indices_1, output_2.get(), keep_indices_2, - exp_output_1.get(), exp_output_2.get()) - - (output_1_, keep_indices_1_, output_2_, keep_indices_2_, exp_output_1_, - exp_output_2_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(output_1_, exp_output_1_) - self.assertAllClose(output_2_, exp_output_2_) - self.assertAllEqual(keep_indices_1_, [0, 1]) - self.assertAllEqual(keep_indices_2_, []) - - def test_prune_small_boxes(self): - def graph_fn(): - boxes = tf.constant([[4.0, 3.0, 7.0, 5.0], - [5.0, 6.0, 10.0, 7.0], - [3.0, 4.0, 6.0, 8.0], - [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes = box_list.BoxList(boxes) - pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3) - return pruned_boxes.get() - exp_boxes = [[3.0, 4.0, 6.0, 8.0], - [0.0, 0.0, 20.0, 20.0]] - pruned_boxes = self.execute(graph_fn, []) - self.assertAllEqual(pruned_boxes, exp_boxes) - - def test_prune_small_boxes_prunes_boxes_with_negative_side(self): - def graph_fn(): - boxes = tf.constant([[4.0, 3.0, 7.0, 5.0], - [5.0, 6.0, 10.0, 7.0], - [3.0, 4.0, 6.0, 8.0], - [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0], - [2.0, 3.0, 1.5, 7.0], # negative height - [2.0, 3.0, 5.0, 1.7]]) # negative width - boxes = box_list.BoxList(boxes) - pruned_boxes = box_list_ops.prune_small_boxes(boxes, 3) - return pruned_boxes.get() - exp_boxes = [[3.0, 4.0, 6.0, 8.0], - [0.0, 0.0, 20.0, 20.0]] - pruned_boxes = self.execute_cpu(graph_fn, []) - self.assertAllEqual(pruned_boxes, exp_boxes) - - def test_change_coordinate_frame(self): - def graph_fn(): - corners = tf.constant([[0.25, 0.5, 0.75, 0.75], [0.5, 0.0, 1.0, 1.0]]) - window = tf.constant([0.25, 0.25, 0.75, 0.75]) - boxes = box_list.BoxList(corners) - - expected_corners = tf.constant([[0, 0.5, 1.0, 1.0], - [0.5, -0.5, 1.5, 1.5]]) - expected_boxes = box_list.BoxList(expected_corners) - output = box_list_ops.change_coordinate_frame(boxes, window) - return output.get(), expected_boxes.get() - output_, expected_boxes_ = self.execute(graph_fn, []) - self.assertAllClose(output_, expected_boxes_) - - def test_ioaworks_on_empty_inputs(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - boxes_empty = box_list.BoxList(tf.zeros((0, 4))) - ioa_empty_1 = box_list_ops.ioa(boxes1, boxes_empty) - ioa_empty_2 = box_list_ops.ioa(boxes_empty, boxes2) - ioa_empty_3 = box_list_ops.ioa(boxes_empty, boxes_empty) - return ioa_empty_1, ioa_empty_2, ioa_empty_3 - ioa_output_1, ioa_output_2, ioa_output_3 = self.execute(graph_fn, []) - self.assertAllEqual(ioa_output_1.shape, (2, 0)) - self.assertAllEqual(ioa_output_2.shape, (0, 3)) - self.assertAllEqual(ioa_output_3.shape, (0, 0)) - - def test_pairwise_distances(self): - def graph_fn(): - corners1 = tf.constant([[0.0, 0.0, 0.0, 0.0], - [1.0, 1.0, 0.0, 2.0]]) - corners2 = tf.constant([[3.0, 4.0, 1.0, 0.0], - [-4.0, 0.0, 0.0, 3.0], - [0.0, 0.0, 0.0, 0.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - dist_matrix = box_list_ops.sq_dist(boxes1, boxes2) - return dist_matrix - exp_output = [[26, 25, 0], [18, 27, 6]] - dist_output = self.execute(graph_fn, []) - self.assertAllClose(dist_output, exp_output) - - def test_boolean_mask(self): - def graph_fn(): - corners = tf.constant( - [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) - indicator = tf.constant([True, False, True, False, True], tf.bool) - boxes = box_list.BoxList(corners) - subset = box_list_ops.boolean_mask(boxes, indicator) - return subset.get() - expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] - subset_output = self.execute_cpu(graph_fn, []) - self.assertAllClose(subset_output, expected_subset) - - def test_static_boolean_mask_with_field(self): - - def graph_fn(corners, weights, indicator): - boxes = box_list.BoxList(corners) - boxes.add_field('weights', weights) - subset = box_list_ops.boolean_mask( - boxes, - indicator, ['weights'], - use_static_shapes=True, - indicator_sum=3) - return (subset.get_field('boxes'), subset.get_field('weights')) - - corners = np.array( - [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]], - dtype=np.float32) - indicator = np.array([True, False, True, False, True], dtype=bool) - weights = np.array([[.1], [.3], [.5], [.7], [.9]], dtype=np.float32) - result_boxes, result_weights = self.execute_cpu( - graph_fn, [corners, weights, indicator]) - expected_boxes = [4 * [0.0], 4 * [2.0], 4 * [4.0]] - expected_weights = [[.1], [.5], [.9]] - - self.assertAllClose(result_boxes, expected_boxes) - self.assertAllClose(result_weights, expected_weights) - - def test_gather(self): - def graph_fn(): - corners = tf.constant( - [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) - indices = tf.constant([0, 2, 4], tf.int32) - boxes = box_list.BoxList(corners) - subset = box_list_ops.gather(boxes, indices) - return subset.get() - expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] - subset_output = self.execute(graph_fn, []) - self.assertAllClose(subset_output, expected_subset) - - def test_static_gather_with_field(self): - - def graph_fn(corners, weights, indices): - boxes = box_list.BoxList(corners) - boxes.add_field('weights', weights) - subset = box_list_ops.gather( - boxes, indices, ['weights'], use_static_shapes=True) - return (subset.get_field('boxes'), subset.get_field('weights')) - - corners = np.array([4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], - 4 * [4.0]], dtype=np.float32) - weights = np.array([[.1], [.3], [.5], [.7], [.9]], dtype=np.float32) - indices = np.array([0, 2, 4], dtype=np.int32) - - result_boxes, result_weights = self.execute(graph_fn, - [corners, weights, indices]) - expected_boxes = [4 * [0.0], 4 * [2.0], 4 * [4.0]] - expected_weights = [[.1], [.5], [.9]] - self.assertAllClose(result_boxes, expected_boxes) - self.assertAllClose(result_weights, expected_weights) - - def test_gather_with_invalid_field(self): - corners = tf.constant([4 * [0.0], 4 * [1.0]]) - indices = tf.constant([0, 1], tf.int32) - weights = tf.constant([[.1], [.3]], tf.float32) - - boxes = box_list.BoxList(corners) - boxes.add_field('weights', weights) - with self.assertRaises(ValueError): - box_list_ops.gather(boxes, indices, ['foo', 'bar']) - - def test_gather_with_invalid_inputs(self): - corners = tf.constant( - [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) - indices_float32 = tf.constant([0, 2, 4], tf.float32) - boxes = box_list.BoxList(corners) - with self.assertRaises(ValueError): - _ = box_list_ops.gather(boxes, indices_float32) - indices_2d = tf.constant([[0, 2, 4]], tf.int32) - boxes = box_list.BoxList(corners) - with self.assertRaises(ValueError): - _ = box_list_ops.gather(boxes, indices_2d) - - def test_gather_with_dynamic_indexing(self): - def graph_fn(): - corners = tf.constant( - [4 * [0.0], 4 * [1.0], 4 * [2.0], 4 * [3.0], 4 * [4.0]]) - weights = tf.constant([.5, .3, .7, .1, .9], tf.float32) - indices = tf.reshape(tf.where(tf.greater(weights, 0.4)), [-1]) - boxes = box_list.BoxList(corners) - boxes.add_field('weights', weights) - subset = box_list_ops.gather(boxes, indices, ['weights']) - return subset.get(), subset.get_field('weights') - expected_subset = [4 * [0.0], 4 * [2.0], 4 * [4.0]] - expected_weights = [.5, .7, .9] - subset_output, weights_output = self.execute(graph_fn, []) - self.assertAllClose(subset_output, expected_subset) - self.assertAllClose(weights_output, expected_weights) - - def test_sort_by_field_ascending_order(self): - exp_corners = [[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], - [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]] - exp_scores = [.95, .9, .75, .6, .5, .3] - exp_weights = [.2, .45, .6, .75, .8, .92] - - def graph_fn(): - shuffle = [2, 4, 0, 5, 1, 3] - corners = tf.constant([exp_corners[i] for i in shuffle], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant( - [exp_scores[i] for i in shuffle], tf.float32)) - boxes.add_field('weights', tf.constant( - [exp_weights[i] for i in shuffle], tf.float32)) - sort_by_weight = box_list_ops.sort_by_field( - boxes, - 'weights', - order=box_list_ops.SortOrder.ascend) - return [sort_by_weight.get(), sort_by_weight.get_field('scores'), - sort_by_weight.get_field('weights')] - corners_out, scores_out, weights_out = self.execute(graph_fn, []) - self.assertAllClose(corners_out, exp_corners) - self.assertAllClose(scores_out, exp_scores) - self.assertAllClose(weights_out, exp_weights) - - def test_sort_by_field_descending_order(self): - exp_corners = [[0, 0, 1, 1], [0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], - [0, 10, 1, 11], [0, 10.1, 1, 11.1], [0, 100, 1, 101]] - exp_scores = [.95, .9, .75, .6, .5, .3] - exp_weights = [.2, .45, .6, .75, .8, .92] - - def graph_fn(): - shuffle = [2, 4, 0, 5, 1, 3] - corners = tf.constant([exp_corners[i] for i in shuffle], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant( - [exp_scores[i] for i in shuffle], tf.float32)) - boxes.add_field('weights', tf.constant( - [exp_weights[i] for i in shuffle], tf.float32)) - sort_by_score = box_list_ops.sort_by_field(boxes, 'scores') - return (sort_by_score.get(), sort_by_score.get_field('scores'), - sort_by_score.get_field('weights')) - - corners_out, scores_out, weights_out = self.execute(graph_fn, []) - self.assertAllClose(corners_out, exp_corners) - self.assertAllClose(scores_out, exp_scores) - self.assertAllClose(weights_out, exp_weights) - - def test_sort_by_field_invalid_inputs(self): - corners = tf.constant([4 * [0.0], 4 * [0.5], 4 * [1.0], 4 * [2.0], 4 * - [3.0], 4 * [4.0]]) - misc = tf.constant([[.95, .9], [.5, .3]], tf.float32) - weights = tf.constant([[.1, .2]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('misc', misc) - boxes.add_field('weights', weights) - - with self.assertRaises(ValueError): - box_list_ops.sort_by_field(boxes, 'area') - - with self.assertRaises(ValueError): - box_list_ops.sort_by_field(boxes, 'misc') - - with self.assertRaises(ValueError): - box_list_ops.sort_by_field(boxes, 'weights') - - def test_visualize_boxes_in_image(self): - def graph_fn(): - image = tf.zeros((6, 4, 3)) - corners = tf.constant([[0, 0, 5, 3], - [0, 0, 3, 2]], tf.float32) - boxes = box_list.BoxList(corners) - image_and_boxes = box_list_ops.visualize_boxes_in_image(image, boxes) - image_and_boxes_bw = tf.cast( - tf.greater(tf.reduce_sum(image_and_boxes, 2), 0.0), dtype=tf.float32) - return image_and_boxes_bw - exp_result = [[1, 1, 1, 0], - [1, 1, 1, 0], - [1, 1, 1, 0], - [1, 0, 1, 0], - [1, 1, 1, 0], - [0, 0, 0, 0]] - output = self.execute_cpu(graph_fn, []) - self.assertAllEqual(output.astype(int), exp_result) - - def test_filter_field_value_equals(self): - def graph_fn(): - corners = tf.constant([[0, 0, 1, 1], - [0, 0.1, 1, 1.1], - [0, -0.1, 1, 0.9], - [0, 10, 1, 11], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('classes', tf.constant([1, 2, 1, 2, 2, 1])) - filtered_boxes1 = box_list_ops.filter_field_value_equals( - boxes, 'classes', 1) - filtered_boxes2 = box_list_ops.filter_field_value_equals( - boxes, 'classes', 2) - return filtered_boxes1.get(), filtered_boxes2.get() - exp_output1 = [[0, 0, 1, 1], [0, -0.1, 1, 0.9], [0, 100, 1, 101]] - exp_output2 = [[0, 0.1, 1, 1.1], [0, 10, 1, 11], [0, 10.1, 1, 11.1]] - filtered_output1, filtered_output2 = self.execute_cpu(graph_fn, []) - self.assertAllClose(filtered_output1, exp_output1) - self.assertAllClose(filtered_output2, exp_output2) - - def test_filter_greater_than(self): - def graph_fn(): - corners = tf.constant([[0, 0, 1, 1], - [0, 0.1, 1, 1.1], - [0, -0.1, 1, 0.9], - [0, 10, 1, 11], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant([.1, .75, .9, .5, .5, .8])) - thresh = .6 - filtered_boxes = box_list_ops.filter_greater_than(boxes, thresh) - return filtered_boxes.get() - exp_output = [[0, 0.1, 1, 1.1], [0, -0.1, 1, 0.9], [0, 100, 1, 101]] - filtered_output = self.execute_cpu(graph_fn, []) - self.assertAllClose(filtered_output, exp_output) - - def test_clip_box_list(self): - def graph_fn(): - boxlist = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], - [0.6, 0.6, 0.8, 0.8], [0.2, 0.2, 0.3, 0.3]], tf.float32)) - boxlist.add_field('classes', tf.constant([0, 0, 1, 1])) - boxlist.add_field('scores', tf.constant([0.75, 0.65, 0.3, 0.2])) - num_boxes = 2 - clipped_boxlist = box_list_ops.pad_or_clip_box_list(boxlist, num_boxes) - return (clipped_boxlist.get(), clipped_boxlist.get_field('classes'), - clipped_boxlist.get_field('scores')) - - expected_boxes = [[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]] - expected_classes = [0, 0] - expected_scores = [0.75, 0.65] - boxes_out, classes_out, scores_out = self.execute(graph_fn, []) - - self.assertAllClose(expected_boxes, boxes_out) - self.assertAllEqual(expected_classes, classes_out) - self.assertAllClose(expected_scores, scores_out) - - def test_pad_box_list(self): - def graph_fn(): - boxlist = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], tf.float32)) - boxlist.add_field('classes', tf.constant([0, 1])) - boxlist.add_field('scores', tf.constant([0.75, 0.2])) - num_boxes = 4 - padded_boxlist = box_list_ops.pad_or_clip_box_list(boxlist, num_boxes) - return (padded_boxlist.get(), padded_boxlist.get_field('classes'), - padded_boxlist.get_field('scores')) - expected_boxes = [[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], - [0, 0, 0, 0], [0, 0, 0, 0]] - expected_classes = [0, 1, 0, 0] - expected_scores = [0.75, 0.2, 0, 0] - boxes_out, classes_out, scores_out = self.execute(graph_fn, []) - self.assertAllClose(expected_boxes, boxes_out) - self.assertAllEqual(expected_classes, classes_out) - self.assertAllClose(expected_scores, scores_out) - - def test_select_random_box(self): - boxes = [[0., 0., 1., 1.], - [0., 1., 2., 3.], - [0., 2., 3., 4.]] - def graph_fn(): - corners = tf.constant(boxes, dtype=tf.float32) - boxlist = box_list.BoxList(corners) - random_bbox, valid = box_list_ops.select_random_box(boxlist) - return random_bbox, valid - random_bbox_out, valid_out = self.execute(graph_fn, []) - norm_small = any( - [np.linalg.norm(random_bbox_out - box) < 1e-6 for box in boxes]) - self.assertTrue(norm_small) - self.assertTrue(valid_out) - - def test_select_random_box_with_empty_boxlist(self): - def graph_fn(): - corners = tf.constant([], shape=[0, 4], dtype=tf.float32) - boxlist = box_list.BoxList(corners) - random_bbox, valid = box_list_ops.select_random_box(boxlist) - return random_bbox, valid - random_bbox_out, valid_out = self.execute_cpu(graph_fn, []) - expected_bbox_out = np.array([[-1., -1., -1., -1.]], dtype=np.float32) - self.assertAllEqual(expected_bbox_out, random_bbox_out) - self.assertFalse(valid_out) - - def test_get_minimal_coverage_box(self): - def graph_fn(): - boxes = [[0., 0., 1., 1.], - [-1., 1., 2., 3.], - [0., 2., 3., 4.]] - corners = tf.constant(boxes, dtype=tf.float32) - boxlist = box_list.BoxList(corners) - coverage_box = box_list_ops.get_minimal_coverage_box(boxlist) - return coverage_box - coverage_box_out = self.execute(graph_fn, []) - expected_coverage_box = [[-1., 0., 3., 4.]] - self.assertAllClose(expected_coverage_box, coverage_box_out) - - def test_get_minimal_coverage_box_with_empty_boxlist(self): - def graph_fn(): - corners = tf.constant([], shape=[0, 4], dtype=tf.float32) - boxlist = box_list.BoxList(corners) - coverage_box = box_list_ops.get_minimal_coverage_box(boxlist) - return coverage_box - coverage_box_out = self.execute(graph_fn, []) - self.assertAllClose([[0.0, 0.0, 1.0, 1.0]], coverage_box_out) - - -class ConcatenateTest(test_case.TestCase): - - def test_invalid_input_box_list_list(self): - with self.assertRaises(ValueError): - box_list_ops.concatenate(None) - with self.assertRaises(ValueError): - box_list_ops.concatenate([]) - with self.assertRaises(ValueError): - corners = tf.constant([[0, 0, 0, 0]], tf.float32) - boxlist = box_list.BoxList(corners) - box_list_ops.concatenate([boxlist, 2]) - - def test_concatenate_with_missing_fields(self): - corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32) - scores1 = tf.constant([1.0, 2.1]) - corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8]], tf.float32) - boxlist1 = box_list.BoxList(corners1) - boxlist1.add_field('scores', scores1) - boxlist2 = box_list.BoxList(corners2) - with self.assertRaises(ValueError): - box_list_ops.concatenate([boxlist1, boxlist2]) - - def test_concatenate_with_incompatible_field_shapes(self): - corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32) - scores1 = tf.constant([1.0, 2.1]) - corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8]], tf.float32) - scores2 = tf.constant([[1.0, 1.0], [2.1, 3.2]]) - boxlist1 = box_list.BoxList(corners1) - boxlist1.add_field('scores', scores1) - boxlist2 = box_list.BoxList(corners2) - boxlist2.add_field('scores', scores2) - with self.assertRaises(ValueError): - box_list_ops.concatenate([boxlist1, boxlist2]) - - def test_concatenate_is_correct(self): - def graph_fn(): - corners1 = tf.constant([[0, 0, 0, 0], [1, 2, 3, 4]], tf.float32) - scores1 = tf.constant([1.0, 2.1]) - corners2 = tf.constant([[0, 3, 1, 6], [2, 4, 3, 8], [1, 0, 5, 10]], - tf.float32) - scores2 = tf.constant([1.0, 2.1, 5.6]) - boxlist1 = box_list.BoxList(corners1) - boxlist1.add_field('scores', scores1) - boxlist2 = box_list.BoxList(corners2) - boxlist2.add_field('scores', scores2) - result = box_list_ops.concatenate([boxlist1, boxlist2]) - return result.get(), result.get_field('scores') - exp_corners = [[0, 0, 0, 0], - [1, 2, 3, 4], - [0, 3, 1, 6], - [2, 4, 3, 8], - [1, 0, 5, 10]] - exp_scores = [1.0, 2.1, 1.0, 2.1, 5.6] - corners_output, scores_output = self.execute(graph_fn, []) - self.assertAllClose(corners_output, exp_corners) - self.assertAllClose(scores_output, exp_scores) - - -class NonMaxSuppressionTest(test_case.TestCase): - - def test_select_from_three_clusters(self): - def graph_fn(): - corners = tf.constant([[0, 0, 1, 1], - [0, 0.1, 1, 1.1], - [0, -0.1, 1, 0.9], - [0, 10, 1, 11], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant([.9, .75, .6, .95, .5, .3])) - iou_thresh = .5 - max_output_size = 3 - nms = box_list_ops.non_max_suppression( - boxes, iou_thresh, max_output_size) - return nms.get() - exp_nms = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 100, 1, 101]] - nms_output = self.execute_cpu(graph_fn, []) - self.assertAllClose(nms_output, exp_nms) - - def test_select_at_most_two_boxes_from_three_clusters(self): - def graph_fn(): - corners = tf.constant([[0, 0, 1, 1], - [0, 0.1, 1, 1.1], - [0, -0.1, 1, 0.9], - [0, 10, 1, 11], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant([.9, .75, .6, .95, .5, .3])) - iou_thresh = .5 - max_output_size = 2 - nms = box_list_ops.non_max_suppression( - boxes, iou_thresh, max_output_size) - return nms.get() - exp_nms = [[0, 10, 1, 11], - [0, 0, 1, 1]] - nms_output = self.execute_cpu(graph_fn, []) - self.assertAllClose(nms_output, exp_nms) - - def test_select_at_most_thirty_boxes_from_three_clusters(self): - def graph_fn(): - corners = tf.constant([[0, 0, 1, 1], - [0, 0.1, 1, 1.1], - [0, -0.1, 1, 0.9], - [0, 10, 1, 11], - [0, 10.1, 1, 11.1], - [0, 100, 1, 101]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant([.9, .75, .6, .95, .5, .3])) - iou_thresh = .5 - max_output_size = 30 - nms = box_list_ops.non_max_suppression( - boxes, iou_thresh, max_output_size) - return nms.get() - exp_nms = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 100, 1, 101]] - nms_output = self.execute_cpu(graph_fn, []) - self.assertAllClose(nms_output, exp_nms) - - def test_select_single_box(self): - def graph_fn(): - corners = tf.constant([[0, 0, 1, 1]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant([.9])) - iou_thresh = .5 - max_output_size = 3 - nms = box_list_ops.non_max_suppression( - boxes, iou_thresh, max_output_size) - return nms.get() - exp_nms = [[0, 0, 1, 1]] - nms_output = self.execute_cpu(graph_fn, []) - self.assertAllClose(nms_output, exp_nms) - - def test_select_from_ten_identical_boxes(self): - def graph_fn(): - corners = tf.constant(10 * [[0, 0, 1, 1]], tf.float32) - boxes = box_list.BoxList(corners) - boxes.add_field('scores', tf.constant(10 * [.9])) - iou_thresh = .5 - max_output_size = 3 - nms = box_list_ops.non_max_suppression( - boxes, iou_thresh, max_output_size) - return nms.get() - exp_nms = [[0, 0, 1, 1]] - nms_output = self.execute_cpu(graph_fn, []) - self.assertAllClose(nms_output, exp_nms) - - def test_copy_extra_fields(self): - tensor1 = np.array([[1], [4]]) - tensor2 = np.array([[1, 1], [2, 2]]) - def graph_fn(): - corners = tf.constant([[0, 0, 1, 1], - [0, 0.1, 1, 1.1]], tf.float32) - boxes = box_list.BoxList(corners) - - boxes.add_field('tensor1', tf.constant(tensor1)) - boxes.add_field('tensor2', tf.constant(tensor2)) - new_boxes = box_list.BoxList(tf.constant([[0, 0, 10, 10], - [1, 3, 5, 5]], tf.float32)) - new_boxes = box_list_ops._copy_extra_fields(new_boxes, boxes) - return new_boxes.get_field('tensor1'), new_boxes.get_field('tensor2') - tensor1_out, tensor2_out = self.execute_cpu(graph_fn, []) - self.assertAllClose(tensor1, tensor1_out) - self.assertAllClose(tensor2, tensor2_out) - - -class CoordinatesConversionTest(test_case.TestCase): - - def test_to_normalized_coordinates(self): - def graph_fn(): - coordinates = tf.constant([[0, 0, 100, 100], - [25, 25, 75, 75]], tf.float32) - img = tf.ones((128, 100, 100, 3)) - boxlist = box_list.BoxList(coordinates) - normalized_boxlist = box_list_ops.to_normalized_coordinates( - boxlist, tf.shape(img)[1], tf.shape(img)[2]) - return normalized_boxlist.get() - expected_boxes = [[0, 0, 1, 1], - [0.25, 0.25, 0.75, 0.75]] - normalized_boxes = self.execute(graph_fn, []) - self.assertAllClose(normalized_boxes, expected_boxes) - - def test_to_normalized_coordinates_already_normalized(self): - def graph_fn(): - coordinates = tf.constant([[0, 0, 1, 1], - [0.25, 0.25, 0.75, 0.75]], tf.float32) - img = tf.ones((128, 100, 100, 3)) - boxlist = box_list.BoxList(coordinates) - normalized_boxlist = box_list_ops.to_normalized_coordinates( - boxlist, tf.shape(img)[1], tf.shape(img)[2]) - return normalized_boxlist.get() - with self.assertRaisesOpError('assertion failed'): - self.execute_cpu(graph_fn, []) - - def test_to_absolute_coordinates(self): - def graph_fn(): - coordinates = tf.constant([[0, 0, 1, 1], - [0.25, 0.25, 0.75, 0.75]], tf.float32) - img = tf.ones((128, 100, 100, 3)) - boxlist = box_list.BoxList(coordinates) - absolute_boxlist = box_list_ops.to_absolute_coordinates(boxlist, - tf.shape(img)[1], - tf.shape(img)[2]) - return absolute_boxlist.get() - expected_boxes = [[0, 0, 100, 100], - [25, 25, 75, 75]] - absolute_boxes = self.execute(graph_fn, []) - self.assertAllClose(absolute_boxes, expected_boxes) - - def test_to_absolute_coordinates_already_abolute(self): - def graph_fn(): - coordinates = tf.constant([[0, 0, 100, 100], - [25, 25, 75, 75]], tf.float32) - img = tf.ones((128, 100, 100, 3)) - boxlist = box_list.BoxList(coordinates) - absolute_boxlist = box_list_ops.to_absolute_coordinates(boxlist, - tf.shape(img)[1], - tf.shape(img)[2]) - return absolute_boxlist.get() - with self.assertRaisesOpError('assertion failed'): - self.execute_cpu(graph_fn, []) - - def test_convert_to_normalized_and_back(self): - coordinates = np.random.uniform(size=(100, 4)) - coordinates = np.round(np.sort(coordinates) * 200) - coordinates[:, 2:4] += 1 - coordinates[99, :] = [0, 0, 201, 201] - def graph_fn(): - img = tf.ones((128, 202, 202, 3)) - - boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32)) - boxlist = box_list_ops.to_normalized_coordinates(boxlist, - tf.shape(img)[1], - tf.shape(img)[2]) - boxlist = box_list_ops.to_absolute_coordinates(boxlist, - tf.shape(img)[1], - tf.shape(img)[2]) - return boxlist.get() - out = self.execute(graph_fn, []) - self.assertAllClose(out, coordinates) - - def test_convert_to_absolute_and_back(self): - coordinates = np.random.uniform(size=(100, 4)) - coordinates = np.sort(coordinates) - coordinates[99, :] = [0, 0, 1, 1] - def graph_fn(): - img = tf.ones((128, 202, 202, 3)) - boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32)) - boxlist = box_list_ops.to_absolute_coordinates(boxlist, - tf.shape(img)[1], - tf.shape(img)[2]) - boxlist = box_list_ops.to_normalized_coordinates(boxlist, - tf.shape(img)[1], - tf.shape(img)[2]) - return boxlist.get() - out = self.execute(graph_fn, []) - self.assertAllClose(out, coordinates) - - def test_to_absolute_coordinates_maximum_coordinate_check(self): - def graph_fn(): - coordinates = tf.constant([[0, 0, 1.2, 1.2], - [0.25, 0.25, 0.75, 0.75]], tf.float32) - img = tf.ones((128, 100, 100, 3)) - boxlist = box_list.BoxList(coordinates) - absolute_boxlist = box_list_ops.to_absolute_coordinates( - boxlist, - tf.shape(img)[1], - tf.shape(img)[2], - maximum_normalized_coordinate=1.1) - return absolute_boxlist.get() - with self.assertRaisesOpError('assertion failed'): - self.execute_cpu(graph_fn, []) - - -class BoxRefinementTest(test_case.TestCase): - - def test_box_voting(self): - def graph_fn(): - candidates = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], [0.6, 0.6, 0.8, 0.8]], tf.float32)) - candidates.add_field('ExtraField', tf.constant([1, 2])) - pool = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], - [0.6, 0.6, 0.8, 0.8]], tf.float32)) - pool.add_field('scores', tf.constant([0.75, 0.25, 0.3])) - averaged_boxes = box_list_ops.box_voting(candidates, pool) - return (averaged_boxes.get(), averaged_boxes.get_field('scores'), - averaged_boxes.get_field('ExtraField')) - - expected_boxes = [[0.1, 0.1, 0.425, 0.425], [0.6, 0.6, 0.8, 0.8]] - expected_scores = [0.5, 0.3] - boxes_out, scores_out, extra_field_out = self.execute(graph_fn, []) - self.assertAllClose(expected_boxes, boxes_out) - self.assertAllClose(expected_scores, scores_out) - self.assertAllEqual(extra_field_out, [1, 2]) - - def test_box_voting_fails_with_negative_scores(self): - def graph_fn(): - candidates = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) - pool = box_list.BoxList(tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) - pool.add_field('scores', tf.constant([-0.2])) - averaged_boxes = box_list_ops.box_voting(candidates, pool) - return averaged_boxes.get() - - with self.assertRaisesOpError('Scores must be non negative'): - self.execute_cpu(graph_fn, []) - - def test_box_voting_fails_when_unmatched(self): - def graph_fn(): - candidates = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4]], tf.float32)) - pool = box_list.BoxList(tf.constant([[0.6, 0.6, 0.8, 0.8]], tf.float32)) - pool.add_field('scores', tf.constant([0.2])) - averaged_boxes = box_list_ops.box_voting(candidates, pool) - return averaged_boxes.get() - with self.assertRaisesOpError('Each box in selected_boxes must match ' - 'with at least one box in pool_boxes.'): - self.execute_cpu(graph_fn, []) - - def test_refine_boxes(self): - def graph_fn(): - pool = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], - [0.6, 0.6, 0.8, 0.8]], tf.float32)) - pool.add_field('ExtraField', tf.constant([1, 2, 3])) - pool.add_field('scores', tf.constant([0.75, 0.25, 0.3])) - averaged_boxes = box_list_ops.refine_boxes(pool, 0.5, 10) - return (averaged_boxes.get(), averaged_boxes.get_field('scores'), - averaged_boxes.get_field('ExtraField')) - boxes_out, scores_out, extra_field_out = self.execute_cpu(graph_fn, []) - expected_boxes = [[0.1, 0.1, 0.425, 0.425], [0.6, 0.6, 0.8, 0.8]] - expected_scores = [0.5, 0.3] - self.assertAllClose(expected_boxes, boxes_out) - self.assertAllClose(expected_scores, scores_out) - self.assertAllEqual(extra_field_out, [1, 3]) - - def test_refine_boxes_multi_class(self): - def graph_fn(): - pool = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5], - [0.6, 0.6, 0.8, 0.8], [0.2, 0.2, 0.3, 0.3]], tf.float32)) - pool.add_field('classes', tf.constant([0, 0, 1, 1])) - pool.add_field('scores', tf.constant([0.75, 0.25, 0.3, 0.2])) - averaged_boxes = box_list_ops.refine_boxes_multi_class(pool, 3, 0.5, 10) - return (averaged_boxes.get(), averaged_boxes.get_field('scores'), - averaged_boxes.get_field('classes')) - boxes_out, scores_out, extra_field_out = self.execute_cpu(graph_fn, []) - expected_boxes = [[0.1, 0.1, 0.425, 0.425], [0.6, 0.6, 0.8, 0.8], - [0.2, 0.2, 0.3, 0.3]] - expected_scores = [0.5, 0.3, 0.2] - self.assertAllClose(expected_boxes, boxes_out) - self.assertAllClose(expected_scores, scores_out) - self.assertAllEqual(extra_field_out, [0, 1, 1]) - - def test_sample_boxes_by_jittering(self): - def graph_fn(): - boxes = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], - [0.1, 0.1, 0.5, 0.5], - [0.6, 0.6, 0.8, 0.8], - [0.2, 0.2, 0.3, 0.3]], tf.float32)) - sampled_boxes = box_list_ops.sample_boxes_by_jittering( - boxlist=boxes, num_boxes_to_sample=10) - iou = box_list_ops.iou(boxes, sampled_boxes) - iou_max = tf.reduce_max(iou, axis=0) - return sampled_boxes.get(), iou_max - np_sampled_boxes, np_iou_max = self.execute(graph_fn, []) - self.assertAllEqual(np_sampled_boxes.shape, [10, 4]) - self.assertAllGreater(np_iou_max, 0.3) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/box_list_test.py b/research/object_detection/core/box_list_test.py deleted file mode 100644 index c1389dbf8ae..00000000000 --- a/research/object_detection/core/box_list_test.py +++ /dev/null @@ -1,121 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.box_list.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import box_list -from object_detection.utils import test_case - - -class BoxListTest(test_case.TestCase): - """Tests for BoxList class.""" - - def test_num_boxes(self): - def graph_fn(): - data = tf.constant([[0, 0, 1, 1], [1, 1, 2, 3], [3, 4, 5, 5]], tf.float32) - boxes = box_list.BoxList(data) - return boxes.num_boxes() - num_boxes_out = self.execute(graph_fn, []) - self.assertEqual(num_boxes_out, 3) - - def test_get_correct_center_coordinates_and_sizes(self): - boxes = np.array([[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]], - np.float32) - def graph_fn(boxes): - boxes = box_list.BoxList(boxes) - centers_sizes = boxes.get_center_coordinates_and_sizes() - return centers_sizes - centers_sizes_out = self.execute(graph_fn, [boxes]) - expected_centers_sizes = [[15, 0.35], [12.5, 0.25], [10, 0.3], [5, 0.3]] - self.assertAllClose(centers_sizes_out, expected_centers_sizes) - - def test_create_box_list_with_dynamic_shape(self): - def graph_fn(): - data = tf.constant([[0, 0, 1, 1], [1, 1, 2, 3], [3, 4, 5, 5]], tf.float32) - indices = tf.reshape(tf.where(tf.greater([1, 0, 1], 0)), [-1]) - data = tf.gather(data, indices) - assert data.get_shape().as_list() == [None, 4] - boxes = box_list.BoxList(data) - return boxes.num_boxes() - num_boxes = self.execute(graph_fn, []) - self.assertEqual(num_boxes, 2) - - def test_transpose_coordinates(self): - boxes = np.array([[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]], - np.float32) - def graph_fn(boxes): - boxes = box_list.BoxList(boxes) - boxes.transpose_coordinates() - return boxes.get() - transpoded_boxes = self.execute(graph_fn, [boxes]) - expected_corners = [[10.0, 10.0, 15.0, 20.0], [0.1, 0.2, 0.4, 0.5]] - self.assertAllClose(transpoded_boxes, expected_corners) - - def test_box_list_invalid_inputs(self): - data0 = tf.constant([[[0, 0, 1, 1], [3, 4, 5, 5]]], tf.float32) - data1 = tf.constant([[0, 0, 1], [1, 1, 2], [3, 4, 5]], tf.float32) - data2 = tf.constant([[0, 0, 1], [1, 1, 2], [3, 4, 5]], tf.int32) - - with self.assertRaises(ValueError): - _ = box_list.BoxList(data0) - with self.assertRaises(ValueError): - _ = box_list.BoxList(data1) - with self.assertRaises(ValueError): - _ = box_list.BoxList(data2) - - def test_num_boxes_static(self): - box_corners = [[10.0, 10.0, 20.0, 15.0], [0.2, 0.1, 0.5, 0.4]] - boxes = box_list.BoxList(tf.constant(box_corners)) - self.assertEqual(boxes.num_boxes_static(), 2) - self.assertEqual(type(boxes.num_boxes_static()), int) - - def test_as_tensor_dict(self): - boxes = tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], - tf.float32) - boxlist = box_list.BoxList(boxes) - classes = tf.constant([0, 1]) - boxlist.add_field('classes', classes) - scores = tf.constant([0.75, 0.2]) - boxlist.add_field('scores', scores) - tensor_dict = boxlist.as_tensor_dict() - - self.assertDictEqual(tensor_dict, {'scores': scores, 'classes': classes, - 'boxes': boxes}) - - def test_as_tensor_dict_with_features(self): - boxes = tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], - tf.float32) - boxlist = box_list.BoxList(boxes) - classes = tf.constant([0, 1]) - boxlist.add_field('classes', classes) - scores = tf.constant([0.75, 0.2]) - boxlist.add_field('scores', scores) - tensor_dict = boxlist.as_tensor_dict(['scores', 'classes']) - - self.assertDictEqual(tensor_dict, {'scores': scores, 'classes': classes}) - - def test_as_tensor_dict_missing_field(self): - boxlist = box_list.BoxList( - tf.constant([[0.1, 0.1, 0.4, 0.4], [0.1, 0.1, 0.5, 0.5]], tf.float32)) - boxlist.add_field('classes', tf.constant([0, 1])) - boxlist.add_field('scores', tf.constant([0.75, 0.2])) - with self.assertRaises(ValueError): - boxlist.as_tensor_dict(['foo', 'bar']) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/box_predictor.py b/research/object_detection/core/box_predictor.py deleted file mode 100644 index 27d77d299bf..00000000000 --- a/research/object_detection/core/box_predictor.py +++ /dev/null @@ -1,227 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Box predictor for object detectors. - -Box predictors are classes that take a high level -image feature map as input and produce two predictions, -(1) a tensor encoding box locations, and -(2) a tensor encoding classes for each box. - -These components are passed directly to loss functions -in our detection models. - -These modules are separated from the main model since the same -few box predictor architectures are shared across many models. -""" -from abc import abstractmethod -import tensorflow.compat.v1 as tf - -BOX_ENCODINGS = 'box_encodings' -CLASS_PREDICTIONS_WITH_BACKGROUND = 'class_predictions_with_background' -MASK_PREDICTIONS = 'mask_predictions' - - -class BoxPredictor(object): - """BoxPredictor.""" - - def __init__(self, is_training, num_classes): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - """ - self._is_training = is_training - self._num_classes = num_classes - - @property - def is_keras_model(self): - return False - - @property - def num_classes(self): - return self._num_classes - - def predict(self, image_features, num_predictions_per_location, - scope=None, **params): - """Computes encoded object locations and corresponding confidences. - - Takes a list of high level image feature maps as input and produces a list - of box encodings and a list of class scores where each element in the output - lists correspond to the feature maps in the input list. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - num_predictions_per_location: A list of integers representing the number - of box predictions to be made per spatial location for each feature map. - scope: Variable and Op scope name. - **params: Additional keyword arguments for specific implementations of - BoxPredictor. - - Returns: - A dictionary containing at least the following tensors. - box_encodings: A list of float tensors. Each entry in the list - corresponds to a feature map in the input `image_features` list. All - tensors in the list have one of the two following shapes: - a. [batch_size, num_anchors_i, q, code_size] representing the location - of the objects, where q is 1 or the number of classes. - b. [batch_size, num_anchors_i, code_size]. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - - Raises: - ValueError: If length of `image_features` is not equal to length of - `num_predictions_per_location`. - """ - if len(image_features) != len(num_predictions_per_location): - raise ValueError('image_feature and num_predictions_per_location must ' - 'be of same length, found: {} vs {}'. - format(len(image_features), - len(num_predictions_per_location))) - if scope is not None: - with tf.variable_scope(scope): - return self._predict(image_features, num_predictions_per_location, - **params) - return self._predict(image_features, num_predictions_per_location, - **params) - - # TODO(rathodv): num_predictions_per_location could be moved to constructor. - # This is currently only used by ConvolutionalBoxPredictor. - @abstractmethod - def _predict(self, image_features, num_predictions_per_location, **params): - """Implementations must override this method. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - num_predictions_per_location: A list of integers representing the number - of box predictions to be made per spatial location for each feature map. - **params: Additional keyword arguments for specific implementations of - BoxPredictor. - - Returns: - A dictionary containing at least the following tensors. - box_encodings: A list of float tensors. Each entry in the list - corresponds to a feature map in the input `image_features` list. All - tensors in the list have one of the two following shapes: - a. [batch_size, num_anchors_i, q, code_size] representing the location - of the objects, where q is 1 or the number of classes. - b. [batch_size, num_anchors_i, code_size]. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - """ - pass - - -class KerasBoxPredictor(tf.keras.layers.Layer): - """Keras-based BoxPredictor.""" - - def __init__(self, is_training, num_classes, freeze_batchnorm, - inplace_batchnorm_update, name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - """ - super(KerasBoxPredictor, self).__init__(name=name) - - self._is_training = is_training - self._num_classes = num_classes - self._freeze_batchnorm = freeze_batchnorm - self._inplace_batchnorm_update = inplace_batchnorm_update - - @property - def is_keras_model(self): - return True - - @property - def num_classes(self): - return self._num_classes - - def call(self, image_features, **kwargs): - """Computes encoded object locations and corresponding confidences. - - Takes a list of high level image feature maps as input and produces a list - of box encodings and a list of class scores where each element in the output - lists correspond to the feature maps in the input list. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - **kwargs: Additional keyword arguments for specific implementations of - BoxPredictor. - - Returns: - A dictionary containing at least the following tensors. - box_encodings: A list of float tensors. Each entry in the list - corresponds to a feature map in the input `image_features` list. All - tensors in the list have one of the two following shapes: - a. [batch_size, num_anchors_i, q, code_size] representing the location - of the objects, where q is 1 or the number of classes. - b. [batch_size, num_anchors_i, code_size]. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - """ - return self._predict(image_features, **kwargs) - - @abstractmethod - def _predict(self, image_features, **kwargs): - """Implementations must override this method. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - **kwargs: Additional keyword arguments for specific implementations of - BoxPredictor. - - Returns: - A dictionary containing at least the following tensors. - box_encodings: A list of float tensors. Each entry in the list - corresponds to a feature map in the input `image_features` list. All - tensors in the list have one of the two following shapes: - a. [batch_size, num_anchors_i, q, code_size] representing the location - of the objects, where q is 1 or the number of classes. - b. [batch_size, num_anchors_i, code_size]. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - """ - raise NotImplementedError diff --git a/research/object_detection/core/class_agnostic_nms_test.py b/research/object_detection/core/class_agnostic_nms_test.py deleted file mode 100644 index ed205c51d36..00000000000 --- a/research/object_detection/core/class_agnostic_nms_test.py +++ /dev/null @@ -1,144 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for google3.third_party.tensorflow_models.object_detection.core.class_agnostic_nms.""" -from absl.testing import parameterized -import tensorflow.compat.v1 as tf -from object_detection.core import post_processing -from object_detection.core import standard_fields as fields -from object_detection.utils import test_case - - -class ClassAgnosticNonMaxSuppressionTest(test_case.TestCase, - parameterized.TestCase): - - def test_class_agnostic_nms_select_with_shared_boxes(self): - def graph_fn(): - boxes = tf.constant( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], tf.float32) - scores = tf.constant([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]) - score_thresh = 0.1 - iou_thresh = .5 - max_classes_per_detection = 1 - max_output_size = 4 - nms, _ = post_processing.class_agnostic_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, max_classes_per_detection, - max_output_size) - return (nms.get(), nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes)) - - exp_nms_corners = [[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002], - [0, 100, 1, 101]] - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - - (nms_corners_output, nms_scores_output, - nms_classes_output) = self.execute_cpu(graph_fn, []) - self.assertAllClose(nms_corners_output, exp_nms_corners) - self.assertAllClose(nms_scores_output, exp_nms_scores) - self.assertAllClose(nms_classes_output, exp_nms_classes) - - - def test_class_agnostic_nms_select_with_per_class_boxes(self): - def graph_fn(): - boxes = tf.constant( - [[[4, 5, 9, 10], [0, 0, 1, 1]], - [[0, 0.1, 1, 1.1], [4, 5, 9, 10]], - [[0, -0.1, 1, 0.9], [4, 5, 9, 10]], - [[0, 10, 1, 11], [4, 5, 9, 10]], - [[0, 10.1, 1, 11.1], [4, 5, 9, 10]], - [[0, 100, 1, 101], [4, 5, 9, 10]], - [[4, 5, 9, 10], [0, 1000, 1, 1002]], - [[4, 5, 9, 10], [0, 1000, 1, 1002.1]]], tf.float32) - scores = tf.constant([[.01, 0.9], - [.75, 0.05], - [.6, 0.01], - [.95, 0], - [.5, 0.01], - [.3, 0.01], - [.01, .85], - [.01, .5]]) - score_thresh = 0.1 - iou_thresh = .5 - max_classes_per_detection = 1 - max_output_size = 4 - nms, _ = post_processing.class_agnostic_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, max_classes_per_detection, - max_output_size) - return (nms.get(), nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes)) - (nms_corners_output, nms_scores_output, - nms_classes_output) = self.execute_cpu(graph_fn, []) - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 100, 1, 101]] - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 1, 1, 0] - self.assertAllClose(nms_corners_output, exp_nms_corners) - self.assertAllClose(nms_scores_output, exp_nms_scores) - self.assertAllClose(nms_classes_output, exp_nms_classes) - - # Two cases will be tested here: using / not using static shapes. - # Named the two test cases for easier control during testing, with a flag of - # '--test_filter=ClassAgnosticNonMaxSuppressionTest.test_batch_classagnostic_nms_with_batch_size_1' - # or - # '--test_filter=ClassAgnosticNonMaxSuppressionTest.test_batch_classagnostic_nms_with_batch_size_1_use_static_shapes'. - @parameterized.named_parameters(('', False), ('_use_static_shapes', True)) - def test_batch_classagnostic_nms_with_batch_size_1(self, - use_static_shapes=False): - def graph_fn(): - boxes = tf.constant( - [[[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]]], tf.float32) - scores = tf.constant([[[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]]]) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - max_classes_per_detection = 1 - use_class_agnostic_nms = True - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, - num_detections) = post_processing.batch_multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_size_per_class=max_output_size, - max_total_size=max_output_size, - use_class_agnostic_nms=use_class_agnostic_nms, - use_static_shapes=use_static_shapes, - max_classes_per_detection=max_classes_per_detection) - self.assertIsNone(nmsed_masks) - self.assertIsNone(nmsed_additional_fields) - return (nmsed_boxes, nmsed_scores, nmsed_classes, num_detections) - exp_nms_corners = [[[0, 10, 1, 11], [0, 0, 1, 1], [0, 1000, 1, 1002], - [0, 100, 1, 101]]] - exp_nms_scores = [[.95, .9, .85, .3]] - exp_nms_classes = [[0, 0, 1, 0]] - (nmsed_boxes, nmsed_scores, nmsed_classes, - num_detections) = self.execute_cpu(graph_fn, []) - self.assertAllClose(nmsed_boxes, exp_nms_corners) - self.assertAllClose(nmsed_scores, exp_nms_scores) - self.assertAllClose(nmsed_classes, exp_nms_classes) - self.assertEqual(num_detections, [4]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/data_decoder.py b/research/object_detection/core/data_decoder.py deleted file mode 100644 index 87ddf72c1b0..00000000000 --- a/research/object_detection/core/data_decoder.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Interface for data decoders. - -Data decoders decode the input data and return a dictionary of tensors keyed by -the entries in core.reader.Fields. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from abc import ABCMeta -from abc import abstractmethod -import six - - -class DataDecoder(six.with_metaclass(ABCMeta, object)): - """Interface for data decoders.""" - - @abstractmethod - def decode(self, data): - """Return a single image and associated labels. - - Args: - data: a string tensor holding a serialized protocol buffer corresponding - to data for a single image. - - Returns: - tensor_dict: a dictionary containing tensors. Possible keys are defined in - reader.Fields. - """ - pass diff --git a/research/object_detection/core/data_parser.py b/research/object_detection/core/data_parser.py deleted file mode 100644 index 889545db78f..00000000000 --- a/research/object_detection/core/data_parser.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Interface for data parsers. - -Data parser parses input data and returns a dictionary of numpy arrays -keyed by the entries in standard_fields.py. Since the parser parses records -to numpy arrays (materialized tensors) directly, it is used to read data for -evaluation/visualization; to parse the data during training, DataDecoder should -be used. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from abc import ABCMeta -from abc import abstractmethod -import six - - -class DataToNumpyParser(six.with_metaclass(ABCMeta, object)): - """Abstract interface for data parser that produces numpy arrays.""" - - @abstractmethod - def parse(self, input_data): - """Parses input and returns a numpy array or a dictionary of numpy arrays. - - Args: - input_data: an input data - - Returns: - A numpy array or a dictionary of numpy arrays or None, if input - cannot be parsed. - """ - pass diff --git a/research/object_detection/core/densepose_ops.py b/research/object_detection/core/densepose_ops.py deleted file mode 100644 index 8dd8f39bafa..00000000000 --- a/research/object_detection/core/densepose_ops.py +++ /dev/null @@ -1,380 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""DensePose operations. - -DensePose part ids are represented as tensors of shape -[num_instances, num_points] and coordinates are represented as tensors of shape -[num_instances, num_points, 4] where each point holds (y, x, v, u). The location -of the DensePose sampled point is (y, x) in normalized coordinates. The surface -coordinate (in the part coordinate frame) is (v, u). Note that dim 1 of both -tensors may contain padding, since the number of sampled points per instance -is not fixed. The value `num_points` represents the maximum number of sampled -points for an instance in the example. -""" -import os - -import numpy as np -import scipy.io -import tensorflow.compat.v1 as tf - -from object_detection.utils import shape_utils - -PART_NAMES = [ - b'torso_back', b'torso_front', b'right_hand', b'left_hand', b'left_foot', - b'right_foot', b'right_upper_leg_back', b'left_upper_leg_back', - b'right_upper_leg_front', b'left_upper_leg_front', b'right_lower_leg_back', - b'left_lower_leg_back', b'right_lower_leg_front', b'left_lower_leg_front', - b'left_upper_arm_back', b'right_upper_arm_back', b'left_upper_arm_front', - b'right_upper_arm_front', b'left_lower_arm_back', b'right_lower_arm_back', - b'left_lower_arm_front', b'right_lower_arm_front', b'right_face', - b'left_face', -] - - -def scale(dp_surface_coords, y_scale, x_scale, scope=None): - """Scales DensePose coordinates in y and x dimensions. - - Args: - dp_surface_coords: a tensor of shape [num_instances, num_points, 4], with - coordinates in (y, x, v, u) format. - y_scale: (float) scalar tensor - x_scale: (float) scalar tensor - scope: name scope. - - Returns: - new_dp_surface_coords: a tensor of shape [num_instances, num_points, 4] - """ - with tf.name_scope(scope, 'DensePoseScale'): - y_scale = tf.cast(y_scale, tf.float32) - x_scale = tf.cast(x_scale, tf.float32) - new_keypoints = dp_surface_coords * [[[y_scale, x_scale, 1, 1]]] - return new_keypoints - - -def clip_to_window(dp_surface_coords, window, scope=None): - """Clips DensePose points to a window. - - This op clips any input DensePose points to a window. - - Args: - dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose surface coordinates in (y, x, v, u) format. - window: a tensor of shape [4] representing the [y_min, x_min, y_max, x_max] - window to which the op should clip the keypoints. - scope: name scope. - - Returns: - new_dp_surface_coords: a tensor of shape [num_instances, num_points, 4]. - """ - with tf.name_scope(scope, 'DensePoseClipToWindow'): - y, x, v, u = tf.split(value=dp_surface_coords, num_or_size_splits=4, axis=2) - win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window) - y = tf.maximum(tf.minimum(y, win_y_max), win_y_min) - x = tf.maximum(tf.minimum(x, win_x_max), win_x_min) - new_dp_surface_coords = tf.concat([y, x, v, u], 2) - return new_dp_surface_coords - - -def prune_outside_window(dp_num_points, dp_part_ids, dp_surface_coords, window, - scope=None): - """Prunes DensePose points that fall outside a given window. - - This function replaces points that fall outside the given window with zeros. - See also clip_to_window which clips any DensePose points that fall outside the - given window. - - Note that this operation uses dynamic shapes, and therefore is not currently - suitable for TPU. - - Args: - dp_num_points: a tensor of shape [num_instances] that indicates how many - (non-padded) DensePose points there are per instance. - dp_part_ids: a tensor of shape [num_instances, num_points] with DensePose - part ids. These part_ids are 0-indexed, where the first non-background - part has index 0. - dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose surface coordinates in (y, x, v, u) format. - window: a tensor of shape [4] representing the [y_min, x_min, y_max, x_max] - window outside of which the op should prune the points. - scope: name scope. - - Returns: - new_dp_num_points: a tensor of shape [num_instances] that indicates how many - (non-padded) DensePose points there are per instance after pruning. - new_dp_part_ids: a tensor of shape [num_instances, num_points] with - DensePose part ids. These part_ids are 0-indexed, where the first - non-background part has index 0. - new_dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose surface coordinates after pruning. - """ - with tf.name_scope(scope, 'DensePosePruneOutsideWindow'): - y, x, _, _ = tf.unstack(dp_surface_coords, axis=-1) - win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window) - - num_instances, num_points = shape_utils.combined_static_and_dynamic_shape( - dp_part_ids) - dp_num_points_tiled = tf.tile(dp_num_points[:, tf.newaxis], - multiples=[1, num_points]) - range_tiled = tf.tile(tf.range(num_points)[tf.newaxis, :], - multiples=[num_instances, 1]) - valid_initial = range_tiled < dp_num_points_tiled - valid_in_window = tf.logical_and( - tf.logical_and(y >= win_y_min, y <= win_y_max), - tf.logical_and(x >= win_x_min, x <= win_x_max)) - valid_indices = tf.logical_and(valid_initial, valid_in_window) - - new_dp_num_points = tf.math.reduce_sum( - tf.cast(valid_indices, tf.int32), axis=1) - max_num_points = tf.math.reduce_max(new_dp_num_points) - - def gather_and_reshuffle(elems): - dp_part_ids, dp_surface_coords, valid_indices = elems - locs = tf.where(valid_indices)[:, 0] - valid_part_ids = tf.gather(dp_part_ids, locs, axis=0) - valid_part_ids_padded = shape_utils.pad_or_clip_nd( - valid_part_ids, output_shape=[max_num_points]) - valid_surface_coords = tf.gather(dp_surface_coords, locs, axis=0) - valid_surface_coords_padded = shape_utils.pad_or_clip_nd( - valid_surface_coords, output_shape=[max_num_points, 4]) - return [valid_part_ids_padded, valid_surface_coords_padded] - - new_dp_part_ids, new_dp_surface_coords = ( - shape_utils.static_or_dynamic_map_fn( - gather_and_reshuffle, - elems=[dp_part_ids, dp_surface_coords, valid_indices], - dtype=[tf.int32, tf.float32], - back_prop=False)) - return new_dp_num_points, new_dp_part_ids, new_dp_surface_coords - - -def change_coordinate_frame(dp_surface_coords, window, scope=None): - """Changes coordinate frame of the points to be relative to window's frame. - - Given a window of the form [y_min, x_min, y_max, x_max] in normalized - coordinates, changes DensePose coordinates to be relative to this window. - - An example use case is data augmentation: where we are given groundtruth - points and would like to randomly crop the image to some window. In this - case we need to change the coordinate frame of each sampled point to be - relative to this new window. - - Args: - dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose surface coordinates in (y, x, v, u) format. - window: a tensor of shape [4] representing the [y_min, x_min, y_max, x_max] - window we should change the coordinate frame to. - scope: name scope. - - Returns: - new_dp_surface_coords: a tensor of shape [num_instances, num_points, 4]. - """ - with tf.name_scope(scope, 'DensePoseChangeCoordinateFrame'): - win_height = window[2] - window[0] - win_width = window[3] - window[1] - new_dp_surface_coords = scale( - dp_surface_coords - [window[0], window[1], 0, 0], - 1.0 / win_height, 1.0 / win_width) - return new_dp_surface_coords - - -def to_normalized_coordinates(dp_surface_coords, height, width, - check_range=True, scope=None): - """Converts absolute DensePose coordinates to normalized in range [0, 1]. - - This function raises an assertion failed error at graph execution time when - the maximum coordinate is smaller than 1.01 (which means that coordinates are - already normalized). The value 1.01 is to deal with small rounding errors. - - Args: - dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose absolute surface coordinates in (y, x, v, u) format. - height: Height of image. - width: Width of image. - check_range: If True, checks if the coordinates are already normalized. - scope: name scope. - - Returns: - A tensor of shape [num_instances, num_points, 4] with normalized - coordinates. - """ - with tf.name_scope(scope, 'DensePoseToNormalizedCoordinates'): - height = tf.cast(height, tf.float32) - width = tf.cast(width, tf.float32) - - if check_range: - max_val = tf.reduce_max(dp_surface_coords[:, :, :2]) - max_assert = tf.Assert(tf.greater(max_val, 1.01), - ['max value is lower than 1.01: ', max_val]) - with tf.control_dependencies([max_assert]): - width = tf.identity(width) - - return scale(dp_surface_coords, 1.0 / height, 1.0 / width) - - -def to_absolute_coordinates(dp_surface_coords, height, width, - check_range=True, scope=None): - """Converts normalized DensePose coordinates to absolute pixel coordinates. - - This function raises an assertion failed error when the maximum - coordinate value is larger than 1.01 (in which case coordinates are already - absolute). - - Args: - dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose normalized surface coordinates in (y, x, v, u) format. - height: Height of image. - width: Width of image. - check_range: If True, checks if the coordinates are normalized or not. - scope: name scope. - - Returns: - A tensor of shape [num_instances, num_points, 4] with absolute coordinates. - """ - with tf.name_scope(scope, 'DensePoseToAbsoluteCoordinates'): - height = tf.cast(height, tf.float32) - width = tf.cast(width, tf.float32) - - if check_range: - max_val = tf.reduce_max(dp_surface_coords[:, :, :2]) - max_assert = tf.Assert(tf.greater_equal(1.01, max_val), - ['maximum coordinate value is larger than 1.01: ', - max_val]) - with tf.control_dependencies([max_assert]): - width = tf.identity(width) - - return scale(dp_surface_coords, height, width) - - -class DensePoseHorizontalFlip(object): - """Class responsible for horizontal flipping of parts and surface coords.""" - - def __init__(self): - """Constructor.""" - - path = os.path.dirname(os.path.abspath(__file__)) - uv_symmetry_transforms_path = tf.resource_loader.get_path_to_datafile( - os.path.join(path, '..', 'dataset_tools', 'densepose', - 'UV_symmetry_transforms.mat')) - tf.logging.info('Loading DensePose symmetry transforms file from {}'.format( - uv_symmetry_transforms_path)) - with tf.io.gfile.GFile(uv_symmetry_transforms_path, 'rb') as f: - data = scipy.io.loadmat(f) - - # Create lookup maps which indicate how a VU coordinate changes after a - # horizontal flip. - uv_symmetry_map = {} - for key in ('U_transforms', 'V_transforms'): - uv_symmetry_map_per_part = [] - for i in range(data[key].shape[1]): - # The following tensor has shape [256, 256]. The raw data is stored as - # uint8 values, so convert to float and scale to the range [0., 1.] - data_normalized = data[key][0, i].astype(np.float32) / 255. - map_per_part = tf.constant(data_normalized, dtype=tf.float32) - uv_symmetry_map_per_part.append(map_per_part) - uv_symmetry_map[key] = tf.reshape( - tf.stack(uv_symmetry_map_per_part, axis=0), [-1]) - # The following dictionary contains flattened lookup maps for the U and V - # coordinates separately. The shape of each is [24 * 256 * 256]. - self.uv_symmetries = uv_symmetry_map - - # Create a list of that maps part index to flipped part index (0-indexed). - part_symmetries = [] - for i, part_name in enumerate(PART_NAMES): - if b'left' in part_name: - part_symmetries.append(PART_NAMES.index( - part_name.replace(b'left', b'right'))) - elif b'right' in part_name: - part_symmetries.append(PART_NAMES.index( - part_name.replace(b'right', b'left'))) - else: - part_symmetries.append(i) - self.part_symmetries = part_symmetries - - def flip_parts_and_coords(self, part_ids, vu): - """Flips part ids and coordinates. - - Args: - part_ids: a [num_instances, num_points] int32 tensor with pre-flipped part - ids. These part_ids are 0-indexed, where the first non-background part - has index 0. - vu: a [num_instances, num_points, 2] float32 tensor with pre-flipped vu - normalized coordinates. - - Returns: - new_part_ids: a [num_instances, num_points] int32 tensor with post-flipped - part ids. These part_ids are 0-indexed, where the first non-background - part has index 0. - new_vu: a [num_instances, num_points, 2] float32 tensor with post-flipped - vu coordinates. - """ - num_instances, num_points = shape_utils.combined_static_and_dynamic_shape( - part_ids) - part_ids_flattened = tf.reshape(part_ids, [-1]) - new_part_ids_flattened = tf.gather(self.part_symmetries, part_ids_flattened) - new_part_ids = tf.reshape(new_part_ids_flattened, - [num_instances, num_points]) - - # Convert VU floating point coordinates to values in [256, 256] grid. - vu = tf.math.minimum(tf.math.maximum(vu, 0.0), 1.0) - vu_locs = tf.cast(vu * 256., dtype=tf.int32) - vu_locs_flattened = tf.reshape(vu_locs, [-1, 2]) - v_locs_flattened, u_locs_flattened = tf.unstack(vu_locs_flattened, axis=1) - - # Convert vu_locs into lookup indices (in flattened part symmetries map). - symmetry_lookup_inds = ( - part_ids_flattened * 65536 + 256 * v_locs_flattened + u_locs_flattened) - - # New VU coordinates. - v_new = tf.gather(self.uv_symmetries['V_transforms'], symmetry_lookup_inds) - u_new = tf.gather(self.uv_symmetries['U_transforms'], symmetry_lookup_inds) - new_vu_flattened = tf.stack([v_new, u_new], axis=1) - new_vu = tf.reshape(new_vu_flattened, [num_instances, num_points, 2]) - - return new_part_ids, new_vu - - -def flip_horizontal(dp_part_ids, dp_surface_coords, scope=None): - """Flips the DensePose points horizontally around the flip_point. - - This operation flips dense pose annotations horizontally. Note that part ids - and surface coordinates may or may not change as a result of the flip. - - Args: - dp_part_ids: a tensor of shape [num_instances, num_points] with DensePose - part ids. These part_ids are 0-indexed, where the first non-background - part has index 0. - dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose surface coordinates in (y, x, v, u) normalized format. - scope: name scope. - - Returns: - new_dp_part_ids: a tensor of shape [num_instances, num_points] with - DensePose part ids after flipping. - new_dp_surface_coords: a tensor of shape [num_instances, num_points, 4] with - DensePose surface coordinates after flipping. - """ - with tf.name_scope(scope, 'DensePoseFlipHorizontal'): - # First flip x coordinate. - y, x, vu = tf.split(dp_surface_coords, num_or_size_splits=[1, 1, 2], axis=2) - xflipped = 1.0 - x - - # Flip part ids and surface coordinates. - horizontal_flip = DensePoseHorizontalFlip() - new_dp_part_ids, new_vu = horizontal_flip.flip_parts_and_coords( - dp_part_ids, vu) - new_dp_surface_coords = tf.concat([y, xflipped, new_vu], axis=2) - return new_dp_part_ids, new_dp_surface_coords - diff --git a/research/object_detection/core/densepose_ops_test.py b/research/object_detection/core/densepose_ops_test.py deleted file mode 100644 index 5b814406d04..00000000000 --- a/research/object_detection/core/densepose_ops_test.py +++ /dev/null @@ -1,178 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.densepose_ops.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import densepose_ops -from object_detection.utils import test_case - - -class DensePoseOpsTest(test_case.TestCase): - """Tests for common DensePose operations.""" - - def test_scale(self): - def graph_fn(): - dp_surface_coords = tf.constant([ - [[0.0, 0.0, 0.1, 0.2], [100.0, 200.0, 0.3, 0.4]], - [[50.0, 120.0, 0.5, 0.6], [100.0, 140.0, 0.7, 0.8]] - ]) - y_scale = tf.constant(1.0 / 100) - x_scale = tf.constant(1.0 / 200) - - output = densepose_ops.scale(dp_surface_coords, y_scale, x_scale) - return output - output = self.execute(graph_fn, []) - - expected_dp_surface_coords = np.array([ - [[0., 0., 0.1, 0.2], [1.0, 1.0, 0.3, 0.4]], - [[0.5, 0.6, 0.5, 0.6], [1.0, 0.7, 0.7, 0.8]] - ]) - self.assertAllClose(output, expected_dp_surface_coords) - - def test_clip_to_window(self): - def graph_fn(): - dp_surface_coords = tf.constant([ - [[0.25, 0.5, 0.1, 0.2], [0.75, 0.75, 0.3, 0.4]], - [[0.5, 0.0, 0.5, 0.6], [1.0, 1.0, 0.7, 0.8]] - ]) - window = tf.constant([0.25, 0.25, 0.75, 0.75]) - - output = densepose_ops.clip_to_window(dp_surface_coords, window) - return output - output = self.execute(graph_fn, []) - - expected_dp_surface_coords = np.array([ - [[0.25, 0.5, 0.1, 0.2], [0.75, 0.75, 0.3, 0.4]], - [[0.5, 0.25, 0.5, 0.6], [0.75, 0.75, 0.7, 0.8]] - ]) - self.assertAllClose(output, expected_dp_surface_coords) - - def test_prune_outside_window(self): - def graph_fn(): - dp_num_points = tf.constant([2, 0, 1]) - dp_part_ids = tf.constant([[1, 1], [0, 0], [16, 0]]) - dp_surface_coords = tf.constant([ - [[0.9, 0.5, 0.1, 0.2], [0.75, 0.75, 0.3, 0.4]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], - [[0.8, 0.5, 0.6, 0.6], [0.5, 0.5, 0.7, 0.7]] - ]) - window = tf.constant([0.25, 0.25, 0.75, 0.75]) - - new_dp_num_points, new_dp_part_ids, new_dp_surface_coords = ( - densepose_ops.prune_outside_window(dp_num_points, dp_part_ids, - dp_surface_coords, window)) - return new_dp_num_points, new_dp_part_ids, new_dp_surface_coords - new_dp_num_points, new_dp_part_ids, new_dp_surface_coords = ( - self.execute_cpu(graph_fn, [])) - - expected_dp_num_points = np.array([1, 0, 0]) - expected_dp_part_ids = np.array([[1], [0], [0]]) - expected_dp_surface_coords = np.array([ - [[0.75, 0.75, 0.3, 0.4]], - [[0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0, 0.0]] - ]) - self.assertAllEqual(new_dp_num_points, expected_dp_num_points) - self.assertAllEqual(new_dp_part_ids, expected_dp_part_ids) - self.assertAllClose(new_dp_surface_coords, expected_dp_surface_coords) - - def test_change_coordinate_frame(self): - def graph_fn(): - dp_surface_coords = tf.constant([ - [[0.25, 0.5, 0.1, 0.2], [0.75, 0.75, 0.3, 0.4]], - [[0.5, 0.0, 0.5, 0.6], [1.0, 1.0, 0.7, 0.8]] - ]) - window = tf.constant([0.25, 0.25, 0.75, 0.75]) - - output = densepose_ops.change_coordinate_frame(dp_surface_coords, window) - return output - output = self.execute(graph_fn, []) - - expected_dp_surface_coords = np.array([ - [[0, 0.5, 0.1, 0.2], [1.0, 1.0, 0.3, 0.4]], - [[0.5, -0.5, 0.5, 0.6], [1.5, 1.5, 0.7, 0.8]] - ]) - self.assertAllClose(output, expected_dp_surface_coords) - - def test_to_normalized_coordinates(self): - def graph_fn(): - dp_surface_coords = tf.constant([ - [[10., 30., 0.1, 0.2], [30., 45., 0.3, 0.4]], - [[20., 0., 0.5, 0.6], [40., 60., 0.7, 0.8]] - ]) - output = densepose_ops.to_normalized_coordinates( - dp_surface_coords, 40, 60) - return output - output = self.execute(graph_fn, []) - - expected_dp_surface_coords = np.array([ - [[0.25, 0.5, 0.1, 0.2], [0.75, 0.75, 0.3, 0.4]], - [[0.5, 0.0, 0.5, 0.6], [1.0, 1.0, 0.7, 0.8]] - ]) - self.assertAllClose(output, expected_dp_surface_coords) - - def test_to_absolute_coordinates(self): - def graph_fn(): - dp_surface_coords = tf.constant([ - [[0.25, 0.5, 0.1, 0.2], [0.75, 0.75, 0.3, 0.4]], - [[0.5, 0.0, 0.5, 0.6], [1.0, 1.0, 0.7, 0.8]] - ]) - output = densepose_ops.to_absolute_coordinates( - dp_surface_coords, 40, 60) - return output - output = self.execute(graph_fn, []) - - expected_dp_surface_coords = np.array([ - [[10., 30., 0.1, 0.2], [30., 45., 0.3, 0.4]], - [[20., 0., 0.5, 0.6], [40., 60., 0.7, 0.8]] - ]) - self.assertAllClose(output, expected_dp_surface_coords) - - def test_horizontal_flip(self): - part_ids_np = np.array([[1, 4], [0, 8]], dtype=np.int32) - surf_coords_np = np.array([ - [[0.1, 0.7, 0.2, 0.4], [0.3, 0.8, 0.2, 0.4]], - [[0.0, 0.5, 0.8, 0.7], [0.6, 1.0, 0.7, 0.9]], - ], dtype=np.float32) - def graph_fn(): - part_ids = tf.constant(part_ids_np, dtype=tf.int32) - surf_coords = tf.constant(surf_coords_np, dtype=tf.float32) - flipped_part_ids, flipped_surf_coords = densepose_ops.flip_horizontal( - part_ids, surf_coords) - flipped_twice_part_ids, flipped_twice_surf_coords = ( - densepose_ops.flip_horizontal(flipped_part_ids, flipped_surf_coords)) - return (flipped_part_ids, flipped_surf_coords, - flipped_twice_part_ids, flipped_twice_surf_coords) - (flipped_part_ids, flipped_surf_coords, flipped_twice_part_ids, - flipped_twice_surf_coords) = self.execute(graph_fn, []) - - expected_flipped_part_ids = [[1, 5], # 1->1, 4->5 - [0, 9]] # 0->0, 8->9 - expected_flipped_surf_coords_yx = np.array([ - [[0.1, 1.0-0.7], [0.3, 1.0-0.8]], - [[0.0, 1.0-0.5], [0.6, 1.0-1.0]], - ], dtype=np.float32) - self.assertAllEqual(expected_flipped_part_ids, flipped_part_ids) - self.assertAllClose(expected_flipped_surf_coords_yx, - flipped_surf_coords[:, :, 0:2]) - self.assertAllEqual(part_ids_np, flipped_twice_part_ids) - self.assertAllClose(surf_coords_np, flipped_twice_surf_coords, rtol=1e-2, - atol=1e-2) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/freezable_batch_norm.py b/research/object_detection/core/freezable_batch_norm.py deleted file mode 100644 index 295aa7b3a50..00000000000 --- a/research/object_detection/core/freezable_batch_norm.py +++ /dev/null @@ -1,68 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A freezable batch norm layer that uses Keras batch normalization.""" -import tensorflow.compat.v1 as tf - - -class FreezableBatchNorm(tf.keras.layers.BatchNormalization): - """Batch normalization layer (Ioffe and Szegedy, 2014). - - This is a `freezable` batch norm layer that supports setting the `training` - parameter in the __init__ method rather than having to set it either via - the Keras learning phase or via the `call` method parameter. This layer will - forward all other parameters to the default Keras `BatchNormalization` - layer - - This is class is necessary because Object Detection model training sometimes - requires batch normalization layers to be `frozen` and used as if it was - evaluation time, despite still training (and potentially using dropout layers) - - Like the default Keras BatchNormalization layer, this will normalize the - activations of the previous layer at each batch, - i.e. applies a transformation that maintains the mean activation - close to 0 and the activation standard deviation close to 1. - - Args: - training: If False, the layer will normalize using the moving average and - std. dev, without updating the learned avg and std. dev. - If None or True, the layer will follow the keras BatchNormalization layer - strategy of checking the Keras learning phase at `call` time to decide - what to do. - **kwargs: The keyword arguments to forward to the keras BatchNormalization - layer constructor. - - Input shape: - Arbitrary. Use the keyword argument `input_shape` - (tuple of integers, does not include the samples axis) - when using this layer as the first layer in a model. - - Output shape: - Same shape as input. - - References: - - [Batch Normalization: Accelerating Deep Network Training by Reducing - Internal Covariate Shift](https://arxiv.org/abs/1502.03167) - """ - - def __init__(self, training=None, **kwargs): - super(FreezableBatchNorm, self).__init__(**kwargs) - self._training = training - - def call(self, inputs, training=None): - # Override the call arg only if the batchnorm is frozen. (Ignore None) - if self._training is False: # pylint: disable=g-bool-id-comparison - training = self._training - return super(FreezableBatchNorm, self).call(inputs, training=training) diff --git a/research/object_detection/core/freezable_batch_norm_tf2_test.py b/research/object_detection/core/freezable_batch_norm_tf2_test.py deleted file mode 100644 index 48b131d6279..00000000000 --- a/research/object_detection/core/freezable_batch_norm_tf2_test.py +++ /dev/null @@ -1,218 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.freezable_batch_norm.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest - -from absl.testing import parameterized -import numpy as np -from six.moves import zip -import tensorflow as tf - - -from object_detection.core import freezable_batch_norm -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top -if tf_version.is_tf2(): - from object_detection.core import freezable_sync_batch_norm -# pylint: enable=g-import-not-at-top - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class FreezableBatchNormTest(tf.test.TestCase, parameterized.TestCase): - """Tests for FreezableBatchNorm operations.""" - - def _build_model(self, use_sync_batch_norm, training=None): - model = tf.keras.models.Sequential() - norm = None - if use_sync_batch_norm: - norm = freezable_sync_batch_norm.FreezableSyncBatchNorm(training=training, - input_shape=(10,), - momentum=0.8) - else: - norm = freezable_batch_norm.FreezableBatchNorm(training=training, - input_shape=(10,), - momentum=0.8) - - model.add(norm) - return model, norm - - def _copy_weights(self, source_weights, target_weights): - for source, target in zip(source_weights, target_weights): - target.assign(source) - - def _train_freezable_batch_norm(self, training_mean, training_var, - use_sync_batch_norm): - model, _ = self._build_model(use_sync_batch_norm=use_sync_batch_norm) - model.compile(loss='mse', optimizer='sgd') - - # centered on training_mean, variance training_var - train_data = np.random.normal( - loc=training_mean, - scale=training_var, - size=(1000, 10)) - model.fit(train_data, train_data, epochs=4, verbose=0) - return model.weights - - def _test_batchnorm_layer( - self, norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, training_mean, training_var): - out_tensor = norm(tf.convert_to_tensor(test_data, dtype=tf.float32), - training=training_arg) - out = out_tensor - out -= norm.beta - out /= norm.gamma - - if not should_be_training: - out *= training_var - out += (training_mean - testing_mean) - out /= testing_var - - np.testing.assert_allclose(out.numpy().mean(), 0.0, atol=1.5e-1) - np.testing.assert_allclose(out.numpy().std(), 1.0, atol=1.5e-1) - - @parameterized.parameters(True, False) - def test_batchnorm_freezing_training_none(self, use_sync_batch_norm): - training_mean = 5.0 - training_var = 10.0 - - testing_mean = -10.0 - testing_var = 5.0 - - # Initially train the batch norm, and save the weights - trained_weights = self._train_freezable_batch_norm(training_mean, - training_var, - use_sync_batch_norm) - - # Load the batch norm weights, freezing training to True. - # Apply the batch norm layer to testing data and ensure it is normalized - # according to the batch statistics. - model, norm = self._build_model(use_sync_batch_norm, training=True) - self._copy_weights(trained_weights, model.weights) - - # centered on testing_mean, variance testing_var - test_data = np.random.normal( - loc=testing_mean, - scale=testing_var, - size=(1000, 10)) - - # Test with training=True passed to the call method: - training_arg = True - should_be_training = True - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - # Reset the weights, because they may have been updating by - # running with training=True - self._copy_weights(trained_weights, model.weights) - - # Test with training=False passed to the call method: - training_arg = False - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - # Test the layer in various Keras learning phase scopes: - training_arg = None - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - tf.keras.backend.set_learning_phase(True) - should_be_training = True - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - # Reset the weights, because they may have been updating by - # running with training=True - self._copy_weights(trained_weights, model.weights) - - tf.keras.backend.set_learning_phase(False) - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - @parameterized.parameters(True, False) - def test_batchnorm_freezing_training_false(self, use_sync_batch_norm): - training_mean = 5.0 - training_var = 10.0 - - testing_mean = -10.0 - testing_var = 5.0 - - # Initially train the batch norm, and save the weights - trained_weights = self._train_freezable_batch_norm(training_mean, - training_var, - use_sync_batch_norm) - - # Load the batch norm back up, freezing training to False. - # Apply the batch norm layer to testing data and ensure it is normalized - # according to the training data's statistics. - model, norm = self._build_model(use_sync_batch_norm, training=False) - self._copy_weights(trained_weights, model.weights) - - # centered on testing_mean, variance testing_var - test_data = np.random.normal( - loc=testing_mean, - scale=testing_var, - size=(1000, 10)) - - # Make sure that the layer is never training - # Test with training=True passed to the call method: - training_arg = True - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - # Test with training=False passed to the call method: - training_arg = False - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - # Test the layer in various Keras learning phase scopes: - training_arg = None - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - tf.keras.backend.set_learning_phase(True) - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - tf.keras.backend.set_learning_phase(False) - should_be_training = False - self._test_batchnorm_layer(norm, should_be_training, test_data, - testing_mean, testing_var, training_arg, - training_mean, training_var) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/freezable_sync_batch_norm.py b/research/object_detection/core/freezable_sync_batch_norm.py deleted file mode 100644 index f95a1064983..00000000000 --- a/research/object_detection/core/freezable_sync_batch_norm.py +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A freezable batch norm layer that uses Keras sync batch normalization.""" -import tensorflow as tf - - -class FreezableSyncBatchNorm(tf.keras.layers.experimental.SyncBatchNormalization - ): - """Sync Batch normalization layer (Ioffe and Szegedy, 2014). - - This is a `freezable` batch norm layer that supports setting the `training` - parameter in the __init__ method rather than having to set it either via - the Keras learning phase or via the `call` method parameter. This layer will - forward all other parameters to the Keras `SyncBatchNormalization` layer - - This is class is necessary because Object Detection model training sometimes - requires batch normalization layers to be `frozen` and used as if it was - evaluation time, despite still training (and potentially using dropout layers) - - Like the default Keras SyncBatchNormalization layer, this will normalize the - activations of the previous layer at each batch, - i.e. applies a transformation that maintains the mean activation - close to 0 and the activation standard deviation close to 1. - - Input shape: - Arbitrary. Use the keyword argument `input_shape` - (tuple of integers, does not include the samples axis) - when using this layer as the first layer in a model. - - Output shape: - Same shape as input. - - References: - - [Batch Normalization: Accelerating Deep Network Training by Reducing - Internal Covariate Shift](https://arxiv.org/abs/1502.03167) - """ - - def __init__(self, training=None, **kwargs): - """Constructor. - - Args: - training: If False, the layer will normalize using the moving average and - std. dev, without updating the learned avg and std. dev. - If None or True, the layer will follow the keras SyncBatchNormalization - layer strategy of checking the Keras learning phase at `call` time to - decide what to do. - **kwargs: The keyword arguments to forward to the keras - SyncBatchNormalization layer constructor. - """ - super(FreezableSyncBatchNorm, self).__init__(**kwargs) - self._training = training - - def call(self, inputs, training=None): - # Override the call arg only if the batchnorm is frozen. (Ignore None) - if self._training is False: # pylint: disable=g-bool-id-comparison - training = self._training - return super(FreezableSyncBatchNorm, self).call(inputs, training=training) diff --git a/research/object_detection/core/keypoint_ops.py b/research/object_detection/core/keypoint_ops.py deleted file mode 100644 index b4eb66d7741..00000000000 --- a/research/object_detection/core/keypoint_ops.py +++ /dev/null @@ -1,390 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Keypoint operations. - -Keypoints are represented as tensors of shape [num_instances, num_keypoints, 2], -where the last dimension holds rank 2 tensors of the form [y, x] representing -the coordinates of the keypoint. -""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.utils import shape_utils - - -def scale(keypoints, y_scale, x_scale, scope=None): - """Scales keypoint coordinates in x and y dimensions. - - Args: - keypoints: a tensor of shape [num_instances, num_keypoints, 2] - y_scale: (float) scalar tensor - x_scale: (float) scalar tensor - scope: name scope. - - Returns: - new_keypoints: a tensor of shape [num_instances, num_keypoints, 2] - """ - with tf.name_scope(scope, 'Scale'): - y_scale = tf.cast(y_scale, tf.float32) - x_scale = tf.cast(x_scale, tf.float32) - new_keypoints = keypoints * [[[y_scale, x_scale]]] - return new_keypoints - - -def clip_to_window(keypoints, window, scope=None): - """Clips keypoints to a window. - - This op clips any input keypoints to a window. - - Args: - keypoints: a tensor of shape [num_instances, num_keypoints, 2] - window: a tensor of shape [4] representing the [y_min, x_min, y_max, x_max] - window to which the op should clip the keypoints. - scope: name scope. - - Returns: - new_keypoints: a tensor of shape [num_instances, num_keypoints, 2] - """ - keypoints.get_shape().assert_has_rank(3) - with tf.name_scope(scope, 'ClipToWindow'): - y, x = tf.split(value=keypoints, num_or_size_splits=2, axis=2) - win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window) - y = tf.maximum(tf.minimum(y, win_y_max), win_y_min) - x = tf.maximum(tf.minimum(x, win_x_max), win_x_min) - new_keypoints = tf.concat([y, x], 2) - return new_keypoints - - -def prune_outside_window(keypoints, window, scope=None): - """Prunes keypoints that fall outside a given window. - - This function replaces keypoints that fall outside the given window with nan. - See also clip_to_window which clips any keypoints that fall outside the given - window. - - Args: - keypoints: a tensor of shape [num_instances, num_keypoints, 2] - window: a tensor of shape [4] representing the [y_min, x_min, y_max, x_max] - window outside of which the op should prune the keypoints. - scope: name scope. - - Returns: - new_keypoints: a tensor of shape [num_instances, num_keypoints, 2] - """ - keypoints.get_shape().assert_has_rank(3) - with tf.name_scope(scope, 'PruneOutsideWindow'): - y, x = tf.split(value=keypoints, num_or_size_splits=2, axis=2) - win_y_min, win_x_min, win_y_max, win_x_max = tf.unstack(window) - - valid_indices = tf.logical_and( - tf.logical_and(y >= win_y_min, y <= win_y_max), - tf.logical_and(x >= win_x_min, x <= win_x_max)) - - new_y = tf.where(valid_indices, y, np.nan * tf.ones_like(y)) - new_x = tf.where(valid_indices, x, np.nan * tf.ones_like(x)) - new_keypoints = tf.concat([new_y, new_x], 2) - - return new_keypoints - - -def change_coordinate_frame(keypoints, window, scope=None): - """Changes coordinate frame of the keypoints to be relative to window's frame. - - Given a window of the form [y_min, x_min, y_max, x_max], changes keypoint - coordinates from keypoints of shape [num_instances, num_keypoints, 2] - to be relative to this window. - - An example use case is data augmentation: where we are given groundtruth - keypoints and would like to randomly crop the image to some window. In this - case we need to change the coordinate frame of each groundtruth keypoint to be - relative to this new window. - - Args: - keypoints: a tensor of shape [num_instances, num_keypoints, 2] - window: a tensor of shape [4] representing the [y_min, x_min, y_max, x_max] - window we should change the coordinate frame to. - scope: name scope. - - Returns: - new_keypoints: a tensor of shape [num_instances, num_keypoints, 2] - """ - with tf.name_scope(scope, 'ChangeCoordinateFrame'): - win_height = window[2] - window[0] - win_width = window[3] - window[1] - new_keypoints = scale(keypoints - [window[0], window[1]], 1.0 / win_height, - 1.0 / win_width) - return new_keypoints - - -def keypoints_to_enclosing_bounding_boxes(keypoints, keypoints_axis=1): - """Creates enclosing bounding boxes from keypoints. - - Args: - keypoints: a [num_instances, num_keypoints, 2] float32 tensor with keypoints - in [y, x] format. - keypoints_axis: An integer indicating the axis that correspond to the - keypoint dimension. - - Returns: - A [num_instances, 4] float32 tensor that tightly covers all the keypoints - for each instance. - """ - ymin = tf.math.reduce_min(keypoints[..., 0], axis=keypoints_axis) - xmin = tf.math.reduce_min(keypoints[..., 1], axis=keypoints_axis) - ymax = tf.math.reduce_max(keypoints[..., 0], axis=keypoints_axis) - xmax = tf.math.reduce_max(keypoints[..., 1], axis=keypoints_axis) - return tf.stack([ymin, xmin, ymax, xmax], axis=keypoints_axis) - - -def to_normalized_coordinates(keypoints, height, width, - check_range=True, scope=None): - """Converts absolute keypoint coordinates to normalized coordinates in [0, 1]. - - Usually one uses the dynamic shape of the image or conv-layer tensor: - keypoints = keypoint_ops.to_normalized_coordinates(keypoints, - tf.shape(images)[1], - tf.shape(images)[2]), - - This function raises an assertion failed error at graph execution time when - the maximum coordinate is smaller than 1.01 (which means that coordinates are - already normalized). The value 1.01 is to deal with small rounding errors. - - Args: - keypoints: A tensor of shape [num_instances, num_keypoints, 2]. - height: Maximum value for y coordinate of absolute keypoint coordinates. - width: Maximum value for x coordinate of absolute keypoint coordinates. - check_range: If True, checks if the coordinates are normalized. - scope: name scope. - - Returns: - tensor of shape [num_instances, num_keypoints, 2] with normalized - coordinates in [0, 1]. - """ - with tf.name_scope(scope, 'ToNormalizedCoordinates'): - height = tf.cast(height, tf.float32) - width = tf.cast(width, tf.float32) - - if check_range: - max_val = tf.reduce_max(keypoints) - max_assert = tf.Assert(tf.greater(max_val, 1.01), - ['max value is lower than 1.01: ', max_val]) - with tf.control_dependencies([max_assert]): - width = tf.identity(width) - - return scale(keypoints, 1.0 / height, 1.0 / width) - - -def to_absolute_coordinates(keypoints, height, width, - check_range=True, scope=None): - """Converts normalized keypoint coordinates to absolute pixel coordinates. - - This function raises an assertion failed error when the maximum keypoint - coordinate value is larger than 1.01 (in which case coordinates are already - absolute). - - Args: - keypoints: A tensor of shape [num_instances, num_keypoints, 2] - height: Maximum value for y coordinate of absolute keypoint coordinates. - width: Maximum value for x coordinate of absolute keypoint coordinates. - check_range: If True, checks if the coordinates are normalized or not. - scope: name scope. - - Returns: - tensor of shape [num_instances, num_keypoints, 2] with absolute coordinates - in terms of the image size. - - """ - with tf.name_scope(scope, 'ToAbsoluteCoordinates'): - height = tf.cast(height, tf.float32) - width = tf.cast(width, tf.float32) - - # Ensure range of input keypoints is correct. - if check_range: - max_val = tf.reduce_max(keypoints) - max_assert = tf.Assert(tf.greater_equal(1.01, max_val), - ['maximum keypoint coordinate value is larger ' - 'than 1.01: ', max_val]) - with tf.control_dependencies([max_assert]): - width = tf.identity(width) - - return scale(keypoints, height, width) - - -def flip_horizontal(keypoints, flip_point, flip_permutation=None, scope=None): - """Flips the keypoints horizontally around the flip_point. - - This operation flips the x coordinate for each keypoint around the flip_point - and also permutes the keypoints in a manner specified by flip_permutation. - - Args: - keypoints: a tensor of shape [num_instances, num_keypoints, 2] - flip_point: (float) scalar tensor representing the x coordinate to flip the - keypoints around. - flip_permutation: integer list or rank 1 int32 tensor containing the - keypoint flip permutation. This specifies the mapping from original - keypoint indices to the flipped keypoint indices. This is used primarily - for keypoints that are not reflection invariant. E.g. Suppose there are 3 - keypoints representing ['head', 'right_eye', 'left_eye'], then a logical - choice for flip_permutation might be [0, 2, 1] since we want to swap the - 'left_eye' and 'right_eye' after a horizontal flip. - Default to None or empty list to keep the original order after flip. - scope: name scope. - - Returns: - new_keypoints: a tensor of shape [num_instances, num_keypoints, 2] - """ - keypoints.get_shape().assert_has_rank(3) - with tf.name_scope(scope, 'FlipHorizontal'): - keypoints = tf.transpose(keypoints, [1, 0, 2]) - if flip_permutation: - keypoints = tf.gather(keypoints, flip_permutation) - v, u = tf.split(value=keypoints, num_or_size_splits=2, axis=2) - u = flip_point * 2.0 - u - new_keypoints = tf.concat([v, u], 2) - new_keypoints = tf.transpose(new_keypoints, [1, 0, 2]) - return new_keypoints - - -def flip_vertical(keypoints, flip_point, flip_permutation=None, scope=None): - """Flips the keypoints vertically around the flip_point. - - This operation flips the y coordinate for each keypoint around the flip_point - and also permutes the keypoints in a manner specified by flip_permutation. - - Args: - keypoints: a tensor of shape [num_instances, num_keypoints, 2] - flip_point: (float) scalar tensor representing the y coordinate to flip the - keypoints around. - flip_permutation: integer list or rank 1 int32 tensor containing the - keypoint flip permutation. This specifies the mapping from original - keypoint indices to the flipped keypoint indices. This is used primarily - for keypoints that are not reflection invariant. E.g. Suppose there are 3 - keypoints representing ['head', 'right_eye', 'left_eye'], then a logical - choice for flip_permutation might be [0, 2, 1] since we want to swap the - 'left_eye' and 'right_eye' after a horizontal flip. - Default to None or empty list to keep the original order after flip. - scope: name scope. - - Returns: - new_keypoints: a tensor of shape [num_instances, num_keypoints, 2] - """ - keypoints.get_shape().assert_has_rank(3) - with tf.name_scope(scope, 'FlipVertical'): - keypoints = tf.transpose(keypoints, [1, 0, 2]) - if flip_permutation: - keypoints = tf.gather(keypoints, flip_permutation) - v, u = tf.split(value=keypoints, num_or_size_splits=2, axis=2) - v = flip_point * 2.0 - v - new_keypoints = tf.concat([v, u], 2) - new_keypoints = tf.transpose(new_keypoints, [1, 0, 2]) - return new_keypoints - - -def rot90(keypoints, rotation_permutation=None, scope=None): - """Rotates the keypoints counter-clockwise by 90 degrees. - - Args: - keypoints: a tensor of shape [num_instances, num_keypoints, 2] - rotation_permutation: integer list or rank 1 int32 tensor containing the - keypoint flip permutation. This specifies the mapping from original - keypoint indices to the rotated keypoint indices. This is used primarily - for keypoints that are not rotation invariant. - Default to None or empty list to keep the original order after rotation. - scope: name scope. - Returns: - new_keypoints: a tensor of shape [num_instances, num_keypoints, 2] - """ - keypoints.get_shape().assert_has_rank(3) - with tf.name_scope(scope, 'Rot90'): - keypoints = tf.transpose(keypoints, [1, 0, 2]) - if rotation_permutation: - keypoints = tf.gather(keypoints, rotation_permutation) - v, u = tf.split(value=keypoints[:, :, ::-1], num_or_size_splits=2, axis=2) - v = 1.0 - v - new_keypoints = tf.concat([v, u], 2) - new_keypoints = tf.transpose(new_keypoints, [1, 0, 2]) - return new_keypoints - - - - -def keypoint_weights_from_visibilities(keypoint_visibilities, - per_keypoint_weights=None): - """Returns a keypoint weights tensor. - - During training, it is often beneficial to consider only those keypoints that - are labeled. This function returns a weights tensor that combines default - per-keypoint weights, as well as the visibilities of individual keypoints. - - The returned tensor satisfies: - keypoint_weights[i, k] = per_keypoint_weights[k] * keypoint_visibilities[i, k] - where per_keypoint_weights[k] is set to 1 if not provided. - - Args: - keypoint_visibilities: A [num_instances, num_keypoints] boolean tensor - indicating whether a keypoint is labeled (and perhaps even visible). - per_keypoint_weights: A list or 1-d tensor of length `num_keypoints` with - per-keypoint weights. If None, will use 1 for each visible keypoint - weight. - - Returns: - A [num_instances, num_keypoints] float32 tensor with keypoint weights. Those - keypoints deemed visible will have the provided per-keypoint weight, and - all others will be set to zero. - """ - keypoint_visibilities.get_shape().assert_has_rank(2) - if per_keypoint_weights is None: - num_keypoints = shape_utils.combined_static_and_dynamic_shape( - keypoint_visibilities)[1] - per_keypoint_weight_mult = tf.ones((1, num_keypoints,), dtype=tf.float32) - else: - per_keypoint_weight_mult = tf.expand_dims(per_keypoint_weights, axis=0) - return per_keypoint_weight_mult * tf.cast(keypoint_visibilities, tf.float32) - - -def set_keypoint_visibilities(keypoints, initial_keypoint_visibilities=None): - """Sets keypoint visibilities based on valid/invalid keypoints. - - Some keypoint operations set invisible keypoints (e.g. cropped keypoints) to - NaN, without affecting any keypoint "visibility" variables. This function is - used to update (or create) keypoint visibilities to agree with visible / - invisible keypoint coordinates. - - Args: - keypoints: a float32 tensor of shape [num_instances, num_keypoints, 2]. - initial_keypoint_visibilities: a boolean tensor of shape - [num_instances, num_keypoints]. If provided, will maintain the visibility - designation of a keypoint, so long as the corresponding coordinates are - not NaN. If not provided, will create keypoint visibilities directly from - the values in `keypoints` (i.e. NaN coordinates map to False, otherwise - they map to True). - - Returns: - keypoint_visibilities: a bool tensor of shape [num_instances, num_keypoints] - indicating whether a keypoint is visible or not. - """ - keypoints.get_shape().assert_has_rank(3) - if initial_keypoint_visibilities is not None: - keypoint_visibilities = tf.cast(initial_keypoint_visibilities, tf.bool) - else: - keypoint_visibilities = tf.ones_like(keypoints[:, :, 0], dtype=tf.bool) - - keypoints_with_nan = tf.math.reduce_any(tf.math.is_nan(keypoints), axis=2) - keypoint_visibilities = tf.where( - keypoints_with_nan, - tf.zeros_like(keypoint_visibilities, dtype=tf.bool), - keypoint_visibilities) - return keypoint_visibilities diff --git a/research/object_detection/core/keypoint_ops_test.py b/research/object_detection/core/keypoint_ops_test.py deleted file mode 100644 index f5ba5b59b74..00000000000 --- a/research/object_detection/core/keypoint_ops_test.py +++ /dev/null @@ -1,395 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.keypoint_ops.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import keypoint_ops -from object_detection.utils import test_case - - -class KeypointOpsTest(test_case.TestCase): - """Tests for common keypoint operations.""" - - def test_scale(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.0, 0.0], [100.0, 200.0]], - [[50.0, 120.0], [100.0, 140.0]] - ]) - y_scale = tf.constant(1.0 / 100) - x_scale = tf.constant(1.0 / 200) - - expected_keypoints = tf.constant([ - [[0., 0.], [1.0, 1.0]], - [[0.5, 0.6], [1.0, 0.7]] - ]) - output = keypoint_ops.scale(keypoints, y_scale, x_scale) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_clip_to_window(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.25, 0.5], [0.75, 0.75]], - [[0.5, 0.0], [1.0, 1.0]] - ]) - window = tf.constant([0.25, 0.25, 0.75, 0.75]) - - expected_keypoints = tf.constant([ - [[0.25, 0.5], [0.75, 0.75]], - [[0.5, 0.25], [0.75, 0.75]] - ]) - output = keypoint_ops.clip_to_window(keypoints, window) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_prune_outside_window(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.25, 0.5], [0.75, 0.75]], - [[0.5, 0.0], [1.0, 1.0]] - ]) - window = tf.constant([0.25, 0.25, 0.75, 0.75]) - - expected_keypoints = tf.constant([[[0.25, 0.5], [0.75, 0.75]], - [[np.nan, np.nan], [np.nan, np.nan]]]) - output = keypoint_ops.prune_outside_window(keypoints, window) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_change_coordinate_frame(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.25, 0.5], [0.75, 0.75]], - [[0.5, 0.0], [1.0, 1.0]] - ]) - window = tf.constant([0.25, 0.25, 0.75, 0.75]) - - expected_keypoints = tf.constant([ - [[0, 0.5], [1.0, 1.0]], - [[0.5, -0.5], [1.5, 1.5]] - ]) - output = keypoint_ops.change_coordinate_frame(keypoints, window) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_keypoints_to_enclosing_bounding_boxes(self): - def graph_fn(): - keypoints = tf.constant( - [ - [ # Instance 0. - [5., 10.], - [3., 20.], - [8., 4.], - ], - [ # Instance 1. - [2., 12.], - [0., 3.], - [5., 19.], - ], - ], dtype=tf.float32) - bboxes = keypoint_ops.keypoints_to_enclosing_bounding_boxes(keypoints) - return bboxes - output = self.execute(graph_fn, []) - expected_bboxes = np.array( - [ - [3., 4., 8., 20.], - [0., 3., 5., 19.] - ]) - self.assertAllClose(expected_bboxes, output) - - def test_keypoints_to_enclosing_bounding_boxes_axis2(self): - def graph_fn(): - keypoints = tf.constant( - [ - [ # Instance 0. - [5., 10.], - [3., 20.], - [8., 4.], - ], - [ # Instance 1. - [2., 12.], - [0., 3.], - [5., 19.], - ], - ], dtype=tf.float32) - keypoints = tf.stack([keypoints, keypoints], axis=0) - bboxes = keypoint_ops.keypoints_to_enclosing_bounding_boxes( - keypoints, keypoints_axis=2) - return bboxes - output = self.execute(graph_fn, []) - - expected_bboxes = np.array( - [ - [3., 4., 8., 20.], - [0., 3., 5., 19.] - ]) - self.assertAllClose(expected_bboxes, output[0]) - self.assertAllClose(expected_bboxes, output[1]) - - def test_to_normalized_coordinates(self): - def graph_fn(): - keypoints = tf.constant([ - [[10., 30.], [30., 45.]], - [[20., 0.], [40., 60.]] - ]) - output = keypoint_ops.to_normalized_coordinates( - keypoints, 40, 60) - expected_keypoints = tf.constant([ - [[0.25, 0.5], [0.75, 0.75]], - [[0.5, 0.0], [1.0, 1.0]] - ]) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_to_normalized_coordinates_already_normalized(self): - if self.has_tpu(): return - def graph_fn(): - keypoints = tf.constant([ - [[0.25, 0.5], [0.75, 0.75]], - [[0.5, 0.0], [1.0, 1.0]] - ]) - output = keypoint_ops.to_normalized_coordinates( - keypoints, 40, 60) - return output - with self.assertRaisesOpError('assertion failed'): - self.execute_cpu(graph_fn, []) - - def test_to_absolute_coordinates(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.25, 0.5], [0.75, 0.75]], - [[0.5, 0.0], [1.0, 1.0]] - ]) - output = keypoint_ops.to_absolute_coordinates( - keypoints, 40, 60) - expected_keypoints = tf.constant([ - [[10., 30.], [30., 45.]], - [[20., 0.], [40., 60.]] - ]) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_to_absolute_coordinates_already_absolute(self): - if self.has_tpu(): return - def graph_fn(): - keypoints = tf.constant([ - [[10., 30.], [30., 45.]], - [[20., 0.], [40., 60.]] - ]) - output = keypoint_ops.to_absolute_coordinates( - keypoints, 40, 60) - return output - with self.assertRaisesOpError('assertion failed'): - self.execute_cpu(graph_fn, []) - - def test_flip_horizontal(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]] - ]) - expected_keypoints = tf.constant([ - [[0.1, 0.9], [0.2, 0.8], [0.3, 0.7]], - [[0.4, 0.6], [0.5, 0.5], [0.6, 0.4]], - ]) - output = keypoint_ops.flip_horizontal(keypoints, 0.5) - return output, expected_keypoints - - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_flip_horizontal_permutation(self): - - def graph_fn(): - keypoints = tf.constant([[[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]]]) - flip_permutation = [0, 2, 1] - - expected_keypoints = tf.constant([ - [[0.1, 0.9], [0.3, 0.7], [0.2, 0.8]], - [[0.4, 0.6], [0.6, 0.4], [0.5, 0.5]], - ]) - output = keypoint_ops.flip_horizontal(keypoints, 0.5, flip_permutation) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_flip_vertical(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]] - ]) - - expected_keypoints = tf.constant([ - [[0.9, 0.1], [0.8, 0.2], [0.7, 0.3]], - [[0.6, 0.4], [0.5, 0.5], [0.4, 0.6]], - ]) - output = keypoint_ops.flip_vertical(keypoints, 0.5) - return output, expected_keypoints - - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_flip_vertical_permutation(self): - - def graph_fn(): - keypoints = tf.constant([[[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]]]) - flip_permutation = [0, 2, 1] - - expected_keypoints = tf.constant([ - [[0.9, 0.1], [0.7, 0.3], [0.8, 0.2]], - [[0.6, 0.4], [0.4, 0.6], [0.5, 0.5]], - ]) - output = keypoint_ops.flip_vertical(keypoints, 0.5, flip_permutation) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_rot90(self): - def graph_fn(): - keypoints = tf.constant([ - [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.4, 0.6], [0.5, 0.6], [0.6, 0.7]] - ]) - expected_keypoints = tf.constant([ - [[0.9, 0.1], [0.8, 0.2], [0.7, 0.3]], - [[0.4, 0.4], [0.4, 0.5], [0.3, 0.6]], - ]) - output = keypoint_ops.rot90(keypoints) - return output, expected_keypoints - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - def test_rot90_permutation(self): - - def graph_fn(): - keypoints = tf.constant([[[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.4, 0.6], [0.5, 0.6], [0.6, 0.7]]]) - rot_permutation = [0, 2, 1] - expected_keypoints = tf.constant([ - [[0.9, 0.1], [0.7, 0.3], [0.8, 0.2]], - [[0.4, 0.4], [0.3, 0.6], [0.4, 0.5]], - ]) - output = keypoint_ops.rot90(keypoints, - rotation_permutation=rot_permutation) - return output, expected_keypoints - - output, expected_keypoints = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoints) - - - def test_keypoint_weights_from_visibilities(self): - def graph_fn(): - keypoint_visibilities = tf.constant([ - [True, True, False], - [False, True, False] - ]) - per_keypoint_weights = [1.0, 2.0, 3.0] - keypoint_weights = keypoint_ops.keypoint_weights_from_visibilities( - keypoint_visibilities, per_keypoint_weights) - return keypoint_weights - expected_keypoint_weights = [ - [1.0, 2.0, 0.0], - [0.0, 2.0, 0.0] - ] - output = self.execute(graph_fn, []) - self.assertAllClose(output, expected_keypoint_weights) - - def test_keypoint_weights_from_visibilities_no_per_kpt_weights(self): - def graph_fn(): - keypoint_visibilities = tf.constant([ - [True, True, False], - [False, True, False] - ]) - keypoint_weights = keypoint_ops.keypoint_weights_from_visibilities( - keypoint_visibilities) - return keypoint_weights - expected_keypoint_weights = [ - [1.0, 1.0, 0.0], - [0.0, 1.0, 0.0] - ] - output = self.execute(graph_fn, []) - self.assertAllClose(expected_keypoint_weights, output) - - def test_set_keypoint_visibilities_no_initial_kpt_vis(self): - keypoints_np = np.array( - [ - [[np.nan, 0.2], - [np.nan, np.nan], - [-3., 7.]], - [[0.5, 0.2], - [4., 1.0], - [-3., np.nan]], - ], dtype=np.float32) - def graph_fn(): - keypoints = tf.constant(keypoints_np, dtype=tf.float32) - keypoint_visibilities = keypoint_ops.set_keypoint_visibilities( - keypoints) - return keypoint_visibilities - - expected_kpt_vis = [ - [False, False, True], - [True, True, False] - ] - output = self.execute(graph_fn, []) - self.assertAllEqual(expected_kpt_vis, output) - - def test_set_keypoint_visibilities(self): - keypoints_np = np.array( - [ - [[np.nan, 0.2], - [np.nan, np.nan], - [-3., 7.]], - [[0.5, 0.2], - [4., 1.0], - [-3., np.nan]], - ], dtype=np.float32) - initial_keypoint_visibilities_np = np.array( - [ - [False, - True, # Will be overriden by NaN coords. - False], # Will be maintained, even though non-NaN coords. - [True, - False, # Will be maintained, even though non-NaN coords. - False] - ]) - def graph_fn(): - keypoints = tf.constant(keypoints_np, dtype=tf.float32) - initial_keypoint_visibilities = tf.constant( - initial_keypoint_visibilities_np, dtype=tf.bool) - keypoint_visibilities = keypoint_ops.set_keypoint_visibilities( - keypoints, initial_keypoint_visibilities) - return keypoint_visibilities - - expected_kpt_vis = [ - [False, False, False], - [True, False, False] - ] - output = self.execute(graph_fn, []) - self.assertAllEqual(expected_kpt_vis, output) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/losses.py b/research/object_detection/core/losses.py deleted file mode 100644 index 3ddeb4f825f..00000000000 --- a/research/object_detection/core/losses.py +++ /dev/null @@ -1,889 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Classification and regression loss functions for object detection. - -Localization losses: - * WeightedL2LocalizationLoss - * WeightedSmoothL1LocalizationLoss - * WeightedIOULocalizationLoss - -Classification losses: - * WeightedSigmoidClassificationLoss - * WeightedSoftmaxClassificationLoss - * WeightedSoftmaxClassificationAgainstLogitsLoss - * BootstrappedSigmoidClassificationLoss - * WeightedDiceClassificationLoss -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc -import six -import tensorflow.compat.v1 as tf -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -class Loss(six.with_metaclass(abc.ABCMeta, object)): - """Abstract base class for loss functions.""" - - def __call__(self, - prediction_tensor, - target_tensor, - ignore_nan_targets=False, - losses_mask=None, - scope=None, - **params): - """Call the loss function. - - Args: - prediction_tensor: an N-d tensor of shape [batch, anchors, ...] - representing predicted quantities. - target_tensor: an N-d tensor of shape [batch, anchors, ...] representing - regression or classification targets. - ignore_nan_targets: whether to ignore nan targets in the loss computation. - E.g. can be used if the target tensor is missing groundtruth data that - shouldn't be factored into the loss. - losses_mask: A [batch] boolean tensor that indicates whether losses should - be applied to individual images in the batch. For elements that - are False, corresponding prediction, target, and weight tensors will not - contribute to loss computation. If None, no filtering will take place - prior to loss computation. - scope: Op scope name. Defaults to 'Loss' if None. - **params: Additional keyword arguments for specific implementations of - the Loss. - - Returns: - loss: a tensor representing the value of the loss function. - """ - with tf.name_scope(scope, 'Loss', - [prediction_tensor, target_tensor, params]) as scope: - if ignore_nan_targets: - target_tensor = tf.where(tf.is_nan(target_tensor), - prediction_tensor, - target_tensor) - if losses_mask is not None: - tensor_multiplier = self._get_loss_multiplier_for_tensor( - prediction_tensor, - losses_mask) - prediction_tensor *= tensor_multiplier - target_tensor *= tensor_multiplier - - if 'weights' in params: - params['weights'] = tf.convert_to_tensor(params['weights']) - weights_multiplier = self._get_loss_multiplier_for_tensor( - params['weights'], - losses_mask) - params['weights'] *= weights_multiplier - return self._compute_loss(prediction_tensor, target_tensor, **params) - - def _get_loss_multiplier_for_tensor(self, tensor, losses_mask): - loss_multiplier_shape = tf.stack([-1] + [1] * (len(tensor.shape) - 1)) - return tf.cast(tf.reshape(losses_mask, loss_multiplier_shape), tf.float32) - - @abc.abstractmethod - def _compute_loss(self, prediction_tensor, target_tensor, **params): - """Method to be overridden by implementations. - - Args: - prediction_tensor: a tensor representing predicted quantities - target_tensor: a tensor representing regression or classification targets - **params: Additional keyword arguments for specific implementations of - the Loss. - - Returns: - loss: an N-d tensor of shape [batch, anchors, ...] containing the loss per - anchor - """ - pass - - -class WeightedL2LocalizationLoss(Loss): - """L2 localization loss function with anchorwise output support. - - Loss[b,a] = .5 * ||weights[b,a] * (prediction[b,a,:] - target[b,a,:])||^2 - """ - - def _compute_loss(self, prediction_tensor, target_tensor, weights): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, - code_size] representing the (encoded) predicted locations of objects. - target_tensor: A float tensor of shape [batch_size, num_anchors, - code_size] representing the regression targets - weights: a float tensor of shape [batch_size, num_anchors] - - Returns: - loss: a float tensor of shape [batch_size, num_anchors] tensor - representing the value of the loss function. - """ - weighted_diff = (prediction_tensor - target_tensor) * tf.expand_dims( - weights, 2) - square_diff = 0.5 * tf.square(weighted_diff) - return tf.reduce_sum(square_diff, 2) - - -class WeightedSmoothL1LocalizationLoss(Loss): - """Smooth L1 localization loss function aka Huber Loss.. - - The smooth L1_loss is defined elementwise as .5 x^2 if |x| <= delta and - delta * (|x|- 0.5*delta) otherwise, where x is the difference between - predictions and target. - - See also Equation (3) in the Fast R-CNN paper by Ross Girshick (ICCV 2015) - """ - - def __init__(self, delta=1.0): - """Constructor. - - Args: - delta: delta for smooth L1 loss. - """ - super(WeightedSmoothL1LocalizationLoss, self).__init__() - self._delta = delta - - def _compute_loss(self, prediction_tensor, target_tensor, weights): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, - code_size] representing the (encoded) predicted locations of objects. - target_tensor: A float tensor of shape [batch_size, num_anchors, - code_size] representing the regression targets - weights: a float tensor of shape [batch_size, num_anchors] - - Returns: - loss: a float tensor of shape [batch_size, num_anchors] tensor - representing the value of the loss function. - """ - return tf.reduce_sum(tf.losses.huber_loss( - target_tensor, - prediction_tensor, - delta=self._delta, - weights=tf.expand_dims(weights, axis=2), - loss_collection=None, - reduction=tf.losses.Reduction.NONE - ), axis=2) - - -class WeightedIOULocalizationLoss(Loss): - """IOU localization loss function. - - Sums the IOU for corresponding pairs of predicted/groundtruth boxes - and for each pair assign a loss of 1 - IOU. We then compute a weighted - sum over all pairs which is returned as the total loss. - """ - - def _compute_loss(self, prediction_tensor, target_tensor, weights): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, 4] - representing the decoded predicted boxes - target_tensor: A float tensor of shape [batch_size, num_anchors, 4] - representing the decoded target boxes - weights: a float tensor of shape [batch_size, num_anchors] - - Returns: - loss: a float tensor of shape [batch_size, num_anchors] tensor - representing the value of the loss function. - """ - predicted_boxes = box_list.BoxList(tf.reshape(prediction_tensor, [-1, 4])) - target_boxes = box_list.BoxList(tf.reshape(target_tensor, [-1, 4])) - per_anchor_iou_loss = 1.0 - box_list_ops.matched_iou(predicted_boxes, - target_boxes) - return tf.reshape(weights, [-1]) * per_anchor_iou_loss - - -class WeightedGIOULocalizationLoss(Loss): - """GIOU localization loss function. - - Sums the GIOU loss for corresponding pairs of predicted/groundtruth boxes - and for each pair assign a loss of 1 - GIOU. We then compute a weighted - sum over all pairs which is returned as the total loss. - """ - - def _compute_loss(self, prediction_tensor, target_tensor, weights): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, 4] - representing the decoded predicted boxes - target_tensor: A float tensor of shape [batch_size, num_anchors, 4] - representing the decoded target boxes - weights: a float tensor of shape [batch_size, num_anchors] - - Returns: - loss: a float tensor of shape [batch_size, num_anchors] tensor - representing the value of the loss function. - """ - batch_size, num_anchors, _ = shape_utils.combined_static_and_dynamic_shape( - prediction_tensor) - predicted_boxes = tf.reshape(prediction_tensor, [-1, 4]) - target_boxes = tf.reshape(target_tensor, [-1, 4]) - - per_anchor_iou_loss = 1 - ops.giou(predicted_boxes, target_boxes) - return tf.reshape(tf.reshape(weights, [-1]) * per_anchor_iou_loss, - [batch_size, num_anchors]) - - -class WeightedSigmoidClassificationLoss(Loss): - """Sigmoid cross entropy classification loss function.""" - - def _compute_loss(self, - prediction_tensor, - target_tensor, - weights, - class_indices=None): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing the predicted logits for each class - target_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing one-hot encoded classification targets - weights: a float tensor of shape, either [batch_size, num_anchors, - num_classes] or [batch_size, num_anchors, 1]. If the shape is - [batch_size, num_anchors, 1], all the classses are equally weighted. - class_indices: (Optional) A 1-D integer tensor of class indices. - If provided, computes loss only for the specified class indices. - - Returns: - loss: a float tensor of shape [batch_size, num_anchors, num_classes] - representing the value of the loss function. - """ - if class_indices is not None: - weights *= tf.reshape( - ops.indices_to_dense_vector(class_indices, - tf.shape(prediction_tensor)[2]), - [1, 1, -1]) - per_entry_cross_ent = (tf.nn.sigmoid_cross_entropy_with_logits( - labels=target_tensor, logits=prediction_tensor)) - return per_entry_cross_ent * weights - - -class WeightedDiceClassificationLoss(Loss): - """Dice loss for classification [1][2]. - - [1]: https://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient - [2]: https://arxiv.org/abs/1606.04797 - - """ - - def __init__(self, squared_normalization, is_prediction_probability=False): - """Initializes the loss object. - - Args: - squared_normalization: boolean, if set, we square the probabilities in the - denominator term used for normalization. - is_prediction_probability: boolean, whether or not the input - prediction_tensor represents a probability. If false, it is - first converted to a probability by applying sigmoid. - """ - - self._squared_normalization = squared_normalization - self.is_prediction_probability = is_prediction_probability - super(WeightedDiceClassificationLoss, self).__init__() - - def _compute_loss(self, - prediction_tensor, - target_tensor, - weights, - class_indices=None): - """Computes the loss value. - - Dice loss uses the area of the ground truth and prediction tensors for - normalization. We compute area by summing along the anchors (2nd) dimension. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_pixels, - num_classes] representing the predicted logits for each class. - num_pixels denotes the total number of pixels in the spatial dimensions - of the mask after flattening. - target_tensor: A float tensor of shape [batch_size, num_pixels, - num_classes] representing one-hot encoded classification targets. - num_pixels denotes the total number of pixels in the spatial dimensions - of the mask after flattening. - weights: a float tensor of shape, either [batch_size, num_anchors, - num_classes] or [batch_size, num_anchors, 1]. If the shape is - [batch_size, num_anchors, 1], all the classses are equally weighted. - class_indices: (Optional) A 1-D integer tensor of class indices. - If provided, computes loss only for the specified class indices. - - Returns: - loss: a float tensor of shape [batch_size, num_classes] - representing the value of the loss function. - """ - if class_indices is not None: - weights *= tf.reshape( - ops.indices_to_dense_vector(class_indices, - tf.shape(prediction_tensor)[2]), - [1, 1, -1]) - - if self.is_prediction_probability: - prob_tensor = prediction_tensor - else: - prob_tensor = tf.nn.sigmoid(prediction_tensor) - - if self._squared_normalization: - prob_tensor = tf.pow(prob_tensor, 2) - target_tensor = tf.pow(target_tensor, 2) - - prob_tensor *= weights - target_tensor *= weights - - prediction_area = tf.reduce_sum(prob_tensor, axis=1) - gt_area = tf.reduce_sum(target_tensor, axis=1) - - intersection = tf.reduce_sum(prob_tensor * target_tensor, axis=1) - dice_coeff = 2 * intersection / tf.maximum(gt_area + prediction_area, 1.0) - dice_loss = 1 - dice_coeff - - return dice_loss - - -class SigmoidFocalClassificationLoss(Loss): - """Sigmoid focal cross entropy loss. - - Focal loss down-weights well classified examples and focusses on the hard - examples. See https://arxiv.org/pdf/1708.02002.pdf for the loss definition. - """ - - def __init__(self, gamma=2.0, alpha=0.25): - """Constructor. - - Args: - gamma: exponent of the modulating factor (1 - p_t) ^ gamma. - alpha: optional alpha weighting factor to balance positives vs negatives. - """ - super(SigmoidFocalClassificationLoss, self).__init__() - self._alpha = alpha - self._gamma = gamma - - def _compute_loss(self, - prediction_tensor, - target_tensor, - weights, - class_indices=None): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing the predicted logits for each class - target_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing one-hot encoded classification targets - weights: a float tensor of shape, either [batch_size, num_anchors, - num_classes] or [batch_size, num_anchors, 1]. If the shape is - [batch_size, num_anchors, 1], all the classses are equally weighted. - class_indices: (Optional) A 1-D integer tensor of class indices. - If provided, computes loss only for the specified class indices. - - Returns: - loss: a float tensor of shape [batch_size, num_anchors, num_classes] - representing the value of the loss function. - """ - if class_indices is not None: - weights *= tf.reshape( - ops.indices_to_dense_vector(class_indices, - tf.shape(prediction_tensor)[2]), - [1, 1, -1]) - per_entry_cross_ent = (tf.nn.sigmoid_cross_entropy_with_logits( - labels=target_tensor, logits=prediction_tensor)) - prediction_probabilities = tf.sigmoid(prediction_tensor) - p_t = ((target_tensor * prediction_probabilities) + - ((1 - target_tensor) * (1 - prediction_probabilities))) - modulating_factor = 1.0 - if self._gamma: - modulating_factor = tf.pow(1.0 - p_t, self._gamma) - alpha_weight_factor = 1.0 - if self._alpha is not None: - alpha_weight_factor = (target_tensor * self._alpha + - (1 - target_tensor) * (1 - self._alpha)) - focal_cross_entropy_loss = (modulating_factor * alpha_weight_factor * - per_entry_cross_ent) - return focal_cross_entropy_loss * weights - - -class WeightedSoftmaxClassificationLoss(Loss): - """Softmax loss function.""" - - def __init__(self, logit_scale=1.0): - """Constructor. - - Args: - logit_scale: When this value is high, the prediction is "diffused" and - when this value is low, the prediction is made peakier. - (default 1.0) - - """ - super(WeightedSoftmaxClassificationLoss, self).__init__() - self._logit_scale = logit_scale - - def _compute_loss(self, prediction_tensor, target_tensor, weights): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing the predicted logits for each class - target_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing one-hot encoded classification targets - weights: a float tensor of shape, either [batch_size, num_anchors, - num_classes] or [batch_size, num_anchors, 1]. If the shape is - [batch_size, num_anchors, 1], all the classses are equally weighted. - - Returns: - loss: a float tensor of shape [batch_size, num_anchors] - representing the value of the loss function. - """ - weights = tf.reduce_mean(weights, axis=2) - num_classes = prediction_tensor.get_shape().as_list()[-1] - prediction_tensor = tf.divide( - prediction_tensor, self._logit_scale, name='scale_logit') - per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits( - labels=tf.reshape(target_tensor, [-1, num_classes]), - logits=tf.reshape(prediction_tensor, [-1, num_classes]))) - return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights - - -class WeightedSoftmaxClassificationAgainstLogitsLoss(Loss): - """Softmax loss function against logits. - - Targets are expected to be provided in logits space instead of "one hot" or - "probability distribution" space. - """ - - def __init__(self, logit_scale=1.0): - """Constructor. - - Args: - logit_scale: When this value is high, the target is "diffused" and - when this value is low, the target is made peakier. - (default 1.0) - - """ - super(WeightedSoftmaxClassificationAgainstLogitsLoss, self).__init__() - self._logit_scale = logit_scale - - def _scale_and_softmax_logits(self, logits): - """Scale logits then apply softmax.""" - scaled_logits = tf.divide(logits, self._logit_scale, name='scale_logits') - return tf.nn.softmax(scaled_logits, name='convert_scores') - - def _compute_loss(self, prediction_tensor, target_tensor, weights): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing the predicted logits for each class - target_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing logit classification targets - weights: a float tensor of shape, either [batch_size, num_anchors, - num_classes] or [batch_size, num_anchors, 1]. If the shape is - [batch_size, num_anchors, 1], all the classses are equally weighted. - - Returns: - loss: a float tensor of shape [batch_size, num_anchors] - representing the value of the loss function. - """ - weights = tf.reduce_mean(weights, axis=2) - num_classes = prediction_tensor.get_shape().as_list()[-1] - target_tensor = self._scale_and_softmax_logits(target_tensor) - prediction_tensor = tf.divide(prediction_tensor, self._logit_scale, - name='scale_logits') - - per_row_cross_ent = (tf.nn.softmax_cross_entropy_with_logits( - labels=tf.reshape(target_tensor, [-1, num_classes]), - logits=tf.reshape(prediction_tensor, [-1, num_classes]))) - return tf.reshape(per_row_cross_ent, tf.shape(weights)) * weights - - -class BootstrappedSigmoidClassificationLoss(Loss): - """Bootstrapped sigmoid cross entropy classification loss function. - - This loss uses a convex combination of training labels and the current model's - predictions as training targets in the classification loss. The idea is that - as the model improves over time, its predictions can be trusted more and we - can use these predictions to mitigate the damage of noisy/incorrect labels, - because incorrect labels are likely to be eventually highly inconsistent with - other stimuli predicted to have the same label by the model. - - In "soft" bootstrapping, we use all predicted class probabilities, whereas in - "hard" bootstrapping, we use the single class favored by the model. - - See also Training Deep Neural Networks On Noisy Labels with Bootstrapping by - Reed et al. (ICLR 2015). - """ - - def __init__(self, alpha, bootstrap_type='soft'): - """Constructor. - - Args: - alpha: a float32 scalar tensor between 0 and 1 representing interpolation - weight - bootstrap_type: set to either 'hard' or 'soft' (default) - - Raises: - ValueError: if bootstrap_type is not either 'hard' or 'soft' - """ - super(BootstrappedSigmoidClassificationLoss, self).__init__() - if bootstrap_type != 'hard' and bootstrap_type != 'soft': - raise ValueError('Unrecognized bootstrap_type: must be one of ' - '\'hard\' or \'soft.\'') - self._alpha = alpha - self._bootstrap_type = bootstrap_type - - def _compute_loss(self, prediction_tensor, target_tensor, weights): - """Compute loss function. - - Args: - prediction_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing the predicted logits for each class - target_tensor: A float tensor of shape [batch_size, num_anchors, - num_classes] representing one-hot encoded classification targets - weights: a float tensor of shape, either [batch_size, num_anchors, - num_classes] or [batch_size, num_anchors, 1]. If the shape is - [batch_size, num_anchors, 1], all the classses are equally weighted. - - Returns: - loss: a float tensor of shape [batch_size, num_anchors, num_classes] - representing the value of the loss function. - """ - if self._bootstrap_type == 'soft': - bootstrap_target_tensor = self._alpha * target_tensor + ( - 1.0 - self._alpha) * tf.sigmoid(prediction_tensor) - else: - bootstrap_target_tensor = self._alpha * target_tensor + ( - 1.0 - self._alpha) * tf.cast( - tf.sigmoid(prediction_tensor) > 0.5, tf.float32) - per_entry_cross_ent = (tf.nn.sigmoid_cross_entropy_with_logits( - labels=bootstrap_target_tensor, logits=prediction_tensor)) - return per_entry_cross_ent * weights - - -class HardExampleMiner(object): - """Hard example mining for regions in a list of images. - - Implements hard example mining to select a subset of regions to be - back-propagated. For each image, selects the regions with highest losses, - subject to the condition that a newly selected region cannot have - an IOU > iou_threshold with any of the previously selected regions. - This can be achieved by re-using a greedy non-maximum suppression algorithm. - A constraint on the number of negatives mined per positive region can also be - enforced. - - Reference papers: "Training Region-based Object Detectors with Online - Hard Example Mining" (CVPR 2016) by Srivastava et al., and - "SSD: Single Shot MultiBox Detector" (ECCV 2016) by Liu et al. - """ - - def __init__(self, - num_hard_examples=64, - iou_threshold=0.7, - loss_type='both', - cls_loss_weight=0.05, - loc_loss_weight=0.06, - max_negatives_per_positive=None, - min_negatives_per_image=0): - """Constructor. - - The hard example mining implemented by this class can replicate the behavior - in the two aforementioned papers (Srivastava et al., and Liu et al). - To replicate the A2 paper (Srivastava et al), num_hard_examples is set - to a fixed parameter (64 by default) and iou_threshold is set to .7 for - running non-max-suppression the predicted boxes prior to hard mining. - In order to replicate the SSD paper (Liu et al), num_hard_examples should - be set to None, max_negatives_per_positive should be 3 and iou_threshold - should be 1.0 (in order to effectively turn off NMS). - - Args: - num_hard_examples: maximum number of hard examples to be - selected per image (prior to enforcing max negative to positive ratio - constraint). If set to None, all examples obtained after NMS are - considered. - iou_threshold: minimum intersection over union for an example - to be discarded during NMS. - loss_type: use only classification losses ('cls', default), - localization losses ('loc') or both losses ('both'). - In the last case, cls_loss_weight and loc_loss_weight are used to - compute weighted sum of the two losses. - cls_loss_weight: weight for classification loss. - loc_loss_weight: weight for location loss. - max_negatives_per_positive: maximum number of negatives to retain for - each positive anchor. By default, num_negatives_per_positive is None, - which means that we do not enforce a prespecified negative:positive - ratio. Note also that num_negatives_per_positives can be a float - (and will be converted to be a float even if it is passed in otherwise). - min_negatives_per_image: minimum number of negative anchors to sample for - a given image. Setting this to a positive number allows sampling - negatives in an image without any positive anchors and thus not biased - towards at least one detection per image. - """ - self._num_hard_examples = num_hard_examples - self._iou_threshold = iou_threshold - self._loss_type = loss_type - self._cls_loss_weight = cls_loss_weight - self._loc_loss_weight = loc_loss_weight - self._max_negatives_per_positive = max_negatives_per_positive - self._min_negatives_per_image = min_negatives_per_image - if self._max_negatives_per_positive is not None: - self._max_negatives_per_positive = float(self._max_negatives_per_positive) - self._num_positives_list = None - self._num_negatives_list = None - - def __call__(self, - location_losses, - cls_losses, - decoded_boxlist_list, - match_list=None): - """Computes localization and classification losses after hard mining. - - Args: - location_losses: a float tensor of shape [num_images, num_anchors] - representing anchorwise localization losses. - cls_losses: a float tensor of shape [num_images, num_anchors] - representing anchorwise classification losses. - decoded_boxlist_list: a list of decoded BoxList representing location - predictions for each image. - match_list: an optional list of matcher.Match objects encoding the match - between anchors and groundtruth boxes for each image of the batch, - with rows of the Match objects corresponding to groundtruth boxes - and columns corresponding to anchors. Match objects in match_list are - used to reference which anchors are positive, negative or ignored. If - self._max_negatives_per_positive exists, these are then used to enforce - a prespecified negative to positive ratio. - - Returns: - mined_location_loss: a float scalar with sum of localization losses from - selected hard examples. - mined_cls_loss: a float scalar with sum of classification losses from - selected hard examples. - Raises: - ValueError: if location_losses, cls_losses and decoded_boxlist_list do - not have compatible shapes (i.e., they must correspond to the same - number of images). - ValueError: if match_list is specified but its length does not match - len(decoded_boxlist_list). - """ - mined_location_losses = [] - mined_cls_losses = [] - location_losses = tf.unstack(location_losses) - cls_losses = tf.unstack(cls_losses) - num_images = len(decoded_boxlist_list) - if not match_list: - match_list = num_images * [None] - if not len(location_losses) == len(decoded_boxlist_list) == len(cls_losses): - raise ValueError('location_losses, cls_losses and decoded_boxlist_list ' - 'do not have compatible shapes.') - if not isinstance(match_list, list): - raise ValueError('match_list must be a list.') - if len(match_list) != len(decoded_boxlist_list): - raise ValueError('match_list must either be None or have ' - 'length=len(decoded_boxlist_list).') - num_positives_list = [] - num_negatives_list = [] - for ind, detection_boxlist in enumerate(decoded_boxlist_list): - box_locations = detection_boxlist.get() - match = match_list[ind] - image_losses = cls_losses[ind] - if self._loss_type == 'loc': - image_losses = location_losses[ind] - elif self._loss_type == 'both': - image_losses *= self._cls_loss_weight - image_losses += location_losses[ind] * self._loc_loss_weight - if self._num_hard_examples is not None: - num_hard_examples = self._num_hard_examples - else: - num_hard_examples = detection_boxlist.num_boxes() - selected_indices = tf.image.non_max_suppression( - box_locations, image_losses, num_hard_examples, self._iou_threshold) - if self._max_negatives_per_positive is not None and match: - (selected_indices, num_positives, - num_negatives) = self._subsample_selection_to_desired_neg_pos_ratio( - selected_indices, match, self._max_negatives_per_positive, - self._min_negatives_per_image) - num_positives_list.append(num_positives) - num_negatives_list.append(num_negatives) - mined_location_losses.append( - tf.reduce_sum(tf.gather(location_losses[ind], selected_indices))) - mined_cls_losses.append( - tf.reduce_sum(tf.gather(cls_losses[ind], selected_indices))) - location_loss = tf.reduce_sum(tf.stack(mined_location_losses)) - cls_loss = tf.reduce_sum(tf.stack(mined_cls_losses)) - if match and self._max_negatives_per_positive: - self._num_positives_list = num_positives_list - self._num_negatives_list = num_negatives_list - return (location_loss, cls_loss) - - def summarize(self): - """Summarize the number of positives and negatives after mining.""" - if self._num_positives_list and self._num_negatives_list: - avg_num_positives = tf.reduce_mean( - tf.cast(self._num_positives_list, dtype=tf.float32)) - avg_num_negatives = tf.reduce_mean( - tf.cast(self._num_negatives_list, dtype=tf.float32)) - tf.summary.scalar('HardExampleMiner/NumPositives', avg_num_positives) - tf.summary.scalar('HardExampleMiner/NumNegatives', avg_num_negatives) - - def _subsample_selection_to_desired_neg_pos_ratio(self, - indices, - match, - max_negatives_per_positive, - min_negatives_per_image=0): - """Subsample a collection of selected indices to a desired neg:pos ratio. - - This function takes a subset of M indices (indexing into a large anchor - collection of N anchors where M=0, - meaning that column i is matched with row match_results[i]. - (2) match_results[i]=-1, meaning that column i is not matched. - (3) match_results[i]=-2, meaning that column i is ignored. - use_matmul_gather: Use matrix multiplication based gather instead of - standard tf.gather. (Default: False). - - Raises: - ValueError: if match_results does not have rank 1 or is not an - integer int32 scalar tensor - """ - if match_results.shape.ndims != 1: - raise ValueError('match_results should have rank 1') - if match_results.dtype != tf.int32: - raise ValueError('match_results should be an int32 or int64 scalar ' - 'tensor') - self._match_results = match_results - self._gather_op = tf.gather - if use_matmul_gather: - self._gather_op = ops.matmul_gather_on_zeroth_axis - - @property - def match_results(self): - """The accessor for match results. - - Returns: - the tensor which encodes the match results. - """ - return self._match_results - - def matched_column_indices(self): - """Returns column indices that match to some row. - - The indices returned by this op are always sorted in increasing order. - - Returns: - column_indices: int32 tensor of shape [K] with column indices. - """ - return self._reshape_and_cast(tf.where(tf.greater(self._match_results, -1))) - - def matched_column_indicator(self): - """Returns column indices that are matched. - - Returns: - column_indices: int32 tensor of shape [K] with column indices. - """ - return tf.greater_equal(self._match_results, 0) - - def num_matched_columns(self): - """Returns number (int32 scalar tensor) of matched columns.""" - return tf.size(self.matched_column_indices()) - - def unmatched_column_indices(self): - """Returns column indices that do not match any row. - - The indices returned by this op are always sorted in increasing order. - - Returns: - column_indices: int32 tensor of shape [K] with column indices. - """ - return self._reshape_and_cast(tf.where(tf.equal(self._match_results, -1))) - - def unmatched_column_indicator(self): - """Returns column indices that are unmatched. - - Returns: - column_indices: int32 tensor of shape [K] with column indices. - """ - return tf.equal(self._match_results, -1) - - def num_unmatched_columns(self): - """Returns number (int32 scalar tensor) of unmatched columns.""" - return tf.size(self.unmatched_column_indices()) - - def ignored_column_indices(self): - """Returns column indices that are ignored (neither Matched nor Unmatched). - - The indices returned by this op are always sorted in increasing order. - - Returns: - column_indices: int32 tensor of shape [K] with column indices. - """ - return self._reshape_and_cast(tf.where(self.ignored_column_indicator())) - - def ignored_column_indicator(self): - """Returns boolean column indicator where True means the colum is ignored. - - Returns: - column_indicator: boolean vector which is True for all ignored column - indices. - """ - return tf.equal(self._match_results, -2) - - def num_ignored_columns(self): - """Returns number (int32 scalar tensor) of matched columns.""" - return tf.size(self.ignored_column_indices()) - - def unmatched_or_ignored_column_indices(self): - """Returns column indices that are unmatched or ignored. - - The indices returned by this op are always sorted in increasing order. - - Returns: - column_indices: int32 tensor of shape [K] with column indices. - """ - return self._reshape_and_cast(tf.where(tf.greater(0, self._match_results))) - - def matched_row_indices(self): - """Returns row indices that match some column. - - The indices returned by this op are ordered so as to be in correspondence - with the output of matched_column_indicator(). For example if - self.matched_column_indicator() is [0,2], and self.matched_row_indices() is - [7, 3], then we know that column 0 was matched to row 7 and column 2 was - matched to row 3. - - Returns: - row_indices: int32 tensor of shape [K] with row indices. - """ - return self._reshape_and_cast( - self._gather_op(tf.cast(self._match_results, dtype=tf.float32), - self.matched_column_indices())) - - def num_matched_rows(self): - """Returns number (int32 scalar tensor) of matched rows.""" - unique_rows, _ = tf.unique(self.matched_row_indices()) - return tf.size(unique_rows) - - def _reshape_and_cast(self, t): - return tf.cast(tf.reshape(t, [-1]), tf.int32) - - def gather_based_on_match(self, input_tensor, unmatched_value, - ignored_value): - """Gathers elements from `input_tensor` based on match results. - - For columns that are matched to a row, gathered_tensor[col] is set to - input_tensor[match_results[col]]. For columns that are unmatched, - gathered_tensor[col] is set to unmatched_value. Finally, for columns that - are ignored gathered_tensor[col] is set to ignored_value. - - Note that the input_tensor.shape[1:] must match with unmatched_value.shape - and ignored_value.shape - - Args: - input_tensor: Tensor to gather values from. - unmatched_value: Constant tensor value for unmatched columns. - ignored_value: Constant tensor value for ignored columns. - - Returns: - gathered_tensor: A tensor containing values gathered from input_tensor. - The shape of the gathered tensor is [match_results.shape[0]] + - input_tensor.shape[1:]. - """ - input_tensor = tf.concat( - [tf.stack([ignored_value, unmatched_value]), - input_tensor], - axis=0) - gather_indices = tf.maximum(self.match_results + 2, 0) - gathered_tensor = self._gather_op(input_tensor, gather_indices) - return gathered_tensor - - -class Matcher(six.with_metaclass(abc.ABCMeta, object)): - """Abstract base class for matcher. - """ - - def __init__(self, use_matmul_gather=False): - """Constructs a Matcher. - - Args: - use_matmul_gather: Force constructed match objects to use matrix - multiplication based gather instead of standard tf.gather. - (Default: False). - """ - self._use_matmul_gather = use_matmul_gather - - def match(self, similarity_matrix, valid_rows=None, scope=None): - """Computes matches among row and column indices and returns the result. - - Computes matches among the row and column indices based on the similarity - matrix and optional arguments. - - Args: - similarity_matrix: Float tensor of shape [N, M] with pairwise similarity - where higher value means more similar. - valid_rows: A boolean tensor of shape [N] indicating the rows that are - valid for matching. - scope: Op scope name. Defaults to 'Match' if None. - - Returns: - A Match object with the results of matching. - """ - with tf.name_scope(scope, 'Match') as scope: - if valid_rows is None: - valid_rows = tf.ones(tf.shape(similarity_matrix)[0], dtype=tf.bool) - return Match(self._match(similarity_matrix, valid_rows), - self._use_matmul_gather) - - @abc.abstractmethod - def _match(self, similarity_matrix, valid_rows): - """Method to be overridden by implementations. - - Args: - similarity_matrix: Float tensor of shape [N, M] with pairwise similarity - where higher value means more similar. - valid_rows: A boolean tensor of shape [N] indicating the rows that are - valid for matching. - Returns: - match_results: Integer tensor of shape [M]: match_results[i]>=0 means - that column i is matched to row match_results[i], match_results[i]=-1 - means that the column is not matched. match_results[i]=-2 means that - the column is ignored (usually this happens when there is a very weak - match which one neither wants as positive nor negative example). - """ - pass diff --git a/research/object_detection/core/matcher_test.py b/research/object_detection/core/matcher_test.py deleted file mode 100644 index ad64075397e..00000000000 --- a/research/object_detection/core/matcher_test.py +++ /dev/null @@ -1,191 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.matcher.""" -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import matcher -from object_detection.utils import test_case - - -class MatchTest(test_case.TestCase): - - def test_get_correct_matched_columnIndices(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - matched_column_indices = match.matched_column_indices() - return matched_column_indices - expected_column_indices = [0, 1, 3, 5] - matched_column_indices = self.execute(graph_fn, []) - self.assertAllEqual(matched_column_indices, expected_column_indices) - - def test_get_correct_counts(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 1, -2]) - match = matcher.Match(match_results) - num_matched_columns = match.num_matched_columns() - num_unmatched_columns = match.num_unmatched_columns() - num_ignored_columns = match.num_ignored_columns() - num_matched_rows = match.num_matched_rows() - return [num_matched_columns, num_unmatched_columns, num_ignored_columns, - num_matched_rows] - (num_matched_columns_out, num_unmatched_columns_out, - num_ignored_columns_out, - num_matched_rows_out) = self.execute_cpu(graph_fn, []) - exp_num_matched_columns = 4 - exp_num_unmatched_columns = 2 - exp_num_ignored_columns = 1 - exp_num_matched_rows = 3 - self.assertAllEqual(num_matched_columns_out, exp_num_matched_columns) - self.assertAllEqual(num_unmatched_columns_out, exp_num_unmatched_columns) - self.assertAllEqual(num_ignored_columns_out, exp_num_ignored_columns) - self.assertAllEqual(num_matched_rows_out, exp_num_matched_rows) - - def testGetCorrectUnmatchedColumnIndices(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - unmatched_column_indices = match.unmatched_column_indices() - return unmatched_column_indices - unmatched_column_indices = self.execute(graph_fn, []) - expected_column_indices = [2, 4] - self.assertAllEqual(unmatched_column_indices, expected_column_indices) - - def testGetCorrectMatchedRowIndices(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - matched_row_indices = match.matched_row_indices() - return matched_row_indices - matched_row_indices = self.execute(graph_fn, []) - expected_row_indices = [3, 1, 0, 5] - self.assertAllEqual(matched_row_indices, expected_row_indices) - - def test_get_correct_ignored_column_indices(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - ignored_column_indices = match.ignored_column_indices() - return ignored_column_indices - ignored_column_indices = self.execute(graph_fn, []) - expected_column_indices = [6] - self.assertAllEqual(ignored_column_indices, expected_column_indices) - - def test_get_correct_matched_column_indicator(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - matched_column_indicator = match.matched_column_indicator() - return matched_column_indicator - expected_column_indicator = [True, True, False, True, False, True, False] - matched_column_indicator = self.execute(graph_fn, []) - self.assertAllEqual(matched_column_indicator, expected_column_indicator) - - def test_get_correct_unmatched_column_indicator(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - unmatched_column_indicator = match.unmatched_column_indicator() - return unmatched_column_indicator - expected_column_indicator = [False, False, True, False, True, False, False] - unmatched_column_indicator = self.execute(graph_fn, []) - self.assertAllEqual(unmatched_column_indicator, expected_column_indicator) - - def test_get_correct_ignored_column_indicator(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - ignored_column_indicator = match.ignored_column_indicator() - return ignored_column_indicator - expected_column_indicator = [False, False, False, False, False, False, True] - ignored_column_indicator = self.execute(graph_fn, []) - self.assertAllEqual(ignored_column_indicator, expected_column_indicator) - - def test_get_correct_unmatched_ignored_column_indices(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - match = matcher.Match(match_results) - unmatched_ignored_column_indices = (match. - unmatched_or_ignored_column_indices()) - return unmatched_ignored_column_indices - expected_column_indices = [2, 4, 6] - unmatched_ignored_column_indices = self.execute(graph_fn, []) - self.assertAllEqual(unmatched_ignored_column_indices, - expected_column_indices) - - def test_all_columns_accounted_for(self): - # Note: deliberately setting to small number so not always - # all possibilities appear (matched, unmatched, ignored) - def graph_fn(): - match_results = tf.random_uniform( - [num_matches], minval=-2, maxval=5, dtype=tf.int32) - match = matcher.Match(match_results) - matched_column_indices = match.matched_column_indices() - unmatched_column_indices = match.unmatched_column_indices() - ignored_column_indices = match.ignored_column_indices() - return (matched_column_indices, unmatched_column_indices, - ignored_column_indices) - num_matches = 10 - matched, unmatched, ignored = self.execute(graph_fn, []) - all_indices = np.hstack((matched, unmatched, ignored)) - all_indices_sorted = np.sort(all_indices) - self.assertAllEqual(all_indices_sorted, - np.arange(num_matches, dtype=np.int32)) - - def test_scalar_gather_based_on_match(self): - def graph_fn(): - match_results = tf.constant([3, 1, -1, 0, -1, 5, -2]) - input_tensor = tf.constant([0, 1, 2, 3, 4, 5, 6, 7], dtype=tf.float32) - match = matcher.Match(match_results) - gathered_tensor = match.gather_based_on_match(input_tensor, - unmatched_value=100., - ignored_value=200.) - return gathered_tensor - expected_gathered_tensor = [3, 1, 100, 0, 100, 5, 200] - gathered_tensor_out = self.execute(graph_fn, []) - self.assertAllEqual(expected_gathered_tensor, gathered_tensor_out) - - def test_multidimensional_gather_based_on_match(self): - def graph_fn(): - match_results = tf.constant([1, -1, -2]) - input_tensor = tf.constant([[0, 0.5, 0, 0.5], [0, 0, 0.5, 0.5]], - dtype=tf.float32) - match = matcher.Match(match_results) - gathered_tensor = match.gather_based_on_match(input_tensor, - unmatched_value=tf.zeros(4), - ignored_value=tf.zeros(4)) - return gathered_tensor - expected_gathered_tensor = [[0, 0, 0.5, 0.5], [0, 0, 0, 0], [0, 0, 0, 0]] - gathered_tensor_out = self.execute(graph_fn, []) - self.assertAllEqual(expected_gathered_tensor, gathered_tensor_out) - - def test_multidimensional_gather_based_on_match_with_matmul_gather_op(self): - def graph_fn(): - match_results = tf.constant([1, -1, -2]) - input_tensor = tf.constant([[0, 0.5, 0, 0.5], [0, 0, 0.5, 0.5]], - dtype=tf.float32) - match = matcher.Match(match_results, use_matmul_gather=True) - gathered_tensor = match.gather_based_on_match(input_tensor, - unmatched_value=tf.zeros(4), - ignored_value=tf.zeros(4)) - return gathered_tensor - expected_gathered_tensor = [[0, 0, 0.5, 0.5], [0, 0, 0, 0], [0, 0, 0, 0]] - gathered_tensor_out = self.execute(graph_fn, []) - self.assertAllEqual(expected_gathered_tensor, gathered_tensor_out) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/minibatch_sampler.py b/research/object_detection/core/minibatch_sampler.py deleted file mode 100644 index 9a5b0a72425..00000000000 --- a/research/object_detection/core/minibatch_sampler.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base minibatch sampler module. - -The job of the minibatch_sampler is to subsample a minibatch based on some -criterion. - -The main function call is: - subsample(indicator, batch_size, **params). -Indicator is a 1d boolean tensor where True denotes which examples can be -sampled. It returns a boolean indicator where True denotes an example has been -sampled.. - -Subclasses should implement the Subsample function and can make use of the -@staticmethod SubsampleIndicator. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from abc import ABCMeta -from abc import abstractmethod - -import six -import tensorflow.compat.v1 as tf - -from object_detection.utils import ops - - -class MinibatchSampler(six.with_metaclass(ABCMeta, object)): - """Abstract base class for subsampling minibatches.""" - - def __init__(self): - """Constructs a minibatch sampler.""" - pass - - @abstractmethod - def subsample(self, indicator, batch_size, **params): - """Returns subsample of entries in indicator. - - Args: - indicator: boolean tensor of shape [N] whose True entries can be sampled. - batch_size: desired batch size. - **params: additional keyword arguments for specific implementations of - the MinibatchSampler. - - Returns: - sample_indicator: boolean tensor of shape [N] whose True entries have been - sampled. If sum(indicator) >= batch_size, sum(is_sampled) = batch_size - """ - pass - - @staticmethod - def subsample_indicator(indicator, num_samples): - """Subsample indicator vector. - - Given a boolean indicator vector with M elements set to `True`, the function - assigns all but `num_samples` of these previously `True` elements to - `False`. If `num_samples` is greater than M, the original indicator vector - is returned. - - Args: - indicator: a 1-dimensional boolean tensor indicating which elements - are allowed to be sampled and which are not. - num_samples: int32 scalar tensor - - Returns: - a boolean tensor with the same shape as input (indicator) tensor - """ - indices = tf.where(indicator) - indices = tf.random_shuffle(indices) - indices = tf.reshape(indices, [-1]) - - num_samples = tf.minimum(tf.size(indices), num_samples) - selected_indices = tf.slice(indices, [0], tf.reshape(num_samples, [1])) - - selected_indicator = ops.indices_to_dense_vector(selected_indices, - tf.shape(indicator)[0]) - - return tf.equal(selected_indicator, 1) diff --git a/research/object_detection/core/minibatch_sampler_test.py b/research/object_detection/core/minibatch_sampler_test.py deleted file mode 100644 index 4b277a5b860..00000000000 --- a/research/object_detection/core/minibatch_sampler_test.py +++ /dev/null @@ -1,71 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for google3.research.vale.object_detection.minibatch_sampler.""" - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import minibatch_sampler -from object_detection.utils import test_case - - -class MinibatchSamplerTest(test_case.TestCase): - - def test_subsample_indicator_when_more_true_elements_than_num_samples(self): - np_indicator = np.array([True, False, True, False, True, True, False]) - def graph_fn(indicator): - samples = minibatch_sampler.MinibatchSampler.subsample_indicator( - indicator, 3) - return samples - samples_out = self.execute(graph_fn, [np_indicator]) - self.assertTrue(np.sum(samples_out), 3) - self.assertAllEqual(samples_out, - np.logical_and(samples_out, np_indicator)) - - def test_subsample_indicator_when_less_true_elements_than_num_samples(self): - np_indicator = np.array([True, False, True, False, True, True, False]) - def graph_fn(indicator): - samples = minibatch_sampler.MinibatchSampler.subsample_indicator( - indicator, 5) - return samples - samples_out = self.execute(graph_fn, [np_indicator]) - self.assertTrue(np.sum(samples_out), 4) - self.assertAllEqual(samples_out, - np.logical_and(samples_out, np_indicator)) - - def test_subsample_indicator_when_num_samples_is_zero(self): - np_indicator = np.array([True, False, True, False, True, True, False]) - def graph_fn(indicator): - samples_none = minibatch_sampler.MinibatchSampler.subsample_indicator( - indicator, 0) - return samples_none - samples_out = self.execute(graph_fn, [np_indicator]) - self.assertAllEqual( - np.zeros_like(samples_out, dtype=bool), - samples_out) - - def test_subsample_indicator_when_indicator_all_false(self): - indicator_empty = np.zeros([0], dtype=bool) - def graph_fn(indicator): - samples_empty = minibatch_sampler.MinibatchSampler.subsample_indicator( - indicator, 4) - return samples_empty - samples_out = self.execute(graph_fn, [indicator_empty]) - self.assertEqual(0, samples_out.size) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/model.py b/research/object_detection/core/model.py deleted file mode 100644 index ae3eae68b1b..00000000000 --- a/research/object_detection/core/model.py +++ /dev/null @@ -1,592 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Abstract detection model. - -This file defines a generic base class for detection models. Programs that are -designed to work with arbitrary detection models should only depend on this -class. We intend for the functions in this class to follow tensor-in/tensor-out -design, thus all functions have tensors or lists/dictionaries holding tensors as -inputs and outputs. - -Abstractly, detection models predict output tensors given input images -which can be passed to a loss function at training time or passed to a -postprocessing function at eval time. The computation graphs at a high level -consequently look as follows: - -Training time: -inputs (images tensor) -> preprocess -> predict -> loss -> outputs (loss tensor) - -Evaluation time: -inputs (images tensor) -> preprocess -> predict -> postprocess - -> outputs (boxes tensor, scores tensor, classes tensor, num_detections tensor) - -DetectionModels must thus implement four functions (1) preprocess, (2) predict, -(3) postprocess and (4) loss. DetectionModels should make no assumptions about -the input size or aspect ratio --- they are responsible for doing any -resize/reshaping necessary (see docstring for the preprocess function). -Output classes are always integers in the range [0, num_classes). Any mapping -of these integers to semantic labels is to be handled outside of this class. - -Images are resized in the `preprocess` method. All of `preprocess`, `predict`, -and `postprocess` should be reentrant. - -The `preprocess` method runs `image_resizer_fn` that returns resized_images and -`true_image_shapes`. Since `image_resizer_fn` can pad the images with zeros, -true_image_shapes indicate the slices that contain the image without padding. -This is useful for padding images to be a fixed size for batching. - -The `postprocess` method uses the true image shapes to clip predictions that lie -outside of images. - -By default, DetectionModels produce bounding box detections; However, we support -a handful of auxiliary annotations associated with each bounding box, namely, -instance masks and keypoints. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc -import six -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields as fields - - -# If using a new enough version of TensorFlow, detection models should be a -# tf module or keras model for tracking. -try: - _BaseClass = tf.keras.layers.Layer -except AttributeError: - _BaseClass = object - - -class DetectionModel(six.with_metaclass(abc.ABCMeta, _BaseClass)): - """Abstract base class for detection models. - - Extends tf.Module to guarantee variable tracking. - """ - - def __init__(self, num_classes): - """Constructor. - - Args: - num_classes: number of classes. Note that num_classes *does not* include - background categories that might be implicitly predicted in various - implementations. - """ - self._num_classes = num_classes - self._groundtruth_lists = {} - self._training_step = None - - super(DetectionModel, self).__init__() - - @property - def num_classes(self): - return self._num_classes - - def groundtruth_lists(self, field): - """Access list of groundtruth tensors. - - Args: - field: a string key, options are - fields.BoxListFields.{boxes,classes,masks,mask_weights,keypoints, - keypoint_visibilities, densepose_*, track_ids, - temporal_offsets, track_match_flags} - fields.InputDataFields.is_annotated. - - Returns: - a list of tensors holding groundtruth information (see also - provide_groundtruth function below), with one entry for each image in the - batch. - Raises: - RuntimeError: if the field has not been provided via provide_groundtruth. - """ - if field not in self._groundtruth_lists: - raise RuntimeError('Groundtruth tensor {} has not been provided'.format( - field)) - return self._groundtruth_lists[field] - - def groundtruth_has_field(self, field): - """Determines whether the groundtruth includes the given field. - - Args: - field: a string key, options are - fields.BoxListFields.{boxes,classes,masks,mask_weights,keypoints, - keypoint_visibilities, densepose_*, track_ids} or - fields.InputDataFields.is_annotated. - - Returns: - True if the groundtruth includes the given field, False otherwise. - """ - return field in self._groundtruth_lists - - @property - def training_step(self): - if self._training_step is None: - raise ValueError('Training step was not provided to the model.') - - return self._training_step - - @staticmethod - def get_side_inputs(features): - """Get side inputs from input features. - - This placeholder method provides a way for a meta-architecture to specify - how to grab additional side inputs from input features (in addition to the - image itself) and allows models to depend on contextual information. By - default, detection models do not use side information (and thus this method - returns an empty dictionary by default. However it can be overridden if - side inputs are necessary." - - Args: - features: A dictionary of tensors. - - Returns: - An empty dictionary by default. - """ - return {} - - @abc.abstractmethod - def preprocess(self, inputs): - """Input preprocessing. - - To be overridden by implementations. - - This function is responsible for any scaling/shifting of input values that - is necessary prior to running the detector on an input image. - It is also responsible for any resizing, padding that might be necessary - as images are assumed to arrive in arbitrary sizes. While this function - could conceivably be part of the predict method (below), it is often - convenient to keep these separate --- for example, we may want to preprocess - on one device, place onto a queue, and let another device (e.g., the GPU) - handle prediction. - - A few important notes about the preprocess function: - + We assume that this operation does not have any trainable variables nor - does it affect the groundtruth annotations in any way (thus data - augmentation operations such as random cropping should be performed - externally). - + There is no assumption that the batchsize in this function is the same as - the batch size in the predict function. In fact, we recommend calling the - preprocess function prior to calling any batching operations (which should - happen outside of the model) and thus assuming that batch sizes are equal - to 1 in the preprocess function. - + There is also no explicit assumption that the output resolutions - must be fixed across inputs --- this is to support "fully convolutional" - settings in which input images can have different shapes/resolutions. - - Args: - inputs: a [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: a [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - """ - pass - - @abc.abstractmethod - def predict(self, preprocessed_inputs, true_image_shapes, **side_inputs): - """Predict prediction tensors from inputs tensor. - - Outputs of this function can be passed to loss or postprocess functions. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float32 tensor - representing a batch of images. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - **side_inputs: additional tensors that are required by the network. - - Returns: - prediction_dict: a dictionary holding prediction tensors to be - passed to the Loss or Postprocess functions. - """ - pass - - @abc.abstractmethod - def postprocess(self, prediction_dict, true_image_shapes, **params): - """Convert predicted output tensors to final detections. - - This stage typically performs a few things such as - * Non-Max Suppression to remove overlapping detection boxes. - * Score conversion and background class removal. - - Outputs adhere to the following conventions: - * Classes are integers in [0, num_classes); background classes are removed - and the first non-background class is mapped to 0. If the model produces - class-agnostic detections, then no output is produced for classes. - * Boxes are to be interpreted as being in [y_min, x_min, y_max, x_max] - format and normalized relative to the image window. - * `num_detections` is provided for settings where detections are padded to a - fixed number of boxes. - * We do not specifically assume any kind of probabilistic interpretation - of the scores --- the only important thing is their relative ordering. - Thus implementations of the postprocess function are free to output - logits, probabilities, calibrated probabilities, or anything else. - - Args: - prediction_dict: a dictionary holding prediction tensors. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - **params: Additional keyword arguments for specific implementations of - DetectionModel. - - Returns: - detections: a dictionary containing the following fields - detection_boxes: [batch, max_detections, 4] - detection_scores: [batch, max_detections] - detection_classes: [batch, max_detections] - (If a model is producing class-agnostic detections, this field may be - missing) - detection_masks: [batch, max_detections, mask_height, mask_width] - (optional) - detection_keypoints: [batch, max_detections, num_keypoints, 2] - (optional) - detection_keypoint_scores: [batch, max_detections, num_keypoints] - (optional) - detection_surface_coords: [batch, max_detections, mask_height, - mask_width, 2] (optional) - num_detections: [batch] - - In addition to the above fields this stage also outputs the following - raw tensors: - - raw_detection_boxes: [batch, total_detections, 4] tensor containing - all detection boxes from `prediction_dict` in the format - [ymin, xmin, ymax, xmax] and normalized co-ordinates. - raw_detection_scores: [batch, total_detections, - num_classes_with_background] tensor of class score logits for - raw detection boxes. - """ - pass - - @abc.abstractmethod - def loss(self, prediction_dict, true_image_shapes): - """Compute scalar loss tensors with respect to provided groundtruth. - - Calling this function requires that groundtruth tensors have been - provided via the provide_groundtruth function. - - Args: - prediction_dict: a dictionary holding predicted tensors - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - a dictionary mapping strings (loss names) to scalar tensors representing - loss values. - """ - pass - - def provide_groundtruth( - self, - groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_masks_list=None, - groundtruth_mask_weights_list=None, - groundtruth_keypoints_list=None, - groundtruth_keypoint_visibilities_list=None, - groundtruth_dp_num_points_list=None, - groundtruth_dp_part_ids_list=None, - groundtruth_dp_surface_coords_list=None, - groundtruth_track_ids_list=None, - groundtruth_temporal_offsets_list=None, - groundtruth_track_match_flags_list=None, - groundtruth_weights_list=None, - groundtruth_confidences_list=None, - groundtruth_is_crowd_list=None, - groundtruth_group_of_list=None, - groundtruth_area_list=None, - is_annotated_list=None, - groundtruth_labeled_classes=None, - groundtruth_verified_neg_classes=None, - groundtruth_not_exhaustive_classes=None, - groundtruth_keypoint_depths_list=None, - groundtruth_keypoint_depth_weights_list=None, - groundtruth_image_classes=None, - training_step=None): - """Provide groundtruth tensors. - - Args: - groundtruth_boxes_list: a list of 2-D tf.float32 tensors of shape - [num_boxes, 4] containing coordinates of the groundtruth boxes. - Groundtruth boxes are provided in [y_min, x_min, y_max, x_max] - format and assumed to be normalized and clipped - relative to the image window with y_min <= y_max and x_min <= x_max. - groundtruth_classes_list: a list of 2-D tf.float32 one-hot (or k-hot) - tensors of shape [num_boxes, num_classes] containing the class targets - with the 0th index assumed to map to the first non-background class. - groundtruth_masks_list: a list of 3-D tf.float32 tensors of - shape [num_boxes, height_in, width_in] containing instance - masks with values in {0, 1}. If None, no masks are provided. - Mask resolution `height_in`x`width_in` must agree with the resolution - of the input image tensor provided to the `preprocess` function. - groundtruth_mask_weights_list: a list of 1-D tf.float32 tensors of shape - [num_boxes] with weights for each instance mask. - groundtruth_keypoints_list: a list of 3-D tf.float32 tensors of - shape [num_boxes, num_keypoints, 2] containing keypoints. - Keypoints are assumed to be provided in normalized coordinates and - missing keypoints should be encoded as NaN (but it is recommended to use - `groundtruth_keypoint_visibilities_list`). - groundtruth_keypoint_visibilities_list: a list of 3-D tf.bool tensors - of shape [num_boxes, num_keypoints] containing keypoint visibilities. - groundtruth_dp_num_points_list: a list of 1-D tf.int32 tensors of shape - [num_boxes] containing the number of DensePose sampled points. - groundtruth_dp_part_ids_list: a list of 2-D tf.int32 tensors of shape - [num_boxes, max_sampled_points] containing the DensePose part ids - (0-indexed) for each sampled point. Note that there may be padding. - groundtruth_dp_surface_coords_list: a list of 3-D tf.float32 tensors of - shape [num_boxes, max_sampled_points, 4] containing the DensePose - surface coordinates for each sampled point. Note that there may be - padding. - groundtruth_track_ids_list: a list of 1-D tf.int32 tensors of shape - [num_boxes] containing the track IDs of groundtruth objects. - groundtruth_temporal_offsets_list: a list of 2-D tf.float32 tensors - of shape [num_boxes, 2] containing the spatial offsets of objects' - centers compared with the previous frame. - groundtruth_track_match_flags_list: a list of 1-D tf.float32 tensors - of shape [num_boxes] containing 0-1 flags that indicate if an object - has existed in the previous frame. - groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape - [num_boxes] containing weights for groundtruth boxes. - groundtruth_confidences_list: A list of 2-D tf.float32 tensors of shape - [num_boxes, num_classes] containing class confidences for groundtruth - boxes. - groundtruth_is_crowd_list: A list of 1-D tf.bool tensors of shape - [num_boxes] containing is_crowd annotations. - groundtruth_group_of_list: A list of 1-D tf.bool tensors of shape - [num_boxes] containing group_of annotations. - groundtruth_area_list: A list of 1-D tf.float32 tensors of shape - [num_boxes] containing the area (in the original absolute coordinates) - of the annotations. - is_annotated_list: A list of scalar tf.bool tensors indicating whether - images have been labeled or not. - groundtruth_labeled_classes: A list of 1-D tf.float32 tensors of shape - [num_classes], containing label indices encoded as k-hot of the classes - that are exhaustively annotated. - groundtruth_verified_neg_classes: A list of 1-D tf.float32 tensors of - shape [num_classes], containing a K-hot representation of classes - which were verified as not present in the image. - groundtruth_not_exhaustive_classes: A list of 1-D tf.float32 tensors of - shape [num_classes], containing a K-hot representation of classes - which don't have all of their instances marked exhaustively. - groundtruth_keypoint_depths_list: a list of 2-D tf.float32 tensors - of shape [num_boxes, num_keypoints] containing keypoint relative depths. - groundtruth_keypoint_depth_weights_list: a list of 2-D tf.float32 tensors - of shape [num_boxes, num_keypoints] containing the weights of the - relative depths. - groundtruth_image_classes: A list of 1-D tf.float32 tensors of shape - [num_classes], containing label indices encoded as k-hot of the classes - that are present or not present in the image. - training_step: An integer denoting the current training step. This is - useful when models want to anneal loss terms. - """ - self._groundtruth_lists[fields.BoxListFields.boxes] = groundtruth_boxes_list - self._groundtruth_lists[ - fields.BoxListFields.classes] = groundtruth_classes_list - if groundtruth_weights_list: - self._groundtruth_lists[fields.BoxListFields. - weights] = groundtruth_weights_list - if groundtruth_confidences_list: - self._groundtruth_lists[fields.BoxListFields. - confidences] = groundtruth_confidences_list - if groundtruth_masks_list: - self._groundtruth_lists[ - fields.BoxListFields.masks] = groundtruth_masks_list - if groundtruth_mask_weights_list: - self._groundtruth_lists[ - fields.BoxListFields.mask_weights] = groundtruth_mask_weights_list - if groundtruth_keypoints_list: - self._groundtruth_lists[ - fields.BoxListFields.keypoints] = groundtruth_keypoints_list - if groundtruth_keypoint_visibilities_list: - self._groundtruth_lists[ - fields.BoxListFields.keypoint_visibilities] = ( - groundtruth_keypoint_visibilities_list) - if groundtruth_keypoint_depths_list: - self._groundtruth_lists[ - fields.BoxListFields.keypoint_depths] = ( - groundtruth_keypoint_depths_list) - if groundtruth_keypoint_depth_weights_list: - self._groundtruth_lists[ - fields.BoxListFields.keypoint_depth_weights] = ( - groundtruth_keypoint_depth_weights_list) - if groundtruth_dp_num_points_list: - self._groundtruth_lists[ - fields.BoxListFields.densepose_num_points] = ( - groundtruth_dp_num_points_list) - if groundtruth_dp_part_ids_list: - self._groundtruth_lists[ - fields.BoxListFields.densepose_part_ids] = ( - groundtruth_dp_part_ids_list) - if groundtruth_dp_surface_coords_list: - self._groundtruth_lists[ - fields.BoxListFields.densepose_surface_coords] = ( - groundtruth_dp_surface_coords_list) - if groundtruth_track_ids_list: - self._groundtruth_lists[ - fields.BoxListFields.track_ids] = groundtruth_track_ids_list - if groundtruth_temporal_offsets_list: - self._groundtruth_lists[ - fields.BoxListFields.temporal_offsets] = ( - groundtruth_temporal_offsets_list) - if groundtruth_track_match_flags_list: - self._groundtruth_lists[ - fields.BoxListFields.track_match_flags] = ( - groundtruth_track_match_flags_list) - if groundtruth_is_crowd_list: - self._groundtruth_lists[ - fields.BoxListFields.is_crowd] = groundtruth_is_crowd_list - if groundtruth_group_of_list: - self._groundtruth_lists[ - fields.BoxListFields.group_of] = groundtruth_group_of_list - if groundtruth_area_list: - self._groundtruth_lists[ - fields.InputDataFields.groundtruth_area] = groundtruth_area_list - if is_annotated_list: - self._groundtruth_lists[ - fields.InputDataFields.is_annotated] = is_annotated_list - if groundtruth_labeled_classes: - self._groundtruth_lists[ - fields.InputDataFields - .groundtruth_labeled_classes] = groundtruth_labeled_classes - if groundtruth_verified_neg_classes: - self._groundtruth_lists[ - fields.InputDataFields - .groundtruth_verified_neg_classes] = groundtruth_verified_neg_classes - if groundtruth_image_classes: - self._groundtruth_lists[ - fields.InputDataFields - .groundtruth_image_classes] = groundtruth_image_classes - if groundtruth_not_exhaustive_classes: - self._groundtruth_lists[ - fields.InputDataFields - .groundtruth_not_exhaustive_classes] = ( - groundtruth_not_exhaustive_classes) - if training_step is not None: - self._training_step = training_step - - @abc.abstractmethod - def regularization_losses(self): - """Returns a list of regularization losses for this model. - - Returns a list of regularization losses for this model that the estimator - needs to use during training/optimization. - - Returns: - A list of regularization loss tensors. - """ - pass - - @abc.abstractmethod - def restore_map(self, - fine_tune_checkpoint_type='detection', - load_all_detection_checkpoint_vars=False): - """Returns a map of variables to load from a foreign checkpoint. - - Returns a map of variable names to load from a checkpoint to variables in - the model graph. This enables the model to initialize based on weights from - another task. For example, the feature extractor variables from a - classification model can be used to bootstrap training of an object - detector. When loading from an object detection model, the checkpoint model - should have the same parameters as this detection model with exception of - the num_classes parameter. - - Args: - fine_tune_checkpoint_type: whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`. Default 'detection'. - load_all_detection_checkpoint_vars: whether to load all variables (when - `fine_tune_checkpoint_type` is `detection`). If False, only variables - within the feature extractor scope are included. Default False. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - """ - pass - - @abc.abstractmethod - def restore_from_objects(self, fine_tune_checkpoint_type='detection'): - """Returns a map of variables to load from a foreign checkpoint. - - Returns a dictionary of Tensorflow 2 Trackable objects (e.g. tf.Module - or Checkpoint). This enables the model to initialize based on weights from - another task. For example, the feature extractor variables from a - classification model can be used to bootstrap training of an object - detector. When loading from an object detection model, the checkpoint model - should have the same parameters as this detection model with exception of - the num_classes parameter. - - Note that this function is intended to be used to restore Keras-based - models when running Tensorflow 2, whereas restore_map (above) is intended - to be used to restore Slim-based models when running Tensorflow 1.x. - - TODO(jonathanhuang,rathodv): Check tf_version and raise unimplemented - error for both restore_map and restore_from_objects depending on version. - - Args: - fine_tune_checkpoint_type: whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`. Default 'detection'. - - Returns: - A dict mapping keys to Trackable objects (tf.Module or Checkpoint). - """ - pass - - @abc.abstractmethod - def updates(self): - """Returns a list of update operators for this model. - - Returns a list of update operators for this model that must be executed at - each training step. The estimator's train op needs to have a control - dependency on these updates. - - Returns: - A list of update operators. - """ - pass - - def call(self, images): - """Returns detections from a batch of images. - - This method calls the preprocess, predict and postprocess function - sequentially and returns the output. - - Args: - images: a [batch_size, height, width, channels] float tensor. - - Returns: - detetcions: The dict of tensors returned by the postprocess function. - """ - - preprocessed_images, shapes = self.preprocess(images) - prediction_dict = self.predict(preprocessed_images, shapes) - return self.postprocess(prediction_dict, shapes) diff --git a/research/object_detection/core/model_test.py b/research/object_detection/core/model_test.py deleted file mode 100644 index fcc36c03d4a..00000000000 --- a/research/object_detection/core/model_test.py +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for model API.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf - -from object_detection.core import model -from object_detection.utils import test_case - - -class FakeModel(model.DetectionModel): - - def __init__(self): - - # sub-networks containing weights of different shapes. - self._network1 = tf.keras.Sequential([ - tf.keras.layers.Conv2D(8, 1) - ]) - - self._network2 = tf.keras.Sequential([ - tf.keras.layers.Conv2D(16, 1) - ]) - - super(FakeModel, self).__init__(num_classes=0) - - def preprocess(self, images): - return images, tf.shape(images) - - def predict(self, images, shapes): - return {'prediction': self._network2(self._network1(images))} - - def postprocess(self, prediction_dict, shapes): - return prediction_dict - - def loss(self): - return tf.constant(0.0) - - def updates(self): - return [] - - def restore_map(self): - return {} - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def regularization_losses(self): - return [] - - -class ModelTest(test_case.TestCase): - - def test_model_call(self): - - detection_model = FakeModel() - - def graph_fn(): - return detection_model(tf.zeros((1, 128, 128, 3))) - - result = self.execute(graph_fn, []) - self.assertEqual(result['prediction'].shape, - (1, 128, 128, 16)) - - def test_freeze(self): - - detection_model = FakeModel() - detection_model(tf.zeros((1, 128, 128, 3))) - - net1_var_shapes = [tuple(var.get_shape().as_list()) for var in - detection_model._network1.trainable_variables] - - del detection_model - - detection_model = FakeModel() - detection_model._network2.trainable = False - detection_model(tf.zeros((1, 128, 128, 3))) - - var_shapes = [tuple(var.get_shape().as_list()) for var in - detection_model._network1.trainable_variables] - - self.assertEqual(set(net1_var_shapes), set(var_shapes)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/multiclass_nms_test.py b/research/object_detection/core/multiclass_nms_test.py deleted file mode 100644 index 80be89da926..00000000000 --- a/research/object_detection/core/multiclass_nms_test.py +++ /dev/null @@ -1,583 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for tensorflow_models.object_detection.core.post_processing.""" -import numpy as np -import tensorflow.compat.v1 as tf -from object_detection.core import post_processing -from object_detection.core import standard_fields as fields -from object_detection.utils import test_case - - -class MulticlassNonMaxSuppressionTest(test_case.TestCase): - - def test_multiclass_nms_select_with_shared_boxes_cpu_only(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - - def graph_fn(boxes, scores): - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - nms, _ = post_processing.multiclass_non_max_suppression( - boxes, scores, score_thresh, iou_thresh, max_output_size) - return (nms.get(), nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes)) - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 100, 1, 101]] - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - (nms_corners_output, nms_scores_output, - nms_classes_output) = self.execute_cpu(graph_fn, [boxes, scores]) - self.assertAllClose(nms_corners_output, exp_nms_corners) - self.assertAllClose(nms_scores_output, exp_nms_scores) - self.assertAllClose(nms_classes_output, exp_nms_classes) - - def test_multiclass_nms_select_with_shared_boxes_pad_to_max_output_size(self): - boxes = np.array([[[0, 0, 1, 1]], - [[0, 0.1, 1, 1.1]], - [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], - [[0, 10.1, 1, 11.1]], - [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], - [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], - [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], - [.01, .85], [.01, .5]], np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_size_per_class = 4 - max_output_size = 5 - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 100, 1, 101]] - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - - def graph_fn(boxes, scores): - nms, num_valid_nms_boxes = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_size_per_class, - max_total_size=max_output_size, - pad_to_max_output_size=True) - return [nms.get(), nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), num_valid_nms_boxes] - - [nms_corners_output, nms_scores_output, nms_classes_output, - num_valid_nms_boxes] = self.execute(graph_fn, [boxes, scores]) - - self.assertEqual(num_valid_nms_boxes, 4) - self.assertAllClose(nms_corners_output[0:num_valid_nms_boxes], - exp_nms_corners) - self.assertAllClose(nms_scores_output[0:num_valid_nms_boxes], - exp_nms_scores) - self.assertAllClose(nms_classes_output[0:num_valid_nms_boxes], - exp_nms_classes) - - def test_multiclass_nms_select_with_shared_boxes_given_keypoints(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - num_keypoints = 6 - keypoints = np.tile(np.reshape(range(8), [8, 1, 1]), - [1, num_keypoints, 2]).astype(np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - def graph_fn(boxes, scores, keypoints): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - pad_to_max_output_size=True, - additional_fields={fields.BoxListFields.keypoints: keypoints}) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), - nms.get_field(fields.BoxListFields.keypoints), nms_valid - ] - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 100, 1, 101]] - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - exp_nms_keypoints = np.tile( - np.reshape(np.array([3, 0, 6, 5], np.float32), [4, 1, 1]), - [1, num_keypoints, 2]) - (nms_corners_output, nms_scores_output, nms_classes_output, nms_keypoints, - nms_valid) = self.execute(graph_fn, [boxes, scores, keypoints]) - - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - self.assertAllEqual(nms_keypoints[:nms_valid], exp_nms_keypoints) - - def test_multiclass_nms_with_shared_boxes_given_keypoint_heatmaps(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - - num_boxes = boxes.shape[0] - heatmap_height = 5 - heatmap_width = 5 - num_keypoints = 17 - keypoint_heatmaps = np.ones( - [num_boxes, heatmap_height, heatmap_width, num_keypoints], - dtype=np.float32) - - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 100, 1, 101]] - - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - exp_nms_keypoint_heatmaps = np.ones( - (4, heatmap_height, heatmap_width, num_keypoints), dtype=np.float32) - - def graph_fn(boxes, scores, keypoint_heatmaps): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - pad_to_max_output_size=True, - additional_fields={ - fields.BoxListFields.keypoint_heatmaps: keypoint_heatmaps - }) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), - nms.get_field(fields.BoxListFields.keypoint_heatmaps), nms_valid - ] - - (nms_corners_output, nms_scores_output, nms_classes_output, - nms_keypoint_heatmaps, - nms_valid) = self.execute(graph_fn, [boxes, scores, keypoint_heatmaps]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - self.assertAllEqual(nms_keypoint_heatmaps[:nms_valid], - exp_nms_keypoint_heatmaps) - - def test_multiclass_nms_with_additional_fields(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - - coarse_boxes_key = 'coarse_boxes' - coarse_boxes = np.array( - [[0.1, 0.1, 1.1, 1.1], [0.1, 0.2, 1.1, 1.2], [0.1, -0.2, 1.1, 1.0], - [0.1, 10.1, 1.1, 11.1], [0.1, 10.2, 1.1, 11.2], [ - 0.1, 100.1, 1.1, 101.1 - ], [0.1, 1000.1, 1.1, 1002.1], [0.1, 1000.1, 1.1, 1002.2]], np.float32) - - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = np.array([[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 100, 1, 101]], dtype=np.float32) - - exp_nms_coarse_corners = np.array([[0.1, 10.1, 1.1, 11.1], - [0.1, 0.1, 1.1, 1.1], - [0.1, 1000.1, 1.1, 1002.1], - [0.1, 100.1, 1.1, 101.1]], - dtype=np.float32) - - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - - def graph_fn(boxes, scores, coarse_boxes): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - pad_to_max_output_size=True, - additional_fields={coarse_boxes_key: coarse_boxes}) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), - nms.get_field(coarse_boxes_key), - nms_valid, - ] - - (nms_corners_output, nms_scores_output, nms_classes_output, - nms_coarse_corners, - nms_valid) = self.execute(graph_fn, [boxes, scores, coarse_boxes]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - self.assertAllEqual(nms_coarse_corners[:nms_valid], exp_nms_coarse_corners) - - def test_multiclass_nms_select_with_shared_boxes_given_masks(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - num_classes = 2 - mask_height = 3 - mask_width = 3 - masks = np.tile( - np.reshape(range(8), [8, 1, 1, 1]), - [1, num_classes, mask_height, mask_width]) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 100, 1, 101]] - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - exp_nms_masks_tensor = np.tile( - np.reshape(np.array([3, 0, 6, 5], np.float32), [4, 1, 1]), - [1, mask_height, mask_width]) - - def graph_fn(boxes, scores, masks): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - masks=masks, - pad_to_max_output_size=True) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), - nms.get_field(fields.BoxListFields.masks), nms_valid - ] - - (nms_corners_output, nms_scores_output, nms_classes_output, nms_masks, - nms_valid) = self.execute(graph_fn, [boxes, scores, masks]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - self.assertAllEqual(nms_masks[:nms_valid], exp_nms_masks_tensor) - - def test_multiclass_nms_select_with_clip_window(self): - boxes = np.array([[[0, 0, 10, 10]], [[1, 1, 11, 11]]], np.float32) - scores = np.array([[.9], [.75]], np.float32) - clip_window = np.array([5, 4, 8, 7], np.float32) - score_thresh = 0.0 - iou_thresh = 0.5 - max_output_size = 100 - - exp_nms_corners = [[5, 4, 8, 7]] - exp_nms_scores = [.9] - exp_nms_classes = [0] - - def graph_fn(boxes, scores, clip_window): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - pad_to_max_output_size=True, - clip_window=clip_window) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), nms_valid - ] - - (nms_corners_output, nms_scores_output, nms_classes_output, - nms_valid) = self.execute(graph_fn, [boxes, scores, clip_window]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - - def test_multiclass_nms_select_with_clip_window_change_coordinate_frame(self): - boxes = np.array([[[0, 0, 10, 10]], [[1, 1, 11, 11]]], np.float32) - scores = np.array([[.9], [.75]], np.float32) - clip_window = np.array([5, 4, 8, 7], np.float32) - score_thresh = 0.0 - iou_thresh = 0.5 - max_output_size = 100 - - exp_nms_corners = [[0, 0, 1, 1]] - exp_nms_scores = [.9] - exp_nms_classes = [0] - - def graph_fn(boxes, scores, clip_window): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - clip_window=clip_window, - pad_to_max_output_size=True, - change_coordinate_frame=True) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), nms_valid - ] - - (nms_corners_output, nms_scores_output, nms_classes_output, - nms_valid) = self.execute(graph_fn, [boxes, scores, clip_window]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - - def test_multiclass_nms_select_with_per_class_cap(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_size_per_class = 2 - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002]] - exp_nms_scores = [.95, .9, .85] - exp_nms_classes = [0, 0, 1] - - def graph_fn(boxes, scores): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_size_per_class, - pad_to_max_output_size=True) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), - nms_valid - ] - - (nms_corners_output, nms_scores_output, - nms_classes_output, nms_valid) = self.execute(graph_fn, [boxes, scores]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - - def test_multiclass_nms_select_with_total_cap(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_size_per_class = 4 - max_total_size = 2 - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1]] - exp_nms_scores = [.95, .9] - exp_nms_classes = [0, 0] - - def graph_fn(boxes, scores): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_size_per_class, - max_total_size, - pad_to_max_output_size=True) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), - nms_valid - ] - - (nms_corners_output, nms_scores_output, - nms_classes_output, nms_valid) = self.execute(graph_fn, [boxes, scores]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - - def test_multiclass_nms_threshold_then_select_with_shared_boxes(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9], [.75], [.6], [.95], [.5], [.3], [.01], [.01]], - np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 3 - - exp_nms = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 100, 1, 101]] - - def graph_fn(boxes, scores): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - pad_to_max_output_size=True) - return nms.get(), nms_valid - - nms_output, nms_valid = self.execute(graph_fn, [boxes, scores]) - self.assertAllClose(nms_output[:nms_valid], exp_nms) - - def test_multiclass_nms_select_with_separate_boxes(self): - boxes = np.array( - [[[0, 0, 1, 1], [0, 0, 4, 5]], [[0, 0.1, 1, 1.1], [0, 0.1, 2, 1.1]], - [[0, -0.1, 1, 0.9], [0, -0.1, 1, 0.9]], [[0, 10, 1, 11], [ - 0, 10, 1, 11 - ]], [[0, 10.1, 1, 11.1], [0, 10.1, 1, 11.1]], - [[0, 100, 1, 101], [0, 100, 1, 101]], - [[0, 1000, 1, 1002], [0, 999, 2, 1004]], - [[0, 1000, 1, 1002.1], [0, 999, 2, 1002.7]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - score_thresh = 0.1 - iou_thresh = .5 - max_output_size = 4 - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 999, 2, 1004], - [0, 100, 1, 101]] - exp_nms_scores = [.95, .9, .85, .3] - exp_nms_classes = [0, 0, 1, 0] - - def graph_fn(boxes, scores): - nms, nms_valid = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_output_size, - pad_to_max_output_size=True) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes), - nms_valid - ] - - (nms_corners_output, nms_scores_output, - nms_classes_output, nms_valid) = self.execute(graph_fn, [boxes, scores]) - self.assertAllClose(nms_corners_output[:nms_valid], exp_nms_corners) - self.assertAllClose(nms_scores_output[:nms_valid], exp_nms_scores) - self.assertAllClose(nms_classes_output[:nms_valid], exp_nms_classes) - - def test_multiclass_soft_nms_select_with_shared_boxes_cpu_only(self): - boxes = np.array( - [[[0, 0, 1, 1]], [[0, 0.1, 1, 1.1]], [[0, -0.1, 1, 0.9]], - [[0, 10, 1, 11]], [[0, 10.1, 1, 11.1]], [[0, 100, 1, 101]], - [[0, 1000, 1, 1002]], [[0, 1000, 1, 1002.1]]], np.float32) - scores = np.array([[.9, 0.01], [.75, 0.05], [.6, 0.01], [.95, 0], - [.5, 0.01], [.3, 0.01], [.01, .85], [.01, .5]], - np.float32) - score_thresh = 0.1 - iou_thresh = 1.0 - max_output_size = 4 - - exp_nms_corners = [[0, 10, 1, 11], - [0, 0, 1, 1], - [0, 1000, 1, 1002], - [0, 0.1, 1, 1.1]] - exp_nms_scores = [.95, .9, .85, .384] - exp_nms_classes = [0, 0, 1, 0] - - def graph_fn(boxes, scores): - nms, _ = post_processing.multiclass_non_max_suppression( - boxes, - scores, - score_thresh, - iou_thresh, - max_size_per_class=max_output_size, - max_total_size=max_output_size, - soft_nms_sigma=0.5) - return [ - nms.get(), - nms.get_field(fields.BoxListFields.scores), - nms.get_field(fields.BoxListFields.classes) - ] - - (nms_corners_output, nms_scores_output, - nms_classes_output) = self.execute_cpu(graph_fn, [boxes, scores]) - self.assertAllClose( - nms_corners_output, exp_nms_corners, rtol=1e-2, atol=1e-2) - self.assertAllClose(nms_scores_output, exp_nms_scores, rtol=1e-2, atol=1e-2) - self.assertAllClose( - nms_classes_output, exp_nms_classes, rtol=1e-2, atol=1e-2) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/post_processing.py b/research/object_detection/core/post_processing.py deleted file mode 100644 index fea777640d5..00000000000 --- a/research/object_detection/core/post_processing.py +++ /dev/null @@ -1,1276 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Post-processing operations on detected boxes.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import numpy as np -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import keypoint_ops -from object_detection.core import standard_fields as fields -from object_detection.utils import shape_utils - -_NMS_TILE_SIZE = 512 - - -def batch_iou(boxes1, boxes2): - """Calculates the overlap between proposal and ground truth boxes. - - Some `boxes2` may have been padded. The returned `iou` tensor for these - boxes will be -1. - - Args: - boxes1: a tensor with a shape of [batch_size, N, 4]. N is the number of - proposals before groundtruth assignment. The last dimension is the pixel - coordinates in [ymin, xmin, ymax, xmax] form. - boxes2: a tensor with a shape of [batch_size, MAX_NUM_INSTANCES, 4]. This - tensor might have paddings with a negative value. - - Returns: - iou: a tensor with as a shape of [batch_size, N, MAX_NUM_INSTANCES]. - """ - with tf.name_scope('BatchIOU'): - y1_min, x1_min, y1_max, x1_max = tf.split( - value=boxes1, num_or_size_splits=4, axis=2) - y2_min, x2_min, y2_max, x2_max = tf.split( - value=boxes2, num_or_size_splits=4, axis=2) - - # Calculates the intersection area. - intersection_xmin = tf.maximum(x1_min, tf.transpose(x2_min, [0, 2, 1])) - intersection_xmax = tf.minimum(x1_max, tf.transpose(x2_max, [0, 2, 1])) - intersection_ymin = tf.maximum(y1_min, tf.transpose(y2_min, [0, 2, 1])) - intersection_ymax = tf.minimum(y1_max, tf.transpose(y2_max, [0, 2, 1])) - intersection_area = tf.maximum( - (intersection_xmax - intersection_xmin), 0) * tf.maximum( - (intersection_ymax - intersection_ymin), 0) - - # Calculates the union area. - area1 = (y1_max - y1_min) * (x1_max - x1_min) - area2 = (y2_max - y2_min) * (x2_max - x2_min) - # Adds a small epsilon to avoid divide-by-zero. - union_area = area1 + tf.transpose(area2, - [0, 2, 1]) - intersection_area + 1e-8 - - # Calculates IoU. - iou = intersection_area / union_area - - # Fills -1 for padded ground truth boxes. - padding_mask = tf.logical_and( - tf.less(intersection_xmax, 0), tf.less(intersection_ymax, 0)) - iou = tf.where(padding_mask, -tf.ones_like(iou), iou) - - return iou - - -def _self_suppression(iou, iou_threshold, loop_condition, iou_sum): - """Bounding-boxes self-suppression loop body. - - Args: - iou: A float Tensor with shape [1, num_boxes, max_num_instance]: IOUs. - iou_threshold: A scalar, representing IOU threshold. - loop_condition: The loop condition returned from last iteration. - iou_sum: iou_sum_new returned from last iteration. - - Returns: - iou_suppressed: A float Tensor with shape [1, num_boxes, max_num_instance], - IOU after suppression. - iou_threshold: A scalar, representing IOU threshold. - loop_condition: Bool Tensor of shape [], the loop condition. - iou_sum_new: The new IOU sum. - """ - del loop_condition - can_suppress_others = tf.cast( - tf.reshape(tf.reduce_max(iou, 1) <= iou_threshold, [1, -1, 1]), iou.dtype) - iou_suppressed = tf.reshape( - tf.cast( - tf.reduce_max(can_suppress_others * iou, 1) <= iou_threshold, - iou.dtype), [1, -1, 1]) * iou - iou_sum_new = tf.reduce_sum(iou_suppressed, [1, 2]) - return [ - iou_suppressed, iou_threshold, - tf.reduce_any(iou_sum - iou_sum_new > iou_threshold), iou_sum_new - ] - - -def _cross_suppression(boxes, box_slice, iou_threshold, inner_idx): - """Bounding-boxes cross-suppression loop body. - - Args: - boxes: A float Tensor of shape [1, anchors, 4], representing boxes. - box_slice: A float Tensor of shape [1, _NMS_TILE_SIZE, 4], the box tile - returned from last iteration - iou_threshold: A scalar, representing IOU threshold. - inner_idx: A scalar, representing inner index. - - Returns: - boxes: A float Tensor of shape [1, anchors, 4], representing boxes. - ret_slice: A float Tensor of shape [1, _NMS_TILE_SIZE, 4], the box tile - after suppression - iou_threshold: A scalar, representing IOU threshold. - inner_idx: A scalar, inner index incremented. - """ - new_slice = tf.slice(boxes, [0, inner_idx * _NMS_TILE_SIZE, 0], - [1, _NMS_TILE_SIZE, 4]) - iou = batch_iou(new_slice, box_slice) - ret_slice = tf.expand_dims( - tf.cast(tf.reduce_all(iou < iou_threshold, [1]), box_slice.dtype), - 2) * box_slice - return boxes, ret_slice, iou_threshold, inner_idx + 1 - - -def _suppression_loop_body(boxes, iou_threshold, output_size, idx): - """Process boxes in the range [idx*_NMS_TILE_SIZE, (idx+1)*_NMS_TILE_SIZE). - - Args: - boxes: a tensor with a shape of [1, anchors, 4]. - iou_threshold: a float representing the threshold for deciding whether boxes - overlap too much with respect to IOU. - output_size: an int32 tensor of size [1]. Representing the number of - selected boxes. - idx: an integer scalar representing induction variable. - - Returns: - boxes: updated boxes. - iou_threshold: pass down iou_threshold to the next iteration. - output_size: the updated output_size. - idx: the updated induction variable. - """ - num_tiles = tf.shape(boxes)[1] // _NMS_TILE_SIZE - - # Iterates over tiles that can possibly suppress the current tile. - box_slice = tf.slice(boxes, [0, idx * _NMS_TILE_SIZE, 0], - [1, _NMS_TILE_SIZE, 4]) - _, box_slice, _, _ = tf.while_loop( - lambda _boxes, _box_slice, _threshold, inner_idx: inner_idx < idx, - _cross_suppression, [boxes, box_slice, iou_threshold, - tf.constant(0)]) - - # Iterates over the current tile to compute self-suppression. - iou = batch_iou(box_slice, box_slice) - mask = tf.expand_dims( - tf.reshape(tf.range(_NMS_TILE_SIZE), [1, -1]) > tf.reshape( - tf.range(_NMS_TILE_SIZE), [-1, 1]), 0) - iou *= tf.cast(tf.logical_and(mask, iou >= iou_threshold), iou.dtype) - suppressed_iou, _, _, _ = tf.while_loop( - lambda _iou, _threshold, loop_condition, _iou_sum: loop_condition, - _self_suppression, - [iou, iou_threshold, - tf.constant(True), - tf.reduce_sum(iou, [1, 2])]) - suppressed_box = tf.reduce_sum(suppressed_iou, 1) > 0 - box_slice *= tf.expand_dims(1.0 - tf.cast(suppressed_box, box_slice.dtype), 2) - - # Uses box_slice to update the input boxes. - mask = tf.reshape( - tf.cast(tf.equal(tf.range(num_tiles), idx), boxes.dtype), [1, -1, 1, 1]) - boxes = tf.tile(tf.expand_dims(box_slice, [1]), - [1, num_tiles, 1, 1]) * mask + tf.reshape( - boxes, [1, num_tiles, _NMS_TILE_SIZE, 4]) * (1 - mask) - boxes = tf.reshape(boxes, [1, -1, 4]) - - # Updates output_size. - output_size += tf.reduce_sum( - tf.cast(tf.reduce_any(box_slice > 0, [2]), tf.int32), [1]) - return boxes, iou_threshold, output_size, idx + 1 - - -def partitioned_non_max_suppression_padded(boxes, - scores, - max_output_size, - iou_threshold=0.5, - score_threshold=float('-inf')): - """A tiled version of [`tf.image.non_max_suppression_padded`](https://www.tensorflow.org/api_docs/python/tf/image/non_max_suppression_padded). - - The overall design of the algorithm is to handle boxes tile-by-tile: - - boxes = boxes.pad_to_multiple_of(tile_size) - num_tiles = len(boxes) // tile_size - output_boxes = [] - for i in range(num_tiles): - box_tile = boxes[i*tile_size : (i+1)*tile_size] - for j in range(i - 1): - suppressing_tile = boxes[j*tile_size : (j+1)*tile_size] - iou = batch_iou(box_tile, suppressing_tile) - # if the box is suppressed in iou, clear it to a dot - box_tile *= _update_boxes(iou) - # Iteratively handle the diagonal tile. - iou = _box_overlap(box_tile, box_tile) - iou_changed = True - while iou_changed: - # boxes that are not suppressed by anything else - suppressing_boxes = _get_suppressing_boxes(iou) - # boxes that are suppressed by suppressing_boxes - suppressed_boxes = _get_suppressed_boxes(iou, suppressing_boxes) - # clear iou to 0 for boxes that are suppressed, as they cannot be used - # to suppress other boxes any more - new_iou = _clear_iou(iou, suppressed_boxes) - iou_changed = (new_iou != iou) - iou = new_iou - # remaining boxes that can still suppress others, are selected boxes. - output_boxes.append(_get_suppressing_boxes(iou)) - if len(output_boxes) >= max_output_size: - break - - Args: - boxes: A 2-D float `Tensor` of shape `[num_boxes, 4]`. - scores: A 1-D float `Tensor` of shape `[num_boxes]` representing a single - score corresponding to each box (each row of boxes). - max_output_size: a scalar integer `Tensor` representing the maximum number - of boxes to be selected by non max suppression. - iou_threshold: a float representing the threshold for deciding whether boxes - overlap too much with respect to IOU. - score_threshold: A float representing the threshold for deciding when to - remove boxes based on score. - - Returns: - selected_indices: a tensor of shape [anchors]. - num_valid_boxes: a scalar int tensor. - nms_proposals: a tensor with a shape of [anchors, 4]. It has - same dtype as input boxes. - nms_scores: a tensor with a shape of [anchors]. It has same - dtype as input scores. - argsort_ids: a tensor of shape [anchors], mapping from input order of boxes - to output order of boxes. - """ - num_boxes = tf.shape(boxes)[0] - pad = tf.cast( - tf.ceil(tf.cast(num_boxes, tf.float32) / _NMS_TILE_SIZE), - tf.int32) * _NMS_TILE_SIZE - num_boxes - - scores, argsort_ids = tf.nn.top_k(scores, k=num_boxes, sorted=True) - boxes = tf.gather(boxes, argsort_ids) - num_boxes = tf.shape(boxes)[0] - num_boxes += pad - boxes = tf.pad( - tf.cast(boxes, tf.float32), [[0, pad], [0, 0]], constant_values=-1) - scores = tf.pad(tf.cast(scores, tf.float32), [[0, pad]]) - - # mask boxes to -1 by score threshold - scores_mask = tf.expand_dims( - tf.cast(scores > score_threshold, boxes.dtype), axis=1) - boxes = ((boxes + 1.) * scores_mask) - 1. - - boxes = tf.expand_dims(boxes, axis=0) - scores = tf.expand_dims(scores, axis=0) - - def _loop_cond(unused_boxes, unused_threshold, output_size, idx): - return tf.logical_and( - tf.reduce_min(output_size) < max_output_size, - idx < num_boxes // _NMS_TILE_SIZE) - - selected_boxes, _, output_size, _ = tf.while_loop( - _loop_cond, _suppression_loop_body, - [boxes, iou_threshold, - tf.zeros([1], tf.int32), - tf.constant(0)]) - idx = num_boxes - tf.cast( - tf.nn.top_k( - tf.cast(tf.reduce_any(selected_boxes > 0, [2]), tf.int32) * - tf.expand_dims(tf.range(num_boxes, 0, -1), 0), max_output_size)[0], - tf.int32) - idx = tf.minimum(idx, num_boxes - 1 - pad) - idx = tf.reshape(idx + tf.reshape(tf.range(1) * num_boxes, [-1, 1]), [-1]) - num_valid_boxes = tf.reduce_sum(output_size) - return (idx, num_valid_boxes, tf.reshape(boxes, [-1, 4]), - tf.reshape(scores, [-1]), argsort_ids) - - -def _validate_boxes_scores_iou_thresh(boxes, scores, iou_thresh, - change_coordinate_frame, clip_window): - """Validates boxes, scores and iou_thresh. - - This function validates the boxes, scores, iou_thresh - and if change_coordinate_frame is True, clip_window must be specified. - - Args: - boxes: A [k, q, 4] float32 tensor containing k detections. `q` can be either - number of classes or 1 depending on whether a separate box is predicted - per class. - scores: A [k, num_classes] float32 tensor containing the scores for each of - the k detections. The scores have to be non-negative when - pad_to_max_output_size is True. - iou_thresh: scalar threshold for IOU (new boxes that have high IOU overlap - with previously selected boxes are removed). - change_coordinate_frame: Whether to normalize coordinates after clipping - relative to clip_window (this can only be set to True if a clip_window is - provided) - clip_window: A float32 tensor of the form [y_min, x_min, y_max, x_max] - representing the window to clip and normalize boxes to before performing - non-max suppression. - - Raises: - ValueError: if iou_thresh is not in [0, 1] or if input boxlist does not - have a valid scores field. - """ - if not 0 <= iou_thresh <= 1.0: - raise ValueError('iou_thresh must be between 0 and 1') - if scores.shape.ndims != 2: - raise ValueError('scores field must be of rank 2') - if shape_utils.get_dim_as_int(scores.shape[1]) is None: - raise ValueError('scores must have statically defined second ' 'dimension') - if boxes.shape.ndims != 3: - raise ValueError('boxes must be of rank 3.') - if not (shape_utils.get_dim_as_int( - boxes.shape[1]) == shape_utils.get_dim_as_int(scores.shape[1]) or - shape_utils.get_dim_as_int(boxes.shape[1]) == 1): - raise ValueError('second dimension of boxes must be either 1 or equal ' - 'to the second dimension of scores') - if shape_utils.get_dim_as_int(boxes.shape[2]) != 4: - raise ValueError('last dimension of boxes must be of size 4.') - if change_coordinate_frame and clip_window is None: - raise ValueError('if change_coordinate_frame is True, then a clip_window' - 'must be specified.') - - -def _clip_window_prune_boxes(sorted_boxes, clip_window, pad_to_max_output_size, - change_coordinate_frame): - """Prune boxes with zero area. - - Args: - sorted_boxes: A BoxList containing k detections. - clip_window: A float32 tensor of the form [y_min, x_min, y_max, x_max] - representing the window to clip and normalize boxes to before performing - non-max suppression. - pad_to_max_output_size: flag indicating whether to pad to max output size or - not. - change_coordinate_frame: Whether to normalize coordinates after clipping - relative to clip_window (this can only be set to True if a clip_window is - provided). - - Returns: - sorted_boxes: A BoxList containing k detections after pruning. - num_valid_nms_boxes_cumulative: Number of valid NMS boxes - """ - sorted_boxes = box_list_ops.clip_to_window( - sorted_boxes, - clip_window, - filter_nonoverlapping=not pad_to_max_output_size) - # Set the scores of boxes with zero area to -1 to keep the default - # behaviour of pruning out zero area boxes. - sorted_boxes_size = tf.shape(sorted_boxes.get())[0] - non_zero_box_area = tf.cast(box_list_ops.area(sorted_boxes), tf.bool) - sorted_boxes_scores = tf.where( - non_zero_box_area, sorted_boxes.get_field(fields.BoxListFields.scores), - -1 * tf.ones(sorted_boxes_size)) - sorted_boxes.add_field(fields.BoxListFields.scores, sorted_boxes_scores) - num_valid_nms_boxes_cumulative = tf.reduce_sum( - tf.cast(tf.greater_equal(sorted_boxes_scores, 0), tf.int32)) - sorted_boxes = box_list_ops.sort_by_field(sorted_boxes, - fields.BoxListFields.scores) - if change_coordinate_frame: - sorted_boxes = box_list_ops.change_coordinate_frame(sorted_boxes, - clip_window) - if sorted_boxes.has_field(fields.BoxListFields.keypoints): - sorted_keypoints = sorted_boxes.get_field(fields.BoxListFields.keypoints) - sorted_keypoints = keypoint_ops.change_coordinate_frame(sorted_keypoints, - clip_window) - sorted_boxes.set_field(fields.BoxListFields.keypoints, sorted_keypoints) - return sorted_boxes, num_valid_nms_boxes_cumulative - - -def _clip_boxes(boxes, clip_window): - """Clips boxes to the given window. - - Args: - boxes: A [batch, num_boxes, 4] float32 tensor containing box coordinates in - [ymin, xmin, ymax, xmax] form. - clip_window: A [batch, 4] float32 tensor with left top and right bottom - coordinate of the window in [ymin, xmin, ymax, xmax] form. - - Returns: - A [batch, num_boxes, 4] float32 tensor containing boxes clipped to the given - window. - """ - ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=-1) - clipped_ymin = tf.maximum(ymin, clip_window[:, 0, tf.newaxis]) - clipped_xmin = tf.maximum(xmin, clip_window[:, 1, tf.newaxis]) - clipped_ymax = tf.minimum(ymax, clip_window[:, 2, tf.newaxis]) - clipped_xmax = tf.minimum(xmax, clip_window[:, 3, tf.newaxis]) - return tf.stack([clipped_ymin, clipped_xmin, clipped_ymax, clipped_xmax], - axis=-1) - - -class NullContextmanager(object): - - def __enter__(self): - pass - - def __exit__(self, type_arg, value_arg, traceback_arg): - return False - - -def multiclass_non_max_suppression(boxes, - scores, - score_thresh, - iou_thresh, - max_size_per_class, - max_total_size=0, - clip_window=None, - change_coordinate_frame=False, - masks=None, - boundaries=None, - pad_to_max_output_size=False, - use_partitioned_nms=False, - additional_fields=None, - soft_nms_sigma=0.0, - use_hard_nms=False, - use_cpu_nms=False, - scope=None): - """Multi-class version of non maximum suppression. - - This op greedily selects a subset of detection bounding boxes, pruning - away boxes that have high IOU (intersection over union) overlap (> thresh) - with already selected boxes. It operates independently for each class for - which scores are provided (via the scores field of the input box_list), - pruning boxes with score less than a provided threshold prior to - applying NMS. - - Please note that this operation is performed on *all* classes, therefore any - background classes should be removed prior to calling this function. - - Selected boxes are guaranteed to be sorted in decreasing order by score (but - the sort is not guaranteed to be stable). - - Args: - boxes: A [k, q, 4] float32 tensor containing k detections. `q` can be either - number of classes or 1 depending on whether a separate box is predicted - per class. - scores: A [k, num_classes] float32 tensor containing the scores for each of - the k detections. The scores have to be non-negative when - pad_to_max_output_size is True. - score_thresh: scalar threshold for score (low scoring boxes are removed). - iou_thresh: scalar threshold for IOU (new boxes that have high IOU overlap - with previously selected boxes are removed). - max_size_per_class: maximum number of retained boxes per class. - max_total_size: maximum number of boxes retained over all classes. By - default returns all boxes retained after capping boxes per class. - clip_window: A float32 tensor of the form [y_min, x_min, y_max, x_max] - representing the window to clip and normalize boxes to before performing - non-max suppression. - change_coordinate_frame: Whether to normalize coordinates after clipping - relative to clip_window (this can only be set to True if a clip_window - is provided) - masks: (optional) a [k, q, mask_height, mask_width] float32 tensor - containing box masks. `q` can be either number of classes or 1 depending - on whether a separate mask is predicted per class. - boundaries: (optional) a [k, q, boundary_height, boundary_width] float32 - tensor containing box boundaries. `q` can be either number of classes or 1 - depending on whether a separate boundary is predicted per class. - pad_to_max_output_size: If true, the output nmsed boxes are padded to be of - length `max_size_per_class`. Defaults to false. - use_partitioned_nms: If true, use partitioned version of - non_max_suppression. - additional_fields: (optional) If not None, a dictionary that maps keys to - tensors whose first dimensions are all of size `k`. After non-maximum - suppression, all tensors corresponding to the selected boxes will be - added to resulting BoxList. - soft_nms_sigma: A scalar float representing the Soft NMS sigma parameter; - See Bodla et al, https://arxiv.org/abs/1704.04503). When - `soft_nms_sigma=0.0` (which is default), we fall back to standard (hard) - NMS. Soft NMS is currently only supported when pad_to_max_output_size is - False. - use_hard_nms: Enforce the usage of hard NMS. - use_cpu_nms: Enforce NMS to run on CPU. - scope: name scope. - - Returns: - A tuple of sorted_boxes and num_valid_nms_boxes. The sorted_boxes is a - BoxList holds M boxes with a rank-1 scores field representing - corresponding scores for each box with scores sorted in decreasing order - and a rank-1 classes field representing a class label for each box. The - num_valid_nms_boxes is a 0-D integer tensor representing the number of - valid elements in `BoxList`, with the valid elements appearing first. - - Raises: - ValueError: if iou_thresh is not in [0, 1] or if input boxlist does not have - a valid scores field. - ValueError: if Soft NMS (tf.image.non_max_suppression_with_scores) is not - supported in the current TF version and `soft_nms_sigma` is nonzero. - """ - _validate_boxes_scores_iou_thresh(boxes, scores, iou_thresh, - change_coordinate_frame, clip_window) - if pad_to_max_output_size and soft_nms_sigma != 0.0: - raise ValueError('Soft NMS (soft_nms_sigma != 0.0) is currently not ' - 'supported when pad_to_max_output_size is True.') - - with tf.name_scope(scope, 'MultiClassNonMaxSuppression'), tf.device( - 'cpu:0') if use_cpu_nms else NullContextmanager(): - num_scores = tf.shape(scores)[0] - num_classes = shape_utils.get_dim_as_int(scores.get_shape()[1]) - - selected_boxes_list = [] - num_valid_nms_boxes_cumulative = tf.constant(0) - per_class_boxes_list = tf.unstack(boxes, axis=1) - if masks is not None: - per_class_masks_list = tf.unstack(masks, axis=1) - if boundaries is not None: - per_class_boundaries_list = tf.unstack(boundaries, axis=1) - boxes_ids = (range(num_classes) if len(per_class_boxes_list) > 1 - else [0] * num_classes) - for class_idx, boxes_idx in zip(range(num_classes), boxes_ids): - per_class_boxes = per_class_boxes_list[boxes_idx] - boxlist_and_class_scores = box_list.BoxList(per_class_boxes) - class_scores = tf.reshape( - tf.slice(scores, [0, class_idx], tf.stack([num_scores, 1])), [-1]) - - boxlist_and_class_scores.add_field(fields.BoxListFields.scores, - class_scores) - if masks is not None: - per_class_masks = per_class_masks_list[boxes_idx] - boxlist_and_class_scores.add_field(fields.BoxListFields.masks, - per_class_masks) - if boundaries is not None: - per_class_boundaries = per_class_boundaries_list[boxes_idx] - boxlist_and_class_scores.add_field(fields.BoxListFields.boundaries, - per_class_boundaries) - if additional_fields is not None: - for key, tensor in additional_fields.items(): - boxlist_and_class_scores.add_field(key, tensor) - - nms_result = None - selected_scores = None - if pad_to_max_output_size: - max_selection_size = max_size_per_class - if use_partitioned_nms: - (selected_indices, num_valid_nms_boxes, - boxlist_and_class_scores.data['boxes'], - boxlist_and_class_scores.data['scores'], - _) = partitioned_non_max_suppression_padded( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field(fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh) - else: - selected_indices, num_valid_nms_boxes = ( - tf.image.non_max_suppression_padded( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field( - fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh, - pad_to_max_output_size=True)) - nms_result = box_list_ops.gather(boxlist_and_class_scores, - selected_indices) - selected_scores = nms_result.get_field(fields.BoxListFields.scores) - else: - max_selection_size = tf.minimum(max_size_per_class, - boxlist_and_class_scores.num_boxes()) - if (hasattr(tf.image, 'non_max_suppression_with_scores') and - tf.compat.forward_compatible(2019, 6, 6) and not use_hard_nms): - (selected_indices, selected_scores - ) = tf.image.non_max_suppression_with_scores( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field(fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh, - soft_nms_sigma=soft_nms_sigma) - num_valid_nms_boxes = tf.shape(selected_indices)[0] - selected_indices = tf.concat( - [selected_indices, - tf.zeros(max_selection_size-num_valid_nms_boxes, tf.int32)], 0) - selected_scores = tf.concat( - [selected_scores, - tf.zeros(max_selection_size-num_valid_nms_boxes, - tf.float32)], -1) - nms_result = box_list_ops.gather(boxlist_and_class_scores, - selected_indices) - else: - if soft_nms_sigma != 0: - raise ValueError('Soft NMS not supported in current TF version!') - selected_indices = tf.image.non_max_suppression( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field(fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh) - num_valid_nms_boxes = tf.shape(selected_indices)[0] - selected_indices = tf.concat( - [selected_indices, - tf.zeros(max_selection_size-num_valid_nms_boxes, tf.int32)], 0) - nms_result = box_list_ops.gather(boxlist_and_class_scores, - selected_indices) - selected_scores = nms_result.get_field(fields.BoxListFields.scores) - # Make the scores -1 for invalid boxes. - valid_nms_boxes_indices = tf.less( - tf.range(max_selection_size), num_valid_nms_boxes) - - nms_result.add_field( - fields.BoxListFields.scores, - tf.where(valid_nms_boxes_indices, - selected_scores, -1*tf.ones(max_selection_size))) - num_valid_nms_boxes_cumulative += num_valid_nms_boxes - - nms_result.add_field( - fields.BoxListFields.classes, (tf.zeros_like( - nms_result.get_field(fields.BoxListFields.scores)) + class_idx)) - selected_boxes_list.append(nms_result) - selected_boxes = box_list_ops.concatenate(selected_boxes_list) - sorted_boxes = box_list_ops.sort_by_field(selected_boxes, - fields.BoxListFields.scores) - if clip_window is not None: - # When pad_to_max_output_size is False, it prunes the boxes with zero - # area. - sorted_boxes, num_valid_nms_boxes_cumulative = _clip_window_prune_boxes( - sorted_boxes, clip_window, pad_to_max_output_size, - change_coordinate_frame) - - if max_total_size: - max_total_size = tf.minimum(max_total_size, sorted_boxes.num_boxes()) - sorted_boxes = box_list_ops.gather(sorted_boxes, tf.range(max_total_size)) - num_valid_nms_boxes_cumulative = tf.where( - max_total_size > num_valid_nms_boxes_cumulative, - num_valid_nms_boxes_cumulative, max_total_size) - # Select only the valid boxes if pad_to_max_output_size is False. - if not pad_to_max_output_size: - sorted_boxes = box_list_ops.gather( - sorted_boxes, tf.range(num_valid_nms_boxes_cumulative)) - - return sorted_boxes, num_valid_nms_boxes_cumulative - - -def class_agnostic_non_max_suppression(boxes, - scores, - score_thresh, - iou_thresh, - max_classes_per_detection=1, - max_total_size=0, - clip_window=None, - change_coordinate_frame=False, - masks=None, - boundaries=None, - pad_to_max_output_size=False, - use_partitioned_nms=False, - additional_fields=None, - soft_nms_sigma=0.0, - scope=None): - """Class-agnostic version of non maximum suppression. - - This op greedily selects a subset of detection bounding boxes, pruning - away boxes that have high IOU (intersection over union) overlap (> thresh) - with already selected boxes. It operates on all the boxes using - max scores across all classes for which scores are provided (via the scores - field of the input box_list), pruning boxes with score less than a provided - threshold prior to applying NMS. - - Please note that this operation is performed in a class-agnostic way, - therefore any background classes should be removed prior to calling this - function. - - Selected boxes are guaranteed to be sorted in decreasing order by score (but - the sort is not guaranteed to be stable). - - Args: - boxes: A [k, q, 4] float32 tensor containing k detections. `q` can be either - number of classes or 1 depending on whether a separate box is predicted - per class. - scores: A [k, num_classes] float32 tensor containing the scores for each of - the k detections. The scores have to be non-negative when - pad_to_max_output_size is True. - score_thresh: scalar threshold for score (low scoring boxes are removed). - iou_thresh: scalar threshold for IOU (new boxes that have high IOU overlap - with previously selected boxes are removed). - max_classes_per_detection: maximum number of retained classes per detection - box in class-agnostic NMS. - max_total_size: maximum number of boxes retained over all classes. By - default returns all boxes retained after capping boxes per class. - clip_window: A float32 tensor of the form [y_min, x_min, y_max, x_max] - representing the window to clip and normalize boxes to before performing - non-max suppression. - change_coordinate_frame: Whether to normalize coordinates after clipping - relative to clip_window (this can only be set to True if a clip_window is - provided) - masks: (optional) a [k, q, mask_height, mask_width] float32 tensor - containing box masks. `q` can be either number of classes or 1 depending - on whether a separate mask is predicted per class. - boundaries: (optional) a [k, q, boundary_height, boundary_width] float32 - tensor containing box boundaries. `q` can be either number of classes or 1 - depending on whether a separate boundary is predicted per class. - pad_to_max_output_size: If true, the output nmsed boxes are padded to be of - length `max_size_per_class`. Defaults to false. - use_partitioned_nms: If true, use partitioned version of - non_max_suppression. - additional_fields: (optional) If not None, a dictionary that maps keys to - tensors whose first dimensions are all of size `k`. After non-maximum - suppression, all tensors corresponding to the selected boxes will be added - to resulting BoxList. - soft_nms_sigma: A scalar float representing the Soft NMS sigma parameter; - See Bodla et al, https://arxiv.org/abs/1704.04503). When - `soft_nms_sigma=0.0` (which is default), we fall back to standard (hard) - NMS. Soft NMS is currently only supported when pad_to_max_output_size is - False. - scope: name scope. - - Returns: - A tuple of sorted_boxes and num_valid_nms_boxes. The sorted_boxes is a - BoxList holds M boxes with a rank-1 scores field representing - corresponding scores for each box with scores sorted in decreasing order - and a rank-1 classes field representing a class label for each box. The - num_valid_nms_boxes is a 0-D integer tensor representing the number of - valid elements in `BoxList`, with the valid elements appearing first. - - Raises: - ValueError: if iou_thresh is not in [0, 1] or if input boxlist does not have - a valid scores field or if non-zero soft_nms_sigma is provided when - pad_to_max_output_size is True. - """ - _validate_boxes_scores_iou_thresh(boxes, scores, iou_thresh, - change_coordinate_frame, clip_window) - if pad_to_max_output_size and soft_nms_sigma != 0.0: - raise ValueError('Soft NMS (soft_nms_sigma != 0.0) is currently not ' - 'supported when pad_to_max_output_size is True.') - - if max_classes_per_detection > 1: - raise ValueError('Max classes per detection box >1 not supported.') - q = shape_utils.get_dim_as_int(boxes.shape[1]) - if q > 1: - class_ids = tf.expand_dims( - tf.argmax(scores, axis=1, output_type=tf.int32), axis=1) - boxes = tf.batch_gather(boxes, class_ids) - if masks is not None: - masks = tf.batch_gather(masks, class_ids) - if boundaries is not None: - boundaries = tf.batch_gather(boundaries, class_ids) - boxes = tf.squeeze(boxes, axis=[1]) - if masks is not None: - masks = tf.squeeze(masks, axis=[1]) - if boundaries is not None: - boundaries = tf.squeeze(boundaries, axis=[1]) - - with tf.name_scope(scope, 'ClassAgnosticNonMaxSuppression'): - boxlist_and_class_scores = box_list.BoxList(boxes) - max_scores = tf.reduce_max(scores, axis=-1) - classes_with_max_scores = tf.argmax(scores, axis=-1) - boxlist_and_class_scores.add_field(fields.BoxListFields.scores, max_scores) - if masks is not None: - boxlist_and_class_scores.add_field(fields.BoxListFields.masks, masks) - if boundaries is not None: - boxlist_and_class_scores.add_field(fields.BoxListFields.boundaries, - boundaries) - - if additional_fields is not None: - for key, tensor in additional_fields.items(): - boxlist_and_class_scores.add_field(key, tensor) - - nms_result = None - selected_scores = None - if pad_to_max_output_size: - max_selection_size = max_total_size - if use_partitioned_nms: - (selected_indices, num_valid_nms_boxes, - boxlist_and_class_scores.data['boxes'], - boxlist_and_class_scores.data['scores'], - argsort_ids) = partitioned_non_max_suppression_padded( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field(fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh) - classes_with_max_scores = tf.gather(classes_with_max_scores, - argsort_ids) - else: - selected_indices, num_valid_nms_boxes = ( - tf.image.non_max_suppression_padded( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field(fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh, - pad_to_max_output_size=True)) - nms_result = box_list_ops.gather(boxlist_and_class_scores, - selected_indices) - selected_scores = nms_result.get_field(fields.BoxListFields.scores) - else: - max_selection_size = tf.minimum(max_total_size, - boxlist_and_class_scores.num_boxes()) - if (hasattr(tf.image, 'non_max_suppression_with_scores') and - tf.compat.forward_compatible(2019, 6, 6)): - (selected_indices, selected_scores - ) = tf.image.non_max_suppression_with_scores( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field(fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh, - soft_nms_sigma=soft_nms_sigma) - num_valid_nms_boxes = tf.shape(selected_indices)[0] - selected_indices = tf.concat([ - selected_indices, - tf.zeros(max_selection_size - num_valid_nms_boxes, tf.int32) - ], 0) - selected_scores = tf.concat( - [selected_scores, - tf.zeros(max_selection_size-num_valid_nms_boxes, tf.float32)], -1) - nms_result = box_list_ops.gather(boxlist_and_class_scores, - selected_indices) - else: - if soft_nms_sigma != 0: - raise ValueError('Soft NMS not supported in current TF version!') - selected_indices = tf.image.non_max_suppression( - boxlist_and_class_scores.get(), - boxlist_and_class_scores.get_field(fields.BoxListFields.scores), - max_selection_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh) - num_valid_nms_boxes = tf.shape(selected_indices)[0] - selected_indices = tf.concat( - [selected_indices, - tf.zeros(max_selection_size-num_valid_nms_boxes, tf.int32)], 0) - nms_result = box_list_ops.gather(boxlist_and_class_scores, - selected_indices) - selected_scores = nms_result.get_field(fields.BoxListFields.scores) - valid_nms_boxes_indices = tf.less( - tf.range(max_selection_size), num_valid_nms_boxes) - nms_result.add_field( - fields.BoxListFields.scores, - tf.where(valid_nms_boxes_indices, - selected_scores, -1*tf.ones(max_selection_size))) - - selected_classes = tf.gather(classes_with_max_scores, selected_indices) - selected_classes = tf.cast(selected_classes, tf.float32) - nms_result.add_field(fields.BoxListFields.classes, selected_classes) - selected_boxes = nms_result - sorted_boxes = box_list_ops.sort_by_field(selected_boxes, - fields.BoxListFields.scores) - - if clip_window is not None: - # When pad_to_max_output_size is False, it prunes the boxes with zero - # area. - sorted_boxes, num_valid_nms_boxes = _clip_window_prune_boxes( - sorted_boxes, clip_window, pad_to_max_output_size, - change_coordinate_frame) - - if max_total_size: - max_total_size = tf.minimum(max_total_size, sorted_boxes.num_boxes()) - sorted_boxes = box_list_ops.gather(sorted_boxes, tf.range(max_total_size)) - num_valid_nms_boxes = tf.where(max_total_size > num_valid_nms_boxes, - num_valid_nms_boxes, max_total_size) - # Select only the valid boxes if pad_to_max_output_size is False. - if not pad_to_max_output_size: - sorted_boxes = box_list_ops.gather(sorted_boxes, - tf.range(num_valid_nms_boxes)) - - return sorted_boxes, num_valid_nms_boxes - - -def batch_multiclass_non_max_suppression(boxes, - scores, - score_thresh, - iou_thresh, - max_size_per_class, - max_total_size=0, - clip_window=None, - change_coordinate_frame=False, - num_valid_boxes=None, - masks=None, - additional_fields=None, - soft_nms_sigma=0.0, - scope=None, - use_static_shapes=False, - use_partitioned_nms=False, - parallel_iterations=32, - use_class_agnostic_nms=False, - max_classes_per_detection=1, - use_dynamic_map_fn=False, - use_combined_nms=False, - use_hard_nms=False, - use_cpu_nms=False): - """Multi-class version of non maximum suppression that operates on a batch. - - This op is similar to `multiclass_non_max_suppression` but operates on a batch - of boxes and scores. See documentation for `multiclass_non_max_suppression` - for details. - - Args: - boxes: A [batch_size, num_anchors, q, 4] float32 tensor containing - detections. If `q` is 1 then same boxes are used for all classes - otherwise, if `q` is equal to number of classes, class-specific boxes are - used. - scores: A [batch_size, num_anchors, num_classes] float32 tensor containing - the scores for each of the `num_anchors` detections. The scores have to be - non-negative when use_static_shapes is set True. - score_thresh: scalar threshold for score (low scoring boxes are removed). - iou_thresh: scalar threshold for IOU (new boxes that have high IOU overlap - with previously selected boxes are removed). - max_size_per_class: maximum number of retained boxes per class. - max_total_size: maximum number of boxes retained over all classes. By - default returns all boxes retained after capping boxes per class. - clip_window: A float32 tensor of shape [batch_size, 4] where each entry is - of the form [y_min, x_min, y_max, x_max] representing the window to clip - boxes to before performing non-max suppression. This argument can also be - a tensor of shape [4] in which case, the same clip window is applied to - all images in the batch. If clip_widow is None, all boxes are used to - perform non-max suppression. - change_coordinate_frame: Whether to normalize coordinates after clipping - relative to clip_window (this can only be set to True if a clip_window is - provided) - num_valid_boxes: (optional) a Tensor of type `int32`. A 1-D tensor of shape - [batch_size] representing the number of valid boxes to be considered for - each image in the batch. This parameter allows for ignoring zero - paddings. - masks: (optional) a [batch_size, num_anchors, q, mask_height, mask_width] - float32 tensor containing box masks. `q` can be either number of classes - or 1 depending on whether a separate mask is predicted per class. - additional_fields: (optional) If not None, a dictionary that maps keys to - tensors whose dimensions are [batch_size, num_anchors, ...]. - soft_nms_sigma: A scalar float representing the Soft NMS sigma parameter; - See Bodla et al, https://arxiv.org/abs/1704.04503). When - `soft_nms_sigma=0.0` (which is default), we fall back to standard (hard) - NMS. Soft NMS is currently only supported when pad_to_max_output_size is - False. - scope: tf scope name. - use_static_shapes: If true, the output nmsed boxes are padded to be of - length `max_size_per_class` and it doesn't clip boxes to max_total_size. - Defaults to false. - use_partitioned_nms: If true, use partitioned version of - non_max_suppression. - parallel_iterations: (optional) number of batch items to process in - parallel. - use_class_agnostic_nms: If true, this uses class-agnostic non max - suppression - max_classes_per_detection: Maximum number of retained classes per detection - box in class-agnostic NMS. - use_dynamic_map_fn: If true, images in the batch will be processed within a - dynamic loop. Otherwise, a static loop will be used if possible. - use_combined_nms: If true, it uses tf.image.combined_non_max_suppression ( - multi-class version of NMS that operates on a batch). - It greedily selects a subset of detection bounding boxes, pruning away - boxes that have high IOU (intersection over union) overlap (> thresh) with - already selected boxes. It operates independently for each batch. - Within each batch, it operates independently for each class for which - scores are provided (via the scores field of the input box_list), - pruning boxes with score less than a provided threshold prior to applying - NMS. This operation is performed on *all* batches and *all* classes - in the batch, therefore any background classes should be removed prior to - calling this function. - Masks and additional fields are not supported. - See argument checks in the code below for unsupported arguments. - use_hard_nms: Enforce the usage of hard NMS. - use_cpu_nms: Enforce NMS to run on CPU. - - Returns: - 'nmsed_boxes': A [batch_size, max_detections, 4] float32 tensor - containing the non-max suppressed boxes. - 'nmsed_scores': A [batch_size, max_detections] float32 tensor containing - the scores for the boxes. - 'nmsed_classes': A [batch_size, max_detections] float32 tensor - containing the class for boxes. - 'nmsed_masks': (optional) a - [batch_size, max_detections, mask_height, mask_width] float32 tensor - containing masks for each selected box. This is set to None if input - `masks` is None. - 'nmsed_additional_fields': (optional) a dictionary of - [batch_size, max_detections, ...] float32 tensors corresponding to the - tensors specified in the input `additional_fields`. This is not returned - if input `additional_fields` is None. - 'num_detections': A [batch_size] int32 tensor indicating the number of - valid detections per batch item. Only the top num_detections[i] entries in - nms_boxes[i], nms_scores[i] and nms_class[i] are valid. The rest of the - entries are zero paddings. - - Raises: - ValueError: if `q` in boxes.shape is not 1 or not equal to number of - classes as inferred from scores.shape. - """ - if use_combined_nms: - if change_coordinate_frame: - raise ValueError( - 'change_coordinate_frame (normalizing coordinates' - ' relative to clip_window) is not supported by combined_nms.') - if num_valid_boxes is not None: - raise ValueError('num_valid_boxes is not supported by combined_nms.') - if masks is not None: - raise ValueError('masks is not supported by combined_nms.') - if soft_nms_sigma != 0.0: - raise ValueError('Soft NMS is not supported by combined_nms.') - if use_class_agnostic_nms: - raise ValueError('class-agnostic NMS is not supported by combined_nms.') - if clip_window is None: - tf.logging.warning( - 'A default clip window of [0. 0. 1. 1.] will be applied for the ' - 'boxes.') - if additional_fields is not None: - tf.logging.warning('additional_fields is not supported by combined_nms.') - if parallel_iterations != 32: - tf.logging.warning('Number of batch items to be processed in parallel is' - ' not configurable by combined_nms.') - if max_classes_per_detection > 1: - tf.logging.warning( - 'max_classes_per_detection is not configurable by combined_nms.') - - with tf.name_scope(scope, 'CombinedNonMaxSuppression'): - (batch_nmsed_boxes, batch_nmsed_scores, batch_nmsed_classes, - batch_num_detections) = tf.image.combined_non_max_suppression( - boxes=boxes, - scores=scores, - max_output_size_per_class=max_size_per_class, - max_total_size=max_total_size, - iou_threshold=iou_thresh, - score_threshold=score_thresh, - clip_boxes=(True if clip_window is None else False), - pad_per_class=use_static_shapes) - if clip_window is not None: - if clip_window.shape.ndims == 1: - boxes_shape = boxes.shape - batch_size = shape_utils.get_dim_as_int(boxes_shape[0]) - clip_window = tf.tile(clip_window[tf.newaxis, :], [batch_size, 1]) - batch_nmsed_boxes = _clip_boxes(batch_nmsed_boxes, clip_window) - # Not supported by combined_non_max_suppression. - batch_nmsed_masks = None - # Not supported by combined_non_max_suppression. - batch_nmsed_additional_fields = None - return (batch_nmsed_boxes, batch_nmsed_scores, batch_nmsed_classes, - batch_nmsed_masks, batch_nmsed_additional_fields, - batch_num_detections) - - q = shape_utils.get_dim_as_int(boxes.shape[2]) - num_classes = shape_utils.get_dim_as_int(scores.shape[2]) - if q != 1 and q != num_classes: - raise ValueError('third dimension of boxes must be either 1 or equal ' - 'to the third dimension of scores.') - if change_coordinate_frame and clip_window is None: - raise ValueError('if change_coordinate_frame is True, then a clip_window' - 'must be specified.') - original_masks = masks - - # Create ordered dictionary using the sorted keys from - # additional fields to ensure getting the same key value assignment - # in _single_image_nms_fn(). The dictionary is thus a sorted version of - # additional_fields. - if additional_fields is None: - ordered_additional_fields = collections.OrderedDict() - else: - ordered_additional_fields = collections.OrderedDict( - sorted(additional_fields.items(), key=lambda item: item[0])) - - with tf.name_scope(scope, 'BatchMultiClassNonMaxSuppression'): - boxes_shape = boxes.shape - batch_size = shape_utils.get_dim_as_int(boxes_shape[0]) - num_anchors = shape_utils.get_dim_as_int(boxes_shape[1]) - - if batch_size is None: - batch_size = tf.shape(boxes)[0] - if num_anchors is None: - num_anchors = tf.shape(boxes)[1] - - # If num valid boxes aren't provided, create one and mark all boxes as - # valid. - if num_valid_boxes is None: - num_valid_boxes = tf.ones([batch_size], dtype=tf.int32) * num_anchors - - # If masks aren't provided, create dummy masks so we can only have one copy - # of _single_image_nms_fn and discard the dummy masks after map_fn. - if masks is None: - masks_shape = tf.stack([batch_size, num_anchors, q, 1, 1]) - masks = tf.zeros(masks_shape) - - if clip_window is None: - clip_window = tf.stack([ - tf.reduce_min(boxes[:, :, :, 0]), - tf.reduce_min(boxes[:, :, :, 1]), - tf.reduce_max(boxes[:, :, :, 2]), - tf.reduce_max(boxes[:, :, :, 3]) - ]) - if clip_window.shape.ndims == 1: - clip_window = tf.tile(tf.expand_dims(clip_window, 0), [batch_size, 1]) - - def _single_image_nms_fn(args): - """Runs NMS on a single image and returns padded output. - - Args: - args: A list of tensors consisting of the following: - per_image_boxes - A [num_anchors, q, 4] float32 tensor containing - detections. If `q` is 1 then same boxes are used for all classes - otherwise, if `q` is equal to number of classes, class-specific - boxes are used. - per_image_scores - A [num_anchors, num_classes] float32 tensor - containing the scores for each of the `num_anchors` detections. - per_image_masks - A [num_anchors, q, mask_height, mask_width] float32 - tensor containing box masks. `q` can be either number of classes - or 1 depending on whether a separate mask is predicted per class. - per_image_clip_window - A 1D float32 tensor of the form - [ymin, xmin, ymax, xmax] representing the window to clip the boxes - to. - per_image_additional_fields - (optional) A variable number of float32 - tensors each with size [num_anchors, ...]. - per_image_num_valid_boxes - A tensor of type `int32`. A 1-D tensor of - shape [batch_size] representing the number of valid boxes to be - considered for each image in the batch. This parameter allows for - ignoring zero paddings. - - Returns: - 'nmsed_boxes': A [max_detections, 4] float32 tensor containing the - non-max suppressed boxes. - 'nmsed_scores': A [max_detections] float32 tensor containing the scores - for the boxes. - 'nmsed_classes': A [max_detections] float32 tensor containing the class - for boxes. - 'nmsed_masks': (optional) a [max_detections, mask_height, mask_width] - float32 tensor containing masks for each selected box. This is set to - None if input `masks` is None. - 'nmsed_additional_fields': (optional) A variable number of float32 - tensors each with size [max_detections, ...] corresponding to the - input `per_image_additional_fields`. - 'num_detections': A [batch_size] int32 tensor indicating the number of - valid detections per batch item. Only the top num_detections[i] - entries in nms_boxes[i], nms_scores[i] and nms_class[i] are valid. The - rest of the entries are zero paddings. - """ - per_image_boxes = args[0] - per_image_scores = args[1] - per_image_masks = args[2] - per_image_clip_window = args[3] - # Make sure that the order of elements passed in args is aligned with - # the iteration order of ordered_additional_fields - per_image_additional_fields = { - key: value - for key, value in zip(ordered_additional_fields, args[4:-1]) - } - per_image_num_valid_boxes = args[-1] - if use_static_shapes: - total_proposals = tf.shape(per_image_scores) - per_image_scores = tf.where( - tf.less(tf.range(total_proposals[0]), per_image_num_valid_boxes), - per_image_scores, - tf.fill(total_proposals, np.finfo('float32').min)) - else: - per_image_boxes = tf.reshape( - tf.slice(per_image_boxes, 3 * [0], - tf.stack([per_image_num_valid_boxes, -1, -1])), [-1, q, 4]) - per_image_scores = tf.reshape( - tf.slice(per_image_scores, [0, 0], - tf.stack([per_image_num_valid_boxes, -1])), - [-1, num_classes]) - per_image_masks = tf.reshape( - tf.slice(per_image_masks, 4 * [0], - tf.stack([per_image_num_valid_boxes, -1, -1, -1])), - [-1, q, shape_utils.get_dim_as_int(per_image_masks.shape[2]), - shape_utils.get_dim_as_int(per_image_masks.shape[3])]) - if per_image_additional_fields is not None: - for key, tensor in per_image_additional_fields.items(): - additional_field_shape = tensor.get_shape() - additional_field_dim = len(additional_field_shape) - per_image_additional_fields[key] = tf.reshape( - tf.slice( - per_image_additional_fields[key], - additional_field_dim * [0], - tf.stack([per_image_num_valid_boxes] + - (additional_field_dim - 1) * [-1])), [-1] + [ - shape_utils.get_dim_as_int(dim) - for dim in additional_field_shape[1:] - ]) - if use_class_agnostic_nms: - nmsed_boxlist, num_valid_nms_boxes = class_agnostic_non_max_suppression( - per_image_boxes, - per_image_scores, - score_thresh, - iou_thresh, - max_classes_per_detection, - max_total_size, - clip_window=per_image_clip_window, - change_coordinate_frame=change_coordinate_frame, - masks=per_image_masks, - pad_to_max_output_size=use_static_shapes, - use_partitioned_nms=use_partitioned_nms, - additional_fields=per_image_additional_fields, - soft_nms_sigma=soft_nms_sigma) - else: - nmsed_boxlist, num_valid_nms_boxes = multiclass_non_max_suppression( - per_image_boxes, - per_image_scores, - score_thresh, - iou_thresh, - max_size_per_class, - max_total_size, - clip_window=per_image_clip_window, - change_coordinate_frame=change_coordinate_frame, - masks=per_image_masks, - pad_to_max_output_size=use_static_shapes, - use_partitioned_nms=use_partitioned_nms, - additional_fields=per_image_additional_fields, - soft_nms_sigma=soft_nms_sigma, - use_hard_nms=use_hard_nms, - use_cpu_nms=use_cpu_nms) - - if not use_static_shapes: - nmsed_boxlist = box_list_ops.pad_or_clip_box_list( - nmsed_boxlist, max_total_size) - num_detections = num_valid_nms_boxes - nmsed_boxes = nmsed_boxlist.get() - nmsed_scores = nmsed_boxlist.get_field(fields.BoxListFields.scores) - nmsed_classes = nmsed_boxlist.get_field(fields.BoxListFields.classes) - nmsed_masks = nmsed_boxlist.get_field(fields.BoxListFields.masks) - nmsed_additional_fields = [] - # Sorting is needed here to ensure that the values stored in - # nmsed_additional_fields are always kept in the same order - # across different execution runs. - for key in sorted(per_image_additional_fields.keys()): - nmsed_additional_fields.append(nmsed_boxlist.get_field(key)) - return ([nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks] + - nmsed_additional_fields + [num_detections]) - - num_additional_fields = 0 - if ordered_additional_fields: - num_additional_fields = len(ordered_additional_fields) - num_nmsed_outputs = 4 + num_additional_fields - - if use_dynamic_map_fn: - map_fn = tf.map_fn - else: - map_fn = shape_utils.static_or_dynamic_map_fn - - batch_outputs = map_fn( - _single_image_nms_fn, - elems=([boxes, scores, masks, clip_window] + - list(ordered_additional_fields.values()) + [num_valid_boxes]), - dtype=(num_nmsed_outputs * [tf.float32] + [tf.int32]), - parallel_iterations=parallel_iterations) - - batch_nmsed_boxes = batch_outputs[0] - batch_nmsed_scores = batch_outputs[1] - batch_nmsed_classes = batch_outputs[2] - batch_nmsed_masks = batch_outputs[3] - batch_nmsed_values = batch_outputs[4:-1] - - batch_nmsed_additional_fields = {} - if num_additional_fields > 0: - # Sort the keys to ensure arranging elements in same order as - # in _single_image_nms_fn. - batch_nmsed_keys = list(ordered_additional_fields.keys()) - for i in range(len(batch_nmsed_keys)): - batch_nmsed_additional_fields[ - batch_nmsed_keys[i]] = batch_nmsed_values[i] - - batch_num_detections = batch_outputs[-1] - - if original_masks is None: - batch_nmsed_masks = None - - if not ordered_additional_fields: - batch_nmsed_additional_fields = None - - return (batch_nmsed_boxes, batch_nmsed_scores, batch_nmsed_classes, - batch_nmsed_masks, batch_nmsed_additional_fields, - batch_num_detections) diff --git a/research/object_detection/core/prefetcher.py b/research/object_detection/core/prefetcher.py deleted file mode 100644 index 31e93eae80e..00000000000 --- a/research/object_detection/core/prefetcher.py +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Provides functions to prefetch tensors to feed into models.""" -import tensorflow.compat.v1 as tf - - -def prefetch(tensor_dict, capacity): - """Creates a prefetch queue for tensors. - - Creates a FIFO queue to asynchronously enqueue tensor_dicts and returns a - dequeue op that evaluates to a tensor_dict. This function is useful in - prefetching preprocessed tensors so that the data is readily available for - consumers. - - Example input pipeline when you don't need batching: - ---------------------------------------------------- - key, string_tensor = slim.parallel_reader.parallel_read(...) - tensor_dict = decoder.decode(string_tensor) - tensor_dict = preprocessor.preprocess(tensor_dict, ...) - prefetch_queue = prefetcher.prefetch(tensor_dict, capacity=20) - tensor_dict = prefetch_queue.dequeue() - outputs = Model(tensor_dict) - ... - ---------------------------------------------------- - - For input pipelines with batching, refer to core/batcher.py - - Args: - tensor_dict: a dictionary of tensors to prefetch. - capacity: the size of the prefetch queue. - - Returns: - a FIFO prefetcher queue - """ - names = list(tensor_dict.keys()) - dtypes = [t.dtype for t in tensor_dict.values()] - shapes = [t.get_shape() for t in tensor_dict.values()] - prefetch_queue = tf.PaddingFIFOQueue(capacity, dtypes=dtypes, - shapes=shapes, - names=names, - name='prefetch_queue') - enqueue_op = prefetch_queue.enqueue(tensor_dict) - tf.train.queue_runner.add_queue_runner(tf.train.queue_runner.QueueRunner( - prefetch_queue, [enqueue_op])) - tf.summary.scalar( - 'queue/%s/fraction_of_%d_full' % (prefetch_queue.name, capacity), - tf.cast(prefetch_queue.size(), dtype=tf.float32) * (1. / capacity)) - return prefetch_queue diff --git a/research/object_detection/core/prefetcher_tf1_test.py b/research/object_detection/core/prefetcher_tf1_test.py deleted file mode 100644 index 95e9155e5e3..00000000000 --- a/research/object_detection/core/prefetcher_tf1_test.py +++ /dev/null @@ -1,109 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.prefetcher.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.core import prefetcher -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class PrefetcherTest(tf.test.TestCase): - """Test class for prefetcher.""" - - def test_prefetch_tensors_with_fully_defined_shapes(self): - with self.test_session() as sess: - batch_size = 10 - image_size = 32 - num_batches = 5 - examples = tf.Variable(tf.constant(0, dtype=tf.int64)) - counter = examples.count_up_to(num_batches) - image = tf.random_normal([batch_size, image_size, - image_size, 3], - dtype=tf.float32, - name='images') - label = tf.random_uniform([batch_size, 1], 0, 10, - dtype=tf.int32, name='labels') - - prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter, - 'image': image, - 'label': label}, - capacity=100) - tensor_dict = prefetch_queue.dequeue() - - self.assertAllEqual(tensor_dict['image'].get_shape().as_list(), - [batch_size, image_size, image_size, 3]) - self.assertAllEqual(tensor_dict['label'].get_shape().as_list(), - [batch_size, 1]) - - tf.initialize_all_variables().run() - with slim.queues.QueueRunners(sess): - for _ in range(num_batches): - results = sess.run(tensor_dict) - self.assertEquals(results['image'].shape, - (batch_size, image_size, image_size, 3)) - self.assertEquals(results['label'].shape, (batch_size, 1)) - with self.assertRaises(tf.errors.OutOfRangeError): - sess.run(tensor_dict) - - def test_prefetch_tensors_with_partially_defined_shapes(self): - with self.test_session() as sess: - batch_size = 10 - image_size = 32 - num_batches = 5 - examples = tf.Variable(tf.constant(0, dtype=tf.int64)) - counter = examples.count_up_to(num_batches) - image = tf.random_normal([batch_size, - tf.Variable(image_size), - tf.Variable(image_size), 3], - dtype=tf.float32, - name='image') - image.set_shape([batch_size, None, None, 3]) - label = tf.random_uniform([batch_size, tf.Variable(1)], 0, - 10, dtype=tf.int32, name='label') - label.set_shape([batch_size, None]) - - prefetch_queue = prefetcher.prefetch(tensor_dict={'counter': counter, - 'image': image, - 'label': label}, - capacity=100) - tensor_dict = prefetch_queue.dequeue() - - self.assertAllEqual(tensor_dict['image'].get_shape().as_list(), - [batch_size, None, None, 3]) - self.assertAllEqual(tensor_dict['label'].get_shape().as_list(), - [batch_size, None]) - - tf.initialize_all_variables().run() - with slim.queues.QueueRunners(sess): - for _ in range(num_batches): - results = sess.run(tensor_dict) - self.assertEquals(results['image'].shape, - (batch_size, image_size, image_size, 3)) - self.assertEquals(results['label'].shape, (batch_size, 1)) - with self.assertRaises(tf.errors.OutOfRangeError): - sess.run(tensor_dict) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/preprocessor.py b/research/object_detection/core/preprocessor.py deleted file mode 100644 index 9e1bc05c054..00000000000 --- a/research/object_detection/core/preprocessor.py +++ /dev/null @@ -1,4768 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Preprocess images and bounding boxes for detection. - -We perform two sets of operations in preprocessing stage: -(a) operations that are applied to both training and testing data, -(b) operations that are applied only to training data for the purpose of - data augmentation. - -A preprocessing function receives a set of inputs, -e.g. an image and bounding boxes, -performs an operation on them, and returns them. -Some examples are: randomly cropping the image, randomly mirroring the image, - randomly changing the brightness, contrast, hue and - randomly jittering the bounding boxes. - -The preprocess function receives a tensor_dict which is a dictionary that maps -different field names to their tensors. For example, -tensor_dict[fields.InputDataFields.image] holds the image tensor. -The image is a rank 4 tensor: [1, height, width, channels] with -dtype=tf.float32. The groundtruth_boxes is a rank 2 tensor: [N, 4] where -in each row there is a box with [ymin xmin ymax xmax]. -Boxes are in normalized coordinates meaning -their coordinate values range in [0, 1] - -To preprocess multiple images with the same operations in cases where -nondeterministic operations are used, a preprocessor_cache.PreprocessorCache -object can be passed into the preprocess function or individual operations. -All nondeterministic operations except random_jitter_boxes support caching. -E.g. -Let tensor_dict{1,2,3,4,5} be copies of the same inputs. -Let preprocess_options contain nondeterministic operation(s) excluding -random_jitter_boxes. - -cache1 = preprocessor_cache.PreprocessorCache() -cache2 = preprocessor_cache.PreprocessorCache() -a = preprocess(tensor_dict1, preprocess_options, preprocess_vars_cache=cache1) -b = preprocess(tensor_dict2, preprocess_options, preprocess_vars_cache=cache1) -c = preprocess(tensor_dict3, preprocess_options, preprocess_vars_cache=cache2) -d = preprocess(tensor_dict4, preprocess_options, preprocess_vars_cache=cache2) -e = preprocess(tensor_dict5, preprocess_options) - -Then correspondings tensors of object pairs (a,b) and (c,d) -are guaranteed to be equal element-wise, but the equality of any other object -pair cannot be determined. - -Important Note: In tensor_dict, images is a rank 4 tensor, but preprocessing -functions receive a rank 3 tensor for processing the image. Thus, inside the -preprocess function we squeeze the image to become a rank 3 tensor and then -we pass it to the functions. At the end of the preprocess we expand the image -back to rank 4. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -import inspect -import math -import sys - -import six -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf - -from tensorflow.python.ops import control_flow_ops -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import densepose_ops -from object_detection.core import keypoint_ops -from object_detection.core import preprocessor_cache -from object_detection.core import standard_fields as fields -from object_detection.utils import autoaugment_utils -from object_detection.utils import ops -from object_detection.utils import patch_ops -from object_detection.utils import shape_utils - - -def _apply_with_random_selector(x, - func, - num_cases, - preprocess_vars_cache=None, - key=''): - """Computes func(x, sel), with sel sampled from [0...num_cases-1]. - - If both preprocess_vars_cache AND key are the same between two calls, sel will - be the same value in both calls. - - Args: - x: input Tensor. - func: Python function to apply. - num_cases: Python int32, number of cases to sample sel from. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - key: variable identifier for preprocess_vars_cache. - - Returns: - The result of func(x, sel), where func receives the value of the - selector as a python integer, but sel is sampled dynamically. - """ - generator_func = functools.partial( - tf.random_uniform, [], maxval=num_cases, dtype=tf.int32) - rand_sel = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.SELECTOR, - preprocess_vars_cache, key) - - # Pass the real x only to one of the func calls. - return control_flow_ops.merge([func( - control_flow_ops.switch(x, tf.equal(rand_sel, case))[1], case) - for case in range(num_cases)])[0] - - -def _apply_with_random_selector_tuples(x, - func, - num_cases, - preprocess_vars_cache=None, - key=''): - """Computes func(x, sel), with sel sampled from [0...num_cases-1]. - - If both preprocess_vars_cache AND key are the same between two calls, sel will - be the same value in both calls. - - Args: - x: A tuple of input tensors. - func: Python function to apply. - num_cases: Python int32, number of cases to sample sel from. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - key: variable identifier for preprocess_vars_cache. - - Returns: - The result of func(x, sel), where func receives the value of the - selector as a python integer, but sel is sampled dynamically. - """ - num_inputs = len(x) - generator_func = functools.partial( - tf.random_uniform, [], maxval=num_cases, dtype=tf.int32) - rand_sel = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.SELECTOR_TUPLES, - preprocess_vars_cache, key) - - # Pass the real x only to one of the func calls. - tuples = [list() for t in x] - for case in range(num_cases): - new_x = [control_flow_ops.switch(t, tf.equal(rand_sel, case))[1] for t in x] - output = func(tuple(new_x), case) - for j in range(num_inputs): - tuples[j].append(output[j]) - - for i in range(num_inputs): - tuples[i] = control_flow_ops.merge(tuples[i])[0] - return tuple(tuples) - - -def _get_or_create_preprocess_rand_vars(generator_func, - function_id, - preprocess_vars_cache, - key=''): - """Returns a tensor stored in preprocess_vars_cache or using generator_func. - - If the tensor was previously generated and appears in the PreprocessorCache, - the previously generated tensor will be returned. Otherwise, a new tensor - is generated using generator_func and stored in the cache. - - Args: - generator_func: A 0-argument function that generates a tensor. - function_id: identifier for the preprocessing function used. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - key: identifier for the variable stored. - Returns: - The generated tensor. - """ - if preprocess_vars_cache is not None: - var = preprocess_vars_cache.get(function_id, key) - if var is None: - var = generator_func() - preprocess_vars_cache.update(function_id, key, var) - else: - var = generator_func() - return var - - -def _random_integer(minval, maxval, seed): - """Returns a random 0-D tensor between minval and maxval. - - Args: - minval: minimum value of the random tensor. - maxval: maximum value of the random tensor. - seed: random seed. - - Returns: - A random 0-D tensor between minval and maxval. - """ - return tf.random_uniform( - [], minval=minval, maxval=maxval, dtype=tf.int32, seed=seed) - - -# TODO(mttang): This method is needed because the current -# tf.image.rgb_to_grayscale method does not support quantization. Replace with -# tf.image.rgb_to_grayscale after quantization support is added. -def _rgb_to_grayscale(images, name=None): - """Converts one or more images from RGB to Grayscale. - - Outputs a tensor of the same `DType` and rank as `images`. The size of the - last dimension of the output is 1, containing the Grayscale value of the - pixels. - - Args: - images: The RGB tensor to convert. Last dimension must have size 3 and - should contain RGB values. - name: A name for the operation (optional). - - Returns: - The converted grayscale image(s). - """ - with tf.name_scope(name, 'rgb_to_grayscale', [images]) as name: - images = tf.convert_to_tensor(images, name='images') - # Remember original dtype to so we can convert back if needed - orig_dtype = images.dtype - flt_image = tf.image.convert_image_dtype(images, tf.float32) - - # Reference for converting between RGB and grayscale. - # https://en.wikipedia.org/wiki/Luma_%28video%29 - rgb_weights = [0.2989, 0.5870, 0.1140] - rank_1 = tf.expand_dims(tf.rank(images) - 1, 0) - gray_float = tf.reduce_sum( - flt_image * rgb_weights, rank_1, keep_dims=True) - gray_float.set_shape(images.get_shape()[:-1].concatenate([1])) - return tf.image.convert_image_dtype(gray_float, orig_dtype, name=name) - - -def normalize_image(image, original_minval, original_maxval, target_minval, - target_maxval): - """Normalizes pixel values in the image. - - Moves the pixel values from the current [original_minval, original_maxval] - range to a the [target_minval, target_maxval] range. - - Args: - image: rank 3 float32 tensor containing 1 - image -> [height, width, channels]. - original_minval: current image minimum value. - original_maxval: current image maximum value. - target_minval: target image minimum value. - target_maxval: target image maximum value. - - Returns: - image: image which is the same shape as input image. - """ - with tf.name_scope('NormalizeImage', values=[image]): - original_minval = float(original_minval) - original_maxval = float(original_maxval) - target_minval = float(target_minval) - target_maxval = float(target_maxval) - image = tf.cast(image, dtype=tf.float32) - image = tf.subtract(image, original_minval) - image = tf.multiply(image, (target_maxval - target_minval) / - (original_maxval - original_minval)) - image = tf.add(image, target_minval) - return image - - -def retain_boxes_above_threshold(boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - keypoints=None, - threshold=0.0): - """Retains boxes whose label weight is above a given threshold. - - If the label weight for a box is missing (represented by NaN), the box is - retained. The boxes that don't pass the threshold will not appear in the - returned tensor. - - Args: - boxes: float32 tensor of shape [num_instance, 4] representing boxes - location in normalized coordinates. - labels: rank 1 int32 tensor of shape [num_instance] containing the object - classes. - label_weights: float32 tensor of shape [num_instance] representing the - weight for each box. - label_confidences: float32 tensor of shape [num_instance] representing the - confidence for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks are of - the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x normalized - coordinates. - threshold: scalar python float. - - Returns: - retained_boxes: [num_retained_instance, 4] - retianed_labels: [num_retained_instance] - retained_label_weights: [num_retained_instance] - - If multiclass_scores, masks, or keypoints are not None, the function also - returns: - - retained_multiclass_scores: [num_retained_instance, num_classes] - retained_masks: [num_retained_instance, height, width] - retained_keypoints: [num_retained_instance, num_keypoints, 2] - """ - with tf.name_scope('RetainBoxesAboveThreshold', - values=[boxes, labels, label_weights]): - indices = tf.where( - tf.logical_or(label_weights > threshold, tf.is_nan(label_weights))) - indices = tf.squeeze(indices, axis=1) - retained_boxes = tf.gather(boxes, indices) - retained_labels = tf.gather(labels, indices) - retained_label_weights = tf.gather(label_weights, indices) - result = [retained_boxes, retained_labels, retained_label_weights] - - if label_confidences is not None: - retained_label_confidences = tf.gather(label_confidences, indices) - result.append(retained_label_confidences) - - if multiclass_scores is not None: - retained_multiclass_scores = tf.gather(multiclass_scores, indices) - result.append(retained_multiclass_scores) - - if masks is not None: - retained_masks = tf.gather(masks, indices) - result.append(retained_masks) - - if keypoints is not None: - retained_keypoints = tf.gather(keypoints, indices) - result.append(retained_keypoints) - - return result - - -def drop_label_probabilistically(boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - keypoints=None, - dropped_label=None, - drop_probability=0.0, - seed=None): - """Drops boxes of a certain label with probability drop_probability. - - Boxes of the label dropped_label will not appear in the returned tensor. - - Args: - boxes: float32 tensor of shape [num_instance, 4] representing boxes - location in normalized coordinates. - labels: rank 1 int32 tensor of shape [num_instance] containing the object - classes. - label_weights: float32 tensor of shape [num_instance] representing the - weight for each box. - label_confidences: float32 tensor of shape [num_instance] representing the - confidence for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks are of - the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x normalized - coordinates. - dropped_label: int32 id of label to drop. - drop_probability: float32 probability of dropping a label. - seed: random seed. - - Returns: - retained_boxes: [num_retained_instance, 4] - retianed_labels: [num_retained_instance] - retained_label_weights: [num_retained_instance] - - If multiclass_scores, masks, or keypoints are not None, the function also - returns: - - retained_multiclass_scores: [num_retained_instance, num_classes] - retained_masks: [num_retained_instance, height, width] - retained_keypoints: [num_retained_instance, num_keypoints, 2] - """ - with tf.name_scope('DropLabelProbabilistically', - values=[boxes, labels]): - indices = tf.where( - tf.logical_or( - tf.random_uniform(tf.shape(labels), seed=seed) > drop_probability, - tf.not_equal(labels, dropped_label))) - indices = tf.squeeze(indices, axis=1) - - retained_boxes = tf.gather(boxes, indices) - retained_labels = tf.gather(labels, indices) - retained_label_weights = tf.gather(label_weights, indices) - result = [retained_boxes, retained_labels, retained_label_weights] - - if label_confidences is not None: - retained_label_confidences = tf.gather(label_confidences, indices) - result.append(retained_label_confidences) - - if multiclass_scores is not None: - retained_multiclass_scores = tf.gather(multiclass_scores, indices) - result.append(retained_multiclass_scores) - - if masks is not None: - retained_masks = tf.gather(masks, indices) - result.append(retained_masks) - - if keypoints is not None: - retained_keypoints = tf.gather(keypoints, indices) - result.append(retained_keypoints) - - return result - - -def remap_labels(labels, - original_labels=None, - new_label=None): - """Remaps labels that have an id in original_labels to new_label. - - Args: - labels: rank 1 int32 tensor of shape [num_instance] containing the object - classes. - original_labels: int list of original labels that should be mapped from. - new_label: int label to map to - Returns: - Remapped labels - """ - new_labels = labels - for original_label in original_labels: - change = tf.where( - tf.equal(new_labels, original_label), - tf.add(tf.zeros_like(new_labels), new_label - original_label), - tf.zeros_like(new_labels)) - new_labels = tf.add( - new_labels, - change) - new_labels = tf.reshape(new_labels, tf.shape(labels)) - return new_labels - - -def _flip_boxes_left_right(boxes): - """Left-right flip the boxes. - - Args: - boxes: Float32 tensor containing the bounding boxes -> [..., 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each last dimension is in the form of [ymin, xmin, ymax, xmax]. - - Returns: - Flipped boxes. - """ - ymin, xmin, ymax, xmax = tf.split(value=boxes, num_or_size_splits=4, axis=-1) - flipped_xmin = tf.subtract(1.0, xmax) - flipped_xmax = tf.subtract(1.0, xmin) - flipped_boxes = tf.concat([ymin, flipped_xmin, ymax, flipped_xmax], axis=-1) - return flipped_boxes - - -def _flip_boxes_up_down(boxes): - """Up-down flip the boxes. - - Args: - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - - Returns: - Flipped boxes. - """ - ymin, xmin, ymax, xmax = tf.split(value=boxes, num_or_size_splits=4, axis=1) - flipped_ymin = tf.subtract(1.0, ymax) - flipped_ymax = tf.subtract(1.0, ymin) - flipped_boxes = tf.concat([flipped_ymin, xmin, flipped_ymax, xmax], 1) - return flipped_boxes - - -def _rot90_boxes(boxes): - """Rotate boxes counter-clockwise by 90 degrees. - - Args: - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - - Returns: - Rotated boxes. - """ - ymin, xmin, ymax, xmax = tf.split(value=boxes, num_or_size_splits=4, axis=1) - rotated_ymin = tf.subtract(1.0, xmax) - rotated_ymax = tf.subtract(1.0, xmin) - rotated_xmin = ymin - rotated_xmax = ymax - rotated_boxes = tf.concat( - [rotated_ymin, rotated_xmin, rotated_ymax, rotated_xmax], 1) - return rotated_boxes - - -def _flip_masks_left_right(masks): - """Left-right flip masks. - - Args: - masks: rank 3 float32 tensor with shape - [num_instances, height, width] representing instance masks. - - Returns: - flipped masks: rank 3 float32 tensor with shape - [num_instances, height, width] representing instance masks. - """ - return masks[:, :, ::-1] - - -def _flip_masks_up_down(masks): - """Up-down flip masks. - - Args: - masks: rank 3 float32 tensor with shape - [num_instances, height, width] representing instance masks. - - Returns: - flipped masks: rank 3 float32 tensor with shape - [num_instances, height, width] representing instance masks. - """ - return masks[:, ::-1, :] - - -def _rot90_masks(masks): - """Rotate masks counter-clockwise by 90 degrees. - - Args: - masks: rank 3 float32 tensor with shape - [num_instances, height, width] representing instance masks. - - Returns: - rotated masks: rank 3 float32 tensor with shape - [num_instances, height, width] representing instance masks. - """ - masks = tf.transpose(masks, [0, 2, 1]) - return masks[:, ::-1, :] - - -def random_horizontal_flip(image, - boxes=None, - masks=None, - keypoints=None, - keypoint_visibilities=None, - densepose_part_ids=None, - densepose_surface_coords=None, - keypoint_depths=None, - keypoint_depth_weights=None, - keypoint_flip_permutation=None, - probability=0.5, - seed=None, - preprocess_vars_cache=None): - """Randomly flips the image and detections horizontally. - - Args: - image: rank 3 float32 tensor with shape [height, width, channels]. - boxes: (optional) rank 2 float32 tensor with shape [N, 4] - containing the bounding boxes. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - keypoint_visibilities: (optional) rank 2 bool tensor with shape - [num_instances, num_keypoints]. - densepose_part_ids: (optional) rank 2 int32 tensor with shape - [num_instances, num_points] holding the part id for each - sampled point. These part_ids are 0-indexed, where the - first non-background part has index 0. - densepose_surface_coords: (optional) rank 3 float32 tensor with shape - [num_instances, num_points, 4]. The DensePose - coordinates are of the form (y, x, v, u) where - (y, x) are the normalized image coordinates for a - sampled point, and (v, u) is the surface - coordinate for the part. - keypoint_depths: (optional) rank 2 float32 tensor with shape [num_instances, - num_keypoints] representing the relative depth of the - keypoints. - keypoint_depth_weights: (optional) rank 2 float32 tensor with shape - [num_instances, num_keypoints] representing the - weights of the relative depth of the keypoints. - keypoint_flip_permutation: rank 1 int32 tensor containing the keypoint flip - permutation. - probability: the probability of performing this augmentation. - seed: random seed - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - - If boxes, masks, keypoints, keypoint_visibilities, - keypoint_flip_permutation, densepose_part_ids, or densepose_surface_coords - are not None,the function also returns the following tensors. - - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - keypoint_visibilities: rank 2 bool tensor with shape - [num_instances, num_keypoints]. - densepose_part_ids: rank 2 int32 tensor with shape - [num_instances, num_points]. - densepose_surface_coords: rank 3 float32 tensor with shape - [num_instances, num_points, 4]. - keypoint_depths: rank 2 float32 tensor with shape [num_instances, - num_keypoints] - keypoint_depth_weights: rank 2 float32 tensor with shape [num_instances, - num_keypoints]. - - Raises: - ValueError: if keypoints are provided but keypoint_flip_permutation is not. - ValueError: if either densepose_part_ids or densepose_surface_coords is - not None, but both are not None. - """ - - def _flip_image(image): - # flip image - image_flipped = tf.image.flip_left_right(image) - return image_flipped - - if keypoints is not None and keypoint_flip_permutation is None: - raise ValueError( - 'keypoints are provided but keypoints_flip_permutation is not provided') - - if ((densepose_part_ids is not None and densepose_surface_coords is None) or - (densepose_part_ids is None and densepose_surface_coords is not None)): - raise ValueError( - 'Must provide both `densepose_part_ids` and `densepose_surface_coords`') - - with tf.name_scope('RandomHorizontalFlip', values=[image, boxes]): - result = [] - # random variable defining whether to do flip or not - generator_func = functools.partial(tf.random_uniform, [], seed=seed) - do_a_flip_random = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.HORIZONTAL_FLIP, - preprocess_vars_cache) - do_a_flip_random = tf.less(do_a_flip_random, probability) - - # flip image - image = tf.cond(do_a_flip_random, lambda: _flip_image(image), lambda: image) - result.append(image) - - # flip boxes - if boxes is not None: - boxes = tf.cond(do_a_flip_random, lambda: _flip_boxes_left_right(boxes), - lambda: boxes) - result.append(boxes) - - # flip masks - if masks is not None: - masks = tf.cond(do_a_flip_random, lambda: _flip_masks_left_right(masks), - lambda: masks) - result.append(masks) - - # flip keypoints - if keypoints is not None and keypoint_flip_permutation is not None: - permutation = keypoint_flip_permutation - keypoints = tf.cond( - do_a_flip_random, - lambda: keypoint_ops.flip_horizontal(keypoints, 0.5, permutation), - lambda: keypoints) - result.append(keypoints) - - # flip keypoint visibilities - if (keypoint_visibilities is not None and - keypoint_flip_permutation is not None): - kpt_flip_perm = keypoint_flip_permutation - keypoint_visibilities = tf.cond( - do_a_flip_random, - lambda: tf.gather(keypoint_visibilities, kpt_flip_perm, axis=1), - lambda: keypoint_visibilities) - result.append(keypoint_visibilities) - - # flip DensePose parts and coordinates - if densepose_part_ids is not None: - flip_densepose_fn = functools.partial( - densepose_ops.flip_horizontal, densepose_part_ids, - densepose_surface_coords) - densepose_tensors = tf.cond( - do_a_flip_random, - flip_densepose_fn, - lambda: (densepose_part_ids, densepose_surface_coords)) - result.extend(densepose_tensors) - - # flip keypoint depths and weights. - if (keypoint_depths is not None and - keypoint_flip_permutation is not None): - kpt_flip_perm = keypoint_flip_permutation - keypoint_depths = tf.cond( - do_a_flip_random, - lambda: tf.gather(keypoint_depths, kpt_flip_perm, axis=1), - lambda: keypoint_depths) - keypoint_depth_weights = tf.cond( - do_a_flip_random, - lambda: tf.gather(keypoint_depth_weights, kpt_flip_perm, axis=1), - lambda: keypoint_depth_weights) - result.append(keypoint_depths) - result.append(keypoint_depth_weights) - - return tuple(result) - - -def random_vertical_flip(image, - boxes=None, - masks=None, - keypoints=None, - keypoint_flip_permutation=None, - probability=0.5, - seed=None, - preprocess_vars_cache=None): - """Randomly flips the image and detections vertically. - - The probability of flipping the image is 50%. - - Args: - image: rank 3 float32 tensor with shape [height, width, channels]. - boxes: (optional) rank 2 float32 tensor with shape [N, 4] - containing the bounding boxes. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - keypoint_flip_permutation: rank 1 int32 tensor containing the keypoint flip - permutation. - probability: the probability of performing this augmentation. - seed: random seed - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - - If boxes, masks, keypoints, and keypoint_flip_permutation are not None, - the function also returns the following tensors. - - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - - Raises: - ValueError: if keypoints are provided but keypoint_flip_permutation is not. - """ - - def _flip_image(image): - # flip image - image_flipped = tf.image.flip_up_down(image) - return image_flipped - - if keypoints is not None and keypoint_flip_permutation is None: - raise ValueError( - 'keypoints are provided but keypoints_flip_permutation is not provided') - - with tf.name_scope('RandomVerticalFlip', values=[image, boxes]): - result = [] - # random variable defining whether to do flip or not - generator_func = functools.partial(tf.random_uniform, [], seed=seed) - do_a_flip_random = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.VERTICAL_FLIP, - preprocess_vars_cache) - do_a_flip_random = tf.less(do_a_flip_random, probability) - - # flip image - image = tf.cond(do_a_flip_random, lambda: _flip_image(image), lambda: image) - result.append(image) - - # flip boxes - if boxes is not None: - boxes = tf.cond(do_a_flip_random, lambda: _flip_boxes_up_down(boxes), - lambda: boxes) - result.append(boxes) - - # flip masks - if masks is not None: - masks = tf.cond(do_a_flip_random, lambda: _flip_masks_up_down(masks), - lambda: masks) - result.append(masks) - - # flip keypoints - if keypoints is not None and keypoint_flip_permutation is not None: - permutation = keypoint_flip_permutation - keypoints = tf.cond( - do_a_flip_random, - lambda: keypoint_ops.flip_vertical(keypoints, 0.5, permutation), - lambda: keypoints) - result.append(keypoints) - - return tuple(result) - - -def random_rotation90(image, - boxes=None, - masks=None, - keypoints=None, - keypoint_rot_permutation=None, - probability=0.5, - seed=None, - preprocess_vars_cache=None): - """Randomly rotates the image and detections 90 degrees counter-clockwise. - - The probability of rotating the image is 50%. This can be combined with - random_horizontal_flip and random_vertical_flip to produce an output with a - uniform distribution of the eight possible 90 degree rotation / reflection - combinations. - - Args: - image: rank 3 float32 tensor with shape [height, width, channels]. - boxes: (optional) rank 2 float32 tensor with shape [N, 4] - containing the bounding boxes. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - keypoint_rot_permutation: rank 1 int32 tensor containing the keypoint flip - permutation. - probability: the probability of performing this augmentation. - seed: random seed - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - - If boxes, masks, and keypoints, are not None, - the function also returns the following tensors. - - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - """ - - def _rot90_image(image): - # flip image - image_rotated = tf.image.rot90(image) - return image_rotated - - with tf.name_scope('RandomRotation90', values=[image, boxes]): - result = [] - - # random variable defining whether to rotate by 90 degrees or not - generator_func = functools.partial(tf.random_uniform, [], seed=seed) - do_a_rot90_random = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.ROTATION90, - preprocess_vars_cache) - do_a_rot90_random = tf.less(do_a_rot90_random, probability) - - # flip image - image = tf.cond(do_a_rot90_random, lambda: _rot90_image(image), - lambda: image) - result.append(image) - - # flip boxes - if boxes is not None: - boxes = tf.cond(do_a_rot90_random, lambda: _rot90_boxes(boxes), - lambda: boxes) - result.append(boxes) - - # flip masks - if masks is not None: - masks = tf.cond(do_a_rot90_random, lambda: _rot90_masks(masks), - lambda: masks) - result.append(masks) - - # flip keypoints - if keypoints is not None: - keypoints = tf.cond( - do_a_rot90_random, - lambda: keypoint_ops.rot90(keypoints, keypoint_rot_permutation), - lambda: keypoints) - result.append(keypoints) - - return tuple(result) - - -def random_pixel_value_scale(image, - minval=0.9, - maxval=1.1, - seed=None, - preprocess_vars_cache=None): - """Scales each value in the pixels of the image. - - This function scales each pixel independent of the other ones. - For each value in image tensor, draws a random number between - minval and maxval and multiples the values with them. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - minval: lower ratio of scaling pixel values. - maxval: upper ratio of scaling pixel values. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - """ - with tf.name_scope('RandomPixelValueScale', values=[image]): - generator_func = functools.partial( - tf.random_uniform, tf.shape(image), - minval=minval, maxval=maxval, - dtype=tf.float32, seed=seed) - color_coef = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.PIXEL_VALUE_SCALE, - preprocess_vars_cache) - - image = tf.multiply(image, color_coef) - image = tf.clip_by_value(image, 0.0, 255.0) - - return image - - -def random_image_scale(image, - masks=None, - min_scale_ratio=0.5, - max_scale_ratio=2.0, - seed=None, - preprocess_vars_cache=None): - """Scales the image size. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels]. - masks: (optional) rank 3 float32 tensor containing masks with - size [height, width, num_masks]. The value is set to None if there are no - masks. - min_scale_ratio: minimum scaling ratio. - max_scale_ratio: maximum scaling ratio. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same rank as input image. - masks: If masks is not none, resized masks which are the same rank as input - masks will be returned. - """ - with tf.name_scope('RandomImageScale', values=[image]): - result = [] - image_shape = tf.shape(image) - image_height = image_shape[0] - image_width = image_shape[1] - generator_func = functools.partial( - tf.random_uniform, [], - minval=min_scale_ratio, maxval=max_scale_ratio, - dtype=tf.float32, seed=seed) - size_coef = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.IMAGE_SCALE, - preprocess_vars_cache) - - image_newysize = tf.cast( - tf.multiply(tf.cast(image_height, dtype=tf.float32), size_coef), - dtype=tf.int32) - image_newxsize = tf.cast( - tf.multiply(tf.cast(image_width, dtype=tf.float32), size_coef), - dtype=tf.int32) - image = tf.image.resize_images( - image, [image_newysize, image_newxsize], align_corners=True) - result.append(image) - if masks is not None: - masks = tf.image.resize_images( - masks, [image_newysize, image_newxsize], - method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, - align_corners=True) - result.append(masks) - return tuple(result) - - -def _augment_only_rgb_channels(image, augment_function): - """Augments only the RGB slice of an image with additional channels.""" - # Skipping the concat if possible reduces latency. - if image.shape[2] == 3: - return augment_function(image) - rgb_slice = image[:, :, :3] - augmented_rgb_slice = augment_function(rgb_slice) - image = tf.concat([augmented_rgb_slice, image[:, :, 3:]], -1) - return image - - -def random_rgb_to_gray(image, - probability=0.1, - seed=None, - preprocess_vars_cache=None): - """Changes the image from RGB to Grayscale with the given probability. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - probability: the probability of returning a grayscale image. - The probability should be a number between [0, 1]. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - """ - def _image_to_gray(image): - image_gray1 = _rgb_to_grayscale(image) - image_gray3 = tf.image.grayscale_to_rgb(image_gray1) - return image_gray3 - - with tf.name_scope('RandomRGBtoGray', values=[image]): - # random variable defining whether to change to grayscale or not - generator_func = functools.partial(tf.random_uniform, [], seed=seed) - do_gray_random = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.RGB_TO_GRAY, - preprocess_vars_cache) - - image = tf.cond( - tf.greater(do_gray_random, probability), lambda: image, - lambda: _augment_only_rgb_channels(image, _image_to_gray)) - - return image - - -def adjust_gamma(image, gamma=1.0, gain=1.0): - """Adjusts the gamma. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - gamma: the gamma value. Must be a non-negative real number. - gain: a constant multiplier. - - Returns: - image: image which is the same shape as input image. - """ - with tf.name_scope('AdjustGamma', values=[image]): - def _adjust_gamma(image): - image = tf.image.adjust_gamma(image / 255, gamma, gain) * 255 - image = tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=255.0) - return image - - image = _augment_only_rgb_channels(image, _adjust_gamma) - return image - - -def random_adjust_brightness(image, - max_delta=0.2, - seed=None, - preprocess_vars_cache=None): - """Randomly adjusts brightness. - - Makes sure the output image is still between 0 and 255. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - max_delta: how much to change the brightness. A value between [0, 1). - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - """ - with tf.name_scope('RandomAdjustBrightness', values=[image]): - generator_func = functools.partial(tf.random_uniform, [], - -max_delta, max_delta, seed=seed) - delta = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.ADJUST_BRIGHTNESS, - preprocess_vars_cache) - - def _adjust_brightness(image): - image = tf.image.adjust_brightness(image / 255, delta) * 255 - image = tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=255.0) - return image - - image = _augment_only_rgb_channels(image, _adjust_brightness) - return image - - -def random_adjust_contrast(image, - min_delta=0.8, - max_delta=1.25, - seed=None, - preprocess_vars_cache=None): - """Randomly adjusts contrast. - - Makes sure the output image is still between 0 and 255. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - min_delta: see max_delta. - max_delta: how much to change the contrast. Contrast will change with a - value between min_delta and max_delta. This value will be - multiplied to the current contrast of the image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - """ - with tf.name_scope('RandomAdjustContrast', values=[image]): - generator_func = functools.partial(tf.random_uniform, [], - min_delta, max_delta, seed=seed) - contrast_factor = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.ADJUST_CONTRAST, - preprocess_vars_cache) - - def _adjust_contrast(image): - image = tf.image.adjust_contrast(image / 255, contrast_factor) * 255 - image = tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=255.0) - return image - image = _augment_only_rgb_channels(image, _adjust_contrast) - return image - - -def random_adjust_hue(image, - max_delta=0.02, - seed=None, - preprocess_vars_cache=None): - """Randomly adjusts hue. - - Makes sure the output image is still between 0 and 255. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - max_delta: change hue randomly with a value between 0 and max_delta. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - """ - with tf.name_scope('RandomAdjustHue', values=[image]): - generator_func = functools.partial(tf.random_uniform, [], - -max_delta, max_delta, seed=seed) - delta = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.ADJUST_HUE, - preprocess_vars_cache) - def _adjust_hue(image): - image = tf.image.adjust_hue(image / 255, delta) * 255 - image = tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=255.0) - return image - image = _augment_only_rgb_channels(image, _adjust_hue) - return image - - -def random_adjust_saturation(image, - min_delta=0.8, - max_delta=1.25, - seed=None, - preprocess_vars_cache=None): - """Randomly adjusts saturation. - - Makes sure the output image is still between 0 and 255. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - min_delta: see max_delta. - max_delta: how much to change the saturation. Saturation will change with a - value between min_delta and max_delta. This value will be - multiplied to the current saturation of the image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - """ - with tf.name_scope('RandomAdjustSaturation', values=[image]): - generator_func = functools.partial(tf.random_uniform, [], - min_delta, max_delta, seed=seed) - saturation_factor = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.ADJUST_SATURATION, - preprocess_vars_cache) - def _adjust_saturation(image): - image = tf.image.adjust_saturation(image / 255, saturation_factor) * 255 - image = tf.clip_by_value(image, clip_value_min=0.0, clip_value_max=255.0) - return image - image = _augment_only_rgb_channels(image, _adjust_saturation) - return image - - -def random_distort_color(image, color_ordering=0, preprocess_vars_cache=None): - """Randomly distorts color. - - Randomly distorts color using a combination of brightness, hue, contrast and - saturation changes. Makes sure the output image is still between 0 and 255. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - color_ordering: Python int, a type of distortion (valid values: 0, 1). - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same shape as input image. - - Raises: - ValueError: if color_ordering is not in {0, 1}. - """ - with tf.name_scope('RandomDistortColor', values=[image]): - if color_ordering == 0: - image = random_adjust_brightness( - image, max_delta=32. / 255., - preprocess_vars_cache=preprocess_vars_cache) - image = random_adjust_saturation( - image, min_delta=0.5, max_delta=1.5, - preprocess_vars_cache=preprocess_vars_cache) - image = random_adjust_hue( - image, max_delta=0.2, - preprocess_vars_cache=preprocess_vars_cache) - image = random_adjust_contrast( - image, min_delta=0.5, max_delta=1.5, - preprocess_vars_cache=preprocess_vars_cache) - - elif color_ordering == 1: - image = random_adjust_brightness( - image, max_delta=32. / 255., - preprocess_vars_cache=preprocess_vars_cache) - image = random_adjust_contrast( - image, min_delta=0.5, max_delta=1.5, - preprocess_vars_cache=preprocess_vars_cache) - image = random_adjust_saturation( - image, min_delta=0.5, max_delta=1.5, - preprocess_vars_cache=preprocess_vars_cache) - image = random_adjust_hue( - image, max_delta=0.2, - preprocess_vars_cache=preprocess_vars_cache) - else: - raise ValueError('color_ordering must be in {0, 1}') - return image - - -def random_jitter_boxes(boxes, ratio=0.05, jitter_mode='default', seed=None): - """Randomly jitters boxes in image. - - Args: - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - ratio: The ratio of the box width and height that the corners can jitter. - For example if the width is 100 pixels and ratio is 0.05, - the corners can jitter up to 5 pixels in the x direction. - jitter_mode: One of - shrink - Only shrinks boxes. - expand - Only expands boxes. - expand_symmetric - Expands the boxes symmetrically along height and width - dimensions without changing the box center. The ratios of expansion - along X, Y dimensions are independent - shrink_symmetric - Shrinks the boxes symmetrically along height and width - dimensions without changing the box center. The ratios of shrinking - along X, Y dimensions are independent - expand_symmetric_xy - Expands the boxes symetrically along height and - width dimensions and the ratio of expansion is same for both. - shrink_symmetric_xy - Shrinks the boxes symetrically along height and - width dimensions and the ratio of shrinking is same for both. - default - Randomly and independently perturbs each box boundary. - seed: random seed. - - Returns: - boxes: boxes which is the same shape as input boxes. - """ - with tf.name_scope('RandomJitterBoxes'): - ymin, xmin, ymax, xmax = (boxes[:, i] for i in range(4)) - - blist = box_list.BoxList(boxes) - ycenter, xcenter, height, width = blist.get_center_coordinates_and_sizes() - - height = tf.maximum(tf.abs(height), 1e-6) - width = tf.maximum(tf.abs(width), 1e-6) - - if jitter_mode in ['shrink', 'shrink_symmetric', 'shrink_symmetric_xy']: - min_ratio, max_ratio = -ratio, 0 - elif jitter_mode in ['expand', 'expand_symmetric', 'expand_symmetric_xy']: - min_ratio, max_ratio = 0, ratio - elif jitter_mode == 'default': - min_ratio, max_ratio = -ratio, ratio - else: - raise ValueError('Unknown jitter mode - %s' % jitter_mode) - - num_boxes = tf.shape(boxes)[0] - - if jitter_mode in ['expand_symmetric', 'shrink_symmetric', - 'expand_symmetric_xy', 'shrink_symmetric_xy']: - distortion = 1.0 + tf.random.uniform( - [num_boxes, 2], minval=min_ratio, maxval=max_ratio, dtype=tf.float32, - seed=seed) - height_distortion = distortion[:, 0] - width_distortion = distortion[:, 1] - - # This is to ensure that all boxes are augmented symmetrically. We clip - # each boundary to lie within the image, and when doing so, we also - # adjust its symmetric counterpart. - max_height_distortion = tf.abs(tf.minimum( - (2.0 * ycenter) / height, 2.0 * (1 - ycenter) / height)) - max_width_distortion = tf.abs(tf.minimum( - (2.0 * xcenter) / width, 2.0 * (1 - xcenter) / width)) - - if jitter_mode in ['expand_symmetric_xy', 'shrink_symmetric_xy']: - height_distortion = width_distortion = distortion[:, 0] - max_height_distortion = max_width_distortion = ( - tf.minimum(max_width_distortion, max_height_distortion)) - - height_distortion = tf.clip_by_value( - height_distortion, -max_height_distortion, max_height_distortion) - width_distortion = tf.clip_by_value( - width_distortion, -max_width_distortion, max_width_distortion) - - ymin = ycenter - (height * height_distortion / 2.0) - ymax = ycenter + (height * height_distortion / 2.0) - xmin = xcenter - (width * width_distortion / 2.0) - xmax = xcenter + (width * width_distortion / 2.0) - - elif jitter_mode in ['expand', 'shrink', 'default']: - distortion = 1.0 + tf.random.uniform( - [num_boxes, 4], minval=min_ratio, maxval=max_ratio, dtype=tf.float32, - seed=seed) - ymin_jitter = height * distortion[:, 0] - xmin_jitter = width * distortion[:, 1] - ymax_jitter = height * distortion[:, 2] - xmax_jitter = width * distortion[:, 3] - - ymin, ymax = ycenter - (ymin_jitter / 2.0), ycenter + (ymax_jitter / 2.0) - xmin, xmax = xcenter - (xmin_jitter / 2.0), xcenter + (xmax_jitter / 2.0) - - else: - raise ValueError('Unknown jitter mode - %s' % jitter_mode) - - boxes = tf.stack([ymin, xmin, ymax, xmax], axis=1) - return tf.clip_by_value(boxes, 0.0, 1.0) - - -def _strict_random_crop_image(image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - mask_weights=None, - keypoints=None, - keypoint_visibilities=None, - densepose_num_points=None, - densepose_part_ids=None, - densepose_surface_coords=None, - min_object_covered=1.0, - aspect_ratio_range=(0.75, 1.33), - area_range=(0.1, 1.0), - overlap_thresh=0.3, - clip_boxes=True, - preprocess_vars_cache=None): - """Performs random crop. - - Note: Keypoint coordinates that are outside the crop will be set to NaN, which - is consistent with the original keypoint encoding for non-existing keypoints. - This function always crops the image and is supposed to be used by - `random_crop_image` function which sometimes returns the image unchanged. - - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes with shape - [num_instances, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - label_confidences: (optional) float32 tensor of shape [num_instances] - representing the confidence for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - mask_weights: (optional) rank 1 float32 tensor with shape [num_instances] - with instance masks weights. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - keypoint_visibilities: (optional) rank 2 bool tensor with shape - [num_instances, num_keypoints]. - densepose_num_points: (optional) rank 1 int32 tensor with shape - [num_instances] with the number of sampled points per - instance. - densepose_part_ids: (optional) rank 2 int32 tensor with shape - [num_instances, num_points] holding the part id for each - sampled point. These part_ids are 0-indexed, where the - first non-background part has index 0. - densepose_surface_coords: (optional) rank 3 float32 tensor with shape - [num_instances, num_points, 4]. The DensePose - coordinates are of the form (y, x, v, u) where - (y, x) are the normalized image coordinates for a - sampled point, and (v, u) is the surface - coordinate for the part. - min_object_covered: the cropped image must cover at least this fraction of - at least one of the input bounding boxes. - aspect_ratio_range: allowed range for aspect ratio of cropped image. - area_range: allowed range for area ratio between cropped image and the - original image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - - If label_weights, multiclass_scores, masks, mask_weights, keypoints, - keypoint_visibilities, densepose_num_points, densepose_part_ids, or - densepose_surface_coords is not None, the function also returns: - label_weights: rank 1 float32 tensor with shape [num_instances]. - multiclass_scores: rank 2 float32 tensor with shape - [num_instances, num_classes] - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - mask_weights: rank 1 float32 tensor with shape [num_instances] with mask - weights. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - keypoint_visibilities: rank 2 bool tensor with shape - [num_instances, num_keypoints] - densepose_num_points: rank 1 int32 tensor with shape [num_instances]. - densepose_part_ids: rank 2 int32 tensor with shape - [num_instances, num_points]. - densepose_surface_coords: rank 3 float32 tensor with shape - [num_instances, num_points, 4]. - - Raises: - ValueError: If some but not all of the DensePose tensors are provided. - """ - with tf.name_scope('RandomCropImage', values=[image, boxes]): - densepose_tensors = [densepose_num_points, densepose_part_ids, - densepose_surface_coords] - if (any(t is not None for t in densepose_tensors) and - not all(t is not None for t in densepose_tensors)): - raise ValueError('If cropping DensePose labels, must provide ' - '`densepose_num_points`, `densepose_part_ids`, and ' - '`densepose_surface_coords`') - image_shape = tf.shape(image) - - # boxes are [N, 4]. Lets first make them [N, 1, 4]. - boxes_expanded = tf.expand_dims( - tf.clip_by_value( - boxes, clip_value_min=0.0, clip_value_max=1.0), 1) - - generator_func = functools.partial( - tf.image.sample_distorted_bounding_box, - image_shape, - bounding_boxes=boxes_expanded, - min_object_covered=min_object_covered, - aspect_ratio_range=aspect_ratio_range, - area_range=area_range, - max_attempts=100, - use_image_if_no_bounding_boxes=True) - - # for ssd cropping, each value of min_object_covered has its own - # cached random variable - sample_distorted_bounding_box = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.STRICT_CROP_IMAGE, - preprocess_vars_cache, key=min_object_covered) - - im_box_begin, im_box_size, im_box = sample_distorted_bounding_box - im_box_end = im_box_begin + im_box_size - new_image = image[im_box_begin[0]:im_box_end[0], - im_box_begin[1]:im_box_end[1], :] - new_image.set_shape([None, None, image.get_shape()[2]]) - - # [1, 4] - im_box_rank2 = tf.squeeze(im_box, axis=[0]) - # [4] - im_box_rank1 = tf.squeeze(im_box) - - boxlist = box_list.BoxList(boxes) - boxlist.add_field('labels', labels) - - if label_weights is not None: - boxlist.add_field('label_weights', label_weights) - - if label_confidences is not None: - boxlist.add_field('label_confidences', label_confidences) - - if multiclass_scores is not None: - boxlist.add_field('multiclass_scores', multiclass_scores) - - im_boxlist = box_list.BoxList(im_box_rank2) - - # remove boxes that are outside cropped image - boxlist, inside_window_ids = box_list_ops.prune_completely_outside_window( - boxlist, im_box_rank1) - - # remove boxes that are outside image - overlapping_boxlist, keep_ids = box_list_ops.prune_non_overlapping_boxes( - boxlist, im_boxlist, overlap_thresh) - - # change the coordinate of the remaining boxes - new_labels = overlapping_boxlist.get_field('labels') - new_boxlist = box_list_ops.change_coordinate_frame(overlapping_boxlist, - im_box_rank1) - new_boxes = new_boxlist.get() - if clip_boxes: - new_boxes = tf.clip_by_value( - new_boxes, clip_value_min=0.0, clip_value_max=1.0) - - result = [new_image, new_boxes, new_labels] - - if label_weights is not None: - new_label_weights = overlapping_boxlist.get_field('label_weights') - result.append(new_label_weights) - - if label_confidences is not None: - new_label_confidences = overlapping_boxlist.get_field('label_confidences') - result.append(new_label_confidences) - - if multiclass_scores is not None: - new_multiclass_scores = overlapping_boxlist.get_field('multiclass_scores') - result.append(new_multiclass_scores) - - if masks is not None: - masks_of_boxes_inside_window = tf.gather(masks, inside_window_ids) - masks_of_boxes_completely_inside_window = tf.gather( - masks_of_boxes_inside_window, keep_ids) - new_masks = masks_of_boxes_completely_inside_window[:, im_box_begin[ - 0]:im_box_end[0], im_box_begin[1]:im_box_end[1]] - result.append(new_masks) - - if mask_weights is not None: - mask_weights_inside_window = tf.gather(mask_weights, inside_window_ids) - mask_weights_completely_inside_window = tf.gather( - mask_weights_inside_window, keep_ids) - result.append(mask_weights_completely_inside_window) - - if keypoints is not None: - keypoints_of_boxes_inside_window = tf.gather(keypoints, inside_window_ids) - keypoints_of_boxes_completely_inside_window = tf.gather( - keypoints_of_boxes_inside_window, keep_ids) - new_keypoints = keypoint_ops.change_coordinate_frame( - keypoints_of_boxes_completely_inside_window, im_box_rank1) - if clip_boxes: - new_keypoints = keypoint_ops.prune_outside_window(new_keypoints, - [0.0, 0.0, 1.0, 1.0]) - result.append(new_keypoints) - - if keypoint_visibilities is not None: - kpt_vis_of_boxes_inside_window = tf.gather(keypoint_visibilities, - inside_window_ids) - kpt_vis_of_boxes_completely_inside_window = tf.gather( - kpt_vis_of_boxes_inside_window, keep_ids) - if clip_boxes: - # Set any keypoints with NaN coordinates to invisible. - new_kpt_visibilities = keypoint_ops.set_keypoint_visibilities( - new_keypoints, kpt_vis_of_boxes_completely_inside_window) - result.append(new_kpt_visibilities) - - if densepose_num_points is not None: - filtered_dp_tensors = [] - for dp_tensor in densepose_tensors: - dp_tensor_inside_window = tf.gather(dp_tensor, inside_window_ids) - dp_tensor_completely_inside_window = tf.gather(dp_tensor_inside_window, - keep_ids) - filtered_dp_tensors.append(dp_tensor_completely_inside_window) - new_dp_num_points = filtered_dp_tensors[0] - new_dp_point_ids = filtered_dp_tensors[1] - new_dp_surf_coords = densepose_ops.change_coordinate_frame( - filtered_dp_tensors[2], im_box_rank1) - if clip_boxes: - new_dp_num_points, new_dp_point_ids, new_dp_surf_coords = ( - densepose_ops.prune_outside_window( - new_dp_num_points, new_dp_point_ids, new_dp_surf_coords, - window=[0.0, 0.0, 1.0, 1.0])) - result.extend([new_dp_num_points, new_dp_point_ids, new_dp_surf_coords]) - return tuple(result) - - -def random_crop_image(image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - mask_weights=None, - keypoints=None, - keypoint_visibilities=None, - densepose_num_points=None, - densepose_part_ids=None, - densepose_surface_coords=None, - min_object_covered=1.0, - aspect_ratio_range=(0.75, 1.33), - area_range=(0.1, 1.0), - overlap_thresh=0.3, - clip_boxes=True, - random_coef=0.0, - seed=None, - preprocess_vars_cache=None): - """Randomly crops the image. - - Given the input image and its bounding boxes, this op randomly - crops a subimage. Given a user-provided set of input constraints, - the crop window is resampled until it satisfies these constraints. - If within 100 trials it is unable to find a valid crop, the original - image is returned. See the Args section for a description of the input - constraints. Both input boxes and returned Boxes are in normalized - form (e.g., lie in the unit square [0, 1]). - This function will return the original image with probability random_coef. - - Note: Keypoint coordinates that are outside the crop will be set to NaN, which - is consistent with the original keypoint encoding for non-existing keypoints. - Also, the keypoint visibility will be set to False. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes with shape - [num_instances, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - label_confidences: (optional) float32 tensor of shape [num_instances]. - representing the confidence for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - mask_weights: (optional) rank 1 float32 tensor with shape [num_instances] - containing weights for each instance mask. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - keypoint_visibilities: (optional) rank 2 bool tensor with shape - [num_instances, num_keypoints]. - densepose_num_points: (optional) rank 1 int32 tensor with shape - [num_instances] with the number of sampled points per - instance. - densepose_part_ids: (optional) rank 2 int32 tensor with shape - [num_instances, num_points] holding the part id for each - sampled point. These part_ids are 0-indexed, where the - first non-background part has index 0. - densepose_surface_coords: (optional) rank 3 float32 tensor with shape - [num_instances, num_points, 4]. The DensePose - coordinates are of the form (y, x, v, u) where - (y, x) are the normalized image coordinates for a - sampled point, and (v, u) is the surface - coordinate for the part. - min_object_covered: the cropped image must cover at least this fraction of - at least one of the input bounding boxes. - aspect_ratio_range: allowed range for aspect ratio of cropped image. - area_range: allowed range for area ratio between cropped image and the - original image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - random_coef: a random coefficient that defines the chance of getting the - original image. If random_coef is 0, we will always get the - cropped image, and if it is 1.0, we will always get the - original image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: Image shape will be [new_height, new_width, channels]. - boxes: boxes which is the same rank as input boxes. Boxes are in normalized - form. - labels: new labels. - - If label_weights, multiclass_scores, masks, keypoints, - keypoint_visibilities, densepose_num_points, densepose_part_ids, - densepose_surface_coords is not None, the function also returns: - label_weights: rank 1 float32 tensor with shape [num_instances]. - multiclass_scores: rank 2 float32 tensor with shape - [num_instances, num_classes] - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - mask_weights: rank 1 float32 tensor with shape [num_instances]. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - keypoint_visibilities: rank 2 bool tensor with shape - [num_instances, num_keypoints] - densepose_num_points: rank 1 int32 tensor with shape [num_instances]. - densepose_part_ids: rank 2 int32 tensor with shape - [num_instances, num_points]. - densepose_surface_coords: rank 3 float32 tensor with shape - [num_instances, num_points, 4]. - """ - - def strict_random_crop_image_fn(): - return _strict_random_crop_image( - image, - boxes, - labels, - label_weights, - label_confidences=label_confidences, - multiclass_scores=multiclass_scores, - masks=masks, - mask_weights=mask_weights, - keypoints=keypoints, - keypoint_visibilities=keypoint_visibilities, - densepose_num_points=densepose_num_points, - densepose_part_ids=densepose_part_ids, - densepose_surface_coords=densepose_surface_coords, - min_object_covered=min_object_covered, - aspect_ratio_range=aspect_ratio_range, - area_range=area_range, - overlap_thresh=overlap_thresh, - clip_boxes=clip_boxes, - preprocess_vars_cache=preprocess_vars_cache) - - # avoids tf.cond to make faster RCNN training on borg. See b/140057645. - if random_coef < sys.float_info.min: - result = strict_random_crop_image_fn() - else: - generator_func = functools.partial(tf.random_uniform, [], seed=seed) - do_a_crop_random = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.CROP_IMAGE, - preprocess_vars_cache) - do_a_crop_random = tf.greater(do_a_crop_random, random_coef) - - outputs = [image, boxes, labels] - - if label_weights is not None: - outputs.append(label_weights) - if label_confidences is not None: - outputs.append(label_confidences) - if multiclass_scores is not None: - outputs.append(multiclass_scores) - if masks is not None: - outputs.append(masks) - if mask_weights is not None: - outputs.append(mask_weights) - if keypoints is not None: - outputs.append(keypoints) - if keypoint_visibilities is not None: - outputs.append(keypoint_visibilities) - if densepose_num_points is not None: - outputs.extend([densepose_num_points, densepose_part_ids, - densepose_surface_coords]) - - result = tf.cond(do_a_crop_random, strict_random_crop_image_fn, - lambda: tuple(outputs)) - return result - - -def random_pad_image(image, - boxes, - masks=None, - keypoints=None, - densepose_surface_coords=None, - min_image_size=None, - max_image_size=None, - pad_color=None, - center_pad=False, - seed=None, - preprocess_vars_cache=None): - """Randomly pads the image. - - This function randomly pads the image with zeros. The final size of the - padded image will be between min_image_size and max_image_size. - if min_image_size is smaller than the input image size, min_image_size will - be set to the input image size. The same for max_image_size. The input image - will be located at a uniformly random location inside the padded image. - The relative location of the boxes to the original image will remain the same. - - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - masks: (optional) rank 3 float32 tensor with shape - [N, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [N, num_keypoints, 2]. The keypoints are in y-x normalized - coordinates. - densepose_surface_coords: (optional) rank 3 float32 tensor with shape - [N, num_points, 4]. The DensePose coordinates are - of the form (y, x, v, u) where (y, x) are the - normalized image coordinates for a sampled point, - and (v, u) is the surface coordinate for the part. - min_image_size: a tensor of size [min_height, min_width], type tf.int32. - If passed as None, will be set to image size - [height, width]. - max_image_size: a tensor of size [max_height, max_width], type tf.int32. - If passed as None, will be set to twice the - image [height * 2, width * 2]. - pad_color: padding color. A rank 1 tensor of [channels] with dtype= - tf.float32. if set as None, it will be set to average color of - the input image. - center_pad: whether the original image will be padded to the center, or - randomly padded (which is default). - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: Image shape will be [new_height, new_width, channels]. - boxes: boxes which is the same rank as input boxes. Boxes are in normalized - form. - - if masks is not None, the function also returns: - masks: rank 3 float32 tensor with shape [N, new_height, new_width] - if keypoints is not None, the function also returns: - keypoints: rank 3 float32 tensor with shape [N, num_keypoints, 2] - if densepose_surface_coords is not None, the function also returns: - densepose_surface_coords: rank 3 float32 tensor with shape - [num_instances, num_points, 4] - """ - if pad_color is None: - pad_color = tf.reduce_mean(image, axis=[0, 1]) - - image_shape = tf.shape(image) - image_height = image_shape[0] - image_width = image_shape[1] - - if max_image_size is None: - max_image_size = tf.stack([image_height * 2, image_width * 2]) - max_image_size = tf.maximum(max_image_size, - tf.stack([image_height, image_width])) - - if min_image_size is None: - min_image_size = tf.stack([image_height, image_width]) - min_image_size = tf.maximum(min_image_size, - tf.stack([image_height, image_width])) - - target_height = tf.cond( - max_image_size[0] > min_image_size[0], - lambda: _random_integer(min_image_size[0], max_image_size[0], seed), - lambda: max_image_size[0]) - - target_width = tf.cond( - max_image_size[1] > min_image_size[1], - lambda: _random_integer(min_image_size[1], max_image_size[1], seed), - lambda: max_image_size[1]) - - offset_height = tf.cond( - target_height > image_height, - lambda: _random_integer(0, target_height - image_height, seed), - lambda: tf.constant(0, dtype=tf.int32)) - - offset_width = tf.cond( - target_width > image_width, - lambda: _random_integer(0, target_width - image_width, seed), - lambda: tf.constant(0, dtype=tf.int32)) - - if center_pad: - offset_height = tf.cast(tf.floor((target_height - image_height) / 2), - tf.int32) - offset_width = tf.cast(tf.floor((target_width - image_width) / 2), - tf.int32) - - gen_func = lambda: (target_height, target_width, offset_height, offset_width) - params = _get_or_create_preprocess_rand_vars( - gen_func, preprocessor_cache.PreprocessorCache.PAD_IMAGE, - preprocess_vars_cache) - target_height, target_width, offset_height, offset_width = params - - new_image = tf.image.pad_to_bounding_box( - image, - offset_height=offset_height, - offset_width=offset_width, - target_height=target_height, - target_width=target_width) - - # Setting color of the padded pixels - image_ones = tf.ones_like(image) - image_ones_padded = tf.image.pad_to_bounding_box( - image_ones, - offset_height=offset_height, - offset_width=offset_width, - target_height=target_height, - target_width=target_width) - image_color_padded = (1.0 - image_ones_padded) * pad_color - new_image += image_color_padded - - # setting boxes - new_window = tf.cast( - tf.stack([ - -offset_height, -offset_width, target_height - offset_height, - target_width - offset_width - ]), - dtype=tf.float32) - new_window /= tf.cast( - tf.stack([image_height, image_width, image_height, image_width]), - dtype=tf.float32) - boxlist = box_list.BoxList(boxes) - new_boxlist = box_list_ops.change_coordinate_frame(boxlist, new_window) - new_boxes = new_boxlist.get() - - result = [new_image, new_boxes] - - if masks is not None: - new_masks = tf.image.pad_to_bounding_box( - masks[:, :, :, tf.newaxis], - offset_height=offset_height, - offset_width=offset_width, - target_height=target_height, - target_width=target_width)[:, :, :, 0] - result.append(new_masks) - - if keypoints is not None: - new_keypoints = keypoint_ops.change_coordinate_frame(keypoints, new_window) - result.append(new_keypoints) - - if densepose_surface_coords is not None: - new_densepose_surface_coords = densepose_ops.change_coordinate_frame( - densepose_surface_coords, new_window) - result.append(new_densepose_surface_coords) - - return tuple(result) - - -def random_absolute_pad_image(image, - boxes, - masks=None, - keypoints=None, - densepose_surface_coords=None, - max_height_padding=None, - max_width_padding=None, - pad_color=None, - seed=None, - preprocess_vars_cache=None): - """Randomly pads the image by small absolute amounts. - - As random_pad_image above, but the padding is of size [0, max_height_padding] - or [0, max_width_padding] instead of padding to a fixed size of - max_height_padding for all images. - - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - masks: (optional) rank 3 float32 tensor with shape - [N, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [N, num_keypoints, 2]. The keypoints are in y-x normalized - coordinates. - densepose_surface_coords: (optional) rank 3 float32 tensor with shape - [N, num_points, 4]. The DensePose coordinates are - of the form (y, x, v, u) where (y, x) are the - normalized image coordinates for a sampled point, - and (v, u) is the surface coordinate for the part. - max_height_padding: a scalar tf.int32 tensor denoting the maximum amount of - height padding. The padding will be chosen uniformly at - random from [0, max_height_padding). - max_width_padding: a scalar tf.int32 tensor denoting the maximum amount of - width padding. The padding will be chosen uniformly at - random from [0, max_width_padding). - pad_color: padding color. A rank 1 tensor of [3] with dtype=tf.float32. - if set as None, it will be set to average color of the input - image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: Image shape will be [new_height, new_width, channels]. - boxes: boxes which is the same rank as input boxes. Boxes are in normalized - form. - if masks is not None, the function also returns: - masks: rank 3 float32 tensor with shape [N, new_height, new_width] - if keypoints is not None, the function also returns: - keypoints: rank 3 float32 tensor with shape [N, num_keypoints, 2] - """ - min_image_size = tf.shape(image)[:2] - max_image_size = min_image_size + tf.cast( - [max_height_padding, max_width_padding], dtype=tf.int32) - return random_pad_image( - image, - boxes, - masks=masks, - keypoints=keypoints, - densepose_surface_coords=densepose_surface_coords, - min_image_size=min_image_size, - max_image_size=max_image_size, - pad_color=pad_color, - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - - -def random_crop_pad_image(image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - min_object_covered=1.0, - aspect_ratio_range=(0.75, 1.33), - area_range=(0.1, 1.0), - overlap_thresh=0.3, - clip_boxes=True, - random_coef=0.0, - min_padded_size_ratio=(1.0, 1.0), - max_padded_size_ratio=(2.0, 2.0), - pad_color=None, - seed=None, - preprocess_vars_cache=None): - """Randomly crops and pads the image. - - Given an input image and its bounding boxes, this op first randomly crops - the image and then randomly pads the image with background values. Parameters - min_padded_size_ratio and max_padded_size_ratio, determine the range of the - final output image size. Specifically, the final image size will have a size - in the range of min_padded_size_ratio * tf.shape(image) and - max_padded_size_ratio * tf.shape(image). Note that these ratios are with - respect to the size of the original image, so we can't capture the same - effect easily by independently applying RandomCropImage - followed by RandomPadImage. - - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: rank 1 float32 containing the label weights. - label_confidences: rank 1 float32 containing the label confidences. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - min_object_covered: the cropped image must cover at least this fraction of - at least one of the input bounding boxes. - aspect_ratio_range: allowed range for aspect ratio of cropped image. - area_range: allowed range for area ratio between cropped image and the - original image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - random_coef: a random coefficient that defines the chance of getting the - original image. If random_coef is 0, we will always get the - cropped image, and if it is 1.0, we will always get the - original image. - min_padded_size_ratio: min ratio of padded image height and width to the - input image's height and width. - max_padded_size_ratio: max ratio of padded image height and width to the - input image's height and width. - pad_color: padding color. A rank 1 tensor of [3] with dtype=tf.float32. - if set as None, it will be set to average color of the randomly - cropped image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - padded_image: padded image. - padded_boxes: boxes which is the same rank as input boxes. Boxes are in - normalized form. - cropped_labels: cropped labels. - if label_weights is not None also returns: - cropped_label_weights: cropped label weights. - if multiclass_scores is not None also returns: - cropped_multiclass_scores: cropped_multiclass_scores. - - """ - image_size = tf.shape(image) - image_height = image_size[0] - image_width = image_size[1] - result = random_crop_image( - image=image, - boxes=boxes, - labels=labels, - label_weights=label_weights, - label_confidences=label_confidences, - multiclass_scores=multiclass_scores, - min_object_covered=min_object_covered, - aspect_ratio_range=aspect_ratio_range, - area_range=area_range, - overlap_thresh=overlap_thresh, - clip_boxes=clip_boxes, - random_coef=random_coef, - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - - cropped_image, cropped_boxes, cropped_labels = result[:3] - - min_image_size = tf.cast( - tf.cast(tf.stack([image_height, image_width]), dtype=tf.float32) * - min_padded_size_ratio, - dtype=tf.int32) - max_image_size = tf.cast( - tf.cast(tf.stack([image_height, image_width]), dtype=tf.float32) * - max_padded_size_ratio, - dtype=tf.int32) - - padded_image, padded_boxes = random_pad_image( # pylint: disable=unbalanced-tuple-unpacking - cropped_image, - cropped_boxes, - min_image_size=min_image_size, - max_image_size=max_image_size, - pad_color=pad_color, - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - - cropped_padded_output = (padded_image, padded_boxes, cropped_labels) - - index = 3 - if label_weights is not None: - cropped_label_weights = result[index] - cropped_padded_output += (cropped_label_weights,) - index += 1 - - if label_confidences is not None: - cropped_label_confidences = result[index] - cropped_padded_output += (cropped_label_confidences,) - index += 1 - - if multiclass_scores is not None: - cropped_multiclass_scores = result[index] - cropped_padded_output += (cropped_multiclass_scores,) - - return cropped_padded_output - - -def random_crop_to_aspect_ratio(image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - keypoints=None, - aspect_ratio=1.0, - overlap_thresh=0.3, - clip_boxes=True, - center_crop=False, - seed=None, - preprocess_vars_cache=None): - """Randomly crops an image to the specified aspect ratio. - - Randomly crops the a portion of the image such that the crop is of the - specified aspect ratio, and the crop is as large as possible. If the specified - aspect ratio is larger than the aspect ratio of the image, this op will - randomly remove rows from the top and bottom of the image. If the specified - aspect ratio is less than the aspect ratio of the image, this op will randomly - remove cols from the left and right of the image. If the specified aspect - ratio is the same as the aspect ratio of the image, this op will return the - image. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - label_confidences: (optional) float32 tensor of shape [num_instances] - representing the confidence for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - aspect_ratio: the aspect ratio of cropped image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - center_crop: whether to take the center crop or a random crop. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - - If label_weights, masks, keypoints, or multiclass_scores is not None, the - function also returns: - label_weights: rank 1 float32 tensor with shape [num_instances]. - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - multiclass_scores: rank 2 float32 tensor with shape - [num_instances, num_classes] - - Raises: - ValueError: If image is not a 3D tensor. - """ - if len(image.get_shape()) != 3: - raise ValueError('Image should be 3D tensor') - - with tf.name_scope('RandomCropToAspectRatio', values=[image]): - image_shape = tf.shape(image) - orig_height = image_shape[0] - orig_width = image_shape[1] - orig_aspect_ratio = tf.cast( - orig_width, dtype=tf.float32) / tf.cast( - orig_height, dtype=tf.float32) - new_aspect_ratio = tf.constant(aspect_ratio, dtype=tf.float32) - - def target_height_fn(): - return tf.cast( - tf.round(tf.cast(orig_width, dtype=tf.float32) / new_aspect_ratio), - dtype=tf.int32) - - target_height = tf.cond(orig_aspect_ratio >= new_aspect_ratio, - lambda: orig_height, target_height_fn) - - def target_width_fn(): - return tf.cast( - tf.round(tf.cast(orig_height, dtype=tf.float32) * new_aspect_ratio), - dtype=tf.int32) - - target_width = tf.cond(orig_aspect_ratio <= new_aspect_ratio, - lambda: orig_width, target_width_fn) - - # either offset_height = 0 and offset_width is randomly chosen from - # [0, offset_width - target_width), or else offset_width = 0 and - # offset_height is randomly chosen from [0, offset_height - target_height) - if center_crop: - offset_height = tf.cast(tf.math.floor((orig_height - target_height) / 2), - tf.int32) - offset_width = tf.cast(tf.math.floor((orig_width - target_width) / 2), - tf.int32) - else: - offset_height = _random_integer(0, orig_height - target_height + 1, seed) - offset_width = _random_integer(0, orig_width - target_width + 1, seed) - - generator_func = lambda: (offset_height, offset_width) - offset_height, offset_width = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.CROP_TO_ASPECT_RATIO, - preprocess_vars_cache) - - new_image = tf.image.crop_to_bounding_box( - image, offset_height, offset_width, target_height, target_width) - - im_box = tf.stack([ - tf.cast(offset_height, dtype=tf.float32) / - tf.cast(orig_height, dtype=tf.float32), - tf.cast(offset_width, dtype=tf.float32) / - tf.cast(orig_width, dtype=tf.float32), - tf.cast(offset_height + target_height, dtype=tf.float32) / - tf.cast(orig_height, dtype=tf.float32), - tf.cast(offset_width + target_width, dtype=tf.float32) / - tf.cast(orig_width, dtype=tf.float32) - ]) - - boxlist = box_list.BoxList(boxes) - boxlist.add_field('labels', labels) - - boxlist.add_field('label_weights', label_weights) - - if label_confidences is not None: - boxlist.add_field('label_confidences', label_confidences) - - if multiclass_scores is not None: - boxlist.add_field('multiclass_scores', multiclass_scores) - - im_boxlist = box_list.BoxList(tf.expand_dims(im_box, 0)) - - # remove boxes whose overlap with the image is less than overlap_thresh - overlapping_boxlist, keep_ids = box_list_ops.prune_non_overlapping_boxes( - boxlist, im_boxlist, overlap_thresh) - - # change the coordinate of the remaining boxes - new_labels = overlapping_boxlist.get_field('labels') - new_boxlist = box_list_ops.change_coordinate_frame(overlapping_boxlist, - im_box) - if clip_boxes: - new_boxlist = box_list_ops.clip_to_window( - new_boxlist, tf.constant([0.0, 0.0, 1.0, 1.0], tf.float32)) - new_boxes = new_boxlist.get() - - result = [new_image, new_boxes, new_labels] - - new_label_weights = overlapping_boxlist.get_field('label_weights') - result.append(new_label_weights) - - if label_confidences is not None: - new_label_confidences = ( - overlapping_boxlist.get_field('label_confidences')) - result.append(new_label_confidences) - - if multiclass_scores is not None: - new_multiclass_scores = overlapping_boxlist.get_field('multiclass_scores') - result.append(new_multiclass_scores) - - if masks is not None: - masks_inside_window = tf.gather(masks, keep_ids) - masks_box_begin = tf.stack([0, offset_height, offset_width]) - masks_box_size = tf.stack([-1, target_height, target_width]) - new_masks = tf.slice(masks_inside_window, masks_box_begin, masks_box_size) - result.append(new_masks) - - if keypoints is not None: - keypoints_inside_window = tf.gather(keypoints, keep_ids) - new_keypoints = keypoint_ops.change_coordinate_frame( - keypoints_inside_window, im_box) - if clip_boxes: - new_keypoints = keypoint_ops.prune_outside_window(new_keypoints, - [0.0, 0.0, 1.0, 1.0]) - result.append(new_keypoints) - - return tuple(result) - - -def random_pad_to_aspect_ratio(image, - boxes, - masks=None, - keypoints=None, - aspect_ratio=1.0, - min_padded_size_ratio=(1.0, 1.0), - max_padded_size_ratio=(2.0, 2.0), - seed=None, - preprocess_vars_cache=None): - """Randomly zero pads an image to the specified aspect ratio. - - Pads the image so that the resulting image will have the specified aspect - ratio without scaling less than the min_padded_size_ratio or more than the - max_padded_size_ratio. If the min_padded_size_ratio or max_padded_size_ratio - is lower than what is possible to maintain the aspect ratio, then this method - will use the least padding to achieve the specified aspect ratio. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - aspect_ratio: aspect ratio of the final image. - min_padded_size_ratio: min ratio of padded image height and width to the - input image's height and width. - max_padded_size_ratio: max ratio of padded image height and width to the - input image's height and width. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - - If masks, or keypoints is not None, the function also returns: - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - - Raises: - ValueError: If image is not a 3D tensor. - """ - if len(image.get_shape()) != 3: - raise ValueError('Image should be 3D tensor') - - with tf.name_scope('RandomPadToAspectRatio', values=[image]): - image_shape = tf.shape(image) - image_height = tf.cast(image_shape[0], dtype=tf.float32) - image_width = tf.cast(image_shape[1], dtype=tf.float32) - image_aspect_ratio = image_width / image_height - new_aspect_ratio = tf.constant(aspect_ratio, dtype=tf.float32) - target_height = tf.cond( - image_aspect_ratio <= new_aspect_ratio, - lambda: image_height, - lambda: image_width / new_aspect_ratio) - target_width = tf.cond( - image_aspect_ratio >= new_aspect_ratio, - lambda: image_width, - lambda: image_height * new_aspect_ratio) - - min_height = tf.maximum( - min_padded_size_ratio[0] * image_height, target_height) - min_width = tf.maximum( - min_padded_size_ratio[1] * image_width, target_width) - max_height = tf.maximum( - max_padded_size_ratio[0] * image_height, target_height) - max_width = tf.maximum( - max_padded_size_ratio[1] * image_width, target_width) - - max_scale = tf.minimum(max_height / target_height, max_width / target_width) - min_scale = tf.minimum( - max_scale, - tf.maximum(min_height / target_height, min_width / target_width)) - - generator_func = functools.partial(tf.random_uniform, [], - min_scale, max_scale, seed=seed) - scale = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.PAD_TO_ASPECT_RATIO, - preprocess_vars_cache) - - target_height = tf.round(scale * target_height) - target_width = tf.round(scale * target_width) - - new_image = tf.image.pad_to_bounding_box( - image, 0, 0, tf.cast(target_height, dtype=tf.int32), - tf.cast(target_width, dtype=tf.int32)) - - im_box = tf.stack([ - 0.0, - 0.0, - target_height / image_height, - target_width / image_width - ]) - boxlist = box_list.BoxList(boxes) - new_boxlist = box_list_ops.change_coordinate_frame(boxlist, im_box) - new_boxes = new_boxlist.get() - - result = [new_image, new_boxes] - - if masks is not None: - new_masks = tf.expand_dims(masks, -1) - new_masks = tf.image.pad_to_bounding_box( - new_masks, 0, 0, tf.cast(target_height, dtype=tf.int32), - tf.cast(target_width, dtype=tf.int32)) - new_masks = tf.squeeze(new_masks, [-1]) - result.append(new_masks) - - if keypoints is not None: - new_keypoints = keypoint_ops.change_coordinate_frame(keypoints, im_box) - result.append(new_keypoints) - - return tuple(result) - - -def random_black_patches(image, - max_black_patches=10, - probability=0.5, - size_to_image_ratio=0.1, - random_seed=None, - preprocess_vars_cache=None): - """Randomly adds some black patches to the image. - - This op adds up to max_black_patches square black patches of a fixed size - to the image where size is specified via the size_to_image_ratio parameter. - - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - max_black_patches: number of times that the function tries to add a - black box to the image. - probability: at each try, what is the chance of adding a box. - size_to_image_ratio: Determines the ratio of the size of the black patches - to the size of the image. - box_size = size_to_image_ratio * - min(image_width, image_height) - random_seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image - """ - def add_black_patch_to_image(image, idx): - """Function for adding one patch to the image. - - Args: - image: image - idx: counter for number of patches that could have been added - - Returns: - image with a randomly added black box - """ - image_shape = tf.shape(image) - image_height = image_shape[0] - image_width = image_shape[1] - box_size = tf.cast( - tf.multiply( - tf.minimum( - tf.cast(image_height, dtype=tf.float32), - tf.cast(image_width, dtype=tf.float32)), size_to_image_ratio), - dtype=tf.int32) - - generator_func = functools.partial(tf.random_uniform, [], minval=0.0, - maxval=(1.0 - size_to_image_ratio), - seed=random_seed) - normalized_y_min = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.ADD_BLACK_PATCH, - preprocess_vars_cache, key=str(idx) + 'y') - normalized_x_min = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.ADD_BLACK_PATCH, - preprocess_vars_cache, key=str(idx) + 'x') - - y_min = tf.cast( - normalized_y_min * tf.cast(image_height, dtype=tf.float32), - dtype=tf.int32) - x_min = tf.cast( - normalized_x_min * tf.cast(image_width, dtype=tf.float32), - dtype=tf.int32) - black_box = tf.ones([box_size, box_size, 3], dtype=tf.float32) - mask = 1.0 - tf.image.pad_to_bounding_box(black_box, y_min, x_min, - image_height, image_width) - image = tf.multiply(image, mask) - return image - - with tf.name_scope('RandomBlackPatchInImage', values=[image]): - for idx in range(max_black_patches): - generator_func = functools.partial(tf.random_uniform, [], - minval=0.0, maxval=1.0, - dtype=tf.float32, seed=random_seed) - random_prob = _get_or_create_preprocess_rand_vars( - generator_func, - preprocessor_cache.PreprocessorCache.BLACK_PATCHES, - preprocess_vars_cache, key=idx) - image = tf.cond( - tf.greater(random_prob, probability), lambda: image, - functools.partial(add_black_patch_to_image, image=image, idx=idx)) - return image - - -def random_jpeg_quality(image, - min_jpeg_quality=0, - max_jpeg_quality=100, - random_coef=0.0, - seed=None, - preprocess_vars_cache=None): - """Randomly encode the image to a random JPEG quality level. - - Args: - image: rank 3 float32 tensor with shape [height, width, channels] and - values in the range [0, 255]. - min_jpeg_quality: An int for the lower bound for selecting a random jpeg - quality level. - max_jpeg_quality: An int for the upper bound for selecting a random jpeg - quality level. - random_coef: a random coefficient that defines the chance of getting the - original image. If random_coef is 0, we will always get the encoded image, - and if it is 1.0, we will always get the original image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this function is called - multiple times with the same non-null cache, it will perform - deterministically. - - Returns: - image: image which is the same shape as input image. - """ - def _adjust_jpeg_quality(): - """Encodes the image as jpeg with a random quality and then decodes.""" - generator_func = functools.partial( - tf.random_uniform, [], - minval=min_jpeg_quality, - maxval=max_jpeg_quality, - dtype=tf.int32, - seed=seed) - quality = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.JPEG_QUALITY, - preprocess_vars_cache, key='quality') - - # Need to convert to uint8 before calling adjust_jpeg_quality since it - # assumes that float features are in the range [0, 1], where herein the - # range is [0, 255]. - image_uint8 = tf.cast(image, tf.uint8) - adjusted_image = tf.image.adjust_jpeg_quality(image_uint8, quality) - return tf.cast(adjusted_image, tf.float32) - - with tf.name_scope('RandomJpegQuality', values=[image]): - generator_func = functools.partial(tf.random_uniform, [], seed=seed) - do_encoding_random = _get_or_create_preprocess_rand_vars( - generator_func, preprocessor_cache.PreprocessorCache.JPEG_QUALITY, - preprocess_vars_cache) - do_encoding_random = tf.greater_equal(do_encoding_random, random_coef) - image = tf.cond(do_encoding_random, _adjust_jpeg_quality, - lambda: tf.cast(image, tf.float32)) - - return image - - -def random_downscale_to_target_pixels(image, - masks=None, - min_target_pixels=300000, - max_target_pixels=800000, - random_coef=0.0, - seed=None, - preprocess_vars_cache=None): - """Randomly downscales the image to a target number of pixels. - - If the image contains less than the chosen target number of pixels, it will - not be downscaled. - - Args: - image: Rank 3 float32 tensor with shape [height, width, channels] and - values in the range [0, 255]. - masks: (optional) Rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks are of - the same height, width as the input `image`. - min_target_pixels: Integer. An inclusive lower bound for for the target - number of pixels. - max_target_pixels: Integer. An exclusive upper bound for for the target - number of pixels. - random_coef: Float. Random coefficient that defines the chance of getting - the original image. If random_coef is 0, we will always apply downscaling, - and if it is 1.0, we will always get the original image. - seed: (optional) Integer. Random seed. - preprocess_vars_cache: (optional) PreprocessorCache object that records - previously performed augmentations. Updated in-place. If this function is - called multiple times with the same non-null cache, it will perform - deterministically. - - Returns: - Tuple with elements: - image: Resized image which is the same rank as input image. - masks: If masks is not None, resized masks which are the same rank as - the input masks. - - Raises: - ValueError: If min_target_pixels or max_target_pixels are not positive. - """ - if min_target_pixels <= 0: - raise ValueError('Minimum target pixels must be positive') - if max_target_pixels <= 0: - raise ValueError('Maximum target pixels must be positive') - - def _resize_image_to_target(target_height, target_width): - # pylint: disable=unbalanced-tuple-unpacking - new_image, _ = resize_image(image, None, target_height, target_width) - return (new_image,) - - def _resize_image_and_masks_to_target(target_height, target_width): - # pylint: disable=unbalanced-tuple-unpacking - new_image, new_masks, _ = resize_image(image, masks, target_height, - target_width) - return new_image, new_masks - - with tf.name_scope('RandomDownscaleToTargetPixels', values=[image]): - generator_fn = functools.partial(tf.random_uniform, [], seed=seed) - do_downscale_random = _get_or_create_preprocess_rand_vars( - generator_fn, - preprocessor_cache.PreprocessorCache.DOWNSCALE_TO_TARGET_PIXELS, - preprocess_vars_cache) - do_downscale_random = tf.greater_equal(do_downscale_random, random_coef) - - generator_fn = functools.partial( - tf.random_uniform, [], - minval=min_target_pixels, - maxval=max_target_pixels, - dtype=tf.int32, - seed=seed) - target_pixels = _get_or_create_preprocess_rand_vars( - generator_fn, - preprocessor_cache.PreprocessorCache.DOWNSCALE_TO_TARGET_PIXELS, - preprocess_vars_cache, - key='target_pixels') - - image_shape = tf.shape(image) - image_height = image_shape[0] - image_width = image_shape[1] - image_pixels = image_height * image_width - scale_factor = tf.sqrt( - tf.cast(target_pixels, dtype=tf.float32) / - tf.cast(image_pixels, dtype=tf.float32)) - target_height = tf.cast( - scale_factor * tf.cast(image_height, dtype=tf.float32), dtype=tf.int32) - target_width = tf.cast( - scale_factor * tf.cast(image_width, dtype=tf.float32), dtype=tf.int32) - image_larger_than_target = tf.greater(image_pixels, target_pixels) - - should_apply_resize = tf.logical_and(do_downscale_random, - image_larger_than_target) - if masks is not None: - resize_fn = functools.partial(_resize_image_and_masks_to_target, - target_height, target_width) - return tf.cond(should_apply_resize, resize_fn, - lambda: (tf.cast(image, dtype=tf.float32), masks)) - else: - resize_fn = lambda: _resize_image_to_target(target_height, target_width) - return tf.cond(should_apply_resize, resize_fn, - lambda: (tf.cast(image, dtype=tf.float32),)) - - -def random_patch_gaussian(image, - min_patch_size=1, - max_patch_size=250, - min_gaussian_stddev=0.0, - max_gaussian_stddev=1.0, - random_coef=0.0, - seed=None, - preprocess_vars_cache=None): - """Randomly applies gaussian noise to a random patch on the image. - - The gaussian noise is applied to the image with values scaled to the range - [0.0, 1.0]. The result of applying gaussian noise to the scaled image is - clipped to be within the range [0.0, 1.0], equivalent to the range - [0.0, 255.0] after rescaling the image back. - - See "Improving Robustness Without Sacrificing Accuracy with Patch Gaussian - Augmentation " by Lopes et al., 2019, for further details. - https://arxiv.org/abs/1906.02611 - - Args: - image: Rank 3 float32 tensor with shape [height, width, channels] and - values in the range [0.0, 255.0]. - min_patch_size: Integer. An inclusive lower bound for the patch size. - max_patch_size: Integer. An exclusive upper bound for the patch size. - min_gaussian_stddev: Float. An inclusive lower bound for the standard - deviation of the gaussian noise. - max_gaussian_stddev: Float. An exclusive upper bound for the standard - deviation of the gaussian noise. - random_coef: Float. Random coefficient that defines the chance of getting - the original image. If random_coef is 0.0, we will always apply - downscaling, and if it is 1.0, we will always get the original image. - seed: (optional) Integer. Random seed. - preprocess_vars_cache: (optional) PreprocessorCache object that records - previously performed augmentations. Updated in-place. If this function is - called multiple times with the same non-null cache, it will perform - deterministically. - - Returns: - Rank 3 float32 tensor with same shape as the input image and with gaussian - noise applied within a random patch. - - Raises: - ValueError: If min_patch_size is < 1. - """ - if min_patch_size < 1: - raise ValueError('Minimum patch size must be >= 1.') - - get_or_create_rand_vars_fn = functools.partial( - _get_or_create_preprocess_rand_vars, - function_id=preprocessor_cache.PreprocessorCache.PATCH_GAUSSIAN, - preprocess_vars_cache=preprocess_vars_cache) - - def _apply_patch_gaussian(image): - """Applies a patch gaussian with random size, location, and stddev.""" - patch_size = get_or_create_rand_vars_fn( - functools.partial( - tf.random_uniform, [], - minval=min_patch_size, - maxval=max_patch_size, - dtype=tf.int32, - seed=seed), - key='patch_size') - gaussian_stddev = get_or_create_rand_vars_fn( - functools.partial( - tf.random_uniform, [], - minval=min_gaussian_stddev, - maxval=max_gaussian_stddev, - dtype=tf.float32, - seed=seed), - key='gaussian_stddev') - - image_shape = tf.shape(image) - y = get_or_create_rand_vars_fn( - functools.partial( - tf.random_uniform, [], - minval=0, - maxval=image_shape[0], - dtype=tf.int32, - seed=seed), - key='y') - x = get_or_create_rand_vars_fn( - functools.partial( - tf.random_uniform, [], - minval=0, - maxval=image_shape[1], - dtype=tf.int32, - seed=seed), - key='x') - gaussian = get_or_create_rand_vars_fn( - functools.partial( - tf.random.normal, - image_shape, - stddev=gaussian_stddev, - dtype=tf.float32, - seed=seed), - key='gaussian') - - scaled_image = image / 255.0 - image_plus_gaussian = tf.clip_by_value(scaled_image + gaussian, 0.0, 1.0) - patch_mask = patch_ops.get_patch_mask(y, x, patch_size, image_shape) - patch_mask = tf.expand_dims(patch_mask, -1) - patch_mask = tf.tile(patch_mask, [1, 1, image_shape[2]]) - patched_image = tf.where(patch_mask, image_plus_gaussian, scaled_image) - return patched_image * 255.0 - - with tf.name_scope('RandomPatchGaussian', values=[image]): - image = tf.cast(image, tf.float32) - patch_gaussian_random = get_or_create_rand_vars_fn( - functools.partial(tf.random_uniform, [], seed=seed)) - do_patch_gaussian = tf.greater_equal(patch_gaussian_random, random_coef) - image = tf.cond(do_patch_gaussian, - lambda: _apply_patch_gaussian(image), - lambda: image) - return image - - -def autoaugment_image(image, boxes, policy_name='v0'): - """Apply an autoaugment policy to the image and boxes. - - See "AutoAugment: Learning Augmentation Policies from Data" by Cubuk et al., - 2018, for further details. https://arxiv.org/abs/1805.09501 - - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 255]. - boxes: rank 2 float32 tensor containing the bounding boxes with shape - [num_instances, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - policy_name: The name of the AutoAugment policy to use. The available - options are `v0`, `v1`, `v2`, `v3` and `test`. `v0` is the policy used for - all of the results in the paper and was found to achieve the best results - on the COCO dataset. `v1`, `v2` and `v3` are additional good policies - found on the COCO dataset that have slight variation in what operations - were used during the search procedure along with how many operations are - applied in parallel to a single image (2 vs 3). - - - Returns: - image: the augmented image. - boxes: boxes which is the same rank as input boxes. Boxes are in normalized - form. boxes will have been augmented along with image. - """ - return autoaugment_utils.distort_image_with_autoaugment( - image, boxes, policy_name) - - -def image_to_float(image): - """Used in Faster R-CNN. Casts image pixel values to float. - - Args: - image: input image which might be in tf.uint8 or sth else format - - Returns: - image: image in tf.float32 format. - """ - with tf.name_scope('ImageToFloat', values=[image]): - image = tf.cast(image, dtype=tf.float32) - return image - - -def random_resize_method(image, target_size, preprocess_vars_cache=None): - """Uses a random resize method to resize the image to target size. - - Args: - image: a rank 3 tensor. - target_size: a list of [target_height, target_width] - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - resized image. - """ - - resized_image = _apply_with_random_selector( - image, - lambda x, method: tf.image.resize_images(x, target_size, method), - num_cases=4, - preprocess_vars_cache=preprocess_vars_cache, - key=preprocessor_cache.PreprocessorCache.RESIZE_METHOD) - - return resized_image - - -def resize_to_range(image, - masks=None, - min_dimension=None, - max_dimension=None, - method=tf.image.ResizeMethod.BILINEAR, - align_corners=False, - pad_to_max_dimension=False, - per_channel_pad_value=(0, 0, 0)): - """Resizes an image so its dimensions are within the provided value. - - The output size can be described by two cases: - 1. If the image can be rescaled so its minimum dimension is equal to the - provided value without the other dimension exceeding max_dimension, - then do so. - 2. Otherwise, resize so the largest dimension is equal to max_dimension. - - Args: - image: A 3D tensor of shape [height, width, channels] - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. - min_dimension: (optional) (scalar) desired size of the smaller image - dimension. - max_dimension: (optional) (scalar) maximum allowed size - of the larger image dimension. - method: (optional) interpolation method used in resizing. Defaults to - BILINEAR. - align_corners: bool. If true, exactly align all 4 corners of the input - and output. Defaults to False. - pad_to_max_dimension: Whether to resize the image and pad it with zeros - so the resulting image is of the spatial size - [max_dimension, max_dimension]. If masks are included they are padded - similarly. - per_channel_pad_value: A tuple of per-channel scalar value to use for - padding. By default pads zeros. - - Returns: - Note that the position of the resized_image_shape changes based on whether - masks are present. - resized_image: A 3D tensor of shape [new_height, new_width, channels], - where the image has been resized (with bilinear interpolation) so that - min(new_height, new_width) == min_dimension or - max(new_height, new_width) == max_dimension. - resized_masks: If masks is not None, also outputs masks. A 3D tensor of - shape [num_instances, new_height, new_width]. - resized_image_shape: A 1D tensor of shape [3] containing shape of the - resized image. - - Raises: - ValueError: if the image is not a 3D tensor. - """ - if len(image.get_shape()) != 3: - raise ValueError('Image should be 3D tensor') - - def _resize_landscape_image(image): - # resize a landscape image - return tf.image.resize_images( - image, tf.stack([min_dimension, max_dimension]), method=method, - align_corners=align_corners, preserve_aspect_ratio=True) - - def _resize_portrait_image(image): - # resize a portrait image - return tf.image.resize_images( - image, tf.stack([max_dimension, min_dimension]), method=method, - align_corners=align_corners, preserve_aspect_ratio=True) - - with tf.name_scope('ResizeToRange', values=[image, min_dimension]): - if image.get_shape().is_fully_defined(): - if image.get_shape()[0] < image.get_shape()[1]: - new_image = _resize_landscape_image(image) - else: - new_image = _resize_portrait_image(image) - new_size = tf.constant(new_image.get_shape().as_list()) - else: - new_image = tf.cond( - tf.less(tf.shape(image)[0], tf.shape(image)[1]), - lambda: _resize_landscape_image(image), - lambda: _resize_portrait_image(image)) - new_size = tf.shape(new_image) - - if pad_to_max_dimension: - channels = tf.unstack(new_image, axis=2) - if len(channels) != len(per_channel_pad_value): - raise ValueError('Number of channels must be equal to the length of ' - 'per-channel pad value.') - new_image = tf.stack( - [ - tf.pad( # pylint: disable=g-complex-comprehension - channels[i], [[0, max_dimension - new_size[0]], - [0, max_dimension - new_size[1]]], - constant_values=per_channel_pad_value[i]) - for i in range(len(channels)) - ], - axis=2) - new_image.set_shape([max_dimension, max_dimension, len(channels)]) - - result = [new_image] - if masks is not None: - new_masks = tf.expand_dims(masks, 3) - new_masks = tf.image.resize_images( - new_masks, - new_size[:-1], - method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, - align_corners=align_corners) - if pad_to_max_dimension: - new_masks = tf.image.pad_to_bounding_box( - new_masks, 0, 0, max_dimension, max_dimension) - new_masks = tf.squeeze(new_masks, 3) - result.append(new_masks) - - result.append(new_size) - return result - - -def _get_image_info(image): - """Returns the height, width and number of channels in the image.""" - image_height = tf.shape(image)[0] - image_width = tf.shape(image)[1] - num_channels = tf.shape(image)[2] - return (image_height, image_width, num_channels) - - -# TODO(alirezafathi): Make sure the static shapes are preserved. -def resize_to_min_dimension(image, masks=None, min_dimension=600, - method=tf.image.ResizeMethod.BILINEAR): - """Resizes image and masks given the min size maintaining the aspect ratio. - - If one of the image dimensions is smaller than min_dimension, it will scale - the image such that its smallest dimension is equal to min_dimension. - Otherwise, will keep the image size as is. - - Args: - image: a tensor of size [height, width, channels]. - masks: (optional) a tensors of size [num_instances, height, width]. - min_dimension: minimum image dimension. - method: (optional) interpolation method used in resizing. Defaults to - BILINEAR. - - Returns: - An array containing resized_image, resized_masks, and resized_image_shape. - Note that the position of the resized_image_shape changes based on whether - masks are present. - resized_image: A tensor of size [new_height, new_width, channels]. - resized_masks: If masks is not None, also outputs masks. A 3D tensor of - shape [num_instances, new_height, new_width] - resized_image_shape: A 1D tensor of shape [3] containing the shape of the - resized image. - - Raises: - ValueError: if the image is not a 3D tensor. - """ - if len(image.get_shape()) != 3: - raise ValueError('Image should be 3D tensor') - - with tf.name_scope('ResizeGivenMinDimension', values=[image, min_dimension]): - (image_height, image_width, num_channels) = _get_image_info(image) - min_image_dimension = tf.minimum(image_height, image_width) - min_target_dimension = tf.maximum(min_image_dimension, min_dimension) - target_ratio = tf.cast(min_target_dimension, dtype=tf.float32) / tf.cast( - min_image_dimension, dtype=tf.float32) - target_height = tf.cast( - tf.cast(image_height, dtype=tf.float32) * target_ratio, dtype=tf.int32) - target_width = tf.cast( - tf.cast(image_width, dtype=tf.float32) * target_ratio, dtype=tf.int32) - image = tf.image.resize_images( - tf.expand_dims(image, axis=0), size=[target_height, target_width], - method=method, - align_corners=True) - result = [tf.squeeze(image, axis=0)] - - if masks is not None: - masks = tf.image.resize_nearest_neighbor( - tf.expand_dims(masks, axis=3), - size=[target_height, target_width], - align_corners=True) - result.append(tf.squeeze(masks, axis=3)) - - result.append(tf.stack([target_height, target_width, num_channels])) - return result - - -def resize_to_max_dimension(image, masks=None, max_dimension=600, - method=tf.image.ResizeMethod.BILINEAR): - """Resizes image and masks given the max size maintaining the aspect ratio. - - If one of the image dimensions is greater than max_dimension, it will scale - the image such that its largest dimension is equal to max_dimension. - Otherwise, will keep the image size as is. - - Args: - image: a tensor of size [height, width, channels]. - masks: (optional) a tensors of size [num_instances, height, width]. - max_dimension: maximum image dimension. - method: (optional) interpolation method used in resizing. Defaults to - BILINEAR. - - Returns: - An array containing resized_image, resized_masks, and resized_image_shape. - Note that the position of the resized_image_shape changes based on whether - masks are present. - resized_image: A tensor of size [new_height, new_width, channels]. - resized_masks: If masks is not None, also outputs masks. A 3D tensor of - shape [num_instances, new_height, new_width] - resized_image_shape: A 1D tensor of shape [3] containing the shape of the - resized image. - - Raises: - ValueError: if the image is not a 3D tensor. - """ - if len(image.get_shape()) != 3: - raise ValueError('Image should be 3D tensor') - - with tf.name_scope('ResizeGivenMaxDimension', values=[image, max_dimension]): - (image_height, image_width, num_channels) = _get_image_info(image) - max_image_dimension = tf.maximum(image_height, image_width) - max_target_dimension = tf.minimum(max_image_dimension, max_dimension) - target_ratio = tf.cast(max_target_dimension, dtype=tf.float32) / tf.cast( - max_image_dimension, dtype=tf.float32) - target_height = tf.cast( - tf.cast(image_height, dtype=tf.float32) * target_ratio, dtype=tf.int32) - target_width = tf.cast( - tf.cast(image_width, dtype=tf.float32) * target_ratio, dtype=tf.int32) - image = tf.image.resize_images( - tf.expand_dims(image, axis=0), size=[target_height, target_width], - method=method, - align_corners=True) - result = [tf.squeeze(image, axis=0)] - - if masks is not None: - masks = tf.image.resize_nearest_neighbor( - tf.expand_dims(masks, axis=3), - size=[target_height, target_width], - align_corners=True) - result.append(tf.squeeze(masks, axis=3)) - - result.append(tf.stack([target_height, target_width, num_channels])) - return result - - -def resize_pad_to_multiple(image, masks=None, multiple=1): - """Resize an image by zero padding it to the specified multiple. - - For example, with an image of size (101, 199, 3) and multiple=4, - the returned image will have shape (104, 200, 3). - - Args: - image: a tensor of shape [height, width, channels] - masks: (optional) a tensor of shape [num_instances, height, width] - multiple: int, the multiple to which the height and width of the input - will be padded. - - Returns: - resized_image: The image with 0 padding applied, such that output - dimensions are divisible by `multiple` - resized_masks: If masks are given, they are resized to the same - spatial dimensions as the image. - resized_image_shape: An integer tensor of shape [3] which holds - the shape of the input image. - - """ - - if len(image.get_shape()) != 3: - raise ValueError('Image should be 3D tensor') - - with tf.name_scope('ResizePadToMultiple', values=[image, multiple]): - image_height, image_width, num_channels = _get_image_info(image) - image = image[tf.newaxis, :, :, :] - image = ops.pad_to_multiple(image, multiple)[0, :, :, :] - result = [image] - - if masks is not None: - masks = tf.transpose(masks, (1, 2, 0)) - masks = masks[tf.newaxis, :, :, :] - - masks = ops.pad_to_multiple(masks, multiple)[0, :, :, :] - masks = tf.transpose(masks, (2, 0, 1)) - result.append(masks) - - result.append(tf.stack([image_height, image_width, num_channels])) - return result - - -def scale_boxes_to_pixel_coordinates(image, boxes, keypoints=None): - """Scales boxes from normalized to pixel coordinates. - - Args: - image: A 3D float32 tensor of shape [height, width, channels]. - boxes: A 2D float32 tensor of shape [num_boxes, 4] containing the bounding - boxes in normalized coordinates. Each row is of the form - [ymin, xmin, ymax, xmax]. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x normalized - coordinates. - - Returns: - image: unchanged input image. - scaled_boxes: a 2D float32 tensor of shape [num_boxes, 4] containing the - bounding boxes in pixel coordinates. - scaled_keypoints: a 3D float32 tensor with shape - [num_instances, num_keypoints, 2] containing the keypoints in pixel - coordinates. - """ - boxlist = box_list.BoxList(boxes) - image_height = tf.shape(image)[0] - image_width = tf.shape(image)[1] - scaled_boxes = box_list_ops.scale(boxlist, image_height, image_width).get() - result = [image, scaled_boxes] - if keypoints is not None: - scaled_keypoints = keypoint_ops.scale(keypoints, image_height, image_width) - result.append(scaled_keypoints) - return tuple(result) - - -# TODO(alirezafathi): Investigate if instead the function should return None if -# masks is None. -# pylint: disable=g-doc-return-or-yield -def resize_image(image, - masks=None, - new_height=600, - new_width=1024, - method=tf.image.ResizeMethod.BILINEAR, - align_corners=False): - """Resizes images to the given height and width. - - Args: - image: A 3D tensor of shape [height, width, channels] - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. - new_height: (optional) (scalar) desired height of the image. - new_width: (optional) (scalar) desired width of the image. - method: (optional) interpolation method used in resizing. Defaults to - BILINEAR. - align_corners: bool. If true, exactly align all 4 corners of the input - and output. Defaults to False. - - Returns: - Note that the position of the resized_image_shape changes based on whether - masks are present. - resized_image: A tensor of size [new_height, new_width, channels]. - resized_masks: If masks is not None, also outputs masks. A 3D tensor of - shape [num_instances, new_height, new_width] - resized_image_shape: A 1D tensor of shape [3] containing the shape of the - resized image. - """ - with tf.name_scope( - 'ResizeImage', - values=[image, new_height, new_width, method, align_corners]): - new_image = tf.image.resize_images( - image, tf.stack([new_height, new_width]), - method=method, - align_corners=align_corners) - image_shape = shape_utils.combined_static_and_dynamic_shape(image) - result = [new_image] - if masks is not None: - num_instances = tf.shape(masks)[0] - new_size = tf.stack([new_height, new_width]) - def resize_masks_branch(): - new_masks = tf.expand_dims(masks, 3) - new_masks = tf.image.resize_nearest_neighbor( - new_masks, new_size, align_corners=align_corners) - new_masks = tf.squeeze(new_masks, axis=3) - return new_masks - - def reshape_masks_branch(): - # The shape function will be computed for both branches of the - # condition, regardless of which branch is actually taken. Make sure - # that we don't trigger an assertion in the shape function when trying - # to reshape a non empty tensor into an empty one. - new_masks = tf.reshape(masks, [-1, new_size[0], new_size[1]]) - return new_masks - - masks = tf.cond(num_instances > 0, resize_masks_branch, - reshape_masks_branch) - result.append(masks) - - result.append(tf.stack([new_height, new_width, image_shape[2]])) - return result - - -def subtract_channel_mean(image, means=None): - """Normalizes an image by subtracting a mean from each channel. - - Args: - image: A 3D tensor of shape [height, width, channels] - means: float list containing a mean for each channel - Returns: - normalized_images: a tensor of shape [height, width, channels] - Raises: - ValueError: if images is not a 4D tensor or if the number of means is not - equal to the number of channels. - """ - with tf.name_scope('SubtractChannelMean', values=[image, means]): - if len(image.get_shape()) != 3: - raise ValueError('Input must be of size [height, width, channels]') - if len(means) != image.get_shape()[-1]: - raise ValueError('len(means) must match the number of channels') - return image - [[means]] - - -def one_hot_encoding(labels, num_classes=None): - """One-hot encodes the multiclass labels. - - Example usage: - labels = tf.constant([1, 4], dtype=tf.int32) - one_hot = OneHotEncoding(labels, num_classes=5) - one_hot.eval() # evaluates to [0, 1, 0, 0, 1] - - Args: - labels: A tensor of shape [None] corresponding to the labels. - num_classes: Number of classes in the dataset. - Returns: - onehot_labels: a tensor of shape [num_classes] corresponding to the one hot - encoding of the labels. - Raises: - ValueError: if num_classes is not specified. - """ - with tf.name_scope('OneHotEncoding', values=[labels]): - if num_classes is None: - raise ValueError('num_classes must be specified') - - labels = tf.one_hot(labels, num_classes, 1, 0) - return tf.reduce_max(labels, 0) - - -def rgb_to_gray(image): - """Converts a 3 channel RGB image to a 1 channel grayscale image. - - Args: - image: Rank 3 float32 tensor containing 1 image -> [height, width, 3] - with pixel values varying between [0, 1]. - - Returns: - image: A single channel grayscale image -> [image, height, 1]. - """ - return _rgb_to_grayscale(image) - - -def random_self_concat_image( - image, boxes, labels, label_weights, label_confidences=None, - multiclass_scores=None, concat_vertical_probability=0.1, - concat_horizontal_probability=0.1, seed=None, - preprocess_vars_cache=None): - """Randomly concatenates the image with itself. - - This function randomly concatenates the image with itself; the random - variables for vertical and horizontal concatenation are independent. - Afterwards, we adjust the old bounding boxes, and add new bounding boxes - for the new objects. - - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: rank 1 float32 containing the label weights. - label_confidences: (optional) rank 1 float32 containing the label - confidences. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for - each box for each class. - concat_vertical_probability: (optional) a tf.float32 scalar denoting the - probability of a vertical concatenation. - concat_horizontal_probability: (optional) a tf.float32 scalar denoting the - probability of a horizontal concatenation. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: Image shape will be [new_height, new_width, channels]. - boxes: boxes which is the same rank as input boxes. Boxes are in normalized - form. - if label_confidences is not None also returns: - maybe_concat_label_confidences: cropped label weights. - if multiclass_scores is not None also returns: - maybe_concat_multiclass_scores: cropped_multiclass_scores. - """ - - concat_vertical = (tf.random_uniform([], seed=seed) < - concat_vertical_probability) - # Note the seed + 1 so we get some semblance of independence even with - # fixed seeds. - concat_horizontal = (tf.random_uniform([], seed=seed + 1 if seed else None) - < concat_horizontal_probability) - - gen_func = lambda: (concat_vertical, concat_horizontal) - params = _get_or_create_preprocess_rand_vars( - gen_func, preprocessor_cache.PreprocessorCache.SELF_CONCAT_IMAGE, - preprocess_vars_cache) - concat_vertical, concat_horizontal = params - - def _concat_image(image, boxes, labels, label_weights, axis): - """Concats the image to itself on `axis`.""" - output_images = tf.concat([image, image], axis=axis) - - if axis == 0: - # Concat vertically, so need to reduce the y coordinates. - old_scaling = tf.constant([0.5, 1.0, 0.5, 1.0]) - new_translation = tf.constant([0.5, 0.0, 0.5, 0.0]) - elif axis == 1: - old_scaling = tf.constant([1.0, 0.5, 1.0, 0.5]) - new_translation = tf.constant([0.0, 0.5, 0.0, 0.5]) - - old_boxes = old_scaling * boxes - new_boxes = old_boxes + new_translation - all_boxes = tf.concat([old_boxes, new_boxes], axis=0) - - return [output_images, all_boxes, tf.tile(labels, [2]), tf.tile( - label_weights, [2])] - - image, boxes, labels, label_weights = tf.cond( - concat_vertical, - lambda: _concat_image(image, boxes, labels, label_weights, axis=0), - lambda: [image, boxes, labels, label_weights], - strict=True) - - outputs = tf.cond( - concat_horizontal, - lambda: _concat_image(image, boxes, labels, label_weights, axis=1), - lambda: [image, boxes, labels, label_weights], - strict=True) - - if label_confidences is not None: - label_confidences = tf.cond(concat_vertical, - lambda: tf.tile(label_confidences, [2]), - lambda: label_confidences) - outputs.append(tf.cond(concat_horizontal, - lambda: tf.tile(label_confidences, [2]), - lambda: label_confidences)) - - if multiclass_scores is not None: - multiclass_scores = tf.cond(concat_vertical, - lambda: tf.tile(multiclass_scores, [2, 1]), - lambda: multiclass_scores) - outputs.append(tf.cond(concat_horizontal, - lambda: tf.tile(multiclass_scores, [2, 1]), - lambda: multiclass_scores)) - - return outputs - - -def ssd_random_crop(image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - keypoints=None, - min_object_covered=(0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - aspect_ratio_range=((0.5, 2.0),) * 7, - area_range=((0.1, 1.0),) * 7, - overlap_thresh=(0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - clip_boxes=(True,) * 7, - random_coef=(0.15,) * 7, - seed=None, - preprocess_vars_cache=None): - """Random crop preprocessing with default parameters as in SSD paper. - - Liu et al., SSD: Single shot multibox detector. - For further information on random crop preprocessing refer to RandomCrop - function above. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: rank 1 float32 tensor containing the weights. - label_confidences: rank 1 float32 tensor containing the confidences. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - min_object_covered: the cropped image must cover at least this fraction of - at least one of the input bounding boxes. - aspect_ratio_range: allowed range for aspect ratio of cropped image. - area_range: allowed range for area ratio between cropped image and the - original image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - random_coef: a random coefficient that defines the chance of getting the - original image. If random_coef is 0, we will always get the - cropped image, and if it is 1.0, we will always get the - original image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - - If label_weights, multiclass_scores, masks, or keypoints is not None, the - function also returns: - label_weights: rank 1 float32 tensor with shape [num_instances]. - multiclass_scores: rank 2 float32 tensor with shape - [num_instances, num_classes] - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - """ - - def random_crop_selector(selected_result, index): - """Applies random_crop_image to selected result. - - Args: - selected_result: A tuple containing image, boxes, labels, keypoints (if - not None), and masks (if not None). - index: The index that was randomly selected. - - Returns: A tuple containing image, boxes, labels, keypoints (if not None), - and masks (if not None). - """ - - i = 3 - image, boxes, labels = selected_result[:i] - selected_label_weights = None - selected_label_confidences = None - selected_multiclass_scores = None - selected_masks = None - selected_keypoints = None - if label_weights is not None: - selected_label_weights = selected_result[i] - i += 1 - if label_confidences is not None: - selected_label_confidences = selected_result[i] - i += 1 - if multiclass_scores is not None: - selected_multiclass_scores = selected_result[i] - i += 1 - if masks is not None: - selected_masks = selected_result[i] - i += 1 - if keypoints is not None: - selected_keypoints = selected_result[i] - - return random_crop_image( - image=image, - boxes=boxes, - labels=labels, - label_weights=selected_label_weights, - label_confidences=selected_label_confidences, - multiclass_scores=selected_multiclass_scores, - masks=selected_masks, - keypoints=selected_keypoints, - min_object_covered=min_object_covered[index], - aspect_ratio_range=aspect_ratio_range[index], - area_range=area_range[index], - overlap_thresh=overlap_thresh[index], - clip_boxes=clip_boxes[index], - random_coef=random_coef[index], - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - - result = _apply_with_random_selector_tuples( - tuple( - t for t in (image, boxes, labels, label_weights, label_confidences, - multiclass_scores, masks, keypoints) if t is not None), - random_crop_selector, - num_cases=len(min_object_covered), - preprocess_vars_cache=preprocess_vars_cache, - key=preprocessor_cache.PreprocessorCache.SSD_CROP_SELECTOR_ID) - return result - - -def ssd_random_crop_pad(image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - min_object_covered=(0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - aspect_ratio_range=((0.5, 2.0),) * 6, - area_range=((0.1, 1.0),) * 6, - overlap_thresh=(0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - clip_boxes=(True,) * 6, - random_coef=(0.15,) * 6, - min_padded_size_ratio=((1.0, 1.0),) * 6, - max_padded_size_ratio=((2.0, 2.0),) * 6, - pad_color=(None,) * 6, - seed=None, - preprocess_vars_cache=None): - """Random crop preprocessing with default parameters as in SSD paper. - - Liu et al., SSD: Single shot multibox detector. - For further information on random crop preprocessing refer to RandomCrop - function above. - - Args: - image: rank 3 float32 tensor containing 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - label_confidences: float32 tensor of shape [num_instances] representing the - confidences for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - min_object_covered: the cropped image must cover at least this fraction of - at least one of the input bounding boxes. - aspect_ratio_range: allowed range for aspect ratio of cropped image. - area_range: allowed range for area ratio between cropped image and the - original image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - random_coef: a random coefficient that defines the chance of getting the - original image. If random_coef is 0, we will always get the - cropped image, and if it is 1.0, we will always get the - original image. - min_padded_size_ratio: min ratio of padded image height and width to the - input image's height and width. - max_padded_size_ratio: max ratio of padded image height and width to the - input image's height and width. - pad_color: padding color. A rank 1 tensor of [3] with dtype=tf.float32. - if set as None, it will be set to average color of the randomly - cropped image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: Image shape will be [new_height, new_width, channels]. - boxes: boxes which is the same rank as input boxes. Boxes are in normalized - form. - new_labels: new labels. - new_label_weights: new label weights. - """ - - def random_crop_pad_selector(image_boxes_labels, index): - """Random crop preprocessing helper.""" - i = 3 - image, boxes, labels = image_boxes_labels[:i] - selected_label_weights = None - selected_label_confidences = None - selected_multiclass_scores = None - if label_weights is not None: - selected_label_weights = image_boxes_labels[i] - i += 1 - if label_confidences is not None: - selected_label_confidences = image_boxes_labels[i] - i += 1 - if multiclass_scores is not None: - selected_multiclass_scores = image_boxes_labels[i] - - return random_crop_pad_image( - image, - boxes, - labels, - label_weights=selected_label_weights, - label_confidences=selected_label_confidences, - multiclass_scores=selected_multiclass_scores, - min_object_covered=min_object_covered[index], - aspect_ratio_range=aspect_ratio_range[index], - area_range=area_range[index], - overlap_thresh=overlap_thresh[index], - clip_boxes=clip_boxes[index], - random_coef=random_coef[index], - min_padded_size_ratio=min_padded_size_ratio[index], - max_padded_size_ratio=max_padded_size_ratio[index], - pad_color=pad_color[index], - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - - return _apply_with_random_selector_tuples( - tuple(t for t in (image, boxes, labels, label_weights, label_confidences, - multiclass_scores) if t is not None), - random_crop_pad_selector, - num_cases=len(min_object_covered), - preprocess_vars_cache=preprocess_vars_cache, - key=preprocessor_cache.PreprocessorCache.SSD_CROP_PAD_SELECTOR_ID) - - -def ssd_random_crop_fixed_aspect_ratio( - image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - keypoints=None, - min_object_covered=(0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - aspect_ratio=1.0, - area_range=((0.1, 1.0),) * 7, - overlap_thresh=(0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - clip_boxes=(True,) * 7, - random_coef=(0.15,) * 7, - seed=None, - preprocess_vars_cache=None): - """Random crop preprocessing with default parameters as in SSD paper. - - Liu et al., SSD: Single shot multibox detector. - For further information on random crop preprocessing refer to RandomCrop - function above. - - The only difference is that the aspect ratio of the crops are fixed. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - label_confidences: (optional) float32 tensor of shape [num_instances] - representing the confidences for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - min_object_covered: the cropped image must cover at least this fraction of - at least one of the input bounding boxes. - aspect_ratio: aspect ratio of the cropped image. - area_range: allowed range for area ratio between cropped image and the - original image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - random_coef: a random coefficient that defines the chance of getting the - original image. If random_coef is 0, we will always get the - cropped image, and if it is 1.0, we will always get the - original image. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - - If multiclass_scores, masks, or keypoints is not None, the function also - returns: - - multiclass_scores: rank 2 float32 tensor with shape - [num_instances, num_classes] - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - """ - aspect_ratio_range = ((aspect_ratio, aspect_ratio),) * len(area_range) - - crop_result = ssd_random_crop( - image, - boxes, - labels, - label_weights=label_weights, - label_confidences=label_confidences, - multiclass_scores=multiclass_scores, - masks=masks, - keypoints=keypoints, - min_object_covered=min_object_covered, - aspect_ratio_range=aspect_ratio_range, - area_range=area_range, - overlap_thresh=overlap_thresh, - clip_boxes=clip_boxes, - random_coef=random_coef, - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - i = 3 - new_image, new_boxes, new_labels = crop_result[:i] - new_label_weights = None - new_label_confidences = None - new_multiclass_scores = None - new_masks = None - new_keypoints = None - if label_weights is not None: - new_label_weights = crop_result[i] - i += 1 - if label_confidences is not None: - new_label_confidences = crop_result[i] - i += 1 - if multiclass_scores is not None: - new_multiclass_scores = crop_result[i] - i += 1 - if masks is not None: - new_masks = crop_result[i] - i += 1 - if keypoints is not None: - new_keypoints = crop_result[i] - - result = random_crop_to_aspect_ratio( - new_image, - new_boxes, - new_labels, - label_weights=new_label_weights, - label_confidences=new_label_confidences, - multiclass_scores=new_multiclass_scores, - masks=new_masks, - keypoints=new_keypoints, - aspect_ratio=aspect_ratio, - clip_boxes=clip_boxes, - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - - return result - - -def ssd_random_crop_pad_fixed_aspect_ratio( - image, - boxes, - labels, - label_weights, - label_confidences=None, - multiclass_scores=None, - masks=None, - keypoints=None, - min_object_covered=(0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - aspect_ratio=1.0, - aspect_ratio_range=((0.5, 2.0),) * 7, - area_range=((0.1, 1.0),) * 7, - overlap_thresh=(0.0, 0.1, 0.3, 0.5, 0.7, 0.9, 1.0), - clip_boxes=(True,) * 7, - random_coef=(0.15,) * 7, - min_padded_size_ratio=(1.0, 1.0), - max_padded_size_ratio=(2.0, 2.0), - seed=None, - preprocess_vars_cache=None): - """Random crop and pad preprocessing with default parameters as in SSD paper. - - Liu et al., SSD: Single shot multibox detector. - For further information on random crop preprocessing refer to RandomCrop - function above. - - The only difference is that after the initial crop, images are zero-padded - to a fixed aspect ratio instead of being resized to that aspect ratio. - - Args: - image: rank 3 float32 tensor contains 1 image -> [height, width, channels] - with pixel values varying between [0, 1]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. - Each row is in the form of [ymin, xmin, ymax, xmax]. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - label_confidences: (optional) float32 tensor of shape [num_instances] - representing the confidence for each box. - multiclass_scores: (optional) float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x - normalized coordinates. - min_object_covered: the cropped image must cover at least this fraction of - at least one of the input bounding boxes. - aspect_ratio: the final aspect ratio to pad to. - aspect_ratio_range: allowed range for aspect ratio of cropped image. - area_range: allowed range for area ratio between cropped image and the - original image. - overlap_thresh: minimum overlap thresh with new cropped - image to keep the box. - clip_boxes: whether to clip the boxes to the cropped image. - random_coef: a random coefficient that defines the chance of getting the - original image. If random_coef is 0, we will always get the - cropped image, and if it is 1.0, we will always get the - original image. - min_padded_size_ratio: min ratio of padded image height and width to the - input image's height and width. - max_padded_size_ratio: max ratio of padded image height and width to the - input image's height and width. - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - - If multiclass_scores, masks, or keypoints is not None, the function also - returns: - - multiclass_scores: rank 2 with shape [num_instances, num_classes] - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - keypoints: rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2] - """ - crop_result = ssd_random_crop( - image, - boxes, - labels, - label_weights=label_weights, - label_confidences=label_confidences, - multiclass_scores=multiclass_scores, - masks=masks, - keypoints=keypoints, - min_object_covered=min_object_covered, - aspect_ratio_range=aspect_ratio_range, - area_range=area_range, - overlap_thresh=overlap_thresh, - clip_boxes=clip_boxes, - random_coef=random_coef, - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - i = 3 - new_image, new_boxes, new_labels = crop_result[:i] - new_label_weights = None - new_label_confidences = None - new_multiclass_scores = None - new_masks = None - new_keypoints = None - if label_weights is not None: - new_label_weights = crop_result[i] - i += 1 - if label_confidences is not None: - new_label_confidences = crop_result[i] - i += 1 - if multiclass_scores is not None: - new_multiclass_scores = crop_result[i] - i += 1 - if masks is not None: - new_masks = crop_result[i] - i += 1 - if keypoints is not None: - new_keypoints = crop_result[i] - - result = random_pad_to_aspect_ratio( - new_image, - new_boxes, - masks=new_masks, - keypoints=new_keypoints, - aspect_ratio=aspect_ratio, - min_padded_size_ratio=min_padded_size_ratio, - max_padded_size_ratio=max_padded_size_ratio, - seed=seed, - preprocess_vars_cache=preprocess_vars_cache) - - result = list(result) - i = 3 - result.insert(2, new_labels) - if new_label_weights is not None: - result.insert(i, new_label_weights) - i += 1 - if new_label_confidences is not None: - result.insert(i, new_label_confidences) - i += 1 - if multiclass_scores is not None: - result.insert(i, new_multiclass_scores) - result = tuple(result) - - return result - - -def convert_class_logits_to_softmax(multiclass_scores, temperature=1.0): - """Converts multiclass logits to softmax scores after applying temperature. - - Args: - multiclass_scores: float32 tensor of shape - [num_instances, num_classes] representing the score for each box for each - class. - temperature: Scale factor to use prior to applying softmax. Larger - temperatures give more uniform distruibutions after softmax. - - Returns: - multiclass_scores: float32 tensor of shape - [num_instances, num_classes] with scaling and softmax applied. - """ - - # Multiclass scores must be stored as logits. Apply temp and softmax. - multiclass_scores_scaled = tf.multiply( - multiclass_scores, 1.0 / temperature, name='scale_logits') - multiclass_scores = tf.nn.softmax(multiclass_scores_scaled, name='softmax') - - return multiclass_scores - - -def _get_crop_border(border, size): - border = tf.cast(border, tf.float32) - size = tf.cast(size, tf.float32) - - i = tf.ceil(tf.log(2.0 * border / size) / tf.log(2.0)) - divisor = tf.pow(2.0, i) - divisor = tf.clip_by_value(divisor, 1, border) - divisor = tf.cast(divisor, tf.int32) - - return tf.cast(border, tf.int32) // divisor - - -def random_square_crop_by_scale(image, boxes, labels, label_weights, - label_confidences=None, masks=None, - keypoints=None, max_border=128, scale_min=0.6, - scale_max=1.3, num_scales=8, seed=None, - preprocess_vars_cache=None): - """Randomly crop a square in proportion to scale and image size. - - Extract a square sized crop from an image whose side length is sampled by - randomly scaling the maximum spatial dimension of the image. If part of - the crop falls outside the image, it is filled with zeros. - The augmentation is borrowed from [1] - [1]: https://arxiv.org/abs/1904.07850 - - Args: - image: rank 3 float32 tensor containing 1 image -> - [height, width, channels]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning their coordinates vary - between [0, 1]. Each row is in the form of [ymin, xmin, ymax, xmax]. - Boxes on the crop boundary are clipped to the boundary and boxes - falling outside the crop are ignored. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - label_confidences: (optional) float32 tensor of shape [num_instances] - representing the confidence for each box. - masks: (optional) rank 3 float32 tensor with shape - [num_instances, height, width] containing instance masks. The masks - are of the same height, width as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape - [num_instances, num_keypoints, 2]. The keypoints are in y-x normalized - coordinates. - max_border: The maximum size of the border. The border defines distance in - pixels to the image boundaries that will not be considered as a center of - a crop. To make sure that the border does not go over the center of the - image, we chose the border value by computing the minimum k, such that - (max_border / (2**k)) < image_dimension/2. - scale_min: float, the minimum value for scale. - scale_max: float, the maximum value for scale. - num_scales: int, the number of discrete scale values to sample between - [scale_min, scale_max] - seed: random seed. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - label_weights: rank 1 float32 tensor with shape [num_instances]. - label_confidences: (optional) float32 tensor of shape [num_instances] - representing the confidence for each box. - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - - """ - - img_shape = tf.shape(image) - height, width = img_shape[0], img_shape[1] - scales = tf.linspace(scale_min, scale_max, num_scales) - - scale = _get_or_create_preprocess_rand_vars( - lambda: scales[_random_integer(0, num_scales, seed)], - preprocessor_cache.PreprocessorCache.SQUARE_CROP_BY_SCALE, - preprocess_vars_cache, 'scale') - - image_size = scale * tf.cast(tf.maximum(height, width), tf.float32) - image_size = tf.cast(image_size, tf.int32) - h_border = _get_crop_border(max_border, height) - w_border = _get_crop_border(max_border, width) - - def y_function(): - y = _random_integer(h_border, - tf.cast(height, tf.int32) - h_border + 1, - seed) - return y - - def x_function(): - x = _random_integer(w_border, - tf.cast(width, tf.int32) - w_border + 1, - seed) - return x - - y_center = _get_or_create_preprocess_rand_vars( - y_function, - preprocessor_cache.PreprocessorCache.SQUARE_CROP_BY_SCALE, - preprocess_vars_cache, 'y_center') - - x_center = _get_or_create_preprocess_rand_vars( - x_function, - preprocessor_cache.PreprocessorCache.SQUARE_CROP_BY_SCALE, - preprocess_vars_cache, 'x_center') - - half_size = tf.cast(image_size / 2, tf.int32) - crop_ymin, crop_ymax = y_center - half_size, y_center + half_size - crop_xmin, crop_xmax = x_center - half_size, x_center + half_size - - ymin = tf.maximum(crop_ymin, 0) - xmin = tf.maximum(crop_xmin, 0) - ymax = tf.minimum(crop_ymax, height - 1) - xmax = tf.minimum(crop_xmax, width - 1) - - cropped_image = image[ymin:ymax, xmin:xmax] - offset_y = tf.maximum(0, ymin - crop_ymin) - offset_x = tf.maximum(0, xmin - crop_xmin) - - oy_i = offset_y - ox_i = offset_x - - output_image = tf.image.pad_to_bounding_box( - cropped_image, offset_height=oy_i, offset_width=ox_i, - target_height=image_size, target_width=image_size) - - if ymin == 0: - # We might be padding the image. - box_ymin = -offset_y - else: - box_ymin = crop_ymin - - if xmin == 0: - # We might be padding the image. - box_xmin = -offset_x - else: - box_xmin = crop_xmin - - box_ymax = box_ymin + image_size - box_xmax = box_xmin + image_size - - image_box = [box_ymin / height, box_xmin / width, - box_ymax / height, box_xmax / width] - boxlist = box_list.BoxList(boxes) - boxlist = box_list_ops.change_coordinate_frame(boxlist, image_box) - boxlist, indices = box_list_ops.prune_completely_outside_window( - boxlist, [0.0, 0.0, 1.0, 1.0]) - boxlist = box_list_ops.clip_to_window(boxlist, [0.0, 0.0, 1.0, 1.0], - filter_nonoverlapping=False) - - return_values = [output_image, boxlist.get(), - tf.gather(labels, indices), - tf.gather(label_weights, indices)] - - if label_confidences is not None: - return_values.append(tf.gather(label_confidences, indices)) - - if masks is not None: - new_masks = tf.expand_dims(masks, -1) - new_masks = new_masks[:, ymin:ymax, xmin:xmax] - new_masks = tf.image.pad_to_bounding_box( - new_masks, oy_i, ox_i, image_size, image_size) - new_masks = tf.squeeze(new_masks, [-1]) - return_values.append(tf.gather(new_masks, indices)) - - if keypoints is not None: - keypoints = tf.gather(keypoints, indices) - keypoints = keypoint_ops.change_coordinate_frame(keypoints, image_box) - keypoints = keypoint_ops.prune_outside_window(keypoints, - [0.0, 0.0, 1.0, 1.0]) - return_values.append(keypoints) - - return return_values - - -def random_scale_crop_and_pad_to_square( - image, - boxes, - labels, - label_weights, - masks=None, - keypoints=None, - label_confidences=None, - scale_min=0.1, - scale_max=2.0, - output_size=512, - resize_method=tf.image.ResizeMethod.BILINEAR, - seed=None): - """Randomly scale, crop, and then pad an image to fixed square dimensions. - - Randomly scale, crop, and then pad an image to the desired square output - dimensions. Specifically, this method first samples a random_scale factor - from a uniform distribution between scale_min and scale_max, and then resizes - the image such that it's maximum dimension is (output_size * random_scale). - Secondly, a square output_size crop is extracted from the resized image - (note, this will only occur when random_scale > 1.0). Lastly, the cropped - region is padded to the desired square output_size, by filling with zeros. - The augmentation is borrowed from [1] - [1]: https://arxiv.org/abs/1911.09070 - - Args: - image: rank 3 float32 tensor containing 1 image -> - [height, width, channels]. - boxes: rank 2 float32 tensor containing the bounding boxes -> [N, 4]. Boxes - are in normalized form meaning their coordinates vary between [0, 1]. Each - row is in the form of [ymin, xmin, ymax, xmax]. Boxes on the crop boundary - are clipped to the boundary and boxes falling outside the crop are - ignored. - labels: rank 1 int32 tensor containing the object classes. - label_weights: float32 tensor of shape [num_instances] representing the - weight for each box. - masks: (optional) rank 3 float32 tensor with shape [num_instances, height, - width] containing instance masks. The masks are of the same height, width - as the input `image`. - keypoints: (optional) rank 3 float32 tensor with shape [num_instances, - num_keypoints, 2]. The keypoints are in y-x normalized coordinates. - label_confidences: (optional) float32 tensor of shape [num_instance] - representing the confidence for each box. - scale_min: float, the minimum value for the random scale factor. - scale_max: float, the maximum value for the random scale factor. - output_size: int, the desired (square) output image size. - resize_method: tf.image.ResizeMethod, resize method to use when scaling the - input images. - seed: random seed. - - Returns: - image: image which is the same rank as input image. - boxes: boxes which is the same rank as input boxes. - Boxes are in normalized form. - labels: new labels. - label_weights: rank 1 float32 tensor with shape [num_instances]. - masks: rank 3 float32 tensor with shape [num_instances, height, width] - containing instance masks. - label_confidences: confidences for retained boxes. - """ - img_shape = tf.shape(image) - input_height, input_width = img_shape[0], img_shape[1] - random_scale = tf.random_uniform([], scale_min, scale_max, seed=seed) - - # Compute the scaled height and width from the random scale. - max_input_dim = tf.cast(tf.maximum(input_height, input_width), tf.float32) - input_ar_y = tf.cast(input_height, tf.float32) / max_input_dim - input_ar_x = tf.cast(input_width, tf.float32) / max_input_dim - scaled_height = tf.cast(random_scale * output_size * input_ar_y, tf.int32) - scaled_width = tf.cast(random_scale * output_size * input_ar_x, tf.int32) - - # Compute the offsets: - offset_y = tf.cast(scaled_height - output_size, tf.float32) - offset_x = tf.cast(scaled_width - output_size, tf.float32) - offset_y = tf.maximum(0.0, offset_y) * tf.random_uniform([], 0, 1, seed=seed) - offset_x = tf.maximum(0.0, offset_x) * tf.random_uniform([], 0, 1, seed=seed) - offset_y = tf.cast(offset_y, tf.int32) - offset_x = tf.cast(offset_x, tf.int32) - - # Scale, crop, and pad the input image. - scaled_image = tf.image.resize_images( - image, [scaled_height, scaled_width], method=resize_method) - scaled_image = scaled_image[offset_y:offset_y + output_size, - offset_x:offset_x + output_size, :] - output_image = tf.image.pad_to_bounding_box(scaled_image, 0, 0, output_size, - output_size) - - # Update the boxes. - new_window = tf.cast( - tf.stack([offset_y, offset_x, - offset_y + output_size, offset_x + output_size]), - dtype=tf.float32) - new_window /= tf.cast( - tf.stack([scaled_height, scaled_width, scaled_height, scaled_width]), - dtype=tf.float32) - boxlist = box_list.BoxList(boxes) - boxlist = box_list_ops.change_coordinate_frame(boxlist, new_window) - boxlist, indices = box_list_ops.prune_completely_outside_window( - boxlist, [0.0, 0.0, 1.0, 1.0]) - boxlist = box_list_ops.clip_to_window( - boxlist, [0.0, 0.0, 1.0, 1.0], filter_nonoverlapping=False) - - return_values = [output_image, boxlist.get(), - tf.gather(labels, indices), - tf.gather(label_weights, indices)] - - if masks is not None: - new_masks = tf.expand_dims(masks, -1) - new_masks = tf.image.resize_images( - new_masks, [scaled_height, scaled_width], method=resize_method) - new_masks = new_masks[:, offset_y:offset_y + output_size, - offset_x:offset_x + output_size, :] - new_masks = tf.image.pad_to_bounding_box( - new_masks, 0, 0, output_size, output_size) - new_masks = tf.squeeze(new_masks, [-1]) - return_values.append(tf.gather(new_masks, indices)) - - if keypoints is not None: - keypoints = tf.gather(keypoints, indices) - keypoints = keypoint_ops.change_coordinate_frame(keypoints, new_window) - keypoints = keypoint_ops.prune_outside_window( - keypoints, [0.0, 0.0, 1.0, 1.0]) - return_values.append(keypoints) - - if label_confidences is not None: - return_values.append(tf.gather(label_confidences, indices)) - - return return_values - - - - -def get_default_func_arg_map(include_label_weights=True, - include_label_confidences=False, - include_multiclass_scores=False, - include_instance_masks=False, - include_instance_mask_weights=False, - include_keypoints=False, - include_keypoint_visibilities=False, - include_dense_pose=False, - include_keypoint_depths=False): - """Returns the default mapping from a preprocessor function to its args. - - Args: - include_label_weights: If True, preprocessing functions will modify the - label weights, too. - include_label_confidences: If True, preprocessing functions will modify the - label confidences, too. - include_multiclass_scores: If True, preprocessing functions will modify the - multiclass scores, too. - include_instance_masks: If True, preprocessing functions will modify the - instance masks, too. - include_instance_mask_weights: If True, preprocessing functions will modify - the instance mask weights. - include_keypoints: If True, preprocessing functions will modify the - keypoints, too. - include_keypoint_visibilities: If True, preprocessing functions will modify - the keypoint visibilities, too. - include_dense_pose: If True, preprocessing functions will modify the - DensePose labels, too. - include_keypoint_depths: If True, preprocessing functions will modify the - keypoint depth labels, too. - - Returns: - A map from preprocessing functions to the arguments they receive. - """ - groundtruth_label_weights = None - if include_label_weights: - groundtruth_label_weights = ( - fields.InputDataFields.groundtruth_weights) - - groundtruth_label_confidences = None - if include_label_confidences: - groundtruth_label_confidences = ( - fields.InputDataFields.groundtruth_confidences) - - multiclass_scores = None - if include_multiclass_scores: - multiclass_scores = (fields.InputDataFields.multiclass_scores) - - groundtruth_instance_masks = None - if include_instance_masks: - groundtruth_instance_masks = ( - fields.InputDataFields.groundtruth_instance_masks) - - groundtruth_instance_mask_weights = None - if include_instance_mask_weights: - groundtruth_instance_mask_weights = ( - fields.InputDataFields.groundtruth_instance_mask_weights) - - groundtruth_keypoints = None - if include_keypoints: - groundtruth_keypoints = fields.InputDataFields.groundtruth_keypoints - - groundtruth_keypoint_visibilities = None - if include_keypoint_visibilities: - groundtruth_keypoint_visibilities = ( - fields.InputDataFields.groundtruth_keypoint_visibilities) - - groundtruth_dp_num_points = None - groundtruth_dp_part_ids = None - groundtruth_dp_surface_coords = None - if include_dense_pose: - groundtruth_dp_num_points = ( - fields.InputDataFields.groundtruth_dp_num_points) - groundtruth_dp_part_ids = ( - fields.InputDataFields.groundtruth_dp_part_ids) - groundtruth_dp_surface_coords = ( - fields.InputDataFields.groundtruth_dp_surface_coords) - groundtruth_keypoint_depths = None - groundtruth_keypoint_depth_weights = None - if include_keypoint_depths: - groundtruth_keypoint_depths = ( - fields.InputDataFields.groundtruth_keypoint_depths) - groundtruth_keypoint_depth_weights = ( - fields.InputDataFields.groundtruth_keypoint_depth_weights) - - prep_func_arg_map = { - normalize_image: (fields.InputDataFields.image,), - random_horizontal_flip: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - groundtruth_instance_masks, - groundtruth_keypoints, - groundtruth_keypoint_visibilities, - groundtruth_dp_part_ids, - groundtruth_dp_surface_coords, - groundtruth_keypoint_depths, - groundtruth_keypoint_depth_weights, - ), - random_vertical_flip: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - groundtruth_instance_masks, - groundtruth_keypoints, - ), - random_rotation90: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - groundtruth_instance_masks, - groundtruth_keypoints, - ), - random_pixel_value_scale: (fields.InputDataFields.image,), - random_image_scale: ( - fields.InputDataFields.image, - groundtruth_instance_masks, - ), - random_rgb_to_gray: (fields.InputDataFields.image,), - random_adjust_brightness: (fields.InputDataFields.image,), - random_adjust_contrast: (fields.InputDataFields.image,), - random_adjust_hue: (fields.InputDataFields.image,), - random_adjust_saturation: (fields.InputDataFields.image,), - random_distort_color: (fields.InputDataFields.image,), - random_jitter_boxes: (fields.InputDataFields.groundtruth_boxes,), - random_crop_image: - (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, groundtruth_label_confidences, - multiclass_scores, groundtruth_instance_masks, - groundtruth_instance_mask_weights, groundtruth_keypoints, - groundtruth_keypoint_visibilities, groundtruth_dp_num_points, - groundtruth_dp_part_ids, groundtruth_dp_surface_coords), - random_pad_image: - (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, groundtruth_instance_masks, - groundtruth_keypoints, groundtruth_dp_surface_coords), - random_absolute_pad_image: - (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, groundtruth_instance_masks, - groundtruth_keypoints, groundtruth_dp_surface_coords), - random_crop_pad_image: (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, - groundtruth_label_confidences, multiclass_scores), - random_crop_to_aspect_ratio: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, - groundtruth_label_confidences, - multiclass_scores, - groundtruth_instance_masks, - groundtruth_keypoints, - ), - random_pad_to_aspect_ratio: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - groundtruth_instance_masks, - groundtruth_keypoints, - ), - random_black_patches: (fields.InputDataFields.image,), - random_jpeg_quality: (fields.InputDataFields.image,), - random_downscale_to_target_pixels: ( - fields.InputDataFields.image, - groundtruth_instance_masks, - ), - random_patch_gaussian: (fields.InputDataFields.image,), - autoaugment_image: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - ), - retain_boxes_above_threshold: ( - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, - groundtruth_label_confidences, - multiclass_scores, - groundtruth_instance_masks, - groundtruth_keypoints, - ), - drop_label_probabilistically: ( - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, - groundtruth_label_confidences, - multiclass_scores, - groundtruth_instance_masks, - groundtruth_keypoints, - ), - remap_labels: (fields.InputDataFields.groundtruth_classes,), - image_to_float: (fields.InputDataFields.image,), - random_resize_method: (fields.InputDataFields.image,), - resize_to_range: ( - fields.InputDataFields.image, - groundtruth_instance_masks, - ), - resize_to_min_dimension: ( - fields.InputDataFields.image, - groundtruth_instance_masks, - ), - scale_boxes_to_pixel_coordinates: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - groundtruth_keypoints, - ), - resize_image: ( - fields.InputDataFields.image, - groundtruth_instance_masks, - ), - subtract_channel_mean: (fields.InputDataFields.image,), - one_hot_encoding: (fields.InputDataFields.groundtruth_image_classes,), - rgb_to_gray: (fields.InputDataFields.image,), - random_self_concat_image: - (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, groundtruth_label_confidences, - multiclass_scores), - ssd_random_crop: (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, - groundtruth_label_confidences, multiclass_scores, - groundtruth_instance_masks, groundtruth_keypoints), - ssd_random_crop_pad: (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, - groundtruth_label_confidences, multiclass_scores), - ssd_random_crop_fixed_aspect_ratio: - (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, groundtruth_label_confidences, - multiclass_scores, groundtruth_instance_masks, groundtruth_keypoints - ), - ssd_random_crop_pad_fixed_aspect_ratio: ( - fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, - groundtruth_label_confidences, - multiclass_scores, - groundtruth_instance_masks, - groundtruth_keypoints, - ), - convert_class_logits_to_softmax: (multiclass_scores,), - random_square_crop_by_scale: - (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, groundtruth_label_confidences, - groundtruth_instance_masks, groundtruth_keypoints), - random_scale_crop_and_pad_to_square: - (fields.InputDataFields.image, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - groundtruth_label_weights, groundtruth_instance_masks, - groundtruth_keypoints, groundtruth_label_confidences), - adjust_gamma: (fields.InputDataFields.image,), - } - - return prep_func_arg_map - - -def preprocess(tensor_dict, - preprocess_options, - func_arg_map=None, - preprocess_vars_cache=None): - """Preprocess images and bounding boxes. - - Various types of preprocessing (to be implemented) based on the - preprocess_options dictionary e.g. "crop image" (affects image and possibly - boxes), "white balance image" (affects only image), etc. If self._options - is None, no preprocessing is done. - - Args: - tensor_dict: dictionary that contains images, boxes, and can contain other - things as well. - images-> rank 4 float32 tensor contains - 1 image -> [1, height, width, 3]. - with pixel values varying between [0, 1] - boxes-> rank 2 float32 tensor containing - the bounding boxes -> [N, 4]. - Boxes are in normalized form meaning - their coordinates vary between [0, 1]. - Each row is in the form - of [ymin, xmin, ymax, xmax]. - preprocess_options: It is a list of tuples, where each tuple contains a - function and a dictionary that contains arguments and - their values. - func_arg_map: mapping from preprocessing functions to arguments that they - expect to receive and return. - preprocess_vars_cache: PreprocessorCache object that records previously - performed augmentations. Updated in-place. If this - function is called multiple times with the same - non-null cache, it will perform deterministically. - - Returns: - tensor_dict: which contains the preprocessed images, bounding boxes, etc. - - Raises: - ValueError: (a) If the functions passed to Preprocess - are not in func_arg_map. - (b) If the arguments that a function needs - do not exist in tensor_dict. - (c) If image in tensor_dict is not rank 4 - """ - if func_arg_map is None: - func_arg_map = get_default_func_arg_map() - # changes the images to image (rank 4 to rank 3) since the functions - # receive rank 3 tensor for image - if fields.InputDataFields.image in tensor_dict: - images = tensor_dict[fields.InputDataFields.image] - if len(images.get_shape()) != 4: - raise ValueError('images in tensor_dict should be rank 4') - image = tf.squeeze(images, axis=0) - tensor_dict[fields.InputDataFields.image] = image - - # Preprocess inputs based on preprocess_options - for option in preprocess_options: - func, params = option - if func not in func_arg_map: - raise ValueError('The function %s does not exist in func_arg_map' % - (func.__name__)) - arg_names = func_arg_map[func] - for a in arg_names: - if a is not None and a not in tensor_dict: - raise ValueError('The function %s requires argument %s' % - (func.__name__, a)) - - def get_arg(key): - return tensor_dict[key] if key is not None else None - - args = [get_arg(a) for a in arg_names] - if preprocess_vars_cache is not None: - if six.PY2: - # pylint: disable=deprecated-method - arg_spec = inspect.getargspec(func) - # pylint: enable=deprecated-method - else: - arg_spec = inspect.getfullargspec(func) - if 'preprocess_vars_cache' in arg_spec.args: - params['preprocess_vars_cache'] = preprocess_vars_cache - - results = func(*args, **params) - if not isinstance(results, (list, tuple)): - results = (results,) - # Removes None args since the return values will not contain those. - arg_names = [arg_name for arg_name in arg_names if arg_name is not None] - for res, arg_name in zip(results, arg_names): - tensor_dict[arg_name] = res - - # changes the image to images (rank 3 to rank 4) to be compatible to what - # we received in the first place - if fields.InputDataFields.image in tensor_dict: - image = tensor_dict[fields.InputDataFields.image] - images = tf.expand_dims(image, 0) - tensor_dict[fields.InputDataFields.image] = images - - return tensor_dict diff --git a/research/object_detection/core/preprocessor_cache.py b/research/object_detection/core/preprocessor_cache.py deleted file mode 100644 index 948710564cf..00000000000 --- a/research/object_detection/core/preprocessor_cache.py +++ /dev/null @@ -1,109 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Records previous preprocessing operations and allows them to be repeated. - -Used with object_detection.core.preprocessor. Passing a PreprocessorCache -into individual data augmentation functions or the general preprocess() function -will store all randomly generated variables in the PreprocessorCache. When -a preprocessor function is called multiple times with the same -PreprocessorCache object, that function will perform the same augmentation -on all calls. -""" - -import collections - - -class PreprocessorCache(object): - """Dictionary wrapper storing random variables generated during preprocessing. - """ - - # Constant keys representing different preprocessing functions - ROTATION90 = 'rotation90' - HORIZONTAL_FLIP = 'horizontal_flip' - VERTICAL_FLIP = 'vertical_flip' - PIXEL_VALUE_SCALE = 'pixel_value_scale' - IMAGE_SCALE = 'image_scale' - RGB_TO_GRAY = 'rgb_to_gray' - ADJUST_BRIGHTNESS = 'adjust_brightness' - ADJUST_CONTRAST = 'adjust_contrast' - ADJUST_HUE = 'adjust_hue' - ADJUST_SATURATION = 'adjust_saturation' - DISTORT_COLOR = 'distort_color' - STRICT_CROP_IMAGE = 'strict_crop_image' - CROP_IMAGE = 'crop_image' - PAD_IMAGE = 'pad_image' - CROP_TO_ASPECT_RATIO = 'crop_to_aspect_ratio' - RESIZE_METHOD = 'resize_method' - PAD_TO_ASPECT_RATIO = 'pad_to_aspect_ratio' - BLACK_PATCHES = 'black_patches' - ADD_BLACK_PATCH = 'add_black_patch' - SELECTOR = 'selector' - SELECTOR_TUPLES = 'selector_tuples' - SELF_CONCAT_IMAGE = 'self_concat_image' - SSD_CROP_SELECTOR_ID = 'ssd_crop_selector_id' - SSD_CROP_PAD_SELECTOR_ID = 'ssd_crop_pad_selector_id' - JPEG_QUALITY = 'jpeg_quality' - DOWNSCALE_TO_TARGET_PIXELS = 'downscale_to_target_pixels' - PATCH_GAUSSIAN = 'patch_gaussian' - SQUARE_CROP_BY_SCALE = 'square_crop_scale' - - # 27 permitted function ids - _VALID_FNS = [ROTATION90, HORIZONTAL_FLIP, VERTICAL_FLIP, PIXEL_VALUE_SCALE, - IMAGE_SCALE, RGB_TO_GRAY, ADJUST_BRIGHTNESS, ADJUST_CONTRAST, - ADJUST_HUE, ADJUST_SATURATION, DISTORT_COLOR, STRICT_CROP_IMAGE, - CROP_IMAGE, PAD_IMAGE, CROP_TO_ASPECT_RATIO, RESIZE_METHOD, - PAD_TO_ASPECT_RATIO, BLACK_PATCHES, ADD_BLACK_PATCH, SELECTOR, - SELECTOR_TUPLES, SELF_CONCAT_IMAGE, SSD_CROP_SELECTOR_ID, - SSD_CROP_PAD_SELECTOR_ID, JPEG_QUALITY, - DOWNSCALE_TO_TARGET_PIXELS, PATCH_GAUSSIAN, - SQUARE_CROP_BY_SCALE] - - def __init__(self): - self._history = collections.defaultdict(dict) - - def clear(self): - """Resets cache.""" - self._history = collections.defaultdict(dict) - - def get(self, function_id, key): - """Gets stored value given a function id and key. - - Args: - function_id: identifier for the preprocessing function used. - key: identifier for the variable stored. - Returns: - value: the corresponding value, expected to be a tensor or - nested structure of tensors. - Raises: - ValueError: if function_id is not one of the 23 valid function ids. - """ - if function_id not in self._VALID_FNS: - raise ValueError('Function id not recognized: %s.' % str(function_id)) - return self._history[function_id].get(key) - - def update(self, function_id, key, value): - """Adds a value to the dictionary. - - Args: - function_id: identifier for the preprocessing function used. - key: identifier for the variable stored. - value: the value to store, expected to be a tensor or nested structure - of tensors. - Raises: - ValueError: if function_id is not one of the 23 valid function ids. - """ - if function_id not in self._VALID_FNS: - raise ValueError('Function id not recognized: %s.' % str(function_id)) - self._history[function_id][key] = value diff --git a/research/object_detection/core/preprocessor_test.py b/research/object_detection/core/preprocessor_test.py deleted file mode 100644 index b844a17164b..00000000000 --- a/research/object_detection/core/preprocessor_test.py +++ /dev/null @@ -1,4377 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.preprocessor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -from absl.testing import parameterized -import numpy as np -import six -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.core import preprocessor -from object_detection.core import preprocessor_cache -from object_detection.core import standard_fields as fields -from object_detection.utils import test_case -from object_detection.utils import tf_version - -if six.PY2: - import mock # pylint: disable=g-import-not-at-top -else: - mock = unittest.mock # pylint: disable=g-import-not-at-top - - -class PreprocessorTest(test_case.TestCase, parameterized.TestCase): - - def createColorfulTestImage(self): - ch255 = tf.fill([1, 100, 200, 1], tf.constant(255, dtype=tf.uint8)) - ch128 = tf.fill([1, 100, 200, 1], tf.constant(128, dtype=tf.uint8)) - ch0 = tf.fill([1, 100, 200, 1], tf.constant(0, dtype=tf.uint8)) - imr = tf.concat([ch255, ch0, ch0], 3) - img = tf.concat([ch255, ch255, ch0], 3) - imb = tf.concat([ch255, ch0, ch255], 3) - imw = tf.concat([ch128, ch128, ch128], 3) - imu = tf.concat([imr, img], 2) - imd = tf.concat([imb, imw], 2) - im = tf.concat([imu, imd], 1) - return im - - def createTestImages(self): - images_r = tf.constant([[[128, 128, 128, 128], [0, 0, 128, 128], - [0, 128, 128, 128], [192, 192, 128, 128]]], - dtype=tf.uint8) - images_r = tf.expand_dims(images_r, 3) - images_g = tf.constant([[[0, 0, 128, 128], [0, 0, 128, 128], - [0, 128, 192, 192], [192, 192, 128, 192]]], - dtype=tf.uint8) - images_g = tf.expand_dims(images_g, 3) - images_b = tf.constant([[[128, 128, 192, 0], [0, 0, 128, 192], - [0, 128, 128, 0], [192, 192, 192, 128]]], - dtype=tf.uint8) - images_b = tf.expand_dims(images_b, 3) - images = tf.concat([images_r, images_g, images_b], 3) - return images - - def createEmptyTestBoxes(self): - boxes = tf.constant([[]], dtype=tf.float32) - return boxes - - def createTestBoxes(self): - boxes = tf.constant( - [[0.0, 0.25, 0.75, 1.0], [0.25, 0.5, 0.75, 1.0]], dtype=tf.float32) - return boxes - - def createRandomTextBoxes(self): - random_boxes = tf.concat([tf.random.uniform([100, 2], 0.0, 0.5, seed=1), - tf.random.uniform([100, 2], 0.5, 1.0, seed=2)], - axis=1) - fixed_boxes = tf.constant( - [[0.0, 0.25, 0.75, 1.0], - [0.25, 0.5, 0.75, 1.0], - [0.0, 0.0, 1.0, 1.0], - [0.1, 0.2, 0.3, 0.4]], dtype=tf.float32) - zero_boxes = tf.zeros((50, 4)) - return tf.concat([random_boxes, fixed_boxes, zero_boxes], axis=0) - - def createTestGroundtruthWeights(self): - return tf.constant([1.0, 0.5], dtype=tf.float32) - - def createZeroBoxes(self): - return tf.zeros((100, 4)) - - def createTestMasks(self): - mask = np.array([ - [[255.0, 0.0, 0.0], - [255.0, 0.0, 0.0], - [255.0, 0.0, 0.0]], - [[255.0, 255.0, 0.0], - [255.0, 255.0, 0.0], - [255.0, 255.0, 0.0]]]) - return tf.constant(mask, dtype=tf.float32) - - def createTestKeypoints(self): - keypoints_np = np.array([ - [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]], - ]) - keypoints = tf.constant(keypoints_np, dtype=tf.float32) - keypoint_visibilities = tf.constant( - [ - [True, True, False], - [False, True, True] - ]) - return keypoints, keypoint_visibilities - - def createTestKeypointDepths(self): - keypoint_depths = tf.constant([ - [1.0, 0.9, 0.8], - [0.7, 0.6, 0.5] - ], dtype=tf.float32) - keypoint_depth_weights = tf.constant([ - [0.5, 0.6, 0.7], - [0.8, 0.9, 1.0] - ], dtype=tf.float32) - return keypoint_depths, keypoint_depth_weights - - def createTestKeypointsInsideCrop(self): - keypoints = np.array([ - [[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]], - [[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]], - ]) - return tf.constant(keypoints, dtype=tf.float32) - - def createTestKeypointsOutsideCrop(self): - keypoints = np.array([ - [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]], - ]) - return tf.constant(keypoints, dtype=tf.float32) - - def createTestDensePose(self): - dp_num_points = tf.constant([1, 3], dtype=tf.int32) - dp_part_ids = tf.constant( - [[4, 0, 0], - [1, 0, 5]], dtype=tf.int32) - dp_surface_coords = tf.constant( - [ - # Instance 0. - [[0.1, 0.2, 0.6, 0.7], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0]], - # Instance 1. - [[0.8, 0.9, 0.2, 0.4], - [0.1, 0.3, 0.2, 0.8], - [0.6, 1.0, 0.3, 0.4]], - ], dtype=tf.float32) - return dp_num_points, dp_part_ids, dp_surface_coords - - def createKeypointFlipPermutation(self): - return [0, 2, 1] - - def createKeypointRotPermutation(self): - return [0, 2, 1] - - def createTestLabels(self): - labels = tf.constant([1, 2], dtype=tf.int32) - return labels - - def createTestLabelsLong(self): - labels = tf.constant([1, 2, 4], dtype=tf.int32) - return labels - - def createTestBoxesOutOfImage(self): - boxes = tf.constant( - [[-0.1, 0.25, 0.75, 1], [0.25, 0.5, 0.75, 1.1]], dtype=tf.float32) - return boxes - - def createTestMultiClassScores(self): - return tf.constant([[1.0, 0.0], [0.5, 0.5]], dtype=tf.float32) - - - def expectedImagesAfterNormalization(self): - images_r = tf.constant([[[0, 0, 0, 0], [-1, -1, 0, 0], - [-1, 0, 0, 0], [0.5, 0.5, 0, 0]]], - dtype=tf.float32) - images_r = tf.expand_dims(images_r, 3) - images_g = tf.constant([[[-1, -1, 0, 0], [-1, -1, 0, 0], - [-1, 0, 0.5, 0.5], [0.5, 0.5, 0, 0.5]]], - dtype=tf.float32) - images_g = tf.expand_dims(images_g, 3) - images_b = tf.constant([[[0, 0, 0.5, -1], [-1, -1, 0, 0.5], - [-1, 0, 0, -1], [0.5, 0.5, 0.5, 0]]], - dtype=tf.float32) - images_b = tf.expand_dims(images_b, 3) - images = tf.concat([images_r, images_g, images_b], 3) - return images - - def expectedMaxImageAfterColorScale(self): - images_r = tf.constant([[[0.1, 0.1, 0.1, 0.1], [-0.9, -0.9, 0.1, 0.1], - [-0.9, 0.1, 0.1, 0.1], [0.6, 0.6, 0.1, 0.1]]], - dtype=tf.float32) - images_r = tf.expand_dims(images_r, 3) - images_g = tf.constant([[[-0.9, -0.9, 0.1, 0.1], [-0.9, -0.9, 0.1, 0.1], - [-0.9, 0.1, 0.6, 0.6], [0.6, 0.6, 0.1, 0.6]]], - dtype=tf.float32) - images_g = tf.expand_dims(images_g, 3) - images_b = tf.constant([[[0.1, 0.1, 0.6, -0.9], [-0.9, -0.9, 0.1, 0.6], - [-0.9, 0.1, 0.1, -0.9], [0.6, 0.6, 0.6, 0.1]]], - dtype=tf.float32) - images_b = tf.expand_dims(images_b, 3) - images = tf.concat([images_r, images_g, images_b], 3) - return images - - def expectedMinImageAfterColorScale(self): - images_r = tf.constant([[[-0.1, -0.1, -0.1, -0.1], [-1, -1, -0.1, -0.1], - [-1, -0.1, -0.1, -0.1], [0.4, 0.4, -0.1, -0.1]]], - dtype=tf.float32) - images_r = tf.expand_dims(images_r, 3) - images_g = tf.constant([[[-1, -1, -0.1, -0.1], [-1, -1, -0.1, -0.1], - [-1, -0.1, 0.4, 0.4], [0.4, 0.4, -0.1, 0.4]]], - dtype=tf.float32) - images_g = tf.expand_dims(images_g, 3) - images_b = tf.constant([[[-0.1, -0.1, 0.4, -1], [-1, -1, -0.1, 0.4], - [-1, -0.1, -0.1, -1], [0.4, 0.4, 0.4, -0.1]]], - dtype=tf.float32) - images_b = tf.expand_dims(images_b, 3) - images = tf.concat([images_r, images_g, images_b], 3) - return images - - def expectedImagesAfterLeftRightFlip(self): - images_r = tf.constant([[[0, 0, 0, 0], [0, 0, -1, -1], - [0, 0, 0, -1], [0, 0, 0.5, 0.5]]], - dtype=tf.float32) - images_r = tf.expand_dims(images_r, 3) - images_g = tf.constant([[[0, 0, -1, -1], [0, 0, -1, -1], - [0.5, 0.5, 0, -1], [0.5, 0, 0.5, 0.5]]], - dtype=tf.float32) - images_g = tf.expand_dims(images_g, 3) - images_b = tf.constant([[[-1, 0.5, 0, 0], [0.5, 0, -1, -1], - [-1, 0, 0, -1], [0, 0.5, 0.5, 0.5]]], - dtype=tf.float32) - images_b = tf.expand_dims(images_b, 3) - images = tf.concat([images_r, images_g, images_b], 3) - return images - - def expectedImagesAfterUpDownFlip(self): - images_r = tf.constant([[[0.5, 0.5, 0, 0], [-1, 0, 0, 0], - [-1, -1, 0, 0], [0, 0, 0, 0]]], - dtype=tf.float32) - images_r = tf.expand_dims(images_r, 3) - images_g = tf.constant([[[0.5, 0.5, 0, 0.5], [-1, 0, 0.5, 0.5], - [-1, -1, 0, 0], [-1, -1, 0, 0]]], - dtype=tf.float32) - images_g = tf.expand_dims(images_g, 3) - images_b = tf.constant([[[0.5, 0.5, 0.5, 0], [-1, 0, 0, -1], - [-1, -1, 0, 0.5], [0, 0, 0.5, -1]]], - dtype=tf.float32) - images_b = tf.expand_dims(images_b, 3) - images = tf.concat([images_r, images_g, images_b], 3) - return images - - def expectedImagesAfterRot90(self): - images_r = tf.constant([[[0, 0, 0, 0], [0, 0, 0, 0], - [0, -1, 0, 0.5], [0, -1, -1, 0.5]]], - dtype=tf.float32) - images_r = tf.expand_dims(images_r, 3) - images_g = tf.constant([[[0, 0, 0.5, 0.5], [0, 0, 0.5, 0], - [-1, -1, 0, 0.5], [-1, -1, -1, 0.5]]], - dtype=tf.float32) - images_g = tf.expand_dims(images_g, 3) - images_b = tf.constant([[[-1, 0.5, -1, 0], [0.5, 0, 0, 0.5], - [0, -1, 0, 0.5], [0, -1, -1, 0.5]]], - dtype=tf.float32) - images_b = tf.expand_dims(images_b, 3) - images = tf.concat([images_r, images_g, images_b], 3) - return images - - def expectedBoxesAfterLeftRightFlip(self): - boxes = tf.constant([[0.0, 0.0, 0.75, 0.75], [0.25, 0.0, 0.75, 0.5]], - dtype=tf.float32) - return boxes - - def expectedBoxesAfterUpDownFlip(self): - boxes = tf.constant([[0.25, 0.25, 1.0, 1.0], [0.25, 0.5, 0.75, 1.0]], - dtype=tf.float32) - return boxes - - def expectedBoxesAfterRot90(self): - boxes = tf.constant( - [[0.0, 0.0, 0.75, 0.75], [0.0, 0.25, 0.5, 0.75]], dtype=tf.float32) - return boxes - - def expectedMasksAfterLeftRightFlip(self): - mask = np.array([ - [[0.0, 0.0, 255.0], - [0.0, 0.0, 255.0], - [0.0, 0.0, 255.0]], - [[0.0, 255.0, 255.0], - [0.0, 255.0, 255.0], - [0.0, 255.0, 255.0]]]) - return tf.constant(mask, dtype=tf.float32) - - def expectedMasksAfterUpDownFlip(self): - mask = np.array([ - [[255.0, 0.0, 0.0], - [255.0, 0.0, 0.0], - [255.0, 0.0, 0.0]], - [[255.0, 255.0, 0.0], - [255.0, 255.0, 0.0], - [255.0, 255.0, 0.0]]]) - return tf.constant(mask, dtype=tf.float32) - - def expectedMasksAfterRot90(self): - mask = np.array([ - [[0.0, 0.0, 0.0], - [0.0, 0.0, 0.0], - [255.0, 255.0, 255.0]], - [[0.0, 0.0, 0.0], - [255.0, 255.0, 255.0], - [255.0, 255.0, 255.0]]]) - return tf.constant(mask, dtype=tf.float32) - - def expectedLabelScoresAfterThresholding(self): - return tf.constant([1.0], dtype=tf.float32) - - def expectedBoxesAfterThresholding(self): - return tf.constant([[0.0, 0.25, 0.75, 1.0]], dtype=tf.float32) - - def expectedLabelsAfterThresholding(self): - return tf.constant([1], dtype=tf.float32) - - def expectedMultiClassScoresAfterThresholding(self): - return tf.constant([[1.0, 0.0]], dtype=tf.float32) - - def expectedMasksAfterThresholding(self): - mask = np.array([ - [[255.0, 0.0, 0.0], - [255.0, 0.0, 0.0], - [255.0, 0.0, 0.0]]]) - return tf.constant(mask, dtype=tf.float32) - - def expectedKeypointsAfterThresholding(self): - keypoints = np.array([ - [[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]] - ]) - return tf.constant(keypoints, dtype=tf.float32) - - def expectedLabelScoresAfterThresholdingWithMissingScore(self): - return tf.constant([np.nan], dtype=tf.float32) - - def expectedBoxesAfterThresholdingWithMissingScore(self): - return tf.constant([[0.25, 0.5, 0.75, 1]], dtype=tf.float32) - - def expectedLabelsAfterThresholdingWithMissingScore(self): - return tf.constant([2], dtype=tf.float32) - - def expectedLabelScoresAfterDropping(self): - return tf.constant([0.5], dtype=tf.float32) - - def expectedBoxesAfterDropping(self): - return tf.constant([[0.25, 0.5, 0.75, 1.0]], dtype=tf.float32) - - def expectedLabelsAfterDropping(self): - return tf.constant([2], dtype=tf.float32) - - def expectedMultiClassScoresAfterDropping(self): - return tf.constant([[0.5, 0.5]], dtype=tf.float32) - - def expectedMasksAfterDropping(self): - masks = np.array([[[255.0, 255.0, 0.0], [255.0, 255.0, 0.0], - [255.0, 255.0, 0.0]]]) - return tf.constant(masks, dtype=tf.float32) - - def expectedKeypointsAfterDropping(self): - keypoints = np.array([[[0.4, 0.4], [0.5, 0.5], [0.6, 0.6]]]) - return tf.constant(keypoints, dtype=tf.float32) - - def expectedLabelsAfterRemapping(self): - return tf.constant([3, 3, 4], dtype=tf.float32) - - def testRgbToGrayscale(self): - def graph_fn(): - images = self.createTestImages() - grayscale_images = preprocessor._rgb_to_grayscale(images) - expected_images = tf.image.rgb_to_grayscale(images) - return grayscale_images, expected_images - (grayscale_images, expected_images) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(expected_images, grayscale_images) - - def testNormalizeImage(self): - def graph_fn(): - preprocess_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 256, - 'target_minval': -1, - 'target_maxval': 1 - })] - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images = tensor_dict[fields.InputDataFields.image] - images_expected = self.expectedImagesAfterNormalization() - return images, images_expected - images_, images_expected_ = self.execute_cpu(graph_fn, []) - images_shape_ = images_.shape - images_expected_shape_ = images_expected_.shape - expected_shape = [1, 4, 4, 3] - self.assertAllEqual(images_expected_shape_, images_shape_) - self.assertAllEqual(images_shape_, expected_shape) - self.assertAllClose(images_, images_expected_) - - def testRetainBoxesAboveThreshold(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - (retained_boxes, retained_labels, - retained_weights) = preprocessor.retain_boxes_above_threshold( - boxes, labels, weights, threshold=0.6) - return [ - retained_boxes, retained_labels, retained_weights, - self.expectedBoxesAfterThresholding(), - self.expectedLabelsAfterThresholding(), - self.expectedLabelScoresAfterThresholding() - ] - - (retained_boxes_, retained_labels_, retained_weights_, - expected_retained_boxes_, expected_retained_labels_, - expected_retained_weights_) = self.execute_cpu(graph_fn, []) - self.assertAllClose( - retained_boxes_, expected_retained_boxes_) - self.assertAllClose( - retained_labels_, expected_retained_labels_) - self.assertAllClose( - retained_weights_, expected_retained_weights_) - - def testRetainBoxesAboveThresholdWithMultiClassScores(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - multiclass_scores = self.createTestMultiClassScores() - (_, _, _, - retained_multiclass_scores) = preprocessor.retain_boxes_above_threshold( - boxes, - labels, - weights, - multiclass_scores=multiclass_scores, - threshold=0.6) - return [ - retained_multiclass_scores, - self.expectedMultiClassScoresAfterThresholding() - ] - - (retained_multiclass_scores_, - expected_retained_multiclass_scores_) = self.execute(graph_fn, []) - self.assertAllClose(retained_multiclass_scores_, - expected_retained_multiclass_scores_) - - def testRetainBoxesAboveThresholdWithMasks(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = self.createTestMasks() - _, _, _, retained_masks = preprocessor.retain_boxes_above_threshold( - boxes, labels, weights, masks, threshold=0.6) - return [ - retained_masks, self.expectedMasksAfterThresholding()] - retained_masks_, expected_retained_masks_ = self.execute_cpu(graph_fn, []) - - self.assertAllClose( - retained_masks_, expected_retained_masks_) - - def testRetainBoxesAboveThresholdWithKeypoints(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - keypoints, _ = self.createTestKeypoints() - (_, _, _, retained_keypoints) = preprocessor.retain_boxes_above_threshold( - boxes, labels, weights, keypoints=keypoints, threshold=0.6) - return [retained_keypoints, self.expectedKeypointsAfterThresholding()] - - (retained_keypoints_, - expected_retained_keypoints_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(retained_keypoints_, expected_retained_keypoints_) - - def testDropLabelProbabilistically(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - (retained_boxes, retained_labels, - retained_weights) = preprocessor.drop_label_probabilistically( - boxes, labels, weights, dropped_label=1, drop_probability=1.0) - return [ - retained_boxes, retained_labels, retained_weights, - self.expectedBoxesAfterDropping(), - self.expectedLabelsAfterDropping(), - self.expectedLabelScoresAfterDropping() - ] - - (retained_boxes_, retained_labels_, retained_weights_, - expected_retained_boxes_, expected_retained_labels_, - expected_retained_weights_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(retained_boxes_, expected_retained_boxes_) - self.assertAllClose(retained_labels_, expected_retained_labels_) - self.assertAllClose(retained_weights_, expected_retained_weights_) - - def testDropLabelProbabilisticallyWithMultiClassScores(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - multiclass_scores = self.createTestMultiClassScores() - (_, _, _, - retained_multiclass_scores) = preprocessor.drop_label_probabilistically( - boxes, - labels, - weights, - multiclass_scores=multiclass_scores, - dropped_label=1, - drop_probability=1.0) - return [retained_multiclass_scores, - self.expectedMultiClassScoresAfterDropping()] - (retained_multiclass_scores_, - expected_retained_multiclass_scores_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(retained_multiclass_scores_, - expected_retained_multiclass_scores_) - - def testDropLabelProbabilisticallyWithMasks(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = self.createTestMasks() - (_, _, _, retained_masks) = preprocessor.drop_label_probabilistically( - boxes, - labels, - weights, - masks=masks, - dropped_label=1, - drop_probability=1.0) - return [retained_masks, self.expectedMasksAfterDropping()] - (retained_masks_, expected_retained_masks_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(retained_masks_, expected_retained_masks_) - - def testDropLabelProbabilisticallyWithKeypoints(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - keypoints, _ = self.createTestKeypoints() - (_, _, _, retained_keypoints) = preprocessor.drop_label_probabilistically( - boxes, - labels, - weights, - keypoints=keypoints, - dropped_label=1, - drop_probability=1.0) - return [retained_keypoints, self.expectedKeypointsAfterDropping()] - - (retained_keypoints_, - expected_retained_keypoints_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(retained_keypoints_, expected_retained_keypoints_) - - def testRemapLabels(self): - def graph_fn(): - labels = self.createTestLabelsLong() - remapped_labels = preprocessor.remap_labels(labels, [1, 2], 3) - return [remapped_labels, self.expectedLabelsAfterRemapping()] - - (remapped_labels_, expected_remapped_labels_) = self.execute_cpu(graph_fn, - []) - self.assertAllClose(remapped_labels_, expected_remapped_labels_) - - def testFlipBoxesLeftRight(self): - def graph_fn(): - boxes = self.createTestBoxes() - flipped_boxes = preprocessor._flip_boxes_left_right(boxes) - expected_boxes = self.expectedBoxesAfterLeftRightFlip() - return flipped_boxes, expected_boxes - flipped_boxes, expected_boxes = self.execute_cpu(graph_fn, []) - self.assertAllEqual(flipped_boxes.flatten(), expected_boxes.flatten()) - - def testFlipBoxesUpDown(self): - def graph_fn(): - boxes = self.createTestBoxes() - flipped_boxes = preprocessor._flip_boxes_up_down(boxes) - expected_boxes = self.expectedBoxesAfterUpDownFlip() - return flipped_boxes, expected_boxes - flipped_boxes, expected_boxes = self.execute_cpu(graph_fn, []) - self.assertAllEqual(flipped_boxes.flatten(), expected_boxes.flatten()) - - def testRot90Boxes(self): - def graph_fn(): - boxes = self.createTestBoxes() - rotated_boxes = preprocessor._rot90_boxes(boxes) - expected_boxes = self.expectedBoxesAfterRot90() - return rotated_boxes, expected_boxes - rotated_boxes, expected_boxes = self.execute_cpu(graph_fn, []) - self.assertAllEqual(rotated_boxes.flatten(), expected_boxes.flatten()) - - def testFlipMasksLeftRight(self): - def graph_fn(): - test_mask = self.createTestMasks() - flipped_mask = preprocessor._flip_masks_left_right(test_mask) - expected_mask = self.expectedMasksAfterLeftRightFlip() - return flipped_mask, expected_mask - flipped_mask, expected_mask = self.execute_cpu(graph_fn, []) - self.assertAllEqual(flipped_mask.flatten(), expected_mask.flatten()) - - def testFlipMasksUpDown(self): - def graph_fn(): - test_mask = self.createTestMasks() - flipped_mask = preprocessor._flip_masks_up_down(test_mask) - expected_mask = self.expectedMasksAfterUpDownFlip() - return flipped_mask, expected_mask - flipped_mask, expected_mask = self.execute_cpu(graph_fn, []) - self.assertAllEqual(flipped_mask.flatten(), expected_mask.flatten()) - - def testRot90Masks(self): - def graph_fn(): - test_mask = self.createTestMasks() - rotated_mask = preprocessor._rot90_masks(test_mask) - expected_mask = self.expectedMasksAfterRot90() - return [rotated_mask, expected_mask] - rotated_mask, expected_mask = self.execute(graph_fn, []) - self.assertAllEqual(rotated_mask.flatten(), expected_mask.flatten()) - - def _testPreprocessorCache(self, - preprocess_options, - test_boxes=False, - test_masks=False, - test_keypoints=False): - if self.is_tf2(): return - def graph_fn(): - cache = preprocessor_cache.PreprocessorCache() - images = self.createTestImages() - boxes = self.createTestBoxes() - weights = self.createTestGroundtruthWeights() - classes = self.createTestLabels() - masks = self.createTestMasks() - keypoints, _ = self.createTestKeypoints() - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=test_masks, include_keypoints=test_keypoints) - out = [] - for _ in range(2): - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_weights: weights - } - if test_boxes: - tensor_dict[fields.InputDataFields.groundtruth_boxes] = boxes - tensor_dict[fields.InputDataFields.groundtruth_classes] = classes - if test_masks: - tensor_dict[fields.InputDataFields.groundtruth_instance_masks] = masks - if test_keypoints: - tensor_dict[fields.InputDataFields.groundtruth_keypoints] = keypoints - out.append( - preprocessor.preprocess(tensor_dict, preprocess_options, - preprocessor_arg_map, cache)) - return out - - out1, out2 = self.execute_cpu_tf1(graph_fn, []) - for (_, v1), (_, v2) in zip(out1.items(), out2.items()): - self.assertAllClose(v1, v2) - - def testRandomHorizontalFlip(self): - def graph_fn(): - preprocess_options = [(preprocessor.random_horizontal_flip, {})] - images = self.expectedImagesAfterNormalization() - boxes = self.createTestBoxes() - tensor_dict = {fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes} - images_expected1 = self.expectedImagesAfterLeftRightFlip() - boxes_expected1 = self.expectedBoxesAfterLeftRightFlip() - images_expected2 = images - boxes_expected2 = boxes - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images = tensor_dict[fields.InputDataFields.image] - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - - boxes_diff1 = tf.squared_difference(boxes, boxes_expected1) - boxes_diff2 = tf.squared_difference(boxes, boxes_expected2) - boxes_diff = tf.multiply(boxes_diff1, boxes_diff2) - boxes_diff_expected = tf.zeros_like(boxes_diff) - - images_diff1 = tf.squared_difference(images, images_expected1) - images_diff2 = tf.squared_difference(images, images_expected2) - images_diff = tf.multiply(images_diff1, images_diff2) - images_diff_expected = tf.zeros_like(images_diff) - return [images_diff, images_diff_expected, boxes_diff, - boxes_diff_expected] - (images_diff_, images_diff_expected_, boxes_diff_, - boxes_diff_expected_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(boxes_diff_, boxes_diff_expected_) - self.assertAllClose(images_diff_, images_diff_expected_) - - def testRandomHorizontalFlipWithEmptyBoxes(self): - def graph_fn(): - preprocess_options = [(preprocessor.random_horizontal_flip, {})] - images = self.expectedImagesAfterNormalization() - boxes = self.createEmptyTestBoxes() - tensor_dict = {fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes} - images_expected1 = self.expectedImagesAfterLeftRightFlip() - boxes_expected = self.createEmptyTestBoxes() - images_expected2 = images - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images = tensor_dict[fields.InputDataFields.image] - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - - images_diff1 = tf.squared_difference(images, images_expected1) - images_diff2 = tf.squared_difference(images, images_expected2) - images_diff = tf.multiply(images_diff1, images_diff2) - images_diff_expected = tf.zeros_like(images_diff) - return [images_diff, images_diff_expected, boxes, boxes_expected] - (images_diff_, images_diff_expected_, boxes_, - boxes_expected_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(boxes_, boxes_expected_) - self.assertAllClose(images_diff_, images_diff_expected_) - - def testRandomHorizontalFlipWithCache(self): - keypoint_flip_permutation = self.createKeypointFlipPermutation() - preprocess_options = [ - (preprocessor.random_horizontal_flip, - {'keypoint_flip_permutation': keypoint_flip_permutation})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - - def testRunRandomHorizontalFlipWithKeypointDepth(self): - - def graph_fn(): - preprocess_options = [(preprocessor.random_horizontal_flip, {})] - image_height = 3 - image_width = 3 - images = tf.random_uniform([1, image_height, image_width, 3]) - boxes = self.createTestBoxes() - masks = self.createTestMasks() - keypoints, keypoint_visibilities = self.createTestKeypoints() - keypoint_depths, keypoint_depth_weights = self.createTestKeypointDepths() - keypoint_flip_permutation = self.createKeypointFlipPermutation() - tensor_dict = { - fields.InputDataFields.image: - images, - fields.InputDataFields.groundtruth_boxes: - boxes, - fields.InputDataFields.groundtruth_instance_masks: - masks, - fields.InputDataFields.groundtruth_keypoints: - keypoints, - fields.InputDataFields.groundtruth_keypoint_visibilities: - keypoint_visibilities, - fields.InputDataFields.groundtruth_keypoint_depths: - keypoint_depths, - fields.InputDataFields.groundtruth_keypoint_depth_weights: - keypoint_depth_weights, - } - preprocess_options = [(preprocessor.random_horizontal_flip, { - 'keypoint_flip_permutation': keypoint_flip_permutation, - 'probability': 1.0 - })] - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True, - include_keypoints=True, - include_keypoint_visibilities=True, - include_dense_pose=False, - include_keypoint_depths=True) - tensor_dict = preprocessor.preprocess( - tensor_dict, preprocess_options, func_arg_map=preprocessor_arg_map) - keypoint_depths = tensor_dict[ - fields.InputDataFields.groundtruth_keypoint_depths] - keypoint_depth_weights = tensor_dict[ - fields.InputDataFields.groundtruth_keypoint_depth_weights] - output_tensors = [keypoint_depths, keypoint_depth_weights] - return output_tensors - - output_tensors = self.execute_cpu(graph_fn, []) - expected_keypoint_depths = [[1.0, 0.8, 0.9], [0.7, 0.5, 0.6]] - expected_keypoint_depth_weights = [[0.5, 0.7, 0.6], [0.8, 1.0, 0.9]] - self.assertAllClose(expected_keypoint_depths, output_tensors[0]) - self.assertAllClose(expected_keypoint_depth_weights, output_tensors[1]) - - def testRandomVerticalFlip(self): - - def graph_fn(): - preprocess_options = [(preprocessor.random_vertical_flip, {})] - images = self.expectedImagesAfterNormalization() - boxes = self.createTestBoxes() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes - } - images_expected1 = self.expectedImagesAfterUpDownFlip() - boxes_expected1 = self.expectedBoxesAfterUpDownFlip() - images_expected2 = images - boxes_expected2 = boxes - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images = tensor_dict[fields.InputDataFields.image] - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - - boxes_diff1 = tf.squared_difference(boxes, boxes_expected1) - boxes_diff2 = tf.squared_difference(boxes, boxes_expected2) - boxes_diff = tf.multiply(boxes_diff1, boxes_diff2) - boxes_diff_expected = tf.zeros_like(boxes_diff) - - images_diff1 = tf.squared_difference(images, images_expected1) - images_diff2 = tf.squared_difference(images, images_expected2) - images_diff = tf.multiply(images_diff1, images_diff2) - images_diff_expected = tf.zeros_like(images_diff) - return [ - images_diff, images_diff_expected, boxes_diff, boxes_diff_expected - ] - - (images_diff_, images_diff_expected_, boxes_diff_, - boxes_diff_expected_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(boxes_diff_, boxes_diff_expected_) - self.assertAllClose(images_diff_, images_diff_expected_) - - def testRandomVerticalFlipWithEmptyBoxes(self): - - def graph_fn(): - preprocess_options = [(preprocessor.random_vertical_flip, {})] - images = self.expectedImagesAfterNormalization() - boxes = self.createEmptyTestBoxes() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes - } - images_expected1 = self.expectedImagesAfterUpDownFlip() - boxes_expected = self.createEmptyTestBoxes() - images_expected2 = images - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images = tensor_dict[fields.InputDataFields.image] - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - - images_diff1 = tf.squared_difference(images, images_expected1) - images_diff2 = tf.squared_difference(images, images_expected2) - images_diff = tf.multiply(images_diff1, images_diff2) - images_diff_expected = tf.zeros_like(images_diff) - return [images_diff, images_diff_expected, boxes, boxes_expected] - - (images_diff_, images_diff_expected_, boxes_, - boxes_expected_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(boxes_, boxes_expected_) - self.assertAllClose(images_diff_, images_diff_expected_) - - def testRandomVerticalFlipWithCache(self): - keypoint_flip_permutation = self.createKeypointFlipPermutation() - preprocess_options = [ - (preprocessor.random_vertical_flip, - {'keypoint_flip_permutation': keypoint_flip_permutation})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - def testRunRandomVerticalFlipWithMaskAndKeypoints(self): - preprocess_options = [(preprocessor.random_vertical_flip, {})] - image_height = 3 - image_width = 3 - images = tf.random_uniform([1, image_height, image_width, 3]) - boxes = self.createTestBoxes() - masks = self.createTestMasks() - keypoints, _ = self.createTestKeypoints() - keypoint_flip_permutation = self.createKeypointFlipPermutation() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_instance_masks: masks, - fields.InputDataFields.groundtruth_keypoints: keypoints - } - preprocess_options = [ - (preprocessor.random_vertical_flip, - {'keypoint_flip_permutation': keypoint_flip_permutation})] - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True, include_keypoints=True) - tensor_dict = preprocessor.preprocess( - tensor_dict, preprocess_options, func_arg_map=preprocessor_arg_map) - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - masks = tensor_dict[fields.InputDataFields.groundtruth_instance_masks] - keypoints = tensor_dict[fields.InputDataFields.groundtruth_keypoints] - self.assertIsNotNone(boxes) - self.assertIsNotNone(masks) - self.assertIsNotNone(keypoints) - - def testRandomRotation90(self): - - def graph_fn(): - preprocess_options = [(preprocessor.random_rotation90, {})] - images = self.expectedImagesAfterNormalization() - boxes = self.createTestBoxes() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes - } - images_expected1 = self.expectedImagesAfterRot90() - boxes_expected1 = self.expectedBoxesAfterRot90() - images_expected2 = images - boxes_expected2 = boxes - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images = tensor_dict[fields.InputDataFields.image] - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - - boxes_diff1 = tf.squared_difference(boxes, boxes_expected1) - boxes_diff2 = tf.squared_difference(boxes, boxes_expected2) - boxes_diff = tf.multiply(boxes_diff1, boxes_diff2) - boxes_diff_expected = tf.zeros_like(boxes_diff) - - images_diff1 = tf.squared_difference(images, images_expected1) - images_diff2 = tf.squared_difference(images, images_expected2) - images_diff = tf.multiply(images_diff1, images_diff2) - images_diff_expected = tf.zeros_like(images_diff) - return [ - images_diff, images_diff_expected, boxes_diff, boxes_diff_expected - ] - - (images_diff_, images_diff_expected_, boxes_diff_, - boxes_diff_expected_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(boxes_diff_, boxes_diff_expected_) - self.assertAllClose(images_diff_, images_diff_expected_) - - def testRandomRotation90WithEmptyBoxes(self): - - def graph_fn(): - preprocess_options = [(preprocessor.random_rotation90, {})] - images = self.expectedImagesAfterNormalization() - boxes = self.createEmptyTestBoxes() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes - } - images_expected1 = self.expectedImagesAfterRot90() - boxes_expected = self.createEmptyTestBoxes() - images_expected2 = images - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images = tensor_dict[fields.InputDataFields.image] - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - - images_diff1 = tf.squared_difference(images, images_expected1) - images_diff2 = tf.squared_difference(images, images_expected2) - images_diff = tf.multiply(images_diff1, images_diff2) - images_diff_expected = tf.zeros_like(images_diff) - return [images_diff, images_diff_expected, boxes, boxes_expected] - - (images_diff_, images_diff_expected_, boxes_, - boxes_expected_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(boxes_, boxes_expected_) - self.assertAllClose(images_diff_, images_diff_expected_) - - def testRandomRotation90WithCache(self): - preprocess_options = [(preprocessor.random_rotation90, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - def testRunRandomRotation90WithMaskAndKeypoints(self): - image_height = 3 - image_width = 3 - images = tf.random_uniform([1, image_height, image_width, 3]) - boxes = self.createTestBoxes() - masks = self.createTestMasks() - keypoints, _ = self.createTestKeypoints() - keypoint_rot_permutation = self.createKeypointRotPermutation() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_instance_masks: masks, - fields.InputDataFields.groundtruth_keypoints: keypoints - } - preprocess_options = [(preprocessor.random_rotation90, { - 'keypoint_rot_permutation': keypoint_rot_permutation - })] - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True, include_keypoints=True) - tensor_dict = preprocessor.preprocess( - tensor_dict, preprocess_options, func_arg_map=preprocessor_arg_map) - boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - masks = tensor_dict[fields.InputDataFields.groundtruth_instance_masks] - keypoints = tensor_dict[fields.InputDataFields.groundtruth_keypoints] - self.assertIsNotNone(boxes) - self.assertIsNotNone(masks) - self.assertIsNotNone(keypoints) - - def testRandomPixelValueScale(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_pixel_value_scale, {})) - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images_min = tf.cast(images, dtype=tf.float32) * 0.9 / 255.0 - images_max = tf.cast(images, dtype=tf.float32) * 1.1 / 255.0 - images = tensor_dict[fields.InputDataFields.image] - values_greater = tf.greater_equal(images, images_min) - values_less = tf.less_equal(images, images_max) - values_true = tf.fill([1, 4, 4, 3], True) - return [values_greater, values_less, values_true] - - (values_greater_, values_less_, - values_true_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(values_greater_, values_true_) - self.assertAllClose(values_less_, values_true_) - - def testRandomPixelValueScaleWithCache(self): - preprocess_options = [] - preprocess_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocess_options.append((preprocessor.random_pixel_value_scale, {})) - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=False, - test_keypoints=False) - - def testRandomImageScale(self): - - def graph_fn(): - preprocess_options = [(preprocessor.random_image_scale, {})] - images_original = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images_original} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images_scaled = tensor_dict[fields.InputDataFields.image] - images_original_shape = tf.shape(images_original) - images_scaled_shape = tf.shape(images_scaled) - return [images_original_shape, images_scaled_shape] - - (images_original_shape_, - images_scaled_shape_) = self.execute_cpu(graph_fn, []) - self.assertLessEqual(images_original_shape_[1] * 0.5, - images_scaled_shape_[1]) - self.assertGreaterEqual(images_original_shape_[1] * 2.0, - images_scaled_shape_[1]) - self.assertLessEqual(images_original_shape_[2] * 0.5, - images_scaled_shape_[2]) - self.assertGreaterEqual(images_original_shape_[2] * 2.0, - images_scaled_shape_[2]) - - def testRandomImageScaleWithCache(self): - preprocess_options = [(preprocessor.random_image_scale, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=False, - test_masks=False, - test_keypoints=False) - - def testRandomRGBtoGray(self): - - def graph_fn(): - preprocess_options = [(preprocessor.random_rgb_to_gray, {})] - images_original = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images_original} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocess_options) - images_gray = tensor_dict[fields.InputDataFields.image] - images_gray_r, images_gray_g, images_gray_b = tf.split( - value=images_gray, num_or_size_splits=3, axis=3) - images_r, images_g, images_b = tf.split( - value=images_original, num_or_size_splits=3, axis=3) - images_r_diff1 = tf.squared_difference( - tf.cast(images_r, dtype=tf.float32), - tf.cast(images_gray_r, dtype=tf.float32)) - images_r_diff2 = tf.squared_difference( - tf.cast(images_gray_r, dtype=tf.float32), - tf.cast(images_gray_g, dtype=tf.float32)) - images_r_diff = tf.multiply(images_r_diff1, images_r_diff2) - images_g_diff1 = tf.squared_difference( - tf.cast(images_g, dtype=tf.float32), - tf.cast(images_gray_g, dtype=tf.float32)) - images_g_diff2 = tf.squared_difference( - tf.cast(images_gray_g, dtype=tf.float32), - tf.cast(images_gray_b, dtype=tf.float32)) - images_g_diff = tf.multiply(images_g_diff1, images_g_diff2) - images_b_diff1 = tf.squared_difference( - tf.cast(images_b, dtype=tf.float32), - tf.cast(images_gray_b, dtype=tf.float32)) - images_b_diff2 = tf.squared_difference( - tf.cast(images_gray_b, dtype=tf.float32), - tf.cast(images_gray_r, dtype=tf.float32)) - images_b_diff = tf.multiply(images_b_diff1, images_b_diff2) - image_zero1 = tf.constant(0, dtype=tf.float32, shape=[1, 4, 4, 1]) - return [images_r_diff, images_g_diff, images_b_diff, image_zero1] - - (images_r_diff_, images_g_diff_, images_b_diff_, - image_zero1_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(images_r_diff_, image_zero1_) - self.assertAllClose(images_g_diff_, image_zero1_) - self.assertAllClose(images_b_diff_, image_zero1_) - - def testRandomRGBtoGrayWithCache(self): - preprocess_options = [( - preprocessor.random_rgb_to_gray, {'probability': 0.5})] - self._testPreprocessorCache(preprocess_options, - test_boxes=False, - test_masks=False, - test_keypoints=False) - - def testRandomAdjustBrightness(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_adjust_brightness, {})) - images_original = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images_original} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images_bright = tensor_dict[fields.InputDataFields.image] - image_original_shape = tf.shape(images_original) - image_bright_shape = tf.shape(images_bright) - return [image_original_shape, image_bright_shape] - - (image_original_shape_, - image_bright_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image_original_shape_, image_bright_shape_) - - def testRandomAdjustBrightnessWithCache(self): - preprocess_options = [] - preprocess_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocess_options.append((preprocessor.random_adjust_brightness, {})) - self._testPreprocessorCache(preprocess_options, - test_boxes=False, - test_masks=False, - test_keypoints=False) - - def testRandomAdjustContrast(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_adjust_contrast, {})) - images_original = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images_original} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images_contrast = tensor_dict[fields.InputDataFields.image] - image_original_shape = tf.shape(images_original) - image_contrast_shape = tf.shape(images_contrast) - return [image_original_shape, image_contrast_shape] - - (image_original_shape_, - image_contrast_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image_original_shape_, image_contrast_shape_) - - def testRandomAdjustContrastWithCache(self): - preprocess_options = [] - preprocess_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocess_options.append((preprocessor.random_adjust_contrast, {})) - self._testPreprocessorCache(preprocess_options, - test_boxes=False, - test_masks=False, - test_keypoints=False) - - def testRandomAdjustHue(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_adjust_hue, {})) - images_original = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images_original} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images_hue = tensor_dict[fields.InputDataFields.image] - image_original_shape = tf.shape(images_original) - image_hue_shape = tf.shape(images_hue) - return [image_original_shape, image_hue_shape] - - (image_original_shape_, image_hue_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image_original_shape_, image_hue_shape_) - - def testRandomAdjustHueWithCache(self): - preprocess_options = [] - preprocess_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocess_options.append((preprocessor.random_adjust_hue, {})) - self._testPreprocessorCache(preprocess_options, - test_boxes=False, - test_masks=False, - test_keypoints=False) - - def testRandomDistortColor(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_distort_color, {})) - images_original = self.createTestImages() - images_original_shape = tf.shape(images_original) - tensor_dict = {fields.InputDataFields.image: images_original} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images_distorted_color = tensor_dict[fields.InputDataFields.image] - images_distorted_color_shape = tf.shape(images_distorted_color) - return [images_original_shape, images_distorted_color_shape] - - (images_original_shape_, - images_distorted_color_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_original_shape_, images_distorted_color_shape_) - - def testRandomDistortColorWithCache(self): - preprocess_options = [] - preprocess_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocess_options.append((preprocessor.random_distort_color, {})) - self._testPreprocessorCache(preprocess_options, - test_boxes=False, - test_masks=False, - test_keypoints=False) - - def testRandomJitterBoxes(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, {})) - boxes = self.createRandomTextBoxes() - boxes_shape = tf.shape(boxes) - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - distorted_boxes_shape = tf.shape(distorted_boxes) - return [boxes_shape, distorted_boxes_shape] - - (boxes_shape_, distorted_boxes_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_shape_, distorted_boxes_shape_) - - @parameterized.parameters( - ['expand', 'shrink', 'expand_symmetric', 'shrink_symmetric', - 'expand_symmetric_xy', 'shrink_symmetric_xy'] - ) - def testRandomJitterBoxesZeroRatio(self, jitter_mode): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, - {'ratio': .0, 'jitter_mode': jitter_mode})) - boxes = self.createRandomTextBoxes() - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - return [boxes, distorted_boxes] - - (boxes, distorted_boxes) = self.execute_cpu(graph_fn, []) - self.assertAllClose(boxes, distorted_boxes) - - def testRandomJitterBoxesExpand(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, - {'jitter_mode': 'expand'})) - boxes = self.createRandomTextBoxes() - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - return [boxes, distorted_boxes] - - boxes, distorted_boxes = self.execute_cpu(graph_fn, []) - ymin, xmin, ymax, xmax = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3] - distorted_ymin, distorted_xmin, distorted_ymax, distorted_xmax = ( - distorted_boxes[:, 0], distorted_boxes[:, 1], distorted_boxes[:, 2], - distorted_boxes[:, 3]) - - self.assertTrue(np.all(distorted_ymin <= ymin)) - self.assertTrue(np.all(distorted_xmin <= xmin)) - self.assertTrue(np.all(distorted_ymax >= ymax)) - self.assertTrue(np.all(distorted_xmax >= xmax)) - - def testRandomJitterBoxesExpandSymmetric(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, - {'jitter_mode': 'expand_symmetric'})) - boxes = self.createRandomTextBoxes() - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - return [boxes, distorted_boxes] - - boxes, distorted_boxes = self.execute_cpu(graph_fn, []) - ymin, xmin, ymax, xmax = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3] - distorted_ymin, distorted_xmin, distorted_ymax, distorted_xmax = ( - distorted_boxes[:, 0], distorted_boxes[:, 1], distorted_boxes[:, 2], - distorted_boxes[:, 3]) - - self.assertTrue(np.all(distorted_ymin <= ymin)) - self.assertTrue(np.all(distorted_xmin <= xmin)) - self.assertTrue(np.all(distorted_ymax >= ymax)) - self.assertTrue(np.all(distorted_xmax >= xmax)) - - self.assertAllClose(ymin - distorted_ymin, distorted_ymax - ymax, rtol=1e-5) - self.assertAllClose(xmin - distorted_xmin, distorted_xmax - xmax, rtol=1e-5) - - def testRandomJitterBoxesExpandSymmetricXY(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, - {'jitter_mode': 'expand_symmetric_xy'})) - boxes = self.createRandomTextBoxes() - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - return [boxes, distorted_boxes] - - boxes, distorted_boxes = self.execute_cpu(graph_fn, []) - ymin, xmin, ymax, xmax = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3] - distorted_ymin, distorted_xmin, distorted_ymax, distorted_xmax = ( - distorted_boxes[:, 0], distorted_boxes[:, 1], distorted_boxes[:, 2], - distorted_boxes[:, 3]) - - self.assertTrue(np.all(distorted_ymin <= ymin)) - self.assertTrue(np.all(distorted_xmin <= xmin)) - self.assertTrue(np.all(distorted_ymax >= ymax)) - self.assertTrue(np.all(distorted_xmax >= xmax)) - - self.assertAllClose(ymin - distorted_ymin, distorted_ymax - ymax, rtol=1e-5) - self.assertAllClose(xmin - distorted_xmin, distorted_xmax - xmax, rtol=1e-5) - - height, width = tf.maximum(1e-6, ymax - ymin), tf.maximum(1e-6, xmax - xmin) - - self.assertAllClose((distorted_ymax - ymax) / height, - (distorted_xmax - xmax) / width, rtol=1e-5) - self.assertAllLessEqual((distorted_ymax - ymax) / height, 0.05) - self.assertAllGreaterEqual((distorted_ymax - ymax) / width, 0.00) - - def testRandomJitterBoxesShrink(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, - {'jitter_mode': 'shrink'})) - boxes = self.createTestBoxes() - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - return [boxes, distorted_boxes] - - boxes, distorted_boxes = self.execute_cpu(graph_fn, []) - ymin, xmin, ymax, xmax = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3] - distorted_ymin, distorted_xmin, distorted_ymax, distorted_xmax = ( - distorted_boxes[:, 0], distorted_boxes[:, 1], distorted_boxes[:, 2], - distorted_boxes[:, 3]) - - self.assertTrue(np.all(distorted_ymin >= ymin)) - self.assertTrue(np.all(distorted_xmin >= xmin)) - self.assertTrue(np.all(distorted_ymax <= ymax)) - self.assertTrue(np.all(distorted_xmax <= xmax)) - - def testRandomJitterBoxesShrinkSymmetric(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, - {'jitter_mode': 'shrink_symmetric'})) - boxes = self.createTestBoxes() - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - return [boxes, distorted_boxes] - - boxes, distorted_boxes = self.execute_cpu(graph_fn, []) - ymin, xmin, ymax, xmax = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3] - distorted_ymin, distorted_xmin, distorted_ymax, distorted_xmax = ( - distorted_boxes[:, 0], distorted_boxes[:, 1], distorted_boxes[:, 2], - distorted_boxes[:, 3]) - - self.assertTrue(np.all(distorted_ymin >= ymin)) - self.assertTrue(np.all(distorted_xmin >= xmin)) - self.assertTrue(np.all(distorted_ymax <= ymax)) - self.assertTrue(np.all(distorted_xmax <= xmax)) - - self.assertAllClose(ymin - distorted_ymin, distorted_ymax - ymax, rtol=1e-5) - self.assertAllClose(xmin - distorted_xmin, distorted_xmax - xmax, rtol=1e-5) - - def testRandomJitterBoxesShrinkSymmetricXY(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.random_jitter_boxes, - {'jitter_mode': 'shrink_symmetric_xy'})) - boxes = self.createTestBoxes() - tensor_dict = {fields.InputDataFields.groundtruth_boxes: boxes} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - distorted_boxes = tensor_dict[fields.InputDataFields.groundtruth_boxes] - return [boxes, distorted_boxes] - - boxes, distorted_boxes = self.execute_cpu(graph_fn, []) - ymin, xmin, ymax, xmax = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3] - distorted_ymin, distorted_xmin, distorted_ymax, distorted_xmax = ( - distorted_boxes[:, 0], distorted_boxes[:, 1], distorted_boxes[:, 2], - distorted_boxes[:, 3]) - - self.assertTrue(np.all(distorted_ymin >= ymin)) - self.assertTrue(np.all(distorted_xmin >= xmin)) - self.assertTrue(np.all(distorted_ymax <= ymax)) - self.assertTrue(np.all(distorted_xmax <= xmax)) - - self.assertAllClose(ymin - distorted_ymin, distorted_ymax - ymax, rtol=1e-5) - self.assertAllClose(xmin - distorted_xmin, distorted_xmax - xmax, rtol=1e-5) - - height, width = tf.maximum(1e-6, ymax - ymin), tf.maximum(1e-6, xmax - xmin) - self.assertAllClose((ymax - distorted_ymax) / height, - (xmax - distorted_xmax) / width, rtol=1e-5) - self.assertAllLessEqual((ymax - distorted_ymax) / height, 0.05) - self.assertAllGreaterEqual((ymax - distorted_ymax)/ width, 0.00) - - def testRandomCropImage(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_crop_image, {})) - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - return [ - boxes_rank, distorted_boxes_rank, images_rank, distorted_images_rank - ] - - (boxes_rank_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - - def testRandomCropImageWithCache(self): - preprocess_options = [(preprocessor.random_rgb_to_gray, - {'probability': 0.5}), - (preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1, - }), - (preprocessor.random_crop_image, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=False, - test_keypoints=False) - - def testRandomCropImageGrayscale(self): - - def graph_fn(): - preprocessing_options = [(preprocessor.rgb_to_gray, {}), - (preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1, - }), (preprocessor.random_crop_image, {})] - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - return [ - boxes_rank, distorted_boxes_rank, images_rank, distorted_images_rank - ] - - (boxes_rank_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - - def testRandomCropImageWithBoxOutOfImage(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_crop_image, {})) - images = self.createTestImages() - boxes = self.createTestBoxesOutOfImage() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - return [ - boxes_rank, distorted_boxes_rank, images_rank, distorted_images_rank - ] - - (boxes_rank_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - - def testRandomCropImageWithRandomCoefOne(self): - - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })] - - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights - } - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_crop_image, { - 'random_coef': 1.0 - })] - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_weights = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_weights] - boxes_shape = tf.shape(boxes) - distorted_boxes_shape = tf.shape(distorted_boxes) - images_shape = tf.shape(images) - distorted_images_shape = tf.shape(distorted_images) - return [ - boxes_shape, distorted_boxes_shape, images_shape, - distorted_images_shape, images, distorted_images, boxes, - distorted_boxes, labels, distorted_labels, weights, distorted_weights - ] - - (boxes_shape_, distorted_boxes_shape_, images_shape_, - distorted_images_shape_, images_, distorted_images_, boxes_, - distorted_boxes_, labels_, distorted_labels_, weights_, - distorted_weights_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_shape_, distorted_boxes_shape_) - self.assertAllEqual(images_shape_, distorted_images_shape_) - self.assertAllClose(images_, distorted_images_) - self.assertAllClose(boxes_, distorted_boxes_) - self.assertAllEqual(labels_, distorted_labels_) - self.assertAllEqual(weights_, distorted_weights_) - - def testRandomCropWithMockSampleDistortedBoundingBox(self): - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })] - - images = self.createColorfulTestImage() - boxes = tf.constant([[0.1, 0.1, 0.8, 0.3], [0.2, 0.4, 0.75, 0.75], - [0.3, 0.1, 0.4, 0.7]], - dtype=tf.float32) - labels = tf.constant([1, 7, 11], dtype=tf.int32) - weights = tf.constant([1.0, 0.5, 0.6], dtype=tf.float32) - - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_crop_image, {})] - - with mock.patch.object(tf.image, 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = (tf.constant( - [6, 143, 0], dtype=tf.int32), tf.constant( - [190, 237, -1], dtype=tf.int32), tf.constant( - [[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_weights = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_weights] - expected_boxes = tf.constant( - [[0.178947, 0.07173, 0.75789469, 0.66244733], - [0.28421, 0.0, 0.38947365, 0.57805908]], - dtype=tf.float32) - expected_labels = tf.constant([7, 11], dtype=tf.int32) - expected_weights = tf.constant([0.5, 0.6], dtype=tf.float32) - return [ - distorted_boxes, distorted_labels, distorted_weights, - expected_boxes, expected_labels, expected_weights - ] - - (distorted_boxes_, distorted_labels_, distorted_weights_, expected_boxes_, - expected_labels_, expected_weights_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(distorted_boxes_, expected_boxes_) - self.assertAllEqual(distorted_labels_, expected_labels_) - self.assertAllEqual(distorted_weights_, expected_weights_) - - def testRandomCropWithoutClipBoxes(self): - - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })] - - images = self.createColorfulTestImage() - boxes = tf.constant([[0.1, 0.1, 0.8, 0.3], - [0.2, 0.4, 0.75, 0.75], - [0.3, 0.1, 0.4, 0.7]], dtype=tf.float32) - keypoints = tf.constant([ - [[0.1, 0.1], [0.8, 0.3]], - [[0.2, 0.4], [0.75, 0.75]], - [[0.3, 0.1], [0.4, 0.7]], - ], dtype=tf.float32) - labels = tf.constant([1, 7, 11], dtype=tf.int32) - weights = tf.constant([1.0, 0.5, 0.6], dtype=tf.float32) - - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_keypoints: keypoints, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - - preprocessing_options = [(preprocessor.random_crop_image, { - 'clip_boxes': False, - })] - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_keypoints=True) - with mock.patch.object(tf.image, 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = (tf.constant( - [6, 143, 0], dtype=tf.int32), tf.constant( - [190, 237, -1], dtype=tf.int32), tf.constant( - [[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_keypoints = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_weights = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_weights] - expected_boxes = tf.constant( - [[0.178947, 0.07173, 0.75789469, 0.66244733], - [0.28421, -0.434599, 0.38947365, 0.57805908]], - dtype=tf.float32) - expected_keypoints = tf.constant( - [[[0.178947, 0.07173], [0.75789469, 0.66244733]], - [[0.28421, -0.434599], [0.38947365, 0.57805908]]], - dtype=tf.float32) - expected_labels = tf.constant([7, 11], dtype=tf.int32) - expected_weights = tf.constant([0.5, 0.6], dtype=tf.float32) - return [distorted_boxes, distorted_keypoints, distorted_labels, - distorted_weights, expected_boxes, expected_keypoints, - expected_labels, expected_weights] - - (distorted_boxes_, distorted_keypoints_, distorted_labels_, - distorted_weights_, expected_boxes_, expected_keypoints_, expected_labels_, - expected_weights_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(distorted_boxes_, expected_boxes_) - self.assertAllClose(distorted_keypoints_, expected_keypoints_) - self.assertAllEqual(distorted_labels_, expected_labels_) - self.assertAllEqual(distorted_weights_, expected_weights_) - - def testRandomCropImageWithMultiClassScores(self): - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_crop_image, {})) - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - multiclass_scores = self.createTestMultiClassScores() - - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.multiclass_scores: multiclass_scores - } - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_multiclass_scores = distorted_tensor_dict[ - fields.InputDataFields.multiclass_scores] - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - multiclass_scores_rank = tf.rank(multiclass_scores) - distorted_multiclass_scores_rank = tf.rank(distorted_multiclass_scores) - return [ - boxes_rank, distorted_boxes, distorted_boxes_rank, images_rank, - distorted_images_rank, multiclass_scores_rank, - distorted_multiclass_scores_rank, distorted_multiclass_scores - ] - - (boxes_rank_, distorted_boxes_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_, multiclass_scores_rank_, - distorted_multiclass_scores_rank_, - distorted_multiclass_scores_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - self.assertAllEqual(multiclass_scores_rank_, - distorted_multiclass_scores_rank_) - self.assertAllEqual(distorted_boxes_.shape[0], - distorted_multiclass_scores_.shape[0]) - - def testStrictRandomCropImageWithGroundtruthWeights(self): - def graph_fn(): - image = self.createColorfulTestImage()[0] - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 143, 0], dtype=tf.int32), - tf.constant([190, 237, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - new_image, new_boxes, new_labels, new_groundtruth_weights = ( - preprocessor._strict_random_crop_image( - image, boxes, labels, weights)) - return [new_image, new_boxes, new_labels, new_groundtruth_weights] - (new_image, new_boxes, _, - new_groundtruth_weights) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array( - [[0.0, 0.0, 0.75789469, 1.0], - [0.23157893, 0.24050637, 0.75789469, 1.0]], dtype=np.float32) - self.assertAllEqual(new_image.shape, [190, 237, 3]) - self.assertAllEqual(new_groundtruth_weights, [1.0, 0.5]) - self.assertAllClose( - new_boxes.flatten(), expected_boxes.flatten()) - - def testStrictRandomCropImageWithMasks(self): - def graph_fn(): - image = self.createColorfulTestImage()[0] - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = tf.random_uniform([2, 200, 400], dtype=tf.float32) - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 143, 0], dtype=tf.int32), - tf.constant([190, 237, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - new_image, new_boxes, new_labels, new_weights, new_masks = ( - preprocessor._strict_random_crop_image( - image, boxes, labels, weights, masks=masks)) - return [new_image, new_boxes, new_labels, new_weights, new_masks] - (new_image, new_boxes, _, _, - new_masks) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array( - [[0.0, 0.0, 0.75789469, 1.0], - [0.23157893, 0.24050637, 0.75789469, 1.0]], dtype=np.float32) - self.assertAllEqual(new_image.shape, [190, 237, 3]) - self.assertAllEqual(new_masks.shape, [2, 190, 237]) - self.assertAllClose( - new_boxes.flatten(), expected_boxes.flatten()) - - def testStrictRandomCropImageWithMaskWeights(self): - def graph_fn(): - image = self.createColorfulTestImage()[0] - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = tf.random_uniform([2, 200, 400], dtype=tf.float32) - mask_weights = tf.constant([1.0, 0.0], dtype=tf.float32) - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 143, 0], dtype=tf.int32), - tf.constant([190, 237, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - results = preprocessor._strict_random_crop_image( - image, boxes, labels, weights, masks=masks, - mask_weights=mask_weights) - return results - (new_image, new_boxes, _, _, - new_masks, new_mask_weights) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array( - [[0.0, 0.0, 0.75789469, 1.0], - [0.23157893, 0.24050637, 0.75789469, 1.0]], dtype=np.float32) - self.assertAllEqual(new_image.shape, [190, 237, 3]) - self.assertAllEqual(new_masks.shape, [2, 190, 237]) - self.assertAllClose(new_mask_weights, [1.0, 0.0]) - self.assertAllClose( - new_boxes.flatten(), expected_boxes.flatten()) - - def testStrictRandomCropImageWithKeypoints(self): - def graph_fn(): - image = self.createColorfulTestImage()[0] - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - keypoints, keypoint_visibilities = self.createTestKeypoints() - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 143, 0], dtype=tf.int32), - tf.constant([190, 237, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - (new_image, new_boxes, new_labels, new_weights, new_keypoints, - new_keypoint_visibilities) = preprocessor._strict_random_crop_image( - image, boxes, labels, weights, keypoints=keypoints, - keypoint_visibilities=keypoint_visibilities) - return [new_image, new_boxes, new_labels, new_weights, new_keypoints, - new_keypoint_visibilities] - (new_image, new_boxes, _, _, new_keypoints, - new_keypoint_visibilities) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array([ - [0.0, 0.0, 0.75789469, 1.0], - [0.23157893, 0.24050637, 0.75789469, 1.0],], dtype=np.float32) - expected_keypoints = np.array([ - [[np.nan, np.nan], - [np.nan, np.nan], - [np.nan, np.nan]], - [[0.38947368, 0.07173], - [0.49473682, 0.24050637], - [0.60000002, 0.40928277]] - ], dtype=np.float32) - expected_keypoint_visibilities = [ - [False, False, False], - [False, True, True] - ] - self.assertAllEqual(new_image.shape, [190, 237, 3]) - self.assertAllClose( - new_boxes, expected_boxes) - self.assertAllClose( - new_keypoints, expected_keypoints) - self.assertAllEqual( - new_keypoint_visibilities, expected_keypoint_visibilities) - - def testRunRandomCropImageWithMasks(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = tf.random_uniform([2, 200, 400], dtype=tf.float32) - mask_weights = tf.constant([1.0, 0.0], dtype=tf.float32) - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_instance_masks: masks, - fields.InputDataFields.groundtruth_instance_mask_weights: - mask_weights - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True, include_instance_mask_weights=True) - - preprocessing_options = [(preprocessor.random_crop_image, {})] - - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 143, 0], dtype=tf.int32), - tf.constant([190, 237, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, - preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_masks = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_instance_masks] - distorted_mask_weights = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_instance_mask_weights] - return [distorted_image, distorted_boxes, distorted_labels, - distorted_masks, distorted_mask_weights] - (distorted_image_, distorted_boxes_, distorted_labels_, - distorted_masks_, distorted_mask_weights_) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array([ - [0.0, 0.0, 0.75789469, 1.0], - [0.23157893, 0.24050637, 0.75789469, 1.0], - ], dtype=np.float32) - self.assertAllEqual(distorted_image_.shape, [1, 190, 237, 3]) - self.assertAllEqual(distorted_masks_.shape, [2, 190, 237]) - self.assertAllClose(distorted_mask_weights_, [1.0, 0.0]) - self.assertAllEqual(distorted_labels_, [1, 2]) - self.assertAllClose( - distorted_boxes_.flatten(), expected_boxes.flatten()) - - def testRunRandomCropImageWithKeypointsInsideCrop(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - keypoints = self.createTestKeypointsInsideCrop() - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_keypoints: keypoints, - fields.InputDataFields.groundtruth_weights: weights - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_keypoints=True) - - preprocessing_options = [(preprocessor.random_crop_image, {})] - - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 143, 0], dtype=tf.int32), - tf.constant([190, 237, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, - preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_keypoints = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - return [distorted_image, distorted_boxes, distorted_labels, - distorted_keypoints] - (distorted_image_, distorted_boxes_, distorted_labels_, - distorted_keypoints_) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array([ - [0.0, 0.0, 0.75789469, 1.0], - [0.23157893, 0.24050637, 0.75789469, 1.0], - ], dtype=np.float32) - expected_keypoints = np.array([ - [[0.38947368, 0.07173], - [0.49473682, 0.24050637], - [0.60000002, 0.40928277]], - [[0.38947368, 0.07173], - [0.49473682, 0.24050637], - [0.60000002, 0.40928277]] - ]) - self.assertAllEqual(distorted_image_.shape, [1, 190, 237, 3]) - self.assertAllEqual(distorted_labels_, [1, 2]) - self.assertAllClose( - distorted_boxes_.flatten(), expected_boxes.flatten()) - self.assertAllClose( - distorted_keypoints_.flatten(), expected_keypoints.flatten()) - - def testRunRandomCropImageWithKeypointsOutsideCrop(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - keypoints = self.createTestKeypointsOutsideCrop() - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_keypoints: keypoints - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_keypoints=True) - - preprocessing_options = [(preprocessor.random_crop_image, {})] - - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 143, 0], dtype=tf.int32), - tf.constant([190, 237, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.3575, 0.98, 0.95]]], dtype=tf.float32)) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, - preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_keypoints = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - return [distorted_image, distorted_boxes, distorted_labels, - distorted_keypoints] - (distorted_image_, distorted_boxes_, distorted_labels_, - distorted_keypoints_) = self.execute_cpu(graph_fn, []) - - expected_boxes = np.array([ - [0.0, 0.0, 0.75789469, 1.0], - [0.23157893, 0.24050637, 0.75789469, 1.0], - ], dtype=np.float32) - expected_keypoints = np.array([ - [[np.nan, np.nan], - [np.nan, np.nan], - [np.nan, np.nan]], - [[np.nan, np.nan], - [np.nan, np.nan], - [np.nan, np.nan]], - ]) - self.assertAllEqual(distorted_image_.shape, [1, 190, 237, 3]) - self.assertAllEqual(distorted_labels_, [1, 2]) - self.assertAllClose( - distorted_boxes_.flatten(), expected_boxes.flatten()) - self.assertAllClose( - distorted_keypoints_.flatten(), expected_keypoints.flatten()) - - def testRunRandomCropImageWithDensePose(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - dp_num_points, dp_part_ids, dp_surface_coords = self.createTestDensePose() - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_dp_num_points: dp_num_points, - fields.InputDataFields.groundtruth_dp_part_ids: dp_part_ids, - fields.InputDataFields.groundtruth_dp_surface_coords: - dp_surface_coords - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_dense_pose=True) - - preprocessing_options = [(preprocessor.random_crop_image, {})] - - with mock.patch.object( - tf.image, - 'sample_distorted_bounding_box' - ) as mock_sample_distorted_bounding_box: - mock_sample_distorted_bounding_box.return_value = ( - tf.constant([6, 40, 0], dtype=tf.int32), - tf.constant([134, 340, -1], dtype=tf.int32), - tf.constant([[[0.03, 0.1, 0.7, 0.95]]], dtype=tf.float32)) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, - preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_dp_num_points = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_dp_num_points] - distorted_dp_part_ids = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_dp_part_ids] - distorted_dp_surface_coords = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_dp_surface_coords] - return [distorted_image, distorted_dp_num_points, distorted_dp_part_ids, - distorted_dp_surface_coords] - (distorted_image_, distorted_dp_num_points_, distorted_dp_part_ids_, - distorted_dp_surface_coords_) = self.execute_cpu(graph_fn, []) - expected_dp_num_points = np.array([1, 1]) - expected_dp_part_ids = np.array([[4], [0]]) - expected_dp_surface_coords = np.array([ - [[0.10447761, 0.1176470, 0.6, 0.7]], - [[0.10447761, 0.2352941, 0.2, 0.8]], - ]) - self.assertAllEqual(distorted_image_.shape, [1, 134, 340, 3]) - self.assertAllEqual(distorted_dp_num_points_, expected_dp_num_points) - self.assertAllEqual(distorted_dp_part_ids_, expected_dp_part_ids) - self.assertAllClose(distorted_dp_surface_coords_, - expected_dp_surface_coords) - - def testRunRetainBoxesAboveThreshold(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - - tensor_dict = { - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - - preprocessing_options = [ - (preprocessor.retain_boxes_above_threshold, {'threshold': 0.6}) - ] - preprocessor_arg_map = preprocessor.get_default_func_arg_map() - retained_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - retained_boxes = retained_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - retained_labels = retained_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - retained_weights = retained_tensor_dict[ - fields.InputDataFields.groundtruth_weights] - return [retained_boxes, retained_labels, retained_weights, - self.expectedBoxesAfterThresholding(), - self.expectedLabelsAfterThresholding(), - self.expectedLabelScoresAfterThresholding()] - - (retained_boxes_, retained_labels_, retained_weights_, - expected_retained_boxes_, expected_retained_labels_, - expected_retained_weights_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(retained_boxes_, expected_retained_boxes_) - self.assertAllClose(retained_labels_, expected_retained_labels_) - self.assertAllClose( - retained_weights_, expected_retained_weights_) - - def testRunRetainBoxesAboveThresholdWithMasks(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = self.createTestMasks() - - tensor_dict = { - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_instance_masks: masks - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_label_weights=True, - include_instance_masks=True) - - preprocessing_options = [ - (preprocessor.retain_boxes_above_threshold, {'threshold': 0.6}) - ] - - retained_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - retained_masks = retained_tensor_dict[ - fields.InputDataFields.groundtruth_instance_masks] - return [retained_masks, self.expectedMasksAfterThresholding()] - (retained_masks_, expected_masks_) = self.execute(graph_fn, []) - self.assertAllClose(retained_masks_, expected_masks_) - - def testRunRetainBoxesAboveThresholdWithKeypoints(self): - def graph_fn(): - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - keypoints, _ = self.createTestKeypoints() - - tensor_dict = { - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_keypoints: keypoints - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_keypoints=True) - - preprocessing_options = [ - (preprocessor.retain_boxes_above_threshold, {'threshold': 0.6}) - ] - - retained_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - retained_keypoints = retained_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - return [retained_keypoints, self.expectedKeypointsAfterThresholding()] - (retained_keypoints_, expected_keypoints_) = self.execute_cpu(graph_fn, []) - self.assertAllClose(retained_keypoints_, expected_keypoints_) - - def testRandomCropToAspectRatioWithCache(self): - preprocess_options = [(preprocessor.random_crop_to_aspect_ratio, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=False, - test_keypoints=False) - - def testRunRandomCropToAspectRatioWithMasks(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = tf.random_uniform([2, 200, 400], dtype=tf.float32) - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_instance_masks: masks - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True) - - preprocessing_options = [(preprocessor.random_crop_to_aspect_ratio, {})] - - with mock.patch.object(preprocessor, - '_random_integer') as mock_random_integer: - mock_random_integer.return_value = tf.constant(0, dtype=tf.int32) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, - preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_masks = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_instance_masks] - return [ - distorted_image, distorted_boxes, distorted_labels, distorted_masks - ] - - (distorted_image_, distorted_boxes_, distorted_labels_, - distorted_masks_) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array([0.0, 0.5, 0.75, 1.0], dtype=np.float32) - self.assertAllEqual(distorted_image_.shape, [1, 200, 200, 3]) - self.assertAllEqual(distorted_labels_, [1]) - self.assertAllClose(distorted_boxes_.flatten(), - expected_boxes.flatten()) - self.assertAllEqual(distorted_masks_.shape, [1, 200, 200]) - - def testRunRandomCropToAspectRatioCenterCrop(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - masks = tf.random_uniform([2, 200, 400], dtype=tf.float32) - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_instance_masks: masks - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True) - - preprocessing_options = [(preprocessor.random_crop_to_aspect_ratio, { - 'center_crop': True - })] - - with mock.patch.object(preprocessor, - '_random_integer') as mock_random_integer: - mock_random_integer.return_value = tf.constant(0, dtype=tf.int32) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, - preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - return [ - distorted_image, distorted_boxes, distorted_labels - ] - - (distorted_image_, distorted_boxes_, distorted_labels_) = self.execute_cpu( - graph_fn, []) - expected_boxes = np.array([[0.0, 0.0, 0.75, 1.0], - [0.25, 0.5, 0.75, 1.0]], dtype=np.float32) - self.assertAllEqual(distorted_image_.shape, [1, 200, 200, 3]) - self.assertAllEqual(distorted_labels_, [1, 2]) - self.assertAllClose(distorted_boxes_.flatten(), - expected_boxes.flatten()) - - def testRunRandomCropToAspectRatioWithKeypoints(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - keypoints, _ = self.createTestKeypoints() - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_keypoints: keypoints - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_keypoints=True) - - preprocessing_options = [(preprocessor.random_crop_to_aspect_ratio, {})] - - with mock.patch.object(preprocessor, - '_random_integer') as mock_random_integer: - mock_random_integer.return_value = tf.constant(0, dtype=tf.int32) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, - preprocessing_options, - func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_keypoints = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - return [distorted_image, distorted_boxes, distorted_labels, - distorted_keypoints] - (distorted_image_, distorted_boxes_, distorted_labels_, - distorted_keypoints_) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array([0.0, 0.5, 0.75, 1.0], dtype=np.float32) - expected_keypoints = np.array( - [[0.1, 0.2], [0.2, 0.4], [0.3, 0.6]], dtype=np.float32) - self.assertAllEqual(distorted_image_.shape, [1, 200, 200, 3]) - self.assertAllEqual(distorted_labels_, [1]) - self.assertAllClose(distorted_boxes_.flatten(), - expected_boxes.flatten()) - self.assertAllClose(distorted_keypoints_.flatten(), - expected_keypoints.flatten()) - - def testRandomPadToAspectRatioWithCache(self): - preprocess_options = [(preprocessor.random_pad_to_aspect_ratio, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - def testRunRandomPadToAspectRatioWithMinMaxPaddedSizeRatios(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map() - preprocessing_options = [(preprocessor.random_pad_to_aspect_ratio, - {'min_padded_size_ratio': (4.0, 4.0), - 'max_padded_size_ratio': (4.0, 4.0)})] - - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - return [distorted_image, distorted_boxes, distorted_labels] - - distorted_image_, distorted_boxes_, distorted_labels_ = self.execute_cpu( - graph_fn, []) - expected_boxes = np.array( - [[0.0, 0.125, 0.1875, 0.5], [0.0625, 0.25, 0.1875, 0.5]], - dtype=np.float32) - self.assertAllEqual(distorted_image_.shape, [1, 800, 800, 3]) - self.assertAllEqual(distorted_labels_, [1, 2]) - self.assertAllClose(distorted_boxes_.flatten(), - expected_boxes.flatten()) - - def testRunRandomPadToAspectRatioWithMasks(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - masks = tf.random_uniform([2, 200, 400], dtype=tf.float32) - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_instance_masks: masks - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True) - - preprocessing_options = [(preprocessor.random_pad_to_aspect_ratio, {})] - - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_masks = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_instance_masks] - return [ - distorted_image, distorted_boxes, distorted_labels, distorted_masks - ] - - (distorted_image_, distorted_boxes_, distorted_labels_, - distorted_masks_) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array( - [[0.0, 0.25, 0.375, 1.0], [0.125, 0.5, 0.375, 1.0]], dtype=np.float32) - self.assertAllEqual(distorted_image_.shape, [1, 400, 400, 3]) - self.assertAllEqual(distorted_labels_, [1, 2]) - self.assertAllClose(distorted_boxes_.flatten(), - expected_boxes.flatten()) - self.assertAllEqual(distorted_masks_.shape, [2, 400, 400]) - - def testRunRandomPadToAspectRatioWithKeypoints(self): - def graph_fn(): - image = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - keypoints, _ = self.createTestKeypoints() - - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_keypoints: keypoints - } - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_keypoints=True) - - preprocessing_options = [(preprocessor.random_pad_to_aspect_ratio, {})] - - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - distorted_image = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_labels = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - distorted_keypoints = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - return [ - distorted_image, distorted_boxes, distorted_labels, - distorted_keypoints - ] - - (distorted_image_, distorted_boxes_, distorted_labels_, - distorted_keypoints_) = self.execute_cpu(graph_fn, []) - expected_boxes = np.array( - [[0.0, 0.25, 0.375, 1.0], [0.125, 0.5, 0.375, 1.0]], dtype=np.float32) - expected_keypoints = np.array([ - [[0.05, 0.1], [0.1, 0.2], [0.15, 0.3]], - [[0.2, 0.4], [0.25, 0.5], [0.3, 0.6]], - ], dtype=np.float32) - self.assertAllEqual(distorted_image_.shape, [1, 400, 400, 3]) - self.assertAllEqual(distorted_labels_, [1, 2]) - self.assertAllClose(distorted_boxes_.flatten(), - expected_boxes.flatten()) - self.assertAllClose(distorted_keypoints_.flatten(), - expected_keypoints.flatten()) - - def testRandomPadImageWithCache(self): - preprocess_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1,}), (preprocessor.random_pad_image, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - def testRandomPadImage(self): - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })] - - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - } - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_pad_image, {})] - padded_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - - padded_images = padded_tensor_dict[fields.InputDataFields.image] - padded_boxes = padded_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - boxes_shape = tf.shape(boxes) - padded_boxes_shape = tf.shape(padded_boxes) - images_shape = tf.shape(images) - padded_images_shape = tf.shape(padded_images) - return [boxes_shape, padded_boxes_shape, images_shape, - padded_images_shape, boxes, padded_boxes] - (boxes_shape_, padded_boxes_shape_, images_shape_, - padded_images_shape_, boxes_, padded_boxes_) = self.execute_cpu(graph_fn, - []) - self.assertAllEqual(boxes_shape_, padded_boxes_shape_) - self.assertTrue((images_shape_[1] >= padded_images_shape_[1] * 0.5).all) - self.assertTrue((images_shape_[2] >= padded_images_shape_[2] * 0.5).all) - self.assertTrue((images_shape_[1] <= padded_images_shape_[1]).all) - self.assertTrue((images_shape_[2] <= padded_images_shape_[2]).all) - self.assertTrue(np.all((boxes_[:, 2] - boxes_[:, 0]) >= ( - padded_boxes_[:, 2] - padded_boxes_[:, 0]))) - self.assertTrue(np.all((boxes_[:, 3] - boxes_[:, 1]) >= ( - padded_boxes_[:, 3] - padded_boxes_[:, 1]))) - - def testRandomPadImageCenterPad(self): - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })] - - images = self.createColorfulTestImage() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - } - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_pad_image, { - 'center_pad': True, - 'min_image_size': [400, 400], - 'max_image_size': [400, 400], - })] - padded_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - - padded_images = padded_tensor_dict[fields.InputDataFields.image] - padded_boxes = padded_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - padded_labels = padded_tensor_dict[ - fields.InputDataFields.groundtruth_classes] - return [padded_images, padded_boxes, padded_labels] - (padded_images_, padded_boxes_, padded_labels_) = self.execute_cpu( - graph_fn, []) - - expected_boxes = np.array([[0.25, 0.25, 0.625, 1.0], - [0.375, 0.5, .625, 1.0]], dtype=np.float32) - - self.assertAllEqual(padded_images_.shape, [1, 400, 400, 3]) - self.assertAllEqual(padded_labels_, [1, 2]) - self.assertAllClose(padded_boxes_.flatten(), - expected_boxes.flatten()) - - @parameterized.parameters( - {'include_dense_pose': False}, - ) - def testRandomPadImageWithKeypointsAndMasks(self, include_dense_pose): - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })] - - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - masks = self.createTestMasks() - keypoints, _ = self.createTestKeypoints() - _, _, dp_surface_coords = self.createTestDensePose() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_instance_masks: masks, - fields.InputDataFields.groundtruth_keypoints: keypoints, - fields.InputDataFields.groundtruth_dp_surface_coords: - dp_surface_coords - } - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_pad_image, {})] - func_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True, - include_keypoints=True, - include_keypoint_visibilities=True, - include_dense_pose=include_dense_pose) - padded_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options, - func_arg_map=func_arg_map) - - padded_images = padded_tensor_dict[fields.InputDataFields.image] - padded_boxes = padded_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - padded_masks = padded_tensor_dict[ - fields.InputDataFields.groundtruth_instance_masks] - padded_keypoints = padded_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - boxes_shape = tf.shape(boxes) - padded_boxes_shape = tf.shape(padded_boxes) - padded_masks_shape = tf.shape(padded_masks) - keypoints_shape = tf.shape(keypoints) - padded_keypoints_shape = tf.shape(padded_keypoints) - images_shape = tf.shape(images) - padded_images_shape = tf.shape(padded_images) - outputs = [boxes_shape, padded_boxes_shape, padded_masks_shape, - keypoints_shape, padded_keypoints_shape, images_shape, - padded_images_shape, boxes, padded_boxes, keypoints, - padded_keypoints] - if include_dense_pose: - padded_dp_surface_coords = padded_tensor_dict[ - fields.InputDataFields.groundtruth_dp_surface_coords] - outputs.extend([dp_surface_coords, padded_dp_surface_coords]) - return outputs - - outputs = self.execute_cpu(graph_fn, []) - boxes_shape_ = outputs[0] - padded_boxes_shape_ = outputs[1] - padded_masks_shape_ = outputs[2] - keypoints_shape_ = outputs[3] - padded_keypoints_shape_ = outputs[4] - images_shape_ = outputs[5] - padded_images_shape_ = outputs[6] - boxes_ = outputs[7] - padded_boxes_ = outputs[8] - keypoints_ = outputs[9] - padded_keypoints_ = outputs[10] - - self.assertAllEqual(boxes_shape_, padded_boxes_shape_) - self.assertAllEqual(keypoints_shape_, padded_keypoints_shape_) - self.assertTrue((images_shape_[1] >= padded_images_shape_[1] * 0.5).all) - self.assertTrue((images_shape_[2] >= padded_images_shape_[2] * 0.5).all) - self.assertTrue((images_shape_[1] <= padded_images_shape_[1]).all) - self.assertTrue((images_shape_[2] <= padded_images_shape_[2]).all) - self.assertAllEqual(padded_masks_shape_[1:3], padded_images_shape_[1:3]) - self.assertTrue(np.all((boxes_[:, 2] - boxes_[:, 0]) >= ( - padded_boxes_[:, 2] - padded_boxes_[:, 0]))) - self.assertTrue(np.all((boxes_[:, 3] - boxes_[:, 1]) >= ( - padded_boxes_[:, 3] - padded_boxes_[:, 1]))) - self.assertTrue(np.all((keypoints_[1, :, 0] - keypoints_[0, :, 0]) >= ( - padded_keypoints_[1, :, 0] - padded_keypoints_[0, :, 0]))) - self.assertTrue(np.all((keypoints_[1, :, 1] - keypoints_[0, :, 1]) >= ( - padded_keypoints_[1, :, 1] - padded_keypoints_[0, :, 1]))) - if include_dense_pose: - dp_surface_coords = outputs[11] - padded_dp_surface_coords = outputs[12] - self.assertAllClose(padded_dp_surface_coords[:, :, 2:], - dp_surface_coords[:, :, 2:]) - - def testRandomAbsolutePadImage(self): - height_padding = 10 - width_padding = 20 - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - tensor_dict = { - fields.InputDataFields.image: tf.cast(images, dtype=tf.float32), - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - } - preprocessing_options = [(preprocessor.random_absolute_pad_image, { - 'max_height_padding': height_padding, - 'max_width_padding': width_padding})] - padded_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - original_shape = tf.shape(images) - final_shape = tf.shape(padded_tensor_dict[fields.InputDataFields.image]) - return original_shape, final_shape - for _ in range(100): - original_shape, output_shape = self.execute_cpu(graph_fn, []) - _, height, width, _ = original_shape - self.assertGreaterEqual(output_shape[1], height) - self.assertLess(output_shape[1], height + height_padding) - self.assertGreaterEqual(output_shape[2], width) - self.assertLess(output_shape[2], width + width_padding) - - def testRandomAbsolutePadImageWithKeypoints(self): - height_padding = 10 - width_padding = 20 - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - keypoints, _ = self.createTestKeypoints() - tensor_dict = { - fields.InputDataFields.image: tf.cast(images, dtype=tf.float32), - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_keypoints: keypoints, - } - - preprocessing_options = [(preprocessor.random_absolute_pad_image, { - 'max_height_padding': height_padding, - 'max_width_padding': width_padding - })] - padded_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - original_shape = tf.shape(images) - final_shape = tf.shape(padded_tensor_dict[fields.InputDataFields.image]) - padded_keypoints = padded_tensor_dict[ - fields.InputDataFields.groundtruth_keypoints] - return (original_shape, final_shape, padded_keypoints) - for _ in range(100): - original_shape, output_shape, padded_keypoints_ = self.execute_cpu( - graph_fn, []) - _, height, width, _ = original_shape - self.assertGreaterEqual(output_shape[1], height) - self.assertLess(output_shape[1], height + height_padding) - self.assertGreaterEqual(output_shape[2], width) - self.assertLess(output_shape[2], width + width_padding) - # Verify the keypoints are populated. The correctness of the keypoint - # coordinates are already tested in random_pad_image function. - self.assertEqual(padded_keypoints_.shape, (2, 3, 2)) - - def testRandomCropPadImageWithCache(self): - preprocess_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1,}), (preprocessor.random_crop_pad_image, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - def testRandomCropPadImageWithRandomCoefOne(self): - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })] - - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_crop_pad_image, { - 'random_coef': 1.0 - })] - padded_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - - padded_images = padded_tensor_dict[fields.InputDataFields.image] - padded_boxes = padded_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - boxes_shape = tf.shape(boxes) - padded_boxes_shape = tf.shape(padded_boxes) - images_shape = tf.shape(images) - padded_images_shape = tf.shape(padded_images) - return [boxes_shape, padded_boxes_shape, images_shape, - padded_images_shape, boxes, padded_boxes] - (boxes_shape_, padded_boxes_shape_, images_shape_, - padded_images_shape_, boxes_, padded_boxes_) = self.execute_cpu(graph_fn, - []) - self.assertAllEqual(boxes_shape_, padded_boxes_shape_) - self.assertTrue((images_shape_[1] >= padded_images_shape_[1] * 0.5).all) - self.assertTrue((images_shape_[2] >= padded_images_shape_[2] * 0.5).all) - self.assertTrue((images_shape_[1] <= padded_images_shape_[1]).all) - self.assertTrue((images_shape_[2] <= padded_images_shape_[2]).all) - self.assertTrue(np.all((boxes_[:, 2] - boxes_[:, 0]) >= ( - padded_boxes_[:, 2] - padded_boxes_[:, 0]))) - self.assertTrue(np.all((boxes_[:, 3] - boxes_[:, 1]) >= ( - padded_boxes_[:, 3] - padded_boxes_[:, 1]))) - - def testRandomCropToAspectRatio(self): - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - tensor_dict = preprocessor.preprocess(tensor_dict, []) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_crop_to_aspect_ratio, { - 'aspect_ratio': 2.0 - })] - cropped_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - - cropped_images = cropped_tensor_dict[fields.InputDataFields.image] - cropped_boxes = cropped_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - boxes_shape = tf.shape(boxes) - cropped_boxes_shape = tf.shape(cropped_boxes) - images_shape = tf.shape(images) - cropped_images_shape = tf.shape(cropped_images) - return [ - boxes_shape, cropped_boxes_shape, images_shape, cropped_images_shape - ] - - (boxes_shape_, cropped_boxes_shape_, images_shape_, - cropped_images_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_shape_, cropped_boxes_shape_) - self.assertEqual(images_shape_[1], cropped_images_shape_[1] * 2) - self.assertEqual(images_shape_[2], cropped_images_shape_[2]) - - def testRandomPadToAspectRatio(self): - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - } - tensor_dict = preprocessor.preprocess(tensor_dict, []) - images = tensor_dict[fields.InputDataFields.image] - - preprocessing_options = [(preprocessor.random_pad_to_aspect_ratio, { - 'aspect_ratio': 2.0 - })] - padded_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - - padded_images = padded_tensor_dict[fields.InputDataFields.image] - padded_boxes = padded_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - boxes_shape = tf.shape(boxes) - padded_boxes_shape = tf.shape(padded_boxes) - images_shape = tf.shape(images) - padded_images_shape = tf.shape(padded_images) - return [ - boxes_shape, padded_boxes_shape, images_shape, padded_images_shape - ] - - (boxes_shape_, padded_boxes_shape_, images_shape_, - padded_images_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_shape_, padded_boxes_shape_) - self.assertEqual(images_shape_[1], padded_images_shape_[1]) - self.assertEqual(2 * images_shape_[2], padded_images_shape_[2]) - - def testRandomBlackPatchesWithCache(self): - preprocess_options = [] - preprocess_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocess_options.append((preprocessor.random_black_patches, { - 'size_to_image_ratio': 0.5 - })) - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - def testRandomBlackPatches(self): - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_black_patches, { - 'size_to_image_ratio': 0.5 - })) - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - blacked_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - blacked_images = blacked_tensor_dict[fields.InputDataFields.image] - images_shape = tf.shape(images) - blacked_images_shape = tf.shape(blacked_images) - return [images_shape, blacked_images_shape] - (images_shape_, blacked_images_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_shape_, blacked_images_shape_) - - def testRandomJpegQuality(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_jpeg_quality, { - 'min_jpeg_quality': 0, - 'max_jpeg_quality': 100 - })] - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - encoded_images = processed_tensor_dict[fields.InputDataFields.image] - images_shape = tf.shape(images) - encoded_images_shape = tf.shape(encoded_images) - return [images_shape, encoded_images_shape] - images_shape_out, encoded_images_shape_out = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_shape_out, encoded_images_shape_out) - - def testRandomJpegQualityKeepsStaticChannelShape(self): - # Set at least three weeks past the forward compatibility horizon for - # tf 1.14 of 2019/11/01. - # https://github.com/tensorflow/tensorflow/blob/v1.14.0/tensorflow/python/compat/compat.py#L30 - if not tf.compat.forward_compatible(year=2019, month=12, day=1): - self.skipTest('Skipping test for future functionality.') - preprocessing_options = [(preprocessor.random_jpeg_quality, { - 'min_jpeg_quality': 0, - 'max_jpeg_quality': 100 - })] - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - encoded_images = processed_tensor_dict[fields.InputDataFields.image] - images_static_channels = images.shape[-1] - encoded_images_static_channels = encoded_images.shape[-1] - self.assertEqual(images_static_channels, encoded_images_static_channels) - - def testRandomJpegQualityWithCache(self): - preprocessing_options = [(preprocessor.random_jpeg_quality, { - 'min_jpeg_quality': 0, - 'max_jpeg_quality': 100 - })] - self._testPreprocessorCache(preprocessing_options) - - def testRandomJpegQualityWithRandomCoefOne(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_jpeg_quality, { - 'random_coef': 1.0 - })] - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - encoded_images = processed_tensor_dict[fields.InputDataFields.image] - images_shape = tf.shape(images) - encoded_images_shape = tf.shape(encoded_images) - return [images, encoded_images, images_shape, encoded_images_shape] - - (images_out, encoded_images_out, images_shape_out, - encoded_images_shape_out) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_shape_out, encoded_images_shape_out) - self.assertAllEqual(images_out, encoded_images_out) - - def testRandomDownscaleToTargetPixels(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_downscale_to_target_pixels, - { - 'min_target_pixels': 100, - 'max_target_pixels': 101 - })] - images = tf.random_uniform([1, 25, 100, 3]) - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - downscaled_images = processed_tensor_dict[fields.InputDataFields.image] - downscaled_shape = tf.shape(downscaled_images) - return downscaled_shape - expected_shape = [1, 5, 20, 3] - downscaled_shape_out = self.execute_cpu(graph_fn, []) - self.assertAllEqual(downscaled_shape_out, expected_shape) - - def testRandomDownscaleToTargetPixelsWithMasks(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_downscale_to_target_pixels, - { - 'min_target_pixels': 100, - 'max_target_pixels': 101 - })] - images = tf.random_uniform([1, 25, 100, 3]) - masks = tf.random_uniform([10, 25, 100]) - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_instance_masks: masks - } - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_instance_masks=True) - processed_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - downscaled_images = processed_tensor_dict[fields.InputDataFields.image] - downscaled_masks = processed_tensor_dict[ - fields.InputDataFields.groundtruth_instance_masks] - downscaled_images_shape = tf.shape(downscaled_images) - downscaled_masks_shape = tf.shape(downscaled_masks) - return [downscaled_images_shape, downscaled_masks_shape] - expected_images_shape = [1, 5, 20, 3] - expected_masks_shape = [10, 5, 20] - (downscaled_images_shape_out, - downscaled_masks_shape_out) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(downscaled_images_shape_out, expected_images_shape) - self.assertAllEqual(downscaled_masks_shape_out, expected_masks_shape) - - @parameterized.parameters( - {'test_masks': False}, - {'test_masks': True} - ) - def testRandomDownscaleToTargetPixelsWithCache(self, test_masks): - preprocessing_options = [(preprocessor.random_downscale_to_target_pixels, { - 'min_target_pixels': 100, - 'max_target_pixels': 999 - })] - self._testPreprocessorCache(preprocessing_options, test_masks=test_masks) - - def testRandomDownscaleToTargetPixelsWithRandomCoefOne(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_downscale_to_target_pixels, - { - 'random_coef': 1.0, - 'min_target_pixels': 10, - 'max_target_pixels': 20, - })] - images = tf.random_uniform([1, 25, 100, 3]) - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - downscaled_images = processed_tensor_dict[fields.InputDataFields.image] - images_shape = tf.shape(images) - downscaled_images_shape = tf.shape(downscaled_images) - return [images, downscaled_images, images_shape, downscaled_images_shape] - (images_out, downscaled_images_out, images_shape_out, - downscaled_images_shape_out) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_shape_out, downscaled_images_shape_out) - self.assertAllEqual(images_out, downscaled_images_out) - - def testRandomDownscaleToTargetPixelsIgnoresSmallImages(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_downscale_to_target_pixels, - { - 'min_target_pixels': 1000, - 'max_target_pixels': 1001 - })] - images = tf.random_uniform([1, 10, 10, 3]) - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - downscaled_images = processed_tensor_dict[fields.InputDataFields.image] - images_shape = tf.shape(images) - downscaled_images_shape = tf.shape(downscaled_images) - return [images, downscaled_images, images_shape, downscaled_images_shape] - (images_out, downscaled_images_out, images_shape_out, - downscaled_images_shape_out) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_shape_out, downscaled_images_shape_out) - self.assertAllEqual(images_out, downscaled_images_out) - - def testRandomPatchGaussianShape(self): - preprocessing_options = [(preprocessor.random_patch_gaussian, { - 'min_patch_size': 1, - 'max_patch_size': 200, - 'min_gaussian_stddev': 0.0, - 'max_gaussian_stddev': 2.0 - })] - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - patched_images = processed_tensor_dict[fields.InputDataFields.image] - images_shape = tf.shape(images) - patched_images_shape = tf.shape(patched_images) - self.assertAllEqual(images_shape, patched_images_shape) - - def testRandomPatchGaussianClippedToLowerBound(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_patch_gaussian, { - 'min_patch_size': 20, - 'max_patch_size': 40, - 'min_gaussian_stddev': 50, - 'max_gaussian_stddev': 100 - })] - images = tf.zeros([1, 5, 4, 3]) - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - patched_images = processed_tensor_dict[fields.InputDataFields.image] - return patched_images - patched_images = self.execute_cpu(graph_fn, []) - self.assertAllGreaterEqual(patched_images, 0.0) - - def testRandomPatchGaussianClippedToUpperBound(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_patch_gaussian, { - 'min_patch_size': 20, - 'max_patch_size': 40, - 'min_gaussian_stddev': 50, - 'max_gaussian_stddev': 100 - })] - images = tf.constant(255.0, shape=[1, 5, 4, 3]) - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - patched_images = processed_tensor_dict[fields.InputDataFields.image] - return patched_images - patched_images = self.execute_cpu(graph_fn, []) - self.assertAllLessEqual(patched_images, 255.0) - - def testRandomPatchGaussianWithCache(self): - preprocessing_options = [(preprocessor.random_patch_gaussian, { - 'min_patch_size': 1, - 'max_patch_size': 200, - 'min_gaussian_stddev': 0.0, - 'max_gaussian_stddev': 2.0 - })] - self._testPreprocessorCache(preprocessing_options) - - def testRandomPatchGaussianWithRandomCoefOne(self): - def graph_fn(): - preprocessing_options = [(preprocessor.random_patch_gaussian, { - 'random_coef': 1.0 - })] - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - processed_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - patched_images = processed_tensor_dict[fields.InputDataFields.image] - images_shape = tf.shape(images) - patched_images_shape = tf.shape(patched_images) - return patched_images_shape, patched_images, images_shape, images - (patched_images_shape, patched_images, images_shape, - images) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_shape, patched_images_shape) - self.assertAllEqual(images, patched_images) - - @unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') - def testAutoAugmentImage(self): - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.autoaugment_image, { - 'policy_name': 'v1' - })) - images = self.createTestImages() - boxes = self.createTestBoxes() - tensor_dict = {fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes} - autoaugment_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options) - augmented_images = autoaugment_tensor_dict[fields.InputDataFields.image] - augmented_boxes = autoaugment_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - images_shape = tf.shape(images) - boxes_shape = tf.shape(boxes) - augmented_images_shape = tf.shape(augmented_images) - augmented_boxes_shape = tf.shape(augmented_boxes) - return [images_shape, boxes_shape, augmented_images_shape, - augmented_boxes_shape] - (images_shape_, boxes_shape_, augmented_images_shape_, - augmented_boxes_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(images_shape_, augmented_images_shape_) - self.assertAllEqual(boxes_shape_, augmented_boxes_shape_) - - def testRandomResizeMethodWithCache(self): - preprocess_options = [] - preprocess_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocess_options.append((preprocessor.random_resize_method, { - 'target_size': (75, 150) - })) - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=True, - test_keypoints=True) - - def testRandomResizeMethod(self): - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.random_resize_method, { - 'target_size': (75, 150) - })) - images = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images} - resized_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - resized_images = resized_tensor_dict[fields.InputDataFields.image] - resized_images_shape = tf.shape(resized_images) - expected_images_shape = tf.constant([1, 75, 150, 3], dtype=tf.int32) - return [expected_images_shape, resized_images_shape] - (expected_images_shape_, resized_images_shape_) = self.execute_cpu(graph_fn, - []) - self.assertAllEqual(expected_images_shape_, - resized_images_shape_) - - def testResizeImageWithMasks(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[15, 60, 40], [10, 15, 30]] - height = 50 - width = 100 - expected_image_shape_list = [[50, 100, 3], [50, 100, 3]] - expected_masks_shape_list = [[15, 50, 100], [10, 50, 100]] - def graph_fn(in_image_shape, in_masks_shape): - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_image( - in_image, in_masks, new_height=height, new_width=width) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return out_image_shape, out_masks_shape - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - (out_image_shape, - out_masks_shape) = self.execute_cpu(graph_fn, [ - np.array(in_image_shape, np.int32), - np.array(in_masks_shape, np.int32) - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeImageWithMasksTensorInputHeightAndWidth(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[15, 60, 40], [10, 15, 30]] - expected_image_shape_list = [[50, 100, 3], [50, 100, 3]] - expected_masks_shape_list = [[15, 50, 100], [10, 50, 100]] - def graph_fn(in_image_shape, in_masks_shape): - height = tf.constant(50, dtype=tf.int32) - width = tf.constant(100, dtype=tf.int32) - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_image( - in_image, in_masks, new_height=height, new_width=width) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return out_image_shape, out_masks_shape - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - (out_image_shape, - out_masks_shape) = self.execute_cpu(graph_fn, [ - np.array(in_image_shape, np.int32), - np.array(in_masks_shape, np.int32) - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeImageWithNoInstanceMask(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[0, 60, 40], [0, 15, 30]] - height = 50 - width = 100 - expected_image_shape_list = [[50, 100, 3], [50, 100, 3]] - expected_masks_shape_list = [[0, 50, 100], [0, 50, 100]] - def graph_fn(in_image_shape, in_masks_shape): - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_image( - in_image, in_masks, new_height=height, new_width=width) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return out_image_shape, out_masks_shape - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - (out_image_shape, - out_masks_shape) = self.execute_cpu(graph_fn, [ - np.array(in_image_shape, np.int32), - np.array(in_masks_shape, np.int32) - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToRangePreservesStaticSpatialShape(self): - """Tests image resizing, checking output sizes.""" - in_shape_list = [[60, 40, 3], [15, 30, 3], [15, 50, 3]] - min_dim = 50 - max_dim = 100 - expected_shape_list = [[75, 50, 3], [50, 100, 3], [30, 100, 3]] - - for in_shape, expected_shape in zip(in_shape_list, expected_shape_list): - in_image = tf.random_uniform(in_shape) - out_image, _ = preprocessor.resize_to_range( - in_image, min_dimension=min_dim, max_dimension=max_dim) - self.assertAllEqual(out_image.get_shape().as_list(), expected_shape) - - def testResizeToRangeWithDynamicSpatialShape(self): - """Tests image resizing, checking output sizes.""" - in_shape_list = [[60, 40, 3], [15, 30, 3], [15, 50, 3]] - min_dim = 50 - max_dim = 100 - expected_shape_list = [[75, 50, 3], [50, 100, 3], [30, 100, 3]] - def graph_fn(in_image_shape): - in_image = tf.random_uniform(in_image_shape) - out_image, _ = preprocessor.resize_to_range( - in_image, min_dimension=min_dim, max_dimension=max_dim) - out_image_shape = tf.shape(out_image) - return out_image_shape - for in_shape, expected_shape in zip(in_shape_list, expected_shape_list): - out_image_shape = self.execute_cpu(graph_fn, [np.array(in_shape, - np.int32)]) - self.assertAllEqual(out_image_shape, expected_shape) - - def testResizeToRangeWithPadToMaxDimensionReturnsCorrectShapes(self): - in_shape_list = [[60, 40, 3], [15, 30, 3], [15, 50, 3]] - min_dim = 50 - max_dim = 100 - expected_shape_list = [[100, 100, 3], [100, 100, 3], [100, 100, 3]] - def graph_fn(in_image): - out_image, _ = preprocessor.resize_to_range( - in_image, - min_dimension=min_dim, - max_dimension=max_dim, - pad_to_max_dimension=True) - return tf.shape(out_image) - for in_shape, expected_shape in zip(in_shape_list, expected_shape_list): - out_image_shape = self.execute_cpu( - graph_fn, [np.random.rand(*in_shape).astype('f')]) - self.assertAllEqual(out_image_shape, expected_shape) - - def testResizeToRangeWithPadToMaxDimensionReturnsCorrectTensor(self): - in_image_np = np.array([[[0, 1, 2]]], np.float32) - ex_image_np = np.array( - [[[0, 1, 2], [123.68, 116.779, 103.939]], - [[123.68, 116.779, 103.939], [123.68, 116.779, 103.939]]], np.float32) - min_dim = 1 - max_dim = 2 - def graph_fn(in_image): - out_image, _ = preprocessor.resize_to_range( - in_image, - min_dimension=min_dim, - max_dimension=max_dim, - pad_to_max_dimension=True, - per_channel_pad_value=(123.68, 116.779, 103.939)) - return out_image - out_image_np = self.execute_cpu(graph_fn, [in_image_np]) - self.assertAllClose(ex_image_np, out_image_np) - - def testResizeToRangeWithMasksPreservesStaticSpatialShape(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[15, 60, 40], [10, 15, 30]] - min_dim = 50 - max_dim = 100 - expected_image_shape_list = [[75, 50, 3], [50, 100, 3]] - expected_masks_shape_list = [[15, 75, 50], [10, 50, 100]] - - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_to_range( - in_image, in_masks, min_dimension=min_dim, max_dimension=max_dim) - self.assertAllEqual(out_masks.get_shape().as_list(), expected_mask_shape) - self.assertAllEqual(out_image.get_shape().as_list(), expected_image_shape) - - def testResizeToRangeWithMasksAndPadToMaxDimension(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[15, 60, 40], [10, 15, 30]] - min_dim = 50 - max_dim = 100 - expected_image_shape_list = [[100, 100, 3], [100, 100, 3]] - expected_masks_shape_list = [[15, 100, 100], [10, 100, 100]] - def graph_fn(in_image, in_masks): - out_image, out_masks, _ = preprocessor.resize_to_range( - in_image, in_masks, min_dimension=min_dim, - max_dimension=max_dim, pad_to_max_dimension=True) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return [out_image_shape, out_masks_shape] - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - out_image_shape, out_masks_shape = self.execute_cpu( - graph_fn, [ - np.random.rand(*in_image_shape).astype('f'), - np.random.rand(*in_masks_shape).astype('f'), - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToRangeWithMasksAndDynamicSpatialShape(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[15, 60, 40], [10, 15, 30]] - min_dim = 50 - max_dim = 100 - expected_image_shape_list = [[75, 50, 3], [50, 100, 3]] - expected_masks_shape_list = [[15, 75, 50], [10, 50, 100]] - def graph_fn(in_image, in_masks): - out_image, out_masks, _ = preprocessor.resize_to_range( - in_image, in_masks, min_dimension=min_dim, max_dimension=max_dim) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return [out_image_shape, out_masks_shape] - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - out_image_shape, out_masks_shape = self.execute_cpu( - graph_fn, [ - np.random.rand(*in_image_shape).astype('f'), - np.random.rand(*in_masks_shape).astype('f'), - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToRangeWithInstanceMasksTensorOfSizeZero(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[0, 60, 40], [0, 15, 30]] - min_dim = 50 - max_dim = 100 - expected_image_shape_list = [[75, 50, 3], [50, 100, 3]] - expected_masks_shape_list = [[0, 75, 50], [0, 50, 100]] - def graph_fn(in_image, in_masks): - out_image, out_masks, _ = preprocessor.resize_to_range( - in_image, in_masks, min_dimension=min_dim, max_dimension=max_dim) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return [out_image_shape, out_masks_shape] - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - out_image_shape, out_masks_shape = self.execute_cpu( - graph_fn, [ - np.random.rand(*in_image_shape).astype('f'), - np.random.rand(*in_masks_shape).astype('f'), - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToRange4DImageTensor(self): - image = tf.random_uniform([1, 200, 300, 3]) - with self.assertRaises(ValueError): - preprocessor.resize_to_range(image, 500, 600) - - def testResizeToRangeSameMinMax(self): - """Tests image resizing, checking output sizes.""" - in_shape_list = [[312, 312, 3], [299, 299, 3]] - min_dim = 320 - max_dim = 320 - expected_shape_list = [[320, 320, 3], [320, 320, 3]] - def graph_fn(in_shape): - in_image = tf.random_uniform(in_shape) - out_image, _ = preprocessor.resize_to_range( - in_image, min_dimension=min_dim, max_dimension=max_dim) - out_image_shape = tf.shape(out_image) - return out_image_shape - for in_shape, expected_shape in zip(in_shape_list, expected_shape_list): - out_image_shape = self.execute_cpu(graph_fn, [np.array(in_shape, - np.int32)]) - self.assertAllEqual(out_image_shape, expected_shape) - - def testResizeToMaxDimensionTensorShapes(self): - """Tests both cases where image should and shouldn't be resized.""" - in_image_shape_list = [[100, 50, 3], [15, 30, 3]] - in_masks_shape_list = [[15, 100, 50], [10, 15, 30]] - max_dim = 50 - expected_image_shape_list = [[50, 25, 3], [15, 30, 3]] - expected_masks_shape_list = [[15, 50, 25], [10, 15, 30]] - def graph_fn(in_image_shape, in_masks_shape): - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_to_max_dimension( - in_image, in_masks, max_dimension=max_dim) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return [out_image_shape, out_masks_shape] - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - out_image_shape, out_masks_shape = self.execute_cpu( - graph_fn, [ - np.array(in_image_shape, np.int32), - np.array(in_masks_shape, np.int32) - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToMaxDimensionWithInstanceMasksTensorOfSizeZero(self): - """Tests both cases where image should and shouldn't be resized.""" - in_image_shape_list = [[100, 50, 3], [15, 30, 3]] - in_masks_shape_list = [[0, 100, 50], [0, 15, 30]] - max_dim = 50 - expected_image_shape_list = [[50, 25, 3], [15, 30, 3]] - expected_masks_shape_list = [[0, 50, 25], [0, 15, 30]] - - def graph_fn(in_image_shape, in_masks_shape): - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_to_max_dimension( - in_image, in_masks, max_dimension=max_dim) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return [out_image_shape, out_masks_shape] - - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - out_image_shape, out_masks_shape = self.execute_cpu( - graph_fn, [ - np.array(in_image_shape, np.int32), - np.array(in_masks_shape, np.int32) - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToMaxDimensionRaisesErrorOn4DImage(self): - image = tf.random_uniform([1, 200, 300, 3]) - with self.assertRaises(ValueError): - preprocessor.resize_to_max_dimension(image, 500) - - def testResizeToMinDimensionTensorShapes(self): - in_image_shape_list = [[60, 55, 3], [15, 30, 3]] - in_masks_shape_list = [[15, 60, 55], [10, 15, 30]] - min_dim = 50 - expected_image_shape_list = [[60, 55, 3], [50, 100, 3]] - expected_masks_shape_list = [[15, 60, 55], [10, 50, 100]] - def graph_fn(in_image_shape, in_masks_shape): - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_to_min_dimension( - in_image, in_masks, min_dimension=min_dim) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return [out_image_shape, out_masks_shape] - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - out_image_shape, out_masks_shape = self.execute_cpu( - graph_fn, [ - np.array(in_image_shape, np.int32), - np.array(in_masks_shape, np.int32) - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToMinDimensionWithInstanceMasksTensorOfSizeZero(self): - """Tests image resizing, checking output sizes.""" - in_image_shape_list = [[60, 40, 3], [15, 30, 3]] - in_masks_shape_list = [[0, 60, 40], [0, 15, 30]] - min_dim = 50 - expected_image_shape_list = [[75, 50, 3], [50, 100, 3]] - expected_masks_shape_list = [[0, 75, 50], [0, 50, 100]] - def graph_fn(in_image_shape, in_masks_shape): - in_image = tf.random_uniform(in_image_shape) - in_masks = tf.random_uniform(in_masks_shape) - out_image, out_masks, _ = preprocessor.resize_to_min_dimension( - in_image, in_masks, min_dimension=min_dim) - out_image_shape = tf.shape(out_image) - out_masks_shape = tf.shape(out_masks) - return [out_image_shape, out_masks_shape] - for (in_image_shape, expected_image_shape, in_masks_shape, - expected_mask_shape) in zip(in_image_shape_list, - expected_image_shape_list, - in_masks_shape_list, - expected_masks_shape_list): - out_image_shape, out_masks_shape = self.execute_cpu( - graph_fn, [ - np.array(in_image_shape, np.int32), - np.array(in_masks_shape, np.int32) - ]) - self.assertAllEqual(out_image_shape, expected_image_shape) - self.assertAllEqual(out_masks_shape, expected_mask_shape) - - def testResizeToMinDimensionRaisesErrorOn4DImage(self): - image = tf.random_uniform([1, 200, 300, 3]) - with self.assertRaises(ValueError): - preprocessor.resize_to_min_dimension(image, 500) - - def testResizePadToMultipleNoMasks(self): - """Tests resizing when padding to multiple without masks.""" - def graph_fn(): - image = tf.ones((200, 100, 3), dtype=tf.float32) - out_image, out_shape = preprocessor.resize_pad_to_multiple( - image, multiple=32) - return out_image, out_shape - - out_image, out_shape = self.execute_cpu(graph_fn, []) - self.assertAllClose(out_image.sum(), 200 * 100 * 3) - self.assertAllEqual(out_shape, (200, 100, 3)) - self.assertAllEqual(out_image.shape, (224, 128, 3)) - - def testResizePadToMultipleWithMasks(self): - """Tests resizing when padding to multiple with masks.""" - def graph_fn(): - image = tf.ones((200, 100, 3), dtype=tf.float32) - masks = tf.ones((10, 200, 100), dtype=tf.float32) - - _, out_masks, out_shape = preprocessor.resize_pad_to_multiple( - image, multiple=32, masks=masks) - return [out_masks, out_shape] - - out_masks, out_shape = self.execute_cpu(graph_fn, []) - self.assertAllClose(out_masks.sum(), 200 * 100 * 10) - self.assertAllEqual(out_shape, (200, 100, 3)) - self.assertAllEqual(out_masks.shape, (10, 224, 128)) - - def testResizePadToMultipleEmptyMasks(self): - """Tests resizing when padding to multiple with an empty mask.""" - def graph_fn(): - image = tf.ones((200, 100, 3), dtype=tf.float32) - masks = tf.ones((0, 200, 100), dtype=tf.float32) - _, out_masks, out_shape = preprocessor.resize_pad_to_multiple( - image, multiple=32, masks=masks) - return [out_masks, out_shape] - out_masks, out_shape = self.execute_cpu(graph_fn, []) - self.assertAllEqual(out_shape, (200, 100, 3)) - self.assertAllEqual(out_masks.shape, (0, 224, 128)) - - def testScaleBoxesToPixelCoordinates(self): - """Tests box scaling, checking scaled values.""" - def graph_fn(): - in_shape = [60, 40, 3] - in_boxes = [[0.1, 0.2, 0.4, 0.6], - [0.5, 0.3, 0.9, 0.7]] - in_image = tf.random_uniform(in_shape) - in_boxes = tf.constant(in_boxes) - _, out_boxes = preprocessor.scale_boxes_to_pixel_coordinates( - in_image, boxes=in_boxes) - return out_boxes - expected_boxes = [[6., 8., 24., 24.], - [30., 12., 54., 28.]] - out_boxes = self.execute_cpu(graph_fn, []) - self.assertAllClose(out_boxes, expected_boxes) - - def testScaleBoxesToPixelCoordinatesWithKeypoints(self): - """Tests box and keypoint scaling, checking scaled values.""" - def graph_fn(): - in_shape = [60, 40, 3] - in_boxes = self.createTestBoxes() - in_keypoints, _ = self.createTestKeypoints() - in_image = tf.random_uniform(in_shape) - (_, out_boxes, - out_keypoints) = preprocessor.scale_boxes_to_pixel_coordinates( - in_image, boxes=in_boxes, keypoints=in_keypoints) - return out_boxes, out_keypoints - expected_boxes = [[0., 10., 45., 40.], - [15., 20., 45., 40.]] - expected_keypoints = [ - [[6., 4.], [12., 8.], [18., 12.]], - [[24., 16.], [30., 20.], [36., 24.]], - ] - out_boxes_, out_keypoints_ = self.execute_cpu(graph_fn, []) - self.assertAllClose(out_boxes_, expected_boxes) - self.assertAllClose(out_keypoints_, expected_keypoints) - - def testSubtractChannelMean(self): - """Tests whether channel means have been subtracted.""" - def graph_fn(): - image = tf.zeros((240, 320, 3)) - means = [1, 2, 3] - actual = preprocessor.subtract_channel_mean(image, means=means) - return actual - actual = self.execute_cpu(graph_fn, []) - self.assertTrue((actual[:, :, 0], -1)) - self.assertTrue((actual[:, :, 1], -2)) - self.assertTrue((actual[:, :, 2], -3)) - - def testOneHotEncoding(self): - """Tests one hot encoding of multiclass labels.""" - def graph_fn(): - labels = tf.constant([1, 4, 2], dtype=tf.int32) - one_hot = preprocessor.one_hot_encoding(labels, num_classes=5) - return one_hot - one_hot = self.execute_cpu(graph_fn, []) - self.assertAllEqual([0, 1, 1, 0, 1], one_hot) - - def testRandomSelfConcatImageVertically(self): - - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - confidences = weights - scores = self.createTestMultiClassScores() - - tensor_dict = { - fields.InputDataFields.image: tf.cast(images, dtype=tf.float32), - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_confidences: confidences, - fields.InputDataFields.multiclass_scores: scores, - } - - preprocessing_options = [(preprocessor.random_self_concat_image, { - 'concat_vertical_probability': 1.0, - 'concat_horizontal_probability': 0.0, - })] - func_arg_map = preprocessor.get_default_func_arg_map( - True, True, True) - output_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=func_arg_map) - - original_shape = tf.shape(images)[1:3] - final_shape = tf.shape(output_tensor_dict[fields.InputDataFields.image])[ - 1:3] - return [ - original_shape, - boxes, - labels, - confidences, - scores, - final_shape, - output_tensor_dict[fields.InputDataFields.groundtruth_boxes], - output_tensor_dict[fields.InputDataFields.groundtruth_classes], - output_tensor_dict[fields.InputDataFields.groundtruth_confidences], - output_tensor_dict[fields.InputDataFields.multiclass_scores], - ] - (original_shape, boxes, labels, confidences, scores, final_shape, new_boxes, - new_labels, new_confidences, new_scores) = self.execute(graph_fn, []) - self.assertAllEqual(final_shape, original_shape * np.array([2, 1])) - self.assertAllEqual(2 * boxes.size, new_boxes.size) - self.assertAllEqual(2 * labels.size, new_labels.size) - self.assertAllEqual(2 * confidences.size, new_confidences.size) - self.assertAllEqual(2 * scores.size, new_scores.size) - - def testRandomSelfConcatImageHorizontally(self): - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - confidences = weights - scores = self.createTestMultiClassScores() - - tensor_dict = { - fields.InputDataFields.image: tf.cast(images, dtype=tf.float32), - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - fields.InputDataFields.groundtruth_confidences: confidences, - fields.InputDataFields.multiclass_scores: scores, - } - - preprocessing_options = [(preprocessor.random_self_concat_image, { - 'concat_vertical_probability': 0.0, - 'concat_horizontal_probability': 1.0, - })] - func_arg_map = preprocessor.get_default_func_arg_map( - True, True, True) - output_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=func_arg_map) - - original_shape = tf.shape(images)[1:3] - final_shape = tf.shape(output_tensor_dict[fields.InputDataFields.image])[ - 1:3] - return [ - original_shape, - boxes, - labels, - confidences, - scores, - final_shape, - output_tensor_dict[fields.InputDataFields.groundtruth_boxes], - output_tensor_dict[fields.InputDataFields.groundtruth_classes], - output_tensor_dict[fields.InputDataFields.groundtruth_confidences], - output_tensor_dict[fields.InputDataFields.multiclass_scores], - ] - (original_shape, boxes, labels, confidences, scores, final_shape, new_boxes, - new_labels, new_confidences, new_scores) = self.execute(graph_fn, []) - self.assertAllEqual(final_shape, original_shape * np.array([1, 2])) - self.assertAllEqual(2 * boxes.size, new_boxes.size) - self.assertAllEqual(2 * labels.size, new_labels.size) - self.assertAllEqual(2 * confidences.size, new_confidences.size) - self.assertAllEqual(2 * scores.size, new_scores.size) - - def testSSDRandomCropWithCache(self): - preprocess_options = [ - (preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - }), - (preprocessor.ssd_random_crop, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=False, - test_keypoints=False) - - def testSSDRandomCrop(self): - def graph_fn(): - preprocessing_options = [ - (preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - }), - (preprocessor.ssd_random_crop, {})] - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - return [boxes_rank, distorted_boxes_rank, images_rank, - distorted_images_rank] - (boxes_rank_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - - def testSSDRandomCropWithMultiClassScores(self): - def graph_fn(): - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - }), (preprocessor.ssd_random_crop, {})] - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - multiclass_scores = self.createTestMultiClassScores() - - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.multiclass_scores: multiclass_scores, - fields.InputDataFields.groundtruth_weights: weights, - } - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_multiclass_scores=True) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - distorted_multiclass_scores = distorted_tensor_dict[ - fields.InputDataFields.multiclass_scores] - - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - multiclass_scores_rank = tf.rank(multiclass_scores) - distorted_multiclass_scores_rank = tf.rank(distorted_multiclass_scores) - return [ - boxes_rank, distorted_boxes, distorted_boxes_rank, images_rank, - distorted_images_rank, multiclass_scores_rank, - distorted_multiclass_scores, distorted_multiclass_scores_rank - ] - - (boxes_rank_, distorted_boxes_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_, multiclass_scores_rank_, - distorted_multiclass_scores_, - distorted_multiclass_scores_rank_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - self.assertAllEqual(multiclass_scores_rank_, - distorted_multiclass_scores_rank_) - self.assertAllEqual(distorted_boxes_.shape[0], - distorted_multiclass_scores_.shape[0]) - - def testSSDRandomCropPad(self): - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - preprocessing_options = [ - (preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - }), - (preprocessor.ssd_random_crop_pad, {})] - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights, - } - distorted_tensor_dict = preprocessor.preprocess(tensor_dict, - preprocessing_options) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - return [ - boxes_rank, distorted_boxes_rank, images_rank, distorted_images_rank - ] - (boxes_rank_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - - def testSSDRandomCropFixedAspectRatioWithCache(self): - preprocess_options = [ - (preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - }), - (preprocessor.ssd_random_crop_fixed_aspect_ratio, {})] - self._testPreprocessorCache(preprocess_options, - test_boxes=True, - test_masks=False, - test_keypoints=False) - - def _testSSDRandomCropFixedAspectRatio(self, - include_multiclass_scores, - include_instance_masks, - include_keypoints): - def graph_fn(): - images = self.createTestImages() - boxes = self.createTestBoxes() - labels = self.createTestLabels() - weights = self.createTestGroundtruthWeights() - preprocessing_options = [(preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - }), (preprocessor.ssd_random_crop_fixed_aspect_ratio, {})] - tensor_dict = { - fields.InputDataFields.image: images, - fields.InputDataFields.groundtruth_boxes: boxes, - fields.InputDataFields.groundtruth_classes: labels, - fields.InputDataFields.groundtruth_weights: weights - } - if include_multiclass_scores: - multiclass_scores = self.createTestMultiClassScores() - tensor_dict[fields.InputDataFields.multiclass_scores] = ( - multiclass_scores) - if include_instance_masks: - masks = self.createTestMasks() - tensor_dict[fields.InputDataFields.groundtruth_instance_masks] = masks - if include_keypoints: - keypoints, _ = self.createTestKeypoints() - tensor_dict[fields.InputDataFields.groundtruth_keypoints] = keypoints - - preprocessor_arg_map = preprocessor.get_default_func_arg_map( - include_multiclass_scores=include_multiclass_scores, - include_instance_masks=include_instance_masks, - include_keypoints=include_keypoints) - distorted_tensor_dict = preprocessor.preprocess( - tensor_dict, preprocessing_options, func_arg_map=preprocessor_arg_map) - distorted_images = distorted_tensor_dict[fields.InputDataFields.image] - distorted_boxes = distorted_tensor_dict[ - fields.InputDataFields.groundtruth_boxes] - images_rank = tf.rank(images) - distorted_images_rank = tf.rank(distorted_images) - boxes_rank = tf.rank(boxes) - distorted_boxes_rank = tf.rank(distorted_boxes) - return [boxes_rank, distorted_boxes_rank, images_rank, - distorted_images_rank] - - (boxes_rank_, distorted_boxes_rank_, images_rank_, - distorted_images_rank_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(boxes_rank_, distorted_boxes_rank_) - self.assertAllEqual(images_rank_, distorted_images_rank_) - - def testSSDRandomCropFixedAspectRatio(self): - self._testSSDRandomCropFixedAspectRatio(include_multiclass_scores=False, - include_instance_masks=False, - include_keypoints=False) - - def testSSDRandomCropFixedAspectRatioWithMultiClassScores(self): - self._testSSDRandomCropFixedAspectRatio(include_multiclass_scores=True, - include_instance_masks=False, - include_keypoints=False) - - def testSSDRandomCropFixedAspectRatioWithMasksAndKeypoints(self): - self._testSSDRandomCropFixedAspectRatio(include_multiclass_scores=False, - include_instance_masks=True, - include_keypoints=True) - - def testSSDRandomCropFixedAspectRatioWithLabelScoresMasksAndKeypoints(self): - self._testSSDRandomCropFixedAspectRatio(include_multiclass_scores=False, - include_instance_masks=True, - include_keypoints=True) - - def testConvertClassLogitsToSoftmax(self): - def graph_fn(): - multiclass_scores = tf.constant( - [[1.0, 0.0], [0.5, 0.5], [1000, 1]], dtype=tf.float32) - temperature = 2.0 - - converted_multiclass_scores = ( - preprocessor.convert_class_logits_to_softmax( - multiclass_scores=multiclass_scores, temperature=temperature)) - return converted_multiclass_scores - converted_multiclass_scores_ = self.execute_cpu(graph_fn, []) - expected_converted_multiclass_scores = [[0.62245935, 0.37754068], - [0.5, 0.5], - [1, 0]] - self.assertAllClose(converted_multiclass_scores_, - expected_converted_multiclass_scores) - - @parameterized.named_parameters( - ('scale_1', 1.0), - ('scale_1.5', 1.5), - ('scale_0.5', 0.5) - ) - def test_square_crop_by_scale(self, scale): - def graph_fn(): - image = np.random.randn(256, 256, 1) - - masks = tf.constant(image[:, :, 0].reshape(1, 256, 256)) - image = tf.constant(image) - keypoints = tf.constant([[[0.25, 0.25], [0.75, 0.75]]]) - - boxes = tf.constant([[0.25, .25, .75, .75]]) - labels = tf.constant([[1]]) - label_confidences = tf.constant([0.75]) - label_weights = tf.constant([[1.]]) - - (new_image, new_boxes, _, _, new_confidences, new_masks, - new_keypoints) = preprocessor.random_square_crop_by_scale( - image, - boxes, - labels, - label_weights, - label_confidences, - masks=masks, - keypoints=keypoints, - max_border=256, - scale_min=scale, - scale_max=scale) - return new_image, new_boxes, new_confidences, new_masks, new_keypoints - image, boxes, confidences, masks, keypoints = self.execute_cpu(graph_fn, []) - ymin, xmin, ymax, xmax = boxes[0] - self.assertAlmostEqual(ymax - ymin, 0.5 / scale) - self.assertAlmostEqual(xmax - xmin, 0.5 / scale) - - k1 = keypoints[0, 0] - k2 = keypoints[0, 1] - self.assertAlmostEqual(k2[0] - k1[0], 0.5 / scale) - self.assertAlmostEqual(k2[1] - k1[1], 0.5 / scale) - - size = max(image.shape) - self.assertAlmostEqual(scale * 256.0, size) - - self.assertAllClose(image[:, :, 0], masks[0, :, :]) - self.assertAllClose(confidences, [0.75]) - - @parameterized.named_parameters(('scale_0_1', 0.1), ('scale_1_0', 1.0), - ('scale_2_0', 2.0)) - def test_random_scale_crop_and_pad_to_square(self, scale): - - def graph_fn(): - image = np.random.randn(512, 256, 1) - box_centers = [0.25, 0.5, 0.75] - box_size = 0.1 - box_corners = [] - box_labels = [] - box_label_weights = [] - keypoints = [] - masks = [] - for center_y in box_centers: - for center_x in box_centers: - box_corners.append( - [center_y - box_size / 2.0, center_x - box_size / 2.0, - center_y + box_size / 2.0, center_x + box_size / 2.0]) - box_labels.append([1]) - box_label_weights.append([1.]) - keypoints.append( - [[center_y - box_size / 2.0, center_x - box_size / 2.0], - [center_y + box_size / 2.0, center_x + box_size / 2.0]]) - masks.append(image[:, :, 0].reshape(512, 256)) - - image = tf.constant(image) - boxes = tf.constant(box_corners) - labels = tf.constant(box_labels) - label_weights = tf.constant(box_label_weights) - keypoints = tf.constant(keypoints) - masks = tf.constant(np.stack(masks)) - - (new_image, new_boxes, _, _, new_masks, - new_keypoints) = preprocessor.random_scale_crop_and_pad_to_square( - image, - boxes, - labels, - label_weights, - masks=masks, - keypoints=keypoints, - scale_min=scale, - scale_max=scale, - output_size=512) - return new_image, new_boxes, new_masks, new_keypoints - - image, boxes, masks, keypoints = self.execute_cpu(graph_fn, []) - - # Since random_scale_crop_and_pad_to_square may prune and clip boxes, - # we only need to find one of the boxes that was not clipped and check - # that it matches the expected dimensions. Note, assertAlmostEqual(a, b) - # is equivalent to round(a-b, 7) == 0. - any_box_has_correct_size = False - effective_scale_y = int(scale * 512) / 512.0 - effective_scale_x = int(scale * 256) / 512.0 - expected_size_y = 0.1 * effective_scale_y - expected_size_x = 0.1 * effective_scale_x - for box in boxes: - ymin, xmin, ymax, xmax = box - any_box_has_correct_size |= ( - (round(ymin, 7) != 0.0) and (round(xmin, 7) != 0.0) and - (round(ymax, 7) != 1.0) and (round(xmax, 7) != 1.0) and - (round((ymax - ymin) - expected_size_y, 7) == 0.0) and - (round((xmax - xmin) - expected_size_x, 7) == 0.0)) - self.assertTrue(any_box_has_correct_size) - - # Similar to the approach above where we check for at least one box with the - # expected dimensions, we check for at least one pair of keypoints whose - # distance matches the expected dimensions. - any_keypoint_pair_has_correct_dist = False - for keypoint_pair in keypoints: - ymin, xmin = keypoint_pair[0] - ymax, xmax = keypoint_pair[1] - any_keypoint_pair_has_correct_dist |= ( - (round(ymin, 7) != 0.0) and (round(xmin, 7) != 0.0) and - (round(ymax, 7) != 1.0) and (round(xmax, 7) != 1.0) and - (round((ymax - ymin) - expected_size_y, 7) == 0.0) and - (round((xmax - xmin) - expected_size_x, 7) == 0.0)) - self.assertTrue(any_keypoint_pair_has_correct_dist) - - self.assertAlmostEqual(512.0, image.shape[0]) - self.assertAlmostEqual(512.0, image.shape[1]) - - self.assertAllClose(image[:, :, 0], - masks[0, :, :]) - - def test_random_scale_crop_and_pad_to_square_handles_confidences(self): - - def graph_fn(): - image = tf.zeros([10, 10, 1]) - boxes = tf.constant([[0, 0, 0.5, 0.5], [0.5, 0.5, 0.75, 0.75]]) - label_weights = tf.constant([1.0, 1.0]) - box_labels = tf.constant([0, 1]) - box_confidences = tf.constant([-1.0, 1.0]) - - (_, new_boxes, _, _, - new_confidences) = preprocessor.random_scale_crop_and_pad_to_square( - image, - boxes, - box_labels, - label_weights, - label_confidences=box_confidences, - scale_min=0.8, - scale_max=0.9, - output_size=10) - return new_boxes, new_confidences - - boxes, confidences = self.execute_cpu(graph_fn, []) - - self.assertLen(boxes, 2) - self.assertAllEqual(confidences, [-1.0, 1.0]) - - def testAdjustGamma(self): - - def graph_fn(): - preprocessing_options = [] - preprocessing_options.append((preprocessor.normalize_image, { - 'original_minval': 0, - 'original_maxval': 255, - 'target_minval': 0, - 'target_maxval': 1 - })) - preprocessing_options.append((preprocessor.adjust_gamma, {})) - images_original = self.createTestImages() - tensor_dict = {fields.InputDataFields.image: images_original} - tensor_dict = preprocessor.preprocess(tensor_dict, preprocessing_options) - images_gamma = tensor_dict[fields.InputDataFields.image] - image_original_shape = tf.shape(images_original) - image_gamma_shape = tf.shape(images_gamma) - return [image_original_shape, image_gamma_shape] - - (image_original_shape_, image_gamma_shape_) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image_original_shape_, image_gamma_shape_) - - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/region_similarity_calculator.py b/research/object_detection/core/region_similarity_calculator.py deleted file mode 100644 index 1e36b59c938..00000000000 --- a/research/object_detection/core/region_similarity_calculator.py +++ /dev/null @@ -1,192 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Region Similarity Calculators for BoxLists. - -Region Similarity Calculators compare a pairwise measure of similarity -between the boxes in two BoxLists. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from abc import ABCMeta -from abc import abstractmethod - -import six -import tensorflow.compat.v1 as tf - -from object_detection.core import box_list_ops -from object_detection.core import standard_fields as fields - - -class RegionSimilarityCalculator(six.with_metaclass(ABCMeta, object)): - """Abstract base class for region similarity calculator.""" - - def compare(self, boxlist1, boxlist2, scope=None): - """Computes matrix of pairwise similarity between BoxLists. - - This op (to be overridden) computes a measure of pairwise similarity between - the boxes in the given BoxLists. Higher values indicate more similarity. - - Note that this method simply measures similarity and does not explicitly - perform a matching. - - Args: - boxlist1: BoxList holding N boxes. - boxlist2: BoxList holding M boxes. - scope: Op scope name. Defaults to 'Compare' if None. - - Returns: - a (float32) tensor of shape [N, M] with pairwise similarity score. - """ - with tf.name_scope(scope, 'Compare', [boxlist1, boxlist2]) as scope: - return self._compare(boxlist1, boxlist2) - - @abstractmethod - def _compare(self, boxlist1, boxlist2): - pass - - -class IouSimilarity(RegionSimilarityCalculator): - """Class to compute similarity based on Intersection over Union (IOU) metric. - - This class computes pairwise similarity between two BoxLists based on IOU. - """ - - def _compare(self, boxlist1, boxlist2): - """Compute pairwise IOU similarity between the two BoxLists. - - Args: - boxlist1: BoxList holding N boxes. - boxlist2: BoxList holding M boxes. - - Returns: - A tensor with shape [N, M] representing pairwise iou scores. - """ - return box_list_ops.iou(boxlist1, boxlist2) - - -class DETRSimilarity(RegionSimilarityCalculator): - """Class to compute similarity for the Detection Transformer model. - - This class computes pairwise DETR similarity between two BoxLists using a - weighted combination of GIOU, classification scores, and the L1 loss. - """ - - def __init__(self, l1_weight=5, giou_weight=2): - super().__init__() - self.l1_weight = l1_weight - self.giou_weight = giou_weight - - def _compare(self, boxlist1, boxlist2): - """Compute pairwise DETR similarity between the two BoxLists. - - Args: - boxlist1: BoxList holding N groundtruth boxes. - boxlist2: BoxList holding M predicted boxes. - - Returns: - A tensor with shape [N, M] representing pairwise DETR similarity scores. - """ - groundtruth_labels = boxlist1.get_field(fields.BoxListFields.classes) - predicted_labels = boxlist2.get_field(fields.BoxListFields.classes) - classification_scores = tf.matmul(groundtruth_labels, - predicted_labels, - transpose_b=True) - loss = self.l1_weight * box_list_ops.l1( - boxlist1, boxlist2) + self.giou_weight * (1 - box_list_ops.giou( - boxlist1, boxlist2)) - classification_scores - return -loss - - -class NegSqDistSimilarity(RegionSimilarityCalculator): - """Class to compute similarity based on the squared distance metric. - - This class computes pairwise similarity between two BoxLists based on the - negative squared distance metric. - """ - - def _compare(self, boxlist1, boxlist2): - """Compute matrix of (negated) sq distances. - - Args: - boxlist1: BoxList holding N boxes. - boxlist2: BoxList holding M boxes. - - Returns: - A tensor with shape [N, M] representing negated pairwise squared distance. - """ - return -1 * box_list_ops.sq_dist(boxlist1, boxlist2) - - -class IoaSimilarity(RegionSimilarityCalculator): - """Class to compute similarity based on Intersection over Area (IOA) metric. - - This class computes pairwise similarity between two BoxLists based on their - pairwise intersections divided by the areas of second BoxLists. - """ - - def _compare(self, boxlist1, boxlist2): - """Compute pairwise IOA similarity between the two BoxLists. - - Args: - boxlist1: BoxList holding N boxes. - boxlist2: BoxList holding M boxes. - - Returns: - A tensor with shape [N, M] representing pairwise IOA scores. - """ - return box_list_ops.ioa(boxlist1, boxlist2) - - -class ThresholdedIouSimilarity(RegionSimilarityCalculator): - """Class to compute similarity based on thresholded IOU and score. - - This class computes pairwise similarity between two BoxLists based on IOU and - a 'score' present in boxlist1. If IOU > threshold, then the entry in the - output pairwise tensor will contain `score`, otherwise 0. - """ - - def __init__(self, iou_threshold=0): - """Initialize the ThresholdedIouSimilarity. - - Args: - iou_threshold: For a given pair of boxes, if the IOU is > iou_threshold, - then the comparison result will be the foreground probability of - the first box, otherwise it will be zero. - """ - super(ThresholdedIouSimilarity, self).__init__() - self._iou_threshold = iou_threshold - - def _compare(self, boxlist1, boxlist2): - """Compute pairwise IOU similarity between the two BoxLists and score. - - Args: - boxlist1: BoxList holding N boxes. Must have a score field. - boxlist2: BoxList holding M boxes. - - Returns: - A tensor with shape [N, M] representing scores threholded by pairwise - iou scores. - """ - ious = box_list_ops.iou(boxlist1, boxlist2) - scores = boxlist1.get_field(fields.BoxListFields.scores) - scores = tf.expand_dims(scores, axis=1) - row_replicated_scores = tf.tile(scores, [1, tf.shape(ious)[-1]]) - thresholded_ious = tf.where(ious > self._iou_threshold, - row_replicated_scores, tf.zeros_like(ious)) - - return thresholded_ious diff --git a/research/object_detection/core/region_similarity_calculator_test.py b/research/object_detection/core/region_similarity_calculator_test.py deleted file mode 100644 index ec1de45be14..00000000000 --- a/research/object_detection/core/region_similarity_calculator_test.py +++ /dev/null @@ -1,117 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for region_similarity_calculator.""" -import tensorflow.compat.v1 as tf - -from object_detection.core import box_list -from object_detection.core import region_similarity_calculator -from object_detection.core import standard_fields as fields -from object_detection.utils import test_case - - -class RegionSimilarityCalculatorTest(test_case.TestCase): - - def test_get_correct_pairwise_similarity_based_on_iou(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - iou_similarity_calculator = region_similarity_calculator.IouSimilarity() - iou_similarity = iou_similarity_calculator.compare(boxes1, boxes2) - return iou_similarity - exp_output = [[2.0 / 16.0, 0, 6.0 / 400.0], [1.0 / 16.0, 0.0, 5.0 / 400.0]] - iou_output = self.execute(graph_fn, []) - self.assertAllClose(iou_output, exp_output) - - def test_get_correct_pairwise_similarity_based_on_squared_distances(self): - def graph_fn(): - corners1 = tf.constant([[0.0, 0.0, 0.0, 0.0], - [1.0, 1.0, 0.0, 2.0]]) - corners2 = tf.constant([[3.0, 4.0, 1.0, 0.0], - [-4.0, 0.0, 0.0, 3.0], - [0.0, 0.0, 0.0, 0.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - dist_similarity_calc = region_similarity_calculator.NegSqDistSimilarity() - dist_similarity = dist_similarity_calc.compare(boxes1, boxes2) - return dist_similarity - exp_output = [[-26, -25, 0], [-18, -27, -6]] - iou_output = self.execute(graph_fn, []) - self.assertAllClose(iou_output, exp_output) - - def test_get_correct_pairwise_similarity_based_on_ioa(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - ioa_similarity_calculator = region_similarity_calculator.IoaSimilarity() - ioa_similarity_1 = ioa_similarity_calculator.compare(boxes1, boxes2) - ioa_similarity_2 = ioa_similarity_calculator.compare(boxes2, boxes1) - return ioa_similarity_1, ioa_similarity_2 - exp_output_1 = [[2.0 / 12.0, 0, 6.0 / 400.0], - [1.0 / 12.0, 0.0, 5.0 / 400.0]] - exp_output_2 = [[2.0 / 6.0, 1.0 / 5.0], - [0, 0], - [6.0 / 6.0, 5.0 / 5.0]] - iou_output_1, iou_output_2 = self.execute(graph_fn, []) - self.assertAllClose(iou_output_1, exp_output_1) - self.assertAllClose(iou_output_2, exp_output_2) - - def test_get_correct_pairwise_similarity_based_on_thresholded_iou(self): - def graph_fn(): - corners1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - corners2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0], - [0.0, 0.0, 20.0, 20.0]]) - scores = tf.constant([.3, .6]) - iou_threshold = .013 - boxes1 = box_list.BoxList(corners1) - boxes1.add_field(fields.BoxListFields.scores, scores) - boxes2 = box_list.BoxList(corners2) - iou_similarity_calculator = ( - region_similarity_calculator.ThresholdedIouSimilarity( - iou_threshold=iou_threshold)) - iou_similarity = iou_similarity_calculator.compare(boxes1, boxes2) - return iou_similarity - exp_output = tf.constant([[0.3, 0., 0.3], [0.6, 0., 0.]]) - iou_output = self.execute(graph_fn, []) - self.assertAllClose(iou_output, exp_output) - - def test_detr_similarity(self): - def graph_fn(): - corners1 = tf.constant([[5.0, 7.0, 7.0, 9.0]]) - corners2 = tf.constant([[5.0, 7.0, 7.0, 9.0], [5.0, 11.0, 7.0, 13.0]]) - groundtruth_labels = tf.constant([[1.0, 0.0]]) - predicted_labels = tf.constant([[0.0, 1000.0], [1000.0, 0.0]]) - boxes1 = box_list.BoxList(corners1) - boxes2 = box_list.BoxList(corners2) - boxes1.add_field(fields.BoxListFields.classes, groundtruth_labels) - boxes2.add_field(fields.BoxListFields.classes, predicted_labels) - detr_similarity_calculator = \ - region_similarity_calculator.DETRSimilarity() - detr_similarity = detr_similarity_calculator.compare( - boxes1, boxes2, None) - return detr_similarity - exp_output = [[0.0, -20 - 8.0/3.0 + 1000.0]] - sim_output = self.execute(graph_fn, []) - self.assertAllClose(sim_output, exp_output) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/core/standard_fields.py b/research/object_detection/core/standard_fields.py deleted file mode 100644 index 2267dff52f8..00000000000 --- a/research/object_detection/core/standard_fields.py +++ /dev/null @@ -1,356 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Contains classes specifying naming conventions used for object detection. - - -Specifies: - InputDataFields: standard fields used by reader/preprocessor/batcher. - DetectionResultFields: standard fields returned by object detector. - BoxListFields: standard field used by BoxList - TfExampleFields: standard fields for tf-example data format (go/tf-example). -""" - - -class InputDataFields(object): - """Names for the input tensors. - - Holds the standard data field names to use for identifying input tensors. This - should be used by the decoder to identify keys for the returned tensor_dict - containing input tensors. And it should be used by the model to identify the - tensors it needs. - - Attributes: - image: image. - image_additional_channels: additional channels. - original_image: image in the original input size. - original_image_spatial_shape: image in the original input size. - key: unique key corresponding to image. - source_id: source of the original image. - filename: original filename of the dataset (without common path). - groundtruth_image_classes: image-level class labels. - groundtruth_image_confidences: image-level class confidences. - groundtruth_labeled_classes: image-level annotation that indicates the - classes for which an image has been labeled. - groundtruth_boxes: coordinates of the ground truth boxes in the image. - groundtruth_classes: box-level class labels. - groundtruth_track_ids: box-level track ID labels. - groundtruth_temporal_offset: box-level temporal offsets, i.e., - movement of the box center in adjacent frames. - groundtruth_track_match_flags: box-level flags indicating if objects - exist in the previous frame. - groundtruth_confidences: box-level class confidences. The shape should be - the same as the shape of groundtruth_classes. - groundtruth_label_types: box-level label types (e.g. explicit negative). - groundtruth_is_crowd: [DEPRECATED, use groundtruth_group_of instead] - is the groundtruth a single object or a crowd. - groundtruth_area: area of a groundtruth segment. - groundtruth_difficult: is a `difficult` object - groundtruth_group_of: is a `group_of` objects, e.g. multiple objects of the - same class, forming a connected group, where instances are heavily - occluding each other. - proposal_boxes: coordinates of object proposal boxes. - proposal_objectness: objectness score of each proposal. - groundtruth_instance_masks: ground truth instance masks. - groundtruth_instance_mask_weights: ground truth instance masks weights. - groundtruth_instance_boundaries: ground truth instance boundaries. - groundtruth_instance_classes: instance mask-level class labels. - groundtruth_keypoints: ground truth keypoints. - groundtruth_keypoint_depths: Relative depth of the keypoints. - groundtruth_keypoint_depth_weights: Weights of the relative depth of the - keypoints. - groundtruth_keypoint_visibilities: ground truth keypoint visibilities. - groundtruth_keypoint_weights: groundtruth weight factor for keypoints. - groundtruth_label_weights: groundtruth label weights. - groundtruth_verified_negative_classes: groundtruth verified negative classes - groundtruth_not_exhaustive_classes: groundtruth not-exhaustively labeled - classes. - groundtruth_weights: groundtruth weight factor for bounding boxes. - groundtruth_dp_num_points: The number of DensePose sampled points for each - instance. - groundtruth_dp_part_ids: Part indices for DensePose points. - groundtruth_dp_surface_coords: Image locations and UV coordinates for - DensePose points. - num_groundtruth_boxes: number of groundtruth boxes. - is_annotated: whether an image has been labeled or not. - true_image_shapes: true shapes of images in the resized images, as resized - images can be padded with zeros. - multiclass_scores: the label score per class for each box. - context_features: a flattened list of contextual features. - context_feature_length: the fixed length of each feature in - context_features, used for reshaping. - valid_context_size: the valid context size, used in filtering the padded - context features. - context_features_image_id_list: the list of image source ids corresponding - to the features in context_features - image_format: format for the images, used to decode - image_height: height of images, used to decode - image_width: width of images, used to decode - """ - image = 'image' - image_additional_channels = 'image_additional_channels' - original_image = 'original_image' - original_image_spatial_shape = 'original_image_spatial_shape' - key = 'key' - source_id = 'source_id' - filename = 'filename' - groundtruth_image_classes = 'groundtruth_image_classes' - groundtruth_image_confidences = 'groundtruth_image_confidences' - groundtruth_labeled_classes = 'groundtruth_labeled_classes' - groundtruth_boxes = 'groundtruth_boxes' - groundtruth_classes = 'groundtruth_classes' - groundtruth_track_ids = 'groundtruth_track_ids' - groundtruth_temporal_offset = 'groundtruth_temporal_offset' - groundtruth_track_match_flags = 'groundtruth_track_match_flags' - groundtruth_confidences = 'groundtruth_confidences' - groundtruth_label_types = 'groundtruth_label_types' - groundtruth_is_crowd = 'groundtruth_is_crowd' - groundtruth_area = 'groundtruth_area' - groundtruth_difficult = 'groundtruth_difficult' - groundtruth_group_of = 'groundtruth_group_of' - proposal_boxes = 'proposal_boxes' - proposal_objectness = 'proposal_objectness' - groundtruth_instance_masks = 'groundtruth_instance_masks' - groundtruth_instance_mask_weights = 'groundtruth_instance_mask_weights' - groundtruth_instance_boundaries = 'groundtruth_instance_boundaries' - groundtruth_instance_classes = 'groundtruth_instance_classes' - groundtruth_keypoints = 'groundtruth_keypoints' - groundtruth_keypoint_depths = 'groundtruth_keypoint_depths' - groundtruth_keypoint_depth_weights = 'groundtruth_keypoint_depth_weights' - groundtruth_keypoint_visibilities = 'groundtruth_keypoint_visibilities' - groundtruth_keypoint_weights = 'groundtruth_keypoint_weights' - groundtruth_label_weights = 'groundtruth_label_weights' - groundtruth_verified_neg_classes = 'groundtruth_verified_neg_classes' - groundtruth_not_exhaustive_classes = 'groundtruth_not_exhaustive_classes' - groundtruth_weights = 'groundtruth_weights' - groundtruth_dp_num_points = 'groundtruth_dp_num_points' - groundtruth_dp_part_ids = 'groundtruth_dp_part_ids' - groundtruth_dp_surface_coords = 'groundtruth_dp_surface_coords' - num_groundtruth_boxes = 'num_groundtruth_boxes' - is_annotated = 'is_annotated' - true_image_shape = 'true_image_shape' - multiclass_scores = 'multiclass_scores' - context_features = 'context_features' - context_feature_length = 'context_feature_length' - valid_context_size = 'valid_context_size' - context_features_image_id_list = 'context_features_image_id_list' - image_timestamps = 'image_timestamps' - image_format = 'image_format' - image_height = 'image_height' - image_width = 'image_width' - - -class DetectionResultFields(object): - """Naming conventions for storing the output of the detector. - - Attributes: - source_id: source of the original image. - key: unique key corresponding to image. - detection_boxes: coordinates of the detection boxes in the image. - detection_scores: detection scores for the detection boxes in the image. - detection_multiclass_scores: class score distribution (including background) - for detection boxes in the image including background class. - detection_classes: detection-level class labels. - detection_masks: contains a segmentation mask for each detection box. - detection_surface_coords: contains DensePose surface coordinates for each - box. - detection_boundaries: contains an object boundary for each detection box. - detection_keypoints: contains detection keypoints for each detection box. - detection_keypoint_scores: contains detection keypoint scores. - detection_keypoint_depths: contains detection keypoint depths. - num_detections: number of detections in the batch. - raw_detection_boxes: contains decoded detection boxes without Non-Max - suppression. - raw_detection_scores: contains class score logits for raw detection boxes. - detection_anchor_indices: The anchor indices of the detections after NMS. - detection_features: contains extracted features for each detected box - after NMS. - """ - - source_id = 'source_id' - key = 'key' - detection_boxes = 'detection_boxes' - detection_scores = 'detection_scores' - detection_multiclass_scores = 'detection_multiclass_scores' - detection_features = 'detection_features' - detection_classes = 'detection_classes' - detection_masks = 'detection_masks' - detection_surface_coords = 'detection_surface_coords' - detection_boundaries = 'detection_boundaries' - detection_keypoints = 'detection_keypoints' - detection_keypoint_scores = 'detection_keypoint_scores' - detection_keypoint_depths = 'detection_keypoint_depths' - detection_embeddings = 'detection_embeddings' - detection_offsets = 'detection_temporal_offsets' - num_detections = 'num_detections' - raw_detection_boxes = 'raw_detection_boxes' - raw_detection_scores = 'raw_detection_scores' - detection_anchor_indices = 'detection_anchor_indices' - - -class BoxListFields(object): - """Naming conventions for BoxLists. - - Attributes: - boxes: bounding box coordinates. - classes: classes per bounding box. - scores: scores per bounding box. - weights: sample weights per bounding box. - objectness: objectness score per bounding box. - masks: masks per bounding box. - mask_weights: mask weights for each bounding box. - boundaries: boundaries per bounding box. - keypoints: keypoints per bounding box. - keypoint_visibilities: keypoint visibilities per bounding box. - keypoint_heatmaps: keypoint heatmaps per bounding box. - keypoint_depths: keypoint depths per bounding box. - keypoint_depth_weights: keypoint depth weights per bounding box. - densepose_num_points: number of DensePose points per bounding box. - densepose_part_ids: DensePose part ids per bounding box. - densepose_surface_coords: DensePose surface coordinates per bounding box. - is_crowd: is_crowd annotation per bounding box. - temporal_offsets: temporal center offsets per bounding box. - track_match_flags: match flags per bounding box. - """ - boxes = 'boxes' - classes = 'classes' - scores = 'scores' - weights = 'weights' - confidences = 'confidences' - objectness = 'objectness' - masks = 'masks' - mask_weights = 'mask_weights' - boundaries = 'boundaries' - keypoints = 'keypoints' - keypoint_visibilities = 'keypoint_visibilities' - keypoint_heatmaps = 'keypoint_heatmaps' - keypoint_depths = 'keypoint_depths' - keypoint_depth_weights = 'keypoint_depth_weights' - densepose_num_points = 'densepose_num_points' - densepose_part_ids = 'densepose_part_ids' - densepose_surface_coords = 'densepose_surface_coords' - is_crowd = 'is_crowd' - group_of = 'group_of' - track_ids = 'track_ids' - temporal_offsets = 'temporal_offsets' - track_match_flags = 'track_match_flags' - - -class PredictionFields(object): - """Naming conventions for standardized prediction outputs. - - Attributes: - feature_maps: List of feature maps for prediction. - anchors: Generated anchors. - raw_detection_boxes: Decoded detection boxes without NMS. - raw_detection_feature_map_indices: Feature map indices from which each raw - detection box was produced. - """ - feature_maps = 'feature_maps' - anchors = 'anchors' - raw_detection_boxes = 'raw_detection_boxes' - raw_detection_feature_map_indices = 'raw_detection_feature_map_indices' - - -class TfExampleFields(object): - """TF-example proto feature names for object detection. - - Holds the standard feature names to load from an Example proto for object - detection. - - Attributes: - image_encoded: JPEG encoded string - image_format: image format, e.g. "JPEG" - filename: filename - channels: number of channels of image - colorspace: colorspace, e.g. "RGB" - height: height of image in pixels, e.g. 462 - width: width of image in pixels, e.g. 581 - source_id: original source of the image - image_class_text: image-level label in text format - image_class_label: image-level label in numerical format - image_class_confidence: image-level confidence of the label - object_class_text: labels in text format, e.g. ["person", "cat"] - object_class_label: labels in numbers, e.g. [16, 8] - object_bbox_xmin: xmin coordinates of groundtruth box, e.g. 10, 30 - object_bbox_xmax: xmax coordinates of groundtruth box, e.g. 50, 40 - object_bbox_ymin: ymin coordinates of groundtruth box, e.g. 40, 50 - object_bbox_ymax: ymax coordinates of groundtruth box, e.g. 80, 70 - object_view: viewpoint of object, e.g. ["frontal", "left"] - object_truncated: is object truncated, e.g. [true, false] - object_occluded: is object occluded, e.g. [true, false] - object_difficult: is object difficult, e.g. [true, false] - object_group_of: is object a single object or a group of objects - object_depiction: is object a depiction - object_is_crowd: [DEPRECATED, use object_group_of instead] - is the object a single object or a crowd - object_segment_area: the area of the segment. - object_weight: a weight factor for the object's bounding box. - instance_masks: instance segmentation masks. - instance_boundaries: instance boundaries. - instance_classes: Classes for each instance segmentation mask. - detection_class_label: class label in numbers. - detection_bbox_ymin: ymin coordinates of a detection box. - detection_bbox_xmin: xmin coordinates of a detection box. - detection_bbox_ymax: ymax coordinates of a detection box. - detection_bbox_xmax: xmax coordinates of a detection box. - detection_score: detection score for the class label and box. - """ - image_encoded = 'image/encoded' - image_format = 'image/format' # format is reserved keyword - filename = 'image/filename' - channels = 'image/channels' - colorspace = 'image/colorspace' - height = 'image/height' - width = 'image/width' - source_id = 'image/source_id' - image_class_text = 'image/class/text' - image_class_label = 'image/class/label' - image_class_confidence = 'image/class/confidence' - object_class_text = 'image/object/class/text' - object_class_label = 'image/object/class/label' - object_bbox_ymin = 'image/object/bbox/ymin' - object_bbox_xmin = 'image/object/bbox/xmin' - object_bbox_ymax = 'image/object/bbox/ymax' - object_bbox_xmax = 'image/object/bbox/xmax' - object_view = 'image/object/view' - object_truncated = 'image/object/truncated' - object_occluded = 'image/object/occluded' - object_difficult = 'image/object/difficult' - object_group_of = 'image/object/group_of' - object_depiction = 'image/object/depiction' - object_is_crowd = 'image/object/is_crowd' - object_segment_area = 'image/object/segment/area' - object_weight = 'image/object/weight' - instance_masks = 'image/segmentation/object' - instance_boundaries = 'image/boundaries/object' - instance_classes = 'image/segmentation/object/class' - detection_class_label = 'image/detection/label' - detection_bbox_ymin = 'image/detection/bbox/ymin' - detection_bbox_xmin = 'image/detection/bbox/xmin' - detection_bbox_ymax = 'image/detection/bbox/ymax' - detection_bbox_xmax = 'image/detection/bbox/xmax' - detection_score = 'image/detection/score' - -# Sequence fields for SequenceExample inputs. -# All others are considered context fields. -SEQUENCE_FIELDS = [InputDataFields.image, - InputDataFields.source_id, - InputDataFields.groundtruth_boxes, - InputDataFields.num_groundtruth_boxes, - InputDataFields.groundtruth_classes, - InputDataFields.groundtruth_weights, - InputDataFields.source_id, - InputDataFields.is_annotated] diff --git a/research/object_detection/core/target_assigner.py b/research/object_detection/core/target_assigner.py deleted file mode 100644 index dfc78a58876..00000000000 --- a/research/object_detection/core/target_assigner.py +++ /dev/null @@ -1,2752 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base target assigner module. - -The job of a TargetAssigner is, for a given set of anchors (bounding boxes) and -groundtruth detections (bounding boxes), to assign classification and regression -targets to each anchor as well as weights to each anchor (specifying, e.g., -which anchors should not contribute to training loss). - -It assigns classification/regression targets by performing the following steps: -1) Computing pairwise similarity between anchors and groundtruth boxes using a - provided RegionSimilarity Calculator -2) Computing a matching based on the similarity matrix using a provided Matcher -3) Assigning regression targets based on the matching and a provided BoxCoder -4) Assigning classification targets based on the matching and groundtruth labels - -Note that TargetAssigners only operate on detections from a single -image at a time, so any logic for applying a TargetAssigner to multiple -images must be handled externally. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf -import tensorflow.compat.v2 as tf2 - -from object_detection.box_coders import faster_rcnn_box_coder -from object_detection.box_coders import mean_stddev_box_coder -from object_detection.core import box_coder -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import densepose_ops -from object_detection.core import keypoint_ops -from object_detection.core import matcher as mat -from object_detection.core import region_similarity_calculator as sim_calc -from object_detection.core import standard_fields as fields -from object_detection.matchers import argmax_matcher -from object_detection.matchers import hungarian_matcher -from object_detection.utils import shape_utils -from object_detection.utils import target_assigner_utils as ta_utils -from object_detection.utils import tf_version - -if tf_version.is_tf1(): - from object_detection.matchers import bipartite_matcher # pylint: disable=g-import-not-at-top - -ResizeMethod = tf2.image.ResizeMethod - -_DEFAULT_KEYPOINT_OFFSET_STD_DEV = 1.0 - - -class TargetAssigner(object): - """Target assigner to compute classification and regression targets.""" - - def __init__(self, - similarity_calc, - matcher, - box_coder_instance, - negative_class_weight=1.0): - """Construct Object Detection Target Assigner. - - Args: - similarity_calc: a RegionSimilarityCalculator - matcher: an object_detection.core.Matcher used to match groundtruth to - anchors. - box_coder_instance: an object_detection.core.BoxCoder used to encode - matching groundtruth boxes with respect to anchors. - negative_class_weight: classification weight to be associated to negative - anchors (default: 1.0). The weight must be in [0., 1.]. - - Raises: - ValueError: if similarity_calc is not a RegionSimilarityCalculator or - if matcher is not a Matcher or if box_coder is not a BoxCoder - """ - if not isinstance(similarity_calc, sim_calc.RegionSimilarityCalculator): - raise ValueError('similarity_calc must be a RegionSimilarityCalculator') - if not isinstance(matcher, mat.Matcher): - raise ValueError('matcher must be a Matcher') - if not isinstance(box_coder_instance, box_coder.BoxCoder): - raise ValueError('box_coder must be a BoxCoder') - self._similarity_calc = similarity_calc - self._matcher = matcher - self._box_coder = box_coder_instance - self._negative_class_weight = negative_class_weight - - @property - def box_coder(self): - return self._box_coder - - # TODO(rathodv): move labels, scores, and weights to groundtruth_boxes fields. - def assign(self, - anchors, - groundtruth_boxes, - groundtruth_labels=None, - unmatched_class_label=None, - groundtruth_weights=None): - """Assign classification and regression targets to each anchor. - - For a given set of anchors and groundtruth detections, match anchors - to groundtruth_boxes and assign classification and regression targets to - each anchor as well as weights based on the resulting match (specifying, - e.g., which anchors should not contribute to training loss). - - Anchors that are not matched to anything are given a classification target - of self._unmatched_cls_target which can be specified via the constructor. - - Args: - anchors: a BoxList representing N anchors - groundtruth_boxes: a BoxList representing M groundtruth boxes - groundtruth_labels: a tensor of shape [M, d_1, ... d_k] - with labels for each of the ground_truth boxes. The subshape - [d_1, ... d_k] can be empty (corresponding to scalar inputs). When set - to None, groundtruth_labels assumes a binary problem where all - ground_truth boxes get a positive label (of 1). - unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] - which is consistent with the classification target for each - anchor (and can be empty for scalar targets). This shape must thus be - compatible with the groundtruth labels that are passed to the "assign" - function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). - If set to None, unmatched_cls_target is set to be [0] for each anchor. - groundtruth_weights: a float tensor of shape [M] indicating the weight to - assign to all anchors match to a particular groundtruth box. The weights - must be in [0., 1.]. If None, all weights are set to 1. Generally no - groundtruth boxes with zero weight match to any anchors as matchers are - aware of groundtruth weights. Additionally, `cls_weights` and - `reg_weights` are calculated using groundtruth weights as an added - safety. - - Returns: - cls_targets: a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k], - where the subshape [d_1, ..., d_k] is compatible with groundtruth_labels - which has shape [num_gt_boxes, d_1, d_2, ... d_k]. - cls_weights: a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k], - representing weights for each element in cls_targets. - reg_targets: a float32 tensor with shape [num_anchors, box_code_dimension] - reg_weights: a float32 tensor with shape [num_anchors] - match: an int32 tensor of shape [num_anchors] containing result of anchor - groundtruth matching. Each position in the tensor indicates an anchor - and holds the following meaning: - (1) if match[i] >= 0, anchor i is matched with groundtruth match[i]. - (2) if match[i]=-1, anchor i is marked to be background . - (3) if match[i]=-2, anchor i is ignored since it is not background and - does not have sufficient overlap to call it a foreground. - - Raises: - ValueError: if anchors or groundtruth_boxes are not of type - box_list.BoxList - """ - if not isinstance(anchors, box_list.BoxList): - raise ValueError('anchors must be an BoxList') - if not isinstance(groundtruth_boxes, box_list.BoxList): - raise ValueError('groundtruth_boxes must be an BoxList') - - if unmatched_class_label is None: - unmatched_class_label = tf.constant([0], tf.float32) - - if groundtruth_labels is None: - groundtruth_labels = tf.ones(tf.expand_dims(groundtruth_boxes.num_boxes(), - 0)) - groundtruth_labels = tf.expand_dims(groundtruth_labels, -1) - - unmatched_shape_assert = shape_utils.assert_shape_equal( - shape_utils.combined_static_and_dynamic_shape(groundtruth_labels)[1:], - shape_utils.combined_static_and_dynamic_shape(unmatched_class_label)) - labels_and_box_shapes_assert = shape_utils.assert_shape_equal( - shape_utils.combined_static_and_dynamic_shape( - groundtruth_labels)[:1], - shape_utils.combined_static_and_dynamic_shape( - groundtruth_boxes.get())[:1]) - - if groundtruth_weights is None: - num_gt_boxes = groundtruth_boxes.num_boxes_static() - if not num_gt_boxes: - num_gt_boxes = groundtruth_boxes.num_boxes() - groundtruth_weights = tf.ones([num_gt_boxes], dtype=tf.float32) - - # set scores on the gt boxes - scores = 1 - groundtruth_labels[:, 0] - groundtruth_boxes.add_field(fields.BoxListFields.scores, scores) - - with tf.control_dependencies( - [unmatched_shape_assert, labels_and_box_shapes_assert]): - match_quality_matrix = self._similarity_calc.compare(groundtruth_boxes, - anchors) - match = self._matcher.match(match_quality_matrix, - valid_rows=tf.greater(groundtruth_weights, 0)) - reg_targets = self._create_regression_targets(anchors, - groundtruth_boxes, - match) - cls_targets = self._create_classification_targets(groundtruth_labels, - unmatched_class_label, - match) - reg_weights = self._create_regression_weights(match, groundtruth_weights) - - cls_weights = self._create_classification_weights(match, - groundtruth_weights) - # convert cls_weights from per-anchor to per-class. - class_label_shape = tf.shape(cls_targets)[1:] - weights_shape = tf.shape(cls_weights) - weights_multiple = tf.concat( - [tf.ones_like(weights_shape), class_label_shape], - axis=0) - for _ in range(len(cls_targets.get_shape()[1:])): - cls_weights = tf.expand_dims(cls_weights, -1) - cls_weights = tf.tile(cls_weights, weights_multiple) - - num_anchors = anchors.num_boxes_static() - if num_anchors is not None: - reg_targets = self._reset_target_shape(reg_targets, num_anchors) - cls_targets = self._reset_target_shape(cls_targets, num_anchors) - reg_weights = self._reset_target_shape(reg_weights, num_anchors) - cls_weights = self._reset_target_shape(cls_weights, num_anchors) - - return (cls_targets, cls_weights, reg_targets, reg_weights, - match.match_results) - - def _reset_target_shape(self, target, num_anchors): - """Sets the static shape of the target. - - Args: - target: the target tensor. Its first dimension will be overwritten. - num_anchors: the number of anchors, which is used to override the target's - first dimension. - - Returns: - A tensor with the shape info filled in. - """ - target_shape = target.get_shape().as_list() - target_shape[0] = num_anchors - target.set_shape(target_shape) - return target - - def _create_regression_targets(self, anchors, groundtruth_boxes, match): - """Returns a regression target for each anchor. - - Args: - anchors: a BoxList representing N anchors - groundtruth_boxes: a BoxList representing M groundtruth_boxes - match: a matcher.Match object - - Returns: - reg_targets: a float32 tensor with shape [N, box_code_dimension] - """ - matched_gt_boxes = match.gather_based_on_match( - groundtruth_boxes.get(), - unmatched_value=tf.zeros(4), - ignored_value=tf.zeros(4)) - matched_gt_boxlist = box_list.BoxList(matched_gt_boxes) - if groundtruth_boxes.has_field(fields.BoxListFields.keypoints): - groundtruth_keypoints = groundtruth_boxes.get_field( - fields.BoxListFields.keypoints) - matched_keypoints = match.gather_based_on_match( - groundtruth_keypoints, - unmatched_value=tf.zeros(groundtruth_keypoints.get_shape()[1:]), - ignored_value=tf.zeros(groundtruth_keypoints.get_shape()[1:])) - matched_gt_boxlist.add_field(fields.BoxListFields.keypoints, - matched_keypoints) - matched_reg_targets = self._box_coder.encode(matched_gt_boxlist, anchors) - match_results_shape = shape_utils.combined_static_and_dynamic_shape( - match.match_results) - - # Zero out the unmatched and ignored regression targets. - unmatched_ignored_reg_targets = tf.tile( - self._default_regression_target(), [match_results_shape[0], 1]) - matched_anchors_mask = match.matched_column_indicator() - reg_targets = tf.where(matched_anchors_mask, - matched_reg_targets, - unmatched_ignored_reg_targets) - return reg_targets - - def _default_regression_target(self): - """Returns the default target for anchors to regress to. - - Default regression targets are set to zero (though in - this implementation what these targets are set to should - not matter as the regression weight of any box set to - regress to the default target is zero). - - Returns: - default_target: a float32 tensor with shape [1, box_code_dimension] - """ - return tf.constant([self._box_coder.code_size*[0]], tf.float32) - - def _create_classification_targets(self, groundtruth_labels, - unmatched_class_label, match): - """Create classification targets for each anchor. - - Assign a classification target of for each anchor to the matching - groundtruth label that is provided by match. Anchors that are not matched - to anything are given the target self._unmatched_cls_target - - Args: - groundtruth_labels: a tensor of shape [num_gt_boxes, d_1, ... d_k] - with labels for each of the ground_truth boxes. The subshape - [d_1, ... d_k] can be empty (corresponding to scalar labels). - unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] - which is consistent with the classification target for each - anchor (and can be empty for scalar targets). This shape must thus be - compatible with the groundtruth labels that are passed to the "assign" - function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). - match: a matcher.Match object that provides a matching between anchors - and groundtruth boxes. - - Returns: - a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k], where the - subshape [d_1, ..., d_k] is compatible with groundtruth_labels which has - shape [num_gt_boxes, d_1, d_2, ... d_k]. - """ - return match.gather_based_on_match( - groundtruth_labels, - unmatched_value=unmatched_class_label, - ignored_value=unmatched_class_label) - - def _create_regression_weights(self, match, groundtruth_weights): - """Set regression weight for each anchor. - - Only positive anchors are set to contribute to the regression loss, so this - method returns a weight of 1 for every positive anchor and 0 for every - negative anchor. - - Args: - match: a matcher.Match object that provides a matching between anchors - and groundtruth boxes. - groundtruth_weights: a float tensor of shape [M] indicating the weight to - assign to all anchors match to a particular groundtruth box. - - Returns: - a float32 tensor with shape [num_anchors] representing regression weights. - """ - return match.gather_based_on_match( - groundtruth_weights, ignored_value=0., unmatched_value=0.) - - def _create_classification_weights(self, - match, - groundtruth_weights): - """Create classification weights for each anchor. - - Positive (matched) anchors are associated with a weight of - positive_class_weight and negative (unmatched) anchors are associated with - a weight of negative_class_weight. When anchors are ignored, weights are set - to zero. By default, both positive/negative weights are set to 1.0, - but they can be adjusted to handle class imbalance (which is almost always - the case in object detection). - - Args: - match: a matcher.Match object that provides a matching between anchors - and groundtruth boxes. - groundtruth_weights: a float tensor of shape [M] indicating the weight to - assign to all anchors match to a particular groundtruth box. - - Returns: - a float32 tensor with shape [num_anchors] representing classification - weights. - """ - return match.gather_based_on_match( - groundtruth_weights, - ignored_value=0., - unmatched_value=self._negative_class_weight) - - def get_box_coder(self): - """Get BoxCoder of this TargetAssigner. - - Returns: - BoxCoder object. - """ - return self._box_coder - - -# TODO(rathodv): This method pulls in all the implementation dependencies into -# core. Therefore its best to have this factory method outside of core. -def create_target_assigner(reference, stage=None, - negative_class_weight=1.0, use_matmul_gather=False): - """Factory function for creating standard target assigners. - - Args: - reference: string referencing the type of TargetAssigner. - stage: string denoting stage: {proposal, detection}. - negative_class_weight: classification weight to be associated to negative - anchors (default: 1.0) - use_matmul_gather: whether to use matrix multiplication based gather which - are better suited for TPUs. - - Returns: - TargetAssigner: desired target assigner. - - Raises: - ValueError: if combination reference+stage is invalid. - """ - if reference == 'Multibox' and stage == 'proposal': - if tf_version.is_tf2(): - raise ValueError('GreedyBipartiteMatcher is not supported in TF 2.X.') - similarity_calc = sim_calc.NegSqDistSimilarity() - matcher = bipartite_matcher.GreedyBipartiteMatcher() - box_coder_instance = mean_stddev_box_coder.MeanStddevBoxCoder() - - elif reference == 'FasterRCNN' and stage == 'proposal': - similarity_calc = sim_calc.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.7, - unmatched_threshold=0.3, - force_match_for_each_row=True, - use_matmul_gather=use_matmul_gather) - box_coder_instance = faster_rcnn_box_coder.FasterRcnnBoxCoder( - scale_factors=[10.0, 10.0, 5.0, 5.0]) - - elif reference == 'FasterRCNN' and stage == 'detection': - similarity_calc = sim_calc.IouSimilarity() - # Uses all proposals with IOU < 0.5 as candidate negatives. - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - negatives_lower_than_unmatched=True, - use_matmul_gather=use_matmul_gather) - box_coder_instance = faster_rcnn_box_coder.FasterRcnnBoxCoder( - scale_factors=[10.0, 10.0, 5.0, 5.0]) - - elif reference == 'FastRCNN': - similarity_calc = sim_calc.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.1, - force_match_for_each_row=False, - negatives_lower_than_unmatched=False, - use_matmul_gather=use_matmul_gather) - box_coder_instance = faster_rcnn_box_coder.FasterRcnnBoxCoder() - - else: - raise ValueError('No valid combination of reference and stage.') - - return TargetAssigner(similarity_calc, matcher, box_coder_instance, - negative_class_weight=negative_class_weight) - - -def batch_assign(target_assigner, - anchors_batch, - gt_box_batch, - gt_class_targets_batch, - unmatched_class_label=None, - gt_weights_batch=None): - """Batched assignment of classification and regression targets. - - Args: - target_assigner: a target assigner. - anchors_batch: BoxList representing N box anchors or list of BoxList objects - with length batch_size representing anchor sets. - gt_box_batch: a list of BoxList objects with length batch_size - representing groundtruth boxes for each image in the batch - gt_class_targets_batch: a list of tensors with length batch_size, where - each tensor has shape [num_gt_boxes_i, classification_target_size] and - num_gt_boxes_i is the number of boxes in the ith boxlist of - gt_box_batch. - unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] - which is consistent with the classification target for each - anchor (and can be empty for scalar targets). This shape must thus be - compatible with the groundtruth labels that are passed to the "assign" - function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). - gt_weights_batch: A list of 1-D tf.float32 tensors of shape - [num_boxes] containing weights for groundtruth boxes. - - Returns: - batch_cls_targets: a tensor with shape [batch_size, num_anchors, - num_classes], - batch_cls_weights: a tensor with shape [batch_size, num_anchors, - num_classes], - batch_reg_targets: a tensor with shape [batch_size, num_anchors, - box_code_dimension] - batch_reg_weights: a tensor with shape [batch_size, num_anchors], - match: an int32 tensor of shape [batch_size, num_anchors] containing result - of anchor groundtruth matching. Each position in the tensor indicates an - anchor and holds the following meaning: - (1) if match[x, i] >= 0, anchor i is matched with groundtruth match[x, i]. - (2) if match[x, i]=-1, anchor i is marked to be background . - (3) if match[x, i]=-2, anchor i is ignored since it is not background and - does not have sufficient overlap to call it a foreground. - - Raises: - ValueError: if input list lengths are inconsistent, i.e., - batch_size == len(gt_box_batch) == len(gt_class_targets_batch) - and batch_size == len(anchors_batch) unless anchors_batch is a single - BoxList. - """ - if not isinstance(anchors_batch, list): - anchors_batch = len(gt_box_batch) * [anchors_batch] - if not all( - isinstance(anchors, box_list.BoxList) for anchors in anchors_batch): - raise ValueError('anchors_batch must be a BoxList or list of BoxLists.') - if not (len(anchors_batch) - == len(gt_box_batch) - == len(gt_class_targets_batch)): - raise ValueError('batch size incompatible with lengths of anchors_batch, ' - 'gt_box_batch and gt_class_targets_batch.') - cls_targets_list = [] - cls_weights_list = [] - reg_targets_list = [] - reg_weights_list = [] - match_list = [] - if gt_weights_batch is None: - gt_weights_batch = [None] * len(gt_class_targets_batch) - for anchors, gt_boxes, gt_class_targets, gt_weights in zip( - anchors_batch, gt_box_batch, gt_class_targets_batch, gt_weights_batch): - (cls_targets, cls_weights, - reg_targets, reg_weights, match) = target_assigner.assign( - anchors, gt_boxes, gt_class_targets, unmatched_class_label, - gt_weights) - cls_targets_list.append(cls_targets) - cls_weights_list.append(cls_weights) - reg_targets_list.append(reg_targets) - reg_weights_list.append(reg_weights) - match_list.append(match) - batch_cls_targets = tf.stack(cls_targets_list) - batch_cls_weights = tf.stack(cls_weights_list) - batch_reg_targets = tf.stack(reg_targets_list) - batch_reg_weights = tf.stack(reg_weights_list) - batch_match = tf.stack(match_list) - return (batch_cls_targets, batch_cls_weights, batch_reg_targets, - batch_reg_weights, batch_match) - - -# Assign an alias to avoid large refactor of existing users. -batch_assign_targets = batch_assign - - -def batch_get_targets(batch_match, groundtruth_tensor_list, - groundtruth_weights_list, unmatched_value, - unmatched_weight): - """Returns targets based on anchor-groundtruth box matching results. - - Args: - batch_match: An int32 tensor of shape [batch, num_anchors] containing the - result of target assignment returned by TargetAssigner.assign(..). - groundtruth_tensor_list: A list of groundtruth tensors of shape - [num_groundtruth, d_1, d_2, ..., d_k]. The tensors can be of any type. - groundtruth_weights_list: A list of weights, one per groundtruth tensor, of - shape [num_groundtruth]. - unmatched_value: A tensor of shape [d_1, d_2, ..., d_k] of the same type as - groundtruth tensor containing target value for anchors that remain - unmatched. - unmatched_weight: Scalar weight to assign to anchors that remain unmatched. - - Returns: - targets: A tensor of shape [batch, num_anchors, d_1, d_2, ..., d_k] - containing targets for anchors. - weights: A float tensor of shape [batch, num_anchors] containing the weights - to assign to each target. - """ - match_list = tf.unstack(batch_match) - targets_list = [] - weights_list = [] - for match_tensor, groundtruth_tensor, groundtruth_weight in zip( - match_list, groundtruth_tensor_list, groundtruth_weights_list): - match_object = mat.Match(match_tensor) - targets = match_object.gather_based_on_match( - groundtruth_tensor, - unmatched_value=unmatched_value, - ignored_value=unmatched_value) - targets_list.append(targets) - weights = match_object.gather_based_on_match( - groundtruth_weight, - unmatched_value=unmatched_weight, - ignored_value=tf.zeros_like(unmatched_weight)) - weights_list.append(weights) - return tf.stack(targets_list), tf.stack(weights_list) - - -def batch_assign_confidences(target_assigner, - anchors_batch, - gt_box_batch, - gt_class_confidences_batch, - gt_weights_batch=None, - unmatched_class_label=None, - include_background_class=True, - implicit_class_weight=1.0): - """Batched assignment of classification and regression targets. - - This differences between batch_assign_confidences and batch_assign_targets: - - 'batch_assign_targets' supports scalar (agnostic), vector (multiclass) and - tensor (high-dimensional) targets. 'batch_assign_confidences' only support - scalar (agnostic) and vector (multiclass) targets. - - 'batch_assign_targets' assumes the input class tensor using the binary - one/K-hot encoding. 'batch_assign_confidences' takes the class confidence - scores as the input, where 1 means positive classes, 0 means implicit - negative classes, and -1 means explicit negative classes. - - 'batch_assign_confidences' assigns the targets in the similar way as - 'batch_assign_targets' except that it gives different weights for implicit - and explicit classes. This allows user to control the negative gradients - pushed differently for implicit and explicit examples during the training. - - Args: - target_assigner: a target assigner. - anchors_batch: BoxList representing N box anchors or list of BoxList objects - with length batch_size representing anchor sets. - gt_box_batch: a list of BoxList objects with length batch_size - representing groundtruth boxes for each image in the batch - gt_class_confidences_batch: a list of tensors with length batch_size, where - each tensor has shape [num_gt_boxes_i, classification_target_size] and - num_gt_boxes_i is the number of boxes in the ith boxlist of - gt_box_batch. Note that in this tensor, 1 means explicit positive class, - -1 means explicit negative class, and 0 means implicit negative class. - gt_weights_batch: A list of 1-D tf.float32 tensors of shape - [num_gt_boxes_i] containing weights for groundtruth boxes. - unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] - which is consistent with the classification target for each - anchor (and can be empty for scalar targets). This shape must thus be - compatible with the groundtruth labels that are passed to the "assign" - function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). - include_background_class: whether or not gt_class_confidences_batch includes - the background class. - implicit_class_weight: the weight assigned to implicit examples. - - Returns: - batch_cls_targets: a tensor with shape [batch_size, num_anchors, - num_classes], - batch_cls_weights: a tensor with shape [batch_size, num_anchors, - num_classes], - batch_reg_targets: a tensor with shape [batch_size, num_anchors, - box_code_dimension] - batch_reg_weights: a tensor with shape [batch_size, num_anchors], - match: an int32 tensor of shape [batch_size, num_anchors] containing result - of anchor groundtruth matching. Each position in the tensor indicates an - anchor and holds the following meaning: - (1) if match[x, i] >= 0, anchor i is matched with groundtruth match[x, i]. - (2) if match[x, i]=-1, anchor i is marked to be background . - (3) if match[x, i]=-2, anchor i is ignored since it is not background and - does not have sufficient overlap to call it a foreground. - - Raises: - ValueError: if input list lengths are inconsistent, i.e., - batch_size == len(gt_box_batch) == len(gt_class_targets_batch) - and batch_size == len(anchors_batch) unless anchors_batch is a single - BoxList, or if any element in gt_class_confidences_batch has rank > 2. - """ - if not isinstance(anchors_batch, list): - anchors_batch = len(gt_box_batch) * [anchors_batch] - if not all( - isinstance(anchors, box_list.BoxList) for anchors in anchors_batch): - raise ValueError('anchors_batch must be a BoxList or list of BoxLists.') - if not (len(anchors_batch) - == len(gt_box_batch) - == len(gt_class_confidences_batch)): - raise ValueError('batch size incompatible with lengths of anchors_batch, ' - 'gt_box_batch and gt_class_confidences_batch.') - - cls_targets_list = [] - cls_weights_list = [] - reg_targets_list = [] - reg_weights_list = [] - match_list = [] - if gt_weights_batch is None: - gt_weights_batch = [None] * len(gt_class_confidences_batch) - for anchors, gt_boxes, gt_class_confidences, gt_weights in zip( - anchors_batch, gt_box_batch, gt_class_confidences_batch, - gt_weights_batch): - - if (gt_class_confidences is not None and - len(gt_class_confidences.get_shape().as_list()) > 2): - raise ValueError('The shape of the class target is not supported. ', - gt_class_confidences.get_shape()) - - cls_targets, _, reg_targets, _, match = target_assigner.assign( - anchors, gt_boxes, gt_class_confidences, unmatched_class_label, - groundtruth_weights=gt_weights) - - if include_background_class: - cls_targets_without_background = tf.slice( - cls_targets, [0, 1], [-1, -1]) - else: - cls_targets_without_background = cls_targets - - positive_mask = tf.greater(cls_targets_without_background, 0.0) - negative_mask = tf.less(cls_targets_without_background, 0.0) - explicit_example_mask = tf.logical_or(positive_mask, negative_mask) - positive_anchors = tf.reduce_any(positive_mask, axis=-1) - - regression_weights = tf.cast(positive_anchors, dtype=tf.float32) - regression_targets = ( - reg_targets * tf.expand_dims(regression_weights, axis=-1)) - regression_weights_expanded = tf.expand_dims(regression_weights, axis=-1) - - cls_targets_without_background = ( - cls_targets_without_background * - (1 - tf.cast(negative_mask, dtype=tf.float32))) - cls_weights_without_background = ((1 - implicit_class_weight) * tf.cast( - explicit_example_mask, dtype=tf.float32) + implicit_class_weight) - - if include_background_class: - cls_weights_background = ( - (1 - implicit_class_weight) * regression_weights_expanded - + implicit_class_weight) - classification_weights = tf.concat( - [cls_weights_background, cls_weights_without_background], axis=-1) - cls_targets_background = 1 - regression_weights_expanded - classification_targets = tf.concat( - [cls_targets_background, cls_targets_without_background], axis=-1) - else: - classification_targets = cls_targets_without_background - classification_weights = cls_weights_without_background - - cls_targets_list.append(classification_targets) - cls_weights_list.append(classification_weights) - reg_targets_list.append(regression_targets) - reg_weights_list.append(regression_weights) - match_list.append(match) - batch_cls_targets = tf.stack(cls_targets_list) - batch_cls_weights = tf.stack(cls_weights_list) - batch_reg_targets = tf.stack(reg_targets_list) - batch_reg_weights = tf.stack(reg_weights_list) - batch_match = tf.stack(match_list) - return (batch_cls_targets, batch_cls_weights, batch_reg_targets, - batch_reg_weights, batch_match) - - -def _smallest_positive_root(a, b, c): - """Returns the smallest positive root of a quadratic equation.""" - - discriminant = tf.sqrt(b ** 2 - 4 * a * c) - - # TODO(vighneshb) We are currently using the slightly incorrect - # CenterNet implementation. The commented lines implement the fixed version - # in https://github.com/princeton-vl/CornerNet. Change the implementation - # after verifying it has no negative impact. - # root1 = (-b - discriminant) / (2 * a) - # root2 = (-b + discriminant) / (2 * a) - - # return tf.where(tf.less(root1, 0), root2, root1) - - return (-b + discriminant) / (2.0) - - -def max_distance_for_overlap(height, width, min_iou): - """Computes how far apart bbox corners can lie while maintaining the iou. - - Given a bounding box size, this function returns a lower bound on how far - apart the corners of another box can lie while still maintaining the given - IoU. The implementation is based on the `gaussian_radius` function in the - Objects as Points github repo: https://github.com/xingyizhou/CenterNet - - Args: - height: A 1-D float Tensor representing height of the ground truth boxes. - width: A 1-D float Tensor representing width of the ground truth boxes. - min_iou: A float representing the minimum IoU desired. - - Returns: - distance: A 1-D Tensor of distances, of the same length as the input - height and width tensors. - """ - - # Given that the detected box is displaced at a distance `d`, the exact - # IoU value will depend on the angle at which each corner is displaced. - # We simplify our computation by assuming that each corner is displaced by - # a distance `d` in both x and y direction. This gives us a lower IoU than - # what is actually realizable and ensures that any box with corners less - # than `d` distance apart will always have an IoU greater than or equal - # to `min_iou` - - # The following 3 cases can be worked on geometrically and come down to - # solving a quadratic inequality. In each case, to ensure `min_iou` we use - # the smallest positive root of the equation. - - # Case where detected box is offset from ground truth and no box completely - # contains the other. - - distance_detection_offset = _smallest_positive_root( - a=1, b=-(height + width), - c=width * height * ((1 - min_iou) / (1 + min_iou)) - ) - - # Case where detection is smaller than ground truth and completely contained - # in it. - distance_detection_in_gt = _smallest_positive_root( - a=4, b=-2 * (height + width), - c=(1 - min_iou) * width * height - ) - - # Case where ground truth is smaller than detection and completely contained - # in it. - distance_gt_in_detection = _smallest_positive_root( - a=4 * min_iou, b=(2 * min_iou) * (width + height), - c=(min_iou - 1) * width * height - ) - - return tf.reduce_min([distance_detection_offset, - distance_gt_in_detection, - distance_detection_in_gt], axis=0) - - -def get_batch_predictions_from_indices(batch_predictions, indices): - """Gets the values of predictions in a batch at the given indices. - - The indices are expected to come from the offset targets generation functions - in this library. The returned value is intended to be used inside a loss - function. - - Args: - batch_predictions: A tensor of shape [batch_size, height, width, channels] - or [batch_size, height, width, class, channels] for class-specific - features (e.g. keypoint joint offsets). - indices: A tensor of shape [num_instances, 3] for single class features or - [num_instances, 4] for multiple classes features. - - Returns: - values: A tensor of shape [num_instances, channels] holding the predicted - values at the given indices. - """ - # Note, gather_nd (and its gradient scatter_nd) runs significantly slower (on - # TPU) than gather with flattened inputs, so reshape the tensor, flatten the - # indices, and run gather. - shape = shape_utils.combined_static_and_dynamic_shape(batch_predictions) - - # [B, H, W, C] -> [H*W, W, 1] or [B, H, W, N, C] -> [H*W*N, W*N, N, 1] - rev_cum_interior_indices = tf.reverse(tf.math.cumprod(shape[-2:0:-1]), [0]) - rev_cum_interior_indices = tf.concat([rev_cum_interior_indices, [1]], axis=0) - - # Compute flattened indices and gather. - flattened_inds = tf.linalg.matmul( - indices, rev_cum_interior_indices[:, tf.newaxis])[:, 0] - batch_predictions_2d = tf.reshape(batch_predictions, [-1, shape[-1]]) - return tf.gather(batch_predictions_2d, flattened_inds, axis=0) - - -def _compute_std_dev_from_box_size(boxes_height, boxes_width, min_overlap): - """Computes the standard deviation of the Gaussian kernel from box size. - - Args: - boxes_height: A 1D tensor with shape [num_instances] representing the height - of each box. - boxes_width: A 1D tensor with shape [num_instances] representing the width - of each box. - min_overlap: The minimum IOU overlap that boxes need to have to not be - penalized. - - Returns: - A 1D tensor with shape [num_instances] representing the computed Gaussian - sigma for each of the box. - """ - # We are dividing by 3 so that points closer than the computed - # distance have a >99% CDF. - sigma = max_distance_for_overlap(boxes_height, boxes_width, min_overlap) - sigma = (2 * tf.math.maximum(tf.math.floor(sigma), 0.0) + 1) / 6.0 - return sigma - - -def _preprocess_keypoints_and_weights(out_height, out_width, keypoints, - class_onehot, class_weights, - keypoint_weights, class_id, - keypoint_indices): - """Preprocesses the keypoints and the corresponding keypoint weights. - - This function performs several common steps to preprocess the keypoints and - keypoint weights features, including: - 1) Select the subset of keypoints based on the keypoint indices, fill the - keypoint NaN values with zeros and convert to absolute coordinates. - 2) Generate the weights of the keypoint using the following information: - a. The class of the instance. - b. The NaN value of the keypoint coordinates. - c. The provided keypoint weights. - - Args: - out_height: An integer or an integer tensor indicating the output height - of the model. - out_width: An integer or an integer tensor indicating the output width of - the model. - keypoints: A float tensor of shape [num_instances, num_total_keypoints, 2] - representing the original keypoint grountruth coordinates. - class_onehot: A float tensor of shape [num_instances, num_classes] - containing the class targets with the 0th index assumed to map to the - first non-background class. - class_weights: A float tensor of shape [num_instances] containing weights - for groundtruth instances. - keypoint_weights: A float tensor of shape - [num_instances, num_total_keypoints] representing the weights of each - keypoints. - class_id: int, the ID of the class (0-indexed) that contains the target - keypoints to consider in this task. - keypoint_indices: A list of integers representing the indices of the - keypoints to be considered in this task. This is used to retrieve the - subset of the keypoints that should be considered in this task. - - Returns: - A tuple of two tensors: - keypoint_absolute: A float tensor of shape - [num_instances, num_keypoints, 2] which is the selected and updated - keypoint coordinates. - keypoint_weights: A float tensor of shape [num_instances, num_keypoints] - representing the updated weight of each keypoint. - """ - # Select the targets keypoints by their type ids and generate the mask - # of valid elements. - valid_mask, keypoints = ta_utils.get_valid_keypoint_mask_for_class( - keypoint_coordinates=keypoints, - class_id=class_id, - class_onehot=class_onehot, - class_weights=class_weights, - keypoint_indices=keypoint_indices) - # Keypoint coordinates in absolute coordinate system. - # The shape of the tensors: [num_instances, num_keypoints, 2]. - keypoints_absolute = keypoint_ops.to_absolute_coordinates( - keypoints, out_height, out_width) - # Assign default weights for the keypoints. - if keypoint_weights is None: - keypoint_weights = tf.ones_like(keypoints[:, :, 0]) - else: - keypoint_weights = tf.gather( - keypoint_weights, indices=keypoint_indices, axis=1) - keypoint_weights = keypoint_weights * valid_mask - return keypoints_absolute, keypoint_weights - - -class CenterNetCenterHeatmapTargetAssigner(object): - """Wrapper to compute the object center heatmap.""" - - def __init__(self, - stride, - min_overlap=0.7, - compute_heatmap_sparse=False, - keypoint_class_id=None, - keypoint_indices=None, - keypoint_weights_for_center=None, - box_heatmap_type='adaptive_gaussian', - heatmap_exponent=1.0): - """Initializes the target assigner. - - Args: - stride: int, the stride of the network in output pixels. - min_overlap: The minimum IOU overlap that boxes need to have to not be - penalized. - compute_heatmap_sparse: bool, indicating whether or not to use the sparse - version of the Op that computes the heatmap. The sparse version scales - better with number of classes, but in some cases is known to cause - OOM error. See (b/170989061). - keypoint_class_id: int, the ID of the class (0-indexed) that contains the - target keypoints to consider in this task. - keypoint_indices: A list of integers representing the indices of the - keypoints to be considered in this task. This is used to retrieve the - subset of the keypoints from gt_keypoints that should be considered in - this task. - keypoint_weights_for_center: The keypoint weights used for calculating the - location of object center. The number of weights need to be the same as - the number of keypoints. The object center is calculated by the weighted - mean of the keypoint locations. If not provided, the object center is - determined by the center of the bounding box (default behavior). - box_heatmap_type: str, the algorithm used to compute the box heatmap, - used when calling the assign_center_targets_from_boxes method. - Options are: - 'adaptaive_gaussian': A box-size adaptive Gaussian from the original - paper[1]. - 'iou': IOU based heatmap target where each point is assigned an IOU - based on its location, assuming that it produced a box centered at - that point with the correct size. - heatmap_exponent: float, The generated heatmap is exponentiated with - this number. A number > 1 will result in the heatmap being more peaky - and a number < 1 will cause the heatmap to be more spreadout. - """ - - self._stride = stride - self._min_overlap = min_overlap - self._compute_heatmap_sparse = compute_heatmap_sparse - self._keypoint_class_id = keypoint_class_id - self._keypoint_indices = keypoint_indices - self._keypoint_weights_for_center = keypoint_weights_for_center - self._box_heatmap_type = box_heatmap_type - self._heatmap_exponent = heatmap_exponent - - def assign_center_targets_from_boxes(self, - height, - width, - gt_boxes_list, - gt_classes_list, - gt_weights_list=None, - maximum_normalized_coordinate=1.1): - """Computes the object center heatmap target. - - Args: - height: int, height of input to the model. This is used to - determine the height of the output. - width: int, width of the input to the model. This is used to - determine the width of the output. - gt_boxes_list: A list of float tensors with shape [num_boxes, 4] - representing the groundtruth detection bounding boxes for each sample in - the batch. The box coordinates are expected in normalized coordinates. - gt_classes_list: A list of float tensors with shape [num_boxes, - num_classes] representing the one-hot encoded class labels for each box - in the gt_boxes_list. - gt_weights_list: A list of float tensors with shape [num_boxes] - representing the weight of each groundtruth detection box. - maximum_normalized_coordinate: Maximum coordinate value to be considered - as normalized, default to 1.1. This is used to check bounds during - converting normalized coordinates to absolute coordinates. - - Returns: - heatmap: A Tensor of size [batch_size, output_height, output_width, - num_classes] representing the per class center heatmap. output_height - and output_width are computed by dividing the input height and width by - the stride specified during initialization. - """ - - out_height = tf.cast(tf.maximum(height // self._stride, 1), tf.float32) - out_width = tf.cast(tf.maximum(width // self._stride, 1), tf.float32) - # Compute the yx-grid to be used to generate the heatmap. Each returned - # tensor has shape of [out_height, out_width] - (y_grid, x_grid) = ta_utils.image_shape_to_grids(out_height, out_width) - - heatmaps = [] - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_boxes_list) - # TODO(vighneshb) Replace the for loop with a batch version. - for boxes, class_targets, weights in zip(gt_boxes_list, gt_classes_list, - gt_weights_list): - boxes = box_list.BoxList(boxes) - # Convert the box coordinates to absolute output image dimension space. - boxes = box_list_ops.to_absolute_coordinates( - boxes, - tf.maximum(height // self._stride, 1), - tf.maximum(width // self._stride, 1), - maximum_normalized_coordinate=maximum_normalized_coordinate) - # Get the box center coordinates. Each returned tensors have the shape of - # [num_instances] - (y_center, x_center, boxes_height, - boxes_width) = boxes.get_center_coordinates_and_sizes() - - # Compute the sigma from box size. The tensor shape: [num_instances]. - sigma = _compute_std_dev_from_box_size(boxes_height, boxes_width, - self._min_overlap) - # Apply the Gaussian kernel to the center coordinates. Returned heatmap - # has shape of [out_height, out_width, num_classes] - - if self._box_heatmap_type == 'adaptive_gaussian': - heatmap = ta_utils.coordinates_to_heatmap( - y_grid=y_grid, - x_grid=x_grid, - y_coordinates=y_center, - x_coordinates=x_center, - sigma=sigma, - channel_onehot=class_targets, - channel_weights=weights, - sparse=self._compute_heatmap_sparse) - elif self._box_heatmap_type == 'iou': - heatmap = ta_utils.coordinates_to_iou(y_grid, x_grid, boxes, - class_targets, weights) - else: - raise ValueError(f'Unknown heatmap type - {self._box_heatmap_type}') - - heatmap = tf.stop_gradient(heatmap) - - heatmaps.append(heatmap) - - # Return the stacked heatmaps over the batch. - stacked_heatmaps = tf.stack(heatmaps, axis=0) - return (tf.pow(stacked_heatmaps, self._heatmap_exponent) if - self._heatmap_exponent != 1.0 else stacked_heatmaps) - - def assign_center_targets_from_keypoints(self, - height, - width, - gt_classes_list, - gt_keypoints_list, - gt_weights_list=None, - gt_keypoints_weights_list=None): - """Computes the object center heatmap target using keypoint locations. - - Args: - height: int, height of input to the model. This is used to - determine the height of the output. - width: int, width of the input to the model. This is used to - determine the width of the output. - gt_classes_list: A list of float tensors with shape [num_boxes, - num_classes] representing the one-hot encoded class labels for each box - in the gt_boxes_list. - gt_keypoints_list: A list of float tensors with shape [num_boxes, 4] - representing the groundtruth detection bounding boxes for each sample in - the batch. The box coordinates are expected in normalized coordinates. - gt_weights_list: A list of float tensors with shape [num_boxes] - representing the weight of each groundtruth detection box. - gt_keypoints_weights_list: [Optional] a list of 3D tf.float32 tensors of - shape [num_instances, num_total_keypoints] representing the weights of - each keypoints. If not provided, then all not NaN keypoints will be - equally weighted. - - Returns: - heatmap: A Tensor of size [batch_size, output_height, output_width, - num_classes] representing the per class center heatmap. output_height - and output_width are computed by dividing the input height and width by - the stride specified during initialization. - """ - assert (self._keypoint_weights_for_center is not None and - self._keypoint_class_id is not None and - self._keypoint_indices is not None) - out_height = tf.cast(tf.maximum(height // self._stride, 1), tf.float32) - out_width = tf.cast(tf.maximum(width // self._stride, 1), tf.float32) - # Compute the yx-grid to be used to generate the heatmap. Each returned - # tensor has shape of [out_height, out_width] - (y_grid, x_grid) = ta_utils.image_shape_to_grids(out_height, out_width) - - heatmaps = [] - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_classes_list) - if gt_keypoints_weights_list is None: - gt_keypoints_weights_list = [None] * len(gt_keypoints_list) - - for keypoints, classes, kp_weights, weights in zip( - gt_keypoints_list, gt_classes_list, gt_keypoints_weights_list, - gt_weights_list): - - keypoints_absolute, kp_weights = _preprocess_keypoints_and_weights( - out_height=out_height, - out_width=out_width, - keypoints=keypoints, - class_onehot=classes, - class_weights=weights, - keypoint_weights=kp_weights, - class_id=self._keypoint_class_id, - keypoint_indices=self._keypoint_indices) - # _, num_keypoints, _ = ( - # shape_utils.combined_static_and_dynamic_shape(keypoints_absolute)) - - # Update the keypoint weights by the specified keypoints weights. - kp_loc_weights = tf.constant( - self._keypoint_weights_for_center, dtype=tf.float32) - updated_kp_weights = kp_weights * kp_loc_weights[tf.newaxis, :] - - # Obtain the sum of the weights for each instance. - # instance_weight_sum has shape: [num_instance]. - instance_weight_sum = tf.reduce_sum(updated_kp_weights, axis=1) - - # Weight the keypoint coordinates by updated_kp_weights. - # weighted_keypoints has shape: [num_instance, num_keypoints, 2] - weighted_keypoints = keypoints_absolute * tf.expand_dims( - updated_kp_weights, axis=2) - - # Compute the mean of the keypoint coordinates over the weighted - # keypoints. - # keypoint_mean has shape: [num_instance, 2] - keypoint_mean = tf.math.divide( - tf.reduce_sum(weighted_keypoints, axis=1), - tf.expand_dims(instance_weight_sum, axis=-1)) - - # Replace the NaN values (due to divided by zeros in the above operation) - # by 0.0 where the sum of instance weight is zero. - # keypoint_mean has shape: [num_instance, 2] - keypoint_mean = tf.where( - tf.stack([instance_weight_sum, instance_weight_sum], axis=1) > 0.0, - keypoint_mean, tf.zeros_like(keypoint_mean)) - - # Compute the distance from each keypoint to the mean location using - # broadcasting and weighted by updated_kp_weights. - # keypoint_dist has shape: [num_instance, num_keypoints] - keypoint_mean = tf.expand_dims(keypoint_mean, axis=1) - keypoint_dist = tf.math.sqrt( - tf.reduce_sum( - tf.math.square(keypoints_absolute - keypoint_mean), axis=2)) - keypoint_dist = keypoint_dist * updated_kp_weights - - # Compute the average of the distances from each keypoint to the mean - # location and update the average value by zero when the instance weight - # is zero. - # avg_radius has shape: [num_instance] - avg_radius = tf.math.divide( - tf.reduce_sum(keypoint_dist, axis=1), instance_weight_sum) - avg_radius = tf.where( - instance_weight_sum > 0.0, avg_radius, tf.zeros_like(avg_radius)) - - # Update the class instance weight. If the instance doesn't contain enough - # valid keypoint values (i.e. instance_weight_sum == 0.0), then set the - # instance weight to zero. - # updated_class_weights has shape: [num_instance] - updated_class_weights = tf.where( - instance_weight_sum > 0.0, weights, tf.zeros_like(weights)) - - # Compute the sigma from average distance. We use 2 * average distance to - # to approximate the width/height of the bounding box. - # sigma has shape: [num_instances]. - sigma = _compute_std_dev_from_box_size(2 * avg_radius, 2 * avg_radius, - self._min_overlap) - - # Apply the Gaussian kernel to the center coordinates. Returned heatmap - # has shape of [out_height, out_width, num_classes] - heatmap = ta_utils.coordinates_to_heatmap( - y_grid=y_grid, - x_grid=x_grid, - y_coordinates=keypoint_mean[:, 0, 0], - x_coordinates=keypoint_mean[:, 0, 1], - sigma=sigma, - channel_onehot=classes, - channel_weights=updated_class_weights, - sparse=self._compute_heatmap_sparse) - heatmaps.append(heatmap) - - # Return the stacked heatmaps over the batch. - return tf.stack(heatmaps, axis=0) - - -class CenterNetBoxTargetAssigner(object): - """Wrapper to compute target tensors for the object detection task. - - This class has methods that take as input a batch of ground truth tensors - (in the form of a list) and return the targets required to train the object - detection task. - """ - - def __init__(self, stride): - """Initializes the target assigner. - - Args: - stride: int, the stride of the network in output pixels. - """ - - self._stride = stride - - def assign_size_and_offset_targets(self, - height, - width, - gt_boxes_list, - gt_weights_list=None, - maximum_normalized_coordinate=1.1): - """Returns the box height/width and center offset targets and their indices. - - The returned values are expected to be used with predicted tensors - of size (batch_size, height//self._stride, width//self._stride, 2). The - predicted values at the relevant indices can be retrieved with the - get_batch_predictions_from_indices function. - - Args: - height: int, height of input to the model. This is used to determine the - height of the output. - width: int, width of the input to the model. This is used to determine the - width of the output. - gt_boxes_list: A list of float tensors with shape [num_boxes, 4] - representing the groundtruth detection bounding boxes for each sample in - the batch. The coordinates are expected in normalized coordinates. - gt_weights_list: A list of tensors with shape [num_boxes] corresponding to - the weight of each groundtruth detection box. - maximum_normalized_coordinate: Maximum coordinate value to be considered - as normalized, default to 1.1. This is used to check bounds during - converting normalized coordinates to absolute coordinates. - - Returns: - batch_indices: an integer tensor of shape [num_boxes, 3] holding the - indices inside the predicted tensor which should be penalized. The - first column indicates the index along the batch dimension and the - second and third columns indicate the index along the y and x - dimensions respectively. - batch_box_height_width: a float tensor of shape [num_boxes, 2] holding - expected height and width of each box in the output space. - batch_offsets: a float tensor of shape [num_boxes, 2] holding the - expected y and x offset of each box in the output space. - batch_weights: a float tensor of shape [num_boxes] indicating the - weight of each prediction. - """ - - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_boxes_list) - - batch_indices = [] - batch_box_height_width = [] - batch_weights = [] - batch_offsets = [] - - for i, (boxes, weights) in enumerate(zip(gt_boxes_list, gt_weights_list)): - boxes = box_list.BoxList(boxes) - boxes = box_list_ops.to_absolute_coordinates( - boxes, - tf.maximum(height // self._stride, 1), - tf.maximum(width // self._stride, 1), - maximum_normalized_coordinate=maximum_normalized_coordinate) - # Get the box center coordinates. Each returned tensors have the shape of - # [num_boxes] - (y_center, x_center, boxes_height, - boxes_width) = boxes.get_center_coordinates_and_sizes() - num_boxes = tf.shape(x_center) - - # Compute the offsets and indices of the box centers. Shape: - # offsets: [num_boxes, 2] - # indices: [num_boxes, 2] - (offsets, indices) = ta_utils.compute_floor_offsets_with_indices( - y_source=y_center, x_source=x_center) - - # Assign ones if weights are not provided. - if weights is None: - weights = tf.ones(num_boxes, dtype=tf.float32) - - # Shape of [num_boxes, 1] integer tensor filled with current batch index. - batch_index = i * tf.ones_like(indices[:, 0:1], dtype=tf.int32) - batch_indices.append(tf.concat([batch_index, indices], axis=1)) - batch_box_height_width.append( - tf.stack([boxes_height, boxes_width], axis=1)) - batch_weights.append(weights) - batch_offsets.append(offsets) - - batch_indices = tf.concat(batch_indices, axis=0) - batch_box_height_width = tf.concat(batch_box_height_width, axis=0) - batch_weights = tf.concat(batch_weights, axis=0) - batch_offsets = tf.concat(batch_offsets, axis=0) - return (batch_indices, batch_box_height_width, batch_offsets, batch_weights) - - -# TODO(yuhuic): Update this class to handle the instance/keypoint weights. -# Currently those weights are used as "mask" to indicate whether an -# instance/keypoint should be considered or not (expecting only either 0 or 1 -# value). In reality, the weights can be any value and this class should handle -# those values properly. -class CenterNetKeypointTargetAssigner(object): - """Wrapper to compute target tensors for the CenterNet keypoint estimation. - - This class has methods that take as input a batch of groundtruth tensors - (in the form of a list) and returns the targets required to train the - CenterNet model for keypoint estimation. Specifically, the class methods - expect the groundtruth in the following formats (consistent with the - standard Object Detection API). Note that usually the groundtruth tensors are - packed with a list which represents the batch dimension: - - gt_classes_list: [Required] a list of 2D tf.float32 one-hot - (or k-hot) tensors of shape [num_instances, num_classes] containing the - class targets with the 0th index assumed to map to the first non-background - class. - gt_keypoints_list: [Required] a list of 3D tf.float32 tensors of - shape [num_instances, num_total_keypoints, 2] containing keypoint - coordinates. Note that the "num_total_keypoints" should be the sum of the - num_keypoints over all possible keypoint types, e.g. human pose, face. - For example, if a dataset contains both 17 human pose keypoints and 5 face - keypoints, then num_total_keypoints = 17 + 5 = 22. - If an intance contains only a subet of keypoints (e.g. human pose keypoints - but not face keypoints), the face keypoints will be filled with zeros. - Also note that keypoints are assumed to be provided in normalized - coordinates and missing keypoints should be encoded as NaN. - gt_keypoints_weights_list: [Optional] a list 3D tf.float32 tensors of shape - [num_instances, num_total_keypoints] representing the weights of each - keypoints. If not provided, then all not NaN keypoints will be equally - weighted. - gt_boxes_list: [Optional] a list of 2D tf.float32 tensors of shape - [num_instances, 4] containing coordinates of the groundtruth boxes. - Groundtruth boxes are provided in [y_min, x_min, y_max, x_max] format and - assumed to be normalized and clipped relative to the image window with - y_min <= y_max and x_min <= x_max. - Note that the boxes are only used to compute the center targets but are not - considered as required output of the keypoint task. If the boxes were not - provided, the center targets will be inferred from the keypoints - [not implemented yet]. - gt_weights_list: [Optional] A list of 1D tf.float32 tensors of shape - [num_instances] containing weights for groundtruth boxes. Only useful when - gt_boxes_list is also provided. - """ - - def __init__(self, - stride, - class_id, - keypoint_indices, - keypoint_std_dev=None, - per_keypoint_offset=False, - peak_radius=0, - compute_heatmap_sparse=False, - per_keypoint_depth=False): - """Initializes a CenterNet keypoints target assigner. - - Args: - stride: int, the stride of the network in output pixels. - class_id: int, the ID of the class (0-indexed) that contains the target - keypoints to consider in this task. For example, if the task is human - pose estimation, the class id should correspond to the "human" class. - keypoint_indices: A list of integers representing the indices of the - keypoints to be considered in this task. This is used to retrieve the - subset of the keypoints from gt_keypoints that should be considered in - this task. - keypoint_std_dev: A list of floats represent the standard deviation of the - Gaussian kernel used to generate the keypoint heatmap (in the unit of - output pixels). It is to provide the flexibility of using different - sizes of Gaussian kernel for each keypoint type. If not provided, then - all standard deviation will be the same as the default value (10.0 in - the output pixel space). If provided, the length of keypoint_std_dev - needs to be the same as the length of keypoint_indices, indicating the - standard deviation of each keypoint type. - per_keypoint_offset: boolean, indicating whether to assign offset for - each keypoint channel. If set False, the output offset target will have - the shape [batch_size, out_height, out_width, 2]. If set True, the - output offset target will have the shape [batch_size, out_height, - out_width, 2 * num_keypoints]. - peak_radius: int, the radius (in the unit of output pixel) around heatmap - peak to assign the offset targets. - compute_heatmap_sparse: bool, indicating whether or not to use the sparse - version of the Op that computes the heatmap. The sparse version scales - better with number of keypoint types, but in some cases is known to - cause an OOM error. See (b/170989061). - per_keypoint_depth: A bool indicates whether the model predicts the depth - of each keypoints in independent channels. Similar to - per_keypoint_offset but for the keypoint depth. - """ - - self._stride = stride - self._class_id = class_id - self._keypoint_indices = keypoint_indices - self._per_keypoint_offset = per_keypoint_offset - self._per_keypoint_depth = per_keypoint_depth - self._peak_radius = peak_radius - self._compute_heatmap_sparse = compute_heatmap_sparse - if keypoint_std_dev is None: - self._keypoint_std_dev = ([_DEFAULT_KEYPOINT_OFFSET_STD_DEV] * - len(keypoint_indices)) - else: - assert len(keypoint_indices) == len(keypoint_std_dev) - self._keypoint_std_dev = keypoint_std_dev - - def assign_keypoint_heatmap_targets(self, - height, - width, - gt_keypoints_list, - gt_classes_list, - gt_keypoints_weights_list=None, - gt_weights_list=None, - gt_boxes_list=None): - """Returns the keypoint heatmap targets for the CenterNet model. - - Args: - height: int, height of input to the CenterNet model. This is used to - determine the height of the output. - width: int, width of the input to the CenterNet model. This is used to - determine the width of the output. - gt_keypoints_list: A list of float tensors with shape [num_instances, - num_total_keypoints, 2]. See class-level description for more detail. - gt_classes_list: A list of float tensors with shape [num_instances, - num_classes]. See class-level description for more detail. - gt_keypoints_weights_list: A list of tensors with shape [num_instances, - num_total_keypoints] corresponding to the weight of each keypoint. - gt_weights_list: A list of float tensors with shape [num_instances]. See - class-level description for more detail. - gt_boxes_list: A list of float tensors with shape [num_instances, 4]. See - class-level description for more detail. If provided, the keypoint - standard deviations will be scaled based on the box sizes. - - Returns: - heatmap: A float tensor of shape [batch_size, output_height, output_width, - num_keypoints] representing the per keypoint type center heatmap. - output_height and output_width are computed by dividing the input height - and width by the stride specified during initialization. Note that the - "num_keypoints" is defined by the length of keypoint_indices, which is - not necessarily equal to "num_total_keypoints". - num_instances_batch: A 2D int tensor of shape - [batch_size, num_keypoints] representing number of instances for each - keypoint type. - valid_mask: A float tensor with shape [batch_size, output_height, - output_width, num_keypoints] where all values within the regions of the - blackout boxes are 0.0 and 1.0 else where. Note that the blackout boxes - are per keypoint type and are blacked out if the keypoint - visibility/weight (of the corresponding keypoint type) is zero. - """ - out_width = tf.cast(tf.maximum(width // self._stride, 1), tf.float32) - out_height = tf.cast(tf.maximum(height // self._stride, 1), tf.float32) - # Compute the yx-grid to be used to generate the heatmap. Each returned - # tensor has shape of [out_height, out_width] - y_grid, x_grid = ta_utils.image_shape_to_grids(out_height, out_width) - - if gt_keypoints_weights_list is None: - gt_keypoints_weights_list = [None] * len(gt_keypoints_list) - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_classes_list) - if gt_boxes_list is None: - gt_boxes_list = [None] * len(gt_keypoints_list) - - heatmaps = [] - num_instances_list = [] - valid_mask_list = [] - for keypoints, classes, kp_weights, weights, boxes in zip( - gt_keypoints_list, gt_classes_list, gt_keypoints_weights_list, - gt_weights_list, gt_boxes_list): - keypoints_absolute, kp_weights = _preprocess_keypoints_and_weights( - out_height=out_height, - out_width=out_width, - keypoints=keypoints, - class_onehot=classes, - class_weights=weights, - keypoint_weights=kp_weights, - class_id=self._class_id, - keypoint_indices=self._keypoint_indices) - num_instances, num_keypoints, _ = ( - shape_utils.combined_static_and_dynamic_shape(keypoints_absolute)) - - # A tensor of shape [num_instances, num_keypoints] with - # each element representing the type dimension for each corresponding - # keypoint: - # [[0, 1, ..., k-1], - # [0, 1, ..., k-1], - # : - # [0, 1, ..., k-1]] - keypoint_types = tf.tile( - input=tf.expand_dims(tf.range(num_keypoints), axis=0), - multiples=[num_instances, 1]) - - # A tensor of shape [num_instances, num_keypoints] with - # each element representing the sigma of the Gaussian kernel for each - # keypoint. - keypoint_std_dev = tf.tile( - input=tf.expand_dims(tf.constant(self._keypoint_std_dev), axis=0), - multiples=[num_instances, 1]) - - # If boxes is not None, then scale the standard deviation based on the - # size of the object bounding boxes similar to object center heatmap. - if boxes is not None: - boxes = box_list.BoxList(boxes) - # Convert the box coordinates to absolute output image dimension space. - boxes = box_list_ops.to_absolute_coordinates( - boxes, - tf.maximum(height // self._stride, 1), - tf.maximum(width // self._stride, 1)) - # Get the box height and width. Each returned tensors have the shape - # of [num_instances] - (_, _, boxes_height, - boxes_width) = boxes.get_center_coordinates_and_sizes() - - # Compute the sigma from box size. The tensor shape: [num_instances]. - sigma = _compute_std_dev_from_box_size(boxes_height, boxes_width, 0.7) - keypoint_std_dev = keypoint_std_dev * tf.stack( - [sigma] * num_keypoints, axis=1) - - # Generate the per-keypoint type valid region mask to ignore regions - # with keypoint weights equal to zeros (e.g. visibility is 0). - # shape of valid_mask: [out_height, out_width, num_keypoints] - kp_weight_list = tf.unstack(kp_weights, axis=1) - valid_mask_channel_list = [] - for kp_weight in kp_weight_list: - blackout = kp_weight < 1e-3 - valid_mask_channel_list.append( - ta_utils.blackout_pixel_weights_by_box_regions( - out_height, out_width, boxes.get(), blackout)) - valid_mask = tf.stack(valid_mask_channel_list, axis=2) - valid_mask_list.append(valid_mask) - - # Apply the Gaussian kernel to the keypoint coordinates. Returned heatmap - # has shape of [out_height, out_width, num_keypoints]. - heatmap = ta_utils.coordinates_to_heatmap( - y_grid=y_grid, - x_grid=x_grid, - y_coordinates=tf.keras.backend.flatten(keypoints_absolute[:, :, 0]), - x_coordinates=tf.keras.backend.flatten(keypoints_absolute[:, :, 1]), - sigma=tf.keras.backend.flatten(keypoint_std_dev), - channel_onehot=tf.one_hot( - tf.keras.backend.flatten(keypoint_types), depth=num_keypoints), - channel_weights=tf.keras.backend.flatten(kp_weights)) - num_instances_list.append( - tf.cast(tf.reduce_sum(kp_weights, axis=0), dtype=tf.int32)) - heatmaps.append(heatmap) - return (tf.stack(heatmaps, axis=0), tf.stack(num_instances_list, axis=0), - tf.stack(valid_mask_list, axis=0)) - - def _get_keypoint_types(self, num_instances, num_keypoints, num_neighbors): - """Gets keypoint type index tensor. - - The function prepares the tensor of keypoint indices with shape - [num_instances, num_keypoints, num_neighbors]. Each element represents the - keypoint type index for each corresponding keypoint and tiled along the 3rd - axis: - [[0, 1, ..., num_keypoints - 1], - [0, 1, ..., num_keypoints - 1], - : - [0, 1, ..., num_keypoints - 1]] - - Args: - num_instances: int, the number of instances, used to define the 1st - dimension. - num_keypoints: int, the number of keypoint types, used to define the 2nd - dimension. - num_neighbors: int, the number of neighborhood pixels to consider for each - keypoint, used to define the 3rd dimension. - - Returns: - A integer tensor of shape [num_instances, num_keypoints, num_neighbors]. - """ - keypoint_types = tf.range(num_keypoints)[tf.newaxis, :, tf.newaxis] - tiled_keypoint_types = tf.tile(keypoint_types, - multiples=[num_instances, 1, num_neighbors]) - return tiled_keypoint_types - - def assign_keypoints_offset_targets(self, - height, - width, - gt_keypoints_list, - gt_classes_list, - gt_keypoints_weights_list=None, - gt_weights_list=None): - """Returns the offsets and indices of the keypoints for location refinement. - - The returned values are used to refine the location of each keypoints in the - heatmap. The predicted values at the relevant indices can be retrieved with - the get_batch_predictions_from_indices function. - - Args: - height: int, height of input to the CenterNet model. This is used to - determine the height of the output. - width: int, width of the input to the CenterNet model. This is used to - determine the width of the output. - gt_keypoints_list: A list of tensors with shape [num_instances, - num_total_keypoints]. See class-level description for more detail. - gt_classes_list: A list of tensors with shape [num_instances, - num_classes]. See class-level description for more detail. - gt_keypoints_weights_list: A list of tensors with shape [num_instances, - num_total_keypoints] corresponding to the weight of each keypoint. - gt_weights_list: A list of float tensors with shape [num_instances]. See - class-level description for more detail. - - Returns: - batch_indices: an integer tensor of shape [num_total_instances, 3] (or - [num_total_instances, 4] if 'per_keypoint_offset' is set True) holding - the indices inside the predicted tensor which should be penalized. The - first column indicates the index along the batch dimension and the - second and third columns indicate the index along the y and x - dimensions respectively. The fourth column corresponds to the channel - dimension (if 'per_keypoint_offset' is set True). - batch_offsets: a float tensor of shape [num_total_instances, 2] holding - the expected y and x offset of each box in the output space. - batch_weights: a float tensor of shape [num_total_instances] indicating - the weight of each prediction. - Note that num_total_instances = batch_size * num_instances * - num_keypoints * num_neighbors - """ - - batch_indices = [] - batch_offsets = [] - batch_weights = [] - - if gt_keypoints_weights_list is None: - gt_keypoints_weights_list = [None] * len(gt_keypoints_list) - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_classes_list) - for i, (keypoints, classes, kp_weights, weights) in enumerate( - zip(gt_keypoints_list, gt_classes_list, gt_keypoints_weights_list, - gt_weights_list)): - keypoints_absolute, kp_weights = _preprocess_keypoints_and_weights( - out_height=tf.maximum(height // self._stride, 1), - out_width=tf.maximum(width // self._stride, 1), - keypoints=keypoints, - class_onehot=classes, - class_weights=weights, - keypoint_weights=kp_weights, - class_id=self._class_id, - keypoint_indices=self._keypoint_indices) - num_instances, num_keypoints, _ = ( - shape_utils.combined_static_and_dynamic_shape(keypoints_absolute)) - - # [num_instances * num_keypoints] - y_source = tf.keras.backend.flatten(keypoints_absolute[:, :, 0]) - x_source = tf.keras.backend.flatten(keypoints_absolute[:, :, 1]) - - # All keypoint coordinates and their neighbors: - # [num_instance * num_keypoints, num_neighbors] - (y_source_neighbors, x_source_neighbors, - valid_sources) = ta_utils.get_surrounding_grids( - tf.cast(tf.maximum(height // self._stride, 1), tf.float32), - tf.cast(tf.maximum(width // self._stride, 1), tf.float32), - y_source, x_source, - self._peak_radius) - _, num_neighbors = shape_utils.combined_static_and_dynamic_shape( - y_source_neighbors) - - # Update the valid keypoint weights. - # [num_instance * num_keypoints, num_neighbors] - valid_keypoints = tf.cast( - valid_sources, dtype=tf.float32) * tf.stack( - [tf.keras.backend.flatten(kp_weights)] * num_neighbors, axis=-1) - - # Compute the offsets and indices of the box centers. Shape: - # offsets: [num_instances * num_keypoints, num_neighbors, 2] - # indices: [num_instances * num_keypoints, num_neighbors, 2] - offsets, indices = ta_utils.compute_floor_offsets_with_indices( - y_source=y_source_neighbors, - x_source=x_source_neighbors, - y_target=y_source, - x_target=x_source) - # Reshape to: - # offsets: [num_instances * num_keypoints * num_neighbors, 2] - # indices: [num_instances * num_keypoints * num_neighbors, 2] - offsets = tf.reshape(offsets, [-1, 2]) - indices = tf.reshape(indices, [-1, 2]) - - # Prepare the batch indices to be prepended. - batch_index = tf.fill( - [num_instances * num_keypoints * num_neighbors, 1], i) - if self._per_keypoint_offset: - tiled_keypoint_types = self._get_keypoint_types( - num_instances, num_keypoints, num_neighbors) - batch_indices.append( - tf.concat([batch_index, indices, - tf.reshape(tiled_keypoint_types, [-1, 1])], axis=1)) - else: - batch_indices.append(tf.concat([batch_index, indices], axis=1)) - batch_offsets.append(offsets) - batch_weights.append(tf.keras.backend.flatten(valid_keypoints)) - - # Concatenate the tensors in the batch in the first dimension: - # shape: [batch_size * num_instances * num_keypoints * num_neighbors, 3] or - # [batch_size * num_instances * num_keypoints * num_neighbors, 4] if - # 'per_keypoint_offset' is set to True. - batch_indices = tf.concat(batch_indices, axis=0) - # shape: [batch_size * num_instances * num_keypoints * num_neighbors] - batch_weights = tf.concat(batch_weights, axis=0) - # shape: [batch_size * num_instances * num_keypoints * num_neighbors, 2] - batch_offsets = tf.concat(batch_offsets, axis=0) - return (batch_indices, batch_offsets, batch_weights) - - def assign_keypoints_depth_targets(self, - height, - width, - gt_keypoints_list, - gt_classes_list, - gt_keypoint_depths_list, - gt_keypoint_depth_weights_list, - gt_keypoints_weights_list=None, - gt_weights_list=None): - """Returns the target depths of the keypoints. - - The returned values are the relative depth information of each keypoints. - - Args: - height: int, height of input to the CenterNet model. This is used to - determine the height of the output. - width: int, width of the input to the CenterNet model. This is used to - determine the width of the output. - gt_keypoints_list: A list of tensors with shape [num_instances, - num_total_keypoints, 2]. See class-level description for more detail. - gt_classes_list: A list of tensors with shape [num_instances, - num_classes]. See class-level description for more detail. - gt_keypoint_depths_list: A list of tensors with shape [num_instances, - num_total_keypoints] corresponding to the relative depth of the - keypoints. - gt_keypoint_depth_weights_list: A list of tensors with shape - [num_instances, num_total_keypoints] corresponding to the weights of - the relative depth. - gt_keypoints_weights_list: A list of tensors with shape [num_instances, - num_total_keypoints] corresponding to the weight of each keypoint. - gt_weights_list: A list of float tensors with shape [num_instances]. See - class-level description for more detail. - - Returns: - batch_indices: an integer tensor of shape [num_total_instances, 3] (or - [num_total_instances, 4] if 'per_keypoint_depth' is set True) holding - the indices inside the predicted tensor which should be penalized. The - first column indicates the index along the batch dimension and the - second and third columns indicate the index along the y and x - dimensions respectively. The fourth column corresponds to the channel - dimension (if 'per_keypoint_offset' is set True). - batch_depths: a float tensor of shape [num_total_instances, 1] (or - [num_total_instances, num_keypoints] if per_keypoint_depth is set True) - indicating the target depth of each keypoint. - batch_weights: a float tensor of shape [num_total_instances] indicating - the weight of each prediction. - Note that num_total_instances = batch_size * num_instances * - num_keypoints * num_neighbors - """ - - batch_indices = [] - batch_weights = [] - batch_depths = [] - - if gt_keypoints_weights_list is None: - gt_keypoints_weights_list = [None] * len(gt_keypoints_list) - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_classes_list) - if gt_keypoint_depths_list is None: - gt_keypoint_depths_list = [None] * len(gt_classes_list) - for i, (keypoints, classes, kp_weights, weights, - keypoint_depths, keypoint_depth_weights) in enumerate( - zip(gt_keypoints_list, gt_classes_list, - gt_keypoints_weights_list, gt_weights_list, - gt_keypoint_depths_list, gt_keypoint_depth_weights_list)): - keypoints_absolute, kp_weights = _preprocess_keypoints_and_weights( - out_height=tf.maximum(height // self._stride, 1), - out_width=tf.maximum(width // self._stride, 1), - keypoints=keypoints, - class_onehot=classes, - class_weights=weights, - keypoint_weights=kp_weights, - class_id=self._class_id, - keypoint_indices=self._keypoint_indices) - num_instances, num_keypoints, _ = ( - shape_utils.combined_static_and_dynamic_shape(keypoints_absolute)) - - # [num_instances * num_keypoints] - y_source = tf.keras.backend.flatten(keypoints_absolute[:, :, 0]) - x_source = tf.keras.backend.flatten(keypoints_absolute[:, :, 1]) - - # All keypoint coordinates and their neighbors: - # [num_instance * num_keypoints, num_neighbors] - (y_source_neighbors, x_source_neighbors, - valid_sources) = ta_utils.get_surrounding_grids( - tf.cast(tf.maximum(height // self._stride, 1), tf.float32), - tf.cast(tf.maximum(width // self._stride, 1), tf.float32), - y_source, x_source, - self._peak_radius) - _, num_neighbors = shape_utils.combined_static_and_dynamic_shape( - y_source_neighbors) - - # Update the valid keypoint weights. - # [num_instance * num_keypoints, num_neighbors] - valid_keypoints = tf.cast( - valid_sources, dtype=tf.float32) * tf.stack( - [tf.keras.backend.flatten(kp_weights)] * num_neighbors, axis=-1) - - # Compute the offsets and indices of the box centers. Shape: - # indices: [num_instances * num_keypoints, num_neighbors, 2] - _, indices = ta_utils.compute_floor_offsets_with_indices( - y_source=y_source_neighbors, - x_source=x_source_neighbors, - y_target=y_source, - x_target=x_source) - # Reshape to: - # indices: [num_instances * num_keypoints * num_neighbors, 2] - indices = tf.reshape(indices, [-1, 2]) - - # Gather the keypoint depth from corresponding keypoint indices: - # [num_instances, num_keypoints] - keypoint_depths = tf.gather( - keypoint_depths, self._keypoint_indices, axis=1) - # Tile the depth target to surrounding pixels. - # [num_instances, num_keypoints, num_neighbors] - tiled_keypoint_depths = tf.tile( - tf.expand_dims(keypoint_depths, axis=-1), - multiples=[1, 1, num_neighbors]) - - # [num_instances, num_keypoints] - keypoint_depth_weights = tf.gather( - keypoint_depth_weights, self._keypoint_indices, axis=1) - # [num_instances, num_keypoints, num_neighbors] - keypoint_depth_weights = tf.tile( - tf.expand_dims(keypoint_depth_weights, axis=-1), - multiples=[1, 1, num_neighbors]) - # Update the weights of keypoint depth by the weights of the keypoints. - # A keypoint depth target is valid only if its corresponding keypoint - # target is also valid. - # [num_instances, num_keypoints, num_neighbors] - tiled_depth_weights = ( - tf.reshape(valid_keypoints, - [num_instances, num_keypoints, num_neighbors]) * - keypoint_depth_weights) - invalid_depths = tf.logical_or( - tf.math.is_nan(tiled_depth_weights), - tf.math.is_nan(tiled_keypoint_depths)) - # Assign zero values and weights to NaN values. - final_keypoint_depths = tf.where(invalid_depths, - tf.zeros_like(tiled_keypoint_depths), - tiled_keypoint_depths) - final_keypoint_depth_weights = tf.where( - invalid_depths, - tf.zeros_like(tiled_depth_weights), - tiled_depth_weights) - # [num_instances * num_keypoints * num_neighbors, 1] - batch_depths.append(tf.reshape(final_keypoint_depths, [-1, 1])) - - # Prepare the batch indices to be prepended. - batch_index = tf.fill( - [num_instances * num_keypoints * num_neighbors, 1], i) - if self._per_keypoint_depth: - tiled_keypoint_types = self._get_keypoint_types( - num_instances, num_keypoints, num_neighbors) - batch_indices.append( - tf.concat([batch_index, indices, - tf.reshape(tiled_keypoint_types, [-1, 1])], axis=1)) - else: - batch_indices.append(tf.concat([batch_index, indices], axis=1)) - batch_weights.append( - tf.keras.backend.flatten(final_keypoint_depth_weights)) - - # Concatenate the tensors in the batch in the first dimension: - # shape: [batch_size * num_instances * num_keypoints * num_neighbors, 3] or - # [batch_size * num_instances * num_keypoints * num_neighbors, 4] if - # 'per_keypoint_offset' is set to True. - batch_indices = tf.concat(batch_indices, axis=0) - # shape: [batch_size * num_instances * num_keypoints * num_neighbors] - batch_weights = tf.concat(batch_weights, axis=0) - # shape: [batch_size * num_instances * num_keypoints * num_neighbors, 1] - batch_depths = tf.concat(batch_depths, axis=0) - return (batch_indices, batch_depths, batch_weights) - - def assign_joint_regression_targets(self, - height, - width, - gt_keypoints_list, - gt_classes_list, - gt_boxes_list=None, - gt_keypoints_weights_list=None, - gt_weights_list=None): - """Returns the joint regression from center grid to keypoints. - - The joint regression is used as the grouping cue from the estimated - keypoints to instance center. The offsets are the vectors from the floored - object center coordinates to the keypoint coordinates. - - Args: - height: int, height of input to the CenterNet model. This is used to - determine the height of the output. - width: int, width of the input to the CenterNet model. This is used to - determine the width of the output. - gt_keypoints_list: A list of float tensors with shape [num_instances, - num_total_keypoints]. See class-level description for more detail. - gt_classes_list: A list of float tensors with shape [num_instances, - num_classes]. See class-level description for more detail. - gt_boxes_list: A list of float tensors with shape [num_instances, 4]. See - class-level description for more detail. If provided, then the center - targets will be computed based on the center of the boxes. - gt_keypoints_weights_list: A list of float tensors with shape - [num_instances, num_total_keypoints] representing to the weight of each - keypoint. - gt_weights_list: A list of float tensors with shape [num_instances]. See - class-level description for more detail. - - Returns: - batch_indices: an integer tensor of shape [num_instances, 4] holding the - indices inside the predicted tensor which should be penalized. The - first column indicates the index along the batch dimension and the - second and third columns indicate the index along the y and x - dimensions respectively, the last dimension refers to the keypoint type - dimension. - batch_offsets: a float tensor of shape [num_instances, 2] holding the - expected y and x offset of each box in the output space. - batch_weights: a float tensor of shape [num_instances] indicating the - weight of each prediction. - Note that num_total_instances = batch_size * num_instances * num_keypoints - - Raises: - NotImplementedError: currently the object center coordinates need to be - computed from groundtruth bounding boxes. The functionality of - generating the object center coordinates from keypoints is not - implemented yet. - """ - - batch_indices = [] - batch_offsets = [] - batch_weights = [] - batch_size = len(gt_keypoints_list) - if gt_keypoints_weights_list is None: - gt_keypoints_weights_list = [None] * batch_size - if gt_boxes_list is None: - gt_boxes_list = [None] * batch_size - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_classes_list) - for i, (keypoints, classes, boxes, kp_weights, weights) in enumerate( - zip(gt_keypoints_list, gt_classes_list, - gt_boxes_list, gt_keypoints_weights_list, gt_weights_list)): - keypoints_absolute, kp_weights = _preprocess_keypoints_and_weights( - out_height=tf.maximum(height // self._stride, 1), - out_width=tf.maximum(width // self._stride, 1), - keypoints=keypoints, - class_onehot=classes, - class_weights=weights, - keypoint_weights=kp_weights, - class_id=self._class_id, - keypoint_indices=self._keypoint_indices) - num_instances, num_keypoints, _ = ( - shape_utils.combined_static_and_dynamic_shape(keypoints_absolute)) - - # If boxes are provided, compute the joint center from it. - if boxes is not None: - # Compute joint center from boxes. - boxes = box_list.BoxList(boxes) - boxes = box_list_ops.to_absolute_coordinates( - boxes, - tf.maximum(height // self._stride, 1), - tf.maximum(width // self._stride, 1)) - y_center, x_center, _, _ = boxes.get_center_coordinates_and_sizes() - else: - # TODO(yuhuic): Add the logic to generate object centers from keypoints. - raise NotImplementedError(( - 'The functionality of generating object centers from keypoints is' - ' not implemented yet. Please provide groundtruth bounding boxes.' - )) - - # Tile the yx center coordinates to be the same shape as keypoints. - y_center_tiled = tf.tile( - tf.reshape(y_center, shape=[num_instances, 1]), - multiples=[1, num_keypoints]) - x_center_tiled = tf.tile( - tf.reshape(x_center, shape=[num_instances, 1]), - multiples=[1, num_keypoints]) - # [num_instance * num_keypoints, num_neighbors] - (y_source_neighbors, x_source_neighbors, - valid_sources) = ta_utils.get_surrounding_grids( - tf.cast(tf.maximum(height // self._stride, 1), tf.float32), - tf.cast(tf.maximum(width // self._stride, 1), tf.float32), - tf.keras.backend.flatten(y_center_tiled), - tf.keras.backend.flatten(x_center_tiled), self._peak_radius) - - _, num_neighbors = shape_utils.combined_static_and_dynamic_shape( - y_source_neighbors) - valid_keypoints = tf.cast( - valid_sources, dtype=tf.float32) * tf.stack( - [tf.keras.backend.flatten(kp_weights)] * num_neighbors, axis=-1) - - # Compute the offsets and indices of the box centers. Shape: - # offsets: [num_instances * num_keypoints, 2] - # indices: [num_instances * num_keypoints, 2] - (offsets, indices) = ta_utils.compute_floor_offsets_with_indices( - y_source=y_source_neighbors, - x_source=x_source_neighbors, - y_target=tf.keras.backend.flatten(keypoints_absolute[:, :, 0]), - x_target=tf.keras.backend.flatten(keypoints_absolute[:, :, 1])) - # Reshape to: - # offsets: [num_instances * num_keypoints * num_neighbors, 2] - # indices: [num_instances * num_keypoints * num_neighbors, 2] - offsets = tf.reshape(offsets, [-1, 2]) - indices = tf.reshape(indices, [-1, 2]) - - # keypoint type tensor: [num_instances, num_keypoints, num_neighbors]. - tiled_keypoint_types = self._get_keypoint_types( - num_instances, num_keypoints, num_neighbors) - - batch_index = tf.fill( - [num_instances * num_keypoints * num_neighbors, 1], i) - batch_indices.append( - tf.concat([batch_index, indices, - tf.reshape(tiled_keypoint_types, [-1, 1])], axis=1)) - batch_offsets.append(offsets) - batch_weights.append(tf.keras.backend.flatten(valid_keypoints)) - - # Concatenate the tensors in the batch in the first dimension: - # shape: [batch_size * num_instances * num_keypoints, 4] - batch_indices = tf.concat(batch_indices, axis=0) - # shape: [batch_size * num_instances * num_keypoints] - batch_weights = tf.concat(batch_weights, axis=0) - # shape: [batch_size * num_instances * num_keypoints, 2] - batch_offsets = tf.concat(batch_offsets, axis=0) - return (batch_indices, batch_offsets, batch_weights) - - -def _resize_masks(masks, height, width, method): - # Resize segmentation masks to conform to output dimensions. Use TF2 - # image resize because TF1's version is buggy: - # https://yaqs.corp.google.com/eng/q/4970450458378240 - masks = tf2.image.resize( - masks[:, :, :, tf.newaxis], - size=(height, width), - method=method) - return masks[:, :, :, 0] - - -class CenterNetMaskTargetAssigner(object): - """Wrapper to compute targets for segmentation masks.""" - - def __init__(self, stride, boxes_scale=1.0): - """Constructor. - - Args: - stride: The stride of the network. Targets are assigned at the output - stride. - boxes_scale: Scale to apply to boxes before producing mask weights. This - is meant to ensure the full object region is properly weighted prior to - applying loss. A value of ~1.05 is typically applied when object regions - should be blacked out (perhaps because valid groundtruth masks are not - present). - """ - self._stride = stride - self._boxes_scale = boxes_scale - - def assign_segmentation_targets( - self, gt_masks_list, gt_classes_list, gt_boxes_list=None, - gt_mask_weights_list=None, mask_resize_method=ResizeMethod.BILINEAR): - """Computes the segmentation targets. - - This utility produces a semantic segmentation mask for each class, starting - with whole image instance segmentation masks. Effectively, each per-class - segmentation target is the union of all masks from that class. - - Args: - gt_masks_list: A list of float tensors with shape [num_boxes, - input_height, input_width] with values in {0, 1} representing instance - masks for each object. - gt_classes_list: A list of float tensors with shape [num_boxes, - num_classes] representing the one-hot encoded class labels for each box - in the gt_boxes_list. - gt_boxes_list: An optional list of float tensors with shape [num_boxes, 4] - with normalized boxes corresponding to each mask. The boxes are used to - spatially allocate mask weights. - gt_mask_weights_list: An optional list of float tensors with shape - [num_boxes] with weights for each mask. If a mask has a zero weight, it - indicates that the box region associated with the mask should not - contribute to the loss. If not provided, will use a per-pixel weight of - 1. - mask_resize_method: A `tf.compat.v2.image.ResizeMethod`. The method to use - when resizing masks from input resolution to output resolution. - - - Returns: - segmentation_targets: An int32 tensor of size [batch_size, output_height, - output_width, num_classes] representing the class of each location in - the output space. - segmentation_weight: A float32 tensor of size [batch_size, output_height, - output_width] indicating the loss weight to apply at each location. - """ - _, num_classes = shape_utils.combined_static_and_dynamic_shape( - gt_classes_list[0]) - - _, input_height, input_width = ( - shape_utils.combined_static_and_dynamic_shape(gt_masks_list[0])) - output_height = tf.maximum(input_height // self._stride, 1) - output_width = tf.maximum(input_width // self._stride, 1) - - if gt_boxes_list is None: - gt_boxes_list = [None] * len(gt_masks_list) - if gt_mask_weights_list is None: - gt_mask_weights_list = [None] * len(gt_masks_list) - - segmentation_targets_list = [] - segmentation_weights_list = [] - - for gt_boxes, gt_masks, gt_mask_weights, gt_classes in zip( - gt_boxes_list, gt_masks_list, gt_mask_weights_list, gt_classes_list): - - if gt_boxes is not None and gt_mask_weights is not None: - boxes = box_list.BoxList(gt_boxes) - # Convert the box coordinates to absolute output image dimension space. - boxes_absolute = box_list_ops.to_absolute_coordinates( - boxes, output_height, output_width) - - # Generate a segmentation weight that applies mask weights in object - # regions. - blackout = gt_mask_weights <= 0 - segmentation_weight_for_image = ( - ta_utils.blackout_pixel_weights_by_box_regions( - output_height, output_width, boxes_absolute.get(), blackout, - weights=gt_mask_weights, boxes_scale=self._boxes_scale)) - segmentation_weights_list.append(segmentation_weight_for_image) - else: - segmentation_weights_list.append(tf.ones((output_height, output_width), - dtype=tf.float32)) - - gt_masks = _resize_masks(gt_masks, output_height, output_width, - mask_resize_method) - gt_masks = gt_masks[:, :, :, tf.newaxis] - gt_classes_reshaped = tf.reshape(gt_classes, [-1, 1, 1, num_classes]) - # Shape: [h, w, num_classes]. - segmentations_for_image = tf.reduce_max( - gt_masks * gt_classes_reshaped, axis=0) - # Avoid the case where max of an empty array is -inf. - segmentations_for_image = tf.maximum(segmentations_for_image, 0.0) - segmentation_targets_list.append(segmentations_for_image) - - segmentation_target = tf.stack(segmentation_targets_list, axis=0) - segmentation_weight = tf.stack(segmentation_weights_list, axis=0) - return segmentation_target, segmentation_weight - - -class CenterNetDensePoseTargetAssigner(object): - """Wrapper to compute targets for DensePose task.""" - - def __init__(self, stride, num_parts=24): - self._stride = stride - self._num_parts = num_parts - - def assign_part_and_coordinate_targets(self, - height, - width, - gt_dp_num_points_list, - gt_dp_part_ids_list, - gt_dp_surface_coords_list, - gt_weights_list=None): - """Returns the DensePose part_id and coordinate targets and their indices. - - The returned values are expected to be used with predicted tensors - of size (batch_size, height//self._stride, width//self._stride, 2). The - predicted values at the relevant indices can be retrieved with the - get_batch_predictions_from_indices function. - - Args: - height: int, height of input to the model. This is used to determine the - height of the output. - width: int, width of the input to the model. This is used to determine the - width of the output. - gt_dp_num_points_list: a list of 1-D tf.int32 tensors of shape [num_boxes] - containing the number of DensePose sampled points per box. - gt_dp_part_ids_list: a list of 2-D tf.int32 tensors of shape - [num_boxes, max_sampled_points] containing the DensePose part ids - (0-indexed) for each sampled point. Note that there may be padding, as - boxes may contain a different number of sampled points. - gt_dp_surface_coords_list: a list of 3-D tf.float32 tensors of shape - [num_boxes, max_sampled_points, 4] containing the DensePose surface - coordinates (normalized) for each sampled point. Note that there may be - padding. - gt_weights_list: A list of 1-D tensors with shape [num_boxes] - corresponding to the weight of each groundtruth detection box. - - Returns: - batch_indices: an integer tensor of shape [num_total_points, 4] holding - the indices inside the predicted tensor which should be penalized. The - first column indicates the index along the batch dimension and the - second and third columns indicate the index along the y and x - dimensions respectively. The fourth column is the part index. - batch_part_ids: an int tensor of shape [num_total_points, num_parts] - holding 1-hot encodings of parts for each sampled point. - batch_surface_coords: a float tensor of shape [num_total_points, 2] - holding the expected (v, u) coordinates for each sampled point. - batch_weights: a float tensor of shape [num_total_points] indicating the - weight of each prediction. - Note that num_total_points = batch_size * num_boxes * max_sampled_points. - """ - - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_dp_num_points_list) - - batch_indices = [] - batch_part_ids = [] - batch_surface_coords = [] - batch_weights = [] - - for i, (num_points, part_ids, surface_coords, weights) in enumerate( - zip(gt_dp_num_points_list, gt_dp_part_ids_list, - gt_dp_surface_coords_list, gt_weights_list)): - num_boxes, max_sampled_points = ( - shape_utils.combined_static_and_dynamic_shape(part_ids)) - part_ids_flattened = tf.reshape(part_ids, [-1]) - part_ids_one_hot = tf.one_hot(part_ids_flattened, depth=self._num_parts) - # Get DensePose coordinates in the output space. - surface_coords_abs = densepose_ops.to_absolute_coordinates( - surface_coords, - tf.maximum(height // self._stride, 1), - tf.maximum(width // self._stride, 1)) - surface_coords_abs = tf.reshape(surface_coords_abs, [-1, 4]) - # Each tensor has shape [num_boxes * max_sampled_points]. - yabs, xabs, v, u = tf.unstack(surface_coords_abs, axis=-1) - - # Get the indices (in output space) for the DensePose coordinates. Note - # that if self._stride is larger than 1, this will have the effect of - # reducing spatial resolution of the groundtruth points. - indices_y = tf.cast(yabs, tf.int32) - indices_x = tf.cast(xabs, tf.int32) - - # Assign ones if weights are not provided. - if weights is None: - weights = tf.ones(num_boxes, dtype=tf.float32) - # Create per-point weights. - weights_per_point = tf.reshape( - tf.tile(weights[:, tf.newaxis], multiples=[1, max_sampled_points]), - shape=[-1]) - # Mask out invalid (i.e. padded) DensePose points. - num_points_tiled = tf.tile(num_points[:, tf.newaxis], - multiples=[1, max_sampled_points]) - range_tiled = tf.tile(tf.range(max_sampled_points)[tf.newaxis, :], - multiples=[num_boxes, 1]) - valid_points = tf.math.less(range_tiled, num_points_tiled) - valid_points = tf.cast(tf.reshape(valid_points, [-1]), dtype=tf.float32) - weights_per_point = weights_per_point * valid_points - - # Shape of [num_boxes * max_sampled_points] integer tensor filled with - # current batch index. - batch_index = i * tf.ones_like(indices_y, dtype=tf.int32) - batch_indices.append( - tf.stack([batch_index, indices_y, indices_x, part_ids_flattened], - axis=1)) - batch_part_ids.append(part_ids_one_hot) - batch_surface_coords.append(tf.stack([v, u], axis=1)) - batch_weights.append(weights_per_point) - - batch_indices = tf.concat(batch_indices, axis=0) - batch_part_ids = tf.concat(batch_part_ids, axis=0) - batch_surface_coords = tf.concat(batch_surface_coords, axis=0) - batch_weights = tf.concat(batch_weights, axis=0) - return batch_indices, batch_part_ids, batch_surface_coords, batch_weights - - -class CenterNetTrackTargetAssigner(object): - """Wrapper to compute targets for tracking task. - - Reference paper: A Simple Baseline for Multi-Object Tracking [1] - [1]: https://arxiv.org/abs/2004.01888 - """ - - def __init__(self, stride, num_track_ids): - self._stride = stride - self._num_track_ids = num_track_ids - - def assign_track_targets(self, - height, - width, - gt_track_ids_list, - gt_boxes_list, - gt_weights_list=None): - """Computes the track ID targets. - - Args: - height: int, height of input to the model. This is used to determine the - height of the output. - width: int, width of the input to the model. This is used to determine the - width of the output. - gt_track_ids_list: A list of 1-D tensors with shape [num_boxes] - corresponding to the track ID of each groundtruth detection box. - gt_boxes_list: A list of float tensors with shape [num_boxes, 4] - representing the groundtruth detection bounding boxes for each sample in - the batch. The coordinates are expected in normalized coordinates. - gt_weights_list: A list of 1-D tensors with shape [num_boxes] - corresponding to the weight of each groundtruth detection box. - - Returns: - batch_indices: an integer tensor of shape [batch_size, num_boxes, 3] - holding the indices inside the predicted tensor which should be - penalized. The first column indicates the index along the batch - dimension and the second and third columns indicate the index - along the y and x dimensions respectively. - batch_weights: a float tensor of shape [batch_size, num_boxes] indicating - the weight of each prediction. - track_id_targets: An int32 tensor of size [batch_size, num_boxes, - num_track_ids] containing the one-hot track ID vector of each - groundtruth detection box. - """ - track_id_targets = tf.one_hot( - gt_track_ids_list, depth=self._num_track_ids, axis=-1) - - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_boxes_list) - - batch_indices = [] - batch_weights = [] - - for i, (boxes, weights) in enumerate(zip(gt_boxes_list, gt_weights_list)): - boxes = box_list.BoxList(boxes) - boxes = box_list_ops.to_absolute_coordinates( - boxes, - tf.maximum(height // self._stride, 1), - tf.maximum(width // self._stride, 1)) - # Get the box center coordinates. Each returned tensors have the shape of - # [num_boxes] - (y_center, x_center, _, _) = boxes.get_center_coordinates_and_sizes() - num_boxes = tf.shape(x_center) - - # Compute the indices of the box centers. Shape: - # indices: [num_boxes, 2] - (_, indices) = ta_utils.compute_floor_offsets_with_indices( - y_source=y_center, x_source=x_center) - - # Assign ones if weights are not provided. - if weights is None: - weights = tf.ones(num_boxes, dtype=tf.float32) - - # Shape of [num_boxes, 1] integer tensor filled with current batch index. - batch_index = i * tf.ones_like(indices[:, 0:1], dtype=tf.int32) - batch_indices.append(tf.concat([batch_index, indices], axis=1)) - batch_weights.append(weights) - - batch_indices = tf.stack(batch_indices, axis=0) - batch_weights = tf.stack(batch_weights, axis=0) - - return batch_indices, batch_weights, track_id_targets - - -def filter_mask_overlap_min_area(masks): - """If a pixel belongs to 2 instances, remove it from the larger instance.""" - - num_instances = tf.shape(masks)[0] - def _filter_min_area(): - """Helper function to filter non empty masks.""" - areas = tf.reduce_sum(masks, axis=[1, 2], keepdims=True) - per_pixel_area = masks * areas - # Make sure background is ignored in argmin. - per_pixel_area = (masks * per_pixel_area + - (1 - masks) * per_pixel_area.dtype.max) - min_index = tf.cast(tf.argmin(per_pixel_area, axis=0), tf.int32) - - filtered_masks = ( - tf.range(num_instances)[:, tf.newaxis, tf.newaxis] - == - min_index[tf.newaxis, :, :] - ) - - return tf.cast(filtered_masks, tf.float32) * masks - - return tf.cond(num_instances > 0, _filter_min_area, - lambda: masks) - - -def filter_mask_overlap(masks, method='min_area'): - - if method == 'min_area': - return filter_mask_overlap_min_area(masks) - else: - raise ValueError('Unknown mask overlap filter type - {}'.format(method)) - - -class CenterNetCornerOffsetTargetAssigner(object): - """Wrapper to compute corner offsets for boxes using masks.""" - - def __init__(self, stride, overlap_resolution='min_area'): - """Initializes the corner offset target assigner. - - Args: - stride: int, the stride of the network in output pixels. - overlap_resolution: string, specifies how we handle overlapping - instance masks. Currently only 'min_area' is supported which assigns - overlapping pixels to the instance with the minimum area. - """ - - self._stride = stride - self._overlap_resolution = overlap_resolution - - def assign_corner_offset_targets( - self, gt_boxes_list, gt_masks_list): - """Computes the corner offset targets and foreground map. - - For each pixel that is part of any object's foreground, this function - computes the relative offsets to the top-left and bottom-right corners of - that instance's bounding box. It also returns a foreground map to indicate - which pixels contain valid corner offsets. - - Args: - gt_boxes_list: A list of float tensors with shape [num_boxes, 4] - representing the groundtruth detection bounding boxes for each sample in - the batch. The coordinates are expected in normalized coordinates. - gt_masks_list: A list of float tensors with shape [num_boxes, - input_height, input_width] with values in {0, 1} representing instance - masks for each object. - - Returns: - corner_offsets: A float tensor of shape [batch_size, height, width, 4] - containing, in order, the (y, x) offsets to the top left corner and - the (y, x) offsets to the bottom right corner for each foregroung pixel - foreground: A float tensor of shape [batch_size, height, width] in which - each pixel is set to 1 if it is a part of any instance's foreground - (and thus contains valid corner offsets) and 0 otherwise. - - """ - _, input_height, input_width = ( - shape_utils.combined_static_and_dynamic_shape(gt_masks_list[0])) - output_height = tf.maximum(input_height // self._stride, 1) - output_width = tf.maximum(input_width // self._stride, 1) - y_grid, x_grid = tf.meshgrid( - tf.range(output_height), tf.range(output_width), - indexing='ij') - y_grid, x_grid = tf.cast(y_grid, tf.float32), tf.cast(x_grid, tf.float32) - - corner_targets = [] - foreground_targets = [] - for gt_masks, gt_boxes in zip(gt_masks_list, gt_boxes_list): - gt_masks = _resize_masks(gt_masks, output_height, output_width, - method=ResizeMethod.NEAREST_NEIGHBOR) - gt_masks = filter_mask_overlap(gt_masks, self._overlap_resolution) - - output_height = tf.cast(output_height, tf.float32) - output_width = tf.cast(output_width, tf.float32) - ymin, xmin, ymax, xmax = tf.unstack(gt_boxes, axis=1) - ymin, ymax = ymin * output_height, ymax * output_height - xmin, xmax = xmin * output_width, xmax * output_width - - top_y = ymin[:, tf.newaxis, tf.newaxis] - y_grid[tf.newaxis] - left_x = xmin[:, tf.newaxis, tf.newaxis] - x_grid[tf.newaxis] - bottom_y = ymax[:, tf.newaxis, tf.newaxis] - y_grid[tf.newaxis] - right_x = xmax[:, tf.newaxis, tf.newaxis] - x_grid[tf.newaxis] - - foreground_target = tf.cast(tf.reduce_sum(gt_masks, axis=0) > 0.5, - tf.float32) - foreground_targets.append(foreground_target) - - corner_target = tf.stack([ - tf.reduce_sum(top_y * gt_masks, axis=0), - tf.reduce_sum(left_x * gt_masks, axis=0), - tf.reduce_sum(bottom_y * gt_masks, axis=0), - tf.reduce_sum(right_x * gt_masks, axis=0), - ], axis=2) - - corner_targets.append(corner_target) - - return (tf.stack(corner_targets, axis=0), - tf.stack(foreground_targets, axis=0)) - - -class CenterNetTemporalOffsetTargetAssigner(object): - """Wrapper to compute target tensors for the temporal offset task. - - This class has methods that take as input a batch of ground truth tensors - (in the form of a list) and returns the targets required to train the - temporal offset task. - """ - - def __init__(self, stride): - """Initializes the target assigner. - - Args: - stride: int, the stride of the network in output pixels. - """ - - self._stride = stride - - def assign_temporal_offset_targets(self, - height, - width, - gt_boxes_list, - gt_offsets_list, - gt_match_list, - gt_weights_list=None): - """Returns the temporal offset targets and their indices. - - For each ground truth box, this function assigns it the corresponding - temporal offset to train the model. - - Args: - height: int, height of input to the model. This is used to determine the - height of the output. - width: int, width of the input to the model. This is used to determine the - width of the output. - gt_boxes_list: A list of float tensors with shape [num_boxes, 4] - representing the groundtruth detection bounding boxes for each sample in - the batch. The coordinates are expected in normalized coordinates. - gt_offsets_list: A list of 2-D tf.float32 tensors of shape [num_boxes, 2] - containing the spatial offsets of objects' centers compared with the - previous frame. - gt_match_list: A list of 1-D tf.float32 tensors of shape [num_boxes] - containing flags that indicate if an object has existed in the - previous frame. - gt_weights_list: A list of tensors with shape [num_boxes] corresponding to - the weight of each groundtruth detection box. - - Returns: - batch_indices: an integer tensor of shape [num_boxes, 3] holding the - indices inside the predicted tensor which should be penalized. The - first column indicates the index along the batch dimension and the - second and third columns indicate the index along the y and x - dimensions respectively. - batch_temporal_offsets: a float tensor of shape [num_boxes, 2] of the - expected y and x temporal offset of each object center in the - output space. - batch_weights: a float tensor of shape [num_boxes] indicating the - weight of each prediction. - """ - - if gt_weights_list is None: - gt_weights_list = [None] * len(gt_boxes_list) - - batch_indices = [] - batch_weights = [] - batch_temporal_offsets = [] - - for i, (boxes, offsets, match_flags, weights) in enumerate(zip( - gt_boxes_list, gt_offsets_list, gt_match_list, gt_weights_list)): - boxes = box_list.BoxList(boxes) - boxes = box_list_ops.to_absolute_coordinates( - boxes, - tf.maximum(height // self._stride, 1), - tf.maximum(width // self._stride, 1)) - # Get the box center coordinates. Each returned tensors have the shape of - # [num_boxes] - (y_center, x_center, _, _) = boxes.get_center_coordinates_and_sizes() - num_boxes = tf.shape(x_center) - - # Compute the offsets and indices of the box centers. Shape: - # offsets: [num_boxes, 2] - # indices: [num_boxes, 2] - (_, indices) = ta_utils.compute_floor_offsets_with_indices( - y_source=y_center, x_source=x_center) - - # Assign ones if weights are not provided. - # if an object is not matched, its weight becomes zero. - if weights is None: - weights = tf.ones(num_boxes, dtype=tf.float32) - weights *= match_flags - - # Shape of [num_boxes, 1] integer tensor filled with current batch index. - batch_index = i * tf.ones_like(indices[:, 0:1], dtype=tf.int32) - batch_indices.append(tf.concat([batch_index, indices], axis=1)) - batch_weights.append(weights) - batch_temporal_offsets.append(offsets) - - batch_indices = tf.concat(batch_indices, axis=0) - batch_weights = tf.concat(batch_weights, axis=0) - batch_temporal_offsets = tf.concat(batch_temporal_offsets, axis=0) - return (batch_indices, batch_temporal_offsets, batch_weights) - - -class DETRTargetAssigner(object): - """Target assigner for DETR (https://arxiv.org/abs/2005.12872). - - Detection Transformer (DETR) matches predicted boxes to groundtruth directly - to determine targets instead of matching anchors to groundtruth. Hence, the - new target assigner. - """ - - def __init__(self): - """Construct Object Detection Target Assigner.""" - self._similarity_calc = sim_calc.DETRSimilarity() - self._matcher = hungarian_matcher.HungarianBipartiteMatcher() - - def batch_assign(self, - pred_box_batch, - gt_box_batch, - pred_class_batch, - gt_class_targets_batch, - gt_weights_batch=None, - unmatched_class_label_batch=None): - """Batched assignment of classification and regression targets. - - Args: - pred_box_batch: a tensor of shape [batch_size, num_queries, 4] - representing predicted bounding boxes. - gt_box_batch: a tensor of shape [batch_size, num_queries, 4] - representing groundtruth bounding boxes. - pred_class_batch: A list of tensors with length batch_size, where each - each tensor has shape [num_queries, num_classes] to be used - by certain similarity calculators. - gt_class_targets_batch: a list of tensors with length batch_size, where - each tensor has shape [num_gt_boxes_i, num_classes] and - num_gt_boxes_i is the number of boxes in the ith boxlist of - gt_box_batch. - gt_weights_batch: A list of 1-D tf.float32 tensors of shape - [num_boxes] containing weights for groundtruth boxes. - unmatched_class_label_batch: a float32 tensor with shape - [d_1, d_2, ..., d_k] which is consistent with the classification target - for each anchor (and can be empty for scalar targets). This shape must - thus be compatible with the `gt_class_targets_batch`. - - Returns: - batch_cls_targets: a tensor with shape [batch_size, num_pred_boxes, - num_classes], - batch_cls_weights: a tensor with shape [batch_size, num_pred_boxes, - num_classes], - batch_reg_targets: a tensor with shape [batch_size, num_pred_boxes, - box_code_dimension] - batch_reg_weights: a tensor with shape [batch_size, num_pred_boxes]. - """ - pred_box_batch = [ - box_list.BoxList(pred_box) - for pred_box in tf.unstack(pred_box_batch)] - gt_box_batch = [ - box_list.BoxList(gt_box) - for gt_box in tf.unstack(gt_box_batch)] - - cls_targets_list = [] - cls_weights_list = [] - reg_targets_list = [] - reg_weights_list = [] - if gt_weights_batch is None: - gt_weights_batch = [None] * len(gt_class_targets_batch) - if unmatched_class_label_batch is None: - unmatched_class_label_batch = [None] * len(gt_class_targets_batch) - pred_class_batch = tf.unstack(pred_class_batch) - for (pred_boxes, gt_boxes, pred_class_batch, gt_class_targets, gt_weights, - unmatched_class_label) in zip(pred_box_batch, gt_box_batch, - pred_class_batch, gt_class_targets_batch, - gt_weights_batch, - unmatched_class_label_batch): - (cls_targets, cls_weights, reg_targets, - reg_weights) = self.assign(pred_boxes, gt_boxes, pred_class_batch, - gt_class_targets, gt_weights, - unmatched_class_label) - cls_targets_list.append(cls_targets) - cls_weights_list.append(cls_weights) - reg_targets_list.append(reg_targets) - reg_weights_list.append(reg_weights) - batch_cls_targets = tf.stack(cls_targets_list) - batch_cls_weights = tf.stack(cls_weights_list) - batch_reg_targets = tf.stack(reg_targets_list) - batch_reg_weights = tf.stack(reg_weights_list) - return (batch_cls_targets, batch_cls_weights, batch_reg_targets, - batch_reg_weights) - - def assign(self, - pred_boxes, - gt_boxes, - pred_classes, - gt_labels, - gt_weights=None, - unmatched_class_label=None): - """Assign classification and regression targets to each box_pred. - - For a given set of pred_boxes and groundtruth detections, match pred_boxes - to gt_boxes and assign classification and regression targets to - each box_pred as well as weights based on the resulting match (specifying, - e.g., which pred_boxes should not contribute to training loss). - - pred_boxes that are not matched to anything are given a classification - target of `unmatched_cls_target`. - - Args: - pred_boxes: a BoxList representing N pred_boxes - gt_boxes: a BoxList representing M groundtruth boxes - pred_classes: A tensor with shape [max_num_boxes, num_classes] - to be used by certain similarity calculators. - gt_labels: a tensor of shape [M, num_classes] - with labels for each of the ground_truth boxes. The subshape - [num_classes] can be empty (corresponding to scalar inputs). When set - to None, gt_labels assumes a binary problem where all - ground_truth boxes get a positive label (of 1). - gt_weights: a float tensor of shape [M] indicating the weight to - assign to all pred_boxes match to a particular groundtruth box. The - weights must be in [0., 1.]. If None, all weights are set to 1. - Generally no groundtruth boxes with zero weight match to any pred_boxes - as matchers are aware of groundtruth weights. Additionally, - `cls_weights` and `reg_weights` are calculated using groundtruth - weights as an added safety. - unmatched_class_label: a float32 tensor with shape [d_1, d_2, ..., d_k] - which is consistent with the classification target for each - anchor (and can be empty for scalar targets). This shape must thus be - compatible with the groundtruth labels that are passed to the "assign" - function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]). - - Returns: - cls_targets: a float32 tensor with shape [num_pred_boxes, num_classes], - where the subshape [num_classes] is compatible with gt_labels - which has shape [num_gt_boxes, num_classes]. - cls_weights: a float32 tensor with shape [num_pred_boxes, num_classes], - representing weights for each element in cls_targets. - reg_targets: a float32 tensor with shape [num_pred_boxes, - box_code_dimension] - reg_weights: a float32 tensor with shape [num_pred_boxes] - - """ - if not unmatched_class_label: - unmatched_class_label = tf.constant( - [1] + [0] * (gt_labels.shape[1] - 1), tf.float32) - - if gt_weights is None: - num_gt_boxes = gt_boxes.num_boxes_static() - if not num_gt_boxes: - num_gt_boxes = gt_boxes.num_boxes() - gt_weights = tf.ones([num_gt_boxes], dtype=tf.float32) - - gt_boxes.add_field(fields.BoxListFields.classes, gt_labels) - pred_boxes.add_field(fields.BoxListFields.classes, pred_classes) - - match_quality_matrix = self._similarity_calc.compare( - gt_boxes, - pred_boxes) - match = self._matcher.match(match_quality_matrix, - valid_rows=tf.greater(gt_weights, 0)) - - matched_gt_boxes = match.gather_based_on_match( - gt_boxes.get(), - unmatched_value=tf.zeros(4), - ignored_value=tf.zeros(4)) - matched_gt_boxlist = box_list.BoxList(matched_gt_boxes) - ty, tx, th, tw = matched_gt_boxlist.get_center_coordinates_and_sizes() - reg_targets = tf.transpose(tf.stack([ty, tx, th, tw])) - cls_targets = match.gather_based_on_match( - gt_labels, - unmatched_value=unmatched_class_label, - ignored_value=unmatched_class_label) - reg_weights = match.gather_based_on_match( - gt_weights, - ignored_value=0., - unmatched_value=0.) - cls_weights = match.gather_based_on_match( - gt_weights, - ignored_value=0., - unmatched_value=1) - - # convert cls_weights from per-box_pred to per-class. - class_label_shape = tf.shape(cls_targets)[1:] - weights_multiple = tf.concat( - [tf.constant([1]), class_label_shape], - axis=0) - cls_weights = tf.expand_dims(cls_weights, -1) - cls_weights = tf.tile(cls_weights, weights_multiple) - - return (cls_targets, cls_weights, reg_targets, reg_weights) diff --git a/research/object_detection/core/target_assigner_test.py b/research/object_detection/core/target_assigner_test.py deleted file mode 100644 index 654f26c6f66..00000000000 --- a/research/object_detection/core/target_assigner_test.py +++ /dev/null @@ -1,2796 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.target_assigner.""" -from absl.testing import parameterized -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.box_coders import keypoint_box_coder -from object_detection.box_coders import mean_stddev_box_coder -from object_detection.core import box_list -from object_detection.core import region_similarity_calculator -from object_detection.core import standard_fields as fields -from object_detection.core import target_assigner as targetassigner -from object_detection.matchers import argmax_matcher -from object_detection.utils import np_box_ops -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -class TargetAssignerTest(test_case.TestCase): - - def test_assign_agnostic(self): - def graph_fn(anchor_means, groundtruth_box_corners): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - result = target_assigner.assign( - anchors_boxlist, groundtruth_boxlist, unmatched_class_label=None) - (cls_targets, cls_weights, reg_targets, reg_weights, _) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 0.8], - [0, 0.5, .5, 1.0]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9]], - dtype=np.float32) - exp_cls_targets = [[1], [1], [0]] - exp_cls_weights = [[1], [1], [1]] - exp_reg_targets = [[0, 0, 0, 0], - [0, 0, -1, 1], - [0, 0, 0, 0]] - exp_reg_weights = [1, 1, 0] - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_box_corners]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_assign_class_agnostic_with_ignored_matches(self): - # Note: test is very similar to above. The third box matched with an IOU - # of 0.35, which is between the matched and unmatched threshold. This means - # That like above the expected classification targets are [1, 1, 0]. - # Unlike above, the third target is ignored and therefore expected - # classification weights are [1, 1, 0]. - def graph_fn(anchor_means, groundtruth_box_corners): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.3) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - result = target_assigner.assign( - anchors_boxlist, groundtruth_boxlist, unmatched_class_label=None) - (cls_targets, cls_weights, reg_targets, reg_weights, _) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 0.8], - [0.0, 0.5, .9, 1.0]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9]], dtype=np.float32) - exp_cls_targets = [[1], [1], [0]] - exp_cls_weights = [[1], [1], [0]] - exp_reg_targets = [[0, 0, 0, 0], - [0, 0, -1, 1], - [0, 0, 0, 0]] - exp_reg_weights = [1, 1, 0] - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_box_corners]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_assign_agnostic_with_keypoints(self): - - def graph_fn(anchor_means, groundtruth_box_corners, - groundtruth_keypoints): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = keypoint_box_coder.KeypointBoxCoder( - num_keypoints=6, scale_factors=[10.0, 10.0, 5.0, 5.0]) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - groundtruth_boxlist.add_field(fields.BoxListFields.keypoints, - groundtruth_keypoints) - result = target_assigner.assign( - anchors_boxlist, groundtruth_boxlist, unmatched_class_label=None) - (cls_targets, cls_weights, reg_targets, reg_weights, _) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 1.0], - [0.0, 0.5, .9, 1.0]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.45, 0.45, 0.95, 0.95]], - dtype=np.float32) - groundtruth_keypoints = np.array( - [[[0.1, 0.2], [0.1, 0.3], [0.2, 0.2], [0.2, 0.2], [0.1, 0.1], [0.9, 0]], - [[0, 0.3], [0.2, 0.4], [0.5, 0.6], [0, 0.6], [0.8, 0.2], [0.2, 0.4]]], - dtype=np.float32) - exp_cls_targets = [[1], [1], [0]] - exp_cls_weights = [[1], [1], [1]] - exp_reg_targets = [[0, 0, 0, 0, -3, -1, -3, 1, -1, -1, -1, -1, -3, -3, 13, - -5], - [-1, -1, 0, 0, -15, -9, -11, -7, -5, -3, -15, -3, 1, -11, - -11, -7], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] - exp_reg_weights = [1, 1, 0] - (cls_targets_out, cls_weights_out, reg_targets_out, - reg_weights_out) = self.execute(graph_fn, [anchor_means, - groundtruth_box_corners, - groundtruth_keypoints]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_assign_class_agnostic_with_keypoints_and_ignored_matches(self): - # Note: test is very similar to above. The third box matched with an IOU - # of 0.35, which is between the matched and unmatched threshold. This means - # That like above the expected classification targets are [1, 1, 0]. - # Unlike above, the third target is ignored and therefore expected - # classification weights are [1, 1, 0]. - def graph_fn(anchor_means, groundtruth_box_corners, - groundtruth_keypoints): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = keypoint_box_coder.KeypointBoxCoder( - num_keypoints=6, scale_factors=[10.0, 10.0, 5.0, 5.0]) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - groundtruth_boxlist.add_field(fields.BoxListFields.keypoints, - groundtruth_keypoints) - result = target_assigner.assign( - anchors_boxlist, groundtruth_boxlist, unmatched_class_label=None) - (cls_targets, cls_weights, reg_targets, reg_weights, _) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 1.0], - [0.0, 0.5, .9, 1.0]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.45, 0.45, 0.95, 0.95]], - dtype=np.float32) - groundtruth_keypoints = np.array( - [[[0.1, 0.2], [0.1, 0.3], [0.2, 0.2], [0.2, 0.2], [0.1, 0.1], [0.9, 0]], - [[0, 0.3], [0.2, 0.4], [0.5, 0.6], [0, 0.6], [0.8, 0.2], [0.2, 0.4]]], - dtype=np.float32) - exp_cls_targets = [[1], [1], [0]] - exp_cls_weights = [[1], [1], [1]] - exp_reg_targets = [[0, 0, 0, 0, -3, -1, -3, 1, -1, -1, -1, -1, -3, -3, 13, - -5], - [-1, -1, 0, 0, -15, -9, -11, -7, -5, -3, -15, -3, 1, -11, - -11, -7], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] - exp_reg_weights = [1, 1, 0] - (cls_targets_out, cls_weights_out, reg_targets_out, - reg_weights_out) = self.execute(graph_fn, [anchor_means, - groundtruth_box_corners, - groundtruth_keypoints]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_assign_multiclass(self): - - def graph_fn(anchor_means, groundtruth_box_corners, groundtruth_labels): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - unmatched_class_label = tf.constant([1, 0, 0, 0, 0, 0, 0], tf.float32) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - result = target_assigner.assign( - anchors_boxlist, - groundtruth_boxlist, - groundtruth_labels, - unmatched_class_label=unmatched_class_label) - (cls_targets, cls_weights, reg_targets, reg_weights, _) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 0.8], - [0, 0.5, .5, 1.0], - [.75, 0, 1.0, .25]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9], - [.75, 0, .95, .27]], dtype=np.float32) - groundtruth_labels = np.array([[0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1, 0], - [0, 0, 0, 1, 0, 0, 0]], dtype=np.float32) - - exp_cls_targets = [[0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1, 0], - [1, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 1, 0, 0, 0]] - exp_cls_weights = [[1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1], - [1, 1, 1, 1, 1, 1, 1]] - exp_reg_targets = [[0, 0, 0, 0], - [0, 0, -1, 1], - [0, 0, 0, 0], - [0, 0, -.5, .2]] - exp_reg_weights = [1, 1, 0, 1] - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_box_corners, groundtruth_labels]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_assign_multiclass_with_groundtruth_weights(self): - - def graph_fn(anchor_means, groundtruth_box_corners, groundtruth_labels, - groundtruth_weights): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - unmatched_class_label = tf.constant([1, 0, 0, 0, 0, 0, 0], tf.float32) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - result = target_assigner.assign( - anchors_boxlist, - groundtruth_boxlist, - groundtruth_labels, - unmatched_class_label=unmatched_class_label, - groundtruth_weights=groundtruth_weights) - (_, cls_weights, _, reg_weights, _) = result - return (cls_weights, reg_weights) - - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 0.8], - [0, 0.5, .5, 1.0], - [.75, 0, 1.0, .25]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9], - [.75, 0, .95, .27]], dtype=np.float32) - groundtruth_labels = np.array([[0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1, 0], - [0, 0, 0, 1, 0, 0, 0]], dtype=np.float32) - groundtruth_weights = np.array([0.3, 0., 0.5], dtype=np.float32) - - # background class gets weight of 1. - exp_cls_weights = [[0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3], - [0, 0, 0, 0, 0, 0, 0], - [1, 1, 1, 1, 1, 1, 1], - [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]] - exp_reg_weights = [0.3, 0., 0., 0.5] # background class gets weight of 0. - - (cls_weights_out, reg_weights_out) = self.execute(graph_fn, [ - anchor_means, groundtruth_box_corners, groundtruth_labels, - groundtruth_weights - ]) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_assign_multidimensional_class_targets(self): - - def graph_fn(anchor_means, groundtruth_box_corners, groundtruth_labels): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - - unmatched_class_label = tf.constant([[0, 0], [0, 0]], tf.float32) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - result = target_assigner.assign( - anchors_boxlist, - groundtruth_boxlist, - groundtruth_labels, - unmatched_class_label=unmatched_class_label) - (cls_targets, cls_weights, reg_targets, reg_weights, _) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 0.8], - [0, 0.5, .5, 1.0], - [.75, 0, 1.0, .25]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9], - [.75, 0, .95, .27]], dtype=np.float32) - - groundtruth_labels = np.array([[[0, 1], [1, 0]], - [[1, 0], [0, 1]], - [[0, 1], [1, .5]]], np.float32) - - exp_cls_targets = [[[0, 1], [1, 0]], - [[1, 0], [0, 1]], - [[0, 0], [0, 0]], - [[0, 1], [1, .5]]] - exp_cls_weights = [[[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]]] - exp_reg_targets = [[0, 0, 0, 0], - [0, 0, -1, 1], - [0, 0, 0, 0], - [0, 0, -.5, .2]] - exp_reg_weights = [1, 1, 0, 1] - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_box_corners, groundtruth_labels]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_assign_empty_groundtruth(self): - - def graph_fn(anchor_means, groundtruth_box_corners, groundtruth_labels): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - unmatched_class_label = tf.constant([0, 0, 0], tf.float32) - anchors_boxlist = box_list.BoxList(anchor_means) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - result = target_assigner.assign( - anchors_boxlist, - groundtruth_boxlist, - groundtruth_labels, - unmatched_class_label=unmatched_class_label) - (cls_targets, cls_weights, reg_targets, reg_weights, _) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_box_corners = np.zeros((0, 4), dtype=np.float32) - groundtruth_labels = np.zeros((0, 3), dtype=np.float32) - anchor_means = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 0.8], - [0, 0.5, .5, 1.0], - [.75, 0, 1.0, .25]], - dtype=np.float32) - exp_cls_targets = [[0, 0, 0], - [0, 0, 0], - [0, 0, 0], - [0, 0, 0]] - exp_cls_weights = [[1, 1, 1], - [1, 1, 1], - [1, 1, 1], - [1, 1, 1]] - exp_reg_targets = [[0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]] - exp_reg_weights = [0, 0, 0, 0] - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_box_corners, groundtruth_labels]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_raises_error_on_incompatible_groundtruth_boxes_and_labels(self): - similarity_calc = region_similarity_calculator.NegSqDistSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder() - unmatched_class_label = tf.constant([1, 0, 0, 0, 0, 0, 0], tf.float32) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - - prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 0.8], - [0, 0.5, .5, 1.0], - [.75, 0, 1.0, .25]]) - priors = box_list.BoxList(prior_means) - - box_corners = [[0.0, 0.0, 0.5, 0.5], - [0.0, 0.0, 0.5, 0.8], - [0.5, 0.5, 0.9, 0.9], - [.75, 0, .95, .27]] - boxes = box_list.BoxList(tf.constant(box_corners)) - - groundtruth_labels = tf.constant([[0, 1, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 1, 0], - [0, 0, 0, 1, 0, 0, 0]], tf.float32) - with self.assertRaisesRegexp(ValueError, 'Unequal shapes'): - target_assigner.assign( - priors, - boxes, - groundtruth_labels, - unmatched_class_label=unmatched_class_label) - - def test_raises_error_on_invalid_groundtruth_labels(self): - similarity_calc = region_similarity_calculator.NegSqDistSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=1.0) - unmatched_class_label = tf.constant([[0, 0], [0, 0], [0, 0]], tf.float32) - target_assigner = targetassigner.TargetAssigner( - similarity_calc, matcher, box_coder) - - prior_means = tf.constant([[0.0, 0.0, 0.5, 0.5]]) - priors = box_list.BoxList(prior_means) - - box_corners = [[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9], - [.75, 0, .95, .27]] - boxes = box_list.BoxList(tf.constant(box_corners)) - groundtruth_labels = tf.constant([[[0, 1], [1, 0]]], tf.float32) - - with self.assertRaises(ValueError): - target_assigner.assign( - priors, - boxes, - groundtruth_labels, - unmatched_class_label=unmatched_class_label) - - -class BatchTargetAssignerTest(test_case.TestCase): - - def _get_target_assigner(self): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - return targetassigner.TargetAssigner(similarity_calc, matcher, box_coder) - - def test_batch_assign_targets(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_targets = [None, None] - anchors_boxlist = box_list.BoxList(anchor_means) - agnostic_target_assigner = self._get_target_assigner() - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_targets( - agnostic_target_assigner, anchors_boxlist, gt_box_batch, - gt_class_targets) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2]], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842]], - dtype=np.float32) - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - - exp_cls_targets = [[[1], [0], [0], [0]], - [[0], [1], [1], [0]]] - exp_cls_weights = [[[1], [1], [1], [1]], - [[1], [1], [1], [1]]] - exp_reg_targets = [[[0, 0, -0.5, -0.5], - [0, 0, 0, 0], - [0, 0, 0, 0,], - [0, 0, 0, 0,],], - [[0, 0, 0, 0,], - [0, 0.01231521, 0, 0], - [0.15789001, -0.01500003, 0.57889998, -1.15799987], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 0, 0, 0], - [0, 1, 1, 0]] - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_boxlist1, groundtruth_boxlist2]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_multiclass_targets(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_targets = [class_targets1, class_targets2] - anchors_boxlist = box_list.BoxList(anchor_means) - multiclass_target_assigner = self._get_target_assigner() - num_classes = 3 - unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32) - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_targets( - multiclass_target_assigner, anchors_boxlist, gt_box_batch, - gt_class_targets, unmatched_class_label) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2]], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842]], - dtype=np.float32) - class_targets1 = np.array([[0, 1, 0, 0]], dtype=np.float32) - class_targets2 = np.array([[0, 0, 0, 1], - [0, 0, 1, 0]], dtype=np.float32) - - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - exp_cls_targets = [[[0, 1, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0]], - [[1, 0, 0, 0], - [0, 0, 0, 1], - [0, 0, 1, 0], - [1, 0, 0, 0]]] - exp_cls_weights = [[[1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1]], - [[1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1]]] - exp_reg_targets = [[[0, 0, -0.5, -0.5], - [0, 0, 0, 0], - [0, 0, 0, 0,], - [0, 0, 0, 0,],], - [[0, 0, 0, 0,], - [0, 0.01231521, 0, 0], - [0.15789001, -0.01500003, 0.57889998, -1.15799987], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 0, 0, 0], - [0, 1, 1, 0]] - - (cls_targets_out, cls_weights_out, reg_targets_out, - reg_weights_out) = self.execute(graph_fn, [ - anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2 - ]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_multiclass_targets_with_padded_groundtruth(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2, groundtruth_weights1, - groundtruth_weights2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_targets = [class_targets1, class_targets2] - gt_weights = [groundtruth_weights1, groundtruth_weights2] - anchors_boxlist = box_list.BoxList(anchor_means) - multiclass_target_assigner = self._get_target_assigner() - num_classes = 3 - unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32) - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_targets( - multiclass_target_assigner, anchors_boxlist, gt_box_batch, - gt_class_targets, unmatched_class_label, gt_weights) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2], - [0., 0., 0., 0.]], dtype=np.float32) - groundtruth_weights1 = np.array([1, 0], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842], - [0, 0, 0, 0]], - dtype=np.float32) - groundtruth_weights2 = np.array([1, 1, 0], dtype=np.float32) - class_targets1 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], dtype=np.float32) - class_targets2 = np.array([[0, 0, 0, 1], - [0, 0, 1, 0], - [0, 0, 0, 0]], dtype=np.float32) - - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - - exp_cls_targets = [[[0, 1, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0]], - [[1, 0, 0, 0], - [0, 0, 0, 1], - [0, 0, 1, 0], - [1, 0, 0, 0]]] - exp_cls_weights = [[[1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1]], - [[1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1], - [1, 1, 1, 1]]] - exp_reg_targets = [[[0, 0, -0.5, -0.5], - [0, 0, 0, 0], - [0, 0, 0, 0,], - [0, 0, 0, 0,],], - [[0, 0, 0, 0,], - [0, 0.01231521, 0, 0], - [0.15789001, -0.01500003, 0.57889998, -1.15799987], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 0, 0, 0], - [0, 1, 1, 0]] - - (cls_targets_out, cls_weights_out, reg_targets_out, - reg_weights_out) = self.execute(graph_fn, [ - anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2, groundtruth_weights1, - groundtruth_weights2 - ]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_multidimensional_targets(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_targets = [class_targets1, class_targets2] - anchors_boxlist = box_list.BoxList(anchor_means) - multiclass_target_assigner = self._get_target_assigner() - target_dimensions = (2, 3) - unmatched_class_label = tf.constant(np.zeros(target_dimensions), - tf.float32) - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_targets( - multiclass_target_assigner, anchors_boxlist, gt_box_batch, - gt_class_targets, unmatched_class_label) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2]], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842]], - dtype=np.float32) - class_targets1 = np.array([[[0, 1, 1], - [1, 1, 0]]], dtype=np.float32) - class_targets2 = np.array([[[0, 1, 1], - [1, 1, 0]], - [[0, 0, 1], - [0, 0, 1]]], dtype=np.float32) - - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - - exp_cls_targets = [[[[0., 1., 1.], - [1., 1., 0.]], - [[0., 0., 0.], - [0., 0., 0.]], - [[0., 0., 0.], - [0., 0., 0.]], - [[0., 0., 0.], - [0., 0., 0.]]], - [[[0., 0., 0.], - [0., 0., 0.]], - [[0., 1., 1.], - [1., 1., 0.]], - [[0., 0., 1.], - [0., 0., 1.]], - [[0., 0., 0.], - [0., 0., 0.]]]] - exp_cls_weights = [[[[1., 1., 1.], - [1., 1., 1.]], - [[1., 1., 1.], - [1., 1., 1.]], - [[1., 1., 1.], - [1., 1., 1.]], - [[1., 1., 1.], - [1., 1., 1.]]], - [[[1., 1., 1.], - [1., 1., 1.]], - [[1., 1., 1.], - [1., 1., 1.]], - [[1., 1., 1.], - [1., 1., 1.]], - [[1., 1., 1.], - [1., 1., 1.]]]] - exp_reg_targets = [[[0, 0, -0.5, -0.5], - [0, 0, 0, 0], - [0, 0, 0, 0,], - [0, 0, 0, 0,],], - [[0, 0, 0, 0,], - [0, 0.01231521, 0, 0], - [0.15789001, -0.01500003, 0.57889998, -1.15799987], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 0, 0, 0], - [0, 1, 1, 0]] - - (cls_targets_out, cls_weights_out, reg_targets_out, - reg_weights_out) = self.execute(graph_fn, [ - anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2 - ]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_empty_groundtruth(self): - - def graph_fn(anchor_means, groundtruth_box_corners, gt_class_targets): - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - gt_box_batch = [groundtruth_boxlist] - gt_class_targets_batch = [gt_class_targets] - anchors_boxlist = box_list.BoxList(anchor_means) - - multiclass_target_assigner = self._get_target_assigner() - num_classes = 3 - unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32) - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_targets( - multiclass_target_assigner, anchors_boxlist, - gt_box_batch, gt_class_targets_batch, unmatched_class_label) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_box_corners = np.zeros((0, 4), dtype=np.float32) - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1]], dtype=np.float32) - exp_cls_targets = [[[1, 0, 0, 0], - [1, 0, 0, 0]]] - exp_cls_weights = [[[1, 1, 1, 1], - [1, 1, 1, 1]]] - exp_reg_targets = [[[0, 0, 0, 0], - [0, 0, 0, 0]]] - exp_reg_weights = [[0, 0]] - num_classes = 3 - pad = 1 - gt_class_targets = np.zeros((0, num_classes + pad), dtype=np.float32) - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_box_corners, gt_class_targets]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - -class BatchGetTargetsTest(test_case.TestCase): - - def test_scalar_targets(self): - batch_match = np.array([[1, 0, 1], - [-2, -1, 1]], dtype=np.int32) - groundtruth_tensors_list = np.array([[11, 12], [13, 14]], dtype=np.int32) - groundtruth_weights_list = np.array([[1.0, 1.0], [1.0, 0.5]], - dtype=np.float32) - unmatched_value = np.array(99, dtype=np.int32) - unmatched_weight = np.array(0.0, dtype=np.float32) - - def graph_fn(batch_match, groundtruth_tensors_list, - groundtruth_weights_list, unmatched_value, unmatched_weight): - targets, weights = targetassigner.batch_get_targets( - batch_match, tf.unstack(groundtruth_tensors_list), - tf.unstack(groundtruth_weights_list), - unmatched_value, unmatched_weight) - return (targets, weights) - - (targets_np, weights_np) = self.execute(graph_fn, [ - batch_match, groundtruth_tensors_list, groundtruth_weights_list, - unmatched_value, unmatched_weight - ]) - self.assertAllEqual([[12, 11, 12], - [99, 99, 14]], targets_np) - self.assertAllClose([[1.0, 1.0, 1.0], - [0.0, 0.0, 0.5]], weights_np) - - def test_1d_targets(self): - batch_match = np.array([[1, 0, 1], - [-2, -1, 1]], dtype=np.int32) - groundtruth_tensors_list = np.array([[[11, 12], [12, 13]], - [[13, 14], [14, 15]]], - dtype=np.float32) - groundtruth_weights_list = np.array([[1.0, 1.0], [1.0, 0.5]], - dtype=np.float32) - unmatched_value = np.array([99, 99], dtype=np.float32) - unmatched_weight = np.array(0.0, dtype=np.float32) - - def graph_fn(batch_match, groundtruth_tensors_list, - groundtruth_weights_list, unmatched_value, unmatched_weight): - targets, weights = targetassigner.batch_get_targets( - batch_match, tf.unstack(groundtruth_tensors_list), - tf.unstack(groundtruth_weights_list), - unmatched_value, unmatched_weight) - return (targets, weights) - - (targets_np, weights_np) = self.execute(graph_fn, [ - batch_match, groundtruth_tensors_list, groundtruth_weights_list, - unmatched_value, unmatched_weight - ]) - self.assertAllClose([[[12, 13], [11, 12], [12, 13]], - [[99, 99], [99, 99], [14, 15]]], targets_np) - self.assertAllClose([[1.0, 1.0, 1.0], - [0.0, 0.0, 0.5]], weights_np) - - -class BatchTargetAssignConfidencesTest(test_case.TestCase): - - def _get_target_assigner(self): - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=0.5, - unmatched_threshold=0.5) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - return targetassigner.TargetAssigner(similarity_calc, matcher, box_coder) - - def test_batch_assign_empty_groundtruth(self): - - def graph_fn(anchor_means, groundtruth_box_corners, gt_class_confidences): - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - gt_box_batch = [groundtruth_boxlist] - gt_class_confidences_batch = [gt_class_confidences] - anchors_boxlist = box_list.BoxList(anchor_means) - - num_classes = 3 - implicit_class_weight = 0.5 - unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32) - multiclass_target_assigner = self._get_target_assigner() - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_confidences( - multiclass_target_assigner, - anchors_boxlist, - gt_box_batch, - gt_class_confidences_batch, - unmatched_class_label=unmatched_class_label, - include_background_class=True, - implicit_class_weight=implicit_class_weight) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_box_corners = np.zeros((0, 4), dtype=np.float32) - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1]], dtype=np.float32) - num_classes = 3 - pad = 1 - gt_class_confidences = np.zeros((0, num_classes + pad), dtype=np.float32) - - exp_cls_targets = [[[1, 0, 0, 0], - [1, 0, 0, 0]]] - exp_cls_weights = [[[0.5, 0.5, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5]]] - exp_reg_targets = [[[0, 0, 0, 0], - [0, 0, 0, 0]]] - exp_reg_weights = [[0, 0]] - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, - [anchor_means, groundtruth_box_corners, gt_class_confidences]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_confidences_agnostic(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_confidences_batch = [None, None] - anchors_boxlist = box_list.BoxList(anchor_means) - agnostic_target_assigner = self._get_target_assigner() - implicit_class_weight = 0.5 - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_confidences( - agnostic_target_assigner, - anchors_boxlist, - gt_box_batch, - gt_class_confidences_batch, - include_background_class=False, - implicit_class_weight=implicit_class_weight) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2]], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842]], - dtype=np.float32) - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - - exp_cls_targets = [[[1], [0], [0], [0]], - [[0], [1], [1], [0]]] - exp_cls_weights = [[[1], [0.5], [0.5], [0.5]], - [[0.5], [1], [1], [0.5]]] - exp_reg_targets = [[[0, 0, -0.5, -0.5], - [0, 0, 0, 0], - [0, 0, 0, 0,], - [0, 0, 0, 0,],], - [[0, 0, 0, 0,], - [0, 0.01231521, 0, 0], - [0.15789001, -0.01500003, 0.57889998, -1.15799987], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 0, 0, 0], - [0, 1, 1, 0]] - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute( - graph_fn, [anchor_means, groundtruth_boxlist1, groundtruth_boxlist2]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_confidences_multiclass(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_confidences_batch = [class_targets1, class_targets2] - anchors_boxlist = box_list.BoxList(anchor_means) - multiclass_target_assigner = self._get_target_assigner() - num_classes = 3 - implicit_class_weight = 0.5 - unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32) - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_confidences( - multiclass_target_assigner, - anchors_boxlist, - gt_box_batch, - gt_class_confidences_batch, - unmatched_class_label=unmatched_class_label, - include_background_class=True, - implicit_class_weight=implicit_class_weight) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2]], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842]], - dtype=np.float32) - class_targets1 = np.array([[0, 1, 0, 0]], dtype=np.float32) - class_targets2 = np.array([[0, 0, 0, 1], - [0, 0, -1, 0]], dtype=np.float32) - - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - exp_cls_targets = [[[0, 1, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0]], - [[1, 0, 0, 0], - [0, 0, 0, 1], - [1, 0, 0, 0], - [1, 0, 0, 0]]] - exp_cls_weights = [[[1, 1, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5]], - [[0.5, 0.5, 0.5, 0.5], - [1, 0.5, 0.5, 1], - [0.5, 0.5, 1, 0.5], - [0.5, 0.5, 0.5, 0.5]]] - exp_reg_targets = [[[0, 0, -0.5, -0.5], - [0, 0, 0, 0], - [0, 0, 0, 0,], - [0, 0, 0, 0,],], - [[0, 0, 0, 0,], - [0, 0.01231521, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 0, 0, 0], - [0, 1, 0, 0]] - - (cls_targets_out, cls_weights_out, reg_targets_out, - reg_weights_out) = self.execute(graph_fn, [ - anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2 - ]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_confidences_multiclass_with_padded_groundtruth(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2, groundtruth_weights1, - groundtruth_weights2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_confidences_batch = [class_targets1, class_targets2] - gt_weights = [groundtruth_weights1, groundtruth_weights2] - anchors_boxlist = box_list.BoxList(anchor_means) - multiclass_target_assigner = self._get_target_assigner() - num_classes = 3 - unmatched_class_label = tf.constant([1] + num_classes * [0], tf.float32) - implicit_class_weight = 0.5 - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_confidences( - multiclass_target_assigner, - anchors_boxlist, - gt_box_batch, - gt_class_confidences_batch, - gt_weights, - unmatched_class_label=unmatched_class_label, - include_background_class=True, - implicit_class_weight=implicit_class_weight) - - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2], - [0., 0., 0., 0.]], dtype=np.float32) - groundtruth_weights1 = np.array([1, 0], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842], - [0, 0, 0, 0]], - dtype=np.float32) - groundtruth_weights2 = np.array([1, 1, 0], dtype=np.float32) - class_targets1 = np.array([[0, 1, 0, 0], [0, 0, 0, 0]], dtype=np.float32) - class_targets2 = np.array([[0, 0, 0, 1], - [0, 0, -1, 0], - [0, 0, 0, 0]], dtype=np.float32) - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - - exp_cls_targets = [[[0, 1, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0], - [1, 0, 0, 0]], - [[1, 0, 0, 0], - [0, 0, 0, 1], - [1, 0, 0, 0], - [1, 0, 0, 0]]] - exp_cls_weights = [[[1, 1, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5]], - [[0.5, 0.5, 0.5, 0.5], - [1, 0.5, 0.5, 1], - [0.5, 0.5, 1, 0.5], - [0.5, 0.5, 0.5, 0.5]]] - exp_reg_targets = [[[0, 0, -0.5, -0.5], - [0, 0, 0, 0], - [0, 0, 0, 0,], - [0, 0, 0, 0,],], - [[0, 0, 0, 0,], - [0, 0.01231521, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 0, 0, 0], - [0, 1, 0, 0]] - - (cls_targets_out, cls_weights_out, reg_targets_out, - reg_weights_out) = self.execute(graph_fn, [ - anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2, groundtruth_weights1, - groundtruth_weights2 - ]) - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - - def test_batch_assign_confidences_multidimensional(self): - - def graph_fn(anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2): - box_list1 = box_list.BoxList(groundtruth_boxlist1) - box_list2 = box_list.BoxList(groundtruth_boxlist2) - gt_box_batch = [box_list1, box_list2] - gt_class_confidences_batch = [class_targets1, class_targets2] - anchors_boxlist = box_list.BoxList(anchor_means) - multiclass_target_assigner = self._get_target_assigner() - target_dimensions = (2, 3) - unmatched_class_label = tf.constant(np.zeros(target_dimensions), - tf.float32) - implicit_class_weight = 0.5 - (cls_targets, cls_weights, reg_targets, reg_weights, - _) = targetassigner.batch_assign_confidences( - multiclass_target_assigner, - anchors_boxlist, - gt_box_batch, - gt_class_confidences_batch, - unmatched_class_label=unmatched_class_label, - include_background_class=True, - implicit_class_weight=implicit_class_weight) - return (cls_targets, cls_weights, reg_targets, reg_weights) - - groundtruth_boxlist1 = np.array([[0., 0., 0.2, 0.2]], dtype=np.float32) - groundtruth_boxlist2 = np.array([[0, 0.25123152, 1, 1], - [0.015789, 0.0985, 0.55789, 0.3842]], - dtype=np.float32) - class_targets1 = np.array([[0, 1, 0, 0]], dtype=np.float32) - class_targets2 = np.array([[0, 0, 0, 1], - [0, 0, 1, 0]], dtype=np.float32) - class_targets1 = np.array([[[0, 1, 1], - [1, 1, 0]]], dtype=np.float32) - class_targets2 = np.array([[[0, 1, 1], - [1, 1, 0]], - [[0, 0, 1], - [0, 0, 1]]], dtype=np.float32) - - anchor_means = np.array([[0, 0, .25, .25], - [0, .25, 1, 1], - [0, .1, .5, .5], - [.75, .75, 1, 1]], dtype=np.float32) - - with self.assertRaises(ValueError): - _, _, _, _ = self.execute(graph_fn, [ - anchor_means, groundtruth_boxlist1, groundtruth_boxlist2, - class_targets1, class_targets2 - ]) - - -class CreateTargetAssignerTest(test_case.TestCase): - - def test_create_target_assigner(self): - """Tests that named constructor gives working target assigners. - - TODO(rathodv): Make this test more general. - """ - corners = [[0.0, 0.0, 1.0, 1.0]] - groundtruth = box_list.BoxList(tf.constant(corners)) - - priors = box_list.BoxList(tf.constant(corners)) - if tf_version.is_tf1(): - multibox_ta = (targetassigner - .create_target_assigner('Multibox', stage='proposal')) - multibox_ta.assign(priors, groundtruth) - # No tests on output, as that may vary arbitrarily as new target assigners - # are added. As long as it is constructed correctly and runs without errors, - # tests on the individual assigners cover correctness of the assignments. - - anchors = box_list.BoxList(tf.constant(corners)) - faster_rcnn_proposals_ta = (targetassigner - .create_target_assigner('FasterRCNN', - stage='proposal')) - faster_rcnn_proposals_ta.assign(anchors, groundtruth) - - fast_rcnn_ta = (targetassigner - .create_target_assigner('FastRCNN')) - fast_rcnn_ta.assign(anchors, groundtruth) - - faster_rcnn_detection_ta = (targetassigner - .create_target_assigner('FasterRCNN', - stage='detection')) - faster_rcnn_detection_ta.assign(anchors, groundtruth) - - with self.assertRaises(ValueError): - targetassigner.create_target_assigner('InvalidDetector', - stage='invalid_stage') - - -def _array_argmax(array): - return np.unravel_index(np.argmax(array), array.shape) - - -class CenterNetCenterHeatmapTargetAssignerTest(test_case.TestCase, - parameterized.TestCase): - - def setUp(self): - super(CenterNetCenterHeatmapTargetAssignerTest, self).setUp() - - self._box_center = [0.0, 0.0, 1.0, 1.0] - self._box_center_small = [0.25, 0.25, 0.75, 0.75] - self._box_lower_left = [0.5, 0.0, 1.0, 0.5] - self._box_center_offset = [0.1, 0.05, 1.0, 1.0] - self._box_odd_coordinates = [0.1625, 0.2125, 0.5625, 0.9625] - - def test_center_location(self): - """Test that the centers are at the correct location.""" - def graph_fn(): - box_batch = [tf.constant([self._box_center, self._box_lower_left])] - classes = [ - tf.one_hot([0, 1], depth=4), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner(4) - targets = assigner.assign_center_targets_from_boxes(80, 80, box_batch, - classes) - return targets - targets = self.execute(graph_fn, []) - self.assertEqual((10, 10), _array_argmax(targets[0, :, :, 0])) - self.assertAlmostEqual(1.0, targets[0, 10, 10, 0]) - self.assertEqual((15, 5), _array_argmax(targets[0, :, :, 1])) - self.assertAlmostEqual(1.0, targets[0, 15, 5, 1]) - - @parameterized.parameters( - {'keypoint_weights_for_center': [1.0, 1.0, 1.0, 1.0]}, - {'keypoint_weights_for_center': [0.0, 0.0, 1.0, 1.0]}, - ) - def test_center_location_by_keypoints(self, keypoint_weights_for_center): - """Test that the centers are at the correct location.""" - kpts_y = [[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8], [0.0, 0.0, 0.0, 0.0]] - kpts_x = [[0.5, 0.6, 0.7, 0.8], [0.1, 0.2, 0.3, 0.4], [0.0, 0.0, 0.0, 0.0]] - gt_keypoints_list = [ - tf.stack([tf.constant(kpts_y), tf.constant(kpts_x)], axis=2) - ] - kpts_weight = [[1.0, 1.0, 1.0, 1.0], [1.0, 0.0, 1.0, 0.0], - [1.0, 0.0, 1.0, 0.0]] - gt_keypoints_weights_list = [tf.constant(kpts_weight)] - gt_classes_list = [ - tf.one_hot([0, 0, 0], depth=1), - ] - gt_weights_list = [tf.constant([1.0, 1.0, 0.0])] - - def graph_fn(): - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 4, - keypoint_class_id=0, - keypoint_indices=[0, 1, 2, 3], - keypoint_weights_for_center=keypoint_weights_for_center) - targets = assigner.assign_center_targets_from_keypoints( - 80, - 80, - gt_classes_list=gt_classes_list, - gt_keypoints_list=gt_keypoints_list, - gt_weights_list=gt_weights_list, - gt_keypoints_weights_list=gt_keypoints_weights_list) - return targets - - targets = self.execute(graph_fn, []) - - if sum(keypoint_weights_for_center) == 4.0: - # There should be two peaks at location (5, 13), and (12, 4). - # (5, 13) = ((0.1 + 0.2 + 0.3 + 0.4) / 4 * 80 / 4, - # (0.5 + 0.6 + 0.7 + 0.8) / 4 * 80 / 4) - # (12, 4) = ((0.5 + 0.7) / 2 * 80 / 4, - # (0.1 + 0.3) / 2 * 80 / 4) - self.assertEqual((5, 13), _array_argmax(targets[0, :, :, 0])) - self.assertAlmostEqual(1.0, targets[0, 5, 13, 0]) - self.assertEqual((1, 20, 20, 1), targets.shape) - targets[0, 5, 13, 0] = 0.0 - self.assertEqual((12, 4), _array_argmax(targets[0, :, :, 0])) - self.assertAlmostEqual(1.0, targets[0, 12, 4, 0]) - else: - # There should be two peaks at location (5, 13), and (12, 4). - # (7, 15) = ((0.3 + 0.4) / 2 * 80 / 4, - # (0.7 + 0.8) / 2 * 80 / 4) - # (14, 6) = (0.7 * 80 / 4, 0.3 * 80 / 4) - self.assertEqual((7, 15), _array_argmax(targets[0, :, :, 0])) - self.assertAlmostEqual(1.0, targets[0, 7, 15, 0]) - self.assertEqual((1, 20, 20, 1), targets.shape) - targets[0, 7, 15, 0] = 0.0 - self.assertEqual((14, 6), _array_argmax(targets[0, :, :, 0])) - self.assertAlmostEqual(1.0, targets[0, 14, 6, 0]) - - def test_center_batch_shape(self): - """Test that the shape of the target for a batch is correct.""" - def graph_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_center]), - tf.constant([self._box_center_small]), - ] - classes = [ - tf.one_hot([0, 1], depth=4), - tf.one_hot([2], depth=4), - tf.one_hot([3], depth=4), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner(4) - targets = assigner.assign_center_targets_from_boxes(80, 80, box_batch, - classes) - return targets - targets = self.execute(graph_fn, []) - self.assertEqual((3, 20, 20, 4), targets.shape) - - def test_center_overlap_maximum(self): - """Test that when boxes overlap we, are computing the maximum.""" - def graph_fn(): - box_batch = [ - tf.constant([ - self._box_center, self._box_center_offset, self._box_center, - self._box_center_offset - ]) - ] - classes = [ - tf.one_hot([0, 0, 1, 2], depth=4), - ] - - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner(4) - targets = assigner.assign_center_targets_from_boxes(80, 80, box_batch, - classes) - return targets - targets = self.execute(graph_fn, []) - class0_targets = targets[0, :, :, 0] - class1_targets = targets[0, :, :, 1] - class2_targets = targets[0, :, :, 2] - np.testing.assert_allclose(class0_targets, - np.maximum(class1_targets, class2_targets)) - - def test_size_blur(self): - """Test that the heatmap of a larger box is more blurred.""" - def graph_fn(): - box_batch = [tf.constant([self._box_center, self._box_center_small])] - - classes = [ - tf.one_hot([0, 1], depth=4), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner(4) - targets = assigner.assign_center_targets_from_boxes(80, 80, box_batch, - classes) - return targets - targets = self.execute(graph_fn, []) - self.assertGreater( - np.count_nonzero(targets[:, :, :, 0]), - np.count_nonzero(targets[:, :, :, 1])) - - def test_weights(self): - """Test that the weights correctly ignore ground truth.""" - def graph1_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_center]), - tf.constant([self._box_center_small]), - ] - classes = [ - tf.one_hot([0, 1], depth=4), - tf.one_hot([2], depth=4), - tf.one_hot([3], depth=4), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner(4) - targets = assigner.assign_center_targets_from_boxes(80, 80, box_batch, - classes) - return targets - - targets = self.execute(graph1_fn, []) - self.assertAlmostEqual(1.0, targets[0, :, :, 0].max()) - self.assertAlmostEqual(1.0, targets[0, :, :, 1].max()) - self.assertAlmostEqual(1.0, targets[1, :, :, 2].max()) - self.assertAlmostEqual(1.0, targets[2, :, :, 3].max()) - self.assertAlmostEqual(0.0, targets[0, :, :, [2, 3]].max()) - self.assertAlmostEqual(0.0, targets[1, :, :, [0, 1, 3]].max()) - self.assertAlmostEqual(0.0, targets[2, :, :, :3].max()) - - def graph2_fn(): - weights = [ - tf.constant([0., 1.]), - tf.constant([1.]), - tf.constant([1.]), - ] - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_center]), - tf.constant([self._box_center_small]), - ] - classes = [ - tf.one_hot([0, 1], depth=4), - tf.one_hot([2], depth=4), - tf.one_hot([3], depth=4), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner(4) - targets = assigner.assign_center_targets_from_boxes(80, 80, box_batch, - classes, - weights) - return targets - targets = self.execute(graph2_fn, []) - self.assertAlmostEqual(1.0, targets[0, :, :, 1].max()) - self.assertAlmostEqual(1.0, targets[1, :, :, 2].max()) - self.assertAlmostEqual(1.0, targets[2, :, :, 3].max()) - self.assertAlmostEqual(0.0, targets[0, :, :, [0, 2, 3]].max()) - self.assertAlmostEqual(0.0, targets[1, :, :, [0, 1, 3]].max()) - self.assertAlmostEqual(0.0, targets[2, :, :, :3].max()) - - def test_low_overlap(self): - def graph1_fn(): - box_batch = [tf.constant([self._box_center])] - classes = [ - tf.one_hot([0], depth=2), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 4, min_overlap=0.1) - targets_low_overlap = assigner.assign_center_targets_from_boxes( - 80, 80, box_batch, classes) - return targets_low_overlap - targets_low_overlap = self.execute(graph1_fn, []) - self.assertLess(1, np.count_nonzero(targets_low_overlap)) - - def graph2_fn(): - box_batch = [tf.constant([self._box_center])] - classes = [ - tf.one_hot([0], depth=2), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 4, min_overlap=0.6) - targets_medium_overlap = assigner.assign_center_targets_from_boxes( - 80, 80, box_batch, classes) - return targets_medium_overlap - targets_medium_overlap = self.execute(graph2_fn, []) - self.assertLess(1, np.count_nonzero(targets_medium_overlap)) - - def graph3_fn(): - box_batch = [tf.constant([self._box_center])] - classes = [ - tf.one_hot([0], depth=2), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 4, min_overlap=0.99) - targets_high_overlap = assigner.assign_center_targets_from_boxes( - 80, 80, box_batch, classes) - return targets_high_overlap - - targets_high_overlap = self.execute(graph3_fn, []) - self.assertTrue(np.all(targets_low_overlap >= targets_medium_overlap)) - self.assertTrue(np.all(targets_medium_overlap >= targets_high_overlap)) - - def test_empty_box_list(self): - """Test that an empty box list gives an all 0 heatmap.""" - def graph_fn(): - box_batch = [ - tf.zeros((0, 4), dtype=tf.float32), - ] - - classes = [ - tf.zeros((0, 5), dtype=tf.float32), - ] - - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 4, min_overlap=0.1) - targets = assigner.assign_center_targets_from_boxes( - 80, 80, box_batch, classes) - return targets - targets = self.execute(graph_fn, []) - np.testing.assert_allclose(targets, 0.) - - -class CenterNetBoxTargetAssignerTest(test_case.TestCase): - - def setUp(self): - super(CenterNetBoxTargetAssignerTest, self).setUp() - self._box_center = [0.0, 0.0, 1.0, 1.0] - self._box_center_small = [0.25, 0.25, 0.75, 0.75] - self._box_lower_left = [0.5, 0.0, 1.0, 0.5] - self._box_center_offset = [0.1, 0.05, 1.0, 1.0] - self._box_odd_coordinates = [0.1625, 0.2125, 0.5625, 0.9625] - - def test_max_distance_for_overlap(self): - """Test that the distance ensures the IoU with random boxes.""" - - # TODO(vighneshb) remove this after the `_smallest_positive_root` - # function if fixed. - self.skipTest(('Skipping test because we are using an incorrect version of' - 'the `max_distance_for_overlap` function to reproduce' - ' results.')) - - rng = np.random.RandomState(0) - n_samples = 100 - - width = rng.uniform(1, 100, size=n_samples) - height = rng.uniform(1, 100, size=n_samples) - min_iou = rng.uniform(0.1, 1.0, size=n_samples) - - def graph_fn(): - max_dist = targetassigner.max_distance_for_overlap(height, width, min_iou) - return max_dist - max_dist = self.execute(graph_fn, []) - xmin1 = np.zeros(n_samples) - ymin1 = np.zeros(n_samples) - xmax1 = np.zeros(n_samples) + width - ymax1 = np.zeros(n_samples) + height - - xmin2 = max_dist * np.cos(rng.uniform(0, 2 * np.pi)) - ymin2 = max_dist * np.sin(rng.uniform(0, 2 * np.pi)) - xmax2 = width + max_dist * np.cos(rng.uniform(0, 2 * np.pi)) - ymax2 = height + max_dist * np.sin(rng.uniform(0, 2 * np.pi)) - - boxes1 = np.vstack([ymin1, xmin1, ymax1, xmax1]).T - boxes2 = np.vstack([ymin2, xmin2, ymax2, xmax2]).T - - iou = np.diag(np_box_ops.iou(boxes1, boxes2)) - - self.assertTrue(np.all(iou >= min_iou)) - - def test_max_distance_for_overlap_centernet(self): - """Test the version of the function used in the CenterNet paper.""" - - def graph_fn(): - distance = targetassigner.max_distance_for_overlap(10, 5, 0.5) - return distance - distance = self.execute(graph_fn, []) - self.assertAlmostEqual(2.807764064, distance) - - def test_assign_size_and_offset_targets(self): - """Test the assign_size_and_offset_targets function.""" - def graph_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_center_offset]), - tf.constant([self._box_center_small, self._box_odd_coordinates]), - ] - - assigner = targetassigner.CenterNetBoxTargetAssigner(4) - indices, hw, yx_offset, weights = assigner.assign_size_and_offset_targets( - 80, 80, box_batch) - return indices, hw, yx_offset, weights - indices, hw, yx_offset, weights = self.execute(graph_fn, []) - self.assertEqual(indices.shape, (5, 3)) - self.assertEqual(hw.shape, (5, 2)) - self.assertEqual(yx_offset.shape, (5, 2)) - self.assertEqual(weights.shape, (5,)) - np.testing.assert_array_equal( - indices, - [[0, 10, 10], [0, 15, 5], [1, 11, 10], [2, 10, 10], [2, 7, 11]]) - np.testing.assert_array_equal( - hw, [[20, 20], [10, 10], [18, 19], [10, 10], [8, 15]]) - np.testing.assert_array_equal( - yx_offset, [[0, 0], [0, 0], [0, 0.5], [0, 0], [0.25, 0.75]]) - np.testing.assert_array_equal(weights, 1) - - def test_assign_size_and_offset_targets_weights(self): - """Test the assign_size_and_offset_targets function with box weights.""" - def graph_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_lower_left, self._box_center_small]), - tf.constant([self._box_center_small, self._box_odd_coordinates]), - ] - - cn_assigner = targetassigner.CenterNetBoxTargetAssigner(4) - weights_batch = [ - tf.constant([0.0, 1.0]), - tf.constant([1.0, 1.0]), - tf.constant([0.0, 0.0]) - ] - indices, hw, yx_offset, weights = cn_assigner.assign_size_and_offset_targets( - 80, 80, box_batch, weights_batch) - return indices, hw, yx_offset, weights - indices, hw, yx_offset, weights = self.execute(graph_fn, []) - self.assertEqual(indices.shape, (6, 3)) - self.assertEqual(hw.shape, (6, 2)) - self.assertEqual(yx_offset.shape, (6, 2)) - self.assertEqual(weights.shape, (6,)) - np.testing.assert_array_equal(indices, - [[0, 10, 10], [0, 15, 5], [1, 15, 5], - [1, 10, 10], [2, 10, 10], [2, 7, 11]]) - np.testing.assert_array_equal( - hw, [[20, 20], [10, 10], [10, 10], [10, 10], [10, 10], [8, 15]]) - np.testing.assert_array_equal( - yx_offset, [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0.25, 0.75]]) - np.testing.assert_array_equal(weights, [0, 1, 1, 1, 0, 0]) - - def test_get_batch_predictions_from_indices(self): - """Test the get_batch_predictions_from_indices function. - - This test verifies that the indices returned by - assign_size_and_offset_targets function work as expected with a predicted - tensor. - - """ - def graph_fn(): - pred_array = np.ones((2, 40, 20, 2), dtype=np.int32) * -1000 - pred_array[0, 20, 10] = [1, 2] - pred_array[0, 30, 5] = [3, 4] - pred_array[1, 20, 10] = [5, 6] - pred_array[1, 14, 11] = [7, 8] - pred_tensor = tf.constant(pred_array) - - indices = tf.constant([ - [0, 20, 10], - [0, 30, 5], - [1, 20, 10], - [1, 14, 11] - ], dtype=tf.int32) - - preds = targetassigner.get_batch_predictions_from_indices( - pred_tensor, indices) - return preds - preds = self.execute(graph_fn, []) - np.testing.assert_array_equal(preds, [[1, 2], [3, 4], [5, 6], [7, 8]]) - - def test_get_batch_predictions_from_indices_with_class(self): - """Test the get_batch_predictions_from_indices function with class axis. - - This test verifies that the indices returned by - assign_size_and_offset_targets function work as expected with a predicted - tensor. - - """ - def graph_fn(): - pred_array = np.ones((2, 40, 20, 5, 2), dtype=np.int32) * -1000 - pred_array[0, 20, 10, 0] = [1, 2] - pred_array[0, 30, 5, 2] = [3, 4] - pred_array[1, 20, 10, 1] = [5, 6] - pred_array[1, 14, 11, 4] = [7, 8] - pred_tensor = tf.constant(pred_array) - - indices = tf.constant([ - [0, 20, 10, 0], - [0, 30, 5, 2], - [1, 20, 10, 1], - [1, 14, 11, 4] - ], dtype=tf.int32) - - preds = targetassigner.get_batch_predictions_from_indices( - pred_tensor, indices) - return preds - preds = self.execute(graph_fn, []) - np.testing.assert_array_equal(preds, [[1, 2], [3, 4], [5, 6], [7, 8]]) - - -class CenterNetIOUTargetAssignerTest(test_case.TestCase): - - def setUp(self): - super(CenterNetIOUTargetAssignerTest, self).setUp() - - self._box_center = [0.0, 0.0, 1.0, 1.0] - self._box_center_small = [0.25, 0.25, 0.75, 0.75] - self._box_lower_left = [0.5, 0.0, 1.0, 0.5] - self._box_center_offset = [0.1, 0.05, 1.0, 1.0] - self._box_odd_coordinates = [0.1625, 0.2125, 0.5625, 0.9625] - - def test_center_location(self): - """Test that the centers are at the correct location.""" - def graph_fn(): - box_batch = [tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_lower_left, self._box_center])] - classes = [ - tf.one_hot([0, 1], depth=4), - tf.one_hot([2, 2], depth=4) - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 4, box_heatmap_type='iou') - targets = assigner.assign_center_targets_from_boxes( - 80, 80, box_batch, classes) - return targets - targets = self.execute(graph_fn, []) - self.assertEqual((10, 10), _array_argmax(targets[0, :, :, 0])) - self.assertAlmostEqual(1.0, targets[0, 10, 10, 0]) - self.assertEqual((15, 5), _array_argmax(targets[0, :, :, 1])) - self.assertAlmostEqual(1.0, targets[0, 15, 5, 1]) - - self.assertAlmostEqual(1.0, targets[1, 15, 5, 2]) - self.assertAlmostEqual(1.0, targets[1, 10, 10, 2]) - self.assertAlmostEqual(0.0, targets[1, 0, 19, 1]) - - def test_exponent(self): - """Test that the centers are at the correct location.""" - def graph_fn(): - box_batch = [tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_lower_left, self._box_center])] - classes = [ - tf.one_hot([0], depth=2), - ] - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 1, box_heatmap_type='iou') - targets = assigner.assign_center_targets_from_boxes( - 4, 4, box_batch, classes) - - assigner = targetassigner.CenterNetCenterHeatmapTargetAssigner( - 1, box_heatmap_type='iou', heatmap_exponent=0.5) - targets_pow = assigner.assign_center_targets_from_boxes( - 4, 4, box_batch, classes) - return targets, targets_pow - - targets, targets_pow = self.execute(graph_fn, []) - self.assertLess(targets[0, 2, 3, 0], 1.0) - self.assertLess(targets_pow[0, 2, 3, 0], 1.0) - self.assertAlmostEqual(targets[0, 2, 3, 0], targets_pow[0, 2, 3, 0] ** 2) - - -class CenterNetKeypointTargetAssignerTest(test_case.TestCase): - - def test_keypoint_heatmap_targets(self): - def graph_fn(): - gt_classes_list = [ - tf.one_hot([0, 1, 0, 1], depth=4), - ] - coordinates = tf.expand_dims( - tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, float('nan'), 0.9, 1.0], - [0.4, 0.1, 0.4, 0.2, 0.1], - [float('nan'), 0.1, 0.5, 0.7, 0.6]]), - dtype=tf.float32), - axis=2) - gt_keypoints_list = [tf.concat([coordinates, coordinates], axis=2)] - gt_boxes_list = [ - tf.constant( - np.array([[0.0, 0.0, 0.3, 0.3], - [0.0, 0.0, 0.5, 0.5], - [0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 1.0, 1.0]]), - dtype=tf.float32) - ] - - cn_assigner = targetassigner.CenterNetKeypointTargetAssigner( - stride=4, - class_id=1, - keypoint_indices=[0, 2]) - (targets, num_instances_batch, - valid_mask) = cn_assigner.assign_keypoint_heatmap_targets( - 120, - 80, - gt_keypoints_list, - gt_classes_list, - gt_boxes_list=gt_boxes_list) - return targets, num_instances_batch, valid_mask - - targets, num_instances_batch, valid_mask = self.execute(graph_fn, []) - # keypoint (0.5, 0.5) is selected. The peak is expected to appear at the - # center of the image. - self.assertEqual((15, 10), _array_argmax(targets[0, :, :, 1])) - self.assertAlmostEqual(1.0, targets[0, 15, 10, 1]) - # No peak for the first class since NaN is selected. - self.assertAlmostEqual(0.0, targets[0, 15, 10, 0]) - # Verify the output heatmap shape. - self.assertAllEqual([1, 30, 20, 2], targets.shape) - # Verify the number of instances is correct. - np.testing.assert_array_almost_equal([[0, 1]], - num_instances_batch) - self.assertAllEqual([1, 30, 20, 2], valid_mask.shape) - # When calling the function, we specify the class id to be 1 (1th and 3rd) - # instance and the keypoint indices to be [0, 2], meaning that the 1st - # instance is the target class with no valid keypoints in it. As a result, - # the region of both keypoint types of the 1st instance boxing box should be - # blacked out (0.0, 0.0, 0.5, 0.5), transfering to (0, 0, 15, 10) in - # absolute output space. - self.assertAlmostEqual(np.sum(valid_mask[:, 0:15, 0:10, 0:2]), 0.0) - # For the 2nd instance, only the 1st keypoint has visibility of 0 so only - # the corresponding valid mask contains zeros. - self.assertAlmostEqual(np.sum(valid_mask[:, 15:30, 10:20, 0]), 0.0) - # All other values are 1.0 so the sum is: - # 30 * 20 * 2 - 15 * 10 * 2 - 15 * 10 * 1 = 750. - self.assertAlmostEqual(np.sum(valid_mask), 750.0) - - def test_assign_keypoints_offset_targets(self): - def graph_fn(): - gt_classes_list = [ - tf.one_hot([0, 1, 0, 1], depth=4), - ] - coordinates = tf.expand_dims( - tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, float('nan'), 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [float('nan'), 0.0, 0.12, 0.7, 0.4]]), - dtype=tf.float32), - axis=2) - gt_keypoints_list = [tf.concat([coordinates, coordinates], axis=2)] - - cn_assigner = targetassigner.CenterNetKeypointTargetAssigner( - stride=4, - class_id=1, - keypoint_indices=[0, 2]) - (indices, offsets, weights) = cn_assigner.assign_keypoints_offset_targets( - height=120, - width=80, - gt_keypoints_list=gt_keypoints_list, - gt_classes_list=gt_classes_list) - return indices, weights, offsets - indices, weights, offsets = self.execute(graph_fn, []) - # Only the last element has positive weight. - np.testing.assert_array_almost_equal( - [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], weights) - # Validate the last element's indices and offsets. - np.testing.assert_array_equal([0, 3, 2], indices[7, :]) - np.testing.assert_array_almost_equal([0.6, 0.4], offsets[7, :]) - - def test_assign_keypoint_depths_target(self): - def graph_fn(): - gt_classes_list = [ - tf.one_hot([0, 1, 0, 1], depth=4), - ] - coordinates = tf.expand_dims( - tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, 0.7, 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [float('nan'), 0.0, 0.12, 0.7, 0.4]]), - dtype=tf.float32), - axis=2) - gt_keypoints_list = [tf.concat([coordinates, coordinates], axis=2)] - depths = tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, float('nan'), 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [0.5, 0.0, 7.0, 0.7, 0.4]]), - dtype=tf.float32) - gt_keypoint_depths_list = [depths] - - gt_keypoint_depth_weights = tf.constant( - np.array([[1.0, 1.0, 1.0, 1.0, 1.0], - [float('nan'), 0.0, 1.0, 0.0, 0.0], - [1.0, 1.0, 1.0, 1.0, 1.0], - [1.0, 1.0, 0.5, 1.0, 1.0]]), - dtype=tf.float32) - gt_keypoint_depth_weights_list = [gt_keypoint_depth_weights] - - cn_assigner = targetassigner.CenterNetKeypointTargetAssigner( - stride=4, - class_id=1, - keypoint_indices=[0, 2], - peak_radius=1) - (indices, depths, weights) = cn_assigner.assign_keypoints_depth_targets( - height=120, - width=80, - gt_keypoints_list=gt_keypoints_list, - gt_classes_list=gt_classes_list, - gt_keypoint_depths_list=gt_keypoint_depths_list, - gt_keypoint_depth_weights_list=gt_keypoint_depth_weights_list) - return indices, depths, weights - indices, depths, weights = self.execute(graph_fn, []) - - # Only the last 5 elements has positive weight. - np.testing.assert_array_almost_equal([ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.5 - ], weights) - # Validate the last 5 elements' depth value. - np.testing.assert_array_almost_equal( - [7.0, 7.0, 7.0, 7.0, 7.0], depths[35:, 0]) - self.assertEqual((40, 3), indices.shape) - np.testing.assert_array_equal([0, 2, 2], indices[35, :]) - - def test_assign_keypoint_depths_per_keypoints(self): - def graph_fn(): - gt_classes_list = [ - tf.one_hot([0, 1, 0, 1], depth=4), - ] - coordinates = tf.expand_dims( - tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, 0.7, 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [float('nan'), 0.0, 0.12, 0.7, 0.4]]), - dtype=tf.float32), - axis=2) - gt_keypoints_list = [tf.concat([coordinates, coordinates], axis=2)] - depths = tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, float('nan'), 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [0.5, 0.0, 7.0, 0.7, 0.4]]), - dtype=tf.float32) - gt_keypoint_depths_list = [depths] - - gt_keypoint_depth_weights = tf.constant( - np.array([[1.0, 1.0, 1.0, 1.0, 1.0], - [float('nan'), 0.0, 1.0, 0.0, 0.0], - [1.0, 1.0, 1.0, 1.0, 1.0], - [1.0, 1.0, 0.5, 1.0, 1.0]]), - dtype=tf.float32) - gt_keypoint_depth_weights_list = [gt_keypoint_depth_weights] - - cn_assigner = targetassigner.CenterNetKeypointTargetAssigner( - stride=4, - class_id=1, - keypoint_indices=[0, 2], - peak_radius=1, - per_keypoint_depth=True) - (indices, depths, weights) = cn_assigner.assign_keypoints_depth_targets( - height=120, - width=80, - gt_keypoints_list=gt_keypoints_list, - gt_classes_list=gt_classes_list, - gt_keypoint_depths_list=gt_keypoint_depths_list, - gt_keypoint_depth_weights_list=gt_keypoint_depth_weights_list) - return indices, depths, weights - indices, depths, weights = self.execute(graph_fn, []) - - # Only the last 5 elements has positive weight. - np.testing.assert_array_almost_equal([ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.5 - ], weights) - # Validate the last 5 elements' depth value. - np.testing.assert_array_almost_equal( - [7.0, 7.0, 7.0, 7.0, 7.0], depths[35:, 0]) - self.assertEqual((40, 4), indices.shape) - np.testing.assert_array_equal([0, 2, 2, 1], indices[35, :]) - - def test_assign_keypoints_offset_targets_radius(self): - def graph_fn(): - gt_classes_list = [ - tf.one_hot([0, 1, 0, 1], depth=4), - ] - coordinates = tf.expand_dims( - tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, float('nan'), 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [float('nan'), 0.0, 0.12, 0.7, 0.4]]), - dtype=tf.float32), - axis=2) - gt_keypoints_list = [tf.concat([coordinates, coordinates], axis=2)] - - cn_assigner = targetassigner.CenterNetKeypointTargetAssigner( - stride=4, - class_id=1, - keypoint_indices=[0, 2], - peak_radius=1, - per_keypoint_offset=True) - (indices, offsets, weights) = cn_assigner.assign_keypoints_offset_targets( - height=120, - width=80, - gt_keypoints_list=gt_keypoints_list, - gt_classes_list=gt_classes_list) - return indices, weights, offsets - indices, weights, offsets = self.execute(graph_fn, []) - - # There are total 8 * 5 (neighbors) = 40 targets. - self.assertAllEqual(indices.shape, [40, 4]) - self.assertAllEqual(offsets.shape, [40, 2]) - self.assertAllEqual(weights.shape, [40]) - # Only the last 5 (radius 1 generates 5 valid points) element has positive - # weight. - np.testing.assert_array_almost_equal([ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0 - ], weights) - # Validate the last element's (with neighbors) indices and offsets. - np.testing.assert_array_equal([0, 2, 2, 1], indices[35, :]) - np.testing.assert_array_equal([0, 3, 1, 1], indices[36, :]) - np.testing.assert_array_equal([0, 3, 2, 1], indices[37, :]) - np.testing.assert_array_equal([0, 3, 3, 1], indices[38, :]) - np.testing.assert_array_equal([0, 4, 2, 1], indices[39, :]) - np.testing.assert_array_almost_equal([1.6, 0.4], offsets[35, :]) - np.testing.assert_array_almost_equal([0.6, 1.4], offsets[36, :]) - np.testing.assert_array_almost_equal([0.6, 0.4], offsets[37, :]) - np.testing.assert_array_almost_equal([0.6, -0.6], offsets[38, :]) - np.testing.assert_array_almost_equal([-0.4, 0.4], offsets[39, :]) - - def test_assign_joint_regression_targets(self): - def graph_fn(): - gt_boxes_list = [ - tf.constant( - np.array([[0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 1.0, 1.0]]), - dtype=tf.float32) - ] - gt_classes_list = [ - tf.one_hot([0, 1, 0, 1], depth=4), - ] - coordinates = tf.expand_dims( - tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, float('nan'), 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [float('nan'), 0.0, 0.12, 0.7, 0.4]]), - dtype=tf.float32), - axis=2) - gt_keypoints_list = [tf.concat([coordinates, coordinates], axis=2)] - - cn_assigner = targetassigner.CenterNetKeypointTargetAssigner( - stride=4, - class_id=1, - keypoint_indices=[0, 2]) - (indices, offsets, weights) = cn_assigner.assign_joint_regression_targets( - height=120, - width=80, - gt_keypoints_list=gt_keypoints_list, - gt_classes_list=gt_classes_list, - gt_boxes_list=gt_boxes_list) - return indices, offsets, weights - indices, offsets, weights = self.execute(graph_fn, []) - np.testing.assert_array_almost_equal( - [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], weights) - np.testing.assert_array_equal([0, 15, 10, 1], indices[7, :]) - np.testing.assert_array_almost_equal([-11.4, -7.6], offsets[7, :]) - - def test_assign_joint_regression_targets_radius(self): - def graph_fn(): - gt_boxes_list = [ - tf.constant( - np.array([[0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 1.0, 1.0]]), - dtype=tf.float32) - ] - gt_classes_list = [ - tf.one_hot([0, 1, 0, 1], depth=4), - ] - coordinates = tf.expand_dims( - tf.constant( - np.array([[0.1, 0.2, 0.3, 0.4, 0.5], - [float('nan'), 0.7, float('nan'), 0.9, 0.4], - [0.4, 0.1, 0.4, 0.2, 0.0], - [float('nan'), 0.0, 0.12, 0.7, 0.4]]), - dtype=tf.float32), - axis=2) - gt_keypoints_list = [tf.concat([coordinates, coordinates], axis=2)] - - cn_assigner = targetassigner.CenterNetKeypointTargetAssigner( - stride=4, - class_id=1, - keypoint_indices=[0, 2], - peak_radius=1) - (indices, offsets, weights) = cn_assigner.assign_joint_regression_targets( - height=120, - width=80, - gt_keypoints_list=gt_keypoints_list, - gt_classes_list=gt_classes_list, - gt_boxes_list=gt_boxes_list) - return indices, offsets, weights - indices, offsets, weights = self.execute(graph_fn, []) - - # There are total 8 * 5 (neighbors) = 40 targets. - self.assertAllEqual(indices.shape, [40, 4]) - self.assertAllEqual(offsets.shape, [40, 2]) - self.assertAllEqual(weights.shape, [40]) - # Only the last 5 (radius 1 generates 5 valid points) element has positive - # weight. - np.testing.assert_array_almost_equal([ - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, - 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0 - ], weights) - # Test the values of the indices and offsets of the last 5 elements. - np.testing.assert_array_equal([0, 14, 10, 1], indices[35, :]) - np.testing.assert_array_equal([0, 15, 9, 1], indices[36, :]) - np.testing.assert_array_equal([0, 15, 10, 1], indices[37, :]) - np.testing.assert_array_equal([0, 15, 11, 1], indices[38, :]) - np.testing.assert_array_equal([0, 16, 10, 1], indices[39, :]) - np.testing.assert_array_almost_equal([-10.4, -7.6], offsets[35, :]) - np.testing.assert_array_almost_equal([-11.4, -6.6], offsets[36, :]) - np.testing.assert_array_almost_equal([-11.4, -7.6], offsets[37, :]) - np.testing.assert_array_almost_equal([-11.4, -8.6], offsets[38, :]) - np.testing.assert_array_almost_equal([-12.4, -7.6], offsets[39, :]) - - -class CenterNetMaskTargetAssignerTest(test_case.TestCase): - - def test_assign_segmentation_targets(self): - def graph_fn(): - gt_masks_list = [ - # Example 0. - tf.constant([ - [ - [1., 0., 0., 0.], - [1., 1., 0., 0.], - [0., 0., 0., 0.], - [0., 0., 0., 0.], - ], - [ - [0., 0., 0., 0.], - [0., 0., 0., 1.], - [0., 0., 0., 0.], - [0., 0., 0., 0.], - ], - [ - [1., 1., 0., 0.], - [1., 1., 0., 0.], - [0., 0., 1., 1.], - [0., 0., 1., 1.], - ] - ], dtype=tf.float32), - # Example 1. - tf.constant([ - [ - [1., 1., 0., 1.], - [1., 1., 1., 1.], - [0., 0., 1., 1.], - [0., 0., 0., 1.], - ], - [ - [0., 0., 0., 0.], - [0., 0., 0., 0.], - [1., 1., 0., 0.], - [1., 1., 0., 0.], - ], - ], dtype=tf.float32), - ] - gt_classes_list = [ - # Example 0. - tf.constant([[1., 0., 0.], - [0., 1., 0.], - [1., 0., 0.]], dtype=tf.float32), - # Example 1. - tf.constant([[0., 1., 0.], - [0., 1., 0.]], dtype=tf.float32) - ] - gt_boxes_list = [ - # Example 0. - tf.constant([[0.0, 0.0, 0.5, 0.5], - [0.0, 0.5, 0.5, 1.0], - [0.0, 0.0, 1.0, 1.0]], dtype=tf.float32), - # Example 1. - tf.constant([[0.0, 0.0, 1.0, 1.0], - [0.5, 0.0, 1.0, 0.5]], dtype=tf.float32) - ] - gt_mask_weights_list = [ - # Example 0. - tf.constant([0.0, 1.0, 1.0], dtype=tf.float32), - # Example 1. - tf.constant([1.0, 1.0], dtype=tf.float32) - ] - cn_assigner = targetassigner.CenterNetMaskTargetAssigner(stride=2) - segmentation_target, segmentation_weight = ( - cn_assigner.assign_segmentation_targets( - gt_masks_list=gt_masks_list, - gt_classes_list=gt_classes_list, - gt_boxes_list=gt_boxes_list, - gt_mask_weights_list=gt_mask_weights_list, - mask_resize_method=targetassigner.ResizeMethod.NEAREST_NEIGHBOR)) - return segmentation_target, segmentation_weight - segmentation_target, segmentation_weight = self.execute(graph_fn, []) - - expected_seg_target = np.array([ - # Example 0 [[class 0, class 1], [background, class 0]] - [[[1, 0, 0], [0, 1, 0]], - [[0, 0, 0], [1, 0, 0]]], - # Example 1 [[class 1, class 1], [class 1, class 1]] - [[[0, 1, 0], [0, 1, 0]], - [[0, 1, 0], [0, 1, 0]]], - ], dtype=np.float32) - np.testing.assert_array_almost_equal( - expected_seg_target, segmentation_target) - expected_seg_weight = np.array([ - [[0, 1], [1, 1]], - [[1, 1], [1, 1]]], dtype=np.float32) - np.testing.assert_array_almost_equal( - expected_seg_weight, segmentation_weight) - - def test_assign_segmentation_targets_no_objects(self): - def graph_fn(): - gt_masks_list = [tf.zeros((0, 5, 5))] - gt_classes_list = [tf.zeros((0, 10))] - cn_assigner = targetassigner.CenterNetMaskTargetAssigner(stride=1) - segmentation_target, _ = cn_assigner.assign_segmentation_targets( - gt_masks_list=gt_masks_list, - gt_classes_list=gt_classes_list, - mask_resize_method=targetassigner.ResizeMethod.NEAREST_NEIGHBOR) - return segmentation_target - - segmentation_target = self.execute(graph_fn, []) - expected_seg_target = np.zeros((1, 5, 5, 10)) - np.testing.assert_array_almost_equal( - expected_seg_target, segmentation_target) - - -class CenterNetDensePoseTargetAssignerTest(test_case.TestCase): - - def test_assign_part_and_coordinate_targets(self): - def graph_fn(): - gt_dp_num_points_list = [ - # Example 0. - tf.constant([2, 0, 3], dtype=tf.int32), - # Example 1. - tf.constant([1, 1], dtype=tf.int32), - ] - gt_dp_part_ids_list = [ - # Example 0. - tf.constant([[1, 6, 0], - [0, 0, 0], - [0, 2, 3]], dtype=tf.int32), - # Example 1. - tf.constant([[7, 0, 0], - [0, 0, 0]], dtype=tf.int32), - ] - gt_dp_surface_coords_list = [ - # Example 0. - tf.constant( - [[[0.11, 0.2, 0.3, 0.4], # Box 0. - [0.6, 0.4, 0.1, 0.0], - [0.0, 0.0, 0.0, 0.0]], - [[0.0, 0.0, 0.0, 0.0], # Box 1. - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0]], - [[0.22, 0.1, 0.6, 0.8], # Box 2. - [0.0, 0.4, 0.5, 1.0], - [0.3, 0.2, 0.4, 0.1]]], - dtype=tf.float32), - # Example 1. - tf.constant( - [[[0.5, 0.5, 0.3, 1.0], # Box 0. - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0]], - [[0.2, 0.2, 0.5, 0.8], # Box 1. - [0.0, 0.0, 0.0, 0.0], - [0.0, 0.0, 0.0, 0.0]]], - dtype=tf.float32), - ] - gt_weights_list = [ - # Example 0. - tf.constant([1.0, 1.0, 0.5], dtype=tf.float32), - # Example 1. - tf.constant([0.0, 1.0], dtype=tf.float32), - ] - cn_assigner = targetassigner.CenterNetDensePoseTargetAssigner(stride=4) - batch_indices, batch_part_ids, batch_surface_coords, batch_weights = ( - cn_assigner.assign_part_and_coordinate_targets( - height=120, - width=80, - gt_dp_num_points_list=gt_dp_num_points_list, - gt_dp_part_ids_list=gt_dp_part_ids_list, - gt_dp_surface_coords_list=gt_dp_surface_coords_list, - gt_weights_list=gt_weights_list)) - - return batch_indices, batch_part_ids, batch_surface_coords, batch_weights - batch_indices, batch_part_ids, batch_surface_coords, batch_weights = ( - self.execute(graph_fn, [])) - - expected_batch_indices = np.array([ - # Example 0. e.g. - # The first set of indices is calculated as follows: - # floor(0.11*120/4) = 3, floor(0.2*80/4) = 4. - [0, 3, 4, 1], [0, 18, 8, 6], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], - [0, 0, 0, 0], [0, 6, 2, 0], [0, 0, 8, 2], [0, 9, 4, 3], - # Example 1. - [1, 15, 10, 7], [1, 0, 0, 0], [1, 0, 0, 0], [1, 6, 4, 0], [1, 0, 0, 0], - [1, 0, 0, 0] - ], dtype=np.int32) - expected_batch_part_ids = tf.one_hot( - [1, 6, 0, 0, 0, 0, 0, 2, 3, 7, 0, 0, 0, 0, 0], depth=24).numpy() - expected_batch_surface_coords = np.array([ - # Box 0. - [0.3, 0.4], [0.1, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], - [0.6, 0.8], [0.5, 1.0], [0.4, 0.1], - # Box 1. - [0.3, 1.0], [0.0, 0.0], [0.0, 0.0], [0.5, 0.8], [0.0, 0.0], [0.0, 0.0], - ], np.float32) - expected_batch_weights = np.array([ - # Box 0. - 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.5, 0.5, 0.5, - # Box 1. - 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 - ], dtype=np.float32) - self.assertAllEqual(expected_batch_indices, batch_indices) - self.assertAllEqual(expected_batch_part_ids, batch_part_ids) - self.assertAllClose(expected_batch_surface_coords, batch_surface_coords) - self.assertAllClose(expected_batch_weights, batch_weights) - - -class CenterNetTrackTargetAssignerTest(test_case.TestCase): - - def setUp(self): - super(CenterNetTrackTargetAssignerTest, self).setUp() - self._box_center = [0.0, 0.0, 1.0, 1.0] - self._box_center_small = [0.25, 0.25, 0.75, 0.75] - self._box_lower_left = [0.5, 0.0, 1.0, 0.5] - self._box_center_offset = [0.1, 0.05, 1.0, 1.0] - self._box_odd_coordinates = [0.1625, 0.2125, 0.5625, 0.9625] - - def test_assign_track_targets(self): - """Test the assign_track_targets function.""" - def graph_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_lower_left, self._box_center_small]), - tf.constant([self._box_center_small, self._box_odd_coordinates]), - ] - track_id_batch = [ - tf.constant([0, 1]), - tf.constant([1, 0]), - tf.constant([0, 2]), - ] - - assigner = targetassigner.CenterNetTrackTargetAssigner( - stride=4, num_track_ids=3) - - (batch_indices, batch_weights, - track_targets) = assigner.assign_track_targets( - height=80, - width=80, - gt_track_ids_list=track_id_batch, - gt_boxes_list=box_batch) - return batch_indices, batch_weights, track_targets - - indices, weights, track_ids = self.execute(graph_fn, []) - - self.assertEqual(indices.shape, (3, 2, 3)) - self.assertEqual(track_ids.shape, (3, 2, 3)) - self.assertEqual(weights.shape, (3, 2)) - - np.testing.assert_array_equal(indices, - [[[0, 10, 10], [0, 15, 5]], - [[1, 15, 5], [1, 10, 10]], - [[2, 10, 10], [2, 7, 11]]]) - np.testing.assert_array_equal(track_ids, - [[[1, 0, 0], [0, 1, 0]], - [[0, 1, 0], [1, 0, 0]], - [[1, 0, 0], [0, 0, 1]]]) - np.testing.assert_array_equal(weights, [[1, 1], [1, 1], [1, 1]]) - - def test_assign_track_targets_weights(self): - """Test the assign_track_targets function with box weights.""" - def graph_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_lower_left, self._box_center_small]), - tf.constant([self._box_center_small, self._box_odd_coordinates]), - ] - track_id_batch = [ - tf.constant([0, 1]), - tf.constant([1, 0]), - tf.constant([0, 2]), - ] - weights_batch = [ - tf.constant([0.0, 1.0]), - tf.constant([1.0, 1.0]), - tf.constant([0.0, 0.0]) - ] - - assigner = targetassigner.CenterNetTrackTargetAssigner( - stride=4, num_track_ids=3) - - (batch_indices, batch_weights, - track_targets) = assigner.assign_track_targets( - height=80, - width=80, - gt_track_ids_list=track_id_batch, - gt_boxes_list=box_batch, - gt_weights_list=weights_batch) - return batch_indices, batch_weights, track_targets - - indices, weights, track_ids = self.execute(graph_fn, []) - - self.assertEqual(indices.shape, (3, 2, 3)) - self.assertEqual(track_ids.shape, (3, 2, 3)) - self.assertEqual(weights.shape, (3, 2)) - - np.testing.assert_array_equal(indices, - [[[0, 10, 10], [0, 15, 5]], - [[1, 15, 5], [1, 10, 10]], - [[2, 10, 10], [2, 7, 11]]]) - np.testing.assert_array_equal(track_ids, - [[[1, 0, 0], [0, 1, 0]], - [[0, 1, 0], [1, 0, 0]], - [[1, 0, 0], [0, 0, 1]]]) - np.testing.assert_array_equal(weights, [[0, 1], [1, 1], [0, 0]]) - # TODO(xwwang): Add a test for the case when no objects are detected. - - -class CornerOffsetTargetAssignerTest(test_case.TestCase): - - def test_filter_overlap_min_area_empty(self): - """Test that empty masks work on CPU.""" - def graph_fn(masks): - return targetassigner.filter_mask_overlap_min_area(masks) - - masks = self.execute_cpu(graph_fn, [np.zeros((0, 5, 5), dtype=np.float32)]) - self.assertEqual(masks.shape, (0, 5, 5)) - - def test_filter_overlap_min_area(self): - """Test the object with min. area is selected instead of overlap.""" - def graph_fn(masks): - return targetassigner.filter_mask_overlap_min_area(masks) - - masks = np.zeros((3, 4, 4), dtype=np.float32) - masks[0, :2, :2] = 1.0 - masks[1, :3, :3] = 1.0 - masks[2, 3, 3] = 1.0 - - masks = self.execute(graph_fn, [masks]) - - self.assertAllClose(masks[0], - [[1, 1, 0, 0], - [1, 1, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]) - self.assertAllClose(masks[1], - [[0, 0, 1, 0], - [0, 0, 1, 0], - [1, 1, 1, 0], - [0, 0, 0, 0]]) - - self.assertAllClose(masks[2], - [[0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 1]]) - - def test_assign_corner_offset_single_object(self): - """Test that corner offsets are correct with a single object.""" - assigner = targetassigner.CenterNetCornerOffsetTargetAssigner(stride=1) - - def graph_fn(): - boxes = [ - tf.constant([[0., 0., 1., 1.]]) - ] - mask = np.zeros((1, 4, 4), dtype=np.float32) - mask[0, 1:3, 1:3] = 1.0 - - masks = [tf.constant(mask)] - return assigner.assign_corner_offset_targets(boxes, masks) - - corner_offsets, foreground = self.execute(graph_fn, []) - self.assertAllClose(foreground[0], - [[0, 0, 0, 0], - [0, 1, 1, 0], - [0, 1, 1, 0], - [0, 0, 0, 0]]) - - self.assertAllClose(corner_offsets[0, :, :, 0], - [[0, 0, 0, 0], - [0, -1, -1, 0], - [0, -2, -2, 0], - [0, 0, 0, 0]]) - self.assertAllClose(corner_offsets[0, :, :, 1], - [[0, 0, 0, 0], - [0, -1, -2, 0], - [0, -1, -2, 0], - [0, 0, 0, 0]]) - self.assertAllClose(corner_offsets[0, :, :, 2], - [[0, 0, 0, 0], - [0, 3, 3, 0], - [0, 2, 2, 0], - [0, 0, 0, 0]]) - self.assertAllClose(corner_offsets[0, :, :, 3], - [[0, 0, 0, 0], - [0, 3, 2, 0], - [0, 3, 2, 0], - [0, 0, 0, 0]]) - - def test_assign_corner_offset_multiple_objects(self): - """Test corner offsets are correct with multiple objects.""" - assigner = targetassigner.CenterNetCornerOffsetTargetAssigner(stride=1) - - def graph_fn(): - boxes = [ - tf.constant([[0., 0., 1., 1.], [0., 0., 0., 0.]]), - tf.constant([[0., 0., .25, .25], [.25, .25, 1., 1.]]) - ] - mask1 = np.zeros((2, 4, 4), dtype=np.float32) - mask1[0, 0, 0] = 1.0 - mask1[0, 3, 3] = 1.0 - - mask2 = np.zeros((2, 4, 4), dtype=np.float32) - mask2[0, :2, :2] = 1.0 - mask2[1, 1:, 1:] = 1.0 - - masks = [tf.constant(mask1), tf.constant(mask2)] - return assigner.assign_corner_offset_targets(boxes, masks) - - corner_offsets, foreground = self.execute(graph_fn, []) - self.assertEqual(corner_offsets.shape, (2, 4, 4, 4)) - self.assertEqual(foreground.shape, (2, 4, 4)) - - self.assertAllClose(foreground[0], - [[1, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 1]]) - - self.assertAllClose(corner_offsets[0, :, :, 0], - [[0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, -3]]) - self.assertAllClose(corner_offsets[0, :, :, 1], - [[0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, -3]]) - self.assertAllClose(corner_offsets[0, :, :, 2], - [[4, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 1]]) - self.assertAllClose(corner_offsets[0, :, :, 3], - [[4, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 1]]) - - self.assertAllClose(foreground[1], - [[1, 1, 0, 0], - [1, 1, 1, 1], - [0, 1, 1, 1], - [0, 1, 1, 1]]) - - self.assertAllClose(corner_offsets[1, :, :, 0], - [[0, 0, 0, 0], - [-1, -1, 0, 0], - [0, -1, -1, -1], - [0, -2, -2, -2]]) - self.assertAllClose(corner_offsets[1, :, :, 1], - [[0, -1, 0, 0], - [0, -1, -1, -2], - [0, 0, -1, -2], - [0, 0, -1, -2]]) - self.assertAllClose(corner_offsets[1, :, :, 2], - [[1, 1, 0, 0], - [0, 0, 3, 3], - [0, 2, 2, 2], - [0, 1, 1, 1]]) - self.assertAllClose(corner_offsets[1, :, :, 3], - [[1, 0, 0, 0], - [1, 0, 2, 1], - [0, 3, 2, 1], - [0, 3, 2, 1]]) - - def test_assign_corner_offsets_no_objects(self): - """Test assignment works with empty input on cpu.""" - assigner = targetassigner.CenterNetCornerOffsetTargetAssigner(stride=1) - - def graph_fn(): - boxes = [ - tf.zeros((0, 4), dtype=tf.float32) - ] - masks = [tf.zeros((0, 5, 5), dtype=tf.float32)] - return assigner.assign_corner_offset_targets(boxes, masks) - - corner_offsets, foreground = self.execute_cpu(graph_fn, []) - self.assertAllClose(corner_offsets, np.zeros((1, 5, 5, 4))) - self.assertAllClose(foreground, np.zeros((1, 5, 5))) - - -class CenterNetTemporalOffsetTargetAssigner(test_case.TestCase): - - def setUp(self): - super(CenterNetTemporalOffsetTargetAssigner, self).setUp() - self._box_center = [0.0, 0.0, 1.0, 1.0] - self._box_center_small = [0.25, 0.25, 0.75, 0.75] - self._box_lower_left = [0.5, 0.0, 1.0, 0.5] - self._box_center_offset = [0.1, 0.05, 1.0, 1.0] - self._box_odd_coordinates = [0.1625, 0.2125, 0.5625, 0.9625] - self._offset_center = [0.5, 0.4] - self._offset_center_small = [0.1, 0.1] - self._offset_lower_left = [-0.1, 0.1] - self._offset_center_offset = [0.4, 0.3] - self._offset_odd_coord = [0.125, -0.125] - - def test_assign_empty_groundtruths(self): - """Tests the assign_offset_targets function with empty inputs.""" - def graph_fn(): - box_batch = [ - tf.zeros((0, 4), dtype=tf.float32), - ] - - offset_batch = [ - tf.zeros((0, 2), dtype=tf.float32), - ] - - match_flag_batch = [ - tf.zeros((0), dtype=tf.float32), - ] - - assigner = targetassigner.CenterNetTemporalOffsetTargetAssigner(4) - indices, temporal_offset, weights = assigner.assign_temporal_offset_targets( - 80, 80, box_batch, offset_batch, match_flag_batch) - return indices, temporal_offset, weights - indices, temporal_offset, weights = self.execute(graph_fn, []) - self.assertEqual(indices.shape, (0, 3)) - self.assertEqual(temporal_offset.shape, (0, 2)) - self.assertEqual(weights.shape, (0,)) - - def test_assign_offset_targets(self): - """Tests the assign_offset_targets function.""" - def graph_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_center_offset]), - tf.constant([self._box_center_small, self._box_odd_coordinates]), - ] - - offset_batch = [ - tf.constant([self._offset_center, self._offset_lower_left]), - tf.constant([self._offset_center_offset]), - tf.constant([self._offset_center_small, self._offset_odd_coord]), - ] - - match_flag_batch = [ - tf.constant([1.0, 1.0]), - tf.constant([1.0]), - tf.constant([1.0, 1.0]), - ] - - assigner = targetassigner.CenterNetTemporalOffsetTargetAssigner(4) - indices, temporal_offset, weights = assigner.assign_temporal_offset_targets( - 80, 80, box_batch, offset_batch, match_flag_batch) - return indices, temporal_offset, weights - indices, temporal_offset, weights = self.execute(graph_fn, []) - self.assertEqual(indices.shape, (5, 3)) - self.assertEqual(temporal_offset.shape, (5, 2)) - self.assertEqual(weights.shape, (5,)) - np.testing.assert_array_equal( - indices, - [[0, 10, 10], [0, 15, 5], [1, 11, 10], [2, 10, 10], [2, 7, 11]]) - np.testing.assert_array_almost_equal( - temporal_offset, - [[0.5, 0.4], [-0.1, 0.1], [0.4, 0.3], [0.1, 0.1], [0.125, -0.125]]) - np.testing.assert_array_equal(weights, 1) - - def test_assign_offset_targets_with_match_flags(self): - """Tests the assign_offset_targets function with match flags.""" - def graph_fn(): - box_batch = [ - tf.constant([self._box_center, self._box_lower_left]), - tf.constant([self._box_center_offset]), - tf.constant([self._box_center_small, self._box_odd_coordinates]), - ] - - offset_batch = [ - tf.constant([self._offset_center, self._offset_lower_left]), - tf.constant([self._offset_center_offset]), - tf.constant([self._offset_center_small, self._offset_odd_coord]), - ] - - match_flag_batch = [ - tf.constant([0.0, 1.0]), - tf.constant([1.0]), - tf.constant([1.0, 1.0]), - ] - - cn_assigner = targetassigner.CenterNetTemporalOffsetTargetAssigner(4) - weights_batch = [ - tf.constant([1.0, 0.0]), - tf.constant([1.0]), - tf.constant([1.0, 1.0]) - ] - indices, temporal_offset, weights = cn_assigner.assign_temporal_offset_targets( - 80, 80, box_batch, offset_batch, match_flag_batch, weights_batch) - return indices, temporal_offset, weights - indices, temporal_offset, weights = self.execute(graph_fn, []) - self.assertEqual(indices.shape, (5, 3)) - self.assertEqual(temporal_offset.shape, (5, 2)) - self.assertEqual(weights.shape, (5,)) - - np.testing.assert_array_equal( - indices, - [[0, 10, 10], [0, 15, 5], [1, 11, 10], [2, 10, 10], [2, 7, 11]]) - np.testing.assert_array_almost_equal( - temporal_offset, - [[0.5, 0.4], [-0.1, 0.1], [0.4, 0.3], [0.1, 0.1], [0.125, -0.125]]) - np.testing.assert_array_equal(weights, [0, 0, 1, 1, 1]) - - -class DETRTargetAssignerTest(test_case.TestCase): - - def test_assign_detr(self): - def graph_fn(pred_corners, groundtruth_box_corners, - groundtruth_labels, predicted_labels): - detr_target_assigner = targetassigner.DETRTargetAssigner() - pred_boxlist = box_list.BoxList(pred_corners) - groundtruth_boxlist = box_list.BoxList(groundtruth_box_corners) - result = detr_target_assigner.assign( - pred_boxlist, groundtruth_boxlist, - predicted_labels, groundtruth_labels) - (cls_targets, cls_weights, reg_targets, reg_weights) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - pred_corners = np.array([[0.25, 0.25, 0.4, 0.2], - [0.5, 0.8, 1.0, 0.8], - [0.9, 0.5, 0.1, 1.0]], dtype=np.float32) - groundtruth_box_corners = np.array([[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9]], - dtype=np.float32) - predicted_labels = np.array([[-3.0, 3.0], [2.0, 9.4], [5.0, 1.0]], - dtype=np.float32) - groundtruth_labels = np.array([[0.0, 1.0], [0.0, 1.0]], - dtype=np.float32) - - exp_cls_targets = [[0, 1], [0, 1], [1, 0]] - exp_cls_weights = [[1, 1], [1, 1], [1, 1]] - exp_reg_targets = [[0.25, 0.25, 0.5, 0.5], - [0.7, 0.7, 0.4, 0.4], - [0, 0, 0, 0]] - exp_reg_weights = [1, 1, 0] - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute_cpu( - graph_fn, [pred_corners, groundtruth_box_corners, - groundtruth_labels, predicted_labels]) - - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - def test_batch_assign_detr(self): - def graph_fn(pred_corners, groundtruth_box_corners, - groundtruth_labels, predicted_labels): - detr_target_assigner = targetassigner.DETRTargetAssigner() - result = detr_target_assigner.batch_assign( - pred_corners, groundtruth_box_corners, - [predicted_labels], [groundtruth_labels]) - (cls_targets, cls_weights, reg_targets, reg_weights) = result - return (cls_targets, cls_weights, reg_targets, reg_weights) - - pred_corners = np.array([[[0.25, 0.25, 0.4, 0.2], - [0.5, 0.8, 1.0, 0.8], - [0.9, 0.5, 0.1, 1.0]]], dtype=np.float32) - groundtruth_box_corners = np.array([[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.9, 0.9]]], - dtype=np.float32) - predicted_labels = np.array([[-3.0, 3.0], [2.0, 9.4], [5.0, 1.0]], - dtype=np.float32) - groundtruth_labels = np.array([[0.0, 1.0], [0.0, 1.0]], - dtype=np.float32) - - exp_cls_targets = [[[0, 1], [0, 1], [1, 0]]] - exp_cls_weights = [[[1, 1], [1, 1], [1, 1]]] - exp_reg_targets = [[[0.25, 0.25, 0.5, 0.5], - [0.7, 0.7, 0.4, 0.4], - [0, 0, 0, 0]]] - exp_reg_weights = [[1, 1, 0]] - - (cls_targets_out, - cls_weights_out, reg_targets_out, reg_weights_out) = self.execute_cpu( - graph_fn, [pred_corners, groundtruth_box_corners, - groundtruth_labels, predicted_labels]) - - self.assertAllClose(cls_targets_out, exp_cls_targets) - self.assertAllClose(cls_weights_out, exp_cls_weights) - self.assertAllClose(reg_targets_out, exp_reg_targets) - self.assertAllClose(reg_weights_out, exp_reg_weights) - self.assertEqual(cls_targets_out.dtype, np.float32) - self.assertEqual(cls_weights_out.dtype, np.float32) - self.assertEqual(reg_targets_out.dtype, np.float32) - self.assertEqual(reg_weights_out.dtype, np.float32) - - -if __name__ == '__main__': - tf.enable_v2_behavior() - tf.test.main() diff --git a/research/object_detection/data/ava_label_map_v2.1.pbtxt b/research/object_detection/data/ava_label_map_v2.1.pbtxt deleted file mode 100644 index 5e2c4856828..00000000000 --- a/research/object_detection/data/ava_label_map_v2.1.pbtxt +++ /dev/null @@ -1,240 +0,0 @@ -item { - name: "bend/bow (at the waist)" - id: 1 -} -item { - name: "crouch/kneel" - id: 3 -} -item { - name: "dance" - id: 4 -} -item { - name: "fall down" - id: 5 -} -item { - name: "get up" - id: 6 -} -item { - name: "jump/leap" - id: 7 -} -item { - name: "lie/sleep" - id: 8 -} -item { - name: "martial art" - id: 9 -} -item { - name: "run/jog" - id: 10 -} -item { - name: "sit" - id: 11 -} -item { - name: "stand" - id: 12 -} -item { - name: "swim" - id: 13 -} -item { - name: "walk" - id: 14 -} -item { - name: "answer phone" - id: 15 -} -item { - name: "carry/hold (an object)" - id: 17 -} -item { - name: "climb (e.g., a mountain)" - id: 20 -} -item { - name: "close (e.g., a door, a box)" - id: 22 -} -item { - name: "cut" - id: 24 -} -item { - name: "dress/put on clothing" - id: 26 -} -item { - name: "drink" - id: 27 -} -item { - name: "drive (e.g., a car, a truck)" - id: 28 -} -item { - name: "eat" - id: 29 -} -item { - name: "enter" - id: 30 -} -item { - name: "hit (an object)" - id: 34 -} -item { - name: "lift/pick up" - id: 36 -} -item { - name: "listen (e.g., to music)" - id: 37 -} -item { - name: "open (e.g., a window, a car door)" - id: 38 -} -item { - name: "play musical instrument" - id: 41 -} -item { - name: "point to (an object)" - id: 43 -} -item { - name: "pull (an object)" - id: 45 -} -item { - name: "push (an object)" - id: 46 -} -item { - name: "put down" - id: 47 -} -item { - name: "read" - id: 48 -} -item { - name: "ride (e.g., a bike, a car, a horse)" - id: 49 -} -item { - name: "sail boat" - id: 51 -} -item { - name: "shoot" - id: 52 -} -item { - name: "smoke" - id: 54 -} -item { - name: "take a photo" - id: 56 -} -item { - name: "text on/look at a cellphone" - id: 57 -} -item { - name: "throw" - id: 58 -} -item { - name: "touch (an object)" - id: 59 -} -item { - name: "turn (e.g., a screwdriver)" - id: 60 -} -item { - name: "watch (e.g., TV)" - id: 61 -} -item { - name: "work on a computer" - id: 62 -} -item { - name: "write" - id: 63 -} -item { - name: "fight/hit (a person)" - id: 64 -} -item { - name: "give/serve (an object) to (a person)" - id: 65 -} -item { - name: "grab (a person)" - id: 66 -} -item { - name: "hand clap" - id: 67 -} -item { - name: "hand shake" - id: 68 -} -item { - name: "hand wave" - id: 69 -} -item { - name: "hug (a person)" - id: 70 -} -item { - name: "kiss (a person)" - id: 72 -} -item { - name: "lift (a person)" - id: 73 -} -item { - name: "listen to (a person)" - id: 74 -} -item { - name: "push (another person)" - id: 76 -} -item { - name: "sing to (e.g., self, a person, a group)" - id: 77 -} -item { - name: "take (an object) from (a person)" - id: 78 -} -item { - name: "talk to (e.g., self, a person, a group)" - id: 79 -} -item { - name: "watch (a person)" - id: 80 -} diff --git a/research/object_detection/data/face_label_map.pbtxt b/research/object_detection/data/face_label_map.pbtxt deleted file mode 100644 index 1c7355db1fd..00000000000 --- a/research/object_detection/data/face_label_map.pbtxt +++ /dev/null @@ -1,6 +0,0 @@ -item { - name: "face" - id: 1 - display_name: "face" -} - diff --git a/research/object_detection/data/face_person_with_keypoints_label_map.pbtxt b/research/object_detection/data/face_person_with_keypoints_label_map.pbtxt deleted file mode 100644 index 181f11b289b..00000000000 --- a/research/object_detection/data/face_person_with_keypoints_label_map.pbtxt +++ /dev/null @@ -1,102 +0,0 @@ -item: { - id: 1 - name: 'face' - display_name: 'face' - keypoints { - id: 0 - label: "left_eye_center" - } - keypoints { - id: 1 - label: "right_eye_center" - } - keypoints { - id: 2 - label: "nose_tip" - } - keypoints { - id: 3 - label: "mouth_center" - } - keypoints { - id: 4 - label: "left_ear_tragion" - } - keypoints { - id: 5 - label: "right_ear_tragion" - } -} -item: { - id: 2 - name: 'Person' - display_name: 'PERSON' - keypoints { - id: 6 - label: "NOSE_TIP" - } - keypoints { - id: 7 - label: "LEFT_EYE" - } - keypoints { - id: 8 - label: "RIGHT_EYE" - } - keypoints { - id: 9 - label: "LEFT_EAR_TRAGION" - } - keypoints { - id: 10 - label: "RIGHT_EAR_TRAGION" - } - keypoints { - id: 11 - label: "LEFT_SHOULDER" - } - keypoints { - id: 12 - label: "RIGHT_SHOULDER" - } - keypoints { - id: 13 - label: "LEFT_ELBOW" - } - keypoints { - id: 14 - label: "RIGHT_ELBOW" - } - keypoints { - id: 15 - label: "LEFT_WRIST" - } - keypoints { - id: 16 - label: "RIGHT_WRIST" - } - keypoints { - id: 17 - label: "LEFT_HIP" - } - keypoints { - id: 18 - label: "RIGHT_HIP" - } - keypoints { - id: 19 - label: "LEFT_KNEE" - } - keypoints { - id: 20 - label: "RIGHT_KNEE" - } - keypoints { - id: 21 - label: "LEFT_ANKLE" - } - keypoints { - id: 22 - label: "RIGHT_ANKLE" - } -} diff --git a/research/object_detection/data/fgvc_2854_classes_label_map.pbtxt b/research/object_detection/data/fgvc_2854_classes_label_map.pbtxt deleted file mode 100644 index 009797f046a..00000000000 --- a/research/object_detection/data/fgvc_2854_classes_label_map.pbtxt +++ /dev/null @@ -1,14270 +0,0 @@ -item { - name: "147457" - id: 1 - display_name: "Nicrophorus tomentosus" -} -item { - name: "81923" - id: 2 - display_name: "Halyomorpha halys" -} -item { - name: "7" - id: 3 - display_name: "Aramus guarauna" -} -item { - name: "201041" - id: 4 - display_name: "Rupornis magnirostris" -} -item { - name: "65551" - id: 5 - display_name: "Hyla eximia" -} -item { - name: "106516" - id: 6 - display_name: "Nannothemis bella" -} -item { - name: "154287" - id: 7 - display_name: "Acalymma vittatum" -} -item { - name: "32798" - id: 8 - display_name: "Ramphotyphlops braminus" -} -item { - name: "8229" - id: 9 - display_name: "Cyanocitta cristata" -} -item { - name: "73766" - id: 10 - display_name: "Drymarchon melanurus" -} -item { - name: "409639" - id: 11 - display_name: "Aenetus virescens" -} -item { - name: "8234" - id: 12 - display_name: "Cyanocitta stelleri" -} -item { - name: "228593" - id: 13 - display_name: "Polygrammate hebraeicum" -} -item { - name: "53" - id: 14 - display_name: "Balearica regulorum" -} -item { - name: "57399" - id: 15 - display_name: "Fistularia commersonii" -} -item { - name: "81979" - id: 16 - display_name: "Syritta pipiens" -} -item { - name: "73788" - id: 17 - display_name: "Plestiodon fasciatus" -} -item { - name: "73790" - id: 18 - display_name: "Plestiodon inexpectatus" -} -item { - name: "16447" - id: 19 - display_name: "Pyrocephalus rubinus" -} -item { - name: "73792" - id: 20 - display_name: "Plestiodon laticeps" -} -item { - name: "49219" - id: 21 - display_name: "Anguilla rostrata" -} -item { - name: "73797" - id: 22 - display_name: "Plestiodon obsoletus" -} -item { - name: "73803" - id: 23 - display_name: "Plestiodon tetragrammus" -} -item { - name: "122956" - id: 24 - display_name: "Syntomoides imaon" -} -item { - name: "82003" - id: 25 - display_name: "Arion ater" -} -item { - name: "32854" - id: 26 - display_name: "Chamaeleo dilepis" -} -item { - name: "42341" - id: 27 - display_name: "Tragelaphus scriptus" -} -item { - name: "82018" - id: 28 - display_name: "Taeniopoda eques" -} -item { - name: "57443" - id: 29 - display_name: "Libellula quadrimaculata" -} -item { - name: "4885" - id: 30 - display_name: "Recurvirostra americana" -} -item { - name: "178403" - id: 31 - display_name: "Phalaenophana pyramusalis" -} -item { - name: "135027" - id: 32 - display_name: "Agalychnis dacnicolor" -} -item { - name: "49262" - id: 33 - display_name: "Haemulon sciurus" -} -item { - name: "98417" - id: 34 - display_name: "Cordulegaster diastatops" -} -item { - name: "57458" - id: 35 - display_name: "Ladona julia" -} -item { - name: "115" - id: 36 - display_name: "Ardeotis kori" -} -item { - name: "49269" - id: 37 - display_name: "Diodon holocanthus" -} -item { - name: "57463" - id: 38 - display_name: "Papilio canadensis" -} -item { - name: "82043" - id: 39 - display_name: "Monochamus scutellatus" -} -item { - name: "147580" - id: 40 - display_name: "Ceratotherium simum simum" -} -item { - name: "98430" - id: 41 - display_name: "Cordulia shurtleffii" -} -item { - name: "8319" - id: 42 - display_name: "Pica nuttalli" -} -item { - name: "43712" - id: 43 - display_name: "Dasyprocta punctata" -} -item { - name: "8335" - id: 44 - display_name: "Perisoreus canadensis" -} -item { - name: "508048" - id: 45 - display_name: "Antigone canadensis" -} -item { - name: "49297" - id: 46 - display_name: "Aetobatus narinari" -} -item { - name: "82069" - id: 47 - display_name: "Phyciodes pulchella" -} -item { - name: "73149" - id: 48 - display_name: "Parkesia noveboracensis" -} -item { - name: "180379" - id: 49 - display_name: "Ardea herodias occidentalis" -} -item { - name: "73884" - id: 50 - display_name: "Pantherophis emoryi" -} -item { - name: "106653" - id: 51 - display_name: "Nehalennia irene" -} -item { - name: "73887" - id: 52 - display_name: "Pantherophis guttatus" -} -item { - name: "73888" - id: 53 - display_name: "Pantherophis obsoletus" -} -item { - name: "162" - id: 54 - display_name: "Porzana carolina" -} -item { - name: "245925" - id: 55 - display_name: "Siproeta stelenes biplagiata" -} -item { - name: "117302" - id: 56 - display_name: "Physalia physalis" -} -item { - name: "57516" - id: 57 - display_name: "Bombus terrestris" -} -item { - name: "204995" - id: 58 - display_name: "Anas platyrhynchos diazi" -} -item { - name: "49348" - id: 59 - display_name: "Hyles lineata" -} -item { - name: "82117" - id: 60 - display_name: "Dolomedes tenebrosus" -} -item { - name: "114891" - id: 61 - display_name: "Varanus salvator" -} -item { - name: "319695" - id: 62 - display_name: "Epilachna mexicana" -} -item { - name: "41168" - id: 63 - display_name: "Desmodus rotundus" -} -item { - name: "13688" - id: 64 - display_name: "Motacilla cinerea" -} -item { - name: "57556" - id: 65 - display_name: "Papio ursinus" -} -item { - name: "16598" - id: 66 - display_name: "Empidonax difficilis" -} -item { - name: "16602" - id: 67 - display_name: "Empidonax minimus" -} -item { - name: "16604" - id: 68 - display_name: "Empidonax fulvifrons" -} -item { - name: "409181" - id: 69 - display_name: "Trite planiceps" -} -item { - name: "82144" - id: 70 - display_name: "Hemileuca eglanterina" -} -item { - name: "16611" - id: 71 - display_name: "Empidonax traillii" -} -item { - name: "82153" - id: 72 - display_name: "Ceratomia undulosa" -} -item { - name: "82155" - id: 73 - display_name: "Bittacomorpha clavipes" -} -item { - name: "205036" - id: 74 - display_name: "Xanthorhoe lacustrata" -} -item { - name: "16624" - id: 75 - display_name: "Empidonax hammondii" -} -item { - name: "16625" - id: 76 - display_name: "Empidonax occidentalis" -} -item { - name: "243" - id: 77 - display_name: "Rallus limicola" -} -item { - name: "41" - id: 78 - display_name: "Grus grus" -} -item { - name: "49402" - id: 79 - display_name: "Abudefduf saxatilis" -} -item { - name: "58550" - id: 80 - display_name: "Callophrys niphon" -} -item { - name: "205055" - id: 81 - display_name: "Zopherus nodulosus haldemani" -} -item { - name: "82177" - id: 82 - display_name: "Hermetia illucens" -} -item { - name: "9601" - id: 83 - display_name: "Quiscalus major" -} -item { - name: "7101" - id: 84 - display_name: "Branta leucopsis" -} -item { - name: "8470" - id: 85 - display_name: "Cyanocorax yucatanicus" -} -item { - name: "74009" - id: 86 - display_name: "Zamenis longissimus" -} -item { - name: "8474" - id: 87 - display_name: "Cyanocorax yncas" -} -item { - name: "82204" - id: 88 - display_name: "Nadata gibbosa" -} -item { - name: "123168" - id: 89 - display_name: "Ensatina eschscholtzii xanthoptica" -} -item { - name: "82210" - id: 90 - display_name: "Heterocampa biundata" -} -item { - name: "48284" - id: 91 - display_name: "Oniscus asellus" -} -item { - name: "4146" - id: 92 - display_name: "Oceanites oceanicus" -} -item { - name: "82225" - id: 93 - display_name: "Lophocampa caryae" -} -item { - name: "9609" - id: 94 - display_name: "Quiscalus niger" -} -item { - name: "65849" - id: 95 - display_name: "Incilius nebulifer" -} -item { - name: "207583" - id: 96 - display_name: "Miomantis caffra" -} -item { - name: "491839" - id: 97 - display_name: "Pyrausta insequalis" -} -item { - name: "74048" - id: 98 - display_name: "Alces americanus" -} -item { - name: "57665" - id: 99 - display_name: "Cotinis mutabilis" -} -item { - name: "65860" - id: 100 - display_name: "Incilius valliceps" -} -item { - name: "52911" - id: 101 - display_name: "Dolichovespula maculata" -} -item { - name: "8524" - id: 102 - display_name: "Psilorhinus morio" -} -item { - name: "49491" - id: 103 - display_name: "Thalassoma bifasciatum" -} -item { - name: "41301" - id: 104 - display_name: "Tadarida brasiliensis" -} -item { - name: "57687" - id: 105 - display_name: "Xylocopa varipuncta" -} -item { - name: "57689" - id: 106 - display_name: "Bombus vosnesenskii" -} -item { - name: "57690" - id: 107 - display_name: "Bombus sonorus" -} -item { - name: "33118" - id: 108 - display_name: "Basiliscus vittatus" -} -item { - name: "205151" - id: 109 - display_name: "Phlogophora meticulosa" -} -item { - name: "49504" - id: 110 - display_name: "Callinectes sapidus" -} -item { - name: "16737" - id: 111 - display_name: "Megarynchus pitangua" -} -item { - name: "357" - id: 112 - display_name: "Gallinula tenebrosa" -} -item { - name: "82278" - id: 113 - display_name: "Ameiurus melas" -} -item { - name: "82279" - id: 114 - display_name: "Automeris io" -} -item { - name: "505478" - id: 115 - display_name: "Gallus gallus domesticus" -} -item { - name: "33135" - id: 116 - display_name: "Crotaphytus collaris" -} -item { - name: "41328" - id: 117 - display_name: "Lavia frons" -} -item { - name: "196979" - id: 118 - display_name: "Anaxyrus boreas halophilus" -} -item { - name: "44902" - id: 119 - display_name: "Sigmodon hispidus" -} -item { - name: "1428" - id: 120 - display_name: "Numida meleagris" -} -item { - name: "119153" - id: 121 - display_name: "Junco hyemalis caniceps" -} -item { - name: "49539" - id: 122 - display_name: "Pisaster brevispinus" -} -item { - name: "328068" - id: 123 - display_name: "Belocaulus angustipes" -} -item { - name: "120214" - id: 124 - display_name: "Clostera albosigma" -} -item { - name: "16779" - id: 125 - display_name: "Tyrannus vociferans" -} -item { - name: "16782" - id: 126 - display_name: "Tyrannus tyrannus" -} -item { - name: "16783" - id: 127 - display_name: "Tyrannus forficatus" -} -item { - name: "16784" - id: 128 - display_name: "Tyrannus crassirostris" -} -item { - name: "57745" - id: 129 - display_name: "Linckia laevigata" -} -item { - name: "205202" - id: 130 - display_name: "Ecliptopera silaceata" -} -item { - name: "205203" - id: 131 - display_name: "Dyspteris abortivaria" -} -item { - name: "16791" - id: 132 - display_name: "Tyrannus verticalis" -} -item { - name: "16793" - id: 133 - display_name: "Tyrannus savana" -} -item { - name: "205213" - id: 134 - display_name: "Caripeta divisata" -} -item { - name: "49566" - id: 135 - display_name: "Cicindela sexguttata" -} -item { - name: "491935" - id: 136 - display_name: "Thylacodes squamigerus" -} -item { - name: "205216" - id: 137 - display_name: "Cerma cerintha" -} -item { - name: "39665" - id: 138 - display_name: "Caretta caretta" -} -item { - name: "147881" - id: 139 - display_name: "Trichechus manatus latirostris" -} -item { - name: "28743" - id: 140 - display_name: "Salvadora hexalepis" -} -item { - name: "205231" - id: 141 - display_name: "Idaea dimidiata" -} -item { - name: "205233" - id: 142 - display_name: "Iridopsis larvaria" -} -item { - name: "205235" - id: 143 - display_name: "Leuconycta diphteroides" -} -item { - name: "436" - id: 144 - display_name: "Gallirallus australis" -} -item { - name: "205238" - id: 145 - display_name: "Metanema inatomaria" -} -item { - name: "49591" - id: 146 - display_name: "Lepomis macrochirus" -} -item { - name: "229817" - id: 147 - display_name: "Raphia frater" -} -item { - name: "49594" - id: 148 - display_name: "Pomoxis nigromaculatus" -} -item { - name: "65979" - id: 149 - display_name: "Lithobates catesbeianus" -} -item { - name: "49596" - id: 150 - display_name: "Salvelinus fontinalis" -} -item { - name: "65982" - id: 151 - display_name: "Lithobates clamitans" -} -item { - name: "8649" - id: 152 - display_name: "Calocitta formosa" -} -item { - name: "8650" - id: 153 - display_name: "Calocitta colliei" -} -item { - name: "82379" - id: 154 - display_name: "Hemaris thysbe" -} -item { - name: "49614" - id: 155 - display_name: "Lepomis gibbosus" -} -item { - name: "63028" - id: 156 - display_name: "Hypercompe scribonia" -} -item { - name: "39672" - id: 157 - display_name: "Eretmochelys imbricata" -} -item { - name: "66003" - id: 158 - display_name: "Lithobates pipiens" -} -item { - name: "197077" - id: 159 - display_name: "Vanessa kershawi" -} -item { - name: "473" - id: 160 - display_name: "Fulica americana" -} -item { - name: "147930" - id: 161 - display_name: "Rabidosa rabida" -} -item { - name: "147931" - id: 162 - display_name: "Panoquina ocola" -} -item { - name: "66012" - id: 163 - display_name: "Lithobates sylvaticus" -} -item { - name: "8671" - id: 164 - display_name: "Pachyramphus aglaiae" -} -item { - name: "41440" - id: 165 - display_name: "Phocoena phocoena" -} -item { - name: "27388" - id: 166 - display_name: "Carphophis amoenus" -} -item { - name: "82418" - id: 167 - display_name: "Cicindela punctulata" -} -item { - name: "25078" - id: 168 - display_name: "Gastrophryne carolinensis" -} -item { - name: "82425" - id: 169 - display_name: "Cicindela repanda" -} -item { - name: "143446" - id: 170 - display_name: "Paonias myops" -} -item { - name: "41478" - id: 171 - display_name: "Eschrichtius robustus" -} -item { - name: "5200" - id: 172 - display_name: "Buteo lagopus" -} -item { - name: "148908" - id: 173 - display_name: "Chrysodeixis includens" -} -item { - name: "41482" - id: 174 - display_name: "Tursiops truncatus" -} -item { - name: "6914" - id: 175 - display_name: "Cygnus atratus" -} -item { - name: "464301" - id: 176 - display_name: "Philesturnus rufusater" -} -item { - name: "129226" - id: 177 - display_name: "Chytolita morbidalis" -} -item { - name: "180759" - id: 178 - display_name: "Aphonopelma iodius" -} -item { - name: "135318" - id: 179 - display_name: "Apantesis phalerata" -} -item { - name: "49699" - id: 180 - display_name: "Pisaster ochraceus" -} -item { - name: "49700" - id: 181 - display_name: "Coluber lateralis lateralis" -} -item { - name: "61532" - id: 182 - display_name: "Propylea quatuordecimpunctata" -} -item { - name: "4368" - id: 183 - display_name: "Larus marinus" -} -item { - name: "41521" - id: 184 - display_name: "Orcinus orca" -} -item { - name: "49716" - id: 185 - display_name: "Paonias excaecata" -} -item { - name: "41526" - id: 186 - display_name: "Delphinus delphis" -} -item { - name: "49723" - id: 187 - display_name: "Pugettia producta" -} -item { - name: "16956" - id: 188 - display_name: "Pitangus sulphuratus" -} -item { - name: "210607" - id: 189 - display_name: "Diastictis fracturalis" -} -item { - name: "148030" - id: 190 - display_name: "Equus asinus" -} -item { - name: "6924" - id: 191 - display_name: "Anas rubripes" -} -item { - name: "30844" - id: 192 - display_name: "Bothriechis schlegelii" -} -item { - name: "123628" - id: 193 - display_name: "Argynnis paphia" -} -item { - name: "131676" - id: 194 - display_name: "Anthus novaeseelandiae novaeseelandiae" -} -item { - name: "41566" - id: 195 - display_name: "Megaptera novaeangliae" -} -item { - name: "49759" - id: 196 - display_name: "Pyrgus oileus" -} -item { - name: "49761" - id: 197 - display_name: "Anartia jatrophae" -} -item { - name: "49766" - id: 198 - display_name: "Heliconius charithonia" -} -item { - name: "33383" - id: 199 - display_name: "Coleonyx brevis" -} -item { - name: "33384" - id: 200 - display_name: "Coleonyx elegans" -} -item { - name: "312764" - id: 201 - display_name: "Euptoieta hegesia meridiania" -} -item { - name: "82538" - id: 202 - display_name: "Vanessa gonerilla" -} -item { - name: "33387" - id: 203 - display_name: "Coleonyx variegatus" -} -item { - name: "56082" - id: 204 - display_name: "Aeshna canadensis" -} -item { - name: "17008" - id: 205 - display_name: "Sayornis phoebe" -} -item { - name: "200808" - id: 206 - display_name: "Sceloporus graciosus vandenburgianus" -} -item { - name: "17013" - id: 207 - display_name: "Sayornis nigricans" -} -item { - name: "122381" - id: 208 - display_name: "Cupido comyntas" -} -item { - name: "123516" - id: 209 - display_name: "Mydas clavatus" -} -item { - name: "8834" - id: 210 - display_name: "Tityra semifasciata" -} -item { - name: "146199" - id: 211 - display_name: "Lampropeltis californiae" -} -item { - name: "17858" - id: 212 - display_name: "Dryocopus lineatus" -} -item { - name: "334616" - id: 213 - display_name: "Battus philenor hirsuta" -} -item { - name: "82582" - id: 214 - display_name: "Labidomera clivicollis" -} -item { - name: "204699" - id: 215 - display_name: "Pseudothyatira cymatophoroides" -} -item { - name: "41638" - id: 216 - display_name: "Ursus americanus" -} -item { - name: "27420" - id: 217 - display_name: "Desmognathus fuscus" -} -item { - name: "81584" - id: 218 - display_name: "Anisota virginiensis" -} -item { - name: "49848" - id: 219 - display_name: "Navanax inermis" -} -item { - name: "143476" - id: 220 - display_name: "Calledapteryx dryopterata" -} -item { - name: "41663" - id: 221 - display_name: "Procyon lotor" -} -item { - name: "49857" - id: 222 - display_name: "Aplysia vaccaria" -} -item { - name: "41673" - id: 223 - display_name: "Nasua narica" -} -item { - name: "41676" - id: 224 - display_name: "Bassariscus astutus" -} -item { - name: "27427" - id: 225 - display_name: "Aneides lugubris" -} -item { - name: "418530" - id: 226 - display_name: "Porphyrio melanotus" -} -item { - name: "311419" - id: 227 - display_name: "Neobernaya spadicea" -} -item { - name: "113502" - id: 228 - display_name: "Sympetrum costiferum" -} -item { - name: "66278" - id: 229 - display_name: "Oophaga pumilio" -} -item { - name: "6951" - id: 230 - display_name: "Anas bahamensis" -} -item { - name: "213740" - id: 231 - display_name: "Antaeotricha schlaegeri" -} -item { - name: "143485" - id: 232 - display_name: "Xanthorhoe ferrugata" -} -item { - name: "120275" - id: 233 - display_name: "Euphyia intermediata" -} -item { - name: "48035" - id: 234 - display_name: "Strongylocentrotus purpuratus" -} -item { - name: "41728" - id: 235 - display_name: "Mirounga angustirostris" -} -item { - name: "41733" - id: 236 - display_name: "Halichoerus grypus" -} -item { - name: "41740" - id: 237 - display_name: "Zalophus californianus" -} -item { - name: "118914" - id: 238 - display_name: "Echinargus isola" -} -item { - name: "4936" - id: 239 - display_name: "Egretta novaehollandiae" -} -item { - name: "131862" - id: 240 - display_name: "Typocerus velutinus" -} -item { - name: "55401" - id: 241 - display_name: "Pieris brassicae" -} -item { - name: "41752" - id: 242 - display_name: "Arctocephalus forsteri" -} -item { - name: "41755" - id: 243 - display_name: "Eumetopias jubatus" -} -item { - name: "123676" - id: 244 - display_name: "Anas crecca carolinensis" -} -item { - name: "41763" - id: 245 - display_name: "Phocarctos hookeri" -} -item { - name: "181034" - id: 246 - display_name: "Cervus elaphus canadensis" -} -item { - name: "49964" - id: 247 - display_name: "Ginglymostoma cirratum" -} -item { - name: "213809" - id: 248 - display_name: "Anticarsia gemmatalis" -} -item { - name: "49972" - id: 249 - display_name: "Battus philenor" -} -item { - name: "205623" - id: 250 - display_name: "Microstylum morosum" -} -item { - name: "336697" - id: 251 - display_name: "Arctia villica" -} -item { - name: "41789" - id: 252 - display_name: "Taxidea taxus" -} -item { - name: "48724" - id: 253 - display_name: "Phidiana hiltoni" -} -item { - name: "123713" - id: 254 - display_name: "Neoscona oaxacensis" -} -item { - name: "33602" - id: 255 - display_name: "Tarentola mauritanica" -} -item { - name: "846" - id: 256 - display_name: "Alectoris chukar" -} -item { - name: "41808" - id: 257 - display_name: "Mustela erminea" -} -item { - name: "50001" - id: 258 - display_name: "Terrapene carolina carolina" -} -item { - name: "41810" - id: 259 - display_name: "Mustela frenata" -} -item { - name: "82774" - id: 260 - display_name: "Oryctes nasicornis" -} -item { - name: "41815" - id: 261 - display_name: "Mustela nivalis" -} -item { - name: "4239" - id: 262 - display_name: "Tachybaptus dominicus" -} -item { - name: "344926" - id: 263 - display_name: "Artemisiospiza belli" -} -item { - name: "82792" - id: 264 - display_name: "Celastrina neglecta" -} -item { - name: "41841" - id: 265 - display_name: "Meles meles" -} -item { - name: "882" - id: 266 - display_name: "Gallus gallus" -} -item { - name: "125758" - id: 267 - display_name: "Mercenaria mercenaria" -} -item { - name: "9081" - id: 268 - display_name: "Cardinalis sinuatus" -} -item { - name: "9083" - id: 269 - display_name: "Cardinalis cardinalis" -} -item { - name: "9092" - id: 270 - display_name: "Melospiza lincolnii" -} -item { - name: "4246" - id: 271 - display_name: "Podilymbus podiceps" -} -item { - name: "9096" - id: 272 - display_name: "Melospiza georgiana" -} -item { - name: "906" - id: 273 - display_name: "Meleagris gallopavo" -} -item { - name: "50059" - id: 274 - display_name: "Limacia cockerelli" -} -item { - name: "394124" - id: 275 - display_name: "Orthodera novaezealandiae" -} -item { - name: "82832" - id: 276 - display_name: "Cosmopepla lintneriana" -} -item { - name: "913" - id: 277 - display_name: "Meleagris ocellata" -} -item { - name: "41877" - id: 278 - display_name: "Conepatus leuconotus" -} -item { - name: "196419" - id: 279 - display_name: "Euborellia annulipes" -} -item { - name: "50071" - id: 280 - display_name: "Erynnis horatius" -} -item { - name: "41880" - id: 281 - display_name: "Mephitis mephitis" -} -item { - name: "50073" - id: 282 - display_name: "Dryas iulia" -} -item { - name: "173793" - id: 283 - display_name: "Diphthera festiva" -} -item { - name: "41886" - id: 284 - display_name: "Crocuta crocuta" -} -item { - name: "30683" - id: 285 - display_name: "Agkistrodon contortrix contortrix" -} -item { - name: "931" - id: 286 - display_name: "Lagopus lagopus" -} -item { - name: "41901" - id: 287 - display_name: "Herpestes javanicus" -} -item { - name: "143517" - id: 288 - display_name: "Biston betularia" -} -item { - name: "9139" - id: 289 - display_name: "Spizella atrogularis" -} -item { - name: "8350" - id: 290 - display_name: "Pyrrhocorax graculus" -} -item { - name: "9144" - id: 291 - display_name: "Spizella breweri" -} -item { - name: "12936" - id: 292 - display_name: "Sialia currucoides" -} -item { - name: "9152" - id: 293 - display_name: "Spizella pusilla" -} -item { - name: "68229" - id: 294 - display_name: "Tramea carolina" -} -item { - name: "6987" - id: 295 - display_name: "Anas superciliosa" -} -item { - name: "9156" - id: 296 - display_name: "Passerella iliaca" -} -item { - name: "202315" - id: 297 - display_name: "Romaleon antennarium" -} -item { - name: "4257" - id: 298 - display_name: "Phoenicopterus ruber" -} -item { - name: "25545" - id: 299 - display_name: "Rana aurora" -} -item { - name: "15282" - id: 300 - display_name: "Sylvia atricapilla" -} -item { - name: "103927" - id: 301 - display_name: "Ladona deplanata" -} -item { - name: "17356" - id: 302 - display_name: "Vireo bellii" -} -item { - name: "26765" - id: 303 - display_name: "Ambystoma mavortium" -} -item { - name: "205777" - id: 304 - display_name: "Plectrodera scalator" -} -item { - name: "17362" - id: 305 - display_name: "Vireo plumbeus" -} -item { - name: "99283" - id: 306 - display_name: "Didymops transversa" -} -item { - name: "17364" - id: 307 - display_name: "Vireo philadelphicus" -} -item { - name: "17365" - id: 308 - display_name: "Vireo flavifrons" -} -item { - name: "17366" - id: 309 - display_name: "Vireo olivaceus" -} -item { - name: "9182" - id: 310 - display_name: "Zonotrichia querula" -} -item { - name: "17375" - id: 311 - display_name: "Vireo huttoni" -} -item { - name: "9184" - id: 312 - display_name: "Zonotrichia albicollis" -} -item { - name: "9185" - id: 313 - display_name: "Zonotrichia atricapilla" -} -item { - name: "50147" - id: 314 - display_name: "Celithemis eponina" -} -item { - name: "47585" - id: 315 - display_name: "Crassostrea virginica" -} -item { - name: "9195" - id: 316 - display_name: "Emberiza citrinella" -} -item { - name: "41964" - id: 317 - display_name: "Panthera leo" -} -item { - name: "6994" - id: 318 - display_name: "Bucephala islandica" -} -item { - name: "52506" - id: 319 - display_name: "Adalia bipunctata" -} -item { - name: "9201" - id: 320 - display_name: "Emberiza schoeniclus" -} -item { - name: "17394" - id: 321 - display_name: "Vireo gilvus" -} -item { - name: "25591" - id: 322 - display_name: "Rana temporaria" -} -item { - name: "41976" - id: 323 - display_name: "Lynx rufus" -} -item { - name: "214015" - id: 324 - display_name: "Apoda y-inversum" -} -item { - name: "50176" - id: 325 - display_name: "Enallagma vesperum" -} -item { - name: "99331" - id: 326 - display_name: "Diplacodes trivialis" -} -item { - name: "50181" - id: 327 - display_name: "Loxosceles reclusa" -} -item { - name: "74758" - id: 328 - display_name: "Neovison vison" -} -item { - name: "123912" - id: 329 - display_name: "Charaxes jasius" -} -item { - name: "41997" - id: 330 - display_name: "Leopardus pardalis" -} -item { - name: "123920" - id: 331 - display_name: "Dorcus parallelipipedus" -} -item { - name: "132334" - id: 332 - display_name: "Urbanus procne" -} -item { - name: "123922" - id: 333 - display_name: "Abudefduf sordidus" -} -item { - name: "9236" - id: 334 - display_name: "Serinus serinus" -} -item { - name: "42007" - id: 335 - display_name: "Puma concolor" -} -item { - name: "9240" - id: 336 - display_name: "Serinus mozambicus" -} -item { - name: "148506" - id: 337 - display_name: "Melanis pixe" -} -item { - name: "58399" - id: 338 - display_name: "Urosalpinx cinerea" -} -item { - name: "312353" - id: 339 - display_name: "Leptophobia aripa elodia" -} -item { - name: "148517" - id: 340 - display_name: "Heliopetes laviana" -} -item { - name: "73905" - id: 341 - display_name: "Phrynosoma cornutum" -} -item { - name: "39772" - id: 342 - display_name: "Chrysemys picta marginata" -} -item { - name: "25646" - id: 343 - display_name: "Rana boylii" -} -item { - name: "62984" - id: 344 - display_name: "Aedes albopictus" -} -item { - name: "123959" - id: 345 - display_name: "Ensatina eschscholtzii oregonensis" -} -item { - name: "1081" - id: 346 - display_name: "Lophura leucomelanos" -} -item { - name: "39775" - id: 347 - display_name: "Chrysemys picta picta" -} -item { - name: "42046" - id: 348 - display_name: "Canis mesomelas" -} -item { - name: "42048" - id: 349 - display_name: "Canis lupus" -} -item { - name: "42051" - id: 350 - display_name: "Canis latrans" -} -item { - name: "9284" - id: 351 - display_name: "Euphonia elegantissima" -} -item { - name: "25669" - id: 352 - display_name: "Rana dalmatina" -} -item { - name: "9287" - id: 353 - display_name: "Euphonia hirundinacea" -} -item { - name: "9291" - id: 354 - display_name: "Euphonia affinis" -} -item { - name: "222284" - id: 355 - display_name: "Iridopsis defectaria" -} -item { - name: "74832" - id: 356 - display_name: "Papio anubis" -} -item { - name: "148563" - id: 357 - display_name: "Myscelia ethusa" -} -item { - name: "42069" - id: 358 - display_name: "Vulpes vulpes" -} -item { - name: "9743" - id: 359 - display_name: "Agelaius tricolor" -} -item { - name: "42076" - id: 360 - display_name: "Urocyon cinereoargenteus" -} -item { - name: "509025" - id: 361 - display_name: "Momotus lessonii" -} -item { - name: "17506" - id: 362 - display_name: "Zosterops japonicus" -} -item { - name: "4283" - id: 363 - display_name: "Phalacrocorax pelagicus" -} -item { - name: "58469" - id: 364 - display_name: "Thorybes pylades" -} -item { - name: "9319" - id: 365 - display_name: "Icterus cucullatus" -} -item { - name: "58473" - id: 366 - display_name: "Erynnis icelus" -} -item { - name: "58475" - id: 367 - display_name: "Erynnis juvenalis" -} -item { - name: "42093" - id: 368 - display_name: "Lycaon pictus" -} -item { - name: "58478" - id: 369 - display_name: "Erynnis baptisiae" -} -item { - name: "9328" - id: 370 - display_name: "Icterus graduacauda" -} -item { - name: "58481" - id: 371 - display_name: "Ancyloxypha numitor" -} -item { - name: "132210" - id: 372 - display_name: "Deloyala guttata" -} -item { - name: "58484" - id: 373 - display_name: "Thymelicus lineola" -} -item { - name: "13701" - id: 374 - display_name: "Motacilla aguimp" -} -item { - name: "410743" - id: 375 - display_name: "Anas superciliosa \303\227 platyrhynchos" -} -item { - name: "9336" - id: 376 - display_name: "Icterus pustulatus" -} -item { - name: "9339" - id: 377 - display_name: "Icterus gularis" -} -item { - name: "124031" - id: 378 - display_name: "Agrius convolvuli" -} -item { - name: "42113" - id: 379 - display_name: "Pecari tajacu" -} -item { - name: "132227" - id: 380 - display_name: "Lethe appalachia" -} -item { - name: "113516" - id: 381 - display_name: "Sympetrum madidum" -} -item { - name: "58509" - id: 382 - display_name: "Anatrytone logan" -} -item { - name: "83086" - id: 383 - display_name: "Eurytides marcellus" -} -item { - name: "58511" - id: 384 - display_name: "Poanes viator" -} -item { - name: "83090" - id: 385 - display_name: "Epimecis hortaria" -} -item { - name: "115859" - id: 386 - display_name: "Micrurus tener tener" -} -item { - name: "129902" - id: 387 - display_name: "Camponotus pennsylvanicus" -} -item { - name: "42134" - id: 388 - display_name: "Sus scrofa" -} -item { - name: "58519" - id: 389 - display_name: "Pompeius verna" -} -item { - name: "205977" - id: 390 - display_name: "Coccinella undecimpunctata" -} -item { - name: "58523" - id: 391 - display_name: "Papilio polyxenes" -} -item { - name: "58525" - id: 392 - display_name: "Papilio troilus" -} -item { - name: "410783" - id: 393 - display_name: "Hypoblemum albovittatum" -} -item { - name: "9376" - id: 394 - display_name: "Carduelis cannabina" -} -item { - name: "58531" - id: 395 - display_name: "Colias philodice" -} -item { - name: "50340" - id: 396 - display_name: "Hylephila phyleus" -} -item { - name: "42149" - id: 397 - display_name: "Hippopotamus amphibius" -} -item { - name: "50342" - id: 398 - display_name: "Erythrodiplax umbrata" -} -item { - name: "12883" - id: 399 - display_name: "Catharus minimus" -} -item { - name: "28557" - id: 400 - display_name: "Storeria occipitomaculata" -} -item { - name: "199" - id: 401 - display_name: "Amaurornis phoenicurus" -} -item { - name: "58541" - id: 402 - display_name: "Satyrium liparops" -} -item { - name: "58543" - id: 403 - display_name: "Callophrys augustinus" -} -item { - name: "42161" - id: 404 - display_name: "Dama dama" -} -item { - name: "61508" - id: 405 - display_name: "Ischnura elegans" -} -item { - name: "1204" - id: 406 - display_name: "Pavo cristatus" -} -item { - name: "42166" - id: 407 - display_name: "Axis axis" -} -item { - name: "146797" - id: 408 - display_name: "Platynota idaeusalis" -} -item { - name: "58556" - id: 409 - display_name: "Celastrina ladon" -} -item { - name: "367477" - id: 410 - display_name: "Rallus crepitans" -} -item { - name: "58561" - id: 411 - display_name: "Libytheana carinenta" -} -item { - name: "58563" - id: 412 - display_name: "Speyeria aphrodite" -} -item { - name: "58564" - id: 413 - display_name: "Boloria bellona" -} -item { - name: "413489" - id: 414 - display_name: "Nestor meridionalis septentrionalis" -} -item { - name: "42184" - id: 415 - display_name: "Capreolus capreolus" -} -item { - name: "9419" - id: 416 - display_name: "Pipilo chlorurus" -} -item { - name: "9420" - id: 417 - display_name: "Pipilo maculatus" -} -item { - name: "9424" - id: 418 - display_name: "Pipilo erythrophthalmus" -} -item { - name: "99539" - id: 419 - display_name: "Dorocordulia libera" -} -item { - name: "58580" - id: 420 - display_name: "Polygonia progne" -} -item { - name: "58581" - id: 421 - display_name: "Nymphalis vaualbum" -} -item { - name: "42199" - id: 422 - display_name: "Rangifer tarandus" -} -item { - name: "58586" - id: 423 - display_name: "Limenitis archippus" -} -item { - name: "58587" - id: 424 - display_name: "Asterocampa clyton" -} -item { - name: "42206" - id: 425 - display_name: "Cervus elaphus" -} -item { - name: "312543" - id: 426 - display_name: "Anartia jatrophae luteipicta" -} -item { - name: "204094" - id: 427 - display_name: "Cairina moschata domestica" -} -item { - name: "4304" - id: 428 - display_name: "Phalacrocorax varius" -} -item { - name: "42210" - id: 429 - display_name: "Cervus nippon" -} -item { - name: "17638" - id: 430 - display_name: "Picoides dorsalis" -} -item { - name: "132330" - id: 431 - display_name: "Chlosyne janais" -} -item { - name: "58603" - id: 432 - display_name: "Megisto cymela" -} -item { - name: "42220" - id: 433 - display_name: "Odocoileus hemionus" -} -item { - name: "17645" - id: 434 - display_name: "Picoides nuttallii" -} -item { - name: "58606" - id: 435 - display_name: "Cercyonis pegala" -} -item { - name: "42223" - id: 436 - display_name: "Odocoileus virginianus" -} -item { - name: "58609" - id: 437 - display_name: "Lepisosteus osseus" -} -item { - name: "17650" - id: 438 - display_name: "Picoides scalaris" -} -item { - name: "132339" - id: 439 - display_name: "Anthanassa texana" -} -item { - name: "58612" - id: 440 - display_name: "Carassius auratus" -} -item { - name: "1406" - id: 441 - display_name: "Callipepla gambelii" -} -item { - name: "9462" - id: 442 - display_name: "Pyrrhula pyrrhula" -} -item { - name: "4308" - id: 443 - display_name: "Phalacrocorax brasilianus" -} -item { - name: "17660" - id: 444 - display_name: "Picoides pubescens" -} -item { - name: "1280" - id: 445 - display_name: "Colinus virginianus" -} -item { - name: "129920" - id: 446 - display_name: "Calliostoma ligatum" -} -item { - name: "58627" - id: 447 - display_name: "Perca flavescens" -} -item { - name: "148742" - id: 448 - display_name: "Hamadryas februa" -} -item { - name: "39809" - id: 449 - display_name: "Terrapene ornata ornata" -} -item { - name: "115979" - id: 450 - display_name: "Plestiodon skiltonianus skiltonianus" -} -item { - name: "9484" - id: 451 - display_name: "Sporophila torqueola" -} -item { - name: "17678" - id: 452 - display_name: "Picoides villosus" -} -item { - name: "3862" - id: 453 - display_name: "Calidris pusilla" -} -item { - name: "70421" - id: 454 - display_name: "Acris blanchardi" -} -item { - name: "124183" - id: 455 - display_name: "Phlogophora periculosa" -} -item { - name: "124184" - id: 456 - display_name: "Plodia interpunctella" -} -item { - name: "99609" - id: 457 - display_name: "Dromogomphus spinosus" -} -item { - name: "99610" - id: 458 - display_name: "Dromogomphus spoliatus" -} -item { - name: "17694" - id: 459 - display_name: "Picoides arcticus" -} -item { - name: "113521" - id: 460 - display_name: "Sympetrum pallipes" -} -item { - name: "320801" - id: 461 - display_name: "Aspidoscelis tesselata" -} -item { - name: "7047" - id: 462 - display_name: "Aythya marila" -} -item { - name: "4317" - id: 463 - display_name: "Phaethon aethereus" -} -item { - name: "81606" - id: 464 - display_name: "Littorina littorea" -} -item { - name: "99891" - id: 465 - display_name: "Enallagma aspersum" -} -item { - name: "9528" - id: 466 - display_name: "Sturnella magna" -} -item { - name: "99641" - id: 467 - display_name: "Dythemis fugax" -} -item { - name: "99644" - id: 468 - display_name: "Dythemis nigrescens" -} -item { - name: "39818" - id: 469 - display_name: "Terrapene carolina triunguis" -} -item { - name: "99647" - id: 470 - display_name: "Dythemis velox" -} -item { - name: "148800" - id: 471 - display_name: "Chioides albofasciatus" -} -item { - name: "19339" - id: 472 - display_name: "Melopsittacus undulatus" -} -item { - name: "47509" - id: 473 - display_name: "Diaulula sandiegensis" -} -item { - name: "148810" - id: 474 - display_name: "Anaea aidea" -} -item { - name: "123070" - id: 475 - display_name: "Capra hircus" -} -item { - name: "7054" - id: 476 - display_name: "Aythya affinis" -} -item { - name: "99897" - id: 477 - display_name: "Enallagma civile" -} -item { - name: "42328" - id: 478 - display_name: "Kobus ellipsiprymnus" -} -item { - name: "48328" - id: 479 - display_name: "Aurelia aurita" -} -item { - name: "132445" - id: 480 - display_name: "Conchylodes ovulalis" -} -item { - name: "215271" - id: 481 - display_name: "Bleptina caradrinalis" -} -item { - name: "83297" - id: 482 - display_name: "Scarus rubroviolaceus" -} -item { - name: "42347" - id: 483 - display_name: "Rupicapra rupicapra" -} -item { - name: "7058" - id: 484 - display_name: "Aythya novaeseelandiae" -} -item { - name: "52457" - id: 485 - display_name: "Chaetodon auriga" -} -item { - name: "1392" - id: 486 - display_name: "Cyrtonyx montezumae" -} -item { - name: "4328" - id: 487 - display_name: "Pelecanus occidentalis" -} -item { - name: "7647" - id: 488 - display_name: "Cinclus cinclus" -} -item { - name: "148856" - id: 489 - display_name: "Anteos clorinde" -} -item { - name: "7060" - id: 490 - display_name: "Chen rossii" -} -item { - name: "58750" - id: 491 - display_name: "Nomophila nearctica" -} -item { - name: "1409" - id: 492 - display_name: "Callipepla californica" -} -item { - name: "9602" - id: 493 - display_name: "Quiscalus quiscula" -} -item { - name: "296326" - id: 494 - display_name: "Oncopeltus sexmaculatus" -} -item { - name: "9607" - id: 495 - display_name: "Quiscalus mexicanus" -} -item { - name: "319724" - id: 496 - display_name: "Euphoria kernii" -} -item { - name: "1419" - id: 497 - display_name: "Callipepla squamata" -} -item { - name: "148883" - id: 498 - display_name: "Eantis tamenund" -} -item { - name: "42391" - id: 499 - display_name: "Ovis canadensis" -} -item { - name: "107937" - id: 500 - display_name: "Orthemis discolor" -} -item { - name: "42405" - id: 501 - display_name: "Syncerus caffer" -} -item { - name: "42408" - id: 502 - display_name: "Bison bison" -} -item { - name: "116137" - id: 503 - display_name: "Sceloporus cowlesi" -} -item { - name: "326296" - id: 504 - display_name: "Bufo bufo" -} -item { - name: "148907" - id: 505 - display_name: "Cydia latiferreana" -} -item { - name: "42414" - id: 506 - display_name: "Oreamnos americanus" -} -item { - name: "116143" - id: 507 - display_name: "Sceloporus tristichus" -} -item { - name: "99912" - id: 508 - display_name: "Enallagma geminatum" -} -item { - name: "226889" - id: 509 - display_name: "Pangrapta decoralis" -} -item { - name: "42429" - id: 510 - display_name: "Antilocapra americana" -} -item { - name: "17855" - id: 511 - display_name: "Dryocopus pileatus" -} -item { - name: "107974" - id: 512 - display_name: "Orthetrum sabina" -} -item { - name: "56225" - id: 513 - display_name: "Polygonia c-album" -} -item { - name: "67016" - id: 514 - display_name: "Rana draytonii" -} -item { - name: "132553" - id: 515 - display_name: "Strymon istapa" -} -item { - name: "73155" - id: 516 - display_name: "Passerina caerulea" -} -item { - name: "26074" - id: 517 - display_name: "Crocodylus moreletii" -} -item { - name: "171903" - id: 518 - display_name: "Oligyra orbiculata" -} -item { - name: "26085" - id: 519 - display_name: "Crocodylus acutus" -} -item { - name: "143613" - id: 520 - display_name: "Homophoberia apicosa" -} -item { - name: "5715" - id: 521 - display_name: "Amazilia beryllina" -} -item { - name: "9721" - id: 522 - display_name: "Geothlypis trichas" -} -item { - name: "154446" - id: 523 - display_name: "Lambdina fiscellaria" -} -item { - name: "236841" - id: 524 - display_name: "Lichanura orcutti" -} -item { - name: "20737" - id: 525 - display_name: "Trogon melanocephalus" -} -item { - name: "124431" - id: 526 - display_name: "Cycloneda sanguinea" -} -item { - name: "124432" - id: 527 - display_name: "Deroceras reticulatum" -} -item { - name: "39566" - id: 528 - display_name: "Apalone ferox" -} -item { - name: "149017" - id: 529 - display_name: "Chlorochlamys chloroleucaria" -} -item { - name: "15281" - id: 530 - display_name: "Sylvia communis" -} -item { - name: "312873" - id: 531 - display_name: "Anartia fatima fatima" -} -item { - name: "9771" - id: 532 - display_name: "Pinicola enucleator" -} -item { - name: "39858" - id: 533 - display_name: "Graptemys geographica" -} -item { - name: "26159" - id: 534 - display_name: "Alligator mississippiensis" -} -item { - name: "304690" - id: 535 - display_name: "Naupactus cervinus" -} -item { - name: "124467" - id: 536 - display_name: "Pseudosphinx tetrio" -} -item { - name: "99892" - id: 537 - display_name: "Enallagma basidens" -} -item { - name: "99895" - id: 538 - display_name: "Enallagma carunculatum" -} -item { - name: "67129" - id: 539 - display_name: "Rhinella marina" -} -item { - name: "83515" - id: 540 - display_name: "Oxybelis aeneus" -} -item { - name: "81681" - id: 541 - display_name: "Campaea perlata" -} -item { - name: "99901" - id: 542 - display_name: "Enallagma cyathigerum" -} -item { - name: "99911" - id: 543 - display_name: "Enallagma exsulans" -} -item { - name: "9800" - id: 544 - display_name: "Coccothraustes vespertinus" -} -item { - name: "9801" - id: 545 - display_name: "Coccothraustes coccothraustes" -} -item { - name: "154551" - id: 546 - display_name: "Leptoglossus zonatus" -} -item { - name: "9807" - id: 547 - display_name: "Vermivora chrysoptera" -} -item { - name: "61157" - id: 548 - display_name: "Trichodes ornatus" -} -item { - name: "99924" - id: 549 - display_name: "Enallagma signatum" -} -item { - name: "1626" - id: 550 - display_name: "Opisthocomus hoazin" -} -item { - name: "132704" - id: 551 - display_name: "Setophaga coronata coronata" -} -item { - name: "119056" - id: 552 - display_name: "Centruroides vittatus" -} -item { - name: "50786" - id: 553 - display_name: "Vanessa annabella" -} -item { - name: "60347" - id: 554 - display_name: "Pituophis catenifer sayi" -} -item { - name: "9833" - id: 555 - display_name: "Diglossa baritula" -} -item { - name: "132718" - id: 556 - display_name: "Scathophaga stercoraria" -} -item { - name: "132719" - id: 557 - display_name: "Calopteron reticulatum" -} -item { - name: "116340" - id: 558 - display_name: "Dreissena polymorpha" -} -item { - name: "134078" - id: 559 - display_name: "Scoliopteryx libatrix" -} -item { - name: "9850" - id: 560 - display_name: "Saltator coerulescens" -} -item { - name: "117695" - id: 561 - display_name: "Cucumaria miniata" -} -item { - name: "9854" - id: 562 - display_name: "Saltator atriceps" -} -item { - name: "132736" - id: 563 - display_name: "Urola nivalis" -} -item { - name: "34435" - id: 564 - display_name: "Hemidactylus turcicus" -} -item { - name: "9864" - id: 565 - display_name: "Sicalis flaveola" -} -item { - name: "7106" - id: 566 - display_name: "Aix galericulata" -} -item { - name: "485010" - id: 567 - display_name: "Chinavia hilaris" -} -item { - name: "132764" - id: 568 - display_name: "Junco hyemalis hyemalis" -} -item { - name: "367558" - id: 569 - display_name: "Eupsittula canicularis" -} -item { - name: "370351" - id: 570 - display_name: "Microcarbo melanoleucos" -} -item { - name: "50867" - id: 571 - display_name: "Argiope bruennichi" -} -item { - name: "67252" - id: 572 - display_name: "Trachycephalus typhonius" -} -item { - name: "132789" - id: 573 - display_name: "Clepsis peritana" -} -item { - name: "9915" - id: 574 - display_name: "Piranga rubra" -} -item { - name: "50880" - id: 575 - display_name: "Limenitis lorquini" -} -item { - name: "9921" - id: 576 - display_name: "Piranga olivacea" -} -item { - name: "100034" - id: 577 - display_name: "Epiaeschna heros" -} -item { - name: "9924" - id: 578 - display_name: "Piranga flava" -} -item { - name: "42339" - id: 579 - display_name: "Tragelaphus strepsiceros" -} -item { - name: "50892" - id: 580 - display_name: "Euphydryas chalcedona" -} -item { - name: "130348" - id: 581 - display_name: "Dione moneta" -} -item { - name: "394966" - id: 582 - display_name: "Phaulacridium marginale" -} -item { - name: "9943" - id: 583 - display_name: "Amphispiza bilineata" -} -item { - name: "4388" - id: 584 - display_name: "Larus dominicanus" -} -item { - name: "1758" - id: 585 - display_name: "Piaya cayana" -} -item { - name: "50913" - id: 586 - display_name: "Hyalophora euryalus" -} -item { - name: "9958" - id: 587 - display_name: "Aimophila ruficeps" -} -item { - name: "59115" - id: 588 - display_name: "Gambusia affinis" -} -item { - name: "64346" - id: 589 - display_name: "Natrix tessellata" -} -item { - name: "59119" - id: 590 - display_name: "Pontia protodice" -} -item { - name: "18160" - id: 591 - display_name: "Melanerpes lewis" -} -item { - name: "18161" - id: 592 - display_name: "Melanerpes uropygialis" -} -item { - name: "50931" - id: 593 - display_name: "Strymon melinus" -} -item { - name: "59124" - id: 594 - display_name: "Anthocharis sara" -} -item { - name: "59127" - id: 595 - display_name: "Lycaena helloides" -} -item { - name: "59128" - id: 596 - display_name: "Atlides halesus" -} -item { - name: "67324" - id: 597 - display_name: "Eurema daira" -} -item { - name: "9981" - id: 598 - display_name: "Passerculus sandwichensis" -} -item { - name: "59134" - id: 599 - display_name: "Satyrium sylvinus" -} -item { - name: "67327" - id: 600 - display_name: "Schistocerca obscura" -} -item { - name: "67328" - id: 601 - display_name: "Pholcus phalangioides" -} -item { - name: "59138" - id: 602 - display_name: "Satyrium saepium" -} -item { - name: "132867" - id: 603 - display_name: "Microtia elva" -} -item { - name: "18181" - id: 604 - display_name: "Melanerpes pucherani" -} -item { - name: "7486" - id: 605 - display_name: "Salpinctes obsoletus" -} -item { - name: "108303" - id: 606 - display_name: "Paltothemis lineatipes" -} -item { - name: "59152" - id: 607 - display_name: "Leptotes marina" -} -item { - name: "132881" - id: 608 - display_name: "Catocala ultronia" -} -item { - name: "143662" - id: 609 - display_name: "Orthosoma brunneum" -} -item { - name: "59164" - id: 610 - display_name: "Plebejus icarioides" -} -item { - name: "18205" - id: 611 - display_name: "Melanerpes carolinus" -} -item { - name: "18206" - id: 612 - display_name: "Melanerpes chrysogenys" -} -item { - name: "83744" - id: 613 - display_name: "Amblyomma americanum" -} -item { - name: "18209" - id: 614 - display_name: "Melanerpes formicivorus" -} -item { - name: "116517" - id: 615 - display_name: "Caiman crocodilus" -} -item { - name: "59176" - id: 616 - display_name: "Phyciodes mylitta" -} -item { - name: "59182" - id: 617 - display_name: "Euphydryas editha" -} -item { - name: "43997" - id: 618 - display_name: "Myocastor coypus" -} -item { - name: "59185" - id: 619 - display_name: "Coenonympha tullia" -} -item { - name: "59187" - id: 620 - display_name: "Erynnis propertius" -} -item { - name: "59188" - id: 621 - display_name: "Erynnis funeralis" -} -item { - name: "59189" - id: 622 - display_name: "Erynnis tristis" -} -item { - name: "59190" - id: 623 - display_name: "Heliopetes ericetorum" -} -item { - name: "34615" - id: 624 - display_name: "Gekko gecko" -} -item { - name: "42808" - id: 625 - display_name: "Trichosurus vulpecula" -} -item { - name: "59194" - id: 626 - display_name: "Ochlodes sylvanoides" -} -item { - name: "59195" - id: 627 - display_name: "Lerodea eufala" -} -item { - name: "18236" - id: 628 - display_name: "Colaptes auratus" -} -item { - name: "10045" - id: 629 - display_name: "Basileuterus rufifrons" -} -item { - name: "59202" - id: 630 - display_name: "Larus michahellis" -} -item { - name: "10053" - id: 631 - display_name: "Ramphocelus passerinii" -} -item { - name: "19975" - id: 632 - display_name: "Athene cunicularia" -} -item { - name: "82231" - id: 633 - display_name: "Periplaneta americana" -} -item { - name: "67409" - id: 634 - display_name: "Gobiesox maeandricus" -} -item { - name: "83795" - id: 635 - display_name: "Cipangopaludina chinensis" -} -item { - name: "59220" - id: 636 - display_name: "Branta hutchinsii" -} -item { - name: "10069" - id: 637 - display_name: "Fringilla montifringilla" -} -item { - name: "10070" - id: 638 - display_name: "Fringilla coelebs" -} -item { - name: "83802" - id: 639 - display_name: "Megacyllene robiniae" -} -item { - name: "83804" - id: 640 - display_name: "Dynastes tityus" -} -item { - name: "51039" - id: 641 - display_name: "Cepaea hortensis" -} -item { - name: "68062" - id: 642 - display_name: "Menemerus bivittatus" -} -item { - name: "47527" - id: 643 - display_name: "Ostracion meleagris" -} -item { - name: "67435" - id: 644 - display_name: "Urbanus proteus" -} -item { - name: "10094" - id: 645 - display_name: "Junco hyemalis" -} -item { - name: "67440" - id: 646 - display_name: "Utetheisa ornatrix" -} -item { - name: "100210" - id: 647 - display_name: "Epitheca canis" -} -item { - name: "1907" - id: 648 - display_name: "Cuculus canorus" -} -item { - name: "100215" - id: 649 - display_name: "Epitheca princeps" -} -item { - name: "27826" - id: 650 - display_name: "Taricha granulosa" -} -item { - name: "129147" - id: 651 - display_name: "Ammophila procera" -} -item { - name: "10111" - id: 652 - display_name: "Junco phaeonotus" -} -item { - name: "83844" - id: 653 - display_name: "Oxyopes salticus" -} -item { - name: "144107" - id: 654 - display_name: "Tetracis crocallata" -} -item { - name: "51097" - id: 655 - display_name: "Papilio zelicaon" -} -item { - name: "10138" - id: 656 - display_name: "Ammodramus nelsoni" -} -item { - name: "10139" - id: 657 - display_name: "Ammodramus savannarum" -} -item { - name: "10147" - id: 658 - display_name: "Ammodramus maritimus" -} -item { - name: "59300" - id: 659 - display_name: "Anagrapha falcifera" -} -item { - name: "51110" - id: 660 - display_name: "Xylocopa virginica" -} -item { - name: "1960" - id: 661 - display_name: "Coccyzus erythropthalmus" -} -item { - name: "42652" - id: 662 - display_name: "Didelphis virginiana" -} -item { - name: "428606" - id: 663 - display_name: "Heraclides rumiko" -} -item { - name: "127303" - id: 664 - display_name: "Callophrys henrici" -} -item { - name: "1964" - id: 665 - display_name: "Coccyzus minor" -} -item { - name: "1965" - id: 666 - display_name: "Coccyzus americanus" -} -item { - name: "8520" - id: 667 - display_name: "Nucifraga columbiana" -} -item { - name: "116658" - id: 668 - display_name: "Siphanta acuta" -} -item { - name: "1972" - id: 669 - display_name: "Crotophaga sulcirostris" -} -item { - name: "10168" - id: 670 - display_name: "Pooecetes gramineus" -} -item { - name: "53893" - id: 671 - display_name: "Chlosyne palla" -} -item { - name: "10173" - id: 672 - display_name: "Arremonops rufivirgatus" -} -item { - name: "1986" - id: 673 - display_name: "Geococcyx californianus" -} -item { - name: "1987" - id: 674 - display_name: "Geococcyx velox" -} -item { - name: "116680" - id: 675 - display_name: "Tabanus atratus" -} -item { - name: "116681" - id: 676 - display_name: "Atteva aurea" -} -item { - name: "124875" - id: 677 - display_name: "Spodoptera litura" -} -item { - name: "26575" - id: 678 - display_name: "Diadophis punctatus" -} -item { - name: "10199" - id: 679 - display_name: "Coereba flaveola" -} -item { - name: "26591" - id: 680 - display_name: "Diadophis punctatus edwardsii" -} -item { - name: "59360" - id: 681 - display_name: "Neverita duplicata" -} -item { - name: "68263" - id: 682 - display_name: "Papilio multicaudata" -} -item { - name: "26598" - id: 683 - display_name: "Diadophis punctatus amabilis" -} -item { - name: "42983" - id: 684 - display_name: "Phascolarctos cinereus" -} -item { - name: "67560" - id: 685 - display_name: "Adelpha californica" -} -item { - name: "10224" - id: 686 - display_name: "Passerina ciris" -} -item { - name: "2038" - id: 687 - display_name: "Alectura lathami" -} -item { - name: "10232" - id: 688 - display_name: "Passerina leclancherii" -} -item { - name: "10234" - id: 689 - display_name: "Passerina amoena" -} -item { - name: "10243" - id: 690 - display_name: "Icteria virens" -} -item { - name: "2052" - id: 691 - display_name: "Crax rubra" -} -item { - name: "94551" - id: 692 - display_name: "Argia immunda" -} -item { - name: "2062" - id: 693 - display_name: "Penelope purpurascens" -} -item { - name: "204490" - id: 694 - display_name: "Copsychus malabaricus" -} -item { - name: "10257" - id: 695 - display_name: "Paroaria capitata" -} -item { - name: "51221" - id: 696 - display_name: "Procambarus clarkii" -} -item { - name: "10262" - id: 697 - display_name: "Cyanerpes cyaneus" -} -item { - name: "508249" - id: 698 - display_name: "Microcarbo melanoleucos brevirostris" -} -item { - name: "18460" - id: 699 - display_name: "Sphyrapicus thyroideus" -} -item { - name: "10271" - id: 700 - display_name: "Pheucticus ludovicianus" -} -item { - name: "18464" - id: 701 - display_name: "Sphyrapicus ruber" -} -item { - name: "10274" - id: 702 - display_name: "Pheucticus melanocephalus" -} -item { - name: "18467" - id: 703 - display_name: "Sphyrapicus nuchalis" -} -item { - name: "100391" - id: 704 - display_name: "Erythrodiplax berenice" -} -item { - name: "2089" - id: 705 - display_name: "Ortalis poliocephala" -} -item { - name: "2090" - id: 706 - display_name: "Ortalis vetula" -} -item { - name: "8038" - id: 707 - display_name: "Corvus albus" -} -item { - name: "67629" - id: 708 - display_name: "Oligocottus maculosus" -} -item { - name: "10286" - id: 709 - display_name: "Mniotilta varia" -} -item { - name: "10288" - id: 710 - display_name: "Volatinia jacarina" -} -item { - name: "100403" - id: 711 - display_name: "Erythrodiplax minuscula" -} -item { - name: "84023" - id: 712 - display_name: "Amorpha juglandis" -} -item { - name: "84024" - id: 713 - display_name: "Galasa nigrinodis" -} -item { - name: "10297" - id: 714 - display_name: "Thraupis palmarum" -} -item { - name: "67642" - id: 715 - display_name: "Pantherophis spiloides" -} -item { - name: "67653" - id: 716 - display_name: "Phoebis agarithe" -} -item { - name: "84038" - id: 717 - display_name: "Haploa lecontei" -} -item { - name: "26695" - id: 718 - display_name: "Scaphiopus holbrookii" -} -item { - name: "84040" - id: 719 - display_name: "Chauliognathus marginatus" -} -item { - name: "51275" - id: 720 - display_name: "Pentatoma rufipes" -} -item { - name: "2124" - id: 721 - display_name: "Momotus mexicanus" -} -item { - name: "26702" - id: 722 - display_name: "Spea hammondii" -} -item { - name: "10325" - id: 723 - display_name: "Euphagus cyanocephalus" -} -item { - name: "43102" - id: 724 - display_name: "Sylvilagus palustris" -} -item { - name: "49509" - id: 725 - display_name: "Lutjanus griseus" -} -item { - name: "116834" - id: 726 - display_name: "Cacatua galerita" -} -item { - name: "127188" - id: 727 - display_name: "Junco hyemalis oreganus" -} -item { - name: "26725" - id: 728 - display_name: "Ambystoma jeffersonianum" -} -item { - name: "43111" - id: 729 - display_name: "Sylvilagus floridanus" -} -item { - name: "43112" - id: 730 - display_name: "Sylvilagus bachmani" -} -item { - name: "67691" - id: 731 - display_name: "Lophocampa maculata" -} -item { - name: "51311" - id: 732 - display_name: "Urbanus dorantes" -} -item { - name: "67700" - id: 733 - display_name: "Caracolus caracolla" -} -item { - name: "43128" - id: 734 - display_name: "Lepus europaeus" -} -item { - name: "26745" - id: 735 - display_name: "Ambystoma texanum" -} -item { - name: "67706" - id: 736 - display_name: "Argiope argentata" -} -item { - name: "26747" - id: 737 - display_name: "Ambystoma gracile" -} -item { - name: "67708" - id: 738 - display_name: "Argiope trifasciata" -} -item { - name: "26749" - id: 739 - display_name: "Ambystoma tigrinum" -} -item { - name: "4896" - id: 740 - display_name: "Pluvialis fulva" -} -item { - name: "10369" - id: 741 - display_name: "Molothrus aeneus" -} -item { - name: "26754" - id: 742 - display_name: "Ambystoma macrodactylum" -} -item { - name: "10373" - id: 743 - display_name: "Molothrus ater" -} -item { - name: "2185" - id: 744 - display_name: "Merops pusillus" -} -item { - name: "84109" - id: 745 - display_name: "Pisaurina mira" -} -item { - name: "67726" - id: 746 - display_name: "Aeshna palmata" -} -item { - name: "2191" - id: 747 - display_name: "Merops apiaster" -} -item { - name: "67731" - id: 748 - display_name: "Anax junius" -} -item { - name: "198804" - id: 749 - display_name: "Satyrium titus" -} -item { - name: "51349" - id: 750 - display_name: "Pyrgus communis" -} -item { - name: "18584" - id: 751 - display_name: "Pteroglossus torquatus" -} -item { - name: "67737" - id: 752 - display_name: "Rhionaeschna multicolor" -} -item { - name: "198812" - id: 753 - display_name: "Lethe anthedon" -} -item { - name: "321697" - id: 754 - display_name: "Melanchroia chephise" -} -item { - name: "198821" - id: 755 - display_name: "Pieris oleracea" -} -item { - name: "26790" - id: 756 - display_name: "Ambystoma maculatum" -} -item { - name: "10411" - id: 757 - display_name: "Loxia curvirostra" -} -item { - name: "133295" - id: 758 - display_name: "Melitaea didyma" -} -item { - name: "67760" - id: 759 - display_name: "Popillia japonica" -} -item { - name: "43188" - id: 760 - display_name: "Ochotona princeps" -} -item { - name: "2229" - id: 761 - display_name: "Merops orientalis" -} -item { - name: "10423" - id: 762 - display_name: "Loxia leucoptera" -} -item { - name: "67771" - id: 763 - display_name: "Leptoglossus occidentalis" -} -item { - name: "84162" - id: 764 - display_name: "Chrysochus auratus" -} -item { - name: "26822" - id: 765 - display_name: "Dicamptodon tenebrosus" -} -item { - name: "26823" - id: 766 - display_name: "Dicamptodon ensatus" -} -item { - name: "51402" - id: 767 - display_name: "Megalops atlanticus" -} -item { - name: "67725" - id: 768 - display_name: "Aeshna interrupta" -} -item { - name: "411858" - id: 769 - display_name: "Vanessa gonerilla gonerilla" -} -item { - name: "26835" - id: 770 - display_name: "Drymobius margaritiferus" -} -item { - name: "84185" - id: 771 - display_name: "Megalopyge opercularis" -} -item { - name: "2266" - id: 772 - display_name: "Coracias garrulus" -} -item { - name: "141531" - id: 773 - display_name: "Lethe eurydice" -} -item { - name: "2269" - id: 774 - display_name: "Coracias caudatus" -} -item { - name: "133346" - id: 775 - display_name: "Melittia cucurbitae" -} -item { - name: "2275" - id: 776 - display_name: "Coracias benghalensis" -} -item { - name: "84196" - id: 777 - display_name: "Pontania californica" -} -item { - name: "10470" - id: 778 - display_name: "Xanthocephalus xanthocephalus" -} -item { - name: "10479" - id: 779 - display_name: "Chondestes grammacus" -} -item { - name: "51440" - id: 780 - display_name: "Pituophis catenifer catenifer" -} -item { - name: "54087" - id: 781 - display_name: "Pieris napi" -} -item { - name: "59635" - id: 782 - display_name: "Phragmatopoma californica" -} -item { - name: "10487" - id: 783 - display_name: "Dolichonyx oryzivorus" -} -item { - name: "67835" - id: 784 - display_name: "Danaus chrysippus" -} -item { - name: "59644" - id: 785 - display_name: "Pantherophis alleghaniensis" -} -item { - name: "59646" - id: 786 - display_name: "Pantherophis bairdi" -} -item { - name: "116999" - id: 787 - display_name: "Pandion haliaetus" -} -item { - name: "117002" - id: 788 - display_name: "Phainopepla nitens" -} -item { - name: "16770" - id: 789 - display_name: "Tyrannus couchii" -} -item { - name: "84239" - id: 790 - display_name: "Callophrys gryneus" -} -item { - name: "104553" - id: 791 - display_name: "Leucorrhinia proxima" -} -item { - name: "117016" - id: 792 - display_name: "Phylloscopus collybita" -} -item { - name: "49540" - id: 793 - display_name: "Gasteracantha cancriformis" -} -item { - name: "59675" - id: 794 - display_name: "Pyrrharctia isabella" -} -item { - name: "469277" - id: 795 - display_name: "Neotibicen superbus" -} -item { - name: "236973" - id: 796 - display_name: "Circus cyaneus hudsonius" -} -item { - name: "59683" - id: 797 - display_name: "Porpita porpita" -} -item { - name: "26916" - id: 798 - display_name: "Contia tenuis" -} -item { - name: "51493" - id: 799 - display_name: "Trimerotropis pallidipennis" -} -item { - name: "51495" - id: 800 - display_name: "Anthocharis cardamines" -} -item { - name: "133416" - id: 801 - display_name: "Phoebis philea" -} -item { - name: "8583" - id: 802 - display_name: "Grallina cyanoleuca" -} -item { - name: "395569" - id: 803 - display_name: "Prionoplus reticularis" -} -item { - name: "59698" - id: 804 - display_name: "Velella velella" -} -item { - name: "141626" - id: 805 - display_name: "Lygaeus turcicus" -} -item { - name: "84286" - id: 806 - display_name: "Diapheromera femorata" -} -item { - name: "117059" - id: 807 - display_name: "Plectrophenax nivalis" -} -item { - name: "133447" - id: 808 - display_name: "Crambus agitatellus" -} -item { - name: "133448" - id: 809 - display_name: "Climaciella brunnea" -} -item { - name: "51534" - id: 810 - display_name: "Leptotes cassius" -} -item { - name: "205197" - id: 811 - display_name: "Eutrapela clemataria" -} -item { - name: "51536" - id: 812 - display_name: "Ascia monuste" -} -item { - name: "10585" - id: 813 - display_name: "Calamospiza melanocorys" -} -item { - name: "49552" - id: 814 - display_name: "Scutigera coleoptrata" -} -item { - name: "51555" - id: 815 - display_name: "Sympetrum illotum" -} -item { - name: "51557" - id: 816 - display_name: "Bombylius major" -} -item { - name: "117095" - id: 817 - display_name: "Regulus calendula" -} -item { - name: "117097" - id: 818 - display_name: "Regulus ignicapilla" -} -item { - name: "117099" - id: 819 - display_name: "Regulus regulus" -} -item { - name: "117100" - id: 820 - display_name: "Regulus satrapa" -} -item { - name: "84333" - id: 821 - display_name: "Eudryas grata" -} -item { - name: "215409" - id: 822 - display_name: "Bradybaena similaris" -} -item { - name: "16787" - id: 823 - display_name: "Tyrannus melancholicus" -} -item { - name: "46225" - id: 824 - display_name: "Tamias dorsalis" -} -item { - name: "59774" - id: 825 - display_name: "Pachydiplax longipennis" -} -item { - name: "59776" - id: 826 - display_name: "Perithemis tenera" -} -item { - name: "119014" - id: 827 - display_name: "Argia fumipennis violacea" -} -item { - name: "4326" - id: 828 - display_name: "Pelecanus conspicillatus" -} -item { - name: "18833" - id: 829 - display_name: "Aulacorhynchus prasinus" -} -item { - name: "43411" - id: 830 - display_name: "Ateles geoffroyi" -} -item { - name: "141725" - id: 831 - display_name: "Nezara viridula" -} -item { - name: "51614" - id: 832 - display_name: "Eurema hecabe" -} -item { - name: "125343" - id: 833 - display_name: "Crepidula fornicata" -} -item { - name: "2464" - id: 834 - display_name: "Todiramphus sanctus" -} -item { - name: "43432" - id: 835 - display_name: "Cebus capucinus" -} -item { - name: "43436" - id: 836 - display_name: "Alouatta palliata" -} -item { - name: "43439" - id: 837 - display_name: "Alouatta pigra" -} -item { - name: "9357" - id: 838 - display_name: "Icterus bullockii" -} -item { - name: "84403" - id: 839 - display_name: "Phyllopalpus pulchellus" -} -item { - name: "10676" - id: 840 - display_name: "Spiza americana" -} -item { - name: "16798" - id: 841 - display_name: "Tyrannus dominicensis" -} -item { - name: "141752" - id: 842 - display_name: "Biblis hyperia" -} -item { - name: "4512" - id: 843 - display_name: "Chlidonias niger" -} -item { - name: "43460" - id: 844 - display_name: "Macaca mulatta" -} -item { - name: "51654" - id: 845 - display_name: "Junonia almana" -} -item { - name: "51659" - id: 846 - display_name: "Anthopleura xanthogrammica" -} -item { - name: "84428" - id: 847 - display_name: "Drepana arcuata" -} -item { - name: "10702" - id: 848 - display_name: "Oriturus superciliosus" -} -item { - name: "68047" - id: 849 - display_name: "Psarocolius montezuma" -} -item { - name: "12707" - id: 850 - display_name: "Turdus pilaris" -} -item { - name: "84437" - id: 851 - display_name: "Nicrophorus orbicollis" -} -item { - name: "84438" - id: 852 - display_name: "Platyprepia virginalis" -} -item { - name: "117209" - id: 853 - display_name: "Notiomystis cincta" -} -item { - name: "343393" - id: 854 - display_name: "Hypsopygia olinalis" -} -item { - name: "27101" - id: 855 - display_name: "Eurycea longicauda" -} -item { - name: "117214" - id: 856 - display_name: "Sagittarius serpentarius" -} -item { - name: "18911" - id: 857 - display_name: "Psittacula krameri" -} -item { - name: "117218" - id: 858 - display_name: "Verrucosa arenata" -} -item { - name: "117221" - id: 859 - display_name: "Dasymutilla occidentalis" -} -item { - name: "35303" - id: 860 - display_name: "Ctenosaura similis" -} -item { - name: "18920" - id: 861 - display_name: "Platycercus eximius" -} -item { - name: "10729" - id: 862 - display_name: "Protonotaria citrea" -} -item { - name: "35306" - id: 863 - display_name: "Ctenosaura pectinata" -} -item { - name: "109650" - id: 864 - display_name: "Platycnemis pennipes" -} -item { - name: "27120" - id: 865 - display_name: "Eurycea bislineata" -} -item { - name: "27123" - id: 866 - display_name: "Eurycea lucifuga" -} -item { - name: "51702" - id: 867 - display_name: "Coccinella septempunctata" -} -item { - name: "2552" - id: 868 - display_name: "Megaceryle torquata" -} -item { - name: "133625" - id: 869 - display_name: "Zanclognatha jacchusalis" -} -item { - name: "18943" - id: 870 - display_name: "Nestor meridionalis" -} -item { - name: "84481" - id: 871 - display_name: "Calopteryx maculata" -} -item { - name: "35330" - id: 872 - display_name: "Sauromalus ater" -} -item { - name: "27140" - id: 873 - display_name: "Coluber constrictor priapus" -} -item { - name: "199179" - id: 874 - display_name: "Polistes chinensis" -} -item { - name: "51724" - id: 875 - display_name: "Mopalia lignosa" -} -item { - name: "27149" - id: 876 - display_name: "Coluber constrictor constrictor" -} -item { - name: "35342" - id: 877 - display_name: "Iguana iguana" -} -item { - name: "27153" - id: 878 - display_name: "Coluber constrictor flaviventris" -} -item { - name: "35347" - id: 879 - display_name: "Amblyrhynchus cristatus" -} -item { - name: "125461" - id: 880 - display_name: "Ursus arctos horribilis" -} -item { - name: "84507" - id: 881 - display_name: "Lygus lineolaris" -} -item { - name: "35356" - id: 882 - display_name: "Dipsosaurus dorsalis" -} -item { - name: "51743" - id: 883 - display_name: "Danaus gilippus" -} -item { - name: "18976" - id: 884 - display_name: "Amazona viridigenalis" -} -item { - name: "125475" - id: 885 - display_name: "Plusiodonta compressipalpis" -} -item { - name: "51748" - id: 886 - display_name: "Danaus gilippus thersippus" -} -item { - name: "68137" - id: 887 - display_name: "Chlorocebus pygerythrus" -} -item { - name: "133675" - id: 888 - display_name: "Coenobita clypeatus" -} -item { - name: "215596" - id: 889 - display_name: "Buprestis aurulenta" -} -item { - name: "117293" - id: 890 - display_name: "Oecophylla smaragdina" -} -item { - name: "68142" - id: 891 - display_name: "Prenolepis imparis" -} -item { - name: "27184" - id: 892 - display_name: "Plethodon glutinosus" -} -item { - name: "27186" - id: 893 - display_name: "Plethodon cinereus" -} -item { - name: "18995" - id: 894 - display_name: "Amazona albifrons" -} -item { - name: "51765" - id: 895 - display_name: "Poanes melane" -} -item { - name: "18998" - id: 896 - display_name: "Amazona oratrix" -} -item { - name: "41396" - id: 897 - display_name: "Rhynchonycteris naso" -} -item { - name: "27194" - id: 898 - display_name: "Plethodon vehiculum" -} -item { - name: "51773" - id: 899 - display_name: "Nathalis iole" -} -item { - name: "12908" - id: 900 - display_name: "Saxicola rubetra" -} -item { - name: "68165" - id: 901 - display_name: "Linepithema humile" -} -item { - name: "154721" - id: 902 - display_name: "Brachygastra mellifica" -} -item { - name: "338504" - id: 903 - display_name: "Xanthocnemis zealandica" -} -item { - name: "338505" - id: 904 - display_name: "Melangyna novaezelandiae" -} -item { - name: "27093" - id: 905 - display_name: "Eurycea cirrigera" -} -item { - name: "65975" - id: 906 - display_name: "Lithobates berlandieri" -} -item { - name: "19020" - id: 907 - display_name: "Ara militaris" -} -item { - name: "474210" - id: 908 - display_name: "Spizelloides arborea" -} -item { - name: "205240" - id: 909 - display_name: "Pantographa limata" -} -item { - name: "27226" - id: 910 - display_name: "Plethodon albagula" -} -item { - name: "318545" - id: 911 - display_name: "Coreus marginatus" -} -item { - name: "2662" - id: 912 - display_name: "Ceryle rudis" -} -item { - name: "109161" - id: 913 - display_name: "Perithemis intensa" -} -item { - name: "51824" - id: 914 - display_name: "Calopteryx splendens" -} -item { - name: "27250" - id: 915 - display_name: "Ensatina eschscholtzii" -} -item { - name: "2676" - id: 916 - display_name: "Chloroceryle aenea" -} -item { - name: "2679" - id: 917 - display_name: "Chloroceryle amazona" -} -item { - name: "84602" - id: 918 - display_name: "Zale lunata" -} -item { - name: "133756" - id: 919 - display_name: "Leptoglossus oppositus" -} -item { - name: "35453" - id: 920 - display_name: "Zootoca vivipara" -} -item { - name: "84612" - id: 921 - display_name: "Polyphylla decemlineata" -} -item { - name: "133765" - id: 922 - display_name: "Eumenes fraternus" -} -item { - name: "68230" - id: 923 - display_name: "Brachymesia gravida" -} -item { - name: "49601" - id: 924 - display_name: "Mola mola" -} -item { - name: "68232" - id: 925 - display_name: "Papilio palamedes" -} -item { - name: "68233" - id: 926 - display_name: "Orthemis ferruginea" -} -item { - name: "68239" - id: 927 - display_name: "Parnassius clodius" -} -item { - name: "68240" - id: 928 - display_name: "Chlosyne lacinia" -} -item { - name: "68244" - id: 929 - display_name: "Euptoieta claudia" -} -item { - name: "68249" - id: 930 - display_name: "Dymasia dymas" -} -item { - name: "68251" - id: 931 - display_name: "Limenitis weidemeyerii" -} -item { - name: "133790" - id: 932 - display_name: "Chalybion californicum" -} -item { - name: "84644" - id: 933 - display_name: "Phalangium opilio" -} -item { - name: "68262" - id: 934 - display_name: "Polygonia faunus" -} -item { - name: "133799" - id: 935 - display_name: "Xenox tigrinus" -} -item { - name: "68264" - id: 936 - display_name: "Asterocampa celtis" -} -item { - name: "132892" - id: 937 - display_name: "Anacridium aegyptium" -} -item { - name: "68268" - id: 938 - display_name: "Euptoieta hegesia" -} -item { - name: "68269" - id: 939 - display_name: "Aglais milberti" -} -item { - name: "43694" - id: 940 - display_name: "Loxodonta africana" -} -item { - name: "59165" - id: 941 - display_name: "Apodemia mormo" -} -item { - name: "68274" - id: 942 - display_name: "Phyciodes phaon" -} -item { - name: "68275" - id: 943 - display_name: "Battus polydamas" -} -item { - name: "84662" - id: 944 - display_name: "Celastrina lucia" -} -item { - name: "16842" - id: 945 - display_name: "Myiozetetes similis" -} -item { - name: "133826" - id: 946 - display_name: "Zelus longipes" -} -item { - name: "14912" - id: 947 - display_name: "Toxostoma curvirostre" -} -item { - name: "53708" - id: 948 - display_name: "Pacifastacus leniusculus" -} -item { - name: "117452" - id: 949 - display_name: "Sphinx kalmiae" -} -item { - name: "182997" - id: 950 - display_name: "Megisto rubricata" -} -item { - name: "223965" - id: 951 - display_name: "Lithacodia musta" -} -item { - name: "125663" - id: 952 - display_name: "Kelletia kelletii" -} -item { - name: "125669" - id: 953 - display_name: "Rumina decollata" -} -item { - name: "68328" - id: 954 - display_name: "Oxythyrea funesta" -} -item { - name: "179324" - id: 955 - display_name: "Dactylotum bicolor" -} -item { - name: "68330" - id: 956 - display_name: "Arctia caja" -} -item { - name: "2548" - id: 957 - display_name: "Megaceryle alcyon" -} -item { - name: "207600" - id: 958 - display_name: "Thasus neocalifornicus" -} -item { - name: "207601" - id: 959 - display_name: "Palpita quadristigmalis" -} -item { - name: "51954" - id: 960 - display_name: "Sphecius speciosus" -} -item { - name: "207603" - id: 961 - display_name: "Prolimacodes badia" -} -item { - name: "7294" - id: 962 - display_name: "Eremophila alpestris" -} -item { - name: "19196" - id: 963 - display_name: "Alisterus scapularis" -} -item { - name: "145194" - id: 964 - display_name: "Cinnyris jugularis" -} -item { - name: "27390" - id: 965 - display_name: "Desmognathus ochrophaeus" -} -item { - name: "207615" - id: 966 - display_name: "Polistes apachus" -} -item { - name: "63275" - id: 967 - display_name: "Tremex columba" -} -item { - name: "61910" - id: 968 - display_name: "Orgyia antiqua" -} -item { - name: "199438" - id: 969 - display_name: "Orgyia postica" -} -item { - name: "43794" - id: 970 - display_name: "Castor canadensis" -} -item { - name: "84755" - id: 971 - display_name: "Arion rufus" -} -item { - name: "51996" - id: 972 - display_name: "Daphnis nerii" -} -item { - name: "194075" - id: 973 - display_name: "Drymarchon melanurus erebennus" -} -item { - name: "133923" - id: 974 - display_name: "Mermiria bivittata" -} -item { - name: "84778" - id: 975 - display_name: "Leptinotarsa decemlineata" -} -item { - name: "11051" - id: 976 - display_name: "Xiphorhynchus flavigaster" -} -item { - name: "121992" - id: 977 - display_name: "Cervus elaphus roosevelti" -} -item { - name: "27459" - id: 978 - display_name: "Batrachoseps attenuatus" -} -item { - name: "84806" - id: 979 - display_name: "Acanalonia conica" -} -item { - name: "52043" - id: 980 - display_name: "Spoladea recurvalis" -} -item { - name: "27468" - id: 981 - display_name: "Batrachoseps major" -} -item { - name: "133966" - id: 982 - display_name: "Lomographa vestaliata" -} -item { - name: "27474" - id: 983 - display_name: "Batrachoseps nigriventris" -} -item { - name: "101204" - id: 984 - display_name: "Gambusia holbrooki" -} -item { - name: "52055" - id: 985 - display_name: "Crocothemis servilia" -} -item { - name: "4580" - id: 986 - display_name: "Jacana jacana" -} -item { - name: "346970" - id: 987 - display_name: "Callophrys dumetorum" -} -item { - name: "27486" - id: 988 - display_name: "Pseudotriton ruber" -} -item { - name: "52075" - id: 989 - display_name: "Atalopedes campestris" -} -item { - name: "27500" - id: 990 - display_name: "Gyrinophilus porphyriticus" -} -item { - name: "73203" - id: 991 - display_name: "Phalaropus fulicarius" -} -item { - name: "322417" - id: 992 - display_name: "Limacus flavus" -} -item { - name: "40083" - id: 993 - display_name: "Gopherus berlandieri" -} -item { - name: "68469" - id: 994 - display_name: "Papilio demodocus" -} -item { - name: "2938" - id: 995 - display_name: "Streptopelia turtur" -} -item { - name: "117633" - id: 996 - display_name: "Mopalia muscosa" -} -item { - name: "117641" - id: 997 - display_name: "Nucella lamellosa" -} -item { - name: "322443" - id: 998 - display_name: "Thasus gigas" -} -item { - name: "68492" - id: 999 - display_name: "Hemidactylus mabouia" -} -item { - name: "143853" - id: 1000 - display_name: "Pica hudsonia" -} -item { - name: "144757" - id: 1001 - display_name: "Corvus cornix" -} -item { - name: "117650" - id: 1002 - display_name: "Mytilus edulis" -} -item { - name: "19349" - id: 1003 - display_name: "Myiopsitta monachus" -} -item { - name: "2969" - id: 1004 - display_name: "Streptopelia decaocto" -} -item { - name: "9919" - id: 1005 - display_name: "Piranga ludoviciana" -} -item { - name: "5009" - id: 1006 - display_name: "Ixobrychus exilis" -} -item { - name: "117666" - id: 1007 - display_name: "Pleuroncodes planipes" -} -item { - name: "7603" - id: 1008 - display_name: "Auriparus flaviceps" -} -item { - name: "117674" - id: 1009 - display_name: "Ligia occidentalis" -} -item { - name: "145223" - id: 1010 - display_name: "Geothlypis tolmiei" -} -item { - name: "60341" - id: 1011 - display_name: "Lithobates sphenocephalus" -} -item { - name: "60342" - id: 1012 - display_name: "Thamnophis proximus" -} -item { - name: "52155" - id: 1013 - display_name: "Dermacentor variabilis" -} -item { - name: "60349" - id: 1014 - display_name: "Scincella lateralis" -} -item { - name: "52158" - id: 1015 - display_name: "Schistocerca nitens" -} -item { - name: "117696" - id: 1016 - display_name: "Dendraster excentricus" -} -item { - name: "232391" - id: 1017 - display_name: "Tetracha carolina" -} -item { - name: "3017" - id: 1018 - display_name: "Columba livia" -} -item { - name: "145229" - id: 1019 - display_name: "Setophaga citrina" -} -item { - name: "84950" - id: 1020 - display_name: "Alypia octomaculata" -} -item { - name: "52188" - id: 1021 - display_name: "Rhincodon typus" -} -item { - name: "494559" - id: 1022 - display_name: "Polydrusus formosus" -} -item { - name: "145232" - id: 1023 - display_name: "Setophaga cerulea" -} -item { - name: "3048" - id: 1024 - display_name: "Columba palumbus" -} -item { - name: "9922" - id: 1025 - display_name: "Piranga bidentata" -} -item { - name: "44026" - id: 1026 - display_name: "Erethizon dorsatum" -} -item { - name: "61505" - id: 1027 - display_name: "Manduca sexta" -} -item { - name: "84994" - id: 1028 - display_name: "Acanthocephala declivis" -} -item { - name: "27652" - id: 1029 - display_name: "Hemidactylium scutatum" -} -item { - name: "117767" - id: 1030 - display_name: "Cervus elaphus nannodes" -} -item { - name: "494603" - id: 1031 - display_name: "Hermissenda opalescens" -} -item { - name: "39819" - id: 1032 - display_name: "Terrapene carolina bauri" -} -item { - name: "3093" - id: 1033 - display_name: "Patagioenas leucocephala" -} -item { - name: "205316" - id: 1034 - display_name: "Aidemona azteca" -} -item { - name: "216093" - id: 1035 - display_name: "Caracolus marginella" -} -item { - name: "44062" - id: 1036 - display_name: "Thomomys bottae" -} -item { - name: "85024" - id: 1037 - display_name: "Heraclides cresphontes" -} -item { - name: "3108" - id: 1038 - display_name: "Patagioenas fasciata" -} -item { - name: "213510" - id: 1039 - display_name: "Anageshna primordialis" -} -item { - name: "85030" - id: 1040 - display_name: "Crocothemis erythraea" -} -item { - name: "85034" - id: 1041 - display_name: "Neoscona crucifera" -} -item { - name: "3117" - id: 1042 - display_name: "Patagioenas flavirostris" -} -item { - name: "207924" - id: 1043 - display_name: "Synchlora frondaria" -} -item { - name: "35900" - id: 1044 - display_name: "Lacerta bilineata" -} -item { - name: "24382" - id: 1045 - display_name: "Osteopilus septentrionalis" -} -item { - name: "145249" - id: 1046 - display_name: "Setophaga discolor" -} -item { - name: "52297" - id: 1047 - display_name: "Triakis semifasciata" -} -item { - name: "27726" - id: 1048 - display_name: "Salamandra salamandra" -} -item { - name: "27727" - id: 1049 - display_name: "Bogertophis subocularis" -} -item { - name: "143043" - id: 1050 - display_name: "Cycnia tenera" -} -item { - name: "52313" - id: 1051 - display_name: "Diodon hystrix" -} -item { - name: "143316" - id: 1052 - display_name: "Schinia florida" -} -item { - name: "61968" - id: 1053 - display_name: "Graphosoma lineatum" -} -item { - name: "502885" - id: 1054 - display_name: "Lissachatina fulica" -} -item { - name: "71029" - id: 1055 - display_name: "Crotalus cerastes cerastes" -} -item { - name: "207977" - id: 1056 - display_name: "Aglais io" -} -item { - name: "19577" - id: 1057 - display_name: "Chordeiles minor" -} -item { - name: "93312" - id: 1058 - display_name: "Acropora palmata" -} -item { - name: "52354" - id: 1059 - display_name: "Ambystoma laterale" -} -item { - name: "19587" - id: 1060 - display_name: "Chordeiles acutipennis" -} -item { - name: "58585" - id: 1061 - display_name: "Limenitis arthemis astyanax" -} -item { - name: "134277" - id: 1062 - display_name: "Gastrophryne olivacea" -} -item { - name: "60551" - id: 1063 - display_name: "Papilio glaucus" -} -item { - name: "3731" - id: 1064 - display_name: "Platalea leucorodia" -} -item { - name: "232593" - id: 1065 - display_name: "Thyris sepulchralis" -} -item { - name: "19609" - id: 1066 - display_name: "Phalaenoptilus nuttallii" -} -item { - name: "126106" - id: 1067 - display_name: "Haploa clymene" -} -item { - name: "27805" - id: 1068 - display_name: "Notophthalmus viridescens" -} -item { - name: "199840" - id: 1069 - display_name: "Haemorhous mexicanus" -} -item { - name: "199841" - id: 1070 - display_name: "Haemorhous purpureus" -} -item { - name: "219719" - id: 1071 - display_name: "Eudryas unio" -} -item { - name: "27818" - id: 1072 - display_name: "Taricha torosa" -} -item { - name: "19627" - id: 1073 - display_name: "Nyctidromus albicollis" -} -item { - name: "28750" - id: 1074 - display_name: "Salvadora grahamiae lineata" -} -item { - name: "27824" - id: 1075 - display_name: "Taricha rivularis" -} -item { - name: "146632" - id: 1076 - display_name: "Toxomerus politus" -} -item { - name: "52402" - id: 1077 - display_name: "Cetonia aurata" -} -item { - name: "18291" - id: 1078 - display_name: "Campephilus guatemalensis" -} -item { - name: "60598" - id: 1079 - display_name: "Ixodes scapularis" -} -item { - name: "199870" - id: 1080 - display_name: "Pyralis farinalis" -} -item { - name: "60607" - id: 1081 - display_name: "Limenitis arthemis" -} -item { - name: "205241" - id: 1082 - display_name: "Plagodis phlogosaria" -} -item { - name: "14898" - id: 1083 - display_name: "Toxostoma rufum" -} -item { - name: "126153" - id: 1084 - display_name: "Amphion floridensis" -} -item { - name: "126155" - id: 1085 - display_name: "Vespula germanica" -} -item { - name: "51392" - id: 1086 - display_name: "Morone saxatilis" -} -item { - name: "3280" - id: 1087 - display_name: "Leptotila verreauxi" -} -item { - name: "19670" - id: 1088 - display_name: "Nyctibius jamaicensis" -} -item { - name: "6929" - id: 1089 - display_name: "Anas penelope" -} -item { - name: "97738" - id: 1090 - display_name: "Chromagrion conditum" -} -item { - name: "52449" - id: 1091 - display_name: "Rhinecanthus rectangulus" -} -item { - name: "52451" - id: 1092 - display_name: "Naso lituratus" -} -item { - name: "56529" - id: 1093 - display_name: "Papilio machaon" -} -item { - name: "199913" - id: 1094 - display_name: "Buteo plagiatus" -} -item { - name: "199914" - id: 1095 - display_name: "Selasphorus calliope" -} -item { - name: "85227" - id: 1096 - display_name: "Hemideina crassidens" -} -item { - name: "36076" - id: 1097 - display_name: "Cophosaurus texanus" -} -item { - name: "36077" - id: 1098 - display_name: "Cophosaurus texanus texanus" -} -item { - name: "208112" - id: 1099 - display_name: "Palpita magniferalis" -} -item { - name: "85235" - id: 1100 - display_name: "Deinacrida rugosa" -} -item { - name: "93429" - id: 1101 - display_name: "Aeshna constricta" -} -item { - name: "36086" - id: 1102 - display_name: "Callisaurus draconoides rhodostictus" -} -item { - name: "126204" - id: 1103 - display_name: "Synchlora aerata" -} -item { - name: "93437" - id: 1104 - display_name: "Aeshna mixta" -} -item { - name: "126207" - id: 1105 - display_name: "Schizura unicornis" -} -item { - name: "126209" - id: 1106 - display_name: "Metcalfa pruinosa" -} -item { - name: "126211" - id: 1107 - display_name: "Poecilocapsus lineatus" -} -item { - name: "36100" - id: 1108 - display_name: "Uta stansburiana elegans" -} -item { - name: "48342" - id: 1109 - display_name: "Hemigrapsus nudus" -} -item { - name: "199942" - id: 1110 - display_name: "Strategus aloeus" -} -item { - name: "126215" - id: 1111 - display_name: "Monobia quadridens" -} -item { - name: "101640" - id: 1112 - display_name: "Gomphaeschna furcillata" -} -item { - name: "126217" - id: 1113 - display_name: "Pyrausta orphisalis" -} -item { - name: "36107" - id: 1114 - display_name: "Urosaurus ornatus" -} -item { - name: "51940" - id: 1115 - display_name: "Hemidactylus frenatus" -} -item { - name: "36121" - id: 1116 - display_name: "Urosaurus graciosus" -} -item { - name: "19743" - id: 1117 - display_name: "Megascops kennicottii" -} -item { - name: "68901" - id: 1118 - display_name: "Salticus scenicus" -} -item { - name: "44326" - id: 1119 - display_name: "Microtus californicus" -} -item { - name: "82481" - id: 1120 - display_name: "Pieris marginalis" -} -item { - name: "474332" - id: 1121 - display_name: "Porphyrio poliocephalus" -} -item { - name: "81674" - id: 1122 - display_name: "Rivula propinqualis" -} -item { - name: "126252" - id: 1123 - display_name: "Mastigoproctus giganteus" -} -item { - name: "36142" - id: 1124 - display_name: "Sceloporus undulatus" -} -item { - name: "68911" - id: 1125 - display_name: "Libellula needhami" -} -item { - name: "68912" - id: 1126 - display_name: "Dysdera crocata" -} -item { - name: "42888" - id: 1127 - display_name: "Macropus giganteus" -} -item { - name: "19765" - id: 1128 - display_name: "Megascops asio" -} -item { - name: "68918" - id: 1129 - display_name: "Poecilanthrax lucifer" -} -item { - name: "333705" - id: 1130 - display_name: "Pantherophis obsoletus lindheimeri" -} -item { - name: "126267" - id: 1131 - display_name: "Coleomegilla maculata" -} -item { - name: "101693" - id: 1132 - display_name: "Gomphus vastus" -} -item { - name: "85221" - id: 1133 - display_name: "Hemideina thoracica" -} -item { - name: "126276" - id: 1134 - display_name: "Agrotis ipsilon" -} -item { - name: "85317" - id: 1135 - display_name: "Eurosta solidaginis" -} -item { - name: "36169" - id: 1136 - display_name: "Sceloporus spinosus" -} -item { - name: "60752" - id: 1137 - display_name: "Hermeuptychia sosybius" -} -item { - name: "60754" - id: 1138 - display_name: "Pyromorpha dimidiata" -} -item { - name: "126291" - id: 1139 - display_name: "Prosapia bicincta" -} -item { - name: "52564" - id: 1140 - display_name: "Anthopleura elegantissima" -} -item { - name: "126293" - id: 1141 - display_name: "Prionoxystus robiniae" -} -item { - name: "120719" - id: 1142 - display_name: "Pseudacris hypochondriaca" -} -item { - name: "36189" - id: 1143 - display_name: "Sceloporus poinsettii" -} -item { - name: "52576" - id: 1144 - display_name: "Uroctonus mordax" -} -item { - name: "36198" - id: 1145 - display_name: "Sceloporus orcutti" -} -item { - name: "52584" - id: 1146 - display_name: "Pantala hymenaea" -} -item { - name: "44395" - id: 1147 - display_name: "Peromyscus leucopus" -} -item { - name: "36204" - id: 1148 - display_name: "Sceloporus occidentalis" -} -item { - name: "52589" - id: 1149 - display_name: "Coenonympha pamphilus" -} -item { - name: "3439" - id: 1150 - display_name: "Zenaida auriculata" -} -item { - name: "36208" - id: 1151 - display_name: "Sceloporus occidentalis bocourtii" -} -item { - name: "72936" - id: 1152 - display_name: "Hymenolaimus malacorhynchos" -} -item { - name: "85362" - id: 1153 - display_name: "Sphex ichneumoneus" -} -item { - name: "36217" - id: 1154 - display_name: "Sceloporus merriami" -} -item { - name: "68993" - id: 1155 - display_name: "Liometopum occidentale" -} -item { - name: "199916" - id: 1156 - display_name: "Setophaga caerulescens" -} -item { - name: "52620" - id: 1157 - display_name: "Cicindela oregona" -} -item { - name: "36243" - id: 1158 - display_name: "Sceloporus jarrovii" -} -item { - name: "52628" - id: 1159 - display_name: "Araneus diadematus" -} -item { - name: "180007" - id: 1160 - display_name: "Otospermophilus beecheyi" -} -item { - name: "85408" - id: 1161 - display_name: "Erythemis collocata" -} -item { - name: "36262" - id: 1162 - display_name: "Sceloporus grammicus" -} -item { - name: "60839" - id: 1163 - display_name: "Spilosoma virginica" -} -item { - name: "16968" - id: 1164 - display_name: "Camptostoma imberbe" -} -item { - name: "4715" - id: 1165 - display_name: "Caracara plancus" -} -item { - name: "313246" - id: 1166 - display_name: "Olla v-nigrum" -} -item { - name: "126393" - id: 1167 - display_name: "Stomolophus meleagris" -} -item { - name: "126397" - id: 1168 - display_name: "Halysidota harrisii" -} -item { - name: "64221" - id: 1169 - display_name: "Bipalium kewense" -} -item { - name: "28102" - id: 1170 - display_name: "Virginia striatula" -} -item { - name: "150985" - id: 1171 - display_name: "Planorbella trivolvis" -} -item { - name: "36306" - id: 1172 - display_name: "Phrynosoma modestum" -} -item { - name: "36307" - id: 1173 - display_name: "Phrynosoma orbiculare" -} -item { - name: "199929" - id: 1174 - display_name: "Plagiometriona clavata" -} -item { - name: "3545" - id: 1175 - display_name: "Columbina passerina" -} -item { - name: "36315" - id: 1176 - display_name: "Phrynosoma hernandesi" -} -item { - name: "367556" - id: 1177 - display_name: "Eupsittula nana" -} -item { - name: "371963" - id: 1178 - display_name: "Lampropeltis multifasciata" -} -item { - name: "36339" - id: 1179 - display_name: "Holbrookia propinqua" -} -item { - name: "36094" - id: 1180 - display_name: "Uta stansburiana" -} -item { - name: "36343" - id: 1181 - display_name: "Holbrookia maculata" -} -item { - name: "52766" - id: 1182 - display_name: "Megaphasma denticrus" -} -item { - name: "18941" - id: 1183 - display_name: "Nestor notabilis" -} -item { - name: "3580" - id: 1184 - display_name: "Columbina talpacoti" -} -item { - name: "123690" - id: 1185 - display_name: "Caranx melampygus" -} -item { - name: "52482" - id: 1186 - display_name: "Episyrphus balteatus" -} -item { - name: "28762" - id: 1187 - display_name: "Rhinocheilus lecontei" -} -item { - name: "3607" - id: 1188 - display_name: "Geopelia striata" -} -item { - name: "52484" - id: 1189 - display_name: "Celastrina echo" -} -item { - name: "61293" - id: 1190 - display_name: "Thaumetopoea pityocampa" -} -item { - name: "19998" - id: 1191 - display_name: "Athene noctua" -} -item { - name: "44575" - id: 1192 - display_name: "Rattus rattus" -} -item { - name: "44576" - id: 1193 - display_name: "Rattus norvegicus" -} -item { - name: "133250" - id: 1194 - display_name: "Tettigonia viridissima" -} -item { - name: "52774" - id: 1195 - display_name: "Bombus fervidus" -} -item { - name: "49756" - id: 1196 - display_name: "Nephila clavipes" -} -item { - name: "52779" - id: 1197 - display_name: "Bombus bimaculatus" -} -item { - name: "52782" - id: 1198 - display_name: "Melissodes bimaculata" -} -item { - name: "126513" - id: 1199 - display_name: "Larinioides cornutus" -} -item { - name: "69170" - id: 1200 - display_name: "Hemigrapsus oregonensis" -} -item { - name: "1971" - id: 1201 - display_name: "Crotophaga ani" -} -item { - name: "12942" - id: 1202 - display_name: "Sialia sialis" -} -item { - name: "126532" - id: 1203 - display_name: "Toxomerus geminatus" -} -item { - name: "216649" - id: 1204 - display_name: "Chauliognathus pensylvanicus" -} -item { - name: "3734" - id: 1205 - display_name: "Platalea alba" -} -item { - name: "216651" - id: 1206 - display_name: "Chelinidea vittiger" -} -item { - name: "20044" - id: 1207 - display_name: "Bubo virginianus" -} -item { - name: "11855" - id: 1208 - display_name: "Petrochelidon fulva" -} -item { - name: "28246" - id: 1209 - display_name: "Arizona elegans" -} -item { - name: "224855" - id: 1210 - display_name: "Melipotis indomita" -} -item { - name: "11867" - id: 1211 - display_name: "Progne subis" -} -item { - name: "126562" - id: 1212 - display_name: "Setophaga coronata auduboni" -} -item { - name: "126568" - id: 1213 - display_name: "Manduca rustica" -} -item { - name: "11882" - id: 1214 - display_name: "Hirundo neoxena" -} -item { - name: "11901" - id: 1215 - display_name: "Hirundo rustica" -} -item { - name: "52865" - id: 1216 - display_name: "Tramea lacerata" -} -item { - name: "142978" - id: 1217 - display_name: "Simyra insularis" -} -item { - name: "123499" - id: 1218 - display_name: "Notophthalmus viridescens viridescens" -} -item { - name: "339592" - id: 1219 - display_name: "Calidris virgata" -} -item { - name: "339593" - id: 1220 - display_name: "Calidris pugnax" -} -item { - name: "44311" - id: 1221 - display_name: "Microtus pennsylvanicus" -} -item { - name: "142988" - id: 1222 - display_name: "Lerema accius" -} -item { - name: "142990" - id: 1223 - display_name: "Autographa precationis" -} -item { - name: "142995" - id: 1224 - display_name: "Hymenia perspectalis" -} -item { - name: "129423" - id: 1225 - display_name: "Zelus luridus" -} -item { - name: "3733" - id: 1226 - display_name: "Platalea regia" -} -item { - name: "470678" - id: 1227 - display_name: "Cerithideopsis californica" -} -item { - name: "146713" - id: 1228 - display_name: "Elaphria grata" -} -item { - name: "143002" - id: 1229 - display_name: "Orthonama obstipata" -} -item { - name: "11931" - id: 1230 - display_name: "Tachycineta thalassina" -} -item { - name: "143005" - id: 1231 - display_name: "Costaconvexa centrostrigaria" -} -item { - name: "3743" - id: 1232 - display_name: "Bostrychia hagedash" -} -item { - name: "143009" - id: 1233 - display_name: "Ectropis crepuscularia" -} -item { - name: "36514" - id: 1234 - display_name: "Anolis carolinensis" -} -item { - name: "143012" - id: 1235 - display_name: "Zanclognatha pedipilalis" -} -item { - name: "11941" - id: 1236 - display_name: "Riparia riparia" -} -item { - name: "52902" - id: 1237 - display_name: "Palthis asopialis" -} -item { - name: "3751" - id: 1238 - display_name: "Eudocimus albus" -} -item { - name: "52906" - id: 1239 - display_name: "Chytonix palliatricula" -} -item { - name: "3756" - id: 1240 - display_name: "Plegadis falcinellus" -} -item { - name: "3759" - id: 1241 - display_name: "Plegadis chihi" -} -item { - name: "143024" - id: 1242 - display_name: "Eusarca confusaria" -} -item { - name: "62067" - id: 1243 - display_name: "Orthetrum cancellatum" -} -item { - name: "28340" - id: 1244 - display_name: "Thamnophis sauritus" -} -item { - name: "28345" - id: 1245 - display_name: "Thamnophis cyrtopsis" -} -item { - name: "143034" - id: 1246 - display_name: "Hippodamia variegata" -} -item { - name: "28347" - id: 1247 - display_name: "Thamnophis cyrtopsis ocellatus" -} -item { - name: "52925" - id: 1248 - display_name: "Phyciodes tharos" -} -item { - name: "8010" - id: 1249 - display_name: "Corvus corax" -} -item { - name: "11970" - id: 1250 - display_name: "Stelgidopteryx serripennis" -} -item { - name: "28362" - id: 1251 - display_name: "Thamnophis sirtalis" -} -item { - name: "3788" - id: 1252 - display_name: "Sula dactylatra" -} -item { - name: "44749" - id: 1253 - display_name: "Neotoma fuscipes" -} -item { - name: "52943" - id: 1254 - display_name: "Trichodezia albovittata" -} -item { - name: "3793" - id: 1255 - display_name: "Sula sula" -} -item { - name: "101667" - id: 1256 - display_name: "Gomphus exilis" -} -item { - name: "3797" - id: 1257 - display_name: "Sula leucogaster" -} -item { - name: "118486" - id: 1258 - display_name: "Macaria aemulataria" -} -item { - name: "3801" - id: 1259 - display_name: "Morus serrator" -} -item { - name: "28378" - id: 1260 - display_name: "Thamnophis radix" -} -item { - name: "118492" - id: 1261 - display_name: "Helicoverpa zea" -} -item { - name: "148793" - id: 1262 - display_name: "Asterocampa leilia" -} -item { - name: "28384" - id: 1263 - display_name: "Thamnophis proximus rubrilineatus" -} -item { - name: "257761" - id: 1264 - display_name: "Phocides polybius" -} -item { - name: "28387" - id: 1265 - display_name: "Thamnophis proximus orarius" -} -item { - name: "28390" - id: 1266 - display_name: "Thamnophis marcianus" -} -item { - name: "118503" - id: 1267 - display_name: "Darapsa myron" -} -item { - name: "3817" - id: 1268 - display_name: "Eudyptula minor" -} -item { - name: "36135" - id: 1269 - display_name: "Uma scoparia" -} -item { - name: "28396" - id: 1270 - display_name: "Thamnophis hammondii" -} -item { - name: "28400" - id: 1271 - display_name: "Thamnophis elegans elegans" -} -item { - name: "118513" - id: 1272 - display_name: "Hypena scabra" -} -item { - name: "28403" - id: 1273 - display_name: "Thamnophis elegans vagrans" -} -item { - name: "201342" - id: 1274 - display_name: "Chalcoela iphitalis" -} -item { - name: "3831" - id: 1275 - display_name: "Megadyptes antipodes" -} -item { - name: "126712" - id: 1276 - display_name: "Corydalus cornutus" -} -item { - name: "30676" - id: 1277 - display_name: "Agkistrodon piscivorus leucostoma" -} -item { - name: "3834" - id: 1278 - display_name: "Scopus umbretta" -} -item { - name: "213631" - id: 1279 - display_name: "Anicla infecta" -} -item { - name: "143105" - id: 1280 - display_name: "Pleuroprucha insulsaria" -} -item { - name: "28418" - id: 1281 - display_name: "Thamnophis atratus" -} -item { - name: "118531" - id: 1282 - display_name: "Parallelia bistriaris" -} -item { - name: "145363" - id: 1283 - display_name: "Troglodytes troglodytes" -} -item { - name: "3845" - id: 1284 - display_name: "Calidris canutus" -} -item { - name: "12038" - id: 1285 - display_name: "Lanius collurio" -} -item { - name: "143114" - id: 1286 - display_name: "Phragmatobia fuliginosa" -} -item { - name: "3851" - id: 1287 - display_name: "Calidris bairdii" -} -item { - name: "324226" - id: 1288 - display_name: "Meleagris gallopavo intermedia" -} -item { - name: "143118" - id: 1289 - display_name: "Pseudeustrotia carneola" -} -item { - name: "3855" - id: 1290 - display_name: "Calidris mauri" -} -item { - name: "3856" - id: 1291 - display_name: "Calidris maritima" -} -item { - name: "3857" - id: 1292 - display_name: "Calidris alpina" -} -item { - name: "143124" - id: 1293 - display_name: "Parapediasia teterrella" -} -item { - name: "143125" - id: 1294 - display_name: "Hypena madefactalis" -} -item { - name: "3863" - id: 1295 - display_name: "Calidris ferruginea" -} -item { - name: "118552" - id: 1296 - display_name: "Felis catus" -} -item { - name: "3865" - id: 1297 - display_name: "Calidris melanotos" -} -item { - name: "3869" - id: 1298 - display_name: "Limnodromus griseus" -} -item { - name: "118558" - id: 1299 - display_name: "Manduca quinquemaculata" -} -item { - name: "118559" - id: 1300 - display_name: "Tetraopes tetrophthalmus" -} -item { - name: "12065" - id: 1301 - display_name: "Malurus cyaneus" -} -item { - name: "3878" - id: 1302 - display_name: "Tringa nebularia" -} -item { - name: "101681" - id: 1303 - display_name: "Gomphus militaris" -} -item { - name: "413483" - id: 1304 - display_name: "Todiramphus sanctus vagans" -} -item { - name: "3885" - id: 1305 - display_name: "Tringa ochropus" -} -item { - name: "3888" - id: 1306 - display_name: "Tringa glareola" -} -item { - name: "126770" - id: 1307 - display_name: "Vulpes vulpes fulvus" -} -item { - name: "3892" - id: 1308 - display_name: "Tringa melanoleuca" -} -item { - name: "3893" - id: 1309 - display_name: "Tringa flavipes" -} -item { - name: "126775" - id: 1310 - display_name: "Cervus elaphus nelsoni" -} -item { - name: "3896" - id: 1311 - display_name: "Numenius arquata" -} -item { - name: "126777" - id: 1312 - display_name: "Peucetia viridans" -} -item { - name: "3901" - id: 1313 - display_name: "Numenius phaeopus" -} -item { - name: "32058" - id: 1314 - display_name: "Elgaria multicarinata webbii" -} -item { - name: "413506" - id: 1315 - display_name: "Phalacrocorax carbo novaehollandiae" -} -item { - name: "413508" - id: 1316 - display_name: "Petroica macrocephala macrocephala" -} -item { - name: "413512" - id: 1317 - display_name: "Petroica australis longipes" -} -item { - name: "61258" - id: 1318 - display_name: "Junonia evarete" -} -item { - name: "28493" - id: 1319 - display_name: "Tantilla nigriceps" -} -item { - name: "413522" - id: 1320 - display_name: "Prosthemadera novaeseelandiae novaeseelandiae" -} -item { - name: "58506" - id: 1321 - display_name: "Polites themistocles" -} -item { - name: "28505" - id: 1322 - display_name: "Tantilla gracilis" -} -item { - name: "20315" - id: 1323 - display_name: "Asio flammeus" -} -item { - name: "143196" - id: 1324 - display_name: "Schinia arcigera" -} -item { - name: "413533" - id: 1325 - display_name: "Rhipidura fuliginosa fuliginosa" -} -item { - name: "3936" - id: 1326 - display_name: "Scolopax minor" -} -item { - name: "3938" - id: 1327 - display_name: "Arenaria interpres" -} -item { - name: "3941" - id: 1328 - display_name: "Arenaria melanocephala" -} -item { - name: "413543" - id: 1329 - display_name: "Rhipidura fuliginosa placabilis" -} -item { - name: "3947" - id: 1330 - display_name: "Limosa limosa" -} -item { - name: "3950" - id: 1331 - display_name: "Limosa haemastica" -} -item { - name: "126269" - id: 1332 - display_name: "Austrolestes colensonis" -} -item { - name: "3954" - id: 1333 - display_name: "Limosa fedoa" -} -item { - name: "199998" - id: 1334 - display_name: "Pedicia albivitta" -} -item { - name: "3959" - id: 1335 - display_name: "Phalaropus lobatus" -} -item { - name: "3962" - id: 1336 - display_name: "Bartramia longicauda" -} -item { - name: "199999" - id: 1337 - display_name: "Callopistria mollissima" -} -item { - name: "104426" - id: 1338 - display_name: "Lestes disjunctus" -} -item { - name: "126848" - id: 1339 - display_name: "Delphinia picta" -} -item { - name: "3951" - id: 1340 - display_name: "Limosa lapponica" -} -item { - name: "20356" - id: 1341 - display_name: "Aegolius acadicus" -} -item { - name: "121792" - id: 1342 - display_name: "Polistes carolina" -} -item { - name: "3978" - id: 1343 - display_name: "Actitis hypoleucos" -} -item { - name: "53911" - id: 1344 - display_name: "Cyprinus carpio" -} -item { - name: "135055" - id: 1345 - display_name: "Bufotes balearicus" -} -item { - name: "19121" - id: 1346 - display_name: "Trichoglossus haematodus" -} -item { - name: "28562" - id: 1347 - display_name: "Storeria dekayi" -} -item { - name: "28563" - id: 1348 - display_name: "Storeria dekayi texana" -} -item { - name: "20372" - id: 1349 - display_name: "Surnia ulula" -} -item { - name: "135064" - id: 1350 - display_name: "Bufotes viridis" -} -item { - name: "28570" - id: 1351 - display_name: "Storeria dekayi dekayi" -} -item { - name: "61341" - id: 1352 - display_name: "Narceus americanus" -} -item { - name: "7493" - id: 1353 - display_name: "Polioptila caerulea" -} -item { - name: "29339" - id: 1354 - display_name: "Natrix natrix" -} -item { - name: "9135" - id: 1355 - display_name: "Spizella passerina" -} -item { - name: "126889" - id: 1356 - display_name: "Toxomerus marginatus" -} -item { - name: "143274" - id: 1357 - display_name: "Gluphisia septentrionis" -} -item { - name: "343021" - id: 1358 - display_name: "Anguis fragilis" -} -item { - name: "14591" - id: 1359 - display_name: "Pycnonotus jocosus" -} -item { - name: "10227" - id: 1360 - display_name: "Passerina cyanea" -} -item { - name: "10228" - id: 1361 - display_name: "Passerina versicolor" -} -item { - name: "61371" - id: 1362 - display_name: "Panulirus interruptus" -} -item { - name: "143294" - id: 1363 - display_name: "Colias croceus" -} -item { - name: "135104" - id: 1364 - display_name: "Ichthyosaura alpestris" -} -item { - name: "83958" - id: 1365 - display_name: "Phryganidia californica" -} -item { - name: "143302" - id: 1366 - display_name: "Megapallifera mutabilis" -} -item { - name: "12231" - id: 1367 - display_name: "Manorina melanocephala" -} -item { - name: "200661" - id: 1368 - display_name: "Coluber constrictor mormon" -} -item { - name: "3681" - id: 1369 - display_name: "Ocyphaps lophotes" -} -item { - name: "4773" - id: 1370 - display_name: "Jabiru mycteria" -} -item { - name: "135140" - id: 1371 - display_name: "Taricha sierrae" -} -item { - name: "28649" - id: 1372 - display_name: "Sonora semiannulata" -} -item { - name: "53226" - id: 1373 - display_name: "Boisea rubrolineata" -} -item { - name: "53227" - id: 1374 - display_name: "Boisea trivittata" -} -item { - name: "14593" - id: 1375 - display_name: "Pycnonotus cafer" -} -item { - name: "61428" - id: 1376 - display_name: "Arion subfuscus" -} -item { - name: "333822" - id: 1377 - display_name: "Anser cygnoides domesticus" -} -item { - name: "41641" - id: 1378 - display_name: "Ursus arctos" -} -item { - name: "56602" - id: 1379 - display_name: "Plebejus lupini" -} -item { - name: "55295" - id: 1380 - display_name: "Grapsus grapsus" -} -item { - name: "36181" - id: 1381 - display_name: "Sceloporus cyanogenys" -} -item { - name: "41708" - id: 1382 - display_name: "Phoca vitulina" -} -item { - name: "118788" - id: 1383 - display_name: "Desmia funeralis" -} -item { - name: "61445" - id: 1384 - display_name: "Acanthocephala terminalis" -} -item { - name: "30721" - id: 1385 - display_name: "Crotalus triseriatus" -} -item { - name: "180010" - id: 1386 - display_name: "Callospermophilus lateralis" -} -item { - name: "53875" - id: 1387 - display_name: "Ocypode quadrata" -} -item { - name: "18358" - id: 1388 - display_name: "Picus viridis" -} -item { - name: "143390" - id: 1389 - display_name: "Oxidus gracilis" -} -item { - name: "55785" - id: 1390 - display_name: "Ochlodes agricola" -} -item { - name: "4141" - id: 1391 - display_name: "Phoebastria nigripes" -} -item { - name: "20526" - id: 1392 - display_name: "Struthio camelus" -} -item { - name: "32093" - id: 1393 - display_name: "Boa constrictor" -} -item { - name: "4144" - id: 1394 - display_name: "Phoebastria immutabilis" -} -item { - name: "74442" - id: 1395 - display_name: "Hydrochoerus hydrochaeris" -} -item { - name: "61492" - id: 1396 - display_name: "Chrysopilus thoracicus" -} -item { - name: "61495" - id: 1397 - display_name: "Erythemis simplicicollis" -} -item { - name: "389177" - id: 1398 - display_name: "Eriophora pustulosa" -} -item { - name: "61503" - id: 1399 - display_name: "Ascalapha odorata" -} -item { - name: "118855" - id: 1400 - display_name: "Calosoma scrutator" -} -item { - name: "61513" - id: 1401 - display_name: "Adelges tsugae" -} -item { - name: "28749" - id: 1402 - display_name: "Salvadora grahamiae" -} -item { - name: "143440" - id: 1403 - display_name: "Ceratomia catalpae" -} -item { - name: "61523" - id: 1404 - display_name: "Helix pomatia" -} -item { - name: "4180" - id: 1405 - display_name: "Fulmarus glacialis" -} -item { - name: "143445" - id: 1406 - display_name: "Pachysphinx modesta" -} -item { - name: "233560" - id: 1407 - display_name: "Vespula squamosa" -} -item { - name: "126308" - id: 1408 - display_name: "Marpesia chiron" -} -item { - name: "61536" - id: 1409 - display_name: "Calopteryx virgo" -} -item { - name: "685" - id: 1410 - display_name: "Francolinus pondicerianus" -} -item { - name: "60774" - id: 1411 - display_name: "Psychomorpha epimenis" -} -item { - name: "135271" - id: 1412 - display_name: "Amphibolips confluenta" -} -item { - name: "69736" - id: 1413 - display_name: "Schistocerca americana" -} -item { - name: "69737" - id: 1414 - display_name: "Xylophanes tersa" -} -item { - name: "6141" - id: 1415 - display_name: "Cynanthus latirostris" -} -item { - name: "4205" - id: 1416 - display_name: "Podiceps nigricollis" -} -item { - name: "69743" - id: 1417 - display_name: "Wallengrenia otho" -} -item { - name: "4208" - id: 1418 - display_name: "Podiceps cristatus" -} -item { - name: "4209" - id: 1419 - display_name: "Podiceps auritus" -} -item { - name: "118901" - id: 1420 - display_name: "Hyles gallii" -} -item { - name: "17871" - id: 1421 - display_name: "Dendrocopos major" -} -item { - name: "143484" - id: 1422 - display_name: "Blepharomastix ranalis" -} -item { - name: "4224" - id: 1423 - display_name: "Podiceps grisegena" -} -item { - name: "200834" - id: 1424 - display_name: "Sphenodon punctatus" -} -item { - name: "179995" - id: 1425 - display_name: "Urocitellus beldingi" -} -item { - name: "322024" - id: 1426 - display_name: "Apatura ilia" -} -item { - name: "44396" - id: 1427 - display_name: "Peromyscus maniculatus" -} -item { - name: "4237" - id: 1428 - display_name: "Tachybaptus ruficollis" -} -item { - name: "118930" - id: 1429 - display_name: "Spodoptera ornithogalli" -} -item { - name: "118936" - id: 1430 - display_name: "Euplagia quadripunctaria" -} -item { - name: "4804" - id: 1431 - display_name: "Charadrius montanus" -} -item { - name: "127133" - id: 1432 - display_name: "Hyphantria cunea" -} -item { - name: "143518" - id: 1433 - display_name: "Prochoerodes lineola" -} -item { - name: "52592" - id: 1434 - display_name: "Pararge aegeria" -} -item { - name: "36149" - id: 1435 - display_name: "Sceloporus torquatus" -} -item { - name: "118951" - id: 1436 - display_name: "Pterophylla camellifolia" -} -item { - name: "4265" - id: 1437 - display_name: "Phalacrocorax auritus" -} -item { - name: "4270" - id: 1438 - display_name: "Phalacrocorax carbo" -} -item { - name: "446640" - id: 1439 - display_name: "Neomonachus schauinslandi" -} -item { - name: "118961" - id: 1440 - display_name: "Conocephalus brevipennis" -} -item { - name: "28850" - id: 1441 - display_name: "Regina septemvittata" -} -item { - name: "4277" - id: 1442 - display_name: "Phalacrocorax penicillatus" -} -item { - name: "4234" - id: 1443 - display_name: "Aechmophorus clarkii" -} -item { - name: "118967" - id: 1444 - display_name: "Psyllobora vigintimaculata" -} -item { - name: "118968" - id: 1445 - display_name: "Allograpta obliqua" -} -item { - name: "118970" - id: 1446 - display_name: "Bombus impatiens" -} -item { - name: "123594" - id: 1447 - display_name: "Anaxyrus americanus americanus" -} -item { - name: "69838" - id: 1448 - display_name: "Cyanea capillata" -} -item { - name: "69844" - id: 1449 - display_name: "Anthocharis midea" -} -item { - name: "48505" - id: 1450 - display_name: "Junonia coenia" -} -item { - name: "151769" - id: 1451 - display_name: "Diaphania hyalinata" -} -item { - name: "151770" - id: 1452 - display_name: "Peridea angulosa" -} -item { - name: "53467" - id: 1453 - display_name: "Leucauge venusta" -} -item { - name: "119013" - id: 1454 - display_name: "Ctenucha virginica" -} -item { - name: "4327" - id: 1455 - display_name: "Pelecanus onocrotalus" -} -item { - name: "143592" - id: 1456 - display_name: "Spragueia leo" -} -item { - name: "200938" - id: 1457 - display_name: "Diaethria anna" -} -item { - name: "4334" - id: 1458 - display_name: "Pelecanus erythrorhynchos" -} -item { - name: "151794" - id: 1459 - display_name: "Atta texana" -} -item { - name: "3454" - id: 1460 - display_name: "Zenaida macroura" -} -item { - name: "4872" - id: 1461 - display_name: "Vanellus miles" -} -item { - name: "4345" - id: 1462 - display_name: "Larus occidentalis" -} -item { - name: "143610" - id: 1463 - display_name: "Besma quercivoraria" -} -item { - name: "20733" - id: 1464 - display_name: "Trogon massena" -} -item { - name: "143615" - id: 1465 - display_name: "Udea rubigalis" -} -item { - name: "4352" - id: 1466 - display_name: "Larus thayeri" -} -item { - name: "4353" - id: 1467 - display_name: "Larus heermanni" -} -item { - name: "4354" - id: 1468 - display_name: "Larus livens" -} -item { - name: "4356" - id: 1469 - display_name: "Larus canus" -} -item { - name: "220826" - id: 1470 - display_name: "Habrosyne scripta" -} -item { - name: "4361" - id: 1471 - display_name: "Larus glaucoides" -} -item { - name: "4364" - id: 1472 - display_name: "Larus delawarensis" -} -item { - name: "102672" - id: 1473 - display_name: "Hetaerina titia" -} -item { - name: "20754" - id: 1474 - display_name: "Trogon collaris" -} -item { - name: "479512" - id: 1475 - display_name: "Acronicta fallax" -} -item { - name: "3460" - id: 1476 - display_name: "Zenaida asiatica" -} -item { - name: "119066" - id: 1477 - display_name: "Idia lubricalis" -} -item { - name: "119068" - id: 1478 - display_name: "Apodemia virgulti" -} -item { - name: "4381" - id: 1479 - display_name: "Larus fuscus" -} -item { - name: "4385" - id: 1480 - display_name: "Larus californicus" -} -item { - name: "69922" - id: 1481 - display_name: "Oncorhynchus nerka" -} -item { - name: "12580" - id: 1482 - display_name: "Prosthemadera novaeseelandiae" -} -item { - name: "69925" - id: 1483 - display_name: "Clinocardium nuttallii" -} -item { - name: "20781" - id: 1484 - display_name: "Trogon elegans" -} -item { - name: "4399" - id: 1485 - display_name: "Larus glaucescens" -} -item { - name: "94513" - id: 1486 - display_name: "Archilestes grandis" -} -item { - name: "119090" - id: 1487 - display_name: "Eremnophila aureonotata" -} -item { - name: "20787" - id: 1488 - display_name: "Trogon citreolus" -} -item { - name: "69940" - id: 1489 - display_name: "Hemiargus ceraunus" -} -item { - name: "61749" - id: 1490 - display_name: "Lucanus cervus" -} -item { - name: "4415" - id: 1491 - display_name: "Cepphus columba" -} -item { - name: "4832" - id: 1492 - display_name: "Himantopus leucocephalus" -} -item { - name: "4418" - id: 1493 - display_name: "Cepphus grylle" -} -item { - name: "12612" - id: 1494 - display_name: "Anthornis melanura" -} -item { - name: "125627" - id: 1495 - display_name: "Ellychnia corrusca" -} -item { - name: "201031" - id: 1496 - display_name: "Leptoptilos crumenifer" -} -item { - name: "201032" - id: 1497 - display_name: "Threskiornis moluccus" -} -item { - name: "60812" - id: 1498 - display_name: "Lucanus capreolus" -} -item { - name: "10295" - id: 1499 - display_name: "Thraupis episcopus" -} -item { - name: "209233" - id: 1500 - display_name: "Equus caballus" -} -item { - name: "119122" - id: 1501 - display_name: "Araneus trifolium" -} -item { - name: "201043" - id: 1502 - display_name: "Geranoaetus albicaudatus" -} -item { - name: "61781" - id: 1503 - display_name: "Ochlodes sylvanus" -} -item { - name: "49133" - id: 1504 - display_name: "Vanessa atalanta" -} -item { - name: "94556" - id: 1505 - display_name: "Argia lugens" -} -item { - name: "94557" - id: 1506 - display_name: "Argia moesta" -} -item { - name: "61524" - id: 1507 - display_name: "Forficula auricularia" -} -item { - name: "4449" - id: 1508 - display_name: "Sterna paradisaea" -} -item { - name: "4450" - id: 1509 - display_name: "Sterna hirundo" -} -item { - name: "348515" - id: 1510 - display_name: "Nyctemera annulata" -} -item { - name: "110625" - id: 1511 - display_name: "Progomphus obscurus" -} -item { - name: "94566" - id: 1512 - display_name: "Argia plana" -} -item { - name: "4457" - id: 1513 - display_name: "Sterna forsteri" -} -item { - name: "94571" - id: 1514 - display_name: "Argia sedula" -} -item { - name: "61804" - id: 1515 - display_name: "Olivella biplicata" -} -item { - name: "204532" - id: 1516 - display_name: "Lanius excubitor" -} -item { - name: "29038" - id: 1517 - display_name: "Pituophis deppei" -} -item { - name: "143728" - id: 1518 - display_name: "Choristoneura rosaceana" -} -item { - name: "94577" - id: 1519 - display_name: "Argia translata" -} -item { - name: "130451" - id: 1520 - display_name: "Dione juno" -} -item { - name: "29044" - id: 1521 - display_name: "Pituophis catenifer" -} -item { - name: "70005" - id: 1522 - display_name: "Ilyanassa obsoleta" -} -item { - name: "143734" - id: 1523 - display_name: "Eupithecia miserulata" -} -item { - name: "20856" - id: 1524 - display_name: "Pharomachrus mocinno" -} -item { - name: "29049" - id: 1525 - display_name: "Pituophis catenifer deserticola" -} -item { - name: "29052" - id: 1526 - display_name: "Pituophis catenifer affinis" -} -item { - name: "29053" - id: 1527 - display_name: "Pituophis catenifer annectens" -} -item { - name: "4478" - id: 1528 - display_name: "Sterna striata" -} -item { - name: "407459" - id: 1529 - display_name: "Dolomedes minor" -} -item { - name: "4489" - id: 1530 - display_name: "Stercorarius parasiticus" -} -item { - name: "4491" - id: 1531 - display_name: "Stercorarius pomarinus" -} -item { - name: "6969" - id: 1532 - display_name: "Anas gracilis" -} -item { - name: "4494" - id: 1533 - display_name: "Rissa tridactyla" -} -item { - name: "4496" - id: 1534 - display_name: "Rynchops niger" -} -item { - name: "4501" - id: 1535 - display_name: "Alca torda" -} -item { - name: "4504" - id: 1536 - display_name: "Fratercula arctica" -} -item { - name: "4509" - id: 1537 - display_name: "Fratercula cirrhata" -} -item { - name: "26693" - id: 1538 - display_name: "Scaphiopus hurterii" -} -item { - name: "94624" - id: 1539 - display_name: "Arigomphus submedianus" -} -item { - name: "94625" - id: 1540 - display_name: "Arigomphus villosipes" -} -item { - name: "120720" - id: 1541 - display_name: "Pseudacris sierra" -} -item { - name: "70057" - id: 1542 - display_name: "Agrilus planipennis" -} -item { - name: "127402" - id: 1543 - display_name: "Grammia virgo" -} -item { - name: "51271" - id: 1544 - display_name: "Trachemys scripta elegans" -} -item { - name: "12716" - id: 1545 - display_name: "Turdus merula" -} -item { - name: "12718" - id: 1546 - display_name: "Turdus plumbeus" -} -item { - name: "12720" - id: 1547 - display_name: "Turdus grayi" -} -item { - name: "63697" - id: 1548 - display_name: "Metacarcinus magister" -} -item { - name: "12727" - id: 1549 - display_name: "Turdus migratorius" -} -item { - name: "26698" - id: 1550 - display_name: "Spea multiplicata" -} -item { - name: "12735" - id: 1551 - display_name: "Turdus viscivorus" -} -item { - name: "26699" - id: 1552 - display_name: "Spea bombifrons" -} -item { - name: "127431" - id: 1553 - display_name: "Emmelina monodactyla" -} -item { - name: "4553" - id: 1554 - display_name: "Cerorhinca monocerata" -} -item { - name: "12748" - id: 1555 - display_name: "Turdus philomelos" -} -item { - name: "233933" - id: 1556 - display_name: "Zale horrida" -} -item { - name: "1468" - id: 1557 - display_name: "Galbula ruficauda" -} -item { - name: "111055" - id: 1558 - display_name: "Pseudoleon superbus" -} -item { - name: "61908" - id: 1559 - display_name: "Orgyia vetusta" -} -item { - name: "43086" - id: 1560 - display_name: "Procavia capensis" -} -item { - name: "143830" - id: 1561 - display_name: "Eumorpha vitis" -} -item { - name: "67663" - id: 1562 - display_name: "Leptysma marginicollis" -} -item { - name: "127457" - id: 1563 - display_name: "Idia americalis" -} -item { - name: "4578" - id: 1564 - display_name: "Jacana spinosa" -} -item { - name: "127460" - id: 1565 - display_name: "Idia aemula" -} -item { - name: "201192" - id: 1566 - display_name: "Saxicola rubicola" -} -item { - name: "20969" - id: 1567 - display_name: "Upupa epops" -} -item { - name: "94699" - id: 1568 - display_name: "Aspidoscelis marmorata" -} -item { - name: "10322" - id: 1569 - display_name: "Euphagus carolinus" -} -item { - name: "53743" - id: 1570 - display_name: "Uca pugilator" -} -item { - name: "61256" - id: 1571 - display_name: "Leptoglossus phyllopus" -} -item { - name: "29438" - id: 1572 - display_name: "Coluber flagellum piceus" -} -item { - name: "53750" - id: 1573 - display_name: "Lottia gigantea" -} -item { - name: "143865" - id: 1574 - display_name: "Odocoileus hemionus hemionus" -} -item { - name: "143867" - id: 1575 - display_name: "Protoboarmia porcelaria" -} -item { - name: "209405" - id: 1576 - display_name: "Cenopis reticulatana" -} -item { - name: "49920" - id: 1577 - display_name: "Nymphalis californica" -} -item { - name: "53762" - id: 1578 - display_name: "Scolopendra polymorpha" -} -item { - name: "127492" - id: 1579 - display_name: "Megalographa biloba" -} -item { - name: "62470" - id: 1580 - display_name: "Limax maximus" -} -item { - name: "4621" - id: 1581 - display_name: "Gavia pacifica" -} -item { - name: "14884" - id: 1582 - display_name: "Mimus gilvus" -} -item { - name: "29200" - id: 1583 - display_name: "Opheodrys aestivus" -} -item { - name: "201233" - id: 1584 - display_name: "Passer italiae" -} -item { - name: "4626" - id: 1585 - display_name: "Gavia immer" -} -item { - name: "4627" - id: 1586 - display_name: "Gavia stellata" -} -item { - name: "12822" - id: 1587 - display_name: "Oenanthe oenanthe" -} -item { - name: "4631" - id: 1588 - display_name: "Fregata magnificens" -} -item { - name: "4636" - id: 1589 - display_name: "Fregata minor" -} -item { - name: "70174" - id: 1590 - display_name: "Hypolimnas bolina" -} -item { - name: "4643" - id: 1591 - display_name: "Falco subbuteo" -} -item { - name: "4644" - id: 1592 - display_name: "Falco mexicanus" -} -item { - name: "4645" - id: 1593 - display_name: "Falco femoralis" -} -item { - name: "4647" - id: 1594 - display_name: "Falco peregrinus" -} -item { - name: "119340" - id: 1595 - display_name: "Amphipyra pyramidoides" -} -item { - name: "61997" - id: 1596 - display_name: "Steatoda grossa" -} -item { - name: "70191" - id: 1597 - display_name: "Ischnura ramburii" -} -item { - name: "53809" - id: 1598 - display_name: "Phidippus audax" -} -item { - name: "143213" - id: 1599 - display_name: "Frontinella communis" -} -item { - name: "4664" - id: 1600 - display_name: "Falco rufigularis" -} -item { - name: "4665" - id: 1601 - display_name: "Falco sparverius" -} -item { - name: "19893" - id: 1602 - display_name: "Strix varia" -} -item { - name: "4672" - id: 1603 - display_name: "Falco columbarius" -} -item { - name: "201281" - id: 1604 - display_name: "Phyllodesma americana" -} -item { - name: "201282" - id: 1605 - display_name: "Gallinula chloropus" -} -item { - name: "152131" - id: 1606 - display_name: "Bagrada hilaris" -} -item { - name: "145276" - id: 1607 - display_name: "Cardellina pusilla" -} -item { - name: "12878" - id: 1608 - display_name: "Catharus ustulatus" -} -item { - name: "4690" - id: 1609 - display_name: "Falco novaeseelandiae" -} -item { - name: "53843" - id: 1610 - display_name: "Brephidium exilis" -} -item { - name: "36281" - id: 1611 - display_name: "Sceloporus clarkii" -} -item { - name: "12890" - id: 1612 - display_name: "Catharus guttatus" -} -item { - name: "62045" - id: 1613 - display_name: "Lygaeus kalmii" -} -item { - name: "47075" - id: 1614 - display_name: "Dasypus novemcinctus" -} -item { - name: "12901" - id: 1615 - display_name: "Catharus fuscescens" -} -item { - name: "4714" - id: 1616 - display_name: "Caracara cheriway" -} -item { - name: "53867" - id: 1617 - display_name: "Erythemis plebeja" -} -item { - name: "62060" - id: 1618 - display_name: "Palomena prasina" -} -item { - name: "53869" - id: 1619 - display_name: "Ocypus olens" -} -item { - name: "4719" - id: 1620 - display_name: "Herpetotheres cachinnans" -} -item { - name: "116840" - id: 1621 - display_name: "Calcarius lapponicus" -} -item { - name: "4726" - id: 1622 - display_name: "Milvago chimachima" -} -item { - name: "29304" - id: 1623 - display_name: "Nerodia taxispilota" -} -item { - name: "29305" - id: 1624 - display_name: "Nerodia sipedon" -} -item { - name: "29306" - id: 1625 - display_name: "Nerodia sipedon sipedon" -} -item { - name: "142783" - id: 1626 - display_name: "Myodocha serripes" -} -item { - name: "4733" - id: 1627 - display_name: "Ciconia ciconia" -} -item { - name: "29310" - id: 1628 - display_name: "Nerodia rhombifer" -} -item { - name: "201343" - id: 1629 - display_name: "Lithacodes fasciola" -} -item { - name: "21121" - id: 1630 - display_name: "Dendrobates auratus" -} -item { - name: "127618" - id: 1631 - display_name: "Epirrhoe alternata" -} -item { - name: "43115" - id: 1632 - display_name: "Sylvilagus audubonii" -} -item { - name: "29317" - id: 1633 - display_name: "Nerodia fasciata" -} -item { - name: "4742" - id: 1634 - display_name: "Mycteria americana" -} -item { - name: "53895" - id: 1635 - display_name: "Stenopelmatus fuscus" -} -item { - name: "4744" - id: 1636 - display_name: "Mycteria ibis" -} -item { - name: "12937" - id: 1637 - display_name: "Sialia mexicana" -} -item { - name: "29322" - id: 1638 - display_name: "Nerodia fasciata confluens" -} -item { - name: "29324" - id: 1639 - display_name: "Nerodia clarkii clarkii" -} -item { - name: "29327" - id: 1640 - display_name: "Nerodia cyclopion" -} -item { - name: "29328" - id: 1641 - display_name: "Nerodia erythrogaster" -} -item { - name: "53905" - id: 1642 - display_name: "Mantis religiosa" -} -item { - name: "4754" - id: 1643 - display_name: "Ephippiorhynchus senegalensis" -} -item { - name: "127635" - id: 1644 - display_name: "Plecia nearctica" -} -item { - name: "4756" - id: 1645 - display_name: "Cathartes aura" -} -item { - name: "29334" - id: 1646 - display_name: "Nerodia erythrogaster flavigaster" -} -item { - name: "12951" - id: 1647 - display_name: "Myadestes townsendi" -} -item { - name: "4761" - id: 1648 - display_name: "Cathartes burrovianus" -} -item { - name: "4763" - id: 1649 - display_name: "Sarcoramphus papa" -} -item { - name: "4765" - id: 1650 - display_name: "Coragyps atratus" -} -item { - name: "19890" - id: 1651 - display_name: "Strix nebulosa" -} -item { - name: "26736" - id: 1652 - display_name: "Ambystoma opacum" -} -item { - name: "66331" - id: 1653 - display_name: "Pelophylax perezi" -} -item { - name: "4776" - id: 1654 - display_name: "Anastomus lamelligerus" -} -item { - name: "4892" - id: 1655 - display_name: "Pluvialis squatarola" -} -item { - name: "4778" - id: 1656 - display_name: "Gymnogyps californianus" -} -item { - name: "12971" - id: 1657 - display_name: "Muscicapa striata" -} -item { - name: "56776" - id: 1658 - display_name: "Glaucopsyche lygdamus" -} -item { - name: "127669" - id: 1659 - display_name: "Jadera haematoloma" -} -item { - name: "4793" - id: 1660 - display_name: "Charadrius vociferus" -} -item { - name: "209594" - id: 1661 - display_name: "Scantius aegyptius" -} -item { - name: "4795" - id: 1662 - display_name: "Charadrius wilsonia" -} -item { - name: "48586" - id: 1663 - display_name: "Cepaea nemoralis" -} -item { - name: "4798" - id: 1664 - display_name: "Charadrius melodus" -} -item { - name: "12992" - id: 1665 - display_name: "Phoenicurus phoenicurus" -} -item { - name: "45763" - id: 1666 - display_name: "Ondatra zibethicus" -} -item { - name: "119492" - id: 1667 - display_name: "Smerinthus cerisyi" -} -item { - name: "13000" - id: 1668 - display_name: "Phoenicurus ochruros" -} -item { - name: "4811" - id: 1669 - display_name: "Charadrius dubius" -} -item { - name: "64973" - id: 1670 - display_name: "Anaxyrus cognatus" -} -item { - name: "2168" - id: 1671 - display_name: "Eumomota superciliosa" -} -item { - name: "6980" - id: 1672 - display_name: "Anas querquedula" -} -item { - name: "64975" - id: 1673 - display_name: "Anaxyrus debilis" -} -item { - name: "43130" - id: 1674 - display_name: "Lepus californicus" -} -item { - name: "67707" - id: 1675 - display_name: "Argiope aurantia" -} -item { - name: "4836" - id: 1676 - display_name: "Himantopus mexicanus" -} -item { - name: "4838" - id: 1677 - display_name: "Haematopus bachmani" -} -item { - name: "43132" - id: 1678 - display_name: "Lepus americanus" -} -item { - name: "144106" - id: 1679 - display_name: "Pica pica" -} -item { - name: "4843" - id: 1680 - display_name: "Haematopus ostralegus" -} -item { - name: "67709" - id: 1681 - display_name: "Antrodiaetus riversi" -} -item { - name: "4848" - id: 1682 - display_name: "Haematopus unicolor" -} -item { - name: "4857" - id: 1683 - display_name: "Vanellus vanellus" -} -item { - name: "29435" - id: 1684 - display_name: "Coluber flagellum testaceus" -} -item { - name: "119550" - id: 1685 - display_name: "Feltia jaculifera" -} -item { - name: "4866" - id: 1686 - display_name: "Vanellus spinosus" -} -item { - name: "4870" - id: 1687 - display_name: "Vanellus armatus" -} -item { - name: "54024" - id: 1688 - display_name: "Satyrium californica" -} -item { - name: "13071" - id: 1689 - display_name: "Luscinia svecica" -} -item { - name: "3544" - id: 1690 - display_name: "Columbina inca" -} -item { - name: "4883" - id: 1691 - display_name: "Recurvirostra avosetta" -} -item { - name: "204701" - id: 1692 - display_name: "Melanchra adjuncta" -} -item { - name: "56083" - id: 1693 - display_name: "Armadillidium vulgare" -} -item { - name: "981" - id: 1694 - display_name: "Phasianus colchicus" -} -item { - name: "4893" - id: 1695 - display_name: "Pluvialis dominica" -} -item { - name: "103200" - id: 1696 - display_name: "Hypsiglena jani" -} -item { - name: "127777" - id: 1697 - display_name: "Vespula vulgaris" -} -item { - name: "7643" - id: 1698 - display_name: "Cinclus mexicanus" -} -item { - name: "13094" - id: 1699 - display_name: "Erithacus rubecula" -} -item { - name: "41777" - id: 1700 - display_name: "Lontra canadensis" -} -item { - name: "64988" - id: 1701 - display_name: "Anaxyrus terrestris" -} -item { - name: "18167" - id: 1702 - display_name: "Melanerpes aurifrons" -} -item { - name: "54064" - id: 1703 - display_name: "Polygonia comma" -} -item { - name: "209713" - id: 1704 - display_name: "Phigalia titea" -} -item { - name: "54068" - id: 1705 - display_name: "Boloria selene" -} -item { - name: "104585" - id: 1706 - display_name: "Libellula semifasciata" -} -item { - name: "119608" - id: 1707 - display_name: "Theba pisana" -} -item { - name: "4801" - id: 1708 - display_name: "Charadrius hiaticula" -} -item { - name: "104586" - id: 1709 - display_name: "Libellula vibrans" -} -item { - name: "4935" - id: 1710 - display_name: "Egretta gularis" -} -item { - name: "4937" - id: 1711 - display_name: "Egretta caerulea" -} -item { - name: "4938" - id: 1712 - display_name: "Egretta tricolor" -} -item { - name: "4940" - id: 1713 - display_name: "Egretta thula" -} -item { - name: "340813" - id: 1714 - display_name: "Hyalymenus tarsatus" -} -item { - name: "4943" - id: 1715 - display_name: "Egretta garzetta" -} -item { - name: "4947" - id: 1716 - display_name: "Egretta sacra" -} -item { - name: "13141" - id: 1717 - display_name: "Monticola solitarius" -} -item { - name: "4952" - id: 1718 - display_name: "Ardea cocoi" -} -item { - name: "4954" - id: 1719 - display_name: "Ardea cinerea" -} -item { - name: "67727" - id: 1720 - display_name: "Aeshna umbrosa" -} -item { - name: "4956" - id: 1721 - display_name: "Ardea herodias" -} -item { - name: "144223" - id: 1722 - display_name: "Chlosyne theona" -} -item { - name: "201568" - id: 1723 - display_name: "Diabrotica undecimpunctata undecimpunctata" -} -item { - name: "47383" - id: 1724 - display_name: "Latrodectus geometricus" -} -item { - name: "119664" - id: 1725 - display_name: "Cacyreus marshalli" -} -item { - name: "62321" - id: 1726 - display_name: "Rutpela maculata" -} -item { - name: "217970" - id: 1727 - display_name: "Cyclophora pendulinaria" -} -item { - name: "4981" - id: 1728 - display_name: "Nycticorax nycticorax" -} -item { - name: "12714" - id: 1729 - display_name: "Turdus rufopalliatus" -} -item { - name: "4994" - id: 1730 - display_name: "Ardeola ralloides" -} -item { - name: "4999" - id: 1731 - display_name: "Nyctanassa violacea" -} -item { - name: "37769" - id: 1732 - display_name: "Plestiodon skiltonianus" -} -item { - name: "213826" - id: 1733 - display_name: "Apamea amputatrix" -} -item { - name: "67736" - id: 1734 - display_name: "Rhionaeschna californica" -} -item { - name: "155380" - id: 1735 - display_name: "Andricus crystallinus" -} -item { - name: "144280" - id: 1736 - display_name: "Aramides cajaneus" -} -item { - name: "5017" - id: 1737 - display_name: "Bubulcus ibis" -} -item { - name: "5020" - id: 1738 - display_name: "Butorides virescens" -} -item { - name: "144285" - id: 1739 - display_name: "Porphyrio martinicus" -} -item { - name: "81729" - id: 1740 - display_name: "Feniseca tarquinius" -} -item { - name: "127905" - id: 1741 - display_name: "Bombus ternarius" -} -item { - name: "5034" - id: 1742 - display_name: "Botaurus lentiginosus" -} -item { - name: "29330" - id: 1743 - display_name: "Nerodia erythrogaster transversa" -} -item { - name: "5036" - id: 1744 - display_name: "Cochlearius cochlearius" -} -item { - name: "46001" - id: 1745 - display_name: "Sciurus vulgaris" -} -item { - name: "46005" - id: 1746 - display_name: "Sciurus variegatoides" -} -item { - name: "127928" - id: 1747 - display_name: "Autochton cellus" -} -item { - name: "340923" - id: 1748 - display_name: "Scolypopa australis" -} -item { - name: "46017" - id: 1749 - display_name: "Sciurus carolinensis" -} -item { - name: "46018" - id: 1750 - display_name: "Sciurus aberti" -} -item { - name: "447427" - id: 1751 - display_name: "Neverita lewisii" -} -item { - name: "46020" - id: 1752 - display_name: "Sciurus niger" -} -item { - name: "5061" - id: 1753 - display_name: "Anhinga novaehollandiae" -} -item { - name: "46023" - id: 1754 - display_name: "Sciurus griseus" -} -item { - name: "122375" - id: 1755 - display_name: "Carterocephalus palaemon" -} -item { - name: "5066" - id: 1756 - display_name: "Anhinga rufa" -} -item { - name: "145289" - id: 1757 - display_name: "Melozone fusca" -} -item { - name: "5074" - id: 1758 - display_name: "Aquila chrysaetos" -} -item { - name: "49998" - id: 1759 - display_name: "Thamnophis sirtalis infernalis" -} -item { - name: "13270" - id: 1760 - display_name: "Hylocichla mustelina" -} -item { - name: "62423" - id: 1761 - display_name: "Cimbex americana" -} -item { - name: "62424" - id: 1762 - display_name: "Sitochroa palealis" -} -item { - name: "111578" - id: 1763 - display_name: "Regina grahamii" -} -item { - name: "144207" - id: 1764 - display_name: "Aphelocoma wollweberi" -} -item { - name: "62429" - id: 1765 - display_name: "Pyronia tithonus" -} -item { - name: "47934" - id: 1766 - display_name: "Libellula luctuosa" -} -item { - name: "50000" - id: 1767 - display_name: "Clemmys guttata" -} -item { - name: "5097" - id: 1768 - display_name: "Accipiter striatus" -} -item { - name: "119789" - id: 1769 - display_name: "Cisseps fulvicollis" -} -item { - name: "5106" - id: 1770 - display_name: "Accipiter nisus" -} -item { - name: "5108" - id: 1771 - display_name: "Accipiter gentilis" -} -item { - name: "62456" - id: 1772 - display_name: "Rhagonycha fulva" -} -item { - name: "4948" - id: 1773 - display_name: "Egretta rufescens" -} -item { - name: "46082" - id: 1774 - display_name: "Marmota marmota" -} -item { - name: "6990" - id: 1775 - display_name: "Bucephala clangula" -} -item { - name: "4535" - id: 1776 - display_name: "Anous stolidus" -} -item { - name: "46087" - id: 1777 - display_name: "Marmota caligata" -} -item { - name: "72458" - id: 1778 - display_name: "Actitis macularius" -} -item { - name: "4951" - id: 1779 - display_name: "Ardea purpurea" -} -item { - name: "128012" - id: 1780 - display_name: "Eumorpha fasciatus" -} -item { - name: "472078" - id: 1781 - display_name: "Todiramphus chloris" -} -item { - name: "46095" - id: 1782 - display_name: "Marmota monax" -} -item { - name: "34" - id: 1783 - display_name: "Grus americana" -} -item { - name: "4835" - id: 1784 - display_name: "Himantopus himantopus" -} -item { - name: "122374" - id: 1785 - display_name: "Eurema mexicana" -} -item { - name: "19812" - id: 1786 - display_name: "Glaucidium gnoma" -} -item { - name: "73823" - id: 1787 - display_name: "Hierophis viridiflavus" -} -item { - name: "5168" - id: 1788 - display_name: "Circus approximans" -} -item { - name: "143110" - id: 1789 - display_name: "Hypagyrtis unipunctata" -} -item { - name: "65976" - id: 1790 - display_name: "Lithobates blairi" -} -item { - name: "5173" - id: 1791 - display_name: "Circus aeruginosus" -} -item { - name: "54327" - id: 1792 - display_name: "Vespa crabro" -} -item { - name: "4273" - id: 1793 - display_name: "Phalacrocorax sulcirostris" -} -item { - name: "5180" - id: 1794 - display_name: "Buteo albonotatus" -} -item { - name: "103485" - id: 1795 - display_name: "Ischnura denticollis" -} -item { - name: "62528" - id: 1796 - display_name: "Butorides striata" -} -item { - name: "62529" - id: 1797 - display_name: "Platalea ajaja" -} -item { - name: "5186" - id: 1798 - display_name: "Buteo brachyurus" -} -item { - name: "103494" - id: 1799 - display_name: "Ischnura hastata" -} -item { - name: "144455" - id: 1800 - display_name: "Ardea alba" -} -item { - name: "103497" - id: 1801 - display_name: "Ischnura perparva" -} -item { - name: "103498" - id: 1802 - display_name: "Ischnura posita" -} -item { - name: "5196" - id: 1803 - display_name: "Buteo swainsoni" -} -item { - name: "128079" - id: 1804 - display_name: "Grammia ornata" -} -item { - name: "29777" - id: 1805 - display_name: "Lampropeltis triangulum" -} -item { - name: "867" - id: 1806 - display_name: "Alectoris rufa" -} -item { - name: "5206" - id: 1807 - display_name: "Buteo lineatus" -} -item { - name: "29783" - id: 1808 - display_name: "Lampropeltis triangulum triangulum" -} -item { - name: "122383" - id: 1809 - display_name: "Plebejus melissa" -} -item { - name: "5212" - id: 1810 - display_name: "Buteo jamaicensis" -} -item { - name: "81495" - id: 1811 - display_name: "Libellula pulchella" -} -item { - name: "35003" - id: 1812 - display_name: "Heloderma suspectum" -} -item { - name: "46180" - id: 1813 - display_name: "Cynomys gunnisoni" -} -item { - name: "144485" - id: 1814 - display_name: "Charadrius nivosus" -} -item { - name: "144490" - id: 1815 - display_name: "Tringa incana" -} -item { - name: "144491" - id: 1816 - display_name: "Tringa semipalmata" -} -item { - name: "25185" - id: 1817 - display_name: "Hypopachus variolosus" -} -item { - name: "5231" - id: 1818 - display_name: "Terathopius ecaudatus" -} -item { - name: "144496" - id: 1819 - display_name: "Gallinago delicata" -} -item { - name: "5233" - id: 1820 - display_name: "Buteogallus anthracinus" -} -item { - name: "211035" - id: 1821 - display_name: "Speranza pustularia" -} -item { - name: "29813" - id: 1822 - display_name: "Lampropeltis getula" -} -item { - name: "144502" - id: 1823 - display_name: "Chroicocephalus philadelphia" -} -item { - name: "5242" - id: 1824 - display_name: "Circaetus gallicus" -} -item { - name: "144507" - id: 1825 - display_name: "Chroicocephalus novaehollandiae" -} -item { - name: "144510" - id: 1826 - display_name: "Chroicocephalus ridibundus" -} -item { - name: "52757" - id: 1827 - display_name: "Polistes fuscatus" -} -item { - name: "144514" - id: 1828 - display_name: "Leucophaeus atricilla" -} -item { - name: "144515" - id: 1829 - display_name: "Leucophaeus pipixcan" -} -item { - name: "46217" - id: 1830 - display_name: "Tamias striatus" -} -item { - name: "144525" - id: 1831 - display_name: "Onychoprion fuscatus" -} -item { - name: "46222" - id: 1832 - display_name: "Tamias minimus" -} -item { - name: "144530" - id: 1833 - display_name: "Sternula antillarum" -} -item { - name: "46230" - id: 1834 - display_name: "Tamias merriami" -} -item { - name: "144537" - id: 1835 - display_name: "Hydroprogne caspia" -} -item { - name: "144539" - id: 1836 - display_name: "Thalasseus maximus" -} -item { - name: "144540" - id: 1837 - display_name: "Thalasseus bergii" -} -item { - name: "5277" - id: 1838 - display_name: "Elanus leucurus" -} -item { - name: "324766" - id: 1839 - display_name: "Epicallima argenticinctella" -} -item { - name: "72486" - id: 1840 - display_name: "Alopochen aegyptiaca" -} -item { - name: "62229" - id: 1841 - display_name: "Ischnura cervula" -} -item { - name: "144550" - id: 1842 - display_name: "Streptopelia senegalensis" -} -item { - name: "46256" - id: 1843 - display_name: "Ammospermophilus harrisii" -} -item { - name: "94559" - id: 1844 - display_name: "Argia nahuana" -} -item { - name: "46259" - id: 1845 - display_name: "Tamiasciurus douglasii" -} -item { - name: "46260" - id: 1846 - display_name: "Tamiasciurus hudsonicus" -} -item { - name: "119989" - id: 1847 - display_name: "Stagmomantis carolina" -} -item { - name: "13494" - id: 1848 - display_name: "Gerygone igata" -} -item { - name: "5305" - id: 1849 - display_name: "Haliaeetus leucocephalus" -} -item { - name: "7596" - id: 1850 - display_name: "Cistothorus platensis" -} -item { - name: "5308" - id: 1851 - display_name: "Haliaeetus vocifer" -} -item { - name: "218301" - id: 1852 - display_name: "Diacme elealis" -} -item { - name: "95422" - id: 1853 - display_name: "Basiaeschna janata" -} -item { - name: "46272" - id: 1854 - display_name: "Glaucomys volans" -} -item { - name: "120010" - id: 1855 - display_name: "Polistes metricus" -} -item { - name: "144594" - id: 1856 - display_name: "Bubo scandiacus" -} -item { - name: "52771" - id: 1857 - display_name: "Gonepteryx rhamni" -} -item { - name: "144597" - id: 1858 - display_name: "Ciccaba virgata" -} -item { - name: "890" - id: 1859 - display_name: "Bonasa umbellus" -} -item { - name: "52773" - id: 1860 - display_name: "Poanes zabulon" -} -item { - name: "120033" - id: 1861 - display_name: "Lapara bombycoides" -} -item { - name: "5346" - id: 1862 - display_name: "Busarellus nigricollis" -} -item { - name: "5349" - id: 1863 - display_name: "Rostrhamus sociabilis" -} -item { - name: "36391" - id: 1864 - display_name: "Anolis equestris" -} -item { - name: "46316" - id: 1865 - display_name: "Trichechus manatus" -} -item { - name: "5267" - id: 1866 - display_name: "Milvus milvus" -} -item { - name: "128241" - id: 1867 - display_name: "Darapsa choerilus" -} -item { - name: "128242" - id: 1868 - display_name: "Palthis angulalis" -} -item { - name: "5366" - id: 1869 - display_name: "Gyps fulvus" -} -item { - name: "204512" - id: 1870 - display_name: "Ficedula hypoleuca" -} -item { - name: "54526" - id: 1871 - display_name: "Crassadoma gigantea" -} -item { - name: "144642" - id: 1872 - display_name: "Momotus coeruliceps" -} -item { - name: "120070" - id: 1873 - display_name: "Strongylocentrotus droebachiensis" -} -item { - name: "54538" - id: 1874 - display_name: "Syngnathus leptorhynchus" -} -item { - name: "81746" - id: 1875 - display_name: "Necrophila americana" -} -item { - name: "300301" - id: 1876 - display_name: "Pseudomyrmex gracilis" -} -item { - name: "202003" - id: 1877 - display_name: "Apiomerus spissipes" -} -item { - name: "41860" - id: 1878 - display_name: "Enhydra lutris" -} -item { - name: "4817" - id: 1879 - display_name: "Charadrius semipalmatus" -} -item { - name: "36145" - id: 1880 - display_name: "Sceloporus variabilis" -} -item { - name: "202012" - id: 1881 - display_name: "Steatoda capensis" -} -item { - name: "62749" - id: 1882 - display_name: "Iphiclides podalirius" -} -item { - name: "5406" - id: 1883 - display_name: "Haliastur indus" -} -item { - name: "62751" - id: 1884 - display_name: "Andricus kingi" -} -item { - name: "5363" - id: 1885 - display_name: "Gyps africanus" -} -item { - name: "5416" - id: 1886 - display_name: "Ictinia mississippiensis" -} -item { - name: "62766" - id: 1887 - display_name: "Issoria lathonia" -} -item { - name: "62768" - id: 1888 - display_name: "Scolia dubia" -} -item { - name: "126206" - id: 1889 - display_name: "Dissosteira carolina" -} -item { - name: "269875" - id: 1890 - display_name: "Mallodon dasystomus" -} -item { - name: "155030" - id: 1891 - display_name: "Limenitis reducta" -} -item { - name: "62345" - id: 1892 - display_name: "Duttaphrynus melanostictus" -} -item { - name: "52519" - id: 1893 - display_name: "Aeshna cyanea" -} -item { - name: "10001" - id: 1894 - display_name: "Dives dives" -} -item { - name: "460365" - id: 1895 - display_name: "Tegula funebralis" -} -item { - name: "13631" - id: 1896 - display_name: "Baeolophus atricristatus" -} -item { - name: "13632" - id: 1897 - display_name: "Baeolophus bicolor" -} -item { - name: "13633" - id: 1898 - display_name: "Baeolophus inornatus" -} -item { - name: "9100" - id: 1899 - display_name: "Melospiza melodia" -} -item { - name: "62796" - id: 1900 - display_name: "Crotaphytus bicinctores" -} -item { - name: "62797" - id: 1901 - display_name: "Gambelia wislizenii" -} -item { - name: "46009" - id: 1902 - display_name: "Sciurus aureogaster" -} -item { - name: "112867" - id: 1903 - display_name: "Sparisoma viride" -} -item { - name: "70997" - id: 1904 - display_name: "Pelecinus polyturator" -} -item { - name: "62806" - id: 1905 - display_name: "Mytilus californianus" -} -item { - name: "120156" - id: 1906 - display_name: "Musca domestica" -} -item { - name: "136548" - id: 1907 - display_name: "Euclea delphinii" -} -item { - name: "50065" - id: 1908 - display_name: "Danaus eresimus" -} -item { - name: "43239" - id: 1909 - display_name: "Tachyglossus aculeatus" -} -item { - name: "145303" - id: 1910 - display_name: "Spinus spinus" -} -item { - name: "120183" - id: 1911 - display_name: "Araneus marmoreus" -} -item { - name: "71032" - id: 1912 - display_name: "Crotalus scutulatus scutulatus" -} -item { - name: "71034" - id: 1913 - display_name: "Tenodera sinensis" -} -item { - name: "143121" - id: 1914 - display_name: "Ochropleura implecta" -} -item { - name: "13695" - id: 1915 - display_name: "Motacilla alba" -} -item { - name: "7458" - id: 1916 - display_name: "Certhia americana" -} -item { - name: "38293" - id: 1917 - display_name: "Lampropholis delicata" -} -item { - name: "144281" - id: 1918 - display_name: "Bucorvus leadbeateri" -} -item { - name: "120217" - id: 1919 - display_name: "Halysidota tessellaris" -} -item { - name: "226718" - id: 1920 - display_name: "Otiorhynchus sulcatus" -} -item { - name: "464287" - id: 1921 - display_name: "Anteaeolidiella oliviae" -} -item { - name: "226720" - id: 1922 - display_name: "Oxychilus draparnaudi" -} -item { - name: "13729" - id: 1923 - display_name: "Anthus pratensis" -} -item { - name: "13732" - id: 1924 - display_name: "Anthus rubescens" -} -item { - name: "11930" - id: 1925 - display_name: "Tachycineta albilinea" -} -item { - name: "71085" - id: 1926 - display_name: "Varanus niloticus" -} -item { - name: "144814" - id: 1927 - display_name: "Poecile carolinensis" -} -item { - name: "144815" - id: 1928 - display_name: "Poecile atricapillus" -} -item { - name: "144816" - id: 1929 - display_name: "Poecile gambeli" -} -item { - name: "144820" - id: 1930 - display_name: "Poecile rufescens" -} -item { - name: "144823" - id: 1931 - display_name: "Periparus ater" -} -item { - name: "10485" - id: 1932 - display_name: "Chlorophanes spiza" -} -item { - name: "40523" - id: 1933 - display_name: "Lasiurus cinereus" -} -item { - name: "47719" - id: 1934 - display_name: "Datana ministra" -} -item { - name: "13770" - id: 1935 - display_name: "Estrilda astrild" -} -item { - name: "144849" - id: 1936 - display_name: "Cyanistes caeruleus" -} -item { - name: "218587" - id: 1937 - display_name: "Discus rotundatus" -} -item { - name: "47105" - id: 1938 - display_name: "Tamandua mexicana" -} -item { - name: "18463" - id: 1939 - display_name: "Sphyrapicus varius" -} -item { - name: "11858" - id: 1940 - display_name: "Petrochelidon pyrrhonota" -} -item { - name: "144882" - id: 1941 - display_name: "Troglodytes pacificus" -} -item { - name: "144883" - id: 1942 - display_name: "Troglodytes hiemalis" -} -item { - name: "153076" - id: 1943 - display_name: "Nephelodes minians" -} -item { - name: "62978" - id: 1944 - display_name: "Chlosyne nycteis" -} -item { - name: "128517" - id: 1945 - display_name: "Catocala ilia" -} -item { - name: "153102" - id: 1946 - display_name: "Dysphania militaris" -} -item { - name: "59651" - id: 1947 - display_name: "Aquarius remigis" -} -item { - name: "13851" - id: 1948 - display_name: "Passer montanus" -} -item { - name: "13858" - id: 1949 - display_name: "Passer domesticus" -} -item { - name: "39742" - id: 1950 - display_name: "Kinosternon flavescens" -} -item { - name: "506118" - id: 1951 - display_name: "Aphelocoma californica" -} -item { - name: "5672" - id: 1952 - display_name: "Amazilia yucatanensis" -} -item { - name: "5676" - id: 1953 - display_name: "Amazilia tzacatl" -} -item { - name: "204503" - id: 1954 - display_name: "Dicrurus adsimilis" -} -item { - name: "52785" - id: 1955 - display_name: "Megachile sculpturalis" -} -item { - name: "126905" - id: 1956 - display_name: "Harrisina americana" -} -item { - name: "55773" - id: 1957 - display_name: "Promachus hinei" -} -item { - name: "84752" - id: 1958 - display_name: "Microcentrum rhombifolium" -} -item { - name: "5698" - id: 1959 - display_name: "Amazilia violiceps" -} -item { - name: "145539" - id: 1960 - display_name: "Ovis canadensis nelsoni" -} -item { - name: "104004" - id: 1961 - display_name: "Lampropeltis splendida" -} -item { - name: "13893" - id: 1962 - display_name: "Lonchura punctulata" -} -item { - name: "63048" - id: 1963 - display_name: "Nuttallina californica" -} -item { - name: "226901" - id: 1964 - display_name: "Panopoda rufimargo" -} -item { - name: "194134" - id: 1965 - display_name: "Anthanassa tulcis" -} -item { - name: "5049" - id: 1966 - display_name: "Tigrisoma mexicanum" -} -item { - name: "407130" - id: 1967 - display_name: "Porphyrio melanotus melanotus" -} -item { - name: "226910" - id: 1968 - display_name: "Panthea furcilla" -} -item { - name: "130661" - id: 1969 - display_name: "Catasticta nimbice" -} -item { - name: "120215" - id: 1970 - display_name: "Bombus griseocollis" -} -item { - name: "144220" - id: 1971 - display_name: "Melanitta americana" -} -item { - name: "9148" - id: 1972 - display_name: "Spizella pallida" -} -item { - name: "320610" - id: 1973 - display_name: "Sceloporus magister" -} -item { - name: "54900" - id: 1974 - display_name: "Papilio polyxenes asterius" -} -item { - name: "36080" - id: 1975 - display_name: "Callisaurus draconoides" -} -item { - name: "5758" - id: 1976 - display_name: "Amazilia rutila" -} -item { - name: "3465" - id: 1977 - display_name: "Zenaida aurita" -} -item { - name: "116461" - id: 1978 - display_name: "Anolis sagrei" -} -item { - name: "61295" - id: 1979 - display_name: "Aporia crataegi" -} -item { - name: "131673" - id: 1980 - display_name: "Tetracis cachexiata" -} -item { - name: "63113" - id: 1981 - display_name: "Blarina brevicauda" -} -item { - name: "26904" - id: 1982 - display_name: "Coronella austriaca" -} -item { - name: "94575" - id: 1983 - display_name: "Argia tibialis" -} -item { - name: "237166" - id: 1984 - display_name: "Lycaena phlaeas hypophlaeas" -} -item { - name: "129305" - id: 1985 - display_name: "Melanoplus bivittatus" -} -item { - name: "63128" - id: 1986 - display_name: "Speyeria atlantis" -} -item { - name: "113514" - id: 1987 - display_name: "Sympetrum internum" -} -item { - name: "48757" - id: 1988 - display_name: "Echinothrix calamaris" -} -item { - name: "128670" - id: 1989 - display_name: "Bombus vagans" -} -item { - name: "13988" - id: 1990 - display_name: "Prunella modularis" -} -item { - name: "54951" - id: 1991 - display_name: "Anartia fatima" -} -item { - name: "54952" - id: 1992 - display_name: "Cardisoma guanhumi" -} -item { - name: "325295" - id: 1993 - display_name: "Cydalima perspectalis" -} -item { - name: "63160" - id: 1994 - display_name: "Celithemis elisa" -} -item { - name: "210615" - id: 1995 - display_name: "Pyrausta volupialis" -} -item { - name: "472766" - id: 1996 - display_name: "Falco tinnunculus" -} -item { - name: "29927" - id: 1997 - display_name: "Heterodon nasicus" -} -item { - name: "145088" - id: 1998 - display_name: "Ixoreus naevius" -} -item { - name: "6432" - id: 1999 - display_name: "Archilochus colubris" -} -item { - name: "5827" - id: 2000 - display_name: "Lampornis clemenciae" -} -item { - name: "15990" - id: 2001 - display_name: "Myiarchus tuberculifer" -} -item { - name: "128712" - id: 2002 - display_name: "Coccinella californica" -} -item { - name: "67559" - id: 2003 - display_name: "Adelpha eulalia" -} -item { - name: "128719" - id: 2004 - display_name: "Echinometra mathaei" -} -item { - name: "10247" - id: 2005 - display_name: "Setophaga ruticilla" -} -item { - name: "202451" - id: 2006 - display_name: "Copaeodes minima" -} -item { - name: "95958" - id: 2007 - display_name: "Boyeria vinosa" -} -item { - name: "16016" - id: 2008 - display_name: "Myiarchus tyrannulus" -} -item { - name: "36202" - id: 2009 - display_name: "Sceloporus olivaceus" -} -item { - name: "95982" - id: 2010 - display_name: "Brachymesia furcata" -} -item { - name: "126589" - id: 2011 - display_name: "Calycopis isobeon" -} -item { - name: "120578" - id: 2012 - display_name: "Micrathena sagittata" -} -item { - name: "194690" - id: 2013 - display_name: "Pogonomyrmex barbatus" -} -item { - name: "120583" - id: 2014 - display_name: "Parasteatoda tepidariorum" -} -item { - name: "202505" - id: 2015 - display_name: "Zosterops lateralis" -} -item { - name: "38671" - id: 2016 - display_name: "Aspidoscelis tigris" -} -item { - name: "38672" - id: 2017 - display_name: "Aspidoscelis tigris stejnegeri" -} -item { - name: "9176" - id: 2018 - display_name: "Zonotrichia leucophrys" -} -item { - name: "120596" - id: 2019 - display_name: "Aphonopelma hentzi" -} -item { - name: "9744" - id: 2020 - display_name: "Agelaius phoeniceus" -} -item { - name: "38684" - id: 2021 - display_name: "Aspidoscelis tigris mundus" -} -item { - name: "62426" - id: 2022 - display_name: "Aphantopus hyperantus" -} -item { - name: "30494" - id: 2023 - display_name: "Micrurus tener" -} -item { - name: "58578" - id: 2024 - display_name: "Euphydryas phaeton" -} -item { - name: "96036" - id: 2025 - display_name: "Brechmorhoga mendax" -} -item { - name: "333608" - id: 2026 - display_name: "Leukoma staminea" -} -item { - name: "38703" - id: 2027 - display_name: "Aspidoscelis sexlineata sexlineata" -} -item { - name: "126600" - id: 2028 - display_name: "Chortophaga viridifasciata" -} -item { - name: "63287" - id: 2029 - display_name: "Megalorchestia californiana" -} -item { - name: "128824" - id: 2030 - display_name: "Lucilia sericata" -} -item { - name: "104249" - id: 2031 - display_name: "Lepisosteus oculatus" -} -item { - name: "203153" - id: 2032 - display_name: "Parus major" -} -item { - name: "9183" - id: 2033 - display_name: "Zonotrichia capensis" -} -item { - name: "82201" - id: 2034 - display_name: "Hypena baltimoralis" -} -item { - name: "145217" - id: 2035 - display_name: "Oreothlypis peregrina" -} -item { - name: "145218" - id: 2036 - display_name: "Oreothlypis celata" -} -item { - name: "145221" - id: 2037 - display_name: "Oreothlypis ruficapilla" -} -item { - name: "145224" - id: 2038 - display_name: "Geothlypis philadelphia" -} -item { - name: "145225" - id: 2039 - display_name: "Geothlypis formosa" -} -item { - name: "448331" - id: 2040 - display_name: "Ambigolimax valentianus" -} -item { - name: "128845" - id: 2041 - display_name: "Copestylum mexicanum" -} -item { - name: "145231" - id: 2042 - display_name: "Setophaga tigrina" -} -item { - name: "145233" - id: 2043 - display_name: "Setophaga americana" -} -item { - name: "145235" - id: 2044 - display_name: "Setophaga magnolia" -} -item { - name: "145236" - id: 2045 - display_name: "Setophaga castanea" -} -item { - name: "145237" - id: 2046 - display_name: "Setophaga fusca" -} -item { - name: "145238" - id: 2047 - display_name: "Setophaga petechia" -} -item { - name: "145240" - id: 2048 - display_name: "Setophaga striata" -} -item { - name: "145242" - id: 2049 - display_name: "Setophaga palmarum" -} -item { - name: "179855" - id: 2050 - display_name: "Polites vibex" -} -item { - name: "145244" - id: 2051 - display_name: "Setophaga pinus" -} -item { - name: "145245" - id: 2052 - display_name: "Setophaga coronata" -} -item { - name: "145246" - id: 2053 - display_name: "Setophaga dominica" -} -item { - name: "5987" - id: 2054 - display_name: "Campylopterus hemileucurus" -} -item { - name: "17382" - id: 2055 - display_name: "Vireo cassinii" -} -item { - name: "145254" - id: 2056 - display_name: "Setophaga nigrescens" -} -item { - name: "145255" - id: 2057 - display_name: "Setophaga townsendi" -} -item { - name: "145256" - id: 2058 - display_name: "Setophaga occidentalis" -} -item { - name: "145257" - id: 2059 - display_name: "Setophaga chrysoparia" -} -item { - name: "145258" - id: 2060 - display_name: "Setophaga virens" -} -item { - name: "48786" - id: 2061 - display_name: "Pollicipes polymerus" -} -item { - name: "36207" - id: 2062 - display_name: "Sceloporus occidentalis longipes" -} -item { - name: "22392" - id: 2063 - display_name: "Eleutherodactylus marnockii" -} -item { - name: "22393" - id: 2064 - display_name: "Eleutherodactylus cystignathoides" -} -item { - name: "145275" - id: 2065 - display_name: "Cardellina canadensis" -} -item { - name: "145277" - id: 2066 - display_name: "Cardellina rubra" -} -item { - name: "7829" - id: 2067 - display_name: "Aphelocoma coerulescens" -} -item { - name: "41963" - id: 2068 - display_name: "Panthera pardus" -} -item { - name: "142998" - id: 2069 - display_name: "Pyrausta acrionalis" -} -item { - name: "18204" - id: 2070 - display_name: "Melanerpes erythrocephalus" -} -item { - name: "47425" - id: 2071 - display_name: "Tonicella lineata" -} -item { - name: "148460" - id: 2072 - display_name: "Charadra deridens" -} -item { - name: "145291" - id: 2073 - display_name: "Emberiza calandra" -} -item { - name: "52523" - id: 2074 - display_name: "Carcinus maenas" -} -item { - name: "46994" - id: 2075 - display_name: "Scapanus latimanus" -} -item { - name: "114314" - id: 2076 - display_name: "Tramea onusta" -} -item { - name: "145300" - id: 2077 - display_name: "Acanthis flammea" -} -item { - name: "63382" - id: 2078 - display_name: "Dermasterias imbricata" -} -item { - name: "126772" - id: 2079 - display_name: "Ursus americanus californiensis" -} -item { - name: "145304" - id: 2080 - display_name: "Spinus pinus" -} -item { - name: "10294" - id: 2081 - display_name: "Thraupis abbas" -} -item { - name: "145308" - id: 2082 - display_name: "Spinus psaltria" -} -item { - name: "145309" - id: 2083 - display_name: "Spinus lawrencei" -} -item { - name: "145310" - id: 2084 - display_name: "Spinus tristis" -} -item { - name: "3739" - id: 2085 - display_name: "Threskiornis aethiopicus" -} -item { - name: "47014" - id: 2086 - display_name: "Scalopus aquaticus" -} -item { - name: "4566" - id: 2087 - display_name: "Gygis alba" -} -item { - name: "43335" - id: 2088 - display_name: "Equus quagga" -} -item { - name: "41970" - id: 2089 - display_name: "Panthera onca" -} -item { - name: "128950" - id: 2090 - display_name: "Lycomorpha pholus" -} -item { - name: "11935" - id: 2091 - display_name: "Tachycineta bicolor" -} -item { - name: "333759" - id: 2092 - display_name: "Larus dominicanus dominicanus" -} -item { - name: "143008" - id: 2093 - display_name: "Herpetogramma pertextalis" -} -item { - name: "235341" - id: 2094 - display_name: "Coenonympha tullia california" -} -item { - name: "44705" - id: 2095 - display_name: "Mus musculus" -} -item { - name: "145352" - id: 2096 - display_name: "Lonchura oryzivora" -} -item { - name: "4840" - id: 2097 - display_name: "Haematopus palliatus" -} -item { - name: "244845" - id: 2098 - display_name: "Apiomerus californicus" -} -item { - name: "145360" - id: 2099 - display_name: "Chloris chloris" -} -item { - name: "5112" - id: 2100 - display_name: "Accipiter cooperii" -} -item { - name: "30675" - id: 2101 - display_name: "Agkistrodon piscivorus" -} -item { - name: "341972" - id: 2102 - display_name: "Crocodylus niloticus" -} -item { - name: "30677" - id: 2103 - display_name: "Agkistrodon piscivorus conanti" -} -item { - name: "30678" - id: 2104 - display_name: "Agkistrodon contortrix" -} -item { - name: "52900" - id: 2105 - display_name: "Caenurgina crassiuscula" -} -item { - name: "30682" - id: 2106 - display_name: "Agkistrodon contortrix laticinctus" -} -item { - name: "47067" - id: 2107 - display_name: "Bradypus variegatus" -} -item { - name: "55260" - id: 2108 - display_name: "Erythemis vesiculosa" -} -item { - name: "17402" - id: 2109 - display_name: "Vireo solitarius" -} -item { - name: "6369" - id: 2110 - display_name: "Selasphorus platycercus" -} -item { - name: "104416" - id: 2111 - display_name: "Lestes alacer" -} -item { - name: "128993" - id: 2112 - display_name: "Narceus annularus" -} -item { - name: "104422" - id: 2113 - display_name: "Lestes congener" -} -item { - name: "227307" - id: 2114 - display_name: "Patalene olyzonaria" -} -item { - name: "104429" - id: 2115 - display_name: "Lestes dryas" -} -item { - name: "194542" - id: 2116 - display_name: "Phyciodes graphica" -} -item { - name: "52904" - id: 2117 - display_name: "Microcrambus elegans" -} -item { - name: "129363" - id: 2118 - display_name: "Calephelis nemesis" -} -item { - name: "144506" - id: 2119 - display_name: "Chroicocephalus scopulinus" -} -item { - name: "30713" - id: 2120 - display_name: "Crotalus oreganus helleri" -} -item { - name: "47101" - id: 2121 - display_name: "Choloepus hoffmanni" -} -item { - name: "210942" - id: 2122 - display_name: "Caedicia simplex" -} -item { - name: "30719" - id: 2123 - display_name: "Crotalus scutulatus" -} -item { - name: "30724" - id: 2124 - display_name: "Crotalus ruber" -} -item { - name: "47110" - id: 2125 - display_name: "Triopha maculata" -} -item { - name: "4235" - id: 2126 - display_name: "Aechmophorus occidentalis" -} -item { - name: "30731" - id: 2127 - display_name: "Crotalus molossus" -} -item { - name: "30733" - id: 2128 - display_name: "Crotalus molossus nigrescens" -} -item { - name: "30735" - id: 2129 - display_name: "Crotalus mitchellii" -} -item { - name: "30740" - id: 2130 - display_name: "Crotalus lepidus" -} -item { - name: "30746" - id: 2131 - display_name: "Crotalus horridus" -} -item { - name: "63518" - id: 2132 - display_name: "Melanoplus differentialis" -} -item { - name: "30751" - id: 2133 - display_name: "Crotalus cerastes" -} -item { - name: "126640" - id: 2134 - display_name: "Caenurgina erechtea" -} -item { - name: "46086" - id: 2135 - display_name: "Marmota flaviventris" -} -item { - name: "194599" - id: 2136 - display_name: "Heliomata cycladata" -} -item { - name: "30764" - id: 2137 - display_name: "Crotalus atrox" -} -item { - name: "204520" - id: 2138 - display_name: "Hemiphaga novaeseelandiae" -} -item { - name: "128141" - id: 2139 - display_name: "Crepidula adunca" -} -item { - name: "121183" - id: 2140 - display_name: "Mythimna unipuncta" -} -item { - name: "40827" - id: 2141 - display_name: "Eidolon helvum" -} -item { - name: "4571" - id: 2142 - display_name: "Xema sabini" -} -item { - name: "211007" - id: 2143 - display_name: "Nepytia canosaria" -} -item { - name: "47171" - id: 2144 - display_name: "Flabellina iodinea" -} -item { - name: "211012" - id: 2145 - display_name: "Maliattha synochitis" -} -item { - name: "30798" - id: 2146 - display_name: "Bothrops asper" -} -item { - name: "47188" - id: 2147 - display_name: "Pachygrapsus crassipes" -} -item { - name: "55387" - id: 2148 - display_name: "Esox lucius" -} -item { - name: "58583" - id: 2149 - display_name: "Limenitis arthemis arthemis" -} -item { - name: "104548" - id: 2150 - display_name: "Leucorrhinia frigida" -} -item { - name: "104550" - id: 2151 - display_name: "Leucorrhinia hudsonica" -} -item { - name: "104551" - id: 2152 - display_name: "Leucorrhinia intacta" -} -item { - name: "47209" - id: 2153 - display_name: "Hermissenda crassicornis" -} -item { - name: "55655" - id: 2154 - display_name: "Lycaena phlaeas" -} -item { - name: "202861" - id: 2155 - display_name: "Otala lactea" -} -item { - name: "143037" - id: 2156 - display_name: "Lineodes integra" -} -item { - name: "47219" - id: 2157 - display_name: "Apis mellifera" -} -item { - name: "24254" - id: 2158 - display_name: "Pseudacris cadaverina" -} -item { - name: "47226" - id: 2159 - display_name: "Papilio rutulus" -} -item { - name: "104572" - id: 2160 - display_name: "Libellula comanche" -} -item { - name: "104574" - id: 2161 - display_name: "Libellula croceipennis" -} -item { - name: "104575" - id: 2162 - display_name: "Libellula cyanea" -} -item { - name: "145538" - id: 2163 - display_name: "Ovis canadensis canadensis" -} -item { - name: "104580" - id: 2164 - display_name: "Libellula incesta" -} -item { - name: "24257" - id: 2165 - display_name: "Pseudacris streckeri" -} -item { - name: "53866" - id: 2166 - display_name: "Calpodes ethlius" -} -item { - name: "18796" - id: 2167 - display_name: "Ramphastos sulfuratus" -} -item { - name: "2413" - id: 2168 - display_name: "Dacelo novaeguineae" -} -item { - name: "482" - id: 2169 - display_name: "Fulica atra" -} -item { - name: "47251" - id: 2170 - display_name: "Sphyraena barracuda" -} -item { - name: "358549" - id: 2171 - display_name: "Hemaris diffinis" -} -item { - name: "81526" - id: 2172 - display_name: "Crotalus viridis" -} -item { - name: "342169" - id: 2173 - display_name: "Hirundo rustica erythrogaster" -} -item { - name: "39280" - id: 2174 - display_name: "Leiocephalus carinatus" -} -item { - name: "47269" - id: 2175 - display_name: "Dasyatis americana" -} -item { - name: "55467" - id: 2176 - display_name: "Sabulodes aegrotata" -} -item { - name: "6316" - id: 2177 - display_name: "Calypte costae" -} -item { - name: "6317" - id: 2178 - display_name: "Calypte anna" -} -item { - name: "47280" - id: 2179 - display_name: "Pterois volitans" -} -item { - name: "81608" - id: 2180 - display_name: "Geukensia demissa" -} -item { - name: "121012" - id: 2181 - display_name: "Euglandina rosea" -} -item { - name: "236980" - id: 2182 - display_name: "Colaptes auratus cafer" -} -item { - name: "38673" - id: 2183 - display_name: "Aspidoscelis tigris tigris" -} -item { - name: "3786" - id: 2184 - display_name: "Sula nebouxii" -} -item { - name: "55487" - id: 2185 - display_name: "Diabrotica undecimpunctata" -} -item { - name: "243904" - id: 2186 - display_name: "Phrynosoma platyrhinos" -} -item { - name: "55489" - id: 2187 - display_name: "Cycloneda munda" -} -item { - name: "204491" - id: 2188 - display_name: "Copsychus saularis" -} -item { - name: "55492" - id: 2189 - display_name: "Cycloneda polita" -} -item { - name: "129222" - id: 2190 - display_name: "Heterophleps triguttaria" -} -item { - name: "129223" - id: 2191 - display_name: "Pasiphila rectangulata" -} -item { - name: "28365" - id: 2192 - display_name: "Thamnophis sirtalis sirtalis" -} -item { - name: "47316" - id: 2193 - display_name: "Chaetodon lunula" -} -item { - name: "6359" - id: 2194 - display_name: "Selasphorus sasin" -} -item { - name: "62500" - id: 2195 - display_name: "Leptophobia aripa" -} -item { - name: "6363" - id: 2196 - display_name: "Selasphorus rufus" -} -item { - name: "96480" - id: 2197 - display_name: "Calopteryx aequabilis" -} -item { - name: "55521" - id: 2198 - display_name: "Papilio eurymedon" -} -item { - name: "6371" - id: 2199 - display_name: "Calothorax lucifer" -} -item { - name: "129263" - id: 2200 - display_name: "Syrbula admirabilis" -} -item { - name: "28371" - id: 2201 - display_name: "Thamnophis sirtalis fitchi" -} -item { - name: "243962" - id: 2202 - display_name: "Charina bottae" -} -item { - name: "145659" - id: 2203 - display_name: "Acronicta americana" -} -item { - name: "14588" - id: 2204 - display_name: "Pycnonotus barbatus" -} -item { - name: "480298" - id: 2205 - display_name: "Cornu aspersum" -} -item { - name: "51584" - id: 2206 - display_name: "Melanitis leda" -} -item { - name: "243970" - id: 2207 - display_name: "Larus glaucescens \303\227 occidentalis" -} -item { - name: "55556" - id: 2208 - display_name: "Oncopeltus fasciatus" -} -item { - name: "506117" - id: 2209 - display_name: "Aphelocoma woodhouseii" -} -item { - name: "63750" - id: 2210 - display_name: "Anavitrinella pampinaria" -} -item { - name: "30983" - id: 2211 - display_name: "Sistrurus miliarius" -} -item { - name: "211210" - id: 2212 - display_name: "Holocnemus pluchei" -} -item { - name: "49587" - id: 2213 - display_name: "Micropterus salmoides" -} -item { - name: "6417" - id: 2214 - display_name: "Florisuga mellivora" -} -item { - name: "47381" - id: 2215 - display_name: "Latrodectus mactans" -} -item { - name: "47382" - id: 2216 - display_name: "Latrodectus hesperus" -} -item { - name: "4851" - id: 2217 - display_name: "Haematopus finschi" -} -item { - name: "51588" - id: 2218 - display_name: "Papilio polytes" -} -item { - name: "144431" - id: 2219 - display_name: "Falcipennis canadensis" -} -item { - name: "118490" - id: 2220 - display_name: "Haematopis grataria" -} -item { - name: "6433" - id: 2221 - display_name: "Archilochus alexandri" -} -item { - name: "52956" - id: 2222 - display_name: "Chaetodon capistratus" -} -item { - name: "203050" - id: 2223 - display_name: "Junonia genoveva" -} -item { - name: "5170" - id: 2224 - display_name: "Circus cyaneus" -} -item { - name: "84332" - id: 2225 - display_name: "Panorpa nuptialis" -} -item { - name: "47414" - id: 2226 - display_name: "Emerita analoga" -} -item { - name: "129335" - id: 2227 - display_name: "Gibbifer californicus" -} -item { - name: "55610" - id: 2228 - display_name: "Pyrrhocoris apterus" -} -item { - name: "58421" - id: 2229 - display_name: "Phidippus johnsoni" -} -item { - name: "208608" - id: 2230 - display_name: "Trachymela sloanei" -} -item { - name: "68138" - id: 2231 - display_name: "Sympetrum corruptum" -} -item { - name: "129350" - id: 2232 - display_name: "Photinus pyralis" -} -item { - name: "55625" - id: 2233 - display_name: "Sympetrum striolatum" -} -item { - name: "55626" - id: 2234 - display_name: "Pieris rapae" -} -item { - name: "203084" - id: 2235 - display_name: "Ardea alba modesta" -} -item { - name: "129362" - id: 2236 - display_name: "Zerene cesonia" -} -item { - name: "55638" - id: 2237 - display_name: "Anania hortulata" -} -item { - name: "148537" - id: 2238 - display_name: "Astraptes fulgerator" -} -item { - name: "55640" - id: 2239 - display_name: "Celastrina argiolus" -} -item { - name: "55641" - id: 2240 - display_name: "Polyommatus icarus" -} -item { - name: "16028" - id: 2241 - display_name: "Myiarchus crinitus" -} -item { - name: "55643" - id: 2242 - display_name: "Araschnia levana" -} -item { - name: "121180" - id: 2243 - display_name: "Megastraea undosa" -} -item { - name: "47454" - id: 2244 - display_name: "Triopha catalinae" -} -item { - name: "28389" - id: 2245 - display_name: "Thamnophis ordinoides" -} -item { - name: "68139" - id: 2246 - display_name: "Sympetrum vicinum" -} -item { - name: "55651" - id: 2247 - display_name: "Autographa gamma" -} -item { - name: "55653" - id: 2248 - display_name: "Maniola jurtina" -} -item { - name: "84369" - id: 2249 - display_name: "Libellula forensis" -} -item { - name: "47135" - id: 2250 - display_name: "Badumna longinqua" -} -item { - name: "48213" - id: 2251 - display_name: "Ariolimax californicus" -} -item { - name: "121196" - id: 2252 - display_name: "Acanthurus coeruleus" -} -item { - name: "47469" - id: 2253 - display_name: "Doris montereyensis" -} -item { - name: "5181" - id: 2254 - display_name: "Buteo regalis" -} -item { - name: "47472" - id: 2255 - display_name: "Acanthodoris lutea" -} -item { - name: "129415" - id: 2256 - display_name: "Copaeodes aurantiaca" -} -item { - name: "47505" - id: 2257 - display_name: "Geitodoris heathi" -} -item { - name: "28398" - id: 2258 - display_name: "Thamnophis elegans" -} -item { - name: "6553" - id: 2259 - display_name: "Aeronautes saxatalis" -} -item { - name: "47516" - id: 2260 - display_name: "Oncorhynchus mykiss" -} -item { - name: "6557" - id: 2261 - display_name: "Chaetura vauxi" -} -item { - name: "47518" - id: 2262 - display_name: "Salmo trutta" -} -item { - name: "55711" - id: 2263 - display_name: "Ladona depressa" -} -item { - name: "55719" - id: 2264 - display_name: "Eristalis tenax" -} -item { - name: "6571" - id: 2265 - display_name: "Chaetura pelagica" -} -item { - name: "119881" - id: 2266 - display_name: "Chrysochus cobaltinus" -} -item { - name: "145239" - id: 2267 - display_name: "Setophaga pensylvanica" -} -item { - name: "154043" - id: 2268 - display_name: "Bombus huntii" -} -item { - name: "41955" - id: 2269 - display_name: "Acinonyx jubatus" -} -item { - name: "55746" - id: 2270 - display_name: "Misumena vatia" -} -item { - name: "12024" - id: 2271 - display_name: "Lanius ludovicianus" -} -item { - name: "5063" - id: 2272 - display_name: "Anhinga anhinga" -} -item { - name: "59892" - id: 2273 - display_name: "Prionus californicus" -} -item { - name: "52986" - id: 2274 - display_name: "Largus californicus" -} -item { - name: "204454" - id: 2275 - display_name: "Acridotheres tristis" -} -item { - name: "14816" - id: 2276 - display_name: "Sitta pygmaea" -} -item { - name: "148560" - id: 2277 - display_name: "Mestra amymone" -} -item { - name: "4585" - id: 2278 - display_name: "Actophilornis africanus" -} -item { - name: "47590" - id: 2279 - display_name: "Phloeodes diabolicus" -} -item { - name: "14823" - id: 2280 - display_name: "Sitta canadensis" -} -item { - name: "14824" - id: 2281 - display_name: "Sitta europaea" -} -item { - name: "14825" - id: 2282 - display_name: "Sitta pusilla" -} -item { - name: "67598" - id: 2283 - display_name: "Solenopsis invicta" -} -item { - name: "6638" - id: 2284 - display_name: "Apus apus" -} -item { - name: "301557" - id: 2285 - display_name: "Euphoria basalis" -} -item { - name: "132070" - id: 2286 - display_name: "Phaneroptera nana" -} -item { - name: "14850" - id: 2287 - display_name: "Sturnus vulgaris" -} -item { - name: "62550" - id: 2288 - display_name: "Seiurus aurocapilla" -} -item { - name: "64006" - id: 2289 - display_name: "Corbicula fluminea" -} -item { - name: "204545" - id: 2290 - display_name: "Motacilla flava" -} -item { - name: "47632" - id: 2291 - display_name: "Katharina tunicata" -} -item { - name: "325309" - id: 2292 - display_name: "Chortophaga viridifasciata viridifasciata" -} -item { - name: "104993" - id: 2293 - display_name: "Macrodiplax balteata" -} -item { - name: "17408" - id: 2294 - display_name: "Vireo griseus" -} -item { - name: "14895" - id: 2295 - display_name: "Toxostoma longirostre" -} -item { - name: "47664" - id: 2296 - display_name: "Henricia leviuscula" -} -item { - name: "31281" - id: 2297 - display_name: "Calotes versicolor" -} -item { - name: "119086" - id: 2298 - display_name: "Agrius cingulata" -} -item { - name: "3849" - id: 2299 - display_name: "Calidris alba" -} -item { - name: "14906" - id: 2300 - display_name: "Toxostoma redivivum" -} -item { - name: "144479" - id: 2301 - display_name: "Gallinula galeata" -} -item { - name: "3850" - id: 2302 - display_name: "Calidris himantopus" -} -item { - name: "117520" - id: 2303 - display_name: "Enhydra lutris nereis" -} -item { - name: "51491" - id: 2304 - display_name: "Myliobatis californica" -} -item { - name: "121612" - id: 2305 - display_name: "Estigmene acrea" -} -item { - name: "105034" - id: 2306 - display_name: "Macromia illinoiensis" -} -item { - name: "6498" - id: 2307 - display_name: "Eugenes fulgens" -} -item { - name: "46179" - id: 2308 - display_name: "Cynomys ludovicianus" -} -item { - name: "105049" - id: 2309 - display_name: "Macromia taeniolata" -} -item { - name: "94045" - id: 2310 - display_name: "Anax longipes" -} -item { - name: "143119" - id: 2311 - display_name: "Galgula partita" -} -item { - name: "9317" - id: 2312 - display_name: "Icterus wagleri" -} -item { - name: "122704" - id: 2313 - display_name: "Nucella ostrina" -} -item { - name: "146709" - id: 2314 - display_name: "Grylloprociphilus imbricator" -} -item { - name: "9318" - id: 2315 - display_name: "Icterus parisorum" -} -item { - name: "85333" - id: 2316 - display_name: "Micrathena gracilis" -} -item { - name: "126737" - id: 2317 - display_name: "Anania funebris" -} -item { - name: "49053" - id: 2318 - display_name: "Cryptochiton stelleri" -} -item { - name: "47721" - id: 2319 - display_name: "Parastichopus californicus" -} -item { - name: "34050" - id: 2320 - display_name: "Phelsuma laticauda" -} -item { - name: "154219" - id: 2321 - display_name: "Notarctia proxima" -} -item { - name: "51781" - id: 2322 - display_name: "Tyria jacobaeae" -} -item { - name: "24230" - id: 2323 - display_name: "Acris crepitans" -} -item { - name: "146032" - id: 2324 - display_name: "Coluber flagellum" -} -item { - name: "146033" - id: 2325 - display_name: "Coluber flagellum flagellum" -} -item { - name: "244340" - id: 2326 - display_name: "Hordnia atropunctata" -} -item { - name: "146037" - id: 2327 - display_name: "Coluber taeniatus" -} -item { - name: "244344" - id: 2328 - display_name: "Scopula rubraria" -} -item { - name: "47737" - id: 2329 - display_name: "Harpaphe haydeniana" -} -item { - name: "5227" - id: 2330 - display_name: "Buteo platypterus" -} -item { - name: "39556" - id: 2331 - display_name: "Apalone spinifera" -} -item { - name: "39560" - id: 2332 - display_name: "Apalone spinifera emoryi" -} -item { - name: "318836" - id: 2333 - display_name: "Gallinago gallinago" -} -item { - name: "105098" - id: 2334 - display_name: "Magicicada septendecim" -} -item { - name: "96907" - id: 2335 - display_name: "Celithemis fasciata" -} -item { - name: "9325" - id: 2336 - display_name: "Icterus spurius" -} -item { - name: "3864" - id: 2337 - display_name: "Calidris minutilla" -} -item { - name: "14995" - id: 2338 - display_name: "Dumetella carolinensis" -} -item { - name: "424597" - id: 2339 - display_name: "Porphyrio hochstetteri" -} -item { - name: "47768" - id: 2340 - display_name: "Doriopsilla albopunctata" -} -item { - name: "498116" - id: 2341 - display_name: "Aeolidia papillosa" -} -item { - name: "244378" - id: 2342 - display_name: "Mallophora fautrix" -} -item { - name: "3866" - id: 2343 - display_name: "Calidris fuscicollis" -} -item { - name: "47776" - id: 2344 - display_name: "Ariolimax columbianus" -} -item { - name: "144497" - id: 2345 - display_name: "Phalaropus tricolor" -} -item { - name: "39824" - id: 2346 - display_name: "Pseudemys nelsoni" -} -item { - name: "236979" - id: 2347 - display_name: "Colaptes auratus auratus" -} -item { - name: "55990" - id: 2348 - display_name: "Podarcis muralis" -} -item { - name: "244407" - id: 2349 - display_name: "Zelus renardii" -} -item { - name: "47802" - id: 2350 - display_name: "Lymantria dispar" -} -item { - name: "15035" - id: 2351 - display_name: "Melanotis caerulescens" -} -item { - name: "51658" - id: 2352 - display_name: "Anthopleura artemisia" -} -item { - name: "121534" - id: 2353 - display_name: "Oreta rosea" -} -item { - name: "73504" - id: 2354 - display_name: "Tiaris olivaceus" -} -item { - name: "15045" - id: 2355 - display_name: "Oreoscoptes montanus" -} -item { - name: "3873" - id: 2356 - display_name: "Limnodromus scolopaceus" -} -item { - name: "47673" - id: 2357 - display_name: "Pycnopodia helianthoides" -} -item { - name: "47817" - id: 2358 - display_name: "Libellula saturata" -} -item { - name: "56644" - id: 2359 - display_name: "Polygonia satyrus" -} -item { - name: "47826" - id: 2360 - display_name: "Cancer productus" -} -item { - name: "3875" - id: 2361 - display_name: "Tringa solitaria" -} -item { - name: "39782" - id: 2362 - display_name: "Trachemys scripta" -} -item { - name: "143140" - id: 2363 - display_name: "Cyllopsis gemma" -} -item { - name: "29818" - id: 2364 - display_name: "Lampropeltis holbrooki" -} -item { - name: "56293" - id: 2365 - display_name: "Macroglossum stellatarum" -} -item { - name: "154340" - id: 2366 - display_name: "Gryllodes sigillatus" -} -item { - name: "14801" - id: 2367 - display_name: "Sitta carolinensis" -} -item { - name: "121578" - id: 2368 - display_name: "Ovis aries" -} -item { - name: "3879" - id: 2369 - display_name: "Tringa totanus" -} -item { - name: "6893" - id: 2370 - display_name: "Dendrocygna autumnalis" -} -item { - name: "154353" - id: 2371 - display_name: "Sunira bicolorago" -} -item { - name: "6898" - id: 2372 - display_name: "Dendrocygna viduata" -} -item { - name: "6899" - id: 2373 - display_name: "Dendrocygna bicolor" -} -item { - name: "9342" - id: 2374 - display_name: "Icterus abeillei" -} -item { - name: "39670" - id: 2375 - display_name: "Lepidochelys olivacea" -} -item { - name: "4867" - id: 2376 - display_name: "Vanellus chilensis" -} -item { - name: "39677" - id: 2377 - display_name: "Dermochelys coriacea" -} -item { - name: "113407" - id: 2378 - display_name: "Stylurus plagiatus" -} -item { - name: "39682" - id: 2379 - display_name: "Chelydra serpentina" -} -item { - name: "6915" - id: 2380 - display_name: "Cygnus buccinator" -} -item { - name: "6916" - id: 2381 - display_name: "Cygnus cygnus" -} -item { - name: "6917" - id: 2382 - display_name: "Cygnus columbianus" -} -item { - name: "29825" - id: 2383 - display_name: "Lampropeltis calligaster calligaster" -} -item { - name: "6921" - id: 2384 - display_name: "Cygnus olor" -} -item { - name: "146186" - id: 2385 - display_name: "Intellagama lesueurii" -} -item { - name: "9346" - id: 2386 - display_name: "Icterus galbula" -} -item { - name: "126765" - id: 2387 - display_name: "Plutella xylostella" -} -item { - name: "71154" - id: 2388 - display_name: "Aphis nerii" -} -item { - name: "6930" - id: 2389 - display_name: "Anas platyrhynchos" -} -item { - name: "6933" - id: 2390 - display_name: "Anas acuta" -} -item { - name: "39703" - id: 2391 - display_name: "Sternotherus odoratus" -} -item { - name: "6937" - id: 2392 - display_name: "Anas crecca" -} -item { - name: "64287" - id: 2393 - display_name: "Lottia digitalis" -} -item { - name: "6944" - id: 2394 - display_name: "Anas cyanoptera" -} -item { - name: "39713" - id: 2395 - display_name: "Kinosternon subrubrum" -} -item { - name: "26691" - id: 2396 - display_name: "Scaphiopus couchii" -} -item { - name: "6948" - id: 2397 - display_name: "Anas fulvigula" -} -item { - name: "6953" - id: 2398 - display_name: "Anas discors" -} -item { - name: "47914" - id: 2399 - display_name: "Eumorpha pandorus" -} -item { - name: "47916" - id: 2400 - display_name: "Actias luna" -} -item { - name: "6957" - id: 2401 - display_name: "Anas strepera" -} -item { - name: "47919" - id: 2402 - display_name: "Antheraea polyphemus" -} -item { - name: "119953" - id: 2403 - display_name: "Hypoprepia fucosa" -} -item { - name: "6961" - id: 2404 - display_name: "Anas clypeata" -} -item { - name: "134119" - id: 2405 - display_name: "Anisomorpha buprestoides" -} -item { - name: "51678" - id: 2406 - display_name: "Coenagrion puella" -} -item { - name: "72502" - id: 2407 - display_name: "Anas chlorotis" -} -item { - name: "49060" - id: 2408 - display_name: "Epiactis prolifera" -} -item { - name: "42122" - id: 2409 - display_name: "Phacochoerus africanus" -} -item { - name: "58507" - id: 2410 - display_name: "Poanes hobomok" -} -item { - name: "121669" - id: 2411 - display_name: "Stenopus hispidus" -} -item { - name: "8143" - id: 2412 - display_name: "Rhipidura leucophrys" -} -item { - name: "6985" - id: 2413 - display_name: "Anas americana" -} -item { - name: "6993" - id: 2414 - display_name: "Bucephala albeola" -} -item { - name: "121682" - id: 2415 - display_name: "Tetraclita rubescens" -} -item { - name: "6996" - id: 2416 - display_name: "Mergus serrator" -} -item { - name: "113498" - id: 2417 - display_name: "Sympetrum ambiguum" -} -item { - name: "39771" - id: 2418 - display_name: "Chrysemys picta" -} -item { - name: "7004" - id: 2419 - display_name: "Mergus merganser" -} -item { - name: "39773" - id: 2420 - display_name: "Chrysemys picta bellii" -} -item { - name: "113503" - id: 2421 - display_name: "Sympetrum danae" -} -item { - name: "113507" - id: 2422 - display_name: "Sympetrum fonscolombii" -} -item { - name: "154469" - id: 2423 - display_name: "Isa textula" -} -item { - name: "47975" - id: 2424 - display_name: "Argia apicalis" -} -item { - name: "7018" - id: 2425 - display_name: "Anser anser" -} -item { - name: "7019" - id: 2426 - display_name: "Anser albifrons" -} -item { - name: "47980" - id: 2427 - display_name: "Speyeria cybele" -} -item { - name: "58514" - id: 2428 - display_name: "Euphyes vestris" -} -item { - name: "113519" - id: 2429 - display_name: "Sympetrum obtrusum" -} -item { - name: "7024" - id: 2430 - display_name: "Somateria mollissima" -} -item { - name: "39793" - id: 2431 - display_name: "Trachemys scripta scripta" -} -item { - name: "367475" - id: 2432 - display_name: "Rallus obsoletus" -} -item { - name: "121716" - id: 2433 - display_name: "Uresiphita reversalis" -} -item { - name: "113525" - id: 2434 - display_name: "Sympetrum sanguineum" -} -item { - name: "113526" - id: 2435 - display_name: "Sympetrum semicinctum" -} -item { - name: "18921" - id: 2436 - display_name: "Platycercus elegans" -} -item { - name: "7032" - id: 2437 - display_name: "Melanitta fusca" -} -item { - name: "5268" - id: 2438 - display_name: "Milvus migrans" -} -item { - name: "144536" - id: 2439 - display_name: "Gelochelidon nilotica" -} -item { - name: "413503" - id: 2440 - display_name: "Ninox novaeseelandiae novaeseelandiae" -} -item { - name: "7036" - id: 2441 - display_name: "Melanitta perspicillata" -} -item { - name: "64382" - id: 2442 - display_name: "Lissotriton vulgaris" -} -item { - name: "39807" - id: 2443 - display_name: "Terrapene ornata" -} -item { - name: "39808" - id: 2444 - display_name: "Terrapene ornata luteola" -} -item { - name: "7044" - id: 2445 - display_name: "Aythya collaris" -} -item { - name: "7045" - id: 2446 - display_name: "Aythya ferina" -} -item { - name: "7046" - id: 2447 - display_name: "Aythya fuligula" -} -item { - name: "146314" - id: 2448 - display_name: "Opheodrys vernalis" -} -item { - name: "3906" - id: 2449 - display_name: "Numenius americanus" -} -item { - name: "39823" - id: 2450 - display_name: "Pseudemys gorzugi" -} -item { - name: "178991" - id: 2451 - display_name: "Sypharochiton pelliserpentis" -} -item { - name: "7061" - id: 2452 - display_name: "Chen caerulescens" -} -item { - name: "39830" - id: 2453 - display_name: "Pseudemys concinna" -} -item { - name: "127490" - id: 2454 - display_name: "Parrhasius m-album" -} -item { - name: "15256" - id: 2455 - display_name: "Chamaea fasciata" -} -item { - name: "39836" - id: 2456 - display_name: "Malaclemys terrapin" -} -item { - name: "133764" - id: 2457 - display_name: "Trichopoda pennipes" -} -item { - name: "334753" - id: 2458 - display_name: "Hypselonotus punctiventris" -} -item { - name: "58611" - id: 2459 - display_name: "Amia calva" -} -item { - name: "56240" - id: 2460 - display_name: "Argia vivida" -} -item { - name: "7089" - id: 2461 - display_name: "Branta canadensis" -} -item { - name: "146354" - id: 2462 - display_name: "Phrynosoma blainvillii" -} -item { - name: "56243" - id: 2463 - display_name: "Plebejus acmon" -} -item { - name: "144542" - id: 2464 - display_name: "Thalasseus elegans" -} -item { - name: "121783" - id: 2465 - display_name: "Lithobates clamitans melanota" -} -item { - name: "39865" - id: 2466 - display_name: "Glyptemys insculpta" -} -item { - name: "39867" - id: 2467 - display_name: "Emys orbicularis" -} -item { - name: "7104" - id: 2468 - display_name: "Branta sandvicensis" -} -item { - name: "50336" - id: 2469 - display_name: "Siproeta stelenes" -} -item { - name: "7056" - id: 2470 - display_name: "Aythya americana" -} -item { - name: "7107" - id: 2471 - display_name: "Aix sponsa" -} -item { - name: "7109" - id: 2472 - display_name: "Lophodytes cucullatus" -} -item { - name: "7111" - id: 2473 - display_name: "Histrionicus histrionicus" -} -item { - name: "367562" - id: 2474 - display_name: "Aratinga nenday" -} -item { - name: "39885" - id: 2475 - display_name: "Emydoidea blandingii" -} -item { - name: "367566" - id: 2476 - display_name: "Psittacara holochlorus" -} -item { - name: "143181" - id: 2477 - display_name: "Marimatha nigrofimbria" -} -item { - name: "7120" - id: 2478 - display_name: "Cairina moschata" -} -item { - name: "7122" - id: 2479 - display_name: "Netta rufina" -} -item { - name: "130003" - id: 2480 - display_name: "Phaeoura quernaria" -} -item { - name: "367572" - id: 2481 - display_name: "Psittacara erythrogenys" -} -item { - name: "17009" - id: 2482 - display_name: "Sayornis saya" -} -item { - name: "154582" - id: 2483 - display_name: "Ennomos magnaria" -} -item { - name: "58532" - id: 2484 - display_name: "Colias eurytheme" -} -item { - name: "121821" - id: 2485 - display_name: "Sceliphron caementarium" -} -item { - name: "48094" - id: 2486 - display_name: "Dryocampa rubicunda" -} -item { - name: "7057" - id: 2487 - display_name: "Aythya valisineria" -} -item { - name: "17646" - id: 2488 - display_name: "Picoides albolarvatus" -} -item { - name: "201551" - id: 2489 - display_name: "Procyon lotor lotor" -} -item { - name: "58534" - id: 2490 - display_name: "Lycaena hyllus" -} -item { - name: "73553" - id: 2491 - display_name: "Vermivora cyanoptera" -} -item { - name: "359401" - id: 2492 - display_name: "Exomala orientalis" -} -item { - name: "8018" - id: 2493 - display_name: "Corvus caurinus" -} -item { - name: "490478" - id: 2494 - display_name: "Tegula brunnea" -} -item { - name: "20307" - id: 2495 - display_name: "Asio otus" -} -item { - name: "227466" - id: 2496 - display_name: "Peridea ferruginea" -} -item { - name: "122172" - id: 2497 - display_name: "Pyrisitia lisa" -} -item { - name: "133631" - id: 2498 - display_name: "Polites peckius" -} -item { - name: "8021" - id: 2499 - display_name: "Corvus brachyrhynchos" -} -item { - name: "7170" - id: 2500 - display_name: "Clangula hyemalis" -} -item { - name: "58539" - id: 2501 - display_name: "Satyrium calanus" -} -item { - name: "27137" - id: 2502 - display_name: "Coluber constrictor" -} -item { - name: "7176" - id: 2503 - display_name: "Chenonetta jubata" -} -item { - name: "42157" - id: 2504 - display_name: "Giraffa camelopardalis" -} -item { - name: "144541" - id: 2505 - display_name: "Thalasseus sandvicensis" -} -item { - name: "23572" - id: 2506 - display_name: "Litoria aurea" -} -item { - name: "354820" - id: 2507 - display_name: "Patiriella regularis" -} -item { - name: "55887" - id: 2508 - display_name: "Andricus quercuscalifornicus" -} -item { - name: "46255" - id: 2509 - display_name: "Ammospermophilus leucurus" -} -item { - name: "334341" - id: 2510 - display_name: "Oryctolagus cuniculus domesticus" -} -item { - name: "144560" - id: 2511 - display_name: "Eolophus roseicapilla" -} -item { - name: "94043" - id: 2512 - display_name: "Anax imperator" -} -item { - name: "425004" - id: 2513 - display_name: "Dryas iulia moderata" -} -item { - name: "269359" - id: 2514 - display_name: "Cactophagus spinolae" -} -item { - name: "72755" - id: 2515 - display_name: "Colaptes rubiginosus" -} -item { - name: "319123" - id: 2516 - display_name: "Meleagris gallopavo silvestris" -} -item { - name: "130846" - id: 2517 - display_name: "Lyssa zampa" -} -item { - name: "203831" - id: 2518 - display_name: "Nemoria bistriaria" -} -item { - name: "367678" - id: 2519 - display_name: "Ptiliogonys cinereus" -} -item { - name: "5301" - id: 2520 - display_name: "Elanoides forficatus" -} -item { - name: "9398" - id: 2521 - display_name: "Carduelis carduelis" -} -item { - name: "143201" - id: 2522 - display_name: "Coryphista meadii" -} -item { - name: "104419" - id: 2523 - display_name: "Lestes australis" -} -item { - name: "367693" - id: 2524 - display_name: "Cassiculus melanicterus" -} -item { - name: "143452" - id: 2525 - display_name: "Deidamia inscriptum" -} -item { - name: "466003" - id: 2526 - display_name: "Romalea microptera" -} -item { - name: "84494" - id: 2527 - display_name: "Paraphidippus aurantius" -} -item { - name: "203866" - id: 2528 - display_name: "Rabdophaga strobiloides" -} -item { - name: "72797" - id: 2529 - display_name: "Dendragapus fuliginosus" -} -item { - name: "7266" - id: 2530 - display_name: "Psaltriparus minimus" -} -item { - name: "120920" - id: 2531 - display_name: "Odocoileus virginianus clavium" -} -item { - name: "7278" - id: 2532 - display_name: "Aegithalos caudatus" -} -item { - name: "30681" - id: 2533 - display_name: "Agkistrodon contortrix mokasen" -} -item { - name: "413547" - id: 2534 - display_name: "Zosterops lateralis lateralis" -} -item { - name: "48262" - id: 2535 - display_name: "Apatelodes torrefacta" -} -item { - name: "121993" - id: 2536 - display_name: "Lampides boeticus" -} -item { - name: "48267" - id: 2537 - display_name: "Crotalus oreganus oreganus" -} -item { - name: "48268" - id: 2538 - display_name: "Crotalus oreganus" -} -item { - name: "147309" - id: 2539 - display_name: "Feltia herilis" -} -item { - name: "146413" - id: 2540 - display_name: "Sceloporus consobrinus" -} -item { - name: "326764" - id: 2541 - display_name: "Cyprinus carpio haematopterus" -} -item { - name: "5315" - id: 2542 - display_name: "Haliaeetus leucogaster" -} -item { - name: "4519" - id: 2543 - display_name: "Uria aalge" -} -item { - name: "40085" - id: 2544 - display_name: "Gopherus polyphemus" -} -item { - name: "23702" - id: 2545 - display_name: "Agalychnis callidryas" -} -item { - name: "210116" - id: 2546 - display_name: "Tringa semipalmata inornatus" -} -item { - name: "40092" - id: 2547 - display_name: "Stigmochelys pardalis" -} -item { - name: "59931" - id: 2548 - display_name: "Acanthurus triostegus" -} -item { - name: "48292" - id: 2549 - display_name: "Philoscia muscorum" -} -item { - name: "146601" - id: 2550 - display_name: "Scolopendra heros" -} -item { - name: "244906" - id: 2551 - display_name: "Panchlora nivea" -} -item { - name: "48302" - id: 2552 - display_name: "Limulus polyphemus" -} -item { - name: "180008" - id: 2553 - display_name: "Otospermophilus variegatus" -} -item { - name: "7347" - id: 2554 - display_name: "Alauda arvensis" -} -item { - name: "43459" - id: 2555 - display_name: "Macaca fascicularis" -} -item { - name: "113846" - id: 2556 - display_name: "Telebasis salva" -} -item { - name: "7356" - id: 2557 - display_name: "Galerida cristata" -} -item { - name: "64705" - id: 2558 - display_name: "Delichon urbicum" -} -item { - name: "145932" - id: 2559 - display_name: "Aspidoscelis hyperythra beldingi" -} -item { - name: "72912" - id: 2560 - display_name: "Helmitheros vermivorum" -} -item { - name: "69805" - id: 2561 - display_name: "Octogomphus specularis" -} -item { - name: "129572" - id: 2562 - display_name: "Aphomia sociella" -} -item { - name: "31964" - id: 2563 - display_name: "Barisia imbricata" -} -item { - name: "244625" - id: 2564 - display_name: "Halmus chalybeus" -} -item { - name: "58576" - id: 2565 - display_name: "Phyciodes cocyta" -} -item { - name: "72931" - id: 2566 - display_name: "Hylocharis leucotis" -} -item { - name: "104449" - id: 2567 - display_name: "Lestes rectangularis" -} -item { - name: "14886" - id: 2568 - display_name: "Mimus polyglottos" -} -item { - name: "23783" - id: 2569 - display_name: "Hyla versicolor" -} -item { - name: "23784" - id: 2570 - display_name: "Hyla plicata" -} -item { - name: "8575" - id: 2571 - display_name: "Gymnorhina tibicen" -} -item { - name: "2599" - id: 2572 - display_name: "Alcedo atthis" -} -item { - name: "61152" - id: 2573 - display_name: "Pyrrhosoma nymphula" -} -item { - name: "58579" - id: 2574 - display_name: "Polygonia interrogationis" -} -item { - name: "31993" - id: 2575 - display_name: "Ophisaurus attenuatus attenuatus" -} -item { - name: "53985" - id: 2576 - display_name: "Odocoileus hemionus californicus" -} -item { - name: "144549" - id: 2577 - display_name: "Streptopelia chinensis" -} -item { - name: "105730" - id: 2578 - display_name: "Micrathyria hagenii" -} -item { - name: "7428" - id: 2579 - display_name: "Bombycilla cedrorum" -} -item { - name: "7429" - id: 2580 - display_name: "Bombycilla garrulus" -} -item { - name: "50391" - id: 2581 - display_name: "Polygonia gracilis" -} -item { - name: "7067" - id: 2582 - display_name: "Tadorna tadorna" -} -item { - name: "413513" - id: 2583 - display_name: "Petroica australis australis" -} -item { - name: "39469" - id: 2584 - display_name: "Varanus varius" -} -item { - name: "58479" - id: 2585 - display_name: "Pholisora catullus" -} -item { - name: "127929" - id: 2586 - display_name: "Achalarus lyciades" -} -item { - name: "48403" - id: 2587 - display_name: "Gasterosteus aculeatus" -} -item { - name: "18990" - id: 2588 - display_name: "Amazona autumnalis" -} -item { - name: "1241" - id: 2589 - display_name: "Dendragapus obscurus" -} -item { - name: "228634" - id: 2590 - display_name: "Ponometia erastrioides" -} -item { - name: "64806" - id: 2591 - display_name: "Pelophylax" -} -item { - name: "51761" - id: 2592 - display_name: "Hetaerina americana" -} -item { - name: "7464" - id: 2593 - display_name: "Catherpes mexicanus" -} -item { - name: "318761" - id: 2594 - display_name: "Sceloporus uniformis" -} -item { - name: "7068" - id: 2595 - display_name: "Tadorna ferruginea" -} -item { - name: "204077" - id: 2596 - display_name: "Achyra rantalis" -} -item { - name: "7470" - id: 2597 - display_name: "Campylorhynchus brunneicapillus" -} -item { - name: "32048" - id: 2598 - display_name: "Gerrhonotus infernalis" -} -item { - name: "204081" - id: 2599 - display_name: "Pyrausta laticlavia" -} -item { - name: "7476" - id: 2600 - display_name: "Campylorhynchus rufinucha" -} -item { - name: "32055" - id: 2601 - display_name: "Elgaria multicarinata" -} -item { - name: "244276" - id: 2602 - display_name: "Rhipidura fuliginosa" -} -item { - name: "144187" - id: 2603 - display_name: "Pyrisitia proterpia" -} -item { - name: "32059" - id: 2604 - display_name: "Elgaria multicarinata multicarinata" -} -item { - name: "32061" - id: 2605 - display_name: "Elgaria kingii" -} -item { - name: "146750" - id: 2606 - display_name: "Lascoria ambigualis" -} -item { - name: "32064" - id: 2607 - display_name: "Elgaria coerulea" -} -item { - name: "23873" - id: 2608 - display_name: "Hyla squirella" -} -item { - name: "48450" - id: 2609 - display_name: "Peltodoris nobilis" -} -item { - name: "64146" - id: 2610 - display_name: "Fissurella volcano" -} -item { - name: "48259" - id: 2611 - display_name: "Pelidnota punctata" -} -item { - name: "122185" - id: 2612 - display_name: "Pantherophis alleghaniensis quadrivittata" -} -item { - name: "7498" - id: 2613 - display_name: "Polioptila melanura" -} -item { - name: "56652" - id: 2614 - display_name: "Haliotis rufescens" -} -item { - name: "122191" - id: 2615 - display_name: "Pelecanus occidentalis carolinensis" -} -item { - name: "73041" - id: 2616 - display_name: "Melozone aberti" -} -item { - name: "199381" - id: 2617 - display_name: "Homalodisca vitripennis" -} -item { - name: "73044" - id: 2618 - display_name: "Melozone crissalis" -} -item { - name: "83290" - id: 2619 - display_name: "Zanclus cornutus" -} -item { - name: "7513" - id: 2620 - display_name: "Thryothorus ludovicianus" -} -item { - name: "28559" - id: 2621 - display_name: "Storeria occipitomaculata occipitomaculata" -} -item { - name: "24255" - id: 2622 - display_name: "Pseudacris maculata" -} -item { - name: "130398" - id: 2623 - display_name: "Melanargia galathea" -} -item { - name: "29925" - id: 2624 - display_name: "Heterodon platirhinos" -} -item { - name: "48484" - id: 2625 - display_name: "Harmonia axyridis" -} -item { - name: "122214" - id: 2626 - display_name: "Odontotaenius disjunctus" -} -item { - name: "39484" - id: 2627 - display_name: "Xantusia vigilis" -} -item { - name: "73919" - id: 2628 - display_name: "Podarcis sicula" -} -item { - name: "154553" - id: 2629 - display_name: "Leptoglossus clypealis" -} -item { - name: "23922" - id: 2630 - display_name: "Hyla intermedia" -} -item { - name: "122228" - id: 2631 - display_name: "Acharia stimulea" -} -item { - name: "108344" - id: 2632 - display_name: "Pantala flavescens" -} -item { - name: "118538" - id: 2633 - display_name: "Cotinis nitida" -} -item { - name: "23930" - id: 2634 - display_name: "Hyla chrysoscelis" -} -item { - name: "23933" - id: 2635 - display_name: "Hyla arenicolor" -} -item { - name: "122238" - id: 2636 - display_name: "Porcellio scaber" -} -item { - name: "479803" - id: 2637 - display_name: "Dioprosopa clavata" -} -item { - name: "5355" - id: 2638 - display_name: "Parabuteo unicinctus" -} -item { - name: "146822" - id: 2639 - display_name: "Texola elada" -} -item { - name: "236935" - id: 2640 - display_name: "Anas platyrhynchos domesticus" -} -item { - name: "7562" - id: 2641 - display_name: "Troglodytes aedon" -} -item { - name: "339444" - id: 2642 - display_name: "Buteo lineatus elegans" -} -item { - name: "42221" - id: 2643 - display_name: "Odocoileus hemionus columbianus" -} -item { - name: "15764" - id: 2644 - display_name: "Thamnophilus doliatus" -} -item { - name: "122261" - id: 2645 - display_name: "Cucullia convexipennis" -} -item { - name: "122262" - id: 2646 - display_name: "Brachystola magna" -} -item { - name: "7576" - id: 2647 - display_name: "Thryomanes bewickii" -} -item { - name: "143015" - id: 2648 - display_name: "Eubaphe mendica" -} -item { - name: "73592" - id: 2649 - display_name: "Actinemys marmorata" -} -item { - name: "84549" - id: 2650 - display_name: "Plathemis lydia" -} -item { - name: "23969" - id: 2651 - display_name: "Hyla cinerea" -} -item { - name: "318882" - id: 2652 - display_name: "Ancistrocerus gazella" -} -item { - name: "7072" - id: 2653 - display_name: "Tadorna variegata" -} -item { - name: "48548" - id: 2654 - display_name: "Vanessa cardui" -} -item { - name: "48549" - id: 2655 - display_name: "Vanessa virginiensis" -} -item { - name: "122278" - id: 2656 - display_name: "Pomacea canaliculata" -} -item { - name: "9457" - id: 2657 - display_name: "Myioborus miniatus" -} -item { - name: "122280" - id: 2658 - display_name: "Pyrgus albescens" -} -item { - name: "122281" - id: 2659 - display_name: "Calycopis cecrops" -} -item { - name: "130474" - id: 2660 - display_name: "Achlyodes pallida" -} -item { - name: "338503" - id: 2661 - display_name: "Phalacrocorax varius varius" -} -item { - name: "9458" - id: 2662 - display_name: "Myioborus pictus" -} -item { - name: "73629" - id: 2663 - display_name: "Anolis nebulosus" -} -item { - name: "122291" - id: 2664 - display_name: "Larus argentatus smithsonianus" -} -item { - name: "56756" - id: 2665 - display_name: "Murgantia histrionica" -} -item { - name: "73148" - id: 2666 - display_name: "Parkesia motacilla" -} -item { - name: "48575" - id: 2667 - display_name: "Okenia rosacea" -} -item { - name: "56768" - id: 2668 - display_name: "Sula granti" -} -item { - name: "48578" - id: 2669 - display_name: "Anteos maerula" -} -item { - name: "64968" - id: 2670 - display_name: "Anaxyrus americanus" -} -item { - name: "64970" - id: 2671 - display_name: "Anaxyrus boreas" -} -item { - name: "115549" - id: 2672 - display_name: "Crotalus lepidus lepidus" -} -item { - name: "64977" - id: 2673 - display_name: "Anaxyrus fowleri" -} -item { - name: "19022" - id: 2674 - display_name: "Ara macao" -} -item { - name: "24259" - id: 2675 - display_name: "Pseudacris regilla" -} -item { - name: "64984" - id: 2676 - display_name: "Anaxyrus punctatus" -} -item { - name: "64985" - id: 2677 - display_name: "Anaxyrus quercicus" -} -item { - name: "73178" - id: 2678 - display_name: "Peucaea ruficauda" -} -item { - name: "64987" - id: 2679 - display_name: "Anaxyrus speciosus" -} -item { - name: "64989" - id: 2680 - display_name: "Anaxyrus woodhousii" -} -item { - name: "339596" - id: 2681 - display_name: "Calidris subruficollis" -} -item { - name: "56552" - id: 2682 - display_name: "Carabus nemoralis" -} -item { - name: "84722" - id: 2683 - display_name: "Ischnura verticalis" -} -item { - name: "122356" - id: 2684 - display_name: "Eumorpha achemon" -} -item { - name: "318965" - id: 2685 - display_name: "Chrysolina bankii" -} -item { - name: "228855" - id: 2686 - display_name: "Protodeltote muscosula" -} -item { - name: "146940" - id: 2687 - display_name: "Agriphila vulgivagella" -} -item { - name: "56832" - id: 2688 - display_name: "Nymphalis antiopa" -} -item { - name: "61355" - id: 2689 - display_name: "Vespula pensylvanica" -} -item { - name: "48645" - id: 2690 - display_name: "Megathura crenulata" -} -item { - name: "73222" - id: 2691 - display_name: "Phoenicopterus roseus" -} -item { - name: "363354" - id: 2692 - display_name: "Lobatus gigas" -} -item { - name: "3802" - id: 2693 - display_name: "Morus bassanus" -} -item { - name: "62722" - id: 2694 - display_name: "Apalone spinifera spinifera" -} -item { - name: "48655" - id: 2695 - display_name: "Aplysia californica" -} -item { - name: "54468" - id: 2696 - display_name: "Aglais urticae" -} -item { - name: "48662" - id: 2697 - display_name: "Danaus plexippus" -} -item { - name: "49071" - id: 2698 - display_name: "Metridium senile" -} -item { - name: "228899" - id: 2699 - display_name: "Psamatodes abydata" -} -item { - name: "133102" - id: 2700 - display_name: "Oncometopia orbona" -} -item { - name: "39659" - id: 2701 - display_name: "Chelonia mydas" -} -item { - name: "121437" - id: 2702 - display_name: "Dolomedes triton" -} -item { - name: "94545" - id: 2703 - display_name: "Argia fumipennis" -} -item { - name: "56887" - id: 2704 - display_name: "Bombus pensylvanicus" -} -item { - name: "40509" - id: 2705 - display_name: "Eptesicus fuscus" -} -item { - name: "58635" - id: 2706 - display_name: "Lepomis megalotis" -} -item { - name: "100369" - id: 2707 - display_name: "Erpetogomphus designatus" -} -item { - name: "58636" - id: 2708 - display_name: "Lepomis cyanellus" -} -item { - name: "40522" - id: 2709 - display_name: "Lasiurus borealis" -} -item { - name: "102006" - id: 2710 - display_name: "Hagenius brevistylus" -} -item { - name: "50283" - id: 2711 - display_name: "Marpesia petreus" -} -item { - name: "123829" - id: 2712 - display_name: "Pelecanus occidentalis californicus" -} -item { - name: "62453" - id: 2713 - display_name: "Anthidium manicatum" -} -item { - name: "56925" - id: 2714 - display_name: "Graphocephala coccinea" -} -item { - name: "48738" - id: 2715 - display_name: "Sphex pensylvanicus" -} -item { - name: "43151" - id: 2716 - display_name: "Oryctolagus cuniculus" -} -item { - name: "19822" - id: 2717 - display_name: "Glaucidium brasilianum" -} -item { - name: "48750" - id: 2718 - display_name: "Lottia scabra" -} -item { - name: "335071" - id: 2719 - display_name: "Elophila obliteralis" -} -item { - name: "81521" - id: 2720 - display_name: "Vipera berus" -} -item { - name: "43697" - id: 2721 - display_name: "Elephas maximus" -} -item { - name: "7079" - id: 2722 - display_name: "Oxyura jamaicensis" -} -item { - name: "43042" - id: 2723 - display_name: "Erinaceus europaeus" -} -item { - name: "40086" - id: 2724 - display_name: "Gopherus agassizii" -} -item { - name: "81545" - id: 2725 - display_name: "Lumbricus terrestris" -} -item { - name: "16010" - id: 2726 - display_name: "Myiarchus cinerascens" -} -item { - name: "2669" - id: 2727 - display_name: "Chloroceryle americana" -} -item { - name: "9535" - id: 2728 - display_name: "Sturnella neglecta" -} -item { - name: "81554" - id: 2729 - display_name: "Ictalurus punctatus" -} -item { - name: "339907" - id: 2730 - display_name: "Ramphastos ambiguus" -} -item { - name: "39814" - id: 2731 - display_name: "Terrapene carolina" -} -item { - name: "10254" - id: 2732 - display_name: "Paroaria coronata" -} -item { - name: "40614" - id: 2733 - display_name: "Antrozous pallidus" -} -item { - name: "502385" - id: 2734 - display_name: "Probole amicaria" -} -item { - name: "24233" - id: 2735 - display_name: "Acris gryllus" -} -item { - name: "81579" - id: 2736 - display_name: "Steatoda triangulosa" -} -item { - name: "81580" - id: 2737 - display_name: "Callosamia promethea" -} -item { - name: "146034" - id: 2738 - display_name: "Coluber lateralis" -} -item { - name: "81582" - id: 2739 - display_name: "Hyalophora cecropia" -} -item { - name: "81583" - id: 2740 - display_name: "Anisota senatoria" -} -item { - name: "66002" - id: 2741 - display_name: "Lithobates palustris" -} -item { - name: "81586" - id: 2742 - display_name: "Citheronia regalis" -} -item { - name: "40629" - id: 2743 - display_name: "Lasionycteris noctivagans" -} -item { - name: "81590" - id: 2744 - display_name: "Eacles imperialis" -} -item { - name: "204472" - id: 2745 - display_name: "Buteo buteo" -} -item { - name: "65212" - id: 2746 - display_name: "Craugastor augusti" -} -item { - name: "48830" - id: 2747 - display_name: "Patiria miniata" -} -item { - name: "48833" - id: 2748 - display_name: "Pisaster giganteus" -} -item { - name: "16071" - id: 2749 - display_name: "Myiodynastes luteiventris" -} -item { - name: "81610" - id: 2750 - display_name: "Balanus glandula" -} -item { - name: "24268" - id: 2751 - display_name: "Pseudacris crucifer" -} -item { - name: "16079" - id: 2752 - display_name: "Contopus sordidulus" -} -item { - name: "204496" - id: 2753 - display_name: "Corvus corone" -} -item { - name: "204498" - id: 2754 - display_name: "Cyanoramphus novaezelandiae" -} -item { - name: "24277" - id: 2755 - display_name: "Smilisca baudinii" -} -item { - name: "22631" - id: 2756 - display_name: "Eleutherodactylus planirostris" -} -item { - name: "16100" - id: 2757 - display_name: "Contopus virens" -} -item { - name: "42278" - id: 2758 - display_name: "Aepyceros melampus" -} -item { - name: "16106" - id: 2759 - display_name: "Contopus pertinax" -} -item { - name: "16110" - id: 2760 - display_name: "Contopus cooperi" -} -item { - name: "42280" - id: 2761 - display_name: "Connochaetes taurinus" -} -item { - name: "47455" - id: 2762 - display_name: "Octopus rubescens" -} -item { - name: "204533" - id: 2763 - display_name: "Larus argentatus" -} -item { - name: "81656" - id: 2764 - display_name: "Nematocampa resistaria" -} -item { - name: "81657" - id: 2765 - display_name: "Lacinipolia renigera" -} -item { - name: "204519" - id: 2766 - display_name: "Halcyon smyrnensis" -} -item { - name: "62762" - id: 2767 - display_name: "Cordulegaster dorsalis" -} -item { - name: "81663" - id: 2768 - display_name: "Malacosoma disstria" -} -item { - name: "32512" - id: 2769 - display_name: "Rena dulcis" -} -item { - name: "81665" - id: 2770 - display_name: "Orgyia leucostigma" -} -item { - name: "130821" - id: 2771 - display_name: "Haploa confusa" -} -item { - name: "81672" - id: 2772 - display_name: "Clemensia albata" -} -item { - name: "204554" - id: 2773 - display_name: "Onychognathus morio" -} -item { - name: "81677" - id: 2774 - display_name: "Euchaetes egle" -} -item { - name: "81680" - id: 2775 - display_name: "Scopula limboundata" -} -item { - name: "318497" - id: 2776 - display_name: "Hemipenthes sinuosa" -} -item { - name: "179987" - id: 2777 - display_name: "Ictidomys parvidens" -} -item { - name: "179988" - id: 2778 - display_name: "Ictidomys tridecemlineatus" -} -item { - name: "81685" - id: 2779 - display_name: "Evergestis pallidata" -} -item { - name: "81687" - id: 2780 - display_name: "Noctua pronuba" -} -item { - name: "179992" - id: 2781 - display_name: "Xerospermophilus spilosoma" -} -item { - name: "179994" - id: 2782 - display_name: "Urocitellus armatus" -} -item { - name: "9519" - id: 2783 - display_name: "Cyanocompsa parellina" -} -item { - name: "179998" - id: 2784 - display_name: "Urocitellus columbianus" -} -item { - name: "114463" - id: 2785 - display_name: "Trithemis annulata" -} -item { - name: "199169" - id: 2786 - display_name: "Catocala maestosa" -} -item { - name: "143323" - id: 2787 - display_name: "Tolype velleda" -} -item { - name: "120113" - id: 2788 - display_name: "Anthrenus verbasci" -} -item { - name: "7601" - id: 2789 - display_name: "Cistothorus palustris" -} -item { - name: "81706" - id: 2790 - display_name: "Alaus oculatus" -} -item { - name: "220974" - id: 2791 - display_name: "Harrisimemna trisignata" -} -item { - name: "20445" - id: 2792 - display_name: "Tyto alba" -} -item { - name: "73523" - id: 2793 - display_name: "Trogon caligatus" -} -item { - name: "49590" - id: 2794 - display_name: "Micropterus dolomieu" -} -item { - name: "41729" - id: 2795 - display_name: "Mirounga leonina" -} -item { - name: "48957" - id: 2796 - display_name: "Arilus cristatus" -} -item { - name: "81727" - id: 2797 - display_name: "Abaeis nicippe" -} -item { - name: "8000" - id: 2798 - display_name: "Corvus monedula" -} -item { - name: "8001" - id: 2799 - display_name: "Corvus ossifragus" -} -item { - name: "171843" - id: 2800 - display_name: "Rabdotus dealbatus" -} -item { - name: "81734" - id: 2801 - display_name: "Neophasia menapia" -} -item { - name: "258813" - id: 2802 - display_name: "Clogmia albipunctata" -} -item { - name: "332243" - id: 2803 - display_name: "Lepturobosca chrysocoma" -} -item { - name: "81744" - id: 2804 - display_name: "Heliconius erato" -} -item { - name: "218424" - id: 2805 - display_name: "Dicymolomia julianalis" -} -item { - name: "3813" - id: 2806 - display_name: "Spheniscus demersus" -} -item { - name: "81749" - id: 2807 - display_name: "Malacosoma americanum" -} -item { - name: "81752" - id: 2808 - display_name: "Pyrausta tyralis" -} -item { - name: "48987" - id: 2809 - display_name: "Hippodamia convergens" -} -item { - name: "8029" - id: 2810 - display_name: "Corvus frugilegus" -} -item { - name: "8031" - id: 2811 - display_name: "Corvus splendens" -} -item { - name: "147298" - id: 2812 - display_name: "Lasiommata megera" -} -item { - name: "7087" - id: 2813 - display_name: "Branta bernicla" -} -item { - name: "48550" - id: 2814 - display_name: "Phoebis sennae" -} -item { - name: "4349" - id: 2815 - display_name: "Larus hyperboreus" -} -item { - name: "84027" - id: 2816 - display_name: "Trigonopeltastes delta" -} -item { - name: "194762" - id: 2817 - display_name: "Vanessa itea" -} -item { - name: "311163" - id: 2818 - display_name: "Pseudomops septentrionalis" -} -item { - name: "55957" - id: 2819 - display_name: "Scudderia furcata" -} -item { - name: "39822" - id: 2820 - display_name: "Pseudemys texana" -} -item { - name: "204685" - id: 2821 - display_name: "Chlosyne ehrenbergii" -} -item { - name: "122767" - id: 2822 - display_name: "Columba livia domestica" -} -item { - name: "55960" - id: 2823 - display_name: "Sceloporus graciosus" -} -item { - name: "121823" - id: 2824 - display_name: "Autographa californica" -} -item { - name: "8088" - id: 2825 - display_name: "Garrulus glandarius" -} -item { - name: "65433" - id: 2826 - display_name: "Ecnomiohyla miotympanum" -} -item { - name: "49051" - id: 2827 - display_name: "Anthopleura sola" -} -item { - name: "125815" - id: 2828 - display_name: "Coenonympha arcania" -} -item { - name: "55963" - id: 2829 - display_name: "Malacosoma californicum" -} -item { - name: "120479" - id: 2830 - display_name: "Anser anser domesticus" -} -item { - name: "133788" - id: 2831 - display_name: "Xylocopa micans" -} -item { - name: "81559" - id: 2832 - display_name: "Epargyreus clarus" -} -item { - name: "81839" - id: 2833 - display_name: "Platycryptus undatus" -} -item { - name: "133791" - id: 2834 - display_name: "Polistes exclamans" -} -item { - name: "84640" - id: 2835 - display_name: "Polistes dominula" -} -item { - name: "73666" - id: 2836 - display_name: "Aspidoscelis exsanguis" -} -item { - name: "73669" - id: 2837 - display_name: "Aspidoscelis gularis" -} -item { - name: "16326" - id: 2838 - display_name: "Mitrephanes phaeocercus" -} -item { - name: "49095" - id: 2839 - display_name: "Pagurus samuelis" -} -item { - name: "73672" - id: 2840 - display_name: "Aspidoscelis hyperythra" -} -item { - name: "59192" - id: 2841 - display_name: "Polites sabuleti" -} -item { - name: "81561" - id: 2842 - display_name: "Anaea andria" -} -item { - name: "81881" - id: 2843 - display_name: "Amphipsalta zelandica" -} -item { - name: "73690" - id: 2844 - display_name: "Aspidoscelis sexlineata" -} -item { - name: "73694" - id: 2845 - display_name: "Aspidoscelis velox" -} -item { - name: "335840" - id: 2846 - display_name: "Pyrausta inornatalis" -} -item { - name: "49126" - id: 2847 - display_name: "Strongylocentrotus franciscanus" -} -item { - name: "204775" - id: 2848 - display_name: "Kricogonia lyside" -} -item { - name: "475115" - id: 2849 - display_name: "Ardenna creatopus" -} -item { - name: "475120" - id: 2850 - display_name: "Ardenna gravis" -} -item { - name: "62803" - id: 2851 - display_name: "Monadenia fidelis" -} -item { - name: "49150" - id: 2852 - display_name: "Agraulis vanillae" -} -item { - name: "83929" - id: 2853 - display_name: "Phanaeus vindex" -} -item { - name: "199839" - id: 2854 - display_name: "Haemorhous cassinii" -} diff --git a/research/object_detection/data/kitti_label_map.pbtxt b/research/object_detection/data/kitti_label_map.pbtxt deleted file mode 100644 index 0afcc6936eb..00000000000 --- a/research/object_detection/data/kitti_label_map.pbtxt +++ /dev/null @@ -1,9 +0,0 @@ -item { - id: 1 - name: 'car' -} - -item { - id: 2 - name: 'pedestrian' -} diff --git a/research/object_detection/data/mscoco_complete_label_map.pbtxt b/research/object_detection/data/mscoco_complete_label_map.pbtxt deleted file mode 100644 index d73fc065a4b..00000000000 --- a/research/object_detection/data/mscoco_complete_label_map.pbtxt +++ /dev/null @@ -1,455 +0,0 @@ -item { - name: "background" - id: 0 - display_name: "background" -} -item { - name: "/m/01g317" - id: 1 - display_name: "person" -} -item { - name: "/m/0199g" - id: 2 - display_name: "bicycle" -} -item { - name: "/m/0k4j" - id: 3 - display_name: "car" -} -item { - name: "/m/04_sv" - id: 4 - display_name: "motorcycle" -} -item { - name: "/m/05czz6l" - id: 5 - display_name: "airplane" -} -item { - name: "/m/01bjv" - id: 6 - display_name: "bus" -} -item { - name: "/m/07jdr" - id: 7 - display_name: "train" -} -item { - name: "/m/07r04" - id: 8 - display_name: "truck" -} -item { - name: "/m/019jd" - id: 9 - display_name: "boat" -} -item { - name: "/m/015qff" - id: 10 - display_name: "traffic light" -} -item { - name: "/m/01pns0" - id: 11 - display_name: "fire hydrant" -} -item { - name: "12" - id: 12 - display_name: "12" -} -item { - name: "/m/02pv19" - id: 13 - display_name: "stop sign" -} -item { - name: "/m/015qbp" - id: 14 - display_name: "parking meter" -} -item { - name: "/m/0cvnqh" - id: 15 - display_name: "bench" -} -item { - name: "/m/015p6" - id: 16 - display_name: "bird" -} -item { - name: "/m/01yrx" - id: 17 - display_name: "cat" -} -item { - name: "/m/0bt9lr" - id: 18 - display_name: "dog" -} -item { - name: "/m/03k3r" - id: 19 - display_name: "horse" -} -item { - name: "/m/07bgp" - id: 20 - display_name: "sheep" -} -item { - name: "/m/01xq0k1" - id: 21 - display_name: "cow" -} -item { - name: "/m/0bwd_0j" - id: 22 - display_name: "elephant" -} -item { - name: "/m/01dws" - id: 23 - display_name: "bear" -} -item { - name: "/m/0898b" - id: 24 - display_name: "zebra" -} -item { - name: "/m/03bk1" - id: 25 - display_name: "giraffe" -} -item { - name: "26" - id: 26 - display_name: "26" -} -item { - name: "/m/01940j" - id: 27 - display_name: "backpack" -} -item { - name: "/m/0hnnb" - id: 28 - display_name: "umbrella" -} -item { - name: "29" - id: 29 - display_name: "29" -} -item { - name: "30" - id: 30 - display_name: "30" -} -item { - name: "/m/080hkjn" - id: 31 - display_name: "handbag" -} -item { - name: "/m/01rkbr" - id: 32 - display_name: "tie" -} -item { - name: "/m/01s55n" - id: 33 - display_name: "suitcase" -} -item { - name: "/m/02wmf" - id: 34 - display_name: "frisbee" -} -item { - name: "/m/071p9" - id: 35 - display_name: "skis" -} -item { - name: "/m/06__v" - id: 36 - display_name: "snowboard" -} -item { - name: "/m/018xm" - id: 37 - display_name: "sports ball" -} -item { - name: "/m/02zt3" - id: 38 - display_name: "kite" -} -item { - name: "/m/03g8mr" - id: 39 - display_name: "baseball bat" -} -item { - name: "/m/03grzl" - id: 40 - display_name: "baseball glove" -} -item { - name: "/m/06_fw" - id: 41 - display_name: "skateboard" -} -item { - name: "/m/019w40" - id: 42 - display_name: "surfboard" -} -item { - name: "/m/0dv9c" - id: 43 - display_name: "tennis racket" -} -item { - name: "/m/04dr76w" - id: 44 - display_name: "bottle" -} -item { - name: "45" - id: 45 - display_name: "45" -} -item { - name: "/m/09tvcd" - id: 46 - display_name: "wine glass" -} -item { - name: "/m/08gqpm" - id: 47 - display_name: "cup" -} -item { - name: "/m/0dt3t" - id: 48 - display_name: "fork" -} -item { - name: "/m/04ctx" - id: 49 - display_name: "knife" -} -item { - name: "/m/0cmx8" - id: 50 - display_name: "spoon" -} -item { - name: "/m/04kkgm" - id: 51 - display_name: "bowl" -} -item { - name: "/m/09qck" - id: 52 - display_name: "banana" -} -item { - name: "/m/014j1m" - id: 53 - display_name: "apple" -} -item { - name: "/m/0l515" - id: 54 - display_name: "sandwich" -} -item { - name: "/m/0cyhj_" - id: 55 - display_name: "orange" -} -item { - name: "/m/0hkxq" - id: 56 - display_name: "broccoli" -} -item { - name: "/m/0fj52s" - id: 57 - display_name: "carrot" -} -item { - name: "/m/01b9xk" - id: 58 - display_name: "hot dog" -} -item { - name: "/m/0663v" - id: 59 - display_name: "pizza" -} -item { - name: "/m/0jy4k" - id: 60 - display_name: "donut" -} -item { - name: "/m/0fszt" - id: 61 - display_name: "cake" -} -item { - name: "/m/01mzpv" - id: 62 - display_name: "chair" -} -item { - name: "/m/02crq1" - id: 63 - display_name: "couch" -} -item { - name: "/m/03fp41" - id: 64 - display_name: "potted plant" -} -item { - name: "/m/03ssj5" - id: 65 - display_name: "bed" -} -item { - name: "66" - id: 66 - display_name: "66" -} -item { - name: "/m/04bcr3" - id: 67 - display_name: "dining table" -} -item { - name: "68" - id: 68 - display_name: "68" -} -item { - name: "69" - id: 69 - display_name: "69" -} -item { - name: "/m/09g1w" - id: 70 - display_name: "toilet" -} -item { - name: "71" - id: 71 - display_name: "71" -} -item { - name: "/m/07c52" - id: 72 - display_name: "tv" -} -item { - name: "/m/01c648" - id: 73 - display_name: "laptop" -} -item { - name: "/m/020lf" - id: 74 - display_name: "mouse" -} -item { - name: "/m/0qjjc" - id: 75 - display_name: "remote" -} -item { - name: "/m/01m2v" - id: 76 - display_name: "keyboard" -} -item { - name: "/m/050k8" - id: 77 - display_name: "cell phone" -} -item { - name: "/m/0fx9l" - id: 78 - display_name: "microwave" -} -item { - name: "/m/029bxz" - id: 79 - display_name: "oven" -} -item { - name: "/m/01k6s3" - id: 80 - display_name: "toaster" -} -item { - name: "/m/0130jx" - id: 81 - display_name: "sink" -} -item { - name: "/m/040b_t" - id: 82 - display_name: "refrigerator" -} -item { - name: "83" - id: 83 - display_name: "83" -} -item { - name: "/m/0bt_c3" - id: 84 - display_name: "book" -} -item { - name: "/m/01x3z" - id: 85 - display_name: "clock" -} -item { - name: "/m/02s195" - id: 86 - display_name: "vase" -} -item { - name: "/m/01lsmm" - id: 87 - display_name: "scissors" -} -item { - name: "/m/0kmg4" - id: 88 - display_name: "teddy bear" -} -item { - name: "/m/03wvsk" - id: 89 - display_name: "hair drier" -} -item { - name: "/m/012xff" - id: 90 - display_name: "toothbrush" -} diff --git a/research/object_detection/data/mscoco_label_map.pbtxt b/research/object_detection/data/mscoco_label_map.pbtxt deleted file mode 100644 index 1f4872bd0c7..00000000000 --- a/research/object_detection/data/mscoco_label_map.pbtxt +++ /dev/null @@ -1,400 +0,0 @@ -item { - name: "/m/01g317" - id: 1 - display_name: "person" -} -item { - name: "/m/0199g" - id: 2 - display_name: "bicycle" -} -item { - name: "/m/0k4j" - id: 3 - display_name: "car" -} -item { - name: "/m/04_sv" - id: 4 - display_name: "motorcycle" -} -item { - name: "/m/05czz6l" - id: 5 - display_name: "airplane" -} -item { - name: "/m/01bjv" - id: 6 - display_name: "bus" -} -item { - name: "/m/07jdr" - id: 7 - display_name: "train" -} -item { - name: "/m/07r04" - id: 8 - display_name: "truck" -} -item { - name: "/m/019jd" - id: 9 - display_name: "boat" -} -item { - name: "/m/015qff" - id: 10 - display_name: "traffic light" -} -item { - name: "/m/01pns0" - id: 11 - display_name: "fire hydrant" -} -item { - name: "/m/02pv19" - id: 13 - display_name: "stop sign" -} -item { - name: "/m/015qbp" - id: 14 - display_name: "parking meter" -} -item { - name: "/m/0cvnqh" - id: 15 - display_name: "bench" -} -item { - name: "/m/015p6" - id: 16 - display_name: "bird" -} -item { - name: "/m/01yrx" - id: 17 - display_name: "cat" -} -item { - name: "/m/0bt9lr" - id: 18 - display_name: "dog" -} -item { - name: "/m/03k3r" - id: 19 - display_name: "horse" -} -item { - name: "/m/07bgp" - id: 20 - display_name: "sheep" -} -item { - name: "/m/01xq0k1" - id: 21 - display_name: "cow" -} -item { - name: "/m/0bwd_0j" - id: 22 - display_name: "elephant" -} -item { - name: "/m/01dws" - id: 23 - display_name: "bear" -} -item { - name: "/m/0898b" - id: 24 - display_name: "zebra" -} -item { - name: "/m/03bk1" - id: 25 - display_name: "giraffe" -} -item { - name: "/m/01940j" - id: 27 - display_name: "backpack" -} -item { - name: "/m/0hnnb" - id: 28 - display_name: "umbrella" -} -item { - name: "/m/080hkjn" - id: 31 - display_name: "handbag" -} -item { - name: "/m/01rkbr" - id: 32 - display_name: "tie" -} -item { - name: "/m/01s55n" - id: 33 - display_name: "suitcase" -} -item { - name: "/m/02wmf" - id: 34 - display_name: "frisbee" -} -item { - name: "/m/071p9" - id: 35 - display_name: "skis" -} -item { - name: "/m/06__v" - id: 36 - display_name: "snowboard" -} -item { - name: "/m/018xm" - id: 37 - display_name: "sports ball" -} -item { - name: "/m/02zt3" - id: 38 - display_name: "kite" -} -item { - name: "/m/03g8mr" - id: 39 - display_name: "baseball bat" -} -item { - name: "/m/03grzl" - id: 40 - display_name: "baseball glove" -} -item { - name: "/m/06_fw" - id: 41 - display_name: "skateboard" -} -item { - name: "/m/019w40" - id: 42 - display_name: "surfboard" -} -item { - name: "/m/0dv9c" - id: 43 - display_name: "tennis racket" -} -item { - name: "/m/04dr76w" - id: 44 - display_name: "bottle" -} -item { - name: "/m/09tvcd" - id: 46 - display_name: "wine glass" -} -item { - name: "/m/08gqpm" - id: 47 - display_name: "cup" -} -item { - name: "/m/0dt3t" - id: 48 - display_name: "fork" -} -item { - name: "/m/04ctx" - id: 49 - display_name: "knife" -} -item { - name: "/m/0cmx8" - id: 50 - display_name: "spoon" -} -item { - name: "/m/04kkgm" - id: 51 - display_name: "bowl" -} -item { - name: "/m/09qck" - id: 52 - display_name: "banana" -} -item { - name: "/m/014j1m" - id: 53 - display_name: "apple" -} -item { - name: "/m/0l515" - id: 54 - display_name: "sandwich" -} -item { - name: "/m/0cyhj_" - id: 55 - display_name: "orange" -} -item { - name: "/m/0hkxq" - id: 56 - display_name: "broccoli" -} -item { - name: "/m/0fj52s" - id: 57 - display_name: "carrot" -} -item { - name: "/m/01b9xk" - id: 58 - display_name: "hot dog" -} -item { - name: "/m/0663v" - id: 59 - display_name: "pizza" -} -item { - name: "/m/0jy4k" - id: 60 - display_name: "donut" -} -item { - name: "/m/0fszt" - id: 61 - display_name: "cake" -} -item { - name: "/m/01mzpv" - id: 62 - display_name: "chair" -} -item { - name: "/m/02crq1" - id: 63 - display_name: "couch" -} -item { - name: "/m/03fp41" - id: 64 - display_name: "potted plant" -} -item { - name: "/m/03ssj5" - id: 65 - display_name: "bed" -} -item { - name: "/m/04bcr3" - id: 67 - display_name: "dining table" -} -item { - name: "/m/09g1w" - id: 70 - display_name: "toilet" -} -item { - name: "/m/07c52" - id: 72 - display_name: "tv" -} -item { - name: "/m/01c648" - id: 73 - display_name: "laptop" -} -item { - name: "/m/020lf" - id: 74 - display_name: "mouse" -} -item { - name: "/m/0qjjc" - id: 75 - display_name: "remote" -} -item { - name: "/m/01m2v" - id: 76 - display_name: "keyboard" -} -item { - name: "/m/050k8" - id: 77 - display_name: "cell phone" -} -item { - name: "/m/0fx9l" - id: 78 - display_name: "microwave" -} -item { - name: "/m/029bxz" - id: 79 - display_name: "oven" -} -item { - name: "/m/01k6s3" - id: 80 - display_name: "toaster" -} -item { - name: "/m/0130jx" - id: 81 - display_name: "sink" -} -item { - name: "/m/040b_t" - id: 82 - display_name: "refrigerator" -} -item { - name: "/m/0bt_c3" - id: 84 - display_name: "book" -} -item { - name: "/m/01x3z" - id: 85 - display_name: "clock" -} -item { - name: "/m/02s195" - id: 86 - display_name: "vase" -} -item { - name: "/m/01lsmm" - id: 87 - display_name: "scissors" -} -item { - name: "/m/0kmg4" - id: 88 - display_name: "teddy bear" -} -item { - name: "/m/03wvsk" - id: 89 - display_name: "hair drier" -} -item { - name: "/m/012xff" - id: 90 - display_name: "toothbrush" -} diff --git a/research/object_detection/data/mscoco_minival_ids.txt b/research/object_detection/data/mscoco_minival_ids.txt deleted file mode 100644 index 5bbff3c18d4..00000000000 --- a/research/object_detection/data/mscoco_minival_ids.txt +++ /dev/null @@ -1,8059 +0,0 @@ -25096 -251824 -35313 -546011 -524186 -205866 -511403 -313916 -47471 -258628 -233560 -576017 -404517 -410056 -178690 -248980 -511724 -429718 -163076 -244111 -126766 -313182 -191981 -139992 -325237 -248129 -214519 -175438 -493321 -174103 -563762 -536795 -289960 -473720 -515540 -292118 -360851 -267175 -532876 -171613 -581415 -259819 -441841 -381682 -58157 -4980 -473929 -70626 -93773 -283412 -36765 -495020 -278401 -329307 -192810 -491784 -506416 -225495 -553747 -86442 -242208 -132686 -385877 -290248 -525705 -5476 -486521 -332512 -138556 -348083 -284375 -40018 -296994 -38685 -432429 -183407 -434358 -472164 -530494 -570693 -193401 -392612 -98872 -445766 -532209 -98322 -285114 -267725 -51605 -314812 -91105 -535506 -540264 -375341 -449828 -277659 -68933 -76873 -217554 -213592 -190776 -516224 -474479 -343599 -578813 -128669 -546292 -475365 -377626 -128833 -427091 -547227 -11742 -80213 -462241 -374574 -121572 -29151 -13892 -262394 -303667 -198724 -7320 -448492 -419080 -460379 -483965 -556516 -139181 -1103 -308715 -207507 -213827 -216083 -445597 -240275 -379585 -116389 -138124 -559051 -326898 -419386 -503660 -519460 -23893 -24458 -518109 -462982 -151492 -514254 -2477 -147165 -570394 -548766 -250083 -364341 -351967 -386277 -328084 -511299 -499349 -315501 -234965 -428562 -219771 -288150 -136021 -168619 -298316 -75118 -189752 -243857 -296222 -554002 -533628 -384596 -202981 -498350 -391463 -183991 -528062 -451084 -7899 -408534 -329030 -318566 -22492 -361285 -226973 -213356 -417265 -105622 -161169 -261487 -167477 -233370 -142999 -256713 -305833 -103579 -352538 -135763 -392144 -61181 -200302 -456908 -286858 -179850 -488075 -174511 -194755 -317822 -2302 -304596 -172556 -548275 -341678 -55299 -134760 -352936 -545129 -377012 -141328 -103757 -552837 -28246 -125167 -328745 -278760 -337133 -403389 -146825 -502558 -265916 -428985 -492041 -113403 -372037 -306103 -287574 -187495 -479805 -336309 -162043 -95899 -43133 -464248 -149115 -247438 -74030 -130645 -282841 -127092 -101172 -536743 -179642 -58133 -49667 -170605 -11347 -365277 -201970 -292663 -217219 -463226 -41924 -281102 -357816 -490878 -100343 -525058 -133503 -416145 -29341 -415413 -125527 -507951 -262609 -240210 -581781 -345137 -526342 -268641 -328777 -32001 -137538 -39115 -415958 -6771 -421865 -64909 -383601 -206907 -420840 -370980 -28452 -571893 -153520 -185890 -392991 -547013 -257359 -279879 -478614 -131919 -40937 -22874 -173375 -106344 -44801 -205401 -312870 -400886 -351530 -344013 -173500 -470423 -396729 -402499 -276585 -377097 -367619 -518908 -263866 -332292 -67805 -152211 -515025 -221350 -525247 -78490 -504342 -95908 -82668 -256199 -220270 -552065 -242379 -84866 -152281 -228464 -223122 -67537 -456968 -368349 -101985 -14681 -543551 -107558 -372009 -99054 -126540 -86877 -492785 -482585 -571564 -501116 -296871 -20395 -181518 -568041 -121154 -56187 -190018 -97156 -310325 -393274 -214574 -243222 -289949 -452121 -150508 -341752 -310757 -24040 -228551 -335589 -12020 -529597 -459884 -344888 -229713 -51948 -370929 -552061 -261072 -120070 -332067 -263014 -158993 -451714 -397327 -20965 -414340 -574946 -370266 -487534 -492246 -264771 -73702 -43997 -235124 -301093 -400048 -77681 -58472 -331386 -13783 -242513 -419158 -59325 -383033 -393258 -529041 -249276 -182775 -351793 -9727 -334069 -566771 -539355 -38662 -423617 -47559 -120592 -508303 -462565 -47916 -218208 -182362 -562101 -441442 -71239 -395378 -522637 -25603 -484450 -872 -171483 -527248 -323155 -240754 -15032 -419144 -313214 -250917 -333430 -242757 -221914 -283190 -194297 -228506 -550691 -172513 -312192 -530619 -113867 -323552 -374115 -35435 -160239 -62877 -441873 -196574 -62858 -557114 -427612 -242869 -356733 -304828 -24880 -490509 -407083 -457877 -402788 -536416 -385912 -544121 -500389 -451102 -12120 -483476 -70987 -482799 -542549 -49236 -424258 -435783 -182366 -438093 -501824 -232845 -53965 -223198 -288933 -450458 -285664 -196484 -408930 -519815 -290981 -398567 -315792 -490683 -257136 -75611 -302498 -332153 -82293 -416911 -558608 -564659 -536195 -370260 -57904 -527270 -6593 -145620 -551650 -470832 -515785 -251404 -287331 -150788 -334006 -266117 -10039 -579158 -328397 -468351 -550400 -31745 -405970 -16761 -323515 -459598 -558457 -570736 -476939 -472610 -72155 -112517 -13659 -530905 -458768 -43486 -560893 -493174 -31217 -262736 -412204 -142722 -151231 -480643 -197245 -398666 -444869 -110999 -191724 -479057 -492420 -170638 -277329 -301908 -395644 -537611 -141887 -47149 -403432 -34818 -372495 -67994 -337497 -478586 -249815 -533462 -281032 -289941 -151911 -271215 -407868 -360700 -508582 -103873 -353658 -369081 -406403 -331692 -26430 -105655 -572630 -37181 -91336 -484587 -318284 -113019 -33055 -25293 -229324 -374052 -384111 -213951 -315195 -319283 -539453 -17655 -308974 -326243 -539436 -417876 -526940 -356347 -221932 -73753 -292648 -262284 -304924 -558587 -374858 -253518 -311744 -539636 -40924 -136624 -334305 -365997 -63355 -191226 -526732 -367128 -575198 -500657 -50637 -17182 -424792 -565353 -563040 -383494 -74458 -155142 -197125 -223857 -428241 -440830 -371289 -437303 -330449 -93771 -82715 -499631 -381257 -563951 -192834 -528600 -404273 -270554 -208053 -188613 -484760 -432016 -129800 -91756 -523097 -317018 -487282 -444913 -159500 -126822 -540564 -105812 -560756 -306099 -471226 -123842 -513219 -154877 -497034 -283928 -564003 -238602 -194780 -462728 -558640 -524373 -455624 -3690 -560367 -316351 -455772 -223777 -161517 -243034 -250440 -239975 -441008 -324715 -152106 -246973 -462805 -296521 -412767 -530913 -370165 -292526 -107244 -217440 -330204 -220176 -577735 -197022 -127451 -518701 -212322 -204887 -27696 -348474 -119233 -282804 -230040 -425690 -409241 -296825 -296353 -375909 -123136 -573891 -338256 -198247 -373375 -151051 -500084 -557596 -120478 -44989 -283380 -149005 -522065 -626 -17198 -309633 -524245 -291589 -322714 -455847 -248468 -371948 -444928 -20438 -481670 -147195 -95022 -548159 -553165 -395324 -391371 -86884 -561121 -219737 -38875 -338159 -377881 -185472 -359277 -114861 -378048 -126226 -10217 -320246 -15827 -178236 -370279 -352978 -408101 -77615 -337044 -223714 -20796 -352445 -263834 -156704 -377867 -119402 -399567 -1180 -257941 -560675 -390471 -209290 -258382 -466339 -56437 -195042 -384230 -203214 -36077 -283038 -38323 -158770 -532381 -395903 -375461 -397857 -326798 -371699 -369503 -495626 -464328 -462211 -397719 -434089 -424793 -476770 -531852 -303538 -525849 -480917 -419653 -265063 -48956 -5184 -279149 -396727 -374266 -124429 -36124 -240213 -147556 -339512 -577182 -288599 -257169 -178254 -393869 -122314 -28713 -48133 -540681 -100974 -368459 -500110 -73634 -460982 -203878 -578344 -443602 -502012 -399666 -103603 -22090 -257529 -176328 -536656 -408873 -116881 -460972 -33835 -460781 -51223 -46463 -89395 -407646 -337453 -461715 -16257 -426987 -234889 -3125 -165643 -517472 -451435 -206800 -112128 -331236 -163306 -94185 -498716 -532732 -146509 -458567 -153832 -105996 -353398 -546976 -283060 -247624 -110048 -243491 -154798 -543600 -149962 -355256 -352900 -203081 -372203 -284605 -516244 -190494 -150301 -326082 -64146 -402858 -413538 -399510 -460251 -94336 -458721 -57345 -424162 -423508 -69356 -567220 -509786 -37038 -111535 -341318 -372067 -358120 -244909 -180653 -39852 -438560 -357041 -67065 -51928 -171717 -520430 -552395 -431355 -528084 -20913 -309610 -262323 -573784 -449485 -154846 -283438 -430871 -199578 -516318 -563912 -348483 -485613 -143440 -94922 -168817 -74457 -45830 -66297 -514173 -99186 -296236 -230903 -452312 -476444 -568981 -100811 -237350 -194724 -453622 -49559 -270609 -113701 -415393 -92173 -137004 -188795 -148280 -448114 -575964 -163155 -518719 -219329 -214247 -363927 -65357 -87617 -552612 -457817 -124796 -47740 -560463 -513968 -273637 -354212 -95959 -261061 -307265 -316237 -191342 -463272 -169273 -396518 -93261 -572733 -407386 -202658 -446497 -420852 -229274 -432724 -34900 -352533 -49891 -66144 -146831 -467484 -97988 -561647 -301155 -507421 -173217 -577584 -451940 -99927 -350639 -178941 -485155 -175948 -360673 -92963 -361321 -48739 -577310 -517795 -93405 -506458 -394681 -167920 -16995 -519573 -270532 -527750 -563403 -494608 -557780 -178691 -8676 -186927 -550173 -361656 -575911 -281315 -534377 -57570 -340894 -37624 -143103 -538243 -425077 -376545 -108129 -170974 -7522 -408906 -264279 -79415 -344025 -186797 -234349 -226472 -123639 -225177 -237984 -38714 -223671 -358247 -152465 -521405 -453722 -361111 -557117 -235832 -309341 -268469 -108353 -532531 -357279 -537280 -437618 -122953 -7088 -36693 -127659 -431901 -57244 -567565 -568111 -202926 -504516 -555685 -322369 -347620 -110231 -568982 -295340 -529798 -300341 -158160 -73588 -119476 -387216 -154994 -259755 -211282 -433971 -263588 -299468 -570138 -123017 -355106 -540172 -406215 -8401 -548844 -161820 -396432 -495348 -222407 -53123 -491556 -108130 -440617 -448309 -22596 -346841 -213829 -135076 -56326 -233139 -487418 -227326 -137763 -383389 -47882 -207797 -167452 -112065 -150703 -421109 -171753 -158279 -240800 -66821 -152886 -163640 -475466 -301799 -106712 -470885 -536370 -420389 -396768 -281950 -18903 -357529 -33650 -168243 -201004 -389295 -557150 -185327 -181256 -557396 -182025 -61564 -301928 -332455 -199403 -18444 -177452 -204206 -38465 -215906 -153103 -445019 -324527 -299207 -429281 -574675 -157067 -241269 -100850 -502818 -576566 -296775 -873 -280363 -355240 -383445 -286182 -67327 -422778 -494855 -337246 -266853 -47516 -381991 -44081 -403862 -381430 -370798 -173383 -387173 -22396 -484066 -349414 -262235 -492814 -65238 -209420 -336276 -453328 -407286 -420490 -360328 -158440 -398534 -489475 -477389 -297108 -69750 -507833 -198992 -99736 -546444 -514914 -482574 -54355 -63478 -191693 -61684 -412914 -267408 -424641 -56872 -318080 -30290 -33441 -199310 -337403 -26731 -453390 -506137 -188945 -185950 -239843 -357944 -290570 -523637 -551952 -513397 -357870 -523517 -277048 -259879 -186991 -521943 -21900 -281074 -187194 -526723 -568147 -513037 -177338 -243831 -203488 -208494 -188460 -289943 -399177 -404668 -160761 -271143 -76087 -478922 -440045 -449432 -61025 -331138 -227019 -147577 -548337 -444294 -458663 -236837 -6854 -444926 -484816 -516641 -397863 -188534 -64822 -213453 -66561 -43218 -514901 -322844 -498453 -488788 -391656 -298994 -64088 -464706 -193720 -199017 -186427 -15278 -350386 -342335 -372024 -550939 -35594 -381382 -235902 -26630 -213765 -550001 -129706 -577149 -353096 -376891 -28499 -427041 -314965 -231163 -5728 -347836 -184388 -27476 -284860 -476872 -301317 -99546 -147653 -529515 -311922 -20777 -2613 -59463 -430670 -560744 -60677 -332087 -296724 -353321 -103306 -363887 -76431 -423058 -120340 -119452 -6723 -462327 -163127 -402723 -489382 -183181 -107656 -375409 -355228 -430762 -512468 -409125 -270544 -559113 -495388 -529434 -38355 -422025 -379667 -131386 -183409 -573536 -581317 -425404 -350084 -472 -28532 -329717 -230220 -187196 -484166 -97434 -224595 -87483 -516998 -314876 -32610 -514586 -344816 -394418 -402330 -305993 -371497 -315790 -294908 -207431 -561014 -26584 -368671 -374990 -54747 -47571 -449424 -283761 -84735 -522127 -120473 -524656 -479659 -131627 -450959 -153300 -580908 -207785 -49115 -284991 -96505 -278306 -291655 -1404 -489304 -557459 -37740 -157465 -390475 -119166 -33871 -247428 -75905 -20779 -65035 -333556 -375415 -383676 -505243 -87327 -16451 -287235 -70190 -245067 -417520 -229234 -183786 -333018 -554156 -198915 -108021 -128262 -412443 -242543 -555050 -436511 -445233 -207886 -156397 -526257 -521357 -413043 -427189 -401614 -94823 -351130 -105945 -182314 -305879 -526197 -64409 -496800 -236461 -138175 -43816 -185904 -345711 -72536 -526737 -360400 -556537 -426053 -59044 -28290 -222548 -434915 -418623 -246454 -111801 -12448 -427133 -459117 -11262 -169045 -469996 -304390 -513096 -322822 -196371 -504977 -395364 -243950 -216218 -417217 -106736 -58194 -504101 -478522 -379314 -30432 -207027 -297146 -91844 -176031 -98287 -278095 -196053 -343692 -523137 -220224 -349485 -376193 -407067 -185781 -37871 -336464 -46331 -44244 -80274 -170147 -361106 -468499 -537864 -467457 -267343 -291528 -287828 -555648 -388284 -576085 -531973 -350122 -422253 -509811 -78093 -410019 -133090 -581205 -343976 -9007 -92478 -450674 -486306 -503978 -46378 -335578 -404071 -225558 -217923 -406217 -138054 -575815 -234990 -336257 -159240 -399516 -226408 -531126 -138599 -61693 -89861 -29504 -163296 -477906 -48419 -25595 -195594 -97592 -392555 -203849 -139248 -245651 -275755 -245426 -127279 -521359 -517623 -235747 -475906 -11198 -336101 -70134 -505447 -218996 -30080 -484457 -120441 -575643 -132703 -197915 -505576 -90956 -99741 -517819 -240918 -150834 -207306 -132682 -88250 -213599 -462584 -413321 -361521 -496081 -410583 -440027 -417284 -397069 -280498 -473171 -129739 -279774 -29370 -518899 -509867 -85556 -434930 -280710 -55077 -348793 -157756 -281111 -190689 -281447 -502854 -232894 -268742 -199553 -220808 -137330 -256903 -116017 -466416 -41635 -110906 -340934 -557501 -146767 -517617 -487159 -1561 -417281 -489014 -292463 -113533 -412247 -263973 -515444 -343561 -310200 -293804 -225867 -150320 -183914 -9707 -89999 -177842 -296524 -287829 -68300 -363654 -465986 -159969 -313948 -522779 -219820 -198352 -12959 -266727 -8016 -175804 -497867 -307892 -287527 -309638 -205854 -114119 -23023 -322586 -383341 -134198 -553522 -70426 -329138 -105367 -175597 -187791 -17944 -366611 -93493 -242422 -41842 -558840 -32203 -19667 -124297 -383726 -252625 -234794 -498228 -102906 -287967 -69021 -51326 -243896 -509423 -440124 -122582 -344325 -34455 -442478 -23587 -236904 -185633 -349841 -44294 -112568 -186296 -71914 -3837 -135486 -223747 -557517 -385181 -265313 -404263 -26564 -516867 -497096 -332351 -345139 -444304 -510877 -356387 -561214 -311471 -408789 -561729 -291380 -174671 -45710 -435136 -388858 -361693 -50811 -531134 -573605 -340175 -534988 -382671 -327047 -348400 -547137 -401037 -490711 -499266 -236370 -449075 -334015 -107234 -232315 -462953 -252048 -186822 -410168 -28994 -45550 -453626 -417957 -468577 -106338 -391684 -375143 -217622 -357903 -347648 -142182 -213843 -299148 -352587 -436676 -161875 -144655 -304741 -235017 -181799 -211042 -335507 -553731 -412531 -229740 -437129 -423830 -561806 -337666 -52016 -138057 -70254 -494393 -73119 -262425 -565395 -305329 -489611 -377080 -569450 -549766 -332940 -235302 -53893 -203781 -38449 -114870 -18699 -396338 -449839 -423613 -379767 -369594 -375812 -359219 -229311 -291675 -224907 -416885 -32964 -573406 -17282 -103375 -81860 -576886 -461334 -35672 -243442 -217269 -445055 -211112 -455675 -412384 -88967 -550643 -24223 -504074 -9275 -155546 -329542 -172658 -331600 -315492 -194208 -162867 -324614 -432017 -140860 -157944 -406616 -486079 -361172 -258346 -494140 -315384 -451014 -242619 -413684 -386187 -408501 -121089 -343603 -232538 -558671 -551596 -32992 -406647 -435260 -11156 -40896 -175382 -110560 -252968 -189694 -63154 -564816 -72004 -164788 -434583 -453104 -111878 -268484 -290768 -473215 -450620 -32673 -277479 -529917 -315868 -562419 -378347 -398637 -84097 -120527 -134193 -431472 -400238 -86426 -208830 -524535 -22213 -516813 -526044 -386193 -246672 -386739 -559252 -153344 -236123 -246074 -323615 -92644 -408621 -323231 -499940 -296105 -578902 -150098 -145015 -131431 -318618 -68409 -497928 -362520 -467755 -112702 -163219 -277289 -192362 -497674 -525439 -56267 -465868 -407570 -551608 -345211 -179653 -55295 -97315 -534041 -505822 -411082 -132375 -25378 -272008 -536605 -123511 -148737 -577712 -493751 -29587 -468297 -528458 -491058 -558976 -181421 -209685 -147545 -486964 -570516 -168662 -19446 -395997 -242911 -232511 -317035 -354527 -5961 -513793 -124390 -370123 -113397 -195790 -252813 -326919 -432414 -409239 -458221 -115667 -212239 -279279 -375554 -546622 -317188 -260818 -286021 -377111 -209868 -243148 -132037 -560624 -459721 -193498 -22623 -254164 -112841 -383470 -62692 -227940 -471335 -44858 -213649 -179898 -102837 -474078 -44478 -256197 -309492 -182923 -421139 -275695 -104965 -480780 -449749 -76513 -578591 -336695 -247474 -320490 -246105 -53183 -485740 -575823 -510735 -290741 -37017 -348708 -279784 -453634 -567644 -434192 -482719 -435324 -544299 -106896 -569926 -301574 -492885 -103462 -487151 -513585 -219647 -303685 -459645 -76292 -188579 -154883 -207728 -425074 -310493 -27221 -371694 -119404 -399665 -273556 -454577 -580698 -267664 -295769 -423740 -22461 -22667 -508443 -390401 -369997 -524627 -193349 -132223 -576743 -130586 -487741 -107542 -501420 -520109 -308156 -540581 -231362 -86471 -472930 -351133 -463605 -575577 -159842 -39504 -223020 -63525 -298627 -139883 -375205 -303549 -16838 -495680 -408112 -394474 -188044 -472143 -463751 -31481 -378139 -190853 -442614 -172006 -140270 -133051 -178028 -495090 -88455 -13232 -46323 -346275 -425905 -487013 -433136 -514402 -521906 -4157 -61418 -567205 -213351 -304008 -296492 -506561 -408120 -415961 -323186 -480379 -349199 -201918 -135023 -456483 -136173 -237917 -4972 -99081 -331569 -150007 -36450 -93400 -487461 -203629 -218093 -487181 -113935 -139512 -210981 -358883 -47419 -248382 -80357 -462663 -83097 -26159 -80429 -283055 -452676 -50159 -12326 -29430 -303264 -158122 -569070 -52925 -534876 -46975 -426376 -170293 -434417 -235517 -218476 -445008 -482774 -305632 -116848 -557252 -229270 -453485 -382214 -54759 -59171 -193328 -17152 -238071 -148531 -409725 -75434 -65358 -473057 -415408 -579415 -48636 -269606 -298784 -162799 -356400 -326854 -24601 -66499 -340247 -20992 -190218 -548464 -122203 -405306 -495376 -536028 -5713 -206831 -9395 -503939 -194440 -474253 -395849 -165141 -204935 -412621 -402922 -87141 -570664 -202622 -137362 -221737 -78947 -112129 -341957 -169562 -164780 -360216 -107641 -415015 -444955 -559102 -123070 -176592 -309366 -116461 -222075 -530470 -214363 -414487 -471567 -292123 -370210 -364243 -510254 -396350 -141524 -220310 -398604 -145436 -392476 -17482 -78032 -336171 -130812 -489743 -346638 -418854 -139072 -263860 -458240 -383443 -337533 -182334 -535608 -517946 -489924 -308117 -129945 -59973 -538364 -513458 -449433 -25165 -335851 -487688 -153834 -347612 -349689 -443688 -486008 -479149 -442286 -61108 -315338 -511546 -506444 -775 -121839 -291412 -497626 -387223 -367095 -557896 -196118 -530652 -447991 -215622 -232160 -296731 -272273 -473415 -364705 -235790 -479950 -141278 -547903 -66523 -353989 -121875 -237735 -100083 -348941 -288983 -390083 -168248 -120776 -489764 -219135 -551713 -256035 -309005 -112493 -579759 -114972 -458992 -295768 -158497 -309696 -363844 -507966 -313491 -280779 -327130 -292901 -127761 -183843 -456521 -164475 -224281 -443713 -72514 -567383 -476215 -565650 -17708 -474471 -248334 -196313 -164759 -212453 -319024 -332916 -35436 -113139 -172716 -7570 -161609 -144534 -137475 -561411 -45844 -332027 -36990 -190160 -421231 -283210 -365611 -511407 -400887 -485071 -481214 -347203 -153506 -397403 -229599 -357322 -76034 -101189 -567444 -92363 -526767 -218811 -362812 -339120 -579696 -399269 -10705 -549012 -410428 -105623 -535307 -419235 -119911 -236604 -515779 -188173 -66397 -549119 -478742 -256180 -128224 -440539 -112818 -315434 -97513 -171970 -433483 -226008 -83217 -424548 -343753 -350334 -479280 -208808 -43266 -399893 -444386 -47687 -499093 -565269 -465835 -167486 -433460 -169872 -299640 -158466 -241373 -50576 -161567 -73560 -349804 -181745 -352684 -450357 -532693 -88335 -256518 -94926 -541197 -14629 -276149 -539439 -498738 -25654 -291330 -146465 -160190 -513064 -75748 -499007 -164464 -134042 -422416 -543315 -34056 -303197 -394801 -293071 -44964 -529083 -414522 -331180 -227599 -581040 -382850 -159898 -176841 -205352 -540782 -406591 -184499 -14380 -350230 -458175 -528786 -314935 -111086 -2191 -20371 -337042 -558371 -296907 -539937 -511463 -574856 -87864 -403817 -152598 -169712 -533227 -173545 -478862 -19455 -258433 -373440 -460229 -525682 -176857 -525050 -277025 -156416 -206784 -415179 -183204 -210374 -312868 -514366 -65208 -376342 -515792 -383066 -85247 -119132 -338007 -88748 -206705 -495808 -532164 -150686 -35474 -207860 -111165 -391199 -346011 -537721 -11390 -487482 -360983 -400347 -92795 -347506 -324322 -371958 -101280 -222842 -563604 -210299 -150616 -96351 -330455 -273551 -228749 -248051 -495252 -372265 -52664 -191874 -157416 -446428 -136681 -1228 -321811 -93791 -477867 -192520 -157124 -40620 -200541 -103904 -329494 -60093 -112573 -489125 -513115 -322968 -561619 -74309 -572462 -248252 -375376 -217312 -243213 -79878 -452218 -349754 -554291 -434043 -460373 -452591 -567787 -504711 -196007 -511153 -312416 -296056 -308849 -203667 -253223 -331230 -465545 -363048 -69392 -301506 -216198 -147979 -6005 -381870 -56983 -320972 -144122 -210855 -151480 -299288 -462486 -103931 -321079 -4134 -239861 -540006 -413805 -221222 -198943 -450790 -380597 -388298 -58737 -246197 -160726 -398554 -513946 -222235 -323851 -364703 -125643 -169800 -445662 -223764 -575372 -489207 -559474 -7155 -453819 -402720 -102355 -415076 -287436 -35705 -111076 -395865 -310862 -570834 -54728 -215778 -80053 -35148 -350488 -524140 -190097 -36661 -302110 -96884 -383397 -245462 -446958 -138937 -424712 -561814 -276964 -148034 -411068 -357824 -103257 -322149 -508899 -580294 -214386 -114419 -271429 -168260 -209835 -573072 -252269 -31980 -161308 -281508 -192714 -247599 -188948 -180563 -419601 -233660 -154804 -311846 -181499 -5535 -175082 -531018 -412338 -166995 -441411 -427820 -516846 -287366 -67959 -271266 -330845 -74209 -508167 -542699 -66485 -453756 -158412 -443784 -118097 -265050 -29074 -152623 -532493 -292988 -530384 -192660 -502336 -472648 -151657 -351626 -241010 -115070 -268356 -539557 -304698 -251140 -497158 -527445 -385428 -179200 -512394 -184978 -141910 -36311 -579457 -19129 -424960 -181714 -126216 -512911 -488360 -379533 -337551 -325410 -364587 -468885 -211107 -90062 -500446 -105960 -451951 -431431 -134178 -164548 -173826 -373988 -15157 -3091 -393557 -380011 -75372 -37403 -209995 -493610 -315899 -353299 -355040 -547000 -86133 -58174 -377326 -510230 -480583 -158588 -432529 -311206 -127626 -239980 -166340 -104185 -405174 -507211 -542782 -448078 -253477 -542694 -567308 -214853 -288824 -283268 -480757 -503200 -221089 -112388 -171539 -124452 -224200 -206362 -428754 -256192 -119414 -351620 -330050 -547504 -216398 -94261 -19916 -163242 -432588 -143824 -361103 -271138 -260150 -313627 -141086 -308263 -388453 -153217 -372794 -514787 -251910 -351335 -92683 -465836 -18442 -404128 -208476 -47873 -303219 -201622 -367489 -32760 -436174 -401926 -338419 -45248 -328464 -312216 -156282 -315702 -300701 -345401 -515350 -29094 -284296 -466449 -351057 -110672 -364853 -10014 -415828 -397522 -451412 -433124 -158277 -93476 -183387 -109889 -223326 -105547 -530061 -256301 -526778 -80974 -86650 -45835 -202154 -92678 -315991 -423919 -455044 -491168 -272253 -146627 -285349 -86001 -44171 -162332 -257328 -432820 -519275 -380639 -269436 -236016 -543215 -346752 -575970 -423498 -136926 -195648 -126634 -133078 -138656 -490012 -122388 -195165 -434900 -533625 -504167 -333697 -216576 -538775 -125072 -391154 -545007 -150292 -566717 -367362 -490991 -356623 -141271 -402795 -516786 -39499 -536716 -293324 -212853 -276381 -57124 -325992 -394659 -452178 -117674 -461172 -518586 -497021 -462345 -526570 -17328 -202928 -62566 -411277 -256983 -49473 -211206 -398031 -277955 -531178 -453959 -27946 -252844 -30273 -536933 -500298 -229111 -7977 -27642 -303726 -79927 -110313 -527691 -442205 -33345 -365851 -233236 -239157 -409221 -400803 -32947 -422516 -359727 -215872 -559454 -289716 -450247 -57827 -312298 -530383 -260048 -35857 -224222 -299533 -13296 -325907 -117869 -54088 -391011 -340478 -205344 -347823 -468604 -78701 -101414 -197499 -490871 -89273 -380343 -441974 -35974 -486114 -354398 -535536 -294030 -7276 -278742 -137028 -98721 -372764 -429802 -72105 -220307 -116845 -195406 -333000 -130401 -264382 -125458 -363036 -286994 -531070 -113801 -4108 -47603 -130118 -573924 -302990 -237566 -21470 -577926 -139436 -425925 -36844 -63602 -399791 -35894 -347228 -225617 -504813 -245320 -466007 -553931 -166731 -164885 -19090 -457262 -247806 -502895 -167593 -352491 -520 -26386 -497348 -352000 -386164 -32901 -730 -30925 -333167 -150361 -231747 -462244 -504958 -260738 -313762 -346645 -486118 -202998 -541613 -183884 -230245 -83172 -126638 -51844 -421673 -118625 -377723 -229427 -371326 -104345 -361687 -114246 -397354 -104137 -120850 -260516 -389168 -234555 -26348 -78522 -409784 -303024 -377949 -69887 -546983 -113736 -298197 -476810 -137315 -376321 -410337 -492905 -119785 -158167 -185930 -354061 -106563 -328452 -506587 -536517 -480173 -570688 -376441 -252127 -247720 -132554 -41923 -400317 -170041 -151938 -198650 -6437 -49091 -221820 -455966 -309859 -300659 -15850 -388014 -253386 -65415 -238228 -548882 -302155 -93483 -371869 -397287 -315249 -360564 -448410 -21382 -477474 -144862 -517515 -230190 -322353 -231568 -14940 -132719 -498942 -182469 -113720 -168890 -94852 -246077 -117535 -52596 -419116 -522020 -255338 -125228 -564332 -106375 -249534 -220915 -177758 -293057 -222430 -196878 -554980 -375606 -173081 -84936 -418907 -562229 -457616 -125700 -66038 -239274 -574110 -305540 -98431 -167347 -53345 -438481 -286010 -5569 -343606 -168898 -191301 -236338 -291394 -715 -520237 -236954 -192212 -524002 -471625 -476029 -413124 -203455 -483328 -476417 -114389 -372428 -369221 -322654 -388157 -561314 -264540 -418680 -359540 -426182 -521613 -92248 -74478 -398905 -554273 -125909 -430583 -418959 -503522 -382999 -403145 -536375 -352618 -108193 -279696 -163253 -439007 -204536 -552186 -269926 -372147 -399921 -201418 -240565 -471483 -91619 -393971 -331648 -385856 -567440 -81922 -391722 -372894 -535997 -134096 -545958 -239943 -186929 -34222 -177714 -277812 -197111 -281878 -532003 -557172 -142890 -196116 -385454 -322845 -374987 -123137 -255112 -111207 -304819 -523526 -336046 -42893 -241273 -240049 -90659 -271364 -408008 -253282 -167067 -354278 -178317 -229653 -93333 -163666 -566920 -495199 -100329 -218119 -558864 -257382 -406152 -206587 -420339 -325919 -278853 -555763 -293200 -151000 -209664 -79380 -197177 -353953 -464522 -392260 -46144 -154202 -164366 -206025 -511236 -24921 -497907 -393226 -318138 -364125 -157321 -492395 -187857 -109939 -441500 -144251 -368581 -51403 -283498 -43555 -89356 -404601 -23272 -425762 -460682 -544629 -209829 -322029 -199247 -307262 -571242 -124236 -162393 -104829 -250766 -563938 -237399 -131516 -483001 -21994 -97958 -540187 -264497 -384808 -343187 -51277 -6712 -566103 -435384 -292082 -359039 -165157 -267972 -263796 -489313 -392722 -541924 -554433 -571034 -146112 -201934 -518716 -64116 -294992 -289586 -159970 -479617 -269006 -140465 -513260 -554805 -6579 -452696 -34445 -548296 -372983 -509656 -199339 -130030 -128372 -449454 -139306 -247914 -99024 -499134 -536653 -468917 -412813 -404338 -215303 -455414 -413497 -574988 -397117 -188631 -378701 -241867 -143129 -419884 -412749 -496954 -317732 -16977 -398309 -162363 -147576 -100016 -209018 -92660 -173302 -525732 -449198 -99734 -12733 -172946 -168032 -210988 -340697 -4795 -534887 -483553 -278323 -178175 -190095 -357542 -230432 -227460 -334609 -562121 -378126 -555357 -325666 -451859 -526837 -531710 -297249 -294839 -499785 -254976 -527220 -173057 -11760 -163012 -215998 -114420 -57812 -563712 -513887 -201859 -36333 -291990 -338375 -460621 -518889 -337502 -133050 -80172 -537007 -295270 -335644 -227852 -336044 -204137 -82259 -165675 -295713 -343937 -442567 -356002 -346932 -62985 -180925 -525381 -13081 -377406 -159774 -462643 -359105 -185821 -390201 -84168 -128059 -80340 -481159 -491902 -306619 -353807 -390569 -541562 -292616 -64621 -439224 -96288 -449798 -160927 -496324 -90778 -126145 -97230 -572767 -11570 -539075 -350988 -3779 -208135 -551315 -216449 -169606 -502 -67765 -281414 -118594 -146127 -543985 -124927 -471394 -385508 -373783 -501315 -140974 -42757 -527054 -202387 -513056 -329931 -153973 -510152 -520812 -534601 -131282 -386638 -508538 -234779 -229329 -396568 -153568 -229478 -153574 -356299 -436694 -324139 -299409 -212462 -478155 -393266 -117836 -190760 -213605 -196 -444382 -445211 -363845 -433277 -521141 -464786 -169076 -301402 -4495 -177258 -328962 -183757 -452966 -416059 -113233 -559417 -280678 -481398 -328372 -234910 -30667 -343062 -383046 -370953 -258089 -404229 -456931 -535183 -300867 -60507 -262672 -7288 -81100 -575395 -539951 -347848 -437594 -352005 -14941 -196453 -528386 -466939 -482187 -293468 -494077 -217285 -362951 -435751 -411480 -517315 -480015 -60610 -353001 -376442 -430265 -478338 -303069 -525344 -437331 -389315 -8179 -31981 -313872 -330920 -515465 -258905 -142249 -323128 -389699 -565012 -124636 -488693 -376608 -309424 -370596 -261940 -39871 -226984 -152866 -515050 -116861 -412876 -120411 -550452 -565273 -273791 -181466 -183155 -293505 -336113 -569997 -303738 -331049 -147030 -74058 -198176 -23991 -198841 -79816 -85183 -261535 -566756 -386291 -318200 -569849 -57429 -36049 -420827 -519271 -24391 -172087 -158795 -133002 -522198 -133698 -499365 -79261 -258860 -457718 -179948 -421875 -558073 -206684 -529762 -456756 -65773 -425722 -53102 -294264 -416730 -38574 -176275 -404297 -127494 -242060 -272212 -189244 -510861 -421370 -208516 -206431 -248457 -39502 -375087 -130839 -308730 -572453 -263474 -544611 -255708 -412604 -390094 -578131 -234463 -493563 -9450 -381914 -148999 -32300 -423576 -569758 -347253 -92939 -112212 -13923 -39472 -363736 -289659 -269949 -88349 -188522 -488915 -129054 -573823 -316000 -440562 -408818 -539302 -199575 -122300 -340047 -322816 -472878 -313922 -228071 -265648 -400166 -169166 -10040 -125245 -148766 -31281 -172599 -431067 -208236 -441824 -175611 -15148 -431199 -521587 -50025 -443139 -349822 -515056 -27530 -571970 -82367 -7115 -424333 -157601 -537506 -447187 -115182 -547597 -5586 -143040 -31650 -196336 -279818 -206273 -403104 -514248 -243190 -558642 -548246 -16848 -391539 -89614 -284589 -191314 -259452 -208380 -209441 -465463 -385005 -321385 -223569 -11727 -87574 -566470 -210890 -323598 -427193 -425676 -401240 -94021 -259571 -447553 -456053 -84693 -14278 -119995 -234595 -408696 -136271 -143560 -357578 -28071 -36561 -157102 -293789 -392251 -356622 -180274 -48320 -475779 -301326 -100977 -413551 -574010 -404479 -80725 -552221 -575441 -197424 -124601 -215633 -359546 -25386 -73199 -334466 -156572 -124614 -34121 -460049 -327623 -441695 -292488 -476514 -464018 -348571 -113413 -125208 -129690 -446218 -493761 -383413 -460390 -343149 -374041 -525211 -451263 -333683 -385194 -107427 -102872 -517249 -475879 -575755 -147787 -297180 -343774 -112437 -142240 -384503 -511111 -51089 -145408 -143582 -408138 -162858 -71850 -126925 -222781 -314616 -425609 -203928 -337563 -223300 -52644 -272566 -232597 -374430 -469075 -267164 -265851 -28134 -308889 -465795 -47263 -233727 -42 -493117 -124621 -533378 -361259 -458750 -429033 -383289 -490927 -520964 -174420 -64425 -378859 -401850 -281475 -46508 -205300 -280736 -110961 -230679 -151956 -321497 -73665 -488736 -165353 -365983 -556230 -21465 -581226 -448861 -3793 -347335 -150726 -75319 -2521 -285894 -133876 -104589 -346013 -63516 -83656 -491515 -326256 -49942 -28508 -475413 -270222 -235839 -48554 -327777 -111179 -507171 -425973 -449490 -205239 -82375 -459575 -432300 -91885 -340922 -270239 -195894 -121417 -344831 -439651 -232148 -391688 -480793 -534275 -260823 -469294 -8688 -255654 -191300 -383464 -81594 -21240 -478077 -517596 -555953 -294119 -402234 -459500 -564280 -106849 -167501 -98328 -267411 -145512 -272599 -50054 -414156 -161129 -418226 -11796 -502090 -390350 -440500 -240727 -104406 -163682 -437910 -143767 -358901 -527631 -500543 -28377 -231097 -227985 -556703 -421566 -73201 -478393 -280347 -15497 -131969 -515760 -295440 -462527 -42147 -120007 -212895 -425361 -454143 -5758 -366782 -213932 -229848 -458861 -132791 -476664 -150365 -343038 -529649 -180515 -499810 -329041 -15660 -419228 -396295 -502644 -321085 -245049 -34193 -217323 -446455 -528046 -375573 -15802 -147448 -407291 -84000 -280891 -150487 -510606 -163025 -249964 -126123 -233771 -118507 -97278 -357386 -23121 -10580 -2153 -176017 -371472 -373289 -173908 -296797 -334083 -301107 -577522 -125404 -278359 -575032 -273002 -266371 -108315 -255633 -503490 -250051 -143927 -117407 -198271 -447043 -329789 -399991 -458388 -87489 -228411 -494634 -260802 -454161 -446322 -231079 -438373 -395665 -244539 -212427 -356660 -347276 -183287 -498374 -21167 -544522 -418533 -288493 -245660 -406103 -406976 -367313 -455555 -117337 -384465 -185697 -160393 -463825 -276852 -181462 -176288 -452816 -102497 -54277 -225791 -361046 -197278 -9857 -227736 -398992 -55868 -170914 -181677 -467803 -560470 -264599 -540372 -559442 -201207 -137227 -267643 -355471 -245431 -555669 -344498 -84783 -193474 -102411 -401860 -119469 -448786 -449990 -568082 -340472 -307573 -231828 -307547 -82052 -15140 -493612 -503972 -386592 -473219 -495557 -159440 -355869 -311531 -209733 -240119 -415048 -296098 -249482 -15663 -151432 -263011 -488539 -463913 -502798 -174276 -495613 -407861 -229304 -146742 -545039 -161202 -295134 -162144 -453317 -52759 -335201 -222903 -20333 -559550 -336049 -346140 -491223 -306611 -102746 -455355 -449921 -477288 -77821 -289712 -452663 -147758 -129571 -490869 -345961 -94501 -160394 -432993 -178796 -372494 -316323 -383435 -194940 -74583 -148911 -518027 -431827 -32724 -158548 -227227 -500330 -54679 -321024 -471175 -252074 -476569 -573258 -337247 -294373 -558661 -148898 -563267 -163112 -411968 -193565 -455210 -349344 -337160 -160456 -255158 -553678 -123843 -549687 -381968 -579471 -100604 -379841 -357526 -197263 -14756 -412639 -210915 -47204 -539251 -166255 -490199 -260363 -91654 -170550 -187888 -97362 -285418 -176993 -292741 -361901 -296988 -223496 -493753 -114907 -151358 -316534 -472509 -499802 -348519 -347747 -58851 -104790 -396779 -130528 -2255 -19624 -526800 -233950 -505945 -131207 -290750 -114090 -196665 -8708 -134688 -394715 -115088 -492196 -530099 -518729 -291572 -421457 -445365 -78929 -415461 -551796 -210002 -207913 -344878 -303893 -149196 -353275 -122413 -553361 -519132 -467135 -431439 -17089 -322119 -228214 -35062 -105689 -366141 -285651 -60409 -472671 -401446 -492846 -21023 -421952 -374100 -265200 -506628 -62298 -243626 -212122 -350648 -409921 -428140 -399212 -388267 -198921 -429246 -202040 -570001 -261346 -61171 -131815 -455448 -82696 -554607 -102174 -386803 -188421 -191846 -209898 -380117 -321064 -119617 -188651 -132210 -244299 -174072 -542910 -378334 -118405 -543347 -183657 -581180 -395289 -64760 -265584 -29573 -493720 -94795 -315601 -416596 -260106 -244019 -463884 -579468 -112085 -300972 -238528 -382542 -57672 -165298 -46889 -289497 -337180 -481252 -7913 -432150 -288161 -403758 -257336 -565331 -346589 -270785 -205670 -231580 -508580 -98871 -239997 -554579 -160057 -404922 -78771 -380756 -171199 -148077 -22892 -145378 -26967 -235200 -176007 -90349 -554377 -189744 -257053 -270515 -66508 -113890 -291983 -558927 -420916 -140908 -58384 -438226 -575776 -106935 -40602 -468993 -494810 -210408 -365685 -483722 -39430 -258793 -272615 -51476 -189919 -443887 -391648 -422670 -445135 -198959 -405529 -459757 -465489 -81827 -262576 -408289 -309237 -76249 -460091 -512630 -45959 -280320 -200492 -404652 -48475 -18480 -457097 -65889 -162256 -265950 -520752 -299082 -51500 -499313 -104906 -35438 -167647 -7274 -387824 -242139 -173166 -399830 -12014 -510642 -154053 -67785 -78170 -514118 -87998 -52703 -203539 -534533 -85926 -274438 -401653 -458790 -509262 -144481 -387515 -246649 -503207 -235131 -501531 -62025 -43286 -272323 -326128 -561889 -167529 -171067 -50778 -301282 -469719 -509388 -480317 -379055 -546428 -192763 -445602 -420882 -232790 -174332 -232865 -292822 -511145 -119502 -312591 -110330 -281353 -116244 -58778 -428079 -64902 -520840 -232054 -473214 -572574 -296684 -351590 -217997 -178761 -71618 -226496 -285212 -381195 -499903 -232849 -468997 -345559 -503097 -578570 -396404 -405223 -578752 -403500 -188958 -504498 -491623 -462929 -525762 -395550 -574227 -240751 -169356 -524694 -40886 -571635 -487774 -86220 -95677 -268987 -502599 -155270 -103855 -125100 -241355 -220214 -391774 -110618 -154587 -134483 -458781 -360877 -465963 -194595 -346934 -127153 -188078 -553869 -102665 -400547 -33759 -42779 -397587 -140295 -151807 -549136 -470288 -89738 -328368 -546934 -164255 -563683 -399988 -360951 -217303 -326781 -546133 -135399 -94666 -330037 -569839 -411070 -497466 -404805 -417854 -318442 -255036 -457230 -346863 -307438 -370448 -5124 -152582 -38118 -12179 -58462 -308420 -329456 -74920 -250368 -186428 -556073 -111806 -361244 -80273 -230964 -156754 -503101 -75173 -389404 -195538 -88848 -286018 -245481 -140929 -533721 -268378 -70048 -315467 -46269 -372807 -192403 -387328 -163033 -481314 -65306 -192529 -321107 -112232 -441216 -412399 -565391 -220670 -61471 -463290 -346707 -67587 -147624 -13031 -396754 -278601 -439426 -42834 -281829 -376209 -353148 -556562 -97579 -217989 -319530 -82551 -235319 -431799 -53892 -52853 -54533 -88897 -225093 -386777 -546742 -273684 -413900 -245447 -577995 -16249 -188414 -485142 -199602 -89258 -109679 -502397 -14494 -13632 -51674 -244999 -305050 -455956 -426795 -560700 -327306 -410301 -343803 -539422 -156740 -527845 -100582 -9941 -466585 -61515 -231895 -157052 -41271 -148128 -141172 -320232 -78565 -539883 -391300 -365182 -322194 -116517 -323496 -473783 -519874 -440706 -361587 -265153 -329946 -342814 -32258 -153510 -194555 -309317 -245006 -300303 -97767 -218224 -370170 -290477 -207178 -456730 -209480 -513775 -199516 -581542 -32524 -416337 -96241 -506279 -422893 -248911 -509855 -355183 -201220 -234914 -333436 -68198 -429074 -328430 -160531 -467854 -280688 -140661 -349525 -267315 -565543 -313162 -25751 -232574 -560358 -505213 -494427 -160308 -287335 -99182 -413260 -558808 -290839 -122954 -229221 -192007 -243189 -117645 -552824 -366111 -102056 -356949 -566298 -97899 -422545 -343769 -13127 -179273 -104486 -37660 -304099 -517570 -20207 -36484 -36492 -155974 -107257 -534019 -522371 -222825 -96183 -509227 -302260 -95078 -280918 -367582 -317033 -347982 -73209 -290521 -187243 -425151 -483723 -573796 -187249 -144114 -132992 -35887 -546067 -426532 -45626 -461805 -129989 -541478 -485489 -578498 -485483 -144784 -248224 -372362 -92050 -423519 -473118 -177207 -105455 -276434 -157767 -384335 -509497 -338191 -224010 -327388 -96988 -43376 -67867 -320743 -555197 -104453 -14439 -512194 -396387 -252559 -108953 -461262 -66320 -97946 -238065 -306139 -572408 -577864 -81004 -464526 -89378 -193389 -259049 -85665 -381134 -412419 -308947 -557510 -502084 -288290 -254609 -188752 -439525 -13980 -140513 -240173 -305268 -38678 -394050 -402926 -364079 -159260 -293034 -55429 -289640 -291028 -211120 -48050 -93887 -361029 -486026 -388374 -207803 -540174 -530630 -430359 -36420 -120099 -199764 -492911 -84498 -200882 -139843 -4975 -421209 -259513 -520324 -211317 -236457 -419344 -3867 -287846 -50434 -26624 -507235 -16238 -103705 -497555 -440060 -175825 -245460 -308276 -178535 -391735 -206391 -201550 -400945 -194634 -262360 -554142 -407574 -225225 -246057 -498627 -486172 -226571 -461751 -459733 -345869 -503841 -286460 -45644 -22861 -285599 -580284 -569565 -286778 -150024 -542101 -484075 -538153 -20470 -128034 -544120 -357109 -450728 -550968 -326230 -558809 -76334 -555387 -47121 -523978 -11081 -378134 -116279 -364884 -488250 -551957 -322824 -545564 -255573 -286327 -355453 -361933 -434897 -32597 -226761 -166482 -557564 -208166 -232115 -283520 -137395 -555894 -103509 -174284 -458313 -316147 -344059 -370701 -548930 -89894 -373662 -572095 -19324 -574411 -45746 -480122 -63950 -92339 -201111 -157053 -401539 -427956 -339099 -274651 -159537 -556101 -323399 -564337 -514915 -556025 -66427 -322357 -173737 -369128 -420230 -45176 -509675 -374677 -272311 -109797 -384723 -383678 -453040 -91080 -301634 -533003 -40361 -221605 -216228 -104002 -161011 -146123 -214421 -496252 -264948 -9759 -138856 -316189 -145734 -50411 -325157 -259099 -516856 -529668 -135976 -467130 -367433 -385598 -520933 -102805 -30066 -436696 -216837 -380754 -350457 -126974 -565374 -73832 -214703 -110501 -380609 -135872 -140231 -251816 -133836 -398866 -230362 -426815 -2240 -51484 -546325 -224093 -221190 -525024 -238806 -99908 -165795 -109146 -537727 -496571 -183803 -211175 -433845 -168692 -526394 -368402 -256309 -468972 -139169 -398440 -171678 -547341 -64332 -533589 -483249 -406000 -330348 -439188 -572886 -252829 -242724 -139127 -404568 -45809 -52257 -458727 -334509 -559665 -60992 -290896 -503106 -27972 -536891 -410855 -31202 -457882 -403315 -87399 -395291 -322141 -226377 -202799 -420826 -553034 -212077 -97693 -266370 -101656 -504142 -342933 -87567 -342060 -268854 -437028 -20175 -198625 -405047 -382374 -338291 -403975 -527906 -322429 -545550 -140043 -107389 -74059 -315621 -110138 -78381 -295576 -494438 -106335 -472349 -15818 -162358 -366484 -44604 -66524 -118606 -366873 -270721 -556478 -350789 -298628 -163314 -262800 -459428 -491725 -285421 -406332 -498280 -34535 -524282 -315744 -226592 -218294 -459141 -242034 -114164 -293733 -248242 -452881 -441496 -54358 -177489 -372861 -349489 -483941 -572802 -356494 -193875 -146570 -58253 -21338 -6220 -341933 -533368 -1818 -428248 -293026 -227656 -193021 -326938 -512966 -226020 -343059 -249720 -540106 -375278 -300023 -126512 -517135 -472540 -361439 -132702 -503294 -109537 -540669 -332007 -245266 -313999 -10386 -225715 -311567 -103837 -302405 -248616 -102654 -155087 -124756 -379659 -569272 -160166 -428234 -422280 -174425 -133412 -174503 -216581 -345063 -52949 -69536 -216161 -272728 -200870 -120792 -193480 -493923 -445567 -558539 -51938 -422706 -416271 -244160 -437898 -327352 -305480 -349459 -522418 -485219 -225133 -361400 -546569 -190015 -348216 -421822 -457683 -178683 -40894 -234526 -465074 -518725 -168096 -210190 -139605 -35195 -463640 -286770 -141651 -112022 -532552 -325327 -227224 -17272 -84163 -331475 -126065 -289309 -8583 -52952 -189427 -579693 -437947 -187565 -215982 -356424 -453731 -463522 -372316 -251797 -70187 -280515 -556608 -341635 -391067 -469480 -476298 -57917 -146672 -122747 -394328 -12209 -80013 -573291 -278449 -129659 -579560 -557190 -227468 -334782 -51157 -23774 -9426 -86582 -39211 -275751 -131597 -51250 -357255 -9041 -346482 -9647 -157019 -409016 -273416 -114414 -298172 -388854 -275025 -58079 -518034 -503518 -146710 -120632 -474680 -303713 -259097 -479630 -208318 -437298 -173704 -361831 -371638 -344279 -230175 -72507 -417980 -72621 -163057 -92894 -543525 -577364 -263696 -472732 -66027 -391584 -197745 -131019 -65604 -91318 -535934 -212646 -576354 -482071 -160556 -120129 -7260 -344881 -447548 -318193 -30383 -527002 -34904 -35677 -526222 -105261 -401897 -399452 -25660 -524595 -384512 -117543 -514600 -268944 -112664 -222340 -569058 -495332 -192153 -75591 -286711 -174888 -577065 -25508 -169972 -401820 -425475 -290700 -173091 -559101 -122418 -244124 -198645 -325519 -276437 -528276 -146614 -45574 -417804 -326420 -250594 -27353 -310407 -370103 -274957 -561160 -167598 -397166 -257458 -404546 -148392 -373396 -62230 -493522 -563665 -274240 -269815 -79024 -527427 -84674 -486788 -267690 -443347 -149304 -412285 -207041 -412916 -10764 -151338 -299000 -17882 -475510 -398188 -558213 -70493 -180779 -347210 -280211 -58146 -379022 -504125 -537604 -464858 -329573 -568623 -228309 -454444 -552775 -557884 -435671 -168706 -142257 -571437 -574845 -387773 -321008 -574208 -405811 -375426 -321887 -256852 -433554 -517029 -125870 -80395 -497139 -490008 -405279 -571857 -225738 -514913 -456239 -499402 -96440 -487607 -370999 -319617 -370233 -60760 -352703 -478575 -84170 -134112 -77689 -185036 -73738 -547502 -104782 -213276 -136908 -436273 -442149 -355000 -374061 -249884 -105711 -136464 -146997 -76351 -388487 -99115 -124135 -24721 -132931 -1149 -182403 -386089 -81691 -480657 -441522 -60989 -268000 -55840 -514321 -577959 -359638 -457986 -533596 -60332 -367082 -772 -535842 -473541 -270677 -409009 -259216 -302318 -117036 -331372 -231125 -384486 -405214 -20760 -579760 -172995 -359110 -83110 -410068 -109916 -328757 -299261 -19028 -515660 -40757 -10256 -442695 -553097 -185903 -74388 -425120 -241326 -299609 -29397 -328728 -283881 -344029 -367336 -27075 -163628 -127263 -488979 -460147 -473050 -405762 -221547 -131581 -561187 -406489 -140696 -452721 -530466 -118965 -398803 -218365 -298738 -19441 -521550 -120157 -498687 -4754 -365866 -70865 -235156 -133386 -142742 -221183 -262391 -567053 -520982 -121349 -448779 -440354 -3983 -578993 -519691 -160703 -103307 -300408 -137106 -488377 -523660 -318022 -132578 -302520 -153040 -408817 -145227 -311190 -159662 -202923 -256775 -359864 -384848 -336404 -185303 -421703 -362682 -464622 -246590 -422729 -165500 -42563 -219216 -520232 -95063 -265547 -532686 -290558 -112591 -448211 -315281 -545475 -225850 -232460 -82740 -272880 -347254 -122047 -352151 -541486 -97249 -200252 -544782 -499571 -379014 -303534 -479909 -305464 -323682 -181524 -273855 -190783 -567801 -119752 -241503 -536429 -327323 -128756 -349868 -500495 -372260 -315824 -484986 -364993 -124759 -300124 -329319 -68628 -14549 -121897 -506595 -115709 -199610 -230150 -31717 -139549 -222332 -534161 -360393 -541664 -507167 -286523 -158660 -66926 -195750 -80022 -589 -252220 -47255 -247014 -49881 -455005 -232453 -445722 -516805 -544122 -541917 -469356 -370042 -130522 -502163 -307866 -408894 -524247 -52233 -177861 -348881 -357943 -295303 -475389 -431691 -61316 -143998 -503483 -340155 -488785 -133636 -133567 -251627 -470095 -34873 -88815 -261178 -468612 -127477 -157960 -15687 -303089 -572331 -456708 -190515 -126131 -239194 -332074 -129765 -107167 -478184 -421833 -359715 -112440 -331317 -74492 -505386 -247839 -534210 -134503 -422700 -352111 -98674 -546219 -520508 -503008 -461953 -101913 -362092 -22103 -359128 -316666 -335579 -414750 -297980 -365652 -53635 -547601 -97589 -570515 -7125 -99828 -321437 -80671 -426275 -294883 -212605 -424293 -338108 -25005 -6949 -234291 -428399 -7149 -343076 -575287 -431848 -307611 -293909 -542511 -564739 -573843 -356878 -472864 -336793 -121904 -161060 -254004 -269873 -216428 -77172 -346517 -498555 -203690 -348973 -117704 -552672 -275270 -208107 -314016 -427518 -278134 -53420 -318777 -238980 -350614 -467315 -61233 -272188 -550797 -125051 -553965 -187286 -282912 -102532 -156076 -467848 -130875 -531585 -523470 -507684 -332582 -438989 -489209 -125944 -127474 -371957 -570349 -283286 -541635 -547106 -253630 -388677 -572525 -542302 -554537 -367205 -228300 -443498 -356432 -123946 -490441 -211063 -224542 -116574 -434510 -33116 -353136 -134167 -128291 -542510 -433963 -147453 -365766 -374806 -336600 -38238 -165476 -535578 -127788 -157099 -173640 -114348 -496722 -58141 -467296 -235864 -5154 -22775 -422536 -136820 -453438 -446359 -41990 -422240 -39267 -391392 -233825 -308504 -478250 -87328 -4079 -127074 -267709 -377635 -353231 -185768 -487897 -124215 -249757 -341681 -557552 -280733 -374734 -281601 -456420 -222266 -491947 -432732 -467157 -94025 -410328 -428291 -397639 -163528 -234697 -557573 -208363 -515962 -358658 -373075 -438995 -425672 -450169 -216103 -254638 -288591 -53626 -43417 -372252 -5038 -218357 -120860 -399349 -485509 -530261 -477087 -352302 -96075 -495443 -133928 -197175 -134074 -212553 -448181 -152000 -254277 -105734 -75481 -343662 -479350 -554347 -71090 -297426 -22176 -277622 -469235 -163041 -221272 -154263 -89296 -68411 -192871 -183217 -258141 -53058 -540529 -566414 -560948 -254535 -246076 -135972 -420069 -431023 -343643 -32682 -515176 -222635 -377155 -547041 -513283 -26017 -366096 -252133 -138078 -25685 -321798 -549361 -14088 -423048 -570810 -374974 -447501 -492544 -554046 -575357 -420791 -6019 -340451 -66800 -565575 -148055 -330432 -483038 -455004 -288765 -11034 -86988 -347142 -450559 -543581 -293757 -556901 -533032 -333020 -260266 -22420 -13948 -512657 -214124 -231236 -177149 -560879 -491793 -35767 -312878 -118542 -450596 -423773 -48653 -224523 -509577 -462677 -75405 -350023 -452122 -42008 -302555 -382309 -468483 -368684 -372580 -31333 -153697 -124876 -330023 -315672 -53990 -136533 -82815 -356836 -414821 -268717 -7333 -77544 -525373 -371042 -227048 -576327 -419309 -239773 -8119 -424135 -297425 -222711 -489909 -393995 -31019 -539326 -517612 -102461 -199989 -483374 -44952 -103863 -528980 -441543 -85381 -247234 -50924 -483994 -87456 -424271 -356091 -534669 -378831 -560662 -298773 -257896 -498274 -305800 -40517 -183949 -276840 -84442 -297620 -298252 -119088 -233315 -283977 -345154 -287649 -427311 -63399 -4700 -463611 -224104 -209388 -431655 -364190 -28864 -412455 -283290 -228541 -422200 -985 -133596 -323853 -503081 -130732 -224675 -199688 -230862 -21396 -485390 -1532 -125778 -235541 -370478 -522478 -514292 -384338 -531707 -178746 -532747 -62915 -519491 -140691 -112093 -358024 -263687 -297595 -506085 -102446 -325768 -29558 -222054 -466965 -316254 -546500 -216785 -194184 -464390 -348371 -231582 -208995 -464339 -308856 -340946 -214604 -570586 -182227 -248441 -89078 -376310 -73450 -115924 -308235 -15994 -8749 -429679 -37751 -122040 -284286 -388707 -248163 -11320 -427997 -282062 -237600 -376751 -223314 -86215 -12443 -163255 -564940 -462640 -522713 -306303 -460675 -126833 -26201 -224757 -357899 -546782 -96427 -480944 -479556 -569273 -520528 -190690 -344832 -462466 -270354 -559776 -279259 -280909 -227781 -163798 -491098 -439658 -416088 -107375 -74132 -379800 -511654 -346687 -226161 -578849 -544272 -146149 -570624 -178299 -126671 -356380 -530766 -175954 -158798 -422095 -55780 -512276 -560626 -187329 -513125 -347216 -306486 -161840 -180917 -188192 -421437 -93120 -324891 -252216 -488476 -578347 -101959 -10693 -170038 -213586 -210439 -469202 -381463 -343248 -127785 -287328 -538690 -16382 -293022 -112378 -435785 -56092 -381504 -284365 -406129 -233119 -53629 -188509 -191053 -81056 -82252 -538319 -38439 -181948 -439710 -529344 -434035 -342958 -563882 -37734 -364743 -330986 -546226 -463211 -62210 -442724 -232241 -293858 -119345 -61953 -577033 -522015 -381587 -350107 -4936 -511307 -228771 -177811 -231450 -176168 -84540 -259408 -264238 -539738 -255827 -459382 -221105 -431742 -204337 -227741 -336356 -37655 -167159 -59352 -165937 -53956 -378712 -88462 -495786 -542938 -566498 -367228 -157577 -442661 -62363 -390689 -480664 -521540 -414249 -20571 -160855 -451683 -156832 -570045 -326542 -568276 -568717 -563311 -113579 -218268 -546095 -160661 -341118 -150649 -462632 -198972 -220025 -61720 -430681 -524011 -457217 -40064 -285583 -314493 -78023 -470882 -298722 -555597 -489829 -314779 -367818 -138503 -243737 -580255 -444565 -386677 -190841 -493074 -234347 -466988 -227033 -519039 -351554 -390585 -443303 -140983 -81079 -538005 -169757 -368780 -457322 -341804 -409116 -181805 -284292 -551358 -344548 -503569 -336587 -417055 -522315 -58705 -148955 -375530 -474934 -577893 -28881 -360772 -445267 -244737 -355777 -72811 -190788 -54513 -243075 -518551 -487530 -292169 -69293 -397303 -129285 -429996 -109532 -53802 -340573 -91280 -535602 -270908 -381925 -549220 -488573 -47131 -32735 -117525 -279085 -43961 -188906 -394677 -395 -185201 -189365 -127596 -32712 -504810 -3703 -182874 -146981 -306755 -453093 -520503 -169808 -225670 -91063 -348584 -461802 -572555 -185922 -131497 -46736 -536006 -256505 -214975 -13445 -350736 -98115 -50304 -361180 -511333 -564820 -429717 -222500 -40083 -538230 -349438 -371250 -528578 -240418 -302380 -261758 -535809 -308388 -578878 -509451 -46919 -562592 -499950 -90374 -318146 -195353 -355325 -314515 -237277 -203024 -238911 -32039 -145591 -16030 -135411 -229350 -421757 -48034 -183704 -307292 -97974 -275999 -448256 -451915 -119113 -143503 -494141 -50124 -306553 -35526 -255279 -560908 -247264 -367599 -192782 -511324 -574350 -67569 -204360 -111907 -2839 -513971 -245201 -185240 -339468 -540101 -539673 -194425 -22168 -520150 -301595 -96006 -68286 -131280 -356662 -182441 -284749 -107108 -49761 -386718 -55244 -187990 -248678 -147721 -425727 -360350 -310797 -76765 -400489 -247639 -279864 -44699 -356145 -69138 -445041 -560598 -165464 -536343 -7818 -322831 -334760 -451463 -348730 -285967 -286353 -201887 -166165 -359 -465591 -519359 -550444 -402711 -3661 -132706 -534983 -306281 -150317 -15978 -580029 -496090 -267127 -210980 -384015 -222559 -2235 -255649 -278168 -440840 -27326 -202562 -230268 -362712 -1573 -107661 -464515 -373132 -447242 -547440 -43613 -200143 -260883 -250901 -64693 -408480 -204757 -319933 -147471 -381332 -518197 -27656 -260257 -434580 -159203 -568630 -497441 -499597 -60179 -574804 -343254 -501762 -220704 -524536 -86946 -456046 -62937 -49633 -144305 -475593 -478553 -574145 -63648 -3794 -303177 -1340 -82835 -371427 -156747 -448694 -219567 -75095 -242615 -492077 -132776 -199125 -349622 -195754 -455548 -181873 -138185 -338044 -362797 -180953 -505826 -69773 -304834 -162580 -154090 -519853 -319687 -132328 -27969 -52166 -100547 -568131 -415218 -348045 -478159 -402869 -10211 -26547 -551692 -105432 -313340 -182348 -383419 -570947 -345353 -226883 -255784 -214199 -262262 -283261 -449708 -299970 -392391 -245997 -330410 -343571 -519542 -37470 -42144 -342521 -498537 -10935 -443860 -512648 -146099 -98599 -123932 -489861 -262895 -184700 -218587 -363581 -21001 -481404 -249356 -64240 -492349 -199236 -481064 -353405 -116479 -132024 -138768 -524665 -434511 -326970 -138784 -340368 -312081 -366615 -171942 -21232 -473850 -93686 -295574 -51054 -162692 -174091 -20070 -270066 -492816 -20904 -484500 -147140 -242972 -420081 -63563 -261712 -316396 -49413 -520787 -510955 -393840 -142487 -19817 -261180 -413736 -230619 -484614 -337011 -496575 -4338 -552545 -5601 -75426 -568863 -184227 -170629 -438567 -505132 -541353 -284674 -322567 -182423 -312051 -18896 -40471 -321725 -188850 -37119 -95569 -187362 -397133 -528972 -487131 -174989 -370325 -223554 -385633 -103485 -537574 -63240 -256566 -86467 -401092 -486968 -308441 -280017 -527464 -131965 -310479 -125556 -220160 -532963 -310052 -107963 -293841 -388534 -45603 -368949 -391825 -5107 -569705 -231549 -250108 -152933 -206433 -358817 -434006 -283904 -152808 -539975 -24629 -410231 -13465 -502318 -51961 -445594 -209062 -38726 -295420 -430079 -240147 -561512 -35795 -102589 -505619 -565469 -271772 -520561 -372300 -178807 -492805 -1083 -303704 -125635 -217521 -278032 -208688 -335325 -140435 -313990 -143822 -320857 -549230 -76844 -424219 -463876 -243199 -2988 -215170 -30012 -377738 -408568 -490624 -404839 -138316 -157206 -404461 -122934 -263346 -21327 -99913 -67975 -339676 -391891 -365305 -337055 -233834 -125524 -46869 -32577 -304744 -104176 -167356 -210404 -307989 -217223 -196046 -454414 -16356 -244487 -543660 -197461 -199681 -476787 -455085 -307074 -260547 -107468 -334769 -29437 -166837 -53838 -502979 -82678 -288860 -535523 -311950 -237723 -98656 -223123 -273930 -58057 -544334 -324857 -198043 -535326 -316505 -12991 -576820 -43611 -107839 -275749 -456695 -78188 -375786 -466239 -184830 -537128 -434513 -244344 -374576 -69140 -434247 -555009 -510857 -220819 -20598 -99416 -74967 -533129 -515577 -213361 -330974 -548848 -431557 -503278 -130043 -402570 -320554 -559884 -252629 -364596 -423484 -271230 -105552 -143143 -285751 -49994 -204162 -80646 -381393 -123415 -118417 -30932 -425412 -388130 -551243 -468337 -484893 -25014 -174390 -463781 -124647 -60823 -361964 -425702 -575110 -532390 -230881 -84592 -189997 -221307 -361472 -32364 -71918 -316365 -492378 -234251 -48504 -418070 -89884 -562045 -506552 -66360 -122962 -262605 -529939 -345229 -294853 -344397 -56091 -8599 -459823 -175785 -226128 -259983 -354515 -379144 -384995 -205253 -116786 -441432 -448810 -83452 -465129 -506906 -90616 -551959 -406404 -157891 -362090 -439630 -45099 -61960 -478430 -489605 -127050 -579872 -475798 -64510 -447733 -33066 -102848 -538819 -323760 -200401 -179765 -251317 -239376 -83836 -578092 -522452 -393056 -278848 -27787 -377239 -473427 -83065 -377005 -576539 -248019 -473370 -536369 -92648 -332461 -437609 -274800 -388846 -323048 -193407 -541898 -480140 -46526 -26432 -339738 -325991 -37705 -528033 -542922 -313420 -190463 -531000 -454907 -26448 -238199 -476652 -457147 -364256 -72632 -430380 -315448 -353320 -18158 -91527 -454252 -546987 -386370 -38064 -19763 -64152 -453216 -55223 -361860 -522566 -509531 -438432 -31164 -163290 -389197 -333440 -173464 -447842 -381615 -99961 -156126 -103134 -394940 -165638 -261706 -378311 -534081 -373848 -401642 -338019 -378096 -289610 -547421 -174672 -133343 -191360 -293751 -520892 -145214 -167668 -37456 -460962 -465267 -292804 -347529 -203661 -10766 -27371 -203845 -155736 -136715 -463588 -26640 -547612 -131453 -184274 -442456 -265085 -223256 -129420 -23019 -536467 -194532 -127585 -392637 -330408 -524775 -31993 -433924 -502852 -553129 -559364 -297343 -71360 -225537 -271148 -345499 -475893 -237463 -5278 -501243 -413235 -444236 -541071 -380088 -468063 -94858 -225913 -295614 -210276 -170975 -205570 -422375 -550365 -308702 -484627 -565031 -98979 -480345 -579548 -272673 -436875 -287874 -16502 -274917 -281809 -442968 -289263 -347766 -160933 -84533 -266409 -122199 -396200 -30958 -504541 -1591 -89432 -387150 -306383 -15260 -154515 -50752 -166913 -102644 -100196 -160278 -349579 -442536 -17923 -310564 -62020 -152004 -578330 -126299 -527025 -83494 -226400 -268435 -445334 -310391 -505156 -19157 -44677 -318171 -447765 -354369 -527486 -329939 -184771 -134856 -467675 -517133 -89697 -447080 -70685 -144938 -519673 -485758 -454957 -564851 -189451 -408757 -192616 -280734 -305060 -243946 -99179 -303971 -170519 -48917 -549965 -300245 -384101 -576607 -186709 -516341 -241668 -133470 -134811 -500825 -464689 -29833 -343820 -213429 -387434 -279305 -444207 -210777 -372043 -189868 -572229 -8495 -370090 -450282 -277080 -199158 -109612 -567708 -245659 -485129 -268363 -23448 -5352 -235597 -6871 -348720 -94113 -314613 -63729 -114458 -215394 -460460 -240387 -398726 -135604 -571728 -415770 -286908 -138151 -146272 -344094 -345209 -241187 -282768 -113037 -545583 -219283 -145873 -285957 -489235 -157271 -197458 -502671 -499845 -334884 -79084 -505573 -115618 -561491 -354202 -279838 -190734 -134738 -269450 -482784 -144610 -52774 -290659 -440646 -25807 -442952 -159215 -318224 -73445 -211653 -527960 -401862 -431026 -488755 -292278 -400554 -272630 -382668 -470298 -166426 -129645 -28820 -161227 -417696 -560677 -283216 -28978 -310302 -154419 -230450 -328289 -73118 -104691 -15085 -405574 -510548 -470005 -102928 -569249 -413126 -77282 -96732 -359020 -42182 -250875 -106206 -354929 -320796 -453341 -237318 -254834 -137265 -399865 -292685 -152252 -319579 -81484 -16599 -162257 -351034 -396051 -502275 -308278 -34483 -13333 -320290 -321579 -349794 -99219 -200162 -369470 -487583 -62703 -251639 -138246 -157170 -477112 -283963 -74860 -307057 -364075 -295491 -34757 -400161 -170194 -120874 -492817 -3817 -183973 -135436 -512989 -114744 -379210 -201072 -293785 -578385 -237420 -7888 -18224 -155317 -522406 -441440 -110482 -173400 -183348 -552504 -475660 -166948 -147025 -443259 -578792 -245227 -546687 -474519 -393284 -249668 -87493 -151651 -100306 -540466 -546556 -212675 -282942 -21310 -385535 -7304 -303409 -386116 -574297 -514550 -217133 -533553 -447152 -578703 -45392 -166205 -180154 -25143 -338802 -330110 -261389 -343506 -442726 -285388 -554934 -421316 -479912 -85192 -34874 -487266 -226173 -20748 -360660 -574509 -543364 -1554 -125539 -566931 -312889 -466945 -444804 -257187 -568587 -427160 -71123 -563849 -138589 -162841 -129663 -107226 -140686 -321663 -437117 -179808 -321718 -62398 -16497 -468933 -219841 -355430 -293554 -293044 -109516 -485887 -490620 -579893 -427135 -31636 -217919 -432441 -314396 -119802 -393682 -201764 -146193 -116358 -84825 -208311 -419774 -177468 -72052 -142585 -519598 -464006 -556083 -412136 -169361 -442929 -84567 -549932 -75560 -74656 -93314 -393838 -383018 -372433 -431281 -556278 -5513 -108503 -500478 -148588 -138713 -368153 -22646 -303778 -270758 -276706 -275429 -492025 -169111 -494328 -35891 -70258 -400528 -165229 -460494 -269311 -307658 -98283 -369294 -319345 -414578 -541550 -425388 -129855 -99477 -383073 -387906 -293124 -155873 -549224 -266021 -52869 -1584 -421902 -498535 -277235 -153013 -452013 -553561 -138040 -20820 -58483 -423506 -569001 -325153 -383039 -213421 -38825 -453283 -384661 -127702 -238147 -104893 -577826 -64974 -240655 -459153 -145665 -49810 -65008 -545385 -125070 -46433 -143329 -429174 -52947 -321314 -253341 -157365 -453162 -111910 -339019 -239575 -362219 -80652 -247317 -460286 -365724 -160875 -372220 -483389 -572181 -146190 -580975 -54761 -348488 -416104 -468778 -18833 -251537 -234366 -510078 -14723 -338595 -153797 -513098 -467138 -404618 -261982 -545730 -135846 -108244 -562557 -180524 -227370 -341856 -131743 -255691 -497878 -68878 -430640 -441473 -347664 -214369 -347018 -225238 -421762 -317024 -6180 -172004 -303101 -22488 -193494 -199346 -409627 -315350 -263463 -190722 -523292 -363902 -573778 -437290 -389812 -517082 -145073 -37907 -489763 -456261 -270386 -508917 -566823 -543897 -362482 -130966 -66632 -181962 -274613 -135708 -549746 -323766 -366714 -353295 -318813 -153307 -213693 -293378 -149446 -199927 -580543 -331727 -238488 -472833 -308645 -424225 -228746 -110435 -495377 -240646 -274491 -130921 -140006 -4688 -115241 -76962 -66650 -47718 -224991 -434187 -272048 -11169 -158222 -154000 -507436 -443499 -109937 -309692 -534018 -22797 -163339 -168683 -210098 -246069 -137954 -143320 -262587 -414795 -226938 -536831 -128791 -459590 -50514 -30067 -317479 -378655 -229968 -522702 -11122 -515266 -136600 -224509 -149912 -97656 -120747 -349480 -155199 -528731 -523807 -168544 -325664 -229981 -434410 -431208 -508996 -63791 -89225 -513690 -136740 -224364 -515424 -508302 -418175 -465552 -439907 -272097 -451087 -396304 -342273 -52507 -300066 -380089 -326248 -167906 -37846 -262993 -60090 -499249 -90432 -74456 -264660 -325598 -480985 -245411 -425644 -224724 -475439 -246478 -487438 -563731 -441854 -522665 -245915 -85747 -315162 -108761 -407521 -388528 -389453 -298331 -447791 -368820 -440034 -305677 -122208 -182369 -543531 -151820 -63650 -457580 -563381 -320899 -14869 -137260 -61925 -376307 -80367 -269089 -203705 -274835 -267321 -418106 -471273 -74037 -227855 -519758 -89045 -321217 -324203 -479129 -503431 -368528 -527718 -278579 -13525 -291582 -301837 -31667 -68120 -14007 -114158 -124262 -33626 -53949 -187585 -192247 -208844 -212766 -318671 -575012 -439339 -364073 -419624 -178078 -427783 -302159 -339368 -190680 -23807 -288579 -312720 -15778 -553558 -571834 -574376 -122161 -493815 -472376 -483432 -149123 -51628 -264628 -26609 -23696 -485081 -441323 -451679 -42055 -378795 -86439 -366493 -520996 -332869 -18014 -554523 -83476 -6040 -421834 -424392 -308160 -335233 -249809 -349098 -358090 -187349 -61782 -35498 -386514 -207108 -578418 -84447 -104108 -126107 -211674 -111909 -490708 -477025 -206757 -556205 -142484 -454296 -464366 -358254 -215482 -468548 -82680 -100909 -405432 -85764 -94651 -63973 -8131 -288592 -257470 -47597 -321557 -34520 -134066 -246701 -317797 -282365 -78176 -29577 -311075 -331937 -190395 -5802 -245112 -111032 -140556 -199127 -376491 -305253 -300375 -545903 -357782 -377911 -74963 -329336 -25057 -3244 -252020 -293474 -171050 -239306 -189772 -238090 -160031 -36761 -445675 -252716 -152214 -239466 -55155 -479829 -420281 -445812 -118106 -434576 -451104 -316708 -438535 -300322 -167952 -390072 -487220 -20247 -9400 -43944 -35770 -487351 -425462 -212203 -9668 -8981 -574241 -332096 -535563 -192944 -498733 -276151 -550645 -507037 -9769 -404249 -236747 -376416 -306415 -45966 -191296 -576875 -493932 -225075 -536444 -79920 -561681 -60700 -99874 -219437 -509819 -466665 -579326 -428739 -394611 -263083 -379554 -279391 -178516 -133690 -77396 -300137 -6861 -435359 -314108 -444152 -500139 -92749 -89188 -300233 -414201 -443204 -211097 diff --git a/research/object_detection/data/oid_bbox_trainable_label_map.pbtxt b/research/object_detection/data/oid_bbox_trainable_label_map.pbtxt deleted file mode 100644 index 863e4f31d71..00000000000 --- a/research/object_detection/data/oid_bbox_trainable_label_map.pbtxt +++ /dev/null @@ -1,2725 +0,0 @@ -item { - name: "/m/01g317" - id: 1 - display_name: "Person" -} -item { - name: "/m/09j2d" - id: 2 - display_name: "Clothing" -} -item { - name: "/m/04yx4" - id: 3 - display_name: "Man" -} -item { - name: "/m/0dzct" - id: 4 - display_name: "Face" -} -item { - name: "/m/07j7r" - id: 5 - display_name: "Tree" -} -item { - name: "/m/05s2s" - id: 6 - display_name: "Plant" -} -item { - name: "/m/03bt1vf" - id: 7 - display_name: "Woman" -} -item { - name: "/m/07yv9" - id: 8 - display_name: "Vehicle" -} -item { - name: "/m/0cgh4" - id: 9 - display_name: "Building" -} -item { - name: "/m/01prls" - id: 10 - display_name: "Land vehicle" -} -item { - name: "/m/09j5n" - id: 11 - display_name: "Footwear" -} -item { - name: "/m/05r655" - id: 12 - display_name: "Girl" -} -item { - name: "/m/0jbk" - id: 13 - display_name: "Animal" -} -item { - name: "/m/0k4j" - id: 14 - display_name: "Car" -} -item { - name: "/m/02wbm" - id: 15 - display_name: "Food" -} -item { - name: "/m/083wq" - id: 16 - display_name: "Wheel" -} -item { - name: "/m/0c9ph5" - id: 17 - display_name: "Flower" -} -item { - name: "/m/0c_jw" - id: 18 - display_name: "Furniture" -} -item { - name: "/m/0d4v4" - id: 19 - display_name: "Window" -} -item { - name: "/m/03jm5" - id: 20 - display_name: "House" -} -item { - name: "/m/01bl7v" - id: 21 - display_name: "Boy" -} -item { - name: "/m/0463sg" - id: 22 - display_name: "Fashion accessory" -} -item { - name: "/m/04bcr3" - id: 23 - display_name: "Table" -} -item { - name: "/m/0jyfg" - id: 24 - display_name: "Glasses" -} -item { - name: "/m/01xyhv" - id: 25 - display_name: "Suit" -} -item { - name: "/m/08dz3q" - id: 26 - display_name: "Auto part" -} -item { - name: "/m/015p6" - id: 27 - display_name: "Bird" -} -item { - name: "/m/05y5lj" - id: 28 - display_name: "Sports equipment" -} -item { - name: "/m/01d40f" - id: 29 - display_name: "Dress" -} -item { - name: "/m/0bt9lr" - id: 30 - display_name: "Dog" -} -item { - name: "/m/01lrl" - id: 31 - display_name: "Carnivore" -} -item { - name: "/m/02p0tk3" - id: 32 - display_name: "Human body" -} -item { - name: "/m/0fly7" - id: 33 - display_name: "Jeans" -} -item { - name: "/m/04szw" - id: 34 - display_name: "Musical instrument" -} -item { - name: "/m/0271t" - id: 35 - display_name: "Drink" -} -item { - name: "/m/019jd" - id: 36 - display_name: "Boat" -} -item { - name: "/m/03q69" - id: 37 - display_name: "Hair" -} -item { - name: "/m/0h9mv" - id: 38 - display_name: "Tire" -} -item { - name: "/m/04hgtk" - id: 39 - display_name: "Head" -} -item { - name: "/m/01yrx" - id: 40 - display_name: "Cat" -} -item { - name: "/m/01rzcn" - id: 41 - display_name: "Watercraft" -} -item { - name: "/m/01mzpv" - id: 42 - display_name: "Chair" -} -item { - name: "/m/0199g" - id: 43 - display_name: "Bike" -} -item { - name: "/m/01fdzj" - id: 44 - display_name: "Tower" -} -item { - name: "/m/04rky" - id: 45 - display_name: "Mammal" -} -item { - name: "/m/079cl" - id: 46 - display_name: "Skyscraper" -} -item { - name: "/m/0dzf4" - id: 47 - display_name: "Arm" -} -item { - name: "/m/0138tl" - id: 48 - display_name: "Toy" -} -item { - name: "/m/06msq" - id: 49 - display_name: "Sculpture" -} -item { - name: "/m/03xxp" - id: 50 - display_name: "Invertebrate" -} -item { - name: "/m/0hg7b" - id: 51 - display_name: "Microphone" -} -item { - name: "/m/01n5jq" - id: 52 - display_name: "Poster" -} -item { - name: "/m/03vt0" - id: 53 - display_name: "Insect" -} -item { - name: "/m/0342h" - id: 54 - display_name: "Guitar" -} -item { - name: "/m/0k0pj" - id: 55 - display_name: "Nose" -} -item { - name: "/m/02dl1y" - id: 56 - display_name: "Hat" -} -item { - name: "/m/04brg2" - id: 57 - display_name: "Tableware" -} -item { - name: "/m/02dgv" - id: 58 - display_name: "Door" -} -item { - name: "/m/01bqk0" - id: 59 - display_name: "Bicycle wheel" -} -item { - name: "/m/017ftj" - id: 60 - display_name: "Sunglasses" -} -item { - name: "/m/052lwg6" - id: 61 - display_name: "Baked goods" -} -item { - name: "/m/014sv8" - id: 62 - display_name: "Eye" -} -item { - name: "/m/0270h" - id: 63 - display_name: "Dessert" -} -item { - name: "/m/0283dt1" - id: 64 - display_name: "Mouth" -} -item { - name: "/m/0k5j" - id: 65 - display_name: "Aircraft" -} -item { - name: "/m/0cmf2" - id: 66 - display_name: "Airplane" -} -item { - name: "/m/07jdr" - id: 67 - display_name: "Train" -} -item { - name: "/m/032b3c" - id: 68 - display_name: "Jacket" -} -item { - name: "/m/033rq4" - id: 69 - display_name: "Street light" -} -item { - name: "/m/0k65p" - id: 70 - display_name: "Hand" -} -item { - name: "/m/01ww8y" - id: 71 - display_name: "Snack" -} -item { - name: "/m/0zvk5" - id: 72 - display_name: "Helmet" -} -item { - name: "/m/07mhn" - id: 73 - display_name: "Trousers" -} -item { - name: "/m/04dr76w" - id: 74 - display_name: "Bottle" -} -item { - name: "/m/03fp41" - id: 75 - display_name: "Houseplant" -} -item { - name: "/m/03k3r" - id: 76 - display_name: "Horse" -} -item { - name: "/m/01y9k5" - id: 77 - display_name: "Desk" -} -item { - name: "/m/0cdl1" - id: 78 - display_name: "Palm tree" -} -item { - name: "/m/0f4s2w" - id: 79 - display_name: "Vegetable" -} -item { - name: "/m/02xwb" - id: 80 - display_name: "Fruit" -} -item { - name: "/m/035r7c" - id: 81 - display_name: "Leg" -} -item { - name: "/m/0bt_c3" - id: 82 - display_name: "Book" -} -item { - name: "/m/01_bhs" - id: 83 - display_name: "Fast food" -} -item { - name: "/m/01599" - id: 84 - display_name: "Beer" -} -item { - name: "/m/03120" - id: 85 - display_name: "Flag" -} -item { - name: "/m/026t6" - id: 86 - display_name: "Drum" -} -item { - name: "/m/01bjv" - id: 87 - display_name: "Bus" -} -item { - name: "/m/07r04" - id: 88 - display_name: "Truck" -} -item { - name: "/m/018xm" - id: 89 - display_name: "Ball" -} -item { - name: "/m/01rkbr" - id: 90 - display_name: "Tie" -} -item { - name: "/m/0fm3zh" - id: 91 - display_name: "Flowerpot" -} -item { - name: "/m/02_n6y" - id: 92 - display_name: "Goggles" -} -item { - name: "/m/04_sv" - id: 93 - display_name: "Motorcycle" -} -item { - name: "/m/06z37_" - id: 94 - display_name: "Picture frame" -} -item { - name: "/m/01bfm9" - id: 95 - display_name: "Shorts" -} -item { - name: "/m/0h8mhzd" - id: 96 - display_name: "Sports uniform" -} -item { - name: "/m/0d_2m" - id: 97 - display_name: "Moths and butterflies" -} -item { - name: "/m/0gjbg72" - id: 98 - display_name: "Shelf" -} -item { - name: "/m/01n4qj" - id: 99 - display_name: "Shirt" -} -item { - name: "/m/0ch_cf" - id: 100 - display_name: "Fish" -} -item { - name: "/m/06m11" - id: 101 - display_name: "Rose" -} -item { - name: "/m/01jfm_" - id: 102 - display_name: "Licence plate" -} -item { - name: "/m/02crq1" - id: 103 - display_name: "Couch" -} -item { - name: "/m/083kb" - id: 104 - display_name: "Weapon" -} -item { - name: "/m/01c648" - id: 105 - display_name: "Laptop" -} -item { - name: "/m/09tvcd" - id: 106 - display_name: "Wine glass" -} -item { - name: "/m/0h2r6" - id: 107 - display_name: "Van" -} -item { - name: "/m/081qc" - id: 108 - display_name: "Wine" -} -item { - name: "/m/09ddx" - id: 109 - display_name: "Duck" -} -item { - name: "/m/03p3bw" - id: 110 - display_name: "Bicycle helmet" -} -item { - name: "/m/0cyf8" - id: 111 - display_name: "Butterfly" -} -item { - name: "/m/0b_rs" - id: 112 - display_name: "Swimming pool" -} -item { - name: "/m/039xj_" - id: 113 - display_name: "Ear" -} -item { - name: "/m/021sj1" - id: 114 - display_name: "Office" -} -item { - name: "/m/0dv5r" - id: 115 - display_name: "Camera" -} -item { - name: "/m/01lynh" - id: 116 - display_name: "Stairs" -} -item { - name: "/m/06bt6" - id: 117 - display_name: "Reptile" -} -item { - name: "/m/01226z" - id: 118 - display_name: "Football" -} -item { - name: "/m/0fszt" - id: 119 - display_name: "Cake" -} -item { - name: "/m/050k8" - id: 120 - display_name: "Mobile phone" -} -item { - name: "/m/02wbtzl" - id: 121 - display_name: "Sun hat" -} -item { - name: "/m/02p5f1q" - id: 122 - display_name: "Coffee cup" -} -item { - name: "/m/025nd" - id: 123 - display_name: "Christmas tree" -} -item { - name: "/m/02522" - id: 124 - display_name: "Computer monitor" -} -item { - name: "/m/09ct_" - id: 125 - display_name: "Helicopter" -} -item { - name: "/m/0cvnqh" - id: 126 - display_name: "Bench" -} -item { - name: "/m/0d5gx" - id: 127 - display_name: "Castle" -} -item { - name: "/m/01xygc" - id: 128 - display_name: "Coat" -} -item { - name: "/m/04m6gz" - id: 129 - display_name: "Porch" -} -item { - name: "/m/01gkx_" - id: 130 - display_name: "Swimwear" -} -item { - name: "/m/01s105" - id: 131 - display_name: "Cabinetry" -} -item { - name: "/m/01j61q" - id: 132 - display_name: "Tent" -} -item { - name: "/m/0hnnb" - id: 133 - display_name: "Umbrella" -} -item { - name: "/m/01j51" - id: 134 - display_name: "Balloon" -} -item { - name: "/m/01knjb" - id: 135 - display_name: "Billboard" -} -item { - name: "/m/03__z0" - id: 136 - display_name: "Bookcase" -} -item { - name: "/m/01m2v" - id: 137 - display_name: "Computer keyboard" -} -item { - name: "/m/0167gd" - id: 138 - display_name: "Doll" -} -item { - name: "/m/0284d" - id: 139 - display_name: "Dairy" -} -item { - name: "/m/03ssj5" - id: 140 - display_name: "Bed" -} -item { - name: "/m/02fq_6" - id: 141 - display_name: "Fedora" -} -item { - name: "/m/06nwz" - id: 142 - display_name: "Seafood" -} -item { - name: "/m/0220r2" - id: 143 - display_name: "Fountain" -} -item { - name: "/m/01mqdt" - id: 144 - display_name: "Traffic sign" -} -item { - name: "/m/0268lbt" - id: 145 - display_name: "Hiking equipment" -} -item { - name: "/m/07c52" - id: 146 - display_name: "Television" -} -item { - name: "/m/0grw1" - id: 147 - display_name: "Salad" -} -item { - name: "/m/01h3n" - id: 148 - display_name: "Bee" -} -item { - name: "/m/078n6m" - id: 149 - display_name: "Coffee table" -} -item { - name: "/m/01xq0k1" - id: 150 - display_name: "Cattle" -} -item { - name: "/m/0gd2v" - id: 151 - display_name: "Marine mammal" -} -item { - name: "/m/0dbvp" - id: 152 - display_name: "Goose" -} -item { - name: "/m/03rszm" - id: 153 - display_name: "Curtain" -} -item { - name: "/m/0h8n5zk" - id: 154 - display_name: "Kitchen & dining room table" -} -item { - name: "/m/019dx1" - id: 155 - display_name: "Home appliance" -} -item { - name: "/m/03hl4l9" - id: 156 - display_name: "Marine invertebrates" -} -item { - name: "/m/0b3fp9" - id: 157 - display_name: "Countertop" -} -item { - name: "/m/02rdsp" - id: 158 - display_name: "Office supplies" -} -item { - name: "/m/0hf58v5" - id: 159 - display_name: "Luggage and bags" -} -item { - name: "/m/04h7h" - id: 160 - display_name: "Lighthouse" -} -item { - name: "/m/024g6" - id: 161 - display_name: "Cocktail" -} -item { - name: "/m/0cffdh" - id: 162 - display_name: "Maple" -} -item { - name: "/m/03q5c7" - id: 163 - display_name: "Saucer" -} -item { - name: "/m/014y4n" - id: 164 - display_name: "Paddle" -} -item { - name: "/m/01yx86" - id: 165 - display_name: "Bronze sculpture" -} -item { - name: "/m/020jm" - id: 166 - display_name: "Beetle" -} -item { - name: "/m/025dyy" - id: 167 - display_name: "Box" -} -item { - name: "/m/01llwg" - id: 168 - display_name: "Necklace" -} -item { - name: "/m/08pbxl" - id: 169 - display_name: "Monkey" -} -item { - name: "/m/02d9qx" - id: 170 - display_name: "Whiteboard" -} -item { - name: "/m/02pkr5" - id: 171 - display_name: "Plumbing fixture" -} -item { - name: "/m/0h99cwc" - id: 172 - display_name: "Kitchen appliance" -} -item { - name: "/m/050gv4" - id: 173 - display_name: "Plate" -} -item { - name: "/m/02vqfm" - id: 174 - display_name: "Coffee" -} -item { - name: "/m/09kx5" - id: 175 - display_name: "Deer" -} -item { - name: "/m/019w40" - id: 176 - display_name: "Surfboard" -} -item { - name: "/m/09dzg" - id: 177 - display_name: "Turtle" -} -item { - name: "/m/07k1x" - id: 178 - display_name: "Tool" -} -item { - name: "/m/080hkjn" - id: 179 - display_name: "Handbag" -} -item { - name: "/m/07qxg_" - id: 180 - display_name: "Football helmet" -} -item { - name: "/m/0ph39" - id: 181 - display_name: "Canoe" -} -item { - name: "/m/018p4k" - id: 182 - display_name: "Cart" -} -item { - name: "/m/02h19r" - id: 183 - display_name: "Scarf" -} -item { - name: "/m/015h_t" - id: 184 - display_name: "Beard" -} -item { - name: "/m/0fqfqc" - id: 185 - display_name: "Drawer" -} -item { - name: "/m/025rp__" - id: 186 - display_name: "Cowboy hat" -} -item { - name: "/m/01x3z" - id: 187 - display_name: "Clock" -} -item { - name: "/m/0crjs" - id: 188 - display_name: "Convenience store" -} -item { - name: "/m/0l515" - id: 189 - display_name: "Sandwich" -} -item { - name: "/m/015qff" - id: 190 - display_name: "Traffic light" -} -item { - name: "/m/09kmb" - id: 191 - display_name: "Spider" -} -item { - name: "/m/09728" - id: 192 - display_name: "Bread" -} -item { - name: "/m/071qp" - id: 193 - display_name: "Squirrel" -} -item { - name: "/m/02s195" - id: 194 - display_name: "Vase" -} -item { - name: "/m/06c54" - id: 195 - display_name: "Rifle" -} -item { - name: "/m/01xqw" - id: 196 - display_name: "Cello" -} -item { - name: "/m/05zsy" - id: 197 - display_name: "Pumpkin" -} -item { - name: "/m/0bwd_0j" - id: 198 - display_name: "Elephant" -} -item { - name: "/m/04m9y" - id: 199 - display_name: "Lizard" -} -item { - name: "/m/052sf" - id: 200 - display_name: "Mushroom" -} -item { - name: "/m/03grzl" - id: 201 - display_name: "Baseball glove" -} -item { - name: "/m/01z1kdw" - id: 202 - display_name: "Juice" -} -item { - name: "/m/02wv6h6" - id: 203 - display_name: "Skirt" -} -item { - name: "/m/016m2d" - id: 204 - display_name: "Skull" -} -item { - name: "/m/0dtln" - id: 205 - display_name: "Lamp" -} -item { - name: "/m/057cc" - id: 206 - display_name: "Musical keyboard" -} -item { - name: "/m/06k2mb" - id: 207 - display_name: "High heels" -} -item { - name: "/m/0f6wt" - id: 208 - display_name: "Falcon" -} -item { - name: "/m/0cxn2" - id: 209 - display_name: "Ice cream" -} -item { - name: "/m/02jvh9" - id: 210 - display_name: "Mug" -} -item { - name: "/m/0gjkl" - id: 211 - display_name: "Watch" -} -item { - name: "/m/01b638" - id: 212 - display_name: "Boot" -} -item { - name: "/m/071p9" - id: 213 - display_name: "Ski" -} -item { - name: "/m/0pg52" - id: 214 - display_name: "Taxi" -} -item { - name: "/m/0ftb8" - id: 215 - display_name: "Sunflower" -} -item { - name: "/m/0hnyx" - id: 216 - display_name: "Pastry" -} -item { - name: "/m/02jz0l" - id: 217 - display_name: "Tap" -} -item { - name: "/m/04kkgm" - id: 218 - display_name: "Bowl" -} -item { - name: "/m/0174n1" - id: 219 - display_name: "Glove" -} -item { - name: "/m/0gv1x" - id: 220 - display_name: "Parrot" -} -item { - name: "/m/09csl" - id: 221 - display_name: "Eagle" -} -item { - name: "/m/02jnhm" - id: 222 - display_name: "Tin can" -} -item { - name: "/m/099ssp" - id: 223 - display_name: "Platter" -} -item { - name: "/m/03nfch" - id: 224 - display_name: "Sandal" -} -item { - name: "/m/07y_7" - id: 225 - display_name: "Violin" -} -item { - name: "/m/05z6w" - id: 226 - display_name: "Penguin" -} -item { - name: "/m/03m3pdh" - id: 227 - display_name: "Sofa bed" -} -item { - name: "/m/09ld4" - id: 228 - display_name: "Frog" -} -item { - name: "/m/09b5t" - id: 229 - display_name: "Chicken" -} -item { - name: "/m/054xkw" - id: 230 - display_name: "Lifejacket" -} -item { - name: "/m/0130jx" - id: 231 - display_name: "Sink" -} -item { - name: "/m/07fbm7" - id: 232 - display_name: "Strawberry" -} -item { - name: "/m/01dws" - id: 233 - display_name: "Bear" -} -item { - name: "/m/01tcjp" - id: 234 - display_name: "Muffin" -} -item { - name: "/m/0dftk" - id: 235 - display_name: "Swan" -} -item { - name: "/m/0c06p" - id: 236 - display_name: "Candle" -} -item { - name: "/m/034c16" - id: 237 - display_name: "Pillow" -} -item { - name: "/m/09d5_" - id: 238 - display_name: "Owl" -} -item { - name: "/m/03hlz0c" - id: 239 - display_name: "Kitchen utensil" -} -item { - name: "/m/0ft9s" - id: 240 - display_name: "Dragonfly" -} -item { - name: "/m/011k07" - id: 241 - display_name: "Tortoise" -} -item { - name: "/m/054_l" - id: 242 - display_name: "Mirror" -} -item { - name: "/m/0jqgx" - id: 243 - display_name: "Lily" -} -item { - name: "/m/0663v" - id: 244 - display_name: "Pizza" -} -item { - name: "/m/0242l" - id: 245 - display_name: "Coin" -} -item { - name: "/m/014trl" - id: 246 - display_name: "Cosmetics" -} -item { - name: "/m/05r5c" - id: 247 - display_name: "Piano" -} -item { - name: "/m/07j87" - id: 248 - display_name: "Tomato" -} -item { - name: "/m/05kyg_" - id: 249 - display_name: "Chest of drawers" -} -item { - name: "/m/0kmg4" - id: 250 - display_name: "Teddy bear" -} -item { - name: "/m/07cmd" - id: 251 - display_name: "Tank" -} -item { - name: "/m/0dv77" - id: 252 - display_name: "Squash" -} -item { - name: "/m/096mb" - id: 253 - display_name: "Lion" -} -item { - name: "/m/01gmv2" - id: 254 - display_name: "Brassiere" -} -item { - name: "/m/07bgp" - id: 255 - display_name: "Sheep" -} -item { - name: "/m/0cmx8" - id: 256 - display_name: "Spoon" -} -item { - name: "/m/029tx" - id: 257 - display_name: "Dinosaur" -} -item { - name: "/m/073bxn" - id: 258 - display_name: "Tripod" -} -item { - name: "/m/0bh9flk" - id: 259 - display_name: "Tablet computer" -} -item { - name: "/m/06mf6" - id: 260 - display_name: "Rabbit" -} -item { - name: "/m/06_fw" - id: 261 - display_name: "Skateboard" -} -item { - name: "/m/078jl" - id: 262 - display_name: "Snake" -} -item { - name: "/m/0fbdv" - id: 263 - display_name: "Shellfish" -} -item { - name: "/m/0h23m" - id: 264 - display_name: "Sparrow" -} -item { - name: "/m/014j1m" - id: 265 - display_name: "Apple" -} -item { - name: "/m/03fwl" - id: 266 - display_name: "Goat" -} -item { - name: "/m/02y6n" - id: 267 - display_name: "French fries" -} -item { - name: "/m/06c7f7" - id: 268 - display_name: "Lipstick" -} -item { - name: "/m/026qbn5" - id: 269 - display_name: "studio couch" -} -item { - name: "/m/0cdn1" - id: 270 - display_name: "Hamburger" -} -item { - name: "/m/07clx" - id: 271 - display_name: "Tea" -} -item { - name: "/m/07cx4" - id: 272 - display_name: "Telephone" -} -item { - name: "/m/03g8mr" - id: 273 - display_name: "Baseball bat" -} -item { - name: "/m/0cnyhnx" - id: 274 - display_name: "Bull" -} -item { - name: "/m/01b7fy" - id: 275 - display_name: "Headphones" -} -item { - name: "/m/04gth" - id: 276 - display_name: "Lavender" -} -item { - name: "/m/0cyfs" - id: 277 - display_name: "Parachute" -} -item { - name: "/m/021mn" - id: 278 - display_name: "Cookie" -} -item { - name: "/m/07dm6" - id: 279 - display_name: "Tiger" -} -item { - name: "/m/0k1tl" - id: 280 - display_name: "Pen" -} -item { - name: "/m/0dv9c" - id: 281 - display_name: "Racket" -} -item { - name: "/m/0dt3t" - id: 282 - display_name: "Fork" -} -item { - name: "/m/04yqq2" - id: 283 - display_name: "Bust" -} -item { - name: "/m/01cmb2" - id: 284 - display_name: "Miniskirt" -} -item { - name: "/m/0gd36" - id: 285 - display_name: "Sea lion" -} -item { - name: "/m/033cnk" - id: 286 - display_name: "Egg" -} -item { - name: "/m/06ncr" - id: 287 - display_name: "Saxophone" -} -item { - name: "/m/03bk1" - id: 288 - display_name: "Giraffe" -} -item { - name: "/m/0bjyj5" - id: 289 - display_name: "Waste container" -} -item { - name: "/m/06__v" - id: 290 - display_name: "Snowboard" -} -item { - name: "/m/0qmmr" - id: 291 - display_name: "Wheelchair" -} -item { - name: "/m/01xgg_" - id: 292 - display_name: "Medical equipment" -} -item { - name: "/m/0czz2" - id: 293 - display_name: "Antelope" -} -item { - name: "/m/02l8p9" - id: 294 - display_name: "Harbor seal" -} -item { - name: "/m/09g1w" - id: 295 - display_name: "Toilet" -} -item { - name: "/m/0ll1f78" - id: 296 - display_name: "Shrimp" -} -item { - name: "/m/0cyhj_" - id: 297 - display_name: "Orange" -} -item { - name: "/m/0642b4" - id: 298 - display_name: "Cupboard" -} -item { - name: "/m/0h8mzrc" - id: 299 - display_name: "Wall clock" -} -item { - name: "/m/068zj" - id: 300 - display_name: "Pig" -} -item { - name: "/m/02z51p" - id: 301 - display_name: "Nightstand" -} -item { - name: "/m/0h8nr_l" - id: 302 - display_name: "Bathroom accessory" -} -item { - name: "/m/0388q" - id: 303 - display_name: "Grape" -} -item { - name: "/m/02hj4" - id: 304 - display_name: "Dolphin" -} -item { - name: "/m/01jfsr" - id: 305 - display_name: "Lantern" -} -item { - name: "/m/07gql" - id: 306 - display_name: "Trumpet" -} -item { - name: "/m/0h8my_4" - id: 307 - display_name: "Tennis racket" -} -item { - name: "/m/0n28_" - id: 308 - display_name: "Crab" -} -item { - name: "/m/0120dh" - id: 309 - display_name: "Sea turtle" -} -item { - name: "/m/020kz" - id: 310 - display_name: "Cannon" -} -item { - name: "/m/0mkg" - id: 311 - display_name: "Accordion" -} -item { - name: "/m/03c7gz" - id: 312 - display_name: "Door handle" -} -item { - name: "/m/09k_b" - id: 313 - display_name: "Lemon" -} -item { - name: "/m/031n1" - id: 314 - display_name: "Foot" -} -item { - name: "/m/04rmv" - id: 315 - display_name: "Mouse" -} -item { - name: "/m/084rd" - id: 316 - display_name: "Wok" -} -item { - name: "/m/02rgn06" - id: 317 - display_name: "Volleyball" -} -item { - name: "/m/05z55" - id: 318 - display_name: "Pasta" -} -item { - name: "/m/01r546" - id: 319 - display_name: "Earrings" -} -item { - name: "/m/09qck" - id: 320 - display_name: "Banana" -} -item { - name: "/m/012w5l" - id: 321 - display_name: "Ladder" -} -item { - name: "/m/01940j" - id: 322 - display_name: "Backpack" -} -item { - name: "/m/09f_2" - id: 323 - display_name: "Crocodile" -} -item { - name: "/m/02p3w7d" - id: 324 - display_name: "Roller skates" -} -item { - name: "/m/057p5t" - id: 325 - display_name: "Scoreboard" -} -item { - name: "/m/0d8zb" - id: 326 - display_name: "Jellyfish" -} -item { - name: "/m/01nq26" - id: 327 - display_name: "Sock" -} -item { - name: "/m/01x_v" - id: 328 - display_name: "Camel" -} -item { - name: "/m/05gqfk" - id: 329 - display_name: "Plastic bag" -} -item { - name: "/m/0cydv" - id: 330 - display_name: "Caterpillar" -} -item { - name: "/m/07030" - id: 331 - display_name: "Sushi" -} -item { - name: "/m/084zz" - id: 332 - display_name: "Whale" -} -item { - name: "/m/0c29q" - id: 333 - display_name: "Leopard" -} -item { - name: "/m/02zn6n" - id: 334 - display_name: "Barrel" -} -item { - name: "/m/03tw93" - id: 335 - display_name: "Fireplace" -} -item { - name: "/m/0fqt361" - id: 336 - display_name: "Stool" -} -item { - name: "/m/0f9_l" - id: 337 - display_name: "Snail" -} -item { - name: "/m/0gm28" - id: 338 - display_name: "Candy" -} -item { - name: "/m/09rvcxw" - id: 339 - display_name: "Rocket" -} -item { - name: "/m/01nkt" - id: 340 - display_name: "Cheese" -} -item { - name: "/m/04p0qw" - id: 341 - display_name: "Billiard table" -} -item { - name: "/m/03hj559" - id: 342 - display_name: "Mixing bowl" -} -item { - name: "/m/07pj7bq" - id: 343 - display_name: "Bowling equipment" -} -item { - name: "/m/04ctx" - id: 344 - display_name: "Knife" -} -item { - name: "/m/0703r8" - id: 345 - display_name: "Loveseat" -} -item { - name: "/m/03qrc" - id: 346 - display_name: "Hamster" -} -item { - name: "/m/020lf" - id: 347 - display_name: "Mouse" -} -item { - name: "/m/0by6g" - id: 348 - display_name: "Shark" -} -item { - name: "/m/01fh4r" - id: 349 - display_name: "Teapot" -} -item { - name: "/m/07c6l" - id: 350 - display_name: "Trombone" -} -item { - name: "/m/03bj1" - id: 351 - display_name: "Panda" -} -item { - name: "/m/0898b" - id: 352 - display_name: "Zebra" -} -item { - name: "/m/02x984l" - id: 353 - display_name: "Mechanical fan" -} -item { - name: "/m/0fj52s" - id: 354 - display_name: "Carrot" -} -item { - name: "/m/0cd4d" - id: 355 - display_name: "Cheetah" -} -item { - name: "/m/02068x" - id: 356 - display_name: "Gondola" -} -item { - name: "/m/01vbnl" - id: 357 - display_name: "Bidet" -} -item { - name: "/m/0449p" - id: 358 - display_name: "Jaguar" -} -item { - name: "/m/0gj37" - id: 359 - display_name: "Ladybug" -} -item { - name: "/m/0nl46" - id: 360 - display_name: "Crown" -} -item { - name: "/m/0152hh" - id: 361 - display_name: "Snowman" -} -item { - name: "/m/03dnzn" - id: 362 - display_name: "Bathtub" -} -item { - name: "/m/05_5p_0" - id: 363 - display_name: "Table tennis racket" -} -item { - name: "/m/02jfl0" - id: 364 - display_name: "Sombrero" -} -item { - name: "/m/01dxs" - id: 365 - display_name: "Brown bear" -} -item { - name: "/m/0cjq5" - id: 366 - display_name: "Lobster" -} -item { - name: "/m/040b_t" - id: 367 - display_name: "Refrigerator" -} -item { - name: "/m/0_cp5" - id: 368 - display_name: "Oyster" -} -item { - name: "/m/0gxl3" - id: 369 - display_name: "Handgun" -} -item { - name: "/m/029bxz" - id: 370 - display_name: "Oven" -} -item { - name: "/m/02zt3" - id: 371 - display_name: "Kite" -} -item { - name: "/m/03d443" - id: 372 - display_name: "Rhinoceros" -} -item { - name: "/m/0306r" - id: 373 - display_name: "Fox" -} -item { - name: "/m/0h8l4fh" - id: 374 - display_name: "Light bulb" -} -item { - name: "/m/0633h" - id: 375 - display_name: "Polar bear" -} -item { - name: "/m/01s55n" - id: 376 - display_name: "Suitcase" -} -item { - name: "/m/0hkxq" - id: 377 - display_name: "Broccoli" -} -item { - name: "/m/0cn6p" - id: 378 - display_name: "Otter" -} -item { - name: "/m/0dbzx" - id: 379 - display_name: "Mule" -} -item { - name: "/m/01dy8n" - id: 380 - display_name: "Woodpecker" -} -item { - name: "/m/01h8tj" - id: 381 - display_name: "Starfish" -} -item { - name: "/m/03s_tn" - id: 382 - display_name: "Kettle" -} -item { - name: "/m/01xs3r" - id: 383 - display_name: "Jet ski" -} -item { - name: "/m/031b6r" - id: 384 - display_name: "Window blind" -} -item { - name: "/m/06j2d" - id: 385 - display_name: "Raven" -} -item { - name: "/m/0hqkz" - id: 386 - display_name: "Grapefruit" -} -item { - name: "/m/01_5g" - id: 387 - display_name: "Chopsticks" -} -item { - name: "/m/02zvsm" - id: 388 - display_name: "Tart" -} -item { - name: "/m/0kpqd" - id: 389 - display_name: "Watermelon" -} -item { - name: "/m/015x4r" - id: 390 - display_name: "Cucumber" -} -item { - name: "/m/061hd_" - id: 391 - display_name: "Infant bed" -} -item { - name: "/m/04ylt" - id: 392 - display_name: "Missile" -} -item { - name: "/m/02wv84t" - id: 393 - display_name: "Gas stove" -} -item { - name: "/m/04y4h8h" - id: 394 - display_name: "Bathroom cabinet" -} -item { - name: "/m/01gllr" - id: 395 - display_name: "Beehive" -} -item { - name: "/m/0pcr" - id: 396 - display_name: "Alpaca" -} -item { - name: "/m/0jy4k" - id: 397 - display_name: "Doughnut" -} -item { - name: "/m/09f20" - id: 398 - display_name: "Hippopotamus" -} -item { - name: "/m/0mcx2" - id: 399 - display_name: "Ipod" -} -item { - name: "/m/04c0y" - id: 400 - display_name: "Kangaroo" -} -item { - name: "/m/0_k2" - id: 401 - display_name: "Ant" -} -item { - name: "/m/0jg57" - id: 402 - display_name: "Bell pepper" -} -item { - name: "/m/03fj2" - id: 403 - display_name: "Goldfish" -} -item { - name: "/m/03ldnb" - id: 404 - display_name: "Ceiling fan" -} -item { - name: "/m/06nrc" - id: 405 - display_name: "Shotgun" -} -item { - name: "/m/01btn" - id: 406 - display_name: "Barge" -} -item { - name: "/m/05vtc" - id: 407 - display_name: "Potato" -} -item { - name: "/m/08hvt4" - id: 408 - display_name: "Jug" -} -item { - name: "/m/0fx9l" - id: 409 - display_name: "Microwave oven" -} -item { - name: "/m/01h44" - id: 410 - display_name: "Bat" -} -item { - name: "/m/05n4y" - id: 411 - display_name: "Ostrich" -} -item { - name: "/m/0jly1" - id: 412 - display_name: "Turkey" -} -item { - name: "/m/06y5r" - id: 413 - display_name: "Sword" -} -item { - name: "/m/05ctyq" - id: 414 - display_name: "Tennis ball" -} -item { - name: "/m/0fp6w" - id: 415 - display_name: "Pineapple" -} -item { - name: "/m/0d4w1" - id: 416 - display_name: "Closet" -} -item { - name: "/m/02pv19" - id: 417 - display_name: "Stop sign" -} -item { - name: "/m/07crc" - id: 418 - display_name: "Taco" -} -item { - name: "/m/01dwwc" - id: 419 - display_name: "Pancake" -} -item { - name: "/m/01b9xk" - id: 420 - display_name: "Hot dog" -} -item { - name: "/m/013y1f" - id: 421 - display_name: "Organ" -} -item { - name: "/m/0m53l" - id: 422 - display_name: "Rays and skates" -} -item { - name: "/m/0174k2" - id: 423 - display_name: "Washing machine" -} -item { - name: "/m/01dwsz" - id: 424 - display_name: "Waffle" -} -item { - name: "/m/04vv5k" - id: 425 - display_name: "Snowplow" -} -item { - name: "/m/04cp_" - id: 426 - display_name: "Koala" -} -item { - name: "/m/0fz0h" - id: 427 - display_name: "Honeycomb" -} -item { - name: "/m/0llzx" - id: 428 - display_name: "Sewing machine" -} -item { - name: "/m/0319l" - id: 429 - display_name: "Horn" -} -item { - name: "/m/04v6l4" - id: 430 - display_name: "Frying pan" -} -item { - name: "/m/0dkzw" - id: 431 - display_name: "Seat belt" -} -item { - name: "/m/027pcv" - id: 432 - display_name: "Zucchini" -} -item { - name: "/m/0323sq" - id: 433 - display_name: "Golf cart" -} -item { - name: "/m/054fyh" - id: 434 - display_name: "Pitcher" -} -item { - name: "/m/01pns0" - id: 435 - display_name: "Fire hydrant" -} -item { - name: "/m/012n7d" - id: 436 - display_name: "Ambulance" -} -item { - name: "/m/044r5d" - id: 437 - display_name: "Golf ball" -} -item { - name: "/m/01krhy" - id: 438 - display_name: "Tiara" -} -item { - name: "/m/0dq75" - id: 439 - display_name: "Raccoon" -} -item { - name: "/m/0176mf" - id: 440 - display_name: "Belt" -} -item { - name: "/m/0h8lkj8" - id: 441 - display_name: "Corded phone" -} -item { - name: "/m/04tn4x" - id: 442 - display_name: "Swim cap" -} -item { - name: "/m/06l9r" - id: 443 - display_name: "Red panda" -} -item { - name: "/m/0cjs7" - id: 444 - display_name: "Asparagus" -} -item { - name: "/m/01lsmm" - id: 445 - display_name: "Scissors" -} -item { - name: "/m/01lcw4" - id: 446 - display_name: "Limousine" -} -item { - name: "/m/047j0r" - id: 447 - display_name: "Filing cabinet" -} -item { - name: "/m/01fb_0" - id: 448 - display_name: "Bagel" -} -item { - name: "/m/04169hn" - id: 449 - display_name: "Wood-burning stove" -} -item { - name: "/m/076bq" - id: 450 - display_name: "Segway" -} -item { - name: "/m/0hdln" - id: 451 - display_name: "Ruler" -} -item { - name: "/m/01g3x7" - id: 452 - display_name: "Bow and arrow" -} -item { - name: "/m/0l3ms" - id: 453 - display_name: "Balance beam" -} -item { - name: "/m/058qzx" - id: 454 - display_name: "Kitchen knife" -} -item { - name: "/m/0h8n6ft" - id: 455 - display_name: "Cake stand" -} -item { - name: "/m/018j2" - id: 456 - display_name: "Banjo" -} -item { - name: "/m/0l14j_" - id: 457 - display_name: "Flute" -} -item { - name: "/m/0wdt60w" - id: 458 - display_name: "Rugby ball" -} -item { - name: "/m/02gzp" - id: 459 - display_name: "Dagger" -} -item { - name: "/m/0h8n6f9" - id: 460 - display_name: "Dog bed" -} -item { - name: "/m/0fbw6" - id: 461 - display_name: "Cabbage" -} -item { - name: "/m/07kng9" - id: 462 - display_name: "Picnic basket" -} -item { - name: "/m/0dj6p" - id: 463 - display_name: "Peach" -} -item { - name: "/m/06pcq" - id: 464 - display_name: "Submarine sandwich" -} -item { - name: "/m/061_f" - id: 465 - display_name: "Pear" -} -item { - name: "/m/04g2r" - id: 466 - display_name: "Lynx" -} -item { - name: "/m/0jwn_" - id: 467 - display_name: "Pomegranate" -} -item { - name: "/m/02f9f_" - id: 468 - display_name: "Shower" -} -item { - name: "/m/01f8m5" - id: 469 - display_name: "Blue jay" -} -item { - name: "/m/01m4t" - id: 470 - display_name: "Printer" -} -item { - name: "/m/0cl4p" - id: 471 - display_name: "Hedgehog" -} -item { - name: "/m/07xyvk" - id: 472 - display_name: "Coffeemaker" -} -item { - name: "/m/084hf" - id: 473 - display_name: "Worm" -} -item { - name: "/m/03v5tg" - id: 474 - display_name: "Drinking straw" -} -item { - name: "/m/0qjjc" - id: 475 - display_name: "Remote control" -} -item { - name: "/m/015x5n" - id: 476 - display_name: "Radish" -} -item { - name: "/m/0ccs93" - id: 477 - display_name: "Canary" -} -item { - name: "/m/0nybt" - id: 478 - display_name: "Seahorse" -} -item { - name: "/m/02vkqh8" - id: 479 - display_name: "Wardrobe" -} -item { - name: "/m/09gtd" - id: 480 - display_name: "Toilet paper" -} -item { - name: "/m/019h78" - id: 481 - display_name: "Centipede" -} -item { - name: "/m/015wgc" - id: 482 - display_name: "Croissant" -} -item { - name: "/m/01x3jk" - id: 483 - display_name: "Snowmobile" -} -item { - name: "/m/01j3zr" - id: 484 - display_name: "Burrito" -} -item { - name: "/m/0c568" - id: 485 - display_name: "Porcupine" -} -item { - name: "/m/02pdsw" - id: 486 - display_name: "Cutting board" -} -item { - name: "/m/029b3" - id: 487 - display_name: "Dice" -} -item { - name: "/m/03q5t" - id: 488 - display_name: "Harpsichord" -} -item { - name: "/m/0p833" - id: 489 - display_name: "Perfume" -} -item { - name: "/m/01d380" - id: 490 - display_name: "Drill" -} -item { - name: "/m/024d2" - id: 491 - display_name: "Calculator" -} -item { - name: "/m/0mw_6" - id: 492 - display_name: "Willow" -} -item { - name: "/m/01f91_" - id: 493 - display_name: "Pretzel" -} -item { - name: "/m/02g30s" - id: 494 - display_name: "Guacamole" -} -item { - name: "/m/01hrv5" - id: 495 - display_name: "Popcorn" -} -item { - name: "/m/03m5k" - id: 496 - display_name: "Harp" -} -item { - name: "/m/0162_1" - id: 497 - display_name: "Towel" -} -item { - name: "/m/063rgb" - id: 498 - display_name: "Mixer" -} -item { - name: "/m/06_72j" - id: 499 - display_name: "Digital clock" -} -item { - name: "/m/046dlr" - id: 500 - display_name: "Alarm clock" -} -item { - name: "/m/047v4b" - id: 501 - display_name: "Artichoke" -} -item { - name: "/m/04zpv" - id: 502 - display_name: "Milk" -} -item { - name: "/m/043nyj" - id: 503 - display_name: "Common fig" -} -item { - name: "/m/03bbps" - id: 504 - display_name: "Power plugs and sockets" -} -item { - name: "/m/02w3r3" - id: 505 - display_name: "Paper towel" -} -item { - name: "/m/02pjr4" - id: 506 - display_name: "Blender" -} -item { - name: "/m/0755b" - id: 507 - display_name: "Scorpion" -} -item { - name: "/m/02lbcq" - id: 508 - display_name: "Stretcher" -} -item { - name: "/m/0fldg" - id: 509 - display_name: "Mango" -} -item { - name: "/m/012074" - id: 510 - display_name: "Magpie" -} -item { - name: "/m/035vxb" - id: 511 - display_name: "Isopod" -} -item { - name: "/m/02w3_ws" - id: 512 - display_name: "Personal care" -} -item { - name: "/m/0f6nr" - id: 513 - display_name: "Unicycle" -} -item { - name: "/m/0420v5" - id: 514 - display_name: "Punching bag" -} -item { - name: "/m/0frqm" - id: 515 - display_name: "Envelope" -} -item { - name: "/m/03txqz" - id: 516 - display_name: "Scale" -} -item { - name: "/m/0271qf7" - id: 517 - display_name: "Wine rack" -} -item { - name: "/m/074d1" - id: 518 - display_name: "Submarine" -} -item { - name: "/m/08p92x" - id: 519 - display_name: "Cream" -} -item { - name: "/m/01j4z9" - id: 520 - display_name: "Chainsaw" -} -item { - name: "/m/0kpt_" - id: 521 - display_name: "Cantaloupe" -} -item { - name: "/m/0h8n27j" - id: 522 - display_name: "Serving tray" -} -item { - name: "/m/03y6mg" - id: 523 - display_name: "Food processor" -} -item { - name: "/m/04h8sr" - id: 524 - display_name: "Dumbbell" -} -item { - name: "/m/065h6l" - id: 525 - display_name: "Jacuzzi" -} -item { - name: "/m/02tsc9" - id: 526 - display_name: "Slow cooker" -} -item { - name: "/m/012ysf" - id: 527 - display_name: "Syringe" -} -item { - name: "/m/0ky7b" - id: 528 - display_name: "Dishwasher" -} -item { - name: "/m/02wg_p" - id: 529 - display_name: "Tree house" -} -item { - name: "/m/0584n8" - id: 530 - display_name: "Briefcase" -} -item { - name: "/m/03kt2w" - id: 531 - display_name: "Stationary bicycle" -} -item { - name: "/m/05kms" - id: 532 - display_name: "Oboe" -} -item { - name: "/m/030610" - id: 533 - display_name: "Treadmill" -} -item { - name: "/m/0lt4_" - id: 534 - display_name: "Binoculars" -} -item { - name: "/m/076lb9" - id: 535 - display_name: "Bench" -} -item { - name: "/m/02ctlc" - id: 536 - display_name: "Cricket ball" -} -item { - name: "/m/02x8cch" - id: 537 - display_name: "Salt and pepper shakers" -} -item { - name: "/m/09gys" - id: 538 - display_name: "Squid" -} -item { - name: "/m/03jbxj" - id: 539 - display_name: "Light switch" -} -item { - name: "/m/012xff" - id: 540 - display_name: "Toothbrush" -} -item { - name: "/m/0h8kx63" - id: 541 - display_name: "Spice rack" -} -item { - name: "/m/073g6" - id: 542 - display_name: "Stethoscope" -} -item { - name: "/m/02cvgx" - id: 543 - display_name: "Winter melon" -} -item { - name: "/m/027rl48" - id: 544 - display_name: "Ladle" -} -item { - name: "/m/01kb5b" - id: 545 - display_name: "Flashlight" -} diff --git a/research/object_detection/data/oid_object_detection_challenge_500_label_map.pbtxt b/research/object_detection/data/oid_object_detection_challenge_500_label_map.pbtxt deleted file mode 100644 index 044f6d4c813..00000000000 --- a/research/object_detection/data/oid_object_detection_challenge_500_label_map.pbtxt +++ /dev/null @@ -1,2500 +0,0 @@ -item { - name: "/m/061hd_" - id: 1 - display_name: "Infant bed" -} -item { - name: "/m/06m11" - id: 2 - display_name: "Rose" -} -item { - name: "/m/03120" - id: 3 - display_name: "Flag" -} -item { - name: "/m/01kb5b" - id: 4 - display_name: "Flashlight" -} -item { - name: "/m/0120dh" - id: 5 - display_name: "Sea turtle" -} -item { - name: "/m/0dv5r" - id: 6 - display_name: "Camera" -} -item { - name: "/m/0jbk" - id: 7 - display_name: "Animal" -} -item { - name: "/m/0174n1" - id: 8 - display_name: "Glove" -} -item { - name: "/m/09f_2" - id: 9 - display_name: "Crocodile" -} -item { - name: "/m/01xq0k1" - id: 10 - display_name: "Cattle" -} -item { - name: "/m/03jm5" - id: 11 - display_name: "House" -} -item { - name: "/m/02g30s" - id: 12 - display_name: "Guacamole" -} -item { - name: "/m/05z6w" - id: 13 - display_name: "Penguin" -} -item { - name: "/m/01jfm_" - id: 14 - display_name: "Vehicle registration plate" -} -item { - name: "/m/076lb9" - id: 15 - display_name: "Training bench" -} -item { - name: "/m/0gj37" - id: 16 - display_name: "Ladybug" -} -item { - name: "/m/0k0pj" - id: 17 - display_name: "Human nose" -} -item { - name: "/m/0kpqd" - id: 18 - display_name: "Watermelon" -} -item { - name: "/m/0l14j_" - id: 19 - display_name: "Flute" -} -item { - name: "/m/0cyf8" - id: 20 - display_name: "Butterfly" -} -item { - name: "/m/0174k2" - id: 21 - display_name: "Washing machine" -} -item { - name: "/m/0dq75" - id: 22 - display_name: "Raccoon" -} -item { - name: "/m/076bq" - id: 23 - display_name: "Segway" -} -item { - name: "/m/07crc" - id: 24 - display_name: "Taco" -} -item { - name: "/m/0d8zb" - id: 25 - display_name: "Jellyfish" -} -item { - name: "/m/0fszt" - id: 26 - display_name: "Cake" -} -item { - name: "/m/0k1tl" - id: 27 - display_name: "Pen" -} -item { - name: "/m/020kz" - id: 28 - display_name: "Cannon" -} -item { - name: "/m/09728" - id: 29 - display_name: "Bread" -} -item { - name: "/m/07j7r" - id: 30 - display_name: "Tree" -} -item { - name: "/m/0fbdv" - id: 31 - display_name: "Shellfish" -} -item { - name: "/m/03ssj5" - id: 32 - display_name: "Bed" -} -item { - name: "/m/03qrc" - id: 33 - display_name: "Hamster" -} -item { - name: "/m/02dl1y" - id: 34 - display_name: "Hat" -} -item { - name: "/m/01k6s3" - id: 35 - display_name: "Toaster" -} -item { - name: "/m/02jfl0" - id: 36 - display_name: "Sombrero" -} -item { - name: "/m/01krhy" - id: 37 - display_name: "Tiara" -} -item { - name: "/m/04kkgm" - id: 38 - display_name: "Bowl" -} -item { - name: "/m/0ft9s" - id: 39 - display_name: "Dragonfly" -} -item { - name: "/m/0d_2m" - id: 40 - display_name: "Moths and butterflies" -} -item { - name: "/m/0czz2" - id: 41 - display_name: "Antelope" -} -item { - name: "/m/0f4s2w" - id: 42 - display_name: "Vegetable" -} -item { - name: "/m/07dd4" - id: 43 - display_name: "Torch" -} -item { - name: "/m/0cgh4" - id: 44 - display_name: "Building" -} -item { - name: "/m/03bbps" - id: 45 - display_name: "Power plugs and sockets" -} -item { - name: "/m/02pjr4" - id: 46 - display_name: "Blender" -} -item { - name: "/m/04p0qw" - id: 47 - display_name: "Billiard table" -} -item { - name: "/m/02pdsw" - id: 48 - display_name: "Cutting board" -} -item { - name: "/m/01yx86" - id: 49 - display_name: "Bronze sculpture" -} -item { - name: "/m/09dzg" - id: 50 - display_name: "Turtle" -} -item { - name: "/m/0hkxq" - id: 51 - display_name: "Broccoli" -} -item { - name: "/m/07dm6" - id: 52 - display_name: "Tiger" -} -item { - name: "/m/054_l" - id: 53 - display_name: "Mirror" -} -item { - name: "/m/01dws" - id: 54 - display_name: "Bear" -} -item { - name: "/m/027pcv" - id: 55 - display_name: "Zucchini" -} -item { - name: "/m/01d40f" - id: 56 - display_name: "Dress" -} -item { - name: "/m/02rgn06" - id: 57 - display_name: "Volleyball" -} -item { - name: "/m/0342h" - id: 58 - display_name: "Guitar" -} -item { - name: "/m/06bt6" - id: 59 - display_name: "Reptile" -} -item { - name: "/m/0323sq" - id: 60 - display_name: "Golf cart" -} -item { - name: "/m/02zvsm" - id: 61 - display_name: "Tart" -} -item { - name: "/m/02fq_6" - id: 62 - display_name: "Fedora" -} -item { - name: "/m/01lrl" - id: 63 - display_name: "Carnivore" -} -item { - name: "/m/0k4j" - id: 64 - display_name: "Car" -} -item { - name: "/m/04h7h" - id: 65 - display_name: "Lighthouse" -} -item { - name: "/m/07xyvk" - id: 66 - display_name: "Coffeemaker" -} -item { - name: "/m/03y6mg" - id: 67 - display_name: "Food processor" -} -item { - name: "/m/07r04" - id: 68 - display_name: "Truck" -} -item { - name: "/m/03__z0" - id: 69 - display_name: "Bookcase" -} -item { - name: "/m/019w40" - id: 70 - display_name: "Surfboard" -} -item { - name: "/m/09j5n" - id: 71 - display_name: "Footwear" -} -item { - name: "/m/0cvnqh" - id: 72 - display_name: "Bench" -} -item { - name: "/m/01llwg" - id: 73 - display_name: "Necklace" -} -item { - name: "/m/0c9ph5" - id: 74 - display_name: "Flower" -} -item { - name: "/m/015x5n" - id: 75 - display_name: "Radish" -} -item { - name: "/m/0gd2v" - id: 76 - display_name: "Marine mammal" -} -item { - name: "/m/04v6l4" - id: 77 - display_name: "Frying pan" -} -item { - name: "/m/02jz0l" - id: 78 - display_name: "Tap" -} -item { - name: "/m/0dj6p" - id: 79 - display_name: "Peach" -} -item { - name: "/m/04ctx" - id: 80 - display_name: "Knife" -} -item { - name: "/m/080hkjn" - id: 81 - display_name: "Handbag" -} -item { - name: "/m/01c648" - id: 82 - display_name: "Laptop" -} -item { - name: "/m/01j61q" - id: 83 - display_name: "Tent" -} -item { - name: "/m/012n7d" - id: 84 - display_name: "Ambulance" -} -item { - name: "/m/025nd" - id: 85 - display_name: "Christmas tree" -} -item { - name: "/m/09csl" - id: 86 - display_name: "Eagle" -} -item { - name: "/m/01lcw4" - id: 87 - display_name: "Limousine" -} -item { - name: "/m/0h8n5zk" - id: 88 - display_name: "Kitchen & dining room table" -} -item { - name: "/m/0633h" - id: 89 - display_name: "Polar bear" -} -item { - name: "/m/01fdzj" - id: 90 - display_name: "Tower" -} -item { - name: "/m/01226z" - id: 91 - display_name: "Football" -} -item { - name: "/m/0mw_6" - id: 92 - display_name: "Willow" -} -item { - name: "/m/04hgtk" - id: 93 - display_name: "Human head" -} -item { - name: "/m/02pv19" - id: 94 - display_name: "Stop sign" -} -item { - name: "/m/09qck" - id: 95 - display_name: "Banana" -} -item { - name: "/m/063rgb" - id: 96 - display_name: "Mixer" -} -item { - name: "/m/0lt4_" - id: 97 - display_name: "Binoculars" -} -item { - name: "/m/0270h" - id: 98 - display_name: "Dessert" -} -item { - name: "/m/01h3n" - id: 99 - display_name: "Bee" -} -item { - name: "/m/01mzpv" - id: 100 - display_name: "Chair" -} -item { - name: "/m/04169hn" - id: 101 - display_name: "Wood-burning stove" -} -item { - name: "/m/0fm3zh" - id: 102 - display_name: "Flowerpot" -} -item { - name: "/m/0d20w4" - id: 103 - display_name: "Beaker" -} -item { - name: "/m/0_cp5" - id: 104 - display_name: "Oyster" -} -item { - name: "/m/01dy8n" - id: 105 - display_name: "Woodpecker" -} -item { - name: "/m/03m5k" - id: 106 - display_name: "Harp" -} -item { - name: "/m/03dnzn" - id: 107 - display_name: "Bathtub" -} -item { - name: "/m/0h8mzrc" - id: 108 - display_name: "Wall clock" -} -item { - name: "/m/0h8mhzd" - id: 109 - display_name: "Sports uniform" -} -item { - name: "/m/03d443" - id: 110 - display_name: "Rhinoceros" -} -item { - name: "/m/01gllr" - id: 111 - display_name: "Beehive" -} -item { - name: "/m/0642b4" - id: 112 - display_name: "Cupboard" -} -item { - name: "/m/09b5t" - id: 113 - display_name: "Chicken" -} -item { - name: "/m/04yx4" - id: 114 - display_name: "Man" -} -item { - name: "/m/01f8m5" - id: 115 - display_name: "Blue jay" -} -item { - name: "/m/015x4r" - id: 116 - display_name: "Cucumber" -} -item { - name: "/m/01j51" - id: 117 - display_name: "Balloon" -} -item { - name: "/m/02zt3" - id: 118 - display_name: "Kite" -} -item { - name: "/m/03tw93" - id: 119 - display_name: "Fireplace" -} -item { - name: "/m/01jfsr" - id: 120 - display_name: "Lantern" -} -item { - name: "/m/04ylt" - id: 121 - display_name: "Missile" -} -item { - name: "/m/0bt_c3" - id: 122 - display_name: "Book" -} -item { - name: "/m/0cmx8" - id: 123 - display_name: "Spoon" -} -item { - name: "/m/0hqkz" - id: 124 - display_name: "Grapefruit" -} -item { - name: "/m/071qp" - id: 125 - display_name: "Squirrel" -} -item { - name: "/m/0cyhj_" - id: 126 - display_name: "Orange" -} -item { - name: "/m/01xygc" - id: 127 - display_name: "Coat" -} -item { - name: "/m/0420v5" - id: 128 - display_name: "Punching bag" -} -item { - name: "/m/0898b" - id: 129 - display_name: "Zebra" -} -item { - name: "/m/01knjb" - id: 130 - display_name: "Billboard" -} -item { - name: "/m/0199g" - id: 131 - display_name: "Bicycle" -} -item { - name: "/m/03c7gz" - id: 132 - display_name: "Door handle" -} -item { - name: "/m/02x984l" - id: 133 - display_name: "Mechanical fan" -} -item { - name: "/m/04zwwv" - id: 134 - display_name: "Ring binder" -} -item { - name: "/m/04bcr3" - id: 135 - display_name: "Table" -} -item { - name: "/m/0gv1x" - id: 136 - display_name: "Parrot" -} -item { - name: "/m/01nq26" - id: 137 - display_name: "Sock" -} -item { - name: "/m/02s195" - id: 138 - display_name: "Vase" -} -item { - name: "/m/083kb" - id: 139 - display_name: "Weapon" -} -item { - name: "/m/06nrc" - id: 140 - display_name: "Shotgun" -} -item { - name: "/m/0jyfg" - id: 141 - display_name: "Glasses" -} -item { - name: "/m/0nybt" - id: 142 - display_name: "Seahorse" -} -item { - name: "/m/0176mf" - id: 143 - display_name: "Belt" -} -item { - name: "/m/01rzcn" - id: 144 - display_name: "Watercraft" -} -item { - name: "/m/0d4v4" - id: 145 - display_name: "Window" -} -item { - name: "/m/03bk1" - id: 146 - display_name: "Giraffe" -} -item { - name: "/m/096mb" - id: 147 - display_name: "Lion" -} -item { - name: "/m/0h9mv" - id: 148 - display_name: "Tire" -} -item { - name: "/m/07yv9" - id: 149 - display_name: "Vehicle" -} -item { - name: "/m/0ph39" - id: 150 - display_name: "Canoe" -} -item { - name: "/m/01rkbr" - id: 151 - display_name: "Tie" -} -item { - name: "/m/0gjbg72" - id: 152 - display_name: "Shelf" -} -item { - name: "/m/06z37_" - id: 153 - display_name: "Picture frame" -} -item { - name: "/m/01m4t" - id: 154 - display_name: "Printer" -} -item { - name: "/m/035r7c" - id: 155 - display_name: "Human leg" -} -item { - name: "/m/019jd" - id: 156 - display_name: "Boat" -} -item { - name: "/m/02tsc9" - id: 157 - display_name: "Slow cooker" -} -item { - name: "/m/015wgc" - id: 158 - display_name: "Croissant" -} -item { - name: "/m/0c06p" - id: 159 - display_name: "Candle" -} -item { - name: "/m/01dwwc" - id: 160 - display_name: "Pancake" -} -item { - name: "/m/034c16" - id: 161 - display_name: "Pillow" -} -item { - name: "/m/0242l" - id: 162 - display_name: "Coin" -} -item { - name: "/m/02lbcq" - id: 163 - display_name: "Stretcher" -} -item { - name: "/m/03nfch" - id: 164 - display_name: "Sandal" -} -item { - name: "/m/03bt1vf" - id: 165 - display_name: "Woman" -} -item { - name: "/m/01lynh" - id: 166 - display_name: "Stairs" -} -item { - name: "/m/03q5t" - id: 167 - display_name: "Harpsichord" -} -item { - name: "/m/0fqt361" - id: 168 - display_name: "Stool" -} -item { - name: "/m/01bjv" - id: 169 - display_name: "Bus" -} -item { - name: "/m/01s55n" - id: 170 - display_name: "Suitcase" -} -item { - name: "/m/0283dt1" - id: 171 - display_name: "Human mouth" -} -item { - name: "/m/01z1kdw" - id: 172 - display_name: "Juice" -} -item { - name: "/m/016m2d" - id: 173 - display_name: "Skull" -} -item { - name: "/m/02dgv" - id: 174 - display_name: "Door" -} -item { - name: "/m/07y_7" - id: 175 - display_name: "Violin" -} -item { - name: "/m/01_5g" - id: 176 - display_name: "Chopsticks" -} -item { - name: "/m/06_72j" - id: 177 - display_name: "Digital clock" -} -item { - name: "/m/0ftb8" - id: 178 - display_name: "Sunflower" -} -item { - name: "/m/0c29q" - id: 179 - display_name: "Leopard" -} -item { - name: "/m/0jg57" - id: 180 - display_name: "Bell pepper" -} -item { - name: "/m/02l8p9" - id: 181 - display_name: "Harbor seal" -} -item { - name: "/m/078jl" - id: 182 - display_name: "Snake" -} -item { - name: "/m/0llzx" - id: 183 - display_name: "Sewing machine" -} -item { - name: "/m/0dbvp" - id: 184 - display_name: "Goose" -} -item { - name: "/m/09ct_" - id: 185 - display_name: "Helicopter" -} -item { - name: "/m/0dkzw" - id: 186 - display_name: "Seat belt" -} -item { - name: "/m/02p5f1q" - id: 187 - display_name: "Coffee cup" -} -item { - name: "/m/0fx9l" - id: 188 - display_name: "Microwave oven" -} -item { - name: "/m/01b9xk" - id: 189 - display_name: "Hot dog" -} -item { - name: "/m/0b3fp9" - id: 190 - display_name: "Countertop" -} -item { - name: "/m/0h8n27j" - id: 191 - display_name: "Serving tray" -} -item { - name: "/m/0h8n6f9" - id: 192 - display_name: "Dog bed" -} -item { - name: "/m/01599" - id: 193 - display_name: "Beer" -} -item { - name: "/m/017ftj" - id: 194 - display_name: "Sunglasses" -} -item { - name: "/m/044r5d" - id: 195 - display_name: "Golf ball" -} -item { - name: "/m/01dwsz" - id: 196 - display_name: "Waffle" -} -item { - name: "/m/0cdl1" - id: 197 - display_name: "Palm tree" -} -item { - name: "/m/07gql" - id: 198 - display_name: "Trumpet" -} -item { - name: "/m/0hdln" - id: 199 - display_name: "Ruler" -} -item { - name: "/m/0zvk5" - id: 200 - display_name: "Helmet" -} -item { - name: "/m/012w5l" - id: 201 - display_name: "Ladder" -} -item { - name: "/m/021sj1" - id: 202 - display_name: "Office building" -} -item { - name: "/m/0bh9flk" - id: 203 - display_name: "Tablet computer" -} -item { - name: "/m/09gtd" - id: 204 - display_name: "Toilet paper" -} -item { - name: "/m/0jwn_" - id: 205 - display_name: "Pomegranate" -} -item { - name: "/m/02wv6h6" - id: 206 - display_name: "Skirt" -} -item { - name: "/m/02wv84t" - id: 207 - display_name: "Gas stove" -} -item { - name: "/m/021mn" - id: 208 - display_name: "Cookie" -} -item { - name: "/m/018p4k" - id: 209 - display_name: "Cart" -} -item { - name: "/m/06j2d" - id: 210 - display_name: "Raven" -} -item { - name: "/m/033cnk" - id: 211 - display_name: "Egg" -} -item { - name: "/m/01j3zr" - id: 212 - display_name: "Burrito" -} -item { - name: "/m/03fwl" - id: 213 - display_name: "Goat" -} -item { - name: "/m/058qzx" - id: 214 - display_name: "Kitchen knife" -} -item { - name: "/m/06_fw" - id: 215 - display_name: "Skateboard" -} -item { - name: "/m/02x8cch" - id: 216 - display_name: "Salt and pepper shakers" -} -item { - name: "/m/04g2r" - id: 217 - display_name: "Lynx" -} -item { - name: "/m/01b638" - id: 218 - display_name: "Boot" -} -item { - name: "/m/099ssp" - id: 219 - display_name: "Platter" -} -item { - name: "/m/071p9" - id: 220 - display_name: "Ski" -} -item { - name: "/m/01gkx_" - id: 221 - display_name: "Swimwear" -} -item { - name: "/m/0b_rs" - id: 222 - display_name: "Swimming pool" -} -item { - name: "/m/03v5tg" - id: 223 - display_name: "Drinking straw" -} -item { - name: "/m/01j5ks" - id: 224 - display_name: "Wrench" -} -item { - name: "/m/026t6" - id: 225 - display_name: "Drum" -} -item { - name: "/m/0_k2" - id: 226 - display_name: "Ant" -} -item { - name: "/m/039xj_" - id: 227 - display_name: "Human ear" -} -item { - name: "/m/01b7fy" - id: 228 - display_name: "Headphones" -} -item { - name: "/m/0220r2" - id: 229 - display_name: "Fountain" -} -item { - name: "/m/015p6" - id: 230 - display_name: "Bird" -} -item { - name: "/m/0fly7" - id: 231 - display_name: "Jeans" -} -item { - name: "/m/07c52" - id: 232 - display_name: "Television" -} -item { - name: "/m/0n28_" - id: 233 - display_name: "Crab" -} -item { - name: "/m/0hg7b" - id: 234 - display_name: "Microphone" -} -item { - name: "/m/019dx1" - id: 235 - display_name: "Home appliance" -} -item { - name: "/m/04vv5k" - id: 236 - display_name: "Snowplow" -} -item { - name: "/m/020jm" - id: 237 - display_name: "Beetle" -} -item { - name: "/m/047v4b" - id: 238 - display_name: "Artichoke" -} -item { - name: "/m/01xs3r" - id: 239 - display_name: "Jet ski" -} -item { - name: "/m/03kt2w" - id: 240 - display_name: "Stationary bicycle" -} -item { - name: "/m/03q69" - id: 241 - display_name: "Human hair" -} -item { - name: "/m/01dxs" - id: 242 - display_name: "Brown bear" -} -item { - name: "/m/01h8tj" - id: 243 - display_name: "Starfish" -} -item { - name: "/m/0dt3t" - id: 244 - display_name: "Fork" -} -item { - name: "/m/0cjq5" - id: 245 - display_name: "Lobster" -} -item { - name: "/m/0h8lkj8" - id: 246 - display_name: "Corded phone" -} -item { - name: "/m/0271t" - id: 247 - display_name: "Drink" -} -item { - name: "/m/03q5c7" - id: 248 - display_name: "Saucer" -} -item { - name: "/m/0fj52s" - id: 249 - display_name: "Carrot" -} -item { - name: "/m/03vt0" - id: 250 - display_name: "Insect" -} -item { - name: "/m/01x3z" - id: 251 - display_name: "Clock" -} -item { - name: "/m/0d5gx" - id: 252 - display_name: "Castle" -} -item { - name: "/m/0h8my_4" - id: 253 - display_name: "Tennis racket" -} -item { - name: "/m/03ldnb" - id: 254 - display_name: "Ceiling fan" -} -item { - name: "/m/0cjs7" - id: 255 - display_name: "Asparagus" -} -item { - name: "/m/0449p" - id: 256 - display_name: "Jaguar" -} -item { - name: "/m/04szw" - id: 257 - display_name: "Musical instrument" -} -item { - name: "/m/07jdr" - id: 258 - display_name: "Train" -} -item { - name: "/m/01yrx" - id: 259 - display_name: "Cat" -} -item { - name: "/m/06c54" - id: 260 - display_name: "Rifle" -} -item { - name: "/m/04h8sr" - id: 261 - display_name: "Dumbbell" -} -item { - name: "/m/050k8" - id: 262 - display_name: "Mobile phone" -} -item { - name: "/m/0pg52" - id: 263 - display_name: "Taxi" -} -item { - name: "/m/02f9f_" - id: 264 - display_name: "Shower" -} -item { - name: "/m/054fyh" - id: 265 - display_name: "Pitcher" -} -item { - name: "/m/09k_b" - id: 266 - display_name: "Lemon" -} -item { - name: "/m/03xxp" - id: 267 - display_name: "Invertebrate" -} -item { - name: "/m/0jly1" - id: 268 - display_name: "Turkey" -} -item { - name: "/m/06k2mb" - id: 269 - display_name: "High heels" -} -item { - name: "/m/04yqq2" - id: 270 - display_name: "Bust" -} -item { - name: "/m/0bwd_0j" - id: 271 - display_name: "Elephant" -} -item { - name: "/m/02h19r" - id: 272 - display_name: "Scarf" -} -item { - name: "/m/02zn6n" - id: 273 - display_name: "Barrel" -} -item { - name: "/m/07c6l" - id: 274 - display_name: "Trombone" -} -item { - name: "/m/05zsy" - id: 275 - display_name: "Pumpkin" -} -item { - name: "/m/025dyy" - id: 276 - display_name: "Box" -} -item { - name: "/m/07j87" - id: 277 - display_name: "Tomato" -} -item { - name: "/m/09ld4" - id: 278 - display_name: "Frog" -} -item { - name: "/m/01vbnl" - id: 279 - display_name: "Bidet" -} -item { - name: "/m/0dzct" - id: 280 - display_name: "Human face" -} -item { - name: "/m/03fp41" - id: 281 - display_name: "Houseplant" -} -item { - name: "/m/0h2r6" - id: 282 - display_name: "Van" -} -item { - name: "/m/0by6g" - id: 283 - display_name: "Shark" -} -item { - name: "/m/0cxn2" - id: 284 - display_name: "Ice cream" -} -item { - name: "/m/04tn4x" - id: 285 - display_name: "Swim cap" -} -item { - name: "/m/0f6wt" - id: 286 - display_name: "Falcon" -} -item { - name: "/m/05n4y" - id: 287 - display_name: "Ostrich" -} -item { - name: "/m/0gxl3" - id: 288 - display_name: "Handgun" -} -item { - name: "/m/02d9qx" - id: 289 - display_name: "Whiteboard" -} -item { - name: "/m/04m9y" - id: 290 - display_name: "Lizard" -} -item { - name: "/m/05z55" - id: 291 - display_name: "Pasta" -} -item { - name: "/m/01x3jk" - id: 292 - display_name: "Snowmobile" -} -item { - name: "/m/0h8l4fh" - id: 293 - display_name: "Light bulb" -} -item { - name: "/m/031b6r" - id: 294 - display_name: "Window blind" -} -item { - name: "/m/01tcjp" - id: 295 - display_name: "Muffin" -} -item { - name: "/m/01f91_" - id: 296 - display_name: "Pretzel" -} -item { - name: "/m/02522" - id: 297 - display_name: "Computer monitor" -} -item { - name: "/m/0319l" - id: 298 - display_name: "Horn" -} -item { - name: "/m/0c_jw" - id: 299 - display_name: "Furniture" -} -item { - name: "/m/0l515" - id: 300 - display_name: "Sandwich" -} -item { - name: "/m/0306r" - id: 301 - display_name: "Fox" -} -item { - name: "/m/0crjs" - id: 302 - display_name: "Convenience store" -} -item { - name: "/m/0ch_cf" - id: 303 - display_name: "Fish" -} -item { - name: "/m/02xwb" - id: 304 - display_name: "Fruit" -} -item { - name: "/m/01r546" - id: 305 - display_name: "Earrings" -} -item { - name: "/m/03rszm" - id: 306 - display_name: "Curtain" -} -item { - name: "/m/0388q" - id: 307 - display_name: "Grape" -} -item { - name: "/m/03m3pdh" - id: 308 - display_name: "Sofa bed" -} -item { - name: "/m/03k3r" - id: 309 - display_name: "Horse" -} -item { - name: "/m/0hf58v5" - id: 310 - display_name: "Luggage and bags" -} -item { - name: "/m/01y9k5" - id: 311 - display_name: "Desk" -} -item { - name: "/m/05441v" - id: 312 - display_name: "Crutch" -} -item { - name: "/m/03p3bw" - id: 313 - display_name: "Bicycle helmet" -} -item { - name: "/m/0175cv" - id: 314 - display_name: "Tick" -} -item { - name: "/m/0cmf2" - id: 315 - display_name: "Airplane" -} -item { - name: "/m/0ccs93" - id: 316 - display_name: "Canary" -} -item { - name: "/m/02d1br" - id: 317 - display_name: "Spatula" -} -item { - name: "/m/0gjkl" - id: 318 - display_name: "Watch" -} -item { - name: "/m/0jqgx" - id: 319 - display_name: "Lily" -} -item { - name: "/m/0h99cwc" - id: 320 - display_name: "Kitchen appliance" -} -item { - name: "/m/047j0r" - id: 321 - display_name: "Filing cabinet" -} -item { - name: "/m/0k5j" - id: 322 - display_name: "Aircraft" -} -item { - name: "/m/0h8n6ft" - id: 323 - display_name: "Cake stand" -} -item { - name: "/m/0gm28" - id: 324 - display_name: "Candy" -} -item { - name: "/m/0130jx" - id: 325 - display_name: "Sink" -} -item { - name: "/m/04rmv" - id: 326 - display_name: "Mouse" -} -item { - name: "/m/081qc" - id: 327 - display_name: "Wine" -} -item { - name: "/m/0qmmr" - id: 328 - display_name: "Wheelchair" -} -item { - name: "/m/03fj2" - id: 329 - display_name: "Goldfish" -} -item { - name: "/m/040b_t" - id: 330 - display_name: "Refrigerator" -} -item { - name: "/m/02y6n" - id: 331 - display_name: "French fries" -} -item { - name: "/m/0fqfqc" - id: 332 - display_name: "Drawer" -} -item { - name: "/m/030610" - id: 333 - display_name: "Treadmill" -} -item { - name: "/m/07kng9" - id: 334 - display_name: "Picnic basket" -} -item { - name: "/m/029b3" - id: 335 - display_name: "Dice" -} -item { - name: "/m/0fbw6" - id: 336 - display_name: "Cabbage" -} -item { - name: "/m/07qxg_" - id: 337 - display_name: "Football helmet" -} -item { - name: "/m/068zj" - id: 338 - display_name: "Pig" -} -item { - name: "/m/01g317" - id: 339 - display_name: "Person" -} -item { - name: "/m/01bfm9" - id: 340 - display_name: "Shorts" -} -item { - name: "/m/02068x" - id: 341 - display_name: "Gondola" -} -item { - name: "/m/0fz0h" - id: 342 - display_name: "Honeycomb" -} -item { - name: "/m/0jy4k" - id: 343 - display_name: "Doughnut" -} -item { - name: "/m/05kyg_" - id: 344 - display_name: "Chest of drawers" -} -item { - name: "/m/01prls" - id: 345 - display_name: "Land vehicle" -} -item { - name: "/m/01h44" - id: 346 - display_name: "Bat" -} -item { - name: "/m/08pbxl" - id: 347 - display_name: "Monkey" -} -item { - name: "/m/02gzp" - id: 348 - display_name: "Dagger" -} -item { - name: "/m/04brg2" - id: 349 - display_name: "Tableware" -} -item { - name: "/m/031n1" - id: 350 - display_name: "Human foot" -} -item { - name: "/m/02jvh9" - id: 351 - display_name: "Mug" -} -item { - name: "/m/046dlr" - id: 352 - display_name: "Alarm clock" -} -item { - name: "/m/0h8ntjv" - id: 353 - display_name: "Pressure cooker" -} -item { - name: "/m/0k65p" - id: 354 - display_name: "Human hand" -} -item { - name: "/m/011k07" - id: 355 - display_name: "Tortoise" -} -item { - name: "/m/03grzl" - id: 356 - display_name: "Baseball glove" -} -item { - name: "/m/06y5r" - id: 357 - display_name: "Sword" -} -item { - name: "/m/061_f" - id: 358 - display_name: "Pear" -} -item { - name: "/m/01cmb2" - id: 359 - display_name: "Miniskirt" -} -item { - name: "/m/01mqdt" - id: 360 - display_name: "Traffic sign" -} -item { - name: "/m/05r655" - id: 361 - display_name: "Girl" -} -item { - name: "/m/02p3w7d" - id: 362 - display_name: "Roller skates" -} -item { - name: "/m/029tx" - id: 363 - display_name: "Dinosaur" -} -item { - name: "/m/04m6gz" - id: 364 - display_name: "Porch" -} -item { - name: "/m/015h_t" - id: 365 - display_name: "Human beard" -} -item { - name: "/m/06pcq" - id: 366 - display_name: "Submarine sandwich" -} -item { - name: "/m/01bms0" - id: 367 - display_name: "Screwdriver" -} -item { - name: "/m/07fbm7" - id: 368 - display_name: "Strawberry" -} -item { - name: "/m/09tvcd" - id: 369 - display_name: "Wine glass" -} -item { - name: "/m/06nwz" - id: 370 - display_name: "Seafood" -} -item { - name: "/m/0dv9c" - id: 371 - display_name: "Racket" -} -item { - name: "/m/083wq" - id: 372 - display_name: "Wheel" -} -item { - name: "/m/0gd36" - id: 373 - display_name: "Sea lion" -} -item { - name: "/m/0138tl" - id: 374 - display_name: "Toy" -} -item { - name: "/m/07clx" - id: 375 - display_name: "Tea" -} -item { - name: "/m/05ctyq" - id: 376 - display_name: "Tennis ball" -} -item { - name: "/m/0bjyj5" - id: 377 - display_name: "Waste container" -} -item { - name: "/m/0dbzx" - id: 378 - display_name: "Mule" -} -item { - name: "/m/02ctlc" - id: 379 - display_name: "Cricket ball" -} -item { - name: "/m/0fp6w" - id: 380 - display_name: "Pineapple" -} -item { - name: "/m/0djtd" - id: 381 - display_name: "Coconut" -} -item { - name: "/m/0167gd" - id: 382 - display_name: "Doll" -} -item { - name: "/m/078n6m" - id: 383 - display_name: "Coffee table" -} -item { - name: "/m/0152hh" - id: 384 - display_name: "Snowman" -} -item { - name: "/m/04gth" - id: 385 - display_name: "Lavender" -} -item { - name: "/m/0ll1f78" - id: 386 - display_name: "Shrimp" -} -item { - name: "/m/0cffdh" - id: 387 - display_name: "Maple" -} -item { - name: "/m/025rp__" - id: 388 - display_name: "Cowboy hat" -} -item { - name: "/m/02_n6y" - id: 389 - display_name: "Goggles" -} -item { - name: "/m/0wdt60w" - id: 390 - display_name: "Rugby ball" -} -item { - name: "/m/0cydv" - id: 391 - display_name: "Caterpillar" -} -item { - name: "/m/01n5jq" - id: 392 - display_name: "Poster" -} -item { - name: "/m/09rvcxw" - id: 393 - display_name: "Rocket" -} -item { - name: "/m/013y1f" - id: 394 - display_name: "Organ" -} -item { - name: "/m/06ncr" - id: 395 - display_name: "Saxophone" -} -item { - name: "/m/015qff" - id: 396 - display_name: "Traffic light" -} -item { - name: "/m/024g6" - id: 397 - display_name: "Cocktail" -} -item { - name: "/m/05gqfk" - id: 398 - display_name: "Plastic bag" -} -item { - name: "/m/0dv77" - id: 399 - display_name: "Squash" -} -item { - name: "/m/052sf" - id: 400 - display_name: "Mushroom" -} -item { - name: "/m/0cdn1" - id: 401 - display_name: "Hamburger" -} -item { - name: "/m/03jbxj" - id: 402 - display_name: "Light switch" -} -item { - name: "/m/0cyfs" - id: 403 - display_name: "Parachute" -} -item { - name: "/m/0kmg4" - id: 404 - display_name: "Teddy bear" -} -item { - name: "/m/02cvgx" - id: 405 - display_name: "Winter melon" -} -item { - name: "/m/09kx5" - id: 406 - display_name: "Deer" -} -item { - name: "/m/057cc" - id: 407 - display_name: "Musical keyboard" -} -item { - name: "/m/02pkr5" - id: 408 - display_name: "Plumbing fixture" -} -item { - name: "/m/057p5t" - id: 409 - display_name: "Scoreboard" -} -item { - name: "/m/03g8mr" - id: 410 - display_name: "Baseball bat" -} -item { - name: "/m/0frqm" - id: 411 - display_name: "Envelope" -} -item { - name: "/m/03m3vtv" - id: 412 - display_name: "Adhesive tape" -} -item { - name: "/m/0584n8" - id: 413 - display_name: "Briefcase" -} -item { - name: "/m/014y4n" - id: 414 - display_name: "Paddle" -} -item { - name: "/m/01g3x7" - id: 415 - display_name: "Bow and arrow" -} -item { - name: "/m/07cx4" - id: 416 - display_name: "Telephone" -} -item { - name: "/m/07bgp" - id: 417 - display_name: "Sheep" -} -item { - name: "/m/032b3c" - id: 418 - display_name: "Jacket" -} -item { - name: "/m/01bl7v" - id: 419 - display_name: "Boy" -} -item { - name: "/m/0663v" - id: 420 - display_name: "Pizza" -} -item { - name: "/m/0cn6p" - id: 421 - display_name: "Otter" -} -item { - name: "/m/02rdsp" - id: 422 - display_name: "Office supplies" -} -item { - name: "/m/02crq1" - id: 423 - display_name: "Couch" -} -item { - name: "/m/01xqw" - id: 424 - display_name: "Cello" -} -item { - name: "/m/0cnyhnx" - id: 425 - display_name: "Bull" -} -item { - name: "/m/01x_v" - id: 426 - display_name: "Camel" -} -item { - name: "/m/018xm" - id: 427 - display_name: "Ball" -} -item { - name: "/m/09ddx" - id: 428 - display_name: "Duck" -} -item { - name: "/m/084zz" - id: 429 - display_name: "Whale" -} -item { - name: "/m/01n4qj" - id: 430 - display_name: "Shirt" -} -item { - name: "/m/07cmd" - id: 431 - display_name: "Tank" -} -item { - name: "/m/04_sv" - id: 432 - display_name: "Motorcycle" -} -item { - name: "/m/0mkg" - id: 433 - display_name: "Accordion" -} -item { - name: "/m/09d5_" - id: 434 - display_name: "Owl" -} -item { - name: "/m/0c568" - id: 435 - display_name: "Porcupine" -} -item { - name: "/m/02wbtzl" - id: 436 - display_name: "Sun hat" -} -item { - name: "/m/05bm6" - id: 437 - display_name: "Nail" -} -item { - name: "/m/01lsmm" - id: 438 - display_name: "Scissors" -} -item { - name: "/m/0dftk" - id: 439 - display_name: "Swan" -} -item { - name: "/m/0dtln" - id: 440 - display_name: "Lamp" -} -item { - name: "/m/0nl46" - id: 441 - display_name: "Crown" -} -item { - name: "/m/05r5c" - id: 442 - display_name: "Piano" -} -item { - name: "/m/06msq" - id: 443 - display_name: "Sculpture" -} -item { - name: "/m/0cd4d" - id: 444 - display_name: "Cheetah" -} -item { - name: "/m/05kms" - id: 445 - display_name: "Oboe" -} -item { - name: "/m/02jnhm" - id: 446 - display_name: "Tin can" -} -item { - name: "/m/0fldg" - id: 447 - display_name: "Mango" -} -item { - name: "/m/073bxn" - id: 448 - display_name: "Tripod" -} -item { - name: "/m/029bxz" - id: 449 - display_name: "Oven" -} -item { - name: "/m/020lf" - id: 450 - display_name: "Computer mouse" -} -item { - name: "/m/01btn" - id: 451 - display_name: "Barge" -} -item { - name: "/m/02vqfm" - id: 452 - display_name: "Coffee" -} -item { - name: "/m/06__v" - id: 453 - display_name: "Snowboard" -} -item { - name: "/m/043nyj" - id: 454 - display_name: "Common fig" -} -item { - name: "/m/0grw1" - id: 455 - display_name: "Salad" -} -item { - name: "/m/03hl4l9" - id: 456 - display_name: "Marine invertebrates" -} -item { - name: "/m/0hnnb" - id: 457 - display_name: "Umbrella" -} -item { - name: "/m/04c0y" - id: 458 - display_name: "Kangaroo" -} -item { - name: "/m/0dzf4" - id: 459 - display_name: "Human arm" -} -item { - name: "/m/07v9_z" - id: 460 - display_name: "Measuring cup" -} -item { - name: "/m/0f9_l" - id: 461 - display_name: "Snail" -} -item { - name: "/m/0703r8" - id: 462 - display_name: "Loveseat" -} -item { - name: "/m/01xyhv" - id: 463 - display_name: "Suit" -} -item { - name: "/m/01fh4r" - id: 464 - display_name: "Teapot" -} -item { - name: "/m/04dr76w" - id: 465 - display_name: "Bottle" -} -item { - name: "/m/0pcr" - id: 466 - display_name: "Alpaca" -} -item { - name: "/m/03s_tn" - id: 467 - display_name: "Kettle" -} -item { - name: "/m/07mhn" - id: 468 - display_name: "Trousers" -} -item { - name: "/m/01hrv5" - id: 469 - display_name: "Popcorn" -} -item { - name: "/m/019h78" - id: 470 - display_name: "Centipede" -} -item { - name: "/m/09kmb" - id: 471 - display_name: "Spider" -} -item { - name: "/m/0h23m" - id: 472 - display_name: "Sparrow" -} -item { - name: "/m/050gv4" - id: 473 - display_name: "Plate" -} -item { - name: "/m/01fb_0" - id: 474 - display_name: "Bagel" -} -item { - name: "/m/02w3_ws" - id: 475 - display_name: "Personal care" -} -item { - name: "/m/014j1m" - id: 476 - display_name: "Apple" -} -item { - name: "/m/01gmv2" - id: 477 - display_name: "Brassiere" -} -item { - name: "/m/04y4h8h" - id: 478 - display_name: "Bathroom cabinet" -} -item { - name: "/m/026qbn5" - id: 479 - display_name: "Studio couch" -} -item { - name: "/m/01m2v" - id: 480 - display_name: "Computer keyboard" -} -item { - name: "/m/05_5p_0" - id: 481 - display_name: "Table tennis racket" -} -item { - name: "/m/07030" - id: 482 - display_name: "Sushi" -} -item { - name: "/m/01s105" - id: 483 - display_name: "Cabinetry" -} -item { - name: "/m/033rq4" - id: 484 - display_name: "Street light" -} -item { - name: "/m/0162_1" - id: 485 - display_name: "Towel" -} -item { - name: "/m/02z51p" - id: 486 - display_name: "Nightstand" -} -item { - name: "/m/06mf6" - id: 487 - display_name: "Rabbit" -} -item { - name: "/m/02hj4" - id: 488 - display_name: "Dolphin" -} -item { - name: "/m/0bt9lr" - id: 489 - display_name: "Dog" -} -item { - name: "/m/08hvt4" - id: 490 - display_name: "Jug" -} -item { - name: "/m/084rd" - id: 491 - display_name: "Wok" -} -item { - name: "/m/01pns0" - id: 492 - display_name: "Fire hydrant" -} -item { - name: "/m/014sv8" - id: 493 - display_name: "Human eye" -} -item { - name: "/m/079cl" - id: 494 - display_name: "Skyscraper" -} -item { - name: "/m/01940j" - id: 495 - display_name: "Backpack" -} -item { - name: "/m/05vtc" - id: 496 - display_name: "Potato" -} -item { - name: "/m/02w3r3" - id: 497 - display_name: "Paper towel" -} -item { - name: "/m/054xkw" - id: 498 - display_name: "Lifejacket" -} -item { - name: "/m/01bqk0" - id: 499 - display_name: "Bicycle wheel" -} -item { - name: "/m/09g1w" - id: 500 - display_name: "Toilet" -} diff --git a/research/object_detection/data/oid_v4_label_map.pbtxt b/research/object_detection/data/oid_v4_label_map.pbtxt deleted file mode 100644 index 643b9e8ed5d..00000000000 --- a/research/object_detection/data/oid_v4_label_map.pbtxt +++ /dev/null @@ -1,3005 +0,0 @@ -item { - name: "/m/011k07" - id: 1 - display_name: "Tortoise" -} -item { - name: "/m/011q46kg" - id: 2 - display_name: "Container" -} -item { - name: "/m/012074" - id: 3 - display_name: "Magpie" -} -item { - name: "/m/0120dh" - id: 4 - display_name: "Sea turtle" -} -item { - name: "/m/01226z" - id: 5 - display_name: "Football" -} -item { - name: "/m/012n7d" - id: 6 - display_name: "Ambulance" -} -item { - name: "/m/012w5l" - id: 7 - display_name: "Ladder" -} -item { - name: "/m/012xff" - id: 8 - display_name: "Toothbrush" -} -item { - name: "/m/012ysf" - id: 9 - display_name: "Syringe" -} -item { - name: "/m/0130jx" - id: 10 - display_name: "Sink" -} -item { - name: "/m/0138tl" - id: 11 - display_name: "Toy" -} -item { - name: "/m/013y1f" - id: 12 - display_name: "Organ" -} -item { - name: "/m/01432t" - id: 13 - display_name: "Cassette deck" -} -item { - name: "/m/014j1m" - id: 14 - display_name: "Apple" -} -item { - name: "/m/014sv8" - id: 15 - display_name: "Human eye" -} -item { - name: "/m/014trl" - id: 16 - display_name: "Cosmetics" -} -item { - name: "/m/014y4n" - id: 17 - display_name: "Paddle" -} -item { - name: "/m/0152hh" - id: 18 - display_name: "Snowman" -} -item { - name: "/m/01599" - id: 19 - display_name: "Beer" -} -item { - name: "/m/01_5g" - id: 20 - display_name: "Chopsticks" -} -item { - name: "/m/015h_t" - id: 21 - display_name: "Human beard" -} -item { - name: "/m/015p6" - id: 22 - display_name: "Bird" -} -item { - name: "/m/015qbp" - id: 23 - display_name: "Parking meter" -} -item { - name: "/m/015qff" - id: 24 - display_name: "Traffic light" -} -item { - name: "/m/015wgc" - id: 25 - display_name: "Croissant" -} -item { - name: "/m/015x4r" - id: 26 - display_name: "Cucumber" -} -item { - name: "/m/015x5n" - id: 27 - display_name: "Radish" -} -item { - name: "/m/0162_1" - id: 28 - display_name: "Towel" -} -item { - name: "/m/0167gd" - id: 29 - display_name: "Doll" -} -item { - name: "/m/016m2d" - id: 30 - display_name: "Skull" -} -item { - name: "/m/0174k2" - id: 31 - display_name: "Washing machine" -} -item { - name: "/m/0174n1" - id: 32 - display_name: "Glove" -} -item { - name: "/m/0175cv" - id: 33 - display_name: "Tick" -} -item { - name: "/m/0176mf" - id: 34 - display_name: "Belt" -} -item { - name: "/m/017ftj" - id: 35 - display_name: "Sunglasses" -} -item { - name: "/m/018j2" - id: 36 - display_name: "Banjo" -} -item { - name: "/m/018p4k" - id: 37 - display_name: "Cart" -} -item { - name: "/m/018xm" - id: 38 - display_name: "Ball" -} -item { - name: "/m/01940j" - id: 39 - display_name: "Backpack" -} -item { - name: "/m/0199g" - id: 40 - display_name: "Bicycle" -} -item { - name: "/m/019dx1" - id: 41 - display_name: "Home appliance" -} -item { - name: "/m/019h78" - id: 42 - display_name: "Centipede" -} -item { - name: "/m/019jd" - id: 43 - display_name: "Boat" -} -item { - name: "/m/019w40" - id: 44 - display_name: "Surfboard" -} -item { - name: "/m/01b638" - id: 45 - display_name: "Boot" -} -item { - name: "/m/01b7fy" - id: 46 - display_name: "Headphones" -} -item { - name: "/m/01b9xk" - id: 47 - display_name: "Hot dog" -} -item { - name: "/m/01bfm9" - id: 48 - display_name: "Shorts" -} -item { - name: "/m/01_bhs" - id: 49 - display_name: "Fast food" -} -item { - name: "/m/01bjv" - id: 50 - display_name: "Bus" -} -item { - name: "/m/01bl7v" - id: 51 - display_name: "Boy" -} -item { - name: "/m/01bms0" - id: 52 - display_name: "Screwdriver" -} -item { - name: "/m/01bqk0" - id: 53 - display_name: "Bicycle wheel" -} -item { - name: "/m/01btn" - id: 54 - display_name: "Barge" -} -item { - name: "/m/01c648" - id: 55 - display_name: "Laptop" -} -item { - name: "/m/01cmb2" - id: 56 - display_name: "Miniskirt" -} -item { - name: "/m/01d380" - id: 57 - display_name: "Drill" -} -item { - name: "/m/01d40f" - id: 58 - display_name: "Dress" -} -item { - name: "/m/01dws" - id: 59 - display_name: "Bear" -} -item { - name: "/m/01dwsz" - id: 60 - display_name: "Waffle" -} -item { - name: "/m/01dwwc" - id: 61 - display_name: "Pancake" -} -item { - name: "/m/01dxs" - id: 62 - display_name: "Brown bear" -} -item { - name: "/m/01dy8n" - id: 63 - display_name: "Woodpecker" -} -item { - name: "/m/01f8m5" - id: 64 - display_name: "Blue jay" -} -item { - name: "/m/01f91_" - id: 65 - display_name: "Pretzel" -} -item { - name: "/m/01fb_0" - id: 66 - display_name: "Bagel" -} -item { - name: "/m/01fdzj" - id: 67 - display_name: "Tower" -} -item { - name: "/m/01fh4r" - id: 68 - display_name: "Teapot" -} -item { - name: "/m/01g317" - id: 69 - display_name: "Person" -} -item { - name: "/m/01g3x7" - id: 70 - display_name: "Bow and arrow" -} -item { - name: "/m/01gkx_" - id: 71 - display_name: "Swimwear" -} -item { - name: "/m/01gllr" - id: 72 - display_name: "Beehive" -} -item { - name: "/m/01gmv2" - id: 73 - display_name: "Brassiere" -} -item { - name: "/m/01h3n" - id: 74 - display_name: "Bee" -} -item { - name: "/m/01h44" - id: 75 - display_name: "Bat" -} -item { - name: "/m/01h8tj" - id: 76 - display_name: "Starfish" -} -item { - name: "/m/01hrv5" - id: 77 - display_name: "Popcorn" -} -item { - name: "/m/01j3zr" - id: 78 - display_name: "Burrito" -} -item { - name: "/m/01j4z9" - id: 79 - display_name: "Chainsaw" -} -item { - name: "/m/01j51" - id: 80 - display_name: "Balloon" -} -item { - name: "/m/01j5ks" - id: 81 - display_name: "Wrench" -} -item { - name: "/m/01j61q" - id: 82 - display_name: "Tent" -} -item { - name: "/m/01jfm_" - id: 83 - display_name: "Vehicle registration plate" -} -item { - name: "/m/01jfsr" - id: 84 - display_name: "Lantern" -} -item { - name: "/m/01k6s3" - id: 85 - display_name: "Toaster" -} -item { - name: "/m/01kb5b" - id: 86 - display_name: "Flashlight" -} -item { - name: "/m/01knjb" - id: 87 - display_name: "Billboard" -} -item { - name: "/m/01krhy" - id: 88 - display_name: "Tiara" -} -item { - name: "/m/01lcw4" - id: 89 - display_name: "Limousine" -} -item { - name: "/m/01llwg" - id: 90 - display_name: "Necklace" -} -item { - name: "/m/01lrl" - id: 91 - display_name: "Carnivore" -} -item { - name: "/m/01lsmm" - id: 92 - display_name: "Scissors" -} -item { - name: "/m/01lynh" - id: 93 - display_name: "Stairs" -} -item { - name: "/m/01m2v" - id: 94 - display_name: "Computer keyboard" -} -item { - name: "/m/01m4t" - id: 95 - display_name: "Printer" -} -item { - name: "/m/01mqdt" - id: 96 - display_name: "Traffic sign" -} -item { - name: "/m/01mzpv" - id: 97 - display_name: "Chair" -} -item { - name: "/m/01n4qj" - id: 98 - display_name: "Shirt" -} -item { - name: "/m/01n5jq" - id: 99 - display_name: "Poster" -} -item { - name: "/m/01nkt" - id: 100 - display_name: "Cheese" -} -item { - name: "/m/01nq26" - id: 101 - display_name: "Sock" -} -item { - name: "/m/01pns0" - id: 102 - display_name: "Fire hydrant" -} -item { - name: "/m/01prls" - id: 103 - display_name: "Land vehicle" -} -item { - name: "/m/01r546" - id: 104 - display_name: "Earrings" -} -item { - name: "/m/01rkbr" - id: 105 - display_name: "Tie" -} -item { - name: "/m/01rzcn" - id: 106 - display_name: "Watercraft" -} -item { - name: "/m/01s105" - id: 107 - display_name: "Cabinetry" -} -item { - name: "/m/01s55n" - id: 108 - display_name: "Suitcase" -} -item { - name: "/m/01tcjp" - id: 109 - display_name: "Muffin" -} -item { - name: "/m/01vbnl" - id: 110 - display_name: "Bidet" -} -item { - name: "/m/01ww8y" - id: 111 - display_name: "Snack" -} -item { - name: "/m/01x3jk" - id: 112 - display_name: "Snowmobile" -} -item { - name: "/m/01x3z" - id: 113 - display_name: "Clock" -} -item { - name: "/m/01xgg_" - id: 114 - display_name: "Medical equipment" -} -item { - name: "/m/01xq0k1" - id: 115 - display_name: "Cattle" -} -item { - name: "/m/01xqw" - id: 116 - display_name: "Cello" -} -item { - name: "/m/01xs3r" - id: 117 - display_name: "Jet ski" -} -item { - name: "/m/01x_v" - id: 118 - display_name: "Camel" -} -item { - name: "/m/01xygc" - id: 119 - display_name: "Coat" -} -item { - name: "/m/01xyhv" - id: 120 - display_name: "Suit" -} -item { - name: "/m/01y9k5" - id: 121 - display_name: "Desk" -} -item { - name: "/m/01yrx" - id: 122 - display_name: "Cat" -} -item { - name: "/m/01yx86" - id: 123 - display_name: "Bronze sculpture" -} -item { - name: "/m/01z1kdw" - id: 124 - display_name: "Juice" -} -item { - name: "/m/02068x" - id: 125 - display_name: "Gondola" -} -item { - name: "/m/020jm" - id: 126 - display_name: "Beetle" -} -item { - name: "/m/020kz" - id: 127 - display_name: "Cannon" -} -item { - name: "/m/020lf" - id: 128 - display_name: "Computer mouse" -} -item { - name: "/m/021mn" - id: 129 - display_name: "Cookie" -} -item { - name: "/m/021sj1" - id: 130 - display_name: "Office building" -} -item { - name: "/m/0220r2" - id: 131 - display_name: "Fountain" -} -item { - name: "/m/0242l" - id: 132 - display_name: "Coin" -} -item { - name: "/m/024d2" - id: 133 - display_name: "Calculator" -} -item { - name: "/m/024g6" - id: 134 - display_name: "Cocktail" -} -item { - name: "/m/02522" - id: 135 - display_name: "Computer monitor" -} -item { - name: "/m/025dyy" - id: 136 - display_name: "Box" -} -item { - name: "/m/025fsf" - id: 137 - display_name: "Stapler" -} -item { - name: "/m/025nd" - id: 138 - display_name: "Christmas tree" -} -item { - name: "/m/025rp__" - id: 139 - display_name: "Cowboy hat" -} -item { - name: "/m/0268lbt" - id: 140 - display_name: "Hiking equipment" -} -item { - name: "/m/026qbn5" - id: 141 - display_name: "Studio couch" -} -item { - name: "/m/026t6" - id: 142 - display_name: "Drum" -} -item { - name: "/m/0270h" - id: 143 - display_name: "Dessert" -} -item { - name: "/m/0271qf7" - id: 144 - display_name: "Wine rack" -} -item { - name: "/m/0271t" - id: 145 - display_name: "Drink" -} -item { - name: "/m/027pcv" - id: 146 - display_name: "Zucchini" -} -item { - name: "/m/027rl48" - id: 147 - display_name: "Ladle" -} -item { - name: "/m/0283dt1" - id: 148 - display_name: "Human mouth" -} -item { - name: "/m/0284d" - id: 149 - display_name: "Dairy" -} -item { - name: "/m/029b3" - id: 150 - display_name: "Dice" -} -item { - name: "/m/029bxz" - id: 151 - display_name: "Oven" -} -item { - name: "/m/029tx" - id: 152 - display_name: "Dinosaur" -} -item { - name: "/m/02bm9n" - id: 153 - display_name: "Ratchet" -} -item { - name: "/m/02crq1" - id: 154 - display_name: "Couch" -} -item { - name: "/m/02ctlc" - id: 155 - display_name: "Cricket ball" -} -item { - name: "/m/02cvgx" - id: 156 - display_name: "Winter melon" -} -item { - name: "/m/02d1br" - id: 157 - display_name: "Spatula" -} -item { - name: "/m/02d9qx" - id: 158 - display_name: "Whiteboard" -} -item { - name: "/m/02ddwp" - id: 159 - display_name: "Pencil sharpener" -} -item { - name: "/m/02dgv" - id: 160 - display_name: "Door" -} -item { - name: "/m/02dl1y" - id: 161 - display_name: "Hat" -} -item { - name: "/m/02f9f_" - id: 162 - display_name: "Shower" -} -item { - name: "/m/02fh7f" - id: 163 - display_name: "Eraser" -} -item { - name: "/m/02fq_6" - id: 164 - display_name: "Fedora" -} -item { - name: "/m/02g30s" - id: 165 - display_name: "Guacamole" -} -item { - name: "/m/02gzp" - id: 166 - display_name: "Dagger" -} -item { - name: "/m/02h19r" - id: 167 - display_name: "Scarf" -} -item { - name: "/m/02hj4" - id: 168 - display_name: "Dolphin" -} -item { - name: "/m/02jfl0" - id: 169 - display_name: "Sombrero" -} -item { - name: "/m/02jnhm" - id: 170 - display_name: "Tin can" -} -item { - name: "/m/02jvh9" - id: 171 - display_name: "Mug" -} -item { - name: "/m/02jz0l" - id: 172 - display_name: "Tap" -} -item { - name: "/m/02l8p9" - id: 173 - display_name: "Harbor seal" -} -item { - name: "/m/02lbcq" - id: 174 - display_name: "Stretcher" -} -item { - name: "/m/02mqfb" - id: 175 - display_name: "Can opener" -} -item { - name: "/m/02_n6y" - id: 176 - display_name: "Goggles" -} -item { - name: "/m/02p0tk3" - id: 177 - display_name: "Human body" -} -item { - name: "/m/02p3w7d" - id: 178 - display_name: "Roller skates" -} -item { - name: "/m/02p5f1q" - id: 179 - display_name: "Coffee cup" -} -item { - name: "/m/02pdsw" - id: 180 - display_name: "Cutting board" -} -item { - name: "/m/02pjr4" - id: 181 - display_name: "Blender" -} -item { - name: "/m/02pkr5" - id: 182 - display_name: "Plumbing fixture" -} -item { - name: "/m/02pv19" - id: 183 - display_name: "Stop sign" -} -item { - name: "/m/02rdsp" - id: 184 - display_name: "Office supplies" -} -item { - name: "/m/02rgn06" - id: 185 - display_name: "Volleyball" -} -item { - name: "/m/02s195" - id: 186 - display_name: "Vase" -} -item { - name: "/m/02tsc9" - id: 187 - display_name: "Slow cooker" -} -item { - name: "/m/02vkqh8" - id: 188 - display_name: "Wardrobe" -} -item { - name: "/m/02vqfm" - id: 189 - display_name: "Coffee" -} -item { - name: "/m/02vwcm" - id: 190 - display_name: "Whisk" -} -item { - name: "/m/02w3r3" - id: 191 - display_name: "Paper towel" -} -item { - name: "/m/02w3_ws" - id: 192 - display_name: "Personal care" -} -item { - name: "/m/02wbm" - id: 193 - display_name: "Food" -} -item { - name: "/m/02wbtzl" - id: 194 - display_name: "Sun hat" -} -item { - name: "/m/02wg_p" - id: 195 - display_name: "Tree house" -} -item { - name: "/m/02wmf" - id: 196 - display_name: "Flying disc" -} -item { - name: "/m/02wv6h6" - id: 197 - display_name: "Skirt" -} -item { - name: "/m/02wv84t" - id: 198 - display_name: "Gas stove" -} -item { - name: "/m/02x8cch" - id: 199 - display_name: "Salt and pepper shakers" -} -item { - name: "/m/02x984l" - id: 200 - display_name: "Mechanical fan" -} -item { - name: "/m/02xb7qb" - id: 201 - display_name: "Face powder" -} -item { - name: "/m/02xqq" - id: 202 - display_name: "Fax" -} -item { - name: "/m/02xwb" - id: 203 - display_name: "Fruit" -} -item { - name: "/m/02y6n" - id: 204 - display_name: "French fries" -} -item { - name: "/m/02z51p" - id: 205 - display_name: "Nightstand" -} -item { - name: "/m/02zn6n" - id: 206 - display_name: "Barrel" -} -item { - name: "/m/02zt3" - id: 207 - display_name: "Kite" -} -item { - name: "/m/02zvsm" - id: 208 - display_name: "Tart" -} -item { - name: "/m/030610" - id: 209 - display_name: "Treadmill" -} -item { - name: "/m/0306r" - id: 210 - display_name: "Fox" -} -item { - name: "/m/03120" - id: 211 - display_name: "Flag" -} -item { - name: "/m/0319l" - id: 212 - display_name: "Horn" -} -item { - name: "/m/031b6r" - id: 213 - display_name: "Window blind" -} -item { - name: "/m/031n1" - id: 214 - display_name: "Human foot" -} -item { - name: "/m/0323sq" - id: 215 - display_name: "Golf cart" -} -item { - name: "/m/032b3c" - id: 216 - display_name: "Jacket" -} -item { - name: "/m/033cnk" - id: 217 - display_name: "Egg" -} -item { - name: "/m/033rq4" - id: 218 - display_name: "Street light" -} -item { - name: "/m/0342h" - id: 219 - display_name: "Guitar" -} -item { - name: "/m/034c16" - id: 220 - display_name: "Pillow" -} -item { - name: "/m/035r7c" - id: 221 - display_name: "Human leg" -} -item { - name: "/m/035vxb" - id: 222 - display_name: "Isopod" -} -item { - name: "/m/0388q" - id: 223 - display_name: "Grape" -} -item { - name: "/m/039xj_" - id: 224 - display_name: "Human ear" -} -item { - name: "/m/03bbps" - id: 225 - display_name: "Power plugs and sockets" -} -item { - name: "/m/03bj1" - id: 226 - display_name: "Panda" -} -item { - name: "/m/03bk1" - id: 227 - display_name: "Giraffe" -} -item { - name: "/m/03bt1vf" - id: 228 - display_name: "Woman" -} -item { - name: "/m/03c7gz" - id: 229 - display_name: "Door handle" -} -item { - name: "/m/03d443" - id: 230 - display_name: "Rhinoceros" -} -item { - name: "/m/03dnzn" - id: 231 - display_name: "Bathtub" -} -item { - name: "/m/03fj2" - id: 232 - display_name: "Goldfish" -} -item { - name: "/m/03fp41" - id: 233 - display_name: "Houseplant" -} -item { - name: "/m/03fwl" - id: 234 - display_name: "Goat" -} -item { - name: "/m/03g8mr" - id: 235 - display_name: "Baseball bat" -} -item { - name: "/m/03grzl" - id: 236 - display_name: "Baseball glove" -} -item { - name: "/m/03hj559" - id: 237 - display_name: "Mixing bowl" -} -item { - name: "/m/03hl4l9" - id: 238 - display_name: "Marine invertebrates" -} -item { - name: "/m/03hlz0c" - id: 239 - display_name: "Kitchen utensil" -} -item { - name: "/m/03jbxj" - id: 240 - display_name: "Light switch" -} -item { - name: "/m/03jm5" - id: 241 - display_name: "House" -} -item { - name: "/m/03k3r" - id: 242 - display_name: "Horse" -} -item { - name: "/m/03kt2w" - id: 243 - display_name: "Stationary bicycle" -} -item { - name: "/m/03l9g" - id: 244 - display_name: "Hammer" -} -item { - name: "/m/03ldnb" - id: 245 - display_name: "Ceiling fan" -} -item { - name: "/m/03m3pdh" - id: 246 - display_name: "Sofa bed" -} -item { - name: "/m/03m3vtv" - id: 247 - display_name: "Adhesive tape" -} -item { - name: "/m/03m5k" - id: 248 - display_name: "Harp" -} -item { - name: "/m/03nfch" - id: 249 - display_name: "Sandal" -} -item { - name: "/m/03p3bw" - id: 250 - display_name: "Bicycle helmet" -} -item { - name: "/m/03q5c7" - id: 251 - display_name: "Saucer" -} -item { - name: "/m/03q5t" - id: 252 - display_name: "Harpsichord" -} -item { - name: "/m/03q69" - id: 253 - display_name: "Human hair" -} -item { - name: "/m/03qhv5" - id: 254 - display_name: "Heater" -} -item { - name: "/m/03qjg" - id: 255 - display_name: "Harmonica" -} -item { - name: "/m/03qrc" - id: 256 - display_name: "Hamster" -} -item { - name: "/m/03rszm" - id: 257 - display_name: "Curtain" -} -item { - name: "/m/03ssj5" - id: 258 - display_name: "Bed" -} -item { - name: "/m/03s_tn" - id: 259 - display_name: "Kettle" -} -item { - name: "/m/03tw93" - id: 260 - display_name: "Fireplace" -} -item { - name: "/m/03txqz" - id: 261 - display_name: "Scale" -} -item { - name: "/m/03v5tg" - id: 262 - display_name: "Drinking straw" -} -item { - name: "/m/03vt0" - id: 263 - display_name: "Insect" -} -item { - name: "/m/03wvsk" - id: 264 - display_name: "Hair dryer" -} -item { - name: "/m/03_wxk" - id: 265 - display_name: "Kitchenware" -} -item { - name: "/m/03wym" - id: 266 - display_name: "Indoor rower" -} -item { - name: "/m/03xxp" - id: 267 - display_name: "Invertebrate" -} -item { - name: "/m/03y6mg" - id: 268 - display_name: "Food processor" -} -item { - name: "/m/03__z0" - id: 269 - display_name: "Bookcase" -} -item { - name: "/m/040b_t" - id: 270 - display_name: "Refrigerator" -} -item { - name: "/m/04169hn" - id: 271 - display_name: "Wood-burning stove" -} -item { - name: "/m/0420v5" - id: 272 - display_name: "Punching bag" -} -item { - name: "/m/043nyj" - id: 273 - display_name: "Common fig" -} -item { - name: "/m/0440zs" - id: 274 - display_name: "Cocktail shaker" -} -item { - name: "/m/0449p" - id: 275 - display_name: "Jaguar" -} -item { - name: "/m/044r5d" - id: 276 - display_name: "Golf ball" -} -item { - name: "/m/0463sg" - id: 277 - display_name: "Fashion accessory" -} -item { - name: "/m/046dlr" - id: 278 - display_name: "Alarm clock" -} -item { - name: "/m/047j0r" - id: 279 - display_name: "Filing cabinet" -} -item { - name: "/m/047v4b" - id: 280 - display_name: "Artichoke" -} -item { - name: "/m/04bcr3" - id: 281 - display_name: "Table" -} -item { - name: "/m/04brg2" - id: 282 - display_name: "Tableware" -} -item { - name: "/m/04c0y" - id: 283 - display_name: "Kangaroo" -} -item { - name: "/m/04cp_" - id: 284 - display_name: "Koala" -} -item { - name: "/m/04ctx" - id: 285 - display_name: "Knife" -} -item { - name: "/m/04dr76w" - id: 286 - display_name: "Bottle" -} -item { - name: "/m/04f5ws" - id: 287 - display_name: "Bottle opener" -} -item { - name: "/m/04g2r" - id: 288 - display_name: "Lynx" -} -item { - name: "/m/04gth" - id: 289 - display_name: "Lavender" -} -item { - name: "/m/04h7h" - id: 290 - display_name: "Lighthouse" -} -item { - name: "/m/04h8sr" - id: 291 - display_name: "Dumbbell" -} -item { - name: "/m/04hgtk" - id: 292 - display_name: "Human head" -} -item { - name: "/m/04kkgm" - id: 293 - display_name: "Bowl" -} -item { - name: "/m/04lvq_" - id: 294 - display_name: "Humidifier" -} -item { - name: "/m/04m6gz" - id: 295 - display_name: "Porch" -} -item { - name: "/m/04m9y" - id: 296 - display_name: "Lizard" -} -item { - name: "/m/04p0qw" - id: 297 - display_name: "Billiard table" -} -item { - name: "/m/04rky" - id: 298 - display_name: "Mammal" -} -item { - name: "/m/04rmv" - id: 299 - display_name: "Mouse" -} -item { - name: "/m/04_sv" - id: 300 - display_name: "Motorcycle" -} -item { - name: "/m/04szw" - id: 301 - display_name: "Musical instrument" -} -item { - name: "/m/04tn4x" - id: 302 - display_name: "Swim cap" -} -item { - name: "/m/04v6l4" - id: 303 - display_name: "Frying pan" -} -item { - name: "/m/04vv5k" - id: 304 - display_name: "Snowplow" -} -item { - name: "/m/04y4h8h" - id: 305 - display_name: "Bathroom cabinet" -} -item { - name: "/m/04ylt" - id: 306 - display_name: "Missile" -} -item { - name: "/m/04yqq2" - id: 307 - display_name: "Bust" -} -item { - name: "/m/04yx4" - id: 308 - display_name: "Man" -} -item { - name: "/m/04z4wx" - id: 309 - display_name: "Waffle iron" -} -item { - name: "/m/04zpv" - id: 310 - display_name: "Milk" -} -item { - name: "/m/04zwwv" - id: 311 - display_name: "Ring binder" -} -item { - name: "/m/050gv4" - id: 312 - display_name: "Plate" -} -item { - name: "/m/050k8" - id: 313 - display_name: "Mobile phone" -} -item { - name: "/m/052lwg6" - id: 314 - display_name: "Baked goods" -} -item { - name: "/m/052sf" - id: 315 - display_name: "Mushroom" -} -item { - name: "/m/05441v" - id: 316 - display_name: "Crutch" -} -item { - name: "/m/054fyh" - id: 317 - display_name: "Pitcher" -} -item { - name: "/m/054_l" - id: 318 - display_name: "Mirror" -} -item { - name: "/m/054xkw" - id: 319 - display_name: "Lifejacket" -} -item { - name: "/m/05_5p_0" - id: 320 - display_name: "Table tennis racket" -} -item { - name: "/m/05676x" - id: 321 - display_name: "Pencil case" -} -item { - name: "/m/057cc" - id: 322 - display_name: "Musical keyboard" -} -item { - name: "/m/057p5t" - id: 323 - display_name: "Scoreboard" -} -item { - name: "/m/0584n8" - id: 324 - display_name: "Briefcase" -} -item { - name: "/m/058qzx" - id: 325 - display_name: "Kitchen knife" -} -item { - name: "/m/05bm6" - id: 326 - display_name: "Nail" -} -item { - name: "/m/05ctyq" - id: 327 - display_name: "Tennis ball" -} -item { - name: "/m/05gqfk" - id: 328 - display_name: "Plastic bag" -} -item { - name: "/m/05kms" - id: 329 - display_name: "Oboe" -} -item { - name: "/m/05kyg_" - id: 330 - display_name: "Chest of drawers" -} -item { - name: "/m/05n4y" - id: 331 - display_name: "Ostrich" -} -item { - name: "/m/05r5c" - id: 332 - display_name: "Piano" -} -item { - name: "/m/05r655" - id: 333 - display_name: "Girl" -} -item { - name: "/m/05s2s" - id: 334 - display_name: "Plant" -} -item { - name: "/m/05vtc" - id: 335 - display_name: "Potato" -} -item { - name: "/m/05w9t9" - id: 336 - display_name: "Hair spray" -} -item { - name: "/m/05y5lj" - id: 337 - display_name: "Sports equipment" -} -item { - name: "/m/05z55" - id: 338 - display_name: "Pasta" -} -item { - name: "/m/05z6w" - id: 339 - display_name: "Penguin" -} -item { - name: "/m/05zsy" - id: 340 - display_name: "Pumpkin" -} -item { - name: "/m/061_f" - id: 341 - display_name: "Pear" -} -item { - name: "/m/061hd_" - id: 342 - display_name: "Infant bed" -} -item { - name: "/m/0633h" - id: 343 - display_name: "Polar bear" -} -item { - name: "/m/063rgb" - id: 344 - display_name: "Mixer" -} -item { - name: "/m/0642b4" - id: 345 - display_name: "Cupboard" -} -item { - name: "/m/065h6l" - id: 346 - display_name: "Jacuzzi" -} -item { - name: "/m/0663v" - id: 347 - display_name: "Pizza" -} -item { - name: "/m/06_72j" - id: 348 - display_name: "Digital clock" -} -item { - name: "/m/068zj" - id: 349 - display_name: "Pig" -} -item { - name: "/m/06bt6" - id: 350 - display_name: "Reptile" -} -item { - name: "/m/06c54" - id: 351 - display_name: "Rifle" -} -item { - name: "/m/06c7f7" - id: 352 - display_name: "Lipstick" -} -item { - name: "/m/06_fw" - id: 353 - display_name: "Skateboard" -} -item { - name: "/m/06j2d" - id: 354 - display_name: "Raven" -} -item { - name: "/m/06k2mb" - id: 355 - display_name: "High heels" -} -item { - name: "/m/06l9r" - id: 356 - display_name: "Red panda" -} -item { - name: "/m/06m11" - id: 357 - display_name: "Rose" -} -item { - name: "/m/06mf6" - id: 358 - display_name: "Rabbit" -} -item { - name: "/m/06msq" - id: 359 - display_name: "Sculpture" -} -item { - name: "/m/06ncr" - id: 360 - display_name: "Saxophone" -} -item { - name: "/m/06nrc" - id: 361 - display_name: "Shotgun" -} -item { - name: "/m/06nwz" - id: 362 - display_name: "Seafood" -} -item { - name: "/m/06pcq" - id: 363 - display_name: "Submarine sandwich" -} -item { - name: "/m/06__v" - id: 364 - display_name: "Snowboard" -} -item { - name: "/m/06y5r" - id: 365 - display_name: "Sword" -} -item { - name: "/m/06z37_" - id: 366 - display_name: "Picture frame" -} -item { - name: "/m/07030" - id: 367 - display_name: "Sushi" -} -item { - name: "/m/0703r8" - id: 368 - display_name: "Loveseat" -} -item { - name: "/m/071p9" - id: 369 - display_name: "Ski" -} -item { - name: "/m/071qp" - id: 370 - display_name: "Squirrel" -} -item { - name: "/m/073bxn" - id: 371 - display_name: "Tripod" -} -item { - name: "/m/073g6" - id: 372 - display_name: "Stethoscope" -} -item { - name: "/m/074d1" - id: 373 - display_name: "Submarine" -} -item { - name: "/m/0755b" - id: 374 - display_name: "Scorpion" -} -item { - name: "/m/076bq" - id: 375 - display_name: "Segway" -} -item { - name: "/m/076lb9" - id: 376 - display_name: "Training bench" -} -item { - name: "/m/078jl" - id: 377 - display_name: "Snake" -} -item { - name: "/m/078n6m" - id: 378 - display_name: "Coffee table" -} -item { - name: "/m/079cl" - id: 379 - display_name: "Skyscraper" -} -item { - name: "/m/07bgp" - id: 380 - display_name: "Sheep" -} -item { - name: "/m/07c52" - id: 381 - display_name: "Television" -} -item { - name: "/m/07c6l" - id: 382 - display_name: "Trombone" -} -item { - name: "/m/07clx" - id: 383 - display_name: "Tea" -} -item { - name: "/m/07cmd" - id: 384 - display_name: "Tank" -} -item { - name: "/m/07crc" - id: 385 - display_name: "Taco" -} -item { - name: "/m/07cx4" - id: 386 - display_name: "Telephone" -} -item { - name: "/m/07dd4" - id: 387 - display_name: "Torch" -} -item { - name: "/m/07dm6" - id: 388 - display_name: "Tiger" -} -item { - name: "/m/07fbm7" - id: 389 - display_name: "Strawberry" -} -item { - name: "/m/07gql" - id: 390 - display_name: "Trumpet" -} -item { - name: "/m/07j7r" - id: 391 - display_name: "Tree" -} -item { - name: "/m/07j87" - id: 392 - display_name: "Tomato" -} -item { - name: "/m/07jdr" - id: 393 - display_name: "Train" -} -item { - name: "/m/07k1x" - id: 394 - display_name: "Tool" -} -item { - name: "/m/07kng9" - id: 395 - display_name: "Picnic basket" -} -item { - name: "/m/07mcwg" - id: 396 - display_name: "Cooking spray" -} -item { - name: "/m/07mhn" - id: 397 - display_name: "Trousers" -} -item { - name: "/m/07pj7bq" - id: 398 - display_name: "Bowling equipment" -} -item { - name: "/m/07qxg_" - id: 399 - display_name: "Football helmet" -} -item { - name: "/m/07r04" - id: 400 - display_name: "Truck" -} -item { - name: "/m/07v9_z" - id: 401 - display_name: "Measuring cup" -} -item { - name: "/m/07xyvk" - id: 402 - display_name: "Coffeemaker" -} -item { - name: "/m/07y_7" - id: 403 - display_name: "Violin" -} -item { - name: "/m/07yv9" - id: 404 - display_name: "Vehicle" -} -item { - name: "/m/080hkjn" - id: 405 - display_name: "Handbag" -} -item { - name: "/m/080n7g" - id: 406 - display_name: "Paper cutter" -} -item { - name: "/m/081qc" - id: 407 - display_name: "Wine" -} -item { - name: "/m/083kb" - id: 408 - display_name: "Weapon" -} -item { - name: "/m/083wq" - id: 409 - display_name: "Wheel" -} -item { - name: "/m/084hf" - id: 410 - display_name: "Worm" -} -item { - name: "/m/084rd" - id: 411 - display_name: "Wok" -} -item { - name: "/m/084zz" - id: 412 - display_name: "Whale" -} -item { - name: "/m/0898b" - id: 413 - display_name: "Zebra" -} -item { - name: "/m/08dz3q" - id: 414 - display_name: "Auto part" -} -item { - name: "/m/08hvt4" - id: 415 - display_name: "Jug" -} -item { - name: "/m/08ks85" - id: 416 - display_name: "Pizza cutter" -} -item { - name: "/m/08p92x" - id: 417 - display_name: "Cream" -} -item { - name: "/m/08pbxl" - id: 418 - display_name: "Monkey" -} -item { - name: "/m/096mb" - id: 419 - display_name: "Lion" -} -item { - name: "/m/09728" - id: 420 - display_name: "Bread" -} -item { - name: "/m/099ssp" - id: 421 - display_name: "Platter" -} -item { - name: "/m/09b5t" - id: 422 - display_name: "Chicken" -} -item { - name: "/m/09csl" - id: 423 - display_name: "Eagle" -} -item { - name: "/m/09ct_" - id: 424 - display_name: "Helicopter" -} -item { - name: "/m/09d5_" - id: 425 - display_name: "Owl" -} -item { - name: "/m/09ddx" - id: 426 - display_name: "Duck" -} -item { - name: "/m/09dzg" - id: 427 - display_name: "Turtle" -} -item { - name: "/m/09f20" - id: 428 - display_name: "Hippopotamus" -} -item { - name: "/m/09f_2" - id: 429 - display_name: "Crocodile" -} -item { - name: "/m/09g1w" - id: 430 - display_name: "Toilet" -} -item { - name: "/m/09gtd" - id: 431 - display_name: "Toilet paper" -} -item { - name: "/m/09gys" - id: 432 - display_name: "Squid" -} -item { - name: "/m/09j2d" - id: 433 - display_name: "Clothing" -} -item { - name: "/m/09j5n" - id: 434 - display_name: "Footwear" -} -item { - name: "/m/09k_b" - id: 435 - display_name: "Lemon" -} -item { - name: "/m/09kmb" - id: 436 - display_name: "Spider" -} -item { - name: "/m/09kx5" - id: 437 - display_name: "Deer" -} -item { - name: "/m/09ld4" - id: 438 - display_name: "Frog" -} -item { - name: "/m/09qck" - id: 439 - display_name: "Banana" -} -item { - name: "/m/09rvcxw" - id: 440 - display_name: "Rocket" -} -item { - name: "/m/09tvcd" - id: 441 - display_name: "Wine glass" -} -item { - name: "/m/0b3fp9" - id: 442 - display_name: "Countertop" -} -item { - name: "/m/0bh9flk" - id: 443 - display_name: "Tablet computer" -} -item { - name: "/m/0bjyj5" - id: 444 - display_name: "Waste container" -} -item { - name: "/m/0b_rs" - id: 445 - display_name: "Swimming pool" -} -item { - name: "/m/0bt9lr" - id: 446 - display_name: "Dog" -} -item { - name: "/m/0bt_c3" - id: 447 - display_name: "Book" -} -item { - name: "/m/0bwd_0j" - id: 448 - display_name: "Elephant" -} -item { - name: "/m/0by6g" - id: 449 - display_name: "Shark" -} -item { - name: "/m/0c06p" - id: 450 - display_name: "Candle" -} -item { - name: "/m/0c29q" - id: 451 - display_name: "Leopard" -} -item { - name: "/m/0c2jj" - id: 452 - display_name: "Axe" -} -item { - name: "/m/0c3m8g" - id: 453 - display_name: "Hand dryer" -} -item { - name: "/m/0c3mkw" - id: 454 - display_name: "Soap dispenser" -} -item { - name: "/m/0c568" - id: 455 - display_name: "Porcupine" -} -item { - name: "/m/0c9ph5" - id: 456 - display_name: "Flower" -} -item { - name: "/m/0ccs93" - id: 457 - display_name: "Canary" -} -item { - name: "/m/0cd4d" - id: 458 - display_name: "Cheetah" -} -item { - name: "/m/0cdl1" - id: 459 - display_name: "Palm tree" -} -item { - name: "/m/0cdn1" - id: 460 - display_name: "Hamburger" -} -item { - name: "/m/0cffdh" - id: 461 - display_name: "Maple" -} -item { - name: "/m/0cgh4" - id: 462 - display_name: "Building" -} -item { - name: "/m/0ch_cf" - id: 463 - display_name: "Fish" -} -item { - name: "/m/0cjq5" - id: 464 - display_name: "Lobster" -} -item { - name: "/m/0cjs7" - id: 465 - display_name: "Asparagus" -} -item { - name: "/m/0c_jw" - id: 466 - display_name: "Furniture" -} -item { - name: "/m/0cl4p" - id: 467 - display_name: "Hedgehog" -} -item { - name: "/m/0cmf2" - id: 468 - display_name: "Airplane" -} -item { - name: "/m/0cmx8" - id: 469 - display_name: "Spoon" -} -item { - name: "/m/0cn6p" - id: 470 - display_name: "Otter" -} -item { - name: "/m/0cnyhnx" - id: 471 - display_name: "Bull" -} -item { - name: "/m/0_cp5" - id: 472 - display_name: "Oyster" -} -item { - name: "/m/0cqn2" - id: 473 - display_name: "Horizontal bar" -} -item { - name: "/m/0crjs" - id: 474 - display_name: "Convenience store" -} -item { - name: "/m/0ct4f" - id: 475 - display_name: "Bomb" -} -item { - name: "/m/0cvnqh" - id: 476 - display_name: "Bench" -} -item { - name: "/m/0cxn2" - id: 477 - display_name: "Ice cream" -} -item { - name: "/m/0cydv" - id: 478 - display_name: "Caterpillar" -} -item { - name: "/m/0cyf8" - id: 479 - display_name: "Butterfly" -} -item { - name: "/m/0cyfs" - id: 480 - display_name: "Parachute" -} -item { - name: "/m/0cyhj_" - id: 481 - display_name: "Orange" -} -item { - name: "/m/0czz2" - id: 482 - display_name: "Antelope" -} -item { - name: "/m/0d20w4" - id: 483 - display_name: "Beaker" -} -item { - name: "/m/0d_2m" - id: 484 - display_name: "Moths and butterflies" -} -item { - name: "/m/0d4v4" - id: 485 - display_name: "Window" -} -item { - name: "/m/0d4w1" - id: 486 - display_name: "Closet" -} -item { - name: "/m/0d5gx" - id: 487 - display_name: "Castle" -} -item { - name: "/m/0d8zb" - id: 488 - display_name: "Jellyfish" -} -item { - name: "/m/0dbvp" - id: 489 - display_name: "Goose" -} -item { - name: "/m/0dbzx" - id: 490 - display_name: "Mule" -} -item { - name: "/m/0dftk" - id: 491 - display_name: "Swan" -} -item { - name: "/m/0dj6p" - id: 492 - display_name: "Peach" -} -item { - name: "/m/0djtd" - id: 493 - display_name: "Coconut" -} -item { - name: "/m/0dkzw" - id: 494 - display_name: "Seat belt" -} -item { - name: "/m/0dq75" - id: 495 - display_name: "Raccoon" -} -item { - name: "/m/0_dqb" - id: 496 - display_name: "Chisel" -} -item { - name: "/m/0dt3t" - id: 497 - display_name: "Fork" -} -item { - name: "/m/0dtln" - id: 498 - display_name: "Lamp" -} -item { - name: "/m/0dv5r" - id: 499 - display_name: "Camera" -} -item { - name: "/m/0dv77" - id: 500 - display_name: "Squash" -} -item { - name: "/m/0dv9c" - id: 501 - display_name: "Racket" -} -item { - name: "/m/0dzct" - id: 502 - display_name: "Human face" -} -item { - name: "/m/0dzf4" - id: 503 - display_name: "Human arm" -} -item { - name: "/m/0f4s2w" - id: 504 - display_name: "Vegetable" -} -item { - name: "/m/0f571" - id: 505 - display_name: "Diaper" -} -item { - name: "/m/0f6nr" - id: 506 - display_name: "Unicycle" -} -item { - name: "/m/0f6wt" - id: 507 - display_name: "Falcon" -} -item { - name: "/m/0f8s22" - id: 508 - display_name: "Chime" -} -item { - name: "/m/0f9_l" - id: 509 - display_name: "Snail" -} -item { - name: "/m/0fbdv" - id: 510 - display_name: "Shellfish" -} -item { - name: "/m/0fbw6" - id: 511 - display_name: "Cabbage" -} -item { - name: "/m/0fj52s" - id: 512 - display_name: "Carrot" -} -item { - name: "/m/0fldg" - id: 513 - display_name: "Mango" -} -item { - name: "/m/0fly7" - id: 514 - display_name: "Jeans" -} -item { - name: "/m/0fm3zh" - id: 515 - display_name: "Flowerpot" -} -item { - name: "/m/0fp6w" - id: 516 - display_name: "Pineapple" -} -item { - name: "/m/0fqfqc" - id: 517 - display_name: "Drawer" -} -item { - name: "/m/0fqt361" - id: 518 - display_name: "Stool" -} -item { - name: "/m/0frqm" - id: 519 - display_name: "Envelope" -} -item { - name: "/m/0fszt" - id: 520 - display_name: "Cake" -} -item { - name: "/m/0ft9s" - id: 521 - display_name: "Dragonfly" -} -item { - name: "/m/0ftb8" - id: 522 - display_name: "Sunflower" -} -item { - name: "/m/0fx9l" - id: 523 - display_name: "Microwave oven" -} -item { - name: "/m/0fz0h" - id: 524 - display_name: "Honeycomb" -} -item { - name: "/m/0gd2v" - id: 525 - display_name: "Marine mammal" -} -item { - name: "/m/0gd36" - id: 526 - display_name: "Sea lion" -} -item { - name: "/m/0gj37" - id: 527 - display_name: "Ladybug" -} -item { - name: "/m/0gjbg72" - id: 528 - display_name: "Shelf" -} -item { - name: "/m/0gjkl" - id: 529 - display_name: "Watch" -} -item { - name: "/m/0gm28" - id: 530 - display_name: "Candy" -} -item { - name: "/m/0grw1" - id: 531 - display_name: "Salad" -} -item { - name: "/m/0gv1x" - id: 532 - display_name: "Parrot" -} -item { - name: "/m/0gxl3" - id: 533 - display_name: "Handgun" -} -item { - name: "/m/0h23m" - id: 534 - display_name: "Sparrow" -} -item { - name: "/m/0h2r6" - id: 535 - display_name: "Van" -} -item { - name: "/m/0h8jyh6" - id: 536 - display_name: "Grinder" -} -item { - name: "/m/0h8kx63" - id: 537 - display_name: "Spice rack" -} -item { - name: "/m/0h8l4fh" - id: 538 - display_name: "Light bulb" -} -item { - name: "/m/0h8lkj8" - id: 539 - display_name: "Corded phone" -} -item { - name: "/m/0h8mhzd" - id: 540 - display_name: "Sports uniform" -} -item { - name: "/m/0h8my_4" - id: 541 - display_name: "Tennis racket" -} -item { - name: "/m/0h8mzrc" - id: 542 - display_name: "Wall clock" -} -item { - name: "/m/0h8n27j" - id: 543 - display_name: "Serving tray" -} -item { - name: "/m/0h8n5zk" - id: 544 - display_name: "Kitchen & dining room table" -} -item { - name: "/m/0h8n6f9" - id: 545 - display_name: "Dog bed" -} -item { - name: "/m/0h8n6ft" - id: 546 - display_name: "Cake stand" -} -item { - name: "/m/0h8nm9j" - id: 547 - display_name: "Cat furniture" -} -item { - name: "/m/0h8nr_l" - id: 548 - display_name: "Bathroom accessory" -} -item { - name: "/m/0h8nsvg" - id: 549 - display_name: "Facial tissue holder" -} -item { - name: "/m/0h8ntjv" - id: 550 - display_name: "Pressure cooker" -} -item { - name: "/m/0h99cwc" - id: 551 - display_name: "Kitchen appliance" -} -item { - name: "/m/0h9mv" - id: 552 - display_name: "Tire" -} -item { - name: "/m/0hdln" - id: 553 - display_name: "Ruler" -} -item { - name: "/m/0hf58v5" - id: 554 - display_name: "Luggage and bags" -} -item { - name: "/m/0hg7b" - id: 555 - display_name: "Microphone" -} -item { - name: "/m/0hkxq" - id: 556 - display_name: "Broccoli" -} -item { - name: "/m/0hnnb" - id: 557 - display_name: "Umbrella" -} -item { - name: "/m/0hnyx" - id: 558 - display_name: "Pastry" -} -item { - name: "/m/0hqkz" - id: 559 - display_name: "Grapefruit" -} -item { - name: "/m/0j496" - id: 560 - display_name: "Band-aid" -} -item { - name: "/m/0jbk" - id: 561 - display_name: "Animal" -} -item { - name: "/m/0jg57" - id: 562 - display_name: "Bell pepper" -} -item { - name: "/m/0jly1" - id: 563 - display_name: "Turkey" -} -item { - name: "/m/0jqgx" - id: 564 - display_name: "Lily" -} -item { - name: "/m/0jwn_" - id: 565 - display_name: "Pomegranate" -} -item { - name: "/m/0jy4k" - id: 566 - display_name: "Doughnut" -} -item { - name: "/m/0jyfg" - id: 567 - display_name: "Glasses" -} -item { - name: "/m/0k0pj" - id: 568 - display_name: "Human nose" -} -item { - name: "/m/0k1tl" - id: 569 - display_name: "Pen" -} -item { - name: "/m/0_k2" - id: 570 - display_name: "Ant" -} -item { - name: "/m/0k4j" - id: 571 - display_name: "Car" -} -item { - name: "/m/0k5j" - id: 572 - display_name: "Aircraft" -} -item { - name: "/m/0k65p" - id: 573 - display_name: "Human hand" -} -item { - name: "/m/0km7z" - id: 574 - display_name: "Skunk" -} -item { - name: "/m/0kmg4" - id: 575 - display_name: "Teddy bear" -} -item { - name: "/m/0kpqd" - id: 576 - display_name: "Watermelon" -} -item { - name: "/m/0kpt_" - id: 577 - display_name: "Cantaloupe" -} -item { - name: "/m/0ky7b" - id: 578 - display_name: "Dishwasher" -} -item { - name: "/m/0l14j_" - id: 579 - display_name: "Flute" -} -item { - name: "/m/0l3ms" - id: 580 - display_name: "Balance beam" -} -item { - name: "/m/0l515" - id: 581 - display_name: "Sandwich" -} -item { - name: "/m/0ll1f78" - id: 582 - display_name: "Shrimp" -} -item { - name: "/m/0llzx" - id: 583 - display_name: "Sewing machine" -} -item { - name: "/m/0lt4_" - id: 584 - display_name: "Binoculars" -} -item { - name: "/m/0m53l" - id: 585 - display_name: "Rays and skates" -} -item { - name: "/m/0mcx2" - id: 586 - display_name: "Ipod" -} -item { - name: "/m/0mkg" - id: 587 - display_name: "Accordion" -} -item { - name: "/m/0mw_6" - id: 588 - display_name: "Willow" -} -item { - name: "/m/0n28_" - id: 589 - display_name: "Crab" -} -item { - name: "/m/0nl46" - id: 590 - display_name: "Crown" -} -item { - name: "/m/0nybt" - id: 591 - display_name: "Seahorse" -} -item { - name: "/m/0p833" - id: 592 - display_name: "Perfume" -} -item { - name: "/m/0pcr" - id: 593 - display_name: "Alpaca" -} -item { - name: "/m/0pg52" - id: 594 - display_name: "Taxi" -} -item { - name: "/m/0ph39" - id: 595 - display_name: "Canoe" -} -item { - name: "/m/0qjjc" - id: 596 - display_name: "Remote control" -} -item { - name: "/m/0qmmr" - id: 597 - display_name: "Wheelchair" -} -item { - name: "/m/0wdt60w" - id: 598 - display_name: "Rugby ball" -} -item { - name: "/m/0xfy" - id: 599 - display_name: "Armadillo" -} -item { - name: "/m/0xzly" - id: 600 - display_name: "Maracas" -} -item { - name: "/m/0zvk5" - id: 601 - display_name: "Helmet" -} diff --git a/research/object_detection/data/pascal_label_map.pbtxt b/research/object_detection/data/pascal_label_map.pbtxt deleted file mode 100644 index c9e9e2affcd..00000000000 --- a/research/object_detection/data/pascal_label_map.pbtxt +++ /dev/null @@ -1,99 +0,0 @@ -item { - id: 1 - name: 'aeroplane' -} - -item { - id: 2 - name: 'bicycle' -} - -item { - id: 3 - name: 'bird' -} - -item { - id: 4 - name: 'boat' -} - -item { - id: 5 - name: 'bottle' -} - -item { - id: 6 - name: 'bus' -} - -item { - id: 7 - name: 'car' -} - -item { - id: 8 - name: 'cat' -} - -item { - id: 9 - name: 'chair' -} - -item { - id: 10 - name: 'cow' -} - -item { - id: 11 - name: 'diningtable' -} - -item { - id: 12 - name: 'dog' -} - -item { - id: 13 - name: 'horse' -} - -item { - id: 14 - name: 'motorbike' -} - -item { - id: 15 - name: 'person' -} - -item { - id: 16 - name: 'pottedplant' -} - -item { - id: 17 - name: 'sheep' -} - -item { - id: 18 - name: 'sofa' -} - -item { - id: 19 - name: 'train' -} - -item { - id: 20 - name: 'tvmonitor' -} diff --git a/research/object_detection/data/pet_label_map.pbtxt b/research/object_detection/data/pet_label_map.pbtxt deleted file mode 100644 index 54d7d351894..00000000000 --- a/research/object_detection/data/pet_label_map.pbtxt +++ /dev/null @@ -1,184 +0,0 @@ -item { - id: 1 - name: 'Abyssinian' -} - -item { - id: 2 - name: 'american_bulldog' -} - -item { - id: 3 - name: 'american_pit_bull_terrier' -} - -item { - id: 4 - name: 'basset_hound' -} - -item { - id: 5 - name: 'beagle' -} - -item { - id: 6 - name: 'Bengal' -} - -item { - id: 7 - name: 'Birman' -} - -item { - id: 8 - name: 'Bombay' -} - -item { - id: 9 - name: 'boxer' -} - -item { - id: 10 - name: 'British_Shorthair' -} - -item { - id: 11 - name: 'chihuahua' -} - -item { - id: 12 - name: 'Egyptian_Mau' -} - -item { - id: 13 - name: 'english_cocker_spaniel' -} - -item { - id: 14 - name: 'english_setter' -} - -item { - id: 15 - name: 'german_shorthaired' -} - -item { - id: 16 - name: 'great_pyrenees' -} - -item { - id: 17 - name: 'havanese' -} - -item { - id: 18 - name: 'japanese_chin' -} - -item { - id: 19 - name: 'keeshond' -} - -item { - id: 20 - name: 'leonberger' -} - -item { - id: 21 - name: 'Maine_Coon' -} - -item { - id: 22 - name: 'miniature_pinscher' -} - -item { - id: 23 - name: 'newfoundland' -} - -item { - id: 24 - name: 'Persian' -} - -item { - id: 25 - name: 'pomeranian' -} - -item { - id: 26 - name: 'pug' -} - -item { - id: 27 - name: 'Ragdoll' -} - -item { - id: 28 - name: 'Russian_Blue' -} - -item { - id: 29 - name: 'saint_bernard' -} - -item { - id: 30 - name: 'samoyed' -} - -item { - id: 31 - name: 'scottish_terrier' -} - -item { - id: 32 - name: 'shiba_inu' -} - -item { - id: 33 - name: 'Siamese' -} - -item { - id: 34 - name: 'Sphynx' -} - -item { - id: 35 - name: 'staffordshire_bull_terrier' -} - -item { - id: 36 - name: 'wheaten_terrier' -} - -item { - id: 37 - name: 'yorkshire_terrier' -} diff --git a/research/object_detection/data/snapshot_serengeti_label_map.pbtxt b/research/object_detection/data/snapshot_serengeti_label_map.pbtxt deleted file mode 100644 index 57555d179f9..00000000000 --- a/research/object_detection/data/snapshot_serengeti_label_map.pbtxt +++ /dev/null @@ -1,240 +0,0 @@ -item { - id: 1 - name: 'human' -} - -item { - id: 2 - name: 'gazelleGrants' -} - -item { - id: 3 - name: 'reedbuck' -} - -item { - id: 4 - name: 'dikDik' -} - -item { - id: 5 - name: 'zebra' -} - -item { - id: 6 - name: 'porcupine' -} - -item { - id: 7 - name: 'gazelleThomsons' -} - -item { - id: 8 - name: 'hyenaSpotted' -} - -item { - id: 9 - name: 'warthog' -} - -item { - id: 10 - name: 'impala' -} - -item { - id: 11 - name: 'elephant' -} - -item { - id: 12 - name: 'giraffe' -} - -item { - id: 13 - name: 'mongoose' -} - -item { - id: 14 - name: 'buffalo' -} - -item { - id: 15 - name: 'hartebeest' -} - -item { - id: 16 - name: 'guineaFowl' -} - -item { - id: 17 - name: 'wildebeest' -} - -item { - id: 18 - name: 'leopard' -} - -item { - id: 19 - name: 'ostrich' -} - -item { - id: 20 - name: 'lionFemale' -} - -item { - id: 21 - name: 'koriBustard' -} - -item { - id: 22 - name: 'otherBird' -} - -item { - id: 23 - name: 'batEaredFox' -} - -item { - id: 24 - name: 'bushbuck' -} - -item { - id: 25 - name: 'jackal' -} - -item { - id: 26 - name: 'cheetah' -} - -item { - id: 27 - name: 'eland' -} - -item { - id: 28 - name: 'aardwolf' -} - -item { - id: 29 - name: 'hippopotamus' -} - -item { - id: 30 - name: 'hyenaStriped' -} - -item { - id: 31 - name: 'aardvark' -} - -item { - id: 32 - name: 'hare' -} - -item { - id: 33 - name: 'baboon' -} - -item { - id: 34 - name: 'vervetMonkey' -} - -item { - id: 35 - name: 'waterbuck' -} - -item { - id: 36 - name: 'secretaryBird' -} - -item { - id: 37 - name: 'serval' -} - -item { - id: 38 - name: 'lionMale' -} - -item { - id: 39 - name: 'topi' -} - -item { - id: 40 - name: 'honeyBadger' -} - -item { - id: 41 - name: 'rodents' -} - -item { - id: 42 - name: 'wildcat' -} - -item { - id: 43 - name: 'civet' -} - -item { - id: 44 - name: 'genet' -} - -item { - id: 45 - name: 'caracal' -} - -item { - id: 46 - name: 'rhinoceros' -} - -item { - id: 47 - name: 'reptiles' -} - -item { - id: 48 - name: 'zorilla' -} - diff --git a/research/object_detection/data_decoders/__init__.py b/research/object_detection/data_decoders/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/data_decoders/tf_example_decoder.py b/research/object_detection/data_decoders/tf_example_decoder.py deleted file mode 100644 index 6e064ebf561..00000000000 --- a/research/object_detection/data_decoders/tf_example_decoder.py +++ /dev/null @@ -1,1099 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tensorflow Example proto decoder for object detection. - -A decoder to decode string tensors containing serialized tensorflow.Example -protos for object detection. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import enum -import functools -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf -from tf_slim import tfexample_decoder as slim_example_decoder -from object_detection.core import data_decoder -from object_detection.core import standard_fields as fields -from object_detection.protos import input_reader_pb2 -from object_detection.utils import label_map_util -from object_detection.utils import shape_utils - -# pylint: disable=g-import-not-at-top -try: - from tensorflow.contrib import lookup as contrib_lookup - -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - -_LABEL_OFFSET = 1 -# The field name of hosting keypoint text feature. Only used within this file -# to help forming the keypoint related features. -_KEYPOINT_TEXT_FIELD = 'image/object/keypoint/text' - - -class Visibility(enum.Enum): - """Visibility definitions. - - This follows the MS Coco convention (http://cocodataset.org/#format-data). - """ - # Keypoint is not labeled. - UNLABELED = 0 - # Keypoint is labeled but falls outside the object segment (e.g. occluded). - NOT_VISIBLE = 1 - # Keypoint is labeled and visible. - VISIBLE = 2 - - -class _ClassTensorHandler(slim_example_decoder.Tensor): - """An ItemHandler to fetch class ids from class text.""" - - def __init__(self, - tensor_key, - label_map_proto_file, - shape_keys=None, - shape=None, - default_value=''): - """Initializes the LookupTensor handler. - - Simply calls a vocabulary (most often, a label mapping) lookup. - - Args: - tensor_key: the name of the `TFExample` feature to read the tensor from. - label_map_proto_file: File path to a text format LabelMapProto message - mapping class text to id. - shape_keys: Optional name or list of names of the TF-Example feature in - which the tensor shape is stored. If a list, then each corresponds to - one dimension of the shape. - shape: Optional output shape of the `Tensor`. If provided, the `Tensor` is - reshaped accordingly. - default_value: The value used when the `tensor_key` is not found in a - particular `TFExample`. - - Raises: - ValueError: if both `shape_keys` and `shape` are specified. - """ - name_to_id = label_map_util.get_label_map_dict( - label_map_proto_file, use_display_name=False) - # We use a default_value of -1, but we expect all labels to be contained - # in the label map. - try: - # Dynamically try to load the tf v2 lookup, falling back to contrib - lookup = tf.compat.v2.lookup - hash_table_class = tf.compat.v2.lookup.StaticHashTable - except AttributeError: - lookup = contrib_lookup - hash_table_class = contrib_lookup.HashTable - name_to_id_table = hash_table_class( - initializer=lookup.KeyValueTensorInitializer( - keys=tf.constant(list(name_to_id.keys())), - values=tf.constant(list(name_to_id.values()), dtype=tf.int64)), - default_value=-1) - display_name_to_id = label_map_util.get_label_map_dict( - label_map_proto_file, use_display_name=True) - # We use a default_value of -1, but we expect all labels to be contained - # in the label map. - display_name_to_id_table = hash_table_class( - initializer=lookup.KeyValueTensorInitializer( - keys=tf.constant(list(display_name_to_id.keys())), - values=tf.constant( - list(display_name_to_id.values()), dtype=tf.int64)), - default_value=-1) - - self._name_to_id_table = name_to_id_table - self._display_name_to_id_table = display_name_to_id_table - super(_ClassTensorHandler, self).__init__(tensor_key, shape_keys, shape, - default_value) - - def tensors_to_item(self, keys_to_tensors): - unmapped_tensor = super(_ClassTensorHandler, - self).tensors_to_item(keys_to_tensors) - return tf.maximum(self._name_to_id_table.lookup(unmapped_tensor), - self._display_name_to_id_table.lookup(unmapped_tensor)) - - -class TfExampleDecoder(data_decoder.DataDecoder): - """Tensorflow Example proto decoder.""" - - def __init__(self, - load_instance_masks=False, - instance_mask_type=input_reader_pb2.NUMERICAL_MASKS, - label_map_proto_file=None, - use_display_name=False, - dct_method='', - num_keypoints=0, - num_additional_channels=0, - load_multiclass_scores=False, - load_context_features=False, - expand_hierarchy_labels=False, - load_dense_pose=False, - load_track_id=False, - load_keypoint_depth_features=False, - use_keypoint_label_map=False): - """Constructor sets keys_to_features and items_to_handlers. - - Args: - load_instance_masks: whether or not to load and handle instance masks. - instance_mask_type: type of instance masks. Options are provided in - input_reader.proto. This is only used if `load_instance_masks` is True. - label_map_proto_file: a file path to a - object_detection.protos.StringIntLabelMap proto. If provided, then the - mapped IDs of 'image/object/class/text' will take precedence over the - existing 'image/object/class/label' ID. Also, if provided, it is - assumed that 'image/object/class/text' will be in the data. - use_display_name: whether or not to use the `display_name` for label - mapping (instead of `name`). Only used if label_map_proto_file is - provided. - dct_method: An optional string. Defaults to None. It only takes - effect when image format is jpeg, used to specify a hint about the - algorithm used for jpeg decompression. Currently valid values - are ['INTEGER_FAST', 'INTEGER_ACCURATE']. The hint may be ignored, for - example, the jpeg library does not have that specific option. - num_keypoints: the number of keypoints per object. - num_additional_channels: how many additional channels to use. - load_multiclass_scores: Whether to load multiclass scores associated with - boxes. - load_context_features: Whether to load information from context_features, - to provide additional context to a detection model for training and/or - inference. - expand_hierarchy_labels: Expands the object and image labels taking into - account the provided hierarchy in the label_map_proto_file. For positive - classes, the labels are extended to ancestor. For negative classes, - the labels are expanded to descendants. - load_dense_pose: Whether to load DensePose annotations. - load_track_id: Whether to load tracking annotations. - load_keypoint_depth_features: Whether to load the keypoint depth features - including keypoint relative depths and weights. If this field is set to - True but no keypoint depth features are in the input tf.Example, then - default values will be populated. - use_keypoint_label_map: If set to True, the 'image/object/keypoint/text' - field will be used to map the keypoint coordinates (using the label - map defined in label_map_proto_file) instead of assuming the ordering - in the tf.Example feature. This is useful when training with multiple - datasets while each of them contains different subset of keypoint - annotations. - - Raises: - ValueError: If `instance_mask_type` option is not one of - input_reader_pb2.DEFAULT, input_reader_pb2.NUMERICAL, or - input_reader_pb2.PNG_MASKS. - ValueError: If `expand_labels_hierarchy` is True, but the - `label_map_proto_file` is not provided. - """ - - # TODO(rathodv): delete unused `use_display_name` argument once we change - # other decoders to handle label maps similarly. - del use_display_name - self.keys_to_features = { - 'image/encoded': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/format': - tf.FixedLenFeature((), tf.string, default_value='jpeg'), - 'image/filename': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/key/sha256': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/source_id': - tf.FixedLenFeature((), tf.string, default_value=''), - 'image/height': - tf.FixedLenFeature((), tf.int64, default_value=1), - 'image/width': - tf.FixedLenFeature((), tf.int64, default_value=1), - # Image-level labels. - 'image/class/text': - tf.VarLenFeature(tf.string), - 'image/class/label': - tf.VarLenFeature(tf.int64), - 'image/neg_category_ids': - tf.VarLenFeature(tf.int64), - 'image/not_exhaustive_category_ids': - tf.VarLenFeature(tf.int64), - 'image/class/confidence': - tf.VarLenFeature(tf.float32), - # Object boxes and classes. - 'image/object/bbox/xmin': - tf.VarLenFeature(tf.float32), - 'image/object/bbox/xmax': - tf.VarLenFeature(tf.float32), - 'image/object/bbox/ymin': - tf.VarLenFeature(tf.float32), - 'image/object/bbox/ymax': - tf.VarLenFeature(tf.float32), - 'image/object/class/label': - tf.VarLenFeature(tf.int64), - 'image/object/class/text': - tf.VarLenFeature(tf.string), - 'image/object/area': - tf.VarLenFeature(tf.float32), - 'image/object/is_crowd': - tf.VarLenFeature(tf.int64), - 'image/object/difficult': - tf.VarLenFeature(tf.int64), - 'image/object/group_of': - tf.VarLenFeature(tf.int64), - 'image/object/weight': - tf.VarLenFeature(tf.float32), - - } - # We are checking `dct_method` instead of passing it directly in order to - # ensure TF version 1.6 compatibility. - if dct_method: - image = slim_example_decoder.Image( - image_key='image/encoded', - format_key='image/format', - channels=3, - dct_method=dct_method) - additional_channel_image = slim_example_decoder.Image( - image_key='image/additional_channels/encoded', - format_key='image/format', - channels=1, - repeated=True, - dct_method=dct_method) - else: - image = slim_example_decoder.Image( - image_key='image/encoded', format_key='image/format', channels=3) - additional_channel_image = slim_example_decoder.Image( - image_key='image/additional_channels/encoded', - format_key='image/format', - channels=1, - repeated=True) - self.items_to_handlers = { - fields.InputDataFields.image: - image, - fields.InputDataFields.source_id: ( - slim_example_decoder.Tensor('image/source_id')), - fields.InputDataFields.key: ( - slim_example_decoder.Tensor('image/key/sha256')), - fields.InputDataFields.filename: ( - slim_example_decoder.Tensor('image/filename')), - # Image-level labels. - fields.InputDataFields.groundtruth_image_confidences: ( - slim_example_decoder.Tensor('image/class/confidence')), - fields.InputDataFields.groundtruth_verified_neg_classes: ( - slim_example_decoder.Tensor('image/neg_category_ids')), - fields.InputDataFields.groundtruth_not_exhaustive_classes: ( - slim_example_decoder.Tensor('image/not_exhaustive_category_ids')), - # Object boxes and classes. - fields.InputDataFields.groundtruth_boxes: ( - slim_example_decoder.BoundingBox(['ymin', 'xmin', 'ymax', 'xmax'], - 'image/object/bbox/')), - fields.InputDataFields.groundtruth_area: - slim_example_decoder.Tensor('image/object/area'), - fields.InputDataFields.groundtruth_is_crowd: ( - slim_example_decoder.Tensor('image/object/is_crowd')), - fields.InputDataFields.groundtruth_difficult: ( - slim_example_decoder.Tensor('image/object/difficult')), - fields.InputDataFields.groundtruth_group_of: ( - slim_example_decoder.Tensor('image/object/group_of')), - fields.InputDataFields.groundtruth_weights: ( - slim_example_decoder.Tensor('image/object/weight')), - - } - - self._keypoint_label_map = None - if use_keypoint_label_map: - assert label_map_proto_file is not None - self._keypoint_label_map = label_map_util.get_keypoint_label_map_dict( - label_map_proto_file) - # We use a default_value of -1, but we expect all labels to be - # contained in the label map. - try: - # Dynamically try to load the tf v2 lookup, falling back to contrib - lookup = tf.compat.v2.lookup - hash_table_class = tf.compat.v2.lookup.StaticHashTable - except AttributeError: - lookup = contrib_lookup - hash_table_class = contrib_lookup.HashTable - self._kpts_name_to_id_table = hash_table_class( - initializer=lookup.KeyValueTensorInitializer( - keys=tf.constant(list(self._keypoint_label_map.keys())), - values=tf.constant( - list(self._keypoint_label_map.values()), dtype=tf.int64)), - default_value=-1) - - self.keys_to_features[_KEYPOINT_TEXT_FIELD] = tf.VarLenFeature( - tf.string) - self.items_to_handlers[_KEYPOINT_TEXT_FIELD] = ( - slim_example_decoder.ItemHandlerCallback( - [_KEYPOINT_TEXT_FIELD], self._keypoint_text_handle)) - - if load_multiclass_scores: - self.keys_to_features[ - 'image/object/class/multiclass_scores'] = tf.VarLenFeature(tf.float32) - self.items_to_handlers[fields.InputDataFields.multiclass_scores] = ( - slim_example_decoder.Tensor('image/object/class/multiclass_scores')) - - if load_context_features: - self.keys_to_features[ - 'image/context_features'] = tf.VarLenFeature(tf.float32) - self.items_to_handlers[fields.InputDataFields.context_features] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/context_features', 'image/context_feature_length'], - self._reshape_context_features)) - - self.keys_to_features[ - 'image/context_feature_length'] = tf.FixedLenFeature((), tf.int64) - self.items_to_handlers[fields.InputDataFields.context_feature_length] = ( - slim_example_decoder.Tensor('image/context_feature_length')) - - if num_additional_channels > 0: - self.keys_to_features[ - 'image/additional_channels/encoded'] = tf.FixedLenFeature( - (num_additional_channels,), tf.string) - self.items_to_handlers[ - fields.InputDataFields. - image_additional_channels] = additional_channel_image - self._num_keypoints = num_keypoints - if num_keypoints > 0: - self.keys_to_features['image/object/keypoint/x'] = ( - tf.VarLenFeature(tf.float32)) - self.keys_to_features['image/object/keypoint/y'] = ( - tf.VarLenFeature(tf.float32)) - self.keys_to_features['image/object/keypoint/visibility'] = ( - tf.VarLenFeature(tf.int64)) - self.items_to_handlers[fields.InputDataFields.groundtruth_keypoints] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/keypoint/y', 'image/object/keypoint/x'], - self._reshape_keypoints)) - kpt_vis_field = fields.InputDataFields.groundtruth_keypoint_visibilities - self.items_to_handlers[kpt_vis_field] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/keypoint/x', 'image/object/keypoint/visibility'], - self._reshape_keypoint_visibilities)) - if load_keypoint_depth_features: - self.keys_to_features['image/object/keypoint/z'] = ( - tf.VarLenFeature(tf.float32)) - self.keys_to_features['image/object/keypoint/z/weights'] = ( - tf.VarLenFeature(tf.float32)) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_keypoint_depths] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/keypoint/x', 'image/object/keypoint/z'], - self._reshape_keypoint_depths)) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_keypoint_depth_weights] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/keypoint/x', - 'image/object/keypoint/z/weights'], - self._reshape_keypoint_depth_weights)) - - if load_instance_masks: - if instance_mask_type in (input_reader_pb2.DEFAULT, - input_reader_pb2.NUMERICAL_MASKS): - self.keys_to_features['image/object/mask'] = ( - tf.VarLenFeature(tf.float32)) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_instance_masks] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/mask', 'image/height', 'image/width'], - self._reshape_instance_masks)) - elif instance_mask_type == input_reader_pb2.PNG_MASKS: - self.keys_to_features['image/object/mask'] = tf.VarLenFeature(tf.string) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_instance_masks] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/mask', 'image/height', 'image/width'], - self._decode_png_instance_masks)) - else: - raise ValueError('Did not recognize the `instance_mask_type` option.') - self.keys_to_features['image/object/mask/weight'] = ( - tf.VarLenFeature(tf.float32)) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_instance_mask_weights] = ( - slim_example_decoder.Tensor('image/object/mask/weight')) - if load_dense_pose: - self.keys_to_features['image/object/densepose/num'] = ( - tf.VarLenFeature(tf.int64)) - self.keys_to_features['image/object/densepose/part_index'] = ( - tf.VarLenFeature(tf.int64)) - self.keys_to_features['image/object/densepose/x'] = ( - tf.VarLenFeature(tf.float32)) - self.keys_to_features['image/object/densepose/y'] = ( - tf.VarLenFeature(tf.float32)) - self.keys_to_features['image/object/densepose/u'] = ( - tf.VarLenFeature(tf.float32)) - self.keys_to_features['image/object/densepose/v'] = ( - tf.VarLenFeature(tf.float32)) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_dp_num_points] = ( - slim_example_decoder.Tensor('image/object/densepose/num')) - self.items_to_handlers[fields.InputDataFields.groundtruth_dp_part_ids] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/densepose/part_index', - 'image/object/densepose/num'], self._dense_pose_part_indices)) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_dp_surface_coords] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/object/densepose/x', 'image/object/densepose/y', - 'image/object/densepose/u', 'image/object/densepose/v', - 'image/object/densepose/num'], - self._dense_pose_surface_coordinates)) - if load_track_id: - self.keys_to_features['image/object/track/label'] = ( - tf.VarLenFeature(tf.int64)) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_track_ids] = ( - slim_example_decoder.Tensor('image/object/track/label')) - - if label_map_proto_file: - # If the label_map_proto is provided, try to use it in conjunction with - # the class text, and fall back to a materialized ID. - label_handler = slim_example_decoder.BackupHandler( - _ClassTensorHandler( - 'image/object/class/text', label_map_proto_file, - default_value=''), - slim_example_decoder.Tensor('image/object/class/label')) - image_label_handler = slim_example_decoder.BackupHandler( - _ClassTensorHandler( - fields.TfExampleFields.image_class_text, - label_map_proto_file, - default_value=''), - slim_example_decoder.Tensor(fields.TfExampleFields.image_class_label)) - else: - label_handler = slim_example_decoder.Tensor('image/object/class/label') - image_label_handler = slim_example_decoder.Tensor( - fields.TfExampleFields.image_class_label) - self.items_to_handlers[ - fields.InputDataFields.groundtruth_classes] = label_handler - self.items_to_handlers[ - fields.InputDataFields.groundtruth_image_classes] = image_label_handler - - self._expand_hierarchy_labels = expand_hierarchy_labels - self._ancestors_lut = None - self._descendants_lut = None - if expand_hierarchy_labels: - if label_map_proto_file: - ancestors_lut, descendants_lut = ( - label_map_util.get_label_map_hierarchy_lut(label_map_proto_file, - True)) - self._ancestors_lut = tf.constant(ancestors_lut, dtype=tf.int64) - self._descendants_lut = tf.constant(descendants_lut, dtype=tf.int64) - else: - raise ValueError('In order to expand labels, the label_map_proto_file ' - 'has to be provided.') - - def decode(self, tf_example_string_tensor): - """Decodes serialized tensorflow example and returns a tensor dictionary. - - Args: - tf_example_string_tensor: a string tensor holding a serialized tensorflow - example proto. - - Returns: - A dictionary of the following tensors. - fields.InputDataFields.image - 3D uint8 tensor of shape [None, None, 3] - containing image. - fields.InputDataFields.original_image_spatial_shape - 1D int32 tensor of - shape [2] containing shape of the image. - fields.InputDataFields.source_id - string tensor containing original - image id. - fields.InputDataFields.key - string tensor with unique sha256 hash key. - fields.InputDataFields.filename - string tensor with original dataset - filename. - fields.InputDataFields.groundtruth_boxes - 2D float32 tensor of shape - [None, 4] containing box corners. - fields.InputDataFields.groundtruth_classes - 1D int64 tensor of shape - [None] containing classes for the boxes. - fields.InputDataFields.groundtruth_weights - 1D float32 tensor of - shape [None] indicating the weights of groundtruth boxes. - fields.InputDataFields.groundtruth_area - 1D float32 tensor of shape - [None] containing containing object mask area in pixel squared. - fields.InputDataFields.groundtruth_is_crowd - 1D bool tensor of shape - [None] indicating if the boxes enclose a crowd. - - Optional: - fields.InputDataFields.groundtruth_image_confidences - 1D float tensor of - shape [None] indicating if a class is present in the image (1.0) or - a class is not present in the image (0.0). - fields.InputDataFields.image_additional_channels - 3D uint8 tensor of - shape [None, None, num_additional_channels]. 1st dim is height; 2nd dim - is width; 3rd dim is the number of additional channels. - fields.InputDataFields.groundtruth_difficult - 1D bool tensor of shape - [None] indicating if the boxes represent `difficult` instances. - fields.InputDataFields.groundtruth_group_of - 1D bool tensor of shape - [None] indicating if the boxes represent `group_of` instances. - fields.InputDataFields.groundtruth_keypoints - 3D float32 tensor of - shape [None, num_keypoints, 2] containing keypoints, where the - coordinates of the keypoints are ordered (y, x). - fields.InputDataFields.groundtruth_keypoint_visibilities - 2D bool - tensor of shape [None, num_keypoints] containing keypoint visibilites. - fields.InputDataFields.groundtruth_instance_masks - 3D float32 tensor of - shape [None, None, None] containing instance masks. - fields.InputDataFields.groundtruth_instance_mask_weights - 1D float32 - tensor of shape [None] containing weights. These are typically values - in {0.0, 1.0} which indicate whether to consider the mask related to an - object. - fields.InputDataFields.groundtruth_image_classes - 1D int64 of shape - [None] containing classes for the boxes. - fields.InputDataFields.multiclass_scores - 1D float32 tensor of shape - [None * num_classes] containing flattened multiclass scores for - groundtruth boxes. - fields.InputDataFields.context_features - 1D float32 tensor of shape - [context_feature_length * num_context_features] - fields.InputDataFields.context_feature_length - int32 tensor specifying - the length of each feature in context_features - """ - serialized_example = tf.reshape(tf_example_string_tensor, shape=[]) - decoder = slim_example_decoder.TFExampleDecoder(self.keys_to_features, - self.items_to_handlers) - keys = decoder.list_items() - tensors = decoder.decode(serialized_example, items=keys) - tensor_dict = dict(zip(keys, tensors)) - is_crowd = fields.InputDataFields.groundtruth_is_crowd - tensor_dict[is_crowd] = tf.cast(tensor_dict[is_crowd], dtype=tf.bool) - tensor_dict[fields.InputDataFields.image].set_shape([None, None, 3]) - tensor_dict[fields.InputDataFields.original_image_spatial_shape] = tf.shape( - tensor_dict[fields.InputDataFields.image])[:2] - - if fields.InputDataFields.image_additional_channels in tensor_dict: - channels = tensor_dict[fields.InputDataFields.image_additional_channels] - channels = tf.squeeze(channels, axis=3) - channels = tf.transpose(channels, perm=[1, 2, 0]) - tensor_dict[fields.InputDataFields.image_additional_channels] = channels - - def default_groundtruth_weights(): - return tf.ones( - [tf.shape(tensor_dict[fields.InputDataFields.groundtruth_boxes])[0]], - dtype=tf.float32) - - tensor_dict[fields.InputDataFields.groundtruth_weights] = tf.cond( - tf.greater( - tf.shape( - tensor_dict[fields.InputDataFields.groundtruth_weights])[0], - 0), lambda: tensor_dict[fields.InputDataFields.groundtruth_weights], - default_groundtruth_weights) - - if fields.InputDataFields.groundtruth_instance_masks in tensor_dict: - gt_instance_masks = tensor_dict[ - fields.InputDataFields.groundtruth_instance_masks] - num_gt_instance_masks = tf.shape(gt_instance_masks)[0] - gt_instance_mask_weights = tensor_dict[ - fields.InputDataFields.groundtruth_instance_mask_weights] - num_gt_instance_mask_weights = tf.shape(gt_instance_mask_weights)[0] - def default_groundtruth_instance_mask_weights(): - return tf.ones([num_gt_instance_masks], dtype=tf.float32) - - tensor_dict[fields.InputDataFields.groundtruth_instance_mask_weights] = ( - tf.cond(tf.greater(num_gt_instance_mask_weights, 0), - lambda: gt_instance_mask_weights, - default_groundtruth_instance_mask_weights)) - - if fields.InputDataFields.groundtruth_keypoints in tensor_dict: - gt_kpt_fld = fields.InputDataFields.groundtruth_keypoints - gt_kpt_vis_fld = fields.InputDataFields.groundtruth_keypoint_visibilities - - if self._keypoint_label_map is None: - # Set all keypoints that are not labeled to NaN. - tensor_dict[gt_kpt_fld] = tf.reshape(tensor_dict[gt_kpt_fld], - [-1, self._num_keypoints, 2]) - tensor_dict[gt_kpt_vis_fld] = tf.reshape( - tensor_dict[gt_kpt_vis_fld], [-1, self._num_keypoints]) - visibilities_tiled = tf.tile( - tf.expand_dims(tensor_dict[gt_kpt_vis_fld], axis=-1), [1, 1, 2]) - tensor_dict[gt_kpt_fld] = tf.where( - visibilities_tiled, tensor_dict[gt_kpt_fld], - np.nan * tf.ones_like(tensor_dict[gt_kpt_fld])) - else: - num_instances = tf.shape(tensor_dict['groundtruth_classes'])[0] - def true_fn(num_instances): - """Logics to process the tensor when num_instances is not zero.""" - kpts_idx = tf.cast(self._kpts_name_to_id_table.lookup( - tensor_dict[_KEYPOINT_TEXT_FIELD]), dtype=tf.int32) - num_kpt_texts = tf.cast( - tf.size(tensor_dict[_KEYPOINT_TEXT_FIELD]) / num_instances, - dtype=tf.int32) - # Prepare the index of the instances: [num_instances, num_kpt_texts]. - instance_idx = tf.tile( - tf.expand_dims(tf.range(num_instances, dtype=tf.int32), axis=-1), - [1, num_kpt_texts]) - # Prepare the index of the keypoints to scatter the keypoint - # coordinates: [num_kpts_texts * num_instances, 2]. - full_kpt_idx = tf.concat([ - tf.reshape( - instance_idx, shape=[num_kpt_texts * num_instances, 1]), - tf.expand_dims(kpts_idx, axis=-1) - ], axis=1) - - # Get the mask and gather only the keypoints with non-negative - # indices (i.e. the keypoint labels in the image/object/keypoint/text - # but do not exist in the label map). - valid_mask = tf.greater_equal(kpts_idx, 0) - full_kpt_idx = tf.boolean_mask(full_kpt_idx, valid_mask) - - gt_kpt = tf.scatter_nd( - full_kpt_idx, - tf.boolean_mask(tensor_dict[gt_kpt_fld], valid_mask), - shape=[num_instances, self._num_keypoints, 2]) - gt_kpt_vis = tf.cast(tf.scatter_nd( - full_kpt_idx, - tf.boolean_mask(tensor_dict[gt_kpt_vis_fld], valid_mask), - shape=[num_instances, self._num_keypoints]), dtype=tf.bool) - visibilities_tiled = tf.tile( - tf.expand_dims(gt_kpt_vis, axis=-1), [1, 1, 2]) - gt_kpt = tf.where(visibilities_tiled, gt_kpt, - np.nan * tf.ones_like(gt_kpt)) - return (gt_kpt, gt_kpt_vis) - - def false_fn(): - """Logics to process the tensor when num_instances is zero.""" - return (tf.zeros([0, self._num_keypoints, 2], dtype=tf.float32), - tf.zeros([0, self._num_keypoints], dtype=tf.bool)) - - true_fn = functools.partial(true_fn, num_instances) - results = tf.cond(num_instances > 0, true_fn, false_fn) - - tensor_dict[gt_kpt_fld] = results[0] - tensor_dict[gt_kpt_vis_fld] = results[1] - # Since the keypoint text tensor won't be used anymore, deleting it from - # the tensor_dict to avoid further code changes to handle it in the - # inputs.py file. - del tensor_dict[_KEYPOINT_TEXT_FIELD] - - if self._expand_hierarchy_labels: - input_fields = fields.InputDataFields - image_classes, image_confidences = self._expand_image_label_hierarchy( - tensor_dict[input_fields.groundtruth_image_classes], - tensor_dict[input_fields.groundtruth_image_confidences]) - tensor_dict[input_fields.groundtruth_image_classes] = image_classes - tensor_dict[input_fields.groundtruth_image_confidences] = ( - image_confidences) - - box_fields = [ - fields.InputDataFields.groundtruth_group_of, - fields.InputDataFields.groundtruth_is_crowd, - fields.InputDataFields.groundtruth_difficult, - fields.InputDataFields.groundtruth_area, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_weights, - ] - - def expand_field(field_name): - return self._expansion_box_field_labels( - tensor_dict[input_fields.groundtruth_classes], - tensor_dict[field_name]) - - # pylint: disable=cell-var-from-loop - for field in box_fields: - if field in tensor_dict: - tensor_dict[field] = tf.cond( - tf.size(tensor_dict[field]) > 0, lambda: expand_field(field), - lambda: tensor_dict[field]) - # pylint: enable=cell-var-from-loop - - tensor_dict[input_fields.groundtruth_classes] = ( - self._expansion_box_field_labels( - tensor_dict[input_fields.groundtruth_classes], - tensor_dict[input_fields.groundtruth_classes], True)) - - if fields.InputDataFields.groundtruth_group_of in tensor_dict: - group_of = fields.InputDataFields.groundtruth_group_of - tensor_dict[group_of] = tf.cast(tensor_dict[group_of], dtype=tf.bool) - - if fields.InputDataFields.groundtruth_dp_num_points in tensor_dict: - tensor_dict[fields.InputDataFields.groundtruth_dp_num_points] = tf.cast( - tensor_dict[fields.InputDataFields.groundtruth_dp_num_points], - dtype=tf.int32) - tensor_dict[fields.InputDataFields.groundtruth_dp_part_ids] = tf.cast( - tensor_dict[fields.InputDataFields.groundtruth_dp_part_ids], - dtype=tf.int32) - - if fields.InputDataFields.groundtruth_track_ids in tensor_dict: - tensor_dict[fields.InputDataFields.groundtruth_track_ids] = tf.cast( - tensor_dict[fields.InputDataFields.groundtruth_track_ids], - dtype=tf.int32) - - return tensor_dict - - def _keypoint_text_handle(self, keys_to_tensors): - """Reshapes keypoint text feature.""" - y = keys_to_tensors[_KEYPOINT_TEXT_FIELD] - if isinstance(y, tf.SparseTensor): - y = tf.sparse_tensor_to_dense(y) - return y - - def _reshape_keypoints(self, keys_to_tensors): - """Reshape keypoints. - - The keypoints are reshaped to [num_instances, num_keypoints, 2]. - - Args: - keys_to_tensors: a dictionary from keys to tensors. Expected keys are: - 'image/object/keypoint/x' - 'image/object/keypoint/y' - - Returns: - A 2-D float tensor of shape [num_instances * num_keypoints, 2] with values - in [0, 1]. - """ - y = keys_to_tensors['image/object/keypoint/y'] - if isinstance(y, tf.SparseTensor): - y = tf.sparse_tensor_to_dense(y) - y = tf.expand_dims(y, 1) - x = keys_to_tensors['image/object/keypoint/x'] - if isinstance(x, tf.SparseTensor): - x = tf.sparse_tensor_to_dense(x) - x = tf.expand_dims(x, 1) - keypoints = tf.concat([y, x], 1) - return keypoints - - def _reshape_keypoint_depths(self, keys_to_tensors): - """Reshape keypoint depths. - - The keypoint depths are reshaped to [num_instances, num_keypoints]. The - keypoint depth tensor is expected to have the same shape as the keypoint x - (or y) tensors. If not (usually because the example does not have the depth - groundtruth), then default depth values (zero) are provided. - - Args: - keys_to_tensors: a dictionary from keys to tensors. Expected keys are: - 'image/object/keypoint/x' - 'image/object/keypoint/z' - - Returns: - A 2-D float tensor of shape [num_instances, num_keypoints] with values - representing the keypoint depths. - """ - x = keys_to_tensors['image/object/keypoint/x'] - z = keys_to_tensors['image/object/keypoint/z'] - if isinstance(z, tf.SparseTensor): - z = tf.sparse_tensor_to_dense(z) - if isinstance(x, tf.SparseTensor): - x = tf.sparse_tensor_to_dense(x) - - default_z = tf.zeros_like(x) - # Use keypoint depth groundtruth if provided, otherwise use the default - # depth value. - z = tf.cond(tf.equal(tf.size(x), tf.size(z)), - true_fn=lambda: z, - false_fn=lambda: default_z) - z = tf.reshape(z, [-1, self._num_keypoints]) - return z - - def _reshape_keypoint_depth_weights(self, keys_to_tensors): - """Reshape keypoint depth weights. - - The keypoint depth weights are reshaped to [num_instances, num_keypoints]. - The keypoint depth weights tensor is expected to have the same shape as the - keypoint x (or y) tensors. If not (usually because the example does not have - the depth weights groundtruth), then default weight values (zero) are - provided. - - Args: - keys_to_tensors: a dictionary from keys to tensors. Expected keys are: - 'image/object/keypoint/x' - 'image/object/keypoint/z/weights' - - Returns: - A 2-D float tensor of shape [num_instances, num_keypoints] with values - representing the keypoint depth weights. - """ - x = keys_to_tensors['image/object/keypoint/x'] - z = keys_to_tensors['image/object/keypoint/z/weights'] - if isinstance(z, tf.SparseTensor): - z = tf.sparse_tensor_to_dense(z) - if isinstance(x, tf.SparseTensor): - x = tf.sparse_tensor_to_dense(x) - - default_z = tf.zeros_like(x) - # Use keypoint depth weights if provided, otherwise use the default - # values. - z = tf.cond(tf.equal(tf.size(x), tf.size(z)), - true_fn=lambda: z, - false_fn=lambda: default_z) - z = tf.reshape(z, [-1, self._num_keypoints]) - return z - - def _reshape_keypoint_visibilities(self, keys_to_tensors): - """Reshape keypoint visibilities. - - The keypoint visibilities are reshaped to [num_instances, - num_keypoints]. - - The raw keypoint visibilities are expected to conform to the - MSCoco definition. See Visibility enum. - - The returned boolean is True for the labeled case (either - Visibility.NOT_VISIBLE or Visibility.VISIBLE). These are the same categories - that COCO uses to evaluate keypoint detection performance: - http://cocodataset.org/#keypoints-eval - - If image/object/keypoint/visibility is not provided, visibilities will be - set to True for finite keypoint coordinate values, and 0 if the coordinates - are NaN. - - Args: - keys_to_tensors: a dictionary from keys to tensors. Expected keys are: - 'image/object/keypoint/x' - 'image/object/keypoint/visibility' - - Returns: - A 1-D bool tensor of shape [num_instances * num_keypoints] with values - in {0, 1}. 1 if the keypoint is labeled, 0 otherwise. - """ - x = keys_to_tensors['image/object/keypoint/x'] - vis = keys_to_tensors['image/object/keypoint/visibility'] - if isinstance(vis, tf.SparseTensor): - vis = tf.sparse_tensor_to_dense(vis) - if isinstance(x, tf.SparseTensor): - x = tf.sparse_tensor_to_dense(x) - - default_vis = tf.where( - tf.math.is_nan(x), - Visibility.UNLABELED.value * tf.ones_like(x, dtype=tf.int64), - Visibility.VISIBLE.value * tf.ones_like(x, dtype=tf.int64)) - # Use visibility if provided, otherwise use the default visibility. - vis = tf.cond(tf.equal(tf.size(x), tf.size(vis)), - true_fn=lambda: vis, - false_fn=lambda: default_vis) - vis = tf.math.logical_or( - tf.math.equal(vis, Visibility.NOT_VISIBLE.value), - tf.math.equal(vis, Visibility.VISIBLE.value)) - return vis - - def _reshape_instance_masks(self, keys_to_tensors): - """Reshape instance segmentation masks. - - The instance segmentation masks are reshaped to [num_instances, height, - width]. - - Args: - keys_to_tensors: a dictionary from keys to tensors. - - Returns: - A 3-D float tensor of shape [num_instances, height, width] with values - in {0, 1}. - """ - height = keys_to_tensors['image/height'] - width = keys_to_tensors['image/width'] - to_shape = tf.cast(tf.stack([-1, height, width]), tf.int32) - masks = keys_to_tensors['image/object/mask'] - if isinstance(masks, tf.SparseTensor): - masks = tf.sparse_tensor_to_dense(masks) - masks = tf.reshape( - tf.cast(tf.greater(masks, 0.0), dtype=tf.float32), to_shape) - return tf.cast(masks, tf.float32) - - def _reshape_context_features(self, keys_to_tensors): - """Reshape context features. - - The instance context_features are reshaped to - [num_context_features, context_feature_length] - - Args: - keys_to_tensors: a dictionary from keys to tensors. - - Returns: - A 2-D float tensor of shape [num_context_features, context_feature_length] - """ - context_feature_length = keys_to_tensors['image/context_feature_length'] - to_shape = tf.cast(tf.stack([-1, context_feature_length]), tf.int32) - context_features = keys_to_tensors['image/context_features'] - if isinstance(context_features, tf.SparseTensor): - context_features = tf.sparse_tensor_to_dense(context_features) - context_features = tf.reshape(context_features, to_shape) - return context_features - - def _decode_png_instance_masks(self, keys_to_tensors): - """Decode PNG instance segmentation masks and stack into dense tensor. - - The instance segmentation masks are reshaped to [num_instances, height, - width]. - - Args: - keys_to_tensors: a dictionary from keys to tensors. - - Returns: - A 3-D float tensor of shape [num_instances, height, width] with values - in {0, 1}. - """ - - def decode_png_mask(image_buffer): - image = tf.squeeze( - tf.image.decode_image(image_buffer, channels=1), axis=2) - image.set_shape([None, None]) - image = tf.cast(tf.greater(image, 0), dtype=tf.float32) - return image - - png_masks = keys_to_tensors['image/object/mask'] - height = keys_to_tensors['image/height'] - width = keys_to_tensors['image/width'] - if isinstance(png_masks, tf.SparseTensor): - png_masks = tf.sparse_tensor_to_dense(png_masks, default_value='') - return tf.cond( - tf.greater(tf.size(png_masks), 0), - lambda: tf.map_fn(decode_png_mask, png_masks, dtype=tf.float32), - lambda: tf.zeros(tf.cast(tf.stack([0, height, width]), dtype=tf.int32))) - - def _dense_pose_part_indices(self, keys_to_tensors): - """Creates a tensor that contains part indices for each DensePose point. - - Args: - keys_to_tensors: a dictionary from keys to tensors. - - Returns: - A 2-D int32 tensor of shape [num_instances, num_points] where each element - contains the DensePose part index (0-23). The value `num_points` - corresponds to the maximum number of sampled points across all instances - in the image. Note that instances with less sampled points will be padded - with zeros in the last dimension. - """ - num_points_per_instances = keys_to_tensors['image/object/densepose/num'] - part_index = keys_to_tensors['image/object/densepose/part_index'] - if isinstance(num_points_per_instances, tf.SparseTensor): - num_points_per_instances = tf.sparse_tensor_to_dense( - num_points_per_instances) - if isinstance(part_index, tf.SparseTensor): - part_index = tf.sparse_tensor_to_dense(part_index) - part_index = tf.cast(part_index, dtype=tf.int32) - max_points_per_instance = tf.cast( - tf.math.reduce_max(num_points_per_instances), dtype=tf.int32) - num_points_cumulative = tf.concat([ - [0], tf.math.cumsum(num_points_per_instances)], axis=0) - - def pad_parts_tensor(instance_ind): - points_range_start = num_points_cumulative[instance_ind] - points_range_end = num_points_cumulative[instance_ind + 1] - part_inds = part_index[points_range_start:points_range_end] - return shape_utils.pad_or_clip_nd(part_inds, - output_shape=[max_points_per_instance]) - - return tf.map_fn(pad_parts_tensor, - tf.range(tf.size(num_points_per_instances)), - dtype=tf.int32) - - def _dense_pose_surface_coordinates(self, keys_to_tensors): - """Creates a tensor that contains surface coords for each DensePose point. - - Args: - keys_to_tensors: a dictionary from keys to tensors. - - Returns: - A 3-D float32 tensor of shape [num_instances, num_points, 4] where each - point contains (y, x, v, u) data for each sampled DensePose point. The - (y, x) coordinate has normalized image locations for the point, and (v, u) - contains the surface coordinate (also normalized) for the part. The value - `num_points` corresponds to the maximum number of sampled points across - all instances in the image. Note that instances with less sampled points - will be padded with zeros in dim=1. - """ - num_points_per_instances = keys_to_tensors['image/object/densepose/num'] - dp_y = keys_to_tensors['image/object/densepose/y'] - dp_x = keys_to_tensors['image/object/densepose/x'] - dp_v = keys_to_tensors['image/object/densepose/v'] - dp_u = keys_to_tensors['image/object/densepose/u'] - if isinstance(num_points_per_instances, tf.SparseTensor): - num_points_per_instances = tf.sparse_tensor_to_dense( - num_points_per_instances) - if isinstance(dp_y, tf.SparseTensor): - dp_y = tf.sparse_tensor_to_dense(dp_y) - if isinstance(dp_x, tf.SparseTensor): - dp_x = tf.sparse_tensor_to_dense(dp_x) - if isinstance(dp_v, tf.SparseTensor): - dp_v = tf.sparse_tensor_to_dense(dp_v) - if isinstance(dp_u, tf.SparseTensor): - dp_u = tf.sparse_tensor_to_dense(dp_u) - max_points_per_instance = tf.cast( - tf.math.reduce_max(num_points_per_instances), dtype=tf.int32) - num_points_cumulative = tf.concat([ - [0], tf.math.cumsum(num_points_per_instances)], axis=0) - - def pad_surface_coordinates_tensor(instance_ind): - """Pads DensePose surface coordinates for each instance.""" - points_range_start = num_points_cumulative[instance_ind] - points_range_end = num_points_cumulative[instance_ind + 1] - y = dp_y[points_range_start:points_range_end] - x = dp_x[points_range_start:points_range_end] - v = dp_v[points_range_start:points_range_end] - u = dp_u[points_range_start:points_range_end] - # Create [num_points_i, 4] tensor, where num_points_i is the number of - # sampled points for instance i. - unpadded_tensor = tf.stack([y, x, v, u], axis=1) - return shape_utils.pad_or_clip_nd( - unpadded_tensor, output_shape=[max_points_per_instance, 4]) - - return tf.map_fn(pad_surface_coordinates_tensor, - tf.range(tf.size(num_points_per_instances)), - dtype=tf.float32) - - def _expand_image_label_hierarchy(self, image_classes, image_confidences): - """Expand image level labels according to the hierarchy. - - Args: - image_classes: Int64 tensor with the image level class ids for a sample. - image_confidences: Float tensor signaling whether a class id is present in - the image (1.0) or not present (0.0). - - Returns: - new_image_classes: Int64 tensor equal to expanding image_classes. - new_image_confidences: Float tensor equal to expanding image_confidences. - """ - - def expand_labels(relation_tensor, confidence_value): - """Expand to ancestors or descendants depending on arguments.""" - mask = tf.equal(image_confidences, confidence_value) - target_image_classes = tf.boolean_mask(image_classes, mask) - expanded_indices = tf.reduce_any((tf.gather( - relation_tensor, target_image_classes - _LABEL_OFFSET, axis=0) > 0), - axis=0) - expanded_indices = tf.where(expanded_indices)[:, 0] + _LABEL_OFFSET - new_groundtruth_image_classes = ( - tf.concat([ - tf.boolean_mask(image_classes, tf.logical_not(mask)), - expanded_indices, - ], - axis=0)) - new_groundtruth_image_confidences = ( - tf.concat([ - tf.boolean_mask(image_confidences, tf.logical_not(mask)), - tf.ones([tf.shape(expanded_indices)[0]], - dtype=image_confidences.dtype) * confidence_value, - ], - axis=0)) - return new_groundtruth_image_classes, new_groundtruth_image_confidences - - image_classes, image_confidences = expand_labels(self._ancestors_lut, 1.0) - new_image_classes, new_image_confidences = expand_labels( - self._descendants_lut, 0.0) - return new_image_classes, new_image_confidences - - def _expansion_box_field_labels(self, - object_classes, - object_field, - copy_class_id=False): - """Expand the labels of a specific object field according to the hierarchy. - - Args: - object_classes: Int64 tensor with the class id for each element in - object_field. - object_field: Tensor to be expanded. - copy_class_id: Boolean to choose whether to use class id values in the - output tensor instead of replicating the original values. - - Returns: - A tensor with the result of expanding object_field. - """ - expanded_indices = tf.gather( - self._ancestors_lut, object_classes - _LABEL_OFFSET, axis=0) - if copy_class_id: - new_object_field = tf.where(expanded_indices > 0)[:, 1] + _LABEL_OFFSET - else: - new_object_field = tf.repeat( - object_field, tf.reduce_sum(expanded_indices, axis=1), axis=0) - return new_object_field - diff --git a/research/object_detection/data_decoders/tf_example_decoder_test.py b/research/object_detection/data_decoders/tf_example_decoder_test.py deleted file mode 100644 index 13cde9b4b37..00000000000 --- a/research/object_detection/data_decoders/tf_example_decoder_test.py +++ /dev/null @@ -1,2001 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.data_decoders.tf_example_decoder.""" - -import os -import numpy as np -import six -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields as fields -from object_detection.data_decoders import tf_example_decoder -from object_detection.protos import input_reader_pb2 -from object_detection.utils import dataset_util -from object_detection.utils import test_case - - -class TfExampleDecoderTest(test_case.TestCase): - - def _create_encoded_and_decoded_data(self, data, encoding_type): - if encoding_type == 'jpeg': - encode_fn = tf.image.encode_jpeg - decode_fn = tf.image.decode_jpeg - elif encoding_type == 'png': - encode_fn = tf.image.encode_png - decode_fn = tf.image.decode_png - else: - raise ValueError('Invalid encoding type.') - - def prepare_data_fn(): - encoded_data = encode_fn(data) - decoded_data = decode_fn(encoded_data) - return encoded_data, decoded_data - - return self.execute_cpu(prepare_data_fn, []) - - def testDecodeAdditionalChannels(self): - image = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data(image, 'jpeg') - - additional_channel = np.random.randint(256, size=(4, 5, 1)).astype(np.uint8) - (encoded_additional_channel, - decoded_additional_channel) = self._create_encoded_and_decoded_data( - additional_channel, 'jpeg') - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/additional_channels/encoded': - dataset_util.bytes_list_feature( - [encoded_additional_channel] * 2), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/source_id': - dataset_util.bytes_feature(six.b('image_id')), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - num_additional_channels=2) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - np.concatenate([decoded_additional_channel] * 2, axis=2), - tensor_dict[fields.InputDataFields.image_additional_channels]) - - def testDecodeJpegImage(self): - image = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, decoded_jpeg = self._create_encoded_and_decoded_data( - image, 'jpeg') - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/source_id': - dataset_util.bytes_feature(six.b('image_id')), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - self.assertAllEqual( - (output[fields.InputDataFields.image].get_shape().as_list()), - [None, None, 3]) - self.assertAllEqual( - (output[fields.InputDataFields.original_image_spatial_shape] - .get_shape().as_list()), [2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual(decoded_jpeg, tensor_dict[fields.InputDataFields.image]) - self.assertAllEqual([4, 5], tensor_dict[fields.InputDataFields. - original_image_spatial_shape]) - self.assertEqual( - six.b('image_id'), tensor_dict[fields.InputDataFields.source_id]) - - def testDecodeImageKeyAndFilename(self): - image = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data(image, 'jpeg') - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/key/sha256': - dataset_util.bytes_feature(six.b('abc')), - 'image/filename': - dataset_util.bytes_feature(six.b('filename')) - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertEqual(six.b('abc'), tensor_dict[fields.InputDataFields.key]) - self.assertEqual( - six.b('filename'), tensor_dict[fields.InputDataFields.filename]) - - def testDecodePngImage(self): - image = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_png, decoded_png = self._create_encoded_and_decoded_data( - image, 'png') - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_png), - 'image/format': - dataset_util.bytes_feature(six.b('png')), - 'image/source_id': - dataset_util.bytes_feature(six.b('image_id')) - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - self.assertAllEqual( - (output[fields.InputDataFields.image].get_shape().as_list()), - [None, None, 3]) - self.assertAllEqual( - (output[fields.InputDataFields.original_image_spatial_shape] - .get_shape().as_list()), [2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual(decoded_png, tensor_dict[fields.InputDataFields.image]) - self.assertAllEqual([4, 5], tensor_dict[fields.InputDataFields. - original_image_spatial_shape]) - self.assertEqual( - six.b('image_id'), tensor_dict[fields.InputDataFields.source_id]) - - def testDecodePngInstanceMasks(self): - image = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_png, _ = self._create_encoded_and_decoded_data(image, 'png') - mask_1 = np.random.randint(0, 2, size=(10, 10, 1)).astype(np.uint8) - mask_2 = np.random.randint(0, 2, size=(10, 10, 1)).astype(np.uint8) - encoded_png_1, _ = self._create_encoded_and_decoded_data(mask_1, 'png') - decoded_png_1 = np.squeeze(mask_1.astype(np.float32)) - encoded_png_2, _ = self._create_encoded_and_decoded_data(mask_2, 'png') - decoded_png_2 = np.squeeze(mask_2.astype(np.float32)) - encoded_masks = [encoded_png_1, encoded_png_2] - decoded_masks = np.stack([decoded_png_1, decoded_png_2]) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_png), - 'image/format': - dataset_util.bytes_feature(six.b('png')), - 'image/object/mask': - dataset_util.bytes_list_feature(encoded_masks) - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - load_instance_masks=True, - instance_mask_type=input_reader_pb2.PNG_MASKS) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - decoded_masks, - tensor_dict[fields.InputDataFields.groundtruth_instance_masks]) - - def testDecodeEmptyPngInstanceMasks(self): - image_tensor = np.random.randint(256, size=(10, 10, 3)).astype(np.uint8) - encoded_png, _ = self._create_encoded_and_decoded_data(image_tensor, 'png') - encoded_masks = [] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_png), - 'image/format': - dataset_util.bytes_feature(six.b('png')), - 'image/object/mask': - dataset_util.bytes_list_feature(encoded_masks), - 'image/height': - dataset_util.int64_feature(10), - 'image/width': - dataset_util.int64_feature(10), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - load_instance_masks=True, - instance_mask_type=input_reader_pb2.PNG_MASKS) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - tensor_dict[fields.InputDataFields.groundtruth_instance_masks].shape, - [0, 10, 10]) - - def testDecodeBoundingBox(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()), - [None, 4]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - expected_boxes = np.vstack([bbox_ymins, bbox_xmins, bbox_ymaxs, - bbox_xmaxs]).transpose() - self.assertAllEqual(expected_boxes, - tensor_dict[fields.InputDataFields.groundtruth_boxes]) - - def testDecodeKeypointDepth(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - keypoint_ys = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] - keypoint_xs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] - keypoint_visibility = [1, 2, 0, 1, 0, 2] - keypoint_depths = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] - keypoint_depth_weights = [1.0, 0.9, 0.8, 0.7, 0.6, 0.5] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/keypoint/y': - dataset_util.float_list_feature(keypoint_ys), - 'image/object/keypoint/x': - dataset_util.float_list_feature(keypoint_xs), - 'image/object/keypoint/z': - dataset_util.float_list_feature(keypoint_depths), - 'image/object/keypoint/z/weights': - dataset_util.float_list_feature(keypoint_depth_weights), - 'image/object/keypoint/visibility': - dataset_util.int64_list_feature(keypoint_visibility), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - num_keypoints=3, load_keypoint_depth_features=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual( - (output[fields.InputDataFields.groundtruth_keypoint_depths].get_shape( - ).as_list()), [2, 3]) - self.assertAllEqual( - (output[fields.InputDataFields.groundtruth_keypoint_depth_weights] - .get_shape().as_list()), [2, 3]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - expected_keypoint_depths = [[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]] - self.assertAllClose( - expected_keypoint_depths, - tensor_dict[fields.InputDataFields.groundtruth_keypoint_depths]) - - expected_keypoint_depth_weights = [[1.0, 0.9, 0.8], [0.7, 0.6, 0.5]] - self.assertAllClose( - expected_keypoint_depth_weights, - tensor_dict[fields.InputDataFields.groundtruth_keypoint_depth_weights]) - - def testDecodeKeypointDepthNoDepth(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - keypoint_ys = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] - keypoint_xs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] - keypoint_visibility = [1, 2, 0, 1, 0, 2] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/keypoint/y': - dataset_util.float_list_feature(keypoint_ys), - 'image/object/keypoint/x': - dataset_util.float_list_feature(keypoint_xs), - 'image/object/keypoint/visibility': - dataset_util.int64_list_feature(keypoint_visibility), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - num_keypoints=3, load_keypoint_depth_features=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - expected_keypoints_depth_default = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] - self.assertAllClose( - expected_keypoints_depth_default, - tensor_dict[fields.InputDataFields.groundtruth_keypoint_depths]) - self.assertAllClose( - expected_keypoints_depth_default, - tensor_dict[fields.InputDataFields.groundtruth_keypoint_depth_weights]) - - def testDecodeKeypoint(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - keypoint_ys = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] - keypoint_xs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] - keypoint_visibility = [1, 2, 0, 1, 0, 2] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/keypoint/y': - dataset_util.float_list_feature(keypoint_ys), - 'image/object/keypoint/x': - dataset_util.float_list_feature(keypoint_xs), - 'image/object/keypoint/visibility': - dataset_util.int64_list_feature(keypoint_visibility), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder(num_keypoints=3) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()), - [None, 4]) - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_keypoints].get_shape().as_list()), - [2, 3, 2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - expected_boxes = np.vstack([bbox_ymins, bbox_xmins, bbox_ymaxs, - bbox_xmaxs]).transpose() - self.assertAllEqual(expected_boxes, - tensor_dict[fields.InputDataFields.groundtruth_boxes]) - - expected_keypoints = [ - [[0.0, 1.0], [1.0, 2.0], [np.nan, np.nan]], - [[3.0, 4.0], [np.nan, np.nan], [5.0, 6.0]]] - self.assertAllClose( - expected_keypoints, - tensor_dict[fields.InputDataFields.groundtruth_keypoints]) - - expected_visibility = ( - (np.array(keypoint_visibility) > 0).reshape((2, 3))) - self.assertAllEqual( - expected_visibility, - tensor_dict[fields.InputDataFields.groundtruth_keypoint_visibilities]) - - def testDecodeKeypointNoInstance(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [] - bbox_xmins = [] - bbox_ymaxs = [] - bbox_xmaxs = [] - keypoint_ys = [] - keypoint_xs = [] - keypoint_visibility = [] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/keypoint/y': - dataset_util.float_list_feature(keypoint_ys), - 'image/object/keypoint/x': - dataset_util.float_list_feature(keypoint_xs), - 'image/object/keypoint/visibility': - dataset_util.int64_list_feature(keypoint_visibility), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder(num_keypoints=3) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()), - [None, 4]) - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_keypoints].get_shape().as_list()), - [0, 3, 2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - [0, 4], tensor_dict[fields.InputDataFields.groundtruth_boxes].shape) - self.assertAllEqual( - [0, 3, 2], - tensor_dict[fields.InputDataFields.groundtruth_keypoints].shape) - - def testDecodeKeypointWithText(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes = [0, 1] - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - keypoint_ys = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] - keypoint_xs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] - keypoint_visibility = [1, 2, 0, 1, 0, 2] - keypoint_texts = [ - six.b('nose'), six.b('left_eye'), six.b('right_eye'), six.b('nose'), - six.b('left_eye'), six.b('right_eye') - ] - - label_map_string = """ - item: { - id: 1 - name: 'face' - display_name: 'face' - keypoints { - id: 0 - label: "nose" - } - keypoints { - id: 2 - label: "right_eye" - } - } - item: { - id: 2 - name: 'person' - display_name: 'person' - keypoints { - id: 1 - label: "left_eye" - } - } - """ - label_map_proto_file = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_proto_file, 'wb') as f: - f.write(label_map_string) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/keypoint/y': - dataset_util.float_list_feature(keypoint_ys), - 'image/object/keypoint/x': - dataset_util.float_list_feature(keypoint_xs), - 'image/object/keypoint/visibility': - dataset_util.int64_list_feature(keypoint_visibility), - 'image/object/keypoint/text': - dataset_util.bytes_list_feature(keypoint_texts), - 'image/object/class/label': - dataset_util.int64_list_feature(bbox_classes), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_proto_file, num_keypoints=5, - use_keypoint_label_map=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()), - [None, 4]) - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_keypoints].get_shape().as_list()), - [None, 5, 2]) - return output - - output = self.execute_cpu(graph_fn, []) - expected_boxes = np.vstack([bbox_ymins, bbox_xmins, bbox_ymaxs, - bbox_xmaxs]).transpose() - self.assertAllEqual(expected_boxes, - output[fields.InputDataFields.groundtruth_boxes]) - - expected_keypoints = [[[0.0, 1.0], [1.0, 2.0], [np.nan, np.nan], - [np.nan, np.nan], [np.nan, np.nan]], - [[3.0, 4.0], [np.nan, np.nan], [5.0, 6.0], - [np.nan, np.nan], [np.nan, np.nan]]] - self.assertAllClose(expected_keypoints, - output[fields.InputDataFields.groundtruth_keypoints]) - - expected_visibility = ( - (np.array(keypoint_visibility) > 0).reshape((2, 3))) - gt_kpts_vis_fld = fields.InputDataFields.groundtruth_keypoint_visibilities - self.assertAllEqual(expected_visibility, output[gt_kpts_vis_fld][:, 0:3]) - # The additional keypoints should all have False visibility. - self.assertAllEqual( - np.zeros([2, 2], dtype=bool), output[gt_kpts_vis_fld][:, 3:]) - - def testDecodeKeypointWithKptsLabelsNotInText(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes = [0, 1] - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - keypoint_ys = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] - keypoint_xs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] - keypoint_visibility = [1, 2, 0, 1, 0, 2] - keypoint_texts = [ - six.b('nose'), six.b('left_eye'), six.b('right_eye'), six.b('nose'), - six.b('left_eye'), six.b('right_eye') - ] - - label_map_string = """ - item: { - id: 1 - name: 'face' - display_name: 'face' - keypoints { - id: 0 - label: "missing_part" - } - keypoints { - id: 2 - label: "right_eye" - } - keypoints { - id: 3 - label: "nose" - } - } - item: { - id: 2 - name: 'person' - display_name: 'person' - keypoints { - id: 1 - label: "left_eye" - } - } - """ - label_map_proto_file = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_proto_file, 'wb') as f: - f.write(label_map_string) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/keypoint/y': - dataset_util.float_list_feature(keypoint_ys), - 'image/object/keypoint/x': - dataset_util.float_list_feature(keypoint_xs), - 'image/object/keypoint/visibility': - dataset_util.int64_list_feature(keypoint_visibility), - 'image/object/keypoint/text': - dataset_util.bytes_list_feature(keypoint_texts), - 'image/object/class/label': - dataset_util.int64_list_feature(bbox_classes), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_proto_file, num_keypoints=5, - use_keypoint_label_map=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()), - [None, 4]) - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_keypoints].get_shape().as_list()), - [None, 5, 2]) - return output - - output = self.execute_cpu(graph_fn, []) - expected_boxes = np.vstack([bbox_ymins, bbox_xmins, bbox_ymaxs, - bbox_xmaxs]).transpose() - self.assertAllEqual(expected_boxes, - output[fields.InputDataFields.groundtruth_boxes]) - - expected_keypoints = [[[np.nan, np.nan], [1., 2.], [np.nan, np.nan], - [0., 1.], [np.nan, np.nan]], - [[np.nan, np.nan], [np.nan, np.nan], [5., 6.], - [3., 4.], [np.nan, np.nan]]] - - gt_kpts_vis_fld = fields.InputDataFields.groundtruth_keypoint_visibilities - self.assertAllClose(expected_keypoints, - output[fields.InputDataFields.groundtruth_keypoints]) - - expected_visibility = [[False, True, False, True, False], - [False, False, True, True, False]] - gt_kpts_vis_fld = fields.InputDataFields.groundtruth_keypoint_visibilities - self.assertAllEqual(expected_visibility, output[gt_kpts_vis_fld]) - - def testDecodeKeypointNoVisibilities(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - keypoint_ys = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0] - keypoint_xs = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/keypoint/y': - dataset_util.float_list_feature(keypoint_ys), - 'image/object/keypoint/x': - dataset_util.float_list_feature(keypoint_xs), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder(num_keypoints=3) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()), - [None, 4]) - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_keypoints].get_shape().as_list()), - [2, 3, 2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - expected_boxes = np.vstack([bbox_ymins, bbox_xmins, bbox_ymaxs, - bbox_xmaxs]).transpose() - self.assertAllEqual(expected_boxes, - tensor_dict[fields.InputDataFields.groundtruth_boxes]) - - expected_keypoints = ( - np.vstack([keypoint_ys, keypoint_xs]).transpose().reshape((2, 3, 2))) - self.assertAllEqual( - expected_keypoints, - tensor_dict[fields.InputDataFields.groundtruth_keypoints]) - - expected_visibility = np.ones((2, 3)) - self.assertAllEqual( - expected_visibility, - tensor_dict[fields.InputDataFields.groundtruth_keypoint_visibilities]) - - def testDecodeDefaultGroundtruthWeights(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()), - [None, 4]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllClose(tensor_dict[fields.InputDataFields.groundtruth_weights], - np.ones(2, dtype=np.float32)) - - def testDecodeObjectLabel(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes = [0, 1] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/label': - dataset_util.int64_list_feature(bbox_classes), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_classes].get_shape().as_list()), - [2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - self.assertAllEqual(bbox_classes, - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeMultiClassScores(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - flattened_multiclass_scores = [100., 50.] + [20., 30.] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/multiclass_scores': - dataset_util.float_list_feature( - flattened_multiclass_scores), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - load_multiclass_scores=True) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual(flattened_multiclass_scores, - tensor_dict[fields.InputDataFields.multiclass_scores]) - - def testDecodeEmptyMultiClassScores(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - load_multiclass_scores=True) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertEqual( - (0,), tensor_dict[fields.InputDataFields.multiclass_scores].shape) - - def testDecodeObjectLabelNoText(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes = [1, 2] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/label': - dataset_util.int64_list_feature(bbox_classes), - })).SerializeToString() - label_map_string = """ - item { - id:1 - name:'cat' - } - item { - id:2 - name:'dog' - } - """ - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_classes].get_shape().as_list()), - [None]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - self.assertAllEqual(bbox_classes, - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeObjectLabelWithText(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes_text = [six.b('cat'), six.b('dog')] - # Annotation label gets overridden by labelmap id. - annotated_bbox_classes = [3, 4] - expected_bbox_classes = [1, 2] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/text': - dataset_util.bytes_list_feature(bbox_classes_text), - 'image/object/class/label': - dataset_util.int64_list_feature(annotated_bbox_classes), - })).SerializeToString() - label_map_string = """ - item { - id:1 - name:'cat' - keypoints { - id: 0 - label: "nose" - } - keypoints { - id: 1 - label: "left_eye" - } - keypoints { - id: 2 - label: "right_eye" - } - } - item { - id:2 - name:'dog' - } - """ - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - - self.assertAllEqual(expected_bbox_classes, - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeObjectLabelUnrecognizedName(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes_text = [six.b('cat'), six.b('cheetah')] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/text': - dataset_util.bytes_list_feature(bbox_classes_text), - })).SerializeToString() - - label_map_string = """ - item { - id:2 - name:'cat' - } - item { - id:1 - name:'dog' - } - """ - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path) - output = example_decoder.decode(tf.convert_to_tensor(example)) - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_classes].get_shape().as_list()), - [None]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual([2, -1], - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeObjectLabelWithMappingWithDisplayName(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes_text = [six.b('cat'), six.b('dog')] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/text': - dataset_util.bytes_list_feature(bbox_classes_text), - })).SerializeToString() - - label_map_string = """ - item { - id:3 - display_name:'cat' - } - item { - id:1 - display_name:'dog' - } - """ - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_classes].get_shape().as_list()), - [None]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual([3, 1], - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeObjectLabelUnrecognizedNameWithMappingWithDisplayName(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes_text = [six.b('cat'), six.b('cheetah')] - bbox_classes_id = [5, 6] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/text': - dataset_util.bytes_list_feature(bbox_classes_text), - 'image/object/class/label': - dataset_util.int64_list_feature(bbox_classes_id), - })).SerializeToString() - - label_map_string = """ - item { - name:'/m/cat' - id:3 - display_name:'cat' - } - item { - name:'/m/dog' - id:1 - display_name:'dog' - } - """ - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual([3, -1], - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeObjectLabelWithMappingWithName(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_classes_text = [six.b('cat'), six.b('dog')] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/class/text': - dataset_util.bytes_list_feature(bbox_classes_text), - })).SerializeToString() - - label_map_string = """ - item { - id:3 - name:'cat' - } - item { - id:1 - name:'dog' - } - """ - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_classes].get_shape().as_list()), - [None]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual([3, 1], - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeObjectArea(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - object_area = [100., 174.] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/area': - dataset_util.float_list_feature(object_area), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_area].get_shape().as_list()), [2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - self.assertAllEqual(object_area, - tensor_dict[fields.InputDataFields.groundtruth_area]) - - def testDecodeVerifiedNegClasses(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - neg_category_ids = [0, 5, 8] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/neg_category_ids': - dataset_util.int64_list_feature(neg_category_ids), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - neg_category_ids, - tensor_dict[fields.InputDataFields.groundtruth_verified_neg_classes]) - - def testDecodeNotExhaustiveClasses(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - not_exhaustive_category_ids = [0, 5, 8] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/not_exhaustive_category_ids': - dataset_util.int64_list_feature( - not_exhaustive_category_ids), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - not_exhaustive_category_ids, - tensor_dict[fields.InputDataFields.groundtruth_not_exhaustive_classes]) - - def testDecodeObjectIsCrowd(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - object_is_crowd = [0, 1] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/is_crowd': - dataset_util.int64_list_feature(object_is_crowd), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_is_crowd].get_shape().as_list()), - [2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - [bool(item) for item in object_is_crowd], - tensor_dict[fields.InputDataFields.groundtruth_is_crowd]) - - def testDecodeObjectDifficult(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - object_difficult = [0, 1] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/difficult': - dataset_util.int64_list_feature(object_difficult), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_difficult].get_shape().as_list()), - [2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - [bool(item) for item in object_difficult], - tensor_dict[fields.InputDataFields.groundtruth_difficult]) - - def testDecodeObjectGroupOf(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - object_group_of = [0, 1] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/group_of': - dataset_util.int64_list_feature(object_group_of), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_group_of].get_shape().as_list()), - [2]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - [bool(item) for item in object_group_of], - tensor_dict[fields.InputDataFields.groundtruth_group_of]) - - def testDecodeObjectWeight(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - object_weights = [0.75, 1.0] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/weight': - dataset_util.float_list_feature(object_weights), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_weights].get_shape().as_list()), - [None]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - self.assertAllEqual(object_weights, - tensor_dict[fields.InputDataFields.groundtruth_weights]) - - def testDecodeClassConfidence(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - class_confidence = [0.0, 1.0, 0.0] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/class/confidence': - dataset_util.float_list_feature(class_confidence), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual( - (output[fields.InputDataFields.groundtruth_image_confidences] - .get_shape().as_list()), [3]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - class_confidence, - tensor_dict[fields.InputDataFields.groundtruth_image_confidences]) - - def testDecodeInstanceSegmentation(self): - num_instances = 4 - image_height = 5 - image_width = 3 - - # Randomly generate image. - image_tensor = np.random.randint( - 256, size=(image_height, image_width, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - - # Randomly generate instance segmentation masks. - instance_masks = ( - np.random.randint(2, size=(num_instances, image_height, - image_width)).astype(np.float32)) - instance_masks_flattened = np.reshape(instance_masks, [-1]) - - # Randomly generate class labels for each instance. - object_classes = np.random.randint( - 100, size=(num_instances)).astype(np.int64) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/height': - dataset_util.int64_feature(image_height), - 'image/width': - dataset_util.int64_feature(image_width), - 'image/object/mask': - dataset_util.float_list_feature(instance_masks_flattened), - 'image/object/class/label': - dataset_util.int64_list_feature(object_classes) - })).SerializeToString() - example_decoder = tf_example_decoder.TfExampleDecoder( - load_instance_masks=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual( - (output[fields.InputDataFields.groundtruth_instance_masks].get_shape( - ).as_list()), [4, 5, 3]) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_classes].get_shape().as_list()), - [4]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - self.assertAllEqual( - instance_masks.astype(np.float32), - tensor_dict[fields.InputDataFields.groundtruth_instance_masks]) - self.assertAllEqual( - tensor_dict[fields.InputDataFields.groundtruth_instance_mask_weights], - [1, 1, 1, 1]) - self.assertAllEqual(object_classes, - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testInstancesNotAvailableByDefault(self): - num_instances = 4 - image_height = 5 - image_width = 3 - # Randomly generate image. - image_tensor = np.random.randint( - 256, size=(image_height, image_width, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - - # Randomly generate instance segmentation masks. - instance_masks = ( - np.random.randint(2, size=(num_instances, image_height, - image_width)).astype(np.float32)) - instance_masks_flattened = np.reshape(instance_masks, [-1]) - - # Randomly generate class labels for each instance. - object_classes = np.random.randint( - 100, size=(num_instances)).astype(np.int64) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/height': - dataset_util.int64_feature(image_height), - 'image/width': - dataset_util.int64_feature(image_width), - 'image/object/mask': - dataset_util.float_list_feature(instance_masks_flattened), - 'image/object/class/label': - dataset_util.int64_list_feature(object_classes) - })).SerializeToString() - example_decoder = tf_example_decoder.TfExampleDecoder() - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertNotIn(fields.InputDataFields.groundtruth_instance_masks, - tensor_dict) - - def testDecodeInstanceSegmentationWithWeights(self): - num_instances = 4 - image_height = 5 - image_width = 3 - - # Randomly generate image. - image_tensor = np.random.randint( - 256, size=(image_height, image_width, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - - # Randomly generate instance segmentation masks. - instance_masks = ( - np.random.randint(2, size=(num_instances, image_height, - image_width)).astype(np.float32)) - instance_masks_flattened = np.reshape(instance_masks, [-1]) - instance_mask_weights = np.array([1, 1, 0, 1], dtype=np.float32) - - # Randomly generate class labels for each instance. - object_classes = np.random.randint( - 100, size=(num_instances)).astype(np.int64) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/height': - dataset_util.int64_feature(image_height), - 'image/width': - dataset_util.int64_feature(image_width), - 'image/object/mask': - dataset_util.float_list_feature(instance_masks_flattened), - 'image/object/mask/weight': - dataset_util.float_list_feature(instance_mask_weights), - 'image/object/class/label': - dataset_util.int64_list_feature(object_classes) - })).SerializeToString() - example_decoder = tf_example_decoder.TfExampleDecoder( - load_instance_masks=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - - self.assertAllEqual( - (output[fields.InputDataFields.groundtruth_instance_masks].get_shape( - ).as_list()), [4, 5, 3]) - self.assertAllEqual( - output[fields.InputDataFields.groundtruth_instance_mask_weights], - [1, 1, 0, 1]) - - self.assertAllEqual((output[ - fields.InputDataFields.groundtruth_classes].get_shape().as_list()), - [4]) - return output - - tensor_dict = self.execute_cpu(graph_fn, []) - - self.assertAllEqual( - instance_masks.astype(np.float32), - tensor_dict[fields.InputDataFields.groundtruth_instance_masks]) - self.assertAllEqual(object_classes, - tensor_dict[fields.InputDataFields.groundtruth_classes]) - - def testDecodeImageLabels(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - - def graph_fn_1(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': dataset_util.bytes_feature(encoded_jpeg), - 'image/format': dataset_util.bytes_feature(six.b('jpeg')), - 'image/class/label': dataset_util.int64_list_feature([1, 2]), - })).SerializeToString() - example_decoder = tf_example_decoder.TfExampleDecoder() - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn_1, []) - self.assertIn(fields.InputDataFields.groundtruth_image_classes, tensor_dict) - self.assertAllEqual( - tensor_dict[fields.InputDataFields.groundtruth_image_classes], - np.array([1, 2])) - - def graph_fn_2(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/class/text': - dataset_util.bytes_list_feature( - [six.b('dog'), six.b('cat')]), - })).SerializeToString() - label_map_string = """ - item { - id:3 - name:'cat' - } - item { - id:1 - name:'dog' - } - """ - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn_2, []) - self.assertIn(fields.InputDataFields.groundtruth_image_classes, tensor_dict) - self.assertAllEqual( - tensor_dict[fields.InputDataFields.groundtruth_image_classes], - np.array([1, 3])) - - def testDecodeContextFeatures(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - num_features = 8 - context_feature_length = 10 - context_features = np.random.random(num_features*context_feature_length) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/context_features': - dataset_util.float_list_feature(context_features), - 'image/context_feature_length': - dataset_util.int64_feature(context_feature_length), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - load_context_features=True) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertAllClose( - context_features.reshape(num_features, context_feature_length), - tensor_dict[fields.InputDataFields.context_features]) - self.assertAllEqual( - context_feature_length, - tensor_dict[fields.InputDataFields.context_feature_length]) - - def testContextFeaturesNotAvailableByDefault(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - num_features = 10 - context_feature_length = 10 - context_features = np.random.random(num_features*context_feature_length) - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/context_features': - dataset_util.float_list_feature(context_features), - 'image/context_feature_length': - dataset_util.int64_feature(context_feature_length), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder() - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - self.assertNotIn(fields.InputDataFields.context_features, - tensor_dict) - - def testExpandLabels(self): - label_map_string = """ - item { - id:1 - name:'cat' - ancestor_ids: 2 - } - item { - id:2 - name:'animal' - descendant_ids: 1 - } - item { - id:3 - name:'man' - ancestor_ids: 5 - } - item { - id:4 - name:'woman' - display_name:'woman' - ancestor_ids: 5 - } - item { - id:5 - name:'person' - descendant_ids: 3 - descendant_ids: 4 - } - """ - - label_map_path = os.path.join(self.get_temp_dir(), 'label_map.pbtxt') - with tf.gfile.Open(label_map_path, 'wb') as f: - f.write(label_map_string) - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0] - bbox_xmins = [1.0, 5.0] - bbox_ymaxs = [2.0, 6.0] - bbox_xmaxs = [3.0, 7.0] - bbox_classes_text = [six.b('cat'), six.b('cat')] - bbox_group_of = [0, 1] - image_class_text = [six.b('cat'), six.b('person')] - image_confidence = [1.0, 0.0] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/class/text': - dataset_util.bytes_list_feature(bbox_classes_text), - 'image/object/group_of': - dataset_util.int64_list_feature(bbox_group_of), - 'image/class/text': - dataset_util.bytes_list_feature(image_class_text), - 'image/class/confidence': - dataset_util.float_list_feature(image_confidence), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - label_map_proto_file=label_map_path, expand_hierarchy_labels=True) - return example_decoder.decode(tf.convert_to_tensor(example)) - - tensor_dict = self.execute_cpu(graph_fn, []) - - boxes = np.vstack([bbox_ymins, bbox_xmins, bbox_ymaxs, - bbox_xmaxs]).transpose() - expected_boxes = np.stack( - [boxes[0, :], boxes[0, :], boxes[1, :], boxes[1, :]], axis=0) - expected_boxes_class = np.array([1, 2, 1, 2]) - expected_boxes_group_of = np.array([0, 0, 1, 1]) - expected_image_class = np.array([1, 2, 3, 4, 5]) - expected_image_confidence = np.array([1.0, 1.0, 0.0, 0.0, 0.0]) - self.assertAllEqual(expected_boxes, - tensor_dict[fields.InputDataFields.groundtruth_boxes]) - self.assertAllEqual(expected_boxes_class, - tensor_dict[fields.InputDataFields.groundtruth_classes]) - self.assertAllEqual( - expected_boxes_group_of, - tensor_dict[fields.InputDataFields.groundtruth_group_of]) - self.assertAllEqual( - expected_image_class, - tensor_dict[fields.InputDataFields.groundtruth_image_classes]) - self.assertAllEqual( - expected_image_confidence, - tensor_dict[fields.InputDataFields.groundtruth_image_confidences]) - - def testDecodeDensePose(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0, 2.0] - bbox_xmins = [1.0, 5.0, 8.0] - bbox_ymaxs = [2.0, 6.0, 1.0] - bbox_xmaxs = [3.0, 7.0, 3.3] - densepose_num = [0, 4, 2] - densepose_part_index = [2, 2, 3, 4, 2, 9] - densepose_x = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6] - densepose_y = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4] - densepose_u = [0.01, 0.02, 0.03, 0.04, 0.05, 0.06] - densepose_v = [0.99, 0.98, 0.97, 0.96, 0.95, 0.94] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/densepose/num': - dataset_util.int64_list_feature(densepose_num), - 'image/object/densepose/part_index': - dataset_util.int64_list_feature(densepose_part_index), - 'image/object/densepose/x': - dataset_util.float_list_feature(densepose_x), - 'image/object/densepose/y': - dataset_util.float_list_feature(densepose_y), - 'image/object/densepose/u': - dataset_util.float_list_feature(densepose_u), - 'image/object/densepose/v': - dataset_util.float_list_feature(densepose_v), - - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - load_dense_pose=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - dp_num_points = output[fields.InputDataFields.groundtruth_dp_num_points] - dp_part_ids = output[fields.InputDataFields.groundtruth_dp_part_ids] - dp_surface_coords = output[ - fields.InputDataFields.groundtruth_dp_surface_coords] - return dp_num_points, dp_part_ids, dp_surface_coords - - dp_num_points, dp_part_ids, dp_surface_coords = self.execute_cpu( - graph_fn, []) - - expected_dp_num_points = [0, 4, 2] - expected_dp_part_ids = [ - [0, 0, 0, 0], - [2, 2, 3, 4], - [2, 9, 0, 0] - ] - expected_dp_surface_coords = np.array( - [ - # Instance 0 (no points). - [[0., 0., 0., 0.], - [0., 0., 0., 0.], - [0., 0., 0., 0.], - [0., 0., 0., 0.]], - # Instance 1 (4 points). - [[0.9, 0.1, 0.99, 0.01], - [0.8, 0.2, 0.98, 0.02], - [0.7, 0.3, 0.97, 0.03], - [0.6, 0.4, 0.96, 0.04]], - # Instance 2 (2 points). - [[0.5, 0.5, 0.95, 0.05], - [0.4, 0.6, 0.94, 0.06], - [0., 0., 0., 0.], - [0., 0., 0., 0.]], - ], dtype=np.float32) - - self.assertAllEqual(dp_num_points, expected_dp_num_points) - self.assertAllEqual(dp_part_ids, expected_dp_part_ids) - self.assertAllClose(dp_surface_coords, expected_dp_surface_coords) - - def testDecodeTrack(self): - image_tensor = np.random.randint(256, size=(4, 5, 3)).astype(np.uint8) - encoded_jpeg, _ = self._create_encoded_and_decoded_data( - image_tensor, 'jpeg') - bbox_ymins = [0.0, 4.0, 2.0] - bbox_xmins = [1.0, 5.0, 8.0] - bbox_ymaxs = [2.0, 6.0, 1.0] - bbox_xmaxs = [3.0, 7.0, 3.3] - track_labels = [0, 1, 2] - - def graph_fn(): - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(bbox_ymins), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(bbox_xmins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(bbox_ymaxs), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(bbox_xmaxs), - 'image/object/track/label': - dataset_util.int64_list_feature(track_labels), - })).SerializeToString() - - example_decoder = tf_example_decoder.TfExampleDecoder( - load_track_id=True) - output = example_decoder.decode(tf.convert_to_tensor(example)) - track_ids = output[fields.InputDataFields.groundtruth_track_ids] - return track_ids - - track_ids = self.execute_cpu(graph_fn, []) - - expected_track_labels = [0, 1, 2] - - self.assertAllEqual(track_ids, expected_track_labels) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/data_decoders/tf_sequence_example_decoder.py b/research/object_detection/data_decoders/tf_sequence_example_decoder.py deleted file mode 100644 index 9bed1597035..00000000000 --- a/research/object_detection/data_decoders/tf_sequence_example_decoder.py +++ /dev/null @@ -1,330 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Sequence example decoder for object detection.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import zip -import tensorflow.compat.v1 as tf -from tf_slim import tfexample_decoder as slim_example_decoder - -from object_detection.core import data_decoder -from object_detection.core import standard_fields as fields -from object_detection.utils import label_map_util - -# pylint: disable=g-import-not-at-top -try: - from tensorflow.contrib import lookup as contrib_lookup -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - - -class _ClassTensorHandler(slim_example_decoder.Tensor): - """An ItemHandler to fetch class ids from class text.""" - - def __init__(self, - tensor_key, - label_map_proto_file, - shape_keys=None, - shape=None, - default_value=''): - """Initializes the LookupTensor handler. - - Simply calls a vocabulary (most often, a label mapping) lookup. - - Args: - tensor_key: the name of the `TFExample` feature to read the tensor from. - label_map_proto_file: File path to a text format LabelMapProto message - mapping class text to id. - shape_keys: Optional name or list of names of the TF-Example feature in - which the tensor shape is stored. If a list, then each corresponds to - one dimension of the shape. - shape: Optional output shape of the `Tensor`. If provided, the `Tensor` is - reshaped accordingly. - default_value: The value used when the `tensor_key` is not found in a - particular `TFExample`. - - Raises: - ValueError: if both `shape_keys` and `shape` are specified. - """ - name_to_id = label_map_util.get_label_map_dict( - label_map_proto_file, use_display_name=False) - # We use a default_value of -1, but we expect all labels to be contained - # in the label map. - try: - # Dynamically try to load the tf v2 lookup, falling back to contrib - lookup = tf.compat.v2.lookup - hash_table_class = tf.compat.v2.lookup.StaticHashTable - except AttributeError: - lookup = contrib_lookup - hash_table_class = contrib_lookup.HashTable - name_to_id_table = hash_table_class( - initializer=lookup.KeyValueTensorInitializer( - keys=tf.constant(list(name_to_id.keys())), - values=tf.constant(list(name_to_id.values()), dtype=tf.int64)), - default_value=-1) - - self._name_to_id_table = name_to_id_table - super(_ClassTensorHandler, self).__init__(tensor_key, shape_keys, shape, - default_value) - - def tensors_to_item(self, keys_to_tensors): - unmapped_tensor = super(_ClassTensorHandler, - self).tensors_to_item(keys_to_tensors) - return self._name_to_id_table.lookup(unmapped_tensor) - - -class TfSequenceExampleDecoder(data_decoder.DataDecoder): - """Tensorflow Sequence Example proto decoder for Object Detection. - - Sequence examples contain sequences of images which share common - features. The structure of TfSequenceExamples can be seen in - dataset_tools/seq_example_util.py - - For the TFODAPI, the following fields are required: - Shared features: - 'image/format' - 'image/height' - 'image/width' - - Features with an entry for each image, where bounding box features can - be empty lists if the image does not contain any objects: - 'image/encoded' - 'image/source_id' - 'region/bbox/xmin' - 'region/bbox/xmax' - 'region/bbox/ymin' - 'region/bbox/ymax' - 'region/label/string' - - Optionally, the sequence example can include context_features for use in - Context R-CNN (see https://arxiv.org/abs/1912.03538): - 'image/context_features' - 'image/context_feature_length' - 'image/context_features_image_id_list' - """ - - def __init__(self, - label_map_proto_file, - load_context_features=False, - load_context_image_ids=False, - use_display_name=False, - fully_annotated=False): - """Constructs `TfSequenceExampleDecoder` object. - - Args: - label_map_proto_file: a file path to a - object_detection.protos.StringIntLabelMap proto. The - label map will be used to map IDs of 'region/label/string'. - It is assumed that 'region/label/string' will be in the data. - load_context_features: Whether to load information from context_features, - to provide additional context to a detection model for training and/or - inference - load_context_image_ids: Whether to load the corresponding image ids for - the context_features in order to visualize attention. - use_display_name: whether or not to use the `display_name` for label - mapping (instead of `name`). Only used if label_map_proto_file is - provided. - fully_annotated: If True, will assume that every frame (whether it has - boxes or not), has been fully annotated. If False, a - 'region/is_annotated' field must be provided in the dataset which - indicates which frames have annotations. Default False. - """ - # Specifies how the tf.SequenceExamples are decoded. - self._context_keys_to_features = { - 'image/format': tf.FixedLenFeature((), tf.string, default_value='jpeg'), - 'image/height': tf.FixedLenFeature((), tf.int64), - 'image/width': tf.FixedLenFeature((), tf.int64), - } - self._sequence_keys_to_feature_lists = { - 'image/encoded': tf.FixedLenSequenceFeature([], dtype=tf.string), - 'image/source_id': tf.FixedLenSequenceFeature([], dtype=tf.string), - 'region/bbox/xmin': tf.VarLenFeature(dtype=tf.float32), - 'region/bbox/xmax': tf.VarLenFeature(dtype=tf.float32), - 'region/bbox/ymin': tf.VarLenFeature(dtype=tf.float32), - 'region/bbox/ymax': tf.VarLenFeature(dtype=tf.float32), - 'region/label/string': tf.VarLenFeature(dtype=tf.string), - 'region/label/confidence': tf.VarLenFeature(dtype=tf.float32), - } - - self._items_to_handlers = { - # Context. - fields.InputDataFields.image_height: - slim_example_decoder.Tensor('image/height'), - fields.InputDataFields.image_width: - slim_example_decoder.Tensor('image/width'), - - # Sequence. - fields.InputDataFields.num_groundtruth_boxes: - slim_example_decoder.NumBoxesSequence('region/bbox/xmin'), - fields.InputDataFields.groundtruth_boxes: - slim_example_decoder.BoundingBoxSequence( - prefix='region/bbox/', default_value=0.0), - fields.InputDataFields.groundtruth_weights: - slim_example_decoder.Tensor('region/label/confidence'), - } - - # If the dataset is sparsely annotated, parse sequence features which - # indicate which frames have been labeled. - if not fully_annotated: - self._sequence_keys_to_feature_lists['region/is_annotated'] = ( - tf.FixedLenSequenceFeature([], dtype=tf.int64)) - self._items_to_handlers[fields.InputDataFields.is_annotated] = ( - slim_example_decoder.Tensor('region/is_annotated')) - - self._items_to_handlers[fields.InputDataFields.image] = ( - slim_example_decoder.Tensor('image/encoded')) - self._items_to_handlers[fields.InputDataFields.source_id] = ( - slim_example_decoder.Tensor('image/source_id')) - - label_handler = _ClassTensorHandler( - 'region/label/string', label_map_proto_file, default_value='') - - self._items_to_handlers[ - fields.InputDataFields.groundtruth_classes] = label_handler - - if load_context_features: - self._context_keys_to_features['image/context_features'] = ( - tf.VarLenFeature(dtype=tf.float32)) - self._items_to_handlers[fields.InputDataFields.context_features] = ( - slim_example_decoder.ItemHandlerCallback( - ['image/context_features', 'image/context_feature_length'], - self._reshape_context_features)) - - self._context_keys_to_features['image/context_feature_length'] = ( - tf.FixedLenFeature((), tf.int64)) - self._items_to_handlers[fields.InputDataFields.context_feature_length] = ( - slim_example_decoder.Tensor('image/context_feature_length')) - - if load_context_image_ids: - self._context_keys_to_features['image/context_features_image_id_list'] = ( - tf.VarLenFeature(dtype=tf.string)) - self._items_to_handlers[ - fields.InputDataFields.context_features_image_id_list] = ( - slim_example_decoder.Tensor( - 'image/context_features_image_id_list', - default_value='')) - - self._fully_annotated = fully_annotated - - def decode(self, tf_seq_example_string_tensor): - """Decodes serialized `tf.SequenceExample`s and returns a tensor dictionary. - - Args: - tf_seq_example_string_tensor: a string tensor holding a serialized - `tf.SequenceExample`. - - Returns: - A list of dictionaries with (at least) the following tensors: - fields.InputDataFields.source_id: a [num_frames] string tensor with a - unique ID for each frame. - fields.InputDataFields.num_groundtruth_boxes: a [num_frames] int32 tensor - specifying the number of boxes in each frame. - fields.InputDataFields.groundtruth_boxes: a [num_frames, num_boxes, 4] - float32 tensor with bounding boxes for each frame. Note that num_boxes - is the maximum boxes seen in any individual frame. Any frames with fewer - boxes are padded with 0.0. - fields.InputDataFields.groundtruth_classes: a [num_frames, num_boxes] - int32 tensor with class indices for each box in each frame. - fields.InputDataFields.groundtruth_weights: a [num_frames, num_boxes] - float32 tensor with weights of the groundtruth boxes. - fields.InputDataFields.is_annotated: a [num_frames] bool tensor specifying - whether the image was annotated or not. If False, the corresponding - entries in the groundtruth tensor will be ignored. - fields.InputDataFields.context_features - 1D float32 tensor of shape - [context_feature_length * num_context_features] - fields.InputDataFields.context_feature_length - int32 tensor specifying - the length of each feature in context_features - fields.InputDataFields.image: a [num_frames] string tensor with - the encoded images. - fields.inputDataFields.context_features_image_id_list: a 1D vector - of shape [num_context_features] containing string tensors. - """ - serialized_example = tf.reshape(tf_seq_example_string_tensor, shape=[]) - decoder = slim_example_decoder.TFSequenceExampleDecoder( - self._context_keys_to_features, self._sequence_keys_to_feature_lists, - self._items_to_handlers) - keys = decoder.list_items() - tensors = decoder.decode(serialized_example, items=keys) - tensor_dict = dict(list(zip(keys, tensors))) - tensor_dict[fields.InputDataFields.groundtruth_boxes].set_shape( - [None, None, 4]) - tensor_dict[fields.InputDataFields.num_groundtruth_boxes] = tf.cast( - tensor_dict[fields.InputDataFields.num_groundtruth_boxes], - dtype=tf.int32) - tensor_dict[fields.InputDataFields.groundtruth_classes] = tf.cast( - tensor_dict[fields.InputDataFields.groundtruth_classes], dtype=tf.int32) - tensor_dict[fields.InputDataFields.original_image_spatial_shape] = tf.cast( - tf.stack([ - tensor_dict[fields.InputDataFields.image_height], - tensor_dict[fields.InputDataFields.image_width] - ]), - dtype=tf.int32) - tensor_dict.pop(fields.InputDataFields.image_height) - tensor_dict.pop(fields.InputDataFields.image_width) - - def default_groundtruth_weights(): - """Produces weights of 1.0 for each valid box, and 0.0 otherwise.""" - num_boxes_per_frame = tensor_dict[ - fields.InputDataFields.num_groundtruth_boxes] - max_num_boxes = tf.reduce_max(num_boxes_per_frame) - num_boxes_per_frame_tiled = tf.tile( - tf.expand_dims(num_boxes_per_frame, axis=-1), - multiples=tf.stack([1, max_num_boxes])) - range_tiled = tf.tile( - tf.expand_dims(tf.range(max_num_boxes), axis=0), - multiples=tf.stack([tf.shape(num_boxes_per_frame)[0], 1])) - return tf.cast( - tf.greater(num_boxes_per_frame_tiled, range_tiled), tf.float32) - - tensor_dict[fields.InputDataFields.groundtruth_weights] = tf.cond( - tf.greater( - tf.size(tensor_dict[fields.InputDataFields.groundtruth_weights]), - 0), lambda: tensor_dict[fields.InputDataFields.groundtruth_weights], - default_groundtruth_weights) - - if self._fully_annotated: - tensor_dict[fields.InputDataFields.is_annotated] = tf.ones_like( - tensor_dict[fields.InputDataFields.num_groundtruth_boxes], - dtype=tf.bool) - else: - tensor_dict[fields.InputDataFields.is_annotated] = tf.cast( - tensor_dict[fields.InputDataFields.is_annotated], dtype=tf.bool) - - return tensor_dict - - def _reshape_context_features(self, keys_to_tensors): - """Reshape context features. - - The instance context_features are reshaped to - [num_context_features, context_feature_length] - - Args: - keys_to_tensors: a dictionary from keys to tensors. - - Returns: - A 2-D float tensor of shape [num_context_features, context_feature_length] - """ - context_feature_length = keys_to_tensors['image/context_feature_length'] - to_shape = tf.cast(tf.stack([-1, context_feature_length]), tf.int32) - context_features = keys_to_tensors['image/context_features'] - if isinstance(context_features, tf.SparseTensor): - context_features = tf.sparse_tensor_to_dense(context_features) - context_features = tf.reshape(context_features, to_shape) - return context_features diff --git a/research/object_detection/data_decoders/tf_sequence_example_decoder_test.py b/research/object_detection/data_decoders/tf_sequence_example_decoder_test.py deleted file mode 100644 index 4aa3afbe073..00000000000 --- a/research/object_detection/data_decoders/tf_sequence_example_decoder_test.py +++ /dev/null @@ -1,312 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for tf_sequence_example_decoder.py.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields as fields -from object_detection.data_decoders import tf_sequence_example_decoder -from object_detection.dataset_tools import seq_example_util -from object_detection.utils import test_case - - -class TfSequenceExampleDecoderTest(test_case.TestCase): - - def _create_label_map(self, path): - label_map_text = """ - item { - name: "dog" - id: 1 - } - item { - name: "cat" - id: 2 - } - item { - name: "panda" - id: 4 - } - """ - with tf.gfile.Open(path, 'wb') as f: - f.write(label_map_text) - - def _make_random_serialized_jpeg_images(self, num_frames, image_height, - image_width): - def graph_fn(): - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - images_list = tf.unstack(images, axis=0) - return [tf.io.encode_jpeg(image) for image in images_list] - encoded_images = self.execute(graph_fn, []) - return encoded_images - - def test_decode_sequence_example(self): - num_frames = 4 - image_height = 20 - image_width = 30 - - expected_groundtruth_boxes = [ - [[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]], - [[0.2, 0.2, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]], - [[0.0, 0.0, 1.0, 1.0], [0.1, 0.1, 0.2, 0.2]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]] - ] - expected_groundtruth_classes = [ - [-1, -1], - [-1, 1], - [1, 2], - [-1, -1] - ] - - flds = fields.InputDataFields - encoded_images = self._make_random_serialized_jpeg_images( - num_frames, image_height, image_width) - - def graph_fn(): - label_map_proto_file = os.path.join(self.get_temp_dir(), 'labelmap.pbtxt') - self._create_label_map(label_map_proto_file) - decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder( - label_map_proto_file=label_map_proto_file) - sequence_example_serialized = seq_example_util.make_sequence_example( - dataset_name='video_dataset', - video_id='video', - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_format='JPEG', - image_source_ids=[str(i) for i in range(num_frames)], - is_annotated=[[1], [1], [1], [1]], - bboxes=[ - [[0., 0., 1., 1.]], # Frame 0. - [[0.2, 0.2, 1., 1.], - [0., 0., 1., 1.]], # Frame 1. - [[0., 0., 1., 1.], # Frame 2. - [0.1, 0.1, 0.2, 0.2]], - [[]], # Frame 3. - ], - label_strings=[ - ['fox'], # Frame 0. Fox will be filtered out. - ['fox', 'dog'], # Frame 1. Fox will be filtered out. - ['dog', 'cat'], # Frame 2. - [], # Frame 3 - ]).SerializeToString() - - example_string_tensor = tf.convert_to_tensor(sequence_example_serialized) - return decoder.decode(example_string_tensor) - - tensor_dict_out = self.execute(graph_fn, []) - self.assertAllClose(expected_groundtruth_boxes, - tensor_dict_out[flds.groundtruth_boxes]) - self.assertAllEqual(expected_groundtruth_classes, - tensor_dict_out[flds.groundtruth_classes]) - - def test_decode_sequence_example_context(self): - num_frames = 4 - image_height = 20 - image_width = 30 - - expected_groundtruth_boxes = [ - [[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]], - [[0.2, 0.2, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]], - [[0.0, 0.0, 1.0, 1.0], [0.1, 0.1, 0.2, 0.2]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]] - ] - expected_groundtruth_classes = [ - [-1, -1], - [-1, 1], - [1, 2], - [-1, -1] - ] - - expected_context_features = np.array( - [[0.0, 0.1, 0.2], [0.3, 0.4, 0.5]], dtype=np.float32) - - flds = fields.InputDataFields - encoded_images = self._make_random_serialized_jpeg_images( - num_frames, image_height, image_width) - - def graph_fn(): - label_map_proto_file = os.path.join(self.get_temp_dir(), 'labelmap.pbtxt') - self._create_label_map(label_map_proto_file) - decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder( - label_map_proto_file=label_map_proto_file, - load_context_features=True) - sequence_example_serialized = seq_example_util.make_sequence_example( - dataset_name='video_dataset', - video_id='video', - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_format='JPEG', - image_source_ids=[str(i) for i in range(num_frames)], - is_annotated=[[1], [1], [1], [1]], - bboxes=[ - [[0., 0., 1., 1.]], # Frame 0. - [[0.2, 0.2, 1., 1.], - [0., 0., 1., 1.]], # Frame 1. - [[0., 0., 1., 1.], # Frame 2. - [0.1, 0.1, 0.2, 0.2]], - [[]], # Frame 3. - ], - label_strings=[ - ['fox'], # Frame 0. Fox will be filtered out. - ['fox', 'dog'], # Frame 1. Fox will be filtered out. - ['dog', 'cat'], # Frame 2. - [], # Frame 3 - ], - context_features=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5], - context_feature_length=[3], - context_features_image_id_list=[b'im_1', b'im_2'] - ).SerializeToString() - - example_string_tensor = tf.convert_to_tensor(sequence_example_serialized) - return decoder.decode(example_string_tensor) - - tensor_dict_out = self.execute(graph_fn, []) - self.assertAllClose(expected_groundtruth_boxes, - tensor_dict_out[flds.groundtruth_boxes]) - self.assertAllEqual(expected_groundtruth_classes, - tensor_dict_out[flds.groundtruth_classes]) - self.assertAllClose(expected_context_features, - tensor_dict_out[flds.context_features]) - - def test_decode_sequence_example_context_image_id_list(self): - num_frames = 4 - image_height = 20 - image_width = 30 - - expected_groundtruth_boxes = [ - [[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]], - [[0.2, 0.2, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]], - [[0.0, 0.0, 1.0, 1.0], [0.1, 0.1, 0.2, 0.2]], - [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]] - ] - expected_groundtruth_classes = [ - [-1, -1], - [-1, 1], - [1, 2], - [-1, -1] - ] - - expected_context_image_ids = [b'im_1', b'im_2'] - - flds = fields.InputDataFields - encoded_images = self._make_random_serialized_jpeg_images( - num_frames, image_height, image_width) - - def graph_fn(): - label_map_proto_file = os.path.join(self.get_temp_dir(), 'labelmap.pbtxt') - self._create_label_map(label_map_proto_file) - decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder( - label_map_proto_file=label_map_proto_file, - load_context_image_ids=True) - sequence_example_serialized = seq_example_util.make_sequence_example( - dataset_name='video_dataset', - video_id='video', - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_format='JPEG', - image_source_ids=[str(i) for i in range(num_frames)], - is_annotated=[[1], [1], [1], [1]], - bboxes=[ - [[0., 0., 1., 1.]], # Frame 0. - [[0.2, 0.2, 1., 1.], - [0., 0., 1., 1.]], # Frame 1. - [[0., 0., 1., 1.], # Frame 2. - [0.1, 0.1, 0.2, 0.2]], - [[]], # Frame 3. - ], - label_strings=[ - ['fox'], # Frame 0. Fox will be filtered out. - ['fox', 'dog'], # Frame 1. Fox will be filtered out. - ['dog', 'cat'], # Frame 2. - [], # Frame 3 - ], - context_features=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5], - context_feature_length=[3], - context_features_image_id_list=[b'im_1', b'im_2'] - ).SerializeToString() - - example_string_tensor = tf.convert_to_tensor(sequence_example_serialized) - return decoder.decode(example_string_tensor) - - tensor_dict_out = self.execute(graph_fn, []) - self.assertAllClose(expected_groundtruth_boxes, - tensor_dict_out[flds.groundtruth_boxes]) - self.assertAllEqual(expected_groundtruth_classes, - tensor_dict_out[flds.groundtruth_classes]) - self.assertAllEqual(expected_context_image_ids, - tensor_dict_out[flds.context_features_image_id_list]) - - def test_decode_sequence_example_negative_clip(self): - num_frames = 4 - image_height = 20 - image_width = 30 - - expected_groundtruth_boxes = -1 * np.ones((4, 0, 4)) - expected_groundtruth_classes = -1 * np.ones((4, 0)) - - flds = fields.InputDataFields - - encoded_images = self._make_random_serialized_jpeg_images( - num_frames, image_height, image_width) - - def graph_fn(): - sequence_example_serialized = seq_example_util.make_sequence_example( - dataset_name='video_dataset', - video_id='video', - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_format='JPEG', - image_source_ids=[str(i) for i in range(num_frames)], - bboxes=[ - [[]], - [[]], - [[]], - [[]] - ], - label_strings=[ - [], - [], - [], - [] - ]).SerializeToString() - example_string_tensor = tf.convert_to_tensor(sequence_example_serialized) - - label_map_proto_file = os.path.join(self.get_temp_dir(), 'labelmap.pbtxt') - self._create_label_map(label_map_proto_file) - decoder = tf_sequence_example_decoder.TfSequenceExampleDecoder( - label_map_proto_file=label_map_proto_file) - return decoder.decode(example_string_tensor) - - tensor_dict_out = self.execute(graph_fn, []) - self.assertAllClose(expected_groundtruth_boxes, - tensor_dict_out[flds.groundtruth_boxes]) - self.assertAllEqual(expected_groundtruth_classes, - tensor_dict_out[flds.groundtruth_classes]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/__init__.py b/research/object_detection/dataset_tools/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/dataset_tools/context_rcnn/__init__.py b/research/object_detection/dataset_tools/context_rcnn/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/dataset_tools/context_rcnn/add_context_to_examples.py b/research/object_detection/dataset_tools/context_rcnn/add_context_to_examples.py deleted file mode 100644 index 21890aa9a02..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/add_context_to_examples.py +++ /dev/null @@ -1,967 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""A Beam job to add contextual memory banks to tf.Examples. - -This tool groups images containing bounding boxes and embedded context features -by a key, either `image/location` or `image/seq_id`, and time horizon, -then uses these groups to build up a contextual memory bank from the embedded -context features from each image in the group and adds that context to the -output tf.Examples for each image in the group. - -Steps to generate a dataset with context from one with bounding boxes and -embedded context features: -1. Use object/detection/export_inference_graph.py to get a `saved_model` for - inference. The input node must accept a tf.Example proto. -2. Run this tool with `saved_model` from step 1 and a TFRecord of tf.Example - protos containing images, bounding boxes, and embedded context features. - The context features can be added to tf.Examples using - generate_embedding_data.py. - -Example Usage: --------------- -python add_context_to_examples.py \ - --input_tfrecord path/to/input_tfrecords* \ - --output_tfrecord path/to/output_tfrecords \ - --sequence_key image/location \ - --time_horizon month - -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import copy -import datetime -import io -import itertools -import json -import os -import numpy as np -import PIL.Image -import six -import tensorflow as tf - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -class ReKeyDataFn(beam.DoFn): - """Re-keys tfrecords by sequence_key. - - This Beam DoFn re-keys the tfrecords by a user-defined sequence_key - """ - - def __init__(self, sequence_key, time_horizon, - reduce_image_size, max_image_dimension): - """Initialization function. - - Args: - sequence_key: A feature name to use as a key for grouping sequences. - Must point to a key of type bytes_list - time_horizon: What length of time to use to partition the data when - building the memory banks. Options: `year`, `month`, `week`, `day `, - `hour`, `minute`, None - reduce_image_size: Whether to reduce the sizes of the stored images. - max_image_dimension: maximum dimension of reduced images - """ - self._sequence_key = sequence_key - if time_horizon is None or time_horizon in {'year', 'month', 'week', 'day', - 'hour', 'minute'}: - self._time_horizon = time_horizon - else: - raise ValueError('Time horizon not supported.') - self._reduce_image_size = reduce_image_size - self._max_image_dimension = max_image_dimension - self._session = None - self._num_examples_processed = beam.metrics.Metrics.counter( - 'data_rekey', 'num_tf_examples_processed') - self._num_images_resized = beam.metrics.Metrics.counter( - 'data_rekey', 'num_images_resized') - self._num_images_read = beam.metrics.Metrics.counter( - 'data_rekey', 'num_images_read') - self._num_images_found = beam.metrics.Metrics.counter( - 'data_rekey', 'num_images_read') - self._num_got_shape = beam.metrics.Metrics.counter( - 'data_rekey', 'num_images_got_shape') - self._num_images_found_size = beam.metrics.Metrics.counter( - 'data_rekey', 'num_images_found_size') - self._num_examples_cleared = beam.metrics.Metrics.counter( - 'data_rekey', 'num_examples_cleared') - self._num_examples_updated = beam.metrics.Metrics.counter( - 'data_rekey', 'num_examples_updated') - - def process(self, tfrecord_entry): - return self._rekey_examples(tfrecord_entry) - - def _largest_size_at_most(self, height, width, largest_side): - """Computes new shape with the largest side equal to `largest_side`. - - Args: - height: an int indicating the current height. - width: an int indicating the current width. - largest_side: A python integer indicating the size of - the largest side after resize. - Returns: - new_height: an int indicating the new height. - new_width: an int indicating the new width. - """ - - x_scale = float(largest_side) / float(width) - y_scale = float(largest_side) / float(height) - scale = min(x_scale, y_scale) - - new_width = int(width * scale) - new_height = int(height * scale) - - return new_height, new_width - - def _resize_image(self, input_example): - """Resizes the image within input_example and updates the height and width. - - Args: - input_example: A tf.Example that we want to update to contain a resized - image. - Returns: - input_example: Updated tf.Example. - """ - - original_image = copy.deepcopy( - input_example.features.feature['image/encoded'].bytes_list.value[0]) - self._num_images_read.inc(1) - - height = copy.deepcopy( - input_example.features.feature['image/height'].int64_list.value[0]) - - width = copy.deepcopy( - input_example.features.feature['image/width'].int64_list.value[0]) - - self._num_got_shape.inc(1) - - new_height, new_width = self._largest_size_at_most( - height, width, self._max_image_dimension) - - self._num_images_found_size.inc(1) - - encoded_jpg_io = io.BytesIO(original_image) - image = PIL.Image.open(encoded_jpg_io) - resized_image = image.resize((new_width, new_height)) - - with io.BytesIO() as output: - resized_image.save(output, format='JPEG') - encoded_resized_image = output.getvalue() - - self._num_images_resized.inc(1) - - del input_example.features.feature['image/encoded'].bytes_list.value[:] - del input_example.features.feature['image/height'].int64_list.value[:] - del input_example.features.feature['image/width'].int64_list.value[:] - - self._num_examples_cleared.inc(1) - - input_example.features.feature['image/encoded'].bytes_list.value.extend( - [encoded_resized_image]) - input_example.features.feature['image/height'].int64_list.value.extend( - [new_height]) - input_example.features.feature['image/width'].int64_list.value.extend( - [new_width]) - self._num_examples_updated.inc(1) - - return input_example - - def _rekey_examples(self, tfrecord_entry): - serialized_example = copy.deepcopy(tfrecord_entry) - - input_example = tf.train.Example.FromString(serialized_example) - - self._num_images_found.inc(1) - - if self._reduce_image_size: - input_example = self._resize_image(input_example) - self._num_images_resized.inc(1) - - new_key = input_example.features.feature[ - self._sequence_key].bytes_list.value[0] - - if self._time_horizon: - date_captured = datetime.datetime.strptime( - six.ensure_str(input_example.features.feature[ - 'image/date_captured'].bytes_list.value[0]), '%Y-%m-%d %H:%M:%S') - year = date_captured.year - month = date_captured.month - day = date_captured.day - week = np.floor(float(day) / float(7)) - hour = date_captured.hour - minute = date_captured.minute - - if self._time_horizon == 'year': - new_key = new_key + six.ensure_binary('/' + str(year)) - elif self._time_horizon == 'month': - new_key = new_key + six.ensure_binary( - '/' + str(year) + '/' + str(month)) - elif self._time_horizon == 'week': - new_key = new_key + six.ensure_binary( - '/' + str(year) + '/' + str(month) + '/' + str(week)) - elif self._time_horizon == 'day': - new_key = new_key + six.ensure_binary( - '/' + str(year) + '/' + str(month) + '/' + str(day)) - elif self._time_horizon == 'hour': - new_key = new_key + six.ensure_binary( - '/' + str(year) + '/' + str(month) + '/' + str(day) + '/' + ( - str(hour))) - elif self._time_horizon == 'minute': - new_key = new_key + six.ensure_binary( - '/' + str(year) + '/' + str(month) + '/' + str(day) + '/' + ( - str(hour) + '/' + str(minute))) - - self._num_examples_processed.inc(1) - - return [(new_key, input_example)] - - -class SortGroupedDataFn(beam.DoFn): - """Sorts data within a keyed group. - - This Beam DoFn sorts the grouped list of image examples by frame_num - """ - - def __init__(self, sequence_key, sorted_image_ids, - max_num_elements_in_context_features): - """Initialization function. - - Args: - sequence_key: A feature name to use as a key for grouping sequences. - Must point to a key of type bytes_list - sorted_image_ids: Whether the image ids are sortable to use as sorting - tie-breakers - max_num_elements_in_context_features: The maximum number of elements - allowed in the memory bank - """ - self._session = None - self._num_examples_processed = beam.metrics.Metrics.counter( - 'sort_group', 'num_groups_sorted') - self._too_many_elements = beam.metrics.Metrics.counter( - 'sort_group', 'too_many_elements') - self._split_elements = beam.metrics.Metrics.counter( - 'sort_group', 'split_elements') - self._sequence_key = six.ensure_binary(sequence_key) - self._sorted_image_ids = sorted_image_ids - self._max_num_elements_in_context_features = ( - max_num_elements_in_context_features) - - def process(self, grouped_entry): - return self._sort_image_examples(grouped_entry) - - def _sort_image_examples(self, grouped_entry): - key, example_collection = grouped_entry - example_list = list(example_collection) - - def get_frame_num(example): - return example.features.feature['image/seq_frame_num'].int64_list.value[0] - - def get_date_captured(example): - return datetime.datetime.strptime( - six.ensure_str( - example.features.feature[ - 'image/date_captured'].bytes_list.value[0]), - '%Y-%m-%d %H:%M:%S') - - def get_image_id(example): - return example.features.feature['image/source_id'].bytes_list.value[0] - - if self._sequence_key == six.ensure_binary('image/seq_id'): - sorting_fn = get_frame_num - elif self._sequence_key == six.ensure_binary('image/location'): - if self._sorted_image_ids: - sorting_fn = get_image_id - else: - sorting_fn = get_date_captured - - sorted_example_list = sorted(example_list, key=sorting_fn) - - num_embeddings = 0 - for example in sorted_example_list: - num_embeddings += example.features.feature[ - 'image/embedding_count'].int64_list.value[0] - - self._num_examples_processed.inc(1) - - # To handle cases where there are more context embeddings within - # the time horizon than the specified maximum, we split the context group - # into subsets sequentially in time, with each subset having the maximum - # number of context embeddings except the final one, which holds the - # remainder. - if num_embeddings > self._max_num_elements_in_context_features: - leftovers = sorted_example_list - output_list = [] - count = 0 - self._too_many_elements.inc(1) - num_embeddings = 0 - max_idx = 0 - for idx, example in enumerate(leftovers): - num_embeddings += example.features.feature[ - 'image/embedding_count'].int64_list.value[0] - if num_embeddings <= self._max_num_elements_in_context_features: - max_idx = idx - while num_embeddings > self._max_num_elements_in_context_features: - self._split_elements.inc(1) - new_key = key + six.ensure_binary('_' + str(count)) - new_list = leftovers[:max_idx] - output_list.append((new_key, new_list)) - leftovers = leftovers[max_idx:] - count += 1 - num_embeddings = 0 - max_idx = 0 - for idx, example in enumerate(leftovers): - num_embeddings += example.features.feature[ - 'image/embedding_count'].int64_list.value[0] - if num_embeddings <= self._max_num_elements_in_context_features: - max_idx = idx - new_key = key + six.ensure_binary('_' + str(count)) - output_list.append((new_key, leftovers)) - else: - output_list = [(key, sorted_example_list)] - - return output_list - - -def get_sliding_window(example_list, max_clip_length, stride_length): - """Yields a sliding window over data from example_list. - - Sliding window has width max_clip_len (n) and stride stride_len (m). - s -> (s0,s1,...s[n-1]), (s[m],s[m+1],...,s[m+n]), ... - - Args: - example_list: A list of examples. - max_clip_length: The maximum length of each clip. - stride_length: The stride between each clip. - - Yields: - A list of lists of examples, each with length <= max_clip_length - """ - - # check if the list is too short to slide over - if len(example_list) < max_clip_length: - yield example_list - else: - starting_values = [i*stride_length for i in - range(len(example_list)) if - len(example_list) > i*stride_length] - for start in starting_values: - result = tuple(itertools.islice(example_list, start, - min(start + max_clip_length, - len(example_list)))) - yield result - - -class GenerateContextFn(beam.DoFn): - """Generates context data for camera trap images. - - This Beam DoFn builds up contextual memory banks from groups of images and - stores them in the output tf.Example or tf.Sequence_example for each image. - """ - - def __init__(self, sequence_key, add_context_features, image_ids_to_keep, - keep_context_features_image_id_list=False, - subsample_context_features_rate=0, - keep_only_positives=False, - context_features_score_threshold=0.7, - keep_only_positives_gt=False, - max_num_elements_in_context_features=5000, - pad_context_features=False, - output_type='tf_example', max_clip_length=None, - context_feature_length=2057): - """Initialization function. - - Args: - sequence_key: A feature name to use as a key for grouping sequences. - add_context_features: Whether to keep and store the contextual memory - bank. - image_ids_to_keep: A list of image ids to save, to use to build data - subsets for evaluation. - keep_context_features_image_id_list: Whether to save an ordered list of - the ids of the images in the contextual memory bank. - subsample_context_features_rate: What rate to subsample images for the - contextual memory bank. - keep_only_positives: Whether to only keep high scoring - (>context_features_score_threshold) features in the contextual memory - bank. - context_features_score_threshold: What threshold to use for keeping - features. - keep_only_positives_gt: Whether to only keep features from images that - contain objects based on the ground truth (for training). - max_num_elements_in_context_features: the maximum number of elements in - the memory bank - pad_context_features: Whether to pad the context features to a fixed size. - output_type: What type of output, tf_example of tf_sequence_example - max_clip_length: The maximum length of a sequence example, before - splitting into multiple - context_feature_length: The length of the context feature embeddings - stored in the input data. - """ - self._session = None - self._num_examples_processed = beam.metrics.Metrics.counter( - 'sequence_data_generation', 'num_seq_examples_processed') - self._num_keys_processed = beam.metrics.Metrics.counter( - 'sequence_data_generation', 'num_keys_processed') - self._sequence_key = sequence_key - self._add_context_features = add_context_features - self._pad_context_features = pad_context_features - self._output_type = output_type - self._max_clip_length = max_clip_length - if six.ensure_str(image_ids_to_keep) == 'All': - self._image_ids_to_keep = None - else: - with tf.io.gfile.GFile(image_ids_to_keep) as f: - self._image_ids_to_keep = json.load(f) - self._keep_context_features_image_id_list = ( - keep_context_features_image_id_list) - self._subsample_context_features_rate = subsample_context_features_rate - self._keep_only_positives = keep_only_positives - self._keep_only_positives_gt = keep_only_positives_gt - self._context_features_score_threshold = context_features_score_threshold - self._max_num_elements_in_context_features = ( - max_num_elements_in_context_features) - self._context_feature_length = context_feature_length - - self._images_kept = beam.metrics.Metrics.counter( - 'sequence_data_generation', 'images_kept') - self._images_loaded = beam.metrics.Metrics.counter( - 'sequence_data_generation', 'images_loaded') - - def process(self, grouped_entry): - return self._add_context_to_example(copy.deepcopy(grouped_entry)) - - def _build_context_features(self, example_list): - context_features = [] - context_features_image_id_list = [] - count = 0 - example_embedding = [] - - for idx, example in enumerate(example_list): - if self._subsample_context_features_rate > 0: - if (idx % self._subsample_context_features_rate) != 0: - example.features.feature[ - 'context_features_idx'].int64_list.value.append( - self._max_num_elements_in_context_features + 1) - continue - if self._keep_only_positives: - if example.features.feature[ - 'image/embedding_score' - ].float_list.value[0] < self._context_features_score_threshold: - example.features.feature[ - 'context_features_idx'].int64_list.value.append( - self._max_num_elements_in_context_features + 1) - continue - if self._keep_only_positives_gt: - if len(example.features.feature[ - 'image/object/bbox/xmin' - ].float_list.value) < 1: - example.features.feature[ - 'context_features_idx'].int64_list.value.append( - self._max_num_elements_in_context_features + 1) - continue - - example_embedding = list(example.features.feature[ - 'image/embedding'].float_list.value) - context_features.extend(example_embedding) - num_embeddings = example.features.feature[ - 'image/embedding_count'].int64_list.value[0] - example_image_id = example.features.feature[ - 'image/source_id'].bytes_list.value[0] - for _ in range(num_embeddings): - example.features.feature[ - 'context_features_idx'].int64_list.value.append(count) - count += 1 - context_features_image_id_list.append(example_image_id) - - if not example_embedding: - example_embedding.append(np.zeros(self._context_feature_length)) - - feature_length = self._context_feature_length - - # If the example_list is not empty and image/embedding_length is in the - # featture dict, feature_length will be assigned to that. Otherwise, it will - # be kept as default. - if example_list and ( - 'image/embedding_length' in example_list[0].features.feature): - feature_length = example_list[0].features.feature[ - 'image/embedding_length'].int64_list.value[0] - - if self._pad_context_features: - while len(context_features_image_id_list) < ( - self._max_num_elements_in_context_features): - context_features_image_id_list.append('') - - return context_features, feature_length, context_features_image_id_list - - def _add_context_to_example(self, grouped_entry): - key, example_collection = grouped_entry - list_of_examples = [] - - example_list = list(example_collection) - - if self._add_context_features: - context_features, feature_length, context_features_image_id_list = ( - self._build_context_features(example_list)) - - if self._image_ids_to_keep is not None: - new_example_list = [] - for example in example_list: - im_id = example.features.feature['image/source_id'].bytes_list.value[0] - self._images_loaded.inc(1) - if six.ensure_str(im_id) in self._image_ids_to_keep: - self._images_kept.inc(1) - new_example_list.append(example) - if new_example_list: - example_list = new_example_list - else: - return [] - - if self._output_type == 'tf_sequence_example': - if self._max_clip_length is not None: - # For now, no overlap - clips = get_sliding_window( - example_list, self._max_clip_length, self._max_clip_length) - else: - clips = [example_list] - - for clip_num, clip_list in enumerate(clips): - # initialize sequence example - seq_example = tf.train.SequenceExample() - video_id = six.ensure_str(key)+'_'+ str(clip_num) - seq_example.context.feature['clip/media_id'].bytes_list.value.append( - video_id.encode('utf8')) - seq_example.context.feature['clip/frames'].int64_list.value.append( - len(clip_list)) - - seq_example.context.feature[ - 'clip/start/timestamp'].int64_list.value.append(0) - seq_example.context.feature[ - 'clip/end/timestamp'].int64_list.value.append(len(clip_list)) - seq_example.context.feature['image/format'].bytes_list.value.append( - six.ensure_binary('JPG')) - seq_example.context.feature['image/channels'].int64_list.value.append(3) - context_example = clip_list[0] - seq_example.context.feature['image/height'].int64_list.value.append( - context_example.features.feature[ - 'image/height'].int64_list.value[0]) - seq_example.context.feature['image/width'].int64_list.value.append( - context_example.features.feature['image/width'].int64_list.value[0]) - - seq_example.context.feature[ - 'image/context_feature_length'].int64_list.value.append( - feature_length) - seq_example.context.feature[ - 'image/context_features'].float_list.value.extend( - context_features) - if self._keep_context_features_image_id_list: - seq_example.context.feature[ - 'image/context_features_image_id_list'].bytes_list.value.extend( - context_features_image_id_list) - - encoded_image_list = seq_example.feature_lists.feature_list[ - 'image/encoded'] - timestamps_list = seq_example.feature_lists.feature_list[ - 'image/timestamp'] - context_features_idx_list = seq_example.feature_lists.feature_list[ - 'image/context_features_idx'] - date_captured_list = seq_example.feature_lists.feature_list[ - 'image/date_captured'] - unix_time_list = seq_example.feature_lists.feature_list[ - 'image/unix_time'] - location_list = seq_example.feature_lists.feature_list['image/location'] - image_ids_list = seq_example.feature_lists.feature_list[ - 'image/source_id'] - gt_xmin_list = seq_example.feature_lists.feature_list[ - 'region/bbox/xmin'] - gt_xmax_list = seq_example.feature_lists.feature_list[ - 'region/bbox/xmax'] - gt_ymin_list = seq_example.feature_lists.feature_list[ - 'region/bbox/ymin'] - gt_ymax_list = seq_example.feature_lists.feature_list[ - 'region/bbox/ymax'] - gt_type_list = seq_example.feature_lists.feature_list[ - 'region/label/index'] - gt_type_string_list = seq_example.feature_lists.feature_list[ - 'region/label/string'] - gt_is_annotated_list = seq_example.feature_lists.feature_list[ - 'region/is_annotated'] - - for idx, example in enumerate(clip_list): - - encoded_image = encoded_image_list.feature.add() - encoded_image.bytes_list.value.extend( - example.features.feature['image/encoded'].bytes_list.value) - - image_id = image_ids_list.feature.add() - image_id.bytes_list.value.append( - example.features.feature['image/source_id'].bytes_list.value[0]) - - timestamp = timestamps_list.feature.add() - # Timestamp is currently order in the list. - timestamp.int64_list.value.extend([idx]) - - context_features_idx = context_features_idx_list.feature.add() - context_features_idx.int64_list.value.extend( - example.features.feature['context_features_idx'].int64_list.value) - - date_captured = date_captured_list.feature.add() - date_captured.bytes_list.value.extend( - example.features.feature['image/date_captured'].bytes_list.value) - unix_time = unix_time_list.feature.add() - unix_time.float_list.value.extend( - example.features.feature['image/unix_time'].float_list.value) - location = location_list.feature.add() - location.bytes_list.value.extend( - example.features.feature['image/location'].bytes_list.value) - - gt_xmin = gt_xmin_list.feature.add() - gt_xmax = gt_xmax_list.feature.add() - gt_ymin = gt_ymin_list.feature.add() - gt_ymax = gt_ymax_list.feature.add() - gt_type = gt_type_list.feature.add() - gt_type_str = gt_type_string_list.feature.add() - - gt_is_annotated = gt_is_annotated_list.feature.add() - gt_is_annotated.int64_list.value.append(1) - - gt_xmin.float_list.value.extend( - example.features.feature[ - 'image/object/bbox/xmin'].float_list.value) - gt_xmax.float_list.value.extend( - example.features.feature[ - 'image/object/bbox/xmax'].float_list.value) - gt_ymin.float_list.value.extend( - example.features.feature[ - 'image/object/bbox/ymin'].float_list.value) - gt_ymax.float_list.value.extend( - example.features.feature[ - 'image/object/bbox/ymax'].float_list.value) - - gt_type.int64_list.value.extend( - example.features.feature[ - 'image/object/class/label'].int64_list.value) - gt_type_str.bytes_list.value.extend( - example.features.feature[ - 'image/object/class/text'].bytes_list.value) - - self._num_examples_processed.inc(1) - list_of_examples.append(seq_example) - - elif self._output_type == 'tf_example': - - for example in example_list: - im_id = example.features.feature['image/source_id'].bytes_list.value[0] - - if self._add_context_features: - example.features.feature[ - 'image/context_features'].float_list.value.extend( - context_features) - example.features.feature[ - 'image/context_feature_length'].int64_list.value.append( - feature_length) - - if self._keep_context_features_image_id_list: - example.features.feature[ - 'image/context_features_image_id_list'].bytes_list.value.extend( - context_features_image_id_list) - - self._num_examples_processed.inc(1) - list_of_examples.append(example) - - return list_of_examples - - -def construct_pipeline(pipeline, - input_tfrecord, - output_tfrecord, - sequence_key, - time_horizon=None, - subsample_context_features_rate=0, - reduce_image_size=True, - max_image_dimension=1024, - add_context_features=True, - sorted_image_ids=True, - image_ids_to_keep='All', - keep_context_features_image_id_list=False, - keep_only_positives=False, - context_features_score_threshold=0.7, - keep_only_positives_gt=False, - max_num_elements_in_context_features=5000, - num_shards=0, - output_type='tf_example', - max_clip_length=None, - context_feature_length=2057): - """Returns a beam pipeline to run object detection inference. - - Args: - pipeline: Initialized beam pipeline. - input_tfrecord: An TFRecord of tf.train.Example protos containing images. - output_tfrecord: An TFRecord of tf.train.Example protos that contain images - in the input TFRecord and the detections from the model. - sequence_key: A feature name to use as a key for grouping sequences. - time_horizon: What length of time to use to partition the data when building - the memory banks. Options: `year`, `month`, `week`, `day `, `hour`, - `minute`, None. - subsample_context_features_rate: What rate to subsample images for the - contextual memory bank. - reduce_image_size: Whether to reduce the size of the stored images. - max_image_dimension: The maximum image dimension to use for resizing. - add_context_features: Whether to keep and store the contextual memory bank. - sorted_image_ids: Whether the image ids are sortable, and can be used as - datetime tie-breakers when building memory banks. - image_ids_to_keep: A list of image ids to save, to use to build data subsets - for evaluation. - keep_context_features_image_id_list: Whether to save an ordered list of the - ids of the images in the contextual memory bank. - keep_only_positives: Whether to only keep high scoring - (>context_features_score_threshold) features in the contextual memory - bank. - context_features_score_threshold: What threshold to use for keeping - features. - keep_only_positives_gt: Whether to only keep features from images that - contain objects based on the ground truth (for training). - max_num_elements_in_context_features: the maximum number of elements in the - memory bank - num_shards: The number of output shards. - output_type: What type of output, tf_example of tf_sequence_example - max_clip_length: The maximum length of a sequence example, before - splitting into multiple - context_feature_length: The length of the context feature embeddings stored - in the input data. - """ - if output_type == 'tf_example': - coder = beam.coders.ProtoCoder(tf.train.Example) - elif output_type == 'tf_sequence_example': - coder = beam.coders.ProtoCoder(tf.train.SequenceExample) - else: - raise ValueError('Unsupported output type.') - input_collection = ( - pipeline | 'ReadInputTFRecord' >> beam.io.tfrecordio.ReadFromTFRecord( - input_tfrecord, - coder=beam.coders.BytesCoder())) - rekey_collection = input_collection | 'RekeyExamples' >> beam.ParDo( - ReKeyDataFn(sequence_key, time_horizon, - reduce_image_size, max_image_dimension)) - grouped_collection = ( - rekey_collection | 'GroupBySequenceKey' >> beam.GroupByKey()) - grouped_collection = ( - grouped_collection | 'ReshuffleGroups' >> beam.Reshuffle()) - ordered_collection = ( - grouped_collection | 'OrderByFrameNumber' >> beam.ParDo( - SortGroupedDataFn(sequence_key, sorted_image_ids, - max_num_elements_in_context_features))) - ordered_collection = ( - ordered_collection | 'ReshuffleSortedGroups' >> beam.Reshuffle()) - output_collection = ( - ordered_collection | 'AddContextToExamples' >> beam.ParDo( - GenerateContextFn( - sequence_key, add_context_features, image_ids_to_keep, - keep_context_features_image_id_list=( - keep_context_features_image_id_list), - subsample_context_features_rate=subsample_context_features_rate, - keep_only_positives=keep_only_positives, - keep_only_positives_gt=keep_only_positives_gt, - context_features_score_threshold=( - context_features_score_threshold), - max_num_elements_in_context_features=( - max_num_elements_in_context_features), - output_type=output_type, - max_clip_length=max_clip_length, - context_feature_length=context_feature_length))) - - output_collection = ( - output_collection | 'ReshuffleExamples' >> beam.Reshuffle()) - _ = output_collection | 'WritetoDisk' >> beam.io.tfrecordio.WriteToTFRecord( - output_tfrecord, - num_shards=num_shards, - coder=coder) - - -def parse_args(argv): - """Command-line argument parser. - - Args: - argv: command line arguments - Returns: - beam_args: Arguments for the beam pipeline. - pipeline_args: Arguments for the pipeline options, such as runner type. - """ - parser = argparse.ArgumentParser() - parser.add_argument( - '--input_tfrecord', - dest='input_tfrecord', - required=True, - help='TFRecord containing images in tf.Example format for object ' - 'detection, with bounding boxes and contextual feature embeddings.') - parser.add_argument( - '--output_tfrecord', - dest='output_tfrecord', - required=True, - help='TFRecord containing images in tf.Example format, with added ' - 'contextual memory banks.') - parser.add_argument( - '--sequence_key', - dest='sequence_key', - default='image/location', - help='Key to use when grouping sequences: so far supports `image/seq_id` ' - 'and `image/location`.') - parser.add_argument( - '--context_feature_length', - dest='context_feature_length', - default=2057, - help='The length of the context feature embeddings stored in the input ' - 'data.') - parser.add_argument( - '--time_horizon', - dest='time_horizon', - default=None, - help='What time horizon to use when splitting the data, if any. Options ' - 'are: `year`, `month`, `week`, `day `, `hour`, `minute`, `None`.') - parser.add_argument( - '--subsample_context_features_rate', - dest='subsample_context_features_rate', - default=0, - help='Whether to subsample the context_features, and if so how many to ' - 'sample. If the rate is set to X, it will sample context from 1 out of ' - 'every X images. Default is sampling from every image, which is X=0.') - parser.add_argument( - '--reduce_image_size', - dest='reduce_image_size', - default=True, - help='downsamples images to have longest side max_image_dimension, ' - 'maintaining aspect ratio') - parser.add_argument( - '--max_image_dimension', - dest='max_image_dimension', - default=1024, - help='Sets max image dimension for resizing.') - parser.add_argument( - '--add_context_features', - dest='add_context_features', - default=True, - help='Adds a memory bank of embeddings to each clip') - parser.add_argument( - '--sorted_image_ids', - dest='sorted_image_ids', - default=True, - help='Whether the image source_ids are sortable to deal with ' - 'date_captured tie-breaks.') - parser.add_argument( - '--image_ids_to_keep', - dest='image_ids_to_keep', - default='All', - help='Path to .json list of image ids to keep, used for ground truth ' - 'eval creation.') - parser.add_argument( - '--keep_context_features_image_id_list', - dest='keep_context_features_image_id_list', - default=False, - help='Whether or not to keep a list of the image_ids corresponding to ' - 'the memory bank.') - parser.add_argument( - '--keep_only_positives', - dest='keep_only_positives', - default=False, - help='Whether or not to keep only positive boxes based on score.') - parser.add_argument( - '--context_features_score_threshold', - dest='context_features_score_threshold', - default=0.7, - help='What score threshold to use for boxes in context_features, when ' - '`keep_only_positives` is set to `True`.') - parser.add_argument( - '--keep_only_positives_gt', - dest='keep_only_positives_gt', - default=False, - help='Whether or not to keep only positive boxes based on gt class.') - parser.add_argument( - '--max_num_elements_in_context_features', - dest='max_num_elements_in_context_features', - default=2000, - help='Sets max number of context feature elements per memory bank. ' - 'If the number of images in the context group is greater than ' - '`max_num_elements_in_context_features`, the context group will be split.' - ) - parser.add_argument( - '--output_type', - dest='output_type', - default='tf_example', - help='Output type, one of `tf_example`, `tf_sequence_example`.') - parser.add_argument( - '--max_clip_length', - dest='max_clip_length', - default=None, - help='Max length for sequence example outputs.') - parser.add_argument( - '--num_shards', - dest='num_shards', - default=0, - help='Number of output shards.') - beam_args, pipeline_args = parser.parse_known_args(argv) - return beam_args, pipeline_args - - -def main(argv=None, save_main_session=True): - """Runs the Beam pipeline that performs inference. - - Args: - argv: Command line arguments. - save_main_session: Whether to save the main session. - """ - args, pipeline_args = parse_args(argv) - - pipeline_options = beam.options.pipeline_options.PipelineOptions( - pipeline_args) - pipeline_options.view_as( - beam.options.pipeline_options.SetupOptions).save_main_session = ( - save_main_session) - - dirname = os.path.dirname(args.output_tfrecord) - tf.io.gfile.makedirs(dirname) - - p = beam.Pipeline(options=pipeline_options) - - construct_pipeline( - p, - args.input_tfrecord, - args.output_tfrecord, - args.sequence_key, - args.time_horizon, - args.subsample_context_features_rate, - args.reduce_image_size, - args.max_image_dimension, - args.add_context_features, - args.sorted_image_ids, - args.image_ids_to_keep, - args.keep_context_features_image_id_list, - args.keep_only_positives, - args.context_features_score_threshold, - args.keep_only_positives_gt, - args.max_num_elements_in_context_features, - args.num_shards, - args.output_type, - args.max_clip_length, - args.context_feature_length) - - p.run() - - -if __name__ == '__main__': - main() diff --git a/research/object_detection/dataset_tools/context_rcnn/add_context_to_examples_tf2_test.py b/research/object_detection/dataset_tools/context_rcnn/add_context_to_examples_tf2_test.py deleted file mode 100644 index 61020b008bc..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/add_context_to_examples_tf2_test.py +++ /dev/null @@ -1,398 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for add_context_to_examples.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import contextlib -import datetime -import os -import tempfile -import unittest - -import numpy as np -import six -import tensorflow as tf - -from object_detection.utils import tf_version - -if tf_version.is_tf2(): - from object_detection.dataset_tools.context_rcnn import add_context_to_examples # pylint:disable=g-import-not-at-top - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -@contextlib.contextmanager -def InMemoryTFRecord(entries): - temp = tempfile.NamedTemporaryFile(delete=False) - filename = temp.name - try: - with tf.io.TFRecordWriter(filename) as writer: - for value in entries: - writer.write(value) - yield filename - finally: - os.unlink(temp.name) - - -def BytesFeature(value): - return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) - - -def BytesListFeature(value): - return tf.train.Feature(bytes_list=tf.train.BytesList(value=value)) - - -def Int64Feature(value): - return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) - - -def Int64ListFeature(value): - return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) - - -def FloatListFeature(value): - return tf.train.Feature(float_list=tf.train.FloatList(value=value)) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class GenerateContextDataTest(tf.test.TestCase): - - def _create_first_tf_example(self): - encoded_image = tf.io.encode_jpeg( - tf.constant(np.ones((4, 4, 3)).astype(np.uint8))).numpy() - - example = tf.train.Example(features=tf.train.Features(feature={ - 'image/encoded': BytesFeature(encoded_image), - 'image/source_id': BytesFeature(six.ensure_binary('image_id_1')), - 'image/height': Int64Feature(4), - 'image/width': Int64Feature(4), - 'image/object/class/label': Int64ListFeature([5, 5]), - 'image/object/class/text': BytesListFeature([six.ensure_binary('hyena'), - six.ensure_binary('hyena') - ]), - 'image/object/bbox/xmin': FloatListFeature([0.0, 0.1]), - 'image/object/bbox/xmax': FloatListFeature([0.2, 0.3]), - 'image/object/bbox/ymin': FloatListFeature([0.4, 0.5]), - 'image/object/bbox/ymax': FloatListFeature([0.6, 0.7]), - 'image/seq_id': BytesFeature(six.ensure_binary('01')), - 'image/seq_num_frames': Int64Feature(2), - 'image/seq_frame_num': Int64Feature(0), - 'image/date_captured': BytesFeature( - six.ensure_binary(str(datetime.datetime(2020, 1, 1, 1, 0, 0)))), - 'image/embedding': FloatListFeature([0.1, 0.2, 0.3]), - 'image/embedding_score': FloatListFeature([0.9]), - 'image/embedding_length': Int64Feature(3), - 'image/embedding_count': Int64Feature(1) - - })) - - return example.SerializeToString() - - def _create_second_tf_example(self): - encoded_image = tf.io.encode_jpeg( - tf.constant(np.ones((4, 4, 3)).astype(np.uint8))).numpy() - - example = tf.train.Example(features=tf.train.Features(feature={ - 'image/encoded': BytesFeature(encoded_image), - 'image/source_id': BytesFeature(six.ensure_binary('image_id_2')), - 'image/height': Int64Feature(4), - 'image/width': Int64Feature(4), - 'image/object/class/label': Int64ListFeature([5]), - 'image/object/class/text': BytesListFeature([six.ensure_binary('hyena') - ]), - 'image/object/bbox/xmin': FloatListFeature([0.0]), - 'image/object/bbox/xmax': FloatListFeature([0.1]), - 'image/object/bbox/ymin': FloatListFeature([0.2]), - 'image/object/bbox/ymax': FloatListFeature([0.3]), - 'image/seq_id': BytesFeature(six.ensure_binary('01')), - 'image/seq_num_frames': Int64Feature(2), - 'image/seq_frame_num': Int64Feature(1), - 'image/date_captured': BytesFeature( - six.ensure_binary(str(datetime.datetime(2020, 1, 1, 1, 1, 0)))), - 'image/embedding': FloatListFeature([0.4, 0.5, 0.6]), - 'image/embedding_score': FloatListFeature([0.9]), - 'image/embedding_length': Int64Feature(3), - 'image/embedding_count': Int64Feature(1) - })) - - return example.SerializeToString() - - def assert_expected_examples(self, tf_example_list): - self.assertAllEqual( - {tf_example.features.feature['image/source_id'].bytes_list.value[0] - for tf_example in tf_example_list}, - {six.ensure_binary('image_id_1'), six.ensure_binary('image_id_2')}) - self.assertAllClose( - tf_example_list[0].features.feature[ - 'image/context_features'].float_list.value, - [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]) - self.assertAllClose( - tf_example_list[1].features.feature[ - 'image/context_features'].float_list.value, - [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]) - - def assert_expected_sequence_example(self, tf_sequence_example_list): - tf_sequence_example = tf_sequence_example_list[0] - num_frames = 2 - - self.assertAllEqual( - tf_sequence_example.context.feature[ - 'clip/media_id'].bytes_list.value[0], six.ensure_binary( - '01_0')) - self.assertAllClose( - tf_sequence_example.context.feature[ - 'image/context_features'].float_list.value, - [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]) - - seq_feature_dict = tf_sequence_example.feature_lists.feature_list - - self.assertLen( - seq_feature_dict['image/encoded'].feature[:], - num_frames) - actual_timestamps = [ - feature.int64_list.value[0] for feature - in seq_feature_dict['image/timestamp'].feature] - timestamps = [0, 1] - self.assertAllEqual(timestamps, actual_timestamps) - - # First image. - self.assertAllClose( - [0.4, 0.5], - seq_feature_dict['region/bbox/ymin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.0, 0.1], - seq_feature_dict['region/bbox/xmin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.6, 0.7], - seq_feature_dict['region/bbox/ymax'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.2, 0.3], - seq_feature_dict['region/bbox/xmax'].feature[0].float_list.value[:]) - self.assertAllEqual( - [six.ensure_binary('hyena'), six.ensure_binary('hyena')], - seq_feature_dict['region/label/string'].feature[0].bytes_list.value[:]) - - # Second example. - self.assertAllClose( - [0.2], - seq_feature_dict['region/bbox/ymin'].feature[1].float_list.value[:]) - self.assertAllClose( - [0.0], - seq_feature_dict['region/bbox/xmin'].feature[1].float_list.value[:]) - self.assertAllClose( - [0.3], - seq_feature_dict['region/bbox/ymax'].feature[1].float_list.value[:]) - self.assertAllClose( - [0.1], - seq_feature_dict['region/bbox/xmax'].feature[1].float_list.value[:]) - self.assertAllEqual( - [six.ensure_binary('hyena')], - seq_feature_dict['region/label/string'].feature[1].bytes_list.value[:]) - - def assert_expected_key(self, key): - self.assertAllEqual(key, b'01') - - def assert_sorted(self, example_collection): - example_list = list(example_collection) - counter = 0 - for example in example_list: - frame_num = example.features.feature[ - 'image/seq_frame_num'].int64_list.value[0] - self.assertGreaterEqual(frame_num, counter) - counter = frame_num - - def assert_context(self, example_collection): - example_list = list(example_collection) - for example in example_list: - context = example.features.feature[ - 'image/context_features'].float_list.value - self.assertAllClose([0.1, 0.2, 0.3, 0.4, 0.5, 0.6], context) - - def assert_resized(self, example): - width = example.features.feature['image/width'].int64_list.value[0] - self.assertAllEqual(width, 2) - height = example.features.feature['image/height'].int64_list.value[0] - self.assertAllEqual(height, 2) - - def assert_size(self, example): - width = example.features.feature['image/width'].int64_list.value[0] - self.assertAllEqual(width, 4) - height = example.features.feature['image/height'].int64_list.value[0] - self.assertAllEqual(height, 4) - - def test_sliding_window(self): - example_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] - max_clip_length = 3 - stride_length = 3 - out_list = [list(i) for i in add_context_to_examples.get_sliding_window( - example_list, max_clip_length, stride_length)] - self.assertAllEqual(out_list, [['a', 'b', 'c'], - ['d', 'e', 'f'], - ['g']]) - - def test_rekey_data_fn(self): - sequence_key = 'image/seq_id' - time_horizon = None - reduce_image_size = False - max_dim = None - - rekey_fn = add_context_to_examples.ReKeyDataFn( - sequence_key, time_horizon, - reduce_image_size, max_dim) - output = rekey_fn.process(self._create_first_tf_example()) - - self.assert_expected_key(output[0][0]) - self.assert_size(output[0][1]) - - def test_rekey_data_fn_w_resize(self): - sequence_key = 'image/seq_id' - time_horizon = None - reduce_image_size = True - max_dim = 2 - - rekey_fn = add_context_to_examples.ReKeyDataFn( - sequence_key, time_horizon, - reduce_image_size, max_dim) - output = rekey_fn.process(self._create_first_tf_example()) - - self.assert_expected_key(output[0][0]) - self.assert_resized(output[0][1]) - - def test_sort_fn(self): - sequence_key = 'image/seq_id' - sorted_image_ids = False - max_num_elements_in_context_features = 10 - sort_fn = add_context_to_examples.SortGroupedDataFn( - sequence_key, sorted_image_ids, max_num_elements_in_context_features) - output = sort_fn.process( - ('dummy_key', [tf.train.Example.FromString( - self._create_second_tf_example()), - tf.train.Example.FromString( - self._create_first_tf_example())])) - - self.assert_sorted(output[0][1]) - - def test_add_context_fn(self): - sequence_key = 'image/seq_id' - add_context_features = True - image_ids_to_keep = 'All' - context_fn = add_context_to_examples.GenerateContextFn( - sequence_key, add_context_features, image_ids_to_keep) - output = context_fn.process( - ('dummy_key', [tf.train.Example.FromString( - self._create_first_tf_example()), - tf.train.Example.FromString( - self._create_second_tf_example())])) - - self.assertEqual(len(output), 2) - self.assert_context(output) - - def test_add_context_fn_output_sequence_example(self): - sequence_key = 'image/seq_id' - add_context_features = True - image_ids_to_keep = 'All' - context_fn = add_context_to_examples.GenerateContextFn( - sequence_key, add_context_features, image_ids_to_keep, - output_type='tf_sequence_example') - output = context_fn.process( - ('01', - [tf.train.Example.FromString(self._create_first_tf_example()), - tf.train.Example.FromString(self._create_second_tf_example())])) - - self.assertEqual(len(output), 1) - self.assert_expected_sequence_example(output) - - def test_add_context_fn_output_sequence_example_cliplen(self): - sequence_key = 'image/seq_id' - add_context_features = True - image_ids_to_keep = 'All' - context_fn = add_context_to_examples.GenerateContextFn( - sequence_key, add_context_features, image_ids_to_keep, - output_type='tf_sequence_example', max_clip_length=1) - output = context_fn.process( - ('01', - [tf.train.Example.FromString(self._create_first_tf_example()), - tf.train.Example.FromString(self._create_second_tf_example())])) - self.assertEqual(len(output), 2) - - def test_beam_pipeline(self): - with InMemoryTFRecord( - [self._create_first_tf_example(), - self._create_second_tf_example()]) as input_tfrecord: - temp_dir = tempfile.mkdtemp(dir=os.environ.get('TEST_TMPDIR')) - output_tfrecord = os.path.join(temp_dir, 'output_tfrecord') - sequence_key = six.ensure_binary('image/seq_id') - max_num_elements = 10 - num_shards = 1 - pipeline_options = beam.options.pipeline_options.PipelineOptions( - runner='DirectRunner') - p = beam.Pipeline(options=pipeline_options) - add_context_to_examples.construct_pipeline( - p, - input_tfrecord, - output_tfrecord, - sequence_key, - max_num_elements_in_context_features=max_num_elements, - num_shards=num_shards) - p.run() - filenames = tf.io.gfile.glob(output_tfrecord + '-?????-of-?????') - actual_output = [] - record_iterator = tf.data.TFRecordDataset( - tf.convert_to_tensor(filenames)).as_numpy_iterator() - for record in record_iterator: - actual_output.append(record) - self.assertEqual(len(actual_output), 2) - self.assert_expected_examples([tf.train.Example.FromString( - tf_example) for tf_example in actual_output]) - - def test_beam_pipeline_sequence_example(self): - with InMemoryTFRecord( - [self._create_first_tf_example(), - self._create_second_tf_example()]) as input_tfrecord: - temp_dir = tempfile.mkdtemp(dir=os.environ.get('TEST_TMPDIR')) - output_tfrecord = os.path.join(temp_dir, 'output_tfrecord') - sequence_key = six.ensure_binary('image/seq_id') - max_num_elements = 10 - num_shards = 1 - pipeline_options = beam.options.pipeline_options.PipelineOptions( - runner='DirectRunner') - p = beam.Pipeline(options=pipeline_options) - add_context_to_examples.construct_pipeline( - p, - input_tfrecord, - output_tfrecord, - sequence_key, - max_num_elements_in_context_features=max_num_elements, - num_shards=num_shards, - output_type='tf_sequence_example') - p.run() - filenames = tf.io.gfile.glob(output_tfrecord + '-?????-of-?????') - actual_output = [] - record_iterator = tf.data.TFRecordDataset( - tf.convert_to_tensor(filenames)).as_numpy_iterator() - for record in record_iterator: - actual_output.append(record) - self.assertEqual(len(actual_output), 1) - self.assert_expected_sequence_example( - [tf.train.SequenceExample.FromString( - tf_example) for tf_example in actual_output]) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/context_rcnn/create_cococameratraps_tfexample_main.py b/research/object_detection/dataset_tools/context_rcnn/create_cococameratraps_tfexample_main.py deleted file mode 100644 index dbf3cad0eac..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/create_cococameratraps_tfexample_main.py +++ /dev/null @@ -1,334 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Beam pipeline to create COCO Camera Traps Object Detection TFRecords. - -Please note that this tool creates sharded output files. - -This tool assumes the input annotations are in the COCO Camera Traps json -format, specified here: -https://github.com/Microsoft/CameraTraps/blob/master/data_management/README.md - -Example usage: - - python create_cococameratraps_tfexample_main.py \ - --alsologtostderr \ - --output_tfrecord_prefix="/path/to/output/tfrecord/location/prefix" \ - --image_directory="/path/to/image/folder/" \ - --input_annotations_file="path/to/annotations.json" - -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import hashlib -import io -import json -import os -import numpy as np -import PIL.Image -import tensorflow as tf -from object_detection.utils import dataset_util - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -class ParseImage(beam.DoFn): - """A DoFn that parses a COCO-CameraTraps json and emits TFRecords.""" - - def __init__(self, image_directory, images, annotations, categories, - keep_bboxes): - """Initialization function. - - Args: - image_directory: Path to image directory - images: list of COCO Camera Traps style image dictionaries - annotations: list of COCO Camera Traps style annotation dictionaries - categories: list of COCO Camera Traps style category dictionaries - keep_bboxes: Whether to keep any bounding boxes that exist in the - annotations - """ - - self._image_directory = image_directory - self._image_dict = {im['id']: im for im in images} - self._annotation_dict = {im['id']: [] for im in images} - self._category_dict = {int(cat['id']): cat for cat in categories} - for ann in annotations: - self._annotation_dict[ann['image_id']].append(ann) - self._images = images - self._keep_bboxes = keep_bboxes - - self._num_examples_processed = beam.metrics.Metrics.counter( - 'cococameratraps_data_generation', 'num_tf_examples_processed') - - def process(self, image_id): - """Builds a tf.Example given an image id. - - Args: - image_id: the image id of the associated image - - Returns: - List of tf.Examples. - """ - - image = self._image_dict[image_id] - annotations = self._annotation_dict[image_id] - image_height = image['height'] - image_width = image['width'] - filename = image['file_name'] - image_id = image['id'] - image_location_id = image['location'] - - image_datetime = str(image['date_captured']) - - image_sequence_id = str(image['seq_id']) - image_sequence_num_frames = int(image['seq_num_frames']) - image_sequence_frame_num = int(image['frame_num']) - - full_path = os.path.join(self._image_directory, filename) - - try: - # Ensure the image exists and is not corrupted - with tf.io.gfile.GFile(full_path, 'rb') as fid: - encoded_jpg = fid.read() - encoded_jpg_io = io.BytesIO(encoded_jpg) - image = PIL.Image.open(encoded_jpg_io) - image = tf.io.decode_jpeg(encoded_jpg, channels=3) - except Exception: # pylint: disable=broad-except - # The image file is missing or corrupt - return [] - - key = hashlib.sha256(encoded_jpg).hexdigest() - feature_dict = { - 'image/height': - dataset_util.int64_feature(image_height), - 'image/width': - dataset_util.int64_feature(image_width), - 'image/filename': - dataset_util.bytes_feature(filename.encode('utf8')), - 'image/source_id': - dataset_util.bytes_feature(str(image_id).encode('utf8')), - 'image/key/sha256': - dataset_util.bytes_feature(key.encode('utf8')), - 'image/encoded': - dataset_util.bytes_feature(encoded_jpg), - 'image/format': - dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/location': - dataset_util.bytes_feature(str(image_location_id).encode('utf8')), - 'image/seq_num_frames': - dataset_util.int64_feature(image_sequence_num_frames), - 'image/seq_frame_num': - dataset_util.int64_feature(image_sequence_frame_num), - 'image/seq_id': - dataset_util.bytes_feature(image_sequence_id.encode('utf8')), - 'image/date_captured': - dataset_util.bytes_feature(image_datetime.encode('utf8')) - } - - num_annotations_skipped = 0 - if annotations: - xmin = [] - xmax = [] - ymin = [] - ymax = [] - category_names = [] - category_ids = [] - area = [] - - for object_annotations in annotations: - if 'bbox' in object_annotations and self._keep_bboxes: - (x, y, width, height) = tuple(object_annotations['bbox']) - if width <= 0 or height <= 0: - num_annotations_skipped += 1 - continue - if x + width > image_width or y + height > image_height: - num_annotations_skipped += 1 - continue - xmin.append(float(x) / image_width) - xmax.append(float(x + width) / image_width) - ymin.append(float(y) / image_height) - ymax.append(float(y + height) / image_height) - if 'area' in object_annotations: - area.append(object_annotations['area']) - else: - # approximate area using l*w/2 - area.append(width*height/2.0) - - category_id = int(object_annotations['category_id']) - category_ids.append(category_id) - category_names.append( - self._category_dict[category_id]['name'].encode('utf8')) - - feature_dict.update({ - 'image/object/bbox/xmin': - dataset_util.float_list_feature(xmin), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(xmax), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(ymin), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(ymax), - 'image/object/class/text': - dataset_util.bytes_list_feature(category_names), - 'image/object/class/label': - dataset_util.int64_list_feature(category_ids), - 'image/object/area': - dataset_util.float_list_feature(area), - }) - - # For classification, add the first category to image/class/label and - # image/class/text - if not category_ids: - feature_dict.update({ - 'image/class/label': - dataset_util.int64_list_feature([0]), - 'image/class/text': - dataset_util.bytes_list_feature(['empty'.encode('utf8')]), - }) - else: - feature_dict.update({ - 'image/class/label': - dataset_util.int64_list_feature([category_ids[0]]), - 'image/class/text': - dataset_util.bytes_list_feature([category_names[0]]), - }) - - else: - # Add empty class if there are no annotations - feature_dict.update({ - 'image/class/label': - dataset_util.int64_list_feature([0]), - 'image/class/text': - dataset_util.bytes_list_feature(['empty'.encode('utf8')]), - }) - - example = tf.train.Example(features=tf.train.Features(feature=feature_dict)) - self._num_examples_processed.inc(1) - - return [(example)] - - -def load_json_data(data_file): - with tf.io.gfile.GFile(data_file, 'r') as fid: - data_dict = json.load(fid) - return data_dict - - -def create_pipeline(pipeline, - image_directory, - input_annotations_file, - output_tfrecord_prefix=None, - num_images_per_shard=200, - keep_bboxes=True): - """Creates a beam pipeline for producing a COCO-CameraTraps Image dataset. - - Args: - pipeline: Initialized beam pipeline. - image_directory: Path to image directory - input_annotations_file: Path to a coco-cameratraps annotation file - output_tfrecord_prefix: Absolute path for tfrecord outputs. Final files will - be named {output_tfrecord_prefix}@N. - num_images_per_shard: The number of images to store in each shard - keep_bboxes: Whether to keep any bounding boxes that exist in the json file - """ - - data = load_json_data(input_annotations_file) - - num_shards = int(np.ceil(float(len(data['images']))/num_images_per_shard)) - - image_examples = ( - pipeline | ('CreateCollections') >> beam.Create( - [im['id'] for im in data['images']]) - | ('ParseImage') >> beam.ParDo(ParseImage( - image_directory, data['images'], data['annotations'], - data['categories'], keep_bboxes=keep_bboxes))) - _ = (image_examples - | ('Reshuffle') >> beam.Reshuffle() - | ('WriteTfImageExample') >> beam.io.tfrecordio.WriteToTFRecord( - output_tfrecord_prefix, - num_shards=num_shards, - coder=beam.coders.ProtoCoder(tf.train.Example))) - - -def parse_args(argv): - """Command-line argument parser. - - Args: - argv: command line arguments - Returns: - beam_args: Arguments for the beam pipeline. - pipeline_args: Arguments for the pipeline options, such as runner type. - """ - parser = argparse.ArgumentParser() - parser.add_argument( - '--image_directory', - dest='image_directory', - required=True, - help='Path to the directory where the images are stored.') - parser.add_argument( - '--output_tfrecord_prefix', - dest='output_tfrecord_prefix', - required=True, - help='Path and prefix to store TFRecords containing images in tf.Example' - 'format.') - parser.add_argument( - '--input_annotations_file', - dest='input_annotations_file', - required=True, - help='Path to Coco-CameraTraps style annotations file.') - parser.add_argument( - '--num_images_per_shard', - dest='num_images_per_shard', - default=200, - help='The number of images to be stored in each outputshard.') - beam_args, pipeline_args = parser.parse_known_args(argv) - return beam_args, pipeline_args - - -def main(argv=None, save_main_session=True): - """Runs the Beam pipeline that performs inference. - - Args: - argv: Command line arguments. - save_main_session: Whether to save the main session. - """ - args, pipeline_args = parse_args(argv) - - pipeline_options = beam.options.pipeline_options.PipelineOptions( - pipeline_args) - pipeline_options.view_as( - beam.options.pipeline_options.SetupOptions).save_main_session = ( - save_main_session) - - dirname = os.path.dirname(args.output_tfrecord_prefix) - tf.io.gfile.makedirs(dirname) - - p = beam.Pipeline(options=pipeline_options) - create_pipeline( - pipeline=p, - image_directory=args.image_directory, - input_annotations_file=args.input_annotations_file, - output_tfrecord_prefix=args.output_tfrecord_prefix, - num_images_per_shard=args.num_images_per_shard) - p.run() - - -if __name__ == '__main__': - main() diff --git a/research/object_detection/dataset_tools/context_rcnn/create_cococameratraps_tfexample_tf2_test.py b/research/object_detection/dataset_tools/context_rcnn/create_cococameratraps_tfexample_tf2_test.py deleted file mode 100644 index 0a1ac203f33..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/create_cococameratraps_tfexample_tf2_test.py +++ /dev/null @@ -1,214 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for create_cococameratraps_tfexample_main.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import datetime -import json -import os -import tempfile -import unittest - -import numpy as np - -from PIL import Image -import tensorflow as tf -from object_detection.utils import tf_version - -if tf_version.is_tf2(): - from object_detection.dataset_tools.context_rcnn import create_cococameratraps_tfexample_main # pylint:disable=g-import-not-at-top - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CreateCOCOCameraTrapsTfexampleTest(tf.test.TestCase): - - IMAGE_HEIGHT = 360 - IMAGE_WIDTH = 480 - - def _write_random_images_to_directory(self, directory, num_frames): - for frame_num in range(num_frames): - img = np.random.randint(0, high=256, - size=(self.IMAGE_HEIGHT, self.IMAGE_WIDTH, 3), - dtype=np.uint8) - pil_image = Image.fromarray(img) - fname = 'im_' + str(frame_num) + '.jpg' - pil_image.save(os.path.join(directory, fname), 'JPEG') - - def _create_json_file(self, directory, num_frames, keep_bboxes=False): - json_dict = {'images': [], 'annotations': []} - json_dict['categories'] = [{'id': 0, 'name': 'empty'}, - {'id': 1, 'name': 'animal'}] - for idx in range(num_frames): - im = {'id': 'im_' + str(idx), - 'file_name': 'im_' + str(idx) + '.jpg', - 'height': self.IMAGE_HEIGHT, - 'width': self.IMAGE_WIDTH, - 'seq_id': 'seq_1', - 'seq_num_frames': num_frames, - 'frame_num': idx, - 'location': 'loc_' + str(idx), - 'date_captured': str(datetime.datetime.now()) - } - json_dict['images'].append(im) - ann = {'id': 'ann' + str(idx), - 'image_id': 'im_' + str(idx), - 'category_id': 1, - } - if keep_bboxes: - ann['bbox'] = [0.0 * self.IMAGE_WIDTH, - 0.1 * self.IMAGE_HEIGHT, - 0.5 * self.IMAGE_WIDTH, - 0.5 * self.IMAGE_HEIGHT] - json_dict['annotations'].append(ann) - - json_path = os.path.join(directory, 'test_file.json') - with tf.io.gfile.GFile(json_path, 'w') as f: - json.dump(json_dict, f) - return json_path - - def assert_expected_example_bbox(self, example): - self.assertAllClose( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.1]) - self.assertAllClose( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.0]) - self.assertAllClose( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.6]) - self.assertAllClose( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.5]) - self.assertAllClose( - example.features.feature['image/object/class/label'] - .int64_list.value, [1]) - self.assertAllEqual( - example.features.feature['image/object/class/text'] - .bytes_list.value, [b'animal']) - self.assertAllClose( - example.features.feature['image/class/label'] - .int64_list.value, [1]) - self.assertAllEqual( - example.features.feature['image/class/text'] - .bytes_list.value, [b'animal']) - - # Check other essential attributes. - self.assertAllEqual( - example.features.feature['image/height'].int64_list.value, - [self.IMAGE_HEIGHT]) - self.assertAllEqual( - example.features.feature['image/width'].int64_list.value, - [self.IMAGE_WIDTH]) - self.assertAllEqual( - example.features.feature['image/source_id'].bytes_list.value, - [b'im_0']) - self.assertTrue( - example.features.feature['image/encoded'].bytes_list.value) - - def assert_expected_example(self, example): - self.assertAllClose( - example.features.feature['image/object/bbox/ymin'].float_list.value, - []) - self.assertAllClose( - example.features.feature['image/object/bbox/xmin'].float_list.value, - []) - self.assertAllClose( - example.features.feature['image/object/bbox/ymax'].float_list.value, - []) - self.assertAllClose( - example.features.feature['image/object/bbox/xmax'].float_list.value, - []) - self.assertAllClose( - example.features.feature['image/object/class/label'] - .int64_list.value, [1]) - self.assertAllEqual( - example.features.feature['image/object/class/text'] - .bytes_list.value, [b'animal']) - self.assertAllClose( - example.features.feature['image/class/label'] - .int64_list.value, [1]) - self.assertAllEqual( - example.features.feature['image/class/text'] - .bytes_list.value, [b'animal']) - - # Check other essential attributes. - self.assertAllEqual( - example.features.feature['image/height'].int64_list.value, - [self.IMAGE_HEIGHT]) - self.assertAllEqual( - example.features.feature['image/width'].int64_list.value, - [self.IMAGE_WIDTH]) - self.assertAllEqual( - example.features.feature['image/source_id'].bytes_list.value, - [b'im_0']) - self.assertTrue( - example.features.feature['image/encoded'].bytes_list.value) - - def test_beam_pipeline(self): - num_frames = 1 - temp_dir = tempfile.mkdtemp(dir=os.environ.get('TEST_TMPDIR')) - json_path = self._create_json_file(temp_dir, num_frames) - output_tfrecord = temp_dir+'/output' - self._write_random_images_to_directory(temp_dir, num_frames) - pipeline_options = beam.options.pipeline_options.PipelineOptions( - runner='DirectRunner') - p = beam.Pipeline(options=pipeline_options) - create_cococameratraps_tfexample_main.create_pipeline( - p, temp_dir, json_path, - output_tfrecord_prefix=output_tfrecord) - p.run() - filenames = tf.io.gfile.glob(output_tfrecord + '-?????-of-?????') - actual_output = [] - record_iterator = tf.data.TFRecordDataset( - tf.convert_to_tensor(filenames)).as_numpy_iterator() - for record in record_iterator: - actual_output.append(record) - self.assertEqual(len(actual_output), num_frames) - self.assert_expected_example(tf.train.Example.FromString( - actual_output[0])) - - def test_beam_pipeline_bbox(self): - num_frames = 1 - temp_dir = tempfile.mkdtemp(dir=os.environ.get('TEST_TMPDIR')) - json_path = self._create_json_file(temp_dir, num_frames, keep_bboxes=True) - output_tfrecord = temp_dir+'/output' - self._write_random_images_to_directory(temp_dir, num_frames) - pipeline_options = beam.options.pipeline_options.PipelineOptions( - runner='DirectRunner') - p = beam.Pipeline(options=pipeline_options) - create_cococameratraps_tfexample_main.create_pipeline( - p, temp_dir, json_path, - output_tfrecord_prefix=output_tfrecord, - keep_bboxes=True) - p.run() - filenames = tf.io.gfile.glob(output_tfrecord+'-?????-of-?????') - actual_output = [] - record_iterator = tf.data.TFRecordDataset( - tf.convert_to_tensor(filenames)).as_numpy_iterator() - for record in record_iterator: - actual_output.append(record) - self.assertEqual(len(actual_output), num_frames) - self.assert_expected_example_bbox(tf.train.Example.FromString( - actual_output[0])) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/context_rcnn/generate_detection_data.py b/research/object_detection/dataset_tools/context_rcnn/generate_detection_data.py deleted file mode 100644 index c826873802f..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/generate_detection_data.py +++ /dev/null @@ -1,283 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""A Beam job to generate detection data for camera trap images. - -This tools allows to run inference with an exported Object Detection model in -`saved_model` format and produce raw detection boxes on images in tf.Examples, -with the assumption that the bounding box class label will match the image-level -class label in the tf.Example. - -Steps to generate a detection dataset: -1. Use object_detection/export_inference_graph.py to get a `saved_model` for - inference. The input node must accept a tf.Example proto. -2. Run this tool with `saved_model` from step 1 and an TFRecord of tf.Example - protos containing images for inference. - -Example Usage: --------------- -python tensorflow_models/object_detection/export_inference_graph.py \ - --alsologtostderr \ - --input_type tf_example \ - --pipeline_config_path path/to/detection_model.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory - -python generate_detection_data.py \ - --alsologtostderr \ - --input_tfrecord path/to/input_tfrecord@X \ - --output_tfrecord path/to/output_tfrecord@X \ - --model_dir path/to/exported_model_directory/saved_model -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import os -import threading -import tensorflow as tf - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -class GenerateDetectionDataFn(beam.DoFn): - """Generates detection data for camera trap images. - - This Beam DoFn performs inference with an object detection `saved_model` and - produces detection boxes for camera trap data, matched to the - object class. - """ - session_lock = threading.Lock() - - def __init__(self, model_dir, confidence_threshold): - """Initialization function. - - Args: - model_dir: A directory containing saved model. - confidence_threshold: the confidence threshold for boxes to keep - """ - self._model_dir = model_dir - self._confidence_threshold = confidence_threshold - self._session = None - self._num_examples_processed = beam.metrics.Metrics.counter( - 'detection_data_generation', 'num_tf_examples_processed') - - def setup(self): - self._load_inference_model() - - def _load_inference_model(self): - # Because initialization of the tf.Session is expensive we share - # one instance across all threads in the worker. This is possible since - # tf.Session.run() is thread safe. - with self.session_lock: - self._detect_fn = tf.saved_model.load(self._model_dir) - - def process(self, tfrecord_entry): - return self._run_inference_and_generate_detections(tfrecord_entry) - - def _run_inference_and_generate_detections(self, tfrecord_entry): - input_example = tf.train.Example.FromString(tfrecord_entry) - if input_example.features.feature[ - 'image/object/bbox/ymin'].float_list.value: - # There are already ground truth boxes for this image, just keep them. - return [input_example] - - detections = self._detect_fn.signatures['serving_default']( - (tf.expand_dims(tf.convert_to_tensor(tfrecord_entry), 0))) - detection_boxes = detections['detection_boxes'] - num_detections = detections['num_detections'] - detection_scores = detections['detection_scores'] - - example = tf.train.Example() - - num_detections = int(num_detections[0]) - - image_class_labels = input_example.features.feature[ - 'image/object/class/label'].int64_list.value - - image_class_texts = input_example.features.feature[ - 'image/object/class/text'].bytes_list.value - - # Ignore any images with multiple classes, - # we can't match the class to the box. - if len(image_class_labels) > 1: - return [] - - # Don't add boxes for images already labeled empty (for now) - if len(image_class_labels) == 1: - # Add boxes over confidence threshold. - for idx, score in enumerate(detection_scores[0]): - if score >= self._confidence_threshold and idx < num_detections: - example.features.feature[ - 'image/object/bbox/ymin'].float_list.value.extend([ - detection_boxes[0, idx, 0]]) - example.features.feature[ - 'image/object/bbox/xmin'].float_list.value.extend([ - detection_boxes[0, idx, 1]]) - example.features.feature[ - 'image/object/bbox/ymax'].float_list.value.extend([ - detection_boxes[0, idx, 2]]) - example.features.feature[ - 'image/object/bbox/xmax'].float_list.value.extend([ - detection_boxes[0, idx, 3]]) - - # Add box scores and class texts and labels. - example.features.feature[ - 'image/object/class/score'].float_list.value.extend( - [score]) - - example.features.feature[ - 'image/object/class/label'].int64_list.value.extend( - [image_class_labels[0]]) - - example.features.feature[ - 'image/object/class/text'].bytes_list.value.extend( - [image_class_texts[0]]) - - # Add other essential example attributes - example.features.feature['image/encoded'].bytes_list.value.extend( - input_example.features.feature['image/encoded'].bytes_list.value) - example.features.feature['image/height'].int64_list.value.extend( - input_example.features.feature['image/height'].int64_list.value) - example.features.feature['image/width'].int64_list.value.extend( - input_example.features.feature['image/width'].int64_list.value) - example.features.feature['image/source_id'].bytes_list.value.extend( - input_example.features.feature['image/source_id'].bytes_list.value) - example.features.feature['image/location'].bytes_list.value.extend( - input_example.features.feature['image/location'].bytes_list.value) - - example.features.feature['image/date_captured'].bytes_list.value.extend( - input_example.features.feature['image/date_captured'].bytes_list.value) - - example.features.feature['image/class/text'].bytes_list.value.extend( - input_example.features.feature['image/class/text'].bytes_list.value) - example.features.feature['image/class/label'].int64_list.value.extend( - input_example.features.feature['image/class/label'].int64_list.value) - - example.features.feature['image/seq_id'].bytes_list.value.extend( - input_example.features.feature['image/seq_id'].bytes_list.value) - example.features.feature['image/seq_num_frames'].int64_list.value.extend( - input_example.features.feature['image/seq_num_frames'].int64_list.value) - example.features.feature['image/seq_frame_num'].int64_list.value.extend( - input_example.features.feature['image/seq_frame_num'].int64_list.value) - - self._num_examples_processed.inc(1) - return [example] - - -def construct_pipeline(pipeline, input_tfrecord, output_tfrecord, model_dir, - confidence_threshold, num_shards): - """Returns a Beam pipeline to run object detection inference. - - Args: - pipeline: Initialized beam pipeline. - input_tfrecord: A TFRecord of tf.train.Example protos containing images. - output_tfrecord: A TFRecord of tf.train.Example protos that contain images - in the input TFRecord and the detections from the model. - model_dir: Path to `saved_model` to use for inference. - confidence_threshold: Threshold to use when keeping detection results. - num_shards: The number of output shards. - """ - input_collection = ( - pipeline | 'ReadInputTFRecord' >> beam.io.tfrecordio.ReadFromTFRecord( - input_tfrecord, - coder=beam.coders.BytesCoder())) - output_collection = input_collection | 'RunInference' >> beam.ParDo( - GenerateDetectionDataFn(model_dir, confidence_threshold)) - output_collection = output_collection | 'Reshuffle' >> beam.Reshuffle() - _ = output_collection | 'WritetoDisk' >> beam.io.tfrecordio.WriteToTFRecord( - output_tfrecord, - num_shards=num_shards, - coder=beam.coders.ProtoCoder(tf.train.Example)) - - -def parse_args(argv): - """Command-line argument parser. - - Args: - argv: command line arguments - Returns: - beam_args: Arguments for the beam pipeline. - pipeline_args: Arguments for the pipeline options, such as runner type. - """ - parser = argparse.ArgumentParser() - parser.add_argument( - '--detection_input_tfrecord', - dest='detection_input_tfrecord', - required=True, - help='TFRecord containing images in tf.Example format for object ' - 'detection.') - parser.add_argument( - '--detection_output_tfrecord', - dest='detection_output_tfrecord', - required=True, - help='TFRecord containing detections in tf.Example format.') - parser.add_argument( - '--detection_model_dir', - dest='detection_model_dir', - required=True, - help='Path to directory containing an object detection SavedModel.') - parser.add_argument( - '--confidence_threshold', - dest='confidence_threshold', - default=0.9, - help='Min confidence to keep bounding boxes.') - parser.add_argument( - '--num_shards', - dest='num_shards', - default=0, - help='Number of output shards.') - beam_args, pipeline_args = parser.parse_known_args(argv) - return beam_args, pipeline_args - - -def main(argv=None, save_main_session=True): - """Runs the Beam pipeline that performs inference. - - Args: - argv: Command line arguments. - save_main_session: Whether to save the main session. - """ - - args, pipeline_args = parse_args(argv) - - pipeline_options = beam.options.pipeline_options.PipelineOptions( - pipeline_args) - pipeline_options.view_as( - beam.options.pipeline_options.SetupOptions).save_main_session = ( - save_main_session) - - dirname = os.path.dirname(args.detection_output_tfrecord) - tf.io.gfile.makedirs(dirname) - - p = beam.Pipeline(options=pipeline_options) - - construct_pipeline( - p, - args.detection_input_tfrecord, - args.detection_output_tfrecord, - args.detection_model_dir, - args.confidence_threshold, - args.num_shards) - - p.run() - - -if __name__ == '__main__': - main() diff --git a/research/object_detection/dataset_tools/context_rcnn/generate_detection_data_tf2_test.py b/research/object_detection/dataset_tools/context_rcnn/generate_detection_data_tf2_test.py deleted file mode 100644 index 3350eb2df7f..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/generate_detection_data_tf2_test.py +++ /dev/null @@ -1,260 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for generate_detection_data.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import contextlib -import os -import tempfile -import unittest -import numpy as np -import six -import tensorflow as tf - -from object_detection import exporter_lib_v2 -from object_detection.builders import model_builder -from object_detection.core import model -from object_detection.protos import pipeline_pb2 -from object_detection.utils import tf_version - -if tf_version.is_tf2(): - from object_detection.dataset_tools.context_rcnn import generate_detection_data # pylint:disable=g-import-not-at-top - -if six.PY2: - import mock # pylint: disable=g-import-not-at-top -else: - mock = unittest.mock - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -class FakeModel(model.DetectionModel): - - def __init__(self, conv_weight_scalar=1.0): - super(FakeModel, self).__init__(num_classes=5) - self._conv = tf.keras.layers.Conv2D( - filters=1, kernel_size=1, strides=(1, 1), padding='valid', - kernel_initializer=tf.keras.initializers.Constant( - value=conv_weight_scalar)) - - def preprocess(self, inputs): - return tf.identity(inputs), exporter_lib_v2.get_true_shapes(inputs) - - def predict(self, preprocessed_inputs, true_image_shapes): - return {'image': self._conv(preprocessed_inputs)} - - def postprocess(self, prediction_dict, true_image_shapes): - with tf.control_dependencies(list(prediction_dict.values())): - postprocessed_tensors = { - 'detection_boxes': tf.constant([[[0.0, 0.1, 0.5, 0.6], - [0.5, 0.5, 0.8, 0.8]]], tf.float32), - 'detection_scores': tf.constant([[0.95, 0.6]], tf.float32), - 'detection_multiclass_scores': tf.constant([[[0.1, 0.7, 0.2], - [0.3, 0.1, 0.6]]], - tf.float32), - 'detection_classes': tf.constant([[0, 1]], tf.float32), - 'num_detections': tf.constant([2], tf.float32) - } - return postprocessed_tensors - - def restore_map(self, checkpoint_path, fine_tune_checkpoint_type): - pass - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def loss(self, prediction_dict, true_image_shapes): - pass - - def regularization_losses(self): - pass - - def updates(self): - pass - - -@contextlib.contextmanager -def InMemoryTFRecord(entries): - temp = tempfile.NamedTemporaryFile(delete=False) - filename = temp.name - try: - with tf.io.TFRecordWriter(filename) as writer: - for value in entries: - writer.write(value) - yield filename - finally: - os.unlink(filename) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class GenerateDetectionDataTest(tf.test.TestCase): - - def _save_checkpoint_from_mock_model(self, checkpoint_path): - """A function to save checkpoint from a fake Detection Model. - - Args: - checkpoint_path: Path to save checkpoint from Fake model. - """ - mock_model = FakeModel() - fake_image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - preprocessed_inputs, true_image_shapes = mock_model.preprocess(fake_image) - predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) - mock_model.postprocess(predictions, true_image_shapes) - ckpt = tf.train.Checkpoint(model=mock_model) - exported_checkpoint_manager = tf.train.CheckpointManager( - ckpt, checkpoint_path, max_to_keep=1) - exported_checkpoint_manager.save(checkpoint_number=0) - - def _export_saved_model(self): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - output_directory = os.path.join(tmp_dir, 'output') - saved_model_path = os.path.join(output_directory, 'saved_model') - tf.io.gfile.makedirs(output_directory) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP['model_build'] = mock_builder - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter_lib_v2.export_inference_graph( - input_type='tf_example', - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory) - saved_model_path = os.path.join(output_directory, 'saved_model') - return saved_model_path - - def _create_tf_example(self): - with self.test_session(): - encoded_image = tf.io.encode_jpeg( - tf.constant(np.ones((4, 6, 3)).astype(np.uint8))).numpy() - - def BytesFeature(value): - return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) - - def Int64Feature(value): - return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) - - example = tf.train.Example(features=tf.train.Features(feature={ - 'image/encoded': BytesFeature(encoded_image), - 'image/source_id': BytesFeature(b'image_id'), - 'image/height': Int64Feature(4), - 'image/width': Int64Feature(6), - 'image/object/class/label': Int64Feature(5), - 'image/object/class/text': BytesFeature(b'hyena'), - 'image/class/label': Int64Feature(5), - 'image/class/text': BytesFeature(b'hyena'), - })) - - return example.SerializeToString() - - def assert_expected_example(self, example): - self.assertAllClose( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.0]) - self.assertAllClose( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.1]) - self.assertAllClose( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.5]) - self.assertAllClose( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.6]) - self.assertAllClose( - example.features.feature['image/object/class/score'] - .float_list.value, [0.95]) - self.assertAllClose( - example.features.feature['image/object/class/label'] - .int64_list.value, [5]) - self.assertAllEqual( - example.features.feature['image/object/class/text'] - .bytes_list.value, [b'hyena']) - self.assertAllClose( - example.features.feature['image/class/label'] - .int64_list.value, [5]) - self.assertAllEqual( - example.features.feature['image/class/text'] - .bytes_list.value, [b'hyena']) - - # Check other essential attributes. - self.assertAllEqual( - example.features.feature['image/height'].int64_list.value, [4]) - self.assertAllEqual( - example.features.feature['image/width'].int64_list.value, [6]) - self.assertAllEqual( - example.features.feature['image/source_id'].bytes_list.value, - [b'image_id']) - self.assertTrue( - example.features.feature['image/encoded'].bytes_list.value) - - def test_generate_detection_data_fn(self): - saved_model_path = self._export_saved_model() - confidence_threshold = 0.8 - inference_fn = generate_detection_data.GenerateDetectionDataFn( - saved_model_path, confidence_threshold) - inference_fn.setup() - generated_example = self._create_tf_example() - self.assertAllEqual(tf.train.Example.FromString( - generated_example).features.feature['image/object/class/label'] - .int64_list.value, [5]) - self.assertAllEqual(tf.train.Example.FromString( - generated_example).features.feature['image/object/class/text'] - .bytes_list.value, [b'hyena']) - output = inference_fn.process(generated_example) - output_example = output[0] - - self.assertAllEqual( - output_example.features.feature['image/object/class/label'] - .int64_list.value, [5]) - self.assertAllEqual(output_example.features.feature['image/width'] - .int64_list.value, [6]) - - self.assert_expected_example(output_example) - - def test_beam_pipeline(self): - with InMemoryTFRecord([self._create_tf_example()]) as input_tfrecord: - temp_dir = tempfile.mkdtemp(dir=os.environ.get('TEST_TMPDIR')) - output_tfrecord = os.path.join(temp_dir, 'output_tfrecord') - saved_model_path = self._export_saved_model() - confidence_threshold = 0.8 - num_shards = 1 - pipeline_options = beam.options.pipeline_options.PipelineOptions( - runner='DirectRunner') - p = beam.Pipeline(options=pipeline_options) - generate_detection_data.construct_pipeline( - p, input_tfrecord, output_tfrecord, saved_model_path, - confidence_threshold, num_shards) - p.run() - filenames = tf.io.gfile.glob(output_tfrecord + '-?????-of-?????') - actual_output = [] - record_iterator = tf.data.TFRecordDataset( - tf.convert_to_tensor(filenames)).as_numpy_iterator() - for record in record_iterator: - actual_output.append(record) - self.assertEqual(len(actual_output), 1) - self.assert_expected_example(tf.train.Example.FromString( - actual_output[0])) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/context_rcnn/generate_embedding_data.py b/research/object_detection/dataset_tools/context_rcnn/generate_embedding_data.py deleted file mode 100644 index dac1168c14a..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/generate_embedding_data.py +++ /dev/null @@ -1,370 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""A Beam job to generate embedding data for camera trap images. - -This tool runs inference with an exported Object Detection model in -`saved_model` format and produce raw embeddings for camera trap data. These -embeddings contain an object-centric feature embedding from Faster R-CNN, the -datetime that the image was taken (normalized in a specific way), and the -position of the object of interest. By default, only the highest-scoring object -embedding is included. - -Steps to generate a embedding dataset: -1. Use object_detection/export_inference_graph.py to get a Faster R-CNN - `saved_model` for inference. The input node must accept a tf.Example proto. -2. Run this tool with `saved_model` from step 1 and an TFRecord of tf.Example - protos containing images for inference. - -Example Usage: --------------- -python tensorflow_models/object_detection/export_inference_graph.py \ - --alsologtostderr \ - --input_type tf_example \ - --pipeline_config_path path/to/faster_rcnn_model.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory \ - --additional_output_tensor_names detection_features - -python generate_embedding_data.py \ - --alsologtostderr \ - --embedding_input_tfrecord path/to/input_tfrecords* \ - --embedding_output_tfrecord path/to/output_tfrecords \ - --embedding_model_dir path/to/exported_model_directory/saved_model -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import datetime -import os -import threading - -import numpy as np -import six -import tensorflow as tf - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -def add_keys(serialized_example): - key = hash(serialized_example) - return key, serialized_example - - -def drop_keys(key_value_tuple): - return key_value_tuple[1] - - -def get_date_captured(example): - date_captured = datetime.datetime.strptime( - six.ensure_str( - example.features.feature['image/date_captured'].bytes_list.value[0]), - '%Y-%m-%d %H:%M:%S') - return date_captured - - -def embed_date_captured(date_captured): - """Encodes the datetime of the image.""" - embedded_date_captured = [] - month_max = 12.0 - day_max = 31.0 - hour_max = 24.0 - minute_max = 60.0 - min_year = 1990.0 - max_year = 2030.0 - - year = (date_captured.year - min_year) / float(max_year - min_year) - embedded_date_captured.append(year) - - month = (date_captured.month - 1) / month_max - embedded_date_captured.append(month) - - day = (date_captured.day - 1) / day_max - embedded_date_captured.append(day) - - hour = date_captured.hour / hour_max - embedded_date_captured.append(hour) - - minute = date_captured.minute / minute_max - embedded_date_captured.append(minute) - - return np.asarray(embedded_date_captured) - - -def embed_position_and_size(box): - """Encodes the bounding box of the object of interest.""" - ymin = box[0] - xmin = box[1] - ymax = box[2] - xmax = box[3] - w = xmax - xmin - h = ymax - ymin - x = xmin + w / 2.0 - y = ymin + h / 2.0 - return np.asarray([x, y, w, h]) - - -def get_bb_embedding(detection_features, detection_boxes, detection_scores, - index): - embedding = detection_features[0][index] - pooled_embedding = np.mean(np.mean(embedding, axis=1), axis=0) - - box = detection_boxes[0][index] - position_embedding = embed_position_and_size(box) - - score = detection_scores[0][index] - return np.concatenate((pooled_embedding, position_embedding)), score - - -class GenerateEmbeddingDataFn(beam.DoFn): - """Generates embedding data for camera trap images. - - This Beam DoFn performs inference with an object detection `saved_model` and - produces contextual embedding vectors. - """ - session_lock = threading.Lock() - - def __init__(self, model_dir, top_k_embedding_count, - bottom_k_embedding_count, embedding_type='final_box_features'): - """Initialization function. - - Args: - model_dir: A directory containing saved model. - top_k_embedding_count: the number of high-confidence embeddings to store - bottom_k_embedding_count: the number of low-confidence embeddings to store - embedding_type: One of 'final_box_features', 'rpn_box_features' - """ - self._model_dir = model_dir - self._session = None - self._num_examples_processed = beam.metrics.Metrics.counter( - 'embedding_data_generation', 'num_tf_examples_processed') - self._top_k_embedding_count = top_k_embedding_count - self._bottom_k_embedding_count = bottom_k_embedding_count - self._embedding_type = embedding_type - - def setup(self): - self._load_inference_model() - - def _load_inference_model(self): - # Because initialization of the tf.Session is expensive we share - # one instance across all threads in the worker. This is possible since - # tf.Session.run() is thread safe. - with self.session_lock: - self._detect_fn = tf.saved_model.load(self._model_dir) - - def process(self, tfexample_key_value): - return self._run_inference_and_generate_embedding(tfexample_key_value) - - def _run_inference_and_generate_embedding(self, tfexample_key_value): - key, tfexample = tfexample_key_value - input_example = tf.train.Example.FromString(tfexample) - example = tf.train.Example() - example.CopyFrom(input_example) - - try: - date_captured = get_date_captured(input_example) - unix_time = ((date_captured - - datetime.datetime.fromtimestamp(0)).total_seconds()) - example.features.feature['image/unix_time'].float_list.value.extend( - [unix_time]) - temporal_embedding = embed_date_captured(date_captured) - except Exception: # pylint: disable=broad-except - temporal_embedding = None - - detections = self._detect_fn.signatures['serving_default']( - (tf.expand_dims(tf.convert_to_tensor(tfexample), 0))) - if self._embedding_type == 'final_box_features': - detection_features = detections['detection_features'] - elif self._embedding_type == 'rpn_box_features': - detection_features = detections['cropped_rpn_box_features'] - else: - raise ValueError('embedding type not supported') - detection_boxes = detections['detection_boxes'] - num_detections = detections['num_detections'] - detection_scores = detections['detection_scores'] - - num_detections = int(num_detections) - embed_all = [] - score_all = [] - - detection_features = np.asarray(detection_features) - - embedding_count = 0 - for index in range(min(num_detections, self._top_k_embedding_count)): - bb_embedding, score = get_bb_embedding( - detection_features, detection_boxes, detection_scores, index) - embed_all.extend(bb_embedding) - if temporal_embedding is not None: embed_all.extend(temporal_embedding) - score_all.append(score) - embedding_count += 1 - - for index in range( - max(0, num_detections - 1), - max(-1, num_detections - 1 - self._bottom_k_embedding_count), -1): - bb_embedding, score = get_bb_embedding( - detection_features, detection_boxes, detection_scores, index) - embed_all.extend(bb_embedding) - if temporal_embedding is not None: embed_all.extend(temporal_embedding) - score_all.append(score) - embedding_count += 1 - - if embedding_count == 0: - bb_embedding, score = get_bb_embedding( - detection_features, detection_boxes, detection_scores, 0) - embed_all.extend(bb_embedding) - if temporal_embedding is not None: embed_all.extend(temporal_embedding) - score_all.append(score) - - # Takes max in case embedding_count is 0. - embedding_length = len(embed_all) // max(1, embedding_count) - - embed_all = np.asarray(embed_all) - - example.features.feature['image/embedding'].float_list.value.extend( - embed_all) - example.features.feature['image/embedding_score'].float_list.value.extend( - score_all) - example.features.feature['image/embedding_length'].int64_list.value.append( - embedding_length) - example.features.feature['image/embedding_count'].int64_list.value.append( - embedding_count) - - self._num_examples_processed.inc(1) - return [(key, example)] - - -def construct_pipeline(pipeline, input_tfrecord, output_tfrecord, model_dir, - top_k_embedding_count, bottom_k_embedding_count, - num_shards, embedding_type): - """Returns a beam pipeline to run object detection inference. - - Args: - pipeline: Initialized beam pipeline. - input_tfrecord: An TFRecord of tf.train.Example protos containing images. - output_tfrecord: An TFRecord of tf.train.Example protos that contain images - in the input TFRecord and the detections from the model. - model_dir: Path to `saved_model` to use for inference. - top_k_embedding_count: The number of high-confidence embeddings to store. - bottom_k_embedding_count: The number of low-confidence embeddings to store. - num_shards: The number of output shards. - embedding_type: Which features to embed. - """ - input_collection = ( - pipeline | 'ReadInputTFRecord' >> beam.io.tfrecordio.ReadFromTFRecord( - input_tfrecord, coder=beam.coders.BytesCoder()) - | 'AddKeys' >> beam.Map(add_keys)) - output_collection = input_collection | 'ExtractEmbedding' >> beam.ParDo( - GenerateEmbeddingDataFn(model_dir, top_k_embedding_count, - bottom_k_embedding_count, embedding_type)) - output_collection = output_collection | 'Reshuffle' >> beam.Reshuffle() - _ = output_collection | 'DropKeys' >> beam.Map( - drop_keys) | 'WritetoDisk' >> beam.io.tfrecordio.WriteToTFRecord( - output_tfrecord, - num_shards=num_shards, - coder=beam.coders.ProtoCoder(tf.train.Example)) - - -def parse_args(argv): - """Command-line argument parser. - - Args: - argv: command line arguments - Returns: - beam_args: Arguments for the beam pipeline. - pipeline_args: Arguments for the pipeline options, such as runner type. - """ - parser = argparse.ArgumentParser() - parser.add_argument( - '--embedding_input_tfrecord', - dest='embedding_input_tfrecord', - required=True, - help='TFRecord containing images in tf.Example format for object ' - 'detection.') - parser.add_argument( - '--embedding_output_tfrecord', - dest='embedding_output_tfrecord', - required=True, - help='TFRecord containing embeddings in tf.Example format.') - parser.add_argument( - '--embedding_model_dir', - dest='embedding_model_dir', - required=True, - help='Path to directory containing an object detection SavedModel with' - 'detection_box_classifier_features in the output.') - parser.add_argument( - '--top_k_embedding_count', - dest='top_k_embedding_count', - default=1, - help='The number of top k embeddings to add to the memory bank.') - parser.add_argument( - '--bottom_k_embedding_count', - dest='bottom_k_embedding_count', - default=0, - help='The number of bottom k embeddings to add to the memory bank.') - parser.add_argument( - '--num_shards', - dest='num_shards', - default=0, - help='Number of output shards.') - parser.add_argument( - '--embedding_type', - dest='embedding_type', - default='final_box_features', - help='What features to embed, supports `final_box_features`, ' - '`rpn_box_features`.') - beam_args, pipeline_args = parser.parse_known_args(argv) - return beam_args, pipeline_args - - -def main(argv=None, save_main_session=True): - """Runs the Beam pipeline that performs inference. - - Args: - argv: Command line arguments. - save_main_session: Whether to save the main session. - """ - args, pipeline_args = parse_args(argv) - - pipeline_options = beam.options.pipeline_options.PipelineOptions( - pipeline_args) - pipeline_options.view_as( - beam.options.pipeline_options.SetupOptions).save_main_session = ( - save_main_session) - - dirname = os.path.dirname(args.embedding_output_tfrecord) - tf.io.gfile.makedirs(dirname) - - p = beam.Pipeline(options=pipeline_options) - - construct_pipeline( - p, - args.embedding_input_tfrecord, - args.embedding_output_tfrecord, - args.embedding_model_dir, - args.top_k_embedding_count, - args.bottom_k_embedding_count, - args.num_shards, - args.embedding_type) - - p.run() - - -if __name__ == '__main__': - main() diff --git a/research/object_detection/dataset_tools/context_rcnn/generate_embedding_data_tf2_test.py b/research/object_detection/dataset_tools/context_rcnn/generate_embedding_data_tf2_test.py deleted file mode 100644 index 156e283eff5..00000000000 --- a/research/object_detection/dataset_tools/context_rcnn/generate_embedding_data_tf2_test.py +++ /dev/null @@ -1,331 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for generate_embedding_data.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import contextlib -import os -import tempfile -import unittest -import numpy as np -import six -import tensorflow as tf -from object_detection import exporter_lib_v2 -from object_detection.builders import model_builder -from object_detection.core import model -from object_detection.protos import pipeline_pb2 -from object_detection.utils import tf_version - -if tf_version.is_tf2(): - from object_detection.dataset_tools.context_rcnn import generate_embedding_data # pylint:disable=g-import-not-at-top - -if six.PY2: - import mock # pylint: disable=g-import-not-at-top -else: - mock = unittest.mock - -try: - import apache_beam as beam # pylint:disable=g-import-not-at-top -except ModuleNotFoundError: - pass - - -class FakeModel(model.DetectionModel): - - def __init__(self, conv_weight_scalar=1.0): - super(FakeModel, self).__init__(num_classes=5) - self._conv = tf.keras.layers.Conv2D( - filters=1, kernel_size=1, strides=(1, 1), padding='valid', - kernel_initializer=tf.keras.initializers.Constant( - value=conv_weight_scalar)) - - def preprocess(self, inputs): - return tf.identity(inputs), exporter_lib_v2.get_true_shapes(inputs) - - def predict(self, preprocessed_inputs, true_image_shapes): - return {'image': self._conv(preprocessed_inputs)} - - def postprocess(self, prediction_dict, true_image_shapes): - with tf.control_dependencies(prediction_dict.values()): - num_features = 100 - feature_dims = 10 - classifier_feature = np.ones( - (2, feature_dims, feature_dims, num_features), - dtype=np.float32).tolist() - postprocessed_tensors = { - 'detection_boxes': tf.constant([[[0.0, 0.1, 0.5, 0.6], - [0.5, 0.5, 0.8, 0.8]]], tf.float32), - 'detection_scores': tf.constant([[0.95, 0.6]], tf.float32), - 'detection_multiclass_scores': tf.constant([[[0.1, 0.7, 0.2], - [0.3, 0.1, 0.6]]], - tf.float32), - 'detection_classes': tf.constant([[0, 1]], tf.float32), - 'num_detections': tf.constant([2], tf.float32), - 'detection_features': - tf.constant([classifier_feature], - tf.float32) - } - return postprocessed_tensors - - def restore_map(self, checkpoint_path, fine_tune_checkpoint_type): - pass - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def loss(self, prediction_dict, true_image_shapes): - pass - - def regularization_losses(self): - pass - - def updates(self): - pass - - -@contextlib.contextmanager -def InMemoryTFRecord(entries): - temp = tempfile.NamedTemporaryFile(delete=False) - filename = temp.name - try: - with tf.io.TFRecordWriter(filename) as writer: - for value in entries: - writer.write(value) - yield filename - finally: - os.unlink(temp.name) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class GenerateEmbeddingData(tf.test.TestCase): - - def _save_checkpoint_from_mock_model(self, checkpoint_path): - """A function to save checkpoint from a fake Detection Model. - - Args: - checkpoint_path: Path to save checkpoint from Fake model. - """ - mock_model = FakeModel() - fake_image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - preprocessed_inputs, true_image_shapes = mock_model.preprocess(fake_image) - predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) - mock_model.postprocess(predictions, true_image_shapes) - ckpt = tf.train.Checkpoint(model=mock_model) - exported_checkpoint_manager = tf.train.CheckpointManager( - ckpt, checkpoint_path, max_to_keep=1) - exported_checkpoint_manager.save(checkpoint_number=0) - - def _export_saved_model(self): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - output_directory = os.path.join(tmp_dir, 'output') - saved_model_path = os.path.join(output_directory, 'saved_model') - tf.io.gfile.makedirs(output_directory) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP['model_build'] = mock_builder - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter_lib_v2.export_inference_graph( - input_type='tf_example', - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory) - saved_model_path = os.path.join(output_directory, 'saved_model') - return saved_model_path - - def _create_tf_example(self): - encoded_image = tf.io.encode_jpeg( - tf.constant(np.ones((4, 4, 3)).astype(np.uint8))).numpy() - - def BytesFeature(value): - return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) - - def Int64Feature(value): - return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) - - def FloatFeature(value): - return tf.train.Feature(float_list=tf.train.FloatList(value=[value])) - - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': BytesFeature(encoded_image), - 'image/source_id': BytesFeature(b'image_id'), - 'image/height': Int64Feature(400), - 'image/width': Int64Feature(600), - 'image/class/label': Int64Feature(5), - 'image/class/text': BytesFeature(b'hyena'), - 'image/object/bbox/xmin': FloatFeature(0.1), - 'image/object/bbox/xmax': FloatFeature(0.6), - 'image/object/bbox/ymin': FloatFeature(0.0), - 'image/object/bbox/ymax': FloatFeature(0.5), - 'image/object/class/score': FloatFeature(0.95), - 'image/object/class/label': Int64Feature(5), - 'image/object/class/text': BytesFeature(b'hyena'), - 'image/date_captured': BytesFeature(b'2019-10-20 12:12:12') - })) - - return example.SerializeToString() - - def assert_expected_example(self, example, topk=False, botk=False): - # Check embeddings - if topk or botk: - self.assertEqual(len( - example.features.feature['image/embedding'].float_list.value), - 218) - self.assertAllEqual( - example.features.feature['image/embedding_count'].int64_list.value, - [2]) - else: - self.assertEqual(len( - example.features.feature['image/embedding'].float_list.value), - 109) - self.assertAllEqual( - example.features.feature['image/embedding_count'].int64_list.value, - [1]) - - self.assertAllEqual( - example.features.feature['image/embedding_length'].int64_list.value, - [109]) - - # Check annotations - self.assertAllClose( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.0]) - self.assertAllClose( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.1]) - self.assertAllClose( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.5]) - self.assertAllClose( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.6]) - self.assertAllClose( - example.features.feature['image/object/class/score'] - .float_list.value, [0.95]) - self.assertAllClose( - example.features.feature['image/object/class/label'] - .int64_list.value, [5]) - self.assertAllEqual( - example.features.feature['image/object/class/text'] - .bytes_list.value, [b'hyena']) - self.assertAllClose( - example.features.feature['image/class/label'] - .int64_list.value, [5]) - self.assertAllEqual( - example.features.feature['image/class/text'] - .bytes_list.value, [b'hyena']) - - # Check other essential attributes. - self.assertAllEqual( - example.features.feature['image/height'].int64_list.value, [400]) - self.assertAllEqual( - example.features.feature['image/width'].int64_list.value, [600]) - self.assertAllEqual( - example.features.feature['image/source_id'].bytes_list.value, - [b'image_id']) - self.assertTrue( - example.features.feature['image/encoded'].bytes_list.value) - - def test_generate_embedding_data_fn(self): - saved_model_path = self._export_saved_model() - top_k_embedding_count = 1 - bottom_k_embedding_count = 0 - inference_fn = generate_embedding_data.GenerateEmbeddingDataFn( - saved_model_path, top_k_embedding_count, bottom_k_embedding_count) - inference_fn.setup() - generated_example = self._create_tf_example() - self.assertAllEqual(tf.train.Example.FromString( - generated_example).features.feature['image/object/class/label'] - .int64_list.value, [5]) - self.assertAllEqual(tf.train.Example.FromString( - generated_example).features.feature['image/object/class/text'] - .bytes_list.value, [b'hyena']) - output = inference_fn.process(('dummy_key', generated_example)) - output_example = output[0][1] - self.assert_expected_example(output_example) - - def test_generate_embedding_data_with_top_k_boxes(self): - saved_model_path = self._export_saved_model() - top_k_embedding_count = 2 - bottom_k_embedding_count = 0 - inference_fn = generate_embedding_data.GenerateEmbeddingDataFn( - saved_model_path, top_k_embedding_count, bottom_k_embedding_count) - inference_fn.setup() - generated_example = self._create_tf_example() - self.assertAllEqual( - tf.train.Example.FromString(generated_example).features - .feature['image/object/class/label'].int64_list.value, [5]) - self.assertAllEqual( - tf.train.Example.FromString(generated_example).features - .feature['image/object/class/text'].bytes_list.value, [b'hyena']) - output = inference_fn.process(('dummy_key', generated_example)) - output_example = output[0][1] - self.assert_expected_example(output_example, topk=True) - - def test_generate_embedding_data_with_bottom_k_boxes(self): - saved_model_path = self._export_saved_model() - top_k_embedding_count = 0 - bottom_k_embedding_count = 2 - inference_fn = generate_embedding_data.GenerateEmbeddingDataFn( - saved_model_path, top_k_embedding_count, bottom_k_embedding_count) - inference_fn.setup() - generated_example = self._create_tf_example() - self.assertAllEqual( - tf.train.Example.FromString(generated_example).features - .feature['image/object/class/label'].int64_list.value, [5]) - self.assertAllEqual( - tf.train.Example.FromString(generated_example).features - .feature['image/object/class/text'].bytes_list.value, [b'hyena']) - output = inference_fn.process(('dummy_key', generated_example)) - output_example = output[0][1] - self.assert_expected_example(output_example, botk=True) - - def test_beam_pipeline(self): - with InMemoryTFRecord([self._create_tf_example()]) as input_tfrecord: - temp_dir = tempfile.mkdtemp(dir=os.environ.get('TEST_TMPDIR')) - output_tfrecord = os.path.join(temp_dir, 'output_tfrecord') - saved_model_path = self._export_saved_model() - top_k_embedding_count = 1 - bottom_k_embedding_count = 0 - num_shards = 1 - embedding_type = 'final_box_features' - pipeline_options = beam.options.pipeline_options.PipelineOptions( - runner='DirectRunner') - p = beam.Pipeline(options=pipeline_options) - generate_embedding_data.construct_pipeline( - p, input_tfrecord, output_tfrecord, saved_model_path, - top_k_embedding_count, bottom_k_embedding_count, num_shards, - embedding_type) - p.run() - filenames = tf.io.gfile.glob( - output_tfrecord + '-?????-of-?????') - actual_output = [] - record_iterator = tf.data.TFRecordDataset( - tf.convert_to_tensor(filenames)).as_numpy_iterator() - for record in record_iterator: - actual_output.append(record) - self.assertEqual(len(actual_output), 1) - self.assert_expected_example(tf.train.Example.FromString( - actual_output[0])) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/create_ava_actions_tf_record.py b/research/object_detection/dataset_tools/create_ava_actions_tf_record.py deleted file mode 100644 index a27001d879c..00000000000 --- a/research/object_detection/dataset_tools/create_ava_actions_tf_record.py +++ /dev/null @@ -1,540 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Code to download and parse the AVA Actions dataset for TensorFlow models. - -The [AVA Actions data set]( -https://research.google.com/ava/index.html) -is a dataset for human action recognition. - -This script downloads the annotations and prepares data from similar annotations -if local video files are available. The video files can be downloaded -from the following website: -https://github.com/cvdfoundation/ava-dataset - -Prior to running this script, please run download_and_preprocess_ava.sh to -download input videos. - -Running this code as a module generates the data set on disk. First, the -required files are downloaded (_download_data) which enables constructing the -label map. Then (in generate_examples), for each split in the data set, the -metadata and image frames are generated from the annotations for each sequence -example (_generate_examples). The data set is written to disk as a set of -numbered TFRecord files. - -Generating the data on disk can take considerable time and disk space. -(Image compression quality is the primary determiner of disk usage. - -If using the Tensorflow Object Detection API, set the input_type field -in the input_reader to TF_SEQUENCE_EXAMPLE. If using this script to generate -data for Context R-CNN scripts, the --examples_for_context flag should be -set to true, so that properly-formatted tf.example objects are written to disk. - -This data is structured for per-clip action classification where images is -the sequence of images and labels are a one-hot encoded value. See -as_dataset() for more details. - -Note that the number of videos changes in the data set over time, so it will -likely be necessary to change the expected number of examples. - -The argument video_path_format_string expects a value as such: - '/path/to/videos/{0}' - -""" -import collections -import contextlib -import csv -import glob -import hashlib -import os -import random -import sys -import zipfile - -from absl import app -from absl import flags -from absl import logging -import cv2 -from six.moves import range -from six.moves import urllib -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import seq_example_util -from object_detection.utils import dataset_util -from object_detection.utils import label_map_util - - -POSSIBLE_TIMESTAMPS = range(902, 1798) -ANNOTATION_URL = 'https://research.google.com/ava/download/ava_v2.2.zip' -SECONDS_TO_MILLI = 1000 -FILEPATTERN = 'ava_actions_%s_1fps_rgb' -SPLITS = { - 'train': { - 'shards': 1000, - 'examples': 862663, - 'csv': '', - 'excluded-csv': '' - }, - 'val': { - 'shards': 100, - 'examples': 243029, - 'csv': '', - 'excluded-csv': '' - }, - # Test doesn't have ground truth, so TF Records can't be created - 'test': { - 'shards': 100, - 'examples': 0, - 'csv': '', - 'excluded-csv': '' - } -} - -NUM_CLASSES = 80 - - -def feature_list_feature(value): - return tf.train.FeatureList(feature=value) - - -class Ava(object): - """Generates and loads the AVA Actions 2.2 data set.""" - - def __init__(self, path_to_output_dir, path_to_data_download): - if not path_to_output_dir: - raise ValueError('You must supply the path to the data directory.') - self.path_to_data_download = path_to_data_download - self.path_to_output_dir = path_to_output_dir - - def generate_and_write_records(self, - splits_to_process='train,val,test', - video_path_format_string=None, - seconds_per_sequence=10, - hop_between_sequences=10, - examples_for_context=False): - """Downloads data and generates sharded TFRecords. - - Downloads the data files, generates metadata, and processes the metadata - with MediaPipe to produce tf.SequenceExamples for training. The resulting - files can be read with as_dataset(). After running this function the - original data files can be deleted. - - Args: - splits_to_process: csv string of which splits to process. Allows - providing a custom CSV with the CSV flag. The original data is still - downloaded to generate the label_map. - video_path_format_string: The format string for the path to local files. - seconds_per_sequence: The length of each sequence, in seconds. - hop_between_sequences: The gap between the centers of - successive sequences. - examples_for_context: Whether to generate sequence examples with context - for context R-CNN. - """ - example_function = self._generate_sequence_examples - if examples_for_context: - example_function = self._generate_examples - - logging.info('Downloading data.') - download_output = self._download_data() - for key in splits_to_process.split(','): - logging.info('Generating examples for split: %s', key) - all_metadata = list(example_function( - download_output[0][key][0], download_output[0][key][1], - download_output[1], seconds_per_sequence, hop_between_sequences, - video_path_format_string)) - logging.info('An example of the metadata: ') - logging.info(all_metadata[0]) - random.seed(47) - random.shuffle(all_metadata) - shards = SPLITS[key]['shards'] - shard_names = [os.path.join( - self.path_to_output_dir, FILEPATTERN % key + '-%05d-of-%05d' % ( - i, shards)) for i in range(shards)] - writers = [tf.io.TFRecordWriter(shard) for shard in shard_names] - with _close_on_exit(writers) as writers: - for i, seq_ex in enumerate(all_metadata): - writers[i % len(writers)].write(seq_ex.SerializeToString()) - logging.info('Data extraction complete.') - - def _generate_sequence_examples(self, annotation_file, excluded_file, - label_map, seconds_per_sequence, - hop_between_sequences, - video_path_format_string): - """For each row in the annotation CSV, generates corresponding examples. - - When iterating through frames for a single sequence example, skips over - excluded frames. When moving to the next sequence example, also skips over - excluded frames as if they don't exist. Generates equal-length sequence - examples, each with length seconds_per_sequence (1 fps) and gaps of - hop_between_sequences frames (and seconds) between them, possible greater - due to excluded frames. - - Args: - annotation_file: path to the file of AVA CSV annotations. - excluded_file: path to a CSV file of excluded timestamps for each video. - label_map: an {int: string} label map. - seconds_per_sequence: The number of seconds per example in each example. - hop_between_sequences: The hop between sequences. If less than - seconds_per_sequence, will overlap. - video_path_format_string: File path format to glob video files. - - Yields: - Each prepared tf.SequenceExample of metadata also containing video frames - """ - fieldnames = ['id', 'timestamp_seconds', 'xmin', 'ymin', 'xmax', 'ymax', - 'action_label'] - frame_excluded = {} - # create a sparse, nested map of videos and frame indices. - with open(excluded_file, 'r') as excluded: - reader = csv.reader(excluded) - for row in reader: - frame_excluded[(row[0], int(float(row[1])))] = True - with open(annotation_file, 'r') as annotations: - reader = csv.DictReader(annotations, fieldnames) - frame_annotations = collections.defaultdict(list) - ids = set() - # aggreggate by video and timestamp: - for row in reader: - ids.add(row['id']) - key = (row['id'], int(float(row['timestamp_seconds']))) - frame_annotations[key].append(row) - # for each video, find aggregates near each sampled frame.: - logging.info('Generating metadata...') - media_num = 1 - for media_id in ids: - logging.info('%d/%d, ignore warnings.\n', media_num, len(ids)) - media_num += 1 - - filepath = glob.glob( - video_path_format_string.format(media_id) + '*')[0] - cur_vid = cv2.VideoCapture(filepath) - width = cur_vid.get(cv2.CAP_PROP_FRAME_WIDTH) - height = cur_vid.get(cv2.CAP_PROP_FRAME_HEIGHT) - middle_frame_time = POSSIBLE_TIMESTAMPS[0] - while middle_frame_time < POSSIBLE_TIMESTAMPS[-1]: - start_time = middle_frame_time - seconds_per_sequence // 2 - ( - 0 if seconds_per_sequence % 2 == 0 else 1) - end_time = middle_frame_time + (seconds_per_sequence // 2) - - total_boxes = [] - total_labels = [] - total_label_strings = [] - total_images = [] - total_source_ids = [] - total_confidences = [] - total_is_annotated = [] - windowed_timestamp = start_time - - while windowed_timestamp < end_time: - if (media_id, windowed_timestamp) in frame_excluded: - end_time += 1 - windowed_timestamp += 1 - logging.info('Ignoring and skipping excluded frame.') - continue - - cur_vid.set(cv2.CAP_PROP_POS_MSEC, - (windowed_timestamp) * SECONDS_TO_MILLI) - _, image = cur_vid.read() - _, buffer = cv2.imencode('.jpg', image) - - bufstring = buffer.tostring() - total_images.append(bufstring) - source_id = str(windowed_timestamp) + '_' + media_id - total_source_ids.append(source_id) - total_is_annotated.append(1) - - boxes = [] - labels = [] - label_strings = [] - confidences = [] - for row in frame_annotations[(media_id, windowed_timestamp)]: - if len(row) > 2 and int(row['action_label']) in label_map: - boxes.append([float(row['ymin']), float(row['xmin']), - float(row['ymax']), float(row['xmax'])]) - labels.append(int(row['action_label'])) - label_strings.append(label_map[int(row['action_label'])]) - confidences.append(1) - else: - logging.warning('Unknown label: %s', row['action_label']) - - total_boxes.append(boxes) - total_labels.append(labels) - total_label_strings.append(label_strings) - total_confidences.append(confidences) - windowed_timestamp += 1 - - if total_boxes: - yield seq_example_util.make_sequence_example( - 'AVA', media_id, total_images, int(height), int(width), 'jpeg', - total_source_ids, None, total_is_annotated, total_boxes, - total_label_strings, use_strs_for_source_id=True) - - # Move middle_time_frame, skipping excluded frames - frames_mv = 0 - frames_excluded_count = 0 - while (frames_mv < hop_between_sequences + frames_excluded_count - and middle_frame_time + frames_mv < POSSIBLE_TIMESTAMPS[-1]): - frames_mv += 1 - if (media_id, windowed_timestamp + frames_mv) in frame_excluded: - frames_excluded_count += 1 - middle_frame_time += frames_mv - - cur_vid.release() - - def _generate_examples(self, annotation_file, excluded_file, label_map, - seconds_per_sequence, hop_between_sequences, - video_path_format_string): - """For each row in the annotation CSV, generates examples. - - When iterating through frames for a single example, skips - over excluded frames. Generates equal-length sequence examples, each with - length seconds_per_sequence (1 fps) and gaps of hop_between_sequences - frames (and seconds) between them, possible greater due to excluded frames. - - Args: - annotation_file: path to the file of AVA CSV annotations. - excluded_file: path to a CSV file of excluded timestamps for each video. - label_map: an {int: string} label map. - seconds_per_sequence: The number of seconds per example in each example. - hop_between_sequences: The hop between sequences. If less than - seconds_per_sequence, will overlap. - video_path_format_string: File path format to glob video files. - - Yields: - Each prepared tf.Example of metadata also containing video frames - """ - del seconds_per_sequence - del hop_between_sequences - fieldnames = ['id', 'timestamp_seconds', 'xmin', 'ymin', 'xmax', 'ymax', - 'action_label'] - frame_excluded = {} - # create a sparse, nested map of videos and frame indices. - with open(excluded_file, 'r') as excluded: - reader = csv.reader(excluded) - for row in reader: - frame_excluded[(row[0], int(float(row[1])))] = True - with open(annotation_file, 'r') as annotations: - reader = csv.DictReader(annotations, fieldnames) - frame_annotations = collections.defaultdict(list) - ids = set() - # aggreggate by video and timestamp: - for row in reader: - ids.add(row['id']) - key = (row['id'], int(float(row['timestamp_seconds']))) - frame_annotations[key].append(row) - # for each video, find aggreggates near each sampled frame.: - logging.info('Generating metadata...') - media_num = 1 - for media_id in ids: - logging.info('%d/%d, ignore warnings.\n', media_num, len(ids)) - media_num += 1 - - filepath = glob.glob( - video_path_format_string.format(media_id) + '*')[0] - cur_vid = cv2.VideoCapture(filepath) - width = cur_vid.get(cv2.CAP_PROP_FRAME_WIDTH) - height = cur_vid.get(cv2.CAP_PROP_FRAME_HEIGHT) - middle_frame_time = POSSIBLE_TIMESTAMPS[0] - total_non_excluded = 0 - while middle_frame_time < POSSIBLE_TIMESTAMPS[-1]: - if (media_id, middle_frame_time) not in frame_excluded: - total_non_excluded += 1 - middle_frame_time += 1 - - middle_frame_time = POSSIBLE_TIMESTAMPS[0] - cur_frame_num = 0 - while middle_frame_time < POSSIBLE_TIMESTAMPS[-1]: - cur_vid.set(cv2.CAP_PROP_POS_MSEC, - middle_frame_time * SECONDS_TO_MILLI) - _, image = cur_vid.read() - _, buffer = cv2.imencode('.jpg', image) - - bufstring = buffer.tostring() - - if (media_id, middle_frame_time) in frame_excluded: - middle_frame_time += 1 - logging.info('Ignoring and skipping excluded frame.') - continue - - cur_frame_num += 1 - source_id = str(middle_frame_time) + '_' + media_id - - xmins = [] - xmaxs = [] - ymins = [] - ymaxs = [] - areas = [] - labels = [] - label_strings = [] - confidences = [] - for row in frame_annotations[(media_id, middle_frame_time)]: - if len(row) > 2 and int(row['action_label']) in label_map: - xmins.append(float(row['xmin'])) - xmaxs.append(float(row['xmax'])) - ymins.append(float(row['ymin'])) - ymaxs.append(float(row['ymax'])) - areas.append(float((xmaxs[-1] - xmins[-1]) * - (ymaxs[-1] - ymins[-1])) / 2) - labels.append(int(row['action_label'])) - label_strings.append(label_map[int(row['action_label'])]) - confidences.append(1) - else: - logging.warning('Unknown label: %s', row['action_label']) - - middle_frame_time += 1/3 - if abs(middle_frame_time - round(middle_frame_time) < 0.0001): - middle_frame_time = round(middle_frame_time) - - key = hashlib.sha256(bufstring).hexdigest() - date_captured_feature = ( - '2020-06-17 00:%02d:%02d' % ((middle_frame_time - 900)*3 // 60, - (middle_frame_time - 900)*3 % 60)) - context_feature_dict = { - 'image/height': - dataset_util.int64_feature(int(height)), - 'image/width': - dataset_util.int64_feature(int(width)), - 'image/format': - dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/source_id': - dataset_util.bytes_feature(source_id.encode('utf8')), - 'image/filename': - dataset_util.bytes_feature(source_id.encode('utf8')), - 'image/encoded': - dataset_util.bytes_feature(bufstring), - 'image/key/sha256': - dataset_util.bytes_feature(key.encode('utf8')), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(xmins), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(xmaxs), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(ymins), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(ymaxs), - 'image/object/area': - dataset_util.float_list_feature(areas), - 'image/object/class/label': - dataset_util.int64_list_feature(labels), - 'image/object/class/text': - dataset_util.bytes_list_feature(label_strings), - 'image/location': - dataset_util.bytes_feature(media_id.encode('utf8')), - 'image/date_captured': - dataset_util.bytes_feature( - date_captured_feature.encode('utf8')), - 'image/seq_num_frames': - dataset_util.int64_feature(total_non_excluded), - 'image/seq_frame_num': - dataset_util.int64_feature(cur_frame_num), - 'image/seq_id': - dataset_util.bytes_feature(media_id.encode('utf8')), - } - - yield tf.train.Example( - features=tf.train.Features(feature=context_feature_dict)) - - cur_vid.release() - - def _download_data(self): - """Downloads and extracts data if not already available.""" - if sys.version_info >= (3, 0): - urlretrieve = urllib.request.urlretrieve - else: - urlretrieve = urllib.request.urlretrieve - logging.info('Creating data directory.') - tf.io.gfile.makedirs(self.path_to_data_download) - logging.info('Downloading annotations.') - paths = {} - - zip_path = os.path.join(self.path_to_data_download, - ANNOTATION_URL.split('/')[-1]) - urlretrieve(ANNOTATION_URL, zip_path) - with zipfile.ZipFile(zip_path, 'r') as zip_ref: - zip_ref.extractall(self.path_to_data_download) - for split in ['train', 'test', 'val']: - csv_path = os.path.join(self.path_to_data_download, - 'ava_%s_v2.2.csv' % split) - excl_name = 'ava_%s_excluded_timestamps_v2.2.csv' % split - excluded_csv_path = os.path.join(self.path_to_data_download, excl_name) - SPLITS[split]['csv'] = csv_path - SPLITS[split]['excluded-csv'] = excluded_csv_path - paths[split] = (csv_path, excluded_csv_path) - - label_map = self.get_label_map(os.path.join( - self.path_to_data_download, - 'ava_action_list_v2.2_for_activitynet_2019.pbtxt')) - return paths, label_map - - def get_label_map(self, path): - """Parses a label map into {integer:string} format.""" - label_map_dict = label_map_util.get_label_map_dict(path) - label_map_dict = {v: bytes(k, 'utf8') for k, v in label_map_dict.items()} - logging.info(label_map_dict) - return label_map_dict - - -@contextlib.contextmanager -def _close_on_exit(writers): - """Call close on all writers on exit.""" - try: - yield writers - finally: - for writer in writers: - writer.close() - - -def main(argv): - if len(argv) > 1: - raise app.UsageError('Too many command-line arguments.') - Ava(flags.FLAGS.path_to_output_dir, - flags.FLAGS.path_to_download_data).generate_and_write_records( - flags.FLAGS.splits_to_process, - flags.FLAGS.video_path_format_string, - flags.FLAGS.seconds_per_sequence, - flags.FLAGS.hop_between_sequences, - flags.FLAGS.examples_for_context) - -if __name__ == '__main__': - flags.DEFINE_string('path_to_download_data', - '', - 'Path to directory to download data to.') - flags.DEFINE_string('path_to_output_dir', - '', - 'Path to directory to write data to.') - flags.DEFINE_string('splits_to_process', - 'train,val', - 'Process these splits. Useful for custom data splits.') - flags.DEFINE_string('video_path_format_string', - None, - 'The format string for the path to local video files. ' - 'Uses the Python string.format() syntax with possible ' - 'arguments of {video}, {start}, {end}, {label_name}, and ' - '{split}, corresponding to columns of the data csvs.') - flags.DEFINE_integer('seconds_per_sequence', - 10, - 'The number of seconds per example in each example.' - 'Always 1 when examples_for_context is True.') - flags.DEFINE_integer('hop_between_sequences', - 10, - 'The hop between sequences. If less than ' - 'seconds_per_sequence, will overlap. Always 1 when ' - 'examples_for_context is True.') - flags.DEFINE_boolean('examples_for_context', - False, - 'Whether to generate examples instead of sequence ' - 'examples. If true, will generate tf.Example objects ' - 'for use in Context R-CNN.') - app.run(main) diff --git a/research/object_detection/dataset_tools/create_coco_tf_record.py b/research/object_detection/dataset_tools/create_coco_tf_record.py deleted file mode 100644 index 2703c427e9b..00000000000 --- a/research/object_detection/dataset_tools/create_coco_tf_record.py +++ /dev/null @@ -1,518 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Convert raw COCO dataset to TFRecord for object_detection. - -This tool supports data generation for object detection (boxes, masks), -keypoint detection, and DensePose. - -Please note that this tool creates sharded output files. - -Example usage: - python create_coco_tf_record.py --logtostderr \ - --train_image_dir="${TRAIN_IMAGE_DIR}" \ - --val_image_dir="${VAL_IMAGE_DIR}" \ - --test_image_dir="${TEST_IMAGE_DIR}" \ - --train_annotations_file="${TRAIN_ANNOTATIONS_FILE}" \ - --val_annotations_file="${VAL_ANNOTATIONS_FILE}" \ - --testdev_annotations_file="${TESTDEV_ANNOTATIONS_FILE}" \ - --output_dir="${OUTPUT_DIR}" -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import hashlib -import io -import json -import logging -import os -import contextlib2 -import numpy as np -import PIL.Image - -from pycocotools import mask -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import tf_record_creation_util -from object_detection.utils import dataset_util -from object_detection.utils import label_map_util - -flags = tf.app.flags -tf.flags.DEFINE_boolean( - 'include_masks', False, 'Whether to include instance segmentations masks ' - '(PNG encoded) in the result. default: False.') -tf.flags.DEFINE_string('train_image_dir', '', 'Training image directory.') -tf.flags.DEFINE_string('val_image_dir', '', 'Validation image directory.') -tf.flags.DEFINE_string('test_image_dir', '', 'Test image directory.') -tf.flags.DEFINE_string('train_annotations_file', '', - 'Training annotations JSON file.') -tf.flags.DEFINE_string('val_annotations_file', '', - 'Validation annotations JSON file.') -tf.flags.DEFINE_string('testdev_annotations_file', '', - 'Test-dev annotations JSON file.') -tf.flags.DEFINE_string('train_keypoint_annotations_file', '', - 'Training annotations JSON file.') -tf.flags.DEFINE_string('val_keypoint_annotations_file', '', - 'Validation annotations JSON file.') -# DensePose is only available for coco 2014. -tf.flags.DEFINE_string('train_densepose_annotations_file', '', - 'Training annotations JSON file for DensePose.') -tf.flags.DEFINE_string('val_densepose_annotations_file', '', - 'Validation annotations JSON file for DensePose.') -tf.flags.DEFINE_string('output_dir', '/tmp/', 'Output data directory.') -# Whether to only produce images/annotations on person class (for keypoint / -# densepose task). -tf.flags.DEFINE_boolean('remove_non_person_annotations', False, 'Whether to ' - 'remove all annotations for non-person objects.') -tf.flags.DEFINE_boolean('remove_non_person_images', False, 'Whether to ' - 'remove all examples that do not contain a person.') - -FLAGS = flags.FLAGS - -logger = tf.get_logger() -logger.setLevel(logging.INFO) - -_COCO_KEYPOINT_NAMES = [ - b'nose', b'left_eye', b'right_eye', b'left_ear', b'right_ear', - b'left_shoulder', b'right_shoulder', b'left_elbow', b'right_elbow', - b'left_wrist', b'right_wrist', b'left_hip', b'right_hip', - b'left_knee', b'right_knee', b'left_ankle', b'right_ankle' -] - -_COCO_PART_NAMES = [ - b'torso_back', b'torso_front', b'right_hand', b'left_hand', b'left_foot', - b'right_foot', b'right_upper_leg_back', b'left_upper_leg_back', - b'right_upper_leg_front', b'left_upper_leg_front', b'right_lower_leg_back', - b'left_lower_leg_back', b'right_lower_leg_front', b'left_lower_leg_front', - b'left_upper_arm_back', b'right_upper_arm_back', b'left_upper_arm_front', - b'right_upper_arm_front', b'left_lower_arm_back', b'right_lower_arm_back', - b'left_lower_arm_front', b'right_lower_arm_front', b'right_face', - b'left_face', -] - -_DP_PART_ID_OFFSET = 1 - - -def clip_to_unit(x): - return min(max(x, 0.0), 1.0) - - -def create_tf_example(image, - annotations_list, - image_dir, - category_index, - include_masks=False, - keypoint_annotations_dict=None, - densepose_annotations_dict=None, - remove_non_person_annotations=False, - remove_non_person_images=False): - """Converts image and annotations to a tf.Example proto. - - Args: - image: dict with keys: [u'license', u'file_name', u'coco_url', u'height', - u'width', u'date_captured', u'flickr_url', u'id'] - annotations_list: - list of dicts with keys: [u'segmentation', u'area', u'iscrowd', - u'image_id', u'bbox', u'category_id', u'id'] Notice that bounding box - coordinates in the official COCO dataset are given as [x, y, width, - height] tuples using absolute coordinates where x, y represent the - top-left (0-indexed) corner. This function converts to the format - expected by the Tensorflow Object Detection API (which is which is - [ymin, xmin, ymax, xmax] with coordinates normalized relative to image - size). - image_dir: directory containing the image files. - category_index: a dict containing COCO category information keyed by the - 'id' field of each category. See the label_map_util.create_category_index - function. - include_masks: Whether to include instance segmentations masks - (PNG encoded) in the result. default: False. - keypoint_annotations_dict: A dictionary that maps from annotation_id to a - dictionary with keys: [u'keypoints', u'num_keypoints'] represeting the - keypoint information for this person object annotation. If None, then - no keypoint annotations will be populated. - densepose_annotations_dict: A dictionary that maps from annotation_id to a - dictionary with keys: [u'dp_I', u'dp_x', u'dp_y', 'dp_U', 'dp_V'] - representing part surface coordinates. For more information see - http://densepose.org/. - remove_non_person_annotations: Whether to remove any annotations that are - not the "person" class. - remove_non_person_images: Whether to remove any images that do not contain - at least one "person" annotation. - - Returns: - key: SHA256 hash of the image. - example: The converted tf.Example - num_annotations_skipped: Number of (invalid) annotations that were ignored. - num_keypoint_annotation_skipped: Number of keypoint annotations that were - skipped. - num_densepose_annotation_skipped: Number of DensePose annotations that were - skipped. - - Raises: - ValueError: if the image pointed to by data['filename'] is not a valid JPEG - """ - image_height = image['height'] - image_width = image['width'] - filename = image['file_name'] - image_id = image['id'] - - full_path = os.path.join(image_dir, filename) - with tf.gfile.GFile(full_path, 'rb') as fid: - encoded_jpg = fid.read() - encoded_jpg_io = io.BytesIO(encoded_jpg) - image = PIL.Image.open(encoded_jpg_io) - key = hashlib.sha256(encoded_jpg).hexdigest() - - xmin = [] - xmax = [] - ymin = [] - ymax = [] - is_crowd = [] - category_names = [] - category_ids = [] - area = [] - encoded_mask_png = [] - keypoints_x = [] - keypoints_y = [] - keypoints_visibility = [] - keypoints_name = [] - num_keypoints = [] - include_keypoint = keypoint_annotations_dict is not None - num_annotations_skipped = 0 - num_keypoint_annotation_used = 0 - num_keypoint_annotation_skipped = 0 - dp_part_index = [] - dp_x = [] - dp_y = [] - dp_u = [] - dp_v = [] - dp_num_points = [] - densepose_keys = ['dp_I', 'dp_U', 'dp_V', 'dp_x', 'dp_y', 'bbox'] - include_densepose = densepose_annotations_dict is not None - num_densepose_annotation_used = 0 - num_densepose_annotation_skipped = 0 - for object_annotations in annotations_list: - (x, y, width, height) = tuple(object_annotations['bbox']) - if width <= 0 or height <= 0: - num_annotations_skipped += 1 - continue - if x + width > image_width or y + height > image_height: - num_annotations_skipped += 1 - continue - category_id = int(object_annotations['category_id']) - category_name = category_index[category_id]['name'].encode('utf8') - if remove_non_person_annotations and category_name != b'person': - num_annotations_skipped += 1 - continue - xmin.append(float(x) / image_width) - xmax.append(float(x + width) / image_width) - ymin.append(float(y) / image_height) - ymax.append(float(y + height) / image_height) - is_crowd.append(object_annotations['iscrowd']) - category_ids.append(category_id) - category_names.append(category_name) - area.append(object_annotations['area']) - - if include_masks: - run_len_encoding = mask.frPyObjects(object_annotations['segmentation'], - image_height, image_width) - binary_mask = mask.decode(run_len_encoding) - if not object_annotations['iscrowd']: - binary_mask = np.amax(binary_mask, axis=2) - pil_image = PIL.Image.fromarray(binary_mask) - output_io = io.BytesIO() - pil_image.save(output_io, format='PNG') - encoded_mask_png.append(output_io.getvalue()) - - if include_keypoint: - annotation_id = object_annotations['id'] - if annotation_id in keypoint_annotations_dict: - num_keypoint_annotation_used += 1 - keypoint_annotations = keypoint_annotations_dict[annotation_id] - keypoints = keypoint_annotations['keypoints'] - num_kpts = keypoint_annotations['num_keypoints'] - keypoints_x_abs = keypoints[::3] - keypoints_x.extend( - [float(x_abs) / image_width for x_abs in keypoints_x_abs]) - keypoints_y_abs = keypoints[1::3] - keypoints_y.extend( - [float(y_abs) / image_height for y_abs in keypoints_y_abs]) - keypoints_visibility.extend(keypoints[2::3]) - keypoints_name.extend(_COCO_KEYPOINT_NAMES) - num_keypoints.append(num_kpts) - else: - keypoints_x.extend([0.0] * len(_COCO_KEYPOINT_NAMES)) - keypoints_y.extend([0.0] * len(_COCO_KEYPOINT_NAMES)) - keypoints_visibility.extend([0] * len(_COCO_KEYPOINT_NAMES)) - keypoints_name.extend(_COCO_KEYPOINT_NAMES) - num_keypoints.append(0) - - if include_densepose: - annotation_id = object_annotations['id'] - if (annotation_id in densepose_annotations_dict and - all(key in densepose_annotations_dict[annotation_id] - for key in densepose_keys)): - dp_annotations = densepose_annotations_dict[annotation_id] - num_densepose_annotation_used += 1 - dp_num_points.append(len(dp_annotations['dp_I'])) - dp_part_index.extend([int(i - _DP_PART_ID_OFFSET) - for i in dp_annotations['dp_I']]) - # DensePose surface coordinates are defined on a [256, 256] grid - # relative to each instance box (i.e. absolute coordinates in range - # [0., 256.]). The following converts the coordinates - # so that they are expressed in normalized image coordinates. - dp_x_box_rel = [ - clip_to_unit(val / 256.) for val in dp_annotations['dp_x']] - dp_x_norm = [(float(x) + x_box_rel * width) / image_width - for x_box_rel in dp_x_box_rel] - dp_y_box_rel = [ - clip_to_unit(val / 256.) for val in dp_annotations['dp_y']] - dp_y_norm = [(float(y) + y_box_rel * height) / image_height - for y_box_rel in dp_y_box_rel] - dp_x.extend(dp_x_norm) - dp_y.extend(dp_y_norm) - dp_u.extend(dp_annotations['dp_U']) - dp_v.extend(dp_annotations['dp_V']) - else: - dp_num_points.append(0) - - if (remove_non_person_images and - not any(name == b'person' for name in category_names)): - return (key, None, num_annotations_skipped, - num_keypoint_annotation_skipped, num_densepose_annotation_skipped) - feature_dict = { - 'image/height': - dataset_util.int64_feature(image_height), - 'image/width': - dataset_util.int64_feature(image_width), - 'image/filename': - dataset_util.bytes_feature(filename.encode('utf8')), - 'image/source_id': - dataset_util.bytes_feature(str(image_id).encode('utf8')), - 'image/key/sha256': - dataset_util.bytes_feature(key.encode('utf8')), - 'image/encoded': - dataset_util.bytes_feature(encoded_jpg), - 'image/format': - dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/object/bbox/xmin': - dataset_util.float_list_feature(xmin), - 'image/object/bbox/xmax': - dataset_util.float_list_feature(xmax), - 'image/object/bbox/ymin': - dataset_util.float_list_feature(ymin), - 'image/object/bbox/ymax': - dataset_util.float_list_feature(ymax), - 'image/object/class/text': - dataset_util.bytes_list_feature(category_names), - 'image/object/is_crowd': - dataset_util.int64_list_feature(is_crowd), - 'image/object/area': - dataset_util.float_list_feature(area), - } - if include_masks: - feature_dict['image/object/mask'] = ( - dataset_util.bytes_list_feature(encoded_mask_png)) - if include_keypoint: - feature_dict['image/object/keypoint/x'] = ( - dataset_util.float_list_feature(keypoints_x)) - feature_dict['image/object/keypoint/y'] = ( - dataset_util.float_list_feature(keypoints_y)) - feature_dict['image/object/keypoint/num'] = ( - dataset_util.int64_list_feature(num_keypoints)) - feature_dict['image/object/keypoint/visibility'] = ( - dataset_util.int64_list_feature(keypoints_visibility)) - feature_dict['image/object/keypoint/text'] = ( - dataset_util.bytes_list_feature(keypoints_name)) - num_keypoint_annotation_skipped = ( - len(keypoint_annotations_dict) - num_keypoint_annotation_used) - if include_densepose: - feature_dict['image/object/densepose/num'] = ( - dataset_util.int64_list_feature(dp_num_points)) - feature_dict['image/object/densepose/part_index'] = ( - dataset_util.int64_list_feature(dp_part_index)) - feature_dict['image/object/densepose/x'] = ( - dataset_util.float_list_feature(dp_x)) - feature_dict['image/object/densepose/y'] = ( - dataset_util.float_list_feature(dp_y)) - feature_dict['image/object/densepose/u'] = ( - dataset_util.float_list_feature(dp_u)) - feature_dict['image/object/densepose/v'] = ( - dataset_util.float_list_feature(dp_v)) - num_densepose_annotation_skipped = ( - len(densepose_annotations_dict) - num_densepose_annotation_used) - - example = tf.train.Example(features=tf.train.Features(feature=feature_dict)) - return (key, example, num_annotations_skipped, - num_keypoint_annotation_skipped, num_densepose_annotation_skipped) - - -def _create_tf_record_from_coco_annotations(annotations_file, image_dir, - output_path, include_masks, - num_shards, - keypoint_annotations_file='', - densepose_annotations_file='', - remove_non_person_annotations=False, - remove_non_person_images=False): - """Loads COCO annotation json files and converts to tf.Record format. - - Args: - annotations_file: JSON file containing bounding box annotations. - image_dir: Directory containing the image files. - output_path: Path to output tf.Record file. - include_masks: Whether to include instance segmentations masks - (PNG encoded) in the result. default: False. - num_shards: number of output file shards. - keypoint_annotations_file: JSON file containing the person keypoint - annotations. If empty, then no person keypoint annotations will be - generated. - densepose_annotations_file: JSON file containing the DensePose annotations. - If empty, then no DensePose annotations will be generated. - remove_non_person_annotations: Whether to remove any annotations that are - not the "person" class. - remove_non_person_images: Whether to remove any images that do not contain - at least one "person" annotation. - """ - with contextlib2.ExitStack() as tf_record_close_stack, \ - tf.gfile.GFile(annotations_file, 'r') as fid: - output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords( - tf_record_close_stack, output_path, num_shards) - groundtruth_data = json.load(fid) - images = groundtruth_data['images'] - category_index = label_map_util.create_category_index( - groundtruth_data['categories']) - - annotations_index = {} - if 'annotations' in groundtruth_data: - logging.info('Found groundtruth annotations. Building annotations index.') - for annotation in groundtruth_data['annotations']: - image_id = annotation['image_id'] - if image_id not in annotations_index: - annotations_index[image_id] = [] - annotations_index[image_id].append(annotation) - missing_annotation_count = 0 - for image in images: - image_id = image['id'] - if image_id not in annotations_index: - missing_annotation_count += 1 - annotations_index[image_id] = [] - logging.info('%d images are missing annotations.', - missing_annotation_count) - - keypoint_annotations_index = {} - if keypoint_annotations_file: - with tf.gfile.GFile(keypoint_annotations_file, 'r') as kid: - keypoint_groundtruth_data = json.load(kid) - if 'annotations' in keypoint_groundtruth_data: - for annotation in keypoint_groundtruth_data['annotations']: - image_id = annotation['image_id'] - if image_id not in keypoint_annotations_index: - keypoint_annotations_index[image_id] = {} - keypoint_annotations_index[image_id][annotation['id']] = annotation - - densepose_annotations_index = {} - if densepose_annotations_file: - with tf.gfile.GFile(densepose_annotations_file, 'r') as fid: - densepose_groundtruth_data = json.load(fid) - if 'annotations' in densepose_groundtruth_data: - for annotation in densepose_groundtruth_data['annotations']: - image_id = annotation['image_id'] - if image_id not in densepose_annotations_index: - densepose_annotations_index[image_id] = {} - densepose_annotations_index[image_id][annotation['id']] = annotation - - total_num_annotations_skipped = 0 - total_num_keypoint_annotations_skipped = 0 - total_num_densepose_annotations_skipped = 0 - for idx, image in enumerate(images): - if idx % 100 == 0: - logging.info('On image %d of %d', idx, len(images)) - annotations_list = annotations_index[image['id']] - keypoint_annotations_dict = None - if keypoint_annotations_file: - keypoint_annotations_dict = {} - if image['id'] in keypoint_annotations_index: - keypoint_annotations_dict = keypoint_annotations_index[image['id']] - densepose_annotations_dict = None - if densepose_annotations_file: - densepose_annotations_dict = {} - if image['id'] in densepose_annotations_index: - densepose_annotations_dict = densepose_annotations_index[image['id']] - (_, tf_example, num_annotations_skipped, num_keypoint_annotations_skipped, - num_densepose_annotations_skipped) = create_tf_example( - image, annotations_list, image_dir, category_index, include_masks, - keypoint_annotations_dict, densepose_annotations_dict, - remove_non_person_annotations, remove_non_person_images) - total_num_annotations_skipped += num_annotations_skipped - total_num_keypoint_annotations_skipped += num_keypoint_annotations_skipped - total_num_densepose_annotations_skipped += ( - num_densepose_annotations_skipped) - shard_idx = idx % num_shards - if tf_example: - output_tfrecords[shard_idx].write(tf_example.SerializeToString()) - logging.info('Finished writing, skipped %d annotations.', - total_num_annotations_skipped) - if keypoint_annotations_file: - logging.info('Finished writing, skipped %d keypoint annotations.', - total_num_keypoint_annotations_skipped) - if densepose_annotations_file: - logging.info('Finished writing, skipped %d DensePose annotations.', - total_num_densepose_annotations_skipped) - - -def main(_): - assert FLAGS.train_image_dir, '`train_image_dir` missing.' - assert FLAGS.val_image_dir, '`val_image_dir` missing.' - assert FLAGS.test_image_dir, '`test_image_dir` missing.' - assert FLAGS.train_annotations_file, '`train_annotations_file` missing.' - assert FLAGS.val_annotations_file, '`val_annotations_file` missing.' - assert FLAGS.testdev_annotations_file, '`testdev_annotations_file` missing.' - - if not tf.gfile.IsDirectory(FLAGS.output_dir): - tf.gfile.MakeDirs(FLAGS.output_dir) - train_output_path = os.path.join(FLAGS.output_dir, 'coco_train.record') - val_output_path = os.path.join(FLAGS.output_dir, 'coco_val.record') - testdev_output_path = os.path.join(FLAGS.output_dir, 'coco_testdev.record') - - _create_tf_record_from_coco_annotations( - FLAGS.train_annotations_file, - FLAGS.train_image_dir, - train_output_path, - FLAGS.include_masks, - num_shards=100, - keypoint_annotations_file=FLAGS.train_keypoint_annotations_file, - densepose_annotations_file=FLAGS.train_densepose_annotations_file, - remove_non_person_annotations=FLAGS.remove_non_person_annotations, - remove_non_person_images=FLAGS.remove_non_person_images) - _create_tf_record_from_coco_annotations( - FLAGS.val_annotations_file, - FLAGS.val_image_dir, - val_output_path, - FLAGS.include_masks, - num_shards=50, - keypoint_annotations_file=FLAGS.val_keypoint_annotations_file, - densepose_annotations_file=FLAGS.val_densepose_annotations_file, - remove_non_person_annotations=FLAGS.remove_non_person_annotations, - remove_non_person_images=FLAGS.remove_non_person_images) - _create_tf_record_from_coco_annotations( - FLAGS.testdev_annotations_file, - FLAGS.test_image_dir, - testdev_output_path, - FLAGS.include_masks, - num_shards=50) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/dataset_tools/create_coco_tf_record_test.py b/research/object_detection/dataset_tools/create_coco_tf_record_test.py deleted file mode 100644 index 659142b7b70..00000000000 --- a/research/object_detection/dataset_tools/create_coco_tf_record_test.py +++ /dev/null @@ -1,497 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Test for create_coco_tf_record.py.""" - -import io -import json -import os - -import numpy as np -import PIL.Image -import six -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import create_coco_tf_record - - -class CreateCocoTFRecordTest(tf.test.TestCase): - - def _assertProtoEqual(self, proto_field, expectation): - """Helper function to assert if a proto field equals some value. - - Args: - proto_field: The protobuf field to compare. - expectation: The expected value of the protobuf field. - """ - proto_list = [p for p in proto_field] - self.assertListEqual(proto_list, expectation) - - def _assertProtoClose(self, proto_field, expectation): - """Helper function to assert if a proto field nearly equals some value. - - Args: - proto_field: The protobuf field to compare. - expectation: The expected value of the protobuf field. - """ - proto_list = [p for p in proto_field] - self.assertAllClose(proto_list, expectation) - - def test_create_tf_example(self): - image_file_name = 'tmp_image.jpg' - image_data = np.random.rand(256, 256, 3) - tmp_dir = self.get_temp_dir() - save_path = os.path.join(tmp_dir, image_file_name) - image = PIL.Image.fromarray(image_data, 'RGB') - image.save(save_path) - - image = { - 'file_name': image_file_name, - 'height': 256, - 'width': 256, - 'id': 11, - } - - annotations_list = [{ - 'area': .5, - 'iscrowd': False, - 'image_id': 11, - 'bbox': [64, 64, 128, 128], - 'category_id': 2, - 'id': 1000, - }] - - image_dir = tmp_dir - category_index = { - 1: { - 'name': 'dog', - 'id': 1 - }, - 2: { - 'name': 'cat', - 'id': 2 - }, - 3: { - 'name': 'human', - 'id': 3 - } - } - - (_, example, - num_annotations_skipped, _, _) = create_coco_tf_record.create_tf_example( - image, annotations_list, image_dir, category_index) - - self.assertEqual(num_annotations_skipped, 0) - self._assertProtoEqual( - example.features.feature['image/height'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/width'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/filename'].bytes_list.value, - [six.b(image_file_name)]) - self._assertProtoEqual( - example.features.feature['image/source_id'].bytes_list.value, - [six.b(str(image['id']))]) - self._assertProtoEqual( - example.features.feature['image/format'].bytes_list.value, - [six.b('jpeg')]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/class/text'].bytes_list.value, - [six.b('cat')]) - - def test_create_tf_example_with_instance_masks(self): - image_file_name = 'tmp_image.jpg' - image_data = np.random.rand(8, 8, 3) - tmp_dir = self.get_temp_dir() - save_path = os.path.join(tmp_dir, image_file_name) - image = PIL.Image.fromarray(image_data, 'RGB') - image.save(save_path) - - image = { - 'file_name': image_file_name, - 'height': 8, - 'width': 8, - 'id': 11, - } - - annotations_list = [{ - 'area': .5, - 'iscrowd': False, - 'image_id': 11, - 'bbox': [0, 0, 8, 8], - 'segmentation': [[4, 0, 0, 0, 0, 4], [8, 4, 4, 8, 8, 8]], - 'category_id': 1, - 'id': 1000, - }] - - image_dir = tmp_dir - category_index = { - 1: { - 'name': 'dog', - 'id': 1 - }, - } - - (_, example, - num_annotations_skipped, _, _) = create_coco_tf_record.create_tf_example( - image, annotations_list, image_dir, category_index, include_masks=True) - - self.assertEqual(num_annotations_skipped, 0) - self._assertProtoEqual( - example.features.feature['image/height'].int64_list.value, [8]) - self._assertProtoEqual( - example.features.feature['image/width'].int64_list.value, [8]) - self._assertProtoEqual( - example.features.feature['image/filename'].bytes_list.value, - [six.b(image_file_name)]) - self._assertProtoEqual( - example.features.feature['image/source_id'].bytes_list.value, - [six.b(str(image['id']))]) - self._assertProtoEqual( - example.features.feature['image/format'].bytes_list.value, - [six.b('jpeg')]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [1]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [1]) - self._assertProtoEqual( - example.features.feature['image/object/class/text'].bytes_list.value, - [six.b('dog')]) - encoded_mask_pngs = [ - io.BytesIO(encoded_masks) for encoded_masks in example.features.feature[ - 'image/object/mask'].bytes_list.value - ] - pil_masks = [ - np.array(PIL.Image.open(encoded_mask_png)) - for encoded_mask_png in encoded_mask_pngs - ] - self.assertEqual(len(pil_masks), 1) - self.assertAllEqual(pil_masks[0], - [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0], - [1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 0, 1, 1], - [0, 0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1, 1]]) - - def test_create_tf_example_with_keypoints(self): - image_dir = self.get_temp_dir() - image_file_name = 'tmp_image.jpg' - image_data = np.random.randint(low=0, high=256, size=(256, 256, 3)).astype( - np.uint8) - save_path = os.path.join(image_dir, image_file_name) - image = PIL.Image.fromarray(image_data, 'RGB') - image.save(save_path) - - image = { - 'file_name': image_file_name, - 'height': 256, - 'width': 256, - 'id': 11, - } - - min_x, min_y = 64, 64 - max_x, max_y = 128, 128 - keypoints = [] - num_visible_keypoints = 0 - xv = [] - yv = [] - vv = [] - for _ in range(17): - xc = min_x + int(np.random.rand()*(max_x - min_x)) - yc = min_y + int(np.random.rand()*(max_y - min_y)) - vis = np.random.randint(0, 3) - xv.append(xc) - yv.append(yc) - vv.append(vis) - keypoints.extend([xc, yc, vis]) - num_visible_keypoints += (vis > 0) - - annotations_list = [{ - 'area': 0.5, - 'iscrowd': False, - 'image_id': 11, - 'bbox': [64, 64, 128, 128], - 'category_id': 1, - 'id': 1000 - }] - - keypoint_annotations_dict = { - 1000: { - 'keypoints': keypoints, - 'num_keypoints': num_visible_keypoints - } - } - - category_index = { - 1: { - 'name': 'person', - 'id': 1 - } - } - - _, example, _, num_keypoint_annotation_skipped, _ = ( - create_coco_tf_record.create_tf_example( - image, - annotations_list, - image_dir, - category_index, - include_masks=False, - keypoint_annotations_dict=keypoint_annotations_dict)) - - self.assertEqual(num_keypoint_annotation_skipped, 0) - self._assertProtoEqual( - example.features.feature['image/height'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/width'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/filename'].bytes_list.value, - [six.b(image_file_name)]) - self._assertProtoEqual( - example.features.feature['image/source_id'].bytes_list.value, - [six.b(str(image['id']))]) - self._assertProtoEqual( - example.features.feature['image/format'].bytes_list.value, - [six.b('jpeg')]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/class/text'].bytes_list.value, - [six.b('person')]) - self._assertProtoClose( - example.features.feature['image/object/keypoint/x'].float_list.value, - np.array(xv, dtype=np.float32) / 256) - self._assertProtoClose( - example.features.feature['image/object/keypoint/y'].float_list.value, - np.array(yv, dtype=np.float32) / 256) - self._assertProtoEqual( - example.features.feature['image/object/keypoint/text'].bytes_list.value, - create_coco_tf_record._COCO_KEYPOINT_NAMES) - self._assertProtoEqual( - example.features.feature[ - 'image/object/keypoint/visibility'].int64_list.value, vv) - - def test_create_tf_example_with_dense_pose(self): - image_dir = self.get_temp_dir() - image_file_name = 'tmp_image.jpg' - image_data = np.random.randint(low=0, high=256, size=(256, 256, 3)).astype( - np.uint8) - save_path = os.path.join(image_dir, image_file_name) - image = PIL.Image.fromarray(image_data, 'RGB') - image.save(save_path) - - image = { - 'file_name': image_file_name, - 'height': 256, - 'width': 256, - 'id': 11, - } - - min_x, min_y = 64, 64 - max_x, max_y = 128, 128 - keypoints = [] - num_visible_keypoints = 0 - xv = [] - yv = [] - vv = [] - for _ in range(17): - xc = min_x + int(np.random.rand()*(max_x - min_x)) - yc = min_y + int(np.random.rand()*(max_y - min_y)) - vis = np.random.randint(0, 3) - xv.append(xc) - yv.append(yc) - vv.append(vis) - keypoints.extend([xc, yc, vis]) - num_visible_keypoints += (vis > 0) - - annotations_list = [{ - 'area': 0.5, - 'iscrowd': False, - 'image_id': 11, - 'bbox': [64, 64, 128, 128], - 'category_id': 1, - 'id': 1000 - }] - - num_points = 45 - dp_i = np.random.randint(1, 25, (num_points,)).astype(np.float32) - dp_u = np.random.randn(num_points) - dp_v = np.random.randn(num_points) - dp_x = np.random.rand(num_points)*256. - dp_y = np.random.rand(num_points)*256. - densepose_annotations_dict = { - 1000: { - 'dp_I': dp_i, - 'dp_U': dp_u, - 'dp_V': dp_v, - 'dp_x': dp_x, - 'dp_y': dp_y, - 'bbox': [64, 64, 128, 128], - } - } - - category_index = { - 1: { - 'name': 'person', - 'id': 1 - } - } - - _, example, _, _, num_densepose_annotation_skipped = ( - create_coco_tf_record.create_tf_example( - image, - annotations_list, - image_dir, - category_index, - include_masks=False, - densepose_annotations_dict=densepose_annotations_dict)) - - self.assertEqual(num_densepose_annotation_skipped, 0) - self._assertProtoEqual( - example.features.feature['image/height'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/width'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/filename'].bytes_list.value, - [six.b(image_file_name)]) - self._assertProtoEqual( - example.features.feature['image/source_id'].bytes_list.value, - [six.b(str(image['id']))]) - self._assertProtoEqual( - example.features.feature['image/format'].bytes_list.value, - [six.b('jpeg')]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/class/text'].bytes_list.value, - [six.b('person')]) - self._assertProtoEqual( - example.features.feature['image/object/densepose/num'].int64_list.value, - [num_points]) - self.assertAllEqual( - example.features.feature[ - 'image/object/densepose/part_index'].int64_list.value, - dp_i.astype(np.int64) - create_coco_tf_record._DP_PART_ID_OFFSET) - self.assertAllClose( - example.features.feature['image/object/densepose/u'].float_list.value, - dp_u) - self.assertAllClose( - example.features.feature['image/object/densepose/v'].float_list.value, - dp_v) - expected_dp_x = (64 + dp_x * 128. / 256.) / 256. - expected_dp_y = (64 + dp_y * 128. / 256.) / 256. - self.assertAllClose( - example.features.feature['image/object/densepose/x'].float_list.value, - expected_dp_x) - self.assertAllClose( - example.features.feature['image/object/densepose/y'].float_list.value, - expected_dp_y) - - def test_create_sharded_tf_record(self): - tmp_dir = self.get_temp_dir() - image_paths = ['tmp1_image.jpg', 'tmp2_image.jpg'] - for image_path in image_paths: - image_data = np.random.rand(256, 256, 3) - save_path = os.path.join(tmp_dir, image_path) - image = PIL.Image.fromarray(image_data, 'RGB') - image.save(save_path) - - images = [{ - 'file_name': image_paths[0], - 'height': 256, - 'width': 256, - 'id': 11, - }, { - 'file_name': image_paths[1], - 'height': 256, - 'width': 256, - 'id': 12, - }] - - annotations = [{ - 'area': .5, - 'iscrowd': False, - 'image_id': 11, - 'bbox': [64, 64, 128, 128], - 'category_id': 2, - 'id': 1000, - }] - - category_index = [{ - 'name': 'dog', - 'id': 1 - }, { - 'name': 'cat', - 'id': 2 - }, { - 'name': 'human', - 'id': 3 - }] - groundtruth_data = {'images': images, 'annotations': annotations, - 'categories': category_index} - annotation_file = os.path.join(tmp_dir, 'annotation.json') - with open(annotation_file, 'w') as annotation_fid: - json.dump(groundtruth_data, annotation_fid) - - output_path = os.path.join(tmp_dir, 'out.record') - create_coco_tf_record._create_tf_record_from_coco_annotations( - annotation_file, - tmp_dir, - output_path, - False, - 2) - self.assertTrue(os.path.exists(output_path + '-00000-of-00002')) - self.assertTrue(os.path.exists(output_path + '-00001-of-00002')) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/create_kitti_tf_record.py b/research/object_detection/dataset_tools/create_kitti_tf_record.py deleted file mode 100644 index fe4f13ec80b..00000000000 --- a/research/object_detection/dataset_tools/create_kitti_tf_record.py +++ /dev/null @@ -1,310 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Convert raw KITTI detection dataset to TFRecord for object_detection. - -Converts KITTI detection dataset to TFRecords with a standard format allowing - to use this dataset to train object detectors. The raw dataset can be - downloaded from: - http://kitti.is.tue.mpg.de/kitti/data_object_image_2.zip. - http://kitti.is.tue.mpg.de/kitti/data_object_label_2.zip - Permission can be requested at the main website. - - KITTI detection dataset contains 7481 training images. Using this code with - the default settings will set aside the first 500 images as a validation set. - This can be altered using the flags, see details below. - -Example usage: - python object_detection/dataset_tools/create_kitti_tf_record.py \ - --data_dir=/home/user/kitti \ - --output_path=/home/user/kitti.record -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - - -import hashlib -import io -import os - -import numpy as np -import PIL.Image as pil -import tensorflow.compat.v1 as tf - -from object_detection.utils import dataset_util -from object_detection.utils import label_map_util -from object_detection.utils.np_box_ops import iou - -tf.app.flags.DEFINE_string('data_dir', '', 'Location of root directory for the ' - 'data. Folder structure is assumed to be:' - '/training/label_2 (annotations) and' - '/data_object_image_2/training/image_2' - '(images).') -tf.app.flags.DEFINE_string('output_path', '', 'Path to which TFRecord files' - 'will be written. The TFRecord with the training set' - 'will be located at: _train.tfrecord.' - 'And the TFRecord with the validation set will be' - 'located at: _val.tfrecord') -tf.app.flags.DEFINE_string('classes_to_use', 'car,pedestrian,dontcare', - 'Comma separated list of class names that will be' - 'used. Adding the dontcare class will remove all' - 'bboxs in the dontcare regions.') -tf.app.flags.DEFINE_string('label_map_path', 'data/kitti_label_map.pbtxt', - 'Path to label map proto.') -tf.app.flags.DEFINE_integer('validation_set_size', '500', 'Number of images to' - 'be used as a validation set.') -FLAGS = tf.app.flags.FLAGS - - -def convert_kitti_to_tfrecords(data_dir, output_path, classes_to_use, - label_map_path, validation_set_size): - """Convert the KITTI detection dataset to TFRecords. - - Args: - data_dir: The full path to the unzipped folder containing the unzipped data - from data_object_image_2 and data_object_label_2.zip. - Folder structure is assumed to be: data_dir/training/label_2 (annotations) - and data_dir/data_object_image_2/training/image_2 (images). - output_path: The path to which TFRecord files will be written. The TFRecord - with the training set will be located at: _train.tfrecord - And the TFRecord with the validation set will be located at: - _val.tfrecord - classes_to_use: List of strings naming the classes for which data should be - converted. Use the same names as presented in the KIITI README file. - Adding dontcare class will remove all other bounding boxes that overlap - with areas marked as dontcare regions. - label_map_path: Path to label map proto - validation_set_size: How many images should be left as the validation set. - (Ffirst `validation_set_size` examples are selected to be in the - validation set). - """ - label_map_dict = label_map_util.get_label_map_dict(label_map_path) - train_count = 0 - val_count = 0 - - annotation_dir = os.path.join(data_dir, - 'training', - 'label_2') - - image_dir = os.path.join(data_dir, - 'data_object_image_2', - 'training', - 'image_2') - - train_writer = tf.python_io.TFRecordWriter('%s_train.tfrecord'% - output_path) - val_writer = tf.python_io.TFRecordWriter('%s_val.tfrecord'% - output_path) - - images = sorted(tf.gfile.ListDirectory(image_dir)) - for img_name in images: - img_num = int(img_name.split('.')[0]) - is_validation_img = img_num < validation_set_size - img_anno = read_annotation_file(os.path.join(annotation_dir, - str(img_num).zfill(6)+'.txt')) - - image_path = os.path.join(image_dir, img_name) - - # Filter all bounding boxes of this frame that are of a legal class, and - # don't overlap with a dontcare region. - # TODO(talremez) filter out targets that are truncated or heavily occluded. - annotation_for_image = filter_annotations(img_anno, classes_to_use) - - example = prepare_example(image_path, annotation_for_image, label_map_dict) - if is_validation_img: - val_writer.write(example.SerializeToString()) - val_count += 1 - else: - train_writer.write(example.SerializeToString()) - train_count += 1 - - train_writer.close() - val_writer.close() - - -def prepare_example(image_path, annotations, label_map_dict): - """Converts a dictionary with annotations for an image to tf.Example proto. - - Args: - image_path: The complete path to image. - annotations: A dictionary representing the annotation of a single object - that appears in the image. - label_map_dict: A map from string label names to integer ids. - - Returns: - example: The converted tf.Example. - """ - with tf.gfile.GFile(image_path, 'rb') as fid: - encoded_png = fid.read() - encoded_png_io = io.BytesIO(encoded_png) - image = pil.open(encoded_png_io) - image = np.asarray(image) - - key = hashlib.sha256(encoded_png).hexdigest() - - width = int(image.shape[1]) - height = int(image.shape[0]) - - xmin_norm = annotations['2d_bbox_left'] / float(width) - ymin_norm = annotations['2d_bbox_top'] / float(height) - xmax_norm = annotations['2d_bbox_right'] / float(width) - ymax_norm = annotations['2d_bbox_bottom'] / float(height) - - difficult_obj = [0]*len(xmin_norm) - - example = tf.train.Example(features=tf.train.Features(feature={ - 'image/height': dataset_util.int64_feature(height), - 'image/width': dataset_util.int64_feature(width), - 'image/filename': dataset_util.bytes_feature(image_path.encode('utf8')), - 'image/source_id': dataset_util.bytes_feature(image_path.encode('utf8')), - 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), - 'image/encoded': dataset_util.bytes_feature(encoded_png), - 'image/format': dataset_util.bytes_feature('png'.encode('utf8')), - 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin_norm), - 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax_norm), - 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin_norm), - 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax_norm), - 'image/object/class/text': dataset_util.bytes_list_feature( - [x.encode('utf8') for x in annotations['type']]), - 'image/object/class/label': dataset_util.int64_list_feature( - [label_map_dict[x] for x in annotations['type']]), - 'image/object/difficult': dataset_util.int64_list_feature(difficult_obj), - 'image/object/truncated': dataset_util.float_list_feature( - annotations['truncated']), - 'image/object/alpha': dataset_util.float_list_feature( - annotations['alpha']), - 'image/object/3d_bbox/height': dataset_util.float_list_feature( - annotations['3d_bbox_height']), - 'image/object/3d_bbox/width': dataset_util.float_list_feature( - annotations['3d_bbox_width']), - 'image/object/3d_bbox/length': dataset_util.float_list_feature( - annotations['3d_bbox_length']), - 'image/object/3d_bbox/x': dataset_util.float_list_feature( - annotations['3d_bbox_x']), - 'image/object/3d_bbox/y': dataset_util.float_list_feature( - annotations['3d_bbox_y']), - 'image/object/3d_bbox/z': dataset_util.float_list_feature( - annotations['3d_bbox_z']), - 'image/object/3d_bbox/rot_y': dataset_util.float_list_feature( - annotations['3d_bbox_rot_y']), - })) - - return example - - -def filter_annotations(img_all_annotations, used_classes): - """Filters out annotations from the unused classes and dontcare regions. - - Filters out the annotations that belong to classes we do now wish to use and - (optionally) also removes all boxes that overlap with dontcare regions. - - Args: - img_all_annotations: A list of annotation dictionaries. See documentation of - read_annotation_file for more details about the format of the annotations. - used_classes: A list of strings listing the classes we want to keep, if the - list contains "dontcare", all bounding boxes with overlapping with dont - care regions will also be filtered out. - - Returns: - img_filtered_annotations: A list of annotation dictionaries that have passed - the filtering. - """ - - img_filtered_annotations = {} - - # Filter the type of the objects. - relevant_annotation_indices = [ - i for i, x in enumerate(img_all_annotations['type']) if x in used_classes - ] - - for key in img_all_annotations.keys(): - img_filtered_annotations[key] = ( - img_all_annotations[key][relevant_annotation_indices]) - - if 'dontcare' in used_classes: - dont_care_indices = [i for i, - x in enumerate(img_filtered_annotations['type']) - if x == 'dontcare'] - - # bounding box format [y_min, x_min, y_max, x_max] - all_boxes = np.stack([img_filtered_annotations['2d_bbox_top'], - img_filtered_annotations['2d_bbox_left'], - img_filtered_annotations['2d_bbox_bottom'], - img_filtered_annotations['2d_bbox_right']], - axis=1) - - ious = iou(boxes1=all_boxes, - boxes2=all_boxes[dont_care_indices]) - - # Remove all bounding boxes that overlap with a dontcare region. - if ious.size > 0: - boxes_to_remove = np.amax(ious, axis=1) > 0.0 - for key in img_all_annotations.keys(): - img_filtered_annotations[key] = ( - img_filtered_annotations[key][np.logical_not(boxes_to_remove)]) - - return img_filtered_annotations - - -def read_annotation_file(filename): - """Reads a KITTI annotation file. - - Converts a KITTI annotation file into a dictionary containing all the - relevant information. - - Args: - filename: the path to the annotataion text file. - - Returns: - anno: A dictionary with the converted annotation information. See annotation - README file for details on the different fields. - """ - with open(filename) as f: - content = f.readlines() - content = [x.strip().split(' ') for x in content] - - anno = {} - anno['type'] = np.array([x[0].lower() for x in content]) - anno['truncated'] = np.array([float(x[1]) for x in content]) - anno['occluded'] = np.array([int(x[2]) for x in content]) - anno['alpha'] = np.array([float(x[3]) for x in content]) - - anno['2d_bbox_left'] = np.array([float(x[4]) for x in content]) - anno['2d_bbox_top'] = np.array([float(x[5]) for x in content]) - anno['2d_bbox_right'] = np.array([float(x[6]) for x in content]) - anno['2d_bbox_bottom'] = np.array([float(x[7]) for x in content]) - - anno['3d_bbox_height'] = np.array([float(x[8]) for x in content]) - anno['3d_bbox_width'] = np.array([float(x[9]) for x in content]) - anno['3d_bbox_length'] = np.array([float(x[10]) for x in content]) - anno['3d_bbox_x'] = np.array([float(x[11]) for x in content]) - anno['3d_bbox_y'] = np.array([float(x[12]) for x in content]) - anno['3d_bbox_z'] = np.array([float(x[13]) for x in content]) - anno['3d_bbox_rot_y'] = np.array([float(x[14]) for x in content]) - - return anno - - -def main(_): - convert_kitti_to_tfrecords( - data_dir=FLAGS.data_dir, - output_path=FLAGS.output_path, - classes_to_use=FLAGS.classes_to_use.split(','), - label_map_path=FLAGS.label_map_path, - validation_set_size=FLAGS.validation_set_size) - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/dataset_tools/create_kitti_tf_record_test.py b/research/object_detection/dataset_tools/create_kitti_tf_record_test.py deleted file mode 100644 index 606c684ef90..00000000000 --- a/research/object_detection/dataset_tools/create_kitti_tf_record_test.py +++ /dev/null @@ -1,132 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Test for create_kitti_tf_record.py.""" - -import os - -import numpy as np -import PIL.Image -import six -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import create_kitti_tf_record - - -class CreateKittiTFRecordTest(tf.test.TestCase): - - def _assertProtoEqual(self, proto_field, expectation): - """Helper function to assert if a proto field equals some value. - - Args: - proto_field: The protobuf field to compare. - expectation: The expected value of the protobuf field. - """ - proto_list = [p for p in proto_field] - self.assertListEqual(proto_list, expectation) - - def test_dict_to_tf_example(self): - image_file_name = 'tmp_image.jpg' - image_data = np.random.rand(256, 256, 3) - save_path = os.path.join(self.get_temp_dir(), image_file_name) - image = PIL.Image.fromarray(image_data, 'RGB') - image.save(save_path) - - annotations = {} - annotations['2d_bbox_left'] = np.array([64]) - annotations['2d_bbox_top'] = np.array([64]) - annotations['2d_bbox_right'] = np.array([192]) - annotations['2d_bbox_bottom'] = np.array([192]) - annotations['type'] = ['car'] - annotations['truncated'] = np.array([1]) - annotations['alpha'] = np.array([2]) - annotations['3d_bbox_height'] = np.array([10]) - annotations['3d_bbox_width'] = np.array([11]) - annotations['3d_bbox_length'] = np.array([12]) - annotations['3d_bbox_x'] = np.array([13]) - annotations['3d_bbox_y'] = np.array([14]) - annotations['3d_bbox_z'] = np.array([15]) - annotations['3d_bbox_rot_y'] = np.array([4]) - - label_map_dict = { - 'background': 0, - 'car': 1, - } - - example = create_kitti_tf_record.prepare_example( - save_path, - annotations, - label_map_dict) - - self._assertProtoEqual( - example.features.feature['image/height'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/width'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/filename'].bytes_list.value, - [six.b(save_path)]) - self._assertProtoEqual( - example.features.feature['image/source_id'].bytes_list.value, - [six.b(save_path)]) - self._assertProtoEqual( - example.features.feature['image/format'].bytes_list.value, - [six.b('png')]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/class/text'].bytes_list.value, - [six.b('car')]) - self._assertProtoEqual( - example.features.feature['image/object/class/label'].int64_list.value, - [1]) - self._assertProtoEqual( - example.features.feature['image/object/truncated'].float_list.value, - [1]) - self._assertProtoEqual( - example.features.feature['image/object/alpha'].float_list.value, - [2]) - self._assertProtoEqual(example.features.feature[ - 'image/object/3d_bbox/height'].float_list.value, [10]) - self._assertProtoEqual( - example.features.feature['image/object/3d_bbox/width'].float_list.value, - [11]) - self._assertProtoEqual(example.features.feature[ - 'image/object/3d_bbox/length'].float_list.value, [12]) - self._assertProtoEqual( - example.features.feature['image/object/3d_bbox/x'].float_list.value, - [13]) - self._assertProtoEqual( - example.features.feature['image/object/3d_bbox/y'].float_list.value, - [14]) - self._assertProtoEqual( - example.features.feature['image/object/3d_bbox/z'].float_list.value, - [15]) - self._assertProtoEqual( - example.features.feature['image/object/3d_bbox/rot_y'].float_list.value, - [4]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/create_oid_tf_record.py b/research/object_detection/dataset_tools/create_oid_tf_record.py deleted file mode 100644 index 9b35765bacc..00000000000 --- a/research/object_detection/dataset_tools/create_oid_tf_record.py +++ /dev/null @@ -1,117 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Creates TFRecords of Open Images dataset for object detection. - -Example usage: - python object_detection/dataset_tools/create_oid_tf_record.py \ - --input_box_annotations_csv=/path/to/input/annotations-human-bbox.csv \ - --input_image_label_annotations_csv=/path/to/input/annotations-label.csv \ - --input_images_directory=/path/to/input/image_pixels_directory \ - --input_label_map=/path/to/input/labels_bbox_545.labelmap \ - --output_tf_record_path_prefix=/path/to/output/prefix.tfrecord - -CSVs with bounding box annotations and image metadata (including the image URLs) -can be downloaded from the Open Images GitHub repository: -https://github.com/openimages/dataset - -This script will include every image found in the input_images_directory in the -output TFRecord, even if the image has no corresponding bounding box annotations -in the input_annotations_csv. If input_image_label_annotations_csv is specified, -it will add image-level labels as well. Note that the information of whether a -label is positivelly or negativelly verified is NOT added to tfrecord. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os - -import contextlib2 -import pandas as pd -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import oid_tfrecord_creation -from object_detection.dataset_tools import tf_record_creation_util -from object_detection.utils import label_map_util - -tf.flags.DEFINE_string('input_box_annotations_csv', None, - 'Path to CSV containing image bounding box annotations') -tf.flags.DEFINE_string('input_images_directory', None, - 'Directory containing the image pixels ' - 'downloaded from the OpenImages GitHub repository.') -tf.flags.DEFINE_string('input_image_label_annotations_csv', None, - 'Path to CSV containing image-level labels annotations') -tf.flags.DEFINE_string('input_label_map', None, 'Path to the label map proto') -tf.flags.DEFINE_string( - 'output_tf_record_path_prefix', None, - 'Path to the output TFRecord. The shard index and the number of shards ' - 'will be appended for each output shard.') -tf.flags.DEFINE_integer('num_shards', 100, 'Number of TFRecord shards') - -FLAGS = tf.flags.FLAGS - - -def main(_): - tf.logging.set_verbosity(tf.logging.INFO) - - required_flags = [ - 'input_box_annotations_csv', 'input_images_directory', 'input_label_map', - 'output_tf_record_path_prefix' - ] - for flag_name in required_flags: - if not getattr(FLAGS, flag_name): - raise ValueError('Flag --{} is required'.format(flag_name)) - - label_map = label_map_util.get_label_map_dict(FLAGS.input_label_map) - all_box_annotations = pd.read_csv(FLAGS.input_box_annotations_csv) - if FLAGS.input_image_label_annotations_csv: - all_label_annotations = pd.read_csv(FLAGS.input_image_label_annotations_csv) - all_label_annotations.rename( - columns={'Confidence': 'ConfidenceImageLabel'}, inplace=True) - else: - all_label_annotations = None - all_images = tf.gfile.Glob( - os.path.join(FLAGS.input_images_directory, '*.jpg')) - all_image_ids = [os.path.splitext(os.path.basename(v))[0] for v in all_images] - all_image_ids = pd.DataFrame({'ImageID': all_image_ids}) - all_annotations = pd.concat( - [all_box_annotations, all_image_ids, all_label_annotations]) - - tf.logging.log(tf.logging.INFO, 'Found %d images...', len(all_image_ids)) - - with contextlib2.ExitStack() as tf_record_close_stack: - output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords( - tf_record_close_stack, FLAGS.output_tf_record_path_prefix, - FLAGS.num_shards) - - for counter, image_data in enumerate(all_annotations.groupby('ImageID')): - tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 1000, - counter) - - image_id, image_annotations = image_data - # In OID image file names are formed by appending ".jpg" to the image ID. - image_path = os.path.join(FLAGS.input_images_directory, image_id + '.jpg') - with tf.gfile.Open(image_path) as image_file: - encoded_image = image_file.read() - - tf_example = oid_tfrecord_creation.tf_example_from_annotations_data_frame( - image_annotations, label_map, encoded_image) - if tf_example: - shard_idx = int(image_id, 16) % FLAGS.num_shards - output_tfrecords[shard_idx].write(tf_example.SerializeToString()) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/dataset_tools/create_pascal_tf_record.py b/research/object_detection/dataset_tools/create_pascal_tf_record.py deleted file mode 100644 index 8d79a3391c4..00000000000 --- a/research/object_detection/dataset_tools/create_pascal_tf_record.py +++ /dev/null @@ -1,185 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Convert raw PASCAL dataset to TFRecord for object_detection. - -Example usage: - python object_detection/dataset_tools/create_pascal_tf_record.py \ - --data_dir=/home/user/VOCdevkit \ - --year=VOC2012 \ - --output_path=/home/user/pascal.record -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import hashlib -import io -import logging -import os - -from lxml import etree -import PIL.Image -import tensorflow.compat.v1 as tf - -from object_detection.utils import dataset_util -from object_detection.utils import label_map_util - - -flags = tf.app.flags -flags.DEFINE_string('data_dir', '', 'Root directory to raw PASCAL VOC dataset.') -flags.DEFINE_string('set', 'train', 'Convert training set, validation set or ' - 'merged set.') -flags.DEFINE_string('annotations_dir', 'Annotations', - '(Relative) path to annotations directory.') -flags.DEFINE_string('year', 'VOC2007', 'Desired challenge year.') -flags.DEFINE_string('output_path', '', 'Path to output TFRecord') -flags.DEFINE_string('label_map_path', 'data/pascal_label_map.pbtxt', - 'Path to label map proto') -flags.DEFINE_boolean('ignore_difficult_instances', False, 'Whether to ignore ' - 'difficult instances') -FLAGS = flags.FLAGS - -SETS = ['train', 'val', 'trainval', 'test'] -YEARS = ['VOC2007', 'VOC2012', 'merged'] - - -def dict_to_tf_example(data, - dataset_directory, - label_map_dict, - ignore_difficult_instances=False, - image_subdirectory='JPEGImages'): - """Convert XML derived dict to tf.Example proto. - - Notice that this function normalizes the bounding box coordinates provided - by the raw data. - - Args: - data: dict holding PASCAL XML fields for a single image (obtained by - running dataset_util.recursive_parse_xml_to_dict) - dataset_directory: Path to root directory holding PASCAL dataset - label_map_dict: A map from string label names to integers ids. - ignore_difficult_instances: Whether to skip difficult instances in the - dataset (default: False). - image_subdirectory: String specifying subdirectory within the - PASCAL dataset directory holding the actual image data. - - Returns: - example: The converted tf.Example. - - Raises: - ValueError: if the image pointed to by data['filename'] is not a valid JPEG - """ - img_path = os.path.join(data['folder'], image_subdirectory, data['filename']) - full_path = os.path.join(dataset_directory, img_path) - with tf.gfile.GFile(full_path, 'rb') as fid: - encoded_jpg = fid.read() - encoded_jpg_io = io.BytesIO(encoded_jpg) - image = PIL.Image.open(encoded_jpg_io) - if image.format != 'JPEG': - raise ValueError('Image format not JPEG') - key = hashlib.sha256(encoded_jpg).hexdigest() - - width = int(data['size']['width']) - height = int(data['size']['height']) - - xmin = [] - ymin = [] - xmax = [] - ymax = [] - classes = [] - classes_text = [] - truncated = [] - poses = [] - difficult_obj = [] - if 'object' in data: - for obj in data['object']: - difficult = bool(int(obj['difficult'])) - if ignore_difficult_instances and difficult: - continue - - difficult_obj.append(int(difficult)) - - xmin.append(float(obj['bndbox']['xmin']) / width) - ymin.append(float(obj['bndbox']['ymin']) / height) - xmax.append(float(obj['bndbox']['xmax']) / width) - ymax.append(float(obj['bndbox']['ymax']) / height) - classes_text.append(obj['name'].encode('utf8')) - classes.append(label_map_dict[obj['name']]) - truncated.append(int(obj['truncated'])) - poses.append(obj['pose'].encode('utf8')) - - example = tf.train.Example(features=tf.train.Features(feature={ - 'image/height': dataset_util.int64_feature(height), - 'image/width': dataset_util.int64_feature(width), - 'image/filename': dataset_util.bytes_feature( - data['filename'].encode('utf8')), - 'image/source_id': dataset_util.bytes_feature( - data['filename'].encode('utf8')), - 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), - 'image/encoded': dataset_util.bytes_feature(encoded_jpg), - 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/object/bbox/xmin': dataset_util.float_list_feature(xmin), - 'image/object/bbox/xmax': dataset_util.float_list_feature(xmax), - 'image/object/bbox/ymin': dataset_util.float_list_feature(ymin), - 'image/object/bbox/ymax': dataset_util.float_list_feature(ymax), - 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), - 'image/object/class/label': dataset_util.int64_list_feature(classes), - 'image/object/difficult': dataset_util.int64_list_feature(difficult_obj), - 'image/object/truncated': dataset_util.int64_list_feature(truncated), - 'image/object/view': dataset_util.bytes_list_feature(poses), - })) - return example - - -def main(_): - if FLAGS.set not in SETS: - raise ValueError('set must be in : {}'.format(SETS)) - if FLAGS.year not in YEARS: - raise ValueError('year must be in : {}'.format(YEARS)) - - data_dir = FLAGS.data_dir - years = ['VOC2007', 'VOC2012'] - if FLAGS.year != 'merged': - years = [FLAGS.year] - - writer = tf.python_io.TFRecordWriter(FLAGS.output_path) - - label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path) - - for year in years: - logging.info('Reading from PASCAL %s dataset.', year) - examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main', - 'aeroplane_' + FLAGS.set + '.txt') - annotations_dir = os.path.join(data_dir, year, FLAGS.annotations_dir) - examples_list = dataset_util.read_examples_list(examples_path) - for idx, example in enumerate(examples_list): - if idx % 100 == 0: - logging.info('On image %d of %d', idx, len(examples_list)) - path = os.path.join(annotations_dir, example + '.xml') - with tf.gfile.GFile(path, 'r') as fid: - xml_str = fid.read() - xml = etree.fromstring(xml_str) - data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation'] - - tf_example = dict_to_tf_example(data, FLAGS.data_dir, label_map_dict, - FLAGS.ignore_difficult_instances) - writer.write(tf_example.SerializeToString()) - - writer.close() - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/dataset_tools/create_pascal_tf_record_test.py b/research/object_detection/dataset_tools/create_pascal_tf_record_test.py deleted file mode 100644 index c751a1391c5..00000000000 --- a/research/object_detection/dataset_tools/create_pascal_tf_record_test.py +++ /dev/null @@ -1,121 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Test for create_pascal_tf_record.py.""" - -import os - -import numpy as np -import PIL.Image -import six -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import create_pascal_tf_record - - -class CreatePascalTFRecordTest(tf.test.TestCase): - - def _assertProtoEqual(self, proto_field, expectation): - """Helper function to assert if a proto field equals some value. - - Args: - proto_field: The protobuf field to compare. - expectation: The expected value of the protobuf field. - """ - proto_list = [p for p in proto_field] - self.assertListEqual(proto_list, expectation) - - def test_dict_to_tf_example(self): - image_file_name = 'tmp_image.jpg' - image_data = np.random.rand(256, 256, 3) - save_path = os.path.join(self.get_temp_dir(), image_file_name) - image = PIL.Image.fromarray(image_data, 'RGB') - image.save(save_path) - - data = { - 'folder': '', - 'filename': image_file_name, - 'size': { - 'height': 256, - 'width': 256, - }, - 'object': [ - { - 'difficult': 1, - 'bndbox': { - 'xmin': 64, - 'ymin': 64, - 'xmax': 192, - 'ymax': 192, - }, - 'name': 'person', - 'truncated': 0, - 'pose': '', - }, - ], - } - - label_map_dict = { - 'background': 0, - 'person': 1, - 'notperson': 2, - } - - example = create_pascal_tf_record.dict_to_tf_example( - data, self.get_temp_dir(), label_map_dict, image_subdirectory='') - self._assertProtoEqual( - example.features.feature['image/height'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/width'].int64_list.value, [256]) - self._assertProtoEqual( - example.features.feature['image/filename'].bytes_list.value, - [six.b(image_file_name)]) - self._assertProtoEqual( - example.features.feature['image/source_id'].bytes_list.value, - [six.b(image_file_name)]) - self._assertProtoEqual( - example.features.feature['image/format'].bytes_list.value, - [six.b('jpeg')]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymin'].float_list.value, - [0.25]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/xmax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/bbox/ymax'].float_list.value, - [0.75]) - self._assertProtoEqual( - example.features.feature['image/object/class/text'].bytes_list.value, - [six.b('person')]) - self._assertProtoEqual( - example.features.feature['image/object/class/label'].int64_list.value, - [1]) - self._assertProtoEqual( - example.features.feature['image/object/difficult'].int64_list.value, - [1]) - self._assertProtoEqual( - example.features.feature['image/object/truncated'].int64_list.value, - [0]) - self._assertProtoEqual( - example.features.feature['image/object/view'].bytes_list.value, - [six.b('')]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/create_pet_tf_record.py b/research/object_detection/dataset_tools/create_pet_tf_record.py deleted file mode 100644 index 78524b50542..00000000000 --- a/research/object_detection/dataset_tools/create_pet_tf_record.py +++ /dev/null @@ -1,318 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Convert the Oxford pet dataset to TFRecord for object_detection. - -See: O. M. Parkhi, A. Vedaldi, A. Zisserman, C. V. Jawahar - Cats and Dogs - IEEE Conference on Computer Vision and Pattern Recognition, 2012 - http://www.robots.ox.ac.uk/~vgg/data/pets/ - -Example usage: - python object_detection/dataset_tools/create_pet_tf_record.py \ - --data_dir=/home/user/pet \ - --output_dir=/home/user/pet/output -""" - -import hashlib -import io -import logging -import os -import random -import re - -import contextlib2 -from lxml import etree -import numpy as np -import PIL.Image -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import tf_record_creation_util -from object_detection.utils import dataset_util -from object_detection.utils import label_map_util - -flags = tf.app.flags -flags.DEFINE_string('data_dir', '', 'Root directory to raw pet dataset.') -flags.DEFINE_string('output_dir', '', 'Path to directory to output TFRecords.') -flags.DEFINE_string('label_map_path', 'data/pet_label_map.pbtxt', - 'Path to label map proto') -flags.DEFINE_boolean('faces_only', True, 'If True, generates bounding boxes ' - 'for pet faces. Otherwise generates bounding boxes (as ' - 'well as segmentations for full pet bodies). Note that ' - 'in the latter case, the resulting files are much larger.') -flags.DEFINE_string('mask_type', 'png', 'How to represent instance ' - 'segmentation masks. Options are "png" or "numerical".') -flags.DEFINE_integer('num_shards', 10, 'Number of TFRecord shards') - -FLAGS = flags.FLAGS - - -def get_class_name_from_filename(file_name): - """Gets the class name from a file. - - Args: - file_name: The file name to get the class name from. - ie. "american_pit_bull_terrier_105.jpg" - - Returns: - A string of the class name. - """ - match = re.match(r'([A-Za-z_]+)(_[0-9]+\.jpg)', file_name, re.I) - return match.groups()[0] - - -def dict_to_tf_example(data, - mask_path, - label_map_dict, - image_subdirectory, - ignore_difficult_instances=False, - faces_only=True, - mask_type='png'): - """Convert XML derived dict to tf.Example proto. - - Notice that this function normalizes the bounding box coordinates provided - by the raw data. - - Args: - data: dict holding PASCAL XML fields for a single image (obtained by - running dataset_util.recursive_parse_xml_to_dict) - mask_path: String path to PNG encoded mask. - label_map_dict: A map from string label names to integers ids. - image_subdirectory: String specifying subdirectory within the - Pascal dataset directory holding the actual image data. - ignore_difficult_instances: Whether to skip difficult instances in the - dataset (default: False). - faces_only: If True, generates bounding boxes for pet faces. Otherwise - generates bounding boxes (as well as segmentations for full pet bodies). - mask_type: 'numerical' or 'png'. 'png' is recommended because it leads to - smaller file sizes. - - Returns: - example: The converted tf.Example. - - Raises: - ValueError: if the image pointed to by data['filename'] is not a valid JPEG - """ - img_path = os.path.join(image_subdirectory, data['filename']) - with tf.gfile.GFile(img_path, 'rb') as fid: - encoded_jpg = fid.read() - encoded_jpg_io = io.BytesIO(encoded_jpg) - image = PIL.Image.open(encoded_jpg_io) - if image.format != 'JPEG': - raise ValueError('Image format not JPEG') - key = hashlib.sha256(encoded_jpg).hexdigest() - - with tf.gfile.GFile(mask_path, 'rb') as fid: - encoded_mask_png = fid.read() - encoded_png_io = io.BytesIO(encoded_mask_png) - mask = PIL.Image.open(encoded_png_io) - if mask.format != 'PNG': - raise ValueError('Mask format not PNG') - - mask_np = np.asarray(mask) - nonbackground_indices_x = np.any(mask_np != 2, axis=0) - nonbackground_indices_y = np.any(mask_np != 2, axis=1) - nonzero_x_indices = np.where(nonbackground_indices_x) - nonzero_y_indices = np.where(nonbackground_indices_y) - - width = int(data['size']['width']) - height = int(data['size']['height']) - - xmins = [] - ymins = [] - xmaxs = [] - ymaxs = [] - classes = [] - classes_text = [] - truncated = [] - poses = [] - difficult_obj = [] - masks = [] - if 'object' in data: - for obj in data['object']: - difficult = bool(int(obj['difficult'])) - if ignore_difficult_instances and difficult: - continue - difficult_obj.append(int(difficult)) - - if faces_only: - xmin = float(obj['bndbox']['xmin']) - xmax = float(obj['bndbox']['xmax']) - ymin = float(obj['bndbox']['ymin']) - ymax = float(obj['bndbox']['ymax']) - else: - xmin = float(np.min(nonzero_x_indices)) - xmax = float(np.max(nonzero_x_indices)) - ymin = float(np.min(nonzero_y_indices)) - ymax = float(np.max(nonzero_y_indices)) - - xmins.append(xmin / width) - ymins.append(ymin / height) - xmaxs.append(xmax / width) - ymaxs.append(ymax / height) - class_name = get_class_name_from_filename(data['filename']) - classes_text.append(class_name.encode('utf8')) - classes.append(label_map_dict[class_name]) - truncated.append(int(obj['truncated'])) - poses.append(obj['pose'].encode('utf8')) - if not faces_only: - mask_remapped = (mask_np != 2).astype(np.uint8) - masks.append(mask_remapped) - - feature_dict = { - 'image/height': dataset_util.int64_feature(height), - 'image/width': dataset_util.int64_feature(width), - 'image/filename': dataset_util.bytes_feature( - data['filename'].encode('utf8')), - 'image/source_id': dataset_util.bytes_feature( - data['filename'].encode('utf8')), - 'image/key/sha256': dataset_util.bytes_feature(key.encode('utf8')), - 'image/encoded': dataset_util.bytes_feature(encoded_jpg), - 'image/format': dataset_util.bytes_feature('jpeg'.encode('utf8')), - 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), - 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), - 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), - 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), - 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), - 'image/object/class/label': dataset_util.int64_list_feature(classes), - 'image/object/difficult': dataset_util.int64_list_feature(difficult_obj), - 'image/object/truncated': dataset_util.int64_list_feature(truncated), - 'image/object/view': dataset_util.bytes_list_feature(poses), - } - if not faces_only: - if mask_type == 'numerical': - mask_stack = np.stack(masks).astype(np.float32) - masks_flattened = np.reshape(mask_stack, [-1]) - feature_dict['image/object/mask'] = ( - dataset_util.float_list_feature(masks_flattened.tolist())) - elif mask_type == 'png': - encoded_mask_png_list = [] - for mask in masks: - img = PIL.Image.fromarray(mask) - output = io.BytesIO() - img.save(output, format='PNG') - encoded_mask_png_list.append(output.getvalue()) - feature_dict['image/object/mask'] = ( - dataset_util.bytes_list_feature(encoded_mask_png_list)) - - example = tf.train.Example(features=tf.train.Features(feature=feature_dict)) - return example - - -def create_tf_record(output_filename, - num_shards, - label_map_dict, - annotations_dir, - image_dir, - examples, - faces_only=True, - mask_type='png'): - """Creates a TFRecord file from examples. - - Args: - output_filename: Path to where output file is saved. - num_shards: Number of shards for output file. - label_map_dict: The label map dictionary. - annotations_dir: Directory where annotation files are stored. - image_dir: Directory where image files are stored. - examples: Examples to parse and save to tf record. - faces_only: If True, generates bounding boxes for pet faces. Otherwise - generates bounding boxes (as well as segmentations for full pet bodies). - mask_type: 'numerical' or 'png'. 'png' is recommended because it leads to - smaller file sizes. - """ - with contextlib2.ExitStack() as tf_record_close_stack: - output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords( - tf_record_close_stack, output_filename, num_shards) - for idx, example in enumerate(examples): - if idx % 100 == 0: - logging.info('On image %d of %d', idx, len(examples)) - xml_path = os.path.join(annotations_dir, 'xmls', example + '.xml') - mask_path = os.path.join(annotations_dir, 'trimaps', example + '.png') - - if not os.path.exists(xml_path): - logging.warning('Could not find %s, ignoring example.', xml_path) - continue - with tf.gfile.GFile(xml_path, 'r') as fid: - xml_str = fid.read() - xml = etree.fromstring(xml_str) - data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation'] - - try: - tf_example = dict_to_tf_example( - data, - mask_path, - label_map_dict, - image_dir, - faces_only=faces_only, - mask_type=mask_type) - if tf_example: - shard_idx = idx % num_shards - output_tfrecords[shard_idx].write(tf_example.SerializeToString()) - except ValueError: - logging.warning('Invalid example: %s, ignoring.', xml_path) - - -# TODO(derekjchow): Add test for pet/PASCAL main files. -def main(_): - data_dir = FLAGS.data_dir - label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path) - - logging.info('Reading from Pet dataset.') - image_dir = os.path.join(data_dir, 'images') - annotations_dir = os.path.join(data_dir, 'annotations') - examples_path = os.path.join(annotations_dir, 'trainval.txt') - examples_list = dataset_util.read_examples_list(examples_path) - - # Test images are not included in the downloaded data set, so we shall perform - # our own split. - random.seed(42) - random.shuffle(examples_list) - num_examples = len(examples_list) - num_train = int(0.7 * num_examples) - train_examples = examples_list[:num_train] - val_examples = examples_list[num_train:] - logging.info('%d training and %d validation examples.', - len(train_examples), len(val_examples)) - - train_output_path = os.path.join(FLAGS.output_dir, 'pet_faces_train.record') - val_output_path = os.path.join(FLAGS.output_dir, 'pet_faces_val.record') - if not FLAGS.faces_only: - train_output_path = os.path.join(FLAGS.output_dir, - 'pets_fullbody_with_masks_train.record') - val_output_path = os.path.join(FLAGS.output_dir, - 'pets_fullbody_with_masks_val.record') - create_tf_record( - train_output_path, - FLAGS.num_shards, - label_map_dict, - annotations_dir, - image_dir, - train_examples, - faces_only=FLAGS.faces_only, - mask_type=FLAGS.mask_type) - create_tf_record( - val_output_path, - FLAGS.num_shards, - label_map_dict, - annotations_dir, - image_dir, - val_examples, - faces_only=FLAGS.faces_only, - mask_type=FLAGS.mask_type) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/dataset_tools/create_pycocotools_package.sh b/research/object_detection/dataset_tools/create_pycocotools_package.sh deleted file mode 100644 index 88ea5114c23..00000000000 --- a/research/object_detection/dataset_tools/create_pycocotools_package.sh +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# Script to download pycocotools and make package for CMLE jobs. -# -# usage: -# bash object_detection/dataset_tools/create_pycocotools_package.sh \ -# /tmp/pycocotools -set -e - -if [ -z "$1" ]; then - echo "usage create_pycocotools_package.sh [output dir]" - exit -fi - -# Create the output directory. -OUTPUT_DIR="${1%/}" -SCRATCH_DIR="${OUTPUT_DIR}/raw" -mkdir -p "${OUTPUT_DIR}" -mkdir -p "${SCRATCH_DIR}" - -cd ${SCRATCH_DIR} -git clone https://github.com/cocodataset/cocoapi.git -cd cocoapi/PythonAPI && mv ../common ./ - -sed "s/\.\.\/common/common/g" setup.py > setup.py.updated -cp -f setup.py.updated setup.py -rm setup.py.updated - -sed "s/\.\.\/common/common/g" pycocotools/_mask.pyx > _mask.pyx.updated -cp -f _mask.pyx.updated pycocotools/_mask.pyx -rm _mask.pyx.updated - -sed "s/import matplotlib\.pyplot as plt/import matplotlib;matplotlib\.use\(\'Agg\'\);import matplotlib\.pyplot as plt/g" pycocotools/coco.py > coco.py.updated -cp -f coco.py.updated pycocotools/coco.py -rm coco.py.updated - -cd "${OUTPUT_DIR}" -tar -czf pycocotools-2.0.tar.gz -C "${SCRATCH_DIR}/cocoapi/" PythonAPI/ -rm -rf ${SCRATCH_DIR} diff --git a/research/object_detection/dataset_tools/densepose/UV_symmetry_transforms.mat b/research/object_detection/dataset_tools/densepose/UV_symmetry_transforms.mat deleted file mode 100644 index 2836cac4d6b..00000000000 Binary files a/research/object_detection/dataset_tools/densepose/UV_symmetry_transforms.mat and /dev/null differ diff --git a/research/object_detection/dataset_tools/download_and_preprocess_ava.sh b/research/object_detection/dataset_tools/download_and_preprocess_ava.sh deleted file mode 100755 index 723f6a7fcf5..00000000000 --- a/research/object_detection/dataset_tools/download_and_preprocess_ava.sh +++ /dev/null @@ -1,30 +0,0 @@ -#!/bin/bash -# This script downloads the videos for the AVA dataset. There are no arguments. -# Copy this script into the desired parent directory of the ava_vids_raw/ -# directory created in this script to store the raw videos. - -mkdir ava_vids_raw -cd ava_vids_raw - -curl -O s3.amazonaws.com/ava-dataset/annotations/ava_file_names_trainval_v2.1.txt - -echo "Downloading all videos." - -cat "ava_file_names_trainval_v2.1.txt" | while read line -do - curl -O s3.amazonaws.com/ava-dataset/trainval/$line - echo "Downloaded " $line -done - -rm "ava_file_names_trainval_v2.1.txt" -cd .. - -# Trimming causes issues with frame seeking in the python script, so it is best left out. -# If included, need to modify the python script to subtract 900 seconds wheen seeking. - -# echo "Trimming all videos." - -# mkdir ava_vids_trimmed -# for filename in ava_vids_raw/*; do -# ffmpeg -ss 900 -to 1800 -i $filename -c copy ava_vids_trimmed/${filename##*/} -# done diff --git a/research/object_detection/dataset_tools/download_and_preprocess_mscoco.sh b/research/object_detection/dataset_tools/download_and_preprocess_mscoco.sh deleted file mode 100644 index 843ba86938d..00000000000 --- a/research/object_detection/dataset_tools/download_and_preprocess_mscoco.sh +++ /dev/null @@ -1,106 +0,0 @@ -#!/bin/bash -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -# Script to download and preprocess the MSCOCO data set for detection. -# -# The outputs of this script are TFRecord files containing serialized -# tf.Example protocol buffers. See create_coco_tf_record.py for details of how -# the tf.Example protocol buffers are constructed and see -# http://cocodataset.org/#overview for an overview of the dataset. -# -# usage: -# bash object_detection/dataset_tools/download_and_preprocess_mscoco.sh \ -# /tmp/mscoco -set -e - -if [ -z "$1" ]; then - echo "usage download_and_preprocess_mscoco.sh [data dir]" - exit -fi - -if [ "$(uname)" == "Darwin" ]; then - UNZIP="tar -xf" -else - UNZIP="unzip -nq" -fi - -# Create the output directories. -OUTPUT_DIR="${1%/}" -SCRATCH_DIR="${OUTPUT_DIR}/raw-data" -mkdir -p "${OUTPUT_DIR}" -mkdir -p "${SCRATCH_DIR}" -CURRENT_DIR=$(pwd) - -# Helper function to download and unpack a .zip file. -function download_and_unzip() { - local BASE_URL=${1} - local FILENAME=${2} - - if [ ! -f ${FILENAME} ]; then - echo "Downloading ${FILENAME} to $(pwd)" - wget -nd -c "${BASE_URL}/${FILENAME}" - else - echo "Skipping download of ${FILENAME}" - fi - echo "Unzipping ${FILENAME}" - ${UNZIP} ${FILENAME} -} - -cd ${SCRATCH_DIR} - -# Download the images. -BASE_IMAGE_URL="http://images.cocodataset.org/zips" - -TRAIN_IMAGE_FILE="train2017.zip" -download_and_unzip ${BASE_IMAGE_URL} ${TRAIN_IMAGE_FILE} -TRAIN_IMAGE_DIR="${SCRATCH_DIR}/train2017" - -VAL_IMAGE_FILE="val2017.zip" -download_and_unzip ${BASE_IMAGE_URL} ${VAL_IMAGE_FILE} -VAL_IMAGE_DIR="${SCRATCH_DIR}/val2017" - -TEST_IMAGE_FILE="test2017.zip" -download_and_unzip ${BASE_IMAGE_URL} ${TEST_IMAGE_FILE} -TEST_IMAGE_DIR="${SCRATCH_DIR}/test2017" - -# Download the annotations. -BASE_INSTANCES_URL="http://images.cocodataset.org/annotations" -INSTANCES_FILE="annotations_trainval2017.zip" -download_and_unzip ${BASE_INSTANCES_URL} ${INSTANCES_FILE} - -TRAIN_ANNOTATIONS_FILE="${SCRATCH_DIR}/annotations/instances_train2017.json" -VAL_ANNOTATIONS_FILE="${SCRATCH_DIR}/annotations/instances_val2017.json" - -# Download the test image info. -BASE_IMAGE_INFO_URL="http://images.cocodataset.org/annotations" -IMAGE_INFO_FILE="image_info_test2017.zip" -download_and_unzip ${BASE_IMAGE_INFO_URL} ${IMAGE_INFO_FILE} - -TESTDEV_ANNOTATIONS_FILE="${SCRATCH_DIR}/annotations/image_info_test-dev2017.json" - -# Build TFRecords of the image data. -cd "${CURRENT_DIR}" -python object_detection/dataset_tools/create_coco_tf_record.py \ - --logtostderr \ - --include_masks \ - --train_image_dir="${TRAIN_IMAGE_DIR}" \ - --val_image_dir="${VAL_IMAGE_DIR}" \ - --test_image_dir="${TEST_IMAGE_DIR}" \ - --train_annotations_file="${TRAIN_ANNOTATIONS_FILE}" \ - --val_annotations_file="${VAL_ANNOTATIONS_FILE}" \ - --testdev_annotations_file="${TESTDEV_ANNOTATIONS_FILE}" \ - --output_dir="${OUTPUT_DIR}" - diff --git a/research/object_detection/dataset_tools/oid_hierarchical_labels_expansion.py b/research/object_detection/dataset_tools/oid_hierarchical_labels_expansion.py deleted file mode 100644 index 21610c7e964..00000000000 --- a/research/object_detection/dataset_tools/oid_hierarchical_labels_expansion.py +++ /dev/null @@ -1,232 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""An executable to expand image-level labels, boxes and segments. - -The expansion is performed using class hierarchy, provided in JSON file. - -The expected file formats are the following: -- for box and segment files: CSV file is expected to have LabelName field -- for image-level labels: CSV file is expected to have LabelName and Confidence -fields - -Note, that LabelName is the only field used for expansion. - -Example usage: -python models/research/object_detection/dataset_tools/\ -oid_hierarchical_labels_expansion.py \ ---json_hierarchy_file= \ ---input_annotations= \ ---output_annotations= \ ---annotation_type=<1 (for boxes and segments) or 2 (for image-level labels)> -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import copy -import json -from absl import app -from absl import flags -import six - -flags.DEFINE_string( - 'json_hierarchy_file', None, - 'Path to the file containing label hierarchy in JSON format.') -flags.DEFINE_string( - 'input_annotations', None, 'Path to Open Images annotations file' - '(either bounding boxes, segments or image-level labels).') -flags.DEFINE_string('output_annotations', None, 'Path to the output file.') -flags.DEFINE_integer( - 'annotation_type', None, - 'Type of the input annotations: 1 - boxes or segments,' - '2 - image-level labels.' -) - -FLAGS = flags.FLAGS - - -def _update_dict(initial_dict, update): - """Updates dictionary with update content. - - Args: - initial_dict: initial dictionary. - update: updated dictionary. - """ - - for key, value_list in update.items(): - if key in initial_dict: - initial_dict[key].update(value_list) - else: - initial_dict[key] = set(value_list) - - -def _build_plain_hierarchy(hierarchy, skip_root=False): - """Expands tree hierarchy representation to parent-child dictionary. - - Args: - hierarchy: labels hierarchy as JSON file. - skip_root: if true skips root from the processing (done for the case when all - classes under hierarchy are collected under virtual node). - - Returns: - keyed_parent - dictionary of parent - all its children nodes. - keyed_child - dictionary of children - all its parent nodes - children - all children of the current node. - """ - all_children = set([]) - all_keyed_parent = {} - all_keyed_child = {} - if 'Subcategory' in hierarchy: - for node in hierarchy['Subcategory']: - keyed_parent, keyed_child, children = _build_plain_hierarchy(node) - # Update is not done through dict.update() since some children have multi- - # ple parents in the hiearchy. - _update_dict(all_keyed_parent, keyed_parent) - _update_dict(all_keyed_child, keyed_child) - all_children.update(children) - - if not skip_root: - all_keyed_parent[hierarchy['LabelName']] = copy.deepcopy(all_children) - all_children.add(hierarchy['LabelName']) - for child, _ in all_keyed_child.items(): - all_keyed_child[child].add(hierarchy['LabelName']) - all_keyed_child[hierarchy['LabelName']] = set([]) - - return all_keyed_parent, all_keyed_child, all_children - - -class OIDHierarchicalLabelsExpansion(object): - """ Main class to perform labels hierachical expansion.""" - - def __init__(self, hierarchy): - """Constructor. - - Args: - hierarchy: labels hierarchy as JSON object. - """ - - self._hierarchy_keyed_parent, self._hierarchy_keyed_child, _ = ( - _build_plain_hierarchy(hierarchy, skip_root=True)) - - def expand_boxes_or_segments_from_csv(self, csv_row, - labelname_column_index=1): - """Expands a row containing bounding boxes/segments from CSV file. - - Args: - csv_row: a single row of Open Images released groundtruth file. - labelname_column_index: 0-based index of LabelName column in CSV file. - - Returns: - a list of strings (including the initial row) corresponding to the ground - truth expanded to multiple annotation for evaluation with Open Images - Challenge 2018/2019 metrics. - """ - # Row header is expected to be the following for boxes: - # ImageID,LabelName,Confidence,XMin,XMax,YMin,YMax,IsGroupOf - # Row header is expected to be the following for segments: - # ImageID,LabelName,ImageWidth,ImageHeight,XMin,XMax,YMin,YMax, - # IsGroupOf,Mask - split_csv_row = six.ensure_str(csv_row).split(',') - result = [csv_row] - assert split_csv_row[ - labelname_column_index] in self._hierarchy_keyed_child - parent_nodes = self._hierarchy_keyed_child[ - split_csv_row[labelname_column_index]] - for parent_node in parent_nodes: - split_csv_row[labelname_column_index] = parent_node - result.append(','.join(split_csv_row)) - return result - - def expand_labels_from_csv(self, - csv_row, - labelname_column_index=1, - confidence_column_index=2): - """Expands a row containing labels from CSV file. - - Args: - csv_row: a single row of Open Images released groundtruth file. - labelname_column_index: 0-based index of LabelName column in CSV file. - confidence_column_index: 0-based index of Confidence column in CSV file. - - Returns: - a list of strings (including the initial row) corresponding to the ground - truth expanded to multiple annotation for evaluation with Open Images - Challenge 2018/2019 metrics. - """ - # Row header is expected to be exactly: - # ImageID,Source,LabelName,Confidence - split_csv_row = six.ensure_str(csv_row).split(',') - result = [csv_row] - if int(split_csv_row[confidence_column_index]) == 1: - assert split_csv_row[ - labelname_column_index] in self._hierarchy_keyed_child - parent_nodes = self._hierarchy_keyed_child[ - split_csv_row[labelname_column_index]] - for parent_node in parent_nodes: - split_csv_row[labelname_column_index] = parent_node - result.append(','.join(split_csv_row)) - else: - assert split_csv_row[ - labelname_column_index] in self._hierarchy_keyed_parent - child_nodes = self._hierarchy_keyed_parent[ - split_csv_row[labelname_column_index]] - for child_node in child_nodes: - split_csv_row[labelname_column_index] = child_node - result.append(','.join(split_csv_row)) - return result - - -def main(unused_args): - - del unused_args - - with open(FLAGS.json_hierarchy_file) as f: - hierarchy = json.load(f) - expansion_generator = OIDHierarchicalLabelsExpansion(hierarchy) - labels_file = False - if FLAGS.annotation_type == 2: - labels_file = True - elif FLAGS.annotation_type != 1: - print('--annotation_type expected value is 1 or 2.') - return -1 - confidence_column_index = -1 - labelname_column_index = -1 - with open(FLAGS.input_annotations, 'r') as source: - with open(FLAGS.output_annotations, 'w') as target: - header = source.readline() - target.writelines([header]) - column_names = header.strip().split(',') - labelname_column_index = column_names.index('LabelName') - if labels_file: - confidence_column_index = column_names.index('Confidence') - for line in source: - if labels_file: - expanded_lines = expansion_generator.expand_labels_from_csv( - line, labelname_column_index, confidence_column_index) - else: - expanded_lines = ( - expansion_generator.expand_boxes_or_segments_from_csv( - line, labelname_column_index)) - target.writelines(expanded_lines) - - -if __name__ == '__main__': - flags.mark_flag_as_required('json_hierarchy_file') - flags.mark_flag_as_required('input_annotations') - flags.mark_flag_as_required('output_annotations') - flags.mark_flag_as_required('annotation_type') - - app.run(main) diff --git a/research/object_detection/dataset_tools/oid_hierarchical_labels_expansion_test.py b/research/object_detection/dataset_tools/oid_hierarchical_labels_expansion_test.py deleted file mode 100644 index ca010c5bed3..00000000000 --- a/research/object_detection/dataset_tools/oid_hierarchical_labels_expansion_test.py +++ /dev/null @@ -1,116 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the OpenImages label expansion (OIDHierarchicalLabelsExpansion).""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import oid_hierarchical_labels_expansion - - -def create_test_data(): - hierarchy = { - 'LabelName': - 'a', - 'Subcategory': [{ - 'LabelName': 'b' - }, { - 'LabelName': - 'c', - 'Subcategory': [{ - 'LabelName': 'd' - }, { - 'LabelName': 'e' - }, { - 'LabelName': 'f', - 'Subcategory': [{ - 'LabelName': 'd' - },] - }] - }, { - 'LabelName': 'f', - 'Subcategory': [{ - 'LabelName': 'd' - },] - }] - } - bbox_rows = [ - '123,xclick,b,1,0.1,0.2,0.1,0.2,1,1,0,0,0', - '123,xclick,d,1,0.2,0.3,0.1,0.2,1,1,0,0,0' - ] - label_rows = [ - '123,verification,b,0', '123,verification,c,0', '124,verification,d,1' - ] - segm_rows = [ - '123,cc,b,100,100,0.1,0.2,0.1,0.2,0,MASK', - '123,cc,d,100,100,0.2,0.3,0.1,0.2,0,MASK', - ] - return hierarchy, bbox_rows, segm_rows, label_rows - - -class HierarchicalLabelsExpansionTest(tf.test.TestCase): - - def test_bbox_expansion(self): - hierarchy, bbox_rows, _, _ = create_test_data() - expansion_generator = ( - oid_hierarchical_labels_expansion.OIDHierarchicalLabelsExpansion( - hierarchy)) - all_result_rows = [] - for row in bbox_rows: - all_result_rows.extend( - expansion_generator.expand_boxes_or_segments_from_csv(row, 2)) - self.assertItemsEqual([ - '123,xclick,b,1,0.1,0.2,0.1,0.2,1,1,0,0,0', - '123,xclick,d,1,0.2,0.3,0.1,0.2,1,1,0,0,0', - '123,xclick,f,1,0.2,0.3,0.1,0.2,1,1,0,0,0', - '123,xclick,c,1,0.2,0.3,0.1,0.2,1,1,0,0,0' - ], all_result_rows) - - def test_segm_expansion(self): - hierarchy, _, segm_rows, _ = create_test_data() - expansion_generator = ( - oid_hierarchical_labels_expansion.OIDHierarchicalLabelsExpansion( - hierarchy)) - all_result_rows = [] - for row in segm_rows: - all_result_rows.extend( - expansion_generator.expand_boxes_or_segments_from_csv(row, 2)) - self.assertItemsEqual([ - '123,cc,b,100,100,0.1,0.2,0.1,0.2,0,MASK', - '123,cc,d,100,100,0.2,0.3,0.1,0.2,0,MASK', - '123,cc,f,100,100,0.2,0.3,0.1,0.2,0,MASK', - '123,cc,c,100,100,0.2,0.3,0.1,0.2,0,MASK' - ], all_result_rows) - - def test_labels_expansion(self): - hierarchy, _, _, label_rows = create_test_data() - expansion_generator = ( - oid_hierarchical_labels_expansion.OIDHierarchicalLabelsExpansion( - hierarchy)) - all_result_rows = [] - for row in label_rows: - all_result_rows.extend( - expansion_generator.expand_labels_from_csv(row, 2, 3)) - self.assertItemsEqual([ - '123,verification,b,0', '123,verification,c,0', '123,verification,d,0', - '123,verification,f,0', '123,verification,e,0', '124,verification,d,1', - '124,verification,f,1', '124,verification,c,1' - ], all_result_rows) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/oid_tfrecord_creation.py b/research/object_detection/dataset_tools/oid_tfrecord_creation.py deleted file mode 100644 index 0cddbbb9cd3..00000000000 --- a/research/object_detection/dataset_tools/oid_tfrecord_creation.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Utilities for creating TFRecords of TF examples for the Open Images dataset. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import six -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields -from object_detection.utils import dataset_util - - -def tf_example_from_annotations_data_frame(annotations_data_frame, label_map, - encoded_image): - """Populates a TF Example message with image annotations from a data frame. - - Args: - annotations_data_frame: Data frame containing the annotations for a single - image. - label_map: String to integer label map. - encoded_image: The encoded image string - - Returns: - The populated TF Example, if the label of at least one object is present in - label_map. Otherwise, returns None. - """ - - filtered_data_frame = annotations_data_frame[ - annotations_data_frame.LabelName.isin(label_map)] - filtered_data_frame_boxes = filtered_data_frame[ - ~filtered_data_frame.YMin.isnull()] - filtered_data_frame_labels = filtered_data_frame[ - filtered_data_frame.YMin.isnull()] - image_id = annotations_data_frame.ImageID.iloc[0] - - feature_map = { - standard_fields.TfExampleFields.object_bbox_ymin: - dataset_util.float_list_feature( - filtered_data_frame_boxes.YMin.to_numpy()), - standard_fields.TfExampleFields.object_bbox_xmin: - dataset_util.float_list_feature( - filtered_data_frame_boxes.XMin.to_numpy()), - standard_fields.TfExampleFields.object_bbox_ymax: - dataset_util.float_list_feature( - filtered_data_frame_boxes.YMax.to_numpy()), - standard_fields.TfExampleFields.object_bbox_xmax: - dataset_util.float_list_feature( - filtered_data_frame_boxes.XMax.to_numpy()), - standard_fields.TfExampleFields.object_class_text: - dataset_util.bytes_list_feature([ - six.ensure_binary(label_text) - for label_text in filtered_data_frame_boxes.LabelName.to_numpy() - ]), - standard_fields.TfExampleFields.object_class_label: - dataset_util.int64_list_feature( - filtered_data_frame_boxes.LabelName.map( - lambda x: label_map[x]).to_numpy()), - standard_fields.TfExampleFields.filename: - dataset_util.bytes_feature( - six.ensure_binary('{}.jpg'.format(image_id))), - standard_fields.TfExampleFields.source_id: - dataset_util.bytes_feature(six.ensure_binary(image_id)), - standard_fields.TfExampleFields.image_encoded: - dataset_util.bytes_feature(six.ensure_binary(encoded_image)), - } - - if 'IsGroupOf' in filtered_data_frame.columns: - feature_map[standard_fields.TfExampleFields. - object_group_of] = dataset_util.int64_list_feature( - filtered_data_frame_boxes.IsGroupOf.to_numpy().astype(int)) - if 'IsOccluded' in filtered_data_frame.columns: - feature_map[standard_fields.TfExampleFields. - object_occluded] = dataset_util.int64_list_feature( - filtered_data_frame_boxes.IsOccluded.to_numpy().astype( - int)) - if 'IsTruncated' in filtered_data_frame.columns: - feature_map[standard_fields.TfExampleFields. - object_truncated] = dataset_util.int64_list_feature( - filtered_data_frame_boxes.IsTruncated.to_numpy().astype( - int)) - if 'IsDepiction' in filtered_data_frame.columns: - feature_map[standard_fields.TfExampleFields. - object_depiction] = dataset_util.int64_list_feature( - filtered_data_frame_boxes.IsDepiction.to_numpy().astype( - int)) - - if 'ConfidenceImageLabel' in filtered_data_frame_labels.columns: - feature_map[standard_fields.TfExampleFields. - image_class_label] = dataset_util.int64_list_feature( - filtered_data_frame_labels.LabelName.map( - lambda x: label_map[x]).to_numpy()) - feature_map[standard_fields.TfExampleFields - .image_class_text] = dataset_util.bytes_list_feature([ - six.ensure_binary(label_text) for label_text in - filtered_data_frame_labels.LabelName.to_numpy() - ]), - return tf.train.Example(features=tf.train.Features(feature=feature_map)) diff --git a/research/object_detection/dataset_tools/oid_tfrecord_creation_test.py b/research/object_detection/dataset_tools/oid_tfrecord_creation_test.py deleted file mode 100644 index b1e945f46d6..00000000000 --- a/research/object_detection/dataset_tools/oid_tfrecord_creation_test.py +++ /dev/null @@ -1,200 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for oid_tfrecord_creation.py.""" - -import pandas as pd -import six -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import oid_tfrecord_creation - - -def create_test_data(): - data = { - 'ImageID': ['i1', 'i1', 'i1', 'i1', 'i1', 'i2', 'i2'], - 'LabelName': ['a', 'a', 'b', 'b', 'c', 'b', 'c'], - 'YMin': [0.3, 0.6, 0.8, 0.1, None, 0.0, 0.0], - 'XMin': [0.1, 0.3, 0.7, 0.0, None, 0.1, 0.1], - 'XMax': [0.2, 0.3, 0.8, 0.5, None, 0.9, 0.9], - 'YMax': [0.3, 0.6, 1, 0.8, None, 0.8, 0.8], - 'IsOccluded': [0, 1, 1, 0, None, 0, 0], - 'IsTruncated': [0, 0, 0, 1, None, 0, 0], - 'IsGroupOf': [0, 0, 0, 0, None, 0, 1], - 'IsDepiction': [1, 0, 0, 0, None, 0, 0], - 'ConfidenceImageLabel': [None, None, None, None, 0, None, None], - } - df = pd.DataFrame(data=data) - label_map = {'a': 0, 'b': 1, 'c': 2} - return label_map, df - - -class TfExampleFromAnnotationsDataFrameTests(tf.test.TestCase): - - def test_simple(self): - label_map, df = create_test_data() - - tf_example = oid_tfrecord_creation.tf_example_from_annotations_data_frame( - df[df.ImageID == 'i1'], label_map, 'encoded_image_test') - self.assertProtoEquals(six.ensure_str(""" - features { - feature { - key: "image/encoded" - value { bytes_list { value: "encoded_image_test" } } } - feature { - key: "image/filename" - value { bytes_list { value: "i1.jpg" } } } - feature { - key: "image/object/bbox/ymin" - value { float_list { value: [0.3, 0.6, 0.8, 0.1] } } } - feature { - key: "image/object/bbox/xmin" - value { float_list { value: [0.1, 0.3, 0.7, 0.0] } } } - feature { - key: "image/object/bbox/ymax" - value { float_list { value: [0.3, 0.6, 1.0, 0.8] } } } - feature { - key: "image/object/bbox/xmax" - value { float_list { value: [0.2, 0.3, 0.8, 0.5] } } } - feature { - key: "image/object/class/label" - value { int64_list { value: [0, 0, 1, 1] } } } - feature { - key: "image/object/class/text" - value { bytes_list { value: ["a", "a", "b", "b"] } } } - feature { - key: "image/source_id" - value { bytes_list { value: "i1" } } } - feature { - key: "image/object/depiction" - value { int64_list { value: [1, 0, 0, 0] } } } - feature { - key: "image/object/group_of" - value { int64_list { value: [0, 0, 0, 0] } } } - feature { - key: "image/object/occluded" - value { int64_list { value: [0, 1, 1, 0] } } } - feature { - key: "image/object/truncated" - value { int64_list { value: [0, 0, 0, 1] } } } - feature { - key: "image/class/label" - value { int64_list { value: [2] } } } - feature { - key: "image/class/text" - value { bytes_list { value: ["c"] } } } } - """), tf_example) - - def test_no_attributes(self): - label_map, df = create_test_data() - - del df['IsDepiction'] - del df['IsGroupOf'] - del df['IsOccluded'] - del df['IsTruncated'] - del df['ConfidenceImageLabel'] - - tf_example = oid_tfrecord_creation.tf_example_from_annotations_data_frame( - df[df.ImageID == 'i2'], label_map, 'encoded_image_test') - self.assertProtoEquals(six.ensure_str(""" - features { - feature { - key: "image/encoded" - value { bytes_list { value: "encoded_image_test" } } } - feature { - key: "image/filename" - value { bytes_list { value: "i2.jpg" } } } - feature { - key: "image/object/bbox/ymin" - value { float_list { value: [0.0, 0.0] } } } - feature { - key: "image/object/bbox/xmin" - value { float_list { value: [0.1, 0.1] } } } - feature { - key: "image/object/bbox/ymax" - value { float_list { value: [0.8, 0.8] } } } - feature { - key: "image/object/bbox/xmax" - value { float_list { value: [0.9, 0.9] } } } - feature { - key: "image/object/class/label" - value { int64_list { value: [1, 2] } } } - feature { - key: "image/object/class/text" - value { bytes_list { value: ["b", "c"] } } } - feature { - key: "image/source_id" - value { bytes_list { value: "i2" } } } } - """), tf_example) - - def test_label_filtering(self): - label_map, df = create_test_data() - - label_map = {'a': 0} - - tf_example = oid_tfrecord_creation.tf_example_from_annotations_data_frame( - df[df.ImageID == 'i1'], label_map, 'encoded_image_test') - self.assertProtoEquals( - six.ensure_str(""" - features { - feature { - key: "image/encoded" - value { bytes_list { value: "encoded_image_test" } } } - feature { - key: "image/filename" - value { bytes_list { value: "i1.jpg" } } } - feature { - key: "image/object/bbox/ymin" - value { float_list { value: [0.3, 0.6] } } } - feature { - key: "image/object/bbox/xmin" - value { float_list { value: [0.1, 0.3] } } } - feature { - key: "image/object/bbox/ymax" - value { float_list { value: [0.3, 0.6] } } } - feature { - key: "image/object/bbox/xmax" - value { float_list { value: [0.2, 0.3] } } } - feature { - key: "image/object/class/label" - value { int64_list { value: [0, 0] } } } - feature { - key: "image/object/class/text" - value { bytes_list { value: ["a", "a"] } } } - feature { - key: "image/source_id" - value { bytes_list { value: "i1" } } } - feature { - key: "image/object/depiction" - value { int64_list { value: [1, 0] } } } - feature { - key: "image/object/group_of" - value { int64_list { value: [0, 0] } } } - feature { - key: "image/object/occluded" - value { int64_list { value: [0, 1] } } } - feature { - key: "image/object/truncated" - value { int64_list { value: [0, 0] } } } - feature { - key: "image/class/label" - value { int64_list { } } } - feature { - key: "image/class/text" - value { bytes_list { } } } } - """), tf_example) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/seq_example_util.py b/research/object_detection/dataset_tools/seq_example_util.py deleted file mode 100644 index 49864d95f92..00000000000 --- a/research/object_detection/dataset_tools/seq_example_util.py +++ /dev/null @@ -1,305 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Common utility for object detection tf.train.SequenceExamples.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import tensorflow.compat.v1 as tf - - -def context_float_feature(ndarray): - """Converts a numpy float array to a context float feature. - - Args: - ndarray: A numpy float array. - - Returns: - A context float feature. - """ - feature = tf.train.Feature() - for val in ndarray: - if isinstance(val, np.ndarray): - val = val.item() - feature.float_list.value.append(val) - return feature - - -def context_int64_feature(ndarray): - """Converts a numpy array to a context int64 feature. - - Args: - ndarray: A numpy int64 array. - - Returns: - A context int64 feature. - """ - feature = tf.train.Feature() - for val in ndarray: - if isinstance(val, np.ndarray): - val = val.item() - feature.int64_list.value.append(val) - return feature - - -def context_bytes_feature(ndarray): - """Converts a numpy bytes array to a context bytes feature. - - Args: - ndarray: A numpy bytes array. - - Returns: - A context bytes feature. - """ - feature = tf.train.Feature() - for val in ndarray: - if isinstance(val, np.ndarray): - val = val.tolist() - feature.bytes_list.value.append(tf.compat.as_bytes(val)) - return feature - - -def sequence_float_feature(ndarray): - """Converts a numpy float array to a sequence float feature. - - Args: - ndarray: A numpy float array. - - Returns: - A sequence float feature. - """ - feature_list = tf.train.FeatureList() - for row in ndarray: - feature = feature_list.feature.add() - if row.size: - feature.float_list.value[:] = np.ravel(row) - return feature_list - - -def sequence_int64_feature(ndarray): - """Converts a numpy int64 array to a sequence int64 feature. - - Args: - ndarray: A numpy int64 array. - - Returns: - A sequence int64 feature. - """ - feature_list = tf.train.FeatureList() - for row in ndarray: - feature = feature_list.feature.add() - if row.size: - feature.int64_list.value[:] = np.ravel(row) - return feature_list - - -def sequence_bytes_feature(ndarray): - """Converts a bytes float array to a sequence bytes feature. - - Args: - ndarray: A numpy bytes array. - - Returns: - A sequence bytes feature. - """ - feature_list = tf.train.FeatureList() - for row in ndarray: - if isinstance(row, np.ndarray): - row = row.tolist() - feature = feature_list.feature.add() - if row: - row = [tf.compat.as_bytes(val) for val in row] - feature.bytes_list.value[:] = np.ravel(row) - return feature_list - - -def sequence_strings_feature(strings): - new_str_arr = [] - for single_str in strings: - new_str_arr.append(tf.train.Feature( - bytes_list=tf.train.BytesList( - value=[single_str.encode('utf8')]))) - return tf.train.FeatureList(feature=new_str_arr) - - -def boxes_to_box_components(bboxes): - """Converts a list of numpy arrays (boxes) to box components. - - Args: - bboxes: A numpy array of bounding boxes. - - Returns: - Bounding box component lists. - """ - ymin_list = [] - xmin_list = [] - ymax_list = [] - xmax_list = [] - for bbox in bboxes: - if bbox != []: # pylint: disable=g-explicit-bool-comparison - bbox = np.array(bbox).astype(np.float32) - ymin, xmin, ymax, xmax = np.split(bbox, 4, axis=1) - else: - ymin, xmin, ymax, xmax = [], [], [], [] - ymin_list.append(np.reshape(ymin, [-1])) - xmin_list.append(np.reshape(xmin, [-1])) - ymax_list.append(np.reshape(ymax, [-1])) - xmax_list.append(np.reshape(xmax, [-1])) - return ymin_list, xmin_list, ymax_list, xmax_list - - -def make_sequence_example(dataset_name, - video_id, - encoded_images, - image_height, - image_width, - image_format=None, - image_source_ids=None, - timestamps=None, - is_annotated=None, - bboxes=None, - label_strings=None, - detection_bboxes=None, - detection_classes=None, - detection_scores=None, - use_strs_for_source_id=False, - context_features=None, - context_feature_length=None, - context_features_image_id_list=None): - """Constructs tf.SequenceExamples. - - Args: - dataset_name: String with dataset name. - video_id: String with video id. - encoded_images: A [num_frames] list (or numpy array) of encoded image - frames. - image_height: Height of the images. - image_width: Width of the images. - image_format: Format of encoded images. - image_source_ids: (Optional) A [num_frames] list of unique string ids for - each image. - timestamps: (Optional) A [num_frames] list (or numpy array) array with image - timestamps. - is_annotated: (Optional) A [num_frames] list (or numpy array) array - in which each element indicates whether the frame has been annotated - (1) or not (0). - bboxes: (Optional) A list (with num_frames elements) of [num_boxes_i, 4] - numpy float32 arrays holding boxes for each frame. - label_strings: (Optional) A list (with num_frames_elements) of [num_boxes_i] - numpy string arrays holding object string labels for each frame. - detection_bboxes: (Optional) A list (with num_frames elements) of - [num_boxes_i, 4] numpy float32 arrays holding prediction boxes for each - frame. - detection_classes: (Optional) A list (with num_frames_elements) of - [num_boxes_i] numpy int64 arrays holding predicted classes for each frame. - detection_scores: (Optional) A list (with num_frames_elements) of - [num_boxes_i] numpy float32 arrays holding predicted object scores for - each frame. - use_strs_for_source_id: (Optional) Whether to write the source IDs as - strings rather than byte lists of characters. - context_features: (Optional) A list or numpy array of features to use in - Context R-CNN, of length num_context_features * context_feature_length. - context_feature_length: (Optional) The length of each context feature, used - for reshaping. - context_features_image_id_list: (Optional) A list of image ids of length - num_context_features corresponding to the context features. - - Returns: - A tf.train.SequenceExample. - """ - num_frames = len(encoded_images) - image_encoded = np.expand_dims(encoded_images, axis=-1) - if timestamps is None: - timestamps = np.arange(num_frames) - image_timestamps = np.expand_dims(timestamps, axis=-1) - - # Context fields. - context_dict = { - 'example/dataset_name': context_bytes_feature([dataset_name]), - 'clip/start/timestamp': context_int64_feature([image_timestamps[0][0]]), - 'clip/end/timestamp': context_int64_feature([image_timestamps[-1][0]]), - 'clip/frames': context_int64_feature([num_frames]), - 'image/channels': context_int64_feature([3]), - 'image/height': context_int64_feature([image_height]), - 'image/width': context_int64_feature([image_width]), - 'clip/media_id': context_bytes_feature([video_id]) - } - - # Sequence fields. - feature_list = { - 'image/encoded': sequence_bytes_feature(image_encoded), - 'image/timestamp': sequence_int64_feature(image_timestamps), - } - - # Add optional fields. - if image_format is not None: - context_dict['image/format'] = context_bytes_feature([image_format]) - if image_source_ids is not None: - if use_strs_for_source_id: - feature_list['image/source_id'] = sequence_strings_feature( - image_source_ids) - else: - feature_list['image/source_id'] = sequence_bytes_feature(image_source_ids) - if bboxes is not None: - bbox_ymin, bbox_xmin, bbox_ymax, bbox_xmax = boxes_to_box_components(bboxes) - feature_list['region/bbox/xmin'] = sequence_float_feature(bbox_xmin) - feature_list['region/bbox/xmax'] = sequence_float_feature(bbox_xmax) - feature_list['region/bbox/ymin'] = sequence_float_feature(bbox_ymin) - feature_list['region/bbox/ymax'] = sequence_float_feature(bbox_ymax) - if is_annotated is None: - is_annotated = np.ones(num_frames, dtype=np.int64) - is_annotated = np.expand_dims(is_annotated, axis=-1) - feature_list['region/is_annotated'] = sequence_int64_feature(is_annotated) - - if label_strings is not None: - feature_list['region/label/string'] = sequence_bytes_feature( - label_strings) - - if detection_bboxes is not None: - det_bbox_ymin, det_bbox_xmin, det_bbox_ymax, det_bbox_xmax = ( - boxes_to_box_components(detection_bboxes)) - feature_list['predicted/region/bbox/xmin'] = sequence_float_feature( - det_bbox_xmin) - feature_list['predicted/region/bbox/xmax'] = sequence_float_feature( - det_bbox_xmax) - feature_list['predicted/region/bbox/ymin'] = sequence_float_feature( - det_bbox_ymin) - feature_list['predicted/region/bbox/ymax'] = sequence_float_feature( - det_bbox_ymax) - if detection_classes is not None: - feature_list['predicted/region/label/index'] = sequence_int64_feature( - detection_classes) - if detection_scores is not None: - feature_list['predicted/region/label/confidence'] = sequence_float_feature( - detection_scores) - - if context_features is not None: - context_dict['image/context_features'] = context_float_feature( - context_features) - if context_feature_length is not None: - context_dict['image/context_feature_length'] = context_int64_feature( - context_feature_length) - if context_features_image_id_list is not None: - context_dict['image/context_features_image_id_list'] = ( - context_bytes_feature(context_features_image_id_list)) - - context = tf.train.Features(feature=context_dict) - feature_lists = tf.train.FeatureLists(feature_list=feature_list) - - sequence_example = tf.train.SequenceExample( - context=context, - feature_lists=feature_lists) - return sequence_example diff --git a/research/object_detection/dataset_tools/seq_example_util_test.py b/research/object_detection/dataset_tools/seq_example_util_test.py deleted file mode 100644 index f3821d68f1c..00000000000 --- a/research/object_detection/dataset_tools/seq_example_util_test.py +++ /dev/null @@ -1,482 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.utils.seq_example_util.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import six -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import seq_example_util -from object_detection.utils import tf_version - - -class SeqExampleUtilTest(tf.test.TestCase): - - def materialize_tensors(self, list_of_tensors): - if tf_version.is_tf2(): - return [tensor.numpy() for tensor in list_of_tensors] - else: - with self.cached_session() as sess: - return sess.run(list_of_tensors) - - def test_make_unlabeled_example(self): - num_frames = 5 - image_height = 100 - image_width = 200 - dataset_name = b'unlabeled_dataset' - video_id = b'video_000' - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - image_source_ids = [str(idx) for idx in range(num_frames)] - images_list = tf.unstack(images, axis=0) - encoded_images_list = [tf.io.encode_jpeg(image) for image in images_list] - encoded_images = self.materialize_tensors(encoded_images_list) - seq_example = seq_example_util.make_sequence_example( - dataset_name=dataset_name, - video_id=video_id, - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - image_format='JPEG', - image_source_ids=image_source_ids) - - context_feature_dict = seq_example.context.feature - self.assertEqual( - dataset_name, - context_feature_dict['example/dataset_name'].bytes_list.value[0]) - self.assertEqual( - 0, - context_feature_dict['clip/start/timestamp'].int64_list.value[0]) - self.assertEqual( - num_frames - 1, - context_feature_dict['clip/end/timestamp'].int64_list.value[0]) - self.assertEqual( - num_frames, - context_feature_dict['clip/frames'].int64_list.value[0]) - self.assertEqual( - 3, - context_feature_dict['image/channels'].int64_list.value[0]) - self.assertEqual( - b'JPEG', - context_feature_dict['image/format'].bytes_list.value[0]) - self.assertEqual( - image_height, - context_feature_dict['image/height'].int64_list.value[0]) - self.assertEqual( - image_width, - context_feature_dict['image/width'].int64_list.value[0]) - self.assertEqual( - video_id, - context_feature_dict['clip/media_id'].bytes_list.value[0]) - - seq_feature_dict = seq_example.feature_lists.feature_list - self.assertLen( - seq_feature_dict['image/encoded'].feature[:], - num_frames) - timestamps = [ - feature.int64_list.value[0] for feature - in seq_feature_dict['image/timestamp'].feature] - self.assertAllEqual(list(range(num_frames)), timestamps) - source_ids = [ - feature.bytes_list.value[0] for feature - in seq_feature_dict['image/source_id'].feature] - self.assertAllEqual( - [six.ensure_binary(str(idx)) for idx in range(num_frames)], - source_ids) - - def test_make_labeled_example(self): - num_frames = 3 - image_height = 100 - image_width = 200 - dataset_name = b'unlabeled_dataset' - video_id = b'video_000' - labels = [b'dog', b'cat', b'wolf'] - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - images_list = tf.unstack(images, axis=0) - encoded_images_list = [tf.io.encode_jpeg(image) for image in images_list] - encoded_images = self.materialize_tensors(encoded_images_list) - timestamps = [100000, 110000, 120000] - is_annotated = [1, 0, 1] - bboxes = [ - np.array([[0., 0., 0., 0.], - [0., 0., 1., 1.]], dtype=np.float32), - np.zeros([0, 4], dtype=np.float32), - np.array([], dtype=np.float32) - ] - label_strings = [ - np.array(labels), - np.array([]), - np.array([]) - ] - - seq_example = seq_example_util.make_sequence_example( - dataset_name=dataset_name, - video_id=video_id, - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - timestamps=timestamps, - is_annotated=is_annotated, - bboxes=bboxes, - label_strings=label_strings) - - context_feature_dict = seq_example.context.feature - self.assertEqual( - dataset_name, - context_feature_dict['example/dataset_name'].bytes_list.value[0]) - self.assertEqual( - timestamps[0], - context_feature_dict['clip/start/timestamp'].int64_list.value[0]) - self.assertEqual( - timestamps[-1], - context_feature_dict['clip/end/timestamp'].int64_list.value[0]) - self.assertEqual( - num_frames, - context_feature_dict['clip/frames'].int64_list.value[0]) - - seq_feature_dict = seq_example.feature_lists.feature_list - self.assertLen( - seq_feature_dict['image/encoded'].feature[:], - num_frames) - actual_timestamps = [ - feature.int64_list.value[0] for feature - in seq_feature_dict['image/timestamp'].feature] - self.assertAllEqual(timestamps, actual_timestamps) - # Frame 0. - self.assertAllEqual( - is_annotated[0], - seq_feature_dict['region/is_annotated'].feature[0].int64_list.value[0]) - self.assertAllClose( - [0., 0.], - seq_feature_dict['region/bbox/ymin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0., 0.], - seq_feature_dict['region/bbox/xmin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0., 1.], - seq_feature_dict['region/bbox/ymax'].feature[0].float_list.value[:]) - self.assertAllClose( - [0., 1.], - seq_feature_dict['region/bbox/xmax'].feature[0].float_list.value[:]) - self.assertAllEqual( - labels, - seq_feature_dict['region/label/string'].feature[0].bytes_list.value[:]) - - # Frame 1. - self.assertAllEqual( - is_annotated[1], - seq_feature_dict['region/is_annotated'].feature[1].int64_list.value[0]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/ymin'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/xmin'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/ymax'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/xmax'].feature[1].float_list.value[:]) - self.assertAllEqual( - [], - seq_feature_dict['region/label/string'].feature[1].bytes_list.value[:]) - - def test_make_labeled_example_with_context_features(self): - num_frames = 2 - image_height = 100 - image_width = 200 - dataset_name = b'unlabeled_dataset' - video_id = b'video_000' - labels = [b'dog', b'cat'] - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - images_list = tf.unstack(images, axis=0) - encoded_images_list = [tf.io.encode_jpeg(image) for image in images_list] - encoded_images = self.materialize_tensors(encoded_images_list) - timestamps = [100000, 110000] - is_annotated = [1, 0] - bboxes = [ - np.array([[0., 0., 0., 0.], - [0., 0., 1., 1.]], dtype=np.float32), - np.zeros([0, 4], dtype=np.float32) - ] - label_strings = [ - np.array(labels), - np.array([]) - ] - context_features = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5] - context_feature_length = [3] - context_features_image_id_list = [b'im_1', b'im_2'] - - seq_example = seq_example_util.make_sequence_example( - dataset_name=dataset_name, - video_id=video_id, - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - timestamps=timestamps, - is_annotated=is_annotated, - bboxes=bboxes, - label_strings=label_strings, - context_features=context_features, - context_feature_length=context_feature_length, - context_features_image_id_list=context_features_image_id_list) - - context_feature_dict = seq_example.context.feature - self.assertEqual( - dataset_name, - context_feature_dict['example/dataset_name'].bytes_list.value[0]) - self.assertEqual( - timestamps[0], - context_feature_dict['clip/start/timestamp'].int64_list.value[0]) - self.assertEqual( - timestamps[-1], - context_feature_dict['clip/end/timestamp'].int64_list.value[0]) - self.assertEqual( - num_frames, - context_feature_dict['clip/frames'].int64_list.value[0]) - - self.assertAllClose( - context_features, - context_feature_dict['image/context_features'].float_list.value[:]) - self.assertEqual( - context_feature_length[0], - context_feature_dict[ - 'image/context_feature_length'].int64_list.value[0]) - self.assertEqual( - context_features_image_id_list, - context_feature_dict[ - 'image/context_features_image_id_list'].bytes_list.value[:]) - - seq_feature_dict = seq_example.feature_lists.feature_list - self.assertLen( - seq_feature_dict['image/encoded'].feature[:], - num_frames) - actual_timestamps = [ - feature.int64_list.value[0] for feature - in seq_feature_dict['image/timestamp'].feature] - self.assertAllEqual(timestamps, actual_timestamps) - # Frame 0. - self.assertAllEqual( - is_annotated[0], - seq_feature_dict['region/is_annotated'].feature[0].int64_list.value[0]) - self.assertAllClose( - [0., 0.], - seq_feature_dict['region/bbox/ymin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0., 0.], - seq_feature_dict['region/bbox/xmin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0., 1.], - seq_feature_dict['region/bbox/ymax'].feature[0].float_list.value[:]) - self.assertAllClose( - [0., 1.], - seq_feature_dict['region/bbox/xmax'].feature[0].float_list.value[:]) - self.assertAllEqual( - labels, - seq_feature_dict['region/label/string'].feature[0].bytes_list.value[:]) - - # Frame 1. - self.assertAllEqual( - is_annotated[1], - seq_feature_dict['region/is_annotated'].feature[1].int64_list.value[0]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/ymin'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/xmin'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/ymax'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict['region/bbox/xmax'].feature[1].float_list.value[:]) - self.assertAllEqual( - [], - seq_feature_dict['region/label/string'].feature[1].bytes_list.value[:]) - - def test_make_labeled_example_with_predictions(self): - num_frames = 2 - image_height = 100 - image_width = 200 - dataset_name = b'unlabeled_dataset' - video_id = b'video_000' - images = tf.cast(tf.random.uniform( - [num_frames, image_height, image_width, 3], - maxval=256, - dtype=tf.int32), dtype=tf.uint8) - images_list = tf.unstack(images, axis=0) - encoded_images_list = [tf.io.encode_jpeg(image) for image in images_list] - encoded_images = self.materialize_tensors(encoded_images_list) - bboxes = [ - np.array([[0., 0., 0.75, 0.75], - [0., 0., 1., 1.]], dtype=np.float32), - np.array([[0., 0.25, 0.5, 0.75]], dtype=np.float32) - ] - label_strings = [ - np.array(['cat', 'frog']), - np.array(['cat']) - ] - detection_bboxes = [ - np.array([[0., 0., 0.75, 0.75]], dtype=np.float32), - np.zeros([0, 4], dtype=np.float32) - ] - detection_classes = [ - np.array([5], dtype=np.int64), - np.array([], dtype=np.int64) - ] - detection_scores = [ - np.array([0.9], dtype=np.float32), - np.array([], dtype=np.float32) - ] - - seq_example = seq_example_util.make_sequence_example( - dataset_name=dataset_name, - video_id=video_id, - encoded_images=encoded_images, - image_height=image_height, - image_width=image_width, - bboxes=bboxes, - label_strings=label_strings, - detection_bboxes=detection_bboxes, - detection_classes=detection_classes, - detection_scores=detection_scores) - - context_feature_dict = seq_example.context.feature - self.assertEqual( - dataset_name, - context_feature_dict['example/dataset_name'].bytes_list.value[0]) - self.assertEqual( - 0, - context_feature_dict['clip/start/timestamp'].int64_list.value[0]) - self.assertEqual( - 1, - context_feature_dict['clip/end/timestamp'].int64_list.value[0]) - self.assertEqual( - num_frames, - context_feature_dict['clip/frames'].int64_list.value[0]) - - seq_feature_dict = seq_example.feature_lists.feature_list - self.assertLen( - seq_feature_dict['image/encoded'].feature[:], - num_frames) - actual_timestamps = [ - feature.int64_list.value[0] for feature - in seq_feature_dict['image/timestamp'].feature] - self.assertAllEqual([0, 1], actual_timestamps) - # Frame 0. - self.assertAllEqual( - 1, - seq_feature_dict['region/is_annotated'].feature[0].int64_list.value[0]) - self.assertAllClose( - [0., 0.], - seq_feature_dict['region/bbox/ymin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0., 0.], - seq_feature_dict['region/bbox/xmin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.75, 1.], - seq_feature_dict['region/bbox/ymax'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.75, 1.], - seq_feature_dict['region/bbox/xmax'].feature[0].float_list.value[:]) - self.assertAllEqual( - [b'cat', b'frog'], - seq_feature_dict['region/label/string'].feature[0].bytes_list.value[:]) - self.assertAllClose( - [0.], - seq_feature_dict[ - 'predicted/region/bbox/ymin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.], - seq_feature_dict[ - 'predicted/region/bbox/xmin'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.75], - seq_feature_dict[ - 'predicted/region/bbox/ymax'].feature[0].float_list.value[:]) - self.assertAllClose( - [0.75], - seq_feature_dict[ - 'predicted/region/bbox/xmax'].feature[0].float_list.value[:]) - self.assertAllEqual( - [5], - seq_feature_dict[ - 'predicted/region/label/index'].feature[0].int64_list.value[:]) - self.assertAllClose( - [0.9], - seq_feature_dict[ - 'predicted/region/label/confidence'].feature[0].float_list.value[:]) - - # Frame 1. - self.assertAllEqual( - 1, - seq_feature_dict['region/is_annotated'].feature[1].int64_list.value[0]) - self.assertAllClose( - [0.0], - seq_feature_dict['region/bbox/ymin'].feature[1].float_list.value[:]) - self.assertAllClose( - [0.25], - seq_feature_dict['region/bbox/xmin'].feature[1].float_list.value[:]) - self.assertAllClose( - [0.5], - seq_feature_dict['region/bbox/ymax'].feature[1].float_list.value[:]) - self.assertAllClose( - [0.75], - seq_feature_dict['region/bbox/xmax'].feature[1].float_list.value[:]) - self.assertAllEqual( - [b'cat'], - seq_feature_dict['region/label/string'].feature[1].bytes_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict[ - 'predicted/region/bbox/ymin'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict[ - 'predicted/region/bbox/xmin'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict[ - 'predicted/region/bbox/ymax'].feature[1].float_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict[ - 'predicted/region/bbox/xmax'].feature[1].float_list.value[:]) - self.assertAllEqual( - [], - seq_feature_dict[ - 'predicted/region/label/index'].feature[1].int64_list.value[:]) - self.assertAllClose( - [], - seq_feature_dict[ - 'predicted/region/label/confidence'].feature[1].float_list.value[:]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dataset_tools/tf_record_creation_util.py b/research/object_detection/dataset_tools/tf_record_creation_util.py deleted file mode 100644 index 48357adf970..00000000000 --- a/research/object_detection/dataset_tools/tf_record_creation_util.py +++ /dev/null @@ -1,47 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Utilities for creating TFRecords of TF examples for the Open Images dataset. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf - - -def open_sharded_output_tfrecords(exit_stack, base_path, num_shards): - """Opens all TFRecord shards for writing and adds them to an exit stack. - - Args: - exit_stack: A context2.ExitStack used to automatically closed the TFRecords - opened in this function. - base_path: The base path for all shards - num_shards: The number of shards - - Returns: - The list of opened TFRecords. Position k in the list corresponds to shard k. - """ - tf_record_output_filenames = [ - '{}-{:05d}-of-{:05d}'.format(base_path, idx, num_shards) - for idx in range(num_shards) - ] - - tfrecords = [ - exit_stack.enter_context(tf.python_io.TFRecordWriter(file_name)) - for file_name in tf_record_output_filenames - ] - - return tfrecords diff --git a/research/object_detection/dataset_tools/tf_record_creation_util_test.py b/research/object_detection/dataset_tools/tf_record_creation_util_test.py deleted file mode 100644 index 6bf7290c8fe..00000000000 --- a/research/object_detection/dataset_tools/tf_record_creation_util_test.py +++ /dev/null @@ -1,48 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for tf_record_creation_util.py.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import os -import contextlib2 -import six -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.dataset_tools import tf_record_creation_util - - -class OpenOutputTfrecordsTests(tf.test.TestCase): - - def test_sharded_tfrecord_writes(self): - with contextlib2.ExitStack() as tf_record_close_stack: - output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords( - tf_record_close_stack, - os.path.join(tf.test.get_temp_dir(), 'test.tfrec'), 10) - for idx in range(10): - output_tfrecords[idx].write(six.ensure_binary('test_{}'.format(idx))) - - for idx in range(10): - tf_record_path = '{}-{:05d}-of-00010'.format( - os.path.join(tf.test.get_temp_dir(), 'test.tfrec'), idx) - records = list(tf.python_io.tf_record_iterator(tf_record_path)) - self.assertAllEqual(records, ['test_{}'.format(idx).encode('utf-8')]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/dockerfiles/android/Dockerfile b/research/object_detection/dockerfiles/android/Dockerfile deleted file mode 100644 index 470f669dccd..00000000000 --- a/research/object_detection/dockerfiles/android/Dockerfile +++ /dev/null @@ -1,140 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# #========================================================================== - -# Pull TF nightly-devel docker image -FROM tensorflow/tensorflow:nightly-devel - -# Get the tensorflow models research directory, and move it into tensorflow -# source folder to match recommendation of installation -RUN git clone --depth 1 https://github.com/tensorflow/models.git && \ - mv models /tensorflow/models - - -# Install gcloud and gsutil commands -# https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu -RUN apt-get -y update && apt-get install -y gpg-agent && \ - export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)" && \ - echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \ - curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \ - apt-get update -y && apt-get install google-cloud-sdk -y - - -# Install the Tensorflow Object Detection API from here -# https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md - -# Install object detection api dependencies - use non-interactive mode to set -# default tzdata config during installation. -RUN export DEBIAN_FRONTEND=noninteractive && \ - apt-get install -y protobuf-compiler python-pil python-lxml python-tk && \ - pip install Cython && \ - pip install contextlib2 && \ - pip install jupyter && \ - pip install matplotlib - -# Install pycocoapi -RUN git clone --depth 1 https://github.com/cocodataset/cocoapi.git && \ - cd cocoapi/PythonAPI && \ - make -j8 && \ - cp -r pycocotools /tensorflow/models/research && \ - cd ../../ && \ - rm -rf cocoapi - -# Get protoc 3.0.0, rather than the old version already in the container -RUN curl -OL "https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip" && \ - unzip protoc-3.0.0-linux-x86_64.zip -d proto3 && \ - mv proto3/bin/* /usr/local/bin && \ - mv proto3/include/* /usr/local/include && \ - rm -rf proto3 protoc-3.0.0-linux-x86_64.zip - -# Run protoc on the object detection repo -RUN cd /tensorflow/models/research && \ - protoc object_detection/protos/*.proto --python_out=. - -# Set the PYTHONPATH to finish installing the API -ENV PYTHONPATH $PYTHONPATH:/tensorflow/models/research:/tensorflow/models/research/slim - - -# Install wget (to make life easier below) and editors (to allow people to edit -# the files inside the container) -RUN apt-get install -y wget vim emacs nano - - -# Grab various data files which are used throughout the demo: dataset, -# pretrained model, and pretrained TensorFlow Lite model. Install these all in -# the same directories as recommended by the blog post. - -# Pets example dataset -RUN mkdir -p /tmp/pet_faces_tfrecord/ && \ - cd /tmp/pet_faces_tfrecord && \ - curl "http://download.tensorflow.org/models/object_detection/pet_faces_tfrecord.tar.gz" | tar xzf - - -# Pretrained model -# This one doesn't need its own directory, since it comes in a folder. -RUN cd /tmp && \ - curl -O "http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync_2018_07_03.tar.gz" && \ - tar xzf ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync_2018_07_03.tar.gz && \ - rm ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync_2018_07_03.tar.gz - -# Trained TensorFlow Lite model. This should get replaced by one generated from -# export_tflite_ssd_graph.py when that command is called. -RUN cd /tmp && \ - curl -L -o tflite.zip \ - https://storage.googleapis.com/download.tensorflow.org/models/tflite/frozengraphs_ssd_mobilenet_v1_0.75_quant_pets_2018_06_29.zip && \ - unzip tflite.zip -d tflite && \ - rm tflite.zip - - -# Install Android development tools -# Inspired by the following sources: -# https://github.com/bitrise-docker/android/blob/master/Dockerfile -# https://github.com/reddit/docker-android-build/blob/master/Dockerfile - -# Set environment variables -ENV ANDROID_HOME /opt/android-sdk-linux -ENV ANDROID_NDK_HOME /opt/android-ndk-r14b -ENV PATH ${PATH}:${ANDROID_HOME}/tools:${ANDROID_HOME}/tools/bin:${ANDROID_HOME}/platform-tools - -# Install SDK tools -RUN cd /opt && \ - curl -OL https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip && \ - unzip sdk-tools-linux-4333796.zip -d ${ANDROID_HOME} && \ - rm sdk-tools-linux-4333796.zip - -# Accept licenses before installing components, no need to echo y for each component -# License is valid for all the standard components in versions installed from this file -# Non-standard components: MIPS system images, preview versions, GDK (Google Glass) and Android Google TV require separate licenses, not accepted there -RUN yes | sdkmanager --licenses - -# Install platform tools, SDK platform, and other build tools -RUN yes | sdkmanager \ - "tools" \ - "platform-tools" \ - "platforms;android-27" \ - "platforms;android-23" \ - "build-tools;27.0.3" \ - "build-tools;23.0.3" - -# Install Android NDK (r14b) -RUN cd /opt && \ - curl -L -o android-ndk.zip http://dl.google.com/android/repository/android-ndk-r14b-linux-x86_64.zip && \ - unzip -q android-ndk.zip && \ - rm -f android-ndk.zip - -# Configure the build to use the things we just downloaded -RUN cd /tensorflow && \ - printf '\n\n\nn\ny\nn\nn\nn\ny\nn\nn\nn\nn\nn\nn\n\ny\n%s\n\n\n' ${ANDROID_HOME}|./configure - - -WORKDIR /tensorflow diff --git a/research/object_detection/dockerfiles/android/README.md b/research/object_detection/dockerfiles/android/README.md deleted file mode 100644 index 69016cbb019..00000000000 --- a/research/object_detection/dockerfiles/android/README.md +++ /dev/null @@ -1,69 +0,0 @@ -# Dockerfile for the TPU and TensorFlow Lite Object Detection tutorial - -This Docker image automates the setup involved with training -object detection models on Google Cloud and building the Android TensorFlow Lite -demo app. We recommend using this container if you decide to work through our -tutorial on ["Training and serving a real-time mobile object detector in -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 -like to use the Object Detection API outside the context of the tutorial. - -A couple words of warning: - -1. Docker containers do not have persistent storage. This means that any changes - you make to files inside the container will not persist if you restart - the container. When running through the tutorial, - **do not close the container**. -2. To be able to deploy the [Android app]( - https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android) - (which you will build at the end of the tutorial), - you will need to kill any instances of `adb` running on the host machine. You - can accomplish this by closing all instances of Android Studio, and then - running `adb kill-server`. - -You can install Docker by following the [instructions here]( -https://docs.docker.com/install/). - -## Running The Container - -From this directory, build the Dockerfile as follows (this takes a while): - -``` -docker build --tag detect-tf . -``` - -Run the container: - -``` -docker run --rm -it --privileged -p 6006:6006 detect-tf -``` - -When running the container, you will find yourself inside the `/tensorflow` -directory, which is the path to the TensorFlow [source -tree](https://github.com/tensorflow/tensorflow). - -## Text Editing - -The tutorial also -requires you to occasionally edit files inside the source tree. -This Docker images comes with `vim`, `nano`, and `emacs` preinstalled for your -convenience. - -## What's In This Container - -This container is derived from the nightly build of TensorFlow, and contains the -sources for TensorFlow at `/tensorflow`, as well as the -[TensorFlow Models](https://github.com/tensorflow/models) which are available at -`/tensorflow/models` (and contain the Object Detection API as a subdirectory -at `/tensorflow/models/research/object_detection`). -The Oxford-IIIT Pets dataset, the COCO pre-trained SSD + MobileNet (v1) -checkpoint, and example -trained model are all available in `/tmp` in their respective folders. - -This container also has the `gsutil` and `gcloud` utilities, the `bazel` build -tool, and all dependencies necessary to use the Object Detection API, and -compile and install the TensorFlow Lite Android demo app. - -At various points throughout the tutorial, you may see references to the -*research directory*. This refers to the `research` folder within the -models repository, located at -`/tensorflow/models/research`. diff --git a/research/object_detection/dockerfiles/tf1/Dockerfile b/research/object_detection/dockerfiles/tf1/Dockerfile deleted file mode 100644 index 9d77523096a..00000000000 --- a/research/object_detection/dockerfiles/tf1/Dockerfile +++ /dev/null @@ -1,41 +0,0 @@ -FROM tensorflow/tensorflow:1.15.2-gpu-py3 - -ARG DEBIAN_FRONTEND=noninteractive - -# Install apt dependencies -RUN apt-get update && apt-get install -y \ - git \ - gpg-agent \ - python3-cairocffi \ - protobuf-compiler \ - python3-pil \ - python3-lxml \ - python3-tk \ - wget - -# Install gcloud and gsutil commands -# https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu -RUN export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)" && \ - echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \ - curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \ - apt-get update -y && apt-get install google-cloud-sdk -y - -# Add new user to avoid running as root -RUN useradd -ms /bin/bash tensorflow -USER tensorflow -WORKDIR /home/tensorflow - -# Copy this version of of the model garden into the image -COPY --chown=tensorflow . /home/tensorflow/models - -# Compile protobuf configs -RUN (cd /home/tensorflow/models/research/ && protoc object_detection/protos/*.proto --python_out=.) -WORKDIR /home/tensorflow/models/research/ - -RUN cp object_detection/packages/tf1/setup.py ./ -ENV PATH="/home/tensorflow/.local/bin:${PATH}" - -RUN python -m pip install --user -U pip -RUN python -m pip install --user . - -ENV TF_CPP_MIN_LOG_LEVEL 3 diff --git a/research/object_detection/dockerfiles/tf1/README.md b/research/object_detection/dockerfiles/tf1/README.md deleted file mode 100644 index 9e4503ca0fa..00000000000 --- a/research/object_detection/dockerfiles/tf1/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# TensorFlow Object Detection on Docker - -These instructions are experimental. - -## Building and running: - -```bash -# From the root of the git repository -docker build -f research/object_detection/dockerfiles/tf1/Dockerfile -t od . -docker run -it od -``` diff --git a/research/object_detection/dockerfiles/tf2/Dockerfile b/research/object_detection/dockerfiles/tf2/Dockerfile deleted file mode 100644 index c4dfc6b2307..00000000000 --- a/research/object_detection/dockerfiles/tf2/Dockerfile +++ /dev/null @@ -1,41 +0,0 @@ -FROM tensorflow/tensorflow:2.2.0-gpu - -ARG DEBIAN_FRONTEND=noninteractive - -# Install apt dependencies -RUN apt-get update && apt-get install -y \ - git \ - gpg-agent \ - python3-cairocffi \ - protobuf-compiler \ - python3-pil \ - python3-lxml \ - python3-tk \ - wget - -# Install gcloud and gsutil commands -# https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu -RUN export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)" && \ - echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \ - curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - && \ - apt-get update -y && apt-get install google-cloud-sdk -y - -# Add new user to avoid running as root -RUN useradd -ms /bin/bash tensorflow -USER tensorflow -WORKDIR /home/tensorflow - -# Copy this version of of the model garden into the image -COPY --chown=tensorflow . /home/tensorflow/models - -# Compile protobuf configs -RUN (cd /home/tensorflow/models/research/ && protoc object_detection/protos/*.proto --python_out=.) -WORKDIR /home/tensorflow/models/research/ - -RUN cp object_detection/packages/tf2/setup.py ./ -ENV PATH="/home/tensorflow/.local/bin:${PATH}" - -RUN python -m pip install -U pip -RUN python -m pip install . - -ENV TF_CPP_MIN_LOG_LEVEL 3 diff --git a/research/object_detection/dockerfiles/tf2/README.md b/research/object_detection/dockerfiles/tf2/README.md deleted file mode 100644 index 14b5184c55d..00000000000 --- a/research/object_detection/dockerfiles/tf2/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# TensorFlow Object Detection on Docker - -These instructions are experimental. - -## Building and running: - -```bash -# From the root of the git repository -docker build -f research/object_detection/dockerfiles/tf2/Dockerfile -t od . -docker run -it od -``` diff --git a/research/object_detection/dockerfiles/tf2_ai_platform/Dockerfile b/research/object_detection/dockerfiles/tf2_ai_platform/Dockerfile deleted file mode 100644 index 0b43e0620f7..00000000000 --- a/research/object_detection/dockerfiles/tf2_ai_platform/Dockerfile +++ /dev/null @@ -1,50 +0,0 @@ -FROM tensorflow/tensorflow:latest-gpu - -ARG DEBIAN_FRONTEND=noninteractive - -# Install apt dependencies -RUN apt-get update && apt-get install -y \ - git \ - gpg-agent \ - python3-cairocffi \ - protobuf-compiler \ - python3-pil \ - python3-lxml \ - python3-tk \ - python3-opencv \ - wget - -# Installs google cloud sdk, this is mostly for using gsutil to export model. -RUN wget -nv \ - https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz && \ - mkdir /root/tools && \ - tar xvzf google-cloud-sdk.tar.gz -C /root/tools && \ - rm google-cloud-sdk.tar.gz && \ - /root/tools/google-cloud-sdk/install.sh --usage-reporting=false \ - --path-update=false --bash-completion=false \ - --disable-installation-options && \ - rm -rf /root/.config/* && \ - ln -s /root/.config /config && \ - rm -rf /root/tools/google-cloud-sdk/.install/.backup - -# Path configuration -ENV PATH $PATH:/root/tools/google-cloud-sdk/bin -# Make sure gsutil will use the default service account -RUN echo '[GoogleCompute]\nservice_account = default' > /etc/boto.cfg - -WORKDIR /home/tensorflow - -## Copy this code (make sure you are under the ../models/research directory) -COPY . /home/tensorflow/models - -# Compile protobuf configs -RUN (cd /home/tensorflow/models/ && protoc object_detection/protos/*.proto --python_out=.) -WORKDIR /home/tensorflow/models/ - -RUN cp object_detection/packages/tf2/setup.py ./ -ENV PATH="/home/tensorflow/.local/bin:${PATH}" - -RUN python -m pip install -U pip -RUN python -m pip install . - -ENTRYPOINT ["python", "object_detection/model_main_tf2.py"] diff --git a/research/object_detection/eval_util.py b/research/object_detection/eval_util.py deleted file mode 100644 index 519e8c97aef..00000000000 --- a/research/object_detection/eval_util.py +++ /dev/null @@ -1,1221 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Common utility functions for evaluation.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import os -import re -import time - -import numpy as np -from six.moves import range -import tensorflow.compat.v1 as tf - -import tf_slim as slim - -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import keypoint_ops -from object_detection.core import standard_fields as fields -from object_detection.metrics import coco_evaluation -from object_detection.metrics import lvis_evaluation -from object_detection.protos import eval_pb2 -from object_detection.utils import label_map_util -from object_detection.utils import object_detection_evaluation -from object_detection.utils import ops -from object_detection.utils import shape_utils -from object_detection.utils import visualization_utils as vis_utils - -EVAL_KEYPOINT_METRIC = 'coco_keypoint_metrics' - -# A dictionary of metric names to classes that implement the metric. The classes -# in the dictionary must implement -# utils.object_detection_evaluation.DetectionEvaluator interface. -EVAL_METRICS_CLASS_DICT = { - 'coco_detection_metrics': - coco_evaluation.CocoDetectionEvaluator, - 'coco_keypoint_metrics': - coco_evaluation.CocoKeypointEvaluator, - 'coco_mask_metrics': - coco_evaluation.CocoMaskEvaluator, - 'coco_panoptic_metrics': - coco_evaluation.CocoPanopticSegmentationEvaluator, - 'lvis_mask_metrics': - lvis_evaluation.LVISMaskEvaluator, - 'oid_challenge_detection_metrics': - object_detection_evaluation.OpenImagesDetectionChallengeEvaluator, - 'oid_challenge_segmentation_metrics': - object_detection_evaluation - .OpenImagesInstanceSegmentationChallengeEvaluator, - 'pascal_voc_detection_metrics': - object_detection_evaluation.PascalDetectionEvaluator, - 'weighted_pascal_voc_detection_metrics': - object_detection_evaluation.WeightedPascalDetectionEvaluator, - 'precision_at_recall_detection_metrics': - object_detection_evaluation.PrecisionAtRecallDetectionEvaluator, - 'pascal_voc_instance_segmentation_metrics': - object_detection_evaluation.PascalInstanceSegmentationEvaluator, - 'weighted_pascal_voc_instance_segmentation_metrics': - object_detection_evaluation.WeightedPascalInstanceSegmentationEvaluator, - 'oid_V2_detection_metrics': - object_detection_evaluation.OpenImagesDetectionEvaluator, -} - -EVAL_DEFAULT_METRIC = 'coco_detection_metrics' - - -def write_metrics(metrics, global_step, summary_dir): - """Write metrics to a summary directory. - - Args: - metrics: A dictionary containing metric names and values. - global_step: Global step at which the metrics are computed. - summary_dir: Directory to write tensorflow summaries to. - """ - tf.logging.info('Writing metrics to tf summary.') - summary_writer = tf.summary.FileWriterCache.get(summary_dir) - for key in sorted(metrics): - summary = tf.Summary(value=[ - tf.Summary.Value(tag=key, simple_value=metrics[key]), - ]) - summary_writer.add_summary(summary, global_step) - tf.logging.info('%s: %f', key, metrics[key]) - tf.logging.info('Metrics written to tf summary.') - - -# TODO(rathodv): Add tests. -def visualize_detection_results(result_dict, - tag, - global_step, - categories, - summary_dir='', - export_dir='', - agnostic_mode=False, - show_groundtruth=False, - groundtruth_box_visualization_color='black', - min_score_thresh=.5, - max_num_predictions=20, - skip_scores=False, - skip_labels=False, - keep_image_id_for_visualization_export=False): - """Visualizes detection results and writes visualizations to image summaries. - - This function visualizes an image with its detected bounding boxes and writes - to image summaries which can be viewed on tensorboard. It optionally also - writes images to a directory. In the case of missing entry in the label map, - unknown class name in the visualization is shown as "N/A". - - Args: - result_dict: a dictionary holding groundtruth and detection - data corresponding to each image being evaluated. The following keys - are required: - 'original_image': a numpy array representing the image with shape - [1, height, width, 3] or [1, height, width, 1] - 'detection_boxes': a numpy array of shape [N, 4] - 'detection_scores': a numpy array of shape [N] - 'detection_classes': a numpy array of shape [N] - The following keys are optional: - 'groundtruth_boxes': a numpy array of shape [N, 4] - 'groundtruth_keypoints': a numpy array of shape [N, num_keypoints, 2] - Detections are assumed to be provided in decreasing order of score and for - display, and we assume that scores are probabilities between 0 and 1. - tag: tensorboard tag (string) to associate with image. - global_step: global step at which the visualization are generated. - categories: a list of dictionaries representing all possible categories. - Each dict in this list has the following keys: - 'id': (required) an integer id uniquely identifying this category - 'name': (required) string representing category name - e.g., 'cat', 'dog', 'pizza' - 'supercategory': (optional) string representing the supercategory - e.g., 'animal', 'vehicle', 'food', etc - summary_dir: the output directory to which the image summaries are written. - export_dir: the output directory to which images are written. If this is - empty (default), then images are not exported. - agnostic_mode: boolean (default: False) controlling whether to evaluate in - class-agnostic mode or not. - show_groundtruth: boolean (default: False) controlling whether to show - groundtruth boxes in addition to detected boxes - groundtruth_box_visualization_color: box color for visualizing groundtruth - boxes - min_score_thresh: minimum score threshold for a box to be visualized - max_num_predictions: maximum number of detections to visualize - skip_scores: whether to skip score when drawing a single detection - skip_labels: whether to skip label when drawing a single detection - keep_image_id_for_visualization_export: whether to keep image identifier in - filename when exported to export_dir - Raises: - ValueError: if result_dict does not contain the expected keys (i.e., - 'original_image', 'detection_boxes', 'detection_scores', - 'detection_classes') - """ - detection_fields = fields.DetectionResultFields - input_fields = fields.InputDataFields - if not set([ - input_fields.original_image, - detection_fields.detection_boxes, - detection_fields.detection_scores, - detection_fields.detection_classes, - ]).issubset(set(result_dict.keys())): - raise ValueError('result_dict does not contain all expected keys.') - if show_groundtruth and input_fields.groundtruth_boxes not in result_dict: - raise ValueError('If show_groundtruth is enabled, result_dict must contain ' - 'groundtruth_boxes.') - tf.logging.info('Creating detection visualizations.') - category_index = label_map_util.create_category_index(categories) - - image = np.squeeze(result_dict[input_fields.original_image], axis=0) - if image.shape[2] == 1: # If one channel image, repeat in RGB. - image = np.tile(image, [1, 1, 3]) - detection_boxes = result_dict[detection_fields.detection_boxes] - detection_scores = result_dict[detection_fields.detection_scores] - detection_classes = np.int32((result_dict[ - detection_fields.detection_classes])) - detection_keypoints = result_dict.get(detection_fields.detection_keypoints) - detection_masks = result_dict.get(detection_fields.detection_masks) - detection_boundaries = result_dict.get(detection_fields.detection_boundaries) - - # Plot groundtruth underneath detections - if show_groundtruth: - groundtruth_boxes = result_dict[input_fields.groundtruth_boxes] - groundtruth_keypoints = result_dict.get(input_fields.groundtruth_keypoints) - vis_utils.visualize_boxes_and_labels_on_image_array( - image=image, - boxes=groundtruth_boxes, - classes=None, - scores=None, - category_index=category_index, - keypoints=groundtruth_keypoints, - use_normalized_coordinates=False, - max_boxes_to_draw=None, - groundtruth_box_visualization_color=groundtruth_box_visualization_color) - vis_utils.visualize_boxes_and_labels_on_image_array( - image, - detection_boxes, - detection_classes, - detection_scores, - category_index, - instance_masks=detection_masks, - instance_boundaries=detection_boundaries, - keypoints=detection_keypoints, - use_normalized_coordinates=False, - max_boxes_to_draw=max_num_predictions, - min_score_thresh=min_score_thresh, - agnostic_mode=agnostic_mode, - skip_scores=skip_scores, - skip_labels=skip_labels) - - if export_dir: - if keep_image_id_for_visualization_export and result_dict[fields. - InputDataFields() - .key]: - export_path = os.path.join(export_dir, 'export-{}-{}.png'.format( - tag, result_dict[fields.InputDataFields().key])) - else: - export_path = os.path.join(export_dir, 'export-{}.png'.format(tag)) - vis_utils.save_image_array_as_png(image, export_path) - - summary = tf.Summary(value=[ - tf.Summary.Value( - tag=tag, - image=tf.Summary.Image( - encoded_image_string=vis_utils.encode_image_array_as_png_str( - image))) - ]) - summary_writer = tf.summary.FileWriterCache.get(summary_dir) - summary_writer.add_summary(summary, global_step) - - tf.logging.info('Detection visualizations written to summary with tag %s.', - tag) - - -def _run_checkpoint_once(tensor_dict, - evaluators=None, - batch_processor=None, - checkpoint_dirs=None, - variables_to_restore=None, - restore_fn=None, - num_batches=1, - master='', - save_graph=False, - save_graph_dir='', - losses_dict=None, - eval_export_path=None, - process_metrics_fn=None): - """Evaluates metrics defined in evaluators and returns summaries. - - This function loads the latest checkpoint in checkpoint_dirs and evaluates - all metrics defined in evaluators. The metrics are processed in batch by the - batch_processor. - - Args: - tensor_dict: a dictionary holding tensors representing a batch of detections - and corresponding groundtruth annotations. - evaluators: a list of object of type DetectionEvaluator to be used for - evaluation. Note that the metric names produced by different evaluators - must be unique. - batch_processor: a function taking four arguments: - 1. tensor_dict: the same tensor_dict that is passed in as the first - argument to this function. - 2. sess: a tensorflow session - 3. batch_index: an integer representing the index of the batch amongst - all batches - By default, batch_processor is None, which defaults to running: - return sess.run(tensor_dict) - To skip an image, it suffices to return an empty dictionary in place of - result_dict. - checkpoint_dirs: list of directories to load into an EnsembleModel. If it - has only one directory, EnsembleModel will not be used -- - a DetectionModel - will be instantiated directly. Not used if restore_fn is set. - variables_to_restore: None, or a dictionary mapping variable names found in - a checkpoint to model variables. The dictionary would normally be - generated by creating a tf.train.ExponentialMovingAverage object and - calling its variables_to_restore() method. Not used if restore_fn is set. - restore_fn: None, or a function that takes a tf.Session object and correctly - restores all necessary variables from the correct checkpoint file. If - None, attempts to restore from the first directory in checkpoint_dirs. - num_batches: the number of batches to use for evaluation. - master: the location of the Tensorflow session. - save_graph: whether or not the Tensorflow graph is stored as a pbtxt file. - save_graph_dir: where to store the Tensorflow graph on disk. If save_graph - is True this must be non-empty. - losses_dict: optional dictionary of scalar detection losses. - eval_export_path: Path for saving a json file that contains the detection - results in json format. - process_metrics_fn: a callback called with evaluation results after each - evaluation is done. It could be used e.g. to back up checkpoints with - best evaluation scores, or to call an external system to update evaluation - results in order to drive best hyper-parameter search. Parameters are: - int checkpoint_number, Dict[str, ObjectDetectionEvalMetrics] metrics, - str checkpoint_file path. - - Returns: - global_step: the count of global steps. - all_evaluator_metrics: A dictionary containing metric names and values. - - Raises: - ValueError: if restore_fn is None and checkpoint_dirs doesn't have at least - one element. - ValueError: if save_graph is True and save_graph_dir is not defined. - """ - if save_graph and not save_graph_dir: - raise ValueError('`save_graph_dir` must be defined.') - sess = tf.Session(master, graph=tf.get_default_graph()) - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - sess.run(tf.tables_initializer()) - checkpoint_file = None - if restore_fn: - restore_fn(sess) - else: - if not checkpoint_dirs: - raise ValueError('`checkpoint_dirs` must have at least one entry.') - checkpoint_file = tf.train.latest_checkpoint(checkpoint_dirs[0]) - saver = tf.train.Saver(variables_to_restore) - saver.restore(sess, checkpoint_file) - - if save_graph: - tf.train.write_graph(sess.graph_def, save_graph_dir, 'eval.pbtxt') - - counters = {'skipped': 0, 'success': 0} - aggregate_result_losses_dict = collections.defaultdict(list) - with slim.queues.QueueRunners(sess): - try: - for batch in range(int(num_batches)): - if (batch + 1) % 100 == 0: - tf.logging.info('Running eval ops batch %d/%d', batch + 1, - num_batches) - if not batch_processor: - try: - if not losses_dict: - losses_dict = {} - result_dict, result_losses_dict = sess.run([tensor_dict, - losses_dict]) - counters['success'] += 1 - except tf.errors.InvalidArgumentError: - tf.logging.info('Skipping image') - counters['skipped'] += 1 - result_dict = {} - else: - result_dict, result_losses_dict = batch_processor( - tensor_dict, sess, batch, counters, losses_dict=losses_dict) - if not result_dict: - continue - for key, value in iter(result_losses_dict.items()): - aggregate_result_losses_dict[key].append(value) - for evaluator in evaluators: - # TODO(b/65130867): Use image_id tensor once we fix the input data - # decoders to return correct image_id. - # TODO(akuznetsa): result_dict contains batches of images, while - # add_single_ground_truth_image_info expects a single image. Fix - if (isinstance(result_dict, dict) and - fields.InputDataFields.key in result_dict and - result_dict[fields.InputDataFields.key]): - image_id = result_dict[fields.InputDataFields.key] - else: - image_id = batch - evaluator.add_single_ground_truth_image_info( - image_id=image_id, groundtruth_dict=result_dict) - evaluator.add_single_detected_image_info( - image_id=image_id, detections_dict=result_dict) - tf.logging.info('Running eval batches done.') - except tf.errors.OutOfRangeError: - tf.logging.info('Done evaluating -- epoch limit reached') - finally: - # When done, ask the threads to stop. - tf.logging.info('# success: %d', counters['success']) - tf.logging.info('# skipped: %d', counters['skipped']) - all_evaluator_metrics = {} - if eval_export_path and eval_export_path is not None: - for evaluator in evaluators: - if (isinstance(evaluator, coco_evaluation.CocoDetectionEvaluator) or - isinstance(evaluator, coco_evaluation.CocoMaskEvaluator)): - tf.logging.info('Started dumping to json file.') - evaluator.dump_detections_to_json_file( - json_output_path=eval_export_path) - tf.logging.info('Finished dumping to json file.') - for evaluator in evaluators: - metrics = evaluator.evaluate() - evaluator.clear() - if any(key in all_evaluator_metrics for key in metrics): - raise ValueError('Metric names between evaluators must not collide.') - all_evaluator_metrics.update(metrics) - global_step = tf.train.global_step(sess, tf.train.get_global_step()) - - for key, value in iter(aggregate_result_losses_dict.items()): - all_evaluator_metrics['Losses/' + key] = np.mean(value) - if process_metrics_fn and checkpoint_file: - m = re.search(r'model.ckpt-(\d+)$', checkpoint_file) - if not m: - tf.logging.error('Failed to parse checkpoint number from: %s', - checkpoint_file) - else: - checkpoint_number = int(m.group(1)) - process_metrics_fn(checkpoint_number, all_evaluator_metrics, - checkpoint_file) - sess.close() - return (global_step, all_evaluator_metrics) - - -# TODO(rathodv): Add tests. -def repeated_checkpoint_run(tensor_dict, - summary_dir, - evaluators, - batch_processor=None, - checkpoint_dirs=None, - variables_to_restore=None, - restore_fn=None, - num_batches=1, - eval_interval_secs=120, - max_number_of_evaluations=None, - max_evaluation_global_step=None, - master='', - save_graph=False, - save_graph_dir='', - losses_dict=None, - eval_export_path=None, - process_metrics_fn=None): - """Periodically evaluates desired tensors using checkpoint_dirs or restore_fn. - - This function repeatedly loads a checkpoint and evaluates a desired - set of tensors (provided by tensor_dict) and hands the resulting numpy - arrays to a function result_processor which can be used to further - process/save/visualize the results. - - Args: - tensor_dict: a dictionary holding tensors representing a batch of detections - and corresponding groundtruth annotations. - summary_dir: a directory to write metrics summaries. - evaluators: a list of object of type DetectionEvaluator to be used for - evaluation. Note that the metric names produced by different evaluators - must be unique. - batch_processor: a function taking three arguments: - 1. tensor_dict: the same tensor_dict that is passed in as the first - argument to this function. - 2. sess: a tensorflow session - 3. batch_index: an integer representing the index of the batch amongst - all batches - By default, batch_processor is None, which defaults to running: - return sess.run(tensor_dict) - checkpoint_dirs: list of directories to load into a DetectionModel or an - EnsembleModel if restore_fn isn't set. Also used to determine when to run - next evaluation. Must have at least one element. - variables_to_restore: None, or a dictionary mapping variable names found in - a checkpoint to model variables. The dictionary would normally be - generated by creating a tf.train.ExponentialMovingAverage object and - calling its variables_to_restore() method. Not used if restore_fn is set. - restore_fn: a function that takes a tf.Session object and correctly restores - all necessary variables from the correct checkpoint file. - num_batches: the number of batches to use for evaluation. - eval_interval_secs: the number of seconds between each evaluation run. - max_number_of_evaluations: the max number of iterations of the evaluation. - If the value is left as None the evaluation continues indefinitely. - max_evaluation_global_step: global step when evaluation stops. - master: the location of the Tensorflow session. - save_graph: whether or not the Tensorflow graph is saved as a pbtxt file. - save_graph_dir: where to save on disk the Tensorflow graph. If store_graph - is True this must be non-empty. - losses_dict: optional dictionary of scalar detection losses. - eval_export_path: Path for saving a json file that contains the detection - results in json format. - process_metrics_fn: a callback called with evaluation results after each - evaluation is done. It could be used e.g. to back up checkpoints with - best evaluation scores, or to call an external system to update evaluation - results in order to drive best hyper-parameter search. Parameters are: - int checkpoint_number, Dict[str, ObjectDetectionEvalMetrics] metrics, - str checkpoint_file path. - - Returns: - metrics: A dictionary containing metric names and values in the latest - evaluation. - - Raises: - ValueError: if max_num_of_evaluations is not None or a positive number. - ValueError: if checkpoint_dirs doesn't have at least one element. - """ - if max_number_of_evaluations and max_number_of_evaluations <= 0: - raise ValueError( - '`max_number_of_evaluations` must be either None or a positive number.') - if max_evaluation_global_step and max_evaluation_global_step <= 0: - raise ValueError( - '`max_evaluation_global_step` must be either None or positive.') - - if not checkpoint_dirs: - raise ValueError('`checkpoint_dirs` must have at least one entry.') - - last_evaluated_model_path = None - number_of_evaluations = 0 - while True: - start = time.time() - tf.logging.info('Starting evaluation at ' + time.strftime( - '%Y-%m-%d-%H:%M:%S', time.gmtime())) - model_path = tf.train.latest_checkpoint(checkpoint_dirs[0]) - if not model_path: - tf.logging.info('No model found in %s. Will try again in %d seconds', - checkpoint_dirs[0], eval_interval_secs) - elif model_path == last_evaluated_model_path: - tf.logging.info('Found already evaluated checkpoint. Will try again in ' - '%d seconds', eval_interval_secs) - else: - last_evaluated_model_path = model_path - global_step, metrics = _run_checkpoint_once( - tensor_dict, - evaluators, - batch_processor, - checkpoint_dirs, - variables_to_restore, - restore_fn, - num_batches, - master, - save_graph, - save_graph_dir, - losses_dict=losses_dict, - eval_export_path=eval_export_path, - process_metrics_fn=process_metrics_fn) - write_metrics(metrics, global_step, summary_dir) - if (max_evaluation_global_step and - global_step >= max_evaluation_global_step): - tf.logging.info('Finished evaluation!') - break - number_of_evaluations += 1 - - if (max_number_of_evaluations and - number_of_evaluations >= max_number_of_evaluations): - tf.logging.info('Finished evaluation!') - break - time_to_next_eval = start + eval_interval_secs - time.time() - if time_to_next_eval > 0: - time.sleep(time_to_next_eval) - - return metrics - - -def _scale_box_to_absolute(args): - boxes, image_shape = args - return box_list_ops.to_absolute_coordinates( - box_list.BoxList(boxes), image_shape[0], image_shape[1]).get() - - -def _resize_detection_masks(arg_tuple): - """Resizes detection masks. - - Args: - arg_tuple: A (detection_boxes, detection_masks, image_shape, pad_shape) - tuple where - detection_boxes is a tf.float32 tensor of size [num_masks, 4] containing - the box corners. Row i contains [ymin, xmin, ymax, xmax] of the box - corresponding to mask i. Note that the box corners are in - normalized coordinates. - detection_masks is a tensor of size - [num_masks, mask_height, mask_width]. - image_shape is a tensor of shape [2] - pad_shape is a tensor of shape [2] --- this is assumed to be greater - than or equal to image_shape along both dimensions and represents a - shape to-be-padded-to. - - Returns: - """ - - detection_boxes, detection_masks, image_shape, pad_shape = arg_tuple - - detection_masks_reframed = ops.reframe_box_masks_to_image_masks( - detection_masks, detection_boxes, image_shape[0], image_shape[1]) - - pad_instance_dim = tf.zeros([3, 1], dtype=tf.int32) - pad_hw_dim = tf.concat([tf.zeros([1], dtype=tf.int32), - pad_shape - image_shape], axis=0) - pad_hw_dim = tf.expand_dims(pad_hw_dim, 1) - paddings = tf.concat([pad_instance_dim, pad_hw_dim], axis=1) - detection_masks_reframed = tf.pad(detection_masks_reframed, paddings) - - # If the masks are currently float, binarize them. Otherwise keep them as - # integers, since they have already been thresholded. - if detection_masks_reframed.dtype == tf.float32: - detection_masks_reframed = tf.greater(detection_masks_reframed, 0.5) - return tf.cast(detection_masks_reframed, tf.uint8) - - -def resize_detection_masks(detection_boxes, detection_masks, - original_image_spatial_shapes): - """Resizes per-box detection masks to be relative to the entire image. - - Note that this function only works when the spatial size of all images in - the batch is the same. If not, this function should be used with batch_size=1. - - Args: - detection_boxes: A [batch_size, num_instances, 4] float tensor containing - bounding boxes. - detection_masks: A [batch_size, num_instances, height, width] float tensor - containing binary instance masks per box. - original_image_spatial_shapes: a [batch_size, 3] shaped int tensor - holding the spatial dimensions of each image in the batch. - Returns: - masks: Masks resized to the spatial extents given by - (original_image_spatial_shapes[0, 0], original_image_spatial_shapes[0, 1]) - """ - # modify original image spatial shapes to be max along each dim - # in evaluator, should have access to original_image_spatial_shape field - # in add_Eval_Dict - max_spatial_shape = tf.reduce_max( - original_image_spatial_shapes, axis=0, keep_dims=True) - tiled_max_spatial_shape = tf.tile( - max_spatial_shape, - multiples=[tf.shape(original_image_spatial_shapes)[0], 1]) - return shape_utils.static_or_dynamic_map_fn( - _resize_detection_masks, - elems=[detection_boxes, - detection_masks, - original_image_spatial_shapes, - tiled_max_spatial_shape], - dtype=tf.uint8) - - -def _resize_groundtruth_masks(args): - """Resizes groundtruth masks to the original image size.""" - mask, true_image_shape, original_image_shape, pad_shape = args - true_height = true_image_shape[0] - true_width = true_image_shape[1] - mask = mask[:, :true_height, :true_width] - mask = tf.expand_dims(mask, 3) - mask = tf.image.resize_images( - mask, - original_image_shape, - method=tf.image.ResizeMethod.NEAREST_NEIGHBOR, - align_corners=True) - - paddings = tf.concat( - [tf.zeros([3, 1], dtype=tf.int32), - tf.expand_dims( - tf.concat([tf.zeros([1], dtype=tf.int32), - pad_shape-original_image_shape], axis=0), - 1)], axis=1) - mask = tf.pad(tf.squeeze(mask, 3), paddings) - return tf.cast(mask, tf.uint8) - - -def _resize_surface_coordinate_masks(args): - detection_boxes, surface_coords, image_shape = args - surface_coords_v, surface_coords_u = tf.unstack(surface_coords, axis=-1) - surface_coords_v_reframed = ops.reframe_box_masks_to_image_masks( - surface_coords_v, detection_boxes, image_shape[0], image_shape[1]) - surface_coords_u_reframed = ops.reframe_box_masks_to_image_masks( - surface_coords_u, detection_boxes, image_shape[0], image_shape[1]) - return tf.stack([surface_coords_v_reframed, surface_coords_u_reframed], - axis=-1) - - -def _scale_keypoint_to_absolute(args): - keypoints, image_shape = args - return keypoint_ops.scale(keypoints, image_shape[0], image_shape[1]) - - -def result_dict_for_single_example(image, - key, - detections, - groundtruth=None, - class_agnostic=False, - scale_to_absolute=False): - """Merges all detection and groundtruth information for a single example. - - Note that evaluation tools require classes that are 1-indexed, and so this - function performs the offset. If `class_agnostic` is True, all output classes - have label 1. - - Args: - image: A single 4D uint8 image tensor of shape [1, H, W, C]. - key: A single string tensor identifying the image. - detections: A dictionary of detections, returned from - DetectionModel.postprocess(). - groundtruth: (Optional) Dictionary of groundtruth items, with fields: - 'groundtruth_boxes': [num_boxes, 4] float32 tensor of boxes, in - normalized coordinates. - 'groundtruth_classes': [num_boxes] int64 tensor of 1-indexed classes. - 'groundtruth_area': [num_boxes] float32 tensor of bbox area. (Optional) - 'groundtruth_is_crowd': [num_boxes] int64 tensor. (Optional) - 'groundtruth_difficult': [num_boxes] int64 tensor. (Optional) - 'groundtruth_group_of': [num_boxes] int64 tensor. (Optional) - 'groundtruth_instance_masks': 3D int64 tensor of instance masks - (Optional). - 'groundtruth_keypoints': [num_boxes, num_keypoints, 2] float32 tensor with - keypoints (Optional). - class_agnostic: Boolean indicating whether the detections are class-agnostic - (i.e. binary). Default False. - scale_to_absolute: Boolean indicating whether boxes and keypoints should be - scaled to absolute coordinates. Note that for IoU based evaluations, it - does not matter whether boxes are expressed in absolute or relative - coordinates. Default False. - - Returns: - A dictionary with: - 'original_image': A [1, H, W, C] uint8 image tensor. - 'key': A string tensor with image identifier. - 'detection_boxes': [max_detections, 4] float32 tensor of boxes, in - normalized or absolute coordinates, depending on the value of - `scale_to_absolute`. - 'detection_scores': [max_detections] float32 tensor of scores. - 'detection_classes': [max_detections] int64 tensor of 1-indexed classes. - 'detection_masks': [max_detections, H, W] float32 tensor of binarized - masks, reframed to full image masks. - 'groundtruth_boxes': [num_boxes, 4] float32 tensor of boxes, in - normalized or absolute coordinates, depending on the value of - `scale_to_absolute`. (Optional) - 'groundtruth_classes': [num_boxes] int64 tensor of 1-indexed classes. - (Optional) - 'groundtruth_area': [num_boxes] float32 tensor of bbox area. (Optional) - 'groundtruth_is_crowd': [num_boxes] int64 tensor. (Optional) - 'groundtruth_difficult': [num_boxes] int64 tensor. (Optional) - 'groundtruth_group_of': [num_boxes] int64 tensor. (Optional) - 'groundtruth_instance_masks': 3D int64 tensor of instance masks - (Optional). - 'groundtruth_keypoints': [num_boxes, num_keypoints, 2] float32 tensor with - keypoints (Optional). - """ - - if groundtruth: - max_gt_boxes = tf.shape( - groundtruth[fields.InputDataFields.groundtruth_boxes])[0] - for gt_key in groundtruth: - # expand groundtruth dict along the batch dimension. - groundtruth[gt_key] = tf.expand_dims(groundtruth[gt_key], 0) - - for detection_key in detections: - detections[detection_key] = tf.expand_dims( - detections[detection_key][0], axis=0) - - batched_output_dict = result_dict_for_batched_example( - image, - tf.expand_dims(key, 0), - detections, - groundtruth, - class_agnostic, - scale_to_absolute, - max_gt_boxes=max_gt_boxes) - - exclude_keys = [ - fields.InputDataFields.original_image, - fields.DetectionResultFields.num_detections, - fields.InputDataFields.num_groundtruth_boxes - ] - - output_dict = { - fields.InputDataFields.original_image: - batched_output_dict[fields.InputDataFields.original_image] - } - - for key in batched_output_dict: - # remove the batch dimension. - if key not in exclude_keys: - output_dict[key] = tf.squeeze(batched_output_dict[key], 0) - return output_dict - - -def result_dict_for_batched_example(images, - keys, - detections, - groundtruth=None, - class_agnostic=False, - scale_to_absolute=False, - original_image_spatial_shapes=None, - true_image_shapes=None, - max_gt_boxes=None, - label_id_offset=1): - """Merges all detection and groundtruth information for a single example. - - Note that evaluation tools require classes that are 1-indexed, and so this - function performs the offset. If `class_agnostic` is True, all output classes - have label 1. - The groundtruth coordinates of boxes/keypoints in 'groundtruth' dictionary are - normalized relative to the (potentially padded) input image, while the - coordinates in 'detection' dictionary are normalized relative to the true - image shape. - - Args: - images: A single 4D uint8 image tensor of shape [batch_size, H, W, C]. - keys: A [batch_size] string/int tensor with image identifier. - detections: A dictionary of detections, returned from - DetectionModel.postprocess(). - groundtruth: (Optional) Dictionary of groundtruth items, with fields: - 'groundtruth_boxes': [batch_size, max_number_of_boxes, 4] float32 tensor - of boxes, in normalized coordinates. - 'groundtruth_classes': [batch_size, max_number_of_boxes] int64 tensor of - 1-indexed classes. - 'groundtruth_area': [batch_size, max_number_of_boxes] float32 tensor of - bbox area. (Optional) - 'groundtruth_is_crowd':[batch_size, max_number_of_boxes] int64 - tensor. (Optional) - 'groundtruth_difficult': [batch_size, max_number_of_boxes] int64 - tensor. (Optional) - 'groundtruth_group_of': [batch_size, max_number_of_boxes] int64 - tensor. (Optional) - 'groundtruth_instance_masks': 4D int64 tensor of instance - masks (Optional). - 'groundtruth_keypoints': [batch_size, max_number_of_boxes, num_keypoints, - 2] float32 tensor with keypoints (Optional). - 'groundtruth_keypoint_visibilities': [batch_size, max_number_of_boxes, - num_keypoints] bool tensor with keypoint visibilities (Optional). - 'groundtruth_labeled_classes': [batch_size, num_classes] int64 - tensor of 1-indexed classes. (Optional) - 'groundtruth_dp_num_points': [batch_size, max_number_of_boxes] int32 - tensor. (Optional) - 'groundtruth_dp_part_ids': [batch_size, max_number_of_boxes, - max_sampled_points] int32 tensor. (Optional) - 'groundtruth_dp_surface_coords_list': [batch_size, max_number_of_boxes, - max_sampled_points, 4] float32 tensor. (Optional) - class_agnostic: Boolean indicating whether the detections are class-agnostic - (i.e. binary). Default False. - scale_to_absolute: Boolean indicating whether boxes and keypoints should be - scaled to absolute coordinates. Note that for IoU based evaluations, it - does not matter whether boxes are expressed in absolute or relative - coordinates. Default False. - original_image_spatial_shapes: A 2D int32 tensor of shape [batch_size, 2] - used to resize the image. When set to None, the image size is retained. - true_image_shapes: A 2D int32 tensor of shape [batch_size, 3] - containing the size of the unpadded original_image. - max_gt_boxes: [batch_size] tensor representing the maximum number of - groundtruth boxes to pad. - label_id_offset: offset for class ids. - - Returns: - A dictionary with: - 'original_image': A [batch_size, H, W, C] uint8 image tensor. - 'original_image_spatial_shape': A [batch_size, 2] tensor containing the - original image sizes. - 'true_image_shape': A [batch_size, 3] tensor containing the size of - the unpadded original_image. - 'key': A [batch_size] string tensor with image identifier. - 'detection_boxes': [batch_size, max_detections, 4] float32 tensor of boxes, - in normalized or absolute coordinates, depending on the value of - `scale_to_absolute`. - 'detection_scores': [batch_size, max_detections] float32 tensor of scores. - 'detection_classes': [batch_size, max_detections] int64 tensor of 1-indexed - classes. - 'detection_masks': [batch_size, max_detections, H, W] uint8 tensor of - instance masks, reframed to full image masks. Note that these may be - binarized (e.g. {0, 1}), or may contain 1-indexed part labels. (Optional) - 'detection_keypoints': [batch_size, max_detections, num_keypoints, 2] - float32 tensor containing keypoint coordinates. (Optional) - 'detection_keypoint_scores': [batch_size, max_detections, num_keypoints] - float32 tensor containing keypoint scores. (Optional) - 'detection_surface_coords': [batch_size, max_detection, H, W, 2] float32 - tensor with normalized surface coordinates (e.g. DensePose UV - coordinates). (Optional) - 'num_detections': [batch_size] int64 tensor containing number of valid - detections. - 'groundtruth_boxes': [batch_size, num_boxes, 4] float32 tensor of boxes, in - normalized or absolute coordinates, depending on the value of - `scale_to_absolute`. (Optional) - 'groundtruth_classes': [batch_size, num_boxes] int64 tensor of 1-indexed - classes. (Optional) - 'groundtruth_area': [batch_size, num_boxes] float32 tensor of bbox - area. (Optional) - 'groundtruth_is_crowd': [batch_size, num_boxes] int64 tensor. (Optional) - 'groundtruth_difficult': [batch_size, num_boxes] int64 tensor. (Optional) - 'groundtruth_group_of': [batch_size, num_boxes] int64 tensor. (Optional) - 'groundtruth_instance_masks': 4D int64 tensor of instance masks - (Optional). - 'groundtruth_keypoints': [batch_size, num_boxes, num_keypoints, 2] float32 - tensor with keypoints (Optional). - 'groundtruth_keypoint_visibilities': [batch_size, num_boxes, num_keypoints] - bool tensor with keypoint visibilities (Optional). - 'groundtruth_labeled_classes': [batch_size, num_classes] int64 tensor - of 1-indexed classes. (Optional) - 'num_groundtruth_boxes': [batch_size] tensor containing the maximum number - of groundtruth boxes per image. - - Raises: - ValueError: if original_image_spatial_shape is not 2D int32 tensor of shape - [2]. - ValueError: if true_image_shapes is not 2D int32 tensor of shape - [3]. - """ - input_data_fields = fields.InputDataFields - if original_image_spatial_shapes is None: - original_image_spatial_shapes = tf.tile( - tf.expand_dims(tf.shape(images)[1:3], axis=0), - multiples=[tf.shape(images)[0], 1]) - else: - if (len(original_image_spatial_shapes.shape) != 2 and - original_image_spatial_shapes.shape[1] != 2): - raise ValueError( - '`original_image_spatial_shape` should be a 2D tensor of shape ' - '[batch_size, 2].') - - if true_image_shapes is None: - true_image_shapes = tf.tile( - tf.expand_dims(tf.shape(images)[1:4], axis=0), - multiples=[tf.shape(images)[0], 1]) - else: - if (len(true_image_shapes.shape) != 2 - and true_image_shapes.shape[1] != 3): - raise ValueError('`true_image_shapes` should be a 2D tensor of ' - 'shape [batch_size, 3].') - - output_dict = { - input_data_fields.original_image: - images, - input_data_fields.key: - keys, - input_data_fields.original_image_spatial_shape: ( - original_image_spatial_shapes), - input_data_fields.true_image_shape: - true_image_shapes - } - - detection_fields = fields.DetectionResultFields - detection_boxes = detections[detection_fields.detection_boxes] - detection_scores = detections[detection_fields.detection_scores] - num_detections = tf.cast(detections[detection_fields.num_detections], - dtype=tf.int32) - - if class_agnostic: - detection_classes = tf.ones_like(detection_scores, dtype=tf.int64) - else: - detection_classes = ( - tf.to_int64(detections[detection_fields.detection_classes]) + - label_id_offset) - - if scale_to_absolute: - output_dict[detection_fields.detection_boxes] = ( - shape_utils.static_or_dynamic_map_fn( - _scale_box_to_absolute, - elems=[detection_boxes, original_image_spatial_shapes], - dtype=tf.float32)) - else: - output_dict[detection_fields.detection_boxes] = detection_boxes - output_dict[detection_fields.detection_classes] = detection_classes - output_dict[detection_fields.detection_scores] = detection_scores - output_dict[detection_fields.num_detections] = num_detections - - if detection_fields.detection_masks in detections: - detection_masks = detections[detection_fields.detection_masks] - output_dict[detection_fields.detection_masks] = resize_detection_masks( - detection_boxes, detection_masks, original_image_spatial_shapes) - - if detection_fields.detection_surface_coords in detections: - detection_surface_coords = detections[ - detection_fields.detection_surface_coords] - output_dict[detection_fields.detection_surface_coords] = ( - shape_utils.static_or_dynamic_map_fn( - _resize_surface_coordinate_masks, - elems=[detection_boxes, detection_surface_coords, - original_image_spatial_shapes], - dtype=tf.float32)) - - if detection_fields.detection_keypoints in detections: - detection_keypoints = detections[detection_fields.detection_keypoints] - output_dict[detection_fields.detection_keypoints] = detection_keypoints - if scale_to_absolute: - output_dict[detection_fields.detection_keypoints] = ( - shape_utils.static_or_dynamic_map_fn( - _scale_keypoint_to_absolute, - elems=[detection_keypoints, original_image_spatial_shapes], - dtype=tf.float32)) - if detection_fields.detection_keypoint_scores in detections: - output_dict[detection_fields.detection_keypoint_scores] = detections[ - detection_fields.detection_keypoint_scores] - else: - output_dict[detection_fields.detection_keypoint_scores] = tf.ones_like( - detections[detection_fields.detection_keypoints][:, :, :, 0]) - - if groundtruth: - if max_gt_boxes is None: - if input_data_fields.num_groundtruth_boxes in groundtruth: - max_gt_boxes = groundtruth[input_data_fields.num_groundtruth_boxes] - else: - raise ValueError( - 'max_gt_boxes must be provided when processing batched examples.') - - if input_data_fields.groundtruth_instance_masks in groundtruth: - masks = groundtruth[input_data_fields.groundtruth_instance_masks] - max_spatial_shape = tf.reduce_max( - original_image_spatial_shapes, axis=0, keep_dims=True) - tiled_max_spatial_shape = tf.tile( - max_spatial_shape, - multiples=[tf.shape(original_image_spatial_shapes)[0], 1]) - groundtruth[input_data_fields.groundtruth_instance_masks] = ( - shape_utils.static_or_dynamic_map_fn( - _resize_groundtruth_masks, - elems=[masks, true_image_shapes, - original_image_spatial_shapes, - tiled_max_spatial_shape], - dtype=tf.uint8)) - - output_dict.update(groundtruth) - - image_shape = tf.cast(tf.shape(images), tf.float32) - image_height, image_width = image_shape[1], image_shape[2] - - def _scale_box_to_normalized_true_image(args): - """Scale the box coordinates to be relative to the true image shape.""" - boxes, true_image_shape = args - true_image_shape = tf.cast(true_image_shape, tf.float32) - true_height, true_width = true_image_shape[0], true_image_shape[1] - normalized_window = tf.stack([0.0, 0.0, true_height / image_height, - true_width / image_width]) - return box_list_ops.change_coordinate_frame( - box_list.BoxList(boxes), normalized_window).get() - - groundtruth_boxes = groundtruth[input_data_fields.groundtruth_boxes] - groundtruth_boxes = shape_utils.static_or_dynamic_map_fn( - _scale_box_to_normalized_true_image, - elems=[groundtruth_boxes, true_image_shapes], dtype=tf.float32) - output_dict[input_data_fields.groundtruth_boxes] = groundtruth_boxes - - if input_data_fields.groundtruth_keypoints in groundtruth: - # If groundtruth_keypoints is in the groundtruth dictionary. Update the - # coordinates to conform with the true image shape. - def _scale_keypoints_to_normalized_true_image(args): - """Scale the box coordinates to be relative to the true image shape.""" - keypoints, true_image_shape = args - true_image_shape = tf.cast(true_image_shape, tf.float32) - true_height, true_width = true_image_shape[0], true_image_shape[1] - normalized_window = tf.stack( - [0.0, 0.0, true_height / image_height, true_width / image_width]) - return keypoint_ops.change_coordinate_frame(keypoints, - normalized_window) - - groundtruth_keypoints = groundtruth[ - input_data_fields.groundtruth_keypoints] - groundtruth_keypoints = shape_utils.static_or_dynamic_map_fn( - _scale_keypoints_to_normalized_true_image, - elems=[groundtruth_keypoints, true_image_shapes], - dtype=tf.float32) - output_dict[ - input_data_fields.groundtruth_keypoints] = groundtruth_keypoints - - if scale_to_absolute: - groundtruth_boxes = output_dict[input_data_fields.groundtruth_boxes] - output_dict[input_data_fields.groundtruth_boxes] = ( - shape_utils.static_or_dynamic_map_fn( - _scale_box_to_absolute, - elems=[groundtruth_boxes, original_image_spatial_shapes], - dtype=tf.float32)) - if input_data_fields.groundtruth_keypoints in groundtruth: - groundtruth_keypoints = output_dict[ - input_data_fields.groundtruth_keypoints] - output_dict[input_data_fields.groundtruth_keypoints] = ( - shape_utils.static_or_dynamic_map_fn( - _scale_keypoint_to_absolute, - elems=[groundtruth_keypoints, original_image_spatial_shapes], - dtype=tf.float32)) - - # For class-agnostic models, groundtruth classes all become 1. - if class_agnostic: - groundtruth_classes = groundtruth[input_data_fields.groundtruth_classes] - groundtruth_classes = tf.ones_like(groundtruth_classes, dtype=tf.int64) - output_dict[input_data_fields.groundtruth_classes] = groundtruth_classes - - output_dict[input_data_fields.num_groundtruth_boxes] = max_gt_boxes - - return output_dict - - -def get_evaluators(eval_config, categories, evaluator_options=None): - """Returns the evaluator class according to eval_config, valid for categories. - - Args: - eval_config: An `eval_pb2.EvalConfig`. - categories: A list of dicts, each of which has the following keys - - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name e.g., 'cat', 'dog'. - 'keypoints': (optional) dict mapping this category's keypoints to unique - ids. - evaluator_options: A dictionary of metric names (see - EVAL_METRICS_CLASS_DICT) to `DetectionEvaluator` initialization - keyword arguments. For example: - evalator_options = { - 'coco_detection_metrics': {'include_metrics_per_category': True} - } - - Returns: - An list of instances of DetectionEvaluator. - - Raises: - ValueError: if metric is not in the metric class dictionary. - """ - evaluator_options = evaluator_options or {} - eval_metric_fn_keys = eval_config.metrics_set - if not eval_metric_fn_keys: - eval_metric_fn_keys = [EVAL_DEFAULT_METRIC] - evaluators_list = [] - for eval_metric_fn_key in eval_metric_fn_keys: - if eval_metric_fn_key not in EVAL_METRICS_CLASS_DICT: - raise ValueError('Metric not found: {}'.format(eval_metric_fn_key)) - kwargs_dict = (evaluator_options[eval_metric_fn_key] if eval_metric_fn_key - in evaluator_options else {}) - evaluators_list.append(EVAL_METRICS_CLASS_DICT[eval_metric_fn_key]( - categories, - **kwargs_dict)) - - if isinstance(eval_config, eval_pb2.EvalConfig): - parameterized_metrics = eval_config.parameterized_metric - for parameterized_metric in parameterized_metrics: - assert parameterized_metric.HasField('parameterized_metric') - if parameterized_metric.WhichOneof( - 'parameterized_metric') == EVAL_KEYPOINT_METRIC: - keypoint_metrics = parameterized_metric.coco_keypoint_metrics - # Create category to keypoints mapping dict. - category_keypoints = {} - class_label = keypoint_metrics.class_label - category = None - for cat in categories: - if cat['name'] == class_label: - category = cat - break - if not category: - continue - keypoints_for_this_class = category['keypoints'] - category_keypoints = [{ - 'id': keypoints_for_this_class[kp_name], 'name': kp_name - } for kp_name in keypoints_for_this_class] - # Create keypoint evaluator for this category. - evaluators_list.append(EVAL_METRICS_CLASS_DICT[EVAL_KEYPOINT_METRIC]( - category['id'], category_keypoints, class_label, - keypoint_metrics.keypoint_label_to_sigmas)) - return evaluators_list - - -def get_eval_metric_ops_for_evaluators(eval_config, - categories, - eval_dict): - """Returns eval metrics ops to use with `tf.estimator.EstimatorSpec`. - - Args: - eval_config: An `eval_pb2.EvalConfig`. - categories: A list of dicts, each of which has the following keys - - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name e.g., 'cat', 'dog'. - eval_dict: An evaluation dictionary, returned from - result_dict_for_single_example(). - - Returns: - A dictionary of metric names to tuple of value_op and update_op that can be - used as eval metric ops in tf.EstimatorSpec. - """ - eval_metric_ops = {} - evaluator_options = evaluator_options_from_eval_config(eval_config) - evaluators_list = get_evaluators(eval_config, categories, evaluator_options) - for evaluator in evaluators_list: - eval_metric_ops.update(evaluator.get_estimator_eval_metric_ops( - eval_dict)) - return eval_metric_ops - - -def evaluator_options_from_eval_config(eval_config): - """Produces a dictionary of evaluation options for each eval metric. - - Args: - eval_config: An `eval_pb2.EvalConfig`. - - Returns: - evaluator_options: A dictionary of metric names (see - EVAL_METRICS_CLASS_DICT) to `DetectionEvaluator` initialization - keyword arguments. For example: - evalator_options = { - 'coco_detection_metrics': {'include_metrics_per_category': True} - } - """ - eval_metric_fn_keys = eval_config.metrics_set - evaluator_options = {} - for eval_metric_fn_key in eval_metric_fn_keys: - if eval_metric_fn_key in ( - 'coco_detection_metrics', 'coco_mask_metrics', 'lvis_mask_metrics'): - evaluator_options[eval_metric_fn_key] = { - 'include_metrics_per_category': ( - eval_config.include_metrics_per_category) - } - - if (hasattr(eval_config, 'all_metrics_per_category') and - eval_config.all_metrics_per_category): - evaluator_options[eval_metric_fn_key].update({ - 'all_metrics_per_category': eval_config.all_metrics_per_category - }) - # For coco detection eval, if the eval_config proto contains the - # "skip_predictions_for_unlabeled_class" field, include this field in - # evaluator_options. - if eval_metric_fn_key == 'coco_detection_metrics' and hasattr( - eval_config, 'skip_predictions_for_unlabeled_class'): - evaluator_options[eval_metric_fn_key].update({ - 'skip_predictions_for_unlabeled_class': - (eval_config.skip_predictions_for_unlabeled_class) - }) - for super_category in eval_config.super_categories: - if 'super_categories' not in evaluator_options[eval_metric_fn_key]: - evaluator_options[eval_metric_fn_key]['super_categories'] = {} - key = super_category - value = eval_config.super_categories[key].split(',') - evaluator_options[eval_metric_fn_key]['super_categories'][key] = value - if eval_metric_fn_key == 'lvis_mask_metrics' and hasattr( - eval_config, 'export_path'): - evaluator_options[eval_metric_fn_key].update({ - 'export_path': eval_config.export_path - }) - - elif eval_metric_fn_key == 'precision_at_recall_detection_metrics': - evaluator_options[eval_metric_fn_key] = { - 'recall_lower_bound': (eval_config.recall_lower_bound), - 'recall_upper_bound': (eval_config.recall_upper_bound), - 'skip_predictions_for_unlabeled_class': - eval_config.skip_predictions_for_unlabeled_class, - } - return evaluator_options - - -def has_densepose(eval_dict): - return (fields.DetectionResultFields.detection_masks in eval_dict and - fields.DetectionResultFields.detection_surface_coords in eval_dict) diff --git a/research/object_detection/eval_util_test.py b/research/object_detection/eval_util_test.py deleted file mode 100644 index 77de679491f..00000000000 --- a/research/object_detection/eval_util_test.py +++ /dev/null @@ -1,464 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for eval_util.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -from absl.testing import parameterized - -import numpy as np -import six -from six.moves import range -import tensorflow.compat.v1 as tf -from google.protobuf import text_format - -from object_detection import eval_util -from object_detection.core import standard_fields as fields -from object_detection.metrics import coco_evaluation -from object_detection.protos import eval_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -class EvalUtilTest(test_case.TestCase, parameterized.TestCase): - - def _get_categories_list(self): - return [{'id': 1, 'name': 'person'}, - {'id': 2, 'name': 'dog'}, - {'id': 3, 'name': 'cat'}] - - def _get_categories_list_with_keypoints(self): - return [{ - 'id': 1, - 'name': 'person', - 'keypoints': { - 'left_eye': 0, - 'right_eye': 3 - } - }, { - 'id': 2, - 'name': 'dog', - 'keypoints': { - 'tail_start': 1, - 'mouth': 2 - } - }, { - 'id': 3, - 'name': 'cat' - }] - - def _make_evaluation_dict(self, - resized_groundtruth_masks=False, - batch_size=1, - max_gt_boxes=None, - scale_to_absolute=False): - input_data_fields = fields.InputDataFields - detection_fields = fields.DetectionResultFields - - image = tf.zeros(shape=[batch_size, 20, 20, 3], dtype=tf.uint8) - if batch_size == 1: - key = tf.constant('image1') - else: - key = tf.constant([str(i) for i in range(batch_size)]) - detection_boxes = tf.tile(tf.constant([[[0., 0., 1., 1.]]]), - multiples=[batch_size, 1, 1]) - detection_scores = tf.tile(tf.constant([[0.8]]), multiples=[batch_size, 1]) - detection_classes = tf.tile(tf.constant([[0]]), multiples=[batch_size, 1]) - detection_masks = tf.tile(tf.ones(shape=[1, 1, 20, 20], dtype=tf.float32), - multiples=[batch_size, 1, 1, 1]) - num_detections = tf.ones([batch_size]) - groundtruth_boxes = tf.constant([[0., 0., 1., 1.]]) - groundtruth_classes = tf.constant([1]) - groundtruth_instance_masks = tf.ones(shape=[1, 20, 20], dtype=tf.uint8) - original_image_spatial_shapes = tf.constant([[20, 20]], dtype=tf.int32) - - groundtruth_keypoints = tf.constant([[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]]) - if resized_groundtruth_masks: - groundtruth_instance_masks = tf.ones(shape=[1, 10, 10], dtype=tf.uint8) - - if batch_size > 1: - groundtruth_boxes = tf.tile(tf.expand_dims(groundtruth_boxes, 0), - multiples=[batch_size, 1, 1]) - groundtruth_classes = tf.tile(tf.expand_dims(groundtruth_classes, 0), - multiples=[batch_size, 1]) - groundtruth_instance_masks = tf.tile( - tf.expand_dims(groundtruth_instance_masks, 0), - multiples=[batch_size, 1, 1, 1]) - groundtruth_keypoints = tf.tile( - tf.expand_dims(groundtruth_keypoints, 0), - multiples=[batch_size, 1, 1]) - original_image_spatial_shapes = tf.tile(original_image_spatial_shapes, - multiples=[batch_size, 1]) - - detections = { - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_masks: detection_masks, - detection_fields.num_detections: num_detections - } - groundtruth = { - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_keypoints: groundtruth_keypoints, - input_data_fields.groundtruth_instance_masks: - groundtruth_instance_masks, - input_data_fields.original_image_spatial_shape: - original_image_spatial_shapes - } - if batch_size > 1: - return eval_util.result_dict_for_batched_example( - image, key, detections, groundtruth, - scale_to_absolute=scale_to_absolute, - max_gt_boxes=max_gt_boxes) - else: - return eval_util.result_dict_for_single_example( - image, key, detections, groundtruth, - scale_to_absolute=scale_to_absolute) - - @parameterized.parameters( - {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': True}, - {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': True}, - {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': False}, - {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': False} - ) - @unittest.skipIf(tf_version.is_tf2(), 'Only compatible with TF1.X') - def test_get_eval_metric_ops_for_coco_detections(self, batch_size=1, - max_gt_boxes=None, - scale_to_absolute=False): - eval_config = eval_pb2.EvalConfig() - eval_config.metrics_set.extend(['coco_detection_metrics']) - categories = self._get_categories_list() - eval_dict = self._make_evaluation_dict(batch_size=batch_size, - max_gt_boxes=max_gt_boxes, - scale_to_absolute=scale_to_absolute) - metric_ops = eval_util.get_eval_metric_ops_for_evaluators( - eval_config, categories, eval_dict) - _, update_op = metric_ops['DetectionBoxes_Precision/mAP'] - - with self.test_session() as sess: - metrics = {} - for key, (value_op, _) in six.iteritems(metric_ops): - metrics[key] = value_op - sess.run(update_op) - metrics = sess.run(metrics) - self.assertAlmostEqual(1.0, metrics['DetectionBoxes_Precision/mAP']) - self.assertNotIn('DetectionMasks_Precision/mAP', metrics) - - @parameterized.parameters( - {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': True}, - {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': True}, - {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': False}, - {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': False} - ) - @unittest.skipIf(tf_version.is_tf2(), 'Only compatible with TF1.X') - def test_get_eval_metric_ops_for_coco_detections_and_masks( - self, batch_size=1, max_gt_boxes=None, scale_to_absolute=False): - eval_config = eval_pb2.EvalConfig() - eval_config.metrics_set.extend( - ['coco_detection_metrics', 'coco_mask_metrics']) - categories = self._get_categories_list() - eval_dict = self._make_evaluation_dict(batch_size=batch_size, - max_gt_boxes=max_gt_boxes, - scale_to_absolute=scale_to_absolute) - metric_ops = eval_util.get_eval_metric_ops_for_evaluators( - eval_config, categories, eval_dict) - _, update_op_boxes = metric_ops['DetectionBoxes_Precision/mAP'] - _, update_op_masks = metric_ops['DetectionMasks_Precision/mAP'] - - with self.test_session() as sess: - metrics = {} - for key, (value_op, _) in six.iteritems(metric_ops): - metrics[key] = value_op - sess.run(update_op_boxes) - sess.run(update_op_masks) - metrics = sess.run(metrics) - self.assertAlmostEqual(1.0, metrics['DetectionBoxes_Precision/mAP']) - self.assertAlmostEqual(1.0, metrics['DetectionMasks_Precision/mAP']) - - @parameterized.parameters( - {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': True}, - {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': True}, - {'batch_size': 1, 'max_gt_boxes': None, 'scale_to_absolute': False}, - {'batch_size': 8, 'max_gt_boxes': [1], 'scale_to_absolute': False} - ) - @unittest.skipIf(tf_version.is_tf2(), 'Only compatible with TF1.X') - def test_get_eval_metric_ops_for_coco_detections_and_resized_masks( - self, batch_size=1, max_gt_boxes=None, scale_to_absolute=False): - eval_config = eval_pb2.EvalConfig() - eval_config.metrics_set.extend( - ['coco_detection_metrics', 'coco_mask_metrics']) - categories = self._get_categories_list() - eval_dict = self._make_evaluation_dict(batch_size=batch_size, - max_gt_boxes=max_gt_boxes, - scale_to_absolute=scale_to_absolute, - resized_groundtruth_masks=True) - metric_ops = eval_util.get_eval_metric_ops_for_evaluators( - eval_config, categories, eval_dict) - _, update_op_boxes = metric_ops['DetectionBoxes_Precision/mAP'] - _, update_op_masks = metric_ops['DetectionMasks_Precision/mAP'] - - with self.test_session() as sess: - metrics = {} - for key, (value_op, _) in six.iteritems(metric_ops): - metrics[key] = value_op - sess.run(update_op_boxes) - sess.run(update_op_masks) - metrics = sess.run(metrics) - self.assertAlmostEqual(1.0, metrics['DetectionBoxes_Precision/mAP']) - self.assertAlmostEqual(1.0, metrics['DetectionMasks_Precision/mAP']) - - @unittest.skipIf(tf_version.is_tf2(), 'Only compatible with TF1.X') - def test_get_eval_metric_ops_raises_error_with_unsupported_metric(self): - eval_config = eval_pb2.EvalConfig() - eval_config.metrics_set.extend(['unsupported_metric']) - categories = self._get_categories_list() - eval_dict = self._make_evaluation_dict() - with self.assertRaises(ValueError): - eval_util.get_eval_metric_ops_for_evaluators( - eval_config, categories, eval_dict) - - def test_get_eval_metric_ops_for_evaluators(self): - eval_config = eval_pb2.EvalConfig() - eval_config.metrics_set.extend([ - 'coco_detection_metrics', 'coco_mask_metrics', - 'precision_at_recall_detection_metrics' - ]) - eval_config.include_metrics_per_category = True - eval_config.recall_lower_bound = 0.2 - eval_config.recall_upper_bound = 0.6 - - evaluator_options = eval_util.evaluator_options_from_eval_config( - eval_config) - self.assertTrue(evaluator_options['coco_detection_metrics'] - ['include_metrics_per_category']) - self.assertFalse(evaluator_options['coco_detection_metrics'] - ['skip_predictions_for_unlabeled_class']) - self.assertTrue( - evaluator_options['coco_mask_metrics']['include_metrics_per_category']) - self.assertAlmostEqual( - evaluator_options['precision_at_recall_detection_metrics'] - ['recall_lower_bound'], eval_config.recall_lower_bound) - self.assertAlmostEqual( - evaluator_options['precision_at_recall_detection_metrics'] - ['recall_upper_bound'], eval_config.recall_upper_bound) - self.assertFalse(evaluator_options['precision_at_recall_detection_metrics'] - ['skip_predictions_for_unlabeled_class']) - - def test_get_evaluator_with_evaluator_options(self): - eval_config = eval_pb2.EvalConfig() - eval_config.metrics_set.extend( - ['coco_detection_metrics', 'precision_at_recall_detection_metrics']) - eval_config.include_metrics_per_category = True - eval_config.skip_predictions_for_unlabeled_class = True - eval_config.recall_lower_bound = 0.2 - eval_config.recall_upper_bound = 0.6 - categories = self._get_categories_list() - - evaluator_options = eval_util.evaluator_options_from_eval_config( - eval_config) - evaluator = eval_util.get_evaluators(eval_config, categories, - evaluator_options) - - self.assertTrue(evaluator[0]._include_metrics_per_category) - self.assertTrue(evaluator[0]._skip_predictions_for_unlabeled_class) - self.assertTrue(evaluator[1]._skip_predictions_for_unlabeled_class) - self.assertAlmostEqual(evaluator[1]._recall_lower_bound, - eval_config.recall_lower_bound) - self.assertAlmostEqual(evaluator[1]._recall_upper_bound, - eval_config.recall_upper_bound) - - def test_get_evaluator_with_no_evaluator_options(self): - eval_config = eval_pb2.EvalConfig() - eval_config.metrics_set.extend( - ['coco_detection_metrics', 'precision_at_recall_detection_metrics']) - eval_config.include_metrics_per_category = True - eval_config.recall_lower_bound = 0.2 - eval_config.recall_upper_bound = 0.6 - categories = self._get_categories_list() - - evaluator = eval_util.get_evaluators( - eval_config, categories, evaluator_options=None) - - # Even though we are setting eval_config.include_metrics_per_category = True - # and bounds on recall, these options are never passed into the - # DetectionEvaluator constructor (via `evaluator_options`). - self.assertFalse(evaluator[0]._include_metrics_per_category) - self.assertAlmostEqual(evaluator[1]._recall_lower_bound, 0.0) - self.assertAlmostEqual(evaluator[1]._recall_upper_bound, 1.0) - - def test_get_evaluator_with_keypoint_metrics(self): - eval_config = eval_pb2.EvalConfig() - person_keypoints_metric = eval_config.parameterized_metric.add() - person_keypoints_metric.coco_keypoint_metrics.class_label = 'person' - person_keypoints_metric.coco_keypoint_metrics.keypoint_label_to_sigmas[ - 'left_eye'] = 0.1 - person_keypoints_metric.coco_keypoint_metrics.keypoint_label_to_sigmas[ - 'right_eye'] = 0.2 - dog_keypoints_metric = eval_config.parameterized_metric.add() - dog_keypoints_metric.coco_keypoint_metrics.class_label = 'dog' - dog_keypoints_metric.coco_keypoint_metrics.keypoint_label_to_sigmas[ - 'tail_start'] = 0.3 - dog_keypoints_metric.coco_keypoint_metrics.keypoint_label_to_sigmas[ - 'mouth'] = 0.4 - categories = self._get_categories_list_with_keypoints() - - evaluator = eval_util.get_evaluators( - eval_config, categories, evaluator_options=None) - - # Verify keypoint evaluator class variables. - self.assertLen(evaluator, 3) - self.assertFalse(evaluator[0]._include_metrics_per_category) - self.assertEqual(evaluator[1]._category_name, 'person') - self.assertEqual(evaluator[2]._category_name, 'dog') - self.assertAllEqual(evaluator[1]._keypoint_ids, [0, 3]) - self.assertAllEqual(evaluator[2]._keypoint_ids, [1, 2]) - self.assertAllClose([0.1, 0.2], evaluator[1]._oks_sigmas) - self.assertAllClose([0.3, 0.4], evaluator[2]._oks_sigmas) - - def test_get_evaluator_with_unmatched_label(self): - eval_config = eval_pb2.EvalConfig() - person_keypoints_metric = eval_config.parameterized_metric.add() - person_keypoints_metric.coco_keypoint_metrics.class_label = 'unmatched' - person_keypoints_metric.coco_keypoint_metrics.keypoint_label_to_sigmas[ - 'kpt'] = 0.1 - categories = self._get_categories_list_with_keypoints() - - evaluator = eval_util.get_evaluators( - eval_config, categories, evaluator_options=None) - self.assertLen(evaluator, 1) - self.assertNotIsInstance( - evaluator[0], coco_evaluation.CocoKeypointEvaluator) - - def test_padded_image_result_dict(self): - - input_data_fields = fields.InputDataFields - detection_fields = fields.DetectionResultFields - key = tf.constant([str(i) for i in range(2)]) - - detection_boxes = np.array([[[0., 0., 1., 1.]], [[0.0, 0.0, 0.5, 0.5]]], - dtype=np.float32) - detection_keypoints = np.array([[0.0, 0.0], [0.5, 0.5], [1.0, 1.0]], - dtype=np.float32) - def graph_fn(): - detections = { - detection_fields.detection_boxes: - tf.constant(detection_boxes), - detection_fields.detection_scores: - tf.constant([[1.], [1.]]), - detection_fields.detection_classes: - tf.constant([[1], [2]]), - detection_fields.num_detections: - tf.constant([1, 1]), - detection_fields.detection_keypoints: - tf.tile( - tf.reshape( - tf.constant(detection_keypoints), shape=[1, 1, 3, 2]), - multiples=[2, 1, 1, 1]) - } - - gt_boxes = detection_boxes - groundtruth = { - input_data_fields.groundtruth_boxes: - tf.constant(gt_boxes), - input_data_fields.groundtruth_classes: - tf.constant([[1.], [1.]]), - input_data_fields.groundtruth_keypoints: - tf.tile( - tf.reshape( - tf.constant(detection_keypoints), shape=[1, 1, 3, 2]), - multiples=[2, 1, 1, 1]) - } - - image = tf.zeros((2, 100, 100, 3), dtype=tf.float32) - - true_image_shapes = tf.constant([[100, 100, 3], [50, 100, 3]]) - original_image_spatial_shapes = tf.constant([[200, 200], [150, 300]]) - - result = eval_util.result_dict_for_batched_example( - image, key, detections, groundtruth, - scale_to_absolute=True, - true_image_shapes=true_image_shapes, - original_image_spatial_shapes=original_image_spatial_shapes, - max_gt_boxes=tf.constant(1)) - return (result[input_data_fields.groundtruth_boxes], - result[input_data_fields.groundtruth_keypoints], - result[detection_fields.detection_boxes], - result[detection_fields.detection_keypoints]) - (gt_boxes, gt_keypoints, detection_boxes, - detection_keypoints) = self.execute_cpu(graph_fn, []) - self.assertAllEqual( - [[[0., 0., 200., 200.]], [[0.0, 0.0, 150., 150.]]], - gt_boxes) - self.assertAllClose([[[[0., 0.], [100., 100.], [200., 200.]]], - [[[0., 0.], [150., 150.], [300., 300.]]]], - gt_keypoints) - - # Predictions from the model are not scaled. - self.assertAllEqual( - [[[0., 0., 200., 200.]], [[0.0, 0.0, 75., 150.]]], - detection_boxes) - self.assertAllClose([[[[0., 0.], [100., 100.], [200., 200.]]], - [[[0., 0.], [75., 150.], [150., 300.]]]], - detection_keypoints) - - def test_evaluator_options_from_eval_config_no_super_categories(self): - eval_config_text_proto = """ - metrics_set: "coco_detection_metrics" - metrics_set: "coco_mask_metrics" - include_metrics_per_category: true - use_moving_averages: false - batch_size: 1; - """ - eval_config = eval_pb2.EvalConfig() - text_format.Merge(eval_config_text_proto, eval_config) - evaluator_options = eval_util.evaluator_options_from_eval_config( - eval_config) - self.assertNotIn('super_categories', evaluator_options['coco_mask_metrics']) - - def test_evaluator_options_from_eval_config_with_super_categories(self): - eval_config_text_proto = """ - metrics_set: "coco_detection_metrics" - metrics_set: "coco_mask_metrics" - include_metrics_per_category: true - use_moving_averages: false - batch_size: 1; - super_categories { - key: "supercat1" - value: "a,b,c" - } - super_categories { - key: "supercat2" - value: "d,e,f" - } - """ - eval_config = eval_pb2.EvalConfig() - text_format.Merge(eval_config_text_proto, eval_config) - evaluator_options = eval_util.evaluator_options_from_eval_config( - eval_config) - self.assertIn('super_categories', evaluator_options['coco_mask_metrics']) - super_categories = evaluator_options[ - 'coco_mask_metrics']['super_categories'] - self.assertIn('supercat1', super_categories) - self.assertIn('supercat2', super_categories) - self.assertAllEqual(super_categories['supercat1'], ['a', 'b', 'c']) - self.assertAllEqual(super_categories['supercat2'], ['d', 'e', 'f']) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/export_inference_graph.py b/research/object_detection/export_inference_graph.py deleted file mode 100644 index bc4bca1d062..00000000000 --- a/research/object_detection/export_inference_graph.py +++ /dev/null @@ -1,205 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Tool to export an object detection model for inference. - -Prepares an object detection tensorflow graph for inference using model -configuration and a trained checkpoint. Outputs inference -graph, associated checkpoint files, a frozen inference graph and a -SavedModel (https://tensorflow.github.io/serving/serving_basic.html). - -The inference graph contains one of three input nodes depending on the user -specified option. - * `image_tensor`: Accepts a uint8 4-D tensor of shape [None, None, None, 3] - * `encoded_image_string_tensor`: Accepts a 1-D string tensor of shape [None] - containing encoded PNG or JPEG images. Image resolutions are expected to be - the same if more than 1 image is provided. - * `tf_example`: Accepts a 1-D string tensor of shape [None] containing - serialized TFExample protos. Image resolutions are expected to be the same - if more than 1 image is provided. - -and the following output nodes returned by the model.postprocess(..): - * `num_detections`: Outputs float32 tensors of the form [batch] - that specifies the number of valid boxes per image in the batch. - * `detection_boxes`: Outputs float32 tensors of the form - [batch, num_boxes, 4] containing detected boxes. - * `detection_scores`: Outputs float32 tensors of the form - [batch, num_boxes] containing class scores for the detections. - * `detection_classes`: Outputs float32 tensors of the form - [batch, num_boxes] containing classes for the detections. - * `raw_detection_boxes`: Outputs float32 tensors of the form - [batch, raw_num_boxes, 4] containing detection boxes without - post-processing. - * `raw_detection_scores`: Outputs float32 tensors of the form - [batch, raw_num_boxes, num_classes_with_background] containing class score - logits for raw detection boxes. - * `detection_masks`: (Optional) Outputs float32 tensors of the form - [batch, num_boxes, mask_height, mask_width] containing predicted instance - masks for each box if its present in the dictionary of postprocessed - tensors returned by the model. - * detection_multiclass_scores: (Optional) Outputs float32 tensor of shape - [batch, num_boxes, num_classes_with_background] for containing class - score distribution for detected boxes including background if any. - * detection_features: (Optional) float32 tensor of shape - [batch, num_boxes, roi_height, roi_width, depth] - containing classifier features - -Notes: - * This tool uses `use_moving_averages` from eval_config to decide which - weights to freeze. - -Example Usage: --------------- -python export_inference_graph.py \ - --input_type image_tensor \ - --pipeline_config_path path/to/ssd_inception_v2.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory - -The expected output would be in the directory -path/to/exported_model_directory (which is created if it does not exist) -with contents: - - inference_graph.pbtxt - - model.ckpt.data-00000-of-00001 - - model.ckpt.info - - model.ckpt.meta - - frozen_inference_graph.pb - + saved_model (a directory) - -Config overrides (see the `config_override` flag) are text protobufs -(also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override -certain fields in the provided pipeline_config_path. These are useful for -making small changes to the inference graph that differ from the training or -eval config. - -Example Usage (in which we change the second stage post-processing score -threshold to be 0.5): - -python export_inference_graph.py \ - --input_type image_tensor \ - --pipeline_config_path path/to/ssd_inception_v2.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory \ - --config_override " \ - model{ \ - faster_rcnn { \ - second_stage_post_processing { \ - batch_non_max_suppression { \ - score_threshold: 0.5 \ - } \ - } \ - } \ - }" -""" -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from object_detection import exporter -from object_detection.protos import pipeline_pb2 - -flags = tf.app.flags - -flags.DEFINE_string('input_type', 'image_tensor', 'Type of input node. Can be ' - 'one of [`image_tensor`, `encoded_image_string_tensor`, ' - '`tf_example`]') -flags.DEFINE_string('input_shape', None, - 'If input_type is `image_tensor`, this can explicitly set ' - 'the shape of this input tensor to a fixed size. The ' - 'dimensions are to be provided as a comma-separated list ' - 'of integers. A value of -1 can be used for unknown ' - 'dimensions. If not specified, for an `image_tensor, the ' - 'default shape will be partially specified as ' - '`[None, None, None, 3]`.') -flags.DEFINE_string('pipeline_config_path', None, - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file.') -flags.DEFINE_string('trained_checkpoint_prefix', None, - 'Path to trained checkpoint, typically of the form ' - 'path/to/model.ckpt') -flags.DEFINE_string('output_directory', None, 'Path to write outputs.') -flags.DEFINE_string('config_override', '', - 'pipeline_pb2.TrainEvalPipelineConfig ' - 'text proto to override pipeline_config_path.') -flags.DEFINE_boolean('write_inference_graph', False, - 'If true, writes inference graph to disk.') -flags.DEFINE_string('additional_output_tensor_names', None, - 'Additional Tensors to output, to be specified as a comma ' - 'separated list of tensor names.') -flags.DEFINE_boolean('use_side_inputs', False, - 'If True, uses side inputs as well as image inputs.') -flags.DEFINE_string('side_input_shapes', None, - 'If use_side_inputs is True, this explicitly sets ' - 'the shape of the side input tensors to a fixed size. The ' - 'dimensions are to be provided as a comma-separated list ' - 'of integers. A value of -1 can be used for unknown ' - 'dimensions. A `/` denotes a break, starting the shape of ' - 'the next side input tensor. This flag is required if ' - 'using side inputs.') -flags.DEFINE_string('side_input_types', None, - 'If use_side_inputs is True, this explicitly sets ' - 'the type of the side input tensors. The ' - 'dimensions are to be provided as a comma-separated list ' - 'of types, each of `string`, `integer`, or `float`. ' - 'This flag is required if using side inputs.') -flags.DEFINE_string('side_input_names', None, - 'If use_side_inputs is True, this explicitly sets ' - 'the names of the side input tensors required by the model ' - 'assuming the names will be a comma-separated list of ' - 'strings. This flag is required if using side inputs.') -tf.app.flags.mark_flag_as_required('pipeline_config_path') -tf.app.flags.mark_flag_as_required('trained_checkpoint_prefix') -tf.app.flags.mark_flag_as_required('output_directory') -FLAGS = flags.FLAGS - - -def main(_): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - with tf.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f: - text_format.Merge(f.read(), pipeline_config) - text_format.Merge(FLAGS.config_override, pipeline_config) - if FLAGS.input_shape: - input_shape = [ - int(dim) if dim != '-1' else None - for dim in FLAGS.input_shape.split(',') - ] - else: - input_shape = None - if FLAGS.use_side_inputs: - side_input_shapes, side_input_names, side_input_types = ( - exporter.parse_side_inputs( - FLAGS.side_input_shapes, - FLAGS.side_input_names, - FLAGS.side_input_types)) - else: - side_input_shapes = None - side_input_names = None - side_input_types = None - if FLAGS.additional_output_tensor_names: - additional_output_tensor_names = list( - FLAGS.additional_output_tensor_names.split(',')) - else: - additional_output_tensor_names = None - exporter.export_inference_graph( - FLAGS.input_type, pipeline_config, FLAGS.trained_checkpoint_prefix, - FLAGS.output_directory, input_shape=input_shape, - write_inference_graph=FLAGS.write_inference_graph, - additional_output_tensor_names=additional_output_tensor_names, - use_side_inputs=FLAGS.use_side_inputs, - side_input_shapes=side_input_shapes, - side_input_names=side_input_names, - side_input_types=side_input_types) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/export_tflite_graph_lib_tf2.py b/research/object_detection/export_tflite_graph_lib_tf2.py deleted file mode 100644 index 19b3d9801d1..00000000000 --- a/research/object_detection/export_tflite_graph_lib_tf2.py +++ /dev/null @@ -1,374 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Library to export TFLite-compatible SavedModel from TF2 detection models.""" -import os -import numpy as np -import tensorflow.compat.v1 as tf1 -import tensorflow.compat.v2 as tf - -from object_detection.builders import model_builder -from object_detection.builders import post_processing_builder -from object_detection.core import box_list -from object_detection.core import standard_fields as fields - -_DEFAULT_NUM_CHANNELS = 3 -_DEFAULT_NUM_COORD_BOX = 4 -_MAX_CLASSES_PER_DETECTION = 1 -_DETECTION_POSTPROCESS_FUNC = 'TFLite_Detection_PostProcess' - - -def get_const_center_size_encoded_anchors(anchors): - """Exports center-size encoded anchors as a constant tensor. - - Args: - anchors: a float32 tensor of shape [num_anchors, 4] containing the anchor - boxes - - Returns: - encoded_anchors: a float32 constant tensor of shape [num_anchors, 4] - containing the anchor boxes. - """ - anchor_boxlist = box_list.BoxList(anchors) - y, x, h, w = anchor_boxlist.get_center_coordinates_and_sizes() - num_anchors = y.get_shape().as_list() - - with tf1.Session() as sess: - y_out, x_out, h_out, w_out = sess.run([y, x, h, w]) - encoded_anchors = tf1.constant( - np.transpose(np.stack((y_out, x_out, h_out, w_out))), - dtype=tf1.float32, - shape=[num_anchors[0], _DEFAULT_NUM_COORD_BOX], - name='anchors') - return num_anchors[0], encoded_anchors - - -class SSDModule(tf.Module): - """Inference Module for TFLite-friendly SSD models.""" - - def __init__(self, pipeline_config, detection_model, max_detections, - use_regular_nms): - """Initialization. - - Args: - pipeline_config: The original pipeline_pb2.TrainEvalPipelineConfig - detection_model: The detection model to use for inference. - max_detections: Max detections desired from the TFLite model. - use_regular_nms: If True, TFLite model uses the (slower) multi-class NMS. - """ - self._process_config(pipeline_config) - self._pipeline_config = pipeline_config - self._model = detection_model - self._max_detections = max_detections - self._use_regular_nms = use_regular_nms - - def _process_config(self, pipeline_config): - self._num_classes = pipeline_config.model.ssd.num_classes - self._nms_score_threshold = pipeline_config.model.ssd.post_processing.batch_non_max_suppression.score_threshold - self._nms_iou_threshold = pipeline_config.model.ssd.post_processing.batch_non_max_suppression.iou_threshold - self._scale_values = {} - self._scale_values[ - 'y_scale'] = pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale - self._scale_values[ - 'x_scale'] = pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale - self._scale_values[ - 'h_scale'] = pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale - self._scale_values[ - 'w_scale'] = pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale - - image_resizer_config = pipeline_config.model.ssd.image_resizer - image_resizer = image_resizer_config.WhichOneof('image_resizer_oneof') - self._num_channels = _DEFAULT_NUM_CHANNELS - - if image_resizer == 'fixed_shape_resizer': - self._height = image_resizer_config.fixed_shape_resizer.height - self._width = image_resizer_config.fixed_shape_resizer.width - if image_resizer_config.fixed_shape_resizer.convert_to_grayscale: - self._num_channels = 1 - else: - raise ValueError( - 'Only fixed_shape_resizer' - 'is supported with tflite. Found {}'.format( - image_resizer_config.WhichOneof('image_resizer_oneof'))) - - def input_shape(self): - """Returns shape of TFLite model input.""" - return [1, self._height, self._width, self._num_channels] - - def postprocess_implements_signature(self): - """Returns tf.implements signature for MLIR legalization of TFLite NMS.""" - implements_signature = [ - 'name: "%s"' % _DETECTION_POSTPROCESS_FUNC, - 'attr { key: "max_detections" value { i: %d } }' % self._max_detections, - 'attr { key: "max_classes_per_detection" value { i: %d } }' % - _MAX_CLASSES_PER_DETECTION, - 'attr { key: "use_regular_nms" value { b: %s } }' % - str(self._use_regular_nms).lower(), - 'attr { key: "nms_score_threshold" value { f: %f } }' % - self._nms_score_threshold, - 'attr { key: "nms_iou_threshold" value { f: %f } }' % - self._nms_iou_threshold, - 'attr { key: "y_scale" value { f: %f } }' % - self._scale_values['y_scale'], - 'attr { key: "x_scale" value { f: %f } }' % - self._scale_values['x_scale'], - 'attr { key: "h_scale" value { f: %f } }' % - self._scale_values['h_scale'], - 'attr { key: "w_scale" value { f: %f } }' % - self._scale_values['w_scale'], - 'attr { key: "num_classes" value { i: %d } }' % self._num_classes - ] - implements_signature = ' '.join(implements_signature) - return implements_signature - - def _get_postprocess_fn(self, num_anchors, num_classes): - # There is no TF equivalent for TFLite's custom post-processing op. - # So we add an 'empty' composite function here, that is legalized to the - # custom op with MLIR. - @tf.function( - experimental_implements=self.postprocess_implements_signature()) - # pylint: disable=g-unused-argument,unused-argument - def dummy_post_processing(box_encodings, class_predictions, anchors): - boxes = tf.constant(0.0, dtype=tf.float32, name='boxes') - scores = tf.constant(0.0, dtype=tf.float32, name='scores') - classes = tf.constant(0.0, dtype=tf.float32, name='classes') - num_detections = tf.constant(0.0, dtype=tf.float32, name='num_detections') - return boxes, classes, scores, num_detections - - return dummy_post_processing - - @tf.function - def inference_fn(self, image): - """Encapsulates SSD inference for TFLite conversion. - - NOTE: The Args & Returns sections below indicate the TFLite model signature, - and not what the TF graph does (since the latter does not include the custom - NMS op used by TFLite) - - Args: - image: a float32 tensor of shape [num_anchors, 4] containing the anchor - boxes - - Returns: - num_detections: a float32 scalar denoting number of total detections. - classes: a float32 tensor denoting class ID for each detection. - scores: a float32 tensor denoting score for each detection. - boxes: a float32 tensor denoting coordinates of each detected box. - """ - predicted_tensors = self._model.predict(image, true_image_shapes=None) - # The score conversion occurs before the post-processing custom op - _, score_conversion_fn = post_processing_builder.build( - self._pipeline_config.model.ssd.post_processing) - class_predictions = score_conversion_fn( - predicted_tensors['class_predictions_with_background']) - - with tf.name_scope('raw_outputs'): - # 'raw_outputs/box_encodings': a float32 tensor of shape - # [1, num_anchors, 4] containing the encoded box predictions. Note that - # these are raw predictions and no Non-Max suppression is applied on - # them and no decode center size boxes is applied to them. - box_encodings = tf.identity( - predicted_tensors['box_encodings'], name='box_encodings') - # 'raw_outputs/class_predictions': a float32 tensor of shape - # [1, num_anchors, num_classes] containing the class scores for each - # anchor after applying score conversion. - class_predictions = tf.identity( - class_predictions, name='class_predictions') - # 'anchors': a float32 tensor of shape - # [4, num_anchors] containing the anchors as a constant node. - num_anchors, anchors = get_const_center_size_encoded_anchors( - predicted_tensors['anchors']) - anchors = tf.identity(anchors, name='anchors') - - # tf.function@ seems to reverse order of inputs, so reverse them here. - return self._get_postprocess_fn(num_anchors, - self._num_classes)(box_encodings, - class_predictions, - anchors)[::-1] - - -class CenterNetModule(tf.Module): - """Inference Module for TFLite-friendly CenterNet models. - - The exported CenterNet model includes the preprocessing and postprocessing - logics so the caller should pass in the raw image pixel values. It supports - both object detection and keypoint estimation task. - """ - - def __init__(self, pipeline_config, max_detections, include_keypoints, - label_map_path=''): - """Initialization. - - Args: - pipeline_config: The original pipeline_pb2.TrainEvalPipelineConfig - max_detections: Max detections desired from the TFLite model. - include_keypoints: If set true, the output dictionary will include the - keypoint coordinates and keypoint confidence scores. - label_map_path: Path to the label map which is used by CenterNet keypoint - estimation task. If provided, the label_map_path in the configuration - will be replaced by this one. - """ - self._max_detections = max_detections - self._include_keypoints = include_keypoints - self._process_config(pipeline_config) - if include_keypoints and label_map_path: - pipeline_config.model.center_net.keypoint_label_map_path = label_map_path - self._pipeline_config = pipeline_config - self._model = model_builder.build( - self._pipeline_config.model, is_training=False) - - def get_model(self): - return self._model - - def _process_config(self, pipeline_config): - self._num_classes = pipeline_config.model.center_net.num_classes - - center_net_config = pipeline_config.model.center_net - image_resizer_config = center_net_config.image_resizer - image_resizer = image_resizer_config.WhichOneof('image_resizer_oneof') - self._num_channels = _DEFAULT_NUM_CHANNELS - - if image_resizer == 'fixed_shape_resizer': - self._height = image_resizer_config.fixed_shape_resizer.height - self._width = image_resizer_config.fixed_shape_resizer.width - if image_resizer_config.fixed_shape_resizer.convert_to_grayscale: - self._num_channels = 1 - else: - raise ValueError( - 'Only fixed_shape_resizer' - 'is supported with tflite. Found {}'.format(image_resizer)) - - center_net_config.object_center_params.max_box_predictions = ( - self._max_detections) - - if not self._include_keypoints: - del center_net_config.keypoint_estimation_task[:] - - def input_shape(self): - """Returns shape of TFLite model input.""" - return [1, self._height, self._width, self._num_channels] - - @tf.function - def inference_fn(self, image): - """Encapsulates CenterNet inference for TFLite conversion. - - Args: - image: a float32 tensor of shape [1, image_height, image_width, channel] - denoting the image pixel values. - - Returns: - A dictionary of predicted tensors: - classes: a float32 tensor with shape [1, max_detections] denoting class - ID for each detection. - scores: a float32 tensor with shape [1, max_detections] denoting score - for each detection. - boxes: a float32 tensor with shape [1, max_detections, 4] denoting - coordinates of each detected box. - keypoints: a float32 with shape [1, max_detections, num_keypoints, 2] - denoting the predicted keypoint coordinates (normalized in between - 0-1). Note that [:, :, :, 0] represents the y coordinates and - [:, :, :, 1] represents the x coordinates. - keypoint_scores: a float32 with shape [1, max_detections, num_keypoints] - denoting keypoint confidence scores. - """ - image = tf.cast(image, tf.float32) - image, shapes = self._model.preprocess(image) - prediction_dict = self._model.predict(image, None) - detections = self._model.postprocess( - prediction_dict, true_image_shapes=shapes) - - field_names = fields.DetectionResultFields - classes_field = field_names.detection_classes - classes = tf.cast(detections[classes_field], tf.float32) - num_detections = tf.cast(detections[field_names.num_detections], tf.float32) - - if self._include_keypoints: - model_outputs = (detections[field_names.detection_boxes], classes, - detections[field_names.detection_scores], num_detections, - detections[field_names.detection_keypoints], - detections[field_names.detection_keypoint_scores]) - else: - model_outputs = (detections[field_names.detection_boxes], classes, - detections[field_names.detection_scores], num_detections) - - # tf.function@ seems to reverse order of inputs, so reverse them here. - return model_outputs[::-1] - - -def export_tflite_model(pipeline_config, trained_checkpoint_dir, - output_directory, max_detections, use_regular_nms, - include_keypoints=False, label_map_path=''): - """Exports inference SavedModel for TFLite conversion. - - NOTE: Only supports SSD meta-architectures for now, and the output model will - have static-shaped, single-batch input. - - This function creates `output_directory` if it does not already exist, - which will hold the intermediate SavedModel that can be used with the TFLite - converter. - - Args: - pipeline_config: pipeline_pb2.TrainAndEvalPipelineConfig proto. - trained_checkpoint_dir: Path to the trained checkpoint file. - output_directory: Path to write outputs. - max_detections: Max detections desired from the TFLite model. - use_regular_nms: If True, TFLite model uses the (slower) multi-class NMS. - Note that this argument is only used by the SSD model. - include_keypoints: Decides whether to also output the keypoint predictions. - Note that this argument is only used by the CenterNet model. - label_map_path: Path to the label map which is used by CenterNet keypoint - estimation task. If provided, the label_map_path in the configuration will - be replaced by this one. - - Raises: - ValueError: if pipeline is invalid. - """ - output_saved_model_directory = os.path.join(output_directory, 'saved_model') - - # Build the underlying model using pipeline config. - # TODO(b/162842801): Add support for other architectures. - if pipeline_config.model.WhichOneof('model') == 'ssd': - detection_model = model_builder.build( - pipeline_config.model, is_training=False) - ckpt = tf.train.Checkpoint(model=detection_model) - # The module helps build a TF SavedModel appropriate for TFLite conversion. - detection_module = SSDModule(pipeline_config, detection_model, - max_detections, use_regular_nms) - elif pipeline_config.model.WhichOneof('model') == 'center_net': - detection_module = CenterNetModule( - pipeline_config, max_detections, include_keypoints, - label_map_path=label_map_path) - ckpt = tf.train.Checkpoint(model=detection_module.get_model()) - else: - raise ValueError('Only ssd or center_net models are supported in tflite. ' - 'Found {} in config'.format( - pipeline_config.model.WhichOneof('model'))) - - manager = tf.train.CheckpointManager( - ckpt, trained_checkpoint_dir, max_to_keep=1) - status = ckpt.restore(manager.latest_checkpoint).expect_partial() - - # Getting the concrete function traces the graph and forces variables to - # be constructed; only after this can we save the saved model. - status.assert_existing_objects_matched() - concrete_function = detection_module.inference_fn.get_concrete_function( - tf.TensorSpec( - shape=detection_module.input_shape(), dtype=tf.float32, name='input')) - status.assert_existing_objects_matched() - - # Export SavedModel. - tf.saved_model.save( - detection_module, - output_saved_model_directory, - signatures=concrete_function) diff --git a/research/object_detection/export_tflite_graph_lib_tf2_test.py b/research/object_detection/export_tflite_graph_lib_tf2_test.py deleted file mode 100644 index edb257145e9..00000000000 --- a/research/object_detection/export_tflite_graph_lib_tf2_test.py +++ /dev/null @@ -1,341 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Test for export_tflite_graph_lib_tf2.py.""" - -from __future__ import division -import os -import unittest -import six - -import tensorflow.compat.v2 as tf - -from object_detection import export_tflite_graph_lib_tf2 -from object_detection.builders import model_builder -from object_detection.core import model -from object_detection.protos import pipeline_pb2 -from object_detection.utils import tf_version -from google.protobuf import text_format - -if six.PY2: - import mock # pylint: disable=g-importing-member,g-import-not-at-top -else: - from unittest import mock # pylint: disable=g-importing-member,g-import-not-at-top - - -class FakeModel(model.DetectionModel): - - def __init__(self): - super(FakeModel, self).__init__(num_classes=2) - self._conv = tf.keras.layers.Conv2D( - filters=1, - kernel_size=1, - strides=(1, 1), - padding='valid', - kernel_initializer=tf.keras.initializers.Constant(value=1.0)) - - def preprocess(self, inputs): - true_image_shapes = [] # Doesn't matter for the fake model. - return tf.identity(inputs), true_image_shapes - - def predict(self, preprocessed_inputs, true_image_shapes): - prediction_tensors = {'image': self._conv(preprocessed_inputs)} - with tf.control_dependencies([prediction_tensors['image']]): - prediction_tensors['box_encodings'] = tf.constant( - [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]], tf.float32) - prediction_tensors['class_predictions_with_background'] = tf.constant( - [[[0.7, 0.6], [0.9, 0.0]]], tf.float32) - with tf.control_dependencies([ - tf.convert_to_tensor( - prediction_tensors['image'].get_shape().as_list()[1:3]) - ]): - prediction_tensors['anchors'] = tf.constant( - [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 1.0]], tf.float32) - return prediction_tensors - - def postprocess(self, prediction_dict, true_image_shapes): - predict_tensor_sum = tf.reduce_sum(prediction_dict['image']) - with tf.control_dependencies(list(prediction_dict.values())): - postprocessed_tensors = { - 'detection_boxes': - tf.constant([[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]], - tf.float32), - 'detection_scores': - predict_tensor_sum + - tf.constant([[0.7, 0.6], [0.9, 0.0]], tf.float32), - 'detection_classes': - tf.constant([[0, 1], [1, 0]], tf.float32), - 'num_detections': - tf.constant([2, 1], tf.float32), - 'detection_keypoints': - tf.zeros([2, 17, 2], tf.float32), - 'detection_keypoint_scores': - tf.zeros([2, 17], tf.float32), - } - return postprocessed_tensors - - def restore_map(self, checkpoint_path, from_detection_checkpoint): - pass - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def loss(self, prediction_dict, true_image_shapes): - pass - - def regularization_losses(self): - pass - - def updates(self): - pass - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ExportTfLiteGraphTest(tf.test.TestCase): - - def _save_checkpoint_from_mock_model(self, checkpoint_dir): - mock_model = FakeModel() - fake_image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - preprocessed_inputs, true_image_shapes = mock_model.preprocess(fake_image) - predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) - mock_model.postprocess(predictions, true_image_shapes) - - ckpt = tf.train.Checkpoint(model=mock_model) - exported_checkpoint_manager = tf.train.CheckpointManager( - ckpt, checkpoint_dir, max_to_keep=1) - exported_checkpoint_manager.save(checkpoint_number=0) - - def _get_ssd_config(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - pipeline_config.model.ssd.post_processing.batch_non_max_suppression.iou_threshold = 0.5 - return pipeline_config - - def _get_center_net_config(self): - pipeline_config_text = """ -model { - center_net { - num_classes: 1 - feature_extractor { - type: "mobilenet_v2_fpn" - } - image_resizer { - fixed_shape_resizer { - height: 10 - width: 10 - } - } - object_detection_task { - localization_loss { - l1_localization_loss { - } - } - } - object_center_params { - classification_loss { - } - max_box_predictions: 20 - } - keypoint_estimation_task { - loss { - localization_loss { - l1_localization_loss { - } - } - classification_loss { - penalty_reduced_logistic_focal_loss { - } - } - } - } - } -} - """ - return text_format.Parse( - pipeline_config_text, pipeline_pb2.TrainEvalPipelineConfig()) - - # The tf.implements signature is important since it ensures MLIR legalization, - # so we test it here. - def test_postprocess_implements_signature(self): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - pipeline_config = self._get_ssd_config() - - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - - detection_model = model_builder.build( - pipeline_config.model, is_training=False) - - ckpt = tf.train.Checkpoint(model=detection_model) - manager = tf.train.CheckpointManager(ckpt, tmp_dir, max_to_keep=1) - ckpt.restore(manager.latest_checkpoint).expect_partial() - - # The module helps build a TF graph appropriate for TFLite conversion. - detection_module = export_tflite_graph_lib_tf2.SSDModule( - pipeline_config=pipeline_config, - detection_model=detection_model, - max_detections=20, - use_regular_nms=True) - - expected_signature = ('name: "TFLite_Detection_PostProcess" attr { key: ' - '"max_detections" value { i: 20 } } attr { key: ' - '"max_classes_per_detection" value { i: 1 } } attr ' - '{ key: "use_regular_nms" value { b: true } } attr ' - '{ key: "nms_score_threshold" value { f: 0.000000 }' - ' } attr { key: "nms_iou_threshold" value { f: ' - '0.500000 } } attr { key: "y_scale" value { f: ' - '10.000000 } } attr { key: "x_scale" value { f: ' - '10.000000 } } attr { key: "h_scale" value { f: ' - '5.000000 } } attr { key: "w_scale" value { f: ' - '5.000000 } } attr { key: "num_classes" value { i: ' - '2 } }') - - self.assertEqual(expected_signature, - detection_module.postprocess_implements_signature()) - - def test_unsupported_architecture(self): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.model.faster_rcnn.num_classes = 10 - - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - expected_message = 'Only ssd or center_net models are supported in tflite' - try: - export_tflite_graph_lib_tf2.export_tflite_model( - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory, - max_detections=10, - use_regular_nms=False) - except ValueError as e: - if expected_message not in str(e): - raise - else: - raise AssertionError('Exception not raised: %s' % expected_message) - - def test_export_yields_saved_model(self): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - export_tflite_graph_lib_tf2.export_tflite_model( - pipeline_config=self._get_ssd_config(), - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory, - max_detections=10, - use_regular_nms=False) - self.assertTrue( - os.path.exists( - os.path.join(output_directory, 'saved_model', 'saved_model.pb'))) - self.assertTrue( - os.path.exists( - os.path.join(output_directory, 'saved_model', 'variables', - 'variables.index'))) - self.assertTrue( - os.path.exists( - os.path.join(output_directory, 'saved_model', 'variables', - 'variables.data-00000-of-00001'))) - - def test_exported_model_inference(self): - tmp_dir = self.get_temp_dir() - output_directory = os.path.join(tmp_dir, 'output') - self._save_checkpoint_from_mock_model(tmp_dir) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - export_tflite_graph_lib_tf2.export_tflite_model( - pipeline_config=self._get_ssd_config(), - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory, - max_detections=10, - use_regular_nms=False) - - saved_model_path = os.path.join(output_directory, 'saved_model') - detect_fn = tf.saved_model.load(saved_model_path) - detect_fn_sig = detect_fn.signatures['serving_default'] - image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - detections = detect_fn_sig(image) - - # The exported graph doesn't have numerically correct outputs, but there - # should be 4. - self.assertEqual(4, len(detections)) - - def test_center_net_inference_object_detection(self): - tmp_dir = self.get_temp_dir() - output_directory = os.path.join(tmp_dir, 'output') - self._save_checkpoint_from_mock_model(tmp_dir) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - export_tflite_graph_lib_tf2.export_tflite_model( - pipeline_config=self._get_center_net_config(), - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory, - max_detections=10, - use_regular_nms=False) - - saved_model_path = os.path.join(output_directory, 'saved_model') - detect_fn = tf.saved_model.load(saved_model_path) - detect_fn_sig = detect_fn.signatures['serving_default'] - image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - detections = detect_fn_sig(image) - - # The exported graph doesn't have numerically correct outputs, but there - # should be 4. - self.assertEqual(4, len(detections)) - - def test_center_net_inference_keypoint(self): - tmp_dir = self.get_temp_dir() - output_directory = os.path.join(tmp_dir, 'output') - self._save_checkpoint_from_mock_model(tmp_dir) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - export_tflite_graph_lib_tf2.export_tflite_model( - pipeline_config=self._get_center_net_config(), - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory, - max_detections=10, - use_regular_nms=False, - include_keypoints=True) - - saved_model_path = os.path.join(output_directory, 'saved_model') - detect_fn = tf.saved_model.load(saved_model_path) - detect_fn_sig = detect_fn.signatures['serving_default'] - image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - detections = detect_fn_sig(image) - - # The exported graph doesn't have numerically correct outputs, but there - # should be 6 (4 for boxes, 2 for keypoints). - self.assertEqual(6, len(detections)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/export_tflite_graph_tf2.py b/research/object_detection/export_tflite_graph_tf2.py deleted file mode 100644 index 3ec4f72e7ac..00000000000 --- a/research/object_detection/export_tflite_graph_tf2.py +++ /dev/null @@ -1,160 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Exports TF2 detection SavedModel for conversion to TensorFlow Lite. - -Link to the TF2 Detection Zoo: -https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md -The output folder will contain an intermediate SavedModel that can be used with -the TfLite converter. - -NOTE: This only supports SSD meta-architectures for now. - -One input: - image: a float32 tensor of shape[1, height, width, 3] containing the - *normalized* input image. - NOTE: See the `preprocess` function defined in the feature extractor class - in the object_detection/models directory. - -Four Outputs: - detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box - locations - detection_classes: a float32 tensor of shape [1, num_boxes] - with class indices - detection_scores: a float32 tensor of shape [1, num_boxes] - with class scores - num_boxes: a float32 tensor of size 1 containing the number of detected boxes - -Example Usage: --------------- -python object_detection/export_tflite_graph_tf2.py \ - --pipeline_config_path path/to/ssd_model/pipeline.config \ - --trained_checkpoint_dir path/to/ssd_model/checkpoint \ - --output_directory path/to/exported_model_directory - -The expected output SavedModel would be in the directory -path/to/exported_model_directory (which is created if it does not exist). - -Config overrides (see the `config_override` flag) are text protobufs -(also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override -certain fields in the provided pipeline_config_path. These are useful for -making small changes to the inference graph that differ from the training or -eval config. - -Example Usage 1 (in which we change the NMS iou_threshold to be 0.5 and -NMS score_threshold to be 0.0): -python object_detection/export_tflite_model_tf2.py \ - --pipeline_config_path path/to/ssd_model/pipeline.config \ - --trained_checkpoint_dir path/to/ssd_model/checkpoint \ - --output_directory path/to/exported_model_directory - --config_override " \ - model{ \ - ssd{ \ - post_processing { \ - batch_non_max_suppression { \ - score_threshold: 0.0 \ - iou_threshold: 0.5 \ - } \ - } \ - } \ - } \ - " - -Example Usage 2 (export CenterNet model for keypoint estimation task with fixed -shape resizer and customized input resolution): -python object_detection/export_tflite_model_tf2.py \ - --pipeline_config_path path/to/ssd_model/pipeline.config \ - --trained_checkpoint_dir path/to/ssd_model/checkpoint \ - --output_directory path/to/exported_model_directory \ - --keypoint_label_map_path path/to/label_map.txt \ - --max_detections 10 \ - --centernet_include_keypoints true \ - --config_override " \ - model{ \ - center_net { \ - image_resizer { \ - fixed_shape_resizer { \ - height: 320 \ - width: 320 \ - } \ - } \ - } \ - }" \ -""" -from absl import app -from absl import flags - -import tensorflow.compat.v2 as tf -from google.protobuf import text_format -from object_detection import export_tflite_graph_lib_tf2 -from object_detection.protos import pipeline_pb2 - -tf.enable_v2_behavior() - -FLAGS = flags.FLAGS - -flags.DEFINE_string( - 'pipeline_config_path', None, - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file.') -flags.DEFINE_string('trained_checkpoint_dir', None, - 'Path to trained checkpoint directory') -flags.DEFINE_string('output_directory', None, 'Path to write outputs.') -flags.DEFINE_string( - 'config_override', '', 'pipeline_pb2.TrainEvalPipelineConfig ' - 'text proto to override pipeline_config_path.') -flags.DEFINE_integer('max_detections', 10, - 'Maximum number of detections (boxes) to return.') -# SSD-specific flags -flags.DEFINE_bool( - 'ssd_use_regular_nms', False, - 'Flag to set postprocessing op to use Regular NMS instead of Fast NMS ' - '(Default false).') -# CenterNet-specific flags -flags.DEFINE_bool( - 'centernet_include_keypoints', False, - 'Whether to export the predicted keypoint tensors. Only CenterNet model' - ' supports this flag.' -) -flags.DEFINE_string( - 'keypoint_label_map_path', None, - 'Path of the label map used by CenterNet keypoint estimation task. If' - ' provided, the label map path in the pipeline config will be replaced by' - ' this one. Note that it is only used when exporting CenterNet model for' - ' keypoint estimation task.' -) - - -def main(argv): - del argv # Unused. - flags.mark_flag_as_required('pipeline_config_path') - flags.mark_flag_as_required('trained_checkpoint_dir') - flags.mark_flag_as_required('output_directory') - - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - - with tf.io.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f: - text_format.Parse(f.read(), pipeline_config) - override_config = pipeline_pb2.TrainEvalPipelineConfig() - text_format.Parse(FLAGS.config_override, override_config) - pipeline_config.MergeFrom(override_config) - - export_tflite_graph_lib_tf2.export_tflite_model( - pipeline_config, FLAGS.trained_checkpoint_dir, FLAGS.output_directory, - FLAGS.max_detections, FLAGS.ssd_use_regular_nms, - FLAGS.centernet_include_keypoints, FLAGS.keypoint_label_map_path) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/object_detection/export_tflite_ssd_graph.py b/research/object_detection/export_tflite_ssd_graph.py deleted file mode 100644 index f37aa514967..00000000000 --- a/research/object_detection/export_tflite_ssd_graph.py +++ /dev/null @@ -1,143 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Exports an SSD detection model to use with tf-lite. - -Outputs file: -* A tflite compatible frozen graph - $output_directory/tflite_graph.pb - -The exported graph has the following input and output nodes. - -Inputs: -'normalized_input_image_tensor': a float32 tensor of shape -[1, height, width, 3] containing the normalized input image. Note that the -height and width must be compatible with the height and width configured in -the fixed_shape_image resizer options in the pipeline config proto. - -In floating point Mobilenet model, 'normalized_image_tensor' has values -between [-1,1). This typically means mapping each pixel (linearly) -to a value between [-1, 1]. Input image -values between 0 and 255 are scaled by (1/128.0) and then a value of --1 is added to them to ensure the range is [-1,1). -In quantized Mobilenet model, 'normalized_image_tensor' has values between [0, -255]. -In general, see the `preprocess` function defined in the feature extractor class -in the object_detection/models directory. - -Outputs: -If add_postprocessing_op is true: frozen graph adds a - TFLite_Detection_PostProcess custom op node has four outputs: - detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box - locations - detection_classes: a float32 tensor of shape [1, num_boxes] - with class indices - detection_scores: a float32 tensor of shape [1, num_boxes] - with class scores - num_boxes: a float32 tensor of size 1 containing the number of detected boxes -else: - the graph has two outputs: - 'raw_outputs/box_encodings': a float32 tensor of shape [1, num_anchors, 4] - containing the encoded box predictions. - 'raw_outputs/class_predictions': a float32 tensor of shape - [1, num_anchors, num_classes] containing the class scores for each anchor - after applying score conversion. - -Example Usage: --------------- -python object_detection/export_tflite_ssd_graph.py \ - --pipeline_config_path path/to/ssd_mobilenet.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory - -The expected output would be in the directory -path/to/exported_model_directory (which is created if it does not exist) -with contents: - - tflite_graph.pbtxt - - tflite_graph.pb -Config overrides (see the `config_override` flag) are text protobufs -(also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override -certain fields in the provided pipeline_config_path. These are useful for -making small changes to the inference graph that differ from the training or -eval config. - -Example Usage (in which we change the NMS iou_threshold to be 0.5 and -NMS score_threshold to be 0.0): -python object_detection/export_tflite_ssd_graph.py \ - --pipeline_config_path path/to/ssd_mobilenet.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory - --config_override " \ - model{ \ - ssd{ \ - post_processing { \ - batch_non_max_suppression { \ - score_threshold: 0.0 \ - iou_threshold: 0.5 \ - } \ - } \ - } \ - } \ - " -""" - -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from object_detection import export_tflite_ssd_graph_lib -from object_detection.protos import pipeline_pb2 - -flags = tf.app.flags -flags.DEFINE_string('output_directory', None, 'Path to write outputs.') -flags.DEFINE_string( - 'pipeline_config_path', None, - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file.') -flags.DEFINE_string('trained_checkpoint_prefix', None, 'Checkpoint prefix.') -flags.DEFINE_integer('max_detections', 10, - 'Maximum number of detections (boxes) to show.') -flags.DEFINE_integer('max_classes_per_detection', 1, - 'Maximum number of classes to output per detection box.') -flags.DEFINE_integer( - 'detections_per_class', 100, - 'Number of anchors used per class in Regular Non-Max-Suppression.') -flags.DEFINE_bool('add_postprocessing_op', True, - 'Add TFLite custom op for postprocessing to the graph.') -flags.DEFINE_bool( - 'use_regular_nms', False, - 'Flag to set postprocessing op to use Regular NMS instead of Fast NMS.') -flags.DEFINE_string( - 'config_override', '', 'pipeline_pb2.TrainEvalPipelineConfig ' - 'text proto to override pipeline_config_path.') - -FLAGS = flags.FLAGS - - -def main(argv): - del argv # Unused. - flags.mark_flag_as_required('output_directory') - flags.mark_flag_as_required('pipeline_config_path') - flags.mark_flag_as_required('trained_checkpoint_prefix') - - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - - with tf.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f: - text_format.Merge(f.read(), pipeline_config) - text_format.Merge(FLAGS.config_override, pipeline_config) - export_tflite_ssd_graph_lib.export_tflite_graph( - pipeline_config, FLAGS.trained_checkpoint_prefix, FLAGS.output_directory, - FLAGS.add_postprocessing_op, FLAGS.max_detections, - FLAGS.max_classes_per_detection, use_regular_nms=FLAGS.use_regular_nms) - - -if __name__ == '__main__': - tf.app.run(main) diff --git a/research/object_detection/export_tflite_ssd_graph_lib.py b/research/object_detection/export_tflite_ssd_graph_lib.py deleted file mode 100644 index 7ad84818350..00000000000 --- a/research/object_detection/export_tflite_ssd_graph_lib.py +++ /dev/null @@ -1,333 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Exports an SSD detection model to use with tf-lite. - -See export_tflite_ssd_graph.py for usage. -""" -import os -import tempfile -import numpy as np -import tensorflow.compat.v1 as tf -from tensorflow.core.framework import attr_value_pb2 -from tensorflow.core.framework import types_pb2 -from tensorflow.core.protobuf import saver_pb2 -from object_detection import exporter -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import model_builder -from object_detection.builders import post_processing_builder -from object_detection.core import box_list -from object_detection.utils import tf_version - -_DEFAULT_NUM_CHANNELS = 3 -_DEFAULT_NUM_COORD_BOX = 4 - -if tf_version.is_tf1(): - from tensorflow.tools.graph_transforms import TransformGraph # pylint: disable=g-import-not-at-top - - -def get_const_center_size_encoded_anchors(anchors): - """Exports center-size encoded anchors as a constant tensor. - - Args: - anchors: a float32 tensor of shape [num_anchors, 4] containing the anchor - boxes - - Returns: - encoded_anchors: a float32 constant tensor of shape [num_anchors, 4] - containing the anchor boxes. - """ - anchor_boxlist = box_list.BoxList(anchors) - y, x, h, w = anchor_boxlist.get_center_coordinates_and_sizes() - num_anchors = y.get_shape().as_list() - - with tf.Session() as sess: - y_out, x_out, h_out, w_out = sess.run([y, x, h, w]) - encoded_anchors = tf.constant( - np.transpose(np.stack((y_out, x_out, h_out, w_out))), - dtype=tf.float32, - shape=[num_anchors[0], _DEFAULT_NUM_COORD_BOX], - name='anchors') - return encoded_anchors - - -def append_postprocessing_op(frozen_graph_def, - max_detections, - max_classes_per_detection, - nms_score_threshold, - nms_iou_threshold, - num_classes, - scale_values, - detections_per_class=100, - use_regular_nms=False, - additional_output_tensors=()): - """Appends postprocessing custom op. - - Args: - frozen_graph_def: Frozen GraphDef for SSD model after freezing the - checkpoint - max_detections: Maximum number of detections (boxes) to show - max_classes_per_detection: Number of classes to display per detection - nms_score_threshold: Score threshold used in Non-maximal suppression in - post-processing - nms_iou_threshold: Intersection-over-union threshold used in Non-maximal - suppression in post-processing - num_classes: number of classes in SSD detector - scale_values: scale values is a dict with following key-value pairs - {y_scale: 10, x_scale: 10, h_scale: 5, w_scale: 5} that are used in decode - centersize boxes - detections_per_class: In regular NonMaxSuppression, number of anchors used - for NonMaxSuppression per class - use_regular_nms: Flag to set postprocessing op to use Regular NMS instead of - Fast NMS. - additional_output_tensors: Array of additional tensor names to output. - Tensors are appended after postprocessing output. - - Returns: - transformed_graph_def: Frozen GraphDef with postprocessing custom op - appended - TFLite_Detection_PostProcess custom op node has four outputs: - detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box - locations - detection_classes: a float32 tensor of shape [1, num_boxes] - with class indices - detection_scores: a float32 tensor of shape [1, num_boxes] - with class scores - num_boxes: a float32 tensor of size 1 containing the number of detected - boxes - """ - new_output = frozen_graph_def.node.add() - new_output.op = 'TFLite_Detection_PostProcess' - new_output.name = 'TFLite_Detection_PostProcess' - new_output.attr['_output_quantized'].CopyFrom( - attr_value_pb2.AttrValue(b=True)) - new_output.attr['_output_types'].list.type.extend([ - types_pb2.DT_FLOAT, types_pb2.DT_FLOAT, types_pb2.DT_FLOAT, - types_pb2.DT_FLOAT - ]) - new_output.attr['_support_output_type_float_in_quantized_op'].CopyFrom( - attr_value_pb2.AttrValue(b=True)) - new_output.attr['max_detections'].CopyFrom( - attr_value_pb2.AttrValue(i=max_detections)) - new_output.attr['max_classes_per_detection'].CopyFrom( - attr_value_pb2.AttrValue(i=max_classes_per_detection)) - new_output.attr['nms_score_threshold'].CopyFrom( - attr_value_pb2.AttrValue(f=nms_score_threshold.pop())) - new_output.attr['nms_iou_threshold'].CopyFrom( - attr_value_pb2.AttrValue(f=nms_iou_threshold.pop())) - new_output.attr['num_classes'].CopyFrom( - attr_value_pb2.AttrValue(i=num_classes)) - - new_output.attr['y_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['y_scale'].pop())) - new_output.attr['x_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['x_scale'].pop())) - new_output.attr['h_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['h_scale'].pop())) - new_output.attr['w_scale'].CopyFrom( - attr_value_pb2.AttrValue(f=scale_values['w_scale'].pop())) - new_output.attr['detections_per_class'].CopyFrom( - attr_value_pb2.AttrValue(i=detections_per_class)) - new_output.attr['use_regular_nms'].CopyFrom( - attr_value_pb2.AttrValue(b=use_regular_nms)) - - new_output.input.extend( - ['raw_outputs/box_encodings', 'raw_outputs/class_predictions', 'anchors']) - # Transform the graph to append new postprocessing op - input_names = [] - output_names = ['TFLite_Detection_PostProcess' - ] + list(additional_output_tensors) - transforms = ['strip_unused_nodes'] - transformed_graph_def = TransformGraph(frozen_graph_def, input_names, - output_names, transforms) - return transformed_graph_def - - -def export_tflite_graph(pipeline_config, - trained_checkpoint_prefix, - output_dir, - add_postprocessing_op, - max_detections, - max_classes_per_detection, - detections_per_class=100, - use_regular_nms=False, - binary_graph_name='tflite_graph.pb', - txt_graph_name='tflite_graph.pbtxt', - additional_output_tensors=()): - """Exports a tflite compatible graph and anchors for ssd detection model. - - Anchors are written to a tensor and tflite compatible graph - is written to output_dir/tflite_graph.pb. - - Args: - pipeline_config: a pipeline.proto object containing the configuration for - SSD model to export. - trained_checkpoint_prefix: a file prefix for the checkpoint containing the - trained parameters of the SSD model. - output_dir: A directory to write the tflite graph and anchor file to. - add_postprocessing_op: If add_postprocessing_op is true: frozen graph adds a - TFLite_Detection_PostProcess custom op - max_detections: Maximum number of detections (boxes) to show - max_classes_per_detection: Number of classes to display per detection - detections_per_class: In regular NonMaxSuppression, number of anchors used - for NonMaxSuppression per class - use_regular_nms: Flag to set postprocessing op to use Regular NMS instead of - Fast NMS. - binary_graph_name: Name of the exported graph file in binary format. - txt_graph_name: Name of the exported graph file in text format. - additional_output_tensors: Array of additional tensor names to output. - Additional tensors are appended to the end of output tensor list. - - Raises: - ValueError: if the pipeline config contains models other than ssd or uses an - fixed_shape_resizer and provides a shape as well. - """ - tf.gfile.MakeDirs(output_dir) - if pipeline_config.model.WhichOneof('model') != 'ssd': - raise ValueError('Only ssd models are supported in tflite. ' - 'Found {} in config'.format( - pipeline_config.model.WhichOneof('model'))) - - num_classes = pipeline_config.model.ssd.num_classes - nms_score_threshold = { - pipeline_config.model.ssd.post_processing.batch_non_max_suppression - .score_threshold - } - nms_iou_threshold = { - pipeline_config.model.ssd.post_processing.batch_non_max_suppression - .iou_threshold - } - scale_values = {} - scale_values['y_scale'] = { - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale - } - scale_values['x_scale'] = { - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale - } - scale_values['h_scale'] = { - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale - } - scale_values['w_scale'] = { - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale - } - - image_resizer_config = pipeline_config.model.ssd.image_resizer - image_resizer = image_resizer_config.WhichOneof('image_resizer_oneof') - num_channels = _DEFAULT_NUM_CHANNELS - if image_resizer == 'fixed_shape_resizer': - height = image_resizer_config.fixed_shape_resizer.height - width = image_resizer_config.fixed_shape_resizer.width - if image_resizer_config.fixed_shape_resizer.convert_to_grayscale: - num_channels = 1 - shape = [1, height, width, num_channels] - else: - raise ValueError( - 'Only fixed_shape_resizer' - 'is supported with tflite. Found {}'.format( - image_resizer_config.WhichOneof('image_resizer_oneof'))) - - image = tf.placeholder( - tf.float32, shape=shape, name='normalized_input_image_tensor') - - detection_model = model_builder.build( - pipeline_config.model, is_training=False) - predicted_tensors = detection_model.predict(image, true_image_shapes=None) - # The score conversion occurs before the post-processing custom op - _, score_conversion_fn = post_processing_builder.build( - pipeline_config.model.ssd.post_processing) - class_predictions = score_conversion_fn( - predicted_tensors['class_predictions_with_background']) - - with tf.name_scope('raw_outputs'): - # 'raw_outputs/box_encodings': a float32 tensor of shape [1, num_anchors, 4] - # containing the encoded box predictions. Note that these are raw - # predictions and no Non-Max suppression is applied on them and - # no decode center size boxes is applied to them. - tf.identity(predicted_tensors['box_encodings'], name='box_encodings') - # 'raw_outputs/class_predictions': a float32 tensor of shape - # [1, num_anchors, num_classes] containing the class scores for each anchor - # after applying score conversion. - tf.identity(class_predictions, name='class_predictions') - # 'anchors': a float32 tensor of shape - # [4, num_anchors] containing the anchors as a constant node. - tf.identity( - get_const_center_size_encoded_anchors(predicted_tensors['anchors']), - name='anchors') - - # Add global step to the graph, so we know the training step number when we - # evaluate the model. - tf.train.get_or_create_global_step() - - # graph rewriter - is_quantized = pipeline_config.HasField('graph_rewriter') - if is_quantized: - graph_rewriter_config = pipeline_config.graph_rewriter - graph_rewriter_fn = graph_rewriter_builder.build( - graph_rewriter_config, is_training=False) - graph_rewriter_fn() - - if pipeline_config.model.ssd.feature_extractor.HasField('fpn'): - exporter.rewrite_nn_resize_op(is_quantized) - - # freeze the graph - saver_kwargs = {} - if pipeline_config.eval_config.use_moving_averages: - saver_kwargs['write_version'] = saver_pb2.SaverDef.V1 - moving_average_checkpoint = tempfile.NamedTemporaryFile() - exporter.replace_variable_values_with_moving_averages( - tf.get_default_graph(), trained_checkpoint_prefix, - moving_average_checkpoint.name) - checkpoint_to_use = moving_average_checkpoint.name - else: - checkpoint_to_use = trained_checkpoint_prefix - - saver = tf.train.Saver(**saver_kwargs) - input_saver_def = saver.as_saver_def() - frozen_graph_def = exporter.freeze_graph_with_def_protos( - input_graph_def=tf.get_default_graph().as_graph_def(), - input_saver_def=input_saver_def, - input_checkpoint=checkpoint_to_use, - output_node_names=','.join([ - 'raw_outputs/box_encodings', 'raw_outputs/class_predictions', - 'anchors' - ] + list(additional_output_tensors)), - restore_op_name='save/restore_all', - filename_tensor_name='save/Const:0', - clear_devices=True, - output_graph='', - initializer_nodes='') - - # Add new operation to do post processing in a custom op (TF Lite only) - if add_postprocessing_op: - transformed_graph_def = append_postprocessing_op( - frozen_graph_def, - max_detections, - max_classes_per_detection, - nms_score_threshold, - nms_iou_threshold, - num_classes, - scale_values, - detections_per_class, - use_regular_nms, - additional_output_tensors=additional_output_tensors) - else: - # Return frozen without adding post-processing custom op - transformed_graph_def = frozen_graph_def - - binary_graph = os.path.join(output_dir, binary_graph_name) - with tf.gfile.GFile(binary_graph, 'wb') as f: - f.write(transformed_graph_def.SerializeToString()) - txt_graph = os.path.join(output_dir, txt_graph_name) - with tf.gfile.GFile(txt_graph, 'w') as f: - f.write(str(transformed_graph_def)) diff --git a/research/object_detection/export_tflite_ssd_graph_lib_tf1_test.py b/research/object_detection/export_tflite_ssd_graph_lib_tf1_test.py deleted file mode 100644 index 87401568119..00000000000 --- a/research/object_detection/export_tflite_ssd_graph_lib_tf1_test.py +++ /dev/null @@ -1,425 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.export_tflite_ssd_graph.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import os -import unittest -import numpy as np -import six -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from tensorflow.core.framework import types_pb2 -from object_detection import export_tflite_ssd_graph_lib -from object_detection import exporter -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import model_builder -from object_detection.core import model -from object_detection.protos import graph_rewriter_pb2 -from object_detection.protos import pipeline_pb2 -from object_detection.protos import post_processing_pb2 -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top - -if six.PY2: - import mock -else: - from unittest import mock # pylint: disable=g-importing-member -# pylint: enable=g-import-not-at-top - - -class FakeModel(model.DetectionModel): - - def __init__(self, add_detection_masks=False): - self._add_detection_masks = add_detection_masks - - def preprocess(self, inputs): - pass - - def predict(self, preprocessed_inputs, true_image_shapes): - features = slim.conv2d(preprocessed_inputs, 3, 1) - with tf.control_dependencies([features]): - prediction_tensors = { - 'box_encodings': - tf.constant([[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]], - tf.float32), - 'class_predictions_with_background': - tf.constant([[[0.7, 0.6], [0.9, 0.0]]], tf.float32), - } - with tf.control_dependencies( - [tf.convert_to_tensor(features.get_shape().as_list()[1:3])]): - prediction_tensors['anchors'] = tf.constant( - [[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 1.0, 1.0]], tf.float32) - return prediction_tensors - - def postprocess(self, prediction_tensors, true_image_shapes): - pass - - def restore_map(self, checkpoint_path, from_detection_checkpoint): - pass - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def loss(self, prediction_dict, true_image_shapes): - pass - - def regularization_losses(self): - pass - - def updates(self): - pass - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ExportTfliteGraphTest(tf.test.TestCase): - - def _save_checkpoint_from_mock_model(self, - checkpoint_path, - use_moving_averages, - quantize=False, - num_channels=3): - g = tf.Graph() - with g.as_default(): - mock_model = FakeModel() - inputs = tf.placeholder(tf.float32, shape=[1, 10, 10, num_channels]) - mock_model.predict(inputs, true_image_shapes=None) - if use_moving_averages: - tf.train.ExponentialMovingAverage(0.0).apply() - tf.train.get_or_create_global_step() - if quantize: - graph_rewriter_config = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_config.quantization.delay = 500000 - graph_rewriter_fn = graph_rewriter_builder.build( - graph_rewriter_config, is_training=False) - graph_rewriter_fn() - - saver = tf.train.Saver() - init = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init) - saver.save(sess, checkpoint_path) - - def _assert_quant_vars_exists(self, tflite_graph_file): - with tf.gfile.Open(tflite_graph_file, mode='rb') as f: - graph_string = f.read() - print(graph_string) - self.assertIn(six.ensure_binary('quant'), graph_string) - - def _import_graph_and_run_inference(self, tflite_graph_file, num_channels=3): - """Imports a tflite graph, runs single inference and returns outputs.""" - graph = tf.Graph() - with graph.as_default(): - graph_def = tf.GraphDef() - with tf.gfile.Open(tflite_graph_file, mode='rb') as f: - graph_def.ParseFromString(f.read()) - tf.import_graph_def(graph_def, name='') - input_tensor = graph.get_tensor_by_name('normalized_input_image_tensor:0') - box_encodings = graph.get_tensor_by_name('raw_outputs/box_encodings:0') - class_predictions = graph.get_tensor_by_name( - 'raw_outputs/class_predictions:0') - with self.test_session(graph) as sess: - [box_encodings_np, class_predictions_np] = sess.run( - [box_encodings, class_predictions], - feed_dict={input_tensor: np.random.rand(1, 10, 10, num_channels)}) - return box_encodings_np, class_predictions_np - - def _export_graph(self, - pipeline_config, - num_channels=3, - additional_output_tensors=()): - """Exports a tflite graph.""" - output_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(output_dir, 'model.ckpt') - tflite_graph_file = os.path.join(output_dir, 'tflite_graph.pb') - - quantize = pipeline_config.HasField('graph_rewriter') - self._save_checkpoint_from_mock_model( - trained_checkpoint_prefix, - use_moving_averages=pipeline_config.eval_config.use_moving_averages, - quantize=quantize, - num_channels=num_channels) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - - with tf.Graph().as_default(): - tf.identity( - tf.constant([[1, 2], [3, 4]], tf.uint8), name='UnattachedTensor') - export_tflite_ssd_graph_lib.export_tflite_graph( - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_dir=output_dir, - add_postprocessing_op=False, - max_detections=10, - max_classes_per_detection=1, - additional_output_tensors=additional_output_tensors) - return tflite_graph_file - - def _export_graph_with_postprocessing_op(self, - pipeline_config, - num_channels=3, - additional_output_tensors=()): - """Exports a tflite graph with custom postprocessing op.""" - output_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(output_dir, 'model.ckpt') - tflite_graph_file = os.path.join(output_dir, 'tflite_graph.pb') - - quantize = pipeline_config.HasField('graph_rewriter') - self._save_checkpoint_from_mock_model( - trained_checkpoint_prefix, - use_moving_averages=pipeline_config.eval_config.use_moving_averages, - quantize=quantize, - num_channels=num_channels) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - - with tf.Graph().as_default(): - tf.identity( - tf.constant([[1, 2], [3, 4]], tf.uint8), name='UnattachedTensor') - export_tflite_ssd_graph_lib.export_tflite_graph( - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_dir=output_dir, - add_postprocessing_op=True, - max_detections=10, - max_classes_per_detection=1, - additional_output_tensors=additional_output_tensors) - return tflite_graph_file - - def test_export_tflite_graph_with_moving_averages(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = True - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - tflite_graph_file = self._export_graph(pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - - (box_encodings_np, class_predictions_np - ) = self._import_graph_and_run_inference(tflite_graph_file) - self.assertAllClose(box_encodings_np, - [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) - self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) - - def test_export_tflite_graph_without_moving_averages(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - tflite_graph_file = self._export_graph(pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - (box_encodings_np, class_predictions_np - ) = self._import_graph_and_run_inference(tflite_graph_file) - self.assertAllClose(box_encodings_np, - [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) - self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) - - def test_export_tflite_graph_grayscale(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - (pipeline_config.model.ssd.image_resizer.fixed_shape_resizer - ).convert_to_grayscale = True - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - tflite_graph_file = self._export_graph(pipeline_config, num_channels=1) - self.assertTrue(os.path.exists(tflite_graph_file)) - (box_encodings_np, - class_predictions_np) = self._import_graph_and_run_inference( - tflite_graph_file, num_channels=1) - self.assertAllClose(box_encodings_np, - [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) - self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) - - def test_export_tflite_graph_with_quantization(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.graph_rewriter.quantization.delay = 500000 - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - tflite_graph_file = self._export_graph(pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - self._assert_quant_vars_exists(tflite_graph_file) - (box_encodings_np, class_predictions_np - ) = self._import_graph_and_run_inference(tflite_graph_file) - self.assertAllClose(box_encodings_np, - [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) - self.assertAllClose(class_predictions_np, [[[0.7, 0.6], [0.9, 0.0]]]) - - def test_export_tflite_graph_with_softmax_score_conversion(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.post_processing.score_converter = ( - post_processing_pb2.PostProcessing.SOFTMAX) - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - tflite_graph_file = self._export_graph(pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - (box_encodings_np, class_predictions_np - ) = self._import_graph_and_run_inference(tflite_graph_file) - self.assertAllClose(box_encodings_np, - [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) - self.assertAllClose(class_predictions_np, - [[[0.524979, 0.475021], [0.710949, 0.28905]]]) - - def test_export_tflite_graph_with_sigmoid_score_conversion(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.post_processing.score_converter = ( - post_processing_pb2.PostProcessing.SIGMOID) - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - tflite_graph_file = self._export_graph(pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - (box_encodings_np, class_predictions_np - ) = self._import_graph_and_run_inference(tflite_graph_file) - self.assertAllClose(box_encodings_np, - [[[0.0, 0.0, 0.5, 0.5], [0.5, 0.5, 0.8, 0.8]]]) - self.assertAllClose(class_predictions_np, - [[[0.668188, 0.645656], [0.710949, 0.5]]]) - - def test_export_tflite_graph_with_postprocessing_op(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.post_processing.score_converter = ( - post_processing_pb2.PostProcessing.SIGMOID) - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.num_classes = 2 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.y_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.x_scale = 10.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.height_scale = 5.0 - pipeline_config.model.ssd.box_coder.faster_rcnn_box_coder.width_scale = 5.0 - tflite_graph_file = self._export_graph_with_postprocessing_op( - pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - graph = tf.Graph() - with graph.as_default(): - graph_def = tf.GraphDef() - with tf.gfile.Open(tflite_graph_file, mode='rb') as f: - graph_def.ParseFromString(f.read()) - all_op_names = [node.name for node in graph_def.node] - self.assertIn('TFLite_Detection_PostProcess', all_op_names) - self.assertNotIn('UnattachedTensor', all_op_names) - for node in graph_def.node: - if node.name == 'TFLite_Detection_PostProcess': - self.assertTrue(node.attr['_output_quantized'].b) - self.assertTrue( - node.attr['_support_output_type_float_in_quantized_op'].b) - self.assertEqual(node.attr['y_scale'].f, 10.0) - self.assertEqual(node.attr['x_scale'].f, 10.0) - self.assertEqual(node.attr['h_scale'].f, 5.0) - self.assertEqual(node.attr['w_scale'].f, 5.0) - self.assertEqual(node.attr['num_classes'].i, 2) - self.assertTrue( - all([ - t == types_pb2.DT_FLOAT - for t in node.attr['_output_types'].list.type - ])) - - def test_export_tflite_graph_with_additional_tensors(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - tflite_graph_file = self._export_graph( - pipeline_config, additional_output_tensors=['UnattachedTensor']) - self.assertTrue(os.path.exists(tflite_graph_file)) - graph = tf.Graph() - with graph.as_default(): - graph_def = tf.GraphDef() - with tf.gfile.Open(tflite_graph_file, mode='rb') as f: - graph_def.ParseFromString(f.read()) - all_op_names = [node.name for node in graph_def.node] - self.assertIn('UnattachedTensor', all_op_names) - - def test_export_tflite_graph_with_postprocess_op_and_additional_tensors(self): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - pipeline_config.model.ssd.post_processing.score_converter = ( - post_processing_pb2.PostProcessing.SIGMOID) - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.num_classes = 2 - tflite_graph_file = self._export_graph_with_postprocessing_op( - pipeline_config, additional_output_tensors=['UnattachedTensor']) - self.assertTrue(os.path.exists(tflite_graph_file)) - graph = tf.Graph() - with graph.as_default(): - graph_def = tf.GraphDef() - with tf.gfile.Open(tflite_graph_file, mode='rb') as f: - graph_def.ParseFromString(f.read()) - all_op_names = [node.name for node in graph_def.node] - self.assertIn('TFLite_Detection_PostProcess', all_op_names) - self.assertIn('UnattachedTensor', all_op_names) - - @mock.patch.object(exporter, 'rewrite_nn_resize_op') - def test_export_with_nn_resize_op_not_called_without_fpn(self, mock_get): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - tflite_graph_file = self._export_graph_with_postprocessing_op( - pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - mock_get.assert_not_called() - - @mock.patch.object(exporter, 'rewrite_nn_resize_op') - def test_export_with_nn_resize_op_called_with_fpn(self, mock_get): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.height = 10 - pipeline_config.model.ssd.image_resizer.fixed_shape_resizer.width = 10 - pipeline_config.model.ssd.feature_extractor.fpn.min_level = 3 - pipeline_config.model.ssd.feature_extractor.fpn.max_level = 7 - tflite_graph_file = self._export_graph_with_postprocessing_op( - pipeline_config) - self.assertTrue(os.path.exists(tflite_graph_file)) - self.assertEqual(1, mock_get.call_count) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/exporter.py b/research/object_detection/exporter.py deleted file mode 100644 index 884bcda6a34..00000000000 --- a/research/object_detection/exporter.py +++ /dev/null @@ -1,663 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Functions to export object detection inference graph.""" -import os -import tempfile -import tensorflow.compat.v1 as tf -import tf_slim as slim -from tensorflow.core.protobuf import saver_pb2 -from tensorflow.python.tools import freeze_graph # pylint: disable=g-direct-tensorflow-import -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import model_builder -from object_detection.core import standard_fields as fields -from object_detection.data_decoders import tf_example_decoder -from object_detection.utils import config_util -from object_detection.utils import shape_utils - -# pylint: disable=g-import-not-at-top -try: - from tensorflow.contrib import tfprof as contrib_tfprof - from tensorflow.contrib.quantize.python import graph_matcher -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - -freeze_graph_with_def_protos = freeze_graph.freeze_graph_with_def_protos - - -def parse_side_inputs(side_input_shapes_string, side_input_names_string, - side_input_types_string): - """Parses side input flags. - - Args: - side_input_shapes_string: The shape of the side input tensors, provided as a - comma-separated list of integers. A value of -1 is used for unknown - dimensions. A `/` denotes a break, starting the shape of the next side - input tensor. - side_input_names_string: The names of the side input tensors, provided as a - comma-separated list of strings. - side_input_types_string: The type of the side input tensors, provided as a - comma-separated list of types, each of `string`, `integer`, or `float`. - - Returns: - side_input_shapes: A list of shapes. - side_input_names: A list of strings. - side_input_types: A list of tensorflow dtypes. - - """ - if side_input_shapes_string: - side_input_shapes = [] - for side_input_shape_list in side_input_shapes_string.split('/'): - side_input_shape = [ - int(dim) if dim != '-1' else None - for dim in side_input_shape_list.split(',') - ] - side_input_shapes.append(side_input_shape) - else: - raise ValueError('When using side_inputs, side_input_shapes must be ' - 'specified in the input flags.') - if side_input_names_string: - side_input_names = list(side_input_names_string.split(',')) - else: - raise ValueError('When using side_inputs, side_input_names must be ' - 'specified in the input flags.') - if side_input_types_string: - typelookup = {'float': tf.float32, 'int': tf.int32, 'string': tf.string} - side_input_types = [ - typelookup[side_input_type] - for side_input_type in side_input_types_string.split(',') - ] - else: - raise ValueError('When using side_inputs, side_input_types must be ' - 'specified in the input flags.') - return side_input_shapes, side_input_names, side_input_types - - -def rewrite_nn_resize_op(is_quantized=False): - """Replaces a custom nearest-neighbor resize op with the Tensorflow version. - - Some graphs use this custom version for TPU-compatibility. - - Args: - is_quantized: True if the default graph is quantized. - """ - def remove_nn(): - """Remove nearest neighbor upsampling structures and replace with TF op.""" - input_pattern = graph_matcher.OpTypePattern( - 'FakeQuantWithMinMaxVars' if is_quantized else '*') - stack_1_pattern = graph_matcher.OpTypePattern( - 'Pack', inputs=[input_pattern, input_pattern], ordered_inputs=False) - reshape_1_pattern = graph_matcher.OpTypePattern( - 'Reshape', inputs=[stack_1_pattern, 'Const'], ordered_inputs=False) - stack_2_pattern = graph_matcher.OpTypePattern( - 'Pack', - inputs=[reshape_1_pattern, reshape_1_pattern], - ordered_inputs=False) - reshape_2_pattern = graph_matcher.OpTypePattern( - 'Reshape', inputs=[stack_2_pattern, 'Const'], ordered_inputs=False) - consumer_pattern1 = graph_matcher.OpTypePattern( - 'Add|AddV2|Max|Mul', - inputs=[reshape_2_pattern, '*'], - ordered_inputs=False) - consumer_pattern2 = graph_matcher.OpTypePattern( - 'StridedSlice', - inputs=[reshape_2_pattern, '*', '*', '*'], - ordered_inputs=False) - - def replace_matches(consumer_pattern): - """Search for nearest neighbor pattern and replace with TF op.""" - match_counter = 0 - matcher = graph_matcher.GraphMatcher(consumer_pattern) - for match in matcher.match_graph(tf.get_default_graph()): - match_counter += 1 - projection_op = match.get_op(input_pattern) - reshape_2_op = match.get_op(reshape_2_pattern) - consumer_op = match.get_op(consumer_pattern) - nn_resize = tf.image.resize_nearest_neighbor( - projection_op.outputs[0], - reshape_2_op.outputs[0].shape.dims[1:3], - align_corners=False, - name=os.path.split(reshape_2_op.name)[0] + - '/resize_nearest_neighbor') - - for index, op_input in enumerate(consumer_op.inputs): - if op_input == reshape_2_op.outputs[0]: - consumer_op._update_input(index, nn_resize) # pylint: disable=protected-access - break - - return match_counter - - match_counter = replace_matches(consumer_pattern1) - match_counter += replace_matches(consumer_pattern2) - - tf.logging.info('Found and fixed {} matches'.format(match_counter)) - return match_counter - - # Applying twice because both inputs to Add could be NN pattern - total_removals = 0 - while remove_nn(): - total_removals += 1 - # This number is chosen based on the nas-fpn architecture. - if total_removals > 4: - raise ValueError('Graph removal encountered a infinite loop.') - - -def replace_variable_values_with_moving_averages(graph, - current_checkpoint_file, - new_checkpoint_file, - no_ema_collection=None): - """Replaces variable values in the checkpoint with their moving averages. - - If the current checkpoint has shadow variables maintaining moving averages of - the variables defined in the graph, this function generates a new checkpoint - where the variables contain the values of their moving averages. - - Args: - graph: a tf.Graph object. - current_checkpoint_file: a checkpoint containing both original variables and - their moving averages. - new_checkpoint_file: file path to write a new checkpoint. - no_ema_collection: A list of namescope substrings to match the variables - to eliminate EMA. - """ - with graph.as_default(): - variable_averages = tf.train.ExponentialMovingAverage(0.0) - ema_variables_to_restore = variable_averages.variables_to_restore() - ema_variables_to_restore = config_util.remove_unnecessary_ema( - ema_variables_to_restore, no_ema_collection) - with tf.Session() as sess: - read_saver = tf.train.Saver(ema_variables_to_restore) - read_saver.restore(sess, current_checkpoint_file) - write_saver = tf.train.Saver() - write_saver.save(sess, new_checkpoint_file) - - -def _image_tensor_input_placeholder(input_shape=None): - """Returns input placeholder and a 4-D uint8 image tensor.""" - if input_shape is None: - input_shape = (None, None, None, 3) - input_tensor = tf.placeholder( - dtype=tf.uint8, shape=input_shape, name='image_tensor') - return input_tensor, input_tensor - - -def _side_input_tensor_placeholder(side_input_shape, side_input_name, - side_input_type): - """Returns side input placeholder and side input tensor.""" - side_input_tensor = tf.placeholder( - dtype=side_input_type, shape=side_input_shape, name=side_input_name) - return side_input_tensor, side_input_tensor - - -def _tf_example_input_placeholder(input_shape=None): - """Returns input that accepts a batch of strings with tf examples. - - Args: - input_shape: the shape to resize the output decoded images to (optional). - - Returns: - a tuple of input placeholder and the output decoded images. - """ - batch_tf_example_placeholder = tf.placeholder( - tf.string, shape=[None], name='tf_example') - def decode(tf_example_string_tensor): - tensor_dict = tf_example_decoder.TfExampleDecoder().decode( - tf_example_string_tensor) - image_tensor = tensor_dict[fields.InputDataFields.image] - if input_shape is not None: - image_tensor = tf.image.resize(image_tensor, input_shape[1:3]) - return image_tensor - return (batch_tf_example_placeholder, - shape_utils.static_or_dynamic_map_fn( - decode, - elems=batch_tf_example_placeholder, - dtype=tf.uint8, - parallel_iterations=32, - back_prop=False)) - - -def _encoded_image_string_tensor_input_placeholder(input_shape=None): - """Returns input that accepts a batch of PNG or JPEG strings. - - Args: - input_shape: the shape to resize the output decoded images to (optional). - - Returns: - a tuple of input placeholder and the output decoded images. - """ - batch_image_str_placeholder = tf.placeholder( - dtype=tf.string, - shape=[None], - name='encoded_image_string_tensor') - def decode(encoded_image_string_tensor): - image_tensor = tf.image.decode_image(encoded_image_string_tensor, - channels=3) - image_tensor.set_shape((None, None, 3)) - if input_shape is not None: - image_tensor = tf.image.resize(image_tensor, input_shape[1:3]) - return image_tensor - return (batch_image_str_placeholder, - tf.map_fn( - decode, - elems=batch_image_str_placeholder, - dtype=tf.uint8, - parallel_iterations=32, - back_prop=False)) - - -input_placeholder_fn_map = { - 'image_tensor': _image_tensor_input_placeholder, - 'encoded_image_string_tensor': - _encoded_image_string_tensor_input_placeholder, - 'tf_example': _tf_example_input_placeholder -} - - -def add_output_tensor_nodes(postprocessed_tensors, - output_collection_name='inference_op'): - """Adds output nodes for detection boxes and scores. - - Adds the following nodes for output tensors - - * num_detections: float32 tensor of shape [batch_size]. - * detection_boxes: float32 tensor of shape [batch_size, num_boxes, 4] - containing detected boxes. - * detection_scores: float32 tensor of shape [batch_size, num_boxes] - containing scores for the detected boxes. - * detection_multiclass_scores: (Optional) float32 tensor of shape - [batch_size, num_boxes, num_classes_with_background] for containing class - score distribution for detected boxes including background if any. - * detection_features: (Optional) float32 tensor of shape - [batch, num_boxes, roi_height, roi_width, depth] - containing classifier features - for each detected box - * detection_classes: float32 tensor of shape [batch_size, num_boxes] - containing class predictions for the detected boxes. - * detection_keypoints: (Optional) float32 tensor of shape - [batch_size, num_boxes, num_keypoints, 2] containing keypoints for each - detection box. - * detection_masks: (Optional) float32 tensor of shape - [batch_size, num_boxes, mask_height, mask_width] containing masks for each - detection box. - - Args: - postprocessed_tensors: a dictionary containing the following fields - 'detection_boxes': [batch, max_detections, 4] - 'detection_scores': [batch, max_detections] - 'detection_multiclass_scores': [batch, max_detections, - num_classes_with_background] - 'detection_features': [batch, num_boxes, roi_height, roi_width, depth] - 'detection_classes': [batch, max_detections] - 'detection_masks': [batch, max_detections, mask_height, mask_width] - (optional). - 'detection_keypoints': [batch, max_detections, num_keypoints, 2] - (optional). - 'num_detections': [batch] - output_collection_name: Name of collection to add output tensors to. - - Returns: - A tensor dict containing the added output tensor nodes. - """ - detection_fields = fields.DetectionResultFields - label_id_offset = 1 - boxes = postprocessed_tensors.get(detection_fields.detection_boxes) - scores = postprocessed_tensors.get(detection_fields.detection_scores) - multiclass_scores = postprocessed_tensors.get( - detection_fields.detection_multiclass_scores) - box_classifier_features = postprocessed_tensors.get( - detection_fields.detection_features) - raw_boxes = postprocessed_tensors.get(detection_fields.raw_detection_boxes) - raw_scores = postprocessed_tensors.get(detection_fields.raw_detection_scores) - classes = postprocessed_tensors.get( - detection_fields.detection_classes) + label_id_offset - keypoints = postprocessed_tensors.get(detection_fields.detection_keypoints) - masks = postprocessed_tensors.get(detection_fields.detection_masks) - num_detections = postprocessed_tensors.get(detection_fields.num_detections) - outputs = {} - outputs[detection_fields.detection_boxes] = tf.identity( - boxes, name=detection_fields.detection_boxes) - outputs[detection_fields.detection_scores] = tf.identity( - scores, name=detection_fields.detection_scores) - if multiclass_scores is not None: - outputs[detection_fields.detection_multiclass_scores] = tf.identity( - multiclass_scores, name=detection_fields.detection_multiclass_scores) - if box_classifier_features is not None: - outputs[detection_fields.detection_features] = tf.identity( - box_classifier_features, - name=detection_fields.detection_features) - outputs[detection_fields.detection_classes] = tf.identity( - classes, name=detection_fields.detection_classes) - outputs[detection_fields.num_detections] = tf.identity( - num_detections, name=detection_fields.num_detections) - if raw_boxes is not None: - outputs[detection_fields.raw_detection_boxes] = tf.identity( - raw_boxes, name=detection_fields.raw_detection_boxes) - if raw_scores is not None: - outputs[detection_fields.raw_detection_scores] = tf.identity( - raw_scores, name=detection_fields.raw_detection_scores) - if keypoints is not None: - outputs[detection_fields.detection_keypoints] = tf.identity( - keypoints, name=detection_fields.detection_keypoints) - if masks is not None: - outputs[detection_fields.detection_masks] = tf.identity( - masks, name=detection_fields.detection_masks) - for output_key in outputs: - tf.add_to_collection(output_collection_name, outputs[output_key]) - - return outputs - - -def write_saved_model(saved_model_path, - frozen_graph_def, - inputs, - outputs): - """Writes SavedModel to disk. - - If checkpoint_path is not None bakes the weights into the graph thereby - eliminating the need of checkpoint files during inference. If the model - was trained with moving averages, setting use_moving_averages to true - restores the moving averages, otherwise the original set of variables - is restored. - - Args: - saved_model_path: Path to write SavedModel. - frozen_graph_def: tf.GraphDef holding frozen graph. - inputs: A tensor dictionary containing the inputs to a DetectionModel. - outputs: A tensor dictionary containing the outputs of a DetectionModel. - """ - with tf.Graph().as_default(): - with tf.Session() as sess: - - tf.import_graph_def(frozen_graph_def, name='') - - builder = tf.saved_model.builder.SavedModelBuilder(saved_model_path) - - tensor_info_inputs = {} - if isinstance(inputs, dict): - for k, v in inputs.items(): - tensor_info_inputs[k] = tf.saved_model.utils.build_tensor_info(v) - else: - tensor_info_inputs['inputs'] = tf.saved_model.utils.build_tensor_info( - inputs) - tensor_info_outputs = {} - for k, v in outputs.items(): - tensor_info_outputs[k] = tf.saved_model.utils.build_tensor_info(v) - - detection_signature = ( - tf.saved_model.signature_def_utils.build_signature_def( - inputs=tensor_info_inputs, - outputs=tensor_info_outputs, - method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME - )) - - builder.add_meta_graph_and_variables( - sess, - [tf.saved_model.tag_constants.SERVING], - signature_def_map={ - tf.saved_model.signature_constants - .DEFAULT_SERVING_SIGNATURE_DEF_KEY: - detection_signature, - }, - ) - builder.save() - - -def write_graph_and_checkpoint(inference_graph_def, - model_path, - input_saver_def, - trained_checkpoint_prefix): - """Writes the graph and the checkpoint into disk.""" - for node in inference_graph_def.node: - node.device = '' - with tf.Graph().as_default(): - tf.import_graph_def(inference_graph_def, name='') - with tf.Session() as sess: - saver = tf.train.Saver( - saver_def=input_saver_def, save_relative_paths=True) - saver.restore(sess, trained_checkpoint_prefix) - saver.save(sess, model_path) - - -def _get_outputs_from_inputs(input_tensors, detection_model, - output_collection_name, **side_inputs): - inputs = tf.cast(input_tensors, dtype=tf.float32) - preprocessed_inputs, true_image_shapes = detection_model.preprocess(inputs) - output_tensors = detection_model.predict( - preprocessed_inputs, true_image_shapes, **side_inputs) - postprocessed_tensors = detection_model.postprocess( - output_tensors, true_image_shapes) - return add_output_tensor_nodes(postprocessed_tensors, - output_collection_name) - - -def build_detection_graph(input_type, detection_model, input_shape, - output_collection_name, graph_hook_fn, - use_side_inputs=False, side_input_shapes=None, - side_input_names=None, side_input_types=None): - """Build the detection graph.""" - if input_type not in input_placeholder_fn_map: - raise ValueError('Unknown input type: {}'.format(input_type)) - placeholder_args = {} - side_inputs = {} - if input_shape is not None: - if (input_type != 'image_tensor' and - input_type != 'encoded_image_string_tensor' and - input_type != 'tf_example' and - input_type != 'tf_sequence_example'): - raise ValueError('Can only specify input shape for `image_tensor`, ' - '`encoded_image_string_tensor`, `tf_example`, ' - ' or `tf_sequence_example` inputs.') - placeholder_args['input_shape'] = input_shape - placeholder_tensor, input_tensors = input_placeholder_fn_map[input_type]( - **placeholder_args) - placeholder_tensors = {'inputs': placeholder_tensor} - if use_side_inputs: - for idx, side_input_name in enumerate(side_input_names): - side_input_placeholder, side_input = _side_input_tensor_placeholder( - side_input_shapes[idx], side_input_name, side_input_types[idx]) - print(side_input) - side_inputs[side_input_name] = side_input - placeholder_tensors[side_input_name] = side_input_placeholder - outputs = _get_outputs_from_inputs( - input_tensors=input_tensors, - detection_model=detection_model, - output_collection_name=output_collection_name, - **side_inputs) - - # Add global step to the graph. - slim.get_or_create_global_step() - - if graph_hook_fn: graph_hook_fn() - - return outputs, placeholder_tensors - - -def _export_inference_graph(input_type, - detection_model, - use_moving_averages, - trained_checkpoint_prefix, - output_directory, - additional_output_tensor_names=None, - input_shape=None, - output_collection_name='inference_op', - graph_hook_fn=None, - write_inference_graph=False, - temp_checkpoint_prefix='', - use_side_inputs=False, - side_input_shapes=None, - side_input_names=None, - side_input_types=None): - """Export helper.""" - tf.gfile.MakeDirs(output_directory) - frozen_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - saved_model_path = os.path.join(output_directory, 'saved_model') - model_path = os.path.join(output_directory, 'model.ckpt') - - outputs, placeholder_tensor_dict = build_detection_graph( - input_type=input_type, - detection_model=detection_model, - input_shape=input_shape, - output_collection_name=output_collection_name, - graph_hook_fn=graph_hook_fn, - use_side_inputs=use_side_inputs, - side_input_shapes=side_input_shapes, - side_input_names=side_input_names, - side_input_types=side_input_types) - - profile_inference_graph(tf.get_default_graph()) - saver_kwargs = {} - if use_moving_averages: - if not temp_checkpoint_prefix: - # This check is to be compatible with both version of SaverDef. - if os.path.isfile(trained_checkpoint_prefix): - saver_kwargs['write_version'] = saver_pb2.SaverDef.V1 - temp_checkpoint_prefix = tempfile.NamedTemporaryFile().name - else: - temp_checkpoint_prefix = tempfile.mkdtemp() - replace_variable_values_with_moving_averages( - tf.get_default_graph(), trained_checkpoint_prefix, - temp_checkpoint_prefix) - checkpoint_to_use = temp_checkpoint_prefix - else: - checkpoint_to_use = trained_checkpoint_prefix - - saver = tf.train.Saver(**saver_kwargs) - input_saver_def = saver.as_saver_def() - - write_graph_and_checkpoint( - inference_graph_def=tf.get_default_graph().as_graph_def(), - model_path=model_path, - input_saver_def=input_saver_def, - trained_checkpoint_prefix=checkpoint_to_use) - if write_inference_graph: - inference_graph_def = tf.get_default_graph().as_graph_def() - inference_graph_path = os.path.join(output_directory, - 'inference_graph.pbtxt') - for node in inference_graph_def.node: - node.device = '' - with tf.gfile.GFile(inference_graph_path, 'wb') as f: - f.write(str(inference_graph_def)) - - if additional_output_tensor_names is not None: - output_node_names = ','.join(list(outputs.keys())+( - additional_output_tensor_names)) - else: - output_node_names = ','.join(outputs.keys()) - - frozen_graph_def = freeze_graph.freeze_graph_with_def_protos( - input_graph_def=tf.get_default_graph().as_graph_def(), - input_saver_def=input_saver_def, - input_checkpoint=checkpoint_to_use, - output_node_names=output_node_names, - restore_op_name='save/restore_all', - filename_tensor_name='save/Const:0', - output_graph=frozen_graph_path, - clear_devices=True, - initializer_nodes='') - - write_saved_model(saved_model_path, frozen_graph_def, - placeholder_tensor_dict, outputs) - - -def export_inference_graph(input_type, - pipeline_config, - trained_checkpoint_prefix, - output_directory, - input_shape=None, - output_collection_name='inference_op', - additional_output_tensor_names=None, - write_inference_graph=False, - use_side_inputs=False, - side_input_shapes=None, - side_input_names=None, - side_input_types=None): - """Exports inference graph for the model specified in the pipeline config. - - Args: - input_type: Type of input for the graph. Can be one of ['image_tensor', - 'encoded_image_string_tensor', 'tf_example']. - pipeline_config: pipeline_pb2.TrainAndEvalPipelineConfig proto. - trained_checkpoint_prefix: Path to the trained checkpoint file. - output_directory: Path to write outputs. - input_shape: Sets a fixed shape for an `image_tensor` input. If not - specified, will default to [None, None, None, 3]. - output_collection_name: Name of collection to add output tensors to. - If None, does not add output tensors to a collection. - additional_output_tensor_names: list of additional output - tensors to include in the frozen graph. - write_inference_graph: If true, writes inference graph to disk. - use_side_inputs: If True, the model requires side_inputs. - side_input_shapes: List of shapes of the side input tensors, - required if use_side_inputs is True. - side_input_names: List of names of the side input tensors, - required if use_side_inputs is True. - side_input_types: List of types of the side input tensors, - required if use_side_inputs is True. - """ - detection_model = model_builder.build(pipeline_config.model, - is_training=False) - graph_rewriter_fn = None - if pipeline_config.HasField('graph_rewriter'): - graph_rewriter_config = pipeline_config.graph_rewriter - graph_rewriter_fn = graph_rewriter_builder.build(graph_rewriter_config, - is_training=False) - _export_inference_graph( - input_type, - detection_model, - pipeline_config.eval_config.use_moving_averages, - trained_checkpoint_prefix, - output_directory, - additional_output_tensor_names, - input_shape, - output_collection_name, - graph_hook_fn=graph_rewriter_fn, - write_inference_graph=write_inference_graph, - use_side_inputs=use_side_inputs, - side_input_shapes=side_input_shapes, - side_input_names=side_input_names, - side_input_types=side_input_types) - pipeline_config.eval_config.use_moving_averages = False - config_util.save_pipeline_config(pipeline_config, output_directory) - - -def profile_inference_graph(graph): - """Profiles the inference graph. - - Prints model parameters and computation FLOPs given an inference graph. - BatchNorms are excluded from the parameter count due to the fact that - BatchNorms are usually folded. BatchNorm, Initializer, Regularizer - and BiasAdd are not considered in FLOP count. - - Args: - graph: the inference graph. - """ - tfprof_vars_option = ( - contrib_tfprof.model_analyzer.TRAINABLE_VARS_PARAMS_STAT_OPTIONS) - tfprof_flops_option = contrib_tfprof.model_analyzer.FLOAT_OPS_OPTIONS - - # Batchnorm is usually folded during inference. - tfprof_vars_option['trim_name_regexes'] = ['.*BatchNorm.*'] - # Initializer and Regularizer are only used in training. - tfprof_flops_option['trim_name_regexes'] = [ - '.*BatchNorm.*', '.*Initializer.*', '.*Regularizer.*', '.*BiasAdd.*' - ] - - contrib_tfprof.model_analyzer.print_model_analysis( - graph, tfprof_options=tfprof_vars_option) - - contrib_tfprof.model_analyzer.print_model_analysis( - graph, tfprof_options=tfprof_flops_option) diff --git a/research/object_detection/exporter_lib_tf2_test.py b/research/object_detection/exporter_lib_tf2_test.py deleted file mode 100644 index 0f1eb6d8ad8..00000000000 --- a/research/object_detection/exporter_lib_tf2_test.py +++ /dev/null @@ -1,379 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Test for exporter_lib_v2.py.""" - -from __future__ import division -import io -import os -import unittest -from absl.testing import parameterized -import numpy as np -from PIL import Image -import six - -import tensorflow.compat.v2 as tf - -from object_detection import exporter_lib_v2 -from object_detection.builders import model_builder -from object_detection.core import model -from object_detection.core import standard_fields as fields -from object_detection.protos import pipeline_pb2 -from object_detection.utils import dataset_util -from object_detection.utils import tf_version - -if six.PY2: - import mock # pylint: disable=g-importing-member,g-import-not-at-top -else: - from unittest import mock # pylint: disable=g-importing-member,g-import-not-at-top - - -class FakeModel(model.DetectionModel): - - def __init__(self, conv_weight_scalar=1.0): - super(FakeModel, self).__init__(num_classes=2) - self._conv = tf.keras.layers.Conv2D( - filters=1, kernel_size=1, strides=(1, 1), padding='valid', - kernel_initializer=tf.keras.initializers.Constant( - value=conv_weight_scalar)) - - def preprocess(self, inputs): - return tf.identity(inputs), exporter_lib_v2.get_true_shapes(inputs) - - def predict(self, preprocessed_inputs, true_image_shapes, **side_inputs): - return_dict = {'image': self._conv(preprocessed_inputs)} - if 'side_inp_1' in side_inputs: - return_dict['image'] += side_inputs['side_inp_1'] - return return_dict - - def postprocess(self, prediction_dict, true_image_shapes): - predict_tensor_sum = tf.reduce_sum(prediction_dict['image']) - with tf.control_dependencies(list(prediction_dict.values())): - postprocessed_tensors = { - 'detection_boxes': tf.constant([[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]], tf.float32), - 'detection_scores': predict_tensor_sum + tf.constant( - [[0.7, 0.6], [0.9, 0.0]], tf.float32), - 'detection_classes': tf.constant([[0, 1], - [1, 0]], tf.float32), - 'num_detections': tf.constant([2, 1], tf.float32), - } - return postprocessed_tensors - - def predict_masks_from_boxes(self, prediction_dict, true_image_shapes, boxes): - output_dict = self.postprocess(prediction_dict, true_image_shapes) - output_dict.update({ - 'detection_masks': tf.ones(shape=(1, 2, 16), dtype=tf.float32), - }) - return output_dict - - def restore_map(self, checkpoint_path, fine_tune_checkpoint_type): - pass - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def loss(self, prediction_dict, true_image_shapes): - pass - - def regularization_losses(self): - pass - - def updates(self): - pass - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ExportInferenceGraphTest(tf.test.TestCase, parameterized.TestCase): - - def _save_checkpoint_from_mock_model( - self, checkpoint_dir, conv_weight_scalar=6.0): - mock_model = FakeModel(conv_weight_scalar) - fake_image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - preprocessed_inputs, true_image_shapes = mock_model.preprocess(fake_image) - predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) - mock_model.postprocess(predictions, true_image_shapes) - - ckpt = tf.train.Checkpoint(model=mock_model) - exported_checkpoint_manager = tf.train.CheckpointManager( - ckpt, checkpoint_dir, max_to_keep=1) - exported_checkpoint_manager.save(checkpoint_number=0) - - @parameterized.parameters( - {'input_type': 'image_tensor'}, - {'input_type': 'encoded_image_string_tensor'}, - {'input_type': 'tf_example'}, - ) - def test_export_yields_correct_directory_structure( - self, input_type='image_tensor'): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP['model_build'] = mock_builder - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter_lib_v2.export_inference_graph( - input_type=input_type, - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'saved_model', 'saved_model.pb'))) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'saved_model', 'variables', 'variables.index'))) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'saved_model', 'variables', - 'variables.data-00000-of-00001'))) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'checkpoint', 'ckpt-0.index'))) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'checkpoint', 'ckpt-0.data-00000-of-00001'))) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'pipeline.config'))) - - def get_dummy_input(self, input_type): - """Get dummy input for the given input type.""" - - if input_type == 'image_tensor': - return np.zeros((1, 20, 20, 3), dtype=np.uint8) - if input_type == 'float_image_tensor': - return np.zeros((1, 20, 20, 3), dtype=np.float32) - elif input_type == 'encoded_image_string_tensor': - image = Image.new('RGB', (20, 20)) - byte_io = io.BytesIO() - image.save(byte_io, 'PNG') - return [byte_io.getvalue()] - elif input_type == 'tf_example': - image_tensor = tf.zeros((20, 20, 3), dtype=tf.uint8) - encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).numpy() - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/source_id': - dataset_util.bytes_feature(six.b('image_id')), - })).SerializeToString() - return [example] - - @parameterized.parameters( - {'input_type': 'image_tensor'}, - {'input_type': 'encoded_image_string_tensor'}, - {'input_type': 'tf_example'}, - {'input_type': 'float_image_tensor'}, - ) - def test_export_saved_model_and_run_inference( - self, input_type='image_tensor'): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP['model_build'] = mock_builder - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter_lib_v2.export_inference_graph( - input_type=input_type, - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory) - - saved_model_path = os.path.join(output_directory, 'saved_model') - detect_fn = tf.saved_model.load(saved_model_path) - image = self.get_dummy_input(input_type) - detections = detect_fn(tf.constant(image)) - - detection_fields = fields.DetectionResultFields - self.assertAllClose(detections[detection_fields.detection_boxes], - [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(detections[detection_fields.detection_scores], - [[0.7, 0.6], [0.9, 0.0]]) - self.assertAllClose(detections[detection_fields.detection_classes], - [[1, 2], [2, 1]]) - self.assertAllClose(detections[detection_fields.num_detections], [2, 1]) - - @parameterized.parameters( - {'use_default_serving': True}, - {'use_default_serving': False} - ) - def test_export_saved_model_and_run_inference_with_side_inputs( - self, input_type='image_tensor', use_default_serving=True): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP['model_build'] = mock_builder - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter_lib_v2.export_inference_graph( - input_type=input_type, - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory, - use_side_inputs=True, - side_input_shapes='1/2,2', - side_input_names='side_inp_1,side_inp_2', - side_input_types='tf.float32,tf.uint8') - - saved_model_path = os.path.join(output_directory, 'saved_model') - detect_fn = tf.saved_model.load(saved_model_path) - detect_fn_sig = detect_fn.signatures['serving_default'] - image = tf.constant(self.get_dummy_input(input_type)) - side_input_1 = np.ones((1,), dtype=np.float32) - side_input_2 = np.ones((2, 2), dtype=np.uint8) - if use_default_serving: - detections = detect_fn_sig(input_tensor=image, - side_inp_1=tf.constant(side_input_1), - side_inp_2=tf.constant(side_input_2)) - else: - detections = detect_fn(image, - tf.constant(side_input_1), - tf.constant(side_input_2)) - - detection_fields = fields.DetectionResultFields - self.assertAllClose(detections[detection_fields.detection_boxes], - [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(detections[detection_fields.detection_scores], - [[400.7, 400.6], [400.9, 400.0]]) - self.assertAllClose(detections[detection_fields.detection_classes], - [[1, 2], [2, 1]]) - self.assertAllClose(detections[detection_fields.num_detections], [2, 1]) - - def test_export_checkpoint_and_run_inference_with_image(self): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir, conv_weight_scalar=2.0) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP['model_build'] = mock_builder - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter_lib_v2.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory) - - mock_model = FakeModel() - ckpt = tf.compat.v2.train.Checkpoint( - model=mock_model) - checkpoint_dir = os.path.join(tmp_dir, 'output', 'checkpoint') - manager = tf.compat.v2.train.CheckpointManager( - ckpt, checkpoint_dir, max_to_keep=7) - ckpt.restore(manager.latest_checkpoint).expect_partial() - - fake_image = tf.ones(shape=[1, 5, 5, 3], dtype=tf.float32) - preprocessed_inputs, true_image_shapes = mock_model.preprocess(fake_image) - predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) - detections = mock_model.postprocess(predictions, true_image_shapes) - - # 150 = conv_weight_scalar * height * width * channels = 2 * 5 * 5 * 3. - self.assertAllClose(detections['detection_scores'], - [[150 + 0.7, 150 + 0.6], [150 + 0.9, 150 + 0.0]]) - - -class DetectionFromImageAndBoxModuleTest(tf.test.TestCase): - - def get_dummy_input(self, input_type): - """Get dummy input for the given input type.""" - - if input_type == 'image_tensor' or input_type == 'image_and_boxes_tensor': - return np.zeros((1, 20, 20, 3), dtype=np.uint8) - if input_type == 'float_image_tensor': - return np.zeros((1, 20, 20, 3), dtype=np.float32) - elif input_type == 'encoded_image_string_tensor': - image = Image.new('RGB', (20, 20)) - byte_io = io.BytesIO() - image.save(byte_io, 'PNG') - return [byte_io.getvalue()] - elif input_type == 'tf_example': - image_tensor = tf.zeros((20, 20, 3), dtype=tf.uint8) - encoded_jpeg = tf.image.encode_jpeg(tf.constant(image_tensor)).numpy() - example = tf.train.Example( - features=tf.train.Features( - feature={ - 'image/encoded': - dataset_util.bytes_feature(encoded_jpeg), - 'image/format': - dataset_util.bytes_feature(six.b('jpeg')), - 'image/source_id': - dataset_util.bytes_feature(six.b('image_id')), - })).SerializeToString() - return [example] - - def _save_checkpoint_from_mock_model(self, - checkpoint_dir, - conv_weight_scalar=6.0): - mock_model = FakeModel(conv_weight_scalar) - fake_image = tf.zeros(shape=[1, 10, 10, 3], dtype=tf.float32) - preprocessed_inputs, true_image_shapes = mock_model.preprocess(fake_image) - predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) - mock_model.postprocess(predictions, true_image_shapes) - - ckpt = tf.train.Checkpoint(model=mock_model) - exported_checkpoint_manager = tf.train.CheckpointManager( - ckpt, checkpoint_dir, max_to_keep=1) - exported_checkpoint_manager.save(checkpoint_number=0) - - def test_export_saved_model_and_run_inference_for_segmentation( - self, input_type='image_and_boxes_tensor'): - tmp_dir = self.get_temp_dir() - self._save_checkpoint_from_mock_model(tmp_dir) - - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP['model_build'] = mock_builder - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter_lib_v2.export_inference_graph( - input_type=input_type, - pipeline_config=pipeline_config, - trained_checkpoint_dir=tmp_dir, - output_directory=output_directory) - - saved_model_path = os.path.join(output_directory, 'saved_model') - detect_fn = tf.saved_model.load(saved_model_path) - image = self.get_dummy_input(input_type) - boxes = tf.constant([ - [ - [0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8], - ], - ]) - detections = detect_fn(tf.constant(image), boxes) - - detection_fields = fields.DetectionResultFields - self.assertIn(detection_fields.detection_masks, detections) - self.assertListEqual( - list(detections[detection_fields.detection_masks].shape), [1, 2, 16]) - - -if __name__ == '__main__': - tf.enable_v2_behavior() - tf.test.main() diff --git a/research/object_detection/exporter_lib_v2.py b/research/object_detection/exporter_lib_v2.py deleted file mode 100644 index 626f021fdb2..00000000000 --- a/research/object_detection/exporter_lib_v2.py +++ /dev/null @@ -1,356 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Functions to export object detection inference graph.""" -import ast -import os - -import tensorflow.compat.v2 as tf -from object_detection.builders import model_builder -from object_detection.core import standard_fields as fields -from object_detection.data_decoders import tf_example_decoder -from object_detection.utils import config_util - - -INPUT_BUILDER_UTIL_MAP = { - 'model_build': model_builder.build, -} - - -def _decode_image(encoded_image_string_tensor): - image_tensor = tf.image.decode_image(encoded_image_string_tensor, - channels=3) - image_tensor.set_shape((None, None, 3)) - return image_tensor - - -def _decode_tf_example(tf_example_string_tensor): - tensor_dict = tf_example_decoder.TfExampleDecoder().decode( - tf_example_string_tensor) - image_tensor = tensor_dict[fields.InputDataFields.image] - return image_tensor - - -def _combine_side_inputs(side_input_shapes='', - side_input_types='', - side_input_names=''): - """Zips the side inputs together. - - Args: - side_input_shapes: forward-slash-separated list of comma-separated lists - describing input shapes. - side_input_types: comma-separated list of the types of the inputs. - side_input_names: comma-separated list of the names of the inputs. - - Returns: - a zipped list of side input tuples. - """ - side_input_shapes = [ - ast.literal_eval('[' + x + ']') for x in side_input_shapes.split('/') - ] - side_input_types = eval('[' + side_input_types + ']') # pylint: disable=eval-used - side_input_names = side_input_names.split(',') - return zip(side_input_shapes, side_input_types, side_input_names) - - -class DetectionInferenceModule(tf.Module): - """Detection Inference Module.""" - - def __init__(self, detection_model, - use_side_inputs=False, - zipped_side_inputs=None): - """Initializes a module for detection. - - Args: - detection_model: the detection model to use for inference. - use_side_inputs: whether to use side inputs. - zipped_side_inputs: the zipped side inputs. - """ - self._model = detection_model - - def _get_side_input_signature(self, zipped_side_inputs): - sig = [] - side_input_names = [] - for info in zipped_side_inputs: - sig.append(tf.TensorSpec(shape=info[0], - dtype=info[1], - name=info[2])) - side_input_names.append(info[2]) - return sig - - def _get_side_names_from_zip(self, zipped_side_inputs): - return [side[2] for side in zipped_side_inputs] - - def _preprocess_input(self, batch_input, decode_fn): - # Input preprocessing happends on the CPU. We don't need to use the device - # placement as it is automatically handled by TF. - def _decode_and_preprocess(single_input): - image = decode_fn(single_input) - image = tf.cast(image, tf.float32) - image, true_shape = self._model.preprocess(image[tf.newaxis, :, :, :]) - return image[0], true_shape[0] - - images, true_shapes = tf.map_fn( - _decode_and_preprocess, - elems=batch_input, - parallel_iterations=32, - back_prop=False, - fn_output_signature=(tf.float32, tf.int32)) - return images, true_shapes - - def _run_inference_on_images(self, images, true_shapes, **kwargs): - """Cast image to float and run inference. - - Args: - images: float32 Tensor of shape [None, None, None, 3]. - true_shapes: int32 Tensor of form [batch, 3] - **kwargs: additional keyword arguments. - - Returns: - Tensor dictionary holding detections. - """ - label_id_offset = 1 - prediction_dict = self._model.predict(images, true_shapes, **kwargs) - detections = self._model.postprocess(prediction_dict, true_shapes) - classes_field = fields.DetectionResultFields.detection_classes - detections[classes_field] = ( - tf.cast(detections[classes_field], tf.float32) + label_id_offset) - - for key, val in detections.items(): - detections[key] = tf.cast(val, tf.float32) - - return detections - - -class DetectionFromImageModule(DetectionInferenceModule): - """Detection Inference Module for image inputs.""" - - def __init__(self, detection_model, - use_side_inputs=False, - zipped_side_inputs=None): - """Initializes a module for detection. - - Args: - detection_model: the detection model to use for inference. - use_side_inputs: whether to use side inputs. - zipped_side_inputs: the zipped side inputs. - """ - if zipped_side_inputs is None: - zipped_side_inputs = [] - sig = [tf.TensorSpec(shape=[1, None, None, 3], - dtype=tf.uint8, - name='input_tensor')] - if use_side_inputs: - sig.extend(self._get_side_input_signature(zipped_side_inputs)) - self._side_input_names = self._get_side_names_from_zip(zipped_side_inputs) - - def call_func(input_tensor, *side_inputs): - kwargs = dict(zip(self._side_input_names, side_inputs)) - images, true_shapes = self._preprocess_input(input_tensor, lambda x: x) - return self._run_inference_on_images(images, true_shapes, **kwargs) - - self.__call__ = tf.function(call_func, input_signature=sig) - - # TODO(kaushikshiv): Check if omitting the signature also works. - super(DetectionFromImageModule, self).__init__(detection_model, - use_side_inputs, - zipped_side_inputs) - - -def get_true_shapes(input_tensor): - input_shape = tf.shape(input_tensor) - batch = input_shape[0] - image_shape = input_shape[1:] - true_shapes = tf.tile(image_shape[tf.newaxis, :], [batch, 1]) - return true_shapes - - -class DetectionFromFloatImageModule(DetectionInferenceModule): - """Detection Inference Module for float image inputs.""" - - @tf.function( - input_signature=[ - tf.TensorSpec(shape=[None, None, None, 3], dtype=tf.float32)]) - def __call__(self, input_tensor): - images, true_shapes = self._preprocess_input(input_tensor, lambda x: x) - return self._run_inference_on_images(images, - true_shapes) - - -class DetectionFromEncodedImageModule(DetectionInferenceModule): - """Detection Inference Module for encoded image string inputs.""" - - @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)]) - def __call__(self, input_tensor): - images, true_shapes = self._preprocess_input(input_tensor, _decode_image) - return self._run_inference_on_images(images, true_shapes) - - -class DetectionFromTFExampleModule(DetectionInferenceModule): - """Detection Inference Module for TF.Example inputs.""" - - @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)]) - def __call__(self, input_tensor): - images, true_shapes = self._preprocess_input(input_tensor, - _decode_tf_example) - return self._run_inference_on_images(images, true_shapes) - - -def export_inference_graph(input_type, - pipeline_config, - trained_checkpoint_dir, - output_directory, - use_side_inputs=False, - side_input_shapes='', - side_input_types='', - side_input_names=''): - """Exports inference graph for the model specified in the pipeline config. - - This function creates `output_directory` if it does not already exist, - which will hold a copy of the pipeline config with filename `pipeline.config`, - and two subdirectories named `checkpoint` and `saved_model` - (containing the exported checkpoint and SavedModel respectively). - - Args: - input_type: Type of input for the graph. Can be one of ['image_tensor', - 'encoded_image_string_tensor', 'tf_example']. - pipeline_config: pipeline_pb2.TrainAndEvalPipelineConfig proto. - trained_checkpoint_dir: Path to the trained checkpoint file. - output_directory: Path to write outputs. - use_side_inputs: boolean that determines whether side inputs should be - included in the input signature. - side_input_shapes: forward-slash-separated list of comma-separated lists - describing input shapes. - side_input_types: comma-separated list of the types of the inputs. - side_input_names: comma-separated list of the names of the inputs. - Raises: - ValueError: if input_type is invalid. - """ - output_checkpoint_directory = os.path.join(output_directory, 'checkpoint') - output_saved_model_directory = os.path.join(output_directory, 'saved_model') - - detection_model = INPUT_BUILDER_UTIL_MAP['model_build']( - pipeline_config.model, is_training=False) - - ckpt = tf.train.Checkpoint( - model=detection_model) - manager = tf.train.CheckpointManager( - ckpt, trained_checkpoint_dir, max_to_keep=1) - status = ckpt.restore(manager.latest_checkpoint).expect_partial() - - if input_type not in DETECTION_MODULE_MAP: - raise ValueError('Unrecognized `input_type`') - if use_side_inputs and input_type != 'image_tensor': - raise ValueError('Side inputs supported for image_tensor input type only.') - - zipped_side_inputs = [] - if use_side_inputs: - zipped_side_inputs = _combine_side_inputs(side_input_shapes, - side_input_types, - side_input_names) - - detection_module = DETECTION_MODULE_MAP[input_type](detection_model, - use_side_inputs, - list(zipped_side_inputs)) - # Getting the concrete function traces the graph and forces variables to - # be constructed --- only after this can we save the checkpoint and - # saved model. - concrete_function = detection_module.__call__.get_concrete_function() - status.assert_existing_objects_matched() - - exported_checkpoint_manager = tf.train.CheckpointManager( - ckpt, output_checkpoint_directory, max_to_keep=1) - exported_checkpoint_manager.save(checkpoint_number=0) - - tf.saved_model.save(detection_module, - output_saved_model_directory, - signatures=concrete_function) - - config_util.save_pipeline_config(pipeline_config, output_directory) - - -class DetectionFromImageAndBoxModule(DetectionInferenceModule): - """Detection Inference Module for image with bounding box inputs. - - The saved model will require two inputs (image and normalized boxes) and run - per-box mask prediction. To be compatible with this exporter, the detection - model has to implement a called predict_masks_from_boxes( - prediction_dict, true_image_shapes, provided_boxes, **params), where - - prediciton_dict is a dict returned by the predict method. - - true_image_shapes is a tensor of size [batch_size, 3], containing the - true shape of each image in case it is padded. - - provided_boxes is a [batch_size, num_boxes, 4] size tensor containing - boxes specified in normalized coordinates. - """ - - def __init__(self, - detection_model, - use_side_inputs=False, - zipped_side_inputs=None): - """Initializes a module for detection. - - Args: - detection_model: the detection model to use for inference. - use_side_inputs: whether to use side inputs. - zipped_side_inputs: the zipped side inputs. - """ - assert hasattr(detection_model, 'predict_masks_from_boxes') - super(DetectionFromImageAndBoxModule, - self).__init__(detection_model, use_side_inputs, zipped_side_inputs) - - def _run_segmentation_on_images(self, image, boxes, **kwargs): - """Run segmentation on images with provided boxes. - - Args: - image: uint8 Tensor of shape [1, None, None, 3]. - boxes: float32 tensor of shape [1, None, 4] containing normalized box - coordinates. - **kwargs: additional keyword arguments. - - Returns: - Tensor dictionary holding detections (including masks). - """ - label_id_offset = 1 - - image = tf.cast(image, tf.float32) - image, shapes = self._model.preprocess(image) - prediction_dict = self._model.predict(image, shapes, **kwargs) - detections = self._model.predict_masks_from_boxes(prediction_dict, shapes, - boxes) - classes_field = fields.DetectionResultFields.detection_classes - detections[classes_field] = ( - tf.cast(detections[classes_field], tf.float32) + label_id_offset) - - for key, val in detections.items(): - detections[key] = tf.cast(val, tf.float32) - - return detections - - @tf.function(input_signature=[ - tf.TensorSpec(shape=[1, None, None, 3], dtype=tf.uint8), - tf.TensorSpec(shape=[1, None, 4], dtype=tf.float32) - ]) - def __call__(self, input_tensor, boxes): - return self._run_segmentation_on_images(input_tensor, boxes) - - -DETECTION_MODULE_MAP = { - 'image_tensor': DetectionFromImageModule, - 'encoded_image_string_tensor': - DetectionFromEncodedImageModule, - 'tf_example': DetectionFromTFExampleModule, - 'float_image_tensor': DetectionFromFloatImageModule, - 'image_and_boxes_tensor': DetectionFromImageAndBoxModule, -} diff --git a/research/object_detection/exporter_main_v2.py b/research/object_detection/exporter_main_v2.py deleted file mode 100644 index 4f310513912..00000000000 --- a/research/object_detection/exporter_main_v2.py +++ /dev/null @@ -1,164 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Tool to export an object detection model for inference. - -Prepares an object detection tensorflow graph for inference using model -configuration and a trained checkpoint. Outputs associated checkpoint files, -a SavedModel, and a copy of the model config. - -The inference graph contains one of three input nodes depending on the user -specified option. - * `image_tensor`: Accepts a uint8 4-D tensor of shape [1, None, None, 3] - * `float_image_tensor`: Accepts a float32 4-D tensor of shape - [1, None, None, 3] - * `encoded_image_string_tensor`: Accepts a 1-D string tensor of shape [None] - containing encoded PNG or JPEG images. Image resolutions are expected to be - the same if more than 1 image is provided. - * `tf_example`: Accepts a 1-D string tensor of shape [None] containing - serialized TFExample protos. Image resolutions are expected to be the same - if more than 1 image is provided. - * `image_and_boxes_tensor`: Accepts a 4-D image tensor of size - [1, None, None, 3] and a boxes tensor of size [1, None, 4] of normalized - bounding boxes. To be able to support this option, the model needs - to implement a predict_masks_from_boxes method. See the documentation - for DetectionFromImageAndBoxModule for details. - -and the following output nodes returned by the model.postprocess(..): - * `num_detections`: Outputs float32 tensors of the form [batch] - that specifies the number of valid boxes per image in the batch. - * `detection_boxes`: Outputs float32 tensors of the form - [batch, num_boxes, 4] containing detected boxes. - * `detection_scores`: Outputs float32 tensors of the form - [batch, num_boxes] containing class scores for the detections. - * `detection_classes`: Outputs float32 tensors of the form - [batch, num_boxes] containing classes for the detections. - - -Example Usage: --------------- -python exporter_main_v2.py \ - --input_type image_tensor \ - --pipeline_config_path path/to/ssd_inception_v2.config \ - --trained_checkpoint_dir path/to/checkpoint \ - --output_directory path/to/exported_model_directory - --use_side_inputs True/False \ - --side_input_shapes dim_0,dim_1,...dim_a/.../dim_0,dim_1,...,dim_z \ - --side_input_names name_a,name_b,...,name_c \ - --side_input_types type_1,type_2 - -The expected output would be in the directory -path/to/exported_model_directory (which is created if it does not exist) -holding two subdirectories (corresponding to checkpoint and SavedModel, -respectively) and a copy of the pipeline config. - -Config overrides (see the `config_override` flag) are text protobufs -(also of type pipeline_pb2.TrainEvalPipelineConfig) which are used to override -certain fields in the provided pipeline_config_path. These are useful for -making small changes to the inference graph that differ from the training or -eval config. - -Example Usage (in which we change the second stage post-processing score -threshold to be 0.5): - -python exporter_main_v2.py \ - --input_type image_tensor \ - --pipeline_config_path path/to/ssd_inception_v2.config \ - --trained_checkpoint_dir path/to/checkpoint \ - --output_directory path/to/exported_model_directory \ - --config_override " \ - model{ \ - faster_rcnn { \ - second_stage_post_processing { \ - batch_non_max_suppression { \ - score_threshold: 0.5 \ - } \ - } \ - } \ - }" - -If side inputs are desired, the following arguments could be appended -(the example below is for Context R-CNN). - --use_side_inputs True \ - --side_input_shapes 1,2000,2057/1 \ - --side_input_names context_features,valid_context_size \ - --side_input_types tf.float32,tf.int32 -""" -from absl import app -from absl import flags - -import tensorflow.compat.v2 as tf -from google.protobuf import text_format -from object_detection import exporter_lib_v2 -from object_detection.protos import pipeline_pb2 - -tf.enable_v2_behavior() - - -FLAGS = flags.FLAGS - -flags.DEFINE_string('input_type', 'image_tensor', 'Type of input node. Can be ' - 'one of [`image_tensor`, `encoded_image_string_tensor`, ' - '`tf_example`, `float_image_tensor`, ' - '`image_and_boxes_tensor`]') -flags.DEFINE_string('pipeline_config_path', None, - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file.') -flags.DEFINE_string('trained_checkpoint_dir', None, - 'Path to trained checkpoint directory') -flags.DEFINE_string('output_directory', None, 'Path to write outputs.') -flags.DEFINE_string('config_override', '', - 'pipeline_pb2.TrainEvalPipelineConfig ' - 'text proto to override pipeline_config_path.') -flags.DEFINE_boolean('use_side_inputs', False, - 'If True, uses side inputs as well as image inputs.') -flags.DEFINE_string('side_input_shapes', '', - 'If use_side_inputs is True, this explicitly sets ' - 'the shape of the side input tensors to a fixed size. The ' - 'dimensions are to be provided as a comma-separated list ' - 'of integers. A value of -1 can be used for unknown ' - 'dimensions. A `/` denotes a break, starting the shape of ' - 'the next side input tensor. This flag is required if ' - 'using side inputs.') -flags.DEFINE_string('side_input_types', '', - 'If use_side_inputs is True, this explicitly sets ' - 'the type of the side input tensors. The ' - 'dimensions are to be provided as a comma-separated list ' - 'of types, each of `string`, `integer`, or `float`. ' - 'This flag is required if using side inputs.') -flags.DEFINE_string('side_input_names', '', - 'If use_side_inputs is True, this explicitly sets ' - 'the names of the side input tensors required by the model ' - 'assuming the names will be a comma-separated list of ' - 'strings. This flag is required if using side inputs.') - -flags.mark_flag_as_required('pipeline_config_path') -flags.mark_flag_as_required('trained_checkpoint_dir') -flags.mark_flag_as_required('output_directory') - - -def main(_): - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - with tf.io.gfile.GFile(FLAGS.pipeline_config_path, 'r') as f: - text_format.Merge(f.read(), pipeline_config) - text_format.Merge(FLAGS.config_override, pipeline_config) - exporter_lib_v2.export_inference_graph( - FLAGS.input_type, pipeline_config, FLAGS.trained_checkpoint_dir, - FLAGS.output_directory, FLAGS.use_side_inputs, FLAGS.side_input_shapes, - FLAGS.side_input_types, FLAGS.side_input_names) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/object_detection/exporter_tf1_test.py b/research/object_detection/exporter_tf1_test.py deleted file mode 100644 index c3810a2c778..00000000000 --- a/research/object_detection/exporter_tf1_test.py +++ /dev/null @@ -1,1209 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.export_inference_graph.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import os -import unittest -import numpy as np -import six -import tensorflow.compat.v1 as tf -from google.protobuf import text_format -from tensorflow.python.framework import dtypes -from tensorflow.python.ops import array_ops -from tensorflow.python.tools import strip_unused_lib -from object_detection import exporter -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import model_builder -from object_detection.core import model -from object_detection.protos import graph_rewriter_pb2 -from object_detection.protos import pipeline_pb2 -from object_detection.utils import ops -from object_detection.utils import tf_version -from object_detection.utils import variables_helper - -if six.PY2: - import mock # pylint: disable=g-import-not-at-top -else: - mock = unittest.mock # pylint: disable=g-import-not-at-top, g-importing-member - -# pylint: disable=g-import-not-at-top -try: - import tf_slim as slim -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - - -class FakeModel(model.DetectionModel): - - def __init__(self, add_detection_keypoints=False, add_detection_masks=False, - add_detection_features=False): - self._add_detection_keypoints = add_detection_keypoints - self._add_detection_masks = add_detection_masks - self._add_detection_features = add_detection_features - - def preprocess(self, inputs): - true_image_shapes = [] # Doesn't matter for the fake model. - return tf.identity(inputs), true_image_shapes - - def predict(self, preprocessed_inputs, true_image_shapes): - return {'image': tf.layers.conv2d(preprocessed_inputs, 3, 1)} - - def postprocess(self, prediction_dict, true_image_shapes): - with tf.control_dependencies(list(prediction_dict.values())): - postprocessed_tensors = { - 'detection_boxes': tf.constant([[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]], tf.float32), - 'detection_scores': tf.constant([[0.7, 0.6], - [0.9, 0.0]], tf.float32), - 'detection_multiclass_scores': tf.constant([[[0.3, 0.7], [0.4, 0.6]], - [[0.1, 0.9], [0.0, 0.0]]], - tf.float32), - 'detection_classes': tf.constant([[0, 1], - [1, 0]], tf.float32), - 'num_detections': tf.constant([2, 1], tf.float32), - 'raw_detection_boxes': tf.constant([[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.5, 0.0, 0.5]]], - tf.float32), - 'raw_detection_scores': tf.constant([[0.7, 0.6], - [0.9, 0.5]], tf.float32), - } - if self._add_detection_keypoints: - postprocessed_tensors['detection_keypoints'] = tf.constant( - np.arange(48).reshape([2, 2, 6, 2]), tf.float32) - if self._add_detection_masks: - postprocessed_tensors['detection_masks'] = tf.constant( - np.arange(64).reshape([2, 2, 4, 4]), tf.float32) - if self._add_detection_features: - # let fake detection features have shape [4, 4, 10] - postprocessed_tensors['detection_features'] = tf.constant( - np.ones((2, 2, 4, 4, 10)), tf.float32) - - return postprocessed_tensors - - def restore_map(self, checkpoint_path, fine_tune_checkpoint_type): - pass - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def loss(self, prediction_dict, true_image_shapes): - pass - - def regularization_losses(self): - pass - - def updates(self): - pass - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ExportInferenceGraphTest(tf.test.TestCase): - - def _save_checkpoint_from_mock_model(self, - checkpoint_path, - use_moving_averages, - enable_quantization=False): - g = tf.Graph() - with g.as_default(): - mock_model = FakeModel() - preprocessed_inputs, true_image_shapes = mock_model.preprocess( - tf.placeholder(tf.float32, shape=[None, None, None, 3])) - predictions = mock_model.predict(preprocessed_inputs, true_image_shapes) - mock_model.postprocess(predictions, true_image_shapes) - if use_moving_averages: - tf.train.ExponentialMovingAverage(0.0).apply() - tf.train.get_or_create_global_step() - if enable_quantization: - graph_rewriter_config = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_config.quantization.delay = 500000 - graph_rewriter_fn = graph_rewriter_builder.build( - graph_rewriter_config, is_training=False) - graph_rewriter_fn() - saver = tf.train.Saver() - init = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init) - saver.save(sess, checkpoint_path) - - def _load_inference_graph(self, inference_graph_path, is_binary=True): - od_graph = tf.Graph() - with od_graph.as_default(): - od_graph_def = tf.GraphDef() - with tf.gfile.GFile(inference_graph_path, mode='rb') as fid: - if is_binary: - od_graph_def.ParseFromString(fid.read()) - else: - text_format.Parse(fid.read(), od_graph_def) - tf.import_graph_def(od_graph_def, name='') - return od_graph - - def _create_tf_example(self, image_array): - with self.test_session(): - encoded_image = tf.image.encode_jpeg(tf.constant(image_array)).eval() - def _bytes_feature(value): - return tf.train.Feature( - bytes_list=tf.train.BytesList(value=[six.ensure_binary(value)])) - - example = tf.train.Example(features=tf.train.Features(feature={ - 'image/encoded': _bytes_feature(encoded_image), - 'image/format': _bytes_feature('jpg'), - 'image/source_id': _bytes_feature('image_id') - })).SerializeToString() - return example - - def test_export_graph_with_image_tensor_input(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'saved_model', 'saved_model.pb'))) - - def test_write_inference_graph(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory, - write_inference_graph=True) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'inference_graph.pbtxt'))) - - def test_export_graph_with_fixed_size_image_tensor_input(self): - input_shape = [1, 320, 320, 3] - - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model( - trained_checkpoint_prefix, use_moving_averages=False) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory, - input_shape=input_shape) - saved_model_path = os.path.join(output_directory, 'saved_model') - self.assertTrue( - os.path.exists(os.path.join(saved_model_path, 'saved_model.pb'))) - - with tf.Graph().as_default() as od_graph: - with self.test_session(graph=od_graph) as sess: - meta_graph = tf.saved_model.loader.load( - sess, [tf.saved_model.tag_constants.SERVING], saved_model_path) - signature = meta_graph.signature_def['serving_default'] - input_tensor_name = signature.inputs['inputs'].name - image_tensor = od_graph.get_tensor_by_name(input_tensor_name) - self.assertSequenceEqual(image_tensor.get_shape().as_list(), - input_shape) - - def test_export_graph_with_tf_example_input(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='tf_example', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'saved_model', 'saved_model.pb'))) - - def test_export_graph_with_fixed_size_tf_example_input(self): - input_shape = [1, 320, 320, 3] - - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model( - trained_checkpoint_prefix, use_moving_averages=False) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='tf_example', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory, - input_shape=input_shape) - saved_model_path = os.path.join(output_directory, 'saved_model') - self.assertTrue( - os.path.exists(os.path.join(saved_model_path, 'saved_model.pb'))) - - def test_export_graph_with_encoded_image_string_input(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='encoded_image_string_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'saved_model', 'saved_model.pb'))) - - def test_export_graph_with_fixed_size_encoded_image_string_input(self): - input_shape = [1, 320, 320, 3] - - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model( - trained_checkpoint_prefix, use_moving_averages=False) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - output_directory = os.path.join(tmp_dir, 'output') - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='encoded_image_string_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory, - input_shape=input_shape) - saved_model_path = os.path.join(output_directory, 'saved_model') - self.assertTrue( - os.path.exists(os.path.join(saved_model_path, 'saved_model.pb'))) - - def _get_variables_in_checkpoint(self, checkpoint_file): - return set([ - var_name - for var_name, _ in tf.train.list_variables(checkpoint_file)]) - - def test_replace_variable_values_with_moving_averages(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - new_checkpoint_prefix = os.path.join(tmp_dir, 'new.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - graph = tf.Graph() - with graph.as_default(): - fake_model = FakeModel() - preprocessed_inputs, true_image_shapes = fake_model.preprocess( - tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3])) - predictions = fake_model.predict(preprocessed_inputs, true_image_shapes) - fake_model.postprocess(predictions, true_image_shapes) - exporter.replace_variable_values_with_moving_averages( - graph, trained_checkpoint_prefix, new_checkpoint_prefix) - - expected_variables = set(['conv2d/bias', 'conv2d/kernel']) - variables_in_old_ckpt = self._get_variables_in_checkpoint( - trained_checkpoint_prefix) - self.assertIn('conv2d/bias/ExponentialMovingAverage', - variables_in_old_ckpt) - self.assertIn('conv2d/kernel/ExponentialMovingAverage', - variables_in_old_ckpt) - variables_in_new_ckpt = self._get_variables_in_checkpoint( - new_checkpoint_prefix) - self.assertTrue(expected_variables.issubset(variables_in_new_ckpt)) - self.assertNotIn('conv2d/bias/ExponentialMovingAverage', - variables_in_new_ckpt) - self.assertNotIn('conv2d/kernel/ExponentialMovingAverage', - variables_in_new_ckpt) - - def test_export_graph_with_moving_averages(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = True - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - self.assertTrue(os.path.exists(os.path.join( - output_directory, 'saved_model', 'saved_model.pb'))) - expected_variables = set(['conv2d/bias', 'conv2d/kernel', 'global_step']) - actual_variables = set( - [var_name for var_name, _ in tf.train.list_variables(output_directory)]) - self.assertTrue(expected_variables.issubset(actual_variables)) - - def test_export_model_with_quantization_nodes(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model( - trained_checkpoint_prefix, - use_moving_averages=False, - enable_quantization=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'inference_graph.pbtxt') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - text_format.Merge( - """graph_rewriter { - quantization { - delay: 50000 - activation_bits: 8 - weight_bits: 8 - } - }""", pipeline_config) - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory, - write_inference_graph=True) - self._load_inference_graph(inference_graph_path, is_binary=False) - has_quant_nodes = False - for v in variables_helper.get_global_variables_safely(): - if six.ensure_str(v.op.name).endswith('act_quant/min'): - has_quant_nodes = True - break - self.assertTrue(has_quant_nodes) - - def test_export_model_with_all_output_nodes(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True, - add_detection_features=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - inference_graph = self._load_inference_graph(inference_graph_path) - with self.test_session(graph=inference_graph): - inference_graph.get_tensor_by_name('image_tensor:0') - inference_graph.get_tensor_by_name('detection_boxes:0') - inference_graph.get_tensor_by_name('detection_scores:0') - inference_graph.get_tensor_by_name('detection_multiclass_scores:0') - inference_graph.get_tensor_by_name('detection_classes:0') - inference_graph.get_tensor_by_name('detection_keypoints:0') - inference_graph.get_tensor_by_name('detection_masks:0') - inference_graph.get_tensor_by_name('num_detections:0') - inference_graph.get_tensor_by_name('detection_features:0') - - def test_export_model_with_detection_only_nodes(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel(add_detection_masks=False) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - inference_graph = self._load_inference_graph(inference_graph_path) - with self.test_session(graph=inference_graph): - inference_graph.get_tensor_by_name('image_tensor:0') - inference_graph.get_tensor_by_name('detection_boxes:0') - inference_graph.get_tensor_by_name('detection_scores:0') - inference_graph.get_tensor_by_name('detection_multiclass_scores:0') - inference_graph.get_tensor_by_name('detection_classes:0') - inference_graph.get_tensor_by_name('num_detections:0') - with self.assertRaises(KeyError): - inference_graph.get_tensor_by_name('detection_keypoints:0') - inference_graph.get_tensor_by_name('detection_masks:0') - - def test_export_model_with_detection_only_nodes_and_detection_features(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel(add_detection_features=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - inference_graph = self._load_inference_graph(inference_graph_path) - with self.test_session(graph=inference_graph): - inference_graph.get_tensor_by_name('image_tensor:0') - inference_graph.get_tensor_by_name('detection_boxes:0') - inference_graph.get_tensor_by_name('detection_scores:0') - inference_graph.get_tensor_by_name('detection_multiclass_scores:0') - inference_graph.get_tensor_by_name('detection_classes:0') - inference_graph.get_tensor_by_name('num_detections:0') - inference_graph.get_tensor_by_name('detection_features:0') - with self.assertRaises(KeyError): - inference_graph.get_tensor_by_name('detection_keypoints:0') - inference_graph.get_tensor_by_name('detection_masks:0') - - def test_export_and_run_inference_with_image_tensor(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - - inference_graph = self._load_inference_graph(inference_graph_path) - with self.test_session(graph=inference_graph) as sess: - image_tensor = inference_graph.get_tensor_by_name('image_tensor:0') - boxes = inference_graph.get_tensor_by_name('detection_boxes:0') - scores = inference_graph.get_tensor_by_name('detection_scores:0') - classes = inference_graph.get_tensor_by_name('detection_classes:0') - keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') - masks = inference_graph.get_tensor_by_name('detection_masks:0') - num_detections = inference_graph.get_tensor_by_name('num_detections:0') - (boxes_np, scores_np, classes_np, keypoints_np, masks_np, - num_detections_np) = sess.run( - [boxes, scores, classes, keypoints, masks, num_detections], - feed_dict={image_tensor: np.ones((2, 4, 4, 3)).astype(np.uint8)}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def _create_encoded_image_string(self, image_array_np, encoding_format): - od_graph = tf.Graph() - with od_graph.as_default(): - if encoding_format == 'jpg': - encoded_string = tf.image.encode_jpeg(image_array_np) - elif encoding_format == 'png': - encoded_string = tf.image.encode_png(image_array_np) - else: - raise ValueError('Supports only the following formats: `jpg`, `png`') - with self.test_session(graph=od_graph): - return encoded_string.eval() - - def test_export_and_run_inference_with_encoded_image_string_tensor(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='encoded_image_string_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - - inference_graph = self._load_inference_graph(inference_graph_path) - jpg_image_str = self._create_encoded_image_string( - np.ones((4, 4, 3)).astype(np.uint8), 'jpg') - png_image_str = self._create_encoded_image_string( - np.ones((4, 4, 3)).astype(np.uint8), 'png') - with self.test_session(graph=inference_graph) as sess: - image_str_tensor = inference_graph.get_tensor_by_name( - 'encoded_image_string_tensor:0') - boxes = inference_graph.get_tensor_by_name('detection_boxes:0') - scores = inference_graph.get_tensor_by_name('detection_scores:0') - multiclass_scores = inference_graph.get_tensor_by_name( - 'detection_multiclass_scores:0') - classes = inference_graph.get_tensor_by_name('detection_classes:0') - keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') - masks = inference_graph.get_tensor_by_name('detection_masks:0') - num_detections = inference_graph.get_tensor_by_name('num_detections:0') - for image_str in [jpg_image_str, png_image_str]: - image_str_batch_np = np.hstack([image_str]* 2) - (boxes_np, scores_np, multiclass_scores_np, classes_np, keypoints_np, - masks_np, num_detections_np) = sess.run( - [ - boxes, scores, multiclass_scores, classes, keypoints, masks, - num_detections - ], - feed_dict={image_str_tensor: image_str_batch_np}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(multiclass_scores_np, [[[0.3, 0.7], [0.4, 0.6]], - [[0.1, 0.9], [0.0, 0.0]]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def test_raise_runtime_error_on_images_with_different_sizes(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='encoded_image_string_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - - inference_graph = self._load_inference_graph(inference_graph_path) - large_image = self._create_encoded_image_string( - np.ones((4, 4, 3)).astype(np.uint8), 'jpg') - small_image = self._create_encoded_image_string( - np.ones((2, 2, 3)).astype(np.uint8), 'jpg') - - image_str_batch_np = np.hstack([large_image, small_image]) - with self.test_session(graph=inference_graph) as sess: - image_str_tensor = inference_graph.get_tensor_by_name( - 'encoded_image_string_tensor:0') - boxes = inference_graph.get_tensor_by_name('detection_boxes:0') - scores = inference_graph.get_tensor_by_name('detection_scores:0') - classes = inference_graph.get_tensor_by_name('detection_classes:0') - keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') - masks = inference_graph.get_tensor_by_name('detection_masks:0') - num_detections = inference_graph.get_tensor_by_name('num_detections:0') - with self.assertRaisesRegexp(tf.errors.InvalidArgumentError, - 'TensorArray.*shape'): - sess.run( - [boxes, scores, classes, keypoints, masks, num_detections], - feed_dict={image_str_tensor: image_str_batch_np}) - - def test_export_and_run_inference_with_tf_example(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='tf_example', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - - inference_graph = self._load_inference_graph(inference_graph_path) - tf_example_np = np.expand_dims(self._create_tf_example( - np.ones((4, 4, 3)).astype(np.uint8)), axis=0) - with self.test_session(graph=inference_graph) as sess: - tf_example = inference_graph.get_tensor_by_name('tf_example:0') - boxes = inference_graph.get_tensor_by_name('detection_boxes:0') - scores = inference_graph.get_tensor_by_name('detection_scores:0') - classes = inference_graph.get_tensor_by_name('detection_classes:0') - keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') - masks = inference_graph.get_tensor_by_name('detection_masks:0') - num_detections = inference_graph.get_tensor_by_name('num_detections:0') - (boxes_np, scores_np, classes_np, keypoints_np, masks_np, - num_detections_np) = sess.run( - [boxes, scores, classes, keypoints, masks, num_detections], - feed_dict={tf_example: tf_example_np}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def test_write_frozen_graph(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - inference_graph_path = os.path.join(output_directory, - 'frozen_inference_graph.pb') - tf.gfile.MakeDirs(output_directory) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - detection_model = model_builder.build(pipeline_config.model, - is_training=False) - outputs, _ = exporter.build_detection_graph( - input_type='tf_example', - detection_model=detection_model, - input_shape=None, - output_collection_name='inference_op', - graph_hook_fn=None) - output_node_names = ','.join(list(outputs.keys())) - saver = tf.train.Saver() - input_saver_def = saver.as_saver_def() - exporter.freeze_graph_with_def_protos( - input_graph_def=tf.get_default_graph().as_graph_def(), - input_saver_def=input_saver_def, - input_checkpoint=trained_checkpoint_prefix, - output_node_names=output_node_names, - restore_op_name='save/restore_all', - filename_tensor_name='save/Const:0', - output_graph=inference_graph_path, - clear_devices=True, - initializer_nodes='') - - inference_graph = self._load_inference_graph(inference_graph_path) - tf_example_np = np.expand_dims(self._create_tf_example( - np.ones((4, 4, 3)).astype(np.uint8)), axis=0) - with self.test_session(graph=inference_graph) as sess: - tf_example = inference_graph.get_tensor_by_name('tf_example:0') - boxes = inference_graph.get_tensor_by_name('detection_boxes:0') - scores = inference_graph.get_tensor_by_name('detection_scores:0') - classes = inference_graph.get_tensor_by_name('detection_classes:0') - keypoints = inference_graph.get_tensor_by_name('detection_keypoints:0') - masks = inference_graph.get_tensor_by_name('detection_masks:0') - num_detections = inference_graph.get_tensor_by_name('num_detections:0') - (boxes_np, scores_np, classes_np, keypoints_np, masks_np, - num_detections_np) = sess.run( - [boxes, scores, classes, keypoints, masks, num_detections], - feed_dict={tf_example: tf_example_np}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def test_export_graph_saves_pipeline_file(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=True) - output_directory = os.path.join(tmp_dir, 'output') - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel() - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - exporter.export_inference_graph( - input_type='image_tensor', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - expected_pipeline_path = os.path.join( - output_directory, 'pipeline.config') - self.assertTrue(os.path.exists(expected_pipeline_path)) - - written_pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - with tf.gfile.GFile(expected_pipeline_path, 'r') as f: - proto_str = f.read() - text_format.Merge(proto_str, written_pipeline_config) - self.assertProtoEquals(pipeline_config, written_pipeline_config) - - def test_export_saved_model_and_run_inference(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - output_directory = os.path.join(tmp_dir, 'output') - saved_model_path = os.path.join(output_directory, 'saved_model') - - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='tf_example', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - - tf_example_np = np.hstack([self._create_tf_example( - np.ones((4, 4, 3)).astype(np.uint8))] * 2) - with tf.Graph().as_default() as od_graph: - with self.test_session(graph=od_graph) as sess: - meta_graph = tf.saved_model.loader.load( - sess, [tf.saved_model.tag_constants.SERVING], saved_model_path) - - signature = meta_graph.signature_def['serving_default'] - input_tensor_name = signature.inputs['inputs'].name - tf_example = od_graph.get_tensor_by_name(input_tensor_name) - - boxes = od_graph.get_tensor_by_name( - signature.outputs['detection_boxes'].name) - scores = od_graph.get_tensor_by_name( - signature.outputs['detection_scores'].name) - multiclass_scores = od_graph.get_tensor_by_name( - signature.outputs['detection_multiclass_scores'].name) - classes = od_graph.get_tensor_by_name( - signature.outputs['detection_classes'].name) - keypoints = od_graph.get_tensor_by_name( - signature.outputs['detection_keypoints'].name) - masks = od_graph.get_tensor_by_name( - signature.outputs['detection_masks'].name) - num_detections = od_graph.get_tensor_by_name( - signature.outputs['num_detections'].name) - - (boxes_np, scores_np, multiclass_scores_np, classes_np, keypoints_np, - masks_np, num_detections_np) = sess.run( - [boxes, scores, multiclass_scores, classes, keypoints, masks, - num_detections], - feed_dict={tf_example: tf_example_np}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(multiclass_scores_np, [[[0.3, 0.7], [0.4, 0.6]], - [[0.1, 0.9], [0.0, 0.0]]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def test_write_saved_model(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - output_directory = os.path.join(tmp_dir, 'output') - saved_model_path = os.path.join(output_directory, 'saved_model') - tf.gfile.MakeDirs(output_directory) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - detection_model = model_builder.build(pipeline_config.model, - is_training=False) - outputs, placeholder_tensor = exporter.build_detection_graph( - input_type='tf_example', - detection_model=detection_model, - input_shape=None, - output_collection_name='inference_op', - graph_hook_fn=None) - output_node_names = ','.join(list(outputs.keys())) - saver = tf.train.Saver() - input_saver_def = saver.as_saver_def() - frozen_graph_def = exporter.freeze_graph_with_def_protos( - input_graph_def=tf.get_default_graph().as_graph_def(), - input_saver_def=input_saver_def, - input_checkpoint=trained_checkpoint_prefix, - output_node_names=output_node_names, - restore_op_name='save/restore_all', - filename_tensor_name='save/Const:0', - output_graph='', - clear_devices=True, - initializer_nodes='') - exporter.write_saved_model( - saved_model_path=saved_model_path, - frozen_graph_def=frozen_graph_def, - inputs=placeholder_tensor, - outputs=outputs) - - tf_example_np = np.hstack([self._create_tf_example( - np.ones((4, 4, 3)).astype(np.uint8))] * 2) - with tf.Graph().as_default() as od_graph: - with self.test_session(graph=od_graph) as sess: - meta_graph = tf.saved_model.loader.load( - sess, [tf.saved_model.tag_constants.SERVING], saved_model_path) - - signature = meta_graph.signature_def['serving_default'] - input_tensor_name = signature.inputs['inputs'].name - tf_example = od_graph.get_tensor_by_name(input_tensor_name) - - boxes = od_graph.get_tensor_by_name( - signature.outputs['detection_boxes'].name) - scores = od_graph.get_tensor_by_name( - signature.outputs['detection_scores'].name) - classes = od_graph.get_tensor_by_name( - signature.outputs['detection_classes'].name) - keypoints = od_graph.get_tensor_by_name( - signature.outputs['detection_keypoints'].name) - masks = od_graph.get_tensor_by_name( - signature.outputs['detection_masks'].name) - num_detections = od_graph.get_tensor_by_name( - signature.outputs['num_detections'].name) - - (boxes_np, scores_np, classes_np, keypoints_np, masks_np, - num_detections_np) = sess.run( - [boxes, scores, classes, keypoints, masks, num_detections], - feed_dict={tf_example: tf_example_np}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def test_export_checkpoint_and_run_inference(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - output_directory = os.path.join(tmp_dir, 'output') - model_path = os.path.join(output_directory, 'model.ckpt') - meta_graph_path = model_path + '.meta' - - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - exporter.export_inference_graph( - input_type='tf_example', - pipeline_config=pipeline_config, - trained_checkpoint_prefix=trained_checkpoint_prefix, - output_directory=output_directory) - - tf_example_np = np.hstack([self._create_tf_example( - np.ones((4, 4, 3)).astype(np.uint8))] * 2) - with tf.Graph().as_default() as od_graph: - with self.test_session(graph=od_graph) as sess: - new_saver = tf.train.import_meta_graph(meta_graph_path) - new_saver.restore(sess, model_path) - - tf_example = od_graph.get_tensor_by_name('tf_example:0') - boxes = od_graph.get_tensor_by_name('detection_boxes:0') - scores = od_graph.get_tensor_by_name('detection_scores:0') - classes = od_graph.get_tensor_by_name('detection_classes:0') - keypoints = od_graph.get_tensor_by_name('detection_keypoints:0') - masks = od_graph.get_tensor_by_name('detection_masks:0') - num_detections = od_graph.get_tensor_by_name('num_detections:0') - (boxes_np, scores_np, classes_np, keypoints_np, masks_np, - num_detections_np) = sess.run( - [boxes, scores, classes, keypoints, masks, num_detections], - feed_dict={tf_example: tf_example_np}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def test_write_graph_and_checkpoint(self): - tmp_dir = self.get_temp_dir() - trained_checkpoint_prefix = os.path.join(tmp_dir, 'model.ckpt') - self._save_checkpoint_from_mock_model(trained_checkpoint_prefix, - use_moving_averages=False) - output_directory = os.path.join(tmp_dir, 'output') - model_path = os.path.join(output_directory, 'model.ckpt') - meta_graph_path = model_path + '.meta' - tf.gfile.MakeDirs(output_directory) - with mock.patch.object( - model_builder, 'build', autospec=True) as mock_builder: - mock_builder.return_value = FakeModel( - add_detection_keypoints=True, add_detection_masks=True) - pipeline_config = pipeline_pb2.TrainEvalPipelineConfig() - pipeline_config.eval_config.use_moving_averages = False - detection_model = model_builder.build(pipeline_config.model, - is_training=False) - exporter.build_detection_graph( - input_type='tf_example', - detection_model=detection_model, - input_shape=None, - output_collection_name='inference_op', - graph_hook_fn=None) - saver = tf.train.Saver() - input_saver_def = saver.as_saver_def() - exporter.write_graph_and_checkpoint( - inference_graph_def=tf.get_default_graph().as_graph_def(), - model_path=model_path, - input_saver_def=input_saver_def, - trained_checkpoint_prefix=trained_checkpoint_prefix) - - tf_example_np = np.hstack([self._create_tf_example( - np.ones((4, 4, 3)).astype(np.uint8))] * 2) - with tf.Graph().as_default() as od_graph: - with self.test_session(graph=od_graph) as sess: - new_saver = tf.train.import_meta_graph(meta_graph_path) - new_saver.restore(sess, model_path) - - tf_example = od_graph.get_tensor_by_name('tf_example:0') - boxes = od_graph.get_tensor_by_name('detection_boxes:0') - scores = od_graph.get_tensor_by_name('detection_scores:0') - raw_boxes = od_graph.get_tensor_by_name('raw_detection_boxes:0') - raw_scores = od_graph.get_tensor_by_name('raw_detection_scores:0') - classes = od_graph.get_tensor_by_name('detection_classes:0') - keypoints = od_graph.get_tensor_by_name('detection_keypoints:0') - masks = od_graph.get_tensor_by_name('detection_masks:0') - num_detections = od_graph.get_tensor_by_name('num_detections:0') - (boxes_np, scores_np, raw_boxes_np, raw_scores_np, classes_np, - keypoints_np, masks_np, num_detections_np) = sess.run( - [boxes, scores, raw_boxes, raw_scores, classes, keypoints, masks, - num_detections], - feed_dict={tf_example: tf_example_np}) - self.assertAllClose(boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.0, 0.0, 0.0]]]) - self.assertAllClose(scores_np, [[0.7, 0.6], - [0.9, 0.0]]) - self.assertAllClose(raw_boxes_np, [[[0.0, 0.0, 0.5, 0.5], - [0.5, 0.5, 0.8, 0.8]], - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.5, 0.0, 0.5]]]) - self.assertAllClose(raw_scores_np, [[0.7, 0.6], - [0.9, 0.5]]) - self.assertAllClose(classes_np, [[1, 2], - [2, 1]]) - self.assertAllClose(keypoints_np, np.arange(48).reshape([2, 2, 6, 2])) - self.assertAllClose(masks_np, np.arange(64).reshape([2, 2, 4, 4])) - self.assertAllClose(num_detections_np, [2, 1]) - - def test_rewrite_nn_resize_op(self): - g = tf.Graph() - with g.as_default(): - x = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) - y = array_ops.placeholder(dtypes.float32, shape=(8, 20, 20, 8)) - s = ops.nearest_neighbor_upsampling(x, 2) - t = s + y - exporter.rewrite_nn_resize_op() - - resize_op_found = False - for op in g.get_operations(): - if op.type == 'ResizeNearestNeighbor': - resize_op_found = True - self.assertEqual(op.inputs[0], x) - self.assertEqual(op.outputs[0].consumers()[0], t.op) - break - - self.assertTrue(resize_op_found) - - def test_rewrite_nn_resize_op_quantized(self): - g = tf.Graph() - with g.as_default(): - x = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) - x_conv = slim.conv2d(x, 8, 1) - y = array_ops.placeholder(dtypes.float32, shape=(8, 20, 20, 8)) - s = ops.nearest_neighbor_upsampling(x_conv, 2) - t = s + y - - graph_rewriter_config = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_config.quantization.delay = 500000 - graph_rewriter_fn = graph_rewriter_builder.build( - graph_rewriter_config, is_training=False) - graph_rewriter_fn() - - exporter.rewrite_nn_resize_op(is_quantized=True) - - resize_op_found = False - for op in g.get_operations(): - if op.type == 'ResizeNearestNeighbor': - resize_op_found = True - self.assertEqual(op.inputs[0].op.type, 'FakeQuantWithMinMaxVars') - self.assertEqual(op.outputs[0].consumers()[0], t.op) - break - - self.assertTrue(resize_op_found) - - def test_rewrite_nn_resize_op_odd_size(self): - g = tf.Graph() - with g.as_default(): - x = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) - s = ops.nearest_neighbor_upsampling(x, 2) - t = s[:, :19, :19, :] - exporter.rewrite_nn_resize_op() - - resize_op_found = False - for op in g.get_operations(): - if op.type == 'ResizeNearestNeighbor': - resize_op_found = True - self.assertEqual(op.inputs[0], x) - self.assertEqual(op.outputs[0].consumers()[0], t.op) - break - - self.assertTrue(resize_op_found) - - def test_rewrite_nn_resize_op_quantized_odd_size(self): - g = tf.Graph() - with g.as_default(): - x = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) - x_conv = slim.conv2d(x, 8, 1) - s = ops.nearest_neighbor_upsampling(x_conv, 2) - t = s[:, :19, :19, :] - - graph_rewriter_config = graph_rewriter_pb2.GraphRewriter() - graph_rewriter_config.quantization.delay = 500000 - graph_rewriter_fn = graph_rewriter_builder.build( - graph_rewriter_config, is_training=False) - graph_rewriter_fn() - - exporter.rewrite_nn_resize_op(is_quantized=True) - - resize_op_found = False - for op in g.get_operations(): - if op.type == 'ResizeNearestNeighbor': - resize_op_found = True - self.assertEqual(op.inputs[0].op.type, 'FakeQuantWithMinMaxVars') - self.assertEqual(op.outputs[0].consumers()[0], t.op) - break - - self.assertTrue(resize_op_found) - - def test_rewrite_nn_resize_op_multiple_path(self): - g = tf.Graph() - with g.as_default(): - with tf.name_scope('nearest_upsampling'): - x_1 = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) - x_1_stack_1 = tf.stack([x_1] * 2, axis=3) - x_1_reshape_1 = tf.reshape(x_1_stack_1, [8, 10, 20, 8]) - x_1_stack_2 = tf.stack([x_1_reshape_1] * 2, axis=2) - x_1_reshape_2 = tf.reshape(x_1_stack_2, [8, 20, 20, 8]) - - with tf.name_scope('nearest_upsampling'): - x_2 = array_ops.placeholder(dtypes.float32, shape=(8, 10, 10, 8)) - x_2_stack_1 = tf.stack([x_2] * 2, axis=3) - x_2_reshape_1 = tf.reshape(x_2_stack_1, [8, 10, 20, 8]) - x_2_stack_2 = tf.stack([x_2_reshape_1] * 2, axis=2) - x_2_reshape_2 = tf.reshape(x_2_stack_2, [8, 20, 20, 8]) - - t = x_1_reshape_2 + x_2_reshape_2 - - exporter.rewrite_nn_resize_op() - - graph_def = g.as_graph_def() - graph_def = strip_unused_lib.strip_unused( - graph_def, - input_node_names=[ - 'nearest_upsampling/Placeholder', 'nearest_upsampling_1/Placeholder' - ], - output_node_names=['add'], - placeholder_type_enum=dtypes.float32.as_datatype_enum) - - counter_resize_op = 0 - t_input_ops = [op.name for op in t.op.inputs] - for node in graph_def.node: - # Make sure Stacks are replaced. - self.assertNotEqual(node.op, 'Pack') - if node.op == 'ResizeNearestNeighbor': - counter_resize_op += 1 - self.assertIn(six.ensure_str(node.name) + ':0', t_input_ops) - self.assertEqual(counter_resize_op, 2) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/g3doc/challenge_evaluation.md b/research/object_detection/g3doc/challenge_evaluation.md deleted file mode 100644 index 15f032d4e8a..00000000000 --- a/research/object_detection/g3doc/challenge_evaluation.md +++ /dev/null @@ -1,215 +0,0 @@ -# Open Images Challenge Evaluation - -The Object Detection API is currently supporting several evaluation metrics used -in the -[Open Images Challenge 2018](https://storage.googleapis.com/openimages/web/challenge.html) -and -[Open Images Challenge 2019](https://storage.googleapis.com/openimages/web/challenge2019.html). -In addition, several data processing tools are available. Detailed instructions -on using the tools for each track are available below. - -**NOTE:** all data links are updated to the Open Images Challenge 2019. - -## Object Detection Track - -The -[Object Detection metric](https://storage.googleapis.com/openimages/web/evaluation.html#object_detection_eval) -protocol requires a pre-processing of the released data to ensure correct -evaluation. The released data contains only leaf-most bounding box annotations -and image-level labels. The evaluation metric implementation is available in the -class `OpenImagesChallengeEvaluator`. - -1. Download - [class hierarchy of Open Images Detection Challenge 2019](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-label500-hierarchy.json) - in JSON format. -2. Download - [ground-truth boundling boxes](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-validation-detection-bbox.csv) - and - [image-level labels](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-validation-detection-human-imagelabels.csv). -3. Run the following command to create hierarchical expansion of the bounding - boxes and image-level label annotations: - -``` -HIERARCHY_FILE=/path/to/challenge-2019-label500-hierarchy.json -BOUNDING_BOXES=/path/to/challenge-2019-validation-detection-bbox -IMAGE_LABELS=/path/to/challenge-2019-validation-detection-human-imagelabels - -python object_detection/dataset_tools/oid_hierarchical_labels_expansion.py \ - --json_hierarchy_file=${HIERARCHY_FILE} \ - --input_annotations=${BOUNDING_BOXES}.csv \ - --output_annotations=${BOUNDING_BOXES}_expanded.csv \ - --annotation_type=1 - -python object_detection/dataset_tools/oid_hierarchical_labels_expansion.py \ - --json_hierarchy_file=${HIERARCHY_FILE} \ - --input_annotations=${IMAGE_LABELS}.csv \ - --output_annotations=${IMAGE_LABELS}_expanded.csv \ - --annotation_type=2 -``` - -1. If you are not using TensorFlow, you can run evaluation directly using your - algorithm's output and generated ground-truth files. {value=4} - -After step 3 you produced the ground-truth files suitable for running 'OID -Challenge Object Detection Metric 2019' evaluation. To run the evaluation, use -the following command: - -``` -INPUT_PREDICTIONS=/path/to/detection_predictions.csv -OUTPUT_METRICS=/path/to/output/metrics/file - -python models/research/object_detection/metrics/oid_challenge_evaluation.py \ - --input_annotations_boxes=${BOUNDING_BOXES}_expanded.csv \ - --input_annotations_labels=${IMAGE_LABELS}_expanded.csv \ - --input_class_labelmap=object_detection/data/oid_object_detection_challenge_500_label_map.pbtxt \ - --input_predictions=${INPUT_PREDICTIONS} \ - --output_metrics=${OUTPUT_METRICS} \ -``` - -Note that predictions file must contain the following keys: -ImageID,LabelName,Score,XMin,XMax,YMin,YMax - -For the Object Detection Track, the participants will be ranked on: - -- "OpenImagesDetectionChallenge_Precision/mAP@0.5IOU" - -To use evaluation within TensorFlow training, use metric name -`oid_challenge_detection_metrics` in the evaluation config. - -## Instance Segmentation Track - -The -[Instance Segmentation metric](https://storage.googleapis.com/openimages/web/evaluation.html#instance_segmentation_eval) -can be directly evaluated using the ground-truth data and model predictions. The -evaluation metric implementation is available in the class -`OpenImagesChallengeEvaluator`. - -1. Download - [class hierarchy of Open Images Instance Segmentation Challenge 2019](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-label300-segmentable-hierarchy.json) - in JSON format. -2. Download - [ground-truth bounding boxes](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-validation-segmentation-bbox.csv) - and - [image-level labels](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-validation-segmentation-labels.csv). -3. Download instance segmentation files for the validation set (see - [Open Images Challenge Downloads page](https://storage.googleapis.com/openimages/web/challenge2019_downloads.html)). - The download consists of a set of .zip archives containing binary .png - masks. - Those should be transformed into a single CSV file in the format: - - ImageID,LabelName,ImageWidth,ImageHeight,XMin,YMin,XMax,YMax,IsGroupOf,Mask - where Mask is MS COCO RLE encoding, compressed with zip, and re-coded with - base64 encoding of a binary mask stored in .png file. See an example - implementation of the encoding function - [here](https://gist.github.com/pculliton/209398a2a52867580c6103e25e55d93c). - -1. Run the following command to create hierarchical expansion of the instance - segmentation, bounding boxes and image-level label annotations: {value=4} - -``` -HIERARCHY_FILE=/path/to/challenge-2019-label300-hierarchy.json -BOUNDING_BOXES=/path/to/challenge-2019-validation-detection-bbox -IMAGE_LABELS=/path/to/challenge-2019-validation-detection-human-imagelabels - -python object_detection/dataset_tools/oid_hierarchical_labels_expansion.py \ - --json_hierarchy_file=${HIERARCHY_FILE} \ - --input_annotations=${BOUNDING_BOXES}.csv \ - --output_annotations=${BOUNDING_BOXES}_expanded.csv \ - --annotation_type=1 - -python object_detection/dataset_tools/oid_hierarchical_labels_expansion.py \ - --json_hierarchy_file=${HIERARCHY_FILE} \ - --input_annotations=${IMAGE_LABELS}.csv \ - --output_annotations=${IMAGE_LABELS}_expanded.csv \ - --annotation_type=2 - -python object_detection/dataset_tools/oid_hierarchical_labels_expansion.py \ - --json_hierarchy_file=${HIERARCHY_FILE} \ - --input_annotations=${INSTANCE_SEGMENTATIONS}.csv \ - --output_annotations=${INSTANCE_SEGMENTATIONS}_expanded.csv \ - --annotation_type=1 -``` - -1. If you are not using TensorFlow, you can run evaluation directly using your - algorithm's output and generated ground-truth files. {value=4} - -``` -INPUT_PREDICTIONS=/path/to/instance_segmentation_predictions.csv -OUTPUT_METRICS=/path/to/output/metrics/file - -python models/research/object_detection/metrics/oid_challenge_evaluation.py \ - --input_annotations_boxes=${BOUNDING_BOXES}_expanded.csv \ - --input_annotations_labels=${IMAGE_LABELS}_expanded.csv \ - --input_class_labelmap=object_detection/data/oid_object_detection_challenge_500_label_map.pbtxt \ - --input_predictions=${INPUT_PREDICTIONS} \ - --input_annotations_segm=${INSTANCE_SEGMENTATIONS}_expanded.csv - --output_metrics=${OUTPUT_METRICS} \ -``` - -Note that predictions file must contain the following keys: -ImageID,ImageWidth,ImageHeight,LabelName,Score,Mask - -Mask must be encoded the same way as groundtruth masks. - -For the Instance Segmentation Track, the participants will be ranked on: - -- "OpenImagesInstanceSegmentationChallenge_Precision/mAP@0.5IOU" - -## Visual Relationships Detection Track - -The -[Visual Relationships Detection metrics](https://storage.googleapis.com/openimages/web/evaluation.html#visual_relationships_eval) -can be directly evaluated using the ground-truth data and model predictions. The -evaluation metric implementation is available in the class -`VRDRelationDetectionEvaluator`,`VRDPhraseDetectionEvaluator`. - -1. Download the ground-truth - [visual relationships annotations](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-validation-vrd.csv) - and - [image-level labels](https://storage.googleapis.com/openimages/challenge_2019/challenge-2019-validation-vrd-labels.csv). -2. Run the follwing command to produce final metrics: - -``` -INPUT_ANNOTATIONS_BOXES=/path/to/challenge-2018-train-vrd.csv -INPUT_ANNOTATIONS_LABELS=/path/to/challenge-2018-train-vrd-labels.csv -INPUT_PREDICTIONS=/path/to/predictions.csv -INPUT_CLASS_LABELMAP=/path/to/oid_object_detection_challenge_500_label_map.pbtxt -INPUT_RELATIONSHIP_LABELMAP=/path/to/relationships_labelmap.pbtxt -OUTPUT_METRICS=/path/to/output/metrics/file - -echo "item { name: '/m/02gy9n' id: 602 display_name: 'Transparent' } -item { name: '/m/05z87' id: 603 display_name: 'Plastic' } -item { name: '/m/0dnr7' id: 604 display_name: '(made of)Textile' } -item { name: '/m/04lbp' id: 605 display_name: '(made of)Leather' } -item { name: '/m/083vt' id: 606 display_name: 'Wooden'} -">>${INPUT_CLASS_LABELMAP} - -echo "item { name: 'at' id: 1 display_name: 'at' } -item { name: 'on' id: 2 display_name: 'on (top of)' } -item { name: 'holds' id: 3 display_name: 'holds' } -item { name: 'plays' id: 4 display_name: 'plays' } -item { name: 'interacts_with' id: 5 display_name: 'interacts with' } -item { name: 'wears' id: 6 display_name: 'wears' } -item { name: 'is' id: 7 display_name: 'is' } -item { name: 'inside_of' id: 8 display_name: 'inside of' } -item { name: 'under' id: 9 display_name: 'under' } -item { name: 'hits' id: 10 display_name: 'hits' } -"> ${INPUT_RELATIONSHIP_LABELMAP} - -python object_detection/metrics/oid_vrd_challenge_evaluation.py \ - --input_annotations_boxes=${INPUT_ANNOTATIONS_BOXES} \ - --input_annotations_labels=${INPUT_ANNOTATIONS_LABELS} \ - --input_predictions=${INPUT_PREDICTIONS} \ - --input_class_labelmap=${INPUT_CLASS_LABELMAP} \ - --input_relationship_labelmap=${INPUT_RELATIONSHIP_LABELMAP} \ - --output_metrics=${OUTPUT_METRICS} -``` - -Note that predictions file must contain the following keys: -ImageID,LabelName1,LabelName2,RelationshipLabel,Score,XMin1,XMax1,YMin1,YMax1,XMin2,XMax2,YMin2,YMax2 - -The participants of the challenge will be evaluated by weighted average of the following three metrics: - -- "VRDMetric_Relationships_mAP@0.5IOU" -- "VRDMetric_Relationships_Recall@50@0.5IOU" -- "VRDMetric_Phrases_mAP@0.5IOU" diff --git a/research/object_detection/g3doc/configuring_jobs.md b/research/object_detection/g3doc/configuring_jobs.md deleted file mode 100644 index 28af44fc3ec..00000000000 --- a/research/object_detection/g3doc/configuring_jobs.md +++ /dev/null @@ -1,258 +0,0 @@ -# Configuring the Object Detection Training Pipeline - -## Overview - -The TensorFlow Object Detection API uses protobuf files to configure the -training and evaluation process. The schema for the training pipeline can be -found in object_detection/protos/pipeline.proto. At a high level, the config -file is split into 5 parts: - -1. The `model` configuration. This defines what type of model will be trained -(ie. meta-architecture, feature extractor). -2. The `train_config`, which decides what parameters should be used to train -model parameters (ie. SGD parameters, input preprocessing and feature extractor -initialization values). -3. The `eval_config`, which determines what set of metrics will be reported for -evaluation. -4. The `train_input_config`, which defines what dataset the model should be -trained on. -5. The `eval_input_config`, which defines what dataset the model will be -evaluated on. Typically this should be different than the training input -dataset. - -A skeleton configuration file is shown below: - -``` -model { - (... Add model config here...) -} - -train_config : { - (... Add train_config here...) -} - -train_input_reader: { - (... Add train_input configuration here...) -} - -eval_config: { - (... Add eval_config here...) -} - -eval_input_reader: { - (... Add eval_input configuration here...) -} -``` - -## Picking Model Parameters - -There are a large number of model parameters to configure. The best settings -will depend on your given application. Faster R-CNN models are better suited to -cases where high accuracy is desired and latency is of lower priority. -Conversely, if processing time is the most important factor, SSD models are -recommended. Read [our paper](https://arxiv.org/abs/1611.10012) for a more -detailed discussion on the speed vs accuracy tradeoff. - -To help new users get started, sample model configurations have been provided -in the object_detection/samples/configs folder. The contents of these -configuration files can be pasted into `model` field of the skeleton -configuration. Users should note that the `num_classes` field should be changed -to a value suited for the dataset the user is training on. - -### Anchor box parameters - -Many object detection models use an anchor generator as a region-sampling -strategy, which generates a large number of anchor boxes in a range of shapes -and sizes, in many locations of the image. The detection algorithm then -incrementally offsets the anchor box closest to the ground truth until it -(closely) matches. You can specify the variety of and position of these anchor -boxes in the `anchor_generator` config. - -Usually, the anchor configs provided with pre-trained checkpoints are -designed for large/versatile datasets (COCO, ImageNet), in which the goal is to -improve accuracy for a wide range of object sizes and positions. But in most -real-world applications, objects are confined to a limited number of sizes. So -adjusting the anchors to be specific to your dataset and environment -can both improve model accuracy and reduce training time. - -The format for these anchor box parameters differ depending on your model -architecture. For details about all fields, see the [`anchor_generator` -definition](https://github.com/tensorflow/models/blob/master/research/object_detection/protos/anchor_generator.proto). -On this page, we'll focus on parameters -used in a traditional single shot detector (SSD) model and SSD models with a -feature pyramid network (FPN) head. - -Regardless of the model architecture, you'll need to understand the following -anchor box concepts: - - + **Scale**: This defines the variety of anchor box sizes. Each box size is - defined as a proportion of the original image size (for SSD models) or as a - factor of the filter's stride length (for FPN). The number of different sizes - is defined using a range of "scales" (relative to image size) or "levels" (the - level on the feature pyramid). For example, to detect small objects with the - configurations below, the `min_scale` and `min_level` are set to a small - value, while `max_scale` and `max_level` specify the largest objects to - detect. - - + **Aspect ratio**: This is the height/width ratio for the anchor boxes. For - example, the `aspect_ratio` value of `1.0` creates a square, and `2.0` creates - a 1:2 rectangle (landscape orientation). You can define as many aspects as you - want and each one is repeated at all anchor box scales. - -Beware that increasing the total number of anchor boxes will exponentially -increase computation costs. Whereas generating fewer anchors that have a higher -chance to overlap with ground truth will both improve accuracy and reduce -computation costs. - -And although you can manually select values for both scale and aspect ratios -that work well for your dataset, there are programmatic techniques you can use -instead. One such strategy to determine the ideal aspect ratios is to perform -k-means clustering of all the ground-truth bounding-box ratios, as shown in this -Colab notebook to [Generate SSD anchor box aspect ratios using k-means -clustering](https://colab.sandbox.google.com/github/tensorflow/models/blob/master/research/object_detection/colab_tutorials/generate_ssd_anchor_box_aspect_ratios_using_k_means_clustering.ipynb). - - -**Single Shot Detector (SSD) full model:** - -Setting `num_layers` to 6 means the model generates each box aspect at 6 -different sizes. The exact sizes are not specified but they're evenly spaced out -between the `min_scale` and `max_scale` values, which specify the smallest box -size is 20% of the input image size and the largest is 95% that size. - -``` -model { - ssd { - anchor_generator { - ssd_anchor_generator { - num_layers: 6 - min_scale: 0.2 - max_scale: 0.95 - aspect_ratios: 1.0 - aspect_ratios: 2.0 - aspect_ratios: 0.5 - } - } - } -} -``` - -For more details, see [`ssd_anchor_generator.proto`](https://github.com/tensorflow/models/blob/master/research/object_detection/protos/ssd_anchor_generator.proto). - -**SSD with Feature Pyramid Network (FPN) head:** - -When using an FPN head, you must specify the anchor box size relative to the -convolutional filter's stride length at a given pyramid level, using -`anchor_scale`. So in this example, the box size is 4.0 multiplied by the -layer's stride length. The number of sizes you get for each aspect simply -depends on how many levels there are between the `min_level` and `max_level`. - -``` -model { - ssd { - anchor_generator { - multiscale_anchor_generator { - anchor_scale: 4.0 - min_level: 3 - max_level: 7 - aspect_ratios: 1.0 - aspect_ratios: 2.0 - aspect_ratios: 0.5 - } - } - } -} -``` - -For more details, see [`multiscale_anchor_generator.proto`](https://github.com/tensorflow/models/blob/master/research/object_detection/protos/multiscale_anchor_generator.proto). - - -## Defining Inputs - -The TensorFlow Object Detection API accepts inputs in the TFRecord file format. -Users must specify the locations of both the training and evaluation files. -Additionally, users should also specify a label map, which define the mapping -between a class id and class name. The label map should be identical between -training and evaluation datasets. - -An example training input configuration looks as follows: - -``` -train_input_reader: { - tf_record_input_reader { - input_path: "/usr/home/username/data/train.record-?????-of-00010" - } - label_map_path: "/usr/home/username/data/label_map.pbtxt" -} -``` - -The `eval_input_reader` follows the same format. Users should substitute the -`input_path` and `label_map_path` arguments. Note that the paths can also point -to Google Cloud Storage buckets (ie. "gs://project_bucket/train.record") to -pull datasets hosted on Google Cloud. - -## Configuring the Trainer - -The `train_config` defines parts of the training process: - -1. Model parameter initialization. -2. Input preprocessing. -3. SGD parameters. - -A sample `train_config` is below: - -``` -train_config: { - batch_size: 1 - optimizer { - momentum_optimizer: { - learning_rate: { - manual_step_learning_rate { - initial_learning_rate: 0.0002 - schedule { - step: 0 - learning_rate: .0002 - } - schedule { - step: 900000 - learning_rate: .00002 - } - schedule { - step: 1200000 - learning_rate: .000002 - } - } - } - momentum_optimizer_value: 0.9 - } - use_moving_average: false - } - fine_tune_checkpoint: "/usr/home/username/tmp/model.ckpt-#####" - from_detection_checkpoint: true - load_all_detection_checkpoint_vars: true - gradient_clipping_by_norm: 10.0 - data_augmentation_options { - random_horizontal_flip { - } - } -} -``` - -### Input Preprocessing - -The `data_augmentation_options` in `train_config` can be used to specify -how training data can be modified. This field is optional. - -### SGD Parameters - -The remainings parameters in `train_config` are hyperparameters for gradient -descent. Please note that the optimal learning rates provided in these -configuration files may depend on the specifics of the training setup (e.g. -number of workers, gpu type). - -## Configuring the Evaluator - -The main components to set in `eval_config` are `num_examples` and -`metrics_set`. The parameter `num_examples` indicates the number of batches ( -currently of batch size 1) used for an evaluation cycle, and often is the total -size of the evaluation dataset. The parameter `metrics_set` indicates which -metrics to run during evaluation (i.e. `"coco_detection_metrics"`). diff --git a/research/object_detection/g3doc/context_rcnn.md b/research/object_detection/g3doc/context_rcnn.md deleted file mode 100644 index 14a42d89afe..00000000000 --- a/research/object_detection/g3doc/context_rcnn.md +++ /dev/null @@ -1,201 +0,0 @@ -# Context R-CNN - -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -Context R-CNN is an object detection model that uses contextual features to -improve object detection. See https://arxiv.org/abs/1912.03538 for more details. - -## Table of Contents - -* [Preparing Context Data for Context R-CNN](#preparing-context-data-for-context-r-cnn) - + [Generating TfRecords from a set of images and a COCO-CameraTraps style - JSON](#generating-tfrecords-from-a-set-of-images-and-a-coco-cameratraps-style-json) - + [Generating weakly-supervised bounding box labels for image-labeled data](#generating-weakly-supervised-bounding-box-labels-for-image-labeled-data) - + [Generating and saving contextual features for each image](#generating-and-saving-contextual-features-for-each-image) - + [Building up contextual memory banks and storing them for each context - group](#building-up-contextual-memory-banks-and-storing-them-for-each-context-group) -- [Training a Context R-CNN Model](#training-a-context-r-cnn-model) -- [Exporting a Context R-CNN Model](#exporting-a-context-r-cnn-model) - -## Preparing Context Data for Context R-CNN - -In this section, we will walk through the process of generating TfRecords with -contextual features. We focus on building context from object-centric features -generated with a pre-trained Faster R-CNN model, but you can adapt the provided -code to use alternative feature extractors. - -Each of these data processing scripts uses Apache Beam, which can be installed -using - -``` -pip install apache-beam -``` - -and can be run locally, or on a cluster for efficient processing of large -amounts of data. Note that generate_detection_data.py and -generate_embedding_data.py both involve running inference, and may be very slow -to run locally. See the -[Apache Beam documentation](https://beam.apache.org/documentation/runners/dataflow/) -for more information, and Google Cloud Documentation for a tutorial on -[running Beam jobs on DataFlow](https://cloud.google.com/dataflow/docs/quickstarts/quickstart-python). - -### Generating TfRecords from a set of images and a COCO-CameraTraps style JSON - -If your data is already stored in TfRecords, you can skip this first step. - -We assume a COCO-CameraTraps json format, as described on -[LILA.science](https://github.com/microsoft/CameraTraps/blob/master/data_management/README.md). - -COCO-CameraTraps is a format that adds static-camera-specific fields, such as a -location ID and datetime, to the well-established COCO format. To generate -appropriate context later on, be sure you have specified each contextual group -with a different location ID, which in the static camera case would be the ID of -the camera, as well as the datetime each photo was taken. We assume that empty -images will be labeled 'empty' with class id 0. - -To generate TfRecords from your database and local image folder, run - -``` -python object_detection/dataset_tools/context_rcnn/create_cococameratraps_tfexample_main.py \ - --alsologtostderr \ - --output_tfrecord_prefix="/path/to/output/tfrecord/location/prefix" \ - --image_directory="/path/to/image/folder/" \ - --input_annotations_file="path/to/annotations.json" -``` - -### Generating weakly-supervised bounding box labels for image-labeled data - -If all your data already has bounding box labels you can skip this step. - -Many camera trap datasets do not have bounding box labels, or only have bounding -box labels for some of the data. We have provided code to add bounding boxes -from a pretrained model (such as the -[Microsoft AI for Earth MegaDetector](https://github.com/microsoft/CameraTraps/blob/master/megadetector.md)) -and match the boxes to the image-level class label. - -To export your pretrained detection model, run - -``` -python object_detection/export_inference_graph.py \ - --alsologtostderr \ - --input_type tf_example \ - --pipeline_config_path path/to/faster_rcnn_model.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory -``` - -To add bounding boxes to your dataset using the above model, run - -``` -python object_detection/dataset_tools/context_rcnn/generate_detection_data.py \ - --alsologtostderr \ - --input_tfrecord path/to/input_tfrecord@X \ - --output_tfrecord path/to/output_tfrecord@X \ - --model_dir path/to/exported_model_directory/saved_model -``` - -If an image already has bounding box labels, those labels are left unchanged. If -an image is labeled 'empty' (class ID 0), we will not generate boxes for that -image. - -### Generating and saving contextual features for each image - -We next extract and store features for each image from a pretrained model. This -model can be the same model as above, or be a class-specific detection model -trained on data from your classes of interest. - -To export your pretrained detection model, run - -``` -python object_detection/export_inference_graph.py \ - --alsologtostderr \ - --input_type tf_example \ - --pipeline_config_path path/to/pipeline.config \ - --trained_checkpoint_prefix path/to/model.ckpt \ - --output_directory path/to/exported_model_directory \ - --additional_output_tensor_names detection_features -``` - -Make sure that you have set `output_final_box_features: true` within -your config file before exporting. This is needed to export the features as an -output, but it does not need to be set during training. - -To generate and save contextual features for your data, run - -``` -python object_detection/dataset_tools/context_rcnn/generate_embedding_data.py \ - --alsologtostderr \ - --embedding_input_tfrecord path/to/input_tfrecords* \ - --embedding_output_tfrecord path/to/output_tfrecords \ - --embedding_model_dir path/to/exported_model_directory/saved_model -``` - -### Building up contextual memory banks and storing them for each context group - -To build the context features you just added for each image into memory banks, -run - -``` -python object_detection/dataset_tools/context_rcnn/add_context_to_examples.py \ - --input_tfrecord path/to/input_tfrecords* \ - --output_tfrecord path/to/output_tfrecords \ - --sequence_key image/location \ - --time_horizon month -``` - -where the input_tfrecords for add_context_to_examples.py are the -output_tfrecords from generate_embedding_data.py. - -For all options, see add_context_to_examples.py. By default, this code builds -TfSequenceExamples, which are more data efficient (this allows you to store the -context features once for each context group, as opposed to once per image). If -you would like to export TfExamples instead, set flag `--output_type -tf_example`. - -If you use TfSequenceExamples, you must be sure to set `input_type: -TF_SEQUENCE_EXAMPLE` within your Context R-CNN configs for both -train_input_reader and test_input_reader. See -`object_detection/test_data/context_rcnn_camera_trap.config` -for an example. - -## Training a Context R-CNN Model - -To train a Context R-CNN model, you must first set up your config file. See -`test_data/context_rcnn_camera_trap.config` for an example. The important -difference between this config and a Faster R-CNN config is the inclusion of a -`context_config` within the model, which defines the necessary Context R-CNN -parameters. - -``` -context_config { - max_num_context_features: 2000 - context_feature_length: 2057 - } -``` - -Once your config file has been updated with your local paths, you can follow -along with documentation for running [locally](running_locally.md), or -[on the cloud](running_on_cloud.md). - -## Exporting a Context R-CNN Model - -Since Context R-CNN takes context features as well as images as input, we have -to explicitly define the other inputs ("side_inputs") to the model when -exporting, as below. This example is shown with default context feature shapes. - -``` -python export_inference_graph.py \ - --input_type image_tensor \ - --input_shape 1,-1,-1,3 \ - --pipeline_config_path /path/to/context_rcnn_model/pipeline.config \ - --trained_checkpoint_prefix /path/to/context_rcnn_model/model.ckpt \ - --output_directory /path/to/output_directory \ - --use_side_inputs True \ - --side_input_shapes 1,2000,2057/1 \ - --side_input_names context_features,valid_context_size \ - --side_input_types float,int - -``` - -If you have questions about Context R-CNN, please contact -[Sara Beery](https://beerys.github.io/). diff --git a/research/object_detection/g3doc/deepmac.md b/research/object_detection/g3doc/deepmac.md deleted file mode 100644 index 98a47bcfaf4..00000000000 --- a/research/object_detection/g3doc/deepmac.md +++ /dev/null @@ -1,100 +0,0 @@ -# DeepMAC model - - - -**DeepMAC** (Deep Mask heads Above CenterNet) is a neural network architecture -that is designed for the partially supervised instance segmentation task. For -details see the -[The surprising impact of mask-head architecture on novel class segmentation](https://arxiv.org/abs/2104.00613) -paper. The figure below shows improved mask predictions for unseen classes as we -use better mask-head architectures. - -

- -

- -Just by using better mask-head architectures (no extra losses or modules) we -achieve state-of-the-art performance in the partially supervised instance -segmentation task. - -## Code structure - -* `deepmac_meta_arch.py` implements our main architecture, DeepMAC, on top of - the CenterNet detection architecture. -* The proto message `DeepMACMaskEstimation` in `center_net.proto` controls the - configuration of the mask head used. -* The field `allowed_masked_classes_ids` controls which classes recieve mask - supervision during training. -* Mask R-CNN based ablations in the paper are implemented in the - [TF model garden](../../../official/projects/deepmac_maskrcnn) - code base. - -## Prerequisites - -1. Follow [TF2 install instructions](tf2.md) to install Object Detection API. -2. Generate COCO dataset by using - [create_coco_tf_record.py](../../../official/vision/data/create_coco_tf_record.py) - -## Configurations - -We provide pre-defined configs which can be run as a -[TF2 training pipeline](tf2_training_and_evaluation.md). Each of these -configurations needs to be passed as the `pipeline_config_path` argument to the -`object_detection/model_main_tf2.py` binary. Note that the `512x512` resolution -models require a TPU `v3-32` and the `1024x1024` resolution models require a TPU -`v3-128` to train. The configs can be found in the [configs/tf2](../configs/tf2) -directory. In the table below `X->Y` indicates that we train with masks from `X` -and evaluate with masks from `Y`. Performance is measured on the `coco-val2017` -set. - -### Partially supervised models - -Resolution | Mask head | Train->Eval | Config name | Mask mAP -:--------- | :------------ | :------------- | :------------------------------------------------- | -------: -512x512 | Hourglass-52 | VOC -> Non-VOC | `center_net_deepmac_512x512_voc_only.config` | 32.5 -1024x1024 | Hourglass-100 | VOC -> Non-VOC | `center_net_deepmac_1024x1024_voc_only.config` | 35.5 -1024x1024 | Hourglass-100 | Non-VOC -> VOC | `center_net_deepmac_1024x1024_non_voc_only.config` | 39.1 - -### Fully supervised models - -Here we report the Mask mAP averaged over all COCO classes on the `test-dev2017` -set . - -Resolution | Mask head | Config name | Mask mAP -:--------- | :------------ | :----------------------------------------- | -------: -1024x1024 | Hourglass-100 | `center_net_deepmac_1024x1024_coco.config` | 39.4 - -## Demos - -* [DeepMAC Colab](../colab_tutorials/deepmac_colab.ipynb) lets you run a - pre-trained DeepMAC model on user-specified boxes. Note that you are not - restricted to COCO classes! -* [iWildCam Notebook](https://www.kaggle.com/vighneshbgoogle/iwildcam-visualize-instance-masks) - to visualize instance masks generated by DeepMAC on the iWildCam dataset. - -## Pre-trained models on COCO -Both these models take Image + boxes as input and produce per-box instance -masks as output. - -* [CenterNet Hourglass backbone](http://download.tensorflow.org/models/object_detection/tf2/20210329/deepmac_1024x1024_coco17.tar.gz) -* [Mask-RCNN SpineNet backbone](https://storage.googleapis.com/tf_model_garden/vision/deepmac_maskrcnn/deepmarc_spinenet.zip) - - -## See also - -* [Mask RCNN code](https://github.com/tensorflow/models/tree/master/official/vision/beta/projects/deepmac_maskrcnn) - in TF Model garden code base. -* Project website - [git.io/deepmac](https://git.io/deepmac) - -## Citation - -``` -@misc{birodkar2021surprising, - title={The surprising impact of mask-head architecture on novel class segmentation}, - author={Vighnesh Birodkar and Zhichao Lu and Siyang Li and Vivek Rathod and Jonathan Huang}, - year={2021}, - eprint={2104.00613}, - archivePrefix={arXiv}, - primaryClass={cs.CV} -} -``` diff --git a/research/object_detection/g3doc/defining_your_own_model.md b/research/object_detection/g3doc/defining_your_own_model.md deleted file mode 100644 index dabc0649f6e..00000000000 --- a/research/object_detection/g3doc/defining_your_own_model.md +++ /dev/null @@ -1,137 +0,0 @@ -# So you want to create a new model! - -In this section, we discuss some of the abstractions that we use -for defining detection models. If you would like to define a new model -architecture for detection and use it in the TensorFlow Detection API, -then this section should also serve as a high level guide to the files that you -will need to edit to get your new model working. - -## DetectionModels (`object_detection/core/model.py`) - -In order to be trained, evaluated, and exported for serving using our -provided binaries, all models under the TensorFlow Object Detection API must -implement the `DetectionModel` interface (see the full definition in `object_detection/core/model.py`). In particular, -each of these models are responsible for implementing 5 functions: - -* `preprocess`: Run any preprocessing (e.g., scaling/shifting/reshaping) of - input values that is necessary prior to running the detector on an input - image. -* `predict`: Produce “raw” prediction tensors that can be passed to loss or - postprocess functions. -* `postprocess`: Convert predicted output tensors to final detections. -* `loss`: Compute scalar loss tensors with respect to provided groundtruth. -* `restore`: Load a checkpoint into the TensorFlow graph. - -Given a `DetectionModel` at training time, we pass each image batch through -the following sequence of functions to compute a loss which can be optimized via -SGD: - -``` -inputs (images tensor) -> preprocess -> predict -> loss -> outputs (loss tensor) -``` - -And at eval time, we pass each image batch through the following sequence of -functions to produce a set of detections: - -``` -inputs (images tensor) -> preprocess -> predict -> postprocess -> - outputs (boxes tensor, scores tensor, classes tensor, num_detections tensor) -``` - -Some conventions to be aware of: - -* `DetectionModel`s should make no assumptions about the input size or aspect - ratio --- they are responsible for doing any resize/reshaping necessary - (see docstring for the `preprocess` function). -* Output classes are always integers in the range `[0, num_classes)`. - Any mapping of these integers to semantic labels is to be handled outside - of this class. We never explicitly emit a “background class” --- thus 0 is - the first non-background class and any logic of predicting and removing - implicit background classes must be handled internally by the implementation. -* Detected boxes are to be interpreted as being in - `[y_min, x_min, y_max, x_max]` format and normalized relative to the - image window. -* We do not specifically assume any kind of probabilistic interpretation of the - scores --- the only important thing is their relative ordering. Thus - implementations of the postprocess function are free to output logits, - probabilities, calibrated probabilities, or anything else. - -## Defining a new Faster R-CNN or SSD Feature Extractor - -In most cases, you probably will not implement a `DetectionModel` from scratch ---- instead you might create a new feature extractor to be used by one of the -SSD or Faster R-CNN meta-architectures. (We think of meta-architectures as -classes that define entire families of models using the `DetectionModel` -abstraction). - -Note: For the following discussion to make sense, we recommend first becoming -familiar with the [Faster R-CNN](https://arxiv.org/abs/1506.01497) paper. - -Let’s now imagine that you have invented a brand new network architecture -(say, “InceptionV100”) for classification and want to see how InceptionV100 -would behave as a feature extractor for detection (say, with Faster R-CNN). -A similar procedure would hold for SSD models, but we’ll discuss Faster R-CNN. - -To use InceptionV100, we will have to define a new -`FasterRCNNFeatureExtractor` and pass it to our `FasterRCNNMetaArch` -constructor as input. See -`object_detection/meta_architectures/faster_rcnn_meta_arch.py` for definitions -of `FasterRCNNFeatureExtractor` and `FasterRCNNMetaArch`, respectively. -A `FasterRCNNFeatureExtractor` must define a few -functions: - -* `preprocess`: Run any preprocessing of input values that is necessary prior - to running the detector on an input image. -* `_extract_proposal_features`: Extract first stage Region Proposal Network - (RPN) features. -* `_extract_box_classifier_features`: Extract second stage Box Classifier - features. -* `restore_from_classification_checkpoint_fn`: Load a checkpoint into the - TensorFlow graph. - -See the `object_detection/models/faster_rcnn_resnet_v1_feature_extractor.py` -definition as one example. Some remarks: - -* We typically initialize the weights of this feature extractor - using those from the - [Slim Resnet-101 classification checkpoint](https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models), - and we know - that images were preprocessed when training this checkpoint - by subtracting a channel mean from each input - image. Thus, we implement the preprocess function to replicate the same - channel mean subtraction behavior. -* The “full” resnet classification network defined in slim is cut into two - parts --- all but the last “resnet block” is put into the - `_extract_proposal_features` function and the final block is separately - defined in the `_extract_box_classifier_features function`. In general, - some experimentation may be required to decide on an optimal layer at - which to “cut” your feature extractor into these two pieces for Faster R-CNN. - -## Register your model for configuration - -Assuming that your new feature extractor does not require nonstandard -configuration, you will want to ideally be able to simply change the -“feature_extractor.type” fields in your configuration protos to point to a -new feature extractor. In order for our API to know how to understand this -new type though, you will first have to register your new feature -extractor with the model builder (`object_detection/builders/model_builder.py`), -whose job is to create models from config protos.. - -Registration is simple --- just add a pointer to the new Feature Extractor -class that you have defined in one of the SSD or Faster R-CNN Feature -Extractor Class maps at the top of the -`object_detection/builders/model_builder.py` file. -We recommend adding a test in `object_detection/builders/model_builder_test.py` -to make sure that parsing your proto will work as expected. - -## Taking your new model for a spin - -After registration you are ready to go with your model! Some final tips: - -* To save time debugging, try running your configuration file locally first - (both training and evaluation). -* Do a sweep of learning rates to figure out which learning rate is best - for your model. -* A small but often important detail: you may find it necessary to disable - batchnorm training (that is, load the batch norm parameters from the - classification checkpoint, but do not update them during gradient descent). diff --git a/research/object_detection/g3doc/evaluation_protocols.md b/research/object_detection/g3doc/evaluation_protocols.md deleted file mode 100644 index d5a070f6bc0..00000000000 --- a/research/object_detection/g3doc/evaluation_protocols.md +++ /dev/null @@ -1,163 +0,0 @@ -# Supported object detection evaluation protocols - -The TensorFlow Object Detection API currently supports three evaluation protocols, -that can be configured in `EvalConfig` by setting `metrics_set` to the -corresponding value. - -## PASCAL VOC 2010 detection metric - -`EvalConfig.metrics_set='pascal_voc_detection_metrics'` - -The commonly used mAP metric for evaluating the quality of object detectors, -computed according to the protocol of the PASCAL VOC Challenge 2010-2012. The -protocol is available -[here](http://host.robots.ox.ac.uk/pascal/VOC/voc2010/devkit_doc_08-May-2010.pdf). - -## Weighted PASCAL VOC detection metric - -`EvalConfig.metrics_set='weighted_pascal_voc_detection_metrics'` - -The weighted PASCAL metric computes the mean average precision as the average -precision when treating all classes as a single class. In comparison, -PASCAL metrics computes the mean average precision as the mean of the -per-class average precisions. - -For example, the test set consists of two classes, "cat" and "dog", and there -are ten times more boxes of "cat" than those of "dog". According to PASCAL VOC -2010 metric, performance on each of the two classes would contribute equally -towards the final mAP value, while for the Weighted PASCAL VOC metric the final -mAP value will be influenced by frequency of each class. - -## PASCAL VOC 2010 instance segmentation metric - -`EvalConfig.metrics_set='pascal_voc_instance_segmentation_metrics'` - -Similar to Pascal VOC 2010 detection metric, but computes the intersection over -union based on the object masks instead of object boxes. - -## Weighted PASCAL VOC instance segmentation metric - -`EvalConfig.metrics_set='weighted_pascal_voc_instance_segmentation_metrics'` - -Similar to the weighted pascal voc 2010 detection metric, but computes the -intersection over union based on the object masks instead of object boxes. - - -## COCO detection metrics - -`EvalConfig.metrics_set='coco_detection_metrics'` - -The COCO metrics are the official detection metrics used to score the -[COCO competition](http://cocodataset.org/) and are similar to Pascal VOC -metrics but have a slightly different implementation and report additional -statistics such as mAP at IOU thresholds of .5:.95, and precision/recall -statistics for small, medium, and large objects. -See the -[pycocotools](https://github.com/cocodataset/cocoapi/tree/master/PythonAPI) -repository for more details. - -## COCO mask metrics - -`EvalConfig.metrics_set='coco_mask_metrics'` - -Similar to the COCO detection metrics, but computes the -intersection over union based on the object masks instead of object boxes. - -## Open Images V2 detection metric - -`EvalConfig.metrics_set='oid_V2_detection_metrics'` - -This metric is defined originally for evaluating detector performance on [Open -Images V2 dataset](https://github.com/openimages/dataset) and is fairly similar -to the PASCAL VOC 2010 metric mentioned above. It computes interpolated average -precision (AP) for each class and averages it among all classes (mAP). - -The difference to the PASCAL VOC 2010 metric is the following: Open Images -annotations contain `group-of` ground-truth boxes (see [Open Images data -description](https://github.com/openimages/dataset#annotations-human-bboxcsv)), -that are treated differently for the purpose of deciding whether detections are -"true positives", "ignored", "false positives". Here we define these three -cases: - -A detection is a "true positive" if there is a non-group-of ground-truth box, -such that: - -* The detection box and the ground-truth box are of the same class, and - intersection-over-union (IoU) between the detection box and the ground-truth - box is greater than the IoU threshold (default value 0.5). \ - Illustration of handling non-group-of boxes: \ - ![alt - groupof_case_eval](img/nongroupof_case_eval.png "illustration of handling non-group-of boxes: yellow box - ground truth bounding box; green box - true positive; red box - false positives.") - - * yellow box - ground-truth box; - * green box - true positive; - * red boxes - false positives. - -* This is the highest scoring detection for this ground truth box that - satisfies the criteria above. - -A detection is "ignored" if it is not a true positive, and there is a `group-of` -ground-truth box such that: - -* The detection box and the ground-truth box are of the same class, and the - area of intersection between the detection box and the ground-truth box - divided by the area of the detection is greater than 0.5. This is intended - to measure whether the detection box is approximately inside the group-of - ground-truth box. \ - Illustration of handling `group-of` boxes: \ - ![alt - groupof_case_eval](img/groupof_case_eval.png "illustration of handling group-of boxes: yellow box - ground truth bounding box; grey boxes - two detections of cars, that are ignored; red box - false positive.") - - * yellow box - ground-truth box; - * grey boxes - two detections on cars, that are ignored; - * red box - false positive. - -A detection is a "false positive" if it is neither a "true positive" nor -"ignored". - -Precision and recall are defined as: - -* Precision = number-of-true-positives/(number-of-true-positives + number-of-false-positives) -* Recall = number-of-true-positives/number-of-non-group-of-boxes - -Note that detections ignored as firing on a `group-of` ground-truth box do not -contribute to the number of true positives. - -The labels in Open Images are organized in a -[hierarchy](https://storage.googleapis.com/openimages/2017_07/bbox_labels_vis/bbox_labels_vis.html). -Ground-truth bounding-boxes are annotated with the most specific class available -in the hierarchy. For example, "car" has two children "limousine" and "van". Any -other kind of car is annotated as "car" (for example, a sedan). Given this -convention, the evaluation software treats all classes independently, ignoring -the hierarchy. To achieve high performance values, object detectors should -output bounding-boxes labelled in the same manner. - -The old metric name is DEPRECATED. -`EvalConfig.metrics_set='open_images_V2_detection_metrics'` - -## OID Challenge Object Detection Metric - -`EvalConfig.metrics_set='oid_challenge_detection_metrics'` - -The metric for the OID Challenge Object Detection Metric 2018/2019 Object -Detection track. The description is provided on the -[Open Images Challenge website](https://storage.googleapis.com/openimages/web/evaluation.html#object_detection_eval). - -The old metric name is DEPRECATED. -`EvalConfig.metrics_set='oid_challenge_object_detection_metrics'` - -## OID Challenge Visual Relationship Detection Metric - -The metric for the OID Challenge Visual Relationship Detection Metric 2018,2019 -Visual Relationship Detection track. The description is provided on the -[Open Images Challenge website](https://storage.googleapis.com/openimages/web/evaluation.html#visual_relationships_eval). -Note: this is currently a stand-alone metric, that can be used only through the -`metrics/oid_vrd_challenge_evaluation.py` util. - -## OID Challenge Instance Segmentation Metric - -`EvalConfig.metrics_set='oid_challenge_segmentation_metrics'` - -The metric for the OID Challenge Instance Segmentation Metric 2019, Instance -Segmentation track. The description is provided on the -[Open Images Challenge website](https://storage.googleapis.com/openimages/web/evaluation.html#instance_segmentation_eval). diff --git a/research/object_detection/g3doc/exporting_models.md b/research/object_detection/g3doc/exporting_models.md deleted file mode 100644 index 701acf3c430..00000000000 --- a/research/object_detection/g3doc/exporting_models.md +++ /dev/null @@ -1,38 +0,0 @@ -# Exporting a trained model for inference - -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -After your model has been trained, you should export it to a TensorFlow -graph proto. A checkpoint will typically consist of three files: - -* model.ckpt-${CHECKPOINT_NUMBER}.data-00000-of-00001 -* model.ckpt-${CHECKPOINT_NUMBER}.index -* model.ckpt-${CHECKPOINT_NUMBER}.meta - -After you've identified a candidate checkpoint to export, run the following -command from tensorflow/models/research: - -``` bash -# From tensorflow/models/research/ -INPUT_TYPE=image_tensor -PIPELINE_CONFIG_PATH={path to pipeline config file} -TRAINED_CKPT_PREFIX={path to model.ckpt} -EXPORT_DIR={path to folder that will be used for export} -python object_detection/export_inference_graph.py \ - --input_type=${INPUT_TYPE} \ - --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ - --trained_checkpoint_prefix=${TRAINED_CKPT_PREFIX} \ - --output_directory=${EXPORT_DIR} -``` - -NOTE: We are configuring our exported model to ingest 4-D image tensors. We can -also configure the exported model to take encoded images or serialized -`tf.Example`s. - -After export, you should see the directory ${EXPORT_DIR} containing the following: - -* saved_model/, a directory containing the saved model format of the exported model -* frozen_inference_graph.pb, the frozen graph format of the exported model -* model.ckpt.*, the model checkpoints used for exporting -* checkpoint, a file specifying to restore included checkpoint files -* pipeline.config, pipeline config file for the exported model diff --git a/research/object_detection/g3doc/faq.md b/research/object_detection/g3doc/faq.md deleted file mode 100644 index f2a6e30ccf7..00000000000 --- a/research/object_detection/g3doc/faq.md +++ /dev/null @@ -1,27 +0,0 @@ -# Frequently Asked Questions - -## Q: How can I ensure that all the groundtruth boxes are used during train and eval? -A: For the object detecion framework to be TPU-complient, we must pad our input -tensors to static shapes. This means that we must pad to a fixed number of -bounding boxes, configured by `InputReader.max_number_of_boxes`. It is -important to set this value to a number larger than the maximum number of -groundtruth boxes in the dataset. If an image is encountered with more -bounding boxes, the excess boxes will be clipped. - -## Q: AttributeError: 'module' object has no attribute 'BackupHandler' -A: This BackupHandler (tf_slim.tfexample_decoder.BackupHandler) was -introduced in tensorflow 1.5.0 so runing with earlier versions may cause this -issue. It now has been replaced by -object_detection.data_decoders.tf_example_decoder.BackupHandler. Whoever sees -this issue should be able to resolve it by syncing your fork to HEAD. -Same for LookupTensor. - -## Q: AttributeError: 'module' object has no attribute 'LookupTensor' -A: Similar to BackupHandler, syncing your fork to HEAD should make it work. - -## Q: Why can't I get the inference time as reported in model zoo? -A: The inference time reported in model zoo is mean time of testing hundreds of -images with an internal machine. As mentioned in -[TensorFlow detection model zoo](tf1_detection_zoo.md), this speed depends -highly on one's specific hardware configuration and should be treated more as -relative timing. diff --git a/research/object_detection/g3doc/img/dogs_detections_output.jpg b/research/object_detection/g3doc/img/dogs_detections_output.jpg deleted file mode 100644 index 9e88a7010fa..00000000000 Binary files a/research/object_detection/g3doc/img/dogs_detections_output.jpg and /dev/null differ diff --git a/research/object_detection/g3doc/img/example_cat.jpg b/research/object_detection/g3doc/img/example_cat.jpg deleted file mode 100644 index 74c7ef4b084..00000000000 Binary files a/research/object_detection/g3doc/img/example_cat.jpg and /dev/null differ diff --git a/research/object_detection/g3doc/img/groupof_case_eval.png b/research/object_detection/g3doc/img/groupof_case_eval.png deleted file mode 100644 index 5abc9b6984f..00000000000 Binary files a/research/object_detection/g3doc/img/groupof_case_eval.png and /dev/null differ diff --git a/research/object_detection/g3doc/img/kites_detections_output.jpg b/research/object_detection/g3doc/img/kites_detections_output.jpg deleted file mode 100644 index 7c0f3364ded..00000000000 Binary files a/research/object_detection/g3doc/img/kites_detections_output.jpg and /dev/null differ diff --git a/research/object_detection/g3doc/img/kites_with_segment_overlay.png b/research/object_detection/g3doc/img/kites_with_segment_overlay.png deleted file mode 100644 index a52e57de193..00000000000 Binary files a/research/object_detection/g3doc/img/kites_with_segment_overlay.png and /dev/null differ diff --git a/research/object_detection/g3doc/img/mask_improvement.png b/research/object_detection/g3doc/img/mask_improvement.png deleted file mode 100644 index 3e35501b8b5..00000000000 Binary files a/research/object_detection/g3doc/img/mask_improvement.png and /dev/null differ diff --git a/research/object_detection/g3doc/img/nongroupof_case_eval.png b/research/object_detection/g3doc/img/nongroupof_case_eval.png deleted file mode 100644 index cbb76f493ad..00000000000 Binary files a/research/object_detection/g3doc/img/nongroupof_case_eval.png and /dev/null differ diff --git a/research/object_detection/g3doc/img/oid_bus_72e19c28aac34ed8.jpg b/research/object_detection/g3doc/img/oid_bus_72e19c28aac34ed8.jpg deleted file mode 100644 index 1e9412ad545..00000000000 Binary files a/research/object_detection/g3doc/img/oid_bus_72e19c28aac34ed8.jpg and /dev/null differ diff --git a/research/object_detection/g3doc/img/oid_monkey_3b4168c89cecbc5b.jpg b/research/object_detection/g3doc/img/oid_monkey_3b4168c89cecbc5b.jpg deleted file mode 100644 index 46b1fb282a4..00000000000 Binary files a/research/object_detection/g3doc/img/oid_monkey_3b4168c89cecbc5b.jpg and /dev/null differ diff --git a/research/object_detection/g3doc/img/oxford_pet.png b/research/object_detection/g3doc/img/oxford_pet.png deleted file mode 100644 index ddac415f5ef..00000000000 Binary files a/research/object_detection/g3doc/img/oxford_pet.png and /dev/null differ diff --git a/research/object_detection/g3doc/img/tensorboard.png b/research/object_detection/g3doc/img/tensorboard.png deleted file mode 100644 index fbcdbeb38cf..00000000000 Binary files a/research/object_detection/g3doc/img/tensorboard.png and /dev/null differ diff --git a/research/object_detection/g3doc/img/tensorboard2.png b/research/object_detection/g3doc/img/tensorboard2.png deleted file mode 100644 index 97ad22daa11..00000000000 Binary files a/research/object_detection/g3doc/img/tensorboard2.png and /dev/null differ diff --git a/research/object_detection/g3doc/img/tf-od-api-logo.png b/research/object_detection/g3doc/img/tf-od-api-logo.png deleted file mode 100644 index 9fa9cc9dba2..00000000000 Binary files a/research/object_detection/g3doc/img/tf-od-api-logo.png and /dev/null differ diff --git a/research/object_detection/g3doc/instance_segmentation.md b/research/object_detection/g3doc/instance_segmentation.md deleted file mode 100644 index f9b4856c90f..00000000000 --- a/research/object_detection/g3doc/instance_segmentation.md +++ /dev/null @@ -1,105 +0,0 @@ -## Run an Instance Segmentation Model - -For some applications it isn't adequate enough to localize an object with a -simple bounding box. For instance, you might want to segment an object region -once it is detected. This class of problems is called **instance segmentation**. - -

- -

- -### Materializing data for instance segmentation {#materializing-instance-seg} - -Instance segmentation is an extension of object detection, where a binary mask -(i.e. object vs. background) is associated with every bounding box. This allows -for more fine-grained information about the extent of the object within the box. -To train an instance segmentation model, a groundtruth mask must be supplied for -every groundtruth bounding box. In additional to the proto fields listed in the -section titled [Using your own dataset](using_your_own_dataset.md), one must -also supply `image/object/mask`, which can either be a repeated list of -single-channel encoded PNG strings, or a single dense 3D binary tensor where -masks corresponding to each object are stacked along the first dimension. Each -is described in more detail below. - -#### PNG Instance Segmentation Masks - -Instance segmentation masks can be supplied as serialized PNG images. - -```shell -image/object/mask = ["\x89PNG\r\n\x1A\n\x00\x00\x00\rIHDR\...", ...] -``` - -These masks are whole-image masks, one for each object instance. The spatial -dimensions of each mask must agree with the image. Each mask has only a single -channel, and the pixel values are either 0 (background) or 1 (object mask). -**PNG masks are the preferred parameterization since they offer considerable -space savings compared to dense numerical masks.** - -#### Dense Numerical Instance Segmentation Masks - -Masks can also be specified via a dense numerical tensor. - -```shell -image/object/mask = [0.0, 0.0, 1.0, 1.0, 0.0, ...] -``` - -For an image with dimensions `H` x `W` and `num_boxes` groundtruth boxes, the -mask corresponds to a [`num_boxes`, `H`, `W`] float32 tensor, flattened into a -single vector of shape `num_boxes` * `H` * `W`. In TensorFlow, examples are read -in row-major format, so the elements are organized as: - -```shell -... mask 0 row 0 ... mask 0 row 1 ... // ... mask 0 row H-1 ... mask 1 row 0 ... -``` - -where each row has W contiguous binary values. - -To see an example tf-records with mask labels, see the examples under the -[Preparing Inputs](preparing_inputs.md) section. - -### Pre-existing config files - -We provide four instance segmentation config files that you can use to train -your own models: - -1. mask_rcnn_inception_resnet_v2_atrous_coco -1. mask_rcnn_resnet101_atrous_coco -1. mask_rcnn_resnet50_atrous_coco -1. mask_rcnn_inception_v2_coco - -For more details see the [detection model zoo](tf1_detection_zoo.md). - -### Updating a Faster R-CNN config file - -Currently, the only supported instance segmentation model is [Mask -R-CNN](https://arxiv.org/abs/1703.06870), which requires Faster R-CNN as the -backbone object detector. - -Once you have a baseline Faster R-CNN pipeline configuration, you can make the -following modifications in order to convert it into a Mask R-CNN model. - -1. Within `train_input_reader` and `eval_input_reader`, set - `load_instance_masks` to `True`. If using PNG masks, set `mask_type` to - `PNG_MASKS`, otherwise you can leave it as the default 'NUMERICAL_MASKS'. -1. Within the `faster_rcnn` config, use a `MaskRCNNBoxPredictor` as the - `second_stage_box_predictor`. -1. Within the `MaskRCNNBoxPredictor` message, set `predict_instance_masks` to - `True`. You must also define `conv_hyperparams`. -1. Within the `faster_rcnn` message, set `number_of_stages` to `3`. -1. Add instance segmentation metrics to the set of metrics: - `'coco_mask_metrics'`. -1. Update the `input_path`s to point at your data. - -Please refer to the section on [Running the pets dataset](running_pets.md) for -additional details. - -> Note: The mask prediction branch consists of a sequence of convolution layers. -> You can set the number of convolution layers and their depth as follows: -> -> 1. Within the `MaskRCNNBoxPredictor` message, set the -> `mask_prediction_conv_depth` to your value of interest. The default value -> is 256. If you set it to `0` (recommended), the depth is computed -> automatically based on the number of classes in the dataset. -> 1. Within the `MaskRCNNBoxPredictor` message, set the -> `mask_prediction_num_conv_layers` to your value of interest. The default -> value is 2. diff --git a/research/object_detection/g3doc/oid_inference_and_evaluation.md b/research/object_detection/g3doc/oid_inference_and_evaluation.md deleted file mode 100644 index d54ad23940b..00000000000 --- a/research/object_detection/g3doc/oid_inference_and_evaluation.md +++ /dev/null @@ -1,257 +0,0 @@ -# Inference and evaluation on the Open Images dataset - -This page presents a tutorial for running object detector inference and -evaluation measure computations on the [Open Images -dataset](https://github.com/openimages/dataset), using tools from the -[TensorFlow Object Detection -API](https://github.com/tensorflow/models/tree/master/research/object_detection). -It shows how to download the images and annotations for the validation and test -sets of Open Images; how to package the downloaded data in a format understood -by the Object Detection API; where to find a trained object detector model for -Open Images; how to run inference; and how to compute evaluation measures on the -inferred detections. - -Inferred detections will look like the following: - -![](img/oid_bus_72e19c28aac34ed8.jpg) -![](img/oid_monkey_3b4168c89cecbc5b.jpg) - -On the validation set of Open Images, this tutorial requires 27GB of free disk -space and the inference step takes approximately 9 hours on a single NVIDIA -Tesla P100 GPU. On the test set -- 75GB and 27 hours respectively. All other -steps require less than two hours in total on both sets. - -## Installing TensorFlow, the Object Detection API, and Google Cloud SDK - -Please run through the [installation instructions](installation.md) to install -TensorFlow and all its dependencies. Ensure the Protobuf libraries are compiled -and the library directories are added to `PYTHONPATH`. You will also need to -`pip` install `pandas` and `contextlib2`. - -Some of the data used in this tutorial lives in Google Cloud buckets. To access -it, you will have to [install the Google Cloud -SDK](https://cloud.google.com/sdk/downloads) on your workstation or laptop. - -## Preparing the Open Images validation and test sets - -In order to run inference and subsequent evaluation measure computations, we -require a dataset of images and ground truth boxes, packaged as TFRecords of -TFExamples. To create such a dataset for Open Images, you will need to first -download ground truth boxes from the [Open Images -website](https://github.com/openimages/dataset): - -```bash -# From tensorflow/models/research -mkdir oid -cd oid -wget https://storage.googleapis.com/openimages/2017_07/annotations_human_bbox_2017_07.tar.gz -tar -xvf annotations_human_bbox_2017_07.tar.gz -``` - -Next, download the images. In this tutorial, we will use lower resolution images -provided by [CVDF](http://www.cvdfoundation.org). Please follow the instructions -on [CVDF's Open Images repository -page](https://github.com/cvdfoundation/open-images-dataset) in order to gain -access to the cloud bucket with the images. Then run: - -```bash -# From tensorflow/models/research/oid -SPLIT=validation # Set SPLIT to "test" to download the images in the test set -mkdir raw_images_${SPLIT} -gsutil -m rsync -r gs://open-images-dataset/$SPLIT raw_images_${SPLIT} -``` - -Another option for downloading the images is to follow the URLs contained in the -[image URLs and metadata CSV -files](https://storage.googleapis.com/openimages/2017_07/images_2017_07.tar.gz) -on the Open Images website. - -At this point, your `tensorflow/models/research/oid` directory should appear as -follows: - -```lang-none -|-- 2017_07 -| |-- test -| | `-- annotations-human-bbox.csv -| |-- train -| | `-- annotations-human-bbox.csv -| `-- validation -| `-- annotations-human-bbox.csv -|-- raw_images_validation (if you downloaded the validation split) -| `-- ... (41,620 files matching regex "[0-9a-f]{16}.jpg") -|-- raw_images_test (if you downloaded the test split) -| `-- ... (125,436 files matching regex "[0-9a-f]{16}.jpg") -`-- annotations_human_bbox_2017_07.tar.gz -``` - -Next, package the data into TFRecords of TFExamples by running: - -```bash -# From tensorflow/models/research/oid -SPLIT=validation # Set SPLIT to "test" to create TFRecords for the test split -mkdir ${SPLIT}_tfrecords - -PYTHONPATH=$PYTHONPATH:$(readlink -f ..) \ -python -m object_detection/dataset_tools/create_oid_tf_record \ - --input_box_annotations_csv 2017_07/$SPLIT/annotations-human-bbox.csv \ - --input_images_directory raw_images_${SPLIT} \ - --input_label_map ../object_detection/data/oid_bbox_trainable_label_map.pbtxt \ - --output_tf_record_path_prefix ${SPLIT}_tfrecords/$SPLIT.tfrecord \ - --num_shards=100 -``` - -To add image-level labels, use the `--input_image_label_annotations_csv` flag. - -This results in 100 TFRecord files (shards), written to -`oid/${SPLIT}_tfrecords`, with filenames matching -`${SPLIT}.tfrecord-000[0-9][0-9]-of-00100`. Each shard contains approximately -the same number of images and is defacto a representative random sample of the -input data. [This enables](#accelerating_inference) a straightforward work -division scheme for distributing inference and also approximate measure -computations on subsets of the validation and test sets. - -## Inferring detections - -Inference requires a trained object detection model. In this tutorial we will -use a model from the [detections model zoo](tf1_detection_zoo.md), which can -be downloaded and unpacked by running the commands below. More information about -the model, such as its architecture and how it was trained, is available in the -[model zoo page](tf1_detection_zoo.md). - -```bash -# From tensorflow/models/research/oid -wget http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_oid_14_10_2017.tar.gz -tar -zxvf faster_rcnn_inception_resnet_v2_atrous_oid_14_10_2017.tar.gz -``` - -At this point, data is packed into TFRecords and we have an object detector -model. We can run inference using: - -```bash -# From tensorflow/models/research/oid -SPLIT=validation # or test -TF_RECORD_FILES=$(ls -1 ${SPLIT}_tfrecords/* | tr '\n' ',') - -PYTHONPATH=$PYTHONPATH:$(readlink -f ..) \ -python -m object_detection/inference/infer_detections \ - --input_tfrecord_paths=$TF_RECORD_FILES \ - --output_tfrecord_path=${SPLIT}_detections.tfrecord-00000-of-00001 \ - --inference_graph=faster_rcnn_inception_resnet_v2_atrous_oid/frozen_inference_graph.pb \ - --discard_image_pixels -``` - -Inference preserves all fields of the input TFExamples, and adds new fields to -store the inferred detections. This allows [computing evaluation -measures](#computing-evaluation-measures) on the output TFRecord alone, as -groundtruth boxes are preserved as well. Since measure computations don't -require access to the images, `infer_detections` can optionally discard them -with the `--discard_image_pixels` flag. Discarding the images drastically -reduces the size of the output TFRecord. - -### Accelerating inference - -Running inference on the whole validation or test set can take a long time to -complete due to the large number of images present in these sets (41,620 and -125,436 respectively). For quick but approximate evaluation, inference and the -subsequent measure computations can be run on a small number of shards. To run -for example on 2% of all the data, it is enough to set `TF_RECORD_FILES` as -shown below before running `infer_detections`: - -```bash -TF_RECORD_FILES=$(ls ${SPLIT}_tfrecords/${SPLIT}.tfrecord-0000[0-1]-of-00100 | tr '\n' ',') -``` - -Please note that computing evaluation measures on a small subset of the data -introduces variance and bias, since some classes of objects won't be seen during -evaluation. In the example above, this leads to 13.2% higher mAP on the first -two shards of the validation set compared to the mAP for the full set ([see mAP -results](#expected-maps)). - -Another way to accelerate inference is to run it in parallel on multiple -TensorFlow devices on possibly multiple machines. The script below uses -[tmux](https://github.com/tmux/tmux/wiki) to run a separate `infer_detections` -process for each GPU on different partition of the input data. - -```bash -# From tensorflow/models/research/oid -SPLIT=validation # or test -NUM_GPUS=4 -NUM_SHARDS=100 - -tmux new-session -d -s "inference" -function tmux_start { tmux new-window -d -n "inference:GPU$1" "${*:2}; exec bash"; } -for gpu_index in $(seq 0 $(($NUM_GPUS-1))); do - start_shard=$(( $gpu_index * $NUM_SHARDS / $NUM_GPUS )) - end_shard=$(( ($gpu_index + 1) * $NUM_SHARDS / $NUM_GPUS - 1)) - TF_RECORD_FILES=$(seq -s, -f "${SPLIT}_tfrecords/${SPLIT}.tfrecord-%05.0f-of-$(printf '%05d' $NUM_SHARDS)" $start_shard $end_shard) - tmux_start ${gpu_index} \ - PYTHONPATH=$PYTHONPATH:$(readlink -f ..) CUDA_VISIBLE_DEVICES=$gpu_index \ - python -m object_detection/inference/infer_detections \ - --input_tfrecord_paths=$TF_RECORD_FILES \ - --output_tfrecord_path=${SPLIT}_detections.tfrecord-$(printf "%05d" $gpu_index)-of-$(printf "%05d" $NUM_GPUS) \ - --inference_graph=faster_rcnn_inception_resnet_v2_atrous_oid/frozen_inference_graph.pb \ - --discard_image_pixels -done -``` - -After all `infer_detections` processes finish, `tensorflow/models/research/oid` -will contain one output TFRecord from each process, with name matching -`validation_detections.tfrecord-0000[0-3]-of-00004`. - -## Computing evaluation measures - -To compute evaluation measures on the inferred detections you first need to -create the appropriate configuration files: - -```bash -# From tensorflow/models/research/oid -SPLIT=validation # or test -NUM_SHARDS=1 # Set to NUM_GPUS if using the parallel evaluation script above - -mkdir -p ${SPLIT}_eval_metrics - -echo " -label_map_path: '../object_detection/data/oid_bbox_trainable_label_map.pbtxt' -tf_record_input_reader: { input_path: '${SPLIT}_detections.tfrecord@${NUM_SHARDS}' } -" > ${SPLIT}_eval_metrics/${SPLIT}_input_config.pbtxt - -echo " -metrics_set: 'oid_V2_detection_metrics' -" > ${SPLIT}_eval_metrics/${SPLIT}_eval_config.pbtxt -``` - -And then run: - -```bash -# From tensorflow/models/research/oid -SPLIT=validation # or test - -PYTHONPATH=$PYTHONPATH:$(readlink -f ..) \ -python -m object_detection/metrics/offline_eval_map_corloc \ - --eval_dir=${SPLIT}_eval_metrics \ - --eval_config_path=${SPLIT}_eval_metrics/${SPLIT}_eval_config.pbtxt \ - --input_config_path=${SPLIT}_eval_metrics/${SPLIT}_input_config.pbtxt -``` - -The first configuration file contains an `object_detection.protos.InputReader` -message that describes the location of the necessary input files. The second -file contains an `object_detection.protos.EvalConfig` message that describes the -evaluation metric. For more information about these protos see the corresponding -source files. - -### Expected mAPs - -The result of running `offline_eval_map_corloc` is a CSV file located at -`${SPLIT}_eval_metrics/metrics.csv`. With the above configuration, the file will -contain average precision at IoU≥0.5 for each of the classes present in the -dataset. It will also contain the mAP@IoU≥0.5. Both the per-class average -precisions and the mAP are computed according to the [Open Images evaluation -protocol](evaluation_protocols.md). The expected mAPs for the validation and -test sets of Open Images in this case are: - -Set | Fraction of data | Images | mAP@IoU≥0.5 ----------: | :--------------: | :-----: | ----------- -validation | everything | 41,620 | 39.2% -validation | first 2 shards | 884 | 52.4% -test | everything | 125,436 | 37.7% -test | first 2 shards | 2,476 | 50.8% diff --git a/research/object_detection/g3doc/preparing_inputs.md b/research/object_detection/g3doc/preparing_inputs.md deleted file mode 100644 index 7e8df08502b..00000000000 --- a/research/object_detection/g3doc/preparing_inputs.md +++ /dev/null @@ -1,59 +0,0 @@ -# Preparing Inputs - -TensorFlow Object Detection API reads data using the TFRecord file format. Two -sample scripts (`create_pascal_tf_record.py` and `create_pet_tf_record.py`) are -provided to convert from the PASCAL VOC dataset and Oxford-IIIT Pet dataset to -TFRecords. - -## Generating the PASCAL VOC TFRecord files. - -The raw 2012 PASCAL VOC data set is located -[here](http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar). -To download, extract and convert it to TFRecords, run the following commands -below: - -```bash -# From tensorflow/models/research/ -wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar -tar -xvf VOCtrainval_11-May-2012.tar -python object_detection/dataset_tools/create_pascal_tf_record.py \ - --label_map_path=object_detection/data/pascal_label_map.pbtxt \ - --data_dir=VOCdevkit --year=VOC2012 --set=train \ - --output_path=pascal_train.record -python object_detection/dataset_tools/create_pascal_tf_record.py \ - --label_map_path=object_detection/data/pascal_label_map.pbtxt \ - --data_dir=VOCdevkit --year=VOC2012 --set=val \ - --output_path=pascal_val.record -``` - -You should end up with two TFRecord files named `pascal_train.record` and -`pascal_val.record` in the `tensorflow/models/research/` directory. - -The label map for the PASCAL VOC data set can be found at -`object_detection/data/pascal_label_map.pbtxt`. - -## Generating the Oxford-IIIT Pet TFRecord files. - -The Oxford-IIIT Pet data set is located -[here](http://www.robots.ox.ac.uk/~vgg/data/pets/). To download, extract and -convert it to TFRecords, run the following commands below: - -```bash -# From tensorflow/models/research/ -wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz -wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz -tar -xvf annotations.tar.gz -tar -xvf images.tar.gz -python object_detection/dataset_tools/create_pet_tf_record.py \ - --label_map_path=object_detection/data/pet_label_map.pbtxt \ - --data_dir=`pwd` \ - --output_dir=`pwd` -``` - -You should end up with two 10-sharded TFRecord files named -`pet_faces_train.record-?????-of-00010` and -`pet_faces_val.record-?????-of-00010` in the `tensorflow/models/research/` -directory. - -The label map for the Pet dataset can be found at -`object_detection/data/pet_label_map.pbtxt`. diff --git a/research/object_detection/g3doc/release_notes.md b/research/object_detection/g3doc/release_notes.md deleted file mode 100644 index 21512397c99..00000000000 --- a/research/object_detection/g3doc/release_notes.md +++ /dev/null @@ -1,358 +0,0 @@ -# Release Notes - -### September 3rd, 2020 - -TF2 OD API models can now be converted to TensorFlow Lite! Only SSD models -currently supported. See documentation. - -**Thanks to contributors**: Sachin Joglekar - -### July 10th, 2020 - -We are happy to announce that the TF OD API officially supports TF2! Our release -includes: - -* New binaries for train/eval/export that are designed to run in eager mode. -* A suite of TF2 compatible (Keras-based) models; this includes migrations of - our most popular TF1.x models (e.g., SSD with MobileNet, RetinaNet, Faster - R-CNN, Mask R-CNN), as well as a few new architectures for which we will - only maintain TF2 implementations: - - 1. CenterNet - a simple and effective anchor-free architecture based on the - recent [Objects as Points](https://arxiv.org/abs/1904.07850) paper by - Zhou et al - 2. [EfficientDet](https://arxiv.org/abs/1911.09070) - a recent family of - SOTA models discovered with the help of Neural Architecture Search. - -* COCO pre-trained weights for all of the models provided as TF2 style - object-based checkpoints. - -* Access to - [Distribution Strategies](https://www.tensorflow.org/guide/distributed_training) - for distributed training --- our model are designed to be trainable using - sync multi-GPU and TPU platforms. - -* Colabs demo’ing eager mode training and inference. - -See our release blogpost -[here](https://blog.tensorflow.org/2020/07/tensorflow-2-meets-object-detection-api.html). -If you are an existing user of the TF OD API using TF 1.x, don’t worry, we’ve -got you covered. - -**Thanks to contributors**: Akhil Chinnakotla, Allen Lavoie, Anirudh Vegesana, -Anjali Sridhar, Austin Myers, Dan Kondratyuk, David Ross, Derek Chow, Jaeyoun -Kim, Jing Li, Jonathan Huang, Jordi Pont-Tuset, Karmel Allison, Kathy Ruan, -Kaushik Shivakumar, Lu He, Mingxing Tan, Pengchong Jin, Ronny Votel, Sara Beery, -Sergi Caelles Prat, Shan Yang, Sudheendra Vijayanarasimhan, Tina Tian, Tomer -Kaftan, Vighnesh Birodkar, Vishnu Banna, Vivek Rathod, Yanhui Liang, Yiming Shi, -Yixin Shi, Yu-hui Chen, Zhichao Lu. - -### June 26th, 2020 - -We have released SSDLite with MobileDet GPU backbone, which achieves 17% mAP -higher than the MobileNetV2 SSDLite (27.5 mAP vs 23.5 mAP) on a NVIDIA Jetson -Xavier at comparable latency (3.2ms vs 3.3ms). - -Along with the model definition, we are also releasing model checkpoints trained -on the COCO dataset. - -Thanks to contributors: Yongzhe Wang, Bo Chen, Hanxiao Liu, Le An -(NVIDIA), Yu-Te Cheng (NVIDIA), Oliver Knieps (NVIDIA), and Josh Park (NVIDIA). - -### June 17th, 2020 - -We have released [Context R-CNN](https://arxiv.org/abs/1912.03538), a model that -uses attention to incorporate contextual information images (e.g. from -temporally nearby frames taken by a static camera) in order to improve accuracy. -Importantly, these contextual images need not be labeled. - -* When applied to a challenging wildlife detection dataset - ([Snapshot Serengeti](http://lila.science/datasets/snapshot-serengeti)), - Context R-CNN with context from up to a month of images outperforms a - single-frame baseline by 17.9% mAP, and outperforms S3D (a 3d convolution - based baseline) by 11.2% mAP. -* Context R-CNN leverages temporal context from the unlabeled frames of a - novel camera deployment to improve performance at that camera, boosting - model generalizeability. - -Read about Context R-CNN on the Google AI blog -[here](https://ai.googleblog.com/2020/06/leveraging-temporal-context-for-object.html). - -We have provided code for generating data with associated context -[here](context_rcnn.md), and a sample config for a Context R-CNN model -[here](../samples/configs/context_rcnn_resnet101_snapshot_serengeti_sync.config). - -Snapshot Serengeti-trained Faster R-CNN and Context R-CNN models can be found in -the -[model zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf1_detection_zoo.md#snapshot-serengeti-camera-trap-trained-models). - -A colab demonstrating Context R-CNN is provided -[here](../colab_tutorials/context_rcnn_tutorial.ipynb). - -Thanks to contributors: Sara Beery, Jonathan Huang, Guanhang Wu, Vivek -Rathod, Ronny Votel, Zhichao Lu, David Ross, Pietro Perona, Tanya Birch, and the -Wildlife Insights AI Team. - -### May 19th, 2020 - -We have released [MobileDets](https://arxiv.org/abs/2004.14525), a set of -high-performance models for mobile CPUs, DSPs and EdgeTPUs. - -* MobileDets outperform MobileNetV3+SSDLite by 1.7 mAP at comparable mobile - CPU inference latencies. MobileDets also outperform MobileNetV2+SSDLite by - 1.9 mAP on mobile CPUs, 3.7 mAP on EdgeTPUs and 3.4 mAP on DSPs while - running equally fast. MobileDets also offer up to 2x speedup over MnasFPN on - EdgeTPUs and DSPs. - -For each of the three hardware platforms we have released model definition, -model checkpoints trained on the COCO14 dataset and converted TFLite models in -fp32 and/or uint8. - -Thanks to contributors: Yunyang Xiong, Hanxiao Liu, Suyog Gupta, Berkin -Akin, Gabriel Bender, Pieter-Jan Kindermans, Mingxing Tan, Vikas Singh, Bo Chen, -Quoc Le, Zhichao Lu. - -### May 7th, 2020 - -We have released a mobile model with the -[MnasFPN head](https://arxiv.org/abs/1912.01106). - -* MnasFPN with MobileNet-V2 backbone is the most accurate (26.6 mAP at 183ms - on Pixel 1) mobile detection model we have released to date. With - depth-multiplier, MnasFPN with MobileNet-V2 backbone is 1.8 mAP higher than - MobileNet-V3-Large with SSDLite (23.8 mAP vs 22.0 mAP) at similar latency - (120ms) on Pixel 1. - -We have released model definition, model checkpoints trained on the COCO14 -dataset and a converted TFLite model. - -Thanks to contributors: Bo Chen, Golnaz Ghiasi, Hanxiao Liu, Tsung-Yi -Lin, Dmitry Kalenichenko, Hartwig Adam, Quoc Le, Zhichao Lu, Jonathan Huang, Hao -Xu. - -### Nov 13th, 2019 - -We have released MobileNetEdgeTPU SSDLite model. - -* SSDLite with MobileNetEdgeTPU backbone, which achieves 10% mAP higher than - MobileNetV2 SSDLite (24.3 mAP vs 22 mAP) on a Google Pixel4 at comparable - latency (6.6ms vs 6.8ms). - -Along with the model definition, we are also releasing model checkpoints trained -on the COCO dataset. - -Thanks to contributors: Yunyang Xiong, Bo Chen, Suyog Gupta, Hanxiao Liu, -Gabriel Bender, Mingxing Tan, Berkin Akin, Zhichao Lu, Quoc Le - -### Oct 15th, 2019 - -We have released two MobileNet V3 SSDLite models (presented in -[Searching for MobileNetV3](https://arxiv.org/abs/1905.02244)). - -* SSDLite with MobileNet-V3-Large backbone, which is 27% faster than Mobilenet - V2 SSDLite (119ms vs 162ms) on a Google Pixel phone CPU at the same mAP. -* SSDLite with MobileNet-V3-Small backbone, which is 37% faster than MnasNet - SSDLite reduced with depth-multiplier (43ms vs 68ms) at the same mAP. - -Along with the model definition, we are also releasing model checkpoints trained -on the COCO dataset. - -Thanks to contributors: Bo Chen, Zhichao Lu, Vivek Rathod, Jonathan Huang - -### July 1st, 2019 - -We have released an updated set of utils and an updated -[tutorial](challenge_evaluation.md) for all three tracks of the -[Open Images Challenge 2019](https://storage.googleapis.com/openimages/web/challenge2019.html)! - -The Instance Segmentation metric for -[Open Images V5](https://storage.googleapis.com/openimages/web/index.html) and -[Challenge 2019](https://storage.googleapis.com/openimages/web/challenge2019.html) -is part of this release. Check out -[the metric description](https://storage.googleapis.com/openimages/web/evaluation.html#instance_segmentation_eval) -on the Open Images website. - -Thanks to contributors: Alina Kuznetsova, Rodrigo Benenson - -### Feb 11, 2019 - -We have released detection models trained on the Open Images Dataset V4 in our -detection model zoo, including - -* Faster R-CNN detector with Inception Resnet V2 feature extractor -* SSD detector with MobileNet V2 feature extractor -* SSD detector with ResNet 101 FPN feature extractor (aka RetinaNet-101) - -Thanks to contributors: Alina Kuznetsova, Yinxiao Li - -### Sep 17, 2018 - -We have released Faster R-CNN detectors with ResNet-50 / ResNet-101 feature -extractors trained on the -[iNaturalist Species Detection Dataset](https://github.com/visipedia/inat_comp/blob/master/2017/README.md#bounding-boxes). -The models are trained on the training split of the iNaturalist data for 4M -iterations, they achieve 55% and 58% mean AP@.5 over 2854 classes respectively. -For more details please refer to this [paper](https://arxiv.org/abs/1707.06642). - -Thanks to contributors: Chen Sun - -### July 13, 2018 - -There are many new updates in this release, extending the functionality and -capability of the API: - -* Moving from slim-based training to - [Estimator](https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator)-based - training. -* Support for [RetinaNet](https://arxiv.org/abs/1708.02002), and a - [MobileNet](https://ai.googleblog.com/2017/06/mobilenets-open-source-models-for.html) - adaptation of RetinaNet. -* A novel SSD-based architecture called the - [Pooling Pyramid Network](https://arxiv.org/abs/1807.03284) (PPN). -* Releasing several [TPU](https://cloud.google.com/tpu/)-compatible models. - These can be found in the `samples/configs/` directory with a comment in the - pipeline configuration files indicating TPU compatibility. -* Support for quantized training. -* Updated documentation for new binaries, Cloud training, and - [TensorFlow Lite](https://www.tensorflow.org/mobile/tflite/). - -See also our -[expanded announcement blogpost](https://ai.googleblog.com/2018/07/accelerated-training-and-inference-with.html) -and accompanying tutorial at the -[TensorFlow blog](https://medium.com/tensorflow/training-and-serving-a-realtime-mobile-object-detector-in-30-minutes-with-cloud-tpus-b78971cf1193). - -Thanks to contributors: Sara Robinson, Aakanksha Chowdhery, Derek Chow, -Pengchong Jin, Jonathan Huang, Vivek Rathod, Zhichao Lu, Ronny Votel - -### June 25, 2018 - -Additional evaluation tools for the -[Open Images Challenge 2018](https://storage.googleapis.com/openimages/web/challenge.html) -are out. Check out our short tutorial on data preparation and running evaluation -[here](challenge_evaluation.md)! - -Thanks to contributors: Alina Kuznetsova - -### June 5, 2018 - -We have released the implementation of evaluation metrics for both tracks of the -[Open Images Challenge 2018](https://storage.googleapis.com/openimages/web/challenge.html) -as a part of the Object Detection API - see the -[evaluation protocols](evaluation_protocols.md) for more details. Additionally, -we have released a tool for hierarchical labels expansion for the Open Images -Challenge: check out -[oid_hierarchical_labels_expansion.py](../dataset_tools/oid_hierarchical_labels_expansion.py). - -Thanks to contributors: Alina Kuznetsova, Vittorio Ferrari, Jasper -Uijlings - -### April 30, 2018 - -We have released a Faster R-CNN detector with ResNet-101 feature extractor -trained on [AVA](https://research.google.com/ava/) v2.1. Compared with other -commonly used object detectors, it changes the action classification loss -function to per-class Sigmoid loss to handle boxes with multiple labels. The -model is trained on the training split of AVA v2.1 for 1.5M iterations, it -achieves mean AP of 11.25% over 60 classes on the validation split of AVA v2.1. -For more details please refer to this [paper](https://arxiv.org/abs/1705.08421). - -Thanks to contributors: Chen Sun, David Ross - -### April 2, 2018 - -Supercharge your mobile phones with the next generation mobile object detector! -We are adding support for MobileNet V2 with SSDLite presented in -[MobileNetV2: Inverted Residuals and Linear Bottlenecks](https://arxiv.org/abs/1801.04381). -This model is 35% faster than Mobilenet V1 SSD on a Google Pixel phone CPU -(200ms vs. 270ms) at the same accuracy. Along with the model definition, we are -also releasing a model checkpoint trained on the COCO dataset. - -Thanks to contributors: Menglong Zhu, Mark Sandler, Zhichao Lu, Vivek -Rathod, Jonathan Huang - -### February 9, 2018 - -We now support instance segmentation!! In this API update we support a number of -instance segmentation models similar to those discussed in the -[Mask R-CNN paper](https://arxiv.org/abs/1703.06870). For further details refer -to [our slides](http://presentations.cocodataset.org/Places17-GMRI.pdf) from the -2017 Coco + Places Workshop. Refer to the section on -[Running an Instance Segmentation Model](instance_segmentation.md) for -instructions on how to configure a model that predicts masks in addition to -object bounding boxes. - -Thanks to contributors: Alireza Fathi, Zhichao Lu, Vivek Rathod, Ronny -Votel, Jonathan Huang - -### November 17, 2017 - -As a part of the Open Images V3 release we have released: - -* An implementation of the Open Images evaluation metric and the - [protocol](evaluation_protocols.md#open-images). -* Additional tools to separate inference of detection and evaluation (see - [this tutorial](oid_inference_and_evaluation.md)). -* A new detection model trained on the Open Images V2 data release (see - [Open Images model](tf1_detection_zoo.md#open-images-models)). - -See more information on the -[Open Images website](https://github.com/openimages/dataset)! - -Thanks to contributors: Stefan Popov, Alina Kuznetsova - -### November 6, 2017 - -We have re-released faster versions of our (pre-trained) models in the -model zoo. In addition to what was available -before, we are also adding Faster R-CNN models trained on COCO with Inception V2 -and Resnet-50 feature extractors, as well as a Faster R-CNN with Resnet-101 -model trained on the KITTI dataset. - -Thanks to contributors: Jonathan Huang, Vivek Rathod, Derek Chow, Tal -Remez, Chen Sun. - -### October 31, 2017 - -We have released a new state-of-the-art model for object detection using the -Faster-RCNN with the -[NASNet-A image featurization](https://arxiv.org/abs/1707.07012). This model -achieves mAP of 43.1% on the test-dev validation dataset for COCO, improving on -the best available model in the zoo by 6% in terms of absolute mAP. - -Thanks to contributors: Barret Zoph, Vijay Vasudevan, Jonathon Shlens, -Quoc Le - -### August 11, 2017 - -We have released an update to the -[Android Detect demo](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android) -which will now run models trained using the TensorFlow Object Detection API on -an Android device. By default, it currently runs a frozen SSD w/Mobilenet -detector trained on COCO, but we encourage you to try out other detection -models! - -Thanks to contributors: Jonathan Huang, Andrew Harp - -### June 15, 2017 - -In addition to our base TensorFlow detection model definitions, this release -includes: - -* A selection of trainable detection models, including: - * Single Shot Multibox Detector (SSD) with MobileNet, - * SSD with Inception V2, - * Region-Based Fully Convolutional Networks (R-FCN) with Resnet 101, - * Faster RCNN with Resnet 101, - * Faster RCNN with Inception Resnet v2 -* Frozen weights (trained on the COCO dataset) for each of the above models to - be used for out-of-the-box inference purposes. -* A [Jupyter notebook](../colab_tutorials/object_detection_tutorial.ipynb) for - performing out-of-the-box inference with one of our released models -* Convenient training and evaluation - [instructions](tf1_training_and_evaluation.md) for local runs and Google - Cloud. - -Thanks to contributors: Jonathan Huang, Vivek Rathod, Derek Chow, Chen -Sun, Menglong Zhu, Matthew Tang, Anoop Korattikara, Alireza Fathi, Ian Fischer, -Zbigniew Wojna, Yang Song, Sergio Guadarrama, Jasper Uijlings, Viacheslav -Kovalevskyi, Kevin Murphy diff --git a/research/object_detection/g3doc/running_notebook.md b/research/object_detection/g3doc/running_notebook.md deleted file mode 100644 index b92aec33aa1..00000000000 --- a/research/object_detection/g3doc/running_notebook.md +++ /dev/null @@ -1,18 +0,0 @@ -# Quick Start: Jupyter notebook for off-the-shelf inference - -[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -If you'd like to hit the ground running and run detection on a few example -images right out of the box, we recommend trying out the Jupyter notebook demo. -To run the Jupyter notebook, run the following command from -`tensorflow/models/research/object_detection`: - -``` -# From tensorflow/models/research/object_detection -jupyter notebook -``` - -The notebook should open in your favorite web browser. Click the -[`object_detection_tutorial.ipynb`](../object_detection_tutorial.ipynb) link to -open the demo. diff --git a/research/object_detection/g3doc/running_on_mobile_tensorflowlite.md b/research/object_detection/g3doc/running_on_mobile_tensorflowlite.md deleted file mode 100644 index 0d670873dcc..00000000000 --- a/research/object_detection/g3doc/running_on_mobile_tensorflowlite.md +++ /dev/null @@ -1,159 +0,0 @@ -# Running on mobile with TensorFlow Lite - -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -In this section, we will show you how to use [TensorFlow -Lite](https://www.tensorflow.org/mobile/tflite/) to get a smaller model and -allow you take advantage of ops that have been optimized for mobile devices. -TensorFlow Lite is TensorFlow’s lightweight solution for mobile and embedded -devices. It enables on-device machine learning inference with low latency and a -small binary size. TensorFlow Lite uses many techniques for this such as -quantized kernels that allow smaller and faster (fixed-point math) models. - -For this section, you will need to build -[TensorFlow from source](https://www.tensorflow.org/install/install_sources) to -get the TensorFlow Lite support for the SSD model. At this time only SSD models -are supported. Models like faster_rcnn are not supported at this time. You will -also need to install the -[bazel build tool](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/examples/android#bazel). - -To make these commands easier to run, let’s set up some environment variables: - -```shell -export CONFIG_FILE=PATH_TO_BE_CONFIGURED/pipeline.config -export CHECKPOINT_PATH=PATH_TO_BE_CONFIGURED/model.ckpt -export OUTPUT_DIR=/tmp/tflite -``` - -We start with a checkpoint and get a TensorFlow frozen graph with compatible ops -that we can use with TensorFlow Lite. First, you’ll need to install these -[python -libraries](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md). -Then to get the frozen graph, run the export_tflite_ssd_graph.py script from the -`models/research` directory with this command: - -```shell -object_detection/export_tflite_ssd_graph.py \ ---pipeline_config_path=$CONFIG_FILE \ ---trained_checkpoint_prefix=$CHECKPOINT_PATH \ ---output_directory=$OUTPUT_DIR \ ---add_postprocessing_op=true -``` - -In the /tmp/tflite directory, you should now see two files: tflite_graph.pb and -tflite_graph.pbtxt. Note that the add_postprocessing flag enables the model to -take advantage of a custom optimized detection post-processing operation which -can be thought of as a replacement for -[tf.image.non_max_suppression](https://www.tensorflow.org/api_docs/python/tf/image/non_max_suppression). -Make sure not to confuse export_tflite_ssd_graph with export_inference_graph in -the same directory. Both scripts output frozen graphs: export_tflite_ssd_graph -will output the frozen graph that we can input to TensorFlow Lite directly and -is the one we’ll be using. - -Next we’ll use TensorFlow Lite to get the optimized model by using -[TfLite Converter](https://www.tensorflow.org/lite/convert), -the TensorFlow Lite Optimizing Converter. This will convert the resulting frozen -graph (tflite_graph.pb) to the TensorFlow Lite flatbuffer format (detect.tflite) -via the following command. For a quantized model, run this from the tensorflow/ -directory: - -```shell -bazel run -c opt tensorflow/lite/python:tflite_convert -- \ ---enable_v1_converter \ ---graph_def_file=$OUTPUT_DIR/tflite_graph.pb \ ---output_file=$OUTPUT_DIR/detect.tflite \ ---input_shapes=1,300,300,3 \ ---input_arrays=normalized_input_image_tensor \ ---output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' \ ---inference_type=QUANTIZED_UINT8 \ ---mean_values=128 \ ---std_dev_values=128 \ ---change_concat_input_ranges=false \ ---allow_custom_ops -``` - -This command takes the input tensor normalized_input_image_tensor after resizing -each camera image frame to 300x300 pixels. The outputs of the quantized model -are named 'TFLite_Detection_PostProcess', 'TFLite_Detection_PostProcess:1', -'TFLite_Detection_PostProcess:2', and 'TFLite_Detection_PostProcess:3' and -represent four arrays: detection_boxes, detection_classes, detection_scores, and -num_detections. The documentation for other flags used in this command is -[here](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/convert/cmdline.md). -If things ran successfully, you should now see a third file in the /tmp/tflite -directory called detect.tflite. This file contains the graph and all model -parameters and can be run via the TensorFlow Lite interpreter on the Android -device. For a floating point model, run this from the tensorflow/ directory: - -```shell -bazel run -c opt tensorflow/lite/python:tflite_convert -- \ ---enable_v1_converter \ ---graph_def_file=$OUTPUT_DIR/tflite_graph.pb \ ---output_file=$OUTPUT_DIR/detect.tflite \ ---input_shapes=1,300,300,3 \ ---input_arrays=normalized_input_image_tensor \ ---output_arrays='TFLite_Detection_PostProcess','TFLite_Detection_PostProcess:1','TFLite_Detection_PostProcess:2','TFLite_Detection_PostProcess:3' \ ---inference_type=FLOAT \ ---allow_custom_ops -``` - -## Adding Metadata to the model - -To make it easier to use tflite models on mobile, you will need to add -[metadata](https://www.tensorflow.org/lite/convert/metadata) to your model and -also -[pack](https://www.tensorflow.org/lite/convert/metadata#pack_metadata_and_associated_files_into_the_model) -the associated labels file to it. -If you need more information, this process is also explained in the -[Metadata writer Object detectors documentation](https://www.tensorflow.org/lite/convert/metadata_writer_tutorial#object_detectors) - -## Running our model on Android - -To run our TensorFlow Lite model on device, we will use Android Studio to build -and run the TensorFlow Lite detection example with the new model. The example is -found in the -[TensorFlow examples repository](https://github.com/tensorflow/examples) under -`/lite/examples/object_detection`. The example can be built with -[Android Studio](https://developer.android.com/studio/index.html), and requires -the -[Android SDK with build tools](https://developer.android.com/tools/revisions/build-tools.html) -that support API >= 21. Additional details are available on the -[TensorFlow Lite example page](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android). - -Next we need to point the app to our new detect.tflite file and give it the -names of our new labels. Specifically, we will copy our TensorFlow Lite -flatbuffer to the app assets directory with the following command: - -```shell -mkdir $TF_EXAMPLES/lite/examples/object_detection/android/app/src/main/assets -cp /tmp/tflite/detect.tflite \ - $TF_EXAMPLES/lite/examples/object_detection/android/app/src/main/assets -``` - -It's important to notice that the labels file should be packed in the model (as -mentioned previously) - -We will now edit the gradle build file to use these assets. First, open the -`build.gradle` file -`$TF_EXAMPLES/lite/examples/object_detection/android/app/build.gradle`. Comment -out the model download script to avoid your assets being overwritten: `// apply -from:'download_model.gradle'` ``` - -If your model is named `detect.tflite`, the example will use it automatically as -long as they've been properly copied into the base assets directory. If you need -to use a custom path or filename, open up the -$TF_EXAMPLES/lite/examples/object_detection/android/app/src/main/java/org/tensorflow/demo/DetectorActivity.java -file in a text editor and find the definition of TF_OD_API_MODEL_FILE. Note that -if your model is quantized, the flag TF_OD_API_IS_QUANTIZED is set to true, and -if your model is floating point, the flag TF_OD_API_IS_QUANTIZED is set to -false. This new section of DetectorActivity.java should now look as follows for -a quantized model: - -```shell - private static final boolean TF_OD_API_IS_QUANTIZED = true; - private static final String TF_OD_API_MODEL_FILE = "detect.tflite"; - private static final String TF_OD_API_LABELS_FILE = "labels_list.txt"; -``` - -Once you’ve copied the TensorFlow Lite model and edited the gradle build script -to not use the downloaded assets, you can build and deploy the app using the -usual Android Studio build process. diff --git a/research/object_detection/g3doc/running_on_mobile_tf2.md b/research/object_detection/g3doc/running_on_mobile_tf2.md deleted file mode 100644 index 69203564f81..00000000000 --- a/research/object_detection/g3doc/running_on_mobile_tf2.md +++ /dev/null @@ -1,170 +0,0 @@ -# Running TF2 Detection API Models on mobile - -[![TensorFlow 2.4](https://img.shields.io/badge/TensorFlow-2.4-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.4.0) -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) - -**NOTE:** This document talks about the *SSD* models in the detection zoo. For -details on our (experimental) CenterNet support, see -[this notebook](../colab_tutorials/centernet_on_device.ipynb). - -[TensorFlow Lite](https://www.tensorflow.org/mobile/tflite/)(TFLite) is -TensorFlow’s lightweight solution for mobile and embedded devices. It enables -on-device machine learning inference with low latency and a small binary size. -TensorFlow Lite uses many techniques for this such as quantized kernels that -allow smaller and faster (fixed-point math) models. - -This document shows how eligible models from the -[TF2 Detection zoo](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md) -can be converted for inference with TFLite. See this Colab tutorial for a -runnable tutorial that walks you through the steps explained in this document: - -Run -in Google Colab - -For an end-to-end Python guide on how to fine-tune an SSD model for mobile -inference, look at -[this Colab](../colab_tutorials/eager_few_shot_od_training_tflite.ipynb). - -**NOTE:** TFLite currently only supports **SSD Architectures** (excluding -EfficientDet) for boxes-based detection. Support for EfficientDet is provided -via the [TFLite Model Maker](https://www.tensorflow.org/lite/tutorials/model_maker_object_detection) -library. - -The output model has the following inputs & outputs: - -``` -One input: - image: a float32 tensor of shape[1, height, width, 3] containing the - *normalized* input image. - NOTE: See the `preprocess` function defined in the feature extractor class - in the object_detection/models directory. - -Four Outputs: - detection_boxes: a float32 tensor of shape [1, num_boxes, 4] with box - locations - detection_classes: a float32 tensor of shape [1, num_boxes] - with class indices - detection_scores: a float32 tensor of shape [1, num_boxes] - with class scores - num_boxes: a float32 tensor of size 1 containing the number of detected boxes -``` - -There are two steps to TFLite conversion: - -### Step 1: Export TFLite inference graph - -This step generates an intermediate SavedModel that can be used with the -[TFLite Converter](https://www.tensorflow.org/lite/convert) via commandline or -Python API. - -To use the script: - -```bash -# From the tensorflow/models/research/ directory -python object_detection/export_tflite_graph_tf2.py \ - --pipeline_config_path path/to/ssd_model/pipeline.config \ - --trained_checkpoint_dir path/to/ssd_model/checkpoint \ - --output_directory path/to/exported_model_directory -``` - -Use `--help` with the above script to get the full list of supported parameters. -These can fine-tune accuracy and speed for your model. - -### Step 2: Convert to TFLite - -Use the [TensorFlow Lite Converter](https://www.tensorflow.org/lite/convert) to -convert the `SavedModel` to TFLite. Note that you need to use `from_saved_model` -for TFLite conversion with the Python API. - -You can also leverage -[Post-training Quantization](https://www.tensorflow.org/lite/performance/post_training_quantization) -to -[optimize performance](https://www.tensorflow.org/lite/performance/model_optimization) -and obtain a smaller model. Note that this is only possible from the *Python -API*. Be sure to use a -[representative dataset](https://www.tensorflow.org/lite/performance/post_training_quantization#full_integer_quantization) -and set the following options on the converter: - -```python -converter.optimizations = [tf.lite.Optimize.DEFAULT] -converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8, - tf.lite.OpsSet.TFLITE_BUILTINS] -converter.representative_dataset = <...> -``` - -### Step 3: add Metadata to the model - -To make it easier to use tflite models on mobile, you will need to add -[metadata](https://www.tensorflow.org/lite/convert/metadata) to your model and -also -[pack](https://www.tensorflow.org/lite/convert/metadata#pack_metadata_and_associated_files_into_the_model) -the associated labels file to it. -If you need more information, This process is also explained in the -[Image classification sample](https://github.com/tensorflow/examples/tree/master/lite/examples/image_classification/metadata) - -## Running our model on Android - -### Integrate the model into your app -You can use the TFLite Task Library's [ObjectDetector API](https://www.tensorflow.org/lite/inference_with_metadata/task_library/object_detector) -to integrate the model into your Android app. - -```java -// Initialization -ObjectDetectorOptions options = ObjectDetectorOptions.builder().setMaxResults(1).build(); -ObjectDetector objectDetector = ObjectDetector.createFromFileAndOptions(context, modelFile, options); - -// Run inference -List results = objectDetector.detect(image); -``` - -### Test the model using the TFLite sample app - -To test our TensorFlow Lite model on device, we will use Android Studio to build -and run the TensorFlow Lite detection example with the new model. The example is -found in the -[TensorFlow examples repository](https://github.com/tensorflow/examples) under -`/lite/examples/object_detection`. The example can be built with -[Android Studio](https://developer.android.com/studio/index.html), and requires -the -[Android SDK with build tools](https://developer.android.com/tools/revisions/build-tools.html) -that support API >= 21. Additional details are available on the -[TensorFlow Lite example page](https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android). - -Next we need to point the app to our new detect.tflite file . Specifically, we -will copy our TensorFlow Lite flatbuffer to the app assets directory with the -following command: - -```shell -mkdir $TF_EXAMPLES/lite/examples/object_detection/android/app/src/main/assets -cp /tmp/tflite/detect.tflite \ - $TF_EXAMPLES/lite/examples/object_detection/android/app/src/main/assets -``` - -It's important to notice that the labels file should be packed in the model (as -mentioned on Step 3) - -We will now edit the gradle build file to use these assets. First, open the -`build.gradle` file -`$TF_EXAMPLES/lite/examples/object_detection/android/app/build.gradle`. Comment -out the model download script to avoid your assets being overwritten: `// apply -from:'download_model.gradle'` ``` - -If your model is named `detect.tflite`, the example will use it automatically as -long as they've been properly copied into the base assets directory. If you need -to use a custom path or filename, open up the -$TF_EXAMPLES/lite/examples/object_detection/android/app/src/main/java/org/tensorflow/demo/DetectorActivity.java -file in a text editor and find the definition of TF_OD_API_MODEL_FILE. Note that -if your model is quantized, the flag TF_OD_API_IS_QUANTIZED is set to true, and -if your model is floating point, the flag TF_OD_API_IS_QUANTIZED is set to -false. This new section of DetectorActivity.java should now look as follows for -a quantized model: - -```shell - private static final boolean TF_OD_API_IS_QUANTIZED = true; - private static final String TF_OD_API_MODEL_FILE = "detect.tflite"; - private static final String TF_OD_API_LABELS_FILE = "labels_list.txt"; -``` - -Once you’ve copied the TensorFlow Lite model and edited the gradle build script -to not use the downloaded assets, you can build and deploy the app using the -usual Android Studio build process. diff --git a/research/object_detection/g3doc/running_pets.md b/research/object_detection/g3doc/running_pets.md deleted file mode 100644 index 7d6b7bfa7c0..00000000000 --- a/research/object_detection/g3doc/running_pets.md +++ /dev/null @@ -1,321 +0,0 @@ -# Quick Start: Distributed Training on the Oxford-IIIT Pets Dataset on Google Cloud - -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -This page is a walkthrough for training an object detector using the TensorFlow -Object Detection API. In this tutorial, we'll be training on the Oxford-IIIT Pets -dataset to build a system to detect various breeds of cats and dogs. The output -of the detector will look like the following: - -![](img/oxford_pet.png) - -## Setting up a Project on Google Cloud - -To accelerate the process, we'll run training and evaluation on [Google Cloud -ML Engine](https://cloud.google.com/ml-engine/) to leverage multiple GPUs. To -begin, you will have to set up Google Cloud via the following steps (if you have -already done this, feel free to skip to the next section): - -1. [Create a GCP project](https://cloud.google.com/resource-manager/docs/creating-managing-projects). -2. [Install the Google Cloud SDK](https://cloud.google.com/sdk/downloads) on -your workstation or laptop. -This will provide the tools you need to upload files to Google Cloud Storage and -start ML training jobs. -3. [Enable the ML Engine -APIs](https://console.cloud.google.com/flows/enableapi?apiid=ml.googleapis.com,compute_component&_ga=1.73374291.1570145678.1496689256). -By default, a new GCP project does not enable APIs to start ML Engine training -jobs. Use the above link to explicitly enable them. -4. [Set up a Google Cloud Storage (GCS) -bucket](https://cloud.google.com/storage/docs/creating-buckets). ML Engine -training jobs can only access files on a Google Cloud Storage bucket. In this -tutorial, we'll be required to upload our dataset and configuration to GCS. - -Please remember the name of your GCS bucket, as we will reference it multiple -times in this document. Substitute `${YOUR_GCS_BUCKET}` with the name of -your bucket in this document. For your convenience, you should define the -environment variable below: - -``` bash -export YOUR_GCS_BUCKET=${YOUR_GCS_BUCKET} -``` - -It is also possible to run locally by following -[the running locally instructions](running_locally.md). - -## Installing TensorFlow and the TensorFlow Object Detection API - -Please run through the [installation instructions](installation.md) to install -TensorFlow and all it dependencies. Ensure the Protobuf libraries are -compiled and the library directories are added to `PYTHONPATH`. - -## Getting the Oxford-IIIT Pets Dataset and Uploading it to Google Cloud Storage - -In order to train a detector, we require a dataset of images, bounding boxes and -classifications. For this demo, we'll use the Oxford-IIIT Pets dataset. The raw -dataset for Oxford-IIIT Pets lives -[here](http://www.robots.ox.ac.uk/~vgg/data/pets/). You will need to download -both the image dataset [`images.tar.gz`](http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz) -and the groundtruth data [`annotations.tar.gz`](http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz) -to the `tensorflow/models/research/` directory and unzip them. This may take -some time. - -``` bash -# From tensorflow/models/research/ -wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz -wget http://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz -tar -xvf images.tar.gz -tar -xvf annotations.tar.gz -``` - -After downloading the tarballs, your `tensorflow/models/research/` directory -should appear as follows: - -```lang-none -- images.tar.gz -- annotations.tar.gz -+ images/ -+ annotations/ -+ object_detection/ -... other files and directories -``` - -The TensorFlow Object Detection API expects data to be in the TFRecord format, -so we'll now run the `create_pet_tf_record` script to convert from the raw -Oxford-IIIT Pet dataset into TFRecords. Run the following commands from the -`tensorflow/models/research/` directory: - -``` bash -# From tensorflow/models/research/ -python object_detection/dataset_tools/create_pet_tf_record.py \ - --label_map_path=object_detection/data/pet_label_map.pbtxt \ - --data_dir=`pwd` \ - --output_dir=`pwd` -``` - -Note: It is normal to see some warnings when running this script. You may ignore -them. - -Two 10-sharded TFRecord files named `pet_faces_train.record-*` and -`pet_faces_val.record-*` should be generated in the -`tensorflow/models/research/` directory. - -Now that the data has been generated, we'll need to upload it to Google Cloud -Storage so the data can be accessed by ML Engine. Run the following command to -copy the files into your GCS bucket (substituting `${YOUR_GCS_BUCKET}`): - -```bash -# From tensorflow/models/research/ -gsutil cp pet_faces_train.record-* gs://${YOUR_GCS_BUCKET}/data/ -gsutil cp pet_faces_val.record-* gs://${YOUR_GCS_BUCKET}/data/ -gsutil cp object_detection/data/pet_label_map.pbtxt gs://${YOUR_GCS_BUCKET}/data/pet_label_map.pbtxt -``` - -Please remember the path where you upload the data to, as we will need this -information when configuring the pipeline in a following step. - -## Downloading a COCO-pretrained Model for Transfer Learning - -Training a state of the art object detector from scratch can take days, even -when using multiple GPUs! In order to speed up training, we'll take an object -detector trained on a different dataset (COCO), and reuse some of it's -parameters to initialize our new model. - -Download our [COCO-pretrained Faster R-CNN with Resnet-101 -model](http://storage.googleapis.com/download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_11_06_2017.tar.gz). -Unzip the contents of the folder and copy the `model.ckpt*` files into your GCS -Bucket. - -``` bash -wget http://storage.googleapis.com/download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_11_06_2017.tar.gz -tar -xvf faster_rcnn_resnet101_coco_11_06_2017.tar.gz -gsutil cp faster_rcnn_resnet101_coco_11_06_2017/model.ckpt.* gs://${YOUR_GCS_BUCKET}/data/ -``` - -Remember the path where you uploaded the model checkpoint to, as we will need it -in the following step. - -## Configuring the Object Detection Pipeline - -In the TensorFlow Object Detection API, the model parameters, training -parameters and eval parameters are all defined by a config file. More details -can be found [here](configuring_jobs.md). For this tutorial, we will use some -predefined templates provided with the source code. In the -`object_detection/samples/configs` folder, there are skeleton object_detection -configuration files. We will use `faster_rcnn_resnet101_pets.config` as a -starting point for configuring the pipeline. Open the file with your favourite -text editor. - -We'll need to configure some paths in order for the template to work. Search the -file for instances of `PATH_TO_BE_CONFIGURED` and replace them with the -appropriate value (typically `gs://${YOUR_GCS_BUCKET}/data/`). Afterwards -upload your edited file onto GCS, making note of the path it was uploaded to -(we'll need it when starting the training/eval jobs). - -``` bash -# From tensorflow/models/research/ - -# Edit the faster_rcnn_resnet101_pets.config template. Please note that there -# are multiple places where PATH_TO_BE_CONFIGURED needs to be set. -sed -i "s|PATH_TO_BE_CONFIGURED|"gs://${YOUR_GCS_BUCKET}"/data|g" \ - object_detection/samples/configs/faster_rcnn_resnet101_pets.config - -# Copy edited template to cloud. -gsutil cp object_detection/samples/configs/faster_rcnn_resnet101_pets.config \ - gs://${YOUR_GCS_BUCKET}/data/faster_rcnn_resnet101_pets.config -``` - -## Checking Your Google Cloud Storage Bucket - -At this point in the tutorial, you should have uploaded the training/validation -datasets (including label map), our COCO trained FasterRCNN finetune checkpoint and your job -configuration to your Google Cloud Storage Bucket. Your bucket should look like -the following: - -```lang-none -+ ${YOUR_GCS_BUCKET}/ - + data/ - - faster_rcnn_resnet101_pets.config - - model.ckpt.index - - model.ckpt.meta - - model.ckpt.data-00000-of-00001 - - pet_label_map.pbtxt - - pet_faces_train.record-* - - pet_faces_val.record-* -``` - -You can inspect your bucket using the [Google Cloud Storage -browser](https://console.cloud.google.com/storage/browser). - -## Starting Training and Evaluation Jobs on Google Cloud ML Engine - -Before we can start a job on Google Cloud ML Engine, we must: - -1. Package the TensorFlow Object Detection code. -2. Write a cluster configuration for our Google Cloud ML job. - -To package the TensorFlow Object Detection code, run the following commands from -the `tensorflow/models/research/` directory: - -```bash -# From tensorflow/models/research/ -bash object_detection/dataset_tools/create_pycocotools_package.sh /tmp/pycocotools -python setup.py sdist -(cd slim && python setup.py sdist) -``` - -This will create python packages dist/object_detection-0.1.tar.gz, -slim/dist/slim-0.1.tar.gz, and /tmp/pycocotools/pycocotools-2.0.tar.gz. - -For running the training Cloud ML job, we'll configure the cluster to use 5 -training jobs and three parameters servers. The -configuration file can be found at `object_detection/samples/cloud/cloud.yml`. - -Note: The code sample below is supported for use with 1.12 runtime version. - -To start training and evaluation, execute the following command from the -`tensorflow/models/research/` directory: - -```bash -# From tensorflow/models/research/ -gcloud ml-engine jobs submit training `whoami`_object_detection_pets_`date +%m_%d_%Y_%H_%M_%S` \ - --runtime-version 1.12 \ - --job-dir=gs://${YOUR_GCS_BUCKET}/model_dir \ - --packages dist/object_detection-0.1.tar.gz,slim/dist/slim-0.1.tar.gz,/tmp/pycocotools/pycocotools-2.0.tar.gz \ - --module-name object_detection.model_main \ - --region us-central1 \ - --config object_detection/samples/cloud/cloud.yml \ - -- \ - --model_dir=gs://${YOUR_GCS_BUCKET}/model_dir \ - --pipeline_config_path=gs://${YOUR_GCS_BUCKET}/data/faster_rcnn_resnet101_pets.config -``` - -Users can monitor and stop training and evaluation jobs on the [ML Engine -Dashboard](https://console.cloud.google.com/mlengine/jobs). - -## Monitoring Progress with Tensorboard - -You can monitor progress of the training and eval jobs by running Tensorboard on -your local machine: - -```bash -# This command needs to be run once to allow your local machine to access your -# GCS bucket. -gcloud auth application-default login - -tensorboard --logdir=gs://${YOUR_GCS_BUCKET}/model_dir -``` - -Once Tensorboard is running, navigate to `localhost:6006` from your favourite -web browser. You should see something similar to the following: - -![](img/tensorboard.png) - -Make sure your Tensorboard version is the same minor version as your TensorFlow (1.x) - -You will also want to click on the images tab to see example detections made by -the model while it trains. After about an hour and a half of training, you can -expect to see something like this: - -![](img/tensorboard2.png) - -Note: It takes roughly 10 minutes for a job to get started on ML Engine, and -roughly an hour for the system to evaluate the validation dataset. It may take -some time to populate the dashboards. If you do not see any entries after half -an hour, check the logs from the [ML Engine -Dashboard](https://console.cloud.google.com/mlengine/jobs). Note that by default -the training jobs are configured to go for much longer than is necessary for -convergence. To save money, we recommend killing your jobs once you've seen -that they've converged. - -## Exporting the TensorFlow Graph - -After your model has been trained, you should export it to a TensorFlow graph -proto. First, you need to identify a candidate checkpoint to export. You can -search your bucket using the [Google Cloud Storage -Browser](https://console.cloud.google.com/storage/browser). The file should be -stored under `${YOUR_GCS_BUCKET}/model_dir`. The checkpoint will typically -consist of three files: - -* `model.ckpt-${CHECKPOINT_NUMBER}.data-00000-of-00001` -* `model.ckpt-${CHECKPOINT_NUMBER}.index` -* `model.ckpt-${CHECKPOINT_NUMBER}.meta` - -After you've identified a candidate checkpoint to export, run the following -command from `tensorflow/models/research/`: - -```bash -# From tensorflow/models/research/ -gsutil cp gs://${YOUR_GCS_BUCKET}/model_dir/model.ckpt-${CHECKPOINT_NUMBER}.* . -python object_detection/export_inference_graph.py \ - --input_type image_tensor \ - --pipeline_config_path object_detection/samples/configs/faster_rcnn_resnet101_pets.config \ - --trained_checkpoint_prefix model.ckpt-${CHECKPOINT_NUMBER} \ - --output_directory exported_graphs -``` - -Afterwards, you should see a directory named `exported_graphs` containing the -SavedModel and frozen graph. - -## Configuring the Instance Segmentation Pipeline - -Mask prediction can be turned on for an object detection config by adding -`predict_instance_masks: true` within the `MaskRCNNBoxPredictor`. Other -parameters such as mask size, number of convolutions in the mask layer, and the -convolution hyper parameters can be defined. We will use -`mask_rcnn_resnet101_pets.config` as a starting point for configuring the -instance segmentation pipeline. Everything above that was mentioned about object -detection holds true for instance segmentation. Instance segmentation consists -of an object detection model with an additional head that predicts the object -mask inside each predicted box once we remove the training and other details. -Please refer to the section on [Running an Instance Segmentation -Model](instance_segmentation.md) for instructions on how to configure a model -that predicts masks in addition to object bounding boxes. - -## What's Next - -Congratulations, you have now trained an object detector for various cats and -dogs! There different things you can do now: - -1. [Test your exported model using the provided Jupyter notebook.](running_notebook.md) -2. [Experiment with different model configurations.](configuring_jobs.md) -3. Train an object detector using your own data. diff --git a/research/object_detection/g3doc/tf1.md b/research/object_detection/g3doc/tf1.md deleted file mode 100644 index f1577600963..00000000000 --- a/research/object_detection/g3doc/tf1.md +++ /dev/null @@ -1,94 +0,0 @@ -# Object Detection API with TensorFlow 1 - -## Requirements - -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) -[![Protobuf Compiler >= 3.0](https://img.shields.io/badge/ProtoBuf%20Compiler-%3E3.0-brightgreen)](https://grpc.io/docs/protoc-installation/#install-using-a-package-manager) - -## Installation - -You can install the TensorFlow Object Detection API either with Python Package -Installer (pip) or Docker. For local runs we recommend using Docker and for -Google Cloud runs we recommend using pip. - -Clone the TensorFlow Models repository and proceed to one of the installation -options. - -```bash -git clone https://github.com/tensorflow/models.git -``` - -### Docker Installation - -```bash -# From the root of the git repository -docker build -f research/object_detection/dockerfiles/tf1/Dockerfile -t od . -docker run -it od -``` - -### Python Package Installation - -```bash -cd models/research -# Compile protos. -protoc object_detection/protos/*.proto --python_out=. -# Install TensorFlow Object Detection API. -cp object_detection/packages/tf1/setup.py . -python -m pip install --use-feature=2020-resolver . -``` - -```bash -# Test the installation. -python object_detection/builders/model_builder_tf1_test.py -``` - -## Quick Start - -### Colabs - -* [Jupyter notebook for off-the-shelf inference](../colab_tutorials/object_detection_tutorial.ipynb) -* [Training a pet detector](running_pets.md) - -### Training and Evaluation - -To train and evaluate your models either locally or on Google Cloud see -[instructions](tf1_training_and_evaluation.md). - -## Model Zoo - -We provide a large collection of models that are trained on several datasets in -the [Model Zoo](tf1_detection_zoo.md). - -## Guides - -* - Configuring an object detection pipeline
-* Preparing inputs
-* - Defining your own model architecture
-* - Bringing in your own dataset
-* - Supported object detection evaluation protocols
-* - TPU compatible detection pipelines
-* - Training and evaluation guide (CPU, GPU, or TPU)
- -## Extras: - -* - Exporting a trained model for inference
-* - Exporting a trained model for TPU inference
-* - Inference and evaluation on the Open Images dataset
-* - Run an instance segmentation model
-* - Run the evaluation for the Open Images Challenge 2018/2019
-* - Running object detection on mobile devices with TensorFlow Lite
-* - Context R-CNN documentation for data preparation, training, and export
diff --git a/research/object_detection/g3doc/tf1_detection_zoo.md b/research/object_detection/g3doc/tf1_detection_zoo.md deleted file mode 100644 index c4982b2cf79..00000000000 --- a/research/object_detection/g3doc/tf1_detection_zoo.md +++ /dev/null @@ -1,198 +0,0 @@ -# TensorFlow 1 Detection Model Zoo - -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) - -We provide a collection of detection models pre-trained on the -[COCO dataset](http://cocodataset.org), the -[Kitti dataset](http://www.cvlibs.net/datasets/kitti/), the -[Open Images dataset](https://storage.googleapis.com/openimages/web/index.html), -the [AVA v2.1 dataset](https://research.google.com/ava/) the -[iNaturalist Species Detection Dataset](https://github.com/visipedia/inat_comp/blob/master/2017/README.md#bounding-boxes) -and the -[Snapshot Serengeti Dataset](http://lila.science/datasets/snapshot-serengeti). -These models can be useful for out-of-the-box inference if you are interested in -categories already in those datasets. They are also useful for initializing your -models when training on novel datasets. - -In the table below, we list each such pre-trained model including: - -* a model name that corresponds to a config file that was used to train this - model in the `samples/configs` directory, -* a download link to a tar.gz file containing the pre-trained model, -* model speed --- we report running time in ms per 600x600 image (including - all pre and post-processing), but please be aware that these timings depend - highly on one's specific hardware configuration (these timings were - performed using an Nvidia GeForce GTX TITAN X card) and should be treated - more as relative timings in many cases. Also note that desktop GPU timing - does not always reflect mobile run time. For example Mobilenet V2 is faster - on mobile devices than Mobilenet V1, but is slightly slower on desktop GPU. -* detector performance on subset of the COCO validation set, Open Images test - split, iNaturalist test split, or Snapshot Serengeti LILA.science test - split. as measured by the dataset-specific mAP measure. Here, higher is - better, and we only report bounding box mAP rounded to the nearest integer. -* Output types (`Boxes`, and `Masks` if applicable ) - -You can un-tar each tar.gz file via, e.g.,: - -``` -tar -xzvf ssd_mobilenet_v1_coco.tar.gz -``` - -Inside the un-tar'ed directory, you will find: - -* a graph proto (`graph.pbtxt`) -* a checkpoint (`model.ckpt.data-00000-of-00001`, `model.ckpt.index`, - `model.ckpt.meta`) -* a frozen graph proto with weights baked into the graph as constants - (`frozen_inference_graph.pb`) to be used for out of the box inference (try - this out in the Jupyter notebook!) -* a config file (`pipeline.config`) which was used to generate the graph. - These directly correspond to a config file in the - [samples/configs](https://github.com/tensorflow/models/tree/master/research/object_detection/samples/configs)) - directory but often with a modified score threshold. In the case of the - heavier Faster R-CNN models, we also provide a version of the model that - uses a highly reduced number of proposals for speed. -* Mobile model only: a TfLite file (`model.tflite`) that can be deployed on - mobile devices. - -Some remarks on frozen inference graphs: - -* If you try to evaluate the frozen graph, you may find performance numbers - for some of the models to be slightly lower than what we report in the below - tables. This is because we discard detections with scores below a threshold - (typically 0.3) when creating the frozen graph. This corresponds effectively - to picking a point on the precision recall curve of a detector (and - discarding the part past that point), which negatively impacts standard mAP - metrics. -* Our frozen inference graphs are generated using the - [v1.12.0](https://github.com/tensorflow/tensorflow/tree/v1.12.0) release - version of TensorFlow; this being said, each frozen inference graph can be - regenerated using your current version of TensorFlow by re-running the - [exporter](https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/exporting_models.md), - pointing it at the model directory as well as the corresponding config file - in - [samples/configs](https://github.com/tensorflow/models/tree/master/research/object_detection/samples/configs). - -## COCO-trained models - -Model name | Speed (ms) | COCO mAP[^1] | Outputs ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------: | :----------: | :-----: -[ssd_mobilenet_v1_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_2018_01_28.tar.gz) | 30 | 21 | Boxes -[ssd_mobilenet_v1_0.75_depth_coco ☆](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_0.75_depth_300x300_coco14_sync_2018_07_03.tar.gz) | 26 | 18 | Boxes -[ssd_mobilenet_v1_quantized_coco ☆](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_quantized_300x300_coco14_sync_2018_07_18.tar.gz) | 29 | 18 | Boxes -[ssd_mobilenet_v1_0.75_depth_quantized_coco ☆](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_0.75_depth_quantized_300x300_coco14_sync_2018_07_18.tar.gz) | 29 | 16 | Boxes -[ssd_mobilenet_v1_ppn_coco ☆](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_ppn_shared_box_predictor_300x300_coco14_sync_2018_07_03.tar.gz) | 26 | 20 | Boxes -[ssd_mobilenet_v1_fpn_coco ☆](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03.tar.gz) | 56 | 32 | Boxes -[ssd_resnet_50_fpn_coco ☆](http://download.tensorflow.org/models/object_detection/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03.tar.gz) | 76 | 35 | Boxes -[ssd_mobilenet_v2_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz) | 31 | 22 | Boxes -[ssd_mobilenet_v2_quantized_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_quantized_300x300_coco_2019_01_03.tar.gz) | 29 | 22 | Boxes -[ssdlite_mobilenet_v2_coco](http://download.tensorflow.org/models/object_detection/ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz) | 27 | 22 | Boxes -[ssd_inception_v2_coco](http://download.tensorflow.org/models/object_detection/ssd_inception_v2_coco_2018_01_28.tar.gz) | 42 | 24 | Boxes -[faster_rcnn_inception_v2_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz) | 58 | 28 | Boxes -[faster_rcnn_resnet50_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet50_coco_2018_01_28.tar.gz) | 89 | 30 | Boxes -[faster_rcnn_resnet50_lowproposals_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet50_lowproposals_coco_2018_01_28.tar.gz) | 64 | | Boxes -[rfcn_resnet101_coco](http://download.tensorflow.org/models/object_detection/rfcn_resnet101_coco_2018_01_28.tar.gz) | 92 | 30 | Boxes -[faster_rcnn_resnet101_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_coco_2018_01_28.tar.gz) | 106 | 32 | Boxes -[faster_rcnn_resnet101_lowproposals_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_lowproposals_coco_2018_01_28.tar.gz) | 82 | | Boxes -[faster_rcnn_inception_resnet_v2_atrous_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_coco_2018_01_28.tar.gz) | 620 | 37 | Boxes -[faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco_2018_01_28.tar.gz) | 241 | | Boxes -[faster_rcnn_nas](http://download.tensorflow.org/models/object_detection/faster_rcnn_nas_coco_2018_01_28.tar.gz) | 1833 | 43 | Boxes -[faster_rcnn_nas_lowproposals_coco](http://download.tensorflow.org/models/object_detection/faster_rcnn_nas_lowproposals_coco_2018_01_28.tar.gz) | 540 | | Boxes -[mask_rcnn_inception_resnet_v2_atrous_coco](http://download.tensorflow.org/models/object_detection/mask_rcnn_inception_resnet_v2_atrous_coco_2018_01_28.tar.gz) | 771 | 36 | Masks -[mask_rcnn_inception_v2_coco](http://download.tensorflow.org/models/object_detection/mask_rcnn_inception_v2_coco_2018_01_28.tar.gz) | 79 | 25 | Masks -[mask_rcnn_resnet101_atrous_coco](http://download.tensorflow.org/models/object_detection/mask_rcnn_resnet101_atrous_coco_2018_01_28.tar.gz) | 470 | 33 | Masks -[mask_rcnn_resnet50_atrous_coco](http://download.tensorflow.org/models/object_detection/mask_rcnn_resnet50_atrous_coco_2018_01_28.tar.gz) | 343 | 29 | Masks - -Note: The asterisk (☆) at the end of model name indicates that this model -supports TPU training. - -Note: If you download the tar.gz file of quantized models and un-tar, you will -get different set of files - a checkpoint, a config file and tflite frozen -graphs (txt/binary). - -### Mobile models - -Model name | Pixel 1 Latency (ms) | COCO mAP | Outputs -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------: | :------: | :-----: -[ssd_mobiledet_cpu_coco](http://download.tensorflow.org/models/object_detection/ssdlite_mobiledet_cpu_320x320_coco_2020_05_19.tar.gz) | 113 | 24.0 | Boxes -[ssd_mobilenet_v2_mnasfpn_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_mnasfpn_shared_box_predictor_320x320_coco_sync_2020_05_18.tar.gz) | 183 | 26.6 | Boxes -[ssd_mobilenet_v3_large_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v3_large_coco_2020_01_14.tar.gz) | 119 | 22.6 | Boxes -[ssd_mobilenet_v3_small_coco](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v3_small_coco_2020_01_14.tar.gz) | 43 | 15.4 | Boxes - -### Pixel4 Edge TPU models - -Model name | Pixel 4 Edge TPU Latency (ms) | COCO mAP (fp32/uint8) | Outputs ---------------------------------------------------------------------------------------------------------------------------------------------- | :---------------------------: | :-------------------: | :-----: -[ssd_mobiledet_edgetpu_coco](http://download.tensorflow.org/models/object_detection/ssdlite_mobiledet_edgetpu_320x320_coco_2020_05_19.tar.gz) | 6.9 | 25.9/25.6 | Boxes -[ssd_mobilenet_edgetpu_coco](https://storage.cloud.google.com/mobilenet_edgetpu/checkpoints/ssdlite_mobilenet_edgetpu_coco_quant.tar.gz) | 6.6 | -/24.3 | Boxes - -### Pixel4 DSP models - -Model name | Pixel 4 DSP Latency (ms) | COCO mAP (fp32/uint8) | Outputs -------------------------------------------------------------------------------------------------------------------------------------- | :----------------------: | :-------------------: | :-----: -[ssd_mobiledet_dsp_coco](http://download.tensorflow.org/models/object_detection/ssdlite_mobiledet_dsp_320x320_coco_2020_05_19.tar.gz) | 12.3 | 28.9/28.8 | Boxes - -## Kitti-trained models - -Model name | Speed (ms) | Pascal mAP@0.5 | Outputs ------------------------------------------------------------------------------------------------------------------------------------ | :--------: | :------------: | :-----: -[faster_rcnn_resnet101_kitti](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_kitti_2018_01_28.tar.gz) | 79 | 87 | Boxes - -## Open Images-trained models - -Model name | Speed (ms) | Open Images mAP@0.5[^2] | Outputs ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------: | :---------------------: | :-----: -[faster_rcnn_inception_resnet_v2_atrous_oidv2](http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_oid_2018_01_28.tar.gz) | 727 | 37 | Boxes -[faster_rcnn_inception_resnet_v2_atrous_lowproposals_oidv2](http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_lowproposals_oid_2018_01_28.tar.gz) | 347 | | Boxes -[facessd_mobilenet_v2_quantized_open_image_v4](http://download.tensorflow.org/models/object_detection/facessd_mobilenet_v2_quantized_320x320_open_image_v4.tar.gz) [^3] | 20 | 73 (faces) | Boxes - -Model name | Speed (ms) | Open Images mAP@0.5[^4] | Outputs ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------: | :---------------------: | :-----: -[faster_rcnn_inception_resnet_v2_atrous_oidv4](http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_oid_v4_2018_12_12.tar.gz) | 425 | 54 | Boxes -[ssd_mobilenetv2_oidv4](http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_oid_v4_2018_12_12.tar.gz) | 89 | 36 | Boxes -[ssd_resnet_101_fpn_oidv4](http://download.tensorflow.org/models/object_detection/ssd_resnet101_v1_fpn_shared_box_predictor_oid_512x512_sync_2019_01_20.tar.gz) | 237 | 38 | Boxes - -## iNaturalist Species-trained models - -Model name | Speed (ms) | Pascal mAP@0.5 | Outputs ---------------------------------------------------------------------------------------------------------------------------------- | :--------: | :------------: | :-----: -[faster_rcnn_resnet101_fgvc](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_fgvc_2018_07_19.tar.gz) | 395 | 58 | Boxes -[faster_rcnn_resnet50_fgvc](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet50_fgvc_2018_07_19.tar.gz) | 366 | 55 | Boxes - -## AVA v2.1 trained models - -Model name | Speed (ms) | Pascal mAP@0.5 | Outputs ------------------------------------------------------------------------------------------------------------------------------------------ | :--------: | :------------: | :-----: -[faster_rcnn_resnet101_ava_v2.1](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_ava_v2.1_2018_04_30.tar.gz) | 93 | 11 | Boxes - -## Snapshot Serengeti Camera Trap trained models - -Model name | COCO mAP@0.5 | Outputs ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | :----------: | :-----: -[faster_rcnn_resnet101_snapshot_serengeti](http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_snapshot_serengeti_2020_06_10.tar.gz) | 38 | Boxes -[context_rcnn_resnet101_snapshot_serengeti](http://download.tensorflow.org/models/object_detection/context_rcnn_resnet101_snapshot_serengeti_2020_06_10.tar.gz) | 56 | Boxes - -## Pixel 6 Edge TPU models - -Model name | Pixel 6 Edge TPU Speed (ms) | Pixel 6 Speed with Post-processing on CPU (ms) | COCO 2017 mAP (uint8) | Outputs ------------------------------------------------------------------------------------------------------------------------------ | :-------------------------: | :--------------------------------------------: | :-------------------: | :-----: -[spaghettinet_edgetpu_s](http://download.tensorflow.org/models/object_detection/tf1/spaghettinet_edgetpu_s_2021_10_13.tar.gz) | 1.3 | 1.8 | 26.3 | Boxes -[spaghettinet_edgetpu_m](http://download.tensorflow.org/models/object_detection/tf1/spaghettinet_edgetpu_m_2021_10_13.tar.gz) | 1.4 | 1.9 | 27.4 | Boxes -[spaghettinet_edgetpu_l](http://download.tensorflow.org/models/object_detection/tf1/spaghettinet_edgetpu_l_2021_10_13.tar.gz) | 1.7 | 2.1 | 28.0 | Boxes - -[^1]: See [MSCOCO evaluation protocol](http://cocodataset.org/#detections-eval). - The COCO mAP numbers, with the exception of the Pixel 6 Edge TPU models, - are evaluated on COCO 14 minival set (note that our split is different - from COCO 17 Val). A full list of image ids used in our split could be - found - [here](https://github.com/tensorflow/models/blob/master/research/object_detection/data/mscoco_minival_ids.txt). -[^2]: This is PASCAL mAP with a slightly different way of true positives - computation: see - [Open Images evaluation protocols](evaluation_protocols.md), - oid_V2_detection_metrics. -[^3]: Non-face boxes are dropped during training and non-face groundtruth boxes - are ignored when evaluating. -[^4]: This is Open Images Challenge metric: see - [Open Images evaluation protocols](evaluation_protocols.md), - oid_challenge_detection_metrics. diff --git a/research/object_detection/g3doc/tf1_training_and_evaluation.md b/research/object_detection/g3doc/tf1_training_and_evaluation.md deleted file mode 100644 index 76c601f1897..00000000000 --- a/research/object_detection/g3doc/tf1_training_and_evaluation.md +++ /dev/null @@ -1,237 +0,0 @@ -# Training and Evaluation with TensorFlow 1 - -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -This page walks through the steps required to train an object detection model. -It assumes the reader has completed the following prerequisites: - -1. The TensorFlow Object Detection API has been installed as documented in the - [installation instructions](tf1.md#installation). -2. A valid data set has been created. See [this page](preparing_inputs.md) for - instructions on how to generate a dataset for the PASCAL VOC challenge or - the Oxford-IIIT Pet dataset. - -## Recommended Directory Structure for Training and Evaluation - -```bash -. -├── data/ -│   ├── eval-00000-of-00001.tfrecord -│   ├── label_map.txt -│   ├── train-00000-of-00002.tfrecord -│   └── train-00001-of-00002.tfrecord -└── models/ - └── my_model_dir/ - ├── eval/ # Created by evaluation job. - ├── my_model.config - └── train/ # - └── model_ckpt-100-data@1 # Created by training job. - └── model_ckpt-100-index # - └── checkpoint # -``` - -## Writing a model configuration - -Please refer to sample [TF1 configs](../samples/configs) and -[configuring jobs](configuring_jobs.md) to create a model config. - -### Model Parameter Initialization - -While optional, it is highly recommended that users utilize classification or -object detection checkpoints. Training an object detector from scratch can take -days. To speed up the training process, it is recommended that users re-use the -feature extractor parameters from a pre-existing image classification or object -detection checkpoint. The`train_config` section in the config provides two -fields to specify pre-existing checkpoints: - -* `fine_tune_checkpoint`: a path prefix to the pre-existing checkpoint - (ie:"/usr/home/username/checkpoint/model.ckpt-#####"). - -* `fine_tune_checkpoint_type`: with value `classification` or `detection` - depending on the type. - -A list of detection checkpoints can be found [here](tf1_detection_zoo.md). - -## Local - -### Training - -A local training job can be run with the following command: - -```bash -# From the tensorflow/models/research/ directory -PIPELINE_CONFIG_PATH={path to pipeline config file} -MODEL_DIR={path to model directory} -NUM_TRAIN_STEPS=50000 -SAMPLE_1_OF_N_EVAL_EXAMPLES=1 -python object_detection/model_main.py \ - --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ - --model_dir=${MODEL_DIR} \ - --num_train_steps=${NUM_TRAIN_STEPS} \ - --sample_1_of_n_eval_examples=${SAMPLE_1_OF_N_EVAL_EXAMPLES} \ - --alsologtostderr -``` - -where `${PIPELINE_CONFIG_PATH}` points to the pipeline config and `${MODEL_DIR}` -points to the directory in which training checkpoints and events will be -written. Note that this binary will interleave both training and evaluation. - -## Google Cloud AI Platform - -The TensorFlow Object Detection API supports training on Google Cloud AI -Platform. This section documents instructions on how to train and evaluate your -model using Cloud AI Platform. The reader should complete the following -prerequistes: - -1. The reader has created and configured a project on Google Cloud AI Platform. - See - [Using GPUs](https://cloud.google.com/ai-platform/training/docs/using-gpus) - and - [Using TPUs](https://cloud.google.com/ai-platform/training/docs/using-tpus) - guides. -2. The reader has a valid data set and stored it in a Google Cloud Storage - bucket. See [this page](preparing_inputs.md) for instructions on how to - generate a dataset for the PASCAL VOC challenge or the Oxford-IIIT Pet - dataset. - -Additionally, it is recommended users test their job by running training and -evaluation jobs for a few iterations [locally on their own machines](#local). - -### Training with multiple workers with single GPU - -Google Cloud ML requires a YAML configuration file for a multiworker training -job using GPUs. A sample YAML file is given below: - -``` -trainingInput: - runtimeVersion: "1.15" - scaleTier: CUSTOM - masterType: standard_gpu - workerCount: 9 - workerType: standard_gpu - parameterServerCount: 3 - parameterServerType: standard - -``` - -Please keep the following guidelines in mind when writing the YAML -configuration: - -* A job with n workers will have n + 1 training machines (n workers + 1 - master). -* The number of parameters servers used should be an odd number to prevent a - parameter server from storing only weight variables or only bias variables - (due to round robin parameter scheduling). -* The learning rate in the training config should be decreased when using a - larger number of workers. Some experimentation is required to find the - optimal learning rate. - -The YAML file should be saved on the local machine (not on GCP). Once it has -been written, a user can start a training job on Cloud ML Engine using the -following command: - -```bash -# From the tensorflow/models/research/ directory -cp object_detection/packages/tf1/setup.py . -gcloud ml-engine jobs submit training object_detection_`date +%m_%d_%Y_%H_%M_%S` \ - --runtime-version 1.15 \ - --python-version 3.6 \ - --job-dir=gs://${MODEL_DIR} \ - --package-path ./object_detection \ - --module-name object_detection.model_main \ - --region us-central1 \ - --config ${PATH_TO_LOCAL_YAML_FILE} \ - -- \ - --model_dir=gs://${MODEL_DIR} \ - --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} -``` - -Where `${PATH_TO_LOCAL_YAML_FILE}` is the local path to the YAML configuration, -`gs://${MODEL_DIR}` specifies the directory on Google Cloud Storage where the -training checkpoints and events will be written to and -`gs://${PIPELINE_CONFIG_PATH}` points to the pipeline configuration stored on -Google Cloud Storage. - -Users can monitor the progress of their training job on the -[ML Engine Dashboard](https://console.cloud.google.com/ai-platform/jobs). - -## Training with TPU - -Launching a training job with a TPU compatible pipeline config requires using a -similar command: - -```bash -# From the tensorflow/models/research/ directory -cp object_detection/packages/tf1/setup.py . -gcloud ml-engine jobs submit training `whoami`_object_detection_`date +%m_%d_%Y_%H_%M_%S` \ - --job-dir=gs://${MODEL_DIR} \ - --package-path ./object_detection \ - --module-name object_detection.model_tpu_main \ - --runtime-version 1.15 \ - --python-version 3.6 \ - --scale-tier BASIC_TPU \ - --region us-central1 \ - -- \ - --tpu_zone us-central1 \ - --model_dir=gs://${MODEL_DIR} \ - --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} -``` - -In contrast with the GPU training command, there is no need to specify a YAML -file, and we point to the *object_detection.model_tpu_main* binary instead of -*object_detection.model_main*. We must also now set `scale-tier` to be -`BASIC_TPU` and provide a `tpu_zone`. Finally as before `pipeline_config_path` -points to a points to the pipeline configuration stored on Google Cloud Storage -(but is now must be a TPU compatible model). - -## Evaluation with GPU - -Note: You only need to do this when using TPU for training, as it does not -interleave evaluation during training, as in the case of Multiworker GPU -training. - -Evaluation jobs run on a single machine, so it is not necessary to write a YAML -configuration for evaluation. Run the following command to start the evaluation -job: - -```bash -# From the tensorflow/models/research/ directory -cp object_detection/packages/tf1/setup.py . -gcloud ml-engine jobs submit training object_detection_eval_`date +%m_%d_%Y_%H_%M_%S` \ - --runtime-version 1.15 \ - --python-version 3.6 \ - --job-dir=gs://${MODEL_DIR} \ - --package-path ./object_detection \ - --module-name object_detection.model_main \ - --region us-central1 \ - --scale-tier BASIC_GPU \ - -- \ - --model_dir=gs://${MODEL_DIR} \ - --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} \ - --checkpoint_dir=gs://${MODEL_DIR} -``` - -Where `gs://${MODEL_DIR}` points to the directory on Google Cloud Storage where -training checkpoints are saved (same as the training job), as well as to where -evaluation events will be saved on Google Cloud Storage and -`gs://${PIPELINE_CONFIG_PATH}` points to where the pipeline configuration is -stored on Google Cloud Storage. - -Typically one starts an evaluation job concurrently with the training job. Note -that we do not support running evaluation on TPU, so the above command line for -launching evaluation jobs is the same whether you are training on GPU or TPU. - -## Running Tensorboard - -Progress for training and eval jobs can be inspected using Tensorboard. If using -the recommended directory structure, Tensorboard can be run using the following -command: - -```bash -tensorboard --logdir=${MODEL_DIR} -``` - -where `${MODEL_DIR}` points to the directory that contains the train and eval -directories. Please note it may take Tensorboard a couple minutes to populate -with data. diff --git a/research/object_detection/g3doc/tf2.md b/research/object_detection/g3doc/tf2.md deleted file mode 100644 index d45d157f3b9..00000000000 --- a/research/object_detection/g3doc/tf2.md +++ /dev/null @@ -1,87 +0,0 @@ -# Object Detection API with TensorFlow 2 - -## Requirements - -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) -[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![Protobuf Compiler >= 3.0](https://img.shields.io/badge/ProtoBuf%20Compiler-%3E3.0-brightgreen)](https://grpc.io/docs/protoc-installation/#install-using-a-package-manager) - -## Installation - -You can install the TensorFlow Object Detection API either with Python Package -Installer (pip) or Docker. For local runs we recommend using Docker and for -Google Cloud runs we recommend using pip. - -Clone the TensorFlow Models repository and proceed to one of the installation -options. - -```bash -git clone https://github.com/tensorflow/models.git -``` - -### Docker Installation - -```bash -# From the root of the git repository -docker build -f research/object_detection/dockerfiles/tf2/Dockerfile -t od . -docker run -it od -``` - -### Python Package Installation - -```bash -cd models/research -# Compile protos. -protoc object_detection/protos/*.proto --python_out=. -# Install TensorFlow Object Detection API. -cp object_detection/packages/tf2/setup.py . -python -m pip install --use-feature=2020-resolver . -``` - -```bash -# Test the installation. -python object_detection/builders/model_builder_tf2_test.py -``` - -## Quick Start - -### Colabs - - - -* Training - - [Fine-tune a pre-trained detector in eager mode on custom data](../colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb) - -* Inference - - [Run inference with models from the zoo](../colab_tutorials/inference_tf2_colab.ipynb) - -* Few Shot Learning for Mobile Inference - - [Fine-tune a pre-trained detector for use with TensorFlow Lite](../colab_tutorials/eager_few_shot_od_training_tflite.ipynb) - - - -## Training and Evaluation - -To train and evaluate your models either locally or on Google Cloud see -[instructions](tf2_training_and_evaluation.md). - -## Model Zoo - -We provide a large collection of models that are trained on COCO 2017 in the -[Model Zoo](tf2_detection_zoo.md). - -## Guides - -* - Configuring an object detection pipeline
-* Preparing inputs
-* - Defining your own model architecture
-* - Bringing in your own dataset
-* - Supported object detection evaluation protocols
-* - TPU compatible detection pipelines
-* - Training and evaluation guide (CPU, GPU, or TPU)
diff --git a/research/object_detection/g3doc/tf2_classification_zoo.md b/research/object_detection/g3doc/tf2_classification_zoo.md deleted file mode 100644 index 23c629ac0e9..00000000000 --- a/research/object_detection/g3doc/tf2_classification_zoo.md +++ /dev/null @@ -1,25 +0,0 @@ -# TensorFlow 2 Classification Model Zoo - -[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) - -We provide a collection of classification models pre-trained on the -[Imagenet](http://www.image-net.org). These can be used to initilize detection -model parameters. - -Model name | ----------- | -[EfficientNet B0](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b0.tar.gz) | -[EfficientNet B1](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b1.tar.gz) | -[EfficientNet B2](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b2.tar.gz) | -[EfficientNet B3](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b3.tar.gz) | -[EfficientNet B4](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b4.tar.gz) | -[EfficientNet B5](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b5.tar.gz) | -[EfficientNet B6](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b6.tar.gz) | -[EfficientNet B7](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/efficientnet_b7.tar.gz) | -[Resnet V1 50](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/resnet50_v1.tar.gz) | -[Resnet V1 101](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/resnet101_v1.tar.gz) | -[Resnet V1 152](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/resnet152_v1.tar.gz) | -[Inception Resnet V2](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/inception_resnet_v2.tar.gz) | -[MobileNet V1](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/mobilnet_v1.tar.gz) | -[MobileNet V2](http://download.tensorflow.org/models/object_detection/classification/tf2/20200710/mobilnet_v2.tar.gz) | diff --git a/research/object_detection/g3doc/tf2_detection_zoo.md b/research/object_detection/g3doc/tf2_detection_zoo.md deleted file mode 100644 index 9b3c4c13825..00000000000 --- a/research/object_detection/g3doc/tf2_detection_zoo.md +++ /dev/null @@ -1,70 +0,0 @@ -# TensorFlow 2 Detection Model Zoo - -[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) - - - -We provide a collection of detection models pre-trained on the -[COCO 2017 dataset](http://cocodataset.org). These models can be useful for -out-of-the-box inference if you are interested in categories already in those -datasets. You can try it in our inference -[colab](../colab_tutorials/inference_tf2_colab.ipynb) - -They are also useful for initializing your models when training on novel -datasets. You can try this out on our few-shot training -[colab](../colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb). - -Please look at [this guide](running_on_mobile_tf2.md) for mobile inference. - - - -Finally, if you would like to train these models from scratch, you can find the -model configs in this [directory](../configs/tf2) (also in the linked -`tar.gz`s). - -Model name | Speed (ms) | COCO mAP | Outputs ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------: | :----------: | :-----: -[CenterNet HourGlass104 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200713/centernet_hg104_512x512_coco17_tpu-8.tar.gz) | 70 | 41.9 | Boxes -[CenterNet HourGlass104 Keypoints 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_hg104_512x512_kpts_coco17_tpu-32.tar.gz) | 76 | 40.0/61.4 | Boxes/Keypoints -[CenterNet HourGlass104 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200713/centernet_hg104_1024x1024_coco17_tpu-32.tar.gz) | 197 | 44.5 | Boxes -[CenterNet HourGlass104 Keypoints 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_hg104_1024x1024_kpts_coco17_tpu-32.tar.gz) | 211 | 42.8/64.5 | Boxes/Keypoints -[CenterNet Resnet50 V1 FPN 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_resnet50_v1_fpn_512x512_coco17_tpu-8.tar.gz) | 27 | 31.2 | Boxes -[CenterNet Resnet50 V1 FPN Keypoints 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_resnet50_v1_fpn_512x512_kpts_coco17_tpu-8.tar.gz) | 30 | 29.3/50.7 | Boxes/Keypoints -[CenterNet Resnet101 V1 FPN 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_resnet101_v1_fpn_512x512_coco17_tpu-8.tar.gz) | 34 | 34.2 | Boxes -[CenterNet Resnet50 V2 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_resnet50_v2_512x512_coco17_tpu-8.tar.gz) | 27 | 29.5 | Boxes -[CenterNet Resnet50 V2 Keypoints 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200711/centernet_resnet50_v2_512x512_kpts_coco17_tpu-8.tar.gz) | 30 | 27.6/48.2 | Boxes/Keypoints -[CenterNet MobileNetV2 FPN 512x512](http://download.tensorflow.org/models/object_detection/tf2/20210210/centernet_mobilenetv2fpn_512x512_coco17_od.tar.gz) | 6 | 23.4 | Boxes -[CenterNet MobileNetV2 FPN Keypoints 512x512](http://download.tensorflow.org/models/object_detection/tf2/20210210/centernet_mobilenetv2fpn_512x512_coco17_kpts.tar.gz) | 6 | 41.7 | Keypoints -[EfficientDet D0 512x512](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d0_coco17_tpu-32.tar.gz) | 39 | 33.6 | Boxes -[EfficientDet D1 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d1_coco17_tpu-32.tar.gz) | 54 | 38.4 | Boxes -[EfficientDet D2 768x768](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d2_coco17_tpu-32.tar.gz) | 67 | 41.8 | Boxes -[EfficientDet D3 896x896](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d3_coco17_tpu-32.tar.gz) | 95 | 45.4 | Boxes -[EfficientDet D4 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d4_coco17_tpu-32.tar.gz) | 133 | 48.5 | Boxes -[EfficientDet D5 1280x1280](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d5_coco17_tpu-32.tar.gz) | 222 | 49.7 | Boxes -[EfficientDet D6 1280x1280](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d6_coco17_tpu-32.tar.gz) | 268 | 50.5 | Boxes -[EfficientDet D7 1536x1536](http://download.tensorflow.org/models/object_detection/tf2/20200711/efficientdet_d7_coco17_tpu-32.tar.gz) | 325 | 51.2 | Boxes -[SSD MobileNet v2 320x320](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_320x320_coco17_tpu-8.tar.gz) |19 | 20.2 | Boxes -[SSD MobileNet V1 FPN 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v1_fpn_640x640_coco17_tpu-8.tar.gz) | 48 | 29.1 | Boxes -[SSD MobileNet V2 FPNLite 320x320](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz) | 22 | 22.2 | Boxes -[SSD MobileNet V2 FPNLite 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8.tar.gz) | 39 | 28.2 | Boxes -[SSD ResNet50 V1 FPN 640x640 (RetinaNet50)](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet50_v1_fpn_640x640_coco17_tpu-8.tar.gz) | 46 | 34.3 | Boxes -[SSD ResNet50 V1 FPN 1024x1024 (RetinaNet50)](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet50_v1_fpn_1024x1024_coco17_tpu-8.tar.gz) | 87 | 38.3 | Boxes -[SSD ResNet101 V1 FPN 640x640 (RetinaNet101)](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_640x640_coco17_tpu-8.tar.gz) | 57 | 35.6 | Boxes -[SSD ResNet101 V1 FPN 1024x1024 (RetinaNet101)](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet101_v1_fpn_1024x1024_coco17_tpu-8.tar.gz) | 104 | 39.5 | Boxes -[SSD ResNet152 V1 FPN 640x640 (RetinaNet152)](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet152_v1_fpn_640x640_coco17_tpu-8.tar.gz) | 80 | 35.4 | Boxes -[SSD ResNet152 V1 FPN 1024x1024 (RetinaNet152)](http://download.tensorflow.org/models/object_detection/tf2/20200711/ssd_resnet152_v1_fpn_1024x1024_coco17_tpu-8.tar.gz) | 111 | 39.6 | Boxes -[Faster R-CNN ResNet50 V1 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.tar.gz) | 53 | 29.3 | Boxes -[Faster R-CNN ResNet50 V1 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet50_v1_1024x1024_coco17_tpu-8.tar.gz) | 65 | 31.0 | Boxes -[Faster R-CNN ResNet50 V1 800x1333](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet50_v1_800x1333_coco17_gpu-8.tar.gz) | 65 | 31.6 | Boxes -[Faster R-CNN ResNet101 V1 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet101_v1_640x640_coco17_tpu-8.tar.gz) | 55 | 31.8 | Boxes -[Faster R-CNN ResNet101 V1 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet101_v1_1024x1024_coco17_tpu-8.tar.gz) | 72 | 37.1 | Boxes -[Faster R-CNN ResNet101 V1 800x1333](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet101_v1_800x1333_coco17_gpu-8.tar.gz) | 77 | 36.6 | Boxes -[Faster R-CNN ResNet152 V1 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet152_v1_640x640_coco17_tpu-8.tar.gz) | 64 | 32.4 | Boxes -[Faster R-CNN ResNet152 V1 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet152_v1_1024x1024_coco17_tpu-8.tar.gz) | 85 | 37.6 | Boxes -[Faster R-CNN ResNet152 V1 800x1333](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet152_v1_800x1333_coco17_gpu-8.tar.gz) | 101 | 37.4 | Boxes -[Faster R-CNN Inception ResNet V2 640x640](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_inception_resnet_v2_640x640_coco17_tpu-8.tar.gz) | 206 | 37.7 | Boxes -[Faster R-CNN Inception ResNet V2 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_inception_resnet_v2_1024x1024_coco17_tpu-8.tar.gz) | 236 | 38.7 | Boxes -[Mask R-CNN Inception ResNet V2 1024x1024](http://download.tensorflow.org/models/object_detection/tf2/20200711/mask_rcnn_inception_resnet_v2_1024x1024_coco17_gpu-8.tar.gz) | 301 | 39.0/34.6 | Boxes/Masks -[ExtremeNet (deprecated)](http://download.tensorflow.org/models/object_detection/tf2/20200711/extremenet.tar.gz) | -- | -- | Boxes -[ExtremeNet](http://download.tensorflow.org/models/object_detection/tf2/20210507/extremenet.tar.gz)| -- | -- | Boxes diff --git a/research/object_detection/g3doc/tf2_training_and_evaluation.md b/research/object_detection/g3doc/tf2_training_and_evaluation.md deleted file mode 100644 index 8934ab2fbf2..00000000000 --- a/research/object_detection/g3doc/tf2_training_and_evaluation.md +++ /dev/null @@ -1,289 +0,0 @@ -# Training and Evaluation with TensorFlow 2 - -[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0) -[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/) - -This page walks through the steps required to train an object detection model. -It assumes the reader has completed the following prerequisites: - -1. The TensorFlow Object Detection API has been installed as documented in the - [installation instructions](tf2.md#installation). -2. A valid data set has been created. See [this page](preparing_inputs.md) for - instructions on how to generate a dataset for the PASCAL VOC challenge or - the Oxford-IIIT Pet dataset. - -## Recommended Directory Structure for Training and Evaluation - -```bash -. -├── data/ -│   ├── eval-00000-of-00001.tfrecord -│   ├── label_map.txt -│   ├── train-00000-of-00002.tfrecord -│   └── train-00001-of-00002.tfrecord -└── models/ - └── my_model_dir/ - ├── eval/ # Created by evaluation job. - ├── my_model.config - └── model_ckpt-100-data@1 # - └── model_ckpt-100-index # Created by training job. - └── checkpoint # -``` - -## Writing a model configuration - -Please refer to sample [TF2 configs](../configs/tf2) and -[configuring jobs](configuring_jobs.md) to create a model config. - -### Model Parameter Initialization - -While optional, it is highly recommended that users utilize classification or -object detection checkpoints. Training an object detector from scratch can take -days. To speed up the training process, it is recommended that users re-use the -feature extractor parameters from a pre-existing image classification or object -detection checkpoint. The `train_config` section in the config provides two -fields to specify pre-existing checkpoints: - -* `fine_tune_checkpoint`: a path prefix to the pre-existing checkpoint - (ie:"/usr/home/username/checkpoint/model.ckpt-#####"). - -* `fine_tune_checkpoint_type`: with value `classification` or `detection` - depending on the type. - -A list of classification checkpoints can be found -[here](tf2_classification_zoo.md) - -A list of detection checkpoints can be found [here](tf2_detection_zoo.md). - -## Local - -### Training - -A local training job can be run with the following command: - -```bash -# From the tensorflow/models/research/ directory -PIPELINE_CONFIG_PATH={path to pipeline config file} -MODEL_DIR={path to model directory} -python object_detection/model_main_tf2.py \ - --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ - --model_dir=${MODEL_DIR} \ - --alsologtostderr -``` - -where `${PIPELINE_CONFIG_PATH}` points to the pipeline config and `${MODEL_DIR}` -points to the directory in which training checkpoints and events will be -written. - -### Evaluation - -A local evaluation job can be run with the following command: - -```bash -# From the tensorflow/models/research/ directory -PIPELINE_CONFIG_PATH={path to pipeline config file} -MODEL_DIR={path to model directory} -CHECKPOINT_DIR=${MODEL_DIR} -MODEL_DIR={path to model directory} -python object_detection/model_main_tf2.py \ - --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ - --model_dir=${MODEL_DIR} \ - --checkpoint_dir=${CHECKPOINT_DIR} \ - --alsologtostderr -``` - -where `${CHECKPOINT_DIR}` points to the directory with checkpoints produced by -the training job. Evaluation events are written to `${MODEL_DIR/eval}` - -## Google Cloud VM - -The TensorFlow Object Detection API supports training on Google Cloud with Deep -Learning GPU VMs and TPU VMs. This section documents instructions on how to -train and evaluate your model on them. The reader should complete the following -prerequistes: - -1. The reader has create and configured a GPU VM or TPU VM on Google Cloud with - TensorFlow >= 2.2.0. See - [TPU quickstart](https://cloud.google.com/tpu/docs/quickstart) and - [GPU quickstart](https://cloud.google.com/ai-platform/deep-learning-vm/docs/tensorflow_start_instance#with-one-or-more-gpus) - -2. The reader has installed the TensorFlow Object Detection API as documented - in the [installation instructions](tf2.md#installation) on the VM. - -3. The reader has a valid data set and stored it in a Google Cloud Storage - bucket or locally on the VM. See [this page](preparing_inputs.md) for - instructions on how to generate a dataset for the PASCAL VOC challenge or - the Oxford-IIIT Pet dataset. - -Additionally, it is recommended users test their job by running training and -evaluation jobs for a few iterations [locally on their own machines](#local). - -### Training - -Training on GPU or TPU VMs is similar to local training. It can be launched -using the following command. - -```bash -# From the tensorflow/models/research/ directory -USE_TPU=true -TPU_NAME="MY_TPU_NAME" -PIPELINE_CONFIG_PATH={path to pipeline config file} -MODEL_DIR={path to model directory} -python object_detection/model_main_tf2.py \ - --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ - --model_dir=${MODEL_DIR} \ - --use_tpu=${USE_TPU} \ # (optional) only required for TPU training. - --tpu_name=${TPU_NAME} \ # (optional) only required for TPU training. - --alsologtostderr -``` - -where `${PIPELINE_CONFIG_PATH}` points to the pipeline config and `${MODEL_DIR}` -points to the root directory for the files produces. Training checkpoints and -events are written to `${MODEL_DIR}`. Note that the paths can be either local or -a path to GCS bucket. - -### Evaluation - -Evaluation is only supported on GPU. Similar to local evaluation it can be -launched using the following command: - -```bash -# From the tensorflow/models/research/ directory -PIPELINE_CONFIG_PATH={path to pipeline config file} -MODEL_DIR={path to model directory} -CHECKPOINT_DIR=${MODEL_DIR} -MODEL_DIR={path to model directory} -python object_detection/model_main_tf2.py \ - --pipeline_config_path=${PIPELINE_CONFIG_PATH} \ - --model_dir=${MODEL_DIR} \ - --checkpoint_dir=${CHECKPOINT_DIR} \ - --alsologtostderr -``` - -where `${CHECKPOINT_DIR}` points to the directory with checkpoints produced by -the training job. Evaluation events are written to `${MODEL_DIR/eval}`. Note -that the paths can be either local or a path to GCS bucket. - -## Google Cloud AI Platform - -The TensorFlow Object Detection API supports also supports training on Google -Cloud AI Platform. This section documents instructions on how to train and -evaluate your model using Cloud ML. The reader should complete the following -prerequistes: - -1. The reader has created and configured a project on Google Cloud AI Platform. - See - [Using GPUs](https://cloud.google.com/ai-platform/training/docs/using-gpus) - and - [Using TPUs](https://cloud.google.com/ai-platform/training/docs/using-tpus) - guides. -2. The reader has a valid data set and stored it in a Google Cloud Storage - bucket. See [this page](preparing_inputs.md) for instructions on how to - generate a dataset for the PASCAL VOC challenge or the Oxford-IIIT Pet - dataset. - -Additionally, it is recommended users test their job by running training and -evaluation jobs for a few iterations [locally on their own machines](#local). - -### Training with multiple GPUs - -A user can start a training job on Cloud AI Platform following the instruction -https://cloud.google.com/ai-platform/training/docs/custom-containers-training. - -```bash -git clone https://github.com/tensorflow/models.git - -# From the tensorflow/models/research/ directory -cp object_detection/dockerfiles/tf2_ai_platform/Dockerfile . - -docker build -t gcr.io/${DOCKER_IMAGE_URI} . - -docker push gcr.io/${DOCKER_IMAGE_URI} -``` - -```bash -gcloud ai-platform jobs submit training object_detection_`date +%m_%d_%Y_%H_%M_%S` \ - --job-dir=gs://${MODEL_DIR} \ - --region us-central1 \ - --master-machine-type n1-highcpu-16 \ - --master-accelerator count=8,type=nvidia-tesla-v100 \ - --master-image-uri gcr.io/${DOCKER_IMAGE_URI} \ - --scale-tier CUSTOM \ - -- \ - --model_dir=gs://${MODEL_DIR} \ - --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} -``` - -Where `gs://${MODEL_DIR}` specifies the directory on Google Cloud Storage where -the training checkpoints and events will be written to and -`gs://${PIPELINE_CONFIG_PATH}` points to the pipeline configuration stored on -Google Cloud Storage, and `gcr.io/${DOCKER_IMAGE_URI}` points to the docker -image stored in Google Container Registry. - -Users can monitor the progress of their training job on the -[ML Engine Dashboard](https://console.cloud.google.com/ai-platform/jobs). - -### Training with TPU - -Launching a training job with a TPU compatible pipeline config requires using -the following command: - -```bash -# From the tensorflow/models/research/ directory -cp object_detection/packages/tf2/setup.py . -gcloud ai-platform jobs submit training `whoami`_object_detection_`date +%m_%d_%Y_%H_%M_%S` \ - --job-dir=gs://${MODEL_DIR} \ - --package-path ./object_detection \ - --module-name object_detection.model_main_tf2 \ - --runtime-version 2.1 \ - --python-version 3.6 \ - --scale-tier BASIC_TPU \ - --region us-central1 \ - -- \ - --use_tpu true \ - --model_dir=gs://${MODEL_DIR} \ - --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} -``` - -As before `pipeline_config_path` points to the pipeline configuration stored on -Google Cloud Storage (but is now must be a TPU compatible model). - -### Evaluating with GPU - -Evaluation jobs run on a single machine. Run the following command to start the -evaluation job: - -```bash -gcloud ai-platform jobs submit training object_detection_eval_`date +%m_%d_%Y_%H_%M_%S` \ - --job-dir=gs://${MODEL_DIR} \ - --region us-central1 \ - --scale-tier BASIC_GPU \ - --master-image-uri gcr.io/${DOCKER_IMAGE_URI} \ - -- \ - --model_dir=gs://${MODEL_DIR} \ - --pipeline_config_path=gs://${PIPELINE_CONFIG_PATH} \ - --checkpoint_dir=gs://${MODEL_DIR} -``` - -where `gs://${MODEL_DIR}` points to the directory on Google Cloud Storage where -training checkpoints are saved and `gs://{PIPELINE_CONFIG_PATH}` points to where -the model configuration file stored on Google Cloud Storage, and -`gcr.io/${DOCKER_IMAGE_URI}` points to the docker image stored in Google -Container Registry. Evaluation events are written to `gs://${MODEL_DIR}/eval` - -Typically one starts an evaluation job concurrently with the training job. Note -that we do not support running evaluation on TPU. - -## Running Tensorboard - -Progress for training and eval jobs can be inspected using Tensorboard. If using -the recommended directory structure, Tensorboard can be run using the following -command: - -```bash -tensorboard --logdir=${MODEL_DIR} -``` - -where `${MODEL_DIR}` points to the directory that contains the train and eval -directories. Please note it may take Tensorboard a couple minutes to populate -with data. diff --git a/research/object_detection/g3doc/tpu_compatibility.md b/research/object_detection/g3doc/tpu_compatibility.md deleted file mode 100644 index 411f1c55cf5..00000000000 --- a/research/object_detection/g3doc/tpu_compatibility.md +++ /dev/null @@ -1,196 +0,0 @@ -# TPU compatible detection pipelines - -[TOC] - -The TensorFlow Object Detection API supports TPU training for some models. To -make models TPU compatible you need to make a few tweaks to the model config as -mentioned below. We also provide several sample configs that you can use as a -template. - -## TPU compatibility - -### Static shaped tensors - -TPU training currently requires all tensors in the TensorFlow Graph to have -static shapes. However, most of the sample configs in Object Detection API have -a few different tensors that are dynamically shaped. Fortunately, we provide -simple alternatives in the model configuration that modifies these tensors to -have static shape: - -* **Image tensors with static shape** - This can be achieved either by using a - `fixed_shape_resizer` that resizes images to a fixed spatial shape or by - setting `pad_to_max_dimension: true` in `keep_aspect_ratio_resizer` which - pads the resized images with zeros to the bottom and right. Padded image - tensors are correctly handled internally within the model. - - ``` - image_resizer { - fixed_shape_resizer { - height: 640 - width: 640 - } - } - ``` - - or - - ``` - image_resizer { - keep_aspect_ratio_resizer { - min_dimension: 640 - max_dimension: 640 - pad_to_max_dimension: true - } - } - ``` - -* **Groundtruth tensors with static shape** - Images in a typical detection - dataset have variable number of groundtruth boxes and associated classes. - Setting `max_number_of_boxes` to a large enough number in `train_config` - pads the groundtruth tensors with zeros to a static shape. Padded - groundtruth tensors are correctly handled internally within the model. - - ``` - train_config: { - fine_tune_checkpoint: "PATH_TO_BE_CONFIGURED/model.ckpt" - batch_size: 64 - max_number_of_boxes: 200 - unpad_groundtruth_tensors: false - } - ``` - -### TPU friendly ops - -Although TPU supports a vast number of tensorflow ops, a few used in the -TensorFlow Object Detection API are unsupported. We list such ops below and -recommend compatible substitutes. - -* **Anchor sampling** - Typically we use hard example mining in standard SSD - pipeliens to balance positive and negative anchors that contribute to the - loss. Hard Example mining uses non max suppression as a subroutine and since - non max suppression is not currently supported on TPUs we cannot use hard - example mining. Fortunately, we provide an implementation of focal loss that - can be used instead of hard example mining. Remove `hard_example_miner` from - the config and substitute `weighted_sigmoid` classification loss with - `weighted_sigmoid_focal` loss. - - ``` - loss { - classification_loss { - weighted_sigmoid_focal { - alpha: 0.25 - gamma: 2.0 - } - } - localization_loss { - weighted_smooth_l1 { - } - } - classification_weight: 1.0 - localization_weight: 1.0 - } - ``` - -* **Target Matching** - Object detection API provides two choices for matcher - used in target assignment: `argmax_matcher` and `bipartite_matcher`. - Bipartite matcher is not currently supported on TPU, therefore we must - modify the configs to use `argmax_matcher`. Additionally, set - `use_matmul_gather: true` for efficiency on TPU. - - ``` - matcher { - argmax_matcher { - matched_threshold: 0.5 - unmatched_threshold: 0.5 - ignore_thresholds: false - negatives_lower_than_unmatched: true - force_match_for_each_row: true - use_matmul_gather: true - } - } - ``` - -### TPU training hyperparameters - -Object Detection training on TPU uses synchronous SGD. On a typical cloud TPU -with 8 cores we recommend batch sizes that are 8x large when compared to a GPU -config that uses asynchronous SGD. We also use fewer training steps (~ 1/100 x) -due to the large batch size. This necessitates careful tuning of some other -training parameters as listed below. - -* **Batch size** - Use the largest batch size that can fit on cloud TPU. - - ``` - train_config { - batch_size: 1024 - } - ``` - -* **Training steps** - Typically only 10s of thousands. - - ``` - train_config { - num_steps: 25000 - } - ``` - -* **Batch norm decay** - Use smaller decay constants (0.97 or 0.997) since we - take fewer training steps. - - ``` - batch_norm { - scale: true, - decay: 0.97, - epsilon: 0.001, - } - ``` - -* **Learning rate** - Use large learning rate with warmup. Scale learning rate - linearly with batch size. See `cosine_decay_learning_rate` or - `manual_step_learning_rate` for examples. - - ``` - learning_rate: { - cosine_decay_learning_rate { - learning_rate_base: .04 - total_steps: 25000 - warmup_learning_rate: .013333 - warmup_steps: 2000 - } - } - ``` - - or - - ``` - learning_rate: { - manual_step_learning_rate { - warmup: true - initial_learning_rate: .01333 - schedule { - step: 2000 - learning_rate: 0.04 - } - schedule { - step: 15000 - learning_rate: 0.004 - } - } - } - ``` - -## Example TPU compatible configs - -We provide example config files that you can use to train your own models on TPU - -* ssd_mobilenet_v1_300x300
-* ssd_mobilenet_v1_ppn_300x300
-* ssd_mobilenet_v1_fpn_640x640 - (mobilenet based retinanet)
-* ssd_resnet50_v1_fpn_640x640 - (retinanet)
- -## Supported Meta architectures - -Currently, `SSDMetaArch` models are supported on TPUs. `FasterRCNNMetaArch` is -going to be supported soon. diff --git a/research/object_detection/g3doc/tpu_exporters.md b/research/object_detection/g3doc/tpu_exporters.md deleted file mode 100644 index 4cc3395aea6..00000000000 --- a/research/object_detection/g3doc/tpu_exporters.md +++ /dev/null @@ -1,37 +0,0 @@ -# Object Detection TPU Inference Exporter - -[![TensorFlow 1.15](https://img.shields.io/badge/TensorFlow-1.15-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v1.15.0) - -This package contains SavedModel Exporter for TPU Inference of object detection -models. - -## Usage - -This Exporter is intended for users who have trained models with CPUs / GPUs, -but would like to use them for inference on TPU without changing their code or -re-training their models. - -Users are assumed to have: - -+ `PIPELINE_CONFIG`: A pipeline_pb2.TrainEvalPipelineConfig config file; -+ `CHECKPOINT`: A model checkpoint trained on any device; - -and need to correctly set: - -+ `EXPORT_DIR`: Path to export SavedModel; -+ `INPUT_PLACEHOLDER`: Name of input placeholder in model's signature_def_map; -+ `INPUT_TYPE`: Type of input node, which can be one of 'image_tensor', - 'encoded_image_string_tensor', or 'tf_example'; -+ `USE_BFLOAT16`: Whether to use bfloat16 instead of float32 on TPU. - -The model can be exported with: - -``` -python object_detection/tpu_exporters/export_saved_model_tpu.py \ - --pipeline_config_file= \ - --ckpt_path= \ - --export_dir= \ - --input_placeholder_name= \ - --input_type= \ - --use_bfloat16= -``` diff --git a/research/object_detection/g3doc/using_your_own_dataset.md b/research/object_detection/g3doc/using_your_own_dataset.md deleted file mode 100644 index c44acb2bf23..00000000000 --- a/research/object_detection/g3doc/using_your_own_dataset.md +++ /dev/null @@ -1,209 +0,0 @@ -# Preparing Inputs - -[TOC] - -To use your own dataset in TensorFlow Object Detection API, you must convert it -into the [TFRecord file format](https://www.tensorflow.org/tutorials/load_data/tfrecord). -This document outlines how to write a script to generate the TFRecord file. - -## Label Maps - -Each dataset is required to have a label map associated with it. This label map -defines a mapping from string class names to integer class Ids. The label map -should be a `StringIntLabelMap` text protobuf. Sample label maps can be found in -object_detection/data. Label maps should always start from id 1. - -## Dataset Requirements - -For every example in your dataset, you should have the following information: - -1. An RGB image for the dataset encoded as jpeg or png. -2. A list of bounding boxes for the image. Each bounding box should contain: - 1. A bounding box coordinates (with origin in top left corner) defined by 4 - floating point numbers [ymin, xmin, ymax, xmax]. Note that we store the - _normalized_ coordinates (x / width, y / height) in the TFRecord dataset. - 2. The class of the object in the bounding box. - -# Example Image - -Consider the following image: - -![Example Image](img/example_cat.jpg "Example Image") - -with the following label map: - -``` -item { - id: 1 - name: 'Cat' -} - - -item { - id: 2 - name: 'Dog' -} -``` - -We can generate a tf.Example proto for this image using the following code: - -```python - -def create_cat_tf_example(encoded_cat_image_data): - """Creates a tf.Example proto from sample cat image. - - Args: - encoded_cat_image_data: The jpg encoded data of the cat image. - - Returns: - example: The created tf.Example. - """ - - height = 1032.0 - width = 1200.0 - filename = 'example_cat.jpg' - image_format = b'jpg' - - xmins = [322.0 / 1200.0] - xmaxs = [1062.0 / 1200.0] - ymins = [174.0 / 1032.0] - ymaxs = [761.0 / 1032.0] - classes_text = ['Cat'] - classes = [1] - - tf_example = tf.train.Example(features=tf.train.Features(feature={ - 'image/height': dataset_util.int64_feature(height), - 'image/width': dataset_util.int64_feature(width), - 'image/filename': dataset_util.bytes_feature(filename), - 'image/source_id': dataset_util.bytes_feature(filename), - 'image/encoded': dataset_util.bytes_feature(encoded_image_data), - 'image/format': dataset_util.bytes_feature(image_format), - 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), - 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), - 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), - 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), - 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), - 'image/object/class/label': dataset_util.int64_list_feature(classes), - })) - return tf_example -``` - -## Conversion Script Outline {#conversion-script-outline} - -A typical conversion script will look like the following: - -```python - -import tensorflow as tf - -from object_detection.utils import dataset_util - - -flags = tf.app.flags -flags.DEFINE_string('output_path', '', 'Path to output TFRecord') -FLAGS = flags.FLAGS - - -def create_tf_example(example): - # TODO(user): Populate the following variables from your example. - height = None # Image height - width = None # Image width - filename = None # Filename of the image. Empty if image is not from file - encoded_image_data = None # Encoded image bytes - image_format = None # b'jpeg' or b'png' - - xmins = [] # List of normalized left x coordinates in bounding box (1 per box) - xmaxs = [] # List of normalized right x coordinates in bounding box - # (1 per box) - ymins = [] # List of normalized top y coordinates in bounding box (1 per box) - ymaxs = [] # List of normalized bottom y coordinates in bounding box - # (1 per box) - classes_text = [] # List of string class name of bounding box (1 per box) - classes = [] # List of integer class id of bounding box (1 per box) - - tf_example = tf.train.Example(features=tf.train.Features(feature={ - 'image/height': dataset_util.int64_feature(height), - 'image/width': dataset_util.int64_feature(width), - 'image/filename': dataset_util.bytes_feature(filename), - 'image/source_id': dataset_util.bytes_feature(filename), - 'image/encoded': dataset_util.bytes_feature(encoded_image_data), - 'image/format': dataset_util.bytes_feature(image_format), - 'image/object/bbox/xmin': dataset_util.float_list_feature(xmins), - 'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs), - 'image/object/bbox/ymin': dataset_util.float_list_feature(ymins), - 'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs), - 'image/object/class/text': dataset_util.bytes_list_feature(classes_text), - 'image/object/class/label': dataset_util.int64_list_feature(classes), - })) - return tf_example - - -def main(_): - writer = tf.python_io.TFRecordWriter(FLAGS.output_path) - - # TODO(user): Write code to read in your dataset to examples variable - - for example in examples: - tf_example = create_tf_example(example) - writer.write(tf_example.SerializeToString()) - - writer.close() - - -if __name__ == '__main__': - tf.app.run() - -``` - -Note: You may notice additional fields in some other datasets. They are -currently unused by the API and are optional. - -Note: Please refer to the section on [Running an Instance Segmentation -Model](instance_segmentation.md) for instructions on how to configure a model -that predicts masks in addition to object bounding boxes. - -## Sharding datasets - -When you have more than a few thousand examples, it is beneficial to shard your -dataset into multiple files: - -* tf.data.Dataset API can read input examples in parallel improving - throughput. -* tf.data.Dataset API can shuffle the examples better with sharded files which - improves performance of the model slightly. - -Instead of writing all tf.Example protos to a single file as shown in -[conversion script outline](#conversion-script-outline), use the snippet below. - -```python -import contextlib2 -from object_detection.dataset_tools import tf_record_creation_util - -num_shards=10 -output_filebase='/path/to/train_dataset.record' - -with contextlib2.ExitStack() as tf_record_close_stack: - output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords( - tf_record_close_stack, output_filebase, num_shards) - for index, example in examples: - tf_example = create_tf_example(example) - output_shard_index = index % num_shards - output_tfrecords[output_shard_index].write(tf_example.SerializeToString()) -``` - -This will produce the following output files - -```bash -/path/to/train_dataset.record-00000-00010 -/path/to/train_dataset.record-00001-00010 -... -/path/to/train_dataset.record-00009-00010 -``` - -which can then be used in the config file as below. - -```bash -tf_record_input_reader { - input_path: "/path/to/train_dataset.record-?????-of-00010" -} -``` diff --git a/research/object_detection/inference/__init__.py b/research/object_detection/inference/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/inference/detection_inference.py b/research/object_detection/inference/detection_inference.py deleted file mode 100644 index b395cd7e74b..00000000000 --- a/research/object_detection/inference/detection_inference.py +++ /dev/null @@ -1,141 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Utility functions for detection inference.""" -from __future__ import division - -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields - - -def build_input(tfrecord_paths): - """Builds the graph's input. - - Args: - tfrecord_paths: List of paths to the input TFRecords - - Returns: - serialized_example_tensor: The next serialized example. String scalar Tensor - image_tensor: The decoded image of the example. Uint8 tensor, - shape=[1, None, None,3] - """ - filename_queue = tf.train.string_input_producer( - tfrecord_paths, shuffle=False, num_epochs=1) - - tf_record_reader = tf.TFRecordReader() - _, serialized_example_tensor = tf_record_reader.read(filename_queue) - features = tf.parse_single_example( - serialized_example_tensor, - features={ - standard_fields.TfExampleFields.image_encoded: - tf.FixedLenFeature([], tf.string), - }) - encoded_image = features[standard_fields.TfExampleFields.image_encoded] - image_tensor = tf.image.decode_image(encoded_image, channels=3) - image_tensor.set_shape([None, None, 3]) - image_tensor = tf.expand_dims(image_tensor, 0) - - return serialized_example_tensor, image_tensor - - -def build_inference_graph(image_tensor, inference_graph_path): - """Loads the inference graph and connects it to the input image. - - Args: - image_tensor: The input image. uint8 tensor, shape=[1, None, None, 3] - inference_graph_path: Path to the inference graph with embedded weights - - Returns: - detected_boxes_tensor: Detected boxes. Float tensor, - shape=[num_detections, 4] - detected_scores_tensor: Detected scores. Float tensor, - shape=[num_detections] - detected_labels_tensor: Detected labels. Int64 tensor, - shape=[num_detections] - """ - with tf.gfile.Open(inference_graph_path, 'rb') as graph_def_file: - graph_content = graph_def_file.read() - graph_def = tf.GraphDef() - graph_def.MergeFromString(graph_content) - - tf.import_graph_def( - graph_def, name='', input_map={'image_tensor': image_tensor}) - - g = tf.get_default_graph() - - num_detections_tensor = tf.squeeze( - g.get_tensor_by_name('num_detections:0'), 0) - num_detections_tensor = tf.cast(num_detections_tensor, tf.int32) - - detected_boxes_tensor = tf.squeeze( - g.get_tensor_by_name('detection_boxes:0'), 0) - detected_boxes_tensor = detected_boxes_tensor[:num_detections_tensor] - - detected_scores_tensor = tf.squeeze( - g.get_tensor_by_name('detection_scores:0'), 0) - detected_scores_tensor = detected_scores_tensor[:num_detections_tensor] - - detected_labels_tensor = tf.squeeze( - g.get_tensor_by_name('detection_classes:0'), 0) - detected_labels_tensor = tf.cast(detected_labels_tensor, tf.int64) - detected_labels_tensor = detected_labels_tensor[:num_detections_tensor] - - return detected_boxes_tensor, detected_scores_tensor, detected_labels_tensor - - -def infer_detections_and_add_to_example( - serialized_example_tensor, detected_boxes_tensor, detected_scores_tensor, - detected_labels_tensor, discard_image_pixels): - """Runs the supplied tensors and adds the inferred detections to the example. - - Args: - serialized_example_tensor: Serialized TF example. Scalar string tensor - detected_boxes_tensor: Detected boxes. Float tensor, - shape=[num_detections, 4] - detected_scores_tensor: Detected scores. Float tensor, - shape=[num_detections] - detected_labels_tensor: Detected labels. Int64 tensor, - shape=[num_detections] - discard_image_pixels: If true, discards the image from the result - Returns: - The de-serialized TF example augmented with the inferred detections. - """ - tf_example = tf.train.Example() - (serialized_example, detected_boxes, detected_scores, - detected_classes) = tf.get_default_session().run([ - serialized_example_tensor, detected_boxes_tensor, detected_scores_tensor, - detected_labels_tensor - ]) - detected_boxes = detected_boxes.T - - tf_example.ParseFromString(serialized_example) - feature = tf_example.features.feature - feature[standard_fields.TfExampleFields. - detection_score].float_list.value[:] = detected_scores - feature[standard_fields.TfExampleFields. - detection_bbox_ymin].float_list.value[:] = detected_boxes[0] - feature[standard_fields.TfExampleFields. - detection_bbox_xmin].float_list.value[:] = detected_boxes[1] - feature[standard_fields.TfExampleFields. - detection_bbox_ymax].float_list.value[:] = detected_boxes[2] - feature[standard_fields.TfExampleFields. - detection_bbox_xmax].float_list.value[:] = detected_boxes[3] - feature[standard_fields.TfExampleFields. - detection_class_label].int64_list.value[:] = detected_classes - - if discard_image_pixels: - del feature[standard_fields.TfExampleFields.image_encoded] - - return tf_example diff --git a/research/object_detection/inference/detection_inference_tf1_test.py b/research/object_detection/inference/detection_inference_tf1_test.py deleted file mode 100644 index 899da129876..00000000000 --- a/research/object_detection/inference/detection_inference_tf1_test.py +++ /dev/null @@ -1,177 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Tests for detection_inference.py.""" - -import os -import unittest -import numpy as np -from PIL import Image -import six -import tensorflow.compat.v1 as tf -from google.protobuf import text_format - -from object_detection.core import standard_fields -from object_detection.inference import detection_inference -from object_detection.utils import dataset_util -from object_detection.utils import tf_version - - -def get_mock_tfrecord_path(): - return os.path.join(tf.test.get_temp_dir(), 'mock.tfrec') - - -def create_mock_tfrecord(): - pil_image = Image.fromarray(np.array([[[123, 0, 0]]], dtype=np.uint8), 'RGB') - image_output_stream = six.BytesIO() - pil_image.save(image_output_stream, format='png') - encoded_image = image_output_stream.getvalue() - - feature_map = { - 'test_field': - dataset_util.float_list_feature([1, 2, 3, 4]), - standard_fields.TfExampleFields.image_encoded: - dataset_util.bytes_feature(encoded_image), - } - - tf_example = tf.train.Example(features=tf.train.Features(feature=feature_map)) - with tf.python_io.TFRecordWriter(get_mock_tfrecord_path()) as writer: - writer.write(tf_example.SerializeToString()) - return encoded_image - - -def get_mock_graph_path(): - return os.path.join(tf.test.get_temp_dir(), 'mock_graph.pb') - - -def create_mock_graph(): - g = tf.Graph() - with g.as_default(): - in_image_tensor = tf.placeholder( - tf.uint8, shape=[1, None, None, 3], name='image_tensor') - tf.constant([2.0], name='num_detections') - tf.constant( - [[[0, 0.8, 0.7, 1], [0.1, 0.2, 0.8, 0.9], [0.2, 0.3, 0.4, 0.5]]], - name='detection_boxes') - tf.constant([[0.1, 0.2, 0.3]], name='detection_scores') - tf.identity( - tf.constant([[1.0, 2.0, 3.0]]) * - tf.reduce_sum(tf.cast(in_image_tensor, dtype=tf.float32)), - name='detection_classes') - graph_def = g.as_graph_def() - - with tf.gfile.Open(get_mock_graph_path(), 'w') as fl: - fl.write(graph_def.SerializeToString()) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class InferDetectionsTests(tf.test.TestCase): - - def test_simple(self): - create_mock_graph() - encoded_image = create_mock_tfrecord() - - serialized_example_tensor, image_tensor = detection_inference.build_input( - [get_mock_tfrecord_path()]) - self.assertAllEqual(image_tensor.get_shape().as_list(), [1, None, None, 3]) - - (detected_boxes_tensor, detected_scores_tensor, - detected_labels_tensor) = detection_inference.build_inference_graph( - image_tensor, get_mock_graph_path()) - - with self.test_session(use_gpu=False) as sess: - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - tf.train.start_queue_runners() - - tf_example = detection_inference.infer_detections_and_add_to_example( - serialized_example_tensor, detected_boxes_tensor, - detected_scores_tensor, detected_labels_tensor, False) - expected_example = tf.train.Example() - text_format.Merge(r""" - features { - feature { - key: "image/detection/bbox/ymin" - value { float_list { value: [0.0, 0.1] } } } - feature { - key: "image/detection/bbox/xmin" - value { float_list { value: [0.8, 0.2] } } } - feature { - key: "image/detection/bbox/ymax" - value { float_list { value: [0.7, 0.8] } } } - feature { - key: "image/detection/bbox/xmax" - value { float_list { value: [1.0, 0.9] } } } - feature { - key: "image/detection/label" - value { int64_list { value: [123, 246] } } } - feature { - key: "image/detection/score" - value { float_list { value: [0.1, 0.2] } } } - feature { - key: "test_field" - value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } }""", - expected_example) - expected_example.features.feature[ - standard_fields.TfExampleFields - .image_encoded].CopyFrom(dataset_util.bytes_feature(encoded_image)) - self.assertProtoEquals(expected_example, tf_example) - - def test_discard_image(self): - create_mock_graph() - create_mock_tfrecord() - - serialized_example_tensor, image_tensor = detection_inference.build_input( - [get_mock_tfrecord_path()]) - (detected_boxes_tensor, detected_scores_tensor, - detected_labels_tensor) = detection_inference.build_inference_graph( - image_tensor, get_mock_graph_path()) - - with self.test_session(use_gpu=False) as sess: - sess.run(tf.global_variables_initializer()) - sess.run(tf.local_variables_initializer()) - tf.train.start_queue_runners() - - tf_example = detection_inference.infer_detections_and_add_to_example( - serialized_example_tensor, detected_boxes_tensor, - detected_scores_tensor, detected_labels_tensor, True) - - self.assertProtoEquals(r""" - features { - feature { - key: "image/detection/bbox/ymin" - value { float_list { value: [0.0, 0.1] } } } - feature { - key: "image/detection/bbox/xmin" - value { float_list { value: [0.8, 0.2] } } } - feature { - key: "image/detection/bbox/ymax" - value { float_list { value: [0.7, 0.8] } } } - feature { - key: "image/detection/bbox/xmax" - value { float_list { value: [1.0, 0.9] } } } - feature { - key: "image/detection/label" - value { int64_list { value: [123, 246] } } } - feature { - key: "image/detection/score" - value { float_list { value: [0.1, 0.2] } } } - feature { - key: "test_field" - value { float_list { value: [1.0, 2.0, 3.0, 4.0] } } } } - """, tf_example) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/inference/infer_detections.py b/research/object_detection/inference/infer_detections.py deleted file mode 100644 index 7bc662f4297..00000000000 --- a/research/object_detection/inference/infer_detections.py +++ /dev/null @@ -1,96 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Infers detections on a TFRecord of TFExamples given an inference graph. - -Example usage: - ./infer_detections \ - --input_tfrecord_paths=/path/to/input/tfrecord1,/path/to/input/tfrecord2 \ - --output_tfrecord_path=/path/to/output/detections.tfrecord \ - --inference_graph=/path/to/frozen_weights_inference_graph.pb - -The output is a TFRecord of TFExamples. Each TFExample from the input is first -augmented with detections from the inference graph and then copied to the -output. - -The input and output nodes of the inference graph are expected to have the same -types, shapes, and semantics, as the input and output nodes of graphs produced -by export_inference_graph.py, when run with --input_type=image_tensor. - -The script can also discard the image pixels in the output. This greatly -reduces the output size and can potentially accelerate reading data in -subsequent processing steps that don't require the images (e.g. computing -metrics). -""" - -import itertools -import tensorflow.compat.v1 as tf -from object_detection.inference import detection_inference - -tf.flags.DEFINE_string('input_tfrecord_paths', None, - 'A comma separated list of paths to input TFRecords.') -tf.flags.DEFINE_string('output_tfrecord_path', None, - 'Path to the output TFRecord.') -tf.flags.DEFINE_string('inference_graph', None, - 'Path to the inference graph with embedded weights.') -tf.flags.DEFINE_boolean('discard_image_pixels', False, - 'Discards the images in the output TFExamples. This' - ' significantly reduces the output size and is useful' - ' if the subsequent tools don\'t need access to the' - ' images (e.g. when computing evaluation measures).') - -FLAGS = tf.flags.FLAGS - - -def main(_): - tf.logging.set_verbosity(tf.logging.INFO) - - required_flags = ['input_tfrecord_paths', 'output_tfrecord_path', - 'inference_graph'] - for flag_name in required_flags: - if not getattr(FLAGS, flag_name): - raise ValueError('Flag --{} is required'.format(flag_name)) - - with tf.Session() as sess: - input_tfrecord_paths = [ - v for v in FLAGS.input_tfrecord_paths.split(',') if v] - tf.logging.info('Reading input from %d files', len(input_tfrecord_paths)) - serialized_example_tensor, image_tensor = detection_inference.build_input( - input_tfrecord_paths) - tf.logging.info('Reading graph and building model...') - (detected_boxes_tensor, detected_scores_tensor, - detected_labels_tensor) = detection_inference.build_inference_graph( - image_tensor, FLAGS.inference_graph) - - tf.logging.info('Running inference and writing output to {}'.format( - FLAGS.output_tfrecord_path)) - sess.run(tf.local_variables_initializer()) - tf.train.start_queue_runners() - with tf.python_io.TFRecordWriter( - FLAGS.output_tfrecord_path) as tf_record_writer: - try: - for counter in itertools.count(): - tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 10, - counter) - tf_example = detection_inference.infer_detections_and_add_to_example( - serialized_example_tensor, detected_boxes_tensor, - detected_scores_tensor, detected_labels_tensor, - FLAGS.discard_image_pixels) - tf_record_writer.write(tf_example.SerializeToString()) - except tf.errors.OutOfRangeError: - tf.logging.info('Finished processing records') - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/inputs.py b/research/object_detection/inputs.py deleted file mode 100644 index 03b68a85e90..00000000000 --- a/research/object_detection/inputs.py +++ /dev/null @@ -1,1214 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Model input function for tf-learn object detection model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools - -import tensorflow.compat.v1 as tf -from tensorflow.compat.v1 import estimator as tf_estimator -from object_detection.builders import dataset_builder -from object_detection.builders import image_resizer_builder -from object_detection.builders import model_builder -from object_detection.builders import preprocessor_builder -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import densepose_ops -from object_detection.core import keypoint_ops -from object_detection.core import preprocessor -from object_detection.core import standard_fields as fields -from object_detection.data_decoders import tf_example_decoder -from object_detection.protos import eval_pb2 -from object_detection.protos import image_resizer_pb2 -from object_detection.protos import input_reader_pb2 -from object_detection.protos import model_pb2 -from object_detection.protos import train_pb2 -from object_detection.utils import config_util -from object_detection.utils import ops as util_ops -from object_detection.utils import shape_utils - -HASH_KEY = 'hash' -HASH_BINS = 1 << 31 -SERVING_FED_EXAMPLE_KEY = 'serialized_example' -_LABEL_OFFSET = 1 - -# A map of names to methods that help build the input pipeline. -INPUT_BUILDER_UTIL_MAP = { - 'dataset_build': dataset_builder.build, - 'model_build': model_builder.build, -} - - -def _multiclass_scores_or_one_hot_labels(multiclass_scores, groundtruth_boxes, - groundtruth_classes, num_classes): - """Returns one-hot encoding of classes when multiclass_scores is empty.""" - - # Replace groundtruth_classes tensor with multiclass_scores tensor when its - # non-empty. If multiclass_scores is empty fall back on groundtruth_classes - # tensor. - def true_fn(): - return tf.reshape(multiclass_scores, - [tf.shape(groundtruth_boxes)[0], num_classes]) - - def false_fn(): - return tf.one_hot(groundtruth_classes, num_classes) - - return tf.cond(tf.size(multiclass_scores) > 0, true_fn, false_fn) - - -def convert_labeled_classes_to_k_hot(groundtruth_labeled_classes, - num_classes, - map_empty_to_ones=False): - """Returns k-hot encoding of the labeled classes. - - If map_empty_to_ones is enabled and the input labeled_classes is empty, - this function assumes all classes are exhaustively labeled, thus returning - an all-one encoding. - - Args: - groundtruth_labeled_classes: a Tensor holding a sparse representation of - labeled classes. - num_classes: an integer representing the number of classes - map_empty_to_ones: boolean (default: False). Set this to be True to default - to an all-ones result if given an empty `groundtruth_labeled_classes`. - Returns: - A k-hot (and 0-indexed) tensor representation of - `groundtruth_labeled_classes`. - """ - - # If the input labeled_classes is empty, it assumes all classes are - # exhaustively labeled, thus returning an all-one encoding. - def true_fn(): - return tf.sparse_to_dense( - groundtruth_labeled_classes - _LABEL_OFFSET, [num_classes], - tf.constant(1, dtype=tf.float32), - validate_indices=False) - - def false_fn(): - return tf.ones(num_classes, dtype=tf.float32) - - if map_empty_to_ones: - return tf.cond(tf.size(groundtruth_labeled_classes) > 0, true_fn, false_fn) - return true_fn() - - -def _remove_unrecognized_classes(class_ids, unrecognized_label): - """Returns class ids with unrecognized classes filtered out.""" - - recognized_indices = tf.squeeze( - tf.where(tf.greater(class_ids, unrecognized_label)), -1) - return tf.gather(class_ids, recognized_indices) - - -def assert_or_prune_invalid_boxes(boxes): - """Makes sure boxes have valid sizes (ymax >= ymin, xmax >= xmin). - - When the hardware supports assertions, the function raises an error when - boxes have an invalid size. If assertions are not supported (e.g. on TPU), - boxes with invalid sizes are filtered out. - - Args: - boxes: float tensor of shape [num_boxes, 4] - - Returns: - boxes: float tensor of shape [num_valid_boxes, 4] with invalid boxes - filtered out. - - Raises: - tf.errors.InvalidArgumentError: When we detect boxes with invalid size. - This is not supported on TPUs. - """ - - ymin, xmin, ymax, xmax = tf.split(boxes, num_or_size_splits=4, axis=1) - - height_check = tf.Assert(tf.reduce_all(ymax >= ymin), [ymin, ymax]) - width_check = tf.Assert(tf.reduce_all(xmax >= xmin), [xmin, xmax]) - - with tf.control_dependencies([height_check, width_check]): - boxes_tensor = tf.concat([ymin, xmin, ymax, xmax], axis=1) - boxlist = box_list.BoxList(boxes_tensor) - # TODO(b/149221748) Remove pruning when XLA supports assertions. - boxlist = box_list_ops.prune_small_boxes(boxlist, 0) - - return boxlist.get() - - -def transform_input_data(tensor_dict, - model_preprocess_fn, - image_resizer_fn, - num_classes, - data_augmentation_fn=None, - merge_multiple_boxes=False, - retain_original_image=False, - use_multiclass_scores=False, - use_bfloat16=False, - retain_original_image_additional_channels=False, - keypoint_type_weight=None, - image_classes_field_map_empty_to_ones=True): - """A single function that is responsible for all input data transformations. - - Data transformation functions are applied in the following order. - 1. If key fields.InputDataFields.image_additional_channels is present in - tensor_dict, the additional channels will be merged into - fields.InputDataFields.image. - 2. data_augmentation_fn (optional): applied on tensor_dict. - 3. model_preprocess_fn: applied only on image tensor in tensor_dict. - 4. keypoint_type_weight (optional): If groundtruth keypoints are in - the tensor dictionary, per-keypoint weights are produced. These weights are - initialized by `keypoint_type_weight` (or ones if left None). - Then, for all keypoints that are not visible, the weights are set to 0 (to - avoid penalizing the model in a loss function). - 5. image_resizer_fn: applied on original image and instance mask tensor in - tensor_dict. - 6. one_hot_encoding: applied to classes tensor in tensor_dict. - 7. merge_multiple_boxes (optional): when groundtruth boxes are exactly the - same they can be merged into a single box with an associated k-hot class - label. - - Args: - tensor_dict: dictionary containing input tensors keyed by - fields.InputDataFields. - model_preprocess_fn: model's preprocess function to apply on image tensor. - This function must take in a 4-D float tensor and return a 4-D preprocess - float tensor and a tensor containing the true image shape. - image_resizer_fn: image resizer function to apply on groundtruth instance - `masks. This function must take a 3-D float tensor of an image and a 3-D - tensor of instance masks and return a resized version of these along with - the true shapes. - num_classes: number of max classes to one-hot (or k-hot) encode the class - labels. - data_augmentation_fn: (optional) data augmentation function to apply on - input `tensor_dict`. - merge_multiple_boxes: (optional) whether to merge multiple groundtruth boxes - and classes for a given image if the boxes are exactly the same. - retain_original_image: (optional) whether to retain original image in the - output dictionary. - use_multiclass_scores: whether to use multiclass scores as class targets - instead of one-hot encoding of `groundtruth_classes`. When - this is True and multiclass_scores is empty, one-hot encoding of - `groundtruth_classes` is used as a fallback. - use_bfloat16: (optional) a bool, whether to use bfloat16 in training. - retain_original_image_additional_channels: (optional) Whether to retain - original image additional channels in the output dictionary. - keypoint_type_weight: A list (of length num_keypoints) containing - groundtruth loss weights to use for each keypoint. If None, will use a - weight of 1. - image_classes_field_map_empty_to_ones: A boolean flag indicating if empty - image classes field indicates that all classes have been labeled on this - image [true] or none [false]. - - Returns: - A dictionary keyed by fields.InputDataFields containing the tensors obtained - after applying all the transformations. - - Raises: - KeyError: If both groundtruth_labeled_classes and groundtruth_image_classes - are provided by the decoder in tensor_dict since both fields are - considered to contain the same information. - """ - out_tensor_dict = tensor_dict.copy() - - input_fields = fields.InputDataFields - labeled_classes_field = input_fields.groundtruth_labeled_classes - image_classes_field = input_fields.groundtruth_image_classes - verified_neg_classes_field = input_fields.groundtruth_verified_neg_classes - not_exhaustive_field = input_fields.groundtruth_not_exhaustive_classes - - if (labeled_classes_field in out_tensor_dict and - image_classes_field in out_tensor_dict): - raise KeyError('groundtruth_labeled_classes and groundtruth_image_classes' - 'are provided by the decoder, but only one should be set.') - - for field, map_empty_to_ones in [(labeled_classes_field, True), - (image_classes_field, - image_classes_field_map_empty_to_ones), - (verified_neg_classes_field, False), - (not_exhaustive_field, False)]: - if field in out_tensor_dict: - out_tensor_dict[field] = _remove_unrecognized_classes( - out_tensor_dict[field], unrecognized_label=-1) - out_tensor_dict[field] = convert_labeled_classes_to_k_hot( - out_tensor_dict[field], num_classes, map_empty_to_ones) - - if input_fields.multiclass_scores in out_tensor_dict: - out_tensor_dict[ - input_fields - .multiclass_scores] = _multiclass_scores_or_one_hot_labels( - out_tensor_dict[input_fields.multiclass_scores], - out_tensor_dict[input_fields.groundtruth_boxes], - out_tensor_dict[input_fields.groundtruth_classes], - num_classes) - - if input_fields.groundtruth_boxes in out_tensor_dict: - out_tensor_dict = util_ops.filter_groundtruth_with_nan_box_coordinates( - out_tensor_dict) - out_tensor_dict = util_ops.filter_unrecognized_classes(out_tensor_dict) - - if retain_original_image: - out_tensor_dict[input_fields.original_image] = tf.cast( - image_resizer_fn(out_tensor_dict[input_fields.image], - None)[0], tf.uint8) - - if input_fields.image_additional_channels in out_tensor_dict: - channels = out_tensor_dict[input_fields.image_additional_channels] - out_tensor_dict[input_fields.image] = tf.concat( - [out_tensor_dict[input_fields.image], channels], axis=2) - if retain_original_image_additional_channels: - out_tensor_dict[ - input_fields.image_additional_channels] = tf.cast( - image_resizer_fn(channels, None)[0], tf.uint8) - - # Apply data augmentation ops. - if data_augmentation_fn is not None: - out_tensor_dict = data_augmentation_fn(out_tensor_dict) - - # Apply model preprocessing ops and resize instance masks. - image = out_tensor_dict[input_fields.image] - preprocessed_resized_image, true_image_shape = model_preprocess_fn( - tf.expand_dims(tf.cast(image, dtype=tf.float32), axis=0)) - - preprocessed_shape = tf.shape(preprocessed_resized_image) - new_height, new_width = preprocessed_shape[1], preprocessed_shape[2] - - im_box = tf.stack([ - 0.0, 0.0, - tf.to_float(new_height) / tf.to_float(true_image_shape[0, 0]), - tf.to_float(new_width) / tf.to_float(true_image_shape[0, 1]) - ]) - - if input_fields.groundtruth_boxes in tensor_dict: - bboxes = out_tensor_dict[input_fields.groundtruth_boxes] - boxlist = box_list.BoxList(bboxes) - realigned_bboxes = box_list_ops.change_coordinate_frame(boxlist, im_box) - - realigned_boxes_tensor = realigned_bboxes.get() - valid_boxes_tensor = assert_or_prune_invalid_boxes(realigned_boxes_tensor) - out_tensor_dict[ - input_fields.groundtruth_boxes] = valid_boxes_tensor - - if input_fields.groundtruth_keypoints in tensor_dict: - keypoints = out_tensor_dict[input_fields.groundtruth_keypoints] - realigned_keypoints = keypoint_ops.change_coordinate_frame(keypoints, - im_box) - out_tensor_dict[ - input_fields.groundtruth_keypoints] = realigned_keypoints - flds_gt_kpt = input_fields.groundtruth_keypoints - flds_gt_kpt_vis = input_fields.groundtruth_keypoint_visibilities - flds_gt_kpt_weights = input_fields.groundtruth_keypoint_weights - if flds_gt_kpt_vis not in out_tensor_dict: - out_tensor_dict[flds_gt_kpt_vis] = tf.ones_like( - out_tensor_dict[flds_gt_kpt][:, :, 0], - dtype=tf.bool) - flds_gt_kpt_depth = fields.InputDataFields.groundtruth_keypoint_depths - flds_gt_kpt_depth_weight = ( - fields.InputDataFields.groundtruth_keypoint_depth_weights) - if flds_gt_kpt_depth in out_tensor_dict: - out_tensor_dict[flds_gt_kpt_depth] = out_tensor_dict[flds_gt_kpt_depth] - out_tensor_dict[flds_gt_kpt_depth_weight] = out_tensor_dict[ - flds_gt_kpt_depth_weight] - - out_tensor_dict[flds_gt_kpt_weights] = ( - keypoint_ops.keypoint_weights_from_visibilities( - out_tensor_dict[flds_gt_kpt_vis], - keypoint_type_weight)) - - dp_surface_coords_fld = input_fields.groundtruth_dp_surface_coords - if dp_surface_coords_fld in tensor_dict: - dp_surface_coords = out_tensor_dict[dp_surface_coords_fld] - realigned_dp_surface_coords = densepose_ops.change_coordinate_frame( - dp_surface_coords, im_box) - out_tensor_dict[dp_surface_coords_fld] = realigned_dp_surface_coords - - if use_bfloat16: - preprocessed_resized_image = tf.cast( - preprocessed_resized_image, tf.bfloat16) - if input_fields.context_features in out_tensor_dict: - out_tensor_dict[input_fields.context_features] = tf.cast( - out_tensor_dict[input_fields.context_features], tf.bfloat16) - out_tensor_dict[input_fields.image] = tf.squeeze( - preprocessed_resized_image, axis=0) - out_tensor_dict[input_fields.true_image_shape] = tf.squeeze( - true_image_shape, axis=0) - if input_fields.groundtruth_instance_masks in out_tensor_dict: - masks = out_tensor_dict[input_fields.groundtruth_instance_masks] - _, resized_masks, _ = image_resizer_fn(image, masks) - if use_bfloat16: - resized_masks = tf.cast(resized_masks, tf.bfloat16) - out_tensor_dict[ - input_fields.groundtruth_instance_masks] = resized_masks - - zero_indexed_groundtruth_classes = out_tensor_dict[ - input_fields.groundtruth_classes] - _LABEL_OFFSET - if use_multiclass_scores: - out_tensor_dict[ - input_fields.groundtruth_classes] = out_tensor_dict[ - input_fields.multiclass_scores] - else: - out_tensor_dict[input_fields.groundtruth_classes] = tf.one_hot( - zero_indexed_groundtruth_classes, num_classes) - out_tensor_dict.pop(input_fields.multiclass_scores, None) - - if input_fields.groundtruth_confidences in out_tensor_dict: - groundtruth_confidences = out_tensor_dict[ - input_fields.groundtruth_confidences] - # Map the confidences to the one-hot encoding of classes - out_tensor_dict[input_fields.groundtruth_confidences] = ( - tf.reshape(groundtruth_confidences, [-1, 1]) * - out_tensor_dict[input_fields.groundtruth_classes]) - else: - groundtruth_confidences = tf.ones_like( - zero_indexed_groundtruth_classes, dtype=tf.float32) - out_tensor_dict[input_fields.groundtruth_confidences] = ( - out_tensor_dict[input_fields.groundtruth_classes]) - - if merge_multiple_boxes: - merged_boxes, merged_classes, merged_confidences, _ = ( - util_ops.merge_boxes_with_multiple_labels( - out_tensor_dict[input_fields.groundtruth_boxes], - zero_indexed_groundtruth_classes, - groundtruth_confidences, - num_classes)) - merged_classes = tf.cast(merged_classes, tf.float32) - out_tensor_dict[input_fields.groundtruth_boxes] = merged_boxes - out_tensor_dict[input_fields.groundtruth_classes] = merged_classes - out_tensor_dict[input_fields.groundtruth_confidences] = ( - merged_confidences) - if input_fields.groundtruth_boxes in out_tensor_dict: - out_tensor_dict[input_fields.num_groundtruth_boxes] = tf.shape( - out_tensor_dict[input_fields.groundtruth_boxes])[0] - - return out_tensor_dict - - -def pad_input_data_to_static_shapes(tensor_dict, - max_num_boxes, - num_classes, - spatial_image_shape=None, - max_num_context_features=None, - context_feature_length=None, - max_dp_points=336): - """Pads input tensors to static shapes. - - In case num_additional_channels > 0, we assume that the additional channels - have already been concatenated to the base image. - - Args: - tensor_dict: Tensor dictionary of input data - max_num_boxes: Max number of groundtruth boxes needed to compute shapes for - padding. - num_classes: Number of classes in the dataset needed to compute shapes for - padding. - spatial_image_shape: A list of two integers of the form [height, width] - containing expected spatial shape of the image. - max_num_context_features (optional): The maximum number of context - features needed to compute shapes padding. - context_feature_length (optional): The length of the context feature. - max_dp_points (optional): The maximum number of DensePose sampled points per - instance. The default (336) is selected since the original DensePose paper - (https://arxiv.org/pdf/1802.00434.pdf) indicates that the maximum number - of samples per part is 14, and therefore 24 * 14 = 336 is the maximum - sampler per instance. - - Returns: - A dictionary keyed by fields.InputDataFields containing padding shapes for - tensors in the dataset. - - Raises: - ValueError: If groundtruth classes is neither rank 1 nor rank 2, or if we - detect that additional channels have not been concatenated yet, or if - max_num_context_features is not specified and context_features is in the - tensor dict. - """ - if not spatial_image_shape or spatial_image_shape == [-1, -1]: - height, width = None, None - else: - height, width = spatial_image_shape # pylint: disable=unpacking-non-sequence - - input_fields = fields.InputDataFields - num_additional_channels = 0 - if input_fields.image_additional_channels in tensor_dict: - num_additional_channels = shape_utils.get_dim_as_int(tensor_dict[ - input_fields.image_additional_channels].shape[2]) - - # We assume that if num_additional_channels > 0, then it has already been - # concatenated to the base image (but not the ground truth). - num_channels = 3 - if input_fields.image in tensor_dict: - num_channels = shape_utils.get_dim_as_int( - tensor_dict[input_fields.image].shape[2]) - - if num_additional_channels: - if num_additional_channels >= num_channels: - raise ValueError( - 'Image must be already concatenated with additional channels.') - - if (input_fields.original_image in tensor_dict and - shape_utils.get_dim_as_int( - tensor_dict[input_fields.original_image].shape[2]) == - num_channels): - raise ValueError( - 'Image must be already concatenated with additional channels.') - - if input_fields.context_features in tensor_dict and ( - max_num_context_features is None): - raise ValueError('max_num_context_features must be specified in the model ' - 'config if include_context is specified in the input ' - 'config') - - padding_shapes = { - input_fields.image: [height, width, num_channels], - input_fields.original_image_spatial_shape: [2], - input_fields.image_additional_channels: [ - height, width, num_additional_channels - ], - input_fields.source_id: [], - input_fields.filename: [], - input_fields.key: [], - input_fields.groundtruth_difficult: [max_num_boxes], - input_fields.groundtruth_boxes: [max_num_boxes, 4], - input_fields.groundtruth_classes: [max_num_boxes, num_classes], - input_fields.groundtruth_instance_masks: [ - max_num_boxes, height, width - ], - input_fields.groundtruth_instance_mask_weights: [max_num_boxes], - input_fields.groundtruth_is_crowd: [max_num_boxes], - input_fields.groundtruth_group_of: [max_num_boxes], - input_fields.groundtruth_area: [max_num_boxes], - input_fields.groundtruth_weights: [max_num_boxes], - input_fields.groundtruth_confidences: [ - max_num_boxes, num_classes - ], - input_fields.num_groundtruth_boxes: [], - input_fields.groundtruth_label_types: [max_num_boxes], - input_fields.groundtruth_label_weights: [max_num_boxes], - input_fields.true_image_shape: [3], - input_fields.groundtruth_image_classes: [num_classes], - input_fields.groundtruth_image_confidences: [num_classes], - input_fields.groundtruth_labeled_classes: [num_classes], - } - - if input_fields.original_image in tensor_dict: - padding_shapes[input_fields.original_image] = [ - height, width, - shape_utils.get_dim_as_int(tensor_dict[input_fields. - original_image].shape[2]) - ] - if input_fields.groundtruth_keypoints in tensor_dict: - tensor_shape = ( - tensor_dict[input_fields.groundtruth_keypoints].shape) - padding_shape = [max_num_boxes, - shape_utils.get_dim_as_int(tensor_shape[1]), - shape_utils.get_dim_as_int(tensor_shape[2])] - padding_shapes[input_fields.groundtruth_keypoints] = padding_shape - if input_fields.groundtruth_keypoint_visibilities in tensor_dict: - tensor_shape = tensor_dict[input_fields. - groundtruth_keypoint_visibilities].shape - padding_shape = [max_num_boxes, shape_utils.get_dim_as_int(tensor_shape[1])] - padding_shapes[input_fields. - groundtruth_keypoint_visibilities] = padding_shape - - if fields.InputDataFields.groundtruth_keypoint_depths in tensor_dict: - tensor_shape = tensor_dict[fields.InputDataFields. - groundtruth_keypoint_depths].shape - padding_shape = [max_num_boxes, shape_utils.get_dim_as_int(tensor_shape[1])] - padding_shapes[fields.InputDataFields. - groundtruth_keypoint_depths] = padding_shape - padding_shapes[fields.InputDataFields. - groundtruth_keypoint_depth_weights] = padding_shape - - if input_fields.groundtruth_keypoint_weights in tensor_dict: - tensor_shape = ( - tensor_dict[input_fields.groundtruth_keypoint_weights].shape) - padding_shape = [max_num_boxes, shape_utils.get_dim_as_int(tensor_shape[1])] - padding_shapes[input_fields. - groundtruth_keypoint_weights] = padding_shape - if input_fields.groundtruth_dp_num_points in tensor_dict: - padding_shapes[ - input_fields.groundtruth_dp_num_points] = [max_num_boxes] - padding_shapes[ - input_fields.groundtruth_dp_part_ids] = [ - max_num_boxes, max_dp_points] - padding_shapes[ - input_fields.groundtruth_dp_surface_coords] = [ - max_num_boxes, max_dp_points, 4] - if input_fields.groundtruth_track_ids in tensor_dict: - padding_shapes[ - input_fields.groundtruth_track_ids] = [max_num_boxes] - - if input_fields.groundtruth_verified_neg_classes in tensor_dict: - padding_shapes[ - input_fields.groundtruth_verified_neg_classes] = [num_classes] - if input_fields.groundtruth_not_exhaustive_classes in tensor_dict: - padding_shapes[ - input_fields.groundtruth_not_exhaustive_classes] = [num_classes] - - # Prepare for ContextRCNN related fields. - if input_fields.context_features in tensor_dict: - padding_shape = [max_num_context_features, context_feature_length] - padding_shapes[input_fields.context_features] = padding_shape - - tensor_shape = tf.shape( - tensor_dict[fields.InputDataFields.context_features]) - tensor_dict[fields.InputDataFields.valid_context_size] = tensor_shape[0] - padding_shapes[fields.InputDataFields.valid_context_size] = [] - if fields.InputDataFields.context_feature_length in tensor_dict: - padding_shapes[fields.InputDataFields.context_feature_length] = [] - if fields.InputDataFields.context_features_image_id_list in tensor_dict: - padding_shapes[fields.InputDataFields.context_features_image_id_list] = [ - max_num_context_features] - - if input_fields.is_annotated in tensor_dict: - padding_shapes[input_fields.is_annotated] = [] - - padded_tensor_dict = {} - for tensor_name in tensor_dict: - padded_tensor_dict[tensor_name] = shape_utils.pad_or_clip_nd( - tensor_dict[tensor_name], padding_shapes[tensor_name]) - - # Make sure that the number of groundtruth boxes now reflects the - # padded/clipped tensors. - if input_fields.num_groundtruth_boxes in padded_tensor_dict: - padded_tensor_dict[input_fields.num_groundtruth_boxes] = ( - tf.minimum( - padded_tensor_dict[input_fields.num_groundtruth_boxes], - max_num_boxes)) - return padded_tensor_dict - - -def augment_input_data(tensor_dict, data_augmentation_options): - """Applies data augmentation ops to input tensors. - - Args: - tensor_dict: A dictionary of input tensors keyed by fields.InputDataFields. - data_augmentation_options: A list of tuples, where each tuple contains a - function and a dictionary that contains arguments and their values. - Usually, this is the output of core/preprocessor.build. - - Returns: - A dictionary of tensors obtained by applying data augmentation ops to the - input tensor dictionary. - """ - tensor_dict[fields.InputDataFields.image] = tf.expand_dims( - tf.cast(tensor_dict[fields.InputDataFields.image], dtype=tf.float32), 0) - - include_instance_masks = (fields.InputDataFields.groundtruth_instance_masks - in tensor_dict) - include_instance_mask_weights = ( - fields.InputDataFields.groundtruth_instance_mask_weights in tensor_dict) - include_keypoints = (fields.InputDataFields.groundtruth_keypoints - in tensor_dict) - include_keypoint_visibilities = ( - fields.InputDataFields.groundtruth_keypoint_visibilities in tensor_dict) - include_keypoint_depths = ( - fields.InputDataFields.groundtruth_keypoint_depths in tensor_dict) - include_label_weights = (fields.InputDataFields.groundtruth_weights - in tensor_dict) - include_label_confidences = (fields.InputDataFields.groundtruth_confidences - in tensor_dict) - include_multiclass_scores = (fields.InputDataFields.multiclass_scores in - tensor_dict) - dense_pose_fields = [fields.InputDataFields.groundtruth_dp_num_points, - fields.InputDataFields.groundtruth_dp_part_ids, - fields.InputDataFields.groundtruth_dp_surface_coords] - include_dense_pose = all(field in tensor_dict for field in dense_pose_fields) - tensor_dict = preprocessor.preprocess( - tensor_dict, data_augmentation_options, - func_arg_map=preprocessor.get_default_func_arg_map( - include_label_weights=include_label_weights, - include_label_confidences=include_label_confidences, - include_multiclass_scores=include_multiclass_scores, - include_instance_masks=include_instance_masks, - include_instance_mask_weights=include_instance_mask_weights, - include_keypoints=include_keypoints, - include_keypoint_visibilities=include_keypoint_visibilities, - include_dense_pose=include_dense_pose, - include_keypoint_depths=include_keypoint_depths)) - tensor_dict[fields.InputDataFields.image] = tf.squeeze( - tensor_dict[fields.InputDataFields.image], axis=0) - return tensor_dict - - -def _get_labels_dict(input_dict): - """Extracts labels dict from input dict.""" - required_label_keys = [ - fields.InputDataFields.num_groundtruth_boxes, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - fields.InputDataFields.groundtruth_weights, - ] - labels_dict = {} - for key in required_label_keys: - labels_dict[key] = input_dict[key] - - optional_label_keys = [ - fields.InputDataFields.groundtruth_confidences, - fields.InputDataFields.groundtruth_labeled_classes, - fields.InputDataFields.groundtruth_keypoints, - fields.InputDataFields.groundtruth_keypoint_depths, - fields.InputDataFields.groundtruth_keypoint_depth_weights, - fields.InputDataFields.groundtruth_instance_masks, - fields.InputDataFields.groundtruth_instance_mask_weights, - fields.InputDataFields.groundtruth_area, - fields.InputDataFields.groundtruth_is_crowd, - fields.InputDataFields.groundtruth_group_of, - fields.InputDataFields.groundtruth_difficult, - fields.InputDataFields.groundtruth_keypoint_visibilities, - fields.InputDataFields.groundtruth_keypoint_weights, - fields.InputDataFields.groundtruth_dp_num_points, - fields.InputDataFields.groundtruth_dp_part_ids, - fields.InputDataFields.groundtruth_dp_surface_coords, - fields.InputDataFields.groundtruth_track_ids, - fields.InputDataFields.groundtruth_verified_neg_classes, - fields.InputDataFields.groundtruth_not_exhaustive_classes, - fields.InputDataFields.groundtruth_image_classes, - ] - - for key in optional_label_keys: - if key in input_dict: - labels_dict[key] = input_dict[key] - if fields.InputDataFields.groundtruth_difficult in labels_dict: - labels_dict[fields.InputDataFields.groundtruth_difficult] = tf.cast( - labels_dict[fields.InputDataFields.groundtruth_difficult], tf.int32) - return labels_dict - - -def _replace_empty_string_with_random_number(string_tensor): - """Returns string unchanged if non-empty, and random string tensor otherwise. - - The random string is an integer 0 and 2**63 - 1, casted as string. - - - Args: - string_tensor: A tf.tensor of dtype string. - - Returns: - out_string: A tf.tensor of dtype string. If string_tensor contains the empty - string, out_string will contain a random integer casted to a string. - Otherwise string_tensor is returned unchanged. - - """ - - empty_string = tf.constant('', dtype=tf.string, name='EmptyString') - - random_source_id = tf.as_string( - tf.random_uniform(shape=[], maxval=2**63 - 1, dtype=tf.int64)) - - out_string = tf.cond( - tf.equal(string_tensor, empty_string), - true_fn=lambda: random_source_id, - false_fn=lambda: string_tensor) - - return out_string - - -def _get_features_dict(input_dict, include_source_id=False): - """Extracts features dict from input dict.""" - - source_id = _replace_empty_string_with_random_number( - input_dict[fields.InputDataFields.source_id]) - - hash_from_source_id = tf.string_to_hash_bucket_fast(source_id, HASH_BINS) - features = { - fields.InputDataFields.image: - input_dict[fields.InputDataFields.image], - HASH_KEY: tf.cast(hash_from_source_id, tf.int32), - fields.InputDataFields.true_image_shape: - input_dict[fields.InputDataFields.true_image_shape], - fields.InputDataFields.original_image_spatial_shape: - input_dict[fields.InputDataFields.original_image_spatial_shape] - } - if include_source_id: - features[fields.InputDataFields.source_id] = source_id - if fields.InputDataFields.original_image in input_dict: - features[fields.InputDataFields.original_image] = input_dict[ - fields.InputDataFields.original_image] - if fields.InputDataFields.image_additional_channels in input_dict: - features[fields.InputDataFields.image_additional_channels] = input_dict[ - fields.InputDataFields.image_additional_channels] - if fields.InputDataFields.context_features in input_dict: - features[fields.InputDataFields.context_features] = input_dict[ - fields.InputDataFields.context_features] - if fields.InputDataFields.valid_context_size in input_dict: - features[fields.InputDataFields.valid_context_size] = input_dict[ - fields.InputDataFields.valid_context_size] - if fields.InputDataFields.context_features_image_id_list in input_dict: - features[fields.InputDataFields.context_features_image_id_list] = ( - input_dict[fields.InputDataFields.context_features_image_id_list]) - return features - - -def create_train_input_fn(train_config, train_input_config, - model_config): - """Creates a train `input` function for `Estimator`. - - Args: - train_config: A train_pb2.TrainConfig. - train_input_config: An input_reader_pb2.InputReader. - model_config: A model_pb2.DetectionModel. - - Returns: - `input_fn` for `Estimator` in TRAIN mode. - """ - - def _train_input_fn(params=None): - return train_input(train_config, train_input_config, model_config, - params=params) - - return _train_input_fn - - -def train_input(train_config, train_input_config, - model_config, model=None, params=None, input_context=None): - """Returns `features` and `labels` tensor dictionaries for training. - - Args: - train_config: A train_pb2.TrainConfig. - train_input_config: An input_reader_pb2.InputReader. - model_config: A model_pb2.DetectionModel. - model: A pre-constructed Detection Model. - If None, one will be created from the config. - params: Parameter dictionary passed from the estimator. - input_context: optional, A tf.distribute.InputContext object used to - shard filenames and compute per-replica batch_size when this function - is being called per-replica. - - Returns: - A tf.data.Dataset that holds (features, labels) tuple. - - features: Dictionary of feature tensors. - features[fields.InputDataFields.image] is a [batch_size, H, W, C] - float32 tensor with preprocessed images. - features[HASH_KEY] is a [batch_size] int32 tensor representing unique - identifiers for the images. - features[fields.InputDataFields.true_image_shape] is a [batch_size, 3] - int32 tensor representing the true image shapes, as preprocessed - images could be padded. - features[fields.InputDataFields.original_image] (optional) is a - [batch_size, H, W, C] float32 tensor with original images. - labels: Dictionary of groundtruth tensors. - labels[fields.InputDataFields.num_groundtruth_boxes] is a [batch_size] - int32 tensor indicating the number of groundtruth boxes. - labels[fields.InputDataFields.groundtruth_boxes] is a - [batch_size, num_boxes, 4] float32 tensor containing the corners of - the groundtruth boxes. - labels[fields.InputDataFields.groundtruth_classes] is a - [batch_size, num_boxes, num_classes] float32 one-hot tensor of - classes. - labels[fields.InputDataFields.groundtruth_weights] is a - [batch_size, num_boxes] float32 tensor containing groundtruth weights - for the boxes. - -- Optional -- - labels[fields.InputDataFields.groundtruth_instance_masks] is a - [batch_size, num_boxes, H, W] float32 tensor containing only binary - values, which represent instance masks for objects. - labels[fields.InputDataFields.groundtruth_instance_mask_weights] is a - [batch_size, num_boxes] float32 tensor containing groundtruth weights - for each instance mask. - labels[fields.InputDataFields.groundtruth_keypoints] is a - [batch_size, num_boxes, num_keypoints, 2] float32 tensor containing - keypoints for each box. - labels[fields.InputDataFields.groundtruth_weights] is a - [batch_size, num_boxes, num_keypoints] float32 tensor containing - groundtruth weights for the keypoints. - labels[fields.InputDataFields.groundtruth_visibilities] is a - [batch_size, num_boxes, num_keypoints] bool tensor containing - groundtruth visibilities for each keypoint. - labels[fields.InputDataFields.groundtruth_labeled_classes] is a - [batch_size, num_classes] float32 k-hot tensor of classes. - labels[fields.InputDataFields.groundtruth_dp_num_points] is a - [batch_size, num_boxes] int32 tensor with the number of sampled - DensePose points per object. - labels[fields.InputDataFields.groundtruth_dp_part_ids] is a - [batch_size, num_boxes, max_sampled_points] int32 tensor with the - DensePose part ids (0-indexed) per object. - labels[fields.InputDataFields.groundtruth_dp_surface_coords] is a - [batch_size, num_boxes, max_sampled_points, 4] float32 tensor with the - DensePose surface coordinates. The format is (y, x, v, u), where (y, x) - are normalized image coordinates and (v, u) are normalized surface part - coordinates. - labels[fields.InputDataFields.groundtruth_track_ids] is a - [batch_size, num_boxes] int32 tensor with the track ID for each object. - - Raises: - TypeError: if the `train_config`, `train_input_config` or `model_config` - are not of the correct type. - """ - if not isinstance(train_config, train_pb2.TrainConfig): - raise TypeError('For training mode, the `train_config` must be a ' - 'train_pb2.TrainConfig.') - if not isinstance(train_input_config, input_reader_pb2.InputReader): - raise TypeError('The `train_input_config` must be a ' - 'input_reader_pb2.InputReader.') - if not isinstance(model_config, model_pb2.DetectionModel): - raise TypeError('The `model_config` must be a ' - 'model_pb2.DetectionModel.') - - if model is None: - model_preprocess_fn = INPUT_BUILDER_UTIL_MAP['model_build']( - model_config, is_training=True).preprocess - else: - model_preprocess_fn = model.preprocess - - num_classes = config_util.get_number_of_classes(model_config) - - def transform_and_pad_input_data_fn(tensor_dict): - """Combines transform and pad operation.""" - data_augmentation_options = [ - preprocessor_builder.build(step) - for step in train_config.data_augmentation_options - ] - data_augmentation_fn = functools.partial( - augment_input_data, - data_augmentation_options=data_augmentation_options) - - image_resizer_config = config_util.get_image_resizer_config(model_config) - image_resizer_fn = image_resizer_builder.build(image_resizer_config) - keypoint_type_weight = train_input_config.keypoint_type_weight or None - transform_data_fn = functools.partial( - transform_input_data, model_preprocess_fn=model_preprocess_fn, - image_resizer_fn=image_resizer_fn, - num_classes=num_classes, - data_augmentation_fn=data_augmentation_fn, - merge_multiple_boxes=train_config.merge_multiple_label_boxes, - retain_original_image=train_config.retain_original_images, - use_multiclass_scores=train_config.use_multiclass_scores, - use_bfloat16=train_config.use_bfloat16, - keypoint_type_weight=keypoint_type_weight) - - tensor_dict = pad_input_data_to_static_shapes( - tensor_dict=transform_data_fn(tensor_dict), - max_num_boxes=train_input_config.max_number_of_boxes, - num_classes=num_classes, - spatial_image_shape=config_util.get_spatial_image_size( - image_resizer_config), - max_num_context_features=config_util.get_max_num_context_features( - model_config), - context_feature_length=config_util.get_context_feature_length( - model_config)) - include_source_id = train_input_config.include_source_id - return (_get_features_dict(tensor_dict, include_source_id), - _get_labels_dict(tensor_dict)) - reduce_to_frame_fn = get_reduce_to_frame_fn(train_input_config, True) - - dataset = INPUT_BUILDER_UTIL_MAP['dataset_build']( - train_input_config, - transform_input_data_fn=transform_and_pad_input_data_fn, - batch_size=params['batch_size'] if params else train_config.batch_size, - input_context=input_context, - reduce_to_frame_fn=reduce_to_frame_fn) - return dataset - - -def create_eval_input_fn(eval_config, eval_input_config, model_config): - """Creates an eval `input` function for `Estimator`. - - Args: - eval_config: An eval_pb2.EvalConfig. - eval_input_config: An input_reader_pb2.InputReader. - model_config: A model_pb2.DetectionModel. - - Returns: - `input_fn` for `Estimator` in EVAL mode. - """ - - def _eval_input_fn(params=None): - return eval_input(eval_config, eval_input_config, model_config, - params=params) - - return _eval_input_fn - - -def eval_input(eval_config, eval_input_config, model_config, - model=None, params=None, input_context=None): - """Returns `features` and `labels` tensor dictionaries for evaluation. - - Args: - eval_config: An eval_pb2.EvalConfig. - eval_input_config: An input_reader_pb2.InputReader. - model_config: A model_pb2.DetectionModel. - model: A pre-constructed Detection Model. - If None, one will be created from the config. - params: Parameter dictionary passed from the estimator. - input_context: optional, A tf.distribute.InputContext object used to - shard filenames and compute per-replica batch_size when this function - is being called per-replica. - - Returns: - A tf.data.Dataset that holds (features, labels) tuple. - - features: Dictionary of feature tensors. - features[fields.InputDataFields.image] is a [1, H, W, C] float32 tensor - with preprocessed images. - features[HASH_KEY] is a [1] int32 tensor representing unique - identifiers for the images. - features[fields.InputDataFields.true_image_shape] is a [1, 3] - int32 tensor representing the true image shapes, as preprocessed - images could be padded. - features[fields.InputDataFields.original_image] is a [1, H', W', C] - float32 tensor with the original image. - labels: Dictionary of groundtruth tensors. - labels[fields.InputDataFields.groundtruth_boxes] is a [1, num_boxes, 4] - float32 tensor containing the corners of the groundtruth boxes. - labels[fields.InputDataFields.groundtruth_classes] is a - [num_boxes, num_classes] float32 one-hot tensor of classes. - labels[fields.InputDataFields.groundtruth_area] is a [1, num_boxes] - float32 tensor containing object areas. - labels[fields.InputDataFields.groundtruth_is_crowd] is a [1, num_boxes] - bool tensor indicating if the boxes enclose a crowd. - labels[fields.InputDataFields.groundtruth_difficult] is a [1, num_boxes] - int32 tensor indicating if the boxes represent difficult instances. - -- Optional -- - labels[fields.InputDataFields.groundtruth_instance_masks] is a - [1, num_boxes, H, W] float32 tensor containing only binary values, - which represent instance masks for objects. - labels[fields.InputDataFields.groundtruth_instance_mask_weights] is a - [1, num_boxes] float32 tensor containing groundtruth weights for each - instance mask. - labels[fields.InputDataFields.groundtruth_weights] is a - [batch_size, num_boxes, num_keypoints] float32 tensor containing - groundtruth weights for the keypoints. - labels[fields.InputDataFields.groundtruth_visibilities] is a - [batch_size, num_boxes, num_keypoints] bool tensor containing - groundtruth visibilities for each keypoint. - labels[fields.InputDataFields.groundtruth_group_of] is a [1, num_boxes] - bool tensor indicating if the box covers more than 5 instances of the - same class which heavily occlude each other. - labels[fields.InputDataFields.groundtruth_labeled_classes] is a - [num_boxes, num_classes] float32 k-hot tensor of classes. - labels[fields.InputDataFields.groundtruth_dp_num_points] is a - [batch_size, num_boxes] int32 tensor with the number of sampled - DensePose points per object. - labels[fields.InputDataFields.groundtruth_dp_part_ids] is a - [batch_size, num_boxes, max_sampled_points] int32 tensor with the - DensePose part ids (0-indexed) per object. - labels[fields.InputDataFields.groundtruth_dp_surface_coords] is a - [batch_size, num_boxes, max_sampled_points, 4] float32 tensor with the - DensePose surface coordinates. The format is (y, x, v, u), where (y, x) - are normalized image coordinates and (v, u) are normalized surface part - coordinates. - labels[fields.InputDataFields.groundtruth_track_ids] is a - [batch_size, num_boxes] int32 tensor with the track ID for each object. - - Raises: - TypeError: if the `eval_config`, `eval_input_config` or `model_config` - are not of the correct type. - """ - params = params or {} - if not isinstance(eval_config, eval_pb2.EvalConfig): - raise TypeError('For eval mode, the `eval_config` must be a ' - 'train_pb2.EvalConfig.') - if not isinstance(eval_input_config, input_reader_pb2.InputReader): - raise TypeError('The `eval_input_config` must be a ' - 'input_reader_pb2.InputReader.') - if not isinstance(model_config, model_pb2.DetectionModel): - raise TypeError('The `model_config` must be a ' - 'model_pb2.DetectionModel.') - - if eval_config.force_no_resize: - arch = model_config.WhichOneof('model') - arch_config = getattr(model_config, arch) - image_resizer_proto = image_resizer_pb2.ImageResizer() - image_resizer_proto.identity_resizer.CopyFrom( - image_resizer_pb2.IdentityResizer()) - arch_config.image_resizer.CopyFrom(image_resizer_proto) - - if model is None: - model_preprocess_fn = INPUT_BUILDER_UTIL_MAP['model_build']( - model_config, is_training=False).preprocess - else: - model_preprocess_fn = model.preprocess - - def transform_and_pad_input_data_fn(tensor_dict): - """Combines transform and pad operation.""" - num_classes = config_util.get_number_of_classes(model_config) - - image_resizer_config = config_util.get_image_resizer_config(model_config) - image_resizer_fn = image_resizer_builder.build(image_resizer_config) - keypoint_type_weight = eval_input_config.keypoint_type_weight or None - - transform_data_fn = functools.partial( - transform_input_data, model_preprocess_fn=model_preprocess_fn, - image_resizer_fn=image_resizer_fn, - num_classes=num_classes, - data_augmentation_fn=None, - retain_original_image=eval_config.retain_original_images, - retain_original_image_additional_channels= - eval_config.retain_original_image_additional_channels, - keypoint_type_weight=keypoint_type_weight, - image_classes_field_map_empty_to_ones=eval_config - .image_classes_field_map_empty_to_ones) - tensor_dict = pad_input_data_to_static_shapes( - tensor_dict=transform_data_fn(tensor_dict), - max_num_boxes=eval_input_config.max_number_of_boxes, - num_classes=config_util.get_number_of_classes(model_config), - spatial_image_shape=config_util.get_spatial_image_size( - image_resizer_config), - max_num_context_features=config_util.get_max_num_context_features( - model_config), - context_feature_length=config_util.get_context_feature_length( - model_config)) - include_source_id = eval_input_config.include_source_id - return (_get_features_dict(tensor_dict, include_source_id), - _get_labels_dict(tensor_dict)) - - reduce_to_frame_fn = get_reduce_to_frame_fn(eval_input_config, False) - - dataset = INPUT_BUILDER_UTIL_MAP['dataset_build']( - eval_input_config, - batch_size=params['batch_size'] if params else eval_config.batch_size, - transform_input_data_fn=transform_and_pad_input_data_fn, - input_context=input_context, - reduce_to_frame_fn=reduce_to_frame_fn) - return dataset - - -def create_predict_input_fn(model_config, predict_input_config): - """Creates a predict `input` function for `Estimator`. - - Args: - model_config: A model_pb2.DetectionModel. - predict_input_config: An input_reader_pb2.InputReader. - - Returns: - `input_fn` for `Estimator` in PREDICT mode. - """ - - def _predict_input_fn(params=None): - """Decodes serialized tf.Examples and returns `ServingInputReceiver`. - - Args: - params: Parameter dictionary passed from the estimator. - - Returns: - `ServingInputReceiver`. - """ - del params - example = tf.placeholder(dtype=tf.string, shape=[], name='tf_example') - - num_classes = config_util.get_number_of_classes(model_config) - model_preprocess_fn = INPUT_BUILDER_UTIL_MAP['model_build']( - model_config, is_training=False).preprocess - - image_resizer_config = config_util.get_image_resizer_config(model_config) - image_resizer_fn = image_resizer_builder.build(image_resizer_config) - - transform_fn = functools.partial( - transform_input_data, model_preprocess_fn=model_preprocess_fn, - image_resizer_fn=image_resizer_fn, - num_classes=num_classes, - data_augmentation_fn=None) - - decoder = tf_example_decoder.TfExampleDecoder( - load_instance_masks=False, - num_additional_channels=predict_input_config.num_additional_channels) - input_dict = transform_fn(decoder.decode(example)) - images = tf.cast(input_dict[fields.InputDataFields.image], dtype=tf.float32) - images = tf.expand_dims(images, axis=0) - true_image_shape = tf.expand_dims( - input_dict[fields.InputDataFields.true_image_shape], axis=0) - - return tf_estimator.export.ServingInputReceiver( - features={ - fields.InputDataFields.image: images, - fields.InputDataFields.true_image_shape: true_image_shape}, - receiver_tensors={SERVING_FED_EXAMPLE_KEY: example}) - - return _predict_input_fn - - -def get_reduce_to_frame_fn(input_reader_config, is_training): - """Returns a function reducing sequence tensors to single frame tensors. - - If the input type is not TF_SEQUENCE_EXAMPLE, the tensors are passed through - this function unchanged. Otherwise, when in training mode, a single frame is - selected at random from the sequence example, and the tensors for that frame - are converted to single frame tensors, with all associated context features. - In evaluation mode all frames are converted to single frame tensors with - copied context tensors. After the sequence example tensors are converted into - one or many single frame tensors, the images from each frame are decoded. - - Args: - input_reader_config: An input_reader_pb2.InputReader. - is_training: Whether we are in training mode. - - Returns: - `reduce_to_frame_fn` for the dataset builder - """ - if input_reader_config.input_type != ( - input_reader_pb2.InputType.Value('TF_SEQUENCE_EXAMPLE')): - return lambda dataset, dataset_map_fn, batch_size, config: dataset - else: - def reduce_to_frame(dataset, dataset_map_fn, batch_size, - input_reader_config): - """Returns a function reducing sequence tensors to single frame tensors. - - Args: - dataset: A tf dataset containing sequence tensors. - dataset_map_fn: A function that handles whether to - map_with_legacy_function for this dataset - batch_size: used if map_with_legacy_function is true to determine - num_parallel_calls - input_reader_config: used if map_with_legacy_function is true to - determine num_parallel_calls - - Returns: - A tf dataset containing single frame tensors. - """ - if is_training: - def get_single_frame(tensor_dict): - """Returns a random frame from a sequence. - - Picks a random frame and returns slices of sequence tensors - corresponding to the random frame. Returns non-sequence tensors - unchanged. - - Args: - tensor_dict: A dictionary containing sequence tensors. - - Returns: - Tensors for a single random frame within the sequence. - """ - num_frames = tf.cast( - tf.shape(tensor_dict[fields.InputDataFields.source_id])[0], - dtype=tf.int32) - if input_reader_config.frame_index == -1: - frame_index = tf.random.uniform((), minval=0, maxval=num_frames, - dtype=tf.int32) - else: - frame_index = tf.constant(input_reader_config.frame_index, - dtype=tf.int32) - out_tensor_dict = {} - for key in tensor_dict: - if key in fields.SEQUENCE_FIELDS: - # Slice random frame from sequence tensors - out_tensor_dict[key] = tensor_dict[key][frame_index] - else: - # Copy all context tensors. - out_tensor_dict[key] = tensor_dict[key] - return out_tensor_dict - dataset = dataset_map_fn(dataset, get_single_frame, batch_size, - input_reader_config) - else: - dataset = dataset_map_fn(dataset, util_ops.tile_context_tensors, - batch_size, input_reader_config) - dataset = dataset.unbatch() - # Decode frame here as SequenceExample tensors contain encoded images. - dataset = dataset_map_fn(dataset, util_ops.decode_image, batch_size, - input_reader_config) - return dataset - return reduce_to_frame diff --git a/research/object_detection/inputs_test.py b/research/object_detection/inputs_test.py deleted file mode 100644 index ea69717a478..00000000000 --- a/research/object_detection/inputs_test.py +++ /dev/null @@ -1,1754 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.tflearn.inputs.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -import os -import unittest -from absl import logging -from absl.testing import parameterized -import numpy as np -import six -import tensorflow.compat.v1 as tf - -from object_detection import inputs -from object_detection.core import preprocessor -from object_detection.core import standard_fields as fields -from object_detection.utils import config_util -from object_detection.utils import test_case -from object_detection.utils import test_utils -from object_detection.utils import tf_version - -if six.PY2: - import mock # pylint: disable=g-import-not-at-top -else: - from unittest import mock # pylint: disable=g-import-not-at-top, g-importing-member - -FLAGS = tf.flags.FLAGS - - -def _get_configs_for_model(model_name): - """Returns configurations for model.""" - fname = os.path.join(tf.resource_loader.get_data_files_path(), - 'samples/configs/' + model_name + '.config') - label_map_path = os.path.join(tf.resource_loader.get_data_files_path(), - 'data/pet_label_map.pbtxt') - data_path = os.path.join(tf.resource_loader.get_data_files_path(), - 'test_data/pets_examples.record') - configs = config_util.get_configs_from_pipeline_file(fname) - override_dict = { - 'train_input_path': data_path, - 'eval_input_path': data_path, - 'label_map_path': label_map_path - } - return config_util.merge_external_params_with_configs( - configs, kwargs_dict=override_dict) - - -def _get_configs_for_model_sequence_example(model_name, frame_index=-1): - """Returns configurations for model.""" - fname = os.path.join(tf.resource_loader.get_data_files_path(), - 'test_data/' + model_name + '.config') - label_map_path = os.path.join(tf.resource_loader.get_data_files_path(), - 'data/snapshot_serengeti_label_map.pbtxt') - data_path = os.path.join( - tf.resource_loader.get_data_files_path(), - 'test_data/snapshot_serengeti_sequence_examples.record') - configs = config_util.get_configs_from_pipeline_file(fname) - override_dict = { - 'train_input_path': data_path, - 'eval_input_path': data_path, - 'label_map_path': label_map_path, - 'frame_index': frame_index - } - return config_util.merge_external_params_with_configs( - configs, kwargs_dict=override_dict) - - -def _make_initializable_iterator(dataset): - """Creates an iterator, and initializes tables. - - Args: - dataset: A `tf.data.Dataset` object. - - Returns: - A `tf.data.Iterator`. - """ - iterator = tf.data.make_initializable_iterator(dataset) - tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer) - return iterator - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only tests under TF2.X.') -class InputFnTest(test_case.TestCase, parameterized.TestCase): - - def test_faster_rcnn_resnet50_train_input(self): - """Tests the training input function for FasterRcnnResnet50.""" - configs = _get_configs_for_model('faster_rcnn_resnet50_pets') - model_config = configs['model'] - model_config.faster_rcnn.num_classes = 37 - train_input_fn = inputs.create_train_input_fn( - configs['train_config'], configs['train_input_config'], model_config) - features, labels = _make_initializable_iterator(train_input_fn()).get_next() - - self.assertAllEqual([1, None, None, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual([1], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [1, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [1, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [1, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - self.assertAllEqual( - [1, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_confidences].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_confidences].dtype) - - def test_faster_rcnn_resnet50_train_input_with_additional_channels(self): - """Tests the training input function for FasterRcnnResnet50.""" - configs = _get_configs_for_model('faster_rcnn_resnet50_pets') - model_config = configs['model'] - configs['train_input_config'].num_additional_channels = 2 - configs['train_config'].retain_original_images = True - model_config.faster_rcnn.num_classes = 37 - train_input_fn = inputs.create_train_input_fn( - configs['train_config'], configs['train_input_config'], model_config) - features, labels = _make_initializable_iterator(train_input_fn()).get_next() - - self.assertAllEqual([1, None, None, 5], - features[fields.InputDataFields.image].shape.as_list()) - self.assertAllEqual( - [1, None, None, 3], - features[fields.InputDataFields.original_image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual([1], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [1, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [1, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [1, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - self.assertAllEqual( - [1, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_confidences].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_confidences].dtype) - - @parameterized.parameters( - {'eval_batch_size': 1}, - {'eval_batch_size': 8} - ) - def test_faster_rcnn_resnet50_eval_input(self, eval_batch_size=1): - """Tests the eval input function for FasterRcnnResnet50.""" - configs = _get_configs_for_model('faster_rcnn_resnet50_pets') - model_config = configs['model'] - model_config.faster_rcnn.num_classes = 37 - eval_config = configs['eval_config'] - eval_config.batch_size = eval_batch_size - eval_input_fn = inputs.create_eval_input_fn( - eval_config, configs['eval_input_configs'][0], model_config) - features, labels = _make_initializable_iterator(eval_input_fn()).get_next() - self.assertAllEqual([eval_batch_size, None, None, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual( - [eval_batch_size, None, None, 3], - features[fields.InputDataFields.original_image].shape.as_list()) - self.assertEqual(tf.uint8, - features[fields.InputDataFields.original_image].dtype) - self.assertAllEqual([eval_batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [eval_batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [eval_batch_size, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_area].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_area].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_is_crowd].shape.as_list()) - self.assertEqual( - tf.bool, labels[fields.InputDataFields.groundtruth_is_crowd].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_difficult].shape.as_list()) - self.assertEqual( - tf.int32, labels[fields.InputDataFields.groundtruth_difficult].dtype) - - def test_context_rcnn_resnet50_train_input_with_sequence_example( - self, train_batch_size=8): - """Tests the training input function for FasterRcnnResnet50.""" - configs = _get_configs_for_model_sequence_example( - 'context_rcnn_camera_trap') - model_config = configs['model'] - train_config = configs['train_config'] - train_config.batch_size = train_batch_size - train_input_fn = inputs.create_train_input_fn( - train_config, configs['train_input_config'], model_config) - features, labels = _make_initializable_iterator(train_input_fn()).get_next() - - self.assertAllEqual([train_batch_size, 640, 640, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual([train_batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [train_batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [train_batch_size, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [train_batch_size, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - self.assertAllEqual( - [train_batch_size, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_confidences].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_confidences].dtype) - - def test_context_rcnn_resnet50_eval_input_with_sequence_example( - self, eval_batch_size=8): - """Tests the eval input function for FasterRcnnResnet50.""" - configs = _get_configs_for_model_sequence_example( - 'context_rcnn_camera_trap') - model_config = configs['model'] - eval_config = configs['eval_config'] - eval_config.batch_size = eval_batch_size - eval_input_fn = inputs.create_eval_input_fn( - eval_config, configs['eval_input_configs'][0], model_config) - features, labels = _make_initializable_iterator(eval_input_fn()).get_next() - self.assertAllEqual([eval_batch_size, 640, 640, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual( - [eval_batch_size, 640, 640, 3], - features[fields.InputDataFields.original_image].shape.as_list()) - self.assertEqual(tf.uint8, - features[fields.InputDataFields.original_image].dtype) - self.assertAllEqual([eval_batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [eval_batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [eval_batch_size, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - - def test_context_rcnn_resnet50_eval_input_with_sequence_example_image_id_list( - self, eval_batch_size=8): - """Tests the eval input function for FasterRcnnResnet50.""" - configs = _get_configs_for_model_sequence_example( - 'context_rcnn_camera_trap') - model_config = configs['model'] - eval_config = configs['eval_config'] - eval_config.batch_size = eval_batch_size - eval_input_config = configs['eval_input_configs'][0] - eval_input_config.load_context_image_ids = True - eval_input_fn = inputs.create_eval_input_fn( - eval_config, eval_input_config, model_config) - features, labels = _make_initializable_iterator(eval_input_fn()).get_next() - self.assertAllEqual([eval_batch_size, 640, 640, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual( - [eval_batch_size, 640, 640, 3], - features[fields.InputDataFields.original_image].shape.as_list()) - self.assertEqual(tf.uint8, - features[fields.InputDataFields.original_image].dtype) - self.assertAllEqual([eval_batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [eval_batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [eval_batch_size, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - - def test_context_rcnn_resnet50_train_input_with_sequence_example_frame_index( - self, train_batch_size=8): - """Tests the training input function for FasterRcnnResnet50.""" - configs = _get_configs_for_model_sequence_example( - 'context_rcnn_camera_trap', frame_index=2) - model_config = configs['model'] - train_config = configs['train_config'] - train_config.batch_size = train_batch_size - train_input_fn = inputs.create_train_input_fn( - train_config, configs['train_input_config'], model_config) - features, labels = _make_initializable_iterator(train_input_fn()).get_next() - - self.assertAllEqual([train_batch_size, 640, 640, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual([train_batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [train_batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [train_batch_size, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [train_batch_size, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - self.assertAllEqual( - [train_batch_size, 100, model_config.faster_rcnn.num_classes], - labels[fields.InputDataFields.groundtruth_confidences].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_confidences].dtype) - - def test_ssd_inceptionV2_train_input(self): - """Tests the training input function for SSDInceptionV2.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - model_config = configs['model'] - model_config.ssd.num_classes = 37 - batch_size = configs['train_config'].batch_size - train_input_fn = inputs.create_train_input_fn( - configs['train_config'], configs['train_input_config'], model_config) - features, labels = _make_initializable_iterator(train_input_fn()).get_next() - - self.assertAllEqual([batch_size, 300, 300, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual([batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [batch_size], - labels[fields.InputDataFields.num_groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.int32, - labels[fields.InputDataFields.num_groundtruth_boxes].dtype) - self.assertAllEqual( - [batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [batch_size, 100, model_config.ssd.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [batch_size, 100], - labels[ - fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - - @parameterized.parameters( - {'eval_batch_size': 1}, - {'eval_batch_size': 8} - ) - def test_ssd_inceptionV2_eval_input(self, eval_batch_size=1): - """Tests the eval input function for SSDInceptionV2.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - model_config = configs['model'] - model_config.ssd.num_classes = 37 - eval_config = configs['eval_config'] - eval_config.batch_size = eval_batch_size - eval_input_fn = inputs.create_eval_input_fn( - eval_config, configs['eval_input_configs'][0], model_config) - features, labels = _make_initializable_iterator(eval_input_fn()).get_next() - self.assertAllEqual([eval_batch_size, 300, 300, 3], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual( - [eval_batch_size, 300, 300, 3], - features[fields.InputDataFields.original_image].shape.as_list()) - self.assertEqual(tf.uint8, - features[fields.InputDataFields.original_image].dtype) - self.assertAllEqual([eval_batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [eval_batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [eval_batch_size, 100, model_config.ssd.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[ - fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual( - tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_area].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_area].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_is_crowd].shape.as_list()) - self.assertEqual( - tf.bool, labels[fields.InputDataFields.groundtruth_is_crowd].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_difficult].shape.as_list()) - self.assertEqual( - tf.int32, labels[fields.InputDataFields.groundtruth_difficult].dtype) - - def test_ssd_inceptionV2_eval_input_with_additional_channels( - self, eval_batch_size=1): - """Tests the eval input function for SSDInceptionV2 with additional channel. - - Args: - eval_batch_size: Batch size for eval set. - """ - configs = _get_configs_for_model('ssd_inception_v2_pets') - model_config = configs['model'] - model_config.ssd.num_classes = 37 - configs['eval_input_configs'][0].num_additional_channels = 1 - eval_config = configs['eval_config'] - eval_config.batch_size = eval_batch_size - eval_config.retain_original_image_additional_channels = True - eval_input_fn = inputs.create_eval_input_fn( - eval_config, configs['eval_input_configs'][0], model_config) - features, labels = _make_initializable_iterator(eval_input_fn()).get_next() - self.assertAllEqual([eval_batch_size, 300, 300, 4], - features[fields.InputDataFields.image].shape.as_list()) - self.assertEqual(tf.float32, features[fields.InputDataFields.image].dtype) - self.assertAllEqual( - [eval_batch_size, 300, 300, 3], - features[fields.InputDataFields.original_image].shape.as_list()) - self.assertEqual(tf.uint8, - features[fields.InputDataFields.original_image].dtype) - self.assertAllEqual([eval_batch_size, 300, 300, 1], features[ - fields.InputDataFields.image_additional_channels].shape.as_list()) - self.assertEqual( - tf.uint8, - features[fields.InputDataFields.image_additional_channels].dtype) - self.assertAllEqual([eval_batch_size], - features[inputs.HASH_KEY].shape.as_list()) - self.assertEqual(tf.int32, features[inputs.HASH_KEY].dtype) - self.assertAllEqual( - [eval_batch_size, 100, 4], - labels[fields.InputDataFields.groundtruth_boxes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_boxes].dtype) - self.assertAllEqual( - [eval_batch_size, 100, model_config.ssd.num_classes], - labels[fields.InputDataFields.groundtruth_classes].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_classes].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_weights].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_weights].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_area].shape.as_list()) - self.assertEqual(tf.float32, - labels[fields.InputDataFields.groundtruth_area].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_is_crowd].shape.as_list()) - self.assertEqual(tf.bool, - labels[fields.InputDataFields.groundtruth_is_crowd].dtype) - self.assertAllEqual( - [eval_batch_size, 100], - labels[fields.InputDataFields.groundtruth_difficult].shape.as_list()) - self.assertEqual(tf.int32, - labels[fields.InputDataFields.groundtruth_difficult].dtype) - - def test_predict_input(self): - """Tests the predict input function.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - predict_input_fn = inputs.create_predict_input_fn( - model_config=configs['model'], - predict_input_config=configs['eval_input_configs'][0]) - serving_input_receiver = predict_input_fn() - - image = serving_input_receiver.features[fields.InputDataFields.image] - receiver_tensors = serving_input_receiver.receiver_tensors[ - inputs.SERVING_FED_EXAMPLE_KEY] - self.assertEqual([1, 300, 300, 3], image.shape.as_list()) - self.assertEqual(tf.float32, image.dtype) - self.assertEqual(tf.string, receiver_tensors.dtype) - - def test_predict_input_with_additional_channels(self): - """Tests the predict input function with additional channels.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['eval_input_configs'][0].num_additional_channels = 2 - predict_input_fn = inputs.create_predict_input_fn( - model_config=configs['model'], - predict_input_config=configs['eval_input_configs'][0]) - serving_input_receiver = predict_input_fn() - - image = serving_input_receiver.features[fields.InputDataFields.image] - receiver_tensors = serving_input_receiver.receiver_tensors[ - inputs.SERVING_FED_EXAMPLE_KEY] - # RGB + 2 additional channels = 5 channels. - self.assertEqual([1, 300, 300, 5], image.shape.as_list()) - self.assertEqual(tf.float32, image.dtype) - self.assertEqual(tf.string, receiver_tensors.dtype) - - def test_error_with_bad_train_config(self): - """Tests that a TypeError is raised with improper train config.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['model'].ssd.num_classes = 37 - train_input_fn = inputs.create_train_input_fn( - train_config=configs['eval_config'], # Expecting `TrainConfig`. - train_input_config=configs['train_input_config'], - model_config=configs['model']) - with self.assertRaises(TypeError): - train_input_fn() - - def test_error_with_bad_train_input_config(self): - """Tests that a TypeError is raised with improper train input config.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['model'].ssd.num_classes = 37 - train_input_fn = inputs.create_train_input_fn( - train_config=configs['train_config'], - train_input_config=configs['model'], # Expecting `InputReader`. - model_config=configs['model']) - with self.assertRaises(TypeError): - train_input_fn() - - def test_error_with_bad_train_model_config(self): - """Tests that a TypeError is raised with improper train model config.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['model'].ssd.num_classes = 37 - train_input_fn = inputs.create_train_input_fn( - train_config=configs['train_config'], - train_input_config=configs['train_input_config'], - model_config=configs['train_config']) # Expecting `DetectionModel`. - with self.assertRaises(TypeError): - train_input_fn() - - def test_error_with_bad_eval_config(self): - """Tests that a TypeError is raised with improper eval config.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['model'].ssd.num_classes = 37 - eval_input_fn = inputs.create_eval_input_fn( - eval_config=configs['train_config'], # Expecting `EvalConfig`. - eval_input_config=configs['eval_input_configs'][0], - model_config=configs['model']) - with self.assertRaises(TypeError): - eval_input_fn() - - def test_error_with_bad_eval_input_config(self): - """Tests that a TypeError is raised with improper eval input config.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['model'].ssd.num_classes = 37 - eval_input_fn = inputs.create_eval_input_fn( - eval_config=configs['eval_config'], - eval_input_config=configs['model'], # Expecting `InputReader`. - model_config=configs['model']) - with self.assertRaises(TypeError): - eval_input_fn() - - def test_error_with_bad_eval_model_config(self): - """Tests that a TypeError is raised with improper eval model config.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['model'].ssd.num_classes = 37 - eval_input_fn = inputs.create_eval_input_fn( - eval_config=configs['eval_config'], - eval_input_config=configs['eval_input_configs'][0], - model_config=configs['eval_config']) # Expecting `DetectionModel`. - with self.assertRaises(TypeError): - eval_input_fn() - - def test_output_equal_in_replace_empty_string_with_random_number(self): - string_placeholder = tf.placeholder(tf.string, shape=[]) - replaced_string = inputs._replace_empty_string_with_random_number( - string_placeholder) - - test_string = b'hello world' - feed_dict = {string_placeholder: test_string} - - with self.test_session() as sess: - out_string = sess.run(replaced_string, feed_dict=feed_dict) - - self.assertEqual(test_string, out_string) - - def test_output_is_integer_in_replace_empty_string_with_random_number(self): - - string_placeholder = tf.placeholder(tf.string, shape=[]) - replaced_string = inputs._replace_empty_string_with_random_number( - string_placeholder) - - empty_string = '' - feed_dict = {string_placeholder: empty_string} - with self.test_session() as sess: - out_string = sess.run(replaced_string, feed_dict=feed_dict) - - is_integer = True - try: - # Test whether out_string is a string which represents an integer, the - # casting below will throw an error if out_string is not castable to int. - int(out_string) - except ValueError: - is_integer = False - - self.assertTrue(is_integer) - - def test_force_no_resize(self): - """Tests the functionality of force_no_reisze option.""" - configs = _get_configs_for_model('ssd_inception_v2_pets') - configs['eval_config'].force_no_resize = True - - eval_input_fn = inputs.create_eval_input_fn( - eval_config=configs['eval_config'], - eval_input_config=configs['eval_input_configs'][0], - model_config=configs['model'] - ) - train_input_fn = inputs.create_train_input_fn( - train_config=configs['train_config'], - train_input_config=configs['train_input_config'], - model_config=configs['model'] - ) - - features_train, _ = _make_initializable_iterator( - train_input_fn()).get_next() - - features_eval, _ = _make_initializable_iterator( - eval_input_fn()).get_next() - - images_train, images_eval = features_train['image'], features_eval['image'] - - self.assertEqual([1, None, None, 3], images_eval.shape.as_list()) - self.assertEqual([24, 300, 300, 3], images_train.shape.as_list()) - - -class DataAugmentationFnTest(test_case.TestCase): - - def test_apply_image_and_box_augmentation(self): - data_augmentation_options = [ - (preprocessor.resize_image, { - 'new_height': 20, - 'new_width': 20, - 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR - }), - (preprocessor.scale_boxes_to_pixel_coordinates, {}), - ] - data_augmentation_fn = functools.partial( - inputs.augment_input_data, - data_augmentation_options=data_augmentation_options) - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1., 1.]], np.float32)) - } - augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) - return (augmented_tensor_dict[fields.InputDataFields.image], - augmented_tensor_dict[fields.InputDataFields. - groundtruth_boxes]) - image, groundtruth_boxes = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image.shape, [20, 20, 3]) - self.assertAllClose(groundtruth_boxes, [[10, 10, 20, 20]]) - - def test_apply_image_and_box_augmentation_with_scores(self): - data_augmentation_options = [ - (preprocessor.resize_image, { - 'new_height': 20, - 'new_width': 20, - 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR - }), - (preprocessor.scale_boxes_to_pixel_coordinates, {}), - ] - data_augmentation_fn = functools.partial( - inputs.augment_input_data, - data_augmentation_options=data_augmentation_options) - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1., 1.]], np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1.0], np.float32)), - fields.InputDataFields.groundtruth_weights: - tf.constant(np.array([0.8], np.float32)), - } - augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) - return (augmented_tensor_dict[fields.InputDataFields.image], - augmented_tensor_dict[fields.InputDataFields.groundtruth_boxes], - augmented_tensor_dict[fields.InputDataFields.groundtruth_classes], - augmented_tensor_dict[fields.InputDataFields.groundtruth_weights]) - (image, groundtruth_boxes, - groundtruth_classes, groundtruth_weights) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image.shape, [20, 20, 3]) - self.assertAllClose(groundtruth_boxes, [[10, 10, 20, 20]]) - self.assertAllClose(groundtruth_classes.shape, [1.0]) - self.assertAllClose(groundtruth_weights, [0.8]) - - def test_include_masks_in_data_augmentation(self): - data_augmentation_options = [ - (preprocessor.resize_image, { - 'new_height': 20, - 'new_width': 20, - 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR - }) - ] - data_augmentation_fn = functools.partial( - inputs.augment_input_data, - data_augmentation_options=data_augmentation_options) - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_instance_masks: - tf.constant(np.zeros([2, 10, 10], np.uint8)), - fields.InputDataFields.groundtruth_instance_mask_weights: - tf.constant([1.0, 0.0], np.float32) - } - augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) - return (augmented_tensor_dict[fields.InputDataFields.image], - augmented_tensor_dict[fields.InputDataFields. - groundtruth_instance_masks], - augmented_tensor_dict[fields.InputDataFields. - groundtruth_instance_mask_weights]) - image, masks, mask_weights = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image.shape, [20, 20, 3]) - self.assertAllEqual(masks.shape, [2, 20, 20]) - self.assertAllClose(mask_weights, [1.0, 0.0]) - - def test_include_keypoints_in_data_augmentation(self): - data_augmentation_options = [ - (preprocessor.resize_image, { - 'new_height': 20, - 'new_width': 20, - 'method': tf.image.ResizeMethod.NEAREST_NEIGHBOR - }), - (preprocessor.scale_boxes_to_pixel_coordinates, {}), - ] - data_augmentation_fn = functools.partial( - inputs.augment_input_data, - data_augmentation_options=data_augmentation_options) - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(10, 10, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1., 1.]], np.float32)), - fields.InputDataFields.groundtruth_keypoints: - tf.constant(np.array([[[0.5, 1.0], [0.5, 0.5]]], np.float32)) - } - augmented_tensor_dict = data_augmentation_fn(tensor_dict=tensor_dict) - return (augmented_tensor_dict[fields.InputDataFields.image], - augmented_tensor_dict[fields.InputDataFields.groundtruth_boxes], - augmented_tensor_dict[fields.InputDataFields. - groundtruth_keypoints]) - image, boxes, keypoints = self.execute_cpu(graph_fn, []) - self.assertAllEqual(image.shape, [20, 20, 3]) - self.assertAllClose(boxes, [[10, 10, 20, 20]]) - self.assertAllClose(keypoints, [[[10, 20], [10, 10]]]) - - -def _fake_model_preprocessor_fn(image): - return (image, tf.expand_dims(tf.shape(image)[1:], axis=0)) - - -def _fake_image_resizer_fn(image, mask): - return (image, mask, tf.shape(image)) - - -def _fake_resize50_preprocess_fn(image): - image = image[0] - image, shape = preprocessor.resize_to_range( - image, min_dimension=50, max_dimension=50, pad_to_max_dimension=True) - - return tf.expand_dims(image, 0), tf.expand_dims(shape, axis=0) - - -class DataTransformationFnTest(test_case.TestCase, parameterized.TestCase): - - def test_combine_additional_channels_if_present(self): - image = np.random.rand(4, 4, 3).astype(np.float32) - additional_channels = np.random.rand(4, 4, 2).astype(np.float32) - def graph_fn(image, additional_channels): - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.image_additional_channels: additional_channels, - fields.InputDataFields.groundtruth_classes: - tf.constant([1, 1], tf.int32) - } - - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=1) - out_tensors = input_transformation_fn(tensor_dict=tensor_dict) - return out_tensors[fields.InputDataFields.image] - out_image = self.execute_cpu(graph_fn, [image, additional_channels]) - self.assertAllEqual(out_image.dtype, tf.float32) - self.assertAllEqual(out_image.shape, [4, 4, 5]) - self.assertAllClose(out_image, np.concatenate((image, additional_channels), - axis=2)) - - def test_use_multiclass_scores_when_present(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: tf.constant(np.random.rand(4, 4, 3). - astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.5, .5, 1, 1]], - np.float32)), - fields.InputDataFields.multiclass_scores: - tf.constant(np.array([0.2, 0.3, 0.5, 0.1, 0.6, 0.3], np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1, 2], np.int32)) - } - - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=3, use_multiclass_scores=True) - transformed_inputs = input_transformation_fn(tensor_dict=tensor_dict) - return transformed_inputs[fields.InputDataFields.groundtruth_classes] - groundtruth_classes = self.execute_cpu(graph_fn, []) - self.assertAllClose( - np.array([[0.2, 0.3, 0.5], [0.1, 0.6, 0.3]], np.float32), - groundtruth_classes) - - @unittest.skipIf(tf_version.is_tf2(), ('Skipping due to different behaviour ' - 'in TF 2.X')) - def test_use_multiclass_scores_when_not_present(self): - def graph_fn(): - zero_num_elements = tf.random.uniform([], minval=0, maxval=1, - dtype=tf.int32) - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.5, .5, 1, 1]], - np.float32)), - fields.InputDataFields.multiclass_scores: tf.zeros(zero_num_elements), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1, 2], np.int32)) - } - - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=3, use_multiclass_scores=True) - - transformed_inputs = input_transformation_fn(tensor_dict=tensor_dict) - return transformed_inputs[fields.InputDataFields.groundtruth_classes] - groundtruth_classes = self.execute_cpu(graph_fn, []) - self.assertAllClose( - np.array([[0, 1, 0], [0, 0, 1]], np.float32), - groundtruth_classes) - - @parameterized.parameters( - {'labeled_classes': [1, 2]}, - {'labeled_classes': []}, - {'labeled_classes': [1, -1, 2]} # -1 denotes an unrecognized class - ) - def test_use_labeled_classes(self, labeled_classes): - - def compute_fn(image, groundtruth_boxes, groundtruth_classes, - groundtruth_labeled_classes): - tensor_dict = { - fields.InputDataFields.image: - image, - fields.InputDataFields.groundtruth_boxes: - groundtruth_boxes, - fields.InputDataFields.groundtruth_classes: - groundtruth_classes, - fields.InputDataFields.groundtruth_labeled_classes: - groundtruth_labeled_classes - } - - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=3) - return input_transformation_fn(tensor_dict=tensor_dict) - - image = np.random.rand(4, 4, 3).astype(np.float32) - groundtruth_boxes = np.array([[.5, .5, 1, 1], [.5, .5, 1, 1]], np.float32) - groundtruth_classes = np.array([1, 2], np.int32) - groundtruth_labeled_classes = np.array(labeled_classes, np.int32) - - transformed_inputs = self.execute_cpu(compute_fn, [ - image, groundtruth_boxes, groundtruth_classes, - groundtruth_labeled_classes - ]) - - if labeled_classes == [1, 2] or labeled_classes == [1, -1, 2]: - transformed_labeled_classes = [1, 1, 0] - elif not labeled_classes: - transformed_labeled_classes = [1, 1, 1] - else: - logging.exception('Unexpected labeled_classes %r', labeled_classes) - - self.assertAllEqual( - np.array(transformed_labeled_classes, np.float32), - transformed_inputs[fields.InputDataFields.groundtruth_labeled_classes]) - - def test_returns_correct_class_label_encodings(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[0, 0, 1, 1], [.5, .5, 1, 1]], np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, 1], np.int32)) - } - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes) - transformed_inputs = input_transformation_fn(tensor_dict=tensor_dict) - return (transformed_inputs[fields.InputDataFields.groundtruth_classes], - transformed_inputs[fields.InputDataFields. - groundtruth_confidences]) - (groundtruth_classes, groundtruth_confidences) = self.execute_cpu(graph_fn, - []) - self.assertAllClose(groundtruth_classes, [[0, 0, 1], [1, 0, 0]]) - self.assertAllClose(groundtruth_confidences, [[0, 0, 1], [1, 0, 0]]) - - def test_returns_correct_labels_with_unrecognized_class(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant( - np.array([[0, 0, 1, 1], [.2, .2, 4, 4], [.5, .5, 1, 1]], - np.float32)), - fields.InputDataFields.groundtruth_area: - tf.constant(np.array([.5, .4, .3])), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, -1, 1], np.int32)), - fields.InputDataFields.groundtruth_keypoints: - tf.constant( - np.array([[[.1, .1]], [[.2, .2]], [[.5, .5]]], - np.float32)), - fields.InputDataFields.groundtruth_keypoint_visibilities: - tf.constant([[True, True], [False, False], [True, True]]), - fields.InputDataFields.groundtruth_instance_masks: - tf.constant(np.random.rand(3, 4, 4).astype(np.float32)), - fields.InputDataFields.groundtruth_is_crowd: - tf.constant([False, True, False]), - fields.InputDataFields.groundtruth_difficult: - tf.constant(np.array([0, 0, 1], np.int32)) - } - - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes) - transformed_inputs = input_transformation_fn(tensor_dict) - return (transformed_inputs[fields.InputDataFields.groundtruth_classes], - transformed_inputs[fields.InputDataFields.num_groundtruth_boxes], - transformed_inputs[fields.InputDataFields.groundtruth_area], - transformed_inputs[fields.InputDataFields. - groundtruth_confidences], - transformed_inputs[fields.InputDataFields.groundtruth_boxes], - transformed_inputs[fields.InputDataFields.groundtruth_keypoints], - transformed_inputs[fields.InputDataFields. - groundtruth_keypoint_visibilities], - transformed_inputs[fields.InputDataFields. - groundtruth_instance_masks], - transformed_inputs[fields.InputDataFields.groundtruth_is_crowd], - transformed_inputs[fields.InputDataFields.groundtruth_difficult]) - (groundtruth_classes, num_groundtruth_boxes, groundtruth_area, - groundtruth_confidences, groundtruth_boxes, groundtruth_keypoints, - groundtruth_keypoint_visibilities, groundtruth_instance_masks, - groundtruth_is_crowd, groundtruth_difficult) = self.execute_cpu(graph_fn, - []) - - self.assertAllClose(groundtruth_classes, [[0, 0, 1], [1, 0, 0]]) - self.assertAllEqual(num_groundtruth_boxes, 2) - self.assertAllClose(groundtruth_area, [.5, .3]) - self.assertAllEqual(groundtruth_confidences, [[0, 0, 1], [1, 0, 0]]) - self.assertAllClose(groundtruth_boxes, [[0, 0, 1, 1], [.5, .5, 1, 1]]) - self.assertAllClose(groundtruth_keypoints, [[[.1, .1]], [[.5, .5]]]) - self.assertAllEqual(groundtruth_keypoint_visibilities, - [[True, True], [True, True]]) - self.assertAllEqual(groundtruth_instance_masks.shape, [2, 4, 4]) - self.assertAllEqual(groundtruth_is_crowd, [False, False]) - self.assertAllEqual(groundtruth_difficult, [0, 1]) - - def test_returns_correct_merged_boxes(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.5, .5, 1, 1]], - np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, 1], np.int32)) - } - - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes, - merge_multiple_boxes=True) - transformed_inputs = input_transformation_fn(tensor_dict) - return (transformed_inputs[fields.InputDataFields.groundtruth_boxes], - transformed_inputs[fields.InputDataFields.groundtruth_classes], - transformed_inputs[fields.InputDataFields. - groundtruth_confidences], - transformed_inputs[fields.InputDataFields.num_groundtruth_boxes]) - (groundtruth_boxes, groundtruth_classes, groundtruth_confidences, - num_groundtruth_boxes) = self.execute_cpu(graph_fn, []) - self.assertAllClose( - groundtruth_boxes, - [[.5, .5, 1., 1.]]) - self.assertAllClose( - groundtruth_classes, - [[1, 0, 1]]) - self.assertAllClose( - groundtruth_confidences, - [[1, 0, 1]]) - self.assertAllClose( - num_groundtruth_boxes, - 1) - - def test_returns_correct_groundtruth_confidences_when_input_present(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[0, 0, 1, 1], [.5, .5, 1, 1]], np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, 1], np.int32)), - fields.InputDataFields.groundtruth_confidences: - tf.constant(np.array([1.0, -1.0], np.float32)) - } - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes) - transformed_inputs = input_transformation_fn(tensor_dict) - return (transformed_inputs[fields.InputDataFields.groundtruth_classes], - transformed_inputs[fields.InputDataFields. - groundtruth_confidences]) - groundtruth_classes, groundtruth_confidences = self.execute_cpu(graph_fn, - []) - self.assertAllClose( - groundtruth_classes, - [[0, 0, 1], [1, 0, 0]]) - self.assertAllClose( - groundtruth_confidences, - [[0, 0, 1], [-1, 0, 0]]) - - def test_returns_resized_masks(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(4, 4, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_instance_masks: - tf.constant(np.random.rand(2, 4, 4).astype(np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, 1], np.int32)), - fields.InputDataFields.original_image_spatial_shape: - tf.constant(np.array([4, 4], np.int32)) - } - - def fake_image_resizer_fn(image, masks=None): - resized_image = tf.image.resize_images(image, [8, 8]) - results = [resized_image] - if masks is not None: - resized_masks = tf.transpose( - tf.image.resize_images(tf.transpose(masks, [1, 2, 0]), [8, 8]), - [2, 0, 1]) - results.append(resized_masks) - results.append(tf.shape(resized_image)) - return results - - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=fake_image_resizer_fn, - num_classes=num_classes, - retain_original_image=True) - transformed_inputs = input_transformation_fn(tensor_dict) - return (transformed_inputs[fields.InputDataFields.original_image], - transformed_inputs[fields.InputDataFields. - original_image_spatial_shape], - transformed_inputs[fields.InputDataFields. - groundtruth_instance_masks]) - (original_image, original_image_shape, - groundtruth_instance_masks) = self.execute_cpu(graph_fn, []) - self.assertEqual(original_image.dtype, np.uint8) - self.assertAllEqual(original_image_shape, [4, 4]) - self.assertAllEqual(original_image.shape, [8, 8, 3]) - self.assertAllEqual(groundtruth_instance_masks.shape, [2, 8, 8]) - - def test_applies_model_preprocess_fn_to_image_tensor(self): - np_image = np.random.randint(256, size=(4, 4, 3)) - def graph_fn(image): - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, 1], np.int32)) - } - - def fake_model_preprocessor_fn(image): - return (image / 255., tf.expand_dims(tf.shape(image)[1:], axis=0)) - - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes) - transformed_inputs = input_transformation_fn(tensor_dict) - return (transformed_inputs[fields.InputDataFields.image], - transformed_inputs[fields.InputDataFields.true_image_shape]) - image, true_image_shape = self.execute_cpu(graph_fn, [np_image]) - self.assertAllClose(image, np_image / 255.) - self.assertAllClose(true_image_shape, [4, 4, 3]) - - def test_applies_data_augmentation_fn_to_tensor_dict(self): - np_image = np.random.randint(256, size=(4, 4, 3)) - def graph_fn(image): - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, 1], np.int32)) - } - - def add_one_data_augmentation_fn(tensor_dict): - return {key: value + 1 for key, value in tensor_dict.items()} - - num_classes = 4 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes, - data_augmentation_fn=add_one_data_augmentation_fn) - transformed_inputs = input_transformation_fn(tensor_dict) - return (transformed_inputs[fields.InputDataFields.image], - transformed_inputs[fields.InputDataFields.groundtruth_classes]) - image, groundtruth_classes = self.execute_cpu(graph_fn, [np_image]) - self.assertAllEqual(image, np_image + 1) - self.assertAllEqual( - groundtruth_classes, - [[0, 0, 0, 1], [0, 1, 0, 0]]) - - def test_applies_data_augmentation_fn_before_model_preprocess_fn(self): - np_image = np.random.randint(256, size=(4, 4, 3)) - def graph_fn(image): - tensor_dict = { - fields.InputDataFields.image: image, - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([3, 1], np.int32)) - } - - def mul_two_model_preprocessor_fn(image): - return (image * 2, tf.expand_dims(tf.shape(image)[1:], axis=0)) - - def add_five_to_image_data_augmentation_fn(tensor_dict): - tensor_dict[fields.InputDataFields.image] += 5 - return tensor_dict - - num_classes = 4 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=mul_two_model_preprocessor_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes, - data_augmentation_fn=add_five_to_image_data_augmentation_fn) - transformed_inputs = input_transformation_fn(tensor_dict) - return transformed_inputs[fields.InputDataFields.image] - image = self.execute_cpu(graph_fn, [np_image]) - self.assertAllEqual(image, (np_image + 5) * 2) - - def test_resize_with_padding(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(100, 50, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.0, .0, .5, .5]], - np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1, 2], np.int32)), - fields.InputDataFields.groundtruth_keypoints: - tf.constant([[[0.1, 0.2]], [[0.3, 0.4]]]), - } - - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_resize50_preprocess_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes,) - transformed_inputs = input_transformation_fn(tensor_dict) - return (transformed_inputs[fields.InputDataFields.groundtruth_boxes], - transformed_inputs[fields.InputDataFields.groundtruth_keypoints]) - groundtruth_boxes, groundtruth_keypoints = self.execute_cpu(graph_fn, []) - self.assertAllClose( - groundtruth_boxes, - [[.5, .25, 1., .5], [.0, .0, .5, .25]]) - self.assertAllClose( - groundtruth_keypoints, - [[[.1, .1]], [[.3, .2]]]) - - def test_groundtruth_keypoint_weights(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(100, 50, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.0, .0, .5, .5]], - np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1, 2], np.int32)), - fields.InputDataFields.groundtruth_keypoints: - tf.constant([[[0.1, 0.2], [0.3, 0.4]], - [[0.5, 0.6], [0.7, 0.8]]]), - fields.InputDataFields.groundtruth_keypoint_visibilities: - tf.constant([[True, False], [True, True]]), - } - - num_classes = 3 - keypoint_type_weight = [1.0, 2.0] - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_resize50_preprocess_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes, - keypoint_type_weight=keypoint_type_weight) - transformed_inputs = input_transformation_fn(tensor_dict=tensor_dict) - return (transformed_inputs[fields.InputDataFields.groundtruth_keypoints], - transformed_inputs[fields.InputDataFields. - groundtruth_keypoint_weights]) - - groundtruth_keypoints, groundtruth_keypoint_weights = self.execute_cpu( - graph_fn, []) - self.assertAllClose( - groundtruth_keypoints, - [[[0.1, 0.1], [0.3, 0.2]], - [[0.5, 0.3], [0.7, 0.4]]]) - self.assertAllClose( - groundtruth_keypoint_weights, - [[1.0, 0.0], [1.0, 2.0]]) - - def test_groundtruth_keypoint_weights_default(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(100, 50, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.0, .0, .5, .5]], - np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1, 2], np.int32)), - fields.InputDataFields.groundtruth_keypoints: - tf.constant([[[0.1, 0.2], [0.3, 0.4]], - [[0.5, 0.6], [0.7, 0.8]]]), - } - - num_classes = 3 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_resize50_preprocess_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes) - transformed_inputs = input_transformation_fn(tensor_dict=tensor_dict) - return (transformed_inputs[fields.InputDataFields.groundtruth_keypoints], - transformed_inputs[fields.InputDataFields. - groundtruth_keypoint_weights]) - groundtruth_keypoints, groundtruth_keypoint_weights = self.execute_cpu( - graph_fn, []) - self.assertAllClose( - groundtruth_keypoints, - [[[0.1, 0.1], [0.3, 0.2]], - [[0.5, 0.3], [0.7, 0.4]]]) - self.assertAllClose( - groundtruth_keypoint_weights, - [[1.0, 1.0], [1.0, 1.0]]) - - def test_groundtruth_dense_pose(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(100, 50, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.0, .0, .5, .5]], - np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1, 2], np.int32)), - fields.InputDataFields.groundtruth_dp_num_points: - tf.constant([0, 2], dtype=tf.int32), - fields.InputDataFields.groundtruth_dp_part_ids: - tf.constant([[0, 0], [4, 23]], dtype=tf.int32), - fields.InputDataFields.groundtruth_dp_surface_coords: - tf.constant([[[0., 0., 0., 0.,], [0., 0., 0., 0.,]], - [[0.1, 0.2, 0.3, 0.4,], [0.6, 0.8, 0.6, 0.7,]]], - dtype=tf.float32), - } - - num_classes = 1 - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_resize50_preprocess_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes) - transformed_inputs = input_transformation_fn(tensor_dict=tensor_dict) - transformed_dp_num_points = transformed_inputs[ - fields.InputDataFields.groundtruth_dp_num_points] - transformed_dp_part_ids = transformed_inputs[ - fields.InputDataFields.groundtruth_dp_part_ids] - transformed_dp_surface_coords = transformed_inputs[ - fields.InputDataFields.groundtruth_dp_surface_coords] - return (transformed_dp_num_points, transformed_dp_part_ids, - transformed_dp_surface_coords) - - dp_num_points, dp_part_ids, dp_surface_coords = self.execute_cpu( - graph_fn, []) - self.assertAllEqual(dp_num_points, [0, 2]) - self.assertAllEqual(dp_part_ids, [[0, 0], [4, 23]]) - self.assertAllClose( - dp_surface_coords, - [[[0., 0., 0., 0.,], [0., 0., 0., 0.,]], - [[0.1, 0.1, 0.3, 0.4,], [0.6, 0.4, 0.6, 0.7,]]]) - - def test_groundtruth_keypoint_depths(self): - def graph_fn(): - tensor_dict = { - fields.InputDataFields.image: - tf.constant(np.random.rand(100, 50, 3).astype(np.float32)), - fields.InputDataFields.groundtruth_boxes: - tf.constant(np.array([[.5, .5, 1, 1], [.0, .0, .5, .5]], - np.float32)), - fields.InputDataFields.groundtruth_classes: - tf.constant(np.array([1, 2], np.int32)), - fields.InputDataFields.groundtruth_keypoints: - tf.constant([[[0.1, 0.2], [0.3, 0.4]], - [[0.5, 0.6], [0.7, 0.8]]]), - fields.InputDataFields.groundtruth_keypoint_visibilities: - tf.constant([[True, False], [True, True]]), - fields.InputDataFields.groundtruth_keypoint_depths: - tf.constant([[1.0, 0.9], [0.8, 0.7]]), - fields.InputDataFields.groundtruth_keypoint_depth_weights: - tf.constant([[0.7, 0.8], [0.9, 1.0]]), - } - - num_classes = 3 - keypoint_type_weight = [1.0, 2.0] - input_transformation_fn = functools.partial( - inputs.transform_input_data, - model_preprocess_fn=_fake_resize50_preprocess_fn, - image_resizer_fn=_fake_image_resizer_fn, - num_classes=num_classes, - keypoint_type_weight=keypoint_type_weight) - transformed_inputs = input_transformation_fn(tensor_dict=tensor_dict) - return (transformed_inputs[ - fields.InputDataFields.groundtruth_keypoint_depths], - transformed_inputs[ - fields.InputDataFields.groundtruth_keypoint_depth_weights]) - - keypoint_depths, keypoint_depth_weights = self.execute_cpu(graph_fn, []) - self.assertAllClose( - keypoint_depths, - [[1.0, 0.9], [0.8, 0.7]]) - self.assertAllClose( - keypoint_depth_weights, - [[0.7, 0.8], [0.9, 1.0]]) - - -class PadInputDataToStaticShapesFnTest(test_case.TestCase): - - def test_pad_images_boxes_and_classes(self): - input_tensor_dict = { - fields.InputDataFields.image: - tf.random.uniform([3, 3, 3]), - fields.InputDataFields.groundtruth_boxes: - tf.random.uniform([2, 4]), - fields.InputDataFields.groundtruth_classes: - tf.random.uniform([2, 3], minval=0, maxval=2, dtype=tf.int32), - fields.InputDataFields.true_image_shape: - tf.constant([3, 3, 3]), - fields.InputDataFields.original_image_spatial_shape: - tf.constant([3, 3]) - } - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6]) - - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), - [5, 6, 3]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.true_image_shape] - .shape.as_list(), [3]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.original_image_spatial_shape] - .shape.as_list(), [2]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.groundtruth_boxes] - .shape.as_list(), [3, 4]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.groundtruth_classes] - .shape.as_list(), [3, 3]) - - def test_clip_boxes_and_classes(self): - def graph_fn(): - input_tensor_dict = { - fields.InputDataFields.groundtruth_boxes: - tf.random.uniform([5, 4]), - fields.InputDataFields.groundtruth_classes: - tf.random.uniform([2, 3], maxval=10, dtype=tf.int32), - fields.InputDataFields.num_groundtruth_boxes: - tf.constant(5) - } - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6]) - return (padded_tensor_dict[fields.InputDataFields.groundtruth_boxes], - padded_tensor_dict[fields.InputDataFields.groundtruth_classes], - padded_tensor_dict[fields.InputDataFields.num_groundtruth_boxes]) - (groundtruth_boxes, groundtruth_classes, - num_groundtruth_boxes) = self.execute_cpu(graph_fn, []) - self.assertAllEqual(groundtruth_boxes.shape, [3, 4]) - self.assertAllEqual(groundtruth_classes.shape, [3, 3]) - self.assertEqual(num_groundtruth_boxes, 3) - - def test_images_and_additional_channels(self): - input_tensor_dict = { - fields.InputDataFields.image: - test_utils.image_with_dynamic_shape(4, 3, 5), - fields.InputDataFields.image_additional_channels: - test_utils.image_with_dynamic_shape(4, 3, 2), - } - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6]) - - # pad_input_data_to_static_shape assumes that image is already concatenated - # with additional channels. - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), - [5, 6, 5]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.image_additional_channels] - .shape.as_list(), [5, 6, 2]) - - def test_images_and_additional_channels_errors(self): - input_tensor_dict = { - fields.InputDataFields.image: - test_utils.image_with_dynamic_shape(10, 10, 3), - fields.InputDataFields.image_additional_channels: - test_utils.image_with_dynamic_shape(10, 10, 2), - fields.InputDataFields.original_image: - test_utils.image_with_dynamic_shape(10, 10, 3), - } - with self.assertRaises(ValueError): - _ = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6]) - - def test_gray_images(self): - input_tensor_dict = { - fields.InputDataFields.image: - test_utils.image_with_dynamic_shape(4, 4, 1), - } - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6]) - - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), - [5, 6, 1]) - - def test_gray_images_and_additional_channels(self): - input_tensor_dict = { - fields.InputDataFields.image: - test_utils.image_with_dynamic_shape(4, 4, 3), - fields.InputDataFields.image_additional_channels: - test_utils.image_with_dynamic_shape(4, 4, 2), - } - # pad_input_data_to_static_shape assumes that image is already concatenated - # with additional channels. - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6]) - - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.image].shape.as_list(), - [5, 6, 3]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.image_additional_channels] - .shape.as_list(), [5, 6, 2]) - - def test_keypoints(self): - keypoints = test_utils.keypoints_with_dynamic_shape(10, 16, 4) - visibilities = tf.cast(tf.random.uniform(tf.shape(keypoints)[:-1], minval=0, - maxval=2, dtype=tf.int32), tf.bool) - input_tensor_dict = { - fields.InputDataFields.groundtruth_keypoints: - test_utils.keypoints_with_dynamic_shape(10, 16, 4), - fields.InputDataFields.groundtruth_keypoint_visibilities: - visibilities - } - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6]) - - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.groundtruth_keypoints] - .shape.as_list(), [3, 16, 4]) - self.assertAllEqual( - padded_tensor_dict[ - fields.InputDataFields.groundtruth_keypoint_visibilities] - .shape.as_list(), [3, 16]) - - def test_dense_pose(self): - input_tensor_dict = { - fields.InputDataFields.groundtruth_dp_num_points: - tf.constant([0, 2], dtype=tf.int32), - fields.InputDataFields.groundtruth_dp_part_ids: - tf.constant([[0, 0], [4, 23]], dtype=tf.int32), - fields.InputDataFields.groundtruth_dp_surface_coords: - tf.constant([[[0., 0., 0., 0.,], [0., 0., 0., 0.,]], - [[0.1, 0.2, 0.3, 0.4,], [0.6, 0.8, 0.6, 0.7,]]], - dtype=tf.float32), - } - - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=1, - spatial_image_shape=[128, 128], - max_dp_points=200) - - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.groundtruth_dp_num_points] - .shape.as_list(), [3]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.groundtruth_dp_part_ids] - .shape.as_list(), [3, 200]) - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.groundtruth_dp_surface_coords] - .shape.as_list(), [3, 200, 4]) - - def test_pad_input_data_to_static_shapes_for_trackid(self): - input_tensor_dict = { - fields.InputDataFields.groundtruth_track_ids: - tf.constant([0, 1], dtype=tf.int32), - } - - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=1, - spatial_image_shape=[128, 128]) - - self.assertAllEqual( - padded_tensor_dict[fields.InputDataFields.groundtruth_track_ids] - .shape.as_list(), [3]) - - def test_context_features(self): - context_memory_size = 8 - context_feature_length = 10 - max_num_context_features = 20 - def graph_fn(): - input_tensor_dict = { - fields.InputDataFields.context_features: - tf.ones([context_memory_size, context_feature_length]), - fields.InputDataFields.context_feature_length: - tf.constant(context_feature_length) - } - padded_tensor_dict = inputs.pad_input_data_to_static_shapes( - tensor_dict=input_tensor_dict, - max_num_boxes=3, - num_classes=3, - spatial_image_shape=[5, 6], - max_num_context_features=max_num_context_features, - context_feature_length=context_feature_length) - - self.assertAllEqual( - padded_tensor_dict[ - fields.InputDataFields.context_features].shape.as_list(), - [max_num_context_features, context_feature_length]) - return padded_tensor_dict[fields.InputDataFields.valid_context_size] - - valid_context_size = self.execute_cpu(graph_fn, []) - self.assertEqual(valid_context_size, context_memory_size) - - -class NegativeSizeTest(test_case.TestCase): - """Test for inputs and related funcitons.""" - - def test_negative_size_error(self): - """Test that error is raised for negative size boxes.""" - - def graph_fn(): - tensors = { - fields.InputDataFields.image: tf.zeros((128, 128, 3)), - fields.InputDataFields.groundtruth_classes: - tf.constant([1, 1], tf.int32), - fields.InputDataFields.groundtruth_boxes: - tf.constant([[0.5, 0.5, 0.4, 0.5]], tf.float32) - } - tensors = inputs.transform_input_data( - tensors, _fake_model_preprocessor_fn, _fake_image_resizer_fn, - num_classes=10) - return tensors[fields.InputDataFields.groundtruth_boxes] - with self.assertRaises(tf.errors.InvalidArgumentError): - self.execute_cpu(graph_fn, []) - - def test_negative_size_no_assert(self): - """Test that negative size boxes are filtered out without assert. - - This test simulates the behaviour when we run on TPU and Assert ops are - not supported. - """ - - tensors = { - fields.InputDataFields.image: tf.zeros((128, 128, 3)), - fields.InputDataFields.groundtruth_classes: - tf.constant([1, 1], tf.int32), - fields.InputDataFields.groundtruth_boxes: - tf.constant([[0.5, 0.5, 0.4, 0.5], [0.5, 0.5, 0.6, 0.6]], - tf.float32) - } - - with mock.patch.object(tf, 'Assert') as tf_assert: - tf_assert.return_value = tf.no_op() - tensors = inputs.transform_input_data( - tensors, _fake_model_preprocessor_fn, _fake_image_resizer_fn, - num_classes=10) - - self.assertAllClose(tensors[fields.InputDataFields.groundtruth_boxes], - [[0.5, 0.5, 0.6, 0.6]]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/legacy/__init__.py b/research/object_detection/legacy/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/legacy/eval.py b/research/object_detection/legacy/eval.py deleted file mode 100644 index 9a7d8c430fa..00000000000 --- a/research/object_detection/legacy/eval.py +++ /dev/null @@ -1,142 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Evaluation executable for detection models. - -This executable is used to evaluate DetectionModels. There are two ways of -configuring the eval job. - -1) A single pipeline_pb2.TrainEvalPipelineConfig file maybe specified instead. -In this mode, the --eval_training_data flag may be given to force the pipeline -to evaluate on training data instead. - -Example usage: - ./eval \ - --logtostderr \ - --checkpoint_dir=path/to/checkpoint_dir \ - --eval_dir=path/to/eval_dir \ - --pipeline_config_path=pipeline_config.pbtxt - -2) Three configuration files may be provided: a model_pb2.DetectionModel -configuration file to define what type of DetectionModel is being evaluated, an -input_reader_pb2.InputReader file to specify what data the model is evaluating -and an eval_pb2.EvalConfig file to configure evaluation parameters. - -Example usage: - ./eval \ - --logtostderr \ - --checkpoint_dir=path/to/checkpoint_dir \ - --eval_dir=path/to/eval_dir \ - --eval_config_path=eval_config.pbtxt \ - --model_config_path=model_config.pbtxt \ - --input_config_path=eval_input_config.pbtxt -""" -import functools -import os -import tensorflow.compat.v1 as tf -from tensorflow.python.util.deprecation import deprecated -from object_detection.builders import dataset_builder -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import model_builder -from object_detection.legacy import evaluator -from object_detection.utils import config_util -from object_detection.utils import label_map_util - -tf.logging.set_verbosity(tf.logging.INFO) - -flags = tf.app.flags -flags.DEFINE_boolean('eval_training_data', False, - 'If training data should be evaluated for this job.') -flags.DEFINE_string( - 'checkpoint_dir', '', - 'Directory containing checkpoints to evaluate, typically ' - 'set to `train_dir` used in the training job.') -flags.DEFINE_string('eval_dir', '', 'Directory to write eval summaries to.') -flags.DEFINE_string( - 'pipeline_config_path', '', - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file. If provided, other configs are ignored') -flags.DEFINE_string('eval_config_path', '', - 'Path to an eval_pb2.EvalConfig config file.') -flags.DEFINE_string('input_config_path', '', - 'Path to an input_reader_pb2.InputReader config file.') -flags.DEFINE_string('model_config_path', '', - 'Path to a model_pb2.DetectionModel config file.') -flags.DEFINE_boolean( - 'run_once', False, 'Option to only run a single pass of ' - 'evaluation. Overrides the `max_evals` parameter in the ' - 'provided config.') -FLAGS = flags.FLAGS - - -@deprecated(None, 'Use object_detection/model_main.py.') -def main(unused_argv): - assert FLAGS.checkpoint_dir, '`checkpoint_dir` is missing.' - assert FLAGS.eval_dir, '`eval_dir` is missing.' - tf.gfile.MakeDirs(FLAGS.eval_dir) - if FLAGS.pipeline_config_path: - configs = config_util.get_configs_from_pipeline_file( - FLAGS.pipeline_config_path) - tf.gfile.Copy( - FLAGS.pipeline_config_path, - os.path.join(FLAGS.eval_dir, 'pipeline.config'), - overwrite=True) - else: - configs = config_util.get_configs_from_multiple_files( - model_config_path=FLAGS.model_config_path, - eval_config_path=FLAGS.eval_config_path, - eval_input_config_path=FLAGS.input_config_path) - for name, config in [('model.config', FLAGS.model_config_path), - ('eval.config', FLAGS.eval_config_path), - ('input.config', FLAGS.input_config_path)]: - tf.gfile.Copy(config, os.path.join(FLAGS.eval_dir, name), overwrite=True) - - model_config = configs['model'] - eval_config = configs['eval_config'] - input_config = configs['eval_input_config'] - if FLAGS.eval_training_data: - input_config = configs['train_input_config'] - - model_fn = functools.partial( - model_builder.build, model_config=model_config, is_training=False) - - def get_next(config): - return dataset_builder.make_initializable_iterator( - dataset_builder.build(config)).get_next() - - create_input_dict_fn = functools.partial(get_next, input_config) - - categories = label_map_util.create_categories_from_labelmap( - input_config.label_map_path) - - if FLAGS.run_once: - eval_config.max_evals = 1 - - graph_rewriter_fn = None - if 'graph_rewriter_config' in configs: - graph_rewriter_fn = graph_rewriter_builder.build( - configs['graph_rewriter_config'], is_training=False) - - evaluator.evaluate( - create_input_dict_fn, - model_fn, - eval_config, - categories, - FLAGS.checkpoint_dir, - FLAGS.eval_dir, - graph_hook_fn=graph_rewriter_fn) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/legacy/evaluator.py b/research/object_detection/legacy/evaluator.py deleted file mode 100644 index a18ac5559f1..00000000000 --- a/research/object_detection/legacy/evaluator.py +++ /dev/null @@ -1,299 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Detection model evaluator. - -This file provides a generic evaluation method that can be used to evaluate a -DetectionModel. -""" - -import logging -import tensorflow.compat.v1 as tf - -from object_detection import eval_util -from object_detection.core import prefetcher -from object_detection.core import standard_fields as fields -from object_detection.metrics import coco_evaluation -from object_detection.utils import object_detection_evaluation - -# A dictionary of metric names to classes that implement the metric. The classes -# in the dictionary must implement -# utils.object_detection_evaluation.DetectionEvaluator interface. -EVAL_METRICS_CLASS_DICT = { - 'pascal_voc_detection_metrics': - object_detection_evaluation.PascalDetectionEvaluator, - 'weighted_pascal_voc_detection_metrics': - object_detection_evaluation.WeightedPascalDetectionEvaluator, - 'pascal_voc_instance_segmentation_metrics': - object_detection_evaluation.PascalInstanceSegmentationEvaluator, - 'weighted_pascal_voc_instance_segmentation_metrics': - object_detection_evaluation.WeightedPascalInstanceSegmentationEvaluator, - 'oid_V2_detection_metrics': - object_detection_evaluation.OpenImagesDetectionEvaluator, - # DEPRECATED: please use oid_V2_detection_metrics instead - 'open_images_V2_detection_metrics': - object_detection_evaluation.OpenImagesDetectionEvaluator, - 'coco_detection_metrics': - coco_evaluation.CocoDetectionEvaluator, - 'coco_mask_metrics': - coco_evaluation.CocoMaskEvaluator, - 'oid_challenge_detection_metrics': - object_detection_evaluation.OpenImagesDetectionChallengeEvaluator, - # DEPRECATED: please use oid_challenge_detection_metrics instead - 'oid_challenge_object_detection_metrics': - object_detection_evaluation.OpenImagesDetectionChallengeEvaluator, - 'oid_challenge_segmentation_metrics': - object_detection_evaluation - .OpenImagesInstanceSegmentationChallengeEvaluator, -} - -EVAL_DEFAULT_METRIC = 'pascal_voc_detection_metrics' - - -def _extract_predictions_and_losses(model, - create_input_dict_fn, - ignore_groundtruth=False): - """Constructs tensorflow detection graph and returns output tensors. - - Args: - model: model to perform predictions with. - create_input_dict_fn: function to create input tensor dictionaries. - ignore_groundtruth: whether groundtruth should be ignored. - - Returns: - prediction_groundtruth_dict: A dictionary with postprocessed tensors (keyed - by standard_fields.DetectionResultsFields) and optional groundtruth - tensors (keyed by standard_fields.InputDataFields). - losses_dict: A dictionary containing detection losses. This is empty when - ignore_groundtruth is true. - """ - input_dict = create_input_dict_fn() - prefetch_queue = prefetcher.prefetch(input_dict, capacity=500) - input_dict = prefetch_queue.dequeue() - original_image = tf.expand_dims(input_dict[fields.InputDataFields.image], 0) - preprocessed_image, true_image_shapes = model.preprocess( - tf.cast(original_image, dtype=tf.float32)) - prediction_dict = model.predict(preprocessed_image, true_image_shapes) - detections = model.postprocess(prediction_dict, true_image_shapes) - - groundtruth = None - losses_dict = {} - if not ignore_groundtruth: - groundtruth = { - fields.InputDataFields.groundtruth_boxes: - input_dict[fields.InputDataFields.groundtruth_boxes], - fields.InputDataFields.groundtruth_classes: - input_dict[fields.InputDataFields.groundtruth_classes], - fields.InputDataFields.groundtruth_area: - input_dict[fields.InputDataFields.groundtruth_area], - fields.InputDataFields.groundtruth_is_crowd: - input_dict[fields.InputDataFields.groundtruth_is_crowd], - fields.InputDataFields.groundtruth_difficult: - input_dict[fields.InputDataFields.groundtruth_difficult] - } - if fields.InputDataFields.groundtruth_group_of in input_dict: - groundtruth[fields.InputDataFields.groundtruth_group_of] = ( - input_dict[fields.InputDataFields.groundtruth_group_of]) - groundtruth_masks_list = None - if fields.DetectionResultFields.detection_masks in detections: - groundtruth[fields.InputDataFields.groundtruth_instance_masks] = ( - input_dict[fields.InputDataFields.groundtruth_instance_masks]) - groundtruth_masks_list = [ - input_dict[fields.InputDataFields.groundtruth_instance_masks]] - groundtruth_keypoints_list = None - if fields.DetectionResultFields.detection_keypoints in detections: - groundtruth[fields.InputDataFields.groundtruth_keypoints] = ( - input_dict[fields.InputDataFields.groundtruth_keypoints]) - groundtruth_keypoints_list = [ - input_dict[fields.InputDataFields.groundtruth_keypoints]] - label_id_offset = 1 - model.provide_groundtruth( - [input_dict[fields.InputDataFields.groundtruth_boxes]], - [tf.one_hot(input_dict[fields.InputDataFields.groundtruth_classes] - - label_id_offset, depth=model.num_classes)], - groundtruth_masks_list=groundtruth_masks_list, - groundtruth_keypoints_list=groundtruth_keypoints_list) - losses_dict.update(model.loss(prediction_dict, true_image_shapes)) - - result_dict = eval_util.result_dict_for_single_example( - original_image, - input_dict[fields.InputDataFields.source_id], - detections, - groundtruth, - class_agnostic=( - fields.DetectionResultFields.detection_classes not in detections), - scale_to_absolute=True) - return result_dict, losses_dict - - -def get_evaluators(eval_config, categories): - """Returns the evaluator class according to eval_config, valid for categories. - - Args: - eval_config: evaluation configurations. - categories: a list of categories to evaluate. - Returns: - An list of instances of DetectionEvaluator. - - Raises: - ValueError: if metric is not in the metric class dictionary. - """ - eval_metric_fn_keys = eval_config.metrics_set - if not eval_metric_fn_keys: - eval_metric_fn_keys = [EVAL_DEFAULT_METRIC] - evaluators_list = [] - for eval_metric_fn_key in eval_metric_fn_keys: - if eval_metric_fn_key not in EVAL_METRICS_CLASS_DICT: - raise ValueError('Metric not found: {}'.format(eval_metric_fn_key)) - if eval_metric_fn_key == 'oid_challenge_object_detection_metrics': - logging.warning( - 'oid_challenge_object_detection_metrics is deprecated; ' - 'use oid_challenge_detection_metrics instead' - ) - if eval_metric_fn_key == 'oid_V2_detection_metrics': - logging.warning( - 'open_images_V2_detection_metrics is deprecated; ' - 'use oid_V2_detection_metrics instead' - ) - evaluators_list.append( - EVAL_METRICS_CLASS_DICT[eval_metric_fn_key](categories=categories)) - return evaluators_list - - -def evaluate(create_input_dict_fn, create_model_fn, eval_config, categories, - checkpoint_dir, eval_dir, graph_hook_fn=None, evaluator_list=None): - """Evaluation function for detection models. - - Args: - create_input_dict_fn: a function to create a tensor input dictionary. - create_model_fn: a function that creates a DetectionModel. - eval_config: a eval_pb2.EvalConfig protobuf. - categories: a list of category dictionaries. Each dict in the list should - have an integer 'id' field and string 'name' field. - checkpoint_dir: directory to load the checkpoints to evaluate from. - eval_dir: directory to write evaluation metrics summary to. - graph_hook_fn: Optional function that is called after the training graph is - completely built. This is helpful to perform additional changes to the - training graph such as optimizing batchnorm. The function should modify - the default graph. - evaluator_list: Optional list of instances of DetectionEvaluator. If not - given, this list of metrics is created according to the eval_config. - - Returns: - metrics: A dictionary containing metric names and values from the latest - run. - """ - - model = create_model_fn() - - if eval_config.ignore_groundtruth and not eval_config.export_path: - logging.fatal('If ignore_groundtruth=True then an export_path is ' - 'required. Aborting!!!') - - tensor_dict, losses_dict = _extract_predictions_and_losses( - model=model, - create_input_dict_fn=create_input_dict_fn, - ignore_groundtruth=eval_config.ignore_groundtruth) - - def _process_batch(tensor_dict, sess, batch_index, counters, - losses_dict=None): - """Evaluates tensors in tensor_dict, losses_dict and visualizes examples. - - This function calls sess.run on tensor_dict, evaluating the original_image - tensor only on the first K examples and visualizing detections overlaid - on this original_image. - - Args: - tensor_dict: a dictionary of tensors - sess: tensorflow session - batch_index: the index of the batch amongst all batches in the run. - counters: a dictionary holding 'success' and 'skipped' fields which can - be updated to keep track of number of successful and failed runs, - respectively. If these fields are not updated, then the success/skipped - counter values shown at the end of evaluation will be incorrect. - losses_dict: Optional dictonary of scalar loss tensors. - - Returns: - result_dict: a dictionary of numpy arrays - result_losses_dict: a dictionary of scalar losses. This is empty if input - losses_dict is None. - """ - try: - if not losses_dict: - losses_dict = {} - result_dict, result_losses_dict = sess.run([tensor_dict, losses_dict]) - counters['success'] += 1 - except tf.errors.InvalidArgumentError: - logging.info('Skipping image') - counters['skipped'] += 1 - return {}, {} - global_step = tf.train.global_step(sess, tf.train.get_global_step()) - if batch_index < eval_config.num_visualizations: - tag = 'image-{}'.format(batch_index) - eval_util.visualize_detection_results( - result_dict, - tag, - global_step, - categories=categories, - summary_dir=eval_dir, - export_dir=eval_config.visualization_export_dir, - show_groundtruth=eval_config.visualize_groundtruth_boxes, - groundtruth_box_visualization_color=eval_config. - groundtruth_box_visualization_color, - min_score_thresh=eval_config.min_score_threshold, - max_num_predictions=eval_config.max_num_boxes_to_visualize, - skip_scores=eval_config.skip_scores, - skip_labels=eval_config.skip_labels, - keep_image_id_for_visualization_export=eval_config. - keep_image_id_for_visualization_export) - return result_dict, result_losses_dict - - if graph_hook_fn: graph_hook_fn() - - variables_to_restore = tf.global_variables() - global_step = tf.train.get_or_create_global_step() - variables_to_restore.append(global_step) - - if eval_config.use_moving_averages: - variable_averages = tf.train.ExponentialMovingAverage(0.0) - variables_to_restore = variable_averages.variables_to_restore() - saver = tf.train.Saver(variables_to_restore) - - def _restore_latest_checkpoint(sess): - latest_checkpoint = tf.train.latest_checkpoint(checkpoint_dir) - saver.restore(sess, latest_checkpoint) - - if not evaluator_list: - evaluator_list = get_evaluators(eval_config, categories) - - metrics = eval_util.repeated_checkpoint_run( - tensor_dict=tensor_dict, - summary_dir=eval_dir, - evaluators=evaluator_list, - batch_processor=_process_batch, - checkpoint_dirs=[checkpoint_dir], - variables_to_restore=None, - restore_fn=_restore_latest_checkpoint, - num_batches=eval_config.num_examples, - eval_interval_secs=eval_config.eval_interval_secs, - max_number_of_evaluations=(1 if eval_config.ignore_groundtruth else - eval_config.max_evals - if eval_config.max_evals else None), - master=eval_config.eval_master, - save_graph=eval_config.save_graph, - save_graph_dir=(eval_dir if eval_config.save_graph else ''), - losses_dict=losses_dict, - eval_export_path=eval_config.export_path) - - return metrics diff --git a/research/object_detection/legacy/train.py b/research/object_detection/legacy/train.py deleted file mode 100644 index 615773760a3..00000000000 --- a/research/object_detection/legacy/train.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Training executable for detection models. - -This executable is used to train DetectionModels. There are two ways of -configuring the training job: - -1) A single pipeline_pb2.TrainEvalPipelineConfig configuration file -can be specified by --pipeline_config_path. - -Example usage: - ./train \ - --logtostderr \ - --train_dir=path/to/train_dir \ - --pipeline_config_path=pipeline_config.pbtxt - -2) Three configuration files can be provided: a model_pb2.DetectionModel -configuration file to define what type of DetectionModel is being trained, an -input_reader_pb2.InputReader file to specify what training data will be used and -a train_pb2.TrainConfig file to configure training parameters. - -Example usage: - ./train \ - --logtostderr \ - --train_dir=path/to/train_dir \ - --model_config_path=model_config.pbtxt \ - --train_config_path=train_config.pbtxt \ - --input_config_path=train_input_config.pbtxt -""" - -import functools -import json -import os -import tensorflow.compat.v1 as tf -from tensorflow.python.util.deprecation import deprecated - - -from object_detection.builders import dataset_builder -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import model_builder -from object_detection.legacy import trainer -from object_detection.utils import config_util - -tf.logging.set_verbosity(tf.logging.INFO) - -flags = tf.app.flags -flags.DEFINE_string('master', '', 'Name of the TensorFlow master to use.') -flags.DEFINE_integer('task', 0, 'task id') -flags.DEFINE_integer('num_clones', 1, 'Number of clones to deploy per worker.') -flags.DEFINE_boolean('clone_on_cpu', False, - 'Force clones to be deployed on CPU. Note that even if ' - 'set to False (allowing ops to run on gpu), some ops may ' - 'still be run on the CPU if they have no GPU kernel.') -flags.DEFINE_integer('worker_replicas', 1, 'Number of worker+trainer ' - 'replicas.') -flags.DEFINE_integer('ps_tasks', 0, - 'Number of parameter server tasks. If None, does not use ' - 'a parameter server.') -flags.DEFINE_string('train_dir', '', - 'Directory to save the checkpoints and training summaries.') - -flags.DEFINE_string('pipeline_config_path', '', - 'Path to a pipeline_pb2.TrainEvalPipelineConfig config ' - 'file. If provided, other configs are ignored') - -flags.DEFINE_string('train_config_path', '', - 'Path to a train_pb2.TrainConfig config file.') -flags.DEFINE_string('input_config_path', '', - 'Path to an input_reader_pb2.InputReader config file.') -flags.DEFINE_string('model_config_path', '', - 'Path to a model_pb2.DetectionModel config file.') - -FLAGS = flags.FLAGS - - -@deprecated(None, 'Use object_detection/model_main.py.') -def main(_): - assert FLAGS.train_dir, '`train_dir` is missing.' - if FLAGS.task == 0: tf.gfile.MakeDirs(FLAGS.train_dir) - if FLAGS.pipeline_config_path: - configs = config_util.get_configs_from_pipeline_file( - FLAGS.pipeline_config_path) - if FLAGS.task == 0: - tf.gfile.Copy(FLAGS.pipeline_config_path, - os.path.join(FLAGS.train_dir, 'pipeline.config'), - overwrite=True) - else: - configs = config_util.get_configs_from_multiple_files( - model_config_path=FLAGS.model_config_path, - train_config_path=FLAGS.train_config_path, - train_input_config_path=FLAGS.input_config_path) - if FLAGS.task == 0: - for name, config in [('model.config', FLAGS.model_config_path), - ('train.config', FLAGS.train_config_path), - ('input.config', FLAGS.input_config_path)]: - tf.gfile.Copy(config, os.path.join(FLAGS.train_dir, name), - overwrite=True) - - model_config = configs['model'] - train_config = configs['train_config'] - input_config = configs['train_input_config'] - - model_fn = functools.partial( - model_builder.build, - model_config=model_config, - is_training=True) - - def get_next(config): - return dataset_builder.make_initializable_iterator( - dataset_builder.build(config)).get_next() - - create_input_dict_fn = functools.partial(get_next, input_config) - - env = json.loads(os.environ.get('TF_CONFIG', '{}')) - cluster_data = env.get('cluster', None) - cluster = tf.train.ClusterSpec(cluster_data) if cluster_data else None - task_data = env.get('task', None) or {'type': 'master', 'index': 0} - task_info = type('TaskSpec', (object,), task_data) - - # Parameters for a single worker. - ps_tasks = 0 - worker_replicas = 1 - worker_job_name = 'lonely_worker' - task = 0 - is_chief = True - master = '' - - if cluster_data and 'worker' in cluster_data: - # Number of total worker replicas include "worker"s and the "master". - worker_replicas = len(cluster_data['worker']) + 1 - if cluster_data and 'ps' in cluster_data: - ps_tasks = len(cluster_data['ps']) - - if worker_replicas > 1 and ps_tasks < 1: - raise ValueError('At least 1 ps task is needed for distributed training.') - - if worker_replicas >= 1 and ps_tasks > 0: - # Set up distributed training. - server = tf.train.Server(tf.train.ClusterSpec(cluster), protocol='grpc', - job_name=task_info.type, - task_index=task_info.index) - if task_info.type == 'ps': - server.join() - return - - worker_job_name = '%s/task:%d' % (task_info.type, task_info.index) - task = task_info.index - is_chief = (task_info.type == 'master') - master = server.target - - graph_rewriter_fn = None - if 'graph_rewriter_config' in configs: - graph_rewriter_fn = graph_rewriter_builder.build( - configs['graph_rewriter_config'], is_training=True) - - trainer.train( - create_input_dict_fn, - model_fn, - train_config, - master, - task, - FLAGS.num_clones, - worker_replicas, - FLAGS.clone_on_cpu, - ps_tasks, - worker_job_name, - is_chief, - FLAGS.train_dir, - graph_hook_fn=graph_rewriter_fn) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/legacy/trainer.py b/research/object_detection/legacy/trainer.py deleted file mode 100644 index 21f8973d78c..00000000000 --- a/research/object_detection/legacy/trainer.py +++ /dev/null @@ -1,415 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Detection model trainer. - -This file provides a generic training method that can be used to train a -DetectionModel. -""" - -import functools - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.builders import optimizer_builder -from object_detection.builders import preprocessor_builder -from object_detection.core import batcher -from object_detection.core import preprocessor -from object_detection.core import standard_fields as fields -from object_detection.utils import ops as util_ops -from object_detection.utils import variables_helper -from deployment import model_deploy - - -def create_input_queue(batch_size_per_clone, create_tensor_dict_fn, - batch_queue_capacity, num_batch_queue_threads, - prefetch_queue_capacity, data_augmentation_options): - """Sets up reader, prefetcher and returns input queue. - - Args: - batch_size_per_clone: batch size to use per clone. - create_tensor_dict_fn: function to create tensor dictionary. - batch_queue_capacity: maximum number of elements to store within a queue. - num_batch_queue_threads: number of threads to use for batching. - prefetch_queue_capacity: maximum capacity of the queue used to prefetch - assembled batches. - data_augmentation_options: a list of tuples, where each tuple contains a - data augmentation function and a dictionary containing arguments and their - values (see preprocessor.py). - - Returns: - input queue: a batcher.BatchQueue object holding enqueued tensor_dicts - (which hold images, boxes and targets). To get a batch of tensor_dicts, - call input_queue.Dequeue(). - """ - tensor_dict = create_tensor_dict_fn() - - tensor_dict[fields.InputDataFields.image] = tf.expand_dims( - tensor_dict[fields.InputDataFields.image], 0) - - images = tensor_dict[fields.InputDataFields.image] - float_images = tf.cast(images, dtype=tf.float32) - tensor_dict[fields.InputDataFields.image] = float_images - - include_instance_masks = (fields.InputDataFields.groundtruth_instance_masks - in tensor_dict) - include_keypoints = (fields.InputDataFields.groundtruth_keypoints - in tensor_dict) - include_multiclass_scores = (fields.InputDataFields.multiclass_scores - in tensor_dict) - if data_augmentation_options: - tensor_dict = preprocessor.preprocess( - tensor_dict, data_augmentation_options, - func_arg_map=preprocessor.get_default_func_arg_map( - include_label_weights=True, - include_multiclass_scores=include_multiclass_scores, - include_instance_masks=include_instance_masks, - include_keypoints=include_keypoints)) - - input_queue = batcher.BatchQueue( - tensor_dict, - batch_size=batch_size_per_clone, - batch_queue_capacity=batch_queue_capacity, - num_batch_queue_threads=num_batch_queue_threads, - prefetch_queue_capacity=prefetch_queue_capacity) - return input_queue - - -def get_inputs(input_queue, - num_classes, - merge_multiple_label_boxes=False, - use_multiclass_scores=False): - """Dequeues batch and constructs inputs to object detection model. - - Args: - input_queue: BatchQueue object holding enqueued tensor_dicts. - num_classes: Number of classes. - merge_multiple_label_boxes: Whether to merge boxes with multiple labels - or not. Defaults to false. Merged boxes are represented with a single - box and a k-hot encoding of the multiple labels associated with the - boxes. - use_multiclass_scores: Whether to use multiclass scores instead of - groundtruth_classes. - - Returns: - images: a list of 3-D float tensor of images. - image_keys: a list of string keys for the images. - locations_list: a list of tensors of shape [num_boxes, 4] - containing the corners of the groundtruth boxes. - classes_list: a list of padded one-hot (or K-hot) float32 tensors containing - target classes. - masks_list: a list of 3-D float tensors of shape [num_boxes, image_height, - image_width] containing instance masks for objects if present in the - input_queue. Else returns None. - keypoints_list: a list of 3-D float tensors of shape [num_boxes, - num_keypoints, 2] containing keypoints for objects if present in the - input queue. Else returns None. - weights_lists: a list of 1-D float32 tensors of shape [num_boxes] - containing groundtruth weight for each box. - """ - read_data_list = input_queue.dequeue() - label_id_offset = 1 - def extract_images_and_targets(read_data): - """Extract images and targets from the input dict.""" - image = read_data[fields.InputDataFields.image] - key = '' - if fields.InputDataFields.source_id in read_data: - key = read_data[fields.InputDataFields.source_id] - location_gt = read_data[fields.InputDataFields.groundtruth_boxes] - classes_gt = tf.cast(read_data[fields.InputDataFields.groundtruth_classes], - tf.int32) - classes_gt -= label_id_offset - - if merge_multiple_label_boxes and use_multiclass_scores: - raise ValueError( - 'Using both merge_multiple_label_boxes and use_multiclass_scores is' - 'not supported' - ) - - if merge_multiple_label_boxes: - location_gt, classes_gt, _ = util_ops.merge_boxes_with_multiple_labels( - location_gt, classes_gt, num_classes) - classes_gt = tf.cast(classes_gt, tf.float32) - elif use_multiclass_scores: - classes_gt = tf.cast(read_data[fields.InputDataFields.multiclass_scores], - tf.float32) - else: - classes_gt = util_ops.padded_one_hot_encoding( - indices=classes_gt, depth=num_classes, left_pad=0) - masks_gt = read_data.get(fields.InputDataFields.groundtruth_instance_masks) - keypoints_gt = read_data.get(fields.InputDataFields.groundtruth_keypoints) - if (merge_multiple_label_boxes and ( - masks_gt is not None or keypoints_gt is not None)): - raise NotImplementedError('Multi-label support is only for boxes.') - weights_gt = read_data.get( - fields.InputDataFields.groundtruth_weights) - return (image, key, location_gt, classes_gt, masks_gt, keypoints_gt, - weights_gt) - - return zip(*map(extract_images_and_targets, read_data_list)) - - -def _create_losses(input_queue, create_model_fn, train_config): - """Creates loss function for a DetectionModel. - - Args: - input_queue: BatchQueue object holding enqueued tensor_dicts. - create_model_fn: A function to create the DetectionModel. - train_config: a train_pb2.TrainConfig protobuf. - """ - detection_model = create_model_fn() - (images, _, groundtruth_boxes_list, groundtruth_classes_list, - groundtruth_masks_list, groundtruth_keypoints_list, - groundtruth_weights_list) = get_inputs( - input_queue, - detection_model.num_classes, - train_config.merge_multiple_label_boxes, - train_config.use_multiclass_scores) - - preprocessed_images = [] - true_image_shapes = [] - for image in images: - resized_image, true_image_shape = detection_model.preprocess(image) - preprocessed_images.append(resized_image) - true_image_shapes.append(true_image_shape) - - images = tf.concat(preprocessed_images, 0) - true_image_shapes = tf.concat(true_image_shapes, 0) - - if any(mask is None for mask in groundtruth_masks_list): - groundtruth_masks_list = None - if any(keypoints is None for keypoints in groundtruth_keypoints_list): - groundtruth_keypoints_list = None - - detection_model.provide_groundtruth( - groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_masks_list, - groundtruth_keypoints_list, - groundtruth_weights_list=groundtruth_weights_list) - prediction_dict = detection_model.predict(images, true_image_shapes) - - losses_dict = detection_model.loss(prediction_dict, true_image_shapes) - for loss_tensor in losses_dict.values(): - tf.losses.add_loss(loss_tensor) - - -def train(create_tensor_dict_fn, - create_model_fn, - train_config, - master, - task, - num_clones, - worker_replicas, - clone_on_cpu, - ps_tasks, - worker_job_name, - is_chief, - train_dir, - graph_hook_fn=None): - """Training function for detection models. - - Args: - create_tensor_dict_fn: a function to create a tensor input dictionary. - create_model_fn: a function that creates a DetectionModel and generates - losses. - train_config: a train_pb2.TrainConfig protobuf. - master: BNS name of the TensorFlow master to use. - task: The task id of this training instance. - num_clones: The number of clones to run per machine. - worker_replicas: The number of work replicas to train with. - clone_on_cpu: True if clones should be forced to run on CPU. - ps_tasks: Number of parameter server tasks. - worker_job_name: Name of the worker job. - is_chief: Whether this replica is the chief replica. - train_dir: Directory to write checkpoints and training summaries to. - graph_hook_fn: Optional function that is called after the inference graph is - built (before optimization). This is helpful to perform additional changes - to the training graph such as adding FakeQuant ops. The function should - modify the default graph. - - Raises: - ValueError: If both num_clones > 1 and train_config.sync_replicas is true. - """ - - detection_model = create_model_fn() - data_augmentation_options = [ - preprocessor_builder.build(step) - for step in train_config.data_augmentation_options] - - with tf.Graph().as_default(): - # Build a configuration specifying multi-GPU and multi-replicas. - deploy_config = model_deploy.DeploymentConfig( - num_clones=num_clones, - clone_on_cpu=clone_on_cpu, - replica_id=task, - num_replicas=worker_replicas, - num_ps_tasks=ps_tasks, - worker_job_name=worker_job_name) - - # Place the global step on the device storing the variables. - with tf.device(deploy_config.variables_device()): - global_step = slim.create_global_step() - - if num_clones != 1 and train_config.sync_replicas: - raise ValueError('In Synchronous SGD mode num_clones must ', - 'be 1. Found num_clones: {}'.format(num_clones)) - batch_size = train_config.batch_size // num_clones - if train_config.sync_replicas: - batch_size //= train_config.replicas_to_aggregate - - with tf.device(deploy_config.inputs_device()): - input_queue = create_input_queue( - batch_size, create_tensor_dict_fn, - train_config.batch_queue_capacity, - train_config.num_batch_queue_threads, - train_config.prefetch_queue_capacity, data_augmentation_options) - - # Gather initial summaries. - # TODO(rathodv): See if summaries can be added/extracted from global tf - # collections so that they don't have to be passed around. - summaries = set(tf.get_collection(tf.GraphKeys.SUMMARIES)) - global_summaries = set([]) - - model_fn = functools.partial(_create_losses, - create_model_fn=create_model_fn, - train_config=train_config) - clones = model_deploy.create_clones(deploy_config, model_fn, [input_queue]) - first_clone_scope = clones[0].scope - - if graph_hook_fn: - with tf.device(deploy_config.variables_device()): - graph_hook_fn() - - # Gather update_ops from the first clone. These contain, for example, - # the updates for the batch_norm variables created by model_fn. - update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS, first_clone_scope) - - with tf.device(deploy_config.optimizer_device()): - training_optimizer, optimizer_summary_vars = optimizer_builder.build( - train_config.optimizer) - for var in optimizer_summary_vars: - tf.summary.scalar(var.op.name, var, family='LearningRate') - - sync_optimizer = None - if train_config.sync_replicas: - training_optimizer = tf.train.SyncReplicasOptimizer( - training_optimizer, - replicas_to_aggregate=train_config.replicas_to_aggregate, - total_num_replicas=worker_replicas) - sync_optimizer = training_optimizer - - with tf.device(deploy_config.optimizer_device()): - regularization_losses = (None if train_config.add_regularization_loss - else []) - total_loss, grads_and_vars = model_deploy.optimize_clones( - clones, training_optimizer, - regularization_losses=regularization_losses) - total_loss = tf.check_numerics(total_loss, 'LossTensor is inf or nan.') - - # Optionally multiply bias gradients by train_config.bias_grad_multiplier. - if train_config.bias_grad_multiplier: - biases_regex_list = ['.*/biases'] - grads_and_vars = variables_helper.multiply_gradients_matching_regex( - grads_and_vars, - biases_regex_list, - multiplier=train_config.bias_grad_multiplier) - - # Optionally freeze some layers by setting their gradients to be zero. - if train_config.freeze_variables: - grads_and_vars = variables_helper.freeze_gradients_matching_regex( - grads_and_vars, train_config.freeze_variables) - - # Optionally clip gradients - if train_config.gradient_clipping_by_norm > 0: - with tf.name_scope('clip_grads'): - grads_and_vars = slim.learning.clip_gradient_norms( - grads_and_vars, train_config.gradient_clipping_by_norm) - - # Create gradient updates. - grad_updates = training_optimizer.apply_gradients(grads_and_vars, - global_step=global_step) - update_ops.append(grad_updates) - update_op = tf.group(*update_ops, name='update_barrier') - with tf.control_dependencies([update_op]): - train_tensor = tf.identity(total_loss, name='train_op') - - # Add summaries. - for model_var in slim.get_model_variables(): - global_summaries.add(tf.summary.histogram('ModelVars/' + - model_var.op.name, model_var)) - for loss_tensor in tf.losses.get_losses(): - global_summaries.add(tf.summary.scalar('Losses/' + loss_tensor.op.name, - loss_tensor)) - global_summaries.add( - tf.summary.scalar('Losses/TotalLoss', tf.losses.get_total_loss())) - - # Add the summaries from the first clone. These contain the summaries - # created by model_fn and either optimize_clones() or _gather_clone_loss(). - summaries |= set(tf.get_collection(tf.GraphKeys.SUMMARIES, - first_clone_scope)) - summaries |= global_summaries - - # Merge all summaries together. - summary_op = tf.summary.merge(list(summaries), name='summary_op') - - # Soft placement allows placing on CPU ops without GPU implementation. - session_config = tf.ConfigProto(allow_soft_placement=True, - log_device_placement=False) - - # Save checkpoints regularly. - keep_checkpoint_every_n_hours = train_config.keep_checkpoint_every_n_hours - saver = tf.train.Saver( - keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours) - - # Create ops required to initialize the model from a given checkpoint. - init_fn = None - if train_config.fine_tune_checkpoint: - if not train_config.fine_tune_checkpoint_type: - # train_config.from_detection_checkpoint field is deprecated. For - # backward compatibility, fine_tune_checkpoint_type is set based on - # from_detection_checkpoint. - if train_config.from_detection_checkpoint: - train_config.fine_tune_checkpoint_type = 'detection' - else: - train_config.fine_tune_checkpoint_type = 'classification' - var_map = detection_model.restore_map( - fine_tune_checkpoint_type=train_config.fine_tune_checkpoint_type, - load_all_detection_checkpoint_vars=( - train_config.load_all_detection_checkpoint_vars)) - available_var_map = (variables_helper. - get_variables_available_in_checkpoint( - var_map, train_config.fine_tune_checkpoint, - include_global_step=False)) - init_saver = tf.train.Saver(available_var_map) - def initializer_fn(sess): - init_saver.restore(sess, train_config.fine_tune_checkpoint) - init_fn = initializer_fn - - slim.learning.train( - train_tensor, - logdir=train_dir, - master=master, - is_chief=is_chief, - session_config=session_config, - startup_delay_steps=train_config.startup_delay_steps, - init_fn=init_fn, - summary_op=summary_op, - number_of_steps=( - train_config.num_steps if train_config.num_steps else None), - save_summaries_secs=120, - sync_optimizer=sync_optimizer, - saver=saver) diff --git a/research/object_detection/legacy/trainer_tf1_test.py b/research/object_detection/legacy/trainer_tf1_test.py deleted file mode 100644 index 0cde654e6a8..00000000000 --- a/research/object_detection/legacy/trainer_tf1_test.py +++ /dev/null @@ -1,295 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.trainer.""" -import unittest -import tensorflow.compat.v1 as tf -import tf_slim as slim -from google.protobuf import text_format - -from object_detection.core import losses -from object_detection.core import model -from object_detection.core import standard_fields as fields -from object_detection.legacy import trainer -from object_detection.protos import train_pb2 -from object_detection.utils import tf_version - - -NUMBER_OF_CLASSES = 2 - - -def get_input_function(): - """A function to get test inputs. Returns an image with one box.""" - image = tf.random_uniform([32, 32, 3], dtype=tf.float32) - key = tf.constant('image_000000') - class_label = tf.random_uniform( - [1], minval=0, maxval=NUMBER_OF_CLASSES, dtype=tf.int32) - box_label = tf.random_uniform( - [1, 4], minval=0.4, maxval=0.6, dtype=tf.float32) - multiclass_scores = tf.random_uniform( - [1, NUMBER_OF_CLASSES], minval=0.4, maxval=0.6, dtype=tf.float32) - - return { - fields.InputDataFields.image: image, - fields.InputDataFields.key: key, - fields.InputDataFields.groundtruth_classes: class_label, - fields.InputDataFields.groundtruth_boxes: box_label, - fields.InputDataFields.multiclass_scores: multiclass_scores - } - - -class FakeDetectionModel(model.DetectionModel): - """A simple (and poor) DetectionModel for use in test.""" - - def __init__(self): - super(FakeDetectionModel, self).__init__(num_classes=NUMBER_OF_CLASSES) - self._classification_loss = losses.WeightedSigmoidClassificationLoss() - self._localization_loss = losses.WeightedSmoothL1LocalizationLoss() - - def preprocess(self, inputs): - """Input preprocessing, resizes images to 28x28. - - Args: - inputs: a [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: a [batch, 28, 28, channels] float32 tensor. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - """ - true_image_shapes = [inputs.shape[:-1].as_list() - for _ in range(inputs.shape[-1])] - return tf.image.resize_images(inputs, [28, 28]), true_image_shapes - - def predict(self, preprocessed_inputs, true_image_shapes): - """Prediction tensors from inputs tensor. - - Args: - preprocessed_inputs: a [batch, 28, 28, channels] float32 tensor. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - prediction_dict: a dictionary holding prediction tensors to be - passed to the Loss or Postprocess functions. - """ - flattened_inputs = slim.flatten(preprocessed_inputs) - class_prediction = slim.fully_connected(flattened_inputs, self._num_classes) - box_prediction = slim.fully_connected(flattened_inputs, 4) - - return { - 'class_predictions_with_background': tf.reshape( - class_prediction, [-1, 1, self._num_classes]), - 'box_encodings': tf.reshape(box_prediction, [-1, 1, 4]) - } - - def postprocess(self, prediction_dict, true_image_shapes, **params): - """Convert predicted output tensors to final detections. Unused. - - Args: - prediction_dict: a dictionary holding prediction tensors. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - **params: Additional keyword arguments for specific implementations of - DetectionModel. - - Returns: - detections: a dictionary with empty fields. - """ - return { - 'detection_boxes': None, - 'detection_scores': None, - 'detection_classes': None, - 'num_detections': None - } - - def loss(self, prediction_dict, true_image_shapes): - """Compute scalar loss tensors with respect to provided groundtruth. - - Calling this function requires that groundtruth tensors have been - provided via the provide_groundtruth function. - - Args: - prediction_dict: a dictionary holding predicted tensors - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - a dictionary mapping strings (loss names) to scalar tensors representing - loss values. - """ - batch_reg_targets = tf.stack( - self.groundtruth_lists(fields.BoxListFields.boxes)) - batch_cls_targets = tf.stack( - self.groundtruth_lists(fields.BoxListFields.classes)) - weights = tf.constant( - 1.0, dtype=tf.float32, - shape=[len(self.groundtruth_lists(fields.BoxListFields.boxes)), 1]) - - location_losses = self._localization_loss( - prediction_dict['box_encodings'], batch_reg_targets, - weights=weights) - cls_losses = self._classification_loss( - prediction_dict['class_predictions_with_background'], batch_cls_targets, - weights=weights) - - loss_dict = { - 'localization_loss': tf.reduce_sum(location_losses), - 'classification_loss': tf.reduce_sum(cls_losses), - } - return loss_dict - - def regularization_losses(self): - """Returns a list of regularization losses for this model. - - Returns a list of regularization losses for this model that the estimator - needs to use during training/optimization. - - Returns: - A list of regularization loss tensors. - """ - pass - - def restore_map(self, fine_tune_checkpoint_type='detection'): - """Returns a map of variables to load from a foreign checkpoint. - - Args: - fine_tune_checkpoint_type: whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`. Default 'detection'. - - Returns: - A dict mapping variable names to variables. - """ - return {var.op.name: var for var in tf.global_variables()} - - def restore_from_objects(self, fine_tune_checkpoint_type): - pass - - def updates(self): - """Returns a list of update operators for this model. - - Returns a list of update operators for this model that must be executed at - each training step. The estimator's train op needs to have a control - dependency on these updates. - - Returns: - A list of update operators. - """ - pass - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class TrainerTest(tf.test.TestCase): - - def test_configure_trainer_and_train_two_steps(self): - train_config_text_proto = """ - optimizer { - adam_optimizer { - learning_rate { - constant_learning_rate { - learning_rate: 0.01 - } - } - } - } - data_augmentation_options { - random_adjust_brightness { - max_delta: 0.2 - } - } - data_augmentation_options { - random_adjust_contrast { - min_delta: 0.7 - max_delta: 1.1 - } - } - num_steps: 2 - """ - train_config = train_pb2.TrainConfig() - text_format.Merge(train_config_text_proto, train_config) - - train_dir = self.get_temp_dir() - - trainer.train( - create_tensor_dict_fn=get_input_function, - create_model_fn=FakeDetectionModel, - train_config=train_config, - master='', - task=0, - num_clones=1, - worker_replicas=1, - clone_on_cpu=True, - ps_tasks=0, - worker_job_name='worker', - is_chief=True, - train_dir=train_dir) - - def test_configure_trainer_with_multiclass_scores_and_train_two_steps(self): - train_config_text_proto = """ - optimizer { - adam_optimizer { - learning_rate { - constant_learning_rate { - learning_rate: 0.01 - } - } - } - } - data_augmentation_options { - random_adjust_brightness { - max_delta: 0.2 - } - } - data_augmentation_options { - random_adjust_contrast { - min_delta: 0.7 - max_delta: 1.1 - } - } - num_steps: 2 - use_multiclass_scores: true - """ - train_config = train_pb2.TrainConfig() - text_format.Merge(train_config_text_proto, train_config) - - train_dir = self.get_temp_dir() - - trainer.train(create_tensor_dict_fn=get_input_function, - create_model_fn=FakeDetectionModel, - train_config=train_config, - master='', - task=0, - num_clones=1, - worker_replicas=1, - clone_on_cpu=True, - ps_tasks=0, - worker_job_name='worker', - is_chief=True, - train_dir=train_dir) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/matchers/__init__.py b/research/object_detection/matchers/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/matchers/argmax_matcher.py b/research/object_detection/matchers/argmax_matcher.py deleted file mode 100644 index a347decbd3c..00000000000 --- a/research/object_detection/matchers/argmax_matcher.py +++ /dev/null @@ -1,208 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Argmax matcher implementation. - -This class takes a similarity matrix and matches columns to rows based on the -maximum value per column. One can specify matched_thresholds and -to prevent columns from matching to rows (generally resulting in a negative -training example) and unmatched_theshold to ignore the match (generally -resulting in neither a positive or negative training example). - -This matcher is used in Fast(er)-RCNN. - -Note: matchers are used in TargetAssigners. There is a create_target_assigner -factory function for popular implementations. -""" -import tensorflow.compat.v1 as tf - -from object_detection.core import matcher -from object_detection.utils import shape_utils - - -class ArgMaxMatcher(matcher.Matcher): - """Matcher based on highest value. - - This class computes matches from a similarity matrix. Each column is matched - to a single row. - - To support object detection target assignment this class enables setting both - matched_threshold (upper threshold) and unmatched_threshold (lower thresholds) - defining three categories of similarity which define whether examples are - positive, negative, or ignored: - (1) similarity >= matched_threshold: Highest similarity. Matched/Positive! - (2) matched_threshold > similarity >= unmatched_threshold: Medium similarity. - Depending on negatives_lower_than_unmatched, this is either - Unmatched/Negative OR Ignore. - (3) unmatched_threshold > similarity: Lowest similarity. Depending on flag - negatives_lower_than_unmatched, either Unmatched/Negative OR Ignore. - For ignored matches this class sets the values in the Match object to -2. - """ - - def __init__(self, - matched_threshold, - unmatched_threshold=None, - negatives_lower_than_unmatched=True, - force_match_for_each_row=False, - use_matmul_gather=False): - """Construct ArgMaxMatcher. - - Args: - matched_threshold: Threshold for positive matches. Positive if - sim >= matched_threshold, where sim is the maximum value of the - similarity matrix for a given column. Set to None for no threshold. - unmatched_threshold: Threshold for negative matches. Negative if - sim < unmatched_threshold. Defaults to matched_threshold - when set to None. - negatives_lower_than_unmatched: Boolean which defaults to True. If True - then negative matches are the ones below the unmatched_threshold, - whereas ignored matches are in between the matched and umatched - threshold. If False, then negative matches are in between the matched - and unmatched threshold, and everything lower than unmatched is ignored. - force_match_for_each_row: If True, ensures that each row is matched to - at least one column (which is not guaranteed otherwise if the - matched_threshold is high). Defaults to False. See - argmax_matcher_test.testMatcherForceMatch() for an example. - use_matmul_gather: Force constructed match objects to use matrix - multiplication based gather instead of standard tf.gather. - (Default: False). - - Raises: - ValueError: if unmatched_threshold is set but matched_threshold is not set - or if unmatched_threshold > matched_threshold. - """ - super(ArgMaxMatcher, self).__init__(use_matmul_gather=use_matmul_gather) - if (matched_threshold is None) and (unmatched_threshold is not None): - raise ValueError('Need to also define matched_threshold when' - 'unmatched_threshold is defined') - self._matched_threshold = matched_threshold - if unmatched_threshold is None: - self._unmatched_threshold = matched_threshold - else: - if unmatched_threshold > matched_threshold: - raise ValueError('unmatched_threshold needs to be smaller or equal' - 'to matched_threshold') - self._unmatched_threshold = unmatched_threshold - if not negatives_lower_than_unmatched: - if self._unmatched_threshold == self._matched_threshold: - raise ValueError('When negatives are in between matched and ' - 'unmatched thresholds, these cannot be of equal ' - 'value. matched: {}, unmatched: {}'.format( - self._matched_threshold, - self._unmatched_threshold)) - self._force_match_for_each_row = force_match_for_each_row - self._negatives_lower_than_unmatched = negatives_lower_than_unmatched - - def _match(self, similarity_matrix, valid_rows): - """Tries to match each column of the similarity matrix to a row. - - Args: - similarity_matrix: tensor of shape [N, M] representing any similarity - metric. - valid_rows: a boolean tensor of shape [N] indicating valid rows. - - Returns: - Match object with corresponding matches for each of M columns. - """ - - def _match_when_rows_are_empty(): - """Performs matching when the rows of similarity matrix are empty. - - When the rows are empty, all detections are false positives. So we return - a tensor of -1's to indicate that the columns do not match to any rows. - - Returns: - matches: int32 tensor indicating the row each column matches to. - """ - similarity_matrix_shape = shape_utils.combined_static_and_dynamic_shape( - similarity_matrix) - return -1 * tf.ones([similarity_matrix_shape[1]], dtype=tf.int32) - - def _match_when_rows_are_non_empty(): - """Performs matching when the rows of similarity matrix are non empty. - - Returns: - matches: int32 tensor indicating the row each column matches to. - """ - # Matches for each column - matches = tf.argmax(similarity_matrix, 0, output_type=tf.int32) - - # Deal with matched and unmatched threshold - if self._matched_threshold is not None: - # Get logical indices of ignored and unmatched columns as tf.int64 - matched_vals = tf.reduce_max(similarity_matrix, 0) - below_unmatched_threshold = tf.greater(self._unmatched_threshold, - matched_vals) - between_thresholds = tf.logical_and( - tf.greater_equal(matched_vals, self._unmatched_threshold), - tf.greater(self._matched_threshold, matched_vals)) - - if self._negatives_lower_than_unmatched: - matches = self._set_values_using_indicator(matches, - below_unmatched_threshold, - -1) - matches = self._set_values_using_indicator(matches, - between_thresholds, - -2) - else: - matches = self._set_values_using_indicator(matches, - below_unmatched_threshold, - -2) - matches = self._set_values_using_indicator(matches, - between_thresholds, - -1) - - if self._force_match_for_each_row: - similarity_matrix_shape = shape_utils.combined_static_and_dynamic_shape( - similarity_matrix) - force_match_column_ids = tf.argmax(similarity_matrix, 1, - output_type=tf.int32) - force_match_column_indicators = ( - tf.one_hot( - force_match_column_ids, depth=similarity_matrix_shape[1]) * - tf.cast(tf.expand_dims(valid_rows, axis=-1), dtype=tf.float32)) - force_match_row_ids = tf.argmax(force_match_column_indicators, 0, - output_type=tf.int32) - force_match_column_mask = tf.cast( - tf.reduce_max(force_match_column_indicators, 0), tf.bool) - final_matches = tf.where(force_match_column_mask, - force_match_row_ids, matches) - return final_matches - else: - return matches - - if similarity_matrix.shape.is_fully_defined(): - if shape_utils.get_dim_as_int(similarity_matrix.shape[0]) == 0: - return _match_when_rows_are_empty() - else: - return _match_when_rows_are_non_empty() - else: - return tf.cond( - tf.greater(tf.shape(similarity_matrix)[0], 0), - _match_when_rows_are_non_empty, _match_when_rows_are_empty) - - def _set_values_using_indicator(self, x, indicator, val): - """Set the indicated fields of x to val. - - Args: - x: tensor. - indicator: boolean with same shape as x. - val: scalar with value to set. - - Returns: - modified tensor. - """ - indicator = tf.cast(indicator, x.dtype) - return tf.add(tf.multiply(x, 1 - indicator), val * indicator) diff --git a/research/object_detection/matchers/argmax_matcher_test.py b/research/object_detection/matchers/argmax_matcher_test.py deleted file mode 100644 index 9305f0a86c8..00000000000 --- a/research/object_detection/matchers/argmax_matcher_test.py +++ /dev/null @@ -1,235 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.matchers.argmax_matcher.""" - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.matchers import argmax_matcher -from object_detection.utils import test_case - - -class ArgMaxMatcherTest(test_case.TestCase): - - def test_return_correct_matches_with_default_thresholds(self): - - def graph_fn(similarity_matrix): - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=None) - match = matcher.match(similarity_matrix) - matched_cols = match.matched_column_indicator() - unmatched_cols = match.unmatched_column_indicator() - match_results = match.match_results - return (matched_cols, unmatched_cols, match_results) - - similarity = np.array([[1., 1, 1, 3, 1], - [2, -1, 2, 0, 4], - [3, 0, -1, 0, 0]], dtype=np.float32) - expected_matched_rows = np.array([2, 0, 1, 0, 1]) - (res_matched_cols, res_unmatched_cols, - res_match_results) = self.execute(graph_fn, [similarity]) - - self.assertAllEqual(res_match_results[res_matched_cols], - expected_matched_rows) - self.assertAllEqual(np.nonzero(res_matched_cols)[0], [0, 1, 2, 3, 4]) - self.assertFalse(np.all(res_unmatched_cols)) - - def test_return_correct_matches_with_empty_rows(self): - - def graph_fn(similarity_matrix): - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=None) - match = matcher.match(similarity_matrix) - return match.unmatched_column_indicator() - similarity = 0.2 * np.ones([0, 5], dtype=np.float32) - res_unmatched_cols = self.execute(graph_fn, [similarity]) - self.assertAllEqual(np.nonzero(res_unmatched_cols)[0], np.arange(5)) - - def test_return_correct_matches_with_matched_threshold(self): - - def graph_fn(similarity): - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=3.) - match = matcher.match(similarity) - matched_cols = match.matched_column_indicator() - unmatched_cols = match.unmatched_column_indicator() - match_results = match.match_results - return (matched_cols, unmatched_cols, match_results) - - similarity = np.array([[1, 1, 1, 3, 1], - [2, -1, 2, 0, 4], - [3, 0, -1, 0, 0]], dtype=np.float32) - expected_matched_cols = np.array([0, 3, 4]) - expected_matched_rows = np.array([2, 0, 1]) - expected_unmatched_cols = np.array([1, 2]) - - (res_matched_cols, res_unmatched_cols, - match_results) = self.execute(graph_fn, [similarity]) - self.assertAllEqual(match_results[res_matched_cols], expected_matched_rows) - self.assertAllEqual(np.nonzero(res_matched_cols)[0], expected_matched_cols) - self.assertAllEqual(np.nonzero(res_unmatched_cols)[0], - expected_unmatched_cols) - - def test_return_correct_matches_with_matched_and_unmatched_threshold(self): - - def graph_fn(similarity): - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=3., - unmatched_threshold=2.) - match = matcher.match(similarity) - matched_cols = match.matched_column_indicator() - unmatched_cols = match.unmatched_column_indicator() - match_results = match.match_results - return (matched_cols, unmatched_cols, match_results) - - similarity = np.array([[1, 1, 1, 3, 1], - [2, -1, 2, 0, 4], - [3, 0, -1, 0, 0]], dtype=np.float32) - expected_matched_cols = np.array([0, 3, 4]) - expected_matched_rows = np.array([2, 0, 1]) - expected_unmatched_cols = np.array([1]) # col 2 has too high maximum val - - (res_matched_cols, res_unmatched_cols, - match_results) = self.execute(graph_fn, [similarity]) - self.assertAllEqual(match_results[res_matched_cols], expected_matched_rows) - self.assertAllEqual(np.nonzero(res_matched_cols)[0], expected_matched_cols) - self.assertAllEqual(np.nonzero(res_unmatched_cols)[0], - expected_unmatched_cols) - - def test_return_correct_matches_negatives_lower_than_unmatched_false(self): - - def graph_fn(similarity): - matcher = argmax_matcher.ArgMaxMatcher( - matched_threshold=3., - unmatched_threshold=2., - negatives_lower_than_unmatched=False) - match = matcher.match(similarity) - matched_cols = match.matched_column_indicator() - unmatched_cols = match.unmatched_column_indicator() - match_results = match.match_results - return (matched_cols, unmatched_cols, match_results) - - similarity = np.array([[1, 1, 1, 3, 1], - [2, -1, 2, 0, 4], - [3, 0, -1, 0, 0]], dtype=np.float32) - expected_matched_cols = np.array([0, 3, 4]) - expected_matched_rows = np.array([2, 0, 1]) - expected_unmatched_cols = np.array([2]) # col 1 has too low maximum val - - (res_matched_cols, res_unmatched_cols, - match_results) = self.execute(graph_fn, [similarity]) - self.assertAllEqual(match_results[res_matched_cols], expected_matched_rows) - self.assertAllEqual(np.nonzero(res_matched_cols)[0], expected_matched_cols) - self.assertAllEqual(np.nonzero(res_unmatched_cols)[0], - expected_unmatched_cols) - - def test_return_correct_matches_unmatched_row_not_using_force_match(self): - - def graph_fn(similarity): - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=3., - unmatched_threshold=2.) - match = matcher.match(similarity) - matched_cols = match.matched_column_indicator() - unmatched_cols = match.unmatched_column_indicator() - match_results = match.match_results - return (matched_cols, unmatched_cols, match_results) - - similarity = np.array([[1, 1, 1, 3, 1], - [-1, 0, -2, -2, -1], - [3, 0, -1, 2, 0]], dtype=np.float32) - expected_matched_cols = np.array([0, 3]) - expected_matched_rows = np.array([2, 0]) - expected_unmatched_cols = np.array([1, 2, 4]) - - (res_matched_cols, res_unmatched_cols, - match_results) = self.execute(graph_fn, [similarity]) - self.assertAllEqual(match_results[res_matched_cols], expected_matched_rows) - self.assertAllEqual(np.nonzero(res_matched_cols)[0], expected_matched_cols) - self.assertAllEqual(np.nonzero(res_unmatched_cols)[0], - expected_unmatched_cols) - - def test_return_correct_matches_unmatched_row_while_using_force_match(self): - def graph_fn(similarity): - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=3., - unmatched_threshold=2., - force_match_for_each_row=True) - match = matcher.match(similarity) - matched_cols = match.matched_column_indicator() - unmatched_cols = match.unmatched_column_indicator() - match_results = match.match_results - return (matched_cols, unmatched_cols, match_results) - - similarity = np.array([[1, 1, 1, 3, 1], - [-1, 0, -2, -2, -1], - [3, 0, -1, 2, 0]], dtype=np.float32) - expected_matched_cols = np.array([0, 1, 3]) - expected_matched_rows = np.array([2, 1, 0]) - expected_unmatched_cols = np.array([2, 4]) # col 2 has too high max val - - (res_matched_cols, res_unmatched_cols, - match_results) = self.execute(graph_fn, [similarity]) - self.assertAllEqual(match_results[res_matched_cols], expected_matched_rows) - self.assertAllEqual(np.nonzero(res_matched_cols)[0], expected_matched_cols) - self.assertAllEqual(np.nonzero(res_unmatched_cols)[0], - expected_unmatched_cols) - - def test_return_correct_matches_using_force_match_padded_groundtruth(self): - def graph_fn(similarity, valid_rows): - matcher = argmax_matcher.ArgMaxMatcher(matched_threshold=3., - unmatched_threshold=2., - force_match_for_each_row=True) - match = matcher.match(similarity, valid_rows) - matched_cols = match.matched_column_indicator() - unmatched_cols = match.unmatched_column_indicator() - match_results = match.match_results - return (matched_cols, unmatched_cols, match_results) - - similarity = np.array([[1, 1, 1, 3, 1], - [-1, 0, -2, -2, -1], - [0, 0, 0, 0, 0], - [3, 0, -1, 2, 0], - [0, 0, 0, 0, 0]], dtype=np.float32) - valid_rows = np.array([True, True, False, True, False]) - expected_matched_cols = np.array([0, 1, 3]) - expected_matched_rows = np.array([3, 1, 0]) - expected_unmatched_cols = np.array([2, 4]) # col 2 has too high max val - - (res_matched_cols, res_unmatched_cols, - match_results) = self.execute(graph_fn, [similarity, valid_rows]) - self.assertAllEqual(match_results[res_matched_cols], expected_matched_rows) - self.assertAllEqual(np.nonzero(res_matched_cols)[0], expected_matched_cols) - self.assertAllEqual(np.nonzero(res_unmatched_cols)[0], - expected_unmatched_cols) - - def test_valid_arguments_corner_case(self): - argmax_matcher.ArgMaxMatcher(matched_threshold=1, - unmatched_threshold=1) - - def test_invalid_arguments_corner_case_negatives_lower_than_thres_false(self): - with self.assertRaises(ValueError): - argmax_matcher.ArgMaxMatcher(matched_threshold=1, - unmatched_threshold=1, - negatives_lower_than_unmatched=False) - - def test_invalid_arguments_no_matched_threshold(self): - with self.assertRaises(ValueError): - argmax_matcher.ArgMaxMatcher(matched_threshold=None, - unmatched_threshold=4) - - def test_invalid_arguments_unmatched_thres_larger_than_matched_thres(self): - with self.assertRaises(ValueError): - argmax_matcher.ArgMaxMatcher(matched_threshold=1, - unmatched_threshold=2) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/matchers/bipartite_matcher.py b/research/object_detection/matchers/bipartite_matcher.py deleted file mode 100644 index f62afe0975f..00000000000 --- a/research/object_detection/matchers/bipartite_matcher.py +++ /dev/null @@ -1,70 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Bipartite matcher implementation.""" - -import tensorflow.compat.v1 as tf - -from tensorflow.contrib.image.python.ops import image_ops -from object_detection.core import matcher - - -class GreedyBipartiteMatcher(matcher.Matcher): - """Wraps a Tensorflow greedy bipartite matcher.""" - - def __init__(self, use_matmul_gather=False): - """Constructs a Matcher. - - Args: - use_matmul_gather: Force constructed match objects to use matrix - multiplication based gather instead of standard tf.gather. - (Default: False). - """ - super(GreedyBipartiteMatcher, self).__init__( - use_matmul_gather=use_matmul_gather) - - def _match(self, similarity_matrix, valid_rows): - """Bipartite matches a collection rows and columns. A greedy bi-partite. - - TODO(rathodv): Add num_valid_columns options to match only that many columns - with all the rows. - - Args: - similarity_matrix: Float tensor of shape [N, M] with pairwise similarity - where higher values mean more similar. - valid_rows: A boolean tensor of shape [N] indicating the rows that are - valid. - - Returns: - match_results: int32 tensor of shape [M] with match_results[i]=-1 - meaning that column i is not matched and otherwise that it is matched to - row match_results[i]. - """ - valid_row_sim_matrix = tf.gather(similarity_matrix, - tf.squeeze(tf.where(valid_rows), axis=-1)) - invalid_row_sim_matrix = tf.gather( - similarity_matrix, - tf.squeeze(tf.where(tf.logical_not(valid_rows)), axis=-1)) - similarity_matrix = tf.concat( - [valid_row_sim_matrix, invalid_row_sim_matrix], axis=0) - # Convert similarity matrix to distance matrix as tf.image.bipartite tries - # to find minimum distance matches. - distance_matrix = -1 * similarity_matrix - num_valid_rows = tf.reduce_sum(tf.cast(valid_rows, dtype=tf.float32)) - _, match_results = image_ops.bipartite_match( - distance_matrix, num_valid_rows=num_valid_rows) - match_results = tf.reshape(match_results, [-1]) - match_results = tf.cast(match_results, tf.int32) - return match_results diff --git a/research/object_detection/matchers/bipartite_matcher_tf1_test.py b/research/object_detection/matchers/bipartite_matcher_tf1_test.py deleted file mode 100644 index d9b72f54cf0..00000000000 --- a/research/object_detection/matchers/bipartite_matcher_tf1_test.py +++ /dev/null @@ -1,92 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.bipartite_matcher.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.utils import test_case -from object_detection.utils import tf_version - -if tf_version.is_tf1(): - from object_detection.matchers import bipartite_matcher # pylint: disable=g-import-not-at-top - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class GreedyBipartiteMatcherTest(test_case.TestCase): - - def test_get_expected_matches_when_all_rows_are_valid(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - valid_rows = np.ones([2], dtype=bool) - expected_match_results = [-1, 1, 0] - def graph_fn(similarity_matrix, valid_rows): - matcher = bipartite_matcher.GreedyBipartiteMatcher() - match = matcher.match(similarity_matrix, valid_rows=valid_rows) - return match._match_results - match_results_out = self.execute(graph_fn, [similarity_matrix, valid_rows]) - self.assertAllEqual(match_results_out, expected_match_results) - - def test_get_expected_matches_with_all_rows_be_default(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - expected_match_results = [-1, 1, 0] - def graph_fn(similarity_matrix): - matcher = bipartite_matcher.GreedyBipartiteMatcher() - match = matcher.match(similarity_matrix) - return match._match_results - match_results_out = self.execute(graph_fn, [similarity_matrix]) - self.assertAllEqual(match_results_out, expected_match_results) - - def test_get_no_matches_with_zero_valid_rows(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - valid_rows = np.zeros([2], dtype=bool) - expected_match_results = [-1, -1, -1] - def graph_fn(similarity_matrix, valid_rows): - matcher = bipartite_matcher.GreedyBipartiteMatcher() - match = matcher.match(similarity_matrix, valid_rows=valid_rows) - return match._match_results - match_results_out = self.execute(graph_fn, [similarity_matrix, valid_rows]) - self.assertAllEqual(match_results_out, expected_match_results) - - def test_get_expected_matches_with_only_one_valid_row(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - valid_rows = np.array([True, False], dtype=bool) - expected_match_results = [-1, -1, 0] - def graph_fn(similarity_matrix, valid_rows): - matcher = bipartite_matcher.GreedyBipartiteMatcher() - match = matcher.match(similarity_matrix, valid_rows=valid_rows) - return match._match_results - match_results_out = self.execute(graph_fn, [similarity_matrix, valid_rows]) - self.assertAllEqual(match_results_out, expected_match_results) - - def test_get_expected_matches_with_only_one_valid_row_at_bottom(self): - similarity_matrix = np.array([[0.15, 0.2, 0.3], [0.50, 0.1, 0.8]], - dtype=np.float32) - valid_rows = np.array([False, True], dtype=bool) - expected_match_results = [-1, -1, 0] - def graph_fn(similarity_matrix, valid_rows): - matcher = bipartite_matcher.GreedyBipartiteMatcher() - match = matcher.match(similarity_matrix, valid_rows=valid_rows) - return match._match_results - match_results_out = self.execute(graph_fn, [similarity_matrix, valid_rows]) - self.assertAllEqual(match_results_out, expected_match_results) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/matchers/hungarian_matcher.py b/research/object_detection/matchers/hungarian_matcher.py deleted file mode 100644 index 63ee5d9f228..00000000000 --- a/research/object_detection/matchers/hungarian_matcher.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Hungarian bipartite matcher implementation.""" - -import numpy as np -from scipy.optimize import linear_sum_assignment - -import tensorflow.compat.v1 as tf -from object_detection.core import matcher - - -class HungarianBipartiteMatcher(matcher.Matcher): - """Wraps a Hungarian bipartite matcher into TensorFlow.""" - - def _match(self, similarity_matrix, valid_rows): - """Optimally bipartite matches a collection rows and columns. - - Args: - similarity_matrix: Float tensor of shape [N, M] with pairwise similarity - where higher values mean more similar. - valid_rows: A boolean tensor of shape [N] indicating the rows that are - valid. - - Returns: - match_results: int32 tensor of shape [M] with match_results[i]=-1 - meaning that column i is not matched and otherwise that it is matched to - row match_results[i]. - """ - valid_row_sim_matrix = tf.gather(similarity_matrix, - tf.squeeze(tf.where(valid_rows), axis=-1)) - distance_matrix = -1 * valid_row_sim_matrix - - def numpy_wrapper(inputs): - def numpy_matching(input_matrix): - row_indices, col_indices = linear_sum_assignment(input_matrix) - match_results = np.full(input_matrix.shape[1], -1) - match_results[col_indices] = row_indices - return match_results.astype(np.int32) - - return tf.numpy_function(numpy_matching, inputs, Tout=[tf.int32]) - - matching_result = tf.autograph.experimental.do_not_convert( - numpy_wrapper)([distance_matrix]) - - return tf.reshape(matching_result, [-1]) diff --git a/research/object_detection/matchers/hungarian_matcher_tf2_test.py b/research/object_detection/matchers/hungarian_matcher_tf2_test.py deleted file mode 100644 index c981bfb7c30..00000000000 --- a/research/object_detection/matchers/hungarian_matcher_tf2_test.py +++ /dev/null @@ -1,105 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.core.bipartite_matcher.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.utils import test_case -from object_detection.utils import tf_version - -if tf_version.is_tf2(): - from object_detection.matchers import hungarian_matcher # pylint: disable=g-import-not-at-top - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class HungarianBipartiteMatcherTest(test_case.TestCase): - - def test_get_expected_matches_when_all_rows_are_valid(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - valid_rows = np.ones([2], dtype=bool) - expected_match_results = [-1, 1, 0] - - matcher = hungarian_matcher.HungarianBipartiteMatcher() - match_results_out = matcher.match(similarity_matrix, valid_rows=valid_rows) - - self.assertAllEqual(match_results_out._match_results.numpy(), - expected_match_results) - - def test_get_expected_matches_with_all_rows_be_default(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - expected_match_results = [-1, 1, 0] - - matcher = hungarian_matcher.HungarianBipartiteMatcher() - match_results_out = matcher.match(similarity_matrix) - - self.assertAllEqual(match_results_out._match_results.numpy(), - expected_match_results) - - def test_get_no_matches_with_zero_valid_rows(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - valid_rows = np.zeros([2], dtype=bool) - expected_match_results = [-1, -1, -1] - - matcher = hungarian_matcher.HungarianBipartiteMatcher() - match_results_out = matcher.match(similarity_matrix, valid_rows=valid_rows) - - self.assertAllEqual(match_results_out._match_results.numpy(), - expected_match_results) - - def test_get_expected_matches_with_only_one_valid_row(self): - similarity_matrix = np.array([[0.50, 0.1, 0.8], [0.15, 0.2, 0.3]], - dtype=np.float32) - valid_rows = np.array([True, False], dtype=bool) - expected_match_results = [-1, -1, 0] - - matcher = hungarian_matcher.HungarianBipartiteMatcher() - match_results_out = matcher.match(similarity_matrix, valid_rows=valid_rows) - - self.assertAllEqual(match_results_out._match_results.numpy(), - expected_match_results) - - def test_get_expected_matches_with_only_one_valid_row_at_bottom(self): - similarity_matrix = np.array([[0.15, 0.2, 0.3], [0.50, 0.1, 0.8]], - dtype=np.float32) - valid_rows = np.array([False, True], dtype=bool) - expected_match_results = [-1, -1, 0] - - matcher = hungarian_matcher.HungarianBipartiteMatcher() - match_results_out = matcher.match(similarity_matrix, valid_rows=valid_rows) - - self.assertAllEqual(match_results_out._match_results.numpy(), - expected_match_results) - - def test_get_expected_matches_with_two_valid_rows(self): - similarity_matrix = np.array([[0.15, 0.2, 0.3], [0.50, 0.1, 0.8], - [0.84, 0.32, 0.2]], - dtype=np.float32) - valid_rows = np.array([True, False, True], dtype=bool) - expected_match_results = [1, -1, 0] - - matcher = hungarian_matcher.HungarianBipartiteMatcher() - match_results_out = matcher.match(similarity_matrix, valid_rows=valid_rows) - - self.assertAllEqual(match_results_out._match_results.numpy(), - expected_match_results) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/__init__.py b/research/object_detection/meta_architectures/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/meta_architectures/center_net_meta_arch.py b/research/object_detection/meta_architectures/center_net_meta_arch.py deleted file mode 100644 index 2f3b1c0761a..00000000000 --- a/research/object_detection/meta_architectures/center_net_meta_arch.py +++ /dev/null @@ -1,4856 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""The CenterNet meta architecture as described in the "Objects as Points" paper [1]. - -[1]: https://arxiv.org/abs/1904.07850 - -""" - -import abc -import collections -import functools -import tensorflow.compat.v1 as tf -import tensorflow.compat.v2 as tf2 - -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import keypoint_ops -from object_detection.core import model -from object_detection.core import standard_fields as fields -from object_detection.core import target_assigner as cn_assigner -from object_detection.utils import shape_utils -from object_detection.utils import target_assigner_utils as ta_utils -from object_detection.utils import tf_version - - -# Number of channels needed to predict size and offsets. -NUM_OFFSET_CHANNELS = 2 -NUM_SIZE_CHANNELS = 2 - -# Error range for detecting peaks. -PEAK_EPSILON = 1e-6 - - -class CenterNetFeatureExtractor(tf.keras.Model): - """Base class for feature extractors for the CenterNet meta architecture. - - Child classes are expected to override the _output_model property which will - return 1 or more tensors predicted by the feature extractor. - - """ - __metaclass__ = abc.ABCMeta - - def __init__(self, name=None, channel_means=(0., 0., 0.), - channel_stds=(1., 1., 1.), bgr_ordering=False): - """Initializes a CenterNet feature extractor. - - Args: - name: str, the name used for the underlying keras model. - channel_means: A tuple of floats, denoting the mean of each channel - which will be subtracted from it. If None or empty, we use 0s. - channel_stds: A tuple of floats, denoting the standard deviation of each - channel. Each channel will be divided by its standard deviation value. - If None or empty, we use 1s. - bgr_ordering: bool, if set will change the channel ordering to be in the - [blue, red, green] order. - """ - super(CenterNetFeatureExtractor, self).__init__(name=name) - - if channel_means is None or len(channel_means) == 0: # pylint:disable=g-explicit-length-test - channel_means = [0., 0., 0.] - - if channel_stds is None or len(channel_stds) == 0: # pylint:disable=g-explicit-length-test - channel_stds = [1., 1., 1.] - - self._channel_means = channel_means - self._channel_stds = channel_stds - self._bgr_ordering = bgr_ordering - - def preprocess(self, inputs): - """Converts a batch of unscaled images to a scale suitable for the model. - - This method normalizes the image using the given `channel_means` and - `channels_stds` values at initialization time while optionally flipping - the channel order if `bgr_ordering` is set. - - Args: - inputs: a [batch, height, width, channels] float32 tensor - - Returns: - outputs: a [batch, height, width, channels] float32 tensor - - """ - - if self._bgr_ordering: - red, green, blue = tf.unstack(inputs, axis=3) - inputs = tf.stack([blue, green, red], axis=3) - - channel_means = tf.reshape(tf.constant(self._channel_means), - [1, 1, 1, -1]) - channel_stds = tf.reshape(tf.constant(self._channel_stds), - [1, 1, 1, -1]) - - return (inputs - channel_means)/channel_stds - - def preprocess_reverse(self, preprocessed_inputs): - """Undo the preprocessing and return the raw image. - - This is a convenience function for some algorithms that require access - to the raw inputs. - - Args: - preprocessed_inputs: A [batch_size, height, width, channels] float - tensor preprocessed_inputs from the preprocess function. - - Returns: - images: A [batch_size, height, width, channels] float tensor with - the preprocessing removed. - """ - channel_means = tf.reshape(tf.constant(self._channel_means), - [1, 1, 1, -1]) - channel_stds = tf.reshape(tf.constant(self._channel_stds), - [1, 1, 1, -1]) - inputs = (preprocessed_inputs * channel_stds) + channel_means - - if self._bgr_ordering: - blue, green, red = tf.unstack(inputs, axis=3) - inputs = tf.stack([red, green, blue], axis=3) - - return inputs - - @property - @abc.abstractmethod - def out_stride(self): - """The stride in the output image of the network.""" - pass - - @property - @abc.abstractmethod - def num_feature_outputs(self): - """Ther number of feature outputs returned by the feature extractor.""" - pass - - @property - def classification_backbone(self): - raise NotImplementedError( - 'Classification backbone not supported for {}'.format(type(self))) - - -def make_prediction_net(num_out_channels, kernel_sizes=(3), num_filters=(256), - bias_fill=None, use_depthwise=False, name=None, - unit_height_conv=True): - """Creates a network to predict the given number of output channels. - - This function is intended to make the prediction heads for the CenterNet - meta architecture. - - Args: - num_out_channels: Number of output channels. - kernel_sizes: A list representing the sizes of the conv kernel in the - intermediate layer. Note that the length of the list indicates the number - of intermediate conv layers and it must be the same as the length of the - num_filters. - num_filters: A list representing the number of filters in the intermediate - conv layer. Note that the length of the list indicates the number of - intermediate conv layers. - bias_fill: If not None, is used to initialize the bias in the final conv - layer. - use_depthwise: If true, use SeparableConv2D to construct the Sequential - layers instead of Conv2D. - name: Optional name for the prediction net. - unit_height_conv: If True, Conv2Ds have asymmetric kernels with height=1. - - Returns: - net: A keras module which when called on an input tensor of size - [batch_size, height, width, num_in_channels] returns an output - of size [batch_size, height, width, num_out_channels] - """ - if isinstance(kernel_sizes, int) and isinstance(num_filters, int): - kernel_sizes = [kernel_sizes] - num_filters = [num_filters] - assert len(kernel_sizes) == len(num_filters) - if use_depthwise: - conv_fn = tf.keras.layers.SeparableConv2D - else: - conv_fn = tf.keras.layers.Conv2D - - # We name the convolution operations explicitly because Keras, by default, - # uses different names during training and evaluation. By setting the names - # here, we avoid unexpected pipeline breakage in TF1. - out_conv = tf.keras.layers.Conv2D( - num_out_channels, - kernel_size=1, - name='conv1' if tf_version.is_tf1() else None) - - if bias_fill is not None: - out_conv.bias_initializer = tf.keras.initializers.constant(bias_fill) - - layers = [] - for idx, (kernel_size, - num_filter) in enumerate(zip(kernel_sizes, num_filters)): - layers.append( - conv_fn( - num_filter, - kernel_size=[1, kernel_size] if unit_height_conv else kernel_size, - padding='same', - name='conv2_%d' % idx if tf_version.is_tf1() else None)) - layers.append(tf.keras.layers.ReLU()) - layers.append(out_conv) - net = tf.keras.Sequential(layers, name=name) - return net - - -def _to_float32(x): - return tf.cast(x, tf.float32) - - -def _get_shape(tensor, num_dims): - assert len(tensor.shape.as_list()) == num_dims - return shape_utils.combined_static_and_dynamic_shape(tensor) - - -def _flatten_spatial_dimensions(batch_images): - batch_size, height, width, channels = _get_shape(batch_images, 4) - return tf.reshape(batch_images, [batch_size, height * width, - channels]) - - -def _multi_range(limit, - value_repetitions=1, - range_repetitions=1, - dtype=tf.int32): - """Creates a sequence with optional value duplication and range repetition. - - As an example (see the Args section for more details), - _multi_range(limit=2, value_repetitions=3, range_repetitions=4) returns: - - [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1] - - Args: - limit: A 0-D Tensor (scalar). Upper limit of sequence, exclusive. - value_repetitions: Integer. The number of times a value in the sequence is - repeated. With value_repetitions=3, the result is [0, 0, 0, 1, 1, 1, ..]. - range_repetitions: Integer. The number of times the range is repeated. With - range_repetitions=3, the result is [0, 1, 2, .., 0, 1, 2, ..]. - dtype: The type of the elements of the resulting tensor. - - Returns: - A 1-D tensor of type `dtype` and size - [`limit` * `value_repetitions` * `range_repetitions`] that contains the - specified range with given repetitions. - """ - return tf.reshape( - tf.tile( - tf.expand_dims(tf.range(limit, dtype=dtype), axis=-1), - multiples=[range_repetitions, value_repetitions]), [-1]) - - -def top_k_feature_map_locations(feature_map, max_pool_kernel_size=3, k=100, - per_channel=False): - """Returns the top k scores and their locations in a feature map. - - Given a feature map, the top k values (based on activation) are returned. If - `per_channel` is True, the top k values **per channel** are returned. Note - that when k equals to 1, ths function uses reduce_max and argmax instead of - top_k to make the logics more efficient. - - The `max_pool_kernel_size` argument allows for selecting local peaks in a - region. This filtering is done per channel, so nothing prevents two values at - the same location to be returned. - - Args: - feature_map: [batch, height, width, channels] float32 feature map. - max_pool_kernel_size: integer, the max pool kernel size to use to pull off - peak score locations in a neighborhood (independently for each channel). - For example, to make sure no two neighboring values (in the same channel) - are returned, set max_pool_kernel_size=3. If None or 1, will not apply max - pooling. - k: The number of highest scoring locations to return. - per_channel: If True, will return the top k scores and locations per - feature map channel. If False, the top k across the entire feature map - (height x width x channels) are returned. - - Returns: - Tuple of - scores: A [batch, N] float32 tensor with scores from the feature map in - descending order. If per_channel is False, N = k. Otherwise, - N = k * channels, and the first k elements correspond to channel 0, the - second k correspond to channel 1, etc. - y_indices: A [batch, N] int tensor with y indices of the top k feature map - locations. If per_channel is False, N = k. Otherwise, - N = k * channels. - x_indices: A [batch, N] int tensor with x indices of the top k feature map - locations. If per_channel is False, N = k. Otherwise, - N = k * channels. - channel_indices: A [batch, N] int tensor with channel indices of the top k - feature map locations. If per_channel is False, N = k. Otherwise, - N = k * channels. - """ - if not max_pool_kernel_size or max_pool_kernel_size == 1: - feature_map_peaks = feature_map - else: - feature_map_max_pool = tf.nn.max_pool( - feature_map, ksize=max_pool_kernel_size, strides=1, padding='SAME') - - feature_map_peak_mask = tf.math.abs( - feature_map - feature_map_max_pool) < PEAK_EPSILON - - # Zero out everything that is not a peak. - feature_map_peaks = ( - feature_map * _to_float32(feature_map_peak_mask)) - - batch_size, _, width, num_channels = _get_shape(feature_map, 4) - - if per_channel: - if k == 1: - feature_map_flattened = tf.reshape( - feature_map_peaks, [batch_size, -1, num_channels]) - scores = tf.math.reduce_max(feature_map_flattened, axis=1) - peak_flat_indices = tf.math.argmax( - feature_map_flattened, axis=1, output_type=tf.dtypes.int32) - peak_flat_indices = tf.expand_dims(peak_flat_indices, axis=-1) - else: - # Perform top k over batch and channels. - feature_map_peaks_transposed = tf.transpose(feature_map_peaks, - perm=[0, 3, 1, 2]) - feature_map_peaks_transposed = tf.reshape( - feature_map_peaks_transposed, [batch_size, num_channels, -1]) - # safe_k will be used whenever there are fewer positions in the heatmap - # than the requested number of locations to score. In that case, all - # positions are returned in sorted order. To ensure consistent shapes for - # downstream ops the outputs are padded with zeros. Safe_k is also - # fine for TPU because TPUs require a fixed input size so the number of - # positions will also be fixed. - safe_k = tf.minimum(k, tf.shape(feature_map_peaks_transposed)[-1]) - scores, peak_flat_indices = tf.math.top_k( - feature_map_peaks_transposed, k=safe_k) - scores = tf.pad(scores, [(0, 0), (0, 0), (0, k - safe_k)]) - peak_flat_indices = tf.pad(peak_flat_indices, - [(0, 0), (0, 0), (0, k - safe_k)]) - scores = tf.ensure_shape(scores, (batch_size, num_channels, k)) - peak_flat_indices = tf.ensure_shape(peak_flat_indices, - (batch_size, num_channels, k)) - # Convert the indices such that they represent the location in the full - # (flattened) feature map of size [batch, height * width * channels]. - channel_idx = tf.range(num_channels)[tf.newaxis, :, tf.newaxis] - peak_flat_indices = num_channels * peak_flat_indices + channel_idx - scores = tf.reshape(scores, [batch_size, -1]) - peak_flat_indices = tf.reshape(peak_flat_indices, [batch_size, -1]) - else: - if k == 1: - feature_map_peaks_flat = tf.reshape(feature_map_peaks, [batch_size, -1]) - scores = tf.math.reduce_max(feature_map_peaks_flat, axis=1, keepdims=True) - peak_flat_indices = tf.expand_dims(tf.math.argmax( - feature_map_peaks_flat, axis=1, output_type=tf.dtypes.int32), axis=-1) - else: - feature_map_peaks_flat = tf.reshape(feature_map_peaks, [batch_size, -1]) - safe_k = tf.minimum(k, tf.shape(feature_map_peaks_flat)[1]) - scores, peak_flat_indices = tf.math.top_k(feature_map_peaks_flat, - k=safe_k) - - # Get x, y and channel indices corresponding to the top indices in the flat - # array. - y_indices, x_indices, channel_indices = ( - row_col_channel_indices_from_flattened_indices( - peak_flat_indices, width, num_channels)) - return scores, y_indices, x_indices, channel_indices - - -def prediction_tensors_to_boxes(y_indices, x_indices, height_width_predictions, - offset_predictions): - """Converts CenterNet class-center, offset and size predictions to boxes. - - Args: - y_indices: A [batch, num_boxes] int32 tensor with y indices corresponding to - object center locations (expressed in output coordinate frame). - x_indices: A [batch, num_boxes] int32 tensor with x indices corresponding to - object center locations (expressed in output coordinate frame). - height_width_predictions: A float tensor of shape [batch_size, height, - width, 2] representing the height and width of a box centered at each - pixel. - offset_predictions: A float tensor of shape [batch_size, height, width, 2] - representing the y and x offsets of a box centered at each pixel. This - helps reduce the error from downsampling. - - Returns: - detection_boxes: A tensor of shape [batch_size, num_boxes, 4] holding the - the raw bounding box coordinates of boxes. - """ - batch_size, num_boxes = _get_shape(y_indices, 2) - _, height, width, _ = _get_shape(height_width_predictions, 4) - height, width = tf.cast(height, tf.float32), tf.cast(width, tf.float32) - - # TF Lite does not support tf.gather with batch_dims > 0, so we need to use - # tf_gather_nd instead and here we prepare the indices for that. - combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_boxes), - tf.reshape(y_indices, [-1]), - tf.reshape(x_indices, [-1]) - ], axis=1) - new_height_width = tf.gather_nd(height_width_predictions, combined_indices) - new_height_width = tf.reshape(new_height_width, [batch_size, num_boxes, 2]) - - new_offsets = tf.gather_nd(offset_predictions, combined_indices) - offsets = tf.reshape(new_offsets, [batch_size, num_boxes, 2]) - - y_indices = _to_float32(y_indices) - x_indices = _to_float32(x_indices) - - height_width = tf.maximum(new_height_width, 0) - heights, widths = tf.unstack(height_width, axis=2) - y_offsets, x_offsets = tf.unstack(offsets, axis=2) - - ymin = y_indices + y_offsets - heights / 2.0 - xmin = x_indices + x_offsets - widths / 2.0 - ymax = y_indices + y_offsets + heights / 2.0 - xmax = x_indices + x_offsets + widths / 2.0 - - ymin = tf.clip_by_value(ymin, 0., height) - xmin = tf.clip_by_value(xmin, 0., width) - ymax = tf.clip_by_value(ymax, 0., height) - xmax = tf.clip_by_value(xmax, 0., width) - boxes = tf.stack([ymin, xmin, ymax, xmax], axis=2) - - return boxes - - -def prediction_tensors_to_temporal_offsets( - y_indices, x_indices, offset_predictions): - """Converts CenterNet temporal offset map predictions to batched format. - - This function is similar to the box offset conversion function, as both - temporal offsets and box offsets are size-2 vectors. - - Args: - y_indices: A [batch, num_boxes] int32 tensor with y indices corresponding to - object center locations (expressed in output coordinate frame). - x_indices: A [batch, num_boxes] int32 tensor with x indices corresponding to - object center locations (expressed in output coordinate frame). - offset_predictions: A float tensor of shape [batch_size, height, width, 2] - representing the y and x offsets of a box's center across adjacent frames. - - Returns: - offsets: A tensor of shape [batch_size, num_boxes, 2] holding the - the object temporal offsets of (y, x) dimensions. - - """ - batch_size, num_boxes = _get_shape(y_indices, 2) - - # TF Lite does not support tf.gather with batch_dims > 0, so we need to use - # tf_gather_nd instead and here we prepare the indices for that. - combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_boxes), - tf.reshape(y_indices, [-1]), - tf.reshape(x_indices, [-1]) - ], axis=1) - - new_offsets = tf.gather_nd(offset_predictions, combined_indices) - offsets = tf.reshape(new_offsets, [batch_size, num_boxes, -1]) - - return offsets - - -def prediction_tensors_to_keypoint_candidates(keypoint_heatmap_predictions, - keypoint_heatmap_offsets, - keypoint_score_threshold=0.1, - max_pool_kernel_size=1, - max_candidates=20, - keypoint_depths=None): - """Convert keypoint heatmap predictions and offsets to keypoint candidates. - - Args: - keypoint_heatmap_predictions: A float tensor of shape [batch_size, height, - width, num_keypoints] representing the per-keypoint heatmaps. - keypoint_heatmap_offsets: A float tensor of shape [batch_size, height, - width, 2] (or [batch_size, height, width, 2 * num_keypoints] if - 'per_keypoint_offset' is set True) representing the per-keypoint offsets. - keypoint_score_threshold: float, the threshold for considering a keypoint a - candidate. - max_pool_kernel_size: integer, the max pool kernel size to use to pull off - peak score locations in a neighborhood. For example, to make sure no two - neighboring values for the same keypoint are returned, set - max_pool_kernel_size=3. If None or 1, will not apply any local filtering. - max_candidates: integer, maximum number of keypoint candidates per keypoint - type. - keypoint_depths: (optional) A float tensor of shape [batch_size, height, - width, 1] (or [batch_size, height, width, num_keypoints] if - 'per_keypoint_depth' is set True) representing the per-keypoint depths. - - Returns: - keypoint_candidates: A tensor of shape - [batch_size, max_candidates, num_keypoints, 2] holding the - location of keypoint candidates in [y, x] format (expressed in absolute - coordinates in the output coordinate frame). - keypoint_scores: A float tensor of shape - [batch_size, max_candidates, num_keypoints] with the scores for each - keypoint candidate. The scores come directly from the heatmap predictions. - num_keypoint_candidates: An integer tensor of shape - [batch_size, num_keypoints] with the number of candidates for each - keypoint type, as it's possible to filter some candidates due to the score - threshold. - depth_candidates: A tensor of shape [batch_size, max_candidates, - num_keypoints] representing the estimated depth of each keypoint - candidate. Return None if the input keypoint_depths is None. - """ - batch_size, _, _, num_keypoints = _get_shape(keypoint_heatmap_predictions, 4) - # Get x, y and channel indices corresponding to the top indices in the - # keypoint heatmap predictions. - # Note that the top k candidates are produced for **each keypoint type**. - # Might be worth eventually trying top k in the feature map, independent of - # the keypoint type. - keypoint_scores, y_indices, x_indices, channel_indices = ( - top_k_feature_map_locations(keypoint_heatmap_predictions, - max_pool_kernel_size=max_pool_kernel_size, - k=max_candidates, - per_channel=True)) - - # TF Lite does not support tf.gather with batch_dims > 0, so we need to use - # tf_gather_nd instead and here we prepare the indices for that. - _, num_indices = _get_shape(y_indices, 2) - combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_indices), - tf.reshape(y_indices, [-1]), - tf.reshape(x_indices, [-1]) - ], axis=1) - - selected_offsets_flat = tf.gather_nd(keypoint_heatmap_offsets, - combined_indices) - selected_offsets = tf.reshape(selected_offsets_flat, - [batch_size, num_indices, -1]) - - y_indices = _to_float32(y_indices) - x_indices = _to_float32(x_indices) - - _, _, num_channels = _get_shape(selected_offsets, 3) - if num_channels > 2: - # Offsets are per keypoint and the last dimension of selected_offsets - # contains all those offsets, so reshape the offsets to make sure that the - # last dimension contains (y_offset, x_offset) for a single keypoint. - reshaped_offsets = tf.reshape(selected_offsets, - [batch_size, num_indices, -1, 2]) - - # TF Lite does not support tf.gather with batch_dims > 0, so we need to use - # tf_gather_nd instead and here we prepare the indices for that. In this - # case, channel_indices indicates which keypoint to use the offset from. - channel_combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_indices), - _multi_range(num_indices, range_repetitions=batch_size), - tf.reshape(channel_indices, [-1]) - ], axis=1) - - offsets = tf.gather_nd(reshaped_offsets, channel_combined_indices) - offsets = tf.reshape(offsets, [batch_size, num_indices, -1]) - else: - offsets = selected_offsets - y_offsets, x_offsets = tf.unstack(offsets, axis=2) - - keypoint_candidates = tf.stack([y_indices + y_offsets, - x_indices + x_offsets], axis=2) - keypoint_candidates = tf.reshape( - keypoint_candidates, - [batch_size, num_keypoints, max_candidates, 2]) - keypoint_candidates = tf.transpose(keypoint_candidates, [0, 2, 1, 3]) - keypoint_scores = tf.reshape( - keypoint_scores, - [batch_size, num_keypoints, max_candidates]) - keypoint_scores = tf.transpose(keypoint_scores, [0, 2, 1]) - num_candidates = tf.reduce_sum( - tf.to_int32(keypoint_scores >= keypoint_score_threshold), axis=1) - - depth_candidates = None - if keypoint_depths is not None: - selected_depth_flat = tf.gather_nd(keypoint_depths, combined_indices) - selected_depth = tf.reshape(selected_depth_flat, - [batch_size, num_indices, -1]) - _, _, num_depth_channels = _get_shape(selected_depth, 3) - if num_depth_channels > 1: - combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_indices), - _multi_range(num_indices, range_repetitions=batch_size), - tf.reshape(channel_indices, [-1]) - ], axis=1) - depth = tf.gather_nd(selected_depth, combined_indices) - depth = tf.reshape(depth, [batch_size, num_indices, -1]) - else: - depth = selected_depth - depth_candidates = tf.reshape(depth, - [batch_size, num_keypoints, max_candidates]) - depth_candidates = tf.transpose(depth_candidates, [0, 2, 1]) - - return keypoint_candidates, keypoint_scores, num_candidates, depth_candidates - - -def argmax_feature_map_locations(feature_map): - """Returns the peak locations in the feature map.""" - batch_size, _, width, num_channels = _get_shape(feature_map, 4) - - feature_map_flattened = tf.reshape( - feature_map, [batch_size, -1, num_channels]) - peak_flat_indices = tf.math.argmax( - feature_map_flattened, axis=1, output_type=tf.dtypes.int32) - # Get x and y indices corresponding to the top indices in the flat array. - y_indices, x_indices = ( - row_col_indices_from_flattened_indices(peak_flat_indices, width)) - channel_indices = tf.tile( - tf.range(num_channels)[tf.newaxis, :], [batch_size, 1]) - return y_indices, x_indices, channel_indices - - -def prediction_tensors_to_single_instance_kpts( - keypoint_heatmap_predictions, - keypoint_heatmap_offsets, - keypoint_score_heatmap=None): - """Convert keypoint heatmap predictions and offsets to keypoint candidates. - - Args: - keypoint_heatmap_predictions: A float tensor of shape [batch_size, height, - width, num_keypoints] representing the per-keypoint heatmaps which is - used for finding the best keypoint candidate locations. - keypoint_heatmap_offsets: A float tensor of shape [batch_size, height, - width, 2] (or [batch_size, height, width, 2 * num_keypoints] if - 'per_keypoint_offset' is set True) representing the per-keypoint offsets. - keypoint_score_heatmap: (optional) A float tensor of shape [batch_size, - height, width, num_keypoints] representing the heatmap which is used for - reporting the confidence scores. If not provided, then the values in the - keypoint_heatmap_predictions will be used. - - Returns: - keypoint_candidates: A tensor of shape - [batch_size, max_candidates, num_keypoints, 2] holding the - location of keypoint candidates in [y, x] format (expressed in absolute - coordinates in the output coordinate frame). - keypoint_scores: A float tensor of shape - [batch_size, max_candidates, num_keypoints] with the scores for each - keypoint candidate. The scores come directly from the heatmap predictions. - num_keypoint_candidates: An integer tensor of shape - [batch_size, num_keypoints] with the number of candidates for each - keypoint type, as it's possible to filter some candidates due to the score - threshold. - """ - batch_size, _, _, num_keypoints = _get_shape( - keypoint_heatmap_predictions, 4) - # Get x, y and channel indices corresponding to the top indices in the - # keypoint heatmap predictions. - y_indices, x_indices, channel_indices = argmax_feature_map_locations( - keypoint_heatmap_predictions) - - # TF Lite does not support tf.gather with batch_dims > 0, so we need to use - # tf_gather_nd instead and here we prepare the indices for that. - _, num_keypoints = _get_shape(y_indices, 2) - combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_keypoints), - tf.reshape(y_indices, [-1]), - tf.reshape(x_indices, [-1]), - ], axis=1) - - # shape: [num_keypoints, num_keypoints * 2] - selected_offsets_flat = tf.gather_nd(keypoint_heatmap_offsets, - combined_indices) - # shape: [num_keypoints, num_keypoints, 2]. - selected_offsets_flat = tf.reshape( - selected_offsets_flat, [num_keypoints, num_keypoints, -1]) - # shape: [num_keypoints]. - channel_indices = tf.keras.backend.flatten(channel_indices) - # shape: [num_keypoints, 2]. - retrieve_indices = tf.stack([channel_indices, channel_indices], axis=1) - # shape: [num_keypoints, 2] - selected_offsets = tf.gather_nd(selected_offsets_flat, retrieve_indices) - y_offsets, x_offsets = tf.unstack(selected_offsets, axis=1) - - keypoint_candidates = tf.stack([ - tf.cast(y_indices, dtype=tf.float32) + tf.expand_dims(y_offsets, axis=0), - tf.cast(x_indices, dtype=tf.float32) + tf.expand_dims(x_offsets, axis=0) - ], axis=2) - keypoint_candidates = tf.expand_dims(keypoint_candidates, axis=0) - - # Append the channel indices back to retrieve the keypoint scores from the - # heatmap. - combined_indices = tf.concat( - [combined_indices, tf.expand_dims(channel_indices, axis=-1)], axis=1) - if keypoint_score_heatmap is None: - keypoint_scores = tf.gather_nd( - keypoint_heatmap_predictions, combined_indices) - else: - keypoint_scores = tf.gather_nd(keypoint_score_heatmap, combined_indices) - keypoint_scores = tf.expand_dims( - tf.expand_dims(keypoint_scores, axis=0), axis=0) - return keypoint_candidates, keypoint_scores - - -def _score_to_distance_map(y_grid, x_grid, heatmap, points_y, points_x, - score_distance_offset): - """Rescores heatmap using the distance information. - - Rescore the heatmap scores using the formula: - score / (d + score_distance_offset), where the d is the distance from each - pixel location to the target point location. - - Args: - y_grid: A float tensor with shape [height, width] representing the - y-coordinate of each pixel grid. - x_grid: A float tensor with shape [height, width] representing the - x-coordinate of each pixel grid. - heatmap: A float tensor with shape [1, height, width, channel] - representing the heatmap to be rescored. - points_y: A float tensor with shape [channel] representing the y - coordinates of the target points for each channel. - points_x: A float tensor with shape [channel] representing the x - coordinates of the target points for each channel. - score_distance_offset: A constant used in the above formula. - - Returns: - A float tensor with shape [1, height, width, channel] representing the - rescored heatmap. - """ - y_diff = y_grid[:, :, tf.newaxis] - points_y - x_diff = x_grid[:, :, tf.newaxis] - points_x - distance = tf.math.sqrt(y_diff**2 + x_diff**2) - return tf.math.divide(heatmap, distance + score_distance_offset) - - -def prediction_to_single_instance_keypoints( - object_heatmap, - keypoint_heatmap, - keypoint_offset, - keypoint_regression, - kp_params, - keypoint_depths=None): - """Postprocess function to predict single instance keypoints. - - This is a simplified postprocessing function based on the assumption that - there is only one instance in the image. If there are multiple instances in - the image, the model prefers to predict the one that is closest to the image - center. Here is a high-level description of what this function does: - 1) Object heatmap re-weighted by the distance between each pixel to the - image center is used to determine the instance center. - 2) Regressed keypoint locations are retrieved from the instance center. The - Gaussian kernel is applied to the regressed keypoint locations to - re-weight the keypoint heatmap. This is to select the keypoints that are - associated with the center instance without using top_k op. - 3) The keypoint locations are computed by the re-weighted keypoint heatmap - and the keypoint offset. - - Args: - object_heatmap: A float tensor of shape [1, height, width, 1] representing - the heapmap of the class. - keypoint_heatmap: A float tensor of shape [1, height, width, num_keypoints] - representing the per-keypoint heatmaps. - keypoint_offset: A float tensor of shape [1, height, width, 2] (or [1, - height, width, 2 * num_keypoints] if 'per_keypoint_offset' is set True) - representing the per-keypoint offsets. - keypoint_regression: A float tensor of shape [1, height, width, 2 * - num_keypoints] representing the joint regression prediction. - kp_params: A `KeypointEstimationParams` object with parameters for a single - keypoint class. - keypoint_depths: (optional) A float tensor of shape [batch_size, height, - width, 1] (or [batch_size, height, width, num_keypoints] if - 'per_keypoint_depth' is set True) representing the per-keypoint depths. - - Returns: - A tuple of two tensors: - keypoint_candidates: A float tensor with shape [1, 1, num_keypoints, 2] - representing the yx-coordinates of the keypoints in the output feature - map space. - keypoint_scores: A float tensor with shape [1, 1, num_keypoints] - representing the keypoint prediction scores. - - Raises: - ValueError: if the input keypoint_std_dev doesn't have valid number of - elements (1 or num_keypoints). - """ - # TODO(yuhuic): add the keypoint depth prediction logics in the browser - # postprocessing back. - del keypoint_depths - - num_keypoints = len(kp_params.keypoint_std_dev) - batch_size, height, width, _ = _get_shape(keypoint_heatmap, 4) - - # Create the image center location. - image_center_y = tf.convert_to_tensor([0.5 * height], dtype=tf.float32) - image_center_x = tf.convert_to_tensor([0.5 * width], dtype=tf.float32) - (y_grid, x_grid) = ta_utils.image_shape_to_grids(height, width) - # Rescore the object heatmap by the distnace to the image center. - object_heatmap = _score_to_distance_map( - y_grid, x_grid, object_heatmap, image_center_y, - image_center_x, kp_params.score_distance_offset) - - # Pick the highest score and location of the weighted object heatmap. - y_indices, x_indices, _ = argmax_feature_map_locations(object_heatmap) - _, num_indices = _get_shape(y_indices, 2) - combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_indices), - tf.reshape(y_indices, [-1]), - tf.reshape(x_indices, [-1]) - ], axis=1) - - # Select the regression vectors from the object center. - selected_regression_flat = tf.gather_nd(keypoint_regression, combined_indices) - # shape: [num_keypoints, 2] - regression_offsets = tf.reshape(selected_regression_flat, [num_keypoints, -1]) - (y_reg, x_reg) = tf.unstack(regression_offsets, axis=1) - y_regressed = tf.cast(y_indices, dtype=tf.float32) + y_reg - x_regressed = tf.cast(x_indices, dtype=tf.float32) + x_reg - - if kp_params.candidate_ranking_mode == 'score_distance_ratio': - reweighted_keypoint_heatmap = _score_to_distance_map( - y_grid, x_grid, keypoint_heatmap, y_regressed, x_regressed, - kp_params.score_distance_offset) - else: - raise ValueError('Unsupported candidate_ranking_mode: %s' % - kp_params.candidate_ranking_mode) - - # Get the keypoint locations/scores: - # keypoint_candidates: [1, 1, num_keypoints, 2] - # keypoint_scores: [1, 1, num_keypoints] - # depth_candidates: [1, 1, num_keypoints] - (keypoint_candidates, keypoint_scores - ) = prediction_tensors_to_single_instance_kpts( - reweighted_keypoint_heatmap, - keypoint_offset, - keypoint_score_heatmap=keypoint_heatmap) - return keypoint_candidates, keypoint_scores, None - - -def _gaussian_weighted_map_const_multi( - y_grid, x_grid, heatmap, points_y, points_x, boxes, - gaussian_denom_ratio): - """Rescores heatmap using the distance information. - - The function is called when the candidate_ranking_mode in the - KeypointEstimationParams is set to be 'gaussian_weighted_const'. The - keypoint candidates are ranked using the formula: - heatmap_score * exp((-distances^2) / (gaussian_denom)) - - where 'gaussian_denom' is determined by: - min(output_feature_height, output_feature_width) * gaussian_denom_ratio - - the 'distances' are the distances between the grid coordinates and the target - points. - - Note that the postfix 'const' refers to the fact that the denominator is a - constant given the input image size, not scaled by the size of each of the - instances. - - Args: - y_grid: A float tensor with shape [height, width] representing the - y-coordinate of each pixel grid. - x_grid: A float tensor with shape [height, width] representing the - x-coordinate of each pixel grid. - heatmap: A float tensor with shape [height, width, num_keypoints] - representing the heatmap to be rescored. - points_y: A float tensor with shape [num_instances, num_keypoints] - representing the y coordinates of the target points for each channel. - points_x: A float tensor with shape [num_instances, num_keypoints] - representing the x coordinates of the target points for each channel. - boxes: A tensor of shape [num_instances, 4] with predicted bounding boxes - for each instance, expressed in the output coordinate frame. - gaussian_denom_ratio: A constant used in the above formula that determines - the denominator of the Gaussian kernel. - - Returns: - A float tensor with shape [height, width, channel] representing - the rescored heatmap. - """ - num_instances, _ = _get_shape(boxes, 2) - height, width, num_keypoints = _get_shape(heatmap, 3) - - # [height, width, num_instances, num_keypoints]. - # Note that we intentionally avoid using tf.newaxis as TfLite converter - # doesn't like it. - y_diff = ( - tf.reshape(y_grid, [height, width, 1, 1]) - - tf.reshape(points_y, [1, 1, num_instances, num_keypoints])) - x_diff = ( - tf.reshape(x_grid, [height, width, 1, 1]) - - tf.reshape(points_x, [1, 1, num_instances, num_keypoints])) - distance_square = y_diff * y_diff + x_diff * x_diff - - y_min, x_min, y_max, x_max = tf.split(boxes, 4, axis=1) - - # Make the mask with all 1.0 in the box regions. - # Shape: [height, width, num_instances] - in_boxes = tf.math.logical_and( - tf.math.logical_and( - tf.reshape(y_grid, [height, width, 1]) >= tf.reshape( - y_min, [1, 1, num_instances]), - tf.reshape(y_grid, [height, width, 1]) < tf.reshape( - y_max, [1, 1, num_instances])), - tf.math.logical_and( - tf.reshape(x_grid, [height, width, 1]) >= tf.reshape( - x_min, [1, 1, num_instances]), - tf.reshape(x_grid, [height, width, 1]) < tf.reshape( - x_max, [1, 1, num_instances]))) - in_boxes = tf.cast(in_boxes, dtype=tf.float32) - - gaussian_denom = tf.cast( - tf.minimum(height, width), dtype=tf.float32) * gaussian_denom_ratio - # shape: [height, width, num_instances, num_keypoints] - gaussian_map = tf.exp((-1 * distance_square) / gaussian_denom) - return tf.expand_dims(heatmap, axis=2) * gaussian_map * tf.reshape( - in_boxes, [height, width, num_instances, 1]) - - -def prediction_tensors_to_multi_instance_kpts( - keypoint_heatmap_predictions, - keypoint_heatmap_offsets, - keypoint_score_heatmap=None): - """Converts keypoint heatmap predictions and offsets to keypoint candidates. - - This function is similar to the 'prediction_tensors_to_single_instance_kpts' - function except that the input keypoint_heatmap_predictions is prepared to - have an additional 'num_instances' dimension for multi-instance prediction. - - Args: - keypoint_heatmap_predictions: A float tensor of shape [height, - width, num_instances, num_keypoints] representing the per-keypoint and - per-instance heatmaps which is used for finding the best keypoint - candidate locations. - keypoint_heatmap_offsets: A float tensor of shape [height, - width, 2 * num_keypoints] representing the per-keypoint offsets. - keypoint_score_heatmap: (optional) A float tensor of shape [height, width, - num_keypoints] representing the heatmap which is used for reporting the - confidence scores. If not provided, then the values in the - keypoint_heatmap_predictions will be used. - - Returns: - keypoint_candidates: A tensor of shape - [1, max_candidates, num_keypoints, 2] holding the - location of keypoint candidates in [y, x] format (expressed in absolute - coordinates in the output coordinate frame). - keypoint_scores: A float tensor of shape - [1, max_candidates, num_keypoints] with the scores for each - keypoint candidate. The scores come directly from the heatmap predictions. - """ - height, width, num_instances, num_keypoints = _get_shape( - keypoint_heatmap_predictions, 4) - - # [height * width, num_instances * num_keypoints]. - feature_map_flattened = tf.reshape( - keypoint_heatmap_predictions, - [-1, num_instances * num_keypoints]) - - # [num_instances * num_keypoints]. - peak_flat_indices = tf.math.argmax( - feature_map_flattened, axis=0, output_type=tf.dtypes.int32) - - # Get x and y indices corresponding to the top indices in the flat array. - y_indices, x_indices = ( - row_col_indices_from_flattened_indices(peak_flat_indices, width)) - # [num_instances * num_keypoints]. - y_indices = tf.reshape(y_indices, [-1]) - x_indices = tf.reshape(x_indices, [-1]) - - # Prepare the indices to gather the offsets from the keypoint_heatmap_offsets. - kpts_idx = _multi_range( - limit=num_keypoints, value_repetitions=1, - range_repetitions=num_instances) - combined_indices = tf.stack([ - y_indices, - x_indices, - kpts_idx - ], axis=1) - - keypoint_heatmap_offsets = tf.reshape( - keypoint_heatmap_offsets, [height, width, num_keypoints, 2]) - # Retrieve the keypoint offsets: shape: - # [num_instance * num_keypoints, 2]. - selected_offsets_flat = tf.gather_nd(keypoint_heatmap_offsets, - combined_indices) - y_offsets, x_offsets = tf.unstack(selected_offsets_flat, axis=1) - - keypoint_candidates = tf.stack([ - tf.cast(y_indices, dtype=tf.float32) + tf.expand_dims(y_offsets, axis=0), - tf.cast(x_indices, dtype=tf.float32) + tf.expand_dims(x_offsets, axis=0) - ], axis=2) - keypoint_candidates = tf.reshape( - keypoint_candidates, [num_instances, num_keypoints, 2]) - - if keypoint_score_heatmap is None: - keypoint_scores = tf.gather_nd( - tf.reduce_max(keypoint_heatmap_predictions, axis=2), combined_indices) - else: - keypoint_scores = tf.gather_nd(keypoint_score_heatmap, combined_indices) - return tf.expand_dims(keypoint_candidates, axis=0), tf.reshape( - keypoint_scores, [1, num_instances, num_keypoints]) - - -def prediction_to_keypoints_argmax( - prediction_dict, - object_y_indices, - object_x_indices, - boxes, - task_name, - kp_params): - """Postprocess function to predict multi instance keypoints with argmax op. - - This is a different implementation of the original keypoint postprocessing - function such that it avoids using topk op (replaced by argmax) as it runs - much slower in the browser. Note that in this function, we assume the - batch_size to be 1 to avoid using 5D tensors which cause issues when - converting to the TfLite model. - - Args: - prediction_dict: a dictionary holding predicted tensors, returned from the - predict() method. This dictionary should contain keypoint prediction - feature maps for each keypoint task. - object_y_indices: A float tensor of shape [batch_size, max_instances] - representing the location indices of the object centers. - object_x_indices: A float tensor of shape [batch_size, max_instances] - representing the location indices of the object centers. - boxes: A tensor of shape [batch_size, num_instances, 4] with predicted - bounding boxes for each instance, expressed in the output coordinate - frame. - task_name: string, the name of the task this namedtuple corresponds to. - Note that it should be an unique identifier of the task. - kp_params: A `KeypointEstimationParams` object with parameters for a single - keypoint class. - - Returns: - A tuple of two tensors: - keypoint_candidates: A float tensor with shape [batch_size, - num_instances, num_keypoints, 2] representing the yx-coordinates of - the keypoints in the output feature map space. - keypoint_scores: A float tensor with shape [batch_size, num_instances, - num_keypoints] representing the keypoint prediction scores. - - Raises: - ValueError: if the candidate_ranking_mode is not supported. - """ - keypoint_heatmap = tf.squeeze(tf.nn.sigmoid(prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_HEATMAP)][-1]), axis=0) - keypoint_offset = tf.squeeze(prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_OFFSET)][-1], axis=0) - keypoint_regression = tf.squeeze(prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_REGRESSION)][-1], axis=0) - height, width, num_keypoints = _get_shape(keypoint_heatmap, 3) - - # Create the y,x grids: [height, width] - (y_grid, x_grid) = ta_utils.image_shape_to_grids(height, width) - - # Prepare the indices to retrieve the information from object centers. - num_instances = _get_shape(object_y_indices, 2)[1] - combined_obj_indices = tf.stack([ - tf.reshape(object_y_indices, [-1]), - tf.reshape(object_x_indices, [-1]) - ], axis=1) - - # Select the regression vectors from the object center. - selected_regression_flat = tf.gather_nd( - keypoint_regression, combined_obj_indices) - selected_regression = tf.reshape( - selected_regression_flat, [num_instances, num_keypoints, 2]) - (y_reg, x_reg) = tf.unstack(selected_regression, axis=2) - - # shape: [num_instances, num_keypoints]. - y_regressed = tf.cast( - tf.reshape(object_y_indices, [num_instances, 1]), - dtype=tf.float32) + y_reg - x_regressed = tf.cast( - tf.reshape(object_x_indices, [num_instances, 1]), - dtype=tf.float32) + x_reg - - if kp_params.candidate_ranking_mode == 'gaussian_weighted_const': - rescored_heatmap = _gaussian_weighted_map_const_multi( - y_grid, x_grid, keypoint_heatmap, y_regressed, x_regressed, - tf.squeeze(boxes, axis=0), kp_params.gaussian_denom_ratio) - - # shape: [height, width, num_keypoints]. - keypoint_score_heatmap = tf.math.reduce_max(rescored_heatmap, axis=2) - else: - raise ValueError( - 'Unsupported ranking mode in the multipose no topk method: %s' % - kp_params.candidate_ranking_mode) - (keypoint_candidates, - keypoint_scores) = prediction_tensors_to_multi_instance_kpts( - keypoint_heatmap_predictions=rescored_heatmap, - keypoint_heatmap_offsets=keypoint_offset, - keypoint_score_heatmap=keypoint_score_heatmap) - return keypoint_candidates, keypoint_scores - - -def regressed_keypoints_at_object_centers(regressed_keypoint_predictions, - y_indices, x_indices): - """Returns the regressed keypoints at specified object centers. - - The original keypoint predictions are regressed relative to each feature map - location. The returned keypoints are expressed in absolute coordinates in the - output frame (i.e. the center offsets are added to each individual regressed - set of keypoints). - - Args: - regressed_keypoint_predictions: A float tensor of shape - [batch_size, height, width, 2 * num_keypoints] holding regressed - keypoints. The last dimension has keypoint coordinates ordered as follows: - [y0, x0, y1, x1, ..., y{J-1}, x{J-1}] where J is the number of keypoints. - y_indices: A [batch, num_instances] int tensor holding y indices for object - centers. These indices correspond to locations in the output feature map. - x_indices: A [batch, num_instances] int tensor holding x indices for object - centers. These indices correspond to locations in the output feature map. - - Returns: - A float tensor of shape [batch_size, num_objects, 2 * num_keypoints] where - regressed keypoints are gathered at the provided locations, and converted - to absolute coordinates in the output coordinate frame. - """ - batch_size, num_instances = _get_shape(y_indices, 2) - - # TF Lite does not support tf.gather with batch_dims > 0, so we need to use - # tf_gather_nd instead and here we prepare the indices for that. - combined_indices = tf.stack([ - _multi_range(batch_size, value_repetitions=num_instances), - tf.reshape(y_indices, [-1]), - tf.reshape(x_indices, [-1]) - ], axis=1) - - relative_regressed_keypoints = tf.gather_nd(regressed_keypoint_predictions, - combined_indices) - relative_regressed_keypoints = tf.reshape( - relative_regressed_keypoints, - [batch_size, num_instances, -1, 2]) - relative_regressed_keypoints_y, relative_regressed_keypoints_x = tf.unstack( - relative_regressed_keypoints, axis=3) - y_indices = _to_float32(tf.expand_dims(y_indices, axis=-1)) - x_indices = _to_float32(tf.expand_dims(x_indices, axis=-1)) - absolute_regressed_keypoints = tf.stack( - [y_indices + relative_regressed_keypoints_y, - x_indices + relative_regressed_keypoints_x], - axis=3) - return tf.reshape(absolute_regressed_keypoints, - [batch_size, num_instances, -1]) - - -def sdr_scaled_ranking_score( - keypoint_scores, distances, bboxes, score_distance_multiplier): - """Score-to-distance-ratio method to rank keypoint candidates. - - This corresponds to the ranking method: 'score_scaled_distance_ratio'. The - keypoint candidates are ranked using the formula: - ranking_score = score / (distance + offset) - - where 'score' is the keypoint heatmap scores, 'distance' is the distance - between the heatmap peak location and the regressed joint location, - 'offset' is a function of the predicted bounding box: - offset = max(bbox height, bbox width) * score_distance_multiplier - - The ranking score is used to find the best keypoint candidate for snapping - regressed joints. - - Args: - keypoint_scores: A float tensor of shape - [batch_size, max_candidates, num_keypoints] indicating the scores for - keypoint candidates. - distances: A float tensor of shape - [batch_size, num_instances, max_candidates, num_keypoints] indicating the - distances between the keypoint candidates and the joint regression - locations of each instances. - bboxes: A tensor of shape [batch_size, num_instances, 4] with predicted - bounding boxes for each instance, expressed in the output coordinate - frame. If not provided, boxes will be computed from regressed keypoints. - score_distance_multiplier: A scalar used to multiply the bounding box size - to be the offset in the score-to-distance-ratio formula. - - Returns: - A float tensor of shape [batch_size, num_instances, max_candidates, - num_keypoints] representing the ranking scores of each keypoint candidates. - """ - # Get ymin, xmin, ymax, xmax bounding box coordinates. - # Shape: [batch_size, num_instances] - ymin, xmin, ymax, xmax = tf.unstack(bboxes, axis=2) - - # Shape: [batch_size, num_instances]. - offsets = tf.math.maximum( - ymax - ymin, xmax - xmin) * score_distance_multiplier - - # Shape: [batch_size, num_instances, max_candidates, num_keypoints] - ranking_scores = keypoint_scores[:, tf.newaxis, :, :] / ( - distances + offsets[:, :, tf.newaxis, tf.newaxis]) - return ranking_scores - - -def gaussian_weighted_score( - keypoint_scores, distances, keypoint_std_dev, bboxes): - """Gaussian weighted method to rank keypoint candidates. - - This corresponds to the ranking method: 'gaussian_weighted'. The - keypoint candidates are ranked using the formula: - score * exp((-distances^2) / (2 * sigma^2)) - - where 'score' is the keypoint heatmap score, 'distances' is the distance - between the heatmap peak location and the regressed joint location and 'sigma' - is a Gaussian standard deviation used in generating the Gausian heatmap target - multiplied by the 'std_dev_multiplier'. - - The ranking score is used to find the best keypoint candidate for snapping - regressed joints. - - Args: - keypoint_scores: A float tensor of shape - [batch_size, max_candidates, num_keypoints] indicating the scores for - keypoint candidates. - distances: A float tensor of shape - [batch_size, num_instances, max_candidates, num_keypoints] indicating the - distances between the keypoint candidates and the joint regression - locations of each instances. - keypoint_std_dev: A list of float represent the standard deviation of the - Gaussian kernel used to generate the keypoint heatmap. It is to provide - the flexibility of using different sizes of Gaussian kernel for each - keypoint class. - bboxes: A tensor of shape [batch_size, num_instances, 4] with predicted - bounding boxes for each instance, expressed in the output coordinate - frame. If not provided, boxes will be computed from regressed keypoints. - - Returns: - A float tensor of shape [batch_size, num_instances, max_candidates, - num_keypoints] representing the ranking scores of each keypoint candidates. - """ - # Get ymin, xmin, ymax, xmax bounding box coordinates. - # Shape: [batch_size, num_instances] - ymin, xmin, ymax, xmax = tf.unstack(bboxes, axis=2) - - # shape: [num_keypoints] - keypoint_std_dev = tf.constant(keypoint_std_dev) - - # shape: [batch_size, num_instances] - sigma = cn_assigner._compute_std_dev_from_box_size( # pylint: disable=protected-access - ymax - ymin, xmax - xmin, min_overlap=0.7) - # shape: [batch_size, num_instances, num_keypoints] - sigma = keypoint_std_dev[tf.newaxis, tf.newaxis, :] * sigma[:, :, tf.newaxis] - (_, _, max_candidates, _) = _get_shape(distances, 4) - # shape: [batch_size, num_instances, max_candidates, num_keypoints] - sigma = tf.tile( - sigma[:, :, tf.newaxis, :], multiples=[1, 1, max_candidates, 1]) - - gaussian_map = tf.exp((-1 * distances * distances) / (2 * sigma * sigma)) - return keypoint_scores[:, tf.newaxis, :, :] * gaussian_map - - -def refine_keypoints(regressed_keypoints, - keypoint_candidates, - keypoint_scores, - num_keypoint_candidates, - bboxes=None, - unmatched_keypoint_score=0.1, - box_scale=1.2, - candidate_search_scale=0.3, - candidate_ranking_mode='min_distance', - score_distance_offset=1e-6, - keypoint_depth_candidates=None, - keypoint_score_threshold=0.1, - score_distance_multiplier=0.1, - keypoint_std_dev=None): - """Refines regressed keypoints by snapping to the nearest candidate keypoints. - - The initial regressed keypoints represent a full set of keypoints regressed - from the centers of the objects. The keypoint candidates are estimated - independently from heatmaps, and are not associated with any object instances. - This function refines the regressed keypoints by "snapping" to the - nearest/highest score/highest score-distance ratio (depending on the - candidate_ranking_mode) candidate of the same keypoint type (e.g. "nose"). - If no candidates are nearby, the regressed keypoint remains unchanged. - - In order to snap a regressed keypoint to a candidate keypoint, the following - must be satisfied: - - the candidate keypoint must be of the same type as the regressed keypoint - - the candidate keypoint must not lie outside the predicted boxes (or the - boxes which encloses the regressed keypoints for the instance if `bboxes` is - not provided). Note that the box is scaled by - `regressed_box_scale` in height and width, to provide some margin around the - keypoints - - the distance to the closest candidate keypoint cannot exceed - candidate_search_scale * max(height, width), where height and width refer to - the bounding box for the instance. - - Note that the same candidate keypoint is allowed to snap to regressed - keypoints in difference instances. - - Args: - regressed_keypoints: A float tensor of shape - [batch_size, num_instances, num_keypoints, 2] with the initial regressed - keypoints. - keypoint_candidates: A tensor of shape - [batch_size, max_candidates, num_keypoints, 2] holding the location of - keypoint candidates in [y, x] format (expressed in absolute coordinates in - the output coordinate frame). - keypoint_scores: A float tensor of shape - [batch_size, max_candidates, num_keypoints] indicating the scores for - keypoint candidates. - num_keypoint_candidates: An integer tensor of shape - [batch_size, num_keypoints] indicating the number of valid candidates for - each keypoint type, as there may be padding (dim 1) of - `keypoint_candidates` and `keypoint_scores`. - bboxes: A tensor of shape [batch_size, num_instances, 4] with predicted - bounding boxes for each instance, expressed in the output coordinate - frame. If not provided, boxes will be computed from regressed keypoints. - unmatched_keypoint_score: float, the default score to use for regressed - keypoints that are not successfully snapped to a nearby candidate. - box_scale: float, the multiplier to expand the bounding boxes (either the - provided boxes or those which tightly cover the regressed keypoints) for - an instance. This scale is typically larger than 1.0 when not providing - `bboxes`. - candidate_search_scale: float, the scale parameter that multiplies the - largest dimension of a bounding box. The resulting distance becomes a - search radius for candidates in the vicinity of each regressed keypoint. - candidate_ranking_mode: A string as one of ['min_distance', - 'score_distance_ratio', 'score_scaled_distance_ratio', - 'gaussian_weighted'] indicating how to select the candidate. If invalid - value is provided, an ValueError will be raised. - score_distance_offset: The distance offset to apply in the denominator when - candidate_ranking_mode is 'score_distance_ratio'. The metric to maximize - in this scenario is score / (distance + score_distance_offset). Larger - values of score_distance_offset make the keypoint score gain more relative - importance. - keypoint_depth_candidates: (optional) A float tensor of shape - [batch_size, max_candidates, num_keypoints] indicating the depths for - keypoint candidates. - keypoint_score_threshold: float, The heatmap score threshold for - a keypoint to become a valid candidate. - score_distance_multiplier: A scalar used to multiply the bounding box size - to be the offset in the score-to-distance-ratio formula. - keypoint_std_dev: A list of float represent the standard deviation of the - Gaussian kernel used to rank the keypoint candidates. It offers the - flexibility of using different sizes of Gaussian kernel for each keypoint - class. Only applicable when the candidate_ranking_mode equals to - 'gaussian_weighted'. - - Returns: - A tuple with: - refined_keypoints: A float tensor of shape - [batch_size, num_instances, num_keypoints, 2] with the final, refined - keypoints. - refined_scores: A float tensor of shape - [batch_size, num_instances, num_keypoints] with scores associated with all - instances and keypoints in `refined_keypoints`. - - Raises: - ValueError: if provided candidate_ranking_mode is not one of - ['min_distance', 'score_distance_ratio'] - """ - batch_size, num_instances, num_keypoints, _ = ( - shape_utils.combined_static_and_dynamic_shape(regressed_keypoints)) - max_candidates = keypoint_candidates.shape[1] - - # Replace all invalid (i.e. padded) keypoint candidates with NaN. - # This will prevent them from being considered. - range_tiled = tf.tile( - tf.reshape(tf.range(max_candidates), [1, max_candidates, 1]), - [batch_size, 1, num_keypoints]) - num_candidates_tiled = tf.tile(tf.expand_dims(num_keypoint_candidates, 1), - [1, max_candidates, 1]) - invalid_candidates = range_tiled >= num_candidates_tiled - - # Pairwise squared distances between regressed keypoints and candidate - # keypoints (for a single keypoint type). - # Shape [batch_size, num_instances, 1, num_keypoints, 2]. - regressed_keypoint_expanded = tf.expand_dims(regressed_keypoints, - axis=2) - # Shape [batch_size, 1, max_candidates, num_keypoints, 2]. - keypoint_candidates_expanded = tf.expand_dims( - keypoint_candidates, axis=1) - # Use explicit tensor shape broadcasting (since the tensor dimensions are - # expanded to 5D) to make it tf.lite compatible. - regressed_keypoint_expanded = tf.tile( - regressed_keypoint_expanded, multiples=[1, 1, max_candidates, 1, 1]) - keypoint_candidates_expanded = tf.tile( - keypoint_candidates_expanded, multiples=[1, num_instances, 1, 1, 1]) - # Replace tf.math.squared_difference by "-" operator and tf.multiply ops since - # tf.lite convert doesn't support squared_difference with undetermined - # dimension. - diff = regressed_keypoint_expanded - keypoint_candidates_expanded - sqrd_distances = tf.math.reduce_sum(tf.multiply(diff, diff), axis=-1) - distances = tf.math.sqrt(sqrd_distances) - - # Replace the invalid candidated with large constant (10^5) to make sure the - # following reduce_min/argmin behaves properly. - max_dist = 1e5 - distances = tf.where( - tf.tile( - tf.expand_dims(invalid_candidates, axis=1), - multiples=[1, num_instances, 1, 1]), - tf.ones_like(distances) * max_dist, - distances - ) - - # Determine the candidates that have the minimum distance to the regressed - # keypoints. Shape [batch_size, num_instances, num_keypoints]. - min_distances = tf.math.reduce_min(distances, axis=2) - if candidate_ranking_mode == 'min_distance': - nearby_candidate_inds = tf.math.argmin(distances, axis=2) - elif candidate_ranking_mode == 'score_distance_ratio': - # tiled_keypoint_scores: - # Shape [batch_size, num_instances, max_candidates, num_keypoints]. - tiled_keypoint_scores = tf.tile( - tf.expand_dims(keypoint_scores, axis=1), - multiples=[1, num_instances, 1, 1]) - ranking_scores = tiled_keypoint_scores / (distances + score_distance_offset) - nearby_candidate_inds = tf.math.argmax(ranking_scores, axis=2) - elif candidate_ranking_mode == 'score_scaled_distance_ratio': - ranking_scores = sdr_scaled_ranking_score( - keypoint_scores, distances, bboxes, score_distance_multiplier) - nearby_candidate_inds = tf.math.argmax(ranking_scores, axis=2) - elif candidate_ranking_mode == 'gaussian_weighted': - ranking_scores = gaussian_weighted_score( - keypoint_scores, distances, keypoint_std_dev, bboxes) - nearby_candidate_inds = tf.math.argmax(ranking_scores, axis=2) - weighted_scores = tf.math.reduce_max(ranking_scores, axis=2) - else: - raise ValueError('Not recognized candidate_ranking_mode: %s' % - candidate_ranking_mode) - - # Gather the coordinates and scores corresponding to the closest candidates. - # Shape of tensors are [batch_size, num_instances, num_keypoints, 2] and - # [batch_size, num_instances, num_keypoints], respectively. - (nearby_candidate_coords, nearby_candidate_scores, - nearby_candidate_depths) = ( - _gather_candidates_at_indices(keypoint_candidates, keypoint_scores, - nearby_candidate_inds, - keypoint_depth_candidates)) - - # If the ranking mode is 'gaussian_weighted', we use the ranking scores as the - # final keypoint confidence since their values are in between [0, 1]. - if candidate_ranking_mode == 'gaussian_weighted': - nearby_candidate_scores = weighted_scores - - if bboxes is None: - # Filter out the chosen candidate with score lower than unmatched - # keypoint score. - mask = tf.cast(nearby_candidate_scores < - keypoint_score_threshold, tf.int32) - else: - bboxes_flattened = tf.reshape(bboxes, [-1, 4]) - - # Scale the bounding boxes. - # Shape [batch_size, num_instances, 4]. - boxlist = box_list.BoxList(bboxes_flattened) - boxlist_scaled = box_list_ops.scale_height_width( - boxlist, box_scale, box_scale) - bboxes_scaled = boxlist_scaled.get() - bboxes = tf.reshape(bboxes_scaled, [batch_size, num_instances, 4]) - - # Get ymin, xmin, ymax, xmax bounding box coordinates, tiled per keypoint. - # Shape [batch_size, num_instances, num_keypoints]. - bboxes_tiled = tf.tile(tf.expand_dims(bboxes, 2), [1, 1, num_keypoints, 1]) - ymin, xmin, ymax, xmax = tf.unstack(bboxes_tiled, axis=3) - - # Produce a mask that indicates whether the original regressed keypoint - # should be used instead of a candidate keypoint. - # Shape [batch_size, num_instances, num_keypoints]. - search_radius = ( - tf.math.maximum(ymax - ymin, xmax - xmin) * candidate_search_scale) - mask = (tf.cast(nearby_candidate_coords[:, :, :, 0] < ymin, tf.int32) + - tf.cast(nearby_candidate_coords[:, :, :, 0] > ymax, tf.int32) + - tf.cast(nearby_candidate_coords[:, :, :, 1] < xmin, tf.int32) + - tf.cast(nearby_candidate_coords[:, :, :, 1] > xmax, tf.int32) + - # Filter out the chosen candidate with score lower than unmatched - # keypoint score. - tf.cast(nearby_candidate_scores < - keypoint_score_threshold, tf.int32) + - tf.cast(min_distances > search_radius, tf.int32)) - mask = mask > 0 - - # Create refined keypoints where candidate keypoints replace original - # regressed keypoints if they are in the vicinity of the regressed keypoints. - # Shape [batch_size, num_instances, num_keypoints, 2]. - refined_keypoints = tf.where( - tf.tile(tf.expand_dims(mask, -1), [1, 1, 1, 2]), - regressed_keypoints, - nearby_candidate_coords) - - # Update keypoints scores. In the case where we use the original regressed - # keypoints, we use a default score of `unmatched_keypoint_score`. - # Shape [batch_size, num_instances, num_keypoints]. - refined_scores = tf.where( - mask, - unmatched_keypoint_score * tf.ones_like(nearby_candidate_scores), - nearby_candidate_scores) - - refined_depths = None - if nearby_candidate_depths is not None: - refined_depths = tf.where(mask, tf.zeros_like(nearby_candidate_depths), - nearby_candidate_depths) - - return refined_keypoints, refined_scores, refined_depths - - -def _pad_to_full_keypoint_dim(keypoint_coords, keypoint_scores, keypoint_inds, - num_total_keypoints): - """Scatter keypoint elements into tensors with full keypoints dimension. - - Args: - keypoint_coords: a [batch_size, num_instances, num_keypoints, 2] float32 - tensor. - keypoint_scores: a [batch_size, num_instances, num_keypoints] float32 - tensor. - keypoint_inds: a list of integers that indicate the keypoint indices for - this specific keypoint class. These indices are used to scatter into - tensors that have a `num_total_keypoints` dimension. - num_total_keypoints: The total number of keypoints that this model predicts. - - Returns: - A tuple with - keypoint_coords_padded: a - [batch_size, num_instances, num_total_keypoints,2] float32 tensor. - keypoint_scores_padded: a [batch_size, num_instances, num_total_keypoints] - float32 tensor. - """ - batch_size, num_instances, _, _ = ( - shape_utils.combined_static_and_dynamic_shape(keypoint_coords)) - kpt_coords_transposed = tf.transpose(keypoint_coords, [2, 0, 1, 3]) - kpt_scores_transposed = tf.transpose(keypoint_scores, [2, 0, 1]) - kpt_inds_tensor = tf.expand_dims(keypoint_inds, axis=-1) - kpt_coords_scattered = tf.scatter_nd( - indices=kpt_inds_tensor, - updates=kpt_coords_transposed, - shape=[num_total_keypoints, batch_size, num_instances, 2]) - kpt_scores_scattered = tf.scatter_nd( - indices=kpt_inds_tensor, - updates=kpt_scores_transposed, - shape=[num_total_keypoints, batch_size, num_instances]) - keypoint_coords_padded = tf.transpose(kpt_coords_scattered, [1, 2, 0, 3]) - keypoint_scores_padded = tf.transpose(kpt_scores_scattered, [1, 2, 0]) - return keypoint_coords_padded, keypoint_scores_padded - - -def _pad_to_full_instance_dim(keypoint_coords, keypoint_scores, instance_inds, - max_instances): - """Scatter keypoint elements into tensors with full instance dimension. - - Args: - keypoint_coords: a [batch_size, num_instances, num_keypoints, 2] float32 - tensor. - keypoint_scores: a [batch_size, num_instances, num_keypoints] float32 - tensor. - instance_inds: a list of integers that indicate the instance indices for - these keypoints. These indices are used to scatter into tensors - that have a `max_instances` dimension. - max_instances: The maximum number of instances detected by the model. - - Returns: - A tuple with - keypoint_coords_padded: a [batch_size, max_instances, num_keypoints, 2] - float32 tensor. - keypoint_scores_padded: a [batch_size, max_instances, num_keypoints] - float32 tensor. - """ - batch_size, _, num_keypoints, _ = ( - shape_utils.combined_static_and_dynamic_shape(keypoint_coords)) - kpt_coords_transposed = tf.transpose(keypoint_coords, [1, 0, 2, 3]) - kpt_scores_transposed = tf.transpose(keypoint_scores, [1, 0, 2]) - instance_inds = tf.expand_dims(instance_inds, axis=-1) - kpt_coords_scattered = tf.scatter_nd( - indices=instance_inds, - updates=kpt_coords_transposed, - shape=[max_instances, batch_size, num_keypoints, 2]) - kpt_scores_scattered = tf.scatter_nd( - indices=instance_inds, - updates=kpt_scores_transposed, - shape=[max_instances, batch_size, num_keypoints]) - keypoint_coords_padded = tf.transpose(kpt_coords_scattered, [1, 0, 2, 3]) - keypoint_scores_padded = tf.transpose(kpt_scores_scattered, [1, 0, 2]) - return keypoint_coords_padded, keypoint_scores_padded - - -def _gather_candidates_at_indices(keypoint_candidates, - keypoint_scores, - indices, - keypoint_depth_candidates=None): - """Gathers keypoint candidate coordinates and scores at indices. - - Args: - keypoint_candidates: a float tensor of shape [batch_size, max_candidates, - num_keypoints, 2] with candidate coordinates. - keypoint_scores: a float tensor of shape [batch_size, max_candidates, - num_keypoints] with keypoint scores. - indices: an integer tensor of shape [batch_size, num_indices, num_keypoints] - with indices. - keypoint_depth_candidates: (optional) a float tensor of shape [batch_size, - max_candidates, num_keypoints] with keypoint depths. - - Returns: - A tuple with - gathered_keypoint_candidates: a float tensor of shape [batch_size, - num_indices, num_keypoints, 2] with gathered coordinates. - gathered_keypoint_scores: a float tensor of shape [batch_size, - num_indices, num_keypoints]. - gathered_keypoint_depths: a float tensor of shape [batch_size, - num_indices, num_keypoints]. Return None if the input - keypoint_depth_candidates is None. - """ - batch_size, num_indices, num_keypoints = _get_shape(indices, 3) - - # Transpose tensors so that all batch dimensions are up front. - keypoint_candidates_transposed = tf.transpose(keypoint_candidates, - [0, 2, 1, 3]) - keypoint_scores_transposed = tf.transpose(keypoint_scores, [0, 2, 1]) - nearby_candidate_inds_transposed = tf.transpose(indices, [0, 2, 1]) - - # TF Lite does not support tf.gather with batch_dims > 0, so we need to use - # tf_gather_nd instead and here we prepare the indices for that. - combined_indices = tf.stack([ - _multi_range( - batch_size, - value_repetitions=num_keypoints * num_indices, - dtype=tf.int64), - _multi_range( - num_keypoints, - value_repetitions=num_indices, - range_repetitions=batch_size, - dtype=tf.int64), - tf.reshape(nearby_candidate_inds_transposed, [-1]) - ], axis=1) - - nearby_candidate_coords_transposed = tf.gather_nd( - keypoint_candidates_transposed, combined_indices) - nearby_candidate_coords_transposed = tf.reshape( - nearby_candidate_coords_transposed, - [batch_size, num_keypoints, num_indices, -1]) - - nearby_candidate_scores_transposed = tf.gather_nd(keypoint_scores_transposed, - combined_indices) - nearby_candidate_scores_transposed = tf.reshape( - nearby_candidate_scores_transposed, - [batch_size, num_keypoints, num_indices]) - - gathered_keypoint_candidates = tf.transpose( - nearby_candidate_coords_transposed, [0, 2, 1, 3]) - # The reshape operation above may result in a singleton last dimension, but - # downstream code requires it to always be at least 2-valued. - original_shape = tf.shape(gathered_keypoint_candidates) - new_shape = tf.concat((original_shape[:3], - [tf.maximum(original_shape[3], 2)]), 0) - gathered_keypoint_candidates = tf.reshape(gathered_keypoint_candidates, - new_shape) - gathered_keypoint_scores = tf.transpose(nearby_candidate_scores_transposed, - [0, 2, 1]) - - gathered_keypoint_depths = None - if keypoint_depth_candidates is not None: - keypoint_depths_transposed = tf.transpose(keypoint_depth_candidates, - [0, 2, 1]) - nearby_candidate_depths_transposed = tf.gather_nd( - keypoint_depths_transposed, combined_indices) - nearby_candidate_depths_transposed = tf.reshape( - nearby_candidate_depths_transposed, - [batch_size, num_keypoints, num_indices]) - gathered_keypoint_depths = tf.transpose(nearby_candidate_depths_transposed, - [0, 2, 1]) - return (gathered_keypoint_candidates, gathered_keypoint_scores, - gathered_keypoint_depths) - - -def flattened_indices_from_row_col_indices(row_indices, col_indices, num_cols): - """Get the index in a flattened array given row and column indices.""" - return (row_indices * num_cols) + col_indices - - -def row_col_channel_indices_from_flattened_indices(indices, num_cols, - num_channels): - """Computes row, column and channel indices from flattened indices. - - Args: - indices: An integer tensor of any shape holding the indices in the flattened - space. - num_cols: Number of columns in the image (width). - num_channels: Number of channels in the image. - - Returns: - row_indices: The row indices corresponding to each of the input indices. - Same shape as indices. - col_indices: The column indices corresponding to each of the input indices. - Same shape as indices. - channel_indices. The channel indices corresponding to each of the input - indices. - - """ - # Be careful with this function when running a model in float16 precision - # (e.g. TF.js with WebGL) because the array indices may not be represented - # accurately if they are too large, resulting in incorrect channel indices. - # See: - # https://en.wikipedia.org/wiki/Half-precision_floating-point_format#Precision_limitations_on_integer_values - # - # Avoid using mod operator to make the ops more easy to be compatible with - # different environments, e.g. WASM. - row_indices = (indices // num_channels) // num_cols - col_indices = (indices // num_channels) - row_indices * num_cols - channel_indices_temp = indices // num_channels - channel_indices = indices - channel_indices_temp * num_channels - - return row_indices, col_indices, channel_indices - - -def row_col_indices_from_flattened_indices(indices, num_cols): - """Computes row and column indices from flattened indices. - - Args: - indices: An integer tensor of any shape holding the indices in the flattened - space. - num_cols: Number of columns in the image (width). - - Returns: - row_indices: The row indices corresponding to each of the input indices. - Same shape as indices. - col_indices: The column indices corresponding to each of the input indices. - Same shape as indices. - - """ - # Avoid using mod operator to make the ops more easy to be compatible with - # different environments, e.g. WASM. - row_indices = indices // num_cols - col_indices = indices - row_indices * num_cols - - return row_indices, col_indices - - -def get_valid_anchor_weights_in_flattened_image(true_image_shapes, height, - width): - """Computes valid anchor weights for an image assuming pixels will be flattened. - - This function is useful when we only want to penalize valid areas in the - image in the case when padding is used. The function assumes that the loss - function will be applied after flattening the spatial dimensions and returns - anchor weights accordingly. - - Args: - true_image_shapes: An integer tensor of shape [batch_size, 3] representing - the true image shape (without padding) for each sample in the batch. - height: height of the prediction from the network. - width: width of the prediction from the network. - - Returns: - valid_anchor_weights: a float tensor of shape [batch_size, height * width] - with 1s in locations where the spatial coordinates fall within the height - and width in true_image_shapes. - """ - - indices = tf.reshape(tf.range(height * width), [1, -1]) - batch_size = tf.shape(true_image_shapes)[0] - batch_indices = tf.ones((batch_size, 1), dtype=tf.int32) * indices - - y_coords, x_coords, _ = row_col_channel_indices_from_flattened_indices( - batch_indices, width, 1) - - max_y, max_x = true_image_shapes[:, 0], true_image_shapes[:, 1] - max_x = _to_float32(tf.expand_dims(max_x, 1)) - max_y = _to_float32(tf.expand_dims(max_y, 1)) - - x_coords = _to_float32(x_coords) - y_coords = _to_float32(y_coords) - - valid_mask = tf.math.logical_and(x_coords < max_x, y_coords < max_y) - - return _to_float32(valid_mask) - - -def convert_strided_predictions_to_normalized_boxes(boxes, stride, - true_image_shapes): - """Converts predictions in the output space to normalized boxes. - - Boxes falling outside the valid image boundary are clipped to be on the - boundary. - - Args: - boxes: A tensor of shape [batch_size, num_boxes, 4] holding the raw - coordinates of boxes in the model's output space. - stride: The stride in the output space. - true_image_shapes: A tensor of shape [batch_size, 3] representing the true - shape of the input not considering padding. - - Returns: - boxes: A tensor of shape [batch_size, num_boxes, 4] representing the - coordinates of the normalized boxes. - """ - # Note: We use tf ops instead of functions in box_list_ops to make this - # function compatible with dynamic batch size. - boxes = boxes * stride - true_image_shapes = tf.tile(true_image_shapes[:, tf.newaxis, :2], [1, 1, 2]) - boxes = boxes / tf.cast(true_image_shapes, tf.float32) - boxes = tf.clip_by_value(boxes, 0.0, 1.0) - return boxes - - -def convert_strided_predictions_to_normalized_keypoints( - keypoint_coords, keypoint_scores, stride, true_image_shapes, - clip_out_of_frame_keypoints=False): - """Converts predictions in the output space to normalized keypoints. - - If clip_out_of_frame_keypoints=False, keypoint coordinates falling outside - the valid image boundary are normalized but not clipped; If - clip_out_of_frame_keypoints=True, keypoint coordinates falling outside the - valid image boundary are clipped to the closest image boundary and the scores - will be set to 0.0. - - Args: - keypoint_coords: A tensor of shape - [batch_size, num_instances, num_keypoints, 2] holding the raw coordinates - of keypoints in the model's output space. - keypoint_scores: A tensor of shape - [batch_size, num_instances, num_keypoints] holding the keypoint scores. - stride: The stride in the output space. - true_image_shapes: A tensor of shape [batch_size, 3] representing the true - shape of the input not considering padding. - clip_out_of_frame_keypoints: A boolean indicating whether keypoints outside - the image boundary should be clipped. If True, keypoint coords will be - clipped to image boundary. If False, keypoints are normalized but not - filtered based on their location. - - Returns: - keypoint_coords_normalized: A tensor of shape - [batch_size, num_instances, num_keypoints, 2] representing the coordinates - of the normalized keypoints. - keypoint_scores: A tensor of shape - [batch_size, num_instances, num_keypoints] representing the updated - keypoint scores. - """ - # Flatten keypoints and scores. - batch_size, _, _, _ = ( - shape_utils.combined_static_and_dynamic_shape(keypoint_coords)) - - # Scale and normalize keypoints. - true_heights, true_widths, _ = tf.unstack(true_image_shapes, axis=1) - yscale = float(stride) / tf.cast(true_heights, tf.float32) - xscale = float(stride) / tf.cast(true_widths, tf.float32) - yx_scale = tf.stack([yscale, xscale], axis=1) - keypoint_coords_normalized = keypoint_coords * tf.reshape( - yx_scale, [batch_size, 1, 1, 2]) - - if clip_out_of_frame_keypoints: - # Determine the keypoints that are in the true image regions. - valid_indices = tf.logical_and( - tf.logical_and(keypoint_coords_normalized[:, :, :, 0] >= 0.0, - keypoint_coords_normalized[:, :, :, 0] <= 1.0), - tf.logical_and(keypoint_coords_normalized[:, :, :, 1] >= 0.0, - keypoint_coords_normalized[:, :, :, 1] <= 1.0)) - batch_window = tf.tile( - tf.constant([[0.0, 0.0, 1.0, 1.0]], dtype=tf.float32), - multiples=[batch_size, 1]) - def clip_to_window(inputs): - keypoints, window = inputs - return keypoint_ops.clip_to_window(keypoints, window) - - keypoint_coords_normalized = shape_utils.static_or_dynamic_map_fn( - clip_to_window, [keypoint_coords_normalized, batch_window], - dtype=tf.float32, back_prop=False) - keypoint_scores = tf.where(valid_indices, keypoint_scores, - tf.zeros_like(keypoint_scores)) - return keypoint_coords_normalized, keypoint_scores - - -def convert_strided_predictions_to_instance_masks( - boxes, classes, masks, true_image_shapes, - densepose_part_heatmap=None, densepose_surface_coords=None, stride=4, - mask_height=256, mask_width=256, score_threshold=0.5, - densepose_class_index=-1): - """Converts predicted full-image masks into instance masks. - - For each predicted detection box: - * Crop and resize the predicted mask (and optionally DensePose coordinates) - based on the detected bounding box coordinates and class prediction. Uses - bilinear resampling. - * Binarize the mask using the provided score threshold. - - Args: - boxes: A tensor of shape [batch, max_detections, 4] holding the predicted - boxes, in normalized coordinates (relative to the true image dimensions). - classes: An integer tensor of shape [batch, max_detections] containing the - detected class for each box (0-indexed). - masks: A [batch, output_height, output_width, num_classes] float32 - tensor with class probabilities. - true_image_shapes: A tensor of shape [batch, 3] representing the true - shape of the inputs not considering padding. - densepose_part_heatmap: (Optional) A [batch, output_height, output_width, - num_parts] float32 tensor with part scores (i.e. logits). - densepose_surface_coords: (Optional) A [batch, output_height, output_width, - 2 * num_parts] float32 tensor with predicted part coordinates (in - vu-format). - stride: The stride in the output space. - mask_height: The desired resized height for instance masks. - mask_width: The desired resized width for instance masks. - score_threshold: The threshold at which to convert predicted mask - into foreground pixels. - densepose_class_index: The class index (0-indexed) corresponding to the - class which has DensePose labels (e.g. person class). - - Returns: - A tuple of masks and surface_coords. - instance_masks: A [batch_size, max_detections, mask_height, mask_width] - uint8 tensor with predicted foreground mask for each - instance. If DensePose tensors are provided, then each pixel value in the - mask encodes the 1-indexed part. - surface_coords: A [batch_size, max_detections, mask_height, mask_width, 2] - float32 tensor with (v, u) coordinates. Note that v, u coordinates are - only defined on instance masks, and the coordinates at each location of - the foreground mask correspond to coordinates on a local part coordinate - system (the specific part can be inferred from the `instance_masks` - output. If DensePose feature maps are not passed to this function, this - output will be None. - - Raises: - ValueError: If one but not both of `densepose_part_heatmap` and - `densepose_surface_coords` is provided. - """ - batch_size, output_height, output_width, _ = ( - shape_utils.combined_static_and_dynamic_shape(masks)) - input_height = stride * output_height - input_width = stride * output_width - - true_heights, true_widths, _ = tf.unstack(true_image_shapes, axis=1) - # If necessary, create dummy DensePose tensors to simplify the map function. - densepose_present = True - if ((densepose_part_heatmap is not None) ^ - (densepose_surface_coords is not None)): - raise ValueError('To use DensePose, both `densepose_part_heatmap` and ' - '`densepose_surface_coords` must be provided') - if densepose_part_heatmap is None and densepose_surface_coords is None: - densepose_present = False - densepose_part_heatmap = tf.zeros( - (batch_size, output_height, output_width, 1), dtype=tf.float32) - densepose_surface_coords = tf.zeros( - (batch_size, output_height, output_width, 2), dtype=tf.float32) - crop_and_threshold_fn = functools.partial( - crop_and_threshold_masks, input_height=input_height, - input_width=input_width, mask_height=mask_height, mask_width=mask_width, - score_threshold=score_threshold, - densepose_class_index=densepose_class_index) - - instance_masks, surface_coords = shape_utils.static_or_dynamic_map_fn( - crop_and_threshold_fn, - elems=[boxes, classes, masks, densepose_part_heatmap, - densepose_surface_coords, true_heights, true_widths], - dtype=[tf.uint8, tf.float32], - back_prop=False) - surface_coords = surface_coords if densepose_present else None - return instance_masks, surface_coords - - -def crop_and_threshold_masks(elems, input_height, input_width, mask_height=256, - mask_width=256, score_threshold=0.5, - densepose_class_index=-1): - """Crops and thresholds masks based on detection boxes. - - Args: - elems: A tuple of - boxes - float32 tensor of shape [max_detections, 4] - classes - int32 tensor of shape [max_detections] (0-indexed) - masks - float32 tensor of shape [output_height, output_width, num_classes] - part_heatmap - float32 tensor of shape [output_height, output_width, - num_parts] - surf_coords - float32 tensor of shape [output_height, output_width, - 2 * num_parts] - true_height - scalar int tensor - true_width - scalar int tensor - input_height: Input height to network. - input_width: Input width to network. - mask_height: Height for resizing mask crops. - mask_width: Width for resizing mask crops. - score_threshold: The threshold at which to convert predicted mask - into foreground pixels. - densepose_class_index: scalar int tensor with the class index (0-indexed) - for DensePose. - - Returns: - A tuple of - all_instances: A [max_detections, mask_height, mask_width] uint8 tensor - with a predicted foreground mask for each instance. Background is encoded - as 0, and foreground is encoded as a positive integer. Specific part - indices are encoded as 1-indexed parts (for classes that have part - information). - surface_coords: A [max_detections, mask_height, mask_width, 2] - float32 tensor with (v, u) coordinates. for each part. - """ - (boxes, classes, masks, part_heatmap, surf_coords, true_height, - true_width) = elems - # Boxes are in normalized coordinates relative to true image shapes. Convert - # coordinates to be normalized relative to input image shapes (since masks - # may still have padding). - boxlist = box_list.BoxList(boxes) - y_scale = true_height / input_height - x_scale = true_width / input_width - boxlist = box_list_ops.scale(boxlist, y_scale, x_scale) - boxes = boxlist.get() - # Convert masks from [output_height, output_width, num_classes] to - # [num_classes, output_height, output_width, 1]. - num_classes = tf.shape(masks)[-1] - masks_4d = tf.transpose(masks, perm=[2, 0, 1])[:, :, :, tf.newaxis] - # Tile part and surface coordinate masks for all classes. - part_heatmap_4d = tf.tile(part_heatmap[tf.newaxis, :, :, :], - multiples=[num_classes, 1, 1, 1]) - surf_coords_4d = tf.tile(surf_coords[tf.newaxis, :, :, :], - multiples=[num_classes, 1, 1, 1]) - feature_maps_concat = tf.concat([masks_4d, part_heatmap_4d, surf_coords_4d], - axis=-1) - # The following tensor has shape - # [max_detections, mask_height, mask_width, 1 + 3 * num_parts]. - cropped_masks = tf2.image.crop_and_resize( - feature_maps_concat, - boxes=boxes, - box_indices=classes, - crop_size=[mask_height, mask_width], - method='bilinear') - - # Split the cropped masks back into instance masks, part masks, and surface - # coordinates. - num_parts = tf.shape(part_heatmap)[-1] - instance_masks, part_heatmap_cropped, surface_coords_cropped = tf.split( - cropped_masks, [1, num_parts, 2 * num_parts], axis=-1) - - # Threshold the instance masks. Resulting tensor has shape - # [max_detections, mask_height, mask_width, 1]. - instance_masks_int = tf.cast( - tf.math.greater_equal(instance_masks, score_threshold), dtype=tf.int32) - - # Produce a binary mask that is 1.0 only: - # - in the foreground region for an instance - # - in detections corresponding to the DensePose class - det_with_parts = tf.equal(classes, densepose_class_index) - det_with_parts = tf.cast( - tf.reshape(det_with_parts, [-1, 1, 1, 1]), dtype=tf.int32) - instance_masks_with_parts = tf.math.multiply(instance_masks_int, - det_with_parts) - - # Similarly, produce a binary mask that holds the foreground masks only for - # instances without parts (i.e. non-DensePose classes). - det_without_parts = 1 - det_with_parts - instance_masks_without_parts = tf.math.multiply(instance_masks_int, - det_without_parts) - - # Assemble a tensor that has standard instance segmentation masks for - # non-DensePose classes (with values in [0, 1]), and part segmentation masks - # for DensePose classes (with vaues in [0, 1, ..., num_parts]). - part_mask_int_zero_indexed = tf.math.argmax( - part_heatmap_cropped, axis=-1, output_type=tf.int32)[:, :, :, tf.newaxis] - part_mask_int_one_indexed = part_mask_int_zero_indexed + 1 - all_instances = (instance_masks_without_parts + - instance_masks_with_parts * part_mask_int_one_indexed) - - # Gather the surface coordinates for the parts. - surface_coords_cropped = tf.reshape( - surface_coords_cropped, [-1, mask_height, mask_width, num_parts, 2]) - surface_coords = gather_surface_coords_for_parts(surface_coords_cropped, - part_mask_int_zero_indexed) - surface_coords = ( - surface_coords * tf.cast(instance_masks_with_parts, tf.float32)) - - return [tf.squeeze(all_instances, axis=3), surface_coords] - - -def gather_surface_coords_for_parts(surface_coords_cropped, - highest_scoring_part): - """Gathers the (v, u) coordinates for the highest scoring DensePose parts. - - Args: - surface_coords_cropped: A [max_detections, height, width, num_parts, 2] - float32 tensor with (v, u) surface coordinates. - highest_scoring_part: A [max_detections, height, width] integer tensor with - the highest scoring part (0-indexed) indices for each location. - - Returns: - A [max_detections, height, width, 2] float32 tensor with the (v, u) - coordinates selected from the highest scoring parts. - """ - max_detections, height, width, num_parts, _ = ( - shape_utils.combined_static_and_dynamic_shape(surface_coords_cropped)) - flattened_surface_coords = tf.reshape(surface_coords_cropped, [-1, 2]) - flattened_part_ids = tf.reshape(highest_scoring_part, [-1]) - - # Produce lookup indices that represent the locations of the highest scoring - # parts in the `flattened_surface_coords` tensor. - flattened_lookup_indices = ( - num_parts * tf.range(max_detections * height * width) + - flattened_part_ids) - - vu_coords_flattened = tf.gather(flattened_surface_coords, - flattened_lookup_indices, axis=0) - return tf.reshape(vu_coords_flattened, [max_detections, height, width, 2]) - - -def predicted_embeddings_at_object_centers(embedding_predictions, - y_indices, x_indices): - """Returns the predicted embeddings at specified object centers. - - Args: - embedding_predictions: A float tensor of shape [batch_size, height, width, - reid_embed_size] holding predicted embeddings. - y_indices: A [batch, num_instances] int tensor holding y indices for object - centers. These indices correspond to locations in the output feature map. - x_indices: A [batch, num_instances] int tensor holding x indices for object - centers. These indices correspond to locations in the output feature map. - - Returns: - A float tensor of shape [batch_size, num_objects, reid_embed_size] where - predicted embeddings are gathered at the provided locations. - """ - batch_size, _, width, _ = _get_shape(embedding_predictions, 4) - flattened_indices = flattened_indices_from_row_col_indices( - y_indices, x_indices, width) - _, num_instances = _get_shape(flattened_indices, 2) - embeddings_flat = _flatten_spatial_dimensions(embedding_predictions) - embeddings = tf.gather(embeddings_flat, flattened_indices, batch_dims=1) - embeddings = tf.reshape(embeddings, [batch_size, num_instances, -1]) - - return embeddings - - -def mask_from_true_image_shape(data_shape, true_image_shapes): - """Get a binary mask based on the true_image_shape. - - Args: - data_shape: a possibly static (4,) tensor for the shape of the feature - map. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is of - the form [height, width, channels] indicating the shapes of true - images in the resized images, as resized images can be padded with - zeros. - Returns: - a [batch, data_height, data_width, 1] tensor of 1.0 wherever data_height - is less than height, etc. - """ - mask_h = tf.cast( - tf.range(data_shape[1]) < true_image_shapes[:, tf.newaxis, 0], - tf.float32) - mask_w = tf.cast( - tf.range(data_shape[2]) < true_image_shapes[:, tf.newaxis, 1], - tf.float32) - mask = tf.expand_dims( - mask_h[:, :, tf.newaxis] * mask_w[:, tf.newaxis, :], 3) - return mask - - -class ObjectDetectionParams( - collections.namedtuple('ObjectDetectionParams', [ - 'localization_loss', 'scale_loss_weight', 'offset_loss_weight', - 'task_loss_weight', 'scale_head_num_filters', - 'scale_head_kernel_sizes', 'offset_head_num_filters', - 'offset_head_kernel_sizes' - ])): - """Namedtuple to host object detection related parameters. - - This is a wrapper class over the fields that are either the hyper-parameters - or the loss functions needed for the object detection task. The class is - immutable after constructed. Please see the __new__ function for detailed - information for each fields. - """ - - __slots__ = () - - def __new__(cls, - localization_loss, - scale_loss_weight, - offset_loss_weight, - task_loss_weight=1.0, - scale_head_num_filters=(256), - scale_head_kernel_sizes=(3), - offset_head_num_filters=(256), - offset_head_kernel_sizes=(3)): - """Constructor with default values for ObjectDetectionParams. - - Args: - localization_loss: a object_detection.core.losses.Loss object to compute - the loss for the center offset and height/width predictions in - CenterNet. - scale_loss_weight: float, The weight for localizing box size. Note that - the scale loss is dependent on the input image size, since we penalize - the raw height and width. This constant may need to be adjusted - depending on the input size. - offset_loss_weight: float, The weight for localizing center offsets. - task_loss_weight: float, the weight of the object detection loss. - scale_head_num_filters: filter numbers of the convolutional layers used - by the object detection box scale prediction head. - scale_head_kernel_sizes: kernel size of the convolutional layers used - by the object detection box scale prediction head. - offset_head_num_filters: filter numbers of the convolutional layers used - by the object detection box offset prediction head. - offset_head_kernel_sizes: kernel size of the convolutional layers used - by the object detection box offset prediction head. - - Returns: - An initialized ObjectDetectionParams namedtuple. - """ - return super(ObjectDetectionParams, - cls).__new__(cls, localization_loss, scale_loss_weight, - offset_loss_weight, task_loss_weight, - scale_head_num_filters, scale_head_kernel_sizes, - offset_head_num_filters, offset_head_kernel_sizes) - - -class KeypointEstimationParams( - collections.namedtuple('KeypointEstimationParams', [ - 'task_name', 'class_id', 'keypoint_indices', 'classification_loss', - 'localization_loss', 'keypoint_labels', 'keypoint_std_dev', - 'keypoint_heatmap_loss_weight', 'keypoint_offset_loss_weight', - 'keypoint_regression_loss_weight', 'keypoint_candidate_score_threshold', - 'heatmap_bias_init', 'num_candidates_per_keypoint', 'task_loss_weight', - 'peak_max_pool_kernel_size', 'unmatched_keypoint_score', 'box_scale', - 'candidate_search_scale', 'candidate_ranking_mode', - 'offset_peak_radius', 'per_keypoint_offset', 'predict_depth', - 'per_keypoint_depth', 'keypoint_depth_loss_weight', - 'score_distance_offset', 'clip_out_of_frame_keypoints', - 'rescore_instances', 'heatmap_head_num_filters', - 'heatmap_head_kernel_sizes', 'offset_head_num_filters', - 'offset_head_kernel_sizes', 'regress_head_num_filters', - 'regress_head_kernel_sizes', 'score_distance_multiplier', - 'std_dev_multiplier', 'rescoring_threshold', 'gaussian_denom_ratio', - 'argmax_postprocessing' - ])): - """Namedtuple to host object detection related parameters. - - This is a wrapper class over the fields that are either the hyper-parameters - or the loss functions needed for the keypoint estimation task. The class is - immutable after constructed. Please see the __new__ function for detailed - information for each fields. - """ - - __slots__ = () - - def __new__(cls, - task_name, - class_id, - keypoint_indices, - classification_loss, - localization_loss, - keypoint_labels=None, - keypoint_std_dev=None, - keypoint_heatmap_loss_weight=1.0, - keypoint_offset_loss_weight=1.0, - keypoint_regression_loss_weight=1.0, - keypoint_candidate_score_threshold=0.1, - heatmap_bias_init=-2.19, - num_candidates_per_keypoint=100, - task_loss_weight=1.0, - peak_max_pool_kernel_size=3, - unmatched_keypoint_score=0.1, - box_scale=1.2, - candidate_search_scale=0.3, - candidate_ranking_mode='min_distance', - offset_peak_radius=0, - per_keypoint_offset=False, - predict_depth=False, - per_keypoint_depth=False, - keypoint_depth_loss_weight=1.0, - score_distance_offset=1e-6, - clip_out_of_frame_keypoints=False, - rescore_instances=False, - heatmap_head_num_filters=(256), - heatmap_head_kernel_sizes=(3), - offset_head_num_filters=(256), - offset_head_kernel_sizes=(3), - regress_head_num_filters=(256), - regress_head_kernel_sizes=(3), - score_distance_multiplier=0.1, - std_dev_multiplier=1.0, - rescoring_threshold=0.0, - argmax_postprocessing=False, - gaussian_denom_ratio=0.1): - """Constructor with default values for KeypointEstimationParams. - - Args: - task_name: string, the name of the task this namedtuple corresponds to. - Note that it should be an unique identifier of the task. - class_id: int, the ID of the class that contains the target keypoints to - considered in this task. For example, if the task is human pose - estimation, the class id should correspond to the "human" class. Note - that the ID is 0-based, meaning that class 0 corresponds to the first - non-background object class. - keypoint_indices: A list of integers representing the indicies of the - keypoints to be considered in this task. This is used to retrieve the - subset of the keypoints from gt_keypoints that should be considered in - this task. - classification_loss: an object_detection.core.losses.Loss object to - compute the loss for the class predictions in CenterNet. - localization_loss: an object_detection.core.losses.Loss object to compute - the loss for the center offset and height/width predictions in - CenterNet. - keypoint_labels: A list of strings representing the label text of each - keypoint, e.g. "nose", 'left_shoulder". Note that the length of this - list should be equal to keypoint_indices. - keypoint_std_dev: A list of float represent the standard deviation of the - Gaussian kernel used to generate the keypoint heatmap. It is to provide - the flexibility of using different sizes of Gaussian kernel for each - keypoint class. - keypoint_heatmap_loss_weight: float, The weight for the keypoint heatmap. - keypoint_offset_loss_weight: float, The weight for the keypoint offsets - loss. - keypoint_regression_loss_weight: float, The weight for keypoint regression - loss. Note that the loss is dependent on the input image size, since we - penalize the raw height and width. This constant may need to be adjusted - depending on the input size. - keypoint_candidate_score_threshold: float, The heatmap score threshold for - a keypoint to become a valid candidate. - heatmap_bias_init: float, the initial value of bias in the convolutional - kernel of the class prediction head. If set to None, the bias is - initialized with zeros. - num_candidates_per_keypoint: The maximum number of candidates to retrieve - for each keypoint. - task_loss_weight: float, the weight of the keypoint estimation loss. - peak_max_pool_kernel_size: Max pool kernel size to use to pull off peak - score locations in a neighborhood (independently for each keypoint - types). - unmatched_keypoint_score: The default score to use for regressed keypoints - that are not successfully snapped to a nearby candidate. - box_scale: The multiplier to expand the bounding boxes (either the - provided boxes or those which tightly cover the regressed keypoints). - candidate_search_scale: The scale parameter that multiplies the largest - dimension of a bounding box. The resulting distance becomes a search - radius for candidates in the vicinity of each regressed keypoint. - candidate_ranking_mode: One of ['min_distance', 'score_distance_ratio', - 'score_scaled_distance_ratio', 'gaussian_weighted'] indicating how to - select the keypoint candidate. - offset_peak_radius: The radius (in the unit of output pixel) around - groundtruth heatmap peak to assign the offset targets. If set 0, then - the offset target will only be assigned to the heatmap peak (same - behavior as the original paper). - per_keypoint_offset: A bool indicates whether to assign offsets for each - keypoint channel separately. If set False, the output offset target has - the shape [batch_size, out_height, out_width, 2] (same behavior as the - original paper). If set True, the output offset target has the shape - [batch_size, out_height, out_width, 2 * num_keypoints] (recommended when - the offset_peak_radius is not zero). - predict_depth: A bool indicates whether to predict the depth of each - keypoints. - per_keypoint_depth: A bool indicates whether the model predicts the depth - of each keypoints in independent channels. Similar to - per_keypoint_offset but for the keypoint depth. - keypoint_depth_loss_weight: The weight of the keypoint depth loss. - score_distance_offset: The distance offset to apply in the denominator - when candidate_ranking_mode is 'score_distance_ratio'. The metric to - maximize in this scenario is score / (distance + score_distance_offset). - Larger values of score_distance_offset make the keypoint score gain more - relative importance. - clip_out_of_frame_keypoints: Whether keypoints outside the image frame - should be clipped back to the image boundary. If True, the keypoints - that are clipped have scores set to 0.0. - rescore_instances: Whether to rescore instances based on a combination of - detection score and keypoint scores. - heatmap_head_num_filters: filter numbers of the convolutional layers used - by the keypoint heatmap prediction head. - heatmap_head_kernel_sizes: kernel size of the convolutional layers used - by the keypoint heatmap prediction head. - offset_head_num_filters: filter numbers of the convolutional layers used - by the keypoint offset prediction head. - offset_head_kernel_sizes: kernel size of the convolutional layers used - by the keypoint offset prediction head. - regress_head_num_filters: filter numbers of the convolutional layers used - by the keypoint regression prediction head. - regress_head_kernel_sizes: kernel size of the convolutional layers used - by the keypoint regression prediction head. - score_distance_multiplier: A scalar used to multiply the bounding box size - to be used as the offset in the score-to-distance-ratio formula. - std_dev_multiplier: A scalar used to multiply the standard deviation to - control the Gaussian kernel which used to weight the candidates. - rescoring_threshold: A scalar used when "rescore_instances" is set to - True. The detection score of an instance is set to be the average over - the scores of the keypoints which their scores higher than the - threshold. - argmax_postprocessing: Whether to use the keypoint postprocessing logic - that replaces the topk op with argmax. Usually used when exporting the - model for predicting keypoints of multiple instances in the browser. - gaussian_denom_ratio: The ratio used to multiply the image size to - determine the denominator of the Gaussian formula. Only applicable when - the candidate_ranking_mode is set to be 'gaussian_weighted_const'. - - Returns: - An initialized KeypointEstimationParams namedtuple. - """ - return super(KeypointEstimationParams, cls).__new__( - cls, task_name, class_id, keypoint_indices, classification_loss, - localization_loss, keypoint_labels, keypoint_std_dev, - keypoint_heatmap_loss_weight, keypoint_offset_loss_weight, - keypoint_regression_loss_weight, keypoint_candidate_score_threshold, - heatmap_bias_init, num_candidates_per_keypoint, task_loss_weight, - peak_max_pool_kernel_size, unmatched_keypoint_score, box_scale, - candidate_search_scale, candidate_ranking_mode, offset_peak_radius, - per_keypoint_offset, predict_depth, per_keypoint_depth, - keypoint_depth_loss_weight, score_distance_offset, - clip_out_of_frame_keypoints, rescore_instances, - heatmap_head_num_filters, heatmap_head_kernel_sizes, - offset_head_num_filters, offset_head_kernel_sizes, - regress_head_num_filters, regress_head_kernel_sizes, - score_distance_multiplier, std_dev_multiplier, rescoring_threshold, - argmax_postprocessing, gaussian_denom_ratio) - - -class ObjectCenterParams( - collections.namedtuple('ObjectCenterParams', [ - 'classification_loss', 'object_center_loss_weight', 'heatmap_bias_init', - 'min_box_overlap_iou', 'max_box_predictions', 'use_labeled_classes', - 'keypoint_weights_for_center', 'center_head_num_filters', - 'center_head_kernel_sizes', 'peak_max_pool_kernel_size' - ])): - """Namedtuple to store object center prediction related parameters.""" - - __slots__ = () - - def __new__(cls, - classification_loss, - object_center_loss_weight, - heatmap_bias_init=-2.19, - min_box_overlap_iou=0.7, - max_box_predictions=100, - use_labeled_classes=False, - keypoint_weights_for_center=None, - center_head_num_filters=(256), - center_head_kernel_sizes=(3), - peak_max_pool_kernel_size=3): - """Constructor with default values for ObjectCenterParams. - - Args: - classification_loss: an object_detection.core.losses.Loss object to - compute the loss for the class predictions in CenterNet. - object_center_loss_weight: float, The weight for the object center loss. - heatmap_bias_init: float, the initial value of bias in the convolutional - kernel of the object center prediction head. If set to None, the bias is - initialized with zeros. - min_box_overlap_iou: float, the minimum IOU overlap that predicted boxes - need have with groundtruth boxes to not be penalized. This is used for - computing the class specific center heatmaps. - max_box_predictions: int, the maximum number of boxes to predict. - use_labeled_classes: boolean, compute the loss only labeled classes. - keypoint_weights_for_center: (optional) The keypoint weights used for - calculating the location of object center. If provided, the number of - weights need to be the same as the number of keypoints. The object - center is calculated by the weighted mean of the keypoint locations. If - not provided, the object center is determined by the center of the - bounding box (default behavior). - center_head_num_filters: filter numbers of the convolutional layers used - by the object center prediction head. - center_head_kernel_sizes: kernel size of the convolutional layers used - by the object center prediction head. - peak_max_pool_kernel_size: Max pool kernel size to use to pull off peak - score locations in a neighborhood for the object detection heatmap. - Returns: - An initialized ObjectCenterParams namedtuple. - """ - return super(ObjectCenterParams, - cls).__new__(cls, classification_loss, - object_center_loss_weight, heatmap_bias_init, - min_box_overlap_iou, max_box_predictions, - use_labeled_classes, keypoint_weights_for_center, - center_head_num_filters, center_head_kernel_sizes, - peak_max_pool_kernel_size) - - -class MaskParams( - collections.namedtuple('MaskParams', [ - 'classification_loss', 'task_loss_weight', 'mask_height', 'mask_width', - 'score_threshold', 'heatmap_bias_init', 'mask_head_num_filters', - 'mask_head_kernel_sizes' - ])): - """Namedtuple to store mask prediction related parameters.""" - - __slots__ = () - - def __new__(cls, - classification_loss, - task_loss_weight=1.0, - mask_height=256, - mask_width=256, - score_threshold=0.5, - heatmap_bias_init=-2.19, - mask_head_num_filters=(256), - mask_head_kernel_sizes=(3)): - """Constructor with default values for MaskParams. - - Args: - classification_loss: an object_detection.core.losses.Loss object to - compute the loss for the semantic segmentation predictions in CenterNet. - task_loss_weight: float, The loss weight for the segmentation task. - mask_height: The height of the resized instance segmentation mask. - mask_width: The width of the resized instance segmentation mask. - score_threshold: The threshold at which to convert predicted mask - probabilities (after passing through sigmoid) into foreground pixels. - heatmap_bias_init: float, the initial value of bias in the convolutional - kernel of the semantic segmentation prediction head. If set to None, the - bias is initialized with zeros. - mask_head_num_filters: filter numbers of the convolutional layers used - by the mask prediction head. - mask_head_kernel_sizes: kernel size of the convolutional layers used - by the mask prediction head. - - Returns: - An initialized MaskParams namedtuple. - """ - return super(MaskParams, - cls).__new__(cls, classification_loss, - task_loss_weight, mask_height, mask_width, - score_threshold, heatmap_bias_init, - mask_head_num_filters, mask_head_kernel_sizes) - - -class DensePoseParams( - collections.namedtuple('DensePoseParams', [ - 'class_id', 'classification_loss', 'localization_loss', - 'part_loss_weight', 'coordinate_loss_weight', 'num_parts', - 'task_loss_weight', 'upsample_to_input_res', 'upsample_method', - 'heatmap_bias_init' - ])): - """Namedtuple to store DensePose prediction related parameters.""" - - __slots__ = () - - def __new__(cls, - class_id, - classification_loss, - localization_loss, - part_loss_weight=1.0, - coordinate_loss_weight=1.0, - num_parts=24, - task_loss_weight=1.0, - upsample_to_input_res=True, - upsample_method='bilinear', - heatmap_bias_init=-2.19): - """Constructor with default values for DensePoseParams. - - Args: - class_id: the ID of the class that contains the DensePose groundtruth. - This should typically correspond to the "person" class. Note that the ID - is 0-based, meaning that class 0 corresponds to the first non-background - object class. - classification_loss: an object_detection.core.losses.Loss object to - compute the loss for the body part predictions in CenterNet. - localization_loss: an object_detection.core.losses.Loss object to compute - the loss for the surface coordinate regression in CenterNet. - part_loss_weight: The loss weight to apply to part prediction. - coordinate_loss_weight: The loss weight to apply to surface coordinate - prediction. - num_parts: The number of DensePose parts to predict. - task_loss_weight: float, the loss weight for the DensePose task. - upsample_to_input_res: Whether to upsample the DensePose feature maps to - the input resolution before applying loss. Note that the prediction - outputs are still at the standard CenterNet output stride. - upsample_method: Method for upsampling DensePose feature maps. Options are - either 'bilinear' or 'nearest'). This takes no effect when - `upsample_to_input_res` is False. - heatmap_bias_init: float, the initial value of bias in the convolutional - kernel of the part prediction head. If set to None, the - bias is initialized with zeros. - - Returns: - An initialized DensePoseParams namedtuple. - """ - return super(DensePoseParams, - cls).__new__(cls, class_id, classification_loss, - localization_loss, part_loss_weight, - coordinate_loss_weight, num_parts, - task_loss_weight, upsample_to_input_res, - upsample_method, heatmap_bias_init) - - -class TrackParams( - collections.namedtuple('TrackParams', [ - 'num_track_ids', 'reid_embed_size', 'num_fc_layers', - 'classification_loss', 'task_loss_weight' - ])): - """Namedtuple to store tracking prediction related parameters.""" - - __slots__ = () - - def __new__(cls, - num_track_ids, - reid_embed_size, - num_fc_layers, - classification_loss, - task_loss_weight=1.0): - """Constructor with default values for TrackParams. - - Args: - num_track_ids: int. The maximum track ID in the dataset. Used for ReID - embedding classification task. - reid_embed_size: int. The embedding size for ReID task. - num_fc_layers: int. The number of (fully-connected, batch-norm, relu) - layers for track ID classification head. - classification_loss: an object_detection.core.losses.Loss object to - compute the loss for the ReID embedding in CenterNet. - task_loss_weight: float, the loss weight for the tracking task. - - Returns: - An initialized TrackParams namedtuple. - """ - return super(TrackParams, - cls).__new__(cls, num_track_ids, reid_embed_size, - num_fc_layers, classification_loss, - task_loss_weight) - - -class TemporalOffsetParams( - collections.namedtuple('TemporalOffsetParams', [ - 'localization_loss', 'task_loss_weight' - ])): - """Namedtuple to store temporal offset related parameters.""" - - __slots__ = () - - def __new__(cls, - localization_loss, - task_loss_weight=1.0): - """Constructor with default values for TrackParams. - - Args: - localization_loss: an object_detection.core.losses.Loss object to - compute the loss for the temporal offset in CenterNet. - task_loss_weight: float, the loss weight for the temporal offset - task. - - Returns: - An initialized TemporalOffsetParams namedtuple. - """ - return super(TemporalOffsetParams, - cls).__new__(cls, localization_loss, task_loss_weight) - -# The following constants are used to generate the keys of the -# (prediction, loss, target assigner,...) dictionaries used in CenterNetMetaArch -# class. -DETECTION_TASK = 'detection_task' -OBJECT_CENTER = 'object_center' -BOX_SCALE = 'box/scale' -BOX_OFFSET = 'box/offset' -KEYPOINT_REGRESSION = 'keypoint/regression' -KEYPOINT_HEATMAP = 'keypoint/heatmap' -KEYPOINT_OFFSET = 'keypoint/offset' -KEYPOINT_DEPTH = 'keypoint/depth' -SEGMENTATION_TASK = 'segmentation_task' -SEGMENTATION_HEATMAP = 'segmentation/heatmap' -DENSEPOSE_TASK = 'densepose_task' -DENSEPOSE_HEATMAP = 'densepose/heatmap' -DENSEPOSE_REGRESSION = 'densepose/regression' -LOSS_KEY_PREFIX = 'Loss' -TRACK_TASK = 'track_task' -TRACK_REID = 'track/reid' -TEMPORALOFFSET_TASK = 'temporal_offset_task' -TEMPORAL_OFFSET = 'track/offset' - - -def get_keypoint_name(task_name, head_name): - return '%s/%s' % (task_name, head_name) - - -def get_num_instances_from_weights(groundtruth_weights_list): - """Computes the number of instances/boxes from the weights in a batch. - - Args: - groundtruth_weights_list: A list of float tensors with shape - [max_num_instances] representing whether there is an actual instance in - the image (with non-zero value) or is padded to match the - max_num_instances (with value 0.0). The list represents the batch - dimension. - - Returns: - A scalar integer tensor incidating how many instances/boxes are in the - images in the batch. Note that this function is usually used to normalize - the loss so the minimum return value is 1 to avoid weird behavior. - """ - num_instances = tf.reduce_sum( - [tf.math.count_nonzero(w) for w in groundtruth_weights_list]) - num_instances = tf.maximum(num_instances, 1) - return num_instances - - -class CenterNetMetaArch(model.DetectionModel): - """The CenterNet meta architecture [1]. - - [1]: https://arxiv.org/abs/1904.07850 - """ - - def __init__(self, - is_training, - add_summaries, - num_classes, - feature_extractor, - image_resizer_fn, - object_center_params, - object_detection_params=None, - keypoint_params_dict=None, - mask_params=None, - densepose_params=None, - track_params=None, - temporal_offset_params=None, - use_depthwise=False, - compute_heatmap_sparse=False, - non_max_suppression_fn=None, - unit_height_conv=False, - output_prediction_dict=False): - """Initializes a CenterNet model. - - Args: - is_training: Set to True if this model is being built for training. - add_summaries: Whether to add tf summaries in the model. - num_classes: int, The number of classes that the model should predict. - feature_extractor: A CenterNetFeatureExtractor to use to extract features - from an image. - image_resizer_fn: a callable for image resizing. This callable always - takes a rank-3 image tensor (corresponding to a single image) and - returns a rank-3 image tensor, possibly with new spatial dimensions and - a 1-D tensor of shape [3] indicating shape of true image within the - resized image tensor as the resized image tensor could be padded. See - builders/image_resizer_builder.py. - object_center_params: An ObjectCenterParams namedtuple. This object holds - the hyper-parameters for object center prediction. This is required by - either object detection or keypoint estimation tasks. - object_detection_params: An ObjectDetectionParams namedtuple. This object - holds the hyper-parameters necessary for object detection. Please see - the class definition for more details. - keypoint_params_dict: A dictionary that maps from task name to the - corresponding KeypointEstimationParams namedtuple. This object holds the - hyper-parameters necessary for multiple keypoint estimations. Please - see the class definition for more details. - mask_params: A MaskParams namedtuple. This object - holds the hyper-parameters for segmentation. Please see the class - definition for more details. - densepose_params: A DensePoseParams namedtuple. This object holds the - hyper-parameters for DensePose prediction. Please see the class - definition for more details. Note that if this is provided, it is - expected that `mask_params` is also provided. - track_params: A TrackParams namedtuple. This object - holds the hyper-parameters for tracking. Please see the class - definition for more details. - temporal_offset_params: A TemporalOffsetParams namedtuple. This object - holds the hyper-parameters for offset prediction based tracking. - use_depthwise: If true, all task heads will be constructed using - separable_conv. Otherwise, standard convoltuions will be used. - compute_heatmap_sparse: bool, whether or not to use the sparse version of - the Op that computes the center heatmaps. The sparse version scales - better with number of channels in the heatmap, but in some cases is - known to cause an OOM error. See b/170989061. - non_max_suppression_fn: Optional Non Max Suppression function to apply. - unit_height_conv: If True, Conv2Ds in prediction heads have asymmetric - kernels with height=1. - output_prediction_dict: If true, combines all items from the dictionary - returned by predict() function into the output of postprocess(). - """ - assert object_detection_params or keypoint_params_dict - # Shorten the name for convenience and better formatting. - self._is_training = is_training - # The Objects as Points paper attaches loss functions to multiple - # (`num_feature_outputs`) feature maps in the the backbone. E.g. - # for the hourglass backbone, `num_feature_outputs` is 2. - self._num_classes = num_classes - self._feature_extractor = feature_extractor - self._num_feature_outputs = feature_extractor.num_feature_outputs - self._stride = self._feature_extractor.out_stride - self._image_resizer_fn = image_resizer_fn - self._center_params = object_center_params - self._od_params = object_detection_params - self._kp_params_dict = keypoint_params_dict - self._mask_params = mask_params - if densepose_params is not None and mask_params is None: - raise ValueError('To run DensePose prediction, `mask_params` must also ' - 'be supplied.') - self._densepose_params = densepose_params - self._track_params = track_params - self._temporal_offset_params = temporal_offset_params - - self._use_depthwise = use_depthwise - self._compute_heatmap_sparse = compute_heatmap_sparse - self._output_prediction_dict = output_prediction_dict - - # subclasses may not implement the unit_height_conv arg, so only provide it - # as a kwarg if it is True. - kwargs = {'unit_height_conv': unit_height_conv} if unit_height_conv else {} - # Construct the prediction head nets. - self._prediction_head_dict = self._construct_prediction_heads( - num_classes, - self._num_feature_outputs, - class_prediction_bias_init=self._center_params.heatmap_bias_init, - **kwargs) - # Initialize the target assigners. - self._target_assigner_dict = self._initialize_target_assigners( - stride=self._stride, - min_box_overlap_iou=self._center_params.min_box_overlap_iou) - - # Will be used in VOD single_frame_meta_arch for tensor reshape. - self._batched_prediction_tensor_names = [] - self._non_max_suppression_fn = non_max_suppression_fn - - super(CenterNetMetaArch, self).__init__(num_classes) - - def set_trainability_by_layer_traversal(self, trainable): - """Sets trainability layer by layer. - - The commonly-seen `model.trainable = False` method does not traverse - the children layer. For example, if the parent is not trainable, we won't - be able to set individual layers as trainable/non-trainable differentially. - - Args: - trainable: (bool) Setting this for the model layer by layer except for - the parent itself. - """ - for layer in self._flatten_layers(include_self=False): - layer.trainable = trainable - - @property - def prediction_head_dict(self): - return self._prediction_head_dict - - @property - def batched_prediction_tensor_names(self): - if not self._batched_prediction_tensor_names: - raise RuntimeError('Must call predict() method to get batched prediction ' - 'tensor names.') - return self._batched_prediction_tensor_names - - def _make_prediction_net_list(self, num_feature_outputs, num_out_channels, - kernel_sizes=(3), num_filters=(256), - bias_fill=None, name=None, - unit_height_conv=False): - prediction_net_list = [] - for i in range(num_feature_outputs): - prediction_net_list.append( - make_prediction_net( - num_out_channels, - kernel_sizes=kernel_sizes, - num_filters=num_filters, - bias_fill=bias_fill, - use_depthwise=self._use_depthwise, - name='{}_{}'.format(name, i) if name else name, - unit_height_conv=unit_height_conv)) - return prediction_net_list - - def _construct_prediction_heads(self, num_classes, num_feature_outputs, - class_prediction_bias_init, - unit_height_conv=False): - """Constructs the prediction heads based on the specific parameters. - - Args: - num_classes: An integer indicating how many classes in total to predict. - num_feature_outputs: An integer indicating how many feature outputs to use - for calculating the loss. The Objects as Points paper attaches loss - functions to multiple (`num_feature_outputs`) feature maps in the the - backbone. E.g. for the hourglass backbone, `num_feature_outputs` is 2. - class_prediction_bias_init: float, the initial value of bias in the - convolutional kernel of the class prediction head. If set to None, the - bias is initialized with zeros. - unit_height_conv: If True, Conv2Ds have asymmetric kernels with height=1. - - Returns: - A dictionary of keras modules generated by calling make_prediction_net - function. It will also create and set a private member of the class when - learning the tracking task. - """ - prediction_heads = {} - prediction_heads[OBJECT_CENTER] = self._make_prediction_net_list( - num_feature_outputs, - num_classes, - kernel_sizes=self._center_params.center_head_kernel_sizes, - num_filters=self._center_params.center_head_num_filters, - bias_fill=class_prediction_bias_init, - name='center', - unit_height_conv=unit_height_conv) - - if self._od_params is not None: - prediction_heads[BOX_SCALE] = self._make_prediction_net_list( - num_feature_outputs, - NUM_SIZE_CHANNELS, - kernel_sizes=self._od_params.scale_head_kernel_sizes, - num_filters=self._od_params.scale_head_num_filters, - name='box_scale', - unit_height_conv=unit_height_conv) - prediction_heads[BOX_OFFSET] = self._make_prediction_net_list( - num_feature_outputs, - NUM_OFFSET_CHANNELS, - kernel_sizes=self._od_params.offset_head_kernel_sizes, - num_filters=self._od_params.offset_head_num_filters, - name='box_offset', - unit_height_conv=unit_height_conv) - - if self._kp_params_dict is not None: - for task_name, kp_params in self._kp_params_dict.items(): - num_keypoints = len(kp_params.keypoint_indices) - prediction_heads[get_keypoint_name( - task_name, KEYPOINT_HEATMAP)] = self._make_prediction_net_list( - num_feature_outputs, - num_keypoints, - kernel_sizes=kp_params.heatmap_head_kernel_sizes, - num_filters=kp_params.heatmap_head_num_filters, - bias_fill=kp_params.heatmap_bias_init, - name='kpt_heatmap', - unit_height_conv=unit_height_conv) - prediction_heads[get_keypoint_name( - task_name, KEYPOINT_REGRESSION)] = self._make_prediction_net_list( - num_feature_outputs, - NUM_OFFSET_CHANNELS * num_keypoints, - kernel_sizes=kp_params.regress_head_kernel_sizes, - num_filters=kp_params.regress_head_num_filters, - name='kpt_regress', - unit_height_conv=unit_height_conv) - - if kp_params.per_keypoint_offset: - prediction_heads[get_keypoint_name( - task_name, KEYPOINT_OFFSET)] = self._make_prediction_net_list( - num_feature_outputs, - NUM_OFFSET_CHANNELS * num_keypoints, - kernel_sizes=kp_params.offset_head_kernel_sizes, - num_filters=kp_params.offset_head_num_filters, - name='kpt_offset', - unit_height_conv=unit_height_conv) - else: - prediction_heads[get_keypoint_name( - task_name, KEYPOINT_OFFSET)] = self._make_prediction_net_list( - num_feature_outputs, - NUM_OFFSET_CHANNELS, - kernel_sizes=kp_params.offset_head_kernel_sizes, - num_filters=kp_params.offset_head_num_filters, - name='kpt_offset', - unit_height_conv=unit_height_conv) - - if kp_params.predict_depth: - num_depth_channel = ( - num_keypoints if kp_params.per_keypoint_depth else 1) - prediction_heads[get_keypoint_name( - task_name, KEYPOINT_DEPTH)] = self._make_prediction_net_list( - num_feature_outputs, num_depth_channel, name='kpt_depth', - unit_height_conv=unit_height_conv) - - if self._mask_params is not None: - prediction_heads[SEGMENTATION_HEATMAP] = self._make_prediction_net_list( - num_feature_outputs, - num_classes, - kernel_sizes=self._mask_params.mask_head_kernel_sizes, - num_filters=self._mask_params.mask_head_num_filters, - bias_fill=self._mask_params.heatmap_bias_init, - name='seg_heatmap', - unit_height_conv=unit_height_conv) - - if self._densepose_params is not None: - prediction_heads[DENSEPOSE_HEATMAP] = self._make_prediction_net_list( - num_feature_outputs, - self._densepose_params.num_parts, - bias_fill=self._densepose_params.heatmap_bias_init, - name='dense_pose_heatmap', - unit_height_conv=unit_height_conv) - prediction_heads[DENSEPOSE_REGRESSION] = self._make_prediction_net_list( - num_feature_outputs, - 2 * self._densepose_params.num_parts, - name='dense_pose_regress', - unit_height_conv=unit_height_conv) - - if self._track_params is not None: - prediction_heads[TRACK_REID] = self._make_prediction_net_list( - num_feature_outputs, - self._track_params.reid_embed_size, - name='track_reid', - unit_height_conv=unit_height_conv) - - # Creates a classification network to train object embeddings by learning - # a projection from embedding space to object track ID space. - self.track_reid_classification_net = tf.keras.Sequential() - for _ in range(self._track_params.num_fc_layers - 1): - self.track_reid_classification_net.add( - tf.keras.layers.Dense(self._track_params.reid_embed_size)) - self.track_reid_classification_net.add( - tf.keras.layers.BatchNormalization()) - self.track_reid_classification_net.add(tf.keras.layers.ReLU()) - self.track_reid_classification_net.add( - tf.keras.layers.Dense(self._track_params.num_track_ids)) - if self._temporal_offset_params is not None: - prediction_heads[TEMPORAL_OFFSET] = self._make_prediction_net_list( - num_feature_outputs, NUM_OFFSET_CHANNELS, name='temporal_offset', - unit_height_conv=unit_height_conv) - return prediction_heads - - def _initialize_target_assigners(self, stride, min_box_overlap_iou): - """Initializes the target assigners and puts them in a dictionary. - - Args: - stride: An integer indicating the stride of the image. - min_box_overlap_iou: float, the minimum IOU overlap that predicted boxes - need have with groundtruth boxes to not be penalized. This is used for - computing the class specific center heatmaps. - - Returns: - A dictionary of initialized target assigners for each task. - """ - target_assigners = {} - keypoint_weights_for_center = ( - self._center_params.keypoint_weights_for_center) - if not keypoint_weights_for_center: - target_assigners[OBJECT_CENTER] = ( - cn_assigner.CenterNetCenterHeatmapTargetAssigner( - stride, min_box_overlap_iou, self._compute_heatmap_sparse)) - self._center_from_keypoints = False - else: - # Determining the object center location by keypoint location is only - # supported when there is exactly one keypoint prediction task and no - # object detection task is specified. - assert len(self._kp_params_dict) == 1 and self._od_params is None - kp_params = next(iter(self._kp_params_dict.values())) - # The number of keypoint_weights_for_center needs to be the same as the - # number of keypoints. - assert len(keypoint_weights_for_center) == len(kp_params.keypoint_indices) - target_assigners[OBJECT_CENTER] = ( - cn_assigner.CenterNetCenterHeatmapTargetAssigner( - stride, - min_box_overlap_iou, - self._compute_heatmap_sparse, - keypoint_class_id=kp_params.class_id, - keypoint_indices=kp_params.keypoint_indices, - keypoint_weights_for_center=keypoint_weights_for_center)) - self._center_from_keypoints = True - if self._od_params is not None: - target_assigners[DETECTION_TASK] = ( - cn_assigner.CenterNetBoxTargetAssigner(stride)) - if self._kp_params_dict is not None: - for task_name, kp_params in self._kp_params_dict.items(): - target_assigners[task_name] = ( - cn_assigner.CenterNetKeypointTargetAssigner( - stride=stride, - class_id=kp_params.class_id, - keypoint_indices=kp_params.keypoint_indices, - keypoint_std_dev=kp_params.keypoint_std_dev, - peak_radius=kp_params.offset_peak_radius, - per_keypoint_offset=kp_params.per_keypoint_offset, - compute_heatmap_sparse=self._compute_heatmap_sparse, - per_keypoint_depth=kp_params.per_keypoint_depth)) - if self._mask_params is not None: - target_assigners[SEGMENTATION_TASK] = ( - cn_assigner.CenterNetMaskTargetAssigner(stride, boxes_scale=1.05)) - if self._densepose_params is not None: - dp_stride = 1 if self._densepose_params.upsample_to_input_res else stride - target_assigners[DENSEPOSE_TASK] = ( - cn_assigner.CenterNetDensePoseTargetAssigner(dp_stride)) - if self._track_params is not None: - target_assigners[TRACK_TASK] = ( - cn_assigner.CenterNetTrackTargetAssigner( - stride, self._track_params.num_track_ids)) - if self._temporal_offset_params is not None: - target_assigners[TEMPORALOFFSET_TASK] = ( - cn_assigner.CenterNetTemporalOffsetTargetAssigner(stride)) - - return target_assigners - - def _compute_object_center_loss(self, input_height, input_width, - object_center_predictions, per_pixel_weights, - maximum_normalized_coordinate=1.1): - """Computes the object center loss. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - object_center_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, num_classes] representing the object center - feature maps. - per_pixel_weights: A float tensor of shape [batch_size, - out_height * out_width, 1] with 1s in locations where the spatial - coordinates fall within the height and width in true_image_shapes. - maximum_normalized_coordinate: Maximum coordinate value to be considered - as normalized, default to 1.1. This is used to check bounds during - converting normalized coordinates to absolute coordinates. - - Returns: - A float scalar tensor representing the object center loss per instance. - """ - gt_classes_list = self.groundtruth_lists(fields.BoxListFields.classes) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - - if self._center_params.use_labeled_classes: - gt_labeled_classes_list = self.groundtruth_lists( - fields.InputDataFields.groundtruth_labeled_classes) - batch_labeled_classes = tf.stack(gt_labeled_classes_list, axis=0) - batch_labeled_classes_shape = tf.shape(batch_labeled_classes) - batch_labeled_classes = tf.reshape( - batch_labeled_classes, - [batch_labeled_classes_shape[0], 1, batch_labeled_classes_shape[-1]]) - per_pixel_weights = per_pixel_weights * batch_labeled_classes - - # Convert the groundtruth to targets. - assigner = self._target_assigner_dict[OBJECT_CENTER] - if self._center_from_keypoints: - gt_keypoints_list = self.groundtruth_lists(fields.BoxListFields.keypoints) - heatmap_targets = assigner.assign_center_targets_from_keypoints( - height=input_height, - width=input_width, - gt_classes_list=gt_classes_list, - gt_keypoints_list=gt_keypoints_list, - gt_weights_list=gt_weights_list, - maximum_normalized_coordinate=maximum_normalized_coordinate) - else: - gt_boxes_list = self.groundtruth_lists(fields.BoxListFields.boxes) - heatmap_targets = assigner.assign_center_targets_from_boxes( - height=input_height, - width=input_width, - gt_boxes_list=gt_boxes_list, - gt_classes_list=gt_classes_list, - gt_weights_list=gt_weights_list, - maximum_normalized_coordinate=maximum_normalized_coordinate) - - flattened_heatmap_targets = _flatten_spatial_dimensions(heatmap_targets) - num_boxes = _to_float32(get_num_instances_from_weights(gt_weights_list)) - - loss = 0.0 - object_center_loss = self._center_params.classification_loss - # Loop through each feature output head. - for pred in object_center_predictions: - pred = _flatten_spatial_dimensions(pred) - loss += object_center_loss( - pred, flattened_heatmap_targets, weights=per_pixel_weights) - loss_per_instance = tf.reduce_sum(loss) / ( - float(len(object_center_predictions)) * num_boxes) - return loss_per_instance - - def _compute_object_detection_losses(self, input_height, input_width, - prediction_dict, per_pixel_weights, - maximum_normalized_coordinate=1.1): - """Computes the weighted object detection losses. - - This wrapper function calls the function which computes the losses for - object detection task and applies corresponding weights to the losses. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - prediction_dict: A dictionary holding predicted tensors output by - "predict" function. See "predict" function for more detailed - description. - per_pixel_weights: A float tensor of shape [batch_size, - out_height * out_width, 1] with 1s in locations where the spatial - coordinates fall within the height and width in true_image_shapes. - maximum_normalized_coordinate: Maximum coordinate value to be considered - as normalized, default to 1.1. This is used to check bounds during - converting normalized coordinates to absolute coordinates. - - Returns: - A dictionary of scalar float tensors representing the weighted losses for - object detection task: - BOX_SCALE: the weighted scale (height/width) loss. - BOX_OFFSET: the weighted object offset loss. - """ - od_scale_loss, od_offset_loss = self._compute_box_scale_and_offset_loss( - scale_predictions=prediction_dict[BOX_SCALE], - offset_predictions=prediction_dict[BOX_OFFSET], - input_height=input_height, - input_width=input_width, - maximum_normalized_coordinate=maximum_normalized_coordinate) - loss_dict = {} - loss_dict[BOX_SCALE] = ( - self._od_params.scale_loss_weight * od_scale_loss) - loss_dict[BOX_OFFSET] = ( - self._od_params.offset_loss_weight * od_offset_loss) - return loss_dict - - def _compute_box_scale_and_offset_loss(self, input_height, input_width, - scale_predictions, offset_predictions, - maximum_normalized_coordinate=1.1): - """Computes the scale loss of the object detection task. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - scale_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, 2] representing the prediction heads of the model - for object scale (i.e height and width). - offset_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, 2] representing the prediction heads of the model - for object offset. - maximum_normalized_coordinate: Maximum coordinate value to be considered - as normalized, default to 1.1. This is used to check bounds during - converting normalized coordinates to absolute coordinates. - - Returns: - A tuple of two losses: - scale_loss: A float scalar tensor representing the object height/width - loss normalized by total number of boxes. - offset_loss: A float scalar tensor representing the object offset loss - normalized by total number of boxes - """ - # TODO(vighneshb) Explore a size invariant version of scale loss. - gt_boxes_list = self.groundtruth_lists(fields.BoxListFields.boxes) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - num_boxes = _to_float32(get_num_instances_from_weights(gt_weights_list)) - num_predictions = float(len(scale_predictions)) - - assigner = self._target_assigner_dict[DETECTION_TASK] - (batch_indices, batch_height_width_targets, batch_offset_targets, - batch_weights) = assigner.assign_size_and_offset_targets( - height=input_height, - width=input_width, - gt_boxes_list=gt_boxes_list, - gt_weights_list=gt_weights_list, - maximum_normalized_coordinate=maximum_normalized_coordinate) - batch_weights = tf.expand_dims(batch_weights, -1) - - scale_loss = 0 - offset_loss = 0 - localization_loss_fn = self._od_params.localization_loss - for scale_pred, offset_pred in zip(scale_predictions, offset_predictions): - # Compute the scale loss. - scale_pred = cn_assigner.get_batch_predictions_from_indices( - scale_pred, batch_indices) - scale_loss += localization_loss_fn( - scale_pred, batch_height_width_targets, weights=batch_weights) - # Compute the offset loss. - offset_pred = cn_assigner.get_batch_predictions_from_indices( - offset_pred, batch_indices) - offset_loss += localization_loss_fn( - offset_pred, batch_offset_targets, weights=batch_weights) - scale_loss = tf.reduce_sum(scale_loss) / ( - num_predictions * num_boxes) - offset_loss = tf.reduce_sum(offset_loss) / ( - num_predictions * num_boxes) - return scale_loss, offset_loss - - def _compute_keypoint_estimation_losses(self, task_name, input_height, - input_width, prediction_dict, - per_pixel_weights): - """Computes the weighted keypoint losses.""" - kp_params = self._kp_params_dict[task_name] - heatmap_key = get_keypoint_name(task_name, KEYPOINT_HEATMAP) - offset_key = get_keypoint_name(task_name, KEYPOINT_OFFSET) - regression_key = get_keypoint_name(task_name, KEYPOINT_REGRESSION) - depth_key = get_keypoint_name(task_name, KEYPOINT_DEPTH) - heatmap_loss = self._compute_kp_heatmap_loss( - input_height=input_height, - input_width=input_width, - task_name=task_name, - heatmap_predictions=prediction_dict[heatmap_key], - classification_loss_fn=kp_params.classification_loss, - per_pixel_weights=per_pixel_weights) - offset_loss = self._compute_kp_offset_loss( - input_height=input_height, - input_width=input_width, - task_name=task_name, - offset_predictions=prediction_dict[offset_key], - localization_loss_fn=kp_params.localization_loss) - reg_loss = self._compute_kp_regression_loss( - input_height=input_height, - input_width=input_width, - task_name=task_name, - regression_predictions=prediction_dict[regression_key], - localization_loss_fn=kp_params.localization_loss) - - loss_dict = {} - loss_dict[heatmap_key] = ( - kp_params.keypoint_heatmap_loss_weight * heatmap_loss) - loss_dict[offset_key] = ( - kp_params.keypoint_offset_loss_weight * offset_loss) - loss_dict[regression_key] = ( - kp_params.keypoint_regression_loss_weight * reg_loss) - if kp_params.predict_depth: - depth_loss = self._compute_kp_depth_loss( - input_height=input_height, - input_width=input_width, - task_name=task_name, - depth_predictions=prediction_dict[depth_key], - localization_loss_fn=kp_params.localization_loss) - loss_dict[depth_key] = kp_params.keypoint_depth_loss_weight * depth_loss - return loss_dict - - def _compute_kp_heatmap_loss(self, input_height, input_width, task_name, - heatmap_predictions, classification_loss_fn, - per_pixel_weights): - """Computes the heatmap loss of the keypoint estimation task. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - task_name: A string representing the name of the keypoint task. - heatmap_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, num_keypoints] representing the prediction heads - of the model for keypoint heatmap. - classification_loss_fn: An object_detection.core.losses.Loss object to - compute the loss for the class predictions in CenterNet. - per_pixel_weights: A float tensor of shape [batch_size, - out_height * out_width, 1] with 1s in locations where the spatial - coordinates fall within the height and width in true_image_shapes. - - Returns: - loss: A float scalar tensor representing the object keypoint heatmap loss - normalized by number of instances. - """ - gt_keypoints_list = self.groundtruth_lists(fields.BoxListFields.keypoints) - gt_classes_list = self.groundtruth_lists(fields.BoxListFields.classes) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - gt_boxes_list = self.groundtruth_lists(fields.BoxListFields.boxes) - - assigner = self._target_assigner_dict[task_name] - (keypoint_heatmap, num_instances_per_kp_type, - valid_mask_batch) = assigner.assign_keypoint_heatmap_targets( - height=input_height, - width=input_width, - gt_keypoints_list=gt_keypoints_list, - gt_weights_list=gt_weights_list, - gt_classes_list=gt_classes_list, - gt_boxes_list=gt_boxes_list) - flattened_valid_mask = _flatten_spatial_dimensions(valid_mask_batch) - flattened_heapmap_targets = _flatten_spatial_dimensions(keypoint_heatmap) - # Sum over the number of instances per keypoint types to get the total - # number of keypoints. Note that this is used to normalized the loss and we - # keep the minimum value to be 1 to avoid generating weird loss value when - # no keypoint is in the image batch. - num_instances = tf.maximum( - tf.cast(tf.reduce_sum(num_instances_per_kp_type), dtype=tf.float32), - 1.0) - loss = 0.0 - # Loop through each feature output head. - for pred in heatmap_predictions: - pred = _flatten_spatial_dimensions(pred) - unweighted_loss = classification_loss_fn( - pred, - flattened_heapmap_targets, - weights=tf.ones_like(per_pixel_weights)) - # Apply the weights after the loss function to have full control over it. - loss += unweighted_loss * per_pixel_weights * flattened_valid_mask - loss = tf.reduce_sum(loss) / ( - float(len(heatmap_predictions)) * num_instances) - return loss - - def _compute_kp_offset_loss(self, input_height, input_width, task_name, - offset_predictions, localization_loss_fn): - """Computes the offset loss of the keypoint estimation task. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - task_name: A string representing the name of the keypoint task. - offset_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, 2] representing the prediction heads of the model - for keypoint offset. - localization_loss_fn: An object_detection.core.losses.Loss object to - compute the loss for the keypoint offset predictions in CenterNet. - - Returns: - loss: A float scalar tensor representing the keypoint offset loss - normalized by number of total keypoints. - """ - gt_keypoints_list = self.groundtruth_lists(fields.BoxListFields.keypoints) - gt_classes_list = self.groundtruth_lists(fields.BoxListFields.classes) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - - assigner = self._target_assigner_dict[task_name] - (batch_indices, batch_offsets, - batch_weights) = assigner.assign_keypoints_offset_targets( - height=input_height, - width=input_width, - gt_keypoints_list=gt_keypoints_list, - gt_weights_list=gt_weights_list, - gt_classes_list=gt_classes_list) - - # Keypoint offset loss. - loss = 0.0 - for prediction in offset_predictions: - batch_size, out_height, out_width, channels = _get_shape(prediction, 4) - if channels > 2: - prediction = tf.reshape( - prediction, shape=[batch_size, out_height, out_width, -1, 2]) - prediction = cn_assigner.get_batch_predictions_from_indices( - prediction, batch_indices) - # The dimensions passed are not as per the doc string but the loss - # still computes the correct value. - unweighted_loss = localization_loss_fn( - prediction, - batch_offsets, - weights=tf.expand_dims(tf.ones_like(batch_weights), -1)) - # Apply the weights after the loss function to have full control over it. - loss += batch_weights * tf.reduce_sum(unweighted_loss, axis=1) - - loss = tf.reduce_sum(loss) / ( - float(len(offset_predictions)) * - tf.maximum(tf.reduce_sum(batch_weights), 1.0)) - return loss - - def _compute_kp_regression_loss(self, input_height, input_width, task_name, - regression_predictions, localization_loss_fn): - """Computes the keypoint regression loss of the keypoint estimation task. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - task_name: A string representing the name of the keypoint task. - regression_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, 2 * num_keypoints] representing the prediction - heads of the model for keypoint regression offset. - localization_loss_fn: An object_detection.core.losses.Loss object to - compute the loss for the keypoint regression offset predictions in - CenterNet. - - Returns: - loss: A float scalar tensor representing the keypoint regression offset - loss normalized by number of total keypoints. - """ - gt_boxes_list = self.groundtruth_lists(fields.BoxListFields.boxes) - gt_keypoints_list = self.groundtruth_lists(fields.BoxListFields.keypoints) - gt_classes_list = self.groundtruth_lists(fields.BoxListFields.classes) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - # keypoint regression offset loss. - assigner = self._target_assigner_dict[task_name] - (batch_indices, batch_regression_offsets, - batch_weights) = assigner.assign_joint_regression_targets( - height=input_height, - width=input_width, - gt_keypoints_list=gt_keypoints_list, - gt_classes_list=gt_classes_list, - gt_weights_list=gt_weights_list, - gt_boxes_list=gt_boxes_list) - - loss = 0.0 - for prediction in regression_predictions: - batch_size, out_height, out_width, _ = _get_shape(prediction, 4) - reshaped_prediction = tf.reshape( - prediction, shape=[batch_size, out_height, out_width, -1, 2]) - reg_prediction = cn_assigner.get_batch_predictions_from_indices( - reshaped_prediction, batch_indices) - unweighted_loss = localization_loss_fn( - reg_prediction, - batch_regression_offsets, - weights=tf.expand_dims(tf.ones_like(batch_weights), -1)) - # Apply the weights after the loss function to have full control over it. - loss += batch_weights * tf.reduce_sum(unweighted_loss, axis=1) - - loss = tf.reduce_sum(loss) / ( - float(len(regression_predictions)) * - tf.maximum(tf.reduce_sum(batch_weights), 1.0)) - return loss - - def _compute_kp_depth_loss(self, input_height, input_width, task_name, - depth_predictions, localization_loss_fn): - """Computes the loss of the keypoint depth estimation. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - task_name: A string representing the name of the keypoint task. - depth_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, 1 (or num_keypoints)] representing the prediction - heads of the model for keypoint depth. - localization_loss_fn: An object_detection.core.losses.Loss object to - compute the loss for the keypoint offset predictions in CenterNet. - - Returns: - loss: A float scalar tensor representing the keypoint depth loss - normalized by number of total keypoints. - """ - kp_params = self._kp_params_dict[task_name] - gt_keypoints_list = self.groundtruth_lists(fields.BoxListFields.keypoints) - gt_classes_list = self.groundtruth_lists(fields.BoxListFields.classes) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - gt_keypoint_depths_list = self.groundtruth_lists( - fields.BoxListFields.keypoint_depths) - gt_keypoint_depth_weights_list = self.groundtruth_lists( - fields.BoxListFields.keypoint_depth_weights) - - assigner = self._target_assigner_dict[task_name] - (batch_indices, batch_depths, - batch_weights) = assigner.assign_keypoints_depth_targets( - height=input_height, - width=input_width, - gt_keypoints_list=gt_keypoints_list, - gt_weights_list=gt_weights_list, - gt_classes_list=gt_classes_list, - gt_keypoint_depths_list=gt_keypoint_depths_list, - gt_keypoint_depth_weights_list=gt_keypoint_depth_weights_list) - - # Keypoint offset loss. - loss = 0.0 - for prediction in depth_predictions: - if kp_params.per_keypoint_depth: - prediction = tf.expand_dims(prediction, axis=-1) - selected_depths = cn_assigner.get_batch_predictions_from_indices( - prediction, batch_indices) - # The dimensions passed are not as per the doc string but the loss - # still computes the correct value. - unweighted_loss = localization_loss_fn( - selected_depths, - batch_depths, - weights=tf.expand_dims(tf.ones_like(batch_weights), -1)) - # Apply the weights after the loss function to have full control over it. - loss += batch_weights * tf.squeeze(unweighted_loss, axis=1) - - loss = tf.reduce_sum(loss) / ( - float(len(depth_predictions)) * - tf.maximum(tf.reduce_sum(batch_weights), 1.0)) - return loss - - def _compute_segmentation_losses(self, prediction_dict, per_pixel_weights): - """Computes all the losses associated with segmentation. - - Args: - prediction_dict: The dictionary returned from the predict() method. - per_pixel_weights: A float tensor of shape [batch_size, - out_height * out_width, 1] with 1s in locations where the spatial - coordinates fall within the height and width in true_image_shapes. - - Returns: - A dictionary with segmentation losses. - """ - segmentation_heatmap = prediction_dict[SEGMENTATION_HEATMAP] - mask_loss = self._compute_mask_loss( - segmentation_heatmap, per_pixel_weights) - losses = { - SEGMENTATION_HEATMAP: mask_loss - } - return losses - - def _compute_mask_loss(self, segmentation_predictions, - per_pixel_weights): - """Computes the mask loss. - - Args: - segmentation_predictions: A list of float32 tensors of shape [batch_size, - out_height, out_width, num_classes]. - per_pixel_weights: A float tensor of shape [batch_size, - out_height * out_width, 1] with 1s in locations where the spatial - coordinates fall within the height and width in true_image_shapes. - - Returns: - A float scalar tensor representing the mask loss. - """ - gt_boxes_list = self.groundtruth_lists(fields.BoxListFields.boxes) - gt_masks_list = self.groundtruth_lists(fields.BoxListFields.masks) - gt_mask_weights_list = None - if self.groundtruth_has_field(fields.BoxListFields.mask_weights): - gt_mask_weights_list = self.groundtruth_lists( - fields.BoxListFields.mask_weights) - gt_classes_list = self.groundtruth_lists(fields.BoxListFields.classes) - - # Convert the groundtruth to targets. - assigner = self._target_assigner_dict[SEGMENTATION_TASK] - heatmap_targets, heatmap_weight = assigner.assign_segmentation_targets( - gt_masks_list=gt_masks_list, - gt_classes_list=gt_classes_list, - gt_boxes_list=gt_boxes_list, - gt_mask_weights_list=gt_mask_weights_list) - - flattened_heatmap_targets = _flatten_spatial_dimensions(heatmap_targets) - flattened_heatmap_mask = _flatten_spatial_dimensions( - heatmap_weight[:, :, :, tf.newaxis]) - per_pixel_weights *= flattened_heatmap_mask - - loss = 0.0 - mask_loss_fn = self._mask_params.classification_loss - - total_pixels_in_loss = tf.math.maximum( - tf.reduce_sum(per_pixel_weights), 1) - - # Loop through each feature output head. - for pred in segmentation_predictions: - pred = _flatten_spatial_dimensions(pred) - loss += mask_loss_fn( - pred, flattened_heatmap_targets, weights=per_pixel_weights) - # TODO(ronnyvotel): Consider other ways to normalize loss. - total_loss = tf.reduce_sum(loss) / ( - float(len(segmentation_predictions)) * total_pixels_in_loss) - return total_loss - - def _compute_densepose_losses(self, input_height, input_width, - prediction_dict): - """Computes the weighted DensePose losses. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - prediction_dict: A dictionary holding predicted tensors output by the - "predict" function. See the "predict" function for more detailed - description. - - Returns: - A dictionary of scalar float tensors representing the weighted losses for - the DensePose task: - DENSEPOSE_HEATMAP: the weighted part segmentation loss. - DENSEPOSE_REGRESSION: the weighted part surface coordinate loss. - """ - dp_heatmap_loss, dp_regression_loss = ( - self._compute_densepose_part_and_coordinate_losses( - input_height=input_height, - input_width=input_width, - part_predictions=prediction_dict[DENSEPOSE_HEATMAP], - surface_coord_predictions=prediction_dict[DENSEPOSE_REGRESSION])) - loss_dict = {} - loss_dict[DENSEPOSE_HEATMAP] = ( - self._densepose_params.part_loss_weight * dp_heatmap_loss) - loss_dict[DENSEPOSE_REGRESSION] = ( - self._densepose_params.coordinate_loss_weight * dp_regression_loss) - return loss_dict - - def _compute_densepose_part_and_coordinate_losses( - self, input_height, input_width, part_predictions, - surface_coord_predictions): - """Computes the individual losses for the DensePose task. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - part_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, num_parts]. - surface_coord_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, 2 * num_parts]. - - Returns: - A tuple with two scalar loss tensors: part_prediction_loss and - surface_coord_loss. - """ - gt_dp_num_points_list = self.groundtruth_lists( - fields.BoxListFields.densepose_num_points) - gt_dp_part_ids_list = self.groundtruth_lists( - fields.BoxListFields.densepose_part_ids) - gt_dp_surface_coords_list = self.groundtruth_lists( - fields.BoxListFields.densepose_surface_coords) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - - assigner = self._target_assigner_dict[DENSEPOSE_TASK] - batch_indices, batch_part_ids, batch_surface_coords, batch_weights = ( - assigner.assign_part_and_coordinate_targets( - height=input_height, - width=input_width, - gt_dp_num_points_list=gt_dp_num_points_list, - gt_dp_part_ids_list=gt_dp_part_ids_list, - gt_dp_surface_coords_list=gt_dp_surface_coords_list, - gt_weights_list=gt_weights_list)) - - part_prediction_loss = 0 - surface_coord_loss = 0 - classification_loss_fn = self._densepose_params.classification_loss - localization_loss_fn = self._densepose_params.localization_loss - num_predictions = float(len(part_predictions)) - num_valid_points = tf.math.count_nonzero(batch_weights) - num_valid_points = tf.cast(tf.math.maximum(num_valid_points, 1), tf.float32) - for part_pred, surface_coord_pred in zip(part_predictions, - surface_coord_predictions): - # Potentially upsample the feature maps, so that better quality (i.e. - # higher res) groundtruth can be applied. - if self._densepose_params.upsample_to_input_res: - part_pred = tf.keras.layers.UpSampling2D( - self._stride, interpolation=self._densepose_params.upsample_method)( - part_pred) - surface_coord_pred = tf.keras.layers.UpSampling2D( - self._stride, interpolation=self._densepose_params.upsample_method)( - surface_coord_pred) - # Compute the part prediction loss. - part_pred = cn_assigner.get_batch_predictions_from_indices( - part_pred, batch_indices[:, 0:3]) - part_prediction_loss += classification_loss_fn( - part_pred[:, tf.newaxis, :], - batch_part_ids[:, tf.newaxis, :], - weights=batch_weights[:, tf.newaxis, tf.newaxis]) - # Compute the surface coordinate loss. - batch_size, out_height, out_width, _ = _get_shape( - surface_coord_pred, 4) - surface_coord_pred = tf.reshape( - surface_coord_pred, [batch_size, out_height, out_width, -1, 2]) - surface_coord_pred = cn_assigner.get_batch_predictions_from_indices( - surface_coord_pred, batch_indices) - surface_coord_loss += localization_loss_fn( - surface_coord_pred, - batch_surface_coords, - weights=batch_weights[:, tf.newaxis]) - part_prediction_loss = tf.reduce_sum(part_prediction_loss) / ( - num_predictions * num_valid_points) - surface_coord_loss = tf.reduce_sum(surface_coord_loss) / ( - num_predictions * num_valid_points) - return part_prediction_loss, surface_coord_loss - - def _compute_track_losses(self, input_height, input_width, prediction_dict): - """Computes all the losses associated with tracking. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - prediction_dict: The dictionary returned from the predict() method. - - Returns: - A dictionary with tracking losses. - """ - object_reid_predictions = prediction_dict[TRACK_REID] - embedding_loss = self._compute_track_embedding_loss( - input_height=input_height, - input_width=input_width, - object_reid_predictions=object_reid_predictions) - losses = { - TRACK_REID: embedding_loss - } - return losses - - def _compute_track_embedding_loss(self, input_height, input_width, - object_reid_predictions): - """Computes the object ReID loss. - - The embedding is trained as a classification task where the target is the - ID of each track among all tracks in the whole dataset. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - object_reid_predictions: A list of float tensors of shape [batch_size, - out_height, out_width, reid_embed_size] representing the object - embedding feature maps. - - Returns: - A float scalar tensor representing the object ReID loss per instance. - """ - gt_track_ids_list = self.groundtruth_lists(fields.BoxListFields.track_ids) - gt_boxes_list = self.groundtruth_lists(fields.BoxListFields.boxes) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - num_boxes = _to_float32(get_num_instances_from_weights(gt_weights_list)) - - # Convert the groundtruth to targets. - assigner = self._target_assigner_dict[TRACK_TASK] - batch_indices, batch_weights, track_targets = assigner.assign_track_targets( - height=input_height, - width=input_width, - gt_track_ids_list=gt_track_ids_list, - gt_boxes_list=gt_boxes_list, - gt_weights_list=gt_weights_list) - batch_weights = tf.expand_dims(batch_weights, -1) - - loss = 0.0 - object_reid_loss = self._track_params.classification_loss - # Loop through each feature output head. - for pred in object_reid_predictions: - embedding_pred = cn_assigner.get_batch_predictions_from_indices( - pred, batch_indices) - - reid_classification = self.track_reid_classification_net(embedding_pred) - - loss += object_reid_loss( - reid_classification, track_targets, weights=batch_weights) - - loss_per_instance = tf.reduce_sum(loss) / ( - float(len(object_reid_predictions)) * num_boxes) - - return loss_per_instance - - def _compute_temporal_offset_loss(self, input_height, - input_width, prediction_dict): - """Computes the temporal offset loss for tracking. - - Args: - input_height: An integer scalar tensor representing input image height. - input_width: An integer scalar tensor representing input image width. - prediction_dict: The dictionary returned from the predict() method. - - Returns: - A dictionary with track/temporal_offset losses. - """ - gt_boxes_list = self.groundtruth_lists(fields.BoxListFields.boxes) - gt_offsets_list = self.groundtruth_lists( - fields.BoxListFields.temporal_offsets) - gt_match_list = self.groundtruth_lists( - fields.BoxListFields.track_match_flags) - gt_weights_list = self.groundtruth_lists(fields.BoxListFields.weights) - num_boxes = tf.cast( - get_num_instances_from_weights(gt_weights_list), tf.float32) - - offset_predictions = prediction_dict[TEMPORAL_OFFSET] - num_predictions = float(len(offset_predictions)) - - assigner = self._target_assigner_dict[TEMPORALOFFSET_TASK] - (batch_indices, batch_offset_targets, - batch_weights) = assigner.assign_temporal_offset_targets( - height=input_height, - width=input_width, - gt_boxes_list=gt_boxes_list, - gt_offsets_list=gt_offsets_list, - gt_match_list=gt_match_list, - gt_weights_list=gt_weights_list) - batch_weights = tf.expand_dims(batch_weights, -1) - - offset_loss_fn = self._temporal_offset_params.localization_loss - loss_dict = {} - offset_loss = 0 - for offset_pred in offset_predictions: - offset_pred = cn_assigner.get_batch_predictions_from_indices( - offset_pred, batch_indices) - offset_loss += offset_loss_fn(offset_pred[:, None], - batch_offset_targets[:, None], - weights=batch_weights) - offset_loss = tf.reduce_sum(offset_loss) / (num_predictions * num_boxes) - loss_dict[TEMPORAL_OFFSET] = offset_loss - return loss_dict - - def _should_clip_keypoints(self): - """Returns a boolean indicating whether keypoint clipping should occur. - - If there is only one keypoint task, clipping is controlled by the field - `clip_out_of_frame_keypoints`. If there are multiple keypoint tasks, - clipping logic is defined based on unanimous agreement of keypoint - parameters. If there is any ambiguity, clip_out_of_frame_keypoints is set - to False (default). - """ - kp_params_iterator = iter(self._kp_params_dict.values()) - if len(self._kp_params_dict) == 1: - kp_params = next(kp_params_iterator) - return kp_params.clip_out_of_frame_keypoints - - # Multi-task setting. - kp_params = next(kp_params_iterator) - should_clip = kp_params.clip_out_of_frame_keypoints - for kp_params in kp_params_iterator: - if kp_params.clip_out_of_frame_keypoints != should_clip: - return False - return should_clip - - def _rescore_instances(self, classes, scores, keypoint_scores): - """Rescores instances based on detection and keypoint scores. - - Args: - classes: A [batch, max_detections] int32 tensor with detection classes. - scores: A [batch, max_detections] float32 tensor with detection scores. - keypoint_scores: A [batch, max_detections, total_num_keypoints] float32 - tensor with keypoint scores. - - Returns: - A [batch, max_detections] float32 tensor with possibly altered detection - scores. - """ - batch, max_detections, total_num_keypoints = ( - shape_utils.combined_static_and_dynamic_shape(keypoint_scores)) - classes_tiled = tf.tile(classes[:, :, tf.newaxis], - multiples=[1, 1, total_num_keypoints]) - # TODO(yuhuic): Investigate whether this function will create subgraphs in - # tflite that will cause the model to run slower at inference. - for kp_params in self._kp_params_dict.values(): - if not kp_params.rescore_instances: - continue - class_id = kp_params.class_id - keypoint_indices = kp_params.keypoint_indices - kpt_mask = tf.reduce_sum( - tf.one_hot(keypoint_indices, depth=total_num_keypoints), axis=0) - kpt_mask_tiled = tf.tile(kpt_mask[tf.newaxis, tf.newaxis, :], - multiples=[batch, max_detections, 1]) - class_and_keypoint_mask = tf.math.logical_and( - classes_tiled == class_id, - kpt_mask_tiled == 1.0) - class_and_keypoint_mask_float = tf.cast(class_and_keypoint_mask, - dtype=tf.float32) - visible_keypoints = tf.math.greater( - keypoint_scores, kp_params.rescoring_threshold) - keypoint_scores = tf.where( - visible_keypoints, keypoint_scores, tf.zeros_like(keypoint_scores)) - num_visible_keypoints = tf.reduce_sum( - class_and_keypoint_mask_float * - tf.cast(visible_keypoints, tf.float32), axis=-1) - num_visible_keypoints = tf.math.maximum(num_visible_keypoints, 1.0) - scores_for_class = (1./num_visible_keypoints) * ( - tf.reduce_sum(class_and_keypoint_mask_float * - scores[:, :, tf.newaxis] * - keypoint_scores, axis=-1)) - scores = tf.where(classes == class_id, - scores_for_class, - scores) - return scores - - def preprocess(self, inputs): - outputs = shape_utils.resize_images_and_return_shapes( - inputs, self._image_resizer_fn) - resized_inputs, true_image_shapes = outputs - - return (self._feature_extractor.preprocess(resized_inputs), - true_image_shapes) - - def predict(self, preprocessed_inputs, _): - """Predicts CenterNet prediction tensors given an input batch. - - Feature extractors are free to produce predictions from multiple feature - maps and therefore we return a dictionary mapping strings to lists. - E.g. the hourglass backbone produces two feature maps. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float32 tensor - representing a batch of images. - - Returns: - prediction_dict: a dictionary holding predicted tensors with - 'preprocessed_inputs' - The input image after being resized and - preprocessed by the feature extractor. - 'extracted_features' - The output of the feature extractor. - 'object_center' - A list of size num_feature_outputs containing - float tensors of size [batch_size, output_height, output_width, - num_classes] representing the predicted object center heatmap logits. - 'box/scale' - [optional] A list of size num_feature_outputs holding - float tensors of size [batch_size, output_height, output_width, 2] - representing the predicted box height and width at each output - location. This field exists only when object detection task is - specified. - 'box/offset' - [optional] A list of size num_feature_outputs holding - float tensors of size [batch_size, output_height, output_width, 2] - representing the predicted y and x offsets at each output location. - '$TASK_NAME/keypoint_heatmap' - [optional] A list of size - num_feature_outputs holding float tensors of size [batch_size, - output_height, output_width, num_keypoints] representing the predicted - keypoint heatmap logits. - '$TASK_NAME/keypoint_offset' - [optional] A list of size - num_feature_outputs holding float tensors of size [batch_size, - output_height, output_width, 2] representing the predicted keypoint - offsets at each output location. - '$TASK_NAME/keypoint_regression' - [optional] A list of size - num_feature_outputs holding float tensors of size [batch_size, - output_height, output_width, 2 * num_keypoints] representing the - predicted keypoint regression at each output location. - 'segmentation/heatmap' - [optional] A list of size num_feature_outputs - holding float tensors of size [batch_size, output_height, - output_width, num_classes] representing the mask logits. - 'densepose/heatmap' - [optional] A list of size num_feature_outputs - holding float tensors of size [batch_size, output_height, - output_width, num_parts] representing the mask logits for each part. - 'densepose/regression' - [optional] A list of size num_feature_outputs - holding float tensors of size [batch_size, output_height, - output_width, 2 * num_parts] representing the DensePose surface - coordinate predictions. - Note the $TASK_NAME is provided by the KeypointEstimation namedtuple - used to differentiate between different keypoint tasks. - """ - features_list = self._feature_extractor(preprocessed_inputs) - - predictions = {} - for head_name, heads in self._prediction_head_dict.items(): - predictions[head_name] = [ - head(feature) for (feature, head) in zip(features_list, heads) - ] - predictions['extracted_features'] = features_list - predictions['preprocessed_inputs'] = preprocessed_inputs - - self._batched_prediction_tensor_names = predictions.keys() - return predictions - - def loss( - self, prediction_dict, true_image_shapes, scope=None, - maximum_normalized_coordinate=1.1): - """Computes scalar loss tensors with respect to provided groundtruth. - - This function implements the various CenterNet losses. - - Args: - prediction_dict: a dictionary holding predicted tensors returned by - "predict" function. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is of - the form [height, width, channels] indicating the shapes of true images - in the resized images, as resized images can be padded with zeros. - scope: Optional scope name. - maximum_normalized_coordinate: Maximum coordinate value to be considered - as normalized, default to 1.1. This is used to check bounds during - converting normalized coordinates to absolute coordinates. - - Returns: - A dictionary mapping the keys [ - 'Loss/object_center', - 'Loss/box/scale', (optional) - 'Loss/box/offset', (optional) - 'Loss/$TASK_NAME/keypoint/heatmap', (optional) - 'Loss/$TASK_NAME/keypoint/offset', (optional) - 'Loss/$TASK_NAME/keypoint/regression', (optional) - 'Loss/segmentation/heatmap', (optional) - 'Loss/densepose/heatmap', (optional) - 'Loss/densepose/regression', (optional) - 'Loss/track/reid'] (optional) - 'Loss/track/offset'] (optional) - scalar tensors corresponding to the losses for different tasks. Note the - $TASK_NAME is provided by the KeypointEstimation namedtuple used to - differentiate between different keypoint tasks. - """ - - _, input_height, input_width, _ = _get_shape( - prediction_dict['preprocessed_inputs'], 4) - - output_height, output_width = (tf.maximum(input_height // self._stride, 1), - tf.maximum(input_width // self._stride, 1)) - - # TODO(vighneshb) Explore whether using floor here is safe. - output_true_image_shapes = tf.ceil( - tf.cast(true_image_shapes, tf.float32) / self._stride) - valid_anchor_weights = get_valid_anchor_weights_in_flattened_image( - output_true_image_shapes, output_height, output_width) - valid_anchor_weights = tf.expand_dims(valid_anchor_weights, 2) - - object_center_loss = self._compute_object_center_loss( - object_center_predictions=prediction_dict[OBJECT_CENTER], - input_height=input_height, - input_width=input_width, - per_pixel_weights=valid_anchor_weights, - maximum_normalized_coordinate=maximum_normalized_coordinate) - losses = { - OBJECT_CENTER: - self._center_params.object_center_loss_weight * object_center_loss - } - if self._od_params is not None: - od_losses = self._compute_object_detection_losses( - input_height=input_height, - input_width=input_width, - prediction_dict=prediction_dict, - per_pixel_weights=valid_anchor_weights, - maximum_normalized_coordinate=maximum_normalized_coordinate) - for key in od_losses: - od_losses[key] = od_losses[key] * self._od_params.task_loss_weight - losses.update(od_losses) - - if self._kp_params_dict is not None: - for task_name, params in self._kp_params_dict.items(): - kp_losses = self._compute_keypoint_estimation_losses( - task_name=task_name, - input_height=input_height, - input_width=input_width, - prediction_dict=prediction_dict, - per_pixel_weights=valid_anchor_weights) - for key in kp_losses: - kp_losses[key] = kp_losses[key] * params.task_loss_weight - losses.update(kp_losses) - - if self._mask_params is not None: - seg_losses = self._compute_segmentation_losses( - prediction_dict=prediction_dict, - per_pixel_weights=valid_anchor_weights) - for key in seg_losses: - seg_losses[key] = seg_losses[key] * self._mask_params.task_loss_weight - losses.update(seg_losses) - - if self._densepose_params is not None: - densepose_losses = self._compute_densepose_losses( - input_height=input_height, - input_width=input_width, - prediction_dict=prediction_dict) - for key in densepose_losses: - densepose_losses[key] = ( - densepose_losses[key] * self._densepose_params.task_loss_weight) - losses.update(densepose_losses) - - if self._track_params is not None: - track_losses = self._compute_track_losses( - input_height=input_height, - input_width=input_width, - prediction_dict=prediction_dict) - for key in track_losses: - track_losses[key] = ( - track_losses[key] * self._track_params.task_loss_weight) - losses.update(track_losses) - - if self._temporal_offset_params is not None: - offset_losses = self._compute_temporal_offset_loss( - input_height=input_height, - input_width=input_width, - prediction_dict=prediction_dict) - for key in offset_losses: - offset_losses[key] = ( - offset_losses[key] * self._temporal_offset_params.task_loss_weight) - losses.update(offset_losses) - - # Prepend the LOSS_KEY_PREFIX to the keys in the dictionary such that the - # losses will be grouped together in Tensorboard. - return dict([('%s/%s' % (LOSS_KEY_PREFIX, key), val) - for key, val in losses.items()]) - - def postprocess(self, prediction_dict, true_image_shapes, **params): - """Produces boxes given a prediction dict returned by predict(). - - Although predict returns a list of tensors, only the last tensor in - each list is used for making box predictions. - - Args: - prediction_dict: a dictionary holding predicted tensors from "predict" - function. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is of - the form [height, width, channels] indicating the shapes of true images - in the resized images, as resized images can be padded with zeros. - **params: Currently ignored. - - Returns: - detections: a dictionary containing the following fields - detection_boxes - A tensor of shape [batch, max_detections, 4] - holding the predicted boxes. - detection_boxes_strided: A tensor of shape [batch_size, num_detections, - 4] holding the predicted boxes in absolute coordinates of the - feature extractor's final layer output. - detection_scores: A tensor of shape [batch, max_detections] holding - the predicted score for each box. - detection_multiclass_scores: A tensor of shape [batch, max_detection, - num_classes] holding multiclass score for each box. - detection_classes: An integer tensor of shape [batch, max_detections] - containing the detected class for each box. - num_detections: An integer tensor of shape [batch] containing the - number of detected boxes for each sample in the batch. - detection_keypoints: (Optional) A float tensor of shape [batch, - max_detections, num_keypoints, 2] with normalized keypoints. Any - invalid keypoints have their coordinates and scores set to 0.0. - detection_keypoint_scores: (Optional) A float tensor of shape [batch, - max_detection, num_keypoints] with scores for each keypoint. - detection_masks: (Optional) A uint8 tensor of shape [batch, - max_detections, mask_height, mask_width] with masks for each - detection. Background is specified with 0, and foreground is specified - with positive integers (1 for standard instance segmentation mask, and - 1-indexed parts for DensePose task). - detection_surface_coords: (Optional) A float32 tensor of shape [batch, - max_detection, mask_height, mask_width, 2] with DensePose surface - coordinates, in (v, u) format. - detection_embeddings: (Optional) A float tensor of shape [batch, - max_detections, reid_embed_size] containing object embeddings. - """ - object_center_prob = tf.nn.sigmoid(prediction_dict[OBJECT_CENTER][-1]) - - if true_image_shapes is None: - # If true_image_shapes is not provided, we assume the whole image is valid - # and infer the true_image_shapes from the object_center_prob shape. - batch_size, strided_height, strided_width, _ = _get_shape( - object_center_prob, 4) - true_image_shapes = tf.stack( - [strided_height * self._stride, strided_width * self._stride, - tf.constant(len(self._feature_extractor._channel_means))]) # pylint: disable=protected-access - true_image_shapes = tf.stack([true_image_shapes] * batch_size, axis=0) - else: - # Mask object centers by true_image_shape. [batch, h, w, 1] - object_center_mask = mask_from_true_image_shape( - _get_shape(object_center_prob, 4), true_image_shapes) - object_center_prob *= object_center_mask - - # Get x, y and channel indices corresponding to the top indices in the class - # center predictions. - detection_scores, y_indices, x_indices, channel_indices = ( - top_k_feature_map_locations( - object_center_prob, - max_pool_kernel_size=self._center_params.peak_max_pool_kernel_size, - k=self._center_params.max_box_predictions)) - multiclass_scores = tf.gather_nd( - object_center_prob, tf.stack([y_indices, x_indices], -1), batch_dims=1) - num_detections = tf.reduce_sum( - tf.cast(detection_scores > 0, tf.int32), axis=1) - postprocess_dict = { - fields.DetectionResultFields.detection_scores: detection_scores, - fields.DetectionResultFields.detection_multiclass_scores: - multiclass_scores, - fields.DetectionResultFields.detection_classes: channel_indices, - fields.DetectionResultFields.num_detections: num_detections, - } - - if self._output_prediction_dict: - postprocess_dict.update(prediction_dict) - postprocess_dict['true_image_shapes'] = true_image_shapes - - boxes_strided = None - if self._od_params: - boxes_strided = ( - prediction_tensors_to_boxes(y_indices, x_indices, - prediction_dict[BOX_SCALE][-1], - prediction_dict[BOX_OFFSET][-1])) - - boxes = convert_strided_predictions_to_normalized_boxes( - boxes_strided, self._stride, true_image_shapes) - - postprocess_dict.update({ - fields.DetectionResultFields.detection_boxes: boxes, - 'detection_boxes_strided': boxes_strided, - }) - - if self._kp_params_dict: - # If the model is trained to predict only one class of object and its - # keypoint, we fall back to a simpler postprocessing function which uses - # the ops that are supported by tf.lite on GPU. - clip_keypoints = self._should_clip_keypoints() - if len(self._kp_params_dict) == 1 and self._num_classes == 1: - task_name, kp_params = next(iter(self._kp_params_dict.items())) - keypoint_depths = None - if kp_params.argmax_postprocessing: - keypoints, keypoint_scores = ( - prediction_to_keypoints_argmax( - prediction_dict, - object_y_indices=y_indices, - object_x_indices=x_indices, - boxes=boxes_strided, - task_name=task_name, - kp_params=kp_params)) - else: - (keypoints, keypoint_scores, - keypoint_depths) = self._postprocess_keypoints_single_class( - prediction_dict, channel_indices, y_indices, x_indices, - boxes_strided, num_detections) - keypoints, keypoint_scores = ( - convert_strided_predictions_to_normalized_keypoints( - keypoints, keypoint_scores, self._stride, true_image_shapes, - clip_out_of_frame_keypoints=clip_keypoints)) - if keypoint_depths is not None: - postprocess_dict.update({ - fields.DetectionResultFields.detection_keypoint_depths: - keypoint_depths - }) - else: - # Multi-class keypoint estimation task does not support depth - # estimation. - assert all([ - not kp_dict.predict_depth - for kp_dict in self._kp_params_dict.values() - ]) - keypoints, keypoint_scores = self._postprocess_keypoints_multi_class( - prediction_dict, channel_indices, y_indices, x_indices, - boxes_strided, num_detections) - keypoints, keypoint_scores = ( - convert_strided_predictions_to_normalized_keypoints( - keypoints, keypoint_scores, self._stride, true_image_shapes, - clip_out_of_frame_keypoints=clip_keypoints)) - - postprocess_dict.update({ - fields.DetectionResultFields.detection_keypoints: keypoints, - fields.DetectionResultFields.detection_keypoint_scores: - keypoint_scores - }) - if self._od_params is None: - # Still output the box prediction by enclosing the keypoints for - # evaluation purpose. - boxes = keypoint_ops.keypoints_to_enclosing_bounding_boxes( - keypoints, keypoints_axis=2) - postprocess_dict.update({ - fields.DetectionResultFields.detection_boxes: boxes, - }) - - if self._mask_params: - masks = tf.nn.sigmoid(prediction_dict[SEGMENTATION_HEATMAP][-1]) - densepose_part_heatmap, densepose_surface_coords = None, None - densepose_class_index = 0 - if self._densepose_params: - densepose_part_heatmap = prediction_dict[DENSEPOSE_HEATMAP][-1] - densepose_surface_coords = prediction_dict[DENSEPOSE_REGRESSION][-1] - densepose_class_index = self._densepose_params.class_id - instance_masks, surface_coords = ( - convert_strided_predictions_to_instance_masks( - boxes, channel_indices, masks, true_image_shapes, - densepose_part_heatmap, densepose_surface_coords, - stride=self._stride, mask_height=self._mask_params.mask_height, - mask_width=self._mask_params.mask_width, - score_threshold=self._mask_params.score_threshold, - densepose_class_index=densepose_class_index)) - postprocess_dict[ - fields.DetectionResultFields.detection_masks] = instance_masks - if self._densepose_params: - postprocess_dict[ - fields.DetectionResultFields.detection_surface_coords] = ( - surface_coords) - - if self._track_params: - embeddings = self._postprocess_embeddings(prediction_dict, - y_indices, x_indices) - postprocess_dict.update({ - fields.DetectionResultFields.detection_embeddings: embeddings - }) - - if self._temporal_offset_params: - offsets = prediction_tensors_to_temporal_offsets( - y_indices, x_indices, - prediction_dict[TEMPORAL_OFFSET][-1]) - postprocess_dict[fields.DetectionResultFields.detection_offsets] = offsets - - if self._non_max_suppression_fn: - boxes = tf.expand_dims( - postprocess_dict.pop(fields.DetectionResultFields.detection_boxes), - axis=-2) - multiclass_scores = postprocess_dict[ - fields.DetectionResultFields.detection_multiclass_scores] - num_classes = tf.shape(multiclass_scores)[2] - class_mask = tf.cast( - tf.one_hot( - postprocess_dict[fields.DetectionResultFields.detection_classes], - depth=num_classes), tf.bool) - # Surpress the scores of those unselected classes to be zeros. Otherwise, - # the downstream NMS ops might be confused and introduce issues. - multiclass_scores = tf.where( - class_mask, multiclass_scores, tf.zeros_like(multiclass_scores)) - num_valid_boxes = postprocess_dict.pop( - fields.DetectionResultFields.num_detections) - # Remove scores and classes as NMS will compute these form multiclass - # scores. - postprocess_dict.pop(fields.DetectionResultFields.detection_scores) - postprocess_dict.pop(fields.DetectionResultFields.detection_classes) - (nmsed_boxes, nmsed_scores, nmsed_classes, _, nmsed_additional_fields, - num_detections) = self._non_max_suppression_fn( - boxes, - multiclass_scores, - additional_fields=postprocess_dict, - num_valid_boxes=num_valid_boxes) - postprocess_dict = nmsed_additional_fields - postprocess_dict[ - fields.DetectionResultFields.detection_boxes] = nmsed_boxes - postprocess_dict[ - fields.DetectionResultFields.detection_scores] = nmsed_scores - postprocess_dict[ - fields.DetectionResultFields.detection_classes] = nmsed_classes - postprocess_dict[ - fields.DetectionResultFields.num_detections] = num_detections - postprocess_dict.update(nmsed_additional_fields) - - # Perform the rescoring once the NMS is applied to make sure the rescored - # scores won't be washed out by the NMS function. - if self._kp_params_dict: - channel_indices = postprocess_dict[ - fields.DetectionResultFields.detection_classes] - detection_scores = postprocess_dict[ - fields.DetectionResultFields.detection_scores] - keypoint_scores = postprocess_dict[ - fields.DetectionResultFields.detection_keypoint_scores] - # Update instance scores based on keypoints. - scores = self._rescore_instances( - channel_indices, detection_scores, keypoint_scores) - postprocess_dict.update({ - fields.DetectionResultFields.detection_scores: scores, - }) - return postprocess_dict - - def postprocess_single_instance_keypoints( - self, - prediction_dict, - true_image_shapes): - """Postprocess for predicting single instance keypoints. - - This postprocess function is a special case of predicting the keypoint of - a single instance in the image (original CenterNet postprocess supports - multi-instance prediction). Due to the simplification assumption, this - postprocessing function achieves much faster inference time. - Here is a short list of the modifications made in this function: - - 1) Assume the model predicts only single class keypoint. - 2) Assume there is only one instance in the image. If multiple instances - appear in the image, the model tends to predict the one that is closer - to the image center (the other ones are considered as background and - are rejected by the model). - 3) Avoid using top_k ops in the postprocessing logics since it is slower - than using argmax. - 4) The predictions other than the keypoints are ignored, e.g. boxes. - 5) The input batch size is assumed to be 1. - - Args: - prediction_dict: a dictionary holding predicted tensors from "predict" - function. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is of - the form [height, width, channels] indicating the shapes of true images - in the resized images, as resized images can be padded with zeros. - - Returns: - detections: a dictionary containing the following fields - detection_keypoints: A float tensor of shape - [1, 1, num_keypoints, 2] with normalized keypoints. Any invalid - keypoints have their coordinates and scores set to 0.0. - detection_keypoint_scores: A float tensor of shape - [1, 1, num_keypoints] with scores for each keypoint. - """ - # The number of keypoint task is expected to be 1. - assert len(self._kp_params_dict) == 1 - task_name, kp_params = next(iter(self._kp_params_dict.items())) - keypoint_heatmap = tf.nn.sigmoid(prediction_dict[get_keypoint_name( - task_name, KEYPOINT_HEATMAP)][-1]) - keypoint_offset = prediction_dict[get_keypoint_name(task_name, - KEYPOINT_OFFSET)][-1] - keypoint_regression = prediction_dict[get_keypoint_name( - task_name, KEYPOINT_REGRESSION)][-1] - object_heatmap = tf.nn.sigmoid(prediction_dict[OBJECT_CENTER][-1]) - - keypoint_depths = None - if kp_params.predict_depth: - keypoint_depths = prediction_dict[get_keypoint_name( - task_name, KEYPOINT_DEPTH)][-1] - keypoints, keypoint_scores, keypoint_depths = ( - prediction_to_single_instance_keypoints( - object_heatmap=object_heatmap, - keypoint_heatmap=keypoint_heatmap, - keypoint_offset=keypoint_offset, - keypoint_regression=keypoint_regression, - kp_params=kp_params, - keypoint_depths=keypoint_depths)) - - keypoints, keypoint_scores = ( - convert_strided_predictions_to_normalized_keypoints( - keypoints, - keypoint_scores, - self._stride, - true_image_shapes, - clip_out_of_frame_keypoints=False)) - postprocess_dict = { - fields.DetectionResultFields.detection_keypoints: keypoints, - fields.DetectionResultFields.detection_keypoint_scores: keypoint_scores - } - - if kp_params.predict_depth: - postprocess_dict.update({ - fields.DetectionResultFields.detection_keypoint_depths: - keypoint_depths - }) - return postprocess_dict - - def _postprocess_embeddings(self, prediction_dict, y_indices, x_indices): - """Performs postprocessing on embedding predictions. - - Args: - prediction_dict: a dictionary holding predicted tensors, returned from the - predict() method. This dictionary should contain embedding prediction - feature maps for tracking task. - y_indices: A [batch_size, max_detections] int tensor with y indices for - all object centers. - x_indices: A [batch_size, max_detections] int tensor with x indices for - all object centers. - - Returns: - embeddings: A [batch_size, max_detection, reid_embed_size] float32 - tensor with L2 normalized embeddings extracted from detection box - centers. - """ - embedding_predictions = prediction_dict[TRACK_REID][-1] - embeddings = predicted_embeddings_at_object_centers( - embedding_predictions, y_indices, x_indices) - embeddings, _ = tf.linalg.normalize(embeddings, axis=-1) - - return embeddings - - def _scatter_keypoints_to_batch(self, num_ind, kpt_coords_for_example, - kpt_scores_for_example, - instance_inds_for_example, max_detections, - total_num_keypoints): - """Helper function to convert scattered keypoints into batch.""" - def left_fn(kpt_coords_for_example, kpt_scores_for_example, - instance_inds_for_example): - # Scatter into tensor where instances align with original detection - # instances. New shape of keypoint coordinates and scores are - # [1, max_detections, num_total_keypoints, 2] and - # [1, max_detections, num_total_keypoints], respectively. - return _pad_to_full_instance_dim( - kpt_coords_for_example, kpt_scores_for_example, - instance_inds_for_example, - self._center_params.max_box_predictions) - - def right_fn(): - kpt_coords_for_example_all_det = tf.zeros( - [1, max_detections, total_num_keypoints, 2], dtype=tf.float32) - kpt_scores_for_example_all_det = tf.zeros( - [1, max_detections, total_num_keypoints], dtype=tf.float32) - return (kpt_coords_for_example_all_det, - kpt_scores_for_example_all_det) - - left_fn = functools.partial(left_fn, kpt_coords_for_example, - kpt_scores_for_example, - instance_inds_for_example) - - # Use dimension values instead of tf.size for tf.lite compatibility. - return tf.cond(num_ind[0] > 0, left_fn, right_fn) - - def _postprocess_keypoints_multi_class(self, prediction_dict, classes, - y_indices, x_indices, boxes, - num_detections): - """Performs postprocessing on keypoint predictions. - - This is the most general keypoint postprocessing function which supports - multiple keypoint tasks (e.g. human and dog keypoints) and multiple object - detection classes. Note that it is the most expensive postprocessing logics - and is currently not tf.lite/tf.js compatible. See - _postprocess_keypoints_single_class if you plan to export the model in more - portable format. - - Args: - prediction_dict: a dictionary holding predicted tensors, returned from the - predict() method. This dictionary should contain keypoint prediction - feature maps for each keypoint task. - classes: A [batch_size, max_detections] int tensor with class indices for - all detected objects. - y_indices: A [batch_size, max_detections] int tensor with y indices for - all object centers. - x_indices: A [batch_size, max_detections] int tensor with x indices for - all object centers. - boxes: A [batch_size, max_detections, 4] float32 tensor with bounding - boxes in (un-normalized) output space. - num_detections: A [batch_size] int tensor with the number of valid - detections for each image. - - Returns: - A tuple of - keypoints: a [batch_size, max_detection, num_total_keypoints, 2] float32 - tensor with keypoints in the output (strided) coordinate frame. - keypoint_scores: a [batch_size, max_detections, num_total_keypoints] - float32 tensor with keypoint scores. - """ - total_num_keypoints = sum(len(kp_dict.keypoint_indices) for kp_dict - in self._kp_params_dict.values()) - batch_size, max_detections = _get_shape(classes, 2) - kpt_coords_for_example_list = [] - kpt_scores_for_example_list = [] - for ex_ind in range(batch_size): - # The tensors that host the keypoint coordinates and scores for all - # instances and all keypoints. They will be updated by scatter_nd_add for - # each keypoint tasks. - kpt_coords_for_example_all_det = tf.zeros( - [max_detections, total_num_keypoints, 2]) - kpt_scores_for_example_all_det = tf.zeros( - [max_detections, total_num_keypoints]) - for task_name, kp_params in self._kp_params_dict.items(): - keypoint_heatmap = prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_HEATMAP)][-1] - keypoint_offsets = prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_OFFSET)][-1] - keypoint_regression = prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_REGRESSION)][-1] - instance_inds = self._get_instance_indices( - classes, num_detections, ex_ind, kp_params.class_id) - - # Gather the feature map locations corresponding to the object class. - y_indices_for_kpt_class = tf.gather(y_indices, instance_inds, axis=1) - x_indices_for_kpt_class = tf.gather(x_indices, instance_inds, axis=1) - if boxes is None: - boxes_for_kpt_class = None - else: - boxes_for_kpt_class = tf.gather(boxes, instance_inds, axis=1) - - # Postprocess keypoints and scores for class and single image. Shapes - # are [1, num_instances_i, num_keypoints_i, 2] and - # [1, num_instances_i, num_keypoints_i], respectively. Note that - # num_instances_i and num_keypoints_i refers to the number of - # instances and keypoints for class i, respectively. - (kpt_coords_for_class, kpt_scores_for_class, _) = ( - self._postprocess_keypoints_for_class_and_image( - keypoint_heatmap, - keypoint_offsets, - keypoint_regression, - classes, - y_indices_for_kpt_class, - x_indices_for_kpt_class, - boxes_for_kpt_class, - ex_ind, - kp_params, - )) - - # Prepare the indices for scatter_nd. The resulting combined_inds has - # the shape of [num_instances_i * num_keypoints_i, 2], where the first - # column corresponds to the instance IDs and the second column - # corresponds to the keypoint IDs. - kpt_inds = tf.constant(kp_params.keypoint_indices, dtype=tf.int32) - kpt_inds = tf.expand_dims(kpt_inds, axis=0) - instance_inds_expand = tf.expand_dims(instance_inds, axis=-1) - kpt_inds_expand = kpt_inds * tf.ones_like(instance_inds_expand) - instance_inds_expand = instance_inds_expand * tf.ones_like(kpt_inds) - combined_inds = tf.stack( - [instance_inds_expand, kpt_inds_expand], axis=2) - combined_inds = tf.reshape(combined_inds, [-1, 2]) - - # Reshape the keypoint coordinates/scores to [num_instances_i * - # num_keypoints_i, 2]/[num_instances_i * num_keypoints_i] to be used - # by scatter_nd_add. - kpt_coords_for_class = tf.reshape(kpt_coords_for_class, [-1, 2]) - kpt_scores_for_class = tf.reshape(kpt_scores_for_class, [-1]) - kpt_coords_for_example_all_det = tf.tensor_scatter_nd_add( - kpt_coords_for_example_all_det, - combined_inds, kpt_coords_for_class) - kpt_scores_for_example_all_det = tf.tensor_scatter_nd_add( - kpt_scores_for_example_all_det, - combined_inds, kpt_scores_for_class) - - kpt_coords_for_example_list.append( - tf.expand_dims(kpt_coords_for_example_all_det, axis=0)) - kpt_scores_for_example_list.append( - tf.expand_dims(kpt_scores_for_example_all_det, axis=0)) - - # Concatenate all keypoints and scores from all examples in the batch. - # Shapes are [batch_size, max_detections, num_total_keypoints, 2] and - # [batch_size, max_detections, num_total_keypoints], respectively. - keypoints = tf.concat(kpt_coords_for_example_list, axis=0) - keypoint_scores = tf.concat(kpt_scores_for_example_list, axis=0) - - return keypoints, keypoint_scores - - def _postprocess_keypoints_single_class(self, prediction_dict, classes, - y_indices, x_indices, boxes, - num_detections): - """Performs postprocessing on keypoint predictions (single class only). - - This function handles the special case of keypoint task that the model - predicts only one class of the bounding box/keypoint (e.g. person). By the - assumption, the function uses only tf.lite supported ops and should run - faster. - - Args: - prediction_dict: a dictionary holding predicted tensors, returned from the - predict() method. This dictionary should contain keypoint prediction - feature maps for each keypoint task. - classes: A [batch_size, max_detections] int tensor with class indices for - all detected objects. - y_indices: A [batch_size, max_detections] int tensor with y indices for - all object centers. - x_indices: A [batch_size, max_detections] int tensor with x indices for - all object centers. - boxes: A [batch_size, max_detections, 4] float32 tensor with bounding - boxes in (un-normalized) output space. - num_detections: A [batch_size] int tensor with the number of valid - detections for each image. - - Returns: - A tuple of - keypoints: a [batch_size, max_detection, num_total_keypoints, 2] float32 - tensor with keypoints in the output (strided) coordinate frame. - keypoint_scores: a [batch_size, max_detections, num_total_keypoints] - float32 tensor with keypoint scores. - """ - # This function only works when there is only one keypoint task and the - # number of classes equal to one. For more general use cases, please use - # _postprocess_keypoints instead. - assert len(self._kp_params_dict) == 1 and self._num_classes == 1 - task_name, kp_params = next(iter(self._kp_params_dict.items())) - keypoint_heatmap = prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_HEATMAP)][-1] - keypoint_offsets = prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_OFFSET)][-1] - keypoint_regression = prediction_dict[ - get_keypoint_name(task_name, KEYPOINT_REGRESSION)][-1] - keypoint_depth_predictions = None - if kp_params.predict_depth: - keypoint_depth_predictions = prediction_dict[get_keypoint_name( - task_name, KEYPOINT_DEPTH)][-1] - - batch_size, _ = _get_shape(classes, 2) - kpt_coords_for_example_list = [] - kpt_scores_for_example_list = [] - kpt_depths_for_example_list = [] - for ex_ind in range(batch_size): - # Postprocess keypoints and scores for class and single image. Shapes - # are [1, max_detections, num_keypoints, 2] and - # [1, max_detections, num_keypoints], respectively. - (kpt_coords_for_class, kpt_scores_for_class, kpt_depths_for_class) = ( - self._postprocess_keypoints_for_class_and_image( - keypoint_heatmap, - keypoint_offsets, - keypoint_regression, - classes, - y_indices, - x_indices, - boxes, - ex_ind, - kp_params, - keypoint_depth_predictions=keypoint_depth_predictions)) - - kpt_coords_for_example_list.append(kpt_coords_for_class) - kpt_scores_for_example_list.append(kpt_scores_for_class) - kpt_depths_for_example_list.append(kpt_depths_for_class) - - # Concatenate all keypoints and scores from all examples in the batch. - # Shapes are [batch_size, max_detections, num_keypoints, 2] and - # [batch_size, max_detections, num_keypoints], respectively. - keypoints = tf.concat(kpt_coords_for_example_list, axis=0) - keypoint_scores = tf.concat(kpt_scores_for_example_list, axis=0) - - keypoint_depths = None - if kp_params.predict_depth: - keypoint_depths = tf.concat(kpt_depths_for_example_list, axis=0) - - return keypoints, keypoint_scores, keypoint_depths - - def _get_instance_indices(self, classes, num_detections, batch_index, - class_id): - """Gets the instance indices that match the target class ID. - - Args: - classes: A [batch_size, max_detections] int tensor with class indices for - all detected objects. - num_detections: A [batch_size] int tensor with the number of valid - detections for each image. - batch_index: An integer specifying the index for an example in the batch. - class_id: Class id - - Returns: - instance_inds: A [num_instances] int32 tensor where each element indicates - the instance location within the `classes` tensor. This is useful to - associate the refined keypoints with the original detections (i.e. - boxes) - """ - classes = classes[batch_index:batch_index+1, ...] - _, max_detections = shape_utils.combined_static_and_dynamic_shape( - classes) - # Get the detection indices corresponding to the target class. - # Call tf.math.equal with matched tensor shape to make it tf.lite - # compatible. - valid_detections_with_kpt_class = tf.math.logical_and( - tf.range(max_detections) < num_detections[batch_index], - tf.math.equal(classes[0], tf.fill(classes[0].shape, class_id))) - instance_inds = tf.where(valid_detections_with_kpt_class)[:, 0] - # Cast the indices tensor to int32 for tf.lite compatibility. - return tf.cast(instance_inds, tf.int32) - - def _postprocess_keypoints_for_class_and_image( - self, - keypoint_heatmap, - keypoint_offsets, - keypoint_regression, - classes, - y_indices, - x_indices, - boxes, - batch_index, - kp_params, - keypoint_depth_predictions=None): - """Postprocess keypoints for a single image and class. - - Args: - keypoint_heatmap: A [batch_size, height, width, num_keypoints] float32 - tensor with keypoint heatmaps. - keypoint_offsets: A [batch_size, height, width, 2] float32 tensor with - local offsets to keypoint centers. - keypoint_regression: A [batch_size, height, width, 2 * num_keypoints] - float32 tensor with regressed offsets to all keypoints. - classes: A [batch_size, max_detections] int tensor with class indices for - all detected objects. - y_indices: A [batch_size, max_detections] int tensor with y indices for - all object centers. - x_indices: A [batch_size, max_detections] int tensor with x indices for - all object centers. - boxes: A [batch_size, max_detections, 4] float32 tensor with detected - boxes in the output (strided) frame. - batch_index: An integer specifying the index for an example in the batch. - kp_params: A `KeypointEstimationParams` object with parameters for a - single keypoint class. - keypoint_depth_predictions: (optional) A [batch_size, height, width, 1] - float32 tensor representing the keypoint depth prediction. - - Returns: - A tuple of - refined_keypoints: A [1, num_instances, num_keypoints, 2] float32 tensor - with refined keypoints for a single class in a single image, expressed - in the output (strided) coordinate frame. Note that `num_instances` is a - dynamic dimension, and corresponds to the number of valid detections - for the specific class. - refined_scores: A [1, num_instances, num_keypoints] float32 tensor with - keypoint scores. - refined_depths: A [1, num_instances, num_keypoints] float32 tensor with - keypoint depths. Return None if the input keypoint_depth_predictions is - None. - """ - num_keypoints = len(kp_params.keypoint_indices) - - keypoint_heatmap = tf.nn.sigmoid( - keypoint_heatmap[batch_index:batch_index+1, ...]) - keypoint_offsets = keypoint_offsets[batch_index:batch_index+1, ...] - keypoint_regression = keypoint_regression[batch_index:batch_index+1, ...] - keypoint_depths = None - if keypoint_depth_predictions is not None: - keypoint_depths = keypoint_depth_predictions[batch_index:batch_index + 1, - ...] - y_indices = y_indices[batch_index:batch_index+1, ...] - x_indices = x_indices[batch_index:batch_index+1, ...] - if boxes is None: - boxes_slice = None - else: - boxes_slice = boxes[batch_index:batch_index+1, ...] - - # Gather the regressed keypoints. Final tensor has shape - # [1, num_instances, num_keypoints, 2]. - regressed_keypoints_for_objects = regressed_keypoints_at_object_centers( - keypoint_regression, y_indices, x_indices) - regressed_keypoints_for_objects = tf.reshape( - regressed_keypoints_for_objects, [1, -1, num_keypoints, 2]) - - # Get the candidate keypoints and scores. - # The shape of keypoint_candidates and keypoint_scores is: - # [1, num_candidates_per_keypoint, num_keypoints, 2] and - # [1, num_candidates_per_keypoint, num_keypoints], respectively. - (keypoint_candidates, keypoint_scores, num_keypoint_candidates, - keypoint_depth_candidates) = ( - prediction_tensors_to_keypoint_candidates( - keypoint_heatmap, - keypoint_offsets, - keypoint_score_threshold=( - kp_params.keypoint_candidate_score_threshold), - max_pool_kernel_size=kp_params.peak_max_pool_kernel_size, - max_candidates=kp_params.num_candidates_per_keypoint, - keypoint_depths=keypoint_depths)) - - kpts_std_dev_postprocess = [ - s * kp_params.std_dev_multiplier for s in kp_params.keypoint_std_dev - ] - # Get the refined keypoints and scores, of shape - # [1, num_instances, num_keypoints, 2] and - # [1, num_instances, num_keypoints], respectively. - (refined_keypoints, refined_scores, refined_depths) = refine_keypoints( - regressed_keypoints_for_objects, - keypoint_candidates, - keypoint_scores, - num_keypoint_candidates, - bboxes=boxes_slice, - unmatched_keypoint_score=kp_params.unmatched_keypoint_score, - box_scale=kp_params.box_scale, - candidate_search_scale=kp_params.candidate_search_scale, - candidate_ranking_mode=kp_params.candidate_ranking_mode, - score_distance_offset=kp_params.score_distance_offset, - keypoint_depth_candidates=keypoint_depth_candidates, - keypoint_score_threshold=(kp_params.keypoint_candidate_score_threshold), - score_distance_multiplier=kp_params.score_distance_multiplier, - keypoint_std_dev=kpts_std_dev_postprocess) - - return refined_keypoints, refined_scores, refined_depths - - def regularization_losses(self): - return [] - - def restore_map(self, - fine_tune_checkpoint_type='detection', - load_all_detection_checkpoint_vars=False): - raise RuntimeError('CenterNetMetaArch not supported under TF1.x.') - - def restore_from_objects(self, fine_tune_checkpoint_type='detection'): - """Returns a map of Trackable objects to load from a foreign checkpoint. - - Returns a dictionary of Tensorflow 2 Trackable objects (e.g. tf.Module - or Checkpoint). This enables the model to initialize based on weights from - another task. For example, the feature extractor variables from a - classification model can be used to bootstrap training of an object - detector. When loading from an object detection model, the checkpoint model - should have the same parameters as this detection model with exception of - the num_classes parameter. - - Note that this function is intended to be used to restore Keras-based - models when running Tensorflow 2, whereas restore_map (not implemented - in CenterNet) is intended to be used to restore Slim-based models when - running Tensorflow 1.x. - - TODO(jonathanhuang): Make this function consistent with other - meta-architectures. - - Args: - fine_tune_checkpoint_type: whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`, `fine_tune`. - Default 'detection'. - 'detection': used when loading models pre-trained on other detection - tasks. With this checkpoint type the weights of the feature extractor - are expected under the attribute 'feature_extractor'. - 'classification': used when loading models pre-trained on an image - classification task. Note that only the encoder section of the network - is loaded and not the upsampling layers. With this checkpoint type, - the weights of only the encoder section are expected under the - attribute 'feature_extractor'. - 'fine_tune': used when loading the entire CenterNet feature extractor - pre-trained on other tasks. The checkpoints saved during CenterNet - model training can be directly loaded using this type. With this - checkpoint type, the weights of the feature extractor are expected - under the attribute 'model._feature_extractor'. - For more details, see the tensorflow section on Loading mechanics. - https://www.tensorflow.org/guide/checkpoint#loading_mechanics - - Returns: - A dict mapping keys to Trackable objects (tf.Module or Checkpoint). - """ - - if fine_tune_checkpoint_type == 'detection': - feature_extractor_model = tf.train.Checkpoint( - _feature_extractor=self._feature_extractor) - return {'model': feature_extractor_model} - - elif fine_tune_checkpoint_type == 'classification': - return { - 'feature_extractor': - self._feature_extractor.classification_backbone - } - elif fine_tune_checkpoint_type == 'full': - return {'model': self} - elif fine_tune_checkpoint_type == 'fine_tune': - raise ValueError(('"fine_tune" is no longer supported for CenterNet. ' - 'Please set fine_tune_checkpoint_type to "detection"' - ' which has the same functionality. If you are using' - ' the ExtremeNet checkpoint, download the new version' - ' from the model zoo.')) - - else: - raise ValueError('Unknown fine tune checkpoint type {}'.format( - fine_tune_checkpoint_type)) - - def updates(self): - if tf_version.is_tf2(): - raise RuntimeError('This model is intended to be used with model_lib_v2 ' - 'which does not support updates()') - else: - update_ops = [] - slim_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) - # Copy the slim ops to avoid modifying the collection - if slim_update_ops: - update_ops.extend(slim_update_ops) - return update_ops diff --git a/research/object_detection/meta_architectures/center_net_meta_arch_tf2_test.py b/research/object_detection/meta_architectures/center_net_meta_arch_tf2_test.py deleted file mode 100644 index 02d38d12678..00000000000 --- a/research/object_detection/meta_architectures/center_net_meta_arch_tf2_test.py +++ /dev/null @@ -1,3600 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the CenterNet Meta architecture code.""" - -from __future__ import division - -import functools -import unittest - -from absl.testing import parameterized -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.builders import post_processing_builder -from object_detection.core import keypoint_ops -from object_detection.core import losses -from object_detection.core import preprocessor -from object_detection.core import standard_fields as fields -from object_detection.core import target_assigner as cn_assigner -from object_detection.meta_architectures import center_net_meta_arch as cnma -from object_detection.models import center_net_resnet_feature_extractor -from object_detection.protos import post_processing_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMetaArchPredictionHeadTest( - test_case.TestCase, parameterized.TestCase): - """Test CenterNet meta architecture prediction head.""" - - @parameterized.parameters([True, False]) - def test_prediction_head(self, use_depthwise): - head = cnma.make_prediction_net(num_out_channels=7, - use_depthwise=use_depthwise) - output = head(np.zeros((4, 128, 128, 8))) - - self.assertEqual((4, 128, 128, 7), output.shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMetaArchHelpersTest(test_case.TestCase, parameterized.TestCase): - """Test for CenterNet meta architecture related functions.""" - - def test_row_col_channel_indices_from_flattened_indices(self): - """Tests that the computation of row, col, channel indices is correct.""" - - r_grid, c_grid, ch_grid = (np.zeros((5, 4, 3), dtype=int), - np.zeros((5, 4, 3), dtype=int), - np.zeros((5, 4, 3), dtype=int)) - - r_grid[..., 0] = r_grid[..., 1] = r_grid[..., 2] = np.array( - [[0, 0, 0, 0], - [1, 1, 1, 1], - [2, 2, 2, 2], - [3, 3, 3, 3], - [4, 4, 4, 4]] - ) - - c_grid[..., 0] = c_grid[..., 1] = c_grid[..., 2] = np.array( - [[0, 1, 2, 3], - [0, 1, 2, 3], - [0, 1, 2, 3], - [0, 1, 2, 3], - [0, 1, 2, 3]] - ) - - for i in range(3): - ch_grid[..., i] = i - - indices = np.arange(60) - ri, ci, chi = cnma.row_col_channel_indices_from_flattened_indices( - indices, 4, 3) - - np.testing.assert_array_equal(ri, r_grid.flatten()) - np.testing.assert_array_equal(ci, c_grid.flatten()) - np.testing.assert_array_equal(chi, ch_grid.flatten()) - - def test_row_col_indices_from_flattened_indices(self): - """Tests that the computation of row, col indices is correct.""" - - r_grid = np.array([[0, 0, 0, 0], [1, 1, 1, 1], [2, 2, 2, 2], [3, 3, 3, 3], - [4, 4, 4, 4]]) - - c_grid = np.array([[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], - [0, 1, 2, 3]]) - - indices = np.arange(20) - ri, ci, = cnma.row_col_indices_from_flattened_indices(indices, 4) - - np.testing.assert_array_equal(ri, r_grid.flatten()) - np.testing.assert_array_equal(ci, c_grid.flatten()) - - def test_flattened_indices_from_row_col_indices(self): - - r = np.array( - [[0, 0, 0, 0], - [1, 1, 1, 1], - [2, 2, 2, 2]] - ) - - c = np.array( - [[0, 1, 2, 3], - [0, 1, 2, 3], - [0, 1, 2, 3]] - ) - - idx = cnma.flattened_indices_from_row_col_indices(r, c, 4) - np.testing.assert_array_equal(np.arange(12), idx.flatten()) - - def test_get_valid_anchor_weights_in_flattened_image(self): - """Tests that the anchor weights are valid upon flattening out.""" - - valid_weights = np.zeros((2, 5, 5), dtype=float) - - valid_weights[0, :3, :4] = 1.0 - valid_weights[1, :2, :2] = 1.0 - - def graph_fn(): - true_image_shapes = tf.constant([[3, 4], [2, 2]]) - w = cnma.get_valid_anchor_weights_in_flattened_image( - true_image_shapes, 5, 5) - return w - - w = self.execute(graph_fn, []) - np.testing.assert_allclose(w, valid_weights.reshape(2, -1)) - self.assertEqual((2, 25), w.shape) - - def test_convert_strided_predictions_to_normalized_boxes(self): - """Tests that boxes have correct coordinates in normalized input space.""" - - def graph_fn(): - boxes = np.zeros((2, 3, 4), dtype=np.float32) - - boxes[0] = [[10, 20, 30, 40], [20, 30, 50, 100], [50, 60, 100, 180]] - boxes[1] = [[-5, -5, 5, 5], [45, 60, 110, 120], [150, 150, 200, 250]] - - true_image_shapes = tf.constant([[100, 90, 3], [150, 150, 3]]) - - clipped_boxes = ( - cnma.convert_strided_predictions_to_normalized_boxes( - boxes, 2, true_image_shapes)) - return clipped_boxes - - clipped_boxes = self.execute(graph_fn, []) - - expected_boxes = np.zeros((2, 3, 4), dtype=np.float32) - expected_boxes[0] = [[0.2, 4./9, 0.6, 8./9], [0.4, 2./3, 1, 1], - [1, 1, 1, 1]] - expected_boxes[1] = [[0., 0, 1./15, 1./15], [3./5, 4./5, 1, 1], - [1, 1, 1, 1]] - - np.testing.assert_allclose(expected_boxes, clipped_boxes) - - @parameterized.parameters( - {'clip_to_window': True}, - {'clip_to_window': False} - ) - def test_convert_strided_predictions_to_normalized_keypoints( - self, clip_to_window): - """Tests that keypoints have correct coordinates in normalized coords.""" - - keypoint_coords_np = np.array( - [ - # Example 0. - [ - [[-10., 8.], [60., 22.], [60., 120.]], - [[20., 20.], [0., 0.], [0., 0.]], - ], - # Example 1. - [ - [[40., 50.], [20., 160.], [200., 150.]], - [[10., 0.], [40., 10.], [0., 0.]], - ], - ], dtype=np.float32) - keypoint_scores_np = np.array( - [ - # Example 0. - [ - [1.0, 0.9, 0.2], - [0.7, 0.0, 0.0], - ], - # Example 1. - [ - [1.0, 1.0, 0.2], - [0.7, 0.6, 0.0], - ], - ], dtype=np.float32) - - def graph_fn(): - keypoint_coords = tf.constant(keypoint_coords_np, dtype=tf.float32) - keypoint_scores = tf.constant(keypoint_scores_np, dtype=tf.float32) - true_image_shapes = tf.constant([[320, 400, 3], [640, 640, 3]]) - stride = 4 - - keypoint_coords_out, keypoint_scores_out = ( - cnma.convert_strided_predictions_to_normalized_keypoints( - keypoint_coords, keypoint_scores, stride, true_image_shapes, - clip_to_window)) - return keypoint_coords_out, keypoint_scores_out - - keypoint_coords_out, keypoint_scores_out = self.execute(graph_fn, []) - - if clip_to_window: - expected_keypoint_coords_np = np.array( - [ - # Example 0. - [ - [[0.0, 0.08], [0.75, 0.22], [0.75, 1.0]], - [[0.25, 0.2], [0., 0.], [0.0, 0.0]], - ], - # Example 1. - [ - [[0.25, 0.3125], [0.125, 1.0], [1.0, 0.9375]], - [[0.0625, 0.], [0.25, 0.0625], [0., 0.]], - ], - ], dtype=np.float32) - expected_keypoint_scores_np = np.array( - [ - # Example 0. - [ - [0.0, 0.9, 0.0], - [0.7, 0.0, 0.0], - ], - # Example 1. - [ - [1.0, 1.0, 0.0], - [0.7, 0.6, 0.0], - ], - ], dtype=np.float32) - else: - expected_keypoint_coords_np = np.array( - [ - # Example 0. - [ - [[-0.125, 0.08], [0.75, 0.22], [0.75, 1.2]], - [[0.25, 0.2], [0., 0.], [0., 0.]], - ], - # Example 1. - [ - [[0.25, 0.3125], [0.125, 1.0], [1.25, 0.9375]], - [[0.0625, 0.], [0.25, 0.0625], [0., 0.]], - ], - ], dtype=np.float32) - expected_keypoint_scores_np = np.array( - [ - # Example 0. - [ - [1.0, 0.9, 0.2], - [0.7, 0.0, 0.0], - ], - # Example 1. - [ - [1.0, 1.0, 0.2], - [0.7, 0.6, 0.0], - ], - ], dtype=np.float32) - np.testing.assert_allclose(expected_keypoint_coords_np, keypoint_coords_out) - np.testing.assert_allclose(expected_keypoint_scores_np, keypoint_scores_out) - - def test_convert_strided_predictions_to_instance_masks(self): - - def graph_fn(): - boxes = tf.constant( - [ - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.5, 0.5, 1.0], - [0.0, 0.0, 0.0, 0.0]], - ], tf.float32) - classes = tf.constant( - [ - [0, 1, 0], - ], tf.int32) - masks_np = np.zeros((1, 4, 4, 2), dtype=np.float32) - masks_np[0, :, 2:, 0] = 1 # Class 0. - masks_np[0, :, :3, 1] = 1 # Class 1. - masks = tf.constant(masks_np) - true_image_shapes = tf.constant([[6, 8, 3]]) - instance_masks, _ = cnma.convert_strided_predictions_to_instance_masks( - boxes, classes, masks, stride=2, mask_height=2, mask_width=2, - true_image_shapes=true_image_shapes) - return instance_masks - - instance_masks = self.execute_cpu(graph_fn, []) - - expected_instance_masks = np.array( - [ - [ - # Mask 0 (class 0). - [[1, 1], - [1, 1]], - # Mask 1 (class 1). - [[1, 0], - [1, 0]], - # Mask 2 (class 0). - [[0, 0], - [0, 0]], - ] - ]) - np.testing.assert_array_equal(expected_instance_masks, instance_masks) - - def test_convert_strided_predictions_raises_error_with_one_tensor(self): - def graph_fn(): - boxes = tf.constant( - [ - [[0.5, 0.5, 1.0, 1.0], - [0.0, 0.5, 0.5, 1.0], - [0.0, 0.0, 0.0, 0.0]], - ], tf.float32) - classes = tf.constant( - [ - [0, 1, 0], - ], tf.int32) - masks_np = np.zeros((1, 4, 4, 2), dtype=np.float32) - masks_np[0, :, 2:, 0] = 1 # Class 0. - masks_np[0, :, :3, 1] = 1 # Class 1. - masks = tf.constant(masks_np) - true_image_shapes = tf.constant([[6, 8, 3]]) - densepose_part_heatmap = tf.random.uniform( - [1, 4, 4, 24]) - instance_masks, _ = cnma.convert_strided_predictions_to_instance_masks( - boxes, classes, masks, true_image_shapes, - densepose_part_heatmap=densepose_part_heatmap, - densepose_surface_coords=None) - return instance_masks - - with self.assertRaises(ValueError): - self.execute_cpu(graph_fn, []) - - def test_crop_and_threshold_masks(self): - boxes_np = np.array( - [[0., 0., 0.5, 0.5], - [0.25, 0.25, 1.0, 1.0]], dtype=np.float32) - classes_np = np.array([0, 2], dtype=np.int32) - masks_np = np.zeros((4, 4, _NUM_CLASSES), dtype=np.float32) - masks_np[0, 0, 0] = 0.8 - masks_np[1, 1, 0] = 0.6 - masks_np[3, 3, 2] = 0.7 - part_heatmap_np = np.zeros((4, 4, _DENSEPOSE_NUM_PARTS), dtype=np.float32) - part_heatmap_np[0, 0, 4] = 1 - part_heatmap_np[0, 0, 2] = 0.6 # Lower scoring. - part_heatmap_np[1, 1, 8] = 0.2 - part_heatmap_np[3, 3, 4] = 0.5 - surf_coords_np = np.zeros((4, 4, 2 * _DENSEPOSE_NUM_PARTS), - dtype=np.float32) - surf_coords_np[:, :, 8:10] = 0.2, 0.9 - surf_coords_np[:, :, 16:18] = 0.3, 0.5 - true_height, true_width = 10, 10 - input_height, input_width = 10, 10 - mask_height = 4 - mask_width = 4 - def graph_fn(): - elems = [ - tf.constant(boxes_np), - tf.constant(classes_np), - tf.constant(masks_np), - tf.constant(part_heatmap_np), - tf.constant(surf_coords_np), - tf.constant(true_height, dtype=tf.int32), - tf.constant(true_width, dtype=tf.int32) - ] - part_masks, surface_coords = cnma.crop_and_threshold_masks( - elems, input_height, input_width, mask_height=mask_height, - mask_width=mask_width, densepose_class_index=0) - return part_masks, surface_coords - - part_masks, surface_coords = self.execute_cpu(graph_fn, []) - - expected_part_masks = np.zeros((2, 4, 4), dtype=np.uint8) - expected_part_masks[0, 0, 0] = 5 # Recall classes are 1-indexed in output. - expected_part_masks[0, 2, 2] = 9 # Recall classes are 1-indexed in output. - expected_part_masks[1, 3, 3] = 1 # Standard instance segmentation mask. - expected_surface_coords = np.zeros((2, 4, 4, 2), dtype=np.float32) - expected_surface_coords[0, 0, 0, :] = 0.2, 0.9 - expected_surface_coords[0, 2, 2, :] = 0.3, 0.5 - np.testing.assert_allclose(expected_part_masks, part_masks) - np.testing.assert_allclose(expected_surface_coords, surface_coords) - - def test_gather_surface_coords_for_parts(self): - surface_coords_cropped_np = np.zeros((2, 5, 5, _DENSEPOSE_NUM_PARTS, 2), - dtype=np.float32) - surface_coords_cropped_np[0, 0, 0, 5] = 0.3, 0.4 - surface_coords_cropped_np[0, 1, 0, 9] = 0.5, 0.6 - highest_scoring_part_np = np.zeros((2, 5, 5), dtype=np.int32) - highest_scoring_part_np[0, 0, 0] = 5 - highest_scoring_part_np[0, 1, 0] = 9 - def graph_fn(): - surface_coords_cropped = tf.constant(surface_coords_cropped_np, - tf.float32) - highest_scoring_part = tf.constant(highest_scoring_part_np, tf.int32) - surface_coords_gathered = cnma.gather_surface_coords_for_parts( - surface_coords_cropped, highest_scoring_part) - return surface_coords_gathered - - surface_coords_gathered = self.execute_cpu(graph_fn, []) - - np.testing.assert_allclose([0.3, 0.4], surface_coords_gathered[0, 0, 0]) - np.testing.assert_allclose([0.5, 0.6], surface_coords_gathered[0, 1, 0]) - - def test_top_k_feature_map_locations(self): - feature_map_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - feature_map_np[0, 2, 0, 1] = 1.0 - feature_map_np[0, 2, 1, 1] = 0.9 # Get's filtered due to max pool. - feature_map_np[0, 0, 1, 0] = 0.7 - feature_map_np[0, 2, 2, 0] = 0.5 - feature_map_np[0, 2, 2, 1] = -0.3 - feature_map_np[1, 2, 1, 1] = 0.7 - feature_map_np[1, 1, 0, 0] = 0.4 - feature_map_np[1, 1, 2, 0] = 0.1 - - def graph_fn(): - feature_map = tf.constant(feature_map_np) - scores, y_inds, x_inds, channel_inds = ( - cnma.top_k_feature_map_locations( - feature_map, max_pool_kernel_size=3, k=3)) - return scores, y_inds, x_inds, channel_inds - - scores, y_inds, x_inds, channel_inds = self.execute(graph_fn, []) - - np.testing.assert_allclose([1.0, 0.7, 0.5], scores[0]) - np.testing.assert_array_equal([2, 0, 2], y_inds[0]) - np.testing.assert_array_equal([0, 1, 2], x_inds[0]) - np.testing.assert_array_equal([1, 0, 0], channel_inds[0]) - - np.testing.assert_allclose([0.7, 0.4, 0.1], scores[1]) - np.testing.assert_array_equal([2, 1, 1], y_inds[1]) - np.testing.assert_array_equal([1, 0, 2], x_inds[1]) - np.testing.assert_array_equal([1, 0, 0], channel_inds[1]) - - def test_top_k_feature_map_locations_no_pooling(self): - feature_map_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - feature_map_np[0, 2, 0, 1] = 1.0 - feature_map_np[0, 2, 1, 1] = 0.9 - feature_map_np[0, 0, 1, 0] = 0.7 - feature_map_np[0, 2, 2, 0] = 0.5 - feature_map_np[0, 2, 2, 1] = -0.3 - feature_map_np[1, 2, 1, 1] = 0.7 - feature_map_np[1, 1, 0, 0] = 0.4 - feature_map_np[1, 1, 2, 0] = 0.1 - - def graph_fn(): - feature_map = tf.constant(feature_map_np) - scores, y_inds, x_inds, channel_inds = ( - cnma.top_k_feature_map_locations( - feature_map, max_pool_kernel_size=1, k=3)) - return scores, y_inds, x_inds, channel_inds - - scores, y_inds, x_inds, channel_inds = self.execute(graph_fn, []) - - np.testing.assert_allclose([1.0, 0.9, 0.7], scores[0]) - np.testing.assert_array_equal([2, 2, 0], y_inds[0]) - np.testing.assert_array_equal([0, 1, 1], x_inds[0]) - np.testing.assert_array_equal([1, 1, 0], channel_inds[0]) - - np.testing.assert_allclose([0.7, 0.4, 0.1], scores[1]) - np.testing.assert_array_equal([2, 1, 1], y_inds[1]) - np.testing.assert_array_equal([1, 0, 2], x_inds[1]) - np.testing.assert_array_equal([1, 0, 0], channel_inds[1]) - - def test_top_k_feature_map_locations_very_large(self): - feature_map_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - feature_map_np[0, 2, 0, 1] = 1.0 - - def graph_fn(): - feature_map = tf.constant(feature_map_np) - feature_map.set_shape(tf.TensorShape([2, 3, None, 2])) - scores, y_inds, x_inds, channel_inds = ( - cnma.top_k_feature_map_locations( - feature_map, max_pool_kernel_size=1, k=3000)) - return scores, y_inds, x_inds, channel_inds - # graph execution will fail if large k's are not handled. - scores, y_inds, x_inds, channel_inds = self.execute(graph_fn, []) - self.assertEqual(scores.shape, (2, 18)) - self.assertEqual(y_inds.shape, (2, 18)) - self.assertEqual(x_inds.shape, (2, 18)) - self.assertEqual(channel_inds.shape, (2, 18)) - - def test_top_k_feature_map_locations_per_channel(self): - feature_map_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - feature_map_np[0, 2, 0, 0] = 1.0 # Selected. - feature_map_np[0, 2, 1, 0] = 0.9 # Get's filtered due to max pool. - feature_map_np[0, 0, 1, 0] = 0.7 # Selected. - feature_map_np[0, 2, 2, 1] = 0.5 # Selected. - feature_map_np[0, 0, 0, 1] = 0.3 # Selected. - feature_map_np[1, 2, 1, 0] = 0.7 # Selected. - feature_map_np[1, 1, 0, 0] = 0.4 # Get's filtered due to max pool. - feature_map_np[1, 1, 2, 0] = 0.3 # Get's filtered due to max pool. - feature_map_np[1, 1, 0, 1] = 0.8 # Selected. - feature_map_np[1, 1, 2, 1] = 0.3 # Selected. - - def graph_fn(): - feature_map = tf.constant(feature_map_np) - scores, y_inds, x_inds, channel_inds = ( - cnma.top_k_feature_map_locations( - feature_map, max_pool_kernel_size=3, k=2, per_channel=True)) - return scores, y_inds, x_inds, channel_inds - - scores, y_inds, x_inds, channel_inds = self.execute(graph_fn, []) - - np.testing.assert_allclose([1.0, 0.7, 0.5, 0.3], scores[0]) - np.testing.assert_array_equal([2, 0, 2, 0], y_inds[0]) - np.testing.assert_array_equal([0, 1, 2, 0], x_inds[0]) - np.testing.assert_array_equal([0, 0, 1, 1], channel_inds[0]) - - np.testing.assert_allclose([0.7, 0.0, 0.8, 0.3], scores[1]) - np.testing.assert_array_equal([2, 0, 1, 1], y_inds[1]) - np.testing.assert_array_equal([1, 0, 0, 2], x_inds[1]) - np.testing.assert_array_equal([0, 0, 1, 1], channel_inds[1]) - - def test_top_k_feature_map_locations_k1(self): - feature_map_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - feature_map_np[0, 2, 0, 0] = 1.0 # Selected. - feature_map_np[0, 2, 1, 0] = 0.9 - feature_map_np[0, 0, 1, 0] = 0.7 - feature_map_np[0, 2, 2, 1] = 0.5 - feature_map_np[0, 0, 0, 1] = 0.3 - feature_map_np[1, 2, 1, 0] = 0.7 - feature_map_np[1, 1, 0, 0] = 0.4 - feature_map_np[1, 1, 2, 0] = 0.3 - feature_map_np[1, 1, 0, 1] = 0.8 # Selected. - feature_map_np[1, 1, 2, 1] = 0.3 - - def graph_fn(): - feature_map = tf.constant(feature_map_np) - scores, y_inds, x_inds, channel_inds = ( - cnma.top_k_feature_map_locations( - feature_map, max_pool_kernel_size=3, k=1, per_channel=False)) - return scores, y_inds, x_inds, channel_inds - - scores, y_inds, x_inds, channel_inds = self.execute(graph_fn, []) - - np.testing.assert_allclose([1.0], scores[0]) - np.testing.assert_array_equal([2], y_inds[0]) - np.testing.assert_array_equal([0], x_inds[0]) - np.testing.assert_array_equal([0], channel_inds[0]) - - np.testing.assert_allclose([0.8], scores[1]) - np.testing.assert_array_equal([1], y_inds[1]) - np.testing.assert_array_equal([0], x_inds[1]) - np.testing.assert_array_equal([1], channel_inds[1]) - - def test_top_k_feature_map_locations_k1_per_channel(self): - feature_map_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - feature_map_np[0, 2, 0, 0] = 1.0 # Selected. - feature_map_np[0, 2, 1, 0] = 0.9 - feature_map_np[0, 0, 1, 0] = 0.7 - feature_map_np[0, 2, 2, 1] = 0.5 # Selected. - feature_map_np[0, 0, 0, 1] = 0.3 - feature_map_np[1, 2, 1, 0] = 0.7 # Selected. - feature_map_np[1, 1, 0, 0] = 0.4 - feature_map_np[1, 1, 2, 0] = 0.3 - feature_map_np[1, 1, 0, 1] = 0.8 # Selected. - feature_map_np[1, 1, 2, 1] = 0.3 - - def graph_fn(): - feature_map = tf.constant(feature_map_np) - scores, y_inds, x_inds, channel_inds = ( - cnma.top_k_feature_map_locations( - feature_map, max_pool_kernel_size=3, k=1, per_channel=True)) - return scores, y_inds, x_inds, channel_inds - - scores, y_inds, x_inds, channel_inds = self.execute(graph_fn, []) - - np.testing.assert_allclose([1.0, 0.5], scores[0]) - np.testing.assert_array_equal([2, 2], y_inds[0]) - np.testing.assert_array_equal([0, 2], x_inds[0]) - np.testing.assert_array_equal([0, 1], channel_inds[0]) - - np.testing.assert_allclose([0.7, 0.8], scores[1]) - np.testing.assert_array_equal([2, 1], y_inds[1]) - np.testing.assert_array_equal([1, 0], x_inds[1]) - np.testing.assert_array_equal([0, 1], channel_inds[1]) - - def test_box_prediction(self): - - class_pred = np.zeros((3, 128, 128, 5), dtype=np.float32) - hw_pred = np.zeros((3, 128, 128, 2), dtype=np.float32) - offset_pred = np.zeros((3, 128, 128, 2), dtype=np.float32) - - # Sample 1, 2 boxes - class_pred[0, 10, 20] = [0.3, .7, 0.0, 0.0, 0.0] - hw_pred[0, 10, 20] = [40, 60] - offset_pred[0, 10, 20] = [1, 2] - - class_pred[0, 50, 60] = [0.55, 0.0, 0.0, 0.0, 0.45] - hw_pred[0, 50, 60] = [50, 50] - offset_pred[0, 50, 60] = [0, 0] - - # Sample 2, 2 boxes (at same location) - class_pred[1, 100, 100] = [0.0, 0.1, 0.9, 0.0, 0.0] - hw_pred[1, 100, 100] = [10, 10] - offset_pred[1, 100, 100] = [1, 3] - - # Sample 3, 3 boxes - class_pred[2, 60, 90] = [0.0, 0.0, 0.0, 0.2, 0.8] - hw_pred[2, 60, 90] = [40, 30] - offset_pred[2, 60, 90] = [0, 0] - - class_pred[2, 65, 95] = [0.0, 0.7, 0.3, 0.0, 0.0] - hw_pred[2, 65, 95] = [20, 20] - offset_pred[2, 65, 95] = [1, 2] - - class_pred[2, 75, 85] = [1.0, 0.0, 0.0, 0.0, 0.0] - hw_pred[2, 75, 85] = [21, 25] - offset_pred[2, 75, 85] = [5, 2] - - def graph_fn(): - class_pred_tensor = tf.constant(class_pred) - hw_pred_tensor = tf.constant(hw_pred) - offset_pred_tensor = tf.constant(offset_pred) - - _, y_indices, x_indices, _ = ( - cnma.top_k_feature_map_locations( - class_pred_tensor, max_pool_kernel_size=3, k=2)) - - boxes = cnma.prediction_tensors_to_boxes( - y_indices, x_indices, hw_pred_tensor, offset_pred_tensor) - return boxes - - boxes = self.execute(graph_fn, []) - - np.testing.assert_allclose( - [[0, 0, 31, 52], [25, 35, 75, 85]], boxes[0]) - np.testing.assert_allclose( - [[96, 98, 106, 108], [96, 98, 106, 108]], boxes[1]) - np.testing.assert_allclose( - [[69.5, 74.5, 90.5, 99.5], [40, 75, 80, 105]], boxes[2]) - - def test_offset_prediction(self): - - class_pred = np.zeros((3, 128, 128, 5), dtype=np.float32) - offset_pred = np.zeros((3, 128, 128, 2), dtype=np.float32) - - # Sample 1, 2 boxes - class_pred[0, 10, 20] = [0.3, .7, 0.0, 0.0, 0.0] - offset_pred[0, 10, 20] = [1, 2] - - class_pred[0, 50, 60] = [0.55, 0.0, 0.0, 0.0, 0.45] - offset_pred[0, 50, 60] = [0, 0] - - # Sample 2, 2 boxes (at same location) - class_pred[1, 100, 100] = [0.0, 0.1, 0.9, 0.0, 0.0] - offset_pred[1, 100, 100] = [1, 3] - - # Sample 3, 3 boxes - class_pred[2, 60, 90] = [0.0, 0.0, 0.0, 0.2, 0.8] - offset_pred[2, 60, 90] = [0, 0] - - class_pred[2, 65, 95] = [0.0, 0.7, 0.3, 0.0, 0.0] - offset_pred[2, 65, 95] = [1, 2] - - class_pred[2, 75, 85] = [1.0, 0.0, 0.0, 0.0, 0.0] - offset_pred[2, 75, 85] = [5, 2] - - def graph_fn(): - class_pred_tensor = tf.constant(class_pred) - offset_pred_tensor = tf.constant(offset_pred) - - _, y_indices, x_indices, _ = ( - cnma.top_k_feature_map_locations( - class_pred_tensor, max_pool_kernel_size=3, k=2)) - - offsets = cnma.prediction_tensors_to_temporal_offsets( - y_indices, x_indices, offset_pred_tensor) - return offsets - - offsets = self.execute(graph_fn, []) - - np.testing.assert_allclose( - [[1, 2], [0, 0]], offsets[0]) - np.testing.assert_allclose( - [[1, 3], [1, 3]], offsets[1]) - np.testing.assert_allclose( - [[5, 2], [0, 0]], offsets[2]) - - def test_keypoint_candidate_prediction(self): - keypoint_heatmap_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - keypoint_heatmap_np[0, 0, 0, 0] = 1.0 - keypoint_heatmap_np[0, 2, 1, 0] = 0.7 - keypoint_heatmap_np[0, 1, 1, 0] = 0.6 - keypoint_heatmap_np[0, 0, 2, 1] = 0.7 - keypoint_heatmap_np[0, 1, 1, 1] = 0.3 # Filtered by low score. - keypoint_heatmap_np[0, 2, 2, 1] = 0.2 - keypoint_heatmap_np[1, 1, 0, 0] = 0.6 - keypoint_heatmap_np[1, 2, 1, 0] = 0.5 - keypoint_heatmap_np[1, 0, 0, 0] = 0.4 - keypoint_heatmap_np[1, 0, 0, 1] = 1.0 - keypoint_heatmap_np[1, 0, 1, 1] = 0.9 - keypoint_heatmap_np[1, 2, 0, 1] = 0.8 - - keypoint_heatmap_offsets_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - keypoint_heatmap_offsets_np[0, 0, 0] = [0.5, 0.25] - keypoint_heatmap_offsets_np[0, 2, 1] = [-0.25, 0.5] - keypoint_heatmap_offsets_np[0, 1, 1] = [0.0, 0.0] - keypoint_heatmap_offsets_np[0, 0, 2] = [1.0, 0.0] - keypoint_heatmap_offsets_np[0, 2, 2] = [1.0, 1.0] - keypoint_heatmap_offsets_np[1, 1, 0] = [0.25, 0.5] - keypoint_heatmap_offsets_np[1, 2, 1] = [0.5, 0.0] - keypoint_heatmap_offsets_np[1, 0, 0] = [0.0, -0.5] - keypoint_heatmap_offsets_np[1, 0, 1] = [0.5, -0.5] - keypoint_heatmap_offsets_np[1, 2, 0] = [-1.0, -0.5] - - def graph_fn(): - keypoint_heatmap = tf.constant(keypoint_heatmap_np, dtype=tf.float32) - keypoint_heatmap_offsets = tf.constant( - keypoint_heatmap_offsets_np, dtype=tf.float32) - - (keypoint_cands, keypoint_scores, num_keypoint_candidates, _) = ( - cnma.prediction_tensors_to_keypoint_candidates( - keypoint_heatmap, - keypoint_heatmap_offsets, - keypoint_score_threshold=0.5, - max_pool_kernel_size=1, - max_candidates=2)) - return keypoint_cands, keypoint_scores, num_keypoint_candidates - - (keypoint_cands, keypoint_scores, - num_keypoint_candidates) = self.execute(graph_fn, []) - - expected_keypoint_candidates = [ - [ # Example 0. - [[0.5, 0.25], [1.0, 2.0]], # Keypoint 1. - [[1.75, 1.5], [1.0, 1.0]], # Keypoint 2. - ], - [ # Example 1. - [[1.25, 0.5], [0.0, -0.5]], # Keypoint 1. - [[2.5, 1.0], [0.5, 0.5]], # Keypoint 2. - ], - ] - expected_keypoint_scores = [ - [ # Example 0. - [1.0, 0.7], # Keypoint 1. - [0.7, 0.3], # Keypoint 2. - ], - [ # Example 1. - [0.6, 1.0], # Keypoint 1. - [0.5, 0.9], # Keypoint 2. - ], - ] - expected_num_keypoint_candidates = [ - [2, 1], - [2, 2] - ] - np.testing.assert_allclose(expected_keypoint_candidates, keypoint_cands) - np.testing.assert_allclose(expected_keypoint_scores, keypoint_scores) - np.testing.assert_array_equal(expected_num_keypoint_candidates, - num_keypoint_candidates) - - def test_prediction_to_single_instance_keypoints(self): - image_size = (9, 9) - object_heatmap_np = np.zeros((1, image_size[0], image_size[1], 1), - dtype=np.float32) - # This should be picked. - object_heatmap_np[0, 4, 4, 0] = 0.9 - # This shouldn't be picked since it's farther away from the center. - object_heatmap_np[0, 2, 2, 0] = 1.0 - - keypoint_heatmap_np = np.zeros((1, image_size[0], image_size[1], 4), - dtype=np.float32) - # Top-left corner should be picked. - keypoint_heatmap_np[0, 1, 1, 0] = 0.9 - keypoint_heatmap_np[0, 4, 4, 0] = 1.0 - # Top-right corner should be picked. - keypoint_heatmap_np[0, 1, 7, 1] = 0.9 - keypoint_heatmap_np[0, 4, 4, 1] = 1.0 - # Bottom-left corner should be picked. - keypoint_heatmap_np[0, 7, 1, 2] = 0.9 - keypoint_heatmap_np[0, 4, 4, 2] = 1.0 - # Bottom-right corner should be picked. - keypoint_heatmap_np[0, 7, 7, 3] = 0.9 - keypoint_heatmap_np[0, 4, 4, 3] = 1.0 - - keypoint_offset_np = np.zeros((1, image_size[0], image_size[1], 8), - dtype=np.float32) - keypoint_offset_np[0, 1, 1] = [0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] - keypoint_offset_np[0, 1, 7] = [0.0, 0.0, 0.5, -0.5, 0.0, 0.0, 0.0, 0.0] - keypoint_offset_np[0, 7, 1] = [0.0, 0.0, 0.0, 0.0, -0.5, 0.5, 0.0, 0.0] - keypoint_offset_np[0, 7, 7] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5, -0.5] - - keypoint_regression_np = np.zeros((1, image_size[0], image_size[1], 8), - dtype=np.float32) - keypoint_regression_np[0, 4, 4] = [-3, -3, -3, 3, 3, -3, 3, 3] - - kp_params = get_fake_kp_params( - candidate_ranking_mode='score_distance_ratio') - - def graph_fn(): - object_heatmap = tf.constant(object_heatmap_np, dtype=tf.float32) - keypoint_heatmap = tf.constant(keypoint_heatmap_np, dtype=tf.float32) - keypoint_offset = tf.constant(keypoint_offset_np, dtype=tf.float32) - keypoint_regression = tf.constant( - keypoint_regression_np, dtype=tf.float32) - - (keypoint_cands, keypoint_scores, _) = ( - cnma.prediction_to_single_instance_keypoints( - object_heatmap, - keypoint_heatmap, - keypoint_offset, - keypoint_regression, - kp_params=kp_params)) - - return keypoint_cands, keypoint_scores - - (keypoint_cands, keypoint_scores) = self.execute(graph_fn, []) - - expected_keypoint_candidates = [[[ - [1.5, 1.5], # top-left - [1.5, 6.5], # top-right - [6.5, 1.5], # bottom-left - [6.5, 6.5], # bottom-right - ]]] - expected_keypoint_scores = [[[0.9, 0.9, 0.9, 0.9]]] - np.testing.assert_allclose(expected_keypoint_candidates, keypoint_cands) - np.testing.assert_allclose(expected_keypoint_scores, keypoint_scores) - - @parameterized.parameters({'provide_keypoint_score': True}, - {'provide_keypoint_score': False}) - def test_prediction_to_multi_instance_keypoints(self, provide_keypoint_score): - image_size = (9, 9) - keypoint_heatmap_np = np.zeros((image_size[0], image_size[1], 3, 4), - dtype=np.float32) - # Instance 0. - keypoint_heatmap_np[1, 1, 0, 0] = 0.9 - keypoint_heatmap_np[1, 7, 0, 1] = 0.9 - keypoint_heatmap_np[7, 1, 0, 2] = 0.9 - keypoint_heatmap_np[7, 7, 0, 3] = 0.9 - # Instance 1. - keypoint_heatmap_np[2, 2, 1, 0] = 0.8 - keypoint_heatmap_np[2, 8, 1, 1] = 0.8 - keypoint_heatmap_np[8, 2, 1, 2] = 0.8 - keypoint_heatmap_np[8, 8, 1, 3] = 0.8 - - keypoint_offset_np = np.zeros((image_size[0], image_size[1], 8), - dtype=np.float32) - keypoint_offset_np[1, 1] = [0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] - keypoint_offset_np[1, 7] = [0.0, 0.0, 0.5, -0.5, 0.0, 0.0, 0.0, 0.0] - keypoint_offset_np[7, 1] = [0.0, 0.0, 0.0, 0.0, -0.5, 0.5, 0.0, 0.0] - keypoint_offset_np[7, 7] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5, -0.5] - keypoint_offset_np[2, 2] = [0.3, 0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] - keypoint_offset_np[2, 8] = [0.0, 0.0, 0.3, -0.3, 0.0, 0.0, 0.0, 0.0] - keypoint_offset_np[8, 2] = [0.0, 0.0, 0.0, 0.0, -0.3, 0.3, 0.0, 0.0] - keypoint_offset_np[8, 8] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.3, -0.3] - - def graph_fn(): - keypoint_heatmap = tf.constant(keypoint_heatmap_np, dtype=tf.float32) - keypoint_offset = tf.constant(keypoint_offset_np, dtype=tf.float32) - - if provide_keypoint_score: - (keypoint_cands, keypoint_scores) = ( - cnma.prediction_tensors_to_multi_instance_kpts( - keypoint_heatmap, - keypoint_offset, - tf.reduce_max(keypoint_heatmap, axis=2))) - else: - (keypoint_cands, keypoint_scores) = ( - cnma.prediction_tensors_to_multi_instance_kpts( - keypoint_heatmap, - keypoint_offset)) - - return keypoint_cands, keypoint_scores - - (keypoint_cands, keypoint_scores) = self.execute(graph_fn, []) - - expected_keypoint_candidates_0 = [ - [1.5, 1.5], # top-left - [1.5, 6.5], # top-right - [6.5, 1.5], # bottom-left - [6.5, 6.5], # bottom-right - ] - expected_keypoint_scores_0 = [0.9, 0.9, 0.9, 0.9] - expected_keypoint_candidates_1 = [ - [2.3, 2.3], # top-left - [2.3, 7.7], # top-right - [7.7, 2.3], # bottom-left - [7.7, 7.7], # bottom-right - ] - expected_keypoint_scores_1 = [0.8, 0.8, 0.8, 0.8] - np.testing.assert_allclose( - expected_keypoint_candidates_0, keypoint_cands[0, 0, :, :]) - np.testing.assert_allclose( - expected_keypoint_candidates_1, keypoint_cands[0, 1, :, :]) - np.testing.assert_allclose( - expected_keypoint_scores_0, keypoint_scores[0, 0, :]) - np.testing.assert_allclose( - expected_keypoint_scores_1, keypoint_scores[0, 1, :]) - - def test_keypoint_candidate_prediction_per_keypoints(self): - keypoint_heatmap_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - keypoint_heatmap_np[0, 0, 0, 0] = 1.0 - keypoint_heatmap_np[0, 2, 1, 0] = 0.7 - keypoint_heatmap_np[0, 1, 1, 0] = 0.6 - keypoint_heatmap_np[0, 0, 2, 1] = 0.7 - keypoint_heatmap_np[0, 1, 1, 1] = 0.3 # Filtered by low score. - keypoint_heatmap_np[0, 2, 2, 1] = 0.2 - keypoint_heatmap_np[1, 1, 0, 0] = 0.6 - keypoint_heatmap_np[1, 2, 1, 0] = 0.5 - keypoint_heatmap_np[1, 0, 0, 0] = 0.4 - keypoint_heatmap_np[1, 0, 0, 1] = 1.0 - keypoint_heatmap_np[1, 0, 1, 1] = 0.9 - keypoint_heatmap_np[1, 2, 0, 1] = 0.8 - - # Note that the keypoint offsets are now per keypoint (as opposed to - # keypoint agnostic, in the test test_keypoint_candidate_prediction). - keypoint_heatmap_offsets_np = np.zeros((2, 3, 3, 4), dtype=np.float32) - keypoint_heatmap_offsets_np[0, 0, 0] = [0.5, 0.25, 0.0, 0.0] - keypoint_heatmap_offsets_np[0, 2, 1] = [-0.25, 0.5, 0.0, 0.0] - keypoint_heatmap_offsets_np[0, 1, 1] = [0.0, 0.0, 0.0, 0.0] - keypoint_heatmap_offsets_np[0, 0, 2] = [0.0, 0.0, 1.0, 0.0] - keypoint_heatmap_offsets_np[0, 2, 2] = [0.0, 0.0, 1.0, 1.0] - keypoint_heatmap_offsets_np[1, 1, 0] = [0.25, 0.5, 0.0, 0.0] - keypoint_heatmap_offsets_np[1, 2, 1] = [0.5, 0.0, 0.0, 0.0] - keypoint_heatmap_offsets_np[1, 0, 0] = [0.0, 0.0, 0.0, -0.5] - keypoint_heatmap_offsets_np[1, 0, 1] = [0.0, 0.0, 0.5, -0.5] - keypoint_heatmap_offsets_np[1, 2, 0] = [0.0, 0.0, -1.0, -0.5] - - def graph_fn(): - keypoint_heatmap = tf.constant(keypoint_heatmap_np, dtype=tf.float32) - keypoint_heatmap_offsets = tf.constant( - keypoint_heatmap_offsets_np, dtype=tf.float32) - - (keypoint_cands, keypoint_scores, num_keypoint_candidates, _) = ( - cnma.prediction_tensors_to_keypoint_candidates( - keypoint_heatmap, - keypoint_heatmap_offsets, - keypoint_score_threshold=0.5, - max_pool_kernel_size=1, - max_candidates=2)) - return keypoint_cands, keypoint_scores, num_keypoint_candidates - - (keypoint_cands, keypoint_scores, - num_keypoint_candidates) = self.execute(graph_fn, []) - - expected_keypoint_candidates = [ - [ # Example 0. - [[0.5, 0.25], [1.0, 2.0]], # Candidate 1 of keypoint 1, 2. - [[1.75, 1.5], [1.0, 1.0]], # Candidate 2 of keypoint 1, 2. - ], - [ # Example 1. - [[1.25, 0.5], [0.0, -0.5]], # Candidate 1 of keypoint 1, 2. - [[2.5, 1.0], [0.5, 0.5]], # Candidate 2 of keypoint 1, 2. - ], - ] - expected_keypoint_scores = [ - [ # Example 0. - [1.0, 0.7], # Candidate 1 scores of keypoint 1, 2. - [0.7, 0.3], # Candidate 2 scores of keypoint 1, 2. - ], - [ # Example 1. - [0.6, 1.0], # Candidate 1 scores of keypoint 1, 2. - [0.5, 0.9], # Candidate 2 scores of keypoint 1, 2. - ], - ] - expected_num_keypoint_candidates = [ - [2, 1], - [2, 2] - ] - np.testing.assert_allclose(expected_keypoint_candidates, keypoint_cands) - np.testing.assert_allclose(expected_keypoint_scores, keypoint_scores) - np.testing.assert_array_equal(expected_num_keypoint_candidates, - num_keypoint_candidates) - - @parameterized.parameters({'per_keypoint_depth': True}, - {'per_keypoint_depth': False}) - def test_keypoint_candidate_prediction_depth(self, per_keypoint_depth): - keypoint_heatmap_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - keypoint_heatmap_np[0, 0, 0, 0] = 1.0 - keypoint_heatmap_np[0, 2, 1, 0] = 0.7 - keypoint_heatmap_np[0, 1, 1, 0] = 0.6 - keypoint_heatmap_np[0, 0, 2, 1] = 0.7 - keypoint_heatmap_np[0, 1, 1, 1] = 0.3 # Filtered by low score. - keypoint_heatmap_np[0, 2, 2, 1] = 0.2 - keypoint_heatmap_np[1, 1, 0, 0] = 0.6 - keypoint_heatmap_np[1, 2, 1, 0] = 0.5 - keypoint_heatmap_np[1, 0, 0, 0] = 0.4 - keypoint_heatmap_np[1, 0, 0, 1] = 1.0 - keypoint_heatmap_np[1, 0, 1, 1] = 0.9 - keypoint_heatmap_np[1, 2, 0, 1] = 0.8 - - if per_keypoint_depth: - keypoint_depths_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - keypoint_depths_np[0, 0, 0, 0] = -1.5 - keypoint_depths_np[0, 2, 1, 0] = -1.0 - keypoint_depths_np[0, 0, 2, 1] = 1.5 - else: - keypoint_depths_np = np.zeros((2, 3, 3, 1), dtype=np.float32) - keypoint_depths_np[0, 0, 0, 0] = -1.5 - keypoint_depths_np[0, 2, 1, 0] = -1.0 - keypoint_depths_np[0, 0, 2, 0] = 1.5 - - keypoint_heatmap_offsets_np = np.zeros((2, 3, 3, 2), dtype=np.float32) - keypoint_heatmap_offsets_np[0, 0, 0] = [0.5, 0.25] - keypoint_heatmap_offsets_np[0, 2, 1] = [-0.25, 0.5] - keypoint_heatmap_offsets_np[0, 1, 1] = [0.0, 0.0] - keypoint_heatmap_offsets_np[0, 0, 2] = [1.0, 0.0] - keypoint_heatmap_offsets_np[0, 2, 2] = [1.0, 1.0] - keypoint_heatmap_offsets_np[1, 1, 0] = [0.25, 0.5] - keypoint_heatmap_offsets_np[1, 2, 1] = [0.5, 0.0] - keypoint_heatmap_offsets_np[1, 0, 0] = [0.0, -0.5] - keypoint_heatmap_offsets_np[1, 0, 1] = [0.5, -0.5] - keypoint_heatmap_offsets_np[1, 2, 0] = [-1.0, -0.5] - - def graph_fn(): - keypoint_heatmap = tf.constant(keypoint_heatmap_np, dtype=tf.float32) - keypoint_heatmap_offsets = tf.constant( - keypoint_heatmap_offsets_np, dtype=tf.float32) - - keypoint_depths = tf.constant(keypoint_depths_np, dtype=tf.float32) - (keypoint_cands, keypoint_scores, num_keypoint_candidates, - keypoint_depths) = ( - cnma.prediction_tensors_to_keypoint_candidates( - keypoint_heatmap, - keypoint_heatmap_offsets, - keypoint_score_threshold=0.5, - max_pool_kernel_size=1, - max_candidates=2, - keypoint_depths=keypoint_depths)) - return (keypoint_cands, keypoint_scores, num_keypoint_candidates, - keypoint_depths) - - (_, keypoint_scores, _, keypoint_depths) = self.execute(graph_fn, []) - - expected_keypoint_scores = [ - [ # Example 0. - [1.0, 0.7], # Keypoint 1. - [0.7, 0.3], # Keypoint 2. - ], - [ # Example 1. - [0.6, 1.0], # Keypoint 1. - [0.5, 0.9], # Keypoint 2. - ], - ] - expected_keypoint_depths = [ - [ - [-1.5, 1.5], - [-1.0, 0.0], - ], - [ - [0., 0.], - [0., 0.], - ], - ] - np.testing.assert_allclose(expected_keypoint_scores, keypoint_scores) - np.testing.assert_allclose(expected_keypoint_depths, keypoint_depths) - - def test_regressed_keypoints_at_object_centers(self): - batch_size = 2 - num_keypoints = 5 - num_instances = 6 - regressed_keypoint_feature_map_np = np.random.randn( - batch_size, 10, 10, 2 * num_keypoints).astype(np.float32) - y_indices = np.random.choice(10, (batch_size, num_instances)) - x_indices = np.random.choice(10, (batch_size, num_instances)) - offsets = np.stack([y_indices, x_indices], axis=2).astype(np.float32) - - def graph_fn(): - regressed_keypoint_feature_map = tf.constant( - regressed_keypoint_feature_map_np, dtype=tf.float32) - - gathered_regressed_keypoints = ( - cnma.regressed_keypoints_at_object_centers( - regressed_keypoint_feature_map, - tf.constant(y_indices, dtype=tf.int32), - tf.constant(x_indices, dtype=tf.int32))) - return gathered_regressed_keypoints - - gathered_regressed_keypoints = self.execute(graph_fn, []) - - expected_gathered_keypoints_0 = regressed_keypoint_feature_map_np[ - 0, y_indices[0], x_indices[0], :] - expected_gathered_keypoints_1 = regressed_keypoint_feature_map_np[ - 1, y_indices[1], x_indices[1], :] - expected_gathered_keypoints = np.stack([ - expected_gathered_keypoints_0, - expected_gathered_keypoints_1], axis=0) - expected_gathered_keypoints = np.reshape( - expected_gathered_keypoints, - [batch_size, num_instances, num_keypoints, 2]) - expected_gathered_keypoints += np.expand_dims(offsets, axis=2) - expected_gathered_keypoints = np.reshape( - expected_gathered_keypoints, - [batch_size, num_instances, -1]) - np.testing.assert_allclose(expected_gathered_keypoints, - gathered_regressed_keypoints) - - @parameterized.parameters( - {'candidate_ranking_mode': 'min_distance'}, - {'candidate_ranking_mode': 'score_distance_ratio'}, - ) - def test_refine_keypoints(self, candidate_ranking_mode): - regressed_keypoints_np = np.array( - [ - # Example 0. - [ - [[2.0, 2.0], [6.0, 10.0], [14.0, 7.0]], # Instance 0. - [[0.0, 6.0], [3.0, 3.0], [5.0, 7.0]], # Instance 1. - ], - # Example 1. - [ - [[6.0, 2.0], [0.0, 0.0], [0.1, 0.1]], # Instance 0. - [[6.0, 2.5], [5.0, 5.0], [9.0, 3.0]], # Instance 1. - ], - ], dtype=np.float32) - keypoint_candidates_np = np.array( - [ - # Example 0. - [ - [[2.0, 2.5], [6.0, 10.5], [4.0, 7.0]], # Candidate 0. - [[1.0, 8.0], [0.0, 0.0], [2.0, 2.0]], # Candidate 1. - [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], # Candidate 2. - ], - # Example 1. - [ - [[6.0, 1.5], [0.1, 0.4], [0.0, 0.0]], # Candidate 0. - [[1.0, 4.0], [0.0, 0.3], [0.0, 0.0]], # Candidate 1. - [[0.0, 0.0], [0.1, 0.3], [0.0, 0.0]], # Candidate 2. - ] - ], dtype=np.float32) - keypoint_scores_np = np.array( - [ - # Example 0. - [ - [0.8, 0.9, 1.0], # Candidate 0. - [0.6, 0.1, 0.9], # Candidate 1. - [0.0, 0.0, 0.0], # Candidate 1. - ], - # Example 1. - [ - [0.7, 0.3, 0.0], # Candidate 0. - [0.6, 0.1, 0.0], # Candidate 1. - [0.0, 0.28, 0.0], # Candidate 1. - ] - ], dtype=np.float32) - num_keypoints_candidates_np = np.array( - [ - # Example 0. - [2, 2, 2], - # Example 1. - [2, 3, 0], - ], dtype=np.int32) - unmatched_keypoint_score = 0.1 - - def graph_fn(): - regressed_keypoints = tf.constant( - regressed_keypoints_np, dtype=tf.float32) - keypoint_candidates = tf.constant( - keypoint_candidates_np, dtype=tf.float32) - keypoint_scores = tf.constant(keypoint_scores_np, dtype=tf.float32) - num_keypoint_candidates = tf.constant(num_keypoints_candidates_np, - dtype=tf.int32) - # The behavior of bboxes=None is different now. We provide the bboxes - # explicitly by using the regressed keypoints to create the same - # behavior. - regressed_keypoints_flattened = tf.reshape( - regressed_keypoints, [-1, 3, 2]) - bboxes_flattened = keypoint_ops.keypoints_to_enclosing_bounding_boxes( - regressed_keypoints_flattened) - (refined_keypoints, refined_scores, _) = cnma.refine_keypoints( - regressed_keypoints, - keypoint_candidates, - keypoint_scores, - num_keypoint_candidates, - bboxes=bboxes_flattened, - unmatched_keypoint_score=unmatched_keypoint_score, - box_scale=1.2, - candidate_search_scale=0.3, - candidate_ranking_mode=candidate_ranking_mode) - return refined_keypoints, refined_scores - - refined_keypoints, refined_scores = self.execute(graph_fn, []) - - if candidate_ranking_mode == 'min_distance': - expected_refined_keypoints = np.array( - [ - # Example 0. - [ - [[2.0, 2.5], [6.0, 10.5], [14.0, 7.0]], # Instance 0. - [[0.0, 6.0], [3.0, 3.0], [4.0, 7.0]], # Instance 1. - ], - # Example 1. - [ - [[6.0, 1.5], [0.0, 0.3], [0.1, 0.1]], # Instance 0. - [[6.0, 2.5], [5.0, 5.0], [9.0, 3.0]], # Instance 1. - ], - ], dtype=np.float32) - expected_refined_scores = np.array( - [ - # Example 0. - [ - [0.8, 0.9, unmatched_keypoint_score], # Instance 0. - [unmatched_keypoint_score, # Instance 1. - unmatched_keypoint_score, 1.0], - ], - # Example 1. - [ - [0.7, 0.1, unmatched_keypoint_score], # Instance 0. - [unmatched_keypoint_score, # Instance 1. - 0.1, unmatched_keypoint_score], - ], - ], dtype=np.float32) - else: - expected_refined_keypoints = np.array( - [ - # Example 0. - [ - [[2.0, 2.5], [6.0, 10.5], [14.0, 7.0]], # Instance 0. - [[0.0, 6.0], [3.0, 3.0], [4.0, 7.0]], # Instance 1. - ], - # Example 1. - [ - [[6.0, 1.5], [0.1, 0.3], [0.1, 0.1]], # Instance 0. - [[6.0, 2.5], [5.0, 5.0], [9.0, 3.0]], # Instance 1. - ], - ], dtype=np.float32) - expected_refined_scores = np.array( - [ - # Example 0. - [ - [0.8, 0.9, unmatched_keypoint_score], # Instance 0. - [unmatched_keypoint_score, # Instance 1. - unmatched_keypoint_score, 1.0], - ], - # Example 1. - [ - [0.7, 0.28, unmatched_keypoint_score], # Instance 0. - [unmatched_keypoint_score, # Instance 1. - 0.1, unmatched_keypoint_score], - ], - ], dtype=np.float32) - - np.testing.assert_allclose(expected_refined_keypoints, refined_keypoints) - np.testing.assert_allclose(expected_refined_scores, refined_scores) - - def test_refine_keypoints_with_empty_regressed_keypoints(self): - regressed_keypoints_np = np.zeros((1, 0, 2, 2), dtype=np.float32) - keypoint_candidates_np = np.ones((1, 1, 2, 2), dtype=np.float32) - keypoint_scores_np = np.ones((1, 1, 2), dtype=np.float32) - num_keypoints_candidates_np = np.ones((1, 1), dtype=np.int32) - unmatched_keypoint_score = 0.1 - - def graph_fn(): - regressed_keypoints = tf.constant( - regressed_keypoints_np, dtype=tf.float32) - keypoint_candidates = tf.constant( - keypoint_candidates_np, dtype=tf.float32) - keypoint_scores = tf.constant(keypoint_scores_np, dtype=tf.float32) - num_keypoint_candidates = tf.constant(num_keypoints_candidates_np, - dtype=tf.int32) - # The behavior of bboxes=None is different now. We provide the bboxes - # explicitly by using the regressed keypoints to create the same - # behavior. - regressed_keypoints_flattened = tf.reshape( - regressed_keypoints, [-1, 3, 2]) - bboxes_flattened = keypoint_ops.keypoints_to_enclosing_bounding_boxes( - regressed_keypoints_flattened) - (refined_keypoints, refined_scores, _) = cnma.refine_keypoints( - regressed_keypoints, - keypoint_candidates, - keypoint_scores, - num_keypoint_candidates, - bboxes=bboxes_flattened, - unmatched_keypoint_score=unmatched_keypoint_score, - box_scale=1.2, - candidate_search_scale=0.3, - candidate_ranking_mode='min_distance') - return refined_keypoints, refined_scores - - refined_keypoints, refined_scores = self.execute(graph_fn, []) - self.assertEqual(refined_keypoints.shape, (1, 0, 2, 2)) - self.assertEqual(refined_scores.shape, (1, 0, 2)) - - def test_refine_keypoints_without_bbox(self): - regressed_keypoints_np = np.array( - [ - # Example 0. - [ - [[2.0, 2.0], [6.0, 10.0], [14.0, 7.0]], # Instance 0. - [[0.0, 6.0], [3.0, 3.0], [5.0, 7.0]], # Instance 1. - ], - ], dtype=np.float32) - keypoint_candidates_np = np.array( - [ - # Example 0. - [ - [[2.0, 2.5], [6.0, 10.5], [4.0, 7.0]], # Candidate 0. - [[1.0, 8.0], [0.0, 0.0], [2.0, 2.0]], # Candidate 1. - [[0.0, 0.0], [0.0, 0.0], [0.0, 0.0]], # Candidate 2. - ], - ], dtype=np.float32) - keypoint_scores_np = np.array( - [ - # Example 0. - [ - [0.8, 0.9, 1.0], # Candidate 0. - [0.6, 0.1, 0.9], # Candidate 1. - [0.0, 0.0, 0.0], # Candidate 1. - ], - ], dtype=np.float32) - num_keypoints_candidates_np = np.array( - [ - # Example 0. - [2, 2, 2], - ], dtype=np.int32) - unmatched_keypoint_score = 0.1 - - def graph_fn(): - regressed_keypoints = tf.constant( - regressed_keypoints_np, dtype=tf.float32) - keypoint_candidates = tf.constant( - keypoint_candidates_np, dtype=tf.float32) - keypoint_scores = tf.constant(keypoint_scores_np, dtype=tf.float32) - num_keypoint_candidates = tf.constant(num_keypoints_candidates_np, - dtype=tf.int32) - (refined_keypoints, refined_scores, _) = cnma.refine_keypoints( - regressed_keypoints, - keypoint_candidates, - keypoint_scores, - num_keypoint_candidates, - bboxes=None, - unmatched_keypoint_score=unmatched_keypoint_score, - box_scale=1.2, - candidate_search_scale=0.3, - candidate_ranking_mode='min_distance') - return refined_keypoints, refined_scores - - refined_keypoints, refined_scores = self.execute(graph_fn, []) - - # The expected refined keypoints pick the ones that are closest to the - # regressed keypoint locations without filtering out the candidates which - # are outside of the bounding box. - expected_refined_keypoints = np.array( - [ - # Example 0. - [ - [[2.0, 2.5], [6.0, 10.5], [4.0, 7.0]], # Instance 0. - [[1.0, 8.0], [0.0, 0.0], [4.0, 7.0]], # Instance 1. - ], - ], dtype=np.float32) - expected_refined_scores = np.array( - [ - # Example 0. - [ - [0.8, 0.9, 1.0], # Instance 0. - [0.6, 0.1, 1.0], # Instance 1. - ], - ], dtype=np.float32) - - np.testing.assert_allclose(expected_refined_keypoints, refined_keypoints) - np.testing.assert_allclose(expected_refined_scores, refined_scores) - - @parameterized.parameters({'predict_depth': True}, {'predict_depth': False}) - def test_refine_keypoints_with_bboxes(self, predict_depth): - regressed_keypoints_np = np.array( - [ - # Example 0. - [ - [[2.0, 2.0], [6.0, 10.0], [14.0, 7.0]], # Instance 0. - [[0.0, 6.0], [3.0, 3.0], [5.0, 7.0]], # Instance 1. - ], - # Example 1. - [ - [[6.0, 2.0], [0.0, 0.0], [0.1, 0.1]], # Instance 0. - [[6.0, 2.5], [5.0, 5.0], [9.0, 3.0]], # Instance 1. - ], - ], dtype=np.float32) - keypoint_candidates_np = np.array( - [ - # Example 0. - [ - [[2.0, 2.5], [6.0, 10.5], [4.0, 7.0]], # Candidate 0. - [[1.0, 8.0], [0.0, 0.0], [2.0, 2.0]], # Candidate 1. - ], - # Example 1. - [ - [[6.0, 1.5], [5.0, 5.0], [0.0, 0.0]], # Candidate 0. - [[1.0, 4.0], [0.0, 0.3], [0.0, 0.0]], # Candidate 1. - ] - ], dtype=np.float32) - keypoint_scores_np = np.array( - [ - # Example 0. - [ - [0.8, 0.9, 1.0], # Candidate 0. - [0.6, 0.1, 0.9], # Candidate 1. - ], - # Example 1. - [ - [0.7, 0.4, 0.0], # Candidate 0. - [0.6, 0.1, 0.0], # Candidate 1. - ] - ], - dtype=np.float32) - keypoint_depths_np = np.array( - [ - # Example 0. - [ - [-0.8, -0.9, -1.0], # Candidate 0. - [-0.6, -0.1, -0.9], # Candidate 1. - ], - # Example 1. - [ - [-0.7, -0.4, -0.0], # Candidate 0. - [-0.6, -0.1, -0.0], # Candidate 1. - ] - ], - dtype=np.float32) - num_keypoints_candidates_np = np.array( - [ - # Example 0. - [2, 2, 2], - # Example 1. - [2, 2, 0], - ], dtype=np.int32) - bboxes_np = np.array( - [ - # Example 0. - [ - [2.0, 2.0, 14.0, 10.0], # Instance 0. - [0.0, 3.0, 5.0, 7.0], # Instance 1. - ], - # Example 1. - [ - [0.0, 0.0, 6.0, 2.0], # Instance 0. - [5.0, 1.4, 9.0, 5.0], # Instance 1. - ], - ], dtype=np.float32) - unmatched_keypoint_score = 0.1 - - def graph_fn(): - regressed_keypoints = tf.constant( - regressed_keypoints_np, dtype=tf.float32) - keypoint_candidates = tf.constant( - keypoint_candidates_np, dtype=tf.float32) - keypoint_scores = tf.constant(keypoint_scores_np, dtype=tf.float32) - if predict_depth: - keypoint_depths = tf.constant(keypoint_depths_np, dtype=tf.float32) - else: - keypoint_depths = None - num_keypoint_candidates = tf.constant(num_keypoints_candidates_np, - dtype=tf.int32) - bboxes = tf.constant(bboxes_np, dtype=tf.float32) - (refined_keypoints, refined_scores, - refined_depths) = cnma.refine_keypoints( - regressed_keypoints, - keypoint_candidates, - keypoint_scores, - num_keypoint_candidates, - bboxes=bboxes, - unmatched_keypoint_score=unmatched_keypoint_score, - box_scale=1.0, - candidate_search_scale=0.3, - keypoint_depth_candidates=keypoint_depths) - if predict_depth: - return refined_keypoints, refined_scores, refined_depths - else: - return refined_keypoints, refined_scores - - expected_refined_keypoints = np.array( - [ - # Example 0. - [ - [[2.0, 2.5], [6.0, 10.0], [14.0, 7.0]], # Instance 0. - [[0.0, 6.0], [3.0, 3.0], [4.0, 7.0]], # Instance 1. - ], - # Example 1. - [ - [[6.0, 1.5], [0.0, 0.3], [0.1, 0.1]], # Instance 0. - [[6.0, 1.5], [5.0, 5.0], [9.0, 3.0]], # Instance 1. - ], - ], dtype=np.float32) - expected_refined_scores = np.array( - [ - # Example 0. - [ - [0.8, unmatched_keypoint_score, # Instance 0. - unmatched_keypoint_score], - [unmatched_keypoint_score, # Instance 1. - unmatched_keypoint_score, 1.0], - ], - # Example 1. - [ - [0.7, 0.1, unmatched_keypoint_score], # Instance 0. - [0.7, 0.4, unmatched_keypoint_score], # Instance 1. - ], - ], dtype=np.float32) - - if predict_depth: - refined_keypoints, refined_scores, refined_depths = self.execute( - graph_fn, []) - expected_refined_depths = np.array([[[-0.8, 0.0, 0.0], [0.0, 0.0, -1.0]], - [[-0.7, -0.1, 0.0], [-0.7, -0.4, - 0.0]]]) - np.testing.assert_allclose(expected_refined_depths, refined_depths) - else: - refined_keypoints, refined_scores = self.execute(graph_fn, []) - np.testing.assert_allclose(expected_refined_keypoints, refined_keypoints) - np.testing.assert_allclose(expected_refined_scores, refined_scores) - - def test_sdr_scaled_ranking_score(self): - keypoint_scores_np = np.array( - [ - # Example 0. - [ - [0.9, 0.9, 0.9], # Candidate 0. - [0.9, 0.9, 0.9], # Candidate 1. - ] - ], - dtype=np.float32) - distances_np = np.expand_dims( - np.array( - [ - # Instance 0. - [ - [2.0, 1.0, 0.0], # Candidate 0. - [2.0, 1.0, 2.0], # Candidate 1. - ], - # Instance 1. - [ - [2.0, 1.0, 0.0], # Candidate 0. - [2.0, 1.0, 2.0], # Candidate 1. - ] - ], - dtype=np.float32), - axis=0) - bboxes_np = np.array( - [ - # Example 0. - [ - [2.0, 2.0, 20.0, 20.0], # Instance 0 large box. - [3.0, 3.0, 4.0, 4.0], # Instance 1 small box. - ], - ], - dtype=np.float32) - - # def graph_fn(): - keypoint_scores = tf.constant( - keypoint_scores_np, dtype=tf.float32) - distances = tf.constant( - distances_np, dtype=tf.float32) - bboxes = tf.constant(bboxes_np, dtype=tf.float32) - ranking_scores = cnma.sdr_scaled_ranking_score( - keypoint_scores=keypoint_scores, - distances=distances, - bboxes=bboxes, - score_distance_multiplier=0.1) - - self.assertAllEqual([1, 2, 2, 3], ranking_scores.shape) - # When the scores are the same, larger distance results in lower ranking - # score. - # instance 0, candidate 0, keypoint type 0 v.s 1 vs. 2 - self.assertGreater(ranking_scores[0, 0, 0, 2], ranking_scores[0, 0, 0, 1]) - self.assertGreater(ranking_scores[0, 0, 0, 1], ranking_scores[0, 0, 0, 0]) - - # When the scores are the same, the difference of distances are the same, - # instance with larger bbox has less ranking score difference, i.e. less - # sensitive to the distance change. - # instance 0 vs. 1, candidate 0, keypoint type 0 and 1 - self.assertGreater( - ranking_scores[0, 1, 1, 1] - ranking_scores[0, 1, 1, 0], - ranking_scores[0, 0, 1, 1] - ranking_scores[0, 0, 1, 0] - ) - - def test_gaussian_weighted_score(self): - keypoint_scores_np = np.array( - [ - # Example 0. - [ - [0.9, 0.9, 0.9], # Candidate 0. - [1.0, 0.8, 1.0], # Candidate 1. - ] - ], - dtype=np.float32) - distances_np = np.expand_dims( - np.array( - [ - # Instance 0. - [ - [2.0, 1.0, 0.0], # Candidate 0. - [1.0, 0.0, 2.0], # Candidate 1. - ], - # Instance 1. - [ - [2.0, 1.0, 0.0], # Candidate 0. - [1.0, 0.0, 2.0], # Candidate 1. - ] - ], - dtype=np.float32), - axis=0) - bboxes_np = np.array( - [ - # Example 0. - [ - [2.0, 2.0, 20.0, 20.0], # Instance 0 large box. - [3.0, 3.0, 4.0, 4.0], # Instance 1 small box. - ], - ], - dtype=np.float32) - - # def graph_fn(): - keypoint_scores = tf.constant( - keypoint_scores_np, dtype=tf.float32) - distances = tf.constant( - distances_np, dtype=tf.float32) - bboxes = tf.constant(bboxes_np, dtype=tf.float32) - ranking_scores = cnma.gaussian_weighted_score( - keypoint_scores=keypoint_scores, - distances=distances, - keypoint_std_dev=[1.0, 0.5, 1.5], - bboxes=bboxes) - - self.assertAllEqual([1, 2, 2, 3], ranking_scores.shape) - # When distance is zero, the candidate's score remains the same. - # instance 0, candidate 0, keypoint type 2 - self.assertAlmostEqual(ranking_scores[0, 0, 0, 2], keypoint_scores[0, 0, 2]) - # instance 0, candidate 1, keypoint type 1 - self.assertAlmostEqual(ranking_scores[0, 0, 1, 1], keypoint_scores[0, 1, 1]) - - # When the distances of two candidates are 1:2 and the keypoint standard - # deviation is 1:2 and the keypoint heatmap scores are the same, the - # resulting ranking score should be the same. - # instance 0, candidate 0, keypoint type 0, 1. - self.assertAlmostEqual( - ranking_scores[0, 0, 0, 0], ranking_scores[0, 0, 0, 1]) - - # When the distances/heatmap scores/keypoint standard deviations are the - # same, the instance with larger bbox size gets higher score. - self.assertGreater(ranking_scores[0, 0, 0, 0], ranking_scores[0, 1, 0, 0]) - - def test_pad_to_full_keypoint_dim(self): - batch_size = 4 - num_instances = 8 - num_keypoints = 2 - keypoint_inds = [1, 3] - num_total_keypoints = 5 - - kpt_coords_np = np.random.randn(batch_size, num_instances, num_keypoints, 2) - kpt_scores_np = np.random.randn(batch_size, num_instances, num_keypoints) - - def graph_fn(): - kpt_coords = tf.constant(kpt_coords_np) - kpt_scores = tf.constant(kpt_scores_np) - kpt_coords_padded, kpt_scores_padded = ( - cnma._pad_to_full_keypoint_dim( - kpt_coords, kpt_scores, keypoint_inds, num_total_keypoints)) - return kpt_coords_padded, kpt_scores_padded - - kpt_coords_padded, kpt_scores_padded = self.execute(graph_fn, []) - - self.assertAllEqual([batch_size, num_instances, num_total_keypoints, 2], - kpt_coords_padded.shape) - self.assertAllEqual([batch_size, num_instances, num_total_keypoints], - kpt_scores_padded.shape) - - for i, kpt_ind in enumerate(keypoint_inds): - np.testing.assert_allclose(kpt_coords_np[:, :, i, :], - kpt_coords_padded[:, :, kpt_ind, :]) - np.testing.assert_allclose(kpt_scores_np[:, :, i], - kpt_scores_padded[:, :, kpt_ind]) - - def test_pad_to_full_instance_dim(self): - batch_size = 4 - max_instances = 8 - num_keypoints = 6 - num_instances = 2 - instance_inds = [1, 3] - - kpt_coords_np = np.random.randn(batch_size, num_instances, num_keypoints, 2) - kpt_scores_np = np.random.randn(batch_size, num_instances, num_keypoints) - - def graph_fn(): - kpt_coords = tf.constant(kpt_coords_np) - kpt_scores = tf.constant(kpt_scores_np) - kpt_coords_padded, kpt_scores_padded = ( - cnma._pad_to_full_instance_dim( - kpt_coords, kpt_scores, instance_inds, max_instances)) - return kpt_coords_padded, kpt_scores_padded - - kpt_coords_padded, kpt_scores_padded = self.execute(graph_fn, []) - - self.assertAllEqual([batch_size, max_instances, num_keypoints, 2], - kpt_coords_padded.shape) - self.assertAllEqual([batch_size, max_instances, num_keypoints], - kpt_scores_padded.shape) - - for i, inst_ind in enumerate(instance_inds): - np.testing.assert_allclose(kpt_coords_np[:, i, :, :], - kpt_coords_padded[:, inst_ind, :, :]) - np.testing.assert_allclose(kpt_scores_np[:, i, :], - kpt_scores_padded[:, inst_ind, :]) - - def test_predicted_embeddings_at_object_centers(self): - batch_size = 2 - embedding_size = 5 - num_instances = 6 - predicted_embedding_feature_map_np = np.random.randn( - batch_size, 10, 10, embedding_size).astype(np.float32) - y_indices = np.random.choice(10, (batch_size, num_instances)) - x_indices = np.random.choice(10, (batch_size, num_instances)) - - def graph_fn(): - predicted_embedding_feature_map = tf.constant( - predicted_embedding_feature_map_np, dtype=tf.float32) - - gathered_predicted_embeddings = ( - cnma.predicted_embeddings_at_object_centers( - predicted_embedding_feature_map, - tf.constant(y_indices, dtype=tf.int32), - tf.constant(x_indices, dtype=tf.int32))) - return gathered_predicted_embeddings - - gathered_predicted_embeddings = self.execute(graph_fn, []) - - expected_gathered_embeddings_0 = predicted_embedding_feature_map_np[ - 0, y_indices[0], x_indices[0], :] - expected_gathered_embeddings_1 = predicted_embedding_feature_map_np[ - 1, y_indices[1], x_indices[1], :] - expected_gathered_embeddings = np.stack([ - expected_gathered_embeddings_0, - expected_gathered_embeddings_1], axis=0) - expected_gathered_embeddings = np.reshape( - expected_gathered_embeddings, - [batch_size, num_instances, embedding_size]) - np.testing.assert_allclose(expected_gathered_embeddings, - gathered_predicted_embeddings) - - -# Common parameters for setting up testing examples across tests. -_NUM_CLASSES = 10 -_KEYPOINT_INDICES = [0, 1, 2, 3] -_NUM_KEYPOINTS = len(_KEYPOINT_INDICES) -_DENSEPOSE_NUM_PARTS = 24 -_TASK_NAME = 'human_pose' -_NUM_TRACK_IDS = 3 -_REID_EMBED_SIZE = 2 -_NUM_FC_LAYERS = 1 - - -def get_fake_center_params(max_box_predictions=5): - """Returns the fake object center parameter namedtuple.""" - return cnma.ObjectCenterParams( - classification_loss=losses.WeightedSigmoidClassificationLoss(), - object_center_loss_weight=1.0, - min_box_overlap_iou=1.0, - max_box_predictions=max_box_predictions, - use_labeled_classes=False, - center_head_num_filters=[128], - center_head_kernel_sizes=[5]) - - -def get_fake_od_params(): - """Returns the fake object detection parameter namedtuple.""" - return cnma.ObjectDetectionParams( - localization_loss=losses.L1LocalizationLoss(), - offset_loss_weight=1.0, - scale_loss_weight=0.1) - - -def get_fake_kp_params(num_candidates_per_keypoint=100, - per_keypoint_offset=False, - predict_depth=False, - per_keypoint_depth=False, - peak_radius=0, - candidate_ranking_mode='min_distance', - argmax_postprocessing=False, - rescore_instances=False): - """Returns the fake keypoint estimation parameter namedtuple.""" - return cnma.KeypointEstimationParams( - task_name=_TASK_NAME, - class_id=1, - keypoint_indices=_KEYPOINT_INDICES, - keypoint_std_dev=[0.00001] * len(_KEYPOINT_INDICES), - classification_loss=losses.WeightedSigmoidClassificationLoss(), - localization_loss=losses.L1LocalizationLoss(), - unmatched_keypoint_score=0.1, - keypoint_candidate_score_threshold=0.1, - num_candidates_per_keypoint=num_candidates_per_keypoint, - per_keypoint_offset=per_keypoint_offset, - predict_depth=predict_depth, - per_keypoint_depth=per_keypoint_depth, - offset_peak_radius=peak_radius, - candidate_ranking_mode=candidate_ranking_mode, - argmax_postprocessing=argmax_postprocessing, - rescore_instances=rescore_instances, - rescoring_threshold=0.5) - - -def get_fake_mask_params(): - """Returns the fake mask estimation parameter namedtuple.""" - return cnma.MaskParams( - classification_loss=losses.WeightedSoftmaxClassificationLoss(), - task_loss_weight=1.0, - mask_height=4, - mask_width=4, - mask_head_num_filters=[96], - mask_head_kernel_sizes=[3]) - - -def get_fake_densepose_params(): - """Returns the fake DensePose estimation parameter namedtuple.""" - return cnma.DensePoseParams( - class_id=1, - classification_loss=losses.WeightedSoftmaxClassificationLoss(), - localization_loss=losses.L1LocalizationLoss(), - part_loss_weight=1.0, - coordinate_loss_weight=1.0, - num_parts=_DENSEPOSE_NUM_PARTS, - task_loss_weight=1.0, - upsample_to_input_res=True, - upsample_method='nearest') - - -def get_fake_track_params(): - """Returns the fake object tracking parameter namedtuple.""" - return cnma.TrackParams( - num_track_ids=_NUM_TRACK_IDS, - reid_embed_size=_REID_EMBED_SIZE, - num_fc_layers=_NUM_FC_LAYERS, - classification_loss=losses.WeightedSoftmaxClassificationLoss(), - task_loss_weight=1.0) - - -def get_fake_temporal_offset_params(): - """Returns the fake temporal offset parameter namedtuple.""" - return cnma.TemporalOffsetParams( - localization_loss=losses.WeightedSmoothL1LocalizationLoss(), - task_loss_weight=1.0) - - -def build_center_net_meta_arch(build_resnet=False, - num_classes=_NUM_CLASSES, - max_box_predictions=5, - apply_non_max_suppression=False, - detection_only=False, - per_keypoint_offset=False, - predict_depth=False, - per_keypoint_depth=False, - peak_radius=0, - keypoint_only=False, - candidate_ranking_mode='min_distance', - argmax_postprocessing=False, - rescore_instances=False): - """Builds the CenterNet meta architecture.""" - if build_resnet: - feature_extractor = ( - center_net_resnet_feature_extractor.CenterNetResnetFeatureExtractor( - 'resnet_v2_101')) - else: - feature_extractor = DummyFeatureExtractor( - channel_means=(1.0, 2.0, 3.0), - channel_stds=(10., 20., 30.), - bgr_ordering=False, - num_feature_outputs=2, - stride=4) - image_resizer_fn = functools.partial( - preprocessor.resize_to_range, - min_dimension=128, - max_dimension=128, - pad_to_max_dimesnion=True) - - non_max_suppression_fn = None - if apply_non_max_suppression: - post_processing_proto = post_processing_pb2.PostProcessing() - post_processing_proto.batch_non_max_suppression.iou_threshold = 0.6 - post_processing_proto.batch_non_max_suppression.score_threshold = 0.6 - (post_processing_proto.batch_non_max_suppression.max_total_detections - ) = max_box_predictions - (post_processing_proto.batch_non_max_suppression.max_detections_per_class - ) = max_box_predictions - (post_processing_proto.batch_non_max_suppression.change_coordinate_frame - ) = False - non_max_suppression_fn, _ = post_processing_builder.build( - post_processing_proto) - - if keypoint_only: - num_candidates_per_keypoint = 100 if max_box_predictions > 1 else 1 - return cnma.CenterNetMetaArch( - is_training=True, - add_summaries=False, - num_classes=num_classes, - feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=get_fake_center_params(max_box_predictions), - keypoint_params_dict={ - _TASK_NAME: - get_fake_kp_params(num_candidates_per_keypoint, - per_keypoint_offset, predict_depth, - per_keypoint_depth, peak_radius, - candidate_ranking_mode, - argmax_postprocessing, rescore_instances) - }, - non_max_suppression_fn=non_max_suppression_fn) - elif detection_only: - return cnma.CenterNetMetaArch( - is_training=True, - add_summaries=False, - num_classes=num_classes, - feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=get_fake_center_params(max_box_predictions), - object_detection_params=get_fake_od_params(), - non_max_suppression_fn=non_max_suppression_fn) - elif num_classes == 1: - num_candidates_per_keypoint = 100 if max_box_predictions > 1 else 1 - return cnma.CenterNetMetaArch( - is_training=True, - add_summaries=False, - num_classes=num_classes, - feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=get_fake_center_params(max_box_predictions), - object_detection_params=get_fake_od_params(), - keypoint_params_dict={ - _TASK_NAME: - get_fake_kp_params(num_candidates_per_keypoint, - per_keypoint_offset, predict_depth, - per_keypoint_depth, peak_radius, - candidate_ranking_mode, - argmax_postprocessing, rescore_instances) - }, - non_max_suppression_fn=non_max_suppression_fn) - else: - return cnma.CenterNetMetaArch( - is_training=True, - add_summaries=False, - num_classes=num_classes, - feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=get_fake_center_params(), - object_detection_params=get_fake_od_params(), - keypoint_params_dict={_TASK_NAME: get_fake_kp_params( - candidate_ranking_mode=candidate_ranking_mode)}, - mask_params=get_fake_mask_params(), - densepose_params=get_fake_densepose_params(), - track_params=get_fake_track_params(), - temporal_offset_params=get_fake_temporal_offset_params(), - non_max_suppression_fn=non_max_suppression_fn) - - -def _logit(p): - return np.log( - (p + np.finfo(np.float32).eps) / (1 - p + np.finfo(np.float32).eps)) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMetaArchLibTest(test_case.TestCase): - """Test for CenterNet meta architecture related functions.""" - - def test_get_keypoint_name(self): - self.assertEqual('human_pose/keypoint_offset', - cnma.get_keypoint_name('human_pose', 'keypoint_offset')) - - def test_get_num_instances_from_weights(self): - weight1 = tf.constant([0.0, 0.0, 0.0], dtype=tf.float32) - weight2 = tf.constant([0.5, 0.9, 0.0], dtype=tf.float32) - weight3 = tf.constant([0.0, 0.0, 1.0], dtype=tf.float32) - - def graph_fn_1(): - # Total of three elements with non-zero values. - num_instances = cnma.get_num_instances_from_weights( - [weight1, weight2, weight3]) - return num_instances - num_instances = self.execute(graph_fn_1, []) - self.assertAlmostEqual(3, num_instances) - - # No non-zero value in the weights. Return minimum value: 1. - def graph_fn_2(): - # Total of three elements with non-zero values. - num_instances = cnma.get_num_instances_from_weights([weight1, weight1]) - return num_instances - num_instances = self.execute(graph_fn_2, []) - self.assertAlmostEqual(1, num_instances) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMetaArchTest(test_case.TestCase, parameterized.TestCase): - """Tests for the CenterNet meta architecture.""" - - def test_construct_prediction_heads(self): - model = build_center_net_meta_arch() - fake_feature_map = np.zeros((4, 128, 128, 8)) - - # Check the dictionary contains expected keys and corresponding heads with - # correct dimensions. - # "object center" head: - output = model._prediction_head_dict[cnma.OBJECT_CENTER][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, _NUM_CLASSES), output.shape) - - # "object scale" (height/width) head: - output = model._prediction_head_dict[cnma.BOX_SCALE][-1](fake_feature_map) - self.assertEqual((4, 128, 128, 2), output.shape) - - # "object offset" head: - output = model._prediction_head_dict[cnma.BOX_OFFSET][-1](fake_feature_map) - self.assertEqual((4, 128, 128, 2), output.shape) - - # "keypoint offset" head: - output = model._prediction_head_dict[ - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET)][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, 2), output.shape) - - # "keypoint heatmap" head: - output = model._prediction_head_dict[cnma.get_keypoint_name( - _TASK_NAME, cnma.KEYPOINT_HEATMAP)][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, _NUM_KEYPOINTS), output.shape) - - # "keypoint regression" head: - output = model._prediction_head_dict[cnma.get_keypoint_name( - _TASK_NAME, cnma.KEYPOINT_REGRESSION)][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, 2 * _NUM_KEYPOINTS), output.shape) - - # "mask" head: - output = model._prediction_head_dict[cnma.SEGMENTATION_HEATMAP][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, _NUM_CLASSES), output.shape) - - # "densepose parts" head: - output = model._prediction_head_dict[cnma.DENSEPOSE_HEATMAP][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, _DENSEPOSE_NUM_PARTS), output.shape) - - # "densepose surface coordinates" head: - output = model._prediction_head_dict[cnma.DENSEPOSE_REGRESSION][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, 2 * _DENSEPOSE_NUM_PARTS), output.shape) - - # "track embedding" head: - output = model._prediction_head_dict[cnma.TRACK_REID][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, _REID_EMBED_SIZE), output.shape) - - # "temporal offset" head: - output = model._prediction_head_dict[cnma.TEMPORAL_OFFSET][-1]( - fake_feature_map) - self.assertEqual((4, 128, 128, 2), output.shape) - - def test_initialize_target_assigners(self): - model = build_center_net_meta_arch() - assigner_dict = model._initialize_target_assigners( - stride=2, - min_box_overlap_iou=0.7) - - # Check whether the correponding target assigner class is initialized. - # object center target assigner: - self.assertIsInstance(assigner_dict[cnma.OBJECT_CENTER], - cn_assigner.CenterNetCenterHeatmapTargetAssigner) - - # object detection target assigner: - self.assertIsInstance(assigner_dict[cnma.DETECTION_TASK], - cn_assigner.CenterNetBoxTargetAssigner) - - # keypoint estimation target assigner: - self.assertIsInstance(assigner_dict[_TASK_NAME], - cn_assigner.CenterNetKeypointTargetAssigner) - - # mask estimation target assigner: - self.assertIsInstance(assigner_dict[cnma.SEGMENTATION_TASK], - cn_assigner.CenterNetMaskTargetAssigner) - - # DensePose estimation target assigner: - self.assertIsInstance(assigner_dict[cnma.DENSEPOSE_TASK], - cn_assigner.CenterNetDensePoseTargetAssigner) - - # Track estimation target assigner: - self.assertIsInstance(assigner_dict[cnma.TRACK_TASK], - cn_assigner.CenterNetTrackTargetAssigner) - - # Temporal Offset target assigner: - self.assertIsInstance(assigner_dict[cnma.TEMPORALOFFSET_TASK], - cn_assigner.CenterNetTemporalOffsetTargetAssigner) - - def test_predict(self): - """Test the predict function.""" - - model = build_center_net_meta_arch() - def graph_fn(): - prediction_dict = model.predict(tf.zeros([2, 128, 128, 3]), None) - return prediction_dict - - prediction_dict = self.execute(graph_fn, []) - - self.assertEqual(prediction_dict['preprocessed_inputs'].shape, - (2, 128, 128, 3)) - self.assertEqual(prediction_dict[cnma.OBJECT_CENTER][0].shape, - (2, 32, 32, _NUM_CLASSES)) - self.assertEqual(prediction_dict[cnma.BOX_SCALE][0].shape, - (2, 32, 32, 2)) - self.assertEqual(prediction_dict[cnma.BOX_OFFSET][0].shape, - (2, 32, 32, 2)) - self.assertEqual(prediction_dict[cnma.SEGMENTATION_HEATMAP][0].shape, - (2, 32, 32, _NUM_CLASSES)) - self.assertEqual(prediction_dict[cnma.DENSEPOSE_HEATMAP][0].shape, - (2, 32, 32, _DENSEPOSE_NUM_PARTS)) - self.assertEqual(prediction_dict[cnma.DENSEPOSE_REGRESSION][0].shape, - (2, 32, 32, 2 * _DENSEPOSE_NUM_PARTS)) - self.assertEqual(prediction_dict[cnma.TRACK_REID][0].shape, - (2, 32, 32, _REID_EMBED_SIZE)) - self.assertEqual(prediction_dict[cnma.TEMPORAL_OFFSET][0].shape, - (2, 32, 32, 2)) - - def test_loss(self): - """Test the loss function.""" - groundtruth_dict = get_fake_groundtruth_dict(16, 32, 4) - model = build_center_net_meta_arch() - model.provide_groundtruth( - groundtruth_boxes_list=groundtruth_dict[fields.BoxListFields.boxes], - groundtruth_weights_list=groundtruth_dict[fields.BoxListFields.weights], - groundtruth_classes_list=groundtruth_dict[fields.BoxListFields.classes], - groundtruth_keypoints_list=groundtruth_dict[ - fields.BoxListFields.keypoints], - groundtruth_masks_list=groundtruth_dict[ - fields.BoxListFields.masks], - groundtruth_dp_num_points_list=groundtruth_dict[ - fields.BoxListFields.densepose_num_points], - groundtruth_dp_part_ids_list=groundtruth_dict[ - fields.BoxListFields.densepose_part_ids], - groundtruth_dp_surface_coords_list=groundtruth_dict[ - fields.BoxListFields.densepose_surface_coords], - groundtruth_track_ids_list=groundtruth_dict[ - fields.BoxListFields.track_ids], - groundtruth_track_match_flags_list=groundtruth_dict[ - fields.BoxListFields.track_match_flags], - groundtruth_temporal_offsets_list=groundtruth_dict[ - fields.BoxListFields.temporal_offsets]) - - kernel_initializer = tf.constant_initializer( - [[1, 1, 0], [-1000000, -1000000, 1000000]]) - model.track_reid_classification_net = tf.keras.layers.Dense( - _NUM_TRACK_IDS, - kernel_initializer=kernel_initializer, - input_shape=(_REID_EMBED_SIZE,)) - - prediction_dict = get_fake_prediction_dict( - input_height=16, input_width=32, stride=4) - - def graph_fn(): - loss_dict = model.loss(prediction_dict, - tf.constant([[16, 24, 3], [16, 24, 3]])) - return loss_dict - - loss_dict = self.execute(graph_fn, []) - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, cnma.OBJECT_CENTER)]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, cnma.BOX_SCALE)]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, cnma.BOX_OFFSET)]) - self.assertGreater( - 0.01, - loss_dict['%s/%s' % - (cnma.LOSS_KEY_PREFIX, - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP))]) - self.assertGreater( - 0.01, - loss_dict['%s/%s' % - (cnma.LOSS_KEY_PREFIX, - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET))]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, - cnma.get_keypoint_name( - _TASK_NAME, cnma.KEYPOINT_REGRESSION))]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, - cnma.SEGMENTATION_HEATMAP)]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, - cnma.DENSEPOSE_HEATMAP)]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, - cnma.DENSEPOSE_REGRESSION)]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, - cnma.TRACK_REID)]) - self.assertGreater( - 0.01, loss_dict['%s/%s' % (cnma.LOSS_KEY_PREFIX, - cnma.TEMPORAL_OFFSET)]) - - @parameterized.parameters( - {'target_class_id': 1, 'with_true_image_shape': True}, - {'target_class_id': 2, 'with_true_image_shape': True}, - {'target_class_id': 1, 'with_true_image_shape': False}, - ) - def test_postprocess(self, target_class_id, with_true_image_shape): - """Test the postprocess function.""" - model = build_center_net_meta_arch() - max_detection = model._center_params.max_box_predictions - num_keypoints = len(model._kp_params_dict[_TASK_NAME].keypoint_indices) - unmatched_keypoint_score = ( - model._kp_params_dict[_TASK_NAME].unmatched_keypoint_score) - - class_center = np.zeros((1, 32, 32, 10), dtype=np.float32) - height_width = np.zeros((1, 32, 32, 2), dtype=np.float32) - offset = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_heatmaps = np.ones( - (1, 32, 32, num_keypoints), dtype=np.float32) * _logit(0.001) - keypoint_offsets = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_regression = np.random.randn(1, 32, 32, num_keypoints * 2) - - class_probs = np.ones(10) * _logit(0.25) - class_probs[target_class_id] = _logit(0.75) - class_center[0, 16, 16] = class_probs - height_width[0, 16, 16] = [5, 10] - offset[0, 16, 16] = [.25, .5] - keypoint_regression[0, 16, 16] = [ - -1., -1., - -1., 1., - 1., -1., - 1., 1.] - keypoint_heatmaps[0, 14, 14, 0] = _logit(0.9) - keypoint_heatmaps[0, 14, 18, 1] = _logit(0.9) - keypoint_heatmaps[0, 18, 14, 2] = _logit(0.9) - keypoint_heatmaps[0, 18, 18, 3] = _logit(0.05) # Note the low score. - - segmentation_heatmap = np.zeros((1, 32, 32, 10), dtype=np.float32) - segmentation_heatmap[:, 14:18, 14:18, target_class_id] = 1.0 - segmentation_heatmap = _logit(segmentation_heatmap) - - dp_part_ind = 4 - dp_part_heatmap = np.zeros((1, 32, 32, _DENSEPOSE_NUM_PARTS), - dtype=np.float32) - dp_part_heatmap[0, 14:18, 14:18, dp_part_ind] = 1.0 - dp_part_heatmap = _logit(dp_part_heatmap) - - dp_surf_coords = np.random.randn(1, 32, 32, 2 * _DENSEPOSE_NUM_PARTS) - - embedding_size = 100 - track_reid_embedding = np.zeros((1, 32, 32, embedding_size), - dtype=np.float32) - track_reid_embedding[0, 16, 16, :] = np.ones(embedding_size) - - temporal_offsets = np.zeros((1, 32, 32, 2), dtype=np.float32) - temporal_offsets[..., 1] = 1 - - class_center = tf.constant(class_center) - height_width = tf.constant(height_width) - offset = tf.constant(offset) - keypoint_heatmaps = tf.constant(keypoint_heatmaps, dtype=tf.float32) - keypoint_offsets = tf.constant(keypoint_offsets, dtype=tf.float32) - keypoint_regression = tf.constant(keypoint_regression, dtype=tf.float32) - segmentation_heatmap = tf.constant(segmentation_heatmap, dtype=tf.float32) - dp_part_heatmap = tf.constant(dp_part_heatmap, dtype=tf.float32) - dp_surf_coords = tf.constant(dp_surf_coords, dtype=tf.float32) - track_reid_embedding = tf.constant(track_reid_embedding, dtype=tf.float32) - temporal_offsets = tf.constant(temporal_offsets, dtype=tf.float32) - - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.BOX_SCALE: [height_width], - cnma.BOX_OFFSET: [offset], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP): - [keypoint_heatmaps], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET): - [keypoint_offsets], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_REGRESSION): - [keypoint_regression], - cnma.SEGMENTATION_HEATMAP: [segmentation_heatmap], - cnma.DENSEPOSE_HEATMAP: [dp_part_heatmap], - cnma.DENSEPOSE_REGRESSION: [dp_surf_coords], - cnma.TRACK_REID: [track_reid_embedding], - cnma.TEMPORAL_OFFSET: [temporal_offsets], - } - - def graph_fn(): - if with_true_image_shape: - detections = model.postprocess(prediction_dict, - tf.constant([[128, 128, 3]])) - else: - detections = model.postprocess(prediction_dict, None) - return detections - - detections = self.execute_cpu(graph_fn, []) - self.assertAllClose(detections['detection_boxes'][0, 0], - np.array([55, 46, 75, 86]) / 128.0) - self.assertAllClose(detections['detection_scores'][0], - [.75, .5, .5, .5, .5]) - expected_multiclass_scores = [.25] * 10 - expected_multiclass_scores[target_class_id] = .75 - self.assertAllClose(expected_multiclass_scores, - detections['detection_multiclass_scores'][0][0]) - - # The output embedding extracted at the object center will be a 3-D array of - # shape [batch, num_boxes, embedding_size]. The valid predicted embedding - # will be the first embedding in the first batch. It is a 1-D array of - # shape [embedding_size] with values all ones. All the values of the - # embedding will then be divided by the square root of 'embedding_size' - # after the L2 normalization. - self.assertAllClose(detections['detection_embeddings'][0, 0], - np.ones(embedding_size) / embedding_size**0.5) - self.assertEqual(detections['detection_classes'][0, 0], target_class_id) - self.assertEqual(detections['num_detections'], [5]) - self.assertAllEqual([1, max_detection, num_keypoints, 2], - detections['detection_keypoints'].shape) - self.assertAllEqual([1, max_detection, num_keypoints], - detections['detection_keypoint_scores'].shape) - self.assertAllEqual([1, max_detection, 4, 4], - detections['detection_masks'].shape) - self.assertAllEqual([1, max_detection, embedding_size], - detections['detection_embeddings'].shape) - self.assertAllEqual([1, max_detection, 2], - detections['detection_temporal_offsets'].shape) - - # Masks should be empty for everything but the first detection. - self.assertAllEqual( - detections['detection_masks'][0, 1:, :, :], - np.zeros_like(detections['detection_masks'][0, 1:, :, :])) - self.assertAllEqual( - detections['detection_surface_coords'][0, 1:, :, :], - np.zeros_like(detections['detection_surface_coords'][0, 1:, :, :])) - - if target_class_id == 1: - expected_kpts_for_obj_0 = np.array( - [[14., 14.], [14., 18.], [18., 14.], [17., 17.]]) / 32. - expected_kpt_scores_for_obj_0 = np.array( - [0.9, 0.9, 0.9, unmatched_keypoint_score]) - np.testing.assert_allclose(detections['detection_keypoints'][0][0], - expected_kpts_for_obj_0, rtol=1e-6) - np.testing.assert_allclose(detections['detection_keypoint_scores'][0][0], - expected_kpt_scores_for_obj_0, rtol=1e-6) - # First detection has DensePose parts. - self.assertSameElements( - np.unique(detections['detection_masks'][0, 0, :, :]), - set([0, dp_part_ind + 1])) - self.assertGreater(np.sum(np.abs(detections['detection_surface_coords'])), - 0.0) - else: - # All keypoint outputs should be zeros. - np.testing.assert_allclose( - detections['detection_keypoints'][0][0], - np.zeros([num_keypoints, 2], float), - rtol=1e-6) - np.testing.assert_allclose( - detections['detection_keypoint_scores'][0][0], - np.zeros([num_keypoints], float), - rtol=1e-6) - # Binary segmentation mask. - self.assertSameElements( - np.unique(detections['detection_masks'][0, 0, :, :]), - set([0, 1])) - # No DensePose surface coordinates. - np.testing.assert_allclose( - detections['detection_surface_coords'][0, 0, :, :], - np.zeros_like(detections['detection_surface_coords'][0, 0, :, :])) - - def test_postprocess_kpts_no_od(self): - """Test the postprocess function.""" - target_class_id = 1 - model = build_center_net_meta_arch(keypoint_only=True) - max_detection = model._center_params.max_box_predictions - num_keypoints = len(model._kp_params_dict[_TASK_NAME].keypoint_indices) - - class_center = np.zeros((1, 32, 32, 10), dtype=np.float32) - keypoint_heatmaps = np.zeros((1, 32, 32, num_keypoints), dtype=np.float32) - keypoint_offsets = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_regression = np.random.randn(1, 32, 32, num_keypoints * 2) - - class_probs = np.ones(10) * _logit(0.25) - class_probs[target_class_id] = _logit(0.75) - class_center[0, 16, 16] = class_probs - keypoint_regression[0, 16, 16] = [ - -1., -1., - -1., 1., - 1., -1., - 1., 1.] - keypoint_heatmaps[0, 14, 14, 0] = _logit(0.9) - keypoint_heatmaps[0, 14, 18, 1] = _logit(0.9) - keypoint_heatmaps[0, 18, 14, 2] = _logit(0.9) - keypoint_heatmaps[0, 18, 18, 3] = _logit(0.05) # Note the low score. - - class_center = tf.constant(class_center) - keypoint_heatmaps = tf.constant(keypoint_heatmaps, dtype=tf.float32) - keypoint_offsets = tf.constant(keypoint_offsets, dtype=tf.float32) - keypoint_regression = tf.constant(keypoint_regression, dtype=tf.float32) - - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP): - [keypoint_heatmaps], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET): - [keypoint_offsets], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_REGRESSION): - [keypoint_regression], - } - - # def graph_fn(): - detections = model.postprocess(prediction_dict, - tf.constant([[128, 128, 3]])) - # return detections - - # detections = self.execute_cpu(graph_fn, []) - self.assertAllClose(detections['detection_scores'][0], - [.75, .5, .5, .5, .5]) - expected_multiclass_scores = [.25] * 10 - expected_multiclass_scores[target_class_id] = .75 - self.assertAllClose(expected_multiclass_scores, - detections['detection_multiclass_scores'][0][0]) - - self.assertEqual(detections['detection_classes'][0, 0], target_class_id) - self.assertEqual(detections['num_detections'], [5]) - self.assertAllEqual([1, max_detection, num_keypoints, 2], - detections['detection_keypoints'].shape) - self.assertAllEqual([1, max_detection, num_keypoints], - detections['detection_keypoint_scores'].shape) - - def test_non_max_suppression(self): - """Tests application of NMS on CenterNet detections.""" - target_class_id = 1 - model = build_center_net_meta_arch(apply_non_max_suppression=True, - detection_only=True) - - class_center = np.zeros((1, 32, 32, 10), dtype=np.float32) - height_width = np.zeros((1, 32, 32, 2), dtype=np.float32) - offset = np.zeros((1, 32, 32, 2), dtype=np.float32) - - class_probs = np.ones(10) * _logit(0.25) - class_probs[target_class_id] = _logit(0.75) - class_center[0, 16, 16] = class_probs - height_width[0, 16, 16] = [5, 10] - offset[0, 16, 16] = [.25, .5] - - class_center = tf.constant(class_center) - height_width = tf.constant(height_width) - offset = tf.constant(offset) - - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.BOX_SCALE: [height_width], - cnma.BOX_OFFSET: [offset], - } - - def graph_fn(): - detections = model.postprocess(prediction_dict, - tf.constant([[128, 128, 3]])) - return detections - - detections = self.execute_cpu(graph_fn, []) - num_detections = int(detections['num_detections']) - self.assertEqual(num_detections, 1) - self.assertAllClose(detections['detection_boxes'][0, 0], - np.array([55, 46, 75, 86]) / 128.0) - self.assertAllClose(detections['detection_scores'][0][:num_detections], - [.75]) - expected_multiclass_scores = [.25] * 10 - expected_multiclass_scores[target_class_id] = .75 - self.assertAllClose(expected_multiclass_scores, - detections['detection_multiclass_scores'][0][0]) - - def test_non_max_suppression_with_kpts_rescoring(self): - """Tests application of NMS on CenterNet detections and keypoints.""" - model = build_center_net_meta_arch( - num_classes=1, max_box_predictions=5, per_keypoint_offset=True, - candidate_ranking_mode='min_distance', - argmax_postprocessing=False, apply_non_max_suppression=True, - rescore_instances=True) - num_keypoints = len(model._kp_params_dict[_TASK_NAME].keypoint_indices) - - class_center = np.zeros((1, 32, 32, 2), dtype=np.float32) - height_width = np.zeros((1, 32, 32, 2), dtype=np.float32) - offset = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_heatmaps = np.ones( - (1, 32, 32, num_keypoints), dtype=np.float32) * _logit(0.01) - keypoint_offsets = np.zeros( - (1, 32, 32, num_keypoints * 2), dtype=np.float32) - keypoint_regression = np.random.randn(1, 32, 32, num_keypoints * 2) - - class_probs = np.zeros(2) - class_probs[1] = _logit(0.75) - class_center[0, 16, 16] = class_probs - height_width[0, 16, 16] = [5, 10] - offset[0, 16, 16] = [.25, .5] - class_center[0, 16, 17] = class_probs - height_width[0, 16, 17] = [5, 10] - offset[0, 16, 17] = [.25, .5] - keypoint_regression[0, 16, 16] = [ - -1., -1., - -1., 1., - 1., -1., - 1., 1.] - keypoint_heatmaps[0, 14, 14, 0] = _logit(0.9) - keypoint_heatmaps[0, 14, 18, 1] = _logit(0.9) - keypoint_heatmaps[0, 18, 14, 2] = _logit(0.9) - keypoint_heatmaps[0, 18, 18, 3] = _logit(0.05) # Note the low score. - - class_center = tf.constant(class_center) - height_width = tf.constant(height_width) - offset = tf.constant(offset) - keypoint_heatmaps = tf.constant(keypoint_heatmaps, dtype=tf.float32) - keypoint_offsets = tf.constant(keypoint_offsets, dtype=tf.float32) - keypoint_regression = tf.constant(keypoint_regression, dtype=tf.float32) - - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.BOX_SCALE: [height_width], - cnma.BOX_OFFSET: [offset], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP): - [keypoint_heatmaps], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET): - [keypoint_offsets], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_REGRESSION): - [keypoint_regression], - } - - def graph_fn(): - detections = model.postprocess(prediction_dict, - tf.constant([[128, 128, 3]])) - return detections - - detections = self.execute_cpu(graph_fn, []) - num_detections = int(detections['num_detections']) - # One of the box is filtered by NMS. - self.assertEqual(num_detections, 1) - # The keypoint scores are [0.9, 0.9, 0.9, 0.1] and the resulting rescored - # score is 0.9 * 3 / 4 = 0.675. - self.assertAllClose(detections['detection_scores'][0][:num_detections], - [0.675]) - - @parameterized.parameters( - { - 'candidate_ranking_mode': 'min_distance', - 'argmax_postprocessing': False - }, - { - 'candidate_ranking_mode': 'gaussian_weighted_const', - 'argmax_postprocessing': True - }) - def test_postprocess_single_class(self, candidate_ranking_mode, - argmax_postprocessing): - """Test the postprocess function.""" - model = build_center_net_meta_arch( - num_classes=1, max_box_predictions=5, per_keypoint_offset=True, - candidate_ranking_mode=candidate_ranking_mode, - argmax_postprocessing=argmax_postprocessing) - max_detection = model._center_params.max_box_predictions - num_keypoints = len(model._kp_params_dict[_TASK_NAME].keypoint_indices) - - class_center = np.zeros((1, 32, 32, 1), dtype=np.float32) - height_width = np.zeros((1, 32, 32, 2), dtype=np.float32) - offset = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_heatmaps = np.ones( - (1, 32, 32, num_keypoints), dtype=np.float32) * _logit(0.01) - keypoint_offsets = np.zeros( - (1, 32, 32, num_keypoints * 2), dtype=np.float32) - keypoint_regression = np.random.randn(1, 32, 32, num_keypoints * 2) - - class_probs = np.zeros(1) - class_probs[0] = _logit(0.75) - class_center[0, 16, 16] = class_probs - height_width[0, 16, 16] = [5, 10] - offset[0, 16, 16] = [.25, .5] - keypoint_regression[0, 16, 16] = [ - -1., -1., - -1., 1., - 1., -1., - 1., 1.] - keypoint_heatmaps[0, 14, 14, 0] = _logit(0.9) - keypoint_heatmaps[0, 14, 18, 1] = _logit(0.9) - keypoint_heatmaps[0, 18, 14, 2] = _logit(0.9) - keypoint_heatmaps[0, 18, 18, 3] = _logit(0.05) # Note the low score. - - class_center = tf.constant(class_center) - height_width = tf.constant(height_width) - offset = tf.constant(offset) - keypoint_heatmaps = tf.constant(keypoint_heatmaps, dtype=tf.float32) - keypoint_offsets = tf.constant(keypoint_offsets, dtype=tf.float32) - keypoint_regression = tf.constant(keypoint_regression, dtype=tf.float32) - - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.BOX_SCALE: [height_width], - cnma.BOX_OFFSET: [offset], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP): - [keypoint_heatmaps], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET): - [keypoint_offsets], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_REGRESSION): - [keypoint_regression], - } - - def graph_fn(): - detections = model.postprocess(prediction_dict, - tf.constant([[128, 128, 3]])) - return detections - - detections = self.execute_cpu(graph_fn, []) - - self.assertAllClose(detections['detection_boxes'][0, 0], - np.array([55, 46, 75, 86]) / 128.0) - self.assertAllClose(detections['detection_scores'][0], - [.75, .5, .5, .5, .5]) - - self.assertEqual(detections['detection_classes'][0, 0], 0) - self.assertEqual(detections['num_detections'], [5]) - self.assertAllEqual([1, max_detection, num_keypoints, 2], - detections['detection_keypoints'].shape) - self.assertAllClose( - [[0.4375, 0.4375], [0.4375, 0.5625], [0.5625, 0.4375]], - detections['detection_keypoints'][0, 0, 0:3, :]) - self.assertAllEqual([1, max_detection, num_keypoints], - detections['detection_keypoint_scores'].shape) - - def test_postprocess_single_instance(self): - """Test the postprocess single instance function.""" - model = build_center_net_meta_arch( - num_classes=1, candidate_ranking_mode='score_distance_ratio') - num_keypoints = len(model._kp_params_dict[_TASK_NAME].keypoint_indices) - - class_center = np.zeros((1, 32, 32, 1), dtype=np.float32) - keypoint_heatmaps = np.zeros((1, 32, 32, num_keypoints), dtype=np.float32) - keypoint_offsets = np.zeros( - (1, 32, 32, num_keypoints * 2), dtype=np.float32) - keypoint_regression = np.random.randn(1, 32, 32, num_keypoints * 2) - - class_probs = np.zeros(1) - class_probs[0] = _logit(0.75) - class_center[0, 16, 16] = class_probs - keypoint_regression[0, 16, 16] = [ - -1., -1., - -1., 1., - 1., -1., - 1., 1.] - keypoint_heatmaps[0, 14, 14, 0] = _logit(0.9) - keypoint_heatmaps[0, 14, 18, 1] = _logit(0.9) - keypoint_heatmaps[0, 18, 14, 2] = _logit(0.9) - keypoint_heatmaps[0, 18, 18, 3] = _logit(0.05) # Note the low score. - - class_center = tf.constant(class_center) - keypoint_heatmaps = tf.constant(keypoint_heatmaps, dtype=tf.float32) - keypoint_offsets = tf.constant(keypoint_offsets, dtype=tf.float32) - keypoint_regression = tf.constant(keypoint_regression, dtype=tf.float32) - - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP): - [keypoint_heatmaps], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET): - [keypoint_offsets], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_REGRESSION): - [keypoint_regression], - } - - def graph_fn(): - detections = model.postprocess_single_instance_keypoints( - prediction_dict, - tf.constant([[128, 128, 3]])) - return detections - - detections = self.execute_cpu(graph_fn, []) - - self.assertAllEqual([1, 1, num_keypoints, 2], - detections['detection_keypoints'].shape) - self.assertAllEqual([1, 1, num_keypoints], - detections['detection_keypoint_scores'].shape) - - @parameterized.parameters( - {'per_keypoint_depth': False}, - {'per_keypoint_depth': True}, - ) - def test_postprocess_single_class_depth(self, per_keypoint_depth): - """Test the postprocess function.""" - model = build_center_net_meta_arch( - num_classes=1, - per_keypoint_offset=per_keypoint_depth, - predict_depth=True, - per_keypoint_depth=per_keypoint_depth) - num_keypoints = len(model._kp_params_dict[_TASK_NAME].keypoint_indices) - - class_center = np.zeros((1, 32, 32, 1), dtype=np.float32) - height_width = np.zeros((1, 32, 32, 2), dtype=np.float32) - offset = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_heatmaps = np.ones( - (1, 32, 32, num_keypoints), dtype=np.float32) * _logit(0.001) - keypoint_offsets = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_regression = np.random.randn(1, 32, 32, num_keypoints * 2) - - class_probs = np.zeros(1) - class_probs[0] = _logit(0.75) - class_center[0, 16, 16] = class_probs - height_width[0, 16, 16] = [5, 10] - offset[0, 16, 16] = [.25, .5] - keypoint_regression[0, 16, 16] = [-1., -1., -1., 1., 1., -1., 1., 1.] - keypoint_heatmaps[0, 14, 14, 0] = _logit(0.9) - keypoint_heatmaps[0, 14, 18, 1] = _logit(0.9) - keypoint_heatmaps[0, 18, 14, 2] = _logit(0.9) - keypoint_heatmaps[0, 18, 18, 3] = _logit(0.05) # Note the low score. - - if per_keypoint_depth: - keypoint_depth = np.zeros((1, 32, 32, num_keypoints), dtype=np.float32) - keypoint_depth[0, 14, 14, 0] = -1.0 - keypoint_depth[0, 14, 18, 1] = -1.1 - keypoint_depth[0, 18, 14, 2] = -1.2 - keypoint_depth[0, 18, 18, 3] = -1.3 - else: - keypoint_depth = np.zeros((1, 32, 32, 1), dtype=np.float32) - keypoint_depth[0, 14, 14, 0] = -1.0 - keypoint_depth[0, 14, 18, 0] = -1.1 - keypoint_depth[0, 18, 14, 0] = -1.2 - keypoint_depth[0, 18, 18, 0] = -1.3 - - class_center = tf.constant(class_center) - height_width = tf.constant(height_width) - offset = tf.constant(offset) - keypoint_heatmaps = tf.constant(keypoint_heatmaps, dtype=tf.float32) - keypoint_offsets = tf.constant(keypoint_offsets, dtype=tf.float32) - keypoint_regression = tf.constant(keypoint_regression, dtype=tf.float32) - keypoint_depth = tf.constant(keypoint_depth, dtype=tf.float32) - - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.BOX_SCALE: [height_width], - cnma.BOX_OFFSET: [offset], - cnma.get_keypoint_name(_TASK_NAME, - cnma.KEYPOINT_HEATMAP): [keypoint_heatmaps], - cnma.get_keypoint_name(_TASK_NAME, - cnma.KEYPOINT_OFFSET): [keypoint_offsets], - cnma.get_keypoint_name(_TASK_NAME, - cnma.KEYPOINT_REGRESSION): [keypoint_regression], - cnma.get_keypoint_name(_TASK_NAME, - cnma.KEYPOINT_DEPTH): [keypoint_depth] - } - - def graph_fn(): - detections = model.postprocess(prediction_dict, - tf.constant([[128, 128, 3]])) - return detections - - detections = self.execute_cpu(graph_fn, []) - - self.assertAllClose(detections['detection_keypoint_depths'][0, 0], - np.array([-1.0, -1.1, -1.2, 0.0])) - self.assertAllClose(detections['detection_keypoint_scores'][0, 0], - np.array([0.9, 0.9, 0.9, 0.1])) - - def test_mask_object_center_in_postprocess_by_true_image_shape(self): - """Test the postprocess function is masked by true_image_shape.""" - model = build_center_net_meta_arch(num_classes=1) - max_detection = model._center_params.max_box_predictions - num_keypoints = len(model._kp_params_dict[_TASK_NAME].keypoint_indices) - - class_center = np.zeros((1, 32, 32, 1), dtype=np.float32) - height_width = np.zeros((1, 32, 32, 2), dtype=np.float32) - offset = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_heatmaps = np.zeros((1, 32, 32, num_keypoints), dtype=np.float32) - keypoint_offsets = np.zeros((1, 32, 32, 2), dtype=np.float32) - keypoint_regression = np.random.randn(1, 32, 32, num_keypoints * 2) - - class_probs = np.zeros(1) - class_probs[0] = _logit(0.75) - class_center[0, 16, 16] = class_probs - height_width[0, 16, 16] = [5, 10] - offset[0, 16, 16] = [.25, .5] - keypoint_regression[0, 16, 16] = [ - -1., -1., - -1., 1., - 1., -1., - 1., 1.] - keypoint_heatmaps[0, 14, 14, 0] = _logit(0.9) - keypoint_heatmaps[0, 14, 18, 1] = _logit(0.9) - keypoint_heatmaps[0, 18, 14, 2] = _logit(0.9) - keypoint_heatmaps[0, 18, 18, 3] = _logit(0.05) # Note the low score. - - class_center = tf.constant(class_center) - height_width = tf.constant(height_width) - offset = tf.constant(offset) - keypoint_heatmaps = tf.constant(keypoint_heatmaps, dtype=tf.float32) - keypoint_offsets = tf.constant(keypoint_offsets, dtype=tf.float32) - keypoint_regression = tf.constant(keypoint_regression, dtype=tf.float32) - - print(class_center) - prediction_dict = { - cnma.OBJECT_CENTER: [class_center], - cnma.BOX_SCALE: [height_width], - cnma.BOX_OFFSET: [offset], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP): - [keypoint_heatmaps], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET): - [keypoint_offsets], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_REGRESSION): - [keypoint_regression], - } - - def graph_fn(): - detections = model.postprocess(prediction_dict, - tf.constant([[1, 1, 3]])) - return detections - - detections = self.execute_cpu(graph_fn, []) - - self.assertAllClose(detections['detection_boxes'][0, 0], - np.array([0, 0, 0, 0])) - # The class_center logits are initialized as 0's so it's filled with 0.5s. - # Despite that, we should only find one box. - self.assertAllClose(detections['detection_scores'][0], - [0.5, 0., 0., 0., 0.]) - - self.assertEqual(np.sum(detections['detection_classes']), 0) - self.assertEqual(detections['num_detections'], [1]) - self.assertAllEqual([1, max_detection, num_keypoints, 2], - detections['detection_keypoints'].shape) - self.assertAllEqual([1, max_detection, num_keypoints], - detections['detection_keypoint_scores'].shape) - - def test_get_instance_indices(self): - classes = tf.constant([[0, 1, 2, 0], [2, 1, 2, 2]], dtype=tf.int32) - num_detections = tf.constant([1, 3], dtype=tf.int32) - batch_index = 1 - class_id = 2 - model = build_center_net_meta_arch() - valid_indices = model._get_instance_indices( - classes, num_detections, batch_index, class_id) - self.assertAllEqual(valid_indices.numpy(), [0, 2]) - - def test_rescore_instances(self): - feature_extractor = DummyFeatureExtractor( - channel_means=(1.0, 2.0, 3.0), - channel_stds=(10., 20., 30.), - bgr_ordering=False, - num_feature_outputs=2, - stride=4) - image_resizer_fn = functools.partial( - preprocessor.resize_to_range, - min_dimension=128, - max_dimension=128, - pad_to_max_dimesnion=True) - - kp_params_1 = cnma.KeypointEstimationParams( - task_name='kpt_task_1', - class_id=0, - keypoint_indices=[0, 1, 2], - keypoint_std_dev=[0.00001] * 3, - classification_loss=losses.WeightedSigmoidClassificationLoss(), - localization_loss=losses.L1LocalizationLoss(), - keypoint_candidate_score_threshold=0.1, - rescore_instances=True) # Note rescoring for class_id = 0. - kp_params_2 = cnma.KeypointEstimationParams( - task_name='kpt_task_2', - class_id=1, - keypoint_indices=[3, 4], - keypoint_std_dev=[0.00001] * 2, - classification_loss=losses.WeightedSigmoidClassificationLoss(), - localization_loss=losses.L1LocalizationLoss(), - keypoint_candidate_score_threshold=0.1, - rescore_instances=False) - model = cnma.CenterNetMetaArch( - is_training=True, - add_summaries=False, - num_classes=2, - feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=get_fake_center_params(), - object_detection_params=get_fake_od_params(), - keypoint_params_dict={ - 'kpt_task_1': kp_params_1, - 'kpt_task_2': kp_params_2, - }) - - def graph_fn(): - classes = tf.constant([[1, 0]], dtype=tf.int32) - scores = tf.constant([[0.5, 0.75]], dtype=tf.float32) - keypoint_scores = tf.constant( - [ - [[0.1, 0.0, 0.3, 0.4, 0.5], - [0.1, 0.2, 0.3, 0.4, 0.5]], - ]) - new_scores = model._rescore_instances(classes, scores, keypoint_scores) - return new_scores - - new_scores = self.execute_cpu(graph_fn, []) - expected_scores = np.array( - [[0.5, 0.75 * (0.1 + 0.3)/2]] - ) - self.assertAllClose(expected_scores, new_scores) - - -def get_fake_prediction_dict(input_height, - input_width, - stride, - per_keypoint_depth=False): - """Prepares the fake prediction dictionary.""" - output_height = input_height // stride - output_width = input_width // stride - object_center = np.zeros((2, output_height, output_width, _NUM_CLASSES), - dtype=np.float32) - # Box center: - # y: floor((0.54 + 0.56) / 2 * 4) = 2, - # x: floor((0.54 + 0.56) / 2 * 8) = 4 - object_center[0, 2, 4, 1] = 1.0 - object_center = _logit(object_center) - - # Box size: - # height: (0.56 - 0.54) * 4 = 0.08 - # width: (0.56 - 0.54) * 8 = 0.16 - object_scale = np.zeros((2, output_height, output_width, 2), dtype=np.float32) - object_scale[0, 2, 4] = 0.08, 0.16 - - # Box center offset coordinate (0.55, 0.55): - # y-offset: 0.55 * 4 - 2 = 0.2 - # x-offset: 0.55 * 8 - 4 = 0.4 - object_offset = np.zeros((2, output_height, output_width, 2), - dtype=np.float32) - object_offset[0, 2, 4] = 0.2, 0.4 - - keypoint_heatmap = np.zeros((2, output_height, output_width, _NUM_KEYPOINTS), - dtype=np.float32) - keypoint_heatmap[0, 2, 4, 1] = 1.0 - keypoint_heatmap[0, 2, 4, 3] = 1.0 - keypoint_heatmap = _logit(keypoint_heatmap) - - keypoint_offset = np.zeros((2, output_height, output_width, 2), - dtype=np.float32) - keypoint_offset[0, 2, 4] = 0.2, 0.4 - - keypoint_depth = np.zeros((2, output_height, output_width, - _NUM_KEYPOINTS if per_keypoint_depth else 1), - dtype=np.float32) - keypoint_depth[0, 2, 4] = 3.0 - - keypoint_regression = np.zeros( - (2, output_height, output_width, 2 * _NUM_KEYPOINTS), dtype=np.float32) - keypoint_regression[0, 2, 4] = 0.0, 0.0, 0.2, 0.4, 0.0, 0.0, 0.2, 0.4 - - mask_heatmap = np.zeros((2, output_height, output_width, _NUM_CLASSES), - dtype=np.float32) - mask_heatmap[0, 2, 4, 1] = 1.0 - mask_heatmap = _logit(mask_heatmap) - - densepose_heatmap = np.zeros((2, output_height, output_width, - _DENSEPOSE_NUM_PARTS), dtype=np.float32) - densepose_heatmap[0, 2, 4, 5] = 1.0 - densepose_heatmap = _logit(densepose_heatmap) - - densepose_regression = np.zeros((2, output_height, output_width, - 2 * _DENSEPOSE_NUM_PARTS), dtype=np.float32) - # The surface coordinate indices for part index 5 are: - # (5 * 2, 5 * 2 + 1), or (10, 11). - densepose_regression[0, 2, 4, 10:12] = 0.4, 0.7 - - track_reid_embedding = np.zeros((2, output_height, output_width, - _REID_EMBED_SIZE), dtype=np.float32) - track_reid_embedding[0, 2, 4, :] = np.arange(_REID_EMBED_SIZE) - - temporal_offsets = np.zeros((2, output_height, output_width, 2), - dtype=np.float32) - temporal_offsets[0, 2, 4, :] = 5 - - prediction_dict = { - 'preprocessed_inputs': - tf.zeros((2, input_height, input_width, 3)), - cnma.OBJECT_CENTER: [ - tf.constant(object_center), - tf.constant(object_center) - ], - cnma.BOX_SCALE: [tf.constant(object_scale), - tf.constant(object_scale)], - cnma.BOX_OFFSET: [tf.constant(object_offset), - tf.constant(object_offset)], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_HEATMAP): [ - tf.constant(keypoint_heatmap), - tf.constant(keypoint_heatmap) - ], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_OFFSET): [ - tf.constant(keypoint_offset), - tf.constant(keypoint_offset) - ], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_REGRESSION): [ - tf.constant(keypoint_regression), - tf.constant(keypoint_regression) - ], - cnma.get_keypoint_name(_TASK_NAME, cnma.KEYPOINT_DEPTH): [ - tf.constant(keypoint_depth), - tf.constant(keypoint_depth) - ], - cnma.SEGMENTATION_HEATMAP: [ - tf.constant(mask_heatmap), - tf.constant(mask_heatmap) - ], - cnma.DENSEPOSE_HEATMAP: [ - tf.constant(densepose_heatmap), - tf.constant(densepose_heatmap), - ], - cnma.DENSEPOSE_REGRESSION: [ - tf.constant(densepose_regression), - tf.constant(densepose_regression), - ], - cnma.TRACK_REID: [ - tf.constant(track_reid_embedding), - tf.constant(track_reid_embedding), - ], - cnma.TEMPORAL_OFFSET: [ - tf.constant(temporal_offsets), - tf.constant(temporal_offsets), - ], - } - return prediction_dict - - -def get_fake_groundtruth_dict(input_height, - input_width, - stride, - has_depth=False): - """Prepares the fake groundtruth dictionary.""" - # A small box with center at (0.55, 0.55). - boxes = [ - tf.constant([[0.54, 0.54, 0.56, 0.56]]), - tf.constant([[0.0, 0.0, 0.5, 0.5]]), - ] - classes = [ - tf.one_hot([1], depth=_NUM_CLASSES), - tf.one_hot([0], depth=_NUM_CLASSES), - ] - weights = [ - tf.constant([1.]), - tf.constant([0.]), - ] - keypoints = [ - tf.tile( - tf.expand_dims( - tf.constant([[float('nan'), 0.55, - float('nan'), 0.55, 0.55, 0.0]]), - axis=2), - multiples=[1, 1, 2]), - tf.tile( - tf.expand_dims( - tf.constant([[float('nan'), 0.55, - float('nan'), 0.55, 0.55, 0.0]]), - axis=2), - multiples=[1, 1, 2]), - ] - if has_depth: - keypoint_depths = [ - tf.constant([[float('nan'), 3.0, - float('nan'), 3.0, 0.55, 0.0]]), - tf.constant([[float('nan'), 0.55, - float('nan'), 0.55, 0.55, 0.0]]) - ] - keypoint_depth_weights = [ - tf.constant([[1.0, 1.0, 1.0, 1.0, 0.0, 0.0]]), - tf.constant([[1.0, 1.0, 1.0, 1.0, 0.0, 0.0]]) - ] - else: - keypoint_depths = [ - tf.constant([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]), - tf.constant([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]) - ] - keypoint_depth_weights = [ - tf.constant([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]), - tf.constant([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]) - ] - labeled_classes = [ - tf.one_hot([1], depth=_NUM_CLASSES) + tf.one_hot([2], depth=_NUM_CLASSES), - tf.one_hot([0], depth=_NUM_CLASSES) + tf.one_hot([1], depth=_NUM_CLASSES), - ] - mask = np.zeros((1, input_height, input_width), dtype=np.float32) - mask[0, 8:8+stride, 16:16+stride] = 1 - masks = [ - tf.constant(mask), - tf.zeros_like(mask), - ] - densepose_num_points = [ - tf.constant([1], dtype=tf.int32), - tf.constant([0], dtype=tf.int32), - ] - densepose_part_ids = [ - tf.constant([[5, 0, 0]], dtype=tf.int32), - tf.constant([[0, 0, 0]], dtype=tf.int32), - ] - densepose_surface_coords_np = np.zeros((1, 3, 4), dtype=np.float32) - densepose_surface_coords_np[0, 0, :] = 0.55, 0.55, 0.4, 0.7 - densepose_surface_coords = [ - tf.constant(densepose_surface_coords_np), - tf.zeros_like(densepose_surface_coords_np) - ] - track_ids = [ - tf.constant([2], dtype=tf.int32), - tf.constant([1], dtype=tf.int32), - ] - temporal_offsets = [ - tf.constant([[5.0, 5.0]], dtype=tf.float32), - tf.constant([[2.0, 3.0]], dtype=tf.float32), - ] - track_match_flags = [ - tf.constant([1.0], dtype=tf.float32), - tf.constant([1.0], dtype=tf.float32), - ] - groundtruth_dict = { - fields.BoxListFields.boxes: boxes, - fields.BoxListFields.weights: weights, - fields.BoxListFields.classes: classes, - fields.BoxListFields.keypoints: keypoints, - fields.BoxListFields.keypoint_depths: keypoint_depths, - fields.BoxListFields.keypoint_depth_weights: keypoint_depth_weights, - fields.BoxListFields.masks: masks, - fields.BoxListFields.densepose_num_points: densepose_num_points, - fields.BoxListFields.densepose_part_ids: densepose_part_ids, - fields.BoxListFields.densepose_surface_coords: densepose_surface_coords, - fields.BoxListFields.track_ids: track_ids, - fields.BoxListFields.temporal_offsets: temporal_offsets, - fields.BoxListFields.track_match_flags: track_match_flags, - fields.InputDataFields.groundtruth_labeled_classes: labeled_classes, - } - return groundtruth_dict - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMetaComputeLossTest(test_case.TestCase, parameterized.TestCase): - """Test for CenterNet loss compuation related functions.""" - - def setUp(self): - self.model = build_center_net_meta_arch() - self.classification_loss_fn = self.model._center_params.classification_loss - self.localization_loss_fn = self.model._od_params.localization_loss - self.true_image_shapes = tf.constant([[16, 24, 3], [16, 24, 3]]) - self.input_height = 16 - self.input_width = 32 - self.stride = 4 - self.per_pixel_weights = self.get_per_pixel_weights(self.true_image_shapes, - self.input_height, - self.input_width, - self.stride) - self.prediction_dict = get_fake_prediction_dict(self.input_height, - self.input_width, - self.stride) - self.model._groundtruth_lists = get_fake_groundtruth_dict( - self.input_height, self.input_width, self.stride) - super(CenterNetMetaComputeLossTest, self).setUp() - - def get_per_pixel_weights(self, true_image_shapes, input_height, input_width, - stride): - output_height, output_width = (input_height // stride, - input_width // stride) - - # TODO(vighneshb) Explore whether using floor here is safe. - output_true_image_shapes = tf.ceil(tf.to_float(true_image_shapes) / stride) - per_pixel_weights = cnma.get_valid_anchor_weights_in_flattened_image( - output_true_image_shapes, output_height, output_width) - per_pixel_weights = tf.expand_dims(per_pixel_weights, 2) - return per_pixel_weights - - def test_compute_object_center_loss(self): - def graph_fn(): - loss = self.model._compute_object_center_loss( - object_center_predictions=self.prediction_dict[cnma.OBJECT_CENTER], - input_height=self.input_height, - input_width=self.input_width, - per_pixel_weights=self.per_pixel_weights) - return loss - - loss = self.execute(graph_fn, []) - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, loss) - - default_value = self.model._center_params.use_labeled_classes - self.model._center_params = ( - self.model._center_params._replace(use_labeled_classes=True)) - loss = self.model._compute_object_center_loss( - object_center_predictions=self.prediction_dict[cnma.OBJECT_CENTER], - input_height=self.input_height, - input_width=self.input_width, - per_pixel_weights=self.per_pixel_weights) - self.model._center_params = ( - self.model._center_params._replace(use_labeled_classes=default_value)) - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, loss) - - def test_compute_box_scale_and_offset_loss(self): - def graph_fn(): - scale_loss, offset_loss = self.model._compute_box_scale_and_offset_loss( - scale_predictions=self.prediction_dict[cnma.BOX_SCALE], - offset_predictions=self.prediction_dict[cnma.BOX_OFFSET], - input_height=self.input_height, - input_width=self.input_width) - return scale_loss, offset_loss - - scale_loss, offset_loss = self.execute(graph_fn, []) - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, scale_loss) - self.assertGreater(0.01, offset_loss) - - def test_compute_kp_heatmap_loss(self): - def graph_fn(): - loss = self.model._compute_kp_heatmap_loss( - input_height=self.input_height, - input_width=self.input_width, - task_name=_TASK_NAME, - heatmap_predictions=self.prediction_dict[cnma.get_keypoint_name( - _TASK_NAME, cnma.KEYPOINT_HEATMAP)], - classification_loss_fn=self.classification_loss_fn, - per_pixel_weights=self.per_pixel_weights) - return loss - - loss = self.execute(graph_fn, []) - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, loss) - - def test_compute_kp_offset_loss(self): - def graph_fn(): - loss = self.model._compute_kp_offset_loss( - input_height=self.input_height, - input_width=self.input_width, - task_name=_TASK_NAME, - offset_predictions=self.prediction_dict[cnma.get_keypoint_name( - _TASK_NAME, cnma.KEYPOINT_OFFSET)], - localization_loss_fn=self.localization_loss_fn) - return loss - - loss = self.execute(graph_fn, []) - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, loss) - - def test_compute_kp_regression_loss(self): - def graph_fn(): - loss = self.model._compute_kp_regression_loss( - input_height=self.input_height, - input_width=self.input_width, - task_name=_TASK_NAME, - regression_predictions=self.prediction_dict[cnma.get_keypoint_name( - _TASK_NAME, cnma.KEYPOINT_REGRESSION,)], - localization_loss_fn=self.localization_loss_fn) - return loss - - loss = self.execute(graph_fn, []) - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, loss) - - @parameterized.parameters( - {'per_keypoint_depth': False}, - {'per_keypoint_depth': True}, - ) - def test_compute_kp_depth_loss(self, per_keypoint_depth): - prediction_dict = get_fake_prediction_dict( - self.input_height, - self.input_width, - self.stride, - per_keypoint_depth=per_keypoint_depth) - model = build_center_net_meta_arch( - num_classes=1, - per_keypoint_offset=per_keypoint_depth, - predict_depth=True, - per_keypoint_depth=per_keypoint_depth, - peak_radius=1 if per_keypoint_depth else 0) - model._groundtruth_lists = get_fake_groundtruth_dict( - self.input_height, self.input_width, self.stride, has_depth=True) - - def graph_fn(): - loss = model._compute_kp_depth_loss( - input_height=self.input_height, - input_width=self.input_width, - task_name=_TASK_NAME, - depth_predictions=prediction_dict[cnma.get_keypoint_name( - _TASK_NAME, cnma.KEYPOINT_DEPTH)], - localization_loss_fn=self.localization_loss_fn) - return loss - - loss = self.execute(graph_fn, []) - - if per_keypoint_depth: - # The loss is computed on a disk with radius 1 but only the center pixel - # has the accurate prediction. The final loss is (4 * |3-0|) / 5 = 2.4 - self.assertAlmostEqual(2.4, loss, delta=1e-4) - else: - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, loss) - - def test_compute_track_embedding_loss(self): - default_fc = self.model.track_reid_classification_net - # Initialize the kernel to extreme values so that the classification score - # is close to (0, 0, 1) after the softmax layer. - kernel_initializer = tf.constant_initializer( - [[1, 1, 0], [-1000000, -1000000, 1000000]]) - self.model.track_reid_classification_net = tf.keras.layers.Dense( - _NUM_TRACK_IDS, - kernel_initializer=kernel_initializer, - input_shape=(_REID_EMBED_SIZE,)) - - loss = self.model._compute_track_embedding_loss( - input_height=self.input_height, - input_width=self.input_width, - object_reid_predictions=self.prediction_dict[cnma.TRACK_REID]) - - self.model.track_reid_classification_net = default_fc - - # The prediction and groundtruth are curated to produce very low loss. - self.assertGreater(0.01, loss) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMetaArchRestoreTest(test_case.TestCase): - - def test_restore_map_resnet(self): - """Test restore map for a resnet backbone.""" - - model = build_center_net_meta_arch(build_resnet=True) - restore_from_objects_map = model.restore_from_objects('classification') - self.assertIsInstance(restore_from_objects_map['feature_extractor'], - tf.keras.Model) - - def test_retore_map_detection(self): - """Test that detection checkpoints can be restored.""" - - model = build_center_net_meta_arch(build_resnet=True) - restore_from_objects_map = model.restore_from_objects('detection') - - self.assertIsInstance(restore_from_objects_map['model']._feature_extractor, - tf.keras.Model) - - -class DummyFeatureExtractor(cnma.CenterNetFeatureExtractor): - - def __init__(self, - channel_means, - channel_stds, - bgr_ordering, - num_feature_outputs, - stride): - self._num_feature_outputs = num_feature_outputs - self._stride = stride - super(DummyFeatureExtractor, self).__init__( - channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - - def predict(self): - pass - - def loss(self): - pass - - def postprocess(self): - pass - - def call(self, inputs): - batch_size, input_height, input_width, _ = inputs.shape - fake_output = tf.ones([ - batch_size, input_height // self._stride, input_width // self._stride, - 64 - ], dtype=tf.float32) - return [fake_output] * self._num_feature_outputs - - @property - def out_stride(self): - return self._stride - - @property - def num_feature_outputs(self): - return self._num_feature_outputs - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetFeatureExtractorTest(test_case.TestCase): - """Test the base feature extractor class.""" - - def test_preprocess(self): - feature_extractor = DummyFeatureExtractor( - channel_means=(1.0, 2.0, 3.0), - channel_stds=(10., 20., 30.), bgr_ordering=False, - num_feature_outputs=2, stride=4) - - img = np.zeros((2, 32, 32, 3)) - img[:, :, :] = 11, 22, 33 - - def graph_fn(): - output = feature_extractor.preprocess(img) - return output - - output = self.execute(graph_fn, []) - self.assertAlmostEqual(output.sum(), 2 * 32 * 32 * 3) - - def test_preprocess_reverse(self): - feature_extractor = DummyFeatureExtractor( - channel_means=(1.0, 2.0, 3.0), - channel_stds=(10., 20., 30.), bgr_ordering=False, - num_feature_outputs=2, stride=4) - - img = np.zeros((2, 32, 32, 3)) - img[:, :, :] = 11, 22, 33 - - def graph_fn(): - output = feature_extractor.preprocess_reverse( - feature_extractor.preprocess(img)) - return output - - output = self.execute(graph_fn, []) - self.assertAllClose(img, output) - - def test_bgr_ordering(self): - feature_extractor = DummyFeatureExtractor( - channel_means=(0.0, 0.0, 0.0), - channel_stds=(1., 1., 1.), bgr_ordering=True, - num_feature_outputs=2, stride=4) - - img = np.zeros((2, 32, 32, 3), dtype=np.float32) - img[:, :, :] = 1, 2, 3 - - def graph_fn(): - output = feature_extractor.preprocess(img) - return output - - output = self.execute(graph_fn, []) - self.assertAllClose(output[..., 2], 1 * np.ones((2, 32, 32))) - self.assertAllClose(output[..., 1], 2 * np.ones((2, 32, 32))) - self.assertAllClose(output[..., 0], 3 * np.ones((2, 32, 32))) - - def test_default_ordering(self): - feature_extractor = DummyFeatureExtractor( - channel_means=(0.0, 0.0, 0.0), - channel_stds=(1., 1., 1.), bgr_ordering=False, - num_feature_outputs=2, stride=4) - - img = np.zeros((2, 32, 32, 3), dtype=np.float32) - img[:, :, :] = 1, 2, 3 - - def graph_fn(): - output = feature_extractor.preprocess(img) - return output - - output = self.execute(graph_fn, []) - self.assertAllClose(output[..., 0], 1 * np.ones((2, 32, 32))) - self.assertAllClose(output[..., 1], 2 * np.ones((2, 32, 32))) - self.assertAllClose(output[..., 2], 3 * np.ones((2, 32, 32))) - - -class Dummy1dFeatureExtractor(cnma.CenterNetFeatureExtractor): - """Returns a static tensor.""" - - def __init__(self, tensor, out_stride=1, channel_means=(0., 0., 0.), - channel_stds=(1., 1., 1.), bgr_ordering=False): - """Intializes the feature extractor. - - Args: - tensor: The tensor to return as the processed feature. - out_stride: The out_stride to return if asked. - channel_means: Ignored, but provided for API compatability. - channel_stds: Ignored, but provided for API compatability. - bgr_ordering: Ignored, but provided for API compatability. - """ - - super().__init__( - channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - self._tensor = tensor - self._out_stride = out_stride - - def call(self, inputs): - return [self._tensor] - - @property - def out_stride(self): - """The stride in the output image of the network.""" - return self._out_stride - - @property - def num_feature_outputs(self): - """Ther number of feature outputs returned by the feature extractor.""" - return 1 - - @property - def supported_sub_model_types(self): - return ['detection'] - - def get_sub_model(self, sub_model_type): - if sub_model_type == 'detection': - return self._network - else: - ValueError('Sub model type "{}" not supported.'.format(sub_model_type)) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMetaArch1dTest(test_case.TestCase, parameterized.TestCase): - - @parameterized.parameters([1, 2]) - def test_outputs_with_correct_shape(self, stride): - # The 1D case reuses code from the 2D cases. These tests only check that - # the output shapes are correct, and relies on other tests for correctness. - batch_size = 2 - height = 1 - width = 32 - channels = 16 - unstrided_inputs = np.random.randn( - batch_size, height, width, channels) - fixed_output_features = np.random.randn( - batch_size, height, width // stride, channels) - max_boxes = 10 - num_classes = 3 - feature_extractor = Dummy1dFeatureExtractor(fixed_output_features, stride) - arch = cnma.CenterNetMetaArch( - is_training=True, - add_summaries=True, - num_classes=num_classes, - feature_extractor=feature_extractor, - image_resizer_fn=None, - object_center_params=cnma.ObjectCenterParams( - classification_loss=losses.PenaltyReducedLogisticFocalLoss(), - object_center_loss_weight=1.0, - max_box_predictions=max_boxes, - ), - object_detection_params=cnma.ObjectDetectionParams( - localization_loss=losses.L1LocalizationLoss(), - scale_loss_weight=1.0, - offset_loss_weight=1.0, - ), - keypoint_params_dict=None, - mask_params=None, - densepose_params=None, - track_params=None, - temporal_offset_params=None, - use_depthwise=False, - compute_heatmap_sparse=False, - non_max_suppression_fn=None, - unit_height_conv=True) - arch.provide_groundtruth( - groundtruth_boxes_list=[ - tf.constant([[0, 0.5, 1.0, 0.75], - [0, 0.1, 1.0, 0.25]], tf.float32), - tf.constant([[0, 0, 1.0, 1.0], - [0, 0, 0.0, 0.0]], tf.float32) - ], - groundtruth_classes_list=[ - tf.constant([[0, 0, 1], - [0, 1, 0]], tf.float32), - tf.constant([[1, 0, 0], - [0, 0, 0]], tf.float32) - ], - groundtruth_weights_list=[ - tf.constant([1.0, 1.0]), - tf.constant([1.0, 0.0])] - ) - - predictions = arch.predict(None, None) # input is hardcoded above. - predictions['preprocessed_inputs'] = tf.constant(unstrided_inputs) - true_shapes = tf.constant([[1, 32, 16], [1, 24, 16]], tf.int32) - postprocess_output = arch.postprocess(predictions, true_shapes) - losses_output = arch.loss(predictions, true_shapes) - - self.assertIn('extracted_features', predictions) - self.assertIn('%s/%s' % (cnma.LOSS_KEY_PREFIX, cnma.OBJECT_CENTER), - losses_output) - self.assertEqual((), losses_output['%s/%s' % ( - cnma.LOSS_KEY_PREFIX, cnma.OBJECT_CENTER)].shape) - self.assertIn('%s/%s' % (cnma.LOSS_KEY_PREFIX, cnma.BOX_SCALE), - losses_output) - self.assertEqual((), losses_output['%s/%s' % ( - cnma.LOSS_KEY_PREFIX, cnma.BOX_SCALE)].shape) - self.assertIn('%s/%s' % (cnma.LOSS_KEY_PREFIX, cnma.BOX_OFFSET), - losses_output) - self.assertEqual((), losses_output['%s/%s' % ( - cnma.LOSS_KEY_PREFIX, cnma.BOX_OFFSET)].shape) - - self.assertIn('detection_scores', postprocess_output) - self.assertEqual(postprocess_output['detection_scores'].shape, - (batch_size, max_boxes)) - self.assertIn('detection_multiclass_scores', postprocess_output) - self.assertEqual(postprocess_output['detection_multiclass_scores'].shape, - (batch_size, max_boxes, num_classes)) - self.assertIn('detection_classes', postprocess_output) - self.assertEqual(postprocess_output['detection_classes'].shape, - (batch_size, max_boxes)) - self.assertIn('num_detections', postprocess_output) - self.assertEqual(postprocess_output['num_detections'].shape, - (batch_size,)) - self.assertIn('detection_boxes', postprocess_output) - self.assertEqual(postprocess_output['detection_boxes'].shape, - (batch_size, max_boxes, 4)) - self.assertIn('detection_boxes_strided', postprocess_output) - self.assertEqual(postprocess_output['detection_boxes_strided'].shape, - (batch_size, max_boxes, 4)) - - self.assertIn(cnma.OBJECT_CENTER, predictions) - self.assertEqual(predictions[cnma.OBJECT_CENTER][0].shape, - (batch_size, height, width // stride, num_classes)) - self.assertIn(cnma.BOX_SCALE, predictions) - self.assertEqual(predictions[cnma.BOX_SCALE][0].shape, - (batch_size, height, width // stride, 2)) - self.assertIn(cnma.BOX_OFFSET, predictions) - self.assertEqual(predictions[cnma.BOX_OFFSET][0].shape, - (batch_size, height, width // stride, 2)) - self.assertIn('preprocessed_inputs', predictions) - - -if __name__ == '__main__': - tf.enable_v2_behavior() - tf.test.main() diff --git a/research/object_detection/meta_architectures/context_rcnn_lib.py b/research/object_detection/meta_architectures/context_rcnn_lib.py deleted file mode 100644 index c446c0d9331..00000000000 --- a/research/object_detection/meta_architectures/context_rcnn_lib.py +++ /dev/null @@ -1,302 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Library functions for ContextRCNN.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf -import tf_slim as slim - - -# The negative value used in padding the invalid weights. -_NEGATIVE_PADDING_VALUE = -100000 - - -def filter_weight_value(weights, values, valid_mask): - """Filters weights and values based on valid_mask. - - _NEGATIVE_PADDING_VALUE will be added to invalid elements in the weights to - avoid their contribution in softmax. 0 will be set for the invalid elements in - the values. - - Args: - weights: A float Tensor of shape [batch_size, input_size, context_size]. - values: A float Tensor of shape [batch_size, context_size, - projected_dimension]. - valid_mask: A boolean Tensor of shape [batch_size, context_size]. True means - valid and False means invalid. - - Returns: - weights: A float Tensor of shape [batch_size, input_size, context_size]. - values: A float Tensor of shape [batch_size, context_size, - projected_dimension]. - - Raises: - ValueError: If shape of doesn't match. - """ - w_batch_size, _, w_context_size = weights.shape - v_batch_size, v_context_size, _ = values.shape - m_batch_size, m_context_size = valid_mask.shape - if w_batch_size != v_batch_size or v_batch_size != m_batch_size: - raise ValueError("Please make sure the first dimension of the input" - " tensors are the same.") - - if w_context_size != v_context_size: - raise ValueError("Please make sure the third dimension of weights matches" - " the second dimension of values.") - - if w_context_size != m_context_size: - raise ValueError("Please make sure the third dimension of the weights" - " matches the second dimension of the valid_mask.") - - valid_mask = valid_mask[..., tf.newaxis] - - # Force the invalid weights to be very negative so it won't contribute to - # the softmax. - - very_negative_mask = tf.ones( - weights.shape, dtype=weights.dtype) * _NEGATIVE_PADDING_VALUE - valid_weight_mask = tf.tile(tf.transpose(valid_mask, perm=[0, 2, 1]), - [1, weights.shape[1], 1]) - weights = tf.where(valid_weight_mask, - x=weights, y=very_negative_mask) - - # Force the invalid values to be 0. - values *= tf.cast(valid_mask, values.dtype) - - return weights, values - - -def compute_valid_mask(num_valid_elements, num_elements): - """Computes mask of valid entries within padded context feature. - - Args: - num_valid_elements: A int32 Tensor of shape [batch_size]. - num_elements: An int32 Tensor. - - Returns: - A boolean Tensor of the shape [batch_size, num_elements]. True means - valid and False means invalid. - """ - batch_size = num_valid_elements.shape[0] - element_idxs = tf.range(num_elements, dtype=tf.int32) - batch_element_idxs = tf.tile(element_idxs[tf.newaxis, ...], [batch_size, 1]) - num_valid_elements = num_valid_elements[..., tf.newaxis] - valid_mask = tf.less(batch_element_idxs, num_valid_elements) - return valid_mask - - -def project_features(features, projection_dimension, is_training, normalize): - """Projects features to another feature space. - - Args: - features: A float Tensor of shape [batch_size, features_size, - num_features]. - projection_dimension: A int32 Tensor. - is_training: A boolean Tensor (affecting batch normalization). - normalize: A boolean Tensor. If true, the output features will be l2 - normalized on the last dimension. - - Returns: - A float Tensor of shape [batch, features_size, projection_dimension]. - """ - # TODO(guanhangwu) Figure out a better way of specifying the batch norm - # params. - batch_norm_params = { - "is_training": is_training, - "decay": 0.97, - "epsilon": 0.001, - "center": True, - "scale": True - } - - batch_size, _, num_features = features.shape - features = tf.reshape(features, [-1, num_features]) - projected_features = slim.fully_connected( - features, - num_outputs=projection_dimension, - activation_fn=tf.nn.relu6, - normalizer_fn=slim.batch_norm, - normalizer_params=batch_norm_params) - - projected_features = tf.reshape(projected_features, - [batch_size, -1, projection_dimension]) - - if normalize: - projected_features = tf.math.l2_normalize(projected_features, axis=-1) - - return projected_features - - -def attention_block(input_features, context_features, bottleneck_dimension, - output_dimension, attention_temperature, - keys_values_valid_mask, queries_valid_mask, - is_training, block_name="AttentionBlock"): - """Generic attention block. - - Args: - input_features: A float Tensor of shape [batch_size, input_size, - num_input_features]. - context_features: A float Tensor of shape [batch_size, context_size, - num_context_features]. - bottleneck_dimension: A int32 Tensor representing the bottleneck dimension - for intermediate projections. - output_dimension: A int32 Tensor representing the last dimension of the - output feature. - attention_temperature: A float Tensor. It controls the temperature of the - softmax for weights calculation. The formula for calculation as follows: - weights = exp(weights / temperature) / sum(exp(weights / temperature)) - keys_values_valid_mask: A boolean Tensor of shape - [batch_size, context_size]. - queries_valid_mask: A boolean Tensor of shape - [batch_size, max_num_proposals]. - is_training: A boolean Tensor (affecting batch normalization). - block_name: A string to specify names for different attention blocks - - Returns: - A float Tensor of shape [batch_size, input_size, output_dimension]. - """ - - with tf.variable_scope(block_name): - queries = project_features( - input_features, bottleneck_dimension, is_training, normalize=True) - keys = project_features( - context_features, bottleneck_dimension, is_training, normalize=True) - values = project_features( - context_features, bottleneck_dimension, is_training, normalize=True) - - # masking out any keys which are padding - keys *= tf.cast(keys_values_valid_mask[..., tf.newaxis], keys.dtype) - queries *= tf.cast(queries_valid_mask[..., tf.newaxis], queries.dtype) - - weights = tf.matmul(queries, keys, transpose_b=True) - - weights, values = filter_weight_value(weights, values, - keys_values_valid_mask) - - weights = tf.identity(tf.nn.softmax(weights / attention_temperature), - name=block_name+"AttentionWeights") - - features = tf.matmul(weights, values) - - output_features = project_features( - features, output_dimension, is_training, normalize=False) - return output_features - - -def _compute_box_context_attention(box_features, num_proposals, - context_features, valid_context_size, - bottleneck_dimension, - attention_temperature, is_training, - max_num_proposals, - use_self_attention=False, - use_long_term_attention=True, - self_attention_in_sequence=False, - num_attention_heads=1, - num_attention_layers=1): - """Computes the attention feature from the context given a batch of box. - - Args: - box_features: A float Tensor of shape [batch_size * max_num_proposals, - height, width, channels]. It is pooled features from first stage - proposals. - num_proposals: The number of valid box proposals. - context_features: A float Tensor of shape [batch_size, context_size, - num_context_features]. - valid_context_size: A int32 Tensor of shape [batch_size]. - bottleneck_dimension: A int32 Tensor representing the bottleneck dimension - for intermediate projections. - attention_temperature: A float Tensor. It controls the temperature of the - softmax for weights calculation. The formula for calculation as follows: - weights = exp(weights / temperature) / sum(exp(weights / temperature)) - is_training: A boolean Tensor (affecting batch normalization). - max_num_proposals: The number of box proposals for each image. - use_self_attention: Whether to use an attention block across the - first stage predicted box features for the input image. - use_long_term_attention: Whether to use an attention block into the context - features. - self_attention_in_sequence: Whether self-attention and long term attention - should be in sequence or parallel. - num_attention_heads: Number of heads for multi-headed attention. - num_attention_layers: Number of heads for multi-layered attention. - - Returns: - A float Tensor of shape [batch_size, max_num_proposals, 1, 1, channels]. - """ - _, context_size, _ = context_features.shape - context_valid_mask = compute_valid_mask(valid_context_size, context_size) - - total_proposals, height, width, channels = box_features.shape - - batch_size = total_proposals // max_num_proposals - box_features = tf.reshape( - box_features, - [batch_size, - max_num_proposals, - height, - width, - channels]) - - # Average pools over height and width dimension so that the shape of - # box_features becomes [batch_size, max_num_proposals, channels]. - box_features = tf.reduce_mean(box_features, [2, 3]) - box_valid_mask = compute_valid_mask( - num_proposals, - box_features.shape[1]) - - if use_self_attention: - self_attention_box_features = attention_block( - box_features, box_features, bottleneck_dimension, channels.value, - attention_temperature, keys_values_valid_mask=box_valid_mask, - queries_valid_mask=box_valid_mask, is_training=is_training, - block_name="SelfAttentionBlock") - - if use_long_term_attention: - if use_self_attention and self_attention_in_sequence: - input_features = tf.add(self_attention_box_features, box_features) - input_features = tf.divide(input_features, 2) - else: - input_features = box_features - original_input_features = input_features - for jdx in range(num_attention_layers): - layer_features = tf.zeros_like(input_features) - for idx in range(num_attention_heads): - block_name = "AttentionBlock" + str(idx) + "_AttentionLayer" +str(jdx) - attention_features = attention_block( - input_features, - context_features, - bottleneck_dimension, - channels.value, - attention_temperature, - keys_values_valid_mask=context_valid_mask, - queries_valid_mask=box_valid_mask, - is_training=is_training, - block_name=block_name) - layer_features = tf.add(layer_features, attention_features) - layer_features = tf.divide(layer_features, num_attention_heads) - input_features = tf.add(input_features, layer_features) - output_features = tf.add(input_features, original_input_features) - if not self_attention_in_sequence and use_self_attention: - output_features = tf.add(self_attention_box_features, output_features) - elif use_self_attention: - output_features = self_attention_box_features - else: - output_features = tf.zeros(self_attention_box_features.shape) - - # Expands the dimension back to match with the original feature map. - output_features = output_features[:, :, tf.newaxis, tf.newaxis, :] - - return output_features diff --git a/research/object_detection/meta_architectures/context_rcnn_lib_tf1_test.py b/research/object_detection/meta_architectures/context_rcnn_lib_tf1_test.py deleted file mode 100644 index cba5aa0a3e1..00000000000 --- a/research/object_detection/meta_architectures/context_rcnn_lib_tf1_test.py +++ /dev/null @@ -1,165 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for context_rcnn_lib.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -from absl.testing import parameterized -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import context_rcnn_lib -from object_detection.utils import test_case -from object_detection.utils import tf_version - -_NEGATIVE_PADDING_VALUE = -100000 - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ContextRcnnLibTest(parameterized.TestCase, test_case.TestCase, - tf.test.TestCase): - """Tests for the functions in context_rcnn_lib.""" - - def test_compute_valid_mask(self): - num_elements = tf.constant(3, tf.int32) - num_valid_elementss = tf.constant((1, 2), tf.int32) - valid_mask = context_rcnn_lib.compute_valid_mask(num_valid_elementss, - num_elements) - expected_valid_mask = tf.constant([[1, 0, 0], [1, 1, 0]], tf.float32) - self.assertAllEqual(valid_mask, expected_valid_mask) - - def test_filter_weight_value(self): - weights = tf.ones((2, 3, 2), tf.float32) * 4 - values = tf.ones((2, 2, 4), tf.float32) - valid_mask = tf.constant([[True, True], [True, False]], tf.bool) - - filtered_weights, filtered_values = context_rcnn_lib.filter_weight_value( - weights, values, valid_mask) - expected_weights = tf.constant([[[4, 4], [4, 4], [4, 4]], - [[4, _NEGATIVE_PADDING_VALUE], - [4, _NEGATIVE_PADDING_VALUE], - [4, _NEGATIVE_PADDING_VALUE]]]) - - expected_values = tf.constant([[[1, 1, 1, 1], [1, 1, 1, 1]], - [[1, 1, 1, 1], [0, 0, 0, 0]]]) - self.assertAllEqual(filtered_weights, expected_weights) - self.assertAllEqual(filtered_values, expected_values) - - # Changes the valid_mask so the results will be different. - valid_mask = tf.constant([[True, True], [False, False]], tf.bool) - - filtered_weights, filtered_values = context_rcnn_lib.filter_weight_value( - weights, values, valid_mask) - expected_weights = tf.constant( - [[[4, 4], [4, 4], [4, 4]], - [[_NEGATIVE_PADDING_VALUE, _NEGATIVE_PADDING_VALUE], - [_NEGATIVE_PADDING_VALUE, _NEGATIVE_PADDING_VALUE], - [_NEGATIVE_PADDING_VALUE, _NEGATIVE_PADDING_VALUE]]]) - - expected_values = tf.constant([[[1, 1, 1, 1], [1, 1, 1, 1]], - [[0, 0, 0, 0], [0, 0, 0, 0]]]) - self.assertAllEqual(filtered_weights, expected_weights) - self.assertAllEqual(filtered_values, expected_values) - - @parameterized.parameters((2, True, True), (2, False, True), - (10, True, False), (10, False, False)) - def test_project_features(self, projection_dimension, is_training, normalize): - features = tf.ones([2, 3, 4], tf.float32) - projected_features = context_rcnn_lib.project_features( - features, - projection_dimension, - is_training=is_training, - normalize=normalize) - - # Makes sure the shape is correct. - self.assertAllEqual(projected_features.shape, [2, 3, projection_dimension]) - - @parameterized.parameters( - (2, 10, 1), - (3, 10, 2), - (4, 20, 3), - (5, 20, 4), - (7, 20, 5), - ) - def test_attention_block(self, bottleneck_dimension, output_dimension, - attention_temperature): - input_features = tf.ones([2, 3, 4], tf.float32) - context_features = tf.ones([2, 2, 3], tf.float32) - valid_mask = tf.constant([[True, True], [False, False]], tf.bool) - box_valid_mask = tf.constant([[True, True, True], [False, False, False]], - tf.bool) - is_training = False - output_features = context_rcnn_lib.attention_block( - input_features, context_features, bottleneck_dimension, - output_dimension, attention_temperature, - keys_values_valid_mask=valid_mask, - queries_valid_mask=box_valid_mask, - is_training=is_training) - - # Makes sure the shape is correct. - self.assertAllEqual(output_features.shape, [2, 3, output_dimension]) - - @parameterized.parameters(True, False) - def test_compute_box_context_attention(self, is_training): - box_features = tf.ones([2 * 3, 4, 4, 4], tf.float32) - context_features = tf.ones([2, 5, 6], tf.float32) - valid_context_size = tf.constant((2, 3), tf.int32) - num_proposals = tf.constant((2, 3), tf.int32) - bottleneck_dimension = 10 - attention_temperature = 1 - attention_features = context_rcnn_lib._compute_box_context_attention( - box_features, num_proposals, context_features, valid_context_size, - bottleneck_dimension, attention_temperature, is_training, - max_num_proposals=3) - # Makes sure the shape is correct. - self.assertAllEqual(attention_features.shape, [2, 3, 1, 1, 4]) - - @parameterized.parameters(True, False) - def test_compute_box_context_attention_with_self_attention(self, is_training): - box_features = tf.ones([2 * 3, 4, 4, 4], tf.float32) - context_features = tf.ones([2, 5, 6], tf.float32) - valid_context_size = tf.constant((2, 3), tf.int32) - num_proposals = tf.constant((2, 3), tf.int32) - bottleneck_dimension = 10 - attention_temperature = 1 - attention_features = context_rcnn_lib._compute_box_context_attention( - box_features, num_proposals, context_features, valid_context_size, - bottleneck_dimension, attention_temperature, is_training, - max_num_proposals=3, - use_self_attention=True) - # Makes sure the shape is correct. - self.assertAllEqual(attention_features.shape, [2, 3, 1, 1, 4]) - - @parameterized.parameters(True, False) - def test_compute_box_context_attention_with_layers_and_heads( - self, is_training): - box_features = tf.ones([2 * 3, 4, 4, 4], tf.float32) - context_features = tf.ones([2, 5, 6], tf.float32) - valid_context_size = tf.constant((2, 3), tf.int32) - num_proposals = tf.constant((2, 3), tf.int32) - bottleneck_dimension = 10 - attention_temperature = 1 - attention_features = context_rcnn_lib._compute_box_context_attention( - box_features, num_proposals, context_features, valid_context_size, - bottleneck_dimension, attention_temperature, is_training, - max_num_proposals=3, - num_attention_layers=3, - num_attention_heads=3) - # Makes sure the shape is correct. - self.assertAllEqual(attention_features.shape, [2, 3, 1, 1, 4]) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/context_rcnn_lib_tf2.py b/research/object_detection/meta_architectures/context_rcnn_lib_tf2.py deleted file mode 100644 index 2cfe026c267..00000000000 --- a/research/object_detection/meta_architectures/context_rcnn_lib_tf2.py +++ /dev/null @@ -1,263 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Library functions for Context R-CNN.""" -import tensorflow as tf - -from object_detection.core import freezable_batch_norm - -# The negative value used in padding the invalid weights. -_NEGATIVE_PADDING_VALUE = -100000 - - -class ContextProjection(tf.keras.layers.Layer): - """Custom layer to do batch normalization and projection.""" - - def __init__(self, projection_dimension, **kwargs): - self.batch_norm = freezable_batch_norm.FreezableBatchNorm( - epsilon=0.001, - center=True, - scale=True, - momentum=0.97, - trainable=True) - self.projection = tf.keras.layers.Dense(units=projection_dimension, - use_bias=True) - self.projection_dimension = projection_dimension - super(ContextProjection, self).__init__(**kwargs) - - def build(self, input_shape): - self.projection.build(input_shape) - self.batch_norm.build(input_shape[:1] + [self.projection_dimension]) - - def call(self, input_features, is_training=False): - return tf.nn.relu6(self.batch_norm(self.projection(input_features), - is_training)) - - -class AttentionBlock(tf.keras.layers.Layer): - """Custom layer to perform all attention.""" - - def __init__(self, bottleneck_dimension, attention_temperature, - output_dimension=None, is_training=False, - name='AttentionBlock', max_num_proposals=100, - **kwargs): - """Constructs an attention block. - - Args: - bottleneck_dimension: A int32 Tensor representing the bottleneck dimension - for intermediate projections. - attention_temperature: A float Tensor. It controls the temperature of the - softmax for weights calculation. The formula for calculation as follows: - weights = exp(weights / temperature) / sum(exp(weights / temperature)) - output_dimension: A int32 Tensor representing the last dimension of the - output feature. - is_training: A boolean Tensor (affecting batch normalization). - name: A string describing what to name the variables in this block. - max_num_proposals: The number of box proposals for each image - **kwargs: Additional keyword arguments. - """ - - self._key_proj = ContextProjection(bottleneck_dimension) - self._val_proj = ContextProjection(bottleneck_dimension) - self._query_proj = ContextProjection(bottleneck_dimension) - self._feature_proj = None - self._attention_temperature = attention_temperature - self._bottleneck_dimension = bottleneck_dimension - self._is_training = is_training - self._output_dimension = output_dimension - self._max_num_proposals = max_num_proposals - if self._output_dimension: - self._feature_proj = ContextProjection(self._output_dimension) - super(AttentionBlock, self).__init__(name=name, **kwargs) - - def build(self, input_shapes): - """Finishes building the attention block. - - Args: - input_shapes: the shape of the primary input box features. - """ - if not self._feature_proj: - self._output_dimension = input_shapes[-1] - self._feature_proj = ContextProjection(self._output_dimension) - - def call(self, box_features, context_features, valid_context_size, - num_proposals): - """Handles a call by performing attention. - - Args: - box_features: A float Tensor of shape [batch_size * input_size, height, - width, num_input_features]. - context_features: A float Tensor of shape [batch_size, context_size, - num_context_features]. - valid_context_size: A int32 Tensor of shape [batch_size]. - num_proposals: A [batch_size] int32 Tensor specifying the number of valid - proposals per image in the batch. - - Returns: - A float Tensor with shape [batch_size, input_size, num_input_features] - containing output features after attention with context features. - """ - - _, context_size, _ = context_features.shape - keys_values_valid_mask = compute_valid_mask( - valid_context_size, context_size) - - total_proposals, height, width, channels = box_features.shape - batch_size = total_proposals // self._max_num_proposals - box_features = tf.reshape( - box_features, - [batch_size, - self._max_num_proposals, - height, - width, - channels]) - - # Average pools over height and width dimension so that the shape of - # box_features becomes [batch_size, max_num_proposals, channels]. - box_features = tf.reduce_mean(box_features, [2, 3]) - - queries_valid_mask = compute_valid_mask(num_proposals, - box_features.shape[1]) - - queries = project_features( - box_features, self._bottleneck_dimension, self._is_training, - self._query_proj, normalize=True) - keys = project_features( - context_features, self._bottleneck_dimension, self._is_training, - self._key_proj, normalize=True) - values = project_features( - context_features, self._bottleneck_dimension, self._is_training, - self._val_proj, normalize=True) - - # masking out any keys which are padding - keys *= tf.cast(keys_values_valid_mask[..., tf.newaxis], keys.dtype) - queries *= tf.cast(queries_valid_mask[..., tf.newaxis], queries.dtype) - - weights = tf.matmul(queries, keys, transpose_b=True) - weights, values = filter_weight_value(weights, values, - keys_values_valid_mask) - weights = tf.nn.softmax(weights / self._attention_temperature) - - features = tf.matmul(weights, values) - output_features = project_features( - features, self._output_dimension, self._is_training, - self._feature_proj, normalize=False) - - output_features = output_features[:, :, tf.newaxis, tf.newaxis, :] - - return output_features - - -def filter_weight_value(weights, values, valid_mask): - """Filters weights and values based on valid_mask. - - _NEGATIVE_PADDING_VALUE will be added to invalid elements in the weights to - avoid their contribution in softmax. 0 will be set for the invalid elements in - the values. - - Args: - weights: A float Tensor of shape [batch_size, input_size, context_size]. - values: A float Tensor of shape [batch_size, context_size, - projected_dimension]. - valid_mask: A boolean Tensor of shape [batch_size, context_size]. True means - valid and False means invalid. - - Returns: - weights: A float Tensor of shape [batch_size, input_size, context_size]. - values: A float Tensor of shape [batch_size, context_size, - projected_dimension]. - - Raises: - ValueError: If shape of doesn't match. - """ - w_batch_size, _, w_context_size = weights.shape - v_batch_size, v_context_size, _ = values.shape - m_batch_size, m_context_size = valid_mask.shape - if w_batch_size != v_batch_size or v_batch_size != m_batch_size: - raise ValueError('Please make sure the first dimension of the input' - ' tensors are the same.') - - if w_context_size != v_context_size: - raise ValueError('Please make sure the third dimension of weights matches' - ' the second dimension of values.') - - if w_context_size != m_context_size: - raise ValueError('Please make sure the third dimension of the weights' - ' matches the second dimension of the valid_mask.') - - valid_mask = valid_mask[..., tf.newaxis] - - # Force the invalid weights to be very negative so it won't contribute to - # the softmax. - weights += tf.transpose( - tf.cast(tf.math.logical_not(valid_mask), weights.dtype) * - _NEGATIVE_PADDING_VALUE, - perm=[0, 2, 1]) - - # Force the invalid values to be 0. - values *= tf.cast(valid_mask, values.dtype) - - return weights, values - - -def project_features(features, bottleneck_dimension, is_training, - layer, normalize=True): - """Projects features to another feature space. - - Args: - features: A float Tensor of shape [batch_size, features_size, - num_features]. - bottleneck_dimension: A int32 Tensor. - is_training: A boolean Tensor (affecting batch normalization). - layer: Contains a custom layer specific to the particular operation - being performed (key, value, query, features) - normalize: A boolean Tensor. If true, the output features will be l2 - normalized on the last dimension. - - Returns: - A float Tensor of shape [batch, features_size, projection_dimension]. - """ - shape_arr = features.shape - batch_size, _, num_features = shape_arr - features = tf.reshape(features, [-1, num_features]) - - projected_features = layer(features, is_training) - - projected_features = tf.reshape(projected_features, - [batch_size, -1, bottleneck_dimension]) - - if normalize: - projected_features = tf.keras.backend.l2_normalize(projected_features, - axis=-1) - - return projected_features - - -def compute_valid_mask(num_valid_elements, num_elements): - """Computes mask of valid entries within padded context feature. - - Args: - num_valid_elements: A int32 Tensor of shape [batch_size]. - num_elements: An int32 Tensor. - - Returns: - A boolean Tensor of the shape [batch_size, num_elements]. True means - valid and False means invalid. - """ - batch_size = num_valid_elements.shape[0] - element_idxs = tf.range(num_elements, dtype=tf.int32) - batch_element_idxs = tf.tile(element_idxs[tf.newaxis, ...], [batch_size, 1]) - num_valid_elements = num_valid_elements[..., tf.newaxis] - valid_mask = tf.less(batch_element_idxs, num_valid_elements) - return valid_mask diff --git a/research/object_detection/meta_architectures/context_rcnn_lib_tf2_test.py b/research/object_detection/meta_architectures/context_rcnn_lib_tf2_test.py deleted file mode 100644 index 11d1d59aa37..00000000000 --- a/research/object_detection/meta_architectures/context_rcnn_lib_tf2_test.py +++ /dev/null @@ -1,121 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for context_rcnn_lib.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -from absl.testing import parameterized -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import context_rcnn_lib_tf2 as context_rcnn_lib -from object_detection.utils import test_case -from object_detection.utils import tf_version - -_NEGATIVE_PADDING_VALUE = -100000 - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ContextRcnnLibTest(parameterized.TestCase, test_case.TestCase): - """Tests for the functions in context_rcnn_lib.""" - - def test_compute_valid_mask(self): - num_elements = tf.constant(3, tf.int32) - num_valid_elementss = tf.constant((1, 2), tf.int32) - valid_mask = context_rcnn_lib.compute_valid_mask(num_valid_elementss, - num_elements) - expected_valid_mask = tf.constant([[1, 0, 0], [1, 1, 0]], tf.float32) - self.assertAllEqual(valid_mask, expected_valid_mask) - - def test_filter_weight_value(self): - weights = tf.ones((2, 3, 2), tf.float32) * 4 - values = tf.ones((2, 2, 4), tf.float32) - valid_mask = tf.constant([[True, True], [True, False]], tf.bool) - - filtered_weights, filtered_values = context_rcnn_lib.filter_weight_value( - weights, values, valid_mask) - expected_weights = tf.constant([[[4, 4], [4, 4], [4, 4]], - [[4, _NEGATIVE_PADDING_VALUE + 4], - [4, _NEGATIVE_PADDING_VALUE + 4], - [4, _NEGATIVE_PADDING_VALUE + 4]]]) - - expected_values = tf.constant([[[1, 1, 1, 1], [1, 1, 1, 1]], - [[1, 1, 1, 1], [0, 0, 0, 0]]]) - self.assertAllEqual(filtered_weights, expected_weights) - self.assertAllEqual(filtered_values, expected_values) - - # Changes the valid_mask so the results will be different. - valid_mask = tf.constant([[True, True], [False, False]], tf.bool) - - filtered_weights, filtered_values = context_rcnn_lib.filter_weight_value( - weights, values, valid_mask) - expected_weights = tf.constant( - [[[4, 4], [4, 4], [4, 4]], - [[_NEGATIVE_PADDING_VALUE + 4, _NEGATIVE_PADDING_VALUE + 4], - [_NEGATIVE_PADDING_VALUE + 4, _NEGATIVE_PADDING_VALUE + 4], - [_NEGATIVE_PADDING_VALUE + 4, _NEGATIVE_PADDING_VALUE + 4]]]) - - expected_values = tf.constant([[[1, 1, 1, 1], [1, 1, 1, 1]], - [[0, 0, 0, 0], [0, 0, 0, 0]]]) - self.assertAllEqual(filtered_weights, expected_weights) - self.assertAllEqual(filtered_values, expected_values) - - @parameterized.parameters((2, True, True), (2, False, True), - (10, True, False), (10, False, False)) - def test_project_features(self, projection_dimension, is_training, normalize): - features = tf.ones([2, 3, 4], tf.float32) - projected_features = context_rcnn_lib.project_features( - features, - projection_dimension, - is_training, - context_rcnn_lib.ContextProjection(projection_dimension), - normalize=normalize) - - # Makes sure the shape is correct. - self.assertAllEqual(projected_features.shape, [2, 3, projection_dimension]) - - @parameterized.parameters( - (2, 10, 1), - (3, 10, 2), - (4, None, 3), - (5, 20, 4), - (7, None, 5), - ) - def test_attention_block(self, bottleneck_dimension, output_dimension, - attention_temperature): - input_features = tf.ones([2 * 8, 3, 3, 3], tf.float32) - context_features = tf.ones([2, 20, 10], tf.float32) - num_proposals = tf.convert_to_tensor([6, 3]) - attention_block = context_rcnn_lib.AttentionBlock( - bottleneck_dimension, - attention_temperature, - output_dimension=output_dimension, - is_training=False, - max_num_proposals=8) - valid_context_size = tf.random_uniform((2,), - minval=0, - maxval=10, - dtype=tf.int32) - output_features = attention_block(input_features, context_features, - valid_context_size, num_proposals) - - # Makes sure the shape is correct. - self.assertAllEqual(output_features.shape, - [2, 8, 1, 1, (output_dimension or 3)]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/context_rcnn_meta_arch.py b/research/object_detection/meta_architectures/context_rcnn_meta_arch.py deleted file mode 100644 index dc7cad1e474..00000000000 --- a/research/object_detection/meta_architectures/context_rcnn_meta_arch.py +++ /dev/null @@ -1,624 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Context R-CNN meta-architecture definition. - -This adds the ability to use attention into contextual features within the -Faster R-CNN object detection framework to improve object detection performance. -See https://arxiv.org/abs/1912.03538 for more information. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools - -import tensorflow.compat.v1 as tf - -from object_detection.core import box_predictor -from object_detection.core import standard_fields as fields -from object_detection.meta_architectures import context_rcnn_lib -from object_detection.meta_architectures import context_rcnn_lib_tf2 -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.protos import faster_rcnn_pb2 -from object_detection.utils import ops -from object_detection.utils import tf_version - -_UNINITIALIZED_FEATURE_EXTRACTOR = '__uninitialized__' - - -class ContextRCNNMetaArch(faster_rcnn_meta_arch.FasterRCNNMetaArch): - """Context R-CNN Meta-architecture definition.""" - - def __init__(self, - is_training, - num_classes, - image_resizer_fn, - feature_extractor, - number_of_stages, - first_stage_anchor_generator, - first_stage_target_assigner, - first_stage_atrous_rate, - first_stage_box_predictor_arg_scope_fn, - first_stage_box_predictor_kernel_size, - first_stage_box_predictor_depth, - first_stage_minibatch_size, - first_stage_sampler, - first_stage_non_max_suppression_fn, - first_stage_max_proposals, - first_stage_localization_loss_weight, - first_stage_objectness_loss_weight, - crop_and_resize_fn, - initial_crop_size, - maxpool_kernel_size, - maxpool_stride, - second_stage_target_assigner, - second_stage_mask_rcnn_box_predictor, - second_stage_batch_size, - second_stage_sampler, - second_stage_non_max_suppression_fn, - second_stage_score_conversion_fn, - second_stage_localization_loss_weight, - second_stage_classification_loss_weight, - second_stage_classification_loss, - second_stage_mask_prediction_loss_weight=1.0, - hard_example_miner=None, - parallel_iterations=16, - add_summaries=True, - clip_anchors_to_image=False, - use_static_shapes=False, - resize_masks=True, - freeze_batchnorm=False, - return_raw_detections_during_predict=False, - output_final_box_features=False, - output_final_box_rpn_features=False, - attention_bottleneck_dimension=None, - attention_temperature=None, - use_self_attention=False, - use_long_term_attention=True, - self_attention_in_sequence=False, - num_attention_heads=1, - num_attention_layers=1, - attention_position=( - faster_rcnn_pb2.AttentionPosition.POST_BOX_CLASSIFIER) - ): - """ContextRCNNMetaArch Constructor. - - Args: - is_training: A boolean indicating whether the training version of the - computation graph should be constructed. - num_classes: Number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - image_resizer_fn: A callable for image resizing. This callable - takes a rank-3 image tensor of shape [height, width, channels] - (corresponding to a single image), an optional rank-3 instance mask - tensor of shape [num_masks, height, width] and returns a resized rank-3 - image tensor, a resized mask tensor if one was provided in the input. In - addition this callable must also return a 1-D tensor of the form - [height, width, channels] containing the size of the true image, as the - image resizer can perform zero padding. See protos/image_resizer.proto. - feature_extractor: A FasterRCNNFeatureExtractor object. - number_of_stages: An integer values taking values in {1, 2, 3}. If - 1, the function will construct only the Region Proposal Network (RPN) - part of the model. If 2, the function will perform box refinement and - other auxiliary predictions all in the second stage. If 3, it will - extract features from refined boxes and perform the auxiliary - predictions on the non-maximum suppressed refined boxes. - If is_training is true and the value of number_of_stages is 3, it is - reduced to 2 since all the model heads are trained in parallel in second - stage during training. - first_stage_anchor_generator: An anchor_generator.AnchorGenerator object - (note that currently we only support - grid_anchor_generator.GridAnchorGenerator objects) - first_stage_target_assigner: Target assigner to use for first stage of - Faster R-CNN (RPN). - first_stage_atrous_rate: A single integer indicating the atrous rate for - the single convolution op which is applied to the `rpn_features_to_crop` - tensor to obtain a tensor to be used for box prediction. Some feature - extractors optionally allow for producing feature maps computed at - denser resolutions. The atrous rate is used to compensate for the - denser feature maps by using an effectively larger receptive field. - (This should typically be set to 1). - first_stage_box_predictor_arg_scope_fn: Either a - Keras layer hyperparams object or a function to construct tf-slim - arg_scope for conv2d, separable_conv2d and fully_connected ops. Used - for the RPN box predictor. If it is a keras hyperparams object the - RPN box predictor will be a Keras model. If it is a function to - construct an arg scope it will be a tf-slim box predictor. - first_stage_box_predictor_kernel_size: Kernel size to use for the - convolution op just prior to RPN box predictions. - first_stage_box_predictor_depth: Output depth for the convolution op - just prior to RPN box predictions. - first_stage_minibatch_size: The "batch size" to use for computing the - objectness and location loss of the region proposal network. This - "batch size" refers to the number of anchors selected as contributing - to the loss function for any given image within the image batch and is - only called "batch_size" due to terminology from the Faster R-CNN paper. - first_stage_sampler: Sampler to use for first stage loss (RPN loss). - first_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression - callable that takes `boxes`, `scores` and optional `clip_window`(with - all other inputs already set) and returns a dictionary containing - tensors with keys: `detection_boxes`, `detection_scores`, - `detection_classes`, `num_detections`. This is used to perform non max - suppression on the boxes predicted by the Region Proposal Network - (RPN). - See `post_processing.batch_multiclass_non_max_suppression` for the type - and shape of these tensors. - first_stage_max_proposals: Maximum number of boxes to retain after - performing Non-Max Suppression (NMS) on the boxes predicted by the - Region Proposal Network (RPN). - first_stage_localization_loss_weight: A float - first_stage_objectness_loss_weight: A float - crop_and_resize_fn: A differentiable resampler to use for cropping RPN - proposal features. - initial_crop_size: A single integer indicating the output size - (width and height are set to be the same) of the initial bilinear - interpolation based cropping during ROI pooling. - maxpool_kernel_size: A single integer indicating the kernel size of the - max pool op on the cropped feature map during ROI pooling. - maxpool_stride: A single integer indicating the stride of the max pool - op on the cropped feature map during ROI pooling. - second_stage_target_assigner: Target assigner to use for second stage of - Faster R-CNN. If the model is configured with multiple prediction heads, - this target assigner is used to generate targets for all heads (with the - correct `unmatched_class_label`). - second_stage_mask_rcnn_box_predictor: Mask R-CNN box predictor to use for - the second stage. - second_stage_batch_size: The batch size used for computing the - classification and refined location loss of the box classifier. This - "batch size" refers to the number of proposals selected as contributing - to the loss function for any given image within the image batch and is - only called "batch_size" due to terminology from the Faster R-CNN paper. - second_stage_sampler: Sampler to use for second stage loss (box - classifier loss). - second_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression - callable that takes `boxes`, `scores`, optional `clip_window` and - optional (kwarg) `mask` inputs (with all other inputs already set) - and returns a dictionary containing tensors with keys: - `detection_boxes`, `detection_scores`, `detection_classes`, - `num_detections`, and (optionally) `detection_masks`. See - `post_processing.batch_multiclass_non_max_suppression` for the type and - shape of these tensors. - second_stage_score_conversion_fn: Callable elementwise nonlinearity - (that takes tensors as inputs and returns tensors). This is usually - used to convert logits to probabilities. - second_stage_localization_loss_weight: A float indicating the scale factor - for second stage localization loss. - second_stage_classification_loss_weight: A float indicating the scale - factor for second stage classification loss. - second_stage_classification_loss: Classification loss used by the second - stage classifier. Either losses.WeightedSigmoidClassificationLoss or - losses.WeightedSoftmaxClassificationLoss. - second_stage_mask_prediction_loss_weight: A float indicating the scale - factor for second stage mask prediction loss. This is applicable only if - second stage box predictor is configured to predict masks. - hard_example_miner: A losses.HardExampleMiner object (can be None). - parallel_iterations: (Optional) The number of iterations allowed to run - in parallel for calls to tf.map_fn. - add_summaries: boolean (default: True) controlling whether summary ops - should be added to tensorflow graph. - clip_anchors_to_image: Normally, anchors generated for a given image size - are pruned during training if they lie outside the image window. This - option clips the anchors to be within the image instead of pruning. - use_static_shapes: If True, uses implementation of ops with static shape - guarantees. - resize_masks: Indicates whether the masks presend in the groundtruth - should be resized in the model with `image_resizer_fn` - freeze_batchnorm: Whether to freeze batch norm parameters in the first - stage box predictor during training or not. When training with a small - batch size (e.g. 1), it is desirable to freeze batch norm update and - use pretrained batch norm params. - return_raw_detections_during_predict: Whether to return raw detection - boxes in the predict() method. These are decoded boxes that have not - been through postprocessing (i.e. NMS). Default False. - output_final_box_features: Whether to output final box features. If true, - it crops the feature map based on the final box prediction and returns - it in the output dict as detection_features. - output_final_box_rpn_features: Whether to output rpn box features. If - true, it crops the rpn feature map based on the final box prediction and - returns it in the output dict as detection_features. - attention_bottleneck_dimension: A single integer. The bottleneck feature - dimension of the attention block. - attention_temperature: A single float. The attention temperature. - use_self_attention: Whether to use self-attention within the box features - in the current frame. - use_long_term_attention: Whether to use attention into the context - features. - self_attention_in_sequence: Whether self attention and long term attention - are in sequence or parallel. - num_attention_heads: The number of attention heads to use. - num_attention_layers: The number of attention layers to use. - attention_position: Whether attention should occur post rpn or post - box classifier. Options are specified in the faster rcnn proto, - default is post box classifier. - - Raises: - ValueError: If `second_stage_batch_size` > `first_stage_max_proposals` at - training time. - ValueError: If first_stage_anchor_generator is not of type - grid_anchor_generator.GridAnchorGenerator. - """ - super(ContextRCNNMetaArch, self).__init__( - is_training, - num_classes, - image_resizer_fn, - feature_extractor, - number_of_stages, - first_stage_anchor_generator, - first_stage_target_assigner, - first_stage_atrous_rate, - first_stage_box_predictor_arg_scope_fn, - first_stage_box_predictor_kernel_size, - first_stage_box_predictor_depth, - first_stage_minibatch_size, - first_stage_sampler, - first_stage_non_max_suppression_fn, - first_stage_max_proposals, - first_stage_localization_loss_weight, - first_stage_objectness_loss_weight, - crop_and_resize_fn, - initial_crop_size, - maxpool_kernel_size, - maxpool_stride, - second_stage_target_assigner, - second_stage_mask_rcnn_box_predictor, - second_stage_batch_size, - second_stage_sampler, - second_stage_non_max_suppression_fn, - second_stage_score_conversion_fn, - second_stage_localization_loss_weight, - second_stage_classification_loss_weight, - second_stage_classification_loss, - second_stage_mask_prediction_loss_weight=( - second_stage_mask_prediction_loss_weight), - hard_example_miner=hard_example_miner, - parallel_iterations=parallel_iterations, - add_summaries=add_summaries, - clip_anchors_to_image=clip_anchors_to_image, - use_static_shapes=use_static_shapes, - resize_masks=resize_masks, - freeze_batchnorm=freeze_batchnorm, - return_raw_detections_during_predict=( - return_raw_detections_during_predict), - output_final_box_features=output_final_box_features, - output_final_box_rpn_features=output_final_box_rpn_features) - - self._attention_position = attention_position - - if tf_version.is_tf1(): - self._context_feature_extract_fn = functools.partial( - context_rcnn_lib._compute_box_context_attention, - bottleneck_dimension=attention_bottleneck_dimension, - attention_temperature=attention_temperature, - is_training=is_training, - max_num_proposals=self.max_num_proposals, - use_self_attention=use_self_attention, - use_long_term_attention=use_long_term_attention, - self_attention_in_sequence=self_attention_in_sequence, - num_attention_heads=num_attention_heads, - num_attention_layers=num_attention_layers) - else: - if use_self_attention: - raise NotImplementedError - if self_attention_in_sequence: - raise NotImplementedError - if not use_long_term_attention: - raise NotImplementedError - if num_attention_heads > 1: - raise NotImplementedError - if num_attention_layers > 1: - raise NotImplementedError - - self._context_feature_extract_fn = context_rcnn_lib_tf2.AttentionBlock( - bottleneck_dimension=attention_bottleneck_dimension, - attention_temperature=attention_temperature, - is_training=is_training, - max_num_proposals=self.max_num_proposals) - - @staticmethod - def get_side_inputs(features): - """Overrides the get_side_inputs function in the base class. - - This function returns context_features and valid_context_size, which will be - used in the _compute_second_stage_input_feature_maps function. - - Args: - features: A dictionary of tensors. - - Returns: - A dictionary of tensors contains context_features and valid_context_size. - - Raises: - ValueError: If context_features or valid_context_size is not in the - features. - """ - if (fields.InputDataFields.context_features not in features or - fields.InputDataFields.valid_context_size not in features): - raise ValueError( - 'Please make sure context_features and valid_context_size are in the ' - 'features') - - return { - fields.InputDataFields.context_features: - features[fields.InputDataFields.context_features], - fields.InputDataFields.valid_context_size: - features[fields.InputDataFields.valid_context_size] - } - - def _predict_second_stage(self, rpn_box_encodings, - rpn_objectness_predictions_with_background, - rpn_features_to_crop, anchors, image_shape, - true_image_shapes, **side_inputs): - """Predicts the output tensors from second stage of Faster R-CNN. - - Args: - rpn_box_encodings: 3-D float tensor of shape - [batch_size, num_valid_anchors, self._box_coder.code_size] containing - predicted boxes. - rpn_objectness_predictions_with_background: 2-D float tensor of shape - [batch_size, num_valid_anchors, 2] containing class - predictions (logits) for each of the anchors. Note that this - tensor *includes* background class predictions (at class index 0). - rpn_features_to_crop: A list of 4-D float32 or bfloat16 tensor with shape - [batch_size, height_i, width_i, depth] representing image features to - crop using the proposal boxes predicted by the RPN. - anchors: 2-D float tensor of shape - [num_anchors, self._box_coder.code_size]. - image_shape: A 1D int32 tensors of size [4] containing the image shape. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - **side_inputs: additional tensors that are required by the network. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) refined_box_encodings: a 3-D float32 tensor with shape - [total_num_proposals, num_classes, self._box_coder.code_size] - representing predicted (final) refined box encodings, where - total_num_proposals=batch_size*self._max_num_proposals. If using a - shared box across classes the shape will instead be - [total_num_proposals, 1, self._box_coder.code_size]. - 2) class_predictions_with_background: a 3-D float32 tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors, where - total_num_proposals=batch_size*self._max_num_proposals. - Note that this tensor *includes* background class predictions - (at class index 0). - 3) num_proposals: An int32 tensor of shape [batch_size] representing the - number of proposals generated by the RPN. `num_proposals` allows us - to keep track of which entries are to be treated as zero paddings and - which are not since we always pad the number of proposals to be - `self.max_num_proposals` for each image. - 4) proposal_boxes: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes in absolute coordinates. - 5) proposal_boxes_normalized: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing decoded proposal - bounding boxes in normalized coordinates. Can be used to override the - boxes proposed by the RPN, thus enabling one to extract features and - get box classification and prediction for externally selected areas - of the image. - 6) box_classifier_features: a 4-D float32/bfloat16 tensor - representing the features for each proposal. - If self._return_raw_detections_during_predict is True, the dictionary - will also contain: - 7) raw_detection_boxes: a 4-D float32 tensor with shape - [batch_size, self.max_num_proposals, num_classes, 4] in normalized - coordinates. - 8) raw_detection_feature_map_indices: a 3-D int32 tensor with shape - [batch_size, self.max_num_proposals, num_classes]. - """ - proposal_boxes_normalized, num_proposals = self._proposal_postprocess( - rpn_box_encodings, rpn_objectness_predictions_with_background, anchors, - image_shape, true_image_shapes) - - prediction_dict = self._box_prediction(rpn_features_to_crop, - proposal_boxes_normalized, - image_shape, true_image_shapes, - num_proposals, - **side_inputs) - prediction_dict['num_proposals'] = num_proposals - return prediction_dict - - def _box_prediction(self, rpn_features_to_crop, proposal_boxes_normalized, - image_shape, true_image_shapes, num_proposals, - **side_inputs): - """Predicts the output tensors from second stage of Faster R-CNN. - - Args: - rpn_features_to_crop: A list 4-D float32 or bfloat16 tensor with shape - [batch_size, height_i, width_i, depth] representing image features to - crop using the proposal boxes predicted by the RPN. - proposal_boxes_normalized: A float tensor with shape [batch_size, - max_num_proposals, 4] representing the (potentially zero padded) - proposal boxes for all images in the batch. These boxes are represented - as normalized coordinates. - image_shape: A 1D int32 tensors of size [4] containing the image shape. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - num_proposals: The number of valid box proposals. - **side_inputs: additional tensors that are required by the network. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) refined_box_encodings: a 3-D float32 tensor with shape - [total_num_proposals, num_classes, self._box_coder.code_size] - representing predicted (final) refined box encodings, where - total_num_proposals=batch_size*self._max_num_proposals. If using a - shared box across classes the shape will instead be - [total_num_proposals, 1, self._box_coder.code_size]. - 2) class_predictions_with_background: a 3-D float32 tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors, where - total_num_proposals=batch_size*self._max_num_proposals. - Note that this tensor *includes* background class predictions - (at class index 0). - 3) proposal_boxes: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes in absolute coordinates. - 4) proposal_boxes_normalized: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing decoded proposal - bounding boxes in normalized coordinates. Can be used to override the - boxes proposed by the RPN, thus enabling one to extract features and - get box classification and prediction for externally selected areas - of the image. - 5) box_classifier_features: a 4-D float32/bfloat16 tensor - representing the features for each proposal. - If self._return_raw_detections_during_predict is True, the dictionary - will also contain: - 6) raw_detection_boxes: a 4-D float32 tensor with shape - [batch_size, self.max_num_proposals, num_classes, 4] in normalized - coordinates. - 7) raw_detection_feature_map_indices: a 3-D int32 tensor with shape - [batch_size, self.max_num_proposals, num_classes]. - 8) final_anchors: a 3-D float tensor of shape [batch_size, - self.max_num_proposals, 4] containing the reference anchors for raw - detection boxes in normalized coordinates. - """ - flattened_proposal_feature_maps = ( - self._compute_second_stage_input_feature_maps( - rpn_features_to_crop, proposal_boxes_normalized, - image_shape, num_proposals, **side_inputs)) - - box_classifier_features = self._extract_box_classifier_features( - flattened_proposal_feature_maps, num_proposals, **side_inputs) - - if self._mask_rcnn_box_predictor.is_keras_model: - box_predictions = self._mask_rcnn_box_predictor( - [box_classifier_features], - prediction_stage=2) - else: - box_predictions = self._mask_rcnn_box_predictor.predict( - [box_classifier_features], - num_predictions_per_location=[1], - scope=self.second_stage_box_predictor_scope, - prediction_stage=2) - - refined_box_encodings = tf.squeeze( - box_predictions[box_predictor.BOX_ENCODINGS], - axis=1, name='all_refined_box_encodings') - class_predictions_with_background = tf.squeeze( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1, name='all_class_predictions_with_background') - - absolute_proposal_boxes = ops.normalized_to_image_coordinates( - proposal_boxes_normalized, image_shape, self._parallel_iterations) - - prediction_dict = { - 'refined_box_encodings': tf.cast(refined_box_encodings, - dtype=tf.float32), - 'class_predictions_with_background': - tf.cast(class_predictions_with_background, dtype=tf.float32), - 'proposal_boxes': absolute_proposal_boxes, - 'box_classifier_features': box_classifier_features, - 'proposal_boxes_normalized': proposal_boxes_normalized, - 'final_anchors': proposal_boxes_normalized - } - - if self._return_raw_detections_during_predict: - prediction_dict.update(self._raw_detections_and_feature_map_inds( - refined_box_encodings, absolute_proposal_boxes, true_image_shapes)) - - return prediction_dict - - def _compute_second_stage_input_feature_maps(self, features_to_crop, - proposal_boxes_normalized, - image_shape, - num_proposals, - context_features, - valid_context_size): - """Crops to a set of proposals from the feature map for a batch of images. - - This function overrides the one in the FasterRCNNMetaArch. Aside from - cropping and resizing the feature maps, which is done in the parent class, - it adds context attention features to the box features. - - Args: - features_to_crop: A float32 Tensor with shape [batch_size, height, width, - depth] - proposal_boxes_normalized: A float32 Tensor with shape [batch_size, - num_proposals, box_code_size] containing proposal boxes in normalized - coordinates. - image_shape: A 1D int32 tensors of size [4] containing the image shape. - num_proposals: The number of valid box proposals. - context_features: A float Tensor of shape [batch_size, context_size, - num_context_features]. - valid_context_size: A int32 Tensor of shape [batch_size]. - - Returns: - A float32 Tensor with shape [K, new_height, new_width, depth]. - """ - del image_shape - box_features = self._crop_and_resize_fn( - features_to_crop, proposal_boxes_normalized, None, - [self._initial_crop_size, self._initial_crop_size]) - - flattened_box_features = self._flatten_first_two_dimensions(box_features) - - flattened_box_features = self._maxpool_layer(flattened_box_features) - - if self._attention_position == ( - faster_rcnn_pb2.AttentionPosition.POST_RPN): - attention_features = self._context_feature_extract_fn( - box_features=flattened_box_features, - num_proposals=num_proposals, - context_features=context_features, - valid_context_size=valid_context_size) - - # Adds box features with attention features. - flattened_box_features += self._flatten_first_two_dimensions( - attention_features) - - return flattened_box_features - - def _extract_box_classifier_features( - self, flattened_box_features, num_proposals, context_features, - valid_context_size, - attention_position=( - faster_rcnn_pb2.AttentionPosition.POST_BOX_CLASSIFIER)): - if self._feature_extractor_for_box_classifier_features == ( - _UNINITIALIZED_FEATURE_EXTRACTOR): - self._feature_extractor_for_box_classifier_features = ( - self._feature_extractor.get_box_classifier_feature_extractor_model( - name=self.second_stage_feature_extractor_scope)) - - if self._feature_extractor_for_box_classifier_features: - box_classifier_features = ( - self._feature_extractor_for_box_classifier_features( - flattened_box_features)) - else: - box_classifier_features = ( - self._feature_extractor.extract_box_classifier_features( - flattened_box_features, - scope=self.second_stage_feature_extractor_scope)) - - if self._attention_position == ( - faster_rcnn_pb2.AttentionPosition.POST_BOX_CLASSIFIER): - attention_features = self._context_feature_extract_fn( - box_features=box_classifier_features, - num_proposals=num_proposals, - context_features=context_features, - valid_context_size=valid_context_size) - - # Adds box features with attention features. - box_classifier_features += self._flatten_first_two_dimensions( - attention_features) - - return box_classifier_features diff --git a/research/object_detection/meta_architectures/context_rcnn_meta_arch_test.py b/research/object_detection/meta_architectures/context_rcnn_meta_arch_test.py deleted file mode 100644 index 7ee8209c7d4..00000000000 --- a/research/object_detection/meta_architectures/context_rcnn_meta_arch_test.py +++ /dev/null @@ -1,540 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.meta_architectures.context_meta_arch.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -import unittest -from unittest import mock # pylint: disable=g-importing-member -from absl.testing import parameterized -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from google.protobuf import text_format - -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.builders import post_processing_builder -from object_detection.core import balanced_positive_negative_sampler as sampler -from object_detection.core import losses -from object_detection.core import post_processing -from object_detection.core import standard_fields as fields -from object_detection.core import target_assigner -from object_detection.meta_architectures import context_rcnn_meta_arch -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.protos import box_predictor_pb2 -from object_detection.protos import hyperparams_pb2 -from object_detection.protos import post_processing_pb2 -from object_detection.utils import spatial_transform_ops as spatial_ops -from object_detection.utils import test_case -from object_detection.utils import test_utils -from object_detection.utils import tf_version - - -class FakeFasterRCNNFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Fake feature extractor to use in tests.""" - - def __init__(self): - super(FakeFasterRCNNFeatureExtractor, self).__init__( - is_training=False, - first_stage_features_stride=32, - reuse_weights=None, - weight_decay=0.0) - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def _extract_proposal_features(self, preprocessed_inputs, scope): - with tf.variable_scope('mock_model'): - proposal_features = 0 * slim.conv2d( - preprocessed_inputs, num_outputs=3, kernel_size=1, scope='layer1') - return proposal_features, {} - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - with tf.variable_scope('mock_model'): - return 0 * slim.conv2d( - proposal_feature_maps, num_outputs=3, kernel_size=1, scope='layer2') - - -class FakeFasterRCNNKerasFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor): - """Fake feature extractor to use in tests.""" - - def __init__(self): - super(FakeFasterRCNNKerasFeatureExtractor, self).__init__( - is_training=False, first_stage_features_stride=32, weight_decay=0.0) - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def get_proposal_feature_extractor_model(self, name): - - class ProposalFeatureExtractor(tf.keras.Model): - """Dummy proposal feature extraction.""" - - def __init__(self, name): - super(ProposalFeatureExtractor, self).__init__(name=name) - self.conv = None - - def build(self, input_shape): - self.conv = tf.keras.layers.Conv2D( - 3, kernel_size=1, padding='SAME', name='layer1') - - def call(self, inputs): - return self.conv(inputs) - - return ProposalFeatureExtractor(name=name) - - def get_box_classifier_feature_extractor_model(self, name): - return tf.keras.Sequential([ - tf.keras.layers.Conv2D( - 3, kernel_size=1, padding='SAME', name=name + '_layer2') - ]) - - -class ContextRCNNMetaArchTest(test_case.TestCase, parameterized.TestCase): - - def _get_model(self, box_predictor, **common_kwargs): - return context_rcnn_meta_arch.ContextRCNNMetaArch( - initial_crop_size=3, - maxpool_kernel_size=1, - maxpool_stride=1, - second_stage_mask_rcnn_box_predictor=box_predictor, - attention_bottleneck_dimension=10, - attention_temperature=0.2, - **common_kwargs) - - def _build_arg_scope_with_hyperparams(self, hyperparams_text_proto, - is_training): - hyperparams = hyperparams_pb2.Hyperparams() - text_format.Merge(hyperparams_text_proto, hyperparams) - return hyperparams_builder.build(hyperparams, is_training=is_training) - - def _build_keras_layer_hyperparams(self, hyperparams_text_proto): - hyperparams = hyperparams_pb2.Hyperparams() - text_format.Merge(hyperparams_text_proto, hyperparams) - return hyperparams_builder.KerasLayerHyperparams(hyperparams) - - def _get_second_stage_box_predictor_text_proto(self, - share_box_across_classes=False - ): - share_box_field = 'true' if share_box_across_classes else 'false' - box_predictor_text_proto = """ - mask_rcnn_box_predictor {{ - fc_hyperparams {{ - op: FC - activation: NONE - regularizer {{ - l2_regularizer {{ - weight: 0.0005 - }} - }} - initializer {{ - variance_scaling_initializer {{ - factor: 1.0 - uniform: true - mode: FAN_AVG - }} - }} - }} - share_box_across_classes: {share_box_across_classes} - }} - """.format(share_box_across_classes=share_box_field) - return box_predictor_text_proto - - def _get_box_classifier_features_shape(self, - image_size, - batch_size, - max_num_proposals, - initial_crop_size, - maxpool_stride, - num_features): - return (batch_size * max_num_proposals, - initial_crop_size/maxpool_stride, - initial_crop_size/maxpool_stride, - num_features) - - def _get_second_stage_box_predictor(self, - num_classes, - is_training, - predict_masks, - masks_are_class_agnostic, - share_box_across_classes=False, - use_keras=False): - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge( - self._get_second_stage_box_predictor_text_proto( - share_box_across_classes), box_predictor_proto) - if predict_masks: - text_format.Merge( - self._add_mask_to_second_stage_box_predictor_text_proto( - masks_are_class_agnostic), box_predictor_proto) - - if use_keras: - return box_predictor_builder.build_keras( - hyperparams_builder.KerasLayerHyperparams, - inplace_batchnorm_update=False, - freeze_batchnorm=False, - box_predictor_config=box_predictor_proto, - num_classes=num_classes, - num_predictions_per_location_list=None, - is_training=is_training) - else: - return box_predictor_builder.build( - hyperparams_builder.build, - box_predictor_proto, - num_classes=num_classes, - is_training=is_training) - - def _build_model(self, - is_training, - number_of_stages, - second_stage_batch_size, - first_stage_max_proposals=8, - num_classes=2, - hard_mining=False, - softmax_second_stage_classification_loss=True, - predict_masks=False, - pad_to_max_dimension=None, - masks_are_class_agnostic=False, - use_matmul_crop_and_resize=False, - clip_anchors_to_image=False, - use_matmul_gather_in_matcher=False, - use_static_shapes=False, - calibration_mapping_value=None, - share_box_across_classes=False, - return_raw_detections_during_predict=False): - use_keras = tf_version.is_tf2() - def image_resizer_fn(image, masks=None): - """Fake image resizer function.""" - resized_inputs = [] - resized_image = tf.identity(image) - if pad_to_max_dimension is not None: - resized_image = tf.image.pad_to_bounding_box(image, 0, 0, - pad_to_max_dimension, - pad_to_max_dimension) - resized_inputs.append(resized_image) - if masks is not None: - resized_masks = tf.identity(masks) - if pad_to_max_dimension is not None: - resized_masks = tf.image.pad_to_bounding_box( - tf.transpose(masks, [1, 2, 0]), 0, 0, pad_to_max_dimension, - pad_to_max_dimension) - resized_masks = tf.transpose(resized_masks, [2, 0, 1]) - resized_inputs.append(resized_masks) - resized_inputs.append(tf.shape(image)) - return resized_inputs - - # anchors in this test are designed so that a subset of anchors are inside - # the image and a subset of anchors are outside. - first_stage_anchor_scales = (0.001, 0.005, 0.1) - first_stage_anchor_aspect_ratios = (0.5, 1.0, 2.0) - first_stage_anchor_strides = (1, 1) - first_stage_anchor_generator = grid_anchor_generator.GridAnchorGenerator( - first_stage_anchor_scales, - first_stage_anchor_aspect_ratios, - anchor_stride=first_stage_anchor_strides) - first_stage_target_assigner = target_assigner.create_target_assigner( - 'FasterRCNN', - 'proposal', - use_matmul_gather=use_matmul_gather_in_matcher) - - if use_keras: - fake_feature_extractor = FakeFasterRCNNKerasFeatureExtractor() - else: - fake_feature_extractor = FakeFasterRCNNFeatureExtractor() - - first_stage_box_predictor_hyperparams_text_proto = """ - op: CONV - activation: RELU - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - } - } - """ - if use_keras: - first_stage_box_predictor_arg_scope_fn = ( - self._build_keras_layer_hyperparams( - first_stage_box_predictor_hyperparams_text_proto)) - else: - first_stage_box_predictor_arg_scope_fn = ( - self._build_arg_scope_with_hyperparams( - first_stage_box_predictor_hyperparams_text_proto, is_training)) - - first_stage_box_predictor_kernel_size = 3 - first_stage_atrous_rate = 1 - first_stage_box_predictor_depth = 512 - first_stage_minibatch_size = 3 - first_stage_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=0.5, is_static=use_static_shapes) - - first_stage_nms_score_threshold = -1.0 - first_stage_nms_iou_threshold = 1.0 - first_stage_non_max_suppression_fn = functools.partial( - post_processing.batch_multiclass_non_max_suppression, - score_thresh=first_stage_nms_score_threshold, - iou_thresh=first_stage_nms_iou_threshold, - max_size_per_class=first_stage_max_proposals, - max_total_size=first_stage_max_proposals, - use_static_shapes=use_static_shapes) - - first_stage_localization_loss_weight = 1.0 - first_stage_objectness_loss_weight = 1.0 - - post_processing_config = post_processing_pb2.PostProcessing() - post_processing_text_proto = """ - score_converter: IDENTITY - batch_non_max_suppression { - score_threshold: -20.0 - iou_threshold: 1.0 - max_detections_per_class: 5 - max_total_detections: 5 - use_static_shapes: """ + '{}'.format(use_static_shapes) + """ - } - """ - if calibration_mapping_value: - calibration_text_proto = """ - calibration_config { - function_approximation { - x_y_pairs { - x_y_pair { - x: 0.0 - y: %f - } - x_y_pair { - x: 1.0 - y: %f - }}}}""" % (calibration_mapping_value, calibration_mapping_value) - post_processing_text_proto = ( - post_processing_text_proto + ' ' + calibration_text_proto) - text_format.Merge(post_processing_text_proto, post_processing_config) - second_stage_non_max_suppression_fn, second_stage_score_conversion_fn = ( - post_processing_builder.build(post_processing_config)) - - second_stage_target_assigner = target_assigner.create_target_assigner( - 'FasterRCNN', - 'detection', - use_matmul_gather=use_matmul_gather_in_matcher) - second_stage_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=1.0, is_static=use_static_shapes) - - second_stage_localization_loss_weight = 1.0 - second_stage_classification_loss_weight = 1.0 - if softmax_second_stage_classification_loss: - second_stage_classification_loss = ( - losses.WeightedSoftmaxClassificationLoss()) - else: - second_stage_classification_loss = ( - losses.WeightedSigmoidClassificationLoss()) - - hard_example_miner = None - if hard_mining: - hard_example_miner = losses.HardExampleMiner( - num_hard_examples=1, - iou_threshold=0.99, - loss_type='both', - cls_loss_weight=second_stage_classification_loss_weight, - loc_loss_weight=second_stage_localization_loss_weight, - max_negatives_per_positive=None) - - crop_and_resize_fn = ( - spatial_ops.multilevel_matmul_crop_and_resize - if use_matmul_crop_and_resize - else spatial_ops.multilevel_native_crop_and_resize) - common_kwargs = { - 'is_training': - is_training, - 'num_classes': - num_classes, - 'image_resizer_fn': - image_resizer_fn, - 'feature_extractor': - fake_feature_extractor, - 'number_of_stages': - number_of_stages, - 'first_stage_anchor_generator': - first_stage_anchor_generator, - 'first_stage_target_assigner': - first_stage_target_assigner, - 'first_stage_atrous_rate': - first_stage_atrous_rate, - 'first_stage_box_predictor_arg_scope_fn': - first_stage_box_predictor_arg_scope_fn, - 'first_stage_box_predictor_kernel_size': - first_stage_box_predictor_kernel_size, - 'first_stage_box_predictor_depth': - first_stage_box_predictor_depth, - 'first_stage_minibatch_size': - first_stage_minibatch_size, - 'first_stage_sampler': - first_stage_sampler, - 'first_stage_non_max_suppression_fn': - first_stage_non_max_suppression_fn, - 'first_stage_max_proposals': - first_stage_max_proposals, - 'first_stage_localization_loss_weight': - first_stage_localization_loss_weight, - 'first_stage_objectness_loss_weight': - first_stage_objectness_loss_weight, - 'second_stage_target_assigner': - second_stage_target_assigner, - 'second_stage_batch_size': - second_stage_batch_size, - 'second_stage_sampler': - second_stage_sampler, - 'second_stage_non_max_suppression_fn': - second_stage_non_max_suppression_fn, - 'second_stage_score_conversion_fn': - second_stage_score_conversion_fn, - 'second_stage_localization_loss_weight': - second_stage_localization_loss_weight, - 'second_stage_classification_loss_weight': - second_stage_classification_loss_weight, - 'second_stage_classification_loss': - second_stage_classification_loss, - 'hard_example_miner': - hard_example_miner, - 'crop_and_resize_fn': - crop_and_resize_fn, - 'clip_anchors_to_image': - clip_anchors_to_image, - 'use_static_shapes': - use_static_shapes, - 'resize_masks': - True, - 'return_raw_detections_during_predict': - return_raw_detections_during_predict - } - - return self._get_model( - self._get_second_stage_box_predictor( - num_classes=num_classes, - is_training=is_training, - use_keras=use_keras, - predict_masks=predict_masks, - masks_are_class_agnostic=masks_are_class_agnostic, - share_box_across_classes=share_box_across_classes), **common_kwargs) - - @unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') - @mock.patch.object(context_rcnn_meta_arch, 'context_rcnn_lib') - def test_prediction_mock_tf1(self, mock_context_rcnn_lib_v1): - """Mocks the context_rcnn_lib_v1 module to test the prediction. - - Using mock object so that we can ensure _compute_box_context_attention is - called in side the prediction function. - - Args: - mock_context_rcnn_lib_v1: mock module for the context_rcnn_lib_v1. - """ - model = self._build_model( - is_training=False, - number_of_stages=2, - second_stage_batch_size=6, - num_classes=42) - mock_tensor = tf.ones([2, 8, 3, 3, 3], tf.float32) - - mock_context_rcnn_lib_v1._compute_box_context_attention.return_value = mock_tensor - inputs_shape = (2, 20, 20, 3) - inputs = tf.cast( - tf.random_uniform(inputs_shape, minval=0, maxval=255, dtype=tf.int32), - dtype=tf.float32) - preprocessed_inputs, true_image_shapes = model.preprocess(inputs) - context_features = tf.random_uniform((2, 20, 10), - minval=0, - maxval=255, - dtype=tf.float32) - valid_context_size = tf.random_uniform((2,), - minval=0, - maxval=10, - dtype=tf.int32) - features = { - fields.InputDataFields.context_features: context_features, - fields.InputDataFields.valid_context_size: valid_context_size - } - - side_inputs = model.get_side_inputs(features) - - _ = model.predict(preprocessed_inputs, true_image_shapes, **side_inputs) - mock_context_rcnn_lib_v1._compute_box_context_attention.assert_called_once() - - @parameterized.named_parameters( - {'testcase_name': 'static_shapes', 'static_shapes': True}, - {'testcase_name': 'nostatic_shapes', 'static_shapes': False}, - ) - def test_prediction_end_to_end(self, static_shapes): - """Runs prediction end to end and test the shape of the results.""" - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, - second_stage_batch_size=6, - use_matmul_crop_and_resize=static_shapes, - clip_anchors_to_image=static_shapes, - use_matmul_gather_in_matcher=static_shapes, - use_static_shapes=static_shapes, - num_classes=42) - - def graph_fn(): - inputs_shape = (2, 20, 20, 3) - inputs = tf.cast( - tf.random_uniform(inputs_shape, minval=0, maxval=255, dtype=tf.int32), - dtype=tf.float32) - preprocessed_inputs, true_image_shapes = model.preprocess(inputs) - context_features = tf.random_uniform((2, 20, 10), - minval=0, - maxval=255, - dtype=tf.float32) - valid_context_size = tf.random_uniform((2,), - minval=0, - maxval=10, - dtype=tf.int32) - features = { - fields.InputDataFields.context_features: context_features, - fields.InputDataFields.valid_context_size: valid_context_size - } - - side_inputs = model.get_side_inputs(features) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes, - **side_inputs) - return (prediction_dict['rpn_box_predictor_features'], - prediction_dict['rpn_box_encodings'], - prediction_dict['refined_box_encodings'], - prediction_dict['proposal_boxes_normalized'], - prediction_dict['proposal_boxes']) - execute_fn = self.execute if static_shapes else self.execute_cpu - (rpn_box_predictor_features, rpn_box_encodings, refined_box_encodings, - proposal_boxes_normalized, proposal_boxes) = execute_fn(graph_fn, [], - graph=g) - self.assertAllEqual(len(rpn_box_predictor_features), 1) - self.assertAllEqual(rpn_box_predictor_features[0].shape, [2, 20, 20, 512]) - self.assertAllEqual(rpn_box_encodings.shape, [2, 3600, 4]) - self.assertAllEqual(refined_box_encodings.shape, [16, 42, 4]) - self.assertAllEqual(proposal_boxes_normalized.shape, [2, 8, 4]) - self.assertAllEqual(proposal_boxes.shape, [2, 8, 4]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/deepmac_meta_arch.py b/research/object_detection/meta_architectures/deepmac_meta_arch.py deleted file mode 100644 index fb85f1e0f8a..00000000000 --- a/research/object_detection/meta_architectures/deepmac_meta_arch.py +++ /dev/null @@ -1,2117 +0,0 @@ -"""Deep Mask heads above CenterNet (DeepMAC)[1] architecture. - -[1]: https://arxiv.org/abs/2104.00613 -""" - -import collections - -from absl import logging -import numpy as np -import tensorflow as tf - -from object_detection.builders import losses_builder -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import losses -from object_detection.core import preprocessor -from object_detection.core import standard_fields as fields -from object_detection.meta_architectures import center_net_meta_arch -from object_detection.models.keras_models import hourglass_network -from object_detection.models.keras_models import resnet_v1 -from object_detection.protos import center_net_pb2 -from object_detection.protos import losses_pb2 -from object_detection.utils import shape_utils -from object_detection.utils import spatial_transform_ops -from object_detection.utils import tf_version - -if tf_version.is_tf2(): - import tensorflow_io as tfio # pylint:disable=g-import-not-at-top - - -INSTANCE_EMBEDDING = 'INSTANCE_EMBEDDING' -PIXEL_EMBEDDING = 'PIXEL_EMBEDDING' -MASK_LOGITS_GT_BOXES = 'MASK_LOGITS_GT_BOXES' -DEEP_MASK_ESTIMATION = 'deep_mask_estimation' -DEEP_MASK_BOX_CONSISTENCY = 'deep_mask_box_consistency' -DEEP_MASK_FEATURE_CONSISTENCY = 'deep_mask_feature_consistency' -DEEP_MASK_POINTLY_SUPERVISED = 'deep_mask_pointly_supervised' -SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS = ( - 'SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS') -DEEP_MASK_AUGMENTED_SELF_SUPERVISION = 'deep_mask_augmented_self_supervision' -CONSISTENCY_FEATURE_MAP = 'CONSISTENCY_FEATURE_MAP' -LOSS_KEY_PREFIX = center_net_meta_arch.LOSS_KEY_PREFIX -NEIGHBORS_2D = [[-1, -1], [-1, 0], [-1, 1], - [0, -1], [0, 1], - [1, -1], [1, 0], [1, 1]] - -WEAK_LOSSES = [DEEP_MASK_BOX_CONSISTENCY, DEEP_MASK_FEATURE_CONSISTENCY, - DEEP_MASK_AUGMENTED_SELF_SUPERVISION, - DEEP_MASK_POINTLY_SUPERVISED] - -MASK_LOSSES = WEAK_LOSSES + [DEEP_MASK_ESTIMATION] - - -DeepMACParams = collections.namedtuple('DeepMACParams', [ - 'classification_loss', 'dim', 'task_loss_weight', 'pixel_embedding_dim', - 'allowed_masked_classes_ids', 'mask_size', 'mask_num_subsamples', - 'use_xy', 'network_type', 'use_instance_embedding', 'num_init_channels', - 'predict_full_resolution_masks', 'postprocess_crop_size', - 'max_roi_jitter_ratio', 'roi_jitter_mode', - 'box_consistency_loss_weight', 'feature_consistency_threshold', - 'feature_consistency_dilation', 'feature_consistency_loss_weight', - 'box_consistency_loss_normalize', 'box_consistency_tightness', - 'feature_consistency_warmup_steps', 'feature_consistency_warmup_start', - 'use_only_last_stage', 'augmented_self_supervision_max_translation', - 'augmented_self_supervision_loss_weight', - 'augmented_self_supervision_flip_probability', - 'augmented_self_supervision_warmup_start', - 'augmented_self_supervision_warmup_steps', - 'augmented_self_supervision_loss', - 'augmented_self_supervision_scale_min', - 'augmented_self_supervision_scale_max', - 'pointly_supervised_keypoint_loss_weight', - 'ignore_per_class_box_overlap', - 'feature_consistency_type', - 'feature_consistency_comparison' - ]) - - -def _get_loss_weight(loss_name, config): - """Utility function to get loss weights by name.""" - if loss_name == DEEP_MASK_ESTIMATION: - return config.task_loss_weight - elif loss_name == DEEP_MASK_FEATURE_CONSISTENCY: - return config.feature_consistency_loss_weight - elif loss_name == DEEP_MASK_BOX_CONSISTENCY: - return config.box_consistency_loss_weight - elif loss_name == DEEP_MASK_AUGMENTED_SELF_SUPERVISION: - return config.augmented_self_supervision_loss_weight - elif loss_name == DEEP_MASK_POINTLY_SUPERVISED: - return config.pointly_supervised_keypoint_loss_weight - else: - raise ValueError('Unknown loss - {}'.format(loss_name)) - - -def subsample_instances(classes, weights, boxes, masks, num_subsamples): - """Randomly subsamples instances to the desired number. - - Args: - classes: [num_instances, num_classes] float tensor of one-hot encoded - classes. - weights: [num_instances] float tensor of weights of each instance. - boxes: [num_instances, 4] tensor of box coordinates. - masks: [num_instances, height, width] tensor of per-instance masks. - num_subsamples: int, the desired number of samples. - - Returns: - classes: [num_subsamples, num_classes] float tensor of classes. - weights: [num_subsamples] float tensor of weights. - boxes: [num_subsamples, 4] float tensor of box coordinates. - masks: [num_subsamples, height, width] float tensor of per-instance masks. - - """ - - if num_subsamples <= -1: - return classes, weights, boxes, masks - - num_instances = tf.reduce_sum(tf.cast(weights > 0.5, tf.int32)) - - if num_instances <= num_subsamples: - return (classes[:num_subsamples], weights[:num_subsamples], - boxes[:num_subsamples], masks[:num_subsamples]) - - else: - random_index = tf.random.uniform([num_subsamples], 0, num_instances, - dtype=tf.int32) - - return (tf.gather(classes, random_index), tf.gather(weights, random_index), - tf.gather(boxes, random_index), tf.gather(masks, random_index)) - - -def _get_deepmac_network_by_type(name, num_init_channels, mask_size=None): - """Get DeepMAC network model given a string type.""" - - if name.startswith('hourglass'): - if name == 'hourglass10': - return hourglass_network.hourglass_10(num_init_channels, - initial_downsample=False) - elif name == 'hourglass20': - return hourglass_network.hourglass_20(num_init_channels, - initial_downsample=False) - elif name == 'hourglass32': - return hourglass_network.hourglass_32(num_init_channels, - initial_downsample=False) - elif name == 'hourglass52': - return hourglass_network.hourglass_52(num_init_channels, - initial_downsample=False) - elif name == 'hourglass100': - return hourglass_network.hourglass_100(num_init_channels, - initial_downsample=False) - elif name == 'hourglass20_uniform_size': - return hourglass_network.hourglass_20_uniform_size(num_init_channels) - - elif name == 'hourglass20_no_shortcut': - return hourglass_network.hourglass_20_no_shortcut(num_init_channels) - - elif name == 'fully_connected': - if not mask_size: - raise ValueError('Mask size must be set.') - return FullyConnectedMaskHead(num_init_channels, mask_size) - - elif _is_mask_head_param_free(name): - return tf.keras.layers.Lambda(lambda x: x) - - elif name.startswith('resnet'): - return ResNetMaskNetwork(name, num_init_channels) - - raise ValueError('Unknown network type {}'.format(name)) - - -def boxes_batch_normalized_to_absolute_coordinates(boxes, height, width): - ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=2) - height, width = tf.cast(height, tf.float32), tf.cast(width, tf.float32) - ymin *= height - ymax *= height - xmin *= width - xmax *= width - - return tf.stack([ymin, xmin, ymax, xmax], axis=2) - - -def boxes_batch_absolute_to_normalized_coordinates(boxes, height, width): - ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=2) - height, width = tf.cast(height, tf.float32), tf.cast(width, tf.float32) - ymin /= height - ymax /= height - xmin /= width - xmax /= width - - return tf.stack([ymin, xmin, ymax, xmax], axis=2) - - -def _resize_instance_masks_non_empty(masks, shape): - """Resize a non-empty tensor of masks to the given shape.""" - height, width = shape - flattened_masks, batch_size, num_instances = flatten_first2_dims(masks) - flattened_masks = flattened_masks[:, :, :, tf.newaxis] - flattened_masks = tf.image.resize( - flattened_masks, (height, width), - method=tf.image.ResizeMethod.BILINEAR) - return unpack_first2_dims( - flattened_masks[:, :, :, 0], batch_size, num_instances) - - -def resize_instance_masks(masks, shape): - batch_size, num_instances = tf.shape(masks)[0], tf.shape(masks)[1] - return tf.cond( - tf.shape(masks)[1] == 0, - lambda: tf.zeros((batch_size, num_instances, shape[0], shape[1])), - lambda: _resize_instance_masks_non_empty(masks, shape)) - - -def filter_masked_classes(masked_class_ids, classes, weights, masks): - """Filter out masks whose class IDs are not present in masked_class_ids. - - Args: - masked_class_ids: A list of class IDs allowed to have masks. These class IDs - are 1-indexed. - classes: A [batch_size, num_instances, num_classes] float tensor containing - the one-hot encoded classes. - weights: A [batch_size, num_instances] float tensor containing the weights - of each sample. - masks: A [batch_size, num_instances, height, width] tensor containing the - mask per instance. - - Returns: - classes_filtered: A [batch_size, num_instances, num_classes] float tensor - containing the one-hot encoded classes with classes not in - masked_class_ids zeroed out. - weights_filtered: A [batch_size, num_instances] float tensor containing the - weights of each sample with instances whose classes aren't in - masked_class_ids zeroed out. - masks_filtered: A [batch_size, num_instances, height, width] tensor - containing the mask per instance with masks not belonging to - masked_class_ids zeroed out. - """ - - if len(masked_class_ids) == 0: # pylint:disable=g-explicit-length-test - return classes, weights, masks - - if tf.shape(classes)[1] == 0: - return classes, weights, masks - - masked_class_ids = tf.constant(np.array(masked_class_ids, dtype=np.int32)) - label_id_offset = 1 - masked_class_ids -= label_id_offset - class_ids = tf.argmax(classes, axis=2, output_type=tf.int32) - matched_classes = tf.equal( - class_ids[:, :, tf.newaxis], masked_class_ids[tf.newaxis, tf.newaxis, :] - ) - - matched_classes = tf.reduce_any(matched_classes, axis=2) - matched_classes = tf.cast(matched_classes, tf.float32) - - return ( - classes * matched_classes[:, :, tf.newaxis], - weights * matched_classes, - masks * matched_classes[:, :, tf.newaxis, tf.newaxis] - ) - - -def per_instance_no_class_overlap(classes, boxes, height, width): - """Returns 1s inside boxes but overlapping boxes of same class are zeroed out. - - Args: - classes: A [batch_size, num_instances, num_classes] float tensor containing - the one-hot encoded classes. - boxes: A [batch_size, num_instances, 4] shaped float tensor of normalized - boxes. - height: int, height of the desired mask. - width: int, width of the desired mask. - - Returns: - mask: A [batch_size, num_instances, height, width] float tensor of 0s and - 1s. - """ - box_mask = fill_boxes(boxes, height, width) - per_class_box_mask = ( - box_mask[:, :, tf.newaxis, :, :] * - classes[:, :, :, tf.newaxis, tf.newaxis]) - - per_class_instance_count = tf.reduce_sum(per_class_box_mask, axis=1) - per_class_valid_map = per_class_instance_count < 2 - class_indices = tf.argmax(classes, axis=2) - - per_instance_valid_map = tf.gather( - per_class_valid_map, class_indices, batch_dims=1) - - return tf.cast(per_instance_valid_map, tf.float32) - - -def flatten_first2_dims(tensor): - """Flatten first 2 dimensions of a tensor. - - Args: - tensor: A tensor with shape [M, N, ....] - - Returns: - flattened_tensor: A tensor of shape [M * N, ...] - M: int, the length of the first dimension of the input. - N: int, the length of the second dimension of the input. - """ - shape = tf.shape(tensor) - d1, d2, rest = shape[0], shape[1], shape[2:] - - tensor = tf.reshape( - tensor, tf.concat([[d1 * d2], rest], axis=0)) - return tensor, d1, d2 - - -def unpack_first2_dims(tensor, dim1, dim2): - """Unpack the flattened first dimension of the tensor into 2 dimensions. - - Args: - tensor: A tensor of shape [dim1 * dim2, ...] - dim1: int, the size of the first dimension. - dim2: int, the size of the second dimension. - - Returns: - unflattened_tensor: A tensor of shape [dim1, dim2, ...]. - """ - shape = tf.shape(tensor) - result_shape = tf.concat([[dim1, dim2], shape[1:]], axis=0) - return tf.reshape(tensor, result_shape) - - -def crop_and_resize_instance_masks(masks, boxes, mask_size): - """Crop and resize each mask according to the given boxes. - - Args: - masks: A [B, N, H, W] float tensor. - boxes: A [B, N, 4] float tensor of normalized boxes. - mask_size: int, the size of the output masks. - - Returns: - masks: A [B, N, mask_size, mask_size] float tensor of cropped and resized - instance masks. - """ - - masks, batch_size, num_instances = flatten_first2_dims(masks) - boxes, _, _ = flatten_first2_dims(boxes) - cropped_masks = spatial_transform_ops.matmul_crop_and_resize( - masks[:, :, :, tf.newaxis], boxes[:, tf.newaxis, :], - [mask_size, mask_size]) - cropped_masks = tf.squeeze(cropped_masks, axis=[1, 4]) - return unpack_first2_dims(cropped_masks, batch_size, num_instances) - - -def fill_boxes(boxes, height, width, expand=0): - """Fills the area included in the boxes with 1s. - - Args: - boxes: A [batch_size, num_instances, 4] shaped float tensor of boxes given - in the normalized coordinate space. - height: int, height of the output image. - width: int, width of the output image. - expand: int, the number of pixels to expand the box by. - - Returns: - filled_boxes: A [batch_size, num_instances, height, width] shaped float - tensor with 1s in the area that falls inside each box. - """ - expand = float(expand) - boxes_abs = boxes_batch_normalized_to_absolute_coordinates( - boxes, height, width) - ymin, xmin, ymax, xmax = tf.unstack( - boxes_abs[:, :, tf.newaxis, tf.newaxis, :], 4, axis=4) - - ygrid, xgrid = tf.meshgrid(tf.range(height), tf.range(width), indexing='ij') - ygrid, xgrid = tf.cast(ygrid, tf.float32), tf.cast(xgrid, tf.float32) - ygrid, xgrid = (ygrid[tf.newaxis, tf.newaxis, :, :], - xgrid[tf.newaxis, tf.newaxis, :, :]) - - ymin -= expand - xmin -= expand - ymax += expand - xmax += expand - - filled_boxes = tf.logical_and( - tf.logical_and(ygrid >= ymin, ygrid <= ymax), - tf.logical_and(xgrid >= xmin, xgrid <= xmax)) - - return tf.cast(filled_boxes, tf.float32) - - -def embedding_projection(x, y): - """Compute dot product between two given embeddings. - - Args: - x: [num_instances, height, width, dimension] float tensor input. - y: [num_instances, height, width, dimension] or - [num_instances, 1, 1, dimension] float tensor input. When the height - and width dimensions are 1, TF will broadcast it. - - Returns: - dist: [num_instances, height, width, 1] A float tensor returning - the per-pixel embedding projection. - """ - - dot = tf.reduce_sum(x * y, axis=3, keepdims=True) - return dot - - -def _get_2d_neighbors_kernel(): - """Returns a conv. kernel that when applies generates 2D neighbors. - - Returns: - kernel: A float tensor of shape [3, 3, 1, 8] - """ - - kernel = np.zeros((3, 3, 1, 8)) - - for i, (y, x) in enumerate(NEIGHBORS_2D): - kernel[1 + y, 1 + x, 0, i] = 1.0 - - return tf.constant(kernel, dtype=tf.float32) - - -def generate_2d_neighbors(input_tensor, dilation=2): - """Generate a feature map of 2D neighbors. - - Note: This op makes 8 (# of neighbors) as the leading dimension so that - following ops on TPU won't have to pad the last dimension to 128. - - Args: - input_tensor: A float tensor of shape [batch_size, height, width, channels]. - dilation: int, the dilation factor for considering neighbors. - - Returns: - output: A float tensor of all 8 2-D neighbors. of shape - [8, batch_size, height, width, channels]. - """ - - # TODO(vighneshb) Minimize tranposing here to save memory. - - # input_tensor: [B, C, H, W] - input_tensor = tf.transpose(input_tensor, (0, 3, 1, 2)) - # input_tensor: [B, C, H, W, 1] - input_tensor = input_tensor[:, :, :, :, tf.newaxis] - - # input_tensor: [B * C, H, W, 1] - input_tensor, batch_size, channels = flatten_first2_dims(input_tensor) - - kernel = _get_2d_neighbors_kernel() - - # output: [B * C, H, W, 8] - output = tf.nn.atrous_conv2d(input_tensor, kernel, rate=dilation, - padding='SAME') - # output: [B, C, H, W, 8] - output = unpack_first2_dims(output, batch_size, channels) - - # return: [8, B, H, W, C] - return tf.transpose(output, [4, 0, 2, 3, 1]) - - -def normalize_feature_map(feature_map): - return tf.math.l2_normalize(feature_map, axis=3, epsilon=1e-4) - - -def gaussian_pixel_similarity(a, b, theta): - norm_difference = tf.linalg.norm(a - b, axis=-1) - similarity = tf.exp(-norm_difference / theta) - return similarity - - -def dotprod_pixel_similarity(a, b): - return tf.reduce_sum(a * b, axis=-1) - - -def dilated_cross_pixel_similarity(feature_map, dilation=2, theta=2.0, - method='gaussian'): - """Dilated cross pixel similarity. - - method supports 2 values - - 'gaussian' from https://arxiv.org/abs/2012.02310 - - 'dotprod' computes the dot product between feature vector for similarity. - This assumes that the features are normalized. - - Args: - feature_map: A float tensor of shape [batch_size, height, width, channels] - dilation: int, the dilation factor. - theta: The denominator while taking difference inside the gaussian. - method: str, either 'gaussian' or 'dotprod'. - - Returns: - dilated_similarity: A tensor of shape [8, batch_size, height, width] - """ - neighbors = generate_2d_neighbors(feature_map, dilation) - feature_map = feature_map[tf.newaxis] - - if method == 'gaussian': - return gaussian_pixel_similarity(feature_map, neighbors, theta=theta) - elif method == 'dotprod': - return dotprod_pixel_similarity(feature_map, neighbors) - else: - raise ValueError('Unknown method for pixel sim %s' % method) - - -def dilated_cross_same_mask_label(instance_masks, dilation=2): - """Dilated cross pixel similarity as defined in [1]. - - [1]: https://arxiv.org/abs/2012.02310 - - Args: - instance_masks: A float tensor of shape [batch_size, num_instances, - height, width] - dilation: int, the dilation factor. - - Returns: - dilated_same_label: A tensor of shape [8, batch_size, num_instances, - height, width] - """ - - # instance_masks: [batch_size, height, width, num_instances] - instance_masks = tf.transpose(instance_masks, (0, 2, 3, 1)) - - # neighbors: [8, batch_size, height, width, num_instances] - neighbors = generate_2d_neighbors(instance_masks, dilation) - # instance_masks = [1, batch_size, height, width, num_instances] - instance_masks = instance_masks[tf.newaxis] - same_mask_prob = ((instance_masks * neighbors) + - ((1 - instance_masks) * (1 - neighbors))) - - return tf.transpose(same_mask_prob, (0, 1, 4, 2, 3)) - - -def _per_pixel_single_conv(input_tensor, params, channels): - """Convolve the given input with the given params. - - Args: - input_tensor: A [num_instances, height, width, channels] shaped - float tensor. - params: A [num_instances, num_params] shaped float tensor. - channels: int, number of channels in the convolution. - - Returns: - output: A float tensor of shape [num_instances, height, width, channels] - """ - - input_channels = input_tensor.get_shape().as_list()[3] - weights = params[:, :(input_channels * channels)] - biases = params[:, (input_channels * channels):] - num_instances = tf.shape(params)[0] - - weights = tf.reshape(weights, (num_instances, input_channels, channels)) - output = (input_tensor[:, :, tf.newaxis, :] @ - weights[:, tf.newaxis, tf.newaxis, :, :]) - - output = output[:, :, 0, :, :] - output = output + biases[:, tf.newaxis, tf.newaxis, :] - return output - - -def per_pixel_conditional_conv(input_tensor, parameters, channels, depth): - """Use parameters perform per-pixel convolutions with the given depth [1]. - - [1]: https://arxiv.org/abs/2003.05664 - - Args: - input_tensor: float tensor of shape [num_instances, height, - width, input_channels] - parameters: A [num_instances, num_params] float tensor. If num_params - is incomparible with the given channels and depth, a ValueError will - be raised. - channels: int, the number of channels in the convolution. - depth: int, the number of layers of convolutions to perform. - - Returns: - output: A [num_instances, height, width] tensor with the conditional - conv applied according to each instance's parameters. - """ - - input_channels = input_tensor.get_shape().as_list()[3] - num_params = parameters.get_shape().as_list()[1] - - input_convs = 1 if depth > 1 else 0 - intermediate_convs = depth - 2 if depth >= 2 else 0 - expected_weights = ((input_channels * channels * input_convs) + - (channels * channels * intermediate_convs) + - channels) # final conv - expected_biases = (channels * (depth - 1)) + 1 - - if depth == 1: - if input_channels != channels: - raise ValueError( - 'When depth=1, input_channels({}) should be equal to'.format( - input_channels) + ' channels({})'.format(channels)) - - if num_params != (expected_weights + expected_biases): - raise ValueError('Expected {} parameters at depth {}, but got {}'.format( - expected_weights + expected_biases, depth, num_params)) - - start = 0 - output = input_tensor - for i in range(depth): - - is_last_layer = i == (depth - 1) - if is_last_layer: - channels = 1 - - num_params_single_conv = channels * input_channels + channels - params = parameters[:, start:start + num_params_single_conv] - - start += num_params_single_conv - output = _per_pixel_single_conv(output, params, channels) - - if not is_last_layer: - output = tf.nn.relu(output) - - input_channels = channels - - return output - - -def flip_boxes_left_right(boxes): - ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=2) - - return tf.stack( - [ymin, 1.0 - xmax, ymax, 1.0 - xmin], axis=2 - ) - - -def transform_images_and_boxes(images, boxes, tx, ty, sx, sy, flip): - """Translate and scale a batch of images and boxes by the given amount. - - The function first translates and then scales the image and assumes the - origin to be at the center of the image. - - Args: - images: A [batch_size, height, width, 3] float tensor of images. - boxes: optional, A [batch_size, num_instances, 4] shaped float tensor of - normalized bounding boxes. If None, the second return value is always - None. - tx: A [batch_size] shaped float tensor of x translations. - ty: A [batch_size] shaped float tensor of y translations. - sx: A [batch_size] shaped float tensor of x scale factor. - sy: A [batch_size] shaped float tensor of y scale factor. - flip: A [batch_size] shaped bool tensor indicating whether or not we - flip the image. - - Returns: - transformed_images: Transfomed images of same shape as `images`. - transformed_boxes: If `boxes` was not None, transformed boxes of same - shape as boxes. - - """ - _, height, width, _ = shape_utils.combined_static_and_dynamic_shape( - images) - - flip_selector = tf.cast(flip, tf.float32) - flip_selector_4d = flip_selector[:, tf.newaxis, tf.newaxis, tf.newaxis] - flip_selector_3d = flip_selector[:, tf.newaxis, tf.newaxis] - flipped_images = tf.image.flip_left_right(images) - images = flipped_images * flip_selector_4d + (1.0 - flip_selector_4d) * images - - cy = cx = tf.zeros_like(tx) + 0.5 - ymin = -ty*sy + cy - sy * 0.5 - xmin = -tx*sx + cx - sx * 0.5 - ymax = -ty*sy + cy + sy * 0.5 - xmax = -tx*sx + cx + sx * 0.5 - crop_box = tf.stack([ymin, xmin, ymax, xmax], axis=1) - - crop_box_expanded = crop_box[:, tf.newaxis, :] - - images_transformed = spatial_transform_ops.matmul_crop_and_resize( - images, crop_box_expanded, (height, width) - ) - images_transformed = images_transformed[:, 0, :, :, :] - - if boxes is not None: - flipped_boxes = flip_boxes_left_right(boxes) - boxes = flipped_boxes * flip_selector_3d + (1.0 - flip_selector_3d) * boxes - win_height = ymax - ymin - win_width = xmax - xmin - win_height = win_height[:, tf.newaxis] - win_width = win_width[:, tf.newaxis] - boxes_transformed = ( - boxes - tf.stack([ymin, xmin, ymin, xmin], axis=1)[:, tf.newaxis, :]) - - boxes_ymin, boxes_xmin, boxes_ymax, boxes_xmax = tf.unstack( - boxes_transformed, axis=2) - boxes_ymin *= 1.0 / win_height - boxes_xmin *= 1.0 / win_width - boxes_ymax *= 1.0 / win_height - boxes_xmax *= 1.0 / win_width - - boxes = tf.stack([boxes_ymin, boxes_xmin, boxes_ymax, boxes_xmax], axis=2) - - return images_transformed, boxes - - -def transform_instance_masks(instance_masks, tx, ty, sx, sy, flip): - """Transforms a batch of instances by the given amount. - - Args: - instance_masks: A [batch_size, num_instances, height, width, 3] float - tensor of instance masks. - tx: A [batch_size] shaped float tensor of x translations. - ty: A [batch_size] shaped float tensor of y translations. - sx: A [batch_size] shaped float tensor of x scale factor. - sy: A [batch_size] shaped float tensor of y scale factor. - flip: A [batch_size] shaped bool tensor indicating whether or not we - flip the image. - - Returns: - transformed_images: Transfomed images of same shape as `images`. - transformed_boxes: If `boxes` was not None, transformed boxes of same - shape as boxes. - - """ - instance_masks, batch_size, num_instances = flatten_first2_dims( - instance_masks) - - repeat = tf.zeros_like(tx, dtype=tf.int32) + num_instances - tx = tf.repeat(tx, repeat) - ty = tf.repeat(ty, repeat) - sx = tf.repeat(sx, repeat) - sy = tf.repeat(sy, repeat) - flip = tf.repeat(flip, repeat) - - instance_masks = instance_masks[:, :, :, tf.newaxis] - instance_masks, _ = transform_images_and_boxes( - instance_masks, boxes=None, tx=tx, ty=ty, sx=sx, sy=sy, flip=flip) - - return unpack_first2_dims( - instance_masks[:, :, :, 0], batch_size, num_instances) - - -class ResNetMaskNetwork(tf.keras.layers.Layer): - """A small wrapper around ResNet blocks to predict masks.""" - - def __init__(self, resnet_type, num_init_channels): - """Creates the ResNet mask network. - - Args: - resnet_type: A string of the for resnetN where N where N is in - [4, 8, 12, 16, 20] - num_init_channels: Number of filters in the ResNet block. - """ - - super(ResNetMaskNetwork, self).__init__() - nc = num_init_channels - - if resnet_type == 'resnet4': - channel_dims = [nc * 2] - blocks = [2] - elif resnet_type == 'resnet8': - channel_dims = [nc * 2] - blocks = [4] - elif resnet_type == 'resnet12': - channel_dims = [nc * 2] - blocks = [6] - elif resnet_type == 'resnet16': - channel_dims = [nc * 2] - blocks = [8] - # Defined such that the channels are roughly similar to the hourglass20. - elif resnet_type == 'resnet20': - channel_dims = [nc * 2, nc * 3] - blocks = [8, 2] - else: - raise ValueError('Unknown resnet type "{}"'.format(resnet_type)) - - self.input_layer = tf.keras.layers.Conv2D(nc, 1, 1) - - # Last channel has to be defined so that batch norm can initialize properly. - model_input = tf.keras.layers.Input([None, None, nc]) - output = model_input - - for i, (num_blocks, channels) in enumerate(zip(blocks, channel_dims)): - output = resnet_v1.stack_basic(output, filters=channels, - blocks=num_blocks, stride1=1, - name='resnet_mask_block_%d' % i) - self.model = tf.keras.Model(inputs=model_input, outputs=output) - - def __call__(self, inputs): - return self.model(self.input_layer(inputs)) - - -class FullyConnectedMaskHead(tf.keras.layers.Layer): - """A 2 layer fully connected mask head.""" - - def __init__(self, num_init_channels, mask_size): - super(FullyConnectedMaskHead, self).__init__() - self.fc1 = tf.keras.layers.Dense(units=1024, activation='relu') - self.fc2 = tf.keras.layers.Dense(units=mask_size*mask_size) - self.mask_size = mask_size - self.num_input_channels = num_init_channels - self.input_layer = tf.keras.layers.Conv2D(num_init_channels, 1, 1) - model_input = tf.keras.layers.Input( - [mask_size * mask_size * num_init_channels,]) - output = self.fc2(self.fc1(model_input)) - self.model = tf.keras.Model(inputs=model_input, outputs=output) - - def __call__(self, inputs): - inputs = self.input_layer(inputs) - inputs_shape = tf.shape(inputs) - num_instances = inputs_shape[0] - height = inputs_shape[1] - width = inputs_shape[2] - dims = inputs_shape[3] - flattened_inputs = tf.reshape(inputs, - [num_instances, height * width * dims]) - flattened_masks = self.model(flattened_inputs) - return tf.reshape(flattened_masks, - [num_instances, self.mask_size, self.mask_size, 1]) - - -class DenseResidualBlock(tf.keras.layers.Layer): - """Residual block for 1D inputs. - - This class implemented the pre-activation version of the ResNet block. - """ - - def __init__(self, hidden_size, use_shortcut_linear): - """Residual Block for 1D inputs. - - Args: - hidden_size: size of the hidden layer. - use_shortcut_linear: bool, whether or not to use a linear layer for - shortcut. - """ - - super(DenseResidualBlock, self).__init__() - - self.bn_0 = tf.keras.layers.experimental.SyncBatchNormalization(axis=-1) - self.bn_1 = tf.keras.layers.experimental.SyncBatchNormalization(axis=-1) - - self.fc_0 = tf.keras.layers.Dense( - hidden_size, activation=None) - self.fc_1 = tf.keras.layers.Dense( - hidden_size, activation=None, kernel_initializer='zeros') - - self.activation = tf.keras.layers.Activation('relu') - - if use_shortcut_linear: - self.shortcut = tf.keras.layers.Dense( - hidden_size, activation=None, use_bias=False) - else: - self.shortcut = tf.keras.layers.Lambda(lambda x: x) - - def __call__(self, inputs): - """Layer's forward pass. - - Args: - inputs: input tensor. - - Returns: - Tensor after residual block w/ CondBatchNorm. - """ - out = self.fc_0(self.activation(self.bn_0(inputs))) - residual_inp = self.fc_1(self.activation(self.bn_1(out))) - - skip = self.shortcut(inputs) - - return residual_inp + skip - - -class DenseResNet(tf.keras.layers.Layer): - """Resnet with dense layers.""" - - def __init__(self, num_layers, hidden_size, output_size): - """Resnet with dense layers. - - Args: - num_layers: int, the number of layers. - hidden_size: size of the hidden layer. - output_size: size of the output. - """ - - super(DenseResNet, self).__init__() - - self.input_proj = DenseResidualBlock(hidden_size, use_shortcut_linear=True) - if num_layers < 4: - raise ValueError( - 'Cannot construct a DenseResNet with less than 4 layers') - - num_blocks = (num_layers - 2) // 2 - - if ((num_blocks * 2) + 2) != num_layers: - raise ValueError(('DenseResNet depth has to be of the form (2n + 2). ' - f'Found {num_layers}')) - - self._num_blocks = num_blocks - blocks = [DenseResidualBlock(hidden_size, use_shortcut_linear=False) - for _ in range(num_blocks)] - self.resnet = tf.keras.Sequential(blocks) - self.out_conv = tf.keras.layers.Dense(output_size) - - def __call__(self, inputs): - net = self.input_proj(inputs) - return self.out_conv(self.resnet(net)) - - -def _is_mask_head_param_free(name): - - # Mask heads which don't have parameters of their own and instead rely - # on the instance embedding. - - if name == 'embedding_projection' or name.startswith('cond_inst'): - return True - return False - - -class MaskHeadNetwork(tf.keras.layers.Layer): - """Mask head class for DeepMAC.""" - - def __init__(self, network_type, num_init_channels=64, - use_instance_embedding=True, mask_size=None): - """Initializes the network. - - Args: - network_type: A string denoting the kind of network we want to use - internally. - num_init_channels: int, the number of channels in the first block. The - number of channels in the following blocks depend on the network type - used. - use_instance_embedding: bool, if set, we concatenate the instance - embedding to the input while predicting the mask. - mask_size: int, size of the output mask. Required only with - `fully_connected` mask type. - """ - - super(MaskHeadNetwork, self).__init__() - - self._net = _get_deepmac_network_by_type( - network_type, num_init_channels, mask_size) - self._use_instance_embedding = use_instance_embedding - - self._network_type = network_type - self._num_init_channels = num_init_channels - - if (self._use_instance_embedding and - (_is_mask_head_param_free(network_type))): - raise ValueError(('Cannot feed instance embedding to mask head when ' - 'mask-head has no parameters.')) - - if _is_mask_head_param_free(network_type): - self.project_out = tf.keras.layers.Lambda(lambda x: x) - else: - self.project_out = tf.keras.layers.Conv2D( - filters=1, kernel_size=1, activation=None) - - def __call__(self, instance_embedding, pixel_embedding, training): - """Returns mask logits given object center and spatial embeddings. - - Args: - instance_embedding: A [num_instances, embedding_size] float tensor - representing the center emedding vector of each instance. - pixel_embedding: A [num_instances, height, width, pixel_embedding_size] - float tensor representing the per-pixel spatial embedding for each - instance. - training: boolean flag indicating training or testing mode. - - Returns: - mask: A [num_instances, height, width] float tensor containing the mask - logits for each instance. - """ - - height = tf.shape(pixel_embedding)[1] - width = tf.shape(pixel_embedding)[2] - - if self._use_instance_embedding: - instance_embedding = instance_embedding[:, tf.newaxis, tf.newaxis, :] - instance_embedding = tf.tile(instance_embedding, [1, height, width, 1]) - inputs = tf.concat([pixel_embedding, instance_embedding], axis=3) - else: - inputs = pixel_embedding - - out = self._net(inputs) - if isinstance(out, list): - out = out[-1] - - if self._network_type == 'embedding_projection': - instance_embedding = instance_embedding[:, tf.newaxis, tf.newaxis, :] - out = embedding_projection(instance_embedding, out) - - elif self._network_type.startswith('cond_inst'): - depth = int(self._network_type.lstrip('cond_inst')) - out = per_pixel_conditional_conv(out, instance_embedding, - self._num_init_channels, depth) - - if out.shape[-1] > 1: - out = self.project_out(out) - - return tf.squeeze(out, axis=-1) - - -def _batch_gt_list(gt_list): - return tf.stack(gt_list, axis=0) - - -def deepmac_proto_to_params(deepmac_config): - """Convert proto to named tuple.""" - - loss = losses_pb2.Loss() - # Add dummy localization loss to avoid the loss_builder throwing error. - loss.localization_loss.weighted_l2.CopyFrom( - losses_pb2.WeightedL2LocalizationLoss()) - - loss.classification_loss.CopyFrom(deepmac_config.classification_loss) - classification_loss, _, _, _, _, _, _ = (losses_builder.build(loss)) - - deepmac_field_class = ( - center_net_pb2.CenterNet.DESCRIPTOR.nested_types_by_name[ - 'DeepMACMaskEstimation']) - - params = {} - for field in deepmac_field_class.fields: - value = getattr(deepmac_config, field.name) - if field.enum_type: - params[field.name] = field.enum_type.values_by_number[value].name.lower() - else: - params[field.name] = value - - params['roi_jitter_mode'] = params.pop('jitter_mode') - params['classification_loss'] = classification_loss - return DeepMACParams(**params) - - -def _warmup_weight(current_training_step, warmup_start, warmup_steps): - """Utility function for warming up loss weights.""" - - if warmup_steps == 0: - return 1.0 - - training_step = tf.cast(current_training_step, tf.float32) - warmup_steps = tf.cast(warmup_steps, tf.float32) - start_step = tf.cast(warmup_start, tf.float32) - warmup_weight = (training_step - start_step) / warmup_steps - warmup_weight = tf.clip_by_value(warmup_weight, 0.0, 1.0) - return warmup_weight - - -class DeepMACMetaArch(center_net_meta_arch.CenterNetMetaArch): - """The experimental CenterNet DeepMAC[1] model. - - [1]: https://arxiv.org/abs/2104.00613 - """ - - def __init__(self, - is_training, - add_summaries, - num_classes, - feature_extractor, - image_resizer_fn, - object_center_params, - object_detection_params, - deepmac_params: DeepMACParams, - compute_heatmap_sparse=False): - """Constructs the super class with object center & detection params only.""" - - self._deepmac_params = deepmac_params - if (self._deepmac_params.predict_full_resolution_masks and - self._deepmac_params.max_roi_jitter_ratio > 0.0): - raise ValueError('Jittering is not supported for full res masks.') - - if self._deepmac_params.mask_num_subsamples > 0: - raise ValueError('Subsampling masks is currently not supported.') - - if self._deepmac_params.network_type == 'embedding_projection': - if self._deepmac_params.use_xy: - raise ValueError( - 'Cannot use x/y coordinates when using embedding projection.') - - pixel_embedding_dim = self._deepmac_params.pixel_embedding_dim - dim = self._deepmac_params.dim - if dim != pixel_embedding_dim: - raise ValueError( - 'When using embedding projection mask head, ' - f'pixel_embedding_dim({pixel_embedding_dim}) ' - f'must be same as dim({dim}).') - - generator_class = tf.random.Generator - self._self_supervised_rng = generator_class.from_non_deterministic_state() - super(DeepMACMetaArch, self).__init__( - is_training=is_training, add_summaries=add_summaries, - num_classes=num_classes, feature_extractor=feature_extractor, - image_resizer_fn=image_resizer_fn, - object_center_params=object_center_params, - object_detection_params=object_detection_params, - compute_heatmap_sparse=compute_heatmap_sparse) - - def _construct_prediction_heads(self, num_classes, num_feature_outputs, - class_prediction_bias_init): - super_instance = super(DeepMACMetaArch, self) - prediction_heads = super_instance._construct_prediction_heads( # pylint:disable=protected-access - num_classes, num_feature_outputs, class_prediction_bias_init) - - if self._deepmac_params is not None: - prediction_heads[INSTANCE_EMBEDDING] = [ - center_net_meta_arch.make_prediction_net(self._deepmac_params.dim) - for _ in range(num_feature_outputs) - ] - - prediction_heads[PIXEL_EMBEDDING] = [ - center_net_meta_arch.make_prediction_net( - self._deepmac_params.pixel_embedding_dim) - for _ in range(num_feature_outputs) - ] - - self._mask_net = MaskHeadNetwork( - network_type=self._deepmac_params.network_type, - use_instance_embedding=self._deepmac_params.use_instance_embedding, - num_init_channels=self._deepmac_params.num_init_channels) - - return prediction_heads - - def _get_mask_head_input(self, boxes, pixel_embedding): - """Get the input to the mask network, given bounding boxes. - - Args: - boxes: A [batch_size, num_instances, 4] float tensor containing bounding - boxes in normalized coordinates. - pixel_embedding: A [batch_size, height, width, embedding_size] float - tensor containing spatial pixel embeddings. - - Returns: - embedding: A [batch_size, num_instances, mask_height, mask_width, - embedding_size + 2] float tensor containing the inputs to the mask - network. For each bounding box, we concatenate the normalized box - coordinates to the cropped pixel embeddings. If - predict_full_resolution_masks is set, mask_height and mask_width are - the same as height and width of pixel_embedding. If not, mask_height - and mask_width are the same as mask_size. - """ - - batch_size, num_instances = tf.shape(boxes)[0], tf.shape(boxes)[1] - mask_size = self._deepmac_params.mask_size - - if self._deepmac_params.predict_full_resolution_masks: - num_instances = tf.shape(boxes)[1] - pixel_embedding = pixel_embedding[:, tf.newaxis, :, :, :] - pixel_embeddings_processed = tf.tile(pixel_embedding, - [1, num_instances, 1, 1, 1]) - image_shape = tf.shape(pixel_embeddings_processed) - image_height, image_width = image_shape[2], image_shape[3] - y_grid, x_grid = tf.meshgrid(tf.linspace(0.0, 1.0, image_height), - tf.linspace(0.0, 1.0, image_width), - indexing='ij') - - ycenter = (boxes[:, :, 0] + boxes[:, :, 2]) / 2.0 - xcenter = (boxes[:, :, 1] + boxes[:, :, 3]) / 2.0 - y_grid = y_grid[tf.newaxis, tf.newaxis, :, :] - x_grid = x_grid[tf.newaxis, tf.newaxis, :, :] - - y_grid -= ycenter[:, :, tf.newaxis, tf.newaxis] - x_grid -= xcenter[:, :, tf.newaxis, tf.newaxis] - coords = tf.stack([y_grid, x_grid], axis=4) - - else: - - # TODO(vighneshb) Explore multilevel_roi_align and align_corners=False. - embeddings = spatial_transform_ops.matmul_crop_and_resize( - pixel_embedding, boxes, [mask_size, mask_size]) - pixel_embeddings_processed = embeddings - mask_shape = tf.shape(pixel_embeddings_processed) - mask_height, mask_width = mask_shape[2], mask_shape[3] - - y_grid, x_grid = tf.meshgrid(tf.linspace(-1.0, 1.0, mask_height), - tf.linspace(-1.0, 1.0, mask_width), - indexing='ij') - coords = tf.stack([y_grid, x_grid], axis=2) - coords = coords[tf.newaxis, tf.newaxis, :, :, :] - coords = tf.tile(coords, [batch_size, num_instances, 1, 1, 1]) - - if self._deepmac_params.use_xy: - return tf.concat([coords, pixel_embeddings_processed], axis=4) - else: - return pixel_embeddings_processed - - def _get_instance_embeddings(self, boxes, instance_embedding): - """Return the instance embeddings from bounding box centers. - - Args: - boxes: A [batch_size, num_instances, 4] float tensor holding bounding - boxes. The coordinates are in normalized input space. - instance_embedding: A [batch_size, height, width, embedding_size] float - tensor containing the instance embeddings. - - Returns: - instance_embeddings: A [batch_size, num_instances, embedding_size] - shaped float tensor containing the center embedding for each instance. - """ - - output_height = tf.cast(tf.shape(instance_embedding)[1], tf.float32) - output_width = tf.cast(tf.shape(instance_embedding)[2], tf.float32) - ymin = boxes[:, :, 0] - xmin = boxes[:, :, 1] - ymax = boxes[:, :, 2] - xmax = boxes[:, :, 3] - - y_center_output = (ymin + ymax) * output_height / 2.0 - x_center_output = (xmin + xmax) * output_width / 2.0 - - center_coords_output = tf.stack([y_center_output, x_center_output], axis=2) - center_coords_output_int = tf.cast(center_coords_output, tf.int32) - - center_latents = tf.gather_nd(instance_embedding, center_coords_output_int, - batch_dims=1) - - return center_latents - - def predict(self, preprocessed_inputs, true_image_shapes): - prediction_dict = super(DeepMACMetaArch, self).predict( - preprocessed_inputs, true_image_shapes) - - if self.groundtruth_has_field(fields.BoxListFields.boxes): - mask_logits = self._predict_mask_logits_from_gt_boxes(prediction_dict) - prediction_dict[MASK_LOGITS_GT_BOXES] = mask_logits - - if self._deepmac_params.augmented_self_supervision_loss_weight > 0.0: - prediction_dict[SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS] = ( - self._predict_deaugmented_mask_logits_on_augmented_inputs( - preprocessed_inputs, true_image_shapes)) - return prediction_dict - - def _predict_deaugmented_mask_logits_on_augmented_inputs( - self, preprocessed_inputs, true_image_shapes): - """Predicts masks on augmented images and reverses that augmentation. - - The masks are de-augmented so that they are aligned with the original image. - - Args: - preprocessed_inputs: A batch of images of shape - [batch_size, height, width, 3]. - true_image_shapes: True shape of the image in case there is any padding. - - Returns: - mask_logits: - A float tensor of shape [batch_size, num_instances, - output_height, output_width, ] - """ - - batch_size = tf.shape(preprocessed_inputs)[0] - gt_boxes = _batch_gt_list( - self.groundtruth_lists(fields.BoxListFields.boxes)) - max_t = self._deepmac_params.augmented_self_supervision_max_translation - tx = self._self_supervised_rng.uniform( - [batch_size], minval=-max_t, maxval=max_t) - ty = self._self_supervised_rng.uniform( - [batch_size], minval=-max_t, maxval=max_t) - - scale_min = self._deepmac_params.augmented_self_supervision_scale_min - scale_max = self._deepmac_params.augmented_self_supervision_scale_max - sx = self._self_supervised_rng.uniform([batch_size], minval=scale_min, - maxval=scale_max) - sy = self._self_supervised_rng.uniform([batch_size], minval=scale_min, - maxval=scale_max) - flip = (self._self_supervised_rng.uniform( - [batch_size], minval=0.0, maxval=1.0) < - self._deepmac_params.augmented_self_supervision_flip_probability) - - augmented_inputs, augmented_boxes = transform_images_and_boxes( - preprocessed_inputs, gt_boxes, tx=tx, ty=ty, sx=sx, sy=sy, flip=flip - ) - - augmented_prediction_dict = super(DeepMACMetaArch, self).predict( - augmented_inputs, true_image_shapes) - - augmented_masks_lists = self._predict_mask_logits_from_boxes( - augmented_prediction_dict, augmented_boxes) - - deaugmented_masks_list = [] - - for mask_logits in augmented_masks_lists: - deaugmented_masks = transform_instance_masks( - mask_logits, tx=-tx, ty=-ty, sx=1.0/sx, sy=1.0/sy, flip=flip) - deaugmented_masks = tf.stop_gradient(deaugmented_masks) - deaugmented_masks_list.append(deaugmented_masks) - - return deaugmented_masks_list - - def _predict_mask_logits_from_embeddings( - self, pixel_embedding, instance_embedding, boxes): - mask_input = self._get_mask_head_input(boxes, pixel_embedding) - mask_input, batch_size, num_instances = flatten_first2_dims(mask_input) - - instance_embeddings = self._get_instance_embeddings( - boxes, instance_embedding) - instance_embeddings, _, _ = flatten_first2_dims(instance_embeddings) - - mask_logits = self._mask_net( - instance_embeddings, mask_input, - training=tf.keras.backend.learning_phase()) - mask_logits = unpack_first2_dims( - mask_logits, batch_size, num_instances) - return mask_logits - - def _predict_mask_logits_from_boxes(self, prediction_dict, boxes): - """Predict mask logits using the predict dict and the given set of boxes. - - Args: - prediction_dict: a dict containing the keys INSTANCE_EMBEDDING and - PIXEL_EMBEDDING, both expected to be list of tensors. - boxes: A [batch_size, num_instances, 4] float tensor of boxes in the - normalized coordinate system. - Returns: - mask_logits_list: A list of mask logits with the same spatial extents - as prediction_dict[PIXEL_EMBEDDING]. - - Returns: - - """ - mask_logits_list = [] - - instance_embedding_list = prediction_dict[INSTANCE_EMBEDDING] - pixel_embedding_list = prediction_dict[PIXEL_EMBEDDING] - - if self._deepmac_params.use_only_last_stage: - instance_embedding_list = [instance_embedding_list[-1]] - pixel_embedding_list = [pixel_embedding_list[-1]] - - for (instance_embedding, pixel_embedding) in zip(instance_embedding_list, - pixel_embedding_list): - - mask_logits_list.append( - self._predict_mask_logits_from_embeddings( - pixel_embedding, instance_embedding, boxes)) - - return mask_logits_list - - def _predict_mask_logits_from_gt_boxes(self, prediction_dict): - return self._predict_mask_logits_from_boxes( - prediction_dict, - _batch_gt_list(self.groundtruth_lists(fields.BoxListFields.boxes))) - - def _get_groundtruth_mask_output(self, boxes, masks): - """Get the expected mask output for each box. - - Args: - boxes: A [batch_size, num_instances, 4] float tensor containing bounding - boxes in normalized coordinates. - masks: A [batch_size, num_instances, height, width] float tensor - containing binary ground truth masks. - - Returns: - masks: If predict_full_resolution_masks is set, masks are not resized - and the size of this tensor is [batch_size, num_instances, - input_height, input_width]. Otherwise, returns a tensor of size - [batch_size, num_instances, mask_size, mask_size]. - """ - - mask_size = self._deepmac_params.mask_size - if self._deepmac_params.predict_full_resolution_masks: - return masks - else: - cropped_masks = crop_and_resize_instance_masks( - masks, boxes, mask_size) - cropped_masks = tf.stop_gradient(cropped_masks) - - # TODO(vighneshb) should we discretize masks? - return cropped_masks - - def _resize_logits_like_gt(self, logits, gt): - height, width = tf.shape(gt)[2], tf.shape(gt)[3] - return resize_instance_masks(logits, (height, width)) - - def _aggregate_classification_loss(self, loss, gt, pred, method): - """Aggregates loss at a per-instance level. - - When this function is used with mask-heads, num_classes is usually 1. - Args: - loss: A [num_instances, num_pixels, num_classes] or - [num_instances, num_classes] tensor. If the tensor is of rank 2, i.e., - of the form [num_instances, num_classes], we will assume that the - number of pixels have already been nornalized. - gt: A [num_instances, num_pixels, num_classes] float tensor of - groundtruths. - pred: A [num_instances, num_pixels, num_classes] float tensor of - preditions. - method: A string in ['auto', 'groundtruth']. - 'auto': When `loss` is rank 2, aggregates by sum. Otherwise, aggregates - by mean. - 'groundtruth_count': Aggreagates the loss by computing sum and dividing - by the number of positive (1) groundtruth pixels. - 'balanced': Normalizes each pixel by the number of positive or negative - pixels depending on the groundtruth. - - Returns: - per_instance_loss: A [num_instances] float tensor. - """ - - rank = len(loss.get_shape().as_list()) - if rank == 2: - axes = [1] - else: - axes = [1, 2] - - if method == 'normalize_auto': - normalization = 1.0 - if rank == 2: - return tf.reduce_sum(loss, axis=axes) - else: - return tf.reduce_mean(loss, axis=axes) - - elif method == 'normalize_groundtruth_count': - normalization = tf.reduce_sum(gt, axis=axes) - return tf.reduce_sum(loss, axis=axes) / normalization - - elif method == 'normalize_balanced': - if rank != 3: - raise ValueError('Cannot apply normalized_balanced aggregation ' - f'to loss of rank {rank}') - normalization = ( - (gt * tf.reduce_sum(gt, keepdims=True, axis=axes)) + - (1 - gt) * tf.reduce_sum(1 - gt, keepdims=True, axis=axes)) - return tf.reduce_sum(loss / normalization, axis=axes) - - else: - raise ValueError('Unknown loss aggregation - {}'.format(method)) - - def _compute_mask_prediction_loss( - self, boxes, mask_logits, mask_gt, classes): - """Compute the per-instance mask loss. - - Args: - boxes: A [batch_size, num_instances, 4] float tensor of GT boxes in - normalized coordinates. - mask_logits: A [batch_size, num_instances, height, width] float tensor of - predicted masks - mask_gt: The groundtruth mask of same shape as mask_logits. - classes: A [batch_size, num_instances, num_classes] shaped tensor of - one-hot encoded classes. - - Returns: - loss: A [batch_size, num_instances] shaped tensor with the loss for each - instance. - """ - - if mask_gt is None: - logging.info('No mask GT provided, mask loss is 0.') - return tf.zeros_like(boxes[:, :, 0]) - - batch_size, num_instances = tf.shape(boxes)[0], tf.shape(boxes)[1] - mask_logits = self._resize_logits_like_gt(mask_logits, mask_gt) - height, width = tf.shape(mask_logits)[2], tf.shape(mask_logits)[3] - - if self._deepmac_params.ignore_per_class_box_overlap: - mask_logits *= per_instance_no_class_overlap( - classes, boxes, height, width) - - height, wdith = tf.shape(mask_gt)[2], tf.shape(mask_gt)[3] - mask_logits *= per_instance_no_class_overlap( - classes, boxes, height, wdith) - - mask_logits = tf.reshape(mask_logits, [batch_size * num_instances, -1, 1]) - mask_gt = tf.reshape(mask_gt, [batch_size * num_instances, -1, 1]) - - loss = self._deepmac_params.classification_loss( - prediction_tensor=mask_logits, - target_tensor=mask_gt, - weights=tf.ones_like(mask_logits)) - - loss = self._aggregate_classification_loss( - loss, mask_gt, mask_logits, 'normalize_auto') - return tf.reshape(loss, [batch_size, num_instances]) - - def _compute_box_consistency_loss( - self, boxes_gt, boxes_for_crop, mask_logits): - """Compute the per-instance box consistency loss. - - Args: - boxes_gt: A [batch_size, num_instances, 4] float tensor of GT boxes. - boxes_for_crop: A [batch_size, num_instances, 4] float tensor of - augmented boxes, to be used when using crop-and-resize based mask head. - mask_logits: A [batch_size, num_instances, height, width] - float tensor of predicted masks. - - Returns: - loss: A [batch_size, num_instances] shaped tensor with the loss for - each instance in the batch. - """ - - shape = tf.shape(mask_logits) - batch_size, num_instances, height, width = ( - shape[0], shape[1], shape[2], shape[3]) - filled_boxes = fill_boxes(boxes_gt, height, width)[:, :, :, :, tf.newaxis] - mask_logits = mask_logits[:, :, :, :, tf.newaxis] - - if self._deepmac_params.predict_full_resolution_masks: - gt_crop = filled_boxes[:, :, :, :, 0] - pred_crop = mask_logits[:, :, :, :, 0] - else: - gt_crop = crop_and_resize_instance_masks( - filled_boxes, boxes_for_crop, self._deepmac_params.mask_size) - pred_crop = crop_and_resize_instance_masks( - mask_logits, boxes_for_crop, self._deepmac_params.mask_size) - - loss = 0.0 - for axis in [2, 3]: - - if self._deepmac_params.box_consistency_tightness: - pred_max_raw = tf.reduce_max(pred_crop, axis=axis) - pred_max_within_box = tf.reduce_max(pred_crop * gt_crop, axis=axis) - box_1d = tf.reduce_max(gt_crop, axis=axis) - pred_max = ((box_1d * pred_max_within_box) + - ((1 - box_1d) * pred_max_raw)) - - else: - pred_max = tf.reduce_max(pred_crop, axis=axis) - - pred_max = pred_max[:, :, :, tf.newaxis] - gt_max = tf.reduce_max(gt_crop, axis=axis)[:, :, :, tf.newaxis] - - flat_pred, batch_size, num_instances = flatten_first2_dims(pred_max) - flat_gt, _, _ = flatten_first2_dims(gt_max) - - # We use flat tensors while calling loss functions because we - # want the loss per-instance to later multiply with the per-instance - # weight. Flattening the first 2 dims allows us to represent each instance - # in each batch as though they were samples in a larger batch. - raw_loss = self._deepmac_params.classification_loss( - prediction_tensor=flat_pred, - target_tensor=flat_gt, - weights=tf.ones_like(flat_pred)) - - agg_loss = self._aggregate_classification_loss( - raw_loss, flat_gt, flat_pred, - self._deepmac_params.box_consistency_loss_normalize) - loss += unpack_first2_dims(agg_loss, batch_size, num_instances) - - return loss - - def _compute_feature_consistency_loss( - self, boxes, consistency_feature_map, mask_logits): - """Compute the per-instance feature consistency loss. - - Args: - boxes: A [batch_size, num_instances, 4] float tensor of GT boxes. - consistency_feature_map: A [batch_size, height, width, 3] - float tensor containing the feature map to use for consistency. - mask_logits: A [batch_size, num_instances, height, width] float tensor of - predicted masks. - - Returns: - loss: A [batch_size, num_instances] shaped tensor with the loss for each - instance fpr each sample in the batch. - """ - - if not self._deepmac_params.predict_full_resolution_masks: - logging.info('Feature consistency is not implemented with RoIAlign ' - ', i.e, fixed sized masks. Returning 0 loss.') - return tf.zeros(tf.shape(boxes)[:2]) - - dilation = self._deepmac_params.feature_consistency_dilation - - height, width = (tf.shape(consistency_feature_map)[1], - tf.shape(consistency_feature_map)[2]) - - comparison = self._deepmac_params.feature_consistency_comparison - if comparison == 'comparison_default_gaussian': - similarity = dilated_cross_pixel_similarity( - consistency_feature_map, dilation=dilation, theta=2.0, - method='gaussian') - elif comparison == 'comparison_normalized_dotprod': - consistency_feature_map = normalize_feature_map(consistency_feature_map) - similarity = dilated_cross_pixel_similarity( - consistency_feature_map, dilation=dilation, theta=2.0, - method='dotprod') - - else: - raise ValueError('Unknown comparison type - %s' % comparison) - - mask_probs = tf.nn.sigmoid(mask_logits) - same_mask_label_probability = dilated_cross_same_mask_label( - mask_probs, dilation=dilation) - same_mask_label_probability = tf.clip_by_value( - same_mask_label_probability, 1e-3, 1.0) - - similarity_mask = ( - similarity > self._deepmac_params.feature_consistency_threshold) - similarity_mask = tf.cast( - similarity_mask[:, :, tf.newaxis, :, :], tf.float32) - per_pixel_loss = -(similarity_mask * - tf.math.log(same_mask_label_probability)) - # TODO(vighneshb) explore if shrinking the box by 1px helps. - box_mask = fill_boxes(boxes, height, width, expand=2) - box_mask_expanded = box_mask[tf.newaxis] - - per_pixel_loss = per_pixel_loss * box_mask_expanded - loss = tf.reduce_sum(per_pixel_loss, axis=[0, 3, 4]) - num_box_pixels = tf.maximum(1.0, tf.reduce_sum(box_mask, axis=[2, 3])) - loss = loss / num_box_pixels - - if tf.keras.backend.learning_phase(): - loss *= _warmup_weight( - current_training_step=self._training_step, - warmup_start=self._deepmac_params.feature_consistency_warmup_start, - warmup_steps=self._deepmac_params.feature_consistency_warmup_steps) - - return loss - - def _self_supervision_loss( - self, predicted_logits, self_supervised_logits, boxes, loss_name): - original_shape = tf.shape(predicted_logits) - batch_size, num_instances = original_shape[0], original_shape[1] - box_mask = fill_boxes(boxes, original_shape[2], original_shape[3]) - - loss_tensor_shape = [batch_size * num_instances, -1, 1] - weights = tf.reshape(box_mask, loss_tensor_shape) - - predicted_logits = tf.reshape(predicted_logits, loss_tensor_shape) - self_supervised_logits = tf.reshape(self_supervised_logits, - loss_tensor_shape) - self_supervised_probs = tf.nn.sigmoid(self_supervised_logits) - predicted_probs = tf.nn.sigmoid(predicted_logits) - num_box_pixels = tf.reduce_sum(weights, axis=[1, 2]) - num_box_pixels = tf.maximum(num_box_pixels, 1.0) - - if loss_name == 'loss_dice': - self_supervised_binary_probs = tf.cast( - self_supervised_logits > 0.0, tf.float32) - - loss_class = losses.WeightedDiceClassificationLoss( - squared_normalization=False) - loss = loss_class(prediction_tensor=predicted_logits, - target_tensor=self_supervised_binary_probs, - weights=weights) - agg_loss = self._aggregate_classification_loss( - loss, gt=self_supervised_probs, pred=predicted_logits, - method='normalize_auto') - elif loss_name == 'loss_mse': - diff = self_supervised_probs - predicted_probs - diff_sq = (diff * diff) - - diff_sq_sum = tf.reduce_sum(diff_sq * weights, axis=[1, 2]) - - agg_loss = diff_sq_sum / num_box_pixels - - elif loss_name == 'loss_kl_div': - loss_class = tf.keras.losses.KLDivergence( - reduction=tf.keras.losses.Reduction.NONE) - predicted_2class_probability = tf.stack( - [predicted_probs, 1 - predicted_probs], axis=2 - ) - target_2class_probability = tf.stack( - [self_supervised_probs, 1 - self_supervised_probs], axis=2 - ) - - loss = loss_class( - y_pred=predicted_2class_probability, - y_true=target_2class_probability) - agg_loss = tf.reduce_sum(loss * weights, axis=[1, 2]) / num_box_pixels - else: - raise RuntimeError('Unknown self-supervision loss %s' % loss_name) - - return tf.reshape(agg_loss, [batch_size, num_instances]) - - def _compute_self_supervised_augmented_loss( - self, original_logits, deaugmented_logits, boxes): - - if deaugmented_logits is None: - logging.info('No self supervised masks provided. ' - 'Returning 0 self-supervised loss,') - return tf.zeros(tf.shape(original_logits)[:2]) - - loss = self._self_supervision_loss( - predicted_logits=original_logits, - self_supervised_logits=deaugmented_logits, - boxes=boxes, - loss_name=self._deepmac_params.augmented_self_supervision_loss) - - if tf.keras.backend.learning_phase(): - loss *= _warmup_weight( - current_training_step=self._training_step, - warmup_start= - self._deepmac_params.augmented_self_supervision_warmup_start, - warmup_steps= - self._deepmac_params.augmented_self_supervision_warmup_steps) - - return loss - - def _compute_pointly_supervised_loss_from_keypoints( - self, mask_logits, keypoints_gt, keypoints_depth_gt): - """Computes per-point mask loss from keypoints. - - Args: - mask_logits: A [batch_size, num_instances, height, width] float tensor - denoting predicted masks. - keypoints_gt: A [batch_size, num_instances, num_keypoints, 2] float tensor - of normalize keypoint coordinates. - keypoints_depth_gt: A [batch_size, num_instances, num_keyponts] float - tensor of keypoint depths. We assume that +1 is foreground and -1 - is background. - Returns: - loss: Pointly supervised loss with shape [batch_size, num_instances]. - """ - - if keypoints_gt is None: - logging.info(('Returning 0 pointly supervised loss because ' - 'keypoints are not given.')) - return tf.zeros(tf.shape(mask_logits)[:2]) - - if keypoints_depth_gt is None: - logging.info(('Returning 0 pointly supervised loss because ' - 'keypoint depths are not given.')) - return tf.zeros(tf.shape(mask_logits)[:2]) - - if not self._deepmac_params.predict_full_resolution_masks: - raise NotImplementedError( - 'Pointly supervised loss not implemented with RoIAlign.') - - num_keypoints = tf.shape(keypoints_gt)[2] - keypoints_nan = tf.math.is_nan(keypoints_gt) - keypoints_gt = tf.where( - keypoints_nan, tf.zeros_like(keypoints_gt), keypoints_gt) - weights = tf.cast( - tf.logical_not(tf.reduce_any(keypoints_nan, axis=3)), tf.float32) - - height, width = tf.shape(mask_logits)[2], tf.shape(mask_logits)[3] - ky, kx = tf.unstack(keypoints_gt, axis=3) - height_f, width_f = tf.cast(height, tf.float32), tf.cast(width, tf.float32) - - ky = tf.clip_by_value(tf.cast(ky * height_f, tf.int32), 0, height - 1) - kx = tf.clip_by_value(tf.cast(kx * width_f, tf.int32), 0, width - 1) - keypoints_gt_int = tf.stack([ky, kx], axis=3) - - mask_logits_flat, batch_size, num_instances = flatten_first2_dims( - mask_logits) - keypoints_gt_int_flat, _, _ = flatten_first2_dims(keypoints_gt_int) - keypoint_depths_flat, _, _ = flatten_first2_dims(keypoints_depth_gt) - weights_flat = tf.logical_not( - tf.reduce_any(keypoints_nan, axis=2)) - weights_flat, _, _ = flatten_first2_dims(weights) - - # TODO(vighneshb): Replace with bilinear interpolation - point_mask_logits = tf.gather_nd( - mask_logits_flat, keypoints_gt_int_flat, batch_dims=1) - - point_mask_logits = tf.reshape( - point_mask_logits, [batch_size * num_instances, num_keypoints, 1]) - - labels = tf.cast(keypoint_depths_flat > 0.0, tf.float32) - labels = tf.reshape( - labels, [batch_size * num_instances, num_keypoints, 1]) - weights_flat = tf.reshape( - weights_flat, [batch_size * num_instances, num_keypoints, 1]) - - loss = self._deepmac_params.classification_loss( - prediction_tensor=point_mask_logits, target_tensor=labels, - weights=weights_flat - ) - - loss = self._aggregate_classification_loss( - loss, gt=labels, pred=point_mask_logits, method='normalize_auto') - - return tf.reshape(loss, [batch_size, num_instances]) - - def _compute_deepmac_losses( - self, boxes, masks_logits, masks_gt, classes, consistency_feature_map, - self_supervised_masks_logits=None, keypoints_gt=None, - keypoints_depth_gt=None): - """Returns the mask loss per instance. - - Args: - boxes: A [batch_size, num_instances, 4] float tensor holding bounding - boxes. The coordinates are in normalized input space. - masks_logits: A [batch_size, num_instances, output_height, output_height]. - float tensor containing the instance mask predictions in their logit - form. - masks_gt: A [batch_size, num_instances, output_height, output_width] float - tensor containing the groundtruth masks. If masks_gt is None, - DEEP_MASK_ESTIMATION is filled with 0s. - classes: A [batch_size, num_instances, num_classes] tensor of one-hot - encoded classes. - consistency_feature_map: [batch_size, output_height, output_width, - channels] float tensor denoting the image to use for consistency. - self_supervised_masks_logits: Optional self-supervised mask logits to - compare against of same shape as mask_logits. - keypoints_gt: A float tensor of shape - [batch_size, num_instances, num_keypoints, 2], representing the points - where we have mask supervision. - keypoints_depth_gt: A float tensor of shape - [batch_size, num_instances, num_keypoints] of keypoint depths which - indicate the mask label at the keypoint locations. depth=+1 is - foreground and depth=-1 is background. - - Returns: - tensor_dict: A dictionary with 4 keys, each mapping to a tensor of shape - [batch_size, num_instances]. The 4 keys are: - - DEEP_MASK_ESTIMATION - - DEEP_MASK_BOX_CONSISTENCY - - DEEP_MASK_FEATURE_CONSISTENCY - - DEEP_MASK_AUGMENTED_SELF_SUPERVISION - - DEEP_MASK_POINTLY_SUPERVISED - """ - - if tf.keras.backend.learning_phase(): - boxes = tf.stop_gradient(boxes) - def jitter_func(boxes): - return preprocessor.random_jitter_boxes( - boxes, self._deepmac_params.max_roi_jitter_ratio, - jitter_mode=self._deepmac_params.roi_jitter_mode) - - boxes_for_crop = tf.map_fn(jitter_func, - boxes, parallel_iterations=128) - else: - boxes_for_crop = boxes - - if masks_gt is not None: - masks_gt = self._get_groundtruth_mask_output( - boxes_for_crop, masks_gt) - mask_prediction_loss = self._compute_mask_prediction_loss( - boxes_for_crop, masks_logits, masks_gt, classes) - - box_consistency_loss = self._compute_box_consistency_loss( - boxes, boxes_for_crop, masks_logits) - - feature_consistency_loss = self._compute_feature_consistency_loss( - boxes, consistency_feature_map, masks_logits) - - self_supervised_loss = self._compute_self_supervised_augmented_loss( - masks_logits, self_supervised_masks_logits, boxes, - ) - - pointly_supervised_loss = ( - self._compute_pointly_supervised_loss_from_keypoints( - masks_logits, keypoints_gt, keypoints_depth_gt)) - - return { - DEEP_MASK_ESTIMATION: mask_prediction_loss, - DEEP_MASK_BOX_CONSISTENCY: box_consistency_loss, - DEEP_MASK_FEATURE_CONSISTENCY: feature_consistency_loss, - DEEP_MASK_AUGMENTED_SELF_SUPERVISION: self_supervised_loss, - DEEP_MASK_POINTLY_SUPERVISED: pointly_supervised_loss, - } - - def _get_lab_image(self, preprocessed_image): - raw_image = self._feature_extractor.preprocess_reverse( - preprocessed_image) - raw_image = raw_image / 255.0 - - if tf_version.is_tf1(): - raise NotImplementedError(('RGB-to-LAB conversion required for the color' - ' consistency loss is not supported in TF1.')) - return tfio.experimental.color.rgb_to_lab(raw_image) - - def _maybe_get_gt_batch(self, field): - """Returns a batch of groundtruth tensors if available, else None.""" - if self.groundtruth_has_field(field): - return _batch_gt_list(self.groundtruth_lists(field)) - else: - return None - - def _get_consistency_feature_map(self, prediction_dict): - - prediction_shape = tf.shape(prediction_dict[MASK_LOGITS_GT_BOXES][0]) - height, width = prediction_shape[2], prediction_shape[3] - - consistency_type = self._deepmac_params.feature_consistency_type - if consistency_type == 'consistency_default_lab': - preprocessed_image = tf.image.resize( - prediction_dict['preprocessed_inputs'], (height, width)) - consistency_feature_map = self._get_lab_image(preprocessed_image) - elif consistency_type == 'consistency_feature_map': - consistency_feature_map = prediction_dict['extracted_features'][-1] - consistency_feature_map = tf.image.resize( - consistency_feature_map, (height, width)) - else: - raise ValueError('Unknown feature consistency type - {}.'.format( - self._deepmac_params.feature_consistency_type)) - - return tf.stop_gradient(consistency_feature_map) - - def _compute_masks_loss(self, prediction_dict): - """Computes the mask loss. - - Args: - prediction_dict: dict from predict() method containing - INSTANCE_EMBEDDING and PIXEL_EMBEDDING prediction. - Both of these are lists of tensors, each of size - [batch_size, height, width, embedding_size]. - - Returns: - loss_dict: A dict mapping string (loss names) to scalar floats. - """ - - allowed_masked_classes_ids = ( - self._deepmac_params.allowed_masked_classes_ids) - - loss_dict = {} - for loss_name in MASK_LOSSES: - loss_dict[loss_name] = 0.0 - - gt_boxes = self._maybe_get_gt_batch(fields.BoxListFields.boxes) - gt_weights = self._maybe_get_gt_batch(fields.BoxListFields.weights) - gt_classes = self._maybe_get_gt_batch(fields.BoxListFields.classes) - gt_masks = self._maybe_get_gt_batch(fields.BoxListFields.masks) - gt_keypoints = self._maybe_get_gt_batch(fields.BoxListFields.keypoints) - gt_depths = self._maybe_get_gt_batch(fields.BoxListFields.keypoint_depths) - - mask_logits_list = prediction_dict[MASK_LOGITS_GT_BOXES] - self_supervised_mask_logits_list = prediction_dict.get( - SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS, - [None] * len(mask_logits_list)) - - assert len(mask_logits_list) == len(self_supervised_mask_logits_list) - consistency_feature_map = self._get_consistency_feature_map(prediction_dict) - - # Iterate over multiple preidctions by backbone (for hourglass length=2) - for (mask_logits, self_supervised_mask_logits) in zip( - mask_logits_list, self_supervised_mask_logits_list): - - # TODO(vighneshb) Add sub-sampling back if required. - _, valid_mask_weights, gt_masks = filter_masked_classes( - allowed_masked_classes_ids, gt_classes, - gt_weights, gt_masks) - - sample_loss_dict = self._compute_deepmac_losses( - boxes=gt_boxes, masks_logits=mask_logits, masks_gt=gt_masks, - classes=gt_classes, consistency_feature_map=consistency_feature_map, - self_supervised_masks_logits=self_supervised_mask_logits, - keypoints_gt=gt_keypoints, keypoints_depth_gt=gt_depths) - - sample_loss_dict[DEEP_MASK_ESTIMATION] *= valid_mask_weights - - for loss_name in WEAK_LOSSES: - sample_loss_dict[loss_name] *= gt_weights - - num_instances = tf.maximum(tf.reduce_sum(gt_weights), 1.0) - num_instances_allowed = tf.maximum( - tf.reduce_sum(valid_mask_weights), 1.0) - - loss_dict[DEEP_MASK_ESTIMATION] += ( - tf.reduce_sum(sample_loss_dict[DEEP_MASK_ESTIMATION]) / - num_instances_allowed) - - for loss_name in WEAK_LOSSES: - loss_dict[loss_name] += (tf.reduce_sum(sample_loss_dict[loss_name]) / - num_instances) - - num_predictions = len(mask_logits_list) - - return dict((key, loss / float(num_predictions)) - for key, loss in loss_dict.items()) - - def loss(self, prediction_dict, true_image_shapes, scope=None): - - losses_dict = super(DeepMACMetaArch, self).loss( - prediction_dict, true_image_shapes, scope) - - if self._deepmac_params is not None: - mask_loss_dict = self._compute_masks_loss( - prediction_dict=prediction_dict) - - for loss_name in MASK_LOSSES: - loss_weight = _get_loss_weight(loss_name, self._deepmac_params) - if loss_weight > 0.0: - losses_dict[LOSS_KEY_PREFIX + '/' + loss_name] = ( - loss_weight * mask_loss_dict[loss_name]) - - return losses_dict - - def postprocess(self, prediction_dict, true_image_shapes, **params): - """Produces boxes given a prediction dict returned by predict(). - - Args: - prediction_dict: a dictionary holding predicted tensors from "predict" - function. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is of - the form [height, width, channels] indicating the shapes of true images - in the resized images, as resized images can be padded with zeros. - **params: Currently ignored. - - Returns: - detections: a dictionary containing the following fields - detection_masks: (Optional) A uint8 tensor of shape [batch, - max_detections, mask_height, mask_width] with masks for each - detection. Background is specified with 0, and foreground is specified - with positive integers (1 for standard instance segmentation mask, and - 1-indexed parts for DensePose task). - And all other fields returned by the super class method. - """ - postprocess_dict = super(DeepMACMetaArch, self).postprocess( - prediction_dict, true_image_shapes, **params) - boxes_strided = postprocess_dict['detection_boxes_strided'] - - if self._deepmac_params is not None: - masks = self._postprocess_masks( - boxes_strided, prediction_dict[INSTANCE_EMBEDDING][-1], - prediction_dict[PIXEL_EMBEDDING][-1]) - postprocess_dict[fields.DetectionResultFields.detection_masks] = masks - - return postprocess_dict - - def _postprocess_masks(self, boxes_output_stride, - instance_embedding, pixel_embedding): - """Postprocess masks with the deep mask network. - - Args: - boxes_output_stride: A [batch_size, num_instances, 4] float tensor - containing the batch of boxes in the absolute output space of the - feature extractor. - instance_embedding: A [batch_size, output_height, output_width, - embedding_size] float tensor containing instance embeddings. - pixel_embedding: A [batch_size, output_height, output_width, - pixel_embedding_size] float tensor containing the per-pixel embedding. - - Returns: - masks: A float tensor of size [batch_size, num_instances, mask_size, - mask_size] containing binary per-box instance masks. - """ - - height, width = (tf.shape(instance_embedding)[1], - tf.shape(instance_embedding)[2]) - boxes = boxes_batch_absolute_to_normalized_coordinates( - boxes_output_stride, height, width) - - mask_logits = self._predict_mask_logits_from_embeddings( - pixel_embedding, instance_embedding, boxes) - - # TODO(vighneshb) Explore sweeping mask thresholds. - - if self._deepmac_params.predict_full_resolution_masks: - - height, width = tf.shape(mask_logits)[1], tf.shape(mask_logits)[2] - height *= self._stride - width *= self._stride - mask_logits = resize_instance_masks(mask_logits, (height, width)) - - mask_logits = crop_and_resize_instance_masks( - mask_logits, boxes, self._deepmac_params.postprocess_crop_size) - - masks_prob = tf.nn.sigmoid(mask_logits) - - return masks_prob - - def _transform_boxes_to_feature_coordinates(self, provided_boxes, - true_image_shapes, - resized_image_shape, - instance_embedding): - """Transforms normalzied boxes to feature map coordinates. - - Args: - provided_boxes: A [batch, num_instances, 4] float tensor containing - normalized bounding boxes. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is of - the form [height, width, channels] indicating the shapes of true images - in the resized images, as resized images can be padded with zeros. - resized_image_shape: A 4D int32 tensor containing shapes of the - preprocessed inputs (N, H, W, C). - instance_embedding: A [batch, output_height, output_width, embedding_size] - float tensor containing instance embeddings. - - Returns: - A float tensor of size [batch, num_instances, 4] containing boxes whose - coordinates have been transformed to the absolute output space of the - feature extractor. - """ - # Input boxes must be normalized. - shape_utils.assert_box_normalized(provided_boxes) - - # Transform the provided boxes to the absolute output space of the feature - # extractor. - height, width = (tf.shape(instance_embedding)[1], - tf.shape(instance_embedding)[2]) - - resized_image_height = resized_image_shape[1] - resized_image_width = resized_image_shape[2] - - def transform_boxes(elems): - boxes_per_image, true_image_shape = elems - blist = box_list.BoxList(boxes_per_image) - # First transform boxes from image space to resized image space since - # there may have paddings in the resized images. - blist = box_list_ops.scale(blist, - true_image_shape[0] / resized_image_height, - true_image_shape[1] / resized_image_width) - # Then transform boxes from resized image space (normalized) to the - # feature map space (absolute). - blist = box_list_ops.to_absolute_coordinates( - blist, height, width, check_range=False) - return blist.get() - - return tf.map_fn( - transform_boxes, [provided_boxes, true_image_shapes], dtype=tf.float32) - - def predict_masks_from_boxes(self, prediction_dict, true_image_shapes, - provided_boxes, **params): - """Produces masks for the provided boxes. - - Args: - prediction_dict: a dictionary holding predicted tensors from "predict" - function. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is of - the form [height, width, channels] indicating the shapes of true images - in the resized images, as resized images can be padded with zeros. - provided_boxes: float tensor of shape [batch, num_boxes, 4] containing - boxes coordinates (normalized) from which we will produce masks. - **params: Currently ignored. - - Returns: - detections: a dictionary containing the following fields - detection_masks: (Optional) A uint8 tensor of shape [batch, - max_detections, mask_height, mask_width] with masks for each - detection. Background is specified with 0, and foreground is specified - with positive integers (1 for standard instance segmentation mask, and - 1-indexed parts for DensePose task). - And all other fields returned by the super class method. - """ - postprocess_dict = super(DeepMACMetaArch, - self).postprocess(prediction_dict, - true_image_shapes, **params) - - instance_embedding = prediction_dict[INSTANCE_EMBEDDING][-1] - resized_image_shapes = shape_utils.combined_static_and_dynamic_shape( - prediction_dict['preprocessed_inputs']) - boxes_strided = self._transform_boxes_to_feature_coordinates( - provided_boxes, true_image_shapes, resized_image_shapes, - instance_embedding) - - if self._deepmac_params is not None: - masks = self._postprocess_masks( - boxes_strided, instance_embedding, - prediction_dict[PIXEL_EMBEDDING][-1]) - postprocess_dict[fields.DetectionResultFields.detection_masks] = masks - - return postprocess_dict diff --git a/research/object_detection/meta_architectures/deepmac_meta_arch_test.py b/research/object_detection/meta_architectures/deepmac_meta_arch_test.py deleted file mode 100644 index 9c83998f79f..00000000000 --- a/research/object_detection/meta_architectures/deepmac_meta_arch_test.py +++ /dev/null @@ -1,1778 +0,0 @@ -"""Tests for google3.third_party.tensorflow_models.object_detection.meta_architectures.deepmac_meta_arch.""" - -import functools -import math -import random -import unittest - -from absl.testing import parameterized -import numpy as np -import tensorflow as tf - -from google.protobuf import text_format -from object_detection.core import losses -from object_detection.core import preprocessor -from object_detection.meta_architectures import center_net_meta_arch -from object_detection.meta_architectures import deepmac_meta_arch -from object_detection.protos import center_net_pb2 -from object_detection.utils import tf_version - - -def _logit(probability): - return math.log(probability / (1. - probability)) - - -LOGIT_HALF = _logit(0.5) -LOGIT_QUARTER = _logit(0.25) - - -class DummyFeatureExtractor(center_net_meta_arch.CenterNetFeatureExtractor): - - def __init__(self, - channel_means, - channel_stds, - bgr_ordering, - num_feature_outputs, - stride): - self._num_feature_outputs = num_feature_outputs - self._stride = stride - super(DummyFeatureExtractor, self).__init__( - channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - - def predict(self): - pass - - def loss(self): - pass - - def postprocess(self): - pass - - def call(self, inputs): - batch_size, input_height, input_width, _ = inputs.shape - fake_output = tf.ones([ - batch_size, input_height // self._stride, input_width // self._stride, - 64 - ], dtype=tf.float32) - return [fake_output] * self._num_feature_outputs - - @property - def out_stride(self): - return self._stride - - @property - def num_feature_outputs(self): - return self._num_feature_outputs - - -class MockMaskNet(tf.keras.layers.Layer): - - def __call__(self, instance_embedding, pixel_embedding, training): - return tf.zeros_like(pixel_embedding[:, :, :, 0]) + 0.9 - - -def build_meta_arch(**override_params): - """Builds the DeepMAC meta architecture.""" - - params = dict( - predict_full_resolution_masks=False, - use_instance_embedding=True, - mask_num_subsamples=-1, - network_type='hourglass10', - use_xy=True, - pixel_embedding_dim=2, - dice_loss_prediction_probability=False, - feature_consistency_threshold=0.5, - use_dice_loss=False, - box_consistency_loss_normalize='normalize_auto', - box_consistency_tightness=False, - task_loss_weight=1.0, - feature_consistency_loss_weight=1.0, - box_consistency_loss_weight=1.0, - num_init_channels=8, - dim=8, - allowed_masked_classes_ids=[], - mask_size=16, - postprocess_crop_size=128, - max_roi_jitter_ratio=0.0, - roi_jitter_mode='default', - feature_consistency_dilation=2, - feature_consistency_warmup_steps=0, - feature_consistency_warmup_start=0, - use_only_last_stage=True, - augmented_self_supervision_max_translation=0.0, - augmented_self_supervision_loss_weight=0.0, - augmented_self_supervision_flip_probability=0.0, - augmented_self_supervision_warmup_start=0, - augmented_self_supervision_warmup_steps=0, - augmented_self_supervision_loss='loss_dice', - augmented_self_supervision_scale_min=1.0, - augmented_self_supervision_scale_max=1.0, - pointly_supervised_keypoint_loss_weight=1.0, - ignore_per_class_box_overlap=False, - feature_consistency_type='consistency_default_lab', - feature_consistency_comparison='comparison_default_gaussian') - - params.update(override_params) - - feature_extractor = DummyFeatureExtractor( - channel_means=(1.0, 2.0, 3.0), - channel_stds=(10., 20., 30.), - bgr_ordering=False, - num_feature_outputs=2, - stride=4) - image_resizer_fn = functools.partial( - preprocessor.resize_to_range, - min_dimension=128, - max_dimension=128, - pad_to_max_dimesnion=True) - - object_center_params = center_net_meta_arch.ObjectCenterParams( - classification_loss=losses.WeightedSigmoidClassificationLoss(), - object_center_loss_weight=1.0, - min_box_overlap_iou=1.0, - max_box_predictions=5, - use_labeled_classes=False) - - use_dice_loss = params.pop('use_dice_loss') - dice_loss_prediction_prob = params.pop('dice_loss_prediction_probability') - if use_dice_loss: - classification_loss = losses.WeightedDiceClassificationLoss( - squared_normalization=False, - is_prediction_probability=dice_loss_prediction_prob) - else: - classification_loss = losses.WeightedSigmoidClassificationLoss() - - deepmac_params = deepmac_meta_arch.DeepMACParams( - classification_loss=classification_loss, - **params - ) - - object_detection_params = center_net_meta_arch.ObjectDetectionParams( - localization_loss=losses.L1LocalizationLoss(), - offset_loss_weight=1.0, - scale_loss_weight=0.1 - ) - - return deepmac_meta_arch.DeepMACMetaArch( - is_training=True, - add_summaries=False, - num_classes=6, - feature_extractor=feature_extractor, - object_center_params=object_center_params, - deepmac_params=deepmac_params, - object_detection_params=object_detection_params, - image_resizer_fn=image_resizer_fn) - - -DEEPMAC_PROTO_TEXT = """ - dim: 153 - task_loss_weight: 5.0 - pixel_embedding_dim: 8 - use_xy: false - use_instance_embedding: false - network_type: "cond_inst3" - - classification_loss { - weighted_dice_classification_loss { - squared_normalization: false - is_prediction_probability: false - } - } - jitter_mode: EXPAND_SYMMETRIC_XY - max_roi_jitter_ratio: 0.0 - predict_full_resolution_masks: true - allowed_masked_classes_ids: [99] - box_consistency_loss_weight: 1.0 - feature_consistency_loss_weight: 1.0 - feature_consistency_threshold: 0.1 - - box_consistency_tightness: false - box_consistency_loss_normalize: NORMALIZE_AUTO - feature_consistency_warmup_steps: 20 - feature_consistency_warmup_start: 10 - use_only_last_stage: false - augmented_self_supervision_warmup_start: 13 - augmented_self_supervision_warmup_steps: 14 - augmented_self_supervision_loss: LOSS_MSE - augmented_self_supervision_loss_weight: 11.0 - augmented_self_supervision_max_translation: 2.5 - augmented_self_supervision_flip_probability: 0.9 - augmented_self_supervision_scale_min: 0.42 - augmented_self_supervision_scale_max: 1.42 - pointly_supervised_keypoint_loss_weight: 0.13 - ignore_per_class_box_overlap: true - feature_consistency_type: CONSISTENCY_FEATURE_MAP - feature_consistency_comparison: COMPARISON_NORMALIZED_DOTPROD - -""" - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class DeepMACUtilsTest(tf.test.TestCase, parameterized.TestCase): - - def test_proto_parse(self): - - proto = center_net_pb2.CenterNet().DeepMACMaskEstimation() - text_format.Parse(DEEPMAC_PROTO_TEXT, proto) - params = deepmac_meta_arch.deepmac_proto_to_params(proto) - self.assertIsInstance(params, deepmac_meta_arch.DeepMACParams) - self.assertEqual(params.num_init_channels, 64) - self.assertEqual(params.dim, 153) - self.assertEqual(params.box_consistency_loss_normalize, 'normalize_auto') - self.assertFalse(params.use_only_last_stage) - self.assertEqual(params.augmented_self_supervision_warmup_start, 13) - self.assertEqual(params.augmented_self_supervision_warmup_steps, 14) - self.assertEqual(params.augmented_self_supervision_loss, 'loss_mse') - self.assertEqual(params.augmented_self_supervision_loss_weight, 11.0) - self.assertEqual(params.augmented_self_supervision_max_translation, 2.5) - self.assertAlmostEqual( - params.augmented_self_supervision_flip_probability, 0.9) - self.assertAlmostEqual( - params.augmented_self_supervision_scale_min, 0.42) - self.assertAlmostEqual( - params.augmented_self_supervision_scale_max, 1.42) - self.assertAlmostEqual( - params.pointly_supervised_keypoint_loss_weight, 0.13) - self.assertTrue(params.ignore_per_class_box_overlap) - self.assertEqual(params.feature_consistency_type, 'consistency_feature_map') - self.assertEqual( - params.feature_consistency_comparison, 'comparison_normalized_dotprod') - - def test_subsample_trivial(self): - """Test subsampling masks.""" - - boxes = np.arange(4).reshape(4, 1) * np.ones((4, 4)) - masks = np.arange(4).reshape(4, 1, 1) * np.ones((4, 32, 32)) - weights = np.ones(4) - classes = tf.one_hot(tf.range(4), depth=4) - - result = deepmac_meta_arch.subsample_instances( - classes, weights, boxes, masks, 4) - self.assertAllClose(result[0], classes) - self.assertAllClose(result[1], weights) - self.assertAllClose(result[2], boxes) - self.assertAllClose(result[3], masks) - - def test_filter_masked_classes(self): - - classes = np.zeros((2, 3, 5), dtype=np.float32) - classes[0, 0] = [1.0, 0.0, 0.0, 0.0, 0.0] - classes[0, 1] = [0.0, 1.0, 0.0, 0.0, 0.0] - classes[0, 2] = [0.0, 0.0, 1.0, 0.0, 0.0] - classes[1, 0] = [0.0, 0.0, 0.0, 1.0, 0.0] - classes[1, 1] = [0.0, 0.0, 0.0, 0.0, 1.0] - classes[1, 2] = [0.0, 0.0, 0.0, 0.0, 1.0] - classes = tf.constant(classes) - - weights = tf.constant([[1.0, 1.0, 1.0], [1.0, 1.0, 0.0]]) - masks = tf.ones((2, 3, 32, 32), dtype=tf.float32) - - classes, weights, masks = deepmac_meta_arch.filter_masked_classes( - [3, 4], classes, weights, masks) - expected_classes = np.zeros((2, 3, 5)) - expected_classes[0, 0] = [0.0, 0.0, 0.0, 0.0, 0.0] - expected_classes[0, 1] = [0.0, 0.0, 0.0, 0.0, 0.0] - expected_classes[0, 2] = [0.0, 0.0, 1.0, 0.0, 0.0] - expected_classes[1, 0] = [0.0, 0.0, 0.0, 1.0, 0.0] - expected_classes[1, 1] = [0.0, 0.0, 0.0, 0.0, 0.0] - expected_classes[1, 2] = [0.0, 0.0, 0.0, 0.0, 0.0] - - self.assertAllClose(expected_classes, classes.numpy()) - self.assertAllClose(np.array(([0.0, 0.0, 1.0], [1.0, 0.0, 0.0])), weights) - - self.assertAllClose(masks[0, 0], np.zeros((32, 32))) - self.assertAllClose(masks[0, 1], np.zeros((32, 32))) - self.assertAllClose(masks[0, 2], np.ones((32, 32))) - self.assertAllClose(masks[1, 0], np.ones((32, 32))) - self.assertAllClose(masks[1, 1], np.zeros((32, 32))) - - def test_fill_boxes(self): - - boxes = tf.constant([[[0., 0., 0.5, 0.5], [0.5, 0.5, 1.0, 1.0]], - [[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) - - filled_boxes = deepmac_meta_arch.fill_boxes(boxes, 32, 32) - expected = np.zeros((2, 2, 32, 32)) - expected[0, 0, :17, :17] = 1.0 - expected[0, 1, 16:, 16:] = 1.0 - expected[1, 0, :, :] = 1.0 - - filled_boxes = filled_boxes.numpy() - self.assertAllClose(expected[0, 0], filled_boxes[0, 0], rtol=1e-3) - self.assertAllClose(expected[0, 1], filled_boxes[0, 1], rtol=1e-3) - self.assertAllClose(expected[1, 0], filled_boxes[1, 0], rtol=1e-3) - - def test_flatten_and_unpack(self): - - t = tf.random.uniform((2, 3, 4, 5, 6)) - flatten = tf.function(deepmac_meta_arch.flatten_first2_dims) - unpack = tf.function(deepmac_meta_arch.unpack_first2_dims) - result, d1, d2 = flatten(t) - result = unpack(result, d1, d2) - self.assertAllClose(result.numpy(), t) - - def test_crop_and_resize_instance_masks(self): - - boxes = tf.zeros((8, 5, 4)) - masks = tf.zeros((8, 5, 128, 128)) - output = deepmac_meta_arch.crop_and_resize_instance_masks( - masks, boxes, 32) - self.assertEqual(output.shape, (8, 5, 32, 32)) - - def test_embedding_projection_prob_shape(self): - dist = deepmac_meta_arch.embedding_projection( - tf.ones((4, 32, 32, 8)), tf.zeros((4, 32, 32, 8))) - self.assertEqual(dist.shape, (4, 32, 32, 1)) - - @parameterized.parameters([1e-20, 1e20]) - def test_embedding_projection_value(self, value): - dist = deepmac_meta_arch.embedding_projection( - tf.zeros((1, 1, 1, 8)), value + tf.zeros((1, 1, 1, 8))).numpy() - max_float = np.finfo(dist.dtype).max - self.assertLess(dist.max(), max_float) - self.assertGreater(dist.max(), -max_float) - - @parameterized.named_parameters( - [('no_conv_shortcut', (False,)), - ('conv_shortcut', (True,))] - ) - def test_res_dense_block(self, conv_shortcut): - - net = deepmac_meta_arch.DenseResidualBlock(32, conv_shortcut) - out = net(tf.zeros((2, 32))) - self.assertEqual(out.shape, (2, 32)) - - @parameterized.parameters( - [4, 8, 20] - ) - def test_dense_resnet(self, num_layers): - - net = deepmac_meta_arch.DenseResNet(num_layers, 16, 8) - out = net(tf.zeros((2, 24))) - self.assertEqual(out.shape, (2, 8)) - - def test_generate_2d_neighbors_shape(self): - - inp = tf.zeros((5, 13, 14, 3)) - out = deepmac_meta_arch.generate_2d_neighbors(inp) - self.assertEqual((8, 5, 13, 14, 3), out.shape) - - def test_generate_2d_neighbors(self): - inp = np.arange(16).reshape(4, 4).astype(np.float32) - inp = tf.stack([inp, inp * 2], axis=2) - inp = tf.reshape(inp, (1, 4, 4, 2)) - out = deepmac_meta_arch.generate_2d_neighbors(inp, dilation=1) - self.assertEqual((8, 1, 4, 4, 2), out.shape) - - for i in range(2): - expected = np.array([0, 1, 2, 4, 6, 8, 9, 10]) * (i + 1) - self.assertAllEqual(out[:, 0, 1, 1, i], expected) - - expected = np.array([1, 2, 3, 5, 7, 9, 10, 11]) * (i + 1) - self.assertAllEqual(out[:, 0, 1, 2, i], expected) - - expected = np.array([4, 5, 6, 8, 10, 12, 13, 14]) * (i + 1) - self.assertAllEqual(out[:, 0, 2, 1, i], expected) - - expected = np.array([5, 6, 7, 9, 11, 13, 14, 15]) * (i + 1) - self.assertAllEqual(out[:, 0, 2, 2, i], expected) - - def test_generate_2d_neighbors_dilation2(self): - inp = np.arange(16).reshape(1, 4, 4, 1).astype(np.float32) - out = deepmac_meta_arch.generate_2d_neighbors(inp, dilation=2) - self.assertEqual((8, 1, 4, 4, 1), out.shape) - - expected = np.array([0, 0, 0, 0, 2, 0, 8, 10]) - self.assertAllEqual(out[:, 0, 0, 0, 0], expected) - - def test_dilated_similarity_shape(self): - fmap = tf.zeros((5, 32, 32, 9)) - similarity = deepmac_meta_arch.dilated_cross_pixel_similarity( - fmap) - self.assertEqual((8, 5, 32, 32), similarity.shape) - - def test_dilated_similarity(self): - - fmap = np.zeros((1, 5, 5, 2), dtype=np.float32) - - fmap[0, 0, 0, :] = 1.0 - fmap[0, 4, 4, :] = 1.0 - - similarity = deepmac_meta_arch.dilated_cross_pixel_similarity( - fmap, theta=1.0, dilation=2) - self.assertAlmostEqual(similarity.numpy()[0, 0, 2, 2], - np.exp(-np.sqrt(2))) - - def test_dilated_same_instance_mask_shape(self): - instances = tf.zeros((2, 5, 32, 32)) - output = deepmac_meta_arch.dilated_cross_same_mask_label(instances) - self.assertEqual((8, 2, 5, 32, 32), output.shape) - - def test_dilated_same_instance_mask(self): - instances = np.zeros((3, 2, 5, 5), dtype=np.float32) - instances[0, 0, 0, 0] = 1.0 - instances[0, 0, 2, 2] = 1.0 - instances[0, 0, 4, 4] = 1.0 - - instances[2, 0, 0, 0] = 1.0 - instances[2, 0, 2, 2] = 1.0 - instances[2, 0, 4, 4] = 0.0 - - output = deepmac_meta_arch.dilated_cross_same_mask_label(instances).numpy() - self.assertAllClose(np.ones((8, 2, 5, 5)), output[:, 1, :, :]) - self.assertAllClose([1, 0, 0, 0, 0, 0, 0, 1], output[:, 0, 0, 2, 2]) - self.assertAllClose([1, 0, 0, 0, 0, 0, 0, 0], output[:, 2, 0, 2, 2]) - - def test_per_pixel_single_conv_multiple_instance(self): - - inp = tf.zeros((5, 32, 32, 7)) - params = tf.zeros((5, 7*8 + 8)) - - out = deepmac_meta_arch._per_pixel_single_conv(inp, params, 8) - self.assertEqual(out.shape, (5, 32, 32, 8)) - - def test_per_pixel_conditional_conv_error(self): - - with self.assertRaises(ValueError): - deepmac_meta_arch.per_pixel_conditional_conv( - tf.zeros((10, 32, 32, 8)), tf.zeros((10, 2)), 8, 3) - - def test_per_pixel_conditional_conv_error_tf_func(self): - - with self.assertRaises(ValueError): - func = tf.function(deepmac_meta_arch.per_pixel_conditional_conv) - func(tf.zeros((10, 32, 32, 8)), tf.zeros((10, 2)), 8, 3) - - def test_per_pixel_conditional_conv_depth1_error(self): - - with self.assertRaises(ValueError): - _ = deepmac_meta_arch.per_pixel_conditional_conv( - tf.zeros((10, 32, 32, 7)), tf.zeros((10, 8)), 99, 1) - - @parameterized.parameters([ - { - 'num_input_channels': 7, - 'instance_embedding_dim': 8, - 'channels': 7, - 'depth': 1 - }, - { - 'num_input_channels': 7, - 'instance_embedding_dim': 82, - 'channels': 9, - 'depth': 2 - }, - { # From https://arxiv.org/abs/2003.05664 - 'num_input_channels': 10, - 'instance_embedding_dim': 169, - 'channels': 8, - 'depth': 3 - }, - { - 'num_input_channels': 8, - 'instance_embedding_dim': 433, - 'channels': 16, - 'depth': 3 - }, - { - 'num_input_channels': 8, - 'instance_embedding_dim': 1377, - 'channels': 32, - 'depth': 3 - }, - { - 'num_input_channels': 8, - 'instance_embedding_dim': 4801, - 'channels': 64, - 'depth': 3 - }, - ]) - def test_per_pixel_conditional_conv_shape( - self, num_input_channels, instance_embedding_dim, channels, depth): - - out = deepmac_meta_arch.per_pixel_conditional_conv( - tf.zeros((10, 32, 32, num_input_channels)), - tf.zeros((10, instance_embedding_dim)), channels, depth) - - self.assertEqual(out.shape, (10, 32, 32, 1)) - - def test_per_pixel_conditional_conv_value_depth1(self): - - input_tensor = tf.constant(np.array([1, 2, 3])) - input_tensor = tf.reshape(input_tensor, (1, 1, 1, 3)) - instance_embedding = tf.constant( - np.array([1, 10, 100, 1000])) - instance_embedding = tf.reshape(instance_embedding, (1, 4)) - out = deepmac_meta_arch.per_pixel_conditional_conv( - input_tensor, instance_embedding, channels=3, depth=1) - - expected_output = np.array([1321]) - expected_output = np.reshape(expected_output, (1, 1, 1, 1)) - self.assertAllClose(expected_output, out) - - def test_per_pixel_conditional_conv_value_depth2_single(self): - - input_tensor = tf.constant(np.array([2])) - input_tensor = tf.reshape(input_tensor, (1, 1, 1, 1)) - instance_embedding = tf.constant( - np.array([-2, 3, 100, 5])) - instance_embedding = tf.reshape(instance_embedding, (1, 4)) - out = deepmac_meta_arch.per_pixel_conditional_conv( - input_tensor, instance_embedding, channels=1, depth=2) - - expected_output = np.array([5]) - expected_output = np.reshape(expected_output, (1, 1, 1, 1)) - self.assertAllClose(expected_output, out) - - def test_per_pixel_conditional_conv_value_depth2_identity(self): - - input_tensor = tf.constant(np.array([1, 2])) - input_tensor = tf.reshape(input_tensor, (1, 1, 1, 2)) - instance_embedding = tf.constant( - np.array([1, 0, 0, 1, 1, -3, 5, 100, -9])) - instance_embedding = tf.reshape( - instance_embedding, (1, 9)) - out = deepmac_meta_arch.per_pixel_conditional_conv( - input_tensor, instance_embedding, channels=2, depth=2) - - expected_output = np.array([1]) - expected_output = np.reshape(expected_output, (1, 1, 1, 1)) - self.assertAllClose(expected_output, out) - - def test_per_instance_no_class_overlap(self): - boxes = tf.constant([[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.4, 0.4]], - [[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]], - dtype=tf.float32) - classes = tf.constant([[[0, 1, 0], [0, 1, 0]], [[0, 1, 0], [1, 0, 0]]], - dtype=tf.float32) - output = deepmac_meta_arch.per_instance_no_class_overlap( - classes, boxes, 2, 2) - self.assertEqual(output.shape, (2, 2, 2, 2)) - self.assertAllClose(output[1], np.ones((2, 2, 2))) - self.assertAllClose(output[0, 1], [[0., 1.0], [1.0, 1.0]]) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class DeepMACMaskHeadTest(tf.test.TestCase, parameterized.TestCase): - - def test_mask_network_params_resnet4(self): - net = deepmac_meta_arch.MaskHeadNetwork('resnet4', num_init_channels=8) - _ = net(tf.zeros((2, 16)), tf.zeros((2, 32, 32, 16)), training=True) - - trainable_params = tf.reduce_sum([tf.reduce_prod(tf.shape(w)) for w in - net.trainable_weights]) - - self.assertEqual(trainable_params.numpy(), 8665) - - def test_mask_network_embedding_projection_small(self): - - net = deepmac_meta_arch.MaskHeadNetwork( - 'embedding_projection', num_init_channels=-1, - use_instance_embedding=False) - call_func = tf.function(net.__call__) - - out = call_func(1e6 + tf.zeros((2, 7)), - tf.zeros((2, 32, 32, 7)), training=True) - self.assertEqual(out.shape, (2, 32, 32)) - self.assertAllGreater(out.numpy(), -np.inf) - self.assertAllLess(out.numpy(), np.inf) - - @parameterized.parameters([ - { - 'mask_net': 'resnet4', - 'mask_net_channels': 8, - 'instance_embedding_dim': 4, - 'input_channels': 16, - 'use_instance_embedding': False - }, - { - 'mask_net': 'hourglass10', - 'mask_net_channels': 8, - 'instance_embedding_dim': 4, - 'input_channels': 16, - 'use_instance_embedding': False - }, - { - 'mask_net': 'hourglass20', - 'mask_net_channels': 8, - 'instance_embedding_dim': 4, - 'input_channels': 16, - 'use_instance_embedding': False - }, - { - 'mask_net': 'cond_inst3', - 'mask_net_channels': 8, - 'instance_embedding_dim': 153, - 'input_channels': 8, - 'use_instance_embedding': False - }, - { - 'mask_net': 'cond_inst3', - 'mask_net_channels': 8, - 'instance_embedding_dim': 169, - 'input_channels': 10, - 'use_instance_embedding': False - }, - { - 'mask_net': 'cond_inst1', - 'mask_net_channels': 8, - 'instance_embedding_dim': 9, - 'input_channels': 8, - 'use_instance_embedding': False - }, - { - 'mask_net': 'cond_inst2', - 'mask_net_channels': 8, - 'instance_embedding_dim': 81, - 'input_channels': 8, - 'use_instance_embedding': False - }, - ]) - def test_mask_network(self, mask_net, mask_net_channels, - instance_embedding_dim, input_channels, - use_instance_embedding): - - net = deepmac_meta_arch.MaskHeadNetwork( - mask_net, num_init_channels=mask_net_channels, - use_instance_embedding=use_instance_embedding) - call_func = tf.function(net.__call__) - - out = call_func(tf.zeros((2, instance_embedding_dim)), - tf.zeros((2, 32, 32, input_channels)), training=True) - self.assertEqual(out.shape, (2, 32, 32)) - self.assertAllGreater(out.numpy(), -np.inf) - self.assertAllLess(out.numpy(), np.inf) - - out = call_func(tf.zeros((2, instance_embedding_dim)), - tf.zeros((2, 32, 32, input_channels)), training=True) - self.assertEqual(out.shape, (2, 32, 32)) - - out = call_func(tf.zeros((0, instance_embedding_dim)), - tf.zeros((0, 32, 32, input_channels)), training=True) - self.assertEqual(out.shape, (0, 32, 32)) - - @parameterized.parameters( - [ - dict(x=4, y=4, height=4, width=4), - dict(x=1, y=2, height=3, width=4), - dict(x=14, y=14, height=5, width=5), - ] - ) - def test_transform_images_and_boxes_identity(self, x, y, height, width): - images = np.zeros((1, 32, 32, 3), dtype=np.float32) - images[:, y:y + height, x:x + width, :] = 1.0 - boxes = tf.constant([[[y / 32., x / 32., - y / 32. + height/32, x/32. + width / 32]]]) - - zeros = tf.zeros(1) - ones = tf.ones(1) - falses = tf.zeros(1, dtype=tf.bool) - images = tf.constant(images) - images_out, boxes_out = deepmac_meta_arch.transform_images_and_boxes( - images, boxes, zeros, zeros, ones, ones, falses) - self.assertAllClose(images, images_out) - self.assertAllClose(boxes, boxes_out) - - coords = np.argwhere(images_out.numpy()[0, :, :, 0] > 0.5) - self.assertEqual(np.min(coords[:, 0]), y) - self.assertEqual(np.min(coords[:, 1]), x) - self.assertEqual(np.max(coords[:, 0]), y + height - 1) - self.assertEqual(np.max(coords[:, 1]), x + width - 1) - - def test_transform_images_and_boxes(self): - images = np.zeros((2, 32, 32, 3), dtype=np.float32) - images[:, 14:19, 14:19, :] = 1.0 - boxes = tf.constant( - [[[14.0 / 32, 14.0 / 32, 18.0 / 32, 18.0 / 32]] * 2] * 2) - flip = tf.constant([False, False]) - - scale_y0 = 2.0 - translate_y0 = 1.0 - scale_x0 = 4.0 - translate_x0 = 4.0 - - scale_y1 = 3.0 - translate_y1 = 3.0 - scale_x1 = 0.5 - translate_x1 = 2.0 - ty = tf.constant([translate_y0/32, translate_y1/32]) - sy = tf.constant([1./scale_y0, 1.0 / scale_y1]) - - tx = tf.constant([translate_x0/32, translate_x1/32]) - sx = tf.constant([1 / scale_x0, 1.0 / scale_x1]) - - images = tf.constant(images) - images_out, boxes_out = deepmac_meta_arch.transform_images_and_boxes( - images, boxes, tx=tx, ty=ty, sx=sx, sy=sy, flip=flip) - - boxes_out = boxes_out.numpy() * 32 - coords = np.argwhere(images_out[0, :, :, 0] >= 0.9) - ymin = np.min(coords[:, 0]) - ymax = np.max(coords[:, 0]) - xmin = np.min(coords[:, 1]) - xmax = np.max(coords[:, 1]) - - self.assertAlmostEqual( - ymin, 16 - 2*scale_y0 + translate_y0, delta=1) - self.assertAlmostEqual( - ymax, 16 + 2*scale_y0 + translate_y0, delta=1) - self.assertAlmostEqual( - xmin, 16 - 2*scale_x0 + translate_x0, delta=1) - self.assertAlmostEqual( - xmax, 16 + 2*scale_x0 + translate_x0, delta=1) - self.assertAlmostEqual(ymin, boxes_out[0, 0, 0], delta=1) - self.assertAlmostEqual(xmin, boxes_out[0, 0, 1], delta=1) - self.assertAlmostEqual(ymax, boxes_out[0, 0, 2], delta=1) - self.assertAlmostEqual(xmax, boxes_out[0, 0, 3], delta=1) - - coords = np.argwhere(images_out[1, :, :, 0] >= 0.9) - ymin = np.min(coords[:, 0]) - ymax = np.max(coords[:, 0]) - xmin = np.min(coords[:, 1]) - xmax = np.max(coords[:, 1]) - - self.assertAlmostEqual( - ymin, 16 - 2*scale_y1 + translate_y1, delta=1) - self.assertAlmostEqual( - ymax, 16 + 2*scale_y1 + translate_y1, delta=1) - self.assertAlmostEqual( - xmin, 16 - 2*scale_x1 + translate_x1, delta=1) - self.assertAlmostEqual( - xmax, 16 + 2*scale_x1 + translate_x1, delta=1) - self.assertAlmostEqual(ymin, boxes_out[1, 0, 0], delta=1) - self.assertAlmostEqual(xmin, boxes_out[1, 0, 1], delta=1) - self.assertAlmostEqual(ymax, boxes_out[1, 0, 2], delta=1) - self.assertAlmostEqual(xmax, boxes_out[1, 0, 3], delta=1) - - def test_transform_images_and_boxes_flip(self): - images = np.zeros((2, 2, 2, 1), dtype=np.float32) - images[0, :, :, 0] = [[1, 2], [3, 4]] - images[1, :, :, 0] = [[1, 2], [3, 4]] - images = tf.constant(images) - - boxes = tf.constant( - [[[0.1, 0.2, 0.3, 0.4]], [[0.1, 0.2, 0.3, 0.4]]], dtype=tf.float32) - - tx = ty = tf.zeros([2], dtype=tf.float32) - sx = sy = tf.ones([2], dtype=tf.float32) - flip = tf.constant([True, False]) - - output_images, output_boxes = deepmac_meta_arch.transform_images_and_boxes( - images, boxes, tx, ty, sx, sy, flip) - - expected_images = np.zeros((2, 2, 2, 1), dtype=np.float32) - expected_images[0, :, :, 0] = [[2, 1], [4, 3]] - expected_images[1, :, :, 0] = [[1, 2], [3, 4]] - self.assertAllClose(output_boxes, - [[[0.1, 0.6, 0.3, 0.8]], [[0.1, 0.2, 0.3, 0.4]]]) - self.assertAllClose(expected_images, output_images) - - def test_transform_images_and_boxes_tf_function(self): - func = tf.function(deepmac_meta_arch.transform_images_and_boxes) - - output, _ = func(images=tf.zeros((2, 32, 32, 3)), boxes=tf.zeros((2, 5, 4)), - tx=tf.zeros(2), ty=tf.zeros(2), - sx=tf.ones(2), sy=tf.ones(2), - flip=tf.zeros(2, dtype=tf.bool)) - self.assertEqual(output.shape, (2, 32, 32, 3)) - - def test_transform_instance_masks(self): - instance_masks = np.zeros((2, 10, 32, 32), dtype=np.float32) - instance_masks[0, 0, 1, 1] = 1 - instance_masks[0, 1, 1, 1] = 1 - - instance_masks[1, 0, 2, 2] = 1 - instance_masks[1, 1, 2, 2] = 1 - - tx = ty = tf.constant([1., 2.]) / 32.0 - sx = sy = tf.ones(2, dtype=tf.float32) - flip = tf.zeros(2, dtype=tf.bool) - - instance_masks = deepmac_meta_arch.transform_instance_masks( - instance_masks, tx, ty, sx, sy, flip=flip) - self.assertEqual(instance_masks.shape, (2, 10, 32, 32)) - self.assertAlmostEqual( - instance_masks[0].numpy().sum(), 2.0) - self.assertGreater( - instance_masks[0, 0, 2, 2].numpy(), 0.5) - self.assertGreater( - instance_masks[0, 1, 2, 2].numpy(), 0.5) - - self.assertAlmostEqual( - instance_masks[1].numpy().sum(), 2.0) - self.assertGreater( - instance_masks[1, 0, 4, 4].numpy(), 0.5) - self.assertGreater( - instance_masks[1, 1, 4, 4].numpy(), 0.5) - - def test_augment_image_and_deaugment_mask(self): - - img = np.zeros((1, 32, 32, 3), dtype=np.float32) - - img[0, 10:12, 10:12, :] = 1.0 - - tx = ty = tf.constant([1.]) / 32.0 - sx = sy = tf.constant([1.0 / 2.0]) - flip = tf.constant([False]) - - img = tf.constant(img) - img_t, _ = deepmac_meta_arch.transform_images_and_boxes( - images=img, boxes=None, tx=tx, ty=ty, sx=sx, sy=sy, flip=flip) - self.assertAlmostEqual(img_t.numpy().sum(), 16 * 3) - - # Converting channels of the image to instances. - masks = tf.transpose(img_t, (0, 3, 1, 2)) - - masks_t = deepmac_meta_arch.transform_instance_masks( - masks, tx=-tx, ty=-ty, sx=1.0/sx, sy=1.0/sy, flip=flip) - - self.assertAlmostEqual(masks_t.numpy().sum(), 4 * 3) - - coords = np.argwhere(masks_t[0, 0, :, :] >= 0.5) - - self.assertAlmostEqual(np.min(coords[:, 0]), 10, delta=1) - self.assertAlmostEqual(np.max(coords[:, 0]), 12, delta=1) - self.assertAlmostEqual(np.min(coords[:, 1]), 10, delta=1) - self.assertAlmostEqual(np.max(coords[:, 1]), 12, delta=1) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class DeepMACMetaArchTest(tf.test.TestCase, parameterized.TestCase): - - # TODO(vighneshb): Add batch_size > 1 tests for loss functions. - - def setUp(self): # pylint:disable=g-missing-super-call - self.model = build_meta_arch() - - def test_get_mask_head_input(self): - - boxes = tf.constant([[[0., 0., 0.25, 0.25], [0.75, 0.75, 1.0, 1.0]], - [[0., 0., 0.25, 0.25], [0.75, 0.75, 1.0, 1.0]]], - dtype=tf.float32) - - pixel_embedding = np.zeros((2, 32, 32, 4), dtype=np.float32) - pixel_embedding[0, :16, :16] = 1.0 - pixel_embedding[0, 16:, 16:] = 2.0 - pixel_embedding[1, :16, :16] = 3.0 - pixel_embedding[1, 16:, 16:] = 4.0 - - pixel_embedding = tf.constant(pixel_embedding) - - mask_inputs = self.model._get_mask_head_input(boxes, pixel_embedding) - self.assertEqual(mask_inputs.shape, (2, 2, 16, 16, 6)) - - y_grid, x_grid = tf.meshgrid(np.linspace(-1.0, 1.0, 16), - np.linspace(-1.0, 1.0, 16), indexing='ij') - - for i, j in ([0, 0], [0, 1], [1, 0], [1, 1]): - self.assertAllClose(y_grid, mask_inputs[i, j, :, :, 0]) - self.assertAllClose(x_grid, mask_inputs[i, j, :, :, 1]) - - zeros = np.zeros((16, 16, 4)) - self.assertAllClose(zeros + 1, mask_inputs[0, 0, :, :, 2:]) - self.assertAllClose(zeros + 2, mask_inputs[0, 1, :, :, 2:]) - self.assertAllClose(zeros + 3, mask_inputs[1, 0, :, :, 2:]) - self.assertAllClose(zeros + 4, mask_inputs[1, 1, :, :, 2:]) - - def test_get_mask_head_input_no_crop_resize(self): - - model = build_meta_arch(predict_full_resolution_masks=True) - boxes = tf.constant([[[0., 0., 1.0, 1.0], [0.0, 0.0, 0.5, 1.0]], - [[0.5, 0.5, 1.0, 1.0], [0.0, 0.0, 0.0, 0.0]]]) - - pixel_embedding_np = np.random.randn(2, 32, 32, 4).astype(np.float32) - pixel_embedding = tf.constant(pixel_embedding_np) - - mask_inputs = model._get_mask_head_input(boxes, pixel_embedding) - self.assertEqual(mask_inputs.shape, (2, 2, 32, 32, 6)) - - y_grid, x_grid = tf.meshgrid(np.linspace(.0, 1.0, 32), - np.linspace(.0, 1.0, 32), indexing='ij') - - self.assertAllClose(y_grid - 0.5, mask_inputs[0, 0, :, :, 0]) - self.assertAllClose(x_grid - 0.5, mask_inputs[0, 0, :, :, 1]) - - self.assertAllClose(y_grid - 0.25, mask_inputs[0, 1, :, :, 0]) - self.assertAllClose(x_grid - 0.5, mask_inputs[0, 1, :, :, 1]) - - self.assertAllClose(y_grid - 0.75, mask_inputs[1, 0, :, :, 0]) - self.assertAllClose(x_grid - 0.75, mask_inputs[1, 0, :, :, 1]) - - self.assertAllClose(y_grid, mask_inputs[1, 1, :, :, 0]) - self.assertAllClose(x_grid, mask_inputs[1, 1, :, :, 1]) - - def test_get_instance_embeddings(self): - - embeddings = np.zeros((2, 32, 32, 2)) - embeddings[0, 8, 8] = 1.0 - embeddings[0, 24, 16] = 2.0 - embeddings[1, 8, 16] = 3.0 - embeddings = tf.constant(embeddings) - - boxes = np.zeros((2, 2, 4), dtype=np.float32) - boxes[0, 0] = [0.0, 0.0, 0.5, 0.5] - boxes[0, 1] = [0.5, 0.0, 1.0, 1.0] - boxes[1, 0] = [0.0, 0.0, 0.5, 1.0] - - boxes = tf.constant(boxes) - - center_embeddings = self.model._get_instance_embeddings(boxes, embeddings) - - self.assertAllClose(center_embeddings[0, 0], [1.0, 1.0]) - self.assertAllClose(center_embeddings[0, 1], [2.0, 2.0]) - self.assertAllClose(center_embeddings[1, 0], [3.0, 3.0]) - - def test_get_groundtruth_mask_output(self): - - boxes = np.zeros((2, 2, 4)) - masks = np.zeros((2, 2, 32, 32)) - - boxes[0, 0] = [0.0, 0.0, 0.25, 0.25] - boxes[0, 1] = [0.75, 0.75, 1.0, 1.0] - boxes[1, 0] = [0.0, 0.0, 0.5, 1.0] - masks = np.zeros((2, 2, 32, 32), dtype=np.float32) - masks[0, 0, :16, :16] = 0.5 - masks[0, 1, 16:, 16:] = 0.1 - masks[1, 0, :17, :] = 0.3 - masks = self.model._get_groundtruth_mask_output(boxes, masks) - self.assertEqual(masks.shape, (2, 2, 16, 16)) - - self.assertAllClose(masks[0, 0], np.zeros((16, 16)) + 0.5) - self.assertAllClose(masks[0, 1], np.zeros((16, 16)) + 0.1) - self.assertAllClose(masks[1, 0], np.zeros((16, 16)) + 0.3) - - def test_get_groundtruth_mask_output_no_crop_resize(self): - - model = build_meta_arch(predict_full_resolution_masks=True) - boxes = tf.zeros((2, 5, 4)) - masks = tf.ones((2, 5, 32, 32)) - masks = model._get_groundtruth_mask_output(boxes, masks) - self.assertAllClose(masks, np.ones((2, 5, 32, 32))) - - def test_predict(self): - - tf.keras.backend.set_learning_phase(True) - self.model.provide_groundtruth( - groundtruth_boxes_list=[tf.convert_to_tensor([[0., 0., 1., 1.]] * 5)], - groundtruth_classes_list=[tf.one_hot([1, 0, 1, 1, 1], depth=6)], - groundtruth_weights_list=[tf.ones(5)], - groundtruth_masks_list=[tf.ones((5, 32, 32))]) - prediction = self.model.predict(tf.zeros((1, 32, 32, 3)), None) - self.assertEqual(prediction['MASK_LOGITS_GT_BOXES'][0].shape, - (1, 5, 16, 16)) - - def test_predict_self_supervised_deaugmented_mask_logits(self): - - tf.keras.backend.set_learning_phase(True) - model = build_meta_arch( - augmented_self_supervision_loss_weight=1.0, - predict_full_resolution_masks=True) - - model.provide_groundtruth( - groundtruth_boxes_list=[tf.convert_to_tensor([[0., 0., 1., 1.]] * 5)], - groundtruth_classes_list=[tf.one_hot([1, 0, 1, 1, 1], depth=6)], - groundtruth_weights_list=[tf.ones(5)], - groundtruth_masks_list=[tf.ones((5, 32, 32))]) - prediction = model.predict(tf.zeros((1, 32, 32, 3)), None) - self.assertEqual(prediction['MASK_LOGITS_GT_BOXES'][0].shape, - (1, 5, 8, 8)) - self.assertEqual( - prediction['SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS'][0].shape, - (1, 5, 8, 8)) - - def test_loss(self): - - model = build_meta_arch() - boxes = tf.constant([[[0.0, 0.0, 0.25, 0.25], [0.75, 0.75, 1.0, 1.0]]]) - masks = np.zeros((1, 2, 32, 32), dtype=np.float32) - masks[0, 0, :16, :16] = 1.0 - masks[0, 1, 16:, 16:] = 1.0 - masks_pred = tf.fill((1, 2, 32, 32), 0.9) - classes = tf.zeros((1, 2, 5)) - - loss_dict = model._compute_deepmac_losses( - boxes, masks_pred, masks, classes, tf.zeros((1, 16, 16, 3))) - self.assertAllClose( - loss_dict[deepmac_meta_arch.DEEP_MASK_ESTIMATION], - np.zeros((1, 2)) - tf.math.log(tf.nn.sigmoid(0.9))) - - def test_loss_no_crop_resize(self): - - model = build_meta_arch(predict_full_resolution_masks=True) - boxes = tf.constant([[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]]) - masks = tf.ones((1, 2, 128, 128), dtype=tf.float32) - masks_pred = tf.fill((1, 2, 32, 32), 0.9) - classes = tf.zeros((1, 2, 5)) - - loss_dict = model._compute_deepmac_losses( - boxes, masks_pred, masks, classes, tf.zeros((1, 32, 32, 3))) - self.assertAllClose( - loss_dict[deepmac_meta_arch.DEEP_MASK_ESTIMATION], - np.zeros((1, 2)) - tf.math.log(tf.nn.sigmoid(0.9))) - - def test_loss_no_crop_resize_dice(self): - - model = build_meta_arch(predict_full_resolution_masks=True, - use_dice_loss=True) - boxes = tf.constant([[[0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 1.0, 1.0]]]) - masks = np.ones((1, 2, 128, 128), dtype=np.float32) - masks = tf.constant(masks) - masks_pred = tf.fill((1, 2, 32, 32), 0.9) - classes = tf.zeros((1, 2, 5)) - - loss_dict = model._compute_deepmac_losses( - boxes, masks_pred, masks, classes, tf.zeros((1, 32, 32, 3))) - pred = tf.nn.sigmoid(0.9) - expected = (1.0 - ((2.0 * pred) / (1.0 + pred))) - self.assertAllClose(loss_dict[deepmac_meta_arch.DEEP_MASK_ESTIMATION], - [[expected, expected]], rtol=1e-3) - - def test_empty_masks(self): - - boxes = tf.zeros([1, 0, 4]) - masks = tf.zeros([1, 0, 128, 128]) - classes = tf.zeros((1, 2, 5)) - - loss_dict = self.model._compute_deepmac_losses( - boxes, masks, masks, classes, - tf.zeros((1, 16, 16, 3))) - self.assertEqual(loss_dict[deepmac_meta_arch.DEEP_MASK_ESTIMATION].shape, - (1, 0)) - - def test_postprocess(self): - model = build_meta_arch() - model._mask_net = MockMaskNet() - boxes = np.zeros((2, 3, 4), dtype=np.float32) - boxes[:, :, [0, 2]] = 0.0 - boxes[:, :, [1, 3]] = 8.0 - boxes = tf.constant(boxes) - - masks = model._postprocess_masks( - boxes, tf.zeros((2, 32, 32, 2)), tf.zeros((2, 32, 32, 2))) - prob = tf.nn.sigmoid(0.9).numpy() - self.assertAllClose(masks, prob * np.ones((2, 3, 16, 16))) - - def test_postprocess_emb_proj(self): - model = build_meta_arch(network_type='embedding_projection', - use_instance_embedding=False, - use_xy=False, pixel_embedding_dim=8, - use_dice_loss=True, - dice_loss_prediction_probability=True) - boxes = np.zeros((2, 3, 4), dtype=np.float32) - boxes[:, :, [0, 2]] = 0.0 - boxes[:, :, [1, 3]] = 8.0 - boxes = tf.constant(boxes) - - masks = model._postprocess_masks( - boxes, tf.zeros((2, 32, 32, 2)), tf.zeros((2, 32, 32, 2))) - self.assertEqual(masks.shape, (2, 3, 16, 16)) - - def test_postprocess_emb_proj_fullres(self): - model = build_meta_arch(network_type='embedding_projection', - predict_full_resolution_masks=True, - use_instance_embedding=False, - pixel_embedding_dim=8, use_xy=False, - use_dice_loss=True) - boxes = np.zeros((2, 3, 4), dtype=np.float32) - boxes = tf.constant(boxes) - - masks = model._postprocess_masks( - boxes, tf.zeros((2, 32, 32, 2)), tf.zeros((2, 32, 32, 2))) - self.assertEqual(masks.shape, (2, 3, 128, 128)) - - def test_postprocess_no_crop_resize_shape(self): - - model = build_meta_arch(predict_full_resolution_masks=True) - model._mask_net = MockMaskNet() - boxes = np.zeros((2, 3, 4), dtype=np.float32) - boxes[:, :, [0, 2]] = 0.0 - boxes[:, :, [1, 3]] = 8.0 - boxes = tf.constant(boxes) - - masks = model._postprocess_masks( - boxes, tf.zeros((2, 32, 32, 2)), tf.zeros((2, 32, 32, 2))) - prob = tf.nn.sigmoid(0.9).numpy() - self.assertAllClose(masks, prob * np.ones((2, 3, 128, 128))) - - def test_transform_boxes_to_feature_coordinates(self): - batch_size = 2 - model = build_meta_arch() - model._mask_net = MockMaskNet() - boxes = np.zeros((batch_size, 3, 4), dtype=np.float32) - boxes[:, :, [0, 2]] = 0.1 - boxes[:, :, [1, 3]] = 0.5 - boxes = tf.constant(boxes) - true_image_shapes = tf.constant([ - [64, 32, 3], # Image 1 is padded during resizing. - [64, 64, 3], # Image 2 is not padded. - ]) - resized_image_height = 64 - resized_image_width = 64 - resized_image_shape = [ - batch_size, resized_image_height, resized_image_width, 3 - ] - - feature_map_height = 32 - feature_map_width = 32 - instance_embedding = tf.zeros( - (batch_size, feature_map_height, feature_map_width, 2)) - - expected_boxes = np.array([ - [ # Image 1 - # 0.1 * (64 / resized_image_height) * feature_map_height -> 3.2 - # 0.5 * (32 / resized_image_width) * feature_map_width -> 8.0 - [3.2, 8., 3.2, 8.], - [3.2, 8., 3.2, 8.], - [3.2, 8., 3.2, 8.], - ], - [ # Image 2 - # 0.1 * (64 / resized_image_height) * feature_map_height -> 3.2 - # 0.5 * (64 / resized_image_width) * feature_map_width -> 16 - [3.2, 16., 3.2, 16.], - [3.2, 16., 3.2, 16.], - [3.2, 16., 3.2, 16.], - ], - ]) - - box_strided = model._transform_boxes_to_feature_coordinates( - boxes, true_image_shapes, resized_image_shape, instance_embedding) - self.assertAllClose(box_strided, expected_boxes) - - def test_fc_tf_function(self): - - net = deepmac_meta_arch.MaskHeadNetwork('fully_connected', 8, mask_size=32) - call_func = tf.function(net.__call__) - - out = call_func(tf.zeros((2, 4)), tf.zeros((2, 32, 32, 8)), training=True) - self.assertEqual(out.shape, (2, 32, 32)) - - def test_box_consistency_loss(self): - - boxes_gt = tf.constant([[[0., 0., 0.49, 1.0]]]) - boxes_jittered = tf.constant([[[0.0, 0.0, 1.0, 1.0]]]) - - mask_prediction = np.zeros((1, 1, 32, 32)).astype(np.float32) - mask_prediction[0, 0, :24, :24] = 1.0 - - loss = self.model._compute_box_consistency_loss( - boxes_gt, boxes_jittered, tf.constant(mask_prediction)) - - yloss = tf.nn.sigmoid_cross_entropy_with_logits( - labels=tf.constant([1.0] * 8 + [0.0] * 8), - logits=[1.0] * 12 + [0.0] * 4) - xloss = tf.nn.sigmoid_cross_entropy_with_logits( - labels=tf.constant([1.0] * 16), - logits=[1.0] * 12 + [0.0] * 4) - yloss_mean = tf.reduce_mean(yloss) - xloss_mean = tf.reduce_mean(xloss) - - self.assertAllClose(loss[0], [yloss_mean + xloss_mean]) - - def test_box_consistency_loss_with_tightness(self): - - boxes_gt = tf.constant([[[0., 0., 0.49, 0.49]]]) - boxes_jittered = None - - mask_prediction = np.zeros((1, 1, 8, 8)).astype(np.float32) - 1e10 - mask_prediction[0, 0, :4, :4] = 1e10 - - model = build_meta_arch(box_consistency_tightness=True, - predict_full_resolution_masks=True) - loss = model._compute_box_consistency_loss( - boxes_gt, boxes_jittered, tf.constant(mask_prediction)) - - self.assertAllClose(loss[0], [0.0]) - - def test_box_consistency_loss_gt_count(self): - - boxes_gt = tf.constant([[ - [0., 0., 1.0, 1.0], - [0., 0., 0.49, 0.49]]]) - boxes_jittered = None - - mask_prediction = np.zeros((1, 2, 32, 32)).astype(np.float32) - mask_prediction[0, 0, :16, :16] = 1.0 - mask_prediction[0, 1, :8, :8] = 1.0 - - model = build_meta_arch( - box_consistency_loss_normalize='normalize_groundtruth_count', - predict_full_resolution_masks=True) - loss_func = ( - model._compute_box_consistency_loss) - loss = loss_func( - boxes_gt, boxes_jittered, tf.constant(mask_prediction)) - - yloss = tf.nn.sigmoid_cross_entropy_with_logits( - labels=tf.constant([1.0] * 32), - logits=[1.0] * 16 + [0.0] * 16) / 32.0 - yloss_mean = tf.reduce_sum(yloss) - xloss = yloss - xloss_mean = tf.reduce_sum(xloss) - - self.assertAllClose(loss[0, 0], yloss_mean + xloss_mean) - - yloss = tf.nn.sigmoid_cross_entropy_with_logits( - labels=tf.constant([1.0] * 16 + [0.0] * 16), - logits=[1.0] * 8 + [0.0] * 24) / 16.0 - yloss_mean = tf.reduce_sum(yloss) - xloss = yloss - xloss_mean = tf.reduce_sum(xloss) - self.assertAllClose(loss[0, 1], yloss_mean + xloss_mean) - - def test_box_consistency_loss_balanced(self): - boxes_gt = tf.constant([[ - [0., 0., 0.49, 0.49]]]) - boxes_jittered = None - - mask_prediction = np.zeros((1, 1, 32, 32)).astype(np.float32) - mask_prediction[0, 0] = 1.0 - - model = build_meta_arch(box_consistency_loss_normalize='normalize_balanced', - predict_full_resolution_masks=True) - loss_func = tf.function( - model._compute_box_consistency_loss) - loss = loss_func( - boxes_gt, boxes_jittered, tf.constant(mask_prediction)) - - yloss = tf.nn.sigmoid_cross_entropy_with_logits( - labels=[0.] * 16 + [1.0] * 16, - logits=[1.0] * 32) - yloss_mean = tf.reduce_sum(yloss) / 16.0 - xloss_mean = yloss_mean - - self.assertAllClose(loss[0, 0], yloss_mean + xloss_mean) - - def test_box_consistency_dice_loss(self): - - model = build_meta_arch(use_dice_loss=True) - boxes_gt = tf.constant([[[0., 0., 0.49, 1.0]]]) - boxes_jittered = tf.constant([[[0.0, 0.0, 1.0, 1.0]]]) - - almost_inf = 1e10 - mask_prediction = np.full((1, 1, 32, 32), -almost_inf, dtype=np.float32) - mask_prediction[0, 0, :24, :24] = almost_inf - - loss = model._compute_box_consistency_loss( - boxes_gt, boxes_jittered, tf.constant(mask_prediction)) - - yloss = 1 - 6.0 / 7 - xloss = 0.2 - - self.assertAllClose(loss, [[yloss + xloss]]) - - def test_feature_consistency_loss_full_res_shape(self): - - model = build_meta_arch(use_dice_loss=True, - predict_full_resolution_masks=True) - boxes = tf.zeros((5, 3, 4)) - img = tf.zeros((5, 32, 32, 3)) - mask_logits = tf.zeros((5, 3, 32, 32)) - - loss = model._compute_feature_consistency_loss( - boxes, img, mask_logits) - self.assertEqual([5, 3], loss.shape) - - def test_feature_consistency_1_threshold(self): - model = build_meta_arch(predict_full_resolution_masks=True, - feature_consistency_threshold=0.99) - boxes = tf.zeros((5, 3, 4)) - img = tf.zeros((5, 32, 32, 3)) - mask_logits = tf.zeros((5, 3, 32, 32)) - 1e4 - - loss = model._compute_feature_consistency_loss( - boxes, img, mask_logits) - self.assertAllClose(loss, np.zeros((5, 3))) - - def test_box_consistency_dice_loss_full_res(self): - - model = build_meta_arch(use_dice_loss=True, - predict_full_resolution_masks=True) - boxes_gt = tf.constant([[[0., 0., 1.0, 1.0]]]) - boxes_jittered = None - - size = 32 - almost_inf = 1e10 - mask_prediction = np.full((1, 1, size, size), -almost_inf, dtype=np.float32) - mask_prediction[0, 0, :(size // 2), :] = almost_inf - - loss = model._compute_box_consistency_loss( - boxes_gt, boxes_jittered, tf.constant(mask_prediction)) - self.assertAlmostEqual(loss[0, 0].numpy(), 1 / 3) - - def test_get_lab_image_shape(self): - - output = self.model._get_lab_image(tf.zeros((2, 4, 4, 3))) - self.assertEqual(output.shape, (2, 4, 4, 3)) - - def test_self_supervised_augmented_loss_identity(self): - model = build_meta_arch(predict_full_resolution_masks=True, - augmented_self_supervision_max_translation=0.0) - - x = tf.random.uniform((2, 3, 32, 32), 0, 1) - boxes = tf.constant([[0., 0., 1., 1.]] * 6) - boxes = tf.reshape(boxes, [2, 3, 4]) - x = tf.cast(x > 0, tf.float32) - x = (x - 0.5) * 2e40 # x is a tensor or large +ve or -ve values. - loss = model._compute_self_supervised_augmented_loss(x, x, boxes) - - self.assertAlmostEqual(loss.numpy().sum(), 0.0) - - def test_self_supervised_mse_augmented_loss_0(self): - model = build_meta_arch(predict_full_resolution_masks=True, - augmented_self_supervision_max_translation=0.0, - augmented_self_supervision_loss='loss_mse') - - x = tf.random.uniform((2, 3, 32, 32), 0, 1) - boxes = tf.constant([[0., 0., 1., 1.]] * 6) - boxes = tf.reshape(boxes, [2, 3, 4]) - loss = model._compute_self_supervised_augmented_loss(x, x, boxes) - - self.assertAlmostEqual(loss.numpy().min(), 0.0) - self.assertAlmostEqual(loss.numpy().max(), 0.0) - - def test_self_supervised_mse_loss_scale_equivalent(self): - model = build_meta_arch(predict_full_resolution_masks=True, - augmented_self_supervision_max_translation=0.0, - augmented_self_supervision_loss='loss_mse') - - x = np.zeros((1, 3, 32, 32), dtype=np.float32) + 100.0 - y = 0.0 * x.copy() - - x[0, 0, :8, :8] = 0.0 - y[0, 0, :8, :8] = 1.0 - x[0, 1, :16, :16] = 0.0 - y[0, 1, :16, :16] = 1.0 - x[0, 2, :16, :16] = 0.0 - x[0, 2, :8, :8] = 1.0 - y[0, 2, :16, :16] = 0.0 - - boxes = np.array([[0., 0., 0.22, 0.22], [0., 0., 0.47, 0.47], - [0., 0., 0.47, 0.47]], - dtype=np.float32) - - boxes = tf.reshape(tf.constant(boxes), [1, 3, 4]) - loss = model._compute_self_supervised_augmented_loss(x, y, boxes) - - self.assertEqual(loss.shape, (1, 3)) - mse_1_minus_0 = (tf.nn.sigmoid(1.0) - tf.nn.sigmoid(0.0)).numpy()**2 - self.assertAlmostEqual(loss.numpy()[0, 0], mse_1_minus_0) - self.assertAlmostEqual(loss.numpy()[0, 1], mse_1_minus_0) - self.assertAlmostEqual(loss.numpy()[0, 2], mse_1_minus_0 / 4.0) - - def test_self_supervised_kldiv_augmented_loss_0(self): - model = build_meta_arch(predict_full_resolution_masks=True, - augmented_self_supervision_max_translation=0.0, - augmented_self_supervision_loss='loss_kl_div') - - x = tf.random.uniform((2, 3, 32, 32), 0, 1) - boxes = tf.constant([[0., 0., 1., 1.]] * 6) - boxes = tf.reshape(boxes, [2, 3, 4]) - loss = model._compute_self_supervised_augmented_loss(x, x, boxes) - - self.assertAlmostEqual(loss.numpy().min(), 0.0) - self.assertAlmostEqual(loss.numpy().max(), 0.0) - - def test_self_supervised_kldiv_scale_equivalent(self): - model = build_meta_arch(predict_full_resolution_masks=True, - augmented_self_supervision_max_translation=0.0, - augmented_self_supervision_loss='loss_kl_div') - - pred = np.zeros((1, 2, 32, 32), dtype=np.float32) + 100.0 - true = 0.0 * pred.copy() - - pred[0, 0, :8, :8] = LOGIT_HALF - true[0, 0, :8, :8] = LOGIT_QUARTER - pred[0, 1, :16, :16] = LOGIT_HALF - true[0, 1, :16, :16] = LOGIT_QUARTER - - boxes = np.array([[0., 0., 0.22, 0.22], [0., 0., 0.47, 0.47]], - dtype=np.float32) - - boxes = tf.reshape(tf.constant(boxes), [1, 2, 4]) - loss = model._compute_self_supervised_augmented_loss( - original_logits=pred, deaugmented_logits=true, boxes=boxes) - - self.assertEqual(loss.shape, (1, 2)) - expected = (3 * math.log(3) - 4 * math.log(2)) / 4.0 - self.assertAlmostEqual(loss.numpy()[0, 0], expected, places=4) - self.assertAlmostEqual(loss.numpy()[0, 1], expected, places=4) - - def test_self_supervision_warmup(self): - tf.keras.backend.set_learning_phase(True) - model = build_meta_arch( - use_dice_loss=True, - predict_full_resolution_masks=True, - network_type='cond_inst1', - dim=9, - pixel_embedding_dim=8, - use_instance_embedding=False, - use_xy=False, - augmented_self_supervision_loss_weight=1.0, - augmented_self_supervision_max_translation=0.5, - augmented_self_supervision_warmup_start=10, - augmented_self_supervision_warmup_steps=40) - num_stages = 1 - prediction = { - 'preprocessed_inputs': tf.random.normal((1, 32, 32, 3)), - 'MASK_LOGITS_GT_BOXES': [tf.random.normal((1, 5, 8, 8))] * num_stages, - 'SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS': - [tf.random.normal((1, 5, 8, 8))] * num_stages, - 'object_center': [tf.random.normal((1, 8, 8, 6))] * num_stages, - 'box/offset': [tf.random.normal((1, 8, 8, 2))] * num_stages, - 'box/scale': [tf.random.normal((1, 8, 8, 2))] * num_stages, - 'extracted_features': [tf.random.normal((3, 32, 32, 7))] * num_stages - } - - boxes = [tf.convert_to_tensor([[0., 0., 1., 1.]] * 5)] - classes = [tf.one_hot([1, 0, 1, 1, 1], depth=6)] - weights = [tf.ones(5)] - masks = [tf.ones((5, 32, 32))] - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=5) - loss_at_5 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=20) - loss_at_20 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=50) - loss_at_50 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=100) - loss_at_100 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - loss_key = 'Loss/' + deepmac_meta_arch.DEEP_MASK_AUGMENTED_SELF_SUPERVISION - self.assertAlmostEqual(loss_at_5[loss_key].numpy(), 0.0) - self.assertGreater(loss_at_20[loss_key], 0.0) - - self.assertAlmostEqual(loss_at_20[loss_key].numpy(), - loss_at_50[loss_key].numpy() / 4.0) - self.assertAlmostEqual(loss_at_50[loss_key].numpy(), - loss_at_100[loss_key].numpy()) - - def test_loss_keys(self): - model = build_meta_arch( - use_dice_loss=True, - augmented_self_supervision_loss_weight=1.0, - augmented_self_supervision_max_translation=0.5, - predict_full_resolution_masks=True) - - prediction = { - 'preprocessed_inputs': tf.random.normal((3, 32, 32, 3)), - 'MASK_LOGITS_GT_BOXES': [tf.random.normal((3, 5, 8, 8))] * 2, - 'object_center': [tf.random.normal((3, 8, 8, 6))] * 2, - 'box/offset': [tf.random.normal((3, 8, 8, 2))] * 2, - 'box/scale': [tf.random.normal((3, 8, 8, 2))] * 2, - 'SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS': ( - [tf.random.normal((3, 5, 8, 8))] * 2), - 'extracted_features': [tf.random.normal((3, 32, 32, 7))] * 2 - } - model.provide_groundtruth( - groundtruth_boxes_list=[ - tf.convert_to_tensor([[0., 0., 1., 1.]] * 5)] * 3, - groundtruth_classes_list=[tf.one_hot([1, 0, 1, 1, 1], depth=6)] * 3, - groundtruth_weights_list=[tf.ones(5)] * 3, - groundtruth_masks_list=[tf.ones((5, 32, 32))] * 3, - groundtruth_keypoints_list=[tf.zeros((5, 10, 2))] * 3, - groundtruth_keypoint_depths_list=[tf.zeros((5, 10))] * 3) - loss = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - self.assertGreater(loss['Loss/deep_mask_estimation'], 0.0) - - for weak_loss in deepmac_meta_arch.MASK_LOSSES: - if weak_loss == deepmac_meta_arch.DEEP_MASK_FEATURE_CONSISTENCY: - continue - self.assertGreater(loss['Loss/' + weak_loss], 0.0, - '{} was <= 0'.format(weak_loss)) - - def test_eval_loss_and_postprocess_keys(self): - - model = build_meta_arch( - use_dice_loss=True, - augmented_self_supervision_loss_weight=1.0, - augmented_self_supervision_max_translation=0.5, - predict_full_resolution_masks=True) - - true_image_shapes = tf.constant([[32, 32, 3]], dtype=tf.int32) - prediction_dict = model.predict( - tf.zeros((1, 32, 32, 3)), true_image_shapes) - output = model.postprocess(prediction_dict, true_image_shapes) - self.assertEqual(output['detection_boxes'].shape, (1, 5, 4)) - self.assertEqual(output['detection_masks'].shape, (1, 5, 128, 128)) - - model.provide_groundtruth( - groundtruth_boxes_list=[ - tf.convert_to_tensor([[0., 0., 1., 1.]] * 5)] * 1, - groundtruth_classes_list=[tf.one_hot([1, 0, 1, 1, 1], depth=6)] * 1, - groundtruth_weights_list=[tf.ones(5)] * 1, - groundtruth_masks_list=[tf.ones((5, 32, 32))] * 1, - groundtruth_keypoints_list=[tf.zeros((5, 10, 2))] * 1, - groundtruth_keypoint_depths_list=[tf.zeros((5, 10))] * 1) - prediction_dict = model.predict( - tf.zeros((1, 32, 32, 3)), true_image_shapes) - model.loss(prediction_dict, true_image_shapes) - - def test_loss_weight_response(self): - tf.random.set_seed(12) - model = build_meta_arch( - use_dice_loss=True, - predict_full_resolution_masks=True, - network_type='cond_inst1', - dim=9, - pixel_embedding_dim=8, - use_instance_embedding=False, - use_xy=False, - augmented_self_supervision_loss_weight=1.0, - augmented_self_supervision_max_translation=0.5, - ) - num_stages = 1 - prediction = { - 'preprocessed_inputs': tf.random.normal((1, 32, 32, 3)), - 'MASK_LOGITS_GT_BOXES': [tf.random.normal((1, 5, 8, 8))] * num_stages, - 'object_center': [tf.random.normal((1, 8, 8, 6))] * num_stages, - 'box/offset': [tf.random.normal((1, 8, 8, 2))] * num_stages, - 'box/scale': [tf.random.normal((1, 8, 8, 2))] * num_stages, - 'SELF_SUPERVISED_DEAUGMENTED_MASK_LOGITS': ( - [tf.random.normal((1, 5, 8, 8))] * num_stages), - 'extracted_features': [tf.random.normal((3, 32, 32, 7))] * num_stages - } - - boxes = [tf.convert_to_tensor([[0., 0., 1., 1.]] * 5)] - classes = [tf.one_hot([1, 0, 1, 1, 1], depth=6)] - weights = [tf.ones(5)] - masks = [tf.ones((5, 32, 32))] - keypoints = [tf.zeros((5, 10, 2))] - keypoint_depths = [tf.ones((5, 10))] - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - groundtruth_keypoints_list=keypoints, - groundtruth_keypoint_depths_list=keypoint_depths) - loss = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - self.assertGreater(loss['Loss/deep_mask_estimation'], 0.0) - - for mask_loss in deepmac_meta_arch.MASK_LOSSES: - self.assertGreater(loss['Loss/' + mask_loss], 0.0, - '{} was <= 0'.format(mask_loss)) - - rng = random.Random(0) - loss_weights = { - deepmac_meta_arch.DEEP_MASK_ESTIMATION: rng.uniform(1, 5), - deepmac_meta_arch.DEEP_MASK_BOX_CONSISTENCY: rng.uniform(1, 5), - deepmac_meta_arch.DEEP_MASK_FEATURE_CONSISTENCY: rng.uniform(1, 5), - deepmac_meta_arch.DEEP_MASK_AUGMENTED_SELF_SUPERVISION: ( - rng.uniform(1, 5)), - deepmac_meta_arch.DEEP_MASK_POINTLY_SUPERVISED: rng.uniform(1, 5) - } - - weighted_model = build_meta_arch( - use_dice_loss=True, - predict_full_resolution_masks=True, - network_type='cond_inst1', - dim=9, - pixel_embedding_dim=8, - use_instance_embedding=False, - use_xy=False, - task_loss_weight=loss_weights[deepmac_meta_arch.DEEP_MASK_ESTIMATION], - box_consistency_loss_weight=( - loss_weights[deepmac_meta_arch.DEEP_MASK_BOX_CONSISTENCY]), - feature_consistency_loss_weight=( - loss_weights[deepmac_meta_arch.DEEP_MASK_FEATURE_CONSISTENCY]), - augmented_self_supervision_loss_weight=( - loss_weights[deepmac_meta_arch.DEEP_MASK_AUGMENTED_SELF_SUPERVISION] - ), - pointly_supervised_keypoint_loss_weight=( - loss_weights[deepmac_meta_arch.DEEP_MASK_POINTLY_SUPERVISED]) - ) - - weighted_model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - groundtruth_keypoints_list=keypoints, - groundtruth_keypoint_depths_list=keypoint_depths) - - weighted_loss = weighted_model.loss(prediction, tf.constant([[32, 32, 3]])) - for mask_loss in deepmac_meta_arch.MASK_LOSSES: - loss_key = 'Loss/' + mask_loss - self.assertAllEqual( - weighted_loss[loss_key], loss[loss_key] * loss_weights[mask_loss], - f'{mask_loss} did not respond to change in weight.') - - @parameterized.parameters( - [dict(feature_consistency_type='consistency_default_lab', - feature_consistency_comparison='comparison_default_gaussian'), - dict(feature_consistency_type='consistency_feature_map', - feature_consistency_comparison='comparison_normalized_dotprod')], - ) - def test_feature_consistency_warmup( - self, feature_consistency_type, feature_consistency_comparison): - tf.keras.backend.set_learning_phase(True) - model = build_meta_arch( - use_dice_loss=True, - predict_full_resolution_masks=True, - network_type='cond_inst1', - dim=9, - pixel_embedding_dim=8, - use_instance_embedding=False, - use_xy=False, - feature_consistency_warmup_steps=10, - feature_consistency_warmup_start=10, - feature_consistency_type=feature_consistency_type, - feature_consistency_comparison=feature_consistency_comparison) - - num_stages = 1 - prediction = { - 'preprocessed_inputs': tf.random.normal((1, 32, 32, 3)), - 'MASK_LOGITS_GT_BOXES': [tf.random.normal((1, 5, 8, 8))] * num_stages, - 'object_center': [tf.random.normal((1, 8, 8, 6))] * num_stages, - 'box/offset': [tf.random.normal((1, 8, 8, 2))] * num_stages, - 'box/scale': [tf.random.normal((1, 8, 8, 2))] * num_stages, - 'extracted_features': [tf.random.normal((3, 32, 32, 7))] * num_stages - } - - boxes = [tf.convert_to_tensor([[0., 0., 1., 1.]] * 5)] - classes = [tf.one_hot([1, 0, 1, 1, 1], depth=6)] - weights = [tf.ones(5)] - masks = [tf.ones((5, 32, 32))] - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=5) - loss_at_5 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=15) - loss_at_15 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=20) - loss_at_20 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - model.provide_groundtruth( - groundtruth_boxes_list=boxes, - groundtruth_classes_list=classes, - groundtruth_weights_list=weights, - groundtruth_masks_list=masks, - training_step=100) - loss_at_100 = model.loss(prediction, tf.constant([[32, 32, 3.0]])) - - loss_key = 'Loss/' + deepmac_meta_arch.DEEP_MASK_FEATURE_CONSISTENCY - self.assertAlmostEqual(loss_at_5[loss_key].numpy(), 0.0) - self.assertGreater(loss_at_15[loss_key], 0.0) - self.assertAlmostEqual(loss_at_15[loss_key].numpy(), - loss_at_20[loss_key].numpy() / 2.0) - self.assertAlmostEqual(loss_at_20[loss_key].numpy(), - loss_at_100[loss_key].numpy()) - - def test_pointly_supervised_loss(self): - tf.keras.backend.set_learning_phase(True) - model = build_meta_arch( - use_dice_loss=False, - predict_full_resolution_masks=True, - network_type='cond_inst1', - dim=9, - pixel_embedding_dim=8, - use_instance_embedding=False, - use_xy=False, - pointly_supervised_keypoint_loss_weight=1.0) - - mask_logits = np.zeros((1, 1, 32, 32), dtype=np.float32) - keypoints = np.zeros((1, 1, 1, 2), dtype=np.float32) - keypoint_depths = np.zeros((1, 1, 1), dtype=np.float32) - - keypoints[..., 0] = 0.5 - keypoints[..., 1] = 0.5 - keypoint_depths[..., 0] = 1.0 - mask_logits[:, :, 16, 16] = 1.0 - - expected_loss = tf.nn.sigmoid_cross_entropy_with_logits( - logits=[[1.0]], labels=[[1.0]] - ).numpy() - loss = model._compute_pointly_supervised_loss_from_keypoints( - mask_logits, keypoints, keypoint_depths) - - self.assertEqual(loss.shape, (1, 1)) - self.assertAllClose(expected_loss, loss) - - def test_ignore_per_class_box_overlap(self): - tf.keras.backend.set_learning_phase(True) - model = build_meta_arch( - use_dice_loss=False, - predict_full_resolution_masks=True, - network_type='cond_inst1', - dim=9, - pixel_embedding_dim=8, - use_instance_embedding=False, - use_xy=False, - pointly_supervised_keypoint_loss_weight=1.0, - ignore_per_class_box_overlap=True) - - self.assertTrue(model._deepmac_params.ignore_per_class_box_overlap) - mask_logits = tf.zeros((2, 3, 16, 16)) - mask_gt = tf.zeros((2, 3, 32, 32)) - boxes = tf.zeros((2, 3, 4)) - classes = tf.zeros((2, 3, 5)) - - loss = model._compute_mask_prediction_loss( - boxes, mask_logits, mask_gt, classes) - - self.assertEqual(loss.shape, (2, 3)) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class FullyConnectedMaskHeadTest(tf.test.TestCase): - - def test_fc_mask_head(self): - head = deepmac_meta_arch.FullyConnectedMaskHead(512, 16) - inputs = tf.random.uniform([100, 16, 16, 512]) - output = head(inputs) - self.assertAllEqual([100, 16, 16, 1], output.numpy().shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ResNetMaskHeadTest(tf.test.TestCase, parameterized.TestCase): - - @parameterized.parameters(['resnet4', 'resnet8', 'resnet20']) - def test_forward(self, name): - net = deepmac_meta_arch.ResNetMaskNetwork(name, 8) - out = net(tf.zeros((3, 32, 32, 16))) - self.assertEqual(out.shape[:3], (3, 32, 32)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py b/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py deleted file mode 100644 index a7dd5b6efa4..00000000000 --- a/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py +++ /dev/null @@ -1,2966 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Faster R-CNN meta-architecture definition. - -General tensorflow implementation of Faster R-CNN detection models. - -See Faster R-CNN: Ren, Shaoqing, et al. -"Faster R-CNN: Towards real-time object detection with region proposal -networks." Advances in neural information processing systems. 2015. - -We allow for three modes: number_of_stages={1, 2, 3}. In case of 1 stage, -all of the user facing methods (e.g., predict, postprocess, loss) can be used as -if the model consisted only of the RPN, returning class agnostic proposals -(these can be thought of as approximate detections with no associated class -information). In case of 2 stages, proposals are computed, then passed -through a second stage "box classifier" to yield (multi-class) detections. -Finally, in case of 3 stages which is only used during eval, proposals are -computed, then passed through a second stage "box classifier" that will compute -refined boxes and classes, and then features are pooled from the refined and -non-maximum suppressed boxes and are passed through the box classifier again. If -number of stages is 3 during training it will be reduced to two automatically. - -Implementations of Faster R-CNN models must define a new -FasterRCNNFeatureExtractor and override three methods: `preprocess`, -`_extract_proposal_features` (the first stage of the model), and -`_extract_box_classifier_features` (the second stage of the model). Optionally, -the `restore_fn` method can be overridden. See tests for an example. - -A few important notes: -+ Batching conventions: We support batched inference and training where -all images within a batch have the same resolution. Batch sizes are determined -dynamically via the shape of the input tensors (rather than being specified -directly as, e.g., a model constructor). - -A complication is that due to non-max suppression, we are not guaranteed to get -the same number of proposals from the first stage RPN (region proposal network) -for each image (though in practice, we should often get the same number of -proposals). For this reason we pad to a max number of proposals per image -within a batch. This `self.max_num_proposals` property is set to the -`first_stage_max_proposals` parameter at inference time and the -`second_stage_batch_size` at training time since we subsample the batch to -be sent through the box classifier during training. - -For the second stage of the pipeline, we arrange the proposals for all images -within the batch along a single batch dimension. For example, the input to -_extract_box_classifier_features is a tensor of shape -`[total_num_proposals, crop_height, crop_width, depth]` where -total_num_proposals is batch_size * self.max_num_proposals. (And note that per -the above comment, a subset of these entries correspond to zero paddings.) - -+ Coordinate representations: -Following the API (see model.DetectionModel definition), our outputs after -postprocessing operations are always normalized boxes however, internally, we -sometimes convert to absolute --- e.g. for loss computation. In particular, -anchors and proposal_boxes are both represented as absolute coordinates. - -Images are resized in the `preprocess` method. - -The Faster R-CNN meta architecture has two post-processing methods -`_postprocess_rpn` which is applied after first stage and -`_postprocess_box_classifier` which is applied after second stage. There are -three different ways post-processing can happen depending on number_of_stages -configured in the meta architecture: - -1. When number_of_stages is 1: - `_postprocess_rpn` is run as part of the `postprocess` method where - true_image_shapes is used to clip proposals, perform non-max suppression and - normalize them. -2. When number of stages is 2: - `_postprocess_rpn` is run as part of the `_predict_second_stage` method where - `resized_image_shapes` is used to clip proposals, perform non-max suppression - and normalize them. In this case `postprocess` method skips `_postprocess_rpn` - and only runs `_postprocess_box_classifier` using `true_image_shapes` to clip - detections, perform non-max suppression and normalize them. -3. When number of stages is 3: - `_postprocess_rpn` is run as part of the `_predict_second_stage` using - `resized_image_shapes` to clip proposals, perform non-max suppression and - normalize them. Subsequently, `_postprocess_box_classifier` is run as part of - `_predict_third_stage` using `true_image_shapes` to clip detections, peform - non-max suppression and normalize them. In this case, the `postprocess` method - skips both `_postprocess_rpn` and `_postprocess_box_classifier`. -""" - -from __future__ import print_function -import abc -import functools -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import box_predictor -from object_detection.core import losses -from object_detection.core import model -from object_detection.core import standard_fields as fields -from object_detection.core import target_assigner -from object_detection.utils import ops -from object_detection.utils import shape_utils -from object_detection.utils import variables_helper - - -_UNINITIALIZED_FEATURE_EXTRACTOR = '__uninitialized__' - - -class FasterRCNNFeatureExtractor(object): - """Faster R-CNN Feature Extractor definition.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0): - """Constructor. - - Args: - is_training: A boolean indicating whether the training version of the - computation graph should be constructed. - first_stage_features_stride: Output stride of extracted RPN feature map. - batch_norm_trainable: Whether to update batch norm parameters during - training or not. When training with a relative large batch size - (e.g. 8), it could be desirable to enable batch norm update. - reuse_weights: Whether to reuse variables. Default is None. - weight_decay: float weight decay for feature extractor (default: 0.0). - """ - self._is_training = is_training - self._first_stage_features_stride = first_stage_features_stride - self._train_batch_norm = (batch_norm_trainable and is_training) - self._reuse_weights = tf.AUTO_REUSE if reuse_weights else None - self._weight_decay = weight_decay - - @abc.abstractmethod - def preprocess(self, resized_inputs): - """Feature-extractor specific preprocessing (minus image resizing).""" - pass - - def extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features. - - This function is responsible for extracting feature maps from preprocessed - images. These features are used by the region proposal network (RPN) to - predict proposals. - - Args: - preprocessed_inputs: A [batch, height, width, channels] float tensor - representing a batch of images. - scope: A scope name. - - Returns: - rpn_feature_map: A tensor with shape [batch, height, width, depth] - activations: A dictionary mapping activation tensor names to tensors. - """ - with tf.variable_scope(scope, values=[preprocessed_inputs]): - return self._extract_proposal_features(preprocessed_inputs, scope) - - @abc.abstractmethod - def _extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features, to be overridden.""" - pass - - def extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features. - - Args: - proposal_feature_maps: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - scope: A scope name. - - Returns: - proposal_classifier_features: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - with tf.variable_scope( - scope, values=[proposal_feature_maps], reuse=tf.AUTO_REUSE): - return self._extract_box_classifier_features(proposal_feature_maps, scope) - - @abc.abstractmethod - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features, to be overridden.""" - pass - - def restore_from_classification_checkpoint_fn( - self, - first_stage_feature_extractor_scope, - second_stage_feature_extractor_scope): - """Returns a map of variables to load from a foreign checkpoint. - - Args: - first_stage_feature_extractor_scope: A scope name for the first stage - feature extractor. - second_stage_feature_extractor_scope: A scope name for the second stage - feature extractor. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - """ - variables_to_restore = {} - for variable in variables_helper.get_global_variables_safely(): - for scope_name in [first_stage_feature_extractor_scope, - second_stage_feature_extractor_scope]: - if variable.op.name.startswith(scope_name): - var_name = variable.op.name.replace(scope_name + '/', '') - variables_to_restore[var_name] = variable - return variables_to_restore - - -class FasterRCNNKerasFeatureExtractor(object): - """Keras-based Faster R-CNN Feature Extractor definition.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - weight_decay=0.0): - """Constructor. - - Args: - is_training: A boolean indicating whether the training version of the - computation graph should be constructed. - first_stage_features_stride: Output stride of extracted RPN feature map. - batch_norm_trainable: Whether to update batch norm parameters during - training or not. When training with a relative large batch size - (e.g. 8), it could be desirable to enable batch norm update. - weight_decay: float weight decay for feature extractor (default: 0.0). - """ - self._is_training = is_training - self._first_stage_features_stride = first_stage_features_stride - self._train_batch_norm = (batch_norm_trainable and is_training) - self._weight_decay = weight_decay - - @abc.abstractmethod - def preprocess(self, resized_inputs): - """Feature-extractor specific preprocessing (minus image resizing).""" - pass - - @abc.abstractmethod - def get_proposal_feature_extractor_model(self, name): - """Get model that extracts first stage RPN features, to be overridden.""" - pass - - @abc.abstractmethod - def get_box_classifier_feature_extractor_model(self, name): - """Get model that extracts second stage box classifier features.""" - pass - - -class FasterRCNNMetaArch(model.DetectionModel): - """Faster R-CNN Meta-architecture definition.""" - - def __init__(self, - is_training, - num_classes, - image_resizer_fn, - feature_extractor, - number_of_stages, - first_stage_anchor_generator, - first_stage_target_assigner, - first_stage_atrous_rate, - first_stage_box_predictor_arg_scope_fn, - first_stage_box_predictor_kernel_size, - first_stage_box_predictor_depth, - first_stage_minibatch_size, - first_stage_sampler, - first_stage_non_max_suppression_fn, - first_stage_max_proposals, - first_stage_localization_loss_weight, - first_stage_objectness_loss_weight, - crop_and_resize_fn, - initial_crop_size, - maxpool_kernel_size, - maxpool_stride, - second_stage_target_assigner, - second_stage_mask_rcnn_box_predictor, - second_stage_batch_size, - second_stage_sampler, - second_stage_non_max_suppression_fn, - second_stage_score_conversion_fn, - second_stage_localization_loss_weight, - second_stage_classification_loss_weight, - second_stage_classification_loss, - second_stage_mask_prediction_loss_weight=1.0, - hard_example_miner=None, - parallel_iterations=16, - add_summaries=True, - clip_anchors_to_image=False, - use_static_shapes=False, - resize_masks=True, - freeze_batchnorm=False, - return_raw_detections_during_predict=False, - output_final_box_features=False, - output_final_box_rpn_features=False): - """FasterRCNNMetaArch Constructor. - - Args: - is_training: A boolean indicating whether the training version of the - computation graph should be constructed. - num_classes: Number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - image_resizer_fn: A callable for image resizing. This callable - takes a rank-3 image tensor of shape [height, width, channels] - (corresponding to a single image), an optional rank-3 instance mask - tensor of shape [num_masks, height, width] and returns a resized rank-3 - image tensor, a resized mask tensor if one was provided in the input. In - addition this callable must also return a 1-D tensor of the form - [height, width, channels] containing the size of the true image, as the - image resizer can perform zero padding. See protos/image_resizer.proto. - feature_extractor: A FasterRCNNFeatureExtractor object. - number_of_stages: An integer values taking values in {1, 2, 3}. If - 1, the function will construct only the Region Proposal Network (RPN) - part of the model. If 2, the function will perform box refinement and - other auxiliary predictions all in the second stage. If 3, it will - extract features from refined boxes and perform the auxiliary - predictions on the non-maximum suppressed refined boxes. - If is_training is true and the value of number_of_stages is 3, it is - reduced to 2 since all the model heads are trained in parallel in second - stage during training. - first_stage_anchor_generator: An anchor_generator.AnchorGenerator object - (note that currently we only support - grid_anchor_generator.GridAnchorGenerator objects) - first_stage_target_assigner: Target assigner to use for first stage of - Faster R-CNN (RPN). - first_stage_atrous_rate: A single integer indicating the atrous rate for - the single convolution op which is applied to the `rpn_features_to_crop` - tensor to obtain a tensor to be used for box prediction. Some feature - extractors optionally allow for producing feature maps computed at - denser resolutions. The atrous rate is used to compensate for the - denser feature maps by using an effectively larger receptive field. - (This should typically be set to 1). - first_stage_box_predictor_arg_scope_fn: Either a - Keras layer hyperparams object or a function to construct tf-slim - arg_scope for conv2d, separable_conv2d and fully_connected ops. Used - for the RPN box predictor. If it is a keras hyperparams object the - RPN box predictor will be a Keras model. If it is a function to - construct an arg scope it will be a tf-slim box predictor. - first_stage_box_predictor_kernel_size: Kernel size to use for the - convolution op just prior to RPN box predictions. - first_stage_box_predictor_depth: Output depth for the convolution op - just prior to RPN box predictions. - first_stage_minibatch_size: The "batch size" to use for computing the - objectness and location loss of the region proposal network. This - "batch size" refers to the number of anchors selected as contributing - to the loss function for any given image within the image batch and is - only called "batch_size" due to terminology from the Faster R-CNN paper. - first_stage_sampler: Sampler to use for first stage loss (RPN loss). - first_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression - callable that takes `boxes`, `scores` and optional `clip_window`(with - all other inputs already set) and returns a dictionary containing - tensors with keys: `detection_boxes`, `detection_scores`, - `detection_classes`, `num_detections`. This is used to perform non max - suppression on the boxes predicted by the Region Proposal Network - (RPN). - See `post_processing.batch_multiclass_non_max_suppression` for the type - and shape of these tensors. - first_stage_max_proposals: Maximum number of boxes to retain after - performing Non-Max Suppression (NMS) on the boxes predicted by the - Region Proposal Network (RPN). - first_stage_localization_loss_weight: A float - first_stage_objectness_loss_weight: A float - crop_and_resize_fn: A differentiable resampler to use for cropping RPN - proposal features. - initial_crop_size: A single integer indicating the output size - (width and height are set to be the same) of the initial bilinear - interpolation based cropping during ROI pooling. - maxpool_kernel_size: A single integer indicating the kernel size of the - max pool op on the cropped feature map during ROI pooling. - maxpool_stride: A single integer indicating the stride of the max pool - op on the cropped feature map during ROI pooling. - second_stage_target_assigner: Target assigner to use for second stage of - Faster R-CNN. If the model is configured with multiple prediction heads, - this target assigner is used to generate targets for all heads (with the - correct `unmatched_class_label`). - second_stage_mask_rcnn_box_predictor: Mask R-CNN box predictor to use for - the second stage. - second_stage_batch_size: The batch size used for computing the - classification and refined location loss of the box classifier. This - "batch size" refers to the number of proposals selected as contributing - to the loss function for any given image within the image batch and is - only called "batch_size" due to terminology from the Faster R-CNN paper. - second_stage_sampler: Sampler to use for second stage loss (box - classifier loss). - second_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression - callable that takes `boxes`, `scores`, optional `clip_window` and - optional (kwarg) `mask` inputs (with all other inputs already set) - and returns a dictionary containing tensors with keys: - `detection_boxes`, `detection_scores`, `detection_classes`, - `num_detections`, and (optionally) `detection_masks`. See - `post_processing.batch_multiclass_non_max_suppression` for the type and - shape of these tensors. - second_stage_score_conversion_fn: Callable elementwise nonlinearity - (that takes tensors as inputs and returns tensors). This is usually - used to convert logits to probabilities. - second_stage_localization_loss_weight: A float indicating the scale factor - for second stage localization loss. - second_stage_classification_loss_weight: A float indicating the scale - factor for second stage classification loss. - second_stage_classification_loss: Classification loss used by the second - stage classifier. Either losses.WeightedSigmoidClassificationLoss or - losses.WeightedSoftmaxClassificationLoss. - second_stage_mask_prediction_loss_weight: A float indicating the scale - factor for second stage mask prediction loss. This is applicable only if - second stage box predictor is configured to predict masks. - hard_example_miner: A losses.HardExampleMiner object (can be None). - parallel_iterations: (Optional) The number of iterations allowed to run - in parallel for calls to tf.map_fn. - add_summaries: boolean (default: True) controlling whether summary ops - should be added to tensorflow graph. - clip_anchors_to_image: Normally, anchors generated for a given image size - are pruned during training if they lie outside the image window. This - option clips the anchors to be within the image instead of pruning. - use_static_shapes: If True, uses implementation of ops with static shape - guarantees. - resize_masks: Indicates whether the masks presend in the groundtruth - should be resized in the model with `image_resizer_fn` - freeze_batchnorm: Whether to freeze batch norm parameters in the first - stage box predictor during training or not. When training with a small - batch size (e.g. 1), it is desirable to freeze batch norm update and - use pretrained batch norm params. - return_raw_detections_during_predict: Whether to return raw detection - boxes in the predict() method. These are decoded boxes that have not - been through postprocessing (i.e. NMS). Default False. - output_final_box_features: Whether to output final box features. If true, - it crops the rpn feature map and passes it through box_classifier then - returns in the output dict as `detection_features`. - output_final_box_rpn_features: Whether to output rpn box features. If - true, it crops the rpn feature map and returns in the output dict as - `detection_features`. - - Raises: - ValueError: If `second_stage_batch_size` > `first_stage_max_proposals` at - training time. - ValueError: If first_stage_anchor_generator is not of type - grid_anchor_generator.GridAnchorGenerator. - """ - # TODO(rathodv): add_summaries is currently unused. Respect that directive - # in the future. - super(FasterRCNNMetaArch, self).__init__(num_classes=num_classes) - - self._is_training = is_training - self._image_resizer_fn = image_resizer_fn - self._resize_masks = resize_masks - self._feature_extractor = feature_extractor - if isinstance(feature_extractor, FasterRCNNKerasFeatureExtractor): - # We delay building the feature extractor until it is used, - # to avoid creating the variables when a model is built just for data - # preprocessing. (This prevents a subtle bug where variable names are - # mismatched across workers, causing only one worker to be able to train) - self._feature_extractor_for_proposal_features = ( - _UNINITIALIZED_FEATURE_EXTRACTOR) - self._feature_extractor_for_box_classifier_features = ( - _UNINITIALIZED_FEATURE_EXTRACTOR) - else: - self._feature_extractor_for_proposal_features = None - self._feature_extractor_for_box_classifier_features = None - - self._number_of_stages = number_of_stages - - self._proposal_target_assigner = first_stage_target_assigner - self._detector_target_assigner = second_stage_target_assigner - # Both proposal and detector target assigners use the same box coder - self._box_coder = self._proposal_target_assigner.box_coder - - # (First stage) Region proposal network parameters - self._first_stage_anchor_generator = first_stage_anchor_generator - self._first_stage_atrous_rate = first_stage_atrous_rate - self._first_stage_box_predictor_depth = first_stage_box_predictor_depth - self._first_stage_box_predictor_kernel_size = ( - first_stage_box_predictor_kernel_size) - self._first_stage_minibatch_size = first_stage_minibatch_size - self._first_stage_sampler = first_stage_sampler - if isinstance(first_stage_box_predictor_arg_scope_fn, - hyperparams_builder.KerasLayerHyperparams): - num_anchors_per_location = ( - self._first_stage_anchor_generator.num_anchors_per_location()) - - conv_hyperparams = ( - first_stage_box_predictor_arg_scope_fn) - self._first_stage_box_predictor_first_conv = ( - tf.keras.Sequential([ - tf.keras.layers.Conv2D( - self._first_stage_box_predictor_depth, - kernel_size=[self._first_stage_box_predictor_kernel_size, - self._first_stage_box_predictor_kernel_size], - dilation_rate=self._first_stage_atrous_rate, - padding='SAME', - name='RPNConv', - **conv_hyperparams.params()), - conv_hyperparams.build_batch_norm( - (self._is_training and not freeze_batchnorm), - name='RPNBatchNorm'), - tf.keras.layers.Lambda( - tf.nn.relu6, - name='RPNActivation') - ], name='FirstStageRPNFeatures')) - self._first_stage_box_predictor = ( - box_predictor_builder.build_convolutional_keras_box_predictor( - is_training=self._is_training, - num_classes=1, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=False, - num_predictions_per_location_list=num_anchors_per_location, - use_dropout=False, - dropout_keep_prob=1.0, - box_code_size=self._box_coder.code_size, - kernel_size=1, - num_layers_before_predictor=0, - min_depth=0, - max_depth=0, - name=self.first_stage_box_predictor_scope)) - else: - self._first_stage_box_predictor_arg_scope_fn = ( - first_stage_box_predictor_arg_scope_fn) - def rpn_box_predictor_feature_extractor(single_rpn_features_to_crop): - with slim.arg_scope(self._first_stage_box_predictor_arg_scope_fn()): - return slim.conv2d( - single_rpn_features_to_crop, - self._first_stage_box_predictor_depth, - kernel_size=[ - self._first_stage_box_predictor_kernel_size, - self._first_stage_box_predictor_kernel_size - ], - rate=self._first_stage_atrous_rate, - activation_fn=tf.nn.relu6, - scope='Conv', - reuse=tf.AUTO_REUSE) - self._first_stage_box_predictor_first_conv = ( - rpn_box_predictor_feature_extractor) - self._first_stage_box_predictor = ( - box_predictor_builder.build_convolutional_box_predictor( - is_training=self._is_training, - num_classes=1, - conv_hyperparams_fn=self._first_stage_box_predictor_arg_scope_fn, - use_dropout=False, - dropout_keep_prob=1.0, - box_code_size=self._box_coder.code_size, - kernel_size=1, - num_layers_before_predictor=0, - min_depth=0, - max_depth=0)) - - self._first_stage_nms_fn = first_stage_non_max_suppression_fn - self._first_stage_max_proposals = first_stage_max_proposals - self._use_static_shapes = use_static_shapes - - self._first_stage_localization_loss = ( - losses.WeightedSmoothL1LocalizationLoss()) - self._first_stage_objectness_loss = ( - losses.WeightedSoftmaxClassificationLoss()) - self._first_stage_loc_loss_weight = first_stage_localization_loss_weight - self._first_stage_obj_loss_weight = first_stage_objectness_loss_weight - - # Per-region cropping parameters - self._crop_and_resize_fn = crop_and_resize_fn - self._initial_crop_size = initial_crop_size - self._maxpool_kernel_size = maxpool_kernel_size - self._maxpool_stride = maxpool_stride - # If max pooling is to be used, build the layer - if maxpool_kernel_size: - self._maxpool_layer = tf.keras.layers.MaxPooling2D( - [self._maxpool_kernel_size, self._maxpool_kernel_size], - strides=self._maxpool_stride, - name='MaxPool2D') - - self._mask_rcnn_box_predictor = second_stage_mask_rcnn_box_predictor - - self._second_stage_batch_size = second_stage_batch_size - self._second_stage_sampler = second_stage_sampler - - self._second_stage_nms_fn = second_stage_non_max_suppression_fn - self._second_stage_score_conversion_fn = second_stage_score_conversion_fn - - self._second_stage_localization_loss = ( - losses.WeightedSmoothL1LocalizationLoss()) - self._second_stage_classification_loss = second_stage_classification_loss - self._second_stage_mask_loss = ( - losses.WeightedSigmoidClassificationLoss()) - self._second_stage_loc_loss_weight = second_stage_localization_loss_weight - self._second_stage_cls_loss_weight = second_stage_classification_loss_weight - self._second_stage_mask_loss_weight = ( - second_stage_mask_prediction_loss_weight) - self._hard_example_miner = hard_example_miner - self._parallel_iterations = parallel_iterations - - self.clip_anchors_to_image = clip_anchors_to_image - - if self._number_of_stages <= 0 or self._number_of_stages > 3: - raise ValueError('Number of stages should be a value in {1, 2, 3}.') - self._batched_prediction_tensor_names = [] - self._return_raw_detections_during_predict = ( - return_raw_detections_during_predict) - self._output_final_box_features = output_final_box_features - self._output_final_box_rpn_features = output_final_box_rpn_features - - @property - def first_stage_feature_extractor_scope(self): - return 'FirstStageFeatureExtractor' - - @property - def second_stage_feature_extractor_scope(self): - return 'SecondStageFeatureExtractor' - - @property - def first_stage_box_predictor_scope(self): - return 'FirstStageBoxPredictor' - - @property - def second_stage_box_predictor_scope(self): - return 'SecondStageBoxPredictor' - - @property - def max_num_proposals(self): - """Max number of proposals (to pad to) for each image in the input batch. - - At training time, this is set to be the `second_stage_batch_size` if hard - example miner is not configured, else it is set to - `first_stage_max_proposals`. At inference time, this is always set to - `first_stage_max_proposals`. - - Returns: - A positive integer. - """ - if self._is_training and not self._hard_example_miner: - return self._second_stage_batch_size - return self._first_stage_max_proposals - - @property - def anchors(self): - if not self._anchors: - raise RuntimeError('anchors have not been constructed yet!') - if not isinstance(self._anchors, box_list.BoxList): - raise RuntimeError('anchors should be a BoxList object, but is not.') - return self._anchors - - @property - def batched_prediction_tensor_names(self): - if not self._batched_prediction_tensor_names: - raise RuntimeError('Must call predict() method to get batched prediction ' - 'tensor names.') - return self._batched_prediction_tensor_names - - @property - def feature_extractor(self): - return self._feature_extractor - - def preprocess(self, inputs): - """Feature-extractor specific preprocessing. - - See base class. - - For Faster R-CNN, we perform image resizing in the base class --- each - class subclassing FasterRCNNMetaArch is responsible for any additional - preprocessing (e.g., scaling pixel values to be in [-1, 1]). - - Args: - inputs: a [batch, height_in, width_in, channels] float tensor representing - a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: a [batch, height_out, width_out, channels] float - tensor representing a batch of images. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - Raises: - ValueError: if inputs tensor does not have type tf.float32 - """ - - with tf.name_scope('Preprocessor'): - (resized_inputs, - true_image_shapes) = shape_utils.resize_images_and_return_shapes( - inputs, self._image_resizer_fn) - - return (self._feature_extractor.preprocess(resized_inputs), - true_image_shapes) - - def _compute_clip_window(self, image_shapes): - """Computes clip window for non max suppression based on image shapes. - - This function assumes that the clip window's left top corner is at (0, 0). - - Args: - image_shapes: A 2-D int32 tensor of shape [batch_size, 3] containing - shapes of images in the batch. Each row represents [height, width, - channels] of an image. - - Returns: - A 2-D float32 tensor of shape [batch_size, 4] containing the clip window - for each image in the form [ymin, xmin, ymax, xmax]. - """ - clip_heights = image_shapes[:, 0] - clip_widths = image_shapes[:, 1] - clip_window = tf.cast( - tf.stack([ - tf.zeros_like(clip_heights), - tf.zeros_like(clip_heights), clip_heights, clip_widths - ], - axis=1), - dtype=tf.float32) - return clip_window - - def _proposal_postprocess(self, rpn_box_encodings, - rpn_objectness_predictions_with_background, anchors, - image_shape, true_image_shapes): - """Wraps over FasterRCNNMetaArch._postprocess_rpn().""" - image_shape_2d = self._image_batch_shape_2d(image_shape) - proposal_boxes_normalized, _, _, num_proposals, _, _ = \ - self._postprocess_rpn( - rpn_box_encodings, rpn_objectness_predictions_with_background, - anchors, image_shape_2d, true_image_shapes) - return proposal_boxes_normalized, num_proposals - - def predict(self, preprocessed_inputs, true_image_shapes, **side_inputs): - """Predicts unpostprocessed tensors from input tensor. - - This function takes an input batch of images and runs it through the - forward pass of the network to yield "raw" un-postprocessed predictions. - If `number_of_stages` is 1, this function only returns first stage - RPN predictions (un-postprocessed). Otherwise it returns both - first stage RPN predictions as well as second stage box classifier - predictions. - - Other remarks: - + Anchor pruning vs. clipping: following the recommendation of the Faster - R-CNN paper, we prune anchors that venture outside the image window at - training time and clip anchors to the image window at inference time. - + Proposal padding: as described at the top of the file, proposals are - padded to self._max_num_proposals and flattened so that proposals from all - images within the input batch are arranged along the same batch dimension. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - **side_inputs: additional tensors that are required by the network. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) rpn_box_predictor_features: A list of 4-D float32 tensor with shape - [batch_size, height_i, width_j, depth] to be used for predicting - proposal boxes and corresponding objectness scores. - 2) rpn_features_to_crop: A list of 4-D float32 tensor with shape - [batch_size, height, width, depth] representing image features to crop - using the proposal boxes predicted by the RPN. - 3) image_shape: a 1-D tensor of shape [4] representing the input - image shape. - 4) rpn_box_encodings: 3-D float tensor of shape - [batch_size, num_anchors, self._box_coder.code_size] containing - predicted boxes. - 5) rpn_objectness_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, 2] containing class - predictions (logits) for each of the anchors. Note that this - tensor *includes* background class predictions (at class index 0). - 6) anchors: A 2-D tensor of shape [num_anchors, 4] representing anchors - for the first stage RPN (in absolute coordinates). Note that - `num_anchors` can differ depending on whether the model is created in - training or inference mode. - 7) feature_maps: A single element list containing a 4-D float32 tensor - with shape batch_size, height, width, depth] representing the RPN - features to crop. - - (and if number_of_stages > 1): - 8) refined_box_encodings: a 3-D tensor with shape - [total_num_proposals, num_classes, self._box_coder.code_size] - representing predicted (final) refined box encodings, where - total_num_proposals=batch_size*self._max_num_proposals. If using - a shared box across classes the shape will instead be - [total_num_proposals, 1, self._box_coder.code_size]. - 9) class_predictions_with_background: a 3-D tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors, where - total_num_proposals=batch_size*self._max_num_proposals. - Note that this tensor *includes* background class predictions - (at class index 0). - 10) num_proposals: An int32 tensor of shape [batch_size] representing - the number of proposals generated by the RPN. `num_proposals` allows - us to keep track of which entries are to be treated as zero paddings - and which are not since we always pad the number of proposals to be - `self.max_num_proposals` for each image. - 11) proposal_boxes: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes in absolute coordinates. - 12) mask_predictions: (optional) a 4-D tensor with shape - [total_num_padded_proposals, num_classes, mask_height, mask_width] - containing instance mask predictions. - 13) raw_detection_boxes: (optional) a - [batch_size, self.max_num_proposals, num_classes, 4] float32 tensor - with detections prior to NMS in normalized coordinates. - 14) raw_detection_feature_map_indices: (optional) a - [batch_size, self.max_num_proposals, num_classes] int32 tensor with - indices indicating which feature map each raw detection box was - produced from. The indices correspond to the elements in the - 'feature_maps' field. - - Raises: - ValueError: If `predict` is called before `preprocess`. - """ - prediction_dict = self._predict_first_stage(preprocessed_inputs) - - if self._number_of_stages >= 2: - prediction_dict.update( - self._predict_second_stage( - prediction_dict['rpn_box_encodings'], - prediction_dict['rpn_objectness_predictions_with_background'], - prediction_dict['rpn_features_to_crop'], - prediction_dict['anchors'], prediction_dict['image_shape'], - true_image_shapes, - **side_inputs)) - - if self._number_of_stages == 3: - prediction_dict = self._predict_third_stage(prediction_dict, - true_image_shapes) - - self._batched_prediction_tensor_names = [ - x for x in prediction_dict if x not in ('image_shape', 'anchors') - ] - return prediction_dict - - def _predict_first_stage(self, preprocessed_inputs): - """First stage of prediction. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) rpn_box_predictor_features: A list of 4-D float32/bfloat16 tensor - with shape [batch_size, height_i, width_j, depth] to be used for - predicting proposal boxes and corresponding objectness scores. - 2) rpn_features_to_crop: A list of 4-D float32/bfloat16 tensor with - shape [batch_size, height, width, depth] representing image features - to crop using the proposal boxes predicted by the RPN. - 3) image_shape: a 1-D tensor of shape [4] representing the input - image shape. - 4) rpn_box_encodings: 3-D float32 tensor of shape - [batch_size, num_anchors, self._box_coder.code_size] containing - predicted boxes. - 5) rpn_objectness_predictions_with_background: 3-D float32 tensor of - shape [batch_size, num_anchors, 2] containing class predictions - (logits) for each of the anchors. Note that this tensor *includes* - background class predictions (at class index 0). - 6) anchors: A 2-D tensor of shape [num_anchors, 4] representing anchors - for the first stage RPN (in absolute coordinates). Note that - `num_anchors` can differ depending on whether the model is created in - training or inference mode. - 7) feature_maps: A single element list containing a 4-D float32 tensor - with shape batch_size, height, width, depth] representing the RPN - features to crop. - """ - (rpn_box_predictor_features, rpn_features_to_crop, anchors_boxlist, - image_shape) = self._extract_rpn_feature_maps(preprocessed_inputs) - (rpn_box_encodings, rpn_objectness_predictions_with_background - ) = self._predict_rpn_proposals(rpn_box_predictor_features) - - # The Faster R-CNN paper recommends pruning anchors that venture outside - # the image window at training time and clipping at inference time. - clip_window = tf.cast(tf.stack([0, 0, image_shape[1], image_shape[2]]), - dtype=tf.float32) - if self._is_training: - if self.clip_anchors_to_image: - anchors_boxlist = box_list_ops.clip_to_window( - anchors_boxlist, clip_window, filter_nonoverlapping=False) - else: - (rpn_box_encodings, rpn_objectness_predictions_with_background, - anchors_boxlist) = self._remove_invalid_anchors_and_predictions( - rpn_box_encodings, rpn_objectness_predictions_with_background, - anchors_boxlist, clip_window) - else: - anchors_boxlist = box_list_ops.clip_to_window( - anchors_boxlist, clip_window, - filter_nonoverlapping=not self._use_static_shapes) - - self._anchors = anchors_boxlist - prediction_dict = { - 'rpn_box_predictor_features': - rpn_box_predictor_features, - 'rpn_features_to_crop': - rpn_features_to_crop, - 'image_shape': - image_shape, - 'rpn_box_encodings': - tf.cast(rpn_box_encodings, dtype=tf.float32), - 'rpn_objectness_predictions_with_background': - tf.cast(rpn_objectness_predictions_with_background, - dtype=tf.float32), - 'anchors': - anchors_boxlist.data['boxes'], - fields.PredictionFields.feature_maps: rpn_features_to_crop - } - return prediction_dict - - def _image_batch_shape_2d(self, image_batch_shape_1d): - """Takes a 1-D image batch shape tensor and converts it to a 2-D tensor. - - Example: - If 1-D image batch shape tensor is [2, 300, 300, 3]. The corresponding 2-D - image batch tensor would be [[300, 300, 3], [300, 300, 3]] - - Args: - image_batch_shape_1d: 1-D tensor of the form [batch_size, height, - width, channels]. - - Returns: - image_batch_shape_2d: 2-D tensor of shape [batch_size, 3] were each row is - of the form [height, width, channels]. - """ - return tf.tile(tf.expand_dims(image_batch_shape_1d[1:], 0), - [image_batch_shape_1d[0], 1]) - - def _predict_second_stage(self, rpn_box_encodings, - rpn_objectness_predictions_with_background, - rpn_features_to_crop, anchors, image_shape, - true_image_shapes, **side_inputs): - """Predicts the output tensors from second stage of Faster R-CNN. - - Args: - rpn_box_encodings: 3-D float tensor of shape - [batch_size, num_valid_anchors, self._box_coder.code_size] containing - predicted boxes. - rpn_objectness_predictions_with_background: 2-D float tensor of shape - [batch_size, num_valid_anchors, 2] containing class - predictions (logits) for each of the anchors. Note that this - tensor *includes* background class predictions (at class index 0). - rpn_features_to_crop: A list of 4-D float32 or bfloat16 tensor with shape - [batch_size, height_i, width_i, depth] representing image features to - crop using the proposal boxes predicted by the RPN. - anchors: 2-D float tensor of shape - [num_anchors, self._box_coder.code_size]. - image_shape: A 1D int32 tensors of size [4] containing the image shape. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - **side_inputs: additional tensors that are required by the network. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) refined_box_encodings: a 3-D float32 tensor with shape - [total_num_proposals, num_classes, self._box_coder.code_size] - representing predicted (final) refined box encodings, where - total_num_proposals=batch_size*self._max_num_proposals. If using a - shared box across classes the shape will instead be - [total_num_proposals, 1, self._box_coder.code_size]. - 2) class_predictions_with_background: a 3-D float32 tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors, where - total_num_proposals=batch_size*self._max_num_proposals. - Note that this tensor *includes* background class predictions - (at class index 0). - 3) num_proposals: An int32 tensor of shape [batch_size] representing the - number of proposals generated by the RPN. `num_proposals` allows us - to keep track of which entries are to be treated as zero paddings and - which are not since we always pad the number of proposals to be - `self.max_num_proposals` for each image. - 4) proposal_boxes: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes in absolute coordinates. - 5) proposal_boxes_normalized: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing decoded proposal - bounding boxes in normalized coordinates. Can be used to override the - boxes proposed by the RPN, thus enabling one to extract features and - get box classification and prediction for externally selected areas - of the image. - 6) box_classifier_features: a 4-D float32/bfloat16 tensor - representing the features for each proposal. - If self._return_raw_detections_during_predict is True, the dictionary - will also contain: - 7) raw_detection_boxes: a 4-D float32 tensor with shape - [batch_size, self.max_num_proposals, num_classes, 4] in normalized - coordinates. - 8) raw_detection_feature_map_indices: a 3-D int32 tensor with shape - [batch_size, self.max_num_proposals, num_classes]. - """ - proposal_boxes_normalized, num_proposals = self._proposal_postprocess( - rpn_box_encodings, rpn_objectness_predictions_with_background, anchors, - image_shape, true_image_shapes) - prediction_dict = self._box_prediction(rpn_features_to_crop, - proposal_boxes_normalized, - image_shape, true_image_shapes, - **side_inputs) - prediction_dict['num_proposals'] = num_proposals - return prediction_dict - - def _box_prediction(self, rpn_features_to_crop, proposal_boxes_normalized, - image_shape, true_image_shapes, **side_inputs): - """Predicts the output tensors from second stage of Faster R-CNN. - - Args: - rpn_features_to_crop: A list 4-D float32 or bfloat16 tensor with shape - [batch_size, height_i, width_i, depth] representing image features to - crop using the proposal boxes predicted by the RPN. - proposal_boxes_normalized: A float tensor with shape [batch_size, - max_num_proposals, 4] representing the (potentially zero padded) - proposal boxes for all images in the batch. These boxes are represented - as normalized coordinates. - image_shape: A 1D int32 tensors of size [4] containing the image shape. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - **side_inputs: additional tensors that are required by the network. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) refined_box_encodings: a 3-D float32 tensor with shape - [total_num_proposals, num_classes, self._box_coder.code_size] - representing predicted (final) refined box encodings, where - total_num_proposals=batch_size*self._max_num_proposals. If using a - shared box across classes the shape will instead be - [total_num_proposals, 1, self._box_coder.code_size]. - 2) class_predictions_with_background: a 3-D float32 tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors, where - total_num_proposals=batch_size*self._max_num_proposals. - Note that this tensor *includes* background class predictions - (at class index 0). - 3) proposal_boxes: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes in absolute coordinates. - 4) proposal_boxes_normalized: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing decoded proposal - bounding boxes in normalized coordinates. Can be used to override the - boxes proposed by the RPN, thus enabling one to extract features and - get box classification and prediction for externally selected areas - of the image. - 5) box_classifier_features: a 4-D float32/bfloat16 tensor - representing the features for each proposal. - If self._return_raw_detections_during_predict is True, the dictionary - will also contain: - 6) raw_detection_boxes: a 4-D float32 tensor with shape - [batch_size, self.max_num_proposals, num_classes, 4] in normalized - coordinates. - 7) raw_detection_feature_map_indices: a 3-D int32 tensor with shape - [batch_size, self.max_num_proposals, num_classes]. - 8) final_anchors: a 3-D float tensor of shape [batch_size, - self.max_num_proposals, 4] containing the reference anchors for raw - detection boxes in normalized coordinates. - """ - flattened_proposal_feature_maps = ( - self._compute_second_stage_input_feature_maps( - rpn_features_to_crop, proposal_boxes_normalized, - image_shape, **side_inputs)) - - box_classifier_features = self._extract_box_classifier_features( - flattened_proposal_feature_maps, **side_inputs) - - if self._mask_rcnn_box_predictor.is_keras_model: - box_predictions = self._mask_rcnn_box_predictor( - [box_classifier_features], - prediction_stage=2) - else: - box_predictions = self._mask_rcnn_box_predictor.predict( - [box_classifier_features], - num_predictions_per_location=[1], - scope=self.second_stage_box_predictor_scope, - prediction_stage=2) - - refined_box_encodings = tf.squeeze( - box_predictions[box_predictor.BOX_ENCODINGS], - axis=1, name='all_refined_box_encodings') - class_predictions_with_background = tf.squeeze( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1, name='all_class_predictions_with_background') - - absolute_proposal_boxes = ops.normalized_to_image_coordinates( - proposal_boxes_normalized, image_shape, self._parallel_iterations) - - prediction_dict = { - 'refined_box_encodings': tf.cast(refined_box_encodings, - dtype=tf.float32), - 'class_predictions_with_background': - tf.cast(class_predictions_with_background, dtype=tf.float32), - 'proposal_boxes': absolute_proposal_boxes, - 'box_classifier_features': box_classifier_features, - 'proposal_boxes_normalized': proposal_boxes_normalized, - 'final_anchors': proposal_boxes_normalized - } - - if self._return_raw_detections_during_predict: - prediction_dict.update(self._raw_detections_and_feature_map_inds( - refined_box_encodings, absolute_proposal_boxes, true_image_shapes)) - - return prediction_dict - - def _raw_detections_and_feature_map_inds( - self, refined_box_encodings, absolute_proposal_boxes, true_image_shapes): - """Returns raw detections and feat map inds from where they originated. - - Args: - refined_box_encodings: [total_num_proposals, num_classes, - self._box_coder.code_size] float32 tensor. - absolute_proposal_boxes: [batch_size, self.max_num_proposals, 4] float32 - tensor representing decoded proposal bounding boxes in absolute - coordinates. - true_image_shapes: [batch, 3] int32 tensor where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - A dictionary with raw detection boxes, and the feature map indices from - which they originated. - """ - box_encodings_batch = tf.reshape( - refined_box_encodings, - [-1, self.max_num_proposals, refined_box_encodings.shape[1], - self._box_coder.code_size]) - raw_detection_boxes_absolute = self._batch_decode_boxes( - box_encodings_batch, absolute_proposal_boxes) - - raw_detection_boxes_normalized = shape_utils.static_or_dynamic_map_fn( - self._normalize_and_clip_boxes, - elems=[raw_detection_boxes_absolute, true_image_shapes], - dtype=tf.float32) - detection_feature_map_indices = tf.zeros_like( - raw_detection_boxes_normalized[:, :, :, 0], dtype=tf.int32) - return { - fields.PredictionFields.raw_detection_boxes: - raw_detection_boxes_normalized, - fields.PredictionFields.raw_detection_feature_map_indices: - detection_feature_map_indices - } - - def _extract_box_classifier_features(self, flattened_feature_maps): - if self._feature_extractor_for_box_classifier_features == ( - _UNINITIALIZED_FEATURE_EXTRACTOR): - self._feature_extractor_for_box_classifier_features = ( - self._feature_extractor.get_box_classifier_feature_extractor_model( - name=self.second_stage_feature_extractor_scope)) - - if self._feature_extractor_for_box_classifier_features: - box_classifier_features = ( - self._feature_extractor_for_box_classifier_features( - flattened_feature_maps)) - else: - box_classifier_features = ( - self._feature_extractor.extract_box_classifier_features( - flattened_feature_maps, - scope=self.second_stage_feature_extractor_scope)) - return box_classifier_features - - def _predict_third_stage(self, prediction_dict, image_shapes): - """Predicts non-box, non-class outputs using refined detections. - - For training, masks as predicted directly on the box_classifier_features, - which are region-features from the initial anchor boxes. - For inference, this happens after calling the post-processing stage, such - that masks are only calculated for the top scored boxes. - - Args: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) refined_box_encodings: a 3-D tensor with shape - [total_num_proposals, num_classes, self._box_coder.code_size] - representing predicted (final) refined box encodings, where - total_num_proposals=batch_size*self._max_num_proposals. If using a - shared box across classes the shape will instead be - [total_num_proposals, 1, self._box_coder.code_size]. - 2) class_predictions_with_background: a 3-D tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors, where - total_num_proposals=batch_size*self._max_num_proposals. - Note that this tensor *includes* background class predictions - (at class index 0). - 3) num_proposals: An int32 tensor of shape [batch_size] representing the - number of proposals generated by the RPN. `num_proposals` allows us - to keep track of which entries are to be treated as zero paddings and - which are not since we always pad the number of proposals to be - `self.max_num_proposals` for each image. - 4) proposal_boxes: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes in absolute coordinates. - 5) box_classifier_features: a 4-D float32 tensor representing the - features for each proposal. - 6) image_shape: a 1-D tensor of shape [4] representing the input - image shape. - image_shapes: A 2-D int32 tensors of shape [batch_size, 3] containing - shapes of images in the batch. - - Returns: - prediction_dict: a dictionary that in addition to the input predictions - does hold the following predictions as well: - 1) mask_predictions: a 4-D tensor with shape - [batch_size, max_detection, mask_height, mask_width] containing - instance mask predictions. - """ - if self._is_training: - curr_box_classifier_features = prediction_dict['box_classifier_features'] - detection_classes = prediction_dict['class_predictions_with_background'] - if self._mask_rcnn_box_predictor.is_keras_model: - mask_predictions = self._mask_rcnn_box_predictor( - [curr_box_classifier_features], - prediction_stage=3) - else: - mask_predictions = self._mask_rcnn_box_predictor.predict( - [curr_box_classifier_features], - num_predictions_per_location=[1], - scope=self.second_stage_box_predictor_scope, - prediction_stage=3) - prediction_dict['mask_predictions'] = tf.squeeze(mask_predictions[ - box_predictor.MASK_PREDICTIONS], axis=1) - else: - detections_dict = self._postprocess_box_classifier( - prediction_dict['refined_box_encodings'], - prediction_dict['class_predictions_with_background'], - prediction_dict['proposal_boxes'], - prediction_dict['num_proposals'], - image_shapes) - prediction_dict.update(detections_dict) - detection_boxes = detections_dict[ - fields.DetectionResultFields.detection_boxes] - detection_classes = detections_dict[ - fields.DetectionResultFields.detection_classes] - rpn_features_to_crop = prediction_dict['rpn_features_to_crop'] - image_shape = prediction_dict['image_shape'] - batch_size = tf.shape(detection_boxes)[0] - max_detection = tf.shape(detection_boxes)[1] - flattened_detected_feature_maps = ( - self._compute_second_stage_input_feature_maps( - rpn_features_to_crop, detection_boxes, image_shape)) - curr_box_classifier_features = self._extract_box_classifier_features( - flattened_detected_feature_maps) - - if self._mask_rcnn_box_predictor.is_keras_model: - mask_predictions = self._mask_rcnn_box_predictor( - [curr_box_classifier_features], - prediction_stage=3) - else: - mask_predictions = self._mask_rcnn_box_predictor.predict( - [curr_box_classifier_features], - num_predictions_per_location=[1], - scope=self.second_stage_box_predictor_scope, - prediction_stage=3) - - detection_masks = tf.squeeze(mask_predictions[ - box_predictor.MASK_PREDICTIONS], axis=1) - - _, num_classes, mask_height, mask_width = ( - detection_masks.get_shape().as_list()) - _, max_detection = detection_classes.get_shape().as_list() - prediction_dict['mask_predictions'] = tf.reshape( - detection_masks, [-1, num_classes, mask_height, mask_width]) - if num_classes > 1: - detection_masks = self._gather_instance_masks( - detection_masks, detection_classes) - - detection_masks = tf.cast(detection_masks, tf.float32) - prediction_dict[fields.DetectionResultFields.detection_masks] = ( - tf.reshape(tf.sigmoid(detection_masks), - [batch_size, max_detection, mask_height, mask_width])) - - return prediction_dict - - def _gather_instance_masks(self, instance_masks, classes): - """Gathers the masks that correspond to classes. - - Args: - instance_masks: A 4-D float32 tensor with shape - [K, num_classes, mask_height, mask_width]. - classes: A 2-D int32 tensor with shape [batch_size, max_detection]. - - Returns: - masks: a 3-D float32 tensor with shape [K, mask_height, mask_width]. - """ - _, num_classes, height, width = instance_masks.get_shape().as_list() - k = tf.shape(instance_masks)[0] - instance_masks = tf.reshape(instance_masks, [-1, height, width]) - classes = tf.cast(tf.reshape(classes, [-1]), dtype=tf.int32) - gather_idx = tf.range(k) * num_classes + classes - return tf.gather(instance_masks, gather_idx) - - def _extract_rpn_feature_maps(self, preprocessed_inputs): - """Extracts RPN features. - - This function extracts two feature maps: a feature map to be directly - fed to a box predictor (to predict location and objectness scores for - proposals) and a feature map from which to crop regions which will then - be sent to the second stage box classifier. - - Args: - preprocessed_inputs: a [batch, height, width, channels] image tensor. - - Returns: - rpn_box_predictor_features: A list of 4-D float32 tensor with shape - [batch, height_i, width_j, depth] to be used for predicting proposal - boxes and corresponding objectness scores. - rpn_features_to_crop: A list of 4-D float32 tensor with shape - [batch, height, width, depth] representing image features to crop using - the proposals boxes. - anchors: A list of BoxList representing anchors (for the RPN) in - absolute coordinates. - image_shape: A 1-D tensor representing the input image shape. - """ - image_shape = tf.shape(preprocessed_inputs) - - rpn_features_to_crop, self.endpoints = self._extract_proposal_features( - preprocessed_inputs) - - # Decide if rpn_features_to_crop is a list. If not make it a list - if not isinstance(rpn_features_to_crop, list): - rpn_features_to_crop = [rpn_features_to_crop] - - feature_map_shapes = [] - rpn_box_predictor_features = [] - for single_rpn_features_to_crop in rpn_features_to_crop: - single_shape = tf.shape(single_rpn_features_to_crop) - feature_map_shapes.append((single_shape[1], single_shape[2])) - single_rpn_box_predictor_features = ( - self._first_stage_box_predictor_first_conv( - single_rpn_features_to_crop)) - rpn_box_predictor_features.append(single_rpn_box_predictor_features) - anchors = box_list_ops.concatenate( - self._first_stage_anchor_generator.generate(feature_map_shapes)) - return (rpn_box_predictor_features, rpn_features_to_crop, - anchors, image_shape) - - def _extract_proposal_features(self, preprocessed_inputs): - if self._feature_extractor_for_proposal_features == ( - _UNINITIALIZED_FEATURE_EXTRACTOR): - self._feature_extractor_for_proposal_features = ( - self._feature_extractor.get_proposal_feature_extractor_model( - name=self.first_stage_feature_extractor_scope)) - if self._feature_extractor_for_proposal_features: - proposal_features = ( - self._feature_extractor_for_proposal_features(preprocessed_inputs), - {}) - else: - proposal_features = ( - self._feature_extractor.extract_proposal_features( - preprocessed_inputs, - scope=self.first_stage_feature_extractor_scope)) - return proposal_features - - def _predict_rpn_proposals(self, rpn_box_predictor_features): - """Adds box predictors to RPN feature map to predict proposals. - - Note resulting tensors will not have been postprocessed. - - Args: - rpn_box_predictor_features: A list of 4-D float32 tensor with shape - [batch, height_i, width_j, depth] to be used for predicting proposal - boxes and corresponding objectness scores. - - Returns: - box_encodings: 3-D float tensor of shape - [batch_size, num_anchors, self._box_coder.code_size] containing - predicted boxes. - objectness_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, 2] containing class - predictions (logits) for each of the anchors. Note that this - tensor *includes* background class predictions (at class index 0). - - Raises: - RuntimeError: if the anchor generator generates anchors corresponding to - multiple feature maps. We currently assume that a single feature map - is generated for the RPN. - """ - num_anchors_per_location = ( - self._first_stage_anchor_generator.num_anchors_per_location()) - - if self._first_stage_box_predictor.is_keras_model: - box_predictions = self._first_stage_box_predictor( - rpn_box_predictor_features) - else: - box_predictions = self._first_stage_box_predictor.predict( - rpn_box_predictor_features, - num_anchors_per_location, - scope=self.first_stage_box_predictor_scope) - - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (tf.squeeze(box_encodings, axis=2), - objectness_predictions_with_background) - - def _remove_invalid_anchors_and_predictions( - self, - box_encodings, - objectness_predictions_with_background, - anchors_boxlist, - clip_window): - """Removes anchors that (partially) fall outside an image. - - Also removes associated box encodings and objectness predictions. - - Args: - box_encodings: 3-D float tensor of shape - [batch_size, num_anchors, self._box_coder.code_size] containing - predicted boxes. - objectness_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, 2] containing class - predictions (logits) for each of the anchors. Note that this - tensor *includes* background class predictions (at class index 0). - anchors_boxlist: A BoxList representing num_anchors anchors (for the RPN) - in absolute coordinates. - clip_window: a 1-D tensor representing the [ymin, xmin, ymax, xmax] - extent of the window to clip/prune to. - - Returns: - box_encodings: 4-D float tensor of shape - [batch_size, num_valid_anchors, self._box_coder.code_size] containing - predicted boxes, where num_valid_anchors <= num_anchors - objectness_predictions_with_background: 2-D float tensor of shape - [batch_size, num_valid_anchors, 2] containing class - predictions (logits) for each of the anchors, where - num_valid_anchors <= num_anchors. Note that this - tensor *includes* background class predictions (at class index 0). - anchors: A BoxList representing num_valid_anchors anchors (for the RPN) in - absolute coordinates. - """ - pruned_anchors_boxlist, keep_indices = box_list_ops.prune_outside_window( - anchors_boxlist, clip_window) - def _batch_gather_kept_indices(predictions_tensor): - return shape_utils.static_or_dynamic_map_fn( - functools.partial(tf.gather, indices=keep_indices), - elems=predictions_tensor, - dtype=tf.float32, - parallel_iterations=self._parallel_iterations, - back_prop=True) - return (_batch_gather_kept_indices(box_encodings), - _batch_gather_kept_indices(objectness_predictions_with_background), - pruned_anchors_boxlist) - - def _flatten_first_two_dimensions(self, inputs): - """Flattens `K-d` tensor along batch dimension to be a `(K-1)-d` tensor. - - Converts `inputs` with shape [A, B, ..., depth] into a tensor of shape - [A * B, ..., depth]. - - Args: - inputs: A float tensor with shape [A, B, ..., depth]. Note that the first - two and last dimensions must be statically defined. - Returns: - A float tensor with shape [A * B, ..., depth] (where the first and last - dimension are statically defined. - """ - combined_shape = shape_utils.combined_static_and_dynamic_shape(inputs) - flattened_shape = tf.stack([combined_shape[0] * combined_shape[1]] + - combined_shape[2:]) - return tf.reshape(inputs, flattened_shape) - - def postprocess(self, prediction_dict, true_image_shapes): - """Convert prediction tensors to final detections. - - This function converts raw predictions tensors to final detection results. - See base class for output format conventions. Note also that by default, - scores are to be interpreted as logits, but if a score_converter is used, - then scores are remapped (and may thus have a different interpretation). - - If number_of_stages=1, the returned results represent proposals from the - first stage RPN and are padded to have self.max_num_proposals for each - image; otherwise, the results can be interpreted as multiclass detections - from the full two-stage model and are padded to self._max_detections. - - Args: - prediction_dict: a dictionary holding prediction tensors (see the - documentation for the predict method. If number_of_stages=1, we - expect prediction_dict to contain `rpn_box_encodings`, - `rpn_objectness_predictions_with_background`, `rpn_features_to_crop`, - and `anchors` fields. Otherwise we expect prediction_dict to - additionally contain `refined_box_encodings`, - `class_predictions_with_background`, `num_proposals`, - `proposal_boxes` and, optionally, `mask_predictions` fields. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - detections: a dictionary containing the following fields - detection_boxes: [batch, max_detection, 4] - detection_scores: [batch, max_detections] - detection_multiclass_scores: [batch, max_detections, 2] - detection_anchor_indices: [batch, max_detections] - detection_classes: [batch, max_detections] - (this entry is only created if rpn_mode=False) - num_detections: [batch] - raw_detection_boxes: [batch, total_detections, 4] - raw_detection_scores: [batch, total_detections, num_classes + 1] - - Raises: - ValueError: If `predict` is called before `preprocess`. - ValueError: If `_output_final_box_features` is true but - rpn_features_to_crop is not in the prediction_dict. - """ - - with tf.name_scope('FirstStagePostprocessor'): - if self._number_of_stages == 1: - - image_shapes = self._image_batch_shape_2d( - prediction_dict['image_shape']) - (proposal_boxes, proposal_scores, proposal_multiclass_scores, - num_proposals, raw_proposal_boxes, - raw_proposal_scores) = self._postprocess_rpn( - prediction_dict['rpn_box_encodings'], - prediction_dict['rpn_objectness_predictions_with_background'], - prediction_dict['anchors'], image_shapes, true_image_shapes) - return { - fields.DetectionResultFields.detection_boxes: - proposal_boxes, - fields.DetectionResultFields.detection_scores: - proposal_scores, - fields.DetectionResultFields.detection_multiclass_scores: - proposal_multiclass_scores, - fields.DetectionResultFields.num_detections: - tf.cast(num_proposals, dtype=tf.float32), - fields.DetectionResultFields.raw_detection_boxes: - raw_proposal_boxes, - fields.DetectionResultFields.raw_detection_scores: - raw_proposal_scores - } - - # TODO(jrru): Remove mask_predictions from _post_process_box_classifier. - if (self._number_of_stages == 2 or - (self._number_of_stages == 3 and self._is_training)): - with tf.name_scope('SecondStagePostprocessor'): - mask_predictions = prediction_dict.get(box_predictor.MASK_PREDICTIONS) - detections_dict = self._postprocess_box_classifier( - prediction_dict['refined_box_encodings'], - prediction_dict['class_predictions_with_background'], - prediction_dict['proposal_boxes'], - prediction_dict['num_proposals'], - true_image_shapes, - mask_predictions=mask_predictions) - - if self._output_final_box_features: - if 'rpn_features_to_crop' not in prediction_dict: - raise ValueError( - 'Please make sure rpn_features_to_crop is in the prediction_dict.' - ) - detections_dict[ - 'detection_features'] = ( - self._add_detection_box_boxclassifier_features_output_node( - detections_dict[ - fields.DetectionResultFields.detection_boxes], - prediction_dict['rpn_features_to_crop'], - prediction_dict['image_shape'])) - if self._output_final_box_rpn_features: - if 'rpn_features_to_crop' not in prediction_dict: - raise ValueError( - 'Please make sure rpn_features_to_crop is in the prediction_dict.' - ) - detections_dict['cropped_rpn_box_features'] = ( - self._add_detection_box_rpn_features_output_node( - detections_dict[fields.DetectionResultFields.detection_boxes], - prediction_dict['rpn_features_to_crop'], - prediction_dict['image_shape'])) - - return detections_dict - - if self._number_of_stages == 3: - # Post processing is already performed in 3rd stage. We need to transfer - # postprocessed tensors from `prediction_dict` to `detections_dict`. - # Remove any items from the prediction dictionary if they are not pure - # Tensors. - non_tensor_predictions = [ - k for k, v in prediction_dict.items() if not isinstance(v, tf.Tensor)] - for k in non_tensor_predictions: - tf.logging.info('Removing {0} from prediction_dict'.format(k)) - prediction_dict.pop(k) - return prediction_dict - - def _add_detection_box_boxclassifier_features_output_node( - self, detection_boxes, rpn_features_to_crop, image_shape): - """Add detection features to outputs. - - This function extracts box features for each box in rpn_features_to_crop. - It returns the extracted box features, reshaped to - [batch size, max_detections, height, width, depth], and average pools - the extracted features across the spatial dimensions and adds a graph node - to the pooled features named 'pooled_detection_features' - - Args: - detection_boxes: a 3-D float32 tensor of shape - [batch_size, max_detections, 4] which represents the bounding boxes. - rpn_features_to_crop: A list of 4-D float32 tensor with shape - [batch, height, width, depth] representing image features to crop using - the proposals boxes. - image_shape: a 1-D tensor of shape [4] representing the image shape. - - Returns: - detection_features: a 4-D float32 tensor of shape - [batch size, max_detections, height, width, depth] representing - cropped image features - """ - with tf.name_scope('SecondStageDetectionFeaturesExtract'): - flattened_detected_feature_maps = ( - self._compute_second_stage_input_feature_maps( - rpn_features_to_crop, detection_boxes, image_shape)) - detection_features_unpooled = self._extract_box_classifier_features( - flattened_detected_feature_maps) - - batch_size = tf.shape(detection_boxes)[0] - max_detections = tf.shape(detection_boxes)[1] - detection_features_pool = tf.reduce_mean( - detection_features_unpooled, axis=[1, 2]) - reshaped_detection_features_pool = tf.reshape( - detection_features_pool, - [batch_size, max_detections, tf.shape(detection_features_pool)[-1]]) - reshaped_detection_features_pool = tf.identity( - reshaped_detection_features_pool, 'pooled_detection_features') - - # TODO(sbeery) add node to extract rpn features here!! - - reshaped_detection_features = tf.reshape( - detection_features_unpooled, - [batch_size, max_detections, - tf.shape(detection_features_unpooled)[1], - tf.shape(detection_features_unpooled)[2], - tf.shape(detection_features_unpooled)[3]]) - - return reshaped_detection_features - - def _add_detection_box_rpn_features_output_node(self, detection_boxes, - rpn_features_to_crop, - image_shape): - """Add detection features to outputs. - - This function extracts box features for each box in rpn_features_to_crop. - It returns the extracted box features, reshaped to - [batch size, max_detections, height, width, depth] - - Args: - detection_boxes: a 3-D float32 tensor of shape - [batch_size, max_detections, 4] which represents the bounding boxes. - rpn_features_to_crop: A list of 4-D float32 tensor with shape - [batch, height, width, depth] representing image features to crop using - the proposals boxes. - image_shape: a 1-D tensor of shape [4] representing the image shape. - - Returns: - detection_features: a 4-D float32 tensor of shape - [batch size, max_detections, height, width, depth] representing - cropped image features - """ - with tf.name_scope('FirstStageDetectionFeaturesExtract'): - flattened_detected_feature_maps = ( - self._compute_second_stage_input_feature_maps( - rpn_features_to_crop, detection_boxes, image_shape)) - - batch_size = tf.shape(detection_boxes)[0] - max_detections = tf.shape(detection_boxes)[1] - reshaped_detection_features = tf.reshape( - flattened_detected_feature_maps, - [batch_size, max_detections, - tf.shape(flattened_detected_feature_maps)[1], - tf.shape(flattened_detected_feature_maps)[2], - tf.shape(flattened_detected_feature_maps)[3]]) - - return reshaped_detection_features - - def _postprocess_rpn(self, - rpn_box_encodings_batch, - rpn_objectness_predictions_with_background_batch, - anchors, - image_shapes, - true_image_shapes): - """Converts first stage prediction tensors from the RPN to proposals. - - This function decodes the raw RPN predictions, runs non-max suppression - on the result. - - Note that the behavior of this function is slightly modified during - training --- specifically, we stop the gradient from passing through the - proposal boxes and we only return a balanced sampled subset of proposals - with size `second_stage_batch_size`. - - Args: - rpn_box_encodings_batch: A 3-D float32 tensor of shape - [batch_size, num_anchors, self._box_coder.code_size] containing - predicted proposal box encodings. - rpn_objectness_predictions_with_background_batch: A 3-D float tensor of - shape [batch_size, num_anchors, 2] containing objectness predictions - (logits) for each of the anchors with 0 corresponding to background - and 1 corresponding to object. - anchors: A 2-D tensor of shape [num_anchors, 4] representing anchors - for the first stage RPN. Note that `num_anchors` can differ depending - on whether the model is created in training or inference mode. - image_shapes: A 2-D tensor of shape [batch, 3] containing the shapes of - images in the batch. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - proposal_boxes: A float tensor with shape - [batch_size, max_num_proposals, 4] representing the (potentially zero - padded) proposal boxes for all images in the batch. These boxes are - represented as normalized coordinates. - proposal_scores: A float tensor with shape - [batch_size, max_num_proposals] representing the (potentially zero - padded) proposal objectness scores for all images in the batch. - proposal_multiclass_scores: A float tensor with shape - [batch_size, max_num_proposals, 2] representing the (potentially zero - padded) proposal multiclass scores for all images in the batch. - num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] - representing the number of proposals predicted for each image in - the batch. - raw_detection_boxes: [batch, total_detections, 4] tensor with decoded - proposal boxes before Non-Max Suppression. - raw_detection_scores: [batch, total_detections, - num_classes_with_background] tensor of multi-class scores for raw - proposal boxes. - """ - rpn_box_encodings_batch = tf.expand_dims(rpn_box_encodings_batch, axis=2) - rpn_encodings_shape = shape_utils.combined_static_and_dynamic_shape( - rpn_box_encodings_batch) - tiled_anchor_boxes = tf.tile( - tf.expand_dims(anchors, 0), [rpn_encodings_shape[0], 1, 1]) - proposal_boxes = self._batch_decode_boxes(rpn_box_encodings_batch, - tiled_anchor_boxes) - raw_proposal_boxes = tf.squeeze(proposal_boxes, axis=2) - rpn_objectness_softmax = tf.nn.softmax( - rpn_objectness_predictions_with_background_batch) - rpn_objectness_softmax_without_background = rpn_objectness_softmax[:, :, 1] - clip_window = self._compute_clip_window(true_image_shapes) - additional_fields = {'multiclass_scores': rpn_objectness_softmax} - (proposal_boxes, proposal_scores, _, _, nmsed_additional_fields, - num_proposals) = self._first_stage_nms_fn( - tf.expand_dims(raw_proposal_boxes, axis=2), - tf.expand_dims(rpn_objectness_softmax_without_background, axis=2), - additional_fields=additional_fields, - clip_window=clip_window) - if self._is_training: - proposal_boxes = tf.stop_gradient(proposal_boxes) - if not self._hard_example_miner: - (groundtruth_boxlists, groundtruth_classes_with_background_list, _, - groundtruth_weights_list - ) = self._format_groundtruth_data(image_shapes) - (proposal_boxes, proposal_scores, - num_proposals) = self._sample_box_classifier_batch( - proposal_boxes, proposal_scores, num_proposals, - groundtruth_boxlists, groundtruth_classes_with_background_list, - groundtruth_weights_list) - # normalize proposal boxes - def normalize_boxes(args): - proposal_boxes_per_image = args[0] - image_shape = args[1] - normalized_boxes_per_image = box_list_ops.to_normalized_coordinates( - box_list.BoxList(proposal_boxes_per_image), image_shape[0], - image_shape[1], check_range=False).get() - return normalized_boxes_per_image - normalized_proposal_boxes = shape_utils.static_or_dynamic_map_fn( - normalize_boxes, elems=[proposal_boxes, image_shapes], dtype=tf.float32) - raw_normalized_proposal_boxes = shape_utils.static_or_dynamic_map_fn( - normalize_boxes, - elems=[raw_proposal_boxes, image_shapes], - dtype=tf.float32) - proposal_multiclass_scores = ( - nmsed_additional_fields.get('multiclass_scores') - if nmsed_additional_fields else None) - return (normalized_proposal_boxes, proposal_scores, - proposal_multiclass_scores, num_proposals, - raw_normalized_proposal_boxes, rpn_objectness_softmax) - - def _sample_box_classifier_batch( - self, - proposal_boxes, - proposal_scores, - num_proposals, - groundtruth_boxlists, - groundtruth_classes_with_background_list, - groundtruth_weights_list): - """Samples a minibatch for second stage. - - Args: - proposal_boxes: A float tensor with shape - [batch_size, num_proposals, 4] representing the (potentially zero - padded) proposal boxes for all images in the batch. These boxes are - represented in absolute coordinates. - proposal_scores: A float tensor with shape - [batch_size, num_proposals] representing the (potentially zero - padded) proposal objectness scores for all images in the batch. - num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] - representing the number of proposals predicted for each image in - the batch. - groundtruth_boxlists: A list of BoxLists containing (absolute) coordinates - of the groundtruth boxes. - groundtruth_classes_with_background_list: A list of 2-D one-hot - (or k-hot) tensors of shape [num_boxes, num_classes+1] containing the - class targets with the 0th index assumed to map to the background class. - groundtruth_weights_list: A list of 1-D tensors of shape [num_boxes] - indicating the weight associated with the groundtruth boxes. - - Returns: - proposal_boxes: A float tensor with shape - [batch_size, second_stage_batch_size, 4] representing the (potentially - zero padded) proposal boxes for all images in the batch. These boxes - are represented in absolute coordinates. - proposal_scores: A float tensor with shape - [batch_size, second_stage_batch_size] representing the (potentially zero - padded) proposal objectness scores for all images in the batch. - num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] - representing the number of proposals predicted for each image in - the batch. - """ - single_image_proposal_box_sample = [] - single_image_proposal_score_sample = [] - single_image_num_proposals_sample = [] - for (single_image_proposal_boxes, - single_image_proposal_scores, - single_image_num_proposals, - single_image_groundtruth_boxlist, - single_image_groundtruth_classes_with_background, - single_image_groundtruth_weights) in zip( - tf.unstack(proposal_boxes), - tf.unstack(proposal_scores), - tf.unstack(num_proposals), - groundtruth_boxlists, - groundtruth_classes_with_background_list, - groundtruth_weights_list): - single_image_boxlist = box_list.BoxList(single_image_proposal_boxes) - single_image_boxlist.add_field(fields.BoxListFields.scores, - single_image_proposal_scores) - sampled_boxlist = self._sample_box_classifier_minibatch_single_image( - single_image_boxlist, - single_image_num_proposals, - single_image_groundtruth_boxlist, - single_image_groundtruth_classes_with_background, - single_image_groundtruth_weights) - sampled_padded_boxlist = box_list_ops.pad_or_clip_box_list( - sampled_boxlist, - num_boxes=self._second_stage_batch_size) - single_image_num_proposals_sample.append(tf.minimum( - sampled_boxlist.num_boxes(), - self._second_stage_batch_size)) - bb = sampled_padded_boxlist.get() - single_image_proposal_box_sample.append(bb) - single_image_proposal_score_sample.append( - sampled_padded_boxlist.get_field(fields.BoxListFields.scores)) - return (tf.stack(single_image_proposal_box_sample), - tf.stack(single_image_proposal_score_sample), - tf.stack(single_image_num_proposals_sample)) - - def _format_groundtruth_data(self, image_shapes): - """Helper function for preparing groundtruth data for target assignment. - - In order to be consistent with the model.DetectionModel interface, - groundtruth boxes are specified in normalized coordinates and classes are - specified as label indices with no assumed background category. To prepare - for target assignment, we: - 1) convert boxes to absolute coordinates, - 2) add a background class at class index 0 - 3) groundtruth instance masks, if available, are resized to match - image_shape. - - Args: - image_shapes: a 2-D int32 tensor of shape [batch_size, 3] containing - shapes of input image in the batch. - - Returns: - groundtruth_boxlists: A list of BoxLists containing (absolute) coordinates - of the groundtruth boxes. - groundtruth_classes_with_background_list: A list of 2-D one-hot - (or k-hot) tensors of shape [num_boxes, num_classes+1] containing the - class targets with the 0th index assumed to map to the background class. - groundtruth_masks_list: If present, a list of 3-D tf.float32 tensors of - shape [num_boxes, image_height, image_width] containing instance masks. - This is set to None if no masks exist in the provided groundtruth. - """ - # pylint: disable=g-complex-comprehension - groundtruth_boxlists = [ - box_list_ops.to_absolute_coordinates( - box_list.BoxList(boxes), image_shapes[i, 0], image_shapes[i, 1]) - for i, boxes in enumerate( - self.groundtruth_lists(fields.BoxListFields.boxes)) - ] - groundtruth_classes_with_background_list = [] - for one_hot_encoding in self.groundtruth_lists( - fields.BoxListFields.classes): - groundtruth_classes_with_background_list.append( - tf.cast( - tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT'), - dtype=tf.float32)) - - groundtruth_masks_list = self._groundtruth_lists.get( - fields.BoxListFields.masks) - # TODO(rathodv): Remove mask resizing once the legacy pipeline is deleted. - if groundtruth_masks_list is not None and self._resize_masks: - resized_masks_list = [] - for mask in groundtruth_masks_list: - - _, resized_mask, _ = self._image_resizer_fn( - # Reuse the given `image_resizer_fn` to resize groundtruth masks. - # `mask` tensor for an image is of the shape [num_masks, - # image_height, image_width]. Below we create a dummy image of the - # the shape [image_height, image_width, 1] to use with - # `image_resizer_fn`. - image=tf.zeros(tf.stack([tf.shape(mask)[1], - tf.shape(mask)[2], 1])), - masks=mask) - resized_masks_list.append(resized_mask) - - groundtruth_masks_list = resized_masks_list - # Masks could be set to bfloat16 in the input pipeline for performance - # reasons. Convert masks back to floating point space here since the rest of - # this module assumes groundtruth to be of float32 type. - float_groundtruth_masks_list = [] - if groundtruth_masks_list: - for mask in groundtruth_masks_list: - float_groundtruth_masks_list.append(tf.cast(mask, tf.float32)) - groundtruth_masks_list = float_groundtruth_masks_list - - if self.groundtruth_has_field(fields.BoxListFields.weights): - groundtruth_weights_list = self.groundtruth_lists( - fields.BoxListFields.weights) - else: - # Set weights for all batch elements equally to 1.0 - groundtruth_weights_list = [] - for groundtruth_classes in groundtruth_classes_with_background_list: - num_gt = tf.shape(groundtruth_classes)[0] - groundtruth_weights = tf.ones(num_gt) - groundtruth_weights_list.append(groundtruth_weights) - - return (groundtruth_boxlists, groundtruth_classes_with_background_list, - groundtruth_masks_list, groundtruth_weights_list) - - def _sample_box_classifier_minibatch_single_image( - self, proposal_boxlist, num_valid_proposals, groundtruth_boxlist, - groundtruth_classes_with_background, groundtruth_weights): - """Samples a mini-batch of proposals to be sent to the box classifier. - - Helper function for self._postprocess_rpn. - - Args: - proposal_boxlist: A BoxList containing K proposal boxes in absolute - coordinates. - num_valid_proposals: Number of valid proposals in the proposal boxlist. - groundtruth_boxlist: A Boxlist containing N groundtruth object boxes in - absolute coordinates. - groundtruth_classes_with_background: A tensor with shape - `[N, self.num_classes + 1]` representing groundtruth classes. The - classes are assumed to be k-hot encoded, and include background as the - zero-th class. - groundtruth_weights: Weights attached to the groundtruth_boxes. - - Returns: - a BoxList contained sampled proposals. - """ - (cls_targets, cls_weights, _, _, _) = self._detector_target_assigner.assign( - proposal_boxlist, - groundtruth_boxlist, - groundtruth_classes_with_background, - unmatched_class_label=tf.constant( - [1] + self._num_classes * [0], dtype=tf.float32), - groundtruth_weights=groundtruth_weights) - # Selects all boxes as candidates if none of them is selected according - # to cls_weights. This could happen as boxes within certain IOU ranges - # are ignored. If triggered, the selected boxes will still be ignored - # during loss computation. - cls_weights = tf.reduce_mean(cls_weights, axis=-1) - positive_indicator = tf.greater(tf.argmax(cls_targets, axis=1), 0) - valid_indicator = tf.logical_and( - tf.range(proposal_boxlist.num_boxes()) < num_valid_proposals, - cls_weights > 0 - ) - selected_positions = self._second_stage_sampler.subsample( - valid_indicator, - self._second_stage_batch_size, - positive_indicator) - return box_list_ops.boolean_mask( - proposal_boxlist, - selected_positions, - use_static_shapes=self._use_static_shapes, - indicator_sum=(self._second_stage_batch_size - if self._use_static_shapes else None)) - - def _compute_second_stage_input_feature_maps(self, features_to_crop, - proposal_boxes_normalized, - image_shape, - **side_inputs): - """Crops to a set of proposals from the feature map for a batch of images. - - Helper function for self._postprocess_rpn. This function calls - `tf.image.crop_and_resize` to create the feature map to be passed to the - second stage box classifier for each proposal. - - Args: - features_to_crop: A float32 tensor with shape - [batch_size, height, width, depth] - proposal_boxes_normalized: A float32 tensor with shape [batch_size, - num_proposals, box_code_size] containing proposal boxes in - normalized coordinates. - image_shape: A 1D int32 tensors of size [4] containing the image shape. - **side_inputs: additional tensors that are required by the network. - - Returns: - A float32 tensor with shape [K, new_height, new_width, depth]. - """ - num_levels = len(features_to_crop) - box_levels = None - if num_levels != 1: - # If there are multiple levels to select, get the box levels - # unit_scale_index: num_levels-2 is chosen based on section 4.2 of - # https://arxiv.org/pdf/1612.03144.pdf and works best for Resnet based - # feature extractor. - box_levels = ops.fpn_feature_levels( - num_levels, num_levels - 2, - tf.sqrt(tf.cast(image_shape[1] * image_shape[2], tf.float32)) / 224.0, - proposal_boxes_normalized) - - cropped_regions = self._flatten_first_two_dimensions( - self._crop_and_resize_fn( - features_to_crop, proposal_boxes_normalized, box_levels, - [self._initial_crop_size, self._initial_crop_size])) - return self._maxpool_layer(cropped_regions) - - def _postprocess_box_classifier(self, - refined_box_encodings, - class_predictions_with_background, - proposal_boxes, - num_proposals, - image_shapes, - mask_predictions=None): - """Converts predictions from the second stage box classifier to detections. - - Args: - refined_box_encodings: a 3-D float tensor with shape - [total_num_padded_proposals, num_classes, self._box_coder.code_size] - representing predicted (final) refined box encodings. If using a shared - box across classes the shape will instead be - [total_num_padded_proposals, 1, 4] - class_predictions_with_background: a 2-D tensor float with shape - [total_num_padded_proposals, num_classes + 1] containing class - predictions (logits) for each of the proposals. Note that this tensor - *includes* background class predictions (at class index 0). - proposal_boxes: a 3-D float tensor with shape - [batch_size, self.max_num_proposals, 4] representing decoded proposal - bounding boxes in absolute coordinates. - num_proposals: a 1-D int32 tensor of shape [batch] representing the number - of proposals predicted for each image in the batch. - image_shapes: a 2-D int32 tensor containing shapes of input image in the - batch. - mask_predictions: (optional) a 4-D float tensor with shape - [total_num_padded_proposals, num_classes, mask_height, mask_width] - containing instance mask prediction logits. - - Returns: - A dictionary containing: - `detection_boxes`: [batch, max_detection, 4] in normalized co-ordinates. - `detection_scores`: [batch, max_detections] - `detection_multiclass_scores`: [batch, max_detections, - num_classes_with_background] tensor with class score distribution for - post-processed detection boxes including background class if any. - `detection_anchor_indices`: [batch, max_detections] with anchor - indices. - `detection_classes`: [batch, max_detections] - `num_detections`: [batch] - `detection_masks`: - (optional) [batch, max_detections, mask_height, mask_width]. Note - that a pixel-wise sigmoid score converter is applied to the detection - masks. - `raw_detection_boxes`: [batch, total_detections, 4] tensor with decoded - detection boxes in normalized coordinates, before Non-Max Suppression. - The value total_detections is the number of second stage anchors - (i.e. the total number of boxes before NMS). - `raw_detection_scores`: [batch, total_detections, - num_classes_with_background] tensor of multi-class scores for - raw detection boxes. The value total_detections is the number of - second stage anchors (i.e. the total number of boxes before NMS). - """ - refined_box_encodings_batch = tf.reshape( - refined_box_encodings, - [-1, - self.max_num_proposals, - refined_box_encodings.shape[1], - self._box_coder.code_size]) - class_predictions_with_background_batch = tf.reshape( - class_predictions_with_background, - [-1, self.max_num_proposals, self.num_classes + 1] - ) - refined_decoded_boxes_batch = self._batch_decode_boxes( - refined_box_encodings_batch, proposal_boxes) - class_predictions_with_background_batch_normalized = ( - self._second_stage_score_conversion_fn( - class_predictions_with_background_batch)) - class_predictions_batch = tf.reshape( - tf.slice(class_predictions_with_background_batch_normalized, - [0, 0, 1], [-1, -1, -1]), - [-1, self.max_num_proposals, self.num_classes]) - clip_window = self._compute_clip_window(image_shapes) - mask_predictions_batch = None - if mask_predictions is not None: - mask_height = shape_utils.get_dim_as_int(mask_predictions.shape[2]) - mask_width = shape_utils.get_dim_as_int(mask_predictions.shape[3]) - mask_predictions = tf.sigmoid(mask_predictions) - mask_predictions_batch = tf.reshape( - mask_predictions, [-1, self.max_num_proposals, - self.num_classes, mask_height, mask_width]) - - batch_size = shape_utils.combined_static_and_dynamic_shape( - refined_box_encodings_batch)[0] - batch_anchor_indices = tf.tile( - tf.expand_dims(tf.range(self.max_num_proposals), 0), - multiples=[batch_size, 1]) - additional_fields = { - 'multiclass_scores': class_predictions_with_background_batch_normalized, - 'anchor_indices': tf.cast(batch_anchor_indices, tf.float32) - } - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, num_detections) = self._second_stage_nms_fn( - refined_decoded_boxes_batch, - class_predictions_batch, - clip_window=clip_window, - change_coordinate_frame=True, - num_valid_boxes=num_proposals, - additional_fields=additional_fields, - masks=mask_predictions_batch) - if refined_decoded_boxes_batch.shape[2] > 1: - class_ids = tf.expand_dims( - tf.argmax(class_predictions_with_background_batch[:, :, 1:], axis=2, - output_type=tf.int32), - axis=-1) - raw_detection_boxes = tf.squeeze( - tf.batch_gather(refined_decoded_boxes_batch, class_ids), axis=2) - else: - raw_detection_boxes = tf.squeeze(refined_decoded_boxes_batch, axis=2) - - raw_normalized_detection_boxes = shape_utils.static_or_dynamic_map_fn( - self._normalize_and_clip_boxes, - elems=[raw_detection_boxes, image_shapes], - dtype=tf.float32) - - detections = { - fields.DetectionResultFields.detection_boxes: - nmsed_boxes, - fields.DetectionResultFields.detection_scores: - nmsed_scores, - fields.DetectionResultFields.detection_classes: - nmsed_classes, - fields.DetectionResultFields.detection_multiclass_scores: - nmsed_additional_fields['multiclass_scores'], - fields.DetectionResultFields.detection_anchor_indices: - tf.cast(nmsed_additional_fields['anchor_indices'], tf.int32), - fields.DetectionResultFields.num_detections: - tf.cast(num_detections, dtype=tf.float32), - fields.DetectionResultFields.raw_detection_boxes: - raw_normalized_detection_boxes, - fields.DetectionResultFields.raw_detection_scores: - class_predictions_with_background_batch_normalized - } - if nmsed_masks is not None: - detections[fields.DetectionResultFields.detection_masks] = nmsed_masks - return detections - - def _batch_decode_boxes(self, box_encodings, anchor_boxes): - """Decodes box encodings with respect to the anchor boxes. - - Args: - box_encodings: a 4-D tensor with shape - [batch_size, num_anchors, num_classes, self._box_coder.code_size] - representing box encodings. - anchor_boxes: [batch_size, num_anchors, self._box_coder.code_size] - representing decoded bounding boxes. If using a shared box across - classes the shape will instead be - [total_num_proposals, 1, self._box_coder.code_size]. - - Returns: - decoded_boxes: a - [batch_size, num_anchors, num_classes, self._box_coder.code_size] - float tensor representing bounding box predictions (for each image in - batch, proposal and class). If using a shared box across classes the - shape will instead be - [batch_size, num_anchors, 1, self._box_coder.code_size]. - """ - combined_shape = shape_utils.combined_static_and_dynamic_shape( - box_encodings) - num_classes = combined_shape[2] - tiled_anchor_boxes = tf.tile( - tf.expand_dims(anchor_boxes, 2), [1, 1, num_classes, 1]) - tiled_anchors_boxlist = box_list.BoxList( - tf.reshape(tiled_anchor_boxes, [-1, 4])) - decoded_boxes = self._box_coder.decode( - tf.reshape(box_encodings, [-1, self._box_coder.code_size]), - tiled_anchors_boxlist) - return tf.reshape(decoded_boxes.get(), - tf.stack([combined_shape[0], combined_shape[1], - num_classes, 4])) - - def _normalize_and_clip_boxes(self, boxes_and_image_shape): - """Normalize and clip boxes.""" - boxes_per_image = boxes_and_image_shape[0] - image_shape = boxes_and_image_shape[1] - - boxes_contains_classes_dim = boxes_per_image.shape.ndims == 3 - if boxes_contains_classes_dim: - boxes_per_image = shape_utils.flatten_first_n_dimensions( - boxes_per_image, 2) - normalized_boxes_per_image = box_list_ops.to_normalized_coordinates( - box_list.BoxList(boxes_per_image), - image_shape[0], - image_shape[1], - check_range=False).get() - - normalized_boxes_per_image = box_list_ops.clip_to_window( - box_list.BoxList(normalized_boxes_per_image), - tf.constant([0.0, 0.0, 1.0, 1.0], tf.float32), - filter_nonoverlapping=False).get() - - if boxes_contains_classes_dim: - max_num_proposals, num_classes, _ = ( - shape_utils.combined_static_and_dynamic_shape( - boxes_and_image_shape[0])) - normalized_boxes_per_image = shape_utils.expand_first_dimension( - normalized_boxes_per_image, [max_num_proposals, num_classes]) - - return normalized_boxes_per_image - - def loss(self, prediction_dict, true_image_shapes, scope=None): - """Compute scalar loss tensors given prediction tensors. - - If number_of_stages=1, only RPN related losses are computed (i.e., - `rpn_localization_loss` and `rpn_objectness_loss`). Otherwise all - losses are computed. - - Args: - prediction_dict: a dictionary holding prediction tensors (see the - documentation for the predict method. If number_of_stages=1, we - expect prediction_dict to contain `rpn_box_encodings`, - `rpn_objectness_predictions_with_background`, `rpn_features_to_crop`, - `image_shape`, and `anchors` fields. Otherwise we expect - prediction_dict to additionally contain `refined_box_encodings`, - `class_predictions_with_background`, `num_proposals`, and - `proposal_boxes` fields. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - scope: Optional scope name. - - Returns: - a dictionary mapping loss keys (`first_stage_localization_loss`, - `first_stage_objectness_loss`, 'second_stage_localization_loss', - 'second_stage_classification_loss') to scalar tensors representing - corresponding loss values. - """ - with tf.name_scope(scope, 'Loss', prediction_dict.values()): - (groundtruth_boxlists, groundtruth_classes_with_background_list, - groundtruth_masks_list, groundtruth_weights_list - ) = self._format_groundtruth_data( - self._image_batch_shape_2d(prediction_dict['image_shape'])) - loss_dict = self._loss_rpn( - prediction_dict['rpn_box_encodings'], - prediction_dict['rpn_objectness_predictions_with_background'], - prediction_dict['anchors'], groundtruth_boxlists, - groundtruth_classes_with_background_list, groundtruth_weights_list) - if self._number_of_stages > 1: - loss_dict.update( - self._loss_box_classifier( - prediction_dict['refined_box_encodings'], - prediction_dict['class_predictions_with_background'], - prediction_dict['proposal_boxes'], - prediction_dict['num_proposals'], groundtruth_boxlists, - groundtruth_classes_with_background_list, - groundtruth_weights_list, prediction_dict['image_shape'], - prediction_dict.get('mask_predictions'), groundtruth_masks_list, - prediction_dict.get( - fields.DetectionResultFields.detection_boxes), - prediction_dict.get( - fields.DetectionResultFields.num_detections))) - return loss_dict - - def _loss_rpn(self, rpn_box_encodings, - rpn_objectness_predictions_with_background, anchors, - groundtruth_boxlists, groundtruth_classes_with_background_list, - groundtruth_weights_list): - """Computes scalar RPN loss tensors. - - Uses self._proposal_target_assigner to obtain regression and classification - targets for the first stage RPN, samples a "minibatch" of anchors to - participate in the loss computation, and returns the RPN losses. - - Args: - rpn_box_encodings: A 3-D float tensor of shape - [batch_size, num_anchors, self._box_coder.code_size] containing - predicted proposal box encodings. - rpn_objectness_predictions_with_background: A 2-D float tensor of shape - [batch_size, num_anchors, 2] containing objectness predictions - (logits) for each of the anchors with 0 corresponding to background - and 1 corresponding to object. - anchors: A 2-D tensor of shape [num_anchors, 4] representing anchors - for the first stage RPN. Note that `num_anchors` can differ depending - on whether the model is created in training or inference mode. - groundtruth_boxlists: A list of BoxLists containing coordinates of the - groundtruth boxes. - groundtruth_classes_with_background_list: A list of 2-D one-hot - (or k-hot) tensors of shape [num_boxes, num_classes+1] containing the - class targets with the 0th index assumed to map to the background class. - groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape - [num_boxes] containing weights for groundtruth boxes. - - Returns: - a dictionary mapping loss keys (`first_stage_localization_loss`, - `first_stage_objectness_loss`) to scalar tensors representing - corresponding loss values. - """ - with tf.name_scope('RPNLoss'): - (batch_cls_targets, batch_cls_weights, batch_reg_targets, - batch_reg_weights, _) = target_assigner.batch_assign_targets( - target_assigner=self._proposal_target_assigner, - anchors_batch=box_list.BoxList(anchors), - gt_box_batch=groundtruth_boxlists, - gt_class_targets_batch=(len(groundtruth_boxlists) * [None]), - gt_weights_batch=groundtruth_weights_list) - batch_cls_weights = tf.reduce_mean(batch_cls_weights, axis=2) - batch_cls_targets = tf.squeeze(batch_cls_targets, axis=2) - - def _minibatch_subsample_fn(inputs): - cls_targets, cls_weights = inputs - return self._first_stage_sampler.subsample( - tf.cast(cls_weights, tf.bool), - self._first_stage_minibatch_size, tf.cast(cls_targets, tf.bool)) - batch_sampled_indices = tf.cast(shape_utils.static_or_dynamic_map_fn( - _minibatch_subsample_fn, - [batch_cls_targets, batch_cls_weights], - dtype=tf.bool, - parallel_iterations=self._parallel_iterations, - back_prop=True), dtype=tf.float32) - - # Normalize by number of examples in sampled minibatch - normalizer = tf.maximum( - tf.reduce_sum(batch_sampled_indices, axis=1), 1.0) - batch_one_hot_targets = tf.one_hot( - tf.cast(batch_cls_targets, dtype=tf.int32), depth=2) - sampled_reg_indices = tf.multiply(batch_sampled_indices, - batch_reg_weights) - - losses_mask = None - if self.groundtruth_has_field(fields.InputDataFields.is_annotated): - losses_mask = tf.stack(self.groundtruth_lists( - fields.InputDataFields.is_annotated)) - localization_losses = self._first_stage_localization_loss( - rpn_box_encodings, batch_reg_targets, weights=sampled_reg_indices, - losses_mask=losses_mask) - objectness_losses = self._first_stage_objectness_loss( - rpn_objectness_predictions_with_background, - batch_one_hot_targets, - weights=tf.expand_dims(batch_sampled_indices, axis=-1), - losses_mask=losses_mask) - localization_loss = tf.reduce_mean( - tf.reduce_sum(localization_losses, axis=1) / normalizer) - objectness_loss = tf.reduce_mean( - tf.reduce_sum(objectness_losses, axis=1) / normalizer) - - localization_loss = tf.multiply(self._first_stage_loc_loss_weight, - localization_loss, - name='localization_loss') - objectness_loss = tf.multiply(self._first_stage_obj_loss_weight, - objectness_loss, name='objectness_loss') - loss_dict = {'Loss/RPNLoss/localization_loss': localization_loss, - 'Loss/RPNLoss/objectness_loss': objectness_loss} - return loss_dict - - def _loss_box_classifier(self, - refined_box_encodings, - class_predictions_with_background, - proposal_boxes, - num_proposals, - groundtruth_boxlists, - groundtruth_classes_with_background_list, - groundtruth_weights_list, - image_shape, - prediction_masks=None, - groundtruth_masks_list=None, - detection_boxes=None, - num_detections=None): - """Computes scalar box classifier loss tensors. - - Uses self._detector_target_assigner to obtain regression and classification - targets for the second stage box classifier, optionally performs - hard mining, and returns losses. All losses are computed independently - for each image and then averaged across the batch. - Please note that for boxes and masks with multiple labels, the box - regression and mask prediction losses are only computed for one label. - - This function assumes that the proposal boxes in the "padded" regions are - actually zero (and thus should not be matched to). - - - Args: - refined_box_encodings: a 3-D tensor with shape - [total_num_proposals, num_classes, box_coder.code_size] representing - predicted (final) refined box encodings. If using a shared box across - classes this will instead have shape - [total_num_proposals, 1, box_coder.code_size]. - class_predictions_with_background: a 2-D tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors. Note that this tensor - *includes* background class predictions (at class index 0). - proposal_boxes: [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes. - num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] - representing the number of proposals predicted for each image in - the batch. - groundtruth_boxlists: a list of BoxLists containing coordinates of the - groundtruth boxes. - groundtruth_classes_with_background_list: a list of 2-D one-hot - (or k-hot) tensors of shape [num_boxes, num_classes + 1] containing the - class targets with the 0th index assumed to map to the background class. - groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape - [num_boxes] containing weights for groundtruth boxes. - image_shape: a 1-D tensor of shape [4] representing the image shape. - prediction_masks: an optional 4-D tensor with shape [total_num_proposals, - num_classes, mask_height, mask_width] containing the instance masks for - each box. - groundtruth_masks_list: an optional list of 3-D tensors of shape - [num_boxes, image_height, image_width] containing the instance masks for - each of the boxes. - detection_boxes: 3-D float tensor of shape [batch, - max_total_detections, 4] containing post-processed detection boxes in - normalized co-ordinates. - num_detections: 1-D int32 tensor of shape [batch] containing number of - valid detections in `detection_boxes`. - - Returns: - a dictionary mapping loss keys ('second_stage_localization_loss', - 'second_stage_classification_loss') to scalar tensors representing - corresponding loss values. - - Raises: - ValueError: if `predict_instance_masks` in - second_stage_mask_rcnn_box_predictor is True and - `groundtruth_masks_list` is not provided. - """ - with tf.name_scope('BoxClassifierLoss'): - paddings_indicator = self._padded_batched_proposals_indicator( - num_proposals, proposal_boxes.shape[1]) - proposal_boxlists = [ - box_list.BoxList(proposal_boxes_single_image) - for proposal_boxes_single_image in tf.unstack(proposal_boxes)] - batch_size = len(proposal_boxlists) - - num_proposals_or_one = tf.cast(tf.expand_dims( - tf.maximum(num_proposals, tf.ones_like(num_proposals)), 1), - dtype=tf.float32) - normalizer = tf.tile(num_proposals_or_one, - [1, self.max_num_proposals]) * batch_size - - (batch_cls_targets_with_background, batch_cls_weights, batch_reg_targets, - batch_reg_weights, _) = target_assigner.batch_assign_targets( - target_assigner=self._detector_target_assigner, - anchors_batch=proposal_boxlists, - gt_box_batch=groundtruth_boxlists, - gt_class_targets_batch=groundtruth_classes_with_background_list, - unmatched_class_label=tf.constant( - [1] + self._num_classes * [0], dtype=tf.float32), - gt_weights_batch=groundtruth_weights_list) - if self.groundtruth_has_field( - fields.InputDataFields.groundtruth_labeled_classes): - gt_labeled_classes = self.groundtruth_lists( - fields.InputDataFields.groundtruth_labeled_classes) - gt_labeled_classes = tf.pad( - gt_labeled_classes, [[0, 0], [1, 0]], - mode='CONSTANT', - constant_values=1) - batch_cls_weights *= tf.expand_dims(gt_labeled_classes, 1) - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [batch_size, self.max_num_proposals, -1]) - - flat_cls_targets_with_background = tf.reshape( - batch_cls_targets_with_background, - [batch_size * self.max_num_proposals, -1]) - one_hot_flat_cls_targets_with_background = tf.argmax( - flat_cls_targets_with_background, axis=1) - one_hot_flat_cls_targets_with_background = tf.one_hot( - one_hot_flat_cls_targets_with_background, - flat_cls_targets_with_background.get_shape()[1]) - - # If using a shared box across classes use directly - if refined_box_encodings.shape[1] == 1: - reshaped_refined_box_encodings = tf.reshape( - refined_box_encodings, - [batch_size, self.max_num_proposals, self._box_coder.code_size]) - # For anchors with multiple labels, picks refined_location_encodings - # for just one class to avoid over-counting for regression loss and - # (optionally) mask loss. - else: - reshaped_refined_box_encodings = ( - self._get_refined_encodings_for_postitive_class( - refined_box_encodings, - one_hot_flat_cls_targets_with_background, batch_size)) - - losses_mask = None - if self.groundtruth_has_field(fields.InputDataFields.is_annotated): - losses_mask = tf.stack(self.groundtruth_lists( - fields.InputDataFields.is_annotated)) - second_stage_loc_losses = self._second_stage_localization_loss( - reshaped_refined_box_encodings, - batch_reg_targets, - weights=batch_reg_weights, - losses_mask=losses_mask) / normalizer - second_stage_cls_losses = ops.reduce_sum_trailing_dimensions( - self._second_stage_classification_loss( - class_predictions_with_background, - batch_cls_targets_with_background, - weights=batch_cls_weights, - losses_mask=losses_mask), - ndims=2) / normalizer - - second_stage_loc_loss = tf.reduce_sum( - second_stage_loc_losses * tf.cast(paddings_indicator, - dtype=tf.float32)) - second_stage_cls_loss = tf.reduce_sum( - second_stage_cls_losses * tf.cast(paddings_indicator, - dtype=tf.float32)) - - if self._hard_example_miner: - (second_stage_loc_loss, second_stage_cls_loss - ) = self._unpad_proposals_and_apply_hard_mining( - proposal_boxlists, second_stage_loc_losses, - second_stage_cls_losses, num_proposals) - localization_loss = tf.multiply(self._second_stage_loc_loss_weight, - second_stage_loc_loss, - name='localization_loss') - - classification_loss = tf.multiply(self._second_stage_cls_loss_weight, - second_stage_cls_loss, - name='classification_loss') - - loss_dict = {'Loss/BoxClassifierLoss/localization_loss': - localization_loss, - 'Loss/BoxClassifierLoss/classification_loss': - classification_loss} - second_stage_mask_loss = None - if prediction_masks is not None: - if groundtruth_masks_list is None: - raise ValueError('Groundtruth instance masks not provided. ' - 'Please configure input reader.') - - if not self._is_training: - (proposal_boxes, proposal_boxlists, paddings_indicator, - one_hot_flat_cls_targets_with_background - ) = self._get_mask_proposal_boxes_and_classes( - detection_boxes, num_detections, image_shape, - groundtruth_boxlists, groundtruth_classes_with_background_list, - groundtruth_weights_list) - unmatched_mask_label = tf.zeros(image_shape[1:3], dtype=tf.float32) - (batch_mask_targets, _, _, batch_mask_target_weights, - _) = target_assigner.batch_assign_targets( - target_assigner=self._detector_target_assigner, - anchors_batch=proposal_boxlists, - gt_box_batch=groundtruth_boxlists, - gt_class_targets_batch=groundtruth_masks_list, - unmatched_class_label=unmatched_mask_label, - gt_weights_batch=groundtruth_weights_list) - - # Pad the prediction_masks with to add zeros for background class to be - # consistent with class predictions. - if prediction_masks.get_shape().as_list()[1] == 1: - # Class agnostic masks or masks for one-class prediction. Logic for - # both cases is the same since background predictions are ignored - # through the batch_mask_target_weights. - prediction_masks_masked_by_class_targets = prediction_masks - else: - prediction_masks_with_background = tf.pad( - prediction_masks, [[0, 0], [1, 0], [0, 0], [0, 0]]) - prediction_masks_masked_by_class_targets = tf.boolean_mask( - prediction_masks_with_background, - tf.greater(one_hot_flat_cls_targets_with_background, 0)) - - mask_height = shape_utils.get_dim_as_int(prediction_masks.shape[2]) - mask_width = shape_utils.get_dim_as_int(prediction_masks.shape[3]) - reshaped_prediction_masks = tf.reshape( - prediction_masks_masked_by_class_targets, - [batch_size, -1, mask_height * mask_width]) - - batch_mask_targets_shape = tf.shape(batch_mask_targets) - flat_gt_masks = tf.reshape(batch_mask_targets, - [-1, batch_mask_targets_shape[2], - batch_mask_targets_shape[3]]) - - # Use normalized proposals to crop mask targets from image masks. - flat_normalized_proposals = box_list_ops.to_normalized_coordinates( - box_list.BoxList(tf.reshape(proposal_boxes, [-1, 4])), - image_shape[1], image_shape[2], check_range=False).get() - - flat_cropped_gt_mask = self._crop_and_resize_fn( - [tf.expand_dims(flat_gt_masks, -1)], - tf.expand_dims(flat_normalized_proposals, axis=1), None, - [mask_height, mask_width]) - # Without stopping gradients into cropped groundtruth masks the - # performance with 100-padded groundtruth masks when batch size > 1 is - # about 4% worse. - # TODO(rathodv): Investigate this since we don't expect any variables - # upstream of flat_cropped_gt_mask. - flat_cropped_gt_mask = tf.stop_gradient(flat_cropped_gt_mask) - - batch_cropped_gt_mask = tf.reshape( - flat_cropped_gt_mask, - [batch_size, -1, mask_height * mask_width]) - - mask_losses_weights = ( - batch_mask_target_weights * tf.cast(paddings_indicator, - dtype=tf.float32)) - mask_losses = self._second_stage_mask_loss( - reshaped_prediction_masks, - batch_cropped_gt_mask, - weights=tf.expand_dims(mask_losses_weights, axis=-1), - losses_mask=losses_mask) - total_mask_loss = tf.reduce_sum(mask_losses) - normalizer = tf.maximum( - tf.reduce_sum(mask_losses_weights * mask_height * mask_width), 1.0) - second_stage_mask_loss = total_mask_loss / normalizer - - if second_stage_mask_loss is not None: - mask_loss = tf.multiply(self._second_stage_mask_loss_weight, - second_stage_mask_loss, name='mask_loss') - loss_dict['Loss/BoxClassifierLoss/mask_loss'] = mask_loss - return loss_dict - - def _get_mask_proposal_boxes_and_classes( - self, detection_boxes, num_detections, image_shape, groundtruth_boxlists, - groundtruth_classes_with_background_list, groundtruth_weights_list): - """Returns proposal boxes and class targets to compute evaluation mask loss. - - During evaluation, detection boxes are used to extract features for mask - prediction. Therefore, to compute mask loss during evaluation detection - boxes must be used to compute correct class and mask targets. This function - returns boxes and classes in the correct format for computing mask targets - during evaluation. - - Args: - detection_boxes: A 3-D float tensor of shape [batch, max_detection_boxes, - 4] containing detection boxes in normalized co-ordinates. - num_detections: A 1-D float tensor of shape [batch] containing number of - valid boxes in `detection_boxes`. - image_shape: A 1-D tensor of shape [4] containing image tensor shape. - groundtruth_boxlists: A list of groundtruth boxlists. - groundtruth_classes_with_background_list: A list of groundtruth classes. - groundtruth_weights_list: A list of groundtruth weights. - Return: - mask_proposal_boxes: detection boxes to use for mask proposals in absolute - co-ordinates. - mask_proposal_boxlists: `mask_proposal_boxes` in a list of BoxLists in - absolute co-ordinates. - mask_proposal_paddings_indicator: a tensor indicating valid boxes. - mask_proposal_one_hot_flat_cls_targets_with_background: Class targets - computed using detection boxes. - """ - batch, max_num_detections, _ = detection_boxes.shape.as_list() - proposal_boxes = tf.reshape(box_list_ops.to_absolute_coordinates( - box_list.BoxList(tf.reshape(detection_boxes, [-1, 4])), image_shape[1], - image_shape[2]).get(), [batch, max_num_detections, 4]) - proposal_boxlists = [ - box_list.BoxList(detection_boxes_single_image) - for detection_boxes_single_image in tf.unstack(proposal_boxes) - ] - paddings_indicator = self._padded_batched_proposals_indicator( - tf.cast(num_detections, dtype=tf.int32), detection_boxes.shape[1]) - (batch_cls_targets_with_background, _, _, _, - _) = target_assigner.batch_assign_targets( - target_assigner=self._detector_target_assigner, - anchors_batch=proposal_boxlists, - gt_box_batch=groundtruth_boxlists, - gt_class_targets_batch=groundtruth_classes_with_background_list, - unmatched_class_label=tf.constant( - [1] + self._num_classes * [0], dtype=tf.float32), - gt_weights_batch=groundtruth_weights_list) - flat_cls_targets_with_background = tf.reshape( - batch_cls_targets_with_background, [-1, self._num_classes + 1]) - one_hot_flat_cls_targets_with_background = tf.argmax( - flat_cls_targets_with_background, axis=1) - one_hot_flat_cls_targets_with_background = tf.one_hot( - one_hot_flat_cls_targets_with_background, - flat_cls_targets_with_background.get_shape()[1]) - return (proposal_boxes, proposal_boxlists, paddings_indicator, - one_hot_flat_cls_targets_with_background) - - def _get_refined_encodings_for_postitive_class( - self, refined_box_encodings, flat_cls_targets_with_background, - batch_size): - # We only predict refined location encodings for the non background - # classes, but we now pad it to make it compatible with the class - # predictions - refined_box_encodings_with_background = tf.pad(refined_box_encodings, - [[0, 0], [1, 0], [0, 0]]) - refined_box_encodings_masked_by_class_targets = ( - box_list_ops.boolean_mask( - box_list.BoxList( - tf.reshape(refined_box_encodings_with_background, - [-1, self._box_coder.code_size])), - tf.reshape(tf.greater(flat_cls_targets_with_background, 0), [-1]), - use_static_shapes=self._use_static_shapes, - indicator_sum=batch_size * self.max_num_proposals - if self._use_static_shapes else None).get()) - return tf.reshape( - refined_box_encodings_masked_by_class_targets, [ - batch_size, self.max_num_proposals, - self._box_coder.code_size - ]) - - def _padded_batched_proposals_indicator(self, - num_proposals, - max_num_proposals): - """Creates indicator matrix of non-pad elements of padded batch proposals. - - Args: - num_proposals: Tensor of type tf.int32 with shape [batch_size]. - max_num_proposals: Maximum number of proposals per image (integer). - - Returns: - A Tensor of type tf.bool with shape [batch_size, max_num_proposals]. - """ - batch_size = tf.size(num_proposals) - tiled_num_proposals = tf.tile( - tf.expand_dims(num_proposals, 1), [1, max_num_proposals]) - tiled_proposal_index = tf.tile( - tf.expand_dims(tf.range(max_num_proposals), 0), [batch_size, 1]) - return tf.greater(tiled_num_proposals, tiled_proposal_index) - - def _unpad_proposals_and_apply_hard_mining(self, - proposal_boxlists, - second_stage_loc_losses, - second_stage_cls_losses, - num_proposals): - """Unpads proposals and applies hard mining. - - Args: - proposal_boxlists: A list of `batch_size` BoxLists each representing - `self.max_num_proposals` representing decoded proposal bounding boxes - for each image. - second_stage_loc_losses: A Tensor of type `float32`. A tensor of shape - `[batch_size, self.max_num_proposals]` representing per-anchor - second stage localization loss values. - second_stage_cls_losses: A Tensor of type `float32`. A tensor of shape - `[batch_size, self.max_num_proposals]` representing per-anchor - second stage classification loss values. - num_proposals: A Tensor of type `int32`. A 1-D tensor of shape [batch] - representing the number of proposals predicted for each image in - the batch. - - Returns: - second_stage_loc_loss: A scalar float32 tensor representing the second - stage localization loss. - second_stage_cls_loss: A scalar float32 tensor representing the second - stage classification loss. - """ - for (proposal_boxlist, single_image_loc_loss, single_image_cls_loss, - single_image_num_proposals) in zip( - proposal_boxlists, - tf.unstack(second_stage_loc_losses), - tf.unstack(second_stage_cls_losses), - tf.unstack(num_proposals)): - proposal_boxlist = box_list.BoxList( - tf.slice(proposal_boxlist.get(), - [0, 0], [single_image_num_proposals, -1])) - single_image_loc_loss = tf.slice(single_image_loc_loss, - [0], [single_image_num_proposals]) - single_image_cls_loss = tf.slice(single_image_cls_loss, - [0], [single_image_num_proposals]) - return self._hard_example_miner( - location_losses=tf.expand_dims(single_image_loc_loss, 0), - cls_losses=tf.expand_dims(single_image_cls_loss, 0), - decoded_boxlist_list=[proposal_boxlist]) - - def regularization_losses(self): - """Returns a list of regularization losses for this model. - - Returns a list of regularization losses for this model that the estimator - needs to use during training/optimization. - - Returns: - A list of regularization loss tensors. - """ - all_losses = [] - slim_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) - # Copy the slim losses to avoid modifying the collection - if slim_losses: - all_losses.extend(slim_losses) - # TODO(kaftan): Possibly raise an error if the feature extractors are - # uninitialized in Keras. - if self._feature_extractor_for_proposal_features: - if (self._feature_extractor_for_proposal_features != - _UNINITIALIZED_FEATURE_EXTRACTOR): - all_losses.extend(self._feature_extractor_for_proposal_features.losses) - if isinstance(self._first_stage_box_predictor_first_conv, - tf.keras.Model): - all_losses.extend( - self._first_stage_box_predictor_first_conv.losses) - if self._first_stage_box_predictor.is_keras_model: - all_losses.extend(self._first_stage_box_predictor.losses) - if self._feature_extractor_for_box_classifier_features: - if (self._feature_extractor_for_box_classifier_features != - _UNINITIALIZED_FEATURE_EXTRACTOR): - all_losses.extend( - self._feature_extractor_for_box_classifier_features.losses) - if self._mask_rcnn_box_predictor: - if self._mask_rcnn_box_predictor.is_keras_model: - all_losses.extend(self._mask_rcnn_box_predictor.losses) - return all_losses - - def restore_map(self, - fine_tune_checkpoint_type='detection', - load_all_detection_checkpoint_vars=False): - """Returns a map of variables to load from a foreign checkpoint. - - See parent class for details. - - Args: - fine_tune_checkpoint_type: whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`. Default 'detection'. - load_all_detection_checkpoint_vars: whether to load all variables (when - `fine_tune_checkpoint_type` is `detection`). If False, only variables - within the feature extractor scopes are included. Default False. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - Raises: - ValueError: if fine_tune_checkpoint_type is neither `classification` - nor `detection`. - """ - if fine_tune_checkpoint_type not in ['detection', 'classification']: - raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format( - fine_tune_checkpoint_type)) - if fine_tune_checkpoint_type == 'classification': - return self._feature_extractor.restore_from_classification_checkpoint_fn( - self.first_stage_feature_extractor_scope, - self.second_stage_feature_extractor_scope) - - variables_to_restore = variables_helper.get_global_variables_safely() - variables_to_restore.append(tf.train.get_or_create_global_step()) - # Only load feature extractor variables to be consistent with loading from - # a classification checkpoint. - include_patterns = None - if not load_all_detection_checkpoint_vars: - include_patterns = [ - self.first_stage_feature_extractor_scope, - self.second_stage_feature_extractor_scope - ] - feature_extractor_variables = slim.filter_variables( - variables_to_restore, include_patterns=include_patterns) - return {var.op.name: var for var in feature_extractor_variables} - - def restore_from_objects(self, fine_tune_checkpoint_type='detection'): - """Returns a map of Trackable objects to load from a foreign checkpoint. - - Returns a dictionary of Tensorflow 2 Trackable objects (e.g. tf.Module - or Checkpoint). This enables the model to initialize based on weights from - another task. For example, the feature extractor variables from a - classification model can be used to bootstrap training of an object - detector. When loading from an object detection model, the checkpoint model - should have the same parameters as this detection model with exception of - the num_classes parameter. - - Note that this function is intended to be used to restore Keras-based - models when running Tensorflow 2, whereas restore_map (above) is intended - to be used to restore Slim-based models when running Tensorflow 1.x. - - Args: - fine_tune_checkpoint_type: whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`. Default 'detection'. - - Returns: - A dict mapping keys to Trackable objects (tf.Module or Checkpoint). - """ - if fine_tune_checkpoint_type == 'classification': - return { - 'feature_extractor': - self._feature_extractor.classification_backbone - } - elif fine_tune_checkpoint_type == 'detection': - fake_model = tf.train.Checkpoint( - _feature_extractor_for_box_classifier_features= - self._feature_extractor_for_box_classifier_features, - _feature_extractor_for_proposal_features= - self._feature_extractor_for_proposal_features) - return {'model': fake_model} - elif fine_tune_checkpoint_type == 'full': - return {'model': self} - else: - raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format( - fine_tune_checkpoint_type)) - - def updates(self): - """Returns a list of update operators for this model. - - Returns a list of update operators for this model that must be executed at - each training step. The estimator's train op needs to have a control - dependency on these updates. - - Returns: - A list of update operators. - """ - update_ops = [] - slim_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) - # Copy the slim ops to avoid modifying the collection - if slim_update_ops: - update_ops.extend(slim_update_ops) - # Passing None to get_updates_for grabs updates that should always be - # executed and don't depend on any model inputs in the graph. - # (E.g. if there was some count that should be incremented every time a - # model is run). - # - # Passing inputs grabs updates that are transitively computed from the - # model inputs being passed in. - # (E.g. a batchnorm update depends on the observed inputs) - if self._feature_extractor_for_proposal_features: - if (self._feature_extractor_for_proposal_features != - _UNINITIALIZED_FEATURE_EXTRACTOR): - update_ops.extend( - self._feature_extractor_for_proposal_features.get_updates_for(None)) - update_ops.extend( - self._feature_extractor_for_proposal_features.get_updates_for( - self._feature_extractor_for_proposal_features.inputs)) - if isinstance(self._first_stage_box_predictor_first_conv, - tf.keras.Model): - update_ops.extend( - self._first_stage_box_predictor_first_conv.get_updates_for( - None)) - update_ops.extend( - self._first_stage_box_predictor_first_conv.get_updates_for( - self._first_stage_box_predictor_first_conv.inputs)) - if self._first_stage_box_predictor.is_keras_model: - update_ops.extend( - self._first_stage_box_predictor.get_updates_for(None)) - update_ops.extend( - self._first_stage_box_predictor.get_updates_for( - self._first_stage_box_predictor.inputs)) - if self._feature_extractor_for_box_classifier_features: - if (self._feature_extractor_for_box_classifier_features != - _UNINITIALIZED_FEATURE_EXTRACTOR): - update_ops.extend( - self._feature_extractor_for_box_classifier_features.get_updates_for( - None)) - update_ops.extend( - self._feature_extractor_for_box_classifier_features.get_updates_for( - self._feature_extractor_for_box_classifier_features.inputs)) - if self._mask_rcnn_box_predictor: - if self._mask_rcnn_box_predictor.is_keras_model: - update_ops.extend( - self._mask_rcnn_box_predictor.get_updates_for(None)) - update_ops.extend( - self._mask_rcnn_box_predictor.get_updates_for( - self._mask_rcnn_box_predictor.inputs)) - return update_ops diff --git a/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test.py b/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test.py deleted file mode 100644 index 5c0369d0fc4..00000000000 --- a/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test.py +++ /dev/null @@ -1,512 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.meta_architectures.faster_rcnn_meta_arch.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl.testing import parameterized -import numpy as np -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import faster_rcnn_meta_arch_test_lib -from object_detection.utils import test_utils - - -class FasterRCNNMetaArchTest( - faster_rcnn_meta_arch_test_lib.FasterRCNNMetaArchTestBase, - parameterized.TestCase): - - def test_postprocess_second_stage_only_inference_mode_with_masks(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, second_stage_batch_size=6) - - batch_size = 2 - total_num_padded_proposals = batch_size * model.max_num_proposals - def graph_fn(): - proposal_boxes = tf.constant( - [[[1, 1, 2, 3], - [0, 0, 1, 1], - [.5, .5, .6, .6], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], - [[2, 3, 6, 8], - [1, 2, 5, 3], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]], dtype=tf.float32) - num_proposals = tf.constant([3, 2], dtype=tf.int32) - refined_box_encodings = tf.zeros( - [total_num_padded_proposals, model.num_classes, 4], dtype=tf.float32) - class_predictions_with_background = tf.ones( - [total_num_padded_proposals, model.num_classes+1], dtype=tf.float32) - image_shape = tf.constant([batch_size, 36, 48, 3], dtype=tf.int32) - - mask_height = 2 - mask_width = 2 - mask_predictions = 30. * tf.ones( - [total_num_padded_proposals, model.num_classes, - mask_height, mask_width], dtype=tf.float32) - - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - detections = model.postprocess({ - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'num_proposals': num_proposals, - 'proposal_boxes': proposal_boxes, - 'image_shape': image_shape, - 'mask_predictions': mask_predictions - }, true_image_shapes) - return (detections['detection_boxes'], - detections['detection_scores'], - detections['detection_classes'], - detections['num_detections'], - detections['detection_masks']) - (detection_boxes, detection_scores, detection_classes, - num_detections, detection_masks) = self.execute_cpu(graph_fn, [], graph=g) - exp_detection_masks = np.array([[[[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]]], - [[[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[0, 0], [0, 0]]]]) - self.assertAllEqual(detection_boxes.shape, [2, 5, 4]) - self.assertAllClose(detection_scores, - [[1, 1, 1, 1, 1], [1, 1, 1, 1, 0]]) - self.assertAllClose(detection_classes, - [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]]) - self.assertAllClose(num_detections, [5, 4]) - self.assertAllClose(detection_masks, exp_detection_masks) - self.assertTrue(np.amax(detection_masks <= 1.0)) - self.assertTrue(np.amin(detection_masks >= 0.0)) - - def test_postprocess_second_stage_only_inference_mode_with_calibration(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, second_stage_batch_size=6, - calibration_mapping_value=0.5) - - batch_size = 2 - total_num_padded_proposals = batch_size * model.max_num_proposals - def graph_fn(): - proposal_boxes = tf.constant( - [[[1, 1, 2, 3], - [0, 0, 1, 1], - [.5, .5, .6, .6], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], - [[2, 3, 6, 8], - [1, 2, 5, 3], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]], dtype=tf.float32) - num_proposals = tf.constant([3, 2], dtype=tf.int32) - refined_box_encodings = tf.zeros( - [total_num_padded_proposals, model.num_classes, 4], dtype=tf.float32) - class_predictions_with_background = tf.ones( - [total_num_padded_proposals, model.num_classes+1], dtype=tf.float32) - image_shape = tf.constant([batch_size, 36, 48, 3], dtype=tf.int32) - - mask_height = 2 - mask_width = 2 - mask_predictions = 30. * tf.ones( - [total_num_padded_proposals, model.num_classes, - mask_height, mask_width], dtype=tf.float32) - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - detections = model.postprocess({ - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'num_proposals': num_proposals, - 'proposal_boxes': proposal_boxes, - 'image_shape': image_shape, - 'mask_predictions': mask_predictions - }, true_image_shapes) - return (detections['detection_boxes'], - detections['detection_scores'], - detections['detection_classes'], - detections['num_detections'], - detections['detection_masks']) - (detection_boxes, detection_scores, detection_classes, - num_detections, detection_masks) = self.execute_cpu(graph_fn, [], graph=g) - exp_detection_masks = np.array([[[[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]]], - [[[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[1, 1], [1, 1]], - [[0, 0], [0, 0]]]]) - - self.assertAllEqual(detection_boxes.shape, [2, 5, 4]) - # All scores map to 0.5, except for the final one, which is pruned. - self.assertAllClose(detection_scores, - [[0.5, 0.5, 0.5, 0.5, 0.5], - [0.5, 0.5, 0.5, 0.5, 0.0]]) - self.assertAllClose(detection_classes, - [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]]) - self.assertAllClose(num_detections, [5, 4]) - self.assertAllClose(detection_masks, - exp_detection_masks) - self.assertTrue(np.amax(detection_masks <= 1.0)) - self.assertTrue(np.amin(detection_masks >= 0.0)) - - def test_postprocess_second_stage_only_inference_mode_with_shared_boxes(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, second_stage_batch_size=6) - - batch_size = 2 - total_num_padded_proposals = batch_size * model.max_num_proposals - def graph_fn(): - proposal_boxes = tf.constant( - [[[1, 1, 2, 3], - [0, 0, 1, 1], - [.5, .5, .6, .6], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], - [[2, 3, 6, 8], - [1, 2, 5, 3], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]], dtype=tf.float32) - num_proposals = tf.constant([3, 2], dtype=tf.int32) - - # This has 1 box instead of one for each class. - refined_box_encodings = tf.zeros( - [total_num_padded_proposals, 1, 4], dtype=tf.float32) - class_predictions_with_background = tf.ones( - [total_num_padded_proposals, model.num_classes+1], dtype=tf.float32) - image_shape = tf.constant([batch_size, 36, 48, 3], dtype=tf.int32) - - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - detections = model.postprocess({ - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'num_proposals': num_proposals, - 'proposal_boxes': proposal_boxes, - 'image_shape': image_shape, - }, true_image_shapes) - return (detections['detection_boxes'], - detections['detection_scores'], - detections['detection_classes'], - detections['num_detections']) - (detection_boxes, detection_scores, detection_classes, - num_detections) = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllEqual(detection_boxes.shape, [2, 5, 4]) - self.assertAllClose(detection_scores, - [[1, 1, 1, 1, 1], [1, 1, 1, 1, 0]]) - self.assertAllClose(detection_classes, - [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]]) - self.assertAllClose(num_detections, [5, 4]) - - @parameterized.parameters( - {'masks_are_class_agnostic': False}, - {'masks_are_class_agnostic': True}, - ) - def test_predict_correct_shapes_in_inference_mode_three_stages_with_masks( - self, masks_are_class_agnostic): - batch_size = 2 - image_size = 10 - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=3, - second_stage_batch_size=2, - predict_masks=True, - masks_are_class_agnostic=masks_are_class_agnostic) - def graph_fn(): - shape = [tf.random_uniform([], minval=batch_size, maxval=batch_size + 1, - dtype=tf.int32), - tf.random_uniform([], minval=image_size, maxval=image_size + 1, - dtype=tf.int32), - tf.random_uniform([], minval=image_size, maxval=image_size + 1, - dtype=tf.int32), - 3] - image = tf.zeros(shape) - _, true_image_shapes = model.preprocess(image) - detections = model.predict(image, true_image_shapes) - return (detections['detection_boxes'], detections['detection_classes'], - detections['detection_scores'], detections['num_detections'], - detections['detection_masks'], detections['mask_predictions']) - (detection_boxes, detection_scores, detection_classes, - num_detections, detection_masks, - mask_predictions) = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllEqual(detection_boxes.shape, [2, 5, 4]) - self.assertAllEqual(detection_masks.shape, - [2, 5, 14, 14]) - self.assertAllEqual(detection_classes.shape, [2, 5]) - self.assertAllEqual(detection_scores.shape, [2, 5]) - self.assertAllEqual(num_detections.shape, [2]) - num_classes = 1 if masks_are_class_agnostic else 2 - self.assertAllEqual(mask_predictions.shape, - [10, num_classes, 14, 14]) - - def test_raw_detection_boxes_and_anchor_indices_correct(self): - batch_size = 2 - image_size = 10 - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, - second_stage_batch_size=2, - share_box_across_classes=True, - return_raw_detections_during_predict=True) - def graph_fn(): - shape = [tf.random_uniform([], minval=batch_size, maxval=batch_size + 1, - dtype=tf.int32), - tf.random_uniform([], minval=image_size, maxval=image_size + 1, - dtype=tf.int32), - tf.random_uniform([], minval=image_size, maxval=image_size + 1, - dtype=tf.int32), - 3] - image = tf.zeros(shape) - _, true_image_shapes = model.preprocess(image) - predict_tensor_dict = model.predict(image, true_image_shapes) - detections = model.postprocess(predict_tensor_dict, true_image_shapes) - return (detections['detection_boxes'], - detections['num_detections'], - detections['detection_anchor_indices'], - detections['raw_detection_boxes'], - predict_tensor_dict['raw_detection_boxes']) - (detection_boxes, num_detections, detection_anchor_indices, - raw_detection_boxes, - predict_raw_detection_boxes) = self.execute_cpu(graph_fn, [], graph=g) - - # Verify that the raw detections from predict and postprocess are the - # same. - self.assertAllClose( - np.squeeze(predict_raw_detection_boxes), raw_detection_boxes) - # Verify that the raw detection boxes at detection anchor indices are the - # same as the postprocessed detections. - for i in range(batch_size): - num_detections_per_image = int(num_detections[i]) - detection_boxes_per_image = detection_boxes[i][ - :num_detections_per_image] - detection_anchor_indices_per_image = detection_anchor_indices[i][ - :num_detections_per_image] - raw_detections_per_image = np.squeeze(raw_detection_boxes[i]) - raw_detections_at_anchor_indices = raw_detections_per_image[ - detection_anchor_indices_per_image] - self.assertAllClose(detection_boxes_per_image, - raw_detections_at_anchor_indices) - - @parameterized.parameters( - {'masks_are_class_agnostic': False}, - {'masks_are_class_agnostic': True}, - ) - def test_predict_gives_correct_shapes_in_train_mode_both_stages_with_masks( - self, masks_are_class_agnostic): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, - number_of_stages=3, - second_stage_batch_size=7, - predict_masks=True, - masks_are_class_agnostic=masks_are_class_agnostic) - batch_size = 2 - image_size = 10 - max_num_proposals = 7 - def graph_fn(): - image_shape = (batch_size, image_size, image_size, 3) - preprocessed_inputs = tf.zeros(image_shape, dtype=tf.float32) - groundtruth_boxes_list = [ - tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), - tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32) - ] - groundtruth_classes_list = [ - tf.constant([[1, 0], [0, 1]], dtype=tf.float32), - tf.constant([[1, 0], [1, 0]], dtype=tf.float32) - ] - groundtruth_weights_list = [ - tf.constant([1, 1], dtype=tf.float32), - tf.constant([1, 1], dtype=tf.float32)] - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth( - groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_weights_list=groundtruth_weights_list) - - result_tensor_dict = model.predict(preprocessed_inputs, true_image_shapes) - return result_tensor_dict['mask_predictions'] - mask_shape_1 = 1 if masks_are_class_agnostic else model._num_classes - mask_out = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllEqual(mask_out.shape, - (2 * max_num_proposals, mask_shape_1, 14, 14)) - - def test_postprocess_third_stage_only_inference_mode(self): - batch_size = 2 - initial_crop_size = 3 - maxpool_stride = 1 - height = initial_crop_size // maxpool_stride - width = initial_crop_size // maxpool_stride - depth = 3 - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, number_of_stages=3, - second_stage_batch_size=6, predict_masks=True) - total_num_padded_proposals = batch_size * model.max_num_proposals - def graph_fn(images_shape, num_proposals, proposal_boxes, - refined_box_encodings, class_predictions_with_background): - _, true_image_shapes = model.preprocess( - tf.zeros(images_shape)) - detections = model.postprocess({ - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'num_proposals': num_proposals, - 'proposal_boxes': proposal_boxes, - 'image_shape': images_shape, - 'detection_boxes': tf.zeros([2, 5, 4]), - 'detection_masks': tf.zeros([2, 5, 14, 14]), - 'detection_scores': tf.zeros([2, 5]), - 'detection_classes': tf.zeros([2, 5]), - 'num_detections': tf.zeros([2]), - 'detection_features': tf.zeros([2, 5, width, height, depth]) - }, true_image_shapes) - return (detections['detection_boxes'], detections['detection_masks'], - detections['detection_scores'], detections['detection_classes'], - detections['num_detections'], - detections['detection_features']) - images_shape = np.array((2, 36, 48, 3), dtype=np.int32) - proposal_boxes = np.array( - [[[1, 1, 2, 3], - [0, 0, 1, 1], - [.5, .5, .6, .6], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], - [[2, 3, 6, 8], - [1, 2, 5, 3], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]]) - num_proposals = np.array([3, 2], dtype=np.int32) - refined_box_encodings = np.zeros( - [total_num_padded_proposals, model.num_classes, 4]) - class_predictions_with_background = np.ones( - [total_num_padded_proposals, model.num_classes+1]) - - (detection_boxes, detection_masks, detection_scores, detection_classes, - num_detections, - detection_features) = self.execute_cpu(graph_fn, - [images_shape, num_proposals, - proposal_boxes, - refined_box_encodings, - class_predictions_with_background], - graph=g) - self.assertAllEqual(detection_boxes.shape, [2, 5, 4]) - self.assertAllEqual(detection_masks.shape, [2, 5, 14, 14]) - self.assertAllClose(detection_scores.shape, [2, 5]) - self.assertAllClose(detection_classes.shape, [2, 5]) - self.assertAllClose(num_detections.shape, [2]) - self.assertTrue(np.amax(detection_masks <= 1.0)) - self.assertTrue(np.amin(detection_masks >= 0.0)) - self.assertAllEqual(detection_features.shape, - [2, 5, width, height, depth]) - self.assertGreaterEqual(np.amax(detection_features), 0) - - def _get_box_classifier_features_shape(self, - image_size, - batch_size, - max_num_proposals, - initial_crop_size, - maxpool_stride, - num_features): - return (batch_size * max_num_proposals, - initial_crop_size // maxpool_stride, - initial_crop_size // maxpool_stride, - num_features) - - def test_output_final_box_features(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, - second_stage_batch_size=6, - output_final_box_features=True) - - batch_size = 2 - total_num_padded_proposals = batch_size * model.max_num_proposals - def graph_fn(): - proposal_boxes = tf.constant([[[1, 1, 2, 3], [0, 0, 1, 1], - [.5, .5, .6, .6], 4 * [0], 4 * [0], - 4 * [0], 4 * [0], 4 * [0]], - [[2, 3, 6, 8], [1, 2, 5, 3], 4 * [0], - 4 * [0], 4 * [0], 4 * [0], 4 * [0], - 4 * [0]]], - dtype=tf.float32) - num_proposals = tf.constant([3, 2], dtype=tf.int32) - refined_box_encodings = tf.zeros( - [total_num_padded_proposals, model.num_classes, 4], dtype=tf.float32) - class_predictions_with_background = tf.ones( - [total_num_padded_proposals, model.num_classes + 1], dtype=tf.float32) - image_shape = tf.constant([batch_size, 36, 48, 3], dtype=tf.int32) - - mask_height = 2 - mask_width = 2 - mask_predictions = 30. * tf.ones([ - total_num_padded_proposals, model.num_classes, mask_height, mask_width - ], - dtype=tf.float32) - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - rpn_features_to_crop = tf.ones((batch_size, mask_height, mask_width, 3), - tf.float32) - detections = model.postprocess( - { - 'refined_box_encodings': - refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'num_proposals': - num_proposals, - 'proposal_boxes': - proposal_boxes, - 'image_shape': - image_shape, - 'mask_predictions': - mask_predictions, - 'rpn_features_to_crop': - [rpn_features_to_crop] - }, true_image_shapes) - self.assertIn('detection_features', detections) - return (detections['detection_boxes'], detections['detection_scores'], - detections['detection_classes'], detections['num_detections'], - detections['detection_masks']) - (detection_boxes, detection_scores, detection_classes, num_detections, - detection_masks) = self.execute_cpu(graph_fn, [], graph=g) - exp_detection_masks = np.array([[[[1, 1], [1, 1]], [[1, 1], [1, 1]], - [[1, 1], [1, 1]], [[1, 1], [1, 1]], - [[1, 1], [1, 1]]], - [[[1, 1], [1, 1]], [[1, 1], [1, 1]], - [[1, 1], [1, 1]], [[1, 1], [1, 1]], - [[0, 0], [0, 0]]]]) - - self.assertAllEqual(detection_boxes.shape, [2, 5, 4]) - self.assertAllClose(detection_scores, - [[1, 1, 1, 1, 1], [1, 1, 1, 1, 0]]) - self.assertAllClose(detection_classes, - [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]]) - self.assertAllClose(num_detections, [5, 4]) - self.assertAllClose(detection_masks, - exp_detection_masks) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test_lib.py b/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test_lib.py deleted file mode 100644 index d5d454de9f9..00000000000 --- a/research/object_detection/meta_architectures/faster_rcnn_meta_arch_test_lib.py +++ /dev/null @@ -1,2182 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.meta_architectures.faster_rcnn_meta_arch.""" -import functools -from absl.testing import parameterized - -import numpy as np -import six -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.anchor_generators import grid_anchor_generator -from object_detection.anchor_generators import multiscale_grid_anchor_generator -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.builders import post_processing_builder -from object_detection.core import balanced_positive_negative_sampler as sampler -from object_detection.core import losses -from object_detection.core import post_processing -from object_detection.core import target_assigner -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.protos import box_predictor_pb2 -from object_detection.protos import hyperparams_pb2 -from object_detection.protos import post_processing_pb2 -from object_detection.utils import spatial_transform_ops as spatial_ops -from object_detection.utils import test_case -from object_detection.utils import test_utils -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top -try: - import tf_slim as slim -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - -BOX_CODE_SIZE = 4 - - -class FakeFasterRCNNFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Fake feature extractor to use in tests.""" - - def __init__(self): - super(FakeFasterRCNNFeatureExtractor, self).__init__( - is_training=False, - first_stage_features_stride=32, - reuse_weights=None, - weight_decay=0.0) - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def _extract_proposal_features(self, preprocessed_inputs, scope): - with tf.variable_scope('mock_model'): - proposal_features = 0 * slim.conv2d( - preprocessed_inputs, num_outputs=3, kernel_size=1, scope='layer1') - return proposal_features, {} - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - with tf.variable_scope('mock_model'): - return 0 * slim.conv2d( - proposal_feature_maps, num_outputs=3, kernel_size=1, scope='layer2') - - -class FakeFasterRCNNMultiLevelFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Fake feature extractor to use in tests.""" - - def __init__(self): - super(FakeFasterRCNNMultiLevelFeatureExtractor, self).__init__( - is_training=False, - first_stage_features_stride=32, - reuse_weights=None, - weight_decay=0.0) - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def _extract_proposal_features(self, preprocessed_inputs, scope): - with tf.variable_scope('mock_model'): - proposal_features_1 = 0 * slim.conv2d( - preprocessed_inputs, num_outputs=3, kernel_size=3, scope='layer1', - padding='VALID') - proposal_features_2 = 0 * slim.conv2d( - proposal_features_1, num_outputs=3, kernel_size=3, scope='layer2', - padding='VALID') - return [proposal_features_1, proposal_features_2], {} - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - with tf.variable_scope('mock_model'): - return 0 * slim.conv2d( - proposal_feature_maps, num_outputs=3, kernel_size=1, scope='layer3') - - -class FakeFasterRCNNKerasFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor): - """Fake feature extractor to use in tests.""" - - def __init__(self): - super(FakeFasterRCNNKerasFeatureExtractor, self).__init__( - is_training=False, - first_stage_features_stride=32, - weight_decay=0.0) - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def get_proposal_feature_extractor_model(self, name): - - class ProposalFeatureExtractor(tf.keras.Model): - """Dummy proposal feature extraction.""" - - def __init__(self, name): - super(ProposalFeatureExtractor, self).__init__(name=name) - self.conv = None - - def build(self, input_shape): - self.conv = tf.keras.layers.Conv2D( - 3, kernel_size=1, padding='SAME', name='layer1') - - def call(self, inputs): - return self.conv(inputs) - - return ProposalFeatureExtractor(name=name) - - def get_box_classifier_feature_extractor_model(self, name): - return tf.keras.Sequential([tf.keras.layers.Conv2D( - 3, kernel_size=1, padding='SAME', name=name + '_layer2')]) - - -class FakeFasterRCNNKerasMultilevelFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor): - """Fake feature extractor to use in tests.""" - - def __init__(self): - super(FakeFasterRCNNKerasMultilevelFeatureExtractor, self).__init__( - is_training=False, - first_stage_features_stride=32, - weight_decay=0.0) - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def get_proposal_feature_extractor_model(self, name): - - class ProposalFeatureExtractor(tf.keras.Model): - """Dummy proposal feature extraction.""" - - def __init__(self, name): - super(ProposalFeatureExtractor, self).__init__(name=name) - self.conv = None - - def build(self, input_shape): - self.conv = tf.keras.layers.Conv2D( - 3, kernel_size=3, name='layer1') - self.conv_1 = tf.keras.layers.Conv2D( - 3, kernel_size=3, name='layer1') - - def call(self, inputs): - output_1 = self.conv(inputs) - output_2 = self.conv_1(output_1) - return [output_1, output_2] - - return ProposalFeatureExtractor(name=name) - - -class FasterRCNNMetaArchTestBase(test_case.TestCase, parameterized.TestCase): - """Base class to test Faster R-CNN and R-FCN meta architectures.""" - - def _build_arg_scope_with_hyperparams(self, - hyperparams_text_proto, - is_training): - hyperparams = hyperparams_pb2.Hyperparams() - text_format.Merge(hyperparams_text_proto, hyperparams) - return hyperparams_builder.build(hyperparams, is_training=is_training) - - def _build_keras_layer_hyperparams(self, hyperparams_text_proto): - hyperparams = hyperparams_pb2.Hyperparams() - text_format.Merge(hyperparams_text_proto, hyperparams) - return hyperparams_builder.KerasLayerHyperparams(hyperparams) - - def _get_second_stage_box_predictor_text_proto( - self, share_box_across_classes=False): - share_box_field = 'true' if share_box_across_classes else 'false' - box_predictor_text_proto = """ - mask_rcnn_box_predictor {{ - fc_hyperparams {{ - op: FC - activation: NONE - regularizer {{ - l2_regularizer {{ - weight: 0.0005 - }} - }} - initializer {{ - variance_scaling_initializer {{ - factor: 1.0 - uniform: true - mode: FAN_AVG - }} - }} - }} - share_box_across_classes: {share_box_across_classes} - }} - """.format(share_box_across_classes=share_box_field) - return box_predictor_text_proto - - def _add_mask_to_second_stage_box_predictor_text_proto( - self, masks_are_class_agnostic=False): - agnostic = 'true' if masks_are_class_agnostic else 'false' - box_predictor_text_proto = """ - mask_rcnn_box_predictor { - predict_instance_masks: true - masks_are_class_agnostic: """ + agnostic + """ - mask_height: 14 - mask_width: 14 - conv_hyperparams { - op: CONV - regularizer { - l2_regularizer { - weight: 0.0 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - } - } - } - } - """ - return box_predictor_text_proto - - def _get_second_stage_box_predictor(self, num_classes, is_training, - predict_masks, masks_are_class_agnostic, - share_box_across_classes=False, - use_keras=False): - box_predictor_proto = box_predictor_pb2.BoxPredictor() - text_format.Merge(self._get_second_stage_box_predictor_text_proto( - share_box_across_classes), box_predictor_proto) - if predict_masks: - text_format.Merge( - self._add_mask_to_second_stage_box_predictor_text_proto( - masks_are_class_agnostic), - box_predictor_proto) - - if use_keras: - return box_predictor_builder.build_keras( - hyperparams_builder.KerasLayerHyperparams, - inplace_batchnorm_update=False, - freeze_batchnorm=False, - box_predictor_config=box_predictor_proto, - num_classes=num_classes, - num_predictions_per_location_list=None, - is_training=is_training) - else: - return box_predictor_builder.build( - hyperparams_builder.build, - box_predictor_proto, - num_classes=num_classes, - is_training=is_training) - - def _get_model(self, box_predictor, keras_model=False, **common_kwargs): - return faster_rcnn_meta_arch.FasterRCNNMetaArch( - initial_crop_size=3, - maxpool_kernel_size=1, - maxpool_stride=1, - second_stage_mask_rcnn_box_predictor=box_predictor, - **common_kwargs) - - def _build_model(self, - is_training, - number_of_stages, - second_stage_batch_size, - first_stage_max_proposals=8, - num_classes=2, - hard_mining=False, - softmax_second_stage_classification_loss=True, - predict_masks=False, - pad_to_max_dimension=None, - masks_are_class_agnostic=False, - use_matmul_crop_and_resize=False, - clip_anchors_to_image=False, - use_matmul_gather_in_matcher=False, - use_static_shapes=False, - calibration_mapping_value=None, - share_box_across_classes=False, - return_raw_detections_during_predict=False, - output_final_box_features=False, - multi_level=False): - use_keras = tf_version.is_tf2() - def image_resizer_fn(image, masks=None): - """Fake image resizer function.""" - resized_inputs = [] - resized_image = tf.identity(image) - if pad_to_max_dimension is not None: - resized_image = tf.image.pad_to_bounding_box(image, 0, 0, - pad_to_max_dimension, - pad_to_max_dimension) - resized_inputs.append(resized_image) - if masks is not None: - resized_masks = tf.identity(masks) - if pad_to_max_dimension is not None: - resized_masks = tf.image.pad_to_bounding_box(tf.transpose(masks, - [1, 2, 0]), - 0, 0, - pad_to_max_dimension, - pad_to_max_dimension) - resized_masks = tf.transpose(resized_masks, [2, 0, 1]) - resized_inputs.append(resized_masks) - resized_inputs.append(tf.shape(image)) - return resized_inputs - - # anchors in this test are designed so that a subset of anchors are inside - # the image and a subset of anchors are outside. - first_stage_anchor_generator = None - if multi_level: - min_level = 0 - max_level = 1 - anchor_scale = 0.1 - aspect_ratios = [1.0, 2.0, 0.5] - scales_per_octave = 2 - normalize_coordinates = False - (first_stage_anchor_generator - ) = multiscale_grid_anchor_generator.MultiscaleGridAnchorGenerator( - min_level, max_level, anchor_scale, aspect_ratios, scales_per_octave, - normalize_coordinates) - else: - first_stage_anchor_scales = (0.001, 0.005, 0.1) - first_stage_anchor_aspect_ratios = (0.5, 1.0, 2.0) - first_stage_anchor_strides = (1, 1) - first_stage_anchor_generator = grid_anchor_generator.GridAnchorGenerator( - first_stage_anchor_scales, - first_stage_anchor_aspect_ratios, - anchor_stride=first_stage_anchor_strides) - first_stage_target_assigner = target_assigner.create_target_assigner( - 'FasterRCNN', - 'proposal', - use_matmul_gather=use_matmul_gather_in_matcher) - - if use_keras: - if multi_level: - fake_feature_extractor = FakeFasterRCNNKerasMultilevelFeatureExtractor() - else: - fake_feature_extractor = FakeFasterRCNNKerasFeatureExtractor() - else: - if multi_level: - fake_feature_extractor = FakeFasterRCNNMultiLevelFeatureExtractor() - else: - fake_feature_extractor = FakeFasterRCNNFeatureExtractor() - - first_stage_box_predictor_hyperparams_text_proto = """ - op: CONV - activation: RELU - regularizer { - l2_regularizer { - weight: 0.00004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - } - } - """ - if use_keras: - first_stage_box_predictor_arg_scope_fn = ( - self._build_keras_layer_hyperparams( - first_stage_box_predictor_hyperparams_text_proto)) - else: - first_stage_box_predictor_arg_scope_fn = ( - self._build_arg_scope_with_hyperparams( - first_stage_box_predictor_hyperparams_text_proto, is_training)) - - first_stage_box_predictor_kernel_size = 3 - first_stage_atrous_rate = 1 - first_stage_box_predictor_depth = 512 - first_stage_minibatch_size = 3 - first_stage_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=0.5, is_static=use_static_shapes) - - first_stage_nms_score_threshold = -1.0 - first_stage_nms_iou_threshold = 1.0 - first_stage_max_proposals = first_stage_max_proposals - first_stage_non_max_suppression_fn = functools.partial( - post_processing.batch_multiclass_non_max_suppression, - score_thresh=first_stage_nms_score_threshold, - iou_thresh=first_stage_nms_iou_threshold, - max_size_per_class=first_stage_max_proposals, - max_total_size=first_stage_max_proposals, - use_static_shapes=use_static_shapes) - - first_stage_localization_loss_weight = 1.0 - first_stage_objectness_loss_weight = 1.0 - - post_processing_config = post_processing_pb2.PostProcessing() - post_processing_text_proto = """ - score_converter: IDENTITY - batch_non_max_suppression { - score_threshold: -20.0 - iou_threshold: 1.0 - max_detections_per_class: 5 - max_total_detections: 5 - use_static_shapes: """ +'{}'.format(use_static_shapes) + """ - } - """ - if calibration_mapping_value: - calibration_text_proto = """ - calibration_config { - function_approximation { - x_y_pairs { - x_y_pair { - x: 0.0 - y: %f - } - x_y_pair { - x: 1.0 - y: %f - }}}}""" % (calibration_mapping_value, calibration_mapping_value) - post_processing_text_proto = (post_processing_text_proto - + ' ' + calibration_text_proto) - text_format.Merge(post_processing_text_proto, post_processing_config) - second_stage_non_max_suppression_fn, second_stage_score_conversion_fn = ( - post_processing_builder.build(post_processing_config)) - - second_stage_target_assigner = target_assigner.create_target_assigner( - 'FasterRCNN', 'detection', - use_matmul_gather=use_matmul_gather_in_matcher) - second_stage_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=1.0, is_static=use_static_shapes) - - second_stage_localization_loss_weight = 1.0 - second_stage_classification_loss_weight = 1.0 - if softmax_second_stage_classification_loss: - second_stage_classification_loss = ( - losses.WeightedSoftmaxClassificationLoss()) - else: - second_stage_classification_loss = ( - losses.WeightedSigmoidClassificationLoss()) - - hard_example_miner = None - if hard_mining: - hard_example_miner = losses.HardExampleMiner( - num_hard_examples=1, - iou_threshold=0.99, - loss_type='both', - cls_loss_weight=second_stage_classification_loss_weight, - loc_loss_weight=second_stage_localization_loss_weight, - max_negatives_per_positive=None) - - crop_and_resize_fn = ( - spatial_ops.multilevel_matmul_crop_and_resize - if use_matmul_crop_and_resize - else spatial_ops.multilevel_native_crop_and_resize) - common_kwargs = { - 'is_training': - is_training, - 'num_classes': - num_classes, - 'image_resizer_fn': - image_resizer_fn, - 'feature_extractor': - fake_feature_extractor, - 'number_of_stages': - number_of_stages, - 'first_stage_anchor_generator': - first_stage_anchor_generator, - 'first_stage_target_assigner': - first_stage_target_assigner, - 'first_stage_atrous_rate': - first_stage_atrous_rate, - 'first_stage_box_predictor_arg_scope_fn': - first_stage_box_predictor_arg_scope_fn, - 'first_stage_box_predictor_kernel_size': - first_stage_box_predictor_kernel_size, - 'first_stage_box_predictor_depth': - first_stage_box_predictor_depth, - 'first_stage_minibatch_size': - first_stage_minibatch_size, - 'first_stage_sampler': - first_stage_sampler, - 'first_stage_non_max_suppression_fn': - first_stage_non_max_suppression_fn, - 'first_stage_max_proposals': - first_stage_max_proposals, - 'first_stage_localization_loss_weight': - first_stage_localization_loss_weight, - 'first_stage_objectness_loss_weight': - first_stage_objectness_loss_weight, - 'second_stage_target_assigner': - second_stage_target_assigner, - 'second_stage_batch_size': - second_stage_batch_size, - 'second_stage_sampler': - second_stage_sampler, - 'second_stage_non_max_suppression_fn': - second_stage_non_max_suppression_fn, - 'second_stage_score_conversion_fn': - second_stage_score_conversion_fn, - 'second_stage_localization_loss_weight': - second_stage_localization_loss_weight, - 'second_stage_classification_loss_weight': - second_stage_classification_loss_weight, - 'second_stage_classification_loss': - second_stage_classification_loss, - 'hard_example_miner': - hard_example_miner, - 'crop_and_resize_fn': - crop_and_resize_fn, - 'clip_anchors_to_image': - clip_anchors_to_image, - 'use_static_shapes': - use_static_shapes, - 'resize_masks': - True, - 'return_raw_detections_during_predict': - return_raw_detections_during_predict, - 'output_final_box_features': - output_final_box_features - } - - return self._get_model( - self._get_second_stage_box_predictor( - num_classes=num_classes, - is_training=is_training, - use_keras=use_keras, - predict_masks=predict_masks, - masks_are_class_agnostic=masks_are_class_agnostic, - share_box_across_classes=share_box_across_classes), **common_kwargs) - - @parameterized.parameters( - {'use_static_shapes': False}, - {'use_static_shapes': True}, - ) - def test_predict_gives_correct_shapes_in_inference_mode_first_stage_only( - self, use_static_shapes=False): - batch_size = 2 - height = 10 - width = 12 - input_image_shape = (batch_size, height, width, 3) - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=1, - second_stage_batch_size=2, - clip_anchors_to_image=use_static_shapes, - use_static_shapes=use_static_shapes) - def graph_fn(images): - """Function to construct tf graph for the test.""" - - preprocessed_inputs, true_image_shapes = model.preprocess(images) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - return (prediction_dict['rpn_box_predictor_features'][0], - prediction_dict['rpn_features_to_crop'][0], - prediction_dict['image_shape'], - prediction_dict['rpn_box_encodings'], - prediction_dict['rpn_objectness_predictions_with_background'], - prediction_dict['anchors']) - - images = np.zeros(input_image_shape, dtype=np.float32) - - # In inference mode, anchors are clipped to the image window, but not - # pruned. Since MockFasterRCNN.extract_proposal_features returns a - # tensor with the same shape as its input, the expected number of anchors - # is height * width * the number of anchors per location (i.e. 3x3). - expected_num_anchors = height * width * 3 * 3 - expected_output_shapes = { - 'rpn_box_predictor_features': (batch_size, height, width, 512), - 'rpn_features_to_crop': (batch_size, height, width, 3), - 'rpn_box_encodings': (batch_size, expected_num_anchors, 4), - 'rpn_objectness_predictions_with_background': - (batch_size, expected_num_anchors, 2), - 'anchors': (expected_num_anchors, 4) - } - - if use_static_shapes: - results = self.execute(graph_fn, [images], graph=g) - else: - results = self.execute_cpu(graph_fn, [images], graph=g) - - self.assertAllEqual(results[0].shape, - expected_output_shapes['rpn_box_predictor_features']) - self.assertAllEqual(results[1].shape, - expected_output_shapes['rpn_features_to_crop']) - self.assertAllEqual(results[2], - input_image_shape) - self.assertAllEqual(results[3].shape, - expected_output_shapes['rpn_box_encodings']) - self.assertAllEqual( - results[4].shape, - expected_output_shapes['rpn_objectness_predictions_with_background']) - self.assertAllEqual(results[5].shape, - expected_output_shapes['anchors']) - - # Check that anchors are clipped to window. - anchors = results[5] - self.assertTrue(np.all(np.greater_equal(anchors, 0))) - self.assertTrue(np.all(np.less_equal(anchors[:, 0], height))) - self.assertTrue(np.all(np.less_equal(anchors[:, 1], width))) - self.assertTrue(np.all(np.less_equal(anchors[:, 2], height))) - self.assertTrue(np.all(np.less_equal(anchors[:, 3], width))) - - @parameterized.parameters( - {'use_static_shapes': False}, - {'use_static_shapes': True}, - ) - def test_predict_shape_in_inference_mode_first_stage_only_multi_level( - self, use_static_shapes): - batch_size = 2 - height = 50 - width = 52 - input_image_shape = (batch_size, height, width, 3) - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=1, - second_stage_batch_size=2, - clip_anchors_to_image=use_static_shapes, - use_static_shapes=use_static_shapes, - multi_level=True) - def graph_fn(images): - """Function to construct tf graph for the test.""" - - preprocessed_inputs, true_image_shapes = model.preprocess(images) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - return (prediction_dict['rpn_box_predictor_features'][0], - prediction_dict['rpn_box_predictor_features'][1], - prediction_dict['rpn_features_to_crop'][0], - prediction_dict['rpn_features_to_crop'][1], - prediction_dict['image_shape'], - prediction_dict['rpn_box_encodings'], - prediction_dict['rpn_objectness_predictions_with_background'], - prediction_dict['anchors']) - - images = np.zeros(input_image_shape, dtype=np.float32) - - # In inference mode, anchors are clipped to the image window, but not - # pruned. Since MockFasterRCNN.extract_proposal_features returns a - # tensor with the same shape as its input, the expected number of anchors - # is height * width * the number of anchors per location (i.e. 3x3). - expected_num_anchors = ((height-2) * (width-2) + (height-4) * (width-4)) * 6 - expected_output_shapes = { - 'rpn_box_predictor_features_0': (batch_size, height-2, width-2, 512), - 'rpn_box_predictor_features_1': (batch_size, height-4, width-4, 512), - 'rpn_features_to_crop_0': (batch_size, height-2, width-2, 3), - 'rpn_features_to_crop_1': (batch_size, height-4, width-4, 3), - 'rpn_box_encodings': (batch_size, expected_num_anchors, 4), - 'rpn_objectness_predictions_with_background': - (batch_size, expected_num_anchors, 2), - } - - if use_static_shapes: - expected_output_shapes['anchors'] = (expected_num_anchors, 4) - else: - expected_output_shapes['anchors'] = (18300, 4) - - if use_static_shapes: - results = self.execute(graph_fn, [images], graph=g) - else: - results = self.execute_cpu(graph_fn, [images], graph=g) - - self.assertAllEqual(results[0].shape, - expected_output_shapes['rpn_box_predictor_features_0']) - self.assertAllEqual(results[1].shape, - expected_output_shapes['rpn_box_predictor_features_1']) - self.assertAllEqual(results[2].shape, - expected_output_shapes['rpn_features_to_crop_0']) - self.assertAllEqual(results[3].shape, - expected_output_shapes['rpn_features_to_crop_1']) - self.assertAllEqual(results[4], - input_image_shape) - self.assertAllEqual(results[5].shape, - expected_output_shapes['rpn_box_encodings']) - self.assertAllEqual( - results[6].shape, - expected_output_shapes['rpn_objectness_predictions_with_background']) - self.assertAllEqual(results[7].shape, - expected_output_shapes['anchors']) - - # Check that anchors are clipped to window. - anchors = results[5] - self.assertTrue(np.all(np.greater_equal(anchors, 0))) - self.assertTrue(np.all(np.less_equal(anchors[:, 0], height))) - self.assertTrue(np.all(np.less_equal(anchors[:, 1], width))) - self.assertTrue(np.all(np.less_equal(anchors[:, 2], height))) - self.assertTrue(np.all(np.less_equal(anchors[:, 3], width))) - - def test_regularization_losses(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, number_of_stages=1, second_stage_batch_size=2) - def graph_fn(): - batch_size = 2 - height = 10 - width = 12 - input_image_shape = (batch_size, height, width, 3) - image, true_image_shapes = model.preprocess(tf.zeros(input_image_shape)) - model.predict(image, true_image_shapes) - - reg_losses = tf.math.add_n(model.regularization_losses()) - return reg_losses - reg_losses = self.execute(graph_fn, [], graph=g) - self.assertGreaterEqual(reg_losses, 0) - - def test_predict_gives_valid_anchors_in_training_mode_first_stage_only(self): - expected_output_keys = set([ - 'rpn_box_predictor_features', 'rpn_features_to_crop', 'image_shape', - 'rpn_box_encodings', 'rpn_objectness_predictions_with_background', - 'anchors', 'feature_maps']) - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, number_of_stages=1, second_stage_batch_size=2,) - - batch_size = 2 - height = 10 - width = 12 - input_image_shape = (batch_size, height, width, 3) - def graph_fn(): - image, true_image_shapes = model.preprocess(tf.zeros(input_image_shape)) - prediction_dict = model.predict(image, true_image_shapes) - self.assertEqual(set(prediction_dict.keys()), expected_output_keys) - return (prediction_dict['image_shape'], prediction_dict['anchors'], - prediction_dict['rpn_box_encodings'], - prediction_dict['rpn_objectness_predictions_with_background']) - - (image_shape, anchors, rpn_box_encodings, - rpn_objectness_predictions_with_background) = self.execute(graph_fn, [], - graph=g) - # At training time, anchors that exceed image bounds are pruned. Thus - # the `expected_num_anchors` in the above inference mode test is now - # a strict upper bound on the number of anchors. - num_anchors_strict_upper_bound = height * width * 3 * 3 - self.assertAllEqual(image_shape, input_image_shape) - self.assertTrue(len(anchors.shape) == 2 and anchors.shape[1] == 4) - num_anchors_out = anchors.shape[0] - self.assertLess(num_anchors_out, num_anchors_strict_upper_bound) - - self.assertTrue(np.all(np.greater_equal(anchors, 0))) - self.assertTrue(np.all(np.less_equal(anchors[:, 0], height))) - self.assertTrue(np.all(np.less_equal(anchors[:, 1], width))) - self.assertTrue(np.all(np.less_equal(anchors[:, 2], height))) - self.assertTrue(np.all(np.less_equal(anchors[:, 3], width))) - - self.assertAllEqual(rpn_box_encodings.shape, - (batch_size, num_anchors_out, 4)) - self.assertAllEqual( - rpn_objectness_predictions_with_background.shape, - (batch_size, num_anchors_out, 2)) - - @parameterized.parameters( - {'use_static_shapes': False}, - {'use_static_shapes': True}, - ) - def test_predict_correct_shapes_in_inference_mode_two_stages( - self, use_static_shapes): - - def compare_results(results, expected_output_shapes): - """Checks if the shape of the predictions are as expected.""" - self.assertAllEqual(results[0][0].shape, - expected_output_shapes['rpn_box_predictor_features']) - self.assertAllEqual(results[1][0].shape, - expected_output_shapes['rpn_features_to_crop']) - self.assertAllEqual(results[2].shape, - expected_output_shapes['image_shape']) - self.assertAllEqual(results[3].shape, - expected_output_shapes['rpn_box_encodings']) - self.assertAllEqual( - results[4].shape, - expected_output_shapes['rpn_objectness_predictions_with_background']) - self.assertAllEqual(results[5].shape, - expected_output_shapes['anchors']) - self.assertAllEqual(results[6].shape, - expected_output_shapes['refined_box_encodings']) - self.assertAllEqual( - results[7].shape, - expected_output_shapes['class_predictions_with_background']) - self.assertAllEqual(results[8].shape, - expected_output_shapes['num_proposals']) - self.assertAllEqual(results[9].shape, - expected_output_shapes['proposal_boxes']) - self.assertAllEqual(results[10].shape, - expected_output_shapes['proposal_boxes_normalized']) - self.assertAllEqual(results[11].shape, - expected_output_shapes['box_classifier_features']) - self.assertAllEqual(results[12].shape, - expected_output_shapes['final_anchors']) - batch_size = 2 - image_size = 10 - max_num_proposals = 8 - initial_crop_size = 3 - maxpool_stride = 1 - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, - second_stage_batch_size=2, - predict_masks=False, - use_matmul_crop_and_resize=use_static_shapes, - clip_anchors_to_image=use_static_shapes, - use_static_shapes=use_static_shapes) - def graph_fn(): - """A function with TF compute.""" - if use_static_shapes: - images = tf.random_uniform((batch_size, image_size, image_size, 3)) - else: - images = tf.random_uniform((tf.random_uniform([], - minval=batch_size, - maxval=batch_size + 1, - dtype=tf.int32), - tf.random_uniform([], - minval=image_size, - maxval=image_size + 1, - dtype=tf.int32), - tf.random_uniform([], - minval=image_size, - maxval=image_size + 1, - dtype=tf.int32), 3)) - preprocessed_inputs, true_image_shapes = model.preprocess(images) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - return (prediction_dict['rpn_box_predictor_features'], - prediction_dict['rpn_features_to_crop'], - prediction_dict['image_shape'], - prediction_dict['rpn_box_encodings'], - prediction_dict['rpn_objectness_predictions_with_background'], - prediction_dict['anchors'], - prediction_dict['refined_box_encodings'], - prediction_dict['class_predictions_with_background'], - prediction_dict['num_proposals'], - prediction_dict['proposal_boxes'], - prediction_dict['proposal_boxes_normalized'], - prediction_dict['box_classifier_features'], - prediction_dict['final_anchors']) - expected_num_anchors = image_size * image_size * 3 * 3 - expected_shapes = { - 'rpn_box_predictor_features': - (2, image_size, image_size, 512), - 'rpn_features_to_crop': (2, image_size, image_size, 3), - 'image_shape': (4,), - 'rpn_box_encodings': (2, expected_num_anchors, 4), - 'rpn_objectness_predictions_with_background': - (2, expected_num_anchors, 2), - 'anchors': (expected_num_anchors, 4), - 'refined_box_encodings': (2 * max_num_proposals, 2, 4), - 'class_predictions_with_background': (2 * max_num_proposals, 2 + 1), - 'num_proposals': (2,), - 'proposal_boxes': (2, max_num_proposals, 4), - 'proposal_boxes_normalized': (2, max_num_proposals, 4), - 'box_classifier_features': - self._get_box_classifier_features_shape(image_size, - batch_size, - max_num_proposals, - initial_crop_size, - maxpool_stride, - 3), - 'feature_maps': [(2, image_size, image_size, 512)], - 'final_anchors': (2, max_num_proposals, 4) - } - - if use_static_shapes: - results = self.execute(graph_fn, [], graph=g) - else: - results = self.execute_cpu(graph_fn, [], graph=g) - compare_results(results, expected_shapes) - - @parameterized.parameters( - {'use_static_shapes': False}, - {'use_static_shapes': True}, - ) - def test_predict_gives_correct_shapes_in_train_mode_both_stages( - self, - use_static_shapes=False): - batch_size = 2 - image_size = 10 - max_num_proposals = 7 - initial_crop_size = 3 - maxpool_stride = 1 - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, - number_of_stages=2, - second_stage_batch_size=7, - predict_masks=False, - use_matmul_crop_and_resize=use_static_shapes, - clip_anchors_to_image=use_static_shapes, - use_static_shapes=use_static_shapes) - - def graph_fn(images, gt_boxes, gt_classes, gt_weights): - """Function to construct tf graph for the test.""" - preprocessed_inputs, true_image_shapes = model.preprocess(images) - model.provide_groundtruth( - groundtruth_boxes_list=tf.unstack(gt_boxes), - groundtruth_classes_list=tf.unstack(gt_classes), - groundtruth_weights_list=tf.unstack(gt_weights)) - result_tensor_dict = model.predict(preprocessed_inputs, true_image_shapes) - return (result_tensor_dict['refined_box_encodings'], - result_tensor_dict['class_predictions_with_background'], - result_tensor_dict['proposal_boxes'], - result_tensor_dict['proposal_boxes_normalized'], - result_tensor_dict['anchors'], - result_tensor_dict['rpn_box_encodings'], - result_tensor_dict['rpn_objectness_predictions_with_background'], - result_tensor_dict['rpn_features_to_crop'][0], - result_tensor_dict['rpn_box_predictor_features'][0], - result_tensor_dict['final_anchors'], - ) - - image_shape = (batch_size, image_size, image_size, 3) - images = np.zeros(image_shape, dtype=np.float32) - gt_boxes = np.stack([ - np.array([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=np.float32), - np.array([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=np.float32) - ]) - gt_classes = np.stack([ - np.array([[1, 0], [0, 1]], dtype=np.float32), - np.array([[1, 0], [1, 0]], dtype=np.float32) - ]) - gt_weights = np.stack([ - np.array([1, 1], dtype=np.float32), - np.array([1, 1], dtype=np.float32) - ]) - if use_static_shapes: - results = self.execute(graph_fn, - [images, gt_boxes, gt_classes, gt_weights], - graph=g) - else: - results = self.execute_cpu(graph_fn, - [images, gt_boxes, gt_classes, gt_weights], - graph=g) - - expected_shapes = { - 'rpn_box_predictor_features': (2, image_size, image_size, 512), - 'rpn_features_to_crop': (2, image_size, image_size, 3), - 'refined_box_encodings': (2 * max_num_proposals, 2, 4), - 'class_predictions_with_background': (2 * max_num_proposals, 2 + 1), - 'proposal_boxes': (2, max_num_proposals, 4), - 'rpn_box_encodings': (2, image_size * image_size * 9, 4), - 'proposal_boxes_normalized': (2, max_num_proposals, 4), - 'box_classifier_features': - self._get_box_classifier_features_shape( - image_size, batch_size, max_num_proposals, initial_crop_size, - maxpool_stride, 3), - 'rpn_objectness_predictions_with_background': - (2, image_size * image_size * 9, 2), - 'final_anchors': (2, max_num_proposals, 4) - } - # TODO(rathodv): Possibly change utils/test_case.py to accept dictionaries - # and return dicionaries so don't have to rely on the order of tensors. - self.assertAllEqual(results[0].shape, - expected_shapes['refined_box_encodings']) - self.assertAllEqual(results[1].shape, - expected_shapes['class_predictions_with_background']) - self.assertAllEqual(results[2].shape, expected_shapes['proposal_boxes']) - self.assertAllEqual(results[3].shape, - expected_shapes['proposal_boxes_normalized']) - anchors_shape = results[4].shape - self.assertAllEqual(results[5].shape, - [batch_size, anchors_shape[0], 4]) - self.assertAllEqual(results[6].shape, - [batch_size, anchors_shape[0], 2]) - self.assertAllEqual(results[7].shape, - expected_shapes['rpn_features_to_crop']) - self.assertAllEqual(results[8].shape, - expected_shapes['rpn_box_predictor_features']) - self.assertAllEqual(results[9].shape, - expected_shapes['final_anchors']) - - @parameterized.parameters( - {'use_static_shapes': False, 'pad_to_max_dimension': None}, - {'use_static_shapes': True, 'pad_to_max_dimension': None}, - {'use_static_shapes': False, 'pad_to_max_dimension': 56,}, - {'use_static_shapes': True, 'pad_to_max_dimension': 56}, - ) - def test_postprocess_first_stage_only_inference_mode( - self, use_static_shapes=False, - pad_to_max_dimension=None): - batch_size = 2 - first_stage_max_proposals = 4 if use_static_shapes else 8 - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=1, second_stage_batch_size=6, - use_matmul_crop_and_resize=use_static_shapes, - clip_anchors_to_image=use_static_shapes, - use_static_shapes=use_static_shapes, - use_matmul_gather_in_matcher=use_static_shapes, - first_stage_max_proposals=first_stage_max_proposals, - pad_to_max_dimension=pad_to_max_dimension) - - def graph_fn(images, - rpn_box_encodings, - rpn_objectness_predictions_with_background, - rpn_features_to_crop, - anchors): - """Function to construct tf graph for the test.""" - preprocessed_images, true_image_shapes = model.preprocess(images) - proposals = model.postprocess({ - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'rpn_features_to_crop': rpn_features_to_crop, - 'image_shape': tf.shape(preprocessed_images), - 'anchors': anchors}, true_image_shapes) - return (proposals['num_detections'], proposals['detection_boxes'], - proposals['detection_scores'], proposals['raw_detection_boxes'], - proposals['raw_detection_scores']) - - anchors = np.array( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=np.float32) - rpn_box_encodings = np.zeros( - (batch_size, anchors.shape[0], BOX_CODE_SIZE), dtype=np.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = np.array([ - [[-10, 13], - [10, -10], - [10, -11], - [-10, 12]], - [[10, -10], - [-10, 13], - [-10, 12], - [10, -11]]], dtype=np.float32) - rpn_features_to_crop = np.ones((batch_size, 8, 8, 10), dtype=np.float32) - image_shape = (batch_size, 32, 32, 3) - images = np.zeros(image_shape, dtype=np.float32) - - if use_static_shapes: - results = self.execute(graph_fn, - [images, rpn_box_encodings, - rpn_objectness_predictions_with_background, - rpn_features_to_crop, anchors], graph=g) - else: - results = self.execute_cpu(graph_fn, - [images, rpn_box_encodings, - rpn_objectness_predictions_with_background, - rpn_features_to_crop, anchors], graph=g) - - expected_proposal_boxes = [ - [[0, 0, .5, .5], [.5, .5, 1, 1], [0, .5, .5, 1], [.5, 0, 1.0, .5]] - + 4 * [4 * [0]], - [[0, .5, .5, 1], [.5, 0, 1.0, .5], [0, 0, .5, .5], [.5, .5, 1, 1]] - + 4 * [4 * [0]]] - expected_proposal_scores = [[1, 1, 0, 0, 0, 0, 0, 0], - [1, 1, 0, 0, 0, 0, 0, 0]] - expected_num_proposals = [4, 4] - expected_raw_proposal_boxes = [[[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.], - [0.5, 0., 1., 0.5], [0.5, 0.5, 1., 1.]], - [[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.], - [0.5, 0., 1., 0.5], [0.5, 0.5, 1., 1.]]] - expected_raw_scores = [[[0., 1.], [1., 0.], [1., 0.], [0., 1.]], - [[1., 0.], [0., 1.], [0., 1.], [1., 0.]]] - - if pad_to_max_dimension is not None: - expected_raw_proposal_boxes = (np.array(expected_raw_proposal_boxes) * - 32 / pad_to_max_dimension) - expected_proposal_boxes = (np.array(expected_proposal_boxes) * - 32 / pad_to_max_dimension) - - self.assertAllClose(results[0], expected_num_proposals) - for indx, num_proposals in enumerate(expected_num_proposals): - self.assertAllClose(results[1][indx][0:num_proposals], - expected_proposal_boxes[indx][0:num_proposals]) - self.assertAllClose(results[2][indx][0:num_proposals], - expected_proposal_scores[indx][0:num_proposals]) - self.assertAllClose(results[3], expected_raw_proposal_boxes) - self.assertAllClose(results[4], expected_raw_scores) - - def _test_postprocess_first_stage_only_train_mode(self, - pad_to_max_dimension=None): - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, - number_of_stages=1, second_stage_batch_size=2, - pad_to_max_dimension=pad_to_max_dimension) - batch_size = 2 - - def graph_fn(): - """A function with TF compute.""" - anchors = tf.constant( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=tf.float32) - rpn_box_encodings = tf.zeros( - [batch_size, anchors.get_shape().as_list()[0], - BOX_CODE_SIZE], dtype=tf.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = tf.constant([ - [[-10, 13], - [-10, 12], - [-10, 11], - [-10, 10]], - [[-10, 13], - [-10, 12], - [-10, 11], - [-10, 10]]], dtype=tf.float32) - rpn_features_to_crop = tf.ones((batch_size, 8, 8, 10), dtype=tf.float32) - image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) - groundtruth_boxes_list = [ - tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), - tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32)] - groundtruth_classes_list = [tf.constant([[1, 0], [0, 1]], - dtype=tf.float32), - tf.constant([[1, 0], [1, 0]], - dtype=tf.float32)] - groundtruth_weights_list = [ - tf.constant([1, 1], dtype=tf.float32), - tf.constant([1, 1], dtype=tf.float32) - ] - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth( - groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_weights_list=groundtruth_weights_list) - proposals = model.postprocess({ - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'rpn_features_to_crop': rpn_features_to_crop, - 'anchors': anchors, - 'image_shape': image_shape}, true_image_shapes) - return (proposals['detection_boxes'], proposals['detection_scores'], - proposals['num_detections'], - proposals['detection_multiclass_scores'], - proposals['raw_detection_boxes'], - proposals['raw_detection_scores']) - - expected_proposal_boxes = [ - [[0, 0, .5, .5], [.5, .5, 1, 1]], [[0, .5, .5, 1], [.5, 0, 1, .5]]] - expected_proposal_scores = [[1, 1], - [1, 1]] - expected_proposal_multiclass_scores = [[[0., 1.], [0., 1.]], - [[0., 1.], [0., 1.]]] - expected_raw_proposal_boxes = [[[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.], - [0.5, 0., 1., 0.5], [0.5, 0.5, 1., 1.]], - [[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.], - [0.5, 0., 1., 0.5], [0.5, 0.5, 1., 1.]]] - expected_raw_scores = [[[0., 1.], [0., 1.], [0., 1.], [0., 1.]], - [[0., 1.], [0., 1.], [0., 1.], [0., 1.]]] - - (proposal_boxes, proposal_scores, batch_num_detections, - batch_multiclass_scores, raw_detection_boxes, - raw_detection_scores) = self.execute_cpu(graph_fn, [], graph=g) - for image_idx in range(batch_size): - num_detections = int(batch_num_detections[image_idx]) - boxes = proposal_boxes[image_idx][:num_detections, :].tolist() - scores = proposal_scores[image_idx][:num_detections].tolist() - multiclass_scores = batch_multiclass_scores[ - image_idx][:num_detections, :].tolist() - expected_boxes = expected_proposal_boxes[image_idx] - expected_scores = expected_proposal_scores[image_idx] - expected_multiclass_scores = expected_proposal_multiclass_scores[ - image_idx] - self.assertTrue( - test_utils.first_rows_close_as_set(boxes, expected_boxes)) - self.assertTrue( - test_utils.first_rows_close_as_set(scores, expected_scores)) - self.assertTrue( - test_utils.first_rows_close_as_set(multiclass_scores, - expected_multiclass_scores)) - - self.assertAllClose(raw_detection_boxes, expected_raw_proposal_boxes) - self.assertAllClose(raw_detection_scores, expected_raw_scores) - - @parameterized.parameters( - {'pad_to_max_dimension': 56}, - {'pad_to_max_dimension': None} - ) - def test_postprocess_first_stage_only_train_mode_padded_image( - self, pad_to_max_dimension): - self._test_postprocess_first_stage_only_train_mode(pad_to_max_dimension) - - @parameterized.parameters( - {'use_static_shapes': False, 'pad_to_max_dimension': None}, - {'use_static_shapes': True, 'pad_to_max_dimension': None}, - {'use_static_shapes': False, 'pad_to_max_dimension': 56}, - {'use_static_shapes': True, 'pad_to_max_dimension': 56}, - ) - def test_postprocess_second_stage_only_inference_mode( - self, use_static_shapes=False, - pad_to_max_dimension=None): - batch_size = 2 - num_classes = 2 - image_shape = np.array((2, 36, 48, 3), dtype=np.int32) - first_stage_max_proposals = 8 - total_num_padded_proposals = batch_size * first_stage_max_proposals - - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=False, - number_of_stages=2, - second_stage_batch_size=6, - use_matmul_crop_and_resize=use_static_shapes, - clip_anchors_to_image=use_static_shapes, - use_static_shapes=use_static_shapes, - use_matmul_gather_in_matcher=use_static_shapes, - pad_to_max_dimension=pad_to_max_dimension) - def graph_fn(images, - refined_box_encodings, - class_predictions_with_background, - num_proposals, - proposal_boxes): - """Function to construct tf graph for the test.""" - _, true_image_shapes = model.preprocess(images) - detections = model.postprocess({ - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'num_proposals': num_proposals, - 'proposal_boxes': proposal_boxes, - }, true_image_shapes) - return (detections['num_detections'], detections['detection_boxes'], - detections['detection_scores'], detections['detection_classes'], - detections['raw_detection_boxes'], - detections['raw_detection_scores'], - detections['detection_multiclass_scores'], - detections['detection_anchor_indices']) - - proposal_boxes = np.array( - [[[1, 1, 2, 3], - [0, 0, 1, 1], - [.5, .5, .6, .6], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]], - [[2, 3, 6, 8], - [1, 2, 5, 3], - 4*[0], 4*[0], 4*[0], 4*[0], 4*[0], 4*[0]]], dtype=np.float32) - - num_proposals = np.array([3, 2], dtype=np.int32) - refined_box_encodings = np.zeros( - [total_num_padded_proposals, num_classes, 4], dtype=np.float32) - class_predictions_with_background = np.ones( - [total_num_padded_proposals, num_classes+1], dtype=np.float32) - images = np.zeros(image_shape, dtype=np.float32) - - if use_static_shapes: - results = self.execute(graph_fn, - [images, refined_box_encodings, - class_predictions_with_background, - num_proposals, proposal_boxes], graph=g) - else: - results = self.execute_cpu(graph_fn, - [images, refined_box_encodings, - class_predictions_with_background, - num_proposals, proposal_boxes], graph=g) - # Note that max_total_detections=5 in the NMS config. - expected_num_detections = [5, 4] - expected_detection_classes = [[0, 0, 0, 1, 1], [0, 0, 1, 1, 0]] - expected_detection_scores = [[1, 1, 1, 1, 1], [1, 1, 1, 1, 0]] - expected_multiclass_scores = [[[1, 1, 1], - [1, 1, 1], - [1, 1, 1], - [1, 1, 1], - [1, 1, 1]], - [[1, 1, 1], - [1, 1, 1], - [1, 1, 1], - [1, 1, 1], - [0, 0, 0]]] - # Note that a single anchor can be used for multiple detections (predictions - # are made independently per class). - expected_anchor_indices = [[0, 1, 2, 0, 1], - [0, 1, 0, 1]] - - h = float(image_shape[1]) - w = float(image_shape[2]) - expected_raw_detection_boxes = np.array( - [[[1 / h, 1 / w, 2 / h, 3 / w], [0, 0, 1 / h, 1 / w], - [.5 / h, .5 / w, .6 / h, .6 / w], 4 * [0], 4 * [0], 4 * [0], 4 * [0], - 4 * [0]], - [[2 / h, 3 / w, 6 / h, 8 / w], [1 / h, 2 / w, 5 / h, 3 / w], 4 * [0], - 4 * [0], 4 * [0], 4 * [0], 4 * [0], 4 * [0]]], - dtype=np.float32) - - self.assertAllClose(results[0], expected_num_detections) - - for indx, num_proposals in enumerate(expected_num_detections): - self.assertAllClose(results[2][indx][0:num_proposals], - expected_detection_scores[indx][0:num_proposals]) - self.assertAllClose(results[3][indx][0:num_proposals], - expected_detection_classes[indx][0:num_proposals]) - self.assertAllClose(results[6][indx][0:num_proposals], - expected_multiclass_scores[indx][0:num_proposals]) - self.assertAllClose(results[7][indx][0:num_proposals], - expected_anchor_indices[indx][0:num_proposals]) - - self.assertAllClose(results[4], expected_raw_detection_boxes) - self.assertAllClose(results[5], - class_predictions_with_background.reshape([-1, 8, 3])) - if not use_static_shapes: - self.assertAllEqual(results[1].shape, [2, 5, 4]) - - def test_preprocess_preserves_dynamic_input_shapes(self): - width = tf.random.uniform([], minval=5, maxval=10, dtype=tf.int32) - batch = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - shape = tf.stack([batch, 5, width, 3]) - image = tf.random.uniform(shape) - model = self._build_model( - is_training=False, number_of_stages=2, second_stage_batch_size=6) - preprocessed_inputs, _ = model.preprocess(image) - self.assertTrue( - preprocessed_inputs.shape.is_compatible_with([None, 5, None, 3])) - - def test_preprocess_preserves_static_input_shapes(self): - shape = tf.stack([2, 5, 5, 3]) - image = tf.random.uniform(shape) - model = self._build_model( - is_training=False, number_of_stages=2, second_stage_batch_size=6) - preprocessed_inputs, _ = model.preprocess(image) - self.assertTrue( - preprocessed_inputs.shape.is_compatible_with([2, 5, 5, 3])) - - # TODO(rathodv): Split test into two - with and without masks. - def test_loss_first_stage_only_mode(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, - number_of_stages=1, second_stage_batch_size=6) - batch_size = 2 - def graph_fn(): - """A function with TF compute.""" - anchors = tf.constant( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=tf.float32) - - rpn_box_encodings = tf.zeros( - [batch_size, - anchors.get_shape().as_list()[0], - BOX_CODE_SIZE], dtype=tf.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = tf.constant([ - [[-10, 13], - [10, -10], - [10, -11], - [-10, 12]], - [[10, -10], - [-10, 13], - [-10, 12], - [10, -11]]], dtype=tf.float32) - image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) - - groundtruth_boxes_list = [ - tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), - tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32)] - groundtruth_classes_list = [tf.constant([[1, 0], [0, 1]], - dtype=tf.float32), - tf.constant([[1, 0], [1, 0]], - dtype=tf.float32)] - - prediction_dict = { - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'image_shape': image_shape, - 'anchors': anchors - } - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list) - loss_dict = model.loss(prediction_dict, true_image_shapes) - self.assertNotIn('Loss/BoxClassifierLoss/localization_loss', - loss_dict) - self.assertNotIn('Loss/BoxClassifierLoss/classification_loss', - loss_dict) - return (loss_dict['Loss/RPNLoss/localization_loss'], - loss_dict['Loss/RPNLoss/objectness_loss']) - loc_loss, obj_loss = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllClose(loc_loss, 0) - self.assertAllClose(obj_loss, 0) - - # TODO(rathodv): Split test into two - with and without masks. - def test_loss_full(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, - number_of_stages=2, second_stage_batch_size=6) - batch_size = 3 - def graph_fn(): - """A function with TF compute.""" - anchors = tf.constant( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=tf.float32) - rpn_box_encodings = tf.zeros( - [batch_size, - anchors.get_shape().as_list()[0], - BOX_CODE_SIZE], dtype=tf.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = tf.constant( - [[[-10, 13], [10, -10], [10, -11], [-10, 12]], - [[10, -10], [-10, 13], [-10, 12], [10, -11]], - [[10, -10], [-10, 13], [-10, 12], [10, -11]]], - dtype=tf.float32) - image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) - - num_proposals = tf.constant([6, 6, 6], dtype=tf.int32) - proposal_boxes = tf.constant( - 3 * [[[0, 0, 16, 16], [0, 16, 16, 32], [16, 0, 32, 16], - [16, 16, 32, 32], [0, 0, 16, 16], [0, 16, 16, 32]]], - dtype=tf.float32) - refined_box_encodings = tf.zeros( - (batch_size * model.max_num_proposals, - model.num_classes, - BOX_CODE_SIZE), dtype=tf.float32) - class_predictions_with_background = tf.constant( - [ - [-10, 10, -10], # first image - [10, -10, -10], - [10, -10, -10], - [-10, -10, 10], - [-10, 10, -10], - [10, -10, -10], - [10, -10, -10], # second image - [-10, 10, -10], - [-10, 10, -10], - [10, -10, -10], - [10, -10, -10], - [-10, 10, -10], - [10, -10, -10], # third image - [-10, 10, -10], - [-10, 10, -10], - [10, -10, -10], - [10, -10, -10], - [-10, 10, -10] - ], - dtype=tf.float32) - - mask_predictions_logits = 20 * tf.ones((batch_size * - model.max_num_proposals, - model.num_classes, - 14, 14), - dtype=tf.float32) - - groundtruth_boxes_list = [ - tf.constant([[0, 0, .5, .5], [.5, .5, 1, 1]], dtype=tf.float32), - tf.constant([[0, .5, .5, 1], [.5, 0, 1, .5]], dtype=tf.float32), - tf.constant([[0, .5, .5, 1], [.5, 0, 1, 1]], dtype=tf.float32) - ] - groundtruth_classes_list = [ - tf.constant([[1, 0], [0, 1]], dtype=tf.float32), - tf.constant([[1, 0], [1, 0]], dtype=tf.float32), - tf.constant([[1, 0], [0, 1]], dtype=tf.float32) - ] - - # Set all elements of groundtruth mask to 1.0. In this case all proposal - # crops of the groundtruth masks should return a mask that covers the - # entire proposal. Thus, if mask_predictions_logits element values are all - # greater than 20, the loss should be zero. - groundtruth_masks_list = [ - tf.convert_to_tensor(np.ones((2, 32, 32)), dtype=tf.float32), - tf.convert_to_tensor(np.ones((2, 32, 32)), dtype=tf.float32), - tf.convert_to_tensor(np.ones((2, 32, 32)), dtype=tf.float32) - ] - groundtruth_weights_list = [ - tf.constant([1, 1], dtype=tf.float32), - tf.constant([1, 1], dtype=tf.float32), - tf.constant([1, 0], dtype=tf.float32) - ] - prediction_dict = { - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'image_shape': image_shape, - 'anchors': anchors, - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'proposal_boxes': proposal_boxes, - 'num_proposals': num_proposals, - 'mask_predictions': mask_predictions_logits - } - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth( - groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_masks_list, - groundtruth_weights_list=groundtruth_weights_list) - loss_dict = model.loss(prediction_dict, true_image_shapes) - return (loss_dict['Loss/RPNLoss/localization_loss'], - loss_dict['Loss/RPNLoss/objectness_loss'], - loss_dict['Loss/BoxClassifierLoss/localization_loss'], - loss_dict['Loss/BoxClassifierLoss/classification_loss'], - loss_dict['Loss/BoxClassifierLoss/mask_loss']) - (rpn_loc_loss, rpn_obj_loss, box_loc_loss, box_cls_loss, - box_mask_loss) = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllClose(rpn_loc_loss, 0) - self.assertAllClose(rpn_obj_loss, 0) - self.assertAllClose(box_loc_loss, 0) - self.assertAllClose(box_cls_loss, 0) - self.assertAllClose(box_mask_loss, 0) - - def test_loss_full_zero_padded_proposals(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, number_of_stages=2, second_stage_batch_size=6) - batch_size = 1 - def graph_fn(): - """A function with TF compute.""" - anchors = tf.constant( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=tf.float32) - rpn_box_encodings = tf.zeros( - [batch_size, - anchors.get_shape().as_list()[0], - BOX_CODE_SIZE], dtype=tf.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = tf.constant([ - [[-10, 13], - [10, -10], - [10, -11], - [10, -12]],], dtype=tf.float32) - image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) - - # box_classifier_batch_size is 6, but here we assume that the number of - # actual proposals (not counting zero paddings) is fewer (3). - num_proposals = tf.constant([3], dtype=tf.int32) - proposal_boxes = tf.constant( - [[[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [0, 0, 0, 0], # begin paddings - [0, 0, 0, 0], - [0, 0, 0, 0]]], dtype=tf.float32) - - refined_box_encodings = tf.zeros( - (batch_size * model.max_num_proposals, - model.num_classes, - BOX_CODE_SIZE), dtype=tf.float32) - class_predictions_with_background = tf.constant( - [[-10, 10, -10], - [10, -10, -10], - [10, -10, -10], - [0, 0, 0], # begin paddings - [0, 0, 0], - [0, 0, 0]], dtype=tf.float32) - - mask_predictions_logits = 20 * tf.ones((batch_size * - model.max_num_proposals, - model.num_classes, - 14, 14), - dtype=tf.float32) - - groundtruth_boxes_list = [ - tf.constant([[0, 0, .5, .5]], dtype=tf.float32)] - groundtruth_classes_list = [tf.constant([[1, 0]], dtype=tf.float32)] - - # Set all elements of groundtruth mask to 1.0. In this case all proposal - # crops of the groundtruth masks should return a mask that covers the - # entire proposal. Thus, if mask_predictions_logits element values are all - # greater than 20, the loss should be zero. - groundtruth_masks_list = [tf.convert_to_tensor(np.ones((1, 32, 32)), - dtype=tf.float32)] - - prediction_dict = { - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'image_shape': image_shape, - 'anchors': anchors, - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'proposal_boxes': proposal_boxes, - 'num_proposals': num_proposals, - 'mask_predictions': mask_predictions_logits - } - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_masks_list) - loss_dict = model.loss(prediction_dict, true_image_shapes) - return (loss_dict['Loss/RPNLoss/localization_loss'], - loss_dict['Loss/RPNLoss/objectness_loss'], - loss_dict['Loss/BoxClassifierLoss/localization_loss'], - loss_dict['Loss/BoxClassifierLoss/classification_loss'], - loss_dict['Loss/BoxClassifierLoss/mask_loss']) - (rpn_loc_loss, rpn_obj_loss, box_loc_loss, box_cls_loss, - box_mask_loss) = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllClose(rpn_loc_loss, 0) - self.assertAllClose(rpn_obj_loss, 0) - self.assertAllClose(box_loc_loss, 0) - self.assertAllClose(box_cls_loss, 0) - self.assertAllClose(box_mask_loss, 0) - - def test_loss_full_multiple_label_groundtruth(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, - number_of_stages=2, second_stage_batch_size=6, - softmax_second_stage_classification_loss=False) - batch_size = 1 - def graph_fn(): - """A function with TF compute.""" - anchors = tf.constant( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=tf.float32) - rpn_box_encodings = tf.zeros( - [batch_size, - anchors.get_shape().as_list()[0], - BOX_CODE_SIZE], dtype=tf.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = tf.constant([ - [[-10, 13], - [10, -10], - [10, -11], - [10, -12]],], dtype=tf.float32) - image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) - - # box_classifier_batch_size is 6, but here we assume that the number of - # actual proposals (not counting zero paddings) is fewer (3). - num_proposals = tf.constant([3], dtype=tf.int32) - proposal_boxes = tf.constant( - [[[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [0, 0, 0, 0], # begin paddings - [0, 0, 0, 0], - [0, 0, 0, 0]]], dtype=tf.float32) - - # second_stage_localization_loss should only be computed for predictions - # that match groundtruth. For multiple label groundtruth boxes, the loss - # should only be computed once for the label with the smaller index. - refined_box_encodings = tf.constant( - [[[0, 0, 0, 0], [1, 1, -1, -1]], - [[1, 1, -1, -1], [1, 1, 1, 1]], - [[1, 1, -1, -1], [1, 1, 1, 1]], - [[1, 1, -1, -1], [1, 1, 1, 1]], - [[1, 1, -1, -1], [1, 1, 1, 1]], - [[1, 1, -1, -1], [1, 1, 1, 1]]], dtype=tf.float32) - class_predictions_with_background = tf.constant( - [[-100, 100, 100], - [100, -100, -100], - [100, -100, -100], - [0, 0, 0], # begin paddings - [0, 0, 0], - [0, 0, 0]], dtype=tf.float32) - - mask_predictions_logits = 20 * tf.ones((batch_size * - model.max_num_proposals, - model.num_classes, - 14, 14), - dtype=tf.float32) - - groundtruth_boxes_list = [ - tf.constant([[0, 0, .5, .5]], dtype=tf.float32)] - # Box contains two ground truth labels. - groundtruth_classes_list = [tf.constant([[1, 1]], dtype=tf.float32)] - - # Set all elements of groundtruth mask to 1.0. In this case all proposal - # crops of the groundtruth masks should return a mask that covers the - # entire proposal. Thus, if mask_predictions_logits element values are all - # greater than 20, the loss should be zero. - groundtruth_masks_list = [tf.convert_to_tensor(np.ones((1, 32, 32)), - dtype=tf.float32)] - - prediction_dict = { - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'image_shape': image_shape, - 'anchors': anchors, - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'proposal_boxes': proposal_boxes, - 'num_proposals': num_proposals, - 'mask_predictions': mask_predictions_logits - } - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_masks_list) - loss_dict = model.loss(prediction_dict, true_image_shapes) - return (loss_dict['Loss/RPNLoss/localization_loss'], - loss_dict['Loss/RPNLoss/objectness_loss'], - loss_dict['Loss/BoxClassifierLoss/localization_loss'], - loss_dict['Loss/BoxClassifierLoss/classification_loss'], - loss_dict['Loss/BoxClassifierLoss/mask_loss']) - (rpn_loc_loss, rpn_obj_loss, box_loc_loss, box_cls_loss, - box_mask_loss) = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllClose(rpn_loc_loss, 0) - self.assertAllClose(rpn_obj_loss, 0) - self.assertAllClose(box_loc_loss, 0) - self.assertAllClose(box_cls_loss, 0) - self.assertAllClose(box_mask_loss, 0) - - @parameterized.parameters( - {'use_static_shapes': False, 'shared_boxes': False}, - {'use_static_shapes': False, 'shared_boxes': True}, - {'use_static_shapes': True, 'shared_boxes': False}, - {'use_static_shapes': True, 'shared_boxes': True}, - ) - def test_loss_full_zero_padded_proposals_nonzero_loss_with_two_images( - self, use_static_shapes=False, shared_boxes=False): - batch_size = 2 - first_stage_max_proposals = 8 - second_stage_batch_size = 6 - num_classes = 2 - with test_utils.GraphContextOrNone() as g: - model = self._build_model( - is_training=True, - number_of_stages=2, - second_stage_batch_size=second_stage_batch_size, - first_stage_max_proposals=first_stage_max_proposals, - num_classes=num_classes, - use_matmul_crop_and_resize=use_static_shapes, - clip_anchors_to_image=use_static_shapes, - use_static_shapes=use_static_shapes) - - def graph_fn(anchors, rpn_box_encodings, - rpn_objectness_predictions_with_background, images, - num_proposals, proposal_boxes, refined_box_encodings, - class_predictions_with_background, groundtruth_boxes, - groundtruth_classes): - """Function to construct tf graph for the test.""" - prediction_dict = { - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'image_shape': tf.shape(images), - 'anchors': anchors, - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'proposal_boxes': proposal_boxes, - 'num_proposals': num_proposals - } - _, true_image_shapes = model.preprocess(images) - model.provide_groundtruth(tf.unstack(groundtruth_boxes), - tf.unstack(groundtruth_classes)) - loss_dict = model.loss(prediction_dict, true_image_shapes) - return (loss_dict['Loss/RPNLoss/localization_loss'], - loss_dict['Loss/RPNLoss/objectness_loss'], - loss_dict['Loss/BoxClassifierLoss/localization_loss'], - loss_dict['Loss/BoxClassifierLoss/classification_loss']) - - anchors = np.array( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=np.float32) - rpn_box_encodings = np.zeros( - [batch_size, anchors.shape[1], BOX_CODE_SIZE], dtype=np.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = np.array( - [[[-10, 13], - [10, -10], - [10, -11], - [10, -12]], - [[-10, 13], - [10, -10], - [10, -11], - [10, -12]]], dtype=np.float32) - images = np.zeros([batch_size, 32, 32, 3], dtype=np.float32) - - # box_classifier_batch_size is 6, but here we assume that the number of - # actual proposals (not counting zero paddings) is fewer. - num_proposals = np.array([3, 2], dtype=np.int32) - proposal_boxes = np.array( - [[[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [0, 0, 0, 0], # begin paddings - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 0, 16, 16], - [0, 16, 16, 32], - [0, 0, 0, 0], # begin paddings - [0, 0, 0, 0], - [0, 0, 0, 0], - [0, 0, 0, 0]]], dtype=np.float32) - - refined_box_encodings = np.zeros( - (batch_size * second_stage_batch_size, 1 - if shared_boxes else num_classes, BOX_CODE_SIZE), - dtype=np.float32) - class_predictions_with_background = np.array( - [[-10, 10, -10], # first image - [10, -10, -10], - [10, -10, -10], - [0, 0, 0], # begin paddings - [0, 0, 0], - [0, 0, 0], - [-10, -10, 10], # second image - [10, -10, -10], - [0, 0, 0], # begin paddings - [0, 0, 0], - [0, 0, 0], - [0, 0, 0],], dtype=np.float32) - - # The first groundtruth box is 4/5 of the anchor size in both directions - # experiencing a loss of: - # 2 * SmoothL1(5 * log(4/5)) / num_proposals - # = 2 * (abs(5 * log(1/2)) - .5) / 3 - # The second groundtruth box is identical to the prediction and thus - # experiences zero loss. - # Total average loss is (abs(5 * log(1/2)) - .5) / 3. - groundtruth_boxes = np.stack([ - np.array([[0.05, 0.05, 0.45, 0.45]], dtype=np.float32), - np.array([[0.0, 0.0, 0.5, 0.5]], dtype=np.float32)]) - groundtruth_classes = np.stack([np.array([[1, 0]], dtype=np.float32), - np.array([[0, 1]], dtype=np.float32)]) - - execute_fn = self.execute_cpu - if use_static_shapes: - execute_fn = self.execute - - results = execute_fn(graph_fn, [ - anchors, rpn_box_encodings, rpn_objectness_predictions_with_background, - images, num_proposals, proposal_boxes, refined_box_encodings, - class_predictions_with_background, groundtruth_boxes, - groundtruth_classes - ], graph=g) - - exp_loc_loss = (-5 * np.log(.8) - 0.5) / 3.0 - - self.assertAllClose(results[0], exp_loc_loss, rtol=1e-4, atol=1e-4) - self.assertAllClose(results[1], 0.0) - self.assertAllClose(results[2], exp_loc_loss, rtol=1e-4, atol=1e-4) - self.assertAllClose(results[3], 0.0) - - def test_loss_with_hard_mining(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model(is_training=True, - number_of_stages=2, - second_stage_batch_size=None, - first_stage_max_proposals=6, - hard_mining=True) - batch_size = 1 - def graph_fn(): - """A function with TF compute.""" - anchors = tf.constant( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=tf.float32) - rpn_box_encodings = tf.zeros( - [batch_size, - anchors.get_shape().as_list()[0], - BOX_CODE_SIZE], dtype=tf.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = tf.constant( - [[[-10, 13], - [-10, 12], - [10, -11], - [10, -12]]], dtype=tf.float32) - image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) - - # box_classifier_batch_size is 6, but here we assume that the number of - # actual proposals (not counting zero paddings) is fewer (3). - num_proposals = tf.constant([3], dtype=tf.int32) - proposal_boxes = tf.constant( - [[[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [0, 0, 0, 0], # begin paddings - [0, 0, 0, 0], - [0, 0, 0, 0]]], dtype=tf.float32) - - refined_box_encodings = tf.zeros( - (batch_size * model.max_num_proposals, - model.num_classes, - BOX_CODE_SIZE), dtype=tf.float32) - class_predictions_with_background = tf.constant( - [[-10, 10, -10], # first image - [-10, -10, 10], - [10, -10, -10], - [0, 0, 0], # begin paddings - [0, 0, 0], - [0, 0, 0]], dtype=tf.float32) - - # The first groundtruth box is 4/5 of the anchor size in both directions - # experiencing a loss of: - # 2 * SmoothL1(5 * log(4/5)) / num_proposals - # = 2 * (abs(5 * log(1/2)) - .5) / 3 - # The second groundtruth box is 46/50 of the anchor size in both - # directions experiencing a loss of: - # 2 * SmoothL1(5 * log(42/50)) / num_proposals - # = 2 * (.5(5 * log(.92))^2 - .5) / 3. - # Since the first groundtruth box experiences greater loss, and we have - # set num_hard_examples=1 in the HardMiner, the final localization loss - # corresponds to that of the first groundtruth box. - groundtruth_boxes_list = [ - tf.constant([[0.05, 0.05, 0.45, 0.45], - [0.02, 0.52, 0.48, 0.98],], dtype=tf.float32)] - groundtruth_classes_list = [tf.constant([[1, 0], [0, 1]], - dtype=tf.float32)] - - prediction_dict = { - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'image_shape': image_shape, - 'anchors': anchors, - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'proposal_boxes': proposal_boxes, - 'num_proposals': num_proposals - } - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list) - loss_dict = model.loss(prediction_dict, true_image_shapes) - return (loss_dict['Loss/BoxClassifierLoss/localization_loss'], - loss_dict['Loss/BoxClassifierLoss/classification_loss']) - loc_loss, cls_loss = self.execute_cpu(graph_fn, [], graph=g) - exp_loc_loss = 2 * (-5 * np.log(.8) - 0.5) / 3.0 - self.assertAllClose(loc_loss, exp_loc_loss) - self.assertAllClose(cls_loss, 0) - - def test_loss_with_hard_mining_and_losses_mask(self): - with test_utils.GraphContextOrNone() as g: - model = self._build_model(is_training=True, - number_of_stages=2, - second_stage_batch_size=None, - first_stage_max_proposals=6, - hard_mining=True) - batch_size = 2 - number_of_proposals = 3 - def graph_fn(): - """A function with TF compute.""" - anchors = tf.constant( - [[0, 0, 16, 16], - [0, 16, 16, 32], - [16, 0, 32, 16], - [16, 16, 32, 32]], dtype=tf.float32) - rpn_box_encodings = tf.zeros( - [batch_size, - anchors.get_shape().as_list()[0], - BOX_CODE_SIZE], dtype=tf.float32) - # use different numbers for the objectness category to break ties in - # order of boxes returned by NMS - rpn_objectness_predictions_with_background = tf.constant( - [[[-10, 13], - [-10, 12], - [10, -11], - [10, -12]], - [[-10, 13], - [-10, 12], - [10, -11], - [10, -12]]], dtype=tf.float32) - image_shape = tf.constant([batch_size, 32, 32, 3], dtype=tf.int32) - - # box_classifier_batch_size is 6, but here we assume that the number of - # actual proposals (not counting zero paddings) is fewer (3). - num_proposals = tf.constant([number_of_proposals, number_of_proposals], - dtype=tf.int32) - proposal_boxes = tf.constant( - [[[0, 0, 16, 16], # first image - [0, 16, 16, 32], - [16, 0, 32, 16], - [0, 0, 0, 0], # begin paddings - [0, 0, 0, 0], - [0, 0, 0, 0]], - [[0, 0, 16, 16], # second image - [0, 16, 16, 32], - [16, 0, 32, 16], - [0, 0, 0, 0], # begin paddings - [0, 0, 0, 0], - [0, 0, 0, 0]]], dtype=tf.float32) - - refined_box_encodings = tf.zeros( - (batch_size * model.max_num_proposals, - model.num_classes, - BOX_CODE_SIZE), dtype=tf.float32) - class_predictions_with_background = tf.constant( - [[-10, 10, -10], # first image - [-10, -10, 10], - [10, -10, -10], - [0, 0, 0], # begin paddings - [0, 0, 0], - [0, 0, 0], - [-10, 10, -10], # second image - [-10, -10, 10], - [10, -10, -10], - [0, 0, 0], # begin paddings - [0, 0, 0], - [0, 0, 0]], dtype=tf.float32) - - # The first groundtruth box is 4/5 of the anchor size in both directions - # experiencing a loss of: - # 2 * SmoothL1(5 * log(4/5)) / (num_proposals * batch_size) - # = 2 * (abs(5 * log(1/2)) - .5) / 3 - # The second groundtruth box is 46/50 of the anchor size in both - # directions experiencing a loss of: - # 2 * SmoothL1(5 * log(42/50)) / (num_proposals * batch_size) - # = 2 * (.5(5 * log(.92))^2 - .5) / 3. - # Since the first groundtruth box experiences greater loss, and we have - # set num_hard_examples=1 in the HardMiner, the final localization loss - # corresponds to that of the first groundtruth box. - groundtruth_boxes_list = [ - tf.constant([[0.05, 0.05, 0.45, 0.45], - [0.02, 0.52, 0.48, 0.98]], dtype=tf.float32), - tf.constant([[0.05, 0.05, 0.45, 0.45], - [0.02, 0.52, 0.48, 0.98]], dtype=tf.float32)] - groundtruth_classes_list = [ - tf.constant([[1, 0], [0, 1]], dtype=tf.float32), - tf.constant([[1, 0], [0, 1]], dtype=tf.float32)] - is_annotated_list = [tf.constant(True, dtype=tf.bool), - tf.constant(False, dtype=tf.bool)] - - prediction_dict = { - 'rpn_box_encodings': rpn_box_encodings, - 'rpn_objectness_predictions_with_background': - rpn_objectness_predictions_with_background, - 'image_shape': image_shape, - 'anchors': anchors, - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'proposal_boxes': proposal_boxes, - 'num_proposals': num_proposals - } - _, true_image_shapes = model.preprocess(tf.zeros(image_shape)) - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list, - is_annotated_list=is_annotated_list) - loss_dict = model.loss(prediction_dict, true_image_shapes) - return (loss_dict['Loss/BoxClassifierLoss/localization_loss'], - loss_dict['Loss/BoxClassifierLoss/classification_loss']) - exp_loc_loss = (2 * (-5 * np.log(.8) - 0.5) / - (number_of_proposals * batch_size)) - loc_loss, cls_loss = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllClose(loc_loss, exp_loc_loss) - self.assertAllClose(cls_loss, 0) - - def test_restore_map_for_classification_ckpt(self): - if tf_version.is_tf2(): self.skipTest('Skipping TF1 only test.') - # Define mock tensorflow classification graph and save variables. - test_graph_classification = tf.Graph() - with test_graph_classification.as_default(): - image = tf.placeholder(dtype=tf.float32, shape=[1, 20, 20, 3]) - with tf.variable_scope('mock_model'): - net = slim.conv2d(image, num_outputs=3, kernel_size=1, scope='layer1') - slim.conv2d(net, num_outputs=3, kernel_size=1, scope='layer2') - - init_op = tf.global_variables_initializer() - saver = tf.train.Saver() - save_path = self.get_temp_dir() - with self.test_session(graph=test_graph_classification) as sess: - sess.run(init_op) - saved_model_path = saver.save(sess, save_path) - - # Create tensorflow detection graph and load variables from - # classification checkpoint. - test_graph_detection = tf.Graph() - with test_graph_detection.as_default(): - model = self._build_model( - is_training=False, - number_of_stages=2, second_stage_batch_size=6) - - inputs_shape = (2, 20, 20, 3) - inputs = tf.cast(tf.random_uniform( - inputs_shape, minval=0, maxval=255, dtype=tf.int32), dtype=tf.float32) - preprocessed_inputs, true_image_shapes = model.preprocess(inputs) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - model.postprocess(prediction_dict, true_image_shapes) - var_map = model.restore_map(fine_tune_checkpoint_type='classification') - self.assertIsInstance(var_map, dict) - saver = tf.train.Saver(var_map) - with self.test_session(graph=test_graph_classification) as sess: - saver.restore(sess, saved_model_path) - for var in sess.run(tf.report_uninitialized_variables()): - self.assertNotIn(model.first_stage_feature_extractor_scope, var) - self.assertNotIn(model.second_stage_feature_extractor_scope, var) - - def test_restore_map_for_detection_ckpt(self): - if tf_version.is_tf2(): self.skipTest('Skipping TF1 only test.') - # Define mock tensorflow classification graph and save variables. - # Define first detection graph and save variables. - test_graph_detection1 = tf.Graph() - with test_graph_detection1.as_default(): - model = self._build_model( - is_training=False, - number_of_stages=2, second_stage_batch_size=6) - inputs_shape = (2, 20, 20, 3) - inputs = tf.cast(tf.random_uniform( - inputs_shape, minval=0, maxval=255, dtype=tf.int32), dtype=tf.float32) - preprocessed_inputs, true_image_shapes = model.preprocess(inputs) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - model.postprocess(prediction_dict, true_image_shapes) - another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable - init_op = tf.global_variables_initializer() - saver = tf.train.Saver() - save_path = self.get_temp_dir() - with self.test_session(graph=test_graph_detection1) as sess: - sess.run(init_op) - saved_model_path = saver.save(sess, save_path) - - # Define second detection graph and restore variables. - test_graph_detection2 = tf.Graph() - with test_graph_detection2.as_default(): - model2 = self._build_model(is_training=False, - number_of_stages=2, - second_stage_batch_size=6, num_classes=42) - - inputs_shape2 = (2, 20, 20, 3) - inputs2 = tf.cast(tf.random_uniform( - inputs_shape2, minval=0, maxval=255, dtype=tf.int32), - dtype=tf.float32) - preprocessed_inputs2, true_image_shapes = model2.preprocess(inputs2) - prediction_dict2 = model2.predict(preprocessed_inputs2, true_image_shapes) - model2.postprocess(prediction_dict2, true_image_shapes) - another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable - var_map = model2.restore_map(fine_tune_checkpoint_type='detection') - self.assertIsInstance(var_map, dict) - saver = tf.train.Saver(var_map) - with self.test_session(graph=test_graph_detection2) as sess: - saver.restore(sess, saved_model_path) - uninitialized_vars_list = sess.run(tf.report_uninitialized_variables()) - self.assertIn(six.b('another_variable'), uninitialized_vars_list) - for var in uninitialized_vars_list: - self.assertNotIn( - six.b(model2.first_stage_feature_extractor_scope), var) - self.assertNotIn( - six.b(model2.second_stage_feature_extractor_scope), var) - - def test_load_all_det_checkpoint_vars(self): - if tf_version.is_tf2(): self.skipTest('Skipping TF1 only test.') - test_graph_detection = tf.Graph() - with test_graph_detection.as_default(): - model = self._build_model( - is_training=False, - number_of_stages=2, - second_stage_batch_size=6, - num_classes=42) - - inputs_shape = (2, 20, 20, 3) - inputs = tf.cast( - tf.random_uniform(inputs_shape, minval=0, maxval=255, dtype=tf.int32), - dtype=tf.float32) - preprocessed_inputs, true_image_shapes = model.preprocess(inputs) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - model.postprocess(prediction_dict, true_image_shapes) - another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable - var_map = model.restore_map( - fine_tune_checkpoint_type='detection', - load_all_detection_checkpoint_vars=True) - self.assertIsInstance(var_map, dict) - self.assertIn('another_variable', var_map) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/rfcn_meta_arch.py b/research/object_detection/meta_architectures/rfcn_meta_arch.py deleted file mode 100644 index c19dc04d6ca..00000000000 --- a/research/object_detection/meta_architectures/rfcn_meta_arch.py +++ /dev/null @@ -1,394 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""R-FCN meta-architecture definition. - -R-FCN: Dai, Jifeng, et al. "R-FCN: Object Detection via Region-based -Fully Convolutional Networks." arXiv preprint arXiv:1605.06409 (2016). - -The R-FCN meta architecture is similar to Faster R-CNN and only differs in the -second stage. Hence this class inherits FasterRCNNMetaArch and overrides only -the `_predict_second_stage` method. - -Similar to Faster R-CNN we allow for two modes: number_of_stages=1 and -number_of_stages=2. In the former setting, all of the user facing methods -(e.g., predict, postprocess, loss) can be used as if the model consisted -only of the RPN, returning class agnostic proposals (these can be thought of as -approximate detections with no associated class information). In the latter -setting, proposals are computed, then passed through a second stage -"box classifier" to yield (multi-class) detections. - -Implementations of R-FCN models must define a new FasterRCNNFeatureExtractor and -override three methods: `preprocess`, `_extract_proposal_features` (the first -stage of the model), and `_extract_box_classifier_features` (the second stage of -the model). Optionally, the `restore_fn` method can be overridden. See tests -for an example. - -See notes in the documentation of Faster R-CNN meta-architecture as they all -apply here. -""" -import tensorflow.compat.v1 as tf - -from object_detection.core import box_predictor -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.utils import ops - - -class RFCNMetaArch(faster_rcnn_meta_arch.FasterRCNNMetaArch): - """R-FCN Meta-architecture definition.""" - - def __init__(self, - is_training, - num_classes, - image_resizer_fn, - feature_extractor, - number_of_stages, - first_stage_anchor_generator, - first_stage_target_assigner, - first_stage_atrous_rate, - first_stage_box_predictor_arg_scope_fn, - first_stage_box_predictor_kernel_size, - first_stage_box_predictor_depth, - first_stage_minibatch_size, - first_stage_sampler, - first_stage_non_max_suppression_fn, - first_stage_max_proposals, - first_stage_localization_loss_weight, - first_stage_objectness_loss_weight, - crop_and_resize_fn, - second_stage_target_assigner, - second_stage_rfcn_box_predictor, - second_stage_batch_size, - second_stage_sampler, - second_stage_non_max_suppression_fn, - second_stage_score_conversion_fn, - second_stage_localization_loss_weight, - second_stage_classification_loss_weight, - second_stage_classification_loss, - hard_example_miner, - parallel_iterations=16, - add_summaries=True, - clip_anchors_to_image=False, - use_static_shapes=False, - resize_masks=False, - freeze_batchnorm=False, - return_raw_detections_during_predict=False, - output_final_box_features=False, - output_final_box_rpn_features=False): - """RFCNMetaArch Constructor. - - Args: - is_training: A boolean indicating whether the training version of the - computation graph should be constructed. - num_classes: Number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - image_resizer_fn: A callable for image resizing. This callable always - takes a rank-3 image tensor (corresponding to a single image) and - returns a rank-3 image tensor, possibly with new spatial dimensions. - See builders/image_resizer_builder.py. - feature_extractor: A FasterRCNNFeatureExtractor object. - number_of_stages: Valid values are {1, 2}. If 1 will only construct the - Region Proposal Network (RPN) part of the model. - first_stage_anchor_generator: An anchor_generator.AnchorGenerator object - (note that currently we only support - grid_anchor_generator.GridAnchorGenerator objects) - first_stage_target_assigner: Target assigner to use for first stage of - R-FCN (RPN). - first_stage_atrous_rate: A single integer indicating the atrous rate for - the single convolution op which is applied to the `rpn_features_to_crop` - tensor to obtain a tensor to be used for box prediction. Some feature - extractors optionally allow for producing feature maps computed at - denser resolutions. The atrous rate is used to compensate for the - denser feature maps by using an effectively larger receptive field. - (This should typically be set to 1). - first_stage_box_predictor_arg_scope_fn: Either a - Keras layer hyperparams object or a function to construct tf-slim - arg_scope for conv2d, separable_conv2d and fully_connected ops. Used - for the RPN box predictor. If it is a keras hyperparams object the - RPN box predictor will be a Keras model. If it is a function to - construct an arg scope it will be a tf-slim box predictor. - first_stage_box_predictor_kernel_size: Kernel size to use for the - convolution op just prior to RPN box predictions. - first_stage_box_predictor_depth: Output depth for the convolution op - just prior to RPN box predictions. - first_stage_minibatch_size: The "batch size" to use for computing the - objectness and location loss of the region proposal network. This - "batch size" refers to the number of anchors selected as contributing - to the loss function for any given image within the image batch and is - only called "batch_size" due to terminology from the Faster R-CNN paper. - first_stage_sampler: The sampler for the boxes used to calculate the RPN - loss after the first stage. - first_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression - callable that takes `boxes`, `scores` and optional `clip_window`(with - all other inputs already set) and returns a dictionary containing - tensors with keys: `detection_boxes`, `detection_scores`, - `detection_classes`, `num_detections`. This is used to perform non max - suppression on the boxes predicted by the Region Proposal Network - (RPN). - See `post_processing.batch_multiclass_non_max_suppression` for the type - and shape of these tensors. - first_stage_max_proposals: Maximum number of boxes to retain after - performing Non-Max Suppression (NMS) on the boxes predicted by the - Region Proposal Network (RPN). - first_stage_localization_loss_weight: A float - first_stage_objectness_loss_weight: A float - crop_and_resize_fn: A differentiable resampler to use for cropping RPN - proposal features. - second_stage_target_assigner: Target assigner to use for second stage of - R-FCN. If the model is configured with multiple prediction heads, this - target assigner is used to generate targets for all heads (with the - correct `unmatched_class_label`). - second_stage_rfcn_box_predictor: RFCN box predictor to use for - second stage. - second_stage_batch_size: The batch size used for computing the - classification and refined location loss of the box classifier. This - "batch size" refers to the number of proposals selected as contributing - to the loss function for any given image within the image batch and is - only called "batch_size" due to terminology from the Faster R-CNN paper. - second_stage_sampler: The sampler for the boxes used for second stage - box classifier. - second_stage_non_max_suppression_fn: batch_multiclass_non_max_suppression - callable that takes `boxes`, `scores`, optional `clip_window` and - optional (kwarg) `mask` inputs (with all other inputs already set) - and returns a dictionary containing tensors with keys: - `detection_boxes`, `detection_scores`, `detection_classes`, - `num_detections`, and (optionally) `detection_masks`. See - `post_processing.batch_multiclass_non_max_suppression` for the type and - shape of these tensors. - second_stage_score_conversion_fn: Callable elementwise nonlinearity - (that takes tensors as inputs and returns tensors). This is usually - used to convert logits to probabilities. - second_stage_localization_loss_weight: A float - second_stage_classification_loss_weight: A float - second_stage_classification_loss: A string indicating which loss function - to use, supports 'softmax' and 'sigmoid'. - hard_example_miner: A losses.HardExampleMiner object (can be None). - parallel_iterations: (Optional) The number of iterations allowed to run - in parallel for calls to tf.map_fn. - add_summaries: boolean (default: True) controlling whether summary ops - should be added to tensorflow graph. - clip_anchors_to_image: The anchors generated are clip to the - window size without filtering the nonoverlapping anchors. This generates - a static number of anchors. This argument is unused. - use_static_shapes: If True, uses implementation of ops with static shape - guarantees. - resize_masks: Indicates whether the masks presend in the groundtruth - should be resized in the model with `image_resizer_fn` - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - return_raw_detections_during_predict: Whether to return raw detection - boxes in the predict() method. These are decoded boxes that have not - been through postprocessing (i.e. NMS). Default False. - output_final_box_features: Whether to output final box features. If true, - it crops the feature map based on the final box prediction and returns - it in the dict as detection_features. - output_final_box_rpn_features: Whether to output rpn box features. If - true, it crops the rpn feature map based on the final box prediction and - returns it in the dict as detection_features. - - Raises: - ValueError: If `second_stage_batch_size` > `first_stage_max_proposals` - ValueError: If first_stage_anchor_generator is not of type - grid_anchor_generator.GridAnchorGenerator. - """ - # TODO(rathodv): add_summaries and crop_and_resize_fn is currently - # unused. Respect that directive in the future. - super(RFCNMetaArch, self).__init__( - is_training, - num_classes, - image_resizer_fn, - feature_extractor, - number_of_stages, - first_stage_anchor_generator, - first_stage_target_assigner, - first_stage_atrous_rate, - first_stage_box_predictor_arg_scope_fn, - first_stage_box_predictor_kernel_size, - first_stage_box_predictor_depth, - first_stage_minibatch_size, - first_stage_sampler, - first_stage_non_max_suppression_fn, - first_stage_max_proposals, - first_stage_localization_loss_weight, - first_stage_objectness_loss_weight, - crop_and_resize_fn, - None, # initial_crop_size is not used in R-FCN - None, # maxpool_kernel_size is not use in R-FCN - None, # maxpool_stride is not use in R-FCN - second_stage_target_assigner, - None, # fully_connected_box_predictor is not used in R-FCN. - second_stage_batch_size, - second_stage_sampler, - second_stage_non_max_suppression_fn, - second_stage_score_conversion_fn, - second_stage_localization_loss_weight, - second_stage_classification_loss_weight, - second_stage_classification_loss, - 1.0, # second stage mask prediction loss weight isn't used in R-FCN. - hard_example_miner, - parallel_iterations, - add_summaries, - clip_anchors_to_image, - use_static_shapes, - resize_masks, - freeze_batchnorm=freeze_batchnorm, - return_raw_detections_during_predict=( - return_raw_detections_during_predict), - output_final_box_features=output_final_box_features, - output_final_box_rpn_features=output_final_box_rpn_features) - - self._rfcn_box_predictor = second_stage_rfcn_box_predictor - - def _predict_second_stage(self, rpn_box_encodings, - rpn_objectness_predictions_with_background, - rpn_features, - anchors, - image_shape, - true_image_shapes): - """Predicts the output tensors from 2nd stage of R-FCN. - - Args: - rpn_box_encodings: 3-D float tensor of shape - [batch_size, num_valid_anchors, self._box_coder.code_size] containing - predicted boxes. - rpn_objectness_predictions_with_background: 3-D float tensor of shape - [batch_size, num_valid_anchors, 2] containing class - predictions (logits) for each of the anchors. Note that this - tensor *includes* background class predictions (at class index 0). - rpn_features: A list of single 4-D float32 tensor with shape - [batch_size, height, width, depth] representing image features from the - RPN. - anchors: 2-D float tensor of shape - [num_anchors, self._box_coder.code_size]. - image_shape: A 1D int32 tensors of size [4] containing the image shape. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) refined_box_encodings: a 3-D tensor with shape - [total_num_proposals, num_classes, 4] representing predicted - (final) refined box encodings, where - total_num_proposals=batch_size*self._max_num_proposals - 2) class_predictions_with_background: a 2-D tensor with shape - [total_num_proposals, num_classes + 1] containing class - predictions (logits) for each of the anchors, where - total_num_proposals=batch_size*self._max_num_proposals. - Note that this tensor *includes* background class predictions - (at class index 0). - 3) num_proposals: An int32 tensor of shape [batch_size] representing the - number of proposals generated by the RPN. `num_proposals` allows us - to keep track of which entries are to be treated as zero paddings and - which are not since we always pad the number of proposals to be - `self.max_num_proposals` for each image. - 4) proposal_boxes: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing - decoded proposal bounding boxes (in absolute coordinates). - 5) proposal_boxes_normalized: A float32 tensor of shape - [batch_size, self.max_num_proposals, 4] representing decoded proposal - bounding boxes (in normalized coordinates). Can be used to override - the boxes proposed by the RPN, thus enabling one to extract box - classification and prediction for externally selected areas of the - image. - 6) box_classifier_features: a 4-D float32 tensor, of shape - [batch_size, feature_map_height, feature_map_width, depth], - representing the box classifier features. - """ - image_shape_2d = tf.tile(tf.expand_dims(image_shape[1:], 0), - [image_shape[0], 1]) - (proposal_boxes_normalized, _, _, num_proposals, _, - _) = self._postprocess_rpn(rpn_box_encodings, - rpn_objectness_predictions_with_background, - anchors, image_shape_2d, true_image_shapes) - - rpn_features = rpn_features[0] - box_classifier_features = ( - self._extract_box_classifier_features(rpn_features)) - - if self._rfcn_box_predictor.is_keras_model: - box_predictions = self._rfcn_box_predictor( - [box_classifier_features], - proposal_boxes=proposal_boxes_normalized) - else: - box_predictions = self._rfcn_box_predictor.predict( - [box_classifier_features], - num_predictions_per_location=[1], - scope=self.second_stage_box_predictor_scope, - proposal_boxes=proposal_boxes_normalized) - refined_box_encodings = tf.squeeze( - tf.concat(box_predictions[box_predictor.BOX_ENCODINGS], axis=1), axis=1) - class_predictions_with_background = tf.squeeze( - tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1), - axis=1) - - absolute_proposal_boxes = ops.normalized_to_image_coordinates( - proposal_boxes_normalized, image_shape, - parallel_iterations=self._parallel_iterations) - - prediction_dict = { - 'refined_box_encodings': refined_box_encodings, - 'class_predictions_with_background': - class_predictions_with_background, - 'num_proposals': num_proposals, - 'proposal_boxes': absolute_proposal_boxes, - 'box_classifier_features': box_classifier_features, - 'proposal_boxes_normalized': proposal_boxes_normalized, - 'final_anchors': absolute_proposal_boxes - } - if self._return_raw_detections_during_predict: - prediction_dict.update(self._raw_detections_and_feature_map_inds( - refined_box_encodings, absolute_proposal_boxes)) - return prediction_dict - - def regularization_losses(self): - """Returns a list of regularization losses for this model. - - Returns a list of regularization losses for this model that the estimator - needs to use during training/optimization. - - Returns: - A list of regularization loss tensors. - """ - reg_losses = super(RFCNMetaArch, self).regularization_losses() - if self._rfcn_box_predictor.is_keras_model: - reg_losses.extend(self._rfcn_box_predictor.losses) - return reg_losses - - def updates(self): - """Returns a list of update operators for this model. - - Returns a list of update operators for this model that must be executed at - each training step. The estimator's train op needs to have a control - dependency on these updates. - - Returns: - A list of update operators. - """ - update_ops = super(RFCNMetaArch, self).updates() - - if self._rfcn_box_predictor.is_keras_model: - update_ops.extend( - self._rfcn_box_predictor.get_updates_for(None)) - update_ops.extend( - self._rfcn_box_predictor.get_updates_for( - self._rfcn_box_predictor.inputs)) - return update_ops diff --git a/research/object_detection/meta_architectures/rfcn_meta_arch_test.py b/research/object_detection/meta_architectures/rfcn_meta_arch_test.py deleted file mode 100644 index 9e279bdf499..00000000000 --- a/research/object_detection/meta_architectures/rfcn_meta_arch_test.py +++ /dev/null @@ -1,67 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.meta_architectures.rfcn_meta_arch.""" - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import faster_rcnn_meta_arch_test_lib -from object_detection.meta_architectures import rfcn_meta_arch - - -class RFCNMetaArchTest( - faster_rcnn_meta_arch_test_lib.FasterRCNNMetaArchTestBase): - - def _get_second_stage_box_predictor_text_proto( - self, share_box_across_classes=False): - del share_box_across_classes - box_predictor_text_proto = """ - rfcn_box_predictor { - conv_hyperparams { - op: CONV - activation: NONE - regularizer { - l2_regularizer { - weight: 0.0005 - } - } - initializer { - variance_scaling_initializer { - factor: 1.0 - uniform: true - mode: FAN_AVG - } - } - } - } - """ - return box_predictor_text_proto - - def _get_model(self, box_predictor, **common_kwargs): - return rfcn_meta_arch.RFCNMetaArch( - second_stage_rfcn_box_predictor=box_predictor, **common_kwargs) - - def _get_box_classifier_features_shape(self, - image_size, - batch_size, - max_num_proposals, - initial_crop_size, - maxpool_stride, - num_features): - return (batch_size, image_size, image_size, num_features) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/ssd_meta_arch.py b/research/object_detection/meta_architectures/ssd_meta_arch.py deleted file mode 100644 index bd4e262061c..00000000000 --- a/research/object_detection/meta_architectures/ssd_meta_arch.py +++ /dev/null @@ -1,1368 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSD Meta-architecture definition. - -General tensorflow implementation of convolutional Multibox/SSD detection -models. -""" -import abc -from absl import logging -import tensorflow.compat.v1 as tf -from tensorflow.python.util.deprecation import deprecated_args -from object_detection.core import box_list -from object_detection.core import box_list_ops -from object_detection.core import matcher -from object_detection.core import model -from object_detection.core import standard_fields as fields -from object_detection.core import target_assigner -from object_detection.utils import ops -from object_detection.utils import shape_utils -from object_detection.utils import variables_helper -from object_detection.utils import visualization_utils - - -# pylint: disable=g-import-not-at-top -try: - import tf_slim as slim -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - - -class SSDFeatureExtractor(object): - """SSD Slim Feature Extractor definition.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False): - """Constructor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - self._is_training = is_training - self._depth_multiplier = depth_multiplier - self._min_depth = min_depth - self._pad_to_multiple = pad_to_multiple - self._conv_hyperparams_fn = conv_hyperparams_fn - self._reuse_weights = reuse_weights - self._use_explicit_padding = use_explicit_padding - self._use_depthwise = use_depthwise - self._num_layers = num_layers - self._override_base_feature_extractor_hyperparams = ( - override_base_feature_extractor_hyperparams) - - @property - def is_keras_model(self): - return False - - @abc.abstractmethod - def preprocess(self, resized_inputs): - """Preprocesses images for feature extraction (minus image resizing). - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - """ - pass - - @abc.abstractmethod - def extract_features(self, preprocessed_inputs): - """Extracts features from preprocessed inputs. - - This function is responsible for extracting feature maps from preprocessed - images. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - raise NotImplementedError - - def restore_from_classification_checkpoint_fn(self, feature_extractor_scope): - """Returns a map of variables to load from a foreign checkpoint. - - Args: - feature_extractor_scope: A scope name for the feature extractor. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - """ - variables_to_restore = {} - for variable in variables_helper.get_global_variables_safely(): - var_name = variable.op.name - if var_name.startswith(feature_extractor_scope + '/'): - var_name = var_name.replace(feature_extractor_scope + '/', '') - variables_to_restore[var_name] = variable - - return variables_to_restore - - -class SSDKerasFeatureExtractor(tf.keras.Model): - """SSD Feature Extractor definition.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False, - name=None): - """Constructor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_config`. - name: A string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDKerasFeatureExtractor, self).__init__(name=name) - - self._is_training = is_training - self._depth_multiplier = depth_multiplier - self._min_depth = min_depth - self._pad_to_multiple = pad_to_multiple - self._conv_hyperparams = conv_hyperparams - self._freeze_batchnorm = freeze_batchnorm - self._inplace_batchnorm_update = inplace_batchnorm_update - self._use_explicit_padding = use_explicit_padding - self._use_depthwise = use_depthwise - self._num_layers = num_layers - self._override_base_feature_extractor_hyperparams = ( - override_base_feature_extractor_hyperparams) - - @property - def is_keras_model(self): - return True - - @abc.abstractmethod - def preprocess(self, resized_inputs): - """Preprocesses images for feature extraction (minus image resizing). - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - """ - raise NotImplementedError - - @abc.abstractmethod - def _extract_features(self, preprocessed_inputs): - """Extracts features from preprocessed inputs. - - This function is responsible for extracting feature maps from preprocessed - images. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - raise NotImplementedError - - # This overrides the keras.Model `call` method with the _extract_features - # method. - def call(self, inputs, **kwargs): - return self._extract_features(inputs) - - -class SSDMetaArch(model.DetectionModel): - """SSD Meta-architecture definition.""" - - @deprecated_args(None, - 'NMS is always placed on TPU; do not use nms_on_host ' - 'as it has no effect.', 'nms_on_host') - def __init__(self, - is_training, - anchor_generator, - box_predictor, - box_coder, - feature_extractor, - encode_background_as_zeros, - image_resizer_fn, - non_max_suppression_fn, - score_conversion_fn, - classification_loss, - localization_loss, - classification_loss_weight, - localization_loss_weight, - normalize_loss_by_num_matches, - hard_example_miner, - target_assigner_instance, - add_summaries=True, - normalize_loc_loss_by_codesize=False, - freeze_batchnorm=False, - inplace_batchnorm_update=False, - add_background_class=True, - explicit_background_class=False, - random_example_sampler=None, - expected_loss_weights_fn=None, - use_confidences_as_targets=False, - implicit_example_weight=0.5, - equalization_loss_config=None, - return_raw_detections_during_predict=False, - nms_on_host=True): - """SSDMetaArch Constructor. - - TODO(rathodv,jonathanhuang): group NMS parameters + score converter into - a class and loss parameters into a class and write config protos for - postprocessing and losses. - - Args: - is_training: A boolean indicating whether the training version of the - computation graph should be constructed. - anchor_generator: an anchor_generator.AnchorGenerator object. - box_predictor: a box_predictor.BoxPredictor object. - box_coder: a box_coder.BoxCoder object. - feature_extractor: a SSDFeatureExtractor object. - encode_background_as_zeros: boolean determining whether background - targets are to be encoded as an all zeros vector or a one-hot - vector (where background is the 0th class). - image_resizer_fn: a callable for image resizing. This callable always - takes a rank-3 image tensor (corresponding to a single image) and - returns a rank-3 image tensor, possibly with new spatial dimensions and - a 1-D tensor of shape [3] indicating shape of true image within - the resized image tensor as the resized image tensor could be padded. - See builders/image_resizer_builder.py. - non_max_suppression_fn: batch_multiclass_non_max_suppression - callable that takes `boxes`, `scores` and optional `clip_window` - inputs (with all other inputs already set) and returns a dictionary - hold tensors with keys: `detection_boxes`, `detection_scores`, - `detection_classes` and `num_detections`. See `post_processing. - batch_multiclass_non_max_suppression` for the type and shape of these - tensors. - score_conversion_fn: callable elementwise nonlinearity (that takes tensors - as inputs and returns tensors). This is usually used to convert logits - to probabilities. - classification_loss: an object_detection.core.losses.Loss object. - localization_loss: a object_detection.core.losses.Loss object. - classification_loss_weight: float - localization_loss_weight: float - normalize_loss_by_num_matches: boolean - hard_example_miner: a losses.HardExampleMiner object (can be None) - target_assigner_instance: target_assigner.TargetAssigner instance to use. - add_summaries: boolean (default: True) controlling whether summary ops - should be added to tensorflow graph. - normalize_loc_loss_by_codesize: whether to normalize localization loss - by code size of the box encoder. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - add_background_class: Whether to add an implicit background class to - one-hot encodings of groundtruth labels. Set to false if training a - single class model or using groundtruth labels with an explicit - background class. - explicit_background_class: Set to true if using groundtruth labels with an - explicit background class, as in multiclass scores. - random_example_sampler: a BalancedPositiveNegativeSampler object that can - perform random example sampling when computing loss. If None, random - sampling process is skipped. Note that random example sampler and hard - example miner can both be applied to the model. In that case, random - sampler will take effect first and hard example miner can only process - the random sampled examples. - expected_loss_weights_fn: If not None, use to calculate - loss by background/foreground weighting. Should take batch_cls_targets - as inputs and return foreground_weights, background_weights. See - expected_classification_loss_by_expected_sampling and - expected_classification_loss_by_reweighting_unmatched_anchors in - third_party/tensorflow_models/object_detection/utils/ops.py as examples. - use_confidences_as_targets: Whether to use groundtruth_condifences field - to assign the targets. - implicit_example_weight: a float number that specifies the weight used - for the implicit negative examples. - equalization_loss_config: a namedtuple that specifies configs for - computing equalization loss. - return_raw_detections_during_predict: Whether to return raw detection - boxes in the predict() method. These are decoded boxes that have not - been through postprocessing (i.e. NMS). Default False. - nms_on_host: boolean (default: True) controlling whether NMS should be - carried out on the host (outside of TPU). - """ - super(SSDMetaArch, self).__init__(num_classes=box_predictor.num_classes) - self._is_training = is_training - self._freeze_batchnorm = freeze_batchnorm - self._inplace_batchnorm_update = inplace_batchnorm_update - - self._anchor_generator = anchor_generator - self._box_predictor = box_predictor - - self._box_coder = box_coder - self._feature_extractor = feature_extractor - self._add_background_class = add_background_class - self._explicit_background_class = explicit_background_class - - if add_background_class and explicit_background_class: - raise ValueError("Cannot have both 'add_background_class' and" - " 'explicit_background_class' true.") - - # Needed for fine-tuning from classification checkpoints whose - # variables do not have the feature extractor scope. - if self._feature_extractor.is_keras_model: - # Keras feature extractors will have a name they implicitly use to scope. - # So, all contained variables are prefixed by this name. - # To load from classification checkpoints, need to filter out this name. - self._extract_features_scope = feature_extractor.name - else: - # Slim feature extractors get an explicit naming scope - self._extract_features_scope = 'FeatureExtractor' - - if encode_background_as_zeros: - background_class = [0] - else: - background_class = [1] - - if self._add_background_class: - num_foreground_classes = self.num_classes - else: - num_foreground_classes = self.num_classes - 1 - - self._unmatched_class_label = tf.constant( - background_class + num_foreground_classes * [0], tf.float32) - - self._target_assigner = target_assigner_instance - - self._classification_loss = classification_loss - self._localization_loss = localization_loss - self._classification_loss_weight = classification_loss_weight - self._localization_loss_weight = localization_loss_weight - self._normalize_loss_by_num_matches = normalize_loss_by_num_matches - self._normalize_loc_loss_by_codesize = normalize_loc_loss_by_codesize - self._hard_example_miner = hard_example_miner - self._random_example_sampler = random_example_sampler - self._parallel_iterations = 16 - - self._image_resizer_fn = image_resizer_fn - self._non_max_suppression_fn = non_max_suppression_fn - self._score_conversion_fn = score_conversion_fn - - self._anchors = None - self._add_summaries = add_summaries - self._batched_prediction_tensor_names = [] - self._expected_loss_weights_fn = expected_loss_weights_fn - self._use_confidences_as_targets = use_confidences_as_targets - self._implicit_example_weight = implicit_example_weight - - self._equalization_loss_config = equalization_loss_config - - self._return_raw_detections_during_predict = ( - return_raw_detections_during_predict) - - @property - def feature_extractor(self): - return self._feature_extractor - - @property - def anchors(self): - if not self._anchors: - raise RuntimeError('anchors have not been constructed yet!') - if not isinstance(self._anchors, box_list.BoxList): - raise RuntimeError('anchors should be a BoxList object, but is not.') - return self._anchors - - @property - def batched_prediction_tensor_names(self): - if not self._batched_prediction_tensor_names: - raise RuntimeError('Must call predict() method to get batched prediction ' - 'tensor names.') - return self._batched_prediction_tensor_names - - def preprocess(self, inputs): - """Feature-extractor specific preprocessing. - - SSD meta architecture uses a default clip_window of [0, 0, 1, 1] during - post-processing. On calling `preprocess` method, clip_window gets updated - based on `true_image_shapes` returned by `image_resizer_fn`. - - Args: - inputs: a [batch, height_in, width_in, channels] float tensor representing - a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: a [batch, height_out, width_out, channels] float - tensor representing a batch of images. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Raises: - ValueError: if inputs tensor does not have type tf.float32 - """ - with tf.name_scope('Preprocessor'): - normalized_inputs = self._feature_extractor.preprocess(inputs) - return shape_utils.resize_images_and_return_shapes( - normalized_inputs, self._image_resizer_fn) - - def _compute_clip_window(self, preprocessed_images, true_image_shapes): - """Computes clip window to use during post_processing. - - Computes a new clip window to use during post-processing based on - `resized_image_shapes` and `true_image_shapes` only if `preprocess` method - has been called. Otherwise returns a default clip window of [0, 0, 1, 1]. - - Args: - preprocessed_images: the [batch, height, width, channels] image - tensor. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. Or None if the clip window should cover the full image. - - Returns: - a 2-D float32 tensor of the form [batch_size, 4] containing the clip - window for each image in the batch in normalized coordinates (relative to - the resized dimensions) where each clip window is of the form [ymin, xmin, - ymax, xmax] or a default clip window of [0, 0, 1, 1]. - - """ - if true_image_shapes is None: - return tf.constant([0, 0, 1, 1], dtype=tf.float32) - - resized_inputs_shape = shape_utils.combined_static_and_dynamic_shape( - preprocessed_images) - true_heights, true_widths, _ = tf.unstack( - tf.cast(true_image_shapes, dtype=tf.float32), axis=1) - padded_height = tf.cast(resized_inputs_shape[1], dtype=tf.float32) - padded_width = tf.cast(resized_inputs_shape[2], dtype=tf.float32) - return tf.stack( - [ - tf.zeros_like(true_heights), - tf.zeros_like(true_widths), true_heights / padded_height, - true_widths / padded_width - ], - axis=1) - - def predict(self, preprocessed_inputs, true_image_shapes): - """Predicts unpostprocessed tensors from input tensor. - - This function takes an input batch of images and runs it through the forward - pass of the network to yield unpostprocessesed predictions. - - A side effect of calling the predict method is that self._anchors is - populated with a box_list.BoxList of anchors. These anchors must be - constructed before the postprocess or loss functions can be called. - - Args: - preprocessed_inputs: a [batch, height, width, channels] image tensor. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - - Returns: - prediction_dict: a dictionary holding "raw" prediction tensors: - 1) preprocessed_inputs: the [batch, height, width, channels] image - tensor. - 2) box_encodings: 4-D float tensor of shape [batch_size, num_anchors, - box_code_dimension] containing predicted boxes. - 3) class_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, num_classes+1] containing class predictions - (logits) for each of the anchors. Note that this tensor *includes* - background class predictions (at class index 0). - 4) feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i]. - 5) anchors: 2-D float tensor of shape [num_anchors, 4] containing - the generated anchors in normalized coordinates. - 6) final_anchors: 3-D float tensor of shape [batch_size, num_anchors, 4] - containing the generated anchors in normalized coordinates. - If self._return_raw_detections_during_predict is True, the dictionary - will also contain: - 7) raw_detection_boxes: a 4-D float32 tensor with shape - [batch_size, self.max_num_proposals, 4] in normalized coordinates. - 8) raw_detection_feature_map_indices: a 3-D int32 tensor with shape - [batch_size, self.max_num_proposals]. - """ - if self._inplace_batchnorm_update: - batchnorm_updates_collections = None - else: - batchnorm_updates_collections = tf.GraphKeys.UPDATE_OPS - if self._feature_extractor.is_keras_model: - feature_maps = self._feature_extractor(preprocessed_inputs) - else: - with slim.arg_scope([slim.batch_norm], - is_training=(self._is_training and - not self._freeze_batchnorm), - updates_collections=batchnorm_updates_collections): - with tf.variable_scope(None, self._extract_features_scope, - [preprocessed_inputs]): - feature_maps = self._feature_extractor.extract_features( - preprocessed_inputs) - - feature_map_spatial_dims = self._get_feature_map_spatial_dims( - feature_maps) - logging.info('feature_map_spatial_dims: %s', feature_map_spatial_dims) - image_shape = shape_utils.combined_static_and_dynamic_shape( - preprocessed_inputs) - boxlist_list = self._anchor_generator.generate( - feature_map_spatial_dims, - im_height=image_shape[1], - im_width=image_shape[2]) - self._anchors = box_list_ops.concatenate(boxlist_list) - if self._box_predictor.is_keras_model: - predictor_results_dict = self._box_predictor(feature_maps) - else: - with slim.arg_scope([slim.batch_norm], - is_training=(self._is_training and - not self._freeze_batchnorm), - updates_collections=batchnorm_updates_collections): - predictor_results_dict = self._box_predictor.predict( - feature_maps, self._anchor_generator.num_anchors_per_location()) - predictions_dict = { - 'preprocessed_inputs': - preprocessed_inputs, - 'feature_maps': - feature_maps, - 'anchors': - self._anchors.get(), - 'final_anchors': - tf.tile( - tf.expand_dims(self._anchors.get(), 0), [image_shape[0], 1, 1]) - } - for prediction_key, prediction_list in iter(predictor_results_dict.items()): - prediction = tf.concat(prediction_list, axis=1) - if (prediction_key == 'box_encodings' and prediction.shape.ndims == 4 and - prediction.shape[2] == 1): - prediction = tf.squeeze(prediction, axis=2) - predictions_dict[prediction_key] = prediction - if self._return_raw_detections_during_predict: - predictions_dict.update(self._raw_detections_and_feature_map_inds( - predictions_dict['box_encodings'], boxlist_list)) - self._batched_prediction_tensor_names = [x for x in predictions_dict - if x != 'anchors'] - return predictions_dict - - def _raw_detections_and_feature_map_inds(self, box_encodings, boxlist_list): - anchors = self._anchors.get() - raw_detection_boxes, _ = self._batch_decode(box_encodings, anchors) - batch_size, _, _ = shape_utils.combined_static_and_dynamic_shape( - raw_detection_boxes) - feature_map_indices = ( - self._anchor_generator.anchor_index_to_feature_map_index(boxlist_list)) - feature_map_indices_batched = tf.tile( - tf.expand_dims(feature_map_indices, 0), - multiples=[batch_size, 1]) - return { - fields.PredictionFields.raw_detection_boxes: raw_detection_boxes, - fields.PredictionFields.raw_detection_feature_map_indices: - feature_map_indices_batched - } - - def _get_feature_map_spatial_dims(self, feature_maps): - """Return list of spatial dimensions for each feature map in a list. - - Args: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i]. - - Returns: - a list of pairs (height, width) for each feature map in feature_maps - """ - feature_map_shapes = [ - shape_utils.combined_static_and_dynamic_shape( - feature_map) for feature_map in feature_maps - ] - return [(shape[1], shape[2]) for shape in feature_map_shapes] - - def postprocess(self, prediction_dict, true_image_shapes): - """Converts prediction tensors to final detections. - - This function converts raw predictions tensors to final detection results by - slicing off the background class, decoding box predictions and applying - non max suppression and clipping to the image window. - - See base class for output format conventions. Note also that by default, - scores are to be interpreted as logits, but if a score_conversion_fn is - used, then scores are remapped (and may thus have a different - interpretation). - - Args: - prediction_dict: a dictionary holding prediction tensors with - 1) preprocessed_inputs: a [batch, height, width, channels] image - tensor. - 2) box_encodings: 3-D float tensor of shape [batch_size, num_anchors, - box_code_dimension] containing predicted boxes. - 3) class_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, num_classes+1] containing class predictions - (logits) for each of the anchors. Note that this tensor *includes* - background class predictions. - 4) mask_predictions: (optional) a 5-D float tensor of shape - [batch_size, num_anchors, q, mask_height, mask_width]. `q` can be - either number of classes or 1 depending on whether a separate mask is - predicted per class. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. Or None, if the clip window should cover the full image. - - Returns: - detections: a dictionary containing the following fields - detection_boxes: [batch, max_detections, 4] tensor with post-processed - detection boxes. - detection_scores: [batch, max_detections] tensor with scalar scores for - post-processed detection boxes. - detection_multiclass_scores: [batch, max_detections, - num_classes_with_background] tensor with class score distribution for - post-processed detection boxes including background class if any. - detection_classes: [batch, max_detections] tensor with classes for - post-processed detection classes. - detection_keypoints: [batch, max_detections, num_keypoints, 2] (if - encoded in the prediction_dict 'box_encodings') - detection_masks: [batch_size, max_detections, mask_height, mask_width] - (optional) - num_detections: [batch] - raw_detection_boxes: [batch, total_detections, 4] tensor with decoded - detection boxes before Non-Max Suppression. - raw_detection_score: [batch, total_detections, - num_classes_with_background] tensor of multi-class scores for raw - detection boxes. - Raises: - ValueError: if prediction_dict does not contain `box_encodings` or - `class_predictions_with_background` fields. - """ - if ('box_encodings' not in prediction_dict or - 'class_predictions_with_background' not in prediction_dict): - raise ValueError('prediction_dict does not contain expected entries.') - if 'anchors' not in prediction_dict: - prediction_dict['anchors'] = self.anchors.get() - with tf.name_scope('Postprocessor'): - preprocessed_images = prediction_dict['preprocessed_inputs'] - box_encodings = prediction_dict['box_encodings'] - box_encodings = tf.identity(box_encodings, 'raw_box_encodings') - class_predictions_with_background = ( - prediction_dict['class_predictions_with_background']) - detection_boxes, detection_keypoints = self._batch_decode( - box_encodings, prediction_dict['anchors']) - detection_boxes = tf.identity(detection_boxes, 'raw_box_locations') - detection_boxes = tf.expand_dims(detection_boxes, axis=2) - - detection_scores_with_background = self._score_conversion_fn( - class_predictions_with_background) - detection_scores = tf.identity(detection_scores_with_background, - 'raw_box_scores') - if self._add_background_class or self._explicit_background_class: - detection_scores = tf.slice(detection_scores, [0, 0, 1], [-1, -1, -1]) - additional_fields = None - - batch_size = ( - shape_utils.combined_static_and_dynamic_shape(preprocessed_images)[0]) - - if 'feature_maps' in prediction_dict: - feature_map_list = [] - for feature_map in prediction_dict['feature_maps']: - feature_map_list.append(tf.reshape(feature_map, [batch_size, -1])) - box_features = tf.concat(feature_map_list, 1) - box_features = tf.identity(box_features, 'raw_box_features') - additional_fields = { - 'multiclass_scores': detection_scores_with_background - } - if self._anchors is not None: - num_boxes = (self._anchors.num_boxes_static() or - self._anchors.num_boxes()) - anchor_indices = tf.range(num_boxes) - batch_anchor_indices = tf.tile( - tf.expand_dims(anchor_indices, 0), [batch_size, 1]) - # All additional fields need to be float. - additional_fields.update({ - 'anchor_indices': tf.cast(batch_anchor_indices, tf.float32), - }) - if detection_keypoints is not None: - detection_keypoints = tf.identity( - detection_keypoints, 'raw_keypoint_locations') - additional_fields[fields.BoxListFields.keypoints] = detection_keypoints - - (nmsed_boxes, nmsed_scores, nmsed_classes, nmsed_masks, - nmsed_additional_fields, - num_detections) = self._non_max_suppression_fn( - detection_boxes, - detection_scores, - clip_window=self._compute_clip_window( - preprocessed_images, true_image_shapes), - additional_fields=additional_fields, - masks=prediction_dict.get('mask_predictions')) - - detection_dict = { - fields.DetectionResultFields.detection_boxes: - nmsed_boxes, - fields.DetectionResultFields.detection_scores: - nmsed_scores, - fields.DetectionResultFields.detection_classes: - nmsed_classes, - fields.DetectionResultFields.num_detections: - tf.cast(num_detections, dtype=tf.float32), - fields.DetectionResultFields.raw_detection_boxes: - tf.squeeze(detection_boxes, axis=2), - fields.DetectionResultFields.raw_detection_scores: - detection_scores_with_background - } - if (nmsed_additional_fields is not None and - fields.InputDataFields.multiclass_scores in nmsed_additional_fields): - detection_dict[ - fields.DetectionResultFields.detection_multiclass_scores] = ( - nmsed_additional_fields[ - fields.InputDataFields.multiclass_scores]) - if (nmsed_additional_fields is not None and - 'anchor_indices' in nmsed_additional_fields): - detection_dict.update({ - fields.DetectionResultFields.detection_anchor_indices: - tf.cast(nmsed_additional_fields['anchor_indices'], tf.int32), - }) - if (nmsed_additional_fields is not None and - fields.BoxListFields.keypoints in nmsed_additional_fields): - detection_dict[fields.DetectionResultFields.detection_keypoints] = ( - nmsed_additional_fields[fields.BoxListFields.keypoints]) - if nmsed_masks is not None: - detection_dict[ - fields.DetectionResultFields.detection_masks] = nmsed_masks - return detection_dict - - def loss(self, prediction_dict, true_image_shapes, scope=None): - """Compute scalar loss tensors with respect to provided groundtruth. - - Calling this function requires that groundtruth tensors have been - provided via the provide_groundtruth function. - - Args: - prediction_dict: a dictionary holding prediction tensors with - 1) box_encodings: 3-D float tensor of shape [batch_size, num_anchors, - box_code_dimension] containing predicted boxes. - 2) class_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, num_classes+1] containing class predictions - (logits) for each of the anchors. Note that this tensor *includes* - background class predictions. - true_image_shapes: int32 tensor of shape [batch, 3] where each row is - of the form [height, width, channels] indicating the shapes - of true images in the resized images, as resized images can be padded - with zeros. - scope: Optional scope name. - - Returns: - a dictionary mapping loss keys (`localization_loss` and - `classification_loss`) to scalar tensors representing corresponding loss - values. - """ - with tf.name_scope(scope, 'Loss', prediction_dict.values()): - keypoints = None - if self.groundtruth_has_field(fields.BoxListFields.keypoints): - keypoints = self.groundtruth_lists(fields.BoxListFields.keypoints) - weights = None - if self.groundtruth_has_field(fields.BoxListFields.weights): - weights = self.groundtruth_lists(fields.BoxListFields.weights) - confidences = None - if self.groundtruth_has_field(fields.BoxListFields.confidences): - confidences = self.groundtruth_lists(fields.BoxListFields.confidences) - (batch_cls_targets, batch_cls_weights, batch_reg_targets, - batch_reg_weights, batch_match) = self._assign_targets( - self.groundtruth_lists(fields.BoxListFields.boxes), - self.groundtruth_lists(fields.BoxListFields.classes), - keypoints, weights, confidences) - match_list = [matcher.Match(match) for match in tf.unstack(batch_match)] - if self._add_summaries: - self._summarize_target_assignment( - self.groundtruth_lists(fields.BoxListFields.boxes), match_list) - - if self._random_example_sampler: - batch_cls_per_anchor_weights = tf.reduce_mean( - batch_cls_weights, axis=-1) - batch_sampled_indicator = tf.cast( - shape_utils.static_or_dynamic_map_fn( - self._minibatch_subsample_fn, - [batch_cls_targets, batch_cls_per_anchor_weights], - dtype=tf.bool, - parallel_iterations=self._parallel_iterations, - back_prop=True), dtype=tf.float32) - batch_reg_weights = tf.multiply(batch_sampled_indicator, - batch_reg_weights) - batch_cls_weights = tf.multiply( - tf.expand_dims(batch_sampled_indicator, -1), - batch_cls_weights) - - losses_mask = None - if self.groundtruth_has_field(fields.InputDataFields.is_annotated): - losses_mask = tf.stack(self.groundtruth_lists( - fields.InputDataFields.is_annotated)) - - - location_losses = self._localization_loss( - prediction_dict['box_encodings'], - batch_reg_targets, - ignore_nan_targets=True, - weights=batch_reg_weights, - losses_mask=losses_mask) - - cls_losses = self._classification_loss( - prediction_dict['class_predictions_with_background'], - batch_cls_targets, - weights=batch_cls_weights, - losses_mask=losses_mask) - - if self._expected_loss_weights_fn: - # Need to compute losses for assigned targets against the - # unmatched_class_label as well as their assigned targets. - # simplest thing (but wasteful) is just to calculate all losses - # twice - batch_size, num_anchors, num_classes = batch_cls_targets.get_shape() - unmatched_targets = tf.ones([batch_size, num_anchors, 1 - ]) * self._unmatched_class_label - - unmatched_cls_losses = self._classification_loss( - prediction_dict['class_predictions_with_background'], - unmatched_targets, - weights=batch_cls_weights, - losses_mask=losses_mask) - - if cls_losses.get_shape().ndims == 3: - batch_size, num_anchors, num_classes = cls_losses.get_shape() - cls_losses = tf.reshape(cls_losses, [batch_size, -1]) - unmatched_cls_losses = tf.reshape(unmatched_cls_losses, - [batch_size, -1]) - batch_cls_targets = tf.reshape( - batch_cls_targets, [batch_size, num_anchors * num_classes, -1]) - batch_cls_targets = tf.concat( - [1 - batch_cls_targets, batch_cls_targets], axis=-1) - - location_losses = tf.tile(location_losses, [1, num_classes]) - - foreground_weights, background_weights = ( - self._expected_loss_weights_fn(batch_cls_targets)) - - cls_losses = ( - foreground_weights * cls_losses + - background_weights * unmatched_cls_losses) - - location_losses *= foreground_weights - - classification_loss = tf.reduce_sum(cls_losses) - localization_loss = tf.reduce_sum(location_losses) - elif self._hard_example_miner: - cls_losses = ops.reduce_sum_trailing_dimensions(cls_losses, ndims=2) - (localization_loss, classification_loss) = self._apply_hard_mining( - location_losses, cls_losses, prediction_dict, match_list) - if self._add_summaries: - self._hard_example_miner.summarize() - else: - cls_losses = ops.reduce_sum_trailing_dimensions(cls_losses, ndims=2) - localization_loss = tf.reduce_sum(location_losses) - classification_loss = tf.reduce_sum(cls_losses) - - # Optionally normalize by number of positive matches - normalizer = tf.constant(1.0, dtype=tf.float32) - if self._normalize_loss_by_num_matches: - normalizer = tf.maximum(tf.cast(tf.reduce_sum(batch_reg_weights), - dtype=tf.float32), - 1.0) - - localization_loss_normalizer = normalizer - if self._normalize_loc_loss_by_codesize: - localization_loss_normalizer *= self._box_coder.code_size - localization_loss = tf.multiply((self._localization_loss_weight / - localization_loss_normalizer), - localization_loss, - name='localization_loss') - classification_loss = tf.multiply((self._classification_loss_weight / - normalizer), classification_loss, - name='classification_loss') - - loss_dict = { - 'Loss/localization_loss': localization_loss, - 'Loss/classification_loss': classification_loss - } - - - return loss_dict - - def _minibatch_subsample_fn(self, inputs): - """Randomly samples anchors for one image. - - Args: - inputs: a list of 2 inputs. First one is a tensor of shape [num_anchors, - num_classes] indicating targets assigned to each anchor. Second one - is a tensor of shape [num_anchors] indicating the class weight of each - anchor. - - Returns: - batch_sampled_indicator: bool tensor of shape [num_anchors] indicating - whether the anchor should be selected for loss computation. - """ - cls_targets, cls_weights = inputs - if self._add_background_class: - # Set background_class bits to 0 so that the positives_indicator - # computation would not consider background class. - background_class = tf.zeros_like(tf.slice(cls_targets, [0, 0], [-1, 1])) - regular_class = tf.slice(cls_targets, [0, 1], [-1, -1]) - cls_targets = tf.concat([background_class, regular_class], 1) - positives_indicator = tf.reduce_sum(cls_targets, axis=1) - return self._random_example_sampler.subsample( - tf.cast(cls_weights, tf.bool), - batch_size=None, - labels=tf.cast(positives_indicator, tf.bool)) - - def _summarize_anchor_classification_loss(self, class_ids, cls_losses): - positive_indices = tf.where(tf.greater(class_ids, 0)) - positive_anchor_cls_loss = tf.squeeze( - tf.gather(cls_losses, positive_indices), axis=1) - visualization_utils.add_cdf_image_summary(positive_anchor_cls_loss, - 'PositiveAnchorLossCDF') - negative_indices = tf.where(tf.equal(class_ids, 0)) - negative_anchor_cls_loss = tf.squeeze( - tf.gather(cls_losses, negative_indices), axis=1) - visualization_utils.add_cdf_image_summary(negative_anchor_cls_loss, - 'NegativeAnchorLossCDF') - - def _assign_targets(self, - groundtruth_boxes_list, - groundtruth_classes_list, - groundtruth_keypoints_list=None, - groundtruth_weights_list=None, - groundtruth_confidences_list=None): - """Assign groundtruth targets. - - Adds a background class to each one-hot encoding of groundtruth classes - and uses target assigner to obtain regression and classification targets. - - Args: - groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4] - containing coordinates of the groundtruth boxes. - Groundtruth boxes are provided in [y_min, x_min, y_max, x_max] - format and assumed to be normalized and clipped - relative to the image window with y_min <= y_max and x_min <= x_max. - groundtruth_classes_list: a list of 2-D one-hot (or k-hot) tensors of - shape [num_boxes, num_classes] containing the class targets with the 0th - index assumed to map to the first non-background class. - groundtruth_keypoints_list: (optional) a list of 3-D tensors of shape - [num_boxes, num_keypoints, 2] - groundtruth_weights_list: A list of 1-D tf.float32 tensors of shape - [num_boxes] containing weights for groundtruth boxes. - groundtruth_confidences_list: A list of 2-D tf.float32 tensors of shape - [num_boxes, num_classes] containing class confidences for - groundtruth boxes. - - Returns: - batch_cls_targets: a tensor with shape [batch_size, num_anchors, - num_classes], - batch_cls_weights: a tensor with shape [batch_size, num_anchors], - batch_reg_targets: a tensor with shape [batch_size, num_anchors, - box_code_dimension] - batch_reg_weights: a tensor with shape [batch_size, num_anchors], - match: an int32 tensor of shape [batch_size, num_anchors], containing - result of anchor groundtruth matching. Each position in the tensor - indicates an anchor and holds the following meaning: - (1) if match[x, i] >= 0, anchor i is matched with groundtruth - match[x, i]. - (2) if match[x, i]=-1, anchor i is marked to be background . - (3) if match[x, i]=-2, anchor i is ignored since it is not background - and does not have sufficient overlap to call it a foreground. - """ - groundtruth_boxlists = [ - box_list.BoxList(boxes) for boxes in groundtruth_boxes_list - ] - train_using_confidences = (self._is_training and - self._use_confidences_as_targets) - if self._add_background_class: - groundtruth_classes_with_background_list = [ - tf.pad(one_hot_encoding, [[0, 0], [1, 0]], mode='CONSTANT') - for one_hot_encoding in groundtruth_classes_list - ] - if train_using_confidences: - groundtruth_confidences_with_background_list = [ - tf.pad(groundtruth_confidences, [[0, 0], [1, 0]], mode='CONSTANT') - for groundtruth_confidences in groundtruth_confidences_list - ] - else: - groundtruth_classes_with_background_list = groundtruth_classes_list - - if groundtruth_keypoints_list is not None: - for boxlist, keypoints in zip( - groundtruth_boxlists, groundtruth_keypoints_list): - boxlist.add_field(fields.BoxListFields.keypoints, keypoints) - if train_using_confidences: - return target_assigner.batch_assign_confidences( - self._target_assigner, - self.anchors, - groundtruth_boxlists, - groundtruth_confidences_with_background_list, - groundtruth_weights_list, - self._unmatched_class_label, - self._add_background_class, - self._implicit_example_weight) - else: - return target_assigner.batch_assign_targets( - self._target_assigner, - self.anchors, - groundtruth_boxlists, - groundtruth_classes_with_background_list, - self._unmatched_class_label, - groundtruth_weights_list) - - def _summarize_target_assignment(self, groundtruth_boxes_list, match_list): - """Creates tensorflow summaries for the input boxes and anchors. - - This function creates four summaries corresponding to the average - number (over images in a batch) of (1) groundtruth boxes, (2) anchors - marked as positive, (3) anchors marked as negative, and (4) anchors marked - as ignored. - - Args: - groundtruth_boxes_list: a list of 2-D tensors of shape [num_boxes, 4] - containing corners of the groundtruth boxes. - match_list: a list of matcher.Match objects encoding the match between - anchors and groundtruth boxes for each image of the batch, - with rows of the Match objects corresponding to groundtruth boxes - and columns corresponding to anchors. - """ - # TODO(rathodv): Add a test for these summaries. - try: - # TODO(kaftan): Integrate these summaries into the v2 style loops - with tf.compat.v2.init_scope(): - if tf.compat.v2.executing_eagerly(): - return - except AttributeError: - pass - - avg_num_gt_boxes = tf.reduce_mean( - tf.cast( - tf.stack([tf.shape(x)[0] for x in groundtruth_boxes_list]), - dtype=tf.float32)) - avg_num_matched_gt_boxes = tf.reduce_mean( - tf.cast( - tf.stack([match.num_matched_rows() for match in match_list]), - dtype=tf.float32)) - avg_pos_anchors = tf.reduce_mean( - tf.cast( - tf.stack([match.num_matched_columns() for match in match_list]), - dtype=tf.float32)) - avg_neg_anchors = tf.reduce_mean( - tf.cast( - tf.stack([match.num_unmatched_columns() for match in match_list]), - dtype=tf.float32)) - avg_ignored_anchors = tf.reduce_mean( - tf.cast( - tf.stack([match.num_ignored_columns() for match in match_list]), - dtype=tf.float32)) - - tf.summary.scalar('AvgNumGroundtruthBoxesPerImage', - avg_num_gt_boxes, - family='TargetAssignment') - tf.summary.scalar('AvgNumGroundtruthBoxesMatchedPerImage', - avg_num_matched_gt_boxes, - family='TargetAssignment') - tf.summary.scalar('AvgNumPositiveAnchorsPerImage', - avg_pos_anchors, - family='TargetAssignment') - tf.summary.scalar('AvgNumNegativeAnchorsPerImage', - avg_neg_anchors, - family='TargetAssignment') - tf.summary.scalar('AvgNumIgnoredAnchorsPerImage', - avg_ignored_anchors, - family='TargetAssignment') - - def _apply_hard_mining(self, location_losses, cls_losses, prediction_dict, - match_list): - """Applies hard mining to anchorwise losses. - - Args: - location_losses: Float tensor of shape [batch_size, num_anchors] - representing anchorwise location losses. - cls_losses: Float tensor of shape [batch_size, num_anchors] - representing anchorwise classification losses. - prediction_dict: p a dictionary holding prediction tensors with - 1) box_encodings: 3-D float tensor of shape [batch_size, num_anchors, - box_code_dimension] containing predicted boxes. - 2) class_predictions_with_background: 3-D float tensor of shape - [batch_size, num_anchors, num_classes+1] containing class predictions - (logits) for each of the anchors. Note that this tensor *includes* - background class predictions. - 3) anchors: (optional) 2-D float tensor of shape [num_anchors, 4]. - match_list: a list of matcher.Match objects encoding the match between - anchors and groundtruth boxes for each image of the batch, - with rows of the Match objects corresponding to groundtruth boxes - and columns corresponding to anchors. - - Returns: - mined_location_loss: a float scalar with sum of localization losses from - selected hard examples. - mined_cls_loss: a float scalar with sum of classification losses from - selected hard examples. - """ - class_predictions = prediction_dict['class_predictions_with_background'] - if self._add_background_class: - class_predictions = tf.slice(class_predictions, [0, 0, 1], [-1, -1, -1]) - - if 'anchors' not in prediction_dict: - prediction_dict['anchors'] = self.anchors.get() - decoded_boxes, _ = self._batch_decode(prediction_dict['box_encodings'], - prediction_dict['anchors']) - decoded_box_tensors_list = tf.unstack(decoded_boxes) - class_prediction_list = tf.unstack(class_predictions) - decoded_boxlist_list = [] - for box_location, box_score in zip(decoded_box_tensors_list, - class_prediction_list): - decoded_boxlist = box_list.BoxList(box_location) - decoded_boxlist.add_field('scores', box_score) - decoded_boxlist_list.append(decoded_boxlist) - return self._hard_example_miner( - location_losses=location_losses, - cls_losses=cls_losses, - decoded_boxlist_list=decoded_boxlist_list, - match_list=match_list) - - def _batch_decode(self, box_encodings, anchors): - """Decodes a batch of box encodings with respect to the anchors. - - Args: - box_encodings: A float32 tensor of shape - [batch_size, num_anchors, box_code_size] containing box encodings. - anchors: A tensor of shape [num_anchors, 4]. - - Returns: - decoded_boxes: A float32 tensor of shape - [batch_size, num_anchors, 4] containing the decoded boxes. - decoded_keypoints: A float32 tensor of shape - [batch_size, num_anchors, num_keypoints, 2] containing the decoded - keypoints if present in the input `box_encodings`, None otherwise. - """ - combined_shape = shape_utils.combined_static_and_dynamic_shape( - box_encodings) - batch_size = combined_shape[0] - tiled_anchor_boxes = tf.tile(tf.expand_dims(anchors, 0), [batch_size, 1, 1]) - tiled_anchors_boxlist = box_list.BoxList( - tf.reshape(tiled_anchor_boxes, [-1, 4])) - decoded_boxes = self._box_coder.decode( - tf.reshape(box_encodings, [-1, self._box_coder.code_size]), - tiled_anchors_boxlist) - decoded_keypoints = None - if decoded_boxes.has_field(fields.BoxListFields.keypoints): - decoded_keypoints = decoded_boxes.get_field( - fields.BoxListFields.keypoints) - num_keypoints = decoded_keypoints.get_shape()[1] - decoded_keypoints = tf.reshape( - decoded_keypoints, - tf.stack([combined_shape[0], combined_shape[1], num_keypoints, 2])) - decoded_boxes = tf.reshape(decoded_boxes.get(), tf.stack( - [combined_shape[0], combined_shape[1], 4])) - return decoded_boxes, decoded_keypoints - - def regularization_losses(self): - """Returns a list of regularization losses for this model. - - Returns a list of regularization losses for this model that the estimator - needs to use during training/optimization. - - Returns: - A list of regularization loss tensors. - """ - losses = [] - slim_losses = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) - # Copy the slim losses to avoid modifying the collection - if slim_losses: - losses.extend(slim_losses) - if self._box_predictor.is_keras_model: - losses.extend(self._box_predictor.losses) - if self._feature_extractor.is_keras_model: - losses.extend(self._feature_extractor.losses) - return losses - - def restore_map(self, - fine_tune_checkpoint_type='detection', - load_all_detection_checkpoint_vars=False): - """Returns a map of variables to load from a foreign checkpoint. - - See parent class for details. - - Args: - fine_tune_checkpoint_type: whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`. Default 'detection'. - load_all_detection_checkpoint_vars: whether to load all variables (when - `fine_tune_checkpoint_type` is `detection`). If False, only variables - within the feature extractor scope are included. Default False. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - Raises: - ValueError: if fine_tune_checkpoint_type is neither `classification` - nor `detection`. - """ - if fine_tune_checkpoint_type == 'classification': - return self._feature_extractor.restore_from_classification_checkpoint_fn( - self._extract_features_scope) - - elif fine_tune_checkpoint_type == 'detection': - variables_to_restore = {} - for variable in variables_helper.get_global_variables_safely(): - var_name = variable.op.name - if load_all_detection_checkpoint_vars: - variables_to_restore[var_name] = variable - else: - if var_name.startswith(self._extract_features_scope): - variables_to_restore[var_name] = variable - return variables_to_restore - - else: - raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format( - fine_tune_checkpoint_type)) - - def restore_from_objects(self, fine_tune_checkpoint_type='detection'): - """Returns a map of Trackable objects to load from a foreign checkpoint. - - Returns a dictionary of Tensorflow 2 Trackable objects (e.g. tf.Module - or Checkpoint). This enables the model to initialize based on weights from - another task. For example, the feature extractor variables from a - classification model can be used to bootstrap training of an object - detector. When loading from an object detection model, the checkpoint model - should have the same parameters as this detection model with exception of - the num_classes parameter. - - Note that this function is intended to be used to restore Keras-based - models when running Tensorflow 2, whereas restore_map (above) is intended - to be used to restore Slim-based models when running Tensorflow 1.x. - - Args: - fine_tune_checkpoint_type: A string inidicating the subset of variables - to load. Valid values: `detection`, `classification`, `full`. Default - `detection`. - An SSD checkpoint has three parts: - 1) Classification Network (like ResNet) - 2) DeConv layers (for FPN) - 3) Box/Class prediction parameters - The parameters will be loaded using the following strategy: - `classification` - will load #1 - `detection` - will load #1, #2 - `full` - will load #1, #2, #3 - - Returns: - A dict mapping keys to Trackable objects (tf.Module or Checkpoint). - """ - if fine_tune_checkpoint_type == 'classification': - return { - 'feature_extractor': - self._feature_extractor.classification_backbone - } - elif fine_tune_checkpoint_type == 'detection': - fake_model = tf.train.Checkpoint( - _feature_extractor=self._feature_extractor) - return {'model': fake_model} - - elif fine_tune_checkpoint_type == 'full': - return {'model': self} - - else: - raise ValueError('Not supported fine_tune_checkpoint_type: {}'.format( - fine_tune_checkpoint_type)) - - def updates(self): - """Returns a list of update operators for this model. - - Returns a list of update operators for this model that must be executed at - each training step. The estimator's train op needs to have a control - dependency on these updates. - - Returns: - A list of update operators. - """ - update_ops = [] - slim_update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) - # Copy the slim ops to avoid modifying the collection - if slim_update_ops: - update_ops.extend(slim_update_ops) - if self._box_predictor.is_keras_model: - update_ops.extend(self._box_predictor.get_updates_for(None)) - update_ops.extend(self._box_predictor.get_updates_for( - self._box_predictor.inputs)) - if self._feature_extractor.is_keras_model: - update_ops.extend(self._feature_extractor.get_updates_for(None)) - update_ops.extend(self._feature_extractor.get_updates_for( - self._feature_extractor.inputs)) - return update_ops diff --git a/research/object_detection/meta_architectures/ssd_meta_arch_test.py b/research/object_detection/meta_architectures/ssd_meta_arch_test.py deleted file mode 100644 index 9ad41651346..00000000000 --- a/research/object_detection/meta_architectures/ssd_meta_arch_test.py +++ /dev/null @@ -1,711 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.meta_architectures.ssd_meta_arch.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl.testing import parameterized - -import numpy as np -import six -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.meta_architectures import ssd_meta_arch_test_lib -from object_detection.protos import model_pb2 -from object_detection.utils import test_utils - -# pylint: disable=g-import-not-at-top -try: - import tf_slim as slim -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - -keras = tf.keras.layers - - -class SsdMetaArchTest(ssd_meta_arch_test_lib.SSDMetaArchTestBase, - parameterized.TestCase): - - def _create_model( - self, - apply_hard_mining=True, - normalize_loc_loss_by_codesize=False, - add_background_class=True, - random_example_sampling=False, - expected_loss_weights=model_pb2.DetectionModel().ssd.loss.NONE, - min_num_negative_samples=1, - desired_negative_sampling_ratio=3, - predict_mask=False, - use_static_shapes=False, - nms_max_size_per_class=5, - calibration_mapping_value=None, - return_raw_detections_during_predict=False): - return super(SsdMetaArchTest, self)._create_model( - model_fn=ssd_meta_arch.SSDMetaArch, - apply_hard_mining=apply_hard_mining, - normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize, - add_background_class=add_background_class, - random_example_sampling=random_example_sampling, - expected_loss_weights=expected_loss_weights, - min_num_negative_samples=min_num_negative_samples, - desired_negative_sampling_ratio=desired_negative_sampling_ratio, - predict_mask=predict_mask, - use_static_shapes=use_static_shapes, - nms_max_size_per_class=nms_max_size_per_class, - calibration_mapping_value=calibration_mapping_value, - return_raw_detections_during_predict=( - return_raw_detections_during_predict)) - - def test_preprocess_preserves_shapes_with_dynamic_input_image(self): - width = tf.random.uniform([], minval=5, maxval=10, dtype=tf.int32) - batch = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - shape = tf.stack([batch, 5, width, 3]) - image = tf.random.uniform(shape) - model, _, _, _ = self._create_model() - preprocessed_inputs, _ = model.preprocess(image) - self.assertTrue( - preprocessed_inputs.shape.is_compatible_with([None, 5, None, 3])) - - def test_preprocess_preserves_shape_with_static_input_image(self): - image = tf.random.uniform([2, 3, 3, 3]) - model, _, _, _ = self._create_model() - preprocessed_inputs, _ = model.preprocess(image) - self.assertTrue(preprocessed_inputs.shape.is_compatible_with([2, 3, 3, 3])) - - def test_predict_result_shapes_on_image_with_dynamic_shape(self): - with test_utils.GraphContextOrNone() as g: - model, num_classes, num_anchors, code_size = self._create_model() - - def graph_fn(): - size = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - batch = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - shape = tf.stack([batch, size, size, 3]) - image = tf.random.uniform(shape) - prediction_dict = model.predict(image, true_image_shapes=None) - self.assertIn('box_encodings', prediction_dict) - self.assertIn('class_predictions_with_background', prediction_dict) - self.assertIn('feature_maps', prediction_dict) - self.assertIn('anchors', prediction_dict) - self.assertIn('final_anchors', prediction_dict) - return (prediction_dict['box_encodings'], - prediction_dict['final_anchors'], - prediction_dict['class_predictions_with_background'], - tf.constant(num_anchors), batch) - (box_encodings_out, final_anchors, class_predictions_with_background, - num_anchors, batch_size) = self.execute_cpu(graph_fn, [], graph=g) - self.assertAllEqual(box_encodings_out.shape, - (batch_size, num_anchors, code_size)) - self.assertAllEqual(final_anchors.shape, - (batch_size, num_anchors, code_size)) - self.assertAllEqual( - class_predictions_with_background.shape, - (batch_size, num_anchors, num_classes + 1)) - - def test_predict_result_shapes_on_image_with_static_shape(self): - - with test_utils.GraphContextOrNone() as g: - model, num_classes, num_anchors, code_size = self._create_model() - - def graph_fn(input_image): - predictions = model.predict(input_image, true_image_shapes=None) - return (predictions['box_encodings'], - predictions['class_predictions_with_background'], - predictions['final_anchors']) - batch_size = 3 - image_size = 2 - channels = 3 - input_image = np.random.rand(batch_size, image_size, image_size, - channels).astype(np.float32) - expected_box_encodings_shape = (batch_size, num_anchors, code_size) - expected_class_predictions_shape = (batch_size, num_anchors, num_classes+1) - final_anchors_shape = (batch_size, num_anchors, 4) - (box_encodings, class_predictions, final_anchors) = self.execute( - graph_fn, [input_image], graph=g) - self.assertAllEqual(box_encodings.shape, expected_box_encodings_shape) - self.assertAllEqual(class_predictions.shape, - expected_class_predictions_shape) - self.assertAllEqual(final_anchors.shape, final_anchors_shape) - - def test_predict_with_raw_output_fields(self): - with test_utils.GraphContextOrNone() as g: - model, num_classes, num_anchors, code_size = self._create_model( - return_raw_detections_during_predict=True) - - def graph_fn(input_image): - predictions = model.predict(input_image, true_image_shapes=None) - return (predictions['box_encodings'], - predictions['class_predictions_with_background'], - predictions['final_anchors'], - predictions['raw_detection_boxes'], - predictions['raw_detection_feature_map_indices']) - batch_size = 3 - image_size = 2 - channels = 3 - input_image = np.random.rand(batch_size, image_size, image_size, - channels).astype(np.float32) - expected_box_encodings_shape = (batch_size, num_anchors, code_size) - expected_class_predictions_shape = (batch_size, num_anchors, num_classes+1) - final_anchors_shape = (batch_size, num_anchors, 4) - expected_raw_detection_boxes_shape = (batch_size, num_anchors, 4) - (box_encodings, class_predictions, final_anchors, raw_detection_boxes, - raw_detection_feature_map_indices) = self.execute( - graph_fn, [input_image], graph=g) - self.assertAllEqual(box_encodings.shape, expected_box_encodings_shape) - self.assertAllEqual(class_predictions.shape, - expected_class_predictions_shape) - self.assertAllEqual(final_anchors.shape, final_anchors_shape) - self.assertAllEqual(raw_detection_boxes.shape, - expected_raw_detection_boxes_shape) - self.assertAllEqual(raw_detection_feature_map_indices, - np.zeros((batch_size, num_anchors))) - - def test_raw_detection_boxes_agree_predict_postprocess(self): - with test_utils.GraphContextOrNone() as g: - model, _, _, _ = self._create_model( - return_raw_detections_during_predict=True) - def graph_fn(): - size = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - batch = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - shape = tf.stack([batch, size, size, 3]) - image = tf.random.uniform(shape) - preprocessed_inputs, true_image_shapes = model.preprocess( - image) - prediction_dict = model.predict(preprocessed_inputs, - true_image_shapes) - raw_detection_boxes_predict = prediction_dict['raw_detection_boxes'] - detections = model.postprocess(prediction_dict, true_image_shapes) - raw_detection_boxes_postprocess = detections['raw_detection_boxes'] - return raw_detection_boxes_predict, raw_detection_boxes_postprocess - (raw_detection_boxes_predict_out, - raw_detection_boxes_postprocess_out) = self.execute_cpu(graph_fn, [], - graph=g) - self.assertAllEqual(raw_detection_boxes_predict_out, - raw_detection_boxes_postprocess_out) - - def test_postprocess_results_are_correct(self): - - with test_utils.GraphContextOrNone() as g: - model, _, _, _ = self._create_model() - - def graph_fn(): - size = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - batch = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - shape = tf.stack([batch, size, size, 3]) - image = tf.random.uniform(shape) - preprocessed_inputs, true_image_shapes = model.preprocess( - image) - prediction_dict = model.predict(preprocessed_inputs, - true_image_shapes) - detections = model.postprocess(prediction_dict, true_image_shapes) - return [ - batch, detections['detection_boxes'], detections['detection_scores'], - detections['detection_classes'], - detections['detection_multiclass_scores'], - detections['num_detections'], detections['raw_detection_boxes'], - detections['raw_detection_scores'], - detections['detection_anchor_indices'] - ] - - expected_boxes = [ - [ - [0, 0, .5, .5], - [0, .5, .5, 1], - [.5, 0, 1, .5], - [0, 0, 0, 0], # pruned prediction - [0, 0, 0, 0] - ], # padding - [ - [0, 0, .5, .5], - [0, .5, .5, 1], - [.5, 0, 1, .5], - [0, 0, 0, 0], # pruned prediction - [0, 0, 0, 0] - ] - ] # padding - expected_scores = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] - expected_multiclass_scores = [[[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]], - [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]] - - expected_classes = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] - expected_num_detections = np.array([3, 3]) - - expected_raw_detection_boxes = [[[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.], - [0.5, 0., 1., 0.5], [1., 1., 1.5, 1.5]], - [[0., 0., 0.5, 0.5], [0., 0.5, 0.5, 1.], - [0.5, 0., 1., 0.5], [1., 1., 1.5, 1.5]]] - expected_raw_detection_scores = [[[0, 0], [0, 0], [0, 0], [0, 0]], - [[0, 0], [0, 0], [0, 0], [0, 0]]] - expected_detection_anchor_indices = [[0, 1, 2], [0, 1, 2]] - (batch, detection_boxes, detection_scores, detection_classes, - detection_multiclass_scores, num_detections, raw_detection_boxes, - raw_detection_scores, detection_anchor_indices) = self.execute_cpu( - graph_fn, [], graph=g) - for image_idx in range(batch): - self.assertTrue( - test_utils.first_rows_close_as_set( - detection_boxes[image_idx].tolist(), expected_boxes[image_idx])) - self.assertSameElements(detection_anchor_indices[image_idx], - expected_detection_anchor_indices[image_idx]) - self.assertAllClose(detection_scores, expected_scores) - self.assertAllClose(detection_classes, expected_classes) - self.assertAllClose(detection_multiclass_scores, expected_multiclass_scores) - self.assertAllClose(num_detections, expected_num_detections) - self.assertAllEqual(raw_detection_boxes, expected_raw_detection_boxes) - self.assertAllEqual(raw_detection_scores, - expected_raw_detection_scores) - - def test_postprocess_results_are_correct_static(self): - with test_utils.GraphContextOrNone() as g: - model, _, _, _ = self._create_model(use_static_shapes=True, - nms_max_size_per_class=4) - - def graph_fn(input_image): - preprocessed_inputs, true_image_shapes = model.preprocess(input_image) - prediction_dict = model.predict(preprocessed_inputs, - true_image_shapes) - detections = model.postprocess(prediction_dict, true_image_shapes) - return (detections['detection_boxes'], detections['detection_scores'], - detections['detection_classes'], detections['num_detections'], - detections['detection_multiclass_scores']) - - expected_boxes = [ - [ - [0, 0, .5, .5], - [0, .5, .5, 1], - [.5, 0, 1, .5], - [0, 0, 0, 0] - ], # padding - [ - [0, 0, .5, .5], - [0, .5, .5, 1], - [.5, 0, 1, .5], - [0, 0, 0, 0] - ] - ] # padding - expected_scores = [[0, 0, 0, 0], [0, 0, 0, 0]] - expected_multiclass_scores = [[[0, 0], [0, 0], [0, 0], [0, 0]], - [[0, 0], [0, 0], [0, 0], [0, 0]]] - expected_classes = [[0, 0, 0, 0], [0, 0, 0, 0]] - expected_num_detections = np.array([3, 3]) - batch_size = 2 - image_size = 2 - channels = 3 - input_image = np.random.rand(batch_size, image_size, image_size, - channels).astype(np.float32) - (detection_boxes, detection_scores, detection_classes, - num_detections, detection_multiclass_scores) = self.execute(graph_fn, - [input_image], - graph=g) - for image_idx in range(batch_size): - self.assertTrue(test_utils.first_rows_close_as_set( - detection_boxes[image_idx][ - 0:expected_num_detections[image_idx]].tolist(), - expected_boxes[image_idx][0:expected_num_detections[image_idx]])) - self.assertAllClose( - detection_scores[image_idx][0:expected_num_detections[image_idx]], - expected_scores[image_idx][0:expected_num_detections[image_idx]]) - self.assertAllClose( - detection_multiclass_scores[image_idx] - [0:expected_num_detections[image_idx]], - expected_multiclass_scores[image_idx] - [0:expected_num_detections[image_idx]]) - self.assertAllClose( - detection_classes[image_idx][0:expected_num_detections[image_idx]], - expected_classes[image_idx][0:expected_num_detections[image_idx]]) - self.assertAllClose(num_detections, - expected_num_detections) - - def test_postprocess_results_are_correct_with_calibration(self): - with test_utils.GraphContextOrNone() as g: - model, _, _, _ = self._create_model(calibration_mapping_value=0.5) - - def graph_fn(): - size = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - batch = tf.random.uniform([], minval=2, maxval=3, dtype=tf.int32) - shape = tf.stack([batch, size, size, 3]) - image = tf.random.uniform(shape) - preprocessed_inputs, true_image_shapes = model.preprocess( - image) - prediction_dict = model.predict(preprocessed_inputs, - true_image_shapes) - detections = model.postprocess(prediction_dict, true_image_shapes) - return detections['detection_scores'], detections['raw_detection_scores'] - # Calibration mapping value below is set to map all scores to 0.5, except - # for the last two detections in each batch (see expected number of - # detections below. - expected_scores = [[0.5, 0.5, 0.5, 0., 0.], [0.5, 0.5, 0.5, 0., 0.]] - expected_raw_detection_scores = [ - [[0.5, 0.5], [0.5, 0.5], [0.5, 0.5], [0.5, 0.5]], - [[0.5, 0.5], [0.5, 0.5], [0.5, 0.5], [0.5, 0.5]] - ] - detection_scores, raw_detection_scores = self.execute_cpu(graph_fn, [], - graph=g) - self.assertAllClose(detection_scores, expected_scores) - self.assertAllEqual(raw_detection_scores, expected_raw_detection_scores) - - def test_loss_results_are_correct(self): - - with test_utils.GraphContextOrNone() as g: - model, num_classes, num_anchors, _ = self._create_model( - apply_hard_mining=False) - def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2): - groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] - groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list) - prediction_dict = model.predict(preprocessed_tensor, - true_image_shapes=None) - loss_dict = model.loss(prediction_dict, true_image_shapes=None) - return (self._get_value_for_matching_key(loss_dict, - 'Loss/localization_loss'), - self._get_value_for_matching_key(loss_dict, - 'Loss/classification_loss')) - batch_size = 2 - preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) - groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_classes1 = np.array([[1]], dtype=np.float32) - groundtruth_classes2 = np.array([[1]], dtype=np.float32) - (localization_loss, classification_loss) = self.execute( - graph_fn, [ - preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2 - ], - graph=g) - - expected_localization_loss = 0.0 - expected_classification_loss = (batch_size * num_anchors - * (num_classes+1) * np.log(2.0)) - - self.assertAllClose(localization_loss, expected_localization_loss) - self.assertAllClose(classification_loss, expected_classification_loss) - - def test_loss_results_are_correct_with_normalize_by_codesize_true(self): - with test_utils.GraphContextOrNone() as g: - model, _, _, _ = self._create_model( - apply_hard_mining=False, normalize_loc_loss_by_codesize=True) - - def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2): - groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] - groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list) - prediction_dict = model.predict(preprocessed_tensor, - true_image_shapes=None) - loss_dict = model.loss(prediction_dict, true_image_shapes=None) - return (self._get_value_for_matching_key(loss_dict, - 'Loss/localization_loss'),) - - batch_size = 2 - preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) - groundtruth_boxes1 = np.array([[0, 0, 1, 1]], dtype=np.float32) - groundtruth_boxes2 = np.array([[0, 0, 1, 1]], dtype=np.float32) - groundtruth_classes1 = np.array([[1]], dtype=np.float32) - groundtruth_classes2 = np.array([[1]], dtype=np.float32) - expected_localization_loss = 0.5 / 4 - localization_loss = self.execute(graph_fn, [preprocessed_input, - groundtruth_boxes1, - groundtruth_boxes2, - groundtruth_classes1, - groundtruth_classes2], graph=g) - self.assertAllClose(localization_loss, expected_localization_loss) - - def test_loss_results_are_correct_with_hard_example_mining(self): - with test_utils.GraphContextOrNone() as g: - model, num_classes, num_anchors, _ = self._create_model() - def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2): - groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] - groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list) - prediction_dict = model.predict(preprocessed_tensor, - true_image_shapes=None) - loss_dict = model.loss(prediction_dict, true_image_shapes=None) - return (self._get_value_for_matching_key(loss_dict, - 'Loss/localization_loss'), - self._get_value_for_matching_key(loss_dict, - 'Loss/classification_loss')) - - batch_size = 2 - preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) - groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_classes1 = np.array([[1]], dtype=np.float32) - groundtruth_classes2 = np.array([[1]], dtype=np.float32) - expected_localization_loss = 0.0 - expected_classification_loss = (batch_size * num_anchors - * (num_classes+1) * np.log(2.0)) - (localization_loss, classification_loss) = self.execute_cpu( - graph_fn, [ - preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2 - ], graph=g) - self.assertAllClose(localization_loss, expected_localization_loss) - self.assertAllClose(classification_loss, expected_classification_loss) - - def test_loss_results_are_correct_without_add_background_class(self): - - with test_utils.GraphContextOrNone() as g: - model, num_classes, num_anchors, _ = self._create_model( - apply_hard_mining=False, add_background_class=False) - - def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2): - groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] - groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list) - prediction_dict = model.predict( - preprocessed_tensor, true_image_shapes=None) - loss_dict = model.loss(prediction_dict, true_image_shapes=None) - return (loss_dict['Loss/localization_loss'], - loss_dict['Loss/classification_loss']) - - batch_size = 2 - preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) - groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_classes1 = np.array([[1]], dtype=np.float32) - groundtruth_classes2 = np.array([[1]], dtype=np.float32) - expected_localization_loss = 0.0 - expected_classification_loss = ( - batch_size * num_anchors * num_classes * np.log(2.0)) - (localization_loss, classification_loss) = self.execute( - graph_fn, [ - preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2 - ], graph=g) - - self.assertAllClose(localization_loss, expected_localization_loss) - self.assertAllClose(classification_loss, expected_classification_loss) - - - def test_loss_results_are_correct_with_losses_mask(self): - with test_utils.GraphContextOrNone() as g: - model, num_classes, num_anchors, _ = self._create_model( - apply_hard_mining=False) - def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_boxes3, groundtruth_classes1, groundtruth_classes2, - groundtruth_classes3): - groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2, - groundtruth_boxes3] - groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2, - groundtruth_classes3] - is_annotated_list = [tf.constant(True), tf.constant(True), - tf.constant(False)] - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list, - is_annotated_list=is_annotated_list) - prediction_dict = model.predict(preprocessed_tensor, - true_image_shapes=None) - loss_dict = model.loss(prediction_dict, true_image_shapes=None) - return (self._get_value_for_matching_key(loss_dict, - 'Loss/localization_loss'), - self._get_value_for_matching_key(loss_dict, - 'Loss/classification_loss')) - - batch_size = 3 - preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) - groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_boxes3 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_classes1 = np.array([[1]], dtype=np.float32) - groundtruth_classes2 = np.array([[1]], dtype=np.float32) - groundtruth_classes3 = np.array([[1]], dtype=np.float32) - expected_localization_loss = 0.0 - # Note that we are subtracting 1 from batch_size, since the final image is - # not annotated. - expected_classification_loss = ((batch_size - 1) * num_anchors - * (num_classes+1) * np.log(2.0)) - (localization_loss, - classification_loss) = self.execute(graph_fn, [preprocessed_input, - groundtruth_boxes1, - groundtruth_boxes2, - groundtruth_boxes3, - groundtruth_classes1, - groundtruth_classes2, - groundtruth_classes3], - graph=g) - self.assertAllClose(localization_loss, expected_localization_loss) - self.assertAllClose(classification_loss, expected_classification_loss) - - def test_restore_map_for_detection_ckpt(self): - # TODO(rathodv): Support TF2.X - if self.is_tf2(): return - model, _, _, _ = self._create_model() - model.predict(tf.constant(np.array([[[[0, 0], [1, 1]], [[1, 0], [0, 1]]]], - dtype=np.float32)), - true_image_shapes=None) - init_op = tf.global_variables_initializer() - saver = tf.train.Saver() - save_path = self.get_temp_dir() - with self.session() as sess: - sess.run(init_op) - saved_model_path = saver.save(sess, save_path) - var_map = model.restore_map( - fine_tune_checkpoint_type='detection', - load_all_detection_checkpoint_vars=False) - self.assertIsInstance(var_map, dict) - saver = tf.train.Saver(var_map) - saver.restore(sess, saved_model_path) - for var in sess.run(tf.report_uninitialized_variables()): - self.assertNotIn('FeatureExtractor', var) - - def test_restore_map_for_classification_ckpt(self): - # TODO(rathodv): Support TF2.X - if self.is_tf2(): return - # Define mock tensorflow classification graph and save variables. - test_graph_classification = tf.Graph() - with test_graph_classification.as_default(): - image = tf.placeholder(dtype=tf.float32, shape=[1, 20, 20, 3]) - - with tf.variable_scope('mock_model'): - net = slim.conv2d(image, num_outputs=32, kernel_size=1, scope='layer1') - slim.conv2d(net, num_outputs=3, kernel_size=1, scope='layer2') - - init_op = tf.global_variables_initializer() - saver = tf.train.Saver() - save_path = self.get_temp_dir() - with self.session(graph=test_graph_classification) as sess: - sess.run(init_op) - saved_model_path = saver.save(sess, save_path) - - # Create tensorflow detection graph and load variables from - # classification checkpoint. - test_graph_detection = tf.Graph() - with test_graph_detection.as_default(): - model, _, _, _ = self._create_model() - inputs_shape = [2, 2, 2, 3] - inputs = tf.cast(tf.random_uniform( - inputs_shape, minval=0, maxval=255, dtype=tf.int32), dtype=tf.float32) - preprocessed_inputs, true_image_shapes = model.preprocess(inputs) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - model.postprocess(prediction_dict, true_image_shapes) - another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable - var_map = model.restore_map(fine_tune_checkpoint_type='classification') - self.assertNotIn('another_variable', var_map) - self.assertIsInstance(var_map, dict) - saver = tf.train.Saver(var_map) - with self.session(graph=test_graph_detection) as sess: - saver.restore(sess, saved_model_path) - for var in sess.run(tf.report_uninitialized_variables()): - self.assertNotIn(six.ensure_binary('FeatureExtractor'), var) - - def test_load_all_det_checkpoint_vars(self): - if self.is_tf2(): return - test_graph_detection = tf.Graph() - with test_graph_detection.as_default(): - model, _, _, _ = self._create_model() - inputs_shape = [2, 2, 2, 3] - inputs = tf.cast( - tf.random_uniform(inputs_shape, minval=0, maxval=255, dtype=tf.int32), - dtype=tf.float32) - preprocessed_inputs, true_image_shapes = model.preprocess(inputs) - prediction_dict = model.predict(preprocessed_inputs, true_image_shapes) - model.postprocess(prediction_dict, true_image_shapes) - another_variable = tf.Variable([17.0], name='another_variable') # pylint: disable=unused-variable - var_map = model.restore_map( - fine_tune_checkpoint_type='detection', - load_all_detection_checkpoint_vars=True) - self.assertIsInstance(var_map, dict) - self.assertIn('another_variable', var_map) - - def test_load_checkpoint_vars_tf2(self): - - if not self.is_tf2(): - self.skipTest('Not running TF2 checkpoint test with TF1.') - - model, _, _, _ = self._create_model() - inputs_shape = [2, 2, 2, 3] - inputs = tf.cast( - tf.random_uniform(inputs_shape, minval=0, maxval=255, dtype=tf.int32), - dtype=tf.float32) - model(inputs) - - detection_var_names = sorted([ - var.name for var in model.restore_from_objects('detection')[ - 'model']._feature_extractor.weights - ]) - expected_detection_names = [ - 'ssd_meta_arch/fake_ssd_keras_feature_extractor/mock_model/layer1/bias:0', - 'ssd_meta_arch/fake_ssd_keras_feature_extractor/mock_model/layer1/kernel:0' - ] - self.assertEqual(detection_var_names, expected_detection_names) - - full_var_names = sorted([ - var.name for var in - model.restore_from_objects('full')['model'].weights - ]) - - exepcted_full_names = ['box_predictor_var:0'] + expected_detection_names - self.assertEqual(exepcted_full_names, full_var_names) - # TODO(vighneshb) Add similar test for classification checkpoint type. - # TODO(vighneshb) Test loading a checkpoint from disk to verify that - # checkpoints are loaded correctly. - - def test_loss_results_are_correct_with_random_example_sampling(self): - with test_utils.GraphContextOrNone() as g: - model, num_classes, _, _ = self._create_model( - random_example_sampling=True) - - def graph_fn(preprocessed_tensor, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2): - groundtruth_boxes_list = [groundtruth_boxes1, groundtruth_boxes2] - groundtruth_classes_list = [groundtruth_classes1, groundtruth_classes2] - model.provide_groundtruth(groundtruth_boxes_list, - groundtruth_classes_list) - prediction_dict = model.predict( - preprocessed_tensor, true_image_shapes=None) - loss_dict = model.loss(prediction_dict, true_image_shapes=None) - return (self._get_value_for_matching_key(loss_dict, - 'Loss/localization_loss'), - self._get_value_for_matching_key(loss_dict, - 'Loss/classification_loss')) - - batch_size = 2 - preprocessed_input = np.random.rand(batch_size, 2, 2, 3).astype(np.float32) - groundtruth_boxes1 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_boxes2 = np.array([[0, 0, .5, .5]], dtype=np.float32) - groundtruth_classes1 = np.array([[1]], dtype=np.float32) - groundtruth_classes2 = np.array([[1]], dtype=np.float32) - expected_localization_loss = 0.0 - # Among 4 anchors (1 positive, 3 negative) in this test, only 2 anchors are - # selected (1 positive, 1 negative) since random sampler will adjust number - # of negative examples to make sure positive example fraction in the batch - # is 0.5. - expected_classification_loss = ( - batch_size * 2 * (num_classes + 1) * np.log(2.0)) - (localization_loss, classification_loss) = self.execute_cpu( - graph_fn, [ - preprocessed_input, groundtruth_boxes1, groundtruth_boxes2, - groundtruth_classes1, groundtruth_classes2 - ], graph=g) - self.assertAllClose(localization_loss, expected_localization_loss) - self.assertAllClose(classification_loss, expected_classification_loss) - - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/meta_architectures/ssd_meta_arch_test_lib.py b/research/object_detection/meta_architectures/ssd_meta_arch_test_lib.py deleted file mode 100644 index 0991388b31a..00000000000 --- a/research/object_detection/meta_architectures/ssd_meta_arch_test_lib.py +++ /dev/null @@ -1,259 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Helper functions for SSD models meta architecture tests.""" - -import functools -import tensorflow.compat.v1 as tf -from google.protobuf import text_format - -from object_detection.builders import post_processing_builder -from object_detection.core import anchor_generator -from object_detection.core import balanced_positive_negative_sampler as sampler -from object_detection.core import box_list -from object_detection.core import losses -from object_detection.core import post_processing -from object_detection.core import region_similarity_calculator as sim_calc -from object_detection.core import target_assigner -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.protos import calibration_pb2 -from object_detection.protos import model_pb2 -from object_detection.utils import ops -from object_detection.utils import test_case -from object_detection.utils import test_utils -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top -try: - import tf_slim as slim -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - -keras = tf.keras.layers - - -class FakeSSDFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """Fake ssd feature extracture for ssd meta arch tests.""" - - def __init__(self): - super(FakeSSDFeatureExtractor, self).__init__( - is_training=True, - depth_multiplier=0, - min_depth=0, - pad_to_multiple=1, - conv_hyperparams_fn=None) - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def extract_features(self, preprocessed_inputs): - with tf.variable_scope('mock_model'): - features = slim.conv2d( - inputs=preprocessed_inputs, - num_outputs=32, - kernel_size=1, - scope='layer1') - return [features] - - -class FakeSSDKerasFeatureExtractor(ssd_meta_arch.SSDKerasFeatureExtractor): - """Fake keras based ssd feature extracture for ssd meta arch tests.""" - - def __init__(self): - with tf.name_scope('mock_model'): - super(FakeSSDKerasFeatureExtractor, self).__init__( - is_training=True, - depth_multiplier=0, - min_depth=0, - pad_to_multiple=1, - conv_hyperparams=None, - freeze_batchnorm=False, - inplace_batchnorm_update=False, - ) - - self._conv = keras.Conv2D(filters=32, kernel_size=1, name='layer1') - - def preprocess(self, resized_inputs): - return tf.identity(resized_inputs) - - def _extract_features(self, preprocessed_inputs, **kwargs): - with tf.name_scope('mock_model'): - return [self._conv(preprocessed_inputs)] - - -class MockAnchorGenerator2x2(anchor_generator.AnchorGenerator): - """A simple 2x2 anchor grid on the unit square used for test only.""" - - def name_scope(self): - return 'MockAnchorGenerator' - - def num_anchors_per_location(self): - return [1] - - def _generate(self, feature_map_shape_list, im_height, im_width): - return [ - box_list.BoxList( - tf.constant( - [ - [0, 0, .5, .5], - [0, .5, .5, 1], - [.5, 0, 1, .5], - [1., 1., 1.5, 1.5] # Anchor that is outside clip_window. - ], - tf.float32)) - ] - - def num_anchors(self): - return 4 - - -class SSDMetaArchTestBase(test_case.TestCase): - """Base class to test SSD based meta architectures.""" - - def _create_model( - self, - model_fn=ssd_meta_arch.SSDMetaArch, - apply_hard_mining=True, - normalize_loc_loss_by_codesize=False, - add_background_class=True, - random_example_sampling=False, - expected_loss_weights=model_pb2.DetectionModel().ssd.loss.NONE, - min_num_negative_samples=1, - desired_negative_sampling_ratio=3, - predict_mask=False, - use_static_shapes=False, - nms_max_size_per_class=5, - calibration_mapping_value=None, - return_raw_detections_during_predict=False): - is_training = False - num_classes = 1 - mock_anchor_generator = MockAnchorGenerator2x2() - use_keras = tf_version.is_tf2() - if use_keras: - mock_box_predictor = test_utils.MockKerasBoxPredictor( - is_training, num_classes, add_background_class=add_background_class) - else: - mock_box_predictor = test_utils.MockBoxPredictor( - is_training, num_classes, add_background_class=add_background_class) - mock_box_coder = test_utils.MockBoxCoder() - if use_keras: - fake_feature_extractor = FakeSSDKerasFeatureExtractor() - else: - fake_feature_extractor = FakeSSDFeatureExtractor() - mock_matcher = test_utils.MockMatcher() - region_similarity_calculator = sim_calc.IouSimilarity() - encode_background_as_zeros = False - - def image_resizer_fn(image): - return [tf.identity(image), tf.shape(image)] - - classification_loss = losses.WeightedSigmoidClassificationLoss() - localization_loss = losses.WeightedSmoothL1LocalizationLoss() - non_max_suppression_fn = functools.partial( - post_processing.batch_multiclass_non_max_suppression, - score_thresh=-20.0, - iou_thresh=1.0, - max_size_per_class=nms_max_size_per_class, - max_total_size=nms_max_size_per_class, - use_static_shapes=use_static_shapes) - score_conversion_fn = tf.identity - calibration_config = calibration_pb2.CalibrationConfig() - if calibration_mapping_value: - calibration_text_proto = """ - function_approximation { - x_y_pairs { - x_y_pair { - x: 0.0 - y: %f - } - x_y_pair { - x: 1.0 - y: %f - }}}""" % (calibration_mapping_value, calibration_mapping_value) - text_format.Merge(calibration_text_proto, calibration_config) - score_conversion_fn = ( - post_processing_builder._build_calibrated_score_converter( # pylint: disable=protected-access - tf.identity, calibration_config)) - classification_loss_weight = 1.0 - localization_loss_weight = 1.0 - negative_class_weight = 1.0 - normalize_loss_by_num_matches = False - - hard_example_miner = None - if apply_hard_mining: - # This hard example miner is expected to be a no-op. - hard_example_miner = losses.HardExampleMiner( - num_hard_examples=None, iou_threshold=1.0) - - random_example_sampler = None - if random_example_sampling: - random_example_sampler = sampler.BalancedPositiveNegativeSampler( - positive_fraction=0.5) - - target_assigner_instance = target_assigner.TargetAssigner( - region_similarity_calculator, - mock_matcher, - mock_box_coder, - negative_class_weight=negative_class_weight) - - model_config = model_pb2.DetectionModel() - if expected_loss_weights == model_config.ssd.loss.NONE: - expected_loss_weights_fn = None - else: - raise ValueError('Not a valid value for expected_loss_weights.') - - code_size = 4 - - kwargs = {} - if predict_mask: - kwargs.update({ - 'mask_prediction_fn': test_utils.MockMaskHead(num_classes=1).predict, - }) - - model = model_fn( - is_training=is_training, - anchor_generator=mock_anchor_generator, - box_predictor=mock_box_predictor, - box_coder=mock_box_coder, - feature_extractor=fake_feature_extractor, - encode_background_as_zeros=encode_background_as_zeros, - image_resizer_fn=image_resizer_fn, - non_max_suppression_fn=non_max_suppression_fn, - score_conversion_fn=score_conversion_fn, - classification_loss=classification_loss, - localization_loss=localization_loss, - classification_loss_weight=classification_loss_weight, - localization_loss_weight=localization_loss_weight, - normalize_loss_by_num_matches=normalize_loss_by_num_matches, - hard_example_miner=hard_example_miner, - target_assigner_instance=target_assigner_instance, - add_summaries=False, - normalize_loc_loss_by_codesize=normalize_loc_loss_by_codesize, - freeze_batchnorm=False, - inplace_batchnorm_update=False, - add_background_class=add_background_class, - random_example_sampler=random_example_sampler, - expected_loss_weights_fn=expected_loss_weights_fn, - return_raw_detections_during_predict=( - return_raw_detections_during_predict), - **kwargs) - return model, num_classes, mock_anchor_generator.num_anchors(), code_size - - def _get_value_for_matching_key(self, dictionary, suffix): - for key in dictionary.keys(): - if key.endswith(suffix): - return dictionary[key] - raise ValueError('key not found {}'.format(suffix)) diff --git a/research/object_detection/metrics/__init__.py b/research/object_detection/metrics/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/metrics/calibration_evaluation.py b/research/object_detection/metrics/calibration_evaluation.py deleted file mode 100644 index e3fc4b05639..00000000000 --- a/research/object_detection/metrics/calibration_evaluation.py +++ /dev/null @@ -1,228 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Class for evaluating object detections with calibration metrics.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf - -from object_detection.box_coders import mean_stddev_box_coder -from object_detection.core import box_list -from object_detection.core import region_similarity_calculator -from object_detection.core import standard_fields -from object_detection.core import target_assigner -from object_detection.matchers import argmax_matcher -from object_detection.metrics import calibration_metrics -from object_detection.utils import object_detection_evaluation - - -# TODO(zbeaver): Implement metrics per category. -class CalibrationDetectionEvaluator( - object_detection_evaluation.DetectionEvaluator): - """Class to evaluate calibration detection metrics.""" - - def __init__(self, - categories, - iou_threshold=0.5): - """Constructor. - - Args: - categories: A list of dicts, each of which has the following keys - - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name e.g., 'cat', 'dog'. - iou_threshold: Threshold above which to consider a box as matched during - evaluation. - """ - super(CalibrationDetectionEvaluator, self).__init__(categories) - - # Constructing target_assigner to match detections to groundtruth. - similarity_calc = region_similarity_calculator.IouSimilarity() - matcher = argmax_matcher.ArgMaxMatcher( - matched_threshold=iou_threshold, unmatched_threshold=iou_threshold) - box_coder = mean_stddev_box_coder.MeanStddevBoxCoder(stddev=0.1) - self._target_assigner = target_assigner.TargetAssigner( - similarity_calc, matcher, box_coder) - - def match_single_image_info(self, image_info): - """Match detections to groundtruth for a single image. - - Detections are matched to available groundtruth in the image based on the - IOU threshold from the constructor. The classes of the detections and - groundtruth matches are then compared. Detections that do not have IOU above - the required threshold or have different classes from their match are - considered negative matches. All inputs in `image_info` originate or are - inferred from the eval_dict passed to class method - `get_estimator_eval_metric_ops`. - - Args: - image_info: a tuple or list containing the following (in order): - - gt_boxes: tf.float32 tensor of groundtruth boxes. - - gt_classes: tf.int64 tensor of groundtruth classes associated with - groundtruth boxes. - - num_gt_box: scalar indicating the number of groundtruth boxes per - image. - - det_boxes: tf.float32 tensor of detection boxes. - - det_classes: tf.int64 tensor of detection classes associated with - detection boxes. - - num_det_box: scalar indicating the number of detection boxes per - image. - Returns: - is_class_matched: tf.int64 tensor identical in shape to det_boxes, - indicating whether detection boxes matched with and had the same - class as groundtruth annotations. - """ - (gt_boxes, gt_classes, num_gt_box, det_boxes, det_classes, - num_det_box) = image_info - detection_boxes = det_boxes[:num_det_box] - detection_classes = det_classes[:num_det_box] - groundtruth_boxes = gt_boxes[:num_gt_box] - groundtruth_classes = gt_classes[:num_gt_box] - det_boxlist = box_list.BoxList(detection_boxes) - gt_boxlist = box_list.BoxList(groundtruth_boxes) - - # Target assigner requires classes in one-hot format. An additional - # dimension is required since gt_classes are 1-indexed; the zero index is - # provided to all non-matches. - one_hot_depth = tf.cast(tf.add(tf.reduce_max(groundtruth_classes), 1), - dtype=tf.int32) - gt_classes_one_hot = tf.one_hot( - groundtruth_classes, one_hot_depth, dtype=tf.float32) - one_hot_cls_targets, _, _, _, _ = self._target_assigner.assign( - det_boxlist, - gt_boxlist, - gt_classes_one_hot, - unmatched_class_label=tf.zeros(shape=one_hot_depth, dtype=tf.float32)) - # Transform from one-hot back to indexes. - cls_targets = tf.argmax(one_hot_cls_targets, axis=1) - is_class_matched = tf.cast( - tf.equal(tf.cast(cls_targets, tf.int64), detection_classes), - dtype=tf.int64) - return is_class_matched - - def get_estimator_eval_metric_ops(self, eval_dict): - """Returns a dictionary of eval metric ops. - - Note that once value_op is called, the detections and groundtruth added via - update_op are cleared. - - This function can take in groundtruth and detections for a batch of images, - or for a single image. For the latter case, the batch dimension for input - tensors need not be present. - - Args: - eval_dict: A dictionary that holds tensors for evaluating object detection - performance. For single-image evaluation, this dictionary may be - produced from eval_util.result_dict_for_single_example(). If multi-image - evaluation, `eval_dict` should contain the fields - 'num_groundtruth_boxes_per_image' and 'num_det_boxes_per_image' to - properly unpad the tensors from the batch. - - Returns: - a dictionary of metric names to tuple of value_op and update_op that can - be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all - update ops must be run together and similarly all value ops must be run - together to guarantee correct behaviour. - """ - # Unpack items from the evaluation dictionary. - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - image_id = eval_dict[input_data_fields.key] - groundtruth_boxes = eval_dict[input_data_fields.groundtruth_boxes] - groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] - detection_boxes = eval_dict[detection_fields.detection_boxes] - detection_scores = eval_dict[detection_fields.detection_scores] - detection_classes = eval_dict[detection_fields.detection_classes] - num_gt_boxes_per_image = eval_dict.get( - 'num_groundtruth_boxes_per_image', None) - num_det_boxes_per_image = eval_dict.get('num_det_boxes_per_image', None) - is_annotated_batched = eval_dict.get('is_annotated', None) - - if not image_id.shape.as_list(): - # Apply a batch dimension to all tensors. - image_id = tf.expand_dims(image_id, 0) - groundtruth_boxes = tf.expand_dims(groundtruth_boxes, 0) - groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) - detection_boxes = tf.expand_dims(detection_boxes, 0) - detection_scores = tf.expand_dims(detection_scores, 0) - detection_classes = tf.expand_dims(detection_classes, 0) - - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.shape(groundtruth_boxes)[1:2] - else: - num_gt_boxes_per_image = tf.expand_dims(num_gt_boxes_per_image, 0) - - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.shape(detection_boxes)[1:2] - else: - num_det_boxes_per_image = tf.expand_dims(num_det_boxes_per_image, 0) - - if is_annotated_batched is None: - is_annotated_batched = tf.constant([True]) - else: - is_annotated_batched = tf.expand_dims(is_annotated_batched, 0) - else: - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.tile( - tf.shape(groundtruth_boxes)[1:2], - multiples=tf.shape(groundtruth_boxes)[0:1]) - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.tile( - tf.shape(detection_boxes)[1:2], - multiples=tf.shape(detection_boxes)[0:1]) - if is_annotated_batched is None: - is_annotated_batched = tf.ones_like(image_id, dtype=tf.bool) - - # Filter images based on is_annotated_batched and match detections. - image_info = [tf.boolean_mask(tensor, is_annotated_batched) for tensor in - [groundtruth_boxes, groundtruth_classes, - num_gt_boxes_per_image, detection_boxes, detection_classes, - num_det_boxes_per_image]] - is_class_matched = tf.map_fn( - self.match_single_image_info, image_info, dtype=tf.int64) - y_true = tf.squeeze(is_class_matched) - y_pred = tf.squeeze(tf.boolean_mask(detection_scores, is_annotated_batched)) - ece, update_op = calibration_metrics.expected_calibration_error( - y_true, y_pred) - return {'CalibrationError/ExpectedCalibrationError': (ece, update_op)} - - def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): - """Adds groundtruth for a single image to be used for evaluation. - - Args: - image_id: A unique string/integer identifier for the image. - groundtruth_dict: A dictionary of groundtruth numpy arrays required - for evaluations. - """ - raise NotImplementedError - - def add_single_detected_image_info(self, image_id, detections_dict): - """Adds detections for a single image to be used for evaluation. - - Args: - image_id: A unique string/integer identifier for the image. - detections_dict: A dictionary of detection numpy arrays required for - evaluation. - """ - raise NotImplementedError - - def evaluate(self): - """Evaluates detections and returns a dictionary of metrics.""" - raise NotImplementedError - - def clear(self): - """Clears the state to prepare for a fresh evaluation.""" - raise NotImplementedError diff --git a/research/object_detection/metrics/calibration_evaluation_tf1_test.py b/research/object_detection/metrics/calibration_evaluation_tf1_test.py deleted file mode 100644 index 0f3d6eb319f..00000000000 --- a/research/object_detection/metrics/calibration_evaluation_tf1_test.py +++ /dev/null @@ -1,203 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for tensorflow_models.object_detection.metrics.calibration_evaluation.""" # pylint: disable=line-too-long - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -import tensorflow.compat.v1 as tf -from object_detection.core import standard_fields -from object_detection.metrics import calibration_evaluation -from object_detection.utils import tf_version - - -def _get_categories_list(): - return [{ - 'id': 1, - 'name': 'person' - }, { - 'id': 2, - 'name': 'dog' - }, { - 'id': 3, - 'name': 'cat' - }] - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class CalibrationDetectionEvaluationTest(tf.test.TestCase): - - def _get_ece(self, ece_op, update_op): - """Return scalar expected calibration error.""" - with self.test_session() as sess: - metrics_vars = tf.get_collection(tf.GraphKeys.METRIC_VARIABLES) - sess.run(tf.variables_initializer(var_list=metrics_vars)) - _ = sess.run(update_op) - return sess.run(ece_op) - - def testGetECEWithMatchingGroundtruthAndDetections(self): - """Tests that ECE is calculated correctly when box matches exist.""" - calibration_evaluator = calibration_evaluation.CalibrationDetectionEvaluator( - _get_categories_list(), iou_threshold=0.5) - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - # All gt and detection boxes match. - base_eval_dict = { - input_data_fields.key: - tf.constant(['image_1', 'image_2', 'image_3']), - input_data_fields.groundtruth_boxes: - tf.constant([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]], - dtype=tf.float32), - detection_fields.detection_boxes: - tf.constant([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]], - dtype=tf.float32), - input_data_fields.groundtruth_classes: - tf.constant([[1], [2], [3]], dtype=tf.int64), - # Note that, in the zero ECE case, the detection class for image_2 - # should NOT match groundtruth, since the detection score is zero. - detection_fields.detection_scores: - tf.constant([[1.0], [0.0], [1.0]], dtype=tf.float32) - } - - # Zero ECE (perfectly calibrated). - zero_ece_eval_dict = base_eval_dict.copy() - zero_ece_eval_dict[detection_fields.detection_classes] = tf.constant( - [[1], [1], [3]], dtype=tf.int64) - zero_ece_op, zero_ece_update_op = ( - calibration_evaluator.get_estimator_eval_metric_ops(zero_ece_eval_dict) - ['CalibrationError/ExpectedCalibrationError']) - zero_ece = self._get_ece(zero_ece_op, zero_ece_update_op) - self.assertAlmostEqual(zero_ece, 0.0) - - # ECE of 1 (poorest calibration). - one_ece_eval_dict = base_eval_dict.copy() - one_ece_eval_dict[detection_fields.detection_classes] = tf.constant( - [[3], [2], [1]], dtype=tf.int64) - one_ece_op, one_ece_update_op = ( - calibration_evaluator.get_estimator_eval_metric_ops(one_ece_eval_dict) - ['CalibrationError/ExpectedCalibrationError']) - one_ece = self._get_ece(one_ece_op, one_ece_update_op) - self.assertAlmostEqual(one_ece, 1.0) - - def testGetECEWithUnmatchedGroundtruthAndDetections(self): - """Tests that ECE is correctly calculated when boxes are unmatched.""" - calibration_evaluator = calibration_evaluation.CalibrationDetectionEvaluator( - _get_categories_list(), iou_threshold=0.5) - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - # No gt and detection boxes match. - eval_dict = { - input_data_fields.key: - tf.constant(['image_1', 'image_2', 'image_3']), - input_data_fields.groundtruth_boxes: - tf.constant([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]], - dtype=tf.float32), - detection_fields.detection_boxes: - tf.constant([[[50., 50., 100., 100.]], - [[25., 25., 50., 50.]], - [[100., 100., 200., 200.]]], - dtype=tf.float32), - input_data_fields.groundtruth_classes: - tf.constant([[1], [2], [3]], dtype=tf.int64), - detection_fields.detection_classes: - tf.constant([[1], [1], [3]], dtype=tf.int64), - # Detection scores of zero when boxes are unmatched = ECE of zero. - detection_fields.detection_scores: - tf.constant([[0.0], [0.0], [0.0]], dtype=tf.float32) - } - - ece_op, update_op = calibration_evaluator.get_estimator_eval_metric_ops( - eval_dict)['CalibrationError/ExpectedCalibrationError'] - ece = self._get_ece(ece_op, update_op) - self.assertAlmostEqual(ece, 0.0) - - def testGetECEWithBatchedDetections(self): - """Tests that ECE is correct with multiple detections per image.""" - calibration_evaluator = calibration_evaluation.CalibrationDetectionEvaluator( - _get_categories_list(), iou_threshold=0.5) - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - # Note that image_2 has mismatched classes and detection scores but should - # still produce ECE of 0 because detection scores are also 0. - eval_dict = { - input_data_fields.key: - tf.constant(['image_1', 'image_2', 'image_3']), - input_data_fields.groundtruth_boxes: - tf.constant([[[100., 100., 200., 200.], [50., 50., 100., 100.]], - [[50., 50., 100., 100.], [100., 100., 200., 200.]], - [[25., 25., 50., 50.], [100., 100., 200., 200.]]], - dtype=tf.float32), - detection_fields.detection_boxes: - tf.constant([[[100., 100., 200., 200.], [50., 50., 100., 100.]], - [[50., 50., 100., 100.], [25., 25., 50., 50.]], - [[25., 25., 50., 50.], [100., 100., 200., 200.]]], - dtype=tf.float32), - input_data_fields.groundtruth_classes: - tf.constant([[1, 2], [2, 3], [3, 1]], dtype=tf.int64), - detection_fields.detection_classes: - tf.constant([[1, 2], [1, 1], [3, 1]], dtype=tf.int64), - detection_fields.detection_scores: - tf.constant([[1.0, 1.0], [0.0, 0.0], [1.0, 1.0]], dtype=tf.float32) - } - - ece_op, update_op = calibration_evaluator.get_estimator_eval_metric_ops( - eval_dict)['CalibrationError/ExpectedCalibrationError'] - ece = self._get_ece(ece_op, update_op) - self.assertAlmostEqual(ece, 0.0) - - def testGetECEWhenImagesFilteredByIsAnnotated(self): - """Tests that ECE is correct when detections filtered by is_annotated.""" - calibration_evaluator = calibration_evaluation.CalibrationDetectionEvaluator( - _get_categories_list(), iou_threshold=0.5) - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - # ECE will be 0 only if the third image is filtered by is_annotated. - eval_dict = { - input_data_fields.key: - tf.constant(['image_1', 'image_2', 'image_3']), - input_data_fields.groundtruth_boxes: - tf.constant([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]], - dtype=tf.float32), - detection_fields.detection_boxes: - tf.constant([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]], - dtype=tf.float32), - input_data_fields.groundtruth_classes: - tf.constant([[1], [2], [1]], dtype=tf.int64), - detection_fields.detection_classes: - tf.constant([[1], [1], [3]], dtype=tf.int64), - detection_fields.detection_scores: - tf.constant([[1.0], [0.0], [1.0]], dtype=tf.float32), - 'is_annotated': tf.constant([True, True, False], dtype=tf.bool) - } - - ece_op, update_op = calibration_evaluator.get_estimator_eval_metric_ops( - eval_dict)['CalibrationError/ExpectedCalibrationError'] - ece = self._get_ece(ece_op, update_op) - self.assertAlmostEqual(ece, 0.0) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/calibration_metrics.py b/research/object_detection/metrics/calibration_metrics.py deleted file mode 100644 index 611c81c3381..00000000000 --- a/research/object_detection/metrics/calibration_metrics.py +++ /dev/null @@ -1,118 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Object detection calibration metrics. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf -from tensorflow.python.ops import metrics_impl - - -def _safe_div(numerator, denominator): - """Divides two tensors element-wise, returning 0 if the denominator is <= 0. - - Args: - numerator: A real `Tensor`. - denominator: A real `Tensor`, with dtype matching `numerator`. - - Returns: - 0 if `denominator` <= 0, else `numerator` / `denominator` - """ - t = tf.truediv(numerator, denominator) - zero = tf.zeros_like(t, dtype=denominator.dtype) - condition = tf.greater(denominator, zero) - zero = tf.cast(zero, t.dtype) - return tf.where(condition, t, zero) - - -def _ece_from_bins(bin_counts, bin_true_sum, bin_preds_sum, name): - """Calculates Expected Calibration Error from accumulated statistics.""" - bin_accuracies = _safe_div(bin_true_sum, bin_counts) - bin_confidences = _safe_div(bin_preds_sum, bin_counts) - abs_bin_errors = tf.abs(bin_accuracies - bin_confidences) - bin_weights = _safe_div(bin_counts, tf.reduce_sum(bin_counts)) - return tf.reduce_sum(abs_bin_errors * bin_weights, name=name) - - -def expected_calibration_error(y_true, y_pred, nbins=20): - """Calculates Expected Calibration Error (ECE). - - ECE is a scalar summary statistic of calibration error. It is the - sample-weighted average of the difference between the predicted and true - probabilities of a positive detection across uniformly-spaced model - confidences [0, 1]. See referenced paper for a thorough explanation. - - Reference: - Guo, et. al, "On Calibration of Modern Neural Networks" - Page 2, Expected Calibration Error (ECE). - https://arxiv.org/pdf/1706.04599.pdf - - This function creates three local variables, `bin_counts`, `bin_true_sum`, and - `bin_preds_sum` that are used to compute ECE. For estimation of the metric - over a stream of data, the function creates an `update_op` operation that - updates these variables and returns the ECE. - - Args: - y_true: 1-D tf.int64 Tensor of binarized ground truth, corresponding to each - prediction in y_pred. - y_pred: 1-D tf.float32 tensor of model confidence scores in range - [0.0, 1.0]. - nbins: int specifying the number of uniformly-spaced bins into which y_pred - will be bucketed. - - Returns: - value_op: A value metric op that returns ece. - update_op: An operation that increments the `bin_counts`, `bin_true_sum`, - and `bin_preds_sum` variables appropriately and whose value matches `ece`. - - Raises: - InvalidArgumentError: if y_pred is not in [0.0, 1.0]. - """ - bin_counts = metrics_impl.metric_variable( - [nbins], tf.float32, name='bin_counts') - bin_true_sum = metrics_impl.metric_variable( - [nbins], tf.float32, name='true_sum') - bin_preds_sum = metrics_impl.metric_variable( - [nbins], tf.float32, name='preds_sum') - - with tf.control_dependencies([ - tf.assert_greater_equal(y_pred, 0.0), - tf.assert_less_equal(y_pred, 1.0), - ]): - bin_ids = tf.histogram_fixed_width_bins(y_pred, [0.0, 1.0], nbins=nbins) - - with tf.control_dependencies([bin_ids]): - update_bin_counts_op = tf.assign_add( - bin_counts, tf.cast(tf.bincount(bin_ids, minlength=nbins), - dtype=tf.float32)) - update_bin_true_sum_op = tf.assign_add( - bin_true_sum, - tf.cast(tf.bincount(bin_ids, weights=y_true, minlength=nbins), - dtype=tf.float32)) - update_bin_preds_sum_op = tf.assign_add( - bin_preds_sum, - tf.cast(tf.bincount(bin_ids, weights=y_pred, minlength=nbins), - dtype=tf.float32)) - - ece_update_op = _ece_from_bins( - update_bin_counts_op, - update_bin_true_sum_op, - update_bin_preds_sum_op, - name='update_op') - ece = _ece_from_bins(bin_counts, bin_true_sum, bin_preds_sum, name='value') - return ece, ece_update_op diff --git a/research/object_detection/metrics/calibration_metrics_tf1_test.py b/research/object_detection/metrics/calibration_metrics_tf1_test.py deleted file mode 100644 index 9c1adbca20d..00000000000 --- a/research/object_detection/metrics/calibration_metrics_tf1_test.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for calibration_metrics.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -import numpy as np -import tensorflow.compat.v1 as tf -from object_detection.metrics import calibration_metrics -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class CalibrationLibTest(tf.test.TestCase): - - @staticmethod - def _get_calibration_placeholders(): - """Returns TF placeholders for y_true and y_pred.""" - return (tf.placeholder(tf.int64, shape=(None)), - tf.placeholder(tf.float32, shape=(None))) - - def test_expected_calibration_error_all_bins_filled(self): - """Test expected calibration error when all bins contain predictions.""" - y_true, y_pred = self._get_calibration_placeholders() - expected_ece_op, update_op = calibration_metrics.expected_calibration_error( - y_true, y_pred, nbins=2) - with self.test_session() as sess: - metrics_vars = tf.get_collection(tf.GraphKeys.METRIC_VARIABLES) - sess.run(tf.variables_initializer(var_list=metrics_vars)) - # Bin calibration errors (|confidence - accuracy| * bin_weight): - # - [0,0.5): |0.2 - 0.333| * (3/5) = 0.08 - # - [0.5, 1]: |0.75 - 0.5| * (2/5) = 0.1 - sess.run( - update_op, - feed_dict={ - y_pred: np.array([0., 0.2, 0.4, 0.5, 1.0]), - y_true: np.array([0, 0, 1, 0, 1]) - }) - actual_ece = 0.08 + 0.1 - expected_ece = sess.run(expected_ece_op) - self.assertAlmostEqual(actual_ece, expected_ece) - - def test_expected_calibration_error_all_bins_not_filled(self): - """Test expected calibration error when no predictions for one bin.""" - y_true, y_pred = self._get_calibration_placeholders() - expected_ece_op, update_op = calibration_metrics.expected_calibration_error( - y_true, y_pred, nbins=2) - with self.test_session() as sess: - metrics_vars = tf.get_collection(tf.GraphKeys.METRIC_VARIABLES) - sess.run(tf.variables_initializer(var_list=metrics_vars)) - # Bin calibration errors (|confidence - accuracy| * bin_weight): - # - [0,0.5): |0.2 - 0.333| * (3/5) = 0.08 - # - [0.5, 1]: |0.75 - 0.5| * (2/5) = 0.1 - sess.run( - update_op, - feed_dict={ - y_pred: np.array([0., 0.2, 0.4]), - y_true: np.array([0, 0, 1]) - }) - actual_ece = np.abs(0.2 - (1 / 3.)) - expected_ece = sess.run(expected_ece_op) - self.assertAlmostEqual(actual_ece, expected_ece) - - def test_expected_calibration_error_with_multiple_data_streams(self): - """Test expected calibration error when multiple data batches provided.""" - y_true, y_pred = self._get_calibration_placeholders() - expected_ece_op, update_op = calibration_metrics.expected_calibration_error( - y_true, y_pred, nbins=2) - with self.test_session() as sess: - metrics_vars = tf.get_collection(tf.GraphKeys.METRIC_VARIABLES) - sess.run(tf.variables_initializer(var_list=metrics_vars)) - # Identical data to test_expected_calibration_error_all_bins_filled, - # except split over three batches. - sess.run( - update_op, - feed_dict={ - y_pred: np.array([0., 0.2]), - y_true: np.array([0, 0]) - }) - sess.run( - update_op, - feed_dict={ - y_pred: np.array([0.4, 0.5]), - y_true: np.array([1, 0]) - }) - sess.run( - update_op, feed_dict={ - y_pred: np.array([1.0]), - y_true: np.array([1]) - }) - actual_ece = 0.08 + 0.1 - expected_ece = sess.run(expected_ece_op) - self.assertAlmostEqual(actual_ece, expected_ece) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/coco_evaluation.py b/research/object_detection/metrics/coco_evaluation.py deleted file mode 100644 index 22ecfac3282..00000000000 --- a/research/object_detection/metrics/coco_evaluation.py +++ /dev/null @@ -1,1902 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Class for evaluating object detections with COCO metrics.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields -from object_detection.metrics import coco_tools -from object_detection.utils import json_utils -from object_detection.utils import np_mask_ops -from object_detection.utils import object_detection_evaluation - - -class CocoDetectionEvaluator(object_detection_evaluation.DetectionEvaluator): - """Class to evaluate COCO detection metrics.""" - - def __init__(self, - categories, - include_metrics_per_category=False, - all_metrics_per_category=False, - skip_predictions_for_unlabeled_class=False, - super_categories=None): - """Constructor. - - Args: - categories: A list of dicts, each of which has the following keys - - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name e.g., 'cat', 'dog'. - include_metrics_per_category: If True, include metrics for each category. - all_metrics_per_category: Whether to include all the summary metrics for - each category in per_category_ap. Be careful with setting it to true if - you have more than handful of categories, because it will pollute - your mldash. - skip_predictions_for_unlabeled_class: Skip predictions that do not match - with the labeled classes for the image. - super_categories: None or a python dict mapping super-category names - (strings) to lists of categories (corresponding to category names - in the label_map). Metrics are aggregated along these super-categories - and added to the `per_category_ap` and are associated with the name - `PerformanceBySuperCategory/`. - """ - super(CocoDetectionEvaluator, self).__init__(categories) - # _image_ids is a dictionary that maps unique image ids to Booleans which - # indicate whether a corresponding detection has been added. - self._image_ids = {} - self._groundtruth_list = [] - self._detection_boxes_list = [] - self._category_id_set = set([cat['id'] for cat in self._categories]) - self._annotation_id = 1 - self._metrics = None - self._include_metrics_per_category = include_metrics_per_category - self._all_metrics_per_category = all_metrics_per_category - self._skip_predictions_for_unlabeled_class = skip_predictions_for_unlabeled_class - self._groundtruth_labeled_classes = {} - self._super_categories = super_categories - - def clear(self): - """Clears the state to prepare for a fresh evaluation.""" - self._image_ids.clear() - self._groundtruth_list = [] - self._detection_boxes_list = [] - - def add_single_ground_truth_image_info(self, - image_id, - groundtruth_dict): - """Adds groundtruth for a single image to be used for evaluation. - - If the image has already been added, a warning is logged, and groundtruth is - ignored. - - Args: - image_id: A unique string/integer identifier for the image. - groundtruth_dict: A dictionary containing - - InputDataFields.groundtruth_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - InputDataFields.groundtruth_classes: integer numpy array of shape - [num_boxes] containing 1-indexed groundtruth classes for the boxes. - InputDataFields.groundtruth_is_crowd (optional): integer numpy array of - shape [num_boxes] containing iscrowd flag for groundtruth boxes. - InputDataFields.groundtruth_area (optional): float numpy array of - shape [num_boxes] containing the area (in the original absolute - coordinates) of the annotated object. - InputDataFields.groundtruth_keypoints (optional): float numpy array of - keypoints with shape [num_boxes, num_keypoints, 2]. - InputDataFields.groundtruth_keypoint_visibilities (optional): integer - numpy array of keypoint visibilities with shape [num_gt_boxes, - num_keypoints]. Integer is treated as an enum with 0=not labeled, - 1=labeled but not visible and 2=labeled and visible. - InputDataFields.groundtruth_labeled_classes (optional): a tensor of - shape [num_classes + 1] containing the multi-hot tensor indicating the - classes that each image is labeled for. Note that the classes labels - are 1-indexed. - """ - if image_id in self._image_ids: - tf.logging.warning('Ignoring ground truth with image id %s since it was ' - 'previously added', image_id) - return - - # Drop optional fields if empty tensor. - groundtruth_is_crowd = groundtruth_dict.get( - standard_fields.InputDataFields.groundtruth_is_crowd) - groundtruth_area = groundtruth_dict.get( - standard_fields.InputDataFields.groundtruth_area) - groundtruth_keypoints = groundtruth_dict.get( - standard_fields.InputDataFields.groundtruth_keypoints) - groundtruth_keypoint_visibilities = groundtruth_dict.get( - standard_fields.InputDataFields.groundtruth_keypoint_visibilities) - if groundtruth_is_crowd is not None and not groundtruth_is_crowd.shape[0]: - groundtruth_is_crowd = None - if groundtruth_area is not None and not groundtruth_area.shape[0]: - groundtruth_area = None - if groundtruth_keypoints is not None and not groundtruth_keypoints.shape[0]: - groundtruth_keypoints = None - if groundtruth_keypoint_visibilities is not None and not groundtruth_keypoint_visibilities.shape[ - 0]: - groundtruth_keypoint_visibilities = None - - self._groundtruth_list.extend( - coco_tools.ExportSingleImageGroundtruthToCoco( - image_id=image_id, - next_annotation_id=self._annotation_id, - category_id_set=self._category_id_set, - groundtruth_boxes=groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_boxes], - groundtruth_classes=groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_classes], - groundtruth_is_crowd=groundtruth_is_crowd, - groundtruth_area=groundtruth_area, - groundtruth_keypoints=groundtruth_keypoints, - groundtruth_keypoint_visibilities=groundtruth_keypoint_visibilities) - ) - - self._annotation_id += groundtruth_dict[standard_fields.InputDataFields. - groundtruth_boxes].shape[0] - if (standard_fields.InputDataFields.groundtruth_labeled_classes - ) in groundtruth_dict: - labeled_classes = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_labeled_classes] - if labeled_classes.shape != (len(self._category_id_set) + 1,): - raise ValueError('Invalid shape for groundtruth labeled classes: {}, ' - 'num_categories_including_background: {}'.format( - labeled_classes, - len(self._category_id_set) + 1)) - self._groundtruth_labeled_classes[image_id] = np.flatnonzero( - groundtruth_dict[standard_fields.InputDataFields - .groundtruth_labeled_classes] == 1).tolist() - - # Boolean to indicate whether a detection has been added for this image. - self._image_ids[image_id] = False - - def add_single_detected_image_info(self, - image_id, - detections_dict): - """Adds detections for a single image to be used for evaluation. - - If a detection has already been added for this image id, a warning is - logged, and the detection is skipped. - - Args: - image_id: A unique string/integer identifier for the image. - detections_dict: A dictionary containing - - DetectionResultFields.detection_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` detection boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - DetectionResultFields.detection_scores: float32 numpy array of shape - [num_boxes] containing detection scores for the boxes. - DetectionResultFields.detection_classes: integer numpy array of shape - [num_boxes] containing 1-indexed detection classes for the boxes. - DetectionResultFields.detection_keypoints (optional): float numpy array - of keypoints with shape [num_boxes, num_keypoints, 2]. - Raises: - ValueError: If groundtruth for the image_id is not available. - """ - if image_id not in self._image_ids: - raise ValueError('Missing groundtruth for image id: {}'.format(image_id)) - - if self._image_ids[image_id]: - tf.logging.warning('Ignoring detection with image id %s since it was ' - 'previously added', image_id) - return - - # Drop optional fields if empty tensor. - detection_keypoints = detections_dict.get( - standard_fields.DetectionResultFields.detection_keypoints) - if detection_keypoints is not None and not detection_keypoints.shape[0]: - detection_keypoints = None - - if self._skip_predictions_for_unlabeled_class: - det_classes = detections_dict[ - standard_fields.DetectionResultFields.detection_classes] - num_det_boxes = det_classes.shape[0] - keep_box_ids = [] - for box_id in range(num_det_boxes): - if det_classes[box_id] in self._groundtruth_labeled_classes[image_id]: - keep_box_ids.append(box_id) - self._detection_boxes_list.extend( - coco_tools.ExportSingleImageDetectionBoxesToCoco( - image_id=image_id, - category_id_set=self._category_id_set, - detection_boxes=detections_dict[ - standard_fields.DetectionResultFields.detection_boxes] - [keep_box_ids], - detection_scores=detections_dict[ - standard_fields.DetectionResultFields.detection_scores] - [keep_box_ids], - detection_classes=detections_dict[ - standard_fields.DetectionResultFields.detection_classes] - [keep_box_ids], - detection_keypoints=detection_keypoints)) - else: - self._detection_boxes_list.extend( - coco_tools.ExportSingleImageDetectionBoxesToCoco( - image_id=image_id, - category_id_set=self._category_id_set, - detection_boxes=detections_dict[ - standard_fields.DetectionResultFields.detection_boxes], - detection_scores=detections_dict[ - standard_fields.DetectionResultFields.detection_scores], - detection_classes=detections_dict[ - standard_fields.DetectionResultFields.detection_classes], - detection_keypoints=detection_keypoints)) - self._image_ids[image_id] = True - - def dump_detections_to_json_file(self, json_output_path): - """Saves the detections into json_output_path in the format used by MS COCO. - - Args: - json_output_path: String containing the output file's path. It can be also - None. In that case nothing will be written to the output file. - """ - if json_output_path and json_output_path is not None: - with tf.gfile.GFile(json_output_path, 'w') as fid: - tf.logging.info('Dumping detections to output json file.') - json_utils.Dump( - obj=self._detection_boxes_list, fid=fid, float_digits=4, indent=2) - - def evaluate(self): - """Evaluates the detection boxes and returns a dictionary of coco metrics. - - Returns: - A dictionary holding - - - 1. summary_metrics: - 'DetectionBoxes_Precision/mAP': mean average precision over classes - averaged over IOU thresholds ranging from .5 to .95 with .05 - increments. - 'DetectionBoxes_Precision/mAP@.50IOU': mean average precision at 50% IOU - 'DetectionBoxes_Precision/mAP@.75IOU': mean average precision at 75% IOU - 'DetectionBoxes_Precision/mAP (small)': mean average precision for small - objects (area < 32^2 pixels). - 'DetectionBoxes_Precision/mAP (medium)': mean average precision for - medium sized objects (32^2 pixels < area < 96^2 pixels). - 'DetectionBoxes_Precision/mAP (large)': mean average precision for large - objects (96^2 pixels < area < 10000^2 pixels). - 'DetectionBoxes_Recall/AR@1': average recall with 1 detection. - 'DetectionBoxes_Recall/AR@10': average recall with 10 detections. - 'DetectionBoxes_Recall/AR@100': average recall with 100 detections. - 'DetectionBoxes_Recall/AR@100 (small)': average recall for small objects - with 100. - 'DetectionBoxes_Recall/AR@100 (medium)': average recall for medium objects - with 100. - 'DetectionBoxes_Recall/AR@100 (large)': average recall for large objects - with 100 detections. - - 2. per_category_ap: if include_metrics_per_category is True, category - specific results with keys of the form: - 'Precision mAP ByCategory/category' (without the supercategory part if - no supercategories exist). For backward compatibility - 'PerformanceByCategory' is included in the output regardless of - all_metrics_per_category. - If super_categories are provided, then this will additionally include - metrics aggregated along the super_categories with keys of the form: - `PerformanceBySuperCategory/` - """ - tf.logging.info('Performing evaluation on %d images.', len(self._image_ids)) - groundtruth_dict = { - 'annotations': self._groundtruth_list, - 'images': [{'id': image_id} for image_id in self._image_ids], - 'categories': self._categories - } - coco_wrapped_groundtruth = coco_tools.COCOWrapper(groundtruth_dict) - coco_wrapped_detections = coco_wrapped_groundtruth.LoadAnnotations( - self._detection_boxes_list) - box_evaluator = coco_tools.COCOEvalWrapper( - coco_wrapped_groundtruth, coco_wrapped_detections, agnostic_mode=False) - box_metrics, box_per_category_ap = box_evaluator.ComputeMetrics( - include_metrics_per_category=self._include_metrics_per_category, - all_metrics_per_category=self._all_metrics_per_category, - super_categories=self._super_categories) - box_metrics.update(box_per_category_ap) - box_metrics = {'DetectionBoxes_'+ key: value - for key, value in iter(box_metrics.items())} - return box_metrics - - def add_eval_dict(self, eval_dict): - """Observes an evaluation result dict for a single example. - - When executing eagerly, once all observations have been observed by this - method you can use `.evaluate()` to get the final metrics. - - When using `tf.estimator.Estimator` for evaluation this function is used by - `get_estimator_eval_metric_ops()` to construct the metric update op. - - Args: - eval_dict: A dictionary that holds tensors for evaluating an object - detection model, returned from - eval_util.result_dict_for_single_example(). - - Returns: - None when executing eagerly, or an update_op that can be used to update - the eval metrics in `tf.estimator.EstimatorSpec`. - """ - - def update_op(image_id_batched, groundtruth_boxes_batched, - groundtruth_classes_batched, groundtruth_is_crowd_batched, - groundtruth_labeled_classes_batched, num_gt_boxes_per_image, - detection_boxes_batched, detection_scores_batched, - detection_classes_batched, num_det_boxes_per_image, - is_annotated_batched): - """Update operation for adding batch of images to Coco evaluator.""" - for (image_id, gt_box, gt_class, gt_is_crowd, gt_labeled_classes, - num_gt_box, det_box, det_score, det_class, - num_det_box, is_annotated) in zip( - image_id_batched, groundtruth_boxes_batched, - groundtruth_classes_batched, groundtruth_is_crowd_batched, - groundtruth_labeled_classes_batched, num_gt_boxes_per_image, - detection_boxes_batched, detection_scores_batched, - detection_classes_batched, num_det_boxes_per_image, - is_annotated_batched): - if is_annotated: - self.add_single_ground_truth_image_info( - image_id, { - 'groundtruth_boxes': gt_box[:num_gt_box], - 'groundtruth_classes': gt_class[:num_gt_box], - 'groundtruth_is_crowd': gt_is_crowd[:num_gt_box], - 'groundtruth_labeled_classes': gt_labeled_classes - }) - self.add_single_detected_image_info( - image_id, - {'detection_boxes': det_box[:num_det_box], - 'detection_scores': det_score[:num_det_box], - 'detection_classes': det_class[:num_det_box]}) - - # Unpack items from the evaluation dictionary. - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - image_id = eval_dict[input_data_fields.key] - groundtruth_boxes = eval_dict[input_data_fields.groundtruth_boxes] - groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] - groundtruth_is_crowd = eval_dict.get( - input_data_fields.groundtruth_is_crowd, None) - groundtruth_labeled_classes = eval_dict.get( - input_data_fields.groundtruth_labeled_classes, None) - detection_boxes = eval_dict[detection_fields.detection_boxes] - detection_scores = eval_dict[detection_fields.detection_scores] - detection_classes = eval_dict[detection_fields.detection_classes] - num_gt_boxes_per_image = eval_dict.get( - input_data_fields.num_groundtruth_boxes, None) - num_det_boxes_per_image = eval_dict.get(detection_fields.num_detections, - None) - is_annotated = eval_dict.get('is_annotated', None) - - if groundtruth_is_crowd is None: - groundtruth_is_crowd = tf.zeros_like(groundtruth_classes, dtype=tf.bool) - - # If groundtruth_labeled_classes is not provided, make it equal to the - # detection_classes. This assumes that all predictions will be kept to - # compute eval metrics. - if groundtruth_labeled_classes is None: - groundtruth_labeled_classes = tf.reduce_max( - tf.one_hot( - tf.cast(detection_classes, tf.int32), - len(self._category_id_set) + 1), - axis=-2) - - if not image_id.shape.as_list(): - # Apply a batch dimension to all tensors. - image_id = tf.expand_dims(image_id, 0) - groundtruth_boxes = tf.expand_dims(groundtruth_boxes, 0) - groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) - groundtruth_is_crowd = tf.expand_dims(groundtruth_is_crowd, 0) - groundtruth_labeled_classes = tf.expand_dims(groundtruth_labeled_classes, - 0) - detection_boxes = tf.expand_dims(detection_boxes, 0) - detection_scores = tf.expand_dims(detection_scores, 0) - detection_classes = tf.expand_dims(detection_classes, 0) - - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.shape(groundtruth_boxes)[1:2] - else: - num_gt_boxes_per_image = tf.expand_dims(num_gt_boxes_per_image, 0) - - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.shape(detection_boxes)[1:2] - else: - num_det_boxes_per_image = tf.expand_dims(num_det_boxes_per_image, 0) - - if is_annotated is None: - is_annotated = tf.constant([True]) - else: - is_annotated = tf.expand_dims(is_annotated, 0) - else: - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.tile( - tf.shape(groundtruth_boxes)[1:2], - multiples=tf.shape(groundtruth_boxes)[0:1]) - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.tile( - tf.shape(detection_boxes)[1:2], - multiples=tf.shape(detection_boxes)[0:1]) - if is_annotated is None: - is_annotated = tf.ones_like(image_id, dtype=tf.bool) - - return tf.py_func(update_op, [ - image_id, groundtruth_boxes, groundtruth_classes, groundtruth_is_crowd, - groundtruth_labeled_classes, num_gt_boxes_per_image, detection_boxes, - detection_scores, detection_classes, num_det_boxes_per_image, - is_annotated - ], []) - - def get_estimator_eval_metric_ops(self, eval_dict): - """Returns a dictionary of eval metric ops. - - Note that once value_op is called, the detections and groundtruth added via - update_op are cleared. - - This function can take in groundtruth and detections for a batch of images, - or for a single image. For the latter case, the batch dimension for input - tensors need not be present. - - Args: - eval_dict: A dictionary that holds tensors for evaluating object detection - performance. For single-image evaluation, this dictionary may be - produced from eval_util.result_dict_for_single_example(). If multi-image - evaluation, `eval_dict` should contain the fields - 'num_groundtruth_boxes_per_image' and 'num_det_boxes_per_image' to - properly unpad the tensors from the batch. - - Returns: - a dictionary of metric names to tuple of value_op and update_op that can - be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all - update ops must be run together and similarly all value ops must be run - together to guarantee correct behaviour. - """ - update_op = self.add_eval_dict(eval_dict) - metric_names = ['DetectionBoxes_Precision/mAP', - 'DetectionBoxes_Precision/mAP@.50IOU', - 'DetectionBoxes_Precision/mAP@.75IOU', - 'DetectionBoxes_Precision/mAP (large)', - 'DetectionBoxes_Precision/mAP (medium)', - 'DetectionBoxes_Precision/mAP (small)', - 'DetectionBoxes_Recall/AR@1', - 'DetectionBoxes_Recall/AR@10', - 'DetectionBoxes_Recall/AR@100', - 'DetectionBoxes_Recall/AR@100 (large)', - 'DetectionBoxes_Recall/AR@100 (medium)', - 'DetectionBoxes_Recall/AR@100 (small)'] - if self._include_metrics_per_category: - for category_dict in self._categories: - metric_names.append('DetectionBoxes_PerformanceByCategory/mAP/' + - category_dict['name']) - - def first_value_func(): - self._metrics = self.evaluate() - self.clear() - return np.float32(self._metrics[metric_names[0]]) - - def value_func_factory(metric_name): - def value_func(): - return np.float32(self._metrics[metric_name]) - return value_func - - # Ensure that the metrics are only evaluated once. - first_value_op = tf.py_func(first_value_func, [], tf.float32) - eval_metric_ops = {metric_names[0]: (first_value_op, update_op)} - with tf.control_dependencies([first_value_op]): - for metric_name in metric_names[1:]: - eval_metric_ops[metric_name] = (tf.py_func( - value_func_factory(metric_name), [], np.float32), update_op) - return eval_metric_ops - - -def convert_masks_to_binary(masks): - """Converts masks to 0 or 1 and uint8 type.""" - return (masks > 0).astype(np.uint8) - - -class CocoKeypointEvaluator(CocoDetectionEvaluator): - """Class to evaluate COCO keypoint metrics.""" - - def __init__(self, - category_id, - category_keypoints, - class_text, - oks_sigmas=None): - """Constructor. - - Args: - category_id: An integer id uniquely identifying this category. - category_keypoints: A list specifying keypoint mappings, with items: - 'id': (required) an integer id identifying the keypoint. - 'name': (required) a string representing the keypoint name. - class_text: A string representing the category name for which keypoint - metrics are to be computed. - oks_sigmas: A dict of keypoint name to standard deviation values for OKS - metrics. If not provided, default value of 0.05 will be used. - """ - self._category_id = category_id - self._category_name = class_text - self._keypoint_ids = sorted( - [keypoint['id'] for keypoint in category_keypoints]) - kpt_id_to_name = {kpt['id']: kpt['name'] for kpt in category_keypoints} - if oks_sigmas: - self._oks_sigmas = np.array([ - oks_sigmas[kpt_id_to_name[idx]] for idx in self._keypoint_ids - ]) - else: - # Default all per-keypoint sigmas to 0. - self._oks_sigmas = np.full((len(self._keypoint_ids)), 0.05) - tf.logging.warning('No default keypoint OKS sigmas provided. Will use ' - '0.05') - tf.logging.info('Using the following keypoint OKS sigmas: {}'.format( - self._oks_sigmas)) - self._metrics = None - super(CocoKeypointEvaluator, self).__init__([{ - 'id': self._category_id, - 'name': class_text - }]) - - def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): - """Adds groundtruth for a single image with keypoints. - - If the image has already been added, a warning is logged, and groundtruth - is ignored. - - Args: - image_id: A unique string/integer identifier for the image. - groundtruth_dict: A dictionary containing - - InputDataFields.groundtruth_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - InputDataFields.groundtruth_classes: integer numpy array of shape - [num_boxes] containing 1-indexed groundtruth classes for the boxes. - InputDataFields.groundtruth_is_crowd (optional): integer numpy array of - shape [num_boxes] containing iscrowd flag for groundtruth boxes. - InputDataFields.groundtruth_area (optional): float numpy array of - shape [num_boxes] containing the area (in the original absolute - coordinates) of the annotated object. - InputDataFields.groundtruth_keypoints: float numpy array of - keypoints with shape [num_boxes, num_keypoints, 2]. - InputDataFields.groundtruth_keypoint_visibilities (optional): integer - numpy array of keypoint visibilities with shape [num_gt_boxes, - num_keypoints]. Integer is treated as an enum with 0=not labels, - 1=labeled but not visible and 2=labeled and visible. - """ - - # Keep only the groundtruth for our category and its keypoints. - groundtruth_classes = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_classes] - groundtruth_boxes = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_boxes] - groundtruth_keypoints = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_keypoints] - class_indices = [ - idx for idx, gt_class_id in enumerate(groundtruth_classes) - if gt_class_id == self._category_id - ] - filtered_groundtruth_classes = np.take( - groundtruth_classes, class_indices, axis=0) - filtered_groundtruth_boxes = np.take( - groundtruth_boxes, class_indices, axis=0) - filtered_groundtruth_keypoints = np.take( - groundtruth_keypoints, class_indices, axis=0) - filtered_groundtruth_keypoints = np.take( - filtered_groundtruth_keypoints, self._keypoint_ids, axis=1) - - filtered_groundtruth_dict = {} - filtered_groundtruth_dict[ - standard_fields.InputDataFields - .groundtruth_classes] = filtered_groundtruth_classes - filtered_groundtruth_dict[standard_fields.InputDataFields - .groundtruth_boxes] = filtered_groundtruth_boxes - filtered_groundtruth_dict[ - standard_fields.InputDataFields - .groundtruth_keypoints] = filtered_groundtruth_keypoints - - if (standard_fields.InputDataFields.groundtruth_is_crowd in - groundtruth_dict.keys()): - groundtruth_is_crowd = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_is_crowd] - filtered_groundtruth_is_crowd = np.take(groundtruth_is_crowd, - class_indices, 0) - filtered_groundtruth_dict[ - standard_fields.InputDataFields - .groundtruth_is_crowd] = filtered_groundtruth_is_crowd - if (standard_fields.InputDataFields.groundtruth_area in - groundtruth_dict.keys()): - groundtruth_area = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_area] - filtered_groundtruth_area = np.take(groundtruth_area, class_indices, 0) - filtered_groundtruth_dict[ - standard_fields.InputDataFields - .groundtruth_area] = filtered_groundtruth_area - if (standard_fields.InputDataFields.groundtruth_keypoint_visibilities in - groundtruth_dict.keys()): - groundtruth_keypoint_visibilities = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_keypoint_visibilities] - filtered_groundtruth_keypoint_visibilities = np.take( - groundtruth_keypoint_visibilities, class_indices, axis=0) - filtered_groundtruth_keypoint_visibilities = np.take( - filtered_groundtruth_keypoint_visibilities, - self._keypoint_ids, - axis=1) - filtered_groundtruth_dict[ - standard_fields.InputDataFields. - groundtruth_keypoint_visibilities] = filtered_groundtruth_keypoint_visibilities - - super(CocoKeypointEvaluator, - self).add_single_ground_truth_image_info(image_id, - filtered_groundtruth_dict) - - def add_single_detected_image_info(self, image_id, detections_dict): - """Adds detections for a single image and the specific category for which keypoints are evaluated. - - If a detection has already been added for this image id, a warning is - logged, and the detection is skipped. - - Args: - image_id: A unique string/integer identifier for the image. - detections_dict: A dictionary containing - - DetectionResultFields.detection_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` detection boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - DetectionResultFields.detection_scores: float32 numpy array of shape - [num_boxes] containing detection scores for the boxes. - DetectionResultFields.detection_classes: integer numpy array of shape - [num_boxes] containing 1-indexed detection classes for the boxes. - DetectionResultFields.detection_keypoints: float numpy array of - keypoints with shape [num_boxes, num_keypoints, 2]. - - Raises: - ValueError: If groundtruth for the image_id is not available. - """ - - # Keep only the detections for our category and its keypoints. - detection_classes = detections_dict[ - standard_fields.DetectionResultFields.detection_classes] - detection_boxes = detections_dict[ - standard_fields.DetectionResultFields.detection_boxes] - detection_scores = detections_dict[ - standard_fields.DetectionResultFields.detection_scores] - detection_keypoints = detections_dict[ - standard_fields.DetectionResultFields.detection_keypoints] - class_indices = [ - idx for idx, class_id in enumerate(detection_classes) - if class_id == self._category_id - ] - filtered_detection_classes = np.take( - detection_classes, class_indices, axis=0) - filtered_detection_boxes = np.take(detection_boxes, class_indices, axis=0) - filtered_detection_scores = np.take(detection_scores, class_indices, axis=0) - filtered_detection_keypoints = np.take( - detection_keypoints, class_indices, axis=0) - filtered_detection_keypoints = np.take( - filtered_detection_keypoints, self._keypoint_ids, axis=1) - - filtered_detections_dict = {} - filtered_detections_dict[standard_fields.DetectionResultFields - .detection_classes] = filtered_detection_classes - filtered_detections_dict[standard_fields.DetectionResultFields - .detection_boxes] = filtered_detection_boxes - filtered_detections_dict[standard_fields.DetectionResultFields - .detection_scores] = filtered_detection_scores - filtered_detections_dict[standard_fields.DetectionResultFields. - detection_keypoints] = filtered_detection_keypoints - - super(CocoKeypointEvaluator, - self).add_single_detected_image_info(image_id, - filtered_detections_dict) - - def evaluate(self): - """Evaluates the keypoints and returns a dictionary of coco metrics. - - Returns: - A dictionary holding - - - 1. summary_metrics: - 'Keypoints_Precision/mAP': mean average precision over classes - averaged over OKS thresholds ranging from .5 to .95 with .05 - increments. - 'Keypoints_Precision/mAP@.50IOU': mean average precision at 50% OKS - 'Keypoints_Precision/mAP@.75IOU': mean average precision at 75% OKS - 'Keypoints_Precision/mAP (medium)': mean average precision for medium - sized objects (32^2 pixels < area < 96^2 pixels). - 'Keypoints_Precision/mAP (large)': mean average precision for large - objects (96^2 pixels < area < 10000^2 pixels). - 'Keypoints_Recall/AR@1': average recall with 1 detection. - 'Keypoints_Recall/AR@10': average recall with 10 detections. - 'Keypoints_Recall/AR@100': average recall with 100 detections. - 'Keypoints_Recall/AR@100 (medium)': average recall for medium objects with - 100. - 'Keypoints_Recall/AR@100 (large)': average recall for large objects with - 100 detections. - """ - tf.logging.info('Performing evaluation on %d images.', len(self._image_ids)) - groundtruth_dict = { - 'annotations': self._groundtruth_list, - 'images': [{'id': image_id} for image_id in self._image_ids], - 'categories': self._categories - } - coco_wrapped_groundtruth = coco_tools.COCOWrapper( - groundtruth_dict, detection_type='bbox') - coco_wrapped_detections = coco_wrapped_groundtruth.LoadAnnotations( - self._detection_boxes_list) - keypoint_evaluator = coco_tools.COCOEvalWrapper( - coco_wrapped_groundtruth, - coco_wrapped_detections, - agnostic_mode=False, - iou_type='keypoints', - oks_sigmas=self._oks_sigmas) - keypoint_metrics, _ = keypoint_evaluator.ComputeMetrics( - include_metrics_per_category=False, all_metrics_per_category=False) - keypoint_metrics = { - 'Keypoints_' + key: value - for key, value in iter(keypoint_metrics.items()) - } - return keypoint_metrics - - def add_eval_dict(self, eval_dict): - """Observes an evaluation result dict for a single example. - - When executing eagerly, once all observations have been observed by this - method you can use `.evaluate()` to get the final metrics. - - When using `tf.estimator.Estimator` for evaluation this function is used by - `get_estimator_eval_metric_ops()` to construct the metric update op. - - Args: - eval_dict: A dictionary that holds tensors for evaluating an object - detection model, returned from - eval_util.result_dict_for_single_example(). - - Returns: - None when executing eagerly, or an update_op that can be used to update - the eval metrics in `tf.estimator.EstimatorSpec`. - """ - def update_op( - image_id_batched, - groundtruth_boxes_batched, - groundtruth_classes_batched, - groundtruth_is_crowd_batched, - groundtruth_area_batched, - groundtruth_keypoints_batched, - groundtruth_keypoint_visibilities_batched, - num_gt_boxes_per_image, - detection_boxes_batched, - detection_scores_batched, - detection_classes_batched, - detection_keypoints_batched, - num_det_boxes_per_image, - is_annotated_batched): - """Update operation for adding batch of images to Coco evaluator.""" - - for (image_id, gt_box, gt_class, gt_is_crowd, gt_area, gt_keyp, - gt_keyp_vis, num_gt_box, det_box, det_score, det_class, det_keyp, - num_det_box, is_annotated) in zip( - image_id_batched, groundtruth_boxes_batched, - groundtruth_classes_batched, groundtruth_is_crowd_batched, - groundtruth_area_batched, groundtruth_keypoints_batched, - groundtruth_keypoint_visibilities_batched, - num_gt_boxes_per_image, detection_boxes_batched, - detection_scores_batched, detection_classes_batched, - detection_keypoints_batched, num_det_boxes_per_image, - is_annotated_batched): - if is_annotated: - self.add_single_ground_truth_image_info( - image_id, { - 'groundtruth_boxes': gt_box[:num_gt_box], - 'groundtruth_classes': gt_class[:num_gt_box], - 'groundtruth_is_crowd': gt_is_crowd[:num_gt_box], - 'groundtruth_area': gt_area[:num_gt_box], - 'groundtruth_keypoints': gt_keyp[:num_gt_box], - 'groundtruth_keypoint_visibilities': gt_keyp_vis[:num_gt_box] - }) - self.add_single_detected_image_info( - image_id, { - 'detection_boxes': det_box[:num_det_box], - 'detection_scores': det_score[:num_det_box], - 'detection_classes': det_class[:num_det_box], - 'detection_keypoints': det_keyp[:num_det_box], - }) - - # Unpack items from the evaluation dictionary. - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - image_id = eval_dict[input_data_fields.key] - groundtruth_boxes = eval_dict[input_data_fields.groundtruth_boxes] - groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] - groundtruth_is_crowd = eval_dict.get(input_data_fields.groundtruth_is_crowd, - None) - groundtruth_area = eval_dict.get(input_data_fields.groundtruth_area, None) - groundtruth_keypoints = eval_dict[input_data_fields.groundtruth_keypoints] - groundtruth_keypoint_visibilities = eval_dict.get( - input_data_fields.groundtruth_keypoint_visibilities, None) - detection_boxes = eval_dict[detection_fields.detection_boxes] - detection_scores = eval_dict[detection_fields.detection_scores] - detection_classes = eval_dict[detection_fields.detection_classes] - detection_keypoints = eval_dict[detection_fields.detection_keypoints] - num_gt_boxes_per_image = eval_dict.get( - 'num_groundtruth_boxes_per_image', None) - num_det_boxes_per_image = eval_dict.get('num_det_boxes_per_image', None) - is_annotated = eval_dict.get('is_annotated', None) - - if groundtruth_is_crowd is None: - groundtruth_is_crowd = tf.zeros_like(groundtruth_classes, dtype=tf.bool) - - if groundtruth_area is None: - groundtruth_area = tf.zeros_like(groundtruth_classes, dtype=tf.float32) - - if not image_id.shape.as_list(): - # Apply a batch dimension to all tensors. - image_id = tf.expand_dims(image_id, 0) - groundtruth_boxes = tf.expand_dims(groundtruth_boxes, 0) - groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) - groundtruth_is_crowd = tf.expand_dims(groundtruth_is_crowd, 0) - groundtruth_area = tf.expand_dims(groundtruth_area, 0) - groundtruth_keypoints = tf.expand_dims(groundtruth_keypoints, 0) - detection_boxes = tf.expand_dims(detection_boxes, 0) - detection_scores = tf.expand_dims(detection_scores, 0) - detection_classes = tf.expand_dims(detection_classes, 0) - detection_keypoints = tf.expand_dims(detection_keypoints, 0) - - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.shape(groundtruth_boxes)[1:2] - else: - num_gt_boxes_per_image = tf.expand_dims(num_gt_boxes_per_image, 0) - - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.shape(detection_boxes)[1:2] - else: - num_det_boxes_per_image = tf.expand_dims(num_det_boxes_per_image, 0) - - if is_annotated is None: - is_annotated = tf.constant([True]) - else: - is_annotated = tf.expand_dims(is_annotated, 0) - - if groundtruth_keypoint_visibilities is None: - groundtruth_keypoint_visibilities = tf.fill([ - tf.shape(groundtruth_boxes)[1], - tf.shape(groundtruth_keypoints)[2] - ], tf.constant(2, dtype=tf.int32)) - groundtruth_keypoint_visibilities = tf.expand_dims( - groundtruth_keypoint_visibilities, 0) - else: - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.tile( - tf.shape(groundtruth_boxes)[1:2], - multiples=tf.shape(groundtruth_boxes)[0:1]) - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.tile( - tf.shape(detection_boxes)[1:2], - multiples=tf.shape(detection_boxes)[0:1]) - if is_annotated is None: - is_annotated = tf.ones_like(image_id, dtype=tf.bool) - if groundtruth_keypoint_visibilities is None: - groundtruth_keypoint_visibilities = tf.fill([ - tf.shape(groundtruth_keypoints)[1], - tf.shape(groundtruth_keypoints)[2] - ], tf.constant(2, dtype=tf.int32)) - groundtruth_keypoint_visibilities = tf.tile( - tf.expand_dims(groundtruth_keypoint_visibilities, 0), - multiples=[tf.shape(groundtruth_keypoints)[0], 1, 1]) - - return tf.py_func(update_op, [ - image_id, groundtruth_boxes, groundtruth_classes, groundtruth_is_crowd, - groundtruth_area, groundtruth_keypoints, - groundtruth_keypoint_visibilities, num_gt_boxes_per_image, - detection_boxes, detection_scores, detection_classes, - detection_keypoints, num_det_boxes_per_image, is_annotated - ], []) - - def get_estimator_eval_metric_ops(self, eval_dict): - """Returns a dictionary of eval metric ops. - - Note that once value_op is called, the detections and groundtruth added via - update_op are cleared. - - This function can take in groundtruth and detections for a batch of images, - or for a single image. For the latter case, the batch dimension for input - tensors need not be present. - - Args: - eval_dict: A dictionary that holds tensors for evaluating object detection - performance. For single-image evaluation, this dictionary may be - produced from eval_util.result_dict_for_single_example(). If multi-image - evaluation, `eval_dict` should contain the fields - 'num_groundtruth_boxes_per_image' and 'num_det_boxes_per_image' to - properly unpad the tensors from the batch. - - Returns: - a dictionary of metric names to tuple of value_op and update_op that can - be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all - update ops must be run together and similarly all value ops must be run - together to guarantee correct behaviour. - """ - update_op = self.add_eval_dict(eval_dict) - category = self._category_name - metric_names = [ - 'Keypoints_Precision/mAP ByCategory/{}'.format(category), - 'Keypoints_Precision/mAP@.50IOU ByCategory/{}'.format(category), - 'Keypoints_Precision/mAP@.75IOU ByCategory/{}'.format(category), - 'Keypoints_Precision/mAP (large) ByCategory/{}'.format(category), - 'Keypoints_Precision/mAP (medium) ByCategory/{}'.format(category), - 'Keypoints_Recall/AR@1 ByCategory/{}'.format(category), - 'Keypoints_Recall/AR@10 ByCategory/{}'.format(category), - 'Keypoints_Recall/AR@100 ByCategory/{}'.format(category), - 'Keypoints_Recall/AR@100 (large) ByCategory/{}'.format(category), - 'Keypoints_Recall/AR@100 (medium) ByCategory/{}'.format(category) - ] - - def first_value_func(): - self._metrics = self.evaluate() - self.clear() - return np.float32(self._metrics[metric_names[0]]) - - def value_func_factory(metric_name): - def value_func(): - return np.float32(self._metrics[metric_name]) - return value_func - - # Ensure that the metrics are only evaluated once. - first_value_op = tf.py_func(first_value_func, [], tf.float32) - eval_metric_ops = {metric_names[0]: (first_value_op, update_op)} - with tf.control_dependencies([first_value_op]): - for metric_name in metric_names[1:]: - eval_metric_ops[metric_name] = (tf.py_func( - value_func_factory(metric_name), [], np.float32), update_op) - return eval_metric_ops - - -class CocoMaskEvaluator(object_detection_evaluation.DetectionEvaluator): - """Class to evaluate COCO detection metrics.""" - - def __init__(self, categories, - include_metrics_per_category=False, - all_metrics_per_category=False, - super_categories=None): - """Constructor. - - Args: - categories: A list of dicts, each of which has the following keys - - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name e.g., 'cat', 'dog'. - include_metrics_per_category: If True, include metrics for each category. - all_metrics_per_category: Whether to include all the summary metrics for - each category in per_category_ap. Be careful with setting it to true if - you have more than handful of categories, because it will pollute - your mldash. - super_categories: None or a python dict mapping super-category names - (strings) to lists of categories (corresponding to category names - in the label_map). Metrics are aggregated along these super-categories - and added to the `per_category_ap` and are associated with the name - `PerformanceBySuperCategory/`. - """ - super(CocoMaskEvaluator, self).__init__(categories) - self._image_id_to_mask_shape_map = {} - self._image_ids_with_detections = set([]) - self._groundtruth_list = [] - self._detection_masks_list = [] - self._category_id_set = set([cat['id'] for cat in self._categories]) - self._annotation_id = 1 - self._include_metrics_per_category = include_metrics_per_category - self._super_categories = super_categories - self._all_metrics_per_category = all_metrics_per_category - - def clear(self): - """Clears the state to prepare for a fresh evaluation.""" - self._image_id_to_mask_shape_map.clear() - self._image_ids_with_detections.clear() - self._groundtruth_list = [] - self._detection_masks_list = [] - - def add_single_ground_truth_image_info(self, - image_id, - groundtruth_dict): - """Adds groundtruth for a single image to be used for evaluation. - - If the image has already been added, a warning is logged, and groundtruth is - ignored. - - Args: - image_id: A unique string/integer identifier for the image. - groundtruth_dict: A dictionary containing - - InputDataFields.groundtruth_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - InputDataFields.groundtruth_classes: integer numpy array of shape - [num_boxes] containing 1-indexed groundtruth classes for the boxes. - InputDataFields.groundtruth_instance_masks: uint8 numpy array of shape - [num_boxes, image_height, image_width] containing groundtruth masks - corresponding to the boxes. The elements of the array must be in - {0, 1}. - InputDataFields.groundtruth_is_crowd (optional): integer numpy array of - shape [num_boxes] containing iscrowd flag for groundtruth boxes. - InputDataFields.groundtruth_area (optional): float numpy array of - shape [num_boxes] containing the area (in the original absolute - coordinates) of the annotated object. - """ - if image_id in self._image_id_to_mask_shape_map: - tf.logging.warning('Ignoring ground truth with image id %s since it was ' - 'previously added', image_id) - return - - # Drop optional fields if empty tensor. - groundtruth_is_crowd = groundtruth_dict.get( - standard_fields.InputDataFields.groundtruth_is_crowd) - groundtruth_area = groundtruth_dict.get( - standard_fields.InputDataFields.groundtruth_area) - if groundtruth_is_crowd is not None and not groundtruth_is_crowd.shape[0]: - groundtruth_is_crowd = None - if groundtruth_area is not None and not groundtruth_area.shape[0]: - groundtruth_area = None - - groundtruth_instance_masks = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_instance_masks] - groundtruth_instance_masks = convert_masks_to_binary( - groundtruth_instance_masks) - self._groundtruth_list.extend( - coco_tools. - ExportSingleImageGroundtruthToCoco( - image_id=image_id, - next_annotation_id=self._annotation_id, - category_id_set=self._category_id_set, - groundtruth_boxes=groundtruth_dict[standard_fields.InputDataFields. - groundtruth_boxes], - groundtruth_classes=groundtruth_dict[standard_fields. - InputDataFields. - groundtruth_classes], - groundtruth_masks=groundtruth_instance_masks, - groundtruth_is_crowd=groundtruth_is_crowd, - groundtruth_area=groundtruth_area)) - self._annotation_id += groundtruth_dict[standard_fields.InputDataFields. - groundtruth_boxes].shape[0] - self._image_id_to_mask_shape_map[image_id] = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_instance_masks].shape - - def add_single_detected_image_info(self, - image_id, - detections_dict): - """Adds detections for a single image to be used for evaluation. - - If a detection has already been added for this image id, a warning is - logged, and the detection is skipped. - - Args: - image_id: A unique string/integer identifier for the image. - detections_dict: A dictionary containing - - DetectionResultFields.detection_scores: float32 numpy array of shape - [num_boxes] containing detection scores for the boxes. - DetectionResultFields.detection_classes: integer numpy array of shape - [num_boxes] containing 1-indexed detection classes for the boxes. - DetectionResultFields.detection_masks: optional uint8 numpy array of - shape [num_boxes, image_height, image_width] containing instance - masks corresponding to the boxes. The elements of the array must be - in {0, 1}. - - Raises: - ValueError: If groundtruth for the image_id is not available or if - spatial shapes of groundtruth_instance_masks and detection_masks are - incompatible. - """ - if image_id not in self._image_id_to_mask_shape_map: - raise ValueError('Missing groundtruth for image id: {}'.format(image_id)) - - if image_id in self._image_ids_with_detections: - tf.logging.warning('Ignoring detection with image id %s since it was ' - 'previously added', image_id) - return - - groundtruth_masks_shape = self._image_id_to_mask_shape_map[image_id] - detection_masks = detections_dict[standard_fields.DetectionResultFields. - detection_masks] - if groundtruth_masks_shape[1:] != detection_masks.shape[1:]: - raise ValueError('Spatial shape of groundtruth masks and detection masks ' - 'are incompatible: {} vs {}'.format( - groundtruth_masks_shape, - detection_masks.shape)) - detection_masks = convert_masks_to_binary(detection_masks) - self._detection_masks_list.extend( - coco_tools.ExportSingleImageDetectionMasksToCoco( - image_id=image_id, - category_id_set=self._category_id_set, - detection_masks=detection_masks, - detection_scores=detections_dict[standard_fields. - DetectionResultFields. - detection_scores], - detection_classes=detections_dict[standard_fields. - DetectionResultFields. - detection_classes])) - self._image_ids_with_detections.update([image_id]) - - def dump_detections_to_json_file(self, json_output_path): - """Saves the detections into json_output_path in the format used by MS COCO. - - Args: - json_output_path: String containing the output file's path. It can be also - None. In that case nothing will be written to the output file. - """ - if json_output_path and json_output_path is not None: - tf.logging.info('Dumping detections to output json file.') - with tf.gfile.GFile(json_output_path, 'w') as fid: - json_utils.Dump( - obj=self._detection_masks_list, fid=fid, float_digits=4, indent=2) - - def evaluate(self): - """Evaluates the detection masks and returns a dictionary of coco metrics. - - Returns: - A dictionary holding - - - 1. summary_metrics: - 'DetectionMasks_Precision/mAP': mean average precision over classes - averaged over IOU thresholds ranging from .5 to .95 with .05 increments. - 'DetectionMasks_Precision/mAP@.50IOU': mean average precision at 50% IOU. - 'DetectionMasks_Precision/mAP@.75IOU': mean average precision at 75% IOU. - 'DetectionMasks_Precision/mAP (small)': mean average precision for small - objects (area < 32^2 pixels). - 'DetectionMasks_Precision/mAP (medium)': mean average precision for medium - sized objects (32^2 pixels < area < 96^2 pixels). - 'DetectionMasks_Precision/mAP (large)': mean average precision for large - objects (96^2 pixels < area < 10000^2 pixels). - 'DetectionMasks_Recall/AR@1': average recall with 1 detection. - 'DetectionMasks_Recall/AR@10': average recall with 10 detections. - 'DetectionMasks_Recall/AR@100': average recall with 100 detections. - 'DetectionMasks_Recall/AR@100 (small)': average recall for small objects - with 100 detections. - 'DetectionMasks_Recall/AR@100 (medium)': average recall for medium objects - with 100 detections. - 'DetectionMasks_Recall/AR@100 (large)': average recall for large objects - with 100 detections. - - 2. per_category_ap: if include_metrics_per_category is True, category - specific results with keys of the form: - 'Precision mAP ByCategory/category' (without the supercategory part if - no supercategories exist). For backward compatibility - 'PerformanceByCategory' is included in the output regardless of - all_metrics_per_category. - If super_categories are provided, then this will additionally include - metrics aggregated along the super_categories with keys of the form: - `PerformanceBySuperCategory/` - """ - groundtruth_dict = { - 'annotations': self._groundtruth_list, - 'images': [{'id': image_id, 'height': shape[1], 'width': shape[2]} - for image_id, shape in self._image_id_to_mask_shape_map. - items()], - 'categories': self._categories - } - coco_wrapped_groundtruth = coco_tools.COCOWrapper( - groundtruth_dict, detection_type='segmentation') - coco_wrapped_detection_masks = coco_wrapped_groundtruth.LoadAnnotations( - self._detection_masks_list) - mask_evaluator = coco_tools.COCOEvalWrapper( - coco_wrapped_groundtruth, coco_wrapped_detection_masks, - agnostic_mode=False, iou_type='segm') - mask_metrics, mask_per_category_ap = mask_evaluator.ComputeMetrics( - include_metrics_per_category=self._include_metrics_per_category, - super_categories=self._super_categories, - all_metrics_per_category=self._all_metrics_per_category) - mask_metrics.update(mask_per_category_ap) - mask_metrics = {'DetectionMasks_'+ key: value - for key, value in mask_metrics.items()} - return mask_metrics - - def add_eval_dict(self, eval_dict): - """Observes an evaluation result dict for a single example. - - When executing eagerly, once all observations have been observed by this - method you can use `.evaluate()` to get the final metrics. - - When using `tf.estimator.Estimator` for evaluation this function is used by - `get_estimator_eval_metric_ops()` to construct the metric update op. - - Args: - eval_dict: A dictionary that holds tensors for evaluating an object - detection model, returned from - eval_util.result_dict_for_single_example(). - - Returns: - None when executing eagerly, or an update_op that can be used to update - the eval metrics in `tf.estimator.EstimatorSpec`. - """ - def update_op(image_id_batched, groundtruth_boxes_batched, - groundtruth_classes_batched, - groundtruth_instance_masks_batched, - groundtruth_is_crowd_batched, num_gt_boxes_per_image, - detection_scores_batched, detection_classes_batched, - detection_masks_batched, num_det_boxes_per_image, - original_image_spatial_shape): - """Update op for metrics.""" - - for (image_id, groundtruth_boxes, groundtruth_classes, - groundtruth_instance_masks, groundtruth_is_crowd, num_gt_box, - detection_scores, detection_classes, - detection_masks, num_det_box, original_image_shape) in zip( - image_id_batched, groundtruth_boxes_batched, - groundtruth_classes_batched, groundtruth_instance_masks_batched, - groundtruth_is_crowd_batched, num_gt_boxes_per_image, - detection_scores_batched, detection_classes_batched, - detection_masks_batched, num_det_boxes_per_image, - original_image_spatial_shape): - self.add_single_ground_truth_image_info( - image_id, { - 'groundtruth_boxes': - groundtruth_boxes[:num_gt_box], - 'groundtruth_classes': - groundtruth_classes[:num_gt_box], - 'groundtruth_instance_masks': - groundtruth_instance_masks[ - :num_gt_box, - :original_image_shape[0], - :original_image_shape[1]], - 'groundtruth_is_crowd': - groundtruth_is_crowd[:num_gt_box] - }) - self.add_single_detected_image_info( - image_id, { - 'detection_scores': detection_scores[:num_det_box], - 'detection_classes': detection_classes[:num_det_box], - 'detection_masks': detection_masks[ - :num_det_box, - :original_image_shape[0], - :original_image_shape[1]] - }) - - # Unpack items from the evaluation dictionary. - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - image_id = eval_dict[input_data_fields.key] - original_image_spatial_shape = eval_dict[ - input_data_fields.original_image_spatial_shape] - groundtruth_boxes = eval_dict[input_data_fields.groundtruth_boxes] - groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] - groundtruth_instance_masks = eval_dict[ - input_data_fields.groundtruth_instance_masks] - groundtruth_is_crowd = eval_dict.get( - input_data_fields.groundtruth_is_crowd, None) - num_gt_boxes_per_image = eval_dict.get( - input_data_fields.num_groundtruth_boxes, None) - detection_scores = eval_dict[detection_fields.detection_scores] - detection_classes = eval_dict[detection_fields.detection_classes] - detection_masks = eval_dict[detection_fields.detection_masks] - num_det_boxes_per_image = eval_dict.get(detection_fields.num_detections, - None) - - if groundtruth_is_crowd is None: - groundtruth_is_crowd = tf.zeros_like(groundtruth_classes, dtype=tf.bool) - - if not image_id.shape.as_list(): - # Apply a batch dimension to all tensors. - image_id = tf.expand_dims(image_id, 0) - groundtruth_boxes = tf.expand_dims(groundtruth_boxes, 0) - groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) - groundtruth_instance_masks = tf.expand_dims(groundtruth_instance_masks, 0) - groundtruth_is_crowd = tf.expand_dims(groundtruth_is_crowd, 0) - detection_scores = tf.expand_dims(detection_scores, 0) - detection_classes = tf.expand_dims(detection_classes, 0) - detection_masks = tf.expand_dims(detection_masks, 0) - - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.shape(groundtruth_boxes)[1:2] - else: - num_gt_boxes_per_image = tf.expand_dims(num_gt_boxes_per_image, 0) - - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.shape(detection_scores)[1:2] - else: - num_det_boxes_per_image = tf.expand_dims(num_det_boxes_per_image, 0) - else: - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.tile( - tf.shape(groundtruth_boxes)[1:2], - multiples=tf.shape(groundtruth_boxes)[0:1]) - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.tile( - tf.shape(detection_scores)[1:2], - multiples=tf.shape(detection_scores)[0:1]) - - return tf.py_func(update_op, [ - image_id, groundtruth_boxes, groundtruth_classes, - groundtruth_instance_masks, groundtruth_is_crowd, - num_gt_boxes_per_image, detection_scores, detection_classes, - detection_masks, num_det_boxes_per_image, original_image_spatial_shape - ], []) - - def get_estimator_eval_metric_ops(self, eval_dict): - """Returns a dictionary of eval metric ops. - - Note that once value_op is called, the detections and groundtruth added via - update_op are cleared. - - Args: - eval_dict: A dictionary that holds tensors for evaluating object detection - performance. For single-image evaluation, this dictionary may be - produced from eval_util.result_dict_for_single_example(). If multi-image - evaluation, `eval_dict` should contain the fields - 'num_groundtruth_boxes_per_image' and 'num_det_boxes_per_image' to - properly unpad the tensors from the batch. - - Returns: - a dictionary of metric names to tuple of value_op and update_op that can - be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all - update ops must be run together and similarly all value ops must be run - together to guarantee correct behaviour. - """ - update_op = self.add_eval_dict(eval_dict) - metric_names = ['DetectionMasks_Precision/mAP', - 'DetectionMasks_Precision/mAP@.50IOU', - 'DetectionMasks_Precision/mAP@.75IOU', - 'DetectionMasks_Precision/mAP (small)', - 'DetectionMasks_Precision/mAP (medium)', - 'DetectionMasks_Precision/mAP (large)', - 'DetectionMasks_Recall/AR@1', - 'DetectionMasks_Recall/AR@10', - 'DetectionMasks_Recall/AR@100', - 'DetectionMasks_Recall/AR@100 (small)', - 'DetectionMasks_Recall/AR@100 (medium)', - 'DetectionMasks_Recall/AR@100 (large)'] - if self._include_metrics_per_category: - for category_dict in self._categories: - metric_names.append('DetectionMasks_PerformanceByCategory/mAP/' + - category_dict['name']) - - def first_value_func(): - self._metrics = self.evaluate() - self.clear() - return np.float32(self._metrics[metric_names[0]]) - - def value_func_factory(metric_name): - def value_func(): - return np.float32(self._metrics[metric_name]) - return value_func - - # Ensure that the metrics are only evaluated once. - first_value_op = tf.py_func(first_value_func, [], tf.float32) - eval_metric_ops = {metric_names[0]: (first_value_op, update_op)} - with tf.control_dependencies([first_value_op]): - for metric_name in metric_names[1:]: - eval_metric_ops[metric_name] = (tf.py_func( - value_func_factory(metric_name), [], np.float32), update_op) - return eval_metric_ops - - -class CocoPanopticSegmentationEvaluator( - object_detection_evaluation.DetectionEvaluator): - """Class to evaluate PQ (panoptic quality) metric on COCO dataset. - - More details about this metric: https://arxiv.org/pdf/1801.00868.pdf. - """ - - def __init__(self, - categories, - include_metrics_per_category=False, - iou_threshold=0.5, - ioa_threshold=0.5): - """Constructor. - - Args: - categories: A list of dicts, each of which has the following keys - - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name e.g., 'cat', 'dog'. - include_metrics_per_category: If True, include metrics for each category. - iou_threshold: intersection-over-union threshold for mask matching (with - normal groundtruths). - ioa_threshold: intersection-over-area threshold for mask matching with - "is_crowd" groundtruths. - """ - super(CocoPanopticSegmentationEvaluator, self).__init__(categories) - self._groundtruth_masks = {} - self._groundtruth_class_labels = {} - self._groundtruth_is_crowd = {} - self._predicted_masks = {} - self._predicted_class_labels = {} - self._include_metrics_per_category = include_metrics_per_category - self._iou_threshold = iou_threshold - self._ioa_threshold = ioa_threshold - - def clear(self): - """Clears the state to prepare for a fresh evaluation.""" - self._groundtruth_masks.clear() - self._groundtruth_class_labels.clear() - self._groundtruth_is_crowd.clear() - self._predicted_masks.clear() - self._predicted_class_labels.clear() - - def add_single_ground_truth_image_info(self, image_id, groundtruth_dict): - """Adds groundtruth for a single image to be used for evaluation. - - If the image has already been added, a warning is logged, and groundtruth is - ignored. - - Args: - image_id: A unique string/integer identifier for the image. - groundtruth_dict: A dictionary containing - - InputDataFields.groundtruth_classes: integer numpy array of shape - [num_masks] containing 1-indexed groundtruth classes for the mask. - InputDataFields.groundtruth_instance_masks: uint8 numpy array of shape - [num_masks, image_height, image_width] containing groundtruth masks. - The elements of the array must be in {0, 1}. - InputDataFields.groundtruth_is_crowd (optional): integer numpy array of - shape [num_boxes] containing iscrowd flag for groundtruth boxes. - """ - - if image_id in self._groundtruth_masks: - tf.logging.warning( - 'Ignoring groundtruth with image %s, since it has already been ' - 'added to the ground truth database.', image_id) - return - - self._groundtruth_masks[image_id] = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_instance_masks] - self._groundtruth_class_labels[image_id] = groundtruth_dict[ - standard_fields.InputDataFields.groundtruth_classes] - groundtruth_is_crowd = groundtruth_dict.get( - standard_fields.InputDataFields.groundtruth_is_crowd) - # Drop groundtruth_is_crowd if empty tensor. - if groundtruth_is_crowd is not None and not groundtruth_is_crowd.size > 0: - groundtruth_is_crowd = None - if groundtruth_is_crowd is not None: - self._groundtruth_is_crowd[image_id] = groundtruth_is_crowd - - def add_single_detected_image_info(self, image_id, detections_dict): - """Adds detections for a single image to be used for evaluation. - - If a detection has already been added for this image id, a warning is - logged, and the detection is skipped. - - Args: - image_id: A unique string/integer identifier for the image. - detections_dict: A dictionary containing - - DetectionResultFields.detection_classes: integer numpy array of shape - [num_masks] containing 1-indexed detection classes for the masks. - DetectionResultFields.detection_masks: optional uint8 numpy array of - shape [num_masks, image_height, image_width] containing instance - masks. The elements of the array must be in {0, 1}. - - Raises: - ValueError: If results and groundtruth shape don't match. - """ - - if image_id not in self._groundtruth_masks: - raise ValueError('Missing groundtruth for image id: {}'.format(image_id)) - - detection_masks = detections_dict[ - standard_fields.DetectionResultFields.detection_masks] - self._predicted_masks[image_id] = detection_masks - self._predicted_class_labels[image_id] = detections_dict[ - standard_fields.DetectionResultFields.detection_classes] - groundtruth_mask_shape = self._groundtruth_masks[image_id].shape - if groundtruth_mask_shape[1:] != detection_masks.shape[1:]: - raise ValueError("The shape of results doesn't match groundtruth.") - - def evaluate(self): - """Evaluates the detection masks and returns a dictionary of coco metrics. - - Returns: - A dictionary holding - - - 1. summary_metric: - 'PanopticQuality@%.2fIOU': mean panoptic quality averaged over classes at - the required IOU. - 'SegmentationQuality@%.2fIOU': mean segmentation quality averaged over - classes at the required IOU. - 'RecognitionQuality@%.2fIOU': mean recognition quality averaged over - classes at the required IOU. - 'NumValidClasses': number of valid classes. A valid class should have at - least one normal (is_crowd=0) groundtruth mask or one predicted mask. - 'NumTotalClasses': number of total classes. - - 2. per_category_pq: if include_metrics_per_category is True, category - specific results with keys of the form: - 'PanopticQuality@%.2fIOU_ByCategory/category'. - """ - # Evaluate and accumulate the iou/tp/fp/fn. - sum_tp_iou, sum_num_tp, sum_num_fp, sum_num_fn = self._evaluate_all_masks() - # Compute PQ metric for each category and average over all classes. - mask_metrics = self._compute_panoptic_metrics(sum_tp_iou, sum_num_tp, - sum_num_fp, sum_num_fn) - return mask_metrics - - def get_estimator_eval_metric_ops(self, eval_dict): - """Returns a dictionary of eval metric ops. - - Note that once value_op is called, the detections and groundtruth added via - update_op are cleared. - - Args: - eval_dict: A dictionary that holds tensors for evaluating object detection - performance. For single-image evaluation, this dictionary may be - produced from eval_util.result_dict_for_single_example(). If multi-image - evaluation, `eval_dict` should contain the fields - 'num_gt_masks_per_image' and 'num_det_masks_per_image' to properly unpad - the tensors from the batch. - - Returns: - a dictionary of metric names to tuple of value_op and update_op that can - be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all - update ops must be run together and similarly all value ops must be run - together to guarantee correct behaviour. - """ - - def update_op(image_id_batched, groundtruth_classes_batched, - groundtruth_instance_masks_batched, - groundtruth_is_crowd_batched, num_gt_masks_per_image, - detection_classes_batched, detection_masks_batched, - num_det_masks_per_image): - """Update op for metrics.""" - for (image_id, groundtruth_classes, groundtruth_instance_masks, - groundtruth_is_crowd, num_gt_mask, detection_classes, - detection_masks, num_det_mask) in zip( - image_id_batched, groundtruth_classes_batched, - groundtruth_instance_masks_batched, groundtruth_is_crowd_batched, - num_gt_masks_per_image, detection_classes_batched, - detection_masks_batched, num_det_masks_per_image): - - self.add_single_ground_truth_image_info( - image_id, { - 'groundtruth_classes': - groundtruth_classes[:num_gt_mask], - 'groundtruth_instance_masks': - groundtruth_instance_masks[:num_gt_mask], - 'groundtruth_is_crowd': - groundtruth_is_crowd[:num_gt_mask] - }) - self.add_single_detected_image_info( - image_id, { - 'detection_classes': detection_classes[:num_det_mask], - 'detection_masks': detection_masks[:num_det_mask] - }) - - # Unpack items from the evaluation dictionary. - (image_id, groundtruth_classes, groundtruth_instance_masks, - groundtruth_is_crowd, num_gt_masks_per_image, detection_classes, - detection_masks, num_det_masks_per_image - ) = self._unpack_evaluation_dictionary_items(eval_dict) - - update_op = tf.py_func(update_op, [ - image_id, groundtruth_classes, groundtruth_instance_masks, - groundtruth_is_crowd, num_gt_masks_per_image, detection_classes, - detection_masks, num_det_masks_per_image - ], []) - - metric_names = [ - 'PanopticQuality@%.2fIOU' % self._iou_threshold, - 'SegmentationQuality@%.2fIOU' % self._iou_threshold, - 'RecognitionQuality@%.2fIOU' % self._iou_threshold - ] - if self._include_metrics_per_category: - for category_dict in self._categories: - metric_names.append('PanopticQuality@%.2fIOU_ByCategory/%s' % - (self._iou_threshold, category_dict['name'])) - - def first_value_func(): - self._metrics = self.evaluate() - self.clear() - return np.float32(self._metrics[metric_names[0]]) - - def value_func_factory(metric_name): - - def value_func(): - return np.float32(self._metrics[metric_name]) - - return value_func - - # Ensure that the metrics are only evaluated once. - first_value_op = tf.py_func(first_value_func, [], tf.float32) - eval_metric_ops = {metric_names[0]: (first_value_op, update_op)} - with tf.control_dependencies([first_value_op]): - for metric_name in metric_names[1:]: - eval_metric_ops[metric_name] = (tf.py_func( - value_func_factory(metric_name), [], np.float32), update_op) - return eval_metric_ops - - def _evaluate_all_masks(self): - """Evaluate all masks and compute sum iou/TP/FP/FN.""" - - sum_num_tp = {category['id']: 0 for category in self._categories} - sum_num_fp = sum_num_tp.copy() - sum_num_fn = sum_num_tp.copy() - sum_tp_iou = sum_num_tp.copy() - - for image_id in self._groundtruth_class_labels: - # Separate normal and is_crowd groundtruth - crowd_gt_indices = self._groundtruth_is_crowd.get(image_id) - (normal_gt_masks, normal_gt_classes, crowd_gt_masks, - crowd_gt_classes) = self._separate_normal_and_crowd_labels( - crowd_gt_indices, self._groundtruth_masks[image_id], - self._groundtruth_class_labels[image_id]) - - # Mask matching to normal GT. - predicted_masks = self._predicted_masks[image_id] - predicted_class_labels = self._predicted_class_labels[image_id] - (overlaps, pred_matched, - gt_matched) = self._match_predictions_to_groundtruths( - predicted_masks, - predicted_class_labels, - normal_gt_masks, - normal_gt_classes, - self._iou_threshold, - is_crowd=False, - with_replacement=False) - - # Accumulate true positives. - for (class_id, is_matched, overlap) in zip(predicted_class_labels, - pred_matched, overlaps): - if is_matched: - sum_num_tp[class_id] += 1 - sum_tp_iou[class_id] += overlap - - # Accumulate false negatives. - for (class_id, is_matched) in zip(normal_gt_classes, gt_matched): - if not is_matched: - sum_num_fn[class_id] += 1 - - # Match remaining predictions to crowd gt. - remained_pred_indices = np.logical_not(pred_matched) - remained_pred_masks = predicted_masks[remained_pred_indices, :, :] - remained_pred_classes = predicted_class_labels[remained_pred_indices] - _, pred_matched, _ = self._match_predictions_to_groundtruths( - remained_pred_masks, - remained_pred_classes, - crowd_gt_masks, - crowd_gt_classes, - self._ioa_threshold, - is_crowd=True, - with_replacement=True) - - # Accumulate false positives - for (class_id, is_matched) in zip(remained_pred_classes, pred_matched): - if not is_matched: - sum_num_fp[class_id] += 1 - return sum_tp_iou, sum_num_tp, sum_num_fp, sum_num_fn - - def _compute_panoptic_metrics(self, sum_tp_iou, sum_num_tp, sum_num_fp, - sum_num_fn): - """Compute PQ metric for each category and average over all classes. - - Args: - sum_tp_iou: dict, summed true positive intersection-over-union (IoU) for - each class, keyed by class_id. - sum_num_tp: the total number of true positives for each class, keyed by - class_id. - sum_num_fp: the total number of false positives for each class, keyed by - class_id. - sum_num_fn: the total number of false negatives for each class, keyed by - class_id. - - Returns: - mask_metrics: a dictionary containing averaged metrics over all classes, - and per-category metrics if required. - """ - mask_metrics = {} - sum_pq = 0 - sum_sq = 0 - sum_rq = 0 - num_valid_classes = 0 - for category in self._categories: - class_id = category['id'] - (panoptic_quality, segmentation_quality, - recognition_quality) = self._compute_panoptic_metrics_single_class( - sum_tp_iou[class_id], sum_num_tp[class_id], sum_num_fp[class_id], - sum_num_fn[class_id]) - if panoptic_quality is not None: - sum_pq += panoptic_quality - sum_sq += segmentation_quality - sum_rq += recognition_quality - num_valid_classes += 1 - if self._include_metrics_per_category: - mask_metrics['PanopticQuality@%.2fIOU_ByCategory/%s' % - (self._iou_threshold, - category['name'])] = panoptic_quality - mask_metrics['PanopticQuality@%.2fIOU' % - self._iou_threshold] = sum_pq / num_valid_classes - mask_metrics['SegmentationQuality@%.2fIOU' % - self._iou_threshold] = sum_sq / num_valid_classes - mask_metrics['RecognitionQuality@%.2fIOU' % - self._iou_threshold] = sum_rq / num_valid_classes - mask_metrics['NumValidClasses'] = num_valid_classes - mask_metrics['NumTotalClasses'] = len(self._categories) - return mask_metrics - - def _compute_panoptic_metrics_single_class(self, sum_tp_iou, num_tp, num_fp, - num_fn): - """Compute panoptic metrics: panoptic/segmentation/recognition quality. - - More computation details in https://arxiv.org/pdf/1801.00868.pdf. - Args: - sum_tp_iou: summed true positive intersection-over-union (IoU) for a - specific class. - num_tp: the total number of true positives for a specific class. - num_fp: the total number of false positives for a specific class. - num_fn: the total number of false negatives for a specific class. - - Returns: - panoptic_quality: sum_tp_iou / (num_tp + 0.5*num_fp + 0.5*num_fn). - segmentation_quality: sum_tp_iou / num_tp. - recognition_quality: num_tp / (num_tp + 0.5*num_fp + 0.5*num_fn). - """ - denominator = num_tp + 0.5 * num_fp + 0.5 * num_fn - # Calculate metric only if there is at least one GT or one prediction. - if denominator > 0: - recognition_quality = num_tp / denominator - if num_tp > 0: - segmentation_quality = sum_tp_iou / num_tp - else: - # If there is no TP for this category. - segmentation_quality = 0 - panoptic_quality = segmentation_quality * recognition_quality - return panoptic_quality, segmentation_quality, recognition_quality - else: - return None, None, None - - def _separate_normal_and_crowd_labels(self, crowd_gt_indices, - groundtruth_masks, groundtruth_classes): - """Separate normal and crowd groundtruth class_labels and masks. - - Args: - crowd_gt_indices: None or array of shape [num_groundtruths]. If None, all - groundtruths are treated as normal ones. - groundtruth_masks: array of shape [num_groundtruths, height, width]. - groundtruth_classes: array of shape [num_groundtruths]. - - Returns: - normal_gt_masks: array of shape [num_normal_groundtruths, height, width]. - normal_gt_classes: array of shape [num_normal_groundtruths]. - crowd_gt_masks: array of shape [num_crowd_groundtruths, height, width]. - crowd_gt_classes: array of shape [num_crowd_groundtruths]. - Raises: - ValueError: if the shape of groundtruth classes doesn't match groundtruth - masks or if the shape of crowd_gt_indices. - """ - if groundtruth_masks.shape[0] != groundtruth_classes.shape[0]: - raise ValueError( - "The number of masks doesn't match the number of labels.") - if crowd_gt_indices is None: - # All gts are treated as normal - crowd_gt_indices = np.zeros(groundtruth_masks.shape, dtype=bool) - else: - if groundtruth_masks.shape[0] != crowd_gt_indices.shape[0]: - raise ValueError( - "The number of masks doesn't match the number of is_crowd labels.") - crowd_gt_indices = crowd_gt_indices.astype(bool) - normal_gt_indices = np.logical_not(crowd_gt_indices) - if normal_gt_indices.size: - normal_gt_masks = groundtruth_masks[normal_gt_indices, :, :] - normal_gt_classes = groundtruth_classes[normal_gt_indices] - crowd_gt_masks = groundtruth_masks[crowd_gt_indices, :, :] - crowd_gt_classes = groundtruth_classes[crowd_gt_indices] - else: - # No groundtruths available, groundtruth_masks.shape = (0, h, w) - normal_gt_masks = groundtruth_masks - normal_gt_classes = groundtruth_classes - crowd_gt_masks = groundtruth_masks - crowd_gt_classes = groundtruth_classes - return normal_gt_masks, normal_gt_classes, crowd_gt_masks, crowd_gt_classes - - def _match_predictions_to_groundtruths(self, - predicted_masks, - predicted_classes, - groundtruth_masks, - groundtruth_classes, - matching_threshold, - is_crowd=False, - with_replacement=False): - """Match the predicted masks to groundtruths. - - Args: - predicted_masks: array of shape [num_predictions, height, width]. - predicted_classes: array of shape [num_predictions]. - groundtruth_masks: array of shape [num_groundtruths, height, width]. - groundtruth_classes: array of shape [num_groundtruths]. - matching_threshold: if the overlap between a prediction and a groundtruth - is larger than this threshold, the prediction is true positive. - is_crowd: whether the groundtruths are crowd annotation or not. If True, - use intersection over area (IoA) as the overlapping metric; otherwise - use intersection over union (IoU). - with_replacement: whether a groundtruth can be matched to multiple - predictions. By default, for normal groundtruths, only 1-1 matching is - allowed for normal groundtruths; for crowd groundtruths, 1-to-many must - be allowed. - - Returns: - best_overlaps: array of shape [num_predictions]. Values representing the - IoU - or IoA with best matched groundtruth. - pred_matched: array of shape [num_predictions]. Boolean value representing - whether the ith prediction is matched to a groundtruth. - gt_matched: array of shape [num_groundtruth]. Boolean value representing - whether the ith groundtruth is matched to a prediction. - Raises: - ValueError: if the shape of groundtruth/predicted masks doesn't match - groundtruth/predicted classes. - """ - if groundtruth_masks.shape[0] != groundtruth_classes.shape[0]: - raise ValueError( - "The number of GT masks doesn't match the number of labels.") - if predicted_masks.shape[0] != predicted_classes.shape[0]: - raise ValueError( - "The number of predicted masks doesn't match the number of labels.") - gt_matched = np.zeros(groundtruth_classes.shape, dtype=bool) - pred_matched = np.zeros(predicted_classes.shape, dtype=bool) - best_overlaps = np.zeros(predicted_classes.shape) - for pid in range(predicted_classes.shape[0]): - best_overlap = 0 - matched_gt_id = -1 - for gid in range(groundtruth_classes.shape[0]): - if predicted_classes[pid] == groundtruth_classes[gid]: - if (not with_replacement) and gt_matched[gid]: - continue - if not is_crowd: - overlap = np_mask_ops.iou(predicted_masks[pid:pid + 1], - groundtruth_masks[gid:gid + 1])[0, 0] - else: - overlap = np_mask_ops.ioa(groundtruth_masks[gid:gid + 1], - predicted_masks[pid:pid + 1])[0, 0] - if overlap >= matching_threshold and overlap > best_overlap: - matched_gt_id = gid - best_overlap = overlap - if matched_gt_id >= 0: - gt_matched[matched_gt_id] = True - pred_matched[pid] = True - best_overlaps[pid] = best_overlap - return best_overlaps, pred_matched, gt_matched - - def _unpack_evaluation_dictionary_items(self, eval_dict): - """Unpack items from the evaluation dictionary.""" - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - image_id = eval_dict[input_data_fields.key] - groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] - groundtruth_instance_masks = eval_dict[ - input_data_fields.groundtruth_instance_masks] - groundtruth_is_crowd = eval_dict.get(input_data_fields.groundtruth_is_crowd, - None) - num_gt_masks_per_image = eval_dict.get( - input_data_fields.num_groundtruth_boxes, None) - detection_classes = eval_dict[detection_fields.detection_classes] - detection_masks = eval_dict[detection_fields.detection_masks] - num_det_masks_per_image = eval_dict.get(detection_fields.num_detections, - None) - if groundtruth_is_crowd is None: - groundtruth_is_crowd = tf.zeros_like(groundtruth_classes, dtype=tf.bool) - - if not image_id.shape.as_list(): - # Apply a batch dimension to all tensors. - image_id = tf.expand_dims(image_id, 0) - groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) - groundtruth_instance_masks = tf.expand_dims(groundtruth_instance_masks, 0) - groundtruth_is_crowd = tf.expand_dims(groundtruth_is_crowd, 0) - detection_classes = tf.expand_dims(detection_classes, 0) - detection_masks = tf.expand_dims(detection_masks, 0) - - if num_gt_masks_per_image is None: - num_gt_masks_per_image = tf.shape(groundtruth_classes)[1:2] - else: - num_gt_masks_per_image = tf.expand_dims(num_gt_masks_per_image, 0) - - if num_det_masks_per_image is None: - num_det_masks_per_image = tf.shape(detection_classes)[1:2] - else: - num_det_masks_per_image = tf.expand_dims(num_det_masks_per_image, 0) - else: - if num_gt_masks_per_image is None: - num_gt_masks_per_image = tf.tile( - tf.shape(groundtruth_classes)[1:2], - multiples=tf.shape(groundtruth_classes)[0:1]) - if num_det_masks_per_image is None: - num_det_masks_per_image = tf.tile( - tf.shape(detection_classes)[1:2], - multiples=tf.shape(detection_classes)[0:1]) - return (image_id, groundtruth_classes, groundtruth_instance_masks, - groundtruth_is_crowd, num_gt_masks_per_image, detection_classes, - detection_masks, num_det_masks_per_image) diff --git a/research/object_detection/metrics/coco_evaluation_test.py b/research/object_detection/metrics/coco_evaluation_test.py deleted file mode 100644 index 8cfb3ee5a19..00000000000 --- a/research/object_detection/metrics/coco_evaluation_test.py +++ /dev/null @@ -1,2106 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for tensorflow_models.object_detection.metrics.coco_evaluation.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -import numpy as np -import tensorflow.compat.v1 as tf -from object_detection.core import standard_fields -from object_detection.metrics import coco_evaluation -from object_detection.utils import tf_version - - -def _get_categories_list(): - return [{ - 'id': 1, - 'name': 'person' - }, { - 'id': 2, - 'name': 'dog' - }, { - 'id': 3, - 'name': 'cat' - }] - - -def _get_category_keypoints_dict(): - return { - 'person': [{ - 'id': 0, - 'name': 'left_eye' - }, { - 'id': 3, - 'name': 'right_eye' - }], - 'dog': [{ - 'id': 1, - 'name': 'tail_start' - }, { - 'id': 2, - 'name': 'mouth' - }] - } - - -class CocoDetectionEvaluationTest(tf.test.TestCase): - - def testGetOneMAPWithMatchingGroundtruthAndDetections(self): - """Tests that mAP is calculated correctly on GT and Detections.""" - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: np.array([1]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image2', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.InputDataFields.groundtruth_classes: np.array([1]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image2', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image3', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[25., 25., 50., 50.]]), - standard_fields.InputDataFields.groundtruth_classes: np.array([1]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image3', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[25., 25., 50., 50.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsSkipCrowd(self): - """Tests computing mAP with is_crowd GT boxes skipped.""" - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.], [99., 99., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1, 2]), - standard_fields.InputDataFields.groundtruth_is_crowd: - np.array([0, 1]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsEmptyCrowd(self): - """Tests computing mAP with empty is_crowd array passed in.""" - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_is_crowd: - np.array([]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - - def testRejectionOnDuplicateGroundtruth(self): - """Tests that groundtruth cannot be added more than once for an image.""" - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - # Add groundtruth - image_key1 = 'img1' - groundtruth_boxes1 = np.array([[0, 0, 1, 1], [0, 0, 2, 2], [0, 0, 3, 3]], - dtype=float) - groundtruth_class_labels1 = np.array([1, 3, 1], dtype=int) - coco_evaluator.add_single_ground_truth_image_info(image_key1, { - standard_fields.InputDataFields.groundtruth_boxes: - groundtruth_boxes1, - standard_fields.InputDataFields.groundtruth_classes: - groundtruth_class_labels1 - }) - groundtruth_lists_len = len(coco_evaluator._groundtruth_list) - - # Add groundtruth with the same image id. - coco_evaluator.add_single_ground_truth_image_info(image_key1, { - standard_fields.InputDataFields.groundtruth_boxes: - groundtruth_boxes1, - standard_fields.InputDataFields.groundtruth_classes: - groundtruth_class_labels1 - }) - self.assertEqual(groundtruth_lists_len, - len(coco_evaluator._groundtruth_list)) - - def testRejectionOnDuplicateDetections(self): - """Tests that detections cannot be added more than once for an image.""" - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - # Add groundtruth - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[99., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: np.array([1]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - detections_lists_len = len(coco_evaluator._detection_boxes_list) - coco_evaluator.add_single_detected_image_info( - image_id='image1', # Note that this image id was previously added. - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - self.assertEqual(detections_lists_len, - len(coco_evaluator._detection_boxes_list)) - - def testExceptionRaisedWithMissingGroundtruth(self): - """Tests that exception is raised for detection with missing groundtruth.""" - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - with self.assertRaises(ValueError): - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - - -@unittest.skipIf(tf_version.is_tf2(), 'Only Supported in TF1.X') -class CocoEvaluationPyFuncTest(tf.test.TestCase): - - def _MatchingGroundtruthAndDetections(self, coco_evaluator): - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] - - with self.test_session() as sess: - sess.run(update_op, - feed_dict={ - image_id: 'image1', - groundtruth_boxes: np.array([[100., 100., 200., 200.]]), - groundtruth_classes: np.array([1]), - detection_boxes: np.array([[100., 100., 200., 200.]]), - detection_scores: np.array([.8]), - detection_classes: np.array([1]) - }) - sess.run(update_op, - feed_dict={ - image_id: 'image2', - groundtruth_boxes: np.array([[50., 50., 100., 100.]]), - groundtruth_classes: np.array([3]), - detection_boxes: np.array([[50., 50., 100., 100.]]), - detection_scores: np.array([.7]), - detection_classes: np.array([3]) - }) - sess.run(update_op, - feed_dict={ - image_id: 'image3', - groundtruth_boxes: np.array([[25., 25., 50., 50.]]), - groundtruth_classes: np.array([2]), - detection_boxes: np.array([[25., 25., 50., 50.]]), - detection_scores: np.array([.9]), - detection_classes: np.array([2]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._detection_boxes_list) - self.assertFalse(coco_evaluator._image_ids) - - def testGetOneMAPWithMatchingGroundtruthAndDetections(self): - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - self._MatchingGroundtruthAndDetections(coco_evaluator) - - # Configured to skip unmatched detector predictions with - # groundtruth_labeled_classes, but reverts to fully-labeled eval since there - # are no groundtruth_labeled_classes set. - def testGetMAPWithSkipUnmatchedPredictionsIgnoreGrountruthLabeledClasses( - self): - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list(), skip_predictions_for_unlabeled_class=True) - self._MatchingGroundtruthAndDetections(coco_evaluator) - - # Test skipping unmatched detector predictions with - # groundtruth_labeled_classes. - def testGetMAPWithSkipUnmatchedPredictions(self): - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list(), skip_predictions_for_unlabeled_class=True) - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - groundtruth_labeled_classes = tf.placeholder(tf.float32, shape=(None)) - detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: - image_id, - input_data_fields.groundtruth_boxes: - groundtruth_boxes, - input_data_fields.groundtruth_classes: - groundtruth_classes, - input_data_fields.groundtruth_labeled_classes: - groundtruth_labeled_classes, - detection_fields.detection_boxes: - detection_boxes, - detection_fields.detection_scores: - detection_scores, - detection_fields.detection_classes: - detection_classes - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: - 'image1', - groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - groundtruth_classes: - np.array([1]), - # Only class 1 is exhaustively labeled for image1. - groundtruth_labeled_classes: - np.array([0., 1., 0., 0.]), - detection_boxes: - np.array([[100., 100., 200., 200.], [100., 100., 200., - 200.]]), - detection_scores: - np.array([.8, .95]), - detection_classes: - np.array([1, 2]) - }) - sess.run( - update_op, - feed_dict={ - image_id: 'image2', - groundtruth_boxes: np.array([[50., 50., 100., 100.]]), - groundtruth_classes: np.array([3]), - groundtruth_labeled_classes: np.array([0., 0., 0., 1.]), - detection_boxes: np.array([[50., 50., 100., 100.]]), - detection_scores: np.array([.7]), - detection_classes: np.array([3]) - }) - sess.run( - update_op, - feed_dict={ - image_id: 'image3', - groundtruth_boxes: np.array([[25., 25., 50., 50.]]), - groundtruth_classes: np.array([2]), - groundtruth_labeled_classes: np.array([0., 0., 1., 0.]), - detection_boxes: np.array([[25., 25., 50., 50.]]), - detection_scores: np.array([.9]), - detection_classes: np.array([2]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._detection_boxes_list) - self.assertFalse(coco_evaluator._image_ids) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsIsAnnotated(self): - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - is_annotated = tf.placeholder(tf.bool, shape=()) - detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - 'is_annotated': is_annotated, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] - - with self.test_session() as sess: - sess.run(update_op, - feed_dict={ - image_id: 'image1', - groundtruth_boxes: np.array([[100., 100., 200., 200.]]), - groundtruth_classes: np.array([1]), - is_annotated: True, - detection_boxes: np.array([[100., 100., 200., 200.]]), - detection_scores: np.array([.8]), - detection_classes: np.array([1]) - }) - sess.run(update_op, - feed_dict={ - image_id: 'image2', - groundtruth_boxes: np.array([[50., 50., 100., 100.]]), - groundtruth_classes: np.array([3]), - is_annotated: True, - detection_boxes: np.array([[50., 50., 100., 100.]]), - detection_scores: np.array([.7]), - detection_classes: np.array([3]) - }) - sess.run(update_op, - feed_dict={ - image_id: 'image3', - groundtruth_boxes: np.array([[25., 25., 50., 50.]]), - groundtruth_classes: np.array([2]), - is_annotated: True, - detection_boxes: np.array([[25., 25., 50., 50.]]), - detection_scores: np.array([.9]), - detection_classes: np.array([2]) - }) - sess.run(update_op, - feed_dict={ - image_id: 'image4', - groundtruth_boxes: np.zeros((0, 4)), - groundtruth_classes: np.zeros((0)), - is_annotated: False, # Note that this image isn't annotated. - detection_boxes: np.array([[25., 25., 50., 50.], - [25., 25., 70., 50.], - [25., 25., 80., 50.], - [25., 25., 90., 50.]]), - detection_scores: np.array([0.6, 0.7, 0.8, 0.9]), - detection_classes: np.array([1, 2, 2, 3]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._detection_boxes_list) - self.assertFalse(coco_evaluator._image_ids) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsPadded(self): - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: - 'image1', - groundtruth_boxes: - np.array([[100., 100., 200., 200.], [-1, -1, -1, -1]]), - groundtruth_classes: - np.array([1, -1]), - detection_boxes: - np.array([[100., 100., 200., 200.], [0., 0., 0., 0.]]), - detection_scores: - np.array([.8, 0.]), - detection_classes: - np.array([1, -1]) - }) - sess.run( - update_op, - feed_dict={ - image_id: - 'image2', - groundtruth_boxes: - np.array([[50., 50., 100., 100.], [-1, -1, -1, -1]]), - groundtruth_classes: - np.array([3, -1]), - detection_boxes: - np.array([[50., 50., 100., 100.], [0., 0., 0., 0.]]), - detection_scores: - np.array([.7, 0.]), - detection_classes: - np.array([3, -1]) - }) - sess.run( - update_op, - feed_dict={ - image_id: - 'image3', - groundtruth_boxes: - np.array([[25., 25., 50., 50.], [10., 10., 15., 15.]]), - groundtruth_classes: - np.array([2, 2]), - detection_boxes: - np.array([[25., 25., 50., 50.], [10., 10., 15., 15.]]), - detection_scores: - np.array([.95, .9]), - detection_classes: - np.array([2, 2]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 0.83333331) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._detection_boxes_list) - self.assertFalse(coco_evaluator._image_ids) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsBatched(self): - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - batch_size = 3 - image_id = tf.placeholder(tf.string, shape=(batch_size)) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - detection_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(batch_size, None)) - detection_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] - - with self.test_session() as sess: - sess.run(update_op, - feed_dict={ - image_id: ['image1', 'image2', 'image3'], - groundtruth_boxes: np.array([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]]), - groundtruth_classes: np.array([[1], [3], [2]]), - detection_boxes: np.array([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]]), - detection_scores: np.array([[.8], [.7], [.9]]), - detection_classes: np.array([[1], [3], [2]]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._detection_boxes_list) - self.assertFalse(coco_evaluator._image_ids) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsPaddedBatches(self): - coco_evaluator = coco_evaluation.CocoDetectionEvaluator( - _get_categories_list()) - batch_size = 3 - image_id = tf.placeholder(tf.string, shape=(batch_size)) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - num_gt_boxes_per_image = tf.placeholder(tf.int32, shape=(None)) - detection_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(batch_size, None)) - detection_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - num_det_boxes_per_image = tf.placeholder(tf.int32, shape=(None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - 'num_groundtruth_boxes_per_image': num_gt_boxes_per_image, - 'num_det_boxes_per_image': num_det_boxes_per_image - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionBoxes_Precision/mAP'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: ['image1', 'image2', 'image3'], - groundtruth_boxes: - np.array([[[100., 100., 200., 200.], [-1, -1, -1, -1]], - [[50., 50., 100., 100.], [-1, -1, -1, -1]], - [[25., 25., 50., 50.], [10., 10., 15., 15.]]]), - groundtruth_classes: - np.array([[1, -1], [3, -1], [2, 2]]), - num_gt_boxes_per_image: - np.array([1, 1, 2]), - detection_boxes: - np.array([[[100., 100., 200., 200.], - [0., 0., 0., 0.], - [0., 0., 0., 0.]], - [[50., 50., 100., 100.], - [0., 0., 0., 0.], - [0., 0., 0., 0.]], - [[25., 25., 50., 50.], - [10., 10., 15., 15.], - [10., 10., 15., 15.]]]), - detection_scores: - np.array([[.8, 0., 0.], [.7, 0., 0.], [.95, .9, 0.9]]), - detection_classes: - np.array([[1, -1, -1], [3, -1, -1], [2, 2, 2]]), - num_det_boxes_per_image: - np.array([1, 1, 3]), - }) - - # Check the number of bounding boxes added. - self.assertEqual(len(coco_evaluator._groundtruth_list), 4) - self.assertEqual(len(coco_evaluator._detection_boxes_list), 5) - - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@1'], 0.83333331) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionBoxes_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._detection_boxes_list) - self.assertFalse(coco_evaluator._image_ids) - - -class CocoKeypointEvaluationTest(tf.test.TestCase): - - def testGetOneMAPWithMatchingKeypoints(self): - """Tests that correct mAP for keypoints is calculated.""" - category_keypoint_dict = _get_category_keypoints_dict() - coco_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]]), - standard_fields.InputDataFields.groundtruth_keypoint_visibilities: - np.array([[2, 0, 0, 2]]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_keypoints: - np.array([[[150., 160.], [1., 2.], [3., 4.], [170., 180.]]]) - }) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image2', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[75., 76.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [77., 78.]]]), - standard_fields.InputDataFields.groundtruth_keypoint_visibilities: - np.array([[2, 0, 0, 2]]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image2', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_keypoints: - np.array([[[75., 76.], [5., 6.], [7., 8.], [77., 78.]]]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - 1.0) - - def testGroundtruthListValues(self): - category_keypoint_dict = _get_category_keypoints_dict() - coco_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), float('nan')], - [float('nan'), float('nan')], [170., 180.]]]), - standard_fields.InputDataFields.groundtruth_keypoint_visibilities: - np.array([[2, 0, 0, 2]]), - standard_fields.InputDataFields.groundtruth_area: np.array([15.]) - }) - gt_dict = coco_evaluator._groundtruth_list[0] - self.assertEqual(gt_dict['id'], 1) - self.assertAlmostEqual(gt_dict['bbox'], [100.0, 100.0, 100.0, 100.0]) - self.assertAlmostEqual( - gt_dict['keypoints'], [160.0, 150.0, 2, 180.0, 170.0, 2]) - self.assertEqual(gt_dict['num_keypoints'], 2) - self.assertAlmostEqual(gt_dict['area'], 15.0) - - def testKeypointVisibilitiesAreOptional(self): - """Tests that evaluator works when visibilities aren't provided.""" - category_keypoint_dict = _get_category_keypoints_dict() - coco_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_keypoints: - np.array([[[150., 160.], [1., 2.], [3., 4.], [170., 180.]]]) - }) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image2', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[75., 76.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [77., 78.]]]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image2', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_keypoints: - np.array([[[75., 76.], [5., 6.], [7., 8.], [77., 78.]]]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - 1.0) - - def testFiltersDetectionsFromOtherCategories(self): - """Tests that the evaluator ignores detections from other categories.""" - category_keypoint_dict = _get_category_keypoints_dict() - coco_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=2, category_keypoints=category_keypoint_dict['person'], - class_text='dog') - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[150., 160.], [170., 180.], [110., 120.], - [130., 140.]]]), - standard_fields.InputDataFields.groundtruth_keypoint_visibilities: - np.array([[2, 2, 2, 2]]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.9]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_keypoints: - np.array([[[150., 160.], [170., 180.], [110., 120.], - [130., 140.]]]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/dog'], - -1.0) - - def testHandlesUnlabeledKeypointData(self): - """Tests that the evaluator handles missing keypoints GT.""" - category_keypoint_dict = _get_category_keypoints_dict() - coco_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]]), - standard_fields.InputDataFields.groundtruth_keypoint_visibilities: - np.array([[0, 0, 0, 2]]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_keypoints: - np.array([[[50., 60.], [1., 2.], [3., 4.], [170., 180.]]]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - 1.0) - - def testIgnoresCrowdAnnotations(self): - """Tests that the evaluator ignores GT marked as crowd.""" - category_keypoint_dict = _get_category_keypoints_dict() - coco_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1]), - standard_fields.InputDataFields.groundtruth_is_crowd: - np.array([1]), - standard_fields.InputDataFields.groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]]), - standard_fields.InputDataFields.groundtruth_keypoint_visibilities: - np.array([[2, 0, 0, 2]]) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_keypoints: - np.array([[[150., 160.], [1., 2.], [3., 4.], [170., 180.]]]) - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - -1.0) - - -@unittest.skipIf(tf_version.is_tf2(), 'Only Supported in TF1.X') -class CocoKeypointEvaluationPyFuncTest(tf.test.TestCase): - - def testGetOneMAPWithMatchingKeypoints(self): - category_keypoint_dict = _get_category_keypoints_dict() - coco_keypoint_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - groundtruth_keypoints = tf.placeholder(tf.float32, shape=(None, 4, 2)) - detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - detection_keypoints = tf.placeholder(tf.float32, shape=(None, 4, 2)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_keypoints: groundtruth_keypoints, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_keypoints: detection_keypoints, - } - - eval_metric_ops = coco_keypoint_evaluator.get_estimator_eval_metric_ops( - eval_dict) - - _, update_op = eval_metric_ops['Keypoints_Precision/mAP ByCategory/person'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: - 'image1', - groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - groundtruth_classes: - np.array([1]), - groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]]), - detection_boxes: - np.array([[100., 100., 200., 200.]]), - detection_scores: - np.array([.8]), - detection_classes: - np.array([1]), - detection_keypoints: - np.array([[[150., 160.], [1., 2.], [3., 4.], [170., 180.]]]) - }) - sess.run( - update_op, - feed_dict={ - image_id: - 'image2', - groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - groundtruth_classes: - np.array([1]), - groundtruth_keypoints: - np.array([[[75., 76.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [77., 78.]]]), - detection_boxes: - np.array([[50., 50., 100., 100.]]), - detection_scores: - np.array([.7]), - detection_classes: - np.array([1]), - detection_keypoints: - np.array([[[75., 76.], [5., 6.], [7., 8.], [77., 78.]]]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.50IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.75IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (medium) ByCategory/person'], 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@1 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@10 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@100 ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (medium) ByCategory/person'], 1.0) - self.assertFalse(coco_keypoint_evaluator._groundtruth_list) - self.assertFalse(coco_keypoint_evaluator._detection_boxes_list) - self.assertFalse(coco_keypoint_evaluator._image_ids) - - def testGetOneMAPWithMatchingKeypointsAndVisibilities(self): - category_keypoint_dict = _get_category_keypoints_dict() - coco_keypoint_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - groundtruth_keypoints = tf.placeholder(tf.float32, shape=(None, 4, 2)) - groundtruth_keypoint_visibilities = tf.placeholder( - tf.float32, shape=(None, 4)) - detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - detection_keypoints = tf.placeholder(tf.float32, shape=(None, 4, 2)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: - image_id, - input_data_fields.groundtruth_boxes: - groundtruth_boxes, - input_data_fields.groundtruth_classes: - groundtruth_classes, - input_data_fields.groundtruth_keypoints: - groundtruth_keypoints, - input_data_fields.groundtruth_keypoint_visibilities: - groundtruth_keypoint_visibilities, - detection_fields.detection_boxes: - detection_boxes, - detection_fields.detection_scores: - detection_scores, - detection_fields.detection_classes: - detection_classes, - detection_fields.detection_keypoints: - detection_keypoints, - } - - eval_metric_ops = coco_keypoint_evaluator.get_estimator_eval_metric_ops( - eval_dict) - - _, update_op = eval_metric_ops['Keypoints_Precision/mAP ByCategory/person'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: - 'image1', - groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - groundtruth_classes: - np.array([1]), - groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]]), - groundtruth_keypoint_visibilities: - np.array([[0, 0, 0, 2]]), - detection_boxes: - np.array([[100., 100., 200., 200.]]), - detection_scores: - np.array([.8]), - detection_classes: - np.array([1]), - detection_keypoints: - np.array([[[50., 60.], [1., 2.], [3., 4.], [170., 180.]]]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.50IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.75IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (medium) ByCategory/person'], -1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@1 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@10 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@100 ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (medium) ByCategory/person'], -1.0) - self.assertFalse(coco_keypoint_evaluator._groundtruth_list) - self.assertFalse(coco_keypoint_evaluator._detection_boxes_list) - self.assertFalse(coco_keypoint_evaluator._image_ids) - - def testGetOneMAPWithMatchingKeypointsIsAnnotated(self): - category_keypoint_dict = _get_category_keypoints_dict() - coco_keypoint_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - groundtruth_keypoints = tf.placeholder(tf.float32, shape=(None, 4, 2)) - is_annotated = tf.placeholder(tf.bool, shape=()) - detection_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - detection_keypoints = tf.placeholder(tf.float32, shape=(None, 4, 2)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_keypoints: groundtruth_keypoints, - 'is_annotated': is_annotated, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_keypoints: detection_keypoints, - } - - eval_metric_ops = coco_keypoint_evaluator.get_estimator_eval_metric_ops( - eval_dict) - - _, update_op = eval_metric_ops['Keypoints_Precision/mAP ByCategory/person'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: - 'image1', - groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - groundtruth_classes: - np.array([1]), - groundtruth_keypoints: - np.array([[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]]), - is_annotated: - True, - detection_boxes: - np.array([[100., 100., 200., 200.]]), - detection_scores: - np.array([.8]), - detection_classes: - np.array([1]), - detection_keypoints: - np.array([[[150., 160.], [1., 2.], [3., 4.], [170., 180.]]]) - }) - sess.run( - update_op, - feed_dict={ - image_id: - 'image2', - groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - groundtruth_classes: - np.array([1]), - groundtruth_keypoints: - np.array([[[75., 76.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [77., 78.]]]), - is_annotated: - True, - detection_boxes: - np.array([[50., 50., 100., 100.]]), - detection_scores: - np.array([.7]), - detection_classes: - np.array([1]), - detection_keypoints: - np.array([[[75., 76.], [5., 6.], [7., 8.], [77., 78.]]]) - }) - sess.run( - update_op, - feed_dict={ - image_id: - 'image3', - groundtruth_boxes: - np.zeros((0, 4)), - groundtruth_classes: - np.zeros((0)), - groundtruth_keypoints: - np.zeros((0, 4, 2)), - is_annotated: - False, # Note that this image isn't annotated. - detection_boxes: - np.array([[25., 25., 50., 50.], [25., 25., 70., 50.], - [25., 25., 80., 50.], [25., 25., 90., 50.]]), - detection_scores: - np.array([0.6, 0.7, 0.8, 0.9]), - detection_classes: - np.array([1, 2, 2, 3]), - detection_keypoints: - np.array([[[0., 0.], [0., 0.], [0., 0.], [0., 0.]]]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.50IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.75IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (medium) ByCategory/person'], 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@1 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@10 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@100 ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (medium) ByCategory/person'], 1.0) - self.assertFalse(coco_keypoint_evaluator._groundtruth_list) - self.assertFalse(coco_keypoint_evaluator._detection_boxes_list) - self.assertFalse(coco_keypoint_evaluator._image_ids) - - def testGetOneMAPWithMatchingKeypointsBatched(self): - category_keypoint_dict = _get_category_keypoints_dict() - coco_keypoint_evaluator = coco_evaluation.CocoKeypointEvaluator( - category_id=1, category_keypoints=category_keypoint_dict['person'], - class_text='person') - batch_size = 2 - image_id = tf.placeholder(tf.string, shape=(batch_size)) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - groundtruth_keypoints = tf.placeholder( - tf.float32, shape=(batch_size, None, 4, 2)) - detection_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) - detection_scores = tf.placeholder(tf.float32, shape=(batch_size, None)) - detection_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - detection_keypoints = tf.placeholder( - tf.float32, shape=(batch_size, None, 4, 2)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_keypoints: groundtruth_keypoints, - detection_fields.detection_boxes: detection_boxes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_keypoints: detection_keypoints - } - - eval_metric_ops = coco_keypoint_evaluator.get_estimator_eval_metric_ops( - eval_dict) - - _, update_op = eval_metric_ops['Keypoints_Precision/mAP ByCategory/person'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: ['image1', 'image2'], - groundtruth_boxes: - np.array([[[100., 100., 200., 200.]], [[50., 50., 100., - 100.]]]), - groundtruth_classes: - np.array([[1], [3]]), - groundtruth_keypoints: - np.array([[[[150., 160.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [170., 180.]]], - [[[75., 76.], [float('nan'), - float('nan')], - [float('nan'), float('nan')], [77., 78.]]]]), - detection_boxes: - np.array([[[100., 100., 200., 200.]], [[50., 50., 100., - 100.]]]), - detection_scores: - np.array([[.8], [.7]]), - detection_classes: - np.array([[1], [3]]), - detection_keypoints: - np.array([[[[150., 160.], [1., 2.], [3., 4.], [170., 180.]]], - [[[75., 76.], [5., 6.], [7., 8.], [77., 78.]]]]) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['Keypoints_Precision/mAP ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.50IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP@.75IOU ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Precision/mAP (medium) ByCategory/person'], -1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@1 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@10 ByCategory/person'], - 1.0) - self.assertAlmostEqual(metrics['Keypoints_Recall/AR@100 ByCategory/person'], - 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (large) ByCategory/person'], 1.0) - self.assertAlmostEqual( - metrics['Keypoints_Recall/AR@100 (medium) ByCategory/person'], -1.0) - self.assertFalse(coco_keypoint_evaluator._groundtruth_list) - self.assertFalse(coco_keypoint_evaluator._detection_boxes_list) - self.assertFalse(coco_keypoint_evaluator._image_ids) - - -class CocoMaskEvaluationTest(tf.test.TestCase): - - def testGetOneMAPWithMatchingGroundtruthAndDetections(self): - coco_evaluator = coco_evaluation.CocoMaskEvaluator(_get_categories_list()) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: np.array([1]), - standard_fields.InputDataFields.groundtruth_instance_masks: - np.pad(np.ones([1, 100, 100], dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), mode='constant') - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[100., 100., 200., 200.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_masks: - np.pad(np.ones([1, 100, 100], dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), mode='constant') - }) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image2', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.InputDataFields.groundtruth_classes: np.array([1]), - standard_fields.InputDataFields.groundtruth_instance_masks: - np.pad(np.ones([1, 50, 50], dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), mode='constant') - }) - coco_evaluator.add_single_detected_image_info( - image_id='image2', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[50., 50., 100., 100.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_masks: - np.pad(np.ones([1, 50, 50], dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), mode='constant') - }) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image3', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[25., 25., 50., 50.]]), - standard_fields.InputDataFields.groundtruth_classes: np.array([1]), - standard_fields.InputDataFields.groundtruth_instance_masks: - np.pad(np.ones([1, 25, 25], dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), mode='constant') - }) - coco_evaluator.add_single_detected_image_info( - image_id='image3', - detections_dict={ - standard_fields.DetectionResultFields.detection_boxes: - np.array([[25., 25., 50., 50.]]), - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_masks: - # The value of 5 is equivalent to 1, since masks will be - # thresholded and binarized before evaluation. - np.pad(5 * np.ones([1, 25, 25], dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), mode='constant') - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP'], 1.0) - coco_evaluator.clear() - self.assertFalse(coco_evaluator._image_id_to_mask_shape_map) - self.assertFalse(coco_evaluator._image_ids_with_detections) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._detection_masks_list) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsSkipCrowd(self): - """Tests computing mAP with is_crowd GT boxes skipped.""" - coco_evaluator = coco_evaluation.CocoMaskEvaluator( - _get_categories_list()) - coco_evaluator.add_single_ground_truth_image_info( - image_id='image1', - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.], [99., 99., 200., 200.]]), - standard_fields.InputDataFields.groundtruth_classes: - np.array([1, 2]), - standard_fields.InputDataFields.groundtruth_is_crowd: - np.array([0, 1]), - standard_fields.InputDataFields.groundtruth_instance_masks: - np.concatenate( - [np.pad(np.ones([1, 100, 100], dtype=np.uint8), - ((0, 0), (100, 56), (100, 56)), mode='constant'), - np.pad(np.ones([1, 101, 101], dtype=np.uint8), - ((0, 0), (99, 56), (99, 56)), mode='constant')], - axis=0) - }) - coco_evaluator.add_single_detected_image_info( - image_id='image1', - detections_dict={ - standard_fields.DetectionResultFields.detection_scores: - np.array([.8]), - standard_fields.DetectionResultFields.detection_classes: - np.array([1]), - standard_fields.DetectionResultFields.detection_masks: - np.pad(np.ones([1, 100, 100], dtype=np.uint8), - ((0, 0), (100, 56), (100, 56)), mode='constant') - }) - metrics = coco_evaluator.evaluate() - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP'], 1.0) - - -@unittest.skipIf(tf_version.is_tf2(), 'Only Supported in TF1.X') -class CocoMaskEvaluationPyFuncTest(tf.test.TestCase): - - def testAddEvalDict(self): - coco_evaluator = coco_evaluation.CocoMaskEvaluator(_get_categories_list()) - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - groundtruth_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) - original_image_spatial_shape = tf.placeholder(tf.int32, shape=(None, 2)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - detection_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_instance_masks: groundtruth_masks, - input_data_fields.original_image_spatial_shape: - original_image_spatial_shape, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_masks: detection_masks, - } - update_op = coco_evaluator.add_eval_dict(eval_dict) - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: - 'image1', - groundtruth_boxes: - np.array([[100., 100., 200., 200.], [50., 50., 100., 100.]]), - groundtruth_classes: - np.array([1, 2]), - groundtruth_masks: - np.stack([ - np.pad( - np.ones([100, 100], dtype=np.uint8), ((10, 10), - (10, 10)), - mode='constant'), - np.pad( - np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), - mode='constant') - ]), - original_image_spatial_shape: np.array([[120, 120]]), - detection_scores: - np.array([.9, .8]), - detection_classes: - np.array([2, 1]), - detection_masks: - np.stack([ - np.pad( - np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), - mode='constant'), - np.pad( - np.ones([100, 100], dtype=np.uint8), ((10, 10), - (10, 10)), - mode='constant'), - ]) - }) - self.assertLen(coco_evaluator._groundtruth_list, 2) - self.assertLen(coco_evaluator._detection_masks_list, 2) - - def testGetOneMAPWithMatchingGroundtruthAndDetections(self): - coco_evaluator = coco_evaluation.CocoMaskEvaluator(_get_categories_list()) - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(None)) - groundtruth_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) - original_image_spatial_shape = tf.placeholder(tf.int32, shape=(None, 2)) - detection_scores = tf.placeholder(tf.float32, shape=(None)) - detection_classes = tf.placeholder(tf.float32, shape=(None)) - detection_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_instance_masks: groundtruth_masks, - input_data_fields.original_image_spatial_shape: - original_image_spatial_shape, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_masks: detection_masks, - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionMasks_Precision/mAP'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: - 'image1', - groundtruth_boxes: - np.array([[100., 100., 200., 200.], [50., 50., 100., 100.]]), - groundtruth_classes: - np.array([1, 2]), - groundtruth_masks: - np.stack([ - np.pad( - np.ones([100, 100], dtype=np.uint8), ((10, 10), - (10, 10)), - mode='constant'), - np.pad( - np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), - mode='constant') - ]), - original_image_spatial_shape: np.array([[120, 120], [120, 120]]), - detection_scores: - np.array([.9, .8]), - detection_classes: - np.array([2, 1]), - detection_masks: - np.stack([ - np.pad( - np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), - mode='constant'), - np.pad( - np.ones([100, 100], dtype=np.uint8), ((10, 10), - (10, 10)), - mode='constant'), - ]) - }) - sess.run(update_op, - feed_dict={ - image_id: 'image2', - groundtruth_boxes: np.array([[50., 50., 100., 100.]]), - groundtruth_classes: np.array([1]), - groundtruth_masks: np.pad(np.ones([1, 50, 50], - dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), - mode='constant'), - original_image_spatial_shape: np.array([[70, 70]]), - detection_scores: np.array([.8]), - detection_classes: np.array([1]), - detection_masks: np.pad(np.ones([1, 50, 50], dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), - mode='constant') - }) - sess.run(update_op, - feed_dict={ - image_id: 'image3', - groundtruth_boxes: np.array([[25., 25., 50., 50.]]), - groundtruth_classes: np.array([1]), - groundtruth_masks: np.pad(np.ones([1, 25, 25], - dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), - mode='constant'), - original_image_spatial_shape: np.array([[45, 45]]), - detection_scores: np.array([.8]), - detection_classes: np.array([1]), - detection_masks: np.pad(np.ones([1, 25, 25], - dtype=np.uint8), - ((0, 0), (10, 10), (10, 10)), - mode='constant') - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@1'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._image_ids_with_detections) - self.assertFalse(coco_evaluator._image_id_to_mask_shape_map) - self.assertFalse(coco_evaluator._detection_masks_list) - - def testGetOneMAPWithMatchingGroundtruthAndDetectionsBatched(self): - coco_evaluator = coco_evaluation.CocoMaskEvaluator(_get_categories_list()) - batch_size = 3 - image_id = tf.placeholder(tf.string, shape=(batch_size)) - groundtruth_boxes = tf.placeholder(tf.float32, shape=(batch_size, None, 4)) - groundtruth_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - groundtruth_masks = tf.placeholder( - tf.uint8, shape=(batch_size, None, None, None)) - original_image_spatial_shape = tf.placeholder(tf.int32, shape=(None, 2)) - detection_scores = tf.placeholder(tf.float32, shape=(batch_size, None)) - detection_classes = tf.placeholder(tf.float32, shape=(batch_size, None)) - detection_masks = tf.placeholder( - tf.uint8, shape=(batch_size, None, None, None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_instance_masks: groundtruth_masks, - input_data_fields.original_image_spatial_shape: - original_image_spatial_shape, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_masks: detection_masks, - } - - eval_metric_ops = coco_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['DetectionMasks_Precision/mAP'] - - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: ['image1', 'image2', 'image3'], - groundtruth_boxes: - np.array([[[100., 100., 200., 200.]], - [[50., 50., 100., 100.]], - [[25., 25., 50., 50.]]]), - groundtruth_classes: - np.array([[1], [1], [1]]), - groundtruth_masks: - np.stack([ - np.pad( - np.ones([1, 100, 100], dtype=np.uint8), - ((0, 0), (0, 0), (0, 0)), - mode='constant'), - np.pad( - np.ones([1, 50, 50], dtype=np.uint8), - ((0, 0), (25, 25), (25, 25)), - mode='constant'), - np.pad( - np.ones([1, 25, 25], dtype=np.uint8), - ((0, 0), (37, 38), (37, 38)), - mode='constant') - ], - axis=0), - original_image_spatial_shape: np.array( - [[100, 100], [100, 100], [100, 100]]), - detection_scores: - np.array([[.8], [.8], [.8]]), - detection_classes: - np.array([[1], [1], [1]]), - detection_masks: - np.stack([ - np.pad( - np.ones([1, 100, 100], dtype=np.uint8), - ((0, 0), (0, 0), (0, 0)), - mode='constant'), - np.pad( - np.ones([1, 50, 50], dtype=np.uint8), - ((0, 0), (25, 25), (25, 25)), - mode='constant'), - np.pad( - np.ones([1, 25, 25], dtype=np.uint8), - ((0, 0), (37, 38), (37, 38)), - mode='constant') - ], - axis=0) - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP@.50IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP@.75IOU'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Precision/mAP (small)'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@1'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@10'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (large)'], 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (medium)'], - 1.0) - self.assertAlmostEqual(metrics['DetectionMasks_Recall/AR@100 (small)'], 1.0) - self.assertFalse(coco_evaluator._groundtruth_list) - self.assertFalse(coco_evaluator._image_ids_with_detections) - self.assertFalse(coco_evaluator._image_id_to_mask_shape_map) - self.assertFalse(coco_evaluator._detection_masks_list) - - -def _get_panoptic_test_data(): - # image1 contains 3 people in gt, (2 normal annotation and 1 "is_crowd" - # annotation), and 3 people in prediction. - gt_masks1 = np.zeros((3, 50, 50), dtype=np.uint8) - result_masks1 = np.zeros((3, 50, 50), dtype=np.uint8) - gt_masks1[0, 10:20, 20:30] = 1 - result_masks1[0, 10:18, 20:30] = 1 - gt_masks1[1, 25:30, 25:35] = 1 - result_masks1[1, 18:25, 25:30] = 1 - gt_masks1[2, 40:50, 40:50] = 1 - result_masks1[2, 47:50, 47:50] = 1 - gt_class1 = np.array([1, 1, 1]) - gt_is_crowd1 = np.array([0, 0, 1]) - result_class1 = np.array([1, 1, 1]) - - # image2 contains 1 dog and 1 cat in gt, while 1 person and 1 dog in - # prediction. - gt_masks2 = np.zeros((2, 30, 40), dtype=np.uint8) - result_masks2 = np.zeros((2, 30, 40), dtype=np.uint8) - gt_masks2[0, 5:15, 20:35] = 1 - gt_masks2[1, 20:30, 0:10] = 1 - result_masks2[0, 20:25, 10:15] = 1 - result_masks2[1, 6:15, 15:35] = 1 - gt_class2 = np.array([2, 3]) - gt_is_crowd2 = np.array([0, 0]) - result_class2 = np.array([1, 2]) - - gt_class = [gt_class1, gt_class2] - gt_masks = [gt_masks1, gt_masks2] - gt_is_crowd = [gt_is_crowd1, gt_is_crowd2] - result_class = [result_class1, result_class2] - result_masks = [result_masks1, result_masks2] - return gt_class, gt_masks, gt_is_crowd, result_class, result_masks - - -class CocoPanopticEvaluationTest(tf.test.TestCase): - - def test_panoptic_quality(self): - pq_evaluator = coco_evaluation.CocoPanopticSegmentationEvaluator( - _get_categories_list(), include_metrics_per_category=True) - (gt_class, gt_masks, gt_is_crowd, result_class, - result_masks) = _get_panoptic_test_data() - - for i in range(2): - pq_evaluator.add_single_ground_truth_image_info( - image_id='image%d' % i, - groundtruth_dict={ - standard_fields.InputDataFields.groundtruth_classes: - gt_class[i], - standard_fields.InputDataFields.groundtruth_instance_masks: - gt_masks[i], - standard_fields.InputDataFields.groundtruth_is_crowd: - gt_is_crowd[i] - }) - - pq_evaluator.add_single_detected_image_info( - image_id='image%d' % i, - detections_dict={ - standard_fields.DetectionResultFields.detection_classes: - result_class[i], - standard_fields.DetectionResultFields.detection_masks: - result_masks[i] - }) - - metrics = pq_evaluator.evaluate() - self.assertAlmostEqual(metrics['PanopticQuality@0.50IOU_ByCategory/person'], - 0.32) - self.assertAlmostEqual(metrics['PanopticQuality@0.50IOU_ByCategory/dog'], - 135.0 / 195) - self.assertAlmostEqual(metrics['PanopticQuality@0.50IOU_ByCategory/cat'], 0) - self.assertAlmostEqual(metrics['SegmentationQuality@0.50IOU'], - (0.8 + 135.0 / 195) / 3) - self.assertAlmostEqual(metrics['RecognitionQuality@0.50IOU'], (0.4 + 1) / 3) - self.assertAlmostEqual(metrics['PanopticQuality@0.50IOU'], - (0.32 + 135.0 / 195) / 3) - self.assertEqual(metrics['NumValidClasses'], 3) - self.assertEqual(metrics['NumTotalClasses'], 3) - - -@unittest.skipIf(tf_version.is_tf2(), 'Only Supported in TF1.X') -class CocoPanopticEvaluationPyFuncTest(tf.test.TestCase): - - def testPanopticQualityNoBatch(self): - pq_evaluator = coco_evaluation.CocoPanopticSegmentationEvaluator( - _get_categories_list(), include_metrics_per_category=True) - - image_id = tf.placeholder(tf.string, shape=()) - groundtruth_classes = tf.placeholder(tf.int32, shape=(None)) - groundtruth_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) - groundtruth_is_crowd = tf.placeholder(tf.int32, shape=(None)) - detection_classes = tf.placeholder(tf.int32, shape=(None)) - detection_masks = tf.placeholder(tf.uint8, shape=(None, None, None)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_instance_masks: groundtruth_masks, - input_data_fields.groundtruth_is_crowd: groundtruth_is_crowd, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_masks: detection_masks, - } - - eval_metric_ops = pq_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['PanopticQuality@0.50IOU'] - (gt_class, gt_masks, gt_is_crowd, result_class, - result_masks) = _get_panoptic_test_data() - - with self.test_session() as sess: - for i in range(2): - sess.run( - update_op, - feed_dict={ - image_id: 'image%d' % i, - groundtruth_classes: gt_class[i], - groundtruth_masks: gt_masks[i], - groundtruth_is_crowd: gt_is_crowd[i], - detection_classes: result_class[i], - detection_masks: result_masks[i] - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['PanopticQuality@0.50IOU'], - (0.32 + 135.0 / 195) / 3) - - def testPanopticQualityBatched(self): - pq_evaluator = coco_evaluation.CocoPanopticSegmentationEvaluator( - _get_categories_list(), include_metrics_per_category=True) - batch_size = 2 - image_id = tf.placeholder(tf.string, shape=(batch_size)) - groundtruth_classes = tf.placeholder(tf.int32, shape=(batch_size, None)) - groundtruth_masks = tf.placeholder( - tf.uint8, shape=(batch_size, None, None, None)) - groundtruth_is_crowd = tf.placeholder(tf.int32, shape=(batch_size, None)) - detection_classes = tf.placeholder(tf.int32, shape=(batch_size, None)) - detection_masks = tf.placeholder( - tf.uint8, shape=(batch_size, None, None, None)) - num_gt_masks_per_image = tf.placeholder(tf.int32, shape=(batch_size)) - num_det_masks_per_image = tf.placeholder(tf.int32, shape=(batch_size)) - - input_data_fields = standard_fields.InputDataFields - detection_fields = standard_fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_instance_masks: groundtruth_masks, - input_data_fields.groundtruth_is_crowd: groundtruth_is_crowd, - input_data_fields.num_groundtruth_boxes: num_gt_masks_per_image, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_masks: detection_masks, - detection_fields.num_detections: num_det_masks_per_image, - } - - eval_metric_ops = pq_evaluator.get_estimator_eval_metric_ops(eval_dict) - - _, update_op = eval_metric_ops['PanopticQuality@0.50IOU'] - (gt_class, gt_masks, gt_is_crowd, result_class, - result_masks) = _get_panoptic_test_data() - with self.test_session() as sess: - sess.run( - update_op, - feed_dict={ - image_id: ['image0', 'image1'], - groundtruth_classes: - np.stack([ - gt_class[0], - np.pad(gt_class[1], (0, 1), mode='constant') - ], - axis=0), - groundtruth_masks: - np.stack([ - np.pad( - gt_masks[0], ((0, 0), (0, 10), (0, 10)), - mode='constant'), - np.pad( - gt_masks[1], ((0, 1), (0, 30), (0, 20)), - mode='constant'), - ], - axis=0), - groundtruth_is_crowd: - np.stack([ - gt_is_crowd[0], - np.pad(gt_is_crowd[1], (0, 1), mode='constant') - ], - axis=0), - num_gt_masks_per_image: np.array([3, 2]), - detection_classes: - np.stack([ - result_class[0], - np.pad(result_class[1], (0, 1), mode='constant') - ], - axis=0), - detection_masks: - np.stack([ - np.pad( - result_masks[0], ((0, 0), (0, 10), (0, 10)), - mode='constant'), - np.pad( - result_masks[1], ((0, 1), (0, 30), (0, 20)), - mode='constant'), - ], - axis=0), - num_det_masks_per_image: np.array([3, 2]), - }) - metrics = {} - for key, (value_op, _) in eval_metric_ops.items(): - metrics[key] = value_op - metrics = sess.run(metrics) - self.assertAlmostEqual(metrics['PanopticQuality@0.50IOU'], - (0.32 + 135.0 / 195) / 3) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/coco_tools.py b/research/object_detection/metrics/coco_tools.py deleted file mode 100644 index b3c5a92765f..00000000000 --- a/research/object_detection/metrics/coco_tools.py +++ /dev/null @@ -1,980 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Wrappers for third party pycocotools to be used within object_detection. - -Note that nothing in this file is tensorflow related and thus cannot -be called directly as a slim metric, for example. - -TODO(jonathanhuang): wrap as a slim metric in metrics.py - - -Usage example: given a set of images with ids in the list image_ids -and corresponding lists of numpy arrays encoding groundtruth (boxes and classes) -and detections (boxes, scores and classes), where elements of each list -correspond to detections/annotations of a single image, -then evaluation (in multi-class mode) can be invoked as follows: - - groundtruth_dict = coco_tools.ExportGroundtruthToCOCO( - image_ids, groundtruth_boxes_list, groundtruth_classes_list, - max_num_classes, output_path=None) - detections_list = coco_tools.ExportDetectionsToCOCO( - image_ids, detection_boxes_list, detection_scores_list, - detection_classes_list, output_path=None) - groundtruth = coco_tools.COCOWrapper(groundtruth_dict) - detections = groundtruth.LoadAnnotations(detections_list) - evaluator = coco_tools.COCOEvalWrapper(groundtruth, detections, - agnostic_mode=False) - metrics = evaluator.ComputeMetrics() - -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from collections import OrderedDict -import copy -import time -import numpy as np - -from pycocotools import coco -from pycocotools import cocoeval -from pycocotools import mask - -import six -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.utils import json_utils - - -class COCOWrapper(coco.COCO): - """Wrapper for the pycocotools COCO class.""" - - def __init__(self, dataset, detection_type='bbox'): - """COCOWrapper constructor. - - See http://mscoco.org/dataset/#format for a description of the format. - By default, the coco.COCO class constructor reads from a JSON file. - This function duplicates the same behavior but loads from a dictionary, - allowing us to perform evaluation without writing to external storage. - - Args: - dataset: a dictionary holding bounding box annotations in the COCO format. - detection_type: type of detections being wrapped. Can be one of ['bbox', - 'segmentation'] - - Raises: - ValueError: if detection_type is unsupported. - """ - supported_detection_types = ['bbox', 'segmentation'] - if detection_type not in supported_detection_types: - raise ValueError('Unsupported detection type: {}. ' - 'Supported values are: {}'.format( - detection_type, supported_detection_types)) - self._detection_type = detection_type - coco.COCO.__init__(self) - self.dataset = dataset - self.createIndex() - - def LoadAnnotations(self, annotations): - """Load annotations dictionary into COCO datastructure. - - See http://mscoco.org/dataset/#format for a description of the annotations - format. As above, this function replicates the default behavior of the API - but does not require writing to external storage. - - Args: - annotations: python list holding object detection results where each - detection is encoded as a dict with required keys ['image_id', - 'category_id', 'score'] and one of ['bbox', 'segmentation'] based on - `detection_type`. - - Returns: - a coco.COCO datastructure holding object detection annotations results - - Raises: - ValueError: if annotations is not a list - ValueError: if annotations do not correspond to the images contained - in self. - """ - results = coco.COCO() - results.dataset['images'] = [img for img in self.dataset['images']] - - tf.logging.info('Loading and preparing annotation results...') - tic = time.time() - - if not isinstance(annotations, list): - raise ValueError('annotations is not a list of objects') - annotation_img_ids = [ann['image_id'] for ann in annotations] - if (set(annotation_img_ids) != (set(annotation_img_ids) - & set(self.getImgIds()))): - raise ValueError('Results do not correspond to current coco set') - results.dataset['categories'] = copy.deepcopy(self.dataset['categories']) - if self._detection_type == 'bbox': - for idx, ann in enumerate(annotations): - bb = ann['bbox'] - ann['area'] = bb[2] * bb[3] - ann['id'] = idx + 1 - ann['iscrowd'] = 0 - elif self._detection_type == 'segmentation': - for idx, ann in enumerate(annotations): - ann['area'] = mask.area(ann['segmentation']) - ann['bbox'] = mask.toBbox(ann['segmentation']) - ann['id'] = idx + 1 - ann['iscrowd'] = 0 - tf.logging.info('DONE (t=%0.2fs)', (time.time() - tic)) - - results.dataset['annotations'] = annotations - results.createIndex() - return results - - -COCO_METRIC_NAMES_AND_INDEX = ( - ('Precision/mAP', 0), - ('Precision/mAP@.50IOU', 1), - ('Precision/mAP@.75IOU', 2), - ('Precision/mAP (small)', 3), - ('Precision/mAP (medium)', 4), - ('Precision/mAP (large)', 5), - ('Recall/AR@1', 6), - ('Recall/AR@10', 7), - ('Recall/AR@100', 8), - ('Recall/AR@100 (small)', 9), - ('Recall/AR@100 (medium)', 10), - ('Recall/AR@100 (large)', 11) -) - -COCO_KEYPOINT_METRIC_NAMES_AND_INDEX = ( - ('Precision/mAP', 0), - ('Precision/mAP@.50IOU', 1), - ('Precision/mAP@.75IOU', 2), - ('Precision/mAP (medium)', 3), - ('Precision/mAP (large)', 4), - ('Recall/AR@1', 5), - ('Recall/AR@10', 6), - ('Recall/AR@100', 7), - ('Recall/AR@100 (medium)', 8), - ('Recall/AR@100 (large)', 9) -) - - -class COCOEvalWrapper(cocoeval.COCOeval): - """Wrapper for the pycocotools COCOeval class. - - To evaluate, create two objects (groundtruth_dict and detections_list) - using the conventions listed at http://mscoco.org/dataset/#format. - Then call evaluation as follows: - - groundtruth = coco_tools.COCOWrapper(groundtruth_dict) - detections = groundtruth.LoadAnnotations(detections_list) - evaluator = coco_tools.COCOEvalWrapper(groundtruth, detections, - agnostic_mode=False) - - metrics = evaluator.ComputeMetrics() - """ - - def __init__(self, groundtruth=None, detections=None, agnostic_mode=False, - iou_type='bbox', oks_sigmas=None): - """COCOEvalWrapper constructor. - - Note that for the area-based metrics to be meaningful, detection and - groundtruth boxes must be in image coordinates measured in pixels. - - Args: - groundtruth: a coco.COCO (or coco_tools.COCOWrapper) object holding - groundtruth annotations - detections: a coco.COCO (or coco_tools.COCOWrapper) object holding - detections - agnostic_mode: boolean (default: False). If True, evaluation ignores - class labels, treating all detections as proposals. - iou_type: IOU type to use for evaluation. Supports `bbox', `segm`, - `keypoints`. - oks_sigmas: Float numpy array holding the OKS variances for keypoints. - """ - cocoeval.COCOeval.__init__(self, groundtruth, detections, iouType=iou_type) - if oks_sigmas is not None: - self.params.kpt_oks_sigmas = oks_sigmas - if agnostic_mode: - self.params.useCats = 0 - self._iou_type = iou_type - - def GetCategory(self, category_id): - """Fetches dictionary holding category information given category id. - - Args: - category_id: integer id - Returns: - dictionary holding 'id', 'name'. - """ - return self.cocoGt.cats[category_id] - - def GetAgnosticMode(self): - """Returns true if COCO Eval is configured to evaluate in agnostic mode.""" - return self.params.useCats == 0 - - def GetCategoryIdList(self): - """Returns list of valid category ids.""" - return self.params.catIds - - def ComputeMetrics(self, - include_metrics_per_category=False, - all_metrics_per_category=False, - super_categories=None): - """Computes detection/keypoint metrics. - - Args: - include_metrics_per_category: If True, will include metrics per category. - all_metrics_per_category: If true, include all the summery metrics for - each category in per_category_ap. Be careful with setting it to true if - you have more than handful of categories, because it will pollute - your mldash. - super_categories: None or a python dict mapping super-category names - (strings) to lists of categories (corresponding to category names - in the label_map). Metrics are aggregated along these super-categories - and added to the `per_category_ap` and are associated with the name - `PerformanceBySuperCategory/`. - - Returns: - 1. summary_metrics: a dictionary holding: - 'Precision/mAP': mean average precision over classes averaged over IOU - thresholds ranging from .5 to .95 with .05 increments - 'Precision/mAP@.50IOU': mean average precision at 50% IOU - 'Precision/mAP@.75IOU': mean average precision at 75% IOU - 'Precision/mAP (small)': mean average precision for small objects - (area < 32^2 pixels). NOTE: not present for 'keypoints' - 'Precision/mAP (medium)': mean average precision for medium sized - objects (32^2 pixels < area < 96^2 pixels) - 'Precision/mAP (large)': mean average precision for large objects - (96^2 pixels < area < 10000^2 pixels) - 'Recall/AR@1': average recall with 1 detection - 'Recall/AR@10': average recall with 10 detections - 'Recall/AR@100': average recall with 100 detections - 'Recall/AR@100 (small)': average recall for small objects with 100 - detections. NOTE: not present for 'keypoints' - 'Recall/AR@100 (medium)': average recall for medium objects with 100 - detections - 'Recall/AR@100 (large)': average recall for large objects with 100 - detections - 2. per_category_ap: a dictionary holding category specific results with - keys of the form: 'Precision mAP ByCategory/category' - (without the supercategory part if no supercategories exist). - For backward compatibility 'PerformanceByCategory' is included in the - output regardless of all_metrics_per_category. - If evaluating class-agnostic mode, per_category_ap is an empty - dictionary. - If super_categories are provided, then this will additionally include - metrics aggregated along the super_categories with keys of the form: - `PerformanceBySuperCategory/` - - Raises: - ValueError: If category_stats does not exist. - """ - self.evaluate() - self.accumulate() - self.summarize() - - summary_metrics = {} - if self._iou_type in ['bbox', 'segm']: - summary_metrics = OrderedDict( - [(name, self.stats[index]) for name, index in - COCO_METRIC_NAMES_AND_INDEX]) - elif self._iou_type == 'keypoints': - category_id = self.GetCategoryIdList()[0] - category_name = self.GetCategory(category_id)['name'] - summary_metrics = OrderedDict([]) - for metric_name, index in COCO_KEYPOINT_METRIC_NAMES_AND_INDEX: - value = self.stats[index] - summary_metrics['{} ByCategory/{}'.format( - metric_name, category_name)] = value - if not include_metrics_per_category: - return summary_metrics, {} - if not hasattr(self, 'category_stats'): - raise ValueError('Category stats do not exist') - per_category_ap = OrderedDict([]) - super_category_ap = OrderedDict([]) - if self.GetAgnosticMode(): - return summary_metrics, per_category_ap - - if super_categories: - for key in super_categories: - super_category_ap['PerformanceBySuperCategory/{}'.format(key)] = 0 - - if all_metrics_per_category: - for metric_name, _ in COCO_METRIC_NAMES_AND_INDEX: - metric_key = '{} BySuperCategory/{}'.format(metric_name, key) - super_category_ap[metric_key] = 0 - - for category_index, category_id in enumerate(self.GetCategoryIdList()): - category = self.GetCategory(category_id)['name'] - # Kept for backward compatilbility - per_category_ap['PerformanceByCategory/mAP/{}'.format( - category)] = self.category_stats[0][category_index] - - if all_metrics_per_category: - for metric_name, index in COCO_METRIC_NAMES_AND_INDEX: - metric_key = '{} ByCategory/{}'.format(metric_name, category) - per_category_ap[metric_key] = self.category_stats[index][ - category_index] - - if super_categories: - for key in super_categories: - if category in super_categories[key]: - metric_key = 'PerformanceBySuperCategory/{}'.format(key) - super_category_ap[metric_key] += self.category_stats[0][ - category_index] - if all_metrics_per_category: - for metric_name, index in COCO_METRIC_NAMES_AND_INDEX: - metric_key = '{} BySuperCategory/{}'.format(metric_name, key) - super_category_ap[metric_key] += ( - self.category_stats[index][category_index]) - - if super_categories: - for key in super_categories: - length = len(super_categories[key]) - super_category_ap['PerformanceBySuperCategory/{}'.format( - key)] /= length - - if all_metrics_per_category: - for metric_name, _ in COCO_METRIC_NAMES_AND_INDEX: - super_category_ap['{} BySuperCategory/{}'.format( - metric_name, key)] /= length - - per_category_ap.update(super_category_ap) - return summary_metrics, per_category_ap - - -def _ConvertBoxToCOCOFormat(box): - """Converts a box in [ymin, xmin, ymax, xmax] format to COCO format. - - This is a utility function for converting from our internal - [ymin, xmin, ymax, xmax] convention to the convention used by the COCO API - i.e., [xmin, ymin, width, height]. - - Args: - box: a [ymin, xmin, ymax, xmax] numpy array - - Returns: - a list of floats representing [xmin, ymin, width, height] - """ - return [float(box[1]), float(box[0]), float(box[3] - box[1]), - float(box[2] - box[0])] - - -def _RleCompress(masks): - """Compresses mask using Run-length encoding provided by pycocotools. - - Args: - masks: uint8 numpy array of shape [mask_height, mask_width] with values in - {0, 1}. - - Returns: - A pycocotools Run-length encoding of the mask. - """ - rle = mask.encode(np.asfortranarray(masks)) - rle['counts'] = six.ensure_str(rle['counts']) - return rle - - -def ExportSingleImageGroundtruthToCoco(image_id, - next_annotation_id, - category_id_set, - groundtruth_boxes, - groundtruth_classes, - groundtruth_keypoints=None, - groundtruth_keypoint_visibilities=None, - groundtruth_masks=None, - groundtruth_is_crowd=None, - groundtruth_area=None): - """Export groundtruth of a single image to COCO format. - - This function converts groundtruth detection annotations represented as numpy - arrays to dictionaries that can be ingested by the COCO evaluation API. Note - that the image_ids provided here must match the ones given to - ExportSingleImageDetectionsToCoco. We assume that boxes and classes are in - correspondence - that is: groundtruth_boxes[i, :], and - groundtruth_classes[i] are associated with the same groundtruth annotation. - - In the exported result, "area" fields are always set to the area of the - groundtruth bounding box. - - Args: - image_id: a unique image identifier either of type integer or string. - next_annotation_id: integer specifying the first id to use for the - groundtruth annotations. All annotations are assigned a continuous integer - id starting from this value. - category_id_set: A set of valid class ids. Groundtruth with classes not in - category_id_set are dropped. - groundtruth_boxes: numpy array (float32) with shape [num_gt_boxes, 4] - groundtruth_classes: numpy array (int) with shape [num_gt_boxes] - groundtruth_keypoints: optional float numpy array of keypoints - with shape [num_gt_boxes, num_keypoints, 2]. - groundtruth_keypoint_visibilities: optional integer numpy array of keypoint - visibilities with shape [num_gt_boxes, num_keypoints]. Integer is treated - as an enum with 0=not labels, 1=labeled but not visible and 2=labeled and - visible. - groundtruth_masks: optional uint8 numpy array of shape [num_detections, - image_height, image_width] containing detection_masks. - groundtruth_is_crowd: optional numpy array (int) with shape [num_gt_boxes] - indicating whether groundtruth boxes are crowd. - groundtruth_area: numpy array (float32) with shape [num_gt_boxes]. If - provided, then the area values (in the original absolute coordinates) will - be populated instead of calculated from bounding box coordinates. - - Returns: - a list of groundtruth annotations for a single image in the COCO format. - - Raises: - ValueError: if (1) groundtruth_boxes and groundtruth_classes do not have the - right lengths or (2) if each of the elements inside these lists do not - have the correct shapes or (3) if image_ids are not integers - """ - - if len(groundtruth_classes.shape) != 1: - raise ValueError('groundtruth_classes is ' - 'expected to be of rank 1.') - if len(groundtruth_boxes.shape) != 2: - raise ValueError('groundtruth_boxes is expected to be of ' - 'rank 2.') - if groundtruth_boxes.shape[1] != 4: - raise ValueError('groundtruth_boxes should have ' - 'shape[1] == 4.') - num_boxes = groundtruth_classes.shape[0] - if num_boxes != groundtruth_boxes.shape[0]: - raise ValueError('Corresponding entries in groundtruth_classes, ' - 'and groundtruth_boxes should have ' - 'compatible shapes (i.e., agree on the 0th dimension).' - 'Classes shape: %d. Boxes shape: %d. Image ID: %s' % ( - groundtruth_classes.shape[0], - groundtruth_boxes.shape[0], image_id)) - has_is_crowd = groundtruth_is_crowd is not None - if has_is_crowd and len(groundtruth_is_crowd.shape) != 1: - raise ValueError('groundtruth_is_crowd is expected to be of rank 1.') - has_keypoints = groundtruth_keypoints is not None - has_keypoint_visibilities = groundtruth_keypoint_visibilities is not None - if has_keypoints and not has_keypoint_visibilities: - groundtruth_keypoint_visibilities = np.full( - (num_boxes, groundtruth_keypoints.shape[1]), 2) - groundtruth_list = [] - for i in range(num_boxes): - if groundtruth_classes[i] in category_id_set: - iscrowd = groundtruth_is_crowd[i] if has_is_crowd else 0 - if groundtruth_area is not None and groundtruth_area[i] > 0: - area = float(groundtruth_area[i]) - else: - area = float((groundtruth_boxes[i, 2] - groundtruth_boxes[i, 0]) * - (groundtruth_boxes[i, 3] - groundtruth_boxes[i, 1])) - export_dict = { - 'id': - next_annotation_id + i, - 'image_id': - image_id, - 'category_id': - int(groundtruth_classes[i]), - 'bbox': - list(_ConvertBoxToCOCOFormat(groundtruth_boxes[i, :])), - 'area': area, - 'iscrowd': - iscrowd - } - if groundtruth_masks is not None: - export_dict['segmentation'] = _RleCompress(groundtruth_masks[i]) - if has_keypoints: - keypoints = groundtruth_keypoints[i] - visibilities = np.reshape(groundtruth_keypoint_visibilities[i], [-1]) - coco_keypoints = [] - num_valid_keypoints = 0 - for keypoint, visibility in zip(keypoints, visibilities): - # Convert from [y, x] to [x, y] as mandated by COCO. - coco_keypoints.append(float(keypoint[1])) - coco_keypoints.append(float(keypoint[0])) - coco_keypoints.append(int(visibility)) - if int(visibility) > 0: - num_valid_keypoints = num_valid_keypoints + 1 - export_dict['keypoints'] = coco_keypoints - export_dict['num_keypoints'] = num_valid_keypoints - - groundtruth_list.append(export_dict) - return groundtruth_list - - -def ExportGroundtruthToCOCO(image_ids, - groundtruth_boxes, - groundtruth_classes, - categories, - output_path=None): - """Export groundtruth detection annotations in numpy arrays to COCO API. - - This function converts a set of groundtruth detection annotations represented - as numpy arrays to dictionaries that can be ingested by the COCO API. - Inputs to this function are three lists: image ids for each groundtruth image, - groundtruth boxes for each image and groundtruth classes respectively. - Note that the image_ids provided here must match the ones given to the - ExportDetectionsToCOCO function in order for evaluation to work properly. - We assume that for each image, boxes, scores and classes are in - correspondence --- that is: image_id[i], groundtruth_boxes[i, :] and - groundtruth_classes[i] are associated with the same groundtruth annotation. - - In the exported result, "area" fields are always set to the area of the - groundtruth bounding box and "iscrowd" fields are always set to 0. - TODO(jonathanhuang): pass in "iscrowd" array for evaluating on COCO dataset. - - Args: - image_ids: a list of unique image identifier either of type integer or - string. - groundtruth_boxes: list of numpy arrays with shape [num_gt_boxes, 4] - (note that num_gt_boxes can be different for each entry in the list) - groundtruth_classes: list of numpy arrays (int) with shape [num_gt_boxes] - (note that num_gt_boxes can be different for each entry in the list) - categories: a list of dictionaries representing all possible categories. - Each dict in this list has the following keys: - 'id': (required) an integer id uniquely identifying this category - 'name': (required) string representing category name - e.g., 'cat', 'dog', 'pizza' - 'supercategory': (optional) string representing the supercategory - e.g., 'animal', 'vehicle', 'food', etc - output_path: (optional) path for exporting result to JSON - Returns: - dictionary that can be read by COCO API - Raises: - ValueError: if (1) groundtruth_boxes and groundtruth_classes do not have the - right lengths or (2) if each of the elements inside these lists do not - have the correct shapes or (3) if image_ids are not integers - """ - category_id_set = set([cat['id'] for cat in categories]) - groundtruth_export_list = [] - image_export_list = [] - if not len(image_ids) == len(groundtruth_boxes) == len(groundtruth_classes): - raise ValueError('Input lists must have the same length') - - # For reasons internal to the COCO API, it is important that annotation ids - # are not equal to zero; we thus start counting from 1. - annotation_id = 1 - for image_id, boxes, classes in zip(image_ids, groundtruth_boxes, - groundtruth_classes): - image_export_list.append({'id': image_id}) - groundtruth_export_list.extend(ExportSingleImageGroundtruthToCoco( - image_id, - annotation_id, - category_id_set, - boxes, - classes)) - num_boxes = classes.shape[0] - annotation_id += num_boxes - - groundtruth_dict = { - 'annotations': groundtruth_export_list, - 'images': image_export_list, - 'categories': categories - } - if output_path: - with tf.gfile.GFile(output_path, 'w') as fid: - json_utils.Dump(groundtruth_dict, fid, float_digits=4, indent=2) - return groundtruth_dict - - -def ExportSingleImageDetectionBoxesToCoco(image_id, - category_id_set, - detection_boxes, - detection_scores, - detection_classes, - detection_keypoints=None, - detection_keypoint_visibilities=None): - """Export detections of a single image to COCO format. - - This function converts detections represented as numpy arrays to dictionaries - that can be ingested by the COCO evaluation API. Note that the image_ids - provided here must match the ones given to the - ExporSingleImageDetectionBoxesToCoco. We assume that boxes, and classes are in - correspondence - that is: boxes[i, :], and classes[i] - are associated with the same groundtruth annotation. - - Args: - image_id: unique image identifier either of type integer or string. - category_id_set: A set of valid class ids. Detections with classes not in - category_id_set are dropped. - detection_boxes: float numpy array of shape [num_detections, 4] containing - detection boxes. - detection_scores: float numpy array of shape [num_detections] containing - scored for the detection boxes. - detection_classes: integer numpy array of shape [num_detections] containing - the classes for detection boxes. - detection_keypoints: optional float numpy array of keypoints - with shape [num_detections, num_keypoints, 2]. - detection_keypoint_visibilities: optional integer numpy array of keypoint - visibilities with shape [num_detections, num_keypoints]. Integer is - treated as an enum with 0=not labels, 1=labeled but not visible and - 2=labeled and visible. - - Returns: - a list of detection annotations for a single image in the COCO format. - - Raises: - ValueError: if (1) detection_boxes, detection_scores and detection_classes - do not have the right lengths or (2) if each of the elements inside these - lists do not have the correct shapes or (3) if image_ids are not integers. - """ - - if len(detection_classes.shape) != 1 or len(detection_scores.shape) != 1: - raise ValueError('All entries in detection_classes and detection_scores' - 'expected to be of rank 1.') - if len(detection_boxes.shape) != 2: - raise ValueError('All entries in detection_boxes expected to be of ' - 'rank 2.') - if detection_boxes.shape[1] != 4: - raise ValueError('All entries in detection_boxes should have ' - 'shape[1] == 4.') - num_boxes = detection_classes.shape[0] - if not num_boxes == detection_boxes.shape[0] == detection_scores.shape[0]: - raise ValueError('Corresponding entries in detection_classes, ' - 'detection_scores and detection_boxes should have ' - 'compatible shapes (i.e., agree on the 0th dimension). ' - 'Classes shape: %d. Boxes shape: %d. ' - 'Scores shape: %d' % ( - detection_classes.shape[0], detection_boxes.shape[0], - detection_scores.shape[0] - )) - detections_list = [] - for i in range(num_boxes): - if detection_classes[i] in category_id_set: - export_dict = { - 'image_id': - image_id, - 'category_id': - int(detection_classes[i]), - 'bbox': - list(_ConvertBoxToCOCOFormat(detection_boxes[i, :])), - 'score': - float(detection_scores[i]), - } - if detection_keypoints is not None: - keypoints = detection_keypoints[i] - num_keypoints = keypoints.shape[0] - if detection_keypoint_visibilities is None: - detection_keypoint_visibilities = np.full((num_boxes, num_keypoints), - 2) - visibilities = np.reshape(detection_keypoint_visibilities[i], [-1]) - coco_keypoints = [] - for keypoint, visibility in zip(keypoints, visibilities): - # Convert from [y, x] to [x, y] as mandated by COCO. - coco_keypoints.append(float(keypoint[1])) - coco_keypoints.append(float(keypoint[0])) - coco_keypoints.append(int(visibility)) - export_dict['keypoints'] = coco_keypoints - export_dict['num_keypoints'] = num_keypoints - detections_list.append(export_dict) - - return detections_list - - -def ExportSingleImageDetectionMasksToCoco(image_id, - category_id_set, - detection_masks, - detection_scores, - detection_classes): - """Export detection masks of a single image to COCO format. - - This function converts detections represented as numpy arrays to dictionaries - that can be ingested by the COCO evaluation API. We assume that - detection_masks, detection_scores, and detection_classes are in correspondence - - that is: detection_masks[i, :], detection_classes[i] and detection_scores[i] - are associated with the same annotation. - - Args: - image_id: unique image identifier either of type integer or string. - category_id_set: A set of valid class ids. Detections with classes not in - category_id_set are dropped. - detection_masks: uint8 numpy array of shape [num_detections, image_height, - image_width] containing detection_masks. - detection_scores: float numpy array of shape [num_detections] containing - scores for detection masks. - detection_classes: integer numpy array of shape [num_detections] containing - the classes for detection masks. - - Returns: - a list of detection mask annotations for a single image in the COCO format. - - Raises: - ValueError: if (1) detection_masks, detection_scores and detection_classes - do not have the right lengths or (2) if each of the elements inside these - lists do not have the correct shapes or (3) if image_ids are not integers. - """ - - if len(detection_classes.shape) != 1 or len(detection_scores.shape) != 1: - raise ValueError('All entries in detection_classes and detection_scores' - 'expected to be of rank 1.') - num_boxes = detection_classes.shape[0] - if not num_boxes == len(detection_masks) == detection_scores.shape[0]: - raise ValueError('Corresponding entries in detection_classes, ' - 'detection_scores and detection_masks should have ' - 'compatible lengths and shapes ' - 'Classes length: %d. Masks length: %d. ' - 'Scores length: %d' % ( - detection_classes.shape[0], len(detection_masks), - detection_scores.shape[0] - )) - detections_list = [] - for i in range(num_boxes): - if detection_classes[i] in category_id_set: - detections_list.append({ - 'image_id': image_id, - 'category_id': int(detection_classes[i]), - 'segmentation': _RleCompress(detection_masks[i]), - 'score': float(detection_scores[i]) - }) - return detections_list - - -def ExportDetectionsToCOCO(image_ids, - detection_boxes, - detection_scores, - detection_classes, - categories, - output_path=None): - """Export detection annotations in numpy arrays to COCO API. - - This function converts a set of predicted detections represented - as numpy arrays to dictionaries that can be ingested by the COCO API. - Inputs to this function are lists, consisting of boxes, scores and - classes, respectively, corresponding to each image for which detections - have been produced. Note that the image_ids provided here must - match the ones given to the ExportGroundtruthToCOCO function in order - for evaluation to work properly. - - We assume that for each image, boxes, scores and classes are in - correspondence --- that is: detection_boxes[i, :], detection_scores[i] and - detection_classes[i] are associated with the same detection. - - Args: - image_ids: a list of unique image identifier either of type integer or - string. - detection_boxes: list of numpy arrays with shape [num_detection_boxes, 4] - detection_scores: list of numpy arrays (float) with shape - [num_detection_boxes]. Note that num_detection_boxes can be different - for each entry in the list. - detection_classes: list of numpy arrays (int) with shape - [num_detection_boxes]. Note that num_detection_boxes can be different - for each entry in the list. - categories: a list of dictionaries representing all possible categories. - Each dict in this list must have an integer 'id' key uniquely identifying - this category. - output_path: (optional) path for exporting result to JSON - - Returns: - list of dictionaries that can be read by COCO API, where each entry - corresponds to a single detection and has keys from: - ['image_id', 'category_id', 'bbox', 'score']. - Raises: - ValueError: if (1) detection_boxes and detection_classes do not have the - right lengths or (2) if each of the elements inside these lists do not - have the correct shapes or (3) if image_ids are not integers. - """ - category_id_set = set([cat['id'] for cat in categories]) - detections_export_list = [] - if not (len(image_ids) == len(detection_boxes) == len(detection_scores) == - len(detection_classes)): - raise ValueError('Input lists must have the same length') - for image_id, boxes, scores, classes in zip(image_ids, detection_boxes, - detection_scores, - detection_classes): - detections_export_list.extend(ExportSingleImageDetectionBoxesToCoco( - image_id, - category_id_set, - boxes, - scores, - classes)) - if output_path: - with tf.gfile.GFile(output_path, 'w') as fid: - json_utils.Dump(detections_export_list, fid, float_digits=4, indent=2) - return detections_export_list - - -def ExportSegmentsToCOCO(image_ids, - detection_masks, - detection_scores, - detection_classes, - categories, - output_path=None): - """Export segmentation masks in numpy arrays to COCO API. - - This function converts a set of predicted instance masks represented - as numpy arrays to dictionaries that can be ingested by the COCO API. - Inputs to this function are lists, consisting of segments, scores and - classes, respectively, corresponding to each image for which detections - have been produced. - - Note this function is recommended to use for small dataset. - For large dataset, it should be used with a merge function - (e.g. in map reduce), otherwise the memory consumption is large. - - We assume that for each image, masks, scores and classes are in - correspondence --- that is: detection_masks[i, :, :, :], detection_scores[i] - and detection_classes[i] are associated with the same detection. - - Args: - image_ids: list of image ids (typically ints or strings) - detection_masks: list of numpy arrays with shape [num_detection, h, w, 1] - and type uint8. The height and width should match the shape of - corresponding image. - detection_scores: list of numpy arrays (float) with shape - [num_detection]. Note that num_detection can be different - for each entry in the list. - detection_classes: list of numpy arrays (int) with shape - [num_detection]. Note that num_detection can be different - for each entry in the list. - categories: a list of dictionaries representing all possible categories. - Each dict in this list must have an integer 'id' key uniquely identifying - this category. - output_path: (optional) path for exporting result to JSON - - Returns: - list of dictionaries that can be read by COCO API, where each entry - corresponds to a single detection and has keys from: - ['image_id', 'category_id', 'segmentation', 'score']. - - Raises: - ValueError: if detection_masks and detection_classes do not have the - right lengths or if each of the elements inside these lists do not - have the correct shapes. - """ - if not (len(image_ids) == len(detection_masks) == len(detection_scores) == - len(detection_classes)): - raise ValueError('Input lists must have the same length') - - segment_export_list = [] - for image_id, masks, scores, classes in zip(image_ids, detection_masks, - detection_scores, - detection_classes): - - if len(classes.shape) != 1 or len(scores.shape) != 1: - raise ValueError('All entries in detection_classes and detection_scores' - 'expected to be of rank 1.') - if len(masks.shape) != 4: - raise ValueError('All entries in masks expected to be of ' - 'rank 4. Given {}'.format(masks.shape)) - - num_boxes = classes.shape[0] - if not num_boxes == masks.shape[0] == scores.shape[0]: - raise ValueError('Corresponding entries in segment_classes, ' - 'detection_scores and detection_boxes should have ' - 'compatible shapes (i.e., agree on the 0th dimension).') - - category_id_set = set([cat['id'] for cat in categories]) - segment_export_list.extend(ExportSingleImageDetectionMasksToCoco( - image_id, category_id_set, np.squeeze(masks, axis=3), scores, classes)) - - if output_path: - with tf.gfile.GFile(output_path, 'w') as fid: - json_utils.Dump(segment_export_list, fid, float_digits=4, indent=2) - return segment_export_list - - -def ExportKeypointsToCOCO(image_ids, - detection_keypoints, - detection_scores, - detection_classes, - categories, - output_path=None): - """Exports keypoints in numpy arrays to COCO API. - - This function converts a set of predicted keypoints represented - as numpy arrays to dictionaries that can be ingested by the COCO API. - Inputs to this function are lists, consisting of keypoints, scores and - classes, respectively, corresponding to each image for which detections - have been produced. - - We assume that for each image, keypoints, scores and classes are in - correspondence --- that is: detection_keypoints[i, :, :, :], - detection_scores[i] and detection_classes[i] are associated with the same - detection. - - Args: - image_ids: list of image ids (typically ints or strings) - detection_keypoints: list of numpy arrays with shape - [num_detection, num_keypoints, 2] and type float32 in absolute - x-y coordinates. - detection_scores: list of numpy arrays (float) with shape - [num_detection]. Note that num_detection can be different - for each entry in the list. - detection_classes: list of numpy arrays (int) with shape - [num_detection]. Note that num_detection can be different - for each entry in the list. - categories: a list of dictionaries representing all possible categories. - Each dict in this list must have an integer 'id' key uniquely identifying - this category and an integer 'num_keypoints' key specifying the number of - keypoints the category has. - output_path: (optional) path for exporting result to JSON - - Returns: - list of dictionaries that can be read by COCO API, where each entry - corresponds to a single detection and has keys from: - ['image_id', 'category_id', 'keypoints', 'score']. - - Raises: - ValueError: if detection_keypoints and detection_classes do not have the - right lengths or if each of the elements inside these lists do not - have the correct shapes. - """ - if not (len(image_ids) == len(detection_keypoints) == - len(detection_scores) == len(detection_classes)): - raise ValueError('Input lists must have the same length') - - keypoints_export_list = [] - for image_id, keypoints, scores, classes in zip( - image_ids, detection_keypoints, detection_scores, detection_classes): - - if len(classes.shape) != 1 or len(scores.shape) != 1: - raise ValueError('All entries in detection_classes and detection_scores' - 'expected to be of rank 1.') - if len(keypoints.shape) != 3: - raise ValueError('All entries in keypoints expected to be of ' - 'rank 3. Given {}'.format(keypoints.shape)) - - num_boxes = classes.shape[0] - if not num_boxes == keypoints.shape[0] == scores.shape[0]: - raise ValueError('Corresponding entries in detection_classes, ' - 'detection_keypoints, and detection_scores should have ' - 'compatible shapes (i.e., agree on the 0th dimension).') - - category_id_set = set([cat['id'] for cat in categories]) - category_id_to_num_keypoints_map = { - cat['id']: cat['num_keypoints'] for cat in categories - if 'num_keypoints' in cat} - - for i in range(num_boxes): - if classes[i] not in category_id_set: - raise ValueError('class id should be in category_id_set\n') - - if classes[i] in category_id_to_num_keypoints_map: - num_keypoints = category_id_to_num_keypoints_map[classes[i]] - # Adds extra ones to indicate the visibility for each keypoint as is - # recommended by MSCOCO. - instance_keypoints = np.concatenate( - [keypoints[i, 0:num_keypoints, :], - np.expand_dims(np.ones(num_keypoints), axis=1)], - axis=1).astype(int) - - instance_keypoints = instance_keypoints.flatten().tolist() - keypoints_export_list.append({ - 'image_id': image_id, - 'category_id': int(classes[i]), - 'keypoints': instance_keypoints, - 'score': float(scores[i]) - }) - - if output_path: - with tf.gfile.GFile(output_path, 'w') as fid: - json_utils.Dump(keypoints_export_list, fid, float_digits=4, indent=2) - return keypoints_export_list diff --git a/research/object_detection/metrics/coco_tools_test.py b/research/object_detection/metrics/coco_tools_test.py deleted file mode 100644 index d3f53ecfc11..00000000000 --- a/research/object_detection/metrics/coco_tools_test.py +++ /dev/null @@ -1,405 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for tensorflow_model.object_detection.metrics.coco_tools.""" -import json -import os -import re -import numpy as np - -from pycocotools import mask - -import tensorflow.compat.v1 as tf - -from object_detection.metrics import coco_tools - - -class CocoToolsTest(tf.test.TestCase): - - def setUp(self): - groundtruth_annotations_list = [ - { - 'id': 1, - 'image_id': 'first', - 'category_id': 1, - 'bbox': [100., 100., 100., 100.], - 'area': 100.**2, - 'iscrowd': 0 - }, - { - 'id': 2, - 'image_id': 'second', - 'category_id': 1, - 'bbox': [50., 50., 50., 50.], - 'area': 50.**2, - 'iscrowd': 0 - }, - ] - image_list = [{'id': 'first'}, {'id': 'second'}] - category_list = [{'id': 0, 'name': 'person'}, - {'id': 1, 'name': 'cat'}, - {'id': 2, 'name': 'dog'}] - self._groundtruth_dict = { - 'annotations': groundtruth_annotations_list, - 'images': image_list, - 'categories': category_list - } - - self._detections_list = [ - { - 'image_id': 'first', - 'category_id': 1, - 'bbox': [100., 100., 100., 100.], - 'score': .8 - }, - { - 'image_id': 'second', - 'category_id': 1, - 'bbox': [50., 50., 50., 50.], - 'score': .7 - }, - ] - - def testCocoWrappers(self): - groundtruth = coco_tools.COCOWrapper(self._groundtruth_dict) - detections = groundtruth.LoadAnnotations(self._detections_list) - evaluator = coco_tools.COCOEvalWrapper(groundtruth, detections) - summary_metrics, _ = evaluator.ComputeMetrics() - self.assertAlmostEqual(1.0, summary_metrics['Precision/mAP']) - - def testExportGroundtruthToCOCO(self): - image_ids = ['first', 'second'] - groundtruth_boxes = [np.array([[100, 100, 200, 200]], float), - np.array([[50, 50, 100, 100]], float)] - groundtruth_classes = [np.array([1], np.int32), np.array([1], np.int32)] - categories = [{'id': 0, 'name': 'person'}, - {'id': 1, 'name': 'cat'}, - {'id': 2, 'name': 'dog'}] - output_path = os.path.join(tf.test.get_temp_dir(), 'groundtruth.json') - result = coco_tools.ExportGroundtruthToCOCO( - image_ids, - groundtruth_boxes, - groundtruth_classes, - categories, - output_path=output_path) - self.assertDictEqual(result, self._groundtruth_dict) - with tf.gfile.GFile(output_path, 'r') as f: - written_result = f.read() - # The json output should have floats written to 4 digits of precision. - matcher = re.compile(r'"bbox":\s+\[\n\s+\d+.\d\d\d\d,', re.MULTILINE) - self.assertTrue(matcher.findall(written_result)) - written_result = json.loads(written_result) - self.assertAlmostEqual(result, written_result) - - def testExportDetectionsToCOCO(self): - image_ids = ['first', 'second'] - detections_boxes = [np.array([[100, 100, 200, 200]], float), - np.array([[50, 50, 100, 100]], float)] - detections_scores = [np.array([.8], float), np.array([.7], float)] - detections_classes = [np.array([1], np.int32), np.array([1], np.int32)] - categories = [{'id': 0, 'name': 'person'}, - {'id': 1, 'name': 'cat'}, - {'id': 2, 'name': 'dog'}] - output_path = os.path.join(tf.test.get_temp_dir(), 'detections.json') - result = coco_tools.ExportDetectionsToCOCO( - image_ids, - detections_boxes, - detections_scores, - detections_classes, - categories, - output_path=output_path) - self.assertListEqual(result, self._detections_list) - with tf.gfile.GFile(output_path, 'r') as f: - written_result = f.read() - # The json output should have floats written to 4 digits of precision. - matcher = re.compile(r'"bbox":\s+\[\n\s+\d+.\d\d\d\d,', re.MULTILINE) - self.assertTrue(matcher.findall(written_result)) - written_result = json.loads(written_result) - self.assertAlmostEqual(result, written_result) - - def testExportSegmentsToCOCO(self): - image_ids = ['first', 'second'] - detection_masks = [np.array( - [[[0, 1, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 1, 0, 1]]], - dtype=np.uint8), np.array( - [[[0, 1, 0, 1], [0, 1, 1, 0], [0, 0, 0, 1], [0, 1, 0, 1]]], - dtype=np.uint8)] - - for i, detection_mask in enumerate(detection_masks): - detection_masks[i] = detection_mask[:, :, :, None] - - detection_scores = [np.array([.8], float), np.array([.7], float)] - detection_classes = [np.array([1], np.int32), np.array([1], np.int32)] - - categories = [{'id': 0, 'name': 'person'}, - {'id': 1, 'name': 'cat'}, - {'id': 2, 'name': 'dog'}] - output_path = os.path.join(tf.test.get_temp_dir(), 'segments.json') - result = coco_tools.ExportSegmentsToCOCO( - image_ids, - detection_masks, - detection_scores, - detection_classes, - categories, - output_path=output_path) - with tf.gfile.GFile(output_path, 'r') as f: - written_result = f.read() - written_result = json.loads(written_result) - mask_load = mask.decode([written_result[0]['segmentation']]) - self.assertTrue(np.allclose(mask_load, detection_masks[0])) - self.assertAlmostEqual(result, written_result) - - def testExportKeypointsToCOCO(self): - image_ids = ['first', 'second'] - detection_keypoints = [ - np.array( - [[[100, 200], [300, 400], [500, 600]], - [[50, 150], [250, 350], [450, 550]]], dtype=np.int32), - np.array( - [[[110, 210], [310, 410], [510, 610]], - [[60, 160], [260, 360], [460, 560]]], dtype=np.int32)] - - detection_scores = [np.array([.8, 0.2], float), - np.array([.7, 0.3], float)] - detection_classes = [np.array([1, 1], np.int32), np.array([1, 1], np.int32)] - - categories = [{'id': 1, 'name': 'person', 'num_keypoints': 3}, - {'id': 2, 'name': 'cat'}, - {'id': 3, 'name': 'dog'}] - - output_path = os.path.join(tf.test.get_temp_dir(), 'keypoints.json') - result = coco_tools.ExportKeypointsToCOCO( - image_ids, - detection_keypoints, - detection_scores, - detection_classes, - categories, - output_path=output_path) - - with tf.gfile.GFile(output_path, 'r') as f: - written_result = f.read() - written_result = json.loads(written_result) - self.assertAlmostEqual(result, written_result) - - def testSingleImageDetectionBoxesExport(self): - boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, 1, 1]], dtype=np.float32) - classes = np.array([1, 2, 3], dtype=np.int32) - scores = np.array([0.8, 0.2, 0.7], dtype=np.float32) - coco_boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, .5, .5]], dtype=np.float32) - coco_annotations = coco_tools.ExportSingleImageDetectionBoxesToCoco( - image_id='first_image', - category_id_set=set([1, 2, 3]), - detection_boxes=boxes, - detection_classes=classes, - detection_scores=scores) - for i, annotation in enumerate(coco_annotations): - self.assertEqual(annotation['image_id'], 'first_image') - self.assertEqual(annotation['category_id'], classes[i]) - self.assertAlmostEqual(annotation['score'], scores[i]) - self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) - - def testSingleImageDetectionMaskExport(self): - masks = np.array( - [[[1, 1,], [1, 1]], - [[0, 0], [0, 1]], - [[0, 0], [0, 0]]], dtype=np.uint8) - classes = np.array([1, 2, 3], dtype=np.int32) - scores = np.array([0.8, 0.2, 0.7], dtype=np.float32) - coco_annotations = coco_tools.ExportSingleImageDetectionMasksToCoco( - image_id='first_image', - category_id_set=set([1, 2, 3]), - detection_classes=classes, - detection_scores=scores, - detection_masks=masks) - expected_counts = ['04', '31', '4'] - for i, mask_annotation in enumerate(coco_annotations): - self.assertEqual(mask_annotation['segmentation']['counts'], - expected_counts[i]) - self.assertTrue(np.all(np.equal(mask.decode( - mask_annotation['segmentation']), masks[i]))) - self.assertEqual(mask_annotation['image_id'], 'first_image') - self.assertEqual(mask_annotation['category_id'], classes[i]) - self.assertAlmostEqual(mask_annotation['score'], scores[i]) - - def testSingleImageGroundtruthExport(self): - masks = np.array( - [[[1, 1,], [1, 1]], - [[0, 0], [0, 1]], - [[0, 0], [0, 0]]], dtype=np.uint8) - boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, 1, 1]], dtype=np.float32) - coco_boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, .5, .5]], dtype=np.float32) - classes = np.array([1, 2, 3], dtype=np.int32) - is_crowd = np.array([0, 1, 0], dtype=np.int32) - next_annotation_id = 1 - expected_counts = ['04', '31', '4'] - - # Tests exporting without passing in is_crowd (for backward compatibility). - coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( - image_id='first_image', - category_id_set=set([1, 2, 3]), - next_annotation_id=next_annotation_id, - groundtruth_boxes=boxes, - groundtruth_classes=classes, - groundtruth_masks=masks) - for i, annotation in enumerate(coco_annotations): - self.assertEqual(annotation['segmentation']['counts'], - expected_counts[i]) - self.assertTrue(np.all(np.equal(mask.decode( - annotation['segmentation']), masks[i]))) - self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) - self.assertEqual(annotation['image_id'], 'first_image') - self.assertEqual(annotation['category_id'], classes[i]) - self.assertEqual(annotation['id'], i + next_annotation_id) - - # Tests exporting with is_crowd. - coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( - image_id='first_image', - category_id_set=set([1, 2, 3]), - next_annotation_id=next_annotation_id, - groundtruth_boxes=boxes, - groundtruth_classes=classes, - groundtruth_masks=masks, - groundtruth_is_crowd=is_crowd) - for i, annotation in enumerate(coco_annotations): - self.assertEqual(annotation['segmentation']['counts'], - expected_counts[i]) - self.assertTrue(np.all(np.equal(mask.decode( - annotation['segmentation']), masks[i]))) - self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) - self.assertEqual(annotation['image_id'], 'first_image') - self.assertEqual(annotation['category_id'], classes[i]) - self.assertEqual(annotation['iscrowd'], is_crowd[i]) - self.assertEqual(annotation['id'], i + next_annotation_id) - - def testSingleImageGroundtruthExportWithKeypoints(self): - boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, 1, 1]], dtype=np.float32) - coco_boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, .5, .5]], dtype=np.float32) - keypoints = np.array([[[0, 0], [0.25, 0.25], [0.75, 0.75]], - [[0, 0], [0.125, 0.125], [0.375, 0.375]], - [[0.5, 0.5], [0.75, 0.75], [1.0, 1.0]]], - dtype=np.float32) - visibilities = np.array([[2, 2, 2], - [2, 2, 0], - [2, 0, 0]], dtype=np.int32) - areas = np.array([15., 16., 17.]) - - classes = np.array([1, 2, 3], dtype=np.int32) - is_crowd = np.array([0, 1, 0], dtype=np.int32) - next_annotation_id = 1 - - # Tests exporting without passing in is_crowd (for backward compatibility). - coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( - image_id='first_image', - category_id_set=set([1, 2, 3]), - next_annotation_id=next_annotation_id, - groundtruth_boxes=boxes, - groundtruth_classes=classes, - groundtruth_keypoints=keypoints, - groundtruth_keypoint_visibilities=visibilities, - groundtruth_area=areas) - for i, annotation in enumerate(coco_annotations): - self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) - self.assertEqual(annotation['image_id'], 'first_image') - self.assertEqual(annotation['category_id'], classes[i]) - self.assertEqual(annotation['id'], i + next_annotation_id) - self.assertEqual(annotation['num_keypoints'], 3 - i) - self.assertEqual(annotation['area'], 15.0 + i) - self.assertTrue( - np.all(np.isclose(annotation['keypoints'][0::3], keypoints[i, :, 1]))) - self.assertTrue( - np.all(np.isclose(annotation['keypoints'][1::3], keypoints[i, :, 0]))) - self.assertTrue( - np.all(np.equal(annotation['keypoints'][2::3], visibilities[i]))) - - # Tests exporting with is_crowd. - coco_annotations = coco_tools.ExportSingleImageGroundtruthToCoco( - image_id='first_image', - category_id_set=set([1, 2, 3]), - next_annotation_id=next_annotation_id, - groundtruth_boxes=boxes, - groundtruth_classes=classes, - groundtruth_keypoints=keypoints, - groundtruth_keypoint_visibilities=visibilities, - groundtruth_is_crowd=is_crowd) - for i, annotation in enumerate(coco_annotations): - self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) - self.assertEqual(annotation['image_id'], 'first_image') - self.assertEqual(annotation['category_id'], classes[i]) - self.assertEqual(annotation['iscrowd'], is_crowd[i]) - self.assertEqual(annotation['id'], i + next_annotation_id) - self.assertEqual(annotation['num_keypoints'], 3 - i) - self.assertTrue( - np.all(np.isclose(annotation['keypoints'][0::3], keypoints[i, :, 1]))) - self.assertTrue( - np.all(np.isclose(annotation['keypoints'][1::3], keypoints[i, :, 0]))) - self.assertTrue( - np.all(np.equal(annotation['keypoints'][2::3], visibilities[i]))) - # Testing the area values are derived from the bounding boxes. - if i == 0: - self.assertAlmostEqual(annotation['area'], 1.0) - else: - self.assertAlmostEqual(annotation['area'], 0.25) - - def testSingleImageDetectionBoxesExportWithKeypoints(self): - boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, 1, 1]], - dtype=np.float32) - coco_boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, .5, .5]], - dtype=np.float32) - keypoints = np.array([[[0, 0], [0.25, 0.25], [0.75, 0.75]], - [[0, 0], [0.125, 0.125], [0.375, 0.375]], - [[0.5, 0.5], [0.75, 0.75], [1.0, 1.0]]], - dtype=np.float32) - visibilities = np.array([[2, 2, 2], [2, 2, 2], [2, 2, 2]], dtype=np.int32) - - classes = np.array([1, 2, 3], dtype=np.int32) - scores = np.array([0.8, 0.2, 0.7], dtype=np.float32) - - # Tests exporting without passing in is_crowd (for backward compatibility). - coco_annotations = coco_tools.ExportSingleImageDetectionBoxesToCoco( - image_id='first_image', - category_id_set=set([1, 2, 3]), - detection_boxes=boxes, - detection_scores=scores, - detection_classes=classes, - detection_keypoints=keypoints, - detection_keypoint_visibilities=visibilities) - for i, annotation in enumerate(coco_annotations): - self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) - self.assertEqual(annotation['image_id'], 'first_image') - self.assertEqual(annotation['category_id'], classes[i]) - self.assertTrue(np.all(np.isclose(annotation['bbox'], coco_boxes[i]))) - self.assertEqual(annotation['score'], scores[i]) - self.assertEqual(annotation['num_keypoints'], 3) - self.assertTrue( - np.all(np.isclose(annotation['keypoints'][0::3], keypoints[i, :, 1]))) - self.assertTrue( - np.all(np.isclose(annotation['keypoints'][1::3], keypoints[i, :, 0]))) - self.assertTrue( - np.all(np.equal(annotation['keypoints'][2::3], visibilities[i]))) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/io_utils.py b/research/object_detection/metrics/io_utils.py deleted file mode 100644 index 900584de1e5..00000000000 --- a/research/object_detection/metrics/io_utils.py +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Common IO utils used in offline metric computation. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import csv - - -def write_csv(fid, metrics): - """Writes metrics key-value pairs to CSV file. - - Args: - fid: File identifier of an opened file. - metrics: A dictionary with metrics to be written. - """ - metrics_writer = csv.writer(fid, delimiter=',') - for metric_name, metric_value in metrics.items(): - metrics_writer.writerow([metric_name, str(metric_value)]) diff --git a/research/object_detection/metrics/lvis_evaluation.py b/research/object_detection/metrics/lvis_evaluation.py deleted file mode 100644 index 4fbd6e42714..00000000000 --- a/research/object_detection/metrics/lvis_evaluation.py +++ /dev/null @@ -1,463 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Class for evaluating object detections with LVIS metrics.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import json -import re - -from lvis import results as lvis_results - -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields as fields -from object_detection.metrics import lvis_tools -from object_detection.utils import object_detection_evaluation - - -def convert_masks_to_binary(masks): - """Converts masks to 0 or 1 and uint8 type.""" - return (masks > 0).astype(np.uint8) - - -class LVISMaskEvaluator(object_detection_evaluation.DetectionEvaluator): - """Class to evaluate LVIS mask metrics.""" - - def __init__(self, - categories, - include_metrics_per_category=False, - export_path=None): - """Constructor. - - Args: - categories: A list of dicts, each of which has the following keys - - 'id': (required) an integer id uniquely identifying this category. - 'name': (required) string representing category name e.g., 'cat', 'dog'. - include_metrics_per_category: Additionally include per-category metrics - (this option is currently unsupported). - export_path: Path to export detections to LVIS compatible JSON format. - """ - super(LVISMaskEvaluator, self).__init__(categories) - self._image_ids_with_detections = set([]) - self._groundtruth_list = [] - self._detection_masks_list = [] - self._category_id_set = set([cat['id'] for cat in self._categories]) - self._annotation_id = 1 - self._image_id_to_mask_shape_map = {} - self._image_id_to_verified_neg_classes = {} - self._image_id_to_not_exhaustive_classes = {} - if include_metrics_per_category: - raise ValueError('include_metrics_per_category not yet supported ' - 'for LVISMaskEvaluator.') - self._export_path = export_path - - def clear(self): - """Clears the state to prepare for a fresh evaluation.""" - self._image_id_to_mask_shape_map.clear() - self._image_ids_with_detections.clear() - self._image_id_to_verified_neg_classes.clear() - self._image_id_to_not_exhaustive_classes.clear() - self._groundtruth_list = [] - self._detection_masks_list = [] - - def add_single_ground_truth_image_info(self, - image_id, - groundtruth_dict): - """Adds groundtruth for a single image to be used for evaluation. - - If the image has already been added, a warning is logged, and groundtruth is - ignored. - - Args: - image_id: A unique string/integer identifier for the image. - groundtruth_dict: A dictionary containing - - InputDataFields.groundtruth_boxes: float32 numpy array of shape - [num_boxes, 4] containing `num_boxes` groundtruth boxes of the format - [ymin, xmin, ymax, xmax] in absolute image coordinates. - InputDataFields.groundtruth_classes: integer numpy array of shape - [num_boxes] containing 1-indexed groundtruth classes for the boxes. - InputDataFields.groundtruth_instance_masks: uint8 numpy array of shape - [num_masks, image_height, image_width] containing groundtruth masks. - The elements of the array must be in {0, 1}. - InputDataFields.groundtruth_verified_neg_classes: [num_classes + 1] - float indicator vector with values in {0, 1}. The length is - num_classes + 1 so as to be compatible with the 1-indexed groundtruth - classes. - InputDataFields.groundtruth_not_exhaustive_classes: [num_classes + 1] - float indicator vector with values in {0, 1}. The length is - num_classes + 1 so as to be compatible with the 1-indexed groundtruth - classes. - InputDataFields.groundtruth_area (optional): float numpy array of - shape [num_boxes] containing the area (in the original absolute - coordinates) of the annotated object. - Raises: - ValueError: if groundtruth_dict is missing a required field - """ - if image_id in self._image_id_to_mask_shape_map: - tf.logging.warning('Ignoring ground truth with image id %s since it was ' - 'previously added', image_id) - return - for key in [fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_classes, - fields.InputDataFields.groundtruth_instance_masks, - fields.InputDataFields.groundtruth_verified_neg_classes, - fields.InputDataFields.groundtruth_not_exhaustive_classes]: - if key not in groundtruth_dict.keys(): - raise ValueError('groundtruth_dict missing entry: {}'.format(key)) - - groundtruth_instance_masks = groundtruth_dict[ - fields.InputDataFields.groundtruth_instance_masks] - groundtruth_instance_masks = convert_masks_to_binary( - groundtruth_instance_masks) - verified_neg_classes_shape = groundtruth_dict[ - fields.InputDataFields.groundtruth_verified_neg_classes].shape - not_exhaustive_classes_shape = groundtruth_dict[ - fields.InputDataFields.groundtruth_not_exhaustive_classes].shape - if verified_neg_classes_shape != (len(self._category_id_set) + 1,): - raise ValueError('Invalid shape for verified_neg_classes_shape.') - if not_exhaustive_classes_shape != (len(self._category_id_set) + 1,): - raise ValueError('Invalid shape for not_exhaustive_classes_shape.') - self._image_id_to_verified_neg_classes[image_id] = np.flatnonzero( - groundtruth_dict[ - fields.InputDataFields.groundtruth_verified_neg_classes] - == 1).tolist() - self._image_id_to_not_exhaustive_classes[image_id] = np.flatnonzero( - groundtruth_dict[ - fields.InputDataFields.groundtruth_not_exhaustive_classes] - == 1).tolist() - - # Drop optional fields if empty tensor. - groundtruth_area = groundtruth_dict.get( - fields.InputDataFields.groundtruth_area) - if groundtruth_area is not None and not groundtruth_area.shape[0]: - groundtruth_area = None - - self._groundtruth_list.extend( - lvis_tools.ExportSingleImageGroundtruthToLVIS( - image_id=image_id, - next_annotation_id=self._annotation_id, - category_id_set=self._category_id_set, - groundtruth_boxes=groundtruth_dict[ - fields.InputDataFields.groundtruth_boxes], - groundtruth_classes=groundtruth_dict[ - fields.InputDataFields.groundtruth_classes], - groundtruth_masks=groundtruth_instance_masks, - groundtruth_area=groundtruth_area) - ) - - self._annotation_id += groundtruth_dict[fields.InputDataFields. - groundtruth_boxes].shape[0] - self._image_id_to_mask_shape_map[image_id] = groundtruth_dict[ - fields.InputDataFields.groundtruth_instance_masks].shape - - def add_single_detected_image_info(self, - image_id, - detections_dict): - """Adds detections for a single image to be used for evaluation. - - If a detection has already been added for this image id, a warning is - logged, and the detection is skipped. - - Args: - image_id: A unique string/integer identifier for the image. - detections_dict: A dictionary containing - - DetectionResultFields.detection_scores: float32 numpy array of shape - [num_boxes] containing detection scores for the boxes. - DetectionResultFields.detection_classes: integer numpy array of shape - [num_boxes] containing 1-indexed detection classes for the boxes. - DetectionResultFields.detection_masks: optional uint8 numpy array of - shape [num_boxes, image_height, image_width] containing instance - masks corresponding to the boxes. The elements of the array must be - in {0, 1}. - Raises: - ValueError: If groundtruth for the image_id is not available. - """ - if image_id not in self._image_id_to_mask_shape_map: - raise ValueError('Missing groundtruth for image id: {}'.format(image_id)) - - if image_id in self._image_ids_with_detections: - tf.logging.warning('Ignoring detection with image id %s since it was ' - 'previously added', image_id) - return - - groundtruth_masks_shape = self._image_id_to_mask_shape_map[image_id] - detection_masks = detections_dict[fields.DetectionResultFields. - detection_masks] - if groundtruth_masks_shape[1:] != detection_masks.shape[1:]: - raise ValueError('Spatial shape of groundtruth masks and detection masks ' - 'are incompatible: {} vs {}'.format( - groundtruth_masks_shape, - detection_masks.shape)) - detection_masks = convert_masks_to_binary(detection_masks) - - self._detection_masks_list.extend( - lvis_tools.ExportSingleImageDetectionMasksToLVIS( - image_id=image_id, - category_id_set=self._category_id_set, - detection_masks=detection_masks, - detection_scores=detections_dict[ - fields.DetectionResultFields.detection_scores], - detection_classes=detections_dict[ - fields.DetectionResultFields.detection_classes])) - self._image_ids_with_detections.update([image_id]) - - def evaluate(self): - """Evaluates the detection boxes and returns a dictionary of coco metrics. - - Returns: - A dictionary holding - """ - if self._export_path: - tf.logging.info('Dumping detections to json.') - self.dump_detections_to_json_file(self._export_path) - tf.logging.info('Performing evaluation on %d images.', - len(self._image_id_to_mask_shape_map.keys())) - # pylint: disable=g-complex-comprehension - groundtruth_dict = { - 'annotations': self._groundtruth_list, - 'images': [ - { - 'id': int(image_id), - 'height': shape[1], - 'width': shape[2], - 'neg_category_ids': - self._image_id_to_verified_neg_classes[image_id], - 'not_exhaustive_category_ids': - self._image_id_to_not_exhaustive_classes[image_id] - } for image_id, shape in self._image_id_to_mask_shape_map.items()], - 'categories': self._categories - } - # pylint: enable=g-complex-comprehension - lvis_wrapped_groundtruth = lvis_tools.LVISWrapper(groundtruth_dict) - detections = lvis_results.LVISResults(lvis_wrapped_groundtruth, - self._detection_masks_list) - mask_evaluator = lvis_tools.LVISEvalWrapper( - lvis_wrapped_groundtruth, detections, iou_type='segm') - mask_metrics = mask_evaluator.ComputeMetrics() - mask_metrics = {'DetectionMasks_'+ key: value - for key, value in iter(mask_metrics.items())} - return mask_metrics - - def add_eval_dict(self, eval_dict): - """Observes an evaluation result dict for a single example. - - When executing eagerly, once all observations have been observed by this - method you can use `.evaluate()` to get the final metrics. - - When using `tf.estimator.Estimator` for evaluation this function is used by - `get_estimator_eval_metric_ops()` to construct the metric update op. - - Args: - eval_dict: A dictionary that holds tensors for evaluating an object - detection model, returned from - eval_util.result_dict_for_single_example(). - - Returns: - None when executing eagerly, or an update_op that can be used to update - the eval metrics in `tf.estimator.EstimatorSpec`. - """ - def update_op(image_id_batched, groundtruth_boxes_batched, - groundtruth_classes_batched, - groundtruth_instance_masks_batched, - groundtruth_verified_neg_classes_batched, - groundtruth_not_exhaustive_classes_batched, - num_gt_boxes_per_image, - detection_scores_batched, detection_classes_batched, - detection_masks_batched, num_det_boxes_per_image, - original_image_spatial_shape): - """Update op for metrics.""" - - for (image_id, groundtruth_boxes, groundtruth_classes, - groundtruth_instance_masks, groundtruth_verified_neg_classes, - groundtruth_not_exhaustive_classes, num_gt_box, - detection_scores, detection_classes, - detection_masks, num_det_box, original_image_shape) in zip( - image_id_batched, groundtruth_boxes_batched, - groundtruth_classes_batched, groundtruth_instance_masks_batched, - groundtruth_verified_neg_classes_batched, - groundtruth_not_exhaustive_classes_batched, - num_gt_boxes_per_image, - detection_scores_batched, detection_classes_batched, - detection_masks_batched, num_det_boxes_per_image, - original_image_spatial_shape): - self.add_single_ground_truth_image_info( - image_id, { - input_data_fields.groundtruth_boxes: - groundtruth_boxes[:num_gt_box], - input_data_fields.groundtruth_classes: - groundtruth_classes[:num_gt_box], - input_data_fields.groundtruth_instance_masks: - groundtruth_instance_masks[ - :num_gt_box, - :original_image_shape[0], - :original_image_shape[1]], - input_data_fields.groundtruth_verified_neg_classes: - groundtruth_verified_neg_classes, - input_data_fields.groundtruth_not_exhaustive_classes: - groundtruth_not_exhaustive_classes - }) - self.add_single_detected_image_info( - image_id, { - 'detection_scores': detection_scores[:num_det_box], - 'detection_classes': detection_classes[:num_det_box], - 'detection_masks': detection_masks[ - :num_det_box, - :original_image_shape[0], - :original_image_shape[1]] - }) - - # Unpack items from the evaluation dictionary. - input_data_fields = fields.InputDataFields - detection_fields = fields.DetectionResultFields - image_id = eval_dict[input_data_fields.key] - original_image_spatial_shape = eval_dict[ - input_data_fields.original_image_spatial_shape] - groundtruth_boxes = eval_dict[input_data_fields.groundtruth_boxes] - groundtruth_classes = eval_dict[input_data_fields.groundtruth_classes] - groundtruth_instance_masks = eval_dict[ - input_data_fields.groundtruth_instance_masks] - groundtruth_verified_neg_classes = eval_dict[ - input_data_fields.groundtruth_verified_neg_classes] - groundtruth_not_exhaustive_classes = eval_dict[ - input_data_fields.groundtruth_not_exhaustive_classes] - - num_gt_boxes_per_image = eval_dict.get( - input_data_fields.num_groundtruth_boxes, None) - detection_scores = eval_dict[detection_fields.detection_scores] - detection_classes = eval_dict[detection_fields.detection_classes] - detection_masks = eval_dict[detection_fields.detection_masks] - num_det_boxes_per_image = eval_dict.get(detection_fields.num_detections, - None) - - if not image_id.shape.as_list(): - # Apply a batch dimension to all tensors. - image_id = tf.expand_dims(image_id, 0) - groundtruth_boxes = tf.expand_dims(groundtruth_boxes, 0) - groundtruth_classes = tf.expand_dims(groundtruth_classes, 0) - groundtruth_instance_masks = tf.expand_dims(groundtruth_instance_masks, 0) - groundtruth_verified_neg_classes = tf.expand_dims( - groundtruth_verified_neg_classes, 0) - groundtruth_not_exhaustive_classes = tf.expand_dims( - groundtruth_not_exhaustive_classes, 0) - detection_scores = tf.expand_dims(detection_scores, 0) - detection_classes = tf.expand_dims(detection_classes, 0) - detection_masks = tf.expand_dims(detection_masks, 0) - - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.shape(groundtruth_boxes)[1:2] - else: - num_gt_boxes_per_image = tf.expand_dims(num_gt_boxes_per_image, 0) - - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.shape(detection_scores)[1:2] - else: - num_det_boxes_per_image = tf.expand_dims(num_det_boxes_per_image, 0) - else: - if num_gt_boxes_per_image is None: - num_gt_boxes_per_image = tf.tile( - tf.shape(groundtruth_boxes)[1:2], - multiples=tf.shape(groundtruth_boxes)[0:1]) - if num_det_boxes_per_image is None: - num_det_boxes_per_image = tf.tile( - tf.shape(detection_scores)[1:2], - multiples=tf.shape(detection_scores)[0:1]) - - return tf.py_func(update_op, [ - image_id, groundtruth_boxes, groundtruth_classes, - groundtruth_instance_masks, groundtruth_verified_neg_classes, - groundtruth_not_exhaustive_classes, - num_gt_boxes_per_image, detection_scores, detection_classes, - detection_masks, num_det_boxes_per_image, original_image_spatial_shape - ], []) - - def get_estimator_eval_metric_ops(self, eval_dict): - """Returns a dictionary of eval metric ops. - - Note that once value_op is called, the detections and groundtruth added via - update_op are cleared. - - Args: - eval_dict: A dictionary that holds tensors for evaluating object detection - performance. For single-image evaluation, this dictionary may be - produced from eval_util.result_dict_for_single_example(). If multi-image - evaluation, `eval_dict` should contain the fields - 'num_groundtruth_boxes_per_image' and 'num_det_boxes_per_image' to - properly unpad the tensors from the batch. - - Returns: - a dictionary of metric names to tuple of value_op and update_op that can - be used as eval metric ops in tf.estimator.EstimatorSpec. Note that all - update ops must be run together and similarly all value ops must be run - together to guarantee correct behaviour. - """ - update_op = self.add_eval_dict(eval_dict) - metric_names = ['DetectionMasks_Precision/mAP', - 'DetectionMasks_Precision/mAP@.50IOU', - 'DetectionMasks_Precision/mAP@.75IOU', - 'DetectionMasks_Precision/mAP (small)', - 'DetectionMasks_Precision/mAP (medium)', - 'DetectionMasks_Precision/mAP (large)', - 'DetectionMasks_Recall/AR@1', - 'DetectionMasks_Recall/AR@10', - 'DetectionMasks_Recall/AR@100', - 'DetectionMasks_Recall/AR@100 (small)', - 'DetectionMasks_Recall/AR@100 (medium)', - 'DetectionMasks_Recall/AR@100 (large)'] - if self._include_metrics_per_category: - for category_dict in self._categories: - metric_names.append('DetectionMasks_PerformanceByCategory/mAP/' + - category_dict['name']) - - def first_value_func(): - self._metrics = self.evaluate() - self.clear() - return np.float32(self._metrics[metric_names[0]]) - - def value_func_factory(metric_name): - def value_func(): - return np.float32(self._metrics[metric_name]) - return value_func - - # Ensure that the metrics are only evaluated once. - first_value_op = tf.py_func(first_value_func, [], tf.float32) - eval_metric_ops = {metric_names[0]: (first_value_op, update_op)} - with tf.control_dependencies([first_value_op]): - for metric_name in metric_names[1:]: - eval_metric_ops[metric_name] = (tf.py_func( - value_func_factory(metric_name), [], np.float32), update_op) - return eval_metric_ops - - def dump_detections_to_json_file(self, json_output_path): - """Saves the detections into json_output_path in the format used by MS COCO. - - Args: - json_output_path: String containing the output file's path. It can be also - None. In that case nothing will be written to the output file. - """ - if json_output_path and json_output_path is not None: - pattern = re.compile(r'\d+\.\d{8,}') - def mround(match): - return '{:.2f}'.format(float(match.group())) - - with tf.io.gfile.GFile(json_output_path, 'w') as fid: - json_string = json.dumps(self._detection_masks_list) - fid.write(re.sub(pattern, mround, json_string)) - - tf.logging.info('Dumping detections to output json file: %s', - json_output_path) diff --git a/research/object_detection/metrics/lvis_evaluation_test.py b/research/object_detection/metrics/lvis_evaluation_test.py deleted file mode 100644 index 2a612e5c93a..00000000000 --- a/research/object_detection/metrics/lvis_evaluation_test.py +++ /dev/null @@ -1,182 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for tensorflow_models.object_detection.metrics.coco_evaluation.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import unittest -import numpy as np -import tensorflow.compat.v1 as tf -from object_detection.core import standard_fields as fields -from object_detection.metrics import lvis_evaluation -from object_detection.utils import tf_version - - -def _get_categories_list(): - return [{ - 'id': 1, - 'name': 'person', - 'frequency': 'f' - }, { - 'id': 2, - 'name': 'dog', - 'frequency': 'c' - }, { - 'id': 3, - 'name': 'cat', - 'frequency': 'r' - }] - - -class LvisMaskEvaluationTest(tf.test.TestCase): - - def testGetOneMAPWithMatchingGroundtruthAndDetections(self): - """Tests that mAP is calculated correctly on GT and Detections.""" - masks1 = np.expand_dims(np.pad( - np.ones([100, 100], dtype=np.uint8), - ((100, 56), (100, 56)), mode='constant'), axis=0) - masks2 = np.expand_dims(np.pad( - np.ones([50, 50], dtype=np.uint8), - ((50, 156), (50, 156)), mode='constant'), axis=0) - masks3 = np.expand_dims(np.pad( - np.ones([25, 25], dtype=np.uint8), - ((25, 206), (25, 206)), mode='constant'), axis=0) - - lvis_evaluator = lvis_evaluation.LVISMaskEvaluator( - _get_categories_list()) - lvis_evaluator.add_single_ground_truth_image_info( - image_id=1, - groundtruth_dict={ - fields.InputDataFields.groundtruth_boxes: - np.array([[100., 100., 200., 200.]]), - fields.InputDataFields.groundtruth_classes: np.array([1]), - fields.InputDataFields.groundtruth_instance_masks: masks1, - fields.InputDataFields.groundtruth_verified_neg_classes: - np.array([0, 0, 0, 0]), - fields.InputDataFields.groundtruth_not_exhaustive_classes: - np.array([0, 0, 0, 0]) - }) - lvis_evaluator.add_single_detected_image_info( - image_id=1, - detections_dict={ - fields.DetectionResultFields.detection_masks: masks1, - fields.DetectionResultFields.detection_scores: - np.array([.8]), - fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - lvis_evaluator.add_single_ground_truth_image_info( - image_id=2, - groundtruth_dict={ - fields.InputDataFields.groundtruth_boxes: - np.array([[50., 50., 100., 100.]]), - fields.InputDataFields.groundtruth_classes: np.array([1]), - fields.InputDataFields.groundtruth_instance_masks: masks2, - fields.InputDataFields.groundtruth_verified_neg_classes: - np.array([0, 0, 0, 0]), - fields.InputDataFields.groundtruth_not_exhaustive_classes: - np.array([0, 0, 0, 0]) - }) - lvis_evaluator.add_single_detected_image_info( - image_id=2, - detections_dict={ - fields.DetectionResultFields.detection_masks: masks2, - fields.DetectionResultFields.detection_scores: - np.array([.8]), - fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - lvis_evaluator.add_single_ground_truth_image_info( - image_id=3, - groundtruth_dict={ - fields.InputDataFields.groundtruth_boxes: - np.array([[25., 25., 50., 50.]]), - fields.InputDataFields.groundtruth_classes: np.array([1]), - fields.InputDataFields.groundtruth_instance_masks: masks3, - fields.InputDataFields.groundtruth_verified_neg_classes: - np.array([0, 0, 0, 0]), - fields.InputDataFields.groundtruth_not_exhaustive_classes: - np.array([0, 0, 0, 0]) - }) - lvis_evaluator.add_single_detected_image_info( - image_id=3, - detections_dict={ - fields.DetectionResultFields.detection_masks: masks3, - fields.DetectionResultFields.detection_scores: - np.array([.8]), - fields.DetectionResultFields.detection_classes: - np.array([1]) - }) - metrics = lvis_evaluator.evaluate() - self.assertAlmostEqual(metrics['DetectionMasks_AP'], 1.0) - - -@unittest.skipIf(tf_version.is_tf1(), 'Only Supported in TF2.X') -class LVISMaskEvaluationPyFuncTest(tf.test.TestCase): - - def testAddEvalDict(self): - lvis_evaluator = lvis_evaluation.LVISMaskEvaluator(_get_categories_list()) - image_id = tf.constant(1, dtype=tf.int32) - groundtruth_boxes = tf.constant( - np.array([[100., 100., 200., 200.], [50., 50., 100., 100.]]), - dtype=tf.float32) - groundtruth_classes = tf.constant(np.array([1, 2]), dtype=tf.float32) - groundtruth_masks = tf.constant(np.stack([ - np.pad(np.ones([100, 100], dtype=np.uint8), ((10, 10), (10, 10)), - mode='constant'), - np.pad(np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), - mode='constant') - ]), dtype=tf.uint8) - original_image_spatial_shapes = tf.constant([[120, 120], [120, 120]], - dtype=tf.int32) - groundtruth_verified_neg_classes = tf.constant(np.array([0, 0, 0, 0]), - dtype=tf.float32) - groundtruth_not_exhaustive_classes = tf.constant(np.array([0, 0, 0, 0]), - dtype=tf.float32) - detection_scores = tf.constant(np.array([.9, .8]), dtype=tf.float32) - detection_classes = tf.constant(np.array([2, 1]), dtype=tf.float32) - detection_masks = tf.constant(np.stack([ - np.pad(np.ones([50, 50], dtype=np.uint8), ((0, 70), (0, 70)), - mode='constant'), - np.pad(np.ones([100, 100], dtype=np.uint8), ((10, 10), (10, 10)), - mode='constant'), - ]), dtype=tf.uint8) - - input_data_fields = fields.InputDataFields - detection_fields = fields.DetectionResultFields - eval_dict = { - input_data_fields.key: image_id, - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes, - input_data_fields.groundtruth_instance_masks: groundtruth_masks, - input_data_fields.groundtruth_verified_neg_classes: - groundtruth_verified_neg_classes, - input_data_fields.groundtruth_not_exhaustive_classes: - groundtruth_not_exhaustive_classes, - input_data_fields.original_image_spatial_shape: - original_image_spatial_shapes, - detection_fields.detection_scores: detection_scores, - detection_fields.detection_classes: detection_classes, - detection_fields.detection_masks: detection_masks - } - lvis_evaluator.add_eval_dict(eval_dict) - self.assertLen(lvis_evaluator._groundtruth_list, 2) - self.assertLen(lvis_evaluator._detection_masks_list, 2) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/lvis_tools.py b/research/object_detection/metrics/lvis_tools.py deleted file mode 100644 index 86f3a234b74..00000000000 --- a/research/object_detection/metrics/lvis_tools.py +++ /dev/null @@ -1,260 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Wrappers for third party lvis to be used within object_detection. - -Usage example: given a set of images with ids in the list image_ids -and corresponding lists of numpy arrays encoding groundtruth (boxes, -masks and classes) and detections (masks, scores and classes), where -elements of each list correspond to detections/annotations of a single image, -then evaluation can be invoked as follows: - - groundtruth = lvis_tools.LVISWrapper(groundtruth_dict) - detections = lvis_results.LVISResults(groundtruth, detections_list) - evaluator = lvis_tools.LVISEvalWrapper(groundtruth, detections, - iou_type='segm') - summary_metrics = evaluator.ComputeMetrics() - -TODO(jonathanhuang): Add support for exporting to JSON. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import logging - -from lvis import eval as lvis_eval -from lvis import lvis -import numpy as np -from pycocotools import mask -import six -from six.moves import range - - -def RleCompress(masks): - """Compresses mask using Run-length encoding provided by pycocotools. - - Args: - masks: uint8 numpy array of shape [mask_height, mask_width] with values in - {0, 1}. - - Returns: - A pycocotools Run-length encoding of the mask. - """ - rle = mask.encode(np.asfortranarray(masks)) - rle['counts'] = six.ensure_str(rle['counts']) - return rle - - -def _ConvertBoxToCOCOFormat(box): - """Converts a box in [ymin, xmin, ymax, xmax] format to COCO format. - - This is a utility function for converting from our internal - [ymin, xmin, ymax, xmax] convention to the convention used by the COCO API - i.e., [xmin, ymin, width, height]. - - Args: - box: a [ymin, xmin, ymax, xmax] numpy array - - Returns: - a list of floats representing [xmin, ymin, width, height] - """ - return [float(box[1]), float(box[0]), float(box[3] - box[1]), - float(box[2] - box[0])] - - -class LVISWrapper(lvis.LVIS): - """Wrapper for the lvis.LVIS class.""" - - def __init__(self, dataset, detection_type='bbox'): - """LVISWrapper constructor. - - See https://www.lvisdataset.org/dataset for a description of the format. - By default, the coco.COCO class constructor reads from a JSON file. - This function duplicates the same behavior but loads from a dictionary, - allowing us to perform evaluation without writing to external storage. - - Args: - dataset: a dictionary holding bounding box annotations in the COCO format. - detection_type: type of detections being wrapped. Can be one of ['bbox', - 'segmentation'] - - Raises: - ValueError: if detection_type is unsupported. - """ - self.logger = logging.getLogger(__name__) - self.logger.info('Loading annotations.') - self.dataset = dataset - self._create_index() - - -class LVISEvalWrapper(lvis_eval.LVISEval): - """LVISEval wrapper.""" - - def __init__(self, groundtruth=None, detections=None, iou_type='bbox'): - lvis_eval.LVISEval.__init__( - self, groundtruth, detections, iou_type=iou_type) - self._iou_type = iou_type - - def ComputeMetrics(self): - self.run() - summary_metrics = {} - summary_metrics = self.results - return summary_metrics - - -def ExportSingleImageGroundtruthToLVIS(image_id, - next_annotation_id, - category_id_set, - groundtruth_boxes, - groundtruth_classes, - groundtruth_masks=None, - groundtruth_area=None): - """Export groundtruth of a single image to LVIS format. - - This function converts groundtruth detection annotations represented as numpy - arrays to dictionaries that can be ingested by the LVIS evaluation API. Note - that the image_ids provided here must match the ones given to - ExportSingleImageDetectionMasksToLVIS. We assume that boxes, classes and masks - are in correspondence - that is, e.g., groundtruth_boxes[i, :], and - groundtruth_classes[i] are associated with the same groundtruth annotation. - - In the exported result, "area" fields are always set to the area of the - groundtruth bounding box. - - Args: - image_id: a unique image identifier castable to integer. - next_annotation_id: integer specifying the first id to use for the - groundtruth annotations. All annotations are assigned a continuous integer - id starting from this value. - category_id_set: A set of valid class ids. Groundtruth with classes not in - category_id_set are dropped. - groundtruth_boxes: numpy array (float32) with shape [num_gt_boxes, 4] - groundtruth_classes: numpy array (int) with shape [num_gt_boxes] - groundtruth_masks: optional uint8 numpy array of shape [num_detections, - image_height, image_width] containing detection_masks. - groundtruth_area: numpy array (float32) with shape [num_gt_boxes]. If - provided, then the area values (in the original absolute coordinates) will - be populated instead of calculated from bounding box coordinates. - - Returns: - a list of groundtruth annotations for a single image in the COCO format. - - Raises: - ValueError: if (1) groundtruth_boxes and groundtruth_classes do not have the - right lengths or (2) if each of the elements inside these lists do not - have the correct shapes or (3) if image_ids are not integers - """ - - if len(groundtruth_classes.shape) != 1: - raise ValueError('groundtruth_classes is ' - 'expected to be of rank 1.') - if len(groundtruth_boxes.shape) != 2: - raise ValueError('groundtruth_boxes is expected to be of ' - 'rank 2.') - if groundtruth_boxes.shape[1] != 4: - raise ValueError('groundtruth_boxes should have ' - 'shape[1] == 4.') - num_boxes = groundtruth_classes.shape[0] - if num_boxes != groundtruth_boxes.shape[0]: - raise ValueError('Corresponding entries in groundtruth_classes, ' - 'and groundtruth_boxes should have ' - 'compatible shapes (i.e., agree on the 0th dimension).' - 'Classes shape: %d. Boxes shape: %d. Image ID: %s' % ( - groundtruth_classes.shape[0], - groundtruth_boxes.shape[0], image_id)) - - groundtruth_list = [] - for i in range(num_boxes): - if groundtruth_classes[i] in category_id_set: - if groundtruth_area is not None and groundtruth_area[i] > 0: - area = float(groundtruth_area[i]) - else: - area = float((groundtruth_boxes[i, 2] - groundtruth_boxes[i, 0]) * - (groundtruth_boxes[i, 3] - groundtruth_boxes[i, 1])) - export_dict = { - 'id': - next_annotation_id + i, - 'image_id': - int(image_id), - 'category_id': - int(groundtruth_classes[i]), - 'bbox': - list(_ConvertBoxToCOCOFormat(groundtruth_boxes[i, :])), - 'area': area, - } - if groundtruth_masks is not None: - export_dict['segmentation'] = RleCompress(groundtruth_masks[i]) - - groundtruth_list.append(export_dict) - return groundtruth_list - - -def ExportSingleImageDetectionMasksToLVIS(image_id, - category_id_set, - detection_masks, - detection_scores, - detection_classes): - """Export detection masks of a single image to LVIS format. - - This function converts detections represented as numpy arrays to dictionaries - that can be ingested by the LVIS evaluation API. We assume that - detection_masks, detection_scores, and detection_classes are in correspondence - - that is: detection_masks[i, :], detection_classes[i] and detection_scores[i] - are associated with the same annotation. - - Args: - image_id: unique image identifier castable to integer. - category_id_set: A set of valid class ids. Detections with classes not in - category_id_set are dropped. - detection_masks: uint8 numpy array of shape [num_detections, image_height, - image_width] containing detection_masks. - detection_scores: float numpy array of shape [num_detections] containing - scores for detection masks. - detection_classes: integer numpy array of shape [num_detections] containing - the classes for detection masks. - - Returns: - a list of detection mask annotations for a single image in the COCO format. - - Raises: - ValueError: if (1) detection_masks, detection_scores and detection_classes - do not have the right lengths or (2) if each of the elements inside these - lists do not have the correct shapes or (3) if image_ids are not integers. - """ - - if len(detection_classes.shape) != 1 or len(detection_scores.shape) != 1: - raise ValueError('All entries in detection_classes and detection_scores' - 'expected to be of rank 1.') - num_boxes = detection_classes.shape[0] - if not num_boxes == len(detection_masks) == detection_scores.shape[0]: - raise ValueError('Corresponding entries in detection_classes, ' - 'detection_scores and detection_masks should have ' - 'compatible lengths and shapes ' - 'Classes length: %d. Masks length: %d. ' - 'Scores length: %d' % ( - detection_classes.shape[0], len(detection_masks), - detection_scores.shape[0] - )) - detections_list = [] - for i in range(num_boxes): - if detection_classes[i] in category_id_set: - detections_list.append({ - 'image_id': int(image_id), - 'category_id': int(detection_classes[i]), - 'segmentation': RleCompress(detection_masks[i]), - 'score': float(detection_scores[i]) - }) - - return detections_list diff --git a/research/object_detection/metrics/lvis_tools_test.py b/research/object_detection/metrics/lvis_tools_test.py deleted file mode 100644 index 5a5585acda9..00000000000 --- a/research/object_detection/metrics/lvis_tools_test.py +++ /dev/null @@ -1,158 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for tensorflow_model.object_detection.metrics.lvis_tools.""" -from lvis import results as lvis_results -import numpy as np -from pycocotools import mask -import tensorflow.compat.v1 as tf -from object_detection.metrics import lvis_tools - - -class LVISToolsTest(tf.test.TestCase): - - def setUp(self): - super(LVISToolsTest, self).setUp() - mask1 = np.pad( - np.ones([100, 100], dtype=np.uint8), - ((100, 56), (100, 56)), mode='constant') - mask2 = np.pad( - np.ones([50, 50], dtype=np.uint8), - ((50, 156), (50, 156)), mode='constant') - mask1_rle = lvis_tools.RleCompress(mask1) - mask2_rle = lvis_tools.RleCompress(mask2) - groundtruth_annotations_list = [ - { - 'id': 1, - 'image_id': 1, - 'category_id': 1, - 'bbox': [100., 100., 100., 100.], - 'area': 100.**2, - 'segmentation': mask1_rle - }, - { - 'id': 2, - 'image_id': 2, - 'category_id': 1, - 'bbox': [50., 50., 50., 50.], - 'area': 50.**2, - 'segmentation': mask2_rle - }, - ] - image_list = [ - { - 'id': 1, - 'neg_category_ids': [], - 'not_exhaustive_category_ids': [], - 'height': 256, - 'width': 256 - }, - { - 'id': 2, - 'neg_category_ids': [], - 'not_exhaustive_category_ids': [], - 'height': 256, - 'width': 256 - } - ] - category_list = [{'id': 0, 'name': 'person', 'frequency': 'f'}, - {'id': 1, 'name': 'cat', 'frequency': 'c'}, - {'id': 2, 'name': 'dog', 'frequency': 'r'}] - self._groundtruth_dict = { - 'annotations': groundtruth_annotations_list, - 'images': image_list, - 'categories': category_list - } - - self._detections_list = [ - { - 'image_id': 1, - 'category_id': 1, - 'segmentation': mask1_rle, - 'score': .8 - }, - { - 'image_id': 2, - 'category_id': 1, - 'segmentation': mask2_rle, - 'score': .7 - }, - ] - - def testLVISWrappers(self): - groundtruth = lvis_tools.LVISWrapper(self._groundtruth_dict) - detections = lvis_results.LVISResults(groundtruth, self._detections_list) - evaluator = lvis_tools.LVISEvalWrapper(groundtruth, detections, - iou_type='segm') - summary_metrics = evaluator.ComputeMetrics() - self.assertAlmostEqual(1.0, summary_metrics['AP']) - - def testSingleImageDetectionMaskExport(self): - masks = np.array( - [[[1, 1,], [1, 1]], - [[0, 0], [0, 1]], - [[0, 0], [0, 0]]], dtype=np.uint8) - classes = np.array([1, 2, 3], dtype=np.int32) - scores = np.array([0.8, 0.2, 0.7], dtype=np.float32) - lvis_annotations = lvis_tools.ExportSingleImageDetectionMasksToLVIS( - image_id=1, - category_id_set=set([1, 2, 3]), - detection_classes=classes, - detection_scores=scores, - detection_masks=masks) - expected_counts = ['04', '31', '4'] - for i, mask_annotation in enumerate(lvis_annotations): - self.assertEqual(mask_annotation['segmentation']['counts'], - expected_counts[i]) - self.assertTrue(np.all(np.equal(mask.decode( - mask_annotation['segmentation']), masks[i]))) - self.assertEqual(mask_annotation['image_id'], 1) - self.assertEqual(mask_annotation['category_id'], classes[i]) - self.assertAlmostEqual(mask_annotation['score'], scores[i]) - - def testSingleImageGroundtruthExport(self): - masks = np.array( - [[[1, 1,], [1, 1]], - [[0, 0], [0, 1]], - [[0, 0], [0, 0]]], dtype=np.uint8) - boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, 1, 1]], dtype=np.float32) - lvis_boxes = np.array([[0, 0, 1, 1], - [0, 0, .5, .5], - [.5, .5, .5, .5]], dtype=np.float32) - classes = np.array([1, 2, 3], dtype=np.int32) - next_annotation_id = 1 - expected_counts = ['04', '31', '4'] - - lvis_annotations = lvis_tools.ExportSingleImageGroundtruthToLVIS( - image_id=1, - category_id_set=set([1, 2, 3]), - next_annotation_id=next_annotation_id, - groundtruth_boxes=boxes, - groundtruth_classes=classes, - groundtruth_masks=masks) - for i, annotation in enumerate(lvis_annotations): - self.assertEqual(annotation['segmentation']['counts'], - expected_counts[i]) - self.assertTrue(np.all(np.equal(mask.decode( - annotation['segmentation']), masks[i]))) - self.assertTrue(np.all(np.isclose(annotation['bbox'], lvis_boxes[i]))) - self.assertEqual(annotation['image_id'], 1) - self.assertEqual(annotation['category_id'], classes[i]) - self.assertEqual(annotation['id'], i + next_annotation_id) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/offline_eval_map_corloc.py b/research/object_detection/metrics/offline_eval_map_corloc.py deleted file mode 100644 index a12b1d98493..00000000000 --- a/research/object_detection/metrics/offline_eval_map_corloc.py +++ /dev/null @@ -1,171 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Evaluation executable for detection data. - -This executable evaluates precomputed detections produced by a detection -model and writes the evaluation results into csv file metrics.csv, stored -in the directory, specified by --eval_dir. - -The evaluation metrics set is supplied in object_detection.protos.EvalConfig -in metrics_set field. -Currently two set of metrics are supported: -- pascal_voc_metrics: standard PASCAL VOC 2007 metric -- open_images_detection_metrics: Open Image V2 metric -All other field of object_detection.protos.EvalConfig are ignored. - -Example usage: - ./compute_metrics \ - --eval_dir=path/to/eval_dir \ - --eval_config_path=path/to/evaluation/configuration/file \ - --input_config_path=path/to/input/configuration/file -""" -import csv -import os -import re -import tensorflow.compat.v1 as tf - -from object_detection import eval_util -from object_detection.core import standard_fields -from object_detection.metrics import tf_example_parser -from object_detection.utils import config_util -from object_detection.utils import label_map_util - -flags = tf.app.flags -tf.logging.set_verbosity(tf.logging.INFO) - -flags.DEFINE_string('eval_dir', None, 'Directory to write eval summaries to.') -flags.DEFINE_string('eval_config_path', None, - 'Path to an eval_pb2.EvalConfig config file.') -flags.DEFINE_string('input_config_path', None, - 'Path to an eval_pb2.InputConfig config file.') - -FLAGS = flags.FLAGS - - -def _generate_sharded_filenames(filename): - m = re.search(r'@(\d{1,})', filename) - if m: - num_shards = int(m.group(1)) - return [ - re.sub(r'@(\d{1,})', '-%.5d-of-%.5d' % (i, num_shards), filename) - for i in range(num_shards) - ] - else: - return [filename] - - -def _generate_filenames(filenames): - result = [] - for filename in filenames: - result += _generate_sharded_filenames(filename) - return result - - -def read_data_and_evaluate(input_config, eval_config): - """Reads pre-computed object detections and groundtruth from tf_record. - - Args: - input_config: input config proto of type - object_detection.protos.InputReader. - eval_config: evaluation config proto of type - object_detection.protos.EvalConfig. - - Returns: - Evaluated detections metrics. - - Raises: - ValueError: if input_reader type is not supported or metric type is unknown. - """ - if input_config.WhichOneof('input_reader') == 'tf_record_input_reader': - input_paths = input_config.tf_record_input_reader.input_path - - categories = label_map_util.create_categories_from_labelmap( - input_config.label_map_path) - - object_detection_evaluators = eval_util.get_evaluators( - eval_config, categories) - # Support a single evaluator - object_detection_evaluator = object_detection_evaluators[0] - - skipped_images = 0 - processed_images = 0 - for input_path in _generate_filenames(input_paths): - tf.logging.info('Processing file: {0}'.format(input_path)) - - record_iterator = tf.python_io.tf_record_iterator(path=input_path) - data_parser = tf_example_parser.TfExampleDetectionAndGTParser() - - for string_record in record_iterator: - tf.logging.log_every_n(tf.logging.INFO, 'Processed %d images...', 1000, - processed_images) - processed_images += 1 - - example = tf.train.Example() - example.ParseFromString(string_record) - decoded_dict = data_parser.parse(example) - - if decoded_dict: - object_detection_evaluator.add_single_ground_truth_image_info( - decoded_dict[standard_fields.DetectionResultFields.key], - decoded_dict) - object_detection_evaluator.add_single_detected_image_info( - decoded_dict[standard_fields.DetectionResultFields.key], - decoded_dict) - else: - skipped_images += 1 - tf.logging.info('Skipped images: {0}'.format(skipped_images)) - - return object_detection_evaluator.evaluate() - - raise ValueError('Unsupported input_reader_config.') - - -def write_metrics(metrics, output_dir): - """Write metrics to the output directory. - - Args: - metrics: A dictionary containing metric names and values. - output_dir: Directory to write metrics to. - """ - tf.logging.info('Writing metrics.') - - with open(os.path.join(output_dir, 'metrics.csv'), 'w') as csvfile: - metrics_writer = csv.writer(csvfile, delimiter=',') - for metric_name, metric_value in metrics.items(): - metrics_writer.writerow([metric_name, str(metric_value)]) - - -def main(argv): - del argv - required_flags = ['input_config_path', 'eval_config_path', 'eval_dir'] - for flag_name in required_flags: - if not getattr(FLAGS, flag_name): - raise ValueError('Flag --{} is required'.format(flag_name)) - - configs = config_util.get_configs_from_multiple_files( - eval_input_config_path=FLAGS.input_config_path, - eval_config_path=FLAGS.eval_config_path) - - eval_config = configs['eval_config'] - input_config = configs['eval_input_config'] - - metrics = read_data_and_evaluate(input_config, eval_config) - - # Save metrics - write_metrics(metrics, FLAGS.eval_dir) - - -if __name__ == '__main__': - tf.app.run(main) diff --git a/research/object_detection/metrics/offline_eval_map_corloc_test.py b/research/object_detection/metrics/offline_eval_map_corloc_test.py deleted file mode 100644 index 9641dfb2d18..00000000000 --- a/research/object_detection/metrics/offline_eval_map_corloc_test.py +++ /dev/null @@ -1,58 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for utilities in offline_eval_map_corloc binary.""" - -import tensorflow.compat.v1 as tf - -from object_detection.metrics import offline_eval_map_corloc as offline_eval - - -class OfflineEvalMapCorlocTest(tf.test.TestCase): - - def test_generateShardedFilenames(self): - test_filename = '/path/to/file' - result = offline_eval._generate_sharded_filenames(test_filename) - self.assertEqual(result, [test_filename]) - - test_filename = '/path/to/file-00000-of-00050' - result = offline_eval._generate_sharded_filenames(test_filename) - self.assertEqual(result, [test_filename]) - - result = offline_eval._generate_sharded_filenames('/path/to/@3.record') - self.assertEqual(result, [ - '/path/to/-00000-of-00003.record', '/path/to/-00001-of-00003.record', - '/path/to/-00002-of-00003.record' - ]) - - result = offline_eval._generate_sharded_filenames('/path/to/abc@3') - self.assertEqual(result, [ - '/path/to/abc-00000-of-00003', '/path/to/abc-00001-of-00003', - '/path/to/abc-00002-of-00003' - ]) - - result = offline_eval._generate_sharded_filenames('/path/to/@1') - self.assertEqual(result, ['/path/to/-00000-of-00001']) - - def test_generateFilenames(self): - test_filenames = ['/path/to/file', '/path/to/@3.record'] - result = offline_eval._generate_filenames(test_filenames) - self.assertEqual(result, [ - '/path/to/file', '/path/to/-00000-of-00003.record', - '/path/to/-00001-of-00003.record', '/path/to/-00002-of-00003.record' - ]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/oid_challenge_evaluation.py b/research/object_detection/metrics/oid_challenge_evaluation.py deleted file mode 100644 index 25f553a917f..00000000000 --- a/research/object_detection/metrics/oid_challenge_evaluation.py +++ /dev/null @@ -1,149 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Runs evaluation using OpenImages groundtruth and predictions. - -Uses Open Images Challenge 2018, 2019 metrics - -Example usage: -python models/research/object_detection/metrics/oid_od_challenge_evaluation.py \ - --input_annotations_boxes=/path/to/input/annotations-human-bbox.csv \ - --input_annotations_labels=/path/to/input/annotations-label.csv \ - --input_class_labelmap=/path/to/input/class_labelmap.pbtxt \ - --input_predictions=/path/to/input/predictions.csv \ - --output_metrics=/path/to/output/metric.csv \ - --input_annotations_segm=[/path/to/input/annotations-human-mask.csv] \ - -If optional flag has_masks is True, Mask column is also expected in CSV. - -CSVs with bounding box annotations, instance segmentations and image label -can be downloaded from the Open Images Challenge website: -https://storage.googleapis.com/openimages/web/challenge.html -The format of the input csv and the metrics itself are described on the -challenge website as well. - - -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import logging - -from absl import app -from absl import flags -import pandas as pd -from google.protobuf import text_format - -from object_detection.metrics import io_utils -from object_detection.metrics import oid_challenge_evaluation_utils as utils -from object_detection.protos import string_int_label_map_pb2 -from object_detection.utils import object_detection_evaluation - -flags.DEFINE_string('input_annotations_boxes', None, - 'File with groundtruth boxes annotations.') -flags.DEFINE_string('input_annotations_labels', None, - 'File with groundtruth labels annotations.') -flags.DEFINE_string( - 'input_predictions', None, - """File with detection predictions; NOTE: no postprocessing is applied in the evaluation script.""" -) -flags.DEFINE_string('input_class_labelmap', None, - 'Open Images Challenge labelmap.') -flags.DEFINE_string('output_metrics', None, 'Output file with csv metrics.') -flags.DEFINE_string( - 'input_annotations_segm', None, - 'File with groundtruth instance segmentation annotations [OPTIONAL].') - -FLAGS = flags.FLAGS - - -def _load_labelmap(labelmap_path): - """Loads labelmap from the labelmap path. - - Args: - labelmap_path: Path to the labelmap. - - Returns: - A dictionary mapping class name to class numerical id - A list with dictionaries, one dictionary per category. - """ - - label_map = string_int_label_map_pb2.StringIntLabelMap() - with open(labelmap_path, 'r') as fid: - label_map_string = fid.read() - text_format.Merge(label_map_string, label_map) - labelmap_dict = {} - categories = [] - for item in label_map.item: - labelmap_dict[item.name] = item.id - categories.append({'id': item.id, 'name': item.name}) - return labelmap_dict, categories - - -def main(unused_argv): - flags.mark_flag_as_required('input_annotations_boxes') - flags.mark_flag_as_required('input_annotations_labels') - flags.mark_flag_as_required('input_predictions') - flags.mark_flag_as_required('input_class_labelmap') - flags.mark_flag_as_required('output_metrics') - - all_location_annotations = pd.read_csv(FLAGS.input_annotations_boxes) - all_label_annotations = pd.read_csv(FLAGS.input_annotations_labels) - all_label_annotations.rename( - columns={'Confidence': 'ConfidenceImageLabel'}, inplace=True) - - is_instance_segmentation_eval = False - if FLAGS.input_annotations_segm: - is_instance_segmentation_eval = True - all_segm_annotations = pd.read_csv(FLAGS.input_annotations_segm) - # Note: this part is unstable as it requires the float point numbers in both - # csvs are exactly the same; - # Will be replaced by more stable solution: merge on LabelName and ImageID - # and filter down by IoU. - all_location_annotations = utils.merge_boxes_and_masks( - all_location_annotations, all_segm_annotations) - all_annotations = pd.concat([all_location_annotations, all_label_annotations]) - - class_label_map, categories = _load_labelmap(FLAGS.input_class_labelmap) - challenge_evaluator = ( - object_detection_evaluation.OpenImagesChallengeEvaluator( - categories, evaluate_masks=is_instance_segmentation_eval)) - - all_predictions = pd.read_csv(FLAGS.input_predictions) - images_processed = 0 - for _, groundtruth in enumerate(all_annotations.groupby('ImageID')): - logging.info('Processing image %d', images_processed) - image_id, image_groundtruth = groundtruth - groundtruth_dictionary = utils.build_groundtruth_dictionary( - image_groundtruth, class_label_map) - challenge_evaluator.add_single_ground_truth_image_info( - image_id, groundtruth_dictionary) - - prediction_dictionary = utils.build_predictions_dictionary( - all_predictions.loc[all_predictions['ImageID'] == image_id], - class_label_map) - challenge_evaluator.add_single_detected_image_info(image_id, - prediction_dictionary) - images_processed += 1 - - metrics = challenge_evaluator.evaluate() - - with open(FLAGS.output_metrics, 'w') as fid: - io_utils.write_csv(fid, metrics) - - -if __name__ == '__main__': - app.run(main) diff --git a/research/object_detection/metrics/oid_challenge_evaluation_utils.py b/research/object_detection/metrics/oid_challenge_evaluation_utils.py deleted file mode 100644 index 86746912336..00000000000 --- a/research/object_detection/metrics/oid_challenge_evaluation_utils.py +++ /dev/null @@ -1,196 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Converts data from CSV to the OpenImagesDetectionChallengeEvaluator format.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import base64 -import zlib - -import numpy as np -import pandas as pd -from pycocotools import mask as coco_mask - -from object_detection.core import standard_fields - - -def _to_normalized_box(mask_np): - """Decodes binary segmentation masks into np.arrays and boxes. - - Args: - mask_np: np.ndarray of size NxWxH. - - Returns: - a np.ndarray of the size Nx4, each row containing normalized coordinates - [YMin, XMin, YMax, XMax] of a box computed of axis parallel enclosing box of - a mask. - """ - coord1, coord2 = np.nonzero(mask_np) - if coord1.size > 0: - ymin = float(min(coord1)) / mask_np.shape[0] - ymax = float(max(coord1) + 1) / mask_np.shape[0] - xmin = float(min(coord2)) / mask_np.shape[1] - xmax = float((max(coord2) + 1)) / mask_np.shape[1] - - return np.array([ymin, xmin, ymax, xmax]) - else: - return np.array([0.0, 0.0, 0.0, 0.0]) - - -def _decode_raw_data_into_masks_and_boxes(segments, image_widths, - image_heights): - """Decods binary segmentation masks into np.arrays and boxes. - - Args: - segments: pandas Series object containing either None entries, or strings - with base64, zlib compressed, COCO RLE-encoded binary masks. All masks are - expected to be the same size. - image_widths: pandas Series of mask widths. - image_heights: pandas Series of mask heights. - - Returns: - a np.ndarray of the size NxWxH, where W and H is determined from the encoded - masks; for the None values, zero arrays of size WxH are created. If input - contains only None values, W=1, H=1. - """ - segment_masks = [] - segment_boxes = [] - ind = segments.first_valid_index() - if ind is not None: - size = [int(image_heights[ind]), int(image_widths[ind])] - else: - # It does not matter which size we pick since no masks will ever be - # evaluated. - return np.zeros((segments.shape[0], 1, 1), dtype=np.uint8), np.zeros( - (segments.shape[0], 4), dtype=np.float32) - - for segment, im_width, im_height in zip(segments, image_widths, - image_heights): - if pd.isnull(segment): - segment_masks.append(np.zeros([1, size[0], size[1]], dtype=np.uint8)) - segment_boxes.append(np.expand_dims(np.array([0.0, 0.0, 0.0, 0.0]), 0)) - else: - compressed_mask = base64.b64decode(segment) - rle_encoded_mask = zlib.decompress(compressed_mask) - decoding_dict = { - 'size': [im_height, im_width], - 'counts': rle_encoded_mask - } - mask_tensor = coco_mask.decode(decoding_dict) - - segment_masks.append(np.expand_dims(mask_tensor, 0)) - segment_boxes.append(np.expand_dims(_to_normalized_box(mask_tensor), 0)) - - return np.concatenate( - segment_masks, axis=0), np.concatenate( - segment_boxes, axis=0) - - -def merge_boxes_and_masks(box_data, mask_data): - return pd.merge( - box_data, - mask_data, - how='outer', - on=['LabelName', 'ImageID', 'XMin', 'XMax', 'YMin', 'YMax', 'IsGroupOf']) - - -def build_groundtruth_dictionary(data, class_label_map): - """Builds a groundtruth dictionary from groundtruth data in CSV file. - - Args: - data: Pandas DataFrame with the groundtruth data for a single image. - class_label_map: Class labelmap from string label name to an integer. - - Returns: - A dictionary with keys suitable for passing to - OpenImagesDetectionChallengeEvaluator.add_single_ground_truth_image_info: - standard_fields.InputDataFields.groundtruth_boxes: float32 numpy array - of shape [num_boxes, 4] containing `num_boxes` groundtruth boxes of - the format [ymin, xmin, ymax, xmax] in absolute image coordinates. - standard_fields.InputDataFields.groundtruth_classes: integer numpy array - of shape [num_boxes] containing 1-indexed groundtruth classes for the - boxes. - standard_fields.InputDataFields.verified_labels: integer 1D numpy array - containing all classes for which labels are verified. - standard_fields.InputDataFields.groundtruth_group_of: Optional length - M numpy boolean array denoting whether a groundtruth box contains a - group of instances. - """ - data_location = data[data.XMin.notnull()] - data_labels = data[data.ConfidenceImageLabel.notnull()] - - dictionary = { - standard_fields.InputDataFields.groundtruth_boxes: - data_location[['YMin', 'XMin', 'YMax', - 'XMax']].to_numpy().astype(float), - standard_fields.InputDataFields.groundtruth_classes: - data_location['LabelName'].map(lambda x: class_label_map[x] - ).to_numpy(), - standard_fields.InputDataFields.groundtruth_group_of: - data_location['IsGroupOf'].to_numpy().astype(int), - standard_fields.InputDataFields.groundtruth_image_classes: - data_labels['LabelName'].map(lambda x: class_label_map[x]).to_numpy(), - } - - if 'Mask' in data_location: - segments, _ = _decode_raw_data_into_masks_and_boxes( - data_location['Mask'], data_location['ImageWidth'], - data_location['ImageHeight']) - dictionary[ - standard_fields.InputDataFields.groundtruth_instance_masks] = segments - - return dictionary - - -def build_predictions_dictionary(data, class_label_map): - """Builds a predictions dictionary from predictions data in CSV file. - - Args: - data: Pandas DataFrame with the predictions data for a single image. - class_label_map: Class labelmap from string label name to an integer. - - Returns: - Dictionary with keys suitable for passing to - OpenImagesDetectionChallengeEvaluator.add_single_detected_image_info: - standard_fields.DetectionResultFields.detection_boxes: float32 numpy - array of shape [num_boxes, 4] containing `num_boxes` detection boxes - of the format [ymin, xmin, ymax, xmax] in absolute image coordinates. - standard_fields.DetectionResultFields.detection_scores: float32 numpy - array of shape [num_boxes] containing detection scores for the boxes. - standard_fields.DetectionResultFields.detection_classes: integer numpy - array of shape [num_boxes] containing 1-indexed detection classes for - the boxes. - - """ - dictionary = { - standard_fields.DetectionResultFields.detection_classes: - data['LabelName'].map(lambda x: class_label_map[x]).to_numpy(), - standard_fields.DetectionResultFields.detection_scores: - data['Score'].to_numpy().astype(float) - } - - if 'Mask' in data: - segments, boxes = _decode_raw_data_into_masks_and_boxes( - data['Mask'], data['ImageWidth'], data['ImageHeight']) - dictionary[standard_fields.DetectionResultFields.detection_masks] = segments - dictionary[standard_fields.DetectionResultFields.detection_boxes] = boxes - else: - dictionary[standard_fields.DetectionResultFields.detection_boxes] = data[[ - 'YMin', 'XMin', 'YMax', 'XMax' - ]].to_numpy().astype(float) - - return dictionary diff --git a/research/object_detection/metrics/oid_challenge_evaluation_utils_test.py b/research/object_detection/metrics/oid_challenge_evaluation_utils_test.py deleted file mode 100644 index 94a1da0327e..00000000000 --- a/research/object_detection/metrics/oid_challenge_evaluation_utils_test.py +++ /dev/null @@ -1,308 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for oid_od_challenge_evaluation_util.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import base64 -import zlib - -import numpy as np -import pandas as pd -from pycocotools import mask as coco_mask -import six -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields -from object_detection.metrics import oid_challenge_evaluation_utils as utils - - -def encode_mask(mask_to_encode): - """Encodes a binary mask into the Kaggle challenge text format. - - The encoding is done in three stages: - - COCO RLE-encoding, - - zlib compression, - - base64 encoding (to use as entry in csv file). - - Args: - mask_to_encode: binary np.ndarray of dtype bool and 2d shape. - - Returns: - A (base64) text string of the encoded mask. - """ - mask_to_encode = np.squeeze(mask_to_encode) - mask_to_encode = mask_to_encode.reshape(mask_to_encode.shape[0], - mask_to_encode.shape[1], 1) - mask_to_encode = mask_to_encode.astype(np.uint8) - mask_to_encode = np.asfortranarray(mask_to_encode) - encoded_mask = coco_mask.encode(mask_to_encode)[0]['counts'] - compressed_mask = zlib.compress(six.ensure_binary(encoded_mask), - zlib.Z_BEST_COMPRESSION) - base64_mask = base64.b64encode(compressed_mask) - return base64_mask - - -class OidUtilTest(tf.test.TestCase): - - def testMaskToNormalizedBox(self): - mask_np = np.array([[0, 0, 0, 0], [0, 1, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]) - box = utils._to_normalized_box(mask_np) - self.assertAllEqual(np.array([0.25, 0.25, 0.75, 0.5]), box) - mask_np = np.array([[0, 0, 0, 0], [0, 1, 0, 1], [0, 1, 0, 1], [0, 1, 1, 1]]) - box = utils._to_normalized_box(mask_np) - self.assertAllEqual(np.array([0.25, 0.25, 1.0, 1.0]), box) - mask_np = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) - box = utils._to_normalized_box(mask_np) - self.assertAllEqual(np.array([0.0, 0.0, 0.0, 0.0]), box) - - def testDecodeToTensors(self): - mask1 = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 0, 0]], dtype=np.uint8) - mask2 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], dtype=np.uint8) - - encoding1 = encode_mask(mask1) - encoding2 = encode_mask(mask2) - - vals = pd.Series([encoding1, encoding2]) - image_widths = pd.Series([mask1.shape[1], mask2.shape[1]]) - image_heights = pd.Series([mask1.shape[0], mask2.shape[0]]) - - segm, bbox = utils._decode_raw_data_into_masks_and_boxes( - vals, image_widths, image_heights) - expected_segm = np.concatenate( - [np.expand_dims(mask1, 0), - np.expand_dims(mask2, 0)], axis=0) - expected_bbox = np.array([[0.0, 0.5, 2.0 / 3.0, 1.0], [0, 0, 0, 0]]) - self.assertAllEqual(expected_segm, segm) - self.assertAllEqual(expected_bbox, bbox) - - def testDecodeToTensorsNoMasks(self): - vals = pd.Series([None, None]) - image_widths = pd.Series([None, None]) - image_heights = pd.Series([None, None]) - segm, bbox = utils._decode_raw_data_into_masks_and_boxes( - vals, image_widths, image_heights) - self.assertAllEqual(np.zeros((2, 1, 1), dtype=np.uint8), segm) - self.assertAllEqual(np.zeros((2, 4), dtype=np.float32), bbox) - - -class OidChallengeEvaluationUtilTest(tf.test.TestCase): - - def testBuildGroundtruthDictionaryBoxes(self): - np_data = pd.DataFrame( - [['fe58ec1b06db2bb7', '/m/04bcr3', 0.0, 0.3, 0.5, 0.6, 1, None], - ['fe58ec1b06db2bb7', '/m/02gy9n', 0.1, 0.2, 0.3, 0.4, 0, None], - ['fe58ec1b06db2bb7', '/m/04bcr3', None, None, None, None, None, 1], - ['fe58ec1b06db2bb7', '/m/083vt', None, None, None, None, None, 0], - ['fe58ec1b06db2bb7', '/m/02gy9n', None, None, None, None, None, 1]], - columns=[ - 'ImageID', 'LabelName', 'XMin', 'XMax', 'YMin', 'YMax', 'IsGroupOf', - 'ConfidenceImageLabel' - ]) - class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} - groundtruth_dictionary = utils.build_groundtruth_dictionary( - np_data, class_label_map) - - self.assertIn(standard_fields.InputDataFields.groundtruth_boxes, - groundtruth_dictionary) - self.assertIn(standard_fields.InputDataFields.groundtruth_classes, - groundtruth_dictionary) - self.assertIn(standard_fields.InputDataFields.groundtruth_group_of, - groundtruth_dictionary) - self.assertIn(standard_fields.InputDataFields.groundtruth_image_classes, - groundtruth_dictionary) - - self.assertAllEqual( - np.array([1, 3]), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_classes]) - self.assertAllEqual( - np.array([1, 0]), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_group_of]) - - expected_boxes_data = np.array([[0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2]]) - - self.assertNDArrayNear( - expected_boxes_data, groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_boxes], 1e-5) - self.assertAllEqual( - np.array([1, 2, 3]), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_image_classes]) - - def testBuildPredictionDictionaryBoxes(self): - np_data = pd.DataFrame( - [['fe58ec1b06db2bb7', '/m/04bcr3', 0.0, 0.3, 0.5, 0.6, 0.1], - ['fe58ec1b06db2bb7', '/m/02gy9n', 0.1, 0.2, 0.3, 0.4, 0.2], - ['fe58ec1b06db2bb7', '/m/04bcr3', 0.0, 0.1, 0.2, 0.3, 0.3]], - columns=[ - 'ImageID', 'LabelName', 'XMin', 'XMax', 'YMin', 'YMax', 'Score' - ]) - class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} - prediction_dictionary = utils.build_predictions_dictionary( - np_data, class_label_map) - - self.assertIn(standard_fields.DetectionResultFields.detection_boxes, - prediction_dictionary) - self.assertIn(standard_fields.DetectionResultFields.detection_classes, - prediction_dictionary) - self.assertIn(standard_fields.DetectionResultFields.detection_scores, - prediction_dictionary) - - self.assertAllEqual( - np.array([1, 3, 1]), prediction_dictionary[ - standard_fields.DetectionResultFields.detection_classes]) - expected_boxes_data = np.array([[0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2], - [0.2, 0.0, 0.3, 0.1]]) - self.assertNDArrayNear( - expected_boxes_data, prediction_dictionary[ - standard_fields.DetectionResultFields.detection_boxes], 1e-5) - self.assertNDArrayNear( - np.array([0.1, 0.2, 0.3]), prediction_dictionary[ - standard_fields.DetectionResultFields.detection_scores], 1e-5) - - def testBuildGroundtruthDictionaryMasks(self): - mask1 = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], - dtype=np.uint8) - mask2 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], - dtype=np.uint8) - - encoding1 = encode_mask(mask1) - encoding2 = encode_mask(mask2) - - np_data = pd.DataFrame( - [[ - 'fe58ec1b06db2bb7', mask1.shape[1], mask1.shape[0], '/m/04bcr3', - 0.0, 0.3, 0.5, 0.6, 0, None, encoding1 - ], - [ - 'fe58ec1b06db2bb7', None, None, '/m/02gy9n', 0.1, 0.2, 0.3, 0.4, 1, - None, None - ], - [ - 'fe58ec1b06db2bb7', mask2.shape[1], mask2.shape[0], '/m/02gy9n', - 0.5, 0.6, 0.8, 0.9, 0, None, encoding2 - ], - [ - 'fe58ec1b06db2bb7', None, None, '/m/04bcr3', None, None, None, - None, None, 1, None - ], - [ - 'fe58ec1b06db2bb7', None, None, '/m/083vt', None, None, None, None, - None, 0, None - ], - [ - 'fe58ec1b06db2bb7', None, None, '/m/02gy9n', None, None, None, - None, None, 1, None - ]], - columns=[ - 'ImageID', 'ImageWidth', 'ImageHeight', 'LabelName', 'XMin', 'XMax', - 'YMin', 'YMax', 'IsGroupOf', 'ConfidenceImageLabel', 'Mask' - ]) - class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} - groundtruth_dictionary = utils.build_groundtruth_dictionary( - np_data, class_label_map) - self.assertIn(standard_fields.InputDataFields.groundtruth_boxes, - groundtruth_dictionary) - self.assertIn(standard_fields.InputDataFields.groundtruth_classes, - groundtruth_dictionary) - self.assertIn(standard_fields.InputDataFields.groundtruth_group_of, - groundtruth_dictionary) - self.assertIn(standard_fields.InputDataFields.groundtruth_image_classes, - groundtruth_dictionary) - self.assertIn(standard_fields.InputDataFields.groundtruth_instance_masks, - groundtruth_dictionary) - self.assertAllEqual( - np.array([1, 3, 3]), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_classes]) - self.assertAllEqual( - np.array([0, 1, 0]), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_group_of]) - - expected_boxes_data = np.array([[0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2], - [0.8, 0.5, 0.9, 0.6]]) - - self.assertNDArrayNear( - expected_boxes_data, groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_boxes], 1e-5) - self.assertAllEqual( - np.array([1, 2, 3]), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_image_classes]) - - expected_segm = np.concatenate([ - np.expand_dims(mask1, 0), - np.zeros((1, 4, 4), dtype=np.uint8), - np.expand_dims(mask2, 0) - ], - axis=0) - self.assertAllEqual( - expected_segm, groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_instance_masks]) - - def testBuildPredictionDictionaryMasks(self): - mask1 = np.array([[0, 0, 1, 1], [0, 0, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]], - dtype=np.uint8) - mask2 = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], - dtype=np.uint8) - - encoding1 = encode_mask(mask1) - encoding2 = encode_mask(mask2) - - np_data = pd.DataFrame([[ - 'fe58ec1b06db2bb7', mask1.shape[1], mask1.shape[0], '/m/04bcr3', - encoding1, 0.8 - ], - [ - 'fe58ec1b06db2bb7', mask2.shape[1], - mask2.shape[0], '/m/02gy9n', encoding2, 0.6 - ]], - columns=[ - 'ImageID', 'ImageWidth', 'ImageHeight', - 'LabelName', 'Mask', 'Score' - ]) - class_label_map = {'/m/04bcr3': 1, '/m/02gy9n': 3} - prediction_dictionary = utils.build_predictions_dictionary( - np_data, class_label_map) - - self.assertIn(standard_fields.DetectionResultFields.detection_boxes, - prediction_dictionary) - self.assertIn(standard_fields.DetectionResultFields.detection_classes, - prediction_dictionary) - self.assertIn(standard_fields.DetectionResultFields.detection_scores, - prediction_dictionary) - self.assertIn(standard_fields.DetectionResultFields.detection_masks, - prediction_dictionary) - - self.assertAllEqual( - np.array([1, 3]), prediction_dictionary[ - standard_fields.DetectionResultFields.detection_classes]) - - expected_boxes_data = np.array([[0.0, 0.5, 0.5, 1.0], [0, 0, 0, 0]]) - self.assertNDArrayNear( - expected_boxes_data, prediction_dictionary[ - standard_fields.DetectionResultFields.detection_boxes], 1e-5) - self.assertNDArrayNear( - np.array([0.8, 0.6]), prediction_dictionary[ - standard_fields.DetectionResultFields.detection_scores], 1e-5) - expected_segm = np.concatenate( - [np.expand_dims(mask1, 0), - np.expand_dims(mask2, 0)], axis=0) - self.assertAllEqual( - expected_segm, prediction_dictionary[ - standard_fields.DetectionResultFields.detection_masks]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/oid_vrd_challenge_evaluation.py b/research/object_detection/metrics/oid_vrd_challenge_evaluation.py deleted file mode 100644 index 7a56c6bc080..00000000000 --- a/research/object_detection/metrics/oid_vrd_challenge_evaluation.py +++ /dev/null @@ -1,154 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Runs evaluation using OpenImages groundtruth and predictions. - -Example usage: -python \ -models/research/object_detection/metrics/oid_vrd_challenge_evaluation.py \ - --input_annotations_vrd=/path/to/input/annotations-human-bbox.csv \ - --input_annotations_labels=/path/to/input/annotations-label.csv \ - --input_class_labelmap=/path/to/input/class_labelmap.pbtxt \ - --input_relationship_labelmap=/path/to/input/relationship_labelmap.pbtxt \ - --input_predictions=/path/to/input/predictions.csv \ - --output_metrics=/path/to/output/metric.csv \ - -CSVs with bounding box annotations and image label (including the image URLs) -can be downloaded from the Open Images Challenge website: -https://storage.googleapis.com/openimages/web/challenge.html -The format of the input csv and the metrics itself are described on the -challenge website. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import argparse -import pandas as pd -from google.protobuf import text_format - -from object_detection.metrics import io_utils -from object_detection.metrics import oid_vrd_challenge_evaluation_utils as utils -from object_detection.protos import string_int_label_map_pb2 -from object_detection.utils import vrd_evaluation - - -def _load_labelmap(labelmap_path): - """Loads labelmap from the labelmap path. - - Args: - labelmap_path: Path to the labelmap. - - Returns: - A dictionary mapping class name to class numerical id. - """ - - label_map = string_int_label_map_pb2.StringIntLabelMap() - with open(labelmap_path, 'r') as fid: - label_map_string = fid.read() - text_format.Merge(label_map_string, label_map) - labelmap_dict = {} - for item in label_map.item: - labelmap_dict[item.name] = item.id - return labelmap_dict - - -def _swap_labelmap_dict(labelmap_dict): - """Swaps keys and labels in labelmap. - - Args: - labelmap_dict: Input dictionary. - - Returns: - A dictionary mapping class name to class numerical id. - """ - return dict((v, k) for k, v in labelmap_dict.iteritems()) - - -def main(parsed_args): - all_box_annotations = pd.read_csv(parsed_args.input_annotations_boxes) - all_label_annotations = pd.read_csv(parsed_args.input_annotations_labels) - all_annotations = pd.concat([all_box_annotations, all_label_annotations]) - - class_label_map = _load_labelmap(parsed_args.input_class_labelmap) - relationship_label_map = _load_labelmap( - parsed_args.input_relationship_labelmap) - - relation_evaluator = vrd_evaluation.VRDRelationDetectionEvaluator() - phrase_evaluator = vrd_evaluation.VRDPhraseDetectionEvaluator() - - for _, groundtruth in enumerate(all_annotations.groupby('ImageID')): - image_id, image_groundtruth = groundtruth - groundtruth_dictionary = utils.build_groundtruth_vrd_dictionary( - image_groundtruth, class_label_map, relationship_label_map) - - relation_evaluator.add_single_ground_truth_image_info( - image_id, groundtruth_dictionary) - phrase_evaluator.add_single_ground_truth_image_info(image_id, - groundtruth_dictionary) - - all_predictions = pd.read_csv(parsed_args.input_predictions) - for _, prediction_data in enumerate(all_predictions.groupby('ImageID')): - image_id, image_predictions = prediction_data - prediction_dictionary = utils.build_predictions_vrd_dictionary( - image_predictions, class_label_map, relationship_label_map) - - relation_evaluator.add_single_detected_image_info(image_id, - prediction_dictionary) - phrase_evaluator.add_single_detected_image_info(image_id, - prediction_dictionary) - - relation_metrics = relation_evaluator.evaluate( - relationships=_swap_labelmap_dict(relationship_label_map)) - phrase_metrics = phrase_evaluator.evaluate( - relationships=_swap_labelmap_dict(relationship_label_map)) - - with open(parsed_args.output_metrics, 'w') as fid: - io_utils.write_csv(fid, relation_metrics) - io_utils.write_csv(fid, phrase_metrics) - - -if __name__ == '__main__': - - parser = argparse.ArgumentParser( - description= - 'Evaluate Open Images Visual Relationship Detection predictions.') - parser.add_argument( - '--input_annotations_vrd', - required=True, - help='File with groundtruth vrd annotations.') - parser.add_argument( - '--input_annotations_labels', - required=True, - help='File with groundtruth labels annotations') - parser.add_argument( - '--input_predictions', - required=True, - help="""File with detection predictions; NOTE: no postprocessing is - applied in the evaluation script.""") - parser.add_argument( - '--input_class_labelmap', - required=True, - help="""OpenImages Challenge labelmap; note: it is expected to include - attributes.""") - parser.add_argument( - '--input_relationship_labelmap', - required=True, - help="""OpenImages Challenge relationship labelmap.""") - parser.add_argument( - '--output_metrics', required=True, help='Output file with csv metrics') - - args = parser.parse_args() - main(args) diff --git a/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils.py b/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils.py deleted file mode 100644 index 590c8c84857..00000000000 --- a/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils.py +++ /dev/null @@ -1,125 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Converts data from CSV format to the VRDDetectionEvaluator format.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -from object_detection.core import standard_fields -from object_detection.utils import vrd_evaluation - - -def build_groundtruth_vrd_dictionary(data, class_label_map, - relationship_label_map): - """Builds a groundtruth dictionary from groundtruth data in CSV file. - - Args: - data: Pandas DataFrame with the groundtruth data for a single image. - class_label_map: Class labelmap from string label name to an integer. - relationship_label_map: Relationship type labelmap from string name to an - integer. - - Returns: - A dictionary with keys suitable for passing to - VRDDetectionEvaluator.add_single_ground_truth_image_info: - standard_fields.InputDataFields.groundtruth_boxes: A numpy array - of structures with the shape [M, 1], representing M tuples, each tuple - containing the same number of named bounding boxes. - Each box is of the format [y_min, x_min, y_max, x_max] (see - datatype vrd_box_data_type, single_box_data_type above). - standard_fields.InputDataFields.groundtruth_classes: A numpy array of - structures shape [M, 1], representing the class labels of the - corresponding bounding boxes and possibly additional classes (see - datatype label_data_type above). - standard_fields.InputDataFields.verified_labels: numpy array - of shape [K] containing verified labels. - """ - data_boxes = data[data.LabelName.isnull()] - data_labels = data[data.LabelName1.isnull()] - - boxes = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.vrd_box_data_type) - boxes['subject'] = data_boxes[['YMin1', 'XMin1', 'YMax1', - 'XMax1']].to_numpy() - boxes['object'] = data_boxes[['YMin2', 'XMin2', 'YMax2', 'XMax2']].to_numpy() - - labels = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.label_data_type) - labels['subject'] = data_boxes['LabelName1'].map( - lambda x: class_label_map[x]).to_numpy() - labels['object'] = data_boxes['LabelName2'].map( - lambda x: class_label_map[x]).to_numpy() - labels['relation'] = data_boxes['RelationshipLabel'].map( - lambda x: relationship_label_map[x]).to_numpy() - - return { - standard_fields.InputDataFields.groundtruth_boxes: - boxes, - standard_fields.InputDataFields.groundtruth_classes: - labels, - standard_fields.InputDataFields.groundtruth_image_classes: - data_labels['LabelName'].map(lambda x: class_label_map[x]) - .to_numpy(), - } - - -def build_predictions_vrd_dictionary(data, class_label_map, - relationship_label_map): - """Builds a predictions dictionary from predictions data in CSV file. - - Args: - data: Pandas DataFrame with the predictions data for a single image. - class_label_map: Class labelmap from string label name to an integer. - relationship_label_map: Relationship type labelmap from string name to an - integer. - - Returns: - Dictionary with keys suitable for passing to - VRDDetectionEvaluator.add_single_detected_image_info: - standard_fields.DetectionResultFields.detection_boxes: A numpy array of - structures with shape [N, 1], representing N tuples, each tuple - containing the same number of named bounding boxes. - Each box is of the format [y_min, x_min, y_max, x_max] (as an example - see datatype vrd_box_data_type, single_box_data_type above). - standard_fields.DetectionResultFields.detection_scores: float32 numpy - array of shape [N] containing detection scores for the boxes. - standard_fields.DetectionResultFields.detection_classes: A numpy array - of structures shape [N, 1], representing the class labels of the - corresponding bounding boxes and possibly additional classes (see - datatype label_data_type above). - """ - data_boxes = data - - boxes = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.vrd_box_data_type) - boxes['subject'] = data_boxes[['YMin1', 'XMin1', 'YMax1', - 'XMax1']].to_numpy() - boxes['object'] = data_boxes[['YMin2', 'XMin2', 'YMax2', 'XMax2']].to_numpy() - - labels = np.zeros(data_boxes.shape[0], dtype=vrd_evaluation.label_data_type) - labels['subject'] = data_boxes['LabelName1'].map( - lambda x: class_label_map[x]).to_numpy() - labels['object'] = data_boxes['LabelName2'].map( - lambda x: class_label_map[x]).to_numpy() - labels['relation'] = data_boxes['RelationshipLabel'].map( - lambda x: relationship_label_map[x]).to_numpy() - - return { - standard_fields.DetectionResultFields.detection_boxes: - boxes, - standard_fields.DetectionResultFields.detection_classes: - labels, - standard_fields.DetectionResultFields.detection_scores: - data_boxes['Score'].to_numpy() - } diff --git a/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils_test.py b/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils_test.py deleted file mode 100644 index 04547bbedac..00000000000 --- a/research/object_detection/metrics/oid_vrd_challenge_evaluation_utils_test.py +++ /dev/null @@ -1,149 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for oid_vrd_challenge_evaluation_utils.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import numpy as np -import pandas as pd -import tensorflow.compat.v1 as tf -from object_detection.core import standard_fields -from object_detection.metrics import oid_vrd_challenge_evaluation_utils as utils -from object_detection.utils import vrd_evaluation - - -class OidVrdChallengeEvaluationUtilsTest(tf.test.TestCase): - - def testBuildGroundtruthDictionary(self): - np_data = pd.DataFrame( - [[ - 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.3, 0.5, 0.6, - 0.0, 0.3, 0.5, 0.6, 'is', None, None - ], [ - 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/02gy9n', 0.0, 0.3, 0.5, 0.6, - 0.1, 0.2, 0.3, 0.4, 'under', None, None - ], [ - 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.1, 0.2, 0.3, - 0.0, 0.1, 0.2, 0.3, 'is', None, None - ], [ - 'fe58ec1b06db2bb7', '/m/083vt', '/m/04bcr3', 0.1, 0.2, 0.3, 0.4, - 0.5, 0.6, 0.7, 0.8, 'at', None, None - ], [ - 'fe58ec1b06db2bb7', None, None, None, None, None, None, None, None, - None, None, None, '/m/04bcr3', 1.0 - ], [ - 'fe58ec1b06db2bb7', None, None, None, None, None, None, None, None, - None, None, None, '/m/083vt', 0.0 - ], [ - 'fe58ec1b06db2bb7', None, None, None, None, None, None, None, None, - None, None, None, '/m/02gy9n', 0.0 - ]], - columns=[ - 'ImageID', 'LabelName1', 'LabelName2', 'XMin1', 'XMax1', 'YMin1', - 'YMax1', 'XMin2', 'XMax2', 'YMin2', 'YMax2', 'RelationshipLabel', - 'LabelName', 'Confidence' - ]) - class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} - relationship_label_map = {'is': 1, 'under': 2, 'at': 3} - groundtruth_dictionary = utils.build_groundtruth_vrd_dictionary( - np_data, class_label_map, relationship_label_map) - - self.assertTrue(standard_fields.InputDataFields.groundtruth_boxes in - groundtruth_dictionary) - self.assertTrue(standard_fields.InputDataFields.groundtruth_classes in - groundtruth_dictionary) - self.assertTrue(standard_fields.InputDataFields.groundtruth_image_classes in - groundtruth_dictionary) - - self.assertAllEqual( - np.array( - [(1, 2, 1), (1, 3, 2), (1, 2, 1), (2, 1, 3)], - dtype=vrd_evaluation.label_data_type), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_classes]) - expected_vrd_data = np.array( - [ - ([0.5, 0.0, 0.6, 0.3], [0.5, 0.0, 0.6, 0.3]), - ([0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2]), - ([0.2, 0.0, 0.3, 0.1], [0.2, 0.0, 0.3, 0.1]), - ([0.3, 0.1, 0.4, 0.2], [0.7, 0.5, 0.8, 0.6]), - ], - dtype=vrd_evaluation.vrd_box_data_type) - for field in expected_vrd_data.dtype.fields: - self.assertNDArrayNear( - expected_vrd_data[field], groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_boxes][field], 1e-5) - self.assertAllEqual( - np.array([1, 2, 3]), groundtruth_dictionary[ - standard_fields.InputDataFields.groundtruth_image_classes]) - - def testBuildPredictionDictionary(self): - np_data = pd.DataFrame( - [[ - 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.3, 0.5, 0.6, - 0.0, 0.3, 0.5, 0.6, 'is', 0.1 - ], [ - 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/02gy9n', 0.0, 0.3, 0.5, 0.6, - 0.1, 0.2, 0.3, 0.4, 'under', 0.2 - ], [ - 'fe58ec1b06db2bb7', '/m/04bcr3', '/m/083vt', 0.0, 0.1, 0.2, 0.3, - 0.0, 0.1, 0.2, 0.3, 'is', 0.3 - ], [ - 'fe58ec1b06db2bb7', '/m/083vt', '/m/04bcr3', 0.1, 0.2, 0.3, 0.4, - 0.5, 0.6, 0.7, 0.8, 'at', 0.4 - ]], - columns=[ - 'ImageID', 'LabelName1', 'LabelName2', 'XMin1', 'XMax1', 'YMin1', - 'YMax1', 'XMin2', 'XMax2', 'YMin2', 'YMax2', 'RelationshipLabel', - 'Score' - ]) - class_label_map = {'/m/04bcr3': 1, '/m/083vt': 2, '/m/02gy9n': 3} - relationship_label_map = {'is': 1, 'under': 2, 'at': 3} - prediction_dictionary = utils.build_predictions_vrd_dictionary( - np_data, class_label_map, relationship_label_map) - - self.assertTrue(standard_fields.DetectionResultFields.detection_boxes in - prediction_dictionary) - self.assertTrue(standard_fields.DetectionResultFields.detection_classes in - prediction_dictionary) - self.assertTrue(standard_fields.DetectionResultFields.detection_scores in - prediction_dictionary) - - self.assertAllEqual( - np.array( - [(1, 2, 1), (1, 3, 2), (1, 2, 1), (2, 1, 3)], - dtype=vrd_evaluation.label_data_type), prediction_dictionary[ - standard_fields.DetectionResultFields.detection_classes]) - expected_vrd_data = np.array( - [ - ([0.5, 0.0, 0.6, 0.3], [0.5, 0.0, 0.6, 0.3]), - ([0.5, 0.0, 0.6, 0.3], [0.3, 0.1, 0.4, 0.2]), - ([0.2, 0.0, 0.3, 0.1], [0.2, 0.0, 0.3, 0.1]), - ([0.3, 0.1, 0.4, 0.2], [0.7, 0.5, 0.8, 0.6]), - ], - dtype=vrd_evaluation.vrd_box_data_type) - for field in expected_vrd_data.dtype.fields: - self.assertNDArrayNear( - expected_vrd_data[field], prediction_dictionary[ - standard_fields.DetectionResultFields.detection_boxes][field], - 1e-5) - self.assertNDArrayNear( - np.array([0.1, 0.2, 0.3, 0.4]), prediction_dictionary[ - standard_fields.DetectionResultFields.detection_scores], 1e-5) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/metrics/tf_example_parser.py b/research/object_detection/metrics/tf_example_parser.py deleted file mode 100644 index 7490f86da1a..00000000000 --- a/research/object_detection/metrics/tf_example_parser.py +++ /dev/null @@ -1,159 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tensorflow Example proto parser for data loading. - -A parser to decode data containing serialized tensorflow.Example -protos into materialized tensors (numpy arrays). -""" - -import numpy as np - -from object_detection.core import data_parser -from object_detection.core import standard_fields as fields - - -class FloatParser(data_parser.DataToNumpyParser): - """Tensorflow Example float parser.""" - - def __init__(self, field_name): - self.field_name = field_name - - def parse(self, tf_example): - return np.array( - tf_example.features.feature[self.field_name].float_list.value, - dtype=float).transpose() if tf_example.features.feature[ - self.field_name].HasField("float_list") else None - - -class StringParser(data_parser.DataToNumpyParser): - """Tensorflow Example string parser.""" - - def __init__(self, field_name): - self.field_name = field_name - - def parse(self, tf_example): - return b"".join(tf_example.features.feature[ - self.field_name].bytes_list.value) if tf_example.features.feature[ - self.field_name].HasField("bytes_list") else None - - -class Int64Parser(data_parser.DataToNumpyParser): - """Tensorflow Example int64 parser.""" - - def __init__(self, field_name): - self.field_name = field_name - - def parse(self, tf_example): - return np.array( - tf_example.features.feature[self.field_name].int64_list.value, - dtype=np.int64).transpose() if tf_example.features.feature[ - self.field_name].HasField("int64_list") else None - - -class BoundingBoxParser(data_parser.DataToNumpyParser): - """Tensorflow Example bounding box parser.""" - - def __init__(self, xmin_field_name, ymin_field_name, xmax_field_name, - ymax_field_name): - self.field_names = [ - ymin_field_name, xmin_field_name, ymax_field_name, xmax_field_name - ] - - def parse(self, tf_example): - result = [] - parsed = True - for field_name in self.field_names: - result.append(tf_example.features.feature[field_name].float_list.value) - parsed &= ( - tf_example.features.feature[field_name].HasField("float_list")) - - return np.array(result).transpose() if parsed else None - - -class TfExampleDetectionAndGTParser(data_parser.DataToNumpyParser): - """Tensorflow Example proto parser.""" - - def __init__(self): - self.items_to_handlers = { - fields.DetectionResultFields.key: - StringParser(fields.TfExampleFields.source_id), - # Object ground truth boxes and classes. - fields.InputDataFields.groundtruth_boxes: (BoundingBoxParser( - fields.TfExampleFields.object_bbox_xmin, - fields.TfExampleFields.object_bbox_ymin, - fields.TfExampleFields.object_bbox_xmax, - fields.TfExampleFields.object_bbox_ymax)), - fields.InputDataFields.groundtruth_classes: ( - Int64Parser(fields.TfExampleFields.object_class_label)), - # Object detections. - fields.DetectionResultFields.detection_boxes: (BoundingBoxParser( - fields.TfExampleFields.detection_bbox_xmin, - fields.TfExampleFields.detection_bbox_ymin, - fields.TfExampleFields.detection_bbox_xmax, - fields.TfExampleFields.detection_bbox_ymax)), - fields.DetectionResultFields.detection_classes: ( - Int64Parser(fields.TfExampleFields.detection_class_label)), - fields.DetectionResultFields.detection_scores: ( - FloatParser(fields.TfExampleFields.detection_score)), - } - - self.optional_items_to_handlers = { - fields.InputDataFields.groundtruth_difficult: - Int64Parser(fields.TfExampleFields.object_difficult), - fields.InputDataFields.groundtruth_group_of: - Int64Parser(fields.TfExampleFields.object_group_of), - fields.InputDataFields.groundtruth_image_classes: - Int64Parser(fields.TfExampleFields.image_class_label), - } - - def parse(self, tf_example): - """Parses tensorflow example and returns a tensor dictionary. - - Args: - tf_example: a tf.Example object. - - Returns: - A dictionary of the following numpy arrays: - fields.DetectionResultFields.source_id - string containing original image - id. - fields.InputDataFields.groundtruth_boxes - a numpy array containing - groundtruth boxes. - fields.InputDataFields.groundtruth_classes - a numpy array containing - groundtruth classes. - fields.InputDataFields.groundtruth_group_of - a numpy array containing - groundtruth group of flag (optional, None if not specified). - fields.InputDataFields.groundtruth_difficult - a numpy array containing - groundtruth difficult flag (optional, None if not specified). - fields.InputDataFields.groundtruth_image_classes - a numpy array - containing groundtruth image-level labels. - fields.DetectionResultFields.detection_boxes - a numpy array containing - detection boxes. - fields.DetectionResultFields.detection_classes - a numpy array containing - detection class labels. - fields.DetectionResultFields.detection_scores - a numpy array containing - detection scores. - Returns None if tf.Example was not parsed or non-optional fields were not - found. - """ - results_dict = {} - parsed = True - for key, parser in self.items_to_handlers.items(): - results_dict[key] = parser.parse(tf_example) - parsed &= (results_dict[key] is not None) - - for key, parser in self.optional_items_to_handlers.items(): - results_dict[key] = parser.parse(tf_example) - - return results_dict if parsed else None diff --git a/research/object_detection/metrics/tf_example_parser_test.py b/research/object_detection/metrics/tf_example_parser_test.py deleted file mode 100644 index c195c7376ac..00000000000 --- a/research/object_detection/metrics/tf_example_parser_test.py +++ /dev/null @@ -1,197 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object_detection.data_decoders.tf_example_parser.""" - -import numpy as np -import numpy.testing as np_testing -import tensorflow.compat.v1 as tf - -from object_detection.core import standard_fields as fields -from object_detection.metrics import tf_example_parser - - -class TfExampleDecoderTest(tf.test.TestCase): - - def _Int64Feature(self, value): - return tf.train.Feature(int64_list=tf.train.Int64List(value=value)) - - def _FloatFeature(self, value): - return tf.train.Feature(float_list=tf.train.FloatList(value=value)) - - def _BytesFeature(self, value): - return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) - - def testParseDetectionsAndGT(self): - source_id = b'abc.jpg' - # y_min, x_min, y_max, x_max - object_bb = np.array([[0.0, 0.5, 0.3], [0.0, 0.1, 0.6], [1.0, 0.6, 0.8], - [1.0, 0.6, 0.7]]).transpose() - detection_bb = np.array([[0.1, 0.2], [0.0, 0.8], [1.0, 0.6], - [1.0, 0.85]]).transpose() - - object_class_label = [1, 1, 2] - object_difficult = [1, 0, 0] - object_group_of = [0, 0, 1] - verified_labels = [1, 2, 3, 4] - detection_class_label = [2, 1] - detection_score = [0.5, 0.3] - features = { - fields.TfExampleFields.source_id: - self._BytesFeature(source_id), - fields.TfExampleFields.object_bbox_ymin: - self._FloatFeature(object_bb[:, 0].tolist()), - fields.TfExampleFields.object_bbox_xmin: - self._FloatFeature(object_bb[:, 1].tolist()), - fields.TfExampleFields.object_bbox_ymax: - self._FloatFeature(object_bb[:, 2].tolist()), - fields.TfExampleFields.object_bbox_xmax: - self._FloatFeature(object_bb[:, 3].tolist()), - fields.TfExampleFields.detection_bbox_ymin: - self._FloatFeature(detection_bb[:, 0].tolist()), - fields.TfExampleFields.detection_bbox_xmin: - self._FloatFeature(detection_bb[:, 1].tolist()), - fields.TfExampleFields.detection_bbox_ymax: - self._FloatFeature(detection_bb[:, 2].tolist()), - fields.TfExampleFields.detection_bbox_xmax: - self._FloatFeature(detection_bb[:, 3].tolist()), - fields.TfExampleFields.detection_class_label: - self._Int64Feature(detection_class_label), - fields.TfExampleFields.detection_score: - self._FloatFeature(detection_score), - } - - example = tf.train.Example(features=tf.train.Features(feature=features)) - parser = tf_example_parser.TfExampleDetectionAndGTParser() - - results_dict = parser.parse(example) - self.assertIsNone(results_dict) - - features[fields.TfExampleFields.object_class_label] = ( - self._Int64Feature(object_class_label)) - features[fields.TfExampleFields.object_difficult] = ( - self._Int64Feature(object_difficult)) - - example = tf.train.Example(features=tf.train.Features(feature=features)) - results_dict = parser.parse(example) - - self.assertIsNotNone(results_dict) - self.assertEqual(source_id, results_dict[fields.DetectionResultFields.key]) - np_testing.assert_almost_equal( - object_bb, results_dict[fields.InputDataFields.groundtruth_boxes]) - np_testing.assert_almost_equal( - detection_bb, - results_dict[fields.DetectionResultFields.detection_boxes]) - np_testing.assert_almost_equal( - detection_score, - results_dict[fields.DetectionResultFields.detection_scores]) - np_testing.assert_almost_equal( - detection_class_label, - results_dict[fields.DetectionResultFields.detection_classes]) - np_testing.assert_almost_equal( - object_difficult, - results_dict[fields.InputDataFields.groundtruth_difficult]) - np_testing.assert_almost_equal( - object_class_label, - results_dict[fields.InputDataFields.groundtruth_classes]) - - parser = tf_example_parser.TfExampleDetectionAndGTParser() - - features[fields.TfExampleFields.object_group_of] = ( - self._Int64Feature(object_group_of)) - - example = tf.train.Example(features=tf.train.Features(feature=features)) - results_dict = parser.parse(example) - self.assertIsNotNone(results_dict) - np_testing.assert_equal( - object_group_of, - results_dict[fields.InputDataFields.groundtruth_group_of]) - - features[fields.TfExampleFields.image_class_label] = ( - self._Int64Feature(verified_labels)) - - example = tf.train.Example(features=tf.train.Features(feature=features)) - results_dict = parser.parse(example) - self.assertIsNotNone(results_dict) - np_testing.assert_equal( - verified_labels, - results_dict[fields.InputDataFields.groundtruth_image_classes]) - - def testParseString(self): - string_val = b'abc' - features = {'string': self._BytesFeature(string_val)} - example = tf.train.Example(features=tf.train.Features(feature=features)) - - parser = tf_example_parser.StringParser('string') - result = parser.parse(example) - self.assertIsNotNone(result) - self.assertEqual(result, string_val) - - parser = tf_example_parser.StringParser('another_string') - result = parser.parse(example) - self.assertIsNone(result) - - def testParseFloat(self): - float_array_val = [1.5, 1.4, 2.0] - features = {'floats': self._FloatFeature(float_array_val)} - example = tf.train.Example(features=tf.train.Features(feature=features)) - - parser = tf_example_parser.FloatParser('floats') - result = parser.parse(example) - self.assertIsNotNone(result) - np_testing.assert_almost_equal(result, float_array_val) - - parser = tf_example_parser.StringParser('another_floats') - result = parser.parse(example) - self.assertIsNone(result) - - def testInt64Parser(self): - int_val = [1, 2, 3] - features = {'ints': self._Int64Feature(int_val)} - example = tf.train.Example(features=tf.train.Features(feature=features)) - - parser = tf_example_parser.Int64Parser('ints') - result = parser.parse(example) - self.assertIsNotNone(result) - np_testing.assert_almost_equal(result, int_val) - - parser = tf_example_parser.Int64Parser('another_ints') - result = parser.parse(example) - self.assertIsNone(result) - - def testBoundingBoxParser(self): - bounding_boxes = np.array([[0.0, 0.5, 0.3], [0.0, 0.1, 0.6], - [1.0, 0.6, 0.8], [1.0, 0.6, 0.7]]).transpose() - features = { - 'ymin': self._FloatFeature(bounding_boxes[:, 0]), - 'xmin': self._FloatFeature(bounding_boxes[:, 1]), - 'ymax': self._FloatFeature(bounding_boxes[:, 2]), - 'xmax': self._FloatFeature(bounding_boxes[:, 3]) - } - - example = tf.train.Example(features=tf.train.Features(feature=features)) - - parser = tf_example_parser.BoundingBoxParser('xmin', 'ymin', 'xmax', 'ymax') - result = parser.parse(example) - self.assertIsNotNone(result) - np_testing.assert_almost_equal(result, bounding_boxes) - - parser = tf_example_parser.BoundingBoxParser('xmin', 'ymin', 'xmax', - 'another_ymax') - result = parser.parse(example) - self.assertIsNone(result) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/model_hparams.py b/research/object_detection/model_hparams.py deleted file mode 100644 index 12b043e9b1c..00000000000 --- a/research/object_detection/model_hparams.py +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Hyperparameters for the object detection model in TF.learn. - -This file consolidates and documents the hyperparameters used by the model. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -# pylint: disable=g-import-not-at-top -try: - from tensorflow.contrib import training as contrib_training -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - - -def create_hparams(hparams_overrides=None): - """Returns hyperparameters, including any flag value overrides. - - Args: - hparams_overrides: Optional hparams overrides, represented as a - string containing comma-separated hparam_name=value pairs. - - Returns: - The hyperparameters as a tf.HParams object. - """ - hparams = contrib_training.HParams( - # Whether a fine tuning checkpoint (provided in the pipeline config) - # should be loaded for training. - load_pretrained=True) - # Override any of the preceding hyperparameter values. - if hparams_overrides: - hparams = hparams.parse(hparams_overrides) - return hparams diff --git a/research/object_detection/model_lib.py b/research/object_detection/model_lib.py deleted file mode 100644 index 27a3816fc40..00000000000 --- a/research/object_detection/model_lib.py +++ /dev/null @@ -1,1162 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Constructs model, inputs, and training environment.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import copy -import functools -import os - -import tensorflow.compat.v1 as tf -from tensorflow.compat.v1 import estimator as tf_estimator -import tensorflow.compat.v2 as tf2 -import tf_slim as slim - -from object_detection import eval_util -from object_detection import exporter as exporter_lib -from object_detection import inputs -from object_detection.builders import graph_rewriter_builder -from object_detection.builders import model_builder -from object_detection.builders import optimizer_builder -from object_detection.core import standard_fields as fields -from object_detection.utils import config_util -from object_detection.utils import label_map_util -from object_detection.utils import ops -from object_detection.utils import shape_utils -from object_detection.utils import variables_helper -from object_detection.utils import visualization_utils as vis_utils - -# pylint: disable=g-import-not-at-top -try: - from tensorflow.contrib import learn as contrib_learn -except ImportError: - # TF 2.0 doesn't ship with contrib. - pass -# pylint: enable=g-import-not-at-top - -# A map of names to methods that help build the model. -MODEL_BUILD_UTIL_MAP = { - 'get_configs_from_pipeline_file': - config_util.get_configs_from_pipeline_file, - 'create_pipeline_proto_from_configs': - config_util.create_pipeline_proto_from_configs, - 'merge_external_params_with_configs': - config_util.merge_external_params_with_configs, - 'create_train_input_fn': - inputs.create_train_input_fn, - 'create_eval_input_fn': - inputs.create_eval_input_fn, - 'create_predict_input_fn': - inputs.create_predict_input_fn, - 'detection_model_fn_base': - model_builder.build, -} - - -def _prepare_groundtruth_for_eval(detection_model, class_agnostic, - max_number_of_boxes): - """Extracts groundtruth data from detection_model and prepares it for eval. - - Args: - detection_model: A `DetectionModel` object. - class_agnostic: Whether the detections are class_agnostic. - max_number_of_boxes: Max number of groundtruth boxes. - - Returns: - A tuple of: - groundtruth: Dictionary with the following fields: - 'groundtruth_boxes': [batch_size, num_boxes, 4] float32 tensor of boxes, - in normalized coordinates. - 'groundtruth_classes': [batch_size, num_boxes] int64 tensor of 1-indexed - classes. - 'groundtruth_masks': 4D float32 tensor of instance masks (if provided in - groundtruth) - 'groundtruth_is_crowd': [batch_size, num_boxes] bool tensor indicating - is_crowd annotations (if provided in groundtruth). - 'groundtruth_area': [batch_size, num_boxes] float32 tensor indicating - the area (in the original absolute coordinates) of annotations (if - provided in groundtruth). - 'num_groundtruth_boxes': [batch_size] tensor containing the maximum number - of groundtruth boxes per image.. - 'groundtruth_keypoints': [batch_size, num_boxes, num_keypoints, 2] float32 - tensor of keypoints (if provided in groundtruth). - 'groundtruth_dp_num_points_list': [batch_size, num_boxes] int32 tensor - with the number of DensePose points for each instance (if provided in - groundtruth). - 'groundtruth_dp_part_ids_list': [batch_size, num_boxes, - max_sampled_points] int32 tensor with the part ids for each DensePose - sampled point (if provided in groundtruth). - 'groundtruth_dp_surface_coords_list': [batch_size, num_boxes, - max_sampled_points, 4] containing the DensePose surface coordinates for - each sampled point (if provided in groundtruth). - 'groundtruth_track_ids_list': [batch_size, num_boxes] int32 tensor - with track ID for each instance (if provided in groundtruth). - 'groundtruth_group_of': [batch_size, num_boxes] bool tensor indicating - group_of annotations (if provided in groundtruth). - 'groundtruth_labeled_classes': [batch_size, num_classes] int64 - tensor of 1-indexed classes. - 'groundtruth_verified_neg_classes': [batch_size, num_classes] float32 - K-hot representation of 1-indexed classes which were verified as not - present in the image. - 'groundtruth_not_exhaustive_classes': [batch_size, num_classes] K-hot - representation of 1-indexed classes which don't have all of their - instances marked exhaustively. - 'input_data_fields.groundtruth_image_classes': integer representation of - the classes that were sent for verification for a given image. Note that - this field does not support batching as the number of classes can be - variable. - class_agnostic: Boolean indicating whether detections are class agnostic. - """ - input_data_fields = fields.InputDataFields() - groundtruth_boxes = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.boxes)) - groundtruth_boxes_shape = tf.shape(groundtruth_boxes) - # For class-agnostic models, groundtruth one-hot encodings collapse to all - # ones. - if class_agnostic: - groundtruth_classes_one_hot = tf.ones( - [groundtruth_boxes_shape[0], groundtruth_boxes_shape[1], 1]) - else: - groundtruth_classes_one_hot = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.classes)) - label_id_offset = 1 # Applying label id offset (b/63711816) - groundtruth_classes = ( - tf.argmax(groundtruth_classes_one_hot, axis=2) + label_id_offset) - groundtruth = { - input_data_fields.groundtruth_boxes: groundtruth_boxes, - input_data_fields.groundtruth_classes: groundtruth_classes - } - - if detection_model.groundtruth_has_field( - input_data_fields.groundtruth_image_classes): - groundtruth_image_classes_k_hot = tf.stack( - detection_model.groundtruth_lists( - input_data_fields.groundtruth_image_classes)) - groundtruth_image_classes = tf.expand_dims( - tf.where(groundtruth_image_classes_k_hot > 0)[:, 1], 0) - # Adds back label_id_offset as it is subtracted in - # convert_labeled_classes_to_k_hot. - groundtruth[ - input_data_fields. - groundtruth_image_classes] = groundtruth_image_classes + label_id_offset - - if detection_model.groundtruth_has_field(fields.BoxListFields.masks): - groundtruth[input_data_fields.groundtruth_instance_masks] = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.masks)) - - if detection_model.groundtruth_has_field(fields.BoxListFields.is_crowd): - groundtruth[input_data_fields.groundtruth_is_crowd] = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.is_crowd)) - - if detection_model.groundtruth_has_field(input_data_fields.groundtruth_area): - groundtruth[input_data_fields.groundtruth_area] = tf.stack( - detection_model.groundtruth_lists(input_data_fields.groundtruth_area)) - - if detection_model.groundtruth_has_field(fields.BoxListFields.keypoints): - groundtruth[input_data_fields.groundtruth_keypoints] = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.keypoints)) - - if detection_model.groundtruth_has_field( - fields.BoxListFields.keypoint_depths): - groundtruth[input_data_fields.groundtruth_keypoint_depths] = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.keypoint_depths)) - groundtruth[ - input_data_fields.groundtruth_keypoint_depth_weights] = tf.stack( - detection_model.groundtruth_lists( - fields.BoxListFields.keypoint_depth_weights)) - - if detection_model.groundtruth_has_field( - fields.BoxListFields.keypoint_visibilities): - groundtruth[input_data_fields.groundtruth_keypoint_visibilities] = tf.stack( - detection_model.groundtruth_lists( - fields.BoxListFields.keypoint_visibilities)) - - if detection_model.groundtruth_has_field(fields.BoxListFields.group_of): - groundtruth[input_data_fields.groundtruth_group_of] = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.group_of)) - - label_id_offset_paddings = tf.constant([[0, 0], [1, 0]]) - if detection_model.groundtruth_has_field( - input_data_fields.groundtruth_verified_neg_classes): - groundtruth[input_data_fields.groundtruth_verified_neg_classes] = tf.pad( - tf.stack( - detection_model.groundtruth_lists( - input_data_fields.groundtruth_verified_neg_classes)), - label_id_offset_paddings) - - if detection_model.groundtruth_has_field( - input_data_fields.groundtruth_not_exhaustive_classes): - groundtruth[input_data_fields.groundtruth_not_exhaustive_classes] = tf.pad( - tf.stack( - detection_model.groundtruth_lists( - input_data_fields.groundtruth_not_exhaustive_classes)), - label_id_offset_paddings) - - if detection_model.groundtruth_has_field( - fields.BoxListFields.densepose_num_points): - groundtruth[input_data_fields.groundtruth_dp_num_points] = tf.stack( - detection_model.groundtruth_lists( - fields.BoxListFields.densepose_num_points)) - if detection_model.groundtruth_has_field( - fields.BoxListFields.densepose_part_ids): - groundtruth[input_data_fields.groundtruth_dp_part_ids] = tf.stack( - detection_model.groundtruth_lists( - fields.BoxListFields.densepose_part_ids)) - if detection_model.groundtruth_has_field( - fields.BoxListFields.densepose_surface_coords): - groundtruth[input_data_fields.groundtruth_dp_surface_coords] = tf.stack( - detection_model.groundtruth_lists( - fields.BoxListFields.densepose_surface_coords)) - - if detection_model.groundtruth_has_field(fields.BoxListFields.track_ids): - groundtruth[input_data_fields.groundtruth_track_ids] = tf.stack( - detection_model.groundtruth_lists(fields.BoxListFields.track_ids)) - - if detection_model.groundtruth_has_field( - input_data_fields.groundtruth_labeled_classes): - groundtruth[input_data_fields.groundtruth_labeled_classes] = tf.pad( - tf.stack( - detection_model.groundtruth_lists( - input_data_fields.groundtruth_labeled_classes)), - label_id_offset_paddings) - - groundtruth[input_data_fields.num_groundtruth_boxes] = ( - tf.tile([max_number_of_boxes], multiples=[groundtruth_boxes_shape[0]])) - return groundtruth - - -def unstack_batch(tensor_dict, unpad_groundtruth_tensors=True): - """Unstacks all tensors in `tensor_dict` along 0th dimension. - - Unstacks tensor from the tensor dict along 0th dimension and returns a - tensor_dict containing values that are lists of unstacked, unpadded tensors. - - Tensors in the `tensor_dict` are expected to be of one of the three shapes: - 1. [batch_size] - 2. [batch_size, height, width, channels] - 3. [batch_size, num_boxes, d1, d2, ... dn] - - When unpad_groundtruth_tensors is set to true, unstacked tensors of form 3 - above are sliced along the `num_boxes` dimension using the value in tensor - field.InputDataFields.num_groundtruth_boxes. - - Note that this function has a static list of input data fields and has to be - kept in sync with the InputDataFields defined in core/standard_fields.py - - Args: - tensor_dict: A dictionary of batched groundtruth tensors. - unpad_groundtruth_tensors: Whether to remove padding along `num_boxes` - dimension of the groundtruth tensors. - - Returns: - A dictionary where the keys are from fields.InputDataFields and values are - a list of unstacked (optionally unpadded) tensors. - - Raises: - ValueError: If unpad_tensors is True and `tensor_dict` does not contain - `num_groundtruth_boxes` tensor. - """ - unbatched_tensor_dict = { - key: tf.unstack(tensor) for key, tensor in tensor_dict.items() - } - if unpad_groundtruth_tensors: - if (fields.InputDataFields.num_groundtruth_boxes - not in unbatched_tensor_dict): - raise ValueError('`num_groundtruth_boxes` not found in tensor_dict. ' - 'Keys available: {}'.format( - unbatched_tensor_dict.keys())) - unbatched_unpadded_tensor_dict = {} - unpad_keys = set([ - # List of input data fields that are padded along the num_boxes - # dimension. This list has to be kept in sync with InputDataFields in - # standard_fields.py. - fields.InputDataFields.groundtruth_instance_masks, - fields.InputDataFields.groundtruth_instance_mask_weights, - fields.InputDataFields.groundtruth_classes, - fields.InputDataFields.groundtruth_boxes, - fields.InputDataFields.groundtruth_keypoints, - fields.InputDataFields.groundtruth_keypoint_depths, - fields.InputDataFields.groundtruth_keypoint_depth_weights, - fields.InputDataFields.groundtruth_keypoint_visibilities, - fields.InputDataFields.groundtruth_dp_num_points, - fields.InputDataFields.groundtruth_dp_part_ids, - fields.InputDataFields.groundtruth_dp_surface_coords, - fields.InputDataFields.groundtruth_track_ids, - fields.InputDataFields.groundtruth_group_of, - fields.InputDataFields.groundtruth_difficult, - fields.InputDataFields.groundtruth_is_crowd, - fields.InputDataFields.groundtruth_area, - fields.InputDataFields.groundtruth_weights - ]).intersection(set(unbatched_tensor_dict.keys())) - - for key in unpad_keys: - unpadded_tensor_list = [] - for num_gt, padded_tensor in zip( - unbatched_tensor_dict[fields.InputDataFields.num_groundtruth_boxes], - unbatched_tensor_dict[key]): - tensor_shape = shape_utils.combined_static_and_dynamic_shape( - padded_tensor) - slice_begin = tf.zeros([len(tensor_shape)], dtype=tf.int32) - slice_size = tf.stack( - [num_gt] + [-1 if dim is None else dim for dim in tensor_shape[1:]]) - unpadded_tensor = tf.slice(padded_tensor, slice_begin, slice_size) - unpadded_tensor_list.append(unpadded_tensor) - unbatched_unpadded_tensor_dict[key] = unpadded_tensor_list - - unbatched_tensor_dict.update(unbatched_unpadded_tensor_dict) - - return unbatched_tensor_dict - - -def provide_groundtruth(model, labels, training_step=None): - """Provides the labels to a model as groundtruth. - - This helper function extracts the corresponding boxes, classes, - keypoints, weights, masks, etc. from the labels, and provides it - as groundtruth to the models. - - Args: - model: The detection model to provide groundtruth to. - labels: The labels for the training or evaluation inputs. - training_step: int, optional. The training step for the model. Useful for - models which want to anneal loss weights. - """ - gt_boxes_list = labels[fields.InputDataFields.groundtruth_boxes] - gt_classes_list = labels[fields.InputDataFields.groundtruth_classes] - gt_masks_list = None - if fields.InputDataFields.groundtruth_instance_masks in labels: - gt_masks_list = labels[fields.InputDataFields.groundtruth_instance_masks] - gt_mask_weights_list = None - if fields.InputDataFields.groundtruth_instance_mask_weights in labels: - gt_mask_weights_list = labels[ - fields.InputDataFields.groundtruth_instance_mask_weights] - gt_keypoints_list = None - if fields.InputDataFields.groundtruth_keypoints in labels: - gt_keypoints_list = labels[fields.InputDataFields.groundtruth_keypoints] - gt_keypoint_depths_list = None - gt_keypoint_depth_weights_list = None - if fields.InputDataFields.groundtruth_keypoint_depths in labels: - gt_keypoint_depths_list = ( - labels[fields.InputDataFields.groundtruth_keypoint_depths]) - gt_keypoint_depth_weights_list = ( - labels[fields.InputDataFields.groundtruth_keypoint_depth_weights]) - gt_keypoint_visibilities_list = None - if fields.InputDataFields.groundtruth_keypoint_visibilities in labels: - gt_keypoint_visibilities_list = labels[ - fields.InputDataFields.groundtruth_keypoint_visibilities] - gt_dp_num_points_list = None - if fields.InputDataFields.groundtruth_dp_num_points in labels: - gt_dp_num_points_list = labels[ - fields.InputDataFields.groundtruth_dp_num_points] - gt_dp_part_ids_list = None - if fields.InputDataFields.groundtruth_dp_part_ids in labels: - gt_dp_part_ids_list = labels[fields.InputDataFields.groundtruth_dp_part_ids] - gt_dp_surface_coords_list = None - if fields.InputDataFields.groundtruth_dp_surface_coords in labels: - gt_dp_surface_coords_list = labels[ - fields.InputDataFields.groundtruth_dp_surface_coords] - gt_track_ids_list = None - if fields.InputDataFields.groundtruth_track_ids in labels: - gt_track_ids_list = labels[fields.InputDataFields.groundtruth_track_ids] - gt_weights_list = None - if fields.InputDataFields.groundtruth_weights in labels: - gt_weights_list = labels[fields.InputDataFields.groundtruth_weights] - gt_confidences_list = None - if fields.InputDataFields.groundtruth_confidences in labels: - gt_confidences_list = labels[fields.InputDataFields.groundtruth_confidences] - gt_is_crowd_list = None - if fields.InputDataFields.groundtruth_is_crowd in labels: - gt_is_crowd_list = labels[fields.InputDataFields.groundtruth_is_crowd] - gt_group_of_list = None - if fields.InputDataFields.groundtruth_group_of in labels: - gt_group_of_list = labels[fields.InputDataFields.groundtruth_group_of] - gt_area_list = None - if fields.InputDataFields.groundtruth_area in labels: - gt_area_list = labels[fields.InputDataFields.groundtruth_area] - gt_labeled_classes = None - if fields.InputDataFields.groundtruth_labeled_classes in labels: - gt_labeled_classes = labels[ - fields.InputDataFields.groundtruth_labeled_classes] - gt_verified_neg_classes = None - if fields.InputDataFields.groundtruth_verified_neg_classes in labels: - gt_verified_neg_classes = labels[ - fields.InputDataFields.groundtruth_verified_neg_classes] - gt_not_exhaustive_classes = None - if fields.InputDataFields.groundtruth_not_exhaustive_classes in labels: - gt_not_exhaustive_classes = labels[ - fields.InputDataFields.groundtruth_not_exhaustive_classes] - groundtruth_image_classes = None - if fields.InputDataFields.groundtruth_image_classes in labels: - groundtruth_image_classes = labels[ - fields.InputDataFields.groundtruth_image_classes] - model.provide_groundtruth( - groundtruth_boxes_list=gt_boxes_list, - groundtruth_classes_list=gt_classes_list, - groundtruth_confidences_list=gt_confidences_list, - groundtruth_labeled_classes=gt_labeled_classes, - groundtruth_masks_list=gt_masks_list, - groundtruth_mask_weights_list=gt_mask_weights_list, - groundtruth_keypoints_list=gt_keypoints_list, - groundtruth_keypoint_visibilities_list=gt_keypoint_visibilities_list, - groundtruth_dp_num_points_list=gt_dp_num_points_list, - groundtruth_dp_part_ids_list=gt_dp_part_ids_list, - groundtruth_dp_surface_coords_list=gt_dp_surface_coords_list, - groundtruth_weights_list=gt_weights_list, - groundtruth_is_crowd_list=gt_is_crowd_list, - groundtruth_group_of_list=gt_group_of_list, - groundtruth_area_list=gt_area_list, - groundtruth_track_ids_list=gt_track_ids_list, - groundtruth_verified_neg_classes=gt_verified_neg_classes, - groundtruth_not_exhaustive_classes=gt_not_exhaustive_classes, - groundtruth_keypoint_depths_list=gt_keypoint_depths_list, - groundtruth_keypoint_depth_weights_list=gt_keypoint_depth_weights_list, - groundtruth_image_classes=groundtruth_image_classes, - training_step=training_step) - - -def create_model_fn(detection_model_fn, - configs, - hparams=None, - use_tpu=False, - postprocess_on_cpu=False): - """Creates a model function for `Estimator`. - - Args: - detection_model_fn: Function that returns a `DetectionModel` instance. - configs: Dictionary of pipeline config objects. - hparams: `HParams` object. - use_tpu: Boolean indicating whether model should be constructed for use on - TPU. - postprocess_on_cpu: When use_tpu and postprocess_on_cpu is true, postprocess - is scheduled on the host cpu. - - Returns: - `model_fn` for `Estimator`. - """ - train_config = configs['train_config'] - eval_input_config = configs['eval_input_config'] - eval_config = configs['eval_config'] - - def model_fn(features, labels, mode, params=None): - """Constructs the object detection model. - - Args: - features: Dictionary of feature tensors, returned from `input_fn`. - labels: Dictionary of groundtruth tensors if mode is TRAIN or EVAL, - otherwise None. - mode: Mode key from tf.estimator.ModeKeys. - params: Parameter dictionary passed from the estimator. - - Returns: - An `EstimatorSpec` that encapsulates the model and its serving - configurations. - """ - params = params or {} - total_loss, train_op, detections, export_outputs = None, None, None, None - is_training = mode == tf_estimator.ModeKeys.TRAIN - - # Make sure to set the Keras learning phase. True during training, - # False for inference. - tf.keras.backend.set_learning_phase(is_training) - # Set policy for mixed-precision training with Keras-based models. - if use_tpu and train_config.use_bfloat16: - # Enable v2 behavior, as `mixed_bfloat16` is only supported in TF 2.0. - tf.keras.layers.enable_v2_dtype_behavior() - tf2.keras.mixed_precision.set_global_policy('mixed_bfloat16') - detection_model = detection_model_fn( - is_training=is_training, add_summaries=(not use_tpu)) - scaffold_fn = None - - if mode == tf_estimator.ModeKeys.TRAIN: - labels = unstack_batch( - labels, - unpad_groundtruth_tensors=train_config.unpad_groundtruth_tensors) - elif mode == tf_estimator.ModeKeys.EVAL: - # For evaling on train data, it is necessary to check whether groundtruth - # must be unpadded. - boxes_shape = ( - labels[ - fields.InputDataFields.groundtruth_boxes].get_shape().as_list()) - unpad_groundtruth_tensors = boxes_shape[1] is not None and not use_tpu - labels = unstack_batch( - labels, unpad_groundtruth_tensors=unpad_groundtruth_tensors) - - if mode in (tf_estimator.ModeKeys.TRAIN, tf_estimator.ModeKeys.EVAL): - provide_groundtruth(detection_model, labels) - - preprocessed_images = features[fields.InputDataFields.image] - - side_inputs = detection_model.get_side_inputs(features) - - if use_tpu and train_config.use_bfloat16: - with tf.tpu.bfloat16_scope(): - prediction_dict = detection_model.predict( - preprocessed_images, - features[fields.InputDataFields.true_image_shape], **side_inputs) - prediction_dict = ops.bfloat16_to_float32_nested(prediction_dict) - else: - prediction_dict = detection_model.predict( - preprocessed_images, - features[fields.InputDataFields.true_image_shape], **side_inputs) - - def postprocess_wrapper(args): - return detection_model.postprocess(args[0], args[1]) - - if mode in (tf_estimator.ModeKeys.EVAL, tf_estimator.ModeKeys.PREDICT): - if use_tpu and postprocess_on_cpu: - detections = tf.tpu.outside_compilation( - postprocess_wrapper, - (prediction_dict, - features[fields.InputDataFields.true_image_shape])) - else: - detections = postprocess_wrapper( - (prediction_dict, - features[fields.InputDataFields.true_image_shape])) - - if mode == tf_estimator.ModeKeys.TRAIN: - load_pretrained = hparams.load_pretrained if hparams else False - if train_config.fine_tune_checkpoint and load_pretrained: - if not train_config.fine_tune_checkpoint_type: - # train_config.from_detection_checkpoint field is deprecated. For - # backward compatibility, set train_config.fine_tune_checkpoint_type - # based on train_config.from_detection_checkpoint. - if train_config.from_detection_checkpoint: - train_config.fine_tune_checkpoint_type = 'detection' - else: - train_config.fine_tune_checkpoint_type = 'classification' - asg_map = detection_model.restore_map( - fine_tune_checkpoint_type=train_config.fine_tune_checkpoint_type, - load_all_detection_checkpoint_vars=( - train_config.load_all_detection_checkpoint_vars)) - available_var_map = ( - variables_helper.get_variables_available_in_checkpoint( - asg_map, - train_config.fine_tune_checkpoint, - include_global_step=False)) - if use_tpu: - - def tpu_scaffold(): - tf.train.init_from_checkpoint(train_config.fine_tune_checkpoint, - available_var_map) - return tf.train.Scaffold() - - scaffold_fn = tpu_scaffold - else: - tf.train.init_from_checkpoint(train_config.fine_tune_checkpoint, - available_var_map) - - if mode in (tf_estimator.ModeKeys.TRAIN, tf_estimator.ModeKeys.EVAL): - if (mode == tf_estimator.ModeKeys.EVAL and - eval_config.use_dummy_loss_in_eval): - total_loss = tf.constant(1.0) - losses_dict = {'Loss/total_loss': total_loss} - else: - losses_dict = detection_model.loss( - prediction_dict, features[fields.InputDataFields.true_image_shape]) - losses = [loss_tensor for loss_tensor in losses_dict.values()] - if train_config.add_regularization_loss: - regularization_losses = detection_model.regularization_losses() - if use_tpu and train_config.use_bfloat16: - regularization_losses = ops.bfloat16_to_float32_nested( - regularization_losses) - if regularization_losses: - regularization_loss = tf.add_n( - regularization_losses, name='regularization_loss') - losses.append(regularization_loss) - losses_dict['Loss/regularization_loss'] = regularization_loss - total_loss = tf.add_n(losses, name='total_loss') - losses_dict['Loss/total_loss'] = total_loss - - if 'graph_rewriter_config' in configs: - graph_rewriter_fn = graph_rewriter_builder.build( - configs['graph_rewriter_config'], is_training=is_training) - graph_rewriter_fn() - - # TODO(rathodv): Stop creating optimizer summary vars in EVAL mode once we - # can write learning rate summaries on TPU without host calls. - global_step = tf.train.get_or_create_global_step() - training_optimizer, optimizer_summary_vars = optimizer_builder.build( - train_config.optimizer) - - if mode == tf_estimator.ModeKeys.TRAIN: - if use_tpu: - training_optimizer = tf.tpu.CrossShardOptimizer(training_optimizer) - - # Optionally freeze some layers by setting their gradients to be zero. - trainable_variables = None - include_variables = ( - train_config.update_trainable_variables - if train_config.update_trainable_variables else None) - exclude_variables = ( - train_config.freeze_variables - if train_config.freeze_variables else None) - trainable_variables = slim.filter_variables( - tf.trainable_variables(), - include_patterns=include_variables, - exclude_patterns=exclude_variables) - - clip_gradients_value = None - if train_config.gradient_clipping_by_norm > 0: - clip_gradients_value = train_config.gradient_clipping_by_norm - - if not use_tpu: - for var in optimizer_summary_vars: - tf.summary.scalar(var.op.name, var) - summaries = [] if use_tpu else None - if train_config.summarize_gradients: - summaries = ['gradients', 'gradient_norm', 'global_gradient_norm'] - train_op = slim.optimizers.optimize_loss( - loss=total_loss, - global_step=global_step, - learning_rate=None, - clip_gradients=clip_gradients_value, - optimizer=training_optimizer, - update_ops=detection_model.updates(), - variables=trainable_variables, - summaries=summaries, - name='') # Preventing scope prefix on all variables. - - if mode == tf_estimator.ModeKeys.PREDICT: - exported_output = exporter_lib.add_output_tensor_nodes(detections) - export_outputs = { - tf.saved_model.signature_constants.PREDICT_METHOD_NAME: - tf_estimator.export.PredictOutput(exported_output) - } - - eval_metric_ops = None - scaffold = None - if mode == tf_estimator.ModeKeys.EVAL: - class_agnostic = ( - fields.DetectionResultFields.detection_classes not in detections) - groundtruth = _prepare_groundtruth_for_eval( - detection_model, class_agnostic, - eval_input_config.max_number_of_boxes) - use_original_images = fields.InputDataFields.original_image in features - if use_original_images: - eval_images = features[fields.InputDataFields.original_image] - true_image_shapes = tf.slice( - features[fields.InputDataFields.true_image_shape], [0, 0], [-1, 3]) - original_image_spatial_shapes = features[ - fields.InputDataFields.original_image_spatial_shape] - else: - eval_images = features[fields.InputDataFields.image] - true_image_shapes = None - original_image_spatial_shapes = None - - eval_dict = eval_util.result_dict_for_batched_example( - eval_images, - features[inputs.HASH_KEY], - detections, - groundtruth, - class_agnostic=class_agnostic, - scale_to_absolute=True, - original_image_spatial_shapes=original_image_spatial_shapes, - true_image_shapes=true_image_shapes) - - if fields.InputDataFields.image_additional_channels in features: - eval_dict[fields.InputDataFields.image_additional_channels] = features[ - fields.InputDataFields.image_additional_channels] - - if class_agnostic: - category_index = label_map_util.create_class_agnostic_category_index() - else: - category_index = label_map_util.create_category_index_from_labelmap( - eval_input_config.label_map_path) - vis_metric_ops = None - if not use_tpu and use_original_images: - keypoint_edges = [(kp.start, kp.end) for kp in eval_config.keypoint_edge - ] - - eval_metric_op_vis = vis_utils.VisualizeSingleFrameDetections( - category_index, - max_examples_to_draw=eval_config.num_visualizations, - max_boxes_to_draw=eval_config.max_num_boxes_to_visualize, - min_score_thresh=eval_config.min_score_threshold, - use_normalized_coordinates=False, - keypoint_edges=keypoint_edges or None) - vis_metric_ops = eval_metric_op_vis.get_estimator_eval_metric_ops( - eval_dict) - - # Eval metrics on a single example. - eval_metric_ops = eval_util.get_eval_metric_ops_for_evaluators( - eval_config, list(category_index.values()), eval_dict) - for loss_key, loss_tensor in iter(losses_dict.items()): - eval_metric_ops[loss_key] = tf.metrics.mean(loss_tensor) - for var in optimizer_summary_vars: - eval_metric_ops[var.op.name] = (var, tf.no_op()) - if vis_metric_ops is not None: - eval_metric_ops.update(vis_metric_ops) - eval_metric_ops = {str(k): v for k, v in eval_metric_ops.items()} - - if eval_config.use_moving_averages: - variable_averages = tf.train.ExponentialMovingAverage(0.0) - variables_to_restore = variable_averages.variables_to_restore() - keep_checkpoint_every_n_hours = ( - train_config.keep_checkpoint_every_n_hours) - saver = tf.train.Saver( - variables_to_restore, - keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours) - scaffold = tf.train.Scaffold(saver=saver) - - # EVAL executes on CPU, so use regular non-TPU EstimatorSpec. - if use_tpu and mode != tf_estimator.ModeKeys.EVAL: - return tf_estimator.tpu.TPUEstimatorSpec( - mode=mode, - scaffold_fn=scaffold_fn, - predictions=detections, - loss=total_loss, - train_op=train_op, - eval_metrics=eval_metric_ops, - export_outputs=export_outputs) - else: - if scaffold is None: - keep_checkpoint_every_n_hours = ( - train_config.keep_checkpoint_every_n_hours) - saver = tf.train.Saver( - sharded=True, - keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours, - save_relative_paths=True) - tf.add_to_collection(tf.GraphKeys.SAVERS, saver) - scaffold = tf.train.Scaffold(saver=saver) - return tf_estimator.EstimatorSpec( - mode=mode, - predictions=detections, - loss=total_loss, - train_op=train_op, - eval_metric_ops=eval_metric_ops, - export_outputs=export_outputs, - scaffold=scaffold) - - return model_fn - - -def create_estimator_and_inputs(run_config, - hparams=None, - pipeline_config_path=None, - config_override=None, - train_steps=None, - sample_1_of_n_eval_examples=1, - sample_1_of_n_eval_on_train_examples=1, - model_fn_creator=create_model_fn, - use_tpu_estimator=False, - use_tpu=False, - num_shards=1, - params=None, - override_eval_num_epochs=True, - save_final_config=False, - postprocess_on_cpu=False, - export_to_tpu=None, - **kwargs): - """Creates `Estimator`, input functions, and steps. - - Args: - run_config: A `RunConfig`. - hparams: (optional) A `HParams`. - pipeline_config_path: A path to a pipeline config file. - config_override: A pipeline_pb2.TrainEvalPipelineConfig text proto to - override the config from `pipeline_config_path`. - train_steps: Number of training steps. If None, the number of training steps - is set from the `TrainConfig` proto. - sample_1_of_n_eval_examples: Integer representing how often an eval example - should be sampled. If 1, will sample all examples. - sample_1_of_n_eval_on_train_examples: Similar to - `sample_1_of_n_eval_examples`, except controls the sampling of training - data for evaluation. - model_fn_creator: A function that creates a `model_fn` for `Estimator`. - Follows the signature: - * Args: - * `detection_model_fn`: Function that returns `DetectionModel` instance. - * `configs`: Dictionary of pipeline config objects. - * `hparams`: `HParams` object. - * Returns: `model_fn` for `Estimator`. - use_tpu_estimator: Whether a `TPUEstimator` should be returned. If False, an - `Estimator` will be returned. - use_tpu: Boolean, whether training and evaluation should run on TPU. Only - used if `use_tpu_estimator` is True. - num_shards: Number of shards (TPU cores). Only used if `use_tpu_estimator` - is True. - params: Parameter dictionary passed from the estimator. Only used if - `use_tpu_estimator` is True. - override_eval_num_epochs: Whether to overwrite the number of epochs to 1 for - eval_input. - save_final_config: Whether to save final config (obtained after applying - overrides) to `estimator.model_dir`. - postprocess_on_cpu: When use_tpu and postprocess_on_cpu are true, - postprocess is scheduled on the host cpu. - export_to_tpu: When use_tpu and export_to_tpu are true, - `export_savedmodel()` exports a metagraph for serving on TPU besides the - one on CPU. - **kwargs: Additional keyword arguments for configuration override. - - Returns: - A dictionary with the following fields: - 'estimator': An `Estimator` or `TPUEstimator`. - 'train_input_fn': A training input function. - 'eval_input_fns': A list of all evaluation input functions. - 'eval_input_names': A list of names for each evaluation input. - 'eval_on_train_input_fn': An evaluation-on-train input function. - 'predict_input_fn': A prediction input function. - 'train_steps': Number of training steps. Either directly from input or from - configuration. - """ - get_configs_from_pipeline_file = MODEL_BUILD_UTIL_MAP[ - 'get_configs_from_pipeline_file'] - merge_external_params_with_configs = MODEL_BUILD_UTIL_MAP[ - 'merge_external_params_with_configs'] - create_pipeline_proto_from_configs = MODEL_BUILD_UTIL_MAP[ - 'create_pipeline_proto_from_configs'] - create_train_input_fn = MODEL_BUILD_UTIL_MAP['create_train_input_fn'] - create_eval_input_fn = MODEL_BUILD_UTIL_MAP['create_eval_input_fn'] - create_predict_input_fn = MODEL_BUILD_UTIL_MAP['create_predict_input_fn'] - detection_model_fn_base = MODEL_BUILD_UTIL_MAP['detection_model_fn_base'] - - configs = get_configs_from_pipeline_file( - pipeline_config_path, config_override=config_override) - kwargs.update({ - 'train_steps': train_steps, - 'use_bfloat16': configs['train_config'].use_bfloat16 and use_tpu - }) - if sample_1_of_n_eval_examples >= 1: - kwargs.update({'sample_1_of_n_eval_examples': sample_1_of_n_eval_examples}) - if override_eval_num_epochs: - kwargs.update({'eval_num_epochs': 1}) - tf.logging.warning( - 'Forced number of epochs for all eval validations to be 1.') - configs = merge_external_params_with_configs( - configs, hparams, kwargs_dict=kwargs) - model_config = configs['model'] - train_config = configs['train_config'] - train_input_config = configs['train_input_config'] - eval_config = configs['eval_config'] - eval_input_configs = configs['eval_input_configs'] - eval_on_train_input_config = copy.deepcopy(train_input_config) - eval_on_train_input_config.sample_1_of_n_examples = ( - sample_1_of_n_eval_on_train_examples) - if override_eval_num_epochs and eval_on_train_input_config.num_epochs != 1: - tf.logging.warning('Expected number of evaluation epochs is 1, but ' - 'instead encountered `eval_on_train_input_config' - '.num_epochs` = ' - '{}. Overwriting `num_epochs` to 1.'.format( - eval_on_train_input_config.num_epochs)) - eval_on_train_input_config.num_epochs = 1 - - # update train_steps from config but only when non-zero value is provided - if train_steps is None and train_config.num_steps != 0: - train_steps = train_config.num_steps - - detection_model_fn = functools.partial( - detection_model_fn_base, model_config=model_config) - - # Create the input functions for TRAIN/EVAL/PREDICT. - train_input_fn = create_train_input_fn( - train_config=train_config, - train_input_config=train_input_config, - model_config=model_config) - eval_input_fns = [] - for eval_input_config in eval_input_configs: - eval_input_fns.append( - create_eval_input_fn( - eval_config=eval_config, - eval_input_config=eval_input_config, - model_config=model_config)) - - eval_input_names = [ - eval_input_config.name for eval_input_config in eval_input_configs - ] - eval_on_train_input_fn = create_eval_input_fn( - eval_config=eval_config, - eval_input_config=eval_on_train_input_config, - model_config=model_config) - predict_input_fn = create_predict_input_fn( - model_config=model_config, predict_input_config=eval_input_configs[0]) - - # Read export_to_tpu from hparams if not passed. - if export_to_tpu is None and hparams is not None: - export_to_tpu = hparams.get('export_to_tpu', False) - tf.logging.info('create_estimator_and_inputs: use_tpu %s, export_to_tpu %s', - use_tpu, export_to_tpu) - model_fn = model_fn_creator(detection_model_fn, configs, hparams, use_tpu, - postprocess_on_cpu) - if use_tpu_estimator: - estimator = tf_estimator.tpu.TPUEstimator( - model_fn=model_fn, - train_batch_size=train_config.batch_size, - # For each core, only batch size 1 is supported for eval. - eval_batch_size=num_shards * 1 if use_tpu else 1, - use_tpu=use_tpu, - config=run_config, - export_to_tpu=export_to_tpu, - eval_on_tpu=False, # Eval runs on CPU, so disable eval on TPU - params=params if params else {}) - else: - estimator = tf_estimator.Estimator(model_fn=model_fn, config=run_config) - - # Write the as-run pipeline config to disk. - if run_config.is_chief and save_final_config: - pipeline_config_final = create_pipeline_proto_from_configs(configs) - config_util.save_pipeline_config(pipeline_config_final, estimator.model_dir) - - return dict( - estimator=estimator, - train_input_fn=train_input_fn, - eval_input_fns=eval_input_fns, - eval_input_names=eval_input_names, - eval_on_train_input_fn=eval_on_train_input_fn, - predict_input_fn=predict_input_fn, - train_steps=train_steps) - - -def create_train_and_eval_specs(train_input_fn, - eval_input_fns, - eval_on_train_input_fn, - predict_input_fn, - train_steps, - eval_on_train_data=False, - final_exporter_name='Servo', - eval_spec_names=None): - """Creates a `TrainSpec` and `EvalSpec`s. - - Args: - train_input_fn: Function that produces features and labels on train data. - eval_input_fns: A list of functions that produce features and labels on eval - data. - eval_on_train_input_fn: Function that produces features and labels for - evaluation on train data. - predict_input_fn: Function that produces features for inference. - train_steps: Number of training steps. - eval_on_train_data: Whether to evaluate model on training data. Default is - False. - final_exporter_name: String name given to `FinalExporter`. - eval_spec_names: A list of string names for each `EvalSpec`. - - Returns: - Tuple of `TrainSpec` and list of `EvalSpecs`. If `eval_on_train_data` is - True, the last `EvalSpec` in the list will correspond to training data. The - rest EvalSpecs in the list are evaluation datas. - """ - train_spec = tf_estimator.TrainSpec( - input_fn=train_input_fn, max_steps=train_steps) - - if eval_spec_names is None: - eval_spec_names = [str(i) for i in range(len(eval_input_fns))] - - eval_specs = [] - for index, (eval_spec_name, - eval_input_fn) in enumerate(zip(eval_spec_names, eval_input_fns)): - # Uses final_exporter_name as exporter_name for the first eval spec for - # backward compatibility. - if index == 0: - exporter_name = final_exporter_name - else: - exporter_name = '{}_{}'.format(final_exporter_name, eval_spec_name) - exporter = tf_estimator.FinalExporter( - name=exporter_name, serving_input_receiver_fn=predict_input_fn) - eval_specs.append( - tf_estimator.EvalSpec( - name=eval_spec_name, - input_fn=eval_input_fn, - steps=None, - exporters=exporter)) - - if eval_on_train_data: - eval_specs.append( - tf_estimator.EvalSpec( - name='eval_on_train', input_fn=eval_on_train_input_fn, steps=None)) - - return train_spec, eval_specs - - -def _evaluate_checkpoint(estimator, - input_fn, - checkpoint_path, - name, - max_retries=0): - """Evaluates a checkpoint. - - Args: - estimator: Estimator object to use for evaluation. - input_fn: Input function to use for evaluation. - checkpoint_path: Path of the checkpoint to evaluate. - name: Namescope for eval summary. - max_retries: Maximum number of times to retry the evaluation on encountering - a tf.errors.InvalidArgumentError. If negative, will always retry the - evaluation. - - Returns: - Estimator evaluation results. - """ - always_retry = True if max_retries < 0 else False - retries = 0 - while always_retry or retries <= max_retries: - try: - return estimator.evaluate( - input_fn=input_fn, - steps=None, - checkpoint_path=checkpoint_path, - name=name) - except tf.errors.InvalidArgumentError as e: - if always_retry or retries < max_retries: - tf.logging.info('Retrying checkpoint evaluation after exception: %s', e) - retries += 1 - else: - raise e - - -def continuous_eval_generator(estimator, - model_dir, - input_fn, - train_steps, - name, - max_retries=0): - """Perform continuous evaluation on checkpoints written to a model directory. - - Args: - estimator: Estimator object to use for evaluation. - model_dir: Model directory to read checkpoints for continuous evaluation. - input_fn: Input function to use for evaluation. - train_steps: Number of training steps. This is used to infer the last - checkpoint and stop evaluation loop. - name: Namescope for eval summary. - max_retries: Maximum number of times to retry the evaluation on encountering - a tf.errors.InvalidArgumentError. If negative, will always retry the - evaluation. - - Yields: - Pair of current step and eval_results. - """ - - def terminate_eval(): - tf.logging.info('Terminating eval after 180 seconds of no checkpoints') - return True - - for ckpt in tf.train.checkpoints_iterator( - model_dir, min_interval_secs=180, timeout=None, - timeout_fn=terminate_eval): - - tf.logging.info('Starting Evaluation.') - try: - eval_results = _evaluate_checkpoint( - estimator=estimator, - input_fn=input_fn, - checkpoint_path=ckpt, - name=name, - max_retries=max_retries) - tf.logging.info('Eval results: %s' % eval_results) - - # Terminate eval job when final checkpoint is reached - current_step = int(os.path.basename(ckpt).split('-')[1]) - yield (current_step, eval_results) - if current_step >= train_steps: - tf.logging.info( - 'Evaluation finished after training step %d' % current_step) - break - - except tf.errors.NotFoundError: - tf.logging.info( - 'Checkpoint %s no longer exists, skipping checkpoint' % ckpt) - - -def continuous_eval(estimator, - model_dir, - input_fn, - train_steps, - name, - max_retries=0): - """Performs continuous evaluation on checkpoints written to a model directory. - - Args: - estimator: Estimator object to use for evaluation. - model_dir: Model directory to read checkpoints for continuous evaluation. - input_fn: Input function to use for evaluation. - train_steps: Number of training steps. This is used to infer the last - checkpoint and stop evaluation loop. - name: Namescope for eval summary. - max_retries: Maximum number of times to retry the evaluation on encountering - a tf.errors.InvalidArgumentError. If negative, will always retry the - evaluation. - """ - for current_step, eval_results in continuous_eval_generator( - estimator, model_dir, input_fn, train_steps, name, max_retries): - tf.logging.info('Step %s, Eval results: %s', current_step, eval_results) - - -def populate_experiment(run_config, - hparams, - pipeline_config_path, - train_steps=None, - eval_steps=None, - model_fn_creator=create_model_fn, - **kwargs): - """Populates an `Experiment` object. - - EXPERIMENT CLASS IS DEPRECATED. Please switch to - tf.estimator.train_and_evaluate. As an example, see model_main.py. - - Args: - run_config: A `RunConfig`. - hparams: A `HParams`. - pipeline_config_path: A path to a pipeline config file. - train_steps: Number of training steps. If None, the number of training steps - is set from the `TrainConfig` proto. - eval_steps: Number of evaluation steps per evaluation cycle. If None, the - number of evaluation steps is set from the `EvalConfig` proto. - model_fn_creator: A function that creates a `model_fn` for `Estimator`. - Follows the signature: - * Args: - * `detection_model_fn`: Function that returns `DetectionModel` instance. - * `configs`: Dictionary of pipeline config objects. - * `hparams`: `HParams` object. - * Returns: `model_fn` for `Estimator`. - **kwargs: Additional keyword arguments for configuration override. - - Returns: - An `Experiment` that defines all aspects of training, evaluation, and - export. - """ - tf.logging.warning('Experiment is being deprecated. Please use ' - 'tf.estimator.train_and_evaluate(). See model_main.py for ' - 'an example.') - train_and_eval_dict = create_estimator_and_inputs( - run_config, - hparams, - pipeline_config_path, - train_steps=train_steps, - eval_steps=eval_steps, - model_fn_creator=model_fn_creator, - save_final_config=True, - **kwargs) - estimator = train_and_eval_dict['estimator'] - train_input_fn = train_and_eval_dict['train_input_fn'] - eval_input_fns = train_and_eval_dict['eval_input_fns'] - predict_input_fn = train_and_eval_dict['predict_input_fn'] - train_steps = train_and_eval_dict['train_steps'] - - export_strategies = [ - contrib_learn.utils.saved_model_export_utils.make_export_strategy( - serving_input_fn=predict_input_fn) - ] - - return contrib_learn.Experiment( - estimator=estimator, - train_input_fn=train_input_fn, - eval_input_fn=eval_input_fns[0], - train_steps=train_steps, - eval_steps=None, - export_strategies=export_strategies, - eval_delay_secs=120, - ) diff --git a/research/object_detection/model_lib_tf1_test.py b/research/object_detection/model_lib_tf1_test.py deleted file mode 100644 index fa8d5ac5e21..00000000000 --- a/research/object_detection/model_lib_tf1_test.py +++ /dev/null @@ -1,506 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object detection model library.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import functools -import os -import unittest -import numpy as np -import tensorflow.compat.v1 as tf -from tensorflow.compat.v1 import estimator as tf_estimator - -from object_detection import inputs -from object_detection import model_hparams -from object_detection import model_lib -from object_detection.builders import model_builder -from object_detection.core import standard_fields as fields -from object_detection.utils import config_util -from object_detection.utils import tf_version - - -# Model for test. Options are: -# 'ssd_inception_v2_pets', 'faster_rcnn_resnet50_pets' -MODEL_NAME_FOR_TEST = 'ssd_inception_v2_pets' - -# Model for testing keypoints. -MODEL_NAME_FOR_KEYPOINTS_TEST = 'ssd_mobilenet_v1_fpp' - -# Model for testing tfSequenceExample inputs. -MODEL_NAME_FOR_SEQUENCE_EXAMPLE_TEST = 'context_rcnn_camera_trap' - - -def _get_data_path(model_name): - """Returns an absolute path to TFRecord file.""" - if model_name == MODEL_NAME_FOR_SEQUENCE_EXAMPLE_TEST: - return os.path.join(tf.resource_loader.get_data_files_path(), 'test_data', - 'snapshot_serengeti_sequence_examples.record') - else: - return os.path.join(tf.resource_loader.get_data_files_path(), 'test_data', - 'pets_examples.record') - - -def get_pipeline_config_path(model_name): - """Returns path to the local pipeline config file.""" - if model_name == MODEL_NAME_FOR_KEYPOINTS_TEST: - return os.path.join(tf.resource_loader.get_data_files_path(), 'test_data', - model_name + '.config') - elif model_name == MODEL_NAME_FOR_SEQUENCE_EXAMPLE_TEST: - return os.path.join(tf.resource_loader.get_data_files_path(), 'test_data', - model_name + '.config') - else: - return os.path.join(tf.resource_loader.get_data_files_path(), 'samples', - 'configs', model_name + '.config') - - -def _get_labelmap_path(): - """Returns an absolute path to label map file.""" - return os.path.join(tf.resource_loader.get_data_files_path(), 'data', - 'pet_label_map.pbtxt') - - -def _get_keypoints_labelmap_path(): - """Returns an absolute path to label map file.""" - return os.path.join(tf.resource_loader.get_data_files_path(), 'data', - 'face_person_with_keypoints_label_map.pbtxt') - - -def _get_sequence_example_labelmap_path(): - """Returns an absolute path to label map file.""" - return os.path.join(tf.resource_loader.get_data_files_path(), 'data', - 'snapshot_serengeti_label_map.pbtxt') - - -def _get_configs_for_model(model_name): - """Returns configurations for model.""" - filename = get_pipeline_config_path(model_name) - data_path = _get_data_path(model_name) - if model_name == MODEL_NAME_FOR_KEYPOINTS_TEST: - label_map_path = _get_keypoints_labelmap_path() - elif model_name == MODEL_NAME_FOR_SEQUENCE_EXAMPLE_TEST: - label_map_path = _get_sequence_example_labelmap_path() - else: - label_map_path = _get_labelmap_path() - configs = config_util.get_configs_from_pipeline_file(filename) - override_dict = { - 'train_input_path': data_path, - 'eval_input_path': data_path, - 'label_map_path': label_map_path - } - configs = config_util.merge_external_params_with_configs( - configs, kwargs_dict=override_dict) - return configs - - -def _make_initializable_iterator(dataset): - """Creates an iterator, and initializes tables. - - Args: - dataset: A `tf.data.Dataset` object. - - Returns: - A `tf.data.Iterator`. - """ - iterator = tf.data.make_initializable_iterator(dataset) - tf.add_to_collection(tf.GraphKeys.TABLE_INITIALIZERS, iterator.initializer) - return iterator - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ModelLibTest(tf.test.TestCase): - - @classmethod - def setUpClass(cls): - tf.reset_default_graph() - - def _assert_model_fn_for_train_eval(self, configs, mode, - class_agnostic=False): - model_config = configs['model'] - train_config = configs['train_config'] - with tf.Graph().as_default(): - if mode == 'train': - features, labels = _make_initializable_iterator( - inputs.create_train_input_fn(configs['train_config'], - configs['train_input_config'], - configs['model'])()).get_next() - model_mode = tf_estimator.ModeKeys.TRAIN - batch_size = train_config.batch_size - elif mode == 'eval': - features, labels = _make_initializable_iterator( - inputs.create_eval_input_fn(configs['eval_config'], - configs['eval_input_config'], - configs['model'])()).get_next() - model_mode = tf_estimator.ModeKeys.EVAL - batch_size = 1 - elif mode == 'eval_on_train': - features, labels = _make_initializable_iterator( - inputs.create_eval_input_fn(configs['eval_config'], - configs['train_input_config'], - configs['model'])()).get_next() - model_mode = tf_estimator.ModeKeys.EVAL - batch_size = 1 - - detection_model_fn = functools.partial( - model_builder.build, model_config=model_config, is_training=True) - - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - - model_fn = model_lib.create_model_fn(detection_model_fn, configs, hparams) - estimator_spec = model_fn(features, labels, model_mode) - - self.assertIsNotNone(estimator_spec.loss) - self.assertIsNotNone(estimator_spec.predictions) - if mode == 'eval' or mode == 'eval_on_train': - if class_agnostic: - self.assertNotIn('detection_classes', estimator_spec.predictions) - else: - detection_classes = estimator_spec.predictions['detection_classes'] - self.assertEqual(batch_size, detection_classes.shape.as_list()[0]) - self.assertEqual(tf.float32, detection_classes.dtype) - detection_boxes = estimator_spec.predictions['detection_boxes'] - detection_scores = estimator_spec.predictions['detection_scores'] - num_detections = estimator_spec.predictions['num_detections'] - self.assertEqual(batch_size, detection_boxes.shape.as_list()[0]) - self.assertEqual(tf.float32, detection_boxes.dtype) - self.assertEqual(batch_size, detection_scores.shape.as_list()[0]) - self.assertEqual(tf.float32, detection_scores.dtype) - self.assertEqual(tf.float32, num_detections.dtype) - if mode == 'eval': - self.assertIn('Detections_Left_Groundtruth_Right/0', - estimator_spec.eval_metric_ops) - if model_mode == tf_estimator.ModeKeys.TRAIN: - self.assertIsNotNone(estimator_spec.train_op) - return estimator_spec - - def _assert_model_fn_for_predict(self, configs): - model_config = configs['model'] - - with tf.Graph().as_default(): - features, _ = _make_initializable_iterator( - inputs.create_eval_input_fn(configs['eval_config'], - configs['eval_input_config'], - configs['model'])()).get_next() - detection_model_fn = functools.partial( - model_builder.build, model_config=model_config, is_training=False) - - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - - model_fn = model_lib.create_model_fn(detection_model_fn, configs, hparams) - estimator_spec = model_fn(features, None, tf_estimator.ModeKeys.PREDICT) - - self.assertIsNone(estimator_spec.loss) - self.assertIsNone(estimator_spec.train_op) - self.assertIsNotNone(estimator_spec.predictions) - self.assertIsNotNone(estimator_spec.export_outputs) - self.assertIn(tf.saved_model.signature_constants.PREDICT_METHOD_NAME, - estimator_spec.export_outputs) - - def test_model_fn_in_train_mode(self): - """Tests the model function in TRAIN mode.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) - self._assert_model_fn_for_train_eval(configs, 'train') - - def test_model_fn_in_train_mode_sequences(self): - """Tests the model function in TRAIN mode.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_SEQUENCE_EXAMPLE_TEST) - self._assert_model_fn_for_train_eval(configs, 'train') - - def test_model_fn_in_train_mode_freeze_all_variables(self): - """Tests model_fn TRAIN mode with all variables frozen.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) - configs['train_config'].freeze_variables.append('.*') - with self.assertRaisesRegexp(ValueError, 'No variables to optimize'): - self._assert_model_fn_for_train_eval(configs, 'train') - - def test_model_fn_in_train_mode_freeze_all_included_variables(self): - """Tests model_fn TRAIN mode with all included variables frozen.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) - train_config = configs['train_config'] - train_config.update_trainable_variables.append('FeatureExtractor') - train_config.freeze_variables.append('.*') - with self.assertRaisesRegexp(ValueError, 'No variables to optimize'): - self._assert_model_fn_for_train_eval(configs, 'train') - - def test_model_fn_in_train_mode_freeze_box_predictor(self): - """Tests model_fn TRAIN mode with FeatureExtractor variables frozen.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) - train_config = configs['train_config'] - train_config.update_trainable_variables.append('FeatureExtractor') - train_config.update_trainable_variables.append('BoxPredictor') - train_config.freeze_variables.append('FeatureExtractor') - self._assert_model_fn_for_train_eval(configs, 'train') - - def test_model_fn_in_eval_mode(self): - """Tests the model function in EVAL mode.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) - self._assert_model_fn_for_train_eval(configs, 'eval') - - def test_model_fn_in_eval_mode_sequences(self): - """Tests the model function in EVAL mode.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_SEQUENCE_EXAMPLE_TEST) - self._assert_model_fn_for_train_eval(configs, 'eval') - - def test_model_fn_in_keypoints_eval_mode(self): - """Tests the model function in EVAL mode with keypoints config.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_KEYPOINTS_TEST) - estimator_spec = self._assert_model_fn_for_train_eval(configs, 'eval') - metric_ops = estimator_spec.eval_metric_ops - self.assertIn('Keypoints_Precision/mAP ByCategory/face', metric_ops) - self.assertIn('Keypoints_Precision/mAP ByCategory/PERSON', metric_ops) - detection_keypoints = estimator_spec.predictions['detection_keypoints'] - self.assertEqual(1, detection_keypoints.shape.as_list()[0]) - self.assertEqual(tf.float32, detection_keypoints.dtype) - - def test_model_fn_in_eval_on_train_mode(self): - """Tests the model function in EVAL mode with train data.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) - self._assert_model_fn_for_train_eval(configs, 'eval_on_train') - - def test_model_fn_in_predict_mode(self): - """Tests the model function in PREDICT mode.""" - configs = _get_configs_for_model(MODEL_NAME_FOR_TEST) - self._assert_model_fn_for_predict(configs) - - def test_create_estimator_and_inputs(self): - """Tests that Estimator and input function are constructed correctly.""" - run_config = tf_estimator.RunConfig() - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - train_steps = 20 - train_and_eval_dict = model_lib.create_estimator_and_inputs( - run_config, - hparams, - pipeline_config_path, - train_steps=train_steps) - estimator = train_and_eval_dict['estimator'] - train_steps = train_and_eval_dict['train_steps'] - self.assertIsInstance(estimator, tf_estimator.Estimator) - self.assertEqual(20, train_steps) - self.assertIn('train_input_fn', train_and_eval_dict) - self.assertIn('eval_input_fns', train_and_eval_dict) - self.assertIn('eval_on_train_input_fn', train_and_eval_dict) - - def test_create_estimator_and_inputs_sequence_example(self): - """Tests that Estimator and input function are constructed correctly.""" - run_config = tf_estimator.RunConfig() - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - pipeline_config_path = get_pipeline_config_path( - MODEL_NAME_FOR_SEQUENCE_EXAMPLE_TEST) - train_steps = 20 - train_and_eval_dict = model_lib.create_estimator_and_inputs( - run_config, - hparams, - pipeline_config_path, - train_steps=train_steps) - estimator = train_and_eval_dict['estimator'] - train_steps = train_and_eval_dict['train_steps'] - self.assertIsInstance(estimator, tf_estimator.Estimator) - self.assertEqual(20, train_steps) - self.assertIn('train_input_fn', train_and_eval_dict) - self.assertIn('eval_input_fns', train_and_eval_dict) - self.assertIn('eval_on_train_input_fn', train_and_eval_dict) - - def test_create_estimator_with_default_train_eval_steps(self): - """Tests that number of train/eval defaults to config values.""" - run_config = tf_estimator.RunConfig() - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - configs = config_util.get_configs_from_pipeline_file(pipeline_config_path) - config_train_steps = configs['train_config'].num_steps - train_and_eval_dict = model_lib.create_estimator_and_inputs( - run_config, hparams, pipeline_config_path) - estimator = train_and_eval_dict['estimator'] - train_steps = train_and_eval_dict['train_steps'] - - self.assertIsInstance(estimator, tf_estimator.Estimator) - self.assertEqual(config_train_steps, train_steps) - - def test_create_tpu_estimator_and_inputs(self): - """Tests that number of train/eval defaults to config values.""" - run_config = tf_estimator.tpu.RunConfig() - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - train_steps = 20 - train_and_eval_dict = model_lib.create_estimator_and_inputs( - run_config, - hparams, - pipeline_config_path, - train_steps=train_steps, - use_tpu_estimator=True) - estimator = train_and_eval_dict['estimator'] - train_steps = train_and_eval_dict['train_steps'] - - self.assertIsInstance(estimator, tf_estimator.tpu.TPUEstimator) - self.assertEqual(20, train_steps) - - def test_create_train_and_eval_specs(self): - """Tests that `TrainSpec` and `EvalSpec` is created correctly.""" - run_config = tf_estimator.RunConfig() - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - train_steps = 20 - train_and_eval_dict = model_lib.create_estimator_and_inputs( - run_config, - hparams, - pipeline_config_path, - train_steps=train_steps) - train_input_fn = train_and_eval_dict['train_input_fn'] - eval_input_fns = train_and_eval_dict['eval_input_fns'] - eval_on_train_input_fn = train_and_eval_dict['eval_on_train_input_fn'] - predict_input_fn = train_and_eval_dict['predict_input_fn'] - train_steps = train_and_eval_dict['train_steps'] - - train_spec, eval_specs = model_lib.create_train_and_eval_specs( - train_input_fn, - eval_input_fns, - eval_on_train_input_fn, - predict_input_fn, - train_steps, - eval_on_train_data=True, - final_exporter_name='exporter', - eval_spec_names=['holdout']) - self.assertEqual(train_steps, train_spec.max_steps) - self.assertEqual(2, len(eval_specs)) - self.assertEqual(None, eval_specs[0].steps) - self.assertEqual('holdout', eval_specs[0].name) - self.assertEqual('exporter', eval_specs[0].exporters[0].name) - self.assertEqual(None, eval_specs[1].steps) - self.assertEqual('eval_on_train', eval_specs[1].name) - - def test_experiment(self): - """Tests that the `Experiment` object is constructed correctly.""" - run_config = tf_estimator.RunConfig() - hparams = model_hparams.create_hparams( - hparams_overrides='load_pretrained=false') - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - experiment = model_lib.populate_experiment( - run_config, - hparams, - pipeline_config_path, - train_steps=10, - eval_steps=20) - self.assertEqual(10, experiment.train_steps) - self.assertEqual(None, experiment.eval_steps) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class UnbatchTensorsTest(tf.test.TestCase): - - def test_unbatch_without_unpadding(self): - image_placeholder = tf.placeholder(tf.float32, [2, None, None, None]) - groundtruth_boxes_placeholder = tf.placeholder(tf.float32, [2, None, None]) - groundtruth_classes_placeholder = tf.placeholder(tf.float32, - [2, None, None]) - groundtruth_weights_placeholder = tf.placeholder(tf.float32, [2, None]) - - tensor_dict = { - fields.InputDataFields.image: - image_placeholder, - fields.InputDataFields.groundtruth_boxes: - groundtruth_boxes_placeholder, - fields.InputDataFields.groundtruth_classes: - groundtruth_classes_placeholder, - fields.InputDataFields.groundtruth_weights: - groundtruth_weights_placeholder - } - unbatched_tensor_dict = model_lib.unstack_batch( - tensor_dict, unpad_groundtruth_tensors=False) - - with self.test_session() as sess: - unbatched_tensor_dict_out = sess.run( - unbatched_tensor_dict, - feed_dict={ - image_placeholder: - np.random.rand(2, 4, 4, 3).astype(np.float32), - groundtruth_boxes_placeholder: - np.random.rand(2, 5, 4).astype(np.float32), - groundtruth_classes_placeholder: - np.random.rand(2, 5, 6).astype(np.float32), - groundtruth_weights_placeholder: - np.random.rand(2, 5).astype(np.float32) - }) - for image_out in unbatched_tensor_dict_out[fields.InputDataFields.image]: - self.assertAllEqual(image_out.shape, [4, 4, 3]) - for groundtruth_boxes_out in unbatched_tensor_dict_out[ - fields.InputDataFields.groundtruth_boxes]: - self.assertAllEqual(groundtruth_boxes_out.shape, [5, 4]) - for groundtruth_classes_out in unbatched_tensor_dict_out[ - fields.InputDataFields.groundtruth_classes]: - self.assertAllEqual(groundtruth_classes_out.shape, [5, 6]) - for groundtruth_weights_out in unbatched_tensor_dict_out[ - fields.InputDataFields.groundtruth_weights]: - self.assertAllEqual(groundtruth_weights_out.shape, [5]) - - def test_unbatch_and_unpad_groundtruth_tensors(self): - image_placeholder = tf.placeholder(tf.float32, [2, None, None, None]) - groundtruth_boxes_placeholder = tf.placeholder(tf.float32, [2, 5, None]) - groundtruth_classes_placeholder = tf.placeholder(tf.float32, [2, 5, None]) - groundtruth_weights_placeholder = tf.placeholder(tf.float32, [2, 5]) - num_groundtruth_placeholder = tf.placeholder(tf.int32, [2]) - - tensor_dict = { - fields.InputDataFields.image: - image_placeholder, - fields.InputDataFields.groundtruth_boxes: - groundtruth_boxes_placeholder, - fields.InputDataFields.groundtruth_classes: - groundtruth_classes_placeholder, - fields.InputDataFields.groundtruth_weights: - groundtruth_weights_placeholder, - fields.InputDataFields.num_groundtruth_boxes: - num_groundtruth_placeholder - } - unbatched_tensor_dict = model_lib.unstack_batch( - tensor_dict, unpad_groundtruth_tensors=True) - with self.test_session() as sess: - unbatched_tensor_dict_out = sess.run( - unbatched_tensor_dict, - feed_dict={ - image_placeholder: - np.random.rand(2, 4, 4, 3).astype(np.float32), - groundtruth_boxes_placeholder: - np.random.rand(2, 5, 4).astype(np.float32), - groundtruth_classes_placeholder: - np.random.rand(2, 5, 6).astype(np.float32), - groundtruth_weights_placeholder: - np.random.rand(2, 5).astype(np.float32), - num_groundtruth_placeholder: - np.array([3, 3], np.int32) - }) - for image_out in unbatched_tensor_dict_out[fields.InputDataFields.image]: - self.assertAllEqual(image_out.shape, [4, 4, 3]) - for groundtruth_boxes_out in unbatched_tensor_dict_out[ - fields.InputDataFields.groundtruth_boxes]: - self.assertAllEqual(groundtruth_boxes_out.shape, [3, 4]) - for groundtruth_classes_out in unbatched_tensor_dict_out[ - fields.InputDataFields.groundtruth_classes]: - self.assertAllEqual(groundtruth_classes_out.shape, [3, 6]) - for groundtruth_weights_out in unbatched_tensor_dict_out[ - fields.InputDataFields.groundtruth_weights]: - self.assertAllEqual(groundtruth_weights_out.shape, [3]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/model_lib_tf2_test.py b/research/object_detection/model_lib_tf2_test.py deleted file mode 100644 index 6cbab6a15de..00000000000 --- a/research/object_detection/model_lib_tf2_test.py +++ /dev/null @@ -1,284 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for object detection model library.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import json -import os -import tempfile -import unittest -import numpy as np -import six -import tensorflow.compat.v1 as tf -import tensorflow.compat.v2 as tf2 - -from object_detection import exporter_lib_v2 -from object_detection import inputs -from object_detection import model_lib_v2 -from object_detection.core import model -from object_detection.protos import train_pb2 -from object_detection.utils import config_util -from object_detection.utils import tf_version - -if six.PY2: - import mock # pylint: disable=g-importing-member,g-import-not-at-top -else: - from unittest import mock # pylint: disable=g-importing-member,g-import-not-at-top - -# Model for test. Current options are: -# 'ssd_mobilenet_v2_pets_keras' -MODEL_NAME_FOR_TEST = 'ssd_mobilenet_v2_pets_keras' - - -def _get_data_path(): - """Returns an absolute path to TFRecord file.""" - return os.path.join(tf.resource_loader.get_data_files_path(), 'test_data', - 'pets_examples.record') - - -def get_pipeline_config_path(model_name): - """Returns path to the local pipeline config file.""" - return os.path.join(tf.resource_loader.get_data_files_path(), 'samples', - 'configs', model_name + '.config') - - -def _get_labelmap_path(): - """Returns an absolute path to label map file.""" - return os.path.join(tf.resource_loader.get_data_files_path(), 'data', - 'pet_label_map.pbtxt') - - -def _get_config_kwarg_overrides(): - """Returns overrides to the configs that insert the correct local paths.""" - data_path = _get_data_path() - label_map_path = _get_labelmap_path() - return { - 'train_input_path': data_path, - 'eval_input_path': data_path, - 'label_map_path': label_map_path, - 'train_input_reader': {'batch_size': 1} - } - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ModelLibTest(tf.test.TestCase): - - @classmethod - def setUpClass(cls): # pylint:disable=g-missing-super-call - tf.keras.backend.clear_session() - - def test_train_loop_then_eval_loop(self): - """Tests that Estimator and input function are constructed correctly.""" - model_dir = tf.test.get_temp_dir() - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - new_pipeline_config_path = os.path.join(model_dir, 'new_pipeline.config') - config_util.clear_fine_tune_checkpoint(pipeline_config_path, - new_pipeline_config_path) - config_kwarg_overrides = _get_config_kwarg_overrides() - - train_steps = 2 - strategy = tf2.distribute.MirroredStrategy(['/cpu:0', '/cpu:1']) - with strategy.scope(): - model_lib_v2.train_loop( - new_pipeline_config_path, - model_dir=model_dir, - train_steps=train_steps, - checkpoint_every_n=1, - num_steps_per_iteration=1, - **config_kwarg_overrides) - - model_lib_v2.eval_continuously( - new_pipeline_config_path, - model_dir=model_dir, - checkpoint_dir=model_dir, - train_steps=train_steps, - wait_interval=1, - timeout=10, - **config_kwarg_overrides) - - -class SimpleModel(model.DetectionModel): - """A model with a single weight vector.""" - - def __init__(self, num_classes=1): - super(SimpleModel, self).__init__(num_classes) - self.weight = tf.keras.backend.variable(np.ones(10), name='weight') - - def postprocess(self, prediction_dict, true_image_shapes): - return {} - - def updates(self): - return [] - - def restore_map(self, *args, **kwargs): - pass - - def restore_from_objects(self, fine_tune_checkpoint_type): - return {'model': self} - - def preprocess(self, _): - return tf.zeros((1, 128, 128, 3)), tf.constant([[128, 128, 3]]) - - def provide_groundtruth(self, *args, **kwargs): - pass - - def predict(self, pred_inputs, true_image_shapes): - return {'prediction': - tf.abs(tf.reduce_sum(self.weight) * tf.reduce_sum(pred_inputs))} - - def loss(self, prediction_dict, _): - return {'loss': tf.reduce_sum(prediction_dict['prediction'])} - - def regularization_losses(self): - return [] - - -def fake_model_builder(*_, **__): - return SimpleModel() - -FAKE_BUILDER_MAP = {'detection_model_fn_base': fake_model_builder} - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ModelCheckpointTest(tf.test.TestCase): - """Test for model checkpoint related functionality.""" - - def test_checkpoint_max_to_keep(self): - """Test that only the most recent checkpoints are kept.""" - - strategy = tf2.distribute.OneDeviceStrategy(device='/cpu:0') - with mock.patch.dict( - model_lib_v2.MODEL_BUILD_UTIL_MAP, FAKE_BUILDER_MAP): - - model_dir = tempfile.mkdtemp(dir=self.get_temp_dir()) - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - new_pipeline_config_path = os.path.join(model_dir, 'new_pipeline.config') - config_util.clear_fine_tune_checkpoint(pipeline_config_path, - new_pipeline_config_path) - config_kwarg_overrides = _get_config_kwarg_overrides() - - with strategy.scope(): - model_lib_v2.train_loop( - new_pipeline_config_path, model_dir=model_dir, - train_steps=5, checkpoint_every_n=2, checkpoint_max_to_keep=3, - num_steps_per_iteration=1, **config_kwarg_overrides - ) - ckpt_files = tf.io.gfile.glob(os.path.join(model_dir, 'ckpt-*.index')) - self.assertEqual(len(ckpt_files), 3, - '{} not of length 3.'.format(ckpt_files)) - - -class IncompatibleModel(SimpleModel): - - def restore_from_objects(self, *args, **kwargs): - return {'weight': self.weight} - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CheckpointV2Test(tf.test.TestCase): - - def setUp(self): - super(CheckpointV2Test, self).setUp() - - self._model = SimpleModel() - tf.keras.backend.set_value(self._model.weight, np.ones(10) * 42) - ckpt = tf.train.Checkpoint(model=self._model) - - self._test_dir = tf.test.get_temp_dir() - self._ckpt_path = ckpt.save(os.path.join(self._test_dir, 'ckpt')) - tf.keras.backend.set_value(self._model.weight, np.ones(10)) - - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - configs = config_util.get_configs_from_pipeline_file(pipeline_config_path) - configs = config_util.merge_external_params_with_configs( - configs, kwargs_dict=_get_config_kwarg_overrides()) - self._train_input_fn = inputs.create_train_input_fn( - configs['train_config'], - configs['train_input_config'], - configs['model']) - - def test_restore_v2(self): - """Test that restoring a v2 style checkpoint works.""" - - model_lib_v2.load_fine_tune_checkpoint( - self._model, self._ckpt_path, checkpoint_type='', - checkpoint_version=train_pb2.CheckpointVersion.V2, - run_model_on_dummy_input=True, - input_dataset=self._train_input_fn(), - unpad_groundtruth_tensors=True) - np.testing.assert_allclose(self._model.weight.numpy(), 42) - - def test_restore_map_incompatible_error(self): - """Test that restoring an incompatible restore map causes an error.""" - - with self.assertRaisesRegex(TypeError, - r'.*received a \(str -> ResourceVariable\).*'): - model_lib_v2.load_fine_tune_checkpoint( - IncompatibleModel(), self._ckpt_path, checkpoint_type='', - checkpoint_version=train_pb2.CheckpointVersion.V2, - run_model_on_dummy_input=True, - input_dataset=self._train_input_fn(), - unpad_groundtruth_tensors=True) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class MetricsExportTest(tf.test.TestCase): - - @classmethod - def setUpClass(cls): # pylint:disable=g-missing-super-call - tf.keras.backend.clear_session() - - def test_export_metrics_json_serializable(self): - """Tests that Estimator and input function are constructed correctly.""" - - strategy = tf2.distribute.OneDeviceStrategy(device='/cpu:0') - - def export(data, _): - json.dumps(data) - - with mock.patch.dict( - exporter_lib_v2.INPUT_BUILDER_UTIL_MAP, FAKE_BUILDER_MAP): - with strategy.scope(): - model_dir = tf.test.get_temp_dir() - new_pipeline_config_path = os.path.join(model_dir, - 'new_pipeline.config') - pipeline_config_path = get_pipeline_config_path(MODEL_NAME_FOR_TEST) - config_util.clear_fine_tune_checkpoint(pipeline_config_path, - new_pipeline_config_path) - train_steps = 2 - with strategy.scope(): - model_lib_v2.train_loop( - new_pipeline_config_path, - model_dir=model_dir, - train_steps=train_steps, - checkpoint_every_n=100, - performance_summary_exporter=export, - num_steps_per_iteration=1, - **_get_config_kwarg_overrides()) - - -def setUpModule(): - # Setup virtual CPUs. - cpus = tf.config.list_physical_devices('CPU') - tf.config.set_logical_device_configuration( - cpus[-1], [tf.config.LogicalDeviceConfiguration()] * 2 - ) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/model_lib_v2.py b/research/object_detection/model_lib_v2.py deleted file mode 100644 index 6279deea703..00000000000 --- a/research/object_detection/model_lib_v2.py +++ /dev/null @@ -1,1169 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Constructs model, inputs, and training environment.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import copy -import os -import pprint -import time - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection import eval_util -from object_detection import inputs -from object_detection import model_lib -from object_detection.builders import optimizer_builder -from object_detection.core import standard_fields as fields -from object_detection.protos import train_pb2 -from object_detection.utils import config_util -from object_detection.utils import label_map_util -from object_detection.utils import ops -from object_detection.utils import variables_helper -from object_detection.utils import visualization_utils as vutils - - -MODEL_BUILD_UTIL_MAP = model_lib.MODEL_BUILD_UTIL_MAP -NUM_STEPS_PER_ITERATION = 100 -LOG_EVERY = 100 - - -RESTORE_MAP_ERROR_TEMPLATE = ( - 'Since we are restoring a v2 style checkpoint' - ' restore_map was expected to return a (str -> Model) mapping,' - ' but we received a ({} -> {}) mapping instead.' -) - - -def _compute_losses_and_predictions_dicts( - model, features, labels, training_step=None, - add_regularization_loss=True): - """Computes the losses dict and predictions dict for a model on inputs. - - Args: - model: a DetectionModel (based on Keras). - features: Dictionary of feature tensors from the input dataset. - Should be in the format output by `inputs.train_input` and - `inputs.eval_input`. - features[fields.InputDataFields.image] is a [batch_size, H, W, C] - float32 tensor with preprocessed images. - features[HASH_KEY] is a [batch_size] int32 tensor representing unique - identifiers for the images. - features[fields.InputDataFields.true_image_shape] is a [batch_size, 3] - int32 tensor representing the true image shapes, as preprocessed - images could be padded. - features[fields.InputDataFields.original_image] (optional) is a - [batch_size, H, W, C] float32 tensor with original images. - labels: A dictionary of groundtruth tensors post-unstacking. The original - labels are of the form returned by `inputs.train_input` and - `inputs.eval_input`. The shapes may have been modified by unstacking with - `model_lib.unstack_batch`. However, the dictionary includes the following - fields. - labels[fields.InputDataFields.num_groundtruth_boxes] is a - int32 tensor indicating the number of valid groundtruth boxes - per image. - labels[fields.InputDataFields.groundtruth_boxes] is a float32 tensor - containing the corners of the groundtruth boxes. - labels[fields.InputDataFields.groundtruth_classes] is a float32 - one-hot tensor of classes. - labels[fields.InputDataFields.groundtruth_weights] is a float32 tensor - containing groundtruth weights for the boxes. - -- Optional -- - labels[fields.InputDataFields.groundtruth_instance_masks] is a - float32 tensor containing only binary values, which represent - instance masks for objects. - labels[fields.InputDataFields.groundtruth_instance_mask_weights] is a - float32 tensor containing weights for the instance masks. - labels[fields.InputDataFields.groundtruth_keypoints] is a - float32 tensor containing keypoints for each box. - labels[fields.InputDataFields.groundtruth_dp_num_points] is an int32 - tensor with the number of sampled DensePose points per object. - labels[fields.InputDataFields.groundtruth_dp_part_ids] is an int32 - tensor with the DensePose part ids (0-indexed) per object. - labels[fields.InputDataFields.groundtruth_dp_surface_coords] is a - float32 tensor with the DensePose surface coordinates. - labels[fields.InputDataFields.groundtruth_group_of] is a tf.bool tensor - containing group_of annotations. - labels[fields.InputDataFields.groundtruth_labeled_classes] is a float32 - k-hot tensor of classes. - labels[fields.InputDataFields.groundtruth_track_ids] is a int32 - tensor of track IDs. - labels[fields.InputDataFields.groundtruth_keypoint_depths] is a - float32 tensor containing keypoint depths information. - labels[fields.InputDataFields.groundtruth_keypoint_depth_weights] is a - float32 tensor containing the weights of the keypoint depth feature. - training_step: int, the current training step. - add_regularization_loss: Whether or not to include the model's - regularization loss in the losses dictionary. - - Returns: - A tuple containing the losses dictionary (with the total loss under - the key 'Loss/total_loss'), and the predictions dictionary produced by - `model.predict`. - - """ - model_lib.provide_groundtruth(model, labels, training_step=training_step) - preprocessed_images = features[fields.InputDataFields.image] - - prediction_dict = model.predict( - preprocessed_images, - features[fields.InputDataFields.true_image_shape], - **model.get_side_inputs(features)) - prediction_dict = ops.bfloat16_to_float32_nested(prediction_dict) - - losses_dict = model.loss( - prediction_dict, features[fields.InputDataFields.true_image_shape]) - losses = [loss_tensor for loss_tensor in losses_dict.values()] - if add_regularization_loss: - # TODO(kaftan): As we figure out mixed precision & bfloat 16, we may - ## need to convert these regularization losses from bfloat16 to float32 - ## as well. - regularization_losses = model.regularization_losses() - if regularization_losses: - regularization_losses = ops.bfloat16_to_float32_nested( - regularization_losses) - regularization_loss = tf.add_n( - regularization_losses, name='regularization_loss') - losses.append(regularization_loss) - losses_dict['Loss/regularization_loss'] = regularization_loss - - total_loss = tf.add_n(losses, name='total_loss') - losses_dict['Loss/total_loss'] = total_loss - - return losses_dict, prediction_dict - - -def _ensure_model_is_built(model, input_dataset, unpad_groundtruth_tensors): - """Ensures that model variables are all built, by running on a dummy input. - - Args: - model: A DetectionModel to be built. - input_dataset: The tf.data Dataset the model is being trained on. Needed to - get the shapes for the dummy loss computation. - unpad_groundtruth_tensors: A parameter passed to unstack_batch. - """ - features, labels = iter(input_dataset).next() - - @tf.function - def _dummy_computation_fn(features, labels): - model._is_training = False # pylint: disable=protected-access - tf.keras.backend.set_learning_phase(False) - - labels = model_lib.unstack_batch( - labels, unpad_groundtruth_tensors=unpad_groundtruth_tensors) - - return _compute_losses_and_predictions_dicts(model, features, labels, - training_step=0) - - strategy = tf.compat.v2.distribute.get_strategy() - if hasattr(tf.distribute.Strategy, 'run'): - strategy.run( - _dummy_computation_fn, args=( - features, - labels, - )) - else: - strategy.experimental_run_v2( - _dummy_computation_fn, args=( - features, - labels, - )) - - -def normalize_dict(values_dict, num_replicas): - - num_replicas = tf.constant(num_replicas, dtype=tf.float32) - return {key: tf.math.divide(loss, num_replicas) for key, loss - in values_dict.items()} - - -def reduce_dict(strategy, reduction_dict, reduction_op): - # TODO(anjalisridhar): explore if it is safe to remove the # num_replicas - # scaling of the loss and switch this to a ReduceOp.Mean - return { - name: strategy.reduce(reduction_op, loss, axis=None) - for name, loss in reduction_dict.items() - } - - -# TODO(kaftan): Explore removing learning_rate from this method & returning -## The full losses dict instead of just total_loss, then doing all summaries -## saving in a utility method called by the outer training loop. -# TODO(kaftan): Explore adding gradient summaries -def eager_train_step(detection_model, - features, - labels, - unpad_groundtruth_tensors, - optimizer, - training_step, - add_regularization_loss=True, - clip_gradients_value=None, - num_replicas=1.0): - """Process a single training batch. - - This method computes the loss for the model on a single training batch, - while tracking the gradients with a gradient tape. It then updates the - model variables with the optimizer, clipping the gradients if - clip_gradients_value is present. - - This method can run eagerly or inside a tf.function. - - Args: - detection_model: A DetectionModel (based on Keras) to train. - features: Dictionary of feature tensors from the input dataset. - Should be in the format output by `inputs.train_input. - features[fields.InputDataFields.image] is a [batch_size, H, W, C] - float32 tensor with preprocessed images. - features[HASH_KEY] is a [batch_size] int32 tensor representing unique - identifiers for the images. - features[fields.InputDataFields.true_image_shape] is a [batch_size, 3] - int32 tensor representing the true image shapes, as preprocessed - images could be padded. - features[fields.InputDataFields.original_image] (optional, not used - during training) is a - [batch_size, H, W, C] float32 tensor with original images. - labels: A dictionary of groundtruth tensors. This method unstacks - these labels using model_lib.unstack_batch. The stacked labels are of - the form returned by `inputs.train_input` and `inputs.eval_input`. - labels[fields.InputDataFields.num_groundtruth_boxes] is a [batch_size] - int32 tensor indicating the number of valid groundtruth boxes - per image. - labels[fields.InputDataFields.groundtruth_boxes] is a - [batch_size, num_boxes, 4] float32 tensor containing the corners of - the groundtruth boxes. - labels[fields.InputDataFields.groundtruth_classes] is a - [batch_size, num_boxes, num_classes] float32 one-hot tensor of - classes. num_classes includes the background class. - labels[fields.InputDataFields.groundtruth_weights] is a - [batch_size, num_boxes] float32 tensor containing groundtruth weights - for the boxes. - -- Optional -- - labels[fields.InputDataFields.groundtruth_instance_masks] is a - [batch_size, num_boxes, H, W] float32 tensor containing only binary - values, which represent instance masks for objects. - labels[fields.InputDataFields.groundtruth_instance_mask_weights] is a - [batch_size, num_boxes] float32 tensor containing weights for the - instance masks. - labels[fields.InputDataFields.groundtruth_keypoints] is a - [batch_size, num_boxes, num_keypoints, 2] float32 tensor containing - keypoints for each box. - labels[fields.InputDataFields.groundtruth_dp_num_points] is a - [batch_size, num_boxes] int32 tensor with the number of DensePose - sampled points per instance. - labels[fields.InputDataFields.groundtruth_dp_part_ids] is a - [batch_size, num_boxes, max_sampled_points] int32 tensor with the - part ids (0-indexed) for each instance. - labels[fields.InputDataFields.groundtruth_dp_surface_coords] is a - [batch_size, num_boxes, max_sampled_points, 4] float32 tensor with the - surface coordinates for each point. Each surface coordinate is of the - form (y, x, v, u) where (y, x) are normalized image locations and - (v, u) are part-relative normalized surface coordinates. - labels[fields.InputDataFields.groundtruth_labeled_classes] is a float32 - k-hot tensor of classes. - labels[fields.InputDataFields.groundtruth_track_ids] is a int32 - tensor of track IDs. - labels[fields.InputDataFields.groundtruth_keypoint_depths] is a - float32 tensor containing keypoint depths information. - labels[fields.InputDataFields.groundtruth_keypoint_depth_weights] is a - float32 tensor containing the weights of the keypoint depth feature. - unpad_groundtruth_tensors: A parameter passed to unstack_batch. - optimizer: The training optimizer that will update the variables. - training_step: int, the training step number. - add_regularization_loss: Whether or not to include the model's - regularization loss in the losses dictionary. - clip_gradients_value: If this is present, clip the gradients global norm - at this value using `tf.clip_by_global_norm`. - num_replicas: The number of replicas in the current distribution strategy. - This is used to scale the total loss so that training in a distribution - strategy works correctly. - - Returns: - The total loss observed at this training step - """ - # """Execute a single training step in the TF v2 style loop.""" - is_training = True - - detection_model._is_training = is_training # pylint: disable=protected-access - tf.keras.backend.set_learning_phase(is_training) - - labels = model_lib.unstack_batch( - labels, unpad_groundtruth_tensors=unpad_groundtruth_tensors) - - with tf.GradientTape() as tape: - losses_dict, _ = _compute_losses_and_predictions_dicts( - detection_model, features, labels, - training_step=training_step, - add_regularization_loss=add_regularization_loss) - - losses_dict = normalize_dict(losses_dict, num_replicas) - - trainable_variables = detection_model.trainable_variables - - total_loss = losses_dict['Loss/total_loss'] - gradients = tape.gradient(total_loss, trainable_variables) - - if clip_gradients_value: - gradients, _ = tf.clip_by_global_norm(gradients, clip_gradients_value) - optimizer.apply_gradients(zip(gradients, trainable_variables)) - - return losses_dict - - -def validate_tf_v2_checkpoint_restore_map(checkpoint_restore_map): - """Ensure that given dict is a valid TF v2 style restore map. - - Args: - checkpoint_restore_map: A nested dict mapping strings to - tf.keras.Model objects. - - Raises: - ValueError: If they keys in checkpoint_restore_map are not strings or if - the values are not keras Model objects. - - """ - - for key, value in checkpoint_restore_map.items(): - if not (isinstance(key, str) and - (isinstance(value, tf.Module) - or isinstance(value, tf.train.Checkpoint))): - if isinstance(key, str) and isinstance(value, dict): - validate_tf_v2_checkpoint_restore_map(value) - else: - raise TypeError( - RESTORE_MAP_ERROR_TEMPLATE.format(key.__class__.__name__, - value.__class__.__name__)) - - -def is_object_based_checkpoint(checkpoint_path): - """Returns true if `checkpoint_path` points to an object-based checkpoint.""" - var_names = [var[0] for var in tf.train.list_variables(checkpoint_path)] - return '_CHECKPOINTABLE_OBJECT_GRAPH' in var_names - - -def load_fine_tune_checkpoint(model, checkpoint_path, checkpoint_type, - checkpoint_version, run_model_on_dummy_input, - input_dataset, unpad_groundtruth_tensors): - """Load a fine tuning classification or detection checkpoint. - - To make sure the model variables are all built, this method first executes - the model by computing a dummy loss. (Models might not have built their - variables before their first execution) - - It then loads an object-based classification or detection checkpoint. - - This method updates the model in-place and does not return a value. - - Args: - model: A DetectionModel (based on Keras) to load a fine-tuning - checkpoint for. - checkpoint_path: Directory with checkpoints file or path to checkpoint. - checkpoint_type: Whether to restore from a full detection - checkpoint (with compatible variable names) or to restore from a - classification checkpoint for initialization prior to training. - Valid values: `detection`, `classification`. - checkpoint_version: train_pb2.CheckpointVersion.V1 or V2 enum indicating - whether to load checkpoints in V1 style or V2 style. In this binary - we only support V2 style (object-based) checkpoints. - run_model_on_dummy_input: Whether to run the model on a dummy input in order - to ensure that all model variables have been built successfully before - loading the fine_tune_checkpoint. - input_dataset: The tf.data Dataset the model is being trained on. Needed - to get the shapes for the dummy loss computation. - unpad_groundtruth_tensors: A parameter passed to unstack_batch. - - Raises: - IOError: if `checkpoint_path` does not point at a valid object-based - checkpoint - ValueError: if `checkpoint_version` is not train_pb2.CheckpointVersion.V2 - """ - if not is_object_based_checkpoint(checkpoint_path): - raise IOError('Checkpoint is expected to be an object-based checkpoint.') - if checkpoint_version == train_pb2.CheckpointVersion.V1: - raise ValueError('Checkpoint version should be V2') - - if run_model_on_dummy_input: - _ensure_model_is_built(model, input_dataset, unpad_groundtruth_tensors) - - restore_from_objects_dict = model.restore_from_objects( - fine_tune_checkpoint_type=checkpoint_type) - validate_tf_v2_checkpoint_restore_map(restore_from_objects_dict) - ckpt = tf.train.Checkpoint(**restore_from_objects_dict) - ckpt.restore( - checkpoint_path).expect_partial().assert_existing_objects_matched() - - -def get_filepath(strategy, filepath): - """Get appropriate filepath for worker. - - Args: - strategy: A tf.distribute.Strategy object. - filepath: A path to where the Checkpoint object is stored. - - Returns: - A temporary filepath for non-chief workers to use or the original filepath - for the chief. - """ - if strategy.extended.should_checkpoint: - return filepath - else: - # TODO(vighneshb) Replace with the public API when TF exposes it. - task_id = strategy.extended._task_id # pylint:disable=protected-access - return os.path.join(filepath, 'temp_worker_{:03d}'.format(task_id)) - - -def clean_temporary_directories(strategy, filepath): - """Temporary directory clean up for MultiWorker Mirrored Strategy. - - This is needed for all non-chief workers. - - Args: - strategy: A tf.distribute.Strategy object. - filepath: The filepath for the temporary directory. - """ - if not strategy.extended.should_checkpoint: - if tf.io.gfile.exists(filepath) and tf.io.gfile.isdir(filepath): - tf.io.gfile.rmtree(filepath) - - -def train_loop( - pipeline_config_path, - model_dir, - config_override=None, - train_steps=None, - use_tpu=False, - save_final_config=False, - checkpoint_every_n=1000, - checkpoint_max_to_keep=7, - record_summaries=True, - performance_summary_exporter=None, - num_steps_per_iteration=NUM_STEPS_PER_ITERATION, - **kwargs): - """Trains a model using eager + functions. - - This method: - 1. Processes the pipeline configs - 2. (Optionally) saves the as-run config - 3. Builds the model & optimizer - 4. Gets the training input data - 5. Loads a fine-tuning detection or classification checkpoint if requested - 6. Loops over the train data, executing distributed training steps inside - tf.functions. - 7. Checkpoints the model every `checkpoint_every_n` training steps. - 8. Logs the training metrics as TensorBoard summaries. - - Args: - pipeline_config_path: A path to a pipeline config file. - model_dir: - The directory to save checkpoints and summaries to. - config_override: A pipeline_pb2.TrainEvalPipelineConfig text proto to - override the config from `pipeline_config_path`. - train_steps: Number of training steps. If None, the number of training steps - is set from the `TrainConfig` proto. - use_tpu: Boolean, whether training and evaluation should run on TPU. - save_final_config: Whether to save final config (obtained after applying - overrides) to `model_dir`. - checkpoint_every_n: - Checkpoint every n training steps. - checkpoint_max_to_keep: - int, the number of most recent checkpoints to keep in the model directory. - record_summaries: Boolean, whether or not to record summaries defined by - the model or the training pipeline. This does not impact the summaries - of the loss values which are always recorded. Examples of summaries - that are controlled by this flag include: - - Image summaries of training images. - - Intermediate tensors which maybe logged by meta architectures. - performance_summary_exporter: function for exporting performance metrics. - num_steps_per_iteration: int, The number of training steps to perform - in each iteration. - **kwargs: Additional keyword arguments for configuration override. - """ - ## Parse the configs - get_configs_from_pipeline_file = MODEL_BUILD_UTIL_MAP[ - 'get_configs_from_pipeline_file'] - merge_external_params_with_configs = MODEL_BUILD_UTIL_MAP[ - 'merge_external_params_with_configs'] - create_pipeline_proto_from_configs = MODEL_BUILD_UTIL_MAP[ - 'create_pipeline_proto_from_configs'] - steps_per_sec_list = [] - - configs = get_configs_from_pipeline_file( - pipeline_config_path, config_override=config_override) - kwargs.update({ - 'train_steps': train_steps, - 'use_bfloat16': configs['train_config'].use_bfloat16 and use_tpu - }) - configs = merge_external_params_with_configs( - configs, None, kwargs_dict=kwargs) - model_config = configs['model'] - train_config = configs['train_config'] - train_input_config = configs['train_input_config'] - - unpad_groundtruth_tensors = train_config.unpad_groundtruth_tensors - add_regularization_loss = train_config.add_regularization_loss - clip_gradients_value = None - if train_config.gradient_clipping_by_norm > 0: - clip_gradients_value = train_config.gradient_clipping_by_norm - - # update train_steps from config but only when non-zero value is provided - if train_steps is None and train_config.num_steps != 0: - train_steps = train_config.num_steps - - if kwargs['use_bfloat16']: - tf.compat.v2.keras.mixed_precision.set_global_policy('mixed_bfloat16') - - if train_config.load_all_detection_checkpoint_vars: - raise ValueError('train_pb2.load_all_detection_checkpoint_vars ' - 'unsupported in TF2') - - config_util.update_fine_tune_checkpoint_type(train_config) - fine_tune_checkpoint_type = train_config.fine_tune_checkpoint_type - fine_tune_checkpoint_version = train_config.fine_tune_checkpoint_version - - # Write the as-run pipeline config to disk. - if save_final_config: - tf.logging.info('Saving pipeline config file to directory %s', model_dir) - pipeline_config_final = create_pipeline_proto_from_configs(configs) - config_util.save_pipeline_config(pipeline_config_final, model_dir) - - # Build the model, optimizer, and training input - strategy = tf.compat.v2.distribute.get_strategy() - with strategy.scope(): - detection_model = MODEL_BUILD_UTIL_MAP['detection_model_fn_base']( - model_config=model_config, is_training=True, - add_summaries=record_summaries) - - def train_dataset_fn(input_context): - """Callable to create train input.""" - # Create the inputs. - train_input = inputs.train_input( - train_config=train_config, - train_input_config=train_input_config, - model_config=model_config, - model=detection_model, - input_context=input_context) - train_input = train_input.repeat() - return train_input - - train_input = strategy.experimental_distribute_datasets_from_function( - train_dataset_fn) - - - global_step = tf.Variable( - 0, trainable=False, dtype=tf.compat.v2.dtypes.int64, name='global_step', - aggregation=tf.compat.v2.VariableAggregation.ONLY_FIRST_REPLICA) - optimizer, (learning_rate,) = optimizer_builder.build( - train_config.optimizer, global_step=global_step) - - # We run the detection_model on dummy inputs in order to ensure that the - # model and all its variables have been properly constructed. Specifically, - # this is currently necessary prior to (potentially) creating shadow copies - # of the model variables for the EMA optimizer. - if train_config.optimizer.use_moving_average: - _ensure_model_is_built(detection_model, train_input, - unpad_groundtruth_tensors) - optimizer.shadow_copy(detection_model) - - if callable(learning_rate): - learning_rate_fn = learning_rate - else: - learning_rate_fn = lambda: learning_rate - - ## Train the model - # Get the appropriate filepath (temporary or not) based on whether the worker - # is the chief. - summary_writer_filepath = get_filepath(strategy, - os.path.join(model_dir, 'train')) - - summary_writer = tf.compat.v2.summary.create_file_writer( - summary_writer_filepath) - - with summary_writer.as_default(): - with strategy.scope(): - with tf.compat.v2.summary.record_if( - lambda: global_step % num_steps_per_iteration == 0): - # Load a fine-tuning checkpoint. - if train_config.fine_tune_checkpoint: - variables_helper.ensure_checkpoint_supported( - train_config.fine_tune_checkpoint, fine_tune_checkpoint_type, - model_dir) - load_fine_tune_checkpoint( - detection_model, train_config.fine_tune_checkpoint, - fine_tune_checkpoint_type, fine_tune_checkpoint_version, - train_config.run_fine_tune_checkpoint_dummy_computation, - train_input, unpad_groundtruth_tensors) - - ckpt = tf.compat.v2.train.Checkpoint( - step=global_step, model=detection_model, optimizer=optimizer) - - manager_dir = get_filepath(strategy, model_dir) - if not strategy.extended.should_checkpoint: - checkpoint_max_to_keep = 1 - manager = tf.compat.v2.train.CheckpointManager( - ckpt, manager_dir, max_to_keep=checkpoint_max_to_keep) - - # We use the following instead of manager.latest_checkpoint because - # manager_dir does not point to the model directory when we are running - # in a worker. - latest_checkpoint = tf.train.latest_checkpoint(model_dir) - ckpt.restore(latest_checkpoint) - - def train_step_fn(features, labels): - """Single train step.""" - - if record_summaries: - tf.compat.v2.summary.image( - name='train_input_images', - step=global_step, - data=features[fields.InputDataFields.image], - max_outputs=3) - losses_dict = eager_train_step( - detection_model, - features, - labels, - unpad_groundtruth_tensors, - optimizer, - training_step=global_step, - add_regularization_loss=add_regularization_loss, - clip_gradients_value=clip_gradients_value, - num_replicas=strategy.num_replicas_in_sync) - global_step.assign_add(1) - return losses_dict - - def _sample_and_train(strategy, train_step_fn, data_iterator): - features, labels = data_iterator.next() - if hasattr(tf.distribute.Strategy, 'run'): - per_replica_losses_dict = strategy.run( - train_step_fn, args=(features, labels)) - else: - per_replica_losses_dict = ( - strategy.experimental_run_v2( - train_step_fn, args=(features, labels))) - - return reduce_dict( - strategy, per_replica_losses_dict, tf.distribute.ReduceOp.SUM) - - @tf.function - def _dist_train_step(data_iterator): - """A distributed train step.""" - - if num_steps_per_iteration > 1: - for _ in tf.range(num_steps_per_iteration - 1): - # Following suggestion on yaqs/5402607292645376 - with tf.name_scope(''): - _sample_and_train(strategy, train_step_fn, data_iterator) - - return _sample_and_train(strategy, train_step_fn, data_iterator) - - train_input_iter = iter(train_input) - - if int(global_step.value()) == 0: - manager.save() - - checkpointed_step = int(global_step.value()) - logged_step = global_step.value() - - last_step_time = time.time() - for _ in range(global_step.value(), train_steps, - num_steps_per_iteration): - - losses_dict = _dist_train_step(train_input_iter) - - time_taken = time.time() - last_step_time - last_step_time = time.time() - steps_per_sec = num_steps_per_iteration * 1.0 / time_taken - - tf.compat.v2.summary.scalar( - 'steps_per_sec', steps_per_sec, step=global_step) - - steps_per_sec_list.append(steps_per_sec) - - logged_dict = losses_dict.copy() - logged_dict['learning_rate'] = learning_rate_fn() - - for key, val in logged_dict.items(): - tf.compat.v2.summary.scalar(key, val, step=global_step) - - if global_step.value() - logged_step >= LOG_EVERY: - logged_dict_np = {name: value.numpy() for name, value in - logged_dict.items()} - tf.logging.info( - 'Step {} per-step time {:.3f}s'.format( - global_step.value(), time_taken / num_steps_per_iteration)) - tf.logging.info(pprint.pformat(logged_dict_np, width=40)) - logged_step = global_step.value() - - if ((int(global_step.value()) - checkpointed_step) >= - checkpoint_every_n): - manager.save() - checkpointed_step = int(global_step.value()) - - # Remove the checkpoint directories of the non-chief workers that - # MultiWorkerMirroredStrategy forces us to save during sync distributed - # training. - clean_temporary_directories(strategy, manager_dir) - clean_temporary_directories(strategy, summary_writer_filepath) - # TODO(pkanwar): add accuracy metrics. - if performance_summary_exporter is not None: - metrics = { - 'steps_per_sec': np.mean(steps_per_sec_list), - 'steps_per_sec_p50': np.median(steps_per_sec_list), - 'steps_per_sec_max': max(steps_per_sec_list), - 'last_batch_loss': float(losses_dict['Loss/total_loss']) - } - mixed_precision = 'bf16' if kwargs['use_bfloat16'] else 'fp32' - performance_summary_exporter(metrics, mixed_precision) - - -def prepare_eval_dict(detections, groundtruth, features): - """Prepares eval dictionary containing detections and groundtruth. - - Takes in `detections` from the model, `groundtruth` and `features` returned - from the eval tf.data.dataset and creates a dictionary of tensors suitable - for detection eval modules. - - Args: - detections: A dictionary of tensors returned by `model.postprocess`. - groundtruth: `inputs.eval_input` returns an eval dataset of (features, - labels) tuple. `groundtruth` must be set to `labels`. - Please note that: - * fields.InputDataFields.groundtruth_classes must be 0-indexed and - in its 1-hot representation. - * fields.InputDataFields.groundtruth_verified_neg_classes must be - 0-indexed and in its multi-hot repesentation. - * fields.InputDataFields.groundtruth_not_exhaustive_classes must be - 0-indexed and in its multi-hot repesentation. - * fields.InputDataFields.groundtruth_labeled_classes must be - 0-indexed and in its multi-hot repesentation. - features: `inputs.eval_input` returns an eval dataset of (features, labels) - tuple. This argument must be set to a dictionary containing the following - keys and their corresponding values from `features` -- - * fields.InputDataFields.image - * fields.InputDataFields.original_image - * fields.InputDataFields.original_image_spatial_shape - * fields.InputDataFields.true_image_shape - * inputs.HASH_KEY - - Returns: - eval_dict: A dictionary of tensors to pass to eval module. - class_agnostic: Whether to evaluate detection in class agnostic mode. - """ - - groundtruth_boxes = groundtruth[fields.InputDataFields.groundtruth_boxes] - groundtruth_boxes_shape = tf.shape(groundtruth_boxes) - # For class-agnostic models, groundtruth one-hot encodings collapse to all - # ones. - class_agnostic = ( - fields.DetectionResultFields.detection_classes not in detections) - if class_agnostic: - groundtruth_classes_one_hot = tf.ones( - [groundtruth_boxes_shape[0], groundtruth_boxes_shape[1], 1]) - else: - groundtruth_classes_one_hot = groundtruth[ - fields.InputDataFields.groundtruth_classes] - label_id_offset = 1 # Applying label id offset (b/63711816) - groundtruth_classes = ( - tf.argmax(groundtruth_classes_one_hot, axis=2) + label_id_offset) - groundtruth[fields.InputDataFields.groundtruth_classes] = groundtruth_classes - - label_id_offset_paddings = tf.constant([[0, 0], [1, 0]]) - if fields.InputDataFields.groundtruth_verified_neg_classes in groundtruth: - groundtruth[ - fields.InputDataFields.groundtruth_verified_neg_classes] = tf.pad( - groundtruth[ - fields.InputDataFields.groundtruth_verified_neg_classes], - label_id_offset_paddings) - if fields.InputDataFields.groundtruth_not_exhaustive_classes in groundtruth: - groundtruth[ - fields.InputDataFields.groundtruth_not_exhaustive_classes] = tf.pad( - groundtruth[ - fields.InputDataFields.groundtruth_not_exhaustive_classes], - label_id_offset_paddings) - if fields.InputDataFields.groundtruth_labeled_classes in groundtruth: - groundtruth[fields.InputDataFields.groundtruth_labeled_classes] = tf.pad( - groundtruth[fields.InputDataFields.groundtruth_labeled_classes], - label_id_offset_paddings) - - use_original_images = fields.InputDataFields.original_image in features - if use_original_images: - eval_images = features[fields.InputDataFields.original_image] - true_image_shapes = features[fields.InputDataFields.true_image_shape][:, :3] - original_image_spatial_shapes = features[ - fields.InputDataFields.original_image_spatial_shape] - else: - eval_images = features[fields.InputDataFields.image] - true_image_shapes = None - original_image_spatial_shapes = None - - eval_dict = eval_util.result_dict_for_batched_example( - eval_images, - features[inputs.HASH_KEY], - detections, - groundtruth, - class_agnostic=class_agnostic, - scale_to_absolute=True, - original_image_spatial_shapes=original_image_spatial_shapes, - true_image_shapes=true_image_shapes) - - return eval_dict, class_agnostic - - -def concat_replica_results(tensor_dict): - new_tensor_dict = {} - for key, values in tensor_dict.items(): - new_tensor_dict[key] = tf.concat(values, axis=0) - return new_tensor_dict - - -def eager_eval_loop( - detection_model, - configs, - eval_dataset, - use_tpu=False, - postprocess_on_cpu=False, - global_step=None, - ): - """Evaluate the model eagerly on the evaluation dataset. - - This method will compute the evaluation metrics specified in the configs on - the entire evaluation dataset, then return the metrics. It will also log - the metrics to TensorBoard. - - Args: - detection_model: A DetectionModel (based on Keras) to evaluate. - configs: Object detection configs that specify the evaluators that should - be used, as well as whether regularization loss should be included and - if bfloat16 should be used on TPUs. - eval_dataset: Dataset containing evaluation data. - use_tpu: Whether a TPU is being used to execute the model for evaluation. - postprocess_on_cpu: Whether model postprocessing should happen on - the CPU when using a TPU to execute the model. - global_step: A variable containing the training step this model was trained - to. Used for logging purposes. - - Returns: - A dict of evaluation metrics representing the results of this evaluation. - """ - del postprocess_on_cpu - train_config = configs['train_config'] - eval_input_config = configs['eval_input_config'] - eval_config = configs['eval_config'] - add_regularization_loss = train_config.add_regularization_loss - - is_training = False - detection_model._is_training = is_training # pylint: disable=protected-access - tf.keras.backend.set_learning_phase(is_training) - - evaluator_options = eval_util.evaluator_options_from_eval_config( - eval_config) - batch_size = eval_config.batch_size - - class_agnostic_category_index = ( - label_map_util.create_class_agnostic_category_index()) - class_agnostic_evaluators = eval_util.get_evaluators( - eval_config, - list(class_agnostic_category_index.values()), - evaluator_options) - - class_aware_evaluators = None - if eval_input_config.label_map_path: - class_aware_category_index = ( - label_map_util.create_category_index_from_labelmap( - eval_input_config.label_map_path)) - class_aware_evaluators = eval_util.get_evaluators( - eval_config, - list(class_aware_category_index.values()), - evaluator_options) - - evaluators = None - loss_metrics = {} - - @tf.function - def compute_eval_dict(features, labels): - """Compute the evaluation result on an image.""" - # For evaling on train data, it is necessary to check whether groundtruth - # must be unpadded. - boxes_shape = ( - labels[fields.InputDataFields.groundtruth_boxes].get_shape().as_list()) - unpad_groundtruth_tensors = (boxes_shape[1] is not None - and not use_tpu - and batch_size == 1) - groundtruth_dict = labels - labels = model_lib.unstack_batch( - labels, unpad_groundtruth_tensors=unpad_groundtruth_tensors) - - losses_dict, prediction_dict = _compute_losses_and_predictions_dicts( - detection_model, features, labels, training_step=None, - add_regularization_loss=add_regularization_loss) - prediction_dict = detection_model.postprocess( - prediction_dict, features[fields.InputDataFields.true_image_shape]) - eval_features = { - fields.InputDataFields.image: - features[fields.InputDataFields.image], - fields.InputDataFields.original_image: - features[fields.InputDataFields.original_image], - fields.InputDataFields.original_image_spatial_shape: - features[fields.InputDataFields.original_image_spatial_shape], - fields.InputDataFields.true_image_shape: - features[fields.InputDataFields.true_image_shape], - inputs.HASH_KEY: features[inputs.HASH_KEY], - } - return losses_dict, prediction_dict, groundtruth_dict, eval_features - - agnostic_categories = label_map_util.create_class_agnostic_category_index() - per_class_categories = label_map_util.create_category_index_from_labelmap( - eval_input_config.label_map_path) - keypoint_edges = [ - (kp.start, kp.end) for kp in eval_config.keypoint_edge] - - strategy = tf.compat.v2.distribute.get_strategy() - - for i, (features, labels) in enumerate(eval_dataset): - try: - (losses_dict, prediction_dict, groundtruth_dict, - eval_features) = strategy.run( - compute_eval_dict, args=(features, labels)) - except Exception as exc: # pylint:disable=broad-except - tf.logging.info('Encountered %s exception.', exc) - tf.logging.info('A replica probably exhausted all examples. Skipping ' - 'pending examples on other replicas.') - break - (local_prediction_dict, local_groundtruth_dict, - local_eval_features) = tf.nest.map_structure( - strategy.experimental_local_results, - [prediction_dict, groundtruth_dict, eval_features]) - local_prediction_dict = concat_replica_results(local_prediction_dict) - local_groundtruth_dict = concat_replica_results(local_groundtruth_dict) - local_eval_features = concat_replica_results(local_eval_features) - - eval_dict, class_agnostic = prepare_eval_dict(local_prediction_dict, - local_groundtruth_dict, - local_eval_features) - for loss_key, loss_tensor in iter(losses_dict.items()): - losses_dict[loss_key] = strategy.reduce(tf.distribute.ReduceOp.MEAN, - loss_tensor, None) - if class_agnostic: - category_index = agnostic_categories - else: - category_index = per_class_categories - - if i % 100 == 0: - tf.logging.info('Finished eval step %d', i) - - use_original_images = fields.InputDataFields.original_image in features - if (use_original_images and i < eval_config.num_visualizations): - sbys_image_list = vutils.draw_side_by_side_evaluation_image( - eval_dict, - category_index=category_index, - max_boxes_to_draw=eval_config.max_num_boxes_to_visualize, - min_score_thresh=eval_config.min_score_threshold, - use_normalized_coordinates=False, - keypoint_edges=keypoint_edges or None) - for j, sbys_image in enumerate(sbys_image_list): - tf.compat.v2.summary.image( - name='eval_side_by_side_{}_{}'.format(i, j), - step=global_step, - data=sbys_image, - max_outputs=eval_config.num_visualizations) - if eval_util.has_densepose(eval_dict): - dp_image_list = vutils.draw_densepose_visualizations( - eval_dict) - for j, dp_image in enumerate(dp_image_list): - tf.compat.v2.summary.image( - name='densepose_detections_{}_{}'.format(i, j), - step=global_step, - data=dp_image, - max_outputs=eval_config.num_visualizations) - - if evaluators is None: - if class_agnostic: - evaluators = class_agnostic_evaluators - else: - evaluators = class_aware_evaluators - - for evaluator in evaluators: - evaluator.add_eval_dict(eval_dict) - - for loss_key, loss_tensor in iter(losses_dict.items()): - if loss_key not in loss_metrics: - loss_metrics[loss_key] = [] - loss_metrics[loss_key].append(loss_tensor) - - eval_metrics = {} - - for evaluator in evaluators: - eval_metrics.update(evaluator.evaluate()) - for loss_key in loss_metrics: - eval_metrics[loss_key] = tf.reduce_mean(loss_metrics[loss_key]) - - eval_metrics = {str(k): v for k, v in eval_metrics.items()} - tf.logging.info('Eval metrics at step %d', global_step.numpy()) - for k in eval_metrics: - tf.compat.v2.summary.scalar(k, eval_metrics[k], step=global_step) - tf.logging.info('\t+ %s: %f', k, eval_metrics[k]) - return eval_metrics - - -def eval_continuously( - pipeline_config_path, - config_override=None, - train_steps=None, - sample_1_of_n_eval_examples=1, - sample_1_of_n_eval_on_train_examples=1, - use_tpu=False, - override_eval_num_epochs=True, - postprocess_on_cpu=False, - model_dir=None, - checkpoint_dir=None, - wait_interval=180, - timeout=3600, - eval_index=0, - save_final_config=False, - **kwargs): - """Run continuous evaluation of a detection model eagerly. - - This method builds the model, and continously restores it from the most - recent training checkpoint in the checkpoint directory & evaluates it - on the evaluation data. - - Args: - pipeline_config_path: A path to a pipeline config file. - config_override: A pipeline_pb2.TrainEvalPipelineConfig text proto to - override the config from `pipeline_config_path`. - train_steps: Number of training steps. If None, the number of training steps - is set from the `TrainConfig` proto. - sample_1_of_n_eval_examples: Integer representing how often an eval example - should be sampled. If 1, will sample all examples. - sample_1_of_n_eval_on_train_examples: Similar to - `sample_1_of_n_eval_examples`, except controls the sampling of training - data for evaluation. - use_tpu: Boolean, whether training and evaluation should run on TPU. - override_eval_num_epochs: Whether to overwrite the number of epochs to 1 for - eval_input. - postprocess_on_cpu: When use_tpu and postprocess_on_cpu are true, - postprocess is scheduled on the host cpu. - model_dir: Directory to output resulting evaluation summaries to. - checkpoint_dir: Directory that contains the training checkpoints. - wait_interval: The mimmum number of seconds to wait before checking for a - new checkpoint. - timeout: The maximum number of seconds to wait for a checkpoint. Execution - will terminate if no new checkpoints are found after these many seconds. - eval_index: int, If given, only evaluate the dataset at the given - index. By default, evaluates dataset at 0'th index. - save_final_config: Whether to save the pipeline config file to the model - directory. - **kwargs: Additional keyword arguments for configuration override. - """ - get_configs_from_pipeline_file = MODEL_BUILD_UTIL_MAP[ - 'get_configs_from_pipeline_file'] - create_pipeline_proto_from_configs = MODEL_BUILD_UTIL_MAP[ - 'create_pipeline_proto_from_configs'] - merge_external_params_with_configs = MODEL_BUILD_UTIL_MAP[ - 'merge_external_params_with_configs'] - - configs = get_configs_from_pipeline_file( - pipeline_config_path, config_override=config_override) - kwargs.update({ - 'sample_1_of_n_eval_examples': sample_1_of_n_eval_examples, - 'use_bfloat16': configs['train_config'].use_bfloat16 and use_tpu - }) - if train_steps is not None: - kwargs['train_steps'] = train_steps - if override_eval_num_epochs: - kwargs.update({'eval_num_epochs': 1}) - tf.logging.warning( - 'Forced number of epochs for all eval validations to be 1.') - configs = merge_external_params_with_configs( - configs, None, kwargs_dict=kwargs) - if model_dir and save_final_config: - tf.logging.info('Saving pipeline config file to directory %s', model_dir) - pipeline_config_final = create_pipeline_proto_from_configs(configs) - config_util.save_pipeline_config(pipeline_config_final, model_dir) - - model_config = configs['model'] - train_input_config = configs['train_input_config'] - eval_config = configs['eval_config'] - eval_input_configs = configs['eval_input_configs'] - eval_on_train_input_config = copy.deepcopy(train_input_config) - eval_on_train_input_config.sample_1_of_n_examples = ( - sample_1_of_n_eval_on_train_examples) - if override_eval_num_epochs and eval_on_train_input_config.num_epochs != 1: - tf.logging.warning( - ('Expected number of evaluation epochs is 1, but ' - 'instead encountered `eval_on_train_input_config' - '.num_epochs` = %d. Overwriting `num_epochs` to 1.'), - eval_on_train_input_config.num_epochs) - eval_on_train_input_config.num_epochs = 1 - - if kwargs['use_bfloat16']: - tf.compat.v2.keras.mixed_precision.set_global_policy('mixed_bfloat16') - - eval_input_config = eval_input_configs[eval_index] - strategy = tf.compat.v2.distribute.get_strategy() - with strategy.scope(): - detection_model = MODEL_BUILD_UTIL_MAP['detection_model_fn_base']( - model_config=model_config, is_training=True) - - eval_input = strategy.experimental_distribute_dataset( - inputs.eval_input( - eval_config=eval_config, - eval_input_config=eval_input_config, - model_config=model_config, - model=detection_model)) - - global_step = tf.compat.v2.Variable( - 0, trainable=False, dtype=tf.compat.v2.dtypes.int64) - - optimizer, _ = optimizer_builder.build( - configs['train_config'].optimizer, global_step=global_step) - - for latest_checkpoint in tf.train.checkpoints_iterator( - checkpoint_dir, timeout=timeout, min_interval_secs=wait_interval): - ckpt = tf.compat.v2.train.Checkpoint( - step=global_step, model=detection_model, optimizer=optimizer) - - # We run the detection_model on dummy inputs in order to ensure that the - # model and all its variables have been properly constructed. Specifically, - # this is currently necessary prior to (potentially) creating shadow copies - # of the model variables for the EMA optimizer. - if eval_config.use_moving_averages: - unpad_groundtruth_tensors = (eval_config.batch_size == 1 and not use_tpu) - _ensure_model_is_built(detection_model, eval_input, - unpad_groundtruth_tensors) - optimizer.shadow_copy(detection_model) - - ckpt.restore(latest_checkpoint).expect_partial() - - if eval_config.use_moving_averages: - optimizer.swap_weights() - - summary_writer = tf.compat.v2.summary.create_file_writer( - os.path.join(model_dir, 'eval', eval_input_config.name)) - with summary_writer.as_default(): - eager_eval_loop( - detection_model, - configs, - eval_input, - use_tpu=use_tpu, - postprocess_on_cpu=postprocess_on_cpu, - global_step=global_step, - ) - - if global_step.numpy() == configs['train_config'].num_steps: - tf.logging.info('Exiting evaluation at step %d', global_step.numpy()) - return diff --git a/research/object_detection/model_main.py b/research/object_detection/model_main.py deleted file mode 100644 index bfd5ba5beb6..00000000000 --- a/research/object_detection/model_main.py +++ /dev/null @@ -1,109 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Binary to run train and evaluation on object detection model.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl import flags - -import tensorflow.compat.v1 as tf -from tensorflow.compat.v1 import estimator as tf_estimator - -from object_detection import model_lib - -flags.DEFINE_string( - 'model_dir', None, 'Path to output model directory ' - 'where event and checkpoint files will be written.') -flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config ' - 'file.') -flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.') -flags.DEFINE_boolean('eval_training_data', False, - 'If training data should be evaluated for this job. Note ' - 'that one call only use this in eval-only mode, and ' - '`checkpoint_dir` must be supplied.') -flags.DEFINE_integer('sample_1_of_n_eval_examples', 1, 'Will sample one of ' - 'every n eval input examples, where n is provided.') -flags.DEFINE_integer('sample_1_of_n_eval_on_train_examples', 5, 'Will sample ' - 'one of every n train input examples for evaluation, ' - 'where n is provided. This is only used if ' - '`eval_training_data` is True.') -flags.DEFINE_string( - 'checkpoint_dir', None, 'Path to directory holding a checkpoint. If ' - '`checkpoint_dir` is provided, this binary operates in eval-only mode, ' - 'writing resulting metrics to `model_dir`.') -flags.DEFINE_boolean( - 'run_once', False, 'If running in eval-only mode, whether to run just ' - 'one round of eval vs running continuously (default).' -) -flags.DEFINE_integer( - 'max_eval_retries', 0, 'If running continuous eval, the maximum number of ' - 'retries upon encountering tf.errors.InvalidArgumentError. If negative, ' - 'will always retry the evaluation.' -) -FLAGS = flags.FLAGS - - -def main(unused_argv): - flags.mark_flag_as_required('model_dir') - flags.mark_flag_as_required('pipeline_config_path') - config = tf_estimator.RunConfig(model_dir=FLAGS.model_dir) - - train_and_eval_dict = model_lib.create_estimator_and_inputs( - run_config=config, - pipeline_config_path=FLAGS.pipeline_config_path, - train_steps=FLAGS.num_train_steps, - sample_1_of_n_eval_examples=FLAGS.sample_1_of_n_eval_examples, - sample_1_of_n_eval_on_train_examples=( - FLAGS.sample_1_of_n_eval_on_train_examples)) - estimator = train_and_eval_dict['estimator'] - train_input_fn = train_and_eval_dict['train_input_fn'] - eval_input_fns = train_and_eval_dict['eval_input_fns'] - eval_on_train_input_fn = train_and_eval_dict['eval_on_train_input_fn'] - predict_input_fn = train_and_eval_dict['predict_input_fn'] - train_steps = train_and_eval_dict['train_steps'] - - if FLAGS.checkpoint_dir: - if FLAGS.eval_training_data: - name = 'training_data' - input_fn = eval_on_train_input_fn - else: - name = 'validation_data' - # The first eval input will be evaluated. - input_fn = eval_input_fns[0] - if FLAGS.run_once: - estimator.evaluate(input_fn, - steps=None, - checkpoint_path=tf.train.latest_checkpoint( - FLAGS.checkpoint_dir)) - else: - model_lib.continuous_eval(estimator, FLAGS.checkpoint_dir, input_fn, - train_steps, name, FLAGS.max_eval_retries) - else: - train_spec, eval_specs = model_lib.create_train_and_eval_specs( - train_input_fn, - eval_input_fns, - eval_on_train_input_fn, - predict_input_fn, - train_steps, - eval_on_train_data=False) - - # Currently only a single Eval Spec is allowed. - tf_estimator.train_and_evaluate(estimator, train_spec, eval_specs[0]) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/model_main_tf2.py b/research/object_detection/model_main_tf2.py deleted file mode 100644 index 501e4de496b..00000000000 --- a/research/object_detection/model_main_tf2.py +++ /dev/null @@ -1,114 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -r"""Creates and runs TF2 object detection models. - -For local training/evaluation run: -PIPELINE_CONFIG_PATH=path/to/pipeline.config -MODEL_DIR=/tmp/model_outputs -NUM_TRAIN_STEPS=10000 -SAMPLE_1_OF_N_EVAL_EXAMPLES=1 -python model_main_tf2.py -- \ - --model_dir=$MODEL_DIR --num_train_steps=$NUM_TRAIN_STEPS \ - --sample_1_of_n_eval_examples=$SAMPLE_1_OF_N_EVAL_EXAMPLES \ - --pipeline_config_path=$PIPELINE_CONFIG_PATH \ - --alsologtostderr -""" -from absl import flags -import tensorflow.compat.v2 as tf -from object_detection import model_lib_v2 - -flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config ' - 'file.') -flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.') -flags.DEFINE_bool('eval_on_train_data', False, 'Enable evaluating on train ' - 'data (only supported in distributed training).') -flags.DEFINE_integer('sample_1_of_n_eval_examples', None, 'Will sample one of ' - 'every n eval input examples, where n is provided.') -flags.DEFINE_integer('sample_1_of_n_eval_on_train_examples', 5, 'Will sample ' - 'one of every n train input examples for evaluation, ' - 'where n is provided. This is only used if ' - '`eval_training_data` is True.') -flags.DEFINE_string( - 'model_dir', None, 'Path to output model directory ' - 'where event and checkpoint files will be written.') -flags.DEFINE_string( - 'checkpoint_dir', None, 'Path to directory holding a checkpoint. If ' - '`checkpoint_dir` is provided, this binary operates in eval-only mode, ' - 'writing resulting metrics to `model_dir`.') - -flags.DEFINE_integer('eval_timeout', 3600, 'Number of seconds to wait for an' - 'evaluation checkpoint before exiting.') - -flags.DEFINE_bool('use_tpu', False, 'Whether the job is executing on a TPU.') -flags.DEFINE_string( - 'tpu_name', - default=None, - help='Name of the Cloud TPU for Cluster Resolvers.') -flags.DEFINE_integer( - 'num_workers', 1, 'When num_workers > 1, training uses ' - 'MultiWorkerMirroredStrategy. When num_workers = 1 it uses ' - 'MirroredStrategy.') -flags.DEFINE_integer( - 'checkpoint_every_n', 1000, 'Integer defining how often we checkpoint.') -flags.DEFINE_boolean('record_summaries', True, - ('Whether or not to record summaries defined by the model' - ' or the training pipeline. This does not impact the' - ' summaries of the loss values which are always' - ' recorded.')) - -FLAGS = flags.FLAGS - - -def main(unused_argv): - flags.mark_flag_as_required('model_dir') - flags.mark_flag_as_required('pipeline_config_path') - tf.config.set_soft_device_placement(True) - - if FLAGS.checkpoint_dir: - model_lib_v2.eval_continuously( - pipeline_config_path=FLAGS.pipeline_config_path, - model_dir=FLAGS.model_dir, - train_steps=FLAGS.num_train_steps, - sample_1_of_n_eval_examples=FLAGS.sample_1_of_n_eval_examples, - sample_1_of_n_eval_on_train_examples=( - FLAGS.sample_1_of_n_eval_on_train_examples), - checkpoint_dir=FLAGS.checkpoint_dir, - wait_interval=300, timeout=FLAGS.eval_timeout) - else: - if FLAGS.use_tpu: - # TPU is automatically inferred if tpu_name is None and - # we are running under cloud ai-platform. - resolver = tf.distribute.cluster_resolver.TPUClusterResolver( - FLAGS.tpu_name) - tf.config.experimental_connect_to_cluster(resolver) - tf.tpu.experimental.initialize_tpu_system(resolver) - strategy = tf.distribute.experimental.TPUStrategy(resolver) - elif FLAGS.num_workers > 1: - strategy = tf.distribute.experimental.MultiWorkerMirroredStrategy() - else: - strategy = tf.compat.v2.distribute.MirroredStrategy() - - with strategy.scope(): - model_lib_v2.train_loop( - pipeline_config_path=FLAGS.pipeline_config_path, - model_dir=FLAGS.model_dir, - train_steps=FLAGS.num_train_steps, - use_tpu=FLAGS.use_tpu, - checkpoint_every_n=FLAGS.checkpoint_every_n, - record_summaries=FLAGS.record_summaries) - -if __name__ == '__main__': - tf.compat.v1.app.run() diff --git a/research/object_detection/model_tpu_main.py b/research/object_detection/model_tpu_main.py deleted file mode 100644 index 0771b38858b..00000000000 --- a/research/object_detection/model_tpu_main.py +++ /dev/null @@ -1,140 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -r"""Creates and runs `Estimator` for object detection model on TPUs. - -This uses the TPUEstimator API to define and run a model in TRAIN/EVAL modes. -""" -# pylint: enable=line-too-long - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl import flags -import tensorflow.compat.v1 as tf -from tensorflow.compat.v1 import estimator as tf_estimator - - -from object_detection import model_lib - -tf.flags.DEFINE_bool('use_tpu', True, 'Use TPUs rather than plain CPUs') - -# Cloud TPU Cluster Resolvers -flags.DEFINE_string( - 'gcp_project', - default=None, - help='Project name for the Cloud TPU-enabled project. If not specified, we ' - 'will attempt to automatically detect the GCE project from metadata.') -flags.DEFINE_string( - 'tpu_zone', - default=None, - help='GCE zone where the Cloud TPU is located in. If not specified, we ' - 'will attempt to automatically detect the GCE project from metadata.') -flags.DEFINE_string( - 'tpu_name', - default=None, - help='Name of the Cloud TPU for Cluster Resolvers.') - -flags.DEFINE_integer('num_shards', 8, 'Number of shards (TPU cores).') -flags.DEFINE_integer('iterations_per_loop', 100, - 'Number of iterations per TPU training loop.') -# For mode=train_and_eval, evaluation occurs after training is finished. -# Note: independently of steps_per_checkpoint, estimator will save the most -# recent checkpoint every 10 minutes by default for train_and_eval -flags.DEFINE_string('mode', 'train', - 'Mode to run: train, eval') -flags.DEFINE_integer('train_batch_size', None, 'Batch size for training. If ' - 'this is not provided, batch size is read from training ' - 'config.') -flags.DEFINE_integer('num_train_steps', None, 'Number of train steps.') -flags.DEFINE_boolean('eval_training_data', False, - 'If training data should be evaluated for this job.') -flags.DEFINE_integer('sample_1_of_n_eval_examples', 1, 'Will sample one of ' - 'every n eval input examples, where n is provided.') -flags.DEFINE_integer('sample_1_of_n_eval_on_train_examples', 5, 'Will sample ' - 'one of every n train input examples for evaluation, ' - 'where n is provided. This is only used if ' - '`eval_training_data` is True.') -flags.DEFINE_string( - 'model_dir', None, 'Path to output model directory ' - 'where event and checkpoint files will be written.') -flags.DEFINE_string('pipeline_config_path', None, 'Path to pipeline config ' - 'file.') -flags.DEFINE_integer( - 'max_eval_retries', 0, 'If running continuous eval, the maximum number of ' - 'retries upon encountering tf.errors.InvalidArgumentError. If negative, ' - 'will always retry the evaluation.' -) - -FLAGS = tf.flags.FLAGS - - -def main(unused_argv): - flags.mark_flag_as_required('model_dir') - flags.mark_flag_as_required('pipeline_config_path') - - tpu_cluster_resolver = ( - tf.distribute.cluster_resolver.TPUClusterResolver( - tpu=[FLAGS.tpu_name], zone=FLAGS.tpu_zone, project=FLAGS.gcp_project)) - tpu_grpc_url = tpu_cluster_resolver.get_master() - - config = tf_estimator.tpu.RunConfig( - master=tpu_grpc_url, - evaluation_master=tpu_grpc_url, - model_dir=FLAGS.model_dir, - tpu_config=tf_estimator.tpu.TPUConfig( - iterations_per_loop=FLAGS.iterations_per_loop, - num_shards=FLAGS.num_shards)) - - kwargs = {} - if FLAGS.train_batch_size: - kwargs['batch_size'] = FLAGS.train_batch_size - - train_and_eval_dict = model_lib.create_estimator_and_inputs( - run_config=config, - pipeline_config_path=FLAGS.pipeline_config_path, - train_steps=FLAGS.num_train_steps, - sample_1_of_n_eval_examples=FLAGS.sample_1_of_n_eval_examples, - sample_1_of_n_eval_on_train_examples=( - FLAGS.sample_1_of_n_eval_on_train_examples), - use_tpu_estimator=True, - use_tpu=FLAGS.use_tpu, - num_shards=FLAGS.num_shards, - save_final_config=FLAGS.mode == 'train', - **kwargs) - estimator = train_and_eval_dict['estimator'] - train_input_fn = train_and_eval_dict['train_input_fn'] - eval_input_fns = train_and_eval_dict['eval_input_fns'] - eval_on_train_input_fn = train_and_eval_dict['eval_on_train_input_fn'] - train_steps = train_and_eval_dict['train_steps'] - - if FLAGS.mode == 'train': - estimator.train(input_fn=train_input_fn, max_steps=train_steps) - - # Continuously evaluating. - if FLAGS.mode == 'eval': - if FLAGS.eval_training_data: - name = 'training_data' - input_fn = eval_on_train_input_fn - else: - name = 'validation_data' - # Currently only a single eval input is allowed. - input_fn = eval_input_fns[0] - model_lib.continuous_eval(estimator, FLAGS.model_dir, input_fn, train_steps, - name, FLAGS.max_eval_retries) - - -if __name__ == '__main__': - tf.app.run() diff --git a/research/object_detection/models/__init__.py b/research/object_detection/models/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/models/bidirectional_feature_pyramid_generators.py b/research/object_detection/models/bidirectional_feature_pyramid_generators.py deleted file mode 100644 index 77dd22e59f5..00000000000 --- a/research/object_detection/models/bidirectional_feature_pyramid_generators.py +++ /dev/null @@ -1,500 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Functions to generate bidirectional feature pyramids based on image features. - -Provides bidirectional feature pyramid network (BiFPN) generators that can be -used to build object detection feature extractors, as proposed by Tan et al. -See https://arxiv.org/abs/1911.09070 for more details. -""" -import collections -import functools -from six.moves import range -from six.moves import zip -import tensorflow as tf - -from object_detection.utils import bifpn_utils - - -def _create_bifpn_input_config(fpn_min_level, - fpn_max_level, - input_max_level, - level_scales=None): - """Creates a BiFPN input config for the input levels from a backbone network. - - Args: - fpn_min_level: the minimum pyramid level (highest feature map resolution) to - use in the BiFPN. - fpn_max_level: the maximum pyramid level (lowest feature map resolution) to - use in the BiFPN. - input_max_level: the maximum pyramid level that will be provided as input to - the BiFPN. Accordingly, the BiFPN will compute additional pyramid levels - from input_max_level, up to the desired fpn_max_level. - level_scales: a list of pyramid level scale factors. If 'None', each level's - scale is set to 2^level by default, which corresponds to each successive - feature map scaling by a factor of 2. - - Returns: - A list of dictionaries for each feature map expected as input to the BiFPN, - where each has entries for the feature map 'name' and 'scale'. - """ - if not level_scales: - level_scales = [2**i for i in range(fpn_min_level, fpn_max_level + 1)] - - bifpn_input_params = [] - for i in range(fpn_min_level, min(fpn_max_level, input_max_level) + 1): - bifpn_input_params.append({ - 'name': '0_up_lvl_{}'.format(i), - 'scale': level_scales[i - fpn_min_level] - }) - - return bifpn_input_params - - -def _get_bifpn_output_node_names(fpn_min_level, fpn_max_level, node_config): - """Returns a list of BiFPN output node names, given a BiFPN node config. - - Args: - fpn_min_level: the minimum pyramid level (highest feature map resolution) - used by the BiFPN. - fpn_max_level: the maximum pyramid level (lowest feature map resolution) - used by the BiFPN. - node_config: the BiFPN node_config, a list of dictionaries corresponding to - each node in the BiFPN computation graph, where each entry should have an - associated 'name'. - - Returns: - A list of strings corresponding to the names of the output BiFPN nodes. - """ - num_output_nodes = fpn_max_level - fpn_min_level + 1 - return [node['name'] for node in node_config[-num_output_nodes:]] - - -def _create_bifpn_node_config(bifpn_num_iterations, - bifpn_num_filters, - fpn_min_level, - fpn_max_level, - input_max_level, - bifpn_node_params=None, - level_scales=None, - use_native_resize_op=False): - """Creates a config specifying a bidirectional feature pyramid network. - - Args: - bifpn_num_iterations: the number of top-down bottom-up feature computations - to repeat in the BiFPN. - bifpn_num_filters: the number of filters (channels) for every feature map - used in the BiFPN. - fpn_min_level: the minimum pyramid level (highest feature map resolution) to - use in the BiFPN. - fpn_max_level: the maximum pyramid level (lowest feature map resolution) to - use in the BiFPN. - input_max_level: the maximum pyramid level that will be provided as input to - the BiFPN. Accordingly, the BiFPN will compute additional pyramid levels - from input_max_level, up to the desired fpn_max_level. - bifpn_node_params: If not 'None', a dictionary of additional default BiFPN - node parameters that will be applied to all BiFPN nodes. - level_scales: a list of pyramid level scale factors. If 'None', each level's - scale is set to 2^level by default, which corresponds to each successive - feature map scaling by a factor of 2. - use_native_resize_op: If true, will use - tf.compat.v1.image.resize_nearest_neighbor for unsampling. - - Returns: - A list of dictionaries used to define nodes in the BiFPN computation graph, - as proposed by EfficientDet, Tan et al (https://arxiv.org/abs/1911.09070). - Each node's entry has the corresponding keys: - name: String. The name of this node in the BiFPN. The node name follows - the format '{bifpn_iteration}_{dn|up}_lvl_{pyramid_level}', where 'dn' - or 'up' refers to whether the node is in the top-down or bottom-up - portion of a single BiFPN iteration. - scale: the scale factor for this node, by default 2^level. - inputs: A list of names of nodes which are inputs to this node. - num_channels: The number of channels for this node. - combine_method: String. Name of the method used to combine input - node feature maps, 'fast_attention' by default for nodes which have more - than one input. Otherwise, 'None' for nodes with only one input node. - input_op: A (partial) function which is called to construct the layers - that will be applied to this BiFPN node's inputs. This function is - called with the arguments: - input_op(name, input_scale, input_num_channels, output_scale, - output_num_channels, conv_hyperparams, is_training, - freeze_batchnorm) - post_combine_op: A (partial) function which is called to construct the - layers that will be applied to the result of the combine operation for - this BiFPN node. This function will be called with the arguments: - post_combine_op(name, conv_hyperparams, is_training, freeze_batchnorm) - If 'None', then no layers will be applied after the combine operation - for this node. - """ - if not level_scales: - level_scales = [2**i for i in range(fpn_min_level, fpn_max_level + 1)] - - default_node_params = { - 'num_channels': - bifpn_num_filters, - 'combine_method': - 'fast_attention', - 'input_op': - functools.partial( - _create_bifpn_resample_block, - downsample_method='max_pooling', - use_native_resize_op=use_native_resize_op), - 'post_combine_op': - functools.partial( - bifpn_utils.create_conv_block, - num_filters=bifpn_num_filters, - kernel_size=3, - strides=1, - padding='SAME', - use_separable=True, - apply_batchnorm=True, - apply_activation=True, - conv_bn_act_pattern=False), - } - if bifpn_node_params: - default_node_params.update(bifpn_node_params) - - bifpn_node_params = [] - # Create additional base pyramid levels not provided as input to the BiFPN. - # Note, combine_method and post_combine_op are set to None for additional - # base pyramid levels because they do not combine multiple input BiFPN nodes. - for i in range(input_max_level + 1, fpn_max_level + 1): - node_params = dict(default_node_params) - node_params.update({ - 'name': '0_up_lvl_{}'.format(i), - 'scale': level_scales[i - fpn_min_level], - 'inputs': ['0_up_lvl_{}'.format(i - 1)], - 'combine_method': None, - 'post_combine_op': None, - }) - bifpn_node_params.append(node_params) - - for i in range(bifpn_num_iterations): - # The first bottom-up feature pyramid (which includes the input pyramid - # levels from the backbone network and the additional base pyramid levels) - # is indexed at 0. So, the first top-down bottom-up pass of the BiFPN is - # indexed from 1, and repeated for bifpn_num_iterations iterations. - bifpn_i = i + 1 - - # Create top-down nodes. - for level_i in reversed(range(fpn_min_level, fpn_max_level)): - inputs = [] - # BiFPN nodes in the top-down pass receive input from the corresponding - # level from the previous BiFPN iteration's bottom-up pass, except for the - # bottom-most (min) level node, which is computed once in the initial - # bottom-up pass, and is afterwards only computed in each top-down pass. - if level_i > fpn_min_level or bifpn_i == 1: - inputs.append('{}_up_lvl_{}'.format(bifpn_i - 1, level_i)) - else: - inputs.append('{}_dn_lvl_{}'.format(bifpn_i - 1, level_i)) - inputs.append(bifpn_node_params[-1]['name']) - node_params = dict(default_node_params) - node_params.update({ - 'name': '{}_dn_lvl_{}'.format(bifpn_i, level_i), - 'scale': level_scales[level_i - fpn_min_level], - 'inputs': inputs - }) - bifpn_node_params.append(node_params) - - # Create bottom-up nodes. - for level_i in range(fpn_min_level + 1, fpn_max_level + 1): - # BiFPN nodes in the bottom-up pass receive input from the corresponding - # level from the preceding top-down pass, except for the top (max) level - # which does not have a corresponding node in the top-down pass. - inputs = ['{}_up_lvl_{}'.format(bifpn_i - 1, level_i)] - if level_i < fpn_max_level: - inputs.append('{}_dn_lvl_{}'.format(bifpn_i, level_i)) - inputs.append(bifpn_node_params[-1]['name']) - node_params = dict(default_node_params) - node_params.update({ - 'name': '{}_up_lvl_{}'.format(bifpn_i, level_i), - 'scale': level_scales[level_i - fpn_min_level], - 'inputs': inputs - }) - bifpn_node_params.append(node_params) - - return bifpn_node_params - - -def _create_bifpn_resample_block(name, - input_scale, - input_num_channels, - output_scale, - output_num_channels, - conv_hyperparams, - is_training, - freeze_batchnorm, - downsample_method=None, - use_native_resize_op=False, - maybe_apply_1x1_conv=True, - apply_1x1_pre_sampling=True, - apply_1x1_post_sampling=False): - """Creates resample block layers for input feature maps to BiFPN nodes. - - Args: - name: String. Name used for this block of layers. - input_scale: Scale factor of the input feature map. - input_num_channels: Number of channels in the input feature map. - output_scale: Scale factor of the output feature map. - output_num_channels: Number of channels in the output feature map. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - is_training: Indicates whether the feature generator is in training mode. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - downsample_method: String. Method to use when downsampling feature maps. - use_native_resize_op: Bool. Whether to use the native resize up when - upsampling feature maps. - maybe_apply_1x1_conv: Bool. If 'True', a 1x1 convolution will only be - applied if the input_num_channels differs from the output_num_channels. - apply_1x1_pre_sampling: Bool. Whether a 1x1 convolution will be applied to - the input feature map before the up/down-sampling operation. - apply_1x1_post_sampling: Bool. Whether a 1x1 convolution will be applied to - the input feature map after the up/down-sampling operation. - - Returns: - A list of layers which may be applied to the input feature maps in order to - compute feature maps with the specified scale and number of channels. - """ - # By default, 1x1 convolutions are only applied before sampling when the - # number of input and output channels differ. - if maybe_apply_1x1_conv and output_num_channels == input_num_channels: - apply_1x1_pre_sampling = False - apply_1x1_post_sampling = False - - apply_bn_for_resampling = True - layers = [] - if apply_1x1_pre_sampling: - layers.extend( - bifpn_utils.create_conv_block( - name=name + '1x1_pre_sample/', - num_filters=output_num_channels, - kernel_size=1, - strides=1, - padding='SAME', - use_separable=False, - apply_batchnorm=apply_bn_for_resampling, - apply_activation=False, - conv_hyperparams=conv_hyperparams, - is_training=is_training, - freeze_batchnorm=freeze_batchnorm)) - - layers.extend( - bifpn_utils.create_resample_feature_map_ops(input_scale, output_scale, - downsample_method, - use_native_resize_op, - conv_hyperparams, is_training, - freeze_batchnorm, name)) - - if apply_1x1_post_sampling: - layers.extend( - bifpn_utils.create_conv_block( - name=name + '1x1_post_sample/', - num_filters=output_num_channels, - kernel_size=1, - strides=1, - padding='SAME', - use_separable=False, - apply_batchnorm=apply_bn_for_resampling, - apply_activation=False, - conv_hyperparams=conv_hyperparams, - is_training=is_training, - freeze_batchnorm=freeze_batchnorm)) - - return layers - - -def _create_bifpn_combine_op(num_inputs, name, combine_method): - """Creates a BiFPN output config, a list of the output BiFPN node names. - - Args: - num_inputs: The number of inputs to this combine operation. - name: String. The name of this combine operation. - combine_method: String. The method used to combine input feature maps. - - Returns: - A function which may be called with a list of num_inputs feature maps - and which will return a single feature map. - """ - - combine_op = None - if num_inputs < 1: - raise ValueError('Expected at least 1 input for BiFPN combine.') - elif num_inputs == 1: - combine_op = lambda x: x[0] - else: - combine_op = bifpn_utils.BiFPNCombineLayer( - combine_method=combine_method, name=name) - return combine_op - - -class KerasBiFpnFeatureMaps(tf.keras.Model): - """Generates Keras based BiFPN feature maps from an input feature map pyramid. - - A Keras model that generates multi-scale feature maps for detection by - iteratively computing top-down and bottom-up feature pyramids, as in the - EfficientDet paper by Tan et al, see arxiv.org/abs/1911.09070 for details. - """ - - def __init__(self, - bifpn_num_iterations, - bifpn_num_filters, - fpn_min_level, - fpn_max_level, - input_max_level, - is_training, - conv_hyperparams, - freeze_batchnorm, - bifpn_node_params=None, - use_native_resize_op=False, - name=None): - """Constructor. - - Args: - bifpn_num_iterations: The number of top-down bottom-up iterations. - bifpn_num_filters: The number of filters (channels) to be used for all - feature maps in this BiFPN. - fpn_min_level: The minimum pyramid level (highest feature map resolution) - to use in the BiFPN. - fpn_max_level: The maximum pyramid level (lowest feature map resolution) - to use in the BiFPN. - input_max_level: The maximum pyramid level that will be provided as input - to the BiFPN. Accordingly, the BiFPN will compute any additional pyramid - levels from input_max_level up to the desired fpn_max_level, with each - successivel level downsampling by a scale factor of 2 by default. - is_training: Indicates whether the feature generator is in training mode. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - bifpn_node_params: An optional dictionary that may be used to specify - default parameters for BiFPN nodes, without the need to provide a custom - bifpn_node_config. For example, if '{ combine_method: 'sum' }', then all - BiFPN nodes will combine input feature maps by summation, rather than - by the default fast attention method. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for unsampling. - name: A string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(KerasBiFpnFeatureMaps, self).__init__(name=name) - bifpn_node_config = _create_bifpn_node_config( - bifpn_num_iterations, - bifpn_num_filters, - fpn_min_level, - fpn_max_level, - input_max_level, - bifpn_node_params, - use_native_resize_op=use_native_resize_op) - bifpn_input_config = _create_bifpn_input_config(fpn_min_level, - fpn_max_level, - input_max_level) - bifpn_output_node_names = _get_bifpn_output_node_names( - fpn_min_level, fpn_max_level, bifpn_node_config) - - self.bifpn_node_config = bifpn_node_config - self.bifpn_output_node_names = bifpn_output_node_names - self.node_input_blocks = [] - self.node_combine_op = [] - self.node_post_combine_block = [] - - all_node_params = bifpn_input_config - all_node_names = [node['name'] for node in all_node_params] - for node_config in bifpn_node_config: - # Maybe transform and/or resample input feature maps. - input_blocks = [] - for input_name in node_config['inputs']: - if input_name not in all_node_names: - raise ValueError( - 'Input feature map ({}) does not exist:'.format(input_name)) - input_index = all_node_names.index(input_name) - input_params = all_node_params[input_index] - input_block = node_config['input_op']( - name='{}/input_{}/'.format(node_config['name'], input_name), - input_scale=input_params['scale'], - input_num_channels=input_params.get('num_channels', None), - output_scale=node_config['scale'], - output_num_channels=node_config['num_channels'], - conv_hyperparams=conv_hyperparams, - is_training=is_training, - freeze_batchnorm=freeze_batchnorm) - input_blocks.append((input_index, input_block)) - - # Combine input feature maps. - combine_op = _create_bifpn_combine_op( - num_inputs=len(input_blocks), - name=(node_config['name'] + '/combine'), - combine_method=node_config['combine_method']) - - # Post-combine layers. - post_combine_block = [] - if node_config['post_combine_op']: - post_combine_block.extend(node_config['post_combine_op']( - name=node_config['name'] + '/post_combine/', - conv_hyperparams=conv_hyperparams, - is_training=is_training, - freeze_batchnorm=freeze_batchnorm)) - - self.node_input_blocks.append(input_blocks) - self.node_combine_op.append(combine_op) - self.node_post_combine_block.append(post_combine_block) - all_node_params.append(node_config) - all_node_names.append(node_config['name']) - - def call(self, feature_pyramid): - """Compute BiFPN feature maps from input feature pyramid. - - Executed when calling the `.__call__` method on input. - - Args: - feature_pyramid: list of tuples of (tensor_name, image_feature_tensor). - - Returns: - feature_maps: an OrderedDict mapping keys (feature map names) to - tensors where each tensor has shape [batch, height_i, width_i, depth_i]. - """ - feature_maps = [el[1] for el in feature_pyramid] - output_feature_maps = [None for node in self.bifpn_output_node_names] - - for index, node in enumerate(self.bifpn_node_config): - node_scope = 'node_{:02d}'.format(index) - with tf.name_scope(node_scope): - # Apply layer blocks to this node's input feature maps. - input_block_results = [] - for input_index, input_block in self.node_input_blocks[index]: - block_result = feature_maps[input_index] - for layer in input_block: - block_result = layer(block_result) - input_block_results.append(block_result) - - # Combine the resulting feature maps. - node_result = self.node_combine_op[index](input_block_results) - - # Apply post-combine layer block if applicable. - for layer in self.node_post_combine_block[index]: - node_result = layer(node_result) - - feature_maps.append(node_result) - - if node['name'] in self.bifpn_output_node_names: - index = self.bifpn_output_node_names.index(node['name']) - output_feature_maps[index] = node_result - - return collections.OrderedDict( - zip(self.bifpn_output_node_names, output_feature_maps)) diff --git a/research/object_detection/models/bidirectional_feature_pyramid_generators_tf2_test.py b/research/object_detection/models/bidirectional_feature_pyramid_generators_tf2_test.py deleted file mode 100644 index cbc815cc446..00000000000 --- a/research/object_detection/models/bidirectional_feature_pyramid_generators_tf2_test.py +++ /dev/null @@ -1,167 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for bidirectional feature pyramid generators.""" -import unittest -from absl.testing import parameterized - -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import hyperparams_builder -from object_detection.models import bidirectional_feature_pyramid_generators as bifpn_generators -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import test_utils -from object_detection.utils import tf_version - - -@parameterized.parameters({'bifpn_num_iterations': 2}, - {'bifpn_num_iterations': 8}) -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class BiFPNFeaturePyramidGeneratorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - force_use_bias: true - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_get_expected_feature_map_shapes(self, bifpn_num_iterations): - with test_utils.GraphContextOrNone() as g: - image_features = [ - ('block3', tf.random_uniform([4, 16, 16, 256], dtype=tf.float32)), - ('block4', tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block5', tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)) - ] - bifpn_generator = bifpn_generators.KerasBiFpnFeatureMaps( - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=128, - fpn_min_level=3, - fpn_max_level=7, - input_max_level=5, - is_training=True, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False) - def graph_fn(): - feature_maps = bifpn_generator(image_features) - return feature_maps - - expected_feature_map_shapes = { - '{}_dn_lvl_3'.format(bifpn_num_iterations): (4, 16, 16, 128), - '{}_up_lvl_4'.format(bifpn_num_iterations): (4, 8, 8, 128), - '{}_up_lvl_5'.format(bifpn_num_iterations): (4, 4, 4, 128), - '{}_up_lvl_6'.format(bifpn_num_iterations): (4, 2, 2, 128), - '{}_up_lvl_7'.format(bifpn_num_iterations): (4, 1, 1, 128)} - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_variable_names(self, bifpn_num_iterations): - with test_utils.GraphContextOrNone() as g: - image_features = [ - ('block3', tf.random_uniform([4, 16, 16, 256], dtype=tf.float32)), - ('block4', tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block5', tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)) - ] - bifpn_generator = bifpn_generators.KerasBiFpnFeatureMaps( - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=128, - fpn_min_level=3, - fpn_max_level=7, - input_max_level=5, - is_training=True, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - name='bifpn') - def graph_fn(): - return bifpn_generator(image_features) - - self.execute(graph_fn, [], g) - expected_variables = [ - 'bifpn/node_00/0_up_lvl_6/input_0_up_lvl_5/1x1_pre_sample/conv/bias', - 'bifpn/node_00/0_up_lvl_6/input_0_up_lvl_5/1x1_pre_sample/conv/kernel', - 'bifpn/node_03/1_dn_lvl_5/input_0_up_lvl_5/1x1_pre_sample/conv/bias', - 'bifpn/node_03/1_dn_lvl_5/input_0_up_lvl_5/1x1_pre_sample/conv/kernel', - 'bifpn/node_04/1_dn_lvl_4/input_0_up_lvl_4/1x1_pre_sample/conv/bias', - 'bifpn/node_04/1_dn_lvl_4/input_0_up_lvl_4/1x1_pre_sample/conv/kernel', - 'bifpn/node_05/1_dn_lvl_3/input_0_up_lvl_3/1x1_pre_sample/conv/bias', - 'bifpn/node_05/1_dn_lvl_3/input_0_up_lvl_3/1x1_pre_sample/conv/kernel', - 'bifpn/node_06/1_up_lvl_4/input_0_up_lvl_4/1x1_pre_sample/conv/bias', - 'bifpn/node_06/1_up_lvl_4/input_0_up_lvl_4/1x1_pre_sample/conv/kernel', - 'bifpn/node_07/1_up_lvl_5/input_0_up_lvl_5/1x1_pre_sample/conv/bias', - 'bifpn/node_07/1_up_lvl_5/input_0_up_lvl_5/1x1_pre_sample/conv/kernel'] - expected_node_variable_patterns = [ - ['bifpn/node_{:02}/{}_dn_lvl_6/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_dn_lvl_6/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_dn_lvl_6/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_dn_lvl_6/post_combine/separable_conv/pointwise_kernel'], - ['bifpn/node_{:02}/{}_dn_lvl_5/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_dn_lvl_5/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_dn_lvl_5/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_dn_lvl_5/post_combine/separable_conv/pointwise_kernel'], - ['bifpn/node_{:02}/{}_dn_lvl_4/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_dn_lvl_4/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_dn_lvl_4/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_dn_lvl_4/post_combine/separable_conv/pointwise_kernel'], - ['bifpn/node_{:02}/{}_dn_lvl_3/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_dn_lvl_3/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_dn_lvl_3/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_dn_lvl_3/post_combine/separable_conv/pointwise_kernel'], - ['bifpn/node_{:02}/{}_up_lvl_4/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_up_lvl_4/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_up_lvl_4/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_up_lvl_4/post_combine/separable_conv/pointwise_kernel'], - ['bifpn/node_{:02}/{}_up_lvl_5/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_up_lvl_5/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_up_lvl_5/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_up_lvl_5/post_combine/separable_conv/pointwise_kernel'], - ['bifpn/node_{:02}/{}_up_lvl_6/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_up_lvl_6/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_up_lvl_6/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_up_lvl_6/post_combine/separable_conv/pointwise_kernel'], - ['bifpn/node_{:02}/{}_up_lvl_7/combine/bifpn_combine_weights', - 'bifpn/node_{:02}/{}_up_lvl_7/post_combine/separable_conv/bias', - 'bifpn/node_{:02}/{}_up_lvl_7/post_combine/separable_conv/depthwise_kernel', - 'bifpn/node_{:02}/{}_up_lvl_7/post_combine/separable_conv/pointwise_kernel']] - - node_i = 2 - for iter_i in range(1, bifpn_num_iterations+1): - for node_variable_patterns in expected_node_variable_patterns: - for pattern in node_variable_patterns: - expected_variables.append(pattern.format(node_i, iter_i)) - node_i += 1 - - expected_variables = set(expected_variables) - actual_variable_set = set( - [var.name.split(':')[0] for var in bifpn_generator.variables]) - self.assertSetEqual(expected_variables, actual_variable_set) - -# TODO(aom): Tests for create_bifpn_combine_op. - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/center_net_hourglass_feature_extractor.py b/research/object_detection/models/center_net_hourglass_feature_extractor.py deleted file mode 100644 index 19867acf582..00000000000 --- a/research/object_detection/models/center_net_hourglass_feature_extractor.py +++ /dev/null @@ -1,115 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Hourglass[1] feature extractor for CenterNet[2] meta architecture. - -[1]: https://arxiv.org/abs/1603.06937 -[2]: https://arxiv.org/abs/1904.07850 -""" - -from object_detection.meta_architectures import center_net_meta_arch -from object_detection.models.keras_models import hourglass_network - - -class CenterNetHourglassFeatureExtractor( - center_net_meta_arch.CenterNetFeatureExtractor): - """The hourglass feature extractor for CenterNet. - - This class is a thin wrapper around the HourglassFeatureExtractor class - along with some preprocessing methods inherited from the base class. - """ - - def __init__(self, hourglass_net, channel_means=(0., 0., 0.), - channel_stds=(1., 1., 1.), bgr_ordering=False): - """Intializes the feature extractor. - - Args: - hourglass_net: The underlying hourglass network to use. - channel_means: A tuple of floats, denoting the mean of each channel - which will be subtracted from it. - channel_stds: A tuple of floats, denoting the standard deviation of each - channel. Each channel will be divided by its standard deviation value. - bgr_ordering: bool, if set will change the channel ordering to be in the - [blue, red, green] order. - """ - - super(CenterNetHourglassFeatureExtractor, self).__init__( - channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - self._network = hourglass_net - - def call(self, inputs): - return self._network(inputs) - - @property - def out_stride(self): - """The stride in the output image of the network.""" - return 4 - - @property - def num_feature_outputs(self): - """Ther number of feature outputs returned by the feature extractor.""" - return self._network.num_hourglasses - - -def hourglass_10(channel_means, channel_stds, bgr_ordering, **kwargs): - """The Hourglass-10 backbone for CenterNet.""" - del kwargs - - network = hourglass_network.hourglass_10(num_channels=32) - return CenterNetHourglassFeatureExtractor( - network, channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - - -def hourglass_20(channel_means, channel_stds, bgr_ordering, **kwargs): - """The Hourglass-20 backbone for CenterNet.""" - del kwargs - - network = hourglass_network.hourglass_20(num_channels=48) - return CenterNetHourglassFeatureExtractor( - network, channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - - -def hourglass_32(channel_means, channel_stds, bgr_ordering, **kwargs): - """The Hourglass-32 backbone for CenterNet.""" - del kwargs - - network = hourglass_network.hourglass_32(num_channels=48) - return CenterNetHourglassFeatureExtractor( - network, channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - - -def hourglass_52(channel_means, channel_stds, bgr_ordering, **kwargs): - """The Hourglass-52 backbone for CenterNet.""" - del kwargs - - network = hourglass_network.hourglass_52(num_channels=64) - return CenterNetHourglassFeatureExtractor( - network, channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - - -def hourglass_104(channel_means, channel_stds, bgr_ordering, **kwargs): - """The Hourglass-104 backbone for CenterNet.""" - del kwargs - - # TODO(vighneshb): update hourglass_104 signature to match with other - # hourglass networks. - network = hourglass_network.hourglass_104() - return CenterNetHourglassFeatureExtractor( - network, channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) diff --git a/research/object_detection/models/center_net_hourglass_feature_extractor_tf2_test.py b/research/object_detection/models/center_net_hourglass_feature_extractor_tf2_test.py deleted file mode 100644 index 31c26c5ab9e..00000000000 --- a/research/object_detection/models/center_net_hourglass_feature_extractor_tf2_test.py +++ /dev/null @@ -1,45 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Testing hourglass feature extractor for CenterNet.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import center_net_hourglass_feature_extractor as hourglass -from object_detection.models.keras_models import hourglass_network -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetHourglassFeatureExtractorTest(test_case.TestCase): - - def test_center_net_hourglass_feature_extractor(self): - - net = hourglass_network.HourglassNetwork( - num_stages=4, blocks_per_stage=[2, 3, 4, 5, 6], - input_channel_dims=4, channel_dims_per_stage=[6, 8, 10, 12, 14], - num_hourglasses=2) - - model = hourglass.CenterNetHourglassFeatureExtractor(net) - def graph_fn(): - return model(tf.zeros((2, 64, 64, 3), dtype=np.float32)) - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs[0].shape, (2, 16, 16, 6)) - self.assertEqual(outputs[1].shape, (2, 16, 16, 6)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/center_net_mobilenet_v2_feature_extractor.py b/research/object_detection/models/center_net_mobilenet_v2_feature_extractor.py deleted file mode 100644 index 323b6383284..00000000000 --- a/research/object_detection/models/center_net_mobilenet_v2_feature_extractor.py +++ /dev/null @@ -1,119 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""MobileNet V2[1] feature extractor for CenterNet[2] meta architecture. - -[1]: https://arxiv.org/abs/1801.04381 -[2]: https://arxiv.org/abs/1904.07850 -""" - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import center_net_meta_arch -from object_detection.models.keras_models import mobilenet_v2 as mobilenetv2 - - -class CenterNetMobileNetV2FeatureExtractor( - center_net_meta_arch.CenterNetFeatureExtractor): - """The MobileNet V2 feature extractor for CenterNet.""" - - def __init__(self, - mobilenet_v2_net, - channel_means=(0., 0., 0.), - channel_stds=(1., 1., 1.), - bgr_ordering=False): - """Intializes the feature extractor. - - Args: - mobilenet_v2_net: The underlying mobilenet_v2 network to use. - channel_means: A tuple of floats, denoting the mean of each channel - which will be subtracted from it. - channel_stds: A tuple of floats, denoting the standard deviation of each - channel. Each channel will be divided by its standard deviation value. - bgr_ordering: bool, if set will change the channel ordering to be in the - [blue, red, green] order. - """ - - super(CenterNetMobileNetV2FeatureExtractor, self).__init__( - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - self._network = mobilenet_v2_net - - output = self._network(self._network.input) - - # MobileNet by itself transforms a 224x224x3 volume into a 7x7x1280, which - # leads to a stride of 32. We perform upsampling to get it to a target - # stride of 4. - for num_filters in [256, 128, 64]: - # 1. We use a simple convolution instead of a deformable convolution - conv = tf.keras.layers.Conv2D( - filters=num_filters, kernel_size=1, strides=1, padding='same') - output = conv(output) - output = tf.keras.layers.BatchNormalization()(output) - output = tf.keras.layers.ReLU()(output) - - # 2. We use the default initialization for the convolution layers - # instead of initializing it to do bilinear upsampling. - conv_transpose = tf.keras.layers.Conv2DTranspose( - filters=num_filters, kernel_size=3, strides=2, padding='same') - output = conv_transpose(output) - output = tf.keras.layers.BatchNormalization()(output) - output = tf.keras.layers.ReLU()(output) - - self._network = tf.keras.models.Model( - inputs=self._network.input, outputs=output) - - def preprocess(self, resized_inputs): - resized_inputs = super(CenterNetMobileNetV2FeatureExtractor, - self).preprocess(resized_inputs) - return tf.keras.applications.mobilenet_v2.preprocess_input(resized_inputs) - - def load_feature_extractor_weights(self, path): - self._network.load_weights(path) - - def call(self, inputs): - return [self._network(inputs)] - - @property - def out_stride(self): - """The stride in the output image of the network.""" - return 4 - - @property - def num_feature_outputs(self): - """The number of feature outputs returned by the feature extractor.""" - return 1 - - @property - def classification_backbone(self): - return self._network - - -def mobilenet_v2(channel_means, channel_stds, bgr_ordering, - depth_multiplier=1.0, **kwargs): - """The MobileNetV2 backbone for CenterNet.""" - del kwargs - - # We set 'is_training' to True for now. - network = mobilenetv2.mobilenet_v2( - batchnorm_training=True, - alpha=depth_multiplier, - include_top=False, - weights='imagenet' if depth_multiplier == 1.0 else None) - return CenterNetMobileNetV2FeatureExtractor( - network, - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering) diff --git a/research/object_detection/models/center_net_mobilenet_v2_feature_extractor_tf2_test.py b/research/object_detection/models/center_net_mobilenet_v2_feature_extractor_tf2_test.py deleted file mode 100644 index 5211701138d..00000000000 --- a/research/object_detection/models/center_net_mobilenet_v2_feature_extractor_tf2_test.py +++ /dev/null @@ -1,46 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Testing mobilenet_v2 feature extractor for CenterNet.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import center_net_mobilenet_v2_feature_extractor -from object_detection.models.keras_models import mobilenet_v2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMobileNetV2FeatureExtractorTest(test_case.TestCase): - - def test_center_net_mobilenet_v2_feature_extractor(self): - - net = mobilenet_v2.mobilenet_v2(True, include_top=False) - - model = center_net_mobilenet_v2_feature_extractor.CenterNetMobileNetV2FeatureExtractor( - net) - - def graph_fn(): - img = np.zeros((8, 224, 224, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs.shape, (8, 56, 56, 64)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor.py b/research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor.py deleted file mode 100644 index 6df26eeffd7..00000000000 --- a/research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor.py +++ /dev/null @@ -1,163 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""MobileNet V2[1] + FPN[2] feature extractor for CenterNet[3] meta architecture. - -[1]: https://arxiv.org/abs/1801.04381 -[2]: https://arxiv.org/abs/1612.03144. -[3]: https://arxiv.org/abs/1904.07850 -""" - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import center_net_meta_arch -from object_detection.models.keras_models import mobilenet_v2 as mobilenetv2 - - -_MOBILENET_V2_FPN_SKIP_LAYERS = [ - 'block_2_add', 'block_5_add', 'block_9_add', 'out_relu' -] - - -class CenterNetMobileNetV2FPNFeatureExtractor( - center_net_meta_arch.CenterNetFeatureExtractor): - """The MobileNet V2 with FPN skip layers feature extractor for CenterNet.""" - - def __init__(self, - mobilenet_v2_net, - channel_means=(0., 0., 0.), - channel_stds=(1., 1., 1.), - bgr_ordering=False, - use_separable_conv=False, - upsampling_interpolation='nearest'): - """Intializes the feature extractor. - - Args: - mobilenet_v2_net: The underlying mobilenet_v2 network to use. - channel_means: A tuple of floats, denoting the mean of each channel - which will be subtracted from it. - channel_stds: A tuple of floats, denoting the standard deviation of each - channel. Each channel will be divided by its standard deviation value. - bgr_ordering: bool, if set will change the channel ordering to be in the - [blue, red, green] order. - use_separable_conv: If set to True, all convolutional layers in the FPN - network will be replaced by separable convolutions. - upsampling_interpolation: A string (one of 'nearest' or 'bilinear') - indicating which interpolation method to use for the upsampling ops in - the FPN. - """ - - super(CenterNetMobileNetV2FPNFeatureExtractor, self).__init__( - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - self._base_model = mobilenet_v2_net - - output = self._base_model(self._base_model.input) - - # Add pyramid feature network on every layer that has stride 2. - skip_outputs = [ - self._base_model.get_layer(skip_layer_name).output - for skip_layer_name in _MOBILENET_V2_FPN_SKIP_LAYERS - ] - self._fpn_model = tf.keras.models.Model( - inputs=self._base_model.input, outputs=skip_outputs) - fpn_outputs = self._fpn_model(self._base_model.input) - - # Construct the top-down feature maps -- we start with an output of - # 7x7x1280, which we continually upsample, apply a residual on and merge. - # This results in a 56x56x24 output volume. - top_layer = fpn_outputs[-1] - # Use normal convolutional layer since the kernel_size is 1. - residual_op = tf.keras.layers.Conv2D( - filters=64, kernel_size=1, strides=1, padding='same') - top_down = residual_op(top_layer) - - num_filters_list = [64, 32, 24] - for i, num_filters in enumerate(num_filters_list): - level_ind = len(num_filters_list) - 1 - i - # Upsample. - upsample_op = tf.keras.layers.UpSampling2D( - 2, interpolation=upsampling_interpolation) - top_down = upsample_op(top_down) - - # Residual (skip-connection) from bottom-up pathway. - # Use normal convolutional layer since the kernel_size is 1. - residual_op = tf.keras.layers.Conv2D( - filters=num_filters, kernel_size=1, strides=1, padding='same') - residual = residual_op(fpn_outputs[level_ind]) - - # Merge. - top_down = top_down + residual - next_num_filters = num_filters_list[i + 1] if i + 1 <= 2 else 24 - if use_separable_conv: - conv = tf.keras.layers.SeparableConv2D( - filters=next_num_filters, kernel_size=3, strides=1, padding='same') - else: - conv = tf.keras.layers.Conv2D( - filters=next_num_filters, kernel_size=3, strides=1, padding='same') - top_down = conv(top_down) - top_down = tf.keras.layers.BatchNormalization()(top_down) - top_down = tf.keras.layers.ReLU()(top_down) - - output = top_down - - self._feature_extractor_model = tf.keras.models.Model( - inputs=self._base_model.input, outputs=output) - - def preprocess(self, resized_inputs): - resized_inputs = super(CenterNetMobileNetV2FPNFeatureExtractor, - self).preprocess(resized_inputs) - return tf.keras.applications.mobilenet_v2.preprocess_input(resized_inputs) - - def load_feature_extractor_weights(self, path): - self._base_model.load_weights(path) - - @property - def classification_backbone(self): - return self._base_model - - def call(self, inputs): - return [self._feature_extractor_model(inputs)] - - @property - def out_stride(self): - """The stride in the output image of the network.""" - return 4 - - @property - def num_feature_outputs(self): - """The number of feature outputs returned by the feature extractor.""" - return 1 - - -def mobilenet_v2_fpn(channel_means, channel_stds, bgr_ordering, - use_separable_conv=False, depth_multiplier=1.0, - upsampling_interpolation='nearest', **kwargs): - """The MobileNetV2+FPN backbone for CenterNet.""" - del kwargs - - # Set to batchnorm_training to True for now. - network = mobilenetv2.mobilenet_v2( - batchnorm_training=True, - alpha=depth_multiplier, - include_top=False, - weights='imagenet' if depth_multiplier == 1.0 else None) - return CenterNetMobileNetV2FPNFeatureExtractor( - network, - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering, - use_separable_conv=use_separable_conv, - upsampling_interpolation=upsampling_interpolation) diff --git a/research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor_tf2_test.py b/research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor_tf2_test.py deleted file mode 100644 index c623510bfbc..00000000000 --- a/research/object_detection/models/center_net_mobilenet_v2_fpn_feature_extractor_tf2_test.py +++ /dev/null @@ -1,132 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Testing mobilenet_v2+FPN feature extractor for CenterNet.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import center_net_mobilenet_v2_fpn_feature_extractor -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetMobileNetV2FPNFeatureExtractorTest(test_case.TestCase): - - def test_center_net_mobilenet_v2_fpn_feature_extractor(self): - - channel_means = (0., 0., 0.) - channel_stds = (1., 1., 1.) - bgr_ordering = False - model = ( - center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn( - channel_means, channel_stds, bgr_ordering, - use_separable_conv=False)) - - def graph_fn(): - img = np.zeros((8, 224, 224, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs.shape, (8, 56, 56, 24)) - - # Pull out the FPN network. - output = model.get_layer('model_1') - for layer in output.layers: - # All convolution layers should be normal 2D convolutions. - if 'conv' in layer.name: - self.assertIsInstance(layer, tf.keras.layers.Conv2D) - - def test_center_net_mobilenet_v2_fpn_feature_extractor_sep_conv(self): - - channel_means = (0., 0., 0.) - channel_stds = (1., 1., 1.) - bgr_ordering = False - model = ( - center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn( - channel_means, channel_stds, bgr_ordering, use_separable_conv=True)) - - def graph_fn(): - img = np.zeros((8, 224, 224, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs.shape, (8, 56, 56, 24)) - # Pull out the FPN network. - backbone = model.get_layer('model') - first_conv = backbone.get_layer('Conv1') - self.assertEqual(32, first_conv.filters) - - # Pull out the FPN network. - output = model.get_layer('model_1') - for layer in output.layers: - # Convolution layers with kernel size not equal to (1, 1) should be - # separable 2D convolutions. - if 'conv' in layer.name and layer.kernel_size != (1, 1): - self.assertIsInstance(layer, tf.keras.layers.SeparableConv2D) - - def test_center_net_mobilenet_v2_fpn_feature_extractor_depth_multiplier(self): - - channel_means = (0., 0., 0.) - channel_stds = (1., 1., 1.) - bgr_ordering = False - model = ( - center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn( - channel_means, channel_stds, bgr_ordering, use_separable_conv=True, - depth_multiplier=2.0)) - - def graph_fn(): - img = np.zeros((8, 224, 224, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs.shape, (8, 56, 56, 24)) - # Pull out the FPN network. - backbone = model.get_layer('model') - first_conv = backbone.get_layer('Conv1') - # Note that the first layer typically has 32 filters, but this model has - # a depth multiplier of 2. - self.assertEqual(64, first_conv.filters) - - def test_center_net_mobilenet_v2_fpn_feature_extractor_interpolation(self): - - channel_means = (0., 0., 0.) - channel_stds = (1., 1., 1.) - bgr_ordering = False - model = ( - center_net_mobilenet_v2_fpn_feature_extractor.mobilenet_v2_fpn( - channel_means, channel_stds, bgr_ordering, use_separable_conv=True, - upsampling_interpolation='bilinear')) - - def graph_fn(): - img = np.zeros((8, 224, 224, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs.shape, (8, 56, 56, 24)) - - # Verify the upsampling layers in the FPN use 'bilinear' interpolation. - fpn = model.get_layer('model_1') - for layer in fpn.layers: - if 'up_sampling2d' in layer.name: - self.assertEqual('bilinear', layer.interpolation) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/center_net_resnet_feature_extractor.py b/research/object_detection/models/center_net_resnet_feature_extractor.py deleted file mode 100644 index 1745a158cfe..00000000000 --- a/research/object_detection/models/center_net_resnet_feature_extractor.py +++ /dev/null @@ -1,153 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Resnetv2 based feature extractors for CenterNet[1] meta architecture. - -[1]: https://arxiv.org/abs/1904.07850 -""" - - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures.center_net_meta_arch import CenterNetFeatureExtractor - - -class CenterNetResnetFeatureExtractor(CenterNetFeatureExtractor): - """Resnet v2 base feature extractor for the CenterNet model.""" - - def __init__(self, resnet_type, channel_means=(0., 0., 0.), - channel_stds=(1., 1., 1.), bgr_ordering=False): - """Initializes the feature extractor with a specific ResNet architecture. - - Args: - resnet_type: A string specifying which kind of ResNet to use. Currently - only `resnet_v2_50` and `resnet_v2_101` are supported. - channel_means: A tuple of floats, denoting the mean of each channel - which will be subtracted from it. - channel_stds: A tuple of floats, denoting the standard deviation of each - channel. Each channel will be divided by its standard deviation value. - bgr_ordering: bool, if set will change the channel ordering to be in the - [blue, red, green] order. - - """ - - super(CenterNetResnetFeatureExtractor, self).__init__( - channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - if resnet_type == 'resnet_v2_101': - self._base_model = tf.keras.applications.ResNet101V2(weights=None, - include_top=False) - output_layer = 'conv5_block3_out' - elif resnet_type == 'resnet_v2_50': - self._base_model = tf.keras.applications.ResNet50V2(weights=None, - include_top=False) - output_layer = 'conv5_block3_out' - else: - raise ValueError('Unknown Resnet Model {}'.format(resnet_type)) - output_layer = self._base_model.get_layer(output_layer) - - self._resnet_model = tf.keras.models.Model(inputs=self._base_model.input, - outputs=output_layer.output) - resnet_output = self._resnet_model(self._base_model.input) - - for num_filters in [256, 128, 64]: - # TODO(vighneshb) This section has a few differences from the paper - # Figure out how much of a performance impact they have. - - # 1. We use a simple convolution instead of a deformable convolution - conv = tf.keras.layers.Conv2D(filters=num_filters, kernel_size=3, - strides=1, padding='same') - resnet_output = conv(resnet_output) - resnet_output = tf.keras.layers.BatchNormalization()(resnet_output) - resnet_output = tf.keras.layers.ReLU()(resnet_output) - - # 2. We use the default initialization for the convolution layers - # instead of initializing it to do bilinear upsampling. - conv_transpose = tf.keras.layers.Conv2DTranspose(filters=num_filters, - kernel_size=3, strides=2, - padding='same') - resnet_output = conv_transpose(resnet_output) - resnet_output = tf.keras.layers.BatchNormalization()(resnet_output) - resnet_output = tf.keras.layers.ReLU()(resnet_output) - - self._feature_extractor_model = tf.keras.models.Model( - inputs=self._base_model.input, outputs=resnet_output) - - def preprocess(self, resized_inputs): - """Preprocess input images for the ResNet model. - - This scales images in the range [0, 255] to the range [-1, 1] - - Args: - resized_inputs: a [batch, height, width, channels] float32 tensor. - - Returns: - outputs: a [batch, height, width, channels] float32 tensor. - - """ - resized_inputs = super(CenterNetResnetFeatureExtractor, self).preprocess( - resized_inputs) - return tf.keras.applications.resnet_v2.preprocess_input(resized_inputs) - - def load_feature_extractor_weights(self, path): - self._base_model.load_weights(path) - - def call(self, inputs): - """Returns image features extracted by the backbone. - - Args: - inputs: An image tensor of shape [batch_size, input_height, - input_width, 3] - - Returns: - features_list: A list of length 1 containing a tensor of shape - [batch_size, input_height // 4, input_width // 4, 64] containing - the features extracted by the ResNet. - """ - return [self._feature_extractor_model(inputs)] - - @property - def num_feature_outputs(self): - return 1 - - @property - def out_stride(self): - return 4 - - @property - def classification_backbone(self): - return self._base_model - - -def resnet_v2_101(channel_means, channel_stds, bgr_ordering, **kwargs): - """The ResNet v2 101 feature extractor.""" - del kwargs - - return CenterNetResnetFeatureExtractor( - resnet_type='resnet_v2_101', - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering - ) - - -def resnet_v2_50(channel_means, channel_stds, bgr_ordering, **kwargs): - """The ResNet v2 50 feature extractor.""" - del kwargs - - return CenterNetResnetFeatureExtractor( - resnet_type='resnet_v2_50', - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering) diff --git a/research/object_detection/models/center_net_resnet_feature_extractor_tf2_test.py b/research/object_detection/models/center_net_resnet_feature_extractor_tf2_test.py deleted file mode 100644 index d8f9b22a746..00000000000 --- a/research/object_detection/models/center_net_resnet_feature_extractor_tf2_test.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Testing ResNet v2 models for the CenterNet meta architecture.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import center_net_resnet_feature_extractor -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetResnetFeatureExtractorTest(test_case.TestCase): - - def test_output_size(self): - """Verify that shape of features returned by the backbone is correct.""" - - model = center_net_resnet_feature_extractor.\ - CenterNetResnetFeatureExtractor('resnet_v2_101') - def graph_fn(): - img = np.zeros((8, 512, 512, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs.shape, (8, 128, 128, 64)) - - def test_output_size_resnet50(self): - """Verify that shape of features returned by the backbone is correct.""" - - model = center_net_resnet_feature_extractor.\ - CenterNetResnetFeatureExtractor('resnet_v2_50') - def graph_fn(): - img = np.zeros((8, 224, 224, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - outputs = self.execute(graph_fn, []) - self.assertEqual(outputs.shape, (8, 56, 56, 64)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/center_net_resnet_v1_fpn_feature_extractor.py b/research/object_detection/models/center_net_resnet_v1_fpn_feature_extractor.py deleted file mode 100644 index 14c66f33831..00000000000 --- a/research/object_detection/models/center_net_resnet_v1_fpn_feature_extractor.py +++ /dev/null @@ -1,212 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Resnetv1 FPN [1] based feature extractors for CenterNet[2] meta architecture. - - -[1]: https://arxiv.org/abs/1612.03144. -[2]: https://arxiv.org/abs/1904.07850. -""" -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures.center_net_meta_arch import CenterNetFeatureExtractor -from object_detection.models.keras_models import resnet_v1 - - -_RESNET_MODEL_OUTPUT_LAYERS = { - 'resnet_v1_18': ['conv2_block2_out', 'conv3_block2_out', - 'conv4_block2_out', 'conv5_block2_out'], - 'resnet_v1_34': ['conv2_block3_out', 'conv3_block4_out', - 'conv4_block6_out', 'conv5_block3_out'], - 'resnet_v1_50': ['conv2_block3_out', 'conv3_block4_out', - 'conv4_block6_out', 'conv5_block3_out'], - 'resnet_v1_101': ['conv2_block3_out', 'conv3_block4_out', - 'conv4_block23_out', 'conv5_block3_out'], -} - - -class CenterNetResnetV1FpnFeatureExtractor(CenterNetFeatureExtractor): - """Resnet v1 FPN base feature extractor for the CenterNet model. - - This feature extractor uses residual skip connections and nearest neighbor - upsampling to produce an output feature map of stride 4, which has precise - localization information along with strong semantic information from the top - of the net. This design does not exactly follow the original FPN design, - specifically: - - Since only one output map is necessary for heatmap prediction (stride 4 - output), the top-down feature maps can have different numbers of channels. - Specifically, the top down feature maps have the following sizes: - [h/4, w/4, 64], [h/8, w/8, 128], [h/16, w/16, 256], [h/32, w/32, 256]. - - No additional coarse features are used after conv5_x. - """ - - def __init__(self, resnet_type, channel_means=(0., 0., 0.), - channel_stds=(1., 1., 1.), bgr_ordering=False): - """Initializes the feature extractor with a specific ResNet architecture. - - Args: - resnet_type: A string specifying which kind of ResNet to use. Currently - only `resnet_v1_50` and `resnet_v1_101` are supported. - channel_means: A tuple of floats, denoting the mean of each channel - which will be subtracted from it. - channel_stds: A tuple of floats, denoting the standard deviation of each - channel. Each channel will be divided by its standard deviation value. - bgr_ordering: bool, if set will change the channel ordering to be in the - [blue, red, green] order. - - """ - - super(CenterNetResnetV1FpnFeatureExtractor, self).__init__( - channel_means=channel_means, channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - if resnet_type == 'resnet_v1_50': - self._base_model = tf.keras.applications.ResNet50(weights=None, - include_top=False) - elif resnet_type == 'resnet_v1_101': - self._base_model = tf.keras.applications.ResNet101(weights=None, - include_top=False) - elif resnet_type == 'resnet_v1_18': - self._base_model = resnet_v1.resnet_v1_18(weights=None, include_top=False) - elif resnet_type == 'resnet_v1_34': - self._base_model = resnet_v1.resnet_v1_34(weights=None, include_top=False) - else: - raise ValueError('Unknown Resnet Model {}'.format(resnet_type)) - output_layers = _RESNET_MODEL_OUTPUT_LAYERS[resnet_type] - outputs = [self._base_model.get_layer(output_layer_name).output - for output_layer_name in output_layers] - - self._resnet_model = tf.keras.models.Model(inputs=self._base_model.input, - outputs=outputs) - resnet_outputs = self._resnet_model(self._base_model.input) - - # Construct the top-down feature maps. - top_layer = resnet_outputs[-1] - residual_op = tf.keras.layers.Conv2D(filters=256, kernel_size=1, - strides=1, padding='same') - top_down = residual_op(top_layer) - - num_filters_list = [256, 128, 64] - for i, num_filters in enumerate(num_filters_list): - level_ind = 2 - i - # Upsample. - upsample_op = tf.keras.layers.UpSampling2D(2, interpolation='nearest') - top_down = upsample_op(top_down) - - # Residual (skip-connection) from bottom-up pathway. - residual_op = tf.keras.layers.Conv2D(filters=num_filters, kernel_size=1, - strides=1, padding='same') - residual = residual_op(resnet_outputs[level_ind]) - - # Merge. - top_down = top_down + residual - next_num_filters = num_filters_list[i+1] if i + 1 <= 2 else 64 - conv = tf.keras.layers.Conv2D(filters=next_num_filters, - kernel_size=3, strides=1, padding='same') - top_down = conv(top_down) - top_down = tf.keras.layers.BatchNormalization()(top_down) - top_down = tf.keras.layers.ReLU()(top_down) - - self._feature_extractor_model = tf.keras.models.Model( - inputs=self._base_model.input, outputs=top_down) - - def preprocess(self, resized_inputs): - """Preprocess input images for the ResNet model. - - This scales images in the range [0, 255] to the range [-1, 1] - - Args: - resized_inputs: a [batch, height, width, channels] float32 tensor. - - Returns: - outputs: a [batch, height, width, channels] float32 tensor. - - """ - resized_inputs = super( - CenterNetResnetV1FpnFeatureExtractor, self).preprocess(resized_inputs) - return tf.keras.applications.resnet.preprocess_input(resized_inputs) - - def load_feature_extractor_weights(self, path): - self._base_model.load_weights(path) - - def call(self, inputs): - """Returns image features extracted by the backbone. - - Args: - inputs: An image tensor of shape [batch_size, input_height, - input_width, 3] - - Returns: - features_list: A list of length 1 containing a tensor of shape - [batch_size, input_height // 4, input_width // 4, 64] containing - the features extracted by the ResNet. - """ - return [self._feature_extractor_model(inputs)] - - @property - def num_feature_outputs(self): - return 1 - - @property - def out_stride(self): - return 4 - - @property - def classification_backbone(self): - return self._base_model - - -def resnet_v1_101_fpn(channel_means, channel_stds, bgr_ordering, **kwargs): - """The ResNet v1 101 FPN feature extractor.""" - del kwargs - - return CenterNetResnetV1FpnFeatureExtractor( - resnet_type='resnet_v1_101', - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering - ) - - -def resnet_v1_50_fpn(channel_means, channel_stds, bgr_ordering, **kwargs): - """The ResNet v1 50 FPN feature extractor.""" - del kwargs - - return CenterNetResnetV1FpnFeatureExtractor( - resnet_type='resnet_v1_50', - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering) - - -def resnet_v1_34_fpn(channel_means, channel_stds, bgr_ordering, **kwargs): - """The ResNet v1 34 FPN feature extractor.""" - del kwargs - - return CenterNetResnetV1FpnFeatureExtractor( - resnet_type='resnet_v1_34', - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering - ) - - -def resnet_v1_18_fpn(channel_means, channel_stds, bgr_ordering, **kwargs): - """The ResNet v1 18 FPN feature extractor.""" - del kwargs - - return CenterNetResnetV1FpnFeatureExtractor( - resnet_type='resnet_v1_18', - channel_means=channel_means, - channel_stds=channel_stds, - bgr_ordering=bgr_ordering) diff --git a/research/object_detection/models/center_net_resnet_v1_fpn_feature_extractor_tf2_test.py b/research/object_detection/models/center_net_resnet_v1_fpn_feature_extractor_tf2_test.py deleted file mode 100644 index 2508e52f793..00000000000 --- a/research/object_detection/models/center_net_resnet_v1_fpn_feature_extractor_tf2_test.py +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Testing ResNet v1 FPN models for the CenterNet meta architecture.""" -import unittest -from absl.testing import parameterized - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import center_net_resnet_v1_fpn_feature_extractor -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class CenterNetResnetV1FpnFeatureExtractorTest(test_case.TestCase, - parameterized.TestCase): - - @parameterized.parameters( - {'resnet_type': 'resnet_v1_50'}, - {'resnet_type': 'resnet_v1_101'}, - {'resnet_type': 'resnet_v1_18'}, - {'resnet_type': 'resnet_v1_34'}, - ) - def test_correct_output_size(self, resnet_type): - """Verify that shape of features returned by the backbone is correct.""" - - model = center_net_resnet_v1_fpn_feature_extractor.\ - CenterNetResnetV1FpnFeatureExtractor(resnet_type) - def graph_fn(): - img = np.zeros((8, 512, 512, 3), dtype=np.float32) - processed_img = model.preprocess(img) - return model(processed_img) - - self.assertEqual(self.execute(graph_fn, []).shape, (8, 128, 128, 64)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/embedded_ssd_mobilenet_v1_feature_extractor.py b/research/object_detection/models/embedded_ssd_mobilenet_v1_feature_extractor.py deleted file mode 100644 index 311c71e848b..00000000000 --- a/research/object_detection/models/embedded_ssd_mobilenet_v1_feature_extractor.py +++ /dev/null @@ -1,164 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Embedded-friendly SSDFeatureExtractor for MobilenetV1 features.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from nets import mobilenet_v1 - - -class EmbeddedSSDMobileNetV1FeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """Embedded-friendly SSD Feature Extractor using MobilenetV1 features. - - This feature extractor is similar to SSD MobileNetV1 feature extractor, and - it fixes input resolution to be 256x256, reduces the number of feature maps - used for box prediction and ensures convolution kernel to be no larger - than input tensor in spatial dimensions. - - This feature extractor requires support of the following ops if used in - embedded devices: - - Conv - - DepthwiseConv - - Relu6 - - All conv/depthwiseconv use SAME padding, and no additional spatial padding is - needed. - """ - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False): - """MobileNetV1 Feature Extractor for Embedded-friendly SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. For EmbeddedSSD it must be set to 1. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - - Raises: - ValueError: upon invalid `pad_to_multiple` values. - """ - if pad_to_multiple != 1: - raise ValueError('Embedded-specific SSD only supports `pad_to_multiple` ' - 'of 1.') - - super(EmbeddedSSDMobileNetV1FeatureExtractor, self).__init__( - is_training, depth_multiplier, min_depth, pad_to_multiple, - conv_hyperparams_fn, reuse_weights, use_explicit_padding, use_depthwise, - override_base_feature_extractor_hyperparams) - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - - Raises: - ValueError: if image height or width are not 256 pixels. - """ - image_shape = preprocessed_inputs.get_shape() - image_shape.assert_has_rank(4) - image_height = image_shape[1].value - image_width = image_shape[2].value - - if image_height is None or image_width is None: - shape_assert = tf.Assert( - tf.logical_and(tf.equal(tf.shape(preprocessed_inputs)[1], 256), - tf.equal(tf.shape(preprocessed_inputs)[2], 256)), - ['image size must be 256 in both height and width.']) - with tf.control_dependencies([shape_assert]): - preprocessed_inputs = tf.identity(preprocessed_inputs) - elif image_height != 256 or image_width != 256: - raise ValueError('image size must be = 256 in both height and width;' - ' image dim = %d,%d' % (image_height, image_width)) - - feature_map_layout = { - 'from_layer': [ - 'Conv2d_11_pointwise', 'Conv2d_13_pointwise', '', '', '' - ], - 'layer_depth': [-1, -1, 512, 256, 256], - 'conv_kernel_size': [-1, -1, 3, 3, 2], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - - with tf.variable_scope('MobilenetV1', - reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v1.mobilenet_v1_arg_scope(is_training=None)): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams - else context_manager.IdentityContextManager()): - _, image_features = mobilenet_v1.mobilenet_v1_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='Conv2d_13_pointwise', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) diff --git a/research/object_detection/models/embedded_ssd_mobilenet_v1_feature_extractor_tf1_test.py b/research/object_detection/models/embedded_ssd_mobilenet_v1_feature_extractor_tf1_test.py deleted file mode 100644 index 4a27e8c8d64..00000000000 --- a/research/object_detection/models/embedded_ssd_mobilenet_v1_feature_extractor_tf1_test.py +++ /dev/null @@ -1,132 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for embedded_ssd_mobilenet_v1_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import embedded_ssd_mobilenet_v1_feature_extractor -from object_detection.models import ssd_feature_extractor_test -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class EmbeddedSSDMobileNetV1FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - is_training=True): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - is_training: whether the network is in training mode. - - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - return (embedded_ssd_mobilenet_v1_feature_extractor. - EmbeddedSSDMobileNetV1FeatureExtractor( - is_training, depth_multiplier, min_depth, pad_to_multiple, - self.conv_hyperparams_fn, - override_base_feature_extractor_hyperparams=True)) - - def test_extract_features_returns_correct_shapes_256(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 16, 16, 512), (2, 8, 8, 1024), - (2, 4, 4, 512), (2, 2, 2, 256), - (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_dynamic_inputs(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 16, 16, 512), (2, 8, 8, 1024), - (2, 4, 4, 512), (2, 2, 2, 256), - (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth(self): - image_height = 256 - image_width = 256 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 16, 16, 32), (2, 8, 8, 32), (2, 4, 4, 32), - (2, 2, 2, 32), (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple_of_1( - self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 16, 16, 512), (2, 8, 8, 1024), - (2, 4, 4, 512), (2, 2, 2, 256), - (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_raises_error_with_pad_to_multiple_not_1(self): - depth_multiplier = 1.0 - pad_to_multiple = 2 - with self.assertRaises(ValueError): - _ = self._create_feature_extractor(depth_multiplier, pad_to_multiple) - - def test_extract_features_raises_error_with_invalid_image_size(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple) - - def test_preprocess_returns_correct_value_range(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'MobilenetV1' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor.py b/research/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor.py deleted file mode 100644 index a94aa207b3a..00000000000 --- a/research/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor.py +++ /dev/null @@ -1,212 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Inception Resnet v2 Faster R-CNN implementation. - -See "Inception-v4, Inception-ResNet and the Impact of Residual Connections on -Learning" by Szegedy et al. (https://arxiv.org/abs/1602.07261) -as well as -"Speed/accuracy trade-offs for modern convolutional object detectors" by -Huang et al. (https://arxiv.org/abs/1611.10012) -""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.utils import variables_helper -from nets import inception_resnet_v2 - - -class FasterRCNNInceptionResnetV2FeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Faster R-CNN with Inception Resnet v2 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16. - """ - if first_stage_features_stride != 8 and first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 8 or 16.') - super(FasterRCNNInceptionResnetV2FeatureExtractor, self).__init__( - is_training, first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay) - - def preprocess(self, resized_inputs): - """Faster R-CNN with Inception Resnet v2 preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: A [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: A [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features. - - Extracts features using the first half of the Inception Resnet v2 network. - We construct the network in `align_feature_maps=True` mode, which means - that all VALID paddings in the network are changed to SAME padding so that - the feature maps are aligned. - - Args: - preprocessed_inputs: A [batch, height, width, channels] float32 tensor - representing a batch of images. - scope: A scope name. - - Returns: - rpn_feature_map: A tensor with shape [batch, height, width, depth] - Raises: - InvalidArgumentError: If the spatial size of `preprocessed_inputs` - (height or width) is less than 33. - ValueError: If the created network is missing the required activation. - """ - if len(preprocessed_inputs.get_shape().as_list()) != 4: - raise ValueError('`preprocessed_inputs` must be 4 dimensional, got a ' - 'tensor of shape %s' % preprocessed_inputs.get_shape()) - - with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope( - weight_decay=self._weight_decay)): - # Forces is_training to False to disable batch norm update. - with slim.arg_scope([slim.batch_norm], - is_training=self._train_batch_norm): - with tf.variable_scope('InceptionResnetV2', - reuse=self._reuse_weights) as scope: - return inception_resnet_v2.inception_resnet_v2_base( - preprocessed_inputs, final_endpoint='PreAuxLogits', - scope=scope, output_stride=self._first_stage_features_stride, - align_feature_maps=True) - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features. - - This function reconstructs the "second half" of the Inception ResNet v2 - network after the part defined in `_extract_proposal_features`. - - Args: - proposal_feature_maps: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - scope: A scope name. - - Returns: - proposal_classifier_features: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - with tf.variable_scope('InceptionResnetV2', reuse=self._reuse_weights): - with slim.arg_scope(inception_resnet_v2.inception_resnet_v2_arg_scope( - weight_decay=self._weight_decay)): - # Forces is_training to False to disable batch norm update. - with slim.arg_scope([slim.batch_norm], - is_training=self._train_batch_norm): - with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], - stride=1, padding='SAME'): - with tf.variable_scope('Mixed_7a'): - with tf.variable_scope('Branch_0'): - tower_conv = slim.conv2d(proposal_feature_maps, - 256, 1, scope='Conv2d_0a_1x1') - tower_conv_1 = slim.conv2d( - tower_conv, 384, 3, stride=2, - padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_1'): - tower_conv1 = slim.conv2d( - proposal_feature_maps, 256, 1, scope='Conv2d_0a_1x1') - tower_conv1_1 = slim.conv2d( - tower_conv1, 288, 3, stride=2, - padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_2'): - tower_conv2 = slim.conv2d( - proposal_feature_maps, 256, 1, scope='Conv2d_0a_1x1') - tower_conv2_1 = slim.conv2d(tower_conv2, 288, 3, - scope='Conv2d_0b_3x3') - tower_conv2_2 = slim.conv2d( - tower_conv2_1, 320, 3, stride=2, - padding='VALID', scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_3'): - tower_pool = slim.max_pool2d( - proposal_feature_maps, 3, stride=2, padding='VALID', - scope='MaxPool_1a_3x3') - net = tf.concat( - [tower_conv_1, tower_conv1_1, tower_conv2_2, tower_pool], 3) - net = slim.repeat(net, 9, inception_resnet_v2.block8, scale=0.20) - net = inception_resnet_v2.block8(net, activation_fn=None) - proposal_classifier_features = slim.conv2d( - net, 1536, 1, scope='Conv2d_7b_1x1') - return proposal_classifier_features - - def restore_from_classification_checkpoint_fn( - self, - first_stage_feature_extractor_scope, - second_stage_feature_extractor_scope): - """Returns a map of variables to load from a foreign checkpoint. - - Note that this overrides the default implementation in - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor which does not work for - InceptionResnetV2 checkpoints. - - TODO(jonathanhuang,rathodv): revisit whether it's possible to force the - `Repeat` namescope as created in `_extract_box_classifier_features` to - start counting at 2 (e.g. `Repeat_2`) so that the default restore_fn can - be used. - - Args: - first_stage_feature_extractor_scope: A scope name for the first stage - feature extractor. - second_stage_feature_extractor_scope: A scope name for the second stage - feature extractor. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - """ - - variables_to_restore = {} - for variable in variables_helper.get_global_variables_safely(): - if variable.op.name.startswith( - first_stage_feature_extractor_scope): - var_name = variable.op.name.replace( - first_stage_feature_extractor_scope + '/', '') - variables_to_restore[var_name] = variable - if variable.op.name.startswith( - second_stage_feature_extractor_scope): - var_name = variable.op.name.replace( - second_stage_feature_extractor_scope - + '/InceptionResnetV2/Repeat', 'InceptionResnetV2/Repeat_2') - var_name = var_name.replace( - second_stage_feature_extractor_scope + '/', '') - variables_to_restore[var_name] = variable - return variables_to_restore diff --git a/research/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor_tf1_test.py b/research/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor_tf1_test.py deleted file mode 100644 index 2505fbfb3ad..00000000000 --- a/research/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor_tf1_test.py +++ /dev/null @@ -1,111 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for models.faster_rcnn_inception_resnet_v2_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class FasterRcnnInceptionResnetV2FeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, first_stage_features_stride): - return frcnn_inc_res.FasterRCNNInceptionResnetV2FeatureExtractor( - is_training=False, - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 299, 299, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 19, 19, 1088]) - - def test_extract_proposal_features_stride_eight(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=8) - preprocessed_inputs = tf.random_uniform( - [1, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 28, 28, 1088]) - - def test_extract_proposal_features_half_size_input(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 7, 7, 1088]) - - def test_extract_proposal_features_dies_on_invalid_stride(self): - with self.assertRaises(ValueError): - self._build_feature_extractor(first_stage_features_stride=99) - - def test_extract_proposal_features_dies_with_incorrect_rank_inputs(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [224, 224, 3], maxval=255, dtype=tf.float32) - with self.assertRaises(ValueError): - feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - proposal_feature_maps = tf.random_uniform( - [2, 17, 17, 1088], maxval=255, dtype=tf.float32) - proposal_classifier_features = ( - feature_extractor.extract_box_classifier_features( - proposal_feature_maps, scope='TestScope')) - features_shape = tf.shape(proposal_classifier_features) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [2, 8, 8, 1536]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_inception_resnet_v2_keras_feature_extractor.py b/research/object_detection/models/faster_rcnn_inception_resnet_v2_keras_feature_extractor.py deleted file mode 100644 index f185aa01dd3..00000000000 --- a/research/object_detection/models/faster_rcnn_inception_resnet_v2_keras_feature_extractor.py +++ /dev/null @@ -1,159 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Inception Resnet v2 Faster R-CNN implementation in Keras. - -See "Inception-v4, Inception-ResNet and the Impact of Residual Connections on -Learning" by Szegedy et al. (https://arxiv.org/abs/1602.07261) -as well as -"Speed/accuracy trade-offs for modern convolutional object detectors" by -Huang et al. (https://arxiv.org/abs/1611.10012) -""" - -# Skip pylint for this file because it times out -# pylint: skip-file - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.models.keras_models import inception_resnet_v2 -from object_detection.utils import model_util -from object_detection.utils import variables_helper - - -class FasterRCNNInceptionResnetV2KerasFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor): - """Faster R-CNN with Inception Resnet v2 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - weight_decay: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16. - """ - if first_stage_features_stride != 8 and first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 8 or 16.') - super(FasterRCNNInceptionResnetV2KerasFeatureExtractor, self).__init__( - is_training, first_stage_features_stride, batch_norm_trainable, - weight_decay) - self._variable_dict = {} - self.classification_backbone = None - - def preprocess(self, resized_inputs): - """Faster R-CNN with Inception Resnet v2 preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: A [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: A [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def get_proposal_feature_extractor_model(self, name=None): - """Returns a model that extracts first stage RPN features. - - Extracts features using the first half of the Inception Resnet v2 network. - We construct the network in `align_feature_maps=True` mode, which means - that all VALID paddings in the network are changed to SAME padding so that - the feature maps are aligned. - - Args: - name: A scope name to construct all variables within. - - Returns: - A Keras model that takes preprocessed_inputs: - A [batch, height, width, channels] float32 tensor - representing a batch of images. - - And returns rpn_feature_map: - A tensor with shape [batch, height, width, depth] - """ - if not self.classification_backbone: - self.classification_backbone = inception_resnet_v2.inception_resnet_v2( - self._train_batch_norm, - output_stride=self._first_stage_features_stride, - align_feature_maps=True, - weight_decay=self._weight_decay, - weights=None, - include_top=False) - with tf.name_scope(name): - with tf.name_scope('InceptionResnetV2'): - proposal_features = self.classification_backbone.get_layer( - name='block17_20_ac').output - keras_model = tf.keras.Model( - inputs=self.classification_backbone.inputs, - outputs=proposal_features) - for variable in keras_model.variables: - self._variable_dict[variable.name[:-2]] = variable - return keras_model - - def get_box_classifier_feature_extractor_model(self, name=None): - """Returns a model that extracts second stage box classifier features. - - This function reconstructs the "second half" of the Inception ResNet v2 - network after the part defined in `get_proposal_feature_extractor_model`. - - Args: - name: A scope name to construct all variables within. - - Returns: - A Keras model that takes proposal_feature_maps: - A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - And returns proposal_classifier_features: - A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - if not self.classification_backbone: - self.classification_backbone = inception_resnet_v2.inception_resnet_v2( - self._train_batch_norm, - output_stride=self._first_stage_features_stride, - align_feature_maps=True, - weight_decay=self._weight_decay, - weights=None, - include_top=False) - with tf.name_scope(name): - with tf.name_scope('InceptionResnetV2'): - proposal_feature_maps = self.classification_backbone.get_layer( - name='block17_20_ac').output - proposal_classifier_features = self.classification_backbone.get_layer( - name='conv_7b_ac').output - - keras_model = model_util.extract_submodel( - model=self.classification_backbone, - inputs=proposal_feature_maps, - outputs=proposal_classifier_features) - for variable in keras_model.variables: - self._variable_dict[variable.name[:-2]] = variable - return keras_model - diff --git a/research/object_detection/models/faster_rcnn_inception_resnet_v2_keras_feature_extractor_tf2_test.py b/research/object_detection/models/faster_rcnn_inception_resnet_v2_keras_feature_extractor_tf2_test.py deleted file mode 100644 index 20bb50ef836..00000000000 --- a/research/object_detection/models/faster_rcnn_inception_resnet_v2_keras_feature_extractor_tf2_test.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for models.faster_rcnn_inception_resnet_v2_keras_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_inception_resnet_v2_keras_feature_extractor as frcnn_inc_res -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class FasterRcnnInceptionResnetV2KerasFeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, first_stage_features_stride): - return frcnn_inc_res.FasterRCNNInceptionResnetV2KerasFeatureExtractor( - is_training=False, - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=False, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 299, 299, 3], maxval=255, dtype=tf.float32) - rpn_feature_map = feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - features_shape = tf.shape(rpn_feature_map) - - self.assertAllEqual(features_shape.numpy(), [1, 19, 19, 1088]) - - def test_extract_proposal_features_stride_eight(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=8) - preprocessed_inputs = tf.random_uniform( - [1, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map = feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - features_shape = tf.shape(rpn_feature_map) - - self.assertAllEqual(features_shape.numpy(), [1, 28, 28, 1088]) - - def test_extract_proposal_features_half_size_input(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map = feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - features_shape = tf.shape(rpn_feature_map) - self.assertAllEqual(features_shape.numpy(), [1, 7, 7, 1088]) - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - proposal_feature_maps = tf.random_uniform( - [2, 17, 17, 1088], maxval=255, dtype=tf.float32) - model = feature_extractor.get_box_classifier_feature_extractor_model( - name='TestScope') - proposal_classifier_features = ( - model(proposal_feature_maps)) - features_shape = tf.shape(proposal_classifier_features) - self.assertAllEqual(features_shape.numpy(), [2, 9, 9, 1536]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_inception_v2_feature_extractor.py b/research/object_detection/models/faster_rcnn_inception_v2_feature_extractor.py deleted file mode 100644 index 549ad6bb2f4..00000000000 --- a/research/object_detection/models/faster_rcnn_inception_v2_feature_extractor.py +++ /dev/null @@ -1,253 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Inception V2 Faster R-CNN implementation. - -See "Rethinking the Inception Architecture for Computer Vision" -https://arxiv.org/abs/1512.00567 -""" -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from nets import inception_v2 - - -def _batch_norm_arg_scope(list_ops, - use_batch_norm=True, - batch_norm_decay=0.9997, - batch_norm_epsilon=0.001, - batch_norm_scale=False, - train_batch_norm=False): - """Slim arg scope for InceptionV2 batch norm.""" - if use_batch_norm: - batch_norm_params = { - 'is_training': train_batch_norm, - 'scale': batch_norm_scale, - 'decay': batch_norm_decay, - 'epsilon': batch_norm_epsilon - } - normalizer_fn = slim.batch_norm - else: - normalizer_fn = None - batch_norm_params = None - - return slim.arg_scope(list_ops, - normalizer_fn=normalizer_fn, - normalizer_params=batch_norm_params) - - -class FasterRCNNInceptionV2FeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Faster R-CNN Inception V2 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0, - depth_multiplier=1.0, - min_depth=16): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16. - """ - if first_stage_features_stride != 8 and first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 8 or 16.') - self._depth_multiplier = depth_multiplier - self._min_depth = min_depth - super(FasterRCNNInceptionV2FeatureExtractor, self).__init__( - is_training, first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay) - - def preprocess(self, resized_inputs): - """Faster R-CNN Inception V2 preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features. - - Args: - preprocessed_inputs: A [batch, height, width, channels] float32 tensor - representing a batch of images. - scope: A scope name. - - Returns: - rpn_feature_map: A tensor with shape [batch, height, width, depth] - activations: A dictionary mapping feature extractor tensor names to - tensors - - Raises: - InvalidArgumentError: If the spatial size of `preprocessed_inputs` - (height or width) is less than 33. - ValueError: If the created network is missing the required activation. - """ - - preprocessed_inputs.get_shape().assert_has_rank(4) - shape_assert = tf.Assert( - tf.logical_and(tf.greater_equal(tf.shape(preprocessed_inputs)[1], 33), - tf.greater_equal(tf.shape(preprocessed_inputs)[2], 33)), - ['image size must at least be 33 in both height and width.']) - - with tf.control_dependencies([shape_assert]): - with tf.variable_scope('InceptionV2', - reuse=self._reuse_weights) as scope: - with _batch_norm_arg_scope([slim.conv2d, slim.separable_conv2d], - batch_norm_scale=True, - train_batch_norm=self._train_batch_norm): - _, activations = inception_v2.inception_v2_base( - preprocessed_inputs, - final_endpoint='Mixed_4e', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - scope=scope) - - return activations['Mixed_4e'], activations - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features. - - Args: - proposal_feature_maps: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - scope: A scope name (unused). - - Returns: - proposal_classifier_features: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - net = proposal_feature_maps - - depth = lambda d: max(int(d * self._depth_multiplier), self._min_depth) - trunc_normal = lambda stddev: tf.truncated_normal_initializer(0.0, stddev) - - data_format = 'NHWC' - concat_dim = 3 if data_format == 'NHWC' else 1 - - with tf.variable_scope('InceptionV2', reuse=self._reuse_weights): - with slim.arg_scope( - [slim.conv2d, slim.max_pool2d, slim.avg_pool2d], - stride=1, - padding='SAME', - data_format=data_format): - with _batch_norm_arg_scope([slim.conv2d, slim.separable_conv2d], - batch_norm_scale=True, - train_batch_norm=self._train_batch_norm): - - with tf.variable_scope('Mixed_5a'): - with tf.variable_scope('Branch_0'): - branch_0 = slim.conv2d( - net, depth(128), [1, 1], - weights_initializer=trunc_normal(0.09), - scope='Conv2d_0a_1x1') - branch_0 = slim.conv2d(branch_0, depth(192), [3, 3], stride=2, - scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_1'): - branch_1 = slim.conv2d( - net, depth(192), [1, 1], - weights_initializer=trunc_normal(0.09), - scope='Conv2d_0a_1x1') - branch_1 = slim.conv2d(branch_1, depth(256), [3, 3], - scope='Conv2d_0b_3x3') - branch_1 = slim.conv2d(branch_1, depth(256), [3, 3], stride=2, - scope='Conv2d_1a_3x3') - with tf.variable_scope('Branch_2'): - branch_2 = slim.max_pool2d(net, [3, 3], stride=2, - scope='MaxPool_1a_3x3') - net = tf.concat([branch_0, branch_1, branch_2], concat_dim) - - with tf.variable_scope('Mixed_5b'): - with tf.variable_scope('Branch_0'): - branch_0 = slim.conv2d(net, depth(352), [1, 1], - scope='Conv2d_0a_1x1') - with tf.variable_scope('Branch_1'): - branch_1 = slim.conv2d( - net, depth(192), [1, 1], - weights_initializer=trunc_normal(0.09), - scope='Conv2d_0a_1x1') - branch_1 = slim.conv2d(branch_1, depth(320), [3, 3], - scope='Conv2d_0b_3x3') - with tf.variable_scope('Branch_2'): - branch_2 = slim.conv2d( - net, depth(160), [1, 1], - weights_initializer=trunc_normal(0.09), - scope='Conv2d_0a_1x1') - branch_2 = slim.conv2d(branch_2, depth(224), [3, 3], - scope='Conv2d_0b_3x3') - branch_2 = slim.conv2d(branch_2, depth(224), [3, 3], - scope='Conv2d_0c_3x3') - with tf.variable_scope('Branch_3'): - branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3') - branch_3 = slim.conv2d( - branch_3, depth(128), [1, 1], - weights_initializer=trunc_normal(0.1), - scope='Conv2d_0b_1x1') - net = tf.concat([branch_0, branch_1, branch_2, branch_3], - concat_dim) - - with tf.variable_scope('Mixed_5c'): - with tf.variable_scope('Branch_0'): - branch_0 = slim.conv2d(net, depth(352), [1, 1], - scope='Conv2d_0a_1x1') - with tf.variable_scope('Branch_1'): - branch_1 = slim.conv2d( - net, depth(192), [1, 1], - weights_initializer=trunc_normal(0.09), - scope='Conv2d_0a_1x1') - branch_1 = slim.conv2d(branch_1, depth(320), [3, 3], - scope='Conv2d_0b_3x3') - with tf.variable_scope('Branch_2'): - branch_2 = slim.conv2d( - net, depth(192), [1, 1], - weights_initializer=trunc_normal(0.09), - scope='Conv2d_0a_1x1') - branch_2 = slim.conv2d(branch_2, depth(224), [3, 3], - scope='Conv2d_0b_3x3') - branch_2 = slim.conv2d(branch_2, depth(224), [3, 3], - scope='Conv2d_0c_3x3') - with tf.variable_scope('Branch_3'): - branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3') - branch_3 = slim.conv2d( - branch_3, depth(128), [1, 1], - weights_initializer=trunc_normal(0.1), - scope='Conv2d_0b_1x1') - proposal_classifier_features = tf.concat( - [branch_0, branch_1, branch_2, branch_3], concat_dim) - - return proposal_classifier_features diff --git a/research/object_detection/models/faster_rcnn_inception_v2_feature_extractor_tf1_test.py b/research/object_detection/models/faster_rcnn_inception_v2_feature_extractor_tf1_test.py deleted file mode 100644 index f5d01145f29..00000000000 --- a/research/object_detection/models/faster_rcnn_inception_v2_feature_extractor_tf1_test.py +++ /dev/null @@ -1,128 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for faster_rcnn_inception_v2_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_inception_v2_feature_extractor as faster_rcnn_inception_v2 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class FasterRcnnInceptionV2FeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, first_stage_features_stride): - return faster_rcnn_inception_v2.FasterRCNNInceptionV2FeatureExtractor( - is_training=False, - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [4, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [4, 14, 14, 576]) - - def test_extract_proposal_features_stride_eight(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=8) - preprocessed_inputs = tf.random_uniform( - [4, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [4, 14, 14, 576]) - - def test_extract_proposal_features_half_size_input(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 7, 7, 576]) - - def test_extract_proposal_features_dies_on_invalid_stride(self): - with self.assertRaises(ValueError): - self._build_feature_extractor(first_stage_features_stride=99) - - def test_extract_proposal_features_dies_on_very_small_images(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.placeholder(tf.float32, (4, None, None, 3)) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - with self.assertRaises(tf.errors.InvalidArgumentError): - sess.run( - features_shape, - feed_dict={preprocessed_inputs: np.random.rand(4, 32, 32, 3)}) - - def test_extract_proposal_features_dies_with_incorrect_rank_inputs(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [224, 224, 3], maxval=255, dtype=tf.float32) - with self.assertRaises(ValueError): - feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - proposal_feature_maps = tf.random_uniform( - [3, 14, 14, 576], maxval=255, dtype=tf.float32) - proposal_classifier_features = ( - feature_extractor.extract_box_classifier_features( - proposal_feature_maps, scope='TestScope')) - features_shape = tf.shape(proposal_classifier_features) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [3, 7, 7, 1024]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_mobilenet_v1_feature_extractor.py b/research/object_detection/models/faster_rcnn_mobilenet_v1_feature_extractor.py deleted file mode 100644 index aa37848bb84..00000000000 --- a/research/object_detection/models/faster_rcnn_mobilenet_v1_feature_extractor.py +++ /dev/null @@ -1,193 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Mobilenet v1 Faster R-CNN implementation.""" -import numpy as np - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.utils import shape_utils -from nets import mobilenet_v1 - - -def _get_mobilenet_conv_no_last_stride_defs(conv_depth_ratio_in_percentage): - if conv_depth_ratio_in_percentage not in [25, 50, 75, 100]: - raise ValueError( - 'Only the following ratio percentages are supported: 25, 50, 75, 100') - conv_depth_ratio_in_percentage = float(conv_depth_ratio_in_percentage) / 100.0 - channels = np.array([ - 32, 64, 128, 128, 256, 256, 512, 512, 512, 512, 512, 512, 1024, 1024 - ], dtype=np.float32) - channels = (channels * conv_depth_ratio_in_percentage).astype(np.int32) - return [ - mobilenet_v1.Conv(kernel=[3, 3], stride=2, depth=channels[0]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[1]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=2, depth=channels[2]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[3]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=2, depth=channels[4]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[5]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=2, depth=channels[6]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[7]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[8]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[9]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[10]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[11]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[12]), - mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=channels[13]) - ] - - -class FasterRCNNMobilenetV1FeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Faster R-CNN Mobilenet V1 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0, - depth_multiplier=1.0, - min_depth=16, - skip_last_stride=False, - conv_depth_ratio_in_percentage=100): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - skip_last_stride: Skip the last stride if True. - conv_depth_ratio_in_percentage: Conv depth ratio in percentage. Only - applied if skip_last_stride is True. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16. - """ - if first_stage_features_stride != 8 and first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 8 or 16.') - self._depth_multiplier = depth_multiplier - self._min_depth = min_depth - self._skip_last_stride = skip_last_stride - self._conv_depth_ratio_in_percentage = conv_depth_ratio_in_percentage - super(FasterRCNNMobilenetV1FeatureExtractor, self).__init__( - is_training, first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay) - - def preprocess(self, resized_inputs): - """Faster R-CNN Mobilenet V1 preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features. - - Args: - preprocessed_inputs: A [batch, height, width, channels] float32 tensor - representing a batch of images. - scope: A scope name. - - Returns: - rpn_feature_map: A tensor with shape [batch, height, width, depth] - activations: A dictionary mapping feature extractor tensor names to - tensors - - Raises: - InvalidArgumentError: If the spatial size of `preprocessed_inputs` - (height or width) is less than 33. - ValueError: If the created network is missing the required activation. - """ - - preprocessed_inputs.get_shape().assert_has_rank(4) - preprocessed_inputs = shape_utils.check_min_image_dim( - min_dim=33, image_tensor=preprocessed_inputs) - - with slim.arg_scope( - mobilenet_v1.mobilenet_v1_arg_scope( - is_training=self._train_batch_norm, - weight_decay=self._weight_decay)): - with tf.variable_scope('MobilenetV1', - reuse=self._reuse_weights) as scope: - params = {} - if self._skip_last_stride: - params['conv_defs'] = _get_mobilenet_conv_no_last_stride_defs( - conv_depth_ratio_in_percentage=self. - _conv_depth_ratio_in_percentage) - _, activations = mobilenet_v1.mobilenet_v1_base( - preprocessed_inputs, - final_endpoint='Conv2d_11_pointwise', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - scope=scope, - **params) - return activations['Conv2d_11_pointwise'], activations - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features. - - Args: - proposal_feature_maps: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - scope: A scope name (unused). - - Returns: - proposal_classifier_features: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - net = proposal_feature_maps - - conv_depth = 1024 - if self._skip_last_stride: - conv_depth_ratio = float(self._conv_depth_ratio_in_percentage) / 100.0 - conv_depth = int(float(conv_depth) * conv_depth_ratio) - - depth = lambda d: max(int(d * 1.0), 16) - with tf.variable_scope('MobilenetV1', reuse=self._reuse_weights): - with slim.arg_scope( - mobilenet_v1.mobilenet_v1_arg_scope( - is_training=self._train_batch_norm, - weight_decay=self._weight_decay)): - with slim.arg_scope( - [slim.conv2d, slim.separable_conv2d], padding='SAME'): - net = slim.separable_conv2d( - net, - depth(conv_depth), [3, 3], - depth_multiplier=1, - stride=2, - scope='Conv2d_12_pointwise') - return slim.separable_conv2d( - net, - depth(conv_depth), [3, 3], - depth_multiplier=1, - stride=1, - scope='Conv2d_13_pointwise') diff --git a/research/object_detection/models/faster_rcnn_mobilenet_v1_feature_extractor_tf1_test.py b/research/object_detection/models/faster_rcnn_mobilenet_v1_feature_extractor_tf1_test.py deleted file mode 100644 index 65a4958e4c2..00000000000 --- a/research/object_detection/models/faster_rcnn_mobilenet_v1_feature_extractor_tf1_test.py +++ /dev/null @@ -1,128 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for faster_rcnn_mobilenet_v1_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_mobilenet_v1_feature_extractor as faster_rcnn_mobilenet_v1 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class FasterRcnnMobilenetV1FeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, first_stage_features_stride): - return faster_rcnn_mobilenet_v1.FasterRCNNMobilenetV1FeatureExtractor( - is_training=False, - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [4, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [4, 14, 14, 512]) - - def test_extract_proposal_features_stride_eight(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=8) - preprocessed_inputs = tf.random_uniform( - [4, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [4, 14, 14, 512]) - - def test_extract_proposal_features_half_size_input(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 7, 7, 512]) - - def test_extract_proposal_features_dies_on_invalid_stride(self): - with self.assertRaises(ValueError): - self._build_feature_extractor(first_stage_features_stride=99) - - def test_extract_proposal_features_dies_on_very_small_images(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.placeholder(tf.float32, (4, None, None, 3)) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - with self.assertRaises(tf.errors.InvalidArgumentError): - sess.run( - features_shape, - feed_dict={preprocessed_inputs: np.random.rand(4, 32, 32, 3)}) - - def test_extract_proposal_features_dies_with_incorrect_rank_inputs(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [224, 224, 3], maxval=255, dtype=tf.float32) - with self.assertRaises(ValueError): - feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - proposal_feature_maps = tf.random_uniform( - [3, 14, 14, 576], maxval=255, dtype=tf.float32) - proposal_classifier_features = ( - feature_extractor.extract_box_classifier_features( - proposal_feature_maps, scope='TestScope')) - features_shape = tf.shape(proposal_classifier_features) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [3, 7, 7, 1024]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_nas_feature_extractor.py b/research/object_detection/models/faster_rcnn_nas_feature_extractor.py deleted file mode 100644 index 74782070ec5..00000000000 --- a/research/object_detection/models/faster_rcnn_nas_feature_extractor.py +++ /dev/null @@ -1,335 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""NASNet Faster R-CNN implementation. - -Learning Transferable Architectures for Scalable Image Recognition -Barret Zoph, Vijay Vasudevan, Jonathon Shlens, Quoc V. Le -https://arxiv.org/abs/1707.07012 -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.utils import variables_helper - -# pylint: disable=g-import-not-at-top -try: - from nets.nasnet import nasnet - from nets.nasnet import nasnet_utils -except: # pylint: disable=bare-except - pass -# pylint: enable=g-import-not-at-top - -arg_scope = slim.arg_scope - - -def nasnet_large_arg_scope_for_detection(is_batch_norm_training=False): - """Defines the default arg scope for the NASNet-A Large for object detection. - - This provides a small edit to switch batch norm training on and off. - - Args: - is_batch_norm_training: Boolean indicating whether to train with batch norm. - - Returns: - An `arg_scope` to use for the NASNet Large Model. - """ - imagenet_scope = nasnet.nasnet_large_arg_scope() - with arg_scope(imagenet_scope): - with arg_scope([slim.batch_norm], is_training=is_batch_norm_training) as sc: - return sc - - -# Note: This is largely a copy of _build_nasnet_base inside nasnet.py but -# with special edits to remove instantiation of the stem and the special -# ability to receive as input a pair of hidden states. -def _build_nasnet_base(hidden_previous, - hidden, - normal_cell, - reduction_cell, - hparams, - true_cell_num, - start_cell_num): - """Constructs a NASNet image model.""" - - # Find where to place the reduction cells or stride normal cells - reduction_indices = nasnet_utils.calc_reduction_layers( - hparams.num_cells, hparams.num_reduction_layers) - - # Note: The None is prepended to match the behavior of _imagenet_stem() - cell_outputs = [None, hidden_previous, hidden] - net = hidden - - # NOTE: In the nasnet.py code, filter_scaling starts at 1.0. We instead - # start at 2.0 because 1 reduction cell has been created which would - # update the filter_scaling to 2.0. - filter_scaling = 2.0 - - # Run the cells - for cell_num in range(start_cell_num, hparams.num_cells): - stride = 1 - if hparams.skip_reduction_layer_input: - prev_layer = cell_outputs[-2] - if cell_num in reduction_indices: - filter_scaling *= hparams.filter_scaling_rate - net = reduction_cell( - net, - scope='reduction_cell_{}'.format(reduction_indices.index(cell_num)), - filter_scaling=filter_scaling, - stride=2, - prev_layer=cell_outputs[-2], - cell_num=true_cell_num) - true_cell_num += 1 - cell_outputs.append(net) - if not hparams.skip_reduction_layer_input: - prev_layer = cell_outputs[-2] - net = normal_cell( - net, - scope='cell_{}'.format(cell_num), - filter_scaling=filter_scaling, - stride=stride, - prev_layer=prev_layer, - cell_num=true_cell_num) - true_cell_num += 1 - cell_outputs.append(net) - - # Final nonlinearity. - # Note that we have dropped the final pooling, dropout and softmax layers - # from the default nasnet version. - with tf.variable_scope('final_layer'): - net = tf.nn.relu(net) - return net - - -# TODO(shlens): Only fixed_shape_resizer is currently supported for NASNet -# featurization. The reason for this is that nasnet.py only supports -# inputs with fully known shapes. We need to update nasnet.py to handle -# shapes not known at compile time. -class FasterRCNNNASFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Faster R-CNN with NASNet-A feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 16. - """ - if first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 16.') - super(FasterRCNNNASFeatureExtractor, self).__init__( - is_training, first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay) - - def preprocess(self, resized_inputs): - """Faster R-CNN with NAS preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: A [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: A [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features. - - Extracts features using the first half of the NASNet network. - We construct the network in `align_feature_maps=True` mode, which means - that all VALID paddings in the network are changed to SAME padding so that - the feature maps are aligned. - - Args: - preprocessed_inputs: A [batch, height, width, channels] float32 tensor - representing a batch of images. - scope: A scope name. - - Returns: - rpn_feature_map: A tensor with shape [batch, height, width, depth] - end_points: A dictionary mapping feature extractor tensor names to tensors - - Raises: - ValueError: If the created network is missing the required activation. - """ - del scope - - if len(preprocessed_inputs.get_shape().as_list()) != 4: - raise ValueError('`preprocessed_inputs` must be 4 dimensional, got a ' - 'tensor of shape %s' % preprocessed_inputs.get_shape()) - - with slim.arg_scope(nasnet_large_arg_scope_for_detection( - is_batch_norm_training=self._train_batch_norm)): - with arg_scope([slim.conv2d, - slim.batch_norm, - slim.separable_conv2d], - reuse=self._reuse_weights): - _, end_points = nasnet.build_nasnet_large( - preprocessed_inputs, num_classes=None, - is_training=self._is_training, - final_endpoint='Cell_11') - - # Note that both 'Cell_10' and 'Cell_11' have equal depth = 2016. - rpn_feature_map = tf.concat([end_points['Cell_10'], - end_points['Cell_11']], 3) - - # nasnet.py does not maintain the batch size in the first dimension. - # This work around permits us retaining the batch for below. - batch = preprocessed_inputs.get_shape().as_list()[0] - shape_without_batch = rpn_feature_map.get_shape().as_list()[1:] - rpn_feature_map_shape = [batch] + shape_without_batch - rpn_feature_map.set_shape(rpn_feature_map_shape) - - return rpn_feature_map, end_points - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features. - - This function reconstructs the "second half" of the NASNet-A - network after the part defined in `_extract_proposal_features`. - - Args: - proposal_feature_maps: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - scope: A scope name. - - Returns: - proposal_classifier_features: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - del scope - - # Note that we always feed into 2 layers of equal depth - # where the first N channels corresponds to previous hidden layer - # and the second N channels correspond to the final hidden layer. - hidden_previous, hidden = tf.split(proposal_feature_maps, 2, axis=3) - - # Note that what follows is largely a copy of build_nasnet_large() within - # nasnet.py. We are copying to minimize code pollution in slim. - - # TODO(shlens,skornblith): Determine the appropriate drop path schedule. - # For now the schedule is the default (1.0->0.7 over 250,000 train steps). - hparams = nasnet.large_imagenet_config() - if not self._is_training: - hparams.set_hparam('drop_path_keep_prob', 1.0) - - # Calculate the total number of cells in the network - # -- Add 2 for the reduction cells. - total_num_cells = hparams.num_cells + 2 - # -- And add 2 for the stem cells for ImageNet training. - total_num_cells += 2 - - normal_cell = nasnet_utils.NasNetANormalCell( - hparams.num_conv_filters, hparams.drop_path_keep_prob, - total_num_cells, hparams.total_training_steps) - reduction_cell = nasnet_utils.NasNetAReductionCell( - hparams.num_conv_filters, hparams.drop_path_keep_prob, - total_num_cells, hparams.total_training_steps) - with arg_scope([slim.dropout, nasnet_utils.drop_path], - is_training=self._is_training): - with arg_scope([slim.batch_norm], is_training=self._train_batch_norm): - with arg_scope([slim.avg_pool2d, - slim.max_pool2d, - slim.conv2d, - slim.batch_norm, - slim.separable_conv2d, - nasnet_utils.factorized_reduction, - nasnet_utils.global_avg_pool, - nasnet_utils.get_channel_index, - nasnet_utils.get_channel_dim], - data_format=hparams.data_format): - - # This corresponds to the cell number just past 'Cell_11' used by - # by _extract_proposal_features(). - start_cell_num = 12 - # Note that this number equals: - # start_cell_num + 2 stem cells + 1 reduction cell - true_cell_num = 15 - - with slim.arg_scope(nasnet.nasnet_large_arg_scope()): - net = _build_nasnet_base(hidden_previous, - hidden, - normal_cell=normal_cell, - reduction_cell=reduction_cell, - hparams=hparams, - true_cell_num=true_cell_num, - start_cell_num=start_cell_num) - - proposal_classifier_features = net - return proposal_classifier_features - - def restore_from_classification_checkpoint_fn( - self, - first_stage_feature_extractor_scope, - second_stage_feature_extractor_scope): - """Returns a map of variables to load from a foreign checkpoint. - - Note that this overrides the default implementation in - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor which does not work for - NASNet-A checkpoints. - - Args: - first_stage_feature_extractor_scope: A scope name for the first stage - feature extractor. - second_stage_feature_extractor_scope: A scope name for the second stage - feature extractor. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - """ - # Note that the NAS checkpoint only contains the moving average version of - # the Variables so we need to generate an appropriate dictionary mapping. - variables_to_restore = {} - for variable in variables_helper.get_global_variables_safely(): - if variable.op.name.startswith( - first_stage_feature_extractor_scope): - var_name = variable.op.name.replace( - first_stage_feature_extractor_scope + '/', '') - var_name += '/ExponentialMovingAverage' - variables_to_restore[var_name] = variable - if variable.op.name.startswith( - second_stage_feature_extractor_scope): - var_name = variable.op.name.replace( - second_stage_feature_extractor_scope + '/', '') - var_name += '/ExponentialMovingAverage' - variables_to_restore[var_name] = variable - return variables_to_restore diff --git a/research/object_detection/models/faster_rcnn_nas_feature_extractor_tf1_test.py b/research/object_detection/models/faster_rcnn_nas_feature_extractor_tf1_test.py deleted file mode 100644 index a41cb0f733d..00000000000 --- a/research/object_detection/models/faster_rcnn_nas_feature_extractor_tf1_test.py +++ /dev/null @@ -1,111 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for models.faster_rcnn_nas_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_nas_feature_extractor as frcnn_nas -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class FasterRcnnNASFeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, first_stage_features_stride): - return frcnn_nas.FasterRCNNNASFeatureExtractor( - is_training=False, - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 299, 299, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 19, 19, 4032]) - - def test_extract_proposal_features_input_size_224(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 14, 14, 4032]) - - def test_extract_proposal_features_input_size_112(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 7, 7, 4032]) - - def test_extract_proposal_features_dies_on_invalid_stride(self): - with self.assertRaises(ValueError): - self._build_feature_extractor(first_stage_features_stride=99) - - def test_extract_proposal_features_dies_with_incorrect_rank_inputs(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [224, 224, 3], maxval=255, dtype=tf.float32) - with self.assertRaises(ValueError): - feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - proposal_feature_maps = tf.random_uniform( - [2, 17, 17, 1088], maxval=255, dtype=tf.float32) - proposal_classifier_features = ( - feature_extractor.extract_box_classifier_features( - proposal_feature_maps, scope='TestScope')) - features_shape = tf.shape(proposal_classifier_features) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [2, 9, 9, 4032]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_pnas_feature_extractor.py b/research/object_detection/models/faster_rcnn_pnas_feature_extractor.py deleted file mode 100644 index ea7a91d410c..00000000000 --- a/research/object_detection/models/faster_rcnn_pnas_feature_extractor.py +++ /dev/null @@ -1,328 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""PNASNet Faster R-CNN implementation. - -Based on PNASNet model: https://arxiv.org/abs/1712.00559 -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.utils import variables_helper -from nets.nasnet import nasnet_utils - -try: - from nets.nasnet import pnasnet # pylint: disable=g-import-not-at-top -except: # pylint: disable=bare-except - pass - -arg_scope = slim.arg_scope - - -def pnasnet_large_arg_scope_for_detection(is_batch_norm_training=False): - """Defines the default arg scope for the PNASNet Large for object detection. - - This provides a small edit to switch batch norm training on and off. - - Args: - is_batch_norm_training: Boolean indicating whether to train with batch norm. - - Returns: - An `arg_scope` to use for the PNASNet Large Model. - """ - imagenet_scope = pnasnet.pnasnet_large_arg_scope() - with arg_scope(imagenet_scope): - with arg_scope([slim.batch_norm], is_training=is_batch_norm_training) as sc: - return sc - - -def _filter_scaling(reduction_indices, start_cell_num): - """Compute the expected filter scaling at given PNASNet cell start_cell_num. - - In the pnasnet.py code, filter_scaling starts at 1.0. We instead - adapt filter scaling to depend on the starting cell. - At first cells, before any reduction, filter_scalling is 1.0. With passing - any reduction cell, the filter_scaling is multiplied by 2. - - Args: - reduction_indices: list of int indices. - start_cell_num: int. - Returns: - filter_scaling: float. - """ - filter_scaling = 1.0 - for ind in reduction_indices: - if ind < start_cell_num: - filter_scaling *= 2.0 - return filter_scaling - - -# Note: This is largely a copy of _build_pnasnet_base inside pnasnet.py but -# with special edits to remove instantiation of the stem and the special -# ability to receive as input a pair of hidden states. It constructs only -# a sub-network from the original PNASNet model, starting from the -# start_cell_num cell and with modified final layer. -def _build_pnasnet_base( - hidden_previous, hidden, normal_cell, hparams, true_cell_num, - start_cell_num): - """Constructs a PNASNet image model for proposal classifier features.""" - - # Find where to place the reduction cells or stride normal cells - reduction_indices = nasnet_utils.calc_reduction_layers( - hparams.num_cells, hparams.num_reduction_layers) - filter_scaling = _filter_scaling(reduction_indices, start_cell_num) - - # Note: The None is prepended to match the behavior of _imagenet_stem() - cell_outputs = [None, hidden_previous, hidden] - net = hidden - - # Run the cells - for cell_num in range(start_cell_num, hparams.num_cells): - is_reduction = cell_num in reduction_indices - stride = 2 if is_reduction else 1 - if is_reduction: filter_scaling *= hparams.filter_scaling_rate - prev_layer = cell_outputs[-2] - net = normal_cell( - net, - scope='cell_{}'.format(cell_num), - filter_scaling=filter_scaling, - stride=stride, - prev_layer=prev_layer, - cell_num=true_cell_num) - true_cell_num += 1 - cell_outputs.append(net) - - # Final nonlinearity. - # Note that we have dropped the final pooling, dropout and softmax layers - # from the default pnasnet version. - with tf.variable_scope('final_layer'): - net = tf.nn.relu(net) - return net - - -# TODO(shlens): Only fixed_shape_resizer is currently supported for PNASNet -# featurization. The reason for this is that pnasnet.py only supports -# inputs with fully known shapes. We need to update pnasnet.py to handle -# shapes not known at compile time. -class FasterRCNNPNASFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Faster R-CNN with PNASNet feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 16. - """ - if first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 16.') - super(FasterRCNNPNASFeatureExtractor, self).__init__( - is_training, first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay) - - def preprocess(self, resized_inputs): - """Faster R-CNN with PNAS preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: A [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: A [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features. - - Extracts features using the first half of the PNASNet network. - We construct the network in `align_feature_maps=True` mode, which means - that all VALID paddings in the network are changed to SAME padding so that - the feature maps are aligned. - - Args: - preprocessed_inputs: A [batch, height, width, channels] float32 tensor - representing a batch of images. - scope: A scope name. - - Returns: - rpn_feature_map: A tensor with shape [batch, height, width, depth] - end_points: A dictionary mapping feature extractor tensor names to tensors - - Raises: - ValueError: If the created network is missing the required activation. - """ - del scope - - if len(preprocessed_inputs.get_shape().as_list()) != 4: - raise ValueError('`preprocessed_inputs` must be 4 dimensional, got a ' - 'tensor of shape %s' % preprocessed_inputs.get_shape()) - - with slim.arg_scope(pnasnet_large_arg_scope_for_detection( - is_batch_norm_training=self._train_batch_norm)): - with arg_scope([slim.conv2d, - slim.batch_norm, - slim.separable_conv2d], - reuse=self._reuse_weights): - _, end_points = pnasnet.build_pnasnet_large( - preprocessed_inputs, num_classes=None, - is_training=self._is_training, - final_endpoint='Cell_7') - - # Note that both 'Cell_6' and 'Cell_7' have equal depth = 2160. - # Cell_7 is the last cell before second reduction. - rpn_feature_map = tf.concat([end_points['Cell_6'], - end_points['Cell_7']], 3) - - # pnasnet.py does not maintain the batch size in the first dimension. - # This work around permits us retaining the batch for below. - batch = preprocessed_inputs.get_shape().as_list()[0] - shape_without_batch = rpn_feature_map.get_shape().as_list()[1:] - rpn_feature_map_shape = [batch] + shape_without_batch - rpn_feature_map.set_shape(rpn_feature_map_shape) - - return rpn_feature_map, end_points - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features. - - This function reconstructs the "second half" of the PNASNet - network after the part defined in `_extract_proposal_features`. - - Args: - proposal_feature_maps: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - scope: A scope name. - - Returns: - proposal_classifier_features: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - del scope - - # Number of used stem cells. - num_stem_cells = 2 - - # Note that we always feed into 2 layers of equal depth - # where the first N channels corresponds to previous hidden layer - # and the second N channels correspond to the final hidden layer. - hidden_previous, hidden = tf.split(proposal_feature_maps, 2, axis=3) - - # Note that what follows is largely a copy of build_pnasnet_large() within - # pnasnet.py. We are copying to minimize code pollution in slim. - - # TODO(shlens,skornblith): Determine the appropriate drop path schedule. - # For now the schedule is the default (1.0->0.7 over 250,000 train steps). - hparams = pnasnet.large_imagenet_config() - if not self._is_training: - hparams.set_hparam('drop_path_keep_prob', 1.0) - - # Calculate the total number of cells in the network - total_num_cells = hparams.num_cells + num_stem_cells - - normal_cell = pnasnet.PNasNetNormalCell( - hparams.num_conv_filters, hparams.drop_path_keep_prob, - total_num_cells, hparams.total_training_steps) - with arg_scope([slim.dropout, nasnet_utils.drop_path], - is_training=self._is_training): - with arg_scope([slim.batch_norm], is_training=self._train_batch_norm): - with arg_scope([slim.avg_pool2d, - slim.max_pool2d, - slim.conv2d, - slim.batch_norm, - slim.separable_conv2d, - nasnet_utils.factorized_reduction, - nasnet_utils.global_avg_pool, - nasnet_utils.get_channel_index, - nasnet_utils.get_channel_dim], - data_format=hparams.data_format): - - # This corresponds to the cell number just past 'Cell_7' used by - # _extract_proposal_features(). - start_cell_num = 8 - true_cell_num = start_cell_num + num_stem_cells - - with slim.arg_scope(pnasnet.pnasnet_large_arg_scope()): - net = _build_pnasnet_base( - hidden_previous, - hidden, - normal_cell=normal_cell, - hparams=hparams, - true_cell_num=true_cell_num, - start_cell_num=start_cell_num) - - proposal_classifier_features = net - return proposal_classifier_features - - def restore_from_classification_checkpoint_fn( - self, - first_stage_feature_extractor_scope, - second_stage_feature_extractor_scope): - """Returns a map of variables to load from a foreign checkpoint. - - Note that this overrides the default implementation in - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor which does not work for - PNASNet checkpoints. - - Args: - first_stage_feature_extractor_scope: A scope name for the first stage - feature extractor. - second_stage_feature_extractor_scope: A scope name for the second stage - feature extractor. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - """ - variables_to_restore = {} - for variable in variables_helper.get_global_variables_safely(): - if variable.op.name.startswith( - first_stage_feature_extractor_scope): - var_name = variable.op.name.replace( - first_stage_feature_extractor_scope + '/', '') - var_name += '/ExponentialMovingAverage' - variables_to_restore[var_name] = variable - if variable.op.name.startswith( - second_stage_feature_extractor_scope): - var_name = variable.op.name.replace( - second_stage_feature_extractor_scope + '/', '') - var_name += '/ExponentialMovingAverage' - variables_to_restore[var_name] = variable - return variables_to_restore diff --git a/research/object_detection/models/faster_rcnn_pnas_feature_extractor_tf1_test.py b/research/object_detection/models/faster_rcnn_pnas_feature_extractor_tf1_test.py deleted file mode 100644 index 16774511b4d..00000000000 --- a/research/object_detection/models/faster_rcnn_pnas_feature_extractor_tf1_test.py +++ /dev/null @@ -1,124 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for models.faster_rcnn_pnas_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_pnas_feature_extractor as frcnn_pnas -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class FasterRcnnPNASFeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, first_stage_features_stride): - return frcnn_pnas.FasterRCNNPNASFeatureExtractor( - is_training=False, - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 299, 299, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 19, 19, 4320]) - - def test_extract_proposal_features_input_size_224(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 14, 14, 4320]) - - def test_extract_proposal_features_input_size_112(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 7, 7, 4320]) - - def test_extract_proposal_features_dies_on_invalid_stride(self): - with self.assertRaises(ValueError): - self._build_feature_extractor(first_stage_features_stride=99) - - def test_extract_proposal_features_dies_with_incorrect_rank_inputs(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [224, 224, 3], maxval=255, dtype=tf.float32) - with self.assertRaises(ValueError): - feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - proposal_feature_maps = tf.random_uniform( - [2, 17, 17, 1088], maxval=255, dtype=tf.float32) - proposal_classifier_features = ( - feature_extractor.extract_box_classifier_features( - proposal_feature_maps, scope='TestScope')) - features_shape = tf.shape(proposal_classifier_features) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [2, 9, 9, 4320]) - - def test_filter_scaling_computation(self): - expected_filter_scaling = { - ((4, 8), 2): 1.0, - ((4, 8), 7): 2.0, - ((4, 8), 8): 2.0, - ((4, 8), 9): 4.0 - } - for args, filter_scaling in expected_filter_scaling.items(): - reduction_indices, start_cell_num = args - self.assertAlmostEqual( - frcnn_pnas._filter_scaling(reduction_indices, start_cell_num), - filter_scaling) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_resnet_keras_feature_extractor.py b/research/object_detection/models/faster_rcnn_resnet_keras_feature_extractor.py deleted file mode 100644 index a6b1e25404c..00000000000 --- a/research/object_detection/models/faster_rcnn_resnet_keras_feature_extractor.py +++ /dev/null @@ -1,254 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Resnet based Faster R-CNN implementation in Keras. - -See Deep Residual Learning for Image Recognition by He et al. -https://arxiv.org/abs/1512.03385 -""" - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.models.keras_models import resnet_v1 -from object_detection.utils import model_util - - -_RESNET_MODEL_CONV4_LAST_LAYERS = { - 'resnet_v1_50': 'conv4_block6_out', - 'resnet_v1_101': 'conv4_block23_out', - 'resnet_v1_152': 'conv4_block36_out', -} - - -class FasterRCNNResnetKerasFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor): - """Faster R-CNN with Resnet feature extractor implementation.""" - - def __init__(self, - is_training, - resnet_v1_base_model, - resnet_v1_base_model_name, - first_stage_features_stride=16, - batch_norm_trainable=False, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - resnet_v1_base_model: base resnet v1 network to use. One of - the resnet_v1.resnet_v1_{50,101,152} models. - resnet_v1_base_model_name: model name under which to construct resnet v1. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - weight_decay: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16. - """ - if first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 16.') - super(FasterRCNNResnetKerasFeatureExtractor, self).__init__( - is_training, first_stage_features_stride, batch_norm_trainable, - weight_decay) - self.classification_backbone = None - self._variable_dict = {} - self._resnet_v1_base_model = resnet_v1_base_model - self._resnet_v1_base_model_name = resnet_v1_base_model_name - - def preprocess(self, resized_inputs): - """Faster R-CNN Resnet V1 preprocessing. - - VGG style channel mean subtraction as described here: - https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md - Note that if the number of channels is not equal to 3, the mean subtraction - will be skipped and the original resized_inputs will be returned. - - Args: - resized_inputs: A [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: A [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - - """ - if resized_inputs.shape.as_list()[3] == 3: - channel_means = [123.68, 116.779, 103.939] - return resized_inputs - [[channel_means]] - else: - return resized_inputs - - def get_proposal_feature_extractor_model(self, name=None): - """Returns a model that extracts first stage RPN features. - - Extracts features using the first half of the Resnet v1 network. - - Args: - name: A scope name to construct all variables within. - - Returns: - A Keras model that takes preprocessed_inputs: - A [batch, height, width, channels] float32 tensor - representing a batch of images. - - And returns rpn_feature_map: - A tensor with shape [batch, height, width, depth] - """ - if not self.classification_backbone: - self.classification_backbone = self._resnet_v1_base_model( - batchnorm_training=self._train_batch_norm, - conv_hyperparams=None, - weight_decay=self._weight_decay, - classes=None, - weights=None, - include_top=False - ) - with tf.name_scope(name): - with tf.name_scope('ResnetV1'): - - conv4_last_layer = _RESNET_MODEL_CONV4_LAST_LAYERS[ - self._resnet_v1_base_model_name] - proposal_features = self.classification_backbone.get_layer( - name=conv4_last_layer).output - keras_model = tf.keras.Model( - inputs=self.classification_backbone.inputs, - outputs=proposal_features) - for variable in keras_model.variables: - self._variable_dict[variable.name[:-2]] = variable - return keras_model - - def get_box_classifier_feature_extractor_model(self, name=None): - """Returns a model that extracts second stage box classifier features. - - This function reconstructs the "second half" of the ResNet v1 - network after the part defined in `get_proposal_feature_extractor_model`. - - Args: - name: A scope name to construct all variables within. - - Returns: - A Keras model that takes proposal_feature_maps: - A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - And returns proposal_classifier_features: - A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - if not self.classification_backbone: - self.classification_backbone = self._resnet_v1_base_model( - batchnorm_training=self._train_batch_norm, - conv_hyperparams=None, - weight_decay=self._weight_decay, - classes=None, - weights=None, - include_top=False - ) - with tf.name_scope(name): - with tf.name_scope('ResnetV1'): - conv4_last_layer = _RESNET_MODEL_CONV4_LAST_LAYERS[ - self._resnet_v1_base_model_name] - proposal_feature_maps = self.classification_backbone.get_layer( - name=conv4_last_layer).output - proposal_classifier_features = self.classification_backbone.get_layer( - name='conv5_block3_out').output - - keras_model = model_util.extract_submodel( - model=self.classification_backbone, - inputs=proposal_feature_maps, - outputs=proposal_classifier_features) - for variable in keras_model.variables: - self._variable_dict[variable.name[:-2]] = variable - return keras_model - - -class FasterRCNNResnet50KerasFeatureExtractor( - FasterRCNNResnetKerasFeatureExtractor): - """Faster R-CNN with Resnet50 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride=16, - batch_norm_trainable=False, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - weight_decay: See base class. - """ - super(FasterRCNNResnet50KerasFeatureExtractor, self).__init__( - is_training=is_training, - resnet_v1_base_model=resnet_v1.resnet_v1_50, - resnet_v1_base_model_name='resnet_v1_50', - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=batch_norm_trainable, - weight_decay=weight_decay) - - -class FasterRCNNResnet101KerasFeatureExtractor( - FasterRCNNResnetKerasFeatureExtractor): - """Faster R-CNN with Resnet101 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride=16, - batch_norm_trainable=False, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - weight_decay: See base class. - """ - super(FasterRCNNResnet101KerasFeatureExtractor, self).__init__( - is_training=is_training, - resnet_v1_base_model=resnet_v1.resnet_v1_101, - resnet_v1_base_model_name='resnet_v1_101', - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=batch_norm_trainable, - weight_decay=weight_decay) - - -class FasterRCNNResnet152KerasFeatureExtractor( - FasterRCNNResnetKerasFeatureExtractor): - """Faster R-CNN with Resnet152 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride=16, - batch_norm_trainable=False, - weight_decay=0.0): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - weight_decay: See base class. - """ - super(FasterRCNNResnet152KerasFeatureExtractor, self).__init__( - is_training=is_training, - resnet_v1_base_model=resnet_v1.resnet_v1_152, - resnet_v1_base_model_name='resnet_v1_152', - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=batch_norm_trainable, - weight_decay=weight_decay) diff --git a/research/object_detection/models/faster_rcnn_resnet_keras_feature_extractor_tf2_test.py b/research/object_detection/models/faster_rcnn_resnet_keras_feature_extractor_tf2_test.py deleted file mode 100644 index 15e8a5fbf15..00000000000 --- a/research/object_detection/models/faster_rcnn_resnet_keras_feature_extractor_tf2_test.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for models.faster_rcnn_resnet_keras_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_resnet_keras_feature_extractor as frcnn_res -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class FasterRcnnResnetKerasFeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, architecture='resnet_v1_50'): - return frcnn_res.FasterRCNNResnet50KerasFeatureExtractor( - is_training=False, - first_stage_features_stride=16, - batch_norm_trainable=False, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor() - preprocessed_inputs = tf.random_uniform( - [1, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map = feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - features_shape = tf.shape(rpn_feature_map) - self.assertAllEqual(features_shape.numpy(), [1, 14, 14, 1024]) - - def test_extract_proposal_features_half_size_input(self): - feature_extractor = self._build_feature_extractor() - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map = feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - features_shape = tf.shape(rpn_feature_map) - self.assertAllEqual(features_shape.numpy(), [1, 7, 7, 1024]) - - def test_extract_proposal_features_dies_with_incorrect_rank_inputs(self): - feature_extractor = self._build_feature_extractor() - preprocessed_inputs = tf.random_uniform( - [224, 224, 3], maxval=255, dtype=tf.float32) - with self.assertRaises(tf.errors.InvalidArgumentError): - feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor() - proposal_feature_maps = tf.random_uniform( - [3, 7, 7, 1024], maxval=255, dtype=tf.float32) - model = feature_extractor.get_box_classifier_feature_extractor_model( - name='TestScope') - proposal_classifier_features = ( - model(proposal_feature_maps)) - features_shape = tf.shape(proposal_classifier_features) - # Note: due to a slight mismatch in slim and keras resnet definitions - # the output shape of the box classifier is slightly different compared to - # that of the slim implementation. The keras version is more `canonical` - # in that it more accurately reflects the original authors' implementation. - # TODO(jonathanhuang): make the output shape match that of the slim - # implementation by using atrous convolutions. - self.assertAllEqual(features_shape.numpy(), [3, 4, 4, 2048]) - - -if __name__ == '__main__': - tf.enable_v2_behavior() - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_resnet_v1_feature_extractor.py b/research/object_detection/models/faster_rcnn_resnet_v1_feature_extractor.py deleted file mode 100644 index 30cd9d42c54..00000000000 --- a/research/object_detection/models/faster_rcnn_resnet_v1_feature_extractor.py +++ /dev/null @@ -1,268 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Resnet V1 Faster R-CNN implementation. - -See "Deep Residual Learning for Image Recognition" by He et al., 2015. -https://arxiv.org/abs/1512.03385 - -Note: this implementation assumes that the classification checkpoint used -to finetune this model is trained using the same configuration as that of -the MSRA provided checkpoints -(see https://github.com/KaimingHe/deep-residual-networks), e.g., with -same preprocessing, batch norm scaling, etc. -""" -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from nets import resnet_utils -from nets import resnet_v1 - - -class FasterRCNNResnetV1FeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNFeatureExtractor): - """Faster R-CNN Resnet V1 feature extractor implementation.""" - - def __init__(self, - architecture, - resnet_model, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0, - activation_fn=tf.nn.relu): - """Constructor. - - Args: - architecture: Architecture name of the Resnet V1 model. - resnet_model: Definition of the Resnet V1 model. - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - activation_fn: Activaton functon to use in Resnet V1 model. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16. - """ - if first_stage_features_stride != 8 and first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 8 or 16.') - self._architecture = architecture - self._resnet_model = resnet_model - self._activation_fn = activation_fn - super(FasterRCNNResnetV1FeatureExtractor, - self).__init__(is_training, first_stage_features_stride, - batch_norm_trainable, reuse_weights, weight_decay) - - def preprocess(self, resized_inputs): - """Faster R-CNN Resnet V1 preprocessing. - - VGG style channel mean subtraction as described here: - https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md - Note that if the number of channels is not equal to 3, the mean subtraction - will be skipped and the original resized_inputs will be returned. - - Args: - resized_inputs: A [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: A [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - - """ - if resized_inputs.shape.as_list()[3] == 3: - channel_means = [123.68, 116.779, 103.939] - return resized_inputs - [[channel_means]] - else: - return resized_inputs - - def _extract_proposal_features(self, preprocessed_inputs, scope): - """Extracts first stage RPN features. - - Args: - preprocessed_inputs: A [batch, height, width, channels] float32 tensor - representing a batch of images. - scope: A scope name. - - Returns: - rpn_feature_map: A tensor with shape [batch, height, width, depth] - activations: A dictionary mapping feature extractor tensor names to - tensors - - Raises: - InvalidArgumentError: If the spatial size of `preprocessed_inputs` - (height or width) is less than 33. - ValueError: If the created network is missing the required activation. - """ - if len(preprocessed_inputs.get_shape().as_list()) != 4: - raise ValueError('`preprocessed_inputs` must be 4 dimensional, got a ' - 'tensor of shape %s' % preprocessed_inputs.get_shape()) - shape_assert = tf.Assert( - tf.logical_and( - tf.greater_equal(tf.shape(preprocessed_inputs)[1], 33), - tf.greater_equal(tf.shape(preprocessed_inputs)[2], 33)), - ['image size must at least be 33 in both height and width.']) - - with tf.control_dependencies([shape_assert]): - # Disables batchnorm for fine-tuning with smaller batch sizes. - # TODO(chensun): Figure out if it is needed when image - # batch size is bigger. - with slim.arg_scope( - resnet_utils.resnet_arg_scope( - batch_norm_epsilon=1e-5, - batch_norm_scale=True, - activation_fn=self._activation_fn, - weight_decay=self._weight_decay)): - with tf.variable_scope( - self._architecture, reuse=self._reuse_weights) as var_scope: - _, activations = self._resnet_model( - preprocessed_inputs, - num_classes=None, - is_training=self._train_batch_norm, - global_pool=False, - output_stride=self._first_stage_features_stride, - spatial_squeeze=False, - scope=var_scope) - - handle = scope + '/%s/block3' % self._architecture - return activations[handle], activations - - def _extract_box_classifier_features(self, proposal_feature_maps, scope): - """Extracts second stage box classifier features. - - Args: - proposal_feature_maps: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - scope: A scope name (unused). - - Returns: - proposal_classifier_features: A 4-D float tensor with shape - [batch_size * self.max_num_proposals, height, width, depth] - representing box classifier features for each proposal. - """ - with tf.variable_scope(self._architecture, reuse=self._reuse_weights): - with slim.arg_scope( - resnet_utils.resnet_arg_scope( - batch_norm_epsilon=1e-5, - batch_norm_scale=True, - activation_fn=self._activation_fn, - weight_decay=self._weight_decay)): - with slim.arg_scope([slim.batch_norm], - is_training=self._train_batch_norm): - blocks = [ - resnet_utils.Block('block4', resnet_v1.bottleneck, [{ - 'depth': 2048, - 'depth_bottleneck': 512, - 'stride': 1 - }] * 3) - ] - proposal_classifier_features = resnet_utils.stack_blocks_dense( - proposal_feature_maps, blocks) - return proposal_classifier_features - - -class FasterRCNNResnet50FeatureExtractor(FasterRCNNResnetV1FeatureExtractor): - """Faster R-CNN Resnet 50 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0, - activation_fn=tf.nn.relu): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - activation_fn: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16, - or if `architecture` is not supported. - """ - super(FasterRCNNResnet50FeatureExtractor, - self).__init__('resnet_v1_50', resnet_v1.resnet_v1_50, is_training, - first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay, activation_fn) - - -class FasterRCNNResnet101FeatureExtractor(FasterRCNNResnetV1FeatureExtractor): - """Faster R-CNN Resnet 101 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0, - activation_fn=tf.nn.relu): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - activation_fn: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16, - or if `architecture` is not supported. - """ - super(FasterRCNNResnet101FeatureExtractor, - self).__init__('resnet_v1_101', resnet_v1.resnet_v1_101, is_training, - first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay, activation_fn) - - -class FasterRCNNResnet152FeatureExtractor(FasterRCNNResnetV1FeatureExtractor): - """Faster R-CNN Resnet 152 feature extractor implementation.""" - - def __init__(self, - is_training, - first_stage_features_stride, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0, - activation_fn=tf.nn.relu): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - reuse_weights: See base class. - weight_decay: See base class. - activation_fn: See base class. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16, - or if `architecture` is not supported. - """ - super(FasterRCNNResnet152FeatureExtractor, - self).__init__('resnet_v1_152', resnet_v1.resnet_v1_152, is_training, - first_stage_features_stride, batch_norm_trainable, - reuse_weights, weight_decay, activation_fn) diff --git a/research/object_detection/models/faster_rcnn_resnet_v1_feature_extractor_tf1_test.py b/research/object_detection/models/faster_rcnn_resnet_v1_feature_extractor_tf1_test.py deleted file mode 100644 index 3d47da04af5..00000000000 --- a/research/object_detection/models/faster_rcnn_resnet_v1_feature_extractor_tf1_test.py +++ /dev/null @@ -1,167 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.models.faster_rcnn_resnet_v1_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import faster_rcnn_resnet_v1_feature_extractor as faster_rcnn_resnet_v1 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class FasterRcnnResnetV1FeatureExtractorTest(tf.test.TestCase): - - def _build_feature_extractor(self, - first_stage_features_stride, - activation_fn=tf.nn.relu, - architecture='resnet_v1_101'): - feature_extractor_map = { - 'resnet_v1_50': - faster_rcnn_resnet_v1.FasterRCNNResnet50FeatureExtractor, - 'resnet_v1_101': - faster_rcnn_resnet_v1.FasterRCNNResnet101FeatureExtractor, - 'resnet_v1_152': - faster_rcnn_resnet_v1.FasterRCNNResnet152FeatureExtractor - } - return feature_extractor_map[architecture]( - is_training=False, - first_stage_features_stride=first_stage_features_stride, - activation_fn=activation_fn, - batch_norm_trainable=False, - reuse_weights=None, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - for architecture in ['resnet_v1_50', 'resnet_v1_101', 'resnet_v1_152']: - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16, architecture=architecture) - preprocessed_inputs = tf.random_uniform( - [4, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [4, 14, 14, 1024]) - - def test_extract_proposal_features_stride_eight(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=8) - preprocessed_inputs = tf.random_uniform( - [4, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [4, 28, 28, 1024]) - - def test_extract_proposal_features_half_size_input(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [1, 112, 112, 3], maxval=255, dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [1, 7, 7, 1024]) - - def test_extract_proposal_features_dies_on_invalid_stride(self): - with self.assertRaises(ValueError): - self._build_feature_extractor(first_stage_features_stride=99) - - def test_extract_proposal_features_dies_on_very_small_images(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.placeholder(tf.float32, (4, None, None, 3)) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - features_shape = tf.shape(rpn_feature_map) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - with self.assertRaises(tf.errors.InvalidArgumentError): - sess.run( - features_shape, - feed_dict={preprocessed_inputs: np.random.rand(4, 32, 32, 3)}) - - def test_extract_proposal_features_dies_with_incorrect_rank_inputs(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - preprocessed_inputs = tf.random_uniform( - [224, 224, 3], maxval=255, dtype=tf.float32) - with self.assertRaises(ValueError): - feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestScope') - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16) - proposal_feature_maps = tf.random_uniform( - [3, 7, 7, 1024], maxval=255, dtype=tf.float32) - proposal_classifier_features = ( - feature_extractor.extract_box_classifier_features( - proposal_feature_maps, scope='TestScope')) - features_shape = tf.shape(proposal_classifier_features) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - features_shape_out = sess.run(features_shape) - self.assertAllEqual(features_shape_out, [3, 7, 7, 2048]) - - def test_overwriting_activation_fn(self): - for architecture in ['resnet_v1_50', 'resnet_v1_101', 'resnet_v1_152']: - feature_extractor = self._build_feature_extractor( - first_stage_features_stride=16, - architecture=architecture, - activation_fn=tf.nn.relu6) - preprocessed_inputs = tf.random_uniform([4, 224, 224, 3], - maxval=255, - dtype=tf.float32) - rpn_feature_map, _ = feature_extractor.extract_proposal_features( - preprocessed_inputs, scope='TestStage1Scope') - _ = feature_extractor.extract_box_classifier_features( - rpn_feature_map, scope='TestStaget2Scope') - conv_ops = [ - op for op in tf.get_default_graph().get_operations() - if op.type == 'Relu6' - ] - op_names = [op.name for op in conv_ops] - - self.assertIsNotNone(conv_ops) - self.assertIn('TestStage1Scope/resnet_v1_50/resnet_v1_50/conv1/Relu6', - op_names) - self.assertIn( - 'TestStaget2Scope/resnet_v1_50/block4/unit_1/bottleneck_v1/conv1/Relu6', - op_names) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/faster_rcnn_resnet_v1_fpn_keras_feature_extractor.py b/research/object_detection/models/faster_rcnn_resnet_v1_fpn_keras_feature_extractor.py deleted file mode 100644 index 27d8844b7b7..00000000000 --- a/research/object_detection/models/faster_rcnn_resnet_v1_fpn_keras_feature_extractor.py +++ /dev/null @@ -1,434 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Faster RCNN Keras-based Resnet V1 FPN Feature Extractor.""" - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import faster_rcnn_meta_arch -from object_detection.models import feature_map_generators -from object_detection.models.keras_models import resnet_v1 -from object_detection.utils import ops - - -_RESNET_MODEL_OUTPUT_LAYERS = { - 'resnet_v1_50': ['conv2_block3_out', 'conv3_block4_out', - 'conv4_block6_out', 'conv5_block3_out'], - 'resnet_v1_101': ['conv2_block3_out', 'conv3_block4_out', - 'conv4_block23_out', 'conv5_block3_out'], - 'resnet_v1_152': ['conv2_block3_out', 'conv3_block8_out', - 'conv4_block36_out', 'conv5_block3_out'], -} - - -class _ResnetFPN(tf.keras.layers.Layer): - """Construct Resnet FPN layer.""" - - def __init__(self, - backbone_classifier, - fpn_features_generator, - coarse_feature_layers, - pad_to_multiple, - fpn_min_level, - resnet_block_names, - base_fpn_max_level): - """Constructor. - - Args: - backbone_classifier: Classifier backbone. Should be one of 'resnet_v1_50', - 'resnet_v1_101', 'resnet_v1_152'. - fpn_features_generator: KerasFpnTopDownFeatureMaps that accepts a - dictionary of features and returns a ordered dictionary of fpn features. - coarse_feature_layers: Coarse feature layers for fpn. - pad_to_multiple: An integer multiple to pad input image. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to Resnet v1 layers. - resnet_block_names: a list of block names of resnet. - base_fpn_max_level: maximum level of fpn without coarse feature layers. - """ - super(_ResnetFPN, self).__init__() - self.classification_backbone = backbone_classifier - self.fpn_features_generator = fpn_features_generator - self.coarse_feature_layers = coarse_feature_layers - self.pad_to_multiple = pad_to_multiple - self._fpn_min_level = fpn_min_level - self._resnet_block_names = resnet_block_names - self._base_fpn_max_level = base_fpn_max_level - - def call(self, inputs): - """Create internal Resnet FPN layer. - - Args: - inputs: A [batch, height_out, width_out, channels] float32 tensor - representing a batch of images. - - Returns: - feature_maps: A list of tensors with shape [batch, height, width, depth] - represent extracted features. - """ - inputs = ops.pad_to_multiple(inputs, self.pad_to_multiple) - backbone_outputs = self.classification_backbone(inputs) - - feature_block_list = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_block_list.append('block{}'.format(level - 1)) - feature_block_map = dict( - list(zip(self._resnet_block_names, backbone_outputs))) - fpn_input_image_features = [ - (feature_block, feature_block_map[feature_block]) - for feature_block in feature_block_list] - fpn_features = self.fpn_features_generator(fpn_input_image_features) - - feature_maps = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_maps.append(fpn_features['top_down_block{}'.format(level-1)]) - last_feature_map = fpn_features['top_down_block{}'.format( - self._base_fpn_max_level - 1)] - - for coarse_feature_layers in self.coarse_feature_layers: - for layer in coarse_feature_layers: - last_feature_map = layer(last_feature_map) - feature_maps.append(last_feature_map) - - return feature_maps - - -class FasterRCNNResnetV1FpnKerasFeatureExtractor( - faster_rcnn_meta_arch.FasterRCNNKerasFeatureExtractor): - """Faster RCNN Feature Extractor using Keras-based Resnet V1 FPN features.""" - - def __init__(self, - is_training, - resnet_v1_base_model, - resnet_v1_base_model_name, - first_stage_features_stride, - conv_hyperparams, - batch_norm_trainable=True, - pad_to_multiple=32, - weight_decay=0.0, - fpn_min_level=2, - fpn_max_level=6, - additional_layer_depth=256, - override_base_feature_extractor_hyperparams=False): - """Constructor. - - Args: - is_training: See base class. - resnet_v1_base_model: base resnet v1 network to use. One of - the resnet_v1.resnet_v1_{50,101,152} models. - resnet_v1_base_model_name: model name under which to construct resnet v1. - first_stage_features_stride: See base class. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - batch_norm_trainable: See base class. - pad_to_multiple: An integer multiple to pad input image. - weight_decay: See base class. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to Resnet v1 layers. - fpn_max_level: the smallest resolution feature map to construct or use in - FPN. FPN constructions uses features maps starting from fpn_min_level - upto the fpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of fpn - levels. - additional_layer_depth: additional feature map layer channel depth. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - - Raises: - ValueError: If `first_stage_features_stride` is not 8 or 16. - """ - if first_stage_features_stride != 8 and first_stage_features_stride != 16: - raise ValueError('`first_stage_features_stride` must be 8 or 16.') - - super(FasterRCNNResnetV1FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - first_stage_features_stride=first_stage_features_stride, - batch_norm_trainable=batch_norm_trainable, - weight_decay=weight_decay) - - self._resnet_v1_base_model = resnet_v1_base_model - self._resnet_v1_base_model_name = resnet_v1_base_model_name - self._conv_hyperparams = conv_hyperparams - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._additional_layer_depth = additional_layer_depth - self._freeze_batchnorm = (not batch_norm_trainable) - self._pad_to_multiple = pad_to_multiple - - self._override_base_feature_extractor_hyperparams = \ - override_base_feature_extractor_hyperparams - self._resnet_block_names = ['block1', 'block2', 'block3', 'block4'] - self.classification_backbone = None - self._fpn_features_generator = None - self._coarse_feature_layers = [] - - def preprocess(self, resized_inputs): - """Faster R-CNN Resnet V1 preprocessing. - - VGG style channel mean subtraction as described here: - https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md - Note that if the number of channels is not equal to 3, the mean subtraction - will be skipped and the original resized_inputs will be returned. - - Args: - resized_inputs: A [batch, height_in, width_in, channels] float32 tensor - representing a batch of images with values between 0 and 255.0. - - Returns: - preprocessed_inputs: A [batch, height_out, width_out, channels] float32 - tensor representing a batch of images. - - """ - if resized_inputs.shape.as_list()[3] == 3: - channel_means = [123.68, 116.779, 103.939] - return resized_inputs - [[channel_means]] - else: - return resized_inputs - - def get_proposal_feature_extractor_model(self, name=None): - """Returns a model that extracts first stage RPN features. - - Extracts features using the Resnet v1 FPN network. - - Args: - name: A scope name to construct all variables within. - - Returns: - A Keras model that takes preprocessed_inputs: - A [batch, height, width, channels] float32 tensor - representing a batch of images. - - And returns rpn_feature_map: - A list of tensors with shape [batch, height, width, depth] - """ - with tf.name_scope(name): - with tf.name_scope('ResnetV1FPN'): - full_resnet_v1_model = self._resnet_v1_base_model( - batchnorm_training=self._train_batch_norm, - conv_hyperparams=(self._conv_hyperparams if - self._override_base_feature_extractor_hyperparams - else None), - classes=None, - weights=None, - include_top=False) - output_layers = _RESNET_MODEL_OUTPUT_LAYERS[ - self._resnet_v1_base_model_name] - outputs = [full_resnet_v1_model.get_layer(output_layer_name).output - for output_layer_name in output_layers] - self.classification_backbone = tf.keras.Model( - inputs=full_resnet_v1_model.inputs, - outputs=outputs) - - self._base_fpn_max_level = min(self._fpn_max_level, 5) - self._num_levels = self._base_fpn_max_level + 1 - self._fpn_min_level - self._fpn_features_generator = ( - feature_map_generators.KerasFpnTopDownFeatureMaps( - num_levels=self._num_levels, - depth=self._additional_layer_depth, - is_training=self._is_training, - conv_hyperparams=self._conv_hyperparams, - freeze_batchnorm=self._freeze_batchnorm, - name='FeatureMaps')) - - # Construct coarse feature layers - for i in range(self._base_fpn_max_level, self._fpn_max_level): - layers = [] - layer_name = 'bottom_up_block{}'.format(i) - layers.append( - tf.keras.layers.Conv2D( - self._additional_layer_depth, - [3, 3], - padding='SAME', - strides=2, - name=layer_name + '_conv', - **self._conv_hyperparams.params())) - layers.append( - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name=layer_name + '_batchnorm')) - layers.append( - self._conv_hyperparams.build_activation_layer( - name=layer_name)) - self._coarse_feature_layers.append(layers) - - feature_extractor_model = _ResnetFPN(self.classification_backbone, - self._fpn_features_generator, - self._coarse_feature_layers, - self._pad_to_multiple, - self._fpn_min_level, - self._resnet_block_names, - self._base_fpn_max_level) - return feature_extractor_model - - def get_box_classifier_feature_extractor_model(self, name=None): - """Returns a model that extracts second stage box classifier features. - - Construct two fully connected layer to extract the box classifier features. - - Args: - name: A scope name to construct all variables within. - - Returns: - A Keras model that takes proposal_feature_maps: - A 4-D float tensor with shape - [batch_size * self.max_num_proposals, crop_height, crop_width, depth] - representing the feature map cropped to each proposal. - - And returns proposal_classifier_features: - A 4-D float tensor with shape - [batch_size * self.max_num_proposals, 1, 1, 1024] - representing box classifier features for each proposal. - """ - with tf.name_scope(name): - with tf.name_scope('ResnetV1FPN'): - feature_extractor_model = tf.keras.models.Sequential([ - tf.keras.layers.Flatten(), - tf.keras.layers.Dense(units=1024, activation='relu'), - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm)), - tf.keras.layers.Dense(units=1024, activation='relu'), - tf.keras.layers.Reshape((1, 1, 1024)) - ]) - return feature_extractor_model - - -class FasterRCNNResnet50FpnKerasFeatureExtractor( - FasterRCNNResnetV1FpnKerasFeatureExtractor): - """Faster RCNN with Resnet50 FPN feature extractor.""" - - def __init__(self, - is_training, - first_stage_features_stride=16, - batch_norm_trainable=True, - conv_hyperparams=None, - weight_decay=0.0, - fpn_min_level=2, - fpn_max_level=6, - additional_layer_depth=256, - override_base_feature_extractor_hyperparams=False): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - conv_hyperparams: See base class. - weight_decay: See base class. - fpn_min_level: See base class. - fpn_max_level: See base class. - additional_layer_depth: See base class. - override_base_feature_extractor_hyperparams: See base class. - """ - super(FasterRCNNResnet50FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - first_stage_features_stride=first_stage_features_stride, - conv_hyperparams=conv_hyperparams, - resnet_v1_base_model=resnet_v1.resnet_v1_50, - resnet_v1_base_model_name='resnet_v1_50', - batch_norm_trainable=batch_norm_trainable, - weight_decay=weight_decay, - fpn_min_level=fpn_min_level, - fpn_max_level=fpn_max_level, - additional_layer_depth=additional_layer_depth, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams - ) - - -class FasterRCNNResnet101FpnKerasFeatureExtractor( - FasterRCNNResnetV1FpnKerasFeatureExtractor): - """Faster RCNN with Resnet101 FPN feature extractor.""" - - def __init__(self, - is_training, - first_stage_features_stride=16, - batch_norm_trainable=True, - conv_hyperparams=None, - weight_decay=0.0, - fpn_min_level=2, - fpn_max_level=6, - additional_layer_depth=256, - override_base_feature_extractor_hyperparams=False): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - conv_hyperparams: See base class. - weight_decay: See base class. - fpn_min_level: See base class. - fpn_max_level: See base class. - additional_layer_depth: See base class. - override_base_feature_extractor_hyperparams: See base class. - """ - super(FasterRCNNResnet101FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - first_stage_features_stride=first_stage_features_stride, - conv_hyperparams=conv_hyperparams, - resnet_v1_base_model=resnet_v1.resnet_v1_101, - resnet_v1_base_model_name='resnet_v1_101', - batch_norm_trainable=batch_norm_trainable, - weight_decay=weight_decay, - fpn_min_level=fpn_min_level, - fpn_max_level=fpn_max_level, - additional_layer_depth=additional_layer_depth, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - - -class FasterRCNNResnet152FpnKerasFeatureExtractor( - FasterRCNNResnetV1FpnKerasFeatureExtractor): - """Faster RCNN with Resnet152 FPN feature extractor.""" - - def __init__(self, - is_training, - first_stage_features_stride=16, - batch_norm_trainable=True, - conv_hyperparams=None, - weight_decay=0.0, - fpn_min_level=2, - fpn_max_level=6, - additional_layer_depth=256, - override_base_feature_extractor_hyperparams=False): - """Constructor. - - Args: - is_training: See base class. - first_stage_features_stride: See base class. - batch_norm_trainable: See base class. - conv_hyperparams: See base class. - weight_decay: See base class. - fpn_min_level: See base class. - fpn_max_level: See base class. - additional_layer_depth: See base class. - override_base_feature_extractor_hyperparams: See base class. - """ - super(FasterRCNNResnet152FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - first_stage_features_stride=first_stage_features_stride, - conv_hyperparams=conv_hyperparams, - resnet_v1_base_model=resnet_v1.resnet_v1_152, - resnet_v1_base_model_name='resnet_v1_152', - batch_norm_trainable=batch_norm_trainable, - weight_decay=weight_decay, - fpn_min_level=fpn_min_level, - fpn_max_level=fpn_max_level, - additional_layer_depth=additional_layer_depth, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) diff --git a/research/object_detection/models/faster_rcnn_resnet_v1_fpn_keras_feature_extractor_tf2_test.py b/research/object_detection/models/faster_rcnn_resnet_v1_fpn_keras_feature_extractor_tf2_test.py deleted file mode 100644 index d0a0813cf65..00000000000 --- a/research/object_detection/models/faster_rcnn_resnet_v1_fpn_keras_feature_extractor_tf2_test.py +++ /dev/null @@ -1,94 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for models.faster_rcnn_resnet_v1_fpn_keras_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import hyperparams_builder -from object_detection.models import faster_rcnn_resnet_v1_fpn_keras_feature_extractor as frcnn_res_fpn -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class FasterRCNNResnetV1FpnKerasFeatureExtractorTest(tf.test.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Parse(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def _build_feature_extractor(self): - return frcnn_res_fpn.FasterRCNNResnet50FpnKerasFeatureExtractor( - is_training=False, - conv_hyperparams=self._build_conv_hyperparams(), - first_stage_features_stride=16, - batch_norm_trainable=False, - weight_decay=0.0) - - def test_extract_proposal_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor() - preprocessed_inputs = tf.random_uniform( - [2, 448, 448, 3], maxval=255, dtype=tf.float32) - rpn_feature_maps = feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - features_shapes = [tf.shape(rpn_feature_map) - for rpn_feature_map in rpn_feature_maps] - - self.assertAllEqual(features_shapes[0].numpy(), [2, 112, 112, 256]) - self.assertAllEqual(features_shapes[1].numpy(), [2, 56, 56, 256]) - self.assertAllEqual(features_shapes[2].numpy(), [2, 28, 28, 256]) - self.assertAllEqual(features_shapes[3].numpy(), [2, 14, 14, 256]) - self.assertAllEqual(features_shapes[4].numpy(), [2, 7, 7, 256]) - - def test_extract_proposal_features_half_size_input(self): - feature_extractor = self._build_feature_extractor() - preprocessed_inputs = tf.random_uniform( - [2, 224, 224, 3], maxval=255, dtype=tf.float32) - rpn_feature_maps = feature_extractor.get_proposal_feature_extractor_model( - name='TestScope')(preprocessed_inputs) - features_shapes = [tf.shape(rpn_feature_map) - for rpn_feature_map in rpn_feature_maps] - - self.assertAllEqual(features_shapes[0].numpy(), [2, 56, 56, 256]) - self.assertAllEqual(features_shapes[1].numpy(), [2, 28, 28, 256]) - self.assertAllEqual(features_shapes[2].numpy(), [2, 14, 14, 256]) - self.assertAllEqual(features_shapes[3].numpy(), [2, 7, 7, 256]) - self.assertAllEqual(features_shapes[4].numpy(), [2, 4, 4, 256]) - - def test_extract_box_classifier_features_returns_expected_size(self): - feature_extractor = self._build_feature_extractor() - proposal_feature_maps = tf.random_uniform( - [3, 7, 7, 1024], maxval=255, dtype=tf.float32) - model = feature_extractor.get_box_classifier_feature_extractor_model( - name='TestScope') - proposal_classifier_features = ( - model(proposal_feature_maps)) - features_shape = tf.shape(proposal_classifier_features) - - self.assertAllEqual(features_shape.numpy(), [3, 1, 1, 1024]) diff --git a/research/object_detection/models/feature_map_generators.py b/research/object_detection/models/feature_map_generators.py deleted file mode 100644 index f0a04c1f767..00000000000 --- a/research/object_detection/models/feature_map_generators.py +++ /dev/null @@ -1,826 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Functions to generate a list of feature maps based on image features. - -Provides several feature map generators that can be used to build object -detection feature extractors. - -Object detection feature extractors usually are built by stacking two components -- A base feature extractor such as Inception V3 and a feature map generator. -Feature map generators build on the base feature extractors and produce a list -of final feature maps. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import collections -import functools -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf -import tf_slim as slim -from object_detection.utils import ops -from object_detection.utils import shape_utils - -# Activation bound used for TPU v1. Activations will be clipped to -# [-ACTIVATION_BOUND, ACTIVATION_BOUND] when training with -# use_bounded_activations enabled. -ACTIVATION_BOUND = 6.0 - - -def get_depth_fn(depth_multiplier, min_depth): - """Builds a callable to compute depth (output channels) of conv filters. - - Args: - depth_multiplier: a multiplier for the nominal depth. - min_depth: a lower bound on the depth of filters. - - Returns: - A callable that takes in a nominal depth and returns the depth to use. - """ - def multiply_depth(depth): - new_depth = int(depth * depth_multiplier) - return max(new_depth, min_depth) - return multiply_depth - - -def create_conv_block( - use_depthwise, kernel_size, padding, stride, layer_name, conv_hyperparams, - is_training, freeze_batchnorm, depth): - """Create Keras layers for depthwise & non-depthwise convolutions. - - Args: - use_depthwise: Whether to use depthwise separable conv instead of regular - conv. - kernel_size: A list of length 2: [kernel_height, kernel_width] of the - filters. Can be an int if both values are the same. - padding: One of 'VALID' or 'SAME'. - stride: A list of length 2: [stride_height, stride_width], specifying the - convolution stride. Can be an int if both strides are the same. - layer_name: String. The name of the layer. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - is_training: Indicates whether the feature generator is in training mode. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - depth: Depth of output feature maps. - - Returns: - A list of conv layers. - """ - layers = [] - if use_depthwise: - kwargs = conv_hyperparams.params() - # Both the regularizer and initializer apply to the depthwise layer, - # so we remap the kernel_* to depthwise_* here. - kwargs['depthwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['depthwise_initializer'] = kwargs['kernel_initializer'] - layers.append( - tf.keras.layers.SeparableConv2D( - depth, [kernel_size, kernel_size], - depth_multiplier=1, - padding=padding, - strides=stride, - name=layer_name + '_depthwise_conv', - **kwargs)) - else: - layers.append(tf.keras.layers.Conv2D( - depth, - [kernel_size, kernel_size], - padding=padding, - strides=stride, - name=layer_name + '_conv', - **conv_hyperparams.params())) - layers.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name=layer_name + '_batchnorm')) - layers.append( - conv_hyperparams.build_activation_layer( - name=layer_name)) - return layers - - -class KerasMultiResolutionFeatureMaps(tf.keras.Model): - """Generates multi resolution feature maps from input image features. - - A Keras model that generates multi-scale feature maps for detection as in the - SSD papers by Liu et al: https://arxiv.org/pdf/1512.02325v2.pdf, See Sec 2.1. - - More specifically, when called on inputs it performs the following two tasks: - 1) If a layer name is provided in the configuration, returns that layer as a - feature map. - 2) If a layer name is left as an empty string, constructs a new feature map - based on the spatial shape and depth configuration. Note that the current - implementation only supports generating new layers using convolution of - stride 2 resulting in a spatial resolution reduction by a factor of 2. - By default convolution kernel size is set to 3, and it can be customized - by caller. - - An example of the configuration for Inception V3: - { - 'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 128] - } - - When this feature generator object is called on input image_features: - Args: - image_features: A dictionary of handles to activation tensors from the - base feature extractor. - - Returns: - feature_maps: an OrderedDict mapping keys (feature map names) to - tensors where each tensor has shape [batch, height_i, width_i, depth_i]. - """ - - def __init__(self, - feature_map_layout, - depth_multiplier, - min_depth, - insert_1x1_conv, - is_training, - conv_hyperparams, - freeze_batchnorm, - name=None): - """Constructor. - - Args: - feature_map_layout: Dictionary of specifications for the feature map - layouts in the following format (Inception V2/V3 respectively): - { - 'from_layer': ['Mixed_3c', 'Mixed_4c', 'Mixed_5c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 128] - } - or - { - 'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 128] - } - If 'from_layer' is specified, the specified feature map is directly used - as a box predictor layer, and the layer_depth is directly infered from - the feature map (instead of using the provided 'layer_depth' parameter). - In this case, our convention is to set 'layer_depth' to -1 for clarity. - Otherwise, if 'from_layer' is an empty string, then the box predictor - layer will be built from the previous layer using convolution - operations. Note that the current implementation only supports - generating new layers using convolutions of stride 2 (resulting in a - spatial resolution reduction by a factor of 2), and will be extended to - a more flexible design. Convolution kernel size is set to 3 by default, - and can be customized by 'conv_kernel_size' parameter (similarily, - 'conv_kernel_size' should be set to -1 if 'from_layer' is specified). - The created convolution operation will be a normal 2D convolution by - default, and a depthwise convolution followed by 1x1 convolution if - 'use_depthwise' is set to True. - depth_multiplier: Depth multiplier for convolutional layers. - min_depth: Minimum depth for convolutional layers. - insert_1x1_conv: A boolean indicating whether an additional 1x1 - convolution should be inserted before shrinking the feature map. - is_training: Indicates whether the feature generator is in training mode. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - name: A string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(KerasMultiResolutionFeatureMaps, self).__init__(name=name) - - self.feature_map_layout = feature_map_layout - self.convolutions = [] - - depth_fn = get_depth_fn(depth_multiplier, min_depth) - - base_from_layer = '' - use_explicit_padding = False - if 'use_explicit_padding' in feature_map_layout: - use_explicit_padding = feature_map_layout['use_explicit_padding'] - use_depthwise = False - if 'use_depthwise' in feature_map_layout: - use_depthwise = feature_map_layout['use_depthwise'] - for index, from_layer in enumerate(feature_map_layout['from_layer']): - net = [] - layer_depth = feature_map_layout['layer_depth'][index] - conv_kernel_size = 3 - if 'conv_kernel_size' in feature_map_layout: - conv_kernel_size = feature_map_layout['conv_kernel_size'][index] - if from_layer: - base_from_layer = from_layer - else: - if insert_1x1_conv: - layer_name = '{}_1_Conv2d_{}_1x1_{}'.format( - base_from_layer, index, depth_fn(layer_depth // 2)) - net.append(tf.keras.layers.Conv2D(depth_fn(layer_depth // 2), - [1, 1], - padding='SAME', - strides=1, - name=layer_name + '_conv', - **conv_hyperparams.params())) - net.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name=layer_name + '_batchnorm')) - net.append( - conv_hyperparams.build_activation_layer( - name=layer_name)) - - layer_name = '{}_2_Conv2d_{}_{}x{}_s2_{}'.format( - base_from_layer, index, conv_kernel_size, conv_kernel_size, - depth_fn(layer_depth)) - stride = 2 - padding = 'SAME' - if use_explicit_padding: - padding = 'VALID' - # We define this function here while capturing the value of - # conv_kernel_size, to avoid holding a reference to the loop variable - # conv_kernel_size inside of a lambda function - def fixed_padding(features, kernel_size=conv_kernel_size): - return ops.fixed_padding(features, kernel_size) - net.append(tf.keras.layers.Lambda(fixed_padding)) - # TODO(rathodv): Add some utilities to simplify the creation of - # Depthwise & non-depthwise convolutions w/ normalization & activations - if use_depthwise: - net.append(tf.keras.layers.DepthwiseConv2D( - [conv_kernel_size, conv_kernel_size], - depth_multiplier=1, - padding=padding, - strides=stride, - name=layer_name + '_depthwise_conv', - **conv_hyperparams.params())) - net.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name=layer_name + '_depthwise_batchnorm')) - net.append( - conv_hyperparams.build_activation_layer( - name=layer_name + '_depthwise')) - - net.append(tf.keras.layers.Conv2D(depth_fn(layer_depth), [1, 1], - padding='SAME', - strides=1, - name=layer_name + '_conv', - **conv_hyperparams.params())) - net.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name=layer_name + '_batchnorm')) - net.append( - conv_hyperparams.build_activation_layer( - name=layer_name)) - - else: - net.append(tf.keras.layers.Conv2D( - depth_fn(layer_depth), - [conv_kernel_size, conv_kernel_size], - padding=padding, - strides=stride, - name=layer_name + '_conv', - **conv_hyperparams.params())) - net.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name=layer_name + '_batchnorm')) - net.append( - conv_hyperparams.build_activation_layer( - name=layer_name)) - - # Until certain bugs are fixed in checkpointable lists, - # this net must be appended only once it's been filled with layers - self.convolutions.append(net) - - def call(self, image_features): - """Generate the multi-resolution feature maps. - - Executed when calling the `.__call__` method on input. - - Args: - image_features: A dictionary of handles to activation tensors from the - base feature extractor. - - Returns: - feature_maps: an OrderedDict mapping keys (feature map names) to - tensors where each tensor has shape [batch, height_i, width_i, depth_i]. - """ - feature_maps = [] - feature_map_keys = [] - - for index, from_layer in enumerate(self.feature_map_layout['from_layer']): - if from_layer: - feature_map = image_features[from_layer] - feature_map_keys.append(from_layer) - else: - feature_map = feature_maps[-1] - for layer in self.convolutions[index]: - feature_map = layer(feature_map) - layer_name = self.convolutions[index][-1].name - feature_map_keys.append(layer_name) - feature_maps.append(feature_map) - return collections.OrderedDict( - [(x, y) for (x, y) in zip(feature_map_keys, feature_maps)]) - - -def multi_resolution_feature_maps(feature_map_layout, depth_multiplier, - min_depth, insert_1x1_conv, image_features, - pool_residual=False): - """Generates multi resolution feature maps from input image features. - - Generates multi-scale feature maps for detection as in the SSD papers by - Liu et al: https://arxiv.org/pdf/1512.02325v2.pdf, See Sec 2.1. - - More specifically, it performs the following two tasks: - 1) If a layer name is provided in the configuration, returns that layer as a - feature map. - 2) If a layer name is left as an empty string, constructs a new feature map - based on the spatial shape and depth configuration. Note that the current - implementation only supports generating new layers using convolution of - stride 2 resulting in a spatial resolution reduction by a factor of 2. - By default convolution kernel size is set to 3, and it can be customized - by caller. - - An example of the configuration for Inception V3: - { - 'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 128] - } - - Args: - feature_map_layout: Dictionary of specifications for the feature map - layouts in the following format (Inception V2/V3 respectively): - { - 'from_layer': ['Mixed_3c', 'Mixed_4c', 'Mixed_5c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 128] - } - or - { - 'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 128] - } - If 'from_layer' is specified, the specified feature map is directly used - as a box predictor layer, and the layer_depth is directly infered from the - feature map (instead of using the provided 'layer_depth' parameter). In - this case, our convention is to set 'layer_depth' to -1 for clarity. - Otherwise, if 'from_layer' is an empty string, then the box predictor - layer will be built from the previous layer using convolution operations. - Note that the current implementation only supports generating new layers - using convolutions of stride 2 (resulting in a spatial resolution - reduction by a factor of 2), and will be extended to a more flexible - design. Convolution kernel size is set to 3 by default, and can be - customized by 'conv_kernel_size' parameter (similarily, 'conv_kernel_size' - should be set to -1 if 'from_layer' is specified). The created convolution - operation will be a normal 2D convolution by default, and a depthwise - convolution followed by 1x1 convolution if 'use_depthwise' is set to True. - depth_multiplier: Depth multiplier for convolutional layers. - min_depth: Minimum depth for convolutional layers. - insert_1x1_conv: A boolean indicating whether an additional 1x1 convolution - should be inserted before shrinking the feature map. - image_features: A dictionary of handles to activation tensors from the - base feature extractor. - pool_residual: Whether to add an average pooling layer followed by a - residual connection between subsequent feature maps when the channel - depth match. For example, with option 'layer_depth': [-1, 512, 256, 256], - a pooling and residual layer is added between the third and forth feature - map. This option is better used with Weight Shared Convolution Box - Predictor when all feature maps have the same channel depth to encourage - more consistent features across multi-scale feature maps. - - Returns: - feature_maps: an OrderedDict mapping keys (feature map names) to - tensors where each tensor has shape [batch, height_i, width_i, depth_i]. - - Raises: - ValueError: if the number entries in 'from_layer' and - 'layer_depth' do not match. - ValueError: if the generated layer does not have the same resolution - as specified. - """ - depth_fn = get_depth_fn(depth_multiplier, min_depth) - - feature_map_keys = [] - feature_maps = [] - base_from_layer = '' - use_explicit_padding = False - if 'use_explicit_padding' in feature_map_layout: - use_explicit_padding = feature_map_layout['use_explicit_padding'] - use_depthwise = False - if 'use_depthwise' in feature_map_layout: - use_depthwise = feature_map_layout['use_depthwise'] - for index, from_layer in enumerate(feature_map_layout['from_layer']): - layer_depth = feature_map_layout['layer_depth'][index] - conv_kernel_size = 3 - if 'conv_kernel_size' in feature_map_layout: - conv_kernel_size = feature_map_layout['conv_kernel_size'][index] - if from_layer: - feature_map = image_features[from_layer] - base_from_layer = from_layer - feature_map_keys.append(from_layer) - else: - pre_layer = feature_maps[-1] - pre_layer_depth = pre_layer.get_shape().as_list()[3] - intermediate_layer = pre_layer - if insert_1x1_conv: - layer_name = '{}_1_Conv2d_{}_1x1_{}'.format( - base_from_layer, index, depth_fn(layer_depth // 2)) - intermediate_layer = slim.conv2d( - pre_layer, - depth_fn(layer_depth // 2), [1, 1], - padding='SAME', - stride=1, - scope=layer_name) - layer_name = '{}_2_Conv2d_{}_{}x{}_s2_{}'.format( - base_from_layer, index, conv_kernel_size, conv_kernel_size, - depth_fn(layer_depth)) - stride = 2 - padding = 'SAME' - if use_explicit_padding: - padding = 'VALID' - intermediate_layer = ops.fixed_padding( - intermediate_layer, conv_kernel_size) - if use_depthwise: - feature_map = slim.separable_conv2d( - intermediate_layer, - None, [conv_kernel_size, conv_kernel_size], - depth_multiplier=1, - padding=padding, - stride=stride, - scope=layer_name + '_depthwise') - feature_map = slim.conv2d( - feature_map, - depth_fn(layer_depth), [1, 1], - padding='SAME', - stride=1, - scope=layer_name) - if pool_residual and pre_layer_depth == depth_fn(layer_depth): - if use_explicit_padding: - pre_layer = ops.fixed_padding(pre_layer, conv_kernel_size) - feature_map += slim.avg_pool2d( - pre_layer, [conv_kernel_size, conv_kernel_size], - padding=padding, - stride=2, - scope=layer_name + '_pool') - else: - feature_map = slim.conv2d( - intermediate_layer, - depth_fn(layer_depth), [conv_kernel_size, conv_kernel_size], - padding=padding, - stride=stride, - scope=layer_name) - feature_map_keys.append(layer_name) - feature_maps.append(feature_map) - return collections.OrderedDict( - [(x, y) for (x, y) in zip(feature_map_keys, feature_maps)]) - - -class KerasFpnTopDownFeatureMaps(tf.keras.Model): - """Generates Keras based `top-down` feature maps for Feature Pyramid Networks. - - See https://arxiv.org/abs/1612.03144 for details. - """ - - def __init__(self, - num_levels, - depth, - is_training, - conv_hyperparams, - freeze_batchnorm, - use_depthwise=False, - use_explicit_padding=False, - use_bounded_activations=False, - use_native_resize_op=False, - scope=None, - name=None): - """Constructor. - - Args: - num_levels: the number of image features. - depth: depth of output feature maps. - is_training: Indicates whether the feature generator is in training mode. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - use_depthwise: whether to use depthwise separable conv instead of regular - conv. - use_explicit_padding: whether to use explicit padding. - use_bounded_activations: Whether or not to clip activations to range - [-ACTIVATION_BOUND, ACTIVATION_BOUND]. Bounded activations better lend - themselves to quantized inference. - use_native_resize_op: If True, uses tf.image.resize_nearest_neighbor op - for the upsampling process instead of reshape and broadcasting - implementation. - scope: A scope name to wrap this op under. - name: A string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(KerasFpnTopDownFeatureMaps, self).__init__(name=name) - - self.scope = scope if scope else 'top_down' - self.top_layers = [] - self.residual_blocks = [] - self.top_down_blocks = [] - self.reshape_blocks = [] - self.conv_layers = [] - - padding = 'VALID' if use_explicit_padding else 'SAME' - stride = 1 - kernel_size = 3 - def clip_by_value(features): - return tf.clip_by_value(features, -ACTIVATION_BOUND, ACTIVATION_BOUND) - - # top layers - self.top_layers.append(tf.keras.layers.Conv2D( - depth, [1, 1], strides=stride, padding=padding, - name='projection_%d' % num_levels, - **conv_hyperparams.params(use_bias=True))) - if use_bounded_activations: - self.top_layers.append(tf.keras.layers.Lambda( - clip_by_value, name='clip_by_value')) - - for level in reversed(list(range(num_levels - 1))): - # to generate residual from image features - residual_net = [] - # to preprocess top_down (the image feature map from last layer) - top_down_net = [] - # to reshape top_down according to residual if necessary - reshaped_residual = [] - # to apply convolution layers to feature map - conv_net = [] - - # residual block - residual_net.append(tf.keras.layers.Conv2D( - depth, [1, 1], padding=padding, strides=1, - name='projection_%d' % (level + 1), - **conv_hyperparams.params(use_bias=True))) - if use_bounded_activations: - residual_net.append(tf.keras.layers.Lambda( - clip_by_value, name='clip_by_value')) - - # top-down block - # TODO (b/128922690): clean-up of ops.nearest_neighbor_upsampling - if use_native_resize_op: - def resize_nearest_neighbor(image): - image_shape = shape_utils.combined_static_and_dynamic_shape(image) - return tf.image.resize_nearest_neighbor( - image, [image_shape[1] * 2, image_shape[2] * 2]) - top_down_net.append(tf.keras.layers.Lambda( - resize_nearest_neighbor, name='nearest_neighbor_upsampling')) - else: - def nearest_neighbor_upsampling(image): - return ops.nearest_neighbor_upsampling(image, scale=2) - top_down_net.append(tf.keras.layers.Lambda( - nearest_neighbor_upsampling, name='nearest_neighbor_upsampling')) - - # reshape block - if use_explicit_padding: - def reshape(inputs): - residual_shape = tf.shape(inputs[0]) - return inputs[1][:, :residual_shape[1], :residual_shape[2], :] - reshaped_residual.append( - tf.keras.layers.Lambda(reshape, name='reshape')) - - # down layers - if use_bounded_activations: - conv_net.append(tf.keras.layers.Lambda( - clip_by_value, name='clip_by_value')) - - if use_explicit_padding: - def fixed_padding(features, kernel_size=kernel_size): - return ops.fixed_padding(features, kernel_size) - conv_net.append(tf.keras.layers.Lambda( - fixed_padding, name='fixed_padding')) - - layer_name = 'smoothing_%d' % (level + 1) - conv_block = create_conv_block( - use_depthwise, kernel_size, padding, stride, layer_name, - conv_hyperparams, is_training, freeze_batchnorm, depth) - conv_net.extend(conv_block) - - self.residual_blocks.append(residual_net) - self.top_down_blocks.append(top_down_net) - self.reshape_blocks.append(reshaped_residual) - self.conv_layers.append(conv_net) - - def call(self, image_features): - """Generate the multi-resolution feature maps. - - Executed when calling the `.__call__` method on input. - - Args: - image_features: list of tuples of (tensor_name, image_feature_tensor). - Spatial resolutions of succesive tensors must reduce exactly by a factor - of 2. - - Returns: - feature_maps: an OrderedDict mapping keys (feature map names) to - tensors where each tensor has shape [batch, height_i, width_i, depth_i]. - """ - output_feature_maps_list = [] - output_feature_map_keys = [] - - with tf.name_scope(self.scope): - top_down = image_features[-1][1] - for layer in self.top_layers: - top_down = layer(top_down) - output_feature_maps_list.append(top_down) - output_feature_map_keys.append('top_down_%s' % image_features[-1][0]) - - num_levels = len(image_features) - for index, level in enumerate(reversed(list(range(num_levels - 1)))): - residual = image_features[level][1] - top_down = output_feature_maps_list[-1] - for layer in self.residual_blocks[index]: - residual = layer(residual) - for layer in self.top_down_blocks[index]: - top_down = layer(top_down) - for layer in self.reshape_blocks[index]: - top_down = layer([residual, top_down]) - top_down += residual - for layer in self.conv_layers[index]: - top_down = layer(top_down) - output_feature_maps_list.append(top_down) - output_feature_map_keys.append('top_down_%s' % image_features[level][0]) - return collections.OrderedDict(reversed( - list(zip(output_feature_map_keys, output_feature_maps_list)))) - - -def fpn_top_down_feature_maps(image_features, - depth, - use_depthwise=False, - use_explicit_padding=False, - use_bounded_activations=False, - scope=None, - use_native_resize_op=False): - """Generates `top-down` feature maps for Feature Pyramid Networks. - - See https://arxiv.org/abs/1612.03144 for details. - - Args: - image_features: list of tuples of (tensor_name, image_feature_tensor). - Spatial resolutions of succesive tensors must reduce exactly by a factor - of 2. - depth: depth of output feature maps. - use_depthwise: whether to use depthwise separable conv instead of regular - conv. - use_explicit_padding: whether to use explicit padding. - use_bounded_activations: Whether or not to clip activations to range - [-ACTIVATION_BOUND, ACTIVATION_BOUND]. Bounded activations better lend - themselves to quantized inference. - scope: A scope name to wrap this op under. - use_native_resize_op: If True, uses tf.image.resize_nearest_neighbor op for - the upsampling process instead of reshape and broadcasting implementation. - - Returns: - feature_maps: an OrderedDict mapping keys (feature map names) to - tensors where each tensor has shape [batch, height_i, width_i, depth_i]. - """ - with tf.name_scope(scope, 'top_down'): - num_levels = len(image_features) - output_feature_maps_list = [] - output_feature_map_keys = [] - padding = 'VALID' if use_explicit_padding else 'SAME' - kernel_size = 3 - with slim.arg_scope( - [slim.conv2d, slim.separable_conv2d], padding=padding, stride=1): - top_down = slim.conv2d( - image_features[-1][1], - depth, [1, 1], activation_fn=None, normalizer_fn=None, - scope='projection_%d' % num_levels) - if use_bounded_activations: - top_down = tf.clip_by_value(top_down, -ACTIVATION_BOUND, - ACTIVATION_BOUND) - output_feature_maps_list.append(top_down) - output_feature_map_keys.append( - 'top_down_%s' % image_features[-1][0]) - - for level in reversed(list(range(num_levels - 1))): - if use_native_resize_op: - with tf.name_scope('nearest_neighbor_upsampling'): - top_down_shape = shape_utils.combined_static_and_dynamic_shape( - top_down) - top_down = tf.image.resize_nearest_neighbor( - top_down, [top_down_shape[1] * 2, top_down_shape[2] * 2]) - else: - top_down = ops.nearest_neighbor_upsampling(top_down, scale=2) - residual = slim.conv2d( - image_features[level][1], depth, [1, 1], - activation_fn=None, normalizer_fn=None, - scope='projection_%d' % (level + 1)) - if use_bounded_activations: - residual = tf.clip_by_value(residual, -ACTIVATION_BOUND, - ACTIVATION_BOUND) - if use_explicit_padding: - # slice top_down to the same shape as residual - residual_shape = tf.shape(residual) - top_down = top_down[:, :residual_shape[1], :residual_shape[2], :] - top_down += residual - if use_bounded_activations: - top_down = tf.clip_by_value(top_down, -ACTIVATION_BOUND, - ACTIVATION_BOUND) - if use_depthwise: - conv_op = functools.partial(slim.separable_conv2d, depth_multiplier=1) - else: - conv_op = slim.conv2d - pre_output = top_down - if use_explicit_padding: - pre_output = ops.fixed_padding(pre_output, kernel_size) - output_feature_maps_list.append(conv_op( - pre_output, - depth, [kernel_size, kernel_size], - scope='smoothing_%d' % (level + 1))) - output_feature_map_keys.append('top_down_%s' % image_features[level][0]) - return collections.OrderedDict(reversed( - list(zip(output_feature_map_keys, output_feature_maps_list)))) - - -def pooling_pyramid_feature_maps(base_feature_map_depth, num_layers, - image_features, replace_pool_with_conv=False): - """Generates pooling pyramid feature maps. - - The pooling pyramid feature maps is motivated by - multi_resolution_feature_maps. The main difference are that it is simpler and - reduces the number of free parameters. - - More specifically: - - Instead of using convolutions to shrink the feature map, it uses max - pooling, therefore totally gets rid of the parameters in convolution. - - By pooling feature from larger map up to a single cell, it generates - features in the same feature space. - - Instead of independently making box predictions from individual maps, it - shares the same classifier across different feature maps, therefore reduces - the "mis-calibration" across different scales. - - See go/ppn-detection for more details. - - Args: - base_feature_map_depth: Depth of the base feature before the max pooling. - num_layers: Number of layers used to make predictions. They are pooled - from the base feature. - image_features: A dictionary of handles to activation tensors from the - feature extractor. - replace_pool_with_conv: Whether or not to replace pooling operations with - convolutions in the PPN. Default is False. - - Returns: - feature_maps: an OrderedDict mapping keys (feature map names) to - tensors where each tensor has shape [batch, height_i, width_i, depth_i]. - Raises: - ValueError: image_features does not contain exactly one entry - """ - if len(image_features) != 1: - raise ValueError('image_features should be a dictionary of length 1.') - image_features = image_features[list(image_features.keys())[0]] - - feature_map_keys = [] - feature_maps = [] - feature_map_key = 'Base_Conv2d_1x1_%d' % base_feature_map_depth - if base_feature_map_depth > 0: - image_features = slim.conv2d( - image_features, - base_feature_map_depth, - [1, 1], # kernel size - padding='SAME', stride=1, scope=feature_map_key) - # Add a 1x1 max-pooling node (a no op node) immediately after the conv2d for - # TPU v1 compatibility. Without the following dummy op, TPU runtime - # compiler will combine the convolution with one max-pooling below into a - # single cycle, so getting the conv2d feature becomes impossible. - image_features = slim.max_pool2d( - image_features, [1, 1], padding='SAME', stride=1, scope=feature_map_key) - feature_map_keys.append(feature_map_key) - feature_maps.append(image_features) - feature_map = image_features - if replace_pool_with_conv: - with slim.arg_scope([slim.conv2d], padding='SAME', stride=2): - for i in range(num_layers - 1): - feature_map_key = 'Conv2d_{}_3x3_s2_{}'.format(i, - base_feature_map_depth) - feature_map = slim.conv2d( - feature_map, base_feature_map_depth, [3, 3], scope=feature_map_key) - feature_map_keys.append(feature_map_key) - feature_maps.append(feature_map) - else: - with slim.arg_scope([slim.max_pool2d], padding='SAME', stride=2): - for i in range(num_layers - 1): - feature_map_key = 'MaxPool2d_%d_2x2' % i - feature_map = slim.max_pool2d( - feature_map, [2, 2], padding='SAME', scope=feature_map_key) - feature_map_keys.append(feature_map_key) - feature_maps.append(feature_map) - return collections.OrderedDict( - [(x, y) for (x, y) in zip(feature_map_keys, feature_maps)]) diff --git a/research/object_detection/models/feature_map_generators_test.py b/research/object_detection/models/feature_map_generators_test.py deleted file mode 100644 index 951e7760bd8..00000000000 --- a/research/object_detection/models/feature_map_generators_test.py +++ /dev/null @@ -1,842 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for feature map generators.""" -import unittest -from absl.testing import parameterized - -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import hyperparams_builder -from object_detection.models import feature_map_generators -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import test_utils -from object_detection.utils import tf_version - -INCEPTION_V2_LAYOUT = { - 'from_layer': ['Mixed_3c', 'Mixed_4c', 'Mixed_5c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 256], - 'anchor_strides': [16, 32, 64, -1, -1, -1], - 'layer_target_norm': [20.0, -1, -1, -1, -1, -1], -} - -INCEPTION_V3_LAYOUT = { - 'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', ''], - 'layer_depth': [-1, -1, -1, 512, 256, 128], - 'anchor_strides': [16, 32, 64, -1, -1, -1], - 'aspect_ratios': [1.0, 2.0, 1.0/2, 3.0, 1.0/3] -} - -EMBEDDED_SSD_MOBILENET_V1_LAYOUT = { - 'from_layer': ['Conv2d_11_pointwise', 'Conv2d_13_pointwise', '', '', ''], - 'layer_depth': [-1, -1, 512, 256, 256], - 'conv_kernel_size': [-1, -1, 3, 3, 2], -} - -SSD_MOBILENET_V1_WEIGHT_SHARED_LAYOUT = { - 'from_layer': ['Conv2d_13_pointwise', '', '', ''], - 'layer_depth': [-1, 256, 256, 256], -} - - -class MultiResolutionFeatureMapGeneratorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def _build_feature_map_generator(self, feature_map_layout, - pool_residual=False): - if tf_version.is_tf2(): - return feature_map_generators.KerasMultiResolutionFeatureMaps( - feature_map_layout=feature_map_layout, - depth_multiplier=1, - min_depth=32, - insert_1x1_conv=True, - freeze_batchnorm=False, - is_training=True, - conv_hyperparams=self._build_conv_hyperparams(), - name='FeatureMaps' - ) - else: - def feature_map_generator(image_features): - return feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=1, - min_depth=32, - insert_1x1_conv=True, - image_features=image_features, - pool_residual=pool_residual) - return feature_map_generator - - def test_get_expected_feature_map_shapes_with_inception_v2(self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Mixed_3c': tf.random_uniform([4, 28, 28, 256], dtype=tf.float32), - 'Mixed_4c': tf.random_uniform([4, 14, 14, 576], dtype=tf.float32), - 'Mixed_5c': tf.random_uniform([4, 7, 7, 1024], dtype=tf.float32) - } - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=INCEPTION_V2_LAYOUT) - def graph_fn(): - feature_maps = feature_map_generator(image_features) - return feature_maps - - expected_feature_map_shapes = { - 'Mixed_3c': (4, 28, 28, 256), - 'Mixed_4c': (4, 14, 14, 576), - 'Mixed_5c': (4, 7, 7, 1024), - 'Mixed_5c_2_Conv2d_3_3x3_s2_512': (4, 4, 4, 512), - 'Mixed_5c_2_Conv2d_4_3x3_s2_256': (4, 2, 2, 256), - 'Mixed_5c_2_Conv2d_5_3x3_s2_256': (4, 1, 1, 256)} - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_feature_map_shapes_with_inception_v2_use_depthwise( - self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Mixed_3c': tf.random_uniform([4, 28, 28, 256], dtype=tf.float32), - 'Mixed_4c': tf.random_uniform([4, 14, 14, 576], dtype=tf.float32), - 'Mixed_5c': tf.random_uniform([4, 7, 7, 1024], dtype=tf.float32) - } - layout_copy = INCEPTION_V2_LAYOUT.copy() - layout_copy['use_depthwise'] = True - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=layout_copy) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'Mixed_3c': (4, 28, 28, 256), - 'Mixed_4c': (4, 14, 14, 576), - 'Mixed_5c': (4, 7, 7, 1024), - 'Mixed_5c_2_Conv2d_3_3x3_s2_512': (4, 4, 4, 512), - 'Mixed_5c_2_Conv2d_4_3x3_s2_256': (4, 2, 2, 256), - 'Mixed_5c_2_Conv2d_5_3x3_s2_256': (4, 1, 1, 256)} - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_feature_map_shapes_use_explicit_padding(self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Mixed_3c': tf.random_uniform([4, 28, 28, 256], dtype=tf.float32), - 'Mixed_4c': tf.random_uniform([4, 14, 14, 576], dtype=tf.float32), - 'Mixed_5c': tf.random_uniform([4, 7, 7, 1024], dtype=tf.float32) - } - layout_copy = INCEPTION_V2_LAYOUT.copy() - layout_copy['use_explicit_padding'] = True - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=layout_copy, - ) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'Mixed_3c': (4, 28, 28, 256), - 'Mixed_4c': (4, 14, 14, 576), - 'Mixed_5c': (4, 7, 7, 1024), - 'Mixed_5c_2_Conv2d_3_3x3_s2_512': (4, 4, 4, 512), - 'Mixed_5c_2_Conv2d_4_3x3_s2_256': (4, 2, 2, 256), - 'Mixed_5c_2_Conv2d_5_3x3_s2_256': (4, 1, 1, 256)} - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_feature_map_shapes_with_inception_v3(self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Mixed_5d': tf.random_uniform([4, 35, 35, 256], dtype=tf.float32), - 'Mixed_6e': tf.random_uniform([4, 17, 17, 576], dtype=tf.float32), - 'Mixed_7c': tf.random_uniform([4, 8, 8, 1024], dtype=tf.float32) - } - - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=INCEPTION_V3_LAYOUT, - ) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'Mixed_5d': (4, 35, 35, 256), - 'Mixed_6e': (4, 17, 17, 576), - 'Mixed_7c': (4, 8, 8, 1024), - 'Mixed_7c_2_Conv2d_3_3x3_s2_512': (4, 4, 4, 512), - 'Mixed_7c_2_Conv2d_4_3x3_s2_256': (4, 2, 2, 256), - 'Mixed_7c_2_Conv2d_5_3x3_s2_128': (4, 1, 1, 128)} - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_feature_map_shapes_with_embedded_ssd_mobilenet_v1( - self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Conv2d_11_pointwise': tf.random_uniform([4, 16, 16, 512], - dtype=tf.float32), - 'Conv2d_13_pointwise': tf.random_uniform([4, 8, 8, 1024], - dtype=tf.float32), - } - - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=EMBEDDED_SSD_MOBILENET_V1_LAYOUT, - ) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'Conv2d_11_pointwise': (4, 16, 16, 512), - 'Conv2d_13_pointwise': (4, 8, 8, 1024), - 'Conv2d_13_pointwise_2_Conv2d_2_3x3_s2_512': (4, 4, 4, 512), - 'Conv2d_13_pointwise_2_Conv2d_3_3x3_s2_256': (4, 2, 2, 256), - 'Conv2d_13_pointwise_2_Conv2d_4_2x2_s2_256': (4, 1, 1, 256)} - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_feature_map_shapes_with_pool_residual_ssd_mobilenet_v1( - self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Conv2d_13_pointwise': tf.random_uniform([4, 8, 8, 1024], - dtype=tf.float32), - } - - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=SSD_MOBILENET_V1_WEIGHT_SHARED_LAYOUT, - pool_residual=True - ) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'Conv2d_13_pointwise': (4, 8, 8, 1024), - 'Conv2d_13_pointwise_2_Conv2d_1_3x3_s2_256': (4, 4, 4, 256), - 'Conv2d_13_pointwise_2_Conv2d_2_3x3_s2_256': (4, 2, 2, 256), - 'Conv2d_13_pointwise_2_Conv2d_3_3x3_s2_256': (4, 1, 1, 256)} - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_variable_names_with_inception_v2(self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Mixed_3c': tf.random_uniform([4, 28, 28, 256], dtype=tf.float32), - 'Mixed_4c': tf.random_uniform([4, 14, 14, 576], dtype=tf.float32), - 'Mixed_5c': tf.random_uniform([4, 7, 7, 1024], dtype=tf.float32) - } - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=INCEPTION_V2_LAYOUT, - ) - def graph_fn(): - return feature_map_generator(image_features) - - self.execute(graph_fn, [], g) - expected_slim_variables = set([ - 'Mixed_5c_1_Conv2d_3_1x1_256/weights', - 'Mixed_5c_1_Conv2d_3_1x1_256/biases', - 'Mixed_5c_2_Conv2d_3_3x3_s2_512/weights', - 'Mixed_5c_2_Conv2d_3_3x3_s2_512/biases', - 'Mixed_5c_1_Conv2d_4_1x1_128/weights', - 'Mixed_5c_1_Conv2d_4_1x1_128/biases', - 'Mixed_5c_2_Conv2d_4_3x3_s2_256/weights', - 'Mixed_5c_2_Conv2d_4_3x3_s2_256/biases', - 'Mixed_5c_1_Conv2d_5_1x1_128/weights', - 'Mixed_5c_1_Conv2d_5_1x1_128/biases', - 'Mixed_5c_2_Conv2d_5_3x3_s2_256/weights', - 'Mixed_5c_2_Conv2d_5_3x3_s2_256/biases', - ]) - - expected_keras_variables = set([ - 'FeatureMaps/Mixed_5c_1_Conv2d_3_1x1_256_conv/kernel', - 'FeatureMaps/Mixed_5c_1_Conv2d_3_1x1_256_conv/bias', - 'FeatureMaps/Mixed_5c_2_Conv2d_3_3x3_s2_512_conv/kernel', - 'FeatureMaps/Mixed_5c_2_Conv2d_3_3x3_s2_512_conv/bias', - 'FeatureMaps/Mixed_5c_1_Conv2d_4_1x1_128_conv/kernel', - 'FeatureMaps/Mixed_5c_1_Conv2d_4_1x1_128_conv/bias', - 'FeatureMaps/Mixed_5c_2_Conv2d_4_3x3_s2_256_conv/kernel', - 'FeatureMaps/Mixed_5c_2_Conv2d_4_3x3_s2_256_conv/bias', - 'FeatureMaps/Mixed_5c_1_Conv2d_5_1x1_128_conv/kernel', - 'FeatureMaps/Mixed_5c_1_Conv2d_5_1x1_128_conv/bias', - 'FeatureMaps/Mixed_5c_2_Conv2d_5_3x3_s2_256_conv/kernel', - 'FeatureMaps/Mixed_5c_2_Conv2d_5_3x3_s2_256_conv/bias', - ]) - - if tf_version.is_tf2(): - actual_variable_set = set( - [var.name.split(':')[0] for var in feature_map_generator.variables]) - self.assertSetEqual(expected_keras_variables, actual_variable_set) - else: - with g.as_default(): - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - self.assertSetEqual(expected_slim_variables, actual_variable_set) - - def test_get_expected_variable_names_with_inception_v2_use_depthwise( - self): - with test_utils.GraphContextOrNone() as g: - image_features = { - 'Mixed_3c': tf.random_uniform([4, 28, 28, 256], dtype=tf.float32), - 'Mixed_4c': tf.random_uniform([4, 14, 14, 576], dtype=tf.float32), - 'Mixed_5c': tf.random_uniform([4, 7, 7, 1024], dtype=tf.float32) - } - layout_copy = INCEPTION_V2_LAYOUT.copy() - layout_copy['use_depthwise'] = True - feature_map_generator = self._build_feature_map_generator( - feature_map_layout=layout_copy, - ) - def graph_fn(): - return feature_map_generator(image_features) - self.execute(graph_fn, [], g) - - expected_slim_variables = set([ - 'Mixed_5c_1_Conv2d_3_1x1_256/weights', - 'Mixed_5c_1_Conv2d_3_1x1_256/biases', - 'Mixed_5c_2_Conv2d_3_3x3_s2_512_depthwise/depthwise_weights', - 'Mixed_5c_2_Conv2d_3_3x3_s2_512_depthwise/biases', - 'Mixed_5c_2_Conv2d_3_3x3_s2_512/weights', - 'Mixed_5c_2_Conv2d_3_3x3_s2_512/biases', - 'Mixed_5c_1_Conv2d_4_1x1_128/weights', - 'Mixed_5c_1_Conv2d_4_1x1_128/biases', - 'Mixed_5c_2_Conv2d_4_3x3_s2_256_depthwise/depthwise_weights', - 'Mixed_5c_2_Conv2d_4_3x3_s2_256_depthwise/biases', - 'Mixed_5c_2_Conv2d_4_3x3_s2_256/weights', - 'Mixed_5c_2_Conv2d_4_3x3_s2_256/biases', - 'Mixed_5c_1_Conv2d_5_1x1_128/weights', - 'Mixed_5c_1_Conv2d_5_1x1_128/biases', - 'Mixed_5c_2_Conv2d_5_3x3_s2_256_depthwise/depthwise_weights', - 'Mixed_5c_2_Conv2d_5_3x3_s2_256_depthwise/biases', - 'Mixed_5c_2_Conv2d_5_3x3_s2_256/weights', - 'Mixed_5c_2_Conv2d_5_3x3_s2_256/biases', - ]) - - expected_keras_variables = set([ - 'FeatureMaps/Mixed_5c_1_Conv2d_3_1x1_256_conv/kernel', - 'FeatureMaps/Mixed_5c_1_Conv2d_3_1x1_256_conv/bias', - ('FeatureMaps/Mixed_5c_2_Conv2d_3_3x3_s2_512_depthwise_conv/' - 'depthwise_kernel'), - ('FeatureMaps/Mixed_5c_2_Conv2d_3_3x3_s2_512_depthwise_conv/' - 'bias'), - 'FeatureMaps/Mixed_5c_2_Conv2d_3_3x3_s2_512_conv/kernel', - 'FeatureMaps/Mixed_5c_2_Conv2d_3_3x3_s2_512_conv/bias', - 'FeatureMaps/Mixed_5c_1_Conv2d_4_1x1_128_conv/kernel', - 'FeatureMaps/Mixed_5c_1_Conv2d_4_1x1_128_conv/bias', - ('FeatureMaps/Mixed_5c_2_Conv2d_4_3x3_s2_256_depthwise_conv/' - 'depthwise_kernel'), - ('FeatureMaps/Mixed_5c_2_Conv2d_4_3x3_s2_256_depthwise_conv/' - 'bias'), - 'FeatureMaps/Mixed_5c_2_Conv2d_4_3x3_s2_256_conv/kernel', - 'FeatureMaps/Mixed_5c_2_Conv2d_4_3x3_s2_256_conv/bias', - 'FeatureMaps/Mixed_5c_1_Conv2d_5_1x1_128_conv/kernel', - 'FeatureMaps/Mixed_5c_1_Conv2d_5_1x1_128_conv/bias', - ('FeatureMaps/Mixed_5c_2_Conv2d_5_3x3_s2_256_depthwise_conv/' - 'depthwise_kernel'), - ('FeatureMaps/Mixed_5c_2_Conv2d_5_3x3_s2_256_depthwise_conv/' - 'bias'), - 'FeatureMaps/Mixed_5c_2_Conv2d_5_3x3_s2_256_conv/kernel', - 'FeatureMaps/Mixed_5c_2_Conv2d_5_3x3_s2_256_conv/bias', - ]) - - if tf_version.is_tf2(): - actual_variable_set = set( - [var.name.split(':')[0] for var in feature_map_generator.variables]) - self.assertSetEqual(expected_keras_variables, actual_variable_set) - else: - with g.as_default(): - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - self.assertSetEqual(expected_slim_variables, actual_variable_set) - - -@parameterized.parameters({'use_native_resize_op': True}, - {'use_native_resize_op': False}) -class FPNFeatureMapGeneratorTest(test_case.TestCase, parameterized.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def _build_feature_map_generator( - self, image_features, depth, use_bounded_activations=False, - use_native_resize_op=False, use_explicit_padding=False, - use_depthwise=False): - if tf_version.is_tf2(): - return feature_map_generators.KerasFpnTopDownFeatureMaps( - num_levels=len(image_features), - depth=depth, - is_training=True, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - use_depthwise=use_depthwise, - use_explicit_padding=use_explicit_padding, - use_bounded_activations=use_bounded_activations, - use_native_resize_op=use_native_resize_op, - scope=None, - name='FeatureMaps', - ) - else: - def feature_map_generator(image_features): - return feature_map_generators.fpn_top_down_feature_maps( - image_features=image_features, - depth=depth, - use_depthwise=use_depthwise, - use_explicit_padding=use_explicit_padding, - use_bounded_activations=use_bounded_activations, - use_native_resize_op=use_native_resize_op) - return feature_map_generator - - def test_get_expected_feature_map_shapes( - self, use_native_resize_op): - with test_utils.GraphContextOrNone() as g: - image_features = [ - ('block2', tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block3', tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)), - ('block4', tf.random_uniform([4, 2, 2, 256], dtype=tf.float32)), - ('block5', tf.random_uniform([4, 1, 1, 256], dtype=tf.float32)) - ] - feature_map_generator = self._build_feature_map_generator( - image_features=image_features, - depth=128, - use_native_resize_op=use_native_resize_op) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'top_down_block2': (4, 8, 8, 128), - 'top_down_block3': (4, 4, 4, 128), - 'top_down_block4': (4, 2, 2, 128), - 'top_down_block5': (4, 1, 1, 128) - } - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_feature_map_shapes_with_explicit_padding( - self, use_native_resize_op): - with test_utils.GraphContextOrNone() as g: - image_features = [ - ('block2', tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block3', tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)), - ('block4', tf.random_uniform([4, 2, 2, 256], dtype=tf.float32)), - ('block5', tf.random_uniform([4, 1, 1, 256], dtype=tf.float32)) - ] - feature_map_generator = self._build_feature_map_generator( - image_features=image_features, - depth=128, - use_explicit_padding=True, - use_native_resize_op=use_native_resize_op) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'top_down_block2': (4, 8, 8, 128), - 'top_down_block3': (4, 4, 4, 128), - 'top_down_block4': (4, 2, 2, 128), - 'top_down_block5': (4, 1, 1, 128) - } - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - @unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') - def test_use_bounded_activations_add_operations( - self, use_native_resize_op): - with test_utils.GraphContextOrNone() as g: - image_features = [('block2', - tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block3', - tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)), - ('block4', - tf.random_uniform([4, 2, 2, 256], dtype=tf.float32)), - ('block5', - tf.random_uniform([4, 1, 1, 256], dtype=tf.float32))] - feature_map_generator = self._build_feature_map_generator( - image_features=image_features, - depth=128, - use_bounded_activations=True, - use_native_resize_op=use_native_resize_op) - def graph_fn(): - return feature_map_generator(image_features) - self.execute(graph_fn, [], g) - expected_added_operations = dict.fromkeys([ - 'top_down/clip_by_value', 'top_down/clip_by_value_1', - 'top_down/clip_by_value_2', 'top_down/clip_by_value_3', - 'top_down/clip_by_value_4', 'top_down/clip_by_value_5', - 'top_down/clip_by_value_6' - ]) - op_names = {op.name: None for op in g.get_operations()} - self.assertDictContainsSubset(expected_added_operations, op_names) - - @unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') - def test_use_bounded_activations_clip_value( - self, use_native_resize_op): - tf_graph = tf.Graph() - with tf_graph.as_default(): - image_features = [ - ('block2', 255 * tf.ones([4, 8, 8, 256], dtype=tf.float32)), - ('block3', 255 * tf.ones([4, 4, 4, 256], dtype=tf.float32)), - ('block4', 255 * tf.ones([4, 2, 2, 256], dtype=tf.float32)), - ('block5', 255 * tf.ones([4, 1, 1, 256], dtype=tf.float32)) - ] - feature_map_generator = self._build_feature_map_generator( - image_features=image_features, - depth=128, - use_bounded_activations=True, - use_native_resize_op=use_native_resize_op) - feature_map_generator(image_features) - - expected_clip_by_value_ops = [ - 'top_down/clip_by_value', 'top_down/clip_by_value_1', - 'top_down/clip_by_value_2', 'top_down/clip_by_value_3', - 'top_down/clip_by_value_4', 'top_down/clip_by_value_5', - 'top_down/clip_by_value_6' - ] - - # Gathers activation tensors before and after clip_by_value operations. - activations = {} - for clip_by_value_op in expected_clip_by_value_ops: - clip_input_tensor = tf_graph.get_operation_by_name( - '{}/Minimum'.format(clip_by_value_op)).inputs[0] - clip_output_tensor = tf_graph.get_tensor_by_name( - '{}:0'.format(clip_by_value_op)) - activations.update({ - 'before_{}'.format(clip_by_value_op): clip_input_tensor, - 'after_{}'.format(clip_by_value_op): clip_output_tensor, - }) - - expected_lower_bound = -feature_map_generators.ACTIVATION_BOUND - expected_upper_bound = feature_map_generators.ACTIVATION_BOUND - init_op = tf.global_variables_initializer() - with self.test_session() as session: - session.run(init_op) - activations_output = session.run(activations) - for clip_by_value_op in expected_clip_by_value_ops: - # Before clipping, activations are beyound the expected bound because - # of large input image_features values. - activations_before_clipping = ( - activations_output['before_{}'.format(clip_by_value_op)]) - before_clipping_lower_bound = np.amin(activations_before_clipping) - before_clipping_upper_bound = np.amax(activations_before_clipping) - self.assertLessEqual(before_clipping_lower_bound, - expected_lower_bound) - self.assertGreaterEqual(before_clipping_upper_bound, - expected_upper_bound) - - # After clipping, activations are bounded as expectation. - activations_after_clipping = ( - activations_output['after_{}'.format(clip_by_value_op)]) - after_clipping_lower_bound = np.amin(activations_after_clipping) - after_clipping_upper_bound = np.amax(activations_after_clipping) - self.assertGreaterEqual(after_clipping_lower_bound, - expected_lower_bound) - self.assertLessEqual(after_clipping_upper_bound, expected_upper_bound) - - def test_get_expected_feature_map_shapes_with_depthwise( - self, use_native_resize_op): - with test_utils.GraphContextOrNone() as g: - image_features = [ - ('block2', tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block3', tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)), - ('block4', tf.random_uniform([4, 2, 2, 256], dtype=tf.float32)), - ('block5', tf.random_uniform([4, 1, 1, 256], dtype=tf.float32)) - ] - feature_map_generator = self._build_feature_map_generator( - image_features=image_features, - depth=128, - use_depthwise=True, - use_native_resize_op=use_native_resize_op) - def graph_fn(): - return feature_map_generator(image_features) - - expected_feature_map_shapes = { - 'top_down_block2': (4, 8, 8, 128), - 'top_down_block3': (4, 4, 4, 128), - 'top_down_block4': (4, 2, 2, 128), - 'top_down_block5': (4, 1, 1, 128) - } - out_feature_maps = self.execute(graph_fn, [], g) - out_feature_map_shapes = dict( - (key, value.shape) for key, value in out_feature_maps.items()) - self.assertDictEqual(expected_feature_map_shapes, out_feature_map_shapes) - - def test_get_expected_variable_names( - self, use_native_resize_op): - with test_utils.GraphContextOrNone() as g: - image_features = [ - ('block2', tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block3', tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)), - ('block4', tf.random_uniform([4, 2, 2, 256], dtype=tf.float32)), - ('block5', tf.random_uniform([4, 1, 1, 256], dtype=tf.float32)) - ] - feature_map_generator = self._build_feature_map_generator( - image_features=image_features, - depth=128, - use_native_resize_op=use_native_resize_op) - def graph_fn(): - return feature_map_generator(image_features) - self.execute(graph_fn, [], g) - expected_slim_variables = set([ - 'projection_1/weights', - 'projection_1/biases', - 'projection_2/weights', - 'projection_2/biases', - 'projection_3/weights', - 'projection_3/biases', - 'projection_4/weights', - 'projection_4/biases', - 'smoothing_1/weights', - 'smoothing_1/biases', - 'smoothing_2/weights', - 'smoothing_2/biases', - 'smoothing_3/weights', - 'smoothing_3/biases', - ]) - - expected_keras_variables = set([ - 'FeatureMaps/top_down/projection_1/kernel', - 'FeatureMaps/top_down/projection_1/bias', - 'FeatureMaps/top_down/projection_2/kernel', - 'FeatureMaps/top_down/projection_2/bias', - 'FeatureMaps/top_down/projection_3/kernel', - 'FeatureMaps/top_down/projection_3/bias', - 'FeatureMaps/top_down/projection_4/kernel', - 'FeatureMaps/top_down/projection_4/bias', - 'FeatureMaps/top_down/smoothing_1_conv/kernel', - 'FeatureMaps/top_down/smoothing_1_conv/bias', - 'FeatureMaps/top_down/smoothing_2_conv/kernel', - 'FeatureMaps/top_down/smoothing_2_conv/bias', - 'FeatureMaps/top_down/smoothing_3_conv/kernel', - 'FeatureMaps/top_down/smoothing_3_conv/bias' - ]) - - if tf_version.is_tf2(): - actual_variable_set = set( - [var.name.split(':')[0] for var in feature_map_generator.variables]) - self.assertSetEqual(expected_keras_variables, actual_variable_set) - else: - with g.as_default(): - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - self.assertSetEqual(expected_slim_variables, actual_variable_set) - - def test_get_expected_variable_names_with_depthwise( - self, use_native_resize_op): - with test_utils.GraphContextOrNone() as g: - image_features = [ - ('block2', tf.random_uniform([4, 8, 8, 256], dtype=tf.float32)), - ('block3', tf.random_uniform([4, 4, 4, 256], dtype=tf.float32)), - ('block4', tf.random_uniform([4, 2, 2, 256], dtype=tf.float32)), - ('block5', tf.random_uniform([4, 1, 1, 256], dtype=tf.float32)) - ] - feature_map_generator = self._build_feature_map_generator( - image_features=image_features, - depth=128, - use_depthwise=True, - use_native_resize_op=use_native_resize_op) - def graph_fn(): - return feature_map_generator(image_features) - self.execute(graph_fn, [], g) - expected_slim_variables = set([ - 'projection_1/weights', - 'projection_1/biases', - 'projection_2/weights', - 'projection_2/biases', - 'projection_3/weights', - 'projection_3/biases', - 'projection_4/weights', - 'projection_4/biases', - 'smoothing_1/depthwise_weights', - 'smoothing_1/pointwise_weights', - 'smoothing_1/biases', - 'smoothing_2/depthwise_weights', - 'smoothing_2/pointwise_weights', - 'smoothing_2/biases', - 'smoothing_3/depthwise_weights', - 'smoothing_3/pointwise_weights', - 'smoothing_3/biases', - ]) - - expected_keras_variables = set([ - 'FeatureMaps/top_down/projection_1/kernel', - 'FeatureMaps/top_down/projection_1/bias', - 'FeatureMaps/top_down/projection_2/kernel', - 'FeatureMaps/top_down/projection_2/bias', - 'FeatureMaps/top_down/projection_3/kernel', - 'FeatureMaps/top_down/projection_3/bias', - 'FeatureMaps/top_down/projection_4/kernel', - 'FeatureMaps/top_down/projection_4/bias', - 'FeatureMaps/top_down/smoothing_1_depthwise_conv/depthwise_kernel', - 'FeatureMaps/top_down/smoothing_1_depthwise_conv/pointwise_kernel', - 'FeatureMaps/top_down/smoothing_1_depthwise_conv/bias', - 'FeatureMaps/top_down/smoothing_2_depthwise_conv/depthwise_kernel', - 'FeatureMaps/top_down/smoothing_2_depthwise_conv/pointwise_kernel', - 'FeatureMaps/top_down/smoothing_2_depthwise_conv/bias', - 'FeatureMaps/top_down/smoothing_3_depthwise_conv/depthwise_kernel', - 'FeatureMaps/top_down/smoothing_3_depthwise_conv/pointwise_kernel', - 'FeatureMaps/top_down/smoothing_3_depthwise_conv/bias' - ]) - - if tf_version.is_tf2(): - actual_variable_set = set( - [var.name.split(':')[0] for var in feature_map_generator.variables]) - self.assertSetEqual(expected_keras_variables, actual_variable_set) - else: - with g.as_default(): - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - self.assertSetEqual(expected_slim_variables, actual_variable_set) - - -class GetDepthFunctionTest(tf.test.TestCase): - - def test_return_min_depth_when_multiplier_is_small(self): - depth_fn = feature_map_generators.get_depth_fn(depth_multiplier=0.5, - min_depth=16) - self.assertEqual(depth_fn(16), 16) - - def test_return_correct_depth_with_multiplier(self): - depth_fn = feature_map_generators.get_depth_fn(depth_multiplier=0.5, - min_depth=16) - self.assertEqual(depth_fn(64), 32) - - -@parameterized.parameters( - {'replace_pool_with_conv': False}, - {'replace_pool_with_conv': True}, -) -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class PoolingPyramidFeatureMapGeneratorTest(tf.test.TestCase): - - def test_get_expected_feature_map_shapes(self, replace_pool_with_conv): - image_features = { - 'image_features': tf.random_uniform([4, 19, 19, 1024]) - } - feature_maps = feature_map_generators.pooling_pyramid_feature_maps( - base_feature_map_depth=1024, - num_layers=6, - image_features=image_features, - replace_pool_with_conv=replace_pool_with_conv) - - expected_pool_feature_map_shapes = { - 'Base_Conv2d_1x1_1024': (4, 19, 19, 1024), - 'MaxPool2d_0_2x2': (4, 10, 10, 1024), - 'MaxPool2d_1_2x2': (4, 5, 5, 1024), - 'MaxPool2d_2_2x2': (4, 3, 3, 1024), - 'MaxPool2d_3_2x2': (4, 2, 2, 1024), - 'MaxPool2d_4_2x2': (4, 1, 1, 1024), - } - - expected_conv_feature_map_shapes = { - 'Base_Conv2d_1x1_1024': (4, 19, 19, 1024), - 'Conv2d_0_3x3_s2_1024': (4, 10, 10, 1024), - 'Conv2d_1_3x3_s2_1024': (4, 5, 5, 1024), - 'Conv2d_2_3x3_s2_1024': (4, 3, 3, 1024), - 'Conv2d_3_3x3_s2_1024': (4, 2, 2, 1024), - 'Conv2d_4_3x3_s2_1024': (4, 1, 1, 1024), - } - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - out_feature_maps = sess.run(feature_maps) - out_feature_map_shapes = {key: value.shape - for key, value in out_feature_maps.items()} - if replace_pool_with_conv: - self.assertDictEqual(expected_conv_feature_map_shapes, - out_feature_map_shapes) - else: - self.assertDictEqual(expected_pool_feature_map_shapes, - out_feature_map_shapes) - - def test_get_expected_variable_names(self, replace_pool_with_conv): - image_features = { - 'image_features': tf.random_uniform([4, 19, 19, 1024]) - } - feature_maps = feature_map_generators.pooling_pyramid_feature_maps( - base_feature_map_depth=1024, - num_layers=6, - image_features=image_features, - replace_pool_with_conv=replace_pool_with_conv) - - expected_pool_variables = set([ - 'Base_Conv2d_1x1_1024/weights', - 'Base_Conv2d_1x1_1024/biases', - ]) - - expected_conv_variables = set([ - 'Base_Conv2d_1x1_1024/weights', - 'Base_Conv2d_1x1_1024/biases', - 'Conv2d_0_3x3_s2_1024/weights', - 'Conv2d_0_3x3_s2_1024/biases', - 'Conv2d_1_3x3_s2_1024/weights', - 'Conv2d_1_3x3_s2_1024/biases', - 'Conv2d_2_3x3_s2_1024/weights', - 'Conv2d_2_3x3_s2_1024/biases', - 'Conv2d_3_3x3_s2_1024/weights', - 'Conv2d_3_3x3_s2_1024/biases', - 'Conv2d_4_3x3_s2_1024/weights', - 'Conv2d_4_3x3_s2_1024/biases', - ]) - - init_op = tf.global_variables_initializer() - with self.test_session() as sess: - sess.run(init_op) - sess.run(feature_maps) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - if replace_pool_with_conv: - self.assertSetEqual(expected_conv_variables, actual_variable_set) - else: - self.assertSetEqual(expected_pool_variables, actual_variable_set) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/keras_models/__init__.py b/research/object_detection/models/keras_models/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/models/keras_models/base_models/original_mobilenet_v2.py b/research/object_detection/models/keras_models/base_models/original_mobilenet_v2.py deleted file mode 100644 index 42b40caf1c3..00000000000 --- a/research/object_detection/models/keras_models/base_models/original_mobilenet_v2.py +++ /dev/null @@ -1,478 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""MobileNet v2 models for Keras. - -MobileNetV2 is a general architecture and can be used for multiple use cases. -Depending on the use case, it can use different input layer size and -different width factors. This allows different width models to reduce -the number of multiply-adds and thereby -reduce inference cost on mobile devices. - -MobileNetV2 is very similar to the original MobileNet, -except that it uses inverted residual blocks with -bottlenecking features. It has a drastically lower -parameter count than the original MobileNet. -MobileNets support any input size greater -than 32 x 32, with larger image sizes -offering better performance. - -The number of parameters and number of multiply-adds -can be modified by using the `alpha` parameter, -which increases/decreases the number of filters in each layer. -By altering the image size and `alpha` parameter, -all 22 models from the paper can be built, with ImageNet weights provided. - -The paper demonstrates the performance of MobileNets using `alpha` values of -1.0 (also called 100 % MobileNet), 0.35, 0.5, 0.75, 1.0, 1.3, and 1.4 - -For each of these `alpha` values, weights for 5 different input image sizes -are provided (224, 192, 160, 128, and 96). - - -The following table describes the performance of -MobileNet on various input sizes: ------------------------------------------------------------------------- -MACs stands for Multiply Adds - - Classification Checkpoint| MACs (M) | Parameters (M)| Top 1 Acc| Top 5 Acc ---------------------------|------------|---------------|---------|----|------- -| [mobilenet_v2_1.4_224] | 582 | 6.06 | 75.0 | 92.5 | -| [mobilenet_v2_1.3_224] | 509 | 5.34 | 74.4 | 92.1 | -| [mobilenet_v2_1.0_224] | 300 | 3.47 | 71.8 | 91.0 | -| [mobilenet_v2_1.0_192] | 221 | 3.47 | 70.7 | 90.1 | -| [mobilenet_v2_1.0_160] | 154 | 3.47 | 68.8 | 89.0 | -| [mobilenet_v2_1.0_128] | 99 | 3.47 | 65.3 | 86.9 | -| [mobilenet_v2_1.0_96] | 56 | 3.47 | 60.3 | 83.2 | -| [mobilenet_v2_0.75_224] | 209 | 2.61 | 69.8 | 89.6 | -| [mobilenet_v2_0.75_192] | 153 | 2.61 | 68.7 | 88.9 | -| [mobilenet_v2_0.75_160] | 107 | 2.61 | 66.4 | 87.3 | -| [mobilenet_v2_0.75_128] | 69 | 2.61 | 63.2 | 85.3 | -| [mobilenet_v2_0.75_96] | 39 | 2.61 | 58.8 | 81.6 | -| [mobilenet_v2_0.5_224] | 97 | 1.95 | 65.4 | 86.4 | -| [mobilenet_v2_0.5_192] | 71 | 1.95 | 63.9 | 85.4 | -| [mobilenet_v2_0.5_160] | 50 | 1.95 | 61.0 | 83.2 | -| [mobilenet_v2_0.5_128] | 32 | 1.95 | 57.7 | 80.8 | -| [mobilenet_v2_0.5_96] | 18 | 1.95 | 51.2 | 75.8 | -| [mobilenet_v2_0.35_224] | 59 | 1.66 | 60.3 | 82.9 | -| [mobilenet_v2_0.35_192] | 43 | 1.66 | 58.2 | 81.2 | -| [mobilenet_v2_0.35_160] | 30 | 1.66 | 55.7 | 79.1 | -| [mobilenet_v2_0.35_128] | 20 | 1.66 | 50.8 | 75.0 | -| [mobilenet_v2_0.35_96] | 11 | 1.66 | 45.5 | 70.4 | - -The weights for all 16 models are obtained and translated from the Tensorflow -checkpoints from TensorFlow checkpoints found at -https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/README.md - -# Reference -This file contains building code for MobileNetV2, based on -[MobileNetV2: Inverted Residuals and Linear Bottlenecks] -(https://arxiv.org/abs/1801.04381) - -Tests comparing this model to the existing Tensorflow model can be -found at -[mobilenet_v2_keras](https://github.com/JonathanCMitchell/mobilenet_v2_keras) -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import warnings -import numpy as np -import tensorflow.compat.v1 as tf - -Model = tf.keras.Model -Input = tf.keras.layers.Input -Activation = tf.keras.layers.Activation -BatchNormalization = tf.keras.layers.BatchNormalization -Conv2D = tf.keras.layers.Conv2D -DepthwiseConv2D = tf.keras.layers.DepthwiseConv2D -GlobalAveragePooling2D = tf.keras.layers.GlobalAveragePooling2D -Add = tf.keras.layers.Add -Dense = tf.keras.layers.Dense -K = tf.keras.Backend - - -def relu6(x): - return K.relu(x, max_value=6) - - -def _obtain_input_shape( - input_shape, - default_size, - min_size, - data_format, - require_flatten): - """Internal utility to compute/validate an ImageNet model's input shape. - - Args: - input_shape: either None (will return the default network input shape), - or a user-provided shape to be validated. - default_size: default input width/height for the model. - min_size: minimum input width/height accepted by the model. - data_format: image data format to use. - require_flatten: whether the model is expected to - be linked to a classifier via a Flatten layer. - - Returns: - An integer shape tuple (may include None entries). - - Raises: - ValueError: in case of invalid argument values. - """ - if input_shape and len(input_shape) == 3: - if data_format == 'channels_first': - if input_shape[0] not in {1, 3}: - warnings.warn( - 'This model usually expects 1 or 3 input channels. ' - 'However, it was passed an input_shape with ' + - str(input_shape[0]) + ' input channels.') - default_shape = (input_shape[0], default_size, default_size) - else: - if input_shape[-1] not in {1, 3}: - warnings.warn( - 'This model usually expects 1 or 3 input channels. ' - 'However, it was passed an input_shape with ' + - str(input_shape[-1]) + ' input channels.') - default_shape = (default_size, default_size, input_shape[-1]) - else: - if data_format == 'channels_first': - default_shape = (3, default_size, default_size) - else: - default_shape = (default_size, default_size, 3) - if input_shape: - if data_format == 'channels_first': - if input_shape is not None: - if len(input_shape) != 3: - raise ValueError( - '`input_shape` must be a tuple of three integers.') - if ((input_shape[1] is not None and input_shape[1] < min_size) or - (input_shape[2] is not None and input_shape[2] < min_size)): - raise ValueError('Input size must be at least ' + - str(min_size) + 'x' + str(min_size) + - '; got `input_shape=' + - str(input_shape) + '`') - else: - if input_shape is not None: - if len(input_shape) != 3: - raise ValueError( - '`input_shape` must be a tuple of three integers.') - if ((input_shape[0] is not None and input_shape[0] < min_size) or - (input_shape[1] is not None and input_shape[1] < min_size)): - raise ValueError('Input size must be at least ' + - str(min_size) + 'x' + str(min_size) + - '; got `input_shape=' + - str(input_shape) + '`') - else: - if require_flatten: - input_shape = default_shape - else: - if data_format == 'channels_first': - input_shape = (3, None, None) - else: - input_shape = (None, None, 3) - if require_flatten: - if None in input_shape: - raise ValueError('If `include_top` is True, ' - 'you should specify a static `input_shape`. ' - 'Got `input_shape=' + str(input_shape) + '`') - return input_shape - - -def preprocess_input(x): - """Preprocesses a numpy array encoding a batch of images. - - This function applies the "Inception" preprocessing which converts - the RGB values from [0, 255] to [-1, 1]. Note that this preprocessing - function is different from `imagenet_utils.preprocess_input()`. - - Args: - x: a 4D numpy array consists of RGB values within [0, 255]. - - Returns: - Preprocessed array. - """ - x /= 128. - x -= 1. - return x.astype(np.float32) - - -# This function is taken from the original tf repo. -# It ensures that all layers have a channel number that is divisible by 8 -# It can be seen here: -# https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py - - -def _make_divisible(v, divisor, min_value=None): - if min_value is None: - min_value = divisor - new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) - # Make sure that round down does not go down by more than 10%. - if new_v < 0.9 * v: - new_v += divisor - return new_v - - -def mobilenet_v2(input_shape=None, - alpha=1.0, - include_top=True, - classes=1000): - """Instantiates the MobileNetV2 architecture. - - To load a MobileNetV2 model via `load_model`, import the custom - objects `relu6` and pass them to the `custom_objects` parameter. - E.g. - model = load_model('mobilenet.h5', custom_objects={ - 'relu6': mobilenet.relu6}) - - Args: - input_shape: optional shape tuple, to be specified if you would - like to use a model with an input img resolution that is not - (224, 224, 3). - It should have exactly 3 inputs channels (224, 224, 3). - You can also omit this option if you would like - to infer input_shape from an input_tensor. - If you choose to include both input_tensor and input_shape then - input_shape will be used if they match, if the shapes - do not match then we will throw an error. - E.g. `(160, 160, 3)` would be one valid value. - alpha: controls the width of the network. This is known as the - width multiplier in the MobileNetV2 paper. - - If `alpha` < 1.0, proportionally decreases the number - of filters in each layer. - - If `alpha` > 1.0, proportionally increases the number - of filters in each layer. - - If `alpha` = 1, default number of filters from the paper - are used at each layer. - include_top: whether to include the fully-connected - layer at the top of the network. - classes: optional number of classes to classify images - into, only to be specified if `include_top` is True, and - if no `weights` argument is specified. - - Returns: - A Keras model instance. - - Raises: - ValueError: in case of invalid argument for `weights`, - or invalid input shape or invalid depth_multiplier, alpha, - rows when weights='imagenet' - """ - - # Determine proper input shape and default size. - # If input_shape is None and no input_tensor - if input_shape is None: - default_size = 224 - - # If input_shape is not None, assume default size - else: - if K.image_data_format() == 'channels_first': - rows = input_shape[1] - cols = input_shape[2] - else: - rows = input_shape[0] - cols = input_shape[1] - - if rows == cols and rows in [96, 128, 160, 192, 224]: - default_size = rows - else: - default_size = 224 - - input_shape = _obtain_input_shape(input_shape, - default_size=default_size, - min_size=32, - data_format=K.image_data_format(), - require_flatten=include_top) - - if K.image_data_format() == 'channels_last': - row_axis, col_axis = (0, 1) - else: - row_axis, col_axis = (1, 2) - rows = input_shape[row_axis] - cols = input_shape[col_axis] - - if K.image_data_format() != 'channels_last': - warnings.warn('The MobileNet family of models is only available ' - 'for the input data format "channels_last" ' - '(width, height, channels). ' - 'However your settings specify the default ' - 'data format "channels_first" (channels, width, height).' - ' You should set `image_data_format="channels_last"` ' - 'in your Keras config located at ~/.keras/keras.json. ' - 'The model being returned right now will expect inputs ' - 'to follow the "channels_last" data format.') - K.set_image_data_format('channels_last') - old_data_format = 'channels_first' - else: - old_data_format = None - - img_input = Input(shape=input_shape) - - first_block_filters = _make_divisible(32 * alpha, 8) - x = Conv2D(first_block_filters, - kernel_size=3, - strides=(2, 2), padding='same', - use_bias=False, name='Conv1')(img_input) - x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='bn_Conv1')(x) - x = Activation(relu6, name='Conv1_relu')(x) - - x = _first_inverted_res_block(x, - filters=16, - alpha=alpha, - stride=1, - block_id=0) - - x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2, - expansion=6, block_id=1) - x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1, - expansion=6, block_id=2) - - x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2, - expansion=6, block_id=3) - x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, - expansion=6, block_id=4) - x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1, - expansion=6, block_id=5) - - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=2, - expansion=6, block_id=6) - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, - expansion=6, block_id=7) - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, - expansion=6, block_id=8) - x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, - expansion=6, block_id=9) - - x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, - expansion=6, block_id=10) - x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, - expansion=6, block_id=11) - x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, - expansion=6, block_id=12) - - x = _inverted_res_block(x, filters=160, alpha=alpha, stride=2, - expansion=6, block_id=13) - x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, - expansion=6, block_id=14) - x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, - expansion=6, block_id=15) - - x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, - expansion=6, block_id=16) - - # no alpha applied to last conv as stated in the paper: - # if the width multiplier is greater than 1 we - # increase the number of output channels - if alpha > 1.0: - last_block_filters = _make_divisible(1280 * alpha, 8) - else: - last_block_filters = 1280 - - x = Conv2D(last_block_filters, - kernel_size=1, - use_bias=False, - name='Conv_1')(x) - x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_1_bn')(x) - x = Activation(relu6, name='out_relu')(x) - - if include_top: - x = GlobalAveragePooling2D()(x) - x = Dense(classes, activation='softmax', - use_bias=True, name='Logits')(x) - - # Ensure that the model takes into account - # any potential predecessors of `input_tensor`. - inputs = img_input - - # Create model. - model = Model(inputs, x, name='mobilenetv2_%0.2f_%s' % (alpha, rows)) - - if old_data_format: - K.set_image_data_format(old_data_format) - return model - - -def _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id): - """Build an inverted res block.""" - in_channels = int(inputs.shape[-1]) - pointwise_conv_filters = int(filters * alpha) - pointwise_filters = _make_divisible(pointwise_conv_filters, 8) - # Expand - - x = Conv2D(expansion * in_channels, kernel_size=1, padding='same', - use_bias=False, activation=None, - name='mobl%d_conv_expand' % block_id)(inputs) - x = BatchNormalization(epsilon=1e-3, momentum=0.999, - name='bn%d_conv_bn_expand' % - block_id)(x) - x = Activation(relu6, name='conv_%d_relu' % block_id)(x) - - # Depthwise - x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None, - use_bias=False, padding='same', - name='mobl%d_conv_depthwise' % block_id)(x) - x = BatchNormalization(epsilon=1e-3, momentum=0.999, - name='bn%d_conv_depthwise' % block_id)(x) - - x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x) - - # Project - x = Conv2D(pointwise_filters, - kernel_size=1, padding='same', use_bias=False, activation=None, - name='mobl%d_conv_project' % block_id)(x) - x = BatchNormalization(epsilon=1e-3, momentum=0.999, - name='bn%d_conv_bn_project' % block_id)(x) - - if in_channels == pointwise_filters and stride == 1: - return Add(name='res_connect_' + str(block_id))([inputs, x]) - - return x - - -def _first_inverted_res_block(inputs, - stride, - alpha, filters, block_id): - """Build the first inverted res block.""" - in_channels = int(inputs.shape[-1]) - pointwise_conv_filters = int(filters * alpha) - pointwise_filters = _make_divisible(pointwise_conv_filters, 8) - - # Depthwise - x = DepthwiseConv2D(kernel_size=3, - strides=stride, activation=None, - use_bias=False, padding='same', - name='mobl%d_conv_depthwise' % - block_id)(inputs) - x = BatchNormalization(epsilon=1e-3, momentum=0.999, - name='bn%d_conv_depthwise' % - block_id)(x) - x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x) - - # Project - x = Conv2D(pointwise_filters, - kernel_size=1, - padding='same', - use_bias=False, - activation=None, - name='mobl%d_conv_project' % - block_id)(x) - x = BatchNormalization(epsilon=1e-3, momentum=0.999, - name='bn%d_conv_project' % - block_id)(x) - - if in_channels == pointwise_filters and stride == 1: - return Add(name='res_connect_' + str(block_id))([inputs, x]) - - return x diff --git a/research/object_detection/models/keras_models/convert_keras_models.py b/research/object_detection/models/keras_models/convert_keras_models.py deleted file mode 100644 index a34af981b37..00000000000 --- a/research/object_detection/models/keras_models/convert_keras_models.py +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Write keras weights into a tensorflow checkpoint. - -The imagenet weights in `keras.applications` are downloaded from github. -This script converts them into the tensorflow checkpoint format and stores them -on disk where they can be easily accessible during training. -""" - -from __future__ import print_function - -import os - -from absl import app -import numpy as np -import tensorflow.compat.v1 as tf - -FLAGS = tf.flags.FLAGS - - -tf.flags.DEFINE_string('model', 'resnet_v2_101', - 'The model to load. The following are supported: ' - '"resnet_v1_50", "resnet_v1_101", "resnet_v2_50", ' - '"resnet_v2_101"') -tf.flags.DEFINE_string('output_path', None, - 'The directory to output weights in.') -tf.flags.DEFINE_boolean('verify_weights', True, - ('Verify the weights are loaded correctly by making ' - 'sure the predictions are the same before and after ' - 'saving.')) - - -def init_model(name): - """Creates a Keras Model with the specific ResNet version.""" - if name == 'resnet_v1_50': - model = tf.keras.applications.ResNet50(weights='imagenet') - elif name == 'resnet_v1_101': - model = tf.keras.applications.ResNet101(weights='imagenet') - elif name == 'resnet_v2_50': - model = tf.keras.applications.ResNet50V2(weights='imagenet') - elif name == 'resnet_v2_101': - model = tf.keras.applications.ResNet101V2(weights='imagenet') - else: - raise ValueError('Model {} not supported'.format(FLAGS.model)) - - return model - - -def main(_): - - model = init_model(FLAGS.model) - - path = os.path.join(FLAGS.output_path, FLAGS.model) - tf.gfile.MakeDirs(path) - weights_path = os.path.join(path, 'weights') - ckpt = tf.train.Checkpoint(feature_extractor=model) - saved_path = ckpt.save(weights_path) - - if FLAGS.verify_weights: - imgs = np.random.randn(1, 224, 224, 3).astype(np.float32) - keras_preds = model(imgs) - - model = init_model(FLAGS.model) - ckpt.restore(saved_path) - loaded_weights_pred = model(imgs).numpy() - - if not np.all(np.isclose(keras_preds, loaded_weights_pred)): - raise RuntimeError('The model was not saved correctly.') - - -if __name__ == '__main__': - tf.enable_v2_behavior() - app.run(main) diff --git a/research/object_detection/models/keras_models/hourglass_network.py b/research/object_detection/models/keras_models/hourglass_network.py deleted file mode 100644 index e6e71545c40..00000000000 --- a/research/object_detection/models/keras_models/hourglass_network.py +++ /dev/null @@ -1,624 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""The Hourglass[1] network. - -[1]: https://arxiv.org/abs/1603.06937 -""" - - -import tensorflow.compat.v2 as tf - - -BATCH_NORM_EPSILON = 1e-5 -BATCH_NORM_MOMENTUM = 0.1 -BATCH_NORM_FUSED = True - - -class IdentityLayer(tf.keras.layers.Layer): - """A layer which passes through the input as it is.""" - - def call(self, inputs): - return inputs - - -def _get_padding_for_kernel_size(kernel_size): - if kernel_size == 7: - return (3, 3) - elif kernel_size == 3: - return (1, 1) - else: - raise ValueError('Padding for kernel size {} not known.'.format( - kernel_size)) - - -def batchnorm(): - try: - return tf.keras.layers.experimental.SyncBatchNormalization( - name='batchnorm', epsilon=1e-5, momentum=0.1) - except AttributeError: - return tf.keras.layers.BatchNormalization( - name='batchnorm', epsilon=1e-5, momentum=0.1, fused=BATCH_NORM_FUSED) - - -class ConvolutionalBlock(tf.keras.layers.Layer): - """Block that aggregates Convolution + Norm layer + ReLU.""" - - def __init__(self, kernel_size, out_channels, stride=1, relu=True, - padding='same'): - """Initializes the Convolutional block. - - Args: - kernel_size: int, convolution kernel size. - out_channels: int, the desired number of output channels. - stride: Integer, stride used in the convolution. - relu: bool, whether to use relu at the end of the layer. - padding: str, the padding scheme to use when kernel_size <= 1 - """ - super(ConvolutionalBlock, self).__init__() - - if kernel_size > 1: - padding = 'valid' - padding_size = _get_padding_for_kernel_size(kernel_size) - - # TODO(vighneshb) Explore if removing and using padding option in conv - # layer works. - self.pad = tf.keras.layers.ZeroPadding2D(padding_size) - else: - self.pad = IdentityLayer() - - self.conv = tf.keras.layers.Conv2D( - filters=out_channels, kernel_size=kernel_size, use_bias=False, - strides=stride, padding=padding) - - self.norm = batchnorm() - - if relu: - self.relu = tf.keras.layers.ReLU() - else: - self.relu = IdentityLayer() - - def call(self, inputs): - net = self.pad(inputs) - net = self.conv(net) - net = self.norm(net) - return self.relu(net) - - -class SkipConvolution(ConvolutionalBlock): - """The skip connection layer for a ResNet.""" - - def __init__(self, out_channels, stride): - """Initializes the skip convolution layer. - - Args: - out_channels: int, the desired number of output channels. - stride: int, the stride for the layer. - """ - super(SkipConvolution, self).__init__( - out_channels=out_channels, kernel_size=1, stride=stride, relu=False) - - -class ResidualBlock(tf.keras.layers.Layer): - """A Residual block.""" - - def __init__(self, out_channels, skip_conv=False, kernel_size=3, stride=1, - padding='same'): - """Initializes the Residual block. - - Args: - out_channels: int, the desired number of output channels. - skip_conv: bool, whether to use a conv layer for skip connections. - kernel_size: int, convolution kernel size. - stride: Integer, stride used in the convolution. - padding: str, the type of padding to use. - """ - - super(ResidualBlock, self).__init__() - self.conv_block = ConvolutionalBlock( - kernel_size=kernel_size, out_channels=out_channels, stride=stride) - - self.conv = tf.keras.layers.Conv2D( - filters=out_channels, kernel_size=kernel_size, use_bias=False, - strides=1, padding=padding) - self.norm = batchnorm() - - if skip_conv: - self.skip = SkipConvolution(out_channels=out_channels, - stride=stride) - else: - self.skip = IdentityLayer() - - self.relu = tf.keras.layers.ReLU() - - def call(self, inputs): - net = self.conv_block(inputs) - net = self.conv(net) - net = self.norm(net) - net_skip = self.skip(inputs) - return self.relu(net + net_skip) - - -class InputDownsampleBlock(tf.keras.layers.Layer): - """Block for the initial feature downsampling.""" - - def __init__(self, out_channels_initial_conv, out_channels_residual_block): - """Initializes the downsample block. - - Args: - out_channels_initial_conv: int, the desired number of output channels - in the initial conv layer. - out_channels_residual_block: int, the desired number of output channels - in the underlying residual block. - """ - - super(InputDownsampleBlock, self).__init__() - self.conv_block = ConvolutionalBlock( - kernel_size=7, out_channels=out_channels_initial_conv, stride=2, - padding='valid') - self.residual_block = ResidualBlock( - out_channels=out_channels_residual_block, stride=2, skip_conv=True) - - def call(self, inputs): - return self.residual_block(self.conv_block(inputs)) - - -class InputConvBlock(tf.keras.layers.Layer): - """Block for the initial feature convolution. - - This block is used in the hourglass network when we don't want to downsample - the input. - """ - - def __init__(self, out_channels_initial_conv, out_channels_residual_block): - """Initializes the downsample block. - - Args: - out_channels_initial_conv: int, the desired number of output channels - in the initial conv layer. - out_channels_residual_block: int, the desired number of output channels - in the underlying residual block. - """ - - super(InputConvBlock, self).__init__() - - self.conv_block = ConvolutionalBlock( - kernel_size=3, out_channels=out_channels_initial_conv, stride=1, - padding='valid') - self.residual_block = ResidualBlock( - out_channels=out_channels_residual_block, stride=1, skip_conv=True) - - def call(self, inputs): - return self.residual_block(self.conv_block(inputs)) - - -def _make_repeated_residual_blocks(out_channels, num_blocks, - initial_stride=1, residual_channels=None, - initial_skip_conv=False): - """Stack Residual blocks one after the other. - - Args: - out_channels: int, the desired number of output channels. - num_blocks: int, the number of residual blocks to be stacked. - initial_stride: int, the stride of the initial residual block. - residual_channels: int, the desired number of output channels in the - intermediate residual blocks. If not specifed, we use out_channels. - initial_skip_conv: bool, if set, the first residual block uses a skip - convolution. This is useful when the number of channels in the input - are not the same as residual_channels. - - Returns: - blocks: A list of residual blocks to be applied in sequence. - - """ - - blocks = [] - - if residual_channels is None: - residual_channels = out_channels - - for i in range(num_blocks - 1): - # Only use the stride at the first block so we don't repeatedly downsample - # the input - stride = initial_stride if i == 0 else 1 - - # If the stide is more than 1, we cannot use an identity layer for the - # skip connection and are forced to use a conv for the skip connection. - skip_conv = stride > 1 - - if i == 0 and initial_skip_conv: - skip_conv = True - - blocks.append( - ResidualBlock(out_channels=residual_channels, stride=stride, - skip_conv=skip_conv) - ) - - if num_blocks == 1: - # If there is only 1 block, the for loop above is not run, - # therefore we honor the requested stride in the last residual block - stride = initial_stride - # We are forced to use a conv in the skip connection if stride > 1 - skip_conv = stride > 1 - else: - stride = 1 - skip_conv = residual_channels != out_channels - - blocks.append(ResidualBlock(out_channels=out_channels, skip_conv=skip_conv, - stride=stride)) - - return blocks - - -def _apply_blocks(inputs, blocks): - net = inputs - - for block in blocks: - net = block(net) - - return net - - -class EncoderDecoderBlock(tf.keras.layers.Layer): - """An encoder-decoder block which recursively defines the hourglass network.""" - - def __init__(self, num_stages, channel_dims, blocks_per_stage, - stagewise_downsample=True, encoder_decoder_shortcut=True): - """Initializes the encoder-decoder block. - - Args: - num_stages: int, Number of stages in the network. At each stage we have 2 - encoder and 1 decoder blocks. The second encoder block downsamples the - input. - channel_dims: int list, the output channels dimensions of stages in - the network. `channel_dims[0]` is used to define the number of - channels in the first encoder block and `channel_dims[1]` is used to - define the number of channels in the second encoder block. The channels - in the recursive inner layers are defined using `channel_dims[1:]` - blocks_per_stage: int list, number of residual blocks to use at each - stage. `blocks_per_stage[0]` defines the number of blocks at the - current stage and `blocks_per_stage[1:]` is used at further stages. - stagewise_downsample: bool, whether or not to downsample before passing - inputs to the next stage. - encoder_decoder_shortcut: bool, whether or not to use shortcut - connections between encoder and decoder. - """ - - super(EncoderDecoderBlock, self).__init__() - - out_channels = channel_dims[0] - out_channels_downsampled = channel_dims[1] - - self.encoder_decoder_shortcut = encoder_decoder_shortcut - - if encoder_decoder_shortcut: - self.merge_features = tf.keras.layers.Add() - self.encoder_block1 = _make_repeated_residual_blocks( - out_channels=out_channels, num_blocks=blocks_per_stage[0], - initial_stride=1) - - initial_stride = 2 if stagewise_downsample else 1 - self.encoder_block2 = _make_repeated_residual_blocks( - out_channels=out_channels_downsampled, - num_blocks=blocks_per_stage[0], initial_stride=initial_stride, - initial_skip_conv=out_channels != out_channels_downsampled) - - if num_stages > 1: - self.inner_block = [ - EncoderDecoderBlock(num_stages - 1, channel_dims[1:], - blocks_per_stage[1:], - stagewise_downsample=stagewise_downsample, - encoder_decoder_shortcut=encoder_decoder_shortcut) - ] - else: - self.inner_block = _make_repeated_residual_blocks( - out_channels=out_channels_downsampled, - num_blocks=blocks_per_stage[1]) - - self.decoder_block = _make_repeated_residual_blocks( - residual_channels=out_channels_downsampled, - out_channels=out_channels, num_blocks=blocks_per_stage[0]) - - self.upsample = tf.keras.layers.UpSampling2D(initial_stride) - - def call(self, inputs): - - if self.encoder_decoder_shortcut: - encoded_outputs = _apply_blocks(inputs, self.encoder_block1) - encoded_downsampled_outputs = _apply_blocks(inputs, self.encoder_block2) - inner_block_outputs = _apply_blocks( - encoded_downsampled_outputs, self.inner_block) - - decoded_outputs = _apply_blocks(inner_block_outputs, self.decoder_block) - upsampled_outputs = self.upsample(decoded_outputs) - - if self.encoder_decoder_shortcut: - return self.merge_features([encoded_outputs, upsampled_outputs]) - else: - return upsampled_outputs - - -class HourglassNetwork(tf.keras.Model): - """The hourglass network.""" - - def __init__(self, num_stages, input_channel_dims, channel_dims_per_stage, - blocks_per_stage, num_hourglasses, initial_downsample=True, - stagewise_downsample=True, encoder_decoder_shortcut=True): - """Intializes the feature extractor. - - Args: - num_stages: int, Number of stages in the network. At each stage we have 2 - encoder and 1 decoder blocks. The second encoder block downsamples the - input. - input_channel_dims: int, the number of channels in the input conv blocks. - channel_dims_per_stage: int list, the output channel dimensions of each - stage in the hourglass network. - blocks_per_stage: int list, number of residual blocks to use at each - stage in the hourglass network - num_hourglasses: int, number of hourglas networks to stack - sequentially. - initial_downsample: bool, if set, downsamples the input by a factor of 4 - before applying the rest of the network. Downsampling is done with a 7x7 - convolution kernel, otherwise a 3x3 kernel is used. - stagewise_downsample: bool, whether or not to downsample before passing - inputs to the next stage. - encoder_decoder_shortcut: bool, whether or not to use shortcut - connections between encoder and decoder. - """ - - super(HourglassNetwork, self).__init__() - - self.num_hourglasses = num_hourglasses - self.initial_downsample = initial_downsample - if initial_downsample: - self.downsample_input = InputDownsampleBlock( - out_channels_initial_conv=input_channel_dims, - out_channels_residual_block=channel_dims_per_stage[0] - ) - else: - self.conv_input = InputConvBlock( - out_channels_initial_conv=input_channel_dims, - out_channels_residual_block=channel_dims_per_stage[0] - ) - - self.hourglass_network = [] - self.output_conv = [] - for _ in range(self.num_hourglasses): - self.hourglass_network.append( - EncoderDecoderBlock( - num_stages=num_stages, channel_dims=channel_dims_per_stage, - blocks_per_stage=blocks_per_stage, - stagewise_downsample=stagewise_downsample, - encoder_decoder_shortcut=encoder_decoder_shortcut) - ) - self.output_conv.append( - ConvolutionalBlock(kernel_size=3, - out_channels=channel_dims_per_stage[0]) - ) - - self.intermediate_conv1 = [] - self.intermediate_conv2 = [] - self.intermediate_residual = [] - - for _ in range(self.num_hourglasses - 1): - self.intermediate_conv1.append( - ConvolutionalBlock( - kernel_size=1, out_channels=channel_dims_per_stage[0], relu=False) - ) - self.intermediate_conv2.append( - ConvolutionalBlock( - kernel_size=1, out_channels=channel_dims_per_stage[0], relu=False) - ) - self.intermediate_residual.append( - ResidualBlock(out_channels=channel_dims_per_stage[0]) - ) - - self.intermediate_relu = tf.keras.layers.ReLU() - - def call(self, inputs): - - if self.initial_downsample: - inputs = self.downsample_input(inputs) - else: - inputs = self.conv_input(inputs) - - outputs = [] - - for i in range(self.num_hourglasses): - - hourglass_output = self.hourglass_network[i](inputs) - - output = self.output_conv[i](hourglass_output) - outputs.append(output) - - if i < self.num_hourglasses - 1: - secondary_output = (self.intermediate_conv1[i](inputs) + - self.intermediate_conv2[i](output)) - secondary_output = self.intermediate_relu(secondary_output) - inputs = self.intermediate_residual[i](secondary_output) - - return outputs - - @property - def out_stride(self): - """The stride in the output image of the network.""" - return 4 - - @property - def num_feature_outputs(self): - """Ther number of feature outputs returned by the feature extractor.""" - return self.num_hourglasses - - -def _layer_depth(layer): - """Compute depth of Conv/Residual blocks or lists of them.""" - - if isinstance(layer, list): - return sum([_layer_depth(l) for l in layer]) - - elif isinstance(layer, ConvolutionalBlock): - return 1 - - elif isinstance(layer, ResidualBlock): - return 2 - - else: - raise ValueError('Unknown layer - {}'.format(layer)) - - -def _encoder_decoder_depth(network): - """Helper function to compute depth of encoder-decoder blocks.""" - - encoder_block2_layers = _layer_depth(network.encoder_block2) - decoder_block_layers = _layer_depth(network.decoder_block) - - if isinstance(network.inner_block[0], EncoderDecoderBlock): - - assert len(network.inner_block) == 1, 'Inner block is expected as length 1.' - inner_block_layers = _encoder_decoder_depth(network.inner_block[0]) - - return inner_block_layers + encoder_block2_layers + decoder_block_layers - - elif isinstance(network.inner_block[0], ResidualBlock): - return (encoder_block2_layers + decoder_block_layers + - _layer_depth(network.inner_block)) - - else: - raise ValueError('Unknown inner block type.') - - -def hourglass_depth(network): - """Helper function to verify depth of hourglass backbone.""" - - input_conv_layers = 3 # 1 ResidualBlock and 1 ConvBlock - - # Only intermediate_conv2 and intermediate_residual are applied before - # sending inputs to the later stages. - intermediate_layers = ( - _layer_depth(network.intermediate_conv2) + - _layer_depth(network.intermediate_residual) - ) - - # network.output_conv is applied before sending input to the later stages - output_layers = _layer_depth(network.output_conv) - - encoder_decoder_layers = sum(_encoder_decoder_depth(net) for net in - network.hourglass_network) - - return (input_conv_layers + encoder_decoder_layers + intermediate_layers - + output_layers) - - -def hourglass_104(): - """The Hourglass-104 backbone. - - The architecture parameters are taken from [1]. - - Returns: - network: An HourglassNetwork object implementing the Hourglass-104 - backbone. - - [1]: https://arxiv.org/abs/1904.07850 - """ - - return HourglassNetwork( - input_channel_dims=128, - channel_dims_per_stage=[256, 256, 384, 384, 384, 512], - num_hourglasses=2, - num_stages=5, - blocks_per_stage=[2, 2, 2, 2, 2, 4], - ) - - -def single_stage_hourglass(input_channel_dims, channel_dims_per_stage, - blocks_per_stage, initial_downsample=True, - stagewise_downsample=True, - encoder_decoder_shortcut=True): - assert len(channel_dims_per_stage) == len(blocks_per_stage) - - return HourglassNetwork( - input_channel_dims=input_channel_dims, - channel_dims_per_stage=channel_dims_per_stage, - num_hourglasses=1, - num_stages=len(channel_dims_per_stage) - 1, - blocks_per_stage=blocks_per_stage, - initial_downsample=initial_downsample, - stagewise_downsample=stagewise_downsample, - encoder_decoder_shortcut=encoder_decoder_shortcut - ) - - -def hourglass_10(num_channels, initial_downsample=True): - nc = num_channels - return single_stage_hourglass( - input_channel_dims=nc, - initial_downsample=initial_downsample, - blocks_per_stage=[1, 1], - channel_dims_per_stage=[nc * 2, nc * 2]) - - -def hourglass_20(num_channels, initial_downsample=True): - nc = num_channels - return single_stage_hourglass( - input_channel_dims=nc, - initial_downsample=initial_downsample, - blocks_per_stage=[1, 2, 2], - channel_dims_per_stage=[nc * 2, nc * 2, nc * 3]) - - -def hourglass_32(num_channels, initial_downsample=True): - nc = num_channels - return single_stage_hourglass( - input_channel_dims=nc, - initial_downsample=initial_downsample, - blocks_per_stage=[2, 2, 2, 2], - channel_dims_per_stage=[nc * 2, nc * 2, nc * 3, nc * 3]) - - -def hourglass_52(num_channels, initial_downsample=True): - nc = num_channels - return single_stage_hourglass( - input_channel_dims=nc, - initial_downsample=initial_downsample, - blocks_per_stage=[2, 2, 2, 2, 2, 4], - channel_dims_per_stage=[nc * 2, nc * 2, nc * 3, nc * 3, nc * 3, nc*4]) - - -def hourglass_100(num_channels, initial_downsample=True): - nc = num_channels - return single_stage_hourglass( - input_channel_dims=nc, - initial_downsample=initial_downsample, - blocks_per_stage=[4, 4, 4, 4, 4, 8], - channel_dims_per_stage=[nc * 2, nc * 2, nc * 3, nc * 3, nc * 3, nc*4]) - - -def hourglass_20_uniform_size(num_channels): - nc = num_channels - return single_stage_hourglass( - input_channel_dims=nc, - blocks_per_stage=[1, 2, 2], - channel_dims_per_stage=[nc * 2, nc * 2, nc * 3], - initial_downsample=False, - stagewise_downsample=False) - - -def hourglass_20_no_shortcut(num_channels): - nc = num_channels - return single_stage_hourglass( - input_channel_dims=nc, - blocks_per_stage=[1, 2, 2], - channel_dims_per_stage=[nc * 2, nc * 2, nc * 3], - initial_downsample=False, - encoder_decoder_shortcut=False) diff --git a/research/object_detection/models/keras_models/hourglass_network_tf2_test.py b/research/object_detection/models/keras_models/hourglass_network_tf2_test.py deleted file mode 100644 index d1813703c7c..00000000000 --- a/research/object_detection/models/keras_models/hourglass_network_tf2_test.py +++ /dev/null @@ -1,158 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Testing the Hourglass network.""" -import unittest -from absl.testing import parameterized -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models.keras_models import hourglass_network as hourglass -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class HourglassFeatureExtractorTest(tf.test.TestCase, parameterized.TestCase): - - def test_identity_layer(self): - - layer = hourglass.IdentityLayer() - output = layer(np.zeros((2, 32, 32, 3), dtype=np.float32)) - self.assertEqual(output.shape, (2, 32, 32, 3)) - - def test_skip_conv_layer_stride_1(self): - - layer = hourglass.SkipConvolution(out_channels=8, stride=1) - output = layer(np.zeros((2, 32, 32, 3), dtype=np.float32)) - self.assertEqual(output.shape, (2, 32, 32, 8)) - - def test_skip_conv_layer_stride_2(self): - - layer = hourglass.SkipConvolution(out_channels=8, stride=2) - output = layer(np.zeros((2, 32, 32, 3), dtype=np.float32)) - self.assertEqual(output.shape, (2, 16, 16, 8)) - - @parameterized.parameters([{'kernel_size': 1}, - {'kernel_size': 3}, - {'kernel_size': 7}]) - def test_conv_block(self, kernel_size): - - layer = hourglass.ConvolutionalBlock( - out_channels=8, kernel_size=kernel_size, stride=1) - output = layer(np.zeros((2, 32, 32, 3), dtype=np.float32)) - self.assertEqual(output.shape, (2, 32, 32, 8)) - - layer = hourglass.ConvolutionalBlock( - out_channels=8, kernel_size=kernel_size, stride=2) - output = layer(np.zeros((2, 32, 32, 3), dtype=np.float32)) - self.assertEqual(output.shape, (2, 16, 16, 8)) - - def test_residual_block_stride_1(self): - - layer = hourglass.ResidualBlock(out_channels=8, stride=1) - output = layer(np.zeros((2, 32, 32, 8), dtype=np.float32)) - self.assertEqual(output.shape, (2, 32, 32, 8)) - - def test_residual_block_stride_2(self): - - layer = hourglass.ResidualBlock(out_channels=8, stride=2, - skip_conv=True) - output = layer(np.zeros((2, 32, 32, 8), dtype=np.float32)) - self.assertEqual(output.shape, (2, 16, 16, 8)) - - def test_input_downsample_block(self): - - layer = hourglass.InputDownsampleBlock( - out_channels_initial_conv=4, out_channels_residual_block=8) - output = layer(np.zeros((2, 32, 32, 8), dtype=np.float32)) - self.assertEqual(output.shape, (2, 8, 8, 8)) - - def test_input_conv_block(self): - layer = hourglass.InputConvBlock( - out_channels_initial_conv=4, out_channels_residual_block=8) - output = layer(np.zeros((2, 32, 32, 8), dtype=np.float32)) - self.assertEqual(output.shape, (2, 32, 32, 8)) - - def test_encoder_decoder_block(self): - - layer = hourglass.EncoderDecoderBlock( - num_stages=4, blocks_per_stage=[2, 3, 4, 5, 6], - channel_dims=[4, 6, 8, 10, 12]) - output = layer(np.zeros((2, 64, 64, 4), dtype=np.float32)) - self.assertEqual(output.shape, (2, 64, 64, 4)) - - def test_hourglass_feature_extractor(self): - - model = hourglass.HourglassNetwork( - num_stages=4, blocks_per_stage=[2, 3, 4, 5, 6], input_channel_dims=4, - channel_dims_per_stage=[6, 8, 10, 12, 14], num_hourglasses=2) - outputs = model(np.zeros((2, 64, 64, 3), dtype=np.float32)) - self.assertEqual(outputs[0].shape, (2, 16, 16, 6)) - self.assertEqual(outputs[1].shape, (2, 16, 16, 6)) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class HourglassDepthTest(tf.test.TestCase): - - def test_hourglass_104(self): - - net = hourglass.hourglass_104() - self.assertEqual(hourglass.hourglass_depth(net), 104) - - def test_hourglass_10(self): - net = hourglass.hourglass_10(2, initial_downsample=False) - self.assertEqual(hourglass.hourglass_depth(net), 10) - - outputs = net(tf.zeros((2, 32, 32, 3))) - self.assertEqual(outputs[0].shape, (2, 32, 32, 4)) - - def test_hourglass_20(self): - net = hourglass.hourglass_20(2, initial_downsample=False) - self.assertEqual(hourglass.hourglass_depth(net), 20) - - outputs = net(tf.zeros((2, 32, 32, 3))) - self.assertEqual(outputs[0].shape, (2, 32, 32, 4)) - - def test_hourglass_32(self): - net = hourglass.hourglass_32(2, initial_downsample=False) - self.assertEqual(hourglass.hourglass_depth(net), 32) - - outputs = net(tf.zeros((2, 32, 32, 3))) - self.assertEqual(outputs[0].shape, (2, 32, 32, 4)) - - def test_hourglass_52(self): - net = hourglass.hourglass_52(2, initial_downsample=False) - self.assertEqual(hourglass.hourglass_depth(net), 52) - - outputs = net(tf.zeros((2, 32, 32, 3))) - self.assertEqual(outputs[0].shape, (2, 32, 32, 4)) - - def test_hourglass_20_uniform_size(self): - net = hourglass.hourglass_20_uniform_size(2) - self.assertEqual(hourglass.hourglass_depth(net), 20) - - outputs = net(tf.zeros((2, 32, 32, 3))) - self.assertEqual(outputs[0].shape, (2, 32, 32, 4)) - - def test_hourglass_100(self): - net = hourglass.hourglass_100(2, initial_downsample=False) - self.assertEqual(hourglass.hourglass_depth(net), 100) - - outputs = net(tf.zeros((2, 32, 32, 3))) - self.assertEqual(outputs[0].shape, (2, 32, 32, 4)) - - -if __name__ == '__main__': - tf.test.main() - diff --git a/research/object_detection/models/keras_models/inception_resnet_v2.py b/research/object_detection/models/keras_models/inception_resnet_v2.py deleted file mode 100644 index 9ecdfa2615f..00000000000 --- a/research/object_detection/models/keras_models/inception_resnet_v2.py +++ /dev/null @@ -1,244 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A wrapper around the Keras InceptionResnetV2 models for object detection.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf - -from object_detection.core import freezable_batch_norm - - -class _LayersOverride(object): - """Alternative Keras layers interface for the Keras InceptionResNetV2.""" - - def __init__(self, - batchnorm_training, - output_stride=16, - align_feature_maps=False, - batchnorm_scale=False, - default_batchnorm_momentum=0.999, - default_batchnorm_epsilon=1e-3, - weight_decay=0.00004): - """Alternative tf.keras.layers interface, for use by InceptionResNetV2. - - It is used by the Keras applications kwargs injection API to - modify the Inception Resnet V2 Keras application with changes required by - the Object Detection API. - - These injected interfaces make the following changes to the network: - - - Supports freezing batch norm layers - - Adds support for feature map alignment (like in the Slim model) - - Adds support for changing the output stride (like in the Slim model) - - Adds support for overriding various batch norm hyperparameters - - Because the Keras inception resnet v2 application does not assign explicit - names to most individual layers, the injection of output stride support - works by identifying convolution layers according to their filter counts - and pre-feature-map-alignment padding arguments. - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - output_stride: A scalar that specifies the requested ratio of input to - output spatial resolution. Only supports 8 and 16. - align_feature_maps: When true, changes all the VALID paddings in the - network to SAME padding so that the feature maps are aligned. - batchnorm_scale: If True, uses an explicit `gamma` multiplier to scale the - activations in the batch normalization layer. - default_batchnorm_momentum: Float. Batch norm layers will be constructed - using this value as the momentum. - default_batchnorm_epsilon: small float added to variance to avoid - dividing by zero. - weight_decay: the l2 regularization weight decay for weights variables. - (gets multiplied by 0.5 to map from slim l2 regularization weight to - Keras l2 regularization weight). - """ - self._use_atrous = output_stride == 8 - self._align_feature_maps = align_feature_maps - self._batchnorm_training = batchnorm_training - self._batchnorm_scale = batchnorm_scale - self._default_batchnorm_momentum = default_batchnorm_momentum - self._default_batchnorm_epsilon = default_batchnorm_epsilon - self.regularizer = tf.keras.regularizers.l2(weight_decay * 0.5) - - def Conv2D(self, filters, kernel_size, **kwargs): - """Builds a Conv2D layer according to the current Object Detection config. - - Overrides the Keras InceptionResnetV2 application's convolutions with ones - that follow the spec specified by the Object Detection hyperparameters. - - If feature map alignment is enabled, the padding will be forced to 'same'. - If output_stride is 8, some conv2d layers will be matched according to - their name or filter counts or pre-alignment padding parameters, and will - have the correct 'dilation rate' or 'strides' set. - - Args: - filters: The number of filters to use for the convolution. - kernel_size: The kernel size to specify the height and width of the 2D - convolution window. - **kwargs: Keyword args specified by the Keras application for - constructing the convolution. - - Returns: - A Keras Conv2D layer specified by the Object Detection hyperparameter - configurations. - """ - kwargs['kernel_regularizer'] = self.regularizer - kwargs['bias_regularizer'] = self.regularizer - - # Because the Keras application does not set explicit names for most layers, - # (instead allowing names to auto-increment), we must match individual - # layers in the model according to their filter count, name, or - # pre-alignment mapping. This means we can only align the feature maps - # after we have applied our updates in cases where output_stride=8. - if self._use_atrous and (filters == 384): - kwargs['strides'] = 1 - - name = kwargs.get('name') - if self._use_atrous and ( - (name and 'block17' in name) or - (filters == 128 or filters == 160 or - (filters == 192 and kwargs.get('padding', '').lower() != 'valid'))): - kwargs['dilation_rate'] = 2 - - if self._align_feature_maps: - kwargs['padding'] = 'same' - - return tf.keras.layers.Conv2D(filters, kernel_size, **kwargs) - - def MaxPooling2D(self, pool_size, strides, **kwargs): - """Builds a pooling layer according to the current Object Detection config. - - Overrides the Keras InceptionResnetV2 application's MaxPooling2D layers with - ones that follow the spec specified by the Object Detection hyperparameters. - - If feature map alignment is enabled, the padding will be forced to 'same'. - If output_stride is 8, some pooling layers will be matched according to - their pre-alignment padding parameters, and will have their 'strides' - argument overridden. - - Args: - pool_size: The pool size specified by the Keras application. - strides: The strides specified by the unwrapped Keras application. - **kwargs: Keyword args specified by the Keras application for - constructing the max pooling layer. - - Returns: - A MaxPool2D layer specified by the Object Detection hyperparameter - configurations. - """ - if self._use_atrous and kwargs.get('padding', '').lower() == 'valid': - strides = 1 - - if self._align_feature_maps: - kwargs['padding'] = 'same' - - return tf.keras.layers.MaxPool2D(pool_size, strides=strides, **kwargs) - - # We alias MaxPool2D because Keras has that alias - MaxPool2D = MaxPooling2D # pylint: disable=invalid-name - - def BatchNormalization(self, **kwargs): - """Builds a normalization layer. - - Overrides the Keras application batch norm with the norm specified by the - Object Detection configuration. - - Args: - **kwargs: Keyword arguments from the `layers.BatchNormalization` calls in - the Keras application. - - Returns: - A normalization layer specified by the Object Detection hyperparameter - configurations. - """ - kwargs['scale'] = self._batchnorm_scale - return freezable_batch_norm.FreezableBatchNorm( - training=self._batchnorm_training, - epsilon=self._default_batchnorm_epsilon, - momentum=self._default_batchnorm_momentum, - **kwargs) - - # Forward all non-overridden methods to the keras layers - def __getattr__(self, item): - return getattr(tf.keras.layers, item) - - -# pylint: disable=invalid-name -def inception_resnet_v2( - batchnorm_training, - output_stride=16, - align_feature_maps=False, - batchnorm_scale=False, - weight_decay=0.00004, - default_batchnorm_momentum=0.9997, - default_batchnorm_epsilon=0.001, - **kwargs): - """Instantiates the InceptionResnetV2 architecture. - - (Modified for object detection) - - This wraps the InceptionResnetV2 tensorflow Keras application, but uses the - Keras application's kwargs-based monkey-patching API to override the Keras - architecture with the following changes: - - - Supports freezing batch norm layers with FreezableBatchNorms - - Adds support for feature map alignment (like in the Slim model) - - Adds support for changing the output stride (like in the Slim model) - - Changes the default batchnorm momentum to 0.9997 - - Adds support for overriding various batchnorm hyperparameters - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - output_stride: A scalar that specifies the requested ratio of input to - output spatial resolution. Only supports 8 and 16. - align_feature_maps: When true, changes all the VALID paddings in the - network to SAME padding so that the feature maps are aligned. - batchnorm_scale: If True, uses an explicit `gamma` multiplier to scale the - activations in the batch normalization layer. - weight_decay: the l2 regularization weight decay for weights variables. - (gets multiplied by 0.5 to map from slim l2 regularization weight to - Keras l2 regularization weight). - default_batchnorm_momentum: Float. Batch norm layers will be constructed - using this value as the momentum. - default_batchnorm_epsilon: small float added to variance to avoid - dividing by zero. - **kwargs: Keyword arguments forwarded directly to the - `tf.keras.applications.InceptionResNetV2` method that constructs the - Keras model. - - Returns: - A Keras model instance. - """ - if output_stride != 8 and output_stride != 16: - raise ValueError('output_stride must be 8 or 16.') - - layers_override = _LayersOverride( - batchnorm_training, - output_stride, - align_feature_maps=align_feature_maps, - batchnorm_scale=batchnorm_scale, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon, - weight_decay=weight_decay) - return tf.keras.applications.InceptionResNetV2( - layers=layers_override, **kwargs) -# pylint: enable=invalid-name diff --git a/research/object_detection/models/keras_models/inception_resnet_v2_tf2_test.py b/research/object_detection/models/keras_models/inception_resnet_v2_tf2_test.py deleted file mode 100644 index 2b3f44f6e45..00000000000 --- a/research/object_detection/models/keras_models/inception_resnet_v2_tf2_test.py +++ /dev/null @@ -1,227 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for inception_resnet_v2.py. - -This test mainly focuses on comparing slim inception resnet v2 and Keras -inception resnet v2 for object detection. To verify the consistency of the two -models, we compare: - 1. Output shape of each layer given different inputs - 2. Number of global variables - -We also visualize the model structure via Tensorboard, and compare the model -layout and the parameters of each Op to make sure the two implementations are -consistent. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import unittest -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.models.keras_models import inception_resnet_v2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - -_KERAS_TO_SLIM_ENDPOINT_NAMES = { - 'activation': 'Conv2d_1a_3x3', - 'activation_1': 'Conv2d_2a_3x3', - 'activation_2': 'Conv2d_2b_3x3', - 'activation_3': 'Conv2d_3b_1x1', - 'activation_4': 'Conv2d_4a_3x3', - 'max_pooling2d': 'MaxPool_3a_3x3', - 'max_pooling2d_1': 'MaxPool_5a_3x3', - 'mixed_5b': 'Mixed_5b', - 'mixed_6a': 'Mixed_6a', - 'block17_20_ac': 'PreAuxLogits', - 'mixed_7a': 'Mixed_7a', - 'conv_7b_ac': 'Conv2d_7b_1x1', -} - -_SLIM_ENDPOINT_SHAPES_128 = { - 'Conv2d_1a_3x3': (2, 64, 64, 32), - 'Conv2d_2a_3x3': (2, 64, 64, 32), - 'Conv2d_2b_3x3': (2, 64, 64, 64), - 'Conv2d_3b_1x1': (2, 32, 32, 80), - 'Conv2d_4a_3x3': (2, 32, 32, 192), - 'Conv2d_7b_1x1': (2, 4, 4, 1536), - 'MaxPool_3a_3x3': (2, 32, 32, 64), - 'MaxPool_5a_3x3': (2, 16, 16, 192), - 'Mixed_5b': (2, 16, 16, 320), - 'Mixed_6a': (2, 8, 8, 1088), - 'Mixed_7a': (2, 4, 4, 2080), - 'PreAuxLogits': (2, 8, 8, 1088)} -_SLIM_ENDPOINT_SHAPES_128_STRIDE_8 = { - 'Conv2d_1a_3x3': (2, 64, 64, 32), - 'Conv2d_2a_3x3': (2, 64, 64, 32), - 'Conv2d_2b_3x3': (2, 64, 64, 64), - 'Conv2d_3b_1x1': (2, 32, 32, 80), - 'Conv2d_4a_3x3': (2, 32, 32, 192), - 'MaxPool_3a_3x3': (2, 32, 32, 64), - 'MaxPool_5a_3x3': (2, 16, 16, 192), - 'Mixed_5b': (2, 16, 16, 320), - 'Mixed_6a': (2, 16, 16, 1088), - 'PreAuxLogits': (2, 16, 16, 1088)} -_SLIM_ENDPOINT_SHAPES_128_ALIGN_FEATURE_MAPS_FALSE = { - 'Conv2d_1a_3x3': (2, 63, 63, 32), - 'Conv2d_2a_3x3': (2, 61, 61, 32), - 'Conv2d_2b_3x3': (2, 61, 61, 64), - 'Conv2d_3b_1x1': (2, 30, 30, 80), - 'Conv2d_4a_3x3': (2, 28, 28, 192), - 'Conv2d_7b_1x1': (2, 2, 2, 1536), - 'MaxPool_3a_3x3': (2, 30, 30, 64), - 'MaxPool_5a_3x3': (2, 13, 13, 192), - 'Mixed_5b': (2, 13, 13, 320), - 'Mixed_6a': (2, 6, 6, 1088), - 'Mixed_7a': (2, 2, 2, 2080), - 'PreAuxLogits': (2, 6, 6, 1088)} -_SLIM_ENDPOINT_SHAPES_299 = {} -_SLIM_ENDPOINT_SHAPES_299_STRIDE_8 = {} -_SLIM_ENDPOINT_SHAPES_299_ALIGN_FEATURE_MAPS_FALSE = {} - -_KERAS_LAYERS_TO_CHECK = list(_KERAS_TO_SLIM_ENDPOINT_NAMES.keys()) - -_NUM_CHANNELS = 3 -_BATCH_SIZE = 2 - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class InceptionResnetV2Test(test_case.TestCase): - - def _create_application_with_layer_outputs( - self, layer_names, batchnorm_training, - output_stride=16, - align_feature_maps=False, - batchnorm_scale=False, - weight_decay=0.00004, - default_batchnorm_momentum=0.9997, - default_batchnorm_epsilon=0.001,): - """Constructs Keras inception_resnet_v2 that extracts layer outputs.""" - # Have to clear the Keras backend to ensure isolation in layer naming - tf.keras.backend.clear_session() - if not layer_names: - layer_names = _KERAS_LAYERS_TO_CHECK - full_model = inception_resnet_v2.inception_resnet_v2( - batchnorm_training=batchnorm_training, - output_stride=output_stride, - align_feature_maps=align_feature_maps, - weights=None, - batchnorm_scale=batchnorm_scale, - weight_decay=weight_decay, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon, - include_top=False) - layer_outputs = [full_model.get_layer(name=layer).output - for layer in layer_names] - return tf.keras.Model( - inputs=full_model.inputs, - outputs=layer_outputs) - - def _check_returns_correct_shape( - self, image_height, image_width, - expected_feature_map_shape, layer_names=None, batchnorm_training=True, - output_stride=16, - align_feature_maps=False, - batchnorm_scale=False, - weight_decay=0.00004, - default_batchnorm_momentum=0.9997, - default_batchnorm_epsilon=0.001,): - if not layer_names: - layer_names = _KERAS_LAYERS_TO_CHECK - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=batchnorm_training, - output_stride=output_stride, - align_feature_maps=align_feature_maps, - batchnorm_scale=batchnorm_scale, - weight_decay=weight_decay, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon) - - image_tensor = np.random.rand(_BATCH_SIZE, image_height, image_width, - _NUM_CHANNELS).astype(np.float32) - feature_maps = model(image_tensor) - - for feature_map, layer_name in zip(feature_maps, layer_names): - endpoint_name = _KERAS_TO_SLIM_ENDPOINT_NAMES[layer_name] - expected_shape = expected_feature_map_shape[endpoint_name] - self.assertAllEqual(feature_map.shape, expected_shape) - - def _get_variables(self, layer_names=None): - tf.keras.backend.clear_session() - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=False) - preprocessed_inputs = tf.random.uniform([4, 40, 40, _NUM_CHANNELS]) - model(preprocessed_inputs) - return model.variables - - def test_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - expected_feature_map_shape = ( - _SLIM_ENDPOINT_SHAPES_128) - self._check_returns_correct_shape( - image_height, image_width, expected_feature_map_shape, - align_feature_maps=True) - - def test_returns_correct_shapes_128_output_stride_8(self): - image_height = 128 - image_width = 128 - expected_feature_map_shape = ( - _SLIM_ENDPOINT_SHAPES_128_STRIDE_8) - - # Output stride of 8 not defined beyond 'block17_20_ac', which is - # PreAuxLogits in slim. So, we exclude those layers in our Keras vs Slim - # comparison. - excluded_layers = {'mixed_7a', 'conv_7b_ac'} - layer_names = [l for l in _KERAS_LAYERS_TO_CHECK - if l not in excluded_layers] - self._check_returns_correct_shape( - image_height, image_width, expected_feature_map_shape, - layer_names=layer_names, output_stride=8, align_feature_maps=True) - - def test_returns_correct_shapes_128_align_feature_maps_false( - self): - image_height = 128 - image_width = 128 - expected_feature_map_shape = ( - _SLIM_ENDPOINT_SHAPES_128_ALIGN_FEATURE_MAPS_FALSE) - self._check_returns_correct_shape( - image_height, image_width, expected_feature_map_shape, - align_feature_maps=False) - - def test_hyperparam_override(self): - model = inception_resnet_v2.inception_resnet_v2( - batchnorm_training=True, - default_batchnorm_momentum=0.2, - default_batchnorm_epsilon=0.1, - weights=None, - include_top=False) - bn_layer = model.get_layer(name='freezable_batch_norm') - self.assertAllClose(bn_layer.momentum, 0.2) - self.assertAllClose(bn_layer.epsilon, 0.1) - - def test_variable_count(self): - variables = self._get_variables() - # 896 is the number of variables from slim inception resnet v2 model. - self.assertEqual(len(variables), 896) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/keras_models/mobilenet_v1.py b/research/object_detection/models/keras_models/mobilenet_v1.py deleted file mode 100644 index 71c20e2cead..00000000000 --- a/research/object_detection/models/keras_models/mobilenet_v1.py +++ /dev/null @@ -1,358 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A wrapper around the Keras MobilenetV1 models for object detection.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf - -from object_detection.core import freezable_batch_norm -from object_detection.models.keras_models import model_utils - - -def _fixed_padding(inputs, kernel_size, rate=1): # pylint: disable=invalid-name - """Pads the input along the spatial dimensions independently of input size. - - Pads the input such that if it was used in a convolution with 'VALID' padding, - the output would have the same dimensions as if the unpadded input was used - in a convolution with 'SAME' padding. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - kernel_size: The kernel to be used in the conv2d or max_pool2d operation. - rate: An integer, rate for atrous convolution. - - Returns: - output: A tensor of size [batch, height_out, width_out, channels] with the - input, either intact (if kernel_size == 1) or padded (if kernel_size > 1). - """ - kernel_size_effective = [kernel_size[0] + (kernel_size[0] - 1) * (rate - 1), - kernel_size[0] + (kernel_size[0] - 1) * (rate - 1)] - pad_total = [kernel_size_effective[0] - 1, kernel_size_effective[1] - 1] - pad_beg = [pad_total[0] // 2, pad_total[1] // 2] - pad_end = [pad_total[0] - pad_beg[0], pad_total[1] - pad_beg[1]] - padded_inputs = tf.pad(inputs, [[0, 0], [pad_beg[0], pad_end[0]], - [pad_beg[1], pad_end[1]], [0, 0]]) - return padded_inputs - - -class _LayersOverride(object): - """Alternative Keras layers interface for the Keras MobileNetV1.""" - - def __init__(self, - batchnorm_training, - default_batchnorm_momentum=0.999, - conv_hyperparams=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=None, - conv_defs=None): - """Alternative tf.keras.layers interface, for use by the Keras MobileNetV1. - - It is used by the Keras applications kwargs injection API to - modify the MobilenetV1 Keras application with changes required by - the Object Detection API. - - These injected interfaces make the following changes to the network: - - - Applies the Object Detection hyperparameter configuration - - Supports FreezableBatchNorms - - Adds support for a min number of filters for each layer - - Makes the `alpha` parameter affect the final convolution block even if it - is less than 1.0 - - Adds support for explicit padding of convolutions - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default mobilenet_v1 layer builders. - use_explicit_padding: If True, use 'valid' padding for convolutions, - but explicitly pre-pads inputs so that the output dimensions are the - same as if 'same' padding were used. Off by default. - alpha: The width multiplier referenced in the MobileNetV1 paper. It - modifies the number of filters in each convolutional layer. It's called - depth multiplier in Keras application MobilenetV1. - min_depth: Minimum number of filters in the convolutional layers. - conv_defs: Network layout to specify the mobilenet_v1 body. Default is - `None` to use the default mobilenet_v1 network layout. - """ - self._alpha = alpha - self._batchnorm_training = batchnorm_training - self._default_batchnorm_momentum = default_batchnorm_momentum - self._conv_hyperparams = conv_hyperparams - self._use_explicit_padding = use_explicit_padding - self._min_depth = min_depth - self._conv_defs = conv_defs - self.regularizer = tf.keras.regularizers.l2(0.00004 * 0.5) - self.initializer = tf.truncated_normal_initializer(stddev=0.09) - - def _FixedPaddingLayer(self, kernel_size, rate=1): - return tf.keras.layers.Lambda( - lambda x: _fixed_padding(x, kernel_size, rate)) - - def Conv2D(self, filters, kernel_size, **kwargs): - """Builds a Conv2D layer according to the current Object Detection config. - - Overrides the Keras MobileNetV1 application's convolutions with ones that - follow the spec specified by the Object Detection hyperparameters. - - Args: - filters: The number of filters to use for the convolution. - kernel_size: The kernel size to specify the height and width of the 2D - convolution window. In this function, the kernel size is expected to - be pair of numbers and the numbers must be equal for this function. - **kwargs: Keyword args specified by the Keras application for - constructing the convolution. - - Returns: - A one-arg callable that will either directly apply a Keras Conv2D layer to - the input argument, or that will first pad the input then apply a Conv2D - layer. - - Raises: - ValueError: if kernel size is not a pair of equal - integers (representing a square kernel). - """ - if not isinstance(kernel_size, tuple): - raise ValueError('kernel is expected to be a tuple.') - if len(kernel_size) != 2: - raise ValueError('kernel is expected to be length two.') - if kernel_size[0] != kernel_size[1]: - raise ValueError('kernel is expected to be square.') - layer_name = kwargs['name'] - if self._conv_defs: - conv_filters = model_utils.get_conv_def(self._conv_defs, layer_name) - if conv_filters: - filters = conv_filters - # Apply the width multiplier and the minimum depth to the convolution layers - filters = int(filters * self._alpha) - if self._min_depth and filters < self._min_depth: - filters = self._min_depth - - if self._conv_hyperparams: - kwargs = self._conv_hyperparams.params(**kwargs) - else: - kwargs['kernel_regularizer'] = self.regularizer - kwargs['kernel_initializer'] = self.initializer - - kwargs['padding'] = 'same' - if self._use_explicit_padding and kernel_size[0] > 1: - kwargs['padding'] = 'valid' - def padded_conv(features): # pylint: disable=invalid-name - padded_features = self._FixedPaddingLayer(kernel_size)(features) - return tf.keras.layers.Conv2D( - filters, kernel_size, **kwargs)(padded_features) - return padded_conv - else: - return tf.keras.layers.Conv2D(filters, kernel_size, **kwargs) - - def DepthwiseConv2D(self, kernel_size, **kwargs): - """Builds a DepthwiseConv2D according to the Object Detection config. - - Overrides the Keras MobileNetV2 application's convolutions with ones that - follow the spec specified by the Object Detection hyperparameters. - - Args: - kernel_size: The kernel size to specify the height and width of the 2D - convolution window. - **kwargs: Keyword args specified by the Keras application for - constructing the convolution. - - Returns: - A one-arg callable that will either directly apply a Keras DepthwiseConv2D - layer to the input argument, or that will first pad the input then apply - the depthwise convolution. - """ - if self._conv_hyperparams: - kwargs = self._conv_hyperparams.params(**kwargs) - # Both regularizer and initializaer also applies to depthwise layer in - # MobilenetV1, so we remap the kernel_* to depthwise_* here. - kwargs['depthwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['depthwise_initializer'] = kwargs['kernel_initializer'] - else: - kwargs['depthwise_regularizer'] = self.regularizer - kwargs['depthwise_initializer'] = self.initializer - - kwargs['padding'] = 'same' - if self._use_explicit_padding: - kwargs['padding'] = 'valid' - def padded_depthwise_conv(features): # pylint: disable=invalid-name - padded_features = self._FixedPaddingLayer(kernel_size)(features) - return tf.keras.layers.DepthwiseConv2D( - kernel_size, **kwargs)(padded_features) - return padded_depthwise_conv - else: - return tf.keras.layers.DepthwiseConv2D(kernel_size, **kwargs) - - def BatchNormalization(self, **kwargs): - """Builds a normalization layer. - - Overrides the Keras application batch norm with the norm specified by the - Object Detection configuration. - - Args: - **kwargs: Only the name is used, all other params ignored. - Required for matching `layers.BatchNormalization` calls in the Keras - application. - - Returns: - A normalization layer specified by the Object Detection hyperparameter - configurations. - """ - name = kwargs.get('name') - if self._conv_hyperparams: - return self._conv_hyperparams.build_batch_norm( - training=self._batchnorm_training, - name=name) - else: - return freezable_batch_norm.FreezableBatchNorm( - training=self._batchnorm_training, - epsilon=1e-3, - momentum=self._default_batchnorm_momentum, - name=name) - - def Input(self, shape): - """Builds an Input layer. - - Overrides the Keras application Input layer with one that uses a - tf.placeholder_with_default instead of a tf.placeholder. This is necessary - to ensure the application works when run on a TPU. - - Args: - shape: The shape for the input layer to use. (Does not include a dimension - for the batch size). - Returns: - An input layer for the specified shape that internally uses a - placeholder_with_default. - """ - default_size = 224 - default_batch_size = 1 - shape = list(shape) - default_shape = [default_size if dim is None else dim for dim in shape] - - input_tensor = tf.constant(0.0, shape=[default_batch_size] + default_shape) - - placeholder_with_default = tf.placeholder_with_default( - input=input_tensor, shape=[None] + shape) - return model_utils.input_layer(shape, placeholder_with_default) - - # pylint: disable=unused-argument - def ReLU(self, *args, **kwargs): - """Builds an activation layer. - - Overrides the Keras application ReLU with the activation specified by the - Object Detection configuration. - - Args: - *args: Ignored, required to match the `tf.keras.ReLU` interface - **kwargs: Only the name is used, - required to match `tf.keras.ReLU` interface - - Returns: - An activation layer specified by the Object Detection hyperparameter - configurations. - """ - name = kwargs.get('name') - if self._conv_hyperparams: - return self._conv_hyperparams.build_activation_layer(name=name) - else: - return tf.keras.layers.Lambda(tf.nn.relu6, name=name) - # pylint: enable=unused-argument - - # pylint: disable=unused-argument - def ZeroPadding2D(self, padding, **kwargs): - """Replaces explicit padding in the Keras application with a no-op. - - Args: - padding: The padding values for image height and width. - **kwargs: Ignored, required to match the Keras applications usage. - - Returns: - A no-op identity lambda. - """ - return lambda x: x - # pylint: enable=unused-argument - - # Forward all non-overridden methods to the keras layers - def __getattr__(self, item): - return getattr(tf.keras.layers, item) - - -# pylint: disable=invalid-name -def mobilenet_v1(batchnorm_training, - default_batchnorm_momentum=0.9997, - conv_hyperparams=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=None, - conv_defs=None, - **kwargs): - """Instantiates the MobileNetV1 architecture, modified for object detection. - - This wraps the MobileNetV1 tensorflow Keras application, but uses the - Keras application's kwargs-based monkey-patching API to override the Keras - architecture with the following changes: - - - Changes the default batchnorm momentum to 0.9997 - - Applies the Object Detection hyperparameter configuration - - Supports FreezableBatchNorms - - Adds support for a min number of filters for each layer - - Makes the `alpha` parameter affect the final convolution block even if it - is less than 1.0 - - Adds support for explicit padding of convolutions - - Makes the Input layer use a tf.placeholder_with_default instead of a - tf.placeholder, to work on TPUs. - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default mobilenet_v1 layer builders. - use_explicit_padding: If True, use 'valid' padding for convolutions, - but explicitly pre-pads inputs so that the output dimensions are the - same as if 'same' padding were used. Off by default. - alpha: The width multiplier referenced in the MobileNetV1 paper. It - modifies the number of filters in each convolutional layer. - min_depth: Minimum number of filters in the convolutional layers. - conv_defs: Network layout to specify the mobilenet_v1 body. Default is - `None` to use the default mobilenet_v1 network layout. - **kwargs: Keyword arguments forwarded directly to the - `tf.keras.applications.Mobilenet` method that constructs the Keras - model. - - Returns: - A Keras model instance. - """ - layers_override = _LayersOverride( - batchnorm_training, - default_batchnorm_momentum=default_batchnorm_momentum, - conv_hyperparams=conv_hyperparams, - use_explicit_padding=use_explicit_padding, - min_depth=min_depth, - alpha=alpha, - conv_defs=conv_defs) - return tf.keras.applications.MobileNet( - alpha=alpha, layers=layers_override, **kwargs) -# pylint: enable=invalid-name diff --git a/research/object_detection/models/keras_models/mobilenet_v1_tf2_test.py b/research/object_detection/models/keras_models/mobilenet_v1_tf2_test.py deleted file mode 100644 index 4892e660c35..00000000000 --- a/research/object_detection/models/keras_models/mobilenet_v1_tf2_test.py +++ /dev/null @@ -1,255 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for mobilenet_v1.py. - -This test mainly focuses on comparing slim MobilenetV1 and Keras MobilenetV1 for -object detection. To verify the consistency of the two models, we compare: - 1. Output shape of each layer given different inputs - 2. Number of global variables - -We also visualize the model structure via Tensorboard, and compare the model -layout and the parameters of each Op to make sure the two implementations are -consistent. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import unittest -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import hyperparams_builder -from object_detection.models.keras_models import mobilenet_v1 -from object_detection.models.keras_models import model_utils -from object_detection.models.keras_models import test_utils -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - -_KERAS_LAYERS_TO_CHECK = [ - 'conv1_relu', - 'conv_dw_1_relu', 'conv_pw_1_relu', - 'conv_dw_2_relu', 'conv_pw_2_relu', - 'conv_dw_3_relu', 'conv_pw_3_relu', - 'conv_dw_4_relu', 'conv_pw_4_relu', - 'conv_dw_5_relu', 'conv_pw_5_relu', - 'conv_dw_6_relu', 'conv_pw_6_relu', - 'conv_dw_7_relu', 'conv_pw_7_relu', - 'conv_dw_8_relu', 'conv_pw_8_relu', - 'conv_dw_9_relu', 'conv_pw_9_relu', - 'conv_dw_10_relu', 'conv_pw_10_relu', - 'conv_dw_11_relu', 'conv_pw_11_relu', - 'conv_dw_12_relu', 'conv_pw_12_relu', - 'conv_dw_13_relu', 'conv_pw_13_relu', -] - -_NUM_CHANNELS = 3 -_BATCH_SIZE = 2 - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class MobilenetV1Test(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - train: true, - scale: false, - center: true, - decay: 0.2, - epsilon: 0.1, - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def _create_application_with_layer_outputs( - self, layer_names, batchnorm_training, - conv_hyperparams=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=None, - conv_defs=None): - """Constructs Keras MobilenetV1 that extracts intermediate layer outputs.""" - if not layer_names: - layer_names = _KERAS_LAYERS_TO_CHECK - full_model = mobilenet_v1.mobilenet_v1( - batchnorm_training=batchnorm_training, - conv_hyperparams=conv_hyperparams, - weights=None, - use_explicit_padding=use_explicit_padding, - alpha=alpha, - min_depth=min_depth, - conv_defs=conv_defs, - include_top=False) - layer_outputs = [full_model.get_layer(name=layer).output - for layer in layer_names] - return tf.keras.Model( - inputs=full_model.inputs, - outputs=layer_outputs) - - def _check_returns_correct_shape( - self, image_height, image_width, depth_multiplier, - expected_feature_map_shape, use_explicit_padding=False, min_depth=8, - layer_names=None, conv_defs=None): - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=False, - use_explicit_padding=use_explicit_padding, - min_depth=min_depth, - alpha=depth_multiplier, - conv_defs=conv_defs) - - image_tensor = np.random.rand(_BATCH_SIZE, image_height, image_width, - _NUM_CHANNELS).astype(np.float32) - feature_maps = model(image_tensor) - - for feature_map, expected_shape in zip(feature_maps, - expected_feature_map_shape): - self.assertAllEqual(feature_map.shape, expected_shape) - - def _check_returns_correct_shapes_with_dynamic_inputs( - self, image_height, image_width, depth_multiplier, - expected_feature_map_shape, use_explicit_padding=False, min_depth=8, - layer_names=None): - image_tensor = tf.random_uniform([_BATCH_SIZE, image_height, image_width, - _NUM_CHANNELS], dtype=tf.float32) - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=False, - use_explicit_padding=use_explicit_padding, - alpha=depth_multiplier) - - feature_maps = model(image_tensor) - - for feature_map, expected_shape in zip(feature_maps, - expected_feature_map_shape): - self.assertAllEqual(feature_map.shape, expected_shape) - - def _get_variables(self, depth_multiplier, layer_names=None): - tf.keras.backend.clear_session() - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=False, use_explicit_padding=False, - alpha=depth_multiplier) - preprocessed_inputs = tf.random.uniform([2, 40, 40, 3]) - model(preprocessed_inputs) - return model.variables - - def test_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.moblenet_v1_expected_feature_map_shape_128) - self._check_returns_correct_shape( - image_height, image_width, depth_multiplier, expected_feature_map_shape) - - def test_returns_correct_shapes_128_explicit_padding( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.moblenet_v1_expected_feature_map_shape_128_explicit_padding) - self._check_returns_correct_shape( - image_height, image_width, depth_multiplier, expected_feature_map_shape, - use_explicit_padding=True) - - def test_returns_correct_shapes_with_dynamic_inputs( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.mobilenet_v1_expected_feature_map_shape_with_dynamic_inputs) - self._check_returns_correct_shapes_with_dynamic_inputs( - image_height, image_width, depth_multiplier, expected_feature_map_shape) - - def test_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.moblenet_v1_expected_feature_map_shape_299) - self._check_returns_correct_shape( - image_height, image_width, depth_multiplier, expected_feature_map_shape) - - def test_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - expected_feature_map_shape = ( - test_utils.moblenet_v1_expected_feature_map_shape_enforcing_min_depth) - self._check_returns_correct_shape( - image_height, image_width, depth_multiplier, expected_feature_map_shape) - - def test_returns_correct_shapes_with_conv_defs( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - conv_def_block_12 = model_utils.ConvDefs( - conv_name='conv_pw_12', filters=512) - conv_def_block_13 = model_utils.ConvDefs( - conv_name='conv_pw_13', filters=256) - conv_defs = [conv_def_block_12, conv_def_block_13] - - expected_feature_map_shape = ( - test_utils.moblenet_v1_expected_feature_map_shape_with_conv_defs) - self._check_returns_correct_shape( - image_height, image_width, depth_multiplier, expected_feature_map_shape, - conv_defs=conv_defs) - - def test_hyperparam_override(self): - hyperparams = self._build_conv_hyperparams() - model = mobilenet_v1.mobilenet_v1( - batchnorm_training=True, - conv_hyperparams=hyperparams, - weights=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=32, - include_top=False) - hyperparams.params() - bn_layer = model.get_layer(name='conv_pw_5_bn') - self.assertAllClose(bn_layer.momentum, 0.2) - self.assertAllClose(bn_layer.epsilon, 0.1) - - def test_variable_count(self): - depth_multiplier = 1 - variables = self._get_variables(depth_multiplier) - # 135 is the number of variables from slim MobilenetV1 model. - self.assertEqual(len(variables), 135) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/keras_models/mobilenet_v2.py b/research/object_detection/models/keras_models/mobilenet_v2.py deleted file mode 100644 index b534cfbb182..00000000000 --- a/research/object_detection/models/keras_models/mobilenet_v2.py +++ /dev/null @@ -1,334 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A wrapper around the MobileNet v2 models for Keras, for object detection.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import tensorflow.compat.v1 as tf - -from object_detection.core import freezable_batch_norm -from object_detection.models.keras_models import model_utils -from object_detection.utils import ops - - -# pylint: disable=invalid-name -# This method copied from the slim mobilenet base network code (same license) -def _make_divisible(v, divisor, min_value=None): - if min_value is None: - min_value = divisor - new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) - # Make sure that round down does not go down by more than 10%. - if new_v < 0.9 * v: - new_v += divisor - return new_v - - -class _LayersOverride(object): - """Alternative Keras layers interface for the Keras MobileNetV2.""" - - def __init__(self, - batchnorm_training, - default_batchnorm_momentum=0.999, - conv_hyperparams=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=None, - conv_defs=None): - """Alternative tf.keras.layers interface, for use by the Keras MobileNetV2. - - It is used by the Keras applications kwargs injection API to - modify the Mobilenet v2 Keras application with changes required by - the Object Detection API. - - These injected interfaces make the following changes to the network: - - - Applies the Object Detection hyperparameter configuration - - Supports FreezableBatchNorms - - Adds support for a min number of filters for each layer - - Makes the `alpha` parameter affect the final convolution block even if it - is less than 1.0 - - Adds support for explicit padding of convolutions - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default mobilenet_v2 layer builders. - use_explicit_padding: If True, use 'valid' padding for convolutions, - but explicitly pre-pads inputs so that the output dimensions are the - same as if 'same' padding were used. Off by default. - alpha: The width multiplier referenced in the MobileNetV2 paper. It - modifies the number of filters in each convolutional layer. - min_depth: Minimum number of filters in the convolutional layers. - conv_defs: Network layout to specify the mobilenet_v2 body. Default is - `None` to use the default mobilenet_v2 network layout. - """ - self._alpha = alpha - self._batchnorm_training = batchnorm_training - self._default_batchnorm_momentum = default_batchnorm_momentum - self._conv_hyperparams = conv_hyperparams - self._use_explicit_padding = use_explicit_padding - self._min_depth = min_depth - self._conv_defs = conv_defs - self.regularizer = tf.keras.regularizers.l2(0.00004 * 0.5) - self.initializer = tf.truncated_normal_initializer(stddev=0.09) - - def _FixedPaddingLayer(self, kernel_size): - return tf.keras.layers.Lambda(lambda x: ops.fixed_padding(x, kernel_size)) - - def Conv2D(self, filters, **kwargs): - """Builds a Conv2D layer according to the current Object Detection config. - - Overrides the Keras MobileNetV2 application's convolutions with ones that - follow the spec specified by the Object Detection hyperparameters. - - Args: - filters: The number of filters to use for the convolution. - **kwargs: Keyword args specified by the Keras application for - constructing the convolution. - - Returns: - A one-arg callable that will either directly apply a Keras Conv2D layer to - the input argument, or that will first pad the input then apply a Conv2D - layer. - """ - # Make sure 'alpha' is always applied to the last convolution block's size - # (This overrides the Keras application's functionality) - layer_name = kwargs.get('name') - if layer_name == 'Conv_1': - if self._conv_defs: - filters = model_utils.get_conv_def(self._conv_defs, 'Conv_1') - else: - filters = 1280 - if self._alpha < 1.0: - filters = _make_divisible(filters * self._alpha, 8) - - # Apply the minimum depth to the convolution layers - if (self._min_depth and (filters < self._min_depth) - and not kwargs.get('name').endswith('expand')): - filters = self._min_depth - - if self._conv_hyperparams: - kwargs = self._conv_hyperparams.params(**kwargs) - else: - kwargs['kernel_regularizer'] = self.regularizer - kwargs['kernel_initializer'] = self.initializer - - kwargs['padding'] = 'same' - kernel_size = kwargs.get('kernel_size') - if self._use_explicit_padding and kernel_size > 1: - kwargs['padding'] = 'valid' - def padded_conv(features): - padded_features = self._FixedPaddingLayer(kernel_size)(features) - return tf.keras.layers.Conv2D(filters, **kwargs)(padded_features) - - return padded_conv - else: - return tf.keras.layers.Conv2D(filters, **kwargs) - - def DepthwiseConv2D(self, **kwargs): - """Builds a DepthwiseConv2D according to the Object Detection config. - - Overrides the Keras MobileNetV2 application's convolutions with ones that - follow the spec specified by the Object Detection hyperparameters. - - Args: - **kwargs: Keyword args specified by the Keras application for - constructing the convolution. - - Returns: - A one-arg callable that will either directly apply a Keras DepthwiseConv2D - layer to the input argument, or that will first pad the input then apply - the depthwise convolution. - """ - if self._conv_hyperparams: - kwargs = self._conv_hyperparams.params(**kwargs) - # Both the regularizer and initializer apply to the depthwise layer in - # MobilenetV1, so we remap the kernel_* to depthwise_* here. - kwargs['depthwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['depthwise_initializer'] = kwargs['kernel_initializer'] - else: - kwargs['depthwise_regularizer'] = self.regularizer - kwargs['depthwise_initializer'] = self.initializer - - kwargs['padding'] = 'same' - kernel_size = kwargs.get('kernel_size') - if self._use_explicit_padding and kernel_size > 1: - kwargs['padding'] = 'valid' - def padded_depthwise_conv(features): - padded_features = self._FixedPaddingLayer(kernel_size)(features) - return tf.keras.layers.DepthwiseConv2D(**kwargs)(padded_features) - - return padded_depthwise_conv - else: - return tf.keras.layers.DepthwiseConv2D(**kwargs) - - def BatchNormalization(self, **kwargs): - """Builds a normalization layer. - - Overrides the Keras application batch norm with the norm specified by the - Object Detection configuration. - - Args: - **kwargs: Only the name is used, all other params ignored. - Required for matching `layers.BatchNormalization` calls in the Keras - application. - - Returns: - A normalization layer specified by the Object Detection hyperparameter - configurations. - """ - name = kwargs.get('name') - if self._conv_hyperparams: - return self._conv_hyperparams.build_batch_norm( - training=self._batchnorm_training, - name=name) - else: - return freezable_batch_norm.FreezableBatchNorm( - training=self._batchnorm_training, - epsilon=1e-3, - momentum=self._default_batchnorm_momentum, - name=name) - - def Input(self, shape): - """Builds an Input layer. - - Overrides the Keras application Input layer with one that uses a - tf.placeholder_with_default instead of a tf.placeholder. This is necessary - to ensure the application works when run on a TPU. - - Args: - shape: The shape for the input layer to use. (Does not include a dimension - for the batch size). - Returns: - An input layer for the specified shape that internally uses a - placeholder_with_default. - """ - default_size = 224 - default_batch_size = 1 - shape = list(shape) - default_shape = [default_size if dim is None else dim for dim in shape] - - input_tensor = tf.constant(0.0, shape=[default_batch_size] + default_shape) - - placeholder_with_default = tf.placeholder_with_default( - input=input_tensor, shape=[None] + shape) - return model_utils.input_layer(shape, placeholder_with_default) - - # pylint: disable=unused-argument - def ReLU(self, *args, **kwargs): - """Builds an activation layer. - - Overrides the Keras application ReLU with the activation specified by the - Object Detection configuration. - - Args: - *args: Ignored, required to match the `tf.keras.ReLU` interface - **kwargs: Only the name is used, - required to match `tf.keras.ReLU` interface - - Returns: - An activation layer specified by the Object Detection hyperparameter - configurations. - """ - name = kwargs.get('name') - if self._conv_hyperparams: - return self._conv_hyperparams.build_activation_layer(name=name) - else: - return tf.keras.layers.Lambda(tf.nn.relu6, name=name) - # pylint: enable=unused-argument - - # pylint: disable=unused-argument - def ZeroPadding2D(self, **kwargs): - """Replaces explicit padding in the Keras application with a no-op. - - Args: - **kwargs: Ignored, required to match the Keras applications usage. - - Returns: - A no-op identity lambda. - """ - return lambda x: x - # pylint: enable=unused-argument - - # Forward all non-overridden methods to the keras layers - def __getattr__(self, item): - return getattr(tf.keras.layers, item) - - -def mobilenet_v2(batchnorm_training, - default_batchnorm_momentum=0.9997, - conv_hyperparams=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=None, - conv_defs=None, - **kwargs): - """Instantiates the MobileNetV2 architecture, modified for object detection. - - This wraps the MobileNetV2 tensorflow Keras application, but uses the - Keras application's kwargs-based monkey-patching API to override the Keras - architecture with the following changes: - - - Changes the default batchnorm momentum to 0.9997 - - Applies the Object Detection hyperparameter configuration - - Supports FreezableBatchNorms - - Adds support for a min number of filters for each layer - - Makes the `alpha` parameter affect the final convolution block even if it - is less than 1.0 - - Adds support for explicit padding of convolutions - - Makes the Input layer use a tf.placeholder_with_default instead of a - tf.placeholder, to work on TPUs. - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default mobilenet_v2 layer builders. - use_explicit_padding: If True, use 'valid' padding for convolutions, - but explicitly pre-pads inputs so that the output dimensions are the - same as if 'same' padding were used. Off by default. - alpha: The width multiplier referenced in the MobileNetV2 paper. It - modifies the number of filters in each convolutional layer. - min_depth: Minimum number of filters in the convolutional layers. - conv_defs: Network layout to specify the mobilenet_v2 body. Default is - `None` to use the default mobilenet_v2 network layout. - **kwargs: Keyword arguments forwarded directly to the - `tf.keras.applications.MobilenetV2` method that constructs the Keras - model. - - Returns: - A Keras model instance. - """ - layers_override = _LayersOverride( - batchnorm_training, - default_batchnorm_momentum=default_batchnorm_momentum, - conv_hyperparams=conv_hyperparams, - use_explicit_padding=use_explicit_padding, - min_depth=min_depth, - alpha=alpha, - conv_defs=conv_defs) - return tf.keras.applications.MobileNetV2(alpha=alpha, - layers=layers_override, - **kwargs) -# pylint: enable=invalid-name diff --git a/research/object_detection/models/keras_models/mobilenet_v2_tf2_test.py b/research/object_detection/models/keras_models/mobilenet_v2_tf2_test.py deleted file mode 100644 index 8a5dc602641..00000000000 --- a/research/object_detection/models/keras_models/mobilenet_v2_tf2_test.py +++ /dev/null @@ -1,249 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for mobilenet_v2.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import unittest -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format - -from object_detection.builders import hyperparams_builder -from object_detection.models.keras_models import mobilenet_v2 -from object_detection.models.keras_models import model_utils -from object_detection.models.keras_models import test_utils -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - -_layers_to_check = [ - 'Conv1_relu', - 'block_1_expand_relu', 'block_1_depthwise_relu', 'block_1_project_BN', - 'block_2_expand_relu', 'block_2_depthwise_relu', 'block_2_project_BN', - 'block_3_expand_relu', 'block_3_depthwise_relu', 'block_3_project_BN', - 'block_4_expand_relu', 'block_4_depthwise_relu', 'block_4_project_BN', - 'block_5_expand_relu', 'block_5_depthwise_relu', 'block_5_project_BN', - 'block_6_expand_relu', 'block_6_depthwise_relu', 'block_6_project_BN', - 'block_7_expand_relu', 'block_7_depthwise_relu', 'block_7_project_BN', - 'block_8_expand_relu', 'block_8_depthwise_relu', 'block_8_project_BN', - 'block_9_expand_relu', 'block_9_depthwise_relu', 'block_9_project_BN', - 'block_10_expand_relu', 'block_10_depthwise_relu', 'block_10_project_BN', - 'block_11_expand_relu', 'block_11_depthwise_relu', 'block_11_project_BN', - 'block_12_expand_relu', 'block_12_depthwise_relu', 'block_12_project_BN', - 'block_13_expand_relu', 'block_13_depthwise_relu', 'block_13_project_BN', - 'block_14_expand_relu', 'block_14_depthwise_relu', 'block_14_project_BN', - 'block_15_expand_relu', 'block_15_depthwise_relu', 'block_15_project_BN', - 'block_16_expand_relu', 'block_16_depthwise_relu', 'block_16_project_BN', - 'out_relu'] - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class MobilenetV2Test(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - batch_norm { - train: true, - scale: false, - center: true, - decay: 0.2, - epsilon: 0.1, - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def _create_application_with_layer_outputs( - self, layer_names, batchnorm_training, - conv_hyperparams=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=None, - conv_defs=None): - """Constructs Keras mobilenetv2 that extracts intermediate layer outputs.""" - # Have to clear the Keras backend to ensure isolation in layer naming - tf.keras.backend.clear_session() - if not layer_names: - layer_names = _layers_to_check - full_model = mobilenet_v2.mobilenet_v2( - batchnorm_training=batchnorm_training, - conv_hyperparams=conv_hyperparams, - weights=None, - use_explicit_padding=use_explicit_padding, - alpha=alpha, - min_depth=min_depth, - include_top=False, - conv_defs=conv_defs) - layer_outputs = [full_model.get_layer(name=layer).output - for layer in layer_names] - return tf.keras.Model( - inputs=full_model.inputs, - outputs=layer_outputs) - - def _check_returns_correct_shape( - self, batch_size, image_height, image_width, depth_multiplier, - expected_feature_map_shapes, use_explicit_padding=False, min_depth=None, - layer_names=None, conv_defs=None): - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=False, - use_explicit_padding=use_explicit_padding, - min_depth=min_depth, - alpha=depth_multiplier, - conv_defs=conv_defs) - - image_tensor = np.random.rand(batch_size, image_height, image_width, - 3).astype(np.float32) - feature_maps = model([image_tensor]) - - for feature_map, expected_shape in zip(feature_maps, - expected_feature_map_shapes): - self.assertAllEqual(feature_map.shape, expected_shape) - - def _check_returns_correct_shapes_with_dynamic_inputs( - self, batch_size, image_height, image_width, depth_multiplier, - expected_feature_map_shapes, use_explicit_padding=False, - layer_names=None): - height = tf.random.uniform([], minval=image_height, maxval=image_height+1, - dtype=tf.int32) - width = tf.random.uniform([], minval=image_width, maxval=image_width+1, - dtype=tf.int32) - image_tensor = tf.random.uniform([batch_size, height, width, - 3], dtype=tf.float32) - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=False, use_explicit_padding=use_explicit_padding, - alpha=depth_multiplier) - feature_maps = model(image_tensor) - for feature_map, expected_shape in zip(feature_maps, - expected_feature_map_shapes): - self.assertAllEqual(feature_map.shape, expected_shape) - - def _get_variables(self, depth_multiplier, layer_names=None): - tf.keras.backend.clear_session() - model = self._create_application_with_layer_outputs( - layer_names=layer_names, - batchnorm_training=False, use_explicit_padding=False, - alpha=depth_multiplier) - preprocessed_inputs = tf.random.uniform([2, 40, 40, 3]) - model(preprocessed_inputs) - return model.variables - - def test_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.moblenet_v2_expected_feature_map_shape_128) - - self._check_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, - expected_feature_map_shape) - - def test_returns_correct_shapes_128_explicit_padding( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.moblenet_v2_expected_feature_map_shape_128_explicit_padding) - self._check_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, - expected_feature_map_shape, use_explicit_padding=True) - - def test_returns_correct_shapes_with_dynamic_inputs( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.mobilenet_v2_expected_feature_map_shape_with_dynamic_inputs) - self._check_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, - expected_feature_map_shape) - - def test_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - expected_feature_map_shape = ( - test_utils.moblenet_v2_expected_feature_map_shape_299) - self._check_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, - expected_feature_map_shape) - - def test_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - expected_feature_map_shape = ( - test_utils.moblenet_v2_expected_feature_map_shape_enforcing_min_depth) - self._check_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, - expected_feature_map_shape, min_depth=32) - - def test_returns_correct_shapes_with_conv_defs( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - conv_1 = model_utils.ConvDefs( - conv_name='Conv_1', filters=256) - conv_defs = [conv_1] - - expected_feature_map_shape = ( - test_utils.moblenet_v2_expected_feature_map_shape_with_conv_defs) - self._check_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, - expected_feature_map_shape, conv_defs=conv_defs) - - def test_hyperparam_override(self): - hyperparams = self._build_conv_hyperparams() - model = mobilenet_v2.mobilenet_v2( - batchnorm_training=True, - conv_hyperparams=hyperparams, - weights=None, - use_explicit_padding=False, - alpha=1.0, - min_depth=32, - include_top=False) - hyperparams.params() - bn_layer = model.get_layer(name='block_5_project_BN') - self.assertAllClose(bn_layer.momentum, 0.2) - self.assertAllClose(bn_layer.epsilon, 0.1) - - def test_variable_count(self): - depth_multiplier = 1 - variables = self._get_variables(depth_multiplier) - self.assertEqual(len(variables), 260) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/keras_models/model_utils.py b/research/object_detection/models/keras_models/model_utils.py deleted file mode 100644 index 77f3cbd15d7..00000000000 --- a/research/object_detection/models/keras_models/model_utils.py +++ /dev/null @@ -1,53 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Utils for Keras models.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import tensorflow.compat.v1 as tf - -# This is to specify the custom config of model structures. For example, -# ConvDefs(conv_name='conv_pw_12', filters=512) for Mobilenet V1 is to specify -# the filters of the conv layer with name 'conv_pw_12' as 512.s -ConvDefs = collections.namedtuple('ConvDefs', ['conv_name', 'filters']) - - -def get_conv_def(conv_defs, layer_name): - """Get the custom config for some layer of the model structure. - - Args: - conv_defs: A named tuple to specify the custom config of the model - network. See `ConvDefs` for details. - layer_name: A string, the name of the layer to be customized. - - Returns: - The number of filters for the layer, or `None` if there is no custom - config for the requested layer. - """ - for conv_def in conv_defs: - if layer_name == conv_def.conv_name: - return conv_def.filters - return None - - -def input_layer(shape, placeholder_with_default): - if tf.executing_eagerly(): - return tf.keras.layers.Input(shape=shape) - else: - return tf.keras.layers.Input(tensor=placeholder_with_default) diff --git a/research/object_detection/models/keras_models/nonlocal_block.py b/research/object_detection/models/keras_models/nonlocal_block.py deleted file mode 100644 index a6a17255a50..00000000000 --- a/research/object_detection/models/keras_models/nonlocal_block.py +++ /dev/null @@ -1,131 +0,0 @@ -"""Layer for Non-Local operation. - -This is a building block which mimics self-attention in a feature map. - -For more information, please see https://arxiv.org/pdf/1711.07971.pdf -""" - -import tensorflow as tf - -from object_detection.utils import shape_utils - - -class NonLocalBlock(tf.keras.layers.Layer): - """A Non-local block.""" - - def __init__(self, bottleneck_channels, pairwise_fn='dot', pool_size=None, - add_coord_conv=False): - """Constructor. - - Args: - bottleneck_channels: The number of channels used to do pairwise - comparisons at each feature location. - pairwise_fn: The pairwise comparison function. Currently supports - 'dot' and 'embedded_softmax'. - pool_size: The downsample size (achieved with max pool) used prior to - doing pairwise comparisons. This does not affect the shape of the output - tensor, but reduces computation. For a pool_size of 2, computation is - dropped by a factor of 4. If None, no downsampling is performed. - add_coord_conv: Concatenates a 2-channel feature map with normalized - coordinates (in range [-1, 1]) to the input, prior to the - non-local block. - - Raises: - RuntimeError: If self._pairwise_fn is not one of "dot" or - "embedded_softmax". - """ - super().__init__() - self._bottleneck_channels = bottleneck_channels - self._add_coord_conv = add_coord_conv - - self._pool_size = pool_size - if pairwise_fn not in ('dot', 'embedded_softmax'): - raise RuntimeError('pairwise_fn must be one of "dot" or ' - '"embedded_softmax"') - self._pairwise_fn = pairwise_fn - - def build(self, input_shape): - channels = input_shape[-1] - self.queries_conv = tf.keras.layers.Conv2D( - filters=self._bottleneck_channels, kernel_size=1, use_bias=False, - strides=1, padding='same') - self.keys_conv = tf.keras.layers.Conv2D( - filters=self._bottleneck_channels, kernel_size=1, use_bias=False, - strides=1, padding='same') - self.values_conv = tf.keras.layers.Conv2D( - filters=self._bottleneck_channels, kernel_size=1, use_bias=False, - strides=1, padding='same') - self.expand_conv = tf.keras.layers.Conv2D( - filters=channels, kernel_size=1, use_bias=False, strides=1, - padding='same') - self.batchnorm = tf.keras.layers.BatchNormalization( - name='batchnorm', epsilon=1e-5, momentum=0.1, fused=True, - beta_initializer='zeros', gamma_initializer='zeros') - if self._pool_size: - self.maxpool_keys = tf.keras.layers.MaxPool2D( - pool_size=(self._pool_size, self._pool_size)) - self.maxpool_values = tf.keras.layers.MaxPool2D( - pool_size=(self._pool_size, self._pool_size)) - - def call(self, inputs): - """Applies a non-local block to an input feature map. - - Args: - inputs: A [batch, height, width, channels] float32 input tensor. - - Returns: - An output tensor of the same shape as the input. - """ - batch, height, width, _ = shape_utils.combined_static_and_dynamic_shape( - inputs) - - x = inputs - if self._add_coord_conv: - coords_x, coords_y = tf.meshgrid(tf.linspace(-1., 1., height), - tf.linspace(-1., 1., width)) - coords = tf.stack([coords_y, coords_x], axis=-1) - coords = tf.tile(coords[tf.newaxis, :, :, :], - multiples=[batch, 1, 1, 1]) - x = tf.concat([x, coords], axis=-1) - - # shape: [B, H, W, bottleneck_channels]. - queries = self.queries_conv(x) - # shape: [B, H, W, bottleneck_channels]. - keys = self.keys_conv(x) - # shape: [B, H, W, bottleneck_channels]. - values = self.values_conv(x) - - keys_height, keys_width = height, width - if self._pool_size: - keys_height = height // self._pool_size - keys_width = width // self._pool_size - # shape: [B, H', W', bottleneck_channels]. - keys = self.maxpool_keys(keys) - values = self.maxpool_values(values) - - # Produce pairwise scores. - queries = tf.reshape( - queries, [batch, height * width, self._bottleneck_channels]) - keys = tf.reshape( - keys, [batch, keys_height * keys_width, self._bottleneck_channels]) - # shape = [B, H*W, H'*W']. - scores = tf.linalg.matmul(queries, keys, transpose_b=True) - if self._pairwise_fn == 'dot': - normalization = tf.cast(height * width, dtype=tf.float32) - scores = (1./normalization) * scores - elif self._pairwise_fn == 'embedded_softmax': - scores = tf.nn.softmax(scores, axis=-1) - - # Multiply scores with values. - # shape = [B, H'*W', bottleneck_channels]. - values = tf.reshape( - values, [batch, keys_height * keys_width, self._bottleneck_channels]) - # shape = [B, H, W, bottleneck_channels]. - weighted_values = tf.linalg.matmul(scores, values) - weighted_values = tf.reshape( - weighted_values, [batch, height, width, self._bottleneck_channels]) - - # Construct residual. - expand = self.batchnorm(self.expand_conv(weighted_values)) - output = expand + inputs - return output diff --git a/research/object_detection/models/keras_models/nonlocal_block_tf2_test.py b/research/object_detection/models/keras_models/nonlocal_block_tf2_test.py deleted file mode 100644 index 77a816d1759..00000000000 --- a/research/object_detection/models/keras_models/nonlocal_block_tf2_test.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Tests for google3.third_party.tensorflow_models.object_detection.models.keras_models.nonlocal_block.""" -import unittest -from absl.testing import parameterized -import tensorflow as tf - -from object_detection.models.keras_models import nonlocal_block -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class NonlocalTest(test_case.TestCase, parameterized.TestCase): - - @parameterized.parameters([{'pool_size': None, - 'add_coord_conv': False}, - {'pool_size': None, - 'add_coord_conv': True}, - {'pool_size': 2, - 'add_coord_conv': False}, - {'pool_size': 2, - 'add_coord_conv': True}]) - def test_run_nonlocal_block(self, pool_size, add_coord_conv): - nonlocal_op = nonlocal_block.NonLocalBlock( - 8, pool_size=pool_size, add_coord_conv=add_coord_conv) - def graph_fn(): - inputs = tf.zeros((4, 16, 16, 32), dtype=tf.float32) - outputs = nonlocal_op(inputs) - return outputs - outputs = self.execute(graph_fn, []) - self.assertAllEqual([4, 16, 16, 32], outputs.shape) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/keras_models/resnet_v1.py b/research/object_detection/models/keras_models/resnet_v1.py deleted file mode 100644 index 25007850eda..00000000000 --- a/research/object_detection/models/keras_models/resnet_v1.py +++ /dev/null @@ -1,541 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""A wrapper around the Keras Resnet V1 models for object detection.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -from keras.applications import resnet - -import tensorflow.compat.v1 as tf - -from object_detection.core import freezable_batch_norm -from object_detection.models.keras_models import model_utils - - -def _fixed_padding(inputs, kernel_size, rate=1): # pylint: disable=invalid-name - """Pads the input along the spatial dimensions independently of input size. - - Pads the input such that if it was used in a convolution with 'VALID' padding, - the output would have the same dimensions as if the unpadded input was used - in a convolution with 'SAME' padding. - - Args: - inputs: A tensor of size [batch, height_in, width_in, channels]. - kernel_size: The kernel to be used in the conv2d or max_pool2d operation. - rate: An integer, rate for atrous convolution. - - Returns: - output: A tensor of size [batch, height_out, width_out, channels] with the - input, either intact (if kernel_size == 1) or padded (if kernel_size > 1). - """ - kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) - pad_total = kernel_size_effective - 1 - pad_beg = pad_total // 2 - pad_end = pad_total - pad_beg - padded_inputs = tf.pad( - inputs, [[0, 0], [pad_beg, pad_end], [pad_beg, pad_end], [0, 0]]) - return padded_inputs - - -class _LayersOverride(object): - """Alternative Keras layers interface for the Keras Resnet V1.""" - - def __init__(self, - batchnorm_training, - batchnorm_scale=True, - default_batchnorm_momentum=0.997, - default_batchnorm_epsilon=1e-5, - weight_decay=0.0001, - conv_hyperparams=None, - min_depth=8, - depth_multiplier=1): - """Alternative tf.keras.layers interface, for use by the Keras Resnet V1. - - The class is used by the Keras applications kwargs injection API to - modify the Resnet V1 Keras application with changes required by - the Object Detection API. - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - batchnorm_scale: If True, uses an explicit `gamma` multiplier to scale - the activations in the batch normalization layer. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - default_batchnorm_epsilon: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the epsilon. - weight_decay: The weight decay to use for regularizing the model. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default resnet_v1 layer builders. - min_depth: Minimum number of filters in the convolutional layers. - depth_multiplier: The depth multiplier to modify the number of filters - in the convolutional layers. - """ - self._batchnorm_training = batchnorm_training - self._batchnorm_scale = batchnorm_scale - self._default_batchnorm_momentum = default_batchnorm_momentum - self._default_batchnorm_epsilon = default_batchnorm_epsilon - self._conv_hyperparams = conv_hyperparams - self._min_depth = min_depth - self._depth_multiplier = depth_multiplier - self.regularizer = tf.keras.regularizers.l2(weight_decay) - self.initializer = tf.variance_scaling_initializer() - - def _FixedPaddingLayer(self, kernel_size, rate=1): # pylint: disable=invalid-name - return tf.keras.layers.Lambda( - lambda x: _fixed_padding(x, kernel_size, rate)) - - def Conv2D(self, filters, kernel_size, **kwargs): # pylint: disable=invalid-name - """Builds a Conv2D layer according to the current Object Detection config. - - Overrides the Keras Resnet application's convolutions with ones that - follow the spec specified by the Object Detection hyperparameters. - - Args: - filters: The number of filters to use for the convolution. - kernel_size: The kernel size to specify the height and width of the 2D - convolution window. - **kwargs: Keyword args specified by the Keras application for - constructing the convolution. - - Returns: - A one-arg callable that will either directly apply a Keras Conv2D layer to - the input argument, or that will first pad the input then apply a Conv2D - layer. - """ - # Apply the minimum depth to the convolution layers. - filters = max(int(filters * self._depth_multiplier), self._min_depth) - - if self._conv_hyperparams: - kwargs = self._conv_hyperparams.params(**kwargs) - else: - kwargs['kernel_regularizer'] = self.regularizer - kwargs['kernel_initializer'] = self.initializer - - # Set use_bias as false to keep it consistent with Slim Resnet model. - kwargs['use_bias'] = False - - kwargs['padding'] = 'same' - stride = kwargs.get('strides') - if stride and kernel_size and stride > 1 and kernel_size > 1: - kwargs['padding'] = 'valid' - def padded_conv(features): # pylint: disable=invalid-name - padded_features = self._FixedPaddingLayer(kernel_size)(features) - return tf.keras.layers.Conv2D( - filters, kernel_size, **kwargs)(padded_features) - return padded_conv - else: - return tf.keras.layers.Conv2D(filters, kernel_size, **kwargs) - - def Activation(self, *args, **kwargs): # pylint: disable=unused-argument,invalid-name - """Builds an activation layer. - - Overrides the Keras application Activation layer specified by the - Object Detection configuration. - - Args: - *args: Ignored, - required to match the `tf.keras.layers.Activation` interface. - **kwargs: Only the name is used, - required to match `tf.keras.layers.Activation` interface. - - Returns: - An activation layer specified by the Object Detection hyperparameter - configurations. - """ - name = kwargs.get('name') - if self._conv_hyperparams: - return self._conv_hyperparams.build_activation_layer(name=name) - else: - return tf.keras.layers.Lambda(tf.nn.relu, name=name) - - def BatchNormalization(self, **kwargs): # pylint: disable=invalid-name - """Builds a normalization layer. - - Overrides the Keras application batch norm with the norm specified by the - Object Detection configuration. - - Args: - **kwargs: Only the name is used, all other params ignored. - Required for matching `layers.BatchNormalization` calls in the Keras - application. - - Returns: - A normalization layer specified by the Object Detection hyperparameter - configurations. - """ - name = kwargs.get('name') - if self._conv_hyperparams: - return self._conv_hyperparams.build_batch_norm( - training=self._batchnorm_training, - name=name) - else: - kwargs['scale'] = self._batchnorm_scale - kwargs['epsilon'] = self._default_batchnorm_epsilon - return freezable_batch_norm.FreezableBatchNorm( - training=self._batchnorm_training, - momentum=self._default_batchnorm_momentum, - **kwargs) - - def Input(self, shape): # pylint: disable=invalid-name - """Builds an Input layer. - - Overrides the Keras application Input layer with one that uses a - tf.placeholder_with_default instead of a tf.placeholder. This is necessary - to ensure the application works when run on a TPU. - - Args: - shape: A tuple of integers representing the shape of the input, which - includes both spatial share and channels, but not the batch size. - Elements of this tuple can be None; 'None' elements represent dimensions - where the shape is not known. - - Returns: - An input layer for the specified shape that internally uses a - placeholder_with_default. - """ - default_size = 224 - default_batch_size = 1 - shape = list(shape) - default_shape = [default_size if dim is None else dim for dim in shape] - - input_tensor = tf.constant(0.0, shape=[default_batch_size] + default_shape) - - placeholder_with_default = tf.placeholder_with_default( - input=input_tensor, shape=[None] + shape) - return model_utils.input_layer(shape, placeholder_with_default) - - def MaxPooling2D(self, pool_size, **kwargs): # pylint: disable=invalid-name - """Builds a MaxPooling2D layer with default padding as 'SAME'. - - This is specified by the default resnet arg_scope in slim. - - Args: - pool_size: The pool size specified by the Keras application. - **kwargs: Ignored, required to match the Keras applications usage. - - Returns: - A MaxPooling2D layer with default padding as 'SAME'. - """ - kwargs['padding'] = 'same' - return tf.keras.layers.MaxPooling2D(pool_size, **kwargs) - - # Add alias as Keras also has it. - MaxPool2D = MaxPooling2D # pylint: disable=invalid-name - - def ZeroPadding2D(self, padding, **kwargs): # pylint: disable=unused-argument,invalid-name - """Replaces explicit padding in the Keras application with a no-op. - - Args: - padding: The padding values for image height and width. - **kwargs: Ignored, required to match the Keras applications usage. - - Returns: - A no-op identity lambda. - """ - return lambda x: x - - # Forward all non-overridden methods to the keras layers - def __getattr__(self, item): - return getattr(tf.keras.layers, item) - - -# pylint: disable=invalid-name -def resnet_v1_50(batchnorm_training, - batchnorm_scale=True, - default_batchnorm_momentum=0.997, - default_batchnorm_epsilon=1e-5, - weight_decay=0.0001, - conv_hyperparams=None, - min_depth=8, - depth_multiplier=1, - **kwargs): - """Instantiates the Resnet50 architecture, modified for object detection. - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - batchnorm_scale: If True, uses an explicit `gamma` multiplier to scale - the activations in the batch normalization layer. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - default_batchnorm_epsilon: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the epsilon. - weight_decay: The weight decay to use for regularizing the model. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default resnet_v1 layer builders. - min_depth: Minimum number of filters in the convolutional layers. - depth_multiplier: The depth multiplier to modify the number of filters - in the convolutional layers. - **kwargs: Keyword arguments forwarded directly to the - `tf.keras.applications.Mobilenet` method that constructs the Keras - model. - - Returns: - A Keras ResnetV1-50 model instance. - """ - layers_override = _LayersOverride( - batchnorm_training, - batchnorm_scale=batchnorm_scale, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon, - conv_hyperparams=conv_hyperparams, - weight_decay=weight_decay, - min_depth=min_depth, - depth_multiplier=depth_multiplier) - return tf.keras.applications.resnet.ResNet50( - layers=layers_override, **kwargs) - - -def resnet_v1_101(batchnorm_training, - batchnorm_scale=True, - default_batchnorm_momentum=0.997, - default_batchnorm_epsilon=1e-5, - weight_decay=0.0001, - conv_hyperparams=None, - min_depth=8, - depth_multiplier=1, - **kwargs): - """Instantiates the Resnet50 architecture, modified for object detection. - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - batchnorm_scale: If True, uses an explicit `gamma` multiplier to scale - the activations in the batch normalization layer. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - default_batchnorm_epsilon: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the epsilon. - weight_decay: The weight decay to use for regularizing the model. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default resnet_v1 layer builders. - min_depth: Minimum number of filters in the convolutional layers. - depth_multiplier: The depth multiplier to modify the number of filters - in the convolutional layers. - **kwargs: Keyword arguments forwarded directly to the - `tf.keras.applications.Mobilenet` method that constructs the Keras - model. - - Returns: - A Keras ResnetV1-101 model instance. - """ - layers_override = _LayersOverride( - batchnorm_training, - batchnorm_scale=batchnorm_scale, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon, - conv_hyperparams=conv_hyperparams, - weight_decay=weight_decay, - min_depth=min_depth, - depth_multiplier=depth_multiplier) - return tf.keras.applications.resnet.ResNet101( - layers=layers_override, **kwargs) - - -def resnet_v1_152(batchnorm_training, - batchnorm_scale=True, - default_batchnorm_momentum=0.997, - default_batchnorm_epsilon=1e-5, - weight_decay=0.0001, - conv_hyperparams=None, - min_depth=8, - depth_multiplier=1, - **kwargs): - """Instantiates the Resnet50 architecture, modified for object detection. - - Args: - batchnorm_training: Bool. Assigned to Batch norm layer `training` param - when constructing `freezable_batch_norm.FreezableBatchNorm` layers. - batchnorm_scale: If True, uses an explicit `gamma` multiplier to scale - the activations in the batch normalization layer. - default_batchnorm_momentum: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the momentum. - default_batchnorm_epsilon: Float. When 'conv_hyperparams' is None, - batch norm layers will be constructed using this value as the epsilon. - weight_decay: The weight decay to use for regularizing the model. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. Optionally set to `None` - to use default resnet_v1 layer builders. - min_depth: Minimum number of filters in the convolutional layers. - depth_multiplier: The depth multiplier to modify the number of filters - in the convolutional layers. - **kwargs: Keyword arguments forwarded directly to the - `tf.keras.applications.Mobilenet` method that constructs the Keras - model. - - Returns: - A Keras ResnetV1-152 model instance. - """ - layers_override = _LayersOverride( - batchnorm_training, - batchnorm_scale=batchnorm_scale, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon, - conv_hyperparams=conv_hyperparams, - weight_decay=weight_decay, - min_depth=min_depth, - depth_multiplier=depth_multiplier) - return tf.keras.applications.resnet.ResNet152( - layers=layers_override, **kwargs) -# pylint: enable=invalid-name - - -# The following codes are based on the existing keras ResNet model pattern: -# google3/third_party/py/keras/applications/resnet.py -def block_basic(x, - filters, - kernel_size=3, - stride=1, - conv_shortcut=False, - name=None): - """A residual block for ResNet18/34. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer. - kernel_size: default 3, kernel size of the bottleneck layer. - stride: default 1, stride of the first layer. - conv_shortcut: default False, use convolution shortcut if True, otherwise - identity shortcut. - name: string, block label. - - Returns: - Output tensor for the residual block. - """ - layers = tf.keras.layers - bn_axis = 3 if tf.keras.backend.image_data_format() == 'channels_last' else 1 - - preact = layers.BatchNormalization( - axis=bn_axis, epsilon=1.001e-5, name=name + '_preact_bn')( - x) - preact = layers.Activation('relu', name=name + '_preact_relu')(preact) - - if conv_shortcut: - shortcut = layers.Conv2D( - filters, 1, strides=1, name=name + '_0_conv')( - preact) - else: - shortcut = layers.MaxPooling2D(1, strides=stride)(x) if stride > 1 else x - - x = layers.ZeroPadding2D( - padding=((1, 1), (1, 1)), name=name + '_1_pad')( - preact) - x = layers.Conv2D( - filters, kernel_size, strides=1, use_bias=False, name=name + '_1_conv')( - x) - x = layers.BatchNormalization( - axis=bn_axis, epsilon=1.001e-5, name=name + '_1_bn')( - x) - x = layers.Activation('relu', name=name + '_1_relu')(x) - - x = layers.ZeroPadding2D(padding=((1, 1), (1, 1)), name=name + '_2_pad')(x) - x = layers.Conv2D( - filters, - kernel_size, - strides=stride, - use_bias=False, - name=name + '_2_conv')( - x) - x = layers.BatchNormalization( - axis=bn_axis, epsilon=1.001e-5, name=name + '_2_bn')( - x) - x = layers.Activation('relu', name=name + '_2_relu')(x) - x = layers.Add(name=name + '_out')([shortcut, x]) - return x - - -def stack_basic(x, filters, blocks, stride1=2, name=None): - """A set of stacked residual blocks for ResNet18/34. - - Args: - x: input tensor. - filters: integer, filters of the bottleneck layer in a block. - blocks: integer, blocks in the stacked blocks. - stride1: default 2, stride of the first layer in the first block. - name: string, stack label. - - Returns: - Output tensor for the stacked blocks. - """ - x = block_basic(x, filters, conv_shortcut=True, name=name + '_block1') - for i in range(2, blocks): - x = block_basic(x, filters, name=name + '_block' + str(i)) - x = block_basic( - x, filters, stride=stride1, name=name + '_block' + str(blocks)) - return x - - -def resnet_v1_18(include_top=True, - weights='imagenet', - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation='softmax'): - """Instantiates the ResNet18 architecture.""" - - def stack_fn(x): - x = stack_basic(x, 64, 2, stride1=1, name='conv2') - x = stack_basic(x, 128, 2, name='conv3') - x = stack_basic(x, 256, 2, name='conv4') - return stack_basic(x, 512, 2, name='conv5') - - return resnet.ResNet( - stack_fn, - True, - True, - 'resnet18', - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation=classifier_activation) - - -def resnet_v1_34(include_top=True, - weights='imagenet', - input_tensor=None, - input_shape=None, - pooling=None, - classes=1000, - classifier_activation='softmax'): - """Instantiates the ResNet34 architecture.""" - - def stack_fn(x): - x = stack_basic(x, 64, 3, stride1=1, name='conv2') - x = stack_basic(x, 128, 4, name='conv3') - x = stack_basic(x, 256, 6, name='conv4') - return stack_basic(x, 512, 3, name='conv5') - - return resnet.ResNet( - stack_fn, - True, - True, - 'resnet34', - include_top, - weights, - input_tensor, - input_shape, - pooling, - classes, - classifier_activation=classifier_activation) diff --git a/research/object_detection/models/keras_models/resnet_v1_tf2_test.py b/research/object_detection/models/keras_models/resnet_v1_tf2_test.py deleted file mode 100644 index 4566bc8ddda..00000000000 --- a/research/object_detection/models/keras_models/resnet_v1_tf2_test.py +++ /dev/null @@ -1,226 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for resnet_v1.py. - -This test mainly focuses on comparing slim resnet v1 and Keras resnet v1 for -object detection. To verify the consistency of the two models, we compare: - 1. Output shape of each layer given different inputs. - 2. Number of global variables. -""" -import unittest - -from absl.testing import parameterized -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.models.keras_models import resnet_v1 -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - -_EXPECTED_SHAPES_224_RESNET50 = { - 'conv2_block3_out': (4, 56, 56, 256), - 'conv3_block4_out': (4, 28, 28, 512), - 'conv4_block6_out': (4, 14, 14, 1024), - 'conv5_block3_out': (4, 7, 7, 2048), -} - -_EXPECTED_SHAPES_224_RESNET101 = { - 'conv2_block3_out': (4, 56, 56, 256), - 'conv3_block4_out': (4, 28, 28, 512), - 'conv4_block23_out': (4, 14, 14, 1024), - 'conv5_block3_out': (4, 7, 7, 2048), -} - -_EXPECTED_SHAPES_224_RESNET152 = { - 'conv2_block3_out': (4, 56, 56, 256), - 'conv3_block8_out': (4, 28, 28, 512), - 'conv4_block36_out': (4, 14, 14, 1024), - 'conv5_block3_out': (4, 7, 7, 2048), -} - -_RESNET_NAMES = ['resnet_v1_50', 'resnet_v1_101', 'resnet_v1_152'] -_RESNET_MODELS = [ - resnet_v1.resnet_v1_50, resnet_v1.resnet_v1_101, resnet_v1.resnet_v1_152 -] -_RESNET_SHAPES = [ - _EXPECTED_SHAPES_224_RESNET50, _EXPECTED_SHAPES_224_RESNET101, - _EXPECTED_SHAPES_224_RESNET152 -] - -_NUM_CHANNELS = 3 -_BATCH_SIZE = 4 - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ResnetV1Test(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6, - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - batch_norm { - scale: true, - decay: 0.997, - epsilon: 0.001, - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def _create_application_with_layer_outputs(self, - model_index, - batchnorm_training, - batchnorm_scale=True, - weight_decay=0.0001, - default_batchnorm_momentum=0.997, - default_batchnorm_epsilon=1e-5): - """Constructs Keras resnet_v1 that extracts layer outputs.""" - # Have to clear the Keras backend to ensure isolation in layer naming - tf.keras.backend.clear_session() - layer_names = _RESNET_SHAPES[model_index].keys() - full_model = _RESNET_MODELS[model_index]( - batchnorm_training=batchnorm_training, - weights=None, - batchnorm_scale=batchnorm_scale, - weight_decay=weight_decay, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon, - include_top=False) - - layer_outputs = [ - full_model.get_layer(name=layer).output for layer in layer_names - ] - return tf.keras.Model(inputs=full_model.inputs, outputs=layer_outputs) - - def _check_returns_correct_shape(self, - image_height, - image_width, - model_index, - expected_feature_map_shape, - batchnorm_training=True, - batchnorm_scale=True, - weight_decay=0.0001, - default_batchnorm_momentum=0.997, - default_batchnorm_epsilon=1e-5): - model = self._create_application_with_layer_outputs( - model_index=model_index, - batchnorm_training=batchnorm_training, - batchnorm_scale=batchnorm_scale, - weight_decay=weight_decay, - default_batchnorm_momentum=default_batchnorm_momentum, - default_batchnorm_epsilon=default_batchnorm_epsilon) - - image_tensor = np.random.rand(_BATCH_SIZE, image_height, image_width, - _NUM_CHANNELS).astype(np.float32) - feature_maps = model(image_tensor) - layer_names = _RESNET_SHAPES[model_index].keys() - for feature_map, layer_name in zip(feature_maps, layer_names): - expected_shape = _RESNET_SHAPES[model_index][layer_name] - self.assertAllEqual(feature_map.shape, expected_shape) - - def _get_variables(self, model_index): - tf.keras.backend.clear_session() - model = self._create_application_with_layer_outputs( - model_index, batchnorm_training=False) - preprocessed_inputs = tf.random.uniform([2, 40, 40, _NUM_CHANNELS]) - model(preprocessed_inputs) - return model.variables - - def test_returns_correct_shapes_224(self): - image_height = 224 - image_width = 224 - for model_index, _ in enumerate(_RESNET_NAMES): - expected_feature_map_shape = _RESNET_SHAPES[model_index] - self._check_returns_correct_shape(image_height, image_width, model_index, - expected_feature_map_shape) - - def test_hyperparam_override(self): - for model_name in _RESNET_MODELS: - model = model_name( - batchnorm_training=True, - default_batchnorm_momentum=0.2, - default_batchnorm_epsilon=0.1, - weights=None, - include_top=False) - bn_layer = model.get_layer(name='conv1_bn') - self.assertAllClose(bn_layer.momentum, 0.2) - self.assertAllClose(bn_layer.epsilon, 0.1) - - def test_variable_count(self): - # The number of variables from slim resnetv1-* model. - variable_nums = [265, 520, 775] - for model_index, var_num in enumerate(variable_nums): - variables = self._get_variables(model_index) - self.assertEqual(len(variables), var_num) - - -class ResnetShapeTest(test_case.TestCase, parameterized.TestCase): - - @unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') - @parameterized.parameters( - { - 'resnet_type': - 'resnet_v1_34', - 'output_layer_names': [ - 'conv2_block3_out', 'conv3_block4_out', 'conv4_block6_out', - 'conv5_block3_out' - ] - }, { - 'resnet_type': - 'resnet_v1_18', - 'output_layer_names': [ - 'conv2_block2_out', 'conv3_block2_out', 'conv4_block2_out', - 'conv5_block2_out' - ] - }) - def test_output_shapes(self, resnet_type, output_layer_names): - if resnet_type == 'resnet_v1_34': - model = resnet_v1.resnet_v1_34(input_shape=(64, 64, 3), weights=None) - else: - model = resnet_v1.resnet_v1_18(input_shape=(64, 64, 3), weights=None) - outputs = [ - model.get_layer(output_layer_name).output - for output_layer_name in output_layer_names - ] - resnet_model = tf.keras.models.Model(inputs=model.input, outputs=outputs) - outputs = resnet_model(np.zeros((2, 64, 64, 3), dtype=np.float32)) - - # Check the shape of 'conv2_block3_out': - self.assertEqual(outputs[0].shape, [2, 16, 16, 64]) - # Check the shape of 'conv3_block4_out': - self.assertEqual(outputs[1].shape, [2, 8, 8, 128]) - # Check the shape of 'conv4_block6_out': - self.assertEqual(outputs[2].shape, [2, 4, 4, 256]) - # Check the shape of 'conv5_block3_out': - self.assertEqual(outputs[3].shape, [2, 2, 2, 512]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/keras_models/test_utils.py b/research/object_detection/models/keras_models/test_utils.py deleted file mode 100644 index 0669b6c697f..00000000000 --- a/research/object_detection/models/keras_models/test_utils.py +++ /dev/null @@ -1,214 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Test utils for other test files.""" - -# import tensorflow as tf -# -# from nets import mobilenet_v1 -# -# slim = tf.contrib.slim -# -# # Layer names of Slim to map Keras layer names in MobilenetV1 -# _MOBLIENET_V1_SLIM_ENDPOINTS = [ -# 'Conv2d_0', -# 'Conv2d_1_depthwise', 'Conv2d_1_pointwise', -# 'Conv2d_2_depthwise', 'Conv2d_2_pointwise', -# 'Conv2d_3_depthwise', 'Conv2d_3_pointwise', -# 'Conv2d_4_depthwise', 'Conv2d_4_pointwise', -# 'Conv2d_5_depthwise', 'Conv2d_5_pointwise', -# 'Conv2d_6_depthwise', 'Conv2d_6_pointwise', -# 'Conv2d_7_depthwise', 'Conv2d_7_pointwise', -# 'Conv2d_8_depthwise', 'Conv2d_8_pointwise', -# 'Conv2d_9_depthwise', 'Conv2d_9_pointwise', -# 'Conv2d_10_depthwise', 'Conv2d_10_pointwise', -# 'Conv2d_11_depthwise', 'Conv2d_11_pointwise', -# 'Conv2d_12_depthwise', 'Conv2d_12_pointwise', -# 'Conv2d_13_depthwise', 'Conv2d_13_pointwise' -# ] -# -# -# # Function to get the output shape of each layer in Slim. It's used to -# # generate the following constant expected_feature_map_shape for MobilenetV1. -# # Similarly, this can also apply to MobilenetV2. -# def _get_slim_endpoint_shapes(inputs, depth_multiplier=1.0, min_depth=8, -# use_explicit_padding=False): -# with slim.arg_scope([slim.conv2d, slim.separable_conv2d], -# normalizer_fn=slim.batch_norm): -# _, end_points = mobilenet_v1.mobilenet_v1_base( -# inputs, final_endpoint='Conv2d_13_pointwise', -# depth_multiplier=depth_multiplier, min_depth=min_depth, -# use_explicit_padding=use_explicit_padding) -# return [end_points[endpoint_name].get_shape() -# for endpoint_name in _MOBLIENET_V1_SLIM_ENDPOINTS] - - -# For Mobilenet V1 -moblenet_v1_expected_feature_map_shape_128 = [ - (2, 64, 64, 32), (2, 64, 64, 32), (2, 64, 64, 64), (2, 32, 32, 64), - (2, 32, 32, 128), (2, 32, 32, 128), (2, 32, 32, 128), (2, 16, 16, 128), - (2, 16, 16, 256), (2, 16, 16, 256), (2, 16, 16, 256), (2, 8, 8, 256), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 4, 4, 512), - (2, 4, 4, 1024), (2, 4, 4, 1024), (2, 4, 4, 1024), -] - -moblenet_v1_expected_feature_map_shape_128_explicit_padding = [ - (2, 64, 64, 32), (2, 64, 64, 32), (2, 64, 64, 64), (2, 32, 32, 64), - (2, 32, 32, 128), (2, 32, 32, 128), (2, 32, 32, 128), (2, 16, 16, 128), - (2, 16, 16, 256), (2, 16, 16, 256), (2, 16, 16, 256), (2, 8, 8, 256), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 4, 4, 512), - (2, 4, 4, 1024), (2, 4, 4, 1024), (2, 4, 4, 1024), -] - -mobilenet_v1_expected_feature_map_shape_with_dynamic_inputs = [ - (2, 64, 64, 32), (2, 64, 64, 32), (2, 64, 64, 64), (2, 32, 32, 64), - (2, 32, 32, 128), (2, 32, 32, 128), (2, 32, 32, 128), (2, 16, 16, 128), - (2, 16, 16, 256), (2, 16, 16, 256), (2, 16, 16, 256), (2, 8, 8, 256), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), - (2, 8, 8, 512), (2, 8, 8, 512), (2, 8, 8, 512), (2, 4, 4, 512), - (2, 4, 4, 1024), (2, 4, 4, 1024), (2, 4, 4, 1024), -] - -moblenet_v1_expected_feature_map_shape_299 = [ - (2, 150, 150, 32), (2, 150, 150, 32), (2, 150, 150, 64), (2, 75, 75, 64), - (2, 75, 75, 128), (2, 75, 75, 128), (2, 75, 75, 128), (2, 38, 38, 128), - (2, 38, 38, 256), (2, 38, 38, 256), (2, 38, 38, 256), (2, 19, 19, 256), - (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), - (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), - (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), (2, 10, 10, 512), - (2, 10, 10, 1024), (2, 10, 10, 1024), (2, 10, 10, 1024), -] - -moblenet_v1_expected_feature_map_shape_enforcing_min_depth = [ - (2, 150, 150, 8), (2, 150, 150, 8), (2, 150, 150, 8), (2, 75, 75, 8), - (2, 75, 75, 8), (2, 75, 75, 8), (2, 75, 75, 8), (2, 38, 38, 8), - (2, 38, 38, 8), (2, 38, 38, 8), (2, 38, 38, 8), (2, 19, 19, 8), - (2, 19, 19, 8), (2, 19, 19, 8), (2, 19, 19, 8), (2, 19, 19, 8), - (2, 19, 19, 8), (2, 19, 19, 8), (2, 19, 19, 8), (2, 19, 19, 8), - (2, 19, 19, 8), (2, 19, 19, 8), (2, 19, 19, 8), (2, 10, 10, 8), - (2, 10, 10, 8), (2, 10, 10, 8), (2, 10, 10, 8), -] - -moblenet_v1_expected_feature_map_shape_with_conv_defs = [ - (2, 150, 150, 32), (2, 150, 150, 32), (2, 150, 150, 64), (2, 75, 75, 64), - (2, 75, 75, 128), (2, 75, 75, 128), (2, 75, 75, 128), (2, 38, 38, 128), - (2, 38, 38, 256), (2, 38, 38, 256), (2, 38, 38, 256), (2, 19, 19, 256), - (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), - (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), - (2, 19, 19, 512), (2, 19, 19, 512), (2, 19, 19, 512), (2, 10, 10, 512), - (2, 10, 10, 512), (2, 10, 10, 512), (2, 10, 10, 256), -] - -# For Mobilenet V2 -moblenet_v2_expected_feature_map_shape_128 = [ - (2, 64, 64, 32), (2, 64, 64, 96), (2, 32, 32, 96), (2, 32, 32, 24), - (2, 32, 32, 144), (2, 32, 32, 144), (2, 32, 32, 24), (2, 32, 32, 144), - (2, 16, 16, 144), (2, 16, 16, 32), (2, 16, 16, 192), (2, 16, 16, 192), - (2, 16, 16, 32), (2, 16, 16, 192), (2, 16, 16, 192), (2, 16, 16, 32), - (2, 16, 16, 192), (2, 8, 8, 192), (2, 8, 8, 64), (2, 8, 8, 384), - (2, 8, 8, 384), (2, 8, 8, 64), (2, 8, 8, 384), (2, 8, 8, 384), - (2, 8, 8, 64), (2, 8, 8, 384), (2, 8, 8, 384), (2, 8, 8, 64), - (2, 8, 8, 384), (2, 8, 8, 384), (2, 8, 8, 96), (2, 8, 8, 576), - (2, 8, 8, 576), (2, 8, 8, 96), (2, 8, 8, 576), (2, 8, 8, 576), - (2, 8, 8, 96), (2, 8, 8, 576), (2, 4, 4, 576), (2, 4, 4, 160), - (2, 4, 4, 960), (2, 4, 4, 960), (2, 4, 4, 160), (2, 4, 4, 960), - (2, 4, 4, 960), (2, 4, 4, 160), (2, 4, 4, 960), (2, 4, 4, 960), - (2, 4, 4, 320), (2, 4, 4, 1280) -] - -moblenet_v2_expected_feature_map_shape_128_explicit_padding = [ - (2, 64, 64, 32), (2, 64, 64, 96), (2, 32, 32, 96), (2, 32, 32, 24), - (2, 32, 32, 144), (2, 32, 32, 144), (2, 32, 32, 24), (2, 32, 32, 144), - (2, 16, 16, 144), (2, 16, 16, 32), (2, 16, 16, 192), (2, 16, 16, 192), - (2, 16, 16, 32), (2, 16, 16, 192), (2, 16, 16, 192), (2, 16, 16, 32), - (2, 16, 16, 192), (2, 8, 8, 192), (2, 8, 8, 64), (2, 8, 8, 384), - (2, 8, 8, 384), (2, 8, 8, 64), (2, 8, 8, 384), (2, 8, 8, 384), - (2, 8, 8, 64), (2, 8, 8, 384), (2, 8, 8, 384), (2, 8, 8, 64), - (2, 8, 8, 384), (2, 8, 8, 384), (2, 8, 8, 96), (2, 8, 8, 576), - (2, 8, 8, 576), (2, 8, 8, 96), (2, 8, 8, 576), (2, 8, 8, 576), - (2, 8, 8, 96), (2, 8, 8, 576), (2, 4, 4, 576), (2, 4, 4, 160), - (2, 4, 4, 960), (2, 4, 4, 960), (2, 4, 4, 160), (2, 4, 4, 960), - (2, 4, 4, 960), (2, 4, 4, 160), (2, 4, 4, 960), (2, 4, 4, 960), - (2, 4, 4, 320), (2, 4, 4, 1280) -] - -mobilenet_v2_expected_feature_map_shape_with_dynamic_inputs = [ - (2, 64, 64, 32), (2, 64, 64, 96), (2, 32, 32, 96), (2, 32, 32, 24), - (2, 32, 32, 144), (2, 32, 32, 144), (2, 32, 32, 24), (2, 32, 32, 144), - (2, 16, 16, 144), (2, 16, 16, 32), (2, 16, 16, 192), (2, 16, 16, 192), - (2, 16, 16, 32), (2, 16, 16, 192), (2, 16, 16, 192), (2, 16, 16, 32), - (2, 16, 16, 192), (2, 8, 8, 192), (2, 8, 8, 64), (2, 8, 8, 384), - (2, 8, 8, 384), (2, 8, 8, 64), (2, 8, 8, 384), (2, 8, 8, 384), - (2, 8, 8, 64), (2, 8, 8, 384), (2, 8, 8, 384), (2, 8, 8, 64), - (2, 8, 8, 384), (2, 8, 8, 384), (2, 8, 8, 96), (2, 8, 8, 576), - (2, 8, 8, 576), (2, 8, 8, 96), (2, 8, 8, 576), (2, 8, 8, 576), - (2, 8, 8, 96), (2, 8, 8, 576), (2, 4, 4, 576), (2, 4, 4, 160), - (2, 4, 4, 960), (2, 4, 4, 960), (2, 4, 4, 160), (2, 4, 4, 960), - (2, 4, 4, 960), (2, 4, 4, 160), (2, 4, 4, 960), (2, 4, 4, 960), - (2, 4, 4, 320), (2, 4, 4, 1280) -] - -moblenet_v2_expected_feature_map_shape_299 = [ - (2, 150, 150, 32), (2, 150, 150, 96), (2, 75, 75, 96), (2, 75, 75, 24), - (2, 75, 75, 144), (2, 75, 75, 144), (2, 75, 75, 24), (2, 75, 75, 144), - (2, 38, 38, 144), (2, 38, 38, 32), (2, 38, 38, 192), (2, 38, 38, 192), - (2, 38, 38, 32), (2, 38, 38, 192), (2, 38, 38, 192), (2, 38, 38, 32), - (2, 38, 38, 192), (2, 19, 19, 192), (2, 19, 19, 64), (2, 19, 19, 384), - (2, 19, 19, 384), (2, 19, 19, 64), (2, 19, 19, 384), (2, 19, 19, 384), - (2, 19, 19, 64), (2, 19, 19, 384), (2, 19, 19, 384), (2, 19, 19, 64), - (2, 19, 19, 384), (2, 19, 19, 384), (2, 19, 19, 96), (2, 19, 19, 576), - (2, 19, 19, 576), (2, 19, 19, 96), (2, 19, 19, 576), (2, 19, 19, 576), - (2, 19, 19, 96), (2, 19, 19, 576), (2, 10, 10, 576), (2, 10, 10, 160), - (2, 10, 10, 960), (2, 10, 10, 960), (2, 10, 10, 160), (2, 10, 10, 960), - (2, 10, 10, 960), (2, 10, 10, 160), (2, 10, 10, 960), (2, 10, 10, 960), - (2, 10, 10, 320), (2, 10, 10, 1280) -] - -moblenet_v2_expected_feature_map_shape_enforcing_min_depth = [ - (2, 150, 150, 32), (2, 150, 150, 192), (2, 75, 75, 192), (2, 75, 75, 32), - (2, 75, 75, 192), (2, 75, 75, 192), (2, 75, 75, 32), (2, 75, 75, 192), - (2, 38, 38, 192), (2, 38, 38, 32), (2, 38, 38, 192), (2, 38, 38, 192), - (2, 38, 38, 32), (2, 38, 38, 192), (2, 38, 38, 192), (2, 38, 38, 32), - (2, 38, 38, 192), (2, 19, 19, 192), (2, 19, 19, 32), (2, 19, 19, 192), - (2, 19, 19, 192), (2, 19, 19, 32), (2, 19, 19, 192), (2, 19, 19, 192), - (2, 19, 19, 32), (2, 19, 19, 192), (2, 19, 19, 192), (2, 19, 19, 32), - (2, 19, 19, 192), (2, 19, 19, 192), (2, 19, 19, 32), (2, 19, 19, 192), - (2, 19, 19, 192), (2, 19, 19, 32), (2, 19, 19, 192), (2, 19, 19, 192), - (2, 19, 19, 32), (2, 19, 19, 192), (2, 10, 10, 192), (2, 10, 10, 32), - (2, 10, 10, 192), (2, 10, 10, 192), (2, 10, 10, 32), (2, 10, 10, 192), - (2, 10, 10, 192), (2, 10, 10, 32), (2, 10, 10, 192), (2, 10, 10, 192), - (2, 10, 10, 32), (2, 10, 10, 32) -] - -moblenet_v2_expected_feature_map_shape_with_conv_defs = [ - (2, 150, 150, 32), (2, 150, 150, 96), (2, 75, 75, 96), (2, 75, 75, 24), - (2, 75, 75, 144), (2, 75, 75, 144), (2, 75, 75, 24), (2, 75, 75, 144), - (2, 38, 38, 144), (2, 38, 38, 32), (2, 38, 38, 192), (2, 38, 38, 192), - (2, 38, 38, 32), (2, 38, 38, 192), (2, 38, 38, 192), (2, 38, 38, 32), - (2, 38, 38, 192), (2, 19, 19, 192), (2, 19, 19, 64), (2, 19, 19, 384), - (2, 19, 19, 384), (2, 19, 19, 64), (2, 19, 19, 384), (2, 19, 19, 384), - (2, 19, 19, 64), (2, 19, 19, 384), (2, 19, 19, 384), (2, 19, 19, 64), - (2, 19, 19, 384), (2, 19, 19, 384), (2, 19, 19, 96), (2, 19, 19, 576), - (2, 19, 19, 576), (2, 19, 19, 96), (2, 19, 19, 576), (2, 19, 19, 576), - (2, 19, 19, 96), (2, 19, 19, 576), (2, 10, 10, 576), (2, 10, 10, 160), - (2, 10, 10, 960), (2, 10, 10, 960), (2, 10, 10, 160), (2, 10, 10, 960), - (2, 10, 10, 960), (2, 10, 10, 160), (2, 10, 10, 960), (2, 10, 10, 960), - (2, 10, 10, 320), (2, 10, 10, 256) -] diff --git a/research/object_detection/models/ssd_efficientnet_bifpn_feature_extractor.py b/research/object_detection/models/ssd_efficientnet_bifpn_feature_extractor.py deleted file mode 100644 index 70b2ab2e64d..00000000000 --- a/research/object_detection/models/ssd_efficientnet_bifpn_feature_extractor.py +++ /dev/null @@ -1,971 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSD Keras-based EfficientNet + BiFPN (EfficientDet) Feature Extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from absl import logging -from keras import backend as keras_backend -from six.moves import range -from six.moves import zip -import tensorflow.compat.v2 as tf - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import bidirectional_feature_pyramid_generators as bifpn_generators -from object_detection.utils import ops -from object_detection.utils import shape_utils -from object_detection.utils import tf_version - -# pylint: disable=g-import-not-at-top -if tf_version.is_tf2(): - try: - from official.legacy.image_classification.efficientnet import efficientnet_model - except ModuleNotFoundError: - from official.vision.image_classification.efficientnet import efficientnet_model - -_EFFICIENTNET_LEVEL_ENDPOINTS = { - 1: 'stack_0/block_0/project_bn', - 2: 'stack_1/block_1/add', - 3: 'stack_2/block_1/add', - 4: 'stack_4/block_2/add', - 5: 'stack_6/block_0/project_bn', -} - - -class SSDEfficientNetBiFPNKerasFeatureExtractor( - ssd_meta_arch.SSDKerasFeatureExtractor): - """SSD Keras-based EfficientNetBiFPN (EfficientDet) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level, - bifpn_max_level, - bifpn_num_iterations, - bifpn_num_filters, - bifpn_combine_method, - efficientnet_version, - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name=None): - """SSD Keras-based EfficientNetBiFPN (EfficientDet) feature extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - efficientnet_version: the EfficientNet version to use for this feature - extractor's backbone. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for bifpn unsampling. - override_base_feature_extractor_hyperparams: Whether to override the - efficientnet backbone's default weight decay with the weight decay - defined by `conv_hyperparams`. Note, only overriding of weight decay is - currently supported. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetBiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - use_explicit_padding=None, - use_depthwise=None, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - if depth_multiplier != 1.0: - raise ValueError('EfficientNetBiFPN does not support a non-default ' - 'depth_multiplier.') - if use_explicit_padding: - raise ValueError('EfficientNetBiFPN does not support explicit padding.') - if use_depthwise: - raise ValueError('EfficientNetBiFPN does not support use_depthwise.') - - self._bifpn_min_level = bifpn_min_level - self._bifpn_max_level = bifpn_max_level - self._bifpn_num_iterations = bifpn_num_iterations - self._bifpn_num_filters = max(bifpn_num_filters, min_depth) - self._bifpn_node_params = {'combine_method': bifpn_combine_method} - self._efficientnet_version = efficientnet_version - self._use_native_resize_op = use_native_resize_op - - logging.info('EfficientDet EfficientNet backbone version: %s', - self._efficientnet_version) - logging.info('EfficientDet BiFPN num filters: %d', self._bifpn_num_filters) - logging.info('EfficientDet BiFPN num iterations: %d', - self._bifpn_num_iterations) - - self._backbone_max_level = min( - max(_EFFICIENTNET_LEVEL_ENDPOINTS.keys()), bifpn_max_level) - self._output_layer_names = [ - _EFFICIENTNET_LEVEL_ENDPOINTS[i] - for i in range(bifpn_min_level, self._backbone_max_level + 1)] - self._output_layer_alias = [ - 'level_{}'.format(i) - for i in range(bifpn_min_level, self._backbone_max_level + 1)] - - # Initialize the EfficientNet backbone. - # Note, this is currently done in the init method rather than in the build - # method, since doing so introduces an error which is not well understood. - efficientnet_overrides = {'rescale_input': False} - if override_base_feature_extractor_hyperparams: - efficientnet_overrides[ - 'weight_decay'] = conv_hyperparams.get_regularizer_weight() - if (conv_hyperparams.use_sync_batch_norm() and - keras_backend.is_tpu_strategy(tf.distribute.get_strategy())): - efficientnet_overrides['batch_norm'] = 'tpu' - efficientnet_base = efficientnet_model.EfficientNet.from_name( - model_name=self._efficientnet_version, overrides=efficientnet_overrides) - outputs = [efficientnet_base.get_layer(output_layer_name).output - for output_layer_name in self._output_layer_names] - self._efficientnet = tf.keras.Model( - inputs=efficientnet_base.inputs, outputs=outputs) - self.classification_backbone = efficientnet_base - self._bifpn_stage = None - - def build(self, input_shape): - self._bifpn_stage = bifpn_generators.KerasBiFpnFeatureMaps( - bifpn_num_iterations=self._bifpn_num_iterations, - bifpn_num_filters=self._bifpn_num_filters, - fpn_min_level=self._bifpn_min_level, - fpn_max_level=self._bifpn_max_level, - input_max_level=self._backbone_max_level, - is_training=self._is_training, - conv_hyperparams=self._conv_hyperparams, - freeze_batchnorm=self._freeze_batchnorm, - bifpn_node_params=self._bifpn_node_params, - use_native_resize_op=self._use_native_resize_op, - name='bifpn') - self.built = True - - def preprocess(self, inputs): - """SSD preprocessing. - - Channel-wise mean subtraction and scaling. - - Args: - inputs: a [batch, height, width, channels] float tensor representing a - batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - if inputs.shape.as_list()[3] == 3: - # Input images are expected to be in the range [0, 255]. - channel_offset = [0.485, 0.456, 0.406] - channel_scale = [0.229, 0.224, 0.225] - return ((inputs / 255.0) - [[channel_offset]]) / [[channel_scale]] - else: - return inputs - - def _extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 129, preprocessed_inputs) - - base_feature_maps = self._efficientnet( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple)) - - output_feature_map_dict = self._bifpn_stage( - list(zip(self._output_layer_alias, base_feature_maps))) - - return list(output_feature_map_dict.values()) - - -class SSDEfficientNetB0BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b0 BiFPN (EfficientDet-d0) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=3, - bifpn_num_filters=64, - bifpn_combine_method='fast_attention', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientDet-D0'): - """SSD Keras EfficientNet-b0 BiFPN (EfficientDet-d0) Feature Extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB0BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b0', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDEfficientNetB1BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b1 BiFPN (EfficientDet-d1) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=4, - bifpn_num_filters=88, - bifpn_combine_method='fast_attention', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientDet-D1'): - """SSD Keras EfficientNet-b1 BiFPN (EfficientDet-d1) Feature Extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB1BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b1', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDEfficientNetB2BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b2 BiFPN (EfficientDet-d2) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=5, - bifpn_num_filters=112, - bifpn_combine_method='fast_attention', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientDet-D2'): - - """SSD Keras EfficientNet-b2 BiFPN (EfficientDet-d2) Feature Extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB2BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b2', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDEfficientNetB3BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b3 BiFPN (EfficientDet-d3) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=6, - bifpn_num_filters=160, - bifpn_combine_method='fast_attention', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientDet-D3'): - - """SSD Keras EfficientNet-b3 BiFPN (EfficientDet-d3) Feature Extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB3BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b3', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDEfficientNetB4BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b4 BiFPN (EfficientDet-d4) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=7, - bifpn_num_filters=224, - bifpn_combine_method='fast_attention', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientDet-D4'): - - """SSD Keras EfficientNet-b4 BiFPN (EfficientDet-d4) Feature Extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB4BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b4', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDEfficientNetB5BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b5 BiFPN (EfficientDet-d5) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=7, - bifpn_num_filters=288, - bifpn_combine_method='fast_attention', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientDet-D5'): - - """SSD Keras EfficientNet-b5 BiFPN (EfficientDet-d5) Feature Extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB5BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b5', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDEfficientNetB6BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b6 BiFPN (EfficientDet-d[6,7]) Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=8, - bifpn_num_filters=384, - bifpn_combine_method='sum', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientDet-D6-D7'): - - """SSD Keras EfficientNet-b6 BiFPN (EfficientDet-d[6,7]) Feature Extractor. - - SSD Keras EfficientNet-b6 BiFPN Feature Extractor, a.k.a. EfficientDet-d6 - and EfficientDet-d7. The EfficientDet-d[6,7] models use the same backbone - EfficientNet-b6 and the same BiFPN architecture, and therefore have the same - number of parameters. They only differ in their input resolutions. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB6BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b6', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDEfficientNetB7BiFPNKerasFeatureExtractor( - SSDEfficientNetBiFPNKerasFeatureExtractor): - """SSD Keras EfficientNet-b7 BiFPN Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=8, - bifpn_num_filters=384, - bifpn_combine_method='sum', - use_explicit_padding=None, - use_depthwise=None, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=None, - name='EfficientNet-B7_BiFPN'): - - """SSD Keras EfficientNet-b7 BiFPN Feature Extractor. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: unsupported by EfficientNetBiFPN. float, depth - multiplier for the feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during training - or not. When training with a small batch size (e.g. 1), it is desirable - to freeze batch norm update and use pretrained batch norm params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - bifpn_min_level: the highest resolution feature map to use in BiFPN. The - valid values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - bifpn_max_level: the smallest resolution feature map to use in the BiFPN. - BiFPN constructions uses features maps starting from bifpn_min_level - upto the bifpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of BiFPN - levels. - bifpn_num_iterations: number of BiFPN iterations. Overrided if - efficientdet_version is provided. - bifpn_num_filters: number of filters (channels) in all BiFPN layers. - Overrided if efficientdet_version is provided. - bifpn_combine_method: the method used to combine BiFPN nodes. - use_explicit_padding: unsupported by EfficientNetBiFPN. Whether to use - explicit padding when extracting features. - use_depthwise: unsupported by EfficientNetBiFPN, since BiFPN uses regular - convolutions when inputs to a node have a differing number of channels, - and use separable convolutions after combine operations. - use_native_resize_op: If True, will use - tf.compat.v1.image.resize_nearest_neighbor for BiFPN unsampling. - override_base_feature_extractor_hyperparams: unsupported. Whether to - override hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras will - auto-generate one from the class name. - """ - super(SSDEfficientNetB7BiFPNKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - bifpn_min_level=bifpn_min_level, - bifpn_max_level=bifpn_max_level, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version='efficientnet-b7', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) diff --git a/research/object_detection/models/ssd_efficientnet_bifpn_feature_extractor_tf2_test.py b/research/object_detection/models/ssd_efficientnet_bifpn_feature_extractor_tf2_test.py deleted file mode 100644 index 450fcc8f854..00000000000 --- a/research/object_detection/models/ssd_efficientnet_bifpn_feature_extractor_tf2_test.py +++ /dev/null @@ -1,179 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for the ssd_efficientnet_bifpn_feature_extractor.""" -import unittest -from absl.testing import parameterized - -import numpy as np -import tensorflow.compat.v2 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.models import ssd_efficientnet_bifpn_feature_extractor -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -def _count_params(model, trainable_only=True): - """Returns the count of all model parameters, or just trainable ones.""" - if not trainable_only: - return model.count_params() - else: - return int(np.sum([ - tf.keras.backend.count_params(p) for p in model.trainable_weights])) - - -@parameterized.parameters( - {'efficientdet_version': 'efficientdet-d0', - 'efficientnet_version': 'efficientnet-b0', - 'bifpn_num_iterations': 3, - 'bifpn_num_filters': 64, - 'bifpn_combine_method': 'fast_attention'}, - {'efficientdet_version': 'efficientdet-d1', - 'efficientnet_version': 'efficientnet-b1', - 'bifpn_num_iterations': 4, - 'bifpn_num_filters': 88, - 'bifpn_combine_method': 'fast_attention'}, - {'efficientdet_version': 'efficientdet-d2', - 'efficientnet_version': 'efficientnet-b2', - 'bifpn_num_iterations': 5, - 'bifpn_num_filters': 112, - 'bifpn_combine_method': 'fast_attention'}, - {'efficientdet_version': 'efficientdet-d3', - 'efficientnet_version': 'efficientnet-b3', - 'bifpn_num_iterations': 6, - 'bifpn_num_filters': 160, - 'bifpn_combine_method': 'fast_attention'}, - {'efficientdet_version': 'efficientdet-d4', - 'efficientnet_version': 'efficientnet-b4', - 'bifpn_num_iterations': 7, - 'bifpn_num_filters': 224, - 'bifpn_combine_method': 'fast_attention'}, - {'efficientdet_version': 'efficientdet-d5', - 'efficientnet_version': 'efficientnet-b5', - 'bifpn_num_iterations': 7, - 'bifpn_num_filters': 288, - 'bifpn_combine_method': 'fast_attention'}, - # efficientdet-d6 and efficientdet-d7 only differ in input size. - {'efficientdet_version': 'efficientdet-d6-d7', - 'efficientnet_version': 'efficientnet-b6', - 'bifpn_num_iterations': 8, - 'bifpn_num_filters': 384, - 'bifpn_combine_method': 'sum'}) -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class SSDEfficientNetBiFPNFeatureExtractorTest( - test_case.TestCase, parameterized.TestCase): - - def _build_conv_hyperparams(self, add_batch_norm=True): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - force_use_bias: true - activation: SWISH - regularizer { - l2_regularizer { - weight: 0.0004 - } - } - initializer { - truncated_normal_initializer { - stddev: 0.03 - mean: 0.0 - } - } - """ - if add_batch_norm: - batch_norm_proto = """ - batch_norm { - scale: true, - decay: 0.99, - epsilon: 0.001, - } - """ - conv_hyperparams_text_proto += batch_norm_proto - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def _create_feature_extractor(self, - efficientnet_version='efficientnet-b0', - bifpn_num_iterations=3, - bifpn_num_filters=64, - bifpn_combine_method='fast_attention'): - """Constructs a new EfficientNetBiFPN feature extractor.""" - depth_multiplier = 1.0 - pad_to_multiple = 1 - min_depth = 16 - return (ssd_efficientnet_bifpn_feature_extractor - .SSDEfficientNetBiFPNKerasFeatureExtractor( - is_training=True, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - bifpn_min_level=3, - bifpn_max_level=7, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method, - efficientnet_version=efficientnet_version)) - - def test_efficientdet_feature_extractor_shapes(self, - efficientdet_version, - efficientnet_version, - bifpn_num_iterations, - bifpn_num_filters, - bifpn_combine_method): - feature_extractor = self._create_feature_extractor( - efficientnet_version=efficientnet_version, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method) - outputs = feature_extractor(np.zeros((2, 256, 256, 3), dtype=np.float32)) - - self.assertEqual(outputs[0].shape, (2, 32, 32, bifpn_num_filters)) - self.assertEqual(outputs[1].shape, (2, 16, 16, bifpn_num_filters)) - self.assertEqual(outputs[2].shape, (2, 8, 8, bifpn_num_filters)) - self.assertEqual(outputs[3].shape, (2, 4, 4, bifpn_num_filters)) - self.assertEqual(outputs[4].shape, (2, 2, 2, bifpn_num_filters)) - - def test_efficientdet_feature_extractor_params(self, - efficientdet_version, - efficientnet_version, - bifpn_num_iterations, - bifpn_num_filters, - bifpn_combine_method): - feature_extractor = self._create_feature_extractor( - efficientnet_version=efficientnet_version, - bifpn_num_iterations=bifpn_num_iterations, - bifpn_num_filters=bifpn_num_filters, - bifpn_combine_method=bifpn_combine_method) - _ = feature_extractor(np.zeros((2, 256, 256, 3), dtype=np.float32)) - expected_params = { - 'efficientdet-d0': 5484829, - 'efficientdet-d1': 8185156, - 'efficientdet-d2': 9818153, - 'efficientdet-d3': 13792706, - 'efficientdet-d4': 22691445, - 'efficientdet-d5': 35795677, - 'efficientdet-d6-d7': 53624512, - } - num_params = _count_params(feature_extractor) - self.assertEqual(expected_params[efficientdet_version], num_params) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_feature_extractor_test.py b/research/object_detection/models/ssd_feature_extractor_test.py deleted file mode 100644 index f28721ce745..00000000000 --- a/research/object_detection/models/ssd_feature_extractor_test.py +++ /dev/null @@ -1,263 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base test class SSDFeatureExtractors.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from abc import abstractmethod - -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf -import tf_slim as slim -from google.protobuf import text_format - -from object_detection.builders import hyperparams_builder -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import test_utils - - -class SsdFeatureExtractorTestBase(test_case.TestCase): - - def _build_conv_hyperparams(self, add_batch_norm=True): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - if add_batch_norm: - batch_norm_proto = """ - batch_norm { - scale: false - } - """ - conv_hyperparams_text_proto += batch_norm_proto - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def conv_hyperparams_fn(self): - with slim.arg_scope([]) as sc: - return sc - - @abstractmethod - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6, - use_keras=False, - use_depthwise=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - use_depthwise: Whether to use depthwise convolutions. - Returns: - an ssd_meta_arch.SSDFeatureExtractor or an - ssd_meta_arch.SSDKerasFeatureExtractor object. - """ - pass - - def _create_features(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - use_keras=False): - kwargs = {} - if use_explicit_padding: - kwargs.update({'use_explicit_padding': use_explicit_padding}) - if use_depthwise: - kwargs.update({'use_depthwise': use_depthwise}) - if num_layers != 6: - kwargs.update({'num_layers': num_layers}) - if use_keras: - kwargs.update({'use_keras': use_keras}) - feature_extractor = self._create_feature_extractor( - depth_multiplier, - pad_to_multiple, - **kwargs) - return feature_extractor - - def _extract_features(self, - image_tensor, - feature_extractor, - use_keras=False): - if use_keras: - feature_maps = feature_extractor(image_tensor) - else: - feature_maps = feature_extractor.extract_features(image_tensor) - return feature_maps - - def check_extract_features_returns_correct_shape(self, - batch_size, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shapes, - use_explicit_padding=False, - num_layers=6, - use_keras=False, - use_depthwise=False, - num_channels=3): - with test_utils.GraphContextOrNone() as g: - feature_extractor = self._create_features( - depth_multiplier, - pad_to_multiple, - use_explicit_padding=use_explicit_padding, - num_layers=num_layers, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def graph_fn(image_tensor): - return self._extract_features( - image_tensor, - feature_extractor, - use_keras=use_keras) - - image_tensor = np.random.rand(batch_size, image_height, image_width, - num_channels).astype(np.float32) - feature_maps = self.execute(graph_fn, [image_tensor], graph=g) - for feature_map, expected_shape in zip( - feature_maps, expected_feature_map_shapes): - self.assertAllEqual(feature_map.shape, expected_shape) - - def check_extract_features_returns_correct_shapes_with_dynamic_inputs( - self, - batch_size, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shapes, - use_explicit_padding=False, - num_layers=6, - use_keras=False, - use_depthwise=False): - - with test_utils.GraphContextOrNone() as g: - feature_extractor = self._create_features( - depth_multiplier, - pad_to_multiple, - use_explicit_padding=use_explicit_padding, - num_layers=num_layers, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def graph_fn(image_height, image_width): - image_tensor = tf.random_uniform([batch_size, image_height, image_width, - 3], dtype=tf.float32) - return self._extract_features( - image_tensor, - feature_extractor, - use_keras=use_keras) - - feature_maps = self.execute_cpu(graph_fn, [ - np.array(image_height, dtype=np.int32), - np.array(image_width, dtype=np.int32) - ], graph=g) - for feature_map, expected_shape in zip( - feature_maps, expected_feature_map_shapes): - self.assertAllEqual(feature_map.shape, expected_shape) - - def check_extract_features_raises_error_with_invalid_image_size( - self, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - use_keras=False, - use_depthwise=False): - - with test_utils.GraphContextOrNone() as g: - batch = 4 - width = tf.random.uniform([], minval=image_width, maxval=image_width+1, - dtype=tf.int32) - height = tf.random.uniform([], minval=image_height, maxval=image_height+1, - dtype=tf.int32) - shape = tf.stack([batch, height, width, 3]) - preprocessed_inputs = tf.random.uniform(shape) - feature_extractor = self._create_features( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def graph_fn(): - feature_maps = self._extract_features( - preprocessed_inputs, - feature_extractor, - use_keras=use_keras) - return feature_maps - if self.is_tf2(): - with self.assertRaises(ValueError): - self.execute_cpu(graph_fn, [], graph=g) - else: - with self.assertRaises(tf.errors.InvalidArgumentError): - self.execute_cpu(graph_fn, [], graph=g) - - def check_feature_extractor_variables_under_scope(self, - depth_multiplier, - pad_to_multiple, - scope_name, - use_keras=False, - use_depthwise=False): - variables = self.get_feature_extractor_variables( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - for variable in variables: - self.assertTrue(variable.name.startswith(scope_name)) - - def get_feature_extractor_variables(self, - depth_multiplier, - pad_to_multiple, - use_keras=False, - use_depthwise=False): - g = tf.Graph() - with g.as_default(): - feature_extractor = self._create_features( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - preprocessed_inputs = tf.placeholder(tf.float32, (4, None, None, 3)) - self._extract_features( - preprocessed_inputs, - feature_extractor, - use_keras=use_keras) - return g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) diff --git a/research/object_detection/models/ssd_inception_v2_feature_extractor.py b/research/object_detection/models/ssd_inception_v2_feature_extractor.py deleted file mode 100644 index d9cb20d7f4f..00000000000 --- a/research/object_detection/models/ssd_inception_v2_feature_extractor.py +++ /dev/null @@ -1,136 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for InceptionV2 features.""" -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import inception_v2 - - -class SSDInceptionV2FeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using InceptionV2 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False): - """InceptionV2 Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - - Raises: - ValueError: If `override_base_feature_extractor_hyperparams` is False. - """ - super(SSDInceptionV2FeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - num_layers=num_layers, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - if not self._override_base_feature_extractor_hyperparams: - raise ValueError('SSD Inception V2 feature extractor always uses' - 'scope returned by `conv_hyperparams_fn` for both the ' - 'base feature extractor and the additional layers ' - 'added since there is no arg_scope defined for the base ' - 'feature extractor.') - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - feature_map_layout = { - 'from_layer': ['Mixed_4c', 'Mixed_5c', '', '', '', '' - ][:self._num_layers], - 'layer_depth': [-1, -1, 512, 256, 256, 128][:self._num_layers], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - - with slim.arg_scope(self._conv_hyperparams_fn()): - with tf.variable_scope('InceptionV2', - reuse=self._reuse_weights) as scope: - _, image_features = inception_v2.inception_v2_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='Mixed_5c', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - scope=scope) - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) diff --git a/research/object_detection/models/ssd_inception_v2_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_inception_v2_feature_extractor_tf1_test.py deleted file mode 100644 index 1e33ed70ed4..00000000000 --- a/research/object_detection/models/ssd_inception_v2_feature_extractor_tf1_test.py +++ /dev/null @@ -1,160 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.models.ssd_inception_v2_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_inception_v2_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdInceptionV2FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6, - is_training=True): - """Constructs a SsdInceptionV2FeatureExtractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - is_training: whether the network is in training mode. - - Returns: - an ssd_inception_v2_feature_extractor.SsdInceptionV2FeatureExtractor. - """ - min_depth = 32 - return ssd_inception_v2_feature_extractor.SSDInceptionV2FeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - num_layers=num_layers, - override_base_feature_extractor_hyperparams=True) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_dynamic_inputs(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 576), (2, 10, 10, 1024), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth(self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 128), (2, 10, 10, 128), - (2, 5, 5, 32), (2, 3, 3, 32), - (2, 2, 2, 32), (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 20, 20, 576), (2, 10, 10, 1024), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_raises_error_with_invalid_image_size(self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'InceptionV2' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name) - - def test_extract_features_with_fewer_layers(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, num_layers=4) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_inception_v3_feature_extractor.py b/research/object_detection/models/ssd_inception_v3_feature_extractor.py deleted file mode 100644 index 5031e121104..00000000000 --- a/research/object_detection/models/ssd_inception_v3_feature_extractor.py +++ /dev/null @@ -1,136 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for InceptionV3 features.""" -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import inception_v3 - - -class SSDInceptionV3FeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using InceptionV3 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False): - """InceptionV3 Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - - Raises: - ValueError: If `override_base_feature_extractor_hyperparams` is False. - """ - super(SSDInceptionV3FeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - num_layers=num_layers, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - - if not self._override_base_feature_extractor_hyperparams: - raise ValueError('SSD Inception V3 feature extractor always uses' - 'scope returned by `conv_hyperparams_fn` for both the ' - 'base feature extractor and the additional layers ' - 'added since there is no arg_scope defined for the base ' - 'feature extractor.') - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - feature_map_layout = { - 'from_layer': ['Mixed_5d', 'Mixed_6e', 'Mixed_7c', '', '', '' - ][:self._num_layers], - 'layer_depth': [-1, -1, -1, 512, 256, 128][:self._num_layers], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - - with slim.arg_scope(self._conv_hyperparams_fn()): - with tf.variable_scope('InceptionV3', reuse=self._reuse_weights) as scope: - _, image_features = inception_v3.inception_v3_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='Mixed_7c', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - scope=scope) - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) diff --git a/research/object_detection/models/ssd_inception_v3_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_inception_v3_feature_extractor_tf1_test.py deleted file mode 100644 index a0cbb451586..00000000000 --- a/research/object_detection/models/ssd_inception_v3_feature_extractor_tf1_test.py +++ /dev/null @@ -1,160 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.models.ssd_inception_v3_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_inception_v3_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdInceptionV3FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6, - is_training=True): - """Constructs a SsdInceptionV3FeatureExtractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - is_training: whether the network is in training mode. - - Returns: - an ssd_inception_v3_feature_extractor.SsdInceptionV3FeatureExtractor. - """ - min_depth = 32 - return ssd_inception_v3_feature_extractor.SSDInceptionV3FeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - num_layers=num_layers, - override_base_feature_extractor_hyperparams=True) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 13, 13, 288), (2, 6, 6, 768), - (2, 2, 2, 2048), (2, 1, 1, 512), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_dynamic_inputs(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 13, 13, 288), (2, 6, 6, 768), - (2, 2, 2, 2048), (2, 1, 1, 512), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 35, 35, 288), (2, 17, 17, 768), - (2, 8, 8, 2048), (2, 4, 4, 512), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth(self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 35, 35, 128), (2, 17, 17, 128), - (2, 8, 8, 192), (2, 4, 4, 32), - (2, 2, 2, 32), (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 37, 37, 288), (2, 18, 18, 768), - (2, 8, 8, 2048), (2, 4, 4, 512), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_raises_error_with_invalid_image_size(self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'InceptionV3' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name) - - def test_extract_features_with_fewer_layers(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 13, 13, 288), (2, 6, 6, 768), - (2, 2, 2, 2048), (2, 1, 1, 512)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, num_layers=4) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobiledet_feature_extractor.py b/research/object_detection/models/ssd_mobiledet_feature_extractor.py deleted file mode 100644 index 019d7543bb7..00000000000 --- a/research/object_detection/models/ssd_mobiledet_feature_extractor.py +++ /dev/null @@ -1,586 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSDFeatureExtractor for MobileDet features.""" - -import functools -import numpy as np -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -BACKBONE_WEIGHT_DECAY = 4e-5 - - -def _scale_filters(filters, multiplier, base=8): - """Scale the filters accordingly to (multiplier, base).""" - round_half_up = int(int(filters) * multiplier / base + 0.5) - result = int(round_half_up * base) - return max(result, base) - - -def _swish6(h): - with tf.name_scope('swish6'): - return h * tf.nn.relu6(h + np.float32(3)) * np.float32(1. / 6.) - - -def _conv(h, filters, kernel_size, strides=1, - normalizer_fn=slim.batch_norm, activation_fn=tf.nn.relu6): - if activation_fn is None: - raise ValueError('Activation function cannot be None. Use tf.identity ' - 'instead to better support quantized training.') - return slim.conv2d( - h, - filters, - kernel_size, - stride=strides, - activation_fn=activation_fn, - normalizer_fn=normalizer_fn, - weights_initializer=tf.initializers.he_normal(), - weights_regularizer=slim.l2_regularizer(BACKBONE_WEIGHT_DECAY), - padding='SAME') - - -def _separable_conv( - h, filters, kernel_size, strides=1, activation_fn=tf.nn.relu6): - """Separable convolution layer.""" - if activation_fn is None: - raise ValueError('Activation function cannot be None. Use tf.identity ' - 'instead to better support quantized training.') - # Depthwise variant of He initialization derived under the principle proposed - # in the original paper. Note the original He normalization was designed for - # full convolutions and calling tf.initializers.he_normal() can over-estimate - # the fan-in of a depthwise kernel by orders of magnitude. - stddev = (2.0 / kernel_size**2)**0.5 / .87962566103423978 - depthwise_initializer = tf.initializers.truncated_normal(stddev=stddev) - return slim.separable_conv2d( - h, - filters, - kernel_size, - stride=strides, - activation_fn=activation_fn, - normalizer_fn=slim.batch_norm, - weights_initializer=depthwise_initializer, - pointwise_initializer=tf.initializers.he_normal(), - weights_regularizer=slim.l2_regularizer(BACKBONE_WEIGHT_DECAY), - padding='SAME') - - -def _squeeze_and_excite(h, hidden_dim, activation_fn=tf.nn.relu6): - with tf.variable_scope(None, default_name='SqueezeExcite'): - height, width = h.shape[1], h.shape[2] - u = slim.avg_pool2d(h, [height, width], stride=1, padding='VALID') - u = _conv(u, hidden_dim, 1, - normalizer_fn=None, activation_fn=activation_fn) - u = _conv(u, h.shape[-1], 1, - normalizer_fn=None, activation_fn=tf.nn.sigmoid) - return u * h - - -def _inverted_bottleneck_no_expansion( - h, filters, activation_fn=tf.nn.relu6, - kernel_size=3, strides=1, use_se=False): - """Inverted bottleneck layer without the first 1x1 expansion convolution.""" - with tf.variable_scope(None, default_name='IBNNoExpansion'): - # Setting filters to None will make _separable_conv a depthwise conv. - h = _separable_conv( - h, None, kernel_size, strides=strides, activation_fn=activation_fn) - if use_se: - hidden_dim = _scale_filters(h.shape[-1], 0.25) - h = _squeeze_and_excite(h, hidden_dim, activation_fn=activation_fn) - h = _conv(h, filters, 1, activation_fn=tf.identity) - return h - - -def _inverted_bottleneck( - h, filters, activation_fn=tf.nn.relu6, - kernel_size=3, expansion=8, strides=1, use_se=False, residual=True): - """Inverted bottleneck layer.""" - with tf.variable_scope(None, default_name='IBN'): - shortcut = h - expanded_filters = int(h.shape[-1]) * expansion - if expansion <= 1: - raise ValueError('Expansion factor must be greater than 1.') - h = _conv(h, expanded_filters, 1, activation_fn=activation_fn) - # Setting filters to None will make _separable_conv a depthwise conv. - h = _separable_conv(h, None, kernel_size, strides=strides, - activation_fn=activation_fn) - if use_se: - hidden_dim = _scale_filters(expanded_filters, 0.25) - h = _squeeze_and_excite(h, hidden_dim, activation_fn=activation_fn) - h = _conv(h, filters, 1, activation_fn=tf.identity) - if residual: - h = h + shortcut - return h - - -def _fused_conv( - h, filters, activation_fn=tf.nn.relu6, - kernel_size=3, expansion=8, strides=1, use_se=False, residual=True): - """Fused convolution layer.""" - with tf.variable_scope(None, default_name='FusedConv'): - shortcut = h - expanded_filters = int(h.shape[-1]) * expansion - if expansion <= 1: - raise ValueError('Expansion factor must be greater than 1.') - h = _conv(h, expanded_filters, kernel_size, strides=strides, - activation_fn=activation_fn) - if use_se: - hidden_dim = _scale_filters(expanded_filters, 0.25) - h = _squeeze_and_excite(h, hidden_dim, activation_fn=activation_fn) - h = _conv(h, filters, 1, activation_fn=tf.identity) - if residual: - h = h + shortcut - return h - - -def _tucker_conv( - h, filters, activation_fn=tf.nn.relu6, - kernel_size=3, input_rank_ratio=0.25, output_rank_ratio=0.25, - strides=1, residual=True): - """Tucker convolution layer (generalized bottleneck).""" - with tf.variable_scope(None, default_name='TuckerConv'): - shortcut = h - input_rank = _scale_filters(h.shape[-1], input_rank_ratio) - h = _conv(h, input_rank, 1, activation_fn=activation_fn) - output_rank = _scale_filters(filters, output_rank_ratio) - h = _conv(h, output_rank, kernel_size, strides=strides, - activation_fn=activation_fn) - h = _conv(h, filters, 1, activation_fn=tf.identity) - if residual: - h = h + shortcut - return h - - -def mobiledet_cpu_backbone(h, multiplier=1.0): - """Build a MobileDet CPU backbone.""" - def _scale(filters): - return _scale_filters(filters, multiplier) - ibn = functools.partial( - _inverted_bottleneck, use_se=True, activation_fn=_swish6) - - endpoints = {} - h = _conv(h, _scale(16), 3, strides=2, activation_fn=_swish6) - h = _inverted_bottleneck_no_expansion( - h, _scale(8), use_se=True, activation_fn=_swish6) - endpoints['C1'] = h - h = ibn(h, _scale(16), expansion=4, strides=2, residual=False) - endpoints['C2'] = h - h = ibn(h, _scale(32), expansion=8, strides=2, residual=False) - h = ibn(h, _scale(32), expansion=4) - h = ibn(h, _scale(32), expansion=4) - h = ibn(h, _scale(32), expansion=4) - endpoints['C3'] = h - h = ibn(h, _scale(72), kernel_size=5, expansion=8, strides=2, residual=False) - h = ibn(h, _scale(72), expansion=8) - h = ibn(h, _scale(72), kernel_size=5, expansion=4) - h = ibn(h, _scale(72), expansion=4) - h = ibn(h, _scale(72), expansion=8, residual=False) - h = ibn(h, _scale(72), expansion=8) - h = ibn(h, _scale(72), expansion=8) - h = ibn(h, _scale(72), expansion=8) - endpoints['C4'] = h - h = ibn(h, _scale(104), kernel_size=5, expansion=8, strides=2, residual=False) - h = ibn(h, _scale(104), kernel_size=5, expansion=4) - h = ibn(h, _scale(104), kernel_size=5, expansion=4) - h = ibn(h, _scale(104), expansion=4) - h = ibn(h, _scale(144), expansion=8, residual=False) - endpoints['C5'] = h - return endpoints - - -def mobiledet_dsp_backbone(h, multiplier=1.0): - """Build a MobileDet DSP backbone.""" - def _scale(filters): - return _scale_filters(filters, multiplier) - - ibn = functools.partial(_inverted_bottleneck, activation_fn=tf.nn.relu6) - fused = functools.partial(_fused_conv, activation_fn=tf.nn.relu6) - tucker = functools.partial(_tucker_conv, activation_fn=tf.nn.relu6) - - endpoints = {} - h = _conv(h, _scale(32), 3, strides=2, activation_fn=tf.nn.relu6) - h = _inverted_bottleneck_no_expansion( - h, _scale(24), activation_fn=tf.nn.relu6) - endpoints['C1'] = h - h = fused(h, _scale(32), expansion=4, strides=2, residual=False) - h = fused(h, _scale(32), expansion=4) - h = ibn(h, _scale(32), expansion=4) - h = tucker(h, _scale(32), input_rank_ratio=0.25, output_rank_ratio=0.75) - endpoints['C2'] = h - h = fused(h, _scale(64), expansion=8, strides=2, residual=False) - h = ibn(h, _scale(64), expansion=4) - h = fused(h, _scale(64), expansion=4) - h = fused(h, _scale(64), expansion=4) - endpoints['C3'] = h - h = fused(h, _scale(120), expansion=8, strides=2, residual=False) - h = ibn(h, _scale(120), expansion=4) - h = ibn(h, _scale(120), expansion=8) - h = ibn(h, _scale(120), expansion=8) - h = fused(h, _scale(144), expansion=8, residual=False) - h = ibn(h, _scale(144), expansion=8) - h = ibn(h, _scale(144), expansion=8) - h = ibn(h, _scale(144), expansion=8) - endpoints['C4'] = h - h = ibn(h, _scale(160), expansion=4, strides=2, residual=False) - h = ibn(h, _scale(160), expansion=4) - h = fused(h, _scale(160), expansion=4) - h = tucker(h, _scale(160), input_rank_ratio=0.75, output_rank_ratio=0.75) - h = ibn(h, _scale(240), expansion=8, residual=False) - endpoints['C5'] = h - return endpoints - - -def mobiledet_edgetpu_backbone(h, multiplier=1.0): - """Build a MobileDet EdgeTPU backbone.""" - def _scale(filters): - return _scale_filters(filters, multiplier) - - ibn = functools.partial(_inverted_bottleneck, activation_fn=tf.nn.relu6) - fused = functools.partial(_fused_conv, activation_fn=tf.nn.relu6) - tucker = functools.partial(_tucker_conv, activation_fn=tf.nn.relu6) - - endpoints = {} - h = _conv(h, _scale(32), 3, strides=2, activation_fn=tf.nn.relu6) - h = tucker(h, _scale(16), - input_rank_ratio=0.25, output_rank_ratio=0.75, residual=False) - endpoints['C1'] = h - h = fused(h, _scale(16), expansion=8, strides=2, residual=False) - h = fused(h, _scale(16), expansion=4) - h = fused(h, _scale(16), expansion=8) - h = fused(h, _scale(16), expansion=4) - endpoints['C2'] = h - h = fused(h, _scale(40), expansion=8, kernel_size=5, strides=2, - residual=False) - h = fused(h, _scale(40), expansion=4) - h = fused(h, _scale(40), expansion=4) - h = fused(h, _scale(40), expansion=4) - endpoints['C3'] = h - h = ibn(h, _scale(72), expansion=8, strides=2, residual=False) - h = ibn(h, _scale(72), expansion=8) - h = fused(h, _scale(72), expansion=4) - h = fused(h, _scale(72), expansion=4) - h = ibn(h, _scale(96), expansion=8, kernel_size=5, residual=False) - h = ibn(h, _scale(96), expansion=8, kernel_size=5) - h = ibn(h, _scale(96), expansion=8) - h = ibn(h, _scale(96), expansion=8) - endpoints['C4'] = h - h = ibn(h, _scale(120), expansion=8, kernel_size=5, strides=2, residual=False) - h = ibn(h, _scale(120), expansion=8) - h = ibn(h, _scale(120), expansion=4, kernel_size=5) - h = ibn(h, _scale(120), expansion=8) - h = ibn(h, _scale(384), expansion=8, kernel_size=5, residual=False) - endpoints['C5'] = h - return endpoints - - -def mobiledet_gpu_backbone(h, multiplier=1.0): - """Build a MobileDet GPU backbone.""" - - def _scale(filters): - return _scale_filters(filters, multiplier) - - ibn = functools.partial(_inverted_bottleneck, activation_fn=tf.nn.relu6) - fused = functools.partial(_fused_conv, activation_fn=tf.nn.relu6) - tucker = functools.partial(_tucker_conv, activation_fn=tf.nn.relu6) - - endpoints = {} - # block 0 - h = _conv(h, _scale(32), 3, strides=2, activation_fn=tf.nn.relu6) - - # block 1 - h = tucker( - h, - _scale(16), - input_rank_ratio=0.25, - output_rank_ratio=0.25, - residual=False) - endpoints['C1'] = h - - # block 2 - h = fused(h, _scale(32), expansion=8, strides=2, residual=False) - h = tucker(h, _scale(32), input_rank_ratio=0.25, output_rank_ratio=0.25) - h = tucker(h, _scale(32), input_rank_ratio=0.25, output_rank_ratio=0.25) - h = tucker(h, _scale(32), input_rank_ratio=0.25, output_rank_ratio=0.25) - endpoints['C2'] = h - - # block 3 - h = fused( - h, _scale(64), expansion=8, kernel_size=3, strides=2, residual=False) - h = fused(h, _scale(64), expansion=8) - h = fused(h, _scale(64), expansion=8) - h = fused(h, _scale(64), expansion=4) - endpoints['C3'] = h - - # block 4 - h = fused( - h, _scale(128), expansion=8, kernel_size=3, strides=2, residual=False) - h = fused(h, _scale(128), expansion=4) - h = fused(h, _scale(128), expansion=4) - h = fused(h, _scale(128), expansion=4) - - # block 5 - h = fused( - h, _scale(128), expansion=8, kernel_size=3, strides=1, residual=False) - h = fused(h, _scale(128), expansion=8) - h = fused(h, _scale(128), expansion=8) - h = fused(h, _scale(128), expansion=8) - endpoints['C4'] = h - - # block 6 - h = fused( - h, _scale(128), expansion=4, kernel_size=3, strides=2, residual=False) - h = fused(h, _scale(128), expansion=4) - h = fused(h, _scale(128), expansion=4) - h = fused(h, _scale(128), expansion=4) - - # block 7 - h = ibn(h, _scale(384), expansion=8, kernel_size=3, strides=1, residual=False) - endpoints['C5'] = h - return endpoints - - -class SSDMobileDetFeatureExtractorBase(ssd_meta_arch.SSDFeatureExtractor): - """Base class of SSD feature extractor using MobileDet features.""" - - def __init__(self, - backbone_fn, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobileDet'): - """MobileDet Feature Extractor for SSD Models. - - Reference: - https://arxiv.org/abs/2004.14525 - - Args: - backbone_fn: function to construct the MobileDet backbone. - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: Integer, minimum feature extractor depth (number of filters). - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the base - feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. - use_depthwise: Whether to use depthwise convolutions in the SSD head. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - scope_name: scope name (string) of network variables. - """ - if use_explicit_padding: - raise NotImplementedError( - 'Explicit padding is not yet supported in MobileDet backbones.') - - super(SSDMobileDetFeatureExtractorBase, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams - ) - self._backbone_fn = backbone_fn - self._scope_name = scope_name - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. The preprocessing assumes an input - value range of [0, 255]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - padded_inputs = ops.pad_to_multiple( - preprocessed_inputs, self._pad_to_multiple) - - feature_map_layout = { - 'from_layer': ['C4', 'C5', '', '', '', ''], - # Do not specify the layer depths (number of filters) for C4 and C5, as - # their values are determined based on the backbone. - 'layer_depth': [-1, -1, 512, 256, 256, 128], - 'use_depthwise': self._use_depthwise, - 'use_explicit_padding': self._use_explicit_padding, - } - - with tf.variable_scope(self._scope_name, reuse=self._reuse_weights): - with slim.arg_scope([slim.batch_norm], - is_training=self._is_training, - epsilon=0.01, decay=0.99, center=True, scale=True): - endpoints = self._backbone_fn( - padded_inputs, - multiplier=self._depth_multiplier) - - image_features = {'C4': endpoints['C4'], 'C5': endpoints['C5']} - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) - - -class SSDMobileDetCPUFeatureExtractor(SSDMobileDetFeatureExtractorBase): - """MobileDet-CPU feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobileDetCPU'): - super(SSDMobileDetCPUFeatureExtractor, self).__init__( - backbone_fn=mobiledet_cpu_backbone, - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name) - - -class SSDMobileDetDSPFeatureExtractor(SSDMobileDetFeatureExtractorBase): - """MobileDet-DSP feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobileDetDSP'): - super(SSDMobileDetDSPFeatureExtractor, self).__init__( - backbone_fn=mobiledet_dsp_backbone, - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name) - - -class SSDMobileDetEdgeTPUFeatureExtractor(SSDMobileDetFeatureExtractorBase): - """MobileDet-EdgeTPU feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobileDetEdgeTPU'): - super(SSDMobileDetEdgeTPUFeatureExtractor, self).__init__( - backbone_fn=mobiledet_edgetpu_backbone, - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name) - - -class SSDMobileDetGPUFeatureExtractor(SSDMobileDetFeatureExtractorBase): - """MobileDet-GPU feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobileDetGPU'): - super(SSDMobileDetGPUFeatureExtractor, self).__init__( - backbone_fn=mobiledet_gpu_backbone, - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name) diff --git a/research/object_detection/models/ssd_mobiledet_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobiledet_feature_extractor_tf1_test.py deleted file mode 100644 index 2af37554b55..00000000000 --- a/research/object_detection/models/ssd_mobiledet_feature_extractor_tf1_test.py +++ /dev/null @@ -1,172 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd_mobiledet_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobiledet_feature_extractor -from object_detection.utils import tf_version - -try: - from tensorflow.contrib import quantize as contrib_quantize # pylint: disable=g-import-not-at-top -except: # pylint: disable=bare-except - pass - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDMobileDetFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - feature_extractor_cls, - is_training=False, - depth_multiplier=1.0, - pad_to_multiple=1, - use_explicit_padding=False, - use_keras=False): - """Constructs a new MobileDet feature extractor. - - Args: - feature_extractor_cls: feature extractor class. - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: If True, we will use 'VALID' padding for - convolutions, but prepad inputs so that the output dimensions are the - same as if 'SAME' padding were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - - Returns: - an ssd_meta_arch.SSDMobileDetFeatureExtractor object. - """ - min_depth = 32 - return feature_extractor_cls( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding) - - def test_mobiledet_cpu_returns_correct_shapes(self): - expected_feature_map_shapes = [(2, 40, 20, 72), - (2, 20, 10, 144), - (2, 10, 5, 512), - (2, 5, 3, 256), - (2, 3, 2, 256), - (2, 2, 1, 128)] - feature_extractor = self._create_feature_extractor( - ssd_mobiledet_feature_extractor.SSDMobileDetCPUFeatureExtractor) - image = tf.random.normal((2, 640, 320, 3)) - feature_maps = feature_extractor.extract_features(image) - - self.assertEqual(len(expected_feature_map_shapes), len(feature_maps)) - for expected_shape, x in zip(expected_feature_map_shapes, feature_maps): - self.assertTrue(x.shape.is_compatible_with(expected_shape)) - - def test_mobiledet_dsp_returns_correct_shapes(self): - expected_feature_map_shapes = [(2, 40, 20, 144), - (2, 20, 10, 240), - (2, 10, 5, 512), - (2, 5, 3, 256), - (2, 3, 2, 256), - (2, 2, 1, 128)] - feature_extractor = self._create_feature_extractor( - ssd_mobiledet_feature_extractor.SSDMobileDetDSPFeatureExtractor) - image = tf.random.normal((2, 640, 320, 3)) - feature_maps = feature_extractor.extract_features(image) - - self.assertEqual(len(expected_feature_map_shapes), len(feature_maps)) - for expected_shape, x in zip(expected_feature_map_shapes, feature_maps): - self.assertTrue(x.shape.is_compatible_with(expected_shape)) - - def test_mobiledet_edgetpu_returns_correct_shapes(self): - expected_feature_map_shapes = [(2, 40, 20, 96), - (2, 20, 10, 384), - (2, 10, 5, 512), - (2, 5, 3, 256), - (2, 3, 2, 256), - (2, 2, 1, 128)] - feature_extractor = self._create_feature_extractor( - ssd_mobiledet_feature_extractor.SSDMobileDetEdgeTPUFeatureExtractor) - image = tf.random.normal((2, 640, 320, 3)) - feature_maps = feature_extractor.extract_features(image) - - self.assertEqual(len(expected_feature_map_shapes), len(feature_maps)) - for expected_shape, x in zip(expected_feature_map_shapes, feature_maps): - self.assertTrue(x.shape.is_compatible_with(expected_shape)) - - def test_mobiledet_gpu_returns_correct_shapes(self): - expected_feature_map_shapes = [(2, 40, 20, 128), (2, 20, 10, 384), - (2, 10, 5, 512), (2, 5, 3, 256), - (2, 3, 2, 256), (2, 2, 1, 128)] - feature_extractor = self._create_feature_extractor( - ssd_mobiledet_feature_extractor.SSDMobileDetGPUFeatureExtractor) - image = tf.random.normal((2, 640, 320, 3)) - feature_maps = feature_extractor.extract_features(image) - - self.assertEqual(len(expected_feature_map_shapes), len(feature_maps)) - for expected_shape, x in zip(expected_feature_map_shapes, feature_maps): - self.assertTrue(x.shape.is_compatible_with(expected_shape)) - - def _check_quantization(self, model_fn): - checkpoint_dir = self.get_temp_dir() - - with tf.Graph().as_default() as training_graph: - model_fn(is_training=True) - contrib_quantize.experimental_create_training_graph(training_graph) - with self.session(graph=training_graph) as sess: - sess.run(tf.global_variables_initializer()) - tf.train.Saver().save(sess, checkpoint_dir) - - with tf.Graph().as_default() as eval_graph: - model_fn(is_training=False) - contrib_quantize.experimental_create_eval_graph(eval_graph) - with self.session(graph=eval_graph) as sess: - tf.train.Saver().restore(sess, checkpoint_dir) - - def test_mobiledet_cpu_quantization(self): - def model_fn(is_training): - feature_extractor = self._create_feature_extractor( - ssd_mobiledet_feature_extractor.SSDMobileDetCPUFeatureExtractor, - is_training=is_training) - image = tf.random.normal((2, 320, 320, 3)) - feature_extractor.extract_features(image) - self._check_quantization(model_fn) - - def test_mobiledet_dsp_quantization(self): - def model_fn(is_training): - feature_extractor = self._create_feature_extractor( - ssd_mobiledet_feature_extractor.SSDMobileDetDSPFeatureExtractor, - is_training=is_training) - image = tf.random.normal((2, 320, 320, 3)) - feature_extractor.extract_features(image) - self._check_quantization(model_fn) - - def test_mobiledet_edgetpu_quantization(self): - def model_fn(is_training): - feature_extractor = self._create_feature_extractor( - ssd_mobiledet_feature_extractor.SSDMobileDetEdgeTPUFeatureExtractor, - is_training=is_training) - image = tf.random.normal((2, 320, 320, 3)) - feature_extractor.extract_features(image) - self._check_quantization(model_fn) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor.py deleted file mode 100644 index 6de4cae310e..00000000000 --- a/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor.py +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSDFeatureExtractor for MobileNetEdgeTPU features.""" - -from object_detection.models import ssd_mobilenet_v3_feature_extractor -from nets.mobilenet import mobilenet_v3 - - -class SSDMobileNetEdgeTPUFeatureExtractor( - ssd_mobilenet_v3_feature_extractor.SSDMobileNetV3FeatureExtractorBase): - """MobileNetEdgeTPU feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobilenetEdgeTPU'): - super(SSDMobileNetEdgeTPUFeatureExtractor, self).__init__( - conv_defs=mobilenet_v3.V3_EDGETPU, - from_layer=['layer_18/expansion_output', 'layer_23'], - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name - ) diff --git a/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor_testbase.py b/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor_testbase.py deleted file mode 100644 index ce3290f895a..00000000000 --- a/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor_testbase.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Base test class for ssd_mobilenet_edgetpu_feature_extractor.""" - -import abc - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test - - -class _SsdMobilenetEdgeTPUFeatureExtractorTestBase( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - """Base class for MobilenetEdgeTPU tests.""" - - @abc.abstractmethod - def _get_input_sizes(self): - """Return feature map sizes for the two inputs to SSD head.""" - pass - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - input_feature_sizes = self._get_input_sizes() - expected_feature_map_shape = [(2, 8, 8, input_feature_sizes[0]), - (2, 4, 4, input_feature_sizes[1]), - (2, 2, 2, 512), (2, 1, 1, 256), (2, 1, 1, - 256), - (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_keras=False) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - input_feature_sizes = self._get_input_sizes() - expected_feature_map_shape = [(2, 19, 19, input_feature_sizes[0]), - (2, 10, 10, input_feature_sizes[1]), - (2, 5, 5, 512), (2, 3, 3, 256), (2, 2, 2, - 256), - (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_keras=False) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - input_feature_sizes = self._get_input_sizes() - expected_feature_map_shape = [(2, 20, 20, input_feature_sizes[0]), - (2, 10, 10, input_feature_sizes[1]), - (2, 5, 5, 512), (2, 3, 3, 256), (2, 2, 2, - 256), - (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=False) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_has_fused_batchnorm(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=False) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image) - self.assertTrue(any('FusedBatchNorm' in op.type - for op in tf.get_default_graph().get_operations())) diff --git a/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor_tf1_test.py deleted file mode 100644 index 841fe5a1488..00000000000 --- a/research/object_detection/models/ssd_mobilenet_edgetpu_feature_extractor_tf1_test.py +++ /dev/null @@ -1,65 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd_mobilenet_edgetpu_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_mobilenet_edgetpu_feature_extractor -from object_detection.models import ssd_mobilenet_edgetpu_feature_extractor_testbase -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetEdgeTPUFeatureExtractorTest( - ssd_mobilenet_edgetpu_feature_extractor_testbase - ._SsdMobilenetEdgeTPUFeatureExtractorTestBase): - - def _get_input_sizes(self): - """Return first two input feature map sizes.""" - return [384, 192] - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - use_keras=False): - """Constructs a new MobileNetEdgeTPU feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - return (ssd_mobilenet_edgetpu_feature_extractor - .SSDMobileNetEdgeTPUFeatureExtractor( - False, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v1_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v1_feature_extractor.py deleted file mode 100644 index 8e9e68fd9ee..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_feature_extractor.py +++ /dev/null @@ -1,137 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for MobilenetV1 features.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import mobilenet_v1 - - -class SSDMobileNetV1FeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using MobilenetV1 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False): - """MobileNetV1 Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDMobileNetV1FeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - num_layers=num_layers, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - feature_map_layout = { - 'from_layer': ['Conv2d_11_pointwise', 'Conv2d_13_pointwise', '', '', - '', ''][:self._num_layers], - 'layer_depth': [-1, -1, 512, 256, 256, 128][:self._num_layers], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - - with tf.variable_scope('MobilenetV1', - reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v1.mobilenet_v1_arg_scope( - is_training=None, regularize_depthwise=True)): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams - else context_manager.IdentityContextManager()): - _, image_features = mobilenet_v1.mobilenet_v1_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='Conv2d_13_pointwise', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) diff --git a/research/object_detection/models/ssd_mobilenet_v1_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_v1_feature_extractor_tf1_test.py deleted file mode 100644 index 2f1d4839693..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_feature_extractor_tf1_test.py +++ /dev/null @@ -1,272 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for SSD Mobilenet V1 feature extractors. - -By using parameterized test decorator, this test serves for both Slim-based and -Keras-based Mobilenet V1 feature extractors in SSD. -""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v1_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetV1FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6, - is_training=False, - use_keras=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - is_training: whether the network is in training mode. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - del use_keras - return ssd_mobilenet_v1_feature_extractor.SSDMobileNetV1FeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding, - num_layers=num_layers) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 512), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=False) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 512), (2, 10, 10, 1024), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=False) - - def test_extract_features_with_dynamic_image_shape(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 512), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=False) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 32), (2, 10, 10, 32), - (2, 5, 5, 32), (2, 3, 3, 32), (2, 2, 2, 32), - (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=False) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 20, 20, 512), (2, 10, 10, 1024), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=False) - - def test_extract_features_raises_error_with_invalid_image_size( - self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - use_keras=False) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=False) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'MobilenetV1' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name, use_keras=False) - - def test_variable_count(self): - depth_multiplier = 1 - pad_to_multiple = 1 - variables = self.get_feature_extractor_variables( - depth_multiplier, pad_to_multiple, use_keras=False) - self.assertEqual(len(variables), 151) - - def test_has_fused_batchnorm(self): - image_height = 40 - image_width = 40 - depth_multiplier = 1 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=False) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image) - self.assertTrue( - any('FusedBatchNorm' in op.type - for op in tf.get_default_graph().get_operations())) - - def test_extract_features_with_fewer_layers(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 512), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, num_layers=4, - use_keras=False) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v1_feature_extractor_tf2_test.py b/research/object_detection/models/ssd_mobilenet_v1_feature_extractor_tf2_test.py deleted file mode 100644 index b60537b8869..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_feature_extractor_tf2_test.py +++ /dev/null @@ -1,248 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for SSD Mobilenet V1 feature extractors. - -By using parameterized test decorator, this test serves for both Slim-based and -Keras-based Mobilenet V1 feature extractors in SSD. -""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v1_keras_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class SsdMobilenetV1FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6, - is_training=False, - use_keras=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - is_training: whether the network is in training mode. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - del use_keras - min_depth = 32 - return (ssd_mobilenet_v1_keras_feature_extractor - .SSDMobileNetV1KerasFeatureExtractor( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams( - add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - use_explicit_padding=use_explicit_padding, - num_layers=num_layers, - name='MobilenetV1')) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 512), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=True) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 512), (2, 10, 10, 1024), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=True) - - def test_extract_features_with_dynamic_image_shape(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 512), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=True) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 32), (2, 10, 10, 32), - (2, 5, 5, 32), (2, 3, 3, 32), (2, 2, 2, 32), - (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=True) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 20, 20, 512), (2, 10, 10, 1024), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=True) - - def test_extract_features_raises_error_with_invalid_image_size( - self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - use_keras=True) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=True) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_extract_features_with_fewer_layers(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 512), (2, 4, 4, 1024), - (2, 2, 2, 512), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, num_layers=4, - use_keras=True) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor.py deleted file mode 100644 index 8169b782203..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor.py +++ /dev/null @@ -1,201 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSD MobilenetV1 FPN Feature Extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import copy -import functools -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import mobilenet_v1 - - -# A modified config of mobilenet v1 that makes it more detection friendly, -def _create_modified_mobilenet_config(): - conv_defs = copy.deepcopy(mobilenet_v1.MOBILENETV1_CONV_DEFS) - conv_defs[-2] = mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=2, depth=512) - conv_defs[-1] = mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=256) - return conv_defs - - -class SSDMobileNetV1FpnFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using MobilenetV1 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False): - """SSD FPN feature extractor based on Mobilenet v1 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the base - feature extractor. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to MobileNet v1 layers - {Conv2d_3_pointwise, Conv2d_5_pointwise, Conv2d_11_pointwise, - Conv2d_13_pointwise}, respectively. - fpn_max_level: the smallest resolution feature map to construct or use in - FPN. FPN constructions uses features maps starting from fpn_min_level - upto the fpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of fpn - levels. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDMobileNetV1FpnFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._additional_layer_depth = additional_layer_depth - self._conv_defs = None - if self._use_depthwise: - self._conv_defs = _create_modified_mobilenet_config() - self._use_native_resize_op = use_native_resize_op - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - with tf.variable_scope('MobilenetV1', - reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v1.mobilenet_v1_arg_scope( - is_training=None, regularize_depthwise=True)): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams - else context_manager.IdentityContextManager()): - _, image_features = mobilenet_v1.mobilenet_v1_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='Conv2d_13_pointwise', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - conv_defs=self._conv_defs, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - - depth_fn = lambda d: max(int(d * self._depth_multiplier), self._min_depth) - with slim.arg_scope(self._conv_hyperparams_fn()): - with tf.variable_scope('fpn', reuse=self._reuse_weights): - feature_blocks = [ - 'Conv2d_3_pointwise', 'Conv2d_5_pointwise', 'Conv2d_11_pointwise', - 'Conv2d_13_pointwise' - ] - base_fpn_max_level = min(self._fpn_max_level, 5) - feature_block_list = [] - for level in range(self._fpn_min_level, base_fpn_max_level + 1): - feature_block_list.append(feature_blocks[level - 2]) - fpn_features = feature_map_generators.fpn_top_down_feature_maps( - [(key, image_features[key]) for key in feature_block_list], - depth=depth_fn(self._additional_layer_depth), - use_depthwise=self._use_depthwise, - use_explicit_padding=self._use_explicit_padding, - use_native_resize_op=self._use_native_resize_op) - feature_maps = [] - for level in range(self._fpn_min_level, base_fpn_max_level + 1): - feature_maps.append(fpn_features['top_down_{}'.format( - feature_blocks[level - 2])]) - last_feature_map = fpn_features['top_down_{}'.format( - feature_blocks[base_fpn_max_level - 2])] - # Construct coarse features - padding = 'VALID' if self._use_explicit_padding else 'SAME' - kernel_size = 3 - for i in range(base_fpn_max_level + 1, self._fpn_max_level + 1): - if self._use_depthwise: - conv_op = functools.partial( - slim.separable_conv2d, depth_multiplier=1) - else: - conv_op = slim.conv2d - if self._use_explicit_padding: - last_feature_map = ops.fixed_padding( - last_feature_map, kernel_size) - last_feature_map = conv_op( - last_feature_map, - num_outputs=depth_fn(self._additional_layer_depth), - kernel_size=[kernel_size, kernel_size], - stride=2, - padding=padding, - scope='bottom_up_Conv2d_{}'.format(i - base_fpn_max_level + 13)) - feature_maps.append(last_feature_map) - return feature_maps diff --git a/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor_tf1_test.py deleted file mode 100644 index 449b7803d39..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor_tf1_test.py +++ /dev/null @@ -1,206 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_mobilenet_v1_fpn_feature_extractor. - -By using parameterized test decorator, this test serves for both Slim-based and -Keras-based Mobilenet V1 FPN feature extractors in SSD. -""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v1_fpn_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetV1FpnFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - is_training=True, use_explicit_padding=False, - use_keras=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - is_training: whether the network is in training mode. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - del use_keras - min_depth = 32 - return (ssd_mobilenet_v1_fpn_feature_extractor. - SSDMobileNetV1FpnFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_depthwise=True, - use_explicit_padding=use_explicit_padding)) - - def test_extract_features_returns_correct_shapes_256(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=False) - - def test_extract_features_returns_correct_shapes_384(self): - image_height = 320 - image_width = 320 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=False) - - def test_extract_features_with_dynamic_image_shape(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=False) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=False) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 256 - image_width = 256 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 32), (2, 16, 16, 32), - (2, 8, 8, 32), (2, 4, 4, 32), - (2, 2, 2, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=False) - - def test_extract_features_raises_error_with_invalid_image_size( - self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple, - use_keras=False) - - def test_preprocess_returns_correct_value_range(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple, - use_keras=False) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'MobilenetV1' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name, use_keras=False) - - def test_variable_count(self): - depth_multiplier = 1 - pad_to_multiple = 1 - variables = self.get_feature_extractor_variables( - depth_multiplier, pad_to_multiple, use_keras=False) - self.assertEqual(len(variables), 153) - - def test_fused_batchnorm(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple, - use_keras=False) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image) - - self.assertTrue( - any('FusedBatchNorm' in op.type - for op in tf.get_default_graph().get_operations())) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor_tf2_test.py b/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor_tf2_test.py deleted file mode 100644 index 307cfa8b0b5..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_fpn_feature_extractor_tf2_test.py +++ /dev/null @@ -1,179 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_mobilenet_v1_fpn_feature_extractor. - -By using parameterized test decorator, this test serves for both Slim-based and -Keras-based Mobilenet V1 FPN feature extractors in SSD. -""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v1_fpn_keras_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class SsdMobilenetV1FpnFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - is_training=True, use_explicit_padding=False, - use_keras=True): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - is_training: whether the network is in training mode. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - del use_keras - return (ssd_mobilenet_v1_fpn_keras_feature_extractor. - SSDMobileNetV1FpnKerasFeatureExtractor( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams( - add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - use_explicit_padding=use_explicit_padding, - use_depthwise=True, - name='MobilenetV1_FPN')) - - def test_extract_features_returns_correct_shapes_256(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=True) - - def test_extract_features_returns_correct_shapes_384(self): - image_height = 320 - image_width = 320 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=True) - - def test_extract_features_with_dynamic_image_shape(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=True) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=True) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 256 - image_width = 256 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 32), (2, 16, 16, 32), - (2, 8, 8, 32), (2, 4, 4, 32), - (2, 2, 2, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, - use_keras=True) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, - use_keras=True) - - def test_extract_features_raises_error_with_invalid_image_size( - self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple, - use_keras=True) - - def test_preprocess_returns_correct_value_range(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple, - use_keras=True) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v1_fpn_keras_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v1_fpn_keras_feature_extractor.py deleted file mode 100644 index cb88c265e31..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_fpn_keras_feature_extractor.py +++ /dev/null @@ -1,255 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSD Keras-based MobilenetV1 FPN Feature Extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.models.keras_models import mobilenet_v1 -from object_detection.models.keras_models import model_utils -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -# A modified config of mobilenet v1 that makes it more detection friendly. -def _create_modified_mobilenet_config(): - conv_def_block_12 = model_utils.ConvDefs(conv_name='conv_pw_12', filters=512) - conv_def_block_13 = model_utils.ConvDefs(conv_name='conv_pw_13', filters=256) - return [conv_def_block_12, conv_def_block_13] - - -class SSDMobileNetV1FpnKerasFeatureExtractor( - ssd_meta_arch.SSDKerasFeatureExtractor): - """SSD Feature Extractor using Keras-based MobilenetV1 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False, - name=None): - """SSD Keras based FPN feature extractor Mobilenet v1 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to MobileNet v1 layers - {Conv2d_3_pointwise, Conv2d_5_pointwise, Conv2d_11_pointwise, - Conv2d_13_pointwise}, respectively. - fpn_max_level: the smallest resolution feature map to construct or use in - FPN. FPN constructions uses features maps starting from fpn_min_level - upto the fpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of fpn - levels. - additional_layer_depth: additional feature map layer channel depth. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: whether to use depthwise convolutions. Default is False. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDMobileNetV1FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._additional_layer_depth = additional_layer_depth - self._conv_defs = None - if self._use_depthwise: - self._conv_defs = _create_modified_mobilenet_config() - self._use_native_resize_op = use_native_resize_op - self._feature_blocks = [ - 'Conv2d_3_pointwise', 'Conv2d_5_pointwise', 'Conv2d_11_pointwise', - 'Conv2d_13_pointwise' - ] - self.classification_backbone = None - self._fpn_features_generator = None - self._coarse_feature_layers = [] - - def build(self, input_shape): - full_mobilenet_v1 = mobilenet_v1.mobilenet_v1( - batchnorm_training=(self._is_training and not self._freeze_batchnorm), - conv_hyperparams=(self._conv_hyperparams - if self._override_base_feature_extractor_hyperparams - else None), - weights=None, - use_explicit_padding=self._use_explicit_padding, - alpha=self._depth_multiplier, - min_depth=self._min_depth, - conv_defs=self._conv_defs, - include_top=False) - conv2d_3_pointwise = full_mobilenet_v1.get_layer( - name='conv_pw_3_relu').output - conv2d_5_pointwise = full_mobilenet_v1.get_layer( - name='conv_pw_5_relu').output - conv2d_11_pointwise = full_mobilenet_v1.get_layer( - name='conv_pw_11_relu').output - conv2d_13_pointwise = full_mobilenet_v1.get_layer( - name='conv_pw_13_relu').output - self.classification_backbone = tf.keras.Model( - inputs=full_mobilenet_v1.inputs, - outputs=[conv2d_3_pointwise, conv2d_5_pointwise, - conv2d_11_pointwise, conv2d_13_pointwise] - ) - # pylint:disable=g-long-lambda - self._depth_fn = lambda d: max( - int(d * self._depth_multiplier), self._min_depth) - self._base_fpn_max_level = min(self._fpn_max_level, 5) - self._num_levels = self._base_fpn_max_level + 1 - self._fpn_min_level - self._fpn_features_generator = ( - feature_map_generators.KerasFpnTopDownFeatureMaps( - num_levels=self._num_levels, - depth=self._depth_fn(self._additional_layer_depth), - use_depthwise=self._use_depthwise, - use_explicit_padding=self._use_explicit_padding, - use_native_resize_op=self._use_native_resize_op, - is_training=self._is_training, - conv_hyperparams=self._conv_hyperparams, - freeze_batchnorm=self._freeze_batchnorm, - name='FeatureMaps')) - # Construct coarse feature layers - padding = 'VALID' if self._use_explicit_padding else 'SAME' - kernel_size = 3 - stride = 2 - for i in range(self._base_fpn_max_level + 1, self._fpn_max_level + 1): - coarse_feature_layers = [] - if self._use_explicit_padding: - def fixed_padding(features, kernel_size=kernel_size): - return ops.fixed_padding(features, kernel_size) - coarse_feature_layers.append(tf.keras.layers.Lambda( - fixed_padding, name='fixed_padding')) - layer_name = 'bottom_up_Conv2d_{}'.format( - i - self._base_fpn_max_level + 13) - conv_block = feature_map_generators.create_conv_block( - self._use_depthwise, kernel_size, padding, stride, layer_name, - self._conv_hyperparams, self._is_training, self._freeze_batchnorm, - self._depth_fn(self._additional_layer_depth)) - coarse_feature_layers.extend(conv_block) - self._coarse_feature_layers.append(coarse_feature_layers) - self.built = True - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - image_features = self.classification_backbone( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple)) - - feature_block_list = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_block_list.append(self._feature_blocks[level - 2]) - - feature_start_index = len(self._feature_blocks) - self._num_levels - fpn_input_image_features = [ - (key, image_features[feature_start_index + index]) - for index, key in enumerate(feature_block_list)] - fpn_features = self._fpn_features_generator(fpn_input_image_features) - - feature_maps = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_maps.append(fpn_features['top_down_{}'.format( - self._feature_blocks[level - 2])]) - last_feature_map = fpn_features['top_down_{}'.format( - self._feature_blocks[self._base_fpn_max_level - 2])] - - for coarse_feature_layers in self._coarse_feature_layers: - for layer in coarse_feature_layers: - last_feature_map = layer(last_feature_map) - feature_maps.append(last_feature_map) - return feature_maps - - def restore_from_classification_checkpoint_fn(self, feature_extractor_scope): - """Returns a map for restoring from an (object-based) checkpoint. - - Args: - feature_extractor_scope: A scope name for the feature extractor (unused). - - Returns: - A dict mapping keys to Keras models - """ - return {'feature_extractor': self.classification_backbone} diff --git a/research/object_detection/models/ssd_mobilenet_v1_keras_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v1_keras_feature_extractor.py deleted file mode 100644 index 01367e2a8fc..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_keras_feature_extractor.py +++ /dev/null @@ -1,164 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for Keras MobilenetV1 features.""" - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.models.keras_models import mobilenet_v1 -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -class SSDMobileNetV1KerasFeatureExtractor( - ssd_meta_arch.SSDKerasFeatureExtractor): - """SSD Feature Extractor using Keras MobilenetV1 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False, - name=None): - """Keras MobileNetV1 Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: A string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDMobileNetV1KerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - num_layers=num_layers, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - self._feature_map_layout = { - 'from_layer': ['Conv2d_11_pointwise', 'Conv2d_13_pointwise', '', '', - '', ''][:self._num_layers], - 'layer_depth': [-1, -1, 512, 256, 256, 128][:self._num_layers], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - self.classification_backbone = None - self._feature_map_generator = None - - def build(self, input_shape): - full_mobilenet_v1 = mobilenet_v1.mobilenet_v1( - batchnorm_training=(self._is_training and not self._freeze_batchnorm), - conv_hyperparams=(self._conv_hyperparams - if self._override_base_feature_extractor_hyperparams - else None), - weights=None, - use_explicit_padding=self._use_explicit_padding, - alpha=self._depth_multiplier, - min_depth=self._min_depth, - include_top=False) - conv2d_11_pointwise = full_mobilenet_v1.get_layer( - name='conv_pw_11_relu').output - conv2d_13_pointwise = full_mobilenet_v1.get_layer( - name='conv_pw_13_relu').output - self.classification_backbone = tf.keras.Model( - inputs=full_mobilenet_v1.inputs, - outputs=[conv2d_11_pointwise, conv2d_13_pointwise]) - self._feature_map_generator = ( - feature_map_generators.KerasMultiResolutionFeatureMaps( - feature_map_layout=self._feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - is_training=self._is_training, - conv_hyperparams=self._conv_hyperparams, - freeze_batchnorm=self._freeze_batchnorm, - name='FeatureMaps')) - self.built = True - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - image_features = self.classification_backbone( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple)) - - feature_maps = self._feature_map_generator({ - 'Conv2d_11_pointwise': image_features[0], - 'Conv2d_13_pointwise': image_features[1]}) - - return list(feature_maps.values()) diff --git a/research/object_detection/models/ssd_mobilenet_v1_ppn_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v1_ppn_feature_extractor.py deleted file mode 100644 index 91e14d4ad68..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_ppn_feature_extractor.py +++ /dev/null @@ -1,83 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for MobilenetV1 PPN features.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import mobilenet_v1 - - -class SSDMobileNetV1PpnFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using MobilenetV1 PPN features.""" - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - with tf.variable_scope('MobilenetV1', - reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v1.mobilenet_v1_arg_scope( - is_training=None, regularize_depthwise=True)): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams - else context_manager.IdentityContextManager()): - _, image_features = mobilenet_v1.mobilenet_v1_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='Conv2d_13_pointwise', - min_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.pooling_pyramid_feature_maps( - base_feature_map_depth=0, - num_layers=6, - image_features={ - 'image_features': image_features['Conv2d_11_pointwise'] - }) - return list(feature_maps.values()) diff --git a/research/object_detection/models/ssd_mobilenet_v1_ppn_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_v1_ppn_feature_extractor_tf1_test.py deleted file mode 100644 index b5918c0dfa9..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v1_ppn_feature_extractor_tf1_test.py +++ /dev/null @@ -1,186 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_mobilenet_v1_ppn_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v1_ppn_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetV1PpnFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - is_training=True, use_explicit_padding=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - is_training: whether the network is in training mode. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - return (ssd_mobilenet_v1_ppn_feature_extractor. - SSDMobileNetV1PpnFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - def test_extract_features_returns_correct_shapes_320(self): - image_height = 320 - image_width = 320 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 20, 20, 512), (2, 10, 10, 512), - (2, 5, 5, 512), (2, 3, 3, 512), - (2, 2, 2, 512), (2, 1, 1, 512)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_returns_correct_shapes_300(self): - image_height = 300 - image_width = 300 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 512), (2, 10, 10, 512), - (2, 5, 5, 512), (2, 3, 3, 512), - (2, 2, 2, 512), (2, 1, 1, 512)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_returns_correct_shapes_640(self): - image_height = 640 - image_width = 640 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 40, 512), (2, 20, 20, 512), - (2, 10, 10, 512), (2, 5, 5, 512), - (2, 3, 3, 512), (2, 2, 2, 512)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_with_dynamic_image_shape(self): - image_height = 320 - image_width = 320 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 20, 20, 512), (2, 10, 10, 512), - (2, 5, 5, 512), (2, 3, 3, 512), - (2, 2, 2, 512), (2, 1, 1, 512)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 20, 20, 512), (2, 10, 10, 512), - (2, 5, 5, 512), (2, 3, 3, 512), - (2, 2, 2, 512)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth(self): - image_height = 256 - image_width = 256 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 16, 16, 32), (2, 8, 8, 32), - (2, 4, 4, 32), (2, 2, 2, 32), - (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_raises_error_with_invalid_image_size(self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'MobilenetV1' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name) - - def test_has_fused_batchnorm(self): - image_height = 320 - image_width = 320 - depth_multiplier = 1 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image) - self.assertTrue(any('FusedBatchNorm' in op.type - for op in tf.get_default_graph().get_operations())) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v2_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v2_feature_extractor.py deleted file mode 100644 index e12177a2172..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_feature_extractor.py +++ /dev/null @@ -1,139 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for MobilenetV2 features.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets.mobilenet import mobilenet -from nets.mobilenet import mobilenet_v2 - - -class SSDMobileNetV2FeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using MobilenetV2 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False): - """MobileNetV2 Feature Extractor for SSD Models. - - Mobilenet v2 (experimental), designed by sandler@. More details can be found - in //knowledge/cerebra/brain/compression/mobilenet/mobilenet_experimental.py - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDMobileNetV2FeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - num_layers=num_layers, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - feature_map_layout = { - 'from_layer': ['layer_15/expansion_output', 'layer_19', '', '', '', '' - ][:self._num_layers], - 'layer_depth': [-1, -1, 512, 256, 256, 128][:self._num_layers], - 'use_depthwise': self._use_depthwise, - 'use_explicit_padding': self._use_explicit_padding, - } - - with tf.variable_scope('MobilenetV2', reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v2.training_scope(is_training=None, bn_decay=0.9997)), \ - slim.arg_scope( - [mobilenet.depth_multiplier], min_depth=self._min_depth): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams else - context_manager.IdentityContextManager()): - _, image_features = mobilenet_v2.mobilenet_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='layer_19', - depth_multiplier=self._depth_multiplier, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) diff --git a/research/object_detection/models/ssd_mobilenet_v2_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_v2_feature_extractor_tf1_test.py deleted file mode 100644 index 96f9bc26e12..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_feature_extractor_tf1_test.py +++ /dev/null @@ -1,196 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_mobilenet_v2_feature_extractor.""" -import unittest - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v2_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetV2FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - return ssd_mobilenet_v2_feature_extractor.SSDMobileNetV2FeatureExtractor( - False, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding, - num_layers=num_layers) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_128_explicit_padding( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_returns_correct_shapes_with_dynamic_inputs( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 576), (2, 10, 10, 1280), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 192), (2, 10, 10, 32), - (2, 5, 5, 32), (2, 3, 3, 32), - (2, 2, 2, 32), (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 20, 20, 576), (2, 10, 10, 1280), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_raises_error_with_invalid_image_size( - self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'MobilenetV2' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name) - - def test_variable_count(self): - depth_multiplier = 1 - pad_to_multiple = 1 - variables = self.get_feature_extractor_variables( - depth_multiplier, pad_to_multiple) - self.assertEqual(len(variables), 292) - - def test_has_fused_batchnorm(self): - image_height = 40 - image_width = 40 - depth_multiplier = 1 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image) - self.assertTrue(any('FusedBatchNorm' in op.type - for op in tf.get_default_graph().get_operations())) - - def test_extract_features_with_fewer_layers(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, num_layers=4) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v2_feature_extractor_tf2_test.py b/research/object_detection/models/ssd_mobilenet_v2_feature_extractor_tf2_test.py deleted file mode 100644 index 6d4cb5afcf7..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_feature_extractor_tf2_test.py +++ /dev/null @@ -1,192 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_mobilenet_v2_feature_extractor.""" -import unittest - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v2_keras_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class SsdMobilenetV2FeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6, - use_keras=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - use_keras: unused argument. - - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - del use_keras - min_depth = 32 - return (ssd_mobilenet_v2_keras_feature_extractor. - SSDMobileNetV2KerasFeatureExtractor( - is_training=False, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - use_explicit_padding=use_explicit_padding, - num_layers=num_layers, - name='MobilenetV2')) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=True) - - def test_extract_features_returns_correct_shapes_128_explicit_padding( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True, use_keras=True) - - def test_extract_features_returns_correct_shapes_with_dynamic_inputs( - self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=True) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 576), (2, 10, 10, 1280), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=True) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 192), (2, 10, 10, 32), - (2, 5, 5, 32), (2, 3, 3, 32), - (2, 2, 2, 32), (2, 1, 1, 32)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=True) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 20, 20, 576), (2, 10, 10, 1280), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=True) - - def test_extract_features_raises_error_with_invalid_image_size( - self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple, - use_keras=True) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'MobilenetV2' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, scope_name, use_keras=True) - - def test_variable_count(self): - depth_multiplier = 1 - pad_to_multiple = 1 - variables = self.get_feature_extractor_variables( - depth_multiplier, pad_to_multiple, use_keras=True) - self.assertEqual(len(variables), 292) - - def test_extract_features_with_fewer_layers(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 576), (2, 4, 4, 1280), - (2, 2, 2, 512), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False, num_layers=4, - use_keras=True) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor.py deleted file mode 100644 index bb5ea66ff7e..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor.py +++ /dev/null @@ -1,198 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSD MobilenetV2 FPN Feature Extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import copy -import functools -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets.mobilenet import mobilenet -from nets.mobilenet import mobilenet_v2 - - -# A modified config of mobilenet v2 that makes it more detection friendly. -def _create_modified_mobilenet_config(): - conv_defs = copy.deepcopy(mobilenet_v2.V2_DEF) - conv_defs['spec'][-1] = mobilenet.op( - slim.conv2d, stride=1, kernel_size=[1, 1], num_outputs=256) - return conv_defs - - -class SSDMobileNetV2FpnFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using MobilenetV2 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False): - """SSD FPN feature extractor based on Mobilenet v2 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the base - feature extractor. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to MobileNet v2 layers - {layer_4, layer_7, layer_14, layer_19}, respectively. - fpn_max_level: the smallest resolution feature map to construct or use in - FPN. FPN constructions uses features maps starting from fpn_min_level - upto the fpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of fpn - levels. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDMobileNetV2FpnFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._additional_layer_depth = additional_layer_depth - self._conv_defs = None - if self._use_depthwise: - self._conv_defs = _create_modified_mobilenet_config() - self._use_native_resize_op = use_native_resize_op - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - with tf.variable_scope('MobilenetV2', reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v2.training_scope(is_training=None, bn_decay=0.9997)), \ - slim.arg_scope( - [mobilenet.depth_multiplier], min_depth=self._min_depth): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams else - context_manager.IdentityContextManager()): - _, image_features = mobilenet_v2.mobilenet_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='layer_19', - depth_multiplier=self._depth_multiplier, - conv_defs=self._conv_defs, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - depth_fn = lambda d: max(int(d * self._depth_multiplier), self._min_depth) - with slim.arg_scope(self._conv_hyperparams_fn()): - with tf.variable_scope('fpn', reuse=self._reuse_weights): - feature_blocks = [ - 'layer_4', 'layer_7', 'layer_14', 'layer_19' - ] - base_fpn_max_level = min(self._fpn_max_level, 5) - feature_block_list = [] - for level in range(self._fpn_min_level, base_fpn_max_level + 1): - feature_block_list.append(feature_blocks[level - 2]) - fpn_features = feature_map_generators.fpn_top_down_feature_maps( - [(key, image_features[key]) for key in feature_block_list], - depth=depth_fn(self._additional_layer_depth), - use_depthwise=self._use_depthwise, - use_explicit_padding=self._use_explicit_padding, - use_native_resize_op=self._use_native_resize_op) - feature_maps = [] - for level in range(self._fpn_min_level, base_fpn_max_level + 1): - feature_maps.append(fpn_features['top_down_{}'.format( - feature_blocks[level - 2])]) - last_feature_map = fpn_features['top_down_{}'.format( - feature_blocks[base_fpn_max_level - 2])] - # Construct coarse features - padding = 'VALID' if self._use_explicit_padding else 'SAME' - kernel_size = 3 - for i in range(base_fpn_max_level + 1, self._fpn_max_level + 1): - if self._use_depthwise: - conv_op = functools.partial( - slim.separable_conv2d, depth_multiplier=1) - else: - conv_op = slim.conv2d - if self._use_explicit_padding: - last_feature_map = ops.fixed_padding( - last_feature_map, kernel_size) - last_feature_map = conv_op( - last_feature_map, - num_outputs=depth_fn(self._additional_layer_depth), - kernel_size=[kernel_size, kernel_size], - stride=2, - padding=padding, - scope='bottom_up_Conv2d_{}'.format(i - base_fpn_max_level + 19)) - feature_maps.append(last_feature_map) - return feature_maps diff --git a/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor_tf1_test.py deleted file mode 100644 index 0605911f2ba..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor_tf1_test.py +++ /dev/null @@ -1,406 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_mobilenet_v2_fpn_feature_extractor. - -By using parameterized test decorator, this test serves for both Slim-based and -Keras-based Mobilenet V2 FPN feature extractors in SSD. -""" -import unittest -from absl.testing import parameterized -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v2_fpn_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -@parameterized.parameters( - { - 'use_depthwise': False - }, - { - 'use_depthwise': True - }, -) -class SsdMobilenetV2FpnFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - is_training=True, - use_explicit_padding=False, - use_keras=False, - use_depthwise=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - is_training: whether the network is in training mode. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - use_depthwise: Whether to use depthwise convolutions. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - del use_keras - min_depth = 32 - return (ssd_mobilenet_v2_fpn_feature_extractor - .SSDMobileNetV2FpnFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_depthwise=use_depthwise, - use_explicit_padding=use_explicit_padding)) - - def test_extract_features_returns_correct_shapes_256(self, use_depthwise): - use_keras = False - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_384(self, use_depthwise): - use_keras = False - image_height = 320 - image_width = 320 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_4_channels(self, - use_depthwise): - use_keras = False - image_height = 320 - image_width = 320 - num_channels = 4 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise, - num_channels=num_channels) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise, - num_channels=num_channels) - - def test_extract_features_with_dynamic_image_shape(self, - use_depthwise): - use_keras = False - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self, use_depthwise): - use_keras = False - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self, use_depthwise): - use_keras = False - image_height = 256 - image_width = 256 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 32), (2, 16, 16, 32), - (2, 8, 8, 32), (2, 4, 4, 32), - (2, 2, 2, 32)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_raises_error_with_invalid_image_size( - self, use_depthwise): - use_keras = False - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_preprocess_returns_correct_value_range(self, - use_depthwise): - use_keras = False - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_variables_only_created_in_scope(self, use_depthwise): - use_keras = False - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = 'MobilenetV2' - self.check_feature_extractor_variables_under_scope( - depth_multiplier, - pad_to_multiple, - scope_name, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_fused_batchnorm(self, use_depthwise): - use_keras = False - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image) - self.assertTrue( - any('FusedBatchNorm' in op.type - for op in tf.get_default_graph().get_operations())) - - def test_variable_count(self, use_depthwise): - use_keras = False - depth_multiplier = 1 - pad_to_multiple = 1 - variables = self.get_feature_extractor_variables( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - expected_variables_len = 274 - if use_depthwise: - expected_variables_len = 278 - self.assertEqual(len(variables), expected_variables_len) - - def test_get_expected_feature_map_variable_names(self, - use_depthwise): - use_keras = False - depth_multiplier = 1.0 - pad_to_multiple = 1 - - slim_expected_feature_maps_variables = set([ - # Slim Mobilenet V2 feature maps - 'MobilenetV2/expanded_conv_4/depthwise/depthwise_weights', - 'MobilenetV2/expanded_conv_7/depthwise/depthwise_weights', - 'MobilenetV2/expanded_conv_14/depthwise/depthwise_weights', - 'MobilenetV2/Conv_1/weights', - # FPN layers - 'MobilenetV2/fpn/bottom_up_Conv2d_20/weights', - 'MobilenetV2/fpn/bottom_up_Conv2d_21/weights', - 'MobilenetV2/fpn/smoothing_1/weights', - 'MobilenetV2/fpn/smoothing_2/weights', - 'MobilenetV2/fpn/projection_1/weights', - 'MobilenetV2/fpn/projection_2/weights', - 'MobilenetV2/fpn/projection_3/weights', - ]) - slim_expected_feature_maps_variables_with_depthwise = set([ - # Slim Mobilenet V2 feature maps - 'MobilenetV2/expanded_conv_4/depthwise/depthwise_weights', - 'MobilenetV2/expanded_conv_7/depthwise/depthwise_weights', - 'MobilenetV2/expanded_conv_14/depthwise/depthwise_weights', - 'MobilenetV2/Conv_1/weights', - # FPN layers - 'MobilenetV2/fpn/bottom_up_Conv2d_20/pointwise_weights', - 'MobilenetV2/fpn/bottom_up_Conv2d_20/depthwise_weights', - 'MobilenetV2/fpn/bottom_up_Conv2d_21/pointwise_weights', - 'MobilenetV2/fpn/bottom_up_Conv2d_21/depthwise_weights', - 'MobilenetV2/fpn/smoothing_1/depthwise_weights', - 'MobilenetV2/fpn/smoothing_1/pointwise_weights', - 'MobilenetV2/fpn/smoothing_2/depthwise_weights', - 'MobilenetV2/fpn/smoothing_2/pointwise_weights', - 'MobilenetV2/fpn/projection_1/weights', - 'MobilenetV2/fpn/projection_2/weights', - 'MobilenetV2/fpn/projection_3/weights', - ]) - - g = tf.Graph() - with g.as_default(): - preprocessed_inputs = tf.placeholder(tf.float32, (4, None, None, 3)) - feature_extractor = self._create_feature_extractor( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - - _ = feature_extractor.extract_features(preprocessed_inputs) - expected_feature_maps_variables = slim_expected_feature_maps_variables - if use_depthwise: - expected_feature_maps_variables = ( - slim_expected_feature_maps_variables_with_depthwise) - actual_variable_set = set([ - var.op.name for var in g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - ]) - variable_intersection = expected_feature_maps_variables.intersection( - actual_variable_set) - self.assertSetEqual(expected_feature_maps_variables, - variable_intersection) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor_tf2_test.py b/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor_tf2_test.py deleted file mode 100644 index c2dc71c8663..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_fpn_feature_extractor_tf2_test.py +++ /dev/null @@ -1,303 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_mobilenet_v2_fpn_feature_extractor. - -By using parameterized test decorator, this test serves for both Slim-based and -Keras-based Mobilenet V2 FPN feature extractors in SSD. -""" -import unittest -from absl.testing import parameterized -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v2_fpn_keras_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -@parameterized.parameters( - { - 'use_depthwise': False, - }, - { - 'use_depthwise': True, - }, -) -class SsdMobilenetV2FpnFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - is_training=True, - use_explicit_padding=False, - use_keras=False, - use_depthwise=False): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - is_training: whether the network is in training mode. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - use_depthwise: Whether to use depthwise convolutions. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - del use_keras - min_depth = 32 - return (ssd_mobilenet_v2_fpn_keras_feature_extractor - .SSDMobileNetV2FpnKerasFeatureExtractor( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams( - add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - name='MobilenetV2_FPN')) - - def test_extract_features_returns_correct_shapes_256(self, - use_depthwise): - use_keras = True - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_384(self, - use_depthwise): - use_keras = True - image_height = 320 - image_width = 320 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_4_channels(self, - use_depthwise): - use_keras = True - image_height = 320 - image_width = 320 - num_channels = 4 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise, - num_channels=num_channels) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise, - num_channels=num_channels) - - def test_extract_features_with_dynamic_image_shape(self, - use_depthwise): - use_keras = True - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self, use_depthwise): - use_keras = True - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 40, 40, 256), (2, 20, 20, 256), - (2, 10, 10, 256), (2, 5, 5, 256), - (2, 3, 3, 256)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth( - self, use_depthwise): - use_keras = True - image_height = 256 - image_width = 256 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 32), (2, 16, 16, 32), - (2, 8, 8, 32), (2, 4, 4, 32), - (2, 2, 2, 32)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=False, - use_keras=use_keras, - use_depthwise=use_depthwise) - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_explicit_padding=True, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_extract_features_raises_error_with_invalid_image_size( - self, use_depthwise=False): - use_keras = True - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - - def test_preprocess_returns_correct_value_range(self, - use_depthwise): - use_keras = True - image_height = 256 - image_width = 256 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor( - depth_multiplier, - pad_to_multiple, - use_keras=use_keras, - use_depthwise=use_depthwise) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v2_fpn_keras_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v2_fpn_keras_feature_extractor.py deleted file mode 100644 index c18319c5384..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_fpn_keras_feature_extractor.py +++ /dev/null @@ -1,243 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSD Keras-based MobilenetV2 FPN Feature Extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.models.keras_models import mobilenet_v2 -from object_detection.models.keras_models import model_utils -from object_detection.utils import ops -from object_detection.utils import shape_utils - -# Total number of blocks in Mobilenet_V2 base network. -NUM_LAYERS = 19 - - -# A modified config of mobilenet v2 that makes it more detection friendly. -def _create_modified_mobilenet_config(): - last_conv = model_utils.ConvDefs(conv_name='Conv_1', filters=256) - return [last_conv] - - -class SSDMobileNetV2FpnKerasFeatureExtractor( - ssd_meta_arch.SSDKerasFeatureExtractor): - """SSD Feature Extractor using Keras-based MobilenetV2 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False, - name=None): - """SSD Keras based FPN feature extractor Mobilenet v2 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to MobileNet v2 layers - {layer_4, layer_7, layer_14, layer_19}, respectively. - fpn_max_level: the smallest resolution feature map to construct or use in - FPN. FPN constructions uses features maps starting from fpn_min_level - upto the fpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of fpn - levels. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDMobileNetV2FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._additional_layer_depth = additional_layer_depth - self._conv_defs = None - if self._use_depthwise: - self._conv_defs = _create_modified_mobilenet_config() - self._use_native_resize_op = use_native_resize_op - self._feature_blocks = ['layer_4', 'layer_7', 'layer_14', 'layer_19'] - self.classification_backbone = None - self._fpn_features_generator = None - self._coarse_feature_layers = [] - - def build(self, input_shape): - full_mobilenet_v2 = mobilenet_v2.mobilenet_v2( - batchnorm_training=(self._is_training and not self._freeze_batchnorm), - conv_hyperparams=(self._conv_hyperparams - if self._override_base_feature_extractor_hyperparams - else None), - weights=None, - use_explicit_padding=self._use_explicit_padding, - alpha=self._depth_multiplier, - min_depth=self._min_depth, - include_top=False, - input_shape=(None, None, input_shape[-1])) - layer_names = [layer.name for layer in full_mobilenet_v2.layers] - outputs = [] - for layer_idx in [4, 7, 14]: - add_name = 'block_{}_add'.format(layer_idx - 2) - project_name = 'block_{}_project_BN'.format(layer_idx - 2) - output_layer_name = add_name if add_name in layer_names else project_name - outputs.append(full_mobilenet_v2.get_layer(output_layer_name).output) - layer_19 = full_mobilenet_v2.get_layer(name='out_relu').output - outputs.append(layer_19) - self.classification_backbone = tf.keras.Model( - inputs=full_mobilenet_v2.inputs, - outputs=outputs) - # pylint:disable=g-long-lambda - self._depth_fn = lambda d: max( - int(d * self._depth_multiplier), self._min_depth) - self._base_fpn_max_level = min(self._fpn_max_level, 5) - self._num_levels = self._base_fpn_max_level + 1 - self._fpn_min_level - self._fpn_features_generator = ( - feature_map_generators.KerasFpnTopDownFeatureMaps( - num_levels=self._num_levels, - depth=self._depth_fn(self._additional_layer_depth), - use_depthwise=self._use_depthwise, - use_explicit_padding=self._use_explicit_padding, - use_native_resize_op=self._use_native_resize_op, - is_training=self._is_training, - conv_hyperparams=self._conv_hyperparams, - freeze_batchnorm=self._freeze_batchnorm, - name='FeatureMaps')) - # Construct coarse feature layers - padding = 'VALID' if self._use_explicit_padding else 'SAME' - kernel_size = 3 - stride = 2 - for i in range(self._base_fpn_max_level + 1, self._fpn_max_level + 1): - coarse_feature_layers = [] - if self._use_explicit_padding: - def fixed_padding(features, kernel_size=kernel_size): - return ops.fixed_padding(features, kernel_size) - coarse_feature_layers.append(tf.keras.layers.Lambda( - fixed_padding, name='fixed_padding')) - layer_name = 'bottom_up_Conv2d_{}'.format( - i - self._base_fpn_max_level + NUM_LAYERS) - conv_block = feature_map_generators.create_conv_block( - self._use_depthwise, kernel_size, padding, stride, layer_name, - self._conv_hyperparams, self._is_training, self._freeze_batchnorm, - self._depth_fn(self._additional_layer_depth)) - coarse_feature_layers.extend(conv_block) - self._coarse_feature_layers.append(coarse_feature_layers) - self.built = True - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - image_features = self.classification_backbone( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple)) - - feature_block_list = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_block_list.append(self._feature_blocks[level - 2]) - - feature_start_index = len(self._feature_blocks) - self._num_levels - fpn_input_image_features = [ - (key, image_features[feature_start_index + index]) - for index, key in enumerate(feature_block_list)] - fpn_features = self._fpn_features_generator(fpn_input_image_features) - - feature_maps = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_maps.append(fpn_features['top_down_{}'.format( - self._feature_blocks[level - 2])]) - last_feature_map = fpn_features['top_down_{}'.format( - self._feature_blocks[self._base_fpn_max_level - 2])] - - for coarse_feature_layers in self._coarse_feature_layers: - for layer in coarse_feature_layers: - last_feature_map = layer(last_feature_map) - feature_maps.append(last_feature_map) - return feature_maps diff --git a/research/object_detection/models/ssd_mobilenet_v2_keras_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v2_keras_feature_extractor.py deleted file mode 100644 index 06a62ffa3dc..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_keras_feature_extractor.py +++ /dev/null @@ -1,167 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for MobilenetV2 features.""" - -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.models.keras_models import mobilenet_v2 -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -class SSDMobileNetV2KerasFeatureExtractor( - ssd_meta_arch.SSDKerasFeatureExtractor): - """SSD Feature Extractor using MobilenetV2 features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False, - name=None): - """MobileNetV2 Feature Extractor for SSD Models. - - Mobilenet v2 (experimental), designed by sandler@. More details can be found - in //knowledge/cerebra/brain/compression/mobilenet/mobilenet_experimental.py - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor (Functions - as a width multiplier for the mobilenet_v2 network itself). - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - name: A string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDMobileNetV2KerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - num_layers=num_layers, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - self._feature_map_layout = { - 'from_layer': ['layer_15/expansion_output', 'layer_19', '', '', '', '' - ][:self._num_layers], - 'layer_depth': [-1, -1, 512, 256, 256, 128][:self._num_layers], - 'use_depthwise': self._use_depthwise, - 'use_explicit_padding': self._use_explicit_padding, - } - - self.classification_backbone = None - self.feature_map_generator = None - - def build(self, input_shape): - full_mobilenet_v2 = mobilenet_v2.mobilenet_v2( - batchnorm_training=(self._is_training and not self._freeze_batchnorm), - conv_hyperparams=(self._conv_hyperparams - if self._override_base_feature_extractor_hyperparams - else None), - weights=None, - use_explicit_padding=self._use_explicit_padding, - alpha=self._depth_multiplier, - min_depth=self._min_depth, - include_top=False) - conv2d_11_pointwise = full_mobilenet_v2.get_layer( - name='block_13_expand_relu').output - conv2d_13_pointwise = full_mobilenet_v2.get_layer(name='out_relu').output - self.classification_backbone = tf.keras.Model( - inputs=full_mobilenet_v2.inputs, - outputs=[conv2d_11_pointwise, conv2d_13_pointwise]) - self.feature_map_generator = ( - feature_map_generators.KerasMultiResolutionFeatureMaps( - feature_map_layout=self._feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - is_training=self._is_training, - conv_hyperparams=self._conv_hyperparams, - freeze_batchnorm=self._freeze_batchnorm, - name='FeatureMaps')) - self.built = True - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - image_features = self.classification_backbone( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple)) - - feature_maps = self.feature_map_generator({ - 'layer_15/expansion_output': image_features[0], - 'layer_19': image_features[1]}) - - return list(feature_maps.values()) diff --git a/research/object_detection/models/ssd_mobilenet_v2_mnasfpn_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v2_mnasfpn_feature_extractor.py deleted file mode 100644 index 2a948edf4fd..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_mnasfpn_feature_extractor.py +++ /dev/null @@ -1,411 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSD MobilenetV2 NAS-FPN Feature Extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections -import functools -from six.moves import range -import tensorflow.compat.v1 as tf - -import tf_slim as slim -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets.mobilenet import mobilenet -from nets.mobilenet import mobilenet_v2 - - -Block = collections.namedtuple( - 'Block', ['inputs', 'output_level', 'kernel_size', 'expansion_size']) - -_MNASFPN_CELL_CONFIG = [ - Block(inputs=(1, 2), output_level=4, kernel_size=3, expansion_size=256), - Block(inputs=(0, 4), output_level=3, kernel_size=3, expansion_size=128), - Block(inputs=(5, 4), output_level=4, kernel_size=3, expansion_size=128), - Block(inputs=(4, 3), output_level=5, kernel_size=5, expansion_size=128), - Block(inputs=(4, 3), output_level=6, kernel_size=3, expansion_size=96), -] - -MNASFPN_DEF = dict( - feature_levels=[3, 4, 5, 6], - spec=[_MNASFPN_CELL_CONFIG] * 4, -) - - -def _maybe_pad(feature, use_explicit_padding, kernel_size=3): - return ops.fixed_padding(feature, - kernel_size) if use_explicit_padding else feature - - -# Wrapper around mobilenet.depth_multiplier -def _apply_multiplier(d, multiplier, min_depth): - p = {'num_outputs': d} - mobilenet.depth_multiplier( - p, multiplier=multiplier, divisible_by=8, min_depth=min_depth) - return p['num_outputs'] - - -def _apply_size_dependent_ordering(input_feature, feature_level, block_level, - expansion_size, use_explicit_padding, - use_native_resize_op): - """Applies Size-Dependent-Ordering when resizing feature maps. - - See https://arxiv.org/abs/1912.01106 - - Args: - input_feature: input feature map to be resized. - feature_level: the level of the input feature. - block_level: the desired output level for the block. - expansion_size: the expansion size for the block. - use_explicit_padding: Whether to use explicit padding. - use_native_resize_op: Whether to use native resize op. - - Returns: - A transformed feature at the desired resolution and expansion size. - """ - padding = 'VALID' if use_explicit_padding else 'SAME' - if feature_level >= block_level: # Perform 1x1 then upsampling. - node = slim.conv2d( - input_feature, - expansion_size, [1, 1], - activation_fn=None, - normalizer_fn=slim.batch_norm, - padding=padding, - scope='Conv1x1') - if feature_level == block_level: - return node - scale = 2**(feature_level - block_level) - if use_native_resize_op: - input_shape = shape_utils.combined_static_and_dynamic_shape(node) - node = tf.image.resize_nearest_neighbor( - node, [input_shape[1] * scale, input_shape[2] * scale]) - else: - node = ops.nearest_neighbor_upsampling(node, scale=scale) - else: # Perform downsampling then 1x1. - stride = 2**(block_level - feature_level) - node = slim.max_pool2d( - _maybe_pad(input_feature, use_explicit_padding), [3, 3], - stride=[stride, stride], - padding=padding, - scope='Downsample') - node = slim.conv2d( - node, - expansion_size, [1, 1], - activation_fn=None, - normalizer_fn=slim.batch_norm, - padding=padding, - scope='Conv1x1') - return node - - -def _mnasfpn_cell(feature_maps, - feature_levels, - cell_spec, - output_channel=48, - use_explicit_padding=False, - use_native_resize_op=False, - multiplier_func=None): - """Create a MnasFPN cell. - - Args: - feature_maps: input feature maps. - feature_levels: levels of the feature maps. - cell_spec: A list of Block configs. - output_channel: Number of features for the input, output and intermediate - feature maps. - use_explicit_padding: Whether to use explicit padding. - use_native_resize_op: Whether to use native resize op. - multiplier_func: Depth-multiplier function. If None, use identity function. - - Returns: - A transformed list of feature maps at the same resolutions as the inputs. - """ - # This is the level where multipliers are realized. - if multiplier_func is None: - multiplier_func = lambda x: x - num_outputs = len(feature_maps) - cell_features = list(feature_maps) - cell_levels = list(feature_levels) - padding = 'VALID' if use_explicit_padding else 'SAME' - for bi, block in enumerate(cell_spec): - with tf.variable_scope('block_{}'.format(bi)): - block_level = block.output_level - intermediate_feature = None - for i, inp in enumerate(block.inputs): - with tf.variable_scope('input_{}'.format(i)): - input_level = cell_levels[inp] - node = _apply_size_dependent_ordering( - cell_features[inp], input_level, block_level, - multiplier_func(block.expansion_size), use_explicit_padding, - use_native_resize_op) - # Add features incrementally to avoid producing AddN, which doesn't - # play well with TfLite. - if intermediate_feature is None: - intermediate_feature = node - else: - intermediate_feature += node - node = tf.nn.relu6(intermediate_feature) - node = slim.separable_conv2d( - _maybe_pad(node, use_explicit_padding, block.kernel_size), - multiplier_func(output_channel), - block.kernel_size, - activation_fn=None, - normalizer_fn=slim.batch_norm, - padding=padding, - scope='SepConv') - cell_features.append(node) - cell_levels.append(block_level) - - # Cell-wide residuals. - out_idx = range(len(cell_features) - num_outputs, len(cell_features)) - for in_i, out_i in enumerate(out_idx): - if cell_features[out_i].shape.as_list( - ) == cell_features[in_i].shape.as_list(): - cell_features[out_i] += cell_features[in_i] - - return cell_features[-num_outputs:] - - -def mnasfpn(feature_maps, - head_def, - output_channel=48, - use_explicit_padding=False, - use_native_resize_op=False, - multiplier_func=None): - """Create the MnasFPN head given head_def.""" - features = feature_maps - for ci, cell_spec in enumerate(head_def['spec']): - with tf.variable_scope('cell_{}'.format(ci)): - features = _mnasfpn_cell(features, head_def['feature_levels'], cell_spec, - output_channel, use_explicit_padding, - use_native_resize_op, multiplier_func) - return features - - -def training_scope(l2_weight_decay=1e-4, is_training=None): - """Arg scope for training MnasFPN.""" - with slim.arg_scope( - [slim.conv2d], - weights_initializer=tf.initializers.he_normal(), - weights_regularizer=slim.l2_regularizer(l2_weight_decay)), \ - slim.arg_scope( - [slim.separable_conv2d], - weights_initializer=tf.initializers.truncated_normal( - stddev=0.536), # He_normal for 3x3 depthwise kernel. - weights_regularizer=slim.l2_regularizer(l2_weight_decay)), \ - slim.arg_scope([slim.batch_norm], - is_training=is_training, - epsilon=0.01, - decay=0.99, - center=True, - scale=True) as s: - return s - - -class SSDMobileNetV2MnasFPNFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using MobilenetV2 MnasFPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - fpn_min_level=3, - fpn_max_level=6, - additional_layer_depth=48, - head_def=None, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False, - data_format='channels_last'): - """SSD MnasFPN feature extractor based on Mobilenet v2 architecture. - - See https://arxiv.org/abs/1912.01106 - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the base - feature extractor. - fpn_min_level: the highest resolution feature map to use in MnasFPN. - Currently the only valid value is 3. - fpn_max_level: the smallest resolution feature map to construct or use in - MnasFPN. Currentl the only valid value is 6. - additional_layer_depth: additional feature map layer channel depth for - NAS-FPN. - head_def: A dictionary specifying the MnasFPN head architecture. Default - uses MNASFPN_DEF. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - use_native_resize_op: Whether to use native resize op. Default is False. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - data_format: The ordering of the dimensions in the inputs, The valid - values are {'channels_first', 'channels_last'). - """ - super(SSDMobileNetV2MnasFPNFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=( - override_base_feature_extractor_hyperparams)) - if fpn_min_level != 3 or fpn_max_level != 6: - raise ValueError('Min and max levels of MnasFPN must be 3 and 6 for now.') - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._fpn_layer_depth = additional_layer_depth - self._head_def = head_def if head_def else MNASFPN_DEF - self._data_format = data_format - self._use_native_resize_op = use_native_resize_op - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def _verify_config(self, inputs): - """Verify that MnasFPN config and its inputs.""" - num_inputs = len(inputs) - assert len(self._head_def['feature_levels']) == num_inputs - - base_width = inputs[0].shape.as_list( - )[1] * 2**self._head_def['feature_levels'][0] - for i in range(1, num_inputs): - width = inputs[i].shape.as_list()[1] - level = self._head_def['feature_levels'][i] - expected_width = base_width // 2**level - if width != expected_width: - raise ValueError( - 'Resolution of input {} does not match its level {}.'.format( - i, level)) - - for cell_spec in self._head_def['spec']: - # The last K nodes in a cell are the inputs to the next cell. Assert that - # their feature maps are at the right level. - for i in range(num_inputs): - if cell_spec[-num_inputs + - i].output_level != self._head_def['feature_levels'][i]: - raise ValueError( - 'Mismatch between node level {} and desired output level {}.' - .format(cell_spec[-num_inputs + i].output_level, - self._head_def['feature_levels'][i])) - # Assert that each block only uses precending blocks. - for bi, block_spec in enumerate(cell_spec): - for inp in block_spec.inputs: - if inp >= bi + num_inputs: - raise ValueError( - 'Block {} is trying to access uncreated block {}.'.format( - bi, inp)) - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - with tf.variable_scope('MobilenetV2', reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v2.training_scope(is_training=None, bn_decay=0.99)), \ - slim.arg_scope( - [mobilenet.depth_multiplier], min_depth=self._min_depth): - with slim.arg_scope( - training_scope(l2_weight_decay=4e-5, - is_training=self._is_training)): - - _, image_features = mobilenet_v2.mobilenet_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - final_endpoint='layer_18', - depth_multiplier=self._depth_multiplier, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - - multiplier_func = functools.partial( - _apply_multiplier, - multiplier=self._depth_multiplier, - min_depth=self._min_depth) - with tf.variable_scope('MnasFPN', reuse=self._reuse_weights): - with slim.arg_scope( - training_scope(l2_weight_decay=1e-4, is_training=self._is_training)): - # Create C6 by downsampling C5. - c6 = slim.max_pool2d( - _maybe_pad(image_features['layer_18'], self._use_explicit_padding), - [3, 3], - stride=[2, 2], - padding='VALID' if self._use_explicit_padding else 'SAME', - scope='C6_downsample') - c6 = slim.conv2d( - c6, - multiplier_func(self._fpn_layer_depth), - [1, 1], - activation_fn=tf.identity, - normalizer_fn=slim.batch_norm, - weights_regularizer=None, # this 1x1 has no kernel regularizer. - padding='VALID', - scope='C6_Conv1x1') - image_features['C6'] = tf.identity(c6) # Needed for quantization. - for k in sorted(image_features.keys()): - tf.logging.error('{}: {}'.format(k, image_features[k])) - - mnasfpn_inputs = [ - image_features['layer_7'], # C3 - image_features['layer_14'], # C4 - image_features['layer_18'], # C5 - image_features['C6'] # C6 - ] - self._verify_config(mnasfpn_inputs) - feature_maps = mnasfpn( - mnasfpn_inputs, - head_def=self._head_def, - output_channel=self._fpn_layer_depth, - use_explicit_padding=self._use_explicit_padding, - use_native_resize_op=self._use_native_resize_op, - multiplier_func=multiplier_func) - return feature_maps diff --git a/research/object_detection/models/ssd_mobilenet_v2_mnasfpn_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_v2_mnasfpn_feature_extractor_tf1_test.py deleted file mode 100644 index b650bf93563..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v2_mnasfpn_feature_extractor_tf1_test.py +++ /dev/null @@ -1,86 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd_mobilenet_v2_nas_fpn_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_mobilenet_v2_mnasfpn_feature_extractor as mnasfpn_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetV2MnasFPNFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False): - min_depth = 16 - is_training = True - fpn_num_filters = 48 - return mnasfpn_feature_extractor.SSDMobileNetV2MnasFPNFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - additional_layer_depth=fpn_num_filters, - use_explicit_padding=use_explicit_padding) - - def test_extract_features_returns_correct_shapes_320_256(self): - image_height = 320 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 40, 32, 48), (2, 20, 16, 48), - (2, 10, 8, 48), (2, 5, 4, 48)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_extract_features_returns_correct_shapes_enforcing_min_depth(self): - image_height = 256 - image_width = 256 - depth_multiplier = 0.5**12 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 16), (2, 16, 16, 16), - (2, 8, 8, 16), (2, 4, 4, 16)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=False) - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_explicit_padding=True) - - def test_preprocess_returns_correct_value_range(self): - image_height = 320 - image_width = 320 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_mobilenet_v3_feature_extractor.py b/research/object_detection/models/ssd_mobilenet_v3_feature_extractor.py deleted file mode 100644 index 6c04e9585a7..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v3_feature_extractor.py +++ /dev/null @@ -1,248 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSDFeatureExtractor for MobileNetV3 features.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets.mobilenet import mobilenet -from nets.mobilenet import mobilenet_v3 - - -class SSDMobileNetV3FeatureExtractorBase(ssd_meta_arch.SSDFeatureExtractor): - """Base class of SSD feature extractor using MobilenetV3 features.""" - - def __init__(self, - conv_defs, - from_layer, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobilenetV3'): - """MobileNetV3 Feature Extractor for SSD Models. - - MobileNet v3. Details found in: - https://arxiv.org/abs/1905.02244 - - Args: - conv_defs: MobileNetV3 conv defs for backbone. - from_layer: A cell of two layer names (string) to connect to the 1st and - 2nd inputs of the SSD head. - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the base - feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - scope_name: scope name (string) of network variables. - """ - super(SSDMobileNetV3FeatureExtractorBase, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams - ) - self._conv_defs = conv_defs - self._from_layer = from_layer - self._scope_name = scope_name - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - Raises: - ValueError if conv_defs is not provided or from_layer does not meet the - size requirement. - """ - - if not self._conv_defs: - raise ValueError('Must provide backbone conv defs.') - - if len(self._from_layer) != 2: - raise ValueError('SSD input feature names are not provided.') - - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - - feature_map_layout = { - 'from_layer': [ - self._from_layer[0], self._from_layer[1], '', '', '', '' - ], - 'layer_depth': [-1, -1, 512, 256, 256, 128], - 'use_depthwise': self._use_depthwise, - 'use_explicit_padding': self._use_explicit_padding, - } - - with tf.variable_scope( - self._scope_name, reuse=self._reuse_weights) as scope: - with slim.arg_scope( - mobilenet_v3.training_scope(is_training=None, bn_decay=0.9997)), \ - slim.arg_scope( - [mobilenet.depth_multiplier], min_depth=self._min_depth): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams else - context_manager.IdentityContextManager()): - _, image_features = mobilenet_v3.mobilenet_base( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - conv_defs=self._conv_defs, - final_endpoint=self._from_layer[1], - depth_multiplier=self._depth_multiplier, - use_explicit_padding=self._use_explicit_padding, - scope=scope) - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) - - -class SSDMobileNetV3LargeFeatureExtractor(SSDMobileNetV3FeatureExtractorBase): - """Mobilenet V3-Large feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobilenetV3'): - super(SSDMobileNetV3LargeFeatureExtractor, self).__init__( - conv_defs=mobilenet_v3.V3_LARGE_DETECTION, - from_layer=['layer_14/expansion_output', 'layer_17'], - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name - ) - - -class SSDMobileNetV3SmallFeatureExtractor(SSDMobileNetV3FeatureExtractorBase): - """Mobilenet V3-Small feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobilenetV3'): - super(SSDMobileNetV3SmallFeatureExtractor, self).__init__( - conv_defs=mobilenet_v3.V3_SMALL_DETECTION, - from_layer=['layer_10/expansion_output', 'layer_13'], - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name - ) - - -class SSDMobileNetV3SmallPrunedFeatureExtractor( - SSDMobileNetV3FeatureExtractorBase): - """Mobilenet V3-Small feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - scope_name='MobilenetV3'): - super(SSDMobileNetV3SmallPrunedFeatureExtractor, self).__init__( - conv_defs=mobilenet_v3.V3_SMALL_PRUNED_DETECTION, - from_layer=['layer_9/expansion_output', 'layer_12'], - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams, - scope_name=scope_name) diff --git a/research/object_detection/models/ssd_mobilenet_v3_feature_extractor_testbase.py b/research/object_detection/models/ssd_mobilenet_v3_feature_extractor_testbase.py deleted file mode 100644 index d5ba60f2efe..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v3_feature_extractor_testbase.py +++ /dev/null @@ -1,112 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Base test class for ssd_mobilenet_v3_feature_extractor.""" - -import abc - -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test - - -class _SsdMobilenetV3FeatureExtractorTestBase( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - """Base class for MobilenetV3 tests.""" - - @abc.abstractmethod - def _get_input_sizes(self): - """Return feature map sizes for the two inputs to SSD head.""" - pass - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - input_feature_sizes = self._get_input_sizes() - expected_feature_map_shape = [(2, 8, 8, input_feature_sizes[0]), - (2, 4, 4, input_feature_sizes[1]), - (2, 2, 2, 512), (2, 1, 1, 256), (2, 1, 1, - 256), - (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_keras=False) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - input_feature_sizes = self._get_input_sizes() - expected_feature_map_shape = [(2, 19, 19, input_feature_sizes[0]), - (2, 10, 10, input_feature_sizes[1]), - (2, 5, 5, 512), (2, 3, 3, 256), (2, 2, 2, - 256), - (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, - image_height, - image_width, - depth_multiplier, - pad_to_multiple, - expected_feature_map_shape, - use_keras=False) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 32 - input_feature_sizes = self._get_input_sizes() - expected_feature_map_shape = [(2, 20, 20, input_feature_sizes[0]), - (2, 10, 10, input_feature_sizes[1]), - (2, 5, 5, 512), (2, 3, 3, 256), (2, 2, 2, - 256), - (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(4, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=False) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_has_fused_batchnorm(self): - image_height = 40 - image_width = 40 - depth_multiplier = 1 - pad_to_multiple = 1 - image_placeholder = tf.placeholder(tf.float32, - [1, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=False) - preprocessed_image = feature_extractor.preprocess(image_placeholder) - _ = feature_extractor.extract_features(preprocessed_image) - self.assertTrue(any('FusedBatchNorm' in op.type - for op in tf.get_default_graph().get_operations())) diff --git a/research/object_detection/models/ssd_mobilenet_v3_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_mobilenet_v3_feature_extractor_tf1_test.py deleted file mode 100644 index 43c02490a73..00000000000 --- a/research/object_detection/models/ssd_mobilenet_v3_feature_extractor_tf1_test.py +++ /dev/null @@ -1,105 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd_mobilenet_v3_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_mobilenet_v3_feature_extractor -from object_detection.models import ssd_mobilenet_v3_feature_extractor_testbase -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetV3LargeFeatureExtractorTest( - ssd_mobilenet_v3_feature_extractor_testbase - ._SsdMobilenetV3FeatureExtractorTestBase): - - def _get_input_sizes(self): - """Return first two input feature map sizes.""" - return [672, 480] - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - use_keras=False): - """Constructs a new Mobilenet V3-Large feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - return ( - ssd_mobilenet_v3_feature_extractor.SSDMobileNetV3LargeFeatureExtractor( - False, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdMobilenetV3SmallFeatureExtractorTest( - ssd_mobilenet_v3_feature_extractor_testbase - ._SsdMobilenetV3FeatureExtractorTestBase): - - def _get_input_sizes(self): - """Return first two input feature map sizes.""" - return [288, 288] - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - use_keras=False): - """Constructs a new Mobilenet V3-Small feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_keras: if True builds a keras-based feature extractor, if False builds - a slim-based one. - - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - return ( - ssd_mobilenet_v3_feature_extractor.SSDMobileNetV3SmallFeatureExtractor( - False, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_pnasnet_feature_extractor.py b/research/object_detection/models/ssd_pnasnet_feature_extractor.py deleted file mode 100644 index 7740533bbb5..00000000000 --- a/research/object_detection/models/ssd_pnasnet_feature_extractor.py +++ /dev/null @@ -1,181 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSDFeatureExtractor for PNASNet features. - -Based on PNASNet ImageNet model: https://arxiv.org/abs/1712.00559 -""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import variables_helper -try: - from nets.nasnet import pnasnet # pylint: disable=g-import-not-at-top -except: # pylint: disable=bare-except - pass - - -def pnasnet_large_arg_scope_for_detection(is_batch_norm_training=False): - """Defines the default arg scope for the PNASNet Large for object detection. - - This provides a small edit to switch batch norm training on and off. - - Args: - is_batch_norm_training: Boolean indicating whether to train with batch norm. - Default is False. - - Returns: - An `arg_scope` to use for the PNASNet Large Model. - """ - imagenet_scope = pnasnet.pnasnet_large_arg_scope() - with slim.arg_scope(imagenet_scope): - with slim.arg_scope([slim.batch_norm], - is_training=is_batch_norm_training) as sc: - return sc - - -class SSDPNASNetFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using PNASNet features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - num_layers=6, - override_base_feature_extractor_hyperparams=False): - """PNASNet Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - use_depthwise: Whether to use depthwise convolutions. - num_layers: Number of SSD layers. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDPNASNetFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - num_layers=num_layers, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - - feature_map_layout = { - 'from_layer': ['Cell_7', 'Cell_11', '', '', '', ''][:self._num_layers], - 'layer_depth': [-1, -1, 512, 256, 256, 128][:self._num_layers], - 'use_explicit_padding': self._use_explicit_padding, - 'use_depthwise': self._use_depthwise, - } - - with slim.arg_scope( - pnasnet_large_arg_scope_for_detection( - is_batch_norm_training=self._is_training)): - with slim.arg_scope([slim.conv2d, slim.batch_norm, slim.separable_conv2d], - reuse=self._reuse_weights): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams else - context_manager.IdentityContextManager()): - _, image_features = pnasnet.build_pnasnet_large( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), - num_classes=None, - is_training=self._is_training, - final_endpoint='Cell_11') - with tf.variable_scope('SSD_feature_maps', reuse=self._reuse_weights): - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.multi_resolution_feature_maps( - feature_map_layout=feature_map_layout, - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - insert_1x1_conv=True, - image_features=image_features) - - return list(feature_maps.values()) - - def restore_from_classification_checkpoint_fn(self, feature_extractor_scope): - """Returns a map of variables to load from a foreign checkpoint. - - Note that this overrides the default implementation in - ssd_meta_arch.SSDFeatureExtractor which does not work for PNASNet - checkpoints. - - Args: - feature_extractor_scope: A scope name for the first stage feature - extractor. - - Returns: - A dict mapping variable names (to load from a checkpoint) to variables in - the model graph. - """ - variables_to_restore = {} - for variable in variables_helper.get_global_variables_safely(): - if variable.op.name.startswith(feature_extractor_scope): - var_name = variable.op.name.replace(feature_extractor_scope + '/', '') - var_name += '/ExponentialMovingAverage' - variables_to_restore[var_name] = variable - return variables_to_restore diff --git a/research/object_detection/models/ssd_pnasnet_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_pnasnet_feature_extractor_tf1_test.py deleted file mode 100644 index d5f5bff92d9..00000000000 --- a/research/object_detection/models/ssd_pnasnet_feature_extractor_tf1_test.py +++ /dev/null @@ -1,108 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for ssd_pnas_feature_extractor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_pnasnet_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SsdPnasNetFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - num_layers=6, - is_training=True): - """Constructs a new feature extractor. - - Args: - depth_multiplier: float depth multiplier for feature extractor - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - use_explicit_padding: Use 'VALID' padding for convolutions, but prepad - inputs so that the output dimensions are the same as if 'SAME' padding - were used. - num_layers: number of SSD layers. - is_training: whether the network is in training mode. - Returns: - an ssd_meta_arch.SSDFeatureExtractor object. - """ - min_depth = 32 - return ssd_pnasnet_feature_extractor.SSDPNASNetFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding, - num_layers=num_layers) - - def test_extract_features_returns_correct_shapes_128(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 2160), (2, 4, 4, 4320), - (2, 2, 2, 512), (2, 1, 1, 256), - (2, 1, 1, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_299(self): - image_height = 299 - image_width = 299 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 2160), (2, 10, 10, 4320), - (2, 5, 5, 512), (2, 3, 3, 256), - (2, 2, 2, 256), (2, 1, 1, 128)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = np.random.rand(2, image_height, image_width, 3) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - self.assertTrue(np.all(np.less_equal(np.abs(preprocessed_image), 1.0))) - - def test_extract_features_with_fewer_layers(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 8, 8, 2160), (2, 4, 4, 4320), - (2, 2, 2, 512), (2, 1, 1, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, num_layers=4) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor.py b/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor.py deleted file mode 100644 index 89bcf2b4dd4..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor.py +++ /dev/null @@ -1,390 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSD Feature Pyramid Network (FPN) feature extractors based on Resnet v1. - -See https://arxiv.org/abs/1708.02002 for details. -""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import resnet_v1 - - -class SSDResnetV1FpnFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD FPN feature extractor based on Resnet v1 architecture.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - resnet_base_fn, - resnet_scope_name, - fpn_scope_name, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False): - """SSD FPN feature extractor based on Resnet v1 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - resnet_base_fn: base resnet network to use. - resnet_scope_name: scope name under which to construct resnet - fpn_scope_name: scope name under which to construct the feature pyramid - network. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - fpn_max_level: the smallest resolution feature map to construct or use in - FPN. FPN constructions uses features maps starting from fpn_min_level - upto the fpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of fpn - levels. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. UNUSED currently. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - - Raises: - ValueError: On supplying invalid arguments for unused arguments. - """ - super(SSDResnetV1FpnFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - if self._use_explicit_padding is True: - raise ValueError('Explicit padding is not a valid option.') - self._resnet_base_fn = resnet_base_fn - self._resnet_scope_name = resnet_scope_name - self._fpn_scope_name = fpn_scope_name - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._additional_layer_depth = additional_layer_depth - self._use_native_resize_op = use_native_resize_op - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - VGG style channel mean subtraction as described here: - https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-mdnge. - Note that if the number of channels is not equal to 3, the mean subtraction - will be skipped and the original resized_inputs will be returned. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - if resized_inputs.shape.as_list()[3] == 3: - channel_means = [123.68, 116.779, 103.939] - return resized_inputs - [[channel_means]] - else: - return resized_inputs - - def _filter_features(self, image_features): - # TODO(rathodv): Change resnet endpoint to strip scope prefixes instead - # of munging the scope here. - filtered_image_features = dict({}) - for key, feature in image_features.items(): - feature_name = key.split('/')[-1] - if feature_name in ['block1', 'block2', 'block3', 'block4']: - filtered_image_features[feature_name] = feature - return filtered_image_features - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 129, preprocessed_inputs) - - with tf.variable_scope( - self._resnet_scope_name, reuse=self._reuse_weights) as scope: - with slim.arg_scope(resnet_v1.resnet_arg_scope()): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams else - context_manager.IdentityContextManager()): - _, image_features = self._resnet_base_fn( - inputs=ops.pad_to_multiple(preprocessed_inputs, - self._pad_to_multiple), - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - store_non_strided_activations=True, - min_base_depth=self._min_depth, - depth_multiplier=self._depth_multiplier, - scope=scope) - image_features = self._filter_features(image_features) - depth_fn = lambda d: max(int(d * self._depth_multiplier), self._min_depth) - with slim.arg_scope(self._conv_hyperparams_fn()): - with tf.variable_scope(self._fpn_scope_name, - reuse=self._reuse_weights): - base_fpn_max_level = min(self._fpn_max_level, 5) - feature_block_list = [] - for level in range(self._fpn_min_level, base_fpn_max_level + 1): - feature_block_list.append('block{}'.format(level - 1)) - fpn_features = feature_map_generators.fpn_top_down_feature_maps( - [(key, image_features[key]) for key in feature_block_list], - depth=depth_fn(self._additional_layer_depth), - use_native_resize_op=self._use_native_resize_op) - feature_maps = [] - for level in range(self._fpn_min_level, base_fpn_max_level + 1): - feature_maps.append( - fpn_features['top_down_block{}'.format(level - 1)]) - last_feature_map = fpn_features['top_down_block{}'.format( - base_fpn_max_level - 1)] - # Construct coarse features - for i in range(base_fpn_max_level, self._fpn_max_level): - last_feature_map = slim.conv2d( - last_feature_map, - num_outputs=depth_fn(self._additional_layer_depth), - kernel_size=[3, 3], - stride=2, - padding='SAME', - scope='bottom_up_block{}'.format(i)) - feature_maps.append(last_feature_map) - return feature_maps - - -class SSDResnet50V1FpnFeatureExtractor(SSDResnetV1FpnFeatureExtractor): - """SSD Resnet50 V1 FPN feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False): - """SSD Resnet50 V1 FPN feature extractor based on Resnet v1 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - fpn_min_level: the minimum level in feature pyramid networks. - fpn_max_level: the maximum level in feature pyramid networks. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. UNUSED currently. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDResnet50V1FpnFeatureExtractor, self).__init__( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - resnet_v1.resnet_v1_50, - 'resnet_v1_50', - 'fpn', - fpn_min_level, - fpn_max_level, - additional_layer_depth, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - - -class SSDResnet101V1FpnFeatureExtractor(SSDResnetV1FpnFeatureExtractor): - """SSD Resnet101 V1 FPN feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False): - """SSD Resnet101 V1 FPN feature extractor based on Resnet v1 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - fpn_min_level: the minimum level in feature pyramid networks. - fpn_max_level: the maximum level in feature pyramid networks. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. UNUSED currently. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDResnet101V1FpnFeatureExtractor, self).__init__( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - resnet_v1.resnet_v1_101, - 'resnet_v1_101', - 'fpn', - fpn_min_level, - fpn_max_level, - additional_layer_depth, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) - - -class SSDResnet152V1FpnFeatureExtractor(SSDResnetV1FpnFeatureExtractor): - """SSD Resnet152 V1 FPN feature extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - use_native_resize_op=False, - override_base_feature_extractor_hyperparams=False): - """SSD Resnet152 V1 FPN feature extractor based on Resnet v1 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - fpn_min_level: the minimum level in feature pyramid networks. - fpn_max_level: the maximum level in feature pyramid networks. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. UNUSED currently. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - use_native_resize_op: Whether to use tf.image.nearest_neighbor_resize - to do upsampling in FPN. Default is false. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDResnet152V1FpnFeatureExtractor, self).__init__( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - resnet_v1.resnet_v1_152, - 'resnet_v1_152', - 'fpn', - fpn_min_level, - fpn_max_level, - additional_layer_depth, - reuse_weights=reuse_weights, - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - use_native_resize_op=use_native_resize_op, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams) diff --git a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_testbase.py b/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_testbase.py deleted file mode 100644 index 72cccd21120..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_testbase.py +++ /dev/null @@ -1,192 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd resnet v1 FPN feature extractors.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import abc -import numpy as np -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.utils import test_utils - - -class SSDResnetFPNFeatureExtractorTestBase( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - """Helper test class for SSD Resnet v1 FPN feature extractors.""" - - @abc.abstractmethod - def _resnet_scope_name(self): - pass - - @abc.abstractmethod - def _fpn_scope_name(self): - return 'fpn' - - @abc.abstractmethod - def _create_feature_extractor(self, - depth_multiplier, - pad_to_multiple, - use_explicit_padding=False, - min_depth=32, - use_keras=False): - pass - - def test_extract_features_returns_correct_shapes_256(self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=self.is_tf2()) - - def test_extract_features_returns_correct_shapes_with_dynamic_inputs( - self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=self.is_tf2()) - - def test_extract_features_returns_correct_shapes_with_depth_multiplier( - self): - image_height = 256 - image_width = 256 - depth_multiplier = 0.5 - expected_num_channels = int(256 * depth_multiplier) - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 32, 32, expected_num_channels), - (2, 16, 16, expected_num_channels), - (2, 8, 8, expected_num_channels), - (2, 4, 4, expected_num_channels), - (2, 2, 2, expected_num_channels)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=self.is_tf2()) - - def test_extract_features_returns_correct_shapes_with_min_depth( - self): - image_height = 256 - image_width = 256 - depth_multiplier = 1.0 - pad_to_multiple = 1 - min_depth = 320 - expected_feature_map_shape = [(2, 32, 32, min_depth), - (2, 16, 16, min_depth), - (2, 8, 8, min_depth), - (2, 4, 4, min_depth), - (2, 2, 2, min_depth)] - - with test_utils.GraphContextOrNone() as g: - image_tensor = tf.random.uniform([2, image_height, image_width, 3]) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, min_depth=min_depth, - use_keras=self.is_tf2()) - - def graph_fn(): - if self.is_tf2(): - return feature_extractor(image_tensor) - return feature_extractor.extract_features(image_tensor) - - feature_maps = self.execute(graph_fn, [], graph=g) - for feature_map, expected_shape in zip(feature_maps, - expected_feature_map_shape): - self.assertAllEqual(feature_map.shape, expected_shape) - - def test_extract_features_returns_correct_shapes_with_pad_to_multiple( - self): - image_height = 254 - image_width = 254 - depth_multiplier = 1.0 - pad_to_multiple = 32 - expected_feature_map_shape = [(2, 32, 32, 256), (2, 16, 16, 256), - (2, 8, 8, 256), (2, 4, 4, 256), - (2, 2, 2, 256)] - - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape, use_keras=self.is_tf2()) - - def test_extract_features_raises_error_with_invalid_image_size( - self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple, - use_keras=self.is_tf2()) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image_np = np.random.rand(4, image_height, image_width, 3) - with test_utils.GraphContextOrNone() as g: - test_image = tf.constant(test_image_np) - feature_extractor = self._create_feature_extractor( - depth_multiplier, pad_to_multiple, use_keras=self.is_tf2()) - - def graph_fn(): - preprocessed_image = feature_extractor.preprocess(test_image) - return preprocessed_image - - preprocessed_image_out = self.execute(graph_fn, [], graph=g) - self.assertAllClose(preprocessed_image_out, - test_image_np - [[123.68, 116.779, 103.939]]) - - def test_variables_only_created_in_scope(self): - if self.is_tf2(): - self.skipTest('test_variables_only_created_in_scope is only tf1') - depth_multiplier = 1 - pad_to_multiple = 1 - scope_name = self._resnet_scope_name() - self.check_feature_extractor_variables_under_scope( - depth_multiplier, - pad_to_multiple, - scope_name, - use_keras=self.is_tf2()) - - def test_variable_count(self): - if self.is_tf2(): - self.skipTest('test_variable_count is only tf1') - depth_multiplier = 1 - pad_to_multiple = 1 - variables = self.get_feature_extractor_variables( - depth_multiplier, - pad_to_multiple, - use_keras=self.is_tf2()) - # The number of expected variables in resnet_v1_50, resnet_v1_101, - # and resnet_v1_152 is 279, 534, and 789 respectively. - expected_variables_len = 279 - scope_name = self._resnet_scope_name() - if scope_name in ('ResNet101V1_FPN', 'resnet_v1_101'): - expected_variables_len = 534 - elif scope_name in ('ResNet152V1_FPN', 'resnet_v1_152'): - expected_variables_len = 789 - self.assertEqual(len(variables), expected_variables_len) diff --git a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_tf1_test.py deleted file mode 100644 index 58952ff9486..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_tf1_test.py +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd resnet v1 FPN feature extractors.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_resnet_v1_fpn_feature_extractor -from object_detection.models import ssd_resnet_v1_fpn_feature_extractor_testbase -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDResnet50V1FeatureExtractorTest( - ssd_resnet_v1_fpn_feature_extractor_testbase. - SSDResnetFPNFeatureExtractorTestBase): - """SSDResnet50v1Fpn feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False, min_depth=32, - use_keras=False): - is_training = True - return ( - ssd_resnet_v1_fpn_feature_extractor.SSDResnet50V1FpnFeatureExtractor( - is_training, depth_multiplier, min_depth, pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - def _resnet_scope_name(self): - return 'resnet_v1_50' - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDResnet101V1FeatureExtractorTest( - ssd_resnet_v1_fpn_feature_extractor_testbase. - SSDResnetFPNFeatureExtractorTestBase): - """SSDResnet101v1Fpn feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False, min_depth=32, - use_keras=False): - is_training = True - return ( - ssd_resnet_v1_fpn_feature_extractor.SSDResnet101V1FpnFeatureExtractor( - is_training, depth_multiplier, min_depth, pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - def _resnet_scope_name(self): - return 'resnet_v1_101' - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDResnet152V1FeatureExtractorTest( - ssd_resnet_v1_fpn_feature_extractor_testbase. - SSDResnetFPNFeatureExtractorTestBase): - """SSDResnet152v1Fpn feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False, min_depth=32, - use_keras=False): - is_training = True - return ( - ssd_resnet_v1_fpn_feature_extractor.SSDResnet152V1FpnFeatureExtractor( - is_training, depth_multiplier, min_depth, pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - def _resnet_scope_name(self): - return 'resnet_v1_152' - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_tf2_test.py b/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_tf2_test.py deleted file mode 100644 index 27c54ddd08f..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_fpn_feature_extractor_tf2_test.py +++ /dev/null @@ -1,103 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd resnet v1 FPN feature extractors.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_resnet_v1_fpn_feature_extractor_testbase -from object_detection.models import ssd_resnet_v1_fpn_keras_feature_extractor -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class SSDResnet50V1FeatureExtractorTest( - ssd_resnet_v1_fpn_feature_extractor_testbase. - SSDResnetFPNFeatureExtractorTestBase): - """SSDResnet50v1Fpn feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False, min_depth=32, - use_keras=True): - is_training = True - return (ssd_resnet_v1_fpn_keras_feature_extractor. - SSDResNet50V1FpnKerasFeatureExtractor( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams( - add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - name='ResNet50V1_FPN')) - - def _resnet_scope_name(self): - return 'ResNet50V1_FPN' - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class SSDResnet101V1FeatureExtractorTest( - ssd_resnet_v1_fpn_feature_extractor_testbase. - SSDResnetFPNFeatureExtractorTestBase): - """SSDResnet101v1Fpn feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False, min_depth=32, - use_keras=False): - is_training = True - return (ssd_resnet_v1_fpn_keras_feature_extractor. - SSDResNet101V1FpnKerasFeatureExtractor( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams( - add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - name='ResNet101V1_FPN')) - - def _resnet_scope_name(self): - return 'ResNet101V1_FPN' - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class SSDResnet152V1FeatureExtractorTest( - ssd_resnet_v1_fpn_feature_extractor_testbase. - SSDResnetFPNFeatureExtractorTestBase): - """SSDResnet152v1Fpn feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False, min_depth=32, - use_keras=False): - is_training = True - return (ssd_resnet_v1_fpn_keras_feature_extractor. - SSDResNet152V1FpnKerasFeatureExtractor( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=self._build_conv_hyperparams( - add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - name='ResNet152V1_FPN')) - - def _resnet_scope_name(self): - return 'ResNet152V1_FPN' - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_resnet_v1_fpn_keras_feature_extractor.py b/research/object_detection/models/ssd_resnet_v1_fpn_keras_feature_extractor.py deleted file mode 100644 index 364b4e8cc23..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_fpn_keras_feature_extractor.py +++ /dev/null @@ -1,456 +0,0 @@ -# Copyright 2019 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""SSD Keras-based ResnetV1 FPN Feature Extractor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.models.keras_models import resnet_v1 -from object_detection.utils import ops -from object_detection.utils import shape_utils - -_RESNET_MODEL_OUTPUT_LAYERS = { - 'resnet_v1_50': ['conv2_block3_out', 'conv3_block4_out', - 'conv4_block6_out', 'conv5_block3_out'], - 'resnet_v1_101': ['conv2_block3_out', 'conv3_block4_out', - 'conv4_block23_out', 'conv5_block3_out'], - 'resnet_v1_152': ['conv2_block3_out', 'conv3_block8_out', - 'conv4_block36_out', 'conv5_block3_out'], -} - - -class SSDResNetV1FpnKerasFeatureExtractor( - ssd_meta_arch.SSDKerasFeatureExtractor): - """SSD Feature Extractor using Keras-based ResnetV1 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - resnet_v1_base_model, - resnet_v1_base_model_name, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=None, - use_depthwise=None, - override_base_feature_extractor_hyperparams=False, - name=None): - """SSD Keras based FPN feature extractor Resnet v1 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - resnet_v1_base_model: base resnet v1 network to use. One of - the resnet_v1.resnet_v1_{50,101,152} models. - resnet_v1_base_model_name: model name under which to construct resnet v1. - fpn_min_level: the highest resolution feature map to use in FPN. The valid - values are {2, 3, 4, 5} which map to Resnet blocks {1, 2, 3, 4} - respectively. - fpn_max_level: the smallest resolution feature map to construct or use in - FPN. FPN constructions uses features maps starting from fpn_min_level - upto the fpn_max_level. In the case that there are not enough feature - maps in the backbone network, additional feature maps are created by - applying stride 2 convolutions until we get the desired number of fpn - levels. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: whether to use explicit padding when extracting - features. Default is None, as it's an invalid option and not implemented - in this feature extractor. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDResNetV1FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - use_explicit_padding=None, - use_depthwise=None, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - if self._use_explicit_padding: - raise ValueError('Explicit padding is not a valid option.') - if self._use_depthwise: - raise ValueError('Depthwise is not a valid option.') - self._fpn_min_level = fpn_min_level - self._fpn_max_level = fpn_max_level - self._additional_layer_depth = additional_layer_depth - self._resnet_v1_base_model = resnet_v1_base_model - self._resnet_v1_base_model_name = resnet_v1_base_model_name - self._resnet_block_names = ['block1', 'block2', 'block3', 'block4'] - self.classification_backbone = None - self._fpn_features_generator = None - self._coarse_feature_layers = [] - - def build(self, input_shape): - full_resnet_v1_model = self._resnet_v1_base_model( - batchnorm_training=(self._is_training and not self._freeze_batchnorm), - conv_hyperparams=(self._conv_hyperparams - if self._override_base_feature_extractor_hyperparams - else None), - depth_multiplier=self._depth_multiplier, - min_depth=self._min_depth, - classes=None, - weights=None, - include_top=False) - output_layers = _RESNET_MODEL_OUTPUT_LAYERS[self._resnet_v1_base_model_name] - outputs = [full_resnet_v1_model.get_layer(output_layer_name).output - for output_layer_name in output_layers] - self.classification_backbone = tf.keras.Model( - inputs=full_resnet_v1_model.inputs, - outputs=outputs) - # pylint:disable=g-long-lambda - self._depth_fn = lambda d: max( - int(d * self._depth_multiplier), self._min_depth) - self._base_fpn_max_level = min(self._fpn_max_level, 5) - self._num_levels = self._base_fpn_max_level + 1 - self._fpn_min_level - self._fpn_features_generator = ( - feature_map_generators.KerasFpnTopDownFeatureMaps( - num_levels=self._num_levels, - depth=self._depth_fn(self._additional_layer_depth), - is_training=self._is_training, - conv_hyperparams=self._conv_hyperparams, - freeze_batchnorm=self._freeze_batchnorm, - name='FeatureMaps')) - # Construct coarse feature layers - depth = self._depth_fn(self._additional_layer_depth) - for i in range(self._base_fpn_max_level, self._fpn_max_level): - layers = [] - layer_name = 'bottom_up_block{}'.format(i) - layers.append( - tf.keras.layers.Conv2D( - depth, - [3, 3], - padding='SAME', - strides=2, - name=layer_name + '_conv', - **self._conv_hyperparams.params())) - layers.append( - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name=layer_name + '_batchnorm')) - layers.append( - self._conv_hyperparams.build_activation_layer( - name=layer_name)) - self._coarse_feature_layers.append(layers) - self.built = True - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - VGG style channel mean subtraction as described here: - https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-mdnge. - Note that if the number of channels is not equal to 3, the mean subtraction - will be skipped and the original resized_inputs will be returned. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - if resized_inputs.shape.as_list()[3] == 3: - channel_means = [123.68, 116.779, 103.939] - return resized_inputs - [[channel_means]] - else: - return resized_inputs - - def _extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 129, preprocessed_inputs) - - image_features = self.classification_backbone( - ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple)) - - feature_block_list = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_block_list.append('block{}'.format(level - 1)) - feature_block_map = dict( - list(zip(self._resnet_block_names, image_features))) - fpn_input_image_features = [ - (feature_block, feature_block_map[feature_block]) - for feature_block in feature_block_list] - fpn_features = self._fpn_features_generator(fpn_input_image_features) - - feature_maps = [] - for level in range(self._fpn_min_level, self._base_fpn_max_level + 1): - feature_maps.append(fpn_features['top_down_block{}'.format(level-1)]) - last_feature_map = fpn_features['top_down_block{}'.format( - self._base_fpn_max_level - 1)] - - for coarse_feature_layers in self._coarse_feature_layers: - for layer in coarse_feature_layers: - last_feature_map = layer(last_feature_map) - feature_maps.append(last_feature_map) - return feature_maps - - -class SSDResNet50V1FpnKerasFeatureExtractor( - SSDResNetV1FpnKerasFeatureExtractor): - """SSD Feature Extractor using Keras-based ResnetV1-50 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=None, - use_depthwise=None, - override_base_feature_extractor_hyperparams=False, - name='ResNet50V1_FPN'): - """SSD Keras based FPN feature extractor ResnetV1-50 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - fpn_min_level: the minimum level in feature pyramid networks. - fpn_max_level: the maximum level in feature pyramid networks. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: whether to use explicit padding when extracting - features. Default is None, as it's an invalid option and not implemented - in this feature extractor. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDResNet50V1FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - resnet_v1_base_model=resnet_v1.resnet_v1_50, - resnet_v1_base_model_name='resnet_v1_50', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDResNet101V1FpnKerasFeatureExtractor( - SSDResNetV1FpnKerasFeatureExtractor): - """SSD Feature Extractor using Keras-based ResnetV1-101 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=None, - use_depthwise=None, - override_base_feature_extractor_hyperparams=False, - name='ResNet101V1_FPN'): - """SSD Keras based FPN feature extractor ResnetV1-101 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - fpn_min_level: the minimum level in feature pyramid networks. - fpn_max_level: the maximum level in feature pyramid networks. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: whether to use explicit padding when extracting - features. Default is None, as it's an invalid option and not implemented - in this feature extractor. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDResNet101V1FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - resnet_v1_base_model=resnet_v1.resnet_v1_101, - resnet_v1_base_model_name='resnet_v1_101', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) - - -class SSDResNet152V1FpnKerasFeatureExtractor( - SSDResNetV1FpnKerasFeatureExtractor): - """SSD Feature Extractor using Keras-based ResnetV1-152 FPN features.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams, - freeze_batchnorm, - inplace_batchnorm_update, - fpn_min_level=3, - fpn_max_level=7, - additional_layer_depth=256, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=None, - override_base_feature_extractor_hyperparams=False, - name='ResNet152V1_FPN'): - """SSD Keras based FPN feature extractor ResnetV1-152 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams: a `hyperparams_builder.KerasLayerHyperparams` object - containing convolution hyperparameters for the layers added on top of - the base feature extractor. - freeze_batchnorm: whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - fpn_min_level: the minimum level in feature pyramid networks. - fpn_max_level: the maximum level in feature pyramid networks. - additional_layer_depth: additional feature map layer channel depth. - reuse_weights: whether to reuse variables. Default is None. - use_explicit_padding: whether to use explicit padding when extracting - features. Default is None, as it's an invalid option and not implemented - in this feature extractor. - use_depthwise: Whether to use depthwise convolutions. UNUSED currently. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams`. - name: a string name scope to assign to the model. If 'None', Keras - will auto-generate one from the class name. - """ - super(SSDResNet152V1FpnKerasFeatureExtractor, self).__init__( - is_training=is_training, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - resnet_v1_base_model=resnet_v1.resnet_v1_152, - resnet_v1_base_model_name='resnet_v1_152', - use_explicit_padding=use_explicit_padding, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams= - override_base_feature_extractor_hyperparams, - name=name) diff --git a/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor.py b/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor.py deleted file mode 100644 index daf470181a6..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor.py +++ /dev/null @@ -1,283 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""SSD feature extractors based on Resnet v1 and PPN architectures.""" - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.models import feature_map_generators -from object_detection.utils import context_manager -from object_detection.utils import ops -from object_detection.utils import shape_utils -from nets import resnet_v1 - - -class _SSDResnetPpnFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD feature extractor based on resnet architecture and PPN.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - resnet_base_fn, - resnet_scope_name, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - base_feature_map_depth=1024, - num_layers=6, - override_base_feature_extractor_hyperparams=False, - use_bounded_activations=False): - """Resnet based PPN Feature Extractor for SSD Models. - - See go/pooling-pyramid for more details about PPN. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - resnet_base_fn: base resnet network to use. - resnet_scope_name: scope name to construct resnet - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - base_feature_map_depth: Depth of the base feature before the max pooling. - num_layers: Number of layers used to make predictions. They are pooled - from the base feature. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - use_bounded_activations: Whether or not to use bounded activations for - resnet v1 bottleneck residual unit. Bounded activations better lend - themselves to quantized inference. - """ - super(_SSDResnetPpnFeatureExtractor, self).__init__( - is_training, depth_multiplier, min_depth, pad_to_multiple, - conv_hyperparams_fn, reuse_weights, use_explicit_padding, use_depthwise, - override_base_feature_extractor_hyperparams) - self._resnet_base_fn = resnet_base_fn - self._resnet_scope_name = resnet_scope_name - self._base_feature_map_depth = base_feature_map_depth - self._num_layers = num_layers - self._use_bounded_activations = use_bounded_activations - - def _filter_features(self, image_features): - # TODO(rathodv): Change resnet endpoint to strip scope prefixes instead - # of munging the scope here. - filtered_image_features = dict({}) - for key, feature in image_features.items(): - feature_name = key.split('/')[-1] - if feature_name in ['block2', 'block3', 'block4']: - filtered_image_features[feature_name] = feature - return filtered_image_features - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - VGG style channel mean subtraction as described here: - https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-mdnge. - Note that if the number of channels is not equal to 3, the mean subtraction - will be skipped and the original resized_inputs will be returned. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - if resized_inputs.shape.as_list()[3] == 3: - channel_means = [123.68, 116.779, 103.939] - return resized_inputs - [[channel_means]] - else: - return resized_inputs - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - - Raises: - ValueError: depth multiplier is not supported. - """ - if self._depth_multiplier != 1.0: - raise ValueError('Depth multiplier not supported.') - - preprocessed_inputs = shape_utils.check_min_image_dim( - 129, preprocessed_inputs) - - with tf.variable_scope( - self._resnet_scope_name, reuse=self._reuse_weights) as scope: - with slim.arg_scope(resnet_v1.resnet_arg_scope()): - with (slim.arg_scope(self._conv_hyperparams_fn()) - if self._override_base_feature_extractor_hyperparams else - context_manager.IdentityContextManager()): - with slim.arg_scope( - [resnet_v1.bottleneck], - use_bounded_activations=self._use_bounded_activations): - _, activations = self._resnet_base_fn( - inputs=ops.pad_to_multiple(preprocessed_inputs, - self._pad_to_multiple), - num_classes=None, - is_training=None, - global_pool=False, - output_stride=None, - store_non_strided_activations=True, - scope=scope) - - with slim.arg_scope(self._conv_hyperparams_fn()): - feature_maps = feature_map_generators.pooling_pyramid_feature_maps( - base_feature_map_depth=self._base_feature_map_depth, - num_layers=self._num_layers, - image_features={ - 'image_features': self._filter_features(activations)['block3'] - }) - return list(feature_maps.values()) - - -class SSDResnet50V1PpnFeatureExtractor(_SSDResnetPpnFeatureExtractor): - """PPN Resnet50 v1 Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False): - """Resnet50 v1 Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDResnet50V1PpnFeatureExtractor, self).__init__( - is_training, depth_multiplier, min_depth, pad_to_multiple, - conv_hyperparams_fn, resnet_v1.resnet_v1_50, 'resnet_v1_50', - reuse_weights, use_explicit_padding, use_depthwise, - override_base_feature_extractor_hyperparams=( - override_base_feature_extractor_hyperparams)) - - -class SSDResnet101V1PpnFeatureExtractor(_SSDResnetPpnFeatureExtractor): - """PPN Resnet101 v1 Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False): - """Resnet101 v1 Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDResnet101V1PpnFeatureExtractor, self).__init__( - is_training, depth_multiplier, min_depth, pad_to_multiple, - conv_hyperparams_fn, resnet_v1.resnet_v1_101, 'resnet_v1_101', - reuse_weights, use_explicit_padding, use_depthwise, - override_base_feature_extractor_hyperparams=( - override_base_feature_extractor_hyperparams)) - - -class SSDResnet152V1PpnFeatureExtractor(_SSDResnetPpnFeatureExtractor): - """PPN Resnet152 v1 Feature Extractor.""" - - def __init__(self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - reuse_weights=None, - use_explicit_padding=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False): - """Resnet152 v1 Feature Extractor for SSD Models. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: float depth multiplier for feature extractor. - min_depth: minimum feature extractor depth. - pad_to_multiple: the nearest multiple to zero pad the input height and - width dimensions to. - conv_hyperparams_fn: A function to construct tf slim arg_scope for conv2d - and separable_conv2d ops in the layers that are added on top of the - base feature extractor. - reuse_weights: Whether to reuse variables. Default is None. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - use_depthwise: Whether to use depthwise convolutions. Default is False. - override_base_feature_extractor_hyperparams: Whether to override - hyperparameters of the base feature extractor with the one from - `conv_hyperparams_fn`. - """ - super(SSDResnet152V1PpnFeatureExtractor, self).__init__( - is_training, depth_multiplier, min_depth, pad_to_multiple, - conv_hyperparams_fn, resnet_v1.resnet_v1_152, 'resnet_v1_152', - reuse_weights, use_explicit_padding, use_depthwise, - override_base_feature_extractor_hyperparams=( - override_base_feature_extractor_hyperparams)) diff --git a/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor_testbase.py b/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor_testbase.py deleted file mode 100644 index ba80c6627a0..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor_testbase.py +++ /dev/null @@ -1,82 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd resnet v1 feature extractors.""" -import abc -import numpy as np -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test - - -class SSDResnetPpnFeatureExtractorTestBase( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - """Helper test class for SSD Resnet PPN feature extractors.""" - - @abc.abstractmethod - def _scope_name(self): - pass - - def test_extract_features_returns_correct_shapes_289(self): - image_height = 289 - image_width = 289 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 1024), (2, 10, 10, 1024), - (2, 5, 5, 1024), (2, 3, 3, 1024), - (2, 2, 2, 1024), (2, 1, 1, 1024)] - self.check_extract_features_returns_correct_shape( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_returns_correct_shapes_with_dynamic_inputs(self): - image_height = 289 - image_width = 289 - depth_multiplier = 1.0 - pad_to_multiple = 1 - expected_feature_map_shape = [(2, 19, 19, 1024), (2, 10, 10, 1024), - (2, 5, 5, 1024), (2, 3, 3, 1024), - (2, 2, 2, 1024), (2, 1, 1, 1024)] - self.check_extract_features_returns_correct_shapes_with_dynamic_inputs( - 2, image_height, image_width, depth_multiplier, pad_to_multiple, - expected_feature_map_shape) - - def test_extract_features_raises_error_with_invalid_image_size(self): - image_height = 32 - image_width = 32 - depth_multiplier = 1.0 - pad_to_multiple = 1 - self.check_extract_features_raises_error_with_invalid_image_size( - image_height, image_width, depth_multiplier, pad_to_multiple) - - def test_preprocess_returns_correct_value_range(self): - image_height = 128 - image_width = 128 - depth_multiplier = 1 - pad_to_multiple = 1 - test_image = tf.constant(np.random.rand(4, image_height, image_width, 3)) - feature_extractor = self._create_feature_extractor(depth_multiplier, - pad_to_multiple) - preprocessed_image = feature_extractor.preprocess(test_image) - with self.test_session() as sess: - test_image_out, preprocessed_image_out = sess.run( - [test_image, preprocessed_image]) - self.assertAllClose(preprocessed_image_out, - test_image_out - [[123.68, 116.779, 103.939]]) - - def test_variables_only_created_in_scope(self): - depth_multiplier = 1 - pad_to_multiple = 1 - self.check_feature_extractor_variables_under_scope( - depth_multiplier, pad_to_multiple, self._scope_name()) diff --git a/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor_tf1_test.py deleted file mode 100644 index bb95cb53f39..00000000000 --- a/research/object_detection/models/ssd_resnet_v1_ppn_feature_extractor_tf1_test.py +++ /dev/null @@ -1,93 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd resnet v1 feature extractors.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_resnet_v1_ppn_feature_extractor -from object_detection.models import ssd_resnet_v1_ppn_feature_extractor_testbase -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDResnet50V1PpnFeatureExtractorTest( - ssd_resnet_v1_ppn_feature_extractor_testbase. - SSDResnetPpnFeatureExtractorTestBase): - """SSDResnet50v1 feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False): - min_depth = 32 - is_training = True - return ssd_resnet_v1_ppn_feature_extractor.SSDResnet50V1PpnFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding) - - def _scope_name(self): - return 'resnet_v1_50' - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDResnet101V1PpnFeatureExtractorTest( - ssd_resnet_v1_ppn_feature_extractor_testbase. - SSDResnetPpnFeatureExtractorTestBase): - """SSDResnet101v1 feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False): - min_depth = 32 - is_training = True - return ( - ssd_resnet_v1_ppn_feature_extractor.SSDResnet101V1PpnFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - def _scope_name(self): - return 'resnet_v1_101' - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDResnet152V1PpnFeatureExtractorTest( - ssd_resnet_v1_ppn_feature_extractor_testbase. - SSDResnetPpnFeatureExtractorTestBase): - """SSDResnet152v1 feature extractor test.""" - - def _create_feature_extractor(self, depth_multiplier, pad_to_multiple, - use_explicit_padding=False): - min_depth = 32 - is_training = True - return ( - ssd_resnet_v1_ppn_feature_extractor.SSDResnet152V1PpnFeatureExtractor( - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - self.conv_hyperparams_fn, - use_explicit_padding=use_explicit_padding)) - - def _scope_name(self): - return 'resnet_v1_152' - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/models/ssd_spaghettinet_feature_extractor.py b/research/object_detection/models/ssd_spaghettinet_feature_extractor.py deleted file mode 100644 index 4a45585c6bc..00000000000 --- a/research/object_detection/models/ssd_spaghettinet_feature_extractor.py +++ /dev/null @@ -1,895 +0,0 @@ -"""SpaghettiNet Feature Extractor.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections - -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from tensorflow.python.training import moving_averages -from object_detection.meta_architectures import ssd_meta_arch -from object_detection.utils import ops -from object_detection.utils import shape_utils - -IbnOp = collections.namedtuple( - 'IbnOp', ['kernel_size', 'expansion_rate', 'stride', 'has_residual']) -SepConvOp = collections.namedtuple('SepConvOp', - ['kernel_size', 'stride', 'has_residual']) -IbnFusedGrouped = collections.namedtuple( - 'IbnFusedGrouped', - ['kernel_size', 'expansion_rate', 'stride', 'groups', 'has_residual']) -SpaghettiStemNode = collections.namedtuple('SpaghettiStemNode', - ['kernel_size', 'num_filters']) -SpaghettiNode = collections.namedtuple( - 'SpaghettiNode', ['layers', 'num_filters', 'edges', 'level']) -SpaghettiResampleEdge = collections.namedtuple('SpaghettiResampleEdge', - ['input']) -SpaghettiPassthroughEdge = collections.namedtuple('SpaghettiPassthroughEdge', - ['input']) -SpaghettiNodeSpecs = collections.namedtuple('SpaghettiNodeSpecs', - ['nodes', 'outputs']) - - -class SpaghettiNet(): - """SpaghettiNet.""" - - def __init__(self, - node_specs, - is_training=False, - use_native_resize_op=False, - use_explicit_padding=False, - activation_fn=tf.nn.relu6, - normalization_fn=slim.batch_norm, - name='spaghetti_node'): - self._node_specs = node_specs - self._is_training = is_training - self._use_native_resize_op = use_native_resize_op - self._use_explicit_padding = use_explicit_padding - self._activation_fn = activation_fn - self._normalization_fn = normalization_fn - self._name = name - self._nodes = {} - - def _quant_var(self, - name, - initializer_val, - vars_collection=tf.GraphKeys.MOVING_AVERAGE_VARIABLES): - """Create an var for storing the min/max quantization range.""" - return slim.model_variable( - name, - shape=[], - initializer=tf.constant_initializer(initializer_val), - collections=[vars_collection], - trainable=False) - - def _quantizable_concat(self, - inputs, - axis, - is_training, - is_quantized=True, - default_min=0, - default_max=6, - ema_decay=0.999, - scope='quantized_concat'): - """Concat replacement with quantization option. - - Allows concat inputs to share the same min max ranges, - from experimental/gazelle/synthetic/model/tpu/utils.py. - - Args: - inputs: list of tensors to concatenate. - axis: dimension along which to concatenate. - is_training: true if the graph is a training graph. - is_quantized: flag to enable/disable quantization. - default_min: default min value for fake quant op. - default_max: default max value for fake quant op. - ema_decay: the moving average decay for the quantization variables. - scope: Optional scope for variable_scope. - - Returns: - Tensor resulting from concatenation of input tensors - """ - if is_quantized: - with tf.variable_scope(scope): - min_var = self._quant_var('min', default_min) - max_var = self._quant_var('max', default_max) - if not is_training: - # If we are building an eval graph just use the values in the - # variables. - quant_inputs = [ - tf.fake_quant_with_min_max_vars(t, min_var, max_var) - for t in inputs - ] - else: - concat_tensors = tf.concat(inputs, axis=axis) - tf.logging.info('concat_tensors: {}'.format(concat_tensors)) - # TFLite requires that 0.0 is always in the [min; max] range. - range_min = tf.minimum( - tf.reduce_min(concat_tensors), 0.0, name='SafeQuantRangeMin') - range_max = tf.maximum( - tf.reduce_max(concat_tensors), 0.0, name='SafeQuantRangeMax') - # Otherwise we need to keep track of the moving averages of the min - # and of the elements of the input tensor max. - min_val = moving_averages.assign_moving_average( - min_var, range_min, ema_decay, name='AssignMinEma') - max_val = moving_averages.assign_moving_average( - max_var, range_max, ema_decay, name='AssignMaxEma') - quant_inputs = [ - tf.fake_quant_with_min_max_vars(t, min_val, max_val) - for t in inputs - ] - outputs = tf.concat(quant_inputs, axis=axis) - else: - outputs = tf.concat(inputs, axis=axis) - return outputs - - def _expanded_conv(self, net, num_filters, expansion_rates, kernel_size, - stride, scope): - """Expanded convolution.""" - expanded_num_filters = num_filters * expansion_rates - add_fixed_padding = self._use_explicit_padding and stride > 1 - padding = 'VALID' if add_fixed_padding else 'SAME' - net = slim.conv2d( - net, - expanded_num_filters, [1, 1], - activation_fn=self._activation_fn, - normalizer_fn=self._normalization_fn, - padding=padding, - scope=scope + '/expansion') - net = slim.separable_conv2d( - ops.fixed_padding(net, kernel_size) if add_fixed_padding else net, - num_outputs=None, - kernel_size=kernel_size, - activation_fn=self._activation_fn, - normalizer_fn=self._normalization_fn, - stride=stride, - padding=padding, - scope=scope + '/depthwise') - net = slim.conv2d( - net, - num_filters, [1, 1], - activation_fn=tf.identity, - normalizer_fn=self._normalization_fn, - padding=padding, - scope=scope + '/projection') - return net - - def _slice_shape_along_axis(self, shape, axis, groups): - """Returns the shape after slicing into groups along the axis.""" - if isinstance(shape, tf.TensorShape): - shape_as_list = shape.as_list() - if shape_as_list[axis] % groups != 0: - raise ValueError('Dimension {} must be divisible by {} groups'.format( - shape_as_list[axis], groups)) - shape_as_list[axis] = shape_as_list[axis] // groups - return tf.TensorShape(shape_as_list) - elif isinstance(shape, tf.Tensor) and shape.shape.rank == 1: - shape_as_list = tf.unstack(shape) - shape_as_list[axis] = shape_as_list[axis] // groups - return tf.stack(shape_as_list) - else: - raise ValueError( - 'Shape should be a TensorShape or rank-1 Tensor, but got: {}'.format( - shape)) - - def _ibn_fused_grouped(self, net, num_filters, expansion_rates, kernel_size, - stride, groups, scope): - """Fused grouped IBN convolution.""" - add_fixed_padding = self._use_explicit_padding and stride > 1 - padding = 'VALID' if add_fixed_padding else 'SAME' - slice_shape = self._slice_shape_along_axis(net.shape, -1, groups) - slice_begin = [0] * net.shape.rank - slice_outputs = [] - output_filters_per_group = net.shape[-1] // groups - expanded_num_filters_per_group = output_filters_per_group * expansion_rates - for idx in range(groups): - slice_input = tf.slice(net, slice_begin, slice_shape) - if isinstance(slice_shape, tf.TensorShape): - slice_begin[-1] += slice_shape.as_list()[-1] - else: - slice_begin[-1] += slice_shape[-1] - slice_outputs.append( - slim.conv2d( - ops.fixed_padding(slice_input, kernel_size) - if add_fixed_padding else slice_input, - expanded_num_filters_per_group, - kernel_size, - activation_fn=self._activation_fn, - normalizer_fn=self._normalization_fn, - stride=stride, - padding=padding, - scope='{}/{}_{}'.format(scope, 'slice', idx))) - # Make inputs to the concat share the same quantization variables. - net = self._quantizable_concat( - slice_outputs, - -1, - self._is_training, - scope='{}/{}'.format(scope, 'concat')) - net = slim.conv2d( - net, - num_filters, [1, 1], - activation_fn=tf.identity, - normalizer_fn=self._normalization_fn, - padding=padding, - scope=scope + '/projection') - return net - - def _sep_conv(self, net, num_filters, kernel_size, stride, scope): - """Depthwise Separable convolution.""" - add_fixed_padding = self._use_explicit_padding and stride > 1 - padding = 'VALID' if add_fixed_padding else 'SAME' - net = slim.separable_conv2d( - ops.fixed_padding(net, kernel_size) if add_fixed_padding else net, - num_outputs=None, - kernel_size=kernel_size, - activation_fn=None, - normalizer_fn=None, - stride=stride, - padding=padding, - scope=scope + '/depthwise') - net = slim.conv2d( - net, - num_filters, [1, 1], - activation_fn=self._activation_fn, - normalizer_fn=self._normalization_fn, - padding=padding, - scope=scope + '/pointwise') - return net - - def _upsample(self, net, num_filters, upsample_ratio, scope): - """Perform 1x1 conv then nearest neighbor upsampling.""" - node_pre_up = slim.conv2d( - net, - num_filters, [1, 1], - activation_fn=tf.identity, - normalizer_fn=self._normalization_fn, - padding='SAME', - scope=scope + '/1x1_before_upsample') - if self._use_native_resize_op: - with tf.name_scope(scope + '/nearest_neighbor_upsampling'): - input_shape = shape_utils.combined_static_and_dynamic_shape(node_pre_up) - node_up = tf.image.resize_nearest_neighbor( - node_pre_up, - [input_shape[1] * upsample_ratio, input_shape[2] * upsample_ratio]) - else: - node_up = ops.nearest_neighbor_upsampling( - node_pre_up, scale=upsample_ratio) - - return node_up - - def _downsample(self, net, num_filters, downsample_ratio, scope): - """Perform maxpool downsampling then 1x1 conv.""" - add_fixed_padding = self._use_explicit_padding and downsample_ratio > 1 - padding = 'VALID' if add_fixed_padding else 'SAME' - node_down = slim.max_pool2d( - ops.fixed_padding(net, downsample_ratio + - 1) if add_fixed_padding else net, - [downsample_ratio + 1, downsample_ratio + 1], - stride=[downsample_ratio, downsample_ratio], - padding=padding, - scope=scope + '/maxpool_downsampling') - node_after_down = slim.conv2d( - node_down, - num_filters, [1, 1], - activation_fn=tf.identity, - normalizer_fn=self._normalization_fn, - padding=padding, - scope=scope + '/1x1_after_downsampling') - return node_after_down - - def _no_resample(self, net, num_filters, scope): - return slim.conv2d( - net, - num_filters, [1, 1], - activation_fn=tf.identity, - normalizer_fn=self._normalization_fn, - padding='SAME', - scope=scope + '/1x1_no_resampling') - - def _spaghetti_node(self, node, scope): - """Spaghetti node.""" - node_spec = self._node_specs.nodes[node] - - # Make spaghetti edges - edge_outputs = [] - edge_min_level = 100 # Currently we don't have any level over 7. - edge_output_shape = None - for edge in node_spec.edges: - if isinstance(edge, SpaghettiPassthroughEdge): - assert len(node_spec.edges) == 1, len(node_spec.edges) - edge_outputs.append(self._nodes[edge.input]) - elif isinstance(edge, SpaghettiResampleEdge): - edge_outputs.append( - self._spaghetti_edge(node, edge.input, - 'edge_{}_{}'.format(edge.input, node))) - if edge_min_level > self._node_specs.nodes[edge.input].level: - edge_min_level = self._node_specs.nodes[edge.input].level - edge_output_shape = tf.shape(edge_outputs[-1]) - else: - raise ValueError('Unknown edge type {}'.format(edge)) - - if len(edge_outputs) == 1: - # When edge_outputs' length is 1, it is passthrough edge. - net = edge_outputs[-1] - else: - # When edge_outputs' length is over 1, need to crop and then add edges. - net = edge_outputs[0][:, :edge_output_shape[1], :edge_output_shape[2], :] - for edge_output in edge_outputs[1:]: - net += edge_output[:, :edge_output_shape[1], :edge_output_shape[2], :] - net = self._activation_fn(net) - - # Make spaghetti node - for idx, layer_spec in enumerate(node_spec.layers): - if isinstance(layer_spec, IbnOp): - net_exp = self._expanded_conv(net, node_spec.num_filters, - layer_spec.expansion_rate, - layer_spec.kernel_size, layer_spec.stride, - '{}_{}'.format(scope, idx)) - elif isinstance(layer_spec, IbnFusedGrouped): - net_exp = self._ibn_fused_grouped(net, node_spec.num_filters, - layer_spec.expansion_rate, - layer_spec.kernel_size, - layer_spec.stride, layer_spec.groups, - '{}_{}'.format(scope, idx)) - elif isinstance(layer_spec, SepConvOp): - net_exp = self._sep_conv(net, node_spec.num_filters, - layer_spec.kernel_size, layer_spec.stride, - '{}_{}'.format(scope, idx)) - else: - raise ValueError('Unsupported layer_spec: {}'.format(layer_spec)) - # Skip connection for all layers other than the first in a node. - net = net_exp + net if layer_spec.has_residual else net_exp - self._nodes[node] = net - - def _spaghetti_edge(self, curr_node, prev_node, scope): - """Create an edge between curr_node and prev_node.""" - curr_spec = self._node_specs.nodes[curr_node] - prev_spec = self._node_specs.nodes[prev_node] - if curr_spec.level < prev_spec.level: - # upsample - output = self._upsample(self._nodes[prev_node], curr_spec.num_filters, - 2**(prev_spec.level - curr_spec.level), scope) - elif curr_spec.level > prev_spec.level: - # downsample - output = self._downsample(self._nodes[prev_node], curr_spec.num_filters, - 2**(curr_spec.level - prev_spec.level), scope) - else: - # 1x1 - output = self._no_resample(self._nodes[prev_node], curr_spec.num_filters, - scope) - return output - - def _spaghetti_stem_node(self, net, node, scope): - stem_spec = self._node_specs.nodes[node] - kernel_size = stem_spec.kernel_size - padding = 'VALID' if self._use_explicit_padding else 'SAME' - self._nodes[node] = slim.conv2d( - ops.fixed_padding(net, kernel_size) - if self._use_explicit_padding else net, - stem_spec.num_filters, [kernel_size, kernel_size], - stride=2, - activation_fn=self._activation_fn, - normalizer_fn=self._normalization_fn, - padding=padding, - scope=scope + '/stem') - - def apply(self, net, scope='spaghetti_net'): - """Apply the SpaghettiNet to the input and return nodes in outputs.""" - for node, node_spec in self._node_specs.nodes.items(): - if isinstance(node_spec, SpaghettiStemNode): - self._spaghetti_stem_node(net, node, '{}/stem_node'.format(scope)) - elif isinstance(node_spec, SpaghettiNode): - self._spaghetti_node(node, '{}/{}'.format(scope, node)) - else: - raise ValueError('Unknown node {}: {}'.format(node, node_spec)) - - return [self._nodes[x] for x in self._node_specs.outputs] - - -def _spaghettinet_edgetpu_s(): - """Architecture definition for SpaghettiNet-EdgeTPU-S.""" - nodes = collections.OrderedDict() - outputs = ['c0n1', 'c0n2', 'c0n3', 'c0n4', 'c0n5'] - nodes['s0'] = SpaghettiStemNode(kernel_size=5, num_filters=24) - nodes['n0'] = SpaghettiNode( - num_filters=48, - level=2, - layers=[ - IbnFusedGrouped(3, 8, 2, 3, False), - ], - edges=[SpaghettiPassthroughEdge(input='s0')]) - nodes['n1'] = SpaghettiNode( - num_filters=64, - level=3, - layers=[ - IbnFusedGrouped(3, 4, 2, 4, False), - IbnFusedGrouped(3, 4, 1, 4, True), - IbnFusedGrouped(3, 4, 1, 4, True), - ], - edges=[SpaghettiPassthroughEdge(input='n0')]) - nodes['n2'] = SpaghettiNode( - num_filters=72, - level=4, - layers=[ - IbnOp(3, 8, 2, False), - IbnFusedGrouped(3, 8, 1, 4, True), - IbnOp(3, 8, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n1')]) - nodes['n3'] = SpaghettiNode( - num_filters=88, - level=5, - layers=[ - IbnOp(3, 8, 2, False), - IbnOp(3, 8, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n2')]) - nodes['n4'] = SpaghettiNode( - num_filters=88, - level=6, - layers=[ - IbnOp(3, 8, 2, False), - SepConvOp(5, 1, True), - SepConvOp(5, 1, True), - SepConvOp(5, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n3')]) - nodes['n5'] = SpaghettiNode( - num_filters=88, - level=7, - layers=[ - SepConvOp(5, 2, False), - SepConvOp(3, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n4')]) - nodes['c0n0'] = SpaghettiNode( - num_filters=144, - level=5, - layers=[ - IbnOp(3, 4, 1, False), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n3'), - SpaghettiResampleEdge(input='n4') - ]) - nodes['c0n1'] = SpaghettiNode( - num_filters=120, - level=4, - layers=[ - IbnOp(3, 8, 1, False), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n2'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n2'] = SpaghettiNode( - num_filters=168, - level=5, - layers=[ - IbnOp(3, 4, 1, False), - ], - edges=[ - SpaghettiResampleEdge(input='c0n1'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n3'] = SpaghettiNode( - num_filters=136, - level=6, - layers=[ - IbnOp(3, 4, 1, False), - SepConvOp(3, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n5'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n4'] = SpaghettiNode( - num_filters=136, - level=7, - layers=[ - IbnOp(3, 4, 1, False), - ], - edges=[ - SpaghettiResampleEdge(input='n5'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n5'] = SpaghettiNode( - num_filters=64, - level=8, - layers=[ - SepConvOp(3, 1, False), - SepConvOp(3, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='c0n4')]) - node_specs = SpaghettiNodeSpecs(nodes=nodes, outputs=outputs) - return node_specs - - -def _spaghettinet_edgetpu_m(): - """Architecture definition for SpaghettiNet-EdgeTPU-M.""" - nodes = collections.OrderedDict() - outputs = ['c0n1', 'c0n2', 'c0n3', 'c0n4', 'c0n5'] - nodes['s0'] = SpaghettiStemNode(kernel_size=5, num_filters=24) - nodes['n0'] = SpaghettiNode( - num_filters=48, - level=2, - layers=[ - IbnFusedGrouped(3, 8, 2, 3, False), - ], - edges=[SpaghettiPassthroughEdge(input='s0')]) - nodes['n1'] = SpaghettiNode( - num_filters=64, - level=3, - layers=[ - IbnFusedGrouped(3, 8, 2, 4, False), - IbnFusedGrouped(3, 4, 1, 4, True), - IbnFusedGrouped(3, 4, 1, 4, True), - IbnFusedGrouped(3, 4, 1, 4, True), - ], - edges=[SpaghettiPassthroughEdge(input='n0')]) - nodes['n2'] = SpaghettiNode( - num_filters=72, - level=4, - layers=[ - IbnOp(3, 8, 2, False), - IbnFusedGrouped(3, 8, 1, 4, True), - IbnOp(3, 8, 1, True), - IbnOp(3, 8, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n1')]) - nodes['n3'] = SpaghettiNode( - num_filters=96, - level=5, - layers=[ - IbnOp(3, 8, 2, False), - IbnOp(3, 8, 1, True), - IbnOp(3, 8, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n2')]) - nodes['n4'] = SpaghettiNode( - num_filters=104, - level=6, - layers=[ - IbnOp(3, 8, 2, False), - IbnOp(3, 4, 1, True), - SepConvOp(5, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n3')]) - nodes['n5'] = SpaghettiNode( - num_filters=56, - level=7, - layers=[ - SepConvOp(5, 2, False), - SepConvOp(3, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n4')]) - nodes['c0n0'] = SpaghettiNode( - num_filters=152, - level=5, - layers=[ - IbnOp(3, 8, 1, False), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n3'), - SpaghettiResampleEdge(input='n4') - ]) - nodes['c0n1'] = SpaghettiNode( - num_filters=120, - level=4, - layers=[ - IbnOp(3, 8, 1, False), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n2'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n2'] = SpaghettiNode( - num_filters=168, - level=5, - layers=[ - IbnOp(3, 4, 1, False), - SepConvOp(3, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='c0n1'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n3'] = SpaghettiNode( - num_filters=136, - level=6, - layers=[ - SepConvOp(3, 1, False), - SepConvOp(3, 1, True), - SepConvOp(3, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n5'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n4'] = SpaghettiNode( - num_filters=136, - level=7, - layers=[ - IbnOp(3, 4, 1, False), - SepConvOp(5, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n5'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n5'] = SpaghettiNode( - num_filters=64, - level=8, - layers=[ - SepConvOp(3, 1, False), - SepConvOp(3, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='c0n4')]) - node_specs = SpaghettiNodeSpecs(nodes=nodes, outputs=outputs) - return node_specs - - -def _spaghettinet_edgetpu_l(): - """Architecture definition for SpaghettiNet-EdgeTPU-L.""" - nodes = collections.OrderedDict() - outputs = ['c0n1', 'c0n2', 'c0n3', 'c0n4', 'c0n5'] - nodes['s0'] = SpaghettiStemNode(kernel_size=5, num_filters=24) - nodes['n0'] = SpaghettiNode( - num_filters=48, - level=2, - layers=[ - IbnFusedGrouped(3, 8, 2, 3, False), - ], - edges=[SpaghettiPassthroughEdge(input='s0')]) - nodes['n1'] = SpaghettiNode( - num_filters=64, - level=3, - layers=[ - IbnFusedGrouped(3, 8, 2, 4, False), - IbnFusedGrouped(3, 8, 1, 4, True), - IbnFusedGrouped(3, 8, 1, 4, True), - IbnFusedGrouped(3, 4, 1, 4, True), - ], - edges=[SpaghettiPassthroughEdge(input='n0')]) - nodes['n2'] = SpaghettiNode( - num_filters=80, - level=4, - layers=[ - IbnOp(3, 8, 2, False), - IbnOp(3, 8, 1, True), - IbnOp(3, 8, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n1')]) - nodes['n3'] = SpaghettiNode( - num_filters=104, - level=5, - layers=[ - IbnOp(3, 8, 2, False), - IbnOp(3, 8, 1, True), - IbnOp(3, 8, 1, True), - IbnOp(3, 8, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n2')]) - nodes['n4'] = SpaghettiNode( - num_filters=88, - level=6, - layers=[ - IbnOp(3, 8, 2, False), - IbnOp(5, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 8, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n3')]) - nodes['n5'] = SpaghettiNode( - num_filters=56, - level=7, - layers=[ - IbnOp(5, 4, 2, False), - SepConvOp(5, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='n4')]) - nodes['c0n0'] = SpaghettiNode( - num_filters=160, - level=5, - layers=[ - IbnOp(3, 8, 1, False), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n3'), - SpaghettiResampleEdge(input='n4') - ]) - nodes['c0n1'] = SpaghettiNode( - num_filters=120, - level=4, - layers=[ - IbnOp(3, 8, 1, False), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 8, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n2'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n2'] = SpaghettiNode( - num_filters=168, - level=5, - layers=[ - IbnOp(3, 4, 1, False), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - IbnOp(3, 4, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='c0n1'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n3'] = SpaghettiNode( - num_filters=112, - level=6, - layers=[ - IbnOp(3, 8, 1, False), - IbnOp(3, 4, 1, True), - SepConvOp(3, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n5'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n4'] = SpaghettiNode( - num_filters=128, - level=7, - layers=[ - IbnOp(3, 4, 1, False), - IbnOp(3, 4, 1, True), - ], - edges=[ - SpaghettiResampleEdge(input='n5'), - SpaghettiResampleEdge(input='c0n0') - ]) - nodes['c0n5'] = SpaghettiNode( - num_filters=64, - level=8, - layers=[ - SepConvOp(5, 1, False), - SepConvOp(5, 1, True), - ], - edges=[SpaghettiPassthroughEdge(input='c0n4')]) - node_specs = SpaghettiNodeSpecs(nodes=nodes, outputs=outputs) - return node_specs - - -def lookup_spaghetti_arch(arch): - """Lookup table for the nodes structure for spaghetti nets.""" - if arch == 'spaghettinet_edgetpu_s': - return _spaghettinet_edgetpu_s() - elif arch == 'spaghettinet_edgetpu_m': - return _spaghettinet_edgetpu_m() - elif arch == 'spaghettinet_edgetpu_l': - return _spaghettinet_edgetpu_l() - else: - raise ValueError('Unknown architecture {}'.format(arch)) - - -class SSDSpaghettinetFeatureExtractor(ssd_meta_arch.SSDFeatureExtractor): - """SSD Feature Extractor using Custom Architecture.""" - - def __init__( - self, - is_training, - depth_multiplier, - min_depth, - pad_to_multiple, - conv_hyperparams_fn, - spaghettinet_arch_name='spaghettinet_edgetpu_m', - use_explicit_padding=False, - reuse_weights=False, - use_depthwise=False, - override_base_feature_extractor_hyperparams=False, - ): - """SSD FPN feature extractor based on Mobilenet v2 architecture. - - Args: - is_training: whether the network is in training mode. - depth_multiplier: Not used in SpaghettiNet. - min_depth: Not used in SpaghettiNet. - pad_to_multiple: Not used in SpaghettiNet. - conv_hyperparams_fn: Not used in SpaghettiNet. - spaghettinet_arch_name: name of the specific architecture. - use_explicit_padding: Whether to use explicit padding when extracting - features. Default is False. - reuse_weights: Not used in SpaghettiNet. - use_depthwise: Not used in SpaghettiNet. - override_base_feature_extractor_hyperparams: Not used in SpaghettiNet. - """ - super(SSDSpaghettinetFeatureExtractor, self).__init__( - is_training=is_training, - use_explicit_padding=use_explicit_padding, - depth_multiplier=depth_multiplier, - min_depth=min_depth, - pad_to_multiple=pad_to_multiple, - conv_hyperparams_fn=conv_hyperparams_fn, - reuse_weights=reuse_weights, - use_depthwise=use_depthwise, - override_base_feature_extractor_hyperparams=override_base_feature_extractor_hyperparams - ) - self._spaghettinet_arch_name = spaghettinet_arch_name - self._use_native_resize_op = False if is_training else True - - def preprocess(self, resized_inputs): - """SSD preprocessing. - - Maps pixel values to the range [-1, 1]. - - Args: - resized_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - """ - return (2.0 / 255.0) * resized_inputs - 1.0 - - def extract_features(self, preprocessed_inputs): - """Extract features from preprocessed inputs. - - Args: - preprocessed_inputs: a [batch, height, width, channels] float tensor - representing a batch of images. - - Returns: - feature_maps: a list of tensors where the ith tensor has shape - [batch, height_i, width_i, depth_i] - """ - preprocessed_inputs = shape_utils.check_min_image_dim( - 33, preprocessed_inputs) - nodes_dict = lookup_spaghetti_arch(self._spaghettinet_arch_name) - - with tf.variable_scope( - self._spaghettinet_arch_name, reuse=self._reuse_weights): - with slim.arg_scope([slim.conv2d], - weights_initializer=tf.truncated_normal_initializer( - mean=0.0, stddev=0.03), - weights_regularizer=slim.l2_regularizer(1e-5)): - with slim.arg_scope([slim.separable_conv2d], - weights_initializer=tf.truncated_normal_initializer( - mean=0.0, stddev=0.03), - weights_regularizer=slim.l2_regularizer(1e-5)): - with slim.arg_scope([slim.batch_norm], - is_training=self._is_training, - epsilon=0.001, - decay=0.97, - center=True, - scale=True): - spaghetti_net = SpaghettiNet( - node_specs=nodes_dict, - is_training=self._is_training, - use_native_resize_op=self._use_native_resize_op, - use_explicit_padding=self._use_explicit_padding, - name=self._spaghettinet_arch_name) - feature_maps = spaghetti_net.apply(preprocessed_inputs) - return feature_maps diff --git a/research/object_detection/models/ssd_spaghettinet_feature_extractor_tf1_test.py b/research/object_detection/models/ssd_spaghettinet_feature_extractor_tf1_test.py deleted file mode 100644 index 76ced60aa6b..00000000000 --- a/research/object_detection/models/ssd_spaghettinet_feature_extractor_tf1_test.py +++ /dev/null @@ -1,108 +0,0 @@ -# Copyright 2020 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== -"""Tests for ssd_spaghettinet_feature_extractor.""" -import unittest -import tensorflow.compat.v1 as tf - -from object_detection.models import ssd_feature_extractor_test -from object_detection.models import ssd_spaghettinet_feature_extractor -from object_detection.utils import tf_version - -try: - from tensorflow.contrib import quantize as contrib_quantize # pylint: disable=g-import-not-at-top -except: # pylint: disable=bare-except - pass - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class SSDSpaghettiNetFeatureExtractorTest( - ssd_feature_extractor_test.SsdFeatureExtractorTestBase): - - def _create_feature_extractor(self, arch_name, is_training=True): - return ssd_spaghettinet_feature_extractor.SSDSpaghettinetFeatureExtractor( - is_training=is_training, - spaghettinet_arch_name=arch_name, - depth_multiplier=1.0, - min_depth=4, - pad_to_multiple=1, - conv_hyperparams_fn=self.conv_hyperparams_fn) - - def _test_spaghettinet_returns_correct_shapes(self, arch_name, - expected_feature_map_shapes): - image = tf.random.normal((1, 320, 320, 3)) - feature_extractor = self._create_feature_extractor(arch_name) - feature_maps = feature_extractor.extract_features(image) - - self.assertEqual(len(expected_feature_map_shapes), len(feature_maps)) - for expected_shape, x in zip(expected_feature_map_shapes, feature_maps): - self.assertTrue(x.shape.is_compatible_with(expected_shape)) - - def test_spaghettinet_edgetpu_s(self): - expected_feature_map_shapes = [(1, 20, 20, 120), (1, 10, 10, 168), - (1, 5, 5, 136), (1, 3, 3, 136), - (1, 3, 3, 64)] - self._test_spaghettinet_returns_correct_shapes('spaghettinet_edgetpu_s', - expected_feature_map_shapes) - - def test_spaghettinet_edgetpu_m(self): - expected_feature_map_shapes = [(1, 20, 20, 120), (1, 10, 10, 168), - (1, 5, 5, 136), (1, 3, 3, 136), - (1, 3, 3, 64)] - self._test_spaghettinet_returns_correct_shapes('spaghettinet_edgetpu_m', - expected_feature_map_shapes) - - def test_spaghettinet_edgetpu_l(self): - expected_feature_map_shapes = [(1, 20, 20, 120), (1, 10, 10, 168), - (1, 5, 5, 112), (1, 3, 3, 128), - (1, 3, 3, 64)] - self._test_spaghettinet_returns_correct_shapes('spaghettinet_edgetpu_l', - expected_feature_map_shapes) - - def _check_quantization(self, model_fn): - checkpoint_dir = self.get_temp_dir() - - with tf.Graph().as_default() as training_graph: - model_fn(is_training=True) - contrib_quantize.experimental_create_training_graph(training_graph) - with self.session(graph=training_graph) as sess: - sess.run(tf.global_variables_initializer()) - tf.train.Saver().save(sess, checkpoint_dir) - - with tf.Graph().as_default() as eval_graph: - model_fn(is_training=False) - contrib_quantize.experimental_create_eval_graph(eval_graph) - with self.session(graph=eval_graph) as sess: - tf.train.Saver().restore(sess, checkpoint_dir) - - def _test_spaghettinet_quantization(self, arch_name): - def model_fn(is_training): - image = tf.random.normal((1, 320, 320, 3)) - feature_extractor = self._create_feature_extractor( - arch_name, is_training=is_training) - feature_extractor.extract_features(image) - self._check_quantization(model_fn) - - def test_spaghettinet_edgetpu_s_quantization(self): - self._test_spaghettinet_quantization('spaghettinet_edgetpu_s') - - def test_spaghettinet_edgetpu_m_quantization(self): - self._test_spaghettinet_quantization('spaghettinet_edgetpu_m') - - def test_spaghettinet_edgetpu_l_quantization(self): - self._test_spaghettinet_quantization('spaghettinet_edgetpu_l') - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/packages/tf1/setup.py b/research/object_detection/packages/tf1/setup.py deleted file mode 100644 index a40a368a6f5..00000000000 --- a/research/object_detection/packages/tf1/setup.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Setup script for object_detection with TF1.0.""" -import os -from setuptools import find_packages -from setuptools import setup - -REQUIRED_PACKAGES = ['pillow', 'lxml', 'matplotlib', 'Cython', - 'contextlib2', 'tf-slim', 'six', 'pycocotools', 'lvis', - 'scipy', 'pandas'] - -setup( - name='object_detection', - version='0.1', - install_requires=REQUIRED_PACKAGES, - include_package_data=True, - packages=( - [p for p in find_packages() if p.startswith('object_detection')] + - find_packages(where=os.path.join('.', 'slim'))), - package_dir={ - 'datasets': os.path.join('slim', 'datasets'), - 'nets': os.path.join('slim', 'nets'), - 'preprocessing': os.path.join('slim', 'preprocessing'), - 'deployment': os.path.join('slim', 'deployment'), - 'scripts': os.path.join('slim', 'scripts'), - }, - description='Tensorflow Object Detection Library with TF1.0', - python_requires='>3.6', -) diff --git a/research/object_detection/packages/tf2/setup.py b/research/object_detection/packages/tf2/setup.py deleted file mode 100644 index aeb7ca54bf7..00000000000 --- a/research/object_detection/packages/tf2/setup.py +++ /dev/null @@ -1,45 +0,0 @@ -"""Setup script for object_detection with TF2.0.""" -import os -from setuptools import find_packages -from setuptools import setup - -REQUIRED_PACKAGES = [ - # Required for apache-beam with PY3 - 'avro-python3', - 'apache-beam', - 'pillow', - 'lxml', - 'matplotlib', - 'Cython', - 'contextlib2', - 'tf-slim', - 'six', - 'pycocotools', - 'lvis', - 'scipy', - 'pandas', - 'tf-models-official>=2.5.1', - 'tensorflow_io', - 'keras', - 'pyparsing==2.4.7', # TODO(b/204103388) - 'sacrebleu<=2.2.0' # https://github.com/mjpost/sacrebleu/issues/209 -] - -setup( - name='object_detection', - version='0.1', - install_requires=REQUIRED_PACKAGES, - include_package_data=True, - packages=( - [p for p in find_packages() if p.startswith('object_detection')] + - find_packages(where=os.path.join('.', 'slim'))), - package_dir={ - 'datasets': os.path.join('slim', 'datasets'), - 'nets': os.path.join('slim', 'nets'), - 'preprocessing': os.path.join('slim', 'preprocessing'), - 'deployment': os.path.join('slim', 'deployment'), - 'scripts': os.path.join('slim', 'scripts'), - }, - description='Tensorflow Object Detection Library', - python_requires='>3.6', -) diff --git a/research/object_detection/predictors/__init__.py b/research/object_detection/predictors/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/predictors/convolutional_box_predictor.py b/research/object_detection/predictors/convolutional_box_predictor.py deleted file mode 100644 index 9996ff194d0..00000000000 --- a/research/object_detection/predictors/convolutional_box_predictor.py +++ /dev/null @@ -1,420 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Convolutional Box Predictors with and without weight sharing.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import functools -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf -import tf_slim as slim -from object_detection.core import box_predictor -from object_detection.utils import shape_utils -from object_detection.utils import static_shape - -BOX_ENCODINGS = box_predictor.BOX_ENCODINGS -CLASS_PREDICTIONS_WITH_BACKGROUND = ( - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND) -MASK_PREDICTIONS = box_predictor.MASK_PREDICTIONS - - -class _NoopVariableScope(object): - """A dummy class that does not push any scope.""" - - def __enter__(self): - return None - - def __exit__(self, exc_type, exc_value, traceback): - return False - - -class ConvolutionalBoxPredictor(box_predictor.BoxPredictor): - """Convolutional Box Predictor. - - Optionally add an intermediate 1x1 convolutional layer after features and - predict in parallel branches box_encodings and - class_predictions_with_background. - - Currently this box predictor assumes that predictions are "shared" across - classes --- that is each anchor makes box predictions which do not depend - on class. - """ - - def __init__(self, - is_training, - num_classes, - box_prediction_head, - class_prediction_head, - other_heads, - conv_hyperparams_fn, - num_layers_before_predictor, - min_depth, - max_depth): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - box_prediction_head: The head that predicts the boxes. - class_prediction_head: The head that predicts the classes. - other_heads: A dictionary mapping head names to convolutional - head classes. - conv_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for convolution ops. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - min_depth: Minimum feature depth prior to predicting box encodings - and class predictions. - max_depth: Maximum feature depth prior to predicting box encodings - and class predictions. If max_depth is set to 0, no additional - feature map will be inserted before location and class predictions. - - Raises: - ValueError: if min_depth > max_depth. - """ - super(ConvolutionalBoxPredictor, self).__init__(is_training, num_classes) - self._box_prediction_head = box_prediction_head - self._class_prediction_head = class_prediction_head - self._other_heads = other_heads - self._conv_hyperparams_fn = conv_hyperparams_fn - self._min_depth = min_depth - self._max_depth = max_depth - self._num_layers_before_predictor = num_layers_before_predictor - - @property - def num_classes(self): - return self._num_classes - - def _predict(self, image_features, num_predictions_per_location_list): - """Computes encoded object locations and corresponding confidences. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - num_predictions_per_location_list: A list of integers representing the - number of box predictions to be made per spatial location for each - feature map. - - Returns: - A dictionary containing: - box_encodings: A list of float tensors of shape - [batch_size, num_anchors_i, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. Each entry in the - list corresponds to a feature map in the input `image_features` list. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - (optional) Predictions from other heads. - """ - predictions = { - BOX_ENCODINGS: [], - CLASS_PREDICTIONS_WITH_BACKGROUND: [], - } - for head_name in self._other_heads.keys(): - predictions[head_name] = [] - # TODO(rathodv): Come up with a better way to generate scope names - # in box predictor once we have time to retrain all models in the zoo. - # The following lines create scope names to be backwards compatible with the - # existing checkpoints. - box_predictor_scopes = [_NoopVariableScope()] - if len(image_features) > 1: - box_predictor_scopes = [ - tf.variable_scope('BoxPredictor_{}'.format(i)) - for i in range(len(image_features)) - ] - for (image_feature, - num_predictions_per_location, box_predictor_scope) in zip( - image_features, num_predictions_per_location_list, - box_predictor_scopes): - net = image_feature - with box_predictor_scope: - with slim.arg_scope(self._conv_hyperparams_fn()): - with slim.arg_scope([slim.dropout], is_training=self._is_training): - # Add additional conv layers before the class predictor. - features_depth = static_shape.get_depth(image_feature.get_shape()) - depth = max(min(features_depth, self._max_depth), self._min_depth) - tf.logging.info('depth of additional conv before box predictor: {}'. - format(depth)) - if depth > 0 and self._num_layers_before_predictor > 0: - for i in range(self._num_layers_before_predictor): - net = slim.conv2d( - net, - depth, [1, 1], - reuse=tf.AUTO_REUSE, - scope='Conv2d_%d_1x1_%d' % (i, depth)) - sorted_keys = sorted(self._other_heads.keys()) - sorted_keys.append(BOX_ENCODINGS) - sorted_keys.append(CLASS_PREDICTIONS_WITH_BACKGROUND) - for head_name in sorted_keys: - if head_name == BOX_ENCODINGS: - head_obj = self._box_prediction_head - elif head_name == CLASS_PREDICTIONS_WITH_BACKGROUND: - head_obj = self._class_prediction_head - else: - head_obj = self._other_heads[head_name] - prediction = head_obj.predict( - features=net, - num_predictions_per_location=num_predictions_per_location) - predictions[head_name].append(prediction) - return predictions - - -# TODO(rathodv): Replace with slim.arg_scope_func_key once its available -# externally. -def _arg_scope_func_key(op): - """Returns a key that can be used to index arg_scope dictionary.""" - return getattr(op, '_key_op', str(op)) - - -# TODO(rathodv): Merge the implementation with ConvolutionalBoxPredictor above -# since they are very similar. -class WeightSharedConvolutionalBoxPredictor(box_predictor.BoxPredictor): - """Convolutional Box Predictor with weight sharing. - - Defines the box predictor as defined in - https://arxiv.org/abs/1708.02002. This class differs from - ConvolutionalBoxPredictor in that it shares weights and biases while - predicting from different feature maps. However, batch_norm parameters are not - shared because the statistics of the activations vary among the different - feature maps. - - Also note that separate multi-layer towers are constructed for the box - encoding and class predictors respectively. - """ - - def __init__(self, - is_training, - num_classes, - box_prediction_head, - class_prediction_head, - other_heads, - conv_hyperparams_fn, - depth, - num_layers_before_predictor, - kernel_size=3, - apply_batch_norm=False, - share_prediction_tower=False, - use_depthwise=False): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - box_prediction_head: The head that predicts the boxes. - class_prediction_head: The head that predicts the classes. - other_heads: A dictionary mapping head names to convolutional - head classes. - conv_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for convolution ops. - depth: depth of conv layers. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - kernel_size: Size of final convolution kernel. - apply_batch_norm: Whether to apply batch normalization to conv layers in - this predictor. - share_prediction_tower: Whether to share the multi-layer tower among box - prediction head, class prediction head and other heads. - use_depthwise: Whether to use depthwise separable conv2d instead of - regular conv2d. - """ - super(WeightSharedConvolutionalBoxPredictor, self).__init__(is_training, - num_classes) - self._box_prediction_head = box_prediction_head - self._class_prediction_head = class_prediction_head - self._other_heads = other_heads - self._conv_hyperparams_fn = conv_hyperparams_fn - self._depth = depth - self._num_layers_before_predictor = num_layers_before_predictor - self._kernel_size = kernel_size - self._apply_batch_norm = apply_batch_norm - self._share_prediction_tower = share_prediction_tower - self._use_depthwise = use_depthwise - - @property - def num_classes(self): - return self._num_classes - - def _insert_additional_projection_layer(self, image_feature, - inserted_layer_counter, - target_channel): - if inserted_layer_counter < 0: - return image_feature, inserted_layer_counter - image_feature = slim.conv2d( - image_feature, - target_channel, [1, 1], - stride=1, - padding='SAME', - activation_fn=None, - normalizer_fn=(tf.identity if self._apply_batch_norm else None), - scope='ProjectionLayer/conv2d_{}'.format( - inserted_layer_counter)) - if self._apply_batch_norm: - image_feature = slim.batch_norm( - image_feature, - scope='ProjectionLayer/conv2d_{}/BatchNorm'.format( - inserted_layer_counter)) - inserted_layer_counter += 1 - return image_feature, inserted_layer_counter - - def _compute_base_tower(self, tower_name_scope, image_feature, feature_index): - net = image_feature - for i in range(self._num_layers_before_predictor): - if self._use_depthwise: - conv_op = functools.partial(slim.separable_conv2d, depth_multiplier=1) - else: - conv_op = slim.conv2d - net = conv_op( - net, - self._depth, [self._kernel_size, self._kernel_size], - stride=1, - padding='SAME', - activation_fn=None, - normalizer_fn=(tf.identity if self._apply_batch_norm else None), - scope='{}/conv2d_{}'.format(tower_name_scope, i)) - if self._apply_batch_norm: - net = slim.batch_norm( - net, - scope='{}/conv2d_{}/BatchNorm/feature_{}'. - format(tower_name_scope, i, feature_index)) - net = tf.nn.relu6(net) - return net - - def _predict_head(self, head_name, head_obj, image_feature, box_tower_feature, - feature_index, num_predictions_per_location): - if head_name == CLASS_PREDICTIONS_WITH_BACKGROUND: - tower_name_scope = 'ClassPredictionTower' - else: - tower_name_scope = head_name + 'PredictionTower' - if self._share_prediction_tower: - head_tower_feature = box_tower_feature - else: - head_tower_feature = self._compute_base_tower( - tower_name_scope=tower_name_scope, - image_feature=image_feature, - feature_index=feature_index) - return head_obj.predict( - features=head_tower_feature, - num_predictions_per_location=num_predictions_per_location) - - def _predict(self, image_features, num_predictions_per_location_list): - """Computes encoded object locations and corresponding confidences. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels] containing features for a batch of images. Note that - when not all tensors in the list have the same number of channels, an - additional projection layer will be added on top the tensor to generate - feature map with number of channels consitent with the majority. - num_predictions_per_location_list: A list of integers representing the - number of box predictions to be made per spatial location for each - feature map. Note that all values must be the same since the weights are - shared. - - Returns: - A dictionary containing: - box_encodings: A list of float tensors of shape - [batch_size, num_anchors_i, code_size] representing the location of - the objects. Each entry in the list corresponds to a feature map in - the input `image_features` list. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - (optional) Predictions from other heads. - E.g., mask_predictions: A list of float tensors of shape - [batch_size, num_anchord_i, num_classes, mask_height, mask_width]. - - - Raises: - ValueError: If the num predictions per locations differs between the - feature maps. - """ - if len(set(num_predictions_per_location_list)) > 1: - raise ValueError('num predictions per location must be same for all' - 'feature maps, found: {}'.format( - num_predictions_per_location_list)) - feature_channels = [ - shape_utils.get_dim_as_int(image_feature.shape[3]) - for image_feature in image_features - ] - has_different_feature_channels = len(set(feature_channels)) > 1 - if has_different_feature_channels: - inserted_layer_counter = 0 - target_channel = max(set(feature_channels), key=feature_channels.count) - tf.logging.info('Not all feature maps have the same number of ' - 'channels, found: {}, appending additional projection ' - 'layers to bring all feature maps to uniformly have {} ' - 'channels.'.format(feature_channels, target_channel)) - else: - # Place holder variables if has_different_feature_channels is False. - target_channel = -1 - inserted_layer_counter = -1 - predictions = { - BOX_ENCODINGS: [], - CLASS_PREDICTIONS_WITH_BACKGROUND: [], - } - for head_name in self._other_heads.keys(): - predictions[head_name] = [] - for feature_index, (image_feature, - num_predictions_per_location) in enumerate( - zip(image_features, - num_predictions_per_location_list)): - with tf.variable_scope('WeightSharedConvolutionalBoxPredictor', - reuse=tf.AUTO_REUSE): - with slim.arg_scope(self._conv_hyperparams_fn()): - # TODO(wangjiang) Pass is_training to the head class directly. - with slim.arg_scope([slim.dropout], is_training=self._is_training): - (image_feature, - inserted_layer_counter) = self._insert_additional_projection_layer( - image_feature, inserted_layer_counter, target_channel) - if self._share_prediction_tower: - box_tower_scope = 'PredictionTower' - else: - box_tower_scope = 'BoxPredictionTower' - box_tower_feature = self._compute_base_tower( - tower_name_scope=box_tower_scope, - image_feature=image_feature, - feature_index=feature_index) - box_encodings = self._box_prediction_head.predict( - features=box_tower_feature, - num_predictions_per_location=num_predictions_per_location) - predictions[BOX_ENCODINGS].append(box_encodings) - sorted_keys = sorted(self._other_heads.keys()) - sorted_keys.append(CLASS_PREDICTIONS_WITH_BACKGROUND) - for head_name in sorted_keys: - if head_name == CLASS_PREDICTIONS_WITH_BACKGROUND: - head_obj = self._class_prediction_head - else: - head_obj = self._other_heads[head_name] - prediction = self._predict_head( - head_name=head_name, - head_obj=head_obj, - image_feature=image_feature, - box_tower_feature=box_tower_feature, - feature_index=feature_index, - num_predictions_per_location=num_predictions_per_location) - predictions[head_name].append(prediction) - return predictions - - diff --git a/research/object_detection/predictors/convolutional_box_predictor_tf1_test.py b/research/object_detection/predictors/convolutional_box_predictor_tf1_test.py deleted file mode 100644 index f83ca62d014..00000000000 --- a/research/object_detection/predictors/convolutional_box_predictor_tf1_test.py +++ /dev/null @@ -1,931 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.convolutional_box_predictor.""" - -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function -import unittest -from absl.testing import parameterized -import numpy as np -from six.moves import range -from six.moves import zip -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.predictors import convolutional_box_predictor as box_predictor -from object_detection.predictors.heads import box_head -from object_detection.predictors.heads import class_head -from object_detection.predictors.heads import mask_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ConvolutionalBoxPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.build(conv_hyperparams, is_training=True) - - def test_get_boxes_for_five_aspect_ratios_per_location(self): - def graph_fn(image_features): - conv_box_predictor = ( - box_predictor_builder.build_convolutional_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, objectness_predictions) - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, objectness_predictions) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 1, 4]) - self.assertAllEqual(objectness_predictions.shape, [4, 320, 1]) - - def test_get_boxes_for_one_aspect_ratio_per_location(self): - def graph_fn(image_features): - conv_box_predictor = ( - box_predictor_builder.build_convolutional_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[1], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (box_encodings, objectness_predictions) - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, objectness_predictions) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 64, 1, 4]) - self.assertAllEqual(objectness_predictions.shape, [4, 64, 1]) - - def test_get_multi_class_predictions_for_five_aspect_ratios_per_location( - self): - num_classes_without_background = 6 - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - def graph_fn(image_features): - conv_box_predictor = ( - box_predictor_builder.build_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], - num_predictions_per_location=[5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - (box_encodings, - class_predictions_with_background) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 1, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 320, num_classes_without_background+1]) - - def test_get_predictions_with_feature_maps_of_dynamic_shape( - self): - image_features = tf.placeholder(dtype=tf.float32, shape=[4, None, None, 64]) - conv_box_predictor = ( - box_predictor_builder.build_convolutional_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - init_op = tf.global_variables_initializer() - - resolution = 32 - expected_num_anchors = resolution*resolution*5 - with self.test_session() as sess: - sess.run(init_op) - (box_encodings_shape, - objectness_predictions_shape) = sess.run( - [tf.shape(box_encodings), tf.shape(objectness_predictions)], - feed_dict={image_features: - np.random.rand(4, resolution, resolution, 64)}) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - self.assertAllEqual(box_encodings_shape, [4, expected_num_anchors, 1, 4]) - self.assertAllEqual(objectness_predictions_shape, - [4, expected_num_anchors, 1]) - expected_variable_set = set([ - 'BoxPredictor/Conv2d_0_1x1_32/biases', - 'BoxPredictor/Conv2d_0_1x1_32/weights', - 'BoxPredictor/BoxEncodingPredictor/biases', - 'BoxPredictor/BoxEncodingPredictor/weights', - 'BoxPredictor/ClassPredictor/biases', - 'BoxPredictor/ClassPredictor/weights']) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_use_depthwise_convolution(self): - image_features = tf.placeholder(dtype=tf.float32, shape=[4, None, None, 64]) - conv_box_predictor = ( - box_predictor_builder.build_convolutional_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - dropout_keep_prob=0.8, - kernel_size=3, - box_code_size=4, - use_dropout=True, - use_depthwise=True)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - init_op = tf.global_variables_initializer() - - resolution = 32 - expected_num_anchors = resolution*resolution*5 - with self.test_session() as sess: - sess.run(init_op) - (box_encodings_shape, - objectness_predictions_shape) = sess.run( - [tf.shape(box_encodings), tf.shape(objectness_predictions)], - feed_dict={image_features: - np.random.rand(4, resolution, resolution, 64)}) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - self.assertAllEqual(box_encodings_shape, [4, expected_num_anchors, 1, 4]) - self.assertAllEqual(objectness_predictions_shape, - [4, expected_num_anchors, 1]) - expected_variable_set = set([ - 'BoxPredictor/Conv2d_0_1x1_32/biases', - 'BoxPredictor/Conv2d_0_1x1_32/weights', - 'BoxPredictor/BoxEncodingPredictor_depthwise/biases', - 'BoxPredictor/BoxEncodingPredictor_depthwise/depthwise_weights', - 'BoxPredictor/BoxEncodingPredictor/biases', - 'BoxPredictor/BoxEncodingPredictor/weights', - 'BoxPredictor/ClassPredictor_depthwise/biases', - 'BoxPredictor/ClassPredictor_depthwise/depthwise_weights', - 'BoxPredictor/ClassPredictor/biases', - 'BoxPredictor/ClassPredictor/weights']) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_no_dangling_outputs(self): - image_features = tf.placeholder(dtype=tf.float32, shape=[4, None, None, 64]) - conv_box_predictor = ( - box_predictor_builder.build_convolutional_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - dropout_keep_prob=0.8, - kernel_size=3, - box_code_size=4, - use_dropout=True, - use_depthwise=True)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[5], - scope='BoxPredictor') - tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - - bad_dangling_ops = [] - types_safe_to_dangle = set(['Assign', 'Mul', 'Const']) - for op in tf.get_default_graph().get_operations(): - if (not op.outputs) or (not op.outputs[0].consumers()): - if 'BoxPredictor' in op.name: - if op.type not in types_safe_to_dangle: - bad_dangling_ops.append(op) - - self.assertEqual(bad_dangling_ops, []) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class WeightSharedConvolutionalBoxPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - batch_norm { - train: true, - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.build(conv_hyperparams, is_training=True) - - def _build_conv_arg_scope_no_batch_norm(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - random_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.build(conv_hyperparams, is_training=True) - - def test_get_boxes_for_five_aspect_ratios_per_location(self): - - def graph_fn(image_features): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (box_encodings, objectness_predictions) - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, objectness_predictions) = self.execute( - graph_fn, [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 4]) - self.assertAllEqual(objectness_predictions.shape, [4, 320, 1]) - - def test_bias_predictions_to_background_with_sigmoid_score_conversion(self): - - def graph_fn(image_features): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=True, - num_classes=2, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=1, - class_prediction_bias_init=-4.6, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[5], - scope='BoxPredictor') - class_predictions = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (tf.nn.sigmoid(class_predictions),) - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - class_predictions = self.execute(graph_fn, [image_features]) - self.assertAlmostEqual(np.mean(class_predictions), 0.01, places=3) - - def test_get_multi_class_predictions_for_five_aspect_ratios_per_location( - self): - - num_classes_without_background = 6 - def graph_fn(image_features): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], - num_predictions_per_location=[5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (box_encodings, class_predictions_with_background) - - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 320, num_classes_without_background+1]) - - def test_get_multi_class_predictions_from_two_feature_maps( - self): - - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2], - num_predictions_per_location=[5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - image_features1 = np.random.rand(4, 8, 8, 64).astype(np.float32) - image_features2 = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features1, image_features2]) - self.assertAllEqual(box_encodings.shape, [4, 640, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 640, num_classes_without_background+1]) - - def test_get_multi_class_predictions_from_feature_maps_of_different_depth( - self): - - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2, image_features3): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2, image_features3], - num_predictions_per_location=[5, 5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - image_features1 = np.random.rand(4, 8, 8, 64).astype(np.float32) - image_features2 = np.random.rand(4, 8, 8, 64).astype(np.float32) - image_features3 = np.random.rand(4, 8, 8, 32).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features1, image_features2, image_features3]) - self.assertAllEqual(box_encodings.shape, [4, 960, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 960, num_classes_without_background+1]) - - def test_predictions_multiple_feature_maps_share_weights_separate_batchnorm( - self): - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=2, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2], - num_predictions_per_location=[5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - with self.test_session(graph=tf.Graph()): - graph_fn(tf.random_uniform([4, 32, 32, 3], dtype=tf.float32), - tf.random_uniform([4, 16, 16, 3], dtype=tf.float32)) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - expected_variable_set = set([ - # Box prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_0/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_1/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_0/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_1/beta'), - # Box prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/biases'), - # Class prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_0/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_1/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_0/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_1/beta'), - # Class prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/biases')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_multiple_feature_maps_share_weights_without_batchnorm( - self): - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - apply_batch_norm=False)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2], - num_predictions_per_location=[5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - with self.test_session(graph=tf.Graph()): - graph_fn(tf.random_uniform([4, 32, 32, 3], dtype=tf.float32), - tf.random_uniform([4, 16, 16, 3], dtype=tf.float32)) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - expected_variable_set = set([ - # Box prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/biases'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/biases'), - # Box prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/biases'), - # Class prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/biases'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/biases'), - # Class prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/biases')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_multiple_feature_maps_share_weights_with_depthwise( - self): - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - apply_batch_norm=False, - use_depthwise=True)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2], - num_predictions_per_location=[5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - with self.test_session(graph=tf.Graph()): - graph_fn(tf.random_uniform([4, 32, 32, 3], dtype=tf.float32), - tf.random_uniform([4, 16, 16, 3], dtype=tf.float32)) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - expected_variable_set = set([ - # Box prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/depthwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/pointwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/biases'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/depthwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/pointwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/biases'), - # Box prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/depthwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/pointwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/biases'), - # Class prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/depthwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/pointwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/biases'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/depthwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/pointwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/biases'), - # Class prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/depthwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/pointwise_weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/biases')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_no_batchnorm_params_when_batchnorm_is_not_configured(self): - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_conv_arg_scope_no_batch_norm(), - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - apply_batch_norm=False)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2], - num_predictions_per_location=[5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - with self.test_session(graph=tf.Graph()): - graph_fn(tf.random_uniform([4, 32, 32, 3], dtype=tf.float32), - tf.random_uniform([4, 16, 16, 3], dtype=tf.float32)) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - expected_variable_set = set([ - # Box prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/biases'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/biases'), - # Box prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/biases'), - # Class prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/biases'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/biases'), - # Class prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/biases')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_share_weights_share_tower_separate_batchnorm( - self): - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - share_prediction_tower=True)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2], - num_predictions_per_location=[5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - with self.test_session(graph=tf.Graph()): - graph_fn(tf.random_uniform([4, 32, 32, 3], dtype=tf.float32), - tf.random_uniform([4, 16, 16, 3], dtype=tf.float32)) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - expected_variable_set = set([ - # Shared prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_0/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_1/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_0/beta'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_1/beta'), - # Box prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/biases'), - # Class prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/biases')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_share_weights_share_tower_without_batchnorm( - self): - num_classes_without_background = 6 - def graph_fn(image_features1, image_features2): - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - share_prediction_tower=True, - apply_batch_norm=False)) - box_predictions = conv_box_predictor.predict( - [image_features1, image_features2], - num_predictions_per_location=[5, 5], - scope='BoxPredictor') - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - with self.test_session(graph=tf.Graph()): - graph_fn(tf.random_uniform([4, 32, 32, 3], dtype=tf.float32), - tf.random_uniform([4, 16, 16, 3], dtype=tf.float32)) - actual_variable_set = set( - [var.op.name for var in tf.trainable_variables()]) - expected_variable_set = set([ - # Shared prediction tower - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/biases'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/biases'), - # Box prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictor/biases'), - # Class prediction head - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/weights'), - ('BoxPredictor/WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictor/biases')]) - - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_get_predictions_with_feature_maps_of_dynamic_shape( - self): - image_features = tf.placeholder(dtype=tf.float32, shape=[4, None, None, 64]) - conv_box_predictor = ( - box_predictor_builder.build_weight_shared_convolutional_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - box_predictions = conv_box_predictor.predict( - [image_features], num_predictions_per_location=[5], - scope='BoxPredictor') - box_encodings = tf.concat(box_predictions[box_predictor.BOX_ENCODINGS], - axis=1) - objectness_predictions = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - init_op = tf.global_variables_initializer() - - resolution = 32 - expected_num_anchors = resolution*resolution*5 - with self.test_session() as sess: - sess.run(init_op) - (box_encodings_shape, - objectness_predictions_shape) = sess.run( - [tf.shape(box_encodings), tf.shape(objectness_predictions)], - feed_dict={image_features: - np.random.rand(4, resolution, resolution, 64)}) - self.assertAllEqual(box_encodings_shape, [4, expected_num_anchors, 4]) - self.assertAllEqual(objectness_predictions_shape, - [4, expected_num_anchors, 1]) - - def test_other_heads_predictions(self): - box_code_size = 4 - num_classes_without_background = 3 - other_head_name = 'Mask' - mask_height = 5 - mask_width = 5 - num_predictions_per_location = 5 - - def graph_fn(image_features): - box_prediction_head = box_head.WeightSharedConvolutionalBoxHead( - box_code_size) - class_prediction_head = class_head.WeightSharedConvolutionalClassHead( - num_classes_without_background + 1) - other_heads = { - other_head_name: - mask_head.WeightSharedConvolutionalMaskHead( - num_classes_without_background, - mask_height=mask_height, - mask_width=mask_width) - } - conv_box_predictor = box_predictor.WeightSharedConvolutionalBoxPredictor( - is_training=False, - num_classes=num_classes_without_background, - box_prediction_head=box_prediction_head, - class_prediction_head=class_prediction_head, - other_heads=other_heads, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - depth=32, - num_layers_before_predictor=2) - box_predictions = conv_box_predictor.predict( - [image_features], - num_predictions_per_location=[num_predictions_per_location], - scope='BoxPredictor') - for key, value in box_predictions.items(): - box_predictions[key] = tf.concat(value, axis=1) - assert len(box_predictions) == 3 - return (box_predictions[box_predictor.BOX_ENCODINGS], - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - box_predictions[other_head_name]) - - batch_size = 4 - feature_ht = 8 - feature_wt = 8 - image_features = np.random.rand(batch_size, feature_ht, feature_wt, - 64).astype(np.float32) - (box_encodings, class_predictions, other_head_predictions) = self.execute( - graph_fn, [image_features]) - num_anchors = feature_ht * feature_wt * num_predictions_per_location - self.assertAllEqual(box_encodings.shape, - [batch_size, num_anchors, box_code_size]) - self.assertAllEqual( - class_predictions.shape, - [batch_size, num_anchors, num_classes_without_background + 1]) - self.assertAllEqual(other_head_predictions.shape, [ - batch_size, num_anchors, num_classes_without_background, mask_height, - mask_width - ]) - - - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/convolutional_keras_box_predictor.py b/research/object_detection/predictors/convolutional_keras_box_predictor.py deleted file mode 100644 index fad48b05a87..00000000000 --- a/research/object_detection/predictors/convolutional_keras_box_predictor.py +++ /dev/null @@ -1,494 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Convolutional Box Predictors with and without weight sharing.""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import collections - -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.core import box_predictor -from object_detection.utils import shape_utils -from object_detection.utils import static_shape - -keras = tf.keras.layers - -BOX_ENCODINGS = box_predictor.BOX_ENCODINGS -CLASS_PREDICTIONS_WITH_BACKGROUND = ( - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND) -MASK_PREDICTIONS = box_predictor.MASK_PREDICTIONS - - -class _NoopVariableScope(object): - """A dummy class that does not push any scope.""" - - def __enter__(self): - return None - - def __exit__(self, exc_type, exc_value, traceback): - return False - - -class ConvolutionalBoxPredictor(box_predictor.KerasBoxPredictor): - """Convolutional Keras Box Predictor. - - Optionally add an intermediate 1x1 convolutional layer after features and - predict in parallel branches box_encodings and - class_predictions_with_background. - - Currently this box predictor assumes that predictions are "shared" across - classes --- that is each anchor makes box predictions which do not depend - on class. - """ - - def __init__(self, - is_training, - num_classes, - box_prediction_heads, - class_prediction_heads, - other_heads, - conv_hyperparams, - num_layers_before_predictor, - min_depth, - max_depth, - freeze_batchnorm, - inplace_batchnorm_update, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - box_prediction_heads: A list of heads that predict the boxes. - class_prediction_heads: A list of heads that predict the classes. - other_heads: A dictionary mapping head names to lists of convolutional - heads. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - min_depth: Minimum feature depth prior to predicting box encodings - and class predictions. - max_depth: Maximum feature depth prior to predicting box encodings - and class predictions. If max_depth is set to 0, no additional - feature map will be inserted before location and class predictions. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - - Raises: - ValueError: if min_depth > max_depth. - """ - super(ConvolutionalBoxPredictor, self).__init__( - is_training, num_classes, freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - name=name) - if min_depth > max_depth: - raise ValueError('min_depth should be less than or equal to max_depth') - if len(box_prediction_heads) != len(class_prediction_heads): - raise ValueError('All lists of heads must be the same length.') - for other_head_list in other_heads.values(): - if len(box_prediction_heads) != len(other_head_list): - raise ValueError('All lists of heads must be the same length.') - - self._prediction_heads = { - BOX_ENCODINGS: box_prediction_heads, - CLASS_PREDICTIONS_WITH_BACKGROUND: class_prediction_heads, - } - - if other_heads: - self._prediction_heads.update(other_heads) - - # We generate a consistent ordering for the prediction head names, - # So that all workers build the model in the exact same order - self._sorted_head_names = sorted(self._prediction_heads.keys()) - - self._conv_hyperparams = conv_hyperparams - self._min_depth = min_depth - self._max_depth = max_depth - self._num_layers_before_predictor = num_layers_before_predictor - - self._shared_nets = [] - - def build(self, input_shapes): - """Creates the variables of the layer.""" - if len(input_shapes) != len(self._prediction_heads[BOX_ENCODINGS]): - raise ValueError('This box predictor was constructed with %d heads,' - 'but there are %d inputs.' % - (len(self._prediction_heads[BOX_ENCODINGS]), - len(input_shapes))) - for stack_index, input_shape in enumerate(input_shapes): - net = [] - - # Add additional conv layers before the class predictor. - features_depth = static_shape.get_depth(input_shape) - depth = max(min(features_depth, self._max_depth), self._min_depth) - tf.logging.info( - 'depth of additional conv before box predictor: {}'.format(depth)) - - if depth > 0 and self._num_layers_before_predictor > 0: - for i in range(self._num_layers_before_predictor): - net.append(keras.Conv2D(depth, [1, 1], - name='SharedConvolutions_%d/Conv2d_%d_1x1_%d' - % (stack_index, i, depth), - padding='SAME', - **self._conv_hyperparams.params())) - net.append(self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='SharedConvolutions_%d/Conv2d_%d_1x1_%d_norm' - % (stack_index, i, depth))) - net.append(self._conv_hyperparams.build_activation_layer( - name='SharedConvolutions_%d/Conv2d_%d_1x1_%d_activation' - % (stack_index, i, depth), - )) - # Until certain bugs are fixed in checkpointable lists, - # this net must be appended only once it's been filled with layers - self._shared_nets.append(net) - self.built = True - - def _predict(self, image_features, **kwargs): - """Computes encoded object locations and corresponding confidences. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - **kwargs: Unused Keyword args - - Returns: - box_encodings: A list of float tensors of shape - [batch_size, num_anchors_i, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. Each entry in the - list corresponds to a feature map in the input `image_features` list. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - """ - predictions = collections.defaultdict(list) - - for (index, net) in enumerate(image_features): - - # Apply shared conv layers before the head predictors. - for layer in self._shared_nets[index]: - net = layer(net) - - for head_name in self._sorted_head_names: - head_obj = self._prediction_heads[head_name][index] - prediction = head_obj(net) - predictions[head_name].append(prediction) - - return predictions - - -class WeightSharedConvolutionalBoxPredictor(box_predictor.KerasBoxPredictor): - """Convolutional Box Predictor with weight sharing based on Keras. - - Defines the box predictor as defined in - https://arxiv.org/abs/1708.02002. This class differs from - ConvolutionalBoxPredictor in that it shares weights and biases while - predicting from different feature maps. However, batch_norm parameters are not - shared because the statistics of the activations vary among the different - feature maps. - - Also note that separate multi-layer towers are constructed for the box - encoding and class predictors respectively. - """ - - def __init__(self, - is_training, - num_classes, - box_prediction_head, - class_prediction_head, - other_heads, - conv_hyperparams, - depth, - num_layers_before_predictor, - freeze_batchnorm, - inplace_batchnorm_update, - kernel_size=3, - apply_batch_norm=False, - share_prediction_tower=False, - use_depthwise=False, - apply_conv_hyperparams_pointwise=False, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - box_prediction_head: The head that predicts the boxes. - class_prediction_head: The head that predicts the classes. - other_heads: A dictionary mapping head names to convolutional - head classes. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - depth: depth of conv layers. - num_layers_before_predictor: Number of the additional conv layers before - the predictor. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - inplace_batchnorm_update: Whether to update batch norm moving average - values inplace. When this is false train op must add a control - dependency on tf.graphkeys.UPDATE_OPS collection in order to update - batch norm statistics. - kernel_size: Size of final convolution kernel. - apply_batch_norm: Whether to apply batch normalization to conv layers in - this predictor. - share_prediction_tower: Whether to share the multi-layer tower among box - prediction head, class prediction head and other heads. - use_depthwise: Whether to use depthwise separable conv2d instead of - regular conv2d. - apply_conv_hyperparams_pointwise: Whether to apply the conv_hyperparams to - the pointwise_initializer and pointwise_regularizer when using depthwise - separable convolutions. By default, conv_hyperparams are only applied to - the depthwise initializer and regularizer when use_depthwise is true. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - """ - super(WeightSharedConvolutionalBoxPredictor, self).__init__( - is_training, num_classes, freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=inplace_batchnorm_update, - name=name) - - self._box_prediction_head = box_prediction_head - self._prediction_heads = { - CLASS_PREDICTIONS_WITH_BACKGROUND: class_prediction_head, - } - if other_heads: - self._prediction_heads.update(other_heads) - # We generate a consistent ordering for the prediction head names, - # so that all workers build the model in the exact same order. - self._sorted_head_names = sorted(self._prediction_heads.keys()) - - self._conv_hyperparams = conv_hyperparams - self._depth = depth - self._num_layers_before_predictor = num_layers_before_predictor - self._kernel_size = kernel_size - self._apply_batch_norm = apply_batch_norm - self._share_prediction_tower = share_prediction_tower - self._use_depthwise = use_depthwise - self._apply_conv_hyperparams_pointwise = apply_conv_hyperparams_pointwise - - # Additional projection layers to bring all feature maps to uniform - # channels. - self._additional_projection_layers = [] - # The base tower layers for each head. - self._base_tower_layers_for_heads = { - BOX_ENCODINGS: [], - CLASS_PREDICTIONS_WITH_BACKGROUND: [], - } - for head_name in other_heads.keys(): - self._base_tower_layers_for_heads[head_name] = [] - - # A dict maps the tower_name_scope of each head to the shared conv layers in - # the base tower for different feature map levels. - self._head_scope_conv_layers = {} - - def _insert_additional_projection_layer( - self, inserted_layer_counter, target_channel): - projection_layers = [] - if inserted_layer_counter >= 0: - use_bias = False if (self._apply_batch_norm and not - self._conv_hyperparams.force_use_bias()) else True - projection_layers.append(keras.Conv2D( - target_channel, [1, 1], strides=1, padding='SAME', - name='ProjectionLayer/conv2d_{}'.format(inserted_layer_counter), - **self._conv_hyperparams.params(use_bias=use_bias))) - if self._apply_batch_norm: - projection_layers.append(self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='ProjectionLayer/conv2d_{}/BatchNorm'.format( - inserted_layer_counter))) - inserted_layer_counter += 1 - return inserted_layer_counter, projection_layers - - def _compute_base_tower(self, tower_name_scope, feature_index): - conv_layers = [] - batch_norm_layers = [] - activation_layers = [] - use_bias = False if (self._apply_batch_norm and not - self._conv_hyperparams.force_use_bias()) else True - for additional_conv_layer_idx in range(self._num_layers_before_predictor): - layer_name = '{}/conv2d_{}'.format( - tower_name_scope, additional_conv_layer_idx) - if tower_name_scope not in self._head_scope_conv_layers: - if self._use_depthwise: - kwargs = self._conv_hyperparams.params(use_bias=use_bias) - # Both the regularizer and initializer apply to the depthwise layer, - # so we remap the kernel_* to depthwise_* here. - kwargs['depthwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['depthwise_initializer'] = kwargs['kernel_initializer'] - if self._apply_conv_hyperparams_pointwise: - kwargs['pointwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['pointwise_initializer'] = kwargs['kernel_initializer'] - conv_layers.append( - tf.keras.layers.SeparableConv2D( - self._depth, [self._kernel_size, self._kernel_size], - padding='SAME', - name=layer_name, - **kwargs)) - else: - conv_layers.append( - tf.keras.layers.Conv2D( - self._depth, - [self._kernel_size, self._kernel_size], - padding='SAME', - name=layer_name, - **self._conv_hyperparams.params(use_bias=use_bias))) - # Each feature gets a separate batchnorm parameter even though they share - # the same convolution weights. - if self._apply_batch_norm: - batch_norm_layers.append(self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='{}/conv2d_{}/BatchNorm/feature_{}'.format( - tower_name_scope, additional_conv_layer_idx, feature_index))) - activation_layers.append(self._conv_hyperparams.build_activation_layer( - name='{}/conv2d_{}/activation_{}'.format( - tower_name_scope, additional_conv_layer_idx, feature_index))) - - # Set conv layers as the shared conv layers for different feature maps with - # the same tower_name_scope. - if tower_name_scope in self._head_scope_conv_layers: - conv_layers = self._head_scope_conv_layers[tower_name_scope] - - # Stack the base_tower_layers in the order of conv_layer, batch_norm_layer - # and activation_layer - base_tower_layers = [] - for i in range(self._num_layers_before_predictor): - base_tower_layers.extend([conv_layers[i]]) - if self._apply_batch_norm: - base_tower_layers.extend([batch_norm_layers[i]]) - base_tower_layers.extend([activation_layers[i]]) - return conv_layers, base_tower_layers - - def build(self, input_shapes): - """Creates the variables of the layer.""" - feature_channels = [ - shape_utils.get_dim_as_int(input_shape[3]) - for input_shape in input_shapes - ] - has_different_feature_channels = len(set(feature_channels)) > 1 - if has_different_feature_channels: - inserted_layer_counter = 0 - target_channel = max(set(feature_channels), key=feature_channels.count) - tf.logging.info('Not all feature maps have the same number of ' - 'channels, found: {}, appending additional projection ' - 'layers to bring all feature maps to uniformly have {} ' - 'channels.'.format(feature_channels, target_channel)) - else: - # Place holder variables if has_different_feature_channels is False. - target_channel = -1 - inserted_layer_counter = -1 - - def _build_layers(tower_name_scope, feature_index): - conv_layers, base_tower_layers = self._compute_base_tower( - tower_name_scope=tower_name_scope, feature_index=feature_index) - if tower_name_scope not in self._head_scope_conv_layers: - self._head_scope_conv_layers[tower_name_scope] = conv_layers - return base_tower_layers - - for feature_index in range(len(input_shapes)): - # Additional projection layers should not be shared as input channels - # (and thus weight shapes) are different - inserted_layer_counter, projection_layers = ( - self._insert_additional_projection_layer( - inserted_layer_counter, target_channel)) - self._additional_projection_layers.append(projection_layers) - - if self._share_prediction_tower: - box_tower_scope = 'PredictionTower' - else: - box_tower_scope = 'BoxPredictionTower' - # For box tower base - box_tower_layers = _build_layers(box_tower_scope, feature_index) - self._base_tower_layers_for_heads[BOX_ENCODINGS].append(box_tower_layers) - - for head_name in self._sorted_head_names: - if head_name == CLASS_PREDICTIONS_WITH_BACKGROUND: - tower_name_scope = 'ClassPredictionTower' - else: - tower_name_scope = '{}PredictionTower'.format(head_name) - box_tower_layers = _build_layers(tower_name_scope, feature_index) - self._base_tower_layers_for_heads[head_name].append(box_tower_layers) - - self.built = True - - def _predict(self, image_features, **kwargs): - """Computes encoded object locations and corresponding confidences. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - **kwargs: Unused Keyword args - - Returns: - box_encodings: A list of float tensors of shape - [batch_size, num_anchors_i, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. Each entry in the - list corresponds to a feature map in the input `image_features` list. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - """ - predictions = collections.defaultdict(list) - - def _apply_layers(base_tower_layers, image_feature): - for layer in base_tower_layers: - image_feature = layer(image_feature) - return image_feature - - for (index, image_feature) in enumerate(image_features): - # Apply additional projection layers to image features - for layer in self._additional_projection_layers[index]: - image_feature = layer(image_feature) - - # Apply box tower layers. - box_tower_feature = _apply_layers( - self._base_tower_layers_for_heads[BOX_ENCODINGS][index], - image_feature) - box_encodings = self._box_prediction_head(box_tower_feature) - predictions[BOX_ENCODINGS].append(box_encodings) - - for head_name in self._sorted_head_names: - head_obj = self._prediction_heads[head_name] - if self._share_prediction_tower: - head_tower_feature = box_tower_feature - else: - head_tower_feature = _apply_layers( - self._base_tower_layers_for_heads[head_name][index], - image_feature) - prediction = head_obj(head_tower_feature) - predictions[head_name].append(prediction) - return predictions diff --git a/research/object_detection/predictors/convolutional_keras_box_predictor_tf2_test.py b/research/object_detection/predictors/convolutional_keras_box_predictor_tf2_test.py deleted file mode 100644 index 180a6e94643..00000000000 --- a/research/object_detection/predictors/convolutional_keras_box_predictor_tf2_test.py +++ /dev/null @@ -1,952 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.convolutional_keras_box_predictor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.predictors import convolutional_keras_box_predictor as box_predictor -from object_detection.predictors.heads import keras_box_head -from object_detection.predictors.heads import keras_class_head -from object_detection.predictors.heads import keras_mask_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ConvolutionalKerasBoxPredictorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_get_boxes_for_five_aspect_ratios_per_location(self): - conv_box_predictor = ( - box_predictor_builder.build_convolutional_keras_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5], - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4 - )) - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, objectness_predictions) - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, objectness_predictions) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 1, 4]) - self.assertAllEqual(objectness_predictions.shape, [4, 320, 1]) - - def test_get_boxes_for_one_aspect_ratio_per_location(self): - conv_box_predictor = ( - box_predictor_builder.build_convolutional_keras_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[1], - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4 - )) - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (box_encodings, objectness_predictions) - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, objectness_predictions) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 64, 1, 4]) - self.assertAllEqual(objectness_predictions.shape, [4, 64, 1]) - - def test_get_multi_class_predictions_for_five_aspect_ratios_per_location( - self): - num_classes_without_background = 6 - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - conv_box_predictor = ( - box_predictor_builder.build_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5], - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4 - )) - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - (box_encodings, - class_predictions_with_background) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 1, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 320, num_classes_without_background+1]) - - def test_get_predictions_with_feature_maps_of_dynamic_shape( - self): - tf.keras.backend.clear_session() - conv_box_predictor = ( - box_predictor_builder.build_convolutional_keras_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5], - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=1, - box_code_size=4 - )) - variables = [] - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return box_encodings, objectness_predictions - resolution = 32 - expected_num_anchors = resolution*resolution*5 - box_encodings, objectness_predictions = self.execute( - graph_fn, [np.random.rand(4, resolution, resolution, 64)]) - - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - self.assertAllEqual(box_encodings.shape, [4, expected_num_anchors, 1, 4]) - self.assertAllEqual(objectness_predictions.shape, - [4, expected_num_anchors, 1]) - expected_variable_set = set([ - 'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/bias', - 'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/kernel', - 'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/bias', - 'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/kernel', - 'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/bias', - 'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/kernel']) - self.assertEqual(expected_variable_set, actual_variable_set) - self.assertEqual(conv_box_predictor._sorted_head_names, - ['box_encodings', 'class_predictions_with_background']) - - def test_use_depthwise_convolution(self): - tf.keras.backend.clear_session() - conv_box_predictor = ( - box_predictor_builder.build_convolutional_keras_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5], - min_depth=0, - max_depth=32, - num_layers_before_predictor=1, - use_dropout=True, - dropout_keep_prob=0.8, - kernel_size=3, - box_code_size=4, - use_depthwise=True - )) - variables = [] - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return box_encodings, objectness_predictions - - resolution = 32 - expected_num_anchors = resolution*resolution*5 - box_encodings, objectness_predictions = self.execute( - graph_fn, [np.random.rand(4, resolution, resolution, 64)]) - - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - self.assertAllEqual(box_encodings.shape, [4, expected_num_anchors, 1, 4]) - self.assertAllEqual(objectness_predictions.shape, - [4, expected_num_anchors, 1]) - expected_variable_set = set([ - 'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/bias', - 'BoxPredictor/SharedConvolutions_0/Conv2d_0_1x1_32/kernel', - - 'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor_depthwise/' - 'bias', - - 'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor_depthwise/' - 'depthwise_kernel', - - 'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/bias', - 'BoxPredictor/ConvolutionalBoxHead_0/BoxEncodingPredictor/kernel', - 'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor_depthwise/bias', - - 'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor_depthwise/' - 'depthwise_kernel', - - 'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/bias', - 'BoxPredictor/ConvolutionalClassHead_0/ClassPredictor/kernel']) - self.assertEqual(expected_variable_set, actual_variable_set) - self.assertEqual(conv_box_predictor._sorted_head_names, - ['box_encodings', 'class_predictions_with_background']) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class WeightSharedConvolutionalKerasBoxPredictorTest(test_case.TestCase): - - def _build_conv_hyperparams(self, add_batch_norm=True): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: RELU_6 - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - stddev: 0.01 - mean: 0.0 - } - } - """ - if add_batch_norm: - batch_norm_proto = """ - batch_norm { - train: true, - } - """ - conv_hyperparams_text_proto += batch_norm_proto - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - # pylint: disable=line-too-long - def test_get_boxes_for_five_aspect_ratios_per_location(self): - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=0, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5], - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - objectness_predictions = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (box_encodings, objectness_predictions) - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, objectness_predictions) = self.execute( - graph_fn, [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 4]) - self.assertAllEqual(objectness_predictions.shape, [4, 320, 1]) - - def test_bias_predictions_to_background_with_sigmoid_score_conversion(self): - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=True, - num_classes=2, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5], - depth=32, - num_layers_before_predictor=1, - class_prediction_bias_init=-4.6, - box_code_size=4)) - - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - class_predictions = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (tf.nn.sigmoid(class_predictions),) - - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - class_predictions = self.execute(graph_fn, [image_features]) - self.assertAlmostEqual(np.mean(class_predictions), 0.01, places=3) - - def test_get_multi_class_predictions_for_five_aspect_ratios_per_location( - self): - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5], - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat(box_predictions[ - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], axis=1) - return (box_encodings, class_predictions_with_background) - - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features]) - self.assertAllEqual(box_encodings.shape, [4, 320, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 320, num_classes_without_background+1]) - - def test_get_multi_class_predictions_from_two_feature_maps( - self): - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5], - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - - def graph_fn(image_features1, image_features2): - box_predictions = conv_box_predictor([image_features1, image_features2]) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - image_features1 = np.random.rand(4, 8, 8, 64).astype(np.float32) - image_features2 = np.random.rand(4, 8, 8, 64).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features1, image_features2]) - self.assertAllEqual(box_encodings.shape, [4, 640, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 640, num_classes_without_background+1]) - - def test_get_multi_class_predictions_from_feature_maps_of_different_depth( - self): - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5, 5], - depth=32, - num_layers_before_predictor=1, - box_code_size=4)) - - def graph_fn(image_features1, image_features2, image_features3): - box_predictions = conv_box_predictor( - [image_features1, image_features2, image_features3]) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - image_features1 = np.random.rand(4, 8, 8, 64).astype(np.float32) - image_features2 = np.random.rand(4, 8, 8, 64).astype(np.float32) - image_features3 = np.random.rand(4, 8, 8, 32).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features1, image_features2, image_features3]) - self.assertAllEqual(box_encodings.shape, [4, 960, 4]) - self.assertAllEqual(class_predictions_with_background.shape, - [4, 960, num_classes_without_background+1]) - - def test_predictions_multiple_feature_maps_share_weights_separate_batchnorm( - self): - tf.keras.backend.clear_session() - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5], - depth=32, - num_layers_before_predictor=2, - box_code_size=4)) - variables = [] - - def graph_fn(image_features1, image_features2): - box_predictions = conv_box_predictor([image_features1, image_features2]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - self.execute(graph_fn, [ - np.random.rand(4, 32, 32, 3).astype(np.float32), - np.random.rand(4, 16, 16, 3).astype(np.float32) - ]) - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - expected_variable_set = set([ - # Box prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_0/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_0/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_0/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_1/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_1/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/BatchNorm/feature_1/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_0/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_0/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_0/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_1/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_1/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/BatchNorm/feature_1/moving_variance'), - # Box prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/bias'), - # Class prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_0/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_0/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_0/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_1/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_1/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/BatchNorm/feature_1/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_0/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_0/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_0/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_1/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_1/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/BatchNorm/feature_1/moving_variance'), - # Class prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/bias')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_multiple_feature_maps_share_weights_without_batchnorm( - self): - tf.keras.backend.clear_session() - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5], - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - apply_batch_norm=False)) - variables = [] - - def graph_fn(image_features1, image_features2): - box_predictions = conv_box_predictor([image_features1, image_features2]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - self.execute(graph_fn, [ - np.random.rand(4, 32, 32, 3).astype(np.float32), - np.random.rand(4, 16, 16, 3).astype(np.float32) - ]) - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - expected_variable_set = set([ - # Box prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/bias'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/bias'), - # Box prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/bias'), - # Class prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/bias'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/bias'), - # Class prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/bias')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_multiple_feature_maps_share_weights_with_depthwise( - self): - tf.keras.backend.clear_session() - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5], - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - apply_batch_norm=False, - use_depthwise=True)) - variables = [] - - def graph_fn(image_features1, image_features2): - box_predictions = conv_box_predictor([image_features1, image_features2]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - self.execute(graph_fn, [ - np.random.rand(4, 32, 32, 3).astype(np.float32), - np.random.rand(4, 16, 16, 3).astype(np.float32) - ]) - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - expected_variable_set = set([ - # Box prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/depthwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/pointwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/bias'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/depthwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/pointwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/bias'), - # Box prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/depthwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/pointwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/bias'), - # Class prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/depthwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/pointwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/bias'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/depthwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/pointwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/bias'), - # Class prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/depthwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/pointwise_kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/bias')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_no_batchnorm_params_when_batchnorm_is_not_configured(self): - tf.keras.backend.clear_session() - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5], - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - apply_batch_norm=False)) - variables = [] - - def graph_fn(image_features1, image_features2): - box_predictions = conv_box_predictor( - [image_features1, image_features2]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - self.execute(graph_fn, [ - np.random.rand(4, 32, 32, 3).astype(np.float32), - np.random.rand(4, 16, 16, 3).astype(np.float32) - ]) - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - expected_variable_set = set([ - # Box prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_0/bias'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'BoxPredictionTower/conv2d_1/bias'), - # Box prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/bias'), - # Class prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_0/bias'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'ClassPredictionTower/conv2d_1/bias'), - # Class prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/bias')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_share_weights_share_tower_separate_batchnorm( - self): - tf.keras.backend.clear_session() - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5], - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - share_prediction_tower=True)) - variables = [] - - def graph_fn(image_features1, image_features2): - box_predictions = conv_box_predictor( - [image_features1, image_features2]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - self.execute(graph_fn, [ - np.random.rand(4, 32, 32, 3).astype(np.float32), - np.random.rand(4, 16, 16, 3).astype(np.float32) - ]) - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - expected_variable_set = set([ - # Shared prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_0/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_1/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_0/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_1/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_0/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/BatchNorm/feature_1/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_0/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_1/beta'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_0/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_1/moving_mean'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_0/moving_variance'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/BatchNorm/feature_1/moving_variance'), - # Box prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/bias'), - # Class prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/bias')]) - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_predictions_share_weights_share_tower_without_batchnorm( - self): - tf.keras.backend.clear_session() - num_classes_without_background = 6 - conv_box_predictor = ( - box_predictor_builder - .build_weight_shared_convolutional_keras_box_predictor( - is_training=False, - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(add_batch_norm=False), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - num_predictions_per_location_list=[5, 5], - depth=32, - num_layers_before_predictor=2, - box_code_size=4, - share_prediction_tower=True, - apply_batch_norm=False)) - variables = [] - - def graph_fn(image_features1, image_features2): - box_predictions = conv_box_predictor( - [image_features1, image_features2]) - variables.extend(list(conv_box_predictor.variables)) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - self.execute(graph_fn, [ - np.random.rand(4, 32, 32, 3).astype(np.float32), - np.random.rand(4, 16, 16, 3).astype(np.float32) - ]) - actual_variable_set = set([var.name.split(':')[0] for var in variables]) - expected_variable_set = set([ - # Shared prediction tower - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_0/bias'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'PredictionTower/conv2d_1/bias'), - # Box prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalBoxHead/BoxPredictor/bias'), - # Class prediction head - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/kernel'), - ('WeightSharedConvolutionalBoxPredictor/' - 'WeightSharedConvolutionalClassHead/ClassPredictor/bias')]) - - self.assertEqual(expected_variable_set, actual_variable_set) - - def test_other_heads_predictions(self): - box_code_size = 4 - num_classes_without_background = 3 - other_head_name = 'Mask' - mask_height = 5 - mask_width = 5 - num_predictions_per_location = 5 - box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( - box_code_size=box_code_size, - conv_hyperparams=self._build_conv_hyperparams(), - num_predictions_per_location=num_predictions_per_location) - class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead( - num_class_slots=num_classes_without_background + 1, - conv_hyperparams=self._build_conv_hyperparams(), - num_predictions_per_location=num_predictions_per_location) - other_heads = { - other_head_name: - keras_mask_head.WeightSharedConvolutionalMaskHead( - num_classes=num_classes_without_background, - conv_hyperparams=self._build_conv_hyperparams(), - num_predictions_per_location=num_predictions_per_location, - mask_height=mask_height, - mask_width=mask_width) - } - - conv_box_predictor = box_predictor.WeightSharedConvolutionalBoxPredictor( - is_training=False, - num_classes=num_classes_without_background, - box_prediction_head=box_prediction_head, - class_prediction_head=class_prediction_head, - other_heads=other_heads, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - inplace_batchnorm_update=False, - depth=32, - num_layers_before_predictor=2) - def graph_fn(image_features): - box_predictions = conv_box_predictor([image_features]) - for key, value in box_predictions.items(): - box_predictions[key] = tf.concat(value, axis=1) - assert len(box_predictions) == 3 - return (box_predictions[box_predictor.BOX_ENCODINGS], - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - box_predictions[other_head_name]) - - batch_size = 4 - feature_ht = 8 - feature_wt = 8 - image_features = np.random.rand(batch_size, feature_ht, feature_wt, - 64).astype(np.float32) - (box_encodings, class_predictions, other_head_predictions) = self.execute( - graph_fn, [image_features]) - num_anchors = feature_ht * feature_wt * num_predictions_per_location - self.assertAllEqual(box_encodings.shape, - [batch_size, num_anchors, box_code_size]) - self.assertAllEqual( - class_predictions.shape, - [batch_size, num_anchors, num_classes_without_background + 1]) - self.assertAllEqual(other_head_predictions.shape, [ - batch_size, num_anchors, num_classes_without_background, mask_height, - mask_width - ]) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/heads/__init__.py b/research/object_detection/predictors/heads/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/predictors/heads/box_head.py b/research/object_detection/predictors/heads/box_head.py deleted file mode 100644 index 6535e9b2819..00000000000 --- a/research/object_detection/predictors/heads/box_head.py +++ /dev/null @@ -1,281 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Box Head. - -Contains Box prediction head classes for different meta architectures. -All the box prediction heads have a predict function that receives the -`features` as the first argument and returns `box_encodings`. -""" -import functools -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.predictors.heads import head - - -class MaskRCNNBoxHead(head.Head): - """Box prediction head. - - Please refer to Mask RCNN paper: - https://arxiv.org/abs/1703.06870 - """ - - def __init__(self, - is_training, - num_classes, - fc_hyperparams_fn, - use_dropout, - dropout_keep_prob, - box_code_size, - share_box_across_classes=False): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - fc_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for fully connected ops. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - box_code_size: Size of encoding for each box. - share_box_across_classes: Whether to share boxes across classes rather - than use a different box for each class. - """ - super(MaskRCNNBoxHead, self).__init__() - self._is_training = is_training - self._num_classes = num_classes - self._fc_hyperparams_fn = fc_hyperparams_fn - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._box_code_size = box_code_size - self._share_box_across_classes = share_box_across_classes - - def predict(self, features, num_predictions_per_location=1): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, - channels] containing features for a batch of images. - num_predictions_per_location: Int containing number of predictions per - location. - - Returns: - box_encodings: A float tensor of shape - [batch_size, 1, num_classes, code_size] representing the location of the - objects. - - Raises: - ValueError: If num_predictions_per_location is not 1. - """ - if num_predictions_per_location != 1: - raise ValueError('Only num_predictions_per_location=1 is supported') - spatial_averaged_roi_pooled_features = tf.reduce_mean( - features, [1, 2], keep_dims=True, name='AvgPool') - flattened_roi_pooled_features = slim.flatten( - spatial_averaged_roi_pooled_features) - if self._use_dropout: - flattened_roi_pooled_features = slim.dropout( - flattened_roi_pooled_features, - keep_prob=self._dropout_keep_prob, - is_training=self._is_training) - number_of_boxes = 1 - if not self._share_box_across_classes: - number_of_boxes = self._num_classes - - with slim.arg_scope(self._fc_hyperparams_fn()): - box_encodings = slim.fully_connected( - flattened_roi_pooled_features, - number_of_boxes * self._box_code_size, - reuse=tf.AUTO_REUSE, - activation_fn=None, - scope='BoxEncodingPredictor') - box_encodings = tf.reshape(box_encodings, - [-1, 1, number_of_boxes, self._box_code_size]) - return box_encodings - - -class ConvolutionalBoxHead(head.Head): - """Convolutional box prediction head.""" - - def __init__(self, - is_training, - box_code_size, - kernel_size, - use_depthwise=False, - box_encodings_clip_range=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - box_code_size: Size of encoding for each box. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - box_encodings_clip_range: Min and max values for clipping box_encodings. - - Raises: - ValueError: if min_depth > max_depth. - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(ConvolutionalBoxHead, self).__init__() - self._is_training = is_training - self._box_code_size = box_code_size - self._kernel_size = kernel_size - self._use_depthwise = use_depthwise - self._box_encodings_clip_range = box_encodings_clip_range - - def predict(self, features, num_predictions_per_location): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - num_predictions_per_location: Number of box predictions to be made per - spatial location. Int specifying number of boxes per location. - - Returns: - box_encodings: A float tensors of shape - [batch_size, num_anchors, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. - """ - net = features - if self._use_depthwise: - box_encodings = slim.separable_conv2d( - net, None, [self._kernel_size, self._kernel_size], - padding='SAME', depth_multiplier=1, stride=1, - rate=1, scope='BoxEncodingPredictor_depthwise') - box_encodings = slim.conv2d( - box_encodings, - num_predictions_per_location * self._box_code_size, [1, 1], - activation_fn=None, - normalizer_fn=None, - normalizer_params=None, - scope='BoxEncodingPredictor') - else: - box_encodings = slim.conv2d( - net, num_predictions_per_location * self._box_code_size, - [self._kernel_size, self._kernel_size], - activation_fn=None, - normalizer_fn=None, - normalizer_params=None, - scope='BoxEncodingPredictor') - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - # Clipping the box encodings to make the inference graph TPU friendly. - if self._box_encodings_clip_range is not None: - box_encodings = tf.clip_by_value( - box_encodings, self._box_encodings_clip_range.min, - self._box_encodings_clip_range.max) - box_encodings = tf.reshape(box_encodings, - [batch_size, -1, 1, self._box_code_size]) - return box_encodings - - -# TODO(alirezafathi): See if possible to unify Weight Shared with regular -# convolutional box head. -class WeightSharedConvolutionalBoxHead(head.Head): - """Weight shared convolutional box prediction head. - - This head allows sharing the same set of parameters (weights) when called more - then once on different feature maps. - """ - - def __init__(self, - box_code_size, - kernel_size=3, - use_depthwise=False, - box_encodings_clip_range=None, - return_flat_predictions=True): - """Constructor. - - Args: - box_code_size: Size of encoding for each box. - kernel_size: Size of final convolution kernel. - use_depthwise: Whether to use depthwise convolutions for prediction steps. - Default is False. - box_encodings_clip_range: Min and max values for clipping box_encodings. - return_flat_predictions: If true, returns flattened prediction tensor - of shape [batch, height * width * num_predictions_per_location, - box_coder]. Otherwise returns the prediction tensor before reshaping, - whose shape is [batch, height, width, num_predictions_per_location * - num_class_slots]. - - Raises: - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(WeightSharedConvolutionalBoxHead, self).__init__() - self._box_code_size = box_code_size - self._kernel_size = kernel_size - self._use_depthwise = use_depthwise - self._box_encodings_clip_range = box_encodings_clip_range - self._return_flat_predictions = return_flat_predictions - - def predict(self, features, num_predictions_per_location): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - num_predictions_per_location: Number of box predictions to be made per - spatial location. - - Returns: - box_encodings: A float tensor of shape - [batch_size, num_anchors, code_size] representing the location of - the objects, or a float tensor of shape [batch, height, width, - num_predictions_per_location * box_code_size] representing grid box - location predictions if self._return_flat_predictions is False. - """ - box_encodings_net = features - if self._use_depthwise: - conv_op = functools.partial(slim.separable_conv2d, depth_multiplier=1) - else: - conv_op = slim.conv2d - box_encodings = conv_op( - box_encodings_net, - num_predictions_per_location * self._box_code_size, - [self._kernel_size, self._kernel_size], - activation_fn=None, stride=1, padding='SAME', - normalizer_fn=None, - scope='BoxPredictor') - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - # Clipping the box encodings to make the inference graph TPU friendly. - if self._box_encodings_clip_range is not None: - box_encodings = tf.clip_by_value( - box_encodings, self._box_encodings_clip_range.min, - self._box_encodings_clip_range.max) - if self._return_flat_predictions: - box_encodings = tf.reshape(box_encodings, - [batch_size, -1, self._box_code_size]) - return box_encodings diff --git a/research/object_detection/predictors/heads/box_head_tf1_test.py b/research/object_detection/predictors/heads/box_head_tf1_test.py deleted file mode 100644 index ab534a2bd02..00000000000 --- a/research/object_detection/predictors/heads/box_head_tf1_test.py +++ /dev/null @@ -1,132 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.heads.box_head.""" -import unittest -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors.heads import box_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class MaskRCNNBoxHeadTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - box_prediction_head = box_head.MaskRCNNBoxHead( - is_training=False, - num_classes=20, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=True, - dropout_keep_prob=0.5, - box_code_size=4, - share_box_across_classes=False) - roi_pooled_features = tf.random_uniform( - [64, 7, 7, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = box_prediction_head.predict( - features=roi_pooled_features, num_predictions_per_location=1) - self.assertAllEqual([64, 1, 20, 4], prediction.get_shape().as_list()) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ConvolutionalBoxPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams( - self, op_type=hyperparams_pb2.Hyperparams.CONV): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - box_prediction_head = box_head.ConvolutionalBoxHead( - is_training=True, - box_code_size=4, - kernel_size=3) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_encodings = box_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 1, 4], box_encodings.get_shape().as_list()) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class WeightSharedConvolutionalBoxPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams( - self, op_type=hyperparams_pb2.Hyperparams.CONV): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - box_prediction_head = box_head.WeightSharedConvolutionalBoxHead( - box_code_size=4) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_encodings = box_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 4], box_encodings.get_shape().as_list()) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/heads/class_head.py b/research/object_detection/predictors/heads/class_head.py deleted file mode 100644 index d7abc23c20c..00000000000 --- a/research/object_detection/predictors/heads/class_head.py +++ /dev/null @@ -1,326 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Class Head. - -Contains Class prediction head classes for different meta architectures. -All the class prediction heads have a predict function that receives the -`features` as the first argument and returns class predictions with background. -""" -import functools -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.predictors.heads import head -from object_detection.utils import shape_utils - - -class MaskRCNNClassHead(head.Head): - """Mask RCNN class prediction head. - - Please refer to Mask RCNN paper: - https://arxiv.org/abs/1703.06870 - """ - - def __init__(self, - is_training, - num_class_slots, - fc_hyperparams_fn, - use_dropout, - dropout_keep_prob, - scope='ClassPredictor'): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_class_slots: number of class slots. Note that num_class_slots may or - may not include an implicit background category. - fc_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for fully connected ops. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - scope: Scope name for the convolution operation. - """ - super(MaskRCNNClassHead, self).__init__() - self._is_training = is_training - self._num_class_slots = num_class_slots - self._fc_hyperparams_fn = fc_hyperparams_fn - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._scope = scope - - def predict(self, features, num_predictions_per_location=1): - """Predicts boxes and class scores. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing features for a batch of images. - num_predictions_per_location: Int containing number of predictions per - location. - - Returns: - class_predictions_with_background: A float tensor of shape - [batch_size, 1, num_class_slots] representing the class predictions for - the proposals. - - Raises: - ValueError: If num_predictions_per_location is not 1. - """ - if num_predictions_per_location != 1: - raise ValueError('Only num_predictions_per_location=1 is supported') - spatial_averaged_roi_pooled_features = tf.reduce_mean( - features, [1, 2], keep_dims=True, name='AvgPool') - flattened_roi_pooled_features = slim.flatten( - spatial_averaged_roi_pooled_features) - if self._use_dropout: - flattened_roi_pooled_features = slim.dropout( - flattened_roi_pooled_features, - keep_prob=self._dropout_keep_prob, - is_training=self._is_training) - - with slim.arg_scope(self._fc_hyperparams_fn()): - class_predictions_with_background = slim.fully_connected( - flattened_roi_pooled_features, - self._num_class_slots, - reuse=tf.AUTO_REUSE, - activation_fn=None, - scope=self._scope) - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [-1, 1, self._num_class_slots]) - return class_predictions_with_background - - -class ConvolutionalClassHead(head.Head): - """Convolutional class prediction head.""" - - def __init__(self, - is_training, - num_class_slots, - use_dropout, - dropout_keep_prob, - kernel_size, - apply_sigmoid_to_scores=False, - class_prediction_bias_init=0.0, - use_depthwise=False, - scope='ClassPredictor'): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_class_slots: number of class slots. Note that num_class_slots may or - may not include an implicit background category. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - apply_sigmoid_to_scores: if True, apply the sigmoid on the output - class_predictions. - class_prediction_bias_init: constant value to initialize bias of the last - conv2d layer before class prediction. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - scope: Scope name for the convolution operation. - - Raises: - ValueError: if min_depth > max_depth. - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(ConvolutionalClassHead, self).__init__() - self._is_training = is_training - self._num_class_slots = num_class_slots - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._kernel_size = kernel_size - self._apply_sigmoid_to_scores = apply_sigmoid_to_scores - self._class_prediction_bias_init = class_prediction_bias_init - self._use_depthwise = use_depthwise - self._scope = scope - - def predict(self, features, num_predictions_per_location): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - num_predictions_per_location: Number of box predictions to be made per - spatial location. - - Returns: - class_predictions_with_background: A float tensors of shape - [batch_size, num_anchors, num_class_slots] representing the class - predictions for the proposals. - """ - net = features - if self._use_dropout: - net = slim.dropout(net, keep_prob=self._dropout_keep_prob) - if self._use_depthwise: - depthwise_scope = self._scope + '_depthwise' - class_predictions_with_background = slim.separable_conv2d( - net, None, [self._kernel_size, self._kernel_size], - padding='SAME', depth_multiplier=1, stride=1, - rate=1, scope=depthwise_scope) - class_predictions_with_background = slim.conv2d( - class_predictions_with_background, - num_predictions_per_location * self._num_class_slots, [1, 1], - activation_fn=None, - normalizer_fn=None, - normalizer_params=None, - scope=self._scope) - else: - class_predictions_with_background = slim.conv2d( - net, - num_predictions_per_location * self._num_class_slots, - [self._kernel_size, self._kernel_size], - activation_fn=None, - normalizer_fn=None, - normalizer_params=None, - scope=self._scope, - biases_initializer=tf.constant_initializer( - self._class_prediction_bias_init)) - if self._apply_sigmoid_to_scores: - class_predictions_with_background = tf.sigmoid( - class_predictions_with_background) - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [batch_size, -1, self._num_class_slots]) - return class_predictions_with_background - - -# TODO(alirezafathi): See if possible to unify Weight Shared with regular -# convolutional class head. -class WeightSharedConvolutionalClassHead(head.Head): - """Weight shared convolutional class prediction head. - - This head allows sharing the same set of parameters (weights) when called more - then once on different feature maps. - """ - - def __init__(self, - num_class_slots, - kernel_size=3, - class_prediction_bias_init=0.0, - use_dropout=False, - dropout_keep_prob=0.8, - use_depthwise=False, - score_converter_fn=tf.identity, - return_flat_predictions=True, - scope='ClassPredictor'): - """Constructor. - - Args: - num_class_slots: number of class slots. Note that num_class_slots may or - may not include an implicit background category. - kernel_size: Size of final convolution kernel. - class_prediction_bias_init: constant value to initialize bias of the last - conv2d layer before class prediction. - use_dropout: Whether to apply dropout to class prediction head. - dropout_keep_prob: Probability of keeping activiations. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - score_converter_fn: Callable elementwise nonlinearity (that takes tensors - as inputs and returns tensors). - return_flat_predictions: If true, returns flattened prediction tensor - of shape [batch, height * width * num_predictions_per_location, - box_coder]. Otherwise returns the prediction tensor before reshaping, - whose shape is [batch, height, width, num_predictions_per_location * - num_class_slots]. - scope: Scope name for the convolution operation. - - Raises: - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(WeightSharedConvolutionalClassHead, self).__init__() - self._num_class_slots = num_class_slots - self._kernel_size = kernel_size - self._class_prediction_bias_init = class_prediction_bias_init - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._use_depthwise = use_depthwise - self._score_converter_fn = score_converter_fn - self._return_flat_predictions = return_flat_predictions - self._scope = scope - - def predict(self, features, num_predictions_per_location): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - num_predictions_per_location: Number of box predictions to be made per - spatial location. - - Returns: - class_predictions_with_background: A tensor of shape - [batch_size, num_anchors, num_class_slots] representing the class - predictions for the proposals, or a tensor of shape [batch, height, - width, num_predictions_per_location * num_class_slots] representing - class predictions before reshaping if self._return_flat_predictions is - False. - """ - class_predictions_net = features - if self._use_dropout: - class_predictions_net = slim.dropout( - class_predictions_net, keep_prob=self._dropout_keep_prob) - if self._use_depthwise: - conv_op = functools.partial(slim.separable_conv2d, depth_multiplier=1) - else: - conv_op = slim.conv2d - class_predictions_with_background = conv_op( - class_predictions_net, - num_predictions_per_location * self._num_class_slots, - [self._kernel_size, self._kernel_size], - activation_fn=None, stride=1, padding='SAME', - normalizer_fn=None, - biases_initializer=tf.constant_initializer( - self._class_prediction_bias_init), - scope=self._scope) - batch_size, height, width = shape_utils.combined_static_and_dynamic_shape( - features)[0:3] - class_predictions_with_background = tf.reshape( - class_predictions_with_background, [ - batch_size, height, width, num_predictions_per_location, - self._num_class_slots - ]) - class_predictions_with_background = self._score_converter_fn( - class_predictions_with_background) - if self._return_flat_predictions: - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [batch_size, -1, self._num_class_slots]) - else: - class_predictions_with_background = tf.reshape( - class_predictions_with_background, [ - batch_size, height, width, - num_predictions_per_location * self._num_class_slots - ]) - return class_predictions_with_background diff --git a/research/object_detection/predictors/heads/class_head_tf1_test.py b/research/object_detection/predictors/heads/class_head_tf1_test.py deleted file mode 100644 index 986a383c1a7..00000000000 --- a/research/object_detection/predictors/heads/class_head_tf1_test.py +++ /dev/null @@ -1,231 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.heads.class_head.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors.heads import class_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class MaskRCNNClassHeadTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - class_prediction_head = class_head.MaskRCNNClassHead( - is_training=False, - num_class_slots=20, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=True, - dropout_keep_prob=0.5) - roi_pooled_features = tf.random_uniform( - [64, 7, 7, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = class_prediction_head.predict( - features=roi_pooled_features, num_predictions_per_location=1) - self.assertAllEqual([64, 1, 20], prediction.get_shape().as_list()) - - def test_scope_name(self): - expected_var_names = set([ - """ClassPredictor/weights""", - """ClassPredictor/biases""" - ]) - - g = tf.Graph() - with g.as_default(): - class_prediction_head = class_head.MaskRCNNClassHead( - is_training=True, - num_class_slots=20, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=True, - dropout_keep_prob=0.5) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - actual_variable_set = set([ - var.op.name for var in g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - ]) - self.assertSetEqual(expected_var_names, actual_variable_set) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ConvolutionalClassPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams( - self, op_type=hyperparams_pb2.Hyperparams.CONV): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - class_prediction_head = class_head.ConvolutionalClassHead( - is_training=True, - num_class_slots=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_predictions = class_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 20], - class_predictions.get_shape().as_list()) - - def test_scope_name(self): - expected_var_names = set([ - """ClassPredictor/weights""", - """ClassPredictor/biases""" - ]) - g = tf.Graph() - with g.as_default(): - class_prediction_head = class_head.ConvolutionalClassHead( - is_training=True, - num_class_slots=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - actual_variable_set = set([ - var.op.name for var in g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - ]) - self.assertSetEqual(expected_var_names, actual_variable_set) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class WeightSharedConvolutionalClassPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams( - self, op_type=hyperparams_pb2.Hyperparams.CONV): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - class_prediction_head = ( - class_head.WeightSharedConvolutionalClassHead(num_class_slots=20)) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_predictions = class_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 20], class_predictions.get_shape().as_list()) - - def test_scope_name(self): - expected_var_names = set([ - """ClassPredictor/weights""", - """ClassPredictor/biases""" - ]) - g = tf.Graph() - with g.as_default(): - class_prediction_head = class_head.WeightSharedConvolutionalClassHead( - num_class_slots=20) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - actual_variable_set = set([ - var.op.name for var in g.get_collection(tf.GraphKeys.GLOBAL_VARIABLES) - ]) - self.assertSetEqual(expected_var_names, actual_variable_set) - - def test_softmax_score_converter(self): - num_class_slots = 10 - batch_size = 2 - height = 17 - width = 19 - num_predictions_per_location = 2 - assert num_predictions_per_location != 1 - - def graph_fn(): - class_prediction_head = ( - class_head.WeightSharedConvolutionalClassHead( - num_class_slots=num_class_slots, - score_converter_fn=tf.nn.softmax)) - image_feature = tf.random_uniform([batch_size, height, width, 1024], - minval=-10.0, - maxval=10.0, - dtype=tf.float32) - class_predictions = class_prediction_head.predict( - features=image_feature, - num_predictions_per_location=num_predictions_per_location) - return class_predictions - - class_predictions_out = self.execute(graph_fn, []) - class_predictions_sum = np.sum(class_predictions_out, axis=-1) - num_anchors = height * width * num_predictions_per_location - exp_class_predictions_sum = np.ones((batch_size, num_anchors), - dtype=np.float32) - self.assertAllEqual((batch_size, num_anchors, num_class_slots), - class_predictions_out.shape) - self.assertAllClose(class_predictions_sum, exp_class_predictions_sum) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/heads/head.py b/research/object_detection/predictors/heads/head.py deleted file mode 100644 index 7dc2a9492f2..00000000000 --- a/research/object_detection/predictors/heads/head.py +++ /dev/null @@ -1,81 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Base head class. - -All the different kinds of prediction heads in different models will inherit -from this class. What is in common between all head classes is that they have a -`predict` function that receives `features` as its first argument. - -How to add a new prediction head to an existing meta architecture? -For example, how can we add a `3d shape` prediction head to Mask RCNN? - -We have to take the following steps to add a new prediction head to an -existing meta arch: -(a) Add a class for predicting the head. This class should inherit from the -`Head` class below and have a `predict` function that receives the features -and predicts the output. The output is always a tf.float32 tensor. -(b) Add the head to the meta architecture. For example in case of Mask RCNN, -go to box_predictor_builder and put in the logic for adding the new head to the -Mask RCNN box predictor. -(c) Add the logic for computing the loss for the new head. -(d) Add the necessary metrics for the new head. -(e) (optional) Add visualization for the new head. -""" -from abc import abstractmethod - -import tensorflow.compat.v1 as tf - - -class Head(object): - """Mask RCNN head base class.""" - - def __init__(self): - """Constructor.""" - pass - - @abstractmethod - def predict(self, features, num_predictions_per_location): - """Returns the head's predictions. - - Args: - features: A float tensor of features. - num_predictions_per_location: Int containing number of predictions per - location. - - Returns: - A tf.float32 tensor. - """ - pass - - -class KerasHead(tf.keras.layers.Layer): - """Keras head base class.""" - - def call(self, features): - """The Keras model call will delegate to the `_predict` method.""" - return self._predict(features) - - @abstractmethod - def _predict(self, features): - """Returns the head's predictions. - - Args: - features: A float tensor of features. - - Returns: - A tf.float32 tensor. - """ - pass diff --git a/research/object_detection/predictors/heads/keras_box_head.py b/research/object_detection/predictors/heads/keras_box_head.py deleted file mode 100644 index daf730646b8..00000000000 --- a/research/object_detection/predictors/heads/keras_box_head.py +++ /dev/null @@ -1,345 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Box Head. - -Contains Box prediction head classes for different meta architectures. -All the box prediction heads have a _predict function that receives the -`features` as the first argument and returns `box_encodings`. -""" -import tensorflow.compat.v1 as tf - -from object_detection.predictors.heads import head - - -class ConvolutionalBoxHead(head.KerasHead): - """Convolutional box prediction head.""" - - def __init__(self, - is_training, - box_code_size, - kernel_size, - num_predictions_per_location, - conv_hyperparams, - freeze_batchnorm, - use_depthwise=False, - box_encodings_clip_range=None, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - box_code_size: Size of encoding for each box. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - num_predictions_per_location: Number of box predictions to be made per - spatial location. Int specifying number of boxes per location. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - box_encodings_clip_range: Min and max values for clipping box_encodings. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - - Raises: - ValueError: if min_depth > max_depth. - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(ConvolutionalBoxHead, self).__init__(name=name) - self._is_training = is_training - self._box_code_size = box_code_size - self._kernel_size = kernel_size - self._num_predictions_per_location = num_predictions_per_location - self._use_depthwise = use_depthwise - self._box_encodings_clip_range = box_encodings_clip_range - - self._box_encoder_layers = [] - - if self._use_depthwise: - self._box_encoder_layers.append( - tf.keras.layers.DepthwiseConv2D( - [self._kernel_size, self._kernel_size], - padding='SAME', - depth_multiplier=1, - strides=1, - dilation_rate=1, - name='BoxEncodingPredictor_depthwise', - **conv_hyperparams.params())) - self._box_encoder_layers.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name='BoxEncodingPredictor_depthwise_batchnorm')) - self._box_encoder_layers.append( - conv_hyperparams.build_activation_layer( - name='BoxEncodingPredictor_depthwise_activation')) - self._box_encoder_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * self._box_code_size, [1, 1], - name='BoxEncodingPredictor', - **conv_hyperparams.params(use_bias=True))) - else: - self._box_encoder_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * self._box_code_size, - [self._kernel_size, self._kernel_size], - padding='SAME', - name='BoxEncodingPredictor', - **conv_hyperparams.params(use_bias=True))) - - def _predict(self, features): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - - Returns: - box_encodings: A float tensor of shape - [batch_size, num_anchors, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. - """ - box_encodings = features - for layer in self._box_encoder_layers: - box_encodings = layer(box_encodings) - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - # Clipping the box encodings to make the inference graph TPU friendly. - if self._box_encodings_clip_range is not None: - box_encodings = tf.clip_by_value( - box_encodings, self._box_encodings_clip_range.min, - self._box_encodings_clip_range.max) - box_encodings = tf.reshape(box_encodings, - [batch_size, -1, 1, self._box_code_size]) - return box_encodings - - -class MaskRCNNBoxHead(head.KerasHead): - """Box prediction head. - - This is a piece of Mask RCNN which is responsible for predicting - just the box encodings. - - Please refer to Mask RCNN paper: - https://arxiv.org/abs/1703.06870 - """ - - def __init__(self, - is_training, - num_classes, - fc_hyperparams, - freeze_batchnorm, - use_dropout, - dropout_keep_prob, - box_code_size, - share_box_across_classes=False, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - fc_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for fully connected dense ops. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - box_code_size: Size of encoding for each box. - share_box_across_classes: Whether to share boxes across classes rather - than use a different box for each class. - name: A string name scope to assign to the box head. If `None`, Keras - will auto-generate one from the class name. - """ - super(MaskRCNNBoxHead, self).__init__(name=name) - self._is_training = is_training - self._num_classes = num_classes - self._fc_hyperparams = fc_hyperparams - self._freeze_batchnorm = freeze_batchnorm - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._box_code_size = box_code_size - self._share_box_across_classes = share_box_across_classes - - self._box_encoder_layers = [tf.keras.layers.Flatten()] - - if self._use_dropout: - self._box_encoder_layers.append( - tf.keras.layers.Dropout(rate=1.0 - self._dropout_keep_prob)) - - self._number_of_boxes = 1 - if not self._share_box_across_classes: - self._number_of_boxes = self._num_classes - - self._box_encoder_layers.append( - tf.keras.layers.Dense(self._number_of_boxes * self._box_code_size, - name='BoxEncodingPredictor_dense')) - self._box_encoder_layers.append( - fc_hyperparams.build_batch_norm(training=(is_training and - not freeze_batchnorm), - name='BoxEncodingPredictor_batchnorm')) - - def _predict(self, features): - """Predicts box encodings. - - Args: - features: A float tensor of shape [batch_size, height, width, - channels] containing features for a batch of images. - - Returns: - box_encodings: A float tensor of shape - [batch_size, 1, num_classes, code_size] representing the location of the - objects. - """ - spatial_averaged_roi_pooled_features = tf.reduce_mean( - features, [1, 2], keep_dims=True, name='AvgPool') - net = spatial_averaged_roi_pooled_features - for layer in self._box_encoder_layers: - net = layer(net) - box_encodings = tf.reshape(net, - [-1, 1, - self._number_of_boxes, - self._box_code_size]) - return box_encodings - - -# TODO(b/128922690): Unify the implementations of ConvolutionalBoxHead -# and WeightSharedConvolutionalBoxHead -class WeightSharedConvolutionalBoxHead(head.KerasHead): - """Weight shared convolutional box prediction head based on Keras. - - This head allows sharing the same set of parameters (weights) when called more - then once on different feature maps. - """ - - def __init__(self, - box_code_size, - num_predictions_per_location, - conv_hyperparams, - kernel_size=3, - use_depthwise=False, - apply_conv_hyperparams_to_heads=False, - box_encodings_clip_range=None, - return_flat_predictions=True, - name=None): - """Constructor. - - Args: - box_code_size: Size of encoding for each box. - num_predictions_per_location: Number of box predictions to be made per - spatial location. Int specifying number of boxes per location. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - kernel_size: Size of final convolution kernel. - use_depthwise: Whether to use depthwise convolutions for prediction steps. - Default is False. - apply_conv_hyperparams_to_heads: Whether to apply conv_hyperparams to - depthwise seperable convolution layers in the box and class heads. By - default, the conv_hyperparams are only applied to layers in the - predictor tower when using depthwise separable convolutions. - box_encodings_clip_range: Min and max values for clipping box_encodings. - return_flat_predictions: If true, returns flattened prediction tensor - of shape [batch, height * width * num_predictions_per_location, - box_coder]. Otherwise returns the prediction tensor before reshaping, - whose shape is [batch, height, width, num_predictions_per_location * - num_class_slots]. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - - Raises: - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(WeightSharedConvolutionalBoxHead, self).__init__(name=name) - self._box_code_size = box_code_size - self._kernel_size = kernel_size - self._num_predictions_per_location = num_predictions_per_location - self._use_depthwise = use_depthwise - self._apply_conv_hyperparams_to_heads = apply_conv_hyperparams_to_heads - self._box_encodings_clip_range = box_encodings_clip_range - self._return_flat_predictions = return_flat_predictions - - self._box_encoder_layers = [] - - if self._use_depthwise: - kwargs = conv_hyperparams.params(use_bias=True) - if self._apply_conv_hyperparams_to_heads: - kwargs['depthwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['depthwise_initializer'] = kwargs['kernel_initializer'] - kwargs['pointwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['pointwise_initializer'] = kwargs['kernel_initializer'] - self._box_encoder_layers.append( - tf.keras.layers.SeparableConv2D( - num_predictions_per_location * self._box_code_size, - [self._kernel_size, self._kernel_size], - padding='SAME', - name='BoxPredictor', - **kwargs)) - else: - self._box_encoder_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * self._box_code_size, - [self._kernel_size, self._kernel_size], - padding='SAME', - name='BoxPredictor', - **conv_hyperparams.params(use_bias=True))) - - def _predict(self, features): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - - Returns: - box_encodings: A float tensor of shape - [batch_size, num_anchors, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. - """ - box_encodings = features - for layer in self._box_encoder_layers: - box_encodings = layer(box_encodings) - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - # Clipping the box encodings to make the inference graph TPU friendly. - if self._box_encodings_clip_range is not None: - box_encodings = tf.clip_by_value( - box_encodings, self._box_encodings_clip_range.min, - self._box_encodings_clip_range.max) - if self._return_flat_predictions: - box_encodings = tf.reshape(box_encodings, - [batch_size, -1, self._box_code_size]) - return box_encodings diff --git a/research/object_detection/predictors/heads/keras_box_head_tf2_test.py b/research/object_detection/predictors/heads/keras_box_head_tf2_test.py deleted file mode 100644 index e9e8b8dcc3a..00000000000 --- a/research/object_detection/predictors/heads/keras_box_head_tf2_test.py +++ /dev/null @@ -1,199 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.heads.box_head.""" -import unittest -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors.heads import keras_box_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ConvolutionalKerasBoxHeadTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_prediction_size_depthwise_false(self): - conv_hyperparams = self._build_conv_hyperparams() - box_prediction_head = keras_box_head.ConvolutionalBoxHead( - is_training=True, - box_code_size=4, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=False) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_encodings = box_prediction_head(image_feature) - return box_encodings - box_encodings = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 1, 4], box_encodings.shape) - - def test_prediction_size_depthwise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - box_prediction_head = keras_box_head.ConvolutionalBoxHead( - is_training=True, - box_code_size=4, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=True) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_encodings = box_prediction_head(image_feature) - return box_encodings - box_encodings = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 1, 4], box_encodings.shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class MaskRCNNKerasBoxHeadTest(test_case.TestCase): - - def _build_fc_hyperparams( - self, op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.KerasLayerHyperparams(hyperparams) - - def test_prediction_size(self): - box_prediction_head = keras_box_head.MaskRCNNBoxHead( - is_training=False, - num_classes=20, - fc_hyperparams=self._build_fc_hyperparams(), - freeze_batchnorm=False, - use_dropout=True, - dropout_keep_prob=0.5, - box_code_size=4, - share_box_across_classes=False) - def graph_fn(): - roi_pooled_features = tf.random_uniform( - [64, 7, 7, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = box_prediction_head(roi_pooled_features) - return prediction - prediction = self.execute(graph_fn, []) - self.assertAllEqual([64, 1, 20, 4], prediction.shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class WeightSharedConvolutionalKerasBoxHead(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_prediction_size_depthwise_false(self): - conv_hyperparams = self._build_conv_hyperparams() - box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( - box_code_size=4, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=False) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_encodings = box_prediction_head(image_feature) - return box_encodings - box_encodings = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 4], box_encodings.shape) - - def test_prediction_size_depthwise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( - box_code_size=4, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=True) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_encodings = box_prediction_head(image_feature) - return box_encodings - box_encodings = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 4], box_encodings.shape) - - def test_variable_count_depth_wise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( - box_code_size=4, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=True) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_prediction_head(image_feature) - self.assertEqual(len(box_prediction_head.variables), 3) - - def test_variable_count_depth_wise_False(self): - conv_hyperparams = self._build_conv_hyperparams() - box_prediction_head = keras_box_head.WeightSharedConvolutionalBoxHead( - box_code_size=4, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=False) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - box_prediction_head(image_feature) - self.assertEqual(len(box_prediction_head.variables), 2) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/heads/keras_class_head.py b/research/object_detection/predictors/heads/keras_class_head.py deleted file mode 100644 index 596f951d42e..00000000000 --- a/research/object_detection/predictors/heads/keras_class_head.py +++ /dev/null @@ -1,375 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Class Head. - -Contains Class prediction head classes for different meta architectures. -All the class prediction heads have a predict function that receives the -`features` as the first argument and returns class predictions with background. -""" -import tensorflow.compat.v1 as tf - -from object_detection.predictors.heads import head -from object_detection.utils import shape_utils - - -class ConvolutionalClassHead(head.KerasHead): - """Convolutional class prediction head.""" - - def __init__(self, - is_training, - num_class_slots, - use_dropout, - dropout_keep_prob, - kernel_size, - num_predictions_per_location, - conv_hyperparams, - freeze_batchnorm, - class_prediction_bias_init=0.0, - use_depthwise=False, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_class_slots: number of class slots. Note that num_class_slots may or - may not include an implicit background category. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - num_predictions_per_location: Number of box predictions to be made per - spatial location. Int specifying number of boxes per location. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - class_prediction_bias_init: constant value to initialize bias of the last - conv2d layer before class prediction. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - - Raises: - ValueError: if min_depth > max_depth. - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(ConvolutionalClassHead, self).__init__(name=name) - self._is_training = is_training - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._kernel_size = kernel_size - self._class_prediction_bias_init = class_prediction_bias_init - self._use_depthwise = use_depthwise - self._num_class_slots = num_class_slots - - self._class_predictor_layers = [] - - if self._use_dropout: - self._class_predictor_layers.append( - # The Dropout layer's `training` parameter for the call method must - # be set implicitly by the Keras set_learning_phase. The object - # detection training code takes care of this. - tf.keras.layers.Dropout(rate=1.0 - self._dropout_keep_prob)) - if self._use_depthwise: - self._class_predictor_layers.append( - tf.keras.layers.DepthwiseConv2D( - [self._kernel_size, self._kernel_size], - padding='SAME', - depth_multiplier=1, - strides=1, - dilation_rate=1, - name='ClassPredictor_depthwise', - **conv_hyperparams.params())) - self._class_predictor_layers.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name='ClassPredictor_depthwise_batchnorm')) - self._class_predictor_layers.append( - conv_hyperparams.build_activation_layer( - name='ClassPredictor_depthwise_activation')) - self._class_predictor_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * self._num_class_slots, [1, 1], - name='ClassPredictor', - **conv_hyperparams.params(use_bias=True))) - else: - self._class_predictor_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * self._num_class_slots, - [self._kernel_size, self._kernel_size], - padding='SAME', - name='ClassPredictor', - bias_initializer=tf.constant_initializer( - self._class_prediction_bias_init), - **conv_hyperparams.params(use_bias=True))) - - def _predict(self, features): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - - Returns: - class_predictions_with_background: A float tensor of shape - [batch_size, num_anchors, num_class_slots] representing the class - predictions for the proposals. - """ - class_predictions_with_background = features - for layer in self._class_predictor_layers: - class_predictions_with_background = layer( - class_predictions_with_background) - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [batch_size, -1, self._num_class_slots]) - return class_predictions_with_background - - -class MaskRCNNClassHead(head.KerasHead): - """Mask RCNN class prediction head. - - This is a piece of Mask RCNN which is responsible for predicting - just the class scores of boxes. - - Please refer to Mask RCNN paper: - https://arxiv.org/abs/1703.06870 - """ - - def __init__(self, - is_training, - num_class_slots, - fc_hyperparams, - freeze_batchnorm, - use_dropout, - dropout_keep_prob, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_class_slots: number of class slots. Note that num_class_slots may or - may not include an implicit background category. - fc_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for fully connected dense ops. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - name: A string name scope to assign to the class head. If `None`, Keras - will auto-generate one from the class name. - """ - super(MaskRCNNClassHead, self).__init__(name=name) - self._is_training = is_training - self._freeze_batchnorm = freeze_batchnorm - self._num_class_slots = num_class_slots - self._fc_hyperparams = fc_hyperparams - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - - self._class_predictor_layers = [tf.keras.layers.Flatten()] - - if self._use_dropout: - self._class_predictor_layers.append( - tf.keras.layers.Dropout(rate=1.0 - self._dropout_keep_prob)) - - self._class_predictor_layers.append( - tf.keras.layers.Dense(self._num_class_slots, - name='ClassPredictor_dense')) - self._class_predictor_layers.append( - fc_hyperparams.build_batch_norm(training=(is_training and - not freeze_batchnorm), - name='ClassPredictor_batchnorm')) - - def _predict(self, features): - """Predicts the class scores for boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing features for a batch of images. - - Returns: - class_predictions_with_background: A float tensor of shape - [batch_size, 1, num_class_slots] representing the class predictions for - the proposals. - """ - spatial_averaged_roi_pooled_features = tf.reduce_mean( - features, [1, 2], keep_dims=True, name='AvgPool') - net = spatial_averaged_roi_pooled_features - for layer in self._class_predictor_layers: - net = layer(net) - class_predictions_with_background = tf.reshape( - net, - [-1, 1, self._num_class_slots]) - return class_predictions_with_background - - -class WeightSharedConvolutionalClassHead(head.KerasHead): - """Weight shared convolutional class prediction head. - - This head allows sharing the same set of parameters (weights) when called more - then once on different feature maps. - """ - - def __init__(self, - num_class_slots, - num_predictions_per_location, - conv_hyperparams, - kernel_size=3, - class_prediction_bias_init=0.0, - use_dropout=False, - dropout_keep_prob=0.8, - use_depthwise=False, - apply_conv_hyperparams_to_heads=False, - score_converter_fn=tf.identity, - return_flat_predictions=True, - name=None): - """Constructor. - - Args: - num_class_slots: number of class slots. Note that num_class_slots may or - may not include an implicit background category. - num_predictions_per_location: Number of box predictions to be made per - spatial location. Int specifying number of boxes per location. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - kernel_size: Size of final convolution kernel. - class_prediction_bias_init: constant value to initialize bias of the last - conv2d layer before class prediction. - use_dropout: Whether to apply dropout to class prediction head. - dropout_keep_prob: Probability of keeping activiations. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - apply_conv_hyperparams_to_heads: Whether to apply conv_hyperparams to - depthwise seperable convolution layers in the box and class heads. By - default, the conv_hyperparams are only applied to layers in the - predictor tower when using depthwise separable convolutions. - score_converter_fn: Callable elementwise nonlinearity (that takes tensors - as inputs and returns tensors). - return_flat_predictions: If true, returns flattened prediction tensor - of shape [batch, height * width * num_predictions_per_location, - box_coder]. Otherwise returns the prediction tensor before reshaping, - whose shape is [batch, height, width, num_predictions_per_location * - num_class_slots]. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - - Raises: - ValueError: if use_depthwise is True and kernel_size is 1. - """ - if use_depthwise and (kernel_size == 1): - raise ValueError('Should not use 1x1 kernel when using depthwise conv') - - super(WeightSharedConvolutionalClassHead, self).__init__(name=name) - self._num_class_slots = num_class_slots - self._num_predictions_per_location = num_predictions_per_location - self._kernel_size = kernel_size - self._class_prediction_bias_init = class_prediction_bias_init - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._use_depthwise = use_depthwise - self._apply_conv_hyperparams_to_heads = apply_conv_hyperparams_to_heads - self._score_converter_fn = score_converter_fn - self._return_flat_predictions = return_flat_predictions - - self._class_predictor_layers = [] - - if self._use_dropout: - self._class_predictor_layers.append( - tf.keras.layers.Dropout(rate=1.0 - self._dropout_keep_prob)) - if self._use_depthwise: - kwargs = conv_hyperparams.params(use_bias=True) - if self._apply_conv_hyperparams_to_heads: - kwargs['depthwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['depthwise_initializer'] = kwargs['kernel_initializer'] - kwargs['pointwise_regularizer'] = kwargs['kernel_regularizer'] - kwargs['pointwise_initializer'] = kwargs['kernel_initializer'] - self._class_predictor_layers.append( - tf.keras.layers.SeparableConv2D( - num_predictions_per_location * self._num_class_slots, - [self._kernel_size, self._kernel_size], - padding='SAME', - depth_multiplier=1, - strides=1, - name='ClassPredictor', - bias_initializer=tf.constant_initializer( - self._class_prediction_bias_init), - **kwargs)) - else: - self._class_predictor_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * self._num_class_slots, - [self._kernel_size, self._kernel_size], - padding='SAME', - name='ClassPredictor', - bias_initializer=tf.constant_initializer( - self._class_prediction_bias_init), - **conv_hyperparams.params(use_bias=True))) - - def _predict(self, features): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - - Returns: - class_predictions_with_background: A float tensor of shape - [batch_size, num_anchors, num_class_slots] representing the class - predictions for the proposals. - """ - class_predictions_with_background = features - for layer in self._class_predictor_layers: - class_predictions_with_background = layer( - class_predictions_with_background) - batch_size, height, width = shape_utils.combined_static_and_dynamic_shape( - features)[0:3] - class_predictions_with_background = tf.reshape( - class_predictions_with_background, [ - batch_size, height, width, self._num_predictions_per_location, - self._num_class_slots - ]) - class_predictions_with_background = self._score_converter_fn( - class_predictions_with_background) - if self._return_flat_predictions: - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [batch_size, -1, self._num_class_slots]) - else: - class_predictions_with_background = tf.reshape( - class_predictions_with_background, [ - batch_size, height, width, - self._num_predictions_per_location * self._num_class_slots - ]) - return class_predictions_with_background diff --git a/research/object_detection/predictors/heads/keras_class_head_tf2_test.py b/research/object_detection/predictors/heads/keras_class_head_tf2_test.py deleted file mode 100644 index 6aa240e98ed..00000000000 --- a/research/object_detection/predictors/heads/keras_class_head_tf2_test.py +++ /dev/null @@ -1,236 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.heads.class_head.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors.heads import keras_class_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ConvolutionalKerasClassPredictorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_prediction_size_depthwise_false(self): - conv_hyperparams = self._build_conv_hyperparams() - class_prediction_head = keras_class_head.ConvolutionalClassHead( - is_training=True, - num_class_slots=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=False) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_predictions = class_prediction_head(image_feature,) - return class_predictions - class_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 20], class_predictions.shape) - - def test_prediction_size_depthwise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - class_prediction_head = keras_class_head.ConvolutionalClassHead( - is_training=True, - num_class_slots=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=True) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_predictions = class_prediction_head(image_feature,) - return class_predictions - class_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 20], class_predictions.shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class MaskRCNNClassHeadTest(test_case.TestCase): - - def _build_fc_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.KerasLayerHyperparams(hyperparams) - - def test_prediction_size(self): - class_prediction_head = keras_class_head.MaskRCNNClassHead( - is_training=False, - num_class_slots=20, - fc_hyperparams=self._build_fc_hyperparams(), - freeze_batchnorm=False, - use_dropout=True, - dropout_keep_prob=0.5) - def graph_fn(): - roi_pooled_features = tf.random_uniform( - [64, 7, 7, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = class_prediction_head(roi_pooled_features) - return prediction - prediction = self.execute(graph_fn, []) - self.assertAllEqual([64, 1, 20], prediction.shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class WeightSharedConvolutionalKerasClassPredictorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_prediction_size_depthwise_false(self): - conv_hyperparams = self._build_conv_hyperparams() - class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead( - num_class_slots=20, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=False) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_predictions = class_prediction_head(image_feature) - return class_predictions - class_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 20], class_predictions.shape) - - def test_prediction_size_depthwise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead( - num_class_slots=20, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=True) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_predictions = class_prediction_head(image_feature) - return class_predictions - class_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 20], class_predictions.shape) - - def test_variable_count_depth_wise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - class_prediction_head = ( - keras_class_head.WeightSharedConvolutionalClassHead( - num_class_slots=20, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=True)) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_prediction_head(image_feature) - self.assertEqual(len(class_prediction_head.variables), 3) - - def test_variable_count_depth_wise_False(self): - conv_hyperparams = self._build_conv_hyperparams() - class_prediction_head = ( - keras_class_head.WeightSharedConvolutionalClassHead( - num_class_slots=20, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=1, - use_depthwise=False)) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - class_prediction_head(image_feature) - self.assertEqual(len(class_prediction_head.variables), 2) - - def test_softmax_score_converter(self): - num_class_slots = 10 - batch_size = 2 - height = 17 - width = 19 - num_predictions_per_location = 2 - assert num_predictions_per_location != 1 - - conv_hyperparams = self._build_conv_hyperparams() - class_prediction_head = keras_class_head.WeightSharedConvolutionalClassHead( - num_class_slots=num_class_slots, - conv_hyperparams=conv_hyperparams, - num_predictions_per_location=num_predictions_per_location, - score_converter_fn=tf.nn.softmax) - - def graph_fn(): - image_feature = tf.random_uniform([batch_size, height, width, 1024], - minval=-10.0, - maxval=10.0, - dtype=tf.float32) - class_predictions = class_prediction_head(image_feature) - return class_predictions - - class_predictions_out = self.execute(graph_fn, []) - class_predictions_sum = np.sum(class_predictions_out, axis=-1) - num_anchors = height * width * num_predictions_per_location - exp_class_predictions_sum = np.ones((batch_size, num_anchors), - dtype=np.float32) - self.assertAllEqual((batch_size, num_anchors, num_class_slots), - class_predictions_out.shape) - self.assertAllClose(class_predictions_sum, exp_class_predictions_sum) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/heads/keras_mask_head.py b/research/object_detection/predictors/heads/keras_mask_head.py deleted file mode 100644 index 277a3f16d76..00000000000 --- a/research/object_detection/predictors/heads/keras_mask_head.py +++ /dev/null @@ -1,446 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Keras Mask Heads. - -Contains Mask prediction head classes for different meta architectures. -All the mask prediction heads have a predict function that receives the -`features` as the first argument and returns `mask_predictions`. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math -from six.moves import range -import tensorflow.compat.v1 as tf - -from object_detection.predictors.heads import head -from object_detection.utils import ops -from object_detection.utils import shape_utils - - -class ConvolutionalMaskHead(head.KerasHead): - """Convolutional class prediction head.""" - - def __init__(self, - is_training, - num_classes, - use_dropout, - dropout_keep_prob, - kernel_size, - num_predictions_per_location, - conv_hyperparams, - freeze_batchnorm, - use_depthwise=False, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=False, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: Number of classes. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - num_predictions_per_location: Number of box predictions to be made per - spatial location. Int specifying number of boxes per location. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Bool. Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - mask_height: Desired output mask height. The default value is 7. - mask_width: Desired output mask width. The default value is 7. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - - Raises: - ValueError: if min_depth > max_depth. - """ - super(ConvolutionalMaskHead, self).__init__(name=name) - self._is_training = is_training - self._num_classes = num_classes - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._kernel_size = kernel_size - self._num_predictions_per_location = num_predictions_per_location - self._use_depthwise = use_depthwise - self._mask_height = mask_height - self._mask_width = mask_width - self._masks_are_class_agnostic = masks_are_class_agnostic - - self._mask_predictor_layers = [] - - # Add a slot for the background class. - if self._masks_are_class_agnostic: - self._num_masks = 1 - else: - self._num_masks = self._num_classes - - num_mask_channels = self._num_masks * self._mask_height * self._mask_width - - if self._use_dropout: - self._mask_predictor_layers.append( - # The Dropout layer's `training` parameter for the call method must - # be set implicitly by the Keras set_learning_phase. The object - # detection training code takes care of this. - tf.keras.layers.Dropout(rate=1.0 - self._dropout_keep_prob)) - if self._use_depthwise: - self._mask_predictor_layers.append( - tf.keras.layers.DepthwiseConv2D( - [self._kernel_size, self._kernel_size], - padding='SAME', - depth_multiplier=1, - strides=1, - dilation_rate=1, - name='MaskPredictor_depthwise', - **conv_hyperparams.params())) - self._mask_predictor_layers.append( - conv_hyperparams.build_batch_norm( - training=(is_training and not freeze_batchnorm), - name='MaskPredictor_depthwise_batchnorm')) - self._mask_predictor_layers.append( - conv_hyperparams.build_activation_layer( - name='MaskPredictor_depthwise_activation')) - self._mask_predictor_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * num_mask_channels, [1, 1], - name='MaskPredictor', - **conv_hyperparams.params(use_bias=True))) - else: - self._mask_predictor_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * num_mask_channels, - [self._kernel_size, self._kernel_size], - padding='SAME', - name='MaskPredictor', - **conv_hyperparams.params(use_bias=True))) - - def _predict(self, features): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - - Returns: - mask_predictions: A float tensors of shape - [batch_size, num_anchors, num_masks, mask_height, mask_width] - representing the mask predictions for the proposals. - """ - mask_predictions = features - for layer in self._mask_predictor_layers: - mask_predictions = layer(mask_predictions) - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - mask_predictions = tf.reshape( - mask_predictions, - [batch_size, -1, self._num_masks, self._mask_height, self._mask_width]) - return mask_predictions - - -class MaskRCNNMaskHead(head.KerasHead): - """Mask RCNN mask prediction head. - - This is a piece of Mask RCNN which is responsible for predicting - just the pixelwise foreground scores for regions within the boxes. - - Please refer to Mask RCNN paper: - https://arxiv.org/abs/1703.06870 - """ - - def __init__(self, - is_training, - num_classes, - freeze_batchnorm, - conv_hyperparams, - mask_height=14, - mask_width=14, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=False, - convolve_then_upsample=False, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the Mask head is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - mask_height: Desired output mask height. The default value is 14. - mask_width: Desired output mask width. The default value is 14. - mask_prediction_num_conv_layers: Number of convolution layers applied to - the image_features in mask prediction branch. - mask_prediction_conv_depth: The depth for the first conv2d_transpose op - applied to the image_features in the mask prediction branch. If set - to 0, the depth of the convolution layers will be automatically chosen - based on the number of object classes and the number of channels in the - image features. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - convolve_then_upsample: Whether to apply convolutions on mask features - before upsampling using nearest neighbor resizing. Otherwise, mask - features are resized to [`mask_height`, `mask_width`] using bilinear - resizing before applying convolutions. - name: A string name scope to assign to the mask head. If `None`, Keras - will auto-generate one from the class name. - """ - super(MaskRCNNMaskHead, self).__init__(name=name) - self._is_training = is_training - self._freeze_batchnorm = freeze_batchnorm - self._num_classes = num_classes - self._conv_hyperparams = conv_hyperparams - self._mask_height = mask_height - self._mask_width = mask_width - self._mask_prediction_num_conv_layers = mask_prediction_num_conv_layers - self._mask_prediction_conv_depth = mask_prediction_conv_depth - self._masks_are_class_agnostic = masks_are_class_agnostic - self._convolve_then_upsample = convolve_then_upsample - - self._mask_predictor_layers = [] - - def build(self, input_shapes): - num_conv_channels = self._mask_prediction_conv_depth - if num_conv_channels == 0: - num_feature_channels = input_shapes.as_list()[3] - num_conv_channels = self._get_mask_predictor_conv_depth( - num_feature_channels, self._num_classes) - - for i in range(self._mask_prediction_num_conv_layers - 1): - self._mask_predictor_layers.append( - tf.keras.layers.Conv2D( - num_conv_channels, - [3, 3], - padding='SAME', - name='MaskPredictor_conv2d_{}'.format(i), - **self._conv_hyperparams.params())) - self._mask_predictor_layers.append( - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='MaskPredictor_batchnorm_{}'.format(i))) - self._mask_predictor_layers.append( - self._conv_hyperparams.build_activation_layer( - name='MaskPredictor_activation_{}'.format(i))) - - if self._convolve_then_upsample: - # Replace Transposed Convolution with a Nearest Neighbor upsampling step - # followed by 3x3 convolution. - height_scale = self._mask_height // shape_utils.get_dim_as_int( - input_shapes[1]) - width_scale = self._mask_width // shape_utils.get_dim_as_int( - input_shapes[2]) - # pylint: disable=g-long-lambda - self._mask_predictor_layers.append(tf.keras.layers.Lambda( - lambda features: ops.nearest_neighbor_upsampling( - features, height_scale=height_scale, width_scale=width_scale) - )) - # pylint: enable=g-long-lambda - self._mask_predictor_layers.append( - tf.keras.layers.Conv2D( - num_conv_channels, - [3, 3], - padding='SAME', - name='MaskPredictor_upsample_conv2d', - **self._conv_hyperparams.params())) - self._mask_predictor_layers.append( - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='MaskPredictor_upsample_batchnorm')) - self._mask_predictor_layers.append( - self._conv_hyperparams.build_activation_layer( - name='MaskPredictor_upsample_activation')) - - num_masks = 1 if self._masks_are_class_agnostic else self._num_classes - self._mask_predictor_layers.append( - tf.keras.layers.Conv2D( - num_masks, - [3, 3], - padding='SAME', - name='MaskPredictor_last_conv2d', - **self._conv_hyperparams.params(use_bias=True))) - - self.built = True - - def _get_mask_predictor_conv_depth(self, - num_feature_channels, - num_classes, - class_weight=3.0, - feature_weight=2.0): - """Computes the depth of the mask predictor convolutions. - - Computes the depth of the mask predictor convolutions given feature channels - and number of classes by performing a weighted average of the two in - log space to compute the number of convolution channels. The weights that - are used for computing the weighted average do not need to sum to 1. - - Args: - num_feature_channels: An integer containing the number of feature - channels. - num_classes: An integer containing the number of classes. - class_weight: Class weight used in computing the weighted average. - feature_weight: Feature weight used in computing the weighted average. - - Returns: - An integer containing the number of convolution channels used by mask - predictor. - """ - num_feature_channels_log = math.log(float(num_feature_channels), 2.0) - num_classes_log = math.log(float(num_classes), 2.0) - weighted_num_feature_channels_log = ( - num_feature_channels_log * feature_weight) - weighted_num_classes_log = num_classes_log * class_weight - total_weight = feature_weight + class_weight - num_conv_channels_log = round( - (weighted_num_feature_channels_log + weighted_num_classes_log) / - total_weight) - return int(math.pow(2.0, num_conv_channels_log)) - - def _predict(self, features): - """Predicts pixelwise foreground scores for regions within the boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing features for a batch of images. - - Returns: - instance_masks: A float tensor of shape - [batch_size, 1, num_classes, mask_height, mask_width]. - """ - if not self._convolve_then_upsample: - features = tf.image.resize_bilinear( - features, [self._mask_height, self._mask_width], - align_corners=True) - - mask_predictions = features - for layer in self._mask_predictor_layers: - mask_predictions = layer(mask_predictions) - return tf.expand_dims( - tf.transpose(mask_predictions, perm=[0, 3, 1, 2]), - axis=1, - name='MaskPredictor') - - -class WeightSharedConvolutionalMaskHead(head.KerasHead): - """Weight shared convolutional mask prediction head based on Keras.""" - - def __init__(self, - num_classes, - num_predictions_per_location, - conv_hyperparams, - kernel_size=3, - use_dropout=False, - dropout_keep_prob=0.8, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=False, - name=None): - """Constructor. - - Args: - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - num_predictions_per_location: Number of box predictions to be made per - spatial location. Int specifying number of boxes per location. - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - kernel_size: Size of final convolution kernel. - use_dropout: Whether to apply dropout to class prediction head. - dropout_keep_prob: Probability of keeping activiations. - mask_height: Desired output mask height. The default value is 7. - mask_width: Desired output mask width. The default value is 7. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - - Raises: - ValueError: if min_depth > max_depth. - """ - super(WeightSharedConvolutionalMaskHead, self).__init__(name=name) - self._num_classes = num_classes - self._num_predictions_per_location = num_predictions_per_location - self._kernel_size = kernel_size - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._mask_height = mask_height - self._mask_width = mask_width - self._masks_are_class_agnostic = masks_are_class_agnostic - - self._mask_predictor_layers = [] - - if self._masks_are_class_agnostic: - self._num_masks = 1 - else: - self._num_masks = self._num_classes - num_mask_channels = self._num_masks * self._mask_height * self._mask_width - - if self._use_dropout: - self._mask_predictor_layers.append( - tf.keras.layers.Dropout(rate=1.0 - self._dropout_keep_prob)) - self._mask_predictor_layers.append( - tf.keras.layers.Conv2D( - num_predictions_per_location * num_mask_channels, - [self._kernel_size, self._kernel_size], - padding='SAME', - name='MaskPredictor', - **conv_hyperparams.params(use_bias=True))) - - def _predict(self, features): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - - Returns: - mask_predictions: A tensor of shape - [batch_size, num_anchors, num_classes, mask_height, mask_width] - representing the mask predictions for the proposals. - """ - mask_predictions = features - for layer in self._mask_predictor_layers: - mask_predictions = layer(mask_predictions) - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - mask_predictions = tf.reshape( - mask_predictions, - [batch_size, -1, self._num_masks, self._mask_height, self._mask_width]) - return mask_predictions diff --git a/research/object_detection/predictors/heads/keras_mask_head_tf2_test.py b/research/object_detection/predictors/heads/keras_mask_head_tf2_test.py deleted file mode 100644 index 5465be06fe1..00000000000 --- a/research/object_detection/predictors/heads/keras_mask_head_tf2_test.py +++ /dev/null @@ -1,252 +0,0 @@ -# Copyright 2018 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.heads.mask_head.""" -import unittest -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors.heads import keras_mask_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class ConvolutionalMaskPredictorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_prediction_size_use_depthwise_false(self): - conv_hyperparams = self._build_conv_hyperparams() - mask_prediction_head = keras_mask_head.ConvolutionalMaskHead( - is_training=True, - num_classes=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=False, - mask_height=7, - mask_width=7) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head(image_feature) - return mask_predictions - mask_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 20, 7, 7], mask_predictions.shape) - - def test_prediction_size_use_depthwise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - mask_prediction_head = keras_mask_head.ConvolutionalMaskHead( - is_training=True, - num_classes=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=True, - mask_height=7, - mask_width=7) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head(image_feature) - return mask_predictions - mask_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 20, 7, 7], mask_predictions.shape) - - def test_class_agnostic_prediction_size_use_depthwise_false(self): - conv_hyperparams = self._build_conv_hyperparams() - mask_prediction_head = keras_mask_head.ConvolutionalMaskHead( - is_training=True, - num_classes=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=False, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=True) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head(image_feature) - return mask_predictions - mask_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 1, 7, 7], mask_predictions.shape) - - def test_class_agnostic_prediction_size_use_depthwise_true(self): - conv_hyperparams = self._build_conv_hyperparams() - mask_prediction_head = keras_mask_head.ConvolutionalMaskHead( - is_training=True, - num_classes=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - conv_hyperparams=conv_hyperparams, - freeze_batchnorm=False, - num_predictions_per_location=1, - use_depthwise=True, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=True) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head(image_feature) - return mask_predictions - mask_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 1, 7, 7], mask_predictions.shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class MaskRCNNMaskHeadTest(test_case.TestCase): - - def _build_conv_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.CONV): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.KerasLayerHyperparams(hyperparams) - - def test_prediction_size(self): - mask_prediction_head = keras_mask_head.MaskRCNNMaskHead( - is_training=True, - num_classes=20, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - mask_height=14, - mask_width=14, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=False) - def graph_fn(): - roi_pooled_features = tf.random_uniform( - [64, 7, 7, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = mask_prediction_head(roi_pooled_features) - return prediction - prediction = self.execute(graph_fn, []) - self.assertAllEqual([64, 1, 20, 14, 14], prediction.shape) - - def test_prediction_size_with_convolve_then_upsample(self): - mask_prediction_head = keras_mask_head.MaskRCNNMaskHead( - is_training=True, - num_classes=20, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - mask_height=28, - mask_width=28, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=True, - convolve_then_upsample=True) - def graph_fn(): - roi_pooled_features = tf.random_uniform( - [64, 14, 14, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = mask_prediction_head(roi_pooled_features) - return prediction - prediction = self.execute(graph_fn, []) - self.assertAllEqual([64, 1, 1, 28, 28], prediction.shape) - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class WeightSharedConvolutionalMaskPredictorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_prediction_size(self): - mask_prediction_head = ( - keras_mask_head.WeightSharedConvolutionalMaskHead( - num_classes=20, - num_predictions_per_location=1, - conv_hyperparams=self._build_conv_hyperparams(), - mask_height=7, - mask_width=7)) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head(image_feature) - return mask_predictions - mask_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 20, 7, 7], mask_predictions.shape) - - def test_class_agnostic_prediction_size(self): - mask_prediction_head = ( - keras_mask_head.WeightSharedConvolutionalMaskHead( - num_classes=20, - num_predictions_per_location=1, - conv_hyperparams=self._build_conv_hyperparams(), - mask_height=7, - mask_width=7, - masks_are_class_agnostic=True)) - def graph_fn(): - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head(image_feature) - return mask_predictions - mask_predictions = self.execute(graph_fn, []) - self.assertAllEqual([64, 323, 1, 7, 7], mask_predictions.shape) - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/heads/keypoint_head.py b/research/object_detection/predictors/heads/keypoint_head.py deleted file mode 100644 index 55494050e64..00000000000 --- a/research/object_detection/predictors/heads/keypoint_head.py +++ /dev/null @@ -1,114 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Keypoint Head. - -Contains Keypoint prediction head classes for different meta architectures. -All the keypoint prediction heads have a predict function that receives the -`features` as the first argument and returns `keypoint_predictions`. -Keypoints could be used to represent the human body joint locations as in -Mask RCNN paper. Or they could be used to represent different part locations of -objects. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.predictors.heads import head - - -class MaskRCNNKeypointHead(head.Head): - """Mask RCNN keypoint prediction head. - - Please refer to Mask RCNN paper: - https://arxiv.org/abs/1703.06870 - """ - - def __init__(self, - num_keypoints=17, - conv_hyperparams_fn=None, - keypoint_heatmap_height=56, - keypoint_heatmap_width=56, - keypoint_prediction_num_conv_layers=8, - keypoint_prediction_conv_depth=512): - """Constructor. - - Args: - num_keypoints: (int scalar) number of keypoints. - conv_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for convolution ops. - keypoint_heatmap_height: Desired output mask height. The default value - is 14. - keypoint_heatmap_width: Desired output mask width. The default value - is 14. - keypoint_prediction_num_conv_layers: Number of convolution layers applied - to the image_features in mask prediction branch. - keypoint_prediction_conv_depth: The depth for the first conv2d_transpose - op applied to the image_features in the mask prediction branch. If set - to 0, the depth of the convolution layers will be automatically chosen - based on the number of object classes and the number of channels in the - image features. - """ - super(MaskRCNNKeypointHead, self).__init__() - self._num_keypoints = num_keypoints - self._conv_hyperparams_fn = conv_hyperparams_fn - self._keypoint_heatmap_height = keypoint_heatmap_height - self._keypoint_heatmap_width = keypoint_heatmap_width - self._keypoint_prediction_num_conv_layers = ( - keypoint_prediction_num_conv_layers) - self._keypoint_prediction_conv_depth = keypoint_prediction_conv_depth - - def predict(self, features, num_predictions_per_location=1): - """Performs keypoint prediction. - - Args: - features: A float tensor of shape [batch_size, height, width, - channels] containing features for a batch of images. - num_predictions_per_location: Int containing number of predictions per - location. - - Returns: - instance_masks: A float tensor of shape - [batch_size, 1, num_keypoints, heatmap_height, heatmap_width]. - - Raises: - ValueError: If num_predictions_per_location is not 1. - """ - if num_predictions_per_location != 1: - raise ValueError('Only num_predictions_per_location=1 is supported') - with slim.arg_scope(self._conv_hyperparams_fn()): - net = slim.conv2d( - features, - self._keypoint_prediction_conv_depth, [3, 3], - scope='conv_1') - for i in range(1, self._keypoint_prediction_num_conv_layers): - net = slim.conv2d( - net, - self._keypoint_prediction_conv_depth, [3, 3], - scope='conv_%d' % (i + 1)) - net = slim.conv2d_transpose( - net, self._num_keypoints, [2, 2], scope='deconv1') - heatmaps_mask = tf.image.resize_bilinear( - net, [self._keypoint_heatmap_height, self._keypoint_heatmap_width], - align_corners=True, - name='upsample') - return tf.expand_dims( - tf.transpose(heatmaps_mask, perm=[0, 3, 1, 2]), - axis=1, - name='KeypointPredictor') diff --git a/research/object_detection/predictors/heads/keypoint_head_tf1_test.py b/research/object_detection/predictors/heads/keypoint_head_tf1_test.py deleted file mode 100644 index 82817498913..00000000000 --- a/research/object_detection/predictors/heads/keypoint_head_tf1_test.py +++ /dev/null @@ -1,60 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.heads.keypoint_head.""" -import unittest -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors.heads import keypoint_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class MaskRCNNKeypointHeadTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - keypoint_prediction_head = keypoint_head.MaskRCNNKeypointHead( - conv_hyperparams_fn=self._build_arg_scope_with_hyperparams()) - roi_pooled_features = tf.random_uniform( - [64, 14, 14, 1024], minval=-2.0, maxval=2.0, dtype=tf.float32) - prediction = keypoint_prediction_head.predict( - features=roi_pooled_features, num_predictions_per_location=1) - self.assertAllEqual([64, 1, 17, 56, 56], prediction.get_shape().as_list()) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/heads/mask_head.py b/research/object_detection/predictors/heads/mask_head.py deleted file mode 100644 index 6629a7b646e..00000000000 --- a/research/object_detection/predictors/heads/mask_head.py +++ /dev/null @@ -1,359 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Mask Head. - -Contains Mask prediction head classes for different meta architectures. -All the mask prediction heads have a predict function that receives the -`features` as the first argument and returns `mask_predictions`. -""" -from __future__ import absolute_import -from __future__ import division -from __future__ import print_function - -import math -from six.moves import range -import tensorflow.compat.v1 as tf -import tf_slim as slim - -from object_detection.predictors.heads import head -from object_detection.utils import ops - - -class MaskRCNNMaskHead(head.Head): - """Mask RCNN mask prediction head. - - Please refer to Mask RCNN paper: - https://arxiv.org/abs/1703.06870 - """ - - def __init__(self, - num_classes, - conv_hyperparams_fn=None, - mask_height=14, - mask_width=14, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=False, - convolve_then_upsample=False): - """Constructor. - - Args: - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - conv_hyperparams_fn: A function to generate tf-slim arg_scope with - hyperparameters for convolution ops. - mask_height: Desired output mask height. The default value is 14. - mask_width: Desired output mask width. The default value is 14. - mask_prediction_num_conv_layers: Number of convolution layers applied to - the image_features in mask prediction branch. - mask_prediction_conv_depth: The depth for the first conv2d_transpose op - applied to the image_features in the mask prediction branch. If set - to 0, the depth of the convolution layers will be automatically chosen - based on the number of object classes and the number of channels in the - image features. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - convolve_then_upsample: Whether to apply convolutions on mask features - before upsampling using nearest neighbor resizing. Otherwise, mask - features are resized to [`mask_height`, `mask_width`] using bilinear - resizing before applying convolutions. - - Raises: - ValueError: conv_hyperparams_fn is None. - """ - super(MaskRCNNMaskHead, self).__init__() - self._num_classes = num_classes - self._conv_hyperparams_fn = conv_hyperparams_fn - self._mask_height = mask_height - self._mask_width = mask_width - self._mask_prediction_num_conv_layers = mask_prediction_num_conv_layers - self._mask_prediction_conv_depth = mask_prediction_conv_depth - self._masks_are_class_agnostic = masks_are_class_agnostic - self._convolve_then_upsample = convolve_then_upsample - if conv_hyperparams_fn is None: - raise ValueError('conv_hyperparams_fn is None.') - - def _get_mask_predictor_conv_depth(self, - num_feature_channels, - num_classes, - class_weight=3.0, - feature_weight=2.0): - """Computes the depth of the mask predictor convolutions. - - Computes the depth of the mask predictor convolutions given feature channels - and number of classes by performing a weighted average of the two in - log space to compute the number of convolution channels. The weights that - are used for computing the weighted average do not need to sum to 1. - - Args: - num_feature_channels: An integer containing the number of feature - channels. - num_classes: An integer containing the number of classes. - class_weight: Class weight used in computing the weighted average. - feature_weight: Feature weight used in computing the weighted average. - - Returns: - An integer containing the number of convolution channels used by mask - predictor. - """ - num_feature_channels_log = math.log(float(num_feature_channels), 2.0) - num_classes_log = math.log(float(num_classes), 2.0) - weighted_num_feature_channels_log = ( - num_feature_channels_log * feature_weight) - weighted_num_classes_log = num_classes_log * class_weight - total_weight = feature_weight + class_weight - num_conv_channels_log = round( - (weighted_num_feature_channels_log + weighted_num_classes_log) / - total_weight) - return int(math.pow(2.0, num_conv_channels_log)) - - def predict(self, features, num_predictions_per_location=1): - """Performs mask prediction. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing features for a batch of images. - num_predictions_per_location: Int containing number of predictions per - location. - - Returns: - instance_masks: A float tensor of shape - [batch_size, 1, num_classes, mask_height, mask_width]. - - Raises: - ValueError: If num_predictions_per_location is not 1. - """ - if num_predictions_per_location != 1: - raise ValueError('Only num_predictions_per_location=1 is supported') - num_conv_channels = self._mask_prediction_conv_depth - if num_conv_channels == 0: - num_feature_channels = features.get_shape().as_list()[3] - num_conv_channels = self._get_mask_predictor_conv_depth( - num_feature_channels, self._num_classes) - with slim.arg_scope(self._conv_hyperparams_fn()): - if not self._convolve_then_upsample: - features = tf.image.resize_bilinear( - features, [self._mask_height, self._mask_width], - align_corners=True) - for _ in range(self._mask_prediction_num_conv_layers - 1): - features = slim.conv2d( - features, - num_outputs=num_conv_channels, - kernel_size=[3, 3]) - if self._convolve_then_upsample: - # Replace Transposed Convolution with a Nearest Neighbor upsampling step - # followed by 3x3 convolution. - height_scale = self._mask_height // features.shape[1].value - width_scale = self._mask_width // features.shape[2].value - features = ops.nearest_neighbor_upsampling( - features, height_scale=height_scale, width_scale=width_scale) - features = slim.conv2d( - features, - num_outputs=num_conv_channels, - kernel_size=[3, 3]) - - num_masks = 1 if self._masks_are_class_agnostic else self._num_classes - mask_predictions = slim.conv2d( - features, - num_outputs=num_masks, - activation_fn=None, - normalizer_fn=None, - kernel_size=[3, 3]) - return tf.expand_dims( - tf.transpose(mask_predictions, perm=[0, 3, 1, 2]), - axis=1, - name='MaskPredictor') - - -class ConvolutionalMaskHead(head.Head): - """Convolutional class prediction head.""" - - def __init__(self, - is_training, - num_classes, - use_dropout, - dropout_keep_prob, - kernel_size, - use_depthwise=False, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=False): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: Number of classes. - use_dropout: Option to use dropout or not. Note that a single dropout - op is applied here prior to both box and class predictions, which stands - in contrast to the ConvolutionalBoxPredictor below. - dropout_keep_prob: Keep probability for dropout. - This is only used if use_dropout is True. - kernel_size: Size of final convolution kernel. If the - spatial resolution of the feature map is smaller than the kernel size, - then the kernel size is automatically set to be - min(feature_width, feature_height). - use_depthwise: Whether to use depthwise convolutions for prediction - steps. Default is False. - mask_height: Desired output mask height. The default value is 7. - mask_width: Desired output mask width. The default value is 7. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - - Raises: - ValueError: if min_depth > max_depth. - """ - super(ConvolutionalMaskHead, self).__init__() - self._is_training = is_training - self._num_classes = num_classes - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._kernel_size = kernel_size - self._use_depthwise = use_depthwise - self._mask_height = mask_height - self._mask_width = mask_width - self._masks_are_class_agnostic = masks_are_class_agnostic - - def predict(self, features, num_predictions_per_location): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - num_predictions_per_location: Number of box predictions to be made per - spatial location. - - Returns: - mask_predictions: A float tensors of shape - [batch_size, num_anchors, num_masks, mask_height, mask_width] - representing the mask predictions for the proposals. - """ - image_feature = features - # Add a slot for the background class. - if self._masks_are_class_agnostic: - num_masks = 1 - else: - num_masks = self._num_classes - num_mask_channels = num_masks * self._mask_height * self._mask_width - net = image_feature - if self._use_dropout: - net = slim.dropout(net, keep_prob=self._dropout_keep_prob) - if self._use_depthwise: - mask_predictions = slim.separable_conv2d( - net, None, [self._kernel_size, self._kernel_size], - padding='SAME', depth_multiplier=1, stride=1, - rate=1, scope='MaskPredictor_depthwise') - mask_predictions = slim.conv2d( - mask_predictions, - num_predictions_per_location * num_mask_channels, - [1, 1], - activation_fn=None, - normalizer_fn=None, - normalizer_params=None, - scope='MaskPredictor') - else: - mask_predictions = slim.conv2d( - net, - num_predictions_per_location * num_mask_channels, - [self._kernel_size, self._kernel_size], - activation_fn=None, - normalizer_fn=None, - normalizer_params=None, - scope='MaskPredictor') - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - mask_predictions = tf.reshape( - mask_predictions, - [batch_size, -1, num_masks, self._mask_height, self._mask_width]) - return mask_predictions - - -# TODO(alirezafathi): See if possible to unify Weight Shared with regular -# convolutional mask head. -class WeightSharedConvolutionalMaskHead(head.Head): - """Weight shared convolutional mask prediction head.""" - - def __init__(self, - num_classes, - kernel_size=3, - use_dropout=False, - dropout_keep_prob=0.8, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=False): - """Constructor. - - Args: - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - kernel_size: Size of final convolution kernel. - use_dropout: Whether to apply dropout to class prediction head. - dropout_keep_prob: Probability of keeping activiations. - mask_height: Desired output mask height. The default value is 7. - mask_width: Desired output mask width. The default value is 7. - masks_are_class_agnostic: Boolean determining if the mask-head is - class-agnostic or not. - """ - super(WeightSharedConvolutionalMaskHead, self).__init__() - self._num_classes = num_classes - self._kernel_size = kernel_size - self._use_dropout = use_dropout - self._dropout_keep_prob = dropout_keep_prob - self._mask_height = mask_height - self._mask_width = mask_width - self._masks_are_class_agnostic = masks_are_class_agnostic - - def predict(self, features, num_predictions_per_location): - """Predicts boxes. - - Args: - features: A float tensor of shape [batch_size, height, width, channels] - containing image features. - num_predictions_per_location: Number of box predictions to be made per - spatial location. - - Returns: - mask_predictions: A tensor of shape - [batch_size, num_anchors, num_classes, mask_height, mask_width] - representing the mask predictions for the proposals. - """ - mask_predictions_net = features - if self._masks_are_class_agnostic: - num_masks = 1 - else: - num_masks = self._num_classes - num_mask_channels = num_masks * self._mask_height * self._mask_width - if self._use_dropout: - mask_predictions_net = slim.dropout( - mask_predictions_net, keep_prob=self._dropout_keep_prob) - mask_predictions = slim.conv2d( - mask_predictions_net, - num_predictions_per_location * num_mask_channels, - [self._kernel_size, self._kernel_size], - activation_fn=None, stride=1, padding='SAME', - normalizer_fn=None, - scope='MaskPredictor') - batch_size = features.get_shape().as_list()[0] - if batch_size is None: - batch_size = tf.shape(features)[0] - mask_predictions = tf.reshape( - mask_predictions, - [batch_size, -1, num_masks, self._mask_height, self._mask_width]) - return mask_predictions diff --git a/research/object_detection/predictors/heads/mask_head_tf1_test.py b/research/object_detection/predictors/heads/mask_head_tf1_test.py deleted file mode 100644 index 15239483613..00000000000 --- a/research/object_detection/predictors/heads/mask_head_tf1_test.py +++ /dev/null @@ -1,190 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.heads.mask_head.""" -import unittest -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors.heads import mask_head -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class MaskRCNNMaskHeadTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - mask_prediction_head = mask_head.MaskRCNNMaskHead( - num_classes=20, - conv_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - mask_height=14, - mask_width=14, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=False) - roi_pooled_features = tf.random_uniform( - [64, 7, 7, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = mask_prediction_head.predict( - features=roi_pooled_features, num_predictions_per_location=1) - self.assertAllEqual([64, 1, 20, 14, 14], prediction.get_shape().as_list()) - - def test_prediction_size_with_convolve_then_upsample(self): - mask_prediction_head = mask_head.MaskRCNNMaskHead( - num_classes=20, - conv_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - mask_height=28, - mask_width=28, - mask_prediction_num_conv_layers=2, - mask_prediction_conv_depth=256, - masks_are_class_agnostic=True, - convolve_then_upsample=True) - roi_pooled_features = tf.random_uniform( - [64, 14, 14, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - prediction = mask_prediction_head.predict( - features=roi_pooled_features, num_predictions_per_location=1) - self.assertAllEqual([64, 1, 1, 28, 28], prediction.get_shape().as_list()) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class ConvolutionalMaskPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams( - self, op_type=hyperparams_pb2.Hyperparams.CONV): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - mask_prediction_head = mask_head.ConvolutionalMaskHead( - is_training=True, - num_classes=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - mask_height=7, - mask_width=7) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 20, 7, 7], - mask_predictions.get_shape().as_list()) - - def test_class_agnostic_prediction_size(self): - mask_prediction_head = mask_head.ConvolutionalMaskHead( - is_training=True, - num_classes=20, - use_dropout=True, - dropout_keep_prob=0.5, - kernel_size=3, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=True) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 1, 7, 7], - mask_predictions.get_shape().as_list()) - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class WeightSharedConvolutionalMaskPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams( - self, op_type=hyperparams_pb2.Hyperparams.CONV): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_prediction_size(self): - mask_prediction_head = ( - mask_head.WeightSharedConvolutionalMaskHead( - num_classes=20, - mask_height=7, - mask_width=7)) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 20, 7, 7], - mask_predictions.get_shape().as_list()) - - def test_class_agnostic_prediction_size(self): - mask_prediction_head = ( - mask_head.WeightSharedConvolutionalMaskHead( - num_classes=20, - mask_height=7, - mask_width=7, - masks_are_class_agnostic=True)) - image_feature = tf.random_uniform( - [64, 17, 19, 1024], minval=-10.0, maxval=10.0, dtype=tf.float32) - mask_predictions = mask_prediction_head.predict( - features=image_feature, - num_predictions_per_location=1) - self.assertAllEqual([64, 323, 1, 7, 7], - mask_predictions.get_shape().as_list()) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/mask_rcnn_box_predictor.py b/research/object_detection/predictors/mask_rcnn_box_predictor.py deleted file mode 100644 index 26ff65daabd..00000000000 --- a/research/object_detection/predictors/mask_rcnn_box_predictor.py +++ /dev/null @@ -1,141 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Mask R-CNN Box Predictor.""" -from object_detection.core import box_predictor - - -BOX_ENCODINGS = box_predictor.BOX_ENCODINGS -CLASS_PREDICTIONS_WITH_BACKGROUND = ( - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND) -MASK_PREDICTIONS = box_predictor.MASK_PREDICTIONS - - -class MaskRCNNBoxPredictor(box_predictor.BoxPredictor): - """Mask R-CNN Box Predictor. - - See Mask R-CNN: He, K., Gkioxari, G., Dollar, P., & Girshick, R. (2017). - Mask R-CNN. arXiv preprint arXiv:1703.06870. - - This is used for the second stage of the Mask R-CNN detector where proposals - cropped from an image are arranged along the batch dimension of the input - image_features tensor. Notice that locations are *not* shared across classes, - thus for each anchor, a separate prediction is made for each class. - - In addition to predicting boxes and classes, optionally this class allows - predicting masks and/or keypoints inside detection boxes. - - Currently this box predictor makes per-class predictions; that is, each - anchor makes a separate box prediction for each class. - """ - - def __init__(self, - is_training, - num_classes, - box_prediction_head, - class_prediction_head, - third_stage_heads): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - box_prediction_head: The head that predicts the boxes in second stage. - class_prediction_head: The head that predicts the classes in second stage. - third_stage_heads: A dictionary mapping head names to mask rcnn head - classes. - """ - super(MaskRCNNBoxPredictor, self).__init__(is_training, num_classes) - self._box_prediction_head = box_prediction_head - self._class_prediction_head = class_prediction_head - self._third_stage_heads = third_stage_heads - - @property - def num_classes(self): - return self._num_classes - - def get_second_stage_prediction_heads(self): - return BOX_ENCODINGS, CLASS_PREDICTIONS_WITH_BACKGROUND - - def get_third_stage_prediction_heads(self): - return sorted(self._third_stage_heads.keys()) - - def _predict(self, - image_features, - num_predictions_per_location, - prediction_stage=2): - """Optionally computes encoded object locations, confidences, and masks. - - Predicts the heads belonging to the given prediction stage. - - Args: - image_features: A list of float tensors of shape - [batch_size, height_i, width_i, channels_i] containing roi pooled - features for each image. The length of the list should be 1 otherwise - a ValueError will be raised. - num_predictions_per_location: A list of integers representing the number - of box predictions to be made per spatial location for each feature map. - Currently, this must be set to [1], or an error will be raised. - prediction_stage: Prediction stage. Acceptable values are 2 and 3. - - Returns: - A dictionary containing the predicted tensors that are listed in - self._prediction_heads. A subset of the following keys will exist in the - dictionary: - BOX_ENCODINGS: A float tensor of shape - [batch_size, 1, num_classes, code_size] representing the - location of the objects. - CLASS_PREDICTIONS_WITH_BACKGROUND: A float tensor of shape - [batch_size, 1, num_classes + 1] representing the class - predictions for the proposals. - MASK_PREDICTIONS: A float tensor of shape - [batch_size, 1, num_classes, image_height, image_width] - - Raises: - ValueError: If num_predictions_per_location is not 1 or if - len(image_features) is not 1. - ValueError: if prediction_stage is not 2 or 3. - """ - if (len(num_predictions_per_location) != 1 or - num_predictions_per_location[0] != 1): - raise ValueError('Currently FullyConnectedBoxPredictor only supports ' - 'predicting a single box per class per location.') - if len(image_features) != 1: - raise ValueError('length of `image_features` must be 1. Found {}'.format( - len(image_features))) - image_feature = image_features[0] - predictions_dict = {} - - if prediction_stage == 2: - predictions_dict[BOX_ENCODINGS] = self._box_prediction_head.predict( - features=image_feature, - num_predictions_per_location=num_predictions_per_location[0]) - predictions_dict[CLASS_PREDICTIONS_WITH_BACKGROUND] = ( - self._class_prediction_head.predict( - features=image_feature, - num_predictions_per_location=num_predictions_per_location[0])) - elif prediction_stage == 3: - for prediction_head in self.get_third_stage_prediction_heads(): - head_object = self._third_stage_heads[prediction_head] - predictions_dict[prediction_head] = head_object.predict( - features=image_feature, - num_predictions_per_location=num_predictions_per_location[0]) - else: - raise ValueError('prediction_stage should be either 2 or 3.') - - return predictions_dict diff --git a/research/object_detection/predictors/mask_rcnn_box_predictor_tf1_test.py b/research/object_detection/predictors/mask_rcnn_box_predictor_tf1_test.py deleted file mode 100644 index d9a4bcbbf00..00000000000 --- a/research/object_detection/predictors/mask_rcnn_box_predictor_tf1_test.py +++ /dev/null @@ -1,154 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.mask_rcnn_box_predictor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.predictors import mask_rcnn_box_predictor as box_predictor -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class MaskRCNNBoxPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.build(hyperparams, is_training=True) - - def test_get_boxes_with_five_classes(self): - def graph_fn(image_features): - mask_box_predictor = box_predictor_builder.build_mask_rcnn_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4, - ) - box_predictions = mask_box_predictor.predict( - [image_features], - num_predictions_per_location=[1], - scope='BoxPredictor', - prediction_stage=2) - return (box_predictions[box_predictor.BOX_ENCODINGS], - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND]) - image_features = np.random.rand(2, 7, 7, 3).astype(np.float32) - (box_encodings, - class_predictions_with_background) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [2, 1, 5, 4]) - self.assertAllEqual(class_predictions_with_background.shape, [2, 1, 6]) - - def test_get_boxes_with_five_classes_share_box_across_classes(self): - def graph_fn(image_features): - mask_box_predictor = box_predictor_builder.build_mask_rcnn_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4, - share_box_across_classes=True - ) - box_predictions = mask_box_predictor.predict( - [image_features], - num_predictions_per_location=[1], - scope='BoxPredictor', - prediction_stage=2) - return (box_predictions[box_predictor.BOX_ENCODINGS], - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND]) - image_features = np.random.rand(2, 7, 7, 3).astype(np.float32) - (box_encodings, - class_predictions_with_background) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [2, 1, 1, 4]) - self.assertAllEqual(class_predictions_with_background.shape, [2, 1, 6]) - - def test_value_error_on_predict_instance_masks_with_no_conv_hyperparms(self): - with self.assertRaises(ValueError): - box_predictor_builder.build_mask_rcnn_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4, - predict_instance_masks=True) - - def test_get_instance_masks(self): - def graph_fn(image_features): - mask_box_predictor = box_predictor_builder.build_mask_rcnn_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4, - conv_hyperparams_fn=self._build_arg_scope_with_hyperparams( - op_type=hyperparams_pb2.Hyperparams.CONV), - predict_instance_masks=True) - box_predictions = mask_box_predictor.predict( - [image_features], - num_predictions_per_location=[1], - scope='BoxPredictor', - prediction_stage=3) - return (box_predictions[box_predictor.MASK_PREDICTIONS],) - image_features = np.random.rand(2, 7, 7, 3).astype(np.float32) - mask_predictions = self.execute(graph_fn, [image_features]) - self.assertAllEqual(mask_predictions.shape, [2, 1, 5, 14, 14]) - - def test_do_not_return_instance_masks_without_request(self): - image_features = tf.random_uniform([2, 7, 7, 3], dtype=tf.float32) - mask_box_predictor = box_predictor_builder.build_mask_rcnn_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams_fn=self._build_arg_scope_with_hyperparams(), - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4) - box_predictions = mask_box_predictor.predict( - [image_features], - num_predictions_per_location=[1], - scope='BoxPredictor', - prediction_stage=2) - self.assertEqual(len(box_predictions), 2) - self.assertTrue(box_predictor.BOX_ENCODINGS in box_predictions) - self.assertTrue(box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND - in box_predictions) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/mask_rcnn_keras_box_predictor.py b/research/object_detection/predictors/mask_rcnn_keras_box_predictor.py deleted file mode 100644 index baca02edda0..00000000000 --- a/research/object_detection/predictors/mask_rcnn_keras_box_predictor.py +++ /dev/null @@ -1,139 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Mask R-CNN Box Predictor.""" -from object_detection.core import box_predictor - - -BOX_ENCODINGS = box_predictor.BOX_ENCODINGS -CLASS_PREDICTIONS_WITH_BACKGROUND = ( - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND) -MASK_PREDICTIONS = box_predictor.MASK_PREDICTIONS - - -class MaskRCNNKerasBoxPredictor(box_predictor.KerasBoxPredictor): - """Mask R-CNN Box Predictor. - - See Mask R-CNN: He, K., Gkioxari, G., Dollar, P., & Girshick, R. (2017). - Mask R-CNN. arXiv preprint arXiv:1703.06870. - - This is used for the second stage of the Mask R-CNN detector where proposals - cropped from an image are arranged along the batch dimension of the input - image_features tensor. Notice that locations are *not* shared across classes, - thus for each anchor, a separate prediction is made for each class. - - In addition to predicting boxes and classes, optionally this class allows - predicting masks and/or keypoints inside detection boxes. - - Currently this box predictor makes per-class predictions; that is, each - anchor makes a separate box prediction for each class. - """ - - def __init__(self, - is_training, - num_classes, - freeze_batchnorm, - box_prediction_head, - class_prediction_head, - third_stage_heads, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - box_prediction_head: The head that predicts the boxes in second stage. - class_prediction_head: The head that predicts the classes in second stage. - third_stage_heads: A dictionary mapping head names to mask rcnn head - classes. - name: A string name scope to assign to the model. If `None`, Keras - will auto-generate one from the class name. - """ - super(MaskRCNNKerasBoxPredictor, self).__init__( - is_training, num_classes, freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=False, name=name) - self._box_prediction_head = box_prediction_head - self._class_prediction_head = class_prediction_head - self._third_stage_heads = third_stage_heads - - @property - def num_classes(self): - return self._num_classes - - def get_second_stage_prediction_heads(self): - return BOX_ENCODINGS, CLASS_PREDICTIONS_WITH_BACKGROUND - - def get_third_stage_prediction_heads(self): - return sorted(self._third_stage_heads.keys()) - - def _predict(self, - image_features, - prediction_stage=2, - **kwargs): - """Optionally computes encoded object locations, confidences, and masks. - - Predicts the heads belonging to the given prediction stage. - - Args: - image_features: A list of float tensors of shape - [batch_size, height_i, width_i, channels_i] containing roi pooled - features for each image. The length of the list should be 1 otherwise - a ValueError will be raised. - prediction_stage: Prediction stage. Acceptable values are 2 and 3. - **kwargs: Unused Keyword args - - Returns: - A dictionary containing the predicted tensors that are listed in - self._prediction_heads. A subset of the following keys will exist in the - dictionary: - BOX_ENCODINGS: A float tensor of shape - [batch_size, 1, num_classes, code_size] representing the - location of the objects. - CLASS_PREDICTIONS_WITH_BACKGROUND: A float tensor of shape - [batch_size, 1, num_classes + 1] representing the class - predictions for the proposals. - MASK_PREDICTIONS: A float tensor of shape - [batch_size, 1, num_classes, image_height, image_width] - - Raises: - ValueError: If num_predictions_per_location is not 1 or if - len(image_features) is not 1. - ValueError: if prediction_stage is not 2 or 3. - """ - if len(image_features) != 1: - raise ValueError('length of `image_features` must be 1. Found {}'.format( - len(image_features))) - image_feature = image_features[0] - predictions_dict = {} - - if prediction_stage == 2: - predictions_dict[BOX_ENCODINGS] = self._box_prediction_head(image_feature) - predictions_dict[CLASS_PREDICTIONS_WITH_BACKGROUND] = ( - self._class_prediction_head(image_feature)) - elif prediction_stage == 3: - for prediction_head in self.get_third_stage_prediction_heads(): - head_object = self._third_stage_heads[prediction_head] - predictions_dict[prediction_head] = head_object(image_feature) - else: - raise ValueError('prediction_stage should be either 2 or 3.') - - return predictions_dict diff --git a/research/object_detection/predictors/mask_rcnn_keras_box_predictor_tf2_test.py b/research/object_detection/predictors/mask_rcnn_keras_box_predictor_tf2_test.py deleted file mode 100644 index a92db9e90fb..00000000000 --- a/research/object_detection/predictors/mask_rcnn_keras_box_predictor_tf2_test.py +++ /dev/null @@ -1,144 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.mask_rcnn_box_predictor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import box_predictor_builder -from object_detection.builders import hyperparams_builder -from object_detection.predictors import mask_rcnn_keras_box_predictor as box_predictor -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class MaskRCNNKerasBoxPredictorTest(test_case.TestCase): - - def _build_hyperparams(self, - op_type=hyperparams_pb2.Hyperparams.FC): - hyperparams = hyperparams_pb2.Hyperparams() - hyperparams_text_proto = """ - activation: NONE - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(hyperparams_text_proto, hyperparams) - hyperparams.op = op_type - return hyperparams_builder.KerasLayerHyperparams(hyperparams) - - def test_get_boxes_with_five_classes(self): - mask_box_predictor = ( - box_predictor_builder.build_mask_rcnn_keras_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams=self._build_hyperparams(), - freeze_batchnorm=False, - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4, - )) - def graph_fn(image_features): - box_predictions = mask_box_predictor( - [image_features], - prediction_stage=2) - return (box_predictions[box_predictor.BOX_ENCODINGS], - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND]) - image_features = np.random.rand(2, 7, 7, 3).astype(np.float32) - (box_encodings, - class_predictions_with_background) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [2, 1, 5, 4]) - self.assertAllEqual(class_predictions_with_background.shape, [2, 1, 6]) - - def test_get_boxes_with_five_classes_share_box_across_classes(self): - mask_box_predictor = ( - box_predictor_builder.build_mask_rcnn_keras_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams=self._build_hyperparams(), - freeze_batchnorm=False, - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4, - share_box_across_classes=True - )) - def graph_fn(image_features): - - box_predictions = mask_box_predictor( - [image_features], - prediction_stage=2) - return (box_predictions[box_predictor.BOX_ENCODINGS], - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND]) - image_features = np.random.rand(2, 7, 7, 3).astype(np.float32) - (box_encodings, - class_predictions_with_background) = self.execute(graph_fn, - [image_features]) - self.assertAllEqual(box_encodings.shape, [2, 1, 1, 4]) - self.assertAllEqual(class_predictions_with_background.shape, [2, 1, 6]) - - def test_get_instance_masks(self): - mask_box_predictor = ( - box_predictor_builder.build_mask_rcnn_keras_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams=self._build_hyperparams(), - freeze_batchnorm=False, - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4, - conv_hyperparams=self._build_hyperparams( - op_type=hyperparams_pb2.Hyperparams.CONV), - predict_instance_masks=True)) - def graph_fn(image_features): - box_predictions = mask_box_predictor( - [image_features], - prediction_stage=3) - return (box_predictions[box_predictor.MASK_PREDICTIONS],) - image_features = np.random.rand(2, 7, 7, 3).astype(np.float32) - mask_predictions = self.execute(graph_fn, [image_features]) - self.assertAllEqual(mask_predictions.shape, [2, 1, 5, 14, 14]) - - def test_do_not_return_instance_masks_without_request(self): - image_features = tf.random_uniform([2, 7, 7, 3], dtype=tf.float32) - mask_box_predictor = ( - box_predictor_builder.build_mask_rcnn_keras_box_predictor( - is_training=False, - num_classes=5, - fc_hyperparams=self._build_hyperparams(), - freeze_batchnorm=False, - use_dropout=False, - dropout_keep_prob=0.5, - box_code_size=4)) - box_predictions = mask_box_predictor( - [image_features], - prediction_stage=2) - self.assertEqual(len(box_predictions), 2) - self.assertTrue(box_predictor.BOX_ENCODINGS in box_predictions) - self.assertTrue(box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND - in box_predictions) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/rfcn_box_predictor.py b/research/object_detection/predictors/rfcn_box_predictor.py deleted file mode 100644 index c5cf7acbebd..00000000000 --- a/research/object_detection/predictors/rfcn_box_predictor.py +++ /dev/null @@ -1,159 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""RFCN Box Predictor.""" -import tensorflow.compat.v1 as tf -import tf_slim as slim -from object_detection.core import box_predictor -from object_detection.utils import ops - - -BOX_ENCODINGS = box_predictor.BOX_ENCODINGS -CLASS_PREDICTIONS_WITH_BACKGROUND = ( - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND) -MASK_PREDICTIONS = box_predictor.MASK_PREDICTIONS - - -class RfcnBoxPredictor(box_predictor.BoxPredictor): - """RFCN Box Predictor. - - Applies a position sensitive ROI pooling on position sensitive feature maps to - predict classes and refined locations. See https://arxiv.org/abs/1605.06409 - for details. - - This is used for the second stage of the RFCN meta architecture. Notice that - locations are *not* shared across classes, thus for each anchor, a separate - prediction is made for each class. - """ - - def __init__(self, - is_training, - num_classes, - conv_hyperparams_fn, - num_spatial_bins, - depth, - crop_size, - box_code_size): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - conv_hyperparams_fn: A function to construct tf-slim arg_scope with - hyperparameters for convolutional layers. - num_spatial_bins: A list of two integers `[spatial_bins_y, - spatial_bins_x]`. - depth: Target depth to reduce the input feature maps to. - crop_size: A list of two integers `[crop_height, crop_width]`. - box_code_size: Size of encoding for each box. - """ - super(RfcnBoxPredictor, self).__init__(is_training, num_classes) - self._conv_hyperparams_fn = conv_hyperparams_fn - self._num_spatial_bins = num_spatial_bins - self._depth = depth - self._crop_size = crop_size - self._box_code_size = box_code_size - - @property - def num_classes(self): - return self._num_classes - - def _predict(self, image_features, num_predictions_per_location, - proposal_boxes): - """Computes encoded object locations and corresponding confidences. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - num_predictions_per_location: A list of integers representing the number - of box predictions to be made per spatial location for each feature map. - Currently, this must be set to [1], or an error will be raised. - proposal_boxes: A float tensor of shape [batch_size, num_proposals, - box_code_size]. - - Returns: - box_encodings: A list of float tensors of shape - [batch_size, num_anchors_i, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. Each entry in the - list corresponds to a feature map in the input `image_features` list. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - - Raises: - ValueError: if num_predictions_per_location is not 1 or if - len(image_features) is not 1. - """ - if (len(num_predictions_per_location) != 1 or - num_predictions_per_location[0] != 1): - raise ValueError('Currently RfcnBoxPredictor only supports ' - 'predicting a single box per class per location.') - if len(image_features) != 1: - raise ValueError('length of `image_features` must be 1. Found {}'. - format(len(image_features))) - image_feature = image_features[0] - num_predictions_per_location = num_predictions_per_location[0] - batch_size = tf.shape(proposal_boxes)[0] - num_boxes = tf.shape(proposal_boxes)[1] - net = image_feature - with slim.arg_scope(self._conv_hyperparams_fn()): - net = slim.conv2d(net, self._depth, [1, 1], scope='reduce_depth') - # Location predictions. - location_feature_map_depth = (self._num_spatial_bins[0] * - self._num_spatial_bins[1] * - self.num_classes * - self._box_code_size) - location_feature_map = slim.conv2d(net, location_feature_map_depth, - [1, 1], activation_fn=None, - scope='refined_locations') - box_encodings = ops.batch_position_sensitive_crop_regions( - location_feature_map, - boxes=proposal_boxes, - crop_size=self._crop_size, - num_spatial_bins=self._num_spatial_bins, - global_pool=True) - box_encodings = tf.squeeze(box_encodings, axis=[2, 3]) - box_encodings = tf.reshape(box_encodings, - [batch_size * num_boxes, 1, self.num_classes, - self._box_code_size]) - - # Class predictions. - total_classes = self.num_classes + 1 # Account for background class. - class_feature_map_depth = (self._num_spatial_bins[0] * - self._num_spatial_bins[1] * - total_classes) - class_feature_map = slim.conv2d(net, class_feature_map_depth, [1, 1], - activation_fn=None, - scope='class_predictions') - class_predictions_with_background = ( - ops.batch_position_sensitive_crop_regions( - class_feature_map, - boxes=proposal_boxes, - crop_size=self._crop_size, - num_spatial_bins=self._num_spatial_bins, - global_pool=True)) - class_predictions_with_background = tf.squeeze( - class_predictions_with_background, axis=[2, 3]) - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [batch_size * num_boxes, 1, total_classes]) - - return {BOX_ENCODINGS: [box_encodings], - CLASS_PREDICTIONS_WITH_BACKGROUND: - [class_predictions_with_background]} diff --git a/research/object_detection/predictors/rfcn_box_predictor_tf1_test.py b/research/object_detection/predictors/rfcn_box_predictor_tf1_test.py deleted file mode 100644 index 555c4b2adea..00000000000 --- a/research/object_detection/predictors/rfcn_box_predictor_tf1_test.py +++ /dev/null @@ -1,80 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.rfcn_box_predictor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors import rfcn_box_predictor as box_predictor -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf2(), 'Skipping TF1.X only test.') -class RfcnBoxPredictorTest(test_case.TestCase): - - def _build_arg_scope_with_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.build(conv_hyperparams, is_training=True) - - def test_get_correct_box_encoding_and_class_prediction_shapes(self): - - def graph_fn(image_features, proposal_boxes): - rfcn_box_predictor = box_predictor.RfcnBoxPredictor( - is_training=False, - num_classes=2, - conv_hyperparams_fn=self._build_arg_scope_with_conv_hyperparams(), - num_spatial_bins=[3, 3], - depth=4, - crop_size=[12, 12], - box_code_size=4 - ) - box_predictions = rfcn_box_predictor.predict( - [image_features], num_predictions_per_location=[1], - scope='BoxPredictor', - proposal_boxes=proposal_boxes) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - proposal_boxes = np.random.rand(4, 2, 4).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features, proposal_boxes]) - - self.assertAllEqual(box_encodings.shape, [8, 1, 2, 4]) - self.assertAllEqual(class_predictions_with_background.shape, [8, 1, 3]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/predictors/rfcn_keras_box_predictor.py b/research/object_detection/predictors/rfcn_keras_box_predictor.py deleted file mode 100644 index 094e665f69c..00000000000 --- a/research/object_detection/predictors/rfcn_keras_box_predictor.py +++ /dev/null @@ -1,204 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""RFCN Box Predictor.""" -import tensorflow.compat.v1 as tf -from object_detection.core import box_predictor -from object_detection.utils import ops - -BOX_ENCODINGS = box_predictor.BOX_ENCODINGS -CLASS_PREDICTIONS_WITH_BACKGROUND = ( - box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND) -MASK_PREDICTIONS = box_predictor.MASK_PREDICTIONS - - -class RfcnKerasBoxPredictor(box_predictor.KerasBoxPredictor): - """RFCN Box Predictor. - - Applies a position sensitive ROI pooling on position sensitive feature maps to - predict classes and refined locations. See https://arxiv.org/abs/1605.06409 - for details. - - This is used for the second stage of the RFCN meta architecture. Notice that - locations are *not* shared across classes, thus for each anchor, a separate - prediction is made for each class. - """ - - def __init__(self, - is_training, - num_classes, - conv_hyperparams, - freeze_batchnorm, - num_spatial_bins, - depth, - crop_size, - box_code_size, - name=None): - """Constructor. - - Args: - is_training: Indicates whether the BoxPredictor is in training mode. - num_classes: number of classes. Note that num_classes *does not* - include the background category, so if groundtruth labels take values - in {0, 1, .., K-1}, num_classes=K (and not K+1, even though the - assigned classification targets can range from {0,... K}). - conv_hyperparams: A `hyperparams_builder.KerasLayerHyperparams` object - containing hyperparameters for convolution ops. - freeze_batchnorm: Whether to freeze batch norm parameters during - training or not. When training with a small batch size (e.g. 1), it is - desirable to freeze batch norm update and use pretrained batch norm - params. - num_spatial_bins: A list of two integers `[spatial_bins_y, - spatial_bins_x]`. - depth: Target depth to reduce the input feature maps to. - crop_size: A list of two integers `[crop_height, crop_width]`. - box_code_size: Size of encoding for each box. - name: A string name scope to assign to the box predictor. If `None`, Keras - will auto-generate one from the class name. - """ - super(RfcnKerasBoxPredictor, self).__init__( - is_training, num_classes, freeze_batchnorm=freeze_batchnorm, - inplace_batchnorm_update=False, name=name) - self._freeze_batchnorm = freeze_batchnorm - self._conv_hyperparams = conv_hyperparams - self._num_spatial_bins = num_spatial_bins - self._depth = depth - self._crop_size = crop_size - self._box_code_size = box_code_size - - # Build the shared layers used for both heads - self._shared_conv_layers = [] - self._shared_conv_layers.append( - tf.keras.layers.Conv2D( - self._depth, - [1, 1], - padding='SAME', - name='reduce_depth_conv', - **self._conv_hyperparams.params())) - self._shared_conv_layers.append( - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='reduce_depth_batchnorm')) - self._shared_conv_layers.append( - self._conv_hyperparams.build_activation_layer( - name='reduce_depth_activation')) - - self._box_encoder_layers = [] - location_feature_map_depth = (self._num_spatial_bins[0] * - self._num_spatial_bins[1] * - self.num_classes * - self._box_code_size) - self._box_encoder_layers.append( - tf.keras.layers.Conv2D( - location_feature_map_depth, - [1, 1], - padding='SAME', - name='refined_locations_conv', - **self._conv_hyperparams.params())) - self._box_encoder_layers.append( - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='refined_locations_batchnorm')) - - self._class_predictor_layers = [] - self._total_classes = self.num_classes + 1 # Account for background class. - class_feature_map_depth = (self._num_spatial_bins[0] * - self._num_spatial_bins[1] * - self._total_classes) - self._class_predictor_layers.append( - tf.keras.layers.Conv2D( - class_feature_map_depth, - [1, 1], - padding='SAME', - name='class_predictions_conv', - **self._conv_hyperparams.params())) - self._class_predictor_layers.append( - self._conv_hyperparams.build_batch_norm( - training=(self._is_training and not self._freeze_batchnorm), - name='class_predictions_batchnorm')) - - @property - def num_classes(self): - return self._num_classes - - def _predict(self, image_features, proposal_boxes, **kwargs): - """Computes encoded object locations and corresponding confidences. - - Args: - image_features: A list of float tensors of shape [batch_size, height_i, - width_i, channels_i] containing features for a batch of images. - proposal_boxes: A float tensor of shape [batch_size, num_proposals, - box_code_size]. - **kwargs: Unused Keyword args - - Returns: - box_encodings: A list of float tensors of shape - [batch_size, num_anchors_i, q, code_size] representing the location of - the objects, where q is 1 or the number of classes. Each entry in the - list corresponds to a feature map in the input `image_features` list. - class_predictions_with_background: A list of float tensors of shape - [batch_size, num_anchors_i, num_classes + 1] representing the class - predictions for the proposals. Each entry in the list corresponds to a - feature map in the input `image_features` list. - - Raises: - ValueError: if num_predictions_per_location is not 1 or if - len(image_features) is not 1. - """ - if len(image_features) != 1: - raise ValueError('length of `image_features` must be 1. Found {}'. - format(len(image_features))) - image_feature = image_features[0] - batch_size = tf.shape(proposal_boxes)[0] - num_boxes = tf.shape(proposal_boxes)[1] - net = image_feature - for layer in self._shared_conv_layers: - net = layer(net) - - # Location predictions. - box_net = net - for layer in self._box_encoder_layers: - box_net = layer(box_net) - box_encodings = ops.batch_position_sensitive_crop_regions( - box_net, - boxes=proposal_boxes, - crop_size=self._crop_size, - num_spatial_bins=self._num_spatial_bins, - global_pool=True) - box_encodings = tf.squeeze(box_encodings, axis=[2, 3]) - box_encodings = tf.reshape(box_encodings, - [batch_size * num_boxes, 1, self.num_classes, - self._box_code_size]) - - # Class predictions. - class_net = net - for layer in self._class_predictor_layers: - class_net = layer(class_net) - class_predictions_with_background = ( - ops.batch_position_sensitive_crop_regions( - class_net, - boxes=proposal_boxes, - crop_size=self._crop_size, - num_spatial_bins=self._num_spatial_bins, - global_pool=True)) - class_predictions_with_background = tf.squeeze( - class_predictions_with_background, axis=[2, 3]) - class_predictions_with_background = tf.reshape( - class_predictions_with_background, - [batch_size * num_boxes, 1, self._total_classes]) - - return {BOX_ENCODINGS: [box_encodings], - CLASS_PREDICTIONS_WITH_BACKGROUND: - [class_predictions_with_background]} diff --git a/research/object_detection/predictors/rfcn_keras_box_predictor_tf2_test.py b/research/object_detection/predictors/rfcn_keras_box_predictor_tf2_test.py deleted file mode 100644 index f845068e35b..00000000000 --- a/research/object_detection/predictors/rfcn_keras_box_predictor_tf2_test.py +++ /dev/null @@ -1,79 +0,0 @@ -# Copyright 2017 The TensorFlow Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================== - -"""Tests for object_detection.predictors.rfcn_box_predictor.""" -import unittest -import numpy as np -import tensorflow.compat.v1 as tf - -from google.protobuf import text_format -from object_detection.builders import hyperparams_builder -from object_detection.predictors import rfcn_keras_box_predictor as box_predictor -from object_detection.protos import hyperparams_pb2 -from object_detection.utils import test_case -from object_detection.utils import tf_version - - -@unittest.skipIf(tf_version.is_tf1(), 'Skipping TF2.X only test.') -class RfcnKerasBoxPredictorTest(test_case.TestCase): - - def _build_conv_hyperparams(self): - conv_hyperparams = hyperparams_pb2.Hyperparams() - conv_hyperparams_text_proto = """ - regularizer { - l2_regularizer { - } - } - initializer { - truncated_normal_initializer { - } - } - """ - text_format.Merge(conv_hyperparams_text_proto, conv_hyperparams) - return hyperparams_builder.KerasLayerHyperparams(conv_hyperparams) - - def test_get_correct_box_encoding_and_class_prediction_shapes(self): - rfcn_box_predictor = box_predictor.RfcnKerasBoxPredictor( - is_training=False, - num_classes=2, - conv_hyperparams=self._build_conv_hyperparams(), - freeze_batchnorm=False, - num_spatial_bins=[3, 3], - depth=4, - crop_size=[12, 12], - box_code_size=4) - def graph_fn(image_features, proposal_boxes): - - box_predictions = rfcn_box_predictor( - [image_features], - proposal_boxes=proposal_boxes) - box_encodings = tf.concat( - box_predictions[box_predictor.BOX_ENCODINGS], axis=1) - class_predictions_with_background = tf.concat( - box_predictions[box_predictor.CLASS_PREDICTIONS_WITH_BACKGROUND], - axis=1) - return (box_encodings, class_predictions_with_background) - - image_features = np.random.rand(4, 8, 8, 64).astype(np.float32) - proposal_boxes = np.random.rand(4, 2, 4).astype(np.float32) - (box_encodings, class_predictions_with_background) = self.execute( - graph_fn, [image_features, proposal_boxes]) - - self.assertAllEqual(box_encodings.shape, [8, 1, 2, 4]) - self.assertAllEqual(class_predictions_with_background.shape, [8, 1, 3]) - - -if __name__ == '__main__': - tf.test.main() diff --git a/research/object_detection/protos/__init__.py b/research/object_detection/protos/__init__.py deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/research/object_detection/protos/anchor_generator.proto b/research/object_detection/protos/anchor_generator.proto deleted file mode 100644 index 9608ca48908..00000000000 --- a/research/object_detection/protos/anchor_generator.proto +++ /dev/null @@ -1,19 +0,0 @@ -syntax = "proto2"; - -package object_detection.protos; - -import "object_detection/protos/flexible_grid_anchor_generator.proto"; -import "object_detection/protos/grid_anchor_generator.proto"; -import "object_detection/protos/multiscale_anchor_generator.proto"; -import "object_detection/protos/ssd_anchor_generator.proto"; - -// Configuration proto for the anchor generator to use in the object detection -// pipeline. See core/anchor_generator.py for details. -message AnchorGenerator { - oneof anchor_generator_oneof { - GridAnchorGenerator grid_anchor_generator = 1; - SsdAnchorGenerator ssd_anchor_generator = 2; - MultiscaleAnchorGenerator multiscale_anchor_generator = 3; - FlexibleGridAnchorGenerator flexible_grid_anchor_generator = 4; - } -} diff --git a/research/object_detection/protos/argmax_matcher.proto b/research/object_detection/protos/argmax_matcher.proto deleted file mode 100644 index 947fcb983dc..00000000000 --- a/research/object_detection/protos/argmax_matcher.proto +++ /dev/null @@ -1,29 +0,0 @@ -syntax = "proto2"; - -package object_detection.protos; - -// Configuration proto for ArgMaxMatcher. See -// matchers/argmax_matcher.py for details. -message ArgMaxMatcher { - // Threshold for positive matches. - optional float matched_threshold = 1 [default = 0.5]; - - // Threshold for negative matches. - optional float unmatched_threshold = 2 [default = 0.5]; - - // Whether to construct ArgMaxMatcher without thresholds. - optional bool ignore_thresholds = 3 [default = false]; - - // If True then negative matches are the ones below the unmatched_threshold, - // whereas ignored matches are in between the matched and umatched - // threshold. If False, then negative matches are in between the matched - // and unmatched threshold, and everything lower than unmatched is ignored. - optional bool negatives_lower_than_unmatched = 4 [default = true]; - - // Whether to ensure each row is matched to at least one column. - optional bool force_match_for_each_row = 5 [default = false]; - - // Force constructed match objects to use matrix multiplication based gather - // instead of standard tf.gather - optional bool use_matmul_gather = 6 [default = false]; -} diff --git a/research/object_detection/protos/bipartite_matcher.proto b/research/object_detection/protos/bipartite_matcher.proto deleted file mode 100644 index 175ecdd1096..00000000000 --- a/research/object_detection/protos/bipartite_matcher.proto +++ /dev/null @@ -1,11 +0,0 @@ -syntax = "proto2"; - -package object_detection.protos; - -// Configuration proto for bipartite matcher. See -// matchers/bipartite_matcher.py for details. -message BipartiteMatcher { - // Force constructed match objects to use matrix multiplication based gather - // instead of standard tf.gather - optional bool use_matmul_gather = 6 [default = false]; -} diff --git a/research/object_detection/protos/box_coder.proto b/research/object_detection/protos/box_coder.proto deleted file mode 100644 index 79b818125a3..00000000000 --- a/research/object_detection/protos/box_coder.proto +++ /dev/null @@ -1,19 +0,0 @@ -syntax = "proto2"; - -package object_detection.protos; - -import "object_detection/protos/faster_rcnn_box_coder.proto"; -import "object_detection/protos/keypoint_box_coder.proto"; -import "object_detection/protos/mean_stddev_box_coder.proto"; -import "object_detection/protos/square_box_coder.proto"; - -// Configuration proto for the box coder to be used in the object detection -// pipeline. See core/box_coder.py for details. -message BoxCoder { - oneof box_coder_oneof { - FasterRcnnBoxCoder faster_rcnn_box_coder = 1; - MeanStddevBoxCoder mean_stddev_box_coder = 2; - SquareBoxCoder square_box_coder = 3; - KeypointBoxCoder keypoint_box_coder = 4; - } -} diff --git a/research/object_detection/protos/box_predictor.proto b/research/object_detection/protos/box_predictor.proto deleted file mode 100644 index c4926502a4f..00000000000 --- a/research/object_detection/protos/box_predictor.proto +++ /dev/null @@ -1,212 +0,0 @@ -syntax = "proto2"; - -package object_detection.protos; - -import "object_detection/protos/hyperparams.proto"; - -// Configuration proto for box predictor. See core/box_predictor.py for details. -message BoxPredictor { - oneof box_predictor_oneof { - ConvolutionalBoxPredictor convolutional_box_predictor = 1; - MaskRCNNBoxPredictor mask_rcnn_box_predictor = 2; - RfcnBoxPredictor rfcn_box_predictor = 3; - WeightSharedConvolutionalBoxPredictor - weight_shared_convolutional_box_predictor = 4; - } -} - -// Configuration proto for Convolutional box predictor. -// Next id: 13 -message ConvolutionalBoxPredictor { - // Hyperparameters for convolution ops used in the box predictor. - optional Hyperparams conv_hyperparams = 1; - - // Minimum feature depth prior to predicting box encodings and class - // predictions. - optional int32 min_depth = 2 [default = 0]; - - // Maximum feature depth prior to predicting box encodings and class - // predictions. If max_depth is set to 0, no additional feature map will be - // inserted before location and class predictions. - optional int32 max_depth = 3 [default = 0]; - - // Number of the additional conv layers before the predictor. - optional int32 num_layers_before_predictor = 4 [default = 0]; - - // Whether to use dropout for class prediction. - optional bool use_dropout = 5 [default = true]; - - // Keep probability for dropout - optional float dropout_keep_probability = 6 [default = 0.8]; - - // Size of final convolution kernel. If the spatial resolution of the feature - // map is smaller than the kernel size, then the kernel size is set to - // min(feature_width, feature_height). - optional int32 kernel_size = 7 [default = 1]; - - // Size of the encoding for boxes. - optional int32 box_code_size = 8 [default = 4]; - - // Whether to apply sigmoid to the output of class predictions. - // TODO(jonathanhuang): Do we need this since we have a post processing - // module.? - optional bool apply_sigmoid_to_scores = 9 [default = false]; - - optional float class_prediction_bias_init = 10 [default = 0.0]; - - // Whether to use depthwise separable convolution for box predictor layers. - optional bool use_depthwise = 11 [default = false]; - - // If specified, apply clipping to box encodings. - message BoxEncodingsClipRange { - optional float min = 1; - optional float max = 2; - } - optional BoxEncodingsClipRange box_encodings_clip_range = 12; -} - -// Configuration proto for weight shared convolutional box predictor. -// Next id: 21 -message WeightSharedConvolutionalBoxPredictor { - // Hyperparameters for convolution ops used in the box predictor. - optional Hyperparams conv_hyperparams = 1; - - // Whether the `conv_hyperparams` should apply to depthwise separable - // convolution layers in the box and class heads, in addition to the layers in - // the predictor tower. By default, the `conv_hyperparams` are only applied to - // layers in the predictor tower when use_depthwise is true. - optional bool apply_conv_hyperparams_to_heads = 19 [default = false]; - - // Whether the `conv_hyperparams` should apply to the `pointwise_initializer` - // and `pointwise_regularizer` when using depthwise separable convolutions in - // the prediction tower layers. By default, the `conv_hyperparams` only apply - // to the `depthwise_initializer` and `depthwise_regularizer`. - optional bool apply_conv_hyperparams_pointwise = 20 [default = false]; - - // Number of the additional conv layers before the predictor. - optional int32 num_layers_before_predictor = 4 [default = 0]; - - // Output depth for the convolution ops prior to predicting box encodings - // and class predictions. - optional int32 depth = 2 [default = 0]; - - // Size of final convolution kernel. If the spatial resolution of the feature - // map is smaller than the kernel size, then the kernel size is set to - // min(feature_width, feature_height). - optional int32 kernel_size = 7 [default = 3]; - - // Size of the encoding for boxes. - optional int32 box_code_size = 8 [default = 4]; - - // Bias initialization for class prediction. It has been show to stabilize - // training where there are large number of negative boxes. See - // https://arxiv.org/abs/1708.02002 for details. - optional float class_prediction_bias_init = 10 [default = 0.0]; - - // Whether to use dropout for class prediction. - optional bool use_dropout = 11 [default = false]; - - // Keep probability for dropout. - optional float dropout_keep_probability = 12 [default = 0.8]; - - // Whether to share the multi-layer tower between box prediction and class - // prediction heads. - optional bool share_prediction_tower = 13 [default = false]; - - // Whether to use depthwise separable convolution for box predictor layers. - optional bool use_depthwise = 14 [default = false]; - - // Enum to specify how to convert the detection scores at inference time. - enum ScoreConverter { - // Input scores equals output scores. - IDENTITY = 0; - - // Applies a sigmoid on input scores. - SIGMOID = 1; - } - - // Callable elementwise score converter at inference time. - optional ScoreConverter score_converter = 16 [default = IDENTITY]; - - // If specified, apply clipping to box encodings. - message BoxEncodingsClipRange { - optional float min = 1; - optional float max = 2; - } - optional BoxEncodingsClipRange box_encodings_clip_range = 17; - -} - - -// TODO(alirezafathi): Refactor the proto file to be able to configure mask rcnn -// head easily. -// Next id: 15 -message MaskRCNNBoxPredictor { - // Hyperparameters for fully connected ops used in the box predictor. - optional Hyperparams fc_hyperparams = 1; - - // Whether to use dropout op prior to the both box and class predictions. - optional bool use_dropout = 2 [default = false]; - - // Keep probability for dropout. This is only used if use_dropout is true. - optional float dropout_keep_probability = 3 [default = 0.5]; - - // Size of the encoding for the boxes. - optional int32 box_code_size = 4 [default = 4]; - - // Hyperparameters for convolution ops used in the box predictor. - optional Hyperparams conv_hyperparams = 5; - - // Whether to predict instance masks inside detection boxes. - optional bool predict_instance_masks = 6 [default = false]; - - // The depth for the first conv2d_transpose op applied to the - // image_features in the mask prediction branch. If set to 0, the value - // will be set automatically based on the number of channels in the image - // features and the number of classes. - optional int32 mask_prediction_conv_depth = 7 [default = 256]; - - // Whether to predict keypoints inside detection boxes. - optional bool predict_keypoints = 8 [default = false]; - - // The height and the width of the predicted mask. - optional int32 mask_height = 9 [default = 15]; - optional int32 mask_width = 10 [default = 15]; - - // The number of convolutions applied to image_features in the mask prediction - // branch. - optional int32 mask_prediction_num_conv_layers = 11 [default = 2]; - optional bool masks_are_class_agnostic = 12 [default = false]; - - // Whether to use one box for all classes rather than a different box for each - // class. - optional bool share_box_across_classes = 13 [default = false]; - - // Whether to apply convolutions on mask features before upsampling using - // nearest neighbor resizing. - // By default, mask features are resized to [`mask_height`, `mask_width`] - // before applying convolutions and predicting masks. - optional bool convolve_then_upsample_masks = 14 [default = false]; -} - -message RfcnBoxPredictor { - // Hyperparameters for convolution ops used in the box predictor. - optional Hyperparams conv_hyperparams = 1; - - // Bin sizes for RFCN crops. - optional int32 num_spatial_bins_height = 2 [default = 3]; - - optional int32 num_spatial_bins_width = 3 [default = 3]; - - // Target depth to reduce the input image features to. - optional int32 depth = 4 [default = 1024]; - - // Size of the encoding for the boxes. - optional int32 box_code_size = 5 [default = 4]; - - // Size to resize the rfcn crops to. - optional int32 crop_height = 6 [default = 12]; - - optional int32 crop_width = 7 [default = 12]; -} - diff --git a/research/object_detection/protos/calibration.proto b/research/object_detection/protos/calibration.proto deleted file mode 100644 index 6025117013f..00000000000 --- a/research/object_detection/protos/calibration.proto +++ /dev/null @@ -1,90 +0,0 @@ -// These protos contain the calibration parameters necessary for transforming -// a model's original detection scores or logits. The parameters result from -// fitting a calibration function on the model's outputs. - -syntax = "proto2"; - -package object_detection.protos; - -// Message wrapper for various calibration configurations. -message CalibrationConfig { - oneof calibrator { - // Class-agnostic calibration via linear interpolation (usually output from - // isotonic regression). - FunctionApproximation function_approximation = 1; - - // Per-class calibration via linear interpolation. - ClassIdFunctionApproximations class_id_function_approximations = 2; - - // Class-agnostic sigmoid calibration. - SigmoidCalibration sigmoid_calibration = 3; - - // Per-class sigmoid calibration. - ClassIdSigmoidCalibrations class_id_sigmoid_calibrations = 4; - - // Temperature scaling calibration. - TemperatureScalingCalibration temperature_scaling_calibration = 5; - } -} - -// Message for class-agnostic domain/range mapping for function -// approximations. -message FunctionApproximation { - // Message mapping class labels to indices - optional XYPairs x_y_pairs = 1; -} - -// Message for class-specific domain/range mapping for function -// approximations. -message ClassIdFunctionApproximations { - // Message mapping class ids to indices. - map class_id_xy_pairs_map = 1; -} - -// Message for class-agnostic Sigmoid Calibration. -message SigmoidCalibration { - // Message mapping class index to Sigmoid Parameters - optional SigmoidParameters sigmoid_parameters = 1; -} - -// Message for class-specific Sigmoid Calibration. -message ClassIdSigmoidCalibrations { - // Message mapping class index to Sigmoid Parameters. - map class_id_sigmoid_parameters_map = 1; -} - -// Message for Temperature Scaling Calibration. -message TemperatureScalingCalibration { - optional float scaler = 1; -} - -// Description of data used to fit the calibration model. CLASS_SPECIFIC -// indicates that the calibration parameters are derived from detections -// pertaining to a single class. ALL_CLASSES indicates that parameters were -// obtained by fitting a model on detections from all classes (including the -// background class). -enum TrainingDataType { - DATA_TYPE_UNKNOWN = 0; - ALL_CLASSES = 1; - CLASS_SPECIFIC = 2; -} - -// Message to store a domain/range pair for function to be approximated. -message XYPairs { - message XYPair { - optional float x = 1; - optional float y = 2; - } - - // Sequence of x/y pairs for function approximation. - repeated XYPair x_y_pair = 1; - - // Description of data used to fit the calibration model. - optional TrainingDataType training_data_type = 2; -} - -// Message defining parameters for sigmoid calibration. -message SigmoidParameters { - optional float a = 1 [default = -1.0]; - optional float b = 2 [default = 0.0]; -} diff --git a/research/object_detection/protos/center_net.proto b/research/object_detection/protos/center_net.proto deleted file mode 100644 index c9e56ce1ee8..00000000000 --- a/research/object_detection/protos/center_net.proto +++ /dev/null @@ -1,621 +0,0 @@ -syntax = "proto2"; - -package object_detection.protos; - -import "object_detection/protos/image_resizer.proto"; -import "object_detection/protos/losses.proto"; -import "object_detection/protos/post_processing.proto"; -import "object_detection/protos/preprocessor.proto"; - -// Configuration for the CenterNet meta architecture from the "Objects as -// Points" paper [1] -// [1]: https://arxiv.org/abs/1904.07850 - -// Next Id = 26 -message CenterNet { - // Number of classes to predict. - optional int32 num_classes = 1; - - // Feature extractor config. - optional CenterNetFeatureExtractor feature_extractor = 2; - - // Image resizer for preprocessing the input image. - optional ImageResizer image_resizer = 3; - - // If set, all task heads will be constructed with separable convolutions. - optional bool use_depthwise = 13 [default = false]; - - // Indicates whether or not to use the sparse version of the Op that computes - // the center heatmaps. The sparse version scales better with number of - // channels in the heatmap, but in some cases is known to cause an OOM error. - // TODO(b/170989061) When bug is fixed, make this the default behavior. - optional bool compute_heatmap_sparse = 15 [default = false]; - - // Parameters to determine the model architecture/layers of the prediction - // heads. - message PredictionHeadParams { - // The two fields: num_filters, kernel_sizes correspond to the parameters of - // the convolutional layers used by the prediction head. If provided, the - // length of the two repeated fields need to be the same and represents the - // number of convolutional layers. - - // Corresponds to the "filters" argument in tf.keras.layers.Conv2D. If not - // provided, the default value [256] will be used. - repeated int32 num_filters = 1; - - // Corresponds to the "kernel_size" argument in tf.keras.layers.Conv2D. If - // not provided, the default value [3] will be used. - repeated int32 kernel_sizes = 2; - } - - // Parameters which are related to object detection task. - message ObjectDetection { - // The original fields are moved to ObjectCenterParams or deleted. - reserved 2, 5, 6, 7; - - // Weight of the task loss. The total loss of the model will be the - // summation of task losses weighted by the weights. - optional float task_loss_weight = 1 [default = 1.0]; - - // Weight for the offset localization loss. - optional float offset_loss_weight = 3 [default = 1.0]; - - // Weight for the height/width localization loss. - optional float scale_loss_weight = 4 [default = 0.1]; - - // Localization loss configuration for object scale and offset losses. - optional LocalizationLoss localization_loss = 8; - - // Parameters to determine the architecture of the object scale prediction - // head. - optional PredictionHeadParams scale_head_params = 9; - - // Parameters to determine the architecture of the object offset prediction - // head. - optional PredictionHeadParams offset_head_params = 10; - } - optional ObjectDetection object_detection_task = 4; - - // Parameters related to object center prediction. This is required for both - // object detection and keypoint estimation tasks. - message ObjectCenterParams { - // Weight for the object center loss. - optional float object_center_loss_weight = 1 [default = 1.0]; - - // Classification loss configuration for object center loss. - optional ClassificationLoss classification_loss = 2; - - // The initial bias value of the convlution kernel of the class heatmap - // prediction head. -2.19 corresponds to predicting foreground with - // a probability of 0.1. See "Focal Loss for Dense Object Detection" - // at https://arxiv.org/abs/1708.02002. - optional float heatmap_bias_init = 3 [default = -2.19]; - - // The minimum IOU overlap boxes need to have to not be penalized. - optional float min_box_overlap_iou = 4 [default = 0.7]; - - // Maximum number of boxes to predict. - optional int32 max_box_predictions = 5 [default = 100]; - - // If set, loss is only computed for the labeled classes. - optional bool use_labeled_classes = 6 [default = false]; - - // The keypoint weights used for calculating the location of object center. - // When the field is provided, the number of weights need to be the same as - // the number of keypoints. The object center is calculated by the weighted - // mean of the keypoint locations. When the field is not provided, the - // object center is determined by the bounding box groundtruth annotations - // (default behavior). - repeated float keypoint_weights_for_center = 7; - - // Parameters to determine the architecture of the object center prediction - // head. - optional PredictionHeadParams center_head_params = 8; - - // Max pool kernel size to use to pull off peak score locations in a - // neighborhood for the object detection heatmap. - optional int32 peak_max_pool_kernel_size = 9 [default = 3]; - } - optional ObjectCenterParams object_center_params = 5; - - // Path of the file that conatins the label map along with the keypoint - // information, including the keypoint indices, corresponding labels, and the - // corresponding class. The file should be the same one as used in the input - // pipeline. Note that a plain text of StringIntLabelMap proto is expected in - // this file. - // It is required only if the keypoint estimation task is specified. - optional string keypoint_label_map_path = 6; - - // Parameters which are related to keypoint estimation task. - message KeypointEstimation { - // Name of the task, e.g. "human pose". Note that the task name should be - // unique to each keypoint task. - optional string task_name = 1; - - // Weight of the task loss. The total loss of the model will be their - // summation of task losses weighted by the weights. - optional float task_loss_weight = 2 [default = 1.0]; - - // Loss configuration for keypoint heatmap, offset, regression losses. Note - // that the localization loss is used for offset/regression losses and - // classification loss is used for heatmap loss. - optional Loss loss = 3; - - // The name of the class that contains the keypoints for this task. This is - // used to retrieve the corresponding keypoint indices from the label map. - // Note that this corresponds to the "name" field, not "display_name". - optional string keypoint_class_name = 4; - - // The standard deviation of the Gaussian kernel used to generate the - // keypoint heatmap. The unit is the pixel in the output image. It is to - // provide the flexibility of using different sizes of Gaussian kernel for - // each keypoint class. Note that if provided, the keypoint standard - // deviations will be overridden by the specified values here, otherwise, - // the default value 5.0 will be used. - // TODO(yuhuic): Update the default value once we found the best value. - map keypoint_label_to_std = 5; - - // Loss weights corresponding to different heads. - optional float keypoint_regression_loss_weight = 6 [default = 1.0]; - optional float keypoint_heatmap_loss_weight = 7 [default = 1.0]; - optional float keypoint_offset_loss_weight = 8 [default = 1.0]; - - // The initial bias value of the convolution kernel of the keypoint heatmap - // prediction head. -2.19 corresponds to predicting foreground with - // a probability of 0.1. See "Focal Loss for Dense Object Detection" - // at https://arxiv.org/abs/1708.02002. - optional float heatmap_bias_init = 9 [default = -2.19]; - - // The heatmap score threshold for a keypoint to become a valid candidate. - optional float keypoint_candidate_score_threshold = 10 [default = 0.1]; - - // The maximum number of candidates to retrieve for each keypoint. - optional int32 num_candidates_per_keypoint = 11 [default = 100]; - - // Max pool kernel size to use to pull off peak score locations in a - // neighborhood (independently for each keypoint types). - optional int32 peak_max_pool_kernel_size = 12 [default = 3]; - - // The default score to use for regressed keypoints that are not - // successfully snapped to a nearby candidate. - optional float unmatched_keypoint_score = 13 [default = 0.1]; - - // The multiplier to expand the bounding boxes (either the provided boxes or - // those which tightly cover the regressed keypoints). Note that new - // expanded box for an instance becomes the feasible search window for all - // associated keypoints. - optional float box_scale = 14 [default = 1.2]; - - // The scale parameter that multiplies the largest dimension of a bounding - // box. The resulting distance becomes a search radius for candidates in the - // vicinity of each regressed keypoint. - optional float candidate_search_scale = 15 [default = 0.3]; - - // One of ['min_distance', 'score_distance_ratio', - // 'score_scaled_distance_ratio', 'gaussian_weighted'] indicating how to - // select the keypoint candidate. - optional string candidate_ranking_mode = 16 [default = "min_distance"]; - - // The score distance ratio offset, only used if candidate_ranking_mode is - // 'score_distance_ratio'. The offset is used in the maximization of score - // distance ratio, defined as: - // keypoint_score / (distance + score_distance_offset) - optional float score_distance_offset = 22 [default = 1.0]; - - // A scalar used to multiply the bounding box size to be used as the offset - // in the score-to-distance-ratio formula. Only applicable when the - // candidate_ranking_mode is score_scaled_distance_ratio. - // The keypoint candidates are ranked using the formula: - // ranking_score = score / (distance + offset) - // where 'score' is the keypoint heatmap scores, 'distance' is the distance - // between the heatmap peak location and the regressed joint location, - // 'offset' is a function of the predicted bounding box: - // offset = max(bbox height, bbox width) * score_distance_multiplier - optional float score_distance_multiplier = 28 [default = 0.1]; - - // A scalar used to multiply the Gaussian standard deviation to control the - // Gaussian kernel which is used to weight the candidates. Only applicable - // when the candidate_ranking_mode is gaussian_weighted. - // The keypoint candidates are ranked using the formula: - // scores * exp((-distances^2) / (2 * sigma^2)) - // where 'distances' is the distance between the heatmap peak location and - // the regressed joint location and 'sigma' is the Gaussian standard - // deviation used in generating the Gaussian heatmap target multiplied by - // the 'std_dev_multiplier'. - optional float std_dev_multiplier = 29 [default = 1.0]; - - // The radius (in the unit of output pixel) around heatmap peak to assign - // the offset targets. If set 0, then the offset target will only be - // assigned to the heatmap peak (same behavior as the original paper). - optional int32 offset_peak_radius = 17 [default = 0]; - - // Indicates whether to assign offsets for each keypoint channel - // separately. If set False, the output offset target has the shape - // [batch_size, out_height, out_width, 2] (same behavior as the original - // paper). If set True, the output offset target has the shape [batch_size, - // out_height, out_width, 2 * num_keypoints] (recommended when the - // offset_peak_radius is not zero). - optional bool per_keypoint_offset = 18 [default = false]; - - // Indicates whether to predict the depth of each keypoints. Note that this - // is only supported in the single class keypoint task. - optional bool predict_depth = 19 [default = false]; - - // Indicates whether to predict depths for each keypoint channel - // separately. If set False, the output depth target has the shape - // [batch_size, out_height, out_width, 1]. If set True, the output depth - // target has the shape [batch_size, out_height, out_width, - // num_keypoints]. Recommend to set this value and "per_keypoint_offset" to - // both be True at the same time. - optional bool per_keypoint_depth = 20 [default = false]; - - // The weight of the keypoint depth loss. - optional float keypoint_depth_loss_weight = 21 [default = 1.0]; - - // Whether keypoints outside the image frame should be clipped back to the - // image boundary. If true, the keypoints that are clipped have scores set - // to 0.0. - optional bool clip_out_of_frame_keypoints = 23 [default = false]; - - // Whether instances should be rescored based on keypoint confidences. If - // False, will use the detection score (from the object center heatmap). If - // True, will compute new scores with: - // new_score = o * (1/k) sum {s_i} - // where o is the object score, s_i is the score for keypoint i, and k is - // the number of keypoints for that class. - optional bool rescore_instances = 24 [default = false]; - - // A scalar used when "rescore_instances" is set to True. The detection - // score of an instance is set to be the average score among those keypoints - // with scores higher than the threshold. - optional float rescoring_threshold = 30 [default = 0.0]; - - // The ratio used to multiply the output feature map size to determine the - // denominator in the Gaussian formula. Only applicable when the - // candidate_ranking_mode is set to be 'gaussian_weighted_const'. - optional float gaussian_denom_ratio = 31 [default = 0.1]; - - // Whether to use the keypoint postprocessing logic that replaces topk op - // with argmax. Usually used when exporting the model for predicting - // keypoints of multiple instances in the browser. - optional bool argmax_postprocessing = 32 [default = false]; - - // Parameters to determine the architecture of the keypoint heatmap - // prediction head. - optional PredictionHeadParams heatmap_head_params = 25; - - // Parameters to determine the architecture of the keypoint offset - // prediction head. - optional PredictionHeadParams offset_head_params = 26; - - // Parameters to determine the architecture of the keypoint regression - // prediction head. - optional PredictionHeadParams regress_head_params = 27; - } - repeated KeypointEstimation keypoint_estimation_task = 7; - - // Parameters which are related to mask estimation task. - // Note: Currently, CenterNet supports a weak instance segmentation, where - // semantic segmentation masks are estimated, and then cropped based on - // bounding box detections. Therefore, it is possible for the same image - // pixel to be assigned to multiple instances. - message MaskEstimation { - // Weight of the task loss. The total loss of the model will be their - // summation of task losses weighted by the weights. - optional float task_loss_weight = 1 [default = 1.0]; - - // Classification loss configuration for segmentation loss. - optional ClassificationLoss classification_loss = 2; - - // Each instance mask (one per detection) is cropped and resized (bilinear - // resampling) from the predicted segmentation feature map. After - // resampling, the masks are binarized with the provided score threshold. - optional int32 mask_height = 4 [default = 256]; - optional int32 mask_width = 5 [default = 256]; - optional float score_threshold = 6 [default = 0.5]; - - // The initial bias value of the convlution kernel of the class heatmap - // prediction head. -2.19 corresponds to predicting foreground with - // a probability of 0.1. - optional float heatmap_bias_init = 3 [default = -2.19]; - - // Parameters to determine the architecture of the segmentation mask - // prediction head. - optional PredictionHeadParams mask_head_params = 7; - } - optional MaskEstimation mask_estimation_task = 8; - - // Parameters which are related to DensePose estimation task. - // http://densepose.org/ - message DensePoseEstimation { - // Weight of the task loss. The total loss of the model will be their - // summation of task losses weighted by the weights. - optional float task_loss_weight = 1 [default = 1.0]; - - // Class ID (0-indexed) that corresponds to the object in the label map that - // contains DensePose data. - optional int32 class_id = 2; - - // Loss configuration for DensePose heatmap and regression losses. Note - // that the localization loss is used for surface coordinate losses and - // classification loss is used for part classification losses. - optional Loss loss = 3; - - // The number of body parts. - optional int32 num_parts = 4 [default = 24]; - - // Loss weights for the two DensePose heads. - optional float part_loss_weight = 5 [default = 1.0]; - optional float coordinate_loss_weight = 6 [default = 1.0]; - - // Whether to upsample the prediction feature maps back to the original - // input dimension prior to applying loss. This has the benefit of - // maintaining finer groundtruth location information. - optional bool upsample_to_input_res = 7 [default = true]; - - // The initial bias value of the convlution kernel of the class heatmap - // prediction head. -2.19 corresponds to predicting foreground with - // a probability of 0.1. - optional float heatmap_bias_init = 8 [default = -2.19]; - } - optional DensePoseEstimation densepose_estimation_task = 9; - - // Parameters which are related to tracking embedding estimation task. - // A Simple Baseline for Multi-Object Tracking [2] - // [2]: https://arxiv.org/abs/2004.01888 - message TrackEstimation { - // Weight of the task loss. The total loss of the model will be the - // summation of task losses weighted by the weights. - optional float task_loss_weight = 1 [default = 1.0]; - - // The maximun track ID of the datset. - optional int32 num_track_ids = 2; - - // The embedding size for re-identification (ReID) task in tracking. - optional int32 reid_embed_size = 3 [default = 128]; - - // The number of (fully-connected, batch-norm, relu) layers for track ID - // classification head. The output dimension of each intermediate FC layer - // will all be 'reid_embed_size'. The last FC layer will directly project to - // the track ID classification space of size 'num_track_ids' without - // batch-norm and relu layers. - optional int32 num_fc_layers = 4 [default = 1]; - - // Classification loss configuration for ReID loss. - optional ClassificationLoss classification_loss = 5; - } - optional TrackEstimation track_estimation_task = 10; - - // Temporal offset prediction head similar to CenterTrack. - // Currently our implementation adopts LSTM, different from original paper. - // See go/lstd-centernet for more details. - // Tracking Objects as Points [3] - // [3]: https://arxiv.org/abs/2004.01177 - message TemporalOffsetEstimation { - // Weight of the task loss. The total loss of the model will be the - // summation of task losses weighted by the weights. - optional float task_loss_weight = 1 [default = 1.0]; - - // Localization loss configuration for offset loss. - optional LocalizationLoss localization_loss = 2; - } - optional TemporalOffsetEstimation temporal_offset_task = 12; - - - // Mask prediction support using DeepMAC. See https://arxiv.org/abs/2104.00613 - // Next ID 37 - message DeepMACMaskEstimation { - // The loss used for penalizing mask predictions. - optional ClassificationLoss classification_loss = 1; - - // Weight of mask prediction loss - optional float task_loss_weight = 2 [default = 1.0]; - - // The dimension of the per-instance embedding. - optional int32 dim = 3 [default = 256]; - - // The dimension of the per-pixel embedding - optional int32 pixel_embedding_dim = 4 [default = 16]; - - // If set, masks are only kept for classes listed here. Masks are deleted - // for all other classes. Note that this is only done at training time, eval - // behavior is unchanged. - repeated int32 allowed_masked_classes_ids = 5; - - // The size of cropped pixel embedding that goes into the 2D mask prediction - // network (RoI align). - optional int32 mask_size = 6 [default = 32]; - - // If set to a positive value, we subsample instances by this amount to - // save memory during training. - optional int32 mask_num_subsamples = 67 [default = -1]; - - // Whether or not to use (x, y) coordinates as input to mask net. - optional bool use_xy = 8 [default = true]; - - // Defines the kind of architecture we want to use for mask network. - optional string network_type = 9 [default = "hourglass52"]; - - // Whether or not we want to use instance embedding in mask network. - optional bool use_instance_embedding = 10 [default = true]; - - // Number of channels in the inital block of the mask prediction network. - optional int32 num_init_channels = 11 [default = 64]; - - // Whether or not to predict masks at full resolution. If true, we predict - // masks at the resolution of the output stride. Otherwise, masks are - // predicted at resolution defined by mask_size - optional bool predict_full_resolution_masks = 12 [default = false]; - - // If predict_full_resolution_masks is set, this parameter controls the size - // of cropped masks returned by post-process. To be compatible with the rest - // of the API, masks are always cropped and resized according to detected - // boxes in postprocess. - optional int32 postprocess_crop_size = 13 [default = 256]; - - // The maximum relative amount by which boxes will be jittered before - // RoI crop happens. The x and y coordinates of the box are jittered - // relative to width and height respectively. - optional float max_roi_jitter_ratio = 14 [default = 0.0]; - - // The mode for jitterting box ROIs. See RandomJitterBoxes in - // preprocessor.proto for more details - optional RandomJitterBoxes.JitterMode jitter_mode = 15 [default = DEFAULT]; - - // Weight for the box consistency loss as described in the BoxInst paper - // https://arxiv.org/abs/2012.02310 - optional float box_consistency_loss_weight = 16 [default = 0.0]; - - optional float feature_consistency_threshold = 17 [default = 0.4]; - - optional int32 feature_consistency_dilation = 18 [default = 2]; - - optional float feature_consistency_loss_weight = 19 [default = 0.0]; - - optional LossNormalize box_consistency_loss_normalize = 20 - [default = NORMALIZE_AUTO]; - - // If set, will use the bounding box tightness prior approach. This means - // that the max will be restricted to only be inside the box for both - // dimensions. See details here: - // https://papers.nips.cc/paper/2019/hash/e6e713296627dff6475085cc6a224464-Abstract.html - optional bool box_consistency_tightness = 21 [default = false]; - - optional int32 feature_consistency_warmup_steps = 22 [default = 0]; - - optional int32 feature_consistency_warmup_start = 23 [default = 0]; - - // TODO(vighneshb) - optional FeatureConsistencyType feature_consistency_type = 35 - [default = CONSISTENCY_DEFAULT_LAB]; - - optional FeatureConsistencyComparison feature_consistency_comparison = 36 - [default = COMPARISON_DEFAULT_GAUSSIAN]; - - // This flag controls whether or not we use the outputs from only the - // last stage of the hourglass for training the mask-heads. - - // DeepMAC has been refactored to process the entire batch at once, - // instead of the previous (simple) approach of processing one sample at - // a time. Because of this, we need to set this flag to continue using - // the old models with the same training hardware. - - // This flag is not needed for 1024x1024 models. The performance and - // memory usage are same as before. - - // For 512x512 models - // - Setting this flag to true will let the model train on TPU-v3 32 - // chips. We observed a small (0.26 mAP) performance drop when doing so. - // - Setting this flag to false (default) increases the TPU requirement - // to TPU-v3 128 and reproduces previously demonstrated performance - // within error bars. - - optional bool use_only_last_stage = 24 [default = false]; - - optional float augmented_self_supervision_max_translation = 25 - [default = 0.0]; - - optional float augmented_self_supervision_flip_probability = 26 - [default = 0.0]; - - optional float augmented_self_supervision_loss_weight = 27 [default = 0.0]; - - optional int32 augmented_self_supervision_warmup_start = 28 [default = 0]; - - optional int32 augmented_self_supervision_warmup_steps = 29 [default = 0]; - - optional AugmentedSelfSupervisionLoss augmented_self_supervision_loss = 30 - [default = LOSS_DICE]; - - optional float augmented_self_supervision_scale_min = 31 [default = 1.0]; - - optional float augmented_self_supervision_scale_max = 32 [default = 1.0]; - - // The loss weight for the pointly supervised loss as defined in the paper - // https://arxiv.org/abs/2104.06404 - - // We assume that point supervision is given through a keypoint dataset, - // where each keypoint represents a sampled point, and its depth indicates - // whether it is a foreground or background point. - // Depth = +1 is assumed to be foreground and - // Depth = -1 is assumed to be background. - optional float pointly_supervised_keypoint_loss_weight = 33 [default = 0.0]; - - // When set, loss computation is ignored at pixels that fall within - // 2 boxes of the same class. - optional bool ignore_per_class_box_overlap = 34 [default = false]; - } - - optional DeepMACMaskEstimation deepmac_mask_estimation = 14; - - // CenterNet does not apply conventional post processing operations such as - // non max suppression as it applies a max-pool operator on box centers. - // However, in some cases we observe the need to remove duplicate predictions - // from CenterNet. Use this optional parameter to apply traditional non max - // suppression and score thresholding. - optional PostProcessing post_processing = 24; - - // If set, dictionary items returned by the predict() function - // are appended to the output of postprocess(). - optional bool output_prediction_dict = 25 [default = false]; -} - -enum LossNormalize { - NORMALIZE_AUTO = 0; // SUM for 2D inputs (dice loss) and MEAN for others. - NORMALIZE_GROUNDTRUTH_COUNT = 1; - NORMALIZE_BALANCED = 3; -} - -enum AugmentedSelfSupervisionLoss { - LOSS_UNSET = 0; - LOSS_DICE = 1; - LOSS_MSE = 2; - LOSS_KL_DIV = 3; -} - -enum FeatureConsistencyType { - CONSISTENCY_DEFAULT_LAB = 0; - CONSISTENCY_FEATURE_MAP = 1; -} - -enum FeatureConsistencyComparison { - COMPARISON_DEFAULT_GAUSSIAN = 0; - COMPARISON_NORMALIZED_DOTPROD = 1; -} - -message CenterNetFeatureExtractor { - optional string type = 1; - - // Channel means to be subtracted from each image channel. If not specified, - // we use a default value of 0. - repeated float channel_means = 2; - - // Channel standard deviations. Each channel will be normalized by dividing - // it by its standard deviation. If not specified, we use a default value - // of 1. - repeated float channel_stds = 3; - - // If set, will change channel order to be [blue, green, red]. This can be - // useful to be compatible with some pre-trained feature extractors. - optional bool bgr_ordering = 4 [default = false]; - - // If set, the feature upsampling layers will be constructed with - // separable convolutions. This is typically applied to feature pyramid - // network if any. - optional bool use_depthwise = 5 [default = false]; - - - // Depth multiplier. Only valid for specific models (e.g. MobileNet). See - // subclasses of `CenterNetFeatureExtractor`. - optional float depth_multiplier = 9 [default = 1.0]; - - // Whether to use separable convolutions. Only valid for specific - // models. See subclasses of `CenterNetFeatureExtractor`. - optional bool use_separable_conv = 10 [default = false]; - - // Which interpolation method to use for the upsampling ops in the FPN. - // Currently only valid for CenterNetMobileNetV2FPNFeatureExtractor. The value - // can be on of 'nearest' or 'bilinear'. - optional string upsampling_interpolation = 11 [default = 'nearest']; -} - diff --git a/research/object_detection/protos/eval.proto b/research/object_detection/protos/eval.proto deleted file mode 100644 index 2ffdfe921d8..00000000000 --- a/research/object_detection/protos/eval.proto +++ /dev/null @@ -1,172 +0,0 @@ -syntax = "proto2"; - -package object_detection.protos; - -// Message for configuring DetectionModel evaluation jobs (eval.py). -// Next id - 37 -message EvalConfig { - optional uint32 batch_size = 25 [default = 1]; - // Number of visualization images to generate. - optional uint32 num_visualizations = 1 [default = 10]; - - // Number of examples to process of evaluation. - optional uint32 num_examples = 2 [default = 5000, deprecated = true]; - - // How often to run evaluation. - optional uint32 eval_interval_secs = 3 [default = 300]; - - // Maximum number of times to run evaluation. If set to 0, will run forever. - optional uint32 max_evals = 4 [default = 0, deprecated = true]; - - // Whether the TensorFlow graph used for evaluation should be saved to disk. - optional bool save_graph = 5 [default = false]; - - // Path to directory to store visualizations in. If empty, visualization - // images are not exported (only shown on Tensorboard). - optional string visualization_export_dir = 6 [default = ""]; - - // BNS name of the TensorFlow master. - optional string eval_master = 7 [default = ""]; - - // Type of metrics to use for evaluation. - repeated string metrics_set = 8; - - // Type of metrics to use for evaluation. Unlike `metrics_set` above, this - // field allows configuring evaluation metric through config files. - repeated ParameterizedMetric parameterized_metric = 31; - - // Path to export detections to COCO compatible JSON format. - optional string export_path = 9 [default ='']; - - // Option to not read groundtruth labels and only export detections to - // COCO-compatible JSON file. - optional bool ignore_groundtruth = 10 [default = false]; - - // Use exponential moving averages of variables for evaluation. - // TODO(rathodv): When this is false make sure the model is constructed - // without moving averages in restore_fn. - optional bool use_moving_averages = 11 [default = false]; - - // Whether to evaluate instance masks. - // Note that since there is no evaluation code currently for instance - // segmentation this option is unused. - optional bool eval_instance_masks = 12 [default = false]; - - // Minimum score threshold for a detected object box to be visualized - optional float min_score_threshold = 13 [default = 0.5]; - - // Maximum number of detections to visualize - optional int32 max_num_boxes_to_visualize = 14 [default = 20]; - - // When drawing a single detection, each label is by default visualized as - //